Require target lra in gcc.c-torture/compile/asmgoto-6.c
[official-gcc.git] / gcc / cp / parser.cc
blob5e2b5cba57e8fac3809abc9d215a2d24371c4cb4
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48 #include "c-family/known-headers.h"
49 #include "contracts.h"
50 #include "bitmap.h"
53 /* The lexer. */
55 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
56 and c-lex.cc) and the C++ parser. */
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_AUTO, /* auto */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
185 RT_CO_YIELD /* co_yield */
188 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
189 reverting it on destruction. */
191 class type_id_in_expr_sentinel
193 cp_parser *parser;
194 bool saved;
195 public:
196 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
197 : parser (parser),
198 saved (parser->in_type_id_in_expr_p)
199 { parser->in_type_id_in_expr_p = set; }
200 ~type_id_in_expr_sentinel ()
201 { parser->in_type_id_in_expr_p = saved; }
204 /* Prototypes. */
206 static cp_lexer *cp_lexer_new_main
207 (void);
208 static cp_lexer *cp_lexer_new_from_tokens
209 (cp_token_cache *tokens);
210 static void cp_lexer_destroy
211 (cp_lexer *);
212 static int cp_lexer_saving_tokens
213 (const cp_lexer *);
214 static cp_token *cp_lexer_token_at
215 (cp_lexer *, cp_token_position);
216 static void cp_lexer_get_preprocessor_token
217 (unsigned, cp_token *);
218 static inline cp_token *cp_lexer_peek_token
219 (cp_lexer *);
220 static cp_token *cp_lexer_peek_nth_token
221 (cp_lexer *, size_t);
222 static inline bool cp_lexer_next_token_is
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_not
225 (cp_lexer *, enum cpp_ttype);
226 static bool cp_lexer_next_token_is_keyword
227 (cp_lexer *, enum rid);
228 static cp_token *cp_lexer_consume_token
229 (cp_lexer *);
230 static void cp_lexer_purge_token
231 (cp_lexer *);
232 static void cp_lexer_purge_tokens_after
233 (cp_lexer *, cp_token_position);
234 static void cp_lexer_save_tokens
235 (cp_lexer *);
236 static void cp_lexer_commit_tokens
237 (cp_lexer *);
238 static void cp_lexer_rollback_tokens
239 (cp_lexer *);
240 static void cp_lexer_print_token
241 (FILE *, cp_token *);
242 static inline bool cp_lexer_debugging_p
243 (cp_lexer *);
244 static void cp_lexer_start_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static void cp_lexer_stop_debugging
247 (cp_lexer *) ATTRIBUTE_UNUSED;
249 static cp_token_cache *cp_token_cache_new
250 (cp_token *, cp_token *);
251 static tree cp_parser_late_noexcept_specifier
252 (cp_parser *, tree);
253 static void noexcept_override_late_checks
254 (tree);
256 static void cp_parser_initial_pragma
257 (cp_token *);
259 static bool cp_parser_omp_declare_reduction_exprs
260 (tree, cp_parser *);
261 static void cp_finalize_oacc_routine
262 (cp_parser *, tree, bool);
264 /* 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, tf_warning_or_error);
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 (!flag_concepts_ts && parser->in_template_argument_list_p)
19827 pedwarn (token->location, 0,
19828 "use of %<auto%> in template argument "
19829 "only available with %<-fconcepts-ts%>");
19830 else if (!flag_concepts)
19831 pedwarn (token->location, 0,
19832 "use of %<auto%> in parameter declaration "
19833 "only available with %<-std=c++20%> or %<-fconcepts%>");
19834 else if (cxx_dialect < cxx14)
19835 error_at (token->location,
19836 "use of %<auto%> in parameter declaration "
19837 "only available with "
19838 "%<-std=c++14%> or %<-std=gnu++14%>");
19840 else
19841 type = make_auto ();
19842 break;
19844 case RID_DECLTYPE:
19845 /* Since DR 743, decltype can either be a simple-type-specifier by
19846 itself or begin a nested-name-specifier. Parsing it will replace
19847 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19848 handling below decide what to do. */
19849 cp_parser_decltype (parser);
19850 cp_lexer_set_token_position (parser->lexer, token);
19851 break;
19853 case RID_TYPEOF:
19854 /* Consume the `typeof' token. */
19855 cp_lexer_consume_token (parser->lexer);
19856 /* Parse the operand to `typeof'. */
19857 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
19858 /* If it is not already a TYPE, take its type. */
19859 if (!TYPE_P (type))
19860 type = finish_typeof (type);
19862 if (decl_specs)
19863 cp_parser_set_decl_spec_type (decl_specs, type,
19864 token,
19865 /*type_definition_p=*/false);
19867 return type;
19869 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
19870 case RID_##CODE:
19871 #include "cp-trait.def"
19872 #undef DEFTRAIT_TYPE
19873 type = cp_parser_trait (parser, token->keyword);
19874 if (decl_specs)
19875 cp_parser_set_decl_spec_type (decl_specs, type,
19876 token,
19877 /*type_definition_p=*/false);
19879 return type;
19881 default:
19882 break;
19885 /* If token is an already-parsed decltype not followed by ::,
19886 it's a simple-type-specifier. */
19887 if (token->type == CPP_DECLTYPE
19888 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
19890 type = saved_checks_value (token->u.tree_check_value);
19891 if (decl_specs)
19893 cp_parser_set_decl_spec_type (decl_specs, type,
19894 token,
19895 /*type_definition_p=*/false);
19896 /* Remember that we are handling a decltype in order to
19897 implement the resolution of DR 1510 when the argument
19898 isn't instantiation dependent. */
19899 decl_specs->decltype_p = true;
19901 cp_lexer_consume_token (parser->lexer);
19902 return type;
19905 /* If the type-specifier was for a built-in type, we're done. */
19906 if (type)
19908 /* Record the type. */
19909 if (decl_specs
19910 && (token->keyword != RID_SIGNED
19911 && token->keyword != RID_UNSIGNED
19912 && token->keyword != RID_SHORT
19913 && token->keyword != RID_LONG))
19914 cp_parser_set_decl_spec_type (decl_specs,
19915 type,
19916 token,
19917 /*type_definition_p=*/false);
19918 if (decl_specs)
19919 decl_specs->any_specifiers_p = true;
19921 /* Consume the token. */
19922 cp_lexer_consume_token (parser->lexer);
19924 if (type == error_mark_node)
19925 return error_mark_node;
19927 /* There is no valid C++ program where a non-template type is
19928 followed by a "<". That usually indicates that the user thought
19929 that the type was a template. */
19930 cp_parser_check_for_invalid_template_id (parser, type, none_type,
19931 token->location);
19933 return TYPE_NAME (type);
19936 /* The type-specifier must be a user-defined type. */
19937 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
19939 bool qualified_p;
19940 bool global_p;
19941 const bool typename_p = (cxx_dialect >= cxx20
19942 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
19944 /* Don't gobble tokens or issue error messages if this is an
19945 optional type-specifier. */
19946 if (flags & CP_PARSER_FLAGS_OPTIONAL)
19947 cp_parser_parse_tentatively (parser);
19949 /* Remember current tentative parsing state -- if we know we need
19950 a type, we can give better diagnostics here. */
19951 bool tent = cp_parser_parsing_tentatively (parser);
19953 token = cp_lexer_peek_token (parser->lexer);
19955 /* Look for the optional `::' operator. */
19956 global_p
19957 = (cp_parser_global_scope_opt (parser,
19958 /*current_scope_valid_p=*/false)
19959 != NULL_TREE);
19960 /* Look for the nested-name specifier. */
19961 qualified_p
19962 = (cp_parser_nested_name_specifier_opt (parser,
19963 /*typename_keyword_p=*/false,
19964 /*check_dependency_p=*/true,
19965 /*type_p=*/false,
19966 /*is_declaration=*/false)
19967 != NULL_TREE);
19968 /* If we have seen a nested-name-specifier, and the next token
19969 is `template', then we are using the template-id production. */
19970 if (parser->scope
19971 && cp_parser_optional_template_keyword (parser))
19973 /* Look for the template-id. */
19974 type = cp_parser_template_id (parser,
19975 /*template_keyword_p=*/true,
19976 /*check_dependency_p=*/true,
19977 none_type,
19978 /*is_declaration=*/false);
19979 /* If the template-id did not name a type, we are out of
19980 luck. */
19981 if (TREE_CODE (type) != TYPE_DECL)
19983 /* ...unless we pretend we have seen 'typename'. */
19984 if (typename_p)
19985 type = cp_parser_make_typename_type (parser, type,
19986 token->location);
19987 else
19989 cp_parser_error (parser, "expected template-id for type");
19990 type = error_mark_node;
19994 /* DR 1812: A < following a qualified-id in a typename-specifier
19995 could safely be assumed to begin a template argument list, so
19996 the template keyword should be optional. */
19997 else if (parser->scope
19998 && qualified_p
19999 && typename_p
20000 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
20002 cp_parser_parse_tentatively (parser);
20004 type = cp_parser_template_id (parser,
20005 /*template_keyword_p=*/true,
20006 /*check_dependency_p=*/true,
20007 none_type,
20008 /*is_declaration=*/false);
20009 /* This is handled below, so back off. */
20010 if (type && concept_check_p (type))
20011 cp_parser_simulate_error (parser);
20013 if (!cp_parser_parse_definitely (parser))
20014 type = NULL_TREE;
20015 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
20016 type = make_typename_type (parser->scope, type, typename_type,
20017 /*complain=*/tf_error);
20018 else if (TREE_CODE (type) != TYPE_DECL)
20019 type = NULL_TREE;
20022 /* Otherwise, look for a type-name. */
20023 if (!type)
20025 if (cxx_dialect >= cxx17)
20026 cp_parser_parse_tentatively (parser);
20028 type = cp_parser_type_name (parser, (qualified_p && typename_p));
20030 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
20031 type = NULL_TREE;
20034 if (!type && flag_concepts && decl_specs)
20036 /* Try for a type-constraint with template arguments. We check
20037 decl_specs here to avoid trying this for a functional cast. */
20039 cp_parser_parse_tentatively (parser);
20041 type = cp_parser_template_id (parser,
20042 /*template_keyword_p=*/false,
20043 /*check_dependency_p=*/true,
20044 none_type,
20045 /*is_declaration=*/false);
20046 if (type && concept_check_p (type))
20048 location_t loc = EXPR_LOCATION (type);
20049 type = cp_parser_placeholder_type_specifier (parser, loc,
20050 type, tent);
20051 if (tent && type == error_mark_node)
20052 /* Perhaps it's a concept-check expression. */
20053 cp_parser_simulate_error (parser);
20055 else
20056 cp_parser_simulate_error (parser);
20058 if (!cp_parser_parse_definitely (parser))
20059 type = NULL_TREE;
20062 if (!type && cxx_dialect >= cxx17)
20064 /* Try class template argument deduction or type-constraint without
20065 template arguments. */
20066 tree name = cp_parser_identifier (parser);
20067 if (name && TREE_CODE (name) == IDENTIFIER_NODE
20068 && parser->scope != error_mark_node)
20070 location_t loc
20071 = cp_lexer_previous_token (parser->lexer)->location;
20072 tree tmpl = cp_parser_lookup_name (parser, name,
20073 none_type,
20074 /*is_template=*/false,
20075 /*is_namespace=*/false,
20076 /*check_dependency=*/true,
20077 /*ambiguous_decls=*/NULL,
20078 token->location);
20079 if (tmpl && tmpl != error_mark_node
20080 && ctad_template_p (tmpl))
20081 type = make_template_placeholder (tmpl);
20082 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
20083 type = cp_parser_placeholder_type_specifier (parser, loc,
20084 tmpl, tent);
20085 else
20087 type = error_mark_node;
20088 if (!cp_parser_simulate_error (parser))
20089 cp_parser_name_lookup_error (parser, name, tmpl,
20090 NLE_TYPE, token->location);
20093 else
20094 type = error_mark_node;
20097 /* If it didn't work out, we don't have a TYPE. */
20098 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
20099 && !cp_parser_parse_definitely (parser))
20100 type = NULL_TREE;
20102 /* Keep track of all name-lookups performed in class scopes. */
20103 if (type
20104 && !global_p
20105 && !qualified_p
20106 && TREE_CODE (type) == TYPE_DECL
20107 && identifier_p (DECL_NAME (type)))
20108 maybe_note_name_used_in_class (DECL_NAME (type), type);
20110 if (type && decl_specs)
20111 cp_parser_set_decl_spec_type (decl_specs, type,
20112 token,
20113 /*type_definition_p=*/false);
20116 /* If we didn't get a type-name, issue an error message. */
20117 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20119 cp_parser_error (parser, "expected type-name");
20120 return error_mark_node;
20123 if (type && type != error_mark_node)
20125 /* See if TYPE is an Objective-C type, and if so, parse and
20126 accept any protocol references following it. Do this before
20127 the cp_parser_check_for_invalid_template_id() call, because
20128 Objective-C types can be followed by '<...>' which would
20129 enclose protocol names rather than template arguments, and so
20130 everything is fine. */
20131 if (c_dialect_objc () && !parser->scope
20132 && (objc_is_id (type) || objc_is_class_name (type)))
20134 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20135 tree qual_type = objc_get_protocol_qualified_type (type, protos);
20137 /* Clobber the "unqualified" type previously entered into
20138 DECL_SPECS with the new, improved protocol-qualified version. */
20139 if (decl_specs)
20140 decl_specs->type = qual_type;
20142 return qual_type;
20145 /* There is no valid C++ program where a non-template type is
20146 followed by a "<". That usually indicates that the user
20147 thought that the type was a template. */
20148 cp_parser_check_for_invalid_template_id (parser, type,
20149 none_type,
20150 token->location);
20153 return type;
20156 /* Parse the remainder of a placholder-type-specifier.
20158 placeholder-type-specifier:
20159 type-constraint_opt auto
20160 type-constraint_opt decltype(auto)
20162 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20163 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20164 parses the placeholder type, and performs some contextual syntactic analysis.
20166 LOC provides the location of the template name.
20168 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20169 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20170 might actually be a concept-check,
20172 Note that the Concepts TS allows the auto or decltype(auto) to be
20173 omitted in a constrained-type-specifier. */
20175 static tree
20176 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20177 tree tmpl, bool tentative)
20179 if (tmpl == error_mark_node)
20180 return error_mark_node;
20182 tree orig_tmpl = tmpl;
20184 /* Get the arguments as written for subsequent analysis. */
20185 tree args = NULL_TREE;
20186 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20188 args = TREE_OPERAND (tmpl, 1);
20189 tmpl = TREE_OPERAND (tmpl, 0);
20191 else
20192 /* A concept-name with no arguments can't be an expression. */
20193 tentative = false;
20195 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20197 /* Get the concept and prototype parameter for the constraint. */
20198 tree_pair info = finish_type_constraints (tmpl, args, complain);
20199 tree con = info.first;
20200 tree proto = info.second;
20201 if (con == error_mark_node)
20202 return error_mark_node;
20204 /* As per the standard, require auto or decltype(auto), except in some
20205 cases (template parameter lists, -fconcepts-ts enabled). */
20206 cp_token *placeholder = NULL, *close_paren = NULL;
20207 if (cxx_dialect >= cxx20)
20209 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20210 placeholder = cp_lexer_consume_token (parser->lexer);
20211 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20213 placeholder = cp_lexer_consume_token (parser->lexer);
20214 matching_parens parens;
20215 parens.require_open (parser);
20216 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20217 close_paren = parens.require_close (parser);
20221 /* A type constraint constrains a contextually determined type or type
20222 parameter pack. However, the Concepts TS does allow concepts
20223 to introduce non-type and template template parameters. */
20224 if (TREE_CODE (proto) != TYPE_DECL)
20226 if (!flag_concepts_ts
20227 || !processing_template_parmlist)
20229 if (!tentative)
20231 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20232 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20234 return error_mark_node;
20238 /* In a template parameter list, a type-parameter can be introduced
20239 by type-constraints alone. */
20240 if (processing_template_parmlist && !placeholder)
20242 /* In a default argument we may not be creating new parameters. */
20243 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20245 /* If this assert turns out to be false, do error() instead. */
20246 gcc_assert (tentative);
20247 return error_mark_node;
20249 return build_constrained_parameter (con, proto, args);
20252 /* Diagnose issues placeholder issues. */
20253 if (!flag_concepts_ts
20254 && !parser->in_result_type_constraint_p
20255 && !placeholder)
20257 if (tentative)
20258 /* Perhaps it's a concept-check expression (c++/91073). */
20259 return error_mark_node;
20261 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20262 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20263 error_at (input_location,
20264 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20265 /* Fall through. This is an error of omission. */
20267 else if (parser->in_result_type_constraint_p && placeholder)
20269 /* A trailing return type only allows type-constraints. */
20270 error_at (input_location,
20271 "unexpected placeholder in constrained result type");
20274 /* In a parameter-declaration-clause, a placeholder-type-specifier
20275 results in an invented template parameter. */
20276 if (parser->auto_is_implicit_function_template_parm_p)
20278 if (close_paren)
20280 location_t loc = make_location (placeholder->location,
20281 placeholder->location,
20282 close_paren->location);
20283 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20284 return error_mark_node;
20286 tree parm = build_constrained_parameter (con, proto, args);
20287 return synthesize_implicit_template_parm (parser, parm);
20290 /* Determine if the type should be deduced using template argument
20291 deduction or decltype deduction. Note that the latter is always
20292 used for type-constraints in trailing return types. */
20293 bool decltype_p = placeholder
20294 ? placeholder->keyword == RID_DECLTYPE
20295 : parser->in_result_type_constraint_p;
20297 /* Otherwise, this is the type of a variable or return type. */
20298 if (decltype_p)
20299 return make_constrained_decltype_auto (con, args);
20300 else
20301 return make_constrained_auto (con, args);
20304 /* Parse a type-name.
20306 type-name:
20307 class-name
20308 enum-name
20309 typedef-name
20310 simple-template-id [in c++0x]
20312 enum-name:
20313 identifier
20315 typedef-name:
20316 identifier
20318 Concepts:
20320 type-name:
20321 concept-name
20322 partial-concept-id
20324 concept-name:
20325 identifier
20327 Returns a TYPE_DECL for the type. */
20329 static tree
20330 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20332 tree type_decl;
20334 /* We can't know yet whether it is a class-name or not. */
20335 cp_parser_parse_tentatively (parser);
20336 /* Try a class-name. */
20337 type_decl = cp_parser_class_name (parser,
20338 typename_keyword_p,
20339 /*template_keyword_p=*/false,
20340 none_type,
20341 /*check_dependency_p=*/true,
20342 /*class_head_p=*/false,
20343 /*is_declaration=*/false);
20344 /* If it's not a class-name, keep looking. */
20345 if (!cp_parser_parse_definitely (parser))
20347 if (cxx_dialect < cxx11)
20348 /* It must be a typedef-name or an enum-name. */
20349 return cp_parser_nonclass_name (parser);
20351 cp_parser_parse_tentatively (parser);
20352 /* It is either a simple-template-id representing an
20353 instantiation of an alias template... */
20354 type_decl = cp_parser_template_id (parser,
20355 /*template_keyword_p=*/false,
20356 /*check_dependency_p=*/true,
20357 none_type,
20358 /*is_declaration=*/false);
20359 /* Note that this must be an instantiation of an alias template
20360 because [temp.names]/6 says:
20362 A template-id that names an alias template specialization
20363 is a type-name.
20365 Whereas [temp.names]/7 says:
20367 A simple-template-id that names a class template
20368 specialization is a class-name.
20370 With concepts, this could also be a partial-concept-id that
20371 declares a non-type template parameter. */
20372 if (type_decl != NULL_TREE
20373 && TREE_CODE (type_decl) == TYPE_DECL
20374 && TYPE_DECL_ALIAS_P (type_decl))
20375 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20376 else
20377 cp_parser_simulate_error (parser);
20379 if (!cp_parser_parse_definitely (parser))
20380 /* ... Or a typedef-name or an enum-name. */
20381 return cp_parser_nonclass_name (parser);
20384 return type_decl;
20387 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20388 or a concept-name.
20390 enum-name:
20391 identifier
20393 typedef-name:
20394 identifier
20396 concept-name:
20397 identifier
20399 Returns a TYPE_DECL for the type. */
20401 static tree
20402 cp_parser_nonclass_name (cp_parser* parser)
20404 tree type_decl;
20405 tree identifier;
20407 cp_token *token = cp_lexer_peek_token (parser->lexer);
20408 identifier = cp_parser_identifier (parser);
20409 if (identifier == error_mark_node)
20410 return error_mark_node;
20412 /* Look up the type-name. */
20413 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20415 type_decl = strip_using_decl (type_decl);
20417 if (TREE_CODE (type_decl) != TYPE_DECL
20418 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20420 /* See if this is an Objective-C type. */
20421 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20422 tree type = objc_get_protocol_qualified_type (identifier, protos);
20423 if (type)
20424 type_decl = TYPE_NAME (type);
20427 /* Issue an error if we did not find a type-name. */
20428 if (TREE_CODE (type_decl) != TYPE_DECL
20429 /* In Objective-C, we have the complication that class names are
20430 normally type names and start declarations (eg, the
20431 "NSObject" in "NSObject *object;"), but can be used in an
20432 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20433 is an expression. So, a classname followed by a dot is not a
20434 valid type-name. */
20435 || (objc_is_class_name (TREE_TYPE (type_decl))
20436 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20438 if (!cp_parser_simulate_error (parser))
20439 cp_parser_name_lookup_error (parser, identifier, type_decl,
20440 NLE_TYPE, token->location);
20441 return error_mark_node;
20443 /* Remember that the name was used in the definition of the
20444 current class so that we can check later to see if the
20445 meaning would have been different after the class was
20446 entirely defined. */
20447 else if (type_decl != error_mark_node
20448 && !parser->scope)
20449 maybe_note_name_used_in_class (identifier, type_decl);
20451 return type_decl;
20454 /* Parse an elaborated-type-specifier. Note that the grammar given
20455 here incorporates the resolution to DR68.
20457 elaborated-type-specifier:
20458 class-key :: [opt] nested-name-specifier [opt] identifier
20459 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20460 enum-key :: [opt] nested-name-specifier [opt] identifier
20461 typename :: [opt] nested-name-specifier identifier
20462 typename :: [opt] nested-name-specifier template [opt]
20463 template-id
20465 GNU extension:
20467 elaborated-type-specifier:
20468 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20469 class-key attributes :: [opt] nested-name-specifier [opt]
20470 template [opt] template-id
20471 enum attributes :: [opt] nested-name-specifier [opt] identifier
20473 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20474 declared `friend'. If IS_DECLARATION is TRUE, then this
20475 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20476 something is being declared.
20478 Returns the TYPE specified. */
20480 static tree
20481 cp_parser_elaborated_type_specifier (cp_parser* parser,
20482 bool is_friend,
20483 bool is_declaration)
20485 enum tag_types tag_type;
20486 tree identifier;
20487 tree type = NULL_TREE;
20488 tree attributes = NULL_TREE;
20489 tree globalscope;
20490 cp_token *token = NULL;
20492 /* For class and enum types the location of the class-key or enum-key. */
20493 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20494 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20495 rid scoped_key = RID_MAX;
20497 /* See if we're looking at the `enum' keyword. */
20498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20500 /* Consume the `enum' token. */
20501 cp_lexer_consume_token (parser->lexer);
20502 /* Remember that it's an enumeration type. */
20503 tag_type = enum_type;
20504 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20505 enums) is used here. */
20506 cp_token *token = cp_lexer_peek_token (parser->lexer);
20507 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20508 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20510 location_t loc = token->location;
20511 gcc_rich_location richloc (loc);
20512 richloc.add_range (input_location);
20513 richloc.add_fixit_remove ();
20514 pedwarn (&richloc, 0, "elaborated-type-specifier for "
20515 "a scoped enum must not use the %qD keyword",
20516 token->u.value);
20517 /* Consume the `struct' or `class' and parse it anyway. */
20518 cp_lexer_consume_token (parser->lexer);
20519 /* Create a combined location for the whole scoped-enum-key. */
20520 key_loc = make_location (key_loc, key_loc, loc);
20522 else
20523 scoped_key = RID_MAX;
20525 /* Parse the attributes. */
20526 attributes = cp_parser_attributes_opt (parser);
20528 /* Or, it might be `typename'. */
20529 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20530 RID_TYPENAME))
20532 /* Consume the `typename' token. */
20533 cp_lexer_consume_token (parser->lexer);
20534 /* Remember that it's a `typename' type. */
20535 tag_type = typename_type;
20537 /* Otherwise it must be a class-key. */
20538 else
20540 key_loc = cp_lexer_peek_token (parser->lexer)->location;
20541 tag_type = cp_parser_class_key (parser);
20542 if (tag_type == none_type)
20543 return error_mark_node;
20544 /* Parse the attributes. */
20545 attributes = cp_parser_attributes_opt (parser);
20548 /* Look for the `::' operator. */
20549 globalscope = cp_parser_global_scope_opt (parser,
20550 /*current_scope_valid_p=*/false);
20551 /* Look for the nested-name-specifier. */
20552 tree nested_name_specifier;
20553 if (tag_type == typename_type && !globalscope)
20555 nested_name_specifier
20556 = cp_parser_nested_name_specifier (parser,
20557 /*typename_keyword_p=*/true,
20558 /*check_dependency_p=*/true,
20559 /*type_p=*/true,
20560 is_declaration);
20561 if (!nested_name_specifier)
20562 return error_mark_node;
20564 else
20565 /* Even though `typename' is not present, the proposed resolution
20566 to Core Issue 180 says that in `class A<T>::B', `B' should be
20567 considered a type-name, even if `A<T>' is dependent. */
20568 nested_name_specifier
20569 = cp_parser_nested_name_specifier_opt (parser,
20570 /*typename_keyword_p=*/true,
20571 /*check_dependency_p=*/true,
20572 /*type_p=*/true,
20573 is_declaration);
20574 /* For everything but enumeration types, consider a template-id.
20575 For an enumeration type, consider only a plain identifier. */
20576 if (tag_type != enum_type)
20578 bool template_p = false;
20579 tree decl;
20581 /* Allow the `template' keyword. */
20582 template_p = cp_parser_optional_template_keyword (parser);
20583 /* If we didn't see `template', we don't know if there's a
20584 template-id or not. */
20585 if (!template_p)
20586 cp_parser_parse_tentatively (parser);
20587 /* The `template' keyword must follow a nested-name-specifier. */
20588 else if (!nested_name_specifier && !globalscope)
20590 cp_parser_error (parser, "%<template%> must follow a nested-"
20591 "name-specifier");
20592 return error_mark_node;
20595 /* Parse the template-id. */
20596 token = cp_lexer_peek_token (parser->lexer);
20597 decl = cp_parser_template_id (parser, template_p,
20598 /*check_dependency_p=*/true,
20599 tag_type,
20600 is_declaration);
20601 /* If we didn't find a template-id, look for an ordinary
20602 identifier. */
20603 if (!template_p && !cp_parser_parse_definitely (parser))
20605 /* We can get here when cp_parser_template_id, called by
20606 cp_parser_class_name with tag_type == none_type, succeeds
20607 and caches a BASELINK. Then, when called again here,
20608 instead of failing and returning an error_mark_node
20609 returns it (see template/typename17.C in C++11).
20610 ??? Could we diagnose this earlier? */
20611 else if (tag_type == typename_type && BASELINK_P (decl))
20613 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20614 type = error_mark_node;
20616 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20617 in effect, then we must assume that, upon instantiation, the
20618 template will correspond to a class. */
20619 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20620 && tag_type == typename_type)
20621 type = make_typename_type (parser->scope, decl,
20622 typename_type,
20623 /*complain=*/tf_error);
20624 /* If the `typename' keyword is in effect and DECL is not a type
20625 decl, then type is non existent. */
20626 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20628 else if (TREE_CODE (decl) == TYPE_DECL)
20630 type = check_elaborated_type_specifier (tag_type, decl,
20631 /*allow_template_p=*/true);
20633 /* If the next token is a semicolon, this must be a specialization,
20634 instantiation, or friend declaration. Check the scope while we
20635 still know whether or not we had a nested-name-specifier. */
20636 if (type != error_mark_node
20637 && !nested_name_specifier && !is_friend
20638 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20639 check_unqualified_spec_or_inst (type, token->location);
20641 else if (decl == error_mark_node)
20642 type = error_mark_node;
20645 if (!type)
20647 token = cp_lexer_peek_token (parser->lexer);
20648 identifier = cp_parser_identifier (parser);
20650 if (identifier == error_mark_node)
20652 parser->scope = NULL_TREE;
20653 return error_mark_node;
20656 /* For a `typename', we needn't call xref_tag. */
20657 if (tag_type == typename_type
20658 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20659 return cp_parser_make_typename_type (parser, identifier,
20660 token->location);
20662 /* Template parameter lists apply only if we are not within a
20663 function parameter list. */
20664 bool template_parm_lists_apply
20665 = parser->num_template_parameter_lists;
20666 if (template_parm_lists_apply)
20667 for (cp_binding_level *s = current_binding_level;
20668 s && s->kind != sk_template_parms;
20669 s = s->level_chain)
20670 if (s->kind == sk_function_parms)
20671 template_parm_lists_apply = false;
20673 /* Look up a qualified name in the usual way. */
20674 if (parser->scope)
20676 tree decl;
20677 tree ambiguous_decls;
20679 decl = cp_parser_lookup_name (parser, identifier,
20680 tag_type,
20681 /*is_template=*/false,
20682 /*is_namespace=*/false,
20683 /*check_dependency=*/true,
20684 &ambiguous_decls,
20685 token->location);
20687 /* If the lookup was ambiguous, an error will already have been
20688 issued. */
20689 if (ambiguous_decls)
20690 return error_mark_node;
20692 /* If we are parsing friend declaration, DECL may be a
20693 TEMPLATE_DECL tree node here. However, we need to check
20694 whether this TEMPLATE_DECL results in valid code. Consider
20695 the following example:
20697 namespace N {
20698 template <class T> class C {};
20700 class X {
20701 template <class T> friend class N::C; // #1, valid code
20703 template <class T> class Y {
20704 friend class N::C; // #2, invalid code
20707 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20708 name lookup of `N::C'. We see that friend declaration must
20709 be template for the code to be valid. Note that
20710 processing_template_decl does not work here since it is
20711 always 1 for the above two cases. */
20713 decl = (cp_parser_maybe_treat_template_as_class
20714 (decl, /*tag_name_p=*/is_friend
20715 && template_parm_lists_apply));
20717 if (TREE_CODE (decl) != TYPE_DECL)
20719 cp_parser_diagnose_invalid_type_name (parser,
20720 identifier,
20721 token->location);
20722 return error_mark_node;
20725 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20727 bool allow_template = (template_parm_lists_apply
20728 || DECL_SELF_REFERENCE_P (decl));
20729 type = check_elaborated_type_specifier (tag_type, decl,
20730 allow_template);
20732 if (type == error_mark_node)
20733 return error_mark_node;
20736 /* Forward declarations of nested types, such as
20738 class C1::C2;
20739 class C1::C2::C3;
20741 are invalid unless all components preceding the final '::'
20742 are complete. If all enclosing types are complete, these
20743 declarations become merely pointless.
20745 Invalid forward declarations of nested types are errors
20746 caught elsewhere in parsing. Those that are pointless arrive
20747 here. */
20749 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20750 && !is_friend && is_declaration
20751 && !processing_explicit_instantiation)
20752 warning (0, "declaration %qD does not declare anything", decl);
20754 type = TREE_TYPE (decl);
20756 else
20758 /* An elaborated-type-specifier sometimes introduces a new type and
20759 sometimes names an existing type. Normally, the rule is that it
20760 introduces a new type only if there is not an existing type of
20761 the same name already in scope. For example, given:
20763 struct S {};
20764 void f() { struct S s; }
20766 the `struct S' in the body of `f' is the same `struct S' as in
20767 the global scope; the existing definition is used. However, if
20768 there were no global declaration, this would introduce a new
20769 local class named `S'.
20771 An exception to this rule applies to the following code:
20773 namespace N { struct S; }
20775 Here, the elaborated-type-specifier names a new type
20776 unconditionally; even if there is already an `S' in the
20777 containing scope this declaration names a new type.
20778 This exception only applies if the elaborated-type-specifier
20779 forms the complete declaration:
20781 [class.name]
20783 A declaration consisting solely of `class-key identifier ;' is
20784 either a redeclaration of the name in the current scope or a
20785 forward declaration of the identifier as a class name. It
20786 introduces the name into the current scope.
20788 We are in this situation precisely when the next token is a `;'.
20790 An exception to the exception is that a `friend' declaration does
20791 *not* name a new type; i.e., given:
20793 struct S { friend struct T; };
20795 `T' is not a new type in the scope of `S'.
20797 Also, `new struct S' or `sizeof (struct S)' never results in the
20798 definition of a new type; a new type can only be declared in a
20799 declaration context. */
20801 TAG_how how;
20803 if (is_friend)
20804 /* Friends have special name lookup rules. */
20805 how = TAG_how::HIDDEN_FRIEND;
20806 else if (is_declaration
20807 && cp_lexer_next_token_is (parser->lexer,
20808 CPP_SEMICOLON))
20809 /* This is a `class-key identifier ;' */
20810 how = TAG_how::CURRENT_ONLY;
20811 else
20812 how = TAG_how::GLOBAL;
20814 bool template_p =
20815 (template_parm_lists_apply
20816 && (cp_parser_next_token_starts_class_definition_p (parser)
20817 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
20818 /* An unqualified name was used to reference this type, so
20819 there were no qualifying templates. */
20820 if (template_parm_lists_apply
20821 && !cp_parser_check_template_parameters (parser,
20822 /*num_templates=*/0,
20823 /*template_id*/false,
20824 token->location,
20825 /*declarator=*/NULL))
20826 return error_mark_node;
20828 type = xref_tag (tag_type, identifier, how, template_p);
20832 if (type == error_mark_node)
20833 return error_mark_node;
20835 /* Allow attributes on forward declarations of classes. */
20836 if (attributes)
20838 if (TREE_CODE (type) == TYPENAME_TYPE)
20839 warning (OPT_Wattributes,
20840 "attributes ignored on uninstantiated type");
20841 else if (tag_type != enum_type
20842 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
20843 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
20844 && ! processing_explicit_instantiation)
20845 warning (OPT_Wattributes,
20846 "attributes ignored on template instantiation");
20847 else if (is_friend && cxx11_attribute_p (attributes))
20849 if (warning (OPT_Wattributes, "attribute ignored"))
20850 inform (input_location, "an attribute that appertains to a friend "
20851 "declaration that is not a definition is ignored");
20853 else if (is_declaration && cp_parser_declares_only_class_p (parser))
20854 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
20855 else
20856 warning (OPT_Wattributes,
20857 "attributes ignored on elaborated-type-specifier that is "
20858 "not a forward declaration");
20861 if (tag_type == enum_type)
20862 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
20863 else
20865 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
20866 for alias definition. */
20867 bool decl_class = (is_declaration
20868 && cp_parser_declares_only_class_p (parser));
20869 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
20870 decl_class);
20872 /* Indicate whether this class was declared as a `class' or as a
20873 `struct'. */
20874 if (CLASS_TYPE_P (type) && !currently_open_class (type))
20875 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
20878 /* A "<" cannot follow an elaborated type specifier. If that
20879 happens, the user was probably trying to form a template-id. */
20880 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
20881 token->location);
20883 return type;
20886 /* Parse an enum-specifier.
20888 enum-specifier:
20889 enum-head { enumerator-list [opt] }
20890 enum-head { enumerator-list , } [C++0x]
20892 enum-head:
20893 enum-key identifier [opt] enum-base [opt]
20894 enum-key nested-name-specifier identifier enum-base [opt]
20896 enum-key:
20897 enum
20898 enum class [C++0x]
20899 enum struct [C++0x]
20901 enum-base: [C++0x]
20902 : type-specifier-seq
20904 opaque-enum-specifier:
20905 enum-key identifier enum-base [opt] ;
20907 GNU Extensions:
20908 enum-key attributes[opt] identifier [opt] enum-base [opt]
20909 { enumerator-list [opt] }attributes[opt]
20910 enum-key attributes[opt] identifier [opt] enum-base [opt]
20911 { enumerator-list, }attributes[opt] [C++0x]
20913 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20914 if the token stream isn't an enum-specifier after all. */
20916 static tree
20917 cp_parser_enum_specifier (cp_parser* parser)
20919 tree identifier;
20920 tree type = NULL_TREE;
20921 tree prev_scope;
20922 tree nested_name_specifier = NULL_TREE;
20923 tree attributes;
20924 bool scoped_enum_p = false;
20925 bool has_underlying_type = false;
20926 bool nested_being_defined = false;
20927 bool new_value_list = false;
20928 bool is_new_type = false;
20929 bool is_unnamed = false;
20930 tree underlying_type = NULL_TREE;
20931 cp_token *type_start_token = NULL;
20932 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
20934 /* Parse tentatively so that we can back up if we don't find a
20935 enum-specifier. */
20936 cp_parser_parse_tentatively (parser);
20938 /* Caller guarantees that the current token is 'enum', an identifier
20939 possibly follows, and the token after that is an opening brace.
20940 If we don't have an identifier, fabricate an anonymous name for
20941 the enumeration being defined. */
20942 cp_lexer_consume_token (parser->lexer);
20944 /* Parse the "class" or "struct", which indicates a scoped
20945 enumeration type in C++0x. */
20946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
20947 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
20949 if (cxx_dialect < cxx11)
20950 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20952 /* Consume the `struct' or `class' token. */
20953 cp_lexer_consume_token (parser->lexer);
20955 scoped_enum_p = true;
20958 attributes = cp_parser_attributes_opt (parser);
20960 /* Clear the qualification. */
20961 parser->scope = NULL_TREE;
20962 parser->qualifying_scope = NULL_TREE;
20963 parser->object_scope = NULL_TREE;
20965 /* Figure out in what scope the declaration is being placed. */
20966 prev_scope = current_scope ();
20968 type_start_token = cp_lexer_peek_token (parser->lexer);
20970 push_deferring_access_checks (dk_no_check);
20971 nested_name_specifier
20972 = cp_parser_nested_name_specifier_opt (parser,
20973 /*typename_keyword_p=*/true,
20974 /*check_dependency_p=*/false,
20975 /*type_p=*/false,
20976 /*is_declaration=*/false);
20978 if (nested_name_specifier)
20980 tree name;
20982 identifier = cp_parser_identifier (parser);
20983 name = cp_parser_lookup_name (parser, identifier,
20984 enum_type,
20985 /*is_template=*/false,
20986 /*is_namespace=*/false,
20987 /*check_dependency=*/true,
20988 /*ambiguous_decls=*/NULL,
20989 input_location);
20990 if (name && name != error_mark_node)
20992 type = TREE_TYPE (name);
20993 if (TREE_CODE (type) == TYPENAME_TYPE)
20995 /* Are template enums allowed in ISO? */
20996 if (template_parm_scope_p ())
20997 pedwarn (type_start_token->location, OPT_Wpedantic,
20998 "%qD is an enumeration template", name);
20999 /* ignore a typename reference, for it will be solved by name
21000 in start_enum. */
21001 type = NULL_TREE;
21004 else if (nested_name_specifier == error_mark_node)
21005 /* We already issued an error. */;
21006 else
21008 error_at (type_start_token->location,
21009 "%qD does not name an enumeration in %qT",
21010 identifier, nested_name_specifier);
21011 nested_name_specifier = error_mark_node;
21014 else
21016 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21017 identifier = cp_parser_identifier (parser);
21018 else
21020 identifier = make_anon_name ();
21021 is_unnamed = true;
21022 if (scoped_enum_p)
21023 error_at (type_start_token->location,
21024 "unnamed scoped enum is not allowed");
21027 pop_deferring_access_checks ();
21029 /* Check for the `:' that denotes a specified underlying type in C++0x.
21030 Note that a ':' could also indicate a bitfield width, however. */
21031 location_t colon_loc = UNKNOWN_LOCATION;
21032 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21034 cp_decl_specifier_seq type_specifiers;
21036 /* Consume the `:'. */
21037 colon_loc = cp_lexer_peek_token (parser->lexer)->location;
21038 cp_lexer_consume_token (parser->lexer);
21040 auto tdf
21041 = make_temp_override (parser->type_definition_forbidden_message,
21042 G_("types may not be defined in enum-base"));
21044 /* Parse the type-specifier-seq. */
21045 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
21046 /*is_declaration=*/false,
21047 /*is_trailing_return=*/false,
21048 &type_specifiers);
21050 /* At this point this is surely not elaborated type specifier. */
21051 if (!cp_parser_parse_definitely (parser))
21052 return NULL_TREE;
21054 if (cxx_dialect < cxx11)
21055 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21057 has_underlying_type = true;
21059 /* If that didn't work, stop. */
21060 if (type_specifiers.type != error_mark_node)
21062 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
21063 /*initialized=*/0, NULL);
21064 if (underlying_type == error_mark_node
21065 || check_for_bare_parameter_packs (underlying_type))
21066 underlying_type = NULL_TREE;
21070 /* Look for the `{' but don't consume it yet. */
21071 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21073 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
21075 if (has_underlying_type)
21076 cp_parser_commit_to_tentative_parse (parser);
21077 cp_parser_error (parser, "expected %<{%>");
21078 if (has_underlying_type)
21079 return error_mark_node;
21081 /* An opaque-enum-specifier must have a ';' here. */
21082 if ((scoped_enum_p || underlying_type)
21083 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21085 if (has_underlying_type)
21086 pedwarn (colon_loc,
21087 OPT_Welaborated_enum_base,
21088 "declaration of enumeration with "
21089 "fixed underlying type and no enumerator list is "
21090 "only permitted as a standalone declaration");
21091 else
21092 cp_parser_error (parser, "expected %<;%> or %<{%>");
21096 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
21097 return NULL_TREE;
21099 if (nested_name_specifier)
21101 if (CLASS_TYPE_P (nested_name_specifier))
21103 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
21104 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
21105 push_scope (nested_name_specifier);
21107 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21108 push_nested_namespace (nested_name_specifier);
21111 /* Issue an error message if type-definitions are forbidden here. */
21112 if (!cp_parser_check_type_definition (parser))
21113 type = error_mark_node;
21114 else
21115 /* Create the new type. We do this before consuming the opening
21116 brace so the enum will be recorded as being on the line of its
21117 tag (or the 'enum' keyword, if there is no tag). */
21118 type = start_enum (identifier, type, underlying_type,
21119 attributes, scoped_enum_p, &is_new_type);
21121 /* If the next token is not '{' it is an opaque-enum-specifier or an
21122 elaborated-type-specifier. */
21123 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21125 auto_timevar tv (TV_PARSE_ENUM);
21127 if (nested_name_specifier
21128 && nested_name_specifier != error_mark_node)
21130 /* The following catches invalid code such as:
21131 enum class S<int>::E { A, B, C }; */
21132 if (!processing_specialization
21133 && CLASS_TYPE_P (nested_name_specifier)
21134 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
21135 error_at (type_start_token->location, "cannot add an enumerator "
21136 "list to a template instantiation");
21138 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
21140 error_at (type_start_token->location,
21141 "%<%T::%E%> has not been declared",
21142 TYPE_CONTEXT (nested_name_specifier),
21143 nested_name_specifier);
21144 type = error_mark_node;
21146 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
21147 && !CLASS_TYPE_P (nested_name_specifier))
21149 error_at (type_start_token->location, "nested name specifier "
21150 "%qT for enum declaration does not name a class "
21151 "or namespace", nested_name_specifier);
21152 type = error_mark_node;
21154 /* If that scope does not contain the scope in which the
21155 class was originally declared, the program is invalid. */
21156 else if (prev_scope && !is_ancestor (prev_scope,
21157 nested_name_specifier))
21159 if (at_namespace_scope_p ())
21160 error_at (type_start_token->location,
21161 "declaration of %qD in namespace %qD which does not "
21162 "enclose %qD",
21163 type, prev_scope, nested_name_specifier);
21164 else
21165 error_at (type_start_token->location,
21166 "declaration of %qD in %qD which does not "
21167 "enclose %qD",
21168 type, prev_scope, nested_name_specifier);
21169 type = error_mark_node;
21171 /* If that scope is the scope where the declaration is being placed
21172 the program is invalid. */
21173 else if (CLASS_TYPE_P (nested_name_specifier)
21174 && CLASS_TYPE_P (prev_scope)
21175 && same_type_p (nested_name_specifier, prev_scope))
21177 permerror (type_start_token->location,
21178 "extra qualification not allowed");
21179 nested_name_specifier = NULL_TREE;
21183 if (scoped_enum_p)
21184 begin_scope (sk_scoped_enum, type);
21186 /* Consume the opening brace. */
21187 matching_braces braces;
21188 braces.consume_open (parser);
21190 if (type == error_mark_node)
21191 ; /* Nothing to add */
21192 else if (OPAQUE_ENUM_P (type)
21193 || (cxx_dialect > cxx98 && processing_specialization))
21195 new_value_list = true;
21196 SET_OPAQUE_ENUM_P (type, false);
21197 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21199 else
21201 error_at (type_start_token->location,
21202 "multiple definition of %q#T", type);
21203 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21204 "previous definition here");
21205 type = error_mark_node;
21208 if (type == error_mark_node)
21209 cp_parser_skip_to_end_of_block_or_statement (parser);
21210 /* If the next token is not '}', then there are some enumerators. */
21211 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21213 if (is_unnamed && !scoped_enum_p
21214 /* Don't warn for enum {} a; here. */
21215 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21216 pedwarn (type_start_token->location, OPT_Wpedantic,
21217 "ISO C++ forbids empty unnamed enum");
21219 else
21221 /* We've seen a '{' so we know we're in an enum-specifier.
21222 Commit to any tentative parse to get syntax errors. */
21223 cp_parser_commit_to_tentative_parse (parser);
21224 cp_parser_enumerator_list (parser, type);
21227 /* Consume the final '}'. */
21228 braces.require_close (parser);
21230 if (scoped_enum_p)
21231 finish_scope ();
21233 else
21235 /* If a ';' follows, then it is an opaque-enum-specifier
21236 and additional restrictions apply. */
21237 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21239 if (is_unnamed)
21240 error_at (type_start_token->location,
21241 "opaque-enum-specifier without name");
21242 else if (nested_name_specifier)
21243 error_at (type_start_token->location,
21244 "opaque-enum-specifier must use a simple identifier");
21248 /* Look for trailing attributes to apply to this enumeration, and
21249 apply them if appropriate. */
21250 if (cp_parser_allow_gnu_extensions_p (parser))
21252 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21253 cplus_decl_attributes (&type,
21254 trailing_attr,
21255 (int) ATTR_FLAG_TYPE_IN_PLACE);
21258 /* Finish up the enumeration. */
21259 if (type != error_mark_node)
21261 if (new_value_list)
21262 finish_enum_value_list (type);
21263 if (is_new_type)
21264 finish_enum (type);
21267 if (nested_name_specifier)
21269 if (CLASS_TYPE_P (nested_name_specifier))
21271 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21272 pop_scope (nested_name_specifier);
21274 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21275 pop_nested_namespace (nested_name_specifier);
21277 return type;
21280 /* Parse an enumerator-list. The enumerators all have the indicated
21281 TYPE.
21283 enumerator-list:
21284 enumerator-definition
21285 enumerator-list , enumerator-definition */
21287 static void
21288 cp_parser_enumerator_list (cp_parser* parser, tree type)
21290 while (true)
21292 /* Parse an enumerator-definition. */
21293 cp_parser_enumerator_definition (parser, type);
21295 /* If the next token is not a ',', we've reached the end of
21296 the list. */
21297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21298 break;
21299 /* Otherwise, consume the `,' and keep going. */
21300 cp_lexer_consume_token (parser->lexer);
21301 /* If the next token is a `}', there is a trailing comma. */
21302 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21304 if (cxx_dialect < cxx11)
21305 pedwarn (input_location, OPT_Wpedantic,
21306 "comma at end of enumerator list");
21307 break;
21312 /* Parse an enumerator-definition. The enumerator has the indicated
21313 TYPE.
21315 enumerator-definition:
21316 enumerator
21317 enumerator = constant-expression
21319 enumerator:
21320 identifier
21322 GNU Extensions:
21324 enumerator-definition:
21325 enumerator attributes [opt]
21326 enumerator attributes [opt] = constant-expression */
21328 static void
21329 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21331 tree identifier;
21332 tree value;
21333 location_t loc;
21335 /* Save the input location because we are interested in the location
21336 of the identifier and not the location of the explicit value. */
21337 loc = cp_lexer_peek_token (parser->lexer)->location;
21339 /* Look for the identifier. */
21340 identifier = cp_parser_identifier (parser);
21341 if (identifier == error_mark_node)
21342 return;
21344 /* Parse any specified attributes. */
21345 tree attrs = cp_parser_attributes_opt (parser);
21347 /* If the next token is an '=', then there is an explicit value. */
21348 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21350 /* Consume the `=' token. */
21351 cp_lexer_consume_token (parser->lexer);
21352 /* Parse the value. */
21353 value = cp_parser_constant_expression (parser);
21355 else
21356 value = NULL_TREE;
21358 /* If we are processing a template, make sure the initializer of the
21359 enumerator doesn't contain any bare template parameter pack. */
21360 if (current_lambda_expr ())
21362 /* In a lambda it should work, but doesn't currently. */
21363 if (uses_parameter_packs (value))
21365 sorry ("unexpanded parameter pack in enumerator in lambda");
21366 value = error_mark_node;
21369 else if (check_for_bare_parameter_packs (value))
21370 value = error_mark_node;
21372 /* Create the enumerator. */
21373 build_enumerator (identifier, value, type, attrs, loc);
21376 /* Parse a namespace-name.
21378 namespace-name:
21379 original-namespace-name
21380 namespace-alias
21382 Returns the NAMESPACE_DECL for the namespace. */
21384 static tree
21385 cp_parser_namespace_name (cp_parser* parser)
21387 tree identifier;
21388 tree namespace_decl;
21390 cp_token *token = cp_lexer_peek_token (parser->lexer);
21392 /* Get the name of the namespace. */
21393 identifier = cp_parser_identifier (parser);
21394 if (identifier == error_mark_node)
21395 return error_mark_node;
21397 /* Look up the identifier in the currently active scope. Look only
21398 for namespaces, due to:
21400 [basic.lookup.udir]
21402 When looking up a namespace-name in a using-directive or alias
21403 definition, only namespace names are considered.
21405 And:
21407 [basic.lookup.qual]
21409 During the lookup of a name preceding the :: scope resolution
21410 operator, object, function, and enumerator names are ignored.
21412 (Note that cp_parser_qualifying_entity only calls this
21413 function if the token after the name is the scope resolution
21414 operator.) */
21415 namespace_decl = cp_parser_lookup_name (parser, identifier,
21416 none_type,
21417 /*is_template=*/false,
21418 /*is_namespace=*/true,
21419 /*check_dependency=*/true,
21420 /*ambiguous_decls=*/NULL,
21421 token->location);
21422 /* If it's not a namespace, issue an error. */
21423 if (namespace_decl == error_mark_node
21424 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21426 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21428 auto_diagnostic_group d;
21429 name_hint hint;
21430 if (namespace_decl == error_mark_node
21431 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21432 hint = suggest_alternative_in_explicit_scope (token->location,
21433 identifier,
21434 parser->scope);
21435 if (const char *suggestion = hint.suggestion ())
21437 gcc_rich_location richloc (token->location);
21438 richloc.add_fixit_replace (suggestion);
21439 error_at (&richloc,
21440 "%qD is not a namespace-name; did you mean %qs?",
21441 identifier, suggestion);
21443 else
21444 error_at (token->location, "%qD is not a namespace-name",
21445 identifier);
21447 else
21448 cp_parser_error (parser, "expected namespace-name");
21449 namespace_decl = error_mark_node;
21452 return namespace_decl;
21455 /* Parse a namespace-definition.
21457 namespace-definition:
21458 named-namespace-definition
21459 unnamed-namespace-definition
21461 named-namespace-definition:
21462 original-namespace-definition
21463 extension-namespace-definition
21465 original-namespace-definition:
21466 namespace identifier { namespace-body }
21468 extension-namespace-definition:
21469 namespace original-namespace-name { namespace-body }
21471 unnamed-namespace-definition:
21472 namespace { namespace-body } */
21474 static void
21475 cp_parser_namespace_definition (cp_parser* parser)
21477 tree identifier;
21478 int nested_definition_count = 0;
21480 cp_ensure_no_omp_declare_simd (parser);
21481 cp_ensure_no_oacc_routine (parser);
21483 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21484 const bool topmost_inline_p = is_inline;
21486 if (is_inline)
21488 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21489 cp_lexer_consume_token (parser->lexer);
21492 /* Look for the `namespace' keyword. */
21493 cp_token* token
21494 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21496 /* Parse any specified attributes before the identifier. */
21497 tree attribs = cp_parser_attributes_opt (parser);
21499 for (;;)
21501 identifier = NULL_TREE;
21503 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21504 RID_INLINE);
21505 if (nested_inline_p && nested_definition_count != 0)
21507 if (pedantic && cxx_dialect < cxx20)
21508 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21509 OPT_Wc__20_extensions, "nested inline namespace "
21510 "definitions only available with %<-std=c++20%> or "
21511 "%<-std=gnu++20%>");
21512 cp_lexer_consume_token (parser->lexer);
21515 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21517 identifier = cp_parser_identifier (parser);
21519 if (cp_next_tokens_can_be_std_attribute_p (parser))
21520 pedwarn (input_location, OPT_Wpedantic,
21521 "standard attributes on namespaces must precede "
21522 "the namespace name");
21524 /* Parse any attributes specified after the identifier. */
21525 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21530 /* Don't forget that the innermost namespace might have been
21531 marked as inline. Use |= because we cannot overwrite
21532 IS_INLINE in case the outermost namespace is inline, but
21533 there are no nested inlines. */
21534 is_inline |= nested_inline_p;
21535 break;
21538 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21539 pedwarn (input_location, OPT_Wc__17_extensions,
21540 "nested namespace definitions only available with "
21541 "%<-std=c++17%> or %<-std=gnu++17%>");
21543 /* Nested namespace names can create new namespaces (unlike
21544 other qualified-ids). */
21545 if (int count = (identifier
21546 ? push_namespace (identifier, nested_inline_p)
21547 : 0))
21548 nested_definition_count += count;
21549 else
21550 cp_parser_error (parser, "nested namespace name required");
21551 cp_lexer_consume_token (parser->lexer);
21554 if (nested_definition_count && !identifier)
21555 cp_parser_error (parser, "namespace name required");
21557 if (nested_definition_count && attribs)
21558 error_at (token->location,
21559 "a nested namespace definition cannot have attributes");
21560 if (nested_definition_count && topmost_inline_p)
21561 error_at (token->location,
21562 "a nested namespace definition cannot be inline");
21564 /* Start the namespace. */
21565 nested_definition_count += push_namespace (identifier, is_inline);
21567 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21569 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21571 /* Look for the `{' to validate starting the namespace. */
21572 matching_braces braces;
21573 if (braces.require_open (parser))
21575 /* Parse the body of the namespace. */
21576 cp_parser_namespace_body (parser);
21578 /* Look for the final `}'. */
21579 braces.require_close (parser);
21582 if (has_visibility)
21583 pop_visibility (1);
21585 /* Pop the nested namespace definitions. */
21586 while (nested_definition_count--)
21587 pop_namespace ();
21590 /* Parse a namespace-body.
21592 namespace-body:
21593 declaration-seq [opt] */
21595 static void
21596 cp_parser_namespace_body (cp_parser* parser)
21598 cp_parser_declaration_seq_opt (parser);
21601 /* Parse a namespace-alias-definition.
21603 namespace-alias-definition:
21604 namespace identifier = qualified-namespace-specifier ; */
21606 static void
21607 cp_parser_namespace_alias_definition (cp_parser* parser)
21609 tree identifier;
21610 tree namespace_specifier;
21612 cp_token *token = cp_lexer_peek_token (parser->lexer);
21614 /* Look for the `namespace' keyword. */
21615 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21616 /* Look for the identifier. */
21617 identifier = cp_parser_identifier (parser);
21618 if (identifier == error_mark_node)
21619 return;
21620 /* Look for the `=' token. */
21621 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21622 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21624 error_at (token->location, "%<namespace%> definition is not allowed here");
21625 /* Skip the definition. */
21626 cp_lexer_consume_token (parser->lexer);
21627 if (cp_parser_skip_to_closing_brace (parser))
21628 cp_lexer_consume_token (parser->lexer);
21629 return;
21631 cp_parser_require (parser, CPP_EQ, RT_EQ);
21632 /* Look for the qualified-namespace-specifier. */
21633 namespace_specifier
21634 = cp_parser_qualified_namespace_specifier (parser);
21635 cp_warn_deprecated_use_scopes (namespace_specifier);
21636 /* Look for the `;' token. */
21637 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21639 /* Register the alias in the symbol table. */
21640 do_namespace_alias (identifier, namespace_specifier);
21643 /* Parse a qualified-namespace-specifier.
21645 qualified-namespace-specifier:
21646 :: [opt] nested-name-specifier [opt] namespace-name
21648 Returns a NAMESPACE_DECL corresponding to the specified
21649 namespace. */
21651 static tree
21652 cp_parser_qualified_namespace_specifier (cp_parser* parser)
21654 /* Look for the optional `::'. */
21655 cp_parser_global_scope_opt (parser,
21656 /*current_scope_valid_p=*/false);
21658 /* Look for the optional nested-name-specifier. */
21659 cp_parser_nested_name_specifier_opt (parser,
21660 /*typename_keyword_p=*/false,
21661 /*check_dependency_p=*/true,
21662 /*type_p=*/false,
21663 /*is_declaration=*/true);
21665 return cp_parser_namespace_name (parser);
21668 /* Subroutine of cp_parser_using_declaration. */
21670 static tree
21671 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21673 tree decl = NULL_TREE;
21674 if (at_class_scope_p ())
21676 /* Create the USING_DECL. */
21677 decl = do_class_using_decl (qscope, identifier);
21679 if (check_for_bare_parameter_packs (decl))
21680 return error_mark_node;
21682 if (decl && typename_p)
21683 USING_DECL_TYPENAME_P (decl) = 1;
21685 /* Add it to the list of members in this class. */
21686 finish_member_declaration (decl);
21688 else
21689 finish_nonmember_using_decl (qscope, identifier);
21690 return decl;
21693 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21694 access declaration.
21696 using-declaration:
21697 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21698 using :: unqualified-id ;
21700 access-declaration:
21701 qualified-id ;
21705 static bool
21706 cp_parser_using_declaration (cp_parser* parser,
21707 bool access_declaration_p)
21709 cp_token *token;
21710 bool typename_p = false;
21711 bool global_scope_p;
21712 tree identifier;
21713 tree qscope;
21714 int oldcount = errorcount;
21715 cp_token *diag_token = NULL;
21717 if (access_declaration_p)
21719 diag_token = cp_lexer_peek_token (parser->lexer);
21720 cp_parser_parse_tentatively (parser);
21722 else
21724 /* Look for the `using' keyword. */
21725 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21727 again:
21728 /* Peek at the next token. */
21729 token = cp_lexer_peek_token (parser->lexer);
21730 /* See if it's `typename'. */
21731 if (token->keyword == RID_TYPENAME)
21733 /* Remember that we've seen it. */
21734 typename_p = true;
21735 /* Consume the `typename' token. */
21736 cp_lexer_consume_token (parser->lexer);
21740 /* Look for the optional global scope qualification. */
21741 global_scope_p
21742 = (cp_parser_global_scope_opt (parser,
21743 /*current_scope_valid_p=*/false)
21744 != NULL_TREE);
21746 /* If we saw `typename', or didn't see `::', then there must be a
21747 nested-name-specifier present. */
21748 if (typename_p || !global_scope_p)
21750 qscope = cp_parser_nested_name_specifier (parser, typename_p,
21751 /*check_dependency_p=*/true,
21752 /*type_p=*/false,
21753 /*is_declaration=*/true);
21754 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21756 cp_parser_skip_to_end_of_block_or_statement (parser);
21757 return false;
21760 /* Otherwise, we could be in either of the two productions. In that
21761 case, treat the nested-name-specifier as optional. */
21762 else
21763 qscope = cp_parser_nested_name_specifier_opt (parser,
21764 /*typename_keyword_p=*/false,
21765 /*check_dependency_p=*/true,
21766 /*type_p=*/false,
21767 /*is_declaration=*/true);
21768 if (!qscope)
21769 qscope = global_namespace;
21771 cp_warn_deprecated_use_scopes (qscope);
21773 if (access_declaration_p
21774 && !MAYBE_CLASS_TYPE_P (qscope)
21775 && TREE_CODE (qscope) != ENUMERAL_TYPE)
21776 /* If the qualifying scope of an access-declaration isn't a class
21777 or enumeration type then it can't be valid. */
21778 cp_parser_simulate_error (parser);
21780 if (access_declaration_p && cp_parser_error_occurred (parser))
21781 /* Something has already gone wrong; there's no need to parse
21782 further. Since an error has occurred, the return value of
21783 cp_parser_parse_definitely will be false, as required. */
21784 return cp_parser_parse_definitely (parser);
21786 token = cp_lexer_peek_token (parser->lexer);
21787 /* Parse the unqualified-id. */
21788 identifier = cp_parser_unqualified_id (parser,
21789 /*template_keyword_p=*/false,
21790 /*check_dependency_p=*/true,
21791 /*declarator_p=*/true,
21792 /*optional_p=*/false);
21794 if (access_declaration_p)
21796 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21797 cp_parser_simulate_error (parser);
21798 if (!cp_parser_parse_definitely (parser))
21799 return false;
21801 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21803 cp_token *ell = cp_lexer_consume_token (parser->lexer);
21804 if (cxx_dialect < cxx17)
21805 pedwarn (ell->location, OPT_Wc__17_extensions,
21806 "pack expansion in using-declaration only available "
21807 "with %<-std=c++17%> or %<-std=gnu++17%>");
21809 /* A parameter pack can appear in the qualifying scope, and/or in the
21810 terminal name (if naming a conversion function). Logically they're
21811 part of a single pack expansion of the overall USING_DECL, but we
21812 express them as separate pack expansions within the USING_DECL since
21813 we can't create a pack expansion over a USING_DECL. */
21814 bool saw_parm_pack = false;
21815 if (uses_parameter_packs (qscope))
21817 qscope = make_pack_expansion (qscope);
21818 saw_parm_pack = true;
21820 if (identifier_p (identifier)
21821 && IDENTIFIER_CONV_OP_P (identifier)
21822 && uses_parameter_packs (TREE_TYPE (identifier)))
21824 identifier = make_conv_op_name (make_pack_expansion
21825 (TREE_TYPE (identifier)));
21826 saw_parm_pack = true;
21828 if (!saw_parm_pack)
21830 /* Issue an error in terms using a SCOPE_REF that includes both
21831 components. */
21832 tree name
21833 = build_qualified_name (NULL_TREE, qscope, identifier, false);
21834 make_pack_expansion (name);
21835 gcc_assert (seen_error ());
21836 qscope = identifier = error_mark_node;
21840 /* The function we call to handle a using-declaration is different
21841 depending on what scope we are in. */
21842 if (qscope == error_mark_node || identifier == error_mark_node)
21844 else if (!identifier_p (identifier)
21845 && TREE_CODE (identifier) != BIT_NOT_EXPR)
21846 /* [namespace.udecl]
21848 A using declaration shall not name a template-id. */
21849 error_at (token->location,
21850 "a template-id may not appear in a using-declaration");
21851 else
21853 tree decl = finish_using_decl (qscope, identifier, typename_p);
21855 if (decl == error_mark_node)
21857 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21858 return false;
21862 if (!access_declaration_p
21863 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21865 cp_token *comma = cp_lexer_consume_token (parser->lexer);
21866 if (cxx_dialect < cxx17)
21867 pedwarn (comma->location, OPT_Wc__17_extensions,
21868 "comma-separated list in using-declaration only available "
21869 "with %<-std=c++17%> or %<-std=gnu++17%>");
21870 goto again;
21873 /* Look for the final `;'. */
21874 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21876 if (access_declaration_p && errorcount == oldcount)
21877 warning_at (diag_token->location, OPT_Wdeprecated,
21878 "access declarations are deprecated "
21879 "in favour of using-declarations; "
21880 "suggestion: add the %<using%> keyword");
21882 return true;
21885 /* C++20 using enum declaration.
21887 using-enum-declaration :
21888 using elaborated-enum-specifier ; */
21890 static void
21891 cp_parser_using_enum (cp_parser *parser)
21893 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21895 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21896 breaks one of the motivating examples in using-enum-5.C.
21897 cp_parser_simple_type_specifier seems to be closer to what we actually
21898 want, though that hasn't been properly specified yet. */
21900 /* Consume 'enum'. */
21901 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
21902 cp_lexer_consume_token (parser->lexer);
21904 cp_token *start = cp_lexer_peek_token (parser->lexer);
21906 tree type = (cp_parser_simple_type_specifier
21907 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
21909 cp_token *end = cp_lexer_previous_token (parser->lexer);
21911 if (type == error_mark_node
21912 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21914 cp_parser_skip_to_end_of_block_or_statement (parser);
21915 return;
21917 if (TREE_CODE (type) == TYPE_DECL)
21918 type = TREE_TYPE (type);
21920 /* The elaborated-enum-specifier shall not name a dependent type and the type
21921 shall have a reachable enum-specifier. */
21922 const char *msg = nullptr;
21923 if (cxx_dialect < cxx20)
21924 msg = _("%<using enum%> "
21925 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21926 else if (dependent_type_p (type))
21927 msg = _("%<using enum%> of dependent type %qT");
21928 else if (TREE_CODE (type) != ENUMERAL_TYPE)
21929 msg = _("%<using enum%> of non-enumeration type %q#T");
21930 else if (!COMPLETE_TYPE_P (type))
21931 msg = _("%<using enum%> of incomplete type %qT");
21932 else if (OPAQUE_ENUM_P (type))
21933 msg = _("%<using enum%> of %qT before its enum-specifier");
21934 if (msg)
21936 location_t loc = make_location (start, start, end);
21937 auto_diagnostic_group g;
21938 error_at (loc, msg, type);
21939 loc = location_of (type);
21940 if (cxx_dialect < cxx20 || loc == input_location)
21942 else if (OPAQUE_ENUM_P (type))
21943 inform (loc, "opaque-enum-declaration here");
21944 else
21945 inform (loc, "declared here");
21948 /* A using-enum-declaration introduces the enumerator names of the named
21949 enumeration as if by a using-declaration for each enumerator. */
21950 if (TREE_CODE (type) == ENUMERAL_TYPE)
21951 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
21952 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
21955 /* Parse an alias-declaration.
21957 alias-declaration:
21958 using identifier attribute-specifier-seq [opt] = type-id */
21960 static tree
21961 cp_parser_alias_declaration (cp_parser* parser)
21963 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
21964 location_t id_location, type_location;
21965 cp_declarator *declarator;
21966 cp_decl_specifier_seq decl_specs;
21967 bool member_p;
21968 const char *saved_message = NULL;
21970 /* Look for the `using' keyword. */
21971 cp_token *using_token
21972 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
21973 if (using_token == NULL)
21974 return error_mark_node;
21976 id_location = cp_lexer_peek_token (parser->lexer)->location;
21977 id = cp_parser_identifier (parser);
21978 if (id == error_mark_node)
21979 return error_mark_node;
21981 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
21982 attributes = cp_parser_attributes_opt (parser);
21983 if (attributes == error_mark_node)
21984 return error_mark_node;
21986 cp_parser_require (parser, CPP_EQ, RT_EQ);
21988 if (cp_parser_error_occurred (parser))
21989 return error_mark_node;
21991 cp_parser_commit_to_tentative_parse (parser);
21993 /* Now we are going to parse the type-id of the declaration. */
21996 [dcl.type]/3 says:
21998 "A type-specifier-seq shall not define a class or enumeration
21999 unless it appears in the type-id of an alias-declaration (7.1.3) that
22000 is not the declaration of a template-declaration."
22002 In other words, if we currently are in an alias template, the
22003 type-id should not define a type.
22005 So let's set parser->type_definition_forbidden_message in that
22006 case; cp_parser_check_type_definition (called by
22007 cp_parser_class_specifier) will then emit an error if a type is
22008 defined in the type-id. */
22009 if (parser->num_template_parameter_lists)
22011 saved_message = parser->type_definition_forbidden_message;
22012 parser->type_definition_forbidden_message =
22013 G_("types may not be defined in alias template declarations");
22016 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22017 &type_location);
22019 /* Restore the error message if need be. */
22020 if (parser->num_template_parameter_lists)
22021 parser->type_definition_forbidden_message = saved_message;
22023 if (type == error_mark_node
22024 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22026 cp_parser_skip_to_end_of_block_or_statement (parser);
22027 return error_mark_node;
22030 /* A typedef-name can also be introduced by an alias-declaration. The
22031 identifier following the using keyword becomes a typedef-name. It has
22032 the same semantics as if it were introduced by the typedef
22033 specifier. In particular, it does not define a new type and it shall
22034 not appear in the type-id. */
22036 clear_decl_specs (&decl_specs);
22037 decl_specs.type = type;
22038 if (attributes != NULL_TREE)
22040 decl_specs.attributes = attributes;
22041 set_and_check_decl_spec_loc (&decl_specs,
22042 ds_attribute,
22043 attrs_token);
22045 set_and_check_decl_spec_loc (&decl_specs,
22046 ds_typedef,
22047 using_token);
22048 set_and_check_decl_spec_loc (&decl_specs,
22049 ds_alias,
22050 using_token);
22051 decl_specs.locations[ds_type_spec] = type_location;
22053 if (parser->num_template_parameter_lists
22054 && !cp_parser_check_template_parameters (parser,
22055 /*num_templates=*/0,
22056 /*template_id*/false,
22057 id_location,
22058 /*declarator=*/NULL))
22059 return error_mark_node;
22061 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
22063 member_p = at_class_scope_p ();
22064 if (member_p)
22065 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
22066 NULL_TREE, attributes);
22067 else
22068 decl = start_decl (declarator, &decl_specs, 0,
22069 attributes, NULL_TREE, &pushed_scope);
22070 if (decl == error_mark_node)
22071 return decl;
22073 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
22075 if (pushed_scope)
22076 pop_scope (pushed_scope);
22078 /* If decl is a template, return its TEMPLATE_DECL so that it gets
22079 added into the symbol table; otherwise, return the TYPE_DECL. */
22080 if (DECL_LANG_SPECIFIC (decl)
22081 && DECL_TEMPLATE_INFO (decl)
22082 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
22084 decl = DECL_TI_TEMPLATE (decl);
22085 if (member_p)
22086 check_member_template (decl);
22089 return decl;
22092 /* Parse a using-directive.
22094 using-directive:
22095 attribute-specifier-seq [opt] using namespace :: [opt]
22096 nested-name-specifier [opt] namespace-name ; */
22098 static void
22099 cp_parser_using_directive (cp_parser* parser)
22101 tree namespace_decl;
22102 tree attribs = cp_parser_std_attribute_spec_seq (parser);
22103 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22105 /* Error during attribute parsing that resulted in skipping
22106 to next semicolon. */
22107 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22108 return;
22111 /* Look for the `using' keyword. */
22112 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22113 /* And the `namespace' keyword. */
22114 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22115 /* Look for the optional `::' operator. */
22116 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22117 /* And the optional nested-name-specifier. */
22118 cp_parser_nested_name_specifier_opt (parser,
22119 /*typename_keyword_p=*/false,
22120 /*check_dependency_p=*/true,
22121 /*type_p=*/false,
22122 /*is_declaration=*/true);
22123 /* Get the namespace being used. */
22124 namespace_decl = cp_parser_namespace_name (parser);
22125 cp_warn_deprecated_use_scopes (namespace_decl);
22126 /* And any specified GNU attributes. */
22127 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22128 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
22130 /* Update the symbol table. */
22131 finish_using_directive (namespace_decl, attribs);
22133 /* Look for the final `;'. */
22134 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22137 /* Parse an asm-definition.
22139 asm-qualifier:
22140 volatile
22141 inline
22142 goto
22144 asm-qualifier-list:
22145 asm-qualifier
22146 asm-qualifier-list asm-qualifier
22148 asm-definition:
22149 asm ( string-literal ) ;
22151 GNU Extension:
22153 asm-definition:
22154 asm asm-qualifier-list [opt] ( string-literal ) ;
22155 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
22156 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22157 : asm-operand-list [opt] ) ;
22158 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22159 : asm-operand-list [opt]
22160 : asm-clobber-list [opt] ) ;
22161 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
22162 : asm-clobber-list [opt]
22163 : asm-goto-list ) ;
22165 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22166 contains goto, and is the only allowed form in that case. No duplicates are
22167 allowed in an asm-qualifier-list. */
22169 static void
22170 cp_parser_asm_definition (cp_parser* parser)
22172 tree outputs = NULL_TREE;
22173 tree inputs = NULL_TREE;
22174 tree clobbers = NULL_TREE;
22175 tree labels = NULL_TREE;
22176 tree asm_stmt;
22177 bool extended_p = false;
22178 bool invalid_inputs_p = false;
22179 bool invalid_outputs_p = false;
22180 required_token missing = RT_NONE;
22181 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
22183 /* Look for the `asm' keyword. */
22184 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
22186 /* In C++20, unevaluated inline assembly is permitted in constexpr
22187 functions. */
22188 if (parser->in_function_body
22189 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22190 && cxx_dialect < cxx20)
22191 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22192 "function only available with %<-std=c++20%> or "
22193 "%<-std=gnu++20%>");
22195 /* Handle the asm-qualifier-list. */
22196 location_t volatile_loc = UNKNOWN_LOCATION;
22197 location_t inline_loc = UNKNOWN_LOCATION;
22198 location_t goto_loc = UNKNOWN_LOCATION;
22199 location_t first_loc = UNKNOWN_LOCATION;
22201 if (cp_parser_allow_gnu_extensions_p (parser))
22202 for (;;)
22204 cp_token *token = cp_lexer_peek_token (parser->lexer);
22205 location_t loc = token->location;
22206 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22208 case RID_VOLATILE:
22209 if (volatile_loc)
22211 error_at (loc, "duplicate %<asm%> qualifier %qT",
22212 token->u.value);
22213 inform (volatile_loc, "first seen here");
22215 else
22217 if (!parser->in_function_body)
22218 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22219 "outside of function body", token->u.value);
22220 volatile_loc = loc;
22222 cp_lexer_consume_token (parser->lexer);
22223 continue;
22225 case RID_INLINE:
22226 if (inline_loc)
22228 error_at (loc, "duplicate %<asm%> qualifier %qT",
22229 token->u.value);
22230 inform (inline_loc, "first seen here");
22232 else
22233 inline_loc = loc;
22234 if (!first_loc)
22235 first_loc = loc;
22236 cp_lexer_consume_token (parser->lexer);
22237 continue;
22239 case RID_GOTO:
22240 if (goto_loc)
22242 error_at (loc, "duplicate %<asm%> qualifier %qT",
22243 token->u.value);
22244 inform (goto_loc, "first seen here");
22246 else
22247 goto_loc = loc;
22248 if (!first_loc)
22249 first_loc = loc;
22250 cp_lexer_consume_token (parser->lexer);
22251 continue;
22253 case RID_CONST:
22254 case RID_RESTRICT:
22255 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22256 cp_lexer_consume_token (parser->lexer);
22257 continue;
22259 default:
22260 break;
22262 break;
22265 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22266 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22267 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22269 if (!parser->in_function_body && (inline_p || goto_p))
22271 error_at (first_loc, "%<asm%> qualifier outside of function body");
22272 inline_p = goto_p = false;
22275 /* Look for the opening `('. */
22276 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22277 return;
22278 /* Look for the string. */
22279 tree string = cp_parser_string_literal (parser, /*translate=*/false,
22280 /*wide_ok=*/false);
22281 if (string == error_mark_node)
22283 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22284 /*consume_paren=*/true);
22285 return;
22288 /* If we're allowing GNU extensions, check for the extended assembly
22289 syntax. Unfortunately, the `:' tokens need not be separated by
22290 a space in C, and so, for compatibility, we tolerate that here
22291 too. Doing that means that we have to treat the `::' operator as
22292 two `:' tokens. */
22293 if (cp_parser_allow_gnu_extensions_p (parser)
22294 && parser->in_function_body
22295 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22296 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22298 bool inputs_p = false;
22299 bool clobbers_p = false;
22300 bool labels_p = false;
22302 /* The extended syntax was used. */
22303 extended_p = true;
22305 /* Look for outputs. */
22306 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22308 /* Consume the `:'. */
22309 cp_lexer_consume_token (parser->lexer);
22310 /* Parse the output-operands. */
22311 if (cp_lexer_next_token_is_not (parser->lexer,
22312 CPP_COLON)
22313 && cp_lexer_next_token_is_not (parser->lexer,
22314 CPP_SCOPE)
22315 && cp_lexer_next_token_is_not (parser->lexer,
22316 CPP_CLOSE_PAREN))
22318 outputs = cp_parser_asm_operand_list (parser);
22319 if (outputs == error_mark_node)
22320 invalid_outputs_p = true;
22323 /* If the next token is `::', there are no outputs, and the
22324 next token is the beginning of the inputs. */
22325 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22326 /* The inputs are coming next. */
22327 inputs_p = true;
22329 /* Look for inputs. */
22330 if (inputs_p
22331 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22333 /* Consume the `:' or `::'. */
22334 cp_lexer_consume_token (parser->lexer);
22335 /* Parse the output-operands. */
22336 if (cp_lexer_next_token_is_not (parser->lexer,
22337 CPP_COLON)
22338 && cp_lexer_next_token_is_not (parser->lexer,
22339 CPP_SCOPE)
22340 && cp_lexer_next_token_is_not (parser->lexer,
22341 CPP_CLOSE_PAREN))
22343 inputs = cp_parser_asm_operand_list (parser);
22344 if (inputs == error_mark_node)
22345 invalid_inputs_p = true;
22348 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22349 /* The clobbers are coming next. */
22350 clobbers_p = true;
22352 /* Look for clobbers. */
22353 if (clobbers_p
22354 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22356 clobbers_p = true;
22357 /* Consume the `:' or `::'. */
22358 cp_lexer_consume_token (parser->lexer);
22359 /* Parse the clobbers. */
22360 if (cp_lexer_next_token_is_not (parser->lexer,
22361 CPP_COLON)
22362 && cp_lexer_next_token_is_not (parser->lexer,
22363 CPP_CLOSE_PAREN))
22364 clobbers = cp_parser_asm_clobber_list (parser);
22366 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22367 /* The labels are coming next. */
22368 labels_p = true;
22370 /* Look for labels. */
22371 if (labels_p
22372 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22374 labels_p = true;
22375 /* Consume the `:' or `::'. */
22376 cp_lexer_consume_token (parser->lexer);
22377 /* Parse the labels. */
22378 labels = cp_parser_asm_label_list (parser);
22381 if (goto_p && !labels_p)
22382 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22384 else if (goto_p)
22385 missing = RT_COLON_SCOPE;
22387 /* Look for the closing `)'. */
22388 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22389 missing ? missing : RT_CLOSE_PAREN))
22390 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22391 /*consume_paren=*/true);
22392 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22394 if (!invalid_inputs_p && !invalid_outputs_p)
22396 /* Create the ASM_EXPR. */
22397 if (parser->in_function_body)
22399 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22400 inputs, clobbers, labels, inline_p);
22401 /* If the extended syntax was not used, mark the ASM_EXPR. */
22402 if (!extended_p)
22404 tree temp = asm_stmt;
22405 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22406 temp = TREE_OPERAND (temp, 0);
22408 ASM_INPUT_P (temp) = 1;
22411 else
22412 symtab->finalize_toplevel_asm (string);
22416 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22417 type that comes from the decl-specifier-seq. */
22419 static tree
22420 strip_declarator_types (tree type, cp_declarator *declarator)
22422 for (cp_declarator *d = declarator; d;)
22423 switch (d->kind)
22425 case cdk_id:
22426 case cdk_decomp:
22427 case cdk_error:
22428 d = NULL;
22429 break;
22431 default:
22432 if (TYPE_PTRMEMFUNC_P (type))
22433 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22434 type = TREE_TYPE (type);
22435 d = d->declarator;
22436 break;
22439 return type;
22442 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22443 a construct looks like a variable definition but is actually a function
22444 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22445 is the declarator for this function declaration. */
22447 static void
22448 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22449 const cp_declarator *declarator)
22451 /* Only warn if we are declaring a function at block scope. */
22452 if (!at_function_scope_p ())
22453 return;
22455 /* And only if there is no storage class specified. */
22456 if (decl_specifiers->storage_class != sc_none
22457 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22458 return;
22460 if (declarator->kind != cdk_function
22461 || !declarator->declarator
22462 || declarator->declarator->kind != cdk_id
22463 || !identifier_p (get_unqualified_id
22464 (const_cast<cp_declarator *>(declarator))))
22465 return;
22467 /* Don't warn when the whole declarator (not just the declarator-id!)
22468 was parenthesized. That is, don't warn for int(n()) but do warn
22469 for int(f)(). */
22470 if (declarator->parenthesized != UNKNOWN_LOCATION)
22471 return;
22473 tree type;
22474 if (decl_specifiers->type)
22476 type = decl_specifiers->type;
22477 if (TREE_CODE (type) == TYPE_DECL)
22478 type = TREE_TYPE (type);
22480 /* If the return type is void there is no ambiguity. */
22481 if (same_type_p (type, void_type_node))
22482 return;
22484 else if (decl_specifiers->any_type_specifiers_p)
22485 /* Code like long f(); will have null ->type. If we have any
22486 type-specifiers, pretend we've seen int. */
22487 type = integer_type_node;
22488 else
22489 return;
22491 auto_diagnostic_group d;
22492 location_t loc = declarator->u.function.parens_loc;
22493 tree params = declarator->u.function.parameters;
22494 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22496 /* The T t() case. */
22497 if (params == void_list_node)
22499 if (warning_at (loc, OPT_Wvexing_parse,
22500 "empty parentheses were disambiguated as a function "
22501 "declaration"))
22503 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22504 means value-initialization or aggregate-initialization, nothing
22505 means default-initialization. We can only suggest removing the
22506 parentheses/adding {} if T has a default constructor. */
22507 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22509 gcc_rich_location iloc (loc);
22510 iloc.add_fixit_remove ();
22511 inform (&iloc, "remove parentheses to default-initialize "
22512 "a variable");
22513 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22515 if (CP_AGGREGATE_TYPE_P (type))
22516 inform (loc, "or replace parentheses with braces to "
22517 "aggregate-initialize a variable");
22518 else
22519 inform (loc, "or replace parentheses with braces to "
22520 "value-initialize a variable");
22524 return;
22527 /* If we had (...) or the parameter-list wasn't parenthesized,
22528 we're done. */
22529 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22530 return;
22532 /* The T t(X()) case. */
22533 if (list_length (params) == 2)
22535 if (warning_at (loc, OPT_Wvexing_parse,
22536 "parentheses were disambiguated as a function "
22537 "declaration"))
22539 gcc_rich_location iloc (loc);
22540 /* {}-initialization means that we can use an initializer-list
22541 constructor if no default constructor is available, so don't
22542 suggest using {} for classes that have an initializer_list
22543 constructor. */
22544 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22546 iloc.add_fixit_replace (get_start (loc), "{");
22547 iloc.add_fixit_replace (get_finish (loc), "}");
22548 inform (&iloc, "replace parentheses with braces to declare a "
22549 "variable");
22551 else
22553 iloc.add_fixit_insert_after (get_start (loc), "(");
22554 iloc.add_fixit_insert_before (get_finish (loc), ")");
22555 inform (&iloc, "add parentheses to declare a variable");
22559 /* The T t(X(), X()) case. */
22560 else if (warning_at (loc, OPT_Wvexing_parse,
22561 "parentheses were disambiguated as a function "
22562 "declaration"))
22564 gcc_rich_location iloc (loc);
22565 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22567 iloc.add_fixit_replace (get_start (loc), "{");
22568 iloc.add_fixit_replace (get_finish (loc), "}");
22569 inform (&iloc, "replace parentheses with braces to declare a "
22570 "variable");
22575 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22576 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22577 has the same meaning as in cp_parser_declarator. */
22579 static void
22580 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22581 cp_decl_specifier_seq *decl_specs,
22582 cp_declarator *declarator,
22583 int *ctor_dtor_or_conv_p)
22585 if (cxx_dialect >= cxx17
22586 && *ctor_dtor_or_conv_p <= 0
22587 && !decl_specs->type
22588 && !decl_specs->any_type_specifiers_p
22589 && function_declarator_p (declarator))
22591 cp_declarator *id = get_id_declarator (declarator);
22592 tree name = id->u.id.unqualified_name;
22593 parser->scope = id->u.id.qualifying_scope;
22594 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22595 if (tmpl
22596 && (DECL_CLASS_TEMPLATE_P (tmpl)
22597 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22599 id->u.id.unqualified_name = dguide_name (tmpl);
22600 id->u.id.sfk = sfk_deduction_guide;
22601 *ctor_dtor_or_conv_p = 1;
22606 /* Declarators [gram.dcl.decl] */
22608 /* Parse an init-declarator.
22610 init-declarator:
22611 declarator initializer [opt]
22613 GNU Extension:
22615 init-declarator:
22616 declarator asm-specification [opt] attributes [opt] initializer [opt]
22618 function-definition:
22619 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22620 function-body
22621 decl-specifier-seq [opt] declarator function-try-block
22623 GNU Extension:
22625 function-definition:
22626 __extension__ function-definition
22628 TM Extension:
22630 function-definition:
22631 decl-specifier-seq [opt] declarator function-transaction-block
22633 The parser flags FLAGS is used to control type-specifier parsing.
22635 The DECL_SPECIFIERS apply to this declarator. Returns a
22636 representation of the entity declared. If MEMBER_P is TRUE, then
22637 this declarator appears in a class scope. The new DECL created by
22638 this declarator is returned.
22640 The CHECKS are access checks that should be performed once we know
22641 what entity is being declared (and, therefore, what classes have
22642 befriended it).
22644 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22645 for a function-definition here as well. If the declarator is a
22646 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22647 be TRUE upon return. By that point, the function-definition will
22648 have been completely parsed.
22650 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22651 is FALSE.
22653 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22654 parsed declaration if it is an uninitialized single declarator not followed
22655 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22656 if present, will not be consumed. If returned, this declarator will be
22657 created with SD_INITIALIZED but will not call cp_finish_decl.
22659 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22660 and there is an initializer, the pointed location_t is set to the
22661 location of the '=' or `(', or '{' in C++11 token introducing the
22662 initializer. */
22664 static tree
22665 cp_parser_init_declarator (cp_parser* parser,
22666 cp_parser_flags flags,
22667 cp_decl_specifier_seq *decl_specifiers,
22668 vec<deferred_access_check, va_gc> *checks,
22669 bool function_definition_allowed_p,
22670 bool member_p,
22671 int declares_class_or_enum,
22672 bool* function_definition_p,
22673 tree* maybe_range_for_decl,
22674 location_t* init_loc,
22675 tree* auto_result)
22677 cp_token *token = NULL, *asm_spec_start_token = NULL,
22678 *attributes_start_token = NULL;
22679 cp_declarator *declarator;
22680 tree prefix_attributes;
22681 tree attributes = NULL;
22682 tree asm_specification;
22683 tree initializer;
22684 tree decl = NULL_TREE;
22685 tree scope;
22686 int is_initialized;
22687 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22688 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22689 "(...)". */
22690 enum cpp_ttype initialization_kind;
22691 bool is_direct_init = false;
22692 bool is_non_constant_init;
22693 int ctor_dtor_or_conv_p;
22694 bool friend_p = cp_parser_friend_p (decl_specifiers);
22695 bool static_p = decl_specifiers->storage_class == sc_static;
22696 tree pushed_scope = NULL_TREE;
22697 bool range_for_decl_p = false;
22698 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22699 location_t tmp_init_loc = UNKNOWN_LOCATION;
22701 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22702 flags |= CP_PARSER_FLAGS_CONSTEVAL;
22704 /* Assume that this is not the declarator for a function
22705 definition. */
22706 if (function_definition_p)
22707 *function_definition_p = false;
22709 /* Default arguments are only permitted for function parameters. */
22710 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22711 parser->default_arg_ok_p = false;
22713 /* Defer access checks while parsing the declarator; we cannot know
22714 what names are accessible until we know what is being
22715 declared. */
22716 resume_deferring_access_checks ();
22718 token = cp_lexer_peek_token (parser->lexer);
22720 /* Parse the declarator. */
22721 declarator
22722 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22723 flags, &ctor_dtor_or_conv_p,
22724 /*parenthesized_p=*/NULL,
22725 member_p, friend_p, static_p);
22726 /* Gather up the deferred checks. */
22727 stop_deferring_access_checks ();
22729 parser->default_arg_ok_p = saved_default_arg_ok_p;
22731 /* If the DECLARATOR was erroneous, there's no need to go
22732 further. */
22733 if (declarator == cp_error_declarator)
22734 return error_mark_node;
22736 /* Check that the number of template-parameter-lists is OK. */
22737 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22738 token->location))
22739 return error_mark_node;
22741 if (declares_class_or_enum & 2)
22742 cp_parser_check_for_definition_in_return_type (declarator,
22743 decl_specifiers->type,
22744 decl_specifiers->locations[ds_type_spec]);
22746 /* Figure out what scope the entity declared by the DECLARATOR is
22747 located in. `grokdeclarator' sometimes changes the scope, so
22748 we compute it now. */
22749 scope = get_scope_of_declarator (declarator);
22751 /* Perform any lookups in the declared type which were thought to be
22752 dependent, but are not in the scope of the declarator. */
22753 decl_specifiers->type
22754 = maybe_update_decl_type (decl_specifiers->type, scope);
22756 /* If we're allowing GNU extensions, look for an
22757 asm-specification. */
22758 if (cp_parser_allow_gnu_extensions_p (parser))
22760 /* Look for an asm-specification. */
22761 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22762 asm_specification = cp_parser_asm_specification_opt (parser);
22764 else
22765 asm_specification = NULL_TREE;
22767 /* Gather the attributes that were provided with the
22768 decl-specifiers. */
22769 prefix_attributes = decl_specifiers->attributes;
22771 /* Look for attributes. */
22772 attributes_start_token = cp_lexer_peek_token (parser->lexer);
22773 attributes = cp_parser_attributes_opt (parser);
22775 /* Peek at the next token. */
22776 token = cp_lexer_peek_token (parser->lexer);
22778 bool bogus_implicit_tmpl = false;
22780 if (function_declarator_p (declarator))
22782 /* Handle C++17 deduction guides. Note that class-scope
22783 non-template deduction guides are instead handled in
22784 cp_parser_member_declaration. */
22785 cp_parser_maybe_adjust_declarator_for_dguide (parser,
22786 decl_specifiers,
22787 declarator,
22788 &ctor_dtor_or_conv_p);
22790 if (!member_p && !cp_parser_error_occurred (parser))
22791 warn_about_ambiguous_parse (decl_specifiers, declarator);
22793 /* Check to see if the token indicates the start of a
22794 function-definition. */
22795 if (cp_parser_token_starts_function_definition_p (token))
22797 if (!function_definition_allowed_p)
22799 /* If a function-definition should not appear here, issue an
22800 error message. */
22801 cp_parser_error (parser,
22802 "a function-definition is not allowed here");
22803 return error_mark_node;
22806 location_t func_brace_location
22807 = cp_lexer_peek_token (parser->lexer)->location;
22809 /* Neither attributes nor an asm-specification are allowed
22810 on a function-definition. */
22811 if (asm_specification)
22812 error_at (asm_spec_start_token->location,
22813 "an %<asm%> specification is not allowed "
22814 "on a function-definition");
22815 if (attributes)
22816 error_at (attributes_start_token->location,
22817 "attributes are not allowed "
22818 "on a function-definition");
22819 /* This is a function-definition. */
22820 *function_definition_p = true;
22822 /* Parse the function definition. */
22823 if (member_p)
22824 decl = cp_parser_save_member_function_body (parser,
22825 decl_specifiers,
22826 declarator,
22827 prefix_attributes);
22828 else
22829 decl =
22830 (cp_parser_function_definition_from_specifiers_and_declarator
22831 (parser, decl_specifiers, prefix_attributes, declarator));
22833 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
22835 /* This is where the prologue starts... */
22836 DECL_STRUCT_FUNCTION (decl)->function_start_locus
22837 = func_brace_location;
22840 return decl;
22843 else if (parser->fully_implicit_function_template_p)
22845 /* A non-template declaration involving a function parameter list
22846 containing an implicit template parameter will be made into a
22847 template. If the resulting declaration is not going to be an
22848 actual function then finish the template scope here to prevent it.
22849 An error message will be issued once we have a decl to talk about.
22851 FIXME probably we should do type deduction rather than create an
22852 implicit template, but the standard currently doesn't allow it. */
22853 bogus_implicit_tmpl = true;
22854 finish_fully_implicit_template (parser, NULL_TREE);
22857 /* [dcl.dcl]
22859 Only in function declarations for constructors, destructors, type
22860 conversions, and deduction guides can the decl-specifier-seq be omitted.
22862 We explicitly postpone this check past the point where we handle
22863 function-definitions because we tolerate function-definitions
22864 that are missing their return types in some modes. */
22865 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
22867 cp_parser_error (parser,
22868 "expected constructor, destructor, or type conversion");
22869 return error_mark_node;
22872 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
22873 an initializer as well. */
22874 if (token->type == CPP_EQ
22875 || token->type == CPP_OPEN_PAREN
22876 || token->type == CPP_OPEN_BRACE)
22878 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
22879 It isn't; it's an expression. (Here '(i)' would have already been
22880 parsed as a declarator.) */
22881 if (token->type == CPP_OPEN_PAREN
22882 && cp_parser_uncommitted_to_tentative_parse_p (parser))
22884 cp_lexer_save_tokens (parser->lexer);
22885 cp_lexer_consume_token (parser->lexer);
22886 cp_parser_skip_to_closing_parenthesis (parser,
22887 /*recovering*/false,
22888 /*or_comma*/false,
22889 /*consume_paren*/true);
22890 /* If this is an initializer, only a ',' or ';' can follow: either
22891 we have another init-declarator, or we're at the end of an
22892 init-declarator-list which can only be followed by a ';'. */
22893 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22894 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22895 cp_lexer_rollback_tokens (parser->lexer);
22896 if (UNLIKELY (!ok))
22897 /* Not an init-declarator. */
22898 return error_mark_node;
22900 is_initialized = SD_INITIALIZED;
22901 initialization_kind = token->type;
22902 declarator->init_loc = token->location;
22903 if (maybe_range_for_decl)
22904 *maybe_range_for_decl = error_mark_node;
22905 tmp_init_loc = token->location;
22906 if (init_loc && *init_loc == UNKNOWN_LOCATION)
22907 *init_loc = tmp_init_loc;
22909 if (token->type == CPP_EQ
22910 && function_declarator_p (declarator))
22912 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
22913 if (t2->keyword == RID_DEFAULT)
22914 is_initialized = SD_DEFAULTED;
22915 else if (t2->keyword == RID_DELETE)
22916 is_initialized = SD_DELETED;
22919 else
22921 /* If the init-declarator isn't initialized and isn't followed by a
22922 `,' or `;', it's not a valid init-declarator. */
22923 if (token->type != CPP_COMMA
22924 && token->type != CPP_SEMICOLON)
22926 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
22927 range_for_decl_p = true;
22928 else
22930 if (!maybe_range_for_decl)
22931 cp_parser_error (parser, "expected initializer");
22932 return error_mark_node;
22935 is_initialized = SD_UNINITIALIZED;
22936 initialization_kind = CPP_EOF;
22939 /* Because start_decl has side-effects, we should only call it if we
22940 know we're going ahead. By this point, we know that we cannot
22941 possibly be looking at any other construct. */
22942 cp_parser_commit_to_tentative_parse (parser);
22944 /* Enter the newly declared entry in the symbol table. If we're
22945 processing a declaration in a class-specifier, we wait until
22946 after processing the initializer. */
22947 if (!member_p)
22949 if (parser->in_unbraced_linkage_specification_p)
22950 decl_specifiers->storage_class = sc_extern;
22951 decl = start_decl (declarator, decl_specifiers,
22952 range_for_decl_p? SD_INITIALIZED : is_initialized,
22953 attributes, prefix_attributes, &pushed_scope);
22954 cp_finalize_omp_declare_simd (parser, decl);
22955 cp_finalize_oacc_routine (parser, decl, false);
22956 /* Adjust location of decl if declarator->id_loc is more appropriate:
22957 set, and decl wasn't merged with another decl, in which case its
22958 location would be different from input_location, and more accurate. */
22959 if (DECL_P (decl)
22960 && declarator->id_loc != UNKNOWN_LOCATION
22961 && DECL_SOURCE_LOCATION (decl) == input_location)
22962 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
22964 else if (scope)
22965 /* Enter the SCOPE. That way unqualified names appearing in the
22966 initializer will be looked up in SCOPE. */
22967 pushed_scope = push_scope (scope);
22969 /* Perform deferred access control checks, now that we know in which
22970 SCOPE the declared entity resides. */
22971 if (!member_p && decl)
22973 tree saved_current_function_decl = NULL_TREE;
22975 /* If the entity being declared is a function, pretend that we
22976 are in its scope. If it is a `friend', it may have access to
22977 things that would not otherwise be accessible. */
22978 if (TREE_CODE (decl) == FUNCTION_DECL)
22980 saved_current_function_decl = current_function_decl;
22981 current_function_decl = decl;
22984 /* Perform access checks for template parameters. */
22985 cp_parser_perform_template_parameter_access_checks (checks);
22987 /* Perform the access control checks for the declarator and the
22988 decl-specifiers. */
22989 perform_deferred_access_checks (tf_warning_or_error);
22991 /* Restore the saved value. */
22992 if (TREE_CODE (decl) == FUNCTION_DECL)
22993 current_function_decl = saved_current_function_decl;
22996 /* Parse the initializer. */
22997 initializer = NULL_TREE;
22998 is_direct_init = false;
22999 is_non_constant_init = true;
23000 if (is_initialized)
23002 if (function_declarator_p (declarator))
23004 if (initialization_kind == CPP_EQ)
23005 initializer = cp_parser_pure_specifier (parser);
23006 else
23008 /* If the declaration was erroneous, we don't really
23009 know what the user intended, so just silently
23010 consume the initializer. */
23011 if (decl != error_mark_node)
23012 error_at (tmp_init_loc, "initializer provided for function");
23013 cp_parser_skip_to_closing_parenthesis (parser,
23014 /*recovering=*/true,
23015 /*or_comma=*/false,
23016 /*consume_paren=*/true);
23019 else
23021 /* We want to record the extra mangling scope for in-class
23022 initializers of class members and initializers of static
23023 data member templates and namespace-scope initializers.
23024 The former involves deferring parsing of the initializer
23025 until end of class as with default arguments. So right
23026 here we only handle the latter two. */
23027 bool has_lambda_scope = false;
23029 if (decl != error_mark_node
23030 && !member_p
23031 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
23032 has_lambda_scope = true;
23034 if (has_lambda_scope)
23035 start_lambda_scope (decl);
23036 initializer = cp_parser_initializer (parser,
23037 &is_direct_init,
23038 &is_non_constant_init);
23039 if (has_lambda_scope)
23040 finish_lambda_scope ();
23041 if (initializer == error_mark_node)
23042 cp_parser_skip_to_end_of_statement (parser);
23046 /* The old parser allows attributes to appear after a parenthesized
23047 initializer. Mark Mitchell proposed removing this functionality
23048 on the GCC mailing lists on 2002-08-13. This parser accepts the
23049 attributes -- but ignores them. Made a permerror in GCC 8. */
23050 if (cp_parser_allow_gnu_extensions_p (parser)
23051 && initialization_kind == CPP_OPEN_PAREN
23052 && cp_parser_attributes_opt (parser)
23053 && permerror (input_location,
23054 "attributes after parenthesized initializer ignored"))
23056 static bool hint;
23057 if (flag_permissive && !hint)
23059 hint = true;
23060 inform (input_location,
23061 "this flexibility is deprecated and will be removed");
23065 /* And now complain about a non-function implicit template. */
23066 if (bogus_implicit_tmpl && decl != error_mark_node)
23067 error_at (DECL_SOURCE_LOCATION (decl),
23068 "non-function %qD declared as implicit template", decl);
23070 /* For an in-class declaration, use `grokfield' to create the
23071 declaration. */
23072 if (member_p)
23074 if (pushed_scope)
23076 pop_scope (pushed_scope);
23077 pushed_scope = NULL_TREE;
23079 decl = grokfield (declarator, decl_specifiers,
23080 initializer, !is_non_constant_init,
23081 /*asmspec=*/NULL_TREE,
23082 attr_chainon (attributes, prefix_attributes));
23083 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
23084 cp_parser_save_default_args (parser, decl);
23085 cp_finalize_omp_declare_simd (parser, decl);
23086 cp_finalize_oacc_routine (parser, decl, false);
23089 /* Finish processing the declaration. But, skip member
23090 declarations. */
23091 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
23093 cp_finish_decl (decl,
23094 initializer, !is_non_constant_init,
23095 asm_specification,
23096 /* If the initializer is in parentheses, then this is
23097 a direct-initialization, which means that an
23098 `explicit' constructor is OK. Otherwise, an
23099 `explicit' constructor cannot be used. */
23100 ((is_direct_init || !is_initialized)
23101 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
23103 else if ((cxx_dialect != cxx98) && friend_p
23104 && decl && TREE_CODE (decl) == FUNCTION_DECL)
23105 /* Core issue #226 (C++0x only): A default template-argument
23106 shall not be specified in a friend class template
23107 declaration. */
23108 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
23109 /*is_partial=*/false, /*is_friend_decl=*/1);
23111 if (!friend_p && pushed_scope)
23112 pop_scope (pushed_scope);
23114 if (function_declarator_p (declarator)
23115 && parser->fully_implicit_function_template_p)
23117 if (member_p)
23118 decl = finish_fully_implicit_template (parser, decl);
23119 else
23120 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23123 if (auto_result && is_initialized && decl_specifiers->type
23124 && type_uses_auto (decl_specifiers->type))
23125 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
23127 return decl;
23130 /* Parse a declarator.
23132 declarator:
23133 direct-declarator
23134 ptr-operator declarator
23136 abstract-declarator:
23137 ptr-operator abstract-declarator [opt]
23138 direct-abstract-declarator
23140 GNU Extensions:
23142 declarator:
23143 attributes [opt] direct-declarator
23144 attributes [opt] ptr-operator declarator
23146 abstract-declarator:
23147 attributes [opt] ptr-operator abstract-declarator [opt]
23148 attributes [opt] direct-abstract-declarator
23150 The parser flags FLAGS is used to control type-specifier parsing.
23152 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23153 detect constructors, destructors, deduction guides, or conversion operators.
23154 It is set to -1 if the declarator is a name, and +1 if it is a
23155 function. Otherwise it is set to zero. Usually you just want to
23156 test for >0, but internally the negative value is used.
23158 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23159 a decl-specifier-seq unless it declares a constructor, destructor,
23160 or conversion. It might seem that we could check this condition in
23161 semantic analysis, rather than parsing, but that makes it difficult
23162 to handle something like `f()'. We want to notice that there are
23163 no decl-specifiers, and therefore realize that this is an
23164 expression, not a declaration.)
23166 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23167 the declarator is a direct-declarator of the form "(...)".
23169 MEMBER_P is true iff this declarator is a member-declarator.
23171 FRIEND_P is true iff this declarator is a friend.
23173 STATIC_P is true iff the keyword static was seen. */
23175 static cp_declarator *
23176 cp_parser_declarator (cp_parser* parser,
23177 cp_parser_declarator_kind dcl_kind,
23178 cp_parser_flags flags,
23179 int* ctor_dtor_or_conv_p,
23180 bool* parenthesized_p,
23181 bool member_p, bool friend_p, bool static_p)
23183 cp_declarator *declarator;
23184 enum tree_code code;
23185 cp_cv_quals cv_quals;
23186 tree class_type;
23187 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
23189 /* Assume this is not a constructor, destructor, or type-conversion
23190 operator. */
23191 if (ctor_dtor_or_conv_p)
23192 *ctor_dtor_or_conv_p = 0;
23194 if (cp_parser_allow_gnu_extensions_p (parser))
23195 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23197 /* Check for the ptr-operator production. */
23198 cp_parser_parse_tentatively (parser);
23199 /* Parse the ptr-operator. */
23200 code = cp_parser_ptr_operator (parser,
23201 &class_type,
23202 &cv_quals,
23203 &std_attributes);
23205 /* If that worked, then we have a ptr-operator. */
23206 if (cp_parser_parse_definitely (parser))
23208 /* If a ptr-operator was found, then this declarator was not
23209 parenthesized. */
23210 if (parenthesized_p)
23211 *parenthesized_p = false;
23212 /* The dependent declarator is optional if we are parsing an
23213 abstract-declarator. */
23214 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23215 cp_parser_parse_tentatively (parser);
23217 /* Parse the dependent declarator. */
23218 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23219 /*ctor_dtor_or_conv_p=*/NULL,
23220 /*parenthesized_p=*/NULL,
23221 member_p, friend_p, static_p);
23223 /* If we are parsing an abstract-declarator, we must handle the
23224 case where the dependent declarator is absent. */
23225 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23226 && !cp_parser_parse_definitely (parser))
23227 declarator = NULL;
23229 declarator = cp_parser_make_indirect_declarator
23230 (code, class_type, cv_quals, declarator, std_attributes);
23232 /* Everything else is a direct-declarator. */
23233 else
23235 if (parenthesized_p)
23236 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23237 CPP_OPEN_PAREN);
23238 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23239 flags, ctor_dtor_or_conv_p,
23240 member_p, friend_p, static_p);
23243 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23244 declarator->attributes = gnu_attributes;
23245 return declarator;
23248 /* Parse a direct-declarator or direct-abstract-declarator.
23250 direct-declarator:
23251 declarator-id
23252 direct-declarator ( parameter-declaration-clause )
23253 cv-qualifier-seq [opt]
23254 ref-qualifier [opt]
23255 exception-specification [opt]
23256 direct-declarator [ constant-expression [opt] ]
23257 ( declarator )
23259 direct-abstract-declarator:
23260 direct-abstract-declarator [opt]
23261 ( parameter-declaration-clause )
23262 cv-qualifier-seq [opt]
23263 ref-qualifier [opt]
23264 exception-specification [opt]
23265 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23266 ( abstract-declarator )
23268 Returns a representation of the declarator. DCL_KIND is
23269 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23270 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23271 we are parsing a direct-declarator. It is
23272 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23273 of ambiguity we prefer an abstract declarator, as per
23274 [dcl.ambig.res].
23275 The parser flags FLAGS is used to control type-specifier parsing.
23276 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23277 as for cp_parser_declarator. */
23279 static cp_declarator *
23280 cp_parser_direct_declarator (cp_parser* parser,
23281 cp_parser_declarator_kind dcl_kind,
23282 cp_parser_flags flags,
23283 int* ctor_dtor_or_conv_p,
23284 bool member_p, bool friend_p, bool static_p)
23286 cp_token *token;
23287 cp_declarator *declarator = NULL;
23288 tree scope = NULL_TREE;
23289 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23290 bool saved_in_declarator_p = parser->in_declarator_p;
23291 bool first = true;
23292 tree pushed_scope = NULL_TREE;
23293 cp_token *open_paren = NULL, *close_paren = NULL;
23295 while (true)
23297 /* Peek at the next token. */
23298 token = cp_lexer_peek_token (parser->lexer);
23299 if (token->type == CPP_OPEN_PAREN)
23301 /* This is either a parameter-declaration-clause, or a
23302 parenthesized declarator. When we know we are parsing a
23303 named declarator, it must be a parenthesized declarator
23304 if FIRST is true. For instance, `(int)' is a
23305 parameter-declaration-clause, with an omitted
23306 direct-abstract-declarator. But `((*))', is a
23307 parenthesized abstract declarator. Finally, when T is a
23308 template parameter `(T)' is a
23309 parameter-declaration-clause, and not a parenthesized
23310 named declarator.
23312 We first try and parse a parameter-declaration-clause,
23313 and then try a nested declarator (if FIRST is true).
23315 It is not an error for it not to be a
23316 parameter-declaration-clause, even when FIRST is
23317 false. Consider,
23319 int i (int);
23320 int i (3);
23322 The first is the declaration of a function while the
23323 second is the definition of a variable, including its
23324 initializer.
23326 Having seen only the parenthesis, we cannot know which of
23327 these two alternatives should be selected. Even more
23328 complex are examples like:
23330 int i (int (a));
23331 int i (int (3));
23333 The former is a function-declaration; the latter is a
23334 variable initialization.
23336 Thus again, we try a parameter-declaration-clause, and if
23337 that fails, we back out and return. */
23339 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23341 tree params;
23342 bool is_declarator = false;
23344 open_paren = NULL;
23346 /* In a member-declarator, the only valid interpretation
23347 of a parenthesis is the start of a
23348 parameter-declaration-clause. (It is invalid to
23349 initialize a static data member with a parenthesized
23350 initializer; only the "=" form of initialization is
23351 permitted.) */
23352 if (!member_p)
23353 cp_parser_parse_tentatively (parser);
23355 /* Consume the `('. */
23356 const location_t parens_start = token->location;
23357 matching_parens parens;
23358 parens.consume_open (parser);
23359 if (first)
23361 /* If this is going to be an abstract declarator, we're
23362 in a declarator and we can't have default args. */
23363 parser->default_arg_ok_p = false;
23364 parser->in_declarator_p = true;
23367 begin_scope (sk_function_parms, NULL_TREE);
23369 /* Parse the parameter-declaration-clause. */
23370 params
23371 = cp_parser_parameter_declaration_clause (parser, flags);
23372 const location_t parens_end
23373 = cp_lexer_peek_token (parser->lexer)->location;
23375 /* Consume the `)'. */
23376 parens.require_close (parser);
23378 /* If all went well, parse the cv-qualifier-seq,
23379 ref-qualifier and the exception-specification. */
23380 if (member_p || cp_parser_parse_definitely (parser))
23382 cp_cv_quals cv_quals;
23383 cp_virt_specifiers virt_specifiers;
23384 cp_ref_qualifier ref_qual;
23385 tree exception_specification;
23386 tree late_return;
23387 tree attrs;
23388 bool memfn = (member_p || (pushed_scope
23389 && CLASS_TYPE_P (pushed_scope)));
23390 unsigned char local_variables_forbidden_p
23391 = parser->local_variables_forbidden_p;
23392 /* 'this' is not allowed in static member functions. */
23393 if (static_p || friend_p)
23394 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23396 is_declarator = true;
23398 if (ctor_dtor_or_conv_p)
23399 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23400 first = false;
23402 /* Parse the cv-qualifier-seq. */
23403 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23404 /* Parse the ref-qualifier. */
23405 ref_qual = cp_parser_ref_qualifier_opt (parser);
23406 /* Parse the tx-qualifier. */
23407 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23409 tree save_ccp = current_class_ptr;
23410 tree save_ccr = current_class_ref;
23411 if (memfn && !friend_p && !static_p)
23412 /* DR 1207: 'this' is in scope after the cv-quals. */
23413 inject_this_parameter (current_class_type, cv_quals);
23415 /* If it turned out that this is e.g. a pointer to a
23416 function, we don't want to delay noexcept parsing. */
23417 if (declarator == NULL || declarator->kind != cdk_id)
23418 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23420 /* Parse the exception-specification. */
23421 exception_specification
23422 = cp_parser_exception_specification_opt (parser,
23423 flags);
23425 attrs = cp_parser_std_attribute_spec_seq (parser);
23427 cp_omp_declare_simd_data odsd;
23428 if ((flag_openmp || flag_openmp_simd)
23429 && declarator
23430 && declarator->std_attributes
23431 && declarator->kind == cdk_id)
23433 tree *pa = &declarator->std_attributes;
23434 cp_parser_handle_directive_omp_attributes (parser, pa,
23435 &odsd, false);
23438 /* In here, we handle cases where attribute is used after
23439 the function declaration. For example:
23440 void func (int x) __attribute__((vector(..))); */
23441 tree gnu_attrs = NULL_TREE;
23442 tree requires_clause = NULL_TREE;
23443 late_return
23444 = cp_parser_late_return_type_opt (parser, declarator,
23445 requires_clause);
23447 cp_finalize_omp_declare_simd (parser, &odsd);
23449 /* Parse the virt-specifier-seq. */
23450 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23452 location_t parens_loc = make_location (parens_start,
23453 parens_start,
23454 parens_end);
23455 /* Create the function-declarator. */
23456 declarator = make_call_declarator (declarator,
23457 params,
23458 cv_quals,
23459 virt_specifiers,
23460 ref_qual,
23461 tx_qual,
23462 exception_specification,
23463 late_return,
23464 requires_clause,
23465 attrs,
23466 parens_loc);
23467 declarator->attributes = gnu_attrs;
23468 /* Any subsequent parameter lists are to do with
23469 return type, so are not those of the declared
23470 function. */
23471 parser->default_arg_ok_p = false;
23473 current_class_ptr = save_ccp;
23474 current_class_ref = save_ccr;
23476 /* Restore the state of local_variables_forbidden_p. */
23477 parser->local_variables_forbidden_p
23478 = local_variables_forbidden_p;
23481 /* Remove the function parms from scope. */
23482 pop_bindings_and_leave_scope ();
23484 if (is_declarator)
23485 /* Repeat the main loop. */
23486 continue;
23489 /* If this is the first, we can try a parenthesized
23490 declarator. */
23491 if (first)
23493 bool saved_in_type_id_in_expr_p;
23495 parser->default_arg_ok_p = saved_default_arg_ok_p;
23496 parser->in_declarator_p = saved_in_declarator_p;
23498 open_paren = token;
23499 /* Consume the `('. */
23500 matching_parens parens;
23501 parens.consume_open (parser);
23502 /* Parse the nested declarator. */
23503 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23504 parser->in_type_id_in_expr_p = true;
23505 declarator
23506 = cp_parser_declarator (parser, dcl_kind, flags,
23507 ctor_dtor_or_conv_p,
23508 /*parenthesized_p=*/NULL,
23509 member_p, friend_p,
23510 /*static_p=*/false);
23511 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23512 first = false;
23513 /* Expect a `)'. */
23514 close_paren = cp_lexer_peek_token (parser->lexer);
23515 if (!parens.require_close (parser))
23516 declarator = cp_error_declarator;
23517 if (declarator == cp_error_declarator)
23518 break;
23520 goto handle_declarator;
23522 /* Otherwise, we must be done. */
23523 else
23524 break;
23526 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23527 && token->type == CPP_OPEN_SQUARE
23528 && !cp_next_tokens_can_be_attribute_p (parser))
23530 /* Parse an array-declarator. */
23531 tree bounds, attrs;
23533 if (ctor_dtor_or_conv_p)
23534 *ctor_dtor_or_conv_p = 0;
23536 open_paren = NULL;
23537 first = false;
23538 parser->default_arg_ok_p = false;
23539 parser->in_declarator_p = true;
23540 /* Consume the `['. */
23541 cp_lexer_consume_token (parser->lexer);
23542 /* Peek at the next token. */
23543 token = cp_lexer_peek_token (parser->lexer);
23544 /* If the next token is `]', then there is no
23545 constant-expression. */
23546 if (token->type != CPP_CLOSE_SQUARE)
23548 bool non_constant_p;
23549 bounds
23550 = cp_parser_constant_expression (parser,
23551 /*allow_non_constant=*/true,
23552 &non_constant_p);
23553 if (!non_constant_p)
23554 /* OK */;
23555 else if (error_operand_p (bounds))
23556 /* Already gave an error. */;
23557 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23558 /* Let compute_array_index_type diagnose this. */;
23559 else if (!parser->in_function_body
23560 || parsing_function_declarator ())
23562 /* Normally, the array bound must be an integral constant
23563 expression. However, as an extension, we allow VLAs
23564 in function scopes as long as they aren't part of a
23565 parameter declaration. */
23566 cp_parser_error (parser,
23567 "array bound is not an integer constant");
23568 bounds = error_mark_node;
23570 else if (processing_template_decl
23571 && !type_dependent_expression_p (bounds))
23573 /* Remember this wasn't a constant-expression. */
23574 bounds = build_nop (TREE_TYPE (bounds), bounds);
23575 TREE_SIDE_EFFECTS (bounds) = 1;
23578 else
23579 bounds = NULL_TREE;
23580 /* Look for the closing `]'. */
23581 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23583 declarator = cp_error_declarator;
23584 break;
23587 attrs = cp_parser_std_attribute_spec_seq (parser);
23588 declarator = make_array_declarator (declarator, bounds);
23589 declarator->std_attributes = attrs;
23591 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23594 tree qualifying_scope;
23595 tree unqualified_name;
23596 tree attrs;
23597 special_function_kind sfk;
23598 bool abstract_ok;
23599 bool pack_expansion_p = false;
23600 cp_token *declarator_id_start_token;
23602 /* Parse a declarator-id */
23603 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23604 if (abstract_ok)
23606 cp_parser_parse_tentatively (parser);
23608 /* If we see an ellipsis, we should be looking at a
23609 parameter pack. */
23610 if (token->type == CPP_ELLIPSIS)
23612 /* Consume the `...' */
23613 cp_lexer_consume_token (parser->lexer);
23615 pack_expansion_p = true;
23619 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23620 unqualified_name
23621 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23622 qualifying_scope = parser->scope;
23623 if (abstract_ok)
23625 bool okay = false;
23627 if (!unqualified_name && pack_expansion_p)
23629 /* Check whether an error occurred. */
23630 okay = !cp_parser_error_occurred (parser);
23632 /* We already consumed the ellipsis to mark a
23633 parameter pack, but we have no way to report it,
23634 so abort the tentative parse. We will be exiting
23635 immediately anyway. */
23636 cp_parser_abort_tentative_parse (parser);
23638 else
23639 okay = cp_parser_parse_definitely (parser);
23641 if (!okay)
23642 unqualified_name = error_mark_node;
23643 else if (unqualified_name
23644 && (qualifying_scope
23645 || (!identifier_p (unqualified_name))))
23647 cp_parser_error (parser, "expected unqualified-id");
23648 unqualified_name = error_mark_node;
23652 if (!unqualified_name)
23653 return NULL;
23654 if (unqualified_name == error_mark_node)
23656 declarator = cp_error_declarator;
23657 pack_expansion_p = false;
23658 declarator->parameter_pack_p = false;
23659 break;
23662 attrs = cp_parser_std_attribute_spec_seq (parser);
23664 if (qualifying_scope && at_namespace_scope_p ()
23665 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23667 /* In the declaration of a member of a template class
23668 outside of the class itself, the SCOPE will sometimes
23669 be a TYPENAME_TYPE. For example, given:
23671 template <typename T>
23672 int S<T>::R::i = 3;
23674 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23675 this context, we must resolve S<T>::R to an ordinary
23676 type, rather than a typename type.
23678 The reason we normally avoid resolving TYPENAME_TYPEs
23679 is that a specialization of `S' might render
23680 `S<T>::R' not a type. However, if `S' is
23681 specialized, then this `i' will not be used, so there
23682 is no harm in resolving the types here. */
23683 tree type;
23685 /* Resolve the TYPENAME_TYPE. */
23686 type = resolve_typename_type (qualifying_scope,
23687 /*only_current_p=*/false);
23688 /* If that failed, the declarator is invalid. */
23689 if (TREE_CODE (type) == TYPENAME_TYPE)
23691 if (typedef_variant_p (type))
23692 error_at (declarator_id_start_token->location,
23693 "cannot define member of dependent typedef "
23694 "%qT", type);
23695 else
23696 error_at (declarator_id_start_token->location,
23697 "%<%T::%E%> is not a type",
23698 TYPE_CONTEXT (qualifying_scope),
23699 TYPE_IDENTIFIER (qualifying_scope));
23701 qualifying_scope = type;
23704 sfk = sfk_none;
23706 if (unqualified_name)
23708 tree class_type;
23710 if (qualifying_scope
23711 && CLASS_TYPE_P (qualifying_scope))
23712 class_type = qualifying_scope;
23713 else
23714 class_type = current_class_type;
23716 if (TREE_CODE (unqualified_name) == TYPE_DECL)
23718 tree name_type = TREE_TYPE (unqualified_name);
23720 if (!class_type || !same_type_p (name_type, class_type))
23722 /* We do not attempt to print the declarator
23723 here because we do not have enough
23724 information about its original syntactic
23725 form. */
23726 cp_parser_error (parser, "invalid declarator");
23727 declarator = cp_error_declarator;
23728 break;
23730 else if (qualifying_scope
23731 && CLASSTYPE_USE_TEMPLATE (name_type))
23733 error_at (declarator_id_start_token->location,
23734 "invalid use of constructor as a template");
23735 inform (declarator_id_start_token->location,
23736 "use %<%T::%D%> instead of %<%T::%D%> to "
23737 "name the constructor in a qualified name",
23738 class_type,
23739 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23740 class_type, name_type);
23741 declarator = cp_error_declarator;
23742 break;
23744 unqualified_name = constructor_name (class_type);
23747 if (class_type)
23749 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23750 sfk = sfk_destructor;
23751 else if (identifier_p (unqualified_name)
23752 && IDENTIFIER_CONV_OP_P (unqualified_name))
23753 sfk = sfk_conversion;
23754 else if (/* There's no way to declare a constructor
23755 for an unnamed type, even if the type
23756 got a name for linkage purposes. */
23757 !TYPE_WAS_UNNAMED (class_type)
23758 /* Handle correctly (c++/19200):
23760 struct S {
23761 struct T{};
23762 friend void S(T);
23765 and also:
23767 namespace N {
23768 void S();
23771 struct S {
23772 friend void N::S();
23773 }; */
23774 && (!friend_p || class_type == qualifying_scope)
23775 && constructor_name_p (unqualified_name,
23776 class_type))
23777 sfk = sfk_constructor;
23778 else if (is_overloaded_fn (unqualified_name)
23779 && DECL_CONSTRUCTOR_P (get_first_fn
23780 (unqualified_name)))
23781 sfk = sfk_constructor;
23783 if (ctor_dtor_or_conv_p && sfk != sfk_none)
23784 *ctor_dtor_or_conv_p = -1;
23787 declarator = make_id_declarator (qualifying_scope,
23788 unqualified_name,
23789 sfk, token->location);
23790 declarator->std_attributes = attrs;
23791 declarator->parameter_pack_p = pack_expansion_p;
23793 if (pack_expansion_p)
23794 maybe_warn_variadic_templates ();
23796 /* We're looking for this case in [temp.res]:
23797 A qualified-id is assumed to name a type if [...]
23798 - it is a decl-specifier of the decl-specifier-seq of a
23799 parameter-declaration in a declarator of a function or
23800 function template declaration, ... */
23801 if (cxx_dialect >= cxx20
23802 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
23803 && declarator->kind == cdk_id
23804 && !at_class_scope_p ()
23805 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23807 /* ...whose declarator-id is qualified. If it isn't, never
23808 assume the parameters to refer to types. */
23809 if (qualifying_scope == NULL_TREE)
23810 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23811 else
23813 /* Now we have something like
23814 template <typename T> int C::x(S::p);
23815 which can be a function template declaration or a
23816 variable template definition. If name lookup for
23817 the declarator-id C::x finds one or more function
23818 templates, assume S::p to name a type. Otherwise,
23819 don't. */
23820 tree decl
23821 = cp_parser_lookup_name (parser, unqualified_name,
23822 none_type,
23823 /*is_template=*/false,
23824 /*is_namespace=*/false,
23825 /*check_dependency=*/false,
23826 /*ambiguous_decls=*/NULL,
23827 token->location);
23829 if (!is_overloaded_fn (decl)
23830 /* Allow
23831 template<typename T>
23832 A<T>::A(T::type) { } */
23833 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
23834 && constructor_name_p (unqualified_name,
23835 qualifying_scope)))
23836 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23841 handle_declarator:;
23842 scope = get_scope_of_declarator (declarator);
23843 if (scope)
23845 /* Any names that appear after the declarator-id for a
23846 member are looked up in the containing scope. */
23847 if (at_function_scope_p ())
23849 /* But declarations with qualified-ids can't appear in a
23850 function. */
23851 cp_parser_error (parser, "qualified-id in declaration");
23852 declarator = cp_error_declarator;
23853 break;
23855 pushed_scope = push_scope (scope);
23857 parser->in_declarator_p = true;
23858 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
23859 || (declarator && declarator->kind == cdk_id))
23860 /* Default args are only allowed on function
23861 declarations. */
23862 parser->default_arg_ok_p = saved_default_arg_ok_p;
23863 else
23864 parser->default_arg_ok_p = false;
23866 first = false;
23868 /* We're done. */
23869 else
23870 break;
23873 /* For an abstract declarator, we might wind up with nothing at this
23874 point. That's an error; the declarator is not optional. */
23875 if (!declarator)
23876 cp_parser_error (parser, "expected declarator");
23877 else if (open_paren)
23879 /* Record overly parenthesized declarator so we can give a
23880 diagnostic about confusing decl/expr disambiguation. */
23881 if (declarator->kind == cdk_array)
23883 /* If the open and close parens are on different lines, this
23884 is probably a formatting thing, so ignore. */
23885 expanded_location open = expand_location (open_paren->location);
23886 expanded_location close = expand_location (close_paren->location);
23887 if (open.line != close.line || open.file != close.file)
23888 open_paren = NULL;
23890 if (open_paren)
23891 declarator->parenthesized = make_location (open_paren->location,
23892 open_paren->location,
23893 close_paren->location);
23896 /* If we entered a scope, we must exit it now. */
23897 if (pushed_scope)
23898 pop_scope (pushed_scope);
23900 parser->default_arg_ok_p = saved_default_arg_ok_p;
23901 parser->in_declarator_p = saved_in_declarator_p;
23903 return declarator;
23906 /* Parse a ptr-operator.
23908 ptr-operator:
23909 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23910 * cv-qualifier-seq [opt]
23912 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23913 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23915 GNU Extension:
23917 ptr-operator:
23918 & cv-qualifier-seq [opt]
23920 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23921 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23922 an rvalue reference. In the case of a pointer-to-member, *TYPE is
23923 filled in with the TYPE containing the member. *CV_QUALS is
23924 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23925 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
23926 Note that the tree codes returned by this function have nothing
23927 to do with the types of trees that will be eventually be created
23928 to represent the pointer or reference type being parsed. They are
23929 just constants with suggestive names. */
23930 static enum tree_code
23931 cp_parser_ptr_operator (cp_parser* parser,
23932 tree* type,
23933 cp_cv_quals *cv_quals,
23934 tree *attributes)
23936 enum tree_code code = ERROR_MARK;
23937 cp_token *token;
23938 tree attrs = NULL_TREE;
23940 /* Assume that it's not a pointer-to-member. */
23941 *type = NULL_TREE;
23942 /* And that there are no cv-qualifiers. */
23943 *cv_quals = TYPE_UNQUALIFIED;
23945 /* Peek at the next token. */
23946 token = cp_lexer_peek_token (parser->lexer);
23948 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
23949 if (token->type == CPP_MULT)
23950 code = INDIRECT_REF;
23951 else if (token->type == CPP_AND)
23952 code = ADDR_EXPR;
23953 else if ((cxx_dialect != cxx98) &&
23954 token->type == CPP_AND_AND) /* C++0x only */
23955 code = NON_LVALUE_EXPR;
23957 if (code != ERROR_MARK)
23959 /* Consume the `*', `&' or `&&'. */
23960 cp_lexer_consume_token (parser->lexer);
23962 /* A `*' can be followed by a cv-qualifier-seq, and so can a
23963 `&', if we are allowing GNU extensions. (The only qualifier
23964 that can legally appear after `&' is `restrict', but that is
23965 enforced during semantic analysis. */
23966 if (code == INDIRECT_REF
23967 || cp_parser_allow_gnu_extensions_p (parser))
23968 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23970 attrs = cp_parser_std_attribute_spec_seq (parser);
23971 if (attributes != NULL)
23972 *attributes = attrs;
23974 else
23976 /* Try the pointer-to-member case. */
23977 cp_parser_parse_tentatively (parser);
23978 /* Look for the optional `::' operator. */
23979 cp_parser_global_scope_opt (parser,
23980 /*current_scope_valid_p=*/false);
23981 /* Look for the nested-name specifier. */
23982 token = cp_lexer_peek_token (parser->lexer);
23983 cp_parser_nested_name_specifier (parser,
23984 /*typename_keyword_p=*/false,
23985 /*check_dependency_p=*/true,
23986 /*type_p=*/false,
23987 /*is_declaration=*/false);
23988 /* If we found it, and the next token is a `*', then we are
23989 indeed looking at a pointer-to-member operator. */
23990 if (!cp_parser_error_occurred (parser)
23991 && cp_parser_require (parser, CPP_MULT, RT_MULT))
23993 /* Indicate that the `*' operator was used. */
23994 code = INDIRECT_REF;
23996 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
23997 error_at (token->location, "%qD is a namespace", parser->scope);
23998 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
23999 error_at (token->location, "cannot form pointer to member of "
24000 "non-class %q#T", parser->scope);
24001 else
24003 /* The type of which the member is a member is given by the
24004 current SCOPE. */
24005 *type = parser->scope;
24006 /* The next name will not be qualified. */
24007 parser->scope = NULL_TREE;
24008 parser->qualifying_scope = NULL_TREE;
24009 parser->object_scope = NULL_TREE;
24010 /* Look for optional c++11 attributes. */
24011 attrs = cp_parser_std_attribute_spec_seq (parser);
24012 if (attributes != NULL)
24013 *attributes = attrs;
24014 /* Look for the optional cv-qualifier-seq. */
24015 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24018 /* If that didn't work we don't have a ptr-operator. */
24019 if (!cp_parser_parse_definitely (parser))
24020 cp_parser_error (parser, "expected ptr-operator");
24023 return code;
24026 /* Parse an (optional) cv-qualifier-seq.
24028 cv-qualifier-seq:
24029 cv-qualifier cv-qualifier-seq [opt]
24031 cv-qualifier:
24032 const
24033 volatile
24035 GNU Extension:
24037 cv-qualifier:
24038 __restrict__
24040 Returns a bitmask representing the cv-qualifiers. */
24042 static cp_cv_quals
24043 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
24045 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
24047 while (true)
24049 cp_token *token;
24050 cp_cv_quals cv_qualifier;
24052 /* Peek at the next token. */
24053 token = cp_lexer_peek_token (parser->lexer);
24054 /* See if it's a cv-qualifier. */
24055 switch (token->keyword)
24057 case RID_CONST:
24058 cv_qualifier = TYPE_QUAL_CONST;
24059 break;
24061 case RID_VOLATILE:
24062 cv_qualifier = TYPE_QUAL_VOLATILE;
24063 break;
24065 case RID_RESTRICT:
24066 cv_qualifier = TYPE_QUAL_RESTRICT;
24067 break;
24069 default:
24070 cv_qualifier = TYPE_UNQUALIFIED;
24071 break;
24074 if (!cv_qualifier)
24075 break;
24077 if (cv_quals & cv_qualifier)
24079 gcc_rich_location richloc (token->location);
24080 richloc.add_fixit_remove ();
24081 error_at (&richloc, "duplicate cv-qualifier");
24082 cp_lexer_purge_token (parser->lexer);
24084 else
24086 cp_lexer_consume_token (parser->lexer);
24087 cv_quals |= cv_qualifier;
24091 return cv_quals;
24094 /* Parse an (optional) ref-qualifier
24096 ref-qualifier:
24100 Returns cp_ref_qualifier representing ref-qualifier. */
24102 static cp_ref_qualifier
24103 cp_parser_ref_qualifier_opt (cp_parser* parser)
24105 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
24107 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
24108 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
24109 return ref_qual;
24111 while (true)
24113 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
24114 cp_token *token = cp_lexer_peek_token (parser->lexer);
24116 switch (token->type)
24118 case CPP_AND:
24119 curr_ref_qual = REF_QUAL_LVALUE;
24120 break;
24122 case CPP_AND_AND:
24123 curr_ref_qual = REF_QUAL_RVALUE;
24124 break;
24126 default:
24127 curr_ref_qual = REF_QUAL_NONE;
24128 break;
24131 if (!curr_ref_qual)
24132 break;
24133 else if (ref_qual)
24135 error_at (token->location, "multiple ref-qualifiers");
24136 cp_lexer_purge_token (parser->lexer);
24138 else
24140 ref_qual = curr_ref_qual;
24141 cp_lexer_consume_token (parser->lexer);
24145 return ref_qual;
24148 /* Parse an optional tx-qualifier.
24150 tx-qualifier:
24151 transaction_safe
24152 transaction_safe_dynamic */
24154 static tree
24155 cp_parser_tx_qualifier_opt (cp_parser *parser)
24157 cp_token *token = cp_lexer_peek_token (parser->lexer);
24158 if (token->type == CPP_NAME)
24160 tree name = token->u.value;
24161 const char *p = IDENTIFIER_POINTER (name);
24162 const int len = strlen ("transaction_safe");
24163 if (startswith (p, "transaction_safe"))
24165 p += len;
24166 if (*p == '\0'
24167 || !strcmp (p, "_dynamic"))
24169 cp_lexer_consume_token (parser->lexer);
24170 if (!flag_tm)
24172 error ("%qE requires %<-fgnu-tm%>", name);
24173 return NULL_TREE;
24175 else
24176 return name;
24180 return NULL_TREE;
24183 /* Parse an (optional) virt-specifier-seq.
24185 virt-specifier-seq:
24186 virt-specifier virt-specifier-seq [opt]
24188 virt-specifier:
24189 override
24190 final
24192 Returns a bitmask representing the virt-specifiers. */
24194 static cp_virt_specifiers
24195 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24197 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24199 while (true)
24201 cp_token *token;
24202 cp_virt_specifiers virt_specifier;
24204 /* Peek at the next token. */
24205 token = cp_lexer_peek_token (parser->lexer);
24206 /* See if it's a virt-specifier-qualifier. */
24207 if (token->type != CPP_NAME)
24208 break;
24209 if (id_equal (token->u.value, "override"))
24211 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24212 virt_specifier = VIRT_SPEC_OVERRIDE;
24214 else if (id_equal (token->u.value, "final"))
24216 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24217 virt_specifier = VIRT_SPEC_FINAL;
24219 else if (id_equal (token->u.value, "__final"))
24221 virt_specifier = VIRT_SPEC_FINAL;
24223 else
24224 break;
24226 if (virt_specifiers & virt_specifier)
24228 gcc_rich_location richloc (token->location);
24229 richloc.add_fixit_remove ();
24230 error_at (&richloc, "duplicate virt-specifier");
24231 cp_lexer_purge_token (parser->lexer);
24233 else
24235 cp_lexer_consume_token (parser->lexer);
24236 virt_specifiers |= virt_specifier;
24239 return virt_specifiers;
24242 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24243 is in scope even though it isn't real. */
24245 void
24246 inject_this_parameter (tree ctype, cp_cv_quals quals)
24248 tree this_parm;
24250 if (current_class_ptr)
24252 /* We don't clear this between NSDMIs. Is it already what we want? */
24253 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24254 if (DECL_P (current_class_ptr)
24255 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24256 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24257 && cp_type_quals (type) == quals)
24258 return;
24261 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24262 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24263 current_class_ptr = NULL_TREE;
24264 current_class_ref
24265 = cp_build_fold_indirect_ref (this_parm);
24266 current_class_ptr = this_parm;
24269 /* Return true iff our current scope is a non-static data member
24270 initializer. */
24272 bool
24273 parsing_nsdmi (void)
24275 /* We recognize NSDMI context by the context-less 'this' pointer set up
24276 by the function above. */
24277 if (current_class_ptr
24278 && TREE_CODE (current_class_ptr) == PARM_DECL
24279 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24280 return true;
24281 return false;
24284 /* True if we're parsing a function declarator. */
24286 bool
24287 parsing_function_declarator ()
24289 /* this_entity is NULL for a function parameter scope while parsing the
24290 declarator; it is set when parsing the body of the function. */
24291 return (current_binding_level->kind == sk_function_parms
24292 && !current_binding_level->this_entity);
24295 /* Parse a late-specified return type, if any. This is not a separate
24296 non-terminal, but part of a function declarator, which looks like
24298 -> trailing-type-specifier-seq abstract-declarator(opt)
24300 Returns the type indicated by the type-id.
24302 In addition to this, parse any queued up #pragma omp declare simd
24303 clauses, and #pragma acc routine clauses.
24305 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24306 function. */
24308 static tree
24309 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24310 tree& requires_clause)
24312 cp_token *token;
24313 tree type = NULL_TREE;
24314 bool declare_simd_p = (parser->omp_declare_simd
24315 && declarator
24316 && declarator->kind == cdk_id);
24318 bool oacc_routine_p = (parser->oacc_routine
24319 && declarator
24320 && declarator->kind == cdk_id);
24322 /* Peek at the next token. */
24323 token = cp_lexer_peek_token (parser->lexer);
24324 /* A late-specified return type is indicated by an initial '->'. */
24325 if (token->type != CPP_DEREF
24326 && token->keyword != RID_REQUIRES
24327 && !(token->type == CPP_NAME
24328 && token->u.value == ridpointers[RID_REQUIRES])
24329 && !(declare_simd_p || oacc_routine_p))
24330 return NULL_TREE;
24332 if (token->type == CPP_DEREF)
24334 /* Consume the ->. */
24335 cp_lexer_consume_token (parser->lexer);
24337 type = cp_parser_trailing_type_id (parser);
24340 /* Function declarations may be followed by a trailing
24341 requires-clause. */
24342 requires_clause = cp_parser_requires_clause_opt (parser, false);
24344 if (declare_simd_p)
24345 declarator->attributes
24346 = cp_parser_late_parsing_omp_declare_simd (parser,
24347 declarator->attributes);
24348 if (oacc_routine_p)
24349 declarator->attributes
24350 = cp_parser_late_parsing_oacc_routine (parser,
24351 declarator->attributes);
24353 return type;
24356 /* Parse a declarator-id.
24358 declarator-id:
24359 id-expression
24360 :: [opt] nested-name-specifier [opt] type-name
24362 In the `id-expression' case, the value returned is as for
24363 cp_parser_id_expression if the id-expression was an unqualified-id.
24364 If the id-expression was a qualified-id, then a SCOPE_REF is
24365 returned. The first operand is the scope (either a NAMESPACE_DECL
24366 or TREE_TYPE), but the second is still just a representation of an
24367 unqualified-id. */
24369 static tree
24370 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24372 tree id;
24373 /* The expression must be an id-expression. Assume that qualified
24374 names are the names of types so that:
24376 template <class T>
24377 int S<T>::R::i = 3;
24379 will work; we must treat `S<T>::R' as the name of a type.
24380 Similarly, assume that qualified names are templates, where
24381 required, so that:
24383 template <class T>
24384 int S<T>::R<T>::i = 3;
24386 will work, too. */
24387 id = cp_parser_id_expression (parser,
24388 /*template_keyword_p=*/false,
24389 /*check_dependency_p=*/false,
24390 /*template_p=*/NULL,
24391 /*declarator_p=*/true,
24392 optional_p);
24393 if (id && BASELINK_P (id))
24394 id = BASELINK_FUNCTIONS (id);
24395 return id;
24398 /* Parse a type-id.
24400 type-id:
24401 type-specifier-seq abstract-declarator [opt]
24403 The parser flags FLAGS is used to control type-specifier parsing.
24405 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24407 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24408 i.e. we've just seen "->".
24410 Returns the TYPE specified. */
24412 static tree
24413 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24414 bool is_template_arg, bool is_trailing_return,
24415 location_t *type_location)
24417 cp_decl_specifier_seq type_specifier_seq;
24418 cp_declarator *abstract_declarator;
24420 /* Parse the type-specifier-seq. */
24421 cp_parser_type_specifier_seq (parser, flags,
24422 /*is_declaration=*/false,
24423 is_trailing_return,
24424 &type_specifier_seq);
24425 if (type_location)
24426 *type_location = type_specifier_seq.locations[ds_type_spec];
24428 if (is_template_arg && type_specifier_seq.type
24429 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24430 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24431 /* A bare template name as a template argument is a template template
24432 argument, not a placeholder, so fail parsing it as a type argument. */
24434 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24435 cp_parser_simulate_error (parser);
24436 return error_mark_node;
24438 if (type_specifier_seq.type == error_mark_node)
24439 return error_mark_node;
24441 /* There might or might not be an abstract declarator. */
24442 cp_parser_parse_tentatively (parser);
24443 /* Look for the declarator. */
24444 abstract_declarator
24445 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24446 CP_PARSER_FLAGS_NONE, NULL,
24447 /*parenthesized_p=*/NULL,
24448 /*member_p=*/false,
24449 /*friend_p=*/false,
24450 /*static_p=*/false);
24451 /* Check to see if there really was a declarator. */
24452 if (!cp_parser_parse_definitely (parser))
24453 abstract_declarator = NULL;
24455 bool auto_typeid_ok = false;
24456 /* The concepts TS allows 'auto' as a type-id. */
24457 if (flag_concepts_ts)
24458 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24459 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24460 outside the template-argument-list context here only for the sake of
24461 diagnostic: grokdeclarator then can emit a better error message for
24462 e.g. using T = auto. */
24463 else if (flag_concepts)
24464 auto_typeid_ok = (!parser->in_type_id_in_expr_p
24465 && !parser->in_template_argument_list_p);
24467 if (type_specifier_seq.type
24468 && !auto_typeid_ok
24469 /* None of the valid uses of 'auto' in C++14 involve the type-id
24470 nonterminal, but it is valid in a trailing-return-type. */
24471 && !(cxx_dialect >= cxx14 && is_trailing_return))
24472 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24474 /* A type-id with type 'auto' is only ok if the abstract declarator
24475 is a function declarator with a late-specified return type.
24477 A type-id with 'auto' is also valid in a trailing-return-type
24478 in a compound-requirement. */
24479 if (abstract_declarator
24480 && abstract_declarator->kind == cdk_function
24481 && abstract_declarator->u.function.late_return_type)
24482 /* OK */;
24483 else if (parser->in_result_type_constraint_p)
24484 /* OK */;
24485 else
24487 if (!cp_parser_simulate_error (parser))
24489 location_t loc = type_specifier_seq.locations[ds_type_spec];
24490 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24492 auto_diagnostic_group g;
24493 gcc_rich_location richloc (loc);
24494 richloc.add_fixit_insert_after ("<>");
24495 error_at (&richloc, "missing template arguments after %qE",
24496 tmpl);
24497 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24498 tmpl);
24500 else if (parser->in_template_argument_list_p)
24501 error_at (loc, "%qT not permitted in template argument",
24502 auto_node);
24503 else
24504 error_at (loc, "invalid use of %qT", auto_node);
24506 return error_mark_node;
24510 return groktypename (&type_specifier_seq, abstract_declarator,
24511 is_template_arg);
24514 /* Wrapper for cp_parser_type_id_1. */
24516 static tree
24517 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24518 location_t *type_location)
24520 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24523 /* Wrapper for cp_parser_type_id_1. */
24525 static tree
24526 cp_parser_template_type_arg (cp_parser *parser)
24528 tree r;
24529 const char *saved_message = parser->type_definition_forbidden_message;
24530 parser->type_definition_forbidden_message
24531 = G_("types may not be defined in template arguments");
24532 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24533 parser->type_definition_forbidden_message = saved_message;
24534 return r;
24537 /* Wrapper for cp_parser_type_id_1. */
24539 static tree
24540 cp_parser_trailing_type_id (cp_parser *parser)
24542 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24543 false, true, NULL);
24546 /* Parse a type-specifier-seq.
24548 type-specifier-seq:
24549 type-specifier type-specifier-seq [opt]
24551 GNU extension:
24553 type-specifier-seq:
24554 attributes type-specifier-seq [opt]
24556 The parser flags FLAGS is used to control type-specifier parsing.
24558 If IS_DECLARATION is true, we are at the start of a "condition" or
24559 exception-declaration, so we might be followed by a declarator-id.
24561 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24562 i.e. we've just seen "->".
24564 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24566 static void
24567 cp_parser_type_specifier_seq (cp_parser* parser,
24568 cp_parser_flags flags,
24569 bool is_declaration,
24570 bool is_trailing_return,
24571 cp_decl_specifier_seq *type_specifier_seq)
24573 bool seen_type_specifier = false;
24574 cp_token *start_token = NULL;
24576 /* Clear the TYPE_SPECIFIER_SEQ. */
24577 clear_decl_specs (type_specifier_seq);
24579 flags |= CP_PARSER_FLAGS_OPTIONAL;
24580 /* In the context of a trailing return type, enum E { } is an
24581 elaborated-type-specifier followed by a function-body, not an
24582 enum-specifier. */
24583 if (is_trailing_return)
24584 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24586 /* Parse the type-specifiers and attributes. */
24587 while (true)
24589 tree type_specifier;
24590 bool is_cv_qualifier;
24592 /* Check for attributes first. */
24593 if (cp_next_tokens_can_be_attribute_p (parser))
24595 /* GNU attributes at the end of a declaration apply to the
24596 declaration as a whole, not to the trailing return type. So look
24597 ahead to see if these attributes are at the end. */
24598 if (seen_type_specifier && is_trailing_return
24599 && cp_next_tokens_can_be_gnu_attribute_p (parser))
24601 size_t n = cp_parser_skip_attributes_opt (parser, 1);
24602 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24603 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24604 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24605 break;
24607 type_specifier_seq->attributes
24608 = attr_chainon (type_specifier_seq->attributes,
24609 cp_parser_attributes_opt (parser));
24610 continue;
24613 /* record the token of the beginning of the type specifier seq,
24614 for error reporting purposes*/
24615 if (!start_token)
24616 start_token = cp_lexer_peek_token (parser->lexer);
24618 /* Look for the type-specifier. */
24619 type_specifier = cp_parser_type_specifier (parser,
24620 flags,
24621 type_specifier_seq,
24622 /*is_declaration=*/false,
24623 NULL,
24624 &is_cv_qualifier);
24625 if (!type_specifier)
24627 /* If the first type-specifier could not be found, this is not a
24628 type-specifier-seq at all. */
24629 if (!seen_type_specifier)
24631 /* Set in_declarator_p to avoid skipping to the semicolon. */
24632 int in_decl = parser->in_declarator_p;
24633 parser->in_declarator_p = true;
24635 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24636 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24637 cp_parser_error (parser, "expected type-specifier");
24639 parser->in_declarator_p = in_decl;
24641 type_specifier_seq->type = error_mark_node;
24642 return;
24644 /* If subsequent type-specifiers could not be found, the
24645 type-specifier-seq is complete. */
24646 break;
24649 seen_type_specifier = true;
24650 /* The standard says that a condition can be:
24652 type-specifier-seq declarator = assignment-expression
24654 However, given:
24656 struct S {};
24657 if (int S = ...)
24659 we should treat the "S" as a declarator, not as a
24660 type-specifier. The standard doesn't say that explicitly for
24661 type-specifier-seq, but it does say that for
24662 decl-specifier-seq in an ordinary declaration. Perhaps it
24663 would be clearer just to allow a decl-specifier-seq here, and
24664 then add a semantic restriction that if any decl-specifiers
24665 that are not type-specifiers appear, the program is invalid. */
24666 if (is_declaration && !is_cv_qualifier)
24667 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24671 /* Return whether the function currently being declared has an associated
24672 template parameter list. */
24674 static bool
24675 function_being_declared_is_template_p (cp_parser* parser)
24677 if (!current_template_parms || processing_template_parmlist)
24678 return false;
24680 if (parser->implicit_template_scope)
24681 return true;
24683 if (at_class_scope_p ()
24684 && TYPE_BEING_DEFINED (current_class_type))
24685 return parser->num_template_parameter_lists != 0;
24687 return ((int) parser->num_template_parameter_lists > template_class_depth
24688 (current_class_type));
24691 /* Parse a parameter-declaration-clause.
24693 parameter-declaration-clause:
24694 parameter-declaration-list [opt] ... [opt]
24695 parameter-declaration-list , ...
24697 The parser flags FLAGS is used to control type-specifier parsing.
24699 Returns a representation for the parameter declarations. A return
24700 value of NULL indicates a parameter-declaration-clause consisting
24701 only of an ellipsis. */
24703 static tree
24704 cp_parser_parameter_declaration_clause (cp_parser* parser,
24705 cp_parser_flags flags)
24707 tree parameters;
24708 cp_token *token;
24709 bool ellipsis_p;
24711 auto cleanup = make_temp_override
24712 (parser->auto_is_implicit_function_template_parm_p);
24714 if (!processing_specialization
24715 && !processing_template_parmlist
24716 && !processing_explicit_instantiation
24717 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24718 actual function or a random abstract declarator. */
24719 && parser->default_arg_ok_p)
24720 if (!current_function_decl
24721 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24722 parser->auto_is_implicit_function_template_parm_p = true;
24724 /* Peek at the next token. */
24725 token = cp_lexer_peek_token (parser->lexer);
24726 /* Check for trivial parameter-declaration-clauses. */
24727 if (token->type == CPP_ELLIPSIS)
24729 /* Consume the `...' token. */
24730 cp_lexer_consume_token (parser->lexer);
24731 return NULL_TREE;
24733 else if (token->type == CPP_CLOSE_PAREN)
24734 /* There are no parameters. */
24735 return void_list_node;
24736 /* Check for `(void)', too, which is a special case. */
24737 else if (token->keyword == RID_VOID
24738 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24739 == CPP_CLOSE_PAREN))
24741 /* Consume the `void' token. */
24742 cp_lexer_consume_token (parser->lexer);
24743 /* There are no parameters. */
24744 return explicit_void_list_node;
24747 /* A vector of parameters that haven't been pushed yet. */
24748 auto_vec<tree> pending_decls;
24750 /* Parse the parameter-declaration-list. */
24751 parameters = cp_parser_parameter_declaration_list (parser, flags,
24752 &pending_decls);
24753 /* If a parse error occurred while parsing the
24754 parameter-declaration-list, then the entire
24755 parameter-declaration-clause is erroneous. */
24756 if (parameters == error_mark_node)
24757 return NULL_TREE;
24759 /* Peek at the next token. */
24760 token = cp_lexer_peek_token (parser->lexer);
24761 /* If it's a `,', the clause should terminate with an ellipsis. */
24762 if (token->type == CPP_COMMA)
24764 /* Consume the `,'. */
24765 cp_lexer_consume_token (parser->lexer);
24766 /* Expect an ellipsis. */
24767 ellipsis_p
24768 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24770 /* It might also be `...' if the optional trailing `,' was
24771 omitted. */
24772 else if (token->type == CPP_ELLIPSIS)
24774 /* Consume the `...' token. */
24775 cp_lexer_consume_token (parser->lexer);
24776 /* And remember that we saw it. */
24777 ellipsis_p = true;
24779 else
24780 ellipsis_p = false;
24782 /* A valid parameter-declaration-clause can only be followed by a ')'.
24783 So it's time to push all the parameters we have seen now that we
24784 know we have a valid declaration. Note that here we may not have
24785 committed yet, nor should we. Pushing here will detect the error
24786 of redefining a parameter. */
24787 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24788 for (tree p : pending_decls)
24789 pushdecl (p);
24791 /* Finish the parameter list. */
24792 if (!ellipsis_p)
24793 parameters = chainon (parameters, void_list_node);
24795 return parameters;
24798 /* Parse a parameter-declaration-list.
24800 parameter-declaration-list:
24801 parameter-declaration
24802 parameter-declaration-list , parameter-declaration
24804 The parser flags FLAGS is used to control type-specifier parsing.
24805 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
24807 Returns a representation of the parameter-declaration-list, as for
24808 cp_parser_parameter_declaration_clause. However, the
24809 `void_list_node' is never appended to the list. */
24811 static tree
24812 cp_parser_parameter_declaration_list (cp_parser* parser,
24813 cp_parser_flags flags,
24814 auto_vec<tree> *pending_decls)
24816 tree parameters = NULL_TREE;
24817 tree *tail = &parameters;
24818 bool saved_in_unbraced_linkage_specification_p;
24819 int index = 0;
24821 /* The special considerations that apply to a function within an
24822 unbraced linkage specifications do not apply to the parameters
24823 to the function. */
24824 saved_in_unbraced_linkage_specification_p
24825 = parser->in_unbraced_linkage_specification_p;
24826 parser->in_unbraced_linkage_specification_p = false;
24828 /* Look for more parameters. */
24829 while (true)
24831 cp_parameter_declarator *parameter;
24832 tree decl = error_mark_node;
24833 bool parenthesized_p = false;
24835 /* Parse the parameter. */
24836 parameter
24837 = cp_parser_parameter_declaration (parser, flags,
24838 /*template_parm_p=*/false,
24839 &parenthesized_p);
24841 /* We don't know yet if the enclosing context is unavailable or deprecated,
24842 so wait and deal with it in grokparms if appropriate. */
24843 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
24845 if (parameter && !cp_parser_error_occurred (parser))
24847 decl = grokdeclarator (parameter->declarator,
24848 &parameter->decl_specifiers,
24849 PARM,
24850 parameter->default_argument != NULL_TREE,
24851 &parameter->decl_specifiers.attributes);
24852 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
24853 DECL_SOURCE_LOCATION (decl) = parameter->loc;
24856 deprecated_state = DEPRECATED_NORMAL;
24858 /* If a parse error occurred parsing the parameter declaration,
24859 then the entire parameter-declaration-list is erroneous. */
24860 if (decl == error_mark_node)
24862 parameters = error_mark_node;
24863 break;
24866 if (parameter->decl_specifiers.attributes)
24867 cplus_decl_attributes (&decl,
24868 parameter->decl_specifiers.attributes,
24870 if (DECL_NAME (decl))
24872 /* We cannot always pushdecl while parsing tentatively because
24873 it may have side effects and we can't be sure yet if we're
24874 parsing a declaration, e.g.:
24876 S foo(int(x), int(x), int{x});
24878 where it's not clear if we're dealing with a constructor call
24879 or a function declaration until we've seen the last argument
24880 which breaks it up.
24881 It's safe to pushdecl so long as it doesn't result in a clash
24882 with an already-pushed parameter. But we don't delay pushing
24883 different parameters to handle
24885 S foo(int(i), decltype(i) j = 42);
24887 which is valid. */
24888 if (pending_decls
24889 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24890 /* See if PARAMETERS already contains a parameter with the same
24891 DECL_NAME as DECL. */
24892 && [parameters, decl] {
24893 for (tree p = parameters; p; p = TREE_CHAIN (p))
24894 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
24895 return true;
24896 return false;
24897 }())
24898 pending_decls->safe_push (decl);
24899 else
24900 decl = pushdecl (decl);
24903 if (decl != error_mark_node)
24905 retrofit_lang_decl (decl);
24906 DECL_PARM_INDEX (decl) = ++index;
24907 DECL_PARM_LEVEL (decl) = function_parm_depth ();
24910 /* Add the new parameter to the list. */
24911 *tail = build_tree_list (parameter->default_argument, decl);
24912 tail = &TREE_CHAIN (*tail);
24914 /* If the parameters were parenthesized, it's the case of
24915 T foo(X(x)) which looks like a variable definition but
24916 is a function declaration. */
24917 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
24918 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
24920 /* Peek at the next token. */
24921 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
24922 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
24923 /* These are for Objective-C++ */
24924 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24925 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24926 /* The parameter-declaration-list is complete. */
24927 break;
24928 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24930 cp_token *token;
24932 /* Peek at the next token. */
24933 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24934 /* If it's an ellipsis, then the list is complete. */
24935 if (token->type == CPP_ELLIPSIS)
24936 break;
24937 /* Otherwise, there must be more parameters. Consume the
24938 `,'. */
24939 cp_lexer_consume_token (parser->lexer);
24940 /* When parsing something like:
24942 int i(float f, double d)
24944 we can tell after seeing the declaration for "f" that we
24945 are not looking at an initialization of a variable "i",
24946 but rather at the declaration of a function "i".
24948 Due to the fact that the parsing of template arguments
24949 (as specified to a template-id) requires backtracking we
24950 cannot use this technique when inside a template argument
24951 list. */
24952 if (!parser->in_template_argument_list_p
24953 && !parser->in_type_id_in_expr_p
24954 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24955 /* However, a parameter-declaration of the form
24956 "float(f)" (which is a valid declaration of a
24957 parameter "f") can also be interpreted as an
24958 expression (the conversion of "f" to "float"). */
24959 && !parenthesized_p)
24960 cp_parser_commit_to_tentative_parse (parser);
24962 else
24964 cp_parser_error (parser, "expected %<,%> or %<...%>");
24965 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24966 cp_parser_skip_to_closing_parenthesis (parser,
24967 /*recovering=*/true,
24968 /*or_comma=*/false,
24969 /*consume_paren=*/false);
24970 break;
24974 parser->in_unbraced_linkage_specification_p
24975 = saved_in_unbraced_linkage_specification_p;
24977 /* Reset implicit_template_scope if we are about to leave the function
24978 parameter list that introduced it. Note that for out-of-line member
24979 definitions, there will be one or more class scopes before we get to
24980 the template parameter scope. */
24982 if (cp_binding_level *its = parser->implicit_template_scope)
24983 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
24985 while (maybe_its->kind == sk_class)
24986 maybe_its = maybe_its->level_chain;
24987 if (maybe_its == its)
24989 parser->implicit_template_parms = 0;
24990 parser->implicit_template_scope = 0;
24994 return parameters;
24997 /* Parse a parameter declaration.
24999 parameter-declaration:
25000 decl-specifier-seq ... [opt] declarator
25001 decl-specifier-seq declarator = assignment-expression
25002 decl-specifier-seq ... [opt] abstract-declarator [opt]
25003 decl-specifier-seq abstract-declarator [opt] = assignment-expression
25005 The parser flags FLAGS is used to control type-specifier parsing.
25007 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
25008 declares a template parameter. (In that case, a non-nested `>'
25009 token encountered during the parsing of the assignment-expression
25010 is not interpreted as a greater-than operator.)
25012 Returns a representation of the parameter, or NULL if an error
25013 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
25014 true iff the declarator is of the form "(p)". */
25016 static cp_parameter_declarator *
25017 cp_parser_parameter_declaration (cp_parser *parser,
25018 cp_parser_flags flags,
25019 bool template_parm_p,
25020 bool *parenthesized_p)
25022 int declares_class_or_enum;
25023 cp_decl_specifier_seq decl_specifiers;
25024 cp_declarator *declarator;
25025 tree default_argument;
25026 cp_token *token = NULL, *declarator_token_start = NULL;
25027 const char *saved_message;
25028 bool template_parameter_pack_p = false;
25030 /* In a template parameter, `>' is not an operator.
25032 [temp.param]
25034 When parsing a default template-argument for a non-type
25035 template-parameter, the first non-nested `>' is taken as the end
25036 of the template parameter-list rather than a greater-than
25037 operator. */
25039 /* Type definitions may not appear in parameter types. */
25040 saved_message = parser->type_definition_forbidden_message;
25041 parser->type_definition_forbidden_message
25042 = G_("types may not be defined in parameter types");
25044 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
25045 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
25046 (current_template_parms)) : 0);
25048 /* Parse the declaration-specifiers. */
25049 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25050 cp_parser_decl_specifier_seq (parser,
25051 flags,
25052 &decl_specifiers,
25053 &declares_class_or_enum);
25055 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
25056 type-constraint opt auto can be used as a decl-specifier of the
25057 decl-specifier-seq of a parameter-declaration of a function declaration
25058 or lambda-expression..." but we must not synthesize an implicit template
25059 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
25060 we want to synthesize only the first auto. */
25061 auto cleanup = make_temp_override
25062 (parser->auto_is_implicit_function_template_parm_p, false);
25064 /* Complain about missing 'typename' or other invalid type names. */
25065 if (!decl_specifiers.any_type_specifiers_p
25066 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25067 decl_specifiers.type = error_mark_node;
25069 /* If an error occurred, there's no reason to attempt to parse the
25070 rest of the declaration. */
25071 if (cp_parser_error_occurred (parser))
25073 parser->type_definition_forbidden_message = saved_message;
25074 return NULL;
25077 /* Peek at the next token. */
25078 token = cp_lexer_peek_token (parser->lexer);
25080 /* If the next token is a `)', `,', `=', `>', or `...', then there
25081 is no declarator. However, when variadic templates are enabled,
25082 there may be a declarator following `...'. */
25083 if (token->type == CPP_CLOSE_PAREN
25084 || token->type == CPP_COMMA
25085 || token->type == CPP_EQ
25086 || token->type == CPP_GREATER)
25088 declarator = NULL;
25089 if (parenthesized_p)
25090 *parenthesized_p = false;
25092 /* Otherwise, there should be a declarator. */
25093 else
25095 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
25096 parser->default_arg_ok_p = false;
25098 /* After seeing a decl-specifier-seq, if the next token is not a
25099 "(" or "{", there is no possibility that the code is a valid
25100 expression. Therefore, if parsing tentatively, we commit at
25101 this point. */
25102 if (!parser->in_template_argument_list_p
25103 /* In an expression context, having seen:
25105 (int((char ...
25107 we cannot be sure whether we are looking at a
25108 function-type (taking a "char" as a parameter) or a cast
25109 of some object of type "char" to "int". */
25110 && !parser->in_type_id_in_expr_p
25111 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25112 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
25114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25116 if (decl_specifiers.type
25117 && template_placeholder_p (decl_specifiers.type))
25118 /* This is a CTAD expression, not a parameter declaration. */
25119 cp_parser_simulate_error (parser);
25121 else
25122 cp_parser_commit_to_tentative_parse (parser);
25124 /* Parse the declarator. */
25125 declarator_token_start = token;
25126 declarator = cp_parser_declarator (parser,
25127 CP_PARSER_DECLARATOR_EITHER,
25128 CP_PARSER_FLAGS_NONE,
25129 /*ctor_dtor_or_conv_p=*/NULL,
25130 parenthesized_p,
25131 /*member_p=*/false,
25132 /*friend_p=*/false,
25133 /*static_p=*/false);
25134 parser->default_arg_ok_p = saved_default_arg_ok_p;
25135 /* After the declarator, allow more attributes. */
25136 decl_specifiers.attributes
25137 = attr_chainon (decl_specifiers.attributes,
25138 cp_parser_attributes_opt (parser));
25140 /* If the declarator is a template parameter pack, remember that and
25141 clear the flag in the declarator itself so we don't get errors
25142 from grokdeclarator. */
25143 if (template_parm_p && declarator && declarator->parameter_pack_p)
25145 declarator->parameter_pack_p = false;
25146 template_parameter_pack_p = true;
25150 /* If the next token is an ellipsis, and we have not seen a declarator
25151 name, and if either the type of the declarator contains parameter
25152 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25153 for, eg, abbreviated integral type names), then we actually have a
25154 parameter pack expansion expression. Otherwise, leave the ellipsis
25155 for a C-style variadic function. */
25156 token = cp_lexer_peek_token (parser->lexer);
25158 /* If a function parameter pack was specified and an implicit template
25159 parameter was introduced during cp_parser_parameter_declaration,
25160 change any implicit parameters introduced into packs. */
25161 if (parser->implicit_template_parms
25162 && ((token->type == CPP_ELLIPSIS
25163 && declarator_can_be_parameter_pack (declarator))
25164 || (declarator && declarator->parameter_pack_p)))
25166 int latest_template_parm_idx = TREE_VEC_LENGTH
25167 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
25169 if (latest_template_parm_idx != template_parm_idx)
25170 decl_specifiers.type = convert_generic_types_to_packs
25171 (decl_specifiers.type,
25172 template_parm_idx, latest_template_parm_idx);
25175 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25177 tree type = decl_specifiers.type;
25179 if (type && DECL_P (type))
25180 type = TREE_TYPE (type);
25182 if (((type
25183 && TREE_CODE (type) != TYPE_PACK_EXPANSION
25184 && (template_parm_p || uses_parameter_packs (type)))
25185 || (!type && template_parm_p))
25186 && declarator_can_be_parameter_pack (declarator))
25188 /* Consume the `...'. */
25189 cp_lexer_consume_token (parser->lexer);
25190 maybe_warn_variadic_templates ();
25192 /* Build a pack expansion type */
25193 if (template_parm_p)
25194 template_parameter_pack_p = true;
25195 else if (declarator)
25196 declarator->parameter_pack_p = true;
25197 else
25198 decl_specifiers.type = make_pack_expansion (type);
25202 /* The restriction on defining new types applies only to the type
25203 of the parameter, not to the default argument. */
25204 parser->type_definition_forbidden_message = saved_message;
25206 /* If the next token is `=', then process a default argument. */
25207 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25209 tree type = decl_specifiers.type;
25210 token = cp_lexer_peek_token (parser->lexer);
25211 if (declarator)
25212 declarator->init_loc = token->location;
25213 /* If we are defining a class, then the tokens that make up the
25214 default argument must be saved and processed later. */
25215 if (!template_parm_p && at_class_scope_p ()
25216 && TYPE_BEING_DEFINED (current_class_type)
25217 && !LAMBDA_TYPE_P (current_class_type))
25218 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25220 /* A constrained-type-specifier may declare a type
25221 template-parameter. */
25222 else if (declares_constrained_type_template_parameter (type))
25223 default_argument
25224 = cp_parser_default_type_template_argument (parser);
25226 /* A constrained-type-specifier may declare a
25227 template-template-parameter. */
25228 else if (declares_constrained_template_template_parameter (type))
25229 default_argument
25230 = cp_parser_default_template_template_argument (parser);
25232 /* Outside of a class definition, we can just parse the
25233 assignment-expression. */
25234 else
25235 default_argument
25236 = cp_parser_default_argument (parser, template_parm_p);
25238 if (!parser->default_arg_ok_p)
25240 permerror (token->location,
25241 "default arguments are only "
25242 "permitted for function parameters");
25244 else if ((declarator && declarator->parameter_pack_p)
25245 || template_parameter_pack_p
25246 || (decl_specifiers.type
25247 && PACK_EXPANSION_P (decl_specifiers.type)))
25249 /* Find the name of the parameter pack. */
25250 cp_declarator *id_declarator = declarator;
25251 while (id_declarator && id_declarator->kind != cdk_id)
25252 id_declarator = id_declarator->declarator;
25254 if (id_declarator && id_declarator->kind == cdk_id)
25255 error_at (declarator_token_start->location,
25256 template_parm_p
25257 ? G_("template parameter pack %qD "
25258 "cannot have a default argument")
25259 : G_("parameter pack %qD cannot have "
25260 "a default argument"),
25261 id_declarator->u.id.unqualified_name);
25262 else
25263 error_at (declarator_token_start->location,
25264 template_parm_p
25265 ? G_("template parameter pack cannot have "
25266 "a default argument")
25267 : G_("parameter pack cannot have a "
25268 "default argument"));
25270 default_argument = NULL_TREE;
25273 else
25274 default_argument = NULL_TREE;
25276 if (default_argument)
25277 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25279 /* Generate a location for the parameter, ranging from the start of the
25280 initial token to the end of the final token (using input_location for
25281 the latter, set up by cp_lexer_set_source_position_from_token when
25282 consuming tokens).
25284 If we have a identifier, then use it for the caret location, e.g.
25286 extern int callee (int one, int (*two)(int, int), float three);
25287 ~~~~~~^~~~~~~~~~~~~~
25289 otherwise, reuse the start location for the caret location e.g.:
25291 extern int callee (int one, int (*)(int, int), float three);
25292 ^~~~~~~~~~~~~~~~~
25295 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25296 ? declarator->id_loc
25297 : decl_spec_token_start->location);
25298 location_t param_loc = make_location (caret_loc,
25299 decl_spec_token_start->location,
25300 input_location);
25302 return make_parameter_declarator (&decl_specifiers,
25303 declarator,
25304 default_argument,
25305 param_loc,
25306 template_parameter_pack_p);
25309 /* Parse a default argument and return it.
25311 TEMPLATE_PARM_P is true if this is a default argument for a
25312 non-type template parameter. */
25313 static tree
25314 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25316 tree default_argument = NULL_TREE;
25317 bool saved_greater_than_is_operator_p;
25318 unsigned char saved_local_variables_forbidden_p;
25319 bool non_constant_p, is_direct_init;
25321 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25322 set correctly. */
25323 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25324 parser->greater_than_is_operator_p = !template_parm_p;
25325 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25326 auto ord = make_temp_override (parser->oacc_routine, NULL);
25327 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25329 /* Local variable names (and the `this' keyword) may not
25330 appear in a default argument. */
25331 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25332 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25333 /* Parse the assignment-expression. */
25334 if (template_parm_p)
25335 push_deferring_access_checks (dk_no_deferred);
25336 tree saved_class_ptr = NULL_TREE;
25337 tree saved_class_ref = NULL_TREE;
25338 /* The "this" pointer is not valid in a default argument. */
25339 if (cfun)
25341 saved_class_ptr = current_class_ptr;
25342 cp_function_chain->x_current_class_ptr = NULL_TREE;
25343 saved_class_ref = current_class_ref;
25344 cp_function_chain->x_current_class_ref = NULL_TREE;
25346 default_argument
25347 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
25348 /* Restore the "this" pointer. */
25349 if (cfun)
25351 cp_function_chain->x_current_class_ptr = saved_class_ptr;
25352 cp_function_chain->x_current_class_ref = saved_class_ref;
25354 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25355 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25356 if (template_parm_p)
25357 pop_deferring_access_checks ();
25358 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25359 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25361 return default_argument;
25364 /* Parse a function-body.
25366 function-body:
25367 compound_statement */
25369 static void
25370 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25372 cp_parser_compound_statement (parser, NULL, (in_function_try_block
25373 ? BCS_TRY_BLOCK : BCS_NORMAL),
25374 true);
25377 /* Parse a ctor-initializer-opt followed by a function-body. Return
25378 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25379 is true we are parsing a function-try-block. */
25381 static void
25382 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25383 bool in_function_try_block)
25385 tree body, list;
25386 const bool check_body_p
25387 = (DECL_CONSTRUCTOR_P (current_function_decl)
25388 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25389 tree last = NULL;
25391 if (in_function_try_block
25392 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25393 && cxx_dialect < cxx20)
25395 if (DECL_CONSTRUCTOR_P (current_function_decl))
25396 pedwarn (input_location, OPT_Wc__20_extensions,
25397 "function-try-block body of %<constexpr%> constructor only "
25398 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25399 else
25400 pedwarn (input_location, OPT_Wc__20_extensions,
25401 "function-try-block body of %<constexpr%> function only "
25402 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25405 /* Begin the function body. */
25406 body = begin_function_body ();
25407 /* Parse the optional ctor-initializer. */
25408 cp_parser_ctor_initializer_opt (parser);
25410 /* If we're parsing a constexpr constructor definition, we need
25411 to check that the constructor body is indeed empty. However,
25412 before we get to cp_parser_function_body lot of junk has been
25413 generated, so we can't just check that we have an empty block.
25414 Rather we take a snapshot of the outermost block, and check whether
25415 cp_parser_function_body changed its state. */
25416 if (check_body_p)
25418 list = cur_stmt_list;
25419 if (STATEMENT_LIST_TAIL (list))
25420 last = STATEMENT_LIST_TAIL (list)->stmt;
25422 /* Parse the function-body. */
25423 cp_parser_function_body (parser, in_function_try_block);
25424 if (check_body_p)
25425 check_constexpr_ctor_body (last, list, /*complain=*/true);
25426 /* Finish the function body. */
25427 finish_function_body (body);
25430 /* Parse an initializer.
25432 initializer:
25433 = initializer-clause
25434 ( expression-list )
25436 Returns an expression representing the initializer. If no
25437 initializer is present, NULL_TREE is returned.
25439 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25440 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25441 set to TRUE if there is no initializer present. If there is an
25442 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25443 is set to true; otherwise it is set to false. */
25445 static tree
25446 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
25447 bool* non_constant_p, bool subexpression_p)
25449 cp_token *token;
25450 tree init;
25452 /* Peek at the next token. */
25453 token = cp_lexer_peek_token (parser->lexer);
25455 /* Let our caller know whether or not this initializer was
25456 parenthesized. */
25457 *is_direct_init = (token->type != CPP_EQ);
25458 /* Assume that the initializer is constant. */
25459 *non_constant_p = false;
25461 if (token->type == CPP_EQ)
25463 /* Consume the `='. */
25464 cp_lexer_consume_token (parser->lexer);
25465 /* Parse the initializer-clause. */
25466 init = cp_parser_initializer_clause (parser, non_constant_p);
25468 else if (token->type == CPP_OPEN_PAREN)
25470 vec<tree, va_gc> *vec;
25471 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25472 /*cast_p=*/false,
25473 /*allow_expansion_p=*/true,
25474 non_constant_p);
25475 if (vec == NULL)
25476 return error_mark_node;
25477 init = build_tree_list_vec (vec);
25478 release_tree_vector (vec);
25480 else if (token->type == CPP_OPEN_BRACE)
25482 cp_lexer_set_source_position (parser->lexer);
25483 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25484 init = cp_parser_braced_list (parser, non_constant_p);
25485 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25487 else
25489 /* Anything else is an error. */
25490 cp_parser_error (parser, "expected initializer");
25491 init = error_mark_node;
25494 if (!subexpression_p && check_for_bare_parameter_packs (init))
25495 init = error_mark_node;
25497 return init;
25500 /* Parse an initializer-clause.
25502 initializer-clause:
25503 assignment-expression
25504 braced-init-list
25506 Returns an expression representing the initializer.
25508 If the `assignment-expression' production is used the value
25509 returned is simply a representation for the expression.
25511 Otherwise, calls cp_parser_braced_list. */
25513 static cp_expr
25514 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25516 cp_expr initializer;
25518 /* Assume the expression is constant. */
25519 *non_constant_p = false;
25521 /* If it is not a `{', then we are looking at an
25522 assignment-expression. */
25523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25525 initializer
25526 = cp_parser_constant_expression (parser,
25527 /*allow_non_constant_p=*/2,
25528 non_constant_p);
25530 else
25531 initializer = cp_parser_braced_list (parser, non_constant_p);
25533 return initializer;
25536 /* Parse a brace-enclosed initializer list.
25538 braced-init-list:
25539 { initializer-list , [opt] }
25540 { designated-initializer-list , [opt] }
25543 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25544 the elements of the initializer-list (or NULL, if the last
25545 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25546 NULL_TREE. There is no way to detect whether or not the optional
25547 trailing `,' was provided. NON_CONSTANT_P is as for
25548 cp_parser_initializer. */
25550 static cp_expr
25551 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
25553 tree initializer;
25554 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25556 /* Consume the `{' token. */
25557 matching_braces braces;
25558 braces.require_open (parser);
25559 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25560 initializer = make_node (CONSTRUCTOR);
25561 /* If it's not a `}', then there is a non-trivial initializer. */
25562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25564 bool designated;
25565 /* Parse the initializer list. */
25566 CONSTRUCTOR_ELTS (initializer)
25567 = cp_parser_initializer_list (parser, non_constant_p, &designated);
25568 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25569 /* A trailing `,' token is allowed. */
25570 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25571 cp_lexer_consume_token (parser->lexer);
25573 else
25574 *non_constant_p = false;
25575 /* Now, there should be a trailing `}'. */
25576 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25577 braces.require_close (parser);
25578 TREE_TYPE (initializer) = init_list_type_node;
25579 recompute_constructor_flags (initializer);
25581 cp_expr result (initializer);
25582 /* Build a location of the form:
25583 { ... }
25584 ^~~~~~~
25585 with caret==start at the open brace, finish at the close brace. */
25586 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25587 result.set_location (combined_loc);
25588 return result;
25591 /* Consume tokens up to, but not including, the next non-nested closing `]'.
25592 Returns true iff we found a closing `]'. */
25594 static bool
25595 cp_parser_skip_up_to_closing_square_bracket (cp_parser *parser)
25597 unsigned square_depth = 0;
25599 while (true)
25601 cp_token * token = cp_lexer_peek_token (parser->lexer);
25603 switch (token->type)
25605 case CPP_PRAGMA_EOL:
25606 if (!parser->lexer->in_pragma)
25607 break;
25608 /* FALLTHRU */
25610 case CPP_EOF:
25611 /* If we've run out of tokens, then there is no closing `]'. */
25612 return false;
25614 case CPP_OPEN_SQUARE:
25615 ++square_depth;
25616 break;
25618 case CPP_CLOSE_SQUARE:
25619 if (!square_depth--)
25620 return true;
25621 break;
25623 default:
25624 break;
25627 /* Consume the current token, skipping it. */
25628 cp_lexer_consume_token (parser->lexer);
25632 /* Consume tokens up to, and including, the next non-nested closing `]'.
25633 Returns true iff we found a closing `]'. */
25635 static bool
25636 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25638 bool found = cp_parser_skip_up_to_closing_square_bracket (parser);
25639 if (found)
25640 cp_lexer_consume_token (parser->lexer);
25641 return found;
25644 /* Return true if we are looking at an array-designator, false otherwise. */
25646 static bool
25647 cp_parser_array_designator_p (cp_parser *parser)
25649 /* Consume the `['. */
25650 cp_lexer_consume_token (parser->lexer);
25652 cp_lexer_save_tokens (parser->lexer);
25654 /* Skip tokens until the next token is a closing square bracket.
25655 If we find the closing `]', and the next token is a `=', then
25656 we are looking at an array designator. */
25657 bool array_designator_p
25658 = (cp_parser_skip_to_closing_square_bracket (parser)
25659 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25661 /* Roll back the tokens we skipped. */
25662 cp_lexer_rollback_tokens (parser->lexer);
25664 return array_designator_p;
25667 /* Parse an initializer-list.
25669 initializer-list:
25670 initializer-clause ... [opt]
25671 initializer-list , initializer-clause ... [opt]
25673 C++20 Extension:
25675 designated-initializer-list:
25676 designated-initializer-clause
25677 designated-initializer-list , designated-initializer-clause
25679 designated-initializer-clause:
25680 designator brace-or-equal-initializer
25682 designator:
25683 . identifier
25685 GNU Extension:
25687 initializer-list:
25688 designation initializer-clause ...[opt]
25689 initializer-list , designation initializer-clause ...[opt]
25691 designation:
25692 . identifier =
25693 identifier :
25694 [ constant-expression ] =
25696 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25697 for the initializer. If the INDEX of the elt is non-NULL, it is the
25698 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25699 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25700 are any designators. */
25702 static vec<constructor_elt, va_gc> *
25703 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25704 bool *designated)
25706 vec<constructor_elt, va_gc> *v = NULL;
25707 bool first_p = true;
25708 tree first_designator = NULL_TREE;
25710 /* Assume all of the expressions are constant. */
25711 *non_constant_p = false;
25713 unsigned nelts = 0;
25714 int suppress = suppress_location_wrappers;
25716 /* Parse the rest of the list. */
25717 while (true)
25719 cp_token *token;
25720 tree designator;
25721 tree initializer;
25722 bool clause_non_constant_p;
25723 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25725 /* Handle the C++20 syntax, '. id ='. */
25726 if ((cxx_dialect >= cxx20
25727 || cp_parser_allow_gnu_extensions_p (parser))
25728 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25729 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25730 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25731 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25732 == CPP_OPEN_BRACE)))
25734 if (pedantic && cxx_dialect < cxx20)
25735 pedwarn (loc, OPT_Wc__20_extensions,
25736 "C++ designated initializers only available with "
25737 "%<-std=c++20%> or %<-std=gnu++20%>");
25738 /* Consume the `.'. */
25739 cp_lexer_consume_token (parser->lexer);
25740 /* Consume the identifier. */
25741 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25742 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25743 /* Consume the `='. */
25744 cp_lexer_consume_token (parser->lexer);
25746 /* Also, if the next token is an identifier and the following one is a
25747 colon, we are looking at the GNU designated-initializer
25748 syntax. */
25749 else if (cp_parser_allow_gnu_extensions_p (parser)
25750 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25751 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25752 == CPP_COLON))
25754 /* Warn the user that they are using an extension. */
25755 pedwarn (loc, OPT_Wpedantic,
25756 "ISO C++ does not allow GNU designated initializers");
25757 /* Consume the identifier. */
25758 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25759 /* Consume the `:'. */
25760 cp_lexer_consume_token (parser->lexer);
25762 /* Also handle C99 array designators, '[ const ] ='. */
25763 else if (cp_parser_allow_gnu_extensions_p (parser)
25764 && !c_dialect_objc ()
25765 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25767 /* In C++11, [ could start a lambda-introducer. */
25768 bool non_const = false;
25770 cp_parser_parse_tentatively (parser);
25772 if (!cp_parser_array_designator_p (parser))
25774 cp_parser_simulate_error (parser);
25775 designator = NULL_TREE;
25777 else
25779 designator = cp_parser_constant_expression (parser, true,
25780 &non_const);
25781 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25782 cp_parser_require (parser, CPP_EQ, RT_EQ);
25785 if (!cp_parser_parse_definitely (parser))
25786 designator = NULL_TREE;
25787 else if (non_const
25788 && (!require_potential_rvalue_constant_expression
25789 (designator)))
25790 designator = NULL_TREE;
25791 if (designator)
25792 /* Warn the user that they are using an extension. */
25793 pedwarn (loc, OPT_Wpedantic,
25794 "ISO C++ does not allow C99 designated initializers");
25796 else
25797 designator = NULL_TREE;
25799 if (first_p)
25801 first_designator = designator;
25802 first_p = false;
25804 else if (cxx_dialect >= cxx20
25805 && first_designator != error_mark_node
25806 && (!first_designator != !designator))
25808 error_at (loc, "either all initializer clauses should be designated "
25809 "or none of them should be");
25810 first_designator = error_mark_node;
25812 else if (cxx_dialect < cxx20 && !first_designator)
25813 first_designator = designator;
25815 /* Parse the initializer. */
25816 initializer = cp_parser_initializer_clause (parser,
25817 &clause_non_constant_p);
25818 /* If any clause is non-constant, so is the entire initializer. */
25819 if (clause_non_constant_p)
25820 *non_constant_p = true;
25822 /* If we have an ellipsis, this is an initializer pack
25823 expansion. */
25824 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25826 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25828 /* Consume the `...'. */
25829 cp_lexer_consume_token (parser->lexer);
25831 if (designator && cxx_dialect >= cxx20)
25832 error_at (loc,
25833 "%<...%> not allowed in designated initializer list");
25835 /* Turn the initializer into an initializer expansion. */
25836 initializer = make_pack_expansion (initializer);
25839 /* Add it to the vector. */
25840 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
25842 /* If the next token is not a comma, we have reached the end of
25843 the list. */
25844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25845 break;
25847 /* Peek at the next token. */
25848 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25849 /* If the next token is a `}', then we're still done. An
25850 initializer-clause can have a trailing `,' after the
25851 initializer-list and before the closing `}'. */
25852 if (token->type == CPP_CLOSE_BRACE)
25853 break;
25855 /* Suppress location wrappers in a long initializer to save memory
25856 (14179). The cutoff is chosen arbitrarily. */
25857 const unsigned loc_max = 256;
25858 unsigned incr = 1;
25859 if (TREE_CODE (initializer) == CONSTRUCTOR)
25860 /* Look one level down because it's easy. Looking deeper would require
25861 passing down a nelts pointer, and I don't think multi-level massive
25862 initializers are common enough to justify this. */
25863 incr = CONSTRUCTOR_NELTS (initializer);
25864 nelts += incr;
25865 if (nelts >= loc_max && (nelts - incr) < loc_max)
25866 ++suppress_location_wrappers;
25868 /* Consume the `,' token. */
25869 cp_lexer_consume_token (parser->lexer);
25872 /* The same identifier shall not appear in multiple designators
25873 of a designated-initializer-list. */
25874 if (first_designator)
25876 unsigned int i;
25877 tree designator, val;
25878 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25879 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25881 if (IDENTIFIER_MARKED (designator))
25883 error_at (cp_expr_loc_or_input_loc (val),
25884 "%<.%s%> designator used multiple times in "
25885 "the same initializer list",
25886 IDENTIFIER_POINTER (designator));
25887 (*v)[i].index = error_mark_node;
25889 else
25890 IDENTIFIER_MARKED (designator) = 1;
25892 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25893 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25894 IDENTIFIER_MARKED (designator) = 0;
25897 suppress_location_wrappers = suppress;
25899 *designated = first_designator != NULL_TREE;
25900 return v;
25903 /* Classes [gram.class] */
25905 /* Parse a class-name.
25907 class-name:
25908 identifier
25909 template-id
25911 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25912 to indicate that names looked up in dependent types should be
25913 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
25914 keyword has been used to indicate that the name that appears next
25915 is a template. TAG_TYPE indicates the explicit tag given before
25916 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
25917 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
25918 is the class being defined in a class-head. If ENUM_OK is TRUE,
25919 enum-names are also accepted.
25921 Returns the TYPE_DECL representing the class. */
25923 static tree
25924 cp_parser_class_name (cp_parser *parser,
25925 bool typename_keyword_p,
25926 bool template_keyword_p,
25927 enum tag_types tag_type,
25928 bool check_dependency_p,
25929 bool class_head_p,
25930 bool is_declaration,
25931 bool enum_ok)
25933 tree decl;
25934 tree identifier = NULL_TREE;
25936 /* All class-names start with an identifier. */
25937 cp_token *token = cp_lexer_peek_token (parser->lexer);
25938 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
25940 cp_parser_error (parser, "expected class-name");
25941 return error_mark_node;
25944 /* PARSER->SCOPE can be cleared when parsing the template-arguments
25945 to a template-id, so we save it here. Consider object scope too,
25946 so that make_typename_type below can use it (cp_parser_template_name
25947 considers object scope also). This may happen with code like
25949 p->template A<T>::a()
25951 where we first want to look up A<T>::a in the class of the object
25952 expression, as per [basic.lookup.classref]. */
25953 tree scope = parser->scope ? parser->scope : parser->context->object_type;
25954 /* This only checks parser->scope to avoid duplicate errors; if
25955 ->object_type is erroneous, go on to give a parse error. */
25956 if (parser->scope == error_mark_node)
25957 return error_mark_node;
25959 /* Any name names a type if we're following the `typename' keyword
25960 in a qualified name where the enclosing scope is type-dependent. */
25961 const bool typename_p = (typename_keyword_p
25962 && parser->scope
25963 && TYPE_P (parser->scope)
25964 && dependent_scope_p (parser->scope));
25965 /* Handle the common case (an identifier, but not a template-id)
25966 efficiently. */
25967 if (token->type == CPP_NAME
25968 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
25970 cp_token *identifier_token;
25971 bool ambiguous_p;
25973 /* Look for the identifier. */
25974 identifier_token = cp_lexer_peek_token (parser->lexer);
25975 ambiguous_p = identifier_token->error_reported;
25976 identifier = cp_parser_identifier (parser);
25977 /* If the next token isn't an identifier, we are certainly not
25978 looking at a class-name. */
25979 if (identifier == error_mark_node)
25980 decl = error_mark_node;
25981 /* If we know this is a type-name, there's no need to look it
25982 up. */
25983 else if (typename_p)
25984 decl = identifier;
25985 else
25987 tree ambiguous_decls;
25988 /* If we already know that this lookup is ambiguous, then
25989 we've already issued an error message; there's no reason
25990 to check again. */
25991 if (ambiguous_p)
25993 cp_parser_simulate_error (parser);
25994 return error_mark_node;
25996 /* If the next token is a `::', then the name must be a type
25997 name.
25999 [basic.lookup.qual]
26001 During the lookup for a name preceding the :: scope
26002 resolution operator, object, function, and enumerator
26003 names are ignored. */
26004 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26005 tag_type = scope_type;
26006 /* Look up the name. */
26007 decl = cp_parser_lookup_name (parser, identifier,
26008 tag_type,
26009 /*is_template=*/false,
26010 /*is_namespace=*/false,
26011 check_dependency_p,
26012 &ambiguous_decls,
26013 identifier_token->location);
26014 if (ambiguous_decls)
26016 if (cp_parser_parsing_tentatively (parser))
26017 cp_parser_simulate_error (parser);
26018 return error_mark_node;
26022 else
26024 /* Try a template-id. */
26025 decl = cp_parser_template_id (parser, template_keyword_p,
26026 check_dependency_p,
26027 tag_type,
26028 is_declaration);
26029 if (decl == error_mark_node)
26030 return error_mark_node;
26033 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
26035 /* If this is a typename, create a TYPENAME_TYPE. */
26036 if (typename_p && decl != error_mark_node)
26038 decl = make_typename_type (scope, decl, typename_type,
26039 /*complain=*/tf_error);
26040 if (decl != error_mark_node)
26041 decl = TYPE_NAME (decl);
26044 decl = strip_using_decl (decl);
26046 /* Check to see that it is really the name of a class. */
26047 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
26048 && identifier_p (TREE_OPERAND (decl, 0))
26049 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26050 /* Situations like this:
26052 template <typename T> struct A {
26053 typename T::template X<int>::I i;
26056 are problematic. Is `T::template X<int>' a class-name? The
26057 standard does not seem to be definitive, but there is no other
26058 valid interpretation of the following `::'. Therefore, those
26059 names are considered class-names. */
26061 decl = make_typename_type (scope, decl, tag_type, tf_error);
26062 if (decl != error_mark_node)
26063 decl = TYPE_NAME (decl);
26065 else if (TREE_CODE (decl) != TYPE_DECL
26066 || TREE_TYPE (decl) == error_mark_node
26067 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
26068 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
26069 /* In Objective-C 2.0, a classname followed by '.' starts a
26070 dot-syntax expression, and it's not a type-name. */
26071 || (c_dialect_objc ()
26072 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
26073 && objc_is_class_name (decl)))
26074 decl = error_mark_node;
26076 if (decl == error_mark_node)
26077 cp_parser_error (parser, "expected class-name");
26078 else if (identifier && !parser->scope)
26079 maybe_note_name_used_in_class (identifier, decl);
26081 return decl;
26084 /* Make sure that any member-function parameters are in scope.
26085 For instance, a function's noexcept-specifier can use the function's
26086 parameters:
26088 struct S {
26089 void fn (int p) noexcept(noexcept(p));
26092 so we need to make sure name lookup can find them. This is used
26093 when we delay parsing of the noexcept-specifier. */
26095 static void
26096 inject_parm_decls (tree decl)
26098 begin_scope (sk_function_parms, decl);
26099 tree args = DECL_ARGUMENTS (decl);
26101 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
26103 if (args && is_this_parameter (args))
26105 gcc_checking_assert (current_class_ptr == NULL_TREE);
26106 current_class_ref = cp_build_fold_indirect_ref (args);
26107 current_class_ptr = args;
26111 /* Undo the effects of inject_parm_decls. */
26113 static void
26114 pop_injected_parms (void)
26116 pop_bindings_and_leave_scope ();
26117 current_class_ptr = current_class_ref = NULL_TREE;
26120 /* Parse a class-specifier.
26122 class-specifier:
26123 class-head { member-specification [opt] }
26125 Returns the TREE_TYPE representing the class. */
26127 tree
26128 cp_parser_class_specifier (cp_parser* parser)
26130 auto_timevar tv (TV_PARSE_STRUCT);
26132 tree type;
26133 tree attributes = NULL_TREE;
26134 bool nested_name_specifier_p;
26135 unsigned saved_num_template_parameter_lists;
26136 bool saved_in_function_body;
26137 unsigned char in_statement;
26138 bool in_switch_statement_p;
26139 bool saved_in_unbraced_linkage_specification_p;
26140 tree old_scope = NULL_TREE;
26141 tree scope = NULL_TREE;
26142 cp_token *closing_brace;
26144 push_deferring_access_checks (dk_no_deferred);
26146 /* Parse the class-head. */
26147 type = cp_parser_class_head (parser,
26148 &nested_name_specifier_p);
26149 /* If the class-head was a semantic disaster, skip the entire body
26150 of the class. */
26151 if (!type)
26153 cp_parser_skip_to_end_of_block_or_statement (parser);
26154 pop_deferring_access_checks ();
26155 return error_mark_node;
26158 /* Look for the `{'. */
26159 matching_braces braces;
26160 if (!braces.require_open (parser))
26162 pop_deferring_access_checks ();
26163 return error_mark_node;
26166 cp_ensure_no_omp_declare_simd (parser);
26167 cp_ensure_no_oacc_routine (parser);
26169 /* Issue an error message if type-definitions are forbidden here. */
26170 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
26171 /* Remember that we are defining one more class. */
26172 ++parser->num_classes_being_defined;
26173 /* Inside the class, surrounding template-parameter-lists do not
26174 apply. */
26175 saved_num_template_parameter_lists
26176 = parser->num_template_parameter_lists;
26177 parser->num_template_parameter_lists = 0;
26178 /* We are not in a function body. */
26179 saved_in_function_body = parser->in_function_body;
26180 parser->in_function_body = false;
26181 /* Or in a loop. */
26182 in_statement = parser->in_statement;
26183 parser->in_statement = 0;
26184 /* Or in a switch. */
26185 in_switch_statement_p = parser->in_switch_statement_p;
26186 parser->in_switch_statement_p = false;
26187 /* We are not immediately inside an extern "lang" block. */
26188 saved_in_unbraced_linkage_specification_p
26189 = parser->in_unbraced_linkage_specification_p;
26190 parser->in_unbraced_linkage_specification_p = false;
26191 /* 'this' from an enclosing non-static member function is unavailable. */
26192 tree saved_ccp = current_class_ptr;
26193 tree saved_ccr = current_class_ref;
26194 current_class_ptr = NULL_TREE;
26195 current_class_ref = NULL_TREE;
26197 /* Start the class. */
26198 if (nested_name_specifier_p)
26200 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
26201 /* SCOPE must be a scope nested inside current scope. */
26202 if (is_nested_namespace (current_namespace,
26203 decl_namespace_context (scope)))
26204 old_scope = push_inner_scope (scope);
26205 else
26206 nested_name_specifier_p = false;
26208 type = begin_class_definition (type);
26210 if (type == error_mark_node)
26211 /* If the type is erroneous, skip the entire body of the class. */
26212 cp_parser_skip_to_closing_brace (parser);
26213 else
26214 /* Parse the member-specification. */
26215 cp_parser_member_specification_opt (parser);
26217 /* Look for the trailing `}'. */
26218 closing_brace = braces.require_close (parser);
26219 /* Look for trailing attributes to apply to this class. */
26220 if (cp_parser_allow_gnu_extensions_p (parser))
26221 attributes = cp_parser_gnu_attributes_opt (parser);
26222 if (type != error_mark_node)
26223 type = finish_struct (type, attributes);
26224 if (nested_name_specifier_p)
26225 pop_inner_scope (old_scope, scope);
26227 /* We've finished a type definition. Check for the common syntax
26228 error of forgetting a semicolon after the definition. We need to
26229 be careful, as we can't just check for not-a-semicolon and be done
26230 with it; the user might have typed:
26232 class X { } c = ...;
26233 class X { } *p = ...;
26235 and so forth. Instead, enumerate all the possible tokens that
26236 might follow this production; if we don't see one of them, then
26237 complain and silently insert the semicolon. */
26239 cp_token *token = cp_lexer_peek_token (parser->lexer);
26240 bool want_semicolon = true;
26242 if (cp_next_tokens_can_be_std_attribute_p (parser))
26243 /* Don't try to parse c++11 attributes here. As per the
26244 grammar, that should be a task for
26245 cp_parser_decl_specifier_seq. */
26246 want_semicolon = false;
26248 switch (token->type)
26250 case CPP_NAME:
26251 case CPP_SEMICOLON:
26252 case CPP_MULT:
26253 case CPP_AND:
26254 case CPP_OPEN_PAREN:
26255 case CPP_CLOSE_PAREN:
26256 case CPP_COMMA:
26257 case CPP_SCOPE:
26258 want_semicolon = false;
26259 break;
26261 /* While it's legal for type qualifiers and storage class
26262 specifiers to follow type definitions in the grammar, only
26263 compiler testsuites contain code like that. Assume that if
26264 we see such code, then what we're really seeing is a case
26265 like:
26267 class X { }
26268 const <type> var = ...;
26272 class Y { }
26273 static <type> func (...) ...
26275 i.e. the qualifier or specifier applies to the next
26276 declaration. To do so, however, we need to look ahead one
26277 more token to see if *that* token is a type specifier.
26279 This code could be improved to handle:
26281 class Z { }
26282 static const <type> var = ...; */
26283 case CPP_KEYWORD:
26284 if (keyword_is_decl_specifier (token->keyword))
26286 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26288 /* Handling user-defined types here would be nice, but very
26289 tricky. */
26290 want_semicolon
26291 = (lookahead->type == CPP_KEYWORD
26292 && keyword_begins_type_specifier (lookahead->keyword));
26294 break;
26295 default:
26296 break;
26299 /* If we don't have a type, then something is very wrong and we
26300 shouldn't try to do anything clever. Likewise for not seeing the
26301 closing brace. */
26302 if (closing_brace && TYPE_P (type) && want_semicolon)
26304 /* Locate the closing brace. */
26305 cp_token_position prev
26306 = cp_lexer_previous_token_position (parser->lexer);
26307 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
26308 location_t loc = prev_token->location;
26310 /* We want to suggest insertion of a ';' immediately *after* the
26311 closing brace, so, if we can, offset the location by 1 column. */
26312 location_t next_loc = loc;
26313 if (!linemap_location_from_macro_expansion_p (line_table, loc))
26314 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26316 rich_location richloc (line_table, next_loc);
26318 /* If we successfully offset the location, suggest the fix-it. */
26319 if (next_loc != loc)
26320 richloc.add_fixit_insert_before (next_loc, ";");
26322 if (CLASSTYPE_DECLARED_CLASS (type))
26323 error_at (&richloc,
26324 "expected %<;%> after class definition");
26325 else if (TREE_CODE (type) == RECORD_TYPE)
26326 error_at (&richloc,
26327 "expected %<;%> after struct definition");
26328 else if (TREE_CODE (type) == UNION_TYPE)
26329 error_at (&richloc,
26330 "expected %<;%> after union definition");
26331 else
26332 gcc_unreachable ();
26334 /* Unget one token and smash it to look as though we encountered
26335 a semicolon in the input stream. */
26336 cp_lexer_set_token_position (parser->lexer, prev);
26337 token = cp_lexer_peek_token (parser->lexer);
26338 token->type = CPP_SEMICOLON;
26339 token->keyword = RID_MAX;
26343 /* If this class is not itself within the scope of another class,
26344 then we need to parse the bodies of all of the queued function
26345 definitions. Note that the queued functions defined in a class
26346 are not always processed immediately following the
26347 class-specifier for that class. Consider:
26349 struct A {
26350 struct B { void f() { sizeof (A); } };
26353 If `f' were processed before the processing of `A' were
26354 completed, there would be no way to compute the size of `A'.
26355 Note that the nesting we are interested in here is lexical --
26356 not the semantic nesting given by TYPE_CONTEXT. In particular,
26357 for:
26359 struct A { struct B; };
26360 struct A::B { void f() { } };
26362 there is no need to delay the parsing of `A::B::f'. */
26363 if (--parser->num_classes_being_defined == 0)
26365 tree decl;
26366 tree class_type = NULL_TREE;
26367 tree pushed_scope = NULL_TREE;
26368 unsigned ix;
26369 cp_default_arg_entry *e;
26371 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26373 /* Skip default arguments, NSDMIs, etc, in order to improve
26374 error recovery (c++/71169, c++/71832). */
26375 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26376 vec_safe_truncate (unparsed_nsdmis, 0);
26377 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26380 /* In a first pass, parse default arguments to the functions.
26381 Then, in a second pass, parse the bodies of the functions.
26382 This two-phased approach handles cases like:
26384 struct S {
26385 void f() { g(); }
26386 void g(int i = 3);
26390 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26392 decl = e->decl;
26393 /* If there are default arguments that have not yet been processed,
26394 take care of them now. */
26395 if (class_type != e->class_type)
26397 if (pushed_scope)
26398 pop_scope (pushed_scope);
26399 class_type = e->class_type;
26400 pushed_scope = push_scope (class_type);
26402 /* Make sure that any template parameters are in scope. */
26403 maybe_begin_member_template_processing (decl);
26404 /* Parse the default argument expressions. */
26405 cp_parser_late_parsing_default_args (parser, decl);
26406 /* Remove any template parameters from the symbol table. */
26407 maybe_end_member_template_processing ();
26409 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26411 /* If there are noexcept-specifiers that have not yet been processed,
26412 take care of them now. Do this before processing NSDMIs as they
26413 may depend on noexcept-specifiers already having been processed. */
26414 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26416 tree ctx = DECL_CONTEXT (decl);
26417 if (class_type != ctx)
26419 if (pushed_scope)
26420 pop_scope (pushed_scope);
26421 class_type = ctx;
26422 pushed_scope = push_scope (class_type);
26425 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26426 def_parse = TREE_PURPOSE (def_parse);
26428 /* Make sure that any template parameters are in scope. */
26429 maybe_begin_member_template_processing (decl);
26431 /* Make sure that any member-function parameters are in scope.
26432 This function doesn't expect ccp to be set. */
26433 current_class_ptr = current_class_ref = NULL_TREE;
26434 inject_parm_decls (decl);
26436 /* 'this' is not allowed in static member functions. */
26437 unsigned char local_variables_forbidden_p
26438 = parser->local_variables_forbidden_p;
26439 if (DECL_THIS_STATIC (decl))
26440 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26442 /* Now we can parse the noexcept-specifier. */
26443 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26445 if (spec == error_mark_node)
26446 spec = NULL_TREE;
26448 /* Update the fn's type directly -- it might have escaped
26449 beyond this decl :( */
26450 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26451 /* Update any instantiations we've already created. We must
26452 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26453 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26454 in the pattern. */
26455 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26456 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26457 = spec ? TREE_PURPOSE (spec) : error_mark_node;
26459 /* Restore the state of local_variables_forbidden_p. */
26460 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26462 /* The finish_struct call above performed various override checking,
26463 but it skipped unparsed noexcept-specifier operands. Now that we
26464 have resolved them, check again. */
26465 noexcept_override_late_checks (decl);
26467 /* Remove any member-function parameters from the symbol table. */
26468 pop_injected_parms ();
26470 /* Remove any template parameters from the symbol table. */
26471 maybe_end_member_template_processing ();
26473 vec_safe_truncate (unparsed_noexcepts, 0);
26475 /* Now parse any NSDMIs. */
26476 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26478 tree ctx = type_context_for_name_lookup (decl);
26479 if (class_type != ctx)
26481 if (pushed_scope)
26482 pop_scope (pushed_scope);
26483 class_type = ctx;
26484 pushed_scope = push_scope (class_type);
26486 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26487 cp_parser_late_parsing_nsdmi (parser, decl);
26489 vec_safe_truncate (unparsed_nsdmis, 0);
26491 /* Now contract attributes. */
26492 FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
26494 tree ctx = DECL_CONTEXT (decl);
26495 if (class_type != ctx)
26497 if (pushed_scope)
26498 pop_scope (pushed_scope);
26499 class_type = ctx;
26500 pushed_scope = push_scope (class_type);
26503 temp_override<tree> cfd(current_function_decl, decl);
26505 /* Make sure that any template parameters are in scope. */
26506 maybe_begin_member_template_processing (decl);
26508 /* Make sure that any member-function parameters are in scope.
26509 This function doesn't expect ccp to be set. */
26510 current_class_ptr = current_class_ref = NULL_TREE;
26511 inject_parm_decls (decl);
26513 /* 'this' is not allowed in static member functions. */
26514 unsigned char local_variables_forbidden_p
26515 = parser->local_variables_forbidden_p;
26516 if (DECL_THIS_STATIC (decl))
26517 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26519 /* Now we can parse contract conditions. */
26520 for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
26522 if (cxx_contract_attribute_p (a))
26523 cp_parser_late_contract_condition (parser, decl, a);
26526 /* Restore the state of local_variables_forbidden_p. */
26527 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26529 /* Remove any member-function parameters from the symbol table. */
26530 pop_injected_parms ();
26532 /* Remove any template parameters from the symbol table. */
26533 maybe_end_member_template_processing ();
26535 /* Perform any deferred contract matching. */
26536 match_deferred_contracts (decl);
26538 vec_safe_truncate (unparsed_contracts, 0);
26540 current_class_ptr = NULL_TREE;
26541 current_class_ref = NULL_TREE;
26542 if (pushed_scope)
26543 pop_scope (pushed_scope);
26545 /* Now parse the body of the functions. */
26546 if (flag_openmp)
26548 /* OpenMP UDRs need to be parsed before all other functions. */
26549 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26550 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26551 cp_parser_late_parsing_for_member (parser, decl);
26552 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26553 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26554 cp_parser_late_parsing_for_member (parser, decl);
26556 else
26557 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26558 cp_parser_late_parsing_for_member (parser, decl);
26559 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26562 /* Put back any saved access checks. */
26563 pop_deferring_access_checks ();
26565 /* Restore saved state. */
26566 parser->in_switch_statement_p = in_switch_statement_p;
26567 parser->in_statement = in_statement;
26568 parser->in_function_body = saved_in_function_body;
26569 parser->num_template_parameter_lists
26570 = saved_num_template_parameter_lists;
26571 parser->in_unbraced_linkage_specification_p
26572 = saved_in_unbraced_linkage_specification_p;
26573 current_class_ptr = saved_ccp;
26574 current_class_ref = saved_ccr;
26576 return type;
26579 /* Parse a class-head.
26581 class-head:
26582 class-key identifier [opt] base-clause [opt]
26583 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26584 class-key nested-name-specifier [opt] template-id
26585 base-clause [opt]
26587 class-virt-specifier:
26588 final
26590 GNU Extensions:
26591 class-key attributes identifier [opt] base-clause [opt]
26592 class-key attributes nested-name-specifier identifier base-clause [opt]
26593 class-key attributes nested-name-specifier [opt] template-id
26594 base-clause [opt]
26596 Upon return BASES is initialized to the list of base classes (or
26597 NULL, if there are none) in the same form returned by
26598 cp_parser_base_clause.
26600 Returns the TYPE of the indicated class. Sets
26601 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26602 involving a nested-name-specifier was used, and FALSE otherwise.
26604 Returns error_mark_node if this is not a class-head.
26606 Returns NULL_TREE if the class-head is syntactically valid, but
26607 semantically invalid in a way that means we should skip the entire
26608 body of the class. */
26610 static tree
26611 cp_parser_class_head (cp_parser* parser,
26612 bool* nested_name_specifier_p)
26614 tree nested_name_specifier;
26615 enum tag_types class_key;
26616 tree id = NULL_TREE;
26617 tree type = NULL_TREE;
26618 tree attributes;
26619 tree bases;
26620 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26621 bool template_id_p = false;
26622 bool qualified_p = false;
26623 bool invalid_nested_name_p = false;
26624 bool invalid_explicit_specialization_p = false;
26625 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26626 tree pushed_scope = NULL_TREE;
26627 unsigned num_templates;
26628 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26629 /* Assume no nested-name-specifier will be present. */
26630 *nested_name_specifier_p = false;
26631 /* Assume no template parameter lists will be used in defining the
26632 type. */
26633 num_templates = 0;
26634 parser->colon_corrects_to_scope_p = false;
26636 /* Look for the class-key. */
26637 class_key = cp_parser_class_key (parser);
26638 if (class_key == none_type)
26639 return error_mark_node;
26641 location_t class_head_start_location = input_location;
26643 /* Parse the attributes. */
26644 attributes = cp_parser_attributes_opt (parser);
26645 if (find_contract (attributes))
26646 diagnose_misapplied_contracts (attributes);
26648 /* If the next token is `::', that is invalid -- but sometimes
26649 people do try to write:
26651 struct ::S {};
26653 Handle this gracefully by accepting the extra qualifier, and then
26654 issuing an error about it later if this really is a
26655 class-head. If it turns out just to be an elaborated type
26656 specifier, remain silent. */
26657 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26658 qualified_p = true;
26660 /* It is OK to define an inaccessible class; for example:
26662 class A { class B; };
26663 class A::B {};
26665 So we want to ignore access when parsing the class name.
26666 However, we might be tentatively parsing what is really an
26667 elaborated-type-specifier naming a template-id, e.g.
26669 struct C<&D::m> c;
26671 In this case the tentative parse as a class-head will fail, but not
26672 before cp_parser_template_id splices in a CPP_TEMPLATE_ID token.
26673 Since dk_no_check is sticky, we must instead use dk_deferred so that
26674 any such CPP_TEMPLATE_ID token created during this tentative parse
26675 will correctly capture the access checks imposed by the template-id . */
26676 push_deferring_access_checks (dk_deferred);
26678 /* Determine the name of the class. Begin by looking for an
26679 optional nested-name-specifier. */
26680 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26681 nested_name_specifier
26682 = cp_parser_nested_name_specifier_opt (parser,
26683 /*typename_keyword_p=*/false,
26684 /*check_dependency_p=*/false,
26685 /*type_p=*/true,
26686 /*is_declaration=*/false);
26687 /* If there was a nested-name-specifier, then there *must* be an
26688 identifier. */
26690 cp_token *bad_template_keyword = NULL;
26692 if (nested_name_specifier)
26694 type_start_token = cp_lexer_peek_token (parser->lexer);
26695 /* Although the grammar says `identifier', it really means
26696 `class-name' or `template-name'. You are only allowed to
26697 define a class that has already been declared with this
26698 syntax.
26700 The proposed resolution for Core Issue 180 says that wherever
26701 you see `class T::X' you should treat `X' as a type-name.
26703 We do not know if we will see a class-name, or a
26704 template-name. We look for a class-name first, in case the
26705 class-name is a template-id; if we looked for the
26706 template-name first we would stop after the template-name. */
26707 cp_parser_parse_tentatively (parser);
26708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26709 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26710 type = cp_parser_class_name (parser,
26711 /*typename_keyword_p=*/false,
26712 /*template_keyword_p=*/false,
26713 class_type,
26714 /*check_dependency_p=*/false,
26715 /*class_head_p=*/true,
26716 /*is_declaration=*/false);
26717 /* If that didn't work, ignore the nested-name-specifier. */
26718 if (!cp_parser_parse_definitely (parser))
26720 invalid_nested_name_p = true;
26721 type_start_token = cp_lexer_peek_token (parser->lexer);
26722 id = cp_parser_identifier (parser);
26723 if (id == error_mark_node)
26724 id = NULL_TREE;
26726 /* If we could not find a corresponding TYPE, treat this
26727 declaration like an unqualified declaration. */
26728 if (type == error_mark_node)
26729 nested_name_specifier = NULL_TREE;
26730 /* Otherwise, count the number of templates used in TYPE and its
26731 containing scopes. */
26732 else
26733 num_templates = num_template_headers_for_class (TREE_TYPE (type));
26735 /* Otherwise, the identifier is optional. */
26736 else
26738 /* We don't know whether what comes next is a template-id,
26739 an identifier, or nothing at all. */
26740 cp_parser_parse_tentatively (parser);
26741 /* Check for a template-id. */
26742 type_start_token = cp_lexer_peek_token (parser->lexer);
26743 id = cp_parser_template_id (parser,
26744 /*template_keyword_p=*/false,
26745 /*check_dependency_p=*/true,
26746 class_key,
26747 /*is_declaration=*/true);
26748 /* If that didn't work, it could still be an identifier. */
26749 if (!cp_parser_parse_definitely (parser))
26751 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26753 type_start_token = cp_lexer_peek_token (parser->lexer);
26754 id = cp_parser_identifier (parser);
26756 else
26757 id = NULL_TREE;
26759 else
26761 template_id_p = true;
26762 ++num_templates;
26766 pop_deferring_access_checks ();
26768 if (id)
26770 cp_parser_check_for_invalid_template_id (parser, id,
26771 class_key,
26772 type_start_token->location);
26774 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
26776 /* If it's not a `:' or a `{' then we can't really be looking at a
26777 class-head, since a class-head only appears as part of a
26778 class-specifier. We have to detect this situation before calling
26779 xref_tag, since that has irreversible side-effects. */
26780 if (!cp_parser_next_token_starts_class_definition_p (parser))
26782 cp_parser_error (parser, "expected %<{%> or %<:%>");
26783 type = error_mark_node;
26784 goto out;
26787 /* At this point, we're going ahead with the class-specifier, even
26788 if some other problem occurs. */
26789 cp_parser_commit_to_tentative_parse (parser);
26790 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
26792 cp_parser_error (parser,
26793 "cannot specify %<override%> for a class");
26794 type = error_mark_node;
26795 goto out;
26797 /* Issue the error about the overly-qualified name now. */
26798 if (qualified_p)
26800 cp_parser_error (parser,
26801 "global qualification of class name is invalid");
26802 type = error_mark_node;
26803 goto out;
26805 else if (invalid_nested_name_p)
26807 cp_parser_error (parser,
26808 "qualified name does not name a class");
26809 type = error_mark_node;
26810 goto out;
26812 else if (nested_name_specifier)
26814 tree scope;
26816 if (bad_template_keyword)
26817 /* [temp.names]: in a qualified-id formed by a class-head-name, the
26818 keyword template shall not appear at the top level. */
26819 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
26820 "keyword %<template%> not allowed in class-head-name");
26822 /* Reject typedef-names in class heads. */
26823 if (!DECL_IMPLICIT_TYPEDEF_P (type))
26825 error_at (type_start_token->location,
26826 "invalid class name in declaration of %qD",
26827 type);
26828 type = NULL_TREE;
26829 goto done;
26832 /* Figure out in what scope the declaration is being placed. */
26833 scope = current_scope ();
26834 /* If that scope does not contain the scope in which the
26835 class was originally declared, the program is invalid. */
26836 if (scope && !is_ancestor (scope, nested_name_specifier))
26838 if (at_namespace_scope_p ())
26839 error_at (type_start_token->location,
26840 "declaration of %qD in namespace %qD which does not "
26841 "enclose %qD",
26842 type, scope, nested_name_specifier);
26843 else
26844 error_at (type_start_token->location,
26845 "declaration of %qD in %qD which does not enclose %qD",
26846 type, scope, nested_name_specifier);
26847 type = NULL_TREE;
26848 goto done;
26850 /* [dcl.meaning]
26852 A declarator-id shall not be qualified except for the
26853 definition of a ... nested class outside of its class
26854 ... [or] the definition or explicit instantiation of a
26855 class member of a namespace outside of its namespace. */
26856 if (scope == nested_name_specifier)
26857 permerror (nested_name_specifier_token_start->location,
26858 "extra qualification not allowed");
26860 /* An explicit-specialization must be preceded by "template <>". If
26861 it is not, try to recover gracefully. */
26862 if (at_namespace_scope_p ()
26863 && parser->num_template_parameter_lists == 0
26864 && !processing_template_parmlist
26865 && template_id_p)
26867 /* Build a location of this form:
26868 struct typename <ARGS>
26869 ^~~~~~~~~~~~~~~~~~~~~~
26870 with caret==start at the start token, and
26871 finishing at the end of the type. */
26872 location_t reported_loc
26873 = make_location (class_head_start_location,
26874 class_head_start_location,
26875 get_finish (type_start_token->location));
26876 rich_location richloc (line_table, reported_loc);
26877 richloc.add_fixit_insert_before (class_head_start_location,
26878 "template <> ");
26879 error_at (&richloc,
26880 "an explicit specialization must be preceded by"
26881 " %<template <>%>");
26882 invalid_explicit_specialization_p = true;
26883 /* Take the same action that would have been taken by
26884 cp_parser_explicit_specialization. */
26885 ++parser->num_template_parameter_lists;
26886 begin_specialization ();
26888 /* There must be no "return" statements between this point and the
26889 end of this function; set "type "to the correct return value and
26890 use "goto done;" to return. */
26891 /* Make sure that the right number of template parameters were
26892 present. */
26893 if (!cp_parser_check_template_parameters (parser, num_templates,
26894 template_id_p,
26895 type_start_token->location,
26896 /*declarator=*/NULL))
26898 /* If something went wrong, there is no point in even trying to
26899 process the class-definition. */
26900 type = NULL_TREE;
26901 goto done;
26904 /* Look up the type. */
26905 if (template_id_p)
26907 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
26908 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
26909 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
26911 error_at (type_start_token->location,
26912 "function template %qD redeclared as a class template", id);
26913 type = error_mark_node;
26915 else
26917 type = TREE_TYPE (id);
26918 type = maybe_process_partial_specialization (type);
26920 /* Check the scope while we still know whether or not we had a
26921 nested-name-specifier. */
26922 if (type != error_mark_node)
26923 check_unqualified_spec_or_inst (type, type_start_token->location);
26925 if (nested_name_specifier)
26926 pushed_scope = push_scope (nested_name_specifier);
26928 else if (nested_name_specifier)
26930 type = TREE_TYPE (type);
26932 /* Given:
26934 template <typename T> struct S { struct T };
26935 template <typename T> struct S<T>::T { };
26937 we will get a TYPENAME_TYPE when processing the definition of
26938 `S::T'. We need to resolve it to the actual type before we
26939 try to define it. */
26940 if (TREE_CODE (type) == TYPENAME_TYPE)
26942 type = resolve_typename_type (type, /*only_current_p=*/false);
26943 if (TREE_CODE (type) == TYPENAME_TYPE)
26945 cp_parser_error (parser, "could not resolve typename type");
26946 type = error_mark_node;
26950 type = maybe_process_partial_specialization (type);
26951 if (type == error_mark_node)
26953 type = NULL_TREE;
26954 goto done;
26957 /* Enter the scope indicated by the nested-name-specifier. */
26958 pushed_scope = push_scope (nested_name_specifier);
26959 /* Get the canonical version of this type. */
26960 type = TYPE_MAIN_DECL (type);
26961 /* Call push_template_decl if it seems like we should be defining a
26962 template either from the template headers or the type we're
26963 defining, so that we diagnose both extra and missing headers. */
26964 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26965 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
26966 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
26968 type = push_template_decl (type);
26969 if (type == error_mark_node)
26971 type = NULL_TREE;
26972 goto done;
26976 type = TREE_TYPE (type);
26977 *nested_name_specifier_p = true;
26979 else /* The name is not a nested name. */
26981 /* If the class was unnamed, create a dummy name. */
26982 if (!id)
26983 id = make_anon_name ();
26984 TAG_how how = (parser->in_type_id_in_expr_p
26985 ? TAG_how::INNERMOST_NON_CLASS
26986 : TAG_how::CURRENT_ONLY);
26987 type = xref_tag (class_key, id, how,
26988 parser->num_template_parameter_lists);
26991 /* Diagnose class/struct/union mismatches. */
26992 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
26993 true, true);
26995 /* Indicate whether this class was declared as a `class' or as a
26996 `struct'. */
26997 if (TREE_CODE (type) == RECORD_TYPE)
26998 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
27000 /* If this type was already complete, and we see another definition,
27001 that's an error. Likewise if the type is already being defined:
27002 this can happen, eg, when it's defined from within an expression
27003 (c++/84605). */
27004 if (type != error_mark_node
27005 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
27007 error_at (type_start_token->location, "redefinition of %q#T",
27008 type);
27009 inform (location_of (type), "previous definition of %q#T",
27010 type);
27011 type = NULL_TREE;
27012 goto done;
27014 else if (type == error_mark_node)
27015 type = NULL_TREE;
27017 if (type)
27019 if (current_lambda_expr ()
27020 && uses_parameter_packs (attributes))
27022 /* In a lambda this should work, but doesn't currently. */
27023 sorry ("unexpanded parameter pack in local class in lambda");
27024 attributes = NULL_TREE;
27027 /* Apply attributes now, before any use of the class as a template
27028 argument in its base list. */
27029 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
27030 fixup_attribute_variants (type);
27033 /* Associate constraints with the type. */
27034 if (flag_concepts)
27035 type = associate_classtype_constraints (type);
27037 /* We will have entered the scope containing the class; the names of
27038 base classes should be looked up in that context. For example:
27040 struct A { struct B {}; struct C; };
27041 struct A::C : B {};
27043 is valid. */
27045 /* Get the list of base-classes, if there is one. Defer access checking
27046 until the entire list has been seen, as per [class.access.general]. */
27047 push_deferring_access_checks (dk_deferred);
27048 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27050 if (type)
27051 pushclass (type);
27052 bases = cp_parser_base_clause (parser);
27053 if (type)
27054 popclass ();
27056 else
27057 bases = NULL_TREE;
27059 /* If we're really defining a class, process the base classes.
27060 If they're invalid, fail. */
27061 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27062 xref_basetypes (type, bases);
27064 /* Now that all bases have been seen and attached to the class, check
27065 accessibility of the types named in the base-clause. This must be
27066 done relative to the class scope, so that we accept e.g.
27068 struct A { protected: struct B {}; };
27069 struct C : A::B, A {}; // OK: A::B is accessible via base A
27071 as per [class.access.general]. */
27072 if (type)
27073 pushclass (type);
27074 pop_to_parent_deferring_access_checks ();
27075 if (type)
27076 popclass ();
27078 done:
27079 /* Leave the scope given by the nested-name-specifier. We will
27080 enter the class scope itself while processing the members. */
27081 if (pushed_scope)
27082 pop_scope (pushed_scope);
27084 if (invalid_explicit_specialization_p)
27086 end_specialization ();
27087 --parser->num_template_parameter_lists;
27090 if (type)
27091 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
27092 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
27093 CLASSTYPE_FINAL (type) = 1;
27094 out:
27095 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27096 return type;
27099 /* Parse a class-key.
27101 class-key:
27102 class
27103 struct
27104 union
27106 Returns the kind of class-key specified, or none_type to indicate
27107 error. */
27109 static enum tag_types
27110 cp_parser_class_key (cp_parser* parser)
27112 cp_token *token;
27113 enum tag_types tag_type;
27115 /* Look for the class-key. */
27116 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
27117 if (!token)
27118 return none_type;
27120 /* Check to see if the TOKEN is a class-key. */
27121 tag_type = cp_parser_token_is_class_key (token);
27122 if (!tag_type)
27123 cp_parser_error (parser, "expected class-key");
27124 return tag_type;
27127 /* Parse a type-parameter-key.
27129 type-parameter-key:
27130 class
27131 typename
27134 static void
27135 cp_parser_type_parameter_key (cp_parser* parser)
27137 /* Look for the type-parameter-key. */
27138 enum tag_types tag_type = none_type;
27139 cp_token *token = cp_lexer_peek_token (parser->lexer);
27140 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
27142 cp_lexer_consume_token (parser->lexer);
27143 if (pedantic && tag_type == typename_type
27144 && cxx_dialect < cxx17)
27145 /* typename is not allowed in a template template parameter
27146 by the standard until C++17. */
27147 pedwarn (token->location, OPT_Wc__17_extensions,
27148 "ISO C++ forbids typename key in template template parameter;"
27149 " use %<-std=c++17%> or %<-std=gnu++17%>");
27151 else
27152 cp_parser_error (parser, "expected %<class%> or %<typename%>");
27154 return;
27157 /* Parse an (optional) member-specification.
27159 member-specification:
27160 member-declaration member-specification [opt]
27161 access-specifier : member-specification [opt] */
27163 static void
27164 cp_parser_member_specification_opt (cp_parser* parser)
27166 while (true)
27168 cp_token *token;
27169 enum rid keyword;
27171 /* Peek at the next token. */
27172 token = cp_lexer_peek_token (parser->lexer);
27173 /* If it's a `}', or EOF then we've seen all the members. */
27174 if (token->type == CPP_CLOSE_BRACE
27175 || token->type == CPP_EOF
27176 || token->type == CPP_PRAGMA_EOL)
27177 break;
27179 /* See if this token is a keyword. */
27180 keyword = token->keyword;
27181 switch (keyword)
27183 case RID_PUBLIC:
27184 case RID_PROTECTED:
27185 case RID_PRIVATE:
27186 /* Consume the access-specifier. */
27187 cp_lexer_consume_token (parser->lexer);
27188 /* Remember which access-specifier is active. */
27189 current_access_specifier = token->u.value;
27190 /* Look for the `:'. */
27191 cp_parser_require (parser, CPP_COLON, RT_COLON);
27192 break;
27194 default:
27195 /* Accept #pragmas at class scope. */
27196 if (token->type == CPP_PRAGMA)
27198 cp_parser_pragma (parser, pragma_member, NULL);
27199 break;
27202 /* Otherwise, the next construction must be a
27203 member-declaration. */
27204 cp_parser_member_declaration (parser);
27209 /* Parse a member-declaration.
27211 member-declaration:
27212 decl-specifier-seq [opt] member-declarator-list [opt] ;
27213 function-definition ; [opt]
27214 :: [opt] nested-name-specifier template [opt] unqualified-id ;
27215 using-declaration
27216 template-declaration
27217 alias-declaration
27219 member-declarator-list:
27220 member-declarator
27221 member-declarator-list , member-declarator
27223 member-declarator:
27224 declarator pure-specifier [opt]
27225 declarator constant-initializer [opt]
27226 identifier [opt] : constant-expression
27228 GNU Extensions:
27230 member-declaration:
27231 __extension__ member-declaration
27233 member-declarator:
27234 declarator attributes [opt] pure-specifier [opt]
27235 declarator attributes [opt] constant-initializer [opt]
27236 identifier [opt] attributes [opt] : constant-expression
27238 C++0x Extensions:
27240 member-declaration:
27241 static_assert-declaration */
27243 static void
27244 cp_parser_member_declaration (cp_parser* parser)
27246 cp_decl_specifier_seq decl_specifiers;
27247 tree prefix_attributes;
27248 tree decl;
27249 int declares_class_or_enum;
27250 bool friend_p;
27251 cp_token *token = NULL;
27252 cp_token *decl_spec_token_start = NULL;
27253 cp_token *initializer_token_start = NULL;
27254 int saved_pedantic;
27255 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27257 /* Check for the `__extension__' keyword. */
27258 if (cp_parser_extension_opt (parser, &saved_pedantic))
27260 /* Recurse. */
27261 cp_parser_member_declaration (parser);
27262 /* Restore the old value of the PEDANTIC flag. */
27263 pedantic = saved_pedantic;
27265 return;
27268 /* Check for a template-declaration. */
27269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27271 /* An explicit specialization here is an error condition, and we
27272 expect the specialization handler to detect and report this. */
27273 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27274 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
27275 cp_parser_explicit_specialization (parser);
27276 else
27277 cp_parser_template_declaration (parser, /*member_p=*/true);
27279 return;
27281 /* Check for a template introduction. */
27282 else if (cp_parser_template_declaration_after_export (parser, true))
27283 return;
27285 /* Check for a using-declaration. */
27286 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27288 if (cxx_dialect < cxx11)
27289 /* Parse the using-declaration. */
27290 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
27291 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
27292 cp_parser_using_enum (parser);
27293 else
27295 tree decl;
27296 bool alias_decl_expected;
27297 cp_parser_parse_tentatively (parser);
27298 decl = cp_parser_alias_declaration (parser);
27299 /* Note that if we actually see the '=' token after the
27300 identifier, cp_parser_alias_declaration commits the
27301 tentative parse. In that case, we really expect an
27302 alias-declaration. Otherwise, we expect a using
27303 declaration. */
27304 alias_decl_expected =
27305 !cp_parser_uncommitted_to_tentative_parse_p (parser);
27306 cp_parser_parse_definitely (parser);
27308 if (alias_decl_expected)
27309 finish_member_declaration (decl);
27310 else
27311 cp_parser_using_declaration (parser,
27312 /*access_declaration_p=*/false);
27314 return;
27317 /* Check for @defs. */
27318 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
27320 tree ivar, member;
27321 tree ivar_chains = cp_parser_objc_defs_expression (parser);
27322 ivar = ivar_chains;
27323 while (ivar)
27325 member = ivar;
27326 ivar = TREE_CHAIN (member);
27327 TREE_CHAIN (member) = NULL_TREE;
27328 finish_member_declaration (member);
27330 return;
27333 /* If the next token is `static_assert' we have a static assertion. */
27334 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
27336 cp_parser_static_assert (parser, /*member_p=*/true);
27337 return;
27340 parser->colon_corrects_to_scope_p = false;
27342 cp_omp_declare_simd_data odsd;
27343 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
27344 goto out;
27346 /* Parse the decl-specifier-seq. */
27347 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27348 cp_parser_decl_specifier_seq (parser,
27349 (CP_PARSER_FLAGS_OPTIONAL
27350 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27351 &decl_specifiers,
27352 &declares_class_or_enum);
27354 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
27355 cp_parser_handle_directive_omp_attributes (parser,
27356 &decl_specifiers.attributes,
27357 &odsd, true);
27359 /* Check for an invalid type-name. */
27360 if (!decl_specifiers.any_type_specifiers_p
27361 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27362 goto out;
27363 /* If there is no declarator, then the decl-specifier-seq should
27364 specify a type. */
27365 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27367 /* If there was no decl-specifier-seq, and the next token is a
27368 `;', then we have something like:
27370 struct S { ; };
27372 [class.mem]
27374 Each member-declaration shall declare at least one member
27375 name of the class. */
27376 if (!decl_specifiers.any_specifiers_p)
27378 cp_token *token = cp_lexer_peek_token (parser->lexer);
27379 if (!in_system_header_at (token->location))
27381 gcc_rich_location richloc (token->location);
27382 richloc.add_fixit_remove ();
27383 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27386 else
27388 /* See if this declaration is a friend. */
27389 friend_p = cp_parser_friend_p (&decl_specifiers);
27390 /* If there were decl-specifiers, check to see if there was
27391 a class-declaration. */
27392 tree type = check_tag_decl (&decl_specifiers,
27393 /*explicit_type_instantiation_p=*/false);
27394 /* Nested classes have already been added to the class, but
27395 a `friend' needs to be explicitly registered. */
27396 if (friend_p)
27398 /* If the `friend' keyword was present, the friend must
27399 be introduced with a class-key. */
27400 if (!declares_class_or_enum && cxx_dialect < cxx11)
27401 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27402 "in C++03 a class-key must be used "
27403 "when declaring a friend");
27404 /* In this case:
27406 template <typename T> struct A {
27407 friend struct A<T>::B;
27410 A<T>::B will be represented by a TYPENAME_TYPE, and
27411 therefore not recognized by check_tag_decl. */
27412 if (!type)
27414 type = decl_specifiers.type;
27415 if (type && TREE_CODE (type) == TYPE_DECL)
27416 type = TREE_TYPE (type);
27418 /* Warn if an attribute cannot appear here, as per
27419 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
27420 we ignore attributes in elaborated-type-specifiers. */
27421 if (!declares_class_or_enum
27422 && cxx11_attribute_p (decl_specifiers.attributes))
27424 decl_specifiers.attributes = NULL_TREE;
27425 if (warning_at (decl_spec_token_start->location,
27426 OPT_Wattributes, "attribute ignored"))
27427 inform (decl_spec_token_start->location, "an attribute "
27428 "that appertains to a friend declaration that "
27429 "is not a definition is ignored");
27431 if (!type || !TYPE_P (type))
27432 error_at (decl_spec_token_start->location,
27433 "friend declaration does not name a class or "
27434 "function");
27435 else
27436 make_friend_class (current_class_type, type,
27437 /*complain=*/true);
27439 /* If there is no TYPE, an error message will already have
27440 been issued. */
27441 else if (!type || type == error_mark_node)
27443 /* An anonymous aggregate has to be handled specially; such
27444 a declaration really declares a data member (with a
27445 particular type), as opposed to a nested class. */
27446 else if (ANON_AGGR_TYPE_P (type))
27448 /* C++11 9.5/6. */
27449 if (decl_specifiers.storage_class != sc_none)
27450 error_at (decl_spec_token_start->location,
27451 "a storage class on an anonymous aggregate "
27452 "in class scope is not allowed");
27454 /* Remove constructors and such from TYPE, now that we
27455 know it is an anonymous aggregate. */
27456 fixup_anonymous_aggr (type);
27457 /* And make the corresponding data member. */
27458 decl = build_decl (decl_spec_token_start->location,
27459 FIELD_DECL, NULL_TREE, type);
27460 /* Add it to the class. */
27461 finish_member_declaration (decl);
27463 else
27464 cp_parser_check_access_in_redeclaration
27465 (TYPE_NAME (type),
27466 decl_spec_token_start->location);
27469 else
27471 bool assume_semicolon = false;
27473 /* Clear attributes from the decl_specifiers but keep them
27474 around as prefix attributes that apply them to the entity
27475 being declared. */
27476 prefix_attributes = decl_specifiers.attributes;
27477 decl_specifiers.attributes = NULL_TREE;
27478 if (parser->omp_declare_simd
27479 && (parser->omp_declare_simd->attribs[0]
27480 == &decl_specifiers.attributes))
27481 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27483 /* See if these declarations will be friends. */
27484 friend_p = cp_parser_friend_p (&decl_specifiers);
27486 /* Keep going until we hit the `;' at the end of the
27487 declaration. */
27488 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27490 tree attributes = NULL_TREE;
27491 tree first_attribute;
27492 tree initializer;
27493 bool named_bitfld = false;
27495 /* Peek at the next token. */
27496 token = cp_lexer_peek_token (parser->lexer);
27498 /* The following code wants to know early if it is a bit-field
27499 or some other declaration. Attributes can appear before
27500 the `:' token. Skip over them without consuming any tokens
27501 to peek if they are followed by `:'. */
27502 if (cp_next_tokens_can_be_attribute_p (parser)
27503 || (token->type == CPP_NAME
27504 && cp_nth_tokens_can_be_attribute_p (parser, 2)
27505 && (named_bitfld = true)))
27507 size_t n
27508 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27509 token = cp_lexer_peek_nth_token (parser->lexer, n);
27512 /* Check for a bitfield declaration. */
27513 if (token->type == CPP_COLON
27514 || (token->type == CPP_NAME
27515 && token == cp_lexer_peek_token (parser->lexer)
27516 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27517 && (named_bitfld = true)))
27519 tree identifier;
27520 tree width;
27521 tree late_attributes = NULL_TREE;
27522 location_t id_location
27523 = cp_lexer_peek_token (parser->lexer)->location;
27525 if (named_bitfld)
27526 identifier = cp_parser_identifier (parser);
27527 else
27528 identifier = NULL_TREE;
27530 /* Look for attributes that apply to the bitfield. */
27531 attributes = cp_parser_attributes_opt (parser);
27533 /* Consume the `:' token. */
27534 cp_lexer_consume_token (parser->lexer);
27536 /* Get the width of the bitfield. */
27537 width = cp_parser_constant_expression (parser, false, NULL,
27538 cxx_dialect >= cxx11);
27540 /* In C++20 and as extension for C++11 and above we allow
27541 default member initializers for bit-fields. */
27542 initializer = NULL_TREE;
27543 if (cxx_dialect >= cxx11
27544 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27545 || cp_lexer_next_token_is (parser->lexer,
27546 CPP_OPEN_BRACE)))
27548 location_t loc
27549 = cp_lexer_peek_token (parser->lexer)->location;
27550 if (cxx_dialect < cxx20
27551 && identifier != NULL_TREE)
27552 pedwarn (loc, OPT_Wc__20_extensions,
27553 "default member initializers for bit-fields "
27554 "only available with %<-std=c++20%> or "
27555 "%<-std=gnu++20%>");
27557 initializer = cp_parser_save_nsdmi (parser);
27558 if (identifier == NULL_TREE)
27560 error_at (loc, "default member initializer for "
27561 "unnamed bit-field");
27562 initializer = NULL_TREE;
27565 else
27567 /* Look for attributes that apply to the bitfield after
27568 the `:' token and width. This is where GCC used to
27569 parse attributes in the past, pedwarn if there is
27570 a std attribute. */
27571 if (cp_next_tokens_can_be_std_attribute_p (parser))
27572 pedwarn (input_location, OPT_Wpedantic,
27573 "ISO C++ allows bit-field attributes only "
27574 "before the %<:%> token");
27576 late_attributes = cp_parser_attributes_opt (parser);
27579 attributes = attr_chainon (attributes, late_attributes);
27581 /* Remember which attributes are prefix attributes and
27582 which are not. */
27583 first_attribute = attributes;
27584 /* Combine the attributes. */
27585 attributes = attr_chainon (prefix_attributes, attributes);
27587 /* Create the bitfield declaration. */
27588 decl = grokbitfield (identifier
27589 ? make_id_declarator (NULL_TREE,
27590 identifier,
27591 sfk_none,
27592 id_location)
27593 : NULL,
27594 &decl_specifiers,
27595 width, initializer,
27596 attributes);
27598 else
27600 cp_declarator *declarator;
27601 tree asm_specification;
27602 int ctor_dtor_or_conv_p;
27603 bool static_p = (decl_specifiers.storage_class == sc_static);
27604 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27605 /* We can't delay parsing for friends,
27606 alias-declarations, and typedefs, even though the
27607 standard seems to require it. */
27608 if (!friend_p
27609 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27610 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27612 /* Parse the declarator. */
27613 declarator
27614 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27615 flags,
27616 &ctor_dtor_or_conv_p,
27617 /*parenthesized_p=*/NULL,
27618 /*member_p=*/true,
27619 friend_p, static_p);
27621 /* If something went wrong parsing the declarator, make sure
27622 that we at least consume some tokens. */
27623 if (declarator == cp_error_declarator)
27625 /* Skip to the end of the statement. */
27626 cp_parser_skip_to_end_of_statement (parser);
27627 /* If the next token is not a semicolon, that is
27628 probably because we just skipped over the body of
27629 a function. So, we consume a semicolon if
27630 present, but do not issue an error message if it
27631 is not present. */
27632 if (cp_lexer_next_token_is (parser->lexer,
27633 CPP_SEMICOLON))
27634 cp_lexer_consume_token (parser->lexer);
27635 goto out;
27638 /* Handle class-scope non-template C++17 deduction guides. */
27639 cp_parser_maybe_adjust_declarator_for_dguide (parser,
27640 &decl_specifiers,
27641 declarator,
27642 &ctor_dtor_or_conv_p);
27644 if (declares_class_or_enum & 2)
27645 cp_parser_check_for_definition_in_return_type
27646 (declarator, decl_specifiers.type,
27647 decl_specifiers.locations[ds_type_spec]);
27649 /* Look for an asm-specification. */
27650 asm_specification = cp_parser_asm_specification_opt (parser);
27651 /* Look for attributes that apply to the declaration. */
27652 attributes = cp_parser_attributes_opt (parser);
27653 /* Remember which attributes are prefix attributes and
27654 which are not. */
27655 first_attribute = attributes;
27656 /* Combine the attributes. */
27657 attributes = attr_chainon (prefix_attributes, attributes);
27659 /* If it's an `=', then we have a constant-initializer or a
27660 pure-specifier. It is not correct to parse the
27661 initializer before registering the member declaration
27662 since the member declaration should be in scope while
27663 its initializer is processed. However, the rest of the
27664 front end does not yet provide an interface that allows
27665 us to handle this correctly. */
27666 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27668 /* In [class.mem]:
27670 A pure-specifier shall be used only in the declaration of
27671 a virtual function.
27673 A member-declarator can contain a constant-initializer
27674 only if it declares a static member of integral or
27675 enumeration type.
27677 Therefore, if the DECLARATOR is for a function, we look
27678 for a pure-specifier; otherwise, we look for a
27679 constant-initializer. When we call `grokfield', it will
27680 perform more stringent semantics checks. */
27681 initializer_token_start = cp_lexer_peek_token (parser->lexer);
27682 declarator->init_loc = initializer_token_start->location;
27683 if (function_declarator_p (declarator)
27684 || (decl_specifiers.type
27685 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27686 && declarator->kind == cdk_id
27687 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27688 == FUNCTION_TYPE)))
27689 initializer = cp_parser_pure_specifier (parser);
27690 else if (decl_specifiers.storage_class != sc_static)
27691 initializer = cp_parser_save_nsdmi (parser);
27692 else if (cxx_dialect >= cxx11)
27694 bool nonconst;
27695 /* Don't require a constant rvalue in C++11, since we
27696 might want a reference constant. We'll enforce
27697 constancy later. */
27698 cp_lexer_consume_token (parser->lexer);
27699 /* Parse the initializer. */
27700 initializer = cp_parser_initializer_clause (parser,
27701 &nonconst);
27703 else
27704 /* Parse the initializer. */
27705 initializer = cp_parser_constant_initializer (parser);
27707 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27708 && !function_declarator_p (declarator))
27710 bool x;
27711 declarator->init_loc
27712 = cp_lexer_peek_token (parser->lexer)->location;
27713 if (decl_specifiers.storage_class != sc_static)
27714 initializer = cp_parser_save_nsdmi (parser);
27715 else
27716 initializer = cp_parser_initializer (parser, &x, &x);
27718 /* Detect invalid bit-field cases such as
27720 int *p : 4;
27721 int &&r : 3;
27723 and similar. */
27724 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27725 /* If there were no type specifiers, it was a
27726 constructor. */
27727 && decl_specifiers.any_type_specifiers_p)
27729 /* This is called for a decent diagnostic only. */
27730 tree d = grokdeclarator (declarator, &decl_specifiers,
27731 BITFIELD, /*initialized=*/false,
27732 &attributes);
27733 if (!error_operand_p (d))
27734 error_at (DECL_SOURCE_LOCATION (d),
27735 "bit-field %qD has non-integral type %qT",
27736 d, TREE_TYPE (d));
27737 cp_parser_skip_to_end_of_statement (parser);
27738 /* Avoid "extra ;" pedwarns. */
27739 if (cp_lexer_next_token_is (parser->lexer,
27740 CPP_SEMICOLON))
27741 cp_lexer_consume_token (parser->lexer);
27742 goto out;
27744 /* Otherwise, there is no initializer. */
27745 else
27746 initializer = NULL_TREE;
27748 /* See if we are probably looking at a function
27749 definition. We are certainly not looking at a
27750 member-declarator. Calling `grokfield' has
27751 side-effects, so we must not do it unless we are sure
27752 that we are looking at a member-declarator. */
27753 if (cp_parser_token_starts_function_definition_p
27754 (cp_lexer_peek_token (parser->lexer)))
27756 /* The grammar does not allow a pure-specifier to be
27757 used when a member function is defined. (It is
27758 possible that this fact is an oversight in the
27759 standard, since a pure function may be defined
27760 outside of the class-specifier. */
27761 if (initializer && initializer_token_start)
27762 error_at (initializer_token_start->location,
27763 "pure-specifier on function-definition");
27764 decl = cp_parser_save_member_function_body (parser,
27765 &decl_specifiers,
27766 declarator,
27767 attributes);
27769 if (parser->fully_implicit_function_template_p)
27770 decl = finish_fully_implicit_template (parser, decl);
27771 /* If the member was not a friend, declare it here. */
27772 if (!friend_p)
27773 finish_member_declaration (decl);
27774 /* Peek at the next token. */
27775 token = cp_lexer_peek_token (parser->lexer);
27776 /* If the next token is a semicolon, consume it. */
27777 if (token->type == CPP_SEMICOLON)
27779 location_t semicolon_loc
27780 = cp_lexer_consume_token (parser->lexer)->location;
27781 gcc_rich_location richloc (semicolon_loc);
27782 richloc.add_fixit_remove ();
27783 warning_at (&richloc, OPT_Wextra_semi,
27784 "extra %<;%> after in-class "
27785 "function definition");
27787 goto out;
27789 else
27790 if (declarator->kind == cdk_function)
27791 declarator->id_loc = token->location;
27793 /* Create the declaration. */
27794 decl = grokfield (declarator, &decl_specifiers,
27795 initializer, /*init_const_expr_p=*/true,
27796 asm_specification, attributes);
27798 if (parser->fully_implicit_function_template_p)
27800 if (friend_p)
27801 finish_fully_implicit_template (parser, 0);
27802 else
27803 decl = finish_fully_implicit_template (parser, decl);
27807 cp_finalize_omp_declare_simd (parser, decl);
27808 cp_finalize_oacc_routine (parser, decl, false);
27810 /* Reset PREFIX_ATTRIBUTES. */
27811 if (attributes != error_mark_node)
27813 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27814 attributes = TREE_CHAIN (attributes);
27815 if (attributes)
27816 TREE_CHAIN (attributes) = NULL_TREE;
27819 /* If there is any qualification still in effect, clear it
27820 now; we will be starting fresh with the next declarator. */
27821 parser->scope = NULL_TREE;
27822 parser->qualifying_scope = NULL_TREE;
27823 parser->object_scope = NULL_TREE;
27824 /* If it's a `,', then there are more declarators. */
27825 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27827 cp_lexer_consume_token (parser->lexer);
27828 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27830 cp_token *token = cp_lexer_previous_token (parser->lexer);
27831 gcc_rich_location richloc (token->location);
27832 richloc.add_fixit_remove ();
27833 error_at (&richloc, "stray %<,%> at end of "
27834 "member declaration");
27837 /* If the next token isn't a `;', then we have a parse error. */
27838 else if (cp_lexer_next_token_is_not (parser->lexer,
27839 CPP_SEMICOLON))
27841 /* The next token might be a ways away from where the
27842 actual semicolon is missing. Find the previous token
27843 and use that for our error position. */
27844 cp_token *token = cp_lexer_previous_token (parser->lexer);
27845 gcc_rich_location richloc (token->location);
27846 richloc.add_fixit_insert_after (";");
27847 error_at (&richloc, "expected %<;%> at end of "
27848 "member declaration");
27850 /* Assume that the user meant to provide a semicolon. If
27851 we were to cp_parser_skip_to_end_of_statement, we might
27852 skip to a semicolon inside a member function definition
27853 and issue nonsensical error messages. */
27854 assume_semicolon = true;
27857 if (decl)
27859 /* Add DECL to the list of members. */
27860 if (!friend_p
27861 /* Explicitly include, eg, NSDMIs, for better error
27862 recovery (c++/58650). */
27863 || !DECL_DECLARES_FUNCTION_P (decl))
27864 finish_member_declaration (decl);
27866 if (DECL_DECLARES_FUNCTION_P (decl))
27867 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
27868 else if (TREE_CODE (decl) == FIELD_DECL
27869 && DECL_INITIAL (decl))
27870 /* Add DECL to the queue of NSDMI to be parsed later. */
27871 vec_safe_push (unparsed_nsdmis, decl);
27874 if (assume_semicolon)
27875 goto out;
27879 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27880 out:
27881 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27882 cp_finalize_omp_declare_simd (parser, &odsd);
27885 /* Parse a pure-specifier.
27887 pure-specifier:
27890 Returns INTEGER_ZERO_NODE if a pure specifier is found.
27891 Otherwise, ERROR_MARK_NODE is returned. */
27893 static tree
27894 cp_parser_pure_specifier (cp_parser* parser)
27896 cp_token *token;
27898 /* Look for the `=' token. */
27899 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27900 return error_mark_node;
27901 /* Look for the `0' token. */
27902 token = cp_lexer_peek_token (parser->lexer);
27904 if (token->type == CPP_EOF
27905 || token->type == CPP_PRAGMA_EOL)
27906 return error_mark_node;
27908 cp_lexer_consume_token (parser->lexer);
27910 /* Accept = default or = delete in c++0x mode. */
27911 if (token->keyword == RID_DEFAULT
27912 || token->keyword == RID_DELETE)
27914 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
27915 return token->u.value;
27918 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
27919 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
27921 cp_parser_error (parser,
27922 "invalid pure specifier (only %<= 0%> is allowed)");
27923 cp_parser_skip_to_end_of_statement (parser);
27924 return error_mark_node;
27926 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27928 error_at (token->location, "templates may not be %<virtual%>");
27929 return error_mark_node;
27932 return integer_zero_node;
27935 /* Parse a constant-initializer.
27937 constant-initializer:
27938 = constant-expression
27940 Returns a representation of the constant-expression. */
27942 static tree
27943 cp_parser_constant_initializer (cp_parser* parser)
27945 /* Look for the `=' token. */
27946 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27947 return error_mark_node;
27949 /* It is invalid to write:
27951 struct S { static const int i = { 7 }; };
27954 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27956 cp_parser_error (parser,
27957 "a brace-enclosed initializer is not allowed here");
27958 /* Consume the opening brace. */
27959 matching_braces braces;
27960 braces.consume_open (parser);
27961 /* Skip the initializer. */
27962 cp_parser_skip_to_closing_brace (parser);
27963 /* Look for the trailing `}'. */
27964 braces.require_close (parser);
27966 return error_mark_node;
27969 return cp_parser_constant_expression (parser);
27972 /* Derived classes [gram.class.derived] */
27974 /* Parse a base-clause.
27976 base-clause:
27977 : base-specifier-list
27979 base-specifier-list:
27980 base-specifier ... [opt]
27981 base-specifier-list , base-specifier ... [opt]
27983 Returns a TREE_LIST representing the base-classes, in the order in
27984 which they were declared. The representation of each node is as
27985 described by cp_parser_base_specifier.
27987 In the case that no bases are specified, this function will return
27988 NULL_TREE, not ERROR_MARK_NODE. */
27990 static tree
27991 cp_parser_base_clause (cp_parser* parser)
27993 tree bases = NULL_TREE;
27995 /* Look for the `:' that begins the list. */
27996 cp_parser_require (parser, CPP_COLON, RT_COLON);
27998 /* Scan the base-specifier-list. */
27999 while (true)
28001 cp_token *token;
28002 tree base;
28003 bool pack_expansion_p = false;
28005 /* Look for the base-specifier. */
28006 base = cp_parser_base_specifier (parser);
28007 /* Look for the (optional) ellipsis. */
28008 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28010 /* Consume the `...'. */
28011 cp_lexer_consume_token (parser->lexer);
28013 pack_expansion_p = true;
28016 /* Add BASE to the front of the list. */
28017 if (base && base != error_mark_node)
28019 if (pack_expansion_p)
28020 /* Make this a pack expansion type. */
28021 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
28023 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
28025 TREE_CHAIN (base) = bases;
28026 bases = base;
28029 /* Peek at the next token. */
28030 token = cp_lexer_peek_token (parser->lexer);
28031 /* If it's not a comma, then the list is complete. */
28032 if (token->type != CPP_COMMA)
28033 break;
28034 /* Consume the `,'. */
28035 cp_lexer_consume_token (parser->lexer);
28038 /* PARSER->SCOPE may still be non-NULL at this point, if the last
28039 base class had a qualified name. However, the next name that
28040 appears is certainly not qualified. */
28041 parser->scope = NULL_TREE;
28042 parser->qualifying_scope = NULL_TREE;
28043 parser->object_scope = NULL_TREE;
28045 return nreverse (bases);
28048 /* Parse a base-specifier.
28050 base-specifier:
28051 :: [opt] nested-name-specifier [opt] class-name
28052 virtual access-specifier [opt] :: [opt] nested-name-specifier
28053 [opt] class-name
28054 access-specifier virtual [opt] :: [opt] nested-name-specifier
28055 [opt] class-name
28057 Returns a TREE_LIST. The TREE_PURPOSE will be one of
28058 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
28059 indicate the specifiers provided. The TREE_VALUE will be a TYPE
28060 (or the ERROR_MARK_NODE) indicating the type that was specified. */
28062 static tree
28063 cp_parser_base_specifier (cp_parser* parser)
28065 cp_token *token;
28066 bool done = false;
28067 bool virtual_p = false;
28068 bool duplicate_virtual_error_issued_p = false;
28069 bool duplicate_access_error_issued_p = false;
28070 bool class_scope_p, template_p;
28071 tree access = access_default_node;
28072 tree type;
28074 /* Process the optional `virtual' and `access-specifier'. */
28075 while (!done)
28077 /* Peek at the next token. */
28078 token = cp_lexer_peek_token (parser->lexer);
28079 /* Process `virtual'. */
28080 switch (token->keyword)
28082 case RID_VIRTUAL:
28083 /* If `virtual' appears more than once, issue an error. */
28084 if (virtual_p && !duplicate_virtual_error_issued_p)
28086 cp_parser_error (parser,
28087 "%<virtual%> specified more than once in base-specifier");
28088 duplicate_virtual_error_issued_p = true;
28091 virtual_p = true;
28093 /* Consume the `virtual' token. */
28094 cp_lexer_consume_token (parser->lexer);
28096 break;
28098 case RID_PUBLIC:
28099 case RID_PROTECTED:
28100 case RID_PRIVATE:
28101 /* If more than one access specifier appears, issue an
28102 error. */
28103 if (access != access_default_node
28104 && !duplicate_access_error_issued_p)
28106 cp_parser_error (parser,
28107 "more than one access specifier in base-specifier");
28108 duplicate_access_error_issued_p = true;
28111 access = ridpointers[(int) token->keyword];
28113 /* Consume the access-specifier. */
28114 cp_lexer_consume_token (parser->lexer);
28116 break;
28118 default:
28119 done = true;
28120 break;
28123 /* It is not uncommon to see programs mechanically, erroneously, use
28124 the 'typename' keyword to denote (dependent) qualified types
28125 as base classes. */
28126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28128 token = cp_lexer_peek_token (parser->lexer);
28129 if (!processing_template_decl)
28130 error_at (token->location,
28131 "keyword %<typename%> not allowed outside of templates");
28132 else
28133 error_at (token->location,
28134 "keyword %<typename%> not allowed in this context "
28135 "(the base class is implicitly a type)");
28136 cp_lexer_consume_token (parser->lexer);
28139 /* Look for the optional `::' operator. */
28140 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28141 /* Look for the nested-name-specifier. The simplest way to
28142 implement:
28144 [temp.res]
28146 The keyword `typename' is not permitted in a base-specifier or
28147 mem-initializer; in these contexts a qualified name that
28148 depends on a template-parameter is implicitly assumed to be a
28149 type name.
28151 is to pretend that we have seen the `typename' keyword at this
28152 point. */
28153 cp_parser_nested_name_specifier_opt (parser,
28154 /*typename_keyword_p=*/true,
28155 /*check_dependency_p=*/true,
28156 /*type_p=*/true,
28157 /*is_declaration=*/true);
28158 /* If the base class is given by a qualified name, assume that names
28159 we see are type names or templates, as appropriate. */
28160 class_scope_p = (parser->scope && TYPE_P (parser->scope));
28161 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
28163 if (!parser->scope
28164 && cp_lexer_next_token_is_decltype (parser->lexer))
28165 /* DR 950 allows decltype as a base-specifier. */
28166 type = cp_parser_decltype (parser);
28167 else
28169 /* Otherwise, look for the class-name. */
28170 type = cp_parser_class_name (parser,
28171 class_scope_p,
28172 template_p,
28173 typename_type,
28174 /*check_dependency_p=*/true,
28175 /*class_head_p=*/false,
28176 /*is_declaration=*/true);
28177 type = TREE_TYPE (type);
28180 if (type == error_mark_node)
28181 return error_mark_node;
28183 return finish_base_specifier (type, access, virtual_p);
28186 /* Exception handling [gram.exception] */
28188 /* Save the tokens that make up the noexcept-specifier for a member-function.
28189 Returns a DEFERRED_PARSE. */
28191 static tree
28192 cp_parser_save_noexcept (cp_parser *parser)
28194 cp_token *first = parser->lexer->next_token;
28195 /* We want everything up to, including, the final ')'. */
28196 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
28197 cp_token *last = parser->lexer->next_token;
28199 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
28200 to carry the information we will need. */
28201 tree expr = make_node (DEFERRED_PARSE);
28202 /* Save away the noexcept-specifier; we will process it when the
28203 class is complete. */
28204 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
28205 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
28206 expr = build_tree_list (expr, NULL_TREE);
28207 return expr;
28210 /* Used for late processing of noexcept-specifiers of member-functions.
28211 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
28212 we saved for later; parse it now. DECL is the declaration of the
28213 member function. */
28215 static tree
28216 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
28218 /* Make sure we've gotten something that hasn't been parsed yet. */
28219 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
28221 push_unparsed_function_queues (parser);
28223 /* Push the saved tokens for the noexcept-specifier onto the parser's
28224 lexer stack. */
28225 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
28226 cp_parser_push_lexer_for_tokens (parser, tokens);
28228 /* Parse the cached noexcept-specifier. */
28229 tree parsed_arg
28230 = cp_parser_noexcept_specification_opt (parser,
28231 CP_PARSER_FLAGS_NONE,
28232 /*require_constexpr=*/true,
28233 /*consumed_expr=*/NULL,
28234 /*return_cond=*/false);
28236 /* Revert to the main lexer. */
28237 cp_parser_pop_lexer (parser);
28239 /* Restore the queue. */
28240 pop_unparsed_function_queues (parser);
28242 /* And we're done. */
28243 return parsed_arg;
28246 /* Perform late checking of overriding function with respect to their
28247 noexcept-specifiers. FNDECL is the member function that potentially
28248 overrides some virtual function with the same signature. */
28250 static void
28251 noexcept_override_late_checks (tree fndecl)
28253 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
28254 tree base_binfo;
28256 if (DECL_STATIC_FUNCTION_P (fndecl))
28257 return;
28259 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
28261 tree basetype = BINFO_TYPE (base_binfo);
28263 if (!TYPE_POLYMORPHIC_P (basetype))
28264 continue;
28266 tree fn = look_for_overrides_here (basetype, fndecl);
28267 if (fn)
28268 maybe_check_overriding_exception_spec (fndecl, fn);
28272 /* Parse an (optional) noexcept-specification.
28274 noexcept-specification:
28275 noexcept ( constant-expression ) [opt]
28277 If no noexcept-specification is present, returns NULL_TREE.
28278 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28279 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28280 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28281 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28282 in which case a boolean condition is returned instead. The parser flags
28283 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28284 the (member) function is `const'. */
28286 static tree
28287 cp_parser_noexcept_specification_opt (cp_parser* parser,
28288 cp_parser_flags flags,
28289 bool require_constexpr,
28290 bool* consumed_expr,
28291 bool return_cond)
28293 cp_token *token;
28294 const char *saved_message;
28296 /* Peek at the next token. */
28297 token = cp_lexer_peek_token (parser->lexer);
28299 /* Is it a noexcept-specification? */
28300 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
28302 tree expr;
28304 /* [class.mem]/6 says that a noexcept-specifer (within the
28305 member-specification of the class) is a complete-class context of
28306 a class. So, if the noexcept-specifier has the optional expression,
28307 just save the tokens, and reparse this after we're done with the
28308 class. */
28310 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
28311 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
28312 /* No need to delay parsing for a number literal or true/false. */
28313 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
28314 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28315 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
28316 && at_class_scope_p ()
28317 && TYPE_BEING_DEFINED (current_class_type)
28318 && !LAMBDA_TYPE_P (current_class_type))
28319 return cp_parser_save_noexcept (parser);
28321 cp_lexer_consume_token (parser->lexer);
28323 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28325 matching_parens parens;
28326 parens.consume_open (parser);
28328 if (require_constexpr)
28330 /* Types may not be defined in an exception-specification. */
28331 saved_message = parser->type_definition_forbidden_message;
28332 parser->type_definition_forbidden_message
28333 = G_("types may not be defined in an exception-specification");
28335 bool non_constant_p;
28336 expr
28337 = cp_parser_constant_expression (parser,
28338 /*allow_non_constant=*/true,
28339 &non_constant_p);
28340 if (non_constant_p
28341 && !require_potential_rvalue_constant_expression (expr))
28343 expr = NULL_TREE;
28344 return_cond = true;
28347 /* Restore the saved message. */
28348 parser->type_definition_forbidden_message = saved_message;
28350 else
28352 expr = cp_parser_expression (parser);
28353 *consumed_expr = true;
28356 parens.require_close (parser);
28358 else
28360 expr = boolean_true_node;
28361 if (!require_constexpr)
28362 *consumed_expr = false;
28365 /* We cannot build a noexcept-spec right away because this will check
28366 that expr is a constexpr. */
28367 if (!return_cond)
28368 return build_noexcept_spec (expr, tf_warning_or_error);
28369 else
28370 return expr;
28372 else
28373 return NULL_TREE;
28376 /* Parse an (optional) exception-specification.
28378 exception-specification:
28379 throw ( type-id-list [opt] )
28381 Returns a TREE_LIST representing the exception-specification. The
28382 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
28383 control parsing. QUALS are qualifiers indicating whether the (member)
28384 function is `const'. */
28386 static tree
28387 cp_parser_exception_specification_opt (cp_parser* parser,
28388 cp_parser_flags flags)
28390 cp_token *token;
28391 tree type_id_list;
28392 const char *saved_message;
28394 /* Peek at the next token. */
28395 token = cp_lexer_peek_token (parser->lexer);
28397 /* Is it a noexcept-specification? */
28398 type_id_list
28399 = cp_parser_noexcept_specification_opt (parser, flags,
28400 /*require_constexpr=*/true,
28401 /*consumed_expr=*/NULL,
28402 /*return_cond=*/false);
28403 if (type_id_list != NULL_TREE)
28404 return type_id_list;
28406 /* If it's not `throw', then there's no exception-specification. */
28407 if (!cp_parser_is_keyword (token, RID_THROW))
28408 return NULL_TREE;
28410 location_t loc = token->location;
28412 /* Consume the `throw'. */
28413 cp_lexer_consume_token (parser->lexer);
28415 /* Look for the `('. */
28416 matching_parens parens;
28417 parens.require_open (parser);
28419 /* Peek at the next token. */
28420 token = cp_lexer_peek_token (parser->lexer);
28421 /* If it's not a `)', then there is a type-id-list. */
28422 if (token->type != CPP_CLOSE_PAREN)
28424 /* Types may not be defined in an exception-specification. */
28425 saved_message = parser->type_definition_forbidden_message;
28426 parser->type_definition_forbidden_message
28427 = G_("types may not be defined in an exception-specification");
28428 /* Parse the type-id-list. */
28429 type_id_list = cp_parser_type_id_list (parser);
28430 /* Restore the saved message. */
28431 parser->type_definition_forbidden_message = saved_message;
28433 if (cxx_dialect >= cxx17)
28435 error_at (loc, "ISO C++17 does not allow dynamic exception "
28436 "specifications");
28437 type_id_list = NULL_TREE;
28439 else if (cxx_dialect >= cxx11)
28440 warning_at (loc, OPT_Wdeprecated,
28441 "dynamic exception specifications are deprecated in "
28442 "C++11");
28444 /* In C++17, throw() is equivalent to noexcept (true). throw()
28445 is deprecated in C++11 and above as well, but is still widely used,
28446 so don't warn about it yet. */
28447 else if (cxx_dialect >= cxx17)
28448 type_id_list = noexcept_true_spec;
28449 else
28450 type_id_list = empty_except_spec;
28452 /* Look for the `)'. */
28453 parens.require_close (parser);
28455 return type_id_list;
28458 /* Parse an (optional) type-id-list.
28460 type-id-list:
28461 type-id ... [opt]
28462 type-id-list , type-id ... [opt]
28464 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28465 in the order that the types were presented. */
28467 static tree
28468 cp_parser_type_id_list (cp_parser* parser)
28470 tree types = NULL_TREE;
28472 while (true)
28474 cp_token *token;
28475 tree type;
28477 token = cp_lexer_peek_token (parser->lexer);
28479 /* Get the next type-id. */
28480 type = cp_parser_type_id (parser);
28481 /* Check for invalid 'auto'. */
28482 if (flag_concepts && type_uses_auto (type))
28484 error_at (token->location,
28485 "invalid use of %<auto%> in exception-specification");
28486 type = error_mark_node;
28488 /* Parse the optional ellipsis. */
28489 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28491 /* Consume the `...'. */
28492 cp_lexer_consume_token (parser->lexer);
28494 /* Turn the type into a pack expansion expression. */
28495 type = make_pack_expansion (type);
28497 /* Add it to the list. */
28498 types = add_exception_specifier (types, type, /*complain=*/1);
28499 /* Peek at the next token. */
28500 token = cp_lexer_peek_token (parser->lexer);
28501 /* If it is not a `,', we are done. */
28502 if (token->type != CPP_COMMA)
28503 break;
28504 /* Consume the `,'. */
28505 cp_lexer_consume_token (parser->lexer);
28508 return nreverse (types);
28511 /* Parse a try-block.
28513 try-block:
28514 try compound-statement handler-seq */
28516 static tree
28517 cp_parser_try_block (cp_parser* parser)
28519 tree try_block;
28521 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28522 if (parser->in_function_body
28523 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28524 && cxx_dialect < cxx20)
28525 pedwarn (input_location, OPT_Wc__20_extensions,
28526 "%<try%> in %<constexpr%> function only "
28527 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28529 try_block = begin_try_block ();
28530 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28531 finish_try_block (try_block);
28532 cp_parser_handler_seq (parser);
28533 finish_handler_sequence (try_block);
28535 return try_block;
28538 /* Parse a function-try-block.
28540 function-try-block:
28541 try ctor-initializer [opt] function-body handler-seq */
28543 static void
28544 cp_parser_function_try_block (cp_parser* parser)
28546 tree compound_stmt;
28547 tree try_block;
28549 /* Look for the `try' keyword. */
28550 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28551 return;
28552 /* Let the rest of the front end know where we are. */
28553 try_block = begin_function_try_block (&compound_stmt);
28554 /* Parse the function-body. */
28555 cp_parser_ctor_initializer_opt_and_function_body
28556 (parser, /*in_function_try_block=*/true);
28557 /* We're done with the `try' part. */
28558 finish_function_try_block (try_block);
28559 /* Parse the handlers. */
28560 cp_parser_handler_seq (parser);
28561 /* We're done with the handlers. */
28562 finish_function_handler_sequence (try_block, compound_stmt);
28565 /* Parse a handler-seq.
28567 handler-seq:
28568 handler handler-seq [opt] */
28570 static void
28571 cp_parser_handler_seq (cp_parser* parser)
28573 while (true)
28575 cp_token *token;
28577 /* Parse the handler. */
28578 cp_parser_handler (parser);
28579 /* Peek at the next token. */
28580 token = cp_lexer_peek_token (parser->lexer);
28581 /* If it's not `catch' then there are no more handlers. */
28582 if (!cp_parser_is_keyword (token, RID_CATCH))
28583 break;
28587 /* Parse a handler.
28589 handler:
28590 catch ( exception-declaration ) compound-statement */
28592 static void
28593 cp_parser_handler (cp_parser* parser)
28595 tree handler;
28596 tree declaration;
28598 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28599 handler = begin_handler ();
28600 matching_parens parens;
28601 parens.require_open (parser);
28602 declaration = cp_parser_exception_declaration (parser);
28603 finish_handler_parms (declaration, handler);
28604 parens.require_close (parser);
28605 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28606 finish_handler (handler);
28609 /* Parse an exception-declaration.
28611 exception-declaration:
28612 type-specifier-seq declarator
28613 type-specifier-seq abstract-declarator
28614 type-specifier-seq
28617 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28618 ellipsis variant is used. */
28620 static tree
28621 cp_parser_exception_declaration (cp_parser* parser)
28623 cp_decl_specifier_seq type_specifiers;
28624 cp_declarator *declarator;
28625 const char *saved_message;
28627 /* If it's an ellipsis, it's easy to handle. */
28628 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28630 /* Consume the `...' token. */
28631 cp_lexer_consume_token (parser->lexer);
28632 return NULL_TREE;
28635 /* Types may not be defined in exception-declarations. */
28636 saved_message = parser->type_definition_forbidden_message;
28637 parser->type_definition_forbidden_message
28638 = G_("types may not be defined in exception-declarations");
28640 /* Parse the type-specifier-seq. */
28641 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28642 /*is_declaration=*/true,
28643 /*is_trailing_return=*/false,
28644 &type_specifiers);
28645 /* If it's a `)', then there is no declarator. */
28646 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28647 declarator = NULL;
28648 else
28649 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28650 CP_PARSER_FLAGS_NONE,
28651 /*ctor_dtor_or_conv_p=*/NULL,
28652 /*parenthesized_p=*/NULL,
28653 /*member_p=*/false,
28654 /*friend_p=*/false,
28655 /*static_p=*/false);
28657 /* Restore the saved message. */
28658 parser->type_definition_forbidden_message = saved_message;
28660 if (!type_specifiers.any_specifiers_p)
28661 return error_mark_node;
28663 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28666 /* Parse a throw-expression.
28668 throw-expression:
28669 throw assignment-expression [opt]
28671 Returns a THROW_EXPR representing the throw-expression. */
28673 static tree
28674 cp_parser_throw_expression (cp_parser* parser)
28676 tree expression;
28677 cp_token* token;
28678 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28680 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28681 token = cp_lexer_peek_token (parser->lexer);
28682 /* Figure out whether or not there is an assignment-expression
28683 following the "throw" keyword. */
28684 if (token->type == CPP_COMMA
28685 || token->type == CPP_SEMICOLON
28686 || token->type == CPP_CLOSE_PAREN
28687 || token->type == CPP_CLOSE_SQUARE
28688 || token->type == CPP_CLOSE_BRACE
28689 || token->type == CPP_COLON)
28690 expression = NULL_TREE;
28691 else
28692 expression = cp_parser_assignment_expression (parser);
28694 /* Construct a location e.g.:
28695 throw x
28696 ^~~~~~~
28697 with caret == start at the start of the "throw" token, and
28698 the end at the end of the final token we consumed. */
28699 location_t combined_loc = make_location (start_loc, start_loc,
28700 parser->lexer);
28701 expression = build_throw (combined_loc, expression);
28703 return expression;
28706 /* Parse a yield-expression.
28708 yield-expression:
28709 co_yield assignment-expression
28710 co_yield braced-init-list
28712 Returns a CO_YIELD_EXPR representing the yield-expression. */
28714 static tree
28715 cp_parser_yield_expression (cp_parser* parser)
28717 tree expr;
28719 cp_token *token = cp_lexer_peek_token (parser->lexer);
28720 location_t kw_loc = token->location; /* Save for later. */
28722 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28724 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28726 bool expr_non_constant_p;
28727 cp_lexer_set_source_position (parser->lexer);
28728 /* ??? : probably a moot point? */
28729 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28730 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
28732 else
28733 expr = cp_parser_assignment_expression (parser);
28735 if (expr == error_mark_node)
28736 return expr;
28738 return finish_co_yield_expr (kw_loc, expr);
28741 /* GNU Extensions */
28743 /* Parse an (optional) asm-specification.
28745 asm-specification:
28746 asm ( string-literal )
28748 If the asm-specification is present, returns a STRING_CST
28749 corresponding to the string-literal. Otherwise, returns
28750 NULL_TREE. */
28752 static tree
28753 cp_parser_asm_specification_opt (cp_parser* parser)
28755 /* Peek at the next token. */
28756 cp_token *token = cp_lexer_peek_token (parser->lexer);
28757 /* If the next token isn't the `asm' keyword, then there's no
28758 asm-specification. */
28759 if (!cp_parser_is_keyword (token, RID_ASM))
28760 return NULL_TREE;
28762 /* Consume the `asm' token. */
28763 cp_lexer_consume_token (parser->lexer);
28764 /* Look for the `('. */
28765 matching_parens parens;
28766 parens.require_open (parser);
28768 /* Look for the string-literal. */
28769 tree asm_specification = cp_parser_string_literal (parser,
28770 /*translate=*/false,
28771 /*wide_ok=*/false);
28773 /* Look for the `)'. */
28774 parens.require_close (parser);
28776 return asm_specification;
28779 /* Parse an asm-operand-list.
28781 asm-operand-list:
28782 asm-operand
28783 asm-operand-list , asm-operand
28785 asm-operand:
28786 string-literal ( expression )
28787 [ string-literal ] string-literal ( expression )
28789 Returns a TREE_LIST representing the operands. The TREE_VALUE of
28790 each node is the expression. The TREE_PURPOSE is itself a
28791 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28792 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28793 is a STRING_CST for the string literal before the parenthesis. Returns
28794 ERROR_MARK_NODE if any of the operands are invalid. */
28796 static tree
28797 cp_parser_asm_operand_list (cp_parser* parser)
28799 tree asm_operands = NULL_TREE;
28800 bool invalid_operands = false;
28802 while (true)
28804 tree name;
28806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28808 /* Consume the `[' token. */
28809 cp_lexer_consume_token (parser->lexer);
28810 /* Read the operand name. */
28811 name = cp_parser_identifier (parser);
28812 if (name != error_mark_node)
28813 name = build_string (IDENTIFIER_LENGTH (name),
28814 IDENTIFIER_POINTER (name));
28815 /* Look for the closing `]'. */
28816 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28818 else
28819 name = NULL_TREE;
28820 /* Look for the string-literal. */
28821 tree string_literal = cp_parser_string_literal (parser,
28822 /*translate=*/false,
28823 /*wide_ok=*/false);
28825 /* Look for the `('. */
28826 matching_parens parens;
28827 parens.require_open (parser);
28828 /* Parse the expression. */
28829 tree expression = cp_parser_expression (parser);
28830 /* Look for the `)'. */
28831 parens.require_close (parser);
28833 if (name == error_mark_node
28834 || string_literal == error_mark_node
28835 || expression == error_mark_node)
28836 invalid_operands = true;
28838 /* Add this operand to the list. */
28839 asm_operands = tree_cons (build_tree_list (name, string_literal),
28840 expression,
28841 asm_operands);
28842 /* If the next token is not a `,', there are no more
28843 operands. */
28844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28845 break;
28846 /* Consume the `,'. */
28847 cp_lexer_consume_token (parser->lexer);
28850 return invalid_operands ? error_mark_node : nreverse (asm_operands);
28853 /* Parse an asm-clobber-list.
28855 asm-clobber-list:
28856 string-literal
28857 asm-clobber-list , string-literal
28859 Returns a TREE_LIST, indicating the clobbers in the order that they
28860 appeared. The TREE_VALUE of each node is a STRING_CST. */
28862 static tree
28863 cp_parser_asm_clobber_list (cp_parser* parser)
28865 tree clobbers = NULL_TREE;
28867 while (true)
28869 /* Look for the string literal. */
28870 tree string_literal = cp_parser_string_literal (parser,
28871 /*translate=*/false,
28872 /*wide_ok=*/false);
28873 /* Add it to the list. */
28874 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
28875 /* If the next token is not a `,', then the list is
28876 complete. */
28877 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28878 break;
28879 /* Consume the `,' token. */
28880 cp_lexer_consume_token (parser->lexer);
28883 return clobbers;
28886 /* Parse an asm-label-list.
28888 asm-label-list:
28889 identifier
28890 asm-label-list , identifier
28892 Returns a TREE_LIST, indicating the labels in the order that they
28893 appeared. The TREE_VALUE of each node is a label. */
28895 static tree
28896 cp_parser_asm_label_list (cp_parser* parser)
28898 tree labels = NULL_TREE;
28900 while (true)
28902 tree identifier, label, name;
28904 /* Look for the identifier. */
28905 identifier = cp_parser_identifier (parser);
28906 if (!error_operand_p (identifier))
28908 label = lookup_label (identifier);
28909 if (TREE_CODE (label) == LABEL_DECL)
28911 TREE_USED (label) = 1;
28912 check_goto (label);
28913 name = build_string (IDENTIFIER_LENGTH (identifier),
28914 IDENTIFIER_POINTER (identifier));
28915 labels = tree_cons (name, label, labels);
28918 /* If the next token is not a `,', then the list is
28919 complete. */
28920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28921 break;
28922 /* Consume the `,' token. */
28923 cp_lexer_consume_token (parser->lexer);
28926 return nreverse (labels);
28929 /* Return TRUE iff the next tokens in the stream are possibly the
28930 beginning of a GNU extension attribute. */
28932 static bool
28933 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
28935 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
28938 /* Return TRUE iff the next tokens in the stream are possibly the
28939 beginning of a standard C++-11 attribute specifier. */
28941 static bool
28942 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
28944 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
28947 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28948 beginning of a standard C++-11 attribute specifier. */
28950 static bool
28951 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
28953 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28955 return (cxx_dialect >= cxx11
28956 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
28957 || (token->type == CPP_OPEN_SQUARE
28958 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
28959 && token->type == CPP_OPEN_SQUARE)));
28962 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28963 beginning of a GNU extension attribute. */
28965 static bool
28966 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
28968 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28970 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
28973 /* Return true iff the next tokens can be the beginning of either a
28974 GNU attribute list, or a standard C++11 attribute sequence. */
28976 static bool
28977 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
28979 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
28980 || cp_next_tokens_can_be_std_attribute_p (parser));
28983 /* Return true iff the next Nth tokens can be the beginning of either
28984 a GNU attribute list, or a standard C++11 attribute sequence. */
28986 static bool
28987 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
28989 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
28990 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
28993 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
28994 of GNU attributes, or return NULL. */
28996 static tree
28997 cp_parser_attributes_opt (cp_parser *parser)
28999 tree attrs = NULL_TREE;
29000 while (true)
29002 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29003 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
29004 else if (cp_next_tokens_can_be_std_attribute_p (parser))
29005 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
29006 else
29007 break;
29009 return attrs;
29012 /* Parse an (optional) series of attributes.
29014 attributes:
29015 attributes attribute
29017 attribute:
29018 __attribute__ (( attribute-list [opt] ))
29020 The return value is as for cp_parser_gnu_attribute_list. */
29022 static tree
29023 cp_parser_gnu_attributes_opt (cp_parser* parser)
29025 tree attributes = NULL_TREE;
29027 auto cleanup = make_temp_override
29028 (parser->auto_is_implicit_function_template_parm_p, false);
29030 while (true)
29032 cp_token *token;
29033 tree attribute_list;
29034 bool ok = true;
29036 /* Peek at the next token. */
29037 token = cp_lexer_peek_token (parser->lexer);
29038 /* If it's not `__attribute__', then we're done. */
29039 if (token->keyword != RID_ATTRIBUTE)
29040 break;
29042 /* Consume the `__attribute__' keyword. */
29043 cp_lexer_consume_token (parser->lexer);
29044 /* Look for the two `(' tokens. */
29045 matching_parens outer_parens;
29046 if (!outer_parens.require_open (parser))
29047 ok = false;
29048 matching_parens inner_parens;
29049 if (!inner_parens.require_open (parser))
29050 ok = false;
29052 /* Peek at the next token. */
29053 token = cp_lexer_peek_token (parser->lexer);
29054 if (token->type != CPP_CLOSE_PAREN)
29055 /* Parse the attribute-list. */
29056 attribute_list = cp_parser_gnu_attribute_list (parser);
29057 else
29058 /* If the next token is a `)', then there is no attribute
29059 list. */
29060 attribute_list = NULL;
29062 /* Look for the two `)' tokens. */
29063 if (!inner_parens.require_close (parser))
29064 ok = false;
29065 if (!outer_parens.require_close (parser))
29066 ok = false;
29067 if (!ok)
29068 cp_parser_skip_to_end_of_statement (parser);
29070 /* Add these new attributes to the list. */
29071 attributes = attr_chainon (attributes, attribute_list);
29074 return attributes;
29077 /* Parse a GNU attribute-list.
29079 attribute-list:
29080 attribute
29081 attribute-list , attribute
29083 attribute:
29084 identifier
29085 identifier ( identifier )
29086 identifier ( identifier , expression-list )
29087 identifier ( expression-list )
29089 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
29090 to an attribute. The TREE_PURPOSE of each node is the identifier
29091 indicating which attribute is in use. The TREE_VALUE represents
29092 the arguments, if any. */
29094 static tree
29095 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
29097 tree attribute_list = NULL_TREE;
29098 bool save_translate_strings_p = parser->translate_strings_p;
29100 /* Don't create wrapper nodes within attributes: the
29101 handlers don't know how to handle them. */
29102 auto_suppress_location_wrappers sentinel;
29104 parser->translate_strings_p = false;
29105 while (true)
29107 cp_token *token;
29108 tree identifier;
29109 tree attribute;
29111 /* Look for the identifier. We also allow keywords here; for
29112 example `__attribute__ ((const))' is legal. */
29113 token = cp_lexer_peek_token (parser->lexer);
29114 if (token->type == CPP_NAME
29115 || token->type == CPP_KEYWORD)
29117 tree arguments = NULL_TREE;
29119 /* Consume the token, but save it since we need it for the
29120 SIMD enabled function parsing. */
29121 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
29123 /* Save away the identifier that indicates which attribute
29124 this is. */
29125 identifier = (token->type == CPP_KEYWORD)
29126 /* For keywords, use the canonical spelling, not the
29127 parsed identifier. */
29128 ? ridpointers[(int) token->keyword]
29129 : id_token->u.value;
29131 identifier = canonicalize_attr_name (identifier);
29132 attribute = build_tree_list (identifier, NULL_TREE);
29134 /* Peek at the next token. */
29135 token = cp_lexer_peek_token (parser->lexer);
29136 /* If it's an `(', then parse the attribute arguments. */
29137 if (token->type == CPP_OPEN_PAREN)
29139 vec<tree, va_gc> *vec;
29140 int attr_flag = (attribute_takes_identifier_p (identifier)
29141 ? id_attr : normal_attr);
29142 if (is_attribute_p ("assume", identifier))
29143 attr_flag = assume_attr;
29144 vec = cp_parser_parenthesized_expression_list
29145 (parser, attr_flag, /*cast_p=*/false,
29146 /*allow_expansion_p=*/false,
29147 /*non_constant_p=*/NULL);
29148 if (vec == NULL)
29149 arguments = error_mark_node;
29150 else
29152 arguments = build_tree_list_vec (vec);
29153 release_tree_vector (vec);
29155 /* Save the arguments away. */
29156 TREE_VALUE (attribute) = arguments;
29159 if (arguments != error_mark_node)
29161 /* Add this attribute to the list. */
29162 TREE_CHAIN (attribute) = attribute_list;
29163 attribute_list = attribute;
29166 token = cp_lexer_peek_token (parser->lexer);
29168 /* Unless EXACTLY_ONE is set look for more attributes.
29169 If the next token isn't a `,', we're done. */
29170 if (exactly_one || token->type != CPP_COMMA)
29171 break;
29173 /* Consume the comma and keep going. */
29174 cp_lexer_consume_token (parser->lexer);
29176 parser->translate_strings_p = save_translate_strings_p;
29178 /* We built up the list in reverse order. */
29179 return nreverse (attribute_list);
29182 /* Parse arguments of omp::directive attribute.
29184 ( directive-name ,[opt] clause-list[opt] )
29186 For directive just remember the first/last tokens for subsequent
29187 parsing. */
29189 static void
29190 cp_parser_omp_directive_args (cp_parser *parser, tree attribute)
29192 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
29193 if (first->type == CPP_CLOSE_PAREN)
29195 cp_lexer_consume_token (parser->lexer);
29196 error_at (first->location, "expected OpenMP directive name");
29197 cp_lexer_consume_token (parser->lexer);
29198 TREE_VALUE (attribute) = NULL_TREE;
29199 return;
29201 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
29202 if (n == 1)
29204 cp_lexer_consume_token (parser->lexer);
29205 error_at (first->location, "expected attribute argument as balanced "
29206 "token sequence");
29207 TREE_VALUE (attribute) = NULL_TREE;
29208 return;
29210 for (n = n - 2; n; --n)
29211 cp_lexer_consume_token (parser->lexer);
29212 cp_token *last = cp_lexer_peek_token (parser->lexer);
29213 cp_lexer_consume_token (parser->lexer);
29214 tree arg = make_node (DEFERRED_PARSE);
29215 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
29216 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
29217 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
29220 /* Parse arguments of omp::sequence attribute.
29222 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
29224 static void
29225 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
29227 matching_parens parens;
29228 parens.consume_open (parser);
29231 cp_token *token = cp_lexer_peek_token (parser->lexer);
29232 if (token->type == CPP_NAME
29233 && token->u.value == omp_identifier
29234 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
29236 cp_lexer_consume_token (parser->lexer);
29237 cp_lexer_consume_token (parser->lexer);
29238 token = cp_lexer_peek_token (parser->lexer);
29240 bool directive = false;
29241 const char *p;
29242 if (token->type != CPP_NAME)
29243 p = "";
29244 else
29245 p = IDENTIFIER_POINTER (token->u.value);
29246 if (strcmp (p, "directive") == 0)
29247 directive = true;
29248 else if (strcmp (p, "sequence") != 0)
29250 error_at (token->location, "expected %<directive%> or %<sequence%>");
29251 cp_parser_skip_to_closing_parenthesis (parser,
29252 /*recovering=*/true,
29253 /*or_comma=*/true,
29254 /*consume_paren=*/false);
29255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29256 break;
29257 cp_lexer_consume_token (parser->lexer);
29259 cp_lexer_consume_token (parser->lexer);
29260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
29261 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
29262 UNKNOWN_LOCATION);
29263 else if (directive)
29264 cp_parser_omp_directive_args (parser, attribute);
29265 else
29266 cp_parser_omp_sequence_args (parser, attribute);
29267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29268 break;
29269 cp_lexer_consume_token (parser->lexer);
29271 while (1);
29272 if (!parens.require_close (parser))
29273 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29274 /*consume_paren=*/true);
29277 /* Parse a standard C++11 attribute.
29279 The returned representation is a TREE_LIST which TREE_PURPOSE is
29280 the scoped name of the attribute, and the TREE_VALUE is its
29281 arguments list.
29283 Note that the scoped name of the attribute is itself a TREE_LIST
29284 which TREE_PURPOSE is the namespace of the attribute, and
29285 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29286 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29287 and which TREE_PURPOSE is directly the attribute name.
29289 Clients of the attribute code should use get_attribute_namespace
29290 and get_attribute_name to get the actual namespace and name of
29291 attributes, regardless of their being GNU or C++11 attributes.
29293 attribute:
29294 attribute-token attribute-argument-clause [opt]
29296 attribute-token:
29297 identifier
29298 attribute-scoped-token
29300 attribute-scoped-token:
29301 attribute-namespace :: identifier
29303 attribute-namespace:
29304 identifier
29306 attribute-argument-clause:
29307 ( balanced-token-seq )
29309 balanced-token-seq:
29310 balanced-token [opt]
29311 balanced-token-seq balanced-token
29313 balanced-token:
29314 ( balanced-token-seq )
29315 [ balanced-token-seq ]
29316 { balanced-token-seq }. */
29318 static tree
29319 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
29321 tree attribute, attr_id = NULL_TREE, arguments;
29322 cp_token *token;
29324 auto cleanup = make_temp_override
29325 (parser->auto_is_implicit_function_template_parm_p, false);
29327 /* First, parse name of the attribute, a.k.a attribute-token. */
29329 token = cp_lexer_peek_token (parser->lexer);
29330 if (token->type == CPP_NAME)
29331 attr_id = token->u.value;
29332 else if (token->type == CPP_KEYWORD)
29333 attr_id = ridpointers[(int) token->keyword];
29334 else if (token->flags & NAMED_OP)
29335 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29337 if (attr_id == NULL_TREE)
29338 return NULL_TREE;
29340 cp_lexer_consume_token (parser->lexer);
29342 token = cp_lexer_peek_token (parser->lexer);
29343 if (token->type == CPP_SCOPE)
29345 /* We are seeing a scoped attribute token. */
29347 cp_lexer_consume_token (parser->lexer);
29348 if (attr_ns)
29349 error_at (token->location, "attribute using prefix used together "
29350 "with scoped attribute token");
29351 attr_ns = attr_id;
29353 token = cp_lexer_peek_token (parser->lexer);
29354 if (token->type == CPP_NAME)
29355 attr_id = token->u.value;
29356 else if (token->type == CPP_KEYWORD)
29357 attr_id = ridpointers[(int) token->keyword];
29358 else if (token->flags & NAMED_OP)
29359 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29360 else
29362 error_at (token->location,
29363 "expected an identifier for the attribute name");
29364 return error_mark_node;
29366 cp_lexer_consume_token (parser->lexer);
29368 attr_ns = canonicalize_attr_name (attr_ns);
29369 attr_id = canonicalize_attr_name (attr_id);
29370 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29371 NULL_TREE);
29372 token = cp_lexer_peek_token (parser->lexer);
29374 else if (attr_ns)
29376 attr_ns = canonicalize_attr_name (attr_ns);
29377 attr_id = canonicalize_attr_name (attr_id);
29378 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29379 NULL_TREE);
29381 else
29383 attr_id = canonicalize_attr_name (attr_id);
29384 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29385 NULL_TREE);
29387 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29388 but no longer: we have to be able to tell [[noreturn]] and
29389 __attribute__((noreturn)) apart. */
29390 /* C++14 deprecated attribute is equivalent to GNU's. */
29391 if (is_attribute_p ("deprecated", attr_id))
29392 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29393 /* C++17 fallthrough attribute is equivalent to GNU's. */
29394 else if (is_attribute_p ("fallthrough", attr_id))
29395 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29396 /* C++23 assume attribute is equivalent to GNU's. */
29397 else if (is_attribute_p ("assume", attr_id))
29398 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29399 /* Transactional Memory TS optimize_for_synchronized attribute is
29400 equivalent to GNU transaction_callable. */
29401 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29402 TREE_PURPOSE (attribute)
29403 = get_identifier ("transaction_callable");
29404 /* Transactional Memory attributes are GNU attributes. */
29405 else if (tm_attr_to_mask (attr_id))
29406 TREE_PURPOSE (attribute) = attr_id;
29409 /* Now parse the optional argument clause of the attribute. */
29411 if (token->type != CPP_OPEN_PAREN)
29413 if ((flag_openmp || flag_openmp_simd)
29414 && attr_ns == omp_identifier
29415 && (is_attribute_p ("directive", attr_id)
29416 || is_attribute_p ("sequence", attr_id)))
29418 error_at (token->location, "%<omp::%E%> attribute requires argument",
29419 attr_id);
29420 return NULL_TREE;
29422 return attribute;
29426 vec<tree, va_gc> *vec;
29427 int attr_flag = normal_attr;
29429 /* Maybe we don't expect to see any arguments for this attribute. */
29430 const attribute_spec *as
29431 = lookup_attribute_spec (TREE_PURPOSE (attribute));
29432 if (as && as->max_length == 0)
29434 error_at (token->location, "%qE attribute does not take any arguments",
29435 attr_id);
29436 cp_parser_skip_to_closing_parenthesis (parser,
29437 /*recovering=*/true,
29438 /*or_comma=*/false,
29439 /*consume_paren=*/true);
29440 return error_mark_node;
29443 if (is_attribute_p ("assume", attr_id)
29444 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
29445 /* The assume attribute needs special handling of the argument. */
29446 attr_flag = assume_attr;
29447 else if (attr_ns == gnu_identifier
29448 && attribute_takes_identifier_p (attr_id))
29449 /* A GNU attribute that takes an identifier in parameter. */
29450 attr_flag = id_attr;
29452 /* If this is a fake attribute created to handle -Wno-attributes,
29453 we must skip parsing the arguments. */
29454 if (as == NULL || attribute_ignored_p (as))
29456 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
29458 if (is_attribute_p ("directive", attr_id))
29460 cp_parser_omp_directive_args (parser, attribute);
29461 return attribute;
29463 else if (is_attribute_p ("sequence", attr_id))
29465 TREE_VALUE (TREE_PURPOSE (attribute))
29466 = get_identifier ("directive");
29467 cp_parser_omp_sequence_args (parser, attribute);
29468 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
29469 return attribute;
29473 /* For unknown attributes, just skip balanced tokens instead of
29474 trying to parse the arguments. Set TREE_VALUE (attribute) to
29475 error_mark_node to distinguish skipped arguments from attributes
29476 with no arguments. */
29477 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29478 cp_lexer_consume_token (parser->lexer);
29479 TREE_VALUE (attribute) = error_mark_node;
29480 return attribute;
29483 vec = cp_parser_parenthesized_expression_list
29484 (parser, attr_flag, /*cast_p=*/false,
29485 /*allow_expansion_p=*/true,
29486 /*non_constant_p=*/NULL);
29487 if (vec == NULL)
29488 arguments = error_mark_node;
29489 else
29491 if (vec->is_empty ())
29492 /* e.g. [[attr()]]. */
29493 error_at (token->location, "parentheses must be omitted if "
29494 "%qE attribute argument list is empty",
29495 attr_id);
29496 arguments = build_tree_list_vec (vec);
29497 release_tree_vector (vec);
29500 if (arguments == error_mark_node)
29501 attribute = error_mark_node;
29502 else
29503 TREE_VALUE (attribute) = arguments;
29506 return attribute;
29509 /* Warn if the attribute ATTRIBUTE appears more than once in the
29510 attribute-list ATTRIBUTES. This used to be enforced for certain
29511 attributes, but the restriction was removed in P2156.
29512 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29513 found in ATTRIBUTES. */
29515 static bool
29516 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29518 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29519 "likely", "unlikely", "fallthrough",
29520 "no_unique_address", "carries_dependency" };
29521 if (attributes)
29522 for (const auto &a : alist)
29523 if (is_attribute_p (a, get_attribute_name (attribute))
29524 && is_attribute_namespace_p ("", attribute)
29525 && lookup_attribute ("", a, attributes))
29527 if (!from_macro_expansion_at (loc))
29528 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29529 "multiple times", a);
29530 return false;
29532 return true;
29535 /* Parse a list of standard C++-11 attributes.
29537 attribute-list:
29538 attribute [opt]
29539 attribute-list , attribute[opt]
29540 attribute ...
29541 attribute-list , attribute ...
29544 static tree
29545 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29547 tree attributes = NULL_TREE, attribute = NULL_TREE;
29548 cp_token *token = NULL;
29550 while (true)
29552 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29553 attribute = cp_parser_std_attribute (parser, attr_ns);
29554 if (attribute == error_mark_node)
29555 break;
29556 if (attribute != NULL_TREE)
29558 if (cp_parser_check_std_attribute (loc, attributes, attribute))
29560 TREE_CHAIN (attribute) = attributes;
29561 attributes = attribute;
29564 token = cp_lexer_peek_token (parser->lexer);
29565 if (token->type == CPP_ELLIPSIS)
29567 cp_lexer_consume_token (parser->lexer);
29568 if (attribute == NULL_TREE)
29569 error_at (token->location,
29570 "expected attribute before %<...%>");
29571 else if (TREE_VALUE (attribute) == NULL_TREE)
29573 error_at (token->location, "attribute with no arguments "
29574 "contains no parameter packs");
29575 return error_mark_node;
29577 else if (TREE_VALUE (attribute) != error_mark_node)
29579 tree pack = make_pack_expansion (TREE_VALUE (attribute));
29580 if (pack == error_mark_node)
29581 return error_mark_node;
29582 TREE_VALUE (attribute) = pack;
29584 token = cp_lexer_peek_token (parser->lexer);
29586 if (token->type != CPP_COMMA)
29587 break;
29588 cp_lexer_consume_token (parser->lexer);
29590 attributes = nreverse (attributes);
29591 return attributes;
29594 /* Optionally parse a C++20 contract role. A NULL return means that no
29595 contract role was specified.
29597 contract-role:
29598 % default
29599 % identifier
29601 If the identifier does not name a known contract role, it will
29602 be assumed to be default. Returns the identifier for the role
29603 token. */
29605 static tree
29606 cp_parser_contract_role (cp_parser *parser)
29608 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
29609 cp_lexer_consume_token (parser->lexer);
29611 cp_token *token = cp_lexer_peek_token (parser->lexer);
29612 tree role_id = NULL_TREE;
29613 if (token->type == CPP_NAME)
29614 role_id = token->u.value;
29615 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
29616 role_id = get_identifier ("default");
29617 else
29619 error_at (token->location, "expected contract-role");
29620 return error_mark_node;
29622 cp_lexer_consume_token (parser->lexer);
29624 /* FIXME: Warn about invalid/unknown roles? */
29625 return role_id;
29628 /* Parse an optional contract mode.
29630 contract-mode:
29631 contract-semantic
29632 [contract-level] [contract-role]
29634 contract-semantic:
29635 check_never_continue
29636 check_maybe_continue
29637 check_always_continue
29639 contract-level:
29640 default
29641 audit
29642 axiom
29644 contract-role:
29645 default
29646 identifier
29648 This grammar is taken from P1332R0. During parsing, this sets options
29649 on the MODE object to determine the configuration of the contract.
29651 Returns a tree containing the identifiers used in the configuration.
29652 This is either an IDENTIFIER with the literal semantic or a TREE_LIST
29653 whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
29654 contract-role, if any. NULL_TREE is returned if no information is
29655 given (i.e., all defaults selected). */
29657 static tree
29658 cp_parser_contract_mode_opt (cp_parser *parser,
29659 bool postcondition_p)
29661 /* The mode is empty; the level and role are default. */
29662 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29663 return NULL_TREE;
29665 /* There is only a role; the level is default. */
29666 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29668 tree role_id = cp_parser_contract_role (parser);
29669 return build_tree_list (role_id, get_identifier ("default"));
29672 /* Otherwise, match semantic or level. */
29673 cp_token *token = cp_lexer_peek_token (parser->lexer);
29674 contract_level level = CONTRACT_INVALID;
29675 contract_semantic semantic = CCS_INVALID;
29676 tree config_id;
29677 if (token->type == CPP_NAME)
29679 config_id = token->u.value;
29681 /* Either a named level, a concrete semantic, or an identifier
29682 for a postcondition. */
29683 const char *ident = IDENTIFIER_POINTER (token->u.value);
29684 level = map_contract_level (ident);
29685 semantic = map_contract_semantic (ident);
29687 /* The identifier is the return value for a postcondition. */
29688 if (level == CONTRACT_INVALID && semantic == CCS_INVALID
29689 && postcondition_p)
29690 return NULL_TREE;
29692 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
29694 config_id = get_identifier ("default");
29695 level = CONTRACT_DEFAULT;
29697 else
29699 /* We got some other token other than a ':'. */
29700 error_at (token->location, "expected contract semantic or level");
29701 return NULL_TREE;
29704 /* Consume the literal semantic or level token. */
29705 cp_lexer_consume_token (parser->lexer);
29707 if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
29709 error_at (token->location,
29710 "expected contract level: "
29711 "%<default%>, %<audit%>, or %<axiom%>");
29712 return NULL_TREE;
29715 /* We matched an explicit semantic. */
29716 if (semantic != CCS_INVALID)
29718 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29720 error ("invalid use of contract role for explicit semantic");
29721 cp_lexer_consume_token (parser->lexer);
29722 cp_lexer_consume_token (parser->lexer);
29724 return config_id;
29727 /* We matched a level, there may be a role; otherwise this is default. */
29728 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29730 tree role_id = cp_parser_contract_role (parser);
29731 return build_tree_list (role_id, config_id);
29734 return build_tree_list (NULL_TREE, config_id);
29737 static tree
29738 find_error (tree *tp, int *, void *)
29740 if (*tp == error_mark_node)
29741 return *tp;
29742 return NULL_TREE;
29745 static bool
29746 contains_error_p (tree t)
29748 return walk_tree (&t, find_error, NULL, NULL);
29751 /* Parse a standard C++20 contract attribute specifier.
29753 contract-attribute-specifier:
29754 [ [ assert contract-level [opt] : conditional-expression ] ]
29755 [ [ pre contract-level [opt] : conditional-expression ] ]
29756 [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
29758 For free functions, we cannot determine the type of the postcondition
29759 identifier because the we haven't called grokdeclarator yet. In those
29760 cases we parse the postcondition as if the identifier was declared as
29761 'auto <identifier>'. We then instantiate the postcondition once the
29762 return type is known.
29764 For member functions, contracts are in the complete-class context, so the
29765 parse is deferred. We also have the return type avaialable (unless it's
29766 deduced), so we don't need to parse the postcondition in terms of a
29767 placeholder. */
29769 static tree
29770 cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
29772 gcc_assert (contract_attribute_p (attribute));
29773 cp_token *token = cp_lexer_consume_token (parser->lexer);
29774 location_t loc = token->location;
29776 bool assertion_p = is_attribute_p ("assert", attribute);
29777 bool postcondition_p = is_attribute_p ("post", attribute);
29779 /* Parse the optional mode. */
29780 tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
29782 /* Check for postcondition identifiers. */
29783 cp_expr identifier;
29784 if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29785 identifier = cp_parser_identifier (parser);
29786 if (identifier == error_mark_node)
29787 return error_mark_node;
29789 cp_parser_require (parser, CPP_COLON, RT_COLON);
29791 /* Defer the parsing of pre/post contracts inside class definitions. */
29792 tree contract;
29793 if (!assertion_p &&
29794 current_class_type &&
29795 TYPE_BEING_DEFINED (current_class_type))
29797 /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
29798 that doesn't close the attribute, return an error and let the attribute
29799 handling code emit an error for missing ']]'. */
29800 cp_token *first = cp_lexer_peek_token (parser->lexer);
29801 cp_parser_skip_to_closing_parenthesis_1 (parser,
29802 /*recovering=*/false,
29803 CPP_CLOSE_SQUARE,
29804 /*consume_paren=*/false);
29805 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
29806 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
29807 return error_mark_node;
29808 cp_token *last = cp_lexer_peek_token (parser->lexer);
29810 /* Build a deferred-parse node. */
29811 tree condition = make_node (DEFERRED_PARSE);
29812 DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
29813 DEFPARSE_INSTANTIATIONS (condition) = NULL;
29815 /* And its corresponding contract. */
29816 contract = grok_contract (attribute, mode, identifier, condition, loc);
29818 else
29820 /* Enable location wrappers when parsing contracts. */
29821 auto suppression = make_temp_override (suppress_location_wrappers, 0);
29823 /* Build a fake variable for the result identifier. */
29824 tree result = NULL_TREE;
29825 if (identifier)
29827 begin_scope (sk_block, NULL_TREE);
29828 result = make_postcondition_variable (identifier);
29829 ++processing_template_decl;
29832 /* Parse the condition, ensuring that parameters or the return variable
29833 aren't flagged for use outside the body of a function. */
29834 ++processing_contract_condition;
29835 cp_expr condition = cp_parser_conditional_expression (parser);
29836 --processing_contract_condition;
29838 /* Try to recover from errors by scanning up to the end of the
29839 attribute. Sometimes we get partially parsed expressions, so
29840 we need to search the condition for errors. */
29841 if (contains_error_p (condition))
29842 cp_parser_skip_up_to_closing_square_bracket (parser);
29844 /* Build the contract. */
29845 contract = grok_contract (attribute, mode, result, condition, loc);
29847 /* Leave our temporary scope for the postcondition result. */
29848 if (result)
29850 --processing_template_decl;
29851 pop_bindings_and_leave_scope ();
29855 if (!flag_contracts)
29857 error_at (loc, "contracts are only available with %<-fcontracts%>");
29858 return error_mark_node;
29861 return finish_contract_attribute (attribute, contract);
29864 /* Parse a contract condition for a deferred contract. */
29866 void cp_parser_late_contract_condition (cp_parser *parser,
29867 tree fn,
29868 tree attribute)
29870 tree contract = TREE_VALUE (TREE_VALUE (attribute));
29872 /* Make sure we've gotten something that hasn't been parsed yet or that
29873 we're not parsing an invalid contract. */
29874 tree condition = CONTRACT_CONDITION (contract);
29875 if (TREE_CODE (condition) != DEFERRED_PARSE)
29876 return;
29878 tree identifier = NULL_TREE;
29879 if (TREE_CODE (contract) == POSTCONDITION_STMT)
29880 identifier = POSTCONDITION_IDENTIFIER (contract);
29882 /* Build a fake variable for the result identifier. */
29883 tree result = NULL_TREE;
29884 if (identifier)
29886 /* TODO: Can we guarantee that the identifier has a location? */
29887 location_t loc = cp_expr_location (contract);
29888 tree type = TREE_TYPE (TREE_TYPE (fn));
29889 if (!check_postcondition_result (fn, type, loc))
29891 invalidate_contract (contract);
29892 return;
29895 begin_scope (sk_block, NULL_TREE);
29896 result = make_postcondition_variable (identifier, type);
29897 ++processing_template_decl;
29900 /* 'this' is not allowed in preconditions of constructors or in postconditions
29901 of destructors. Note that the previous value of this variable is
29902 established by the calling function, so we need to save it here. */
29903 tree saved_ccr = current_class_ref;
29904 tree saved_ccp = current_class_ptr;
29905 if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
29906 (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
29908 current_class_ref = current_class_ptr = NULL_TREE;
29909 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
29912 push_unparsed_function_queues (parser);
29914 /* Push the saved tokens onto the parser's lexer stack. */
29915 cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
29916 cp_parser_push_lexer_for_tokens (parser, tokens);
29918 /* Parse the condition, ensuring that parameters or the return variable
29919 aren't flagged for use outside the body of a function. */
29920 ++processing_contract_condition;
29921 condition = cp_parser_conditional_expression (parser);
29922 --processing_contract_condition;
29924 /* Revert to the main lexer. */
29925 cp_parser_pop_lexer (parser);
29927 /* Restore the queue. */
29928 pop_unparsed_function_queues (parser);
29930 current_class_ref = saved_ccr;
29931 current_class_ptr = saved_ccp;
29933 /* Commit to changes. */
29934 update_late_contract (contract, result, condition);
29936 /* Leave our temporary scope for the postcondition result. */
29937 if (result)
29939 --processing_template_decl;
29940 pop_bindings_and_leave_scope ();
29944 /* Parse a standard C++-11 attribute specifier.
29946 attribute-specifier:
29947 [ [ attribute-using-prefix [opt] attribute-list ] ]
29948 contract-attribute-specifier
29949 alignment-specifier
29951 attribute-using-prefix:
29952 using attribute-namespace :
29954 alignment-specifier:
29955 alignas ( type-id ... [opt] )
29956 alignas ( alignment-expression ... [opt] ).
29958 Extensions for contracts:
29960 contract-attribute-specifier:
29961 [ [ assert : contract-mode [opt] : conditional-expression ] ]
29962 [ [ pre : contract-mode [opt] : conditional-expression ] ]
29963 [ [ post : contract-mode [opt] identifier [opt] :
29964 conditional-expression ] ] */
29966 static tree
29967 cp_parser_std_attribute_spec (cp_parser *parser)
29969 tree attributes = NULL_TREE;
29970 cp_token *token = cp_lexer_peek_token (parser->lexer);
29972 if (token->type == CPP_OPEN_SQUARE
29973 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
29975 tree attr_ns = NULL_TREE;
29976 tree attr_name = NULL_TREE;
29978 cp_lexer_consume_token (parser->lexer);
29979 cp_lexer_consume_token (parser->lexer);
29981 token = cp_lexer_peek_token (parser->lexer);
29982 if (token->type == CPP_NAME)
29984 attr_name = token->u.value;
29985 attr_name = canonicalize_attr_name (attr_name);
29988 /* Handle contract-attribute-specs specially. */
29989 if (attr_name && contract_attribute_p (attr_name))
29991 tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
29992 if (attrs != error_mark_node)
29993 attributes = attrs;
29994 goto finish_attrs;
29997 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29999 token = cp_lexer_peek_nth_token (parser->lexer, 2);
30000 if (token->type == CPP_NAME)
30001 attr_ns = token->u.value;
30002 else if (token->type == CPP_KEYWORD)
30003 attr_ns = ridpointers[(int) token->keyword];
30004 else if (token->flags & NAMED_OP)
30005 attr_ns = get_identifier (cpp_type2name (token->type,
30006 token->flags));
30007 if (attr_ns
30008 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
30010 if (cxx_dialect < cxx17)
30011 pedwarn (input_location, OPT_Wc__17_extensions,
30012 "attribute using prefix only available "
30013 "with %<-std=c++17%> or %<-std=gnu++17%>");
30015 cp_lexer_consume_token (parser->lexer);
30016 cp_lexer_consume_token (parser->lexer);
30017 cp_lexer_consume_token (parser->lexer);
30019 else
30020 attr_ns = NULL_TREE;
30023 attributes = cp_parser_std_attribute_list (parser, attr_ns);
30025 finish_attrs:
30026 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
30027 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
30028 cp_parser_skip_to_end_of_statement (parser);
30029 else
30030 /* Warn about parsing c++11 attribute in non-c++11 mode, only
30031 when we are sure that we have actually parsed them. */
30032 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30034 else
30036 tree alignas_expr;
30038 /* Look for an alignment-specifier. */
30040 token = cp_lexer_peek_token (parser->lexer);
30042 if (token->type != CPP_KEYWORD
30043 || token->keyword != RID_ALIGNAS)
30044 return NULL_TREE;
30046 cp_lexer_consume_token (parser->lexer);
30047 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30049 matching_parens parens;
30050 if (!parens.require_open (parser))
30051 return error_mark_node;
30053 cp_parser_parse_tentatively (parser);
30054 alignas_expr = cp_parser_type_id (parser);
30056 if (!cp_parser_parse_definitely (parser))
30058 alignas_expr = cp_parser_assignment_expression (parser);
30059 if (alignas_expr == error_mark_node)
30060 cp_parser_skip_to_end_of_statement (parser);
30061 if (alignas_expr == NULL_TREE
30062 || alignas_expr == error_mark_node)
30063 return alignas_expr;
30066 alignas_expr = cxx_alignas_expr (alignas_expr);
30067 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
30069 /* Handle alignas (pack...). */
30070 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30072 cp_lexer_consume_token (parser->lexer);
30073 alignas_expr = make_pack_expansion (alignas_expr);
30076 /* Something went wrong, so don't build the attribute. */
30077 if (alignas_expr == error_mark_node)
30078 return error_mark_node;
30080 /* Missing ')' means the code cannot possibly be valid; go ahead
30081 and commit to make sure we issue a hard error. */
30082 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
30083 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30084 cp_parser_commit_to_tentative_parse (parser);
30086 if (!parens.require_close (parser))
30087 return error_mark_node;
30089 /* Build the C++-11 representation of an 'aligned'
30090 attribute. */
30091 attributes
30092 = build_tree_list (build_tree_list (gnu_identifier,
30093 aligned_identifier), alignas_expr);
30096 return attributes;
30099 /* Parse a standard C++-11 attribute-specifier-seq.
30101 attribute-specifier-seq:
30102 attribute-specifier-seq [opt] attribute-specifier */
30104 static tree
30105 cp_parser_std_attribute_spec_seq (cp_parser *parser)
30107 tree attr_specs = NULL_TREE;
30108 tree attr_last = NULL_TREE;
30110 /* Don't create wrapper nodes within attributes: the
30111 handlers don't know how to handle them. */
30112 auto_suppress_location_wrappers sentinel;
30114 while (true)
30116 tree attr_spec = cp_parser_std_attribute_spec (parser);
30117 if (attr_spec == NULL_TREE)
30118 break;
30119 if (attr_spec == error_mark_node)
30120 return error_mark_node;
30122 if (attr_last)
30123 TREE_CHAIN (attr_last) = attr_spec;
30124 else
30125 attr_specs = attr_last = attr_spec;
30126 attr_last = tree_last (attr_last);
30129 return attr_specs;
30132 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
30133 return index of the first token after balanced-token, or N on failure. */
30135 static size_t
30136 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
30138 size_t orig_n = n;
30139 int nparens = 0, nbraces = 0, nsquares = 0;
30141 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
30143 case CPP_PRAGMA_EOL:
30144 if (!parser->lexer->in_pragma)
30145 break;
30146 /* FALLTHRU */
30147 case CPP_EOF:
30148 /* Ran out of tokens. */
30149 return orig_n;
30150 case CPP_OPEN_PAREN:
30151 ++nparens;
30152 break;
30153 case CPP_OPEN_BRACE:
30154 ++nbraces;
30155 break;
30156 case CPP_OPEN_SQUARE:
30157 ++nsquares;
30158 break;
30159 case CPP_CLOSE_PAREN:
30160 --nparens;
30161 break;
30162 case CPP_CLOSE_BRACE:
30163 --nbraces;
30164 break;
30165 case CPP_CLOSE_SQUARE:
30166 --nsquares;
30167 break;
30168 default:
30169 break;
30171 while (nparens || nbraces || nsquares);
30172 return n;
30175 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
30176 return index of the first token after the GNU attribute tokens, or N on
30177 failure. */
30179 static size_t
30180 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
30182 while (true)
30184 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
30185 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
30186 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
30187 break;
30189 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
30190 if (n2 == n + 2)
30191 break;
30192 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
30193 break;
30194 n = n2 + 1;
30196 return n;
30199 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
30200 next token), return index of the first token after the standard C++11
30201 attribute tokens, or N on failure. */
30203 static size_t
30204 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
30206 while (true)
30208 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
30209 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
30211 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30212 if (n2 == n + 1)
30213 break;
30214 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
30215 break;
30216 n = n2 + 1;
30218 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
30219 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
30221 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30222 if (n2 == n + 1)
30223 break;
30224 n = n2;
30226 else
30227 break;
30229 return n;
30232 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
30233 as the next token), return index of the first token after the attribute
30234 tokens, or N on failure. */
30236 static size_t
30237 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
30239 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
30240 return cp_parser_skip_gnu_attributes_opt (parser, n);
30241 return cp_parser_skip_std_attribute_spec_seq (parser, n);
30244 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
30245 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
30246 current value of the PEDANTIC flag, regardless of whether or not
30247 the `__extension__' keyword is present. The caller is responsible
30248 for restoring the value of the PEDANTIC flag. */
30250 static bool
30251 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
30253 /* Save the old value of the PEDANTIC flag. */
30254 *saved_pedantic = pedantic;
30256 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
30258 /* Consume the `__extension__' token. */
30259 cp_lexer_consume_token (parser->lexer);
30260 /* We're not being pedantic while the `__extension__' keyword is
30261 in effect. */
30262 pedantic = 0;
30264 return true;
30267 return false;
30270 /* Parse a label declaration.
30272 label-declaration:
30273 __label__ label-declarator-seq ;
30275 label-declarator-seq:
30276 identifier , label-declarator-seq
30277 identifier */
30279 static void
30280 cp_parser_label_declaration (cp_parser* parser)
30282 /* Look for the `__label__' keyword. */
30283 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
30285 while (true)
30287 tree identifier;
30289 /* Look for an identifier. */
30290 identifier = cp_parser_identifier (parser);
30291 /* If we failed, stop. */
30292 if (identifier == error_mark_node)
30293 break;
30294 /* Declare it as a label. */
30295 finish_label_decl (identifier);
30296 /* If the next token is a `;', stop. */
30297 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30298 break;
30299 /* Look for the `,' separating the label declarations. */
30300 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
30303 /* Look for the final `;'. */
30304 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30307 // -------------------------------------------------------------------------- //
30308 // Concept definitions
30310 static tree
30311 cp_parser_concept_definition (cp_parser *parser)
30313 /* A concept definition is an unevaluated context. */
30314 cp_unevaluated u;
30316 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
30317 cp_lexer_consume_token (parser->lexer);
30319 cp_expr id = cp_parser_identifier (parser);
30320 if (id == error_mark_node)
30322 cp_parser_skip_to_end_of_statement (parser);
30323 cp_parser_consume_semicolon_at_end_of_statement (parser);
30324 return NULL_TREE;
30327 tree attrs = cp_parser_attributes_opt (parser);
30329 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30331 cp_parser_skip_to_end_of_statement (parser);
30332 cp_parser_consume_semicolon_at_end_of_statement (parser);
30333 return error_mark_node;
30336 processing_constraint_expression_sentinel parsing_constraint;
30337 tree init = cp_parser_constraint_expression (parser);
30338 if (init == error_mark_node)
30339 cp_parser_skip_to_end_of_statement (parser);
30341 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
30342 but continue as if it were. */
30343 cp_parser_consume_semicolon_at_end_of_statement (parser);
30345 return finish_concept_definition (id, init, attrs);
30348 // -------------------------------------------------------------------------- //
30349 // Requires Clause
30351 /* Diagnose an expression that should appear in ()'s within a requires-clause
30352 and suggest where to place those parentheses. */
30354 static void
30355 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
30357 error_at (loc, "expression must be enclosed in parentheses");
30360 static void
30361 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
30363 gcc_rich_location richloc (loc);
30364 richloc.add_fixit_insert_before ("(");
30365 richloc.add_fixit_insert_after (")");
30366 error_at (&richloc, "expression must be enclosed in parentheses");
30369 /* Characterizes the likely kind of expression intended by a mis-written
30370 primary constraint. */
30371 enum primary_constraint_error
30373 pce_ok,
30374 pce_maybe_operator,
30375 pce_maybe_postfix
30378 /* Returns true if the token(s) following a primary-expression in a
30379 constraint-logical-* expression would require parentheses. */
30381 static primary_constraint_error
30382 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
30384 cp_token *token = cp_lexer_peek_token (parser->lexer);
30385 switch (token->type)
30387 default:
30388 return pce_ok;
30390 case CPP_EQ:
30392 /* An equal sign may be part of the definition of a function,
30393 and not an assignment operator, when parsing the expression
30394 for a trailing requires-clause. For example:
30396 template<typename T>
30397 struct S {
30398 S() requires C<T> = default;
30401 Don't try to reparse this a binary operator. */
30402 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
30403 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
30404 return pce_ok;
30406 gcc_fallthrough ();
30409 /* Arithmetic operators. */
30410 case CPP_PLUS:
30411 case CPP_MINUS:
30412 case CPP_MULT:
30413 case CPP_DIV:
30414 case CPP_MOD:
30415 /* Bitwise operators. */
30416 case CPP_AND:
30417 case CPP_OR:
30418 case CPP_XOR:
30419 case CPP_RSHIFT:
30420 case CPP_LSHIFT:
30421 /* Relational operators. */
30422 case CPP_EQ_EQ:
30423 case CPP_NOT_EQ:
30424 case CPP_LESS:
30425 case CPP_GREATER:
30426 case CPP_LESS_EQ:
30427 case CPP_GREATER_EQ:
30428 case CPP_SPACESHIP:
30429 /* Pointer-to-member. */
30430 case CPP_DOT_STAR:
30431 case CPP_DEREF_STAR:
30432 /* Assignment operators. */
30433 case CPP_PLUS_EQ:
30434 case CPP_MINUS_EQ:
30435 case CPP_MULT_EQ:
30436 case CPP_DIV_EQ:
30437 case CPP_MOD_EQ:
30438 case CPP_AND_EQ:
30439 case CPP_OR_EQ:
30440 case CPP_XOR_EQ:
30441 case CPP_RSHIFT_EQ:
30442 case CPP_LSHIFT_EQ:
30443 /* Conditional operator */
30444 case CPP_QUERY:
30445 /* Unenclosed binary or conditional operator. */
30446 return pce_maybe_operator;
30448 case CPP_OPEN_PAREN:
30450 /* A primary constraint that precedes the parameter-list of a
30451 lambda expression is followed by an open paren.
30453 []<typename T> requires C (T a, T b) { ... }
30455 Don't try to re-parse this as a postfix expression. */
30456 if (lambda_p)
30457 return pce_ok;
30459 gcc_fallthrough ();
30461 case CPP_OPEN_SQUARE:
30463 /* A primary-constraint-expression followed by a '[[' is not a
30464 postfix expression. */
30465 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
30466 return pce_ok;
30468 gcc_fallthrough ();
30470 case CPP_PLUS_PLUS:
30471 case CPP_MINUS_MINUS:
30472 case CPP_DOT:
30473 /* Unenclosed postfix operator. */
30474 return pce_maybe_postfix;
30476 case CPP_DEREF:
30477 /* A primary constraint that precedes the lambda-declarator of a
30478 lambda expression is followed by trailing return type.
30480 []<typename T> requires C -> void {}
30482 Don't try to re-parse this as a postfix expression in
30483 C++23 and later. In C++20 ( needs to come in between but we
30484 allow it to be omitted with pedwarn. */
30485 if (lambda_p)
30486 return pce_ok;
30487 /* Unenclosed postfix operator. */
30488 return pce_maybe_postfix;
30492 /* Returns true if the next token begins a unary expression, preceded by
30493 an operator or keyword. */
30495 static bool
30496 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
30498 cp_token *token = cp_lexer_peek_token (parser->lexer);
30499 switch (token->type)
30501 case CPP_NOT:
30502 case CPP_PLUS:
30503 case CPP_MINUS:
30504 case CPP_MULT:
30505 case CPP_COMPL:
30506 case CPP_PLUS_PLUS:
30507 case CPP_MINUS_MINUS:
30508 return true;
30510 case CPP_KEYWORD:
30512 switch (token->keyword)
30514 case RID_STATCAST:
30515 case RID_DYNCAST:
30516 case RID_REINTCAST:
30517 case RID_CONSTCAST:
30518 case RID_TYPEID:
30519 case RID_SIZEOF:
30520 case RID_ALIGNOF:
30521 case RID_NOEXCEPT:
30522 case RID_NEW:
30523 case RID_DELETE:
30524 case RID_THROW:
30525 return true;
30527 default:
30528 break;
30532 default:
30533 break;
30536 return false;
30539 /* Parse a primary expression within a constraint. */
30541 static cp_expr
30542 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
30544 /* If this looks like a unary expression, parse it as such, but diagnose
30545 it as ill-formed; it requires parens. */
30546 if (cp_parser_unary_constraint_requires_parens (parser))
30548 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
30549 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
30550 return e;
30553 cp_lexer_save_tokens (parser->lexer);
30554 cp_id_kind idk;
30555 location_t loc = input_location;
30556 cp_expr expr = cp_parser_primary_expression (parser,
30557 /*address_p=*/false,
30558 /*cast_p=*/false,
30559 /*template_arg_p=*/false,
30560 &idk);
30561 expr.maybe_add_location_wrapper ();
30563 primary_constraint_error pce = pce_ok;
30564 if (expr != error_mark_node)
30566 /* The primary-expression could be part of an unenclosed non-logical
30567 compound expression. */
30568 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
30570 if (pce == pce_ok)
30572 cp_lexer_commit_tokens (parser->lexer);
30573 return finish_constraint_primary_expr (expr);
30576 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
30577 error, but return the expression as if it were valid. */
30578 cp_lexer_rollback_tokens (parser->lexer);
30579 cp_parser_parse_tentatively (parser);
30580 if (pce == pce_maybe_operator)
30581 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30582 else
30583 expr = cp_parser_simple_cast_expression (parser);
30584 if (cp_parser_parse_definitely (parser))
30586 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
30587 return expr;
30590 /* Otherwise, something has gone very wrong, and we can't generate a more
30591 meaningful diagnostic or recover. */
30592 cp_parser_diagnose_ungrouped_constraint_plain (loc);
30593 return error_mark_node;
30596 /* Parse a constraint-logical-and-expression.
30598 constraint-logical-and-expression:
30599 primary-expression
30600 constraint-logical-and-expression '&&' primary-expression */
30602 static cp_expr
30603 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
30605 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
30606 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
30608 cp_token *op = cp_lexer_consume_token (parser->lexer);
30609 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
30610 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
30612 return lhs;
30615 /* Parse a constraint-logical-or-expression.
30617 constraint-logical-or-expression:
30618 constraint-logical-and-expression
30619 constraint-logical-or-expression '||' constraint-logical-and-expression */
30621 static cp_expr
30622 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
30624 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
30625 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
30627 cp_token *op = cp_lexer_consume_token (parser->lexer);
30628 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
30629 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
30631 return lhs;
30634 /* Parse the expression after a requires-clause. This has a different grammar
30635 than that in the concepts TS. */
30637 static tree
30638 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
30640 processing_constraint_expression_sentinel parsing_constraint;
30641 ++processing_template_decl;
30642 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
30643 --processing_template_decl;
30644 if (check_for_bare_parameter_packs (expr))
30645 expr = error_mark_node;
30646 return expr;
30649 /* Parse a expression after a requires clause.
30651 constraint-expression:
30652 logical-or-expression
30654 The required logical-or-expression must be a constant expression. Note
30655 that we don't check that the expression is constepxr here. We defer until
30656 we analyze constraints and then, we only check atomic constraints. */
30658 static tree
30659 cp_parser_constraint_expression (cp_parser *parser)
30661 processing_constraint_expression_sentinel parsing_constraint;
30662 ++processing_template_decl;
30663 cp_expr expr = cp_parser_binary_expression (parser, false, true,
30664 PREC_NOT_OPERATOR, NULL);
30665 --processing_template_decl;
30666 if (check_for_bare_parameter_packs (expr))
30667 expr = error_mark_node;
30668 expr.maybe_add_location_wrapper ();
30669 return expr;
30672 /* Optionally parse a requires clause:
30674 requires-clause:
30675 `requires` constraint-logical-or-expression.
30676 [ConceptsTS]
30677 `requires constraint-expression.
30679 LAMBDA_P is true when the requires-clause is parsed before the
30680 parameter-list of a lambda-declarator. */
30682 static tree
30683 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
30685 /* A requires clause is an unevaluated context. */
30686 cp_unevaluated u;
30688 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30689 if (tok->keyword != RID_REQUIRES)
30691 if (!flag_concepts && tok->type == CPP_NAME
30692 && tok->u.value == ridpointers[RID_REQUIRES])
30694 error_at (cp_lexer_peek_token (parser->lexer)->location,
30695 "%<requires%> only available with "
30696 "%<-std=c++20%> or %<-fconcepts%>");
30697 /* Parse and discard the requires-clause. */
30698 cp_lexer_consume_token (parser->lexer);
30699 cp_parser_constraint_expression (parser);
30701 return NULL_TREE;
30704 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
30705 if (tok2->type == CPP_OPEN_BRACE)
30707 /* An opening brace following the start of a requires-clause is
30708 ill-formed; the user likely forgot the second `requires' that
30709 would start a requires-expression. */
30710 gcc_rich_location richloc (tok2->location);
30711 richloc.add_fixit_insert_after (tok->location, " requires");
30712 error_at (&richloc, "missing additional %<requires%> to start "
30713 "a requires-expression");
30714 /* Don't consume the `requires', so that it's reused as the start of a
30715 requires-expression. */
30717 else
30718 cp_lexer_consume_token (parser->lexer);
30720 if (!flag_concepts_ts)
30721 return cp_parser_requires_clause_expression (parser, lambda_p);
30722 else
30723 return cp_parser_constraint_expression (parser);
30726 /*---------------------------------------------------------------------------
30727 Requires expressions
30728 ---------------------------------------------------------------------------*/
30730 /* Parse a requires expression
30732 requirement-expression:
30733 'requires' requirement-parameter-list [opt] requirement-body */
30735 static tree
30736 cp_parser_requires_expression (cp_parser *parser)
30738 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30739 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
30741 /* Avoid committing to outer tentative parse. */
30742 tentative_firewall firewall (parser);
30744 /* This is definitely a requires-expression. */
30745 cp_parser_commit_to_tentative_parse (parser);
30747 tree parms, reqs;
30749 /* Local parameters are delared as variables within the scope
30750 of the expression. They are not visible past the end of
30751 the expression. Expressions within the requires-expression
30752 are unevaluated. */
30753 struct scope_sentinel
30755 scope_sentinel ()
30757 ++cp_unevaluated_operand;
30758 begin_scope (sk_function_parms, NULL_TREE);
30759 current_binding_level->requires_expression = true;
30762 ~scope_sentinel ()
30764 pop_bindings_and_leave_scope ();
30765 --cp_unevaluated_operand;
30767 } s;
30769 /* Parse the optional parameter list. */
30770 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30772 parms = cp_parser_requirement_parameter_list (parser);
30773 if (parms == error_mark_node)
30774 return error_mark_node;
30776 else
30777 parms = NULL_TREE;
30779 /* Parse the requirement body. */
30780 ++processing_template_decl;
30781 reqs = cp_parser_requirement_body (parser);
30782 --processing_template_decl;
30783 if (reqs == error_mark_node)
30784 return error_mark_node;
30787 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
30788 the parm chain. */
30789 grokparms (parms, &parms);
30790 loc = make_location (loc, loc, parser->lexer);
30791 tree expr = finish_requires_expr (loc, parms, reqs);
30792 if (!processing_template_decl)
30794 /* Perform semantic processing now to diagnose any invalid types and
30795 expressions. */
30796 int saved_errorcount = errorcount;
30797 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
30798 if (errorcount > saved_errorcount)
30799 return error_mark_node;
30801 return expr;
30804 /* Parse a parameterized requirement.
30806 requirement-parameter-list:
30807 '(' parameter-declaration-clause ')' */
30809 static tree
30810 cp_parser_requirement_parameter_list (cp_parser *parser)
30812 matching_parens parens;
30813 if (!parens.require_open (parser))
30814 return error_mark_node;
30816 tree parms = (cp_parser_parameter_declaration_clause
30817 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
30819 if (!parens.require_close (parser))
30820 return error_mark_node;
30822 /* Modify the declared parameters by removing their context
30823 so they don't refer to the enclosing scope and explicitly
30824 indicating that they are constraint variables. */
30825 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
30827 if (parm == void_list_node || parm == explicit_void_list_node)
30828 break;
30829 tree decl = TREE_VALUE (parm);
30830 if (decl != error_mark_node)
30832 DECL_CONTEXT (decl) = NULL_TREE;
30833 CONSTRAINT_VAR_P (decl) = true;
30837 return parms;
30840 /* Parse the body of a requirement.
30842 requirement-body:
30843 '{' requirement-list '}' */
30844 static tree
30845 cp_parser_requirement_body (cp_parser *parser)
30847 matching_braces braces;
30848 if (!braces.require_open (parser))
30849 return error_mark_node;
30851 tree reqs = cp_parser_requirement_seq (parser);
30853 if (!braces.require_close (parser))
30854 return error_mark_node;
30856 return reqs;
30859 /* Parse a sequence of requirements.
30861 requirement-seq:
30862 requirement
30863 requirement-seq requirement */
30865 static tree
30866 cp_parser_requirement_seq (cp_parser *parser)
30868 tree result = NULL_TREE;
30871 tree req = cp_parser_requirement (parser);
30872 if (req != error_mark_node)
30873 result = tree_cons (NULL_TREE, req, result);
30875 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
30876 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
30878 /* If there are no valid requirements, this is not a valid expression. */
30879 if (!result)
30880 return error_mark_node;
30882 /* Reverse the order of requirements so they are analyzed in order. */
30883 return nreverse (result);
30886 /* Parse a syntactic requirement or type requirement.
30888 requirement:
30889 simple-requirement
30890 compound-requirement
30891 type-requirement
30892 nested-requirement */
30894 static tree
30895 cp_parser_requirement (cp_parser *parser)
30897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30898 return cp_parser_compound_requirement (parser);
30899 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
30901 /* It's probably a type-requirement. */
30902 cp_parser_parse_tentatively (parser);
30903 tree req = cp_parser_type_requirement (parser);
30904 if (cp_parser_parse_definitely (parser))
30905 return req;
30906 /* No, maybe it's something like typename T::type(); */
30907 cp_parser_parse_tentatively (parser);
30908 req = cp_parser_simple_requirement (parser);
30909 if (cp_parser_parse_definitely (parser))
30910 return req;
30911 /* Non-tentative for the error. */
30912 return cp_parser_type_requirement (parser);
30914 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
30915 return cp_parser_nested_requirement (parser);
30916 else
30917 return cp_parser_simple_requirement (parser);
30920 /* Parse a simple requirement.
30922 simple-requirement:
30923 expression ';' */
30925 static tree
30926 cp_parser_simple_requirement (cp_parser *parser)
30928 location_t start = cp_lexer_peek_token (parser->lexer)->location;
30929 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
30930 if (expr == error_mark_node)
30931 cp_parser_skip_to_end_of_statement (parser);
30933 cp_parser_consume_semicolon_at_end_of_statement (parser);
30935 if (!expr || expr == error_mark_node)
30936 return error_mark_node;
30938 /* Sometimes we don't get locations, so use the cached token location
30939 as a reasonable approximation. */
30940 if (expr.get_location() == UNKNOWN_LOCATION)
30941 expr.set_location (start);
30943 for (tree t = expr; ; )
30945 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
30946 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
30948 t = TREE_OPERAND (t, 0);
30949 continue;
30951 if (concept_check_p (t))
30953 gcc_rich_location richloc (get_start (start));
30954 richloc.add_fixit_insert_before (start, "requires ");
30955 warning_at (&richloc, OPT_Wmissing_requires, "testing "
30956 "if a concept-id is a valid expression; add "
30957 "%<requires%> to check satisfaction");
30959 break;
30962 return finish_simple_requirement (expr.get_location (), expr);
30965 /* Parse a type requirement
30967 type-requirement
30968 nested-name-specifier [opt] required-type-name ';'
30970 required-type-name:
30971 type-name
30972 'template' [opt] simple-template-id */
30974 static tree
30975 cp_parser_type_requirement (cp_parser *parser)
30977 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
30978 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30980 // Save the scope before parsing name specifiers.
30981 tree saved_scope = parser->scope;
30982 tree saved_object_scope = parser->object_scope;
30983 tree saved_qualifying_scope = parser->qualifying_scope;
30984 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
30985 cp_parser_nested_name_specifier_opt (parser,
30986 /*typename_keyword_p=*/true,
30987 /*check_dependency_p=*/false,
30988 /*type_p=*/true,
30989 /*is_declaration=*/false);
30991 tree type;
30992 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30994 cp_lexer_consume_token (parser->lexer);
30995 type = cp_parser_template_id (parser,
30996 /*template_keyword_p=*/true,
30997 /*check_dependency=*/false,
30998 /*tag_type=*/none_type,
30999 /*is_declaration=*/false);
31000 type = make_typename_type (parser->scope, type, typename_type,
31001 /*complain=*/tf_error);
31003 else
31004 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
31006 if (TREE_CODE (type) == TYPE_DECL)
31007 type = TREE_TYPE (type);
31009 parser->scope = saved_scope;
31010 parser->object_scope = saved_object_scope;
31011 parser->qualifying_scope = saved_qualifying_scope;
31013 if (type == error_mark_node)
31014 cp_parser_skip_to_end_of_statement (parser);
31016 cp_parser_consume_semicolon_at_end_of_statement (parser);
31018 if (type == error_mark_node)
31019 return error_mark_node;
31021 loc = make_location (loc, start_tok->location, parser->lexer);
31022 return finish_type_requirement (loc, type);
31025 /* Parse a compound requirement
31027 compound-requirement:
31028 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
31030 static tree
31031 cp_parser_compound_requirement (cp_parser *parser)
31033 /* Parse an expression enclosed in '{ }'s. */
31034 matching_braces braces;
31035 if (!braces.require_open (parser))
31036 return error_mark_node;
31038 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
31040 tree expr = cp_parser_expression (parser, NULL, false, false);
31041 if (expr == error_mark_node)
31042 cp_parser_skip_to_closing_brace (parser);
31044 if (!braces.require_close (parser))
31046 cp_parser_skip_to_end_of_statement (parser);
31047 cp_parser_consume_semicolon_at_end_of_statement (parser);
31048 return error_mark_node;
31051 /* If the expression was invalid, skip the remainder of the requirement. */
31052 if (!expr || expr == error_mark_node)
31054 cp_parser_skip_to_end_of_statement (parser);
31055 cp_parser_consume_semicolon_at_end_of_statement (parser);
31056 return error_mark_node;
31059 /* Parse the optional noexcept. */
31060 bool noexcept_p = false;
31061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
31063 cp_lexer_consume_token (parser->lexer);
31064 noexcept_p = true;
31067 /* Parse the optional trailing return type. */
31068 tree type = NULL_TREE;
31069 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
31071 cp_lexer_consume_token (parser->lexer);
31072 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31074 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
31075 parser->in_result_type_constraint_p = true;
31076 /* C++20 allows either a type-id or a type-constraint. Parsing
31077 a type-id will subsume the parsing for a type-constraint but
31078 allow for more syntactic forms (e.g., const C<T>*). */
31079 type = cp_parser_trailing_type_id (parser);
31080 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
31081 if (type == error_mark_node)
31082 return error_mark_node;
31084 location_t type_loc = make_location (tok->location, tok->location,
31085 parser->lexer);
31087 /* Check that we haven't written something like 'const C<T>*'. */
31088 if (type_uses_auto (type))
31090 if (!is_auto (type))
31092 error_at (type_loc,
31093 "result type is not a plain type-constraint");
31094 cp_parser_consume_semicolon_at_end_of_statement (parser);
31095 return error_mark_node;
31098 else if (!flag_concepts_ts)
31099 /* P1452R2 removed the trailing-return-type option. */
31100 error_at (type_loc,
31101 "return-type-requirement is not a type-constraint");
31104 location_t loc = make_location (expr_token->location,
31105 braces.open_location (),
31106 parser->lexer);
31108 cp_parser_consume_semicolon_at_end_of_statement (parser);
31110 if (expr == error_mark_node || type == error_mark_node)
31111 return error_mark_node;
31113 return finish_compound_requirement (loc, expr, type, noexcept_p);
31116 /* Parse a nested requirement. This is the same as a requires clause.
31118 nested-requirement:
31119 requires-clause */
31121 static tree
31122 cp_parser_nested_requirement (cp_parser *parser)
31124 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31125 cp_token *tok = cp_lexer_consume_token (parser->lexer);
31126 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31127 tree req = cp_parser_constraint_expression (parser);
31128 if (req == error_mark_node)
31129 cp_parser_skip_to_end_of_statement (parser);
31130 loc = make_location (loc, tok->location, parser->lexer);
31131 cp_parser_consume_semicolon_at_end_of_statement (parser);
31132 if (req == error_mark_node)
31133 return error_mark_node;
31134 return finish_nested_requirement (loc, req);
31137 /* Support Functions */
31139 /* Return the appropriate prefer_type argument for lookup_name based on
31140 tag_type. */
31142 static inline LOOK_want
31143 prefer_type_arg (tag_types tag_type)
31145 switch (tag_type)
31147 case none_type: return LOOK_want::NORMAL; // No preference.
31148 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
31149 default: return LOOK_want::TYPE; // Type only.
31153 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
31154 NAME should have one of the representations used for an
31155 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
31156 is returned. If PARSER->SCOPE is a dependent type, then a
31157 SCOPE_REF is returned.
31159 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
31160 returned; the name was already resolved when the TEMPLATE_ID_EXPR
31161 was formed. Abstractly, such entities should not be passed to this
31162 function, because they do not need to be looked up, but it is
31163 simpler to check for this special case here, rather than at the
31164 call-sites.
31166 In cases not explicitly covered above, this function returns a
31167 DECL, OVERLOAD, or baselink representing the result of the lookup.
31168 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
31169 is returned.
31171 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
31172 (e.g., "struct") that was used. In that case bindings that do not
31173 refer to types are ignored.
31175 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
31176 ignored.
31178 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
31179 are ignored.
31181 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
31182 types.
31184 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
31185 TREE_LIST of candidates if name-lookup results in an ambiguity, and
31186 NULL_TREE otherwise. */
31188 static cp_expr
31189 cp_parser_lookup_name (cp_parser *parser, tree name,
31190 enum tag_types tag_type,
31191 bool is_template,
31192 bool is_namespace,
31193 bool check_dependency,
31194 tree *ambiguous_decls,
31195 location_t name_location)
31197 tree decl;
31198 tree object_type = parser->context->object_type;
31200 /* Assume that the lookup will be unambiguous. */
31201 if (ambiguous_decls)
31202 *ambiguous_decls = NULL_TREE;
31204 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
31205 no longer valid. Note that if we are parsing tentatively, and
31206 the parse fails, OBJECT_TYPE will be automatically restored. */
31207 parser->context->object_type = NULL_TREE;
31209 if (name == error_mark_node)
31210 return error_mark_node;
31212 /* A template-id has already been resolved; there is no lookup to
31213 do. */
31214 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
31215 return name;
31216 if (BASELINK_P (name))
31218 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
31219 == TEMPLATE_ID_EXPR);
31220 return name;
31223 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
31224 it should already have been checked to make sure that the name
31225 used matches the type being destroyed. */
31226 if (TREE_CODE (name) == BIT_NOT_EXPR)
31228 tree type;
31230 /* Figure out to which type this destructor applies. */
31231 if (parser->scope)
31232 type = parser->scope;
31233 else if (object_type)
31234 type = object_type;
31235 else
31236 type = current_class_type;
31237 /* If that's not a class type, there is no destructor. */
31238 if (!type || !CLASS_TYPE_P (type))
31239 return error_mark_node;
31241 /* In a non-static member function, check implicit this->. */
31242 if (current_class_ref)
31243 return lookup_destructor (current_class_ref, parser->scope, name,
31244 tf_warning_or_error);
31246 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
31247 lazily_declare_fn (sfk_destructor, type);
31249 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
31250 return dtor;
31252 return error_mark_node;
31255 /* By this point, the NAME should be an ordinary identifier. If
31256 the id-expression was a qualified name, the qualifying scope is
31257 stored in PARSER->SCOPE at this point. */
31258 gcc_assert (identifier_p (name));
31260 /* Perform the lookup. */
31261 if (parser->scope)
31263 bool dependent_p;
31265 if (parser->scope == error_mark_node)
31266 return error_mark_node;
31268 /* If the SCOPE is dependent, the lookup must be deferred until
31269 the template is instantiated -- unless we are explicitly
31270 looking up names in uninstantiated templates. Even then, we
31271 cannot look up the name if the scope is not a class type; it
31272 might, for example, be a template type parameter. */
31273 dependent_p = (TYPE_P (parser->scope)
31274 && dependent_scope_p (parser->scope));
31275 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
31276 && dependent_p)
31277 /* Defer lookup. */
31278 decl = error_mark_node;
31279 else
31281 tree pushed_scope = NULL_TREE;
31283 /* If PARSER->SCOPE is a dependent type, then it must be a
31284 class type, and we must not be checking dependencies;
31285 otherwise, we would have processed this lookup above. So
31286 that PARSER->SCOPE is not considered a dependent base by
31287 lookup_member, we must enter the scope here. */
31288 if (dependent_p)
31289 pushed_scope = push_scope (parser->scope);
31291 /* If the PARSER->SCOPE is a template specialization, it
31292 may be instantiated during name lookup. In that case,
31293 errors may be issued. Even if we rollback the current
31294 tentative parse, those errors are valid. */
31295 decl = lookup_qualified_name (parser->scope, name,
31296 prefer_type_arg (tag_type),
31297 /*complain=*/true);
31299 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
31300 lookup result and the nested-name-specifier nominates a class C:
31301 * if the name specified after the nested-name-specifier, when
31302 looked up in C, is the injected-class-name of C (Clause 9), or
31303 * if the name specified after the nested-name-specifier is the
31304 same as the identifier or the simple-template-id's template-
31305 name in the last component of the nested-name-specifier,
31306 the name is instead considered to name the constructor of
31307 class C. [ Note: for example, the constructor is not an
31308 acceptable lookup result in an elaborated-type-specifier so
31309 the constructor would not be used in place of the
31310 injected-class-name. --end note ] Such a constructor name
31311 shall be used only in the declarator-id of a declaration that
31312 names a constructor or in a using-declaration. */
31313 if (tag_type == none_type
31314 && DECL_SELF_REFERENCE_P (decl)
31315 && same_type_p (DECL_CONTEXT (decl), parser->scope))
31316 decl = lookup_qualified_name (parser->scope, ctor_identifier,
31317 prefer_type_arg (tag_type),
31318 /*complain=*/true);
31320 if (pushed_scope)
31321 pop_scope (pushed_scope);
31324 /* If the scope is a dependent type and either we deferred lookup or
31325 we did lookup but didn't find the name, rememeber the name. */
31326 if (decl == error_mark_node && TYPE_P (parser->scope)
31327 && dependent_type_p (parser->scope))
31329 if (tag_type)
31331 tree type;
31333 /* The resolution to Core Issue 180 says that `struct
31334 A::B' should be considered a type-name, even if `A'
31335 is dependent. */
31336 type = make_typename_type (parser->scope, name, tag_type,
31337 /*complain=*/tf_error);
31338 if (type != error_mark_node)
31339 decl = TYPE_NAME (type);
31341 else if (is_template
31342 && (cp_parser_next_token_ends_template_argument_p (parser)
31343 || cp_lexer_next_token_is (parser->lexer,
31344 CPP_CLOSE_PAREN)))
31345 decl = make_unbound_class_template (parser->scope,
31346 name, NULL_TREE,
31347 /*complain=*/tf_error);
31348 else
31349 decl = build_qualified_name (/*type=*/NULL_TREE,
31350 parser->scope, name,
31351 is_template);
31353 parser->qualifying_scope = parser->scope;
31354 parser->object_scope = NULL_TREE;
31356 else if (object_type)
31358 bool dep = dependent_scope_p (object_type);
31360 /* Look up the name in the scope of the OBJECT_TYPE, unless the
31361 OBJECT_TYPE is not a class. */
31362 if (!dep && CLASS_TYPE_P (object_type))
31363 /* If the OBJECT_TYPE is a template specialization, it may
31364 be instantiated during name lookup. In that case, errors
31365 may be issued. Even if we rollback the current tentative
31366 parse, those errors are valid. */
31367 decl = lookup_member (object_type,
31368 name,
31369 /*protect=*/0,
31370 /*prefer_type=*/tag_type != none_type,
31371 tf_warning_or_error);
31372 else
31373 decl = NULL_TREE;
31375 if (!decl)
31376 /* Look it up in the enclosing context. DR 141: When looking for a
31377 template-name after -> or ., only consider class templates. */
31378 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31379 /* DR 141: When looking in the
31380 current enclosing context for a
31381 template-name after -> or ., only
31382 consider class templates. */
31383 : is_template ? LOOK_want::TYPE
31384 : prefer_type_arg (tag_type));
31386 /* If we did unqualified lookup of a dependent member-qualified name and
31387 found something, do we want to use it? P1787 clarified that we need
31388 to look in the object scope first even if it's dependent, but for now
31389 let's still use it in some cases.
31390 FIXME remember unqualified lookup result to use if member lookup fails
31391 at instantiation time. */
31392 if (decl && dep && is_template)
31394 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
31395 /* Only use the unqualified class template lookup if we're actually
31396 looking at a template arg list. */
31397 if (!cp_parser_skip_entire_template_parameter_list (parser))
31398 decl = NULL_TREE;
31401 /* If we know we're looking for a type (e.g. A in p->A::x),
31402 mock up a typename. */
31403 if (!decl && object_type && tag_type != none_type
31404 && dependentish_scope_p (object_type))
31406 tree type = build_typename_type (object_type, name, name,
31407 typename_type);
31408 decl = TYPE_NAME (type);
31411 parser->object_scope = object_type;
31412 parser->qualifying_scope = NULL_TREE;
31414 else
31416 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31417 : prefer_type_arg (tag_type));
31418 parser->qualifying_scope = NULL_TREE;
31419 parser->object_scope = NULL_TREE;
31422 /* If the lookup failed, let our caller know. */
31423 if (!decl || decl == error_mark_node)
31424 return error_mark_node;
31426 /* If we have resolved the name of a member declaration, check to
31427 see if the declaration is accessible. When the name resolves to
31428 set of overloaded functions, accessibility is checked when
31429 overload resolution is done. If we have a TREE_LIST, then the lookup
31430 is either ambiguous or it found multiple injected-class-names, the
31431 accessibility of which is trivially satisfied.
31433 During an explicit instantiation, access is not checked at all,
31434 as per [temp.explicit]. */
31435 if (DECL_P (decl))
31436 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
31437 tf_warning_or_error);
31439 /* Pull out the template from an injected-class-name (or multiple). */
31440 if (is_template)
31441 decl = maybe_get_template_decl_from_type_decl (decl);
31443 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
31444 if (TREE_CODE (decl) == TREE_LIST)
31446 if (ambiguous_decls)
31447 *ambiguous_decls = decl;
31448 /* The error message we have to print is too complicated for
31449 cp_parser_error, so we incorporate its actions directly. */
31450 if (!cp_parser_simulate_error (parser))
31452 error_at (name_location, "reference to %qD is ambiguous",
31453 name);
31454 print_candidates (decl);
31456 return error_mark_node;
31459 gcc_assert (DECL_P (decl)
31460 || TREE_CODE (decl) == OVERLOAD
31461 || TREE_CODE (decl) == SCOPE_REF
31462 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
31463 || BASELINK_P (decl));
31465 maybe_record_typedef_use (decl);
31467 return cp_expr (decl, name_location);
31470 /* Like cp_parser_lookup_name, but for use in the typical case where
31471 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
31472 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
31474 static tree
31475 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
31477 return cp_parser_lookup_name (parser, name,
31478 none_type,
31479 /*is_template=*/false,
31480 /*is_namespace=*/false,
31481 /*check_dependency=*/true,
31482 /*ambiguous_decls=*/NULL,
31483 location);
31486 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
31487 the current context, return the TYPE_DECL. If TAG_NAME_P is
31488 true, the DECL indicates the class being defined in a class-head,
31489 or declared in an elaborated-type-specifier.
31491 Otherwise, return DECL. */
31493 static tree
31494 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
31496 /* If the TEMPLATE_DECL is being declared as part of a class-head,
31497 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
31499 struct A {
31500 template <typename T> struct B;
31503 template <typename T> struct A::B {};
31505 Similarly, in an elaborated-type-specifier:
31507 namespace N { struct X{}; }
31509 struct A {
31510 template <typename T> friend struct N::X;
31513 However, if the DECL refers to a class type, and we are in
31514 the scope of the class, then the name lookup automatically
31515 finds the TYPE_DECL created by build_self_reference rather
31516 than a TEMPLATE_DECL. For example, in:
31518 template <class T> struct S {
31519 S s;
31522 there is no need to handle such case. */
31524 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
31525 return DECL_TEMPLATE_RESULT (decl);
31527 return decl;
31530 /* If too many, or too few, template-parameter lists apply to the
31531 declarator, issue an error message. Returns TRUE if all went well,
31532 and FALSE otherwise. */
31534 static bool
31535 cp_parser_check_declarator_template_parameters (cp_parser* parser,
31536 cp_declarator *declarator,
31537 location_t declarator_location)
31539 switch (declarator->kind)
31541 case cdk_id:
31543 unsigned num_templates = 0;
31544 tree scope = declarator->u.id.qualifying_scope;
31545 bool template_id_p = false;
31547 if (scope)
31548 num_templates = num_template_headers_for_class (scope);
31549 else if (TREE_CODE (declarator->u.id.unqualified_name)
31550 == TEMPLATE_ID_EXPR)
31552 /* If the DECLARATOR has the form `X<y>' then it uses one
31553 additional level of template parameters. */
31554 ++num_templates;
31555 template_id_p = true;
31558 return cp_parser_check_template_parameters
31559 (parser, num_templates, template_id_p, declarator_location,
31560 declarator);
31563 case cdk_function:
31564 case cdk_array:
31565 case cdk_pointer:
31566 case cdk_reference:
31567 case cdk_ptrmem:
31568 return (cp_parser_check_declarator_template_parameters
31569 (parser, declarator->declarator, declarator_location));
31571 case cdk_decomp:
31572 case cdk_error:
31573 return true;
31575 default:
31576 gcc_unreachable ();
31578 return false;
31581 /* NUM_TEMPLATES were used in the current declaration. If that is
31582 invalid, return FALSE and issue an error messages. Otherwise,
31583 return TRUE. If DECLARATOR is non-NULL, then we are checking a
31584 declarator and we can print more accurate diagnostics. */
31586 static bool
31587 cp_parser_check_template_parameters (cp_parser* parser,
31588 unsigned num_templates,
31589 bool template_id_p,
31590 location_t location,
31591 cp_declarator *declarator)
31593 /* If there are the same number of template classes and parameter
31594 lists, that's OK. */
31595 if (parser->num_template_parameter_lists == num_templates)
31596 return true;
31597 /* If there are more, but only one more, and the name ends in an identifier,
31598 then we are declaring a primary template. That's OK too. */
31599 if (!template_id_p
31600 && parser->num_template_parameter_lists == num_templates + 1)
31601 return true;
31603 if (cp_parser_simulate_error (parser))
31604 return false;
31606 /* If there are more template classes than parameter lists, we have
31607 something like:
31609 template <class T> void S<T>::R<T>::f (); */
31610 if (parser->num_template_parameter_lists < num_templates)
31612 if (declarator && !current_function_decl)
31613 error_at (location, "specializing member %<%T::%E%> "
31614 "requires %<template<>%> syntax",
31615 declarator->u.id.qualifying_scope,
31616 declarator->u.id.unqualified_name);
31617 else if (declarator)
31618 error_at (location, "invalid declaration of %<%T::%E%>",
31619 declarator->u.id.qualifying_scope,
31620 declarator->u.id.unqualified_name);
31621 else
31622 error_at (location, "too few template-parameter-lists");
31623 return false;
31625 /* Otherwise, there are too many template parameter lists. We have
31626 something like:
31628 template <class T> template <class U> void S::f(); */
31629 error_at (location, "too many template-parameter-lists");
31630 return false;
31633 /* Parse an optional `::' token indicating that the following name is
31634 from the global namespace. If so, PARSER->SCOPE is set to the
31635 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
31636 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
31637 Returns the new value of PARSER->SCOPE, if the `::' token is
31638 present, and NULL_TREE otherwise. */
31640 static tree
31641 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
31643 cp_token *token;
31645 /* Peek at the next token. */
31646 token = cp_lexer_peek_token (parser->lexer);
31647 /* If we're looking at a `::' token then we're starting from the
31648 global namespace, not our current location. */
31649 if (token->type == CPP_SCOPE)
31651 /* Consume the `::' token. */
31652 cp_lexer_consume_token (parser->lexer);
31653 /* Set the SCOPE so that we know where to start the lookup. */
31654 parser->scope = global_namespace;
31655 parser->qualifying_scope = global_namespace;
31656 parser->object_scope = NULL_TREE;
31658 return parser->scope;
31660 else if (!current_scope_valid_p)
31662 parser->scope = NULL_TREE;
31663 parser->qualifying_scope = NULL_TREE;
31664 parser->object_scope = NULL_TREE;
31667 return NULL_TREE;
31670 /* Returns TRUE if the upcoming token sequence is the start of a
31671 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
31672 declarator is preceded by the `friend' specifier. The parser flags FLAGS
31673 is used to control type-specifier parsing. */
31675 static bool
31676 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
31677 bool friend_p)
31679 bool constructor_p;
31680 bool outside_class_specifier_p;
31681 tree nested_name_specifier;
31682 cp_token *next_token;
31684 /* The common case is that this is not a constructor declarator, so
31685 try to avoid doing lots of work if at all possible. It's not
31686 valid declare a constructor at function scope. */
31687 if (parser->in_function_body)
31688 return false;
31689 /* And only certain tokens can begin a constructor declarator. */
31690 next_token = cp_lexer_peek_token (parser->lexer);
31691 if (next_token->type != CPP_NAME
31692 && next_token->type != CPP_SCOPE
31693 && next_token->type != CPP_NESTED_NAME_SPECIFIER
31694 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
31695 declarator-id of a constructor or destructor. */
31696 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
31697 return false;
31699 /* Parse tentatively; we are going to roll back all of the tokens
31700 consumed here. */
31701 cp_parser_parse_tentatively (parser);
31702 /* Assume that we are looking at a constructor declarator. */
31703 constructor_p = true;
31705 /* Look for the optional `::' operator. */
31706 cp_parser_global_scope_opt (parser,
31707 /*current_scope_valid_p=*/false);
31708 /* Look for the nested-name-specifier. */
31709 nested_name_specifier
31710 = (cp_parser_nested_name_specifier_opt (parser,
31711 /*typename_keyword_p=*/false,
31712 /*check_dependency_p=*/false,
31713 /*type_p=*/false,
31714 /*is_declaration=*/false));
31716 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
31717 if (nested_name_specifier
31718 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
31720 tree s = resolve_typename_type (nested_name_specifier,
31721 /*only_current_p=*/false);
31722 if (TREE_CODE (s) != TYPENAME_TYPE)
31723 nested_name_specifier = s;
31726 outside_class_specifier_p = (!at_class_scope_p ()
31727 || !TYPE_BEING_DEFINED (current_class_type)
31728 || friend_p);
31730 /* Outside of a class-specifier, there must be a
31731 nested-name-specifier. Except in C++17 mode, where we
31732 might be declaring a guiding declaration. */
31733 if (!nested_name_specifier && outside_class_specifier_p
31734 && cxx_dialect < cxx17)
31735 constructor_p = false;
31736 else if (nested_name_specifier == error_mark_node)
31737 constructor_p = false;
31739 /* If we have a class scope, this is easy; DR 147 says that S::S always
31740 names the constructor, and no other qualified name could. */
31741 if (constructor_p && nested_name_specifier
31742 && CLASS_TYPE_P (nested_name_specifier))
31744 tree id = cp_parser_unqualified_id (parser,
31745 /*template_keyword_p=*/false,
31746 /*check_dependency_p=*/false,
31747 /*declarator_p=*/true,
31748 /*optional_p=*/false);
31749 if (is_overloaded_fn (id))
31750 id = DECL_NAME (get_first_fn (id));
31751 if (!constructor_name_p (id, nested_name_specifier))
31752 constructor_p = false;
31754 /* If we still think that this might be a constructor-declarator,
31755 look for a class-name. */
31756 else if (constructor_p)
31758 /* If we have:
31760 template <typename T> struct S {
31761 S();
31764 we must recognize that the nested `S' names a class. */
31765 if (cxx_dialect >= cxx17)
31766 cp_parser_parse_tentatively (parser);
31768 tree type_decl;
31769 type_decl = cp_parser_class_name (parser,
31770 /*typename_keyword_p=*/false,
31771 /*template_keyword_p=*/false,
31772 none_type,
31773 /*check_dependency_p=*/false,
31774 /*class_head_p=*/false,
31775 /*is_declaration=*/false);
31777 if (cxx_dialect >= cxx17
31778 && !cp_parser_parse_definitely (parser))
31780 type_decl = NULL_TREE;
31781 tree tmpl = cp_parser_template_name (parser,
31782 /*template_keyword*/false,
31783 /*check_dependency_p*/false,
31784 /*is_declaration*/false,
31785 none_type,
31786 /*is_identifier*/NULL);
31787 if (DECL_CLASS_TEMPLATE_P (tmpl)
31788 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
31789 /* It's a deduction guide, return true. */;
31790 else
31791 cp_parser_simulate_error (parser);
31794 /* If there was no class-name, then this is not a constructor.
31795 Otherwise, if we are in a class-specifier and we aren't
31796 handling a friend declaration, check that its type matches
31797 current_class_type (c++/38313). Note: error_mark_node
31798 is left alone for error recovery purposes. */
31799 constructor_p = (!cp_parser_error_occurred (parser)
31800 && (outside_class_specifier_p
31801 || type_decl == NULL_TREE
31802 || type_decl == error_mark_node
31803 || same_type_p (current_class_type,
31804 TREE_TYPE (type_decl))));
31806 /* If we're still considering a constructor, we have to see a `(',
31807 to begin the parameter-declaration-clause, followed by either a
31808 `)', an `...', or a decl-specifier. We need to check for a
31809 type-specifier to avoid being fooled into thinking that:
31811 S (f) (int);
31813 is a constructor. (It is actually a function named `f' that
31814 takes one parameter (of type `int') and returns a value of type
31815 `S'. */
31816 if (constructor_p
31817 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31818 constructor_p = false;
31820 if (constructor_p
31821 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31822 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
31823 /* A parameter declaration begins with a decl-specifier,
31824 which is either the "attribute" keyword, a storage class
31825 specifier, or (usually) a type-specifier. */
31826 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
31827 /* GNU attributes can actually appear both at the start of
31828 a parameter and parenthesized declarator.
31829 S (__attribute__((unused)) int);
31830 is a constructor, but
31831 S (__attribute__((unused)) foo) (int);
31832 is a function declaration. [[attribute]] can appear in the
31833 first form too, but not in the second form. */
31834 && !cp_next_tokens_can_be_std_attribute_p (parser))
31836 tree type;
31837 tree pushed_scope = NULL_TREE;
31838 unsigned saved_num_template_parameter_lists;
31840 if (cp_parser_allow_gnu_extensions_p (parser)
31841 && cp_next_tokens_can_be_gnu_attribute_p (parser))
31843 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
31844 while (--n)
31845 cp_lexer_consume_token (parser->lexer);
31848 /* Names appearing in the type-specifier should be looked up
31849 in the scope of the class. */
31850 if (current_class_type)
31851 type = NULL_TREE;
31852 else if (type_decl)
31854 type = TREE_TYPE (type_decl);
31855 if (TREE_CODE (type) == TYPENAME_TYPE)
31857 type = resolve_typename_type (type,
31858 /*only_current_p=*/false);
31859 if (TREE_CODE (type) == TYPENAME_TYPE)
31861 cp_parser_abort_tentative_parse (parser);
31862 return false;
31865 pushed_scope = push_scope (type);
31868 /* Inside the constructor parameter list, surrounding
31869 template-parameter-lists do not apply. */
31870 saved_num_template_parameter_lists
31871 = parser->num_template_parameter_lists;
31872 parser->num_template_parameter_lists = 0;
31874 /* Look for the type-specifier. It's not optional, but its typename
31875 might be. Unless this is a friend declaration; we don't want to
31876 treat
31878 friend S (T::fn)(int);
31880 as a constructor, but with P0634, we might assume a type when
31881 looking for the type-specifier. It is actually a function named
31882 `T::fn' that takes one parameter (of type `int') and returns a
31883 value of type `S'. Constructors can be friends, but they must
31884 use a qualified name.
31886 Parse with an empty set of declaration specifiers since we're
31887 trying to match a decl-specifier-seq of the first parameter.
31888 This must be non-null so that cp_parser_simple_type_specifier
31889 will recognize a constrained placeholder type such as:
31890 'C<int> auto' where C is a type concept. */
31891 cp_decl_specifier_seq ctor_specs;
31892 clear_decl_specs (&ctor_specs);
31893 cp_parser_type_specifier (parser,
31894 (friend_p ? CP_PARSER_FLAGS_NONE
31895 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
31896 /*decl_specs=*/&ctor_specs,
31897 /*is_declarator=*/true,
31898 /*declares_class_or_enum=*/NULL,
31899 /*is_cv_qualifier=*/NULL);
31901 parser->num_template_parameter_lists
31902 = saved_num_template_parameter_lists;
31904 /* Leave the scope of the class. */
31905 if (pushed_scope)
31906 pop_scope (pushed_scope);
31908 constructor_p = !cp_parser_error_occurred (parser);
31912 /* We did not really want to consume any tokens. */
31913 cp_parser_abort_tentative_parse (parser);
31915 return constructor_p;
31918 /* Parse the definition of the function given by the DECL_SPECIFIERS,
31919 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
31920 they must be performed once we are in the scope of the function.
31922 Returns the function defined. */
31924 static tree
31925 cp_parser_function_definition_from_specifiers_and_declarator
31926 (cp_parser* parser,
31927 cp_decl_specifier_seq *decl_specifiers,
31928 tree attributes,
31929 const cp_declarator *declarator)
31931 tree fn;
31932 bool success_p;
31934 /* Begin the function-definition. */
31935 success_p = start_function (decl_specifiers, declarator, attributes);
31937 /* The things we're about to see are not directly qualified by any
31938 template headers we've seen thus far. */
31939 reset_specialization ();
31941 /* If there were names looked up in the decl-specifier-seq that we
31942 did not check, check them now. We must wait until we are in the
31943 scope of the function to perform the checks, since the function
31944 might be a friend. */
31945 perform_deferred_access_checks (tf_warning_or_error);
31947 if (success_p)
31949 cp_finalize_omp_declare_simd (parser, current_function_decl);
31950 parser->omp_declare_simd = NULL;
31951 cp_finalize_oacc_routine (parser, current_function_decl, true);
31952 parser->oacc_routine = NULL;
31955 if (!success_p)
31957 /* Skip the entire function. */
31958 cp_parser_skip_to_end_of_block_or_statement (parser);
31959 fn = error_mark_node;
31961 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
31963 /* Seen already, skip it. An error message has already been output. */
31964 cp_parser_skip_to_end_of_block_or_statement (parser);
31965 fn = current_function_decl;
31966 current_function_decl = NULL_TREE;
31967 /* If this is a function from a class, pop the nested class. */
31968 if (current_class_name)
31969 pop_nested_class ();
31971 else
31973 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
31974 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
31975 fn = cp_parser_function_definition_after_declarator (parser,
31976 /*inline_p=*/false);
31979 return fn;
31982 /* Parse the part of a function-definition that follows the
31983 declarator. INLINE_P is TRUE iff this function is an inline
31984 function defined within a class-specifier.
31986 Returns the function defined. */
31988 static tree
31989 cp_parser_function_definition_after_declarator (cp_parser* parser,
31990 bool inline_p)
31992 tree fn;
31993 bool saved_in_unbraced_linkage_specification_p;
31994 bool saved_in_function_body;
31995 unsigned saved_num_template_parameter_lists;
31996 cp_token *token;
31997 bool fully_implicit_function_template_p
31998 = parser->fully_implicit_function_template_p;
31999 parser->fully_implicit_function_template_p = false;
32000 tree implicit_template_parms
32001 = parser->implicit_template_parms;
32002 parser->implicit_template_parms = 0;
32003 cp_binding_level* implicit_template_scope
32004 = parser->implicit_template_scope;
32005 parser->implicit_template_scope = 0;
32007 saved_in_function_body = parser->in_function_body;
32008 parser->in_function_body = true;
32009 /* If the next token is `return', then the code may be trying to
32010 make use of the "named return value" extension that G++ used to
32011 support. */
32012 token = cp_lexer_peek_token (parser->lexer);
32013 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
32015 /* Consume the `return' keyword. */
32016 cp_lexer_consume_token (parser->lexer);
32017 /* Look for the identifier that indicates what value is to be
32018 returned. */
32019 cp_parser_identifier (parser);
32020 /* Issue an error message. */
32021 error_at (token->location,
32022 "named return values are no longer supported");
32023 /* Skip tokens until we reach the start of the function body. */
32024 while (true)
32026 cp_token *token = cp_lexer_peek_token (parser->lexer);
32027 if (token->type == CPP_OPEN_BRACE
32028 || token->type == CPP_EOF
32029 || token->type == CPP_PRAGMA_EOL)
32030 break;
32031 cp_lexer_consume_token (parser->lexer);
32034 /* The `extern' in `extern "C" void f () { ... }' does not apply to
32035 anything declared inside `f'. */
32036 saved_in_unbraced_linkage_specification_p
32037 = parser->in_unbraced_linkage_specification_p;
32038 parser->in_unbraced_linkage_specification_p = false;
32039 /* Inside the function, surrounding template-parameter-lists do not
32040 apply. */
32041 saved_num_template_parameter_lists
32042 = parser->num_template_parameter_lists;
32043 parser->num_template_parameter_lists = 0;
32045 /* If the next token is `try', `__transaction_atomic', or
32046 `__transaction_relaxed`, then we are looking at either function-try-block
32047 or function-transaction-block. Note that all of these include the
32048 function-body. */
32049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
32050 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
32051 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32052 RID_TRANSACTION_RELAXED))
32053 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
32054 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32055 cp_parser_function_try_block (parser);
32056 else
32057 cp_parser_ctor_initializer_opt_and_function_body
32058 (parser, /*in_function_try_block=*/false);
32060 /* Finish the function. */
32061 fn = finish_function (inline_p);
32063 if (modules_p ()
32064 && !inline_p
32065 && TYPE_P (DECL_CONTEXT (fn))
32066 && (DECL_DECLARED_INLINE_P (fn)
32067 || processing_template_decl))
32068 set_defining_module (fn);
32070 /* Generate code for it, if necessary. */
32071 expand_or_defer_fn (fn);
32073 /* Restore the saved values. */
32074 parser->in_unbraced_linkage_specification_p
32075 = saved_in_unbraced_linkage_specification_p;
32076 parser->num_template_parameter_lists
32077 = saved_num_template_parameter_lists;
32078 parser->in_function_body = saved_in_function_body;
32080 parser->fully_implicit_function_template_p
32081 = fully_implicit_function_template_p;
32082 parser->implicit_template_parms
32083 = implicit_template_parms;
32084 parser->implicit_template_scope
32085 = implicit_template_scope;
32087 if (parser->fully_implicit_function_template_p)
32088 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
32090 return fn;
32093 /* Parse a template-declaration body (following argument list). */
32095 static void
32096 cp_parser_template_declaration_after_parameters (cp_parser* parser,
32097 tree parameter_list,
32098 bool member_p)
32100 tree decl = NULL_TREE;
32101 bool friend_p = false;
32103 /* We just processed one more parameter list. */
32104 ++parser->num_template_parameter_lists;
32106 /* Get the deferred access checks from the parameter list. These
32107 will be checked once we know what is being declared, as for a
32108 member template the checks must be performed in the scope of the
32109 class containing the member. */
32110 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
32112 /* Tentatively parse for a new template parameter list, which can either be
32113 the template keyword or a template introduction. */
32114 if (cp_parser_template_declaration_after_export (parser, member_p))
32115 /* OK */;
32116 else if (cxx_dialect >= cxx11
32117 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
32118 decl = cp_parser_alias_declaration (parser);
32119 else if (flag_concepts
32120 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
32121 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32122 /* -fconcept-ts 'concept bool' syntax is handled below, in
32123 cp_parser_single_declaration. */
32124 decl = cp_parser_concept_definition (parser);
32125 else
32127 cp_token *token = cp_lexer_peek_token (parser->lexer);
32128 decl = cp_parser_single_declaration (parser,
32129 checks,
32130 member_p,
32131 /*explicit_specialization_p=*/false,
32132 &friend_p);
32134 /* If this is a member template declaration, let the front
32135 end know. */
32136 if (member_p && !friend_p && decl)
32138 if (TREE_CODE (decl) == TYPE_DECL)
32139 cp_parser_check_access_in_redeclaration (decl, token->location);
32141 decl = finish_member_template_decl (decl);
32143 else if (friend_p && decl
32144 && DECL_DECLARES_TYPE_P (decl))
32145 make_friend_class (current_class_type, TREE_TYPE (decl),
32146 /*complain=*/true);
32148 /* We are done with the current parameter list. */
32149 --parser->num_template_parameter_lists;
32151 pop_deferring_access_checks ();
32153 /* Finish up. */
32154 finish_template_decl (parameter_list);
32156 /* Check the template arguments for a literal operator template. */
32157 if (decl
32158 && DECL_DECLARES_FUNCTION_P (decl)
32159 && UDLIT_OPER_P (DECL_NAME (decl)))
32161 bool ok = true;
32162 if (parameter_list == NULL_TREE)
32163 ok = false;
32164 else
32166 int num_parms = TREE_VEC_LENGTH (parameter_list);
32167 if (num_parms == 1)
32169 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
32170 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32171 if (TREE_CODE (parm) != PARM_DECL)
32172 ok = false;
32173 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
32174 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32175 /* OK, C++20 string literal operator template. We don't need
32176 to warn in lower dialects here because we will have already
32177 warned about the template parameter. */;
32178 else if (TREE_TYPE (parm) != char_type_node
32179 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32180 ok = false;
32182 else if (num_parms == 2 && cxx_dialect >= cxx14)
32184 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
32185 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
32186 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
32187 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32188 if (TREE_CODE (parm) != PARM_DECL
32189 || TREE_TYPE (parm) != TREE_TYPE (type)
32190 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32191 ok = false;
32192 else
32193 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
32194 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
32195 "ISO C++ did not adopt string literal operator templa"
32196 "tes taking an argument pack of characters");
32198 else
32199 ok = false;
32201 if (!ok)
32203 if (cxx_dialect > cxx17)
32204 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32205 "template %qD has invalid parameter list; expected "
32206 "non-type template parameter pack %<<char...>%> or "
32207 "single non-type parameter of class type",
32208 decl);
32209 else
32210 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32211 "template %qD has invalid parameter list; expected "
32212 "non-type template parameter pack %<<char...>%>",
32213 decl);
32217 /* Register member declarations. */
32218 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
32219 finish_member_declaration (decl);
32220 /* If DECL is a function template, we must return to parse it later.
32221 (Even though there is no definition, there might be default
32222 arguments that need handling.) */
32223 if (member_p && decl
32224 && DECL_DECLARES_FUNCTION_P (decl))
32225 vec_safe_push (unparsed_funs_with_definitions, decl);
32228 /* Parse a template introduction header for a template-declaration. Returns
32229 false if tentative parse fails. */
32231 static bool
32232 cp_parser_template_introduction (cp_parser* parser, bool member_p)
32234 cp_parser_parse_tentatively (parser);
32236 tree saved_scope = parser->scope;
32237 tree saved_object_scope = parser->object_scope;
32238 tree saved_qualifying_scope = parser->qualifying_scope;
32239 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32241 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
32243 /* In classes don't parse valid unnamed bitfields as invalid
32244 template introductions. */
32245 if (member_p)
32246 parser->colon_corrects_to_scope_p = false;
32248 /* Look for the optional `::' operator. */
32249 cp_parser_global_scope_opt (parser,
32250 /*current_scope_valid_p=*/false);
32251 /* Look for the nested-name-specifier. */
32252 cp_parser_nested_name_specifier_opt (parser,
32253 /*typename_keyword_p=*/false,
32254 /*check_dependency_p=*/true,
32255 /*type_p=*/false,
32256 /*is_declaration=*/false);
32258 cp_token *token = cp_lexer_peek_token (parser->lexer);
32259 tree concept_name = cp_parser_identifier (parser);
32261 /* Look up the concept for which we will be matching
32262 template parameters. */
32263 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
32264 token->location);
32265 parser->scope = saved_scope;
32266 parser->object_scope = saved_object_scope;
32267 parser->qualifying_scope = saved_qualifying_scope;
32268 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32270 if (concept_name == error_mark_node
32271 || (seen_error () && !concept_definition_p (tmpl_decl)))
32272 cp_parser_simulate_error (parser);
32274 /* Look for opening brace for introduction. */
32275 matching_braces braces;
32276 braces.require_open (parser);
32277 location_t open_loc = input_location;
32279 if (!cp_parser_parse_definitely (parser))
32280 return false;
32282 push_deferring_access_checks (dk_deferred);
32284 /* Build vector of placeholder parameters and grab
32285 matching identifiers. */
32286 tree introduction_list = cp_parser_introduction_list (parser);
32288 /* Look for closing brace for introduction. */
32289 if (!braces.require_close (parser))
32290 return true;
32292 /* The introduction-list shall not be empty. */
32293 int nargs = TREE_VEC_LENGTH (introduction_list);
32294 if (nargs == 0)
32296 /* In cp_parser_introduction_list we have already issued an error. */
32297 return true;
32300 if (tmpl_decl == error_mark_node)
32302 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
32303 token->location);
32304 return true;
32307 /* Build and associate the constraint. */
32308 location_t introduction_loc = make_location (open_loc,
32309 start_token->location,
32310 parser->lexer);
32311 tree parms = finish_template_introduction (tmpl_decl,
32312 introduction_list,
32313 introduction_loc);
32314 if (parms && parms != error_mark_node)
32316 if (!flag_concepts_ts)
32317 pedwarn (introduction_loc, 0, "template-introductions"
32318 " are not part of C++20 concepts; use %qs to enable",
32319 "-fconcepts-ts");
32321 cp_parser_template_declaration_after_parameters (parser, parms,
32322 member_p);
32323 return true;
32326 if (parms == NULL_TREE)
32327 error_at (token->location, "no matching concept for template-introduction");
32329 return true;
32332 /* Parse a normal template-declaration following the template keyword. */
32334 static void
32335 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
32337 tree parameter_list;
32338 bool need_lang_pop;
32339 location_t location = input_location;
32341 /* Look for the `<' token. */
32342 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
32343 return;
32344 if (at_class_scope_p () && current_function_decl)
32346 /* 14.5.2.2 [temp.mem]
32348 A local class shall not have member templates. */
32349 error_at (location,
32350 "invalid declaration of member template in local class");
32351 cp_parser_skip_to_end_of_block_or_statement (parser);
32352 return;
32354 /* [temp]
32356 A template ... shall not have C linkage. */
32357 if (current_lang_name == lang_name_c)
32359 error_at (location, "template with C linkage");
32360 maybe_show_extern_c_location ();
32361 /* Give it C++ linkage to avoid confusing other parts of the
32362 front end. */
32363 push_lang_context (lang_name_cplusplus);
32364 need_lang_pop = true;
32366 else
32367 need_lang_pop = false;
32369 /* We cannot perform access checks on the template parameter
32370 declarations until we know what is being declared, just as we
32371 cannot check the decl-specifier list. */
32372 push_deferring_access_checks (dk_deferred);
32374 /* If the next token is `>', then we have an invalid
32375 specialization. Rather than complain about an invalid template
32376 parameter, issue an error message here. */
32377 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
32379 cp_parser_error (parser, "invalid explicit specialization");
32380 begin_specialization ();
32381 parameter_list = NULL_TREE;
32383 else
32385 /* Parse the template parameters. */
32386 parameter_list = cp_parser_template_parameter_list (parser);
32389 /* Look for the `>'. */
32390 cp_parser_require_end_of_template_parameter_list (parser);
32392 /* Manage template requirements */
32393 if (flag_concepts)
32395 tree reqs = get_shorthand_constraints (current_template_parms);
32396 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
32397 reqs = combine_constraint_expressions (reqs, treqs);
32398 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
32401 cp_parser_template_declaration_after_parameters (parser, parameter_list,
32402 member_p);
32404 /* For the erroneous case of a template with C linkage, we pushed an
32405 implicit C++ linkage scope; exit that scope now. */
32406 if (need_lang_pop)
32407 pop_lang_context ();
32410 /* Parse a template-declaration, assuming that the `export' (and
32411 `extern') keywords, if present, has already been scanned. MEMBER_P
32412 is as for cp_parser_template_declaration. */
32414 static bool
32415 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
32417 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
32419 cp_lexer_consume_token (parser->lexer);
32420 cp_parser_explicit_template_declaration (parser, member_p);
32421 return true;
32423 else if (flag_concepts)
32424 return cp_parser_template_introduction (parser, member_p);
32426 return false;
32429 /* Perform the deferred access checks from a template-parameter-list.
32430 CHECKS is a TREE_LIST of access checks, as returned by
32431 get_deferred_access_checks. */
32433 static void
32434 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
32436 ++processing_template_parmlist;
32437 perform_access_checks (checks, tf_warning_or_error);
32438 --processing_template_parmlist;
32441 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
32442 `function-definition' sequence that follows a template header.
32443 If MEMBER_P is true, this declaration appears in a class scope.
32445 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
32446 *FRIEND_P is set to TRUE iff the declaration is a friend. */
32448 static tree
32449 cp_parser_single_declaration (cp_parser* parser,
32450 vec<deferred_access_check, va_gc> *checks,
32451 bool member_p,
32452 bool explicit_specialization_p,
32453 bool* friend_p)
32455 int declares_class_or_enum;
32456 tree decl = NULL_TREE;
32457 cp_decl_specifier_seq decl_specifiers;
32458 bool function_definition_p = false;
32459 cp_token *decl_spec_token_start;
32461 /* This function is only used when processing a template
32462 declaration. */
32463 gcc_assert (innermost_scope_kind () == sk_template_parms
32464 || innermost_scope_kind () == sk_template_spec);
32466 /* Defer access checks until we know what is being declared. */
32467 push_deferring_access_checks (dk_deferred);
32469 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
32470 alternative. */
32471 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
32472 cp_parser_decl_specifier_seq (parser,
32473 (CP_PARSER_FLAGS_OPTIONAL
32474 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
32475 &decl_specifiers,
32476 &declares_class_or_enum);
32478 cp_omp_declare_simd_data odsd;
32479 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
32480 cp_parser_handle_directive_omp_attributes (parser,
32481 &decl_specifiers.attributes,
32482 &odsd, true);
32484 if (friend_p)
32485 *friend_p = cp_parser_friend_p (&decl_specifiers);
32487 /* There are no template typedefs. */
32488 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
32490 error_at (decl_spec_token_start->location,
32491 "template declaration of %<typedef%>");
32492 decl = error_mark_node;
32495 /* Gather up the access checks that occurred the
32496 decl-specifier-seq. */
32497 stop_deferring_access_checks ();
32499 /* Check for the declaration of a template class. */
32500 if (declares_class_or_enum)
32502 if (cp_parser_declares_only_class_p (parser)
32503 || (declares_class_or_enum & 2))
32505 decl = shadow_tag (&decl_specifiers);
32507 /* In this case:
32509 struct C {
32510 friend template <typename T> struct A<T>::B;
32513 A<T>::B will be represented by a TYPENAME_TYPE, and
32514 therefore not recognized by shadow_tag. */
32515 if (friend_p && *friend_p
32516 && !decl
32517 && decl_specifiers.type
32518 && TYPE_P (decl_specifiers.type))
32519 decl = decl_specifiers.type;
32521 if (decl && decl != error_mark_node)
32522 decl = TYPE_NAME (decl);
32523 else
32524 decl = error_mark_node;
32526 /* If this is a declaration, but not a definition, associate
32527 any constraints with the type declaration. Constraints
32528 are associated with definitions in cp_parser_class_specifier. */
32529 if (declares_class_or_enum == 1)
32530 associate_classtype_constraints (TREE_TYPE (decl));
32532 /* Perform access checks for template parameters. */
32533 cp_parser_perform_template_parameter_access_checks (checks);
32535 /* Give a helpful diagnostic for
32536 template <class T> struct A { } a;
32537 if we aren't already recovering from an error. */
32538 if (!cp_parser_declares_only_class_p (parser)
32539 && !seen_error ())
32541 error_at (cp_lexer_peek_token (parser->lexer)->location,
32542 "a class template declaration must not declare "
32543 "anything else");
32544 cp_parser_skip_to_end_of_block_or_statement (parser);
32545 goto out;
32550 /* Complain about missing 'typename' or other invalid type names. */
32551 if (!decl_specifiers.any_type_specifiers_p
32552 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
32554 /* cp_parser_parse_and_diagnose_invalid_type_name calls
32555 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
32556 the rest of this declaration. */
32557 decl = error_mark_node;
32558 goto out;
32561 /* If it's not a template class, try for a template function. If
32562 the next token is a `;', then this declaration does not declare
32563 anything. But, if there were errors in the decl-specifiers, then
32564 the error might well have come from an attempted class-specifier.
32565 In that case, there's no need to warn about a missing declarator. */
32566 if (!decl
32567 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
32568 || decl_specifiers.type != error_mark_node))
32570 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
32571 /* We don't delay parsing for friends, though CWG 2510 may change
32572 that. */
32573 if (member_p && !(friend_p && *friend_p))
32574 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
32575 decl = cp_parser_init_declarator (parser,
32576 flags,
32577 &decl_specifiers,
32578 checks,
32579 /*function_definition_allowed_p=*/true,
32580 member_p,
32581 declares_class_or_enum,
32582 &function_definition_p,
32583 NULL, NULL, NULL);
32585 /* 7.1.1-1 [dcl.stc]
32587 A storage-class-specifier shall not be specified in an explicit
32588 specialization... */
32589 if (decl
32590 && explicit_specialization_p
32591 && decl_specifiers.storage_class != sc_none)
32593 error_at (decl_spec_token_start->location,
32594 "explicit template specialization cannot have a storage class");
32595 decl = error_mark_node;
32598 if (decl && VAR_P (decl))
32599 check_template_variable (decl);
32602 /* Look for a trailing `;' after the declaration. */
32603 if (!function_definition_p
32604 && (decl == error_mark_node
32605 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
32606 cp_parser_skip_to_end_of_block_or_statement (parser);
32608 out:
32609 pop_deferring_access_checks ();
32611 /* Clear any current qualification; whatever comes next is the start
32612 of something new. */
32613 parser->scope = NULL_TREE;
32614 parser->qualifying_scope = NULL_TREE;
32615 parser->object_scope = NULL_TREE;
32617 cp_finalize_omp_declare_simd (parser, &odsd);
32619 return decl;
32622 /* Parse a cast-expression that is not the operand of a unary "&". */
32624 static cp_expr
32625 cp_parser_simple_cast_expression (cp_parser *parser)
32627 return cp_parser_cast_expression (parser, /*address_p=*/false,
32628 /*cast_p=*/false, /*decltype*/false, NULL);
32631 /* Parse a functional cast to TYPE. Returns an expression
32632 representing the cast. */
32634 static cp_expr
32635 cp_parser_functional_cast (cp_parser* parser, tree type)
32637 vec<tree, va_gc> *vec;
32638 tree expression_list;
32639 cp_expr cast;
32640 bool nonconst_p;
32642 location_t start_loc = input_location;
32644 if (!type)
32645 type = error_mark_node;
32647 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32649 cp_lexer_set_source_position (parser->lexer);
32650 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32651 expression_list = cp_parser_braced_list (parser, &nonconst_p);
32652 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
32653 if (TREE_CODE (type) == TYPE_DECL)
32654 type = TREE_TYPE (type);
32656 cast = finish_compound_literal (type, expression_list,
32657 tf_warning_or_error, fcl_functional);
32658 /* Create a location of the form:
32659 type_name{i, f}
32660 ^~~~~~~~~~~~~~~
32661 with caret == start at the start of the type name,
32662 finishing at the closing brace. */
32663 location_t combined_loc = make_location (start_loc, start_loc,
32664 parser->lexer);
32665 cast.set_location (combined_loc);
32666 return cast;
32670 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
32671 /*cast_p=*/true,
32672 /*allow_expansion_p=*/true,
32673 /*non_constant_p=*/NULL);
32674 if (vec == NULL)
32675 expression_list = error_mark_node;
32676 else
32678 expression_list = build_tree_list_vec (vec);
32679 release_tree_vector (vec);
32682 /* Create a location of the form:
32683 float(i)
32684 ^~~~~~~~
32685 with caret == start at the start of the type name,
32686 finishing at the closing paren. */
32687 location_t combined_loc = make_location (start_loc, start_loc,
32688 parser->lexer);
32689 cast = build_functional_cast (combined_loc, type, expression_list,
32690 tf_warning_or_error);
32692 /* [expr.const]/1: In an integral constant expression "only type
32693 conversions to integral or enumeration type can be used". */
32694 if (TREE_CODE (type) == TYPE_DECL)
32695 type = TREE_TYPE (type);
32696 if (cast != error_mark_node
32697 && !cast_valid_in_integral_constant_expression_p (type)
32698 && cp_parser_non_integral_constant_expression (parser,
32699 NIC_CONSTRUCTOR))
32700 return error_mark_node;
32702 return cast;
32705 /* Save the tokens that make up the body of a member function defined
32706 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
32707 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
32708 specifiers applied to the declaration. Returns the FUNCTION_DECL
32709 for the member function. */
32711 static tree
32712 cp_parser_save_member_function_body (cp_parser* parser,
32713 cp_decl_specifier_seq *decl_specifiers,
32714 cp_declarator *declarator,
32715 tree attributes)
32717 cp_token *first;
32718 cp_token *last;
32719 tree fn;
32720 bool function_try_block = false;
32722 /* Create the FUNCTION_DECL. */
32723 fn = grokmethod (decl_specifiers, declarator, attributes);
32724 cp_finalize_omp_declare_simd (parser, fn);
32725 cp_finalize_oacc_routine (parser, fn, true);
32726 /* If something went badly wrong, bail out now. */
32727 if (fn == error_mark_node)
32729 /* If there's a function-body, skip it. */
32730 if (cp_parser_token_starts_function_definition_p
32731 (cp_lexer_peek_token (parser->lexer)))
32732 cp_parser_skip_to_end_of_block_or_statement (parser);
32733 return error_mark_node;
32736 /* Remember it, if there are default args to post process. */
32737 cp_parser_save_default_args (parser, fn);
32739 /* Save away the tokens that make up the body of the
32740 function. */
32741 first = parser->lexer->next_token;
32743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
32744 cp_lexer_consume_token (parser->lexer);
32745 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32746 RID_TRANSACTION_ATOMIC))
32748 cp_lexer_consume_token (parser->lexer);
32749 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
32750 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
32751 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
32752 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
32753 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
32754 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
32755 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
32757 cp_lexer_consume_token (parser->lexer);
32758 cp_lexer_consume_token (parser->lexer);
32759 cp_lexer_consume_token (parser->lexer);
32760 cp_lexer_consume_token (parser->lexer);
32761 cp_lexer_consume_token (parser->lexer);
32763 else
32764 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
32765 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32767 cp_lexer_consume_token (parser->lexer);
32768 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32769 break;
32773 /* Handle function try blocks. */
32774 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32776 cp_lexer_consume_token (parser->lexer);
32777 function_try_block = true;
32779 /* We can have braced-init-list mem-initializers before the fn body. */
32780 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32782 cp_lexer_consume_token (parser->lexer);
32783 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
32785 /* cache_group will stop after an un-nested { } pair, too. */
32786 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32787 break;
32789 /* variadic mem-inits have ... after the ')'. */
32790 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32791 cp_lexer_consume_token (parser->lexer);
32794 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32795 /* Handle function try blocks. */
32796 if (function_try_block)
32797 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
32798 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32799 last = parser->lexer->next_token;
32801 /* Save away the inline definition; we will process it when the
32802 class is complete. */
32803 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
32804 DECL_PENDING_INLINE_P (fn) = 1;
32806 /* We need to know that this was defined in the class, so that
32807 friend templates are handled correctly. */
32808 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
32810 /* Add FN to the queue of functions to be parsed later. */
32811 vec_safe_push (unparsed_funs_with_definitions, fn);
32813 return fn;
32816 /* Save the tokens that make up the in-class initializer for a non-static
32817 data member. Returns a DEFERRED_PARSE. */
32819 static tree
32820 cp_parser_save_nsdmi (cp_parser* parser)
32822 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
32825 /* Parse a template-argument-list, as well as the trailing ">" (but
32826 not the opening "<"). See cp_parser_template_argument_list for the
32827 return value. */
32829 static tree
32830 cp_parser_enclosed_template_argument_list (cp_parser* parser)
32832 tree arguments;
32833 tree saved_scope;
32834 tree saved_qualifying_scope;
32835 tree saved_object_scope;
32836 bool saved_greater_than_is_operator_p;
32838 /* [temp.names]
32840 When parsing a template-id, the first non-nested `>' is taken as
32841 the end of the template-argument-list rather than a greater-than
32842 operator. */
32843 saved_greater_than_is_operator_p
32844 = parser->greater_than_is_operator_p;
32845 parser->greater_than_is_operator_p = false;
32846 /* Parsing the argument list may modify SCOPE, so we save it
32847 here. */
32848 saved_scope = parser->scope;
32849 saved_qualifying_scope = parser->qualifying_scope;
32850 saved_object_scope = parser->object_scope;
32851 /* We need to evaluate the template arguments, even though this
32852 template-id may be nested within a "sizeof". */
32853 cp_evaluated ev;
32854 /* Parse the template-argument-list itself. */
32855 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
32856 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
32857 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
32858 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
32860 arguments = make_tree_vec (0);
32861 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
32863 else
32864 arguments = cp_parser_template_argument_list (parser);
32865 /* Look for the `>' that ends the template-argument-list. If we find
32866 a '>>' instead, it's probably just a typo. */
32867 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
32869 if (cxx_dialect != cxx98)
32871 /* In C++0x, a `>>' in a template argument list or cast
32872 expression is considered to be two separate `>'
32873 tokens. So, change the current token to a `>', but don't
32874 consume it: it will be consumed later when the outer
32875 template argument list (or cast expression) is parsed.
32876 Note that this replacement of `>' for `>>' is necessary
32877 even if we are parsing tentatively: in the tentative
32878 case, after calling
32879 cp_parser_enclosed_template_argument_list we will always
32880 throw away all of the template arguments and the first
32881 closing `>', either because the template argument list
32882 was erroneous or because we are replacing those tokens
32883 with a CPP_TEMPLATE_ID token. The second `>' (which will
32884 not have been thrown away) is needed either to close an
32885 outer template argument list or to complete a new-style
32886 cast. */
32887 cp_token *token = cp_lexer_peek_token (parser->lexer);
32888 token->type = CPP_GREATER;
32890 else if (!saved_greater_than_is_operator_p)
32892 /* If we're in a nested template argument list, the '>>' has
32893 to be a typo for '> >'. We emit the error message, but we
32894 continue parsing and we push a '>' as next token, so that
32895 the argument list will be parsed correctly. Note that the
32896 global source location is still on the token before the
32897 '>>', so we need to say explicitly where we want it. */
32898 cp_token *token = cp_lexer_peek_token (parser->lexer);
32899 gcc_rich_location richloc (token->location);
32900 richloc.add_fixit_replace ("> >");
32901 error_at (&richloc, "%<>>%> should be %<> >%> "
32902 "within a nested template argument list");
32904 token->type = CPP_GREATER;
32906 else
32908 /* If this is not a nested template argument list, the '>>'
32909 is a typo for '>'. Emit an error message and continue.
32910 Same deal about the token location, but here we can get it
32911 right by consuming the '>>' before issuing the diagnostic. */
32912 cp_token *token = cp_lexer_consume_token (parser->lexer);
32913 error_at (token->location,
32914 "spurious %<>>%>, use %<>%> to terminate "
32915 "a template argument list");
32918 /* Similarly for >>= and >=. */
32919 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
32920 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
32922 cp_token *token = cp_lexer_consume_token (parser->lexer);
32923 gcc_rich_location richloc (token->location);
32924 enum cpp_ttype new_type;
32925 const char *replacement;
32926 if (token->type == CPP_GREATER_EQ)
32928 replacement = "> =";
32929 new_type = CPP_EQ;
32931 else if (!saved_greater_than_is_operator_p)
32933 if (cxx_dialect != cxx98)
32934 replacement = ">> =";
32935 else
32936 replacement = "> > =";
32937 new_type = CPP_GREATER;
32939 else
32941 replacement = "> >=";
32942 new_type = CPP_GREATER_EQ;
32944 richloc.add_fixit_replace (replacement);
32945 error_at (&richloc, "%qs should be %qs to terminate a template "
32946 "argument list",
32947 cpp_type2name (token->type, token->flags), replacement);
32948 token->type = new_type;
32950 else
32951 cp_parser_require_end_of_template_parameter_list (parser);
32952 /* The `>' token might be a greater-than operator again now. */
32953 parser->greater_than_is_operator_p
32954 = saved_greater_than_is_operator_p;
32955 /* Restore the SAVED_SCOPE. */
32956 parser->scope = saved_scope;
32957 parser->qualifying_scope = saved_qualifying_scope;
32958 parser->object_scope = saved_object_scope;
32960 return arguments;
32963 /* MEMBER_FUNCTION is a member function, or a friend. If default
32964 arguments, or the body of the function have not yet been parsed,
32965 parse them now. */
32967 static void
32968 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
32970 auto_timevar tv (TV_PARSE_INMETH);
32972 /* If this member is a template, get the underlying
32973 FUNCTION_DECL. */
32974 if (DECL_FUNCTION_TEMPLATE_P (member_function))
32975 member_function = DECL_TEMPLATE_RESULT (member_function);
32977 /* There should not be any class definitions in progress at this
32978 point; the bodies of members are only parsed outside of all class
32979 definitions. */
32980 gcc_assert (parser->num_classes_being_defined == 0);
32981 /* While we're parsing the member functions we might encounter more
32982 classes. We want to handle them right away, but we don't want
32983 them getting mixed up with functions that are currently in the
32984 queue. */
32985 push_unparsed_function_queues (parser);
32987 /* Make sure that any template parameters are in scope. */
32988 maybe_begin_member_template_processing (member_function);
32990 /* If the body of the function has not yet been parsed, parse it
32991 now. Except if the tokens have been purged (PR c++/39751). */
32992 if (DECL_PENDING_INLINE_P (member_function)
32993 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
32995 tree function_scope;
32996 cp_token_cache *tokens;
32998 /* The function is no longer pending; we are processing it. */
32999 tokens = DECL_PENDING_INLINE_INFO (member_function);
33000 DECL_PENDING_INLINE_INFO (member_function) = NULL;
33001 DECL_PENDING_INLINE_P (member_function) = 0;
33003 /* If this is a local class, enter the scope of the containing
33004 function. */
33005 function_scope = current_function_decl;
33006 if (function_scope)
33007 push_function_context ();
33009 /* Push the body of the function onto the lexer stack. */
33010 cp_parser_push_lexer_for_tokens (parser, tokens);
33012 /* Let the front end know that we going to be defining this
33013 function. */
33014 start_preparsed_function (member_function, NULL_TREE,
33015 SF_PRE_PARSED | SF_INCLASS_INLINE);
33017 /* #pragma omp declare reduction needs special parsing. */
33018 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
33020 parser->lexer->in_pragma = true;
33021 cp_parser_omp_declare_reduction_exprs (member_function, parser);
33022 finish_function (/*inline_p=*/true);
33023 cp_check_omp_declare_reduction (member_function);
33025 else
33026 /* Now, parse the body of the function. */
33027 cp_parser_function_definition_after_declarator (parser,
33028 /*inline_p=*/true);
33030 /* Leave the scope of the containing function. */
33031 if (function_scope)
33032 pop_function_context ();
33033 cp_parser_pop_lexer (parser);
33036 /* Remove any template parameters from the symbol table. */
33037 maybe_end_member_template_processing ();
33039 /* Restore the queue. */
33040 pop_unparsed_function_queues (parser);
33043 /* If DECL contains any default args, remember it on the unparsed
33044 functions queue. */
33046 static void
33047 cp_parser_save_default_args (cp_parser* parser, tree decl)
33049 tree probe;
33051 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
33052 probe;
33053 probe = TREE_CHAIN (probe))
33054 if (TREE_PURPOSE (probe))
33056 cp_default_arg_entry entry = {current_class_type, decl};
33057 vec_safe_push (unparsed_funs_with_default_args, entry);
33058 break;
33061 /* Remember if there is a noexcept-specifier to post process. */
33062 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
33063 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
33064 vec_safe_push (unparsed_noexcepts, decl);
33066 /* Contracts are deferred. */
33067 for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
33068 if (cxx_contract_attribute_p (attr))
33070 vec_safe_push (unparsed_contracts, decl);
33071 break;
33075 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
33076 which is either a FIELD_DECL or PARM_DECL. Parse it and return
33077 the result. For a PARM_DECL, PARMTYPE is the corresponding type
33078 from the parameter-type-list. */
33080 static tree
33081 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
33082 tree default_arg, tree parmtype)
33084 cp_token_cache *tokens;
33085 tree parsed_arg;
33086 bool dummy;
33088 if (default_arg == error_mark_node)
33089 return error_mark_node;
33091 /* Push the saved tokens for the default argument onto the parser's
33092 lexer stack. */
33093 tokens = DEFPARSE_TOKENS (default_arg);
33094 cp_parser_push_lexer_for_tokens (parser, tokens);
33096 start_lambda_scope (decl);
33098 /* Parse the default argument. */
33099 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
33100 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
33101 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33103 finish_lambda_scope ();
33105 if (parsed_arg == error_mark_node)
33106 cp_parser_skip_to_end_of_statement (parser);
33108 if (!processing_template_decl)
33110 /* In a non-template class, check conversions now. In a template,
33111 we'll wait and instantiate these as needed. */
33112 if (TREE_CODE (decl) == PARM_DECL)
33113 parsed_arg = check_default_argument (parmtype, parsed_arg,
33114 tf_warning_or_error);
33115 else if (maybe_reject_flexarray_init (decl, parsed_arg))
33116 parsed_arg = error_mark_node;
33117 else
33118 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
33121 /* If the token stream has not been completely used up, then
33122 there was extra junk after the end of the default
33123 argument. */
33124 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33126 if (TREE_CODE (decl) == PARM_DECL)
33127 cp_parser_error (parser, "expected %<,%>");
33128 else
33129 cp_parser_error (parser, "expected %<;%>");
33132 /* Revert to the main lexer. */
33133 cp_parser_pop_lexer (parser);
33135 return parsed_arg;
33138 /* FIELD is a non-static data member with an initializer which we saved for
33139 later; parse it now. */
33141 static void
33142 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
33144 tree def;
33146 maybe_begin_member_template_processing (field);
33148 push_unparsed_function_queues (parser);
33149 def = cp_parser_late_parse_one_default_arg (parser, field,
33150 DECL_INITIAL (field),
33151 NULL_TREE);
33152 pop_unparsed_function_queues (parser);
33154 maybe_end_member_template_processing ();
33156 DECL_INITIAL (field) = def;
33159 /* FN is a FUNCTION_DECL which may contains a parameter with an
33160 unparsed DEFERRED_PARSE. Parse the default args now. This function
33161 assumes that the current scope is the scope in which the default
33162 argument should be processed. */
33164 static void
33165 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
33167 unsigned char saved_local_variables_forbidden_p;
33169 /* While we're parsing the default args, we might (due to the
33170 statement expression extension) encounter more classes. We want
33171 to handle them right away, but we don't want them getting mixed
33172 up with default args that are currently in the queue. */
33173 push_unparsed_function_queues (parser);
33175 /* Local variable names (and the `this' keyword) may not appear
33176 in a default argument. */
33177 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
33178 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
33180 push_defarg_context (fn);
33182 begin_scope (sk_function_parms, fn);
33184 /* Gather the PARM_DECLs into a vec so we can keep track of them when
33185 pushdecl clears DECL_CHAIN. */
33186 releasing_vec parms;
33187 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
33188 parmdecl = DECL_CHAIN (parmdecl))
33189 vec_safe_push (parms, parmdecl);
33191 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
33192 for (int i = 0;
33193 parm && parm != void_list_node;
33194 parm = TREE_CHAIN (parm),
33195 ++i)
33197 tree default_arg = TREE_PURPOSE (parm);
33198 tree parsed_arg;
33200 tree parmdecl = parms[i];
33201 pushdecl (parmdecl);
33203 if (!default_arg)
33204 continue;
33206 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
33207 /* This can happen for a friend declaration for a function
33208 already declared with default arguments. */
33209 continue;
33211 parsed_arg
33212 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
33213 default_arg,
33214 TREE_VALUE (parm));
33215 TREE_PURPOSE (parm) = parsed_arg;
33217 /* Update any instantiations we've already created. */
33218 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
33219 TREE_PURPOSE (copy) = parsed_arg;
33222 pop_bindings_and_leave_scope ();
33224 /* Restore DECL_CHAINs after clobbering by pushdecl. */
33225 parm = NULL_TREE;
33226 for (int i = parms->length () - 1; i >= 0; --i)
33228 DECL_CHAIN (parms[i]) = parm;
33229 parm = parms[i];
33232 pop_defarg_context ();
33234 /* Make sure no default arg is missing. */
33235 check_default_args (fn);
33237 /* Restore the state of local_variables_forbidden_p. */
33238 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
33240 /* Restore the queue. */
33241 pop_unparsed_function_queues (parser);
33244 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
33246 sizeof ... ( identifier )
33248 where the 'sizeof' token has already been consumed. */
33250 static tree
33251 cp_parser_sizeof_pack (cp_parser *parser)
33253 /* Consume the `...'. */
33254 cp_lexer_consume_token (parser->lexer);
33255 maybe_warn_variadic_templates ();
33257 matching_parens parens;
33258 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
33259 if (paren)
33260 parens.consume_open (parser);
33261 else
33262 permerror (cp_lexer_peek_token (parser->lexer)->location,
33263 "%<sizeof...%> argument must be surrounded by parentheses");
33265 cp_token *token = cp_lexer_peek_token (parser->lexer);
33266 tree name = cp_parser_identifier (parser);
33267 if (name == error_mark_node)
33268 return error_mark_node;
33269 /* The name is not qualified. */
33270 parser->scope = NULL_TREE;
33271 parser->qualifying_scope = NULL_TREE;
33272 parser->object_scope = NULL_TREE;
33273 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
33274 if (expr == error_mark_node)
33275 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
33276 token->location);
33277 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
33278 expr = TREE_TYPE (expr);
33279 else if (TREE_CODE (expr) == CONST_DECL)
33280 expr = DECL_INITIAL (expr);
33281 expr = make_pack_expansion (expr);
33282 if (expr != error_mark_node)
33283 PACK_EXPANSION_SIZEOF_P (expr) = true;
33285 if (paren)
33286 parens.require_close (parser);
33288 return expr;
33291 /* Parse the operand of `sizeof' (or a similar operator). Returns
33292 either a TYPE or an expression, depending on the form of the
33293 input. The KEYWORD indicates which kind of expression we have
33294 encountered. */
33296 static tree
33297 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
33299 tree expr = NULL_TREE;
33300 const char *saved_message;
33301 const char *saved_message_arg;
33302 bool saved_integral_constant_expression_p;
33303 bool saved_non_integral_constant_expression_p;
33305 /* If it's a `...', then we are computing the length of a parameter
33306 pack. */
33307 if (keyword == RID_SIZEOF
33308 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33309 return cp_parser_sizeof_pack (parser);
33311 /* Types cannot be defined in a `sizeof' expression. Save away the
33312 old message. */
33313 saved_message = parser->type_definition_forbidden_message;
33314 saved_message_arg = parser->type_definition_forbidden_message_arg;
33315 parser->type_definition_forbidden_message
33316 = G_("types may not be defined in %qs expressions");
33317 parser->type_definition_forbidden_message_arg
33318 = IDENTIFIER_POINTER (ridpointers[keyword]);
33320 /* The restrictions on constant-expressions do not apply inside
33321 sizeof expressions. */
33322 saved_integral_constant_expression_p
33323 = parser->integral_constant_expression_p;
33324 saved_non_integral_constant_expression_p
33325 = parser->non_integral_constant_expression_p;
33326 parser->integral_constant_expression_p = false;
33328 auto cleanup = make_temp_override
33329 (parser->auto_is_implicit_function_template_parm_p, false);
33331 /* Do not actually evaluate the expression. */
33332 ++cp_unevaluated_operand;
33333 ++c_inhibit_evaluation_warnings;
33334 /* If it's a `(', then we might be looking at the type-id
33335 construction. */
33336 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33338 tree type = NULL_TREE;
33340 tentative_firewall firewall (parser);
33342 /* We can't be sure yet whether we're looking at a type-id or an
33343 expression. */
33344 cp_parser_parse_tentatively (parser);
33346 matching_parens parens;
33347 parens.consume_open (parser);
33349 /* Note: as a GNU Extension, compound literals are considered
33350 postfix-expressions as they are in C99, so they are valid
33351 arguments to sizeof. See comment in cp_parser_cast_expression
33352 for details. */
33353 if (cp_parser_compound_literal_p (parser))
33354 cp_parser_simulate_error (parser);
33355 else
33357 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
33358 parser->in_type_id_in_expr_p = true;
33359 /* Look for the type-id. */
33360 type = cp_parser_type_id (parser);
33361 /* Look for the closing `)'. */
33362 parens.require_close (parser);
33363 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
33366 /* If all went well, then we're done. */
33367 if (cp_parser_parse_definitely (parser))
33368 expr = type;
33369 else
33371 /* Commit to the tentative_firewall so we get syntax errors. */
33372 cp_parser_commit_to_tentative_parse (parser);
33374 expr = cp_parser_unary_expression (parser);
33377 else
33378 expr = cp_parser_unary_expression (parser);
33380 /* Go back to evaluating expressions. */
33381 --cp_unevaluated_operand;
33382 --c_inhibit_evaluation_warnings;
33384 /* And restore the old one. */
33385 parser->type_definition_forbidden_message = saved_message;
33386 parser->type_definition_forbidden_message_arg = saved_message_arg;
33387 parser->integral_constant_expression_p
33388 = saved_integral_constant_expression_p;
33389 parser->non_integral_constant_expression_p
33390 = saved_non_integral_constant_expression_p;
33392 return expr;
33395 /* If the current declaration has no declarator, return true. */
33397 static bool
33398 cp_parser_declares_only_class_p (cp_parser *parser)
33400 /* If the next token is a `;' or a `,' then there is no
33401 declarator. */
33402 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33403 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
33406 /* Update the DECL_SPECS to reflect the storage class indicated by
33407 KEYWORD. */
33409 static void
33410 cp_parser_set_storage_class (cp_parser *parser,
33411 cp_decl_specifier_seq *decl_specs,
33412 enum rid keyword,
33413 cp_token *token)
33415 cp_storage_class storage_class;
33417 switch (keyword)
33419 case RID_AUTO:
33420 storage_class = sc_auto;
33421 break;
33422 case RID_REGISTER:
33423 storage_class = sc_register;
33424 break;
33425 case RID_STATIC:
33426 storage_class = sc_static;
33427 break;
33428 case RID_EXTERN:
33429 storage_class = sc_extern;
33430 break;
33431 case RID_MUTABLE:
33432 storage_class = sc_mutable;
33433 break;
33434 default:
33435 gcc_unreachable ();
33438 if (parser->in_unbraced_linkage_specification_p)
33440 error_at (token->location, "invalid use of %qD in linkage specification",
33441 ridpointers[keyword]);
33442 return;
33444 else if (decl_specs->storage_class != sc_none)
33446 if (decl_specs->conflicting_specifiers_p)
33447 return;
33448 gcc_rich_location richloc (token->location);
33449 richloc.add_location_if_nearby (decl_specs->locations[ds_storage_class]);
33450 if (decl_specs->storage_class == storage_class)
33451 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
33452 else
33453 error_at (&richloc,
33454 "%qD specifier conflicts with %qs",
33455 ridpointers[keyword],
33456 cp_storage_class_name[decl_specs->storage_class]);
33457 decl_specs->conflicting_specifiers_p = true;
33458 return;
33461 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
33462 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
33463 && decl_specs->gnu_thread_keyword_p)
33465 pedwarn (decl_specs->locations[ds_thread], 0,
33466 "%<__thread%> before %qD", ridpointers[keyword]);
33469 decl_specs->storage_class = storage_class;
33470 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
33472 /* A storage class specifier cannot be applied alongside a typedef
33473 specifier. If there is a typedef specifier present then set
33474 conflicting_specifiers_p which will trigger an error later
33475 on in grokdeclarator. */
33476 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33477 && !decl_specs->conflicting_specifiers_p)
33479 gcc_rich_location richloc (token->location);
33480 richloc.add_location_if_nearby (decl_specs->locations[ds_typedef]);
33481 error_at (&richloc,
33482 "%qD specifier conflicts with %<typedef%>",
33483 ridpointers[keyword]);
33484 decl_specs->conflicting_specifiers_p = true;
33488 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
33489 is true, the type is a class or enum definition. */
33491 static void
33492 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
33493 tree type_spec,
33494 cp_token *token,
33495 bool type_definition_p)
33497 decl_specs->any_specifiers_p = true;
33499 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
33500 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
33501 this is what happened. In system headers, we ignore these
33502 declarations so that G++ can work with system headers that are not
33503 C++-safe. */
33504 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33505 && !type_definition_p
33506 && TYPE_P (type_spec)
33507 && (type_spec == boolean_type_node
33508 || type_spec == char8_type_node
33509 || type_spec == char16_type_node
33510 || type_spec == char32_type_node
33511 || extended_float_type_p (type_spec)
33512 || type_spec == wchar_type_node)
33513 && (decl_specs->type
33514 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
33515 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
33516 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
33517 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
33519 decl_specs->redefined_builtin_type = type_spec;
33520 set_and_check_decl_spec_loc (decl_specs,
33521 ds_redefined_builtin_type_spec,
33522 token);
33523 if (!decl_specs->type)
33525 decl_specs->type = type_spec;
33526 decl_specs->type_definition_p = false;
33527 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
33530 else if (decl_specs->type)
33531 decl_specs->multiple_types_p = true;
33532 else
33534 decl_specs->type = type_spec;
33535 decl_specs->type_definition_p = type_definition_p;
33536 decl_specs->redefined_builtin_type = NULL_TREE;
33537 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
33541 /* True iff TOKEN is the GNU keyword __thread. */
33543 static bool
33544 token_is__thread (cp_token *token)
33546 gcc_assert (token->keyword == RID_THREAD);
33547 return id_equal (token->u.value, "__thread");
33550 /* Set the location for a declarator specifier and check if it is
33551 duplicated.
33553 DECL_SPECS is the sequence of declarator specifiers onto which to
33554 set the location.
33556 DS is the single declarator specifier to set which location is to
33557 be set onto the existing sequence of declarators.
33559 LOCATION is the location for the declarator specifier to
33560 consider. */
33562 static void
33563 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
33564 cp_decl_spec ds, cp_token *token)
33566 gcc_assert (ds < ds_last);
33568 if (decl_specs == NULL)
33569 return;
33571 location_t location = token->location;
33573 if (decl_specs->locations[ds] == 0)
33575 decl_specs->locations[ds] = location;
33576 if (ds == ds_thread)
33577 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
33579 else
33581 if (ds == ds_long)
33583 if (decl_specs->locations[ds_long_long] != 0)
33584 error_at (location,
33585 "%<long long long%> is too long for GCC");
33586 else
33588 decl_specs->locations[ds_long_long] = location;
33589 pedwarn_cxx98 (location,
33590 OPT_Wlong_long,
33591 "ISO C++ 1998 does not support %<long long%>");
33594 else if (ds == ds_thread)
33596 bool gnu = token_is__thread (token);
33597 gcc_rich_location richloc (location);
33598 if (gnu != decl_specs->gnu_thread_keyword_p)
33600 richloc.add_range (decl_specs->locations[ds_thread]);
33601 error_at (&richloc,
33602 "both %<__thread%> and %<thread_local%> specified");
33604 else
33606 richloc.add_fixit_remove ();
33607 error_at (&richloc, "duplicate %qD", token->u.value);
33610 else
33612 static const char *const decl_spec_names[] = {
33613 "signed",
33614 "unsigned",
33615 "short",
33616 "long",
33617 "const",
33618 "volatile",
33619 "restrict",
33620 "inline",
33621 "virtual",
33622 "explicit",
33623 "friend",
33624 "typedef",
33625 "using",
33626 "constexpr",
33627 "__complex",
33628 "constinit",
33629 "consteval"
33631 gcc_rich_location richloc (location);
33632 richloc.add_fixit_remove ();
33633 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
33638 /* Return true iff the declarator specifier DS is present in the
33639 sequence of declarator specifiers DECL_SPECS. */
33641 bool
33642 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
33643 cp_decl_spec ds)
33645 gcc_assert (ds < ds_last);
33647 if (decl_specs == NULL)
33648 return false;
33650 return decl_specs->locations[ds] != 0;
33653 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
33654 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
33656 static bool
33657 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
33659 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
33662 /* Issue an error message indicating that TOKEN_DESC was expected.
33663 If KEYWORD is true, it indicated this function is called by
33664 cp_parser_require_keword and the required token can only be
33665 a indicated keyword.
33667 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33668 within any error as the location of an "opening" token matching
33669 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33670 RT_CLOSE_PAREN). */
33672 static void
33673 cp_parser_required_error (cp_parser *parser,
33674 required_token token_desc,
33675 bool keyword,
33676 location_t matching_location)
33678 if (cp_parser_simulate_error (parser))
33679 return;
33681 const char *gmsgid = NULL;
33682 switch (token_desc)
33684 case RT_NEW:
33685 gmsgid = G_("expected %<new%>");
33686 break;
33687 case RT_DELETE:
33688 gmsgid = G_("expected %<delete%>");
33689 break;
33690 case RT_RETURN:
33691 gmsgid = G_("expected %<return%>");
33692 break;
33693 case RT_WHILE:
33694 gmsgid = G_("expected %<while%>");
33695 break;
33696 case RT_EXTERN:
33697 gmsgid = G_("expected %<extern%>");
33698 break;
33699 case RT_STATIC_ASSERT:
33700 gmsgid = G_("expected %<static_assert%>");
33701 break;
33702 case RT_DECLTYPE:
33703 gmsgid = G_("expected %<decltype%>");
33704 break;
33705 case RT_OPERATOR:
33706 gmsgid = G_("expected %<operator%>");
33707 break;
33708 case RT_CLASS:
33709 gmsgid = G_("expected %<class%>");
33710 break;
33711 case RT_TEMPLATE:
33712 gmsgid = G_("expected %<template%>");
33713 break;
33714 case RT_NAMESPACE:
33715 gmsgid = G_("expected %<namespace%>");
33716 break;
33717 case RT_USING:
33718 gmsgid = G_("expected %<using%>");
33719 break;
33720 case RT_ASM:
33721 gmsgid = G_("expected %<asm%>");
33722 break;
33723 case RT_TRY:
33724 gmsgid = G_("expected %<try%>");
33725 break;
33726 case RT_CATCH:
33727 gmsgid = G_("expected %<catch%>");
33728 break;
33729 case RT_THROW:
33730 gmsgid = G_("expected %<throw%>");
33731 break;
33732 case RT_AUTO:
33733 gmsgid = G_("expected %<auto%>");
33734 break;
33735 case RT_LABEL:
33736 gmsgid = G_("expected %<__label__%>");
33737 break;
33738 case RT_AT_TRY:
33739 gmsgid = G_("expected %<@try%>");
33740 break;
33741 case RT_AT_SYNCHRONIZED:
33742 gmsgid = G_("expected %<@synchronized%>");
33743 break;
33744 case RT_AT_THROW:
33745 gmsgid = G_("expected %<@throw%>");
33746 break;
33747 case RT_TRANSACTION_ATOMIC:
33748 gmsgid = G_("expected %<__transaction_atomic%>");
33749 break;
33750 case RT_TRANSACTION_RELAXED:
33751 gmsgid = G_("expected %<__transaction_relaxed%>");
33752 break;
33753 case RT_CO_YIELD:
33754 gmsgid = G_("expected %<co_yield%>");
33755 break;
33756 default:
33757 break;
33760 if (!gmsgid && !keyword)
33762 switch (token_desc)
33764 case RT_SEMICOLON:
33765 gmsgid = G_("expected %<;%>");
33766 break;
33767 case RT_OPEN_PAREN:
33768 gmsgid = G_("expected %<(%>");
33769 break;
33770 case RT_CLOSE_BRACE:
33771 gmsgid = G_("expected %<}%>");
33772 break;
33773 case RT_OPEN_BRACE:
33774 gmsgid = G_("expected %<{%>");
33775 break;
33776 case RT_CLOSE_SQUARE:
33777 gmsgid = G_("expected %<]%>");
33778 break;
33779 case RT_OPEN_SQUARE:
33780 gmsgid = G_("expected %<[%>");
33781 break;
33782 case RT_COMMA:
33783 gmsgid = G_("expected %<,%>");
33784 break;
33785 case RT_SCOPE:
33786 gmsgid = G_("expected %<::%>");
33787 break;
33788 case RT_LESS:
33789 gmsgid = G_("expected %<<%>");
33790 break;
33791 case RT_GREATER:
33792 gmsgid = G_("expected %<>%>");
33793 break;
33794 case RT_EQ:
33795 gmsgid = G_("expected %<=%>");
33796 break;
33797 case RT_ELLIPSIS:
33798 gmsgid = G_("expected %<...%>");
33799 break;
33800 case RT_MULT:
33801 gmsgid = G_("expected %<*%>");
33802 break;
33803 case RT_COMPL:
33804 gmsgid = G_("expected %<~%>");
33805 break;
33806 case RT_COLON:
33807 gmsgid = G_("expected %<:%>");
33808 break;
33809 case RT_COLON_SCOPE:
33810 gmsgid = G_("expected %<:%> or %<::%>");
33811 break;
33812 case RT_CLOSE_PAREN:
33813 gmsgid = G_("expected %<)%>");
33814 break;
33815 case RT_COMMA_CLOSE_PAREN:
33816 gmsgid = G_("expected %<,%> or %<)%>");
33817 break;
33818 case RT_PRAGMA_EOL:
33819 gmsgid = G_("expected end of line");
33820 break;
33821 case RT_NAME:
33822 gmsgid = G_("expected identifier");
33823 break;
33824 case RT_SELECT:
33825 gmsgid = G_("expected selection-statement");
33826 break;
33827 case RT_ITERATION:
33828 gmsgid = G_("expected iteration-statement");
33829 break;
33830 case RT_JUMP:
33831 gmsgid = G_("expected jump-statement");
33832 break;
33833 case RT_CLASS_KEY:
33834 gmsgid = G_("expected class-key");
33835 break;
33836 case RT_CLASS_TYPENAME_TEMPLATE:
33837 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
33838 break;
33839 default:
33840 gcc_unreachable ();
33844 if (gmsgid)
33845 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
33849 /* If the next token is of the indicated TYPE, consume it. Otherwise,
33850 issue an error message indicating that TOKEN_DESC was expected.
33852 Returns the token consumed, if the token had the appropriate type.
33853 Otherwise, returns NULL.
33855 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33856 within any error as the location of an "opening" token matching
33857 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33858 RT_CLOSE_PAREN). */
33860 static cp_token *
33861 cp_parser_require (cp_parser* parser,
33862 enum cpp_ttype type,
33863 required_token token_desc,
33864 location_t matching_location)
33866 if (cp_lexer_next_token_is (parser->lexer, type))
33867 return cp_lexer_consume_token (parser->lexer);
33868 else
33870 /* Output the MESSAGE -- unless we're parsing tentatively. */
33871 if (!cp_parser_simulate_error (parser))
33872 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
33873 matching_location);
33874 return NULL;
33878 /* Skip an entire parameter list from start to finish. The next token must
33879 be the initial "<" of the parameter list. Returns true on success and
33880 false otherwise. */
33882 static bool
33883 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
33885 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
33886 requires it. */
33887 cp_lexer_consume_token (parser->lexer);
33888 return cp_parser_skip_to_end_of_template_parameter_list (parser);
33891 /* Ensure we are at the end of a template parameter list. If we are, return.
33892 If we are not, something has gone wrong, in which case issue an error and
33893 skip to end of the parameter list. */
33895 static void
33896 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
33898 /* Are we ready, yet? If not, issue error message. */
33899 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
33900 return;
33902 cp_parser_skip_to_end_of_template_parameter_list (parser);
33905 /* You should only call this function from inside a template parameter list
33906 (i.e. the current token should at least be the initial "<" of the
33907 parameter list). If you are skipping the entire list, it may be better to
33908 use cp_parser_skip_entire_template_parameter_list.
33910 Tokens are skipped until the final ">" is found, or if we see
33911 '{', '}', ';', or if we find an unbalanced ')' or ']'.
33913 Returns true if we successfully reached the end, and false if
33914 something unexpected happened (e.g. end of file). */
33916 static bool
33917 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
33919 /* Current level of '< ... >'. */
33920 unsigned level = 0;
33921 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
33922 unsigned nesting_depth = 0;
33924 /* Skip tokens until the desired token is found. */
33925 while (true)
33927 /* Peek at the next token. */
33928 switch (cp_lexer_peek_token (parser->lexer)->type)
33930 case CPP_LESS:
33931 if (!nesting_depth)
33932 ++level;
33933 break;
33935 case CPP_RSHIFT:
33936 if (cxx_dialect == cxx98)
33937 /* C++0x views the `>>' operator as two `>' tokens, but
33938 C++98 does not. */
33939 break;
33940 else if (!nesting_depth && level-- == 0)
33942 /* We've hit a `>>' where the first `>' closes the
33943 template argument list, and the second `>' is
33944 spurious. Just consume the `>>' and stop; we've
33945 already produced at least one error. */
33946 cp_lexer_consume_token (parser->lexer);
33947 return false;
33949 /* Fall through for C++0x, so we handle the second `>' in
33950 the `>>'. */
33951 gcc_fallthrough ();
33953 case CPP_GREATER:
33954 if (!nesting_depth && level-- == 0)
33956 /* We've reached the token we want, consume it and stop. */
33957 cp_lexer_consume_token (parser->lexer);
33958 return true;
33960 break;
33962 case CPP_OPEN_PAREN:
33963 case CPP_OPEN_SQUARE:
33964 ++nesting_depth;
33965 break;
33967 case CPP_CLOSE_PAREN:
33968 case CPP_CLOSE_SQUARE:
33969 if (nesting_depth-- == 0)
33970 return false;
33971 break;
33973 case CPP_EOF:
33974 case CPP_PRAGMA_EOL:
33975 case CPP_SEMICOLON:
33976 case CPP_OPEN_BRACE:
33977 case CPP_CLOSE_BRACE:
33978 /* The '>' was probably forgotten, don't look further. */
33979 return false;
33981 default:
33982 break;
33985 /* Consume this token. */
33986 cp_lexer_consume_token (parser->lexer);
33990 /* If the next token is the indicated keyword, consume it. Otherwise,
33991 issue an error message indicating that TOKEN_DESC was expected.
33993 Returns the token consumed, if the token had the appropriate type.
33994 Otherwise, returns NULL. */
33996 static cp_token *
33997 cp_parser_require_keyword (cp_parser* parser,
33998 enum rid keyword,
33999 required_token token_desc)
34001 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
34003 if (token && token->keyword != keyword)
34005 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
34006 UNKNOWN_LOCATION);
34007 return NULL;
34010 return token;
34013 /* Returns TRUE iff TOKEN is a token that can begin the body of a
34014 function-definition. */
34016 static bool
34017 cp_parser_token_starts_function_definition_p (cp_token* token)
34019 return (/* An ordinary function-body begins with an `{'. */
34020 token->type == CPP_OPEN_BRACE
34021 /* A ctor-initializer begins with a `:'. */
34022 || token->type == CPP_COLON
34023 /* A function-try-block begins with `try'. */
34024 || token->keyword == RID_TRY
34025 /* A function-transaction-block begins with `__transaction_atomic'
34026 or `__transaction_relaxed'. */
34027 || token->keyword == RID_TRANSACTION_ATOMIC
34028 || token->keyword == RID_TRANSACTION_RELAXED
34029 /* The named return value extension begins with `return'. */
34030 || token->keyword == RID_RETURN);
34033 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
34034 definition. */
34036 static bool
34037 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
34039 cp_token *token;
34041 token = cp_lexer_peek_token (parser->lexer);
34042 return (token->type == CPP_OPEN_BRACE
34043 || (token->type == CPP_COLON
34044 && !parser->colon_doesnt_start_class_def_p));
34047 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
34048 C++0x) ending a template-argument. */
34050 static bool
34051 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
34053 cp_token *token;
34055 token = cp_lexer_peek_token (parser->lexer);
34056 return (token->type == CPP_COMMA
34057 || token->type == CPP_GREATER
34058 || token->type == CPP_ELLIPSIS
34059 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
34060 /* For better diagnostics, treat >>= like that too, that
34061 shouldn't appear non-nested in template arguments. */
34062 || token->type == CPP_RSHIFT_EQ);
34065 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
34066 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
34068 static bool
34069 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
34070 size_t n)
34072 cp_token *token;
34074 token = cp_lexer_peek_nth_token (parser->lexer, n);
34075 if (token->type == CPP_LESS)
34076 return true;
34077 /* Check for the sequence `<::' in the original code. It would be lexed as
34078 `[:', where `[' is a digraph, and there is no whitespace before
34079 `:'. */
34080 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
34082 cp_token *token2;
34083 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
34084 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
34085 return true;
34087 return false;
34090 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
34091 or none_type otherwise. */
34093 static enum tag_types
34094 cp_parser_token_is_class_key (cp_token* token)
34096 switch (token->keyword)
34098 case RID_CLASS:
34099 return class_type;
34100 case RID_STRUCT:
34101 return record_type;
34102 case RID_UNION:
34103 return union_type;
34105 default:
34106 return none_type;
34110 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
34111 or none_type otherwise or if the token is null. */
34113 static enum tag_types
34114 cp_parser_token_is_type_parameter_key (cp_token* token)
34116 if (!token)
34117 return none_type;
34119 switch (token->keyword)
34121 case RID_CLASS:
34122 return class_type;
34123 case RID_TYPENAME:
34124 return typename_type;
34126 default:
34127 return none_type;
34131 /* Diagnose redundant enum-keys. */
34133 static void
34134 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
34135 tree type, rid scoped_key)
34137 if (!warn_redundant_tags)
34138 return;
34140 tree type_decl = TYPE_MAIN_DECL (type);
34141 tree name = DECL_NAME (type_decl);
34142 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
34143 push_deferring_access_checks (dk_no_check);
34144 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34145 pop_deferring_access_checks ();
34147 /* The enum-key is redundant for uses of the TYPE that are not
34148 declarations and for which name lookup returns just the type
34149 itself. */
34150 if (decl != type_decl)
34151 return;
34153 if (scoped_key != RID_CLASS
34154 && scoped_key != RID_STRUCT
34155 && current_lang_name != lang_name_cplusplus
34156 && current_namespace == global_namespace)
34158 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
34159 enum tag in shared C/C++ code in files (such as headers) included
34160 in the main source file. */
34161 const line_map_ordinary *map = NULL;
34162 linemap_resolve_location (line_table, key_loc,
34163 LRK_MACRO_DEFINITION_LOCATION,
34164 &map);
34165 if (!MAIN_FILE_P (map))
34166 return;
34169 gcc_rich_location richloc (key_loc);
34170 richloc.add_fixit_remove (key_loc);
34171 warning_at (&richloc, OPT_Wredundant_tags,
34172 "redundant enum-key %<enum%s%> in reference to %q#T",
34173 (scoped_key == RID_CLASS ? " class"
34174 : scoped_key == RID_STRUCT ? " struct" : ""), type);
34177 /* Describes the set of declarations of a struct, class, or class template
34178 or its specializations. Used for -Wmismatched-tags. */
34180 class class_decl_loc_t
34182 public:
34184 class_decl_loc_t ()
34185 : locvec (), idxdef (), def_class_key ()
34187 locvec.create (4);
34190 /* Constructs an object for a single declaration of a class with
34191 CLASS_KEY at the current location in the current function (or
34192 at another scope). KEY_REDUNDANT is true if the class-key may
34193 be omitted in the current context without an ambiguity with
34194 another symbol with the same name.
34195 DEF_P is true for a class declaration that is a definition.
34196 CURLOC is the associated location. */
34197 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
34198 location_t curloc = input_location)
34199 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
34201 locvec.create (4);
34202 class_key_loc_t ckl (current_function_decl, curloc, class_key,
34203 key_redundant);
34204 locvec.quick_push (ckl);
34207 /* Copy, assign, and destroy the object. Necessary because LOCVEC
34208 isn't safely copyable and assignable and doesn't release storage
34209 on its own. */
34210 class_decl_loc_t (const class_decl_loc_t &rhs)
34211 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
34212 def_class_key (rhs.def_class_key)
34215 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
34217 if (this == &rhs)
34218 return *this;
34219 locvec.release ();
34220 locvec = rhs.locvec.copy ();
34221 idxdef = rhs.idxdef;
34222 def_class_key = rhs.def_class_key;
34223 return *this;
34226 ~class_decl_loc_t ()
34228 locvec.release ();
34231 /* Issues -Wmismatched-tags for a single class. */
34232 void diag_mismatched_tags (tree);
34234 /* Issues -Wmismatched-tags for all classes. */
34235 static void diag_mismatched_tags ();
34237 /* Adds TYPE_DECL to the collection of class decls and diagnoses
34238 redundant tags (if -Wredundant-tags is enabled). */
34239 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
34241 /* Either adds this decl to the collection of class decls
34242 or diagnoses it, whichever is appropriate. */
34243 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
34245 private:
34247 tree function (unsigned i) const
34249 return locvec[i].func;
34252 location_t location (unsigned i) const
34254 return locvec[i].loc;
34257 bool key_redundant (unsigned i) const
34259 return locvec[i].key_redundant;
34262 tag_types class_key (unsigned i) const
34264 return locvec[i].class_key;
34267 /* True if a definition for the class has been seen. */
34268 bool def_p () const
34270 return idxdef < locvec.length ();
34273 /* The location of a single mention of a class type with the given
34274 class-key. */
34275 struct class_key_loc_t
34277 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
34278 : func (func), loc (loc), class_key (key), key_redundant (redundant)
34281 /* The function the type is mentioned in. */
34282 tree func;
34283 /* The exact location. */
34284 location_t loc;
34285 /* The class-key used in the mention of the type. */
34286 tag_types class_key;
34287 /* True when the class-key could be omitted at this location
34288 without an ambiguity with another symbol of the same name. */
34289 bool key_redundant;
34291 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
34292 vec <class_key_loc_t> locvec;
34293 /* LOCVEC index of the definition or UINT_MAX if none exists. */
34294 unsigned idxdef;
34295 /* The class-key the class was last declared with or none_type when
34296 it has been declared with a mismatched key. */
34297 tag_types def_class_key;
34299 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
34300 description above. */
34301 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
34302 static class_to_loc_map_t class2loc;
34305 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
34307 /* Issue an error message if the CLASS_KEY does not match the TYPE.
34308 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34309 is set for a declaration of class TYPE and clear for a reference to
34310 it that is not a declaration of it. */
34312 static void
34313 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
34314 tag_types class_key, tree type, bool def_p,
34315 bool decl_p)
34317 if (type == error_mark_node)
34318 return;
34320 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
34321 if (seen_as_union != (class_key == union_type))
34323 if (permerror (input_location, "%qs tag used in naming %q#T",
34324 class_key == union_type ? "union"
34325 : class_key == record_type ? "struct" : "class",
34326 type))
34327 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
34328 "%q#T was previously declared here", type);
34329 return;
34332 if (!warn_mismatched_tags && !warn_redundant_tags)
34333 return;
34335 /* Only consider the true class-keys below and ignore typename_type,
34336 etc. that are not C++ class-keys. */
34337 if (class_key != class_type
34338 && class_key != record_type
34339 && class_key != union_type)
34340 return;
34342 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
34345 /* Returns the template or specialization of one to which the RECORD_TYPE
34346 TYPE corresponds. */
34348 static tree
34349 specialization_of (tree type)
34351 tree ret = type;
34353 /* Determine the template or its partial specialization to which TYPE
34354 corresponds. */
34355 if (tree ti = most_specialized_partial_spec (type, tf_none))
34356 if (ti != error_mark_node)
34357 ret = TREE_TYPE (TI_TEMPLATE (ti));
34359 if (ret == type)
34360 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
34362 return TYPE_MAIN_DECL (ret);
34366 /* Adds the class TYPE to the collection of class decls and diagnoses
34367 redundant tags (if -Wredundant-tags is enabled).
34368 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34369 is set for a (likely, based on syntactic context) declaration of class
34370 TYPE and clear for a reference to it that is not a declaration of it. */
34372 void
34373 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
34374 tag_types class_key, tree type, bool def_p, bool decl_p)
34376 tree type_decl = TYPE_MAIN_DECL (type);
34377 tree name = DECL_NAME (type_decl);
34378 /* Look up the NAME to see if it unambiguously refers to the TYPE
34379 and set KEY_REDUNDANT if so. */
34380 push_deferring_access_checks (dk_no_check);
34381 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34382 pop_deferring_access_checks ();
34384 /* The class-key is redundant for uses of the CLASS_TYPE that are
34385 neither definitions of it nor declarations, and for which name
34386 lookup returns just the type itself. */
34387 bool key_redundant = (!def_p && !decl_p
34388 && (decl == type_decl
34389 || TREE_CODE (decl) == TEMPLATE_DECL
34390 || (CLASS_TYPE_P (type)
34391 && TYPE_BEING_DEFINED (type))));
34393 if (key_redundant
34394 && class_key != class_type
34395 && current_lang_name != lang_name_cplusplus
34396 && current_namespace == global_namespace)
34398 /* Avoid issuing the diagnostic for apparently redundant struct
34399 and union class-keys in shared C/C++ code in files (such as
34400 headers) included in the main source file. */
34401 const line_map_ordinary *map = NULL;
34402 linemap_resolve_location (line_table, key_loc,
34403 LRK_MACRO_DEFINITION_LOCATION,
34404 &map);
34405 if (!MAIN_FILE_P (map))
34406 key_redundant = false;
34409 /* Set if a declaration of TYPE has previously been seen or if it must
34410 exist in a precompiled header. */
34411 bool exist;
34412 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
34413 if (!exist)
34415 tree type = TREE_TYPE (type_decl);
34416 if (def_p || !COMPLETE_TYPE_P (type))
34418 /* TYPE_DECL is the first declaration or definition of the type
34419 (outside precompiled headers -- see below). Just create
34420 a new entry for it and return unless it's a declaration
34421 involving a template that may need to be diagnosed by
34422 -Wredundant-tags. */
34423 *rdl = class_decl_loc_t (class_key, false, def_p);
34424 if (TREE_CODE (decl) != TEMPLATE_DECL)
34425 return;
34427 else
34429 /* TYPE was previously defined in some unknown precompiled header.
34430 Simply add a record of its definition at an unknown location and
34431 proceed below to add a reference to it at the current location.
34432 (Declarations in precompiled headers that are not definitions
34433 are ignored.) */
34434 tag_types def_key
34435 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
34436 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
34437 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
34438 exist = true;
34442 /* A prior declaration of TYPE_DECL has been seen. */
34444 if (key_redundant)
34446 gcc_rich_location richloc (key_loc);
34447 richloc.add_fixit_remove (key_loc);
34448 warning_at (&richloc, OPT_Wredundant_tags,
34449 "redundant class-key %qs in reference to %q#T",
34450 class_key == union_type ? "union"
34451 : class_key == record_type ? "struct" : "class",
34452 type);
34455 if (!exist)
34456 /* Do nothing if this is the first declaration of the type. */
34457 return;
34459 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
34460 /* Do nothing if the class-key in this declaration matches
34461 the definition. */
34462 return;
34464 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
34465 def_p);
34468 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
34469 of class decls or diagnoses it, whichever is appropriate. */
34471 void
34472 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
34473 tag_types class_key,
34474 bool redundant,
34475 bool def_p)
34477 /* Reset the CLASS_KEY associated with this type on mismatch.
34478 This is an optimization that lets the diagnostic code skip
34479 over classes that use the same class-key in all declarations. */
34480 if (def_class_key != class_key)
34481 def_class_key = none_type;
34483 /* Set IDXDEF to the index of the vector corresponding to
34484 the definition. */
34485 if (def_p)
34486 idxdef = locvec.length ();
34488 /* Append a record of this declaration to the vector. */
34489 class_key_loc_t ckl (current_function_decl, input_location, class_key,
34490 redundant);
34491 locvec.safe_push (ckl);
34493 if (idxdef == UINT_MAX)
34494 return;
34496 /* As a space optimization diagnose declarations of a class
34497 whose definition has been seen and purge the LOCVEC of
34498 all entries except the definition. */
34499 diag_mismatched_tags (type_decl);
34500 if (idxdef)
34502 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
34503 locvec.release ();
34504 locvec.reserve (2);
34505 locvec.safe_push (ent);
34506 idxdef = 0;
34508 else
34509 /* Pop the entry pushed above for this declaration. */
34510 locvec.pop ();
34513 /* Issues -Wmismatched-tags for a single class. */
34515 void
34516 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
34518 if (!warn_mismatched_tags)
34519 return;
34521 /* Number of uses of the class. */
34522 const unsigned ndecls = locvec.length ();
34524 /* The class (or template) declaration guiding the decisions about
34525 the diagnostic. For ordinary classes it's the same as THIS. For
34526 uses of instantiations of templates other than their declarations
34527 it points to the record for the declaration of the corresponding
34528 primary template or partial specialization. */
34529 class_decl_loc_t *cdlguide = this;
34531 tree type = TREE_TYPE (type_decl);
34532 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
34534 /* For implicit instantiations of a primary template look up
34535 the primary or partial specialization and use it as
34536 the expected class-key rather than using the class-key of
34537 the first reference to the instantiation. The primary must
34538 be (and inevitably is) at index zero. */
34539 tree spec = specialization_of (type);
34540 cdlguide = class2loc.get (spec);
34541 /* It's possible that we didn't find SPEC. Consider:
34543 template<typename T> struct A {
34544 template<typename U> struct W { };
34546 struct A<int>::W<int> w; // #1
34548 where while parsing A and #1 we've stashed
34549 A<T>
34550 A<T>::W<U>
34551 A<int>::W<int>
34552 into CLASS2LOC. If TYPE is A<int>::W<int>, specialization_of
34553 will yield A<int>::W<U> which may be in CLASS2LOC if we had
34554 an A<int> class specialization, but otherwise won't be in it.
34555 So try to look up A<T>::W<U>. */
34556 if (!cdlguide)
34558 spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
34559 cdlguide = class2loc.get (spec);
34561 /* Now we really should have found something. */
34562 gcc_assert (cdlguide != NULL);
34564 /* Skip declarations that consistently use the same class-key. */
34565 else if (def_class_key != none_type)
34566 return;
34568 /* Set if a definition for the class has been seen. */
34569 const bool def_p = cdlguide->def_p ();
34571 /* The index of the declaration whose class-key this declaration
34572 is expected to match. It's either the class-key of the class
34573 definition if one exists or the first declaration otherwise. */
34574 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
34576 /* The class-key the class is expected to be declared with: it's
34577 either the key used in its definition or the first declaration
34578 if no definition has been provided.
34579 For implicit instantiations of a primary template it's
34580 the class-key used to declare the primary with. The primary
34581 must be at index zero. */
34582 const tag_types xpect_key = cdlguide->class_key (idxguide);
34584 unsigned idx = 0;
34585 /* Advance IDX to the first declaration that either is not
34586 a definition or that doesn't match the first declaration
34587 if no definition is provided. */
34588 while (class_key (idx) == xpect_key)
34589 if (++idx == ndecls)
34590 return;
34592 /* Save the current function before changing it below. */
34593 tree save_func = current_function_decl;
34594 /* Set the function declaration to print in diagnostic context. */
34595 current_function_decl = function (idx);
34597 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
34598 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
34600 location_t loc = location (idx);
34601 bool key_redundant_p = key_redundant (idx);
34602 auto_diagnostic_group d;
34603 /* Issue a warning for the first mismatched declaration.
34604 Avoid using "%#qT" since the class-key for the same type will
34605 be the same regardless of which one was used in the declaraion. */
34606 if (warning_at (loc, OPT_Wmismatched_tags,
34607 "%qT declared with a mismatched class-key %qs",
34608 type_decl, xmatchkstr))
34610 /* Suggest how to avoid the warning for each instance since
34611 the guidance may be different depending on context. */
34612 inform (loc,
34613 (key_redundant_p
34614 ? G_("remove the class-key or replace it with %qs")
34615 : G_("replace the class-key with %qs")),
34616 xpectkstr);
34618 /* Also point to the first declaration or definition that guided
34619 the decision to issue the warning above. */
34620 inform (cdlguide->location (idxguide),
34621 (def_p
34622 ? G_("%qT defined as %qs here")
34623 : G_("%qT first declared as %qs here")),
34624 type_decl, xpectkstr);
34627 /* Issue warnings for the remaining inconsistent declarations. */
34628 for (unsigned i = idx + 1; i != ndecls; ++i)
34630 tag_types clskey = class_key (i);
34631 /* Skip over the declarations that match either the definition
34632 if one was provided or the first declaration. */
34633 if (clskey == xpect_key)
34634 continue;
34636 loc = location (i);
34637 key_redundant_p = key_redundant (i);
34638 /* Set the function declaration to print in diagnostic context. */
34639 current_function_decl = function (i);
34640 if (warning_at (loc, OPT_Wmismatched_tags,
34641 "%qT declared with a mismatched class-key %qs",
34642 type_decl, xmatchkstr))
34643 /* Suggest how to avoid the warning for each instance since
34644 the guidance may be different depending on context. */
34645 inform (loc,
34646 (key_redundant_p
34647 ? G_("remove the class-key or replace it with %qs")
34648 : G_("replace the class-key with %qs")),
34649 xpectkstr);
34652 /* Restore the current function in case it was replaced above. */
34653 current_function_decl = save_func;
34656 /* Issues -Wmismatched-tags for all classes. Called at the end
34657 of processing a translation unit, after declarations of all class
34658 types and their uses have been recorded. */
34660 void
34661 class_decl_loc_t::diag_mismatched_tags ()
34663 /* CLASS2LOC should be empty if both -Wmismatched-tags and
34664 -Wredundant-tags are disabled. */
34665 gcc_assert (warn_mismatched_tags
34666 || warn_redundant_tags
34667 || class2loc.is_empty ());
34669 /* Save the current function before changing on return. It should
34670 be null at this point. */
34671 temp_override<tree> cleanup (current_function_decl);
34673 if (warn_mismatched_tags)
34675 /* Iterate over the collected class/struct/template declarations. */
34676 typedef class_to_loc_map_t::iterator iter_t;
34677 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
34679 tree type_decl = (*it).first;
34680 class_decl_loc_t &recloc = (*it).second;
34681 recloc.diag_mismatched_tags (type_decl);
34685 class2loc.empty ();
34688 /* Issue an error message if DECL is redeclared with different
34689 access than its original declaration [class.access.spec/3].
34690 This applies to nested classes, nested class templates and
34691 enumerations [class.mem/1]. */
34693 static void
34694 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
34696 if (!decl
34697 || (!CLASS_TYPE_P (TREE_TYPE (decl))
34698 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
34699 return;
34701 if ((TREE_PRIVATE (decl)
34702 != (current_access_specifier == access_private_node))
34703 || (TREE_PROTECTED (decl)
34704 != (current_access_specifier == access_protected_node)))
34705 error_at (location, "%qD redeclared with different access", decl);
34708 /* Look for the `template' keyword, as a syntactic disambiguator.
34709 Return TRUE iff it is present, in which case it will be
34710 consumed. */
34712 static bool
34713 cp_parser_optional_template_keyword (cp_parser *parser)
34715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
34717 /* In C++98 the `template' keyword can only be used within templates;
34718 outside templates the parser can always figure out what is a
34719 template and what is not. In C++11, per the resolution of DR 468,
34720 `template' is allowed in cases where it is not strictly necessary. */
34721 if (!processing_template_decl
34722 && pedantic && cxx_dialect == cxx98)
34724 cp_token *token = cp_lexer_peek_token (parser->lexer);
34725 pedwarn (token->location, OPT_Wpedantic,
34726 "in C++98 %<template%> (as a disambiguator) is only "
34727 "allowed within templates");
34728 /* If this part of the token stream is rescanned, the same
34729 error message would be generated. So, we purge the token
34730 from the stream. */
34731 cp_lexer_purge_token (parser->lexer);
34732 return false;
34734 else
34736 /* Consume the `template' keyword. */
34737 cp_lexer_consume_token (parser->lexer);
34738 return true;
34741 return false;
34744 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
34745 set PARSER->SCOPE, and perform other related actions. */
34747 static void
34748 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
34750 struct tree_check *check_value;
34752 /* Get the stored value. */
34753 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
34754 /* Set the scope from the stored value. */
34755 parser->scope = saved_checks_value (check_value);
34756 parser->qualifying_scope = check_value->qualifying_scope;
34757 parser->object_scope = parser->context->object_type;
34758 parser->context->object_type = NULL_TREE;
34761 /* Consume tokens up through a non-nested END token. Returns TRUE if we
34762 encounter the end of a block before what we were looking for. */
34764 static bool
34765 cp_parser_cache_group (cp_parser *parser,
34766 enum cpp_ttype end,
34767 unsigned depth)
34769 while (true)
34771 cp_token *token = cp_lexer_peek_token (parser->lexer);
34773 /* Abort a parenthesized expression if we encounter a semicolon. */
34774 if ((end == CPP_CLOSE_PAREN || depth == 0)
34775 && token->type == CPP_SEMICOLON)
34776 return true;
34777 /* If we've reached the end of the file, stop. */
34778 if (token->type == CPP_EOF
34779 || (end != CPP_PRAGMA_EOL
34780 && token->type == CPP_PRAGMA_EOL))
34781 return true;
34782 if (token->type == CPP_CLOSE_BRACE && depth == 0)
34783 /* We've hit the end of an enclosing block, so there's been some
34784 kind of syntax error. */
34785 return true;
34787 /* Consume the token. */
34788 cp_lexer_consume_token (parser->lexer);
34789 /* See if it starts a new group. */
34790 if (token->type == CPP_OPEN_BRACE)
34792 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
34793 /* In theory this should probably check end == '}', but
34794 cp_parser_save_member_function_body needs it to exit
34795 after either '}' or ')' when called with ')'. */
34796 if (depth == 0)
34797 return false;
34799 else if (token->type == CPP_OPEN_PAREN)
34801 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
34802 if (depth == 0 && end == CPP_CLOSE_PAREN)
34803 return false;
34805 else if (token->type == CPP_PRAGMA)
34806 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
34807 else if (token->type == end)
34808 return false;
34812 /* Like above, for caching a default argument or NSDMI. Both of these are
34813 terminated by a non-nested comma, but it can be unclear whether or not a
34814 comma is nested in a template argument list unless we do more parsing.
34815 In order to handle this ambiguity, when we encounter a ',' after a '<'
34816 we try to parse what follows as a parameter-declaration-list (in the
34817 case of a default argument) or a member-declarator (in the case of an
34818 NSDMI). If that succeeds, then we stop caching. */
34820 static tree
34821 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
34823 unsigned depth = 0;
34824 int maybe_template_id = 0;
34825 cp_token *first_token;
34826 cp_token *token;
34827 tree default_argument;
34829 /* Add tokens until we have processed the entire default
34830 argument. We add the range [first_token, token). */
34831 first_token = cp_lexer_peek_token (parser->lexer);
34832 if (first_token->type == CPP_OPEN_BRACE)
34834 /* For list-initialization, this is straightforward. */
34835 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
34836 token = cp_lexer_peek_token (parser->lexer);
34838 else while (true)
34840 bool done = false;
34842 /* Peek at the next token. */
34843 token = cp_lexer_peek_token (parser->lexer);
34844 /* What we do depends on what token we have. */
34845 switch (token->type)
34847 /* In valid code, a default argument must be
34848 immediately followed by a `,' `)', or `...'. */
34849 case CPP_COMMA:
34850 if (depth == 0 && maybe_template_id)
34852 /* If we've seen a '<', we might be in a
34853 template-argument-list. Until Core issue 325 is
34854 resolved, we don't know how this situation ought
34855 to be handled, so try to DTRT. We check whether
34856 what comes after the comma is a valid parameter
34857 declaration list. If it is, then the comma ends
34858 the default argument; otherwise the default
34859 argument continues. */
34860 bool error = false;
34861 cp_token *peek;
34863 /* Set ITALP so cp_parser_parameter_declaration_list
34864 doesn't decide to commit to this parse. */
34865 bool saved_italp = parser->in_template_argument_list_p;
34866 parser->in_template_argument_list_p = true;
34868 cp_parser_parse_tentatively (parser);
34870 if (nsdmi)
34872 /* Parse declarators until we reach a non-comma or
34873 somthing that cannot be an initializer.
34874 Just checking whether we're looking at a single
34875 declarator is insufficient. Consider:
34876 int var = tuple<T,U>::x;
34877 The template parameter 'U' looks exactly like a
34878 declarator. */
34881 int ctor_dtor_or_conv_p;
34882 cp_lexer_consume_token (parser->lexer);
34883 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
34884 CP_PARSER_FLAGS_NONE,
34885 &ctor_dtor_or_conv_p,
34886 /*parenthesized_p=*/NULL,
34887 /*member_p=*/true,
34888 /*friend_p=*/false,
34889 /*static_p=*/false);
34890 peek = cp_lexer_peek_token (parser->lexer);
34891 if (cp_parser_error_occurred (parser))
34892 break;
34894 while (peek->type == CPP_COMMA);
34895 /* If we met an '=' or ';' then the original comma
34896 was the end of the NSDMI. Otherwise assume
34897 we're still in the NSDMI. */
34898 error = (peek->type != CPP_EQ
34899 && peek->type != CPP_SEMICOLON);
34901 else
34903 cp_lexer_consume_token (parser->lexer);
34904 begin_scope (sk_function_parms, NULL_TREE);
34905 tree t = cp_parser_parameter_declaration_list
34906 (parser, CP_PARSER_FLAGS_NONE,
34907 /*pending_decls*/nullptr);
34908 if (t == error_mark_node)
34909 error = true;
34910 pop_bindings_and_leave_scope ();
34912 if (!cp_parser_error_occurred (parser) && !error)
34913 done = true;
34914 cp_parser_abort_tentative_parse (parser);
34916 parser->in_template_argument_list_p = saved_italp;
34917 break;
34919 /* FALLTHRU */
34920 case CPP_CLOSE_PAREN:
34921 case CPP_ELLIPSIS:
34922 /* If we run into a non-nested `;', `}', or `]',
34923 then the code is invalid -- but the default
34924 argument is certainly over. */
34925 case CPP_SEMICOLON:
34926 case CPP_CLOSE_BRACE:
34927 case CPP_CLOSE_SQUARE:
34928 if (depth == 0
34929 /* Handle correctly int n = sizeof ... ( p ); */
34930 && token->type != CPP_ELLIPSIS)
34931 done = true;
34932 /* Update DEPTH, if necessary. */
34933 else if (token->type == CPP_CLOSE_PAREN
34934 || token->type == CPP_CLOSE_BRACE
34935 || token->type == CPP_CLOSE_SQUARE)
34936 --depth;
34937 break;
34939 case CPP_OPEN_PAREN:
34940 case CPP_OPEN_SQUARE:
34941 case CPP_OPEN_BRACE:
34942 ++depth;
34943 break;
34945 case CPP_LESS:
34946 if (depth == 0)
34947 /* This might be the comparison operator, or it might
34948 start a template argument list. */
34949 ++maybe_template_id;
34950 break;
34952 case CPP_RSHIFT:
34953 if (cxx_dialect == cxx98)
34954 break;
34955 /* Fall through for C++0x, which treats the `>>'
34956 operator like two `>' tokens in certain
34957 cases. */
34958 gcc_fallthrough ();
34960 case CPP_GREATER:
34961 if (depth == 0)
34963 /* This might be an operator, or it might close a
34964 template argument list. But if a previous '<'
34965 started a template argument list, this will have
34966 closed it, so we can't be in one anymore. */
34967 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
34968 if (maybe_template_id < 0)
34969 maybe_template_id = 0;
34971 break;
34973 /* If we run out of tokens, issue an error message. */
34974 case CPP_EOF:
34975 case CPP_PRAGMA_EOL:
34976 error_at (token->location, "file ends in default argument");
34977 return error_mark_node;
34979 case CPP_NAME:
34980 case CPP_SCOPE:
34981 /* In these cases, we should look for template-ids.
34982 For example, if the default argument is
34983 `X<int, double>()', we need to do name lookup to
34984 figure out whether or not `X' is a template; if
34985 so, the `,' does not end the default argument.
34987 That is not yet done. */
34988 break;
34990 default:
34991 break;
34994 /* If we've reached the end, stop. */
34995 if (done)
34996 break;
34998 /* Add the token to the token block. */
34999 token = cp_lexer_consume_token (parser->lexer);
35002 /* Create a DEFERRED_PARSE to represent the unparsed default
35003 argument. */
35004 default_argument = make_node (DEFERRED_PARSE);
35005 DEFPARSE_TOKENS (default_argument)
35006 = cp_token_cache_new (first_token, token);
35007 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
35009 return default_argument;
35012 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
35014 location_t
35015 defparse_location (tree default_argument)
35017 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
35018 location_t start = tokens->first->location;
35019 location_t end = tokens->last->location;
35020 return make_location (start, start, end);
35023 /* Begin parsing tentatively. We always save tokens while parsing
35024 tentatively so that if the tentative parsing fails we can restore the
35025 tokens. */
35027 static void
35028 cp_parser_parse_tentatively (cp_parser* parser)
35030 /* Enter a new parsing context. */
35031 parser->context = cp_parser_context_new (parser->context);
35032 /* Begin saving tokens. */
35033 cp_lexer_save_tokens (parser->lexer);
35034 /* In order to avoid repetitive access control error messages,
35035 access checks are queued up until we are no longer parsing
35036 tentatively. */
35037 push_deferring_access_checks (dk_deferred);
35040 /* Commit to the currently active tentative parse. */
35042 static void
35043 cp_parser_commit_to_tentative_parse (cp_parser* parser)
35045 cp_parser_context *context;
35046 cp_lexer *lexer;
35048 /* Mark all of the levels as committed. */
35049 lexer = parser->lexer;
35050 for (context = parser->context; context->next; context = context->next)
35052 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35053 break;
35054 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35055 while (!cp_lexer_saving_tokens (lexer))
35056 lexer = lexer->next;
35057 cp_lexer_commit_tokens (lexer);
35061 /* Commit to the topmost currently active tentative parse.
35063 Note that this function shouldn't be called when there are
35064 irreversible side-effects while in a tentative state. For
35065 example, we shouldn't create a permanent entry in the symbol
35066 table, or issue an error message that might not apply if the
35067 tentative parse is aborted. */
35069 static void
35070 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
35072 cp_parser_context *context = parser->context;
35073 cp_lexer *lexer = parser->lexer;
35075 if (context)
35077 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35078 return;
35079 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35081 while (!cp_lexer_saving_tokens (lexer))
35082 lexer = lexer->next;
35083 cp_lexer_commit_tokens (lexer);
35087 /* Abort the currently active tentative parse. All consumed tokens
35088 will be rolled back, and no diagnostics will be issued. */
35090 static void
35091 cp_parser_abort_tentative_parse (cp_parser* parser)
35093 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
35094 || errorcount > 0);
35095 cp_parser_simulate_error (parser);
35096 /* Now, pretend that we want to see if the construct was
35097 successfully parsed. */
35098 cp_parser_parse_definitely (parser);
35101 /* Stop parsing tentatively. If a parse error has occurred, restore the
35102 token stream. Otherwise, commit to the tokens we have consumed.
35103 Returns true if no error occurred; false otherwise. */
35105 static bool
35106 cp_parser_parse_definitely (cp_parser* parser)
35108 bool error_occurred;
35109 cp_parser_context *context;
35111 /* Remember whether or not an error occurred, since we are about to
35112 destroy that information. */
35113 error_occurred = cp_parser_error_occurred (parser);
35114 /* Remove the topmost context from the stack. */
35115 context = parser->context;
35116 parser->context = context->next;
35117 /* If no parse errors occurred, commit to the tentative parse. */
35118 if (!error_occurred)
35120 /* Commit to the tokens read tentatively, unless that was
35121 already done. */
35122 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
35123 cp_lexer_commit_tokens (parser->lexer);
35125 pop_to_parent_deferring_access_checks ();
35127 /* Otherwise, if errors occurred, roll back our state so that things
35128 are just as they were before we began the tentative parse. */
35129 else
35131 cp_lexer_rollback_tokens (parser->lexer);
35132 pop_deferring_access_checks ();
35134 /* Add the context to the front of the free list. */
35135 context->next = cp_parser_context_free_list;
35136 cp_parser_context_free_list = context;
35138 return !error_occurred;
35141 /* Returns true if we are parsing tentatively and are not committed to
35142 this tentative parse. */
35144 static bool
35145 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
35147 return (cp_parser_parsing_tentatively (parser)
35148 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
35151 /* Returns nonzero iff an error has occurred during the most recent
35152 tentative parse. */
35154 static bool
35155 cp_parser_error_occurred (cp_parser* parser)
35157 return (cp_parser_parsing_tentatively (parser)
35158 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
35161 /* Returns nonzero if GNU extensions are allowed. */
35163 static bool
35164 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
35166 return parser->allow_gnu_extensions_p;
35169 /* Objective-C++ Productions */
35172 /* Parse an Objective-C expression, which feeds into a primary-expression
35173 above.
35175 objc-expression:
35176 objc-message-expression
35177 objc-string-literal
35178 objc-encode-expression
35179 objc-protocol-expression
35180 objc-selector-expression
35182 Returns a tree representation of the expression. */
35184 static cp_expr
35185 cp_parser_objc_expression (cp_parser* parser)
35187 /* Try to figure out what kind of declaration is present. */
35188 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35190 switch (kwd->type)
35192 case CPP_OPEN_SQUARE:
35193 return cp_parser_objc_message_expression (parser);
35195 case CPP_OBJC_STRING:
35196 kwd = cp_lexer_consume_token (parser->lexer);
35197 return objc_build_string_object (kwd->u.value);
35199 case CPP_KEYWORD:
35200 switch (kwd->keyword)
35202 case RID_AT_ENCODE:
35203 return cp_parser_objc_encode_expression (parser);
35205 case RID_AT_PROTOCOL:
35206 return cp_parser_objc_protocol_expression (parser);
35208 case RID_AT_SELECTOR:
35209 return cp_parser_objc_selector_expression (parser);
35211 default:
35212 break;
35214 /* FALLTHRU */
35215 default:
35216 error_at (kwd->location,
35217 "misplaced %<@%D%> Objective-C++ construct",
35218 kwd->u.value);
35219 cp_parser_skip_to_end_of_block_or_statement (parser);
35222 return error_mark_node;
35225 /* Parse an Objective-C message expression.
35227 objc-message-expression:
35228 [ objc-message-receiver objc-message-args ]
35230 Returns a representation of an Objective-C message. */
35232 static tree
35233 cp_parser_objc_message_expression (cp_parser* parser)
35235 tree receiver, messageargs;
35237 parser->objective_c_message_context_p = true;
35238 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35239 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
35240 receiver = cp_parser_objc_message_receiver (parser);
35241 messageargs = cp_parser_objc_message_args (parser);
35242 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
35243 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
35245 tree result = objc_build_message_expr (receiver, messageargs);
35247 /* Construct a location e.g.
35248 [self func1:5]
35249 ^~~~~~~~~~~~~~
35250 ranging from the '[' to the ']', with the caret at the start. */
35251 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
35252 protected_set_expr_location (result, combined_loc);
35254 parser->objective_c_message_context_p = false;
35255 return result;
35258 /* Parse an objc-message-receiver.
35260 objc-message-receiver:
35261 expression
35262 simple-type-specifier
35264 Returns a representation of the type or expression. */
35266 static tree
35267 cp_parser_objc_message_receiver (cp_parser* parser)
35269 tree rcv;
35271 /* An Objective-C message receiver may be either (1) a type
35272 or (2) an expression. */
35273 cp_parser_parse_tentatively (parser);
35274 rcv = cp_parser_expression (parser);
35276 /* If that worked out, fine. */
35277 if (cp_parser_parse_definitely (parser))
35278 return rcv;
35280 cp_parser_parse_tentatively (parser);
35281 rcv = cp_parser_simple_type_specifier (parser,
35282 /*decl_specs=*/NULL,
35283 CP_PARSER_FLAGS_NONE);
35285 if (cp_parser_parse_definitely (parser))
35286 return objc_get_class_reference (rcv);
35288 cp_parser_error (parser, "objective-c++ message receiver expected");
35289 return error_mark_node;
35292 /* Parse the arguments and selectors comprising an Objective-C message.
35294 objc-message-args:
35295 objc-selector
35296 objc-selector-args
35297 objc-selector-args , objc-comma-args
35299 objc-selector-args:
35300 objc-selector [opt] : assignment-expression
35301 objc-selector-args objc-selector [opt] : assignment-expression
35303 objc-comma-args:
35304 assignment-expression
35305 objc-comma-args , assignment-expression
35307 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
35308 selector arguments and TREE_VALUE containing a list of comma
35309 arguments. */
35311 static tree
35312 cp_parser_objc_message_args (cp_parser* parser)
35314 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
35315 bool maybe_unary_selector_p = true;
35316 cp_token *token = cp_lexer_peek_token (parser->lexer);
35318 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35320 tree selector = NULL_TREE, arg;
35322 if (token->type != CPP_COLON)
35323 selector = cp_parser_objc_selector (parser);
35325 /* Detect if we have a unary selector. */
35326 if (maybe_unary_selector_p
35327 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35328 return build_tree_list (selector, NULL_TREE);
35330 maybe_unary_selector_p = false;
35331 cp_parser_require (parser, CPP_COLON, RT_COLON);
35332 arg = cp_parser_assignment_expression (parser);
35334 sel_args
35335 = chainon (sel_args,
35336 build_tree_list (selector, arg));
35338 token = cp_lexer_peek_token (parser->lexer);
35341 /* Handle non-selector arguments, if any. */
35342 while (token->type == CPP_COMMA)
35344 tree arg;
35346 cp_lexer_consume_token (parser->lexer);
35347 arg = cp_parser_assignment_expression (parser);
35349 addl_args
35350 = chainon (addl_args,
35351 build_tree_list (NULL_TREE, arg));
35353 token = cp_lexer_peek_token (parser->lexer);
35356 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
35358 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
35359 return build_tree_list (error_mark_node, error_mark_node);
35362 return build_tree_list (sel_args, addl_args);
35365 /* Parse an Objective-C encode expression.
35367 objc-encode-expression:
35368 @encode objc-typename
35370 Returns an encoded representation of the type argument. */
35372 static cp_expr
35373 cp_parser_objc_encode_expression (cp_parser* parser)
35375 tree type;
35376 cp_token *token;
35377 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35379 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
35380 matching_parens parens;
35381 parens.require_open (parser);
35382 token = cp_lexer_peek_token (parser->lexer);
35383 type = complete_type (cp_parser_type_id (parser));
35384 parens.require_close (parser);
35386 if (!type)
35388 error_at (token->location,
35389 "%<@encode%> must specify a type as an argument");
35390 return error_mark_node;
35393 /* This happens if we find @encode(T) (where T is a template
35394 typename or something dependent on a template typename) when
35395 parsing a template. In that case, we can't compile it
35396 immediately, but we rather create an AT_ENCODE_EXPR which will
35397 need to be instantiated when the template is used.
35399 if (dependent_type_p (type))
35401 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
35402 TREE_READONLY (value) = 1;
35403 return value;
35407 /* Build a location of the form:
35408 @encode(int)
35409 ^~~~~~~~~~~~
35410 with caret==start at the @ token, finishing at the close paren. */
35411 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35413 return cp_expr (objc_build_encode_expr (type), combined_loc);
35416 /* Parse an Objective-C @defs expression. */
35418 static tree
35419 cp_parser_objc_defs_expression (cp_parser *parser)
35421 tree name;
35423 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
35424 matching_parens parens;
35425 parens.require_open (parser);
35426 name = cp_parser_identifier (parser);
35427 parens.require_close (parser);
35429 return objc_get_class_ivars (name);
35432 /* Parse an Objective-C protocol expression.
35434 objc-protocol-expression:
35435 @protocol ( identifier )
35437 Returns a representation of the protocol expression. */
35439 static tree
35440 cp_parser_objc_protocol_expression (cp_parser* parser)
35442 tree proto;
35443 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35445 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
35446 matching_parens parens;
35447 parens.require_open (parser);
35448 proto = cp_parser_identifier (parser);
35449 parens.require_close (parser);
35451 /* Build a location of the form:
35452 @protocol(prot)
35453 ^~~~~~~~~~~~~~~
35454 with caret==start at the @ token, finishing at the close paren. */
35455 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35456 tree result = objc_build_protocol_expr (proto);
35457 protected_set_expr_location (result, combined_loc);
35458 return result;
35461 /* Parse an Objective-C selector expression.
35463 objc-selector-expression:
35464 @selector ( objc-method-signature )
35466 objc-method-signature:
35467 objc-selector
35468 objc-selector-seq
35470 objc-selector-seq:
35471 objc-selector :
35472 objc-selector-seq objc-selector :
35474 Returns a representation of the method selector. */
35476 static tree
35477 cp_parser_objc_selector_expression (cp_parser* parser)
35479 tree sel_seq = NULL_TREE;
35480 bool maybe_unary_selector_p = true;
35481 cp_token *token;
35482 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35484 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
35485 matching_parens parens;
35486 parens.require_open (parser);
35487 token = cp_lexer_peek_token (parser->lexer);
35489 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
35490 || token->type == CPP_SCOPE)
35492 tree selector = NULL_TREE;
35494 if (token->type != CPP_COLON
35495 || token->type == CPP_SCOPE)
35496 selector = cp_parser_objc_selector (parser);
35498 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
35499 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
35501 /* Detect if we have a unary selector. */
35502 if (maybe_unary_selector_p)
35504 sel_seq = selector;
35505 goto finish_selector;
35507 else
35509 cp_parser_error (parser, "expected %<:%>");
35512 maybe_unary_selector_p = false;
35513 token = cp_lexer_consume_token (parser->lexer);
35515 if (token->type == CPP_SCOPE)
35517 sel_seq
35518 = chainon (sel_seq,
35519 build_tree_list (selector, NULL_TREE));
35520 sel_seq
35521 = chainon (sel_seq,
35522 build_tree_list (NULL_TREE, NULL_TREE));
35524 else
35525 sel_seq
35526 = chainon (sel_seq,
35527 build_tree_list (selector, NULL_TREE));
35529 token = cp_lexer_peek_token (parser->lexer);
35532 finish_selector:
35533 parens.require_close (parser);
35536 /* Build a location of the form:
35537 @selector(func)
35538 ^~~~~~~~~~~~~~~
35539 with caret==start at the @ token, finishing at the close paren. */
35540 location_t combined_loc = make_location (loc, loc, parser->lexer);
35541 tree result = objc_build_selector_expr (combined_loc, sel_seq);
35542 /* TODO: objc_build_selector_expr doesn't always honor the location. */
35543 protected_set_expr_location (result, combined_loc);
35544 return result;
35547 /* Parse a list of identifiers.
35549 objc-identifier-list:
35550 identifier
35551 objc-identifier-list , identifier
35553 Returns a TREE_LIST of identifier nodes. */
35555 static tree
35556 cp_parser_objc_identifier_list (cp_parser* parser)
35558 tree identifier;
35559 tree list;
35560 cp_token *sep;
35562 identifier = cp_parser_identifier (parser);
35563 if (identifier == error_mark_node)
35564 return error_mark_node;
35566 list = build_tree_list (NULL_TREE, identifier);
35567 sep = cp_lexer_peek_token (parser->lexer);
35569 while (sep->type == CPP_COMMA)
35571 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35572 identifier = cp_parser_identifier (parser);
35573 if (identifier == error_mark_node)
35574 return list;
35576 list = chainon (list, build_tree_list (NULL_TREE,
35577 identifier));
35578 sep = cp_lexer_peek_token (parser->lexer);
35581 return list;
35584 /* Parse an Objective-C alias declaration.
35586 objc-alias-declaration:
35587 @compatibility_alias identifier identifier ;
35589 This function registers the alias mapping with the Objective-C front end.
35590 It returns nothing. */
35592 static void
35593 cp_parser_objc_alias_declaration (cp_parser* parser)
35595 tree alias, orig;
35597 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
35598 alias = cp_parser_identifier (parser);
35599 orig = cp_parser_identifier (parser);
35600 objc_declare_alias (alias, orig);
35601 cp_parser_consume_semicolon_at_end_of_statement (parser);
35604 /* Parse an Objective-C class forward-declaration.
35606 objc-class-declaration:
35607 @class objc-identifier-list ;
35609 The function registers the forward declarations with the Objective-C
35610 front end. It returns nothing. */
35612 static void
35613 cp_parser_objc_class_declaration (cp_parser* parser)
35615 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
35616 while (true)
35618 tree id;
35620 id = cp_parser_identifier (parser);
35621 if (id == error_mark_node)
35622 break;
35624 objc_declare_class (id);
35626 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35627 cp_lexer_consume_token (parser->lexer);
35628 else
35629 break;
35631 cp_parser_consume_semicolon_at_end_of_statement (parser);
35634 /* Parse a list of Objective-C protocol references.
35636 objc-protocol-refs-opt:
35637 objc-protocol-refs [opt]
35639 objc-protocol-refs:
35640 < objc-identifier-list >
35642 Returns a TREE_LIST of identifiers, if any. */
35644 static tree
35645 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
35647 tree protorefs = NULL_TREE;
35649 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
35651 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
35652 protorefs = cp_parser_objc_identifier_list (parser);
35653 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
35656 return protorefs;
35659 /* Parse a Objective-C visibility specification. */
35661 static void
35662 cp_parser_objc_visibility_spec (cp_parser* parser)
35664 cp_token *vis = cp_lexer_peek_token (parser->lexer);
35666 switch (vis->keyword)
35668 case RID_AT_PRIVATE:
35669 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
35670 break;
35671 case RID_AT_PROTECTED:
35672 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
35673 break;
35674 case RID_AT_PUBLIC:
35675 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
35676 break;
35677 case RID_AT_PACKAGE:
35678 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
35679 break;
35680 default:
35681 return;
35684 /* Eat '@private'/'@protected'/'@public'. */
35685 cp_lexer_consume_token (parser->lexer);
35688 /* Parse an Objective-C method type. Return 'true' if it is a class
35689 (+) method, and 'false' if it is an instance (-) method. */
35691 static inline bool
35692 cp_parser_objc_method_type (cp_parser* parser)
35694 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
35695 return true;
35696 else
35697 return false;
35700 /* Parse an Objective-C protocol qualifier. */
35702 static tree
35703 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
35705 tree quals = NULL_TREE, node;
35706 cp_token *token = cp_lexer_peek_token (parser->lexer);
35708 node = token->u.value;
35710 while (node && identifier_p (node)
35711 && (node == ridpointers [(int) RID_IN]
35712 || node == ridpointers [(int) RID_OUT]
35713 || node == ridpointers [(int) RID_INOUT]
35714 || node == ridpointers [(int) RID_BYCOPY]
35715 || node == ridpointers [(int) RID_BYREF]
35716 || node == ridpointers [(int) RID_ONEWAY]))
35718 quals = tree_cons (NULL_TREE, node, quals);
35719 cp_lexer_consume_token (parser->lexer);
35720 token = cp_lexer_peek_token (parser->lexer);
35721 node = token->u.value;
35724 return quals;
35727 /* Parse an Objective-C typename. */
35729 static tree
35730 cp_parser_objc_typename (cp_parser* parser)
35732 tree type_name = NULL_TREE;
35734 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35736 tree proto_quals, cp_type = NULL_TREE;
35738 matching_parens parens;
35739 parens.consume_open (parser); /* Eat '('. */
35740 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
35742 /* An ObjC type name may consist of just protocol qualifiers, in which
35743 case the type shall default to 'id'. */
35744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35746 cp_type = cp_parser_type_id (parser);
35748 /* If the type could not be parsed, an error has already
35749 been produced. For error recovery, behave as if it had
35750 not been specified, which will use the default type
35751 'id'. */
35752 if (cp_type == error_mark_node)
35754 cp_type = NULL_TREE;
35755 /* We need to skip to the closing parenthesis as
35756 cp_parser_type_id() does not seem to do it for
35757 us. */
35758 cp_parser_skip_to_closing_parenthesis (parser,
35759 /*recovering=*/true,
35760 /*or_comma=*/false,
35761 /*consume_paren=*/false);
35765 parens.require_close (parser);
35766 type_name = build_tree_list (proto_quals, cp_type);
35769 return type_name;
35772 /* Check to see if TYPE refers to an Objective-C selector name. */
35774 static bool
35775 cp_parser_objc_selector_p (enum cpp_ttype type)
35777 return (type == CPP_NAME || type == CPP_KEYWORD
35778 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
35779 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
35780 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
35781 || type == CPP_XOR || type == CPP_XOR_EQ);
35784 /* Parse an Objective-C selector. */
35786 static tree
35787 cp_parser_objc_selector (cp_parser* parser)
35789 cp_token *token = cp_lexer_consume_token (parser->lexer);
35791 if (!cp_parser_objc_selector_p (token->type))
35793 error_at (token->location, "invalid Objective-C++ selector name");
35794 return error_mark_node;
35797 /* C++ operator names are allowed to appear in ObjC selectors. */
35798 switch (token->type)
35800 case CPP_AND_AND: return get_identifier ("and");
35801 case CPP_AND_EQ: return get_identifier ("and_eq");
35802 case CPP_AND: return get_identifier ("bitand");
35803 case CPP_OR: return get_identifier ("bitor");
35804 case CPP_COMPL: return get_identifier ("compl");
35805 case CPP_NOT: return get_identifier ("not");
35806 case CPP_NOT_EQ: return get_identifier ("not_eq");
35807 case CPP_OR_OR: return get_identifier ("or");
35808 case CPP_OR_EQ: return get_identifier ("or_eq");
35809 case CPP_XOR: return get_identifier ("xor");
35810 case CPP_XOR_EQ: return get_identifier ("xor_eq");
35811 default: return token->u.value;
35815 /* Parse an Objective-C params list. */
35817 static tree
35818 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
35820 tree params = NULL_TREE;
35821 bool maybe_unary_selector_p = true;
35822 cp_token *token = cp_lexer_peek_token (parser->lexer);
35824 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35826 tree selector = NULL_TREE, type_name, identifier;
35827 tree parm_attr = NULL_TREE;
35829 if (token->keyword == RID_ATTRIBUTE)
35830 break;
35832 if (token->type != CPP_COLON)
35833 selector = cp_parser_objc_selector (parser);
35835 /* Detect if we have a unary selector. */
35836 if (maybe_unary_selector_p
35837 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35839 params = selector; /* Might be followed by attributes. */
35840 break;
35843 maybe_unary_selector_p = false;
35844 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35846 /* Something went quite wrong. There should be a colon
35847 here, but there is not. Stop parsing parameters. */
35848 break;
35850 type_name = cp_parser_objc_typename (parser);
35851 /* New ObjC allows attributes on parameters too. */
35852 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
35853 parm_attr = cp_parser_attributes_opt (parser);
35854 identifier = cp_parser_identifier (parser);
35856 params
35857 = chainon (params,
35858 objc_build_keyword_decl (selector,
35859 type_name,
35860 identifier,
35861 parm_attr));
35863 token = cp_lexer_peek_token (parser->lexer);
35866 if (params == NULL_TREE)
35868 cp_parser_error (parser, "objective-c++ method declaration is expected");
35869 return error_mark_node;
35872 /* We allow tail attributes for the method. */
35873 if (token->keyword == RID_ATTRIBUTE)
35875 *attributes = cp_parser_attributes_opt (parser);
35876 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35877 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35878 return params;
35879 cp_parser_error (parser,
35880 "method attributes must be specified at the end");
35881 return error_mark_node;
35884 if (params == NULL_TREE)
35886 cp_parser_error (parser, "objective-c++ method declaration is expected");
35887 return error_mark_node;
35889 return params;
35892 /* Parse the non-keyword Objective-C params. */
35894 static tree
35895 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
35896 tree* attributes)
35898 tree params = make_node (TREE_LIST);
35899 cp_token *token = cp_lexer_peek_token (parser->lexer);
35900 *ellipsisp = false; /* Initially, assume no ellipsis. */
35902 while (token->type == CPP_COMMA)
35904 cp_parameter_declarator *parmdecl;
35905 tree parm;
35907 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35908 token = cp_lexer_peek_token (parser->lexer);
35910 if (token->type == CPP_ELLIPSIS)
35912 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
35913 *ellipsisp = true;
35914 token = cp_lexer_peek_token (parser->lexer);
35915 break;
35918 /* TODO: parse attributes for tail parameters. */
35919 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35920 false, NULL);
35921 parm = grokdeclarator (parmdecl->declarator,
35922 &parmdecl->decl_specifiers,
35923 PARM, /*initialized=*/0,
35924 /*attrlist=*/NULL);
35926 chainon (params, build_tree_list (NULL_TREE, parm));
35927 token = cp_lexer_peek_token (parser->lexer);
35930 /* We allow tail attributes for the method. */
35931 if (token->keyword == RID_ATTRIBUTE)
35933 if (*attributes == NULL_TREE)
35935 *attributes = cp_parser_attributes_opt (parser);
35936 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35937 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35938 return params;
35940 else
35941 /* We have an error, but parse the attributes, so that we can
35942 carry on. */
35943 *attributes = cp_parser_attributes_opt (parser);
35945 cp_parser_error (parser,
35946 "method attributes must be specified at the end");
35947 return error_mark_node;
35950 return params;
35953 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
35955 static void
35956 cp_parser_objc_interstitial_code (cp_parser* parser)
35958 cp_token *token = cp_lexer_peek_token (parser->lexer);
35960 /* If the next token is `extern' and the following token is a string
35961 literal, then we have a linkage specification. */
35962 if (token->keyword == RID_EXTERN
35963 && cp_parser_is_pure_string_literal
35964 (cp_lexer_peek_nth_token (parser->lexer, 2)))
35965 cp_parser_linkage_specification (parser, NULL_TREE);
35966 /* Handle #pragma, if any. */
35967 else if (token->type == CPP_PRAGMA)
35968 cp_parser_pragma (parser, pragma_objc_icode, NULL);
35969 /* Allow stray semicolons. */
35970 else if (token->type == CPP_SEMICOLON)
35971 cp_lexer_consume_token (parser->lexer);
35972 /* Mark methods as optional or required, when building protocols. */
35973 else if (token->keyword == RID_AT_OPTIONAL)
35975 cp_lexer_consume_token (parser->lexer);
35976 objc_set_method_opt (true);
35978 else if (token->keyword == RID_AT_REQUIRED)
35980 cp_lexer_consume_token (parser->lexer);
35981 objc_set_method_opt (false);
35983 else if (token->keyword == RID_NAMESPACE)
35984 cp_parser_namespace_definition (parser);
35985 /* Other stray characters must generate errors. */
35986 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
35988 cp_lexer_consume_token (parser->lexer);
35989 error ("stray %qs between Objective-C++ methods",
35990 token->type == CPP_OPEN_BRACE ? "{" : "}");
35992 /* Finally, try to parse a block-declaration, or a function-definition. */
35993 else
35994 cp_parser_block_declaration (parser, /*statement_p=*/false);
35997 /* Parse a method signature. */
35999 static tree
36000 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
36002 tree rettype, kwdparms, optparms;
36003 bool ellipsis = false;
36004 bool is_class_method;
36006 is_class_method = cp_parser_objc_method_type (parser);
36007 rettype = cp_parser_objc_typename (parser);
36008 *attributes = NULL_TREE;
36009 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
36010 if (kwdparms == error_mark_node)
36011 return error_mark_node;
36012 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
36013 if (optparms == error_mark_node)
36014 return error_mark_node;
36016 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
36019 static bool
36020 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
36022 tree tattr;
36023 cp_lexer_save_tokens (parser->lexer);
36024 tattr = cp_parser_attributes_opt (parser);
36025 gcc_assert (tattr) ;
36027 /* If the attributes are followed by a method introducer, this is not allowed.
36028 Dump the attributes and flag the situation. */
36029 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
36030 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36031 return true;
36033 /* Otherwise, the attributes introduce some interstitial code, possibly so
36034 rewind to allow that check. */
36035 cp_lexer_rollback_tokens (parser->lexer);
36036 return false;
36039 /* Parse an Objective-C method prototype list. */
36041 static void
36042 cp_parser_objc_method_prototype_list (cp_parser* parser)
36044 cp_token *token = cp_lexer_peek_token (parser->lexer);
36046 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
36048 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36050 tree attributes, sig;
36051 bool is_class_method;
36052 if (token->type == CPP_PLUS)
36053 is_class_method = true;
36054 else
36055 is_class_method = false;
36056 sig = cp_parser_objc_method_signature (parser, &attributes);
36057 if (sig == error_mark_node)
36059 cp_parser_skip_to_end_of_block_or_statement (parser);
36060 token = cp_lexer_peek_token (parser->lexer);
36061 continue;
36063 objc_add_method_declaration (is_class_method, sig, attributes);
36064 cp_parser_consume_semicolon_at_end_of_statement (parser);
36066 else if (token->keyword == RID_AT_PROPERTY)
36067 cp_parser_objc_at_property_declaration (parser);
36068 else if (token->keyword == RID_ATTRIBUTE
36069 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36070 warning_at (cp_lexer_peek_token (parser->lexer)->location,
36071 OPT_Wattributes,
36072 "prefix attributes are ignored for methods");
36073 else
36074 /* Allow for interspersed non-ObjC++ code. */
36075 cp_parser_objc_interstitial_code (parser);
36077 token = cp_lexer_peek_token (parser->lexer);
36080 if (token->type != CPP_EOF)
36081 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36082 else
36083 cp_parser_error (parser, "expected %<@end%>");
36085 objc_finish_interface ();
36088 /* Parse an Objective-C method definition list. */
36090 static void
36091 cp_parser_objc_method_definition_list (cp_parser* parser)
36093 for (;;)
36095 cp_token *token = cp_lexer_peek_token (parser->lexer);
36097 if (token->keyword == RID_AT_END)
36099 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36100 break;
36102 else if (token->type == CPP_EOF)
36104 cp_parser_error (parser, "expected %<@end%>");
36105 break;
36107 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36109 bool is_class_method = token->type == CPP_PLUS;
36111 push_deferring_access_checks (dk_deferred);
36112 tree attribute;
36113 tree sig = cp_parser_objc_method_signature (parser, &attribute);
36114 if (sig == error_mark_node)
36115 cp_parser_skip_to_end_of_block_or_statement (parser);
36116 else
36118 objc_start_method_definition (is_class_method, sig,
36119 attribute, NULL_TREE);
36121 /* For historical reasons, we accept an optional semicolon. */
36122 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36123 cp_lexer_consume_token (parser->lexer);
36125 perform_deferred_access_checks (tf_warning_or_error);
36126 stop_deferring_access_checks ();
36127 tree meth
36128 = cp_parser_function_definition_after_declarator (parser, false);
36129 pop_deferring_access_checks ();
36130 objc_finish_method_definition (meth);
36133 /* The following case will be removed once @synthesize is
36134 completely implemented. */
36135 else if (token->keyword == RID_AT_PROPERTY)
36136 cp_parser_objc_at_property_declaration (parser);
36137 else if (token->keyword == RID_AT_SYNTHESIZE)
36138 cp_parser_objc_at_synthesize_declaration (parser);
36139 else if (token->keyword == RID_AT_DYNAMIC)
36140 cp_parser_objc_at_dynamic_declaration (parser);
36141 else if (token->keyword == RID_ATTRIBUTE
36142 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36143 warning_at (token->location, OPT_Wattributes,
36144 "prefix attributes are ignored for methods");
36145 else
36146 /* Allow for interspersed non-ObjC++ code. */
36147 cp_parser_objc_interstitial_code (parser);
36150 objc_finish_implementation ();
36153 /* Parse Objective-C ivars. */
36155 static void
36156 cp_parser_objc_class_ivars (cp_parser* parser)
36158 cp_token *token = cp_lexer_peek_token (parser->lexer);
36160 if (token->type != CPP_OPEN_BRACE)
36161 return; /* No ivars specified. */
36163 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
36164 token = cp_lexer_peek_token (parser->lexer);
36166 while (token->type != CPP_CLOSE_BRACE
36167 && token->keyword != RID_AT_END && token->type != CPP_EOF)
36169 cp_decl_specifier_seq declspecs;
36170 int decl_class_or_enum_p;
36171 tree prefix_attributes;
36173 cp_parser_objc_visibility_spec (parser);
36175 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36176 break;
36178 cp_parser_decl_specifier_seq (parser,
36179 CP_PARSER_FLAGS_OPTIONAL,
36180 &declspecs,
36181 &decl_class_or_enum_p);
36183 /* auto, register, static, extern, mutable. */
36184 if (declspecs.storage_class != sc_none)
36186 cp_parser_error (parser, "invalid type for instance variable");
36187 declspecs.storage_class = sc_none;
36190 /* thread_local. */
36191 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36193 cp_parser_error (parser, "invalid type for instance variable");
36194 declspecs.locations[ds_thread] = 0;
36197 /* typedef. */
36198 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36200 cp_parser_error (parser, "invalid type for instance variable");
36201 declspecs.locations[ds_typedef] = 0;
36204 prefix_attributes = declspecs.attributes;
36205 declspecs.attributes = NULL_TREE;
36207 /* Keep going until we hit the `;' at the end of the
36208 declaration. */
36209 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36211 tree width = NULL_TREE, attributes, first_attribute, decl;
36212 cp_declarator *declarator = NULL;
36213 int ctor_dtor_or_conv_p;
36215 /* Check for a (possibly unnamed) bitfield declaration. */
36216 token = cp_lexer_peek_token (parser->lexer);
36217 if (token->type == CPP_COLON)
36218 goto eat_colon;
36220 if (token->type == CPP_NAME
36221 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
36222 == CPP_COLON))
36224 /* Get the name of the bitfield. */
36225 declarator = make_id_declarator (NULL_TREE,
36226 cp_parser_identifier (parser),
36227 sfk_none, token->location);
36229 eat_colon:
36230 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36231 /* Get the width of the bitfield. */
36232 width
36233 = cp_parser_constant_expression (parser);
36235 else
36237 /* Parse the declarator. */
36238 declarator
36239 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36240 CP_PARSER_FLAGS_NONE,
36241 &ctor_dtor_or_conv_p,
36242 /*parenthesized_p=*/NULL,
36243 /*member_p=*/false,
36244 /*friend_p=*/false,
36245 /*static_p=*/false);
36248 /* Look for attributes that apply to the ivar. */
36249 attributes = cp_parser_attributes_opt (parser);
36250 /* Remember which attributes are prefix attributes and
36251 which are not. */
36252 first_attribute = attributes;
36253 /* Combine the attributes. */
36254 attributes = attr_chainon (prefix_attributes, attributes);
36256 if (width)
36257 /* Create the bitfield declaration. */
36258 decl = grokbitfield (declarator, &declspecs,
36259 width, NULL_TREE, attributes);
36260 else
36261 decl = grokfield (declarator, &declspecs,
36262 NULL_TREE, /*init_const_expr_p=*/false,
36263 NULL_TREE, attributes);
36265 /* Add the instance variable. */
36266 if (decl != error_mark_node && decl != NULL_TREE)
36267 objc_add_instance_variable (decl);
36269 /* Reset PREFIX_ATTRIBUTES. */
36270 if (attributes != error_mark_node)
36272 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36273 attributes = TREE_CHAIN (attributes);
36274 if (attributes)
36275 TREE_CHAIN (attributes) = NULL_TREE;
36278 token = cp_lexer_peek_token (parser->lexer);
36280 if (token->type == CPP_COMMA)
36282 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36283 continue;
36285 break;
36288 cp_parser_consume_semicolon_at_end_of_statement (parser);
36289 token = cp_lexer_peek_token (parser->lexer);
36292 if (token->keyword == RID_AT_END)
36293 cp_parser_error (parser, "expected %<}%>");
36295 /* Do not consume the RID_AT_END, so it will be read again as terminating
36296 the @interface of @implementation. */
36297 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
36298 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
36300 /* For historical reasons, we accept an optional semicolon. */
36301 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36302 cp_lexer_consume_token (parser->lexer);
36305 /* Parse an Objective-C protocol declaration. */
36307 static void
36308 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
36310 tree proto, protorefs;
36311 cp_token *tok;
36313 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36314 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36316 tok = cp_lexer_peek_token (parser->lexer);
36317 error_at (tok->location, "identifier expected after %<@protocol%>");
36318 cp_parser_consume_semicolon_at_end_of_statement (parser);
36319 return;
36322 /* See if we have a forward declaration or a definition. */
36323 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
36325 /* Try a forward declaration first. */
36326 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
36328 while (true)
36330 tree id;
36332 id = cp_parser_identifier (parser);
36333 if (id == error_mark_node)
36334 break;
36336 objc_declare_protocol (id, attributes);
36338 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36339 cp_lexer_consume_token (parser->lexer);
36340 else
36341 break;
36343 cp_parser_consume_semicolon_at_end_of_statement (parser);
36346 /* Ok, we got a full-fledged definition (or at least should). */
36347 else
36349 proto = cp_parser_identifier (parser);
36350 protorefs = cp_parser_objc_protocol_refs_opt (parser);
36351 objc_start_protocol (proto, protorefs, attributes);
36352 cp_parser_objc_method_prototype_list (parser);
36356 /* Parse an Objective-C superclass or category. */
36358 static void
36359 cp_parser_objc_superclass_or_category (cp_parser *parser,
36360 bool iface_p,
36361 tree *super,
36362 tree *categ, bool *is_class_extension)
36364 cp_token *next = cp_lexer_peek_token (parser->lexer);
36366 *super = *categ = NULL_TREE;
36367 *is_class_extension = false;
36368 if (next->type == CPP_COLON)
36370 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36371 *super = cp_parser_identifier (parser);
36373 else if (next->type == CPP_OPEN_PAREN)
36375 matching_parens parens;
36376 parens.consume_open (parser); /* Eat '('. */
36378 /* If there is no category name, and this is an @interface, we
36379 have a class extension. */
36380 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36382 *categ = NULL_TREE;
36383 *is_class_extension = true;
36385 else
36386 *categ = cp_parser_identifier (parser);
36388 parens.require_close (parser);
36392 /* Parse an Objective-C class interface. */
36394 static void
36395 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
36397 tree name, super, categ, protos;
36398 bool is_class_extension;
36400 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
36401 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
36402 name = cp_parser_identifier (parser);
36403 if (name == error_mark_node)
36405 /* It's hard to recover because even if valid @interface stuff
36406 is to follow, we can't compile it (or validate it) if we
36407 don't even know which class it refers to. Let's assume this
36408 was a stray '@interface' token in the stream and skip it.
36410 return;
36412 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
36413 &is_class_extension);
36414 protos = cp_parser_objc_protocol_refs_opt (parser);
36416 /* We have either a class or a category on our hands. */
36417 if (categ || is_class_extension)
36418 objc_start_category_interface (name, categ, protos, attributes);
36419 else
36421 objc_start_class_interface (name, nam_loc, super, protos, attributes);
36422 /* Handle instance variable declarations, if any. */
36423 cp_parser_objc_class_ivars (parser);
36424 objc_continue_interface ();
36427 cp_parser_objc_method_prototype_list (parser);
36430 /* Parse an Objective-C class implementation. */
36432 static void
36433 cp_parser_objc_class_implementation (cp_parser* parser)
36435 tree name, super, categ;
36436 bool is_class_extension;
36438 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
36439 name = cp_parser_identifier (parser);
36440 if (name == error_mark_node)
36442 /* It's hard to recover because even if valid @implementation
36443 stuff is to follow, we can't compile it (or validate it) if
36444 we don't even know which class it refers to. Let's assume
36445 this was a stray '@implementation' token in the stream and
36446 skip it.
36448 return;
36450 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
36451 &is_class_extension);
36453 /* We have either a class or a category on our hands. */
36454 if (categ)
36455 objc_start_category_implementation (name, categ);
36456 else
36458 objc_start_class_implementation (name, super);
36459 /* Handle instance variable declarations, if any. */
36460 cp_parser_objc_class_ivars (parser);
36461 objc_continue_implementation ();
36464 cp_parser_objc_method_definition_list (parser);
36467 /* Consume the @end token and finish off the implementation. */
36469 static void
36470 cp_parser_objc_end_implementation (cp_parser* parser)
36472 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36473 objc_finish_implementation ();
36476 /* Parse an Objective-C declaration. */
36478 static void
36479 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
36481 /* Try to figure out what kind of declaration is present. */
36482 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36484 if (attributes)
36485 switch (kwd->keyword)
36487 case RID_AT_ALIAS:
36488 case RID_AT_CLASS:
36489 case RID_AT_END:
36490 error_at (kwd->location, "attributes may not be specified before"
36491 " the %<@%D%> Objective-C++ keyword",
36492 kwd->u.value);
36493 attributes = NULL;
36494 break;
36495 case RID_AT_IMPLEMENTATION:
36496 warning_at (kwd->location, OPT_Wattributes,
36497 "prefix attributes are ignored before %<@%D%>",
36498 kwd->u.value);
36499 attributes = NULL;
36500 default:
36501 break;
36504 switch (kwd->keyword)
36506 case RID_AT_ALIAS:
36507 cp_parser_objc_alias_declaration (parser);
36508 break;
36509 case RID_AT_CLASS:
36510 cp_parser_objc_class_declaration (parser);
36511 break;
36512 case RID_AT_PROTOCOL:
36513 cp_parser_objc_protocol_declaration (parser, attributes);
36514 break;
36515 case RID_AT_INTERFACE:
36516 cp_parser_objc_class_interface (parser, attributes);
36517 break;
36518 case RID_AT_IMPLEMENTATION:
36519 cp_parser_objc_class_implementation (parser);
36520 break;
36521 case RID_AT_END:
36522 cp_parser_objc_end_implementation (parser);
36523 break;
36524 default:
36525 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36526 kwd->u.value);
36527 cp_parser_skip_to_end_of_block_or_statement (parser);
36531 /* Parse an Objective-C try-catch-finally statement.
36533 objc-try-catch-finally-stmt:
36534 @try compound-statement objc-catch-clause-seq [opt]
36535 objc-finally-clause [opt]
36537 objc-catch-clause-seq:
36538 objc-catch-clause objc-catch-clause-seq [opt]
36540 objc-catch-clause:
36541 @catch ( objc-exception-declaration ) compound-statement
36543 objc-finally-clause:
36544 @finally compound-statement
36546 objc-exception-declaration:
36547 parameter-declaration
36548 '...'
36550 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
36552 Returns NULL_TREE.
36554 PS: This function is identical to c_parser_objc_try_catch_finally_statement
36555 for C. Keep them in sync. */
36557 static tree
36558 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
36560 location_t location;
36561 tree stmt;
36563 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
36564 location = cp_lexer_peek_token (parser->lexer)->location;
36565 objc_maybe_warn_exceptions (location);
36566 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
36567 node, lest it get absorbed into the surrounding block. */
36568 stmt = push_stmt_list ();
36569 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36570 objc_begin_try_stmt (location, pop_stmt_list (stmt));
36572 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
36574 cp_parameter_declarator *parm;
36575 tree parameter_declaration = error_mark_node;
36576 bool seen_open_paren = false;
36577 matching_parens parens;
36579 cp_lexer_consume_token (parser->lexer);
36580 if (parens.require_open (parser))
36581 seen_open_paren = true;
36582 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
36584 /* We have "@catch (...)" (where the '...' are literally
36585 what is in the code). Skip the '...'.
36586 parameter_declaration is set to NULL_TREE, and
36587 objc_being_catch_clauses() knows that that means
36588 '...'. */
36589 cp_lexer_consume_token (parser->lexer);
36590 parameter_declaration = NULL_TREE;
36592 else
36594 /* We have "@catch (NSException *exception)" or something
36595 like that. Parse the parameter declaration. */
36596 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36597 false, NULL);
36598 if (parm == NULL)
36599 parameter_declaration = error_mark_node;
36600 else
36601 parameter_declaration = grokdeclarator (parm->declarator,
36602 &parm->decl_specifiers,
36603 PARM, /*initialized=*/0,
36604 /*attrlist=*/NULL);
36606 if (seen_open_paren)
36607 parens.require_close (parser);
36608 else
36610 /* If there was no open parenthesis, we are recovering from
36611 an error, and we are trying to figure out what mistake
36612 the user has made. */
36614 /* If there is an immediate closing parenthesis, the user
36615 probably forgot the opening one (ie, they typed "@catch
36616 NSException *e)". Parse the closing parenthesis and keep
36617 going. */
36618 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36619 cp_lexer_consume_token (parser->lexer);
36621 /* If these is no immediate closing parenthesis, the user
36622 probably doesn't know that parenthesis are required at
36623 all (ie, they typed "@catch NSException *e"). So, just
36624 forget about the closing parenthesis and keep going. */
36626 objc_begin_catch_clause (parameter_declaration);
36627 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36628 objc_finish_catch_clause ();
36630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
36632 cp_lexer_consume_token (parser->lexer);
36633 location = cp_lexer_peek_token (parser->lexer)->location;
36634 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
36635 node, lest it get absorbed into the surrounding block. */
36636 stmt = push_stmt_list ();
36637 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36638 objc_build_finally_clause (location, pop_stmt_list (stmt));
36641 return objc_finish_try_stmt ();
36644 /* Parse an Objective-C synchronized statement.
36646 objc-synchronized-stmt:
36647 @synchronized ( expression ) compound-statement
36649 Returns NULL_TREE. */
36651 static tree
36652 cp_parser_objc_synchronized_statement (cp_parser *parser)
36654 location_t location;
36655 tree lock, stmt;
36657 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
36659 location = cp_lexer_peek_token (parser->lexer)->location;
36660 objc_maybe_warn_exceptions (location);
36661 matching_parens parens;
36662 parens.require_open (parser);
36663 lock = cp_parser_expression (parser);
36664 parens.require_close (parser);
36666 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
36667 node, lest it get absorbed into the surrounding block. */
36668 stmt = push_stmt_list ();
36669 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36671 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
36674 /* Parse an Objective-C throw statement.
36676 objc-throw-stmt:
36677 @throw assignment-expression [opt] ;
36679 Returns a constructed '@throw' statement. */
36681 static tree
36682 cp_parser_objc_throw_statement (cp_parser *parser)
36684 tree expr = NULL_TREE;
36685 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36687 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
36689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36690 expr = cp_parser_expression (parser);
36692 cp_parser_consume_semicolon_at_end_of_statement (parser);
36694 return objc_build_throw_stmt (loc, expr);
36697 /* Parse an Objective-C statement. */
36699 static tree
36700 cp_parser_objc_statement (cp_parser * parser)
36702 /* Try to figure out what kind of declaration is present. */
36703 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36705 switch (kwd->keyword)
36707 case RID_AT_TRY:
36708 return cp_parser_objc_try_catch_finally_statement (parser);
36709 case RID_AT_SYNCHRONIZED:
36710 return cp_parser_objc_synchronized_statement (parser);
36711 case RID_AT_THROW:
36712 return cp_parser_objc_throw_statement (parser);
36713 default:
36714 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36715 kwd->u.value);
36716 cp_parser_skip_to_end_of_block_or_statement (parser);
36719 return error_mark_node;
36722 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
36723 look ahead to see if an objc keyword follows the attributes. This
36724 is to detect the use of prefix attributes on ObjC @interface and
36725 @protocol. */
36727 static bool
36728 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
36730 cp_lexer_save_tokens (parser->lexer);
36731 tree addon = cp_parser_attributes_opt (parser);
36732 if (addon
36733 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
36735 cp_lexer_commit_tokens (parser->lexer);
36736 if (*attrib)
36737 TREE_CHAIN (*attrib) = addon;
36738 else
36739 *attrib = addon;
36740 return true;
36742 cp_lexer_rollback_tokens (parser->lexer);
36743 return false;
36746 /* This routine is a minimal replacement for
36747 c_parser_struct_declaration () used when parsing the list of
36748 types/names or ObjC++ properties. For example, when parsing the
36749 code
36751 @property (readonly) int a, b, c;
36753 this function is responsible for parsing "int a, int b, int c" and
36754 returning the declarations as CHAIN of DECLs.
36756 TODO: Share this code with cp_parser_objc_class_ivars. It's very
36757 similar parsing. */
36758 static tree
36759 cp_parser_objc_struct_declaration (cp_parser *parser)
36761 tree decls = NULL_TREE;
36762 cp_decl_specifier_seq declspecs;
36763 int decl_class_or_enum_p;
36764 tree prefix_attributes;
36766 cp_parser_decl_specifier_seq (parser,
36767 CP_PARSER_FLAGS_NONE,
36768 &declspecs,
36769 &decl_class_or_enum_p);
36771 if (declspecs.type == error_mark_node)
36772 return error_mark_node;
36774 /* auto, register, static, extern, mutable. */
36775 if (declspecs.storage_class != sc_none)
36777 cp_parser_error (parser, "invalid type for property");
36778 declspecs.storage_class = sc_none;
36781 /* thread_local. */
36782 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36784 cp_parser_error (parser, "invalid type for property");
36785 declspecs.locations[ds_thread] = 0;
36788 /* typedef. */
36789 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36791 cp_parser_error (parser, "invalid type for property");
36792 declspecs.locations[ds_typedef] = 0;
36795 prefix_attributes = declspecs.attributes;
36796 declspecs.attributes = NULL_TREE;
36798 /* Keep going until we hit the `;' at the end of the declaration. */
36799 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36801 tree attributes, first_attribute, decl;
36802 cp_declarator *declarator;
36803 cp_token *token;
36805 /* Parse the declarator. */
36806 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36807 CP_PARSER_FLAGS_NONE,
36808 NULL, NULL, false, false, false);
36810 /* Look for attributes that apply to the ivar. */
36811 attributes = cp_parser_attributes_opt (parser);
36812 /* Remember which attributes are prefix attributes and
36813 which are not. */
36814 first_attribute = attributes;
36815 /* Combine the attributes. */
36816 attributes = attr_chainon (prefix_attributes, attributes);
36818 decl = grokfield (declarator, &declspecs,
36819 NULL_TREE, /*init_const_expr_p=*/false,
36820 NULL_TREE, attributes);
36822 if (decl == error_mark_node || decl == NULL_TREE)
36823 return error_mark_node;
36825 /* Reset PREFIX_ATTRIBUTES. */
36826 if (attributes != error_mark_node)
36828 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36829 attributes = TREE_CHAIN (attributes);
36830 if (attributes)
36831 TREE_CHAIN (attributes) = NULL_TREE;
36834 DECL_CHAIN (decl) = decls;
36835 decls = decl;
36837 token = cp_lexer_peek_token (parser->lexer);
36838 if (token->type == CPP_COMMA)
36840 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36841 continue;
36843 else
36844 break;
36846 return decls;
36849 /* Parse an Objective-C @property declaration. The syntax is:
36851 objc-property-declaration:
36852 '@property' objc-property-attributes[opt] struct-declaration ;
36854 objc-property-attributes:
36855 '(' objc-property-attribute-list ')'
36857 objc-property-attribute-list:
36858 objc-property-attribute
36859 objc-property-attribute-list, objc-property-attribute
36861 objc-property-attribute
36862 'getter' = identifier
36863 'setter' = identifier
36864 'readonly'
36865 'readwrite'
36866 'assign'
36867 'retain'
36868 'copy'
36869 'nonatomic'
36871 For example:
36872 @property NSString *name;
36873 @property (readonly) id object;
36874 @property (retain, nonatomic, getter=getTheName) id name;
36875 @property int a, b, c;
36877 PS: This function is identical to
36878 c_parser_objc_at_property_declaration for C. Keep them in sync. */
36879 static void
36880 cp_parser_objc_at_property_declaration (cp_parser *parser)
36882 /* Parse the optional attribute list.
36884 A list of parsed, but not verified, attributes. */
36885 auto_delete_vec<property_attribute_info> prop_attr_list;
36886 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36888 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
36890 /* Parse the optional attribute list... */
36891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36893 /* Eat the '('. */
36894 matching_parens parens;
36895 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
36896 parens.consume_open (parser);
36897 bool syntax_error = false;
36899 /* Allow empty @property attribute lists, but with a warning. */
36900 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
36901 location_t attr_comb;
36902 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36904 attr_comb = make_location (attr_end, attr_start, attr_end);
36905 warning_at (attr_comb, OPT_Wattributes,
36906 "empty property attribute list");
36908 else
36909 while (true)
36911 cp_token *token = cp_lexer_peek_token (parser->lexer);
36912 attr_start = token->location;
36913 attr_end = get_finish (token->location);
36914 attr_comb = make_location (attr_start, attr_start, attr_end);
36916 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
36918 warning_at (attr_comb, OPT_Wattributes,
36919 "missing property attribute");
36920 if (token->type == CPP_CLOSE_PAREN)
36921 break;
36922 cp_lexer_consume_token (parser->lexer);
36923 continue;
36926 tree attr_name = NULL_TREE;
36927 if (identifier_p (token->u.value))
36928 attr_name = token->u.value;
36930 enum rid keyword;
36931 if (token->type == CPP_NAME)
36932 keyword = C_RID_CODE (token->u.value);
36933 else if (token->type == CPP_KEYWORD
36934 && token->keyword == RID_CLASS)
36935 /* Account for accepting the 'class' keyword in this context. */
36936 keyword = RID_CLASS;
36937 else
36938 keyword = RID_MAX; /* By definition, an unknown property. */
36939 cp_lexer_consume_token (parser->lexer);
36941 enum objc_property_attribute_kind prop_kind
36942 = objc_prop_attr_kind_for_rid (keyword);
36943 property_attribute_info *prop
36944 = new property_attribute_info (attr_name, attr_comb, prop_kind);
36945 prop_attr_list.safe_push (prop);
36947 tree meth_name;
36948 switch (prop->prop_kind)
36950 default: break;
36951 case OBJC_PROPERTY_ATTR_UNKNOWN:
36952 if (attr_name)
36953 error_at (attr_start, "unknown property attribute %qE",
36954 attr_name);
36955 else
36956 error_at (attr_start, "unknown property attribute");
36957 prop->parse_error = syntax_error = true;
36958 break;
36960 case OBJC_PROPERTY_ATTR_GETTER:
36961 case OBJC_PROPERTY_ATTR_SETTER:
36962 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36964 attr_comb = make_location (attr_end, attr_start, attr_end);
36965 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
36966 attr_name);
36967 prop->parse_error = syntax_error = true;
36968 break;
36971 token = cp_lexer_peek_token (parser->lexer);
36972 attr_end = token->location;
36973 cp_lexer_consume_token (parser->lexer); /* eat the = */
36975 if (!cp_parser_objc_selector_p
36976 (cp_lexer_peek_token (parser->lexer)->type))
36978 attr_comb = make_location (attr_end, attr_start, attr_end);
36979 error_at (attr_comb, "expected %qE selector name",
36980 attr_name);
36981 prop->parse_error = syntax_error = true;
36982 break;
36985 /* Get the end of the method name, and consume the name. */
36986 token = cp_lexer_peek_token (parser->lexer);
36987 attr_end = get_finish (token->location);
36988 /* Because method names may contain C++ keywords, we have a
36989 routine to fetch them (this also consumes the token). */
36990 meth_name = cp_parser_objc_selector (parser);
36992 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
36994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36996 attr_comb = make_location (attr_end, attr_start,
36997 attr_end);
36998 error_at (attr_comb, "setter method names must"
36999 " terminate with %<:%>");
37000 prop->parse_error = syntax_error = true;
37002 else
37004 attr_end = get_finish (cp_lexer_peek_token
37005 (parser->lexer)->location);
37006 cp_lexer_consume_token (parser->lexer);
37008 attr_comb = make_location (attr_start, attr_start,
37009 attr_end);
37011 else
37012 attr_comb = make_location (attr_start, attr_start,
37013 attr_end);
37014 prop->ident = meth_name;
37015 /* Updated location including all that was successfully
37016 parsed. */
37017 prop->prop_loc = attr_comb;
37018 break;
37021 /* If we see a comma here, then keep going - even if we already
37022 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
37023 this makes a more useful output and avoid spurious warnings
37024 about missing attributes that are, in fact, specified after the
37025 one with the syntax error. */
37026 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37027 cp_lexer_consume_token (parser->lexer);
37028 else
37029 break;
37032 if (syntax_error || !parens.require_close (parser))
37033 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37034 /*or_comma=*/false,
37035 /*consume_paren=*/true);
37038 /* 'properties' is the list of properties that we read. Usually a
37039 single one, but maybe more (eg, in "@property int a, b, c;" there
37040 are three).
37041 TODO: Update this parsing so that it accepts (erroneous) bitfields so
37042 that we can issue a meaningful and consistent (between C/C++) error
37043 message from objc_add_property_declaration (). */
37044 tree properties = cp_parser_objc_struct_declaration (parser);
37046 if (properties == error_mark_node)
37047 cp_parser_skip_to_end_of_statement (parser);
37048 else if (properties == NULL_TREE)
37049 cp_parser_error (parser, "expected identifier");
37050 else
37052 /* Comma-separated properties are chained together in reverse order;
37053 add them one by one. */
37054 properties = nreverse (properties);
37055 for (; properties; properties = TREE_CHAIN (properties))
37056 objc_add_property_declaration (loc, copy_node (properties),
37057 prop_attr_list);
37060 cp_parser_consume_semicolon_at_end_of_statement (parser);
37063 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
37065 objc-synthesize-declaration:
37066 @synthesize objc-synthesize-identifier-list ;
37068 objc-synthesize-identifier-list:
37069 objc-synthesize-identifier
37070 objc-synthesize-identifier-list, objc-synthesize-identifier
37072 objc-synthesize-identifier
37073 identifier
37074 identifier = identifier
37076 For example:
37077 @synthesize MyProperty;
37078 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
37080 PS: This function is identical to c_parser_objc_at_synthesize_declaration
37081 for C. Keep them in sync.
37083 static void
37084 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
37086 tree list = NULL_TREE;
37087 location_t loc;
37088 loc = cp_lexer_peek_token (parser->lexer)->location;
37090 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
37091 while (true)
37093 tree property, ivar;
37094 property = cp_parser_identifier (parser);
37095 if (property == error_mark_node)
37097 cp_parser_consume_semicolon_at_end_of_statement (parser);
37098 return;
37100 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
37102 cp_lexer_consume_token (parser->lexer);
37103 ivar = cp_parser_identifier (parser);
37104 if (ivar == error_mark_node)
37106 cp_parser_consume_semicolon_at_end_of_statement (parser);
37107 return;
37110 else
37111 ivar = NULL_TREE;
37112 list = chainon (list, build_tree_list (ivar, property));
37113 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37114 cp_lexer_consume_token (parser->lexer);
37115 else
37116 break;
37118 cp_parser_consume_semicolon_at_end_of_statement (parser);
37119 objc_add_synthesize_declaration (loc, list);
37122 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
37124 objc-dynamic-declaration:
37125 @dynamic identifier-list ;
37127 For example:
37128 @dynamic MyProperty;
37129 @dynamic MyProperty, AnotherProperty;
37131 PS: This function is identical to c_parser_objc_at_dynamic_declaration
37132 for C. Keep them in sync.
37134 static void
37135 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
37137 tree list = NULL_TREE;
37138 location_t loc;
37139 loc = cp_lexer_peek_token (parser->lexer)->location;
37141 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
37142 while (true)
37144 tree property;
37145 property = cp_parser_identifier (parser);
37146 if (property == error_mark_node)
37148 cp_parser_consume_semicolon_at_end_of_statement (parser);
37149 return;
37151 list = chainon (list, build_tree_list (NULL, property));
37152 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37153 cp_lexer_consume_token (parser->lexer);
37154 else
37155 break;
37157 cp_parser_consume_semicolon_at_end_of_statement (parser);
37158 objc_add_dynamic_declaration (loc, list);
37162 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
37164 /* Returns name of the next clause.
37165 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
37166 the token is not consumed. Otherwise appropriate pragma_omp_clause is
37167 returned and the token is consumed. */
37169 static pragma_omp_clause
37170 cp_parser_omp_clause_name (cp_parser *parser)
37172 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
37174 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
37175 result = PRAGMA_OACC_CLAUSE_AUTO;
37176 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
37177 result = PRAGMA_OMP_CLAUSE_IF;
37178 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37179 result = PRAGMA_OMP_CLAUSE_DEFAULT;
37180 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
37181 result = PRAGMA_OACC_CLAUSE_DELETE;
37182 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37183 result = PRAGMA_OMP_CLAUSE_PRIVATE;
37184 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37185 result = PRAGMA_OMP_CLAUSE_FOR;
37186 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37188 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37189 const char *p = IDENTIFIER_POINTER (id);
37191 switch (p[0])
37193 case 'a':
37194 if (!strcmp ("affinity", p))
37195 result = PRAGMA_OMP_CLAUSE_AFFINITY;
37196 else if (!strcmp ("aligned", p))
37197 result = PRAGMA_OMP_CLAUSE_ALIGNED;
37198 else if (!strcmp ("allocate", p))
37199 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
37200 else if (!strcmp ("async", p))
37201 result = PRAGMA_OACC_CLAUSE_ASYNC;
37202 else if (!strcmp ("attach", p))
37203 result = PRAGMA_OACC_CLAUSE_ATTACH;
37204 break;
37205 case 'b':
37206 if (!strcmp ("bind", p))
37207 result = PRAGMA_OMP_CLAUSE_BIND;
37208 break;
37209 case 'c':
37210 if (!strcmp ("collapse", p))
37211 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
37212 else if (!strcmp ("copy", p))
37213 result = PRAGMA_OACC_CLAUSE_COPY;
37214 else if (!strcmp ("copyin", p))
37215 result = PRAGMA_OMP_CLAUSE_COPYIN;
37216 else if (!strcmp ("copyout", p))
37217 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37218 else if (!strcmp ("copyprivate", p))
37219 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
37220 else if (!strcmp ("create", p))
37221 result = PRAGMA_OACC_CLAUSE_CREATE;
37222 break;
37223 case 'd':
37224 if (!strcmp ("defaultmap", p))
37225 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
37226 else if (!strcmp ("depend", p))
37227 result = PRAGMA_OMP_CLAUSE_DEPEND;
37228 else if (!strcmp ("detach", p))
37229 result = PRAGMA_OACC_CLAUSE_DETACH;
37230 else if (!strcmp ("device", p))
37231 result = PRAGMA_OMP_CLAUSE_DEVICE;
37232 else if (!strcmp ("deviceptr", p))
37233 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
37234 else if (!strcmp ("device_resident", p))
37235 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
37236 else if (!strcmp ("device_type", p))
37237 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
37238 else if (!strcmp ("dist_schedule", p))
37239 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
37240 else if (!strcmp ("doacross", p))
37241 result = PRAGMA_OMP_CLAUSE_DOACROSS;
37242 break;
37243 case 'e':
37244 if (!strcmp ("enter", p))
37245 result = PRAGMA_OMP_CLAUSE_ENTER;
37246 break;
37247 case 'f':
37248 if (!strcmp ("filter", p))
37249 result = PRAGMA_OMP_CLAUSE_FILTER;
37250 else if (!strcmp ("final", p))
37251 result = PRAGMA_OMP_CLAUSE_FINAL;
37252 else if (!strcmp ("finalize", p))
37253 result = PRAGMA_OACC_CLAUSE_FINALIZE;
37254 else if (!strcmp ("firstprivate", p))
37255 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
37256 else if (!strcmp ("from", p))
37257 result = PRAGMA_OMP_CLAUSE_FROM;
37258 break;
37259 case 'g':
37260 if (!strcmp ("gang", p))
37261 result = PRAGMA_OACC_CLAUSE_GANG;
37262 else if (!strcmp ("grainsize", p))
37263 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
37264 break;
37265 case 'h':
37266 if (!strcmp ("has_device_addr", p))
37267 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
37268 else if (!strcmp ("hint", p))
37269 result = PRAGMA_OMP_CLAUSE_HINT;
37270 else if (!strcmp ("host", p))
37271 result = PRAGMA_OACC_CLAUSE_HOST;
37272 break;
37273 case 'i':
37274 if (!strcmp ("if_present", p))
37275 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
37276 else if (!strcmp ("in_reduction", p))
37277 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
37278 else if (!strcmp ("inbranch", p))
37279 result = PRAGMA_OMP_CLAUSE_INBRANCH;
37280 else if (!strcmp ("independent", p))
37281 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
37282 else if (!strcmp ("is_device_ptr", p))
37283 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
37284 break;
37285 case 'l':
37286 if (!strcmp ("lastprivate", p))
37287 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
37288 else if (!strcmp ("linear", p))
37289 result = PRAGMA_OMP_CLAUSE_LINEAR;
37290 else if (!strcmp ("link", p))
37291 result = PRAGMA_OMP_CLAUSE_LINK;
37292 break;
37293 case 'm':
37294 if (!strcmp ("map", p))
37295 result = PRAGMA_OMP_CLAUSE_MAP;
37296 else if (!strcmp ("mergeable", p))
37297 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
37298 break;
37299 case 'n':
37300 if (!strcmp ("no_create", p))
37301 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
37302 else if (!strcmp ("nogroup", p))
37303 result = PRAGMA_OMP_CLAUSE_NOGROUP;
37304 else if (!strcmp ("nohost", p))
37305 result = PRAGMA_OACC_CLAUSE_NOHOST;
37306 else if (!strcmp ("nontemporal", p))
37307 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
37308 else if (!strcmp ("notinbranch", p))
37309 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
37310 else if (!strcmp ("nowait", p))
37311 result = PRAGMA_OMP_CLAUSE_NOWAIT;
37312 else if (!strcmp ("num_gangs", p))
37313 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
37314 else if (!strcmp ("num_tasks", p))
37315 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
37316 else if (!strcmp ("num_teams", p))
37317 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
37318 else if (!strcmp ("num_threads", p))
37319 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
37320 else if (!strcmp ("num_workers", p))
37321 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
37322 break;
37323 case 'o':
37324 if (!strcmp ("ordered", p))
37325 result = PRAGMA_OMP_CLAUSE_ORDERED;
37326 else if (!strcmp ("order", p))
37327 result = PRAGMA_OMP_CLAUSE_ORDER;
37328 break;
37329 case 'p':
37330 if (!strcmp ("parallel", p))
37331 result = PRAGMA_OMP_CLAUSE_PARALLEL;
37332 else if (!strcmp ("present", p))
37333 result = PRAGMA_OACC_CLAUSE_PRESENT;
37334 else if (!strcmp ("present_or_copy", p)
37335 || !strcmp ("pcopy", p))
37336 result = PRAGMA_OACC_CLAUSE_COPY;
37337 else if (!strcmp ("present_or_copyin", p)
37338 || !strcmp ("pcopyin", p))
37339 result = PRAGMA_OACC_CLAUSE_COPYIN;
37340 else if (!strcmp ("present_or_copyout", p)
37341 || !strcmp ("pcopyout", p))
37342 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37343 else if (!strcmp ("present_or_create", p)
37344 || !strcmp ("pcreate", p))
37345 result = PRAGMA_OACC_CLAUSE_CREATE;
37346 else if (!strcmp ("priority", p))
37347 result = PRAGMA_OMP_CLAUSE_PRIORITY;
37348 else if (!strcmp ("proc_bind", p))
37349 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
37350 break;
37351 case 'r':
37352 if (!strcmp ("reduction", p))
37353 result = PRAGMA_OMP_CLAUSE_REDUCTION;
37354 break;
37355 case 's':
37356 if (!strcmp ("safelen", p))
37357 result = PRAGMA_OMP_CLAUSE_SAFELEN;
37358 else if (!strcmp ("schedule", p))
37359 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
37360 else if (!strcmp ("sections", p))
37361 result = PRAGMA_OMP_CLAUSE_SECTIONS;
37362 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
37363 result = PRAGMA_OACC_CLAUSE_HOST;
37364 else if (!strcmp ("seq", p))
37365 result = PRAGMA_OACC_CLAUSE_SEQ;
37366 else if (!strcmp ("shared", p))
37367 result = PRAGMA_OMP_CLAUSE_SHARED;
37368 else if (!strcmp ("simd", p))
37369 result = PRAGMA_OMP_CLAUSE_SIMD;
37370 else if (!strcmp ("simdlen", p))
37371 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
37372 break;
37373 case 't':
37374 if (!strcmp ("task_reduction", p))
37375 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
37376 else if (!strcmp ("taskgroup", p))
37377 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
37378 else if (!strcmp ("thread_limit", p))
37379 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
37380 else if (!strcmp ("threads", p))
37381 result = PRAGMA_OMP_CLAUSE_THREADS;
37382 else if (!strcmp ("tile", p))
37383 result = PRAGMA_OACC_CLAUSE_TILE;
37384 else if (!strcmp ("to", p))
37385 result = PRAGMA_OMP_CLAUSE_TO;
37386 break;
37387 case 'u':
37388 if (!strcmp ("uniform", p))
37389 result = PRAGMA_OMP_CLAUSE_UNIFORM;
37390 else if (!strcmp ("untied", p))
37391 result = PRAGMA_OMP_CLAUSE_UNTIED;
37392 else if (!strcmp ("use_device", p))
37393 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
37394 else if (!strcmp ("use_device_addr", p))
37395 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
37396 else if (!strcmp ("use_device_ptr", p))
37397 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
37398 break;
37399 case 'v':
37400 if (!strcmp ("vector", p))
37401 result = PRAGMA_OACC_CLAUSE_VECTOR;
37402 else if (!strcmp ("vector_length", p))
37403 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
37404 break;
37405 case 'w':
37406 if (!strcmp ("wait", p))
37407 result = PRAGMA_OACC_CLAUSE_WAIT;
37408 else if (!strcmp ("worker", p))
37409 result = PRAGMA_OACC_CLAUSE_WORKER;
37410 break;
37414 if (result != PRAGMA_OMP_CLAUSE_NONE)
37415 cp_lexer_consume_token (parser->lexer);
37417 return result;
37420 /* Validate that a clause of the given type does not already exist. */
37422 static void
37423 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
37424 const char *name, location_t location)
37426 if (omp_find_clause (clauses, code))
37427 error_at (location, "too many %qs clauses", name);
37430 /* OpenMP 2.5:
37431 variable-list:
37432 identifier
37433 variable-list , identifier
37435 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
37436 colon). An opening parenthesis will have been consumed by the caller.
37438 If KIND is nonzero, create the appropriate node and install the decl
37439 in OMP_CLAUSE_DECL and add the node to the head of the list.
37441 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
37442 return the list created.
37444 COLON can be NULL if only closing parenthesis should end the list,
37445 or pointer to bool which will receive false if the list is terminated
37446 by closing parenthesis or true if the list is terminated by colon.
37448 The optional ALLOW_DEREF argument is true if list items can use the deref
37449 (->) operator. */
37451 struct omp_dim
37453 tree low_bound, length;
37454 location_t loc;
37455 bool no_colon;
37456 omp_dim (tree lb, tree len, location_t lo, bool nc)
37457 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
37460 static tree
37461 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
37462 tree list, bool *colon,
37463 bool allow_deref = false)
37465 auto_vec<omp_dim> dims;
37466 bool array_section_p;
37467 cp_token *token;
37468 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37469 if (colon)
37471 parser->colon_corrects_to_scope_p = false;
37472 *colon = false;
37474 while (1)
37476 tree name, decl;
37478 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37479 cp_parser_parse_tentatively (parser);
37480 token = cp_lexer_peek_token (parser->lexer);
37481 if (kind != 0
37482 && cp_parser_is_keyword (token, RID_THIS))
37484 decl = finish_this_expr ();
37485 if (TREE_CODE (decl) == NON_LVALUE_EXPR
37486 || CONVERT_EXPR_P (decl))
37487 decl = TREE_OPERAND (decl, 0);
37488 cp_lexer_consume_token (parser->lexer);
37490 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
37491 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
37492 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
37494 cp_id_kind idk;
37495 decl = cp_parser_primary_expression (parser, false, false, false,
37496 &idk);
37498 else if (kind == OMP_CLAUSE_DEPEND
37499 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
37500 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37501 || cp_lexer_nth_token_is (parser->lexer, 2,
37502 CPP_CLOSE_PAREN)))
37504 decl = ridpointers[RID_OMP_ALL_MEMORY];
37505 cp_lexer_consume_token (parser->lexer);
37507 else
37509 name = cp_parser_id_expression (parser, /*template_p=*/false,
37510 /*check_dependency_p=*/true,
37511 /*template_p=*/NULL,
37512 /*declarator_p=*/false,
37513 /*optional_p=*/false);
37514 if (name == error_mark_node)
37516 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37517 && cp_parser_simulate_error (parser))
37518 goto depend_lvalue;
37519 goto skip_comma;
37522 if (identifier_p (name))
37523 decl = cp_parser_lookup_name_simple (parser, name, token->location);
37524 else
37525 decl = name;
37526 if (decl == error_mark_node)
37528 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37529 && cp_parser_simulate_error (parser))
37530 goto depend_lvalue;
37531 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
37532 token->location);
37535 if (outer_automatic_var_p (decl))
37536 decl = process_outer_var_ref (decl, tf_warning_or_error);
37537 if (decl == error_mark_node)
37539 else if (kind != 0)
37541 switch (kind)
37543 case OMP_CLAUSE__CACHE_:
37544 /* The OpenACC cache directive explicitly only allows "array
37545 elements or subarrays". */
37546 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
37548 error_at (token->location, "expected %<[%>");
37549 decl = error_mark_node;
37550 break;
37552 /* FALLTHROUGH. */
37553 case OMP_CLAUSE_MAP:
37554 case OMP_CLAUSE_FROM:
37555 case OMP_CLAUSE_TO:
37556 start_component_ref:
37557 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37558 || (allow_deref
37559 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
37561 cpp_ttype ttype
37562 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37563 ? CPP_DOT : CPP_DEREF;
37564 location_t loc
37565 = cp_lexer_peek_token (parser->lexer)->location;
37566 cp_id_kind idk = CP_ID_KIND_NONE;
37567 cp_lexer_consume_token (parser->lexer);
37568 decl = convert_from_reference (decl);
37569 decl = (cp_parser_postfix_dot_deref_expression
37570 (parser, ttype, cp_expr (decl, token->location),
37571 false, &idk, loc));
37573 /* FALLTHROUGH. */
37574 case OMP_CLAUSE_AFFINITY:
37575 case OMP_CLAUSE_DEPEND:
37576 case OMP_CLAUSE_REDUCTION:
37577 case OMP_CLAUSE_IN_REDUCTION:
37578 case OMP_CLAUSE_TASK_REDUCTION:
37579 case OMP_CLAUSE_HAS_DEVICE_ADDR:
37580 array_section_p = false;
37581 dims.truncate (0);
37582 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
37584 location_t loc = UNKNOWN_LOCATION;
37585 tree low_bound = NULL_TREE, length = NULL_TREE;
37586 bool no_colon = false;
37588 parser->colon_corrects_to_scope_p = false;
37589 cp_lexer_consume_token (parser->lexer);
37590 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37592 loc = cp_lexer_peek_token (parser->lexer)->location;
37593 low_bound = cp_parser_expression (parser);
37594 /* Later handling is not prepared to see through these. */
37595 gcc_checking_assert (!location_wrapper_p (low_bound));
37597 if (!colon)
37598 parser->colon_corrects_to_scope_p
37599 = saved_colon_corrects_to_scope_p;
37600 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
37602 length = integer_one_node;
37603 no_colon = true;
37605 else
37607 /* Look for `:'. */
37608 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37610 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37611 && cp_parser_simulate_error (parser))
37612 goto depend_lvalue;
37613 goto skip_comma;
37615 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37616 cp_parser_commit_to_tentative_parse (parser);
37617 else
37618 array_section_p = true;
37619 if (!cp_lexer_next_token_is (parser->lexer,
37620 CPP_CLOSE_SQUARE))
37622 length = cp_parser_expression (parser);
37623 /* Later handling is not prepared to see through these. */
37624 gcc_checking_assert (!location_wrapper_p (length));
37627 /* Look for the closing `]'. */
37628 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
37629 RT_CLOSE_SQUARE))
37631 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37632 && cp_parser_simulate_error (parser))
37633 goto depend_lvalue;
37634 goto skip_comma;
37637 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
37640 if ((kind == OMP_CLAUSE_MAP
37641 || kind == OMP_CLAUSE_FROM
37642 || kind == OMP_CLAUSE_TO)
37643 && !array_section_p
37644 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37645 || (allow_deref
37646 && cp_lexer_next_token_is (parser->lexer,
37647 CPP_DEREF))))
37649 for (unsigned i = 0; i < dims.length (); i++)
37651 gcc_assert (dims[i].length == integer_one_node);
37652 decl = build_array_ref (dims[i].loc,
37653 decl, dims[i].low_bound);
37655 goto start_component_ref;
37657 else
37658 for (unsigned i = 0; i < dims.length (); i++)
37659 decl = tree_cons (dims[i].low_bound, dims[i].length, decl);
37661 break;
37662 default:
37663 break;
37666 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
37669 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
37670 && cp_parser_simulate_error (parser))
37672 depend_lvalue:
37673 cp_parser_abort_tentative_parse (parser);
37674 decl = cp_parser_assignment_expression (parser, NULL,
37675 false, false);
37677 else
37678 cp_parser_parse_definitely (parser);
37681 tree u = build_omp_clause (token->location, kind);
37682 OMP_CLAUSE_DECL (u) = decl;
37683 OMP_CLAUSE_CHAIN (u) = list;
37684 list = u;
37686 else
37687 list = tree_cons (decl, NULL_TREE, list);
37689 get_comma:
37690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
37691 break;
37692 cp_lexer_consume_token (parser->lexer);
37695 if (colon)
37696 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37698 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37700 *colon = true;
37701 cp_parser_require (parser, CPP_COLON, RT_COLON);
37702 return list;
37705 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37707 int ending;
37709 /* Try to resync to an unnested comma. Copied from
37710 cp_parser_parenthesized_expression_list. */
37711 skip_comma:
37712 if (colon)
37713 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37714 ending = cp_parser_skip_to_closing_parenthesis (parser,
37715 /*recovering=*/true,
37716 /*or_comma=*/true,
37717 /*consume_paren=*/true);
37718 if (ending < 0)
37719 goto get_comma;
37722 return list;
37725 /* Similarly, but expect leading and trailing parenthesis. This is a very
37726 common case for omp clauses. */
37728 static tree
37729 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
37730 bool allow_deref = false)
37732 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37733 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
37734 allow_deref);
37735 return list;
37738 /* OpenACC 2.0:
37739 copy ( variable-list )
37740 copyin ( variable-list )
37741 copyout ( variable-list )
37742 create ( variable-list )
37743 delete ( variable-list )
37744 present ( variable-list )
37746 OpenACC 2.6:
37747 no_create ( variable-list )
37748 attach ( variable-list )
37749 detach ( variable-list ) */
37751 static tree
37752 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
37753 tree list)
37755 enum gomp_map_kind kind;
37756 switch (c_kind)
37758 case PRAGMA_OACC_CLAUSE_ATTACH:
37759 kind = GOMP_MAP_ATTACH;
37760 break;
37761 case PRAGMA_OACC_CLAUSE_COPY:
37762 kind = GOMP_MAP_TOFROM;
37763 break;
37764 case PRAGMA_OACC_CLAUSE_COPYIN:
37765 kind = GOMP_MAP_TO;
37766 break;
37767 case PRAGMA_OACC_CLAUSE_COPYOUT:
37768 kind = GOMP_MAP_FROM;
37769 break;
37770 case PRAGMA_OACC_CLAUSE_CREATE:
37771 kind = GOMP_MAP_ALLOC;
37772 break;
37773 case PRAGMA_OACC_CLAUSE_DELETE:
37774 kind = GOMP_MAP_RELEASE;
37775 break;
37776 case PRAGMA_OACC_CLAUSE_DETACH:
37777 kind = GOMP_MAP_DETACH;
37778 break;
37779 case PRAGMA_OACC_CLAUSE_DEVICE:
37780 kind = GOMP_MAP_FORCE_TO;
37781 break;
37782 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
37783 kind = GOMP_MAP_DEVICE_RESIDENT;
37784 break;
37785 case PRAGMA_OACC_CLAUSE_HOST:
37786 kind = GOMP_MAP_FORCE_FROM;
37787 break;
37788 case PRAGMA_OACC_CLAUSE_LINK:
37789 kind = GOMP_MAP_LINK;
37790 break;
37791 case PRAGMA_OACC_CLAUSE_NO_CREATE:
37792 kind = GOMP_MAP_IF_PRESENT;
37793 break;
37794 case PRAGMA_OACC_CLAUSE_PRESENT:
37795 kind = GOMP_MAP_FORCE_PRESENT;
37796 break;
37797 default:
37798 gcc_unreachable ();
37800 tree nl, c;
37801 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
37803 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
37804 OMP_CLAUSE_SET_MAP_KIND (c, kind);
37806 return nl;
37809 /* OpenACC 2.0:
37810 deviceptr ( variable-list ) */
37812 static tree
37813 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
37815 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37816 tree vars, t;
37818 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
37819 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
37820 variable-list must only allow for pointer variables. */
37821 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37822 for (t = vars; t; t = TREE_CHAIN (t))
37824 tree v = TREE_PURPOSE (t);
37825 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
37826 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
37827 OMP_CLAUSE_DECL (u) = v;
37828 OMP_CLAUSE_CHAIN (u) = list;
37829 list = u;
37832 return list;
37835 /* OpenACC 2.5:
37836 auto
37837 finalize
37838 independent
37839 nohost
37840 seq */
37842 static tree
37843 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
37844 tree list)
37846 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
37848 tree c = build_omp_clause (loc, code);
37849 OMP_CLAUSE_CHAIN (c) = list;
37851 return c;
37854 /* OpenACC:
37855 num_gangs ( expression )
37856 num_workers ( expression )
37857 vector_length ( expression ) */
37859 static tree
37860 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
37861 const char *str, tree list)
37863 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37865 matching_parens parens;
37866 if (!parens.require_open (parser))
37867 return list;
37869 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
37871 if (t == error_mark_node
37872 || !parens.require_close (parser))
37874 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37875 /*or_comma=*/false,
37876 /*consume_paren=*/true);
37877 return list;
37880 check_no_duplicate_clause (list, code, str, loc);
37882 tree c = build_omp_clause (loc, code);
37883 OMP_CLAUSE_OPERAND (c, 0) = t;
37884 OMP_CLAUSE_CHAIN (c) = list;
37885 return c;
37888 /* OpenACC:
37890 gang [( gang-arg-list )]
37891 worker [( [num:] int-expr )]
37892 vector [( [length:] int-expr )]
37894 where gang-arg is one of:
37896 [num:] int-expr
37897 static: size-expr
37899 and size-expr may be:
37902 int-expr
37905 static tree
37906 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
37907 omp_clause_code kind,
37908 const char *str, tree list)
37910 const char *id = "num";
37911 cp_lexer *lexer = parser->lexer;
37912 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
37914 if (kind == OMP_CLAUSE_VECTOR)
37915 id = "length";
37917 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
37919 matching_parens parens;
37920 parens.consume_open (parser);
37924 cp_token *next = cp_lexer_peek_token (lexer);
37925 int idx = 0;
37927 /* Gang static argument. */
37928 if (kind == OMP_CLAUSE_GANG
37929 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
37931 cp_lexer_consume_token (lexer);
37933 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37934 goto cleanup_error;
37936 idx = 1;
37937 if (ops[idx] != NULL)
37939 cp_parser_error (parser, "too many %<static%> arguments");
37940 goto cleanup_error;
37943 /* Check for the '*' argument. */
37944 if (cp_lexer_next_token_is (lexer, CPP_MULT)
37945 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37946 || cp_lexer_nth_token_is (parser->lexer, 2,
37947 CPP_CLOSE_PAREN)))
37949 cp_lexer_consume_token (lexer);
37950 ops[idx] = integer_minus_one_node;
37952 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
37954 cp_lexer_consume_token (lexer);
37955 continue;
37957 else break;
37960 /* Worker num: argument and vector length: arguments. */
37961 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
37962 && id_equal (next->u.value, id)
37963 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
37965 cp_lexer_consume_token (lexer); /* id */
37966 cp_lexer_consume_token (lexer); /* ':' */
37969 /* Now collect the actual argument. */
37970 if (ops[idx] != NULL_TREE)
37972 cp_parser_error (parser, "unexpected argument");
37973 goto cleanup_error;
37976 tree expr = cp_parser_assignment_expression (parser, NULL, false,
37977 false);
37978 if (expr == error_mark_node)
37979 goto cleanup_error;
37981 mark_exp_read (expr);
37982 ops[idx] = expr;
37984 if (kind == OMP_CLAUSE_GANG
37985 && cp_lexer_next_token_is (lexer, CPP_COMMA))
37987 cp_lexer_consume_token (lexer);
37988 continue;
37990 break;
37992 while (1);
37994 if (!parens.require_close (parser))
37995 goto cleanup_error;
37998 check_no_duplicate_clause (list, kind, str, loc);
38000 c = build_omp_clause (loc, kind);
38002 if (ops[1])
38003 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
38005 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
38006 OMP_CLAUSE_CHAIN (c) = list;
38008 return c;
38010 cleanup_error:
38011 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38012 return list;
38015 /* OpenACC 2.0:
38016 tile ( size-expr-list ) */
38018 static tree
38019 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
38021 tree c, expr = error_mark_node;
38022 tree tile = NULL_TREE;
38024 /* Collapse and tile are mutually exclusive. (The spec doesn't say
38025 so, but the spec authors never considered such a case and have
38026 differing opinions on what it might mean, including 'not
38027 allowed'.) */
38028 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
38029 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
38030 clause_loc);
38032 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38033 return list;
38037 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
38038 return list;
38040 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
38041 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38042 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
38044 cp_lexer_consume_token (parser->lexer);
38045 expr = integer_zero_node;
38047 else
38048 expr = cp_parser_constant_expression (parser);
38050 tile = tree_cons (NULL_TREE, expr, tile);
38052 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
38054 /* Consume the trailing ')'. */
38055 cp_lexer_consume_token (parser->lexer);
38057 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
38058 tile = nreverse (tile);
38059 OMP_CLAUSE_TILE_LIST (c) = tile;
38060 OMP_CLAUSE_CHAIN (c) = list;
38061 return c;
38064 /* OpenACC 2.0
38065 Parse wait clause or directive parameters. */
38067 static tree
38068 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
38070 vec<tree, va_gc> *args;
38071 tree t, args_tree;
38073 args = cp_parser_parenthesized_expression_list (parser, non_attr,
38074 /*cast_p=*/false,
38075 /*allow_expansion_p=*/true,
38076 /*non_constant_p=*/NULL);
38078 if (args == NULL || args->length () == 0)
38080 if (args != NULL)
38082 cp_parser_error (parser, "expected integer expression list");
38083 release_tree_vector (args);
38085 return list;
38088 args_tree = build_tree_list_vec (args);
38090 release_tree_vector (args);
38092 for (t = args_tree; t; t = TREE_CHAIN (t))
38094 tree targ = TREE_VALUE (t);
38096 if (targ != error_mark_node)
38098 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
38099 error ("%<wait%> expression must be integral");
38100 else
38102 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
38104 targ = mark_rvalue_use (targ);
38105 OMP_CLAUSE_DECL (c) = targ;
38106 OMP_CLAUSE_CHAIN (c) = list;
38107 list = c;
38112 return list;
38115 /* OpenACC:
38116 wait [( int-expr-list )] */
38118 static tree
38119 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
38121 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38123 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38124 list = cp_parser_oacc_wait_list (parser, location, list);
38125 else
38127 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
38129 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
38130 OMP_CLAUSE_CHAIN (c) = list;
38131 list = c;
38134 return list;
38137 /* OpenMP 3.0:
38138 collapse ( constant-expression ) */
38140 static tree
38141 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
38143 tree c, num;
38144 location_t loc;
38145 HOST_WIDE_INT n;
38147 loc = cp_lexer_peek_token (parser->lexer)->location;
38148 matching_parens parens;
38149 if (!parens.require_open (parser))
38150 return list;
38152 num = cp_parser_constant_expression (parser);
38154 if (!parens.require_close (parser))
38155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38156 /*or_comma=*/false,
38157 /*consume_paren=*/true);
38159 if (num == error_mark_node)
38160 return list;
38161 num = fold_non_dependent_expr (num);
38162 if (!tree_fits_shwi_p (num)
38163 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38164 || (n = tree_to_shwi (num)) <= 0
38165 || (int) n != n)
38167 error_at (loc, "collapse argument needs positive constant integer expression");
38168 return list;
38171 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
38172 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
38173 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
38174 OMP_CLAUSE_CHAIN (c) = list;
38175 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
38177 return c;
38180 /* OpenMP 2.5:
38181 default ( none | shared )
38183 OpenMP 5.1:
38184 default ( private | firstprivate )
38186 OpenACC:
38187 default ( none | present ) */
38189 static tree
38190 cp_parser_omp_clause_default (cp_parser *parser, tree list,
38191 location_t location, bool is_oacc)
38193 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
38194 tree c;
38196 matching_parens parens;
38197 if (!parens.require_open (parser))
38198 return list;
38199 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
38201 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
38202 cp_lexer_consume_token (parser->lexer);
38204 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38206 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38207 const char *p = IDENTIFIER_POINTER (id);
38209 switch (p[0])
38211 case 'n':
38212 if (strcmp ("none", p) != 0)
38213 goto invalid_kind;
38214 kind = OMP_CLAUSE_DEFAULT_NONE;
38215 break;
38217 case 'p':
38218 if (strcmp ("present", p) != 0 || !is_oacc)
38219 goto invalid_kind;
38220 kind = OMP_CLAUSE_DEFAULT_PRESENT;
38221 break;
38223 case 'f':
38224 if (strcmp ("firstprivate", p) != 0 || is_oacc)
38225 goto invalid_kind;
38226 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
38227 break;
38229 case 's':
38230 if (strcmp ("shared", p) != 0 || is_oacc)
38231 goto invalid_kind;
38232 kind = OMP_CLAUSE_DEFAULT_SHARED;
38233 break;
38235 default:
38236 goto invalid_kind;
38239 cp_lexer_consume_token (parser->lexer);
38241 else
38243 invalid_kind:
38244 if (is_oacc)
38245 cp_parser_error (parser, "expected %<none%> or %<present%>");
38246 else
38247 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
38248 "%<private%> or %<firstprivate%>");
38251 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
38252 || !parens.require_close (parser))
38253 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38254 /*or_comma=*/false,
38255 /*consume_paren=*/true);
38257 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
38258 return list;
38260 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
38261 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
38262 OMP_CLAUSE_CHAIN (c) = list;
38263 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
38265 return c;
38268 /* OpenMP 3.1:
38269 final ( expression ) */
38271 static tree
38272 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
38274 tree t, c;
38276 matching_parens parens;
38277 if (!parens.require_open (parser))
38278 return list;
38280 t = cp_parser_assignment_expression (parser);
38282 if (t == error_mark_node
38283 || !parens.require_close (parser))
38284 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38285 /*or_comma=*/false,
38286 /*consume_paren=*/true);
38288 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
38290 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
38291 OMP_CLAUSE_FINAL_EXPR (c) = t;
38292 OMP_CLAUSE_CHAIN (c) = list;
38294 return c;
38297 /* OpenMP 2.5:
38298 if ( expression )
38300 OpenMP 4.5:
38301 if ( directive-name-modifier : expression )
38303 directive-name-modifier:
38304 parallel | task | taskloop | target data | target | target update
38305 | target enter data | target exit data
38307 OpenMP 5.0:
38308 directive-name-modifier:
38309 ... | simd | cancel */
38311 static tree
38312 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
38313 bool is_omp)
38315 tree t, c;
38316 enum tree_code if_modifier = ERROR_MARK;
38318 matching_parens parens;
38319 if (!parens.require_open (parser))
38320 return list;
38322 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38324 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38325 const char *p = IDENTIFIER_POINTER (id);
38326 int n = 2;
38328 if (strcmp ("cancel", p) == 0)
38329 if_modifier = VOID_CST;
38330 else if (strcmp ("parallel", p) == 0)
38331 if_modifier = OMP_PARALLEL;
38332 else if (strcmp ("simd", p) == 0)
38333 if_modifier = OMP_SIMD;
38334 else if (strcmp ("task", p) == 0)
38335 if_modifier = OMP_TASK;
38336 else if (strcmp ("taskloop", p) == 0)
38337 if_modifier = OMP_TASKLOOP;
38338 else if (strcmp ("target", p) == 0)
38340 if_modifier = OMP_TARGET;
38341 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38343 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
38344 p = IDENTIFIER_POINTER (id);
38345 if (strcmp ("data", p) == 0)
38346 if_modifier = OMP_TARGET_DATA;
38347 else if (strcmp ("update", p) == 0)
38348 if_modifier = OMP_TARGET_UPDATE;
38349 else if (strcmp ("enter", p) == 0)
38350 if_modifier = OMP_TARGET_ENTER_DATA;
38351 else if (strcmp ("exit", p) == 0)
38352 if_modifier = OMP_TARGET_EXIT_DATA;
38353 if (if_modifier != OMP_TARGET)
38354 n = 3;
38355 else
38357 location_t loc
38358 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
38359 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
38360 "or %<exit%>");
38361 if_modifier = ERROR_MARK;
38363 if (if_modifier == OMP_TARGET_ENTER_DATA
38364 || if_modifier == OMP_TARGET_EXIT_DATA)
38366 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
38368 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
38369 p = IDENTIFIER_POINTER (id);
38370 if (strcmp ("data", p) == 0)
38371 n = 4;
38373 if (n != 4)
38375 location_t loc
38376 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
38377 error_at (loc, "expected %<data%>");
38378 if_modifier = ERROR_MARK;
38383 if (if_modifier != ERROR_MARK)
38385 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
38387 while (n-- > 0)
38388 cp_lexer_consume_token (parser->lexer);
38390 else
38392 if (n > 2)
38394 location_t loc
38395 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
38396 error_at (loc, "expected %<:%>");
38398 if_modifier = ERROR_MARK;
38403 t = cp_parser_assignment_expression (parser);
38405 if (t == error_mark_node
38406 || !parens.require_close (parser))
38407 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38408 /*or_comma=*/false,
38409 /*consume_paren=*/true);
38411 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
38412 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
38414 if (if_modifier != ERROR_MARK
38415 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
38417 const char *p = NULL;
38418 switch (if_modifier)
38420 case VOID_CST: p = "cancel"; break;
38421 case OMP_PARALLEL: p = "parallel"; break;
38422 case OMP_SIMD: p = "simd"; break;
38423 case OMP_TASK: p = "task"; break;
38424 case OMP_TASKLOOP: p = "taskloop"; break;
38425 case OMP_TARGET_DATA: p = "target data"; break;
38426 case OMP_TARGET: p = "target"; break;
38427 case OMP_TARGET_UPDATE: p = "target update"; break;
38428 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
38429 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
38430 default: gcc_unreachable ();
38432 error_at (location, "too many %<if%> clauses with %qs modifier",
38434 return list;
38436 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
38438 if (!is_omp)
38439 error_at (location, "too many %<if%> clauses");
38440 else
38441 error_at (location, "too many %<if%> clauses without modifier");
38442 return list;
38444 else if (if_modifier == ERROR_MARK
38445 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
38447 error_at (location, "if any %<if%> clause has modifier, then all "
38448 "%<if%> clauses have to use modifier");
38449 return list;
38453 c = build_omp_clause (location, OMP_CLAUSE_IF);
38454 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
38455 OMP_CLAUSE_IF_EXPR (c) = t;
38456 OMP_CLAUSE_CHAIN (c) = list;
38458 return c;
38461 /* OpenMP 3.1:
38462 mergeable */
38464 static tree
38465 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
38466 tree list, location_t location)
38468 tree c;
38470 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
38471 location);
38473 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
38474 OMP_CLAUSE_CHAIN (c) = list;
38475 return c;
38478 /* OpenMP 2.5:
38479 nowait */
38481 static tree
38482 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
38483 tree list, location_t location)
38485 tree c;
38487 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
38489 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
38490 OMP_CLAUSE_CHAIN (c) = list;
38491 return c;
38494 /* OpenMP 2.5:
38495 num_threads ( expression ) */
38497 static tree
38498 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
38499 location_t location)
38501 tree t, c;
38503 matching_parens parens;
38504 if (!parens.require_open (parser))
38505 return list;
38507 t = cp_parser_assignment_expression (parser);
38509 if (t == error_mark_node
38510 || !parens.require_close (parser))
38511 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38512 /*or_comma=*/false,
38513 /*consume_paren=*/true);
38515 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
38516 "num_threads", location);
38518 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
38519 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
38520 OMP_CLAUSE_CHAIN (c) = list;
38522 return c;
38525 /* OpenMP 4.5:
38526 num_tasks ( expression )
38528 OpenMP 5.1:
38529 num_tasks ( strict : expression ) */
38531 static tree
38532 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
38533 location_t location)
38535 tree t, c;
38537 matching_parens parens;
38538 if (!parens.require_open (parser))
38539 return list;
38541 bool strict = false;
38542 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38543 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38545 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38546 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
38548 strict = true;
38549 cp_lexer_consume_token (parser->lexer);
38550 cp_lexer_consume_token (parser->lexer);
38554 t = cp_parser_assignment_expression (parser);
38556 if (t == error_mark_node
38557 || !parens.require_close (parser))
38558 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38559 /*or_comma=*/false,
38560 /*consume_paren=*/true);
38562 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
38563 "num_tasks", location);
38565 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
38566 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
38567 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
38568 OMP_CLAUSE_CHAIN (c) = list;
38570 return c;
38573 /* OpenMP 4.5:
38574 grainsize ( expression )
38576 OpenMP 5.1:
38577 grainsize ( strict : expression ) */
38579 static tree
38580 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
38581 location_t location)
38583 tree t, c;
38585 matching_parens parens;
38586 if (!parens.require_open (parser))
38587 return list;
38589 bool strict = false;
38590 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38591 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38593 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38594 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
38596 strict = true;
38597 cp_lexer_consume_token (parser->lexer);
38598 cp_lexer_consume_token (parser->lexer);
38602 t = cp_parser_assignment_expression (parser);
38604 if (t == error_mark_node
38605 || !parens.require_close (parser))
38606 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38607 /*or_comma=*/false,
38608 /*consume_paren=*/true);
38610 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
38611 "grainsize", location);
38613 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
38614 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
38615 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
38616 OMP_CLAUSE_CHAIN (c) = list;
38618 return c;
38621 /* OpenMP 4.5:
38622 priority ( expression ) */
38624 static tree
38625 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
38626 location_t location)
38628 tree t, c;
38630 matching_parens parens;
38631 if (!parens.require_open (parser))
38632 return list;
38634 t = cp_parser_assignment_expression (parser);
38636 if (t == error_mark_node
38637 || !parens.require_close (parser))
38638 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38639 /*or_comma=*/false,
38640 /*consume_paren=*/true);
38642 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
38643 "priority", location);
38645 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
38646 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
38647 OMP_CLAUSE_CHAIN (c) = list;
38649 return c;
38652 /* OpenMP 4.5:
38653 hint ( expression ) */
38655 static tree
38656 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
38658 tree t, c;
38660 matching_parens parens;
38661 if (!parens.require_open (parser))
38662 return list;
38664 t = cp_parser_assignment_expression (parser);
38666 if (t != error_mark_node)
38668 t = fold_non_dependent_expr (t);
38669 if (!value_dependent_expression_p (t)
38670 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
38671 || !tree_fits_shwi_p (t)
38672 || tree_int_cst_sgn (t) == -1))
38673 error_at (location, "expected constant integer expression with "
38674 "valid sync-hint value");
38676 if (t == error_mark_node
38677 || !parens.require_close (parser))
38678 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38679 /*or_comma=*/false,
38680 /*consume_paren=*/true);
38681 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
38683 c = build_omp_clause (location, OMP_CLAUSE_HINT);
38684 OMP_CLAUSE_HINT_EXPR (c) = t;
38685 OMP_CLAUSE_CHAIN (c) = list;
38687 return c;
38690 /* OpenMP 5.1:
38691 filter ( integer-expression ) */
38693 static tree
38694 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
38696 tree t, c;
38698 matching_parens parens;
38699 if (!parens.require_open (parser))
38700 return list;
38702 t = cp_parser_assignment_expression (parser);
38704 if (t == error_mark_node
38705 || !parens.require_close (parser))
38706 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38707 /*or_comma=*/false,
38708 /*consume_paren=*/true);
38709 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
38711 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
38712 OMP_CLAUSE_FILTER_EXPR (c) = t;
38713 OMP_CLAUSE_CHAIN (c) = list;
38715 return c;
38718 /* OpenMP 4.5:
38719 defaultmap ( tofrom : scalar )
38721 OpenMP 5.0:
38722 defaultmap ( implicit-behavior [ : variable-category ] ) */
38724 static tree
38725 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
38726 location_t location)
38728 tree c, id;
38729 const char *p;
38730 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38731 enum omp_clause_defaultmap_kind category
38732 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
38734 matching_parens parens;
38735 if (!parens.require_open (parser))
38736 return list;
38738 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
38739 p = "default";
38740 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38742 invalid_behavior:
38743 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
38744 "%<tofrom%>, %<firstprivate%>, %<none%> "
38745 "or %<default%>");
38746 goto out_err;
38748 else
38750 id = cp_lexer_peek_token (parser->lexer)->u.value;
38751 p = IDENTIFIER_POINTER (id);
38754 switch (p[0])
38756 case 'a':
38757 if (strcmp ("alloc", p) == 0)
38758 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
38759 else
38760 goto invalid_behavior;
38761 break;
38763 case 'd':
38764 if (strcmp ("default", p) == 0)
38765 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38766 else
38767 goto invalid_behavior;
38768 break;
38770 case 'f':
38771 if (strcmp ("firstprivate", p) == 0)
38772 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
38773 else if (strcmp ("from", p) == 0)
38774 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
38775 else
38776 goto invalid_behavior;
38777 break;
38779 case 'n':
38780 if (strcmp ("none", p) == 0)
38781 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
38782 else
38783 goto invalid_behavior;
38784 break;
38786 case 'p':
38787 if (strcmp ("present", p) == 0)
38788 behavior = OMP_CLAUSE_DEFAULTMAP_PRESENT;
38789 else
38790 goto invalid_behavior;
38791 break;
38793 case 't':
38794 if (strcmp ("tofrom", p) == 0)
38795 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
38796 else if (strcmp ("to", p) == 0)
38797 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
38798 else
38799 goto invalid_behavior;
38800 break;
38802 default:
38803 goto invalid_behavior;
38805 cp_lexer_consume_token (parser->lexer);
38807 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38809 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38810 goto out_err;
38812 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38814 invalid_category:
38815 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
38816 "%<pointer%>");
38817 goto out_err;
38819 id = cp_lexer_peek_token (parser->lexer)->u.value;
38820 p = IDENTIFIER_POINTER (id);
38822 switch (p[0])
38824 case 'a':
38825 if (strcmp ("aggregate", p) == 0)
38826 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
38827 else
38828 goto invalid_category;
38829 break;
38831 case 'p':
38832 if (strcmp ("pointer", p) == 0)
38833 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
38834 else
38835 goto invalid_category;
38836 break;
38838 case 's':
38839 if (strcmp ("scalar", p) == 0)
38840 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
38841 else
38842 goto invalid_category;
38843 break;
38845 default:
38846 goto invalid_category;
38849 cp_lexer_consume_token (parser->lexer);
38851 if (!parens.require_close (parser))
38852 goto out_err;
38854 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
38855 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
38856 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
38857 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
38858 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
38859 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
38861 enum omp_clause_defaultmap_kind cat = category;
38862 location_t loc = OMP_CLAUSE_LOCATION (c);
38863 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
38864 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
38865 p = NULL;
38866 switch (cat)
38868 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
38869 p = NULL;
38870 break;
38871 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
38872 p = "aggregate";
38873 break;
38874 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
38875 p = "pointer";
38876 break;
38877 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
38878 p = "scalar";
38879 break;
38880 default:
38881 gcc_unreachable ();
38883 if (p)
38884 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
38886 else
38887 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
38888 "category");
38889 break;
38892 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
38893 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
38894 OMP_CLAUSE_CHAIN (c) = list;
38895 return c;
38897 out_err:
38898 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38899 /*or_comma=*/false,
38900 /*consume_paren=*/true);
38901 return list;
38904 /* OpenMP 5.0:
38905 order ( concurrent )
38907 OpenMP 5.1:
38908 order ( order-modifier : concurrent )
38910 order-modifier:
38911 reproducible
38912 unconstrained */
38914 static tree
38915 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
38917 tree c, id;
38918 const char *p;
38919 bool unconstrained = false;
38920 bool reproducible = false;
38922 matching_parens parens;
38923 if (!parens.require_open (parser))
38924 return list;
38926 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38927 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38929 id = cp_lexer_peek_token (parser->lexer)->u.value;
38930 p = IDENTIFIER_POINTER (id);
38931 if (strcmp (p, "unconstrained") == 0)
38932 unconstrained = true;
38933 else if (strcmp (p, "reproducible") == 0)
38934 reproducible = true;
38935 else
38937 cp_parser_error (parser, "expected %<reproducible%> or "
38938 "%<unconstrained%>");
38939 goto out_err;
38941 cp_lexer_consume_token (parser->lexer);
38942 cp_lexer_consume_token (parser->lexer);
38944 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38946 cp_parser_error (parser, "expected %<concurrent%>");
38947 goto out_err;
38949 else
38951 id = cp_lexer_peek_token (parser->lexer)->u.value;
38952 p = IDENTIFIER_POINTER (id);
38954 if (strcmp (p, "concurrent") != 0)
38956 cp_parser_error (parser, "expected %<concurrent%>");
38957 goto out_err;
38959 cp_lexer_consume_token (parser->lexer);
38960 if (!parens.require_close (parser))
38961 goto out_err;
38963 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
38964 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
38965 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
38966 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
38967 OMP_CLAUSE_CHAIN (c) = list;
38968 return c;
38970 out_err:
38971 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38972 /*or_comma=*/false,
38973 /*consume_paren=*/true);
38974 return list;
38977 /* OpenMP 5.0:
38978 bind ( teams | parallel | thread ) */
38980 static tree
38981 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
38982 location_t location)
38984 tree c;
38985 const char *p;
38986 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
38988 matching_parens parens;
38989 if (!parens.require_open (parser))
38990 return list;
38992 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38994 invalid:
38995 cp_parser_error (parser,
38996 "expected %<teams%>, %<parallel%> or %<thread%>");
38997 goto out_err;
38999 else
39001 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39002 p = IDENTIFIER_POINTER (id);
39004 if (strcmp (p, "teams") == 0)
39005 kind = OMP_CLAUSE_BIND_TEAMS;
39006 else if (strcmp (p, "parallel") == 0)
39007 kind = OMP_CLAUSE_BIND_PARALLEL;
39008 else if (strcmp (p, "thread") != 0)
39009 goto invalid;
39010 cp_lexer_consume_token (parser->lexer);
39011 if (!parens.require_close (parser))
39012 goto out_err;
39014 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
39015 c = build_omp_clause (location, OMP_CLAUSE_BIND);
39016 OMP_CLAUSE_BIND_KIND (c) = kind;
39017 OMP_CLAUSE_CHAIN (c) = list;
39018 return c;
39020 out_err:
39021 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39022 /*or_comma=*/false,
39023 /*consume_paren=*/true);
39024 return list;
39027 /* OpenMP 2.5:
39028 ordered
39030 OpenMP 4.5:
39031 ordered ( constant-expression ) */
39033 static tree
39034 cp_parser_omp_clause_ordered (cp_parser *parser,
39035 tree list, location_t location)
39037 tree c, num = NULL_TREE;
39038 HOST_WIDE_INT n;
39040 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
39041 "ordered", location);
39043 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39045 matching_parens parens;
39046 parens.consume_open (parser);
39048 num = cp_parser_constant_expression (parser);
39050 if (!parens.require_close (parser))
39051 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39052 /*or_comma=*/false,
39053 /*consume_paren=*/true);
39055 if (num == error_mark_node)
39056 return list;
39057 num = fold_non_dependent_expr (num);
39058 if (!tree_fits_shwi_p (num)
39059 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
39060 || (n = tree_to_shwi (num)) <= 0
39061 || (int) n != n)
39063 error_at (location,
39064 "ordered argument needs positive constant integer "
39065 "expression");
39066 return list;
39070 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
39071 OMP_CLAUSE_ORDERED_EXPR (c) = num;
39072 OMP_CLAUSE_CHAIN (c) = list;
39073 return c;
39076 /* OpenMP 2.5:
39077 reduction ( reduction-operator : variable-list )
39079 reduction-operator:
39080 One of: + * - & ^ | && ||
39082 OpenMP 3.1:
39084 reduction-operator:
39085 One of: + * - & ^ | && || min max
39087 OpenMP 4.0:
39089 reduction-operator:
39090 One of: + * - & ^ | && ||
39091 id-expression
39093 OpenMP 5.0:
39094 reduction ( reduction-modifier, reduction-operator : variable-list )
39095 in_reduction ( reduction-operator : variable-list )
39096 task_reduction ( reduction-operator : variable-list ) */
39098 static tree
39099 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
39100 bool is_omp, tree list)
39102 enum tree_code code = ERROR_MARK;
39103 tree nlist, c, id = NULL_TREE;
39104 bool task = false;
39105 bool inscan = false;
39107 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39108 return list;
39110 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
39112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
39113 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39115 cp_lexer_consume_token (parser->lexer);
39116 cp_lexer_consume_token (parser->lexer);
39118 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39119 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39121 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39122 const char *p = IDENTIFIER_POINTER (id);
39123 if (strcmp (p, "task") == 0)
39124 task = true;
39125 else if (strcmp (p, "inscan") == 0)
39126 inscan = true;
39127 if (task || inscan)
39129 cp_lexer_consume_token (parser->lexer);
39130 cp_lexer_consume_token (parser->lexer);
39135 switch (cp_lexer_peek_token (parser->lexer)->type)
39137 case CPP_PLUS: code = PLUS_EXPR; break;
39138 case CPP_MULT: code = MULT_EXPR; break;
39139 case CPP_MINUS: code = MINUS_EXPR; break;
39140 case CPP_AND: code = BIT_AND_EXPR; break;
39141 case CPP_XOR: code = BIT_XOR_EXPR; break;
39142 case CPP_OR: code = BIT_IOR_EXPR; break;
39143 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
39144 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
39145 default: break;
39148 if (code != ERROR_MARK)
39149 cp_lexer_consume_token (parser->lexer);
39150 else
39152 bool saved_colon_corrects_to_scope_p;
39153 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39154 parser->colon_corrects_to_scope_p = false;
39155 id = cp_parser_id_expression (parser, /*template_p=*/false,
39156 /*check_dependency_p=*/true,
39157 /*template_p=*/NULL,
39158 /*declarator_p=*/false,
39159 /*optional_p=*/false);
39160 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39161 if (identifier_p (id))
39163 const char *p = IDENTIFIER_POINTER (id);
39165 if (strcmp (p, "min") == 0)
39166 code = MIN_EXPR;
39167 else if (strcmp (p, "max") == 0)
39168 code = MAX_EXPR;
39169 else if (id == ovl_op_identifier (false, PLUS_EXPR))
39170 code = PLUS_EXPR;
39171 else if (id == ovl_op_identifier (false, MULT_EXPR))
39172 code = MULT_EXPR;
39173 else if (id == ovl_op_identifier (false, MINUS_EXPR))
39174 code = MINUS_EXPR;
39175 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
39176 code = BIT_AND_EXPR;
39177 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
39178 code = BIT_IOR_EXPR;
39179 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
39180 code = BIT_XOR_EXPR;
39181 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
39182 code = TRUTH_ANDIF_EXPR;
39183 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
39184 code = TRUTH_ORIF_EXPR;
39185 id = omp_reduction_id (code, id, NULL_TREE);
39186 tree scope = parser->scope;
39187 if (scope)
39188 id = build_qualified_name (NULL_TREE, scope, id, false);
39189 parser->scope = NULL_TREE;
39190 parser->qualifying_scope = NULL_TREE;
39191 parser->object_scope = NULL_TREE;
39193 else
39195 error ("invalid reduction-identifier");
39196 resync_fail:
39197 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39198 /*or_comma=*/false,
39199 /*consume_paren=*/true);
39200 return list;
39204 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39205 goto resync_fail;
39207 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
39208 NULL);
39209 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39211 OMP_CLAUSE_REDUCTION_CODE (c) = code;
39212 if (task)
39213 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
39214 else if (inscan)
39215 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
39216 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
39219 return nlist;
39222 /* OpenMP 2.5:
39223 schedule ( schedule-kind )
39224 schedule ( schedule-kind , expression )
39226 schedule-kind:
39227 static | dynamic | guided | runtime | auto
39229 OpenMP 4.5:
39230 schedule ( schedule-modifier : schedule-kind )
39231 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
39233 schedule-modifier:
39234 simd
39235 monotonic
39236 nonmonotonic */
39238 static tree
39239 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
39241 tree c, t;
39242 int modifiers = 0, nmodifiers = 0;
39244 matching_parens parens;
39245 if (!parens.require_open (parser))
39246 return list;
39248 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
39250 location_t comma = UNKNOWN_LOCATION;
39251 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39253 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39254 const char *p = IDENTIFIER_POINTER (id);
39255 if (strcmp ("simd", p) == 0)
39256 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
39257 else if (strcmp ("monotonic", p) == 0)
39258 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
39259 else if (strcmp ("nonmonotonic", p) == 0)
39260 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
39261 else
39262 break;
39263 comma = UNKNOWN_LOCATION;
39264 cp_lexer_consume_token (parser->lexer);
39265 if (nmodifiers++ == 0
39266 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39268 comma = cp_lexer_peek_token (parser->lexer)->location;
39269 cp_lexer_consume_token (parser->lexer);
39271 else
39273 cp_parser_require (parser, CPP_COLON, RT_COLON);
39274 break;
39277 if (comma != UNKNOWN_LOCATION)
39278 error_at (comma, "expected %<:%>");
39280 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39282 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39283 const char *p = IDENTIFIER_POINTER (id);
39285 switch (p[0])
39287 case 'd':
39288 if (strcmp ("dynamic", p) != 0)
39289 goto invalid_kind;
39290 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
39291 break;
39293 case 'g':
39294 if (strcmp ("guided", p) != 0)
39295 goto invalid_kind;
39296 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
39297 break;
39299 case 'r':
39300 if (strcmp ("runtime", p) != 0)
39301 goto invalid_kind;
39302 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
39303 break;
39305 default:
39306 goto invalid_kind;
39309 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39310 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
39311 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
39312 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
39313 else
39314 goto invalid_kind;
39315 cp_lexer_consume_token (parser->lexer);
39317 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
39318 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39319 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
39320 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39322 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
39323 "specified");
39324 modifiers = 0;
39327 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39329 cp_token *token;
39330 cp_lexer_consume_token (parser->lexer);
39332 token = cp_lexer_peek_token (parser->lexer);
39333 t = cp_parser_assignment_expression (parser);
39335 if (t == error_mark_node)
39336 goto resync_fail;
39337 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
39338 error_at (token->location, "schedule %<runtime%> does not take "
39339 "a %<chunk_size%> parameter");
39340 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
39341 error_at (token->location, "schedule %<auto%> does not take "
39342 "a %<chunk_size%> parameter");
39343 else
39344 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
39346 if (!parens.require_close (parser))
39347 goto resync_fail;
39349 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39350 goto resync_fail;
39352 OMP_CLAUSE_SCHEDULE_KIND (c)
39353 = (enum omp_clause_schedule_kind)
39354 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
39356 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
39357 OMP_CLAUSE_CHAIN (c) = list;
39358 return c;
39360 invalid_kind:
39361 cp_parser_error (parser, "invalid schedule kind");
39362 resync_fail:
39363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39364 /*or_comma=*/false,
39365 /*consume_paren=*/true);
39366 return list;
39369 /* OpenMP 3.0:
39370 untied */
39372 static tree
39373 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
39374 tree list, location_t location)
39376 tree c;
39378 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
39380 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
39381 OMP_CLAUSE_CHAIN (c) = list;
39382 return c;
39385 /* OpenMP 4.0:
39386 inbranch
39387 notinbranch */
39389 static tree
39390 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
39391 tree list, location_t location)
39393 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
39394 tree c = build_omp_clause (location, code);
39395 OMP_CLAUSE_CHAIN (c) = list;
39396 return c;
39399 /* OpenMP 4.0:
39400 parallel
39402 sections
39403 taskgroup */
39405 static tree
39406 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
39407 enum omp_clause_code code,
39408 tree list, location_t location)
39410 tree c = build_omp_clause (location, code);
39411 OMP_CLAUSE_CHAIN (c) = list;
39412 return c;
39415 /* OpenMP 4.5:
39416 nogroup */
39418 static tree
39419 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
39420 tree list, location_t location)
39422 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
39423 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
39424 OMP_CLAUSE_CHAIN (c) = list;
39425 return c;
39428 /* OpenMP 4.5:
39429 simd
39430 threads */
39432 static tree
39433 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
39434 enum omp_clause_code code,
39435 tree list, location_t location)
39437 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
39438 tree c = build_omp_clause (location, code);
39439 OMP_CLAUSE_CHAIN (c) = list;
39440 return c;
39443 /* OpenMP 4.0:
39444 num_teams ( expression )
39446 OpenMP 5.1:
39447 num_teams ( expression : expression ) */
39449 static tree
39450 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
39451 location_t location)
39453 tree upper, lower = NULL_TREE, c;
39455 matching_parens parens;
39456 if (!parens.require_open (parser))
39457 return list;
39459 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39460 parser->colon_corrects_to_scope_p = false;
39461 upper = cp_parser_assignment_expression (parser);
39462 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39464 if (upper != error_mark_node
39465 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39467 lower = upper;
39468 cp_lexer_consume_token (parser->lexer);
39469 upper = cp_parser_assignment_expression (parser);
39472 if (upper == error_mark_node
39473 || !parens.require_close (parser))
39474 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39475 /*or_comma=*/false,
39476 /*consume_paren=*/true);
39478 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
39479 "num_teams", location);
39481 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
39482 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
39483 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
39484 OMP_CLAUSE_CHAIN (c) = list;
39486 return c;
39489 /* OpenMP 4.0:
39490 thread_limit ( expression ) */
39492 static tree
39493 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
39494 location_t location)
39496 tree t, c;
39498 matching_parens parens;
39499 if (!parens.require_open (parser))
39500 return list;
39502 t = cp_parser_assignment_expression (parser);
39504 if (t == error_mark_node
39505 || !parens.require_close (parser))
39506 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39507 /*or_comma=*/false,
39508 /*consume_paren=*/true);
39510 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
39511 "thread_limit", location);
39513 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
39514 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
39515 OMP_CLAUSE_CHAIN (c) = list;
39517 return c;
39520 /* OpenMP 4.0:
39521 aligned ( variable-list )
39522 aligned ( variable-list : constant-expression ) */
39524 static tree
39525 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
39527 tree nlist, c, alignment = NULL_TREE;
39528 bool colon;
39530 matching_parens parens;
39531 if (!parens.require_open (parser))
39532 return list;
39534 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
39535 &colon);
39537 if (colon)
39539 alignment = cp_parser_constant_expression (parser);
39541 if (!parens.require_close (parser))
39542 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39543 /*or_comma=*/false,
39544 /*consume_paren=*/true);
39546 if (alignment == error_mark_node)
39547 alignment = NULL_TREE;
39550 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39551 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
39553 return nlist;
39556 /* OpenMP 5.0:
39557 allocate ( variable-list )
39558 allocate ( expression : variable-list )
39560 OpenMP 5.1:
39561 allocate ( allocator-modifier : variable-list )
39562 allocate ( allocator-modifier , allocator-modifier : variable-list )
39564 allocator-modifier:
39565 allocator ( expression )
39566 align ( expression ) */
39568 static tree
39569 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
39571 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
39572 bool colon, has_modifiers = false;
39574 matching_parens parens;
39575 if (!parens.require_open (parser))
39576 return list;
39578 cp_parser_parse_tentatively (parser);
39579 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39580 parser->colon_corrects_to_scope_p = false;
39581 for (int mod = 0; mod < 2; mod++)
39582 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39583 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39585 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39586 const char *p = IDENTIFIER_POINTER (id);
39587 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
39588 break;
39589 cp_lexer_consume_token (parser->lexer);
39590 matching_parens parens2;
39591 if (!parens2.require_open (parser))
39592 break;
39593 if (strcmp (p, "allocator") == 0)
39595 if (allocator != NULL_TREE)
39596 break;
39597 allocator = cp_parser_assignment_expression (parser);
39599 else
39601 if (align != NULL_TREE)
39602 break;
39603 align = cp_parser_assignment_expression (parser);
39605 if (!parens2.require_close (parser))
39606 break;
39607 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39609 has_modifiers = true;
39610 break;
39612 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
39613 break;
39614 cp_lexer_consume_token (parser->lexer);
39616 else
39617 break;
39618 if (!has_modifiers)
39620 cp_parser_abort_tentative_parse (parser);
39621 align = NULL_TREE;
39622 allocator = NULL_TREE;
39623 cp_parser_parse_tentatively (parser);
39624 allocator = cp_parser_assignment_expression (parser);
39626 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39627 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39629 cp_parser_parse_definitely (parser);
39630 cp_lexer_consume_token (parser->lexer);
39631 if (allocator == error_mark_node)
39632 allocator = NULL_TREE;
39633 if (align == error_mark_node)
39634 align = NULL_TREE;
39636 else
39638 cp_parser_abort_tentative_parse (parser);
39639 allocator = NULL_TREE;
39640 align = NULL_TREE;
39643 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
39644 &colon);
39646 if (allocator || align)
39647 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39649 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
39650 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
39653 return nlist;
39656 /* OpenMP 2.5:
39657 lastprivate ( variable-list )
39659 OpenMP 5.0:
39660 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
39662 static tree
39663 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
39665 bool conditional = false;
39667 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39668 return list;
39670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39671 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39673 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39674 const char *p = IDENTIFIER_POINTER (id);
39676 if (strcmp ("conditional", p) == 0)
39678 conditional = true;
39679 cp_lexer_consume_token (parser->lexer);
39680 cp_lexer_consume_token (parser->lexer);
39684 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
39685 list, NULL);
39687 if (conditional)
39688 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39689 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
39690 return nlist;
39693 /* OpenMP 4.0:
39694 linear ( variable-list )
39695 linear ( variable-list : expression )
39697 OpenMP 4.5:
39698 linear ( modifier ( variable-list ) )
39699 linear ( modifier ( variable-list ) : expression )
39701 modifier:
39704 uval
39706 OpenMP 5.2:
39707 linear ( variable-list : modifiers-list )
39709 modifiers:
39712 uval
39713 step ( expression ) */
39715 static tree
39716 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
39717 bool declare_simd)
39719 tree nlist, c, step = integer_one_node;
39720 bool colon;
39721 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
39722 bool old_linear_modifier = false;
39724 matching_parens parens;
39725 if (!parens.require_open (parser))
39726 return list;
39728 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39730 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39731 const char *p = IDENTIFIER_POINTER (id);
39733 if (strcmp ("ref", p) == 0)
39734 kind = OMP_CLAUSE_LINEAR_REF;
39735 else if (strcmp ("val", p) == 0)
39736 kind = OMP_CLAUSE_LINEAR_VAL;
39737 else if (strcmp ("uval", p) == 0)
39738 kind = OMP_CLAUSE_LINEAR_UVAL;
39739 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39741 cp_lexer_consume_token (parser->lexer);
39742 old_linear_modifier = true;
39744 else
39745 kind = OMP_CLAUSE_LINEAR_DEFAULT;
39748 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
39749 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
39750 &colon);
39751 else
39753 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
39754 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
39755 if (colon)
39756 cp_parser_require (parser, CPP_COLON, RT_COLON);
39757 else if (!parens.require_close (parser))
39758 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39759 /*or_comma=*/false,
39760 /*consume_paren=*/true);
39763 if (colon)
39765 bool has_modifiers = false;
39766 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
39767 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39769 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39770 const char *p = IDENTIFIER_POINTER (id);
39771 size_t pos = 0;
39772 if (strcmp ("ref", p) == 0
39773 || strcmp ("val", p) == 0
39774 || strcmp ("uval", p) == 0)
39775 pos = 2;
39776 else if (strcmp ("step", p) == 0
39777 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39779 pos = cp_parser_skip_balanced_tokens (parser, 2);
39780 if (pos == 2)
39781 pos = 0;
39783 if (pos != 0
39784 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
39785 || cp_lexer_nth_token_is (parser->lexer, pos,
39786 CPP_CLOSE_PAREN)))
39787 has_modifiers = true;
39790 step = NULL_TREE;
39791 if (has_modifiers)
39793 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39795 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39796 const char *p = IDENTIFIER_POINTER (id);
39797 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
39798 if (strcmp ("ref", p) == 0)
39799 nkind = OMP_CLAUSE_LINEAR_REF;
39800 else if (strcmp ("val", p) == 0)
39801 nkind = OMP_CLAUSE_LINEAR_VAL;
39802 else if (strcmp ("uval", p) == 0)
39803 nkind = OMP_CLAUSE_LINEAR_UVAL;
39804 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
39806 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
39807 error_at (cp_lexer_peek_token (parser->lexer)->location,
39808 "multiple linear modifiers");
39809 kind = nkind;
39810 cp_lexer_consume_token (parser->lexer);
39812 else if (strcmp ("step", p) == 0)
39814 location_t step_loc
39815 = cp_lexer_peek_token (parser->lexer)->location;
39816 cp_lexer_consume_token (parser->lexer);
39817 matching_parens parens2;
39818 if (parens2.require_open (parser))
39820 if (step)
39821 error_at (step_loc, "multiple %<step%> modifiers");
39822 if (declare_simd
39823 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39824 && cp_lexer_nth_token_is (parser->lexer, 2,
39825 CPP_CLOSE_PAREN))
39827 cp_token *token
39828 = cp_lexer_peek_token (parser->lexer);
39829 location_t tok_loc = token->location;
39830 cp_parser_parse_tentatively (parser);
39831 step = cp_parser_id_expression (parser, false, true,
39832 NULL, false, false);
39833 if (step != error_mark_node)
39834 step = cp_parser_lookup_name_simple (parser, step,
39835 tok_loc);
39836 if (step == error_mark_node)
39838 step = NULL_TREE;
39839 cp_parser_abort_tentative_parse (parser);
39841 else if (!cp_parser_parse_definitely (parser))
39842 step = NULL_TREE;
39844 if (!step)
39845 step = cp_parser_assignment_expression (parser);
39846 if (!parens2.require_close (parser))
39847 cp_parser_skip_to_closing_parenthesis (parser, true,
39848 false, true);
39850 else
39851 break;
39853 else
39854 break;
39855 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39857 cp_lexer_consume_token (parser->lexer);
39858 continue;
39860 break;
39862 if (!step)
39863 step = integer_one_node;
39865 else if (declare_simd
39866 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39867 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
39869 cp_token *token = cp_lexer_peek_token (parser->lexer);
39870 cp_parser_parse_tentatively (parser);
39871 step = cp_parser_id_expression (parser, /*template_p=*/false,
39872 /*check_dependency_p=*/true,
39873 /*template_p=*/NULL,
39874 /*declarator_p=*/false,
39875 /*optional_p=*/false);
39876 if (step != error_mark_node)
39877 step = cp_parser_lookup_name_simple (parser, step, token->location);
39878 if (step == error_mark_node)
39880 step = NULL_TREE;
39881 cp_parser_abort_tentative_parse (parser);
39883 else if (!cp_parser_parse_definitely (parser))
39884 step = NULL_TREE;
39886 if (!step)
39887 step = cp_parser_assignment_expression (parser);
39889 if (!parens.require_close (parser))
39890 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39891 /*or_comma=*/false,
39892 /*consume_paren=*/true);
39894 if (step == error_mark_node)
39895 return list;
39898 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39900 OMP_CLAUSE_LINEAR_STEP (c) = step;
39901 OMP_CLAUSE_LINEAR_KIND (c) = kind;
39902 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
39905 return nlist;
39908 /* OpenMP 4.0:
39909 safelen ( constant-expression ) */
39911 static tree
39912 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
39913 location_t location)
39915 tree t, c;
39917 matching_parens parens;
39918 if (!parens.require_open (parser))
39919 return list;
39921 t = cp_parser_constant_expression (parser);
39923 if (t == error_mark_node
39924 || !parens.require_close (parser))
39925 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39926 /*or_comma=*/false,
39927 /*consume_paren=*/true);
39929 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
39931 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
39932 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
39933 OMP_CLAUSE_CHAIN (c) = list;
39935 return c;
39938 /* OpenMP 4.0:
39939 simdlen ( constant-expression ) */
39941 static tree
39942 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
39943 location_t location)
39945 tree t, c;
39947 matching_parens parens;
39948 if (!parens.require_open (parser))
39949 return list;
39951 t = cp_parser_constant_expression (parser);
39953 if (t == error_mark_node
39954 || !parens.require_close (parser))
39955 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39956 /*or_comma=*/false,
39957 /*consume_paren=*/true);
39959 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
39961 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
39962 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
39963 OMP_CLAUSE_CHAIN (c) = list;
39965 return c;
39968 /* OpenMP 4.5:
39969 vec:
39970 identifier [+/- integer]
39971 vec , identifier [+/- integer]
39974 static tree
39975 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
39976 tree list, bool depend_p)
39978 tree vec = NULL;
39980 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39982 cp_parser_error (parser, "expected identifier");
39983 return list;
39986 if (!depend_p)
39988 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39989 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
39990 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
39991 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
39992 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
39994 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
39995 if (integer_onep (val))
39997 cp_lexer_consume_token (parser->lexer);
39998 cp_lexer_consume_token (parser->lexer);
39999 cp_lexer_consume_token (parser->lexer);
40000 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40001 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40002 OMP_CLAUSE_CHAIN (u) = list;
40003 return u;
40008 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40010 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
40011 tree t, identifier = cp_parser_identifier (parser);
40012 tree addend = NULL;
40014 if (identifier == error_mark_node)
40015 t = error_mark_node;
40016 else
40018 t = cp_parser_lookup_name_simple
40019 (parser, identifier,
40020 cp_lexer_peek_token (parser->lexer)->location);
40021 if (t == error_mark_node)
40022 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
40023 id_loc);
40026 bool neg = false;
40027 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
40028 neg = true;
40029 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
40031 addend = integer_zero_node;
40032 goto add_to_vector;
40034 cp_lexer_consume_token (parser->lexer);
40036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
40038 cp_parser_error (parser, "expected integer");
40039 return list;
40042 addend = cp_lexer_peek_token (parser->lexer)->u.value;
40043 if (TREE_CODE (addend) != INTEGER_CST)
40045 cp_parser_error (parser, "expected integer");
40046 return list;
40048 cp_lexer_consume_token (parser->lexer);
40050 add_to_vector:
40051 if (t != error_mark_node)
40053 vec = tree_cons (addend, t, vec);
40054 if (neg)
40055 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
40058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
40059 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40060 break;
40062 cp_lexer_consume_token (parser->lexer);
40065 if (vec)
40067 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40068 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40069 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
40070 OMP_CLAUSE_DECL (u) = nreverse (vec);
40071 OMP_CLAUSE_CHAIN (u) = list;
40072 return u;
40074 return list;
40077 /* OpenMP 5.0:
40078 detach ( event-handle ) */
40080 static tree
40081 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
40083 matching_parens parens;
40085 if (!parens.require_open (parser))
40086 return list;
40088 cp_token *token;
40089 tree name, decl;
40091 token = cp_lexer_peek_token (parser->lexer);
40092 name = cp_parser_id_expression (parser, /*template_p=*/false,
40093 /*check_dependency_p=*/true,
40094 /*template_p=*/NULL,
40095 /*declarator_p=*/false,
40096 /*optional_p=*/false);
40097 if (name == error_mark_node)
40098 decl = error_mark_node;
40099 else
40101 if (identifier_p (name))
40102 decl = cp_parser_lookup_name_simple (parser, name, token->location);
40103 else
40104 decl = name;
40105 if (decl == error_mark_node)
40106 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
40107 token->location);
40110 if (decl == error_mark_node
40111 || !parens.require_close (parser))
40112 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40113 /*or_comma=*/false,
40114 /*consume_paren=*/true);
40116 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
40117 OMP_CLAUSE_DECL (u) = decl;
40118 OMP_CLAUSE_CHAIN (u) = list;
40120 return u;
40123 /* OpenMP 5.0:
40124 iterators ( iterators-definition )
40126 iterators-definition:
40127 iterator-specifier
40128 iterator-specifier , iterators-definition
40130 iterator-specifier:
40131 identifier = range-specification
40132 iterator-type identifier = range-specification
40134 range-specification:
40135 begin : end
40136 begin : end : step */
40138 static tree
40139 cp_parser_omp_iterators (cp_parser *parser)
40141 tree ret = NULL_TREE, *last = &ret;
40142 cp_lexer_consume_token (parser->lexer);
40144 matching_parens parens;
40145 if (!parens.require_open (parser))
40146 return error_mark_node;
40148 bool saved_colon_corrects_to_scope_p
40149 = parser->colon_corrects_to_scope_p;
40150 bool saved_colon_doesnt_start_class_def_p
40151 = parser->colon_doesnt_start_class_def_p;
40155 tree iter_type;
40156 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40157 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
40158 iter_type = integer_type_node;
40159 else
40161 const char *saved_message
40162 = parser->type_definition_forbidden_message;
40163 parser->type_definition_forbidden_message
40164 = G_("types may not be defined in iterator type");
40166 iter_type = cp_parser_type_id (parser);
40168 parser->type_definition_forbidden_message = saved_message;
40171 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40172 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40174 cp_parser_error (parser, "expected identifier");
40175 break;
40178 tree id = cp_parser_identifier (parser);
40179 if (id == error_mark_node)
40180 break;
40182 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40183 break;
40185 parser->colon_corrects_to_scope_p = false;
40186 parser->colon_doesnt_start_class_def_p = true;
40187 tree begin = cp_parser_assignment_expression (parser);
40189 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40190 break;
40192 tree end = cp_parser_assignment_expression (parser);
40194 tree step = integer_one_node;
40195 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40197 cp_lexer_consume_token (parser->lexer);
40198 step = cp_parser_assignment_expression (parser);
40201 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
40202 DECL_ARTIFICIAL (iter_var) = 1;
40203 DECL_CONTEXT (iter_var) = current_function_decl;
40204 pushdecl (iter_var);
40206 *last = make_tree_vec (6);
40207 TREE_VEC_ELT (*last, 0) = iter_var;
40208 TREE_VEC_ELT (*last, 1) = begin;
40209 TREE_VEC_ELT (*last, 2) = end;
40210 TREE_VEC_ELT (*last, 3) = step;
40211 last = &TREE_CHAIN (*last);
40213 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40215 cp_lexer_consume_token (parser->lexer);
40216 continue;
40218 break;
40220 while (1);
40222 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40223 parser->colon_doesnt_start_class_def_p
40224 = saved_colon_doesnt_start_class_def_p;
40226 if (!parens.require_close (parser))
40227 cp_parser_skip_to_closing_parenthesis (parser,
40228 /*recovering=*/true,
40229 /*or_comma=*/false,
40230 /*consume_paren=*/true);
40232 return ret ? ret : error_mark_node;
40235 /* OpenMP 5.0:
40236 affinity ( [aff-modifier :] variable-list )
40237 aff-modifier:
40238 iterator ( iterators-definition ) */
40240 static tree
40241 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
40243 tree nlist, c, iterators = NULL_TREE;
40245 matching_parens parens;
40246 if (!parens.require_open (parser))
40247 return list;
40249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40252 const char *p = IDENTIFIER_POINTER (id);
40253 bool parse_iter = ((strcmp ("iterator", p) == 0)
40254 && (cp_lexer_nth_token_is (parser->lexer, 2,
40255 CPP_OPEN_PAREN)));
40256 if (parse_iter)
40258 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
40259 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
40261 if (parse_iter)
40263 begin_scope (sk_omp, NULL);
40264 iterators = cp_parser_omp_iterators (parser);
40265 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40267 if (iterators)
40268 poplevel (0, 1, 0);
40269 cp_parser_skip_to_closing_parenthesis (parser,
40270 /*recovering=*/true,
40271 /*or_comma=*/false,
40272 /*consume_paren=*/true);
40273 return list;
40277 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
40278 list, NULL);
40279 if (iterators)
40281 tree block = poplevel (1, 1, 0);
40282 if (iterators != error_mark_node)
40284 TREE_VEC_ELT (iterators, 5) = block;
40285 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40286 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
40287 OMP_CLAUSE_DECL (c));
40290 return nlist;
40293 /* OpenMP 4.0:
40294 depend ( depend-kind : variable-list )
40296 depend-kind:
40297 in | out | inout
40299 OpenMP 4.5:
40300 depend ( source )
40302 depend ( sink : vec )
40304 OpenMP 5.0:
40305 depend ( depend-modifier , depend-kind: variable-list )
40307 depend-kind:
40308 in | out | inout | mutexinoutset | depobj
40310 depend-modifier:
40311 iterator ( iterators-definition ) */
40313 static tree
40314 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
40316 tree nlist, c, iterators = NULL_TREE;
40317 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
40318 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
40320 matching_parens parens;
40321 if (!parens.require_open (parser))
40322 return list;
40326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40327 goto invalid_kind;
40329 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40330 const char *p = IDENTIFIER_POINTER (id);
40332 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
40334 begin_scope (sk_omp, NULL);
40335 iterators = cp_parser_omp_iterators (parser);
40336 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
40337 continue;
40339 if (strcmp ("in", p) == 0)
40340 kind = OMP_CLAUSE_DEPEND_IN;
40341 else if (strcmp ("inout", p) == 0)
40342 kind = OMP_CLAUSE_DEPEND_INOUT;
40343 else if (strcmp ("inoutset", p) == 0)
40344 kind = OMP_CLAUSE_DEPEND_INOUTSET;
40345 else if (strcmp ("mutexinoutset", p) == 0)
40346 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
40347 else if (strcmp ("out", p) == 0)
40348 kind = OMP_CLAUSE_DEPEND_OUT;
40349 else if (strcmp ("depobj", p) == 0)
40350 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
40351 else if (strcmp ("sink", p) == 0)
40352 dkind = OMP_CLAUSE_DOACROSS_SINK;
40353 else if (strcmp ("source", p) == 0)
40354 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
40355 else
40356 goto invalid_kind;
40357 break;
40359 while (1);
40361 cp_lexer_consume_token (parser->lexer);
40363 if (iterators
40364 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
40365 || dkind == OMP_CLAUSE_DOACROSS_SINK))
40367 poplevel (0, 1, 0);
40368 error_at (loc, "%<iterator%> modifier incompatible with %qs",
40369 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
40370 iterators = NULL_TREE;
40373 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
40375 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
40376 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
40377 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
40378 OMP_CLAUSE_DECL (c) = NULL_TREE;
40379 OMP_CLAUSE_CHAIN (c) = list;
40380 if (!parens.require_close (parser))
40381 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40382 /*or_comma=*/false,
40383 /*consume_paren=*/true);
40384 return c;
40387 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40388 goto resync_fail;
40390 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
40392 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
40393 if (!parens.require_close (parser))
40394 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40395 /*or_comma=*/false,
40396 /*consume_paren=*/true);
40398 else
40400 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
40401 list, NULL);
40403 if (iterators)
40405 tree block = poplevel (1, 1, 0);
40406 if (iterators == error_mark_node)
40407 iterators = NULL_TREE;
40408 else
40409 TREE_VEC_ELT (iterators, 5) = block;
40412 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40414 OMP_CLAUSE_DEPEND_KIND (c) = kind;
40415 if (iterators)
40416 OMP_CLAUSE_DECL (c)
40417 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
40420 return nlist;
40422 invalid_kind:
40423 cp_parser_error (parser, "invalid depend kind");
40424 resync_fail:
40425 if (iterators)
40426 poplevel (0, 1, 0);
40427 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40428 /*or_comma=*/false,
40429 /*consume_paren=*/true);
40430 return list;
40433 /* OpenMP 5.2:
40434 doacross ( source : )
40435 doacross ( source : omp_cur_iteration )
40437 doacross ( sink : vec )
40438 doacross ( sink : omp_cur_iteration - logical_iteration ) */
40440 static tree
40441 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
40443 tree nlist;
40444 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
40446 matching_parens parens;
40447 if (!parens.require_open (parser))
40448 return list;
40450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40452 invalid_kind:
40453 cp_parser_error (parser, "invalid doacross kind");
40454 resync_fail:
40455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40456 /*or_comma=*/false,
40457 /*consume_paren=*/true);
40458 return list;
40461 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40462 const char *p = IDENTIFIER_POINTER (id);
40464 if (strcmp ("sink", p) == 0)
40465 kind = OMP_CLAUSE_DOACROSS_SINK;
40466 else if (strcmp ("source", p) == 0)
40467 kind = OMP_CLAUSE_DOACROSS_SOURCE;
40468 else
40469 goto invalid_kind;
40471 cp_lexer_consume_token (parser->lexer);
40473 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40474 goto resync_fail;
40476 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
40478 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40480 id = cp_lexer_peek_token (parser->lexer)->u.value;
40481 p = IDENTIFIER_POINTER (id);
40482 if (strcmp (p, "omp_cur_iteration") == 0)
40483 cp_lexer_consume_token (parser->lexer);
40485 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
40486 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
40487 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
40488 OMP_CLAUSE_CHAIN (nlist) = list;
40490 else
40491 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
40493 if (!parens.require_close (parser))
40494 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40495 /*or_comma=*/false,
40496 /*consume_paren=*/true);
40497 return nlist;
40500 /* OpenMP 4.0:
40501 from ( variable-list )
40502 to ( variable-list )
40504 OpenMP 5.1:
40505 from ( [present :] variable-list )
40506 to ( [present :] variable-list ) */
40508 static tree
40509 cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind,
40510 tree list)
40512 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40513 return list;
40515 bool present = false;
40516 cp_token *token = cp_lexer_peek_token (parser->lexer);
40518 if (token->type == CPP_NAME
40519 && strcmp (IDENTIFIER_POINTER (token->u.value), "present") == 0
40520 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40522 present = true;
40523 cp_lexer_consume_token (parser->lexer);
40524 cp_lexer_consume_token (parser->lexer);
40527 tree nl = cp_parser_omp_var_list_no_open (parser, kind, list, NULL, true);
40528 if (present)
40529 for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
40530 OMP_CLAUSE_MOTION_PRESENT (c) = 1;
40532 return nl;
40535 /* OpenMP 4.0:
40536 map ( map-kind : variable-list )
40537 map ( variable-list )
40539 map-kind:
40540 alloc | to | from | tofrom
40542 OpenMP 4.5:
40543 map-kind:
40544 alloc | to | from | tofrom | release | delete
40546 map ( always [,] map-kind: variable-list )
40548 OpenMP 5.0:
40549 map ( [map-type-modifier[,] ...] map-kind: variable-list )
40551 map-type-modifier:
40552 always | close */
40554 static tree
40555 cp_parser_omp_clause_map (cp_parser *parser, tree list)
40557 tree nlist, c;
40558 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
40560 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40561 return list;
40563 int pos = 1;
40564 int map_kind_pos = 0;
40565 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
40566 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
40568 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
40570 map_kind_pos = pos;
40571 break;
40574 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
40575 pos++;
40576 pos++;
40579 bool always_modifier = false;
40580 bool close_modifier = false;
40581 bool present_modifier = false;
40582 for (int pos = 1; pos < map_kind_pos; ++pos)
40584 cp_token *tok = cp_lexer_peek_token (parser->lexer);
40585 if (tok->type == CPP_COMMA)
40587 cp_lexer_consume_token (parser->lexer);
40588 continue;
40591 const char *p = IDENTIFIER_POINTER (tok->u.value);
40592 if (strcmp ("always", p) == 0)
40594 if (always_modifier)
40596 cp_parser_error (parser, "too many %<always%> modifiers");
40597 cp_parser_skip_to_closing_parenthesis (parser,
40598 /*recovering=*/true,
40599 /*or_comma=*/false,
40600 /*consume_paren=*/true);
40601 return list;
40603 always_modifier = true;
40605 else if (strcmp ("close", p) == 0)
40607 if (close_modifier)
40609 cp_parser_error (parser, "too many %<close%> modifiers");
40610 cp_parser_skip_to_closing_parenthesis (parser,
40611 /*recovering=*/true,
40612 /*or_comma=*/false,
40613 /*consume_paren=*/true);
40614 return list;
40616 close_modifier = true;
40618 else if (strcmp ("present", p) == 0)
40620 if (present_modifier)
40622 cp_parser_error (parser, "too many %<present%> modifiers");
40623 cp_parser_skip_to_closing_parenthesis (parser,
40624 /*recovering=*/true,
40625 /*or_comma=*/false,
40626 /*consume_paren=*/true);
40627 return list;
40629 present_modifier = true;
40631 else
40633 cp_parser_error (parser, "%<map%> clause with map-type modifier other"
40634 " than %<always%>, %<close%> or %<present%>");
40635 cp_parser_skip_to_closing_parenthesis (parser,
40636 /*recovering=*/true,
40637 /*or_comma=*/false,
40638 /*consume_paren=*/true);
40639 return list;
40642 cp_lexer_consume_token (parser->lexer);
40645 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40646 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
40648 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40649 const char *p = IDENTIFIER_POINTER (id);
40650 int always_present_modifier = always_modifier && present_modifier;
40652 if (strcmp ("alloc", p) == 0)
40653 kind = present_modifier ? GOMP_MAP_PRESENT_ALLOC : GOMP_MAP_ALLOC;
40654 else if (strcmp ("to", p) == 0)
40655 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TO
40656 : present_modifier ? GOMP_MAP_PRESENT_TO
40657 : always_modifier ? GOMP_MAP_ALWAYS_TO
40658 : GOMP_MAP_TO);
40659 else if (strcmp ("from", p) == 0)
40660 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_FROM
40661 : present_modifier ? GOMP_MAP_PRESENT_FROM
40662 : always_modifier ? GOMP_MAP_ALWAYS_FROM
40663 : GOMP_MAP_FROM);
40664 else if (strcmp ("tofrom", p) == 0)
40665 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TOFROM
40666 : present_modifier ? GOMP_MAP_PRESENT_TOFROM
40667 : always_modifier ? GOMP_MAP_ALWAYS_TOFROM
40668 : GOMP_MAP_TOFROM);
40669 else if (strcmp ("release", p) == 0)
40670 kind = GOMP_MAP_RELEASE;
40671 else
40673 cp_parser_error (parser, "invalid map kind");
40674 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40675 /*or_comma=*/false,
40676 /*consume_paren=*/true);
40677 return list;
40679 cp_lexer_consume_token (parser->lexer);
40680 cp_lexer_consume_token (parser->lexer);
40682 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
40683 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
40685 kind = GOMP_MAP_DELETE;
40686 cp_lexer_consume_token (parser->lexer);
40687 cp_lexer_consume_token (parser->lexer);
40690 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
40691 NULL, true);
40693 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40694 OMP_CLAUSE_SET_MAP_KIND (c, kind);
40696 return nlist;
40699 /* OpenMP 4.0:
40700 device ( expression )
40702 OpenMP 5.0:
40703 device ( [device-modifier :] integer-expression )
40705 device-modifier:
40706 ancestor | device_num */
40708 static tree
40709 cp_parser_omp_clause_device (cp_parser *parser, tree list,
40710 location_t location)
40712 tree t, c;
40713 bool ancestor = false;
40715 matching_parens parens;
40716 if (!parens.require_open (parser))
40717 return list;
40719 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40720 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40722 cp_token *tok = cp_lexer_peek_token (parser->lexer);
40723 const char *p = IDENTIFIER_POINTER (tok->u.value);
40724 if (strcmp ("ancestor", p) == 0)
40726 ancestor = true;
40728 /* A requires directive with the reverse_offload clause must be
40729 specified. */
40730 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
40732 error_at (tok->location, "%<ancestor%> device modifier not "
40733 "preceded by %<requires%> directive "
40734 "with %<reverse_offload%> clause");
40735 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40736 return list;
40739 else if (strcmp ("device_num", p) == 0)
40741 else
40743 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
40744 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40745 return list;
40747 cp_lexer_consume_token (parser->lexer);
40748 cp_lexer_consume_token (parser->lexer);
40751 t = cp_parser_assignment_expression (parser);
40753 if (t == error_mark_node
40754 || !parens.require_close (parser))
40755 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40756 /*or_comma=*/false,
40757 /*consume_paren=*/true);
40759 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
40760 "device", location);
40762 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
40763 OMP_CLAUSE_DEVICE_ID (c) = t;
40764 OMP_CLAUSE_CHAIN (c) = list;
40765 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
40767 return c;
40770 /* OpenMP 4.0:
40771 dist_schedule ( static )
40772 dist_schedule ( static , expression ) */
40774 static tree
40775 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
40776 location_t location)
40778 tree c, t;
40780 matching_parens parens;
40781 if (!parens.require_open (parser))
40782 return list;
40784 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
40786 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
40787 goto invalid_kind;
40788 cp_lexer_consume_token (parser->lexer);
40790 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40792 cp_lexer_consume_token (parser->lexer);
40794 t = cp_parser_assignment_expression (parser);
40796 if (t == error_mark_node)
40797 goto resync_fail;
40798 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
40800 if (!parens.require_close (parser))
40801 goto resync_fail;
40803 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40804 goto resync_fail;
40806 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
40807 "dist_schedule", location); */
40808 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
40809 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
40810 OMP_CLAUSE_CHAIN (c) = list;
40811 return c;
40813 invalid_kind:
40814 cp_parser_error (parser, "invalid dist_schedule kind");
40815 resync_fail:
40816 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40817 /*or_comma=*/false,
40818 /*consume_paren=*/true);
40819 return list;
40822 /* OpenMP 4.0:
40823 proc_bind ( proc-bind-kind )
40825 proc-bind-kind:
40826 primary | master | close | spread
40827 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
40829 static tree
40830 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
40831 location_t location)
40833 tree c;
40834 enum omp_clause_proc_bind_kind kind;
40836 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40837 return list;
40839 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40841 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40842 const char *p = IDENTIFIER_POINTER (id);
40844 if (strcmp ("primary", p) == 0)
40845 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
40846 else if (strcmp ("master", p) == 0)
40847 kind = OMP_CLAUSE_PROC_BIND_MASTER;
40848 else if (strcmp ("close", p) == 0)
40849 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
40850 else if (strcmp ("spread", p) == 0)
40851 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
40852 else
40853 goto invalid_kind;
40855 else
40856 goto invalid_kind;
40858 cp_lexer_consume_token (parser->lexer);
40859 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40860 goto resync_fail;
40862 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
40863 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
40864 location);
40865 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
40866 OMP_CLAUSE_CHAIN (c) = list;
40867 return c;
40869 invalid_kind:
40870 cp_parser_error (parser, "invalid depend kind");
40871 resync_fail:
40872 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40873 /*or_comma=*/false,
40874 /*consume_paren=*/true);
40875 return list;
40878 /* OpenMP 5.0:
40879 device_type ( host | nohost | any ) */
40881 static tree
40882 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
40883 location_t location)
40885 tree c;
40886 enum omp_clause_device_type_kind kind;
40888 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40889 return list;
40891 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40893 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40894 const char *p = IDENTIFIER_POINTER (id);
40896 if (strcmp ("host", p) == 0)
40897 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
40898 else if (strcmp ("nohost", p) == 0)
40899 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
40900 else if (strcmp ("any", p) == 0)
40901 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
40902 else
40903 goto invalid_kind;
40905 else
40906 goto invalid_kind;
40908 cp_lexer_consume_token (parser->lexer);
40909 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40910 goto resync_fail;
40912 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
40913 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
40914 location);
40915 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
40916 OMP_CLAUSE_CHAIN (c) = list;
40917 return c;
40919 invalid_kind:
40920 cp_parser_error (parser, "invalid depend kind");
40921 resync_fail:
40922 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40923 /*or_comma=*/false,
40924 /*consume_paren=*/true);
40925 return list;
40928 /* OpenACC:
40929 async [( int-expr )] */
40931 static tree
40932 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
40934 tree c, t;
40935 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40937 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
40939 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40941 matching_parens parens;
40942 parens.consume_open (parser);
40944 t = cp_parser_assignment_expression (parser);
40945 if (t == error_mark_node
40946 || !parens.require_close (parser))
40947 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40948 /*or_comma=*/false,
40949 /*consume_paren=*/true);
40952 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
40954 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
40955 OMP_CLAUSE_ASYNC_EXPR (c) = t;
40956 OMP_CLAUSE_CHAIN (c) = list;
40957 list = c;
40959 return list;
40962 /* Parse all OpenACC clauses. The set clauses allowed by the directive
40963 is a bitmask in MASK. Return the list of clauses found. */
40965 static tree
40966 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
40967 const char *where, cp_token *pragma_tok,
40968 bool finish_p = true)
40970 tree clauses = NULL;
40971 bool first = true;
40973 /* Don't create location wrapper nodes within OpenACC clauses. */
40974 auto_suppress_location_wrappers sentinel;
40976 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40978 location_t here;
40979 pragma_omp_clause c_kind;
40980 omp_clause_code code;
40981 const char *c_name;
40982 tree prev = clauses;
40984 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40985 cp_lexer_consume_token (parser->lexer);
40987 here = cp_lexer_peek_token (parser->lexer)->location;
40988 c_kind = cp_parser_omp_clause_name (parser);
40990 switch (c_kind)
40992 case PRAGMA_OACC_CLAUSE_ASYNC:
40993 clauses = cp_parser_oacc_clause_async (parser, clauses);
40994 c_name = "async";
40995 break;
40996 case PRAGMA_OACC_CLAUSE_AUTO:
40997 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
40998 clauses);
40999 c_name = "auto";
41000 break;
41001 case PRAGMA_OACC_CLAUSE_ATTACH:
41002 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41003 c_name = "attach";
41004 break;
41005 case PRAGMA_OACC_CLAUSE_COLLAPSE:
41006 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
41007 c_name = "collapse";
41008 break;
41009 case PRAGMA_OACC_CLAUSE_COPY:
41010 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41011 c_name = "copy";
41012 break;
41013 case PRAGMA_OACC_CLAUSE_COPYIN:
41014 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41015 c_name = "copyin";
41016 break;
41017 case PRAGMA_OACC_CLAUSE_COPYOUT:
41018 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41019 c_name = "copyout";
41020 break;
41021 case PRAGMA_OACC_CLAUSE_CREATE:
41022 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41023 c_name = "create";
41024 break;
41025 case PRAGMA_OACC_CLAUSE_DELETE:
41026 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41027 c_name = "delete";
41028 break;
41029 case PRAGMA_OMP_CLAUSE_DEFAULT:
41030 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
41031 c_name = "default";
41032 break;
41033 case PRAGMA_OACC_CLAUSE_DETACH:
41034 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41035 c_name = "detach";
41036 break;
41037 case PRAGMA_OACC_CLAUSE_DEVICE:
41038 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41039 c_name = "device";
41040 break;
41041 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
41042 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
41043 c_name = "deviceptr";
41044 break;
41045 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
41046 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41047 c_name = "device_resident";
41048 break;
41049 case PRAGMA_OACC_CLAUSE_FINALIZE:
41050 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
41051 clauses);
41052 c_name = "finalize";
41053 break;
41054 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
41055 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41056 clauses);
41057 c_name = "firstprivate";
41058 break;
41059 case PRAGMA_OACC_CLAUSE_GANG:
41060 c_name = "gang";
41061 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
41062 c_name, clauses);
41063 break;
41064 case PRAGMA_OACC_CLAUSE_HOST:
41065 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41066 c_name = "host";
41067 break;
41068 case PRAGMA_OACC_CLAUSE_IF:
41069 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
41070 c_name = "if";
41071 break;
41072 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
41073 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
41074 clauses);
41075 c_name = "if_present";
41076 break;
41077 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
41078 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
41079 clauses);
41080 c_name = "independent";
41081 break;
41082 case PRAGMA_OACC_CLAUSE_LINK:
41083 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41084 c_name = "link";
41085 break;
41086 case PRAGMA_OACC_CLAUSE_NO_CREATE:
41087 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41088 c_name = "no_create";
41089 break;
41090 case PRAGMA_OACC_CLAUSE_NOHOST:
41091 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
41092 clauses);
41093 c_name = "nohost";
41094 break;
41095 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
41096 code = OMP_CLAUSE_NUM_GANGS;
41097 c_name = "num_gangs";
41098 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41099 clauses);
41100 break;
41101 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
41102 c_name = "num_workers";
41103 code = OMP_CLAUSE_NUM_WORKERS;
41104 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41105 clauses);
41106 break;
41107 case PRAGMA_OACC_CLAUSE_PRESENT:
41108 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41109 c_name = "present";
41110 break;
41111 case PRAGMA_OACC_CLAUSE_PRIVATE:
41112 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41113 clauses);
41114 c_name = "private";
41115 break;
41116 case PRAGMA_OACC_CLAUSE_REDUCTION:
41117 clauses
41118 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41119 false, clauses);
41120 c_name = "reduction";
41121 break;
41122 case PRAGMA_OACC_CLAUSE_SEQ:
41123 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
41124 clauses);
41125 c_name = "seq";
41126 break;
41127 case PRAGMA_OACC_CLAUSE_TILE:
41128 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
41129 c_name = "tile";
41130 break;
41131 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
41132 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41133 clauses);
41134 c_name = "use_device";
41135 break;
41136 case PRAGMA_OACC_CLAUSE_VECTOR:
41137 c_name = "vector";
41138 clauses = cp_parser_oacc_shape_clause (parser, here,
41139 OMP_CLAUSE_VECTOR,
41140 c_name, clauses);
41141 break;
41142 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
41143 c_name = "vector_length";
41144 code = OMP_CLAUSE_VECTOR_LENGTH;
41145 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41146 clauses);
41147 break;
41148 case PRAGMA_OACC_CLAUSE_WAIT:
41149 clauses = cp_parser_oacc_clause_wait (parser, clauses);
41150 c_name = "wait";
41151 break;
41152 case PRAGMA_OACC_CLAUSE_WORKER:
41153 c_name = "worker";
41154 clauses = cp_parser_oacc_shape_clause (parser, here,
41155 OMP_CLAUSE_WORKER,
41156 c_name, clauses);
41157 break;
41158 default:
41159 cp_parser_error (parser, "expected an OpenACC clause");
41160 goto saw_error;
41163 first = false;
41165 if (((mask >> c_kind) & 1) == 0)
41167 /* Remove the invalid clause(s) from the list to avoid
41168 confusing the rest of the compiler. */
41169 clauses = prev;
41170 error_at (here, "%qs is not valid for %qs", c_name, where);
41174 saw_error:
41175 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41177 if (finish_p)
41178 return finish_omp_clauses (clauses, C_ORT_ACC);
41180 return clauses;
41183 /* Parse all OpenMP clauses. The set clauses allowed by the directive
41184 is a bitmask in MASK. Return the list of clauses found.
41185 FINISH_P set if finish_omp_clauses should be called.
41186 NESTED non-zero if clauses should be terminated by closing paren instead
41187 of end of pragma. If it is 2, additionally commas are required in between
41188 the clauses. */
41190 static tree
41191 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
41192 const char *where, cp_token *pragma_tok,
41193 bool finish_p = true, int nested = 0)
41195 tree clauses = NULL;
41196 bool first = true;
41197 cp_token *token = NULL;
41199 /* Don't create location wrapper nodes within OpenMP clauses. */
41200 auto_suppress_location_wrappers sentinel;
41202 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41204 pragma_omp_clause c_kind;
41205 const char *c_name;
41206 tree prev = clauses;
41208 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
41209 break;
41211 if (!first || nested != 2)
41213 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41214 cp_lexer_consume_token (parser->lexer);
41215 else if (nested == 2)
41216 error_at (cp_lexer_peek_token (parser->lexer)->location,
41217 "clauses in %<simd%> trait should be separated "
41218 "by %<,%>");
41221 token = cp_lexer_peek_token (parser->lexer);
41222 c_kind = cp_parser_omp_clause_name (parser);
41224 switch (c_kind)
41226 case PRAGMA_OMP_CLAUSE_BIND:
41227 clauses = cp_parser_omp_clause_bind (parser, clauses,
41228 token->location);
41229 c_name = "bind";
41230 break;
41231 case PRAGMA_OMP_CLAUSE_COLLAPSE:
41232 clauses = cp_parser_omp_clause_collapse (parser, clauses,
41233 token->location);
41234 c_name = "collapse";
41235 break;
41236 case PRAGMA_OMP_CLAUSE_COPYIN:
41237 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
41238 c_name = "copyin";
41239 break;
41240 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
41241 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
41242 clauses);
41243 c_name = "copyprivate";
41244 break;
41245 case PRAGMA_OMP_CLAUSE_DEFAULT:
41246 clauses = cp_parser_omp_clause_default (parser, clauses,
41247 token->location, false);
41248 c_name = "default";
41249 break;
41250 case PRAGMA_OMP_CLAUSE_FILTER:
41251 clauses = cp_parser_omp_clause_filter (parser, clauses,
41252 token->location);
41253 c_name = "filter";
41254 break;
41255 case PRAGMA_OMP_CLAUSE_FINAL:
41256 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
41257 c_name = "final";
41258 break;
41259 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
41260 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41261 clauses);
41262 c_name = "firstprivate";
41263 break;
41264 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
41265 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
41266 token->location);
41267 c_name = "grainsize";
41268 break;
41269 case PRAGMA_OMP_CLAUSE_HINT:
41270 clauses = cp_parser_omp_clause_hint (parser, clauses,
41271 token->location);
41272 c_name = "hint";
41273 break;
41274 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
41275 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
41276 token->location);
41277 c_name = "defaultmap";
41278 break;
41279 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
41280 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41281 clauses);
41282 c_name = "use_device_ptr";
41283 break;
41284 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
41285 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
41286 clauses);
41287 c_name = "use_device_addr";
41288 break;
41289 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
41290 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
41291 clauses);
41292 c_name = "is_device_ptr";
41293 break;
41294 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
41295 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
41296 clauses);
41297 c_name = "has_device_addr";
41298 break;
41299 case PRAGMA_OMP_CLAUSE_IF:
41300 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
41301 true);
41302 c_name = "if";
41303 break;
41304 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
41305 clauses
41306 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
41307 true, clauses);
41308 c_name = "in_reduction";
41309 break;
41310 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
41311 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
41312 c_name = "lastprivate";
41313 break;
41314 case PRAGMA_OMP_CLAUSE_MERGEABLE:
41315 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
41316 token->location);
41317 c_name = "mergeable";
41318 break;
41319 case PRAGMA_OMP_CLAUSE_NOWAIT:
41320 clauses = cp_parser_omp_clause_nowait (parser, clauses,
41321 token->location);
41322 c_name = "nowait";
41323 break;
41324 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
41325 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
41326 token->location);
41327 c_name = "num_tasks";
41328 break;
41329 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
41330 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
41331 token->location);
41332 c_name = "num_threads";
41333 break;
41334 case PRAGMA_OMP_CLAUSE_ORDER:
41335 clauses = cp_parser_omp_clause_order (parser, clauses,
41336 token->location);
41337 c_name = "order";
41338 break;
41339 case PRAGMA_OMP_CLAUSE_ORDERED:
41340 clauses = cp_parser_omp_clause_ordered (parser, clauses,
41341 token->location);
41342 c_name = "ordered";
41343 break;
41344 case PRAGMA_OMP_CLAUSE_PRIORITY:
41345 clauses = cp_parser_omp_clause_priority (parser, clauses,
41346 token->location);
41347 c_name = "priority";
41348 break;
41349 case PRAGMA_OMP_CLAUSE_PRIVATE:
41350 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41351 clauses);
41352 c_name = "private";
41353 break;
41354 case PRAGMA_OMP_CLAUSE_REDUCTION:
41355 clauses
41356 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41357 true, clauses);
41358 c_name = "reduction";
41359 break;
41360 case PRAGMA_OMP_CLAUSE_SCHEDULE:
41361 clauses = cp_parser_omp_clause_schedule (parser, clauses,
41362 token->location);
41363 c_name = "schedule";
41364 break;
41365 case PRAGMA_OMP_CLAUSE_SHARED:
41366 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
41367 clauses);
41368 c_name = "shared";
41369 break;
41370 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
41371 clauses
41372 = cp_parser_omp_clause_reduction (parser,
41373 OMP_CLAUSE_TASK_REDUCTION,
41374 true, clauses);
41375 c_name = "task_reduction";
41376 break;
41377 case PRAGMA_OMP_CLAUSE_UNTIED:
41378 clauses = cp_parser_omp_clause_untied (parser, clauses,
41379 token->location);
41380 c_name = "untied";
41381 break;
41382 case PRAGMA_OMP_CLAUSE_INBRANCH:
41383 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
41384 clauses, token->location);
41385 c_name = "inbranch";
41386 break;
41387 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
41388 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
41389 clauses);
41390 c_name = "nontemporal";
41391 break;
41392 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
41393 clauses = cp_parser_omp_clause_branch (parser,
41394 OMP_CLAUSE_NOTINBRANCH,
41395 clauses, token->location);
41396 c_name = "notinbranch";
41397 break;
41398 case PRAGMA_OMP_CLAUSE_PARALLEL:
41399 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
41400 clauses, token->location);
41401 c_name = "parallel";
41402 if (!first)
41404 clause_not_first:
41405 error_at (token->location, "%qs must be the first clause of %qs",
41406 c_name, where);
41407 clauses = prev;
41409 break;
41410 case PRAGMA_OMP_CLAUSE_FOR:
41411 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
41412 clauses, token->location);
41413 c_name = "for";
41414 if (!first)
41415 goto clause_not_first;
41416 break;
41417 case PRAGMA_OMP_CLAUSE_SECTIONS:
41418 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
41419 clauses, token->location);
41420 c_name = "sections";
41421 if (!first)
41422 goto clause_not_first;
41423 break;
41424 case PRAGMA_OMP_CLAUSE_TASKGROUP:
41425 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
41426 clauses, token->location);
41427 c_name = "taskgroup";
41428 if (!first)
41429 goto clause_not_first;
41430 break;
41431 case PRAGMA_OMP_CLAUSE_LINK:
41432 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
41433 c_name = "link";
41434 break;
41435 case PRAGMA_OMP_CLAUSE_TO:
41436 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
41438 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
41439 clauses);
41440 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
41441 OMP_CLAUSE_ENTER_TO (c) = 1;
41442 clauses = nl;
41444 else
41445 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_TO,
41446 clauses);
41447 c_name = "to";
41448 break;
41449 case PRAGMA_OMP_CLAUSE_FROM:
41450 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_FROM,
41451 clauses);
41452 c_name = "from";
41453 break;
41454 case PRAGMA_OMP_CLAUSE_UNIFORM:
41455 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
41456 clauses);
41457 c_name = "uniform";
41458 break;
41459 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
41460 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
41461 token->location);
41462 c_name = "num_teams";
41463 break;
41464 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
41465 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
41466 token->location);
41467 c_name = "thread_limit";
41468 break;
41469 case PRAGMA_OMP_CLAUSE_ALIGNED:
41470 clauses = cp_parser_omp_clause_aligned (parser, clauses);
41471 c_name = "aligned";
41472 break;
41473 case PRAGMA_OMP_CLAUSE_ALLOCATE:
41474 clauses = cp_parser_omp_clause_allocate (parser, clauses);
41475 c_name = "allocate";
41476 break;
41477 case PRAGMA_OMP_CLAUSE_LINEAR:
41479 bool declare_simd = false;
41480 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
41481 declare_simd = true;
41482 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
41484 c_name = "linear";
41485 break;
41486 case PRAGMA_OMP_CLAUSE_AFFINITY:
41487 clauses = cp_parser_omp_clause_affinity (parser, clauses);
41488 c_name = "affinity";
41489 break;
41490 case PRAGMA_OMP_CLAUSE_DEPEND:
41491 clauses = cp_parser_omp_clause_depend (parser, clauses,
41492 token->location);
41493 c_name = "depend";
41494 break;
41495 case PRAGMA_OMP_CLAUSE_DOACROSS:
41496 clauses = cp_parser_omp_clause_doacross (parser, clauses,
41497 token->location);
41498 c_name = "doacross";
41499 break;
41500 case PRAGMA_OMP_CLAUSE_DETACH:
41501 clauses = cp_parser_omp_clause_detach (parser, clauses);
41502 c_name = "detach";
41503 break;
41504 case PRAGMA_OMP_CLAUSE_MAP:
41505 clauses = cp_parser_omp_clause_map (parser, clauses);
41506 c_name = "map";
41507 break;
41508 case PRAGMA_OMP_CLAUSE_DEVICE:
41509 clauses = cp_parser_omp_clause_device (parser, clauses,
41510 token->location);
41511 c_name = "device";
41512 break;
41513 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
41514 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
41515 token->location);
41516 c_name = "dist_schedule";
41517 break;
41518 case PRAGMA_OMP_CLAUSE_PROC_BIND:
41519 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
41520 token->location);
41521 c_name = "proc_bind";
41522 break;
41523 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
41524 clauses = cp_parser_omp_clause_device_type (parser, clauses,
41525 token->location);
41526 c_name = "device_type";
41527 break;
41528 case PRAGMA_OMP_CLAUSE_SAFELEN:
41529 clauses = cp_parser_omp_clause_safelen (parser, clauses,
41530 token->location);
41531 c_name = "safelen";
41532 break;
41533 case PRAGMA_OMP_CLAUSE_SIMDLEN:
41534 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
41535 token->location);
41536 c_name = "simdlen";
41537 break;
41538 case PRAGMA_OMP_CLAUSE_NOGROUP:
41539 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
41540 token->location);
41541 c_name = "nogroup";
41542 break;
41543 case PRAGMA_OMP_CLAUSE_THREADS:
41544 clauses
41545 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
41546 clauses, token->location);
41547 c_name = "threads";
41548 break;
41549 case PRAGMA_OMP_CLAUSE_SIMD:
41550 clauses
41551 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
41552 clauses, token->location);
41553 c_name = "simd";
41554 break;
41555 case PRAGMA_OMP_CLAUSE_ENTER:
41556 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
41557 clauses);
41558 c_name = "enter";
41559 break;
41560 default:
41561 cp_parser_error (parser, "expected an OpenMP clause");
41562 goto saw_error;
41565 first = false;
41567 if (((mask >> c_kind) & 1) == 0)
41569 /* Remove the invalid clause(s) from the list to avoid
41570 confusing the rest of the compiler. */
41571 clauses = prev;
41572 error_at (token->location, "%qs is not valid for %qs", c_name, where);
41575 saw_error:
41576 if (!nested)
41577 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41578 if (finish_p)
41580 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
41581 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
41582 else
41583 return finish_omp_clauses (clauses, C_ORT_OMP);
41585 return clauses;
41588 /* OpenMP 2.5:
41589 structured-block:
41590 statement
41592 In practice, we're also interested in adding the statement to an
41593 outer node. So it is convenient if we work around the fact that
41594 cp_parser_statement calls add_stmt. */
41596 static unsigned
41597 cp_parser_begin_omp_structured_block (cp_parser *parser)
41599 unsigned save = parser->in_statement;
41601 /* Only move the values to IN_OMP_BLOCK if they weren't false.
41602 This preserves the "not within loop or switch" style error messages
41603 for nonsense cases like
41604 void foo() {
41605 #pragma omp single
41606 break;
41609 if (parser->in_statement)
41610 parser->in_statement = IN_OMP_BLOCK;
41612 return save;
41615 static void
41616 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
41618 parser->in_statement = save;
41621 static tree
41622 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
41624 tree stmt = begin_omp_structured_block ();
41625 unsigned int save = cp_parser_begin_omp_structured_block (parser);
41627 parser->omp_attrs_forbidden_p = true;
41628 cp_parser_statement (parser, NULL_TREE, false, if_p);
41630 cp_parser_end_omp_structured_block (parser, save);
41631 return finish_omp_structured_block (stmt);
41634 /* OpenMP 5.x:
41635 # pragma omp allocate (list) clauses
41637 OpenMP 5.0 clause:
41638 allocator (omp_allocator_handle_t expression)
41640 OpenMP 5.1 additional clause:
41641 align (constant-expression)] */
41643 static void
41644 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
41646 tree allocator = NULL_TREE;
41647 tree alignment = NULL_TREE;
41648 location_t loc = pragma_tok->location;
41649 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
41653 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41654 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41655 cp_lexer_consume_token (parser->lexer);
41657 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41658 break;
41659 matching_parens parens;
41660 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41661 const char *p = IDENTIFIER_POINTER (id);
41662 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41663 cp_lexer_consume_token (parser->lexer);
41664 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
41666 error_at (cloc, "expected %<allocator%> or %<align%>");
41667 break;
41669 if (!parens.require_open (parser))
41670 break;
41671 tree expr = cp_parser_assignment_expression (parser);
41672 if (p[2] == 'i' && alignment)
41674 error_at (cloc, "too many %qs clauses", "align");
41675 break;
41677 else if (p[2] == 'i')
41679 if (expr != error_mark_node)
41680 alignment = expr;
41681 /* FIXME: Remove when adding check to semantics.cc; cf FIXME below. */
41682 if (alignment
41683 && !type_dependent_expression_p (alignment)
41684 && !INTEGRAL_TYPE_P (TREE_TYPE (alignment)))
41686 error_at (cloc, "%<align%> clause argument needs to be "
41687 "positive constant power of two integer "
41688 "expression");
41689 alignment = NULL_TREE;
41691 else if (alignment)
41693 alignment = mark_rvalue_use (alignment);
41694 if (!processing_template_decl)
41696 alignment = maybe_constant_value (alignment);
41697 if (TREE_CODE (alignment) != INTEGER_CST
41698 || !tree_fits_uhwi_p (alignment)
41699 || !integer_pow2p (alignment))
41701 error_at (cloc, "%<align%> clause argument needs to be "
41702 "positive constant power of two integer "
41703 "expression");
41704 alignment = NULL_TREE;
41709 else if (allocator)
41711 error_at (cloc, "too many %qs clauses", "allocator");
41712 break;
41714 else
41716 if (expr != error_mark_node)
41717 allocator = expr;
41719 parens.require_close (parser);
41720 } while (true);
41721 cp_parser_require_pragma_eol (parser, pragma_tok);
41723 if (allocator || alignment)
41724 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
41726 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
41727 OMP_CLAUSE_ALLOCATE_ALIGN (c) = alignment;
41730 /* FIXME: When implementing properly, delete the align/allocate expr error
41731 check above and add one in semantics.cc (to properly handle templates).
41732 Base this on the allocator/align modifiers check for the 'allocate' clause
41733 in semantics.cc's finish_omp_clauses. */
41734 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
41737 /* OpenMP 2.5:
41738 # pragma omp atomic new-line
41739 expression-stmt
41741 expression-stmt:
41742 x binop= expr | x++ | ++x | x-- | --x
41743 binop:
41744 +, *, -, /, &, ^, |, <<, >>
41746 where x is an lvalue expression with scalar type.
41748 OpenMP 3.1:
41749 # pragma omp atomic new-line
41750 update-stmt
41752 # pragma omp atomic read new-line
41753 read-stmt
41755 # pragma omp atomic write new-line
41756 write-stmt
41758 # pragma omp atomic update new-line
41759 update-stmt
41761 # pragma omp atomic capture new-line
41762 capture-stmt
41764 # pragma omp atomic capture new-line
41765 capture-block
41767 read-stmt:
41768 v = x
41769 write-stmt:
41770 x = expr
41771 update-stmt:
41772 expression-stmt | x = x binop expr
41773 capture-stmt:
41774 v = expression-stmt
41775 capture-block:
41776 { v = x; update-stmt; } | { update-stmt; v = x; }
41778 OpenMP 4.0:
41779 update-stmt:
41780 expression-stmt | x = x binop expr | x = expr binop x
41781 capture-stmt:
41782 v = update-stmt
41783 capture-block:
41784 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
41786 OpenMP 5.1:
41787 # pragma omp atomic compare new-line
41788 conditional-update-atomic
41790 # pragma omp atomic compare capture new-line
41791 conditional-update-capture-atomic
41793 conditional-update-atomic:
41794 cond-expr-stmt | cond-update-stmt
41795 cond-expr-stmt:
41796 x = expr ordop x ? expr : x;
41797 x = x ordop expr ? expr : x;
41798 x = x == e ? d : x;
41799 cond-update-stmt:
41800 if (expr ordop x) { x = expr; }
41801 if (x ordop expr) { x = expr; }
41802 if (x == e) { x = d; }
41803 ordop:
41804 <, >
41805 conditional-update-capture-atomic:
41806 v = cond-expr-stmt
41807 { v = x; cond-expr-stmt }
41808 { cond-expr-stmt v = x; }
41809 { v = x; cond-update-stmt }
41810 { cond-update-stmt v = x; }
41811 if (x == e) { x = d; } else { v = x; }
41812 { r = x == e; if (r) { x = d; } }
41813 { r = x == e; if (r) { x = d; } else { v = x; } }
41815 where x, r and v are lvalue expressions with scalar type,
41816 expr, e and d are expressions with scalar type and e might be
41817 the same as v. */
41819 static void
41820 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
41822 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
41823 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
41824 location_t loc = pragma_tok->location;
41825 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
41826 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
41827 bool structured_block = false;
41828 tree clauses = NULL_TREE;
41829 bool capture = false;
41830 bool compare = false;
41831 bool weak = false;
41832 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41833 bool no_semicolon = false;
41834 bool extra_scope = false;
41836 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41838 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41839 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41840 cp_lexer_consume_token (parser->lexer);
41842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41844 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41845 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41846 const char *p = IDENTIFIER_POINTER (id);
41847 enum tree_code new_code = ERROR_MARK;
41848 enum omp_memory_order new_memory_order
41849 = OMP_MEMORY_ORDER_UNSPECIFIED;
41850 bool new_capture = false;
41851 bool new_compare = false;
41852 bool new_weak = false;
41853 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41855 if (!strcmp (p, "read"))
41856 new_code = OMP_ATOMIC_READ;
41857 else if (!strcmp (p, "write"))
41858 new_code = NOP_EXPR;
41859 else if (!strcmp (p, "update"))
41860 new_code = OMP_ATOMIC;
41861 else if (openacc && !strcmp (p, "capture"))
41862 new_code = OMP_ATOMIC_CAPTURE_NEW;
41863 else if (openacc)
41865 p = NULL;
41866 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
41867 "or %<capture%> clause");
41869 else if (!strcmp (p, "capture"))
41870 new_capture = true;
41871 else if (!strcmp (p, "compare"))
41872 new_compare = true;
41873 else if (!strcmp (p, "weak"))
41874 new_weak = true;
41875 else if (!strcmp (p, "fail"))
41877 matching_parens parens;
41879 cp_lexer_consume_token (parser->lexer);
41880 if (!parens.require_open (parser))
41881 continue;
41883 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41885 id = cp_lexer_peek_token (parser->lexer)->u.value;
41886 const char *q = IDENTIFIER_POINTER (id);
41888 if (!strcmp (q, "seq_cst"))
41889 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
41890 else if (!strcmp (q, "acquire"))
41891 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
41892 else if (!strcmp (q, "relaxed"))
41893 new_fail = OMP_MEMORY_ORDER_RELAXED;
41896 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41898 cp_lexer_consume_token (parser->lexer);
41899 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41900 error_at (cloc, "too many %qs clauses", "fail");
41901 else
41902 fail = new_fail;
41904 else
41905 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
41906 "or %<relaxed%>");
41907 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
41908 || !parens.require_close (parser))
41909 cp_parser_skip_to_closing_parenthesis (parser,
41910 /*recovering=*/true,
41911 /*or_comma=*/false,
41912 /*consume_paren=*/true);
41913 continue;
41915 else if (!strcmp (p, "seq_cst"))
41916 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41917 else if (!strcmp (p, "acq_rel"))
41918 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
41919 else if (!strcmp (p, "release"))
41920 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
41921 else if (!strcmp (p, "acquire"))
41922 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
41923 else if (!strcmp (p, "relaxed"))
41924 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
41925 else if (!strcmp (p, "hint"))
41927 cp_lexer_consume_token (parser->lexer);
41928 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
41929 continue;
41931 else
41933 p = NULL;
41934 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
41935 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
41936 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
41937 "%<relaxed%> or %<hint%> clause");
41939 if (p)
41941 if (new_code != ERROR_MARK)
41943 /* OpenACC permits 'update capture'. */
41944 if (openacc
41945 && code == OMP_ATOMIC
41946 && new_code == OMP_ATOMIC_CAPTURE_NEW)
41947 code = new_code;
41948 else if (code != ERROR_MARK)
41949 error_at (cloc, "too many atomic clauses");
41950 else
41951 code = new_code;
41953 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
41955 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
41956 error_at (cloc, "too many memory order clauses");
41957 else
41958 memory_order = new_memory_order;
41960 else if (new_capture)
41962 if (capture)
41963 error_at (cloc, "too many %qs clauses", "capture");
41964 else
41965 capture = true;
41967 else if (new_compare)
41969 if (compare)
41970 error_at (cloc, "too many %qs clauses", "compare");
41971 else
41972 compare = true;
41974 else if (new_weak)
41976 if (weak)
41977 error_at (cloc, "too many %qs clauses", "weak");
41978 else
41979 weak = true;
41981 cp_lexer_consume_token (parser->lexer);
41982 continue;
41985 break;
41987 cp_parser_require_pragma_eol (parser, pragma_tok);
41989 if (code == ERROR_MARK)
41990 code = OMP_ATOMIC;
41991 if (capture)
41993 if (code != OMP_ATOMIC)
41994 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
41995 "clauses", "capture");
41996 else
41997 code = OMP_ATOMIC_CAPTURE_NEW;
41999 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
42001 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42002 "clauses", "compare");
42003 compare = false;
42005 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
42007 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
42008 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42010 if (weak && !compare)
42012 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
42013 weak = false;
42015 if (openacc)
42016 memory_order = OMP_MEMORY_ORDER_RELAXED;
42017 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
42019 omp_requires_mask
42020 = (enum omp_requires) (omp_requires_mask
42021 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
42022 switch ((enum omp_memory_order)
42023 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
42025 case OMP_MEMORY_ORDER_UNSPECIFIED:
42026 case OMP_MEMORY_ORDER_RELAXED:
42027 memory_order = OMP_MEMORY_ORDER_RELAXED;
42028 break;
42029 case OMP_MEMORY_ORDER_SEQ_CST:
42030 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42031 break;
42032 case OMP_MEMORY_ORDER_ACQ_REL:
42033 switch (code)
42035 case OMP_ATOMIC_READ:
42036 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42037 break;
42038 case NOP_EXPR: /* atomic write */
42039 memory_order = OMP_MEMORY_ORDER_RELEASE;
42040 break;
42041 default:
42042 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42043 break;
42045 break;
42046 default:
42047 gcc_unreachable ();
42050 else
42051 switch (code)
42053 case OMP_ATOMIC_READ:
42054 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
42056 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
42057 "%<release%> clause");
42058 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42060 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42061 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42062 break;
42063 case NOP_EXPR: /* atomic write */
42064 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
42066 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
42067 "%<acquire%> clause");
42068 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42070 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42071 memory_order = OMP_MEMORY_ORDER_RELEASE;
42072 break;
42073 default:
42074 break;
42076 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42077 memory_order
42078 = (enum omp_memory_order) (memory_order
42079 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
42081 switch (code)
42083 case OMP_ATOMIC_READ:
42084 case NOP_EXPR: /* atomic write */
42085 v = cp_parser_unary_expression (parser);
42086 if (v == error_mark_node)
42087 goto saw_error;
42088 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42089 goto saw_error;
42090 if (code == NOP_EXPR)
42091 lhs = cp_parser_expression (parser);
42092 else
42093 lhs = cp_parser_unary_expression (parser);
42094 if (lhs == error_mark_node)
42095 goto saw_error;
42096 if (code == NOP_EXPR)
42098 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
42099 opcode. */
42100 code = OMP_ATOMIC;
42101 rhs = lhs;
42102 lhs = v;
42103 v = NULL_TREE;
42105 goto done;
42106 case OMP_ATOMIC_CAPTURE_NEW:
42107 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42109 cp_lexer_consume_token (parser->lexer);
42110 structured_block = true;
42112 else if (compare
42113 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42114 break;
42115 else
42117 v = cp_parser_unary_expression (parser);
42118 if (v == error_mark_node)
42119 goto saw_error;
42120 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42121 goto saw_error;
42122 if (compare
42123 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42125 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42126 error_at (eloc, "expected expression");
42127 goto saw_error;
42130 default:
42131 break;
42134 restart:
42135 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42137 cp_lexer_consume_token (parser->lexer);
42139 matching_parens parens;
42140 if (!parens.require_open (parser))
42141 goto saw_error;
42142 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42143 tree cmp_expr;
42144 if (r)
42145 cmp_expr = cp_parser_unary_expression (parser);
42146 else
42147 cmp_expr = cp_parser_binary_expression (parser, false, true,
42148 PREC_NOT_OPERATOR, NULL);
42149 if (!parens.require_close (parser))
42150 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
42151 if (cmp_expr == error_mark_node)
42152 goto saw_error;
42153 if (r)
42155 if (!cp_tree_equal (cmp_expr, r))
42156 goto bad_if;
42157 cmp_expr = rhs;
42158 rhs = NULL_TREE;
42159 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
42161 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42163 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
42165 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
42166 "expected %<==%> comparison in %<if%> condition");
42167 goto saw_error;
42169 else if (TREE_CODE (cmp_expr) != GT_EXPR
42170 && TREE_CODE (cmp_expr) != LT_EXPR)
42172 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
42173 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
42174 "condition");
42175 goto saw_error;
42177 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
42178 goto saw_error;
42180 extra_scope = true;
42181 eloc = cp_lexer_peek_token (parser->lexer)->location;
42182 lhs = cp_parser_unary_expression (parser);
42183 orig_lhs = lhs;
42184 if (lhs == error_mark_node)
42185 goto saw_error;
42186 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
42188 cp_parser_error (parser, "expected %<=%>");
42189 goto saw_error;
42191 cp_lexer_consume_token (parser->lexer);
42192 eloc = cp_lexer_peek_token (parser->lexer)->location;
42193 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42194 rhs1 = cp_parser_expression (parser);
42195 else
42196 rhs1 = cp_parser_simple_cast_expression (parser);
42198 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42199 goto saw_error;
42201 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
42202 goto saw_error;
42204 extra_scope = false;
42205 no_semicolon = true;
42207 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
42209 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42211 opcode = COND_EXPR;
42212 rhs = TREE_OPERAND (cmp_expr, 1);
42214 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
42216 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
42217 ? MIN_EXPR : MAX_EXPR);
42218 rhs = rhs1;
42219 rhs1 = TREE_OPERAND (cmp_expr, 0);
42221 else
42222 goto bad_if;
42224 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
42225 goto bad_if;
42226 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
42227 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
42229 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
42230 ? MAX_EXPR : MIN_EXPR);
42231 rhs = rhs1;
42232 rhs1 = TREE_OPERAND (cmp_expr, 1);
42234 else
42236 bad_if:
42237 cp_parser_error (parser,
42238 "invalid form of %<#pragma omp atomic compare%>");
42239 goto saw_error;
42242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
42244 if (code != OMP_ATOMIC_CAPTURE_NEW
42245 || (structured_block && r == NULL_TREE)
42246 || TREE_CODE (cmp_expr) != EQ_EXPR)
42248 eloc = cp_lexer_peek_token (parser->lexer)->location;
42249 error_at (eloc, "unexpected %<else%>");
42250 goto saw_error;
42253 cp_lexer_consume_token (parser->lexer);
42255 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
42256 goto saw_error;
42258 extra_scope = true;
42259 v = cp_parser_unary_expression (parser);
42260 if (v == error_mark_node)
42261 goto saw_error;
42262 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42263 goto saw_error;
42265 tree expr = cp_parser_simple_cast_expression (parser);
42267 if (!cp_tree_equal (expr, lhs))
42268 goto bad_if;
42270 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42271 goto saw_error;
42273 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
42274 goto saw_error;
42276 extra_scope = false;
42277 code = OMP_ATOMIC_CAPTURE_OLD;
42278 if (r == NULL_TREE)
42279 /* Signal to c_finish_omp_atomic that in
42280 if (x == e) { x = d; } else { v = x; }
42281 case the store to v should be conditional. */
42282 r = void_list_node;
42284 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42286 cp_parser_error (parser, "expected %<else%>");
42287 goto saw_error;
42289 else if (code == OMP_ATOMIC_CAPTURE_NEW
42290 && r != NULL_TREE
42291 && v == NULL_TREE)
42292 code = OMP_ATOMIC;
42293 goto stmt_done;
42295 lhs = cp_parser_unary_expression (parser);
42296 orig_lhs = lhs;
42297 switch (TREE_CODE (lhs))
42299 case ERROR_MARK:
42300 goto saw_error;
42302 case POSTINCREMENT_EXPR:
42303 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42304 code = OMP_ATOMIC_CAPTURE_OLD;
42305 /* FALLTHROUGH */
42306 case PREINCREMENT_EXPR:
42307 lhs = TREE_OPERAND (lhs, 0);
42308 opcode = PLUS_EXPR;
42309 rhs = integer_one_node;
42310 if (compare)
42311 goto invalid_compare;
42312 break;
42314 case POSTDECREMENT_EXPR:
42315 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42316 code = OMP_ATOMIC_CAPTURE_OLD;
42317 /* FALLTHROUGH */
42318 case PREDECREMENT_EXPR:
42319 lhs = TREE_OPERAND (lhs, 0);
42320 opcode = MINUS_EXPR;
42321 rhs = integer_one_node;
42322 if (compare)
42323 goto invalid_compare;
42324 break;
42326 case COMPOUND_EXPR:
42327 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
42328 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
42329 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
42330 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
42331 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
42332 (TREE_OPERAND (lhs, 1), 0), 0)))
42333 == BOOLEAN_TYPE)
42334 /* Undo effects of boolean_increment for post {in,de}crement. */
42335 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
42336 /* FALLTHRU */
42337 case MODIFY_EXPR:
42338 if (TREE_CODE (lhs) == MODIFY_EXPR
42339 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
42341 /* Undo effects of boolean_increment. */
42342 if (integer_onep (TREE_OPERAND (lhs, 1)))
42344 /* This is pre or post increment. */
42345 rhs = TREE_OPERAND (lhs, 1);
42346 lhs = TREE_OPERAND (lhs, 0);
42347 opcode = NOP_EXPR;
42348 if (code == OMP_ATOMIC_CAPTURE_NEW
42349 && !structured_block
42350 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
42351 code = OMP_ATOMIC_CAPTURE_OLD;
42352 if (compare)
42353 goto invalid_compare;
42354 break;
42357 /* FALLTHRU */
42358 default:
42359 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
42361 cp_parser_error (parser, "expected %<=%>");
42362 goto saw_error;
42364 switch (cp_lexer_peek_token (parser->lexer)->type)
42366 case CPP_MULT_EQ:
42367 opcode = MULT_EXPR;
42368 break;
42369 case CPP_DIV_EQ:
42370 opcode = TRUNC_DIV_EXPR;
42371 break;
42372 case CPP_PLUS_EQ:
42373 opcode = PLUS_EXPR;
42374 break;
42375 case CPP_MINUS_EQ:
42376 opcode = MINUS_EXPR;
42377 break;
42378 case CPP_LSHIFT_EQ:
42379 opcode = LSHIFT_EXPR;
42380 break;
42381 case CPP_RSHIFT_EQ:
42382 opcode = RSHIFT_EXPR;
42383 break;
42384 case CPP_AND_EQ:
42385 opcode = BIT_AND_EXPR;
42386 break;
42387 case CPP_OR_EQ:
42388 opcode = BIT_IOR_EXPR;
42389 break;
42390 case CPP_XOR_EQ:
42391 opcode = BIT_XOR_EXPR;
42392 break;
42393 case CPP_EQ:
42394 enum cp_parser_prec oprec;
42395 cp_token *token;
42396 cp_lexer_consume_token (parser->lexer);
42397 cp_parser_parse_tentatively (parser);
42398 rhs1 = cp_parser_simple_cast_expression (parser);
42399 if (rhs1 == error_mark_node)
42401 cp_parser_abort_tentative_parse (parser);
42402 cp_parser_simple_cast_expression (parser);
42403 goto saw_error;
42405 token = cp_lexer_peek_token (parser->lexer);
42406 if (token->type != CPP_SEMICOLON
42407 && (!compare || token->type != CPP_QUERY)
42408 && !cp_tree_equal (lhs, rhs1))
42410 cp_parser_abort_tentative_parse (parser);
42411 cp_parser_parse_tentatively (parser);
42412 rhs = cp_parser_binary_expression (parser, false, true,
42413 PREC_NOT_OPERATOR, NULL);
42414 if (rhs == error_mark_node)
42416 cp_parser_abort_tentative_parse (parser);
42417 cp_parser_binary_expression (parser, false, true,
42418 PREC_NOT_OPERATOR, NULL);
42419 goto saw_error;
42421 switch (TREE_CODE (rhs))
42423 case MULT_EXPR:
42424 case TRUNC_DIV_EXPR:
42425 case RDIV_EXPR:
42426 case PLUS_EXPR:
42427 case MINUS_EXPR:
42428 case LSHIFT_EXPR:
42429 case RSHIFT_EXPR:
42430 case BIT_AND_EXPR:
42431 case BIT_IOR_EXPR:
42432 case BIT_XOR_EXPR:
42433 if (compare)
42434 break;
42435 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
42437 if (cp_parser_parse_definitely (parser))
42439 opcode = TREE_CODE (rhs);
42440 rhs1 = TREE_OPERAND (rhs, 0);
42441 rhs = TREE_OPERAND (rhs, 1);
42442 goto stmt_done;
42444 else
42445 goto saw_error;
42447 break;
42448 case EQ_EXPR:
42449 if (!compare
42450 || code != OMP_ATOMIC_CAPTURE_NEW
42451 || !structured_block
42452 || v
42453 || r)
42454 break;
42455 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
42456 && cp_lexer_nth_token_is_keyword (parser->lexer,
42457 2, RID_IF))
42459 if (cp_parser_parse_definitely (parser))
42461 r = lhs;
42462 lhs = NULL_TREE;
42463 rhs1 = NULL_TREE;
42464 cp_lexer_consume_token (parser->lexer);
42465 goto restart;
42468 break;
42469 case GT_EXPR:
42470 case LT_EXPR:
42471 if (compare
42472 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
42473 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
42474 && cp_parser_parse_definitely (parser))
42476 opcode = TREE_CODE (rhs);
42477 rhs1 = TREE_OPERAND (rhs, 0);
42478 rhs = TREE_OPERAND (rhs, 1);
42479 cond_expr:
42480 cp_lexer_consume_token (parser->lexer);
42481 bool saved_colon_corrects_to_scope_p
42482 = parser->colon_corrects_to_scope_p;
42483 parser->colon_corrects_to_scope_p = false;
42484 tree e1 = cp_parser_expression (parser);
42485 parser->colon_corrects_to_scope_p
42486 = saved_colon_corrects_to_scope_p;
42487 cp_parser_require (parser, CPP_COLON, RT_COLON);
42488 tree e2 = cp_parser_simple_cast_expression (parser);
42489 if (cp_tree_equal (lhs, e2))
42491 if (cp_tree_equal (lhs, rhs1))
42493 if (opcode == EQ_EXPR)
42495 opcode = COND_EXPR;
42496 rhs1 = e1;
42497 goto stmt_done;
42499 if (cp_tree_equal (rhs, e1))
42501 opcode
42502 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
42503 rhs = e1;
42504 goto stmt_done;
42507 else
42509 gcc_assert (opcode != EQ_EXPR);
42510 if (cp_tree_equal (rhs1, e1))
42512 opcode
42513 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
42514 rhs1 = rhs;
42515 rhs = e1;
42516 goto stmt_done;
42520 cp_parser_error (parser,
42521 "invalid form of "
42522 "%<#pragma omp atomic compare%>");
42523 goto saw_error;
42525 break;
42526 default:
42527 break;
42529 cp_parser_abort_tentative_parse (parser);
42530 if (structured_block
42531 && code == OMP_ATOMIC_CAPTURE_OLD
42532 && !compare)
42534 rhs = cp_parser_expression (parser);
42535 if (rhs == error_mark_node)
42536 goto saw_error;
42537 opcode = NOP_EXPR;
42538 rhs1 = NULL_TREE;
42539 goto stmt_done;
42541 cp_parser_error (parser,
42542 "invalid form of %<#pragma omp atomic%>");
42543 goto saw_error;
42545 if (!cp_parser_parse_definitely (parser))
42546 goto saw_error;
42547 switch (token->type)
42549 case CPP_SEMICOLON:
42550 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
42552 code = OMP_ATOMIC_CAPTURE_OLD;
42553 v = lhs;
42554 lhs = NULL_TREE;
42555 lhs1 = rhs1;
42556 rhs1 = NULL_TREE;
42557 cp_lexer_consume_token (parser->lexer);
42558 goto restart;
42560 else if (structured_block && !compare)
42562 opcode = NOP_EXPR;
42563 rhs = rhs1;
42564 rhs1 = NULL_TREE;
42565 goto stmt_done;
42567 cp_parser_error (parser,
42568 "invalid form of %<#pragma omp atomic%>");
42569 goto saw_error;
42570 case CPP_MULT:
42571 opcode = MULT_EXPR;
42572 break;
42573 case CPP_DIV:
42574 opcode = TRUNC_DIV_EXPR;
42575 break;
42576 case CPP_PLUS:
42577 opcode = PLUS_EXPR;
42578 break;
42579 case CPP_MINUS:
42580 opcode = MINUS_EXPR;
42581 break;
42582 case CPP_LSHIFT:
42583 opcode = LSHIFT_EXPR;
42584 break;
42585 case CPP_RSHIFT:
42586 opcode = RSHIFT_EXPR;
42587 break;
42588 case CPP_AND:
42589 opcode = BIT_AND_EXPR;
42590 break;
42591 case CPP_OR:
42592 opcode = BIT_IOR_EXPR;
42593 break;
42594 case CPP_XOR:
42595 opcode = BIT_XOR_EXPR;
42596 break;
42597 case CPP_EQ_EQ:
42598 opcode = EQ_EXPR;
42599 break;
42600 case CPP_GREATER:
42601 opcode = GT_EXPR;
42602 break;
42603 case CPP_LESS:
42604 opcode = LT_EXPR;
42605 break;
42606 default:
42607 cp_parser_error (parser,
42608 "invalid operator for %<#pragma omp atomic%>");
42609 goto saw_error;
42611 if (compare
42612 && TREE_CODE_CLASS (opcode) != tcc_comparison)
42614 cp_parser_error (parser,
42615 "invalid form of "
42616 "%<#pragma omp atomic compare%>");
42617 goto saw_error;
42619 oprec = TOKEN_PRECEDENCE (token);
42620 gcc_assert (oprec != PREC_NOT_OPERATOR);
42621 if (commutative_tree_code (opcode))
42622 oprec = (enum cp_parser_prec) (oprec - 1);
42623 cp_lexer_consume_token (parser->lexer);
42624 rhs = cp_parser_binary_expression (parser, false, false,
42625 oprec, NULL);
42626 if (rhs == error_mark_node)
42627 goto saw_error;
42628 if (compare)
42630 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
42632 cp_parser_error (parser,
42633 "invalid form of "
42634 "%<#pragma omp atomic compare%>");
42635 goto saw_error;
42637 goto cond_expr;
42639 goto stmt_done;
42640 default:
42641 cp_parser_error (parser,
42642 "invalid operator for %<#pragma omp atomic%>");
42643 goto saw_error;
42645 cp_lexer_consume_token (parser->lexer);
42647 rhs = cp_parser_expression (parser);
42648 if (rhs == error_mark_node)
42649 goto saw_error;
42650 break;
42652 stmt_done:
42653 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
42655 if (!no_semicolon
42656 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42657 goto saw_error;
42658 no_semicolon = false;
42659 v = cp_parser_unary_expression (parser);
42660 if (v == error_mark_node)
42661 goto saw_error;
42662 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42663 goto saw_error;
42664 lhs1 = cp_parser_unary_expression (parser);
42665 if (lhs1 == error_mark_node)
42666 goto saw_error;
42668 if (structured_block)
42670 if (!no_semicolon)
42671 cp_parser_consume_semicolon_at_end_of_statement (parser);
42672 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
42674 done:
42675 if (weak && opcode != COND_EXPR)
42677 error_at (loc, "%<weak%> clause requires atomic equality comparison");
42678 weak = false;
42680 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42681 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
42682 rhs1, r, clauses, memory_order, weak);
42683 if (!structured_block && !no_semicolon)
42684 cp_parser_consume_semicolon_at_end_of_statement (parser);
42685 return;
42687 invalid_compare:
42688 error ("invalid form of %<pragma omp atomic compare%>");
42689 /* FALLTHRU */
42690 saw_error:
42691 cp_parser_skip_to_end_of_block_or_statement (parser);
42692 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42693 cp_lexer_consume_token (parser->lexer);
42694 if (structured_block)
42696 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42697 cp_lexer_consume_token (parser->lexer);
42698 else if (code == OMP_ATOMIC_CAPTURE_NEW)
42700 cp_parser_skip_to_end_of_block_or_statement (parser);
42701 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42702 cp_lexer_consume_token (parser->lexer);
42708 /* OpenMP 2.5:
42709 # pragma omp barrier new-line */
42711 static void
42712 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
42714 cp_parser_require_pragma_eol (parser, pragma_tok);
42715 finish_omp_barrier ();
42718 /* OpenMP 2.5:
42719 # pragma omp critical [(name)] new-line
42720 structured-block
42722 OpenMP 4.5:
42723 # pragma omp critical [(name) [hint(expression)]] new-line
42724 structured-block */
42726 #define OMP_CRITICAL_CLAUSE_MASK \
42727 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
42729 static tree
42730 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42732 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
42734 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42736 matching_parens parens;
42737 parens.consume_open (parser);
42739 name = cp_parser_identifier (parser);
42741 if (name == error_mark_node
42742 || !parens.require_close (parser))
42743 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42744 /*or_comma=*/false,
42745 /*consume_paren=*/true);
42746 if (name == error_mark_node)
42747 name = NULL;
42749 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42750 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42751 cp_lexer_consume_token (parser->lexer);
42754 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
42755 "#pragma omp critical", pragma_tok);
42757 stmt = cp_parser_omp_structured_block (parser, if_p);
42758 return c_finish_omp_critical (input_location, stmt, name, clauses);
42761 /* OpenMP 5.0:
42762 # pragma omp depobj ( depobj ) depobj-clause new-line
42764 depobj-clause:
42765 depend (dependence-type : locator)
42766 destroy
42767 update (dependence-type)
42769 dependence-type:
42772 inout
42773 mutexinout */
42775 static void
42776 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
42778 location_t loc = pragma_tok->location;
42779 matching_parens parens;
42780 if (!parens.require_open (parser))
42782 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42783 return;
42786 tree depobj = cp_parser_assignment_expression (parser);
42788 if (!parens.require_close (parser))
42789 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42790 /*or_comma=*/false,
42791 /*consume_paren=*/true);
42793 tree clause = NULL_TREE;
42794 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
42795 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42796 cp_lexer_consume_token (parser->lexer);
42797 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
42798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42800 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42801 const char *p = IDENTIFIER_POINTER (id);
42803 cp_lexer_consume_token (parser->lexer);
42804 if (!strcmp ("depend", p))
42806 /* Don't create location wrapper nodes within the depend clause. */
42807 auto_suppress_location_wrappers sentinel;
42808 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
42809 if (clause)
42810 clause = finish_omp_clauses (clause, C_ORT_OMP);
42811 if (!clause)
42812 clause = error_mark_node;
42814 else if (!strcmp ("destroy", p))
42815 kind = OMP_CLAUSE_DEPEND_LAST;
42816 else if (!strcmp ("update", p))
42818 matching_parens c_parens;
42819 if (c_parens.require_open (parser))
42821 location_t c2_loc
42822 = cp_lexer_peek_token (parser->lexer)->location;
42823 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42825 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
42826 const char *p2 = IDENTIFIER_POINTER (id2);
42828 cp_lexer_consume_token (parser->lexer);
42829 if (!strcmp ("in", p2))
42830 kind = OMP_CLAUSE_DEPEND_IN;
42831 else if (!strcmp ("out", p2))
42832 kind = OMP_CLAUSE_DEPEND_OUT;
42833 else if (!strcmp ("inout", p2))
42834 kind = OMP_CLAUSE_DEPEND_INOUT;
42835 else if (!strcmp ("mutexinoutset", p2))
42836 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
42837 else if (!strcmp ("inoutset", p2))
42838 kind = OMP_CLAUSE_DEPEND_INOUTSET;
42840 if (kind == OMP_CLAUSE_DEPEND_INVALID)
42842 clause = error_mark_node;
42843 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
42844 "%<mutexinoutset%> or %<inoutset%>");
42846 if (!c_parens.require_close (parser))
42847 cp_parser_skip_to_closing_parenthesis (parser,
42848 /*recovering=*/true,
42849 /*or_comma=*/false,
42850 /*consume_paren=*/true);
42852 else
42853 clause = error_mark_node;
42856 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
42858 clause = error_mark_node;
42859 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
42861 cp_parser_require_pragma_eol (parser, pragma_tok);
42863 finish_omp_depobj (loc, depobj, kind, clause);
42867 /* OpenMP 2.5:
42868 # pragma omp flush flush-vars[opt] new-line
42870 flush-vars:
42871 ( variable-list )
42873 OpenMP 5.0:
42874 # pragma omp flush memory-order-clause new-line */
42876 static void
42877 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
42879 enum memmodel mo = MEMMODEL_LAST;
42880 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42881 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42882 cp_lexer_consume_token (parser->lexer);
42883 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42885 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42886 const char *p = IDENTIFIER_POINTER (id);
42887 if (!strcmp (p, "seq_cst"))
42888 mo = MEMMODEL_SEQ_CST;
42889 else if (!strcmp (p, "acq_rel"))
42890 mo = MEMMODEL_ACQ_REL;
42891 else if (!strcmp (p, "release"))
42892 mo = MEMMODEL_RELEASE;
42893 else if (!strcmp (p, "acquire"))
42894 mo = MEMMODEL_ACQUIRE;
42895 else
42896 error_at (cp_lexer_peek_token (parser->lexer)->location,
42897 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
42898 "%<acquire%>");
42899 cp_lexer_consume_token (parser->lexer);
42901 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42903 if (mo != MEMMODEL_LAST)
42904 error_at (cp_lexer_peek_token (parser->lexer)->location,
42905 "%<flush%> list specified together with memory order "
42906 "clause");
42907 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
42909 cp_parser_require_pragma_eol (parser, pragma_tok);
42911 finish_omp_flush (mo);
42914 /* Helper function, to parse omp for increment expression. */
42916 static tree
42917 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
42919 tree cond = cp_parser_binary_expression (parser, false, true,
42920 PREC_NOT_OPERATOR, NULL);
42921 if (cond == error_mark_node
42922 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
42924 cp_parser_skip_to_end_of_statement (parser);
42925 return error_mark_node;
42928 switch (TREE_CODE (cond))
42930 case GT_EXPR:
42931 case GE_EXPR:
42932 case LT_EXPR:
42933 case LE_EXPR:
42934 break;
42935 case NE_EXPR:
42936 if (code != OACC_LOOP)
42937 break;
42938 gcc_fallthrough ();
42939 default:
42940 return error_mark_node;
42943 /* If decl is an iterator, preserve LHS and RHS of the relational
42944 expr until finish_omp_for. */
42945 if (decl
42946 && (type_dependent_expression_p (decl)
42947 || CLASS_TYPE_P (TREE_TYPE (decl))))
42948 return cond;
42950 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
42951 TREE_CODE (cond),
42952 TREE_OPERAND (cond, 0), ERROR_MARK,
42953 TREE_OPERAND (cond, 1), ERROR_MARK,
42954 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
42957 /* Helper function, to parse omp for increment expression. */
42959 static tree
42960 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
42962 cp_token *token = cp_lexer_peek_token (parser->lexer);
42963 enum tree_code op;
42964 tree lhs, rhs;
42965 cp_id_kind idk;
42966 bool decl_first;
42968 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
42970 op = (token->type == CPP_PLUS_PLUS
42971 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
42972 cp_lexer_consume_token (parser->lexer);
42973 lhs = cp_parser_simple_cast_expression (parser);
42974 if (lhs != decl
42975 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
42976 return error_mark_node;
42977 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
42980 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
42981 if (lhs != decl
42982 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
42983 return error_mark_node;
42985 token = cp_lexer_peek_token (parser->lexer);
42986 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
42988 op = (token->type == CPP_PLUS_PLUS
42989 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
42990 cp_lexer_consume_token (parser->lexer);
42991 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
42994 op = cp_parser_assignment_operator_opt (parser);
42995 if (op == ERROR_MARK)
42996 return error_mark_node;
42998 if (op != NOP_EXPR)
43000 rhs = cp_parser_assignment_expression (parser);
43001 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
43002 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43005 lhs = cp_parser_binary_expression (parser, false, false,
43006 PREC_ADDITIVE_EXPRESSION, NULL);
43007 token = cp_lexer_peek_token (parser->lexer);
43008 decl_first = (lhs == decl
43009 || (processing_template_decl && cp_tree_equal (lhs, decl)));
43010 if (decl_first)
43011 lhs = NULL_TREE;
43012 if (token->type != CPP_PLUS
43013 && token->type != CPP_MINUS)
43014 return error_mark_node;
43018 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
43019 cp_lexer_consume_token (parser->lexer);
43020 rhs = cp_parser_binary_expression (parser, false, false,
43021 PREC_ADDITIVE_EXPRESSION, NULL);
43022 token = cp_lexer_peek_token (parser->lexer);
43023 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
43025 if (lhs == NULL_TREE)
43027 if (op == PLUS_EXPR)
43028 lhs = rhs;
43029 else
43030 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
43031 NULL_TREE, tf_warning_or_error);
43033 else
43034 lhs = build_x_binary_op (input_location, op,
43035 lhs, ERROR_MARK,
43036 rhs, ERROR_MARK,
43037 NULL_TREE, NULL, tf_warning_or_error);
43040 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
43042 if (!decl_first)
43044 if ((rhs != decl
43045 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
43046 || op == MINUS_EXPR)
43047 return error_mark_node;
43048 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
43050 else
43051 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
43053 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43056 /* Parse the initialization statement of an OpenMP for loop.
43058 Return true if the resulting construct should have an
43059 OMP_CLAUSE_PRIVATE added to it. */
43061 static tree
43062 cp_parser_omp_for_loop_init (cp_parser *parser,
43063 tree &this_pre_body,
43064 releasing_vec &for_block,
43065 tree &init,
43066 tree &orig_init,
43067 tree &decl,
43068 tree &real_decl)
43070 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43071 return NULL_TREE;
43073 tree add_private_clause = NULL_TREE;
43075 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
43077 init-expr:
43078 var = lb
43079 integer-type var = lb
43080 random-access-iterator-type var = lb
43081 pointer-type var = lb
43083 cp_decl_specifier_seq type_specifiers;
43085 /* First, try to parse as an initialized declaration. See
43086 cp_parser_condition, from whence the bulk of this is copied. */
43088 cp_parser_parse_tentatively (parser);
43089 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
43090 /*is_declaration=*/true,
43091 /*is_trailing_return=*/false,
43092 &type_specifiers);
43093 if (cp_parser_parse_definitely (parser))
43095 /* If parsing a type specifier seq succeeded, then this
43096 MUST be a initialized declaration. */
43097 tree asm_specification, attributes;
43098 cp_declarator *declarator;
43100 declarator = cp_parser_declarator (parser,
43101 CP_PARSER_DECLARATOR_NAMED,
43102 CP_PARSER_FLAGS_NONE,
43103 /*ctor_dtor_or_conv_p=*/NULL,
43104 /*parenthesized_p=*/NULL,
43105 /*member_p=*/false,
43106 /*friend_p=*/false,
43107 /*static_p=*/false);
43108 attributes = cp_parser_attributes_opt (parser);
43109 asm_specification = cp_parser_asm_specification_opt (parser);
43111 if (declarator == cp_error_declarator)
43112 cp_parser_skip_to_end_of_statement (parser);
43114 else
43116 tree pushed_scope, auto_node;
43118 decl = start_decl (declarator, &type_specifiers,
43119 SD_INITIALIZED, attributes,
43120 /*prefix_attributes=*/NULL_TREE,
43121 &pushed_scope);
43123 auto_node = type_uses_auto (TREE_TYPE (decl));
43124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
43126 if (cp_lexer_next_token_is (parser->lexer,
43127 CPP_OPEN_PAREN))
43128 error ("parenthesized initialization is not allowed in "
43129 "OpenMP %<for%> loop");
43130 else
43131 /* Trigger an error. */
43132 cp_parser_require (parser, CPP_EQ, RT_EQ);
43134 init = error_mark_node;
43135 cp_parser_skip_to_end_of_statement (parser);
43137 else if (CLASS_TYPE_P (TREE_TYPE (decl))
43138 || type_dependent_expression_p (decl)
43139 || auto_node)
43141 bool is_direct_init, is_non_constant_init;
43143 init = cp_parser_initializer (parser,
43144 &is_direct_init,
43145 &is_non_constant_init);
43147 if (auto_node)
43149 TREE_TYPE (decl)
43150 = do_auto_deduction (TREE_TYPE (decl), init,
43151 auto_node);
43153 if (!CLASS_TYPE_P (TREE_TYPE (decl))
43154 && !type_dependent_expression_p (decl))
43155 goto non_class;
43158 cp_finish_decl (decl, init, !is_non_constant_init,
43159 asm_specification,
43160 LOOKUP_ONLYCONVERTING);
43161 orig_init = init;
43162 if (CLASS_TYPE_P (TREE_TYPE (decl)))
43164 vec_safe_push (for_block, this_pre_body);
43165 init = NULL_TREE;
43167 else
43169 init = pop_stmt_list (this_pre_body);
43170 if (init && TREE_CODE (init) == STATEMENT_LIST)
43172 tree_stmt_iterator i = tsi_start (init);
43173 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
43174 while (!tsi_end_p (i))
43176 tree t = tsi_stmt (i);
43177 if (TREE_CODE (t) == DECL_EXPR
43178 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
43180 tsi_delink (&i);
43181 vec_safe_push (for_block, t);
43182 continue;
43184 break;
43186 if (tsi_one_before_end_p (i))
43188 tree t = tsi_stmt (i);
43189 tsi_delink (&i);
43190 free_stmt_list (init);
43191 init = t;
43195 this_pre_body = NULL_TREE;
43197 else
43199 /* Consume '='. */
43200 cp_lexer_consume_token (parser->lexer);
43201 init = cp_parser_assignment_expression (parser);
43203 non_class:
43204 if (TYPE_REF_P (TREE_TYPE (decl)))
43205 init = error_mark_node;
43206 else
43207 cp_finish_decl (decl, NULL_TREE,
43208 /*init_const_expr_p=*/false,
43209 asm_specification,
43210 LOOKUP_ONLYCONVERTING);
43213 if (pushed_scope)
43214 pop_scope (pushed_scope);
43217 else
43219 cp_id_kind idk;
43220 /* If parsing a type specifier sequence failed, then
43221 this MUST be a simple expression. */
43222 cp_parser_parse_tentatively (parser);
43223 decl = cp_parser_primary_expression (parser, false, false,
43224 false, &idk);
43225 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
43226 if (!cp_parser_error_occurred (parser)
43227 && decl
43228 && (TREE_CODE (decl) == COMPONENT_REF
43229 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
43231 cp_parser_abort_tentative_parse (parser);
43232 cp_parser_parse_tentatively (parser);
43233 cp_token *token = cp_lexer_peek_token (parser->lexer);
43234 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
43235 /*check_dependency_p=*/true,
43236 /*template_p=*/NULL,
43237 /*declarator_p=*/false,
43238 /*optional_p=*/false);
43239 if (name != error_mark_node
43240 && last_tok == cp_lexer_peek_token (parser->lexer))
43242 decl = cp_parser_lookup_name_simple (parser, name,
43243 token->location);
43244 if (TREE_CODE (decl) == FIELD_DECL)
43245 add_private_clause = omp_privatize_field (decl, false);
43247 cp_parser_abort_tentative_parse (parser);
43248 cp_parser_parse_tentatively (parser);
43249 decl = cp_parser_primary_expression (parser, false, false,
43250 false, &idk);
43252 if (!cp_parser_error_occurred (parser)
43253 && decl
43254 && DECL_P (decl)
43255 && CLASS_TYPE_P (TREE_TYPE (decl)))
43257 tree rhs;
43259 cp_parser_parse_definitely (parser);
43260 cp_parser_require (parser, CPP_EQ, RT_EQ);
43261 rhs = cp_parser_assignment_expression (parser);
43262 orig_init = rhs;
43263 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
43264 decl, NOP_EXPR,
43265 rhs, NULL_TREE,
43266 tf_warning_or_error));
43267 if (!add_private_clause)
43268 add_private_clause = decl;
43270 else
43272 decl = NULL;
43273 cp_parser_abort_tentative_parse (parser);
43274 init = cp_parser_expression (parser);
43275 if (init)
43277 if (TREE_CODE (init) == MODIFY_EXPR
43278 || TREE_CODE (init) == MODOP_EXPR)
43279 real_decl = TREE_OPERAND (init, 0);
43283 return add_private_clause;
43286 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
43288 void
43289 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
43290 tree &decl, tree &orig_decl, tree &init,
43291 tree &orig_init, tree &cond, tree &incr)
43293 tree begin, end, range_temp_decl = NULL_TREE;
43294 tree iter_type, begin_expr, end_expr;
43295 bool clear_has_value_expr = false;
43297 if (processing_template_decl)
43299 if (check_for_bare_parameter_packs (init))
43300 init = error_mark_node;
43301 if (!type_dependent_expression_p (init)
43302 /* do_auto_deduction doesn't mess with template init-lists. */
43303 && !BRACE_ENCLOSED_INITIALIZER_P (init))
43305 tree d = decl;
43306 tree decomp_first_name = NULL_TREE;
43307 unsigned decomp_cnt = 0;
43308 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
43310 tree v = DECL_VALUE_EXPR (decl);
43311 if (TREE_CODE (v) == ARRAY_REF
43312 && VAR_P (TREE_OPERAND (v, 0))
43313 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
43315 d = TREE_OPERAND (v, 0);
43316 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
43317 decomp_first_name = decl;
43320 do_range_for_auto_deduction (d, init, decomp_first_name, decomp_cnt);
43322 cond = global_namespace;
43323 incr = NULL_TREE;
43324 orig_init = init;
43325 if (this_pre_body)
43326 this_pre_body = pop_stmt_list (this_pre_body);
43327 return;
43330 init = mark_lvalue_use (init);
43332 if (decl == error_mark_node || init == error_mark_node)
43333 /* If an error happened previously do nothing or else a lot of
43334 unhelpful errors would be issued. */
43335 begin_expr = end_expr = iter_type = error_mark_node;
43336 else
43338 tree range_temp;
43340 if (VAR_P (init)
43341 && array_of_runtime_bound_p (TREE_TYPE (init)))
43342 /* Can't bind a reference to an array of runtime bound. */
43343 range_temp = init;
43344 else
43346 range_temp = build_range_temp (init);
43347 DECL_NAME (range_temp) = NULL_TREE;
43348 pushdecl (range_temp);
43349 cp_finish_decl (range_temp, init,
43350 /*is_constant_init*/false, NULL_TREE,
43351 LOOKUP_ONLYCONVERTING);
43352 range_temp_decl = range_temp;
43353 range_temp = convert_from_reference (range_temp);
43355 iter_type = cp_parser_perform_range_for_lookup (range_temp,
43356 &begin_expr, &end_expr);
43359 tree end_iter_type = iter_type;
43360 if (cxx_dialect >= cxx17)
43361 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
43362 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
43363 TREE_USED (end) = 1;
43364 DECL_ARTIFICIAL (end) = 1;
43365 pushdecl (end);
43366 cp_finish_decl (end, end_expr,
43367 /*is_constant_init*/false, NULL_TREE,
43368 LOOKUP_ONLYCONVERTING);
43370 /* The new for initialization statement. */
43371 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
43372 TREE_USED (begin) = 1;
43373 DECL_ARTIFICIAL (begin) = 1;
43374 pushdecl (begin);
43375 orig_init = init;
43376 if (CLASS_TYPE_P (iter_type))
43377 init = NULL_TREE;
43378 else
43380 init = begin_expr;
43381 begin_expr = NULL_TREE;
43383 cp_finish_decl (begin, begin_expr,
43384 /*is_constant_init*/false, NULL_TREE,
43385 LOOKUP_ONLYCONVERTING);
43387 /* The new for condition. */
43388 if (CLASS_TYPE_P (iter_type))
43389 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
43390 else
43391 cond = build_x_binary_op (input_location, NE_EXPR,
43392 begin, ERROR_MARK,
43393 end, ERROR_MARK,
43394 NULL_TREE, NULL, tf_warning_or_error);
43396 /* The new increment expression. */
43397 if (CLASS_TYPE_P (iter_type))
43398 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
43399 else
43400 incr = finish_unary_op_expr (input_location,
43401 PREINCREMENT_EXPR, begin,
43402 tf_warning_or_error);
43404 orig_decl = decl;
43405 decl = begin;
43406 if (for_block)
43408 vec_safe_push (for_block, this_pre_body);
43409 this_pre_body = NULL_TREE;
43412 tree decomp_first_name = NULL_TREE;
43413 unsigned decomp_cnt = 0;
43414 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
43416 tree v = DECL_VALUE_EXPR (orig_decl);
43417 if (TREE_CODE (v) == ARRAY_REF
43418 && VAR_P (TREE_OPERAND (v, 0))
43419 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
43421 tree d = orig_decl;
43422 orig_decl = TREE_OPERAND (v, 0);
43423 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
43424 decomp_first_name = d;
43428 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
43429 if (auto_node)
43431 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
43432 NULL_TREE, tf_none);
43433 if (!error_operand_p (t))
43435 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
43436 t, auto_node);
43437 if (decomp_first_name)
43439 ++processing_template_decl;
43440 cp_finish_decomp (orig_decl, decomp_first_name, decomp_cnt);
43441 --processing_template_decl;
43442 if (!processing_template_decl)
43443 clear_has_value_expr = true;
43448 tree v = make_tree_vec (decomp_cnt + 3);
43449 TREE_VEC_ELT (v, 0) = range_temp_decl;
43450 TREE_VEC_ELT (v, 1) = end;
43451 TREE_VEC_ELT (v, 2) = orig_decl;
43452 if (clear_has_value_expr)
43453 TREE_PUBLIC (v) = 1;
43454 for (unsigned i = 0; i < decomp_cnt; i++)
43456 if (clear_has_value_expr)
43458 /* If cp_finish_decomp was called with processing_template_decl
43459 temporarily set to 1, then decomp names will have deduced
43460 name but the DECL_VALUE_EXPR will be dependent. Hide those
43461 from folding of other loop initializers e.g. for warning
43462 purposes until cp_finish_omp_range_for. */
43463 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (decomp_first_name)
43464 || (TREE_TYPE (decomp_first_name)
43465 == error_mark_node));
43466 DECL_HAS_VALUE_EXPR_P (decomp_first_name) = 0;
43468 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
43469 decomp_first_name = DECL_CHAIN (decomp_first_name);
43471 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
43474 /* Helper for cp_parser_omp_for_loop, finalize part of range for
43475 inside of the collapsed body. */
43477 void
43478 cp_finish_omp_range_for (tree orig, tree begin)
43480 gcc_assert (TREE_CODE (orig) == TREE_LIST
43481 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
43482 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
43483 tree decomp_first_name = NULL_TREE;
43484 unsigned int decomp_cnt = 0;
43486 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
43488 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
43489 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
43490 if (TREE_PUBLIC (TREE_CHAIN (orig)))
43492 /* Undo temporary clearing of DECL_HAS_VALUE_EXPR_P done
43493 by cp_convert_omp_range_for above. */
43494 TREE_PUBLIC (TREE_CHAIN (orig)) = 0;
43495 tree d = decomp_first_name;
43496 for (unsigned i = 0; i < decomp_cnt; i++)
43498 if (TREE_TYPE (d) != error_mark_node)
43499 DECL_HAS_VALUE_EXPR_P (d) = 1;
43500 d = DECL_CHAIN (d);
43503 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
43506 /* The declaration is initialized with *__begin inside the loop body. */
43507 cp_finish_decl (decl,
43508 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
43509 NULL_TREE, tf_warning_or_error),
43510 /*is_constant_init*/false, NULL_TREE,
43511 LOOKUP_ONLYCONVERTING);
43512 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
43513 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
43516 /* Return true if next tokens contain a standard attribute that contains
43517 omp::directive (DIRECTIVE). */
43519 static bool
43520 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
43521 bool tentative)
43523 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
43524 if (n < 10)
43525 return false;
43526 for (i = 5; i < n - 4; i++)
43527 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
43528 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
43529 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
43531 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
43532 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
43533 if (strcmp (IDENTIFIER_POINTER (first), "directive"))
43534 continue;
43535 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
43536 break;
43538 if (i == n - 4)
43539 return false;
43540 cp_parser_parse_tentatively (parser);
43541 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
43542 location_t last_loc
43543 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
43544 location_t middle_loc = UNKNOWN_LOCATION;
43545 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
43546 int cnt = 0;
43547 bool seen = false;
43548 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
43549 if (get_attribute_namespace (attr) == omp_identifier
43550 && is_attribute_p ("directive", get_attribute_name (attr)))
43552 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
43554 tree d = TREE_VALUE (a);
43555 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
43556 cp_token *first = DEFPARSE_TOKENS (d)->first;
43557 cnt++;
43558 if (first->type == CPP_NAME
43559 && strcmp (IDENTIFIER_POINTER (first->u.value),
43560 directive) == 0)
43562 seen = true;
43563 if (middle_loc == UNKNOWN_LOCATION)
43564 middle_loc = first->location;
43568 if (!seen || tentative)
43570 cp_parser_abort_tentative_parse (parser);
43571 return seen;
43573 if (cnt != 1 || TREE_CHAIN (std_attrs))
43575 error_at (make_location (first_loc, last_loc, middle_loc),
43576 "%<[[omp::directive(%s)]]%> must be the only specified "
43577 "attribute on a statement", directive);
43578 cp_parser_abort_tentative_parse (parser);
43579 return false;
43581 if (!cp_parser_parse_definitely (parser))
43582 return false;
43583 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
43584 return true;
43587 /* Parse an OpenMP structured block sequence. KIND is the corresponding
43588 separating directive. */
43590 static tree
43591 cp_parser_omp_structured_block_sequence (cp_parser *parser,
43592 enum pragma_kind kind)
43594 tree stmt = begin_omp_structured_block ();
43595 unsigned int save = cp_parser_begin_omp_structured_block (parser);
43597 cp_parser_statement (parser, NULL_TREE, false, NULL);
43598 while (true)
43600 cp_token *token = cp_lexer_peek_token (parser->lexer);
43602 if (token->type == CPP_CLOSE_BRACE
43603 || token->type == CPP_EOF
43604 || token->type == CPP_PRAGMA_EOL
43605 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
43606 || (kind != PRAGMA_NONE
43607 && cp_parser_pragma_kind (token) == kind))
43608 break;
43610 if (kind != PRAGMA_NONE
43611 && cp_parser_omp_section_scan (parser,
43612 kind == PRAGMA_OMP_SCAN
43613 ? "scan" : "section", false))
43614 break;
43616 cp_parser_statement (parser, NULL_TREE, false, NULL);
43619 cp_parser_end_omp_structured_block (parser, save);
43620 return finish_omp_structured_block (stmt);
43624 /* OpenMP 5.0:
43626 scan-loop-body:
43627 { structured-block scan-directive structured-block } */
43629 static void
43630 cp_parser_omp_scan_loop_body (cp_parser *parser)
43632 tree substmt, clauses = NULL_TREE;
43633 bool found_scan = false;
43635 matching_braces braces;
43636 if (!braces.require_open (parser))
43637 return;
43639 cp_token *tok = cp_lexer_peek_token (parser->lexer);
43640 if (cp_parser_pragma_kind (tok) != PRAGMA_OMP_SCAN)
43641 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
43642 else
43644 warning_at (tok->location, 0, "%<#pragma omp scan%> with zero preceding "
43645 "executable statements");
43646 substmt = build_empty_stmt (tok->location);
43648 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
43649 add_stmt (substmt);
43651 tok = cp_lexer_peek_token (parser->lexer);
43652 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
43654 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
43655 found_scan = true;
43657 cp_lexer_consume_token (parser->lexer);
43659 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43660 cp_lexer_consume_token (parser->lexer);
43662 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43664 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43665 const char *p = IDENTIFIER_POINTER (id);
43666 if (strcmp (p, "inclusive") == 0)
43667 clause = OMP_CLAUSE_INCLUSIVE;
43668 else if (strcmp (p, "exclusive") == 0)
43669 clause = OMP_CLAUSE_EXCLUSIVE;
43671 if (clause != OMP_CLAUSE_ERROR)
43673 cp_lexer_consume_token (parser->lexer);
43674 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
43676 else
43677 cp_parser_error (parser, "expected %<inclusive%> or "
43678 "%<exclusive%> clause");
43680 cp_parser_require_pragma_eol (parser, tok);
43682 else
43683 error ("expected %<#pragma omp scan%>");
43685 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43686 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43687 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
43688 else
43690 if (found_scan)
43691 warning_at (tok->location, 0, "%<#pragma omp scan%> with zero "
43692 "succeeding executable statements");
43693 substmt = build_empty_stmt (tok->location);
43695 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
43696 clauses);
43697 add_stmt (substmt);
43699 braces.require_close (parser);
43702 /* Parse the restricted form of the for statement allowed by OpenMP. */
43704 static tree
43705 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
43706 tree *cclauses, bool *if_p)
43708 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
43709 tree orig_decl;
43710 tree real_decl, initv, condv, incrv, declv, orig_declv;
43711 tree this_pre_body, cl, ordered_cl = NULL_TREE;
43712 location_t loc_first;
43713 bool collapse_err = false;
43714 int i, collapse = 1, ordered = 0, count, nbraces = 0;
43715 releasing_vec for_block;
43716 auto_vec<tree, 4> orig_inits;
43717 bool tiling = false;
43718 bool inscan = false;
43720 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
43721 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
43722 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
43723 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
43725 tiling = true;
43726 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
43728 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
43729 && OMP_CLAUSE_ORDERED_EXPR (cl))
43731 ordered_cl = cl;
43732 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
43734 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
43735 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
43736 && (code == OMP_SIMD || code == OMP_FOR))
43737 inscan = true;
43739 if (ordered && ordered < collapse)
43741 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
43742 "%<ordered%> clause parameter is less than %<collapse%>");
43743 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
43744 = build_int_cst (NULL_TREE, collapse);
43745 ordered = collapse;
43748 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
43749 count = ordered ? ordered : collapse;
43751 declv = make_tree_vec (count);
43752 initv = make_tree_vec (count);
43753 condv = make_tree_vec (count);
43754 incrv = make_tree_vec (count);
43755 orig_declv = NULL_TREE;
43757 loc_first = cp_lexer_peek_token (parser->lexer)->location;
43759 for (i = 0; i < count; i++)
43761 int bracecount = 0;
43762 tree add_private_clause = NULL_TREE;
43763 location_t loc;
43765 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43767 if (!collapse_err)
43768 cp_parser_error (parser, "for statement expected");
43769 return NULL;
43771 loc = cp_lexer_consume_token (parser->lexer)->location;
43773 /* Don't create location wrapper nodes within an OpenMP "for"
43774 statement. */
43775 auto_suppress_location_wrappers sentinel;
43777 matching_parens parens;
43778 if (!parens.require_open (parser))
43779 return NULL;
43781 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
43782 this_pre_body = push_stmt_list ();
43784 if (code != OACC_LOOP && cxx_dialect >= cxx11)
43786 /* Save tokens so that we can put them back. */
43787 cp_lexer_save_tokens (parser->lexer);
43789 /* Look for ':' that is not nested in () or {}. */
43790 bool is_range_for
43791 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
43792 /*recovering=*/false,
43793 CPP_COLON,
43794 /*consume_paren=*/
43795 false) == -1);
43797 /* Roll back the tokens we skipped. */
43798 cp_lexer_rollback_tokens (parser->lexer);
43800 if (is_range_for)
43802 bool saved_colon_corrects_to_scope_p
43803 = parser->colon_corrects_to_scope_p;
43805 /* A colon is used in range-based for. */
43806 parser->colon_corrects_to_scope_p = false;
43808 /* Parse the declaration. */
43809 cp_parser_simple_declaration (parser,
43810 /*function_definition_allowed_p=*/
43811 false, &decl);
43812 parser->colon_corrects_to_scope_p
43813 = saved_colon_corrects_to_scope_p;
43815 cp_parser_require (parser, CPP_COLON, RT_COLON);
43817 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
43818 false, 0, true);
43820 cp_convert_omp_range_for (this_pre_body, for_block, decl,
43821 orig_decl, init, orig_init,
43822 cond, incr);
43823 if (this_pre_body)
43825 if (pre_body)
43827 tree t = pre_body;
43828 pre_body = push_stmt_list ();
43829 add_stmt (t);
43830 add_stmt (this_pre_body);
43831 pre_body = pop_stmt_list (pre_body);
43833 else
43834 pre_body = this_pre_body;
43837 if (ordered_cl)
43838 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
43839 "%<ordered%> clause with parameter on "
43840 "range-based %<for%> loop");
43842 goto parse_close_paren;
43846 add_private_clause
43847 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
43848 init, orig_init, decl, real_decl);
43850 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43851 if (this_pre_body)
43853 this_pre_body = pop_stmt_list (this_pre_body);
43854 if (pre_body)
43856 tree t = pre_body;
43857 pre_body = push_stmt_list ();
43858 add_stmt (t);
43859 add_stmt (this_pre_body);
43860 pre_body = pop_stmt_list (pre_body);
43862 else
43863 pre_body = this_pre_body;
43866 if (decl)
43867 real_decl = decl;
43868 if (cclauses != NULL
43869 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
43870 && real_decl != NULL_TREE
43871 && code != OMP_LOOP)
43873 tree *c;
43874 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
43875 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
43876 && OMP_CLAUSE_DECL (*c) == real_decl)
43878 error_at (loc, "iteration variable %qD"
43879 " should not be firstprivate", real_decl);
43880 *c = OMP_CLAUSE_CHAIN (*c);
43882 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
43883 && OMP_CLAUSE_DECL (*c) == real_decl)
43885 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
43886 tree l = *c;
43887 *c = OMP_CLAUSE_CHAIN (*c);
43888 if (code == OMP_SIMD)
43890 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43891 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
43893 else
43895 OMP_CLAUSE_CHAIN (l) = clauses;
43896 clauses = l;
43898 add_private_clause = NULL_TREE;
43900 else
43902 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
43903 && OMP_CLAUSE_DECL (*c) == real_decl)
43904 add_private_clause = NULL_TREE;
43905 c = &OMP_CLAUSE_CHAIN (*c);
43909 if (add_private_clause)
43911 tree c;
43912 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
43914 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
43915 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
43916 && OMP_CLAUSE_DECL (c) == decl)
43917 break;
43918 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
43919 && OMP_CLAUSE_DECL (c) == decl)
43920 error_at (loc, "iteration variable %qD "
43921 "should not be firstprivate",
43922 decl);
43923 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
43924 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
43925 && OMP_CLAUSE_DECL (c) == decl)
43926 error_at (loc, "iteration variable %qD should not be reduction",
43927 decl);
43929 if (c == NULL)
43931 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
43932 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
43933 else if (code != OMP_SIMD)
43934 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
43935 else
43936 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
43937 OMP_CLAUSE_DECL (c) = add_private_clause;
43938 c = finish_omp_clauses (c, C_ORT_OMP);
43939 if (c)
43941 OMP_CLAUSE_CHAIN (c) = clauses;
43942 clauses = c;
43943 /* For linear, signal that we need to fill up
43944 the so far unknown linear step. */
43945 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
43946 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
43951 cond = NULL;
43952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
43953 cond = cp_parser_omp_for_cond (parser, decl, code);
43954 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43956 incr = NULL;
43957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
43959 /* If decl is an iterator, preserve the operator on decl
43960 until finish_omp_for. */
43961 if (real_decl
43962 && ((processing_template_decl
43963 && (TREE_TYPE (real_decl) == NULL_TREE
43964 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
43965 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
43966 incr = cp_parser_omp_for_incr (parser, real_decl);
43967 else
43968 incr = cp_parser_expression (parser);
43969 protected_set_expr_location_if_unset (incr, input_location);
43972 parse_close_paren:
43973 if (!parens.require_close (parser))
43974 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43975 /*or_comma=*/false,
43976 /*consume_paren=*/true);
43978 TREE_VEC_ELT (declv, i) = decl;
43979 TREE_VEC_ELT (initv, i) = init;
43980 TREE_VEC_ELT (condv, i) = cond;
43981 TREE_VEC_ELT (incrv, i) = incr;
43982 if (orig_init)
43984 orig_inits.safe_grow_cleared (i + 1, true);
43985 orig_inits[i] = orig_init;
43987 if (orig_decl)
43989 if (!orig_declv)
43990 orig_declv = copy_node (declv);
43991 TREE_VEC_ELT (orig_declv, i) = orig_decl;
43993 else if (orig_declv)
43994 TREE_VEC_ELT (orig_declv, i) = decl;
43996 if (i == count - 1)
43997 break;
43999 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
44000 in between the collapsed for loops to be still considered perfectly
44001 nested. Hopefully the final version clarifies this.
44002 For now handle (multiple) {'s and empty statements. */
44003 cp_parser_parse_tentatively (parser);
44004 for (;;)
44006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44007 break;
44008 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
44010 cp_lexer_consume_token (parser->lexer);
44011 bracecount++;
44013 else if (bracecount
44014 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
44015 cp_lexer_consume_token (parser->lexer);
44016 else
44018 loc = cp_lexer_peek_token (parser->lexer)->location;
44019 error_at (loc, "not enough for loops to collapse");
44020 collapse_err = true;
44021 cp_parser_abort_tentative_parse (parser);
44022 declv = NULL_TREE;
44023 break;
44027 if (declv)
44029 cp_parser_parse_definitely (parser);
44030 nbraces += bracecount;
44034 if (nbraces)
44035 if_p = NULL;
44037 /* Note that we saved the original contents of this flag when we entered
44038 the structured block, and so we don't need to re-save it here. */
44039 parser->in_statement = IN_OMP_FOR;
44041 /* Note that the grammar doesn't call for a structured block here,
44042 though the loop as a whole is a structured block. */
44043 if (orig_declv)
44045 body = begin_omp_structured_block ();
44046 for (i = 0; i < count; i++)
44047 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
44048 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
44049 TREE_VEC_ELT (declv, i));
44051 else
44052 body = push_stmt_list ();
44053 if (inscan)
44054 cp_parser_omp_scan_loop_body (parser);
44055 else
44056 cp_parser_statement (parser, NULL_TREE, false, if_p);
44057 if (orig_declv)
44058 body = finish_omp_structured_block (body);
44059 else
44060 body = pop_stmt_list (body);
44062 if (declv == NULL_TREE)
44063 ret = NULL_TREE;
44064 else
44065 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
44066 incrv, body, pre_body, &orig_inits, clauses);
44068 while (nbraces)
44070 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
44072 cp_lexer_consume_token (parser->lexer);
44073 nbraces--;
44075 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
44076 cp_lexer_consume_token (parser->lexer);
44077 else
44079 if (!collapse_err)
44081 error_at (cp_lexer_peek_token (parser->lexer)->location,
44082 "collapsed loops not perfectly nested");
44084 collapse_err = true;
44085 cp_parser_statement_seq_opt (parser, NULL);
44086 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
44087 break;
44091 while (!for_block->is_empty ())
44093 tree t = for_block->pop ();
44094 if (TREE_CODE (t) == STATEMENT_LIST)
44095 add_stmt (pop_stmt_list (t));
44096 else
44097 add_stmt (t);
44100 return ret;
44103 /* Helper function for OpenMP parsing, split clauses and call
44104 finish_omp_clauses on each of the set of clauses afterwards. */
44106 static void
44107 cp_omp_split_clauses (location_t loc, enum tree_code code,
44108 omp_clause_mask mask, tree clauses, tree *cclauses)
44110 int i;
44111 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
44112 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
44113 if (cclauses[i])
44114 cclauses[i] = finish_omp_clauses (cclauses[i],
44115 i == C_OMP_CLAUSE_SPLIT_TARGET
44116 ? C_ORT_OMP_TARGET : C_ORT_OMP);
44119 /* OpenMP 5.0:
44120 #pragma omp loop loop-clause[optseq] new-line
44121 for-loop */
44123 #define OMP_LOOP_CLAUSE_MASK \
44124 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
44129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44131 static tree
44132 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
44133 char *p_name, omp_clause_mask mask, tree *cclauses,
44134 bool *if_p)
44136 tree clauses, sb, ret;
44137 unsigned int save;
44138 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44140 strcat (p_name, " loop");
44141 mask |= OMP_LOOP_CLAUSE_MASK;
44143 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44144 cclauses == NULL);
44145 if (cclauses)
44147 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
44148 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
44151 keep_next_level (true);
44152 sb = begin_omp_structured_block ();
44153 save = cp_parser_begin_omp_structured_block (parser);
44155 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
44157 cp_parser_end_omp_structured_block (parser, save);
44158 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44160 return ret;
44163 /* OpenMP 4.0:
44164 #pragma omp simd simd-clause[optseq] new-line
44165 for-loop */
44167 #define OMP_SIMD_CLAUSE_MASK \
44168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
44169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
44170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
44172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
44178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44180 static tree
44181 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
44182 char *p_name, omp_clause_mask mask, tree *cclauses,
44183 bool *if_p)
44185 tree clauses, sb, ret;
44186 unsigned int save;
44187 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44189 strcat (p_name, " simd");
44190 mask |= OMP_SIMD_CLAUSE_MASK;
44192 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44193 cclauses == NULL);
44194 if (cclauses)
44196 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
44197 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
44200 keep_next_level (true);
44201 sb = begin_omp_structured_block ();
44202 save = cp_parser_begin_omp_structured_block (parser);
44204 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
44206 cp_parser_end_omp_structured_block (parser, save);
44207 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44209 return ret;
44212 /* OpenMP 2.5:
44213 #pragma omp for for-clause[optseq] new-line
44214 for-loop
44216 OpenMP 4.0:
44217 #pragma omp for simd for-simd-clause[optseq] new-line
44218 for-loop */
44220 #define OMP_FOR_CLAUSE_MASK \
44221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
44227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
44228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
44229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44233 static tree
44234 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
44235 char *p_name, omp_clause_mask mask, tree *cclauses,
44236 bool *if_p)
44238 tree clauses, sb, ret;
44239 unsigned int save;
44240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44242 strcat (p_name, " for");
44243 mask |= OMP_FOR_CLAUSE_MASK;
44244 /* parallel for{, simd} disallows nowait clause, but for
44245 target {teams distribute ,}parallel for{, simd} it should be accepted. */
44246 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
44247 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
44248 /* Composite distribute parallel for{, simd} disallows ordered clause. */
44249 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44250 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
44252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44254 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44255 const char *p = IDENTIFIER_POINTER (id);
44257 if (strcmp (p, "simd") == 0)
44259 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44260 if (cclauses == NULL)
44261 cclauses = cclauses_buf;
44263 cp_lexer_consume_token (parser->lexer);
44264 if (!flag_openmp) /* flag_openmp_simd */
44265 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44266 cclauses, if_p);
44267 sb = begin_omp_structured_block ();
44268 save = cp_parser_begin_omp_structured_block (parser);
44269 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44270 cclauses, if_p);
44271 cp_parser_end_omp_structured_block (parser, save);
44272 tree body = finish_omp_structured_block (sb);
44273 if (ret == NULL)
44274 return ret;
44275 ret = make_node (OMP_FOR);
44276 TREE_TYPE (ret) = void_type_node;
44277 OMP_FOR_BODY (ret) = body;
44278 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44279 SET_EXPR_LOCATION (ret, loc);
44280 add_stmt (ret);
44281 return ret;
44284 if (!flag_openmp) /* flag_openmp_simd */
44286 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44287 return NULL_TREE;
44290 /* Composite distribute parallel for disallows linear clause. */
44291 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44292 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
44294 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44295 cclauses == NULL);
44296 if (cclauses)
44298 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
44299 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44302 keep_next_level (true);
44303 sb = begin_omp_structured_block ();
44304 save = cp_parser_begin_omp_structured_block (parser);
44306 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
44308 cp_parser_end_omp_structured_block (parser, save);
44309 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44311 return ret;
44314 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
44315 omp_clause_mask, tree *, bool *);
44317 /* OpenMP 2.5:
44318 # pragma omp master new-line
44319 structured-block */
44321 static tree
44322 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
44323 char *p_name, omp_clause_mask mask, tree *cclauses,
44324 bool *if_p)
44326 tree clauses, sb, ret;
44327 unsigned int save;
44328 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44330 strcat (p_name, " master");
44332 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44334 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44335 const char *p = IDENTIFIER_POINTER (id);
44337 if (strcmp (p, "taskloop") == 0)
44339 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44340 if (cclauses == NULL)
44341 cclauses = cclauses_buf;
44343 cp_lexer_consume_token (parser->lexer);
44344 if (!flag_openmp) /* flag_openmp_simd */
44345 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44346 cclauses, if_p);
44347 sb = begin_omp_structured_block ();
44348 save = cp_parser_begin_omp_structured_block (parser);
44349 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44350 cclauses, if_p);
44351 cp_parser_end_omp_structured_block (parser, save);
44352 tree body = finish_omp_structured_block (sb);
44353 if (ret == NULL)
44354 return ret;
44355 ret = c_finish_omp_master (loc, body);
44356 OMP_MASTER_COMBINED (ret) = 1;
44357 return ret;
44360 if (!flag_openmp) /* flag_openmp_simd */
44362 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44363 return NULL_TREE;
44366 if (cclauses)
44368 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44369 false);
44370 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
44372 else
44373 cp_parser_require_pragma_eol (parser, pragma_tok);
44375 return c_finish_omp_master (loc,
44376 cp_parser_omp_structured_block (parser, if_p));
44379 /* OpenMP 5.1:
44380 # pragma omp masked masked-clauses new-line
44381 structured-block */
44383 #define OMP_MASKED_CLAUSE_MASK \
44384 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
44386 static tree
44387 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
44388 char *p_name, omp_clause_mask mask, tree *cclauses,
44389 bool *if_p)
44391 tree clauses, sb, ret;
44392 unsigned int save;
44393 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44395 strcat (p_name, " masked");
44396 mask |= OMP_MASKED_CLAUSE_MASK;
44398 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44400 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44401 const char *p = IDENTIFIER_POINTER (id);
44403 if (strcmp (p, "taskloop") == 0)
44405 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44406 if (cclauses == NULL)
44407 cclauses = cclauses_buf;
44409 cp_lexer_consume_token (parser->lexer);
44410 if (!flag_openmp) /* flag_openmp_simd */
44411 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44412 cclauses, if_p);
44413 sb = begin_omp_structured_block ();
44414 save = cp_parser_begin_omp_structured_block (parser);
44415 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44416 cclauses, if_p);
44417 cp_parser_end_omp_structured_block (parser, save);
44418 tree body = finish_omp_structured_block (sb);
44419 if (ret == NULL)
44420 return ret;
44421 ret = c_finish_omp_masked (loc, body,
44422 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
44423 OMP_MASKED_COMBINED (ret) = 1;
44424 return ret;
44427 if (!flag_openmp) /* flag_openmp_simd */
44429 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44430 return NULL_TREE;
44433 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44434 cclauses == NULL);
44435 if (cclauses)
44437 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
44438 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
44441 return c_finish_omp_masked (loc,
44442 cp_parser_omp_structured_block (parser, if_p),
44443 clauses);
44446 /* OpenMP 2.5:
44447 # pragma omp ordered new-line
44448 structured-block
44450 OpenMP 4.5:
44451 # pragma omp ordered ordered-clauses new-line
44452 structured-block */
44454 #define OMP_ORDERED_CLAUSE_MASK \
44455 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
44456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
44458 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
44459 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
44462 static bool
44463 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
44464 enum pragma_context context, bool *if_p)
44466 location_t loc = pragma_tok->location;
44467 int n = 1;
44469 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
44470 n = 2;
44472 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
44474 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
44475 const char *p = IDENTIFIER_POINTER (id);
44477 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
44479 if (!flag_openmp) /* flag_openmp_simd */
44481 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44482 return false;
44484 if (context == pragma_stmt)
44486 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
44487 "%qs clause may only be used in compound "
44488 "statements", p);
44489 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44490 return true;
44492 tree clauses
44493 = cp_parser_omp_all_clauses (parser,
44494 OMP_ORDERED_DEPEND_CLAUSE_MASK,
44495 "#pragma omp ordered", pragma_tok);
44496 c_finish_omp_ordered (loc, clauses, NULL_TREE);
44497 return false;
44501 tree clauses
44502 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
44503 "#pragma omp ordered", pragma_tok);
44505 if (!flag_openmp /* flag_openmp_simd */
44506 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
44507 return false;
44509 c_finish_omp_ordered (loc, clauses,
44510 cp_parser_omp_structured_block (parser, if_p));
44511 return true;
44514 /* OpenMP 2.5:
44516 section-scope:
44517 { section-sequence }
44519 section-sequence:
44520 section-directive[opt] structured-block
44521 section-sequence section-directive structured-block */
44523 static tree
44524 cp_parser_omp_sections_scope (cp_parser *parser)
44526 tree stmt, substmt;
44527 bool error_suppress = false;
44528 cp_token *tok;
44530 matching_braces braces;
44531 if (!braces.require_open (parser))
44532 return NULL_TREE;
44534 stmt = push_stmt_list ();
44536 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
44537 != PRAGMA_OMP_SECTION
44538 && !cp_parser_omp_section_scan (parser, "section", true))
44540 substmt = cp_parser_omp_structured_block_sequence (parser,
44541 PRAGMA_OMP_SECTION);
44542 substmt = build1 (OMP_SECTION, void_type_node, substmt);
44543 add_stmt (substmt);
44546 while (1)
44548 tok = cp_lexer_peek_token (parser->lexer);
44549 if (tok->type == CPP_CLOSE_BRACE)
44550 break;
44551 if (tok->type == CPP_EOF)
44552 break;
44554 if (cp_parser_omp_section_scan (parser, "section", false))
44555 tok = cp_lexer_peek_token (parser->lexer);
44556 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
44558 cp_lexer_consume_token (parser->lexer);
44559 cp_parser_require_pragma_eol (parser, tok);
44560 error_suppress = false;
44562 else if (!error_suppress)
44564 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
44565 error_suppress = true;
44568 substmt = cp_parser_omp_structured_block_sequence (parser,
44569 PRAGMA_OMP_SECTION);
44570 substmt = build1 (OMP_SECTION, void_type_node, substmt);
44571 add_stmt (substmt);
44573 braces.require_close (parser);
44575 substmt = pop_stmt_list (stmt);
44577 stmt = make_node (OMP_SECTIONS);
44578 TREE_TYPE (stmt) = void_type_node;
44579 OMP_SECTIONS_BODY (stmt) = substmt;
44581 add_stmt (stmt);
44582 return stmt;
44585 /* OpenMP 2.5:
44586 # pragma omp sections sections-clause[optseq] newline
44587 sections-scope */
44589 #define OMP_SECTIONS_CLAUSE_MASK \
44590 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44597 static tree
44598 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
44599 char *p_name, omp_clause_mask mask, tree *cclauses)
44601 tree clauses, ret;
44602 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44604 strcat (p_name, " sections");
44605 mask |= OMP_SECTIONS_CLAUSE_MASK;
44606 if (cclauses)
44607 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
44609 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44610 cclauses == NULL);
44611 if (cclauses)
44613 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
44614 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
44617 ret = cp_parser_omp_sections_scope (parser);
44618 if (ret)
44619 OMP_SECTIONS_CLAUSES (ret) = clauses;
44621 return ret;
44624 /* OpenMP 2.5:
44625 # pragma omp parallel parallel-clause[optseq] new-line
44626 structured-block
44627 # pragma omp parallel for parallel-for-clause[optseq] new-line
44628 structured-block
44629 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
44630 structured-block
44632 OpenMP 4.0:
44633 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
44634 structured-block */
44636 #define OMP_PARALLEL_CLAUSE_MASK \
44637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
44641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
44643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
44645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
44648 static tree
44649 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
44650 char *p_name, omp_clause_mask mask, tree *cclauses,
44651 bool *if_p)
44653 tree stmt, clauses, block;
44654 unsigned int save;
44655 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44657 strcat (p_name, " parallel");
44658 mask |= OMP_PARALLEL_CLAUSE_MASK;
44659 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
44660 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
44661 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
44662 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
44664 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44666 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44667 if (cclauses == NULL)
44668 cclauses = cclauses_buf;
44670 cp_lexer_consume_token (parser->lexer);
44671 if (!flag_openmp) /* flag_openmp_simd */
44672 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
44673 if_p);
44674 block = begin_omp_parallel ();
44675 save = cp_parser_begin_omp_structured_block (parser);
44676 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
44677 if_p);
44678 cp_parser_end_omp_structured_block (parser, save);
44679 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44680 block);
44681 if (ret == NULL_TREE)
44682 return ret;
44683 OMP_PARALLEL_COMBINED (stmt) = 1;
44684 return stmt;
44686 /* When combined with distribute, parallel has to be followed by for.
44687 #pragma omp target parallel is allowed though. */
44688 else if (cclauses
44689 && (mask & (OMP_CLAUSE_MASK_1
44690 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44692 error_at (loc, "expected %<for%> after %qs", p_name);
44693 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44694 return NULL_TREE;
44696 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44698 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44699 const char *p = IDENTIFIER_POINTER (id);
44700 if (cclauses == NULL && strcmp (p, "masked") == 0)
44702 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44703 cclauses = cclauses_buf;
44705 cp_lexer_consume_token (parser->lexer);
44706 if (!flag_openmp) /* flag_openmp_simd */
44707 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
44708 cclauses, if_p);
44709 block = begin_omp_parallel ();
44710 save = cp_parser_begin_omp_structured_block (parser);
44711 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
44712 cclauses, if_p);
44713 cp_parser_end_omp_structured_block (parser, save);
44714 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44715 block);
44716 if (ret == NULL_TREE)
44717 return ret;
44718 /* masked does have just filter clause, but during gimplification
44719 isn't represented by a gimplification omp context, so for
44720 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
44721 so that
44722 #pragma omp parallel masked
44723 #pragma omp taskloop simd lastprivate (x)
44724 isn't confused with
44725 #pragma omp parallel masked taskloop simd lastprivate (x) */
44726 if (OMP_MASKED_COMBINED (ret))
44727 OMP_PARALLEL_COMBINED (stmt) = 1;
44728 return stmt;
44730 else if (cclauses == NULL && strcmp (p, "master") == 0)
44732 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44733 cclauses = cclauses_buf;
44735 cp_lexer_consume_token (parser->lexer);
44736 if (!flag_openmp) /* flag_openmp_simd */
44737 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
44738 cclauses, if_p);
44739 block = begin_omp_parallel ();
44740 save = cp_parser_begin_omp_structured_block (parser);
44741 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
44742 cclauses, if_p);
44743 cp_parser_end_omp_structured_block (parser, save);
44744 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44745 block);
44746 if (ret == NULL_TREE)
44747 return ret;
44748 /* master doesn't have any clauses and during gimplification
44749 isn't represented by a gimplification omp context, so for
44750 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
44751 so that
44752 #pragma omp parallel master
44753 #pragma omp taskloop simd lastprivate (x)
44754 isn't confused with
44755 #pragma omp parallel master taskloop simd lastprivate (x) */
44756 if (OMP_MASTER_COMBINED (ret))
44757 OMP_PARALLEL_COMBINED (stmt) = 1;
44758 return stmt;
44760 else if (strcmp (p, "loop") == 0)
44762 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44763 if (cclauses == NULL)
44764 cclauses = cclauses_buf;
44766 cp_lexer_consume_token (parser->lexer);
44767 if (!flag_openmp) /* flag_openmp_simd */
44768 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44769 cclauses, if_p);
44770 block = begin_omp_parallel ();
44771 save = cp_parser_begin_omp_structured_block (parser);
44772 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44773 cclauses, if_p);
44774 cp_parser_end_omp_structured_block (parser, save);
44775 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44776 block);
44777 if (ret == NULL_TREE)
44778 return ret;
44779 OMP_PARALLEL_COMBINED (stmt) = 1;
44780 return stmt;
44782 else if (!flag_openmp) /* flag_openmp_simd */
44784 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44785 return NULL_TREE;
44787 else if (cclauses == NULL && strcmp (p, "sections") == 0)
44789 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44790 cclauses = cclauses_buf;
44792 cp_lexer_consume_token (parser->lexer);
44793 block = begin_omp_parallel ();
44794 save = cp_parser_begin_omp_structured_block (parser);
44795 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
44796 cp_parser_end_omp_structured_block (parser, save);
44797 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44798 block);
44799 OMP_PARALLEL_COMBINED (stmt) = 1;
44800 return stmt;
44803 else if (!flag_openmp) /* flag_openmp_simd */
44805 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44806 return NULL_TREE;
44809 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44810 cclauses == NULL);
44811 if (cclauses)
44813 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
44814 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
44817 block = begin_omp_parallel ();
44818 save = cp_parser_begin_omp_structured_block (parser);
44819 parser->omp_attrs_forbidden_p = true;
44820 cp_parser_statement (parser, NULL_TREE, false, if_p);
44821 cp_parser_end_omp_structured_block (parser, save);
44822 stmt = finish_omp_parallel (clauses, block);
44823 return stmt;
44826 /* OpenMP 2.5:
44827 # pragma omp single single-clause[optseq] new-line
44828 structured-block */
44830 #define OMP_SINGLE_CLAUSE_MASK \
44831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
44834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44837 static tree
44838 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44840 tree stmt = make_node (OMP_SINGLE);
44841 TREE_TYPE (stmt) = void_type_node;
44842 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44844 OMP_SINGLE_CLAUSES (stmt)
44845 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
44846 "#pragma omp single", pragma_tok);
44847 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44849 return add_stmt (stmt);
44852 /* OpenMP 5.1:
44853 # pragma omp scope scope-clause[optseq] new-line
44854 structured-block */
44856 #define OMP_SCOPE_CLAUSE_MASK \
44857 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44863 static tree
44864 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44866 tree stmt = make_node (OMP_SCOPE);
44867 TREE_TYPE (stmt) = void_type_node;
44868 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44870 OMP_SCOPE_CLAUSES (stmt)
44871 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
44872 "#pragma omp scope", pragma_tok);
44873 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44875 return add_stmt (stmt);
44878 /* OpenMP 3.0:
44879 # pragma omp task task-clause[optseq] new-line
44880 structured-block */
44882 #define OMP_TASK_CLAUSE_MASK \
44883 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
44885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
44886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
44890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
44891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
44893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
44896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
44898 static tree
44899 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44901 tree clauses, block;
44902 unsigned int save;
44904 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
44905 "#pragma omp task", pragma_tok);
44906 block = begin_omp_task ();
44907 save = cp_parser_begin_omp_structured_block (parser);
44908 parser->omp_attrs_forbidden_p = true;
44909 cp_parser_statement (parser, NULL_TREE, false, if_p);
44910 cp_parser_end_omp_structured_block (parser, save);
44911 return finish_omp_task (clauses, block);
44914 /* OpenMP 3.0:
44915 # pragma omp taskwait new-line
44917 OpenMP 5.0:
44918 # pragma omp taskwait taskwait-clause[opt] new-line */
44920 #define OMP_TASKWAIT_CLAUSE_MASK \
44921 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44924 static void
44925 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
44927 tree clauses
44928 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
44929 "#pragma omp taskwait", pragma_tok);
44931 if (clauses)
44933 tree stmt = make_node (OMP_TASK);
44934 TREE_TYPE (stmt) = void_node;
44935 OMP_TASK_CLAUSES (stmt) = clauses;
44936 OMP_TASK_BODY (stmt) = NULL_TREE;
44937 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44938 add_stmt (stmt);
44940 else
44941 finish_omp_taskwait ();
44944 /* OpenMP 3.1:
44945 # pragma omp taskyield new-line */
44947 static void
44948 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
44950 cp_parser_require_pragma_eol (parser, pragma_tok);
44951 finish_omp_taskyield ();
44954 /* OpenMP 4.0:
44955 # pragma omp taskgroup new-line
44956 structured-block
44958 OpenMP 5.0:
44959 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
44961 #define OMP_TASKGROUP_CLAUSE_MASK \
44962 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
44965 static tree
44966 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44968 tree clauses
44969 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
44970 "#pragma omp taskgroup", pragma_tok);
44971 return c_finish_omp_taskgroup (input_location,
44972 cp_parser_omp_structured_block (parser,
44973 if_p),
44974 clauses);
44978 /* OpenMP 2.5:
44979 # pragma omp threadprivate (variable-list) */
44981 static void
44982 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
44984 tree vars;
44986 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
44987 cp_parser_require_pragma_eol (parser, pragma_tok);
44989 finish_omp_threadprivate (vars);
44992 /* OpenMP 4.0:
44993 # pragma omp cancel cancel-clause[optseq] new-line */
44995 #define OMP_CANCEL_CLAUSE_MASK \
44996 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
44997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
44998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
44999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
45000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
45002 static void
45003 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
45005 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
45006 "#pragma omp cancel", pragma_tok);
45007 finish_omp_cancel (clauses);
45010 /* OpenMP 4.0:
45011 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
45013 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
45014 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
45015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
45016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
45017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
45019 static bool
45020 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
45021 enum pragma_context context)
45023 tree clauses;
45024 bool point_seen = false;
45026 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45028 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45029 const char *p = IDENTIFIER_POINTER (id);
45031 if (strcmp (p, "point") == 0)
45033 cp_lexer_consume_token (parser->lexer);
45034 point_seen = true;
45037 if (!point_seen)
45039 cp_parser_error (parser, "expected %<point%>");
45040 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45041 return false;
45044 if (context != pragma_compound)
45046 if (context == pragma_stmt)
45047 error_at (pragma_tok->location,
45048 "%<#pragma %s%> may only be used in compound statements",
45049 "omp cancellation point");
45050 else
45051 cp_parser_error (parser, "expected declaration specifiers");
45052 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45053 return true;
45056 clauses = cp_parser_omp_all_clauses (parser,
45057 OMP_CANCELLATION_POINT_CLAUSE_MASK,
45058 "#pragma omp cancellation point",
45059 pragma_tok);
45060 finish_omp_cancellation_point (clauses);
45061 return true;
45064 /* OpenMP 4.0:
45065 #pragma omp distribute distribute-clause[optseq] new-line
45066 for-loop */
45068 #define OMP_DISTRIBUTE_CLAUSE_MASK \
45069 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
45073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45077 static tree
45078 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
45079 char *p_name, omp_clause_mask mask, tree *cclauses,
45080 bool *if_p)
45082 tree clauses, sb, ret;
45083 unsigned int save;
45084 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45086 strcat (p_name, " distribute");
45087 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
45089 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45091 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45092 const char *p = IDENTIFIER_POINTER (id);
45093 bool simd = false;
45094 bool parallel = false;
45096 if (strcmp (p, "simd") == 0)
45097 simd = true;
45098 else
45099 parallel = strcmp (p, "parallel") == 0;
45100 if (parallel || simd)
45102 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45103 if (cclauses == NULL)
45104 cclauses = cclauses_buf;
45105 cp_lexer_consume_token (parser->lexer);
45106 if (!flag_openmp) /* flag_openmp_simd */
45108 if (simd)
45109 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45110 cclauses, if_p);
45111 else
45112 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
45113 cclauses, if_p);
45115 sb = begin_omp_structured_block ();
45116 save = cp_parser_begin_omp_structured_block (parser);
45117 if (simd)
45118 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45119 cclauses, if_p);
45120 else
45121 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
45122 cclauses, if_p);
45123 cp_parser_end_omp_structured_block (parser, save);
45124 tree body = finish_omp_structured_block (sb);
45125 if (ret == NULL)
45126 return ret;
45127 ret = make_node (OMP_DISTRIBUTE);
45128 TREE_TYPE (ret) = void_type_node;
45129 OMP_FOR_BODY (ret) = body;
45130 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
45131 SET_EXPR_LOCATION (ret, loc);
45132 add_stmt (ret);
45133 return ret;
45136 if (!flag_openmp) /* flag_openmp_simd */
45138 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45139 return NULL_TREE;
45142 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45143 cclauses == NULL);
45144 if (cclauses)
45146 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
45147 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
45150 keep_next_level (true);
45151 sb = begin_omp_structured_block ();
45152 save = cp_parser_begin_omp_structured_block (parser);
45154 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
45156 cp_parser_end_omp_structured_block (parser, save);
45157 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
45159 return ret;
45162 /* OpenMP 4.0:
45163 # pragma omp teams teams-clause[optseq] new-line
45164 structured-block */
45166 #define OMP_TEAMS_CLAUSE_MASK \
45167 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
45172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
45173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
45176 static tree
45177 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
45178 char *p_name, omp_clause_mask mask, tree *cclauses,
45179 bool *if_p)
45181 tree clauses, sb, ret;
45182 unsigned int save;
45183 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45185 strcat (p_name, " teams");
45186 mask |= OMP_TEAMS_CLAUSE_MASK;
45188 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45190 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45191 const char *p = IDENTIFIER_POINTER (id);
45192 if (strcmp (p, "distribute") == 0)
45194 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45195 if (cclauses == NULL)
45196 cclauses = cclauses_buf;
45198 cp_lexer_consume_token (parser->lexer);
45199 if (!flag_openmp) /* flag_openmp_simd */
45200 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
45201 cclauses, if_p);
45202 keep_next_level (true);
45203 sb = begin_omp_structured_block ();
45204 save = cp_parser_begin_omp_structured_block (parser);
45205 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
45206 cclauses, if_p);
45207 cp_parser_end_omp_structured_block (parser, save);
45208 tree body = finish_omp_structured_block (sb);
45209 if (ret == NULL)
45210 return ret;
45211 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45212 ret = make_node (OMP_TEAMS);
45213 TREE_TYPE (ret) = void_type_node;
45214 OMP_TEAMS_CLAUSES (ret) = clauses;
45215 OMP_TEAMS_BODY (ret) = body;
45216 OMP_TEAMS_COMBINED (ret) = 1;
45217 SET_EXPR_LOCATION (ret, loc);
45218 return add_stmt (ret);
45220 else if (strcmp (p, "loop") == 0)
45222 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45223 if (cclauses == NULL)
45224 cclauses = cclauses_buf;
45226 cp_lexer_consume_token (parser->lexer);
45227 if (!flag_openmp) /* flag_openmp_simd */
45228 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45229 cclauses, if_p);
45230 keep_next_level (true);
45231 sb = begin_omp_structured_block ();
45232 save = cp_parser_begin_omp_structured_block (parser);
45233 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45234 cclauses, if_p);
45235 cp_parser_end_omp_structured_block (parser, save);
45236 tree body = finish_omp_structured_block (sb);
45237 if (ret == NULL)
45238 return ret;
45239 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45240 ret = make_node (OMP_TEAMS);
45241 TREE_TYPE (ret) = void_type_node;
45242 OMP_TEAMS_CLAUSES (ret) = clauses;
45243 OMP_TEAMS_BODY (ret) = body;
45244 OMP_TEAMS_COMBINED (ret) = 1;
45245 SET_EXPR_LOCATION (ret, loc);
45246 return add_stmt (ret);
45249 if (!flag_openmp) /* flag_openmp_simd */
45251 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45252 return NULL_TREE;
45255 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45256 cclauses == NULL);
45257 if (cclauses)
45259 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
45260 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45263 tree stmt = make_node (OMP_TEAMS);
45264 TREE_TYPE (stmt) = void_type_node;
45265 OMP_TEAMS_CLAUSES (stmt) = clauses;
45266 keep_next_level (true);
45267 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45268 SET_EXPR_LOCATION (stmt, loc);
45270 return add_stmt (stmt);
45273 /* OpenMP 4.0:
45274 # pragma omp target data target-data-clause[optseq] new-line
45275 structured-block */
45277 #define OMP_TARGET_DATA_CLAUSE_MASK \
45278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
45282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
45284 static tree
45285 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45287 if (flag_openmp)
45288 omp_requires_mask
45289 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45291 tree clauses
45292 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
45293 "#pragma omp target data", pragma_tok);
45294 c_omp_adjust_map_clauses (clauses, false);
45295 int map_seen = 0;
45296 for (tree *pc = &clauses; *pc;)
45298 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45299 switch (OMP_CLAUSE_MAP_KIND (*pc))
45301 case GOMP_MAP_TO:
45302 case GOMP_MAP_ALWAYS_TO:
45303 case GOMP_MAP_PRESENT_TO:
45304 case GOMP_MAP_ALWAYS_PRESENT_TO:
45305 case GOMP_MAP_FROM:
45306 case GOMP_MAP_ALWAYS_FROM:
45307 case GOMP_MAP_PRESENT_FROM:
45308 case GOMP_MAP_ALWAYS_PRESENT_FROM:
45309 case GOMP_MAP_TOFROM:
45310 case GOMP_MAP_ALWAYS_TOFROM:
45311 case GOMP_MAP_PRESENT_TOFROM:
45312 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
45313 case GOMP_MAP_ALLOC:
45314 case GOMP_MAP_PRESENT_ALLOC:
45315 map_seen = 3;
45316 break;
45317 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45318 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
45319 case GOMP_MAP_ALWAYS_POINTER:
45320 case GOMP_MAP_ATTACH_DETACH:
45321 break;
45322 default:
45323 map_seen |= 1;
45324 error_at (OMP_CLAUSE_LOCATION (*pc),
45325 "%<#pragma omp target data%> with map-type other "
45326 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
45327 "on %<map%> clause");
45328 *pc = OMP_CLAUSE_CHAIN (*pc);
45329 continue;
45331 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
45332 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
45333 map_seen = 3;
45334 pc = &OMP_CLAUSE_CHAIN (*pc);
45337 if (map_seen != 3)
45339 if (map_seen == 0)
45340 error_at (pragma_tok->location,
45341 "%<#pragma omp target data%> must contain at least "
45342 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
45343 "clause");
45344 return NULL_TREE;
45347 tree stmt = make_node (OMP_TARGET_DATA);
45348 TREE_TYPE (stmt) = void_type_node;
45349 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
45351 keep_next_level (true);
45352 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45354 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45355 return add_stmt (stmt);
45358 /* OpenMP 4.5:
45359 # pragma omp target enter data target-enter-data-clause[optseq] new-line
45360 structured-block */
45362 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
45363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45369 static bool
45370 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
45371 enum pragma_context context)
45373 bool data_seen = false;
45374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45376 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45377 const char *p = IDENTIFIER_POINTER (id);
45379 if (strcmp (p, "data") == 0)
45381 cp_lexer_consume_token (parser->lexer);
45382 data_seen = true;
45385 if (!data_seen)
45387 cp_parser_error (parser, "expected %<data%>");
45388 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45389 return false;
45392 if (context == pragma_stmt)
45394 error_at (pragma_tok->location,
45395 "%<#pragma %s%> may only be used in compound statements",
45396 "omp target enter data");
45397 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45398 return true;
45401 if (flag_openmp)
45402 omp_requires_mask
45403 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45405 tree clauses
45406 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
45407 "#pragma omp target enter data", pragma_tok);
45408 c_omp_adjust_map_clauses (clauses, false);
45409 int map_seen = 0;
45410 for (tree *pc = &clauses; *pc;)
45412 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45413 switch (OMP_CLAUSE_MAP_KIND (*pc))
45415 case GOMP_MAP_TO:
45416 case GOMP_MAP_ALWAYS_TO:
45417 case GOMP_MAP_PRESENT_TO:
45418 case GOMP_MAP_ALWAYS_PRESENT_TO:
45419 case GOMP_MAP_ALLOC:
45420 case GOMP_MAP_PRESENT_ALLOC:
45421 map_seen = 3;
45422 break;
45423 case GOMP_MAP_TOFROM:
45424 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
45425 map_seen = 3;
45426 break;
45427 case GOMP_MAP_ALWAYS_TOFROM:
45428 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
45429 map_seen = 3;
45430 break;
45431 case GOMP_MAP_PRESENT_TOFROM:
45432 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_TO);
45433 map_seen = 3;
45434 break;
45435 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
45436 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_TO);
45437 map_seen = 3;
45438 break;
45439 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45440 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
45441 case GOMP_MAP_ALWAYS_POINTER:
45442 case GOMP_MAP_ATTACH_DETACH:
45443 break;
45444 default:
45445 map_seen |= 1;
45446 error_at (OMP_CLAUSE_LOCATION (*pc),
45447 "%<#pragma omp target enter data%> with map-type other "
45448 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
45449 *pc = OMP_CLAUSE_CHAIN (*pc);
45450 continue;
45452 pc = &OMP_CLAUSE_CHAIN (*pc);
45455 if (map_seen != 3)
45457 if (map_seen == 0)
45458 error_at (pragma_tok->location,
45459 "%<#pragma omp target enter data%> must contain at least "
45460 "one %<map%> clause");
45461 return true;
45464 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
45465 TREE_TYPE (stmt) = void_type_node;
45466 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
45467 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45468 add_stmt (stmt);
45469 return true;
45472 /* OpenMP 4.5:
45473 # pragma omp target exit data target-enter-data-clause[optseq] new-line
45474 structured-block */
45476 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
45477 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45483 static bool
45484 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
45485 enum pragma_context context)
45487 bool data_seen = false;
45488 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45490 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45491 const char *p = IDENTIFIER_POINTER (id);
45493 if (strcmp (p, "data") == 0)
45495 cp_lexer_consume_token (parser->lexer);
45496 data_seen = true;
45499 if (!data_seen)
45501 cp_parser_error (parser, "expected %<data%>");
45502 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45503 return false;
45506 if (context == pragma_stmt)
45508 error_at (pragma_tok->location,
45509 "%<#pragma %s%> may only be used in compound statements",
45510 "omp target exit data");
45511 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45512 return true;
45515 if (flag_openmp)
45516 omp_requires_mask
45517 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45519 tree clauses
45520 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
45521 "#pragma omp target exit data", pragma_tok);
45522 c_omp_adjust_map_clauses (clauses, false);
45523 int map_seen = 0;
45524 for (tree *pc = &clauses; *pc;)
45526 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45527 switch (OMP_CLAUSE_MAP_KIND (*pc))
45529 case GOMP_MAP_FROM:
45530 case GOMP_MAP_ALWAYS_FROM:
45531 case GOMP_MAP_PRESENT_FROM:
45532 case GOMP_MAP_ALWAYS_PRESENT_FROM:
45533 case GOMP_MAP_RELEASE:
45534 case GOMP_MAP_DELETE:
45535 map_seen = 3;
45536 break;
45537 case GOMP_MAP_TOFROM:
45538 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
45539 map_seen = 3;
45540 break;
45541 case GOMP_MAP_ALWAYS_TOFROM:
45542 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
45543 map_seen = 3;
45544 break;
45545 case GOMP_MAP_PRESENT_TOFROM:
45546 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_FROM);
45547 map_seen = 3;
45548 break;
45549 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
45550 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_FROM);
45551 map_seen = 3;
45552 break;
45553 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45554 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
45555 case GOMP_MAP_ALWAYS_POINTER:
45556 case GOMP_MAP_ATTACH_DETACH:
45557 break;
45558 default:
45559 map_seen |= 1;
45560 error_at (OMP_CLAUSE_LOCATION (*pc),
45561 "%<#pragma omp target exit data%> with map-type other "
45562 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
45563 "on %<map%> clause");
45564 *pc = OMP_CLAUSE_CHAIN (*pc);
45565 continue;
45567 pc = &OMP_CLAUSE_CHAIN (*pc);
45570 if (map_seen != 3)
45572 if (map_seen == 0)
45573 error_at (pragma_tok->location,
45574 "%<#pragma omp target exit data%> must contain at least "
45575 "one %<map%> clause");
45576 return true;
45579 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
45580 TREE_TYPE (stmt) = void_type_node;
45581 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
45582 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45583 add_stmt (stmt);
45584 return true;
45587 /* OpenMP 4.0:
45588 # pragma omp target update target-update-clause[optseq] new-line */
45590 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
45591 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
45592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
45593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45598 static bool
45599 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
45600 enum pragma_context context)
45602 if (context == pragma_stmt)
45604 error_at (pragma_tok->location,
45605 "%<#pragma %s%> may only be used in compound statements",
45606 "omp target update");
45607 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45608 return true;
45611 tree clauses
45612 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
45613 "#pragma omp target update", pragma_tok);
45614 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
45615 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
45617 error_at (pragma_tok->location,
45618 "%<#pragma omp target update%> must contain at least one "
45619 "%<from%> or %<to%> clauses");
45620 return true;
45623 if (flag_openmp)
45624 omp_requires_mask
45625 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45627 tree stmt = make_node (OMP_TARGET_UPDATE);
45628 TREE_TYPE (stmt) = void_type_node;
45629 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
45630 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45631 add_stmt (stmt);
45632 return true;
45635 /* OpenMP 4.0:
45636 # pragma omp target target-clause[optseq] new-line
45637 structured-block */
45639 #define OMP_TARGET_CLAUSE_MASK \
45640 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
45645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
45648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
45650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
45651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
45652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
45654 static bool
45655 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
45656 enum pragma_context context, bool *if_p)
45658 if (flag_openmp)
45659 omp_requires_mask
45660 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45662 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45664 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45665 const char *p = IDENTIFIER_POINTER (id);
45666 enum tree_code ccode = ERROR_MARK;
45668 if (strcmp (p, "teams") == 0)
45669 ccode = OMP_TEAMS;
45670 else if (strcmp (p, "parallel") == 0)
45671 ccode = OMP_PARALLEL;
45672 else if (strcmp (p, "simd") == 0)
45673 ccode = OMP_SIMD;
45674 if (ccode != ERROR_MARK)
45676 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
45677 char p_name[sizeof ("#pragma omp target teams distribute "
45678 "parallel for simd")];
45680 cp_lexer_consume_token (parser->lexer);
45681 strcpy (p_name, "#pragma omp target");
45682 if (!flag_openmp) /* flag_openmp_simd */
45684 tree stmt;
45685 switch (ccode)
45687 case OMP_TEAMS:
45688 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
45689 OMP_TARGET_CLAUSE_MASK,
45690 cclauses, if_p);
45691 break;
45692 case OMP_PARALLEL:
45693 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
45694 OMP_TARGET_CLAUSE_MASK,
45695 cclauses, if_p);
45696 break;
45697 case OMP_SIMD:
45698 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
45699 OMP_TARGET_CLAUSE_MASK,
45700 cclauses, if_p);
45701 break;
45702 default:
45703 gcc_unreachable ();
45705 return stmt != NULL_TREE;
45707 keep_next_level (true);
45708 tree sb = begin_omp_structured_block (), ret;
45709 unsigned save = cp_parser_begin_omp_structured_block (parser);
45710 switch (ccode)
45712 case OMP_TEAMS:
45713 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
45714 OMP_TARGET_CLAUSE_MASK, cclauses,
45715 if_p);
45716 break;
45717 case OMP_PARALLEL:
45718 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
45719 OMP_TARGET_CLAUSE_MASK, cclauses,
45720 if_p);
45721 break;
45722 case OMP_SIMD:
45723 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
45724 OMP_TARGET_CLAUSE_MASK, cclauses,
45725 if_p);
45726 break;
45727 default:
45728 gcc_unreachable ();
45730 cp_parser_end_omp_structured_block (parser, save);
45731 tree body = finish_omp_structured_block (sb);
45732 if (ret == NULL_TREE)
45733 return false;
45734 if (ccode == OMP_TEAMS && !processing_template_decl)
45735 /* For combined target teams, ensure the num_teams and
45736 thread_limit clause expressions are evaluated on the host,
45737 before entering the target construct. */
45738 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45739 c; c = OMP_CLAUSE_CHAIN (c))
45740 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
45741 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
45742 for (int i = 0;
45743 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
45744 if (OMP_CLAUSE_OPERAND (c, i)
45745 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
45747 tree expr = OMP_CLAUSE_OPERAND (c, i);
45748 expr = force_target_expr (TREE_TYPE (expr), expr,
45749 tf_none);
45750 if (expr == error_mark_node)
45751 continue;
45752 tree tmp = TARGET_EXPR_SLOT (expr);
45753 add_stmt (expr);
45754 OMP_CLAUSE_OPERAND (c, i) = expr;
45755 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
45756 OMP_CLAUSE_FIRSTPRIVATE);
45757 OMP_CLAUSE_DECL (tc) = tmp;
45758 OMP_CLAUSE_CHAIN (tc)
45759 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
45760 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
45762 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
45763 finish_omp_target (pragma_tok->location,
45764 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
45765 return true;
45767 else if (!flag_openmp) /* flag_openmp_simd */
45769 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45770 return false;
45772 else if (strcmp (p, "data") == 0)
45774 cp_lexer_consume_token (parser->lexer);
45775 cp_parser_omp_target_data (parser, pragma_tok, if_p);
45776 return true;
45778 else if (strcmp (p, "enter") == 0)
45780 cp_lexer_consume_token (parser->lexer);
45781 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
45783 else if (strcmp (p, "exit") == 0)
45785 cp_lexer_consume_token (parser->lexer);
45786 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
45788 else if (strcmp (p, "update") == 0)
45790 cp_lexer_consume_token (parser->lexer);
45791 return cp_parser_omp_target_update (parser, pragma_tok, context);
45794 if (!flag_openmp) /* flag_openmp_simd */
45796 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45797 return false;
45800 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
45801 "#pragma omp target", pragma_tok,
45802 false);
45803 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45804 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
45806 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
45807 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
45808 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
45809 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
45810 OMP_CLAUSE_CHAIN (c) = nc;
45812 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
45814 c_omp_adjust_map_clauses (clauses, true);
45815 keep_next_level (true);
45816 tree body = cp_parser_omp_structured_block (parser, if_p);
45818 finish_omp_target (pragma_tok->location, clauses, body, false);
45819 return true;
45822 /* OpenACC 2.0:
45823 # pragma acc cache (variable-list) new-line
45826 static tree
45827 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
45829 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
45830 clauses. */
45831 auto_suppress_location_wrappers sentinel;
45833 tree stmt, clauses;
45835 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
45836 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
45838 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
45840 stmt = make_node (OACC_CACHE);
45841 TREE_TYPE (stmt) = void_type_node;
45842 OACC_CACHE_CLAUSES (stmt) = clauses;
45843 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45844 add_stmt (stmt);
45846 return stmt;
45849 /* OpenACC 2.0:
45850 # pragma acc data oacc-data-clause[optseq] new-line
45851 structured-block */
45853 #define OACC_DATA_CLAUSE_MASK \
45854 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
45860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
45865 static tree
45866 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45868 tree stmt, clauses, block;
45869 unsigned int save;
45871 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
45872 "#pragma acc data", pragma_tok);
45874 block = begin_omp_parallel ();
45875 save = cp_parser_begin_omp_structured_block (parser);
45876 cp_parser_statement (parser, NULL_TREE, false, if_p);
45877 cp_parser_end_omp_structured_block (parser, save);
45878 stmt = finish_oacc_data (clauses, block);
45879 return stmt;
45882 /* OpenACC 2.0:
45883 # pragma acc host_data <clauses> new-line
45884 structured-block */
45886 #define OACC_HOST_DATA_CLAUSE_MASK \
45887 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
45888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
45891 static tree
45892 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45894 tree stmt, clauses, block;
45895 unsigned int save;
45897 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
45898 "#pragma acc host_data", pragma_tok);
45900 block = begin_omp_parallel ();
45901 save = cp_parser_begin_omp_structured_block (parser);
45902 cp_parser_statement (parser, NULL_TREE, false, if_p);
45903 cp_parser_end_omp_structured_block (parser, save);
45904 stmt = finish_oacc_host_data (clauses, block);
45905 return stmt;
45908 /* OpenACC 2.0:
45909 # pragma acc declare oacc-data-clause[optseq] new-line
45912 #define OACC_DECLARE_CLAUSE_MASK \
45913 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
45919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
45920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
45922 static tree
45923 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
45925 tree clauses, stmt;
45926 bool error = false;
45927 bool found_in_scope = global_bindings_p ();
45929 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
45930 "#pragma acc declare", pragma_tok, true);
45933 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45935 error_at (pragma_tok->location,
45936 "no valid clauses specified in %<#pragma acc declare%>");
45937 return NULL_TREE;
45940 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
45942 location_t loc = OMP_CLAUSE_LOCATION (t);
45943 tree decl = OMP_CLAUSE_DECL (t);
45944 if (!DECL_P (decl))
45946 error_at (loc, "array section in %<#pragma acc declare%>");
45947 error = true;
45948 continue;
45950 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
45951 switch (OMP_CLAUSE_MAP_KIND (t))
45953 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45954 case GOMP_MAP_ALLOC:
45955 case GOMP_MAP_TO:
45956 case GOMP_MAP_FORCE_DEVICEPTR:
45957 case GOMP_MAP_DEVICE_RESIDENT:
45958 break;
45960 case GOMP_MAP_LINK:
45961 if (!global_bindings_p ()
45962 && (TREE_STATIC (decl)
45963 || !DECL_EXTERNAL (decl)))
45965 error_at (loc,
45966 "%qD must be a global variable in "
45967 "%<#pragma acc declare link%>",
45968 decl);
45969 error = true;
45970 continue;
45972 break;
45974 default:
45975 if (global_bindings_p ())
45977 error_at (loc, "invalid OpenACC clause at file scope");
45978 error = true;
45979 continue;
45981 if (DECL_EXTERNAL (decl))
45983 error_at (loc,
45984 "invalid use of %<extern%> variable %qD "
45985 "in %<#pragma acc declare%>", decl);
45986 error = true;
45987 continue;
45989 else if (TREE_PUBLIC (decl))
45991 error_at (loc,
45992 "invalid use of %<global%> variable %qD "
45993 "in %<#pragma acc declare%>", decl);
45994 error = true;
45995 continue;
45997 break;
46000 if (!found_in_scope)
46001 /* This seems to ignore the existence of cleanup scopes?
46002 What is the meaning for local extern decls? The local
46003 extern is in this scope, but it is referring to a decl that
46004 is namespace scope. */
46005 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
46006 if (d == decl)
46008 found_in_scope = true;
46009 break;
46011 if (!found_in_scope)
46013 error_at (loc,
46014 "%qD must be a variable declared in the same scope as "
46015 "%<#pragma acc declare%>", decl);
46016 error = true;
46017 continue;
46020 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
46021 || lookup_attribute ("omp declare target link",
46022 DECL_ATTRIBUTES (decl)))
46024 error_at (loc, "variable %qD used more than once with "
46025 "%<#pragma acc declare%>", decl);
46026 error = true;
46027 continue;
46030 if (!error)
46032 tree id;
46034 if (DECL_LOCAL_DECL_P (decl))
46035 /* We need to mark the aliased decl, as that is the entity
46036 that is being referred to. This won't work for
46037 dependent variables, but it didn't work for them before
46038 DECL_LOCAL_DECL_P was a thing either. But then
46039 dependent local extern variable decls are as rare as
46040 hen's teeth. */
46041 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
46042 if (alias != error_mark_node)
46043 decl = alias;
46045 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
46046 id = get_identifier ("omp declare target link");
46047 else
46048 id = get_identifier ("omp declare target");
46050 DECL_ATTRIBUTES (decl)
46051 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
46052 if (current_binding_level->kind == sk_namespace)
46054 symtab_node *node = symtab_node::get (decl);
46055 if (node != NULL)
46057 node->offloadable = 1;
46058 if (ENABLE_OFFLOADING)
46060 g->have_offload = true;
46061 if (is_a <varpool_node *> (node))
46062 vec_safe_push (offload_vars, decl);
46069 if (error || current_binding_level->kind == sk_namespace)
46070 return NULL_TREE;
46072 stmt = make_node (OACC_DECLARE);
46073 TREE_TYPE (stmt) = void_type_node;
46074 OACC_DECLARE_CLAUSES (stmt) = clauses;
46075 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46077 add_stmt (stmt);
46079 return NULL_TREE;
46082 /* OpenACC 2.0:
46083 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
46087 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
46089 LOC is the location of the #pragma token.
46092 #define OACC_ENTER_DATA_CLAUSE_MASK \
46093 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46100 #define OACC_EXIT_DATA_CLAUSE_MASK \
46101 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
46105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
46106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
46107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46109 static tree
46110 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
46111 bool enter)
46113 location_t loc = pragma_tok->location;
46114 tree stmt, clauses;
46115 const char *p = "";
46117 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46118 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46120 if (strcmp (p, "data") != 0)
46122 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
46123 enter ? "enter" : "exit");
46124 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46125 return NULL_TREE;
46128 cp_lexer_consume_token (parser->lexer);
46130 if (enter)
46131 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
46132 "#pragma acc enter data", pragma_tok);
46133 else
46134 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
46135 "#pragma acc exit data", pragma_tok);
46137 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
46139 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
46140 enter ? "enter" : "exit");
46141 return NULL_TREE;
46144 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
46145 TREE_TYPE (stmt) = void_type_node;
46146 OMP_STANDALONE_CLAUSES (stmt) = clauses;
46147 SET_EXPR_LOCATION (stmt, loc);
46148 add_stmt (stmt);
46149 return stmt;
46152 /* OpenACC 2.0:
46153 # pragma acc loop oacc-loop-clause[optseq] new-line
46154 structured-block */
46156 #define OACC_LOOP_CLAUSE_MASK \
46157 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
46158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
46161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
46162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
46163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
46164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
46165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
46166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
46168 static tree
46169 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
46170 omp_clause_mask mask, tree *cclauses, bool *if_p)
46172 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
46174 strcat (p_name, " loop");
46175 mask |= OACC_LOOP_CLAUSE_MASK;
46177 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
46178 cclauses == NULL);
46179 if (cclauses)
46181 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
46182 if (*cclauses)
46183 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
46184 if (clauses)
46185 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
46188 tree block = begin_omp_structured_block ();
46189 int save = cp_parser_begin_omp_structured_block (parser);
46190 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
46191 cp_parser_end_omp_structured_block (parser, save);
46192 add_stmt (finish_omp_structured_block (block));
46194 return stmt;
46197 /* OpenACC 2.0:
46198 # pragma acc kernels oacc-kernels-clause[optseq] new-line
46199 structured-block
46203 # pragma acc parallel oacc-parallel-clause[optseq] new-line
46204 structured-block
46206 OpenACC 2.6:
46208 # pragma acc serial oacc-serial-clause[optseq] new-line
46211 #define OACC_KERNELS_CLAUSE_MASK \
46212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
46223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
46224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
46226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46228 #define OACC_PARALLEL_CLAUSE_MASK \
46229 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
46238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
46241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
46242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
46246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46248 #define OACC_SERIAL_CLAUSE_MASK \
46249 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
46261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46265 static tree
46266 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
46267 char *p_name, bool *if_p)
46269 omp_clause_mask mask;
46270 enum tree_code code;
46271 switch (cp_parser_pragma_kind (pragma_tok))
46273 case PRAGMA_OACC_KERNELS:
46274 strcat (p_name, " kernels");
46275 mask = OACC_KERNELS_CLAUSE_MASK;
46276 code = OACC_KERNELS;
46277 break;
46278 case PRAGMA_OACC_PARALLEL:
46279 strcat (p_name, " parallel");
46280 mask = OACC_PARALLEL_CLAUSE_MASK;
46281 code = OACC_PARALLEL;
46282 break;
46283 case PRAGMA_OACC_SERIAL:
46284 strcat (p_name, " serial");
46285 mask = OACC_SERIAL_CLAUSE_MASK;
46286 code = OACC_SERIAL;
46287 break;
46288 default:
46289 gcc_unreachable ();
46292 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46294 const char *p
46295 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46296 if (strcmp (p, "loop") == 0)
46298 cp_lexer_consume_token (parser->lexer);
46299 tree block = begin_omp_parallel ();
46300 tree clauses;
46301 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
46302 &clauses, if_p);
46303 protected_set_expr_location (stmt, pragma_tok->location);
46304 return finish_omp_construct (code, block, clauses);
46308 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
46310 tree block = begin_omp_parallel ();
46311 unsigned int save = cp_parser_begin_omp_structured_block (parser);
46312 cp_parser_statement (parser, NULL_TREE, false, if_p);
46313 cp_parser_end_omp_structured_block (parser, save);
46314 return finish_omp_construct (code, block, clauses);
46317 /* OpenACC 2.0:
46318 # pragma acc update oacc-update-clause[optseq] new-line
46321 #define OACC_UPDATE_CLAUSE_MASK \
46322 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
46324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
46325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
46327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
46329 static tree
46330 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
46332 tree stmt, clauses;
46334 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
46335 "#pragma acc update", pragma_tok);
46337 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
46339 error_at (pragma_tok->location,
46340 "%<#pragma acc update%> must contain at least one "
46341 "%<device%> or %<host%> or %<self%> clause");
46342 return NULL_TREE;
46345 stmt = make_node (OACC_UPDATE);
46346 TREE_TYPE (stmt) = void_type_node;
46347 OACC_UPDATE_CLAUSES (stmt) = clauses;
46348 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46349 add_stmt (stmt);
46350 return stmt;
46353 /* OpenACC 2.0:
46354 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
46356 LOC is the location of the #pragma token.
46359 #define OACC_WAIT_CLAUSE_MASK \
46360 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
46362 static tree
46363 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
46365 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
46366 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46368 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
46369 list = cp_parser_oacc_wait_list (parser, loc, list);
46371 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
46372 "#pragma acc wait", pragma_tok);
46374 stmt = c_finish_oacc_wait (loc, list, clauses);
46375 stmt = finish_expr_stmt (stmt);
46377 return stmt;
46380 /* OpenMP 4.0:
46381 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
46383 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
46384 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
46385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
46386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
46387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
46388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
46389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
46391 static void
46392 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
46393 enum pragma_context context,
46394 bool variant_p)
46396 bool first_p = parser->omp_declare_simd == NULL;
46397 cp_omp_declare_simd_data data;
46398 if (first_p)
46400 data.error_seen = false;
46401 data.fndecl_seen = false;
46402 data.variant_p = variant_p;
46403 data.tokens = vNULL;
46404 data.attribs[0] = NULL;
46405 data.attribs[1] = NULL;
46406 data.loc = UNKNOWN_LOCATION;
46407 /* It is safe to take the address of a local variable; it will only be
46408 used while this scope is live. */
46409 parser->omp_declare_simd = &data;
46411 else if (parser->omp_declare_simd->variant_p != variant_p)
46413 error_at (pragma_tok->location,
46414 "%<#pragma omp declare %s%> followed by "
46415 "%<#pragma omp declare %s%>",
46416 parser->omp_declare_simd->variant_p ? "variant" : "simd",
46417 parser->omp_declare_simd->variant_p ? "simd" : "variant");
46418 parser->omp_declare_simd->error_seen = true;
46421 /* Store away all pragma tokens. */
46422 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46423 cp_lexer_consume_token (parser->lexer);
46424 cp_parser_require_pragma_eol (parser, pragma_tok);
46425 struct cp_token_cache *cp
46426 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
46427 parser->omp_declare_simd->tokens.safe_push (cp);
46429 if (first_p)
46431 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
46432 cp_parser_pragma (parser, context, NULL);
46433 switch (context)
46435 case pragma_external:
46436 cp_parser_declaration (parser, NULL_TREE);
46437 break;
46438 case pragma_member:
46439 cp_parser_member_declaration (parser);
46440 break;
46441 case pragma_objc_icode:
46442 cp_parser_block_declaration (parser, /*statement_p=*/false);
46443 break;
46444 default:
46445 cp_parser_declaration_statement (parser);
46446 break;
46448 if (parser->omp_declare_simd
46449 && !parser->omp_declare_simd->error_seen
46450 && !parser->omp_declare_simd->fndecl_seen)
46451 error_at (pragma_tok->location,
46452 "%<#pragma omp declare %s%> not immediately followed by "
46453 "function declaration or definition",
46454 parser->omp_declare_simd->variant_p ? "variant" : "simd");
46455 data.tokens.release ();
46456 parser->omp_declare_simd = NULL;
46460 static const char *const omp_construct_selectors[] = {
46461 "simd", "target", "teams", "parallel", "for", NULL };
46462 static const char *const omp_device_selectors[] = {
46463 "kind", "isa", "arch", NULL };
46464 static const char *const omp_implementation_selectors[] = {
46465 "vendor", "extension", "atomic_default_mem_order", "unified_address",
46466 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
46467 static const char *const omp_user_selectors[] = {
46468 "condition", NULL };
46470 /* OpenMP 5.0:
46472 trait-selector:
46473 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
46475 trait-score:
46476 score(score-expression) */
46478 static tree
46479 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
46481 tree ret = NULL_TREE;
46484 tree selector;
46485 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
46486 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46487 selector = cp_lexer_peek_token (parser->lexer)->u.value;
46488 else
46490 cp_parser_error (parser, "expected trait selector name");
46491 return error_mark_node;
46494 tree properties = NULL_TREE;
46495 const char *const *selectors = NULL;
46496 bool allow_score = true;
46497 bool allow_user = false;
46498 int property_limit = 0;
46499 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
46500 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
46501 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
46502 switch (IDENTIFIER_POINTER (set)[0])
46504 case 'c': /* construct */
46505 selectors = omp_construct_selectors;
46506 allow_score = false;
46507 property_limit = 1;
46508 property_kind = CTX_PROPERTY_SIMD;
46509 break;
46510 case 'd': /* device */
46511 selectors = omp_device_selectors;
46512 allow_score = false;
46513 allow_user = true;
46514 property_limit = 3;
46515 property_kind = CTX_PROPERTY_NAME_LIST;
46516 break;
46517 case 'i': /* implementation */
46518 selectors = omp_implementation_selectors;
46519 allow_user = true;
46520 property_limit = 3;
46521 property_kind = CTX_PROPERTY_NAME_LIST;
46522 break;
46523 case 'u': /* user */
46524 selectors = omp_user_selectors;
46525 property_limit = 1;
46526 property_kind = CTX_PROPERTY_EXPR;
46527 break;
46528 default:
46529 gcc_unreachable ();
46531 for (int i = 0; ; i++)
46533 if (selectors[i] == NULL)
46535 if (allow_user)
46537 property_kind = CTX_PROPERTY_USER;
46538 break;
46540 else
46542 error ("selector %qs not allowed for context selector "
46543 "set %qs", IDENTIFIER_POINTER (selector),
46544 IDENTIFIER_POINTER (set));
46545 cp_lexer_consume_token (parser->lexer);
46546 return error_mark_node;
46549 if (i == property_limit)
46550 property_kind = CTX_PROPERTY_NONE;
46551 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
46552 break;
46554 if (property_kind == CTX_PROPERTY_NAME_LIST
46555 && IDENTIFIER_POINTER (set)[0] == 'i'
46556 && strcmp (IDENTIFIER_POINTER (selector),
46557 "atomic_default_mem_order") == 0)
46558 property_kind = CTX_PROPERTY_ID;
46560 cp_lexer_consume_token (parser->lexer);
46562 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46564 if (property_kind == CTX_PROPERTY_NONE)
46566 error ("selector %qs does not accept any properties",
46567 IDENTIFIER_POINTER (selector));
46568 return error_mark_node;
46571 matching_parens parens;
46572 parens.consume_open (parser);
46574 cp_token *token = cp_lexer_peek_token (parser->lexer);
46575 if (allow_score
46576 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
46577 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
46578 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
46580 cp_lexer_save_tokens (parser->lexer);
46581 cp_lexer_consume_token (parser->lexer);
46582 cp_lexer_consume_token (parser->lexer);
46583 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
46584 true)
46585 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
46587 cp_lexer_rollback_tokens (parser->lexer);
46588 cp_lexer_consume_token (parser->lexer);
46590 matching_parens parens2;
46591 parens2.require_open (parser);
46592 tree score = cp_parser_constant_expression (parser);
46593 if (!parens2.require_close (parser))
46594 cp_parser_skip_to_closing_parenthesis (parser, true,
46595 false, true);
46596 cp_parser_require (parser, CPP_COLON, RT_COLON);
46597 if (score != error_mark_node)
46599 score = fold_non_dependent_expr (score);
46600 if (value_dependent_expression_p (score))
46601 properties = tree_cons (get_identifier (" score"),
46602 score, properties);
46603 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
46604 || TREE_CODE (score) != INTEGER_CST)
46605 error_at (token->location, "score argument must be "
46606 "constant integer expression");
46607 else if (tree_int_cst_sgn (score) < 0)
46608 error_at (token->location, "score argument must be "
46609 "non-negative");
46610 else
46611 properties = tree_cons (get_identifier (" score"),
46612 score, properties);
46615 else
46616 cp_lexer_rollback_tokens (parser->lexer);
46618 token = cp_lexer_peek_token (parser->lexer);
46621 switch (property_kind)
46623 tree t;
46624 case CTX_PROPERTY_USER:
46627 t = cp_parser_constant_expression (parser);
46628 if (t != error_mark_node)
46630 t = fold_non_dependent_expr (t);
46631 if (TREE_CODE (t) == STRING_CST)
46632 properties = tree_cons (NULL_TREE, t, properties);
46633 else if (!value_dependent_expression_p (t)
46634 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
46635 || !tree_fits_shwi_p (t)))
46636 error_at (token->location, "property must be "
46637 "constant integer expression or string "
46638 "literal");
46639 else
46640 properties = tree_cons (NULL_TREE, t, properties);
46642 else
46643 return error_mark_node;
46645 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46646 cp_lexer_consume_token (parser->lexer);
46647 else
46648 break;
46650 while (1);
46651 break;
46652 case CTX_PROPERTY_ID:
46653 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
46654 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46656 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
46657 cp_lexer_consume_token (parser->lexer);
46658 properties = tree_cons (prop, NULL_TREE, properties);
46660 else
46662 cp_parser_error (parser, "expected identifier");
46663 return error_mark_node;
46665 break;
46666 case CTX_PROPERTY_NAME_LIST:
46669 tree prop = NULL_TREE, value = NULL_TREE;
46670 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
46671 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46673 prop = cp_lexer_peek_token (parser->lexer)->u.value;
46674 cp_lexer_consume_token (parser->lexer);
46676 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
46677 value = cp_parser_string_literal (parser,
46678 /*translate=*/false,
46679 /*wide_ok=*/false);
46680 else
46682 cp_parser_error (parser, "expected identifier or "
46683 "string literal");
46684 return error_mark_node;
46687 properties = tree_cons (prop, value, properties);
46689 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46690 cp_lexer_consume_token (parser->lexer);
46691 else
46692 break;
46694 while (1);
46695 break;
46696 case CTX_PROPERTY_EXPR:
46697 t = cp_parser_constant_expression (parser);
46698 if (t != error_mark_node)
46700 t = fold_non_dependent_expr (t);
46701 if (!value_dependent_expression_p (t)
46702 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
46703 || !tree_fits_shwi_p (t)))
46704 error_at (token->location, "property must be "
46705 "constant integer expression");
46706 else
46707 properties = tree_cons (NULL_TREE, t, properties);
46709 else
46710 return error_mark_node;
46711 break;
46712 case CTX_PROPERTY_SIMD:
46713 if (!has_parms_p)
46715 error_at (token->location, "properties for %<simd%> "
46716 "selector may not be specified in "
46717 "%<metadirective%>");
46718 return error_mark_node;
46720 properties
46721 = cp_parser_omp_all_clauses (parser,
46722 OMP_DECLARE_SIMD_CLAUSE_MASK,
46723 "simd", NULL, true, 2);
46724 break;
46725 default:
46726 gcc_unreachable ();
46729 if (!parens.require_close (parser))
46730 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
46732 properties = nreverse (properties);
46734 else if (property_kind == CTX_PROPERTY_NAME_LIST
46735 || property_kind == CTX_PROPERTY_ID
46736 || property_kind == CTX_PROPERTY_EXPR)
46738 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
46739 return error_mark_node;
46742 ret = tree_cons (selector, properties, ret);
46744 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46745 cp_lexer_consume_token (parser->lexer);
46746 else
46747 break;
46749 while (1);
46751 return nreverse (ret);
46754 /* OpenMP 5.0:
46756 trait-set-selector[,trait-set-selector[,...]]
46758 trait-set-selector:
46759 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
46761 trait-set-selector-name:
46762 constructor
46763 device
46764 implementation
46765 user */
46767 static tree
46768 cp_parser_omp_context_selector_specification (cp_parser *parser,
46769 bool has_parms_p)
46771 tree ret = NULL_TREE;
46774 const char *setp = "";
46775 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46776 setp
46777 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46778 switch (setp[0])
46780 case 'c':
46781 if (strcmp (setp, "construct") == 0)
46782 setp = NULL;
46783 break;
46784 case 'd':
46785 if (strcmp (setp, "device") == 0)
46786 setp = NULL;
46787 break;
46788 case 'i':
46789 if (strcmp (setp, "implementation") == 0)
46790 setp = NULL;
46791 break;
46792 case 'u':
46793 if (strcmp (setp, "user") == 0)
46794 setp = NULL;
46795 break;
46796 default:
46797 break;
46799 if (setp)
46801 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
46802 "%<implementation%> or %<user%>");
46803 return error_mark_node;
46806 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
46807 cp_lexer_consume_token (parser->lexer);
46809 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
46810 return error_mark_node;
46812 matching_braces braces;
46813 if (!braces.require_open (parser))
46814 return error_mark_node;
46816 tree selectors
46817 = cp_parser_omp_context_selector (parser, set, has_parms_p);
46818 if (selectors == error_mark_node)
46820 cp_parser_skip_to_closing_brace (parser);
46821 ret = error_mark_node;
46823 else if (ret != error_mark_node)
46824 ret = tree_cons (set, selectors, ret);
46826 braces.require_close (parser);
46828 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46829 cp_lexer_consume_token (parser->lexer);
46830 else
46831 break;
46833 while (1);
46835 if (ret == error_mark_node)
46836 return ret;
46837 return nreverse (ret);
46840 /* Assumption clauses:
46841 OpenMP 5.1
46842 absent (directive-name-list)
46843 contains (directive-name-list)
46844 holds (expression)
46845 no_openmp
46846 no_openmp_routines
46847 no_parallelism */
46849 static void
46850 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
46851 bool is_assume)
46853 bool no_openmp = false;
46854 bool no_openmp_routines = false;
46855 bool no_parallelism = false;
46856 bitmap_head absent_head, contains_head;
46858 bitmap_obstack_initialize (NULL);
46859 bitmap_initialize (&absent_head, &bitmap_default_obstack);
46860 bitmap_initialize (&contains_head, &bitmap_default_obstack);
46862 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
46863 error_at (cp_lexer_peek_token (parser->lexer)->location,
46864 "expected at least one assumption clause");
46866 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46868 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46869 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46870 cp_lexer_consume_token (parser->lexer);
46872 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46873 break;
46875 const char *p
46876 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46877 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46879 if (!strcmp (p, "no_openmp"))
46881 cp_lexer_consume_token (parser->lexer);
46882 if (no_openmp)
46883 error_at (cloc, "too many %qs clauses", "no_openmp");
46884 no_openmp = true;
46886 else if (!strcmp (p, "no_openmp_routines"))
46888 cp_lexer_consume_token (parser->lexer);
46889 if (no_openmp_routines)
46890 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
46891 no_openmp_routines = true;
46893 else if (!strcmp (p, "no_parallelism"))
46895 cp_lexer_consume_token (parser->lexer);
46896 if (no_parallelism)
46897 error_at (cloc, "too many %qs clauses", "no_parallelism");
46898 no_parallelism = true;
46900 else if (!strcmp (p, "holds"))
46902 cp_lexer_consume_token (parser->lexer);
46903 matching_parens parens;
46904 if (parens.require_open (parser))
46906 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
46907 tree t = cp_parser_assignment_expression (parser);
46908 if (!type_dependent_expression_p (t))
46909 t = contextual_conv_bool (t, tf_warning_or_error);
46910 if (is_assume && !error_operand_p (t))
46911 finish_expr_stmt (build_assume_call (eloc, t));
46912 if (!parens.require_close (parser))
46913 cp_parser_skip_to_closing_parenthesis (parser,
46914 /*recovering=*/true,
46915 /*or_comma=*/false,
46916 /*consume_paren=*/true);
46919 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
46921 cp_lexer_consume_token (parser->lexer);
46922 matching_parens parens;
46923 if (parens.require_open (parser))
46927 const char *directive[3] = {};
46928 int i;
46929 location_t dloc
46930 = cp_lexer_peek_token (parser->lexer)->location;
46931 for (i = 0; i < 3; i++)
46933 tree id;
46934 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
46935 id = cp_lexer_peek_nth_token (parser->lexer,
46936 i + 1)->u.value;
46937 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
46938 CPP_KEYWORD))
46940 enum rid rid
46941 = cp_lexer_peek_nth_token (parser->lexer,
46942 i + 1)->keyword;
46943 id = ridpointers[rid];
46945 else
46946 break;
46947 directive[i] = IDENTIFIER_POINTER (id);
46949 if (i == 0)
46950 error_at (dloc, "expected directive name");
46951 else
46953 const struct c_omp_directive *dir
46954 = c_omp_categorize_directive (directive[0],
46955 directive[1],
46956 directive[2]);
46957 if (dir == NULL
46958 || dir->kind == C_OMP_DIR_DECLARATIVE
46959 || dir->kind == C_OMP_DIR_INFORMATIONAL
46960 || dir->id == PRAGMA_OMP_END
46961 || (!dir->second && directive[1])
46962 || (!dir->third && directive[2]))
46963 error_at (dloc, "unknown OpenMP directive name in "
46964 "%qs clause argument", p);
46965 else
46967 int id = dir - c_omp_directives;
46968 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
46969 : &absent_head, id))
46970 error_at (dloc, "%<%s%s%s%s%s%> directive "
46971 "mentioned in both %<absent%> and "
46972 "%<contains%> clauses",
46973 directive[0],
46974 directive[1] ? " " : "",
46975 directive[1] ? directive[1] : "",
46976 directive[2] ? " " : "",
46977 directive[2] ? directive[2] : "");
46978 else if (!bitmap_set_bit (p[0] == 'a'
46979 ? &absent_head
46980 : &contains_head, id))
46981 error_at (dloc, "%<%s%s%s%s%s%> directive "
46982 "mentioned multiple times in %qs "
46983 "clauses",
46984 directive[0],
46985 directive[1] ? " " : "",
46986 directive[1] ? directive[1] : "",
46987 directive[2] ? " " : "",
46988 directive[2] ? directive[2] : "", p);
46990 for (; i; --i)
46991 cp_lexer_consume_token (parser->lexer);
46993 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46994 cp_lexer_consume_token (parser->lexer);
46995 else
46996 break;
46998 while (1);
46999 if (!parens.require_close (parser))
47000 cp_parser_skip_to_closing_parenthesis (parser,
47001 /*recovering=*/true,
47002 /*or_comma=*/false,
47003 /*consume_paren=*/true);
47006 else if (startswith (p, "ext_"))
47008 warning_at (cloc, 0, "unknown assumption clause %qs", p);
47009 cp_lexer_consume_token (parser->lexer);
47010 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47011 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
47012 n; --n)
47013 cp_lexer_consume_token (parser->lexer);
47015 else
47017 cp_lexer_consume_token (parser->lexer);
47018 error_at (cloc, "expected assumption clause");
47019 break;
47022 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47025 /* OpenMP 5.1
47026 # pragma omp assume clauses[optseq] new-line */
47028 static void
47029 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47031 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
47032 add_stmt (cp_parser_omp_structured_block (parser, if_p));
47035 /* OpenMP 5.1
47036 # pragma omp assumes clauses[optseq] new-line */
47038 static bool
47039 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
47041 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
47042 return false;
47045 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
47046 that into "omp declare variant base" attribute. */
47048 static tree
47049 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
47050 tree attrs)
47052 matching_parens parens;
47053 if (!parens.require_open (parser))
47055 fail:
47056 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47057 return attrs;
47060 bool template_p;
47061 cp_id_kind idk = CP_ID_KIND_NONE;
47062 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
47063 cp_expr varid
47064 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
47065 /*check_dependency_p=*/true,
47066 /*template_p=*/&template_p,
47067 /*declarator_p=*/false,
47068 /*optional_p=*/false);
47069 parens.require_close (parser);
47071 tree variant;
47072 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
47073 || TREE_CODE (varid) == TYPE_DECL
47074 || varid == error_mark_node)
47075 variant = varid;
47076 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
47077 variant = NULL_TREE;
47078 else
47080 tree ambiguous_decls;
47081 variant = cp_parser_lookup_name (parser, varid, none_type,
47082 template_p, /*is_namespace=*/false,
47083 /*check_dependency=*/true,
47084 &ambiguous_decls,
47085 varid.get_location ());
47086 if (ambiguous_decls)
47087 variant = NULL_TREE;
47089 if (variant == NULL_TREE)
47090 variant = error_mark_node;
47091 else if (TREE_CODE (variant) != SCOPE_REF)
47093 const char *error_msg;
47094 variant
47095 = finish_id_expression (varid, variant, parser->scope,
47096 &idk, false, true,
47097 &parser->non_integral_constant_expression_p,
47098 template_p, true, false, false, &error_msg,
47099 varid.get_location ());
47100 if (error_msg)
47101 cp_parser_error (parser, error_msg);
47103 location_t caret_loc = get_pure_location (varid.get_location ());
47104 location_t start_loc = get_start (varid_token->location);
47105 location_t finish_loc = get_finish (varid.get_location ());
47106 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
47108 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47109 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47110 cp_lexer_consume_token (parser->lexer);
47112 const char *clause = "";
47113 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
47114 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47115 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47116 if (strcmp (clause, "match"))
47118 cp_parser_error (parser, "expected %<match%>");
47119 goto fail;
47122 cp_lexer_consume_token (parser->lexer);
47124 if (!parens.require_open (parser))
47125 goto fail;
47127 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
47128 if (ctx == error_mark_node)
47129 goto fail;
47130 ctx = omp_check_context_selector (match_loc, ctx);
47131 if (ctx != error_mark_node && variant != error_mark_node)
47133 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
47134 match_loc);
47135 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
47136 loc_node = tree_cons (match_loc_node,
47137 build_int_cst (integer_type_node, idk),
47138 build_tree_list (loc_node, integer_zero_node));
47139 attrs = tree_cons (get_identifier ("omp declare variant base"),
47140 tree_cons (variant, ctx, loc_node), attrs);
47141 if (processing_template_decl)
47142 ATTR_IS_DEPENDENT (attrs) = 1;
47145 parens.require_close (parser);
47146 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47147 return attrs;
47151 /* Finalize #pragma omp declare simd clauses after direct declarator has
47152 been parsed, and put that into "omp declare simd" attribute. */
47154 static tree
47155 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
47157 struct cp_token_cache *ce;
47158 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
47159 int i;
47161 if (!data->error_seen && data->fndecl_seen)
47163 error ("%<#pragma omp declare %s%> not immediately followed by "
47164 "a single function declaration or definition",
47165 data->variant_p ? "variant" : "simd");
47166 data->error_seen = true;
47168 if (data->error_seen)
47169 return attrs;
47171 FOR_EACH_VEC_ELT (data->tokens, i, ce)
47173 tree c, cl;
47175 cp_parser_push_lexer_for_tokens (parser, ce);
47176 parser->lexer->in_pragma = true;
47177 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
47178 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47179 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47180 const char *kind = IDENTIFIER_POINTER (id);
47181 cp_lexer_consume_token (parser->lexer);
47182 if (strcmp (kind, "simd") == 0)
47184 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
47185 "#pragma omp declare simd",
47186 pragma_tok);
47187 if (cl)
47188 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
47189 c = build_tree_list (get_identifier ("omp declare simd"), cl);
47190 TREE_CHAIN (c) = attrs;
47191 if (processing_template_decl)
47192 ATTR_IS_DEPENDENT (c) = 1;
47193 attrs = c;
47195 else
47197 gcc_assert (strcmp (kind, "variant") == 0);
47198 attrs
47199 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
47201 cp_parser_pop_lexer (parser);
47204 cp_lexer *lexer = NULL;
47205 for (int i = 0; i < 2; i++)
47207 if (data->attribs[i] == NULL)
47208 continue;
47209 for (tree *pa = data->attribs[i]; *pa; )
47210 if (get_attribute_namespace (*pa) == omp_identifier
47211 && is_attribute_p ("directive", get_attribute_name (*pa)))
47213 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
47215 tree d = TREE_VALUE (a);
47216 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
47217 cp_token *first = DEFPARSE_TOKENS (d)->first;
47218 cp_token *last = DEFPARSE_TOKENS (d)->last;
47219 const char *directive[3] = {};
47220 for (int j = 0; j < 3; j++)
47222 tree id = NULL_TREE;
47223 if (first + j == last)
47224 break;
47225 if (first[j].type == CPP_NAME)
47226 id = first[j].u.value;
47227 else if (first[j].type == CPP_KEYWORD)
47228 id = ridpointers[(int) first[j].keyword];
47229 else
47230 break;
47231 directive[j] = IDENTIFIER_POINTER (id);
47233 const c_omp_directive *dir = NULL;
47234 if (directive[0])
47235 dir = c_omp_categorize_directive (directive[0], directive[1],
47236 directive[2]);
47237 if (dir == NULL)
47239 error_at (first->location,
47240 "unknown OpenMP directive name in "
47241 "%<omp::directive%> attribute argument");
47242 continue;
47244 if (dir->id != PRAGMA_OMP_DECLARE
47245 || (strcmp (directive[1], "simd") != 0
47246 && strcmp (directive[1], "variant") != 0))
47248 error_at (first->location,
47249 "OpenMP directive other than %<declare simd%> "
47250 "or %<declare variant%> appertains to a "
47251 "declaration");
47252 continue;
47255 if (parser->omp_attrs_forbidden_p)
47257 error_at (first->location,
47258 "mixing OpenMP directives with attribute and "
47259 "pragma syntax on the same statement");
47260 parser->omp_attrs_forbidden_p = false;
47263 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
47264 continue;
47265 if (lexer == NULL)
47267 lexer = cp_lexer_alloc ();
47268 lexer->debugging_p = parser->lexer->debugging_p;
47270 vec_safe_reserve (lexer->buffer, (last - first) + 2);
47271 cp_token tok = {};
47272 tok.type = CPP_PRAGMA;
47273 tok.keyword = RID_MAX;
47274 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
47275 tok.location = first->location;
47276 lexer->buffer->quick_push (tok);
47277 while (++first < last)
47278 lexer->buffer->quick_push (*first);
47279 tok = {};
47280 tok.type = CPP_PRAGMA_EOL;
47281 tok.keyword = RID_MAX;
47282 tok.location = last->location;
47283 lexer->buffer->quick_push (tok);
47284 tok = {};
47285 tok.type = CPP_EOF;
47286 tok.keyword = RID_MAX;
47287 tok.location = last->location;
47288 lexer->buffer->quick_push (tok);
47289 lexer->next = parser->lexer;
47290 lexer->next_token = lexer->buffer->address ();
47291 lexer->last_token = lexer->next_token
47292 + lexer->buffer->length ()
47293 - 1;
47294 lexer->in_omp_attribute_pragma = true;
47295 parser->lexer = lexer;
47296 /* Move the current source position to that of the first token
47297 in the new lexer. */
47298 cp_lexer_set_source_position_from_token (lexer->next_token);
47300 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47301 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47302 const char *kind = IDENTIFIER_POINTER (id);
47303 cp_lexer_consume_token (parser->lexer);
47305 tree c, cl;
47306 if (strcmp (kind, "simd") == 0)
47308 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47309 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47310 cp_lexer_consume_token (parser->lexer);
47312 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
47313 cl = cp_parser_omp_all_clauses (parser, mask,
47314 "#pragma omp declare simd",
47315 pragma_tok);
47316 if (cl)
47317 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
47318 c = build_tree_list (get_identifier ("omp declare simd"),
47319 cl);
47320 TREE_CHAIN (c) = attrs;
47321 if (processing_template_decl)
47322 ATTR_IS_DEPENDENT (c) = 1;
47323 attrs = c;
47325 else
47327 gcc_assert (strcmp (kind, "variant") == 0);
47328 attrs
47329 = cp_finish_omp_declare_variant (parser, pragma_tok,
47330 attrs);
47332 gcc_assert (parser->lexer != lexer);
47333 vec_safe_truncate (lexer->buffer, 0);
47335 *pa = TREE_CHAIN (*pa);
47337 else
47338 pa = &TREE_CHAIN (*pa);
47340 if (lexer)
47341 cp_lexer_destroy (lexer);
47343 data->fndecl_seen = true;
47344 return attrs;
47347 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
47348 on #pragma omp declare target. Return false if errors were reported. */
47350 static bool
47351 handle_omp_declare_target_clause (tree c, tree t, int device_type)
47353 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
47354 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
47355 tree id;
47356 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
47358 id = get_identifier ("omp declare target link");
47359 std::swap (at1, at2);
47361 else
47362 id = get_identifier ("omp declare target");
47363 if (at2)
47365 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
47366 error_at (OMP_CLAUSE_LOCATION (c),
47367 "%qD specified both in declare target %<link%> and %qs"
47368 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
47369 else
47370 error_at (OMP_CLAUSE_LOCATION (c),
47371 "%qD specified both in declare target %<link%> and "
47372 "%<to%> or %<enter%> clauses", t);
47373 return false;
47375 if (!at1)
47377 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
47378 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
47379 return true;
47381 symtab_node *node = symtab_node::get (t);
47382 if (node != NULL)
47384 node->offloadable = 1;
47385 if (ENABLE_OFFLOADING)
47387 g->have_offload = true;
47388 if (is_a <varpool_node *> (node))
47389 vec_safe_push (offload_vars, t);
47393 if (TREE_CODE (t) != FUNCTION_DECL)
47394 return true;
47395 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
47397 tree at3 = lookup_attribute ("omp declare target host",
47398 DECL_ATTRIBUTES (t));
47399 if (at3 == NULL_TREE)
47401 id = get_identifier ("omp declare target host");
47402 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
47405 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
47407 tree at3 = lookup_attribute ("omp declare target nohost",
47408 DECL_ATTRIBUTES (t));
47409 if (at3 == NULL_TREE)
47411 id = get_identifier ("omp declare target nohost");
47412 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
47415 return true;
47418 /* OpenMP 4.0:
47419 # pragma omp declare target new-line
47420 declarations and definitions
47421 # pragma omp end declare target new-line
47423 OpenMP 4.5:
47424 # pragma omp declare target ( extended-list ) new-line
47426 # pragma omp declare target declare-target-clauses[seq] new-line */
47428 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
47429 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
47430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
47431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
47432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
47434 static void
47435 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
47437 tree clauses = NULL_TREE;
47438 int device_type = 0;
47439 bool only_device_type = true;
47440 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
47441 || (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47442 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
47443 clauses
47444 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
47445 "#pragma omp declare target", pragma_tok);
47446 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47448 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
47449 clauses);
47450 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
47451 cp_parser_require_pragma_eol (parser, pragma_tok);
47453 else
47455 cp_omp_declare_target_attr a
47456 = { parser->lexer->in_omp_attribute_pragma, -1 };
47457 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
47458 cp_parser_require_pragma_eol (parser, pragma_tok);
47459 return;
47461 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47462 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
47463 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
47464 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47466 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
47467 continue;
47468 tree t = OMP_CLAUSE_DECL (c);
47469 only_device_type = false;
47470 if (!handle_omp_declare_target_clause (c, t, device_type))
47471 continue;
47472 if (VAR_OR_FUNCTION_DECL_P (t)
47473 && DECL_LOCAL_DECL_P (t)
47474 && DECL_LANG_SPECIFIC (t)
47475 && DECL_LOCAL_DECL_ALIAS (t)
47476 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
47477 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
47478 device_type);
47480 if (device_type && only_device_type)
47481 error_at (OMP_CLAUSE_LOCATION (clauses),
47482 "directive with only %<device_type%> clause");
47485 /* OpenMP 5.1
47486 # pragma omp begin assumes clauses[optseq] new-line
47488 # pragma omp begin declare target clauses[optseq] new-line */
47490 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
47491 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE)
47493 static void
47494 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
47496 const char *p = "";
47497 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
47498 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47500 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47501 p = IDENTIFIER_POINTER (id);
47503 if (strcmp (p, "declare") == 0)
47505 cp_lexer_consume_token (parser->lexer);
47506 p = "";
47507 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47509 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47510 p = IDENTIFIER_POINTER (id);
47512 if (strcmp (p, "target") == 0)
47514 cp_lexer_consume_token (parser->lexer);
47515 tree clauses
47516 = cp_parser_omp_all_clauses (parser,
47517 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
47518 "#pragma omp begin declare target",
47519 pragma_tok);
47520 int device_type = 0;
47521 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47522 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
47523 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
47524 cp_omp_declare_target_attr a
47525 = { in_omp_attribute_pragma, device_type };
47526 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
47528 else
47530 cp_parser_error (parser, "expected %<target%>");
47531 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47534 else if (strcmp (p, "assumes") == 0)
47536 cp_lexer_consume_token (parser->lexer);
47537 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
47538 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
47539 vec_safe_push (scope_chain->omp_begin_assumes, a);
47541 else
47543 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
47544 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47548 /* OpenMP 4.0:
47549 # pragma omp end declare target new-line
47551 OpenMP 5.1:
47552 # pragma omp end assumes new-line */
47554 static void
47555 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
47557 const char *p = "";
47558 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
47559 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47561 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47562 p = IDENTIFIER_POINTER (id);
47564 if (strcmp (p, "declare") == 0)
47566 cp_lexer_consume_token (parser->lexer);
47567 p = "";
47568 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47570 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47571 p = IDENTIFIER_POINTER (id);
47573 if (strcmp (p, "target") == 0)
47574 cp_lexer_consume_token (parser->lexer);
47575 else
47577 cp_parser_error (parser, "expected %<target%>");
47578 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47579 return;
47581 cp_parser_require_pragma_eol (parser, pragma_tok);
47582 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
47583 error_at (pragma_tok->location,
47584 "%<#pragma omp end declare target%> without corresponding "
47585 "%<#pragma omp declare target%> or "
47586 "%<#pragma omp begin declare target%>");
47587 else
47589 cp_omp_declare_target_attr
47590 a = scope_chain->omp_declare_target_attribute->pop ();
47591 if (a.attr_syntax != in_omp_attribute_pragma)
47593 if (a.attr_syntax)
47594 error_at (pragma_tok->location,
47595 "%qs in attribute syntax terminated "
47596 "with %qs in pragma syntax",
47597 a.device_type >= 0 ? "begin declare target"
47598 : "declare target",
47599 "end declare target");
47600 else
47601 error_at (pragma_tok->location,
47602 "%qs in pragma syntax terminated "
47603 "with %qs in attribute syntax",
47604 a.device_type >= 0 ? "begin declare target"
47605 : "declare target",
47606 "end declare target");
47610 else if (strcmp (p, "assumes") == 0)
47612 cp_lexer_consume_token (parser->lexer);
47613 cp_parser_require_pragma_eol (parser, pragma_tok);
47614 if (!vec_safe_length (scope_chain->omp_begin_assumes))
47615 error_at (pragma_tok->location,
47616 "%qs without corresponding %qs",
47617 "#pragma omp end assumes", "#pragma omp begin assumes");
47618 else
47620 cp_omp_begin_assumes_data
47621 a = scope_chain->omp_begin_assumes->pop ();
47622 if (a.attr_syntax != in_omp_attribute_pragma)
47624 if (a.attr_syntax)
47625 error_at (pragma_tok->location,
47626 "%qs in attribute syntax terminated "
47627 "with %qs in pragma syntax",
47628 "begin assumes", "end assumes");
47629 else
47630 error_at (pragma_tok->location,
47631 "%qs in pragma syntax terminated "
47632 "with %qs in attribute syntax",
47633 "begin assumes", "end assumes");
47637 else
47639 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
47640 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47641 return;
47645 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
47646 expression and optional initializer clause of
47647 #pragma omp declare reduction. We store the expression(s) as
47648 either 3, 6 or 7 special statements inside of the artificial function's
47649 body. The first two statements are DECL_EXPRs for the artificial
47650 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
47651 expression that uses those variables.
47652 If there was any INITIALIZER clause, this is followed by further statements,
47653 the fourth and fifth statements are DECL_EXPRs for the artificial
47654 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
47655 constructor variant (first token after open paren is not omp_priv),
47656 then the sixth statement is a statement with the function call expression
47657 that uses the OMP_PRIV and optionally OMP_ORIG variable.
47658 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
47659 to initialize the OMP_PRIV artificial variable and there is seventh
47660 statement, a DECL_EXPR of the OMP_PRIV statement again. */
47662 static bool
47663 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
47665 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
47666 gcc_assert (TYPE_REF_P (type));
47667 type = TREE_TYPE (type);
47668 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
47669 DECL_ARTIFICIAL (omp_out) = 1;
47670 pushdecl (omp_out);
47671 add_decl_expr (omp_out);
47672 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
47673 DECL_ARTIFICIAL (omp_in) = 1;
47674 pushdecl (omp_in);
47675 add_decl_expr (omp_in);
47676 tree combiner;
47677 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
47679 keep_next_level (true);
47680 tree block = begin_omp_structured_block ();
47681 combiner = cp_parser_expression (parser);
47682 finish_expr_stmt (combiner);
47683 block = finish_omp_structured_block (block);
47684 if (processing_template_decl)
47685 block = build_stmt (input_location, EXPR_STMT, block);
47686 add_stmt (block);
47688 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
47689 return false;
47691 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47692 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47693 cp_lexer_consume_token (parser->lexer);
47695 const char *p = "";
47696 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47698 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47699 p = IDENTIFIER_POINTER (id);
47702 if (strcmp (p, "initializer") == 0)
47704 cp_lexer_consume_token (parser->lexer);
47705 matching_parens parens;
47706 if (!parens.require_open (parser))
47707 return false;
47709 p = "";
47710 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47712 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47713 p = IDENTIFIER_POINTER (id);
47716 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
47717 DECL_ARTIFICIAL (omp_priv) = 1;
47718 pushdecl (omp_priv);
47719 add_decl_expr (omp_priv);
47720 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
47721 DECL_ARTIFICIAL (omp_orig) = 1;
47722 pushdecl (omp_orig);
47723 add_decl_expr (omp_orig);
47725 keep_next_level (true);
47726 block = begin_omp_structured_block ();
47728 bool ctor = false;
47729 if (strcmp (p, "omp_priv") == 0)
47731 bool is_direct_init, is_non_constant_init;
47732 ctor = true;
47733 cp_lexer_consume_token (parser->lexer);
47734 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
47735 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
47736 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
47737 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
47738 == CPP_CLOSE_PAREN
47739 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
47740 == CPP_CLOSE_PAREN))
47742 finish_omp_structured_block (block);
47743 error ("invalid initializer clause");
47744 return false;
47746 initializer = cp_parser_initializer (parser, &is_direct_init,
47747 &is_non_constant_init);
47748 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
47749 NULL_TREE, LOOKUP_ONLYCONVERTING);
47751 else
47753 cp_parser_parse_tentatively (parser);
47754 /* Don't create location wrapper nodes here. */
47755 auto_suppress_location_wrappers sentinel;
47756 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
47757 /*check_dependency_p=*/true,
47758 /*template_p=*/NULL,
47759 /*declarator_p=*/false,
47760 /*optional_p=*/false);
47761 vec<tree, va_gc> *args;
47762 if (fn_name == error_mark_node
47763 || cp_parser_error_occurred (parser)
47764 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
47765 || ((args = cp_parser_parenthesized_expression_list
47766 (parser, non_attr, /*cast_p=*/false,
47767 /*allow_expansion_p=*/true,
47768 /*non_constant_p=*/NULL)),
47769 cp_parser_error_occurred (parser)))
47771 finish_omp_structured_block (block);
47772 cp_parser_abort_tentative_parse (parser);
47773 cp_parser_error (parser, "expected id-expression (arguments)");
47774 return false;
47776 unsigned int i;
47777 tree arg;
47778 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
47779 if (arg == omp_priv
47780 || (TREE_CODE (arg) == ADDR_EXPR
47781 && TREE_OPERAND (arg, 0) == omp_priv))
47782 break;
47783 cp_parser_abort_tentative_parse (parser);
47784 if (arg == NULL_TREE)
47785 error ("one of the initializer call arguments should be %<omp_priv%>"
47786 " or %<&omp_priv%>");
47787 initializer = cp_parser_postfix_expression (parser, false, false, false,
47788 false, NULL);
47789 finish_expr_stmt (initializer);
47792 block = finish_omp_structured_block (block);
47793 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
47794 if (processing_template_decl)
47795 block = build_stmt (input_location, EXPR_STMT, block);
47796 add_stmt (block);
47798 if (ctor)
47799 add_decl_expr (omp_orig);
47801 if (!parens.require_close (parser))
47802 return false;
47805 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
47806 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
47807 UNKNOWN_LOCATION);
47809 return true;
47812 /* OpenMP 4.0
47813 #pragma omp declare reduction (reduction-id : typename-list : expression) \
47814 initializer-clause[opt] new-line
47816 initializer-clause:
47817 initializer (omp_priv initializer)
47818 initializer (function-name (argument-list)) */
47820 static void
47821 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
47822 enum pragma_context)
47824 auto_vec<tree> types;
47825 enum tree_code reduc_code = ERROR_MARK;
47826 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
47827 unsigned int i;
47828 cp_token *first_token;
47829 cp_token_cache *cp;
47830 int errs;
47831 void *p;
47833 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
47834 p = obstack_alloc (&declarator_obstack, 0);
47836 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
47837 goto fail;
47839 switch (cp_lexer_peek_token (parser->lexer)->type)
47841 case CPP_PLUS:
47842 reduc_code = PLUS_EXPR;
47843 break;
47844 case CPP_MULT:
47845 reduc_code = MULT_EXPR;
47846 break;
47847 case CPP_MINUS:
47848 reduc_code = MINUS_EXPR;
47849 break;
47850 case CPP_AND:
47851 reduc_code = BIT_AND_EXPR;
47852 break;
47853 case CPP_XOR:
47854 reduc_code = BIT_XOR_EXPR;
47855 break;
47856 case CPP_OR:
47857 reduc_code = BIT_IOR_EXPR;
47858 break;
47859 case CPP_AND_AND:
47860 reduc_code = TRUTH_ANDIF_EXPR;
47861 break;
47862 case CPP_OR_OR:
47863 reduc_code = TRUTH_ORIF_EXPR;
47864 break;
47865 case CPP_NAME:
47866 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
47867 break;
47868 default:
47869 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
47870 "%<|%>, %<&&%>, %<||%> or identifier");
47871 goto fail;
47874 if (reduc_code != ERROR_MARK)
47875 cp_lexer_consume_token (parser->lexer);
47877 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
47878 if (reduc_id == error_mark_node)
47879 goto fail;
47881 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
47882 goto fail;
47884 /* Types may not be defined in declare reduction type list. */
47885 const char *saved_message;
47886 saved_message = parser->type_definition_forbidden_message;
47887 parser->type_definition_forbidden_message
47888 = G_("types may not be defined in declare reduction type list");
47889 bool saved_colon_corrects_to_scope_p;
47890 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
47891 parser->colon_corrects_to_scope_p = false;
47892 bool saved_colon_doesnt_start_class_def_p;
47893 saved_colon_doesnt_start_class_def_p
47894 = parser->colon_doesnt_start_class_def_p;
47895 parser->colon_doesnt_start_class_def_p = true;
47897 while (true)
47899 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47900 type = cp_parser_type_id (parser);
47901 if (type == error_mark_node)
47903 else if (ARITHMETIC_TYPE_P (type)
47904 && (orig_reduc_id == NULL_TREE
47905 || (TREE_CODE (type) != COMPLEX_TYPE
47906 && (id_equal (orig_reduc_id, "min")
47907 || id_equal (orig_reduc_id, "max")))))
47908 error_at (loc, "predeclared arithmetic type %qT in "
47909 "%<#pragma omp declare reduction%>", type);
47910 else if (FUNC_OR_METHOD_TYPE_P (type)
47911 || TREE_CODE (type) == ARRAY_TYPE)
47912 error_at (loc, "function or array type %qT in "
47913 "%<#pragma omp declare reduction%>", type);
47914 else if (TYPE_REF_P (type))
47915 error_at (loc, "reference type %qT in "
47916 "%<#pragma omp declare reduction%>", type);
47917 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
47918 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
47919 "type %qT in %<#pragma omp declare reduction%>", type);
47920 else
47921 types.safe_push (type);
47923 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47924 cp_lexer_consume_token (parser->lexer);
47925 else
47926 break;
47929 /* Restore the saved message. */
47930 parser->type_definition_forbidden_message = saved_message;
47931 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
47932 parser->colon_doesnt_start_class_def_p
47933 = saved_colon_doesnt_start_class_def_p;
47935 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
47936 || types.is_empty ())
47938 fail:
47939 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47940 goto done;
47943 first_token = cp_lexer_peek_token (parser->lexer);
47944 cp = NULL;
47945 errs = errorcount;
47946 FOR_EACH_VEC_ELT (types, i, type)
47948 tree fntype
47949 = build_function_type_list (void_type_node,
47950 cp_build_reference_type (type, false),
47951 NULL_TREE);
47952 tree this_reduc_id = reduc_id;
47953 if (!dependent_type_p (type))
47954 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
47955 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
47956 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
47957 DECL_ARTIFICIAL (fndecl) = 1;
47958 DECL_EXTERNAL (fndecl) = 1;
47959 DECL_DECLARED_INLINE_P (fndecl) = 1;
47960 DECL_IGNORED_P (fndecl) = 1;
47961 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
47962 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
47963 DECL_ATTRIBUTES (fndecl)
47964 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
47965 DECL_ATTRIBUTES (fndecl));
47966 bool block_scope = false;
47967 if (current_function_decl)
47969 block_scope = true;
47970 DECL_CONTEXT (fndecl) = current_function_decl;
47971 DECL_LOCAL_DECL_P (fndecl) = true;
47974 if (processing_template_decl)
47975 fndecl = push_template_decl (fndecl);
47977 if (block_scope)
47979 if (!processing_template_decl)
47980 pushdecl (fndecl);
47982 else if (current_class_type)
47984 if (cp == NULL)
47986 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47987 cp_lexer_consume_token (parser->lexer);
47988 cp = cp_token_cache_new (first_token,
47989 cp_lexer_peek_nth_token (parser->lexer,
47990 2));
47992 DECL_STATIC_FUNCTION_P (fndecl) = 1;
47993 finish_member_declaration (fndecl);
47994 DECL_PENDING_INLINE_INFO (fndecl) = cp;
47995 DECL_PENDING_INLINE_P (fndecl) = 1;
47996 vec_safe_push (unparsed_funs_with_definitions, fndecl);
47997 continue;
47999 else
48001 DECL_CONTEXT (fndecl) = current_namespace;
48002 tree d = pushdecl (fndecl);
48003 /* We should never meet a matched duplicate decl. */
48004 gcc_checking_assert (d == error_mark_node || d == fndecl);
48007 tree block = NULL_TREE;
48008 if (!block_scope)
48009 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
48010 else
48011 block = begin_omp_structured_block ();
48012 if (cp)
48014 cp_parser_push_lexer_for_tokens (parser, cp);
48015 parser->lexer->in_pragma = true;
48018 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
48020 if (cp)
48021 cp_parser_pop_lexer (parser);
48022 if (!block_scope)
48023 finish_function (/*inline_p=*/false);
48024 else
48026 DECL_CONTEXT (fndecl) = current_function_decl;
48027 if (DECL_TEMPLATE_INFO (fndecl))
48028 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
48030 if (!ok)
48031 goto fail;
48033 if (block_scope)
48035 block = finish_omp_structured_block (block);
48036 if (TREE_CODE (block) == BIND_EXPR)
48037 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
48038 else if (TREE_CODE (block) == STATEMENT_LIST)
48039 DECL_SAVED_TREE (fndecl) = block;
48040 if (processing_template_decl)
48041 add_decl_expr (fndecl);
48044 cp_check_omp_declare_reduction (fndecl);
48045 if (cp == NULL && types.length () > 1)
48046 cp = cp_token_cache_new (first_token,
48047 cp_lexer_peek_nth_token (parser->lexer, 2));
48048 if (errs != errorcount)
48049 break;
48052 cp_parser_require_pragma_eol (parser, pragma_tok);
48054 done:
48055 /* Free any declarators allocated. */
48056 obstack_free (&declarator_obstack, p);
48059 /* OpenMP 4.0
48060 #pragma omp declare simd declare-simd-clauses[optseq] new-line
48061 #pragma omp declare reduction (reduction-id : typename-list : expression) \
48062 initializer-clause[opt] new-line
48063 #pragma omp declare target new-line
48065 OpenMP 5.0
48066 #pragma omp declare variant (identifier) match (context-selector) */
48068 static bool
48069 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
48070 enum pragma_context context)
48072 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48074 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48075 const char *p = IDENTIFIER_POINTER (id);
48077 if (strcmp (p, "simd") == 0)
48079 cp_lexer_consume_token (parser->lexer);
48080 cp_parser_omp_declare_simd (parser, pragma_tok,
48081 context, false);
48082 return true;
48084 if (flag_openmp && strcmp (p, "variant") == 0)
48086 cp_lexer_consume_token (parser->lexer);
48087 cp_parser_omp_declare_simd (parser, pragma_tok,
48088 context, true);
48089 return true;
48091 cp_ensure_no_omp_declare_simd (parser);
48092 if (strcmp (p, "reduction") == 0)
48094 cp_lexer_consume_token (parser->lexer);
48095 cp_parser_omp_declare_reduction (parser, pragma_tok,
48096 context);
48097 return false;
48099 if (!flag_openmp) /* flag_openmp_simd */
48101 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48102 return false;
48104 if (strcmp (p, "target") == 0)
48106 cp_lexer_consume_token (parser->lexer);
48107 cp_parser_omp_declare_target (parser, pragma_tok);
48108 return false;
48111 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
48112 "%<target%> or %<variant%>");
48113 cp_parser_require_pragma_eol (parser, pragma_tok);
48114 return false;
48117 /* OpenMP 5.0
48118 #pragma omp requires clauses[optseq] new-line */
48120 static bool
48121 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
48123 enum omp_requires new_req = (enum omp_requires) 0;
48125 location_t loc = pragma_tok->location;
48126 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48128 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48129 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48130 cp_lexer_consume_token (parser->lexer);
48132 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48134 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48135 const char *p = IDENTIFIER_POINTER (id);
48136 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48137 enum omp_requires this_req = (enum omp_requires) 0;
48139 if (!strcmp (p, "unified_address"))
48140 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
48141 else if (!strcmp (p, "unified_shared_memory"))
48142 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
48143 else if (!strcmp (p, "dynamic_allocators"))
48144 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
48145 else if (!strcmp (p, "reverse_offload"))
48146 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
48147 else if (!strcmp (p, "atomic_default_mem_order"))
48149 cp_lexer_consume_token (parser->lexer);
48151 matching_parens parens;
48152 if (parens.require_open (parser))
48154 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48156 id = cp_lexer_peek_token (parser->lexer)->u.value;
48157 p = IDENTIFIER_POINTER (id);
48159 if (!strcmp (p, "seq_cst"))
48160 this_req
48161 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
48162 else if (!strcmp (p, "relaxed"))
48163 this_req
48164 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
48165 else if (!strcmp (p, "acq_rel"))
48166 this_req
48167 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
48169 if (this_req == 0)
48171 error_at (cp_lexer_peek_token (parser->lexer)->location,
48172 "expected %<seq_cst%>, %<relaxed%> or "
48173 "%<acq_rel%>");
48174 switch (cp_lexer_peek_token (parser->lexer)->type)
48176 case CPP_EOF:
48177 case CPP_PRAGMA_EOL:
48178 case CPP_CLOSE_PAREN:
48179 break;
48180 default:
48181 if (cp_lexer_nth_token_is (parser->lexer, 2,
48182 CPP_CLOSE_PAREN))
48183 cp_lexer_consume_token (parser->lexer);
48184 break;
48187 else
48188 cp_lexer_consume_token (parser->lexer);
48190 if (!parens.require_close (parser))
48191 cp_parser_skip_to_closing_parenthesis (parser,
48192 /*recovering=*/true,
48193 /*or_comma=*/false,
48194 /*consume_paren=*/
48195 true);
48197 if (this_req == 0)
48199 cp_parser_require_pragma_eol (parser, pragma_tok);
48200 return false;
48203 p = NULL;
48205 else
48207 error_at (cloc, "expected %<unified_address%>, "
48208 "%<unified_shared_memory%>, "
48209 "%<dynamic_allocators%>, "
48210 "%<reverse_offload%> "
48211 "or %<atomic_default_mem_order%> clause");
48212 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48213 return false;
48215 if (p)
48216 cp_lexer_consume_token (parser->lexer);
48217 if (this_req)
48219 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48221 if ((this_req & new_req) != 0)
48222 error_at (cloc, "too many %qs clauses", p);
48223 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
48224 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
48225 error_at (cloc, "%qs clause used lexically after first "
48226 "target construct or offloading API", p);
48228 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48230 error_at (cloc, "too many %qs clauses",
48231 "atomic_default_mem_order");
48232 this_req = (enum omp_requires) 0;
48234 else if ((omp_requires_mask
48235 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48237 error_at (cloc, "more than one %<atomic_default_mem_order%>"
48238 " clause in a single compilation unit");
48239 this_req
48240 = (enum omp_requires)
48241 (omp_requires_mask
48242 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
48244 else if ((omp_requires_mask
48245 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
48246 error_at (cloc, "%<atomic_default_mem_order%> clause used "
48247 "lexically after first %<atomic%> construct "
48248 "without memory order clause");
48249 new_req = (enum omp_requires) (new_req | this_req);
48250 omp_requires_mask
48251 = (enum omp_requires) (omp_requires_mask | this_req);
48252 continue;
48255 break;
48257 cp_parser_require_pragma_eol (parser, pragma_tok);
48259 if (new_req == 0)
48260 error_at (loc, "%<pragma omp requires%> requires at least one clause");
48261 return false;
48265 /* OpenMP 5.1:
48266 #pragma omp nothing new-line */
48268 static void
48269 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
48271 cp_parser_require_pragma_eol (parser, pragma_tok);
48275 /* OpenMP 5.1
48276 #pragma omp error clauses[optseq] new-line */
48278 static bool
48279 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
48280 enum pragma_context context)
48282 int at_compilation = -1;
48283 int severity_fatal = -1;
48284 tree message = NULL_TREE;
48285 bool bad = false;
48286 location_t loc = pragma_tok->location;
48288 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48290 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48291 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48292 cp_lexer_consume_token (parser->lexer);
48294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
48295 break;
48297 const char *p
48298 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48299 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48300 static const char *args[] = {
48301 "execution", "compilation", "warning", "fatal"
48303 int *v = NULL;
48304 int idx = 0, n = -1;
48305 tree m = NULL_TREE;
48307 if (!strcmp (p, "at"))
48308 v = &at_compilation;
48309 else if (!strcmp (p, "severity"))
48311 v = &severity_fatal;
48312 idx += 2;
48314 else if (strcmp (p, "message"))
48316 error_at (cloc,
48317 "expected %<at%>, %<severity%> or %<message%> clause");
48318 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48319 return false;
48322 cp_lexer_consume_token (parser->lexer);
48324 matching_parens parens;
48325 if (parens.require_open (parser))
48327 if (v == NULL)
48329 m = cp_parser_assignment_expression (parser);
48330 if (type_dependent_expression_p (m))
48331 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
48332 else
48333 m = perform_implicit_conversion_flags (const_string_type_node, m,
48334 tf_warning_or_error,
48335 LOOKUP_NORMAL);
48337 else
48339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48341 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
48342 const char *q = IDENTIFIER_POINTER (val);
48344 if (!strcmp (q, args[idx]))
48345 n = 0;
48346 else if (!strcmp (q, args[idx + 1]))
48347 n = 1;
48349 if (n == -1)
48351 error_at (cp_lexer_peek_token (parser->lexer)->location,
48352 "expected %qs or %qs", args[idx], args[idx + 1]);
48353 bad = true;
48354 switch (cp_lexer_peek_token (parser->lexer)->type)
48356 case CPP_EOF:
48357 case CPP_PRAGMA_EOL:
48358 case CPP_CLOSE_PAREN:
48359 break;
48360 default:
48361 if (cp_lexer_nth_token_is (parser->lexer, 2,
48362 CPP_CLOSE_PAREN))
48363 cp_lexer_consume_token (parser->lexer);
48364 break;
48367 else
48368 cp_lexer_consume_token (parser->lexer);
48371 if (!parens.require_close (parser))
48372 cp_parser_skip_to_closing_parenthesis (parser,
48373 /*recovering=*/true,
48374 /*or_comma=*/false,
48375 /*consume_paren=*/
48376 true);
48378 if (v == NULL)
48380 if (message)
48382 error_at (cloc, "too many %qs clauses", p);
48383 bad = true;
48385 else
48386 message = m;
48388 else if (n != -1)
48390 if (*v != -1)
48392 error_at (cloc, "too many %qs clauses", p);
48393 bad = true;
48395 else
48396 *v = n;
48399 else
48400 bad = true;
48402 cp_parser_require_pragma_eol (parser, pragma_tok);
48403 if (bad)
48404 return true;
48406 if (at_compilation == -1)
48407 at_compilation = 1;
48408 if (severity_fatal == -1)
48409 severity_fatal = 1;
48410 if (!at_compilation)
48412 if (context != pragma_compound)
48414 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
48415 "may only be used in compound statements");
48416 return true;
48418 tree fndecl
48419 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
48420 : BUILT_IN_GOMP_WARNING);
48421 if (!message)
48422 message = build_zero_cst (const_string_type_node);
48423 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
48424 build_all_ones_cst (size_type_node));
48425 add_stmt (stmt);
48426 return true;
48429 if (in_discarded_stmt)
48430 return false;
48432 const char *msg = NULL;
48433 if (message)
48435 msg = c_getstr (fold_for_warn (message));
48436 if (msg == NULL)
48437 msg = _("<message unknown at compile time>");
48439 if (msg)
48440 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
48441 "%<pragma omp error%> encountered: %s", msg);
48442 else
48443 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
48444 "%<pragma omp error%> encountered");
48445 return false;
48448 /* OpenMP 4.5:
48449 #pragma omp taskloop taskloop-clause[optseq] new-line
48450 for-loop
48452 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
48453 for-loop */
48455 #define OMP_TASKLOOP_CLAUSE_MASK \
48456 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
48457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
48458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
48459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
48460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
48461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
48462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
48463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
48464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
48465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
48466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
48467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
48468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
48469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
48470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
48471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
48472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
48474 static tree
48475 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
48476 char *p_name, omp_clause_mask mask, tree *cclauses,
48477 bool *if_p)
48479 tree clauses, sb, ret;
48480 unsigned int save;
48481 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
48483 strcat (p_name, " taskloop");
48484 mask |= OMP_TASKLOOP_CLAUSE_MASK;
48485 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
48486 clause. */
48487 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
48488 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
48490 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48492 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48493 const char *p = IDENTIFIER_POINTER (id);
48495 if (strcmp (p, "simd") == 0)
48497 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
48498 if (cclauses == NULL)
48499 cclauses = cclauses_buf;
48501 cp_lexer_consume_token (parser->lexer);
48502 if (!flag_openmp) /* flag_openmp_simd */
48503 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
48504 cclauses, if_p);
48505 sb = begin_omp_structured_block ();
48506 save = cp_parser_begin_omp_structured_block (parser);
48507 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
48508 cclauses, if_p);
48509 cp_parser_end_omp_structured_block (parser, save);
48510 tree body = finish_omp_structured_block (sb);
48511 if (ret == NULL)
48512 return ret;
48513 ret = make_node (OMP_TASKLOOP);
48514 TREE_TYPE (ret) = void_type_node;
48515 OMP_FOR_BODY (ret) = body;
48516 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
48517 SET_EXPR_LOCATION (ret, loc);
48518 add_stmt (ret);
48519 return ret;
48522 if (!flag_openmp) /* flag_openmp_simd */
48524 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48525 return NULL_TREE;
48528 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
48529 cclauses == NULL);
48530 if (cclauses)
48532 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
48533 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
48536 keep_next_level (true);
48537 sb = begin_omp_structured_block ();
48538 save = cp_parser_begin_omp_structured_block (parser);
48540 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
48541 if_p);
48543 cp_parser_end_omp_structured_block (parser, save);
48544 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
48546 return ret;
48550 /* OpenACC 2.0:
48551 # pragma acc routine oacc-routine-clause[optseq] new-line
48552 function-definition
48554 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
48557 #define OACC_ROUTINE_CLAUSE_MASK \
48558 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
48559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
48560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
48561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
48562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
48564 /* Parse the OpenACC routine pragma. This has an optional '( name )'
48565 component, which must resolve to a declared namespace-scope
48566 function. The clauses are either processed directly (for a named
48567 function), or defered until the immediatley following declaration
48568 is parsed. */
48570 static void
48571 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
48572 enum pragma_context context)
48574 gcc_checking_assert (context == pragma_external);
48575 /* The checking for "another pragma following this one" in the "no optional
48576 '( name )'" case makes sure that we dont re-enter. */
48577 gcc_checking_assert (parser->oacc_routine == NULL);
48579 cp_oacc_routine_data data;
48580 data.error_seen = false;
48581 data.fndecl_seen = false;
48582 data.tokens = vNULL;
48583 data.clauses = NULL_TREE;
48584 data.loc = pragma_tok->location;
48585 /* It is safe to take the address of a local variable; it will only be
48586 used while this scope is live. */
48587 parser->oacc_routine = &data;
48589 /* Look for optional '( name )'. */
48590 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48592 matching_parens parens;
48593 parens.consume_open (parser); /* '(' */
48595 /* We parse the name as an id-expression. If it resolves to
48596 anything other than a non-overloaded function at namespace
48597 scope, it's an error. */
48598 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
48599 tree name = cp_parser_id_expression (parser,
48600 /*template_keyword_p=*/false,
48601 /*check_dependency_p=*/false,
48602 /*template_p=*/NULL,
48603 /*declarator_p=*/false,
48604 /*optional_p=*/false);
48605 tree decl = (identifier_p (name)
48606 ? cp_parser_lookup_name_simple (parser, name, name_loc)
48607 : name);
48608 if (name != error_mark_node && decl == error_mark_node)
48609 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
48611 if (decl == error_mark_node
48612 || !parens.require_close (parser))
48614 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48615 parser->oacc_routine = NULL;
48616 return;
48619 data.clauses
48620 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
48621 "#pragma acc routine",
48622 cp_lexer_peek_token (parser->lexer));
48623 /* The clauses are in reverse order; fix that to make later diagnostic
48624 emission easier. */
48625 data.clauses = nreverse (data.clauses);
48627 if (decl && is_overloaded_fn (decl)
48628 && (TREE_CODE (decl) != FUNCTION_DECL
48629 || DECL_FUNCTION_TEMPLATE_P (decl)))
48631 error_at (name_loc,
48632 "%<#pragma acc routine%> names a set of overloads");
48633 parser->oacc_routine = NULL;
48634 return;
48637 /* Perhaps we should use the same rule as declarations in different
48638 namespaces? */
48639 if (!DECL_NAMESPACE_SCOPE_P (decl))
48641 error_at (name_loc,
48642 "%qD does not refer to a namespace scope function", decl);
48643 parser->oacc_routine = NULL;
48644 return;
48647 if (TREE_CODE (decl) != FUNCTION_DECL)
48649 error_at (name_loc, "%qD does not refer to a function", decl);
48650 parser->oacc_routine = NULL;
48651 return;
48654 cp_finalize_oacc_routine (parser, decl, false);
48655 parser->oacc_routine = NULL;
48657 else /* No optional '( name )'. */
48659 /* Store away all pragma tokens. */
48660 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48661 cp_lexer_consume_token (parser->lexer);
48662 cp_parser_require_pragma_eol (parser, pragma_tok);
48663 struct cp_token_cache *cp
48664 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
48665 parser->oacc_routine->tokens.safe_push (cp);
48667 /* Emit a helpful diagnostic if there's another pragma following this
48668 one. */
48669 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
48671 cp_ensure_no_oacc_routine (parser);
48672 data.tokens.release ();
48673 /* ..., and then just keep going. */
48674 return;
48677 /* We only have to consider the pragma_external case here. */
48678 cp_parser_declaration (parser, NULL_TREE);
48679 if (parser->oacc_routine
48680 && !parser->oacc_routine->fndecl_seen)
48681 cp_ensure_no_oacc_routine (parser);
48682 else
48683 parser->oacc_routine = NULL;
48684 data.tokens.release ();
48688 /* Finalize #pragma acc routine clauses after direct declarator has
48689 been parsed. */
48691 static tree
48692 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
48694 struct cp_token_cache *ce;
48695 cp_oacc_routine_data *data = parser->oacc_routine;
48697 if (!data->error_seen && data->fndecl_seen)
48699 error_at (data->loc,
48700 "%<#pragma acc routine%> not immediately followed by "
48701 "a single function declaration or definition");
48702 data->error_seen = true;
48704 if (data->error_seen)
48705 return attrs;
48707 gcc_checking_assert (data->tokens.length () == 1);
48708 ce = data->tokens[0];
48710 cp_parser_push_lexer_for_tokens (parser, ce);
48711 parser->lexer->in_pragma = true;
48712 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
48714 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48715 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
48716 parser->oacc_routine->clauses
48717 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
48718 "#pragma acc routine", pragma_tok);
48719 /* The clauses are in reverse order; fix that to make later diagnostic
48720 emission easier. */
48721 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
48722 cp_parser_pop_lexer (parser);
48723 /* Later, cp_finalize_oacc_routine will process the clauses. */
48724 parser->oacc_routine->fndecl_seen = true;
48726 return attrs;
48729 /* Apply any saved OpenACC routine clauses to a just-parsed
48730 declaration. */
48732 static void
48733 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
48735 if (UNLIKELY (parser->oacc_routine != NULL))
48737 /* Keep going if we're in error reporting mode. */
48738 if (parser->oacc_routine->error_seen
48739 || fndecl == error_mark_node)
48740 return;
48742 if (TREE_CODE (fndecl) != FUNCTION_DECL)
48744 if (parser->oacc_routine->fndecl_seen)
48746 error_at (parser->oacc_routine->loc,
48747 "%<#pragma acc routine%> not immediately followed by"
48748 " a single function declaration or definition");
48749 parser->oacc_routine = NULL;
48750 return;
48753 cp_ensure_no_oacc_routine (parser);
48754 return;
48757 int compatible
48758 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
48759 parser->oacc_routine->loc,
48760 "#pragma acc routine");
48761 if (compatible < 0)
48763 parser->oacc_routine = NULL;
48764 return;
48766 if (compatible > 0)
48769 else
48771 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
48773 error_at (parser->oacc_routine->loc,
48774 TREE_USED (fndecl)
48775 ? G_("%<#pragma acc routine%> must be applied before"
48776 " use")
48777 : G_("%<#pragma acc routine%> must be applied before"
48778 " definition"));
48779 parser->oacc_routine = NULL;
48780 return;
48783 /* Set the routine's level of parallelism. */
48784 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
48785 oacc_replace_fn_attrib (fndecl, dims);
48787 /* Add an "omp declare target" attribute. */
48788 DECL_ATTRIBUTES (fndecl)
48789 = tree_cons (get_identifier ("omp declare target"),
48790 parser->oacc_routine->clauses,
48791 DECL_ATTRIBUTES (fndecl));
48796 /* Main entry point to OpenMP statement pragmas. */
48798 static void
48799 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
48801 tree stmt;
48802 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
48803 omp_clause_mask mask (0);
48805 switch (cp_parser_pragma_kind (pragma_tok))
48807 case PRAGMA_OACC_ATOMIC:
48808 cp_parser_omp_atomic (parser, pragma_tok, true);
48809 return;
48810 case PRAGMA_OACC_CACHE:
48811 stmt = cp_parser_oacc_cache (parser, pragma_tok);
48812 break;
48813 case PRAGMA_OACC_DATA:
48814 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
48815 break;
48816 case PRAGMA_OACC_ENTER_DATA:
48817 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
48818 break;
48819 case PRAGMA_OACC_EXIT_DATA:
48820 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
48821 break;
48822 case PRAGMA_OACC_HOST_DATA:
48823 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
48824 break;
48825 case PRAGMA_OACC_KERNELS:
48826 case PRAGMA_OACC_PARALLEL:
48827 case PRAGMA_OACC_SERIAL:
48828 strcpy (p_name, "#pragma acc");
48829 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
48830 break;
48831 case PRAGMA_OACC_LOOP:
48832 strcpy (p_name, "#pragma acc");
48833 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
48834 if_p);
48835 break;
48836 case PRAGMA_OACC_UPDATE:
48837 stmt = cp_parser_oacc_update (parser, pragma_tok);
48838 break;
48839 case PRAGMA_OACC_WAIT:
48840 stmt = cp_parser_oacc_wait (parser, pragma_tok);
48841 break;
48842 case PRAGMA_OMP_ALLOCATE:
48843 cp_parser_omp_allocate (parser, pragma_tok);
48844 return;
48845 case PRAGMA_OMP_ATOMIC:
48846 cp_parser_omp_atomic (parser, pragma_tok, false);
48847 return;
48848 case PRAGMA_OMP_CRITICAL:
48849 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
48850 break;
48851 case PRAGMA_OMP_DISTRIBUTE:
48852 strcpy (p_name, "#pragma omp");
48853 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
48854 if_p);
48855 break;
48856 case PRAGMA_OMP_FOR:
48857 strcpy (p_name, "#pragma omp");
48858 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
48859 if_p);
48860 break;
48861 case PRAGMA_OMP_LOOP:
48862 strcpy (p_name, "#pragma omp");
48863 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
48864 if_p);
48865 break;
48866 case PRAGMA_OMP_MASKED:
48867 strcpy (p_name, "#pragma omp");
48868 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
48869 if_p);
48870 break;
48871 case PRAGMA_OMP_MASTER:
48872 strcpy (p_name, "#pragma omp");
48873 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
48874 if_p);
48875 break;
48876 case PRAGMA_OMP_PARALLEL:
48877 strcpy (p_name, "#pragma omp");
48878 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
48879 if_p);
48880 break;
48881 case PRAGMA_OMP_SCOPE:
48882 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
48883 break;
48884 case PRAGMA_OMP_SECTIONS:
48885 strcpy (p_name, "#pragma omp");
48886 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
48887 break;
48888 case PRAGMA_OMP_SIMD:
48889 strcpy (p_name, "#pragma omp");
48890 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
48891 if_p);
48892 break;
48893 case PRAGMA_OMP_SINGLE:
48894 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
48895 break;
48896 case PRAGMA_OMP_TASK:
48897 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
48898 break;
48899 case PRAGMA_OMP_TASKGROUP:
48900 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
48901 break;
48902 case PRAGMA_OMP_TASKLOOP:
48903 strcpy (p_name, "#pragma omp");
48904 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
48905 if_p);
48906 break;
48907 case PRAGMA_OMP_TEAMS:
48908 strcpy (p_name, "#pragma omp");
48909 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
48910 if_p);
48911 break;
48912 case PRAGMA_OMP_ASSUME:
48913 cp_parser_omp_assume (parser, pragma_tok, if_p);
48914 return;
48915 default:
48916 gcc_unreachable ();
48919 protected_set_expr_location (stmt, pragma_tok->location);
48922 /* Transactional Memory parsing routines. */
48924 /* Parse a transaction attribute.
48926 txn-attribute:
48927 attribute
48928 [ [ identifier ] ]
48930 We use this instead of cp_parser_attributes_opt for transactions to avoid
48931 the pedwarn in C++98 mode. */
48933 static tree
48934 cp_parser_txn_attribute_opt (cp_parser *parser)
48936 cp_token *token;
48937 tree attr_name, attr = NULL;
48939 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
48940 return cp_parser_attributes_opt (parser);
48942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
48943 return NULL_TREE;
48944 cp_lexer_consume_token (parser->lexer);
48945 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
48946 goto error1;
48948 token = cp_lexer_peek_token (parser->lexer);
48949 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
48951 token = cp_lexer_consume_token (parser->lexer);
48953 attr_name = (token->type == CPP_KEYWORD
48954 /* For keywords, use the canonical spelling,
48955 not the parsed identifier. */
48956 ? ridpointers[(int) token->keyword]
48957 : token->u.value);
48958 attr = build_tree_list (attr_name, NULL_TREE);
48960 else
48961 cp_parser_error (parser, "expected identifier");
48963 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
48964 error1:
48965 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
48966 return attr;
48969 /* Parse a __transaction_atomic or __transaction_relaxed statement.
48971 transaction-statement:
48972 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
48973 compound-statement
48974 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
48977 static tree
48978 cp_parser_transaction (cp_parser *parser, cp_token *token)
48980 unsigned char old_in = parser->in_transaction;
48981 unsigned char this_in = 1, new_in;
48982 enum rid keyword = token->keyword;
48983 tree stmt, attrs, noex;
48985 cp_lexer_consume_token (parser->lexer);
48987 if (keyword == RID_TRANSACTION_RELAXED
48988 || keyword == RID_SYNCHRONIZED)
48989 this_in |= TM_STMT_ATTR_RELAXED;
48990 else
48992 attrs = cp_parser_txn_attribute_opt (parser);
48993 if (attrs)
48994 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
48997 /* Parse a noexcept specification. */
48998 if (keyword == RID_ATOMIC_NOEXCEPT)
48999 noex = boolean_true_node;
49000 else if (keyword == RID_ATOMIC_CANCEL)
49002 /* cancel-and-throw is unimplemented. */
49003 sorry ("%<atomic_cancel%>");
49004 noex = NULL_TREE;
49006 else
49007 noex = cp_parser_noexcept_specification_opt (parser,
49008 CP_PARSER_FLAGS_NONE,
49009 /*require_constexpr=*/true,
49010 /*consumed_expr=*/NULL,
49011 /*return_cond=*/true);
49013 /* Keep track if we're in the lexical scope of an outer transaction. */
49014 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
49016 stmt = begin_transaction_stmt (token->location, NULL, this_in);
49018 parser->in_transaction = new_in;
49019 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
49020 parser->in_transaction = old_in;
49022 finish_transaction_stmt (stmt, NULL, this_in, noex);
49024 return stmt;
49027 /* Parse a __transaction_atomic or __transaction_relaxed expression.
49029 transaction-expression:
49030 __transaction_atomic txn-noexcept-spec[opt] ( expression )
49031 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
49034 static tree
49035 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
49037 unsigned char old_in = parser->in_transaction;
49038 unsigned char this_in = 1;
49039 cp_token *token;
49040 tree expr, noex;
49041 bool noex_expr;
49042 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49044 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
49045 || keyword == RID_TRANSACTION_RELAXED);
49047 if (!flag_tm)
49048 error_at (loc,
49049 keyword == RID_TRANSACTION_RELAXED
49050 ? G_("%<__transaction_relaxed%> without transactional memory "
49051 "support enabled")
49052 : G_("%<__transaction_atomic%> without transactional memory "
49053 "support enabled"));
49055 token = cp_parser_require_keyword (parser, keyword,
49056 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
49057 : RT_TRANSACTION_RELAXED));
49058 gcc_assert (token != NULL);
49060 if (keyword == RID_TRANSACTION_RELAXED)
49061 this_in |= TM_STMT_ATTR_RELAXED;
49063 /* Set this early. This might mean that we allow transaction_cancel in
49064 an expression that we find out later actually has to be a constexpr.
49065 However, we expect that cxx_constant_value will be able to deal with
49066 this; also, if the noexcept has no constexpr, then what we parse next
49067 really is a transaction's body. */
49068 parser->in_transaction = this_in;
49070 /* Parse a noexcept specification. */
49071 noex = cp_parser_noexcept_specification_opt (parser,
49072 CP_PARSER_FLAGS_NONE,
49073 /*require_constexpr=*/false,
49074 &noex_expr,
49075 /*return_cond=*/true);
49077 if (!noex || !noex_expr
49078 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
49080 matching_parens parens;
49081 parens.require_open (parser);
49083 expr = cp_parser_expression (parser);
49084 expr = finish_parenthesized_expr (expr);
49086 parens.require_close (parser);
49088 else
49090 /* The only expression that is available got parsed for the noexcept
49091 already. noexcept is true then. */
49092 expr = noex;
49093 noex = boolean_true_node;
49096 expr = build_transaction_expr (token->location, expr, this_in, noex);
49097 parser->in_transaction = old_in;
49099 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
49100 return error_mark_node;
49102 return (flag_tm ? expr : error_mark_node);
49105 /* Parse a function-transaction-block.
49107 function-transaction-block:
49108 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
49109 function-body
49110 __transaction_atomic txn-attribute[opt] function-try-block
49111 __transaction_relaxed ctor-initializer[opt] function-body
49112 __transaction_relaxed function-try-block
49115 static void
49116 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
49118 unsigned char old_in = parser->in_transaction;
49119 unsigned char new_in = 1;
49120 tree compound_stmt, stmt, attrs;
49121 cp_token *token;
49123 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
49124 || keyword == RID_TRANSACTION_RELAXED);
49125 token = cp_parser_require_keyword (parser, keyword,
49126 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
49127 : RT_TRANSACTION_RELAXED));
49128 gcc_assert (token != NULL);
49130 if (keyword == RID_TRANSACTION_RELAXED)
49131 new_in |= TM_STMT_ATTR_RELAXED;
49132 else
49134 attrs = cp_parser_txn_attribute_opt (parser);
49135 if (attrs)
49136 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
49139 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
49141 parser->in_transaction = new_in;
49143 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
49144 cp_parser_function_try_block (parser);
49145 else
49146 cp_parser_ctor_initializer_opt_and_function_body
49147 (parser, /*in_function_try_block=*/false);
49149 parser->in_transaction = old_in;
49151 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
49154 /* Parse a __transaction_cancel statement.
49156 cancel-statement:
49157 __transaction_cancel txn-attribute[opt] ;
49158 __transaction_cancel txn-attribute[opt] throw-expression ;
49160 ??? Cancel and throw is not yet implemented. */
49162 static tree
49163 cp_parser_transaction_cancel (cp_parser *parser)
49165 cp_token *token;
49166 bool is_outer = false;
49167 tree stmt, attrs;
49169 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
49170 RT_TRANSACTION_CANCEL);
49171 gcc_assert (token != NULL);
49173 attrs = cp_parser_txn_attribute_opt (parser);
49174 if (attrs)
49175 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
49177 /* ??? Parse cancel-and-throw here. */
49179 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
49181 if (!flag_tm)
49183 error_at (token->location, "%<__transaction_cancel%> without "
49184 "transactional memory support enabled");
49185 return error_mark_node;
49187 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
49189 error_at (token->location, "%<__transaction_cancel%> within a "
49190 "%<__transaction_relaxed%>");
49191 return error_mark_node;
49193 else if (is_outer)
49195 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
49196 && !is_tm_may_cancel_outer (current_function_decl))
49198 error_at (token->location, "outer %<__transaction_cancel%> not "
49199 "within outer %<__transaction_atomic%>");
49200 error_at (token->location,
49201 " or a %<transaction_may_cancel_outer%> function");
49202 return error_mark_node;
49205 else if (parser->in_transaction == 0)
49207 error_at (token->location, "%<__transaction_cancel%> not within "
49208 "%<__transaction_atomic%>");
49209 return error_mark_node;
49212 stmt = build_tm_abort_call (token->location, is_outer);
49213 add_stmt (stmt);
49215 return stmt;
49219 /* Special handling for the first token or line in the file. The first
49220 thing in the file might be #pragma GCC pch_preprocess, which loads a
49221 PCH file, which is a GC collection point. So we need to handle this
49222 first pragma without benefit of an existing lexer structure.
49224 Always returns one token to the caller in *FIRST_TOKEN. This is
49225 either the true first token of the file, or the first token after
49226 the initial pragma. */
49228 static void
49229 cp_parser_initial_pragma (cp_token *first_token)
49231 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
49232 return;
49234 cp_lexer_get_preprocessor_token (0, first_token);
49236 tree name = NULL;
49237 if (first_token->type == CPP_STRING)
49239 name = first_token->u.value;
49241 cp_lexer_get_preprocessor_token (0, first_token);
49244 /* Skip to the end of the pragma. */
49245 if (first_token->type != CPP_PRAGMA_EOL)
49247 error_at (first_token->location,
49248 "malformed %<#pragma GCC pch_preprocess%>");
49250 cp_lexer_get_preprocessor_token (0, first_token);
49251 while (first_token->type != CPP_PRAGMA_EOL);
49254 /* Now actually load the PCH file. */
49255 if (name)
49256 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
49258 /* Read one more token to return to our caller. We have to do this
49259 after reading the PCH file in, since its pointers have to be
49260 live. */
49261 cp_lexer_get_preprocessor_token (0, first_token);
49264 /* Parse a pragma GCC ivdep. */
49266 static bool
49267 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
49269 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49270 return true;
49273 /* Parse a pragma GCC unroll. */
49275 static unsigned short
49276 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
49278 location_t location = cp_lexer_peek_token (parser->lexer)->location;
49279 tree expr = cp_parser_constant_expression (parser);
49280 unsigned short unroll;
49281 expr = maybe_constant_value (expr);
49282 HOST_WIDE_INT lunroll = 0;
49283 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
49284 || TREE_CODE (expr) != INTEGER_CST
49285 || (lunroll = tree_to_shwi (expr)) < 0
49286 || lunroll >= USHRT_MAX)
49288 error_at (location, "%<#pragma GCC unroll%> requires an"
49289 " assignment-expression that evaluates to a non-negative"
49290 " integral constant less than %u", USHRT_MAX);
49291 unroll = 0;
49293 else
49295 unroll = (unsigned short)lunroll;
49296 if (unroll == 0)
49297 unroll = 1;
49299 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49300 return unroll;
49303 /* Normal parsing of a pragma token. Here we can (and must) use the
49304 regular lexer. */
49306 static bool
49307 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
49309 cp_token *pragma_tok;
49310 unsigned int id;
49311 tree stmt;
49312 bool ret = false;
49314 pragma_tok = cp_lexer_consume_token (parser->lexer);
49315 gcc_assert (pragma_tok->type == CPP_PRAGMA);
49316 parser->lexer->in_pragma = true;
49318 id = cp_parser_pragma_kind (pragma_tok);
49319 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
49320 cp_ensure_no_omp_declare_simd (parser);
49321 switch (id)
49323 case PRAGMA_GCC_PCH_PREPROCESS:
49324 error_at (pragma_tok->location,
49325 "%<#pragma GCC pch_preprocess%> must be first");
49326 break;
49328 case PRAGMA_OMP_BARRIER:
49329 switch (context)
49331 case pragma_compound:
49332 cp_parser_omp_barrier (parser, pragma_tok);
49333 return false;
49334 case pragma_stmt:
49335 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
49336 "used in compound statements", "omp barrier");
49337 ret = true;
49338 break;
49339 default:
49340 goto bad_stmt;
49342 break;
49344 case PRAGMA_OMP_DEPOBJ:
49345 switch (context)
49347 case pragma_compound:
49348 cp_parser_omp_depobj (parser, pragma_tok);
49349 return false;
49350 case pragma_stmt:
49351 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
49352 "used in compound statements", "omp depobj");
49353 ret = true;
49354 break;
49355 default:
49356 goto bad_stmt;
49358 break;
49360 case PRAGMA_OMP_FLUSH:
49361 switch (context)
49363 case pragma_compound:
49364 cp_parser_omp_flush (parser, pragma_tok);
49365 return false;
49366 case pragma_stmt:
49367 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
49368 "used in compound statements", "omp flush");
49369 ret = true;
49370 break;
49371 default:
49372 goto bad_stmt;
49374 break;
49376 case PRAGMA_OMP_TASKWAIT:
49377 switch (context)
49379 case pragma_compound:
49380 cp_parser_omp_taskwait (parser, pragma_tok);
49381 return false;
49382 case pragma_stmt:
49383 error_at (pragma_tok->location,
49384 "%<#pragma %s%> may only be used in compound statements",
49385 "omp taskwait");
49386 ret = true;
49387 break;
49388 default:
49389 goto bad_stmt;
49391 break;
49393 case PRAGMA_OMP_TASKYIELD:
49394 switch (context)
49396 case pragma_compound:
49397 cp_parser_omp_taskyield (parser, pragma_tok);
49398 return false;
49399 case pragma_stmt:
49400 error_at (pragma_tok->location,
49401 "%<#pragma %s%> may only be used in compound statements",
49402 "omp taskyield");
49403 ret = true;
49404 break;
49405 default:
49406 goto bad_stmt;
49408 break;
49410 case PRAGMA_OMP_CANCEL:
49411 switch (context)
49413 case pragma_compound:
49414 cp_parser_omp_cancel (parser, pragma_tok);
49415 return false;
49416 case pragma_stmt:
49417 error_at (pragma_tok->location,
49418 "%<#pragma %s%> may only be used in compound statements",
49419 "omp cancel");
49420 ret = true;
49421 break;
49422 default:
49423 goto bad_stmt;
49425 break;
49427 case PRAGMA_OMP_CANCELLATION_POINT:
49428 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
49430 case PRAGMA_OMP_THREADPRIVATE:
49431 cp_parser_omp_threadprivate (parser, pragma_tok);
49432 return false;
49434 case PRAGMA_OMP_DECLARE:
49435 return cp_parser_omp_declare (parser, pragma_tok, context);
49437 case PRAGMA_OACC_DECLARE:
49438 cp_parser_oacc_declare (parser, pragma_tok);
49439 return false;
49441 case PRAGMA_OACC_ENTER_DATA:
49442 if (context == pragma_stmt)
49444 error_at (pragma_tok->location,
49445 "%<#pragma %s%> may only be used in compound statements",
49446 "acc enter data");
49447 ret = true;
49448 break;
49450 else if (context != pragma_compound)
49451 goto bad_stmt;
49452 cp_parser_omp_construct (parser, pragma_tok, if_p);
49453 return true;
49455 case PRAGMA_OACC_EXIT_DATA:
49456 if (context == pragma_stmt)
49458 error_at (pragma_tok->location,
49459 "%<#pragma %s%> may only be used in compound statements",
49460 "acc exit data");
49461 ret = true;
49462 break;
49464 else if (context != pragma_compound)
49465 goto bad_stmt;
49466 cp_parser_omp_construct (parser, pragma_tok, if_p);
49467 return true;
49469 case PRAGMA_OACC_ROUTINE:
49470 if (context != pragma_external)
49472 error_at (pragma_tok->location,
49473 "%<#pragma acc routine%> must be at file scope");
49474 ret = true;
49475 break;
49477 cp_parser_oacc_routine (parser, pragma_tok, context);
49478 return false;
49480 case PRAGMA_OACC_UPDATE:
49481 if (context == pragma_stmt)
49483 error_at (pragma_tok->location,
49484 "%<#pragma %s%> may only be used in compound statements",
49485 "acc update");
49486 ret = true;
49487 break;
49489 else if (context != pragma_compound)
49490 goto bad_stmt;
49491 cp_parser_omp_construct (parser, pragma_tok, if_p);
49492 return true;
49494 case PRAGMA_OACC_WAIT:
49495 if (context == pragma_stmt)
49497 error_at (pragma_tok->location,
49498 "%<#pragma %s%> may only be used in compound statements",
49499 "acc wait");
49500 ret = true;
49501 break;
49503 else if (context != pragma_compound)
49504 goto bad_stmt;
49505 cp_parser_omp_construct (parser, pragma_tok, if_p);
49506 return true;
49507 case PRAGMA_OMP_ALLOCATE:
49508 cp_parser_omp_allocate (parser, pragma_tok);
49509 return false;
49510 case PRAGMA_OACC_ATOMIC:
49511 case PRAGMA_OACC_CACHE:
49512 case PRAGMA_OACC_DATA:
49513 case PRAGMA_OACC_HOST_DATA:
49514 case PRAGMA_OACC_KERNELS:
49515 case PRAGMA_OACC_LOOP:
49516 case PRAGMA_OACC_PARALLEL:
49517 case PRAGMA_OACC_SERIAL:
49518 case PRAGMA_OMP_ASSUME:
49519 case PRAGMA_OMP_ATOMIC:
49520 case PRAGMA_OMP_CRITICAL:
49521 case PRAGMA_OMP_DISTRIBUTE:
49522 case PRAGMA_OMP_FOR:
49523 case PRAGMA_OMP_LOOP:
49524 case PRAGMA_OMP_MASKED:
49525 case PRAGMA_OMP_MASTER:
49526 case PRAGMA_OMP_PARALLEL:
49527 case PRAGMA_OMP_SCOPE:
49528 case PRAGMA_OMP_SECTIONS:
49529 case PRAGMA_OMP_SIMD:
49530 case PRAGMA_OMP_SINGLE:
49531 case PRAGMA_OMP_TASK:
49532 case PRAGMA_OMP_TASKGROUP:
49533 case PRAGMA_OMP_TASKLOOP:
49534 case PRAGMA_OMP_TEAMS:
49535 if (context != pragma_stmt && context != pragma_compound)
49536 goto bad_stmt;
49537 stmt = push_omp_privatization_clauses (false);
49538 cp_parser_omp_construct (parser, pragma_tok, if_p);
49539 pop_omp_privatization_clauses (stmt);
49540 return true;
49542 case PRAGMA_OMP_REQUIRES:
49543 if (context != pragma_external)
49545 error_at (pragma_tok->location,
49546 "%<#pragma omp requires%> may only be used at file or "
49547 "namespace scope");
49548 ret = true;
49549 break;
49551 return cp_parser_omp_requires (parser, pragma_tok);
49553 case PRAGMA_OMP_ASSUMES:
49554 if (context != pragma_external)
49556 error_at (pragma_tok->location,
49557 "%<#pragma omp assumes%> may only be used at file or "
49558 "namespace scope");
49559 ret = true;
49560 break;
49562 return cp_parser_omp_assumes (parser, pragma_tok);
49564 case PRAGMA_OMP_NOTHING:
49565 cp_parser_omp_nothing (parser, pragma_tok);
49566 return false;
49568 case PRAGMA_OMP_ERROR:
49569 return cp_parser_omp_error (parser, pragma_tok, context);
49571 case PRAGMA_OMP_ORDERED:
49572 if (context != pragma_stmt && context != pragma_compound)
49573 goto bad_stmt;
49574 stmt = push_omp_privatization_clauses (false);
49575 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
49576 pop_omp_privatization_clauses (stmt);
49577 return ret;
49579 case PRAGMA_OMP_TARGET:
49580 if (context != pragma_stmt && context != pragma_compound)
49581 goto bad_stmt;
49582 stmt = push_omp_privatization_clauses (false);
49583 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
49584 pop_omp_privatization_clauses (stmt);
49585 return ret;
49587 case PRAGMA_OMP_BEGIN:
49588 cp_parser_omp_begin (parser, pragma_tok);
49589 return false;
49591 case PRAGMA_OMP_END:
49592 cp_parser_omp_end (parser, pragma_tok);
49593 return false;
49595 case PRAGMA_OMP_SCAN:
49596 error_at (pragma_tok->location,
49597 "%<#pragma omp scan%> may only be used in "
49598 "a loop construct with %<inscan%> %<reduction%> clause");
49599 break;
49601 case PRAGMA_OMP_SECTION:
49602 error_at (pragma_tok->location,
49603 "%<#pragma omp section%> may only be used in "
49604 "%<#pragma omp sections%> construct");
49605 break;
49607 case PRAGMA_IVDEP:
49609 if (context == pragma_external)
49611 error_at (pragma_tok->location,
49612 "%<#pragma GCC ivdep%> must be inside a function");
49613 break;
49615 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
49616 unsigned short unroll;
49617 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
49618 if (tok->type == CPP_PRAGMA
49619 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
49621 tok = cp_lexer_consume_token (parser->lexer);
49622 unroll = cp_parser_pragma_unroll (parser, tok);
49623 tok = cp_lexer_peek_token (the_parser->lexer);
49625 else
49626 unroll = 0;
49627 if (tok->type != CPP_KEYWORD
49628 || (tok->keyword != RID_FOR
49629 && tok->keyword != RID_WHILE
49630 && tok->keyword != RID_DO))
49632 cp_parser_error (parser, "for, while or do statement expected");
49633 return false;
49635 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
49636 return true;
49639 case PRAGMA_UNROLL:
49641 if (context == pragma_external)
49643 error_at (pragma_tok->location,
49644 "%<#pragma GCC unroll%> must be inside a function");
49645 break;
49647 const unsigned short unroll
49648 = cp_parser_pragma_unroll (parser, pragma_tok);
49649 bool ivdep;
49650 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
49651 if (tok->type == CPP_PRAGMA
49652 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
49654 tok = cp_lexer_consume_token (parser->lexer);
49655 ivdep = cp_parser_pragma_ivdep (parser, tok);
49656 tok = cp_lexer_peek_token (the_parser->lexer);
49658 else
49659 ivdep = false;
49660 if (tok->type != CPP_KEYWORD
49661 || (tok->keyword != RID_FOR
49662 && tok->keyword != RID_WHILE
49663 && tok->keyword != RID_DO))
49665 cp_parser_error (parser, "for, while or do statement expected");
49666 return false;
49668 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
49669 return true;
49672 default:
49673 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
49674 c_invoke_pragma_handler (id);
49675 break;
49677 bad_stmt:
49678 cp_parser_error (parser, "expected declaration specifiers");
49679 break;
49682 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49683 return ret;
49686 /* The interface the pragma parsers have to the lexer. */
49688 enum cpp_ttype
49689 pragma_lex (tree *value, location_t *loc)
49691 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
49692 enum cpp_ttype ret = tok->type;
49694 *value = tok->u.value;
49695 if (loc)
49696 *loc = tok->location;
49698 if (ret == CPP_PRAGMA_EOL)
49699 ret = CPP_EOF;
49700 else if (ret == CPP_STRING)
49701 *value = cp_parser_string_literal (the_parser, /*translate=*/false,
49702 /*wide_ok=*/false);
49703 else
49705 if (ret == CPP_KEYWORD)
49706 ret = CPP_NAME;
49707 cp_lexer_consume_token (the_parser->lexer);
49710 return ret;
49714 /* External interface. */
49716 /* Parse one entire translation unit. */
49718 void
49719 c_parse_file (void)
49721 static bool already_called = false;
49723 if (already_called)
49724 fatal_error (input_location,
49725 "multi-source compilation not implemented for C++");
49726 already_called = true;
49728 /* cp_lexer_new_main is called before doing any GC allocation
49729 because tokenization might load a PCH file. */
49730 cp_lexer_new_main ();
49732 cp_parser_translation_unit (the_parser);
49733 class_decl_loc_t::diag_mismatched_tags ();
49735 the_parser = NULL;
49737 finish_translation_unit ();
49740 /* Create an identifier for a generic parameter type (a synthesized
49741 template parameter implied by `auto' or a concept identifier). */
49743 static GTY(()) int generic_parm_count;
49744 static tree
49745 make_generic_type_name ()
49747 char buf[32];
49748 sprintf (buf, "auto:%d", ++generic_parm_count);
49749 return get_identifier (buf);
49752 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
49753 (creating a new template parameter list if necessary). Returns the newly
49754 created template type parm. */
49756 static tree
49757 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
49759 /* A requires-clause is not a function and cannot have placeholders. */
49760 if (current_binding_level->requires_expression)
49762 error ("placeholder type not allowed in this context");
49763 return error_mark_node;
49766 gcc_assert (current_binding_level->kind == sk_function_parms);
49768 /* We are either continuing a function template that already contains implicit
49769 template parameters, creating a new fully-implicit function template, or
49770 extending an existing explicit function template with implicit template
49771 parameters. */
49773 cp_binding_level *const entry_scope = current_binding_level;
49775 bool become_template = false;
49776 cp_binding_level *parent_scope = 0;
49778 if (parser->implicit_template_scope)
49780 gcc_assert (parser->implicit_template_parms);
49782 current_binding_level = parser->implicit_template_scope;
49784 else
49786 /* Roll back to the existing template parameter scope (in the case of
49787 extending an explicit function template) or introduce a new template
49788 parameter scope ahead of the function parameter scope (or class scope
49789 in the case of out-of-line member definitions). The function scope is
49790 added back after template parameter synthesis below. */
49792 cp_binding_level *scope = entry_scope;
49794 while (scope->kind == sk_function_parms)
49796 parent_scope = scope;
49797 scope = scope->level_chain;
49799 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
49801 /* If not defining a class, then any class scope is a scope level in
49802 an out-of-line member definition. In this case simply wind back
49803 beyond the first such scope to inject the template parameter list.
49804 Otherwise wind back to the class being defined. The latter can
49805 occur in class member friend declarations such as:
49807 class A {
49808 void foo (auto);
49810 class B {
49811 friend void A::foo (auto);
49814 The template parameter list synthesized for the friend declaration
49815 must be injected in the scope of 'B'. This can also occur in
49816 erroneous cases such as:
49818 struct A {
49819 struct B {
49820 void foo (auto);
49822 void B::foo (auto) {}
49825 Here the attempted definition of 'B::foo' within 'A' is ill-formed
49826 but, nevertheless, the template parameter list synthesized for the
49827 declarator should be injected into the scope of 'A' as if the
49828 ill-formed template was specified explicitly. */
49830 while (scope->kind == sk_class && !scope->defining_class_p)
49832 parent_scope = scope;
49833 scope = scope->level_chain;
49837 current_binding_level = scope;
49839 if (scope->kind != sk_template_parms
49840 || !function_being_declared_is_template_p (parser))
49842 /* Introduce a new template parameter list for implicit template
49843 parameters. */
49845 become_template = true;
49847 parser->implicit_template_scope
49848 = begin_scope (sk_template_parms, NULL);
49850 ++processing_template_decl;
49852 parser->fully_implicit_function_template_p = true;
49853 ++parser->num_template_parameter_lists;
49855 else
49857 /* Synthesize implicit template parameters at the end of the explicit
49858 template parameter list. */
49860 gcc_assert (current_template_parms);
49862 parser->implicit_template_scope = scope;
49864 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
49865 parser->implicit_template_parms
49866 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
49870 /* Synthesize a new template parameter and track the current template
49871 parameter chain with implicit_template_parms. */
49873 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
49874 tree synth_id = make_generic_type_name ();
49875 bool non_type = false;
49877 /* Synthesize the type template parameter. */
49878 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
49879 tree synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
49881 if (become_template)
49882 current_template_parms = tree_cons (size_int (current_template_depth + 1),
49883 NULL_TREE, current_template_parms);
49885 /* Attach the constraint to the parm before processing. */
49886 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
49887 TREE_TYPE (node) = constr;
49888 tree new_parm
49889 = process_template_parm (parser->implicit_template_parms,
49890 input_location,
49891 node,
49892 /*non_type=*/non_type,
49893 /*param_pack=*/false);
49894 // Process_template_parm returns the list of parms, and
49895 // parser->implicit_template_parms holds the final node of the parm
49896 // list. We really want to manipulate the newly appended element.
49897 gcc_checking_assert (!parser->implicit_template_parms
49898 || parser->implicit_template_parms == new_parm);
49899 if (parser->implicit_template_parms)
49900 new_parm = TREE_CHAIN (new_parm);
49901 gcc_checking_assert (!TREE_CHAIN (new_parm));
49903 // Record the last implicit parm node
49904 parser->implicit_template_parms = new_parm;
49906 /* Mark the synthetic declaration "virtual". This is used when
49907 comparing template-heads to determine if whether an abbreviated
49908 function template is equivalent to an explicit template.
49910 Note that DECL_ARTIFICIAL is used elsewhere for template
49911 parameters. */
49912 if (TREE_VALUE (new_parm) != error_mark_node)
49913 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
49915 tree new_decl = get_local_decls ();
49916 if (non_type)
49917 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
49918 new_decl = DECL_INITIAL (new_decl);
49920 /* If creating a fully implicit function template, start the new implicit
49921 template parameter list with this synthesized type, otherwise grow the
49922 current template parameter list. */
49924 if (become_template)
49926 parent_scope->level_chain = current_binding_level;
49928 tree new_parms = make_tree_vec (1);
49929 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
49930 TREE_VALUE (current_template_parms) = new_parms;
49932 else
49934 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
49935 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
49936 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
49937 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
49940 /* If the new parameter was constrained, we need to add that to the
49941 constraints in the template parameter list. */
49942 if (tree req = TEMPLATE_PARM_CONSTRAINTS (new_parm))
49944 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
49945 reqs = combine_constraint_expressions (reqs, req);
49946 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
49949 current_binding_level = entry_scope;
49951 return new_decl;
49954 /* Finish the declaration of a fully implicit function template. Such a
49955 template has no explicit template parameter list so has not been through the
49956 normal template head and tail processing. synthesize_implicit_template_parm
49957 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
49958 provided if the declaration is a class member such that its template
49959 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
49960 form is returned. Otherwise NULL_TREE is returned. */
49962 static tree
49963 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
49965 gcc_assert (parser->fully_implicit_function_template_p);
49967 if (member_decl_opt && member_decl_opt != error_mark_node
49968 && DECL_VIRTUAL_P (member_decl_opt))
49970 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
49971 "implicit templates may not be %<virtual%>");
49972 DECL_VIRTUAL_P (member_decl_opt) = false;
49975 if (member_decl_opt)
49976 member_decl_opt = finish_member_template_decl (member_decl_opt);
49977 end_template_decl ();
49979 parser->fully_implicit_function_template_p = false;
49980 parser->implicit_template_parms = 0;
49981 parser->implicit_template_scope = 0;
49982 --parser->num_template_parameter_lists;
49984 return member_decl_opt;
49987 /* Like finish_fully_implicit_template, but to be used in error
49988 recovery, rearranging scopes so that we restore the state we had
49989 before synthesize_implicit_template_parm inserted the implement
49990 template parms scope. */
49992 static void
49993 abort_fully_implicit_template (cp_parser *parser)
49995 cp_binding_level *return_to_scope = current_binding_level;
49997 if (parser->implicit_template_scope
49998 && return_to_scope != parser->implicit_template_scope)
50000 cp_binding_level *child = return_to_scope;
50001 for (cp_binding_level *scope = child->level_chain;
50002 scope != parser->implicit_template_scope;
50003 scope = child->level_chain)
50004 child = scope;
50005 child->level_chain = parser->implicit_template_scope->level_chain;
50006 parser->implicit_template_scope->level_chain = return_to_scope;
50007 current_binding_level = parser->implicit_template_scope;
50009 else
50010 return_to_scope = return_to_scope->level_chain;
50012 finish_fully_implicit_template (parser, NULL);
50014 gcc_assert (current_binding_level == return_to_scope);
50017 /* Helper function for diagnostics that have complained about things
50018 being used with 'extern "C"' linkage.
50020 Attempt to issue a note showing where the 'extern "C"' linkage began. */
50022 void
50023 maybe_show_extern_c_location (void)
50025 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
50026 inform (the_parser->innermost_linkage_specification_location,
50027 "%<extern \"C\"%> linkage started here");
50030 #include "gt-cp-parser.h"