attribs: Fix wrong error with -Wno-attribute=A::b [PR103649]
[official-gcc.git] / gcc / cp / parser.c
blob33fb40a5b59a3fb09f0c35ea6073658be31b3b81
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2021 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"
51 /* The lexer. */
53 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
54 and c-lex.c) and the C++ parser. */
56 /* The various kinds of non integral constant we encounter. */
57 enum non_integral_constant {
58 NIC_NONE,
59 /* floating-point literal */
60 NIC_FLOAT,
61 /* %<this%> */
62 NIC_THIS,
63 /* %<__FUNCTION__%> */
64 NIC_FUNC_NAME,
65 /* %<__PRETTY_FUNCTION__%> */
66 NIC_PRETTY_FUNC,
67 /* %<__func__%> */
68 NIC_C99_FUNC,
69 /* "%<va_arg%> */
70 NIC_VA_ARG,
71 /* a cast */
72 NIC_CAST,
73 /* %<typeid%> operator */
74 NIC_TYPEID,
75 /* non-constant compound literals */
76 NIC_NCC,
77 /* a function call */
78 NIC_FUNC_CALL,
79 /* an increment */
80 NIC_INC,
81 /* an decrement */
82 NIC_DEC,
83 /* an array reference */
84 NIC_ARRAY_REF,
85 /* %<->%> */
86 NIC_ARROW,
87 /* %<.%> */
88 NIC_POINT,
89 /* the address of a label */
90 NIC_ADDR_LABEL,
91 /* %<*%> */
92 NIC_STAR,
93 /* %<&%> */
94 NIC_ADDR,
95 /* %<++%> */
96 NIC_PREINCREMENT,
97 /* %<--%> */
98 NIC_PREDECREMENT,
99 /* %<new%> */
100 NIC_NEW,
101 /* %<delete%> */
102 NIC_DEL,
103 /* calls to overloaded operators */
104 NIC_OVERLOADED,
105 /* an assignment */
106 NIC_ASSIGNMENT,
107 /* a comma operator */
108 NIC_COMMA,
109 /* a call to a constructor */
110 NIC_CONSTRUCTOR,
111 /* a transaction expression */
112 NIC_TRANSACTION
115 /* The various kinds of errors about name-lookup failing. */
116 enum name_lookup_error {
117 /* NULL */
118 NLE_NULL,
119 /* is not a type */
120 NLE_TYPE,
121 /* is not a class or namespace */
122 NLE_CXX98,
123 /* is not a class, namespace, or enumeration */
124 NLE_NOT_CXX98
127 /* The various kinds of required token */
128 enum required_token {
129 RT_NONE,
130 RT_SEMICOLON, /* ';' */
131 RT_OPEN_PAREN, /* '(' */
132 RT_CLOSE_BRACE, /* '}' */
133 RT_OPEN_BRACE, /* '{' */
134 RT_CLOSE_SQUARE, /* ']' */
135 RT_OPEN_SQUARE, /* '[' */
136 RT_COMMA, /* ',' */
137 RT_SCOPE, /* '::' */
138 RT_LESS, /* '<' */
139 RT_GREATER, /* '>' */
140 RT_EQ, /* '=' */
141 RT_ELLIPSIS, /* '...' */
142 RT_MULT, /* '*' */
143 RT_COMPL, /* '~' */
144 RT_COLON, /* ':' */
145 RT_COLON_SCOPE, /* ':' or '::' */
146 RT_CLOSE_PAREN, /* ')' */
147 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
148 RT_PRAGMA_EOL, /* end of line */
149 RT_NAME, /* identifier */
151 /* The type is CPP_KEYWORD */
152 RT_NEW, /* new */
153 RT_DELETE, /* delete */
154 RT_RETURN, /* return */
155 RT_WHILE, /* while */
156 RT_EXTERN, /* extern */
157 RT_STATIC_ASSERT, /* static_assert */
158 RT_DECLTYPE, /* decltype */
159 RT_OPERATOR, /* operator */
160 RT_CLASS, /* class */
161 RT_TEMPLATE, /* template */
162 RT_NAMESPACE, /* namespace */
163 RT_USING, /* using */
164 RT_ASM, /* asm */
165 RT_TRY, /* try */
166 RT_CATCH, /* catch */
167 RT_THROW, /* throw */
168 RT_AUTO, /* auto */
169 RT_LABEL, /* __label__ */
170 RT_AT_TRY, /* @try */
171 RT_AT_SYNCHRONIZED, /* @synchronized */
172 RT_AT_THROW, /* @throw */
174 RT_SELECT, /* selection-statement */
175 RT_ITERATION, /* iteration-statement */
176 RT_JUMP, /* jump-statement */
177 RT_CLASS_KEY, /* class-key */
178 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
179 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
180 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
181 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
183 RT_CO_YIELD /* co_yield */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (unsigned, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
249 static tree cp_parser_late_noexcept_specifier
250 (cp_parser *, tree);
251 static void noexcept_override_late_checks
252 (tree, tree);
254 static void cp_parser_initial_pragma
255 (cp_token *);
257 static bool cp_parser_omp_declare_reduction_exprs
258 (tree, cp_parser *);
259 static void cp_finalize_oacc_routine
260 (cp_parser *, tree, bool);
262 /* Manifest constants. */
263 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264 #define CP_SAVED_TOKEN_STACK 5
266 /* Variables. */
268 /* The stream to which debugging output should be written. */
269 static FILE *cp_lexer_debug_stream;
271 /* Nonzero if we are parsing an unevaluated operand: an operand to
272 sizeof, typeof, or alignof. */
273 int cp_unevaluated_operand;
275 /* Dump up to NUM tokens in BUFFER to FILE starting with token
276 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
277 first token in BUFFER. If NUM is 0, dump all the tokens. If
278 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279 highlighted by surrounding it in [[ ]]. */
281 static void
282 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
283 cp_token *start_token, unsigned num,
284 cp_token *curr_token)
286 unsigned i, nprinted;
287 cp_token *token;
288 bool do_print;
290 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
292 if (buffer == NULL)
293 return;
295 if (num == 0)
296 num = buffer->length ();
298 if (start_token == NULL)
299 start_token = buffer->address ();
301 if (start_token > buffer->address ())
303 cp_lexer_print_token (file, &(*buffer)[0]);
304 fprintf (file, " ... ");
307 do_print = false;
308 nprinted = 0;
309 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
311 if (token == start_token)
312 do_print = true;
314 if (!do_print)
315 continue;
317 nprinted++;
318 if (token == curr_token)
319 fprintf (file, "[[");
321 cp_lexer_print_token (file, token);
323 if (token == curr_token)
324 fprintf (file, "]]");
326 switch (token->type)
328 case CPP_SEMICOLON:
329 case CPP_OPEN_BRACE:
330 case CPP_CLOSE_BRACE:
331 case CPP_EOF:
332 fputc ('\n', file);
333 break;
335 default:
336 fputc (' ', file);
340 if (i == num && i < buffer->length ())
342 fprintf (file, " ... ");
343 cp_lexer_print_token (file, &buffer->last ());
346 fprintf (file, "\n");
350 /* Dump all tokens in BUFFER to stderr. */
352 void
353 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
355 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
358 DEBUG_FUNCTION void
359 debug (vec<cp_token, va_gc> &ref)
361 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
364 DEBUG_FUNCTION void
365 debug (vec<cp_token, va_gc> *ptr)
367 if (ptr)
368 debug (*ptr);
369 else
370 fprintf (stderr, "<nil>\n");
374 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
375 description for T. */
377 static void
378 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
380 if (t)
382 fprintf (file, "%s: ", desc);
383 print_node_brief (file, "", t, 0);
388 /* Dump parser context C to FILE. */
390 static void
391 cp_debug_print_context (FILE *file, cp_parser_context *c)
393 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
394 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
395 print_node_brief (file, "", c->object_type, 0);
396 fprintf (file, "}\n");
400 /* Print the stack of parsing contexts to FILE starting with FIRST. */
402 static void
403 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
405 unsigned i;
406 cp_parser_context *c;
408 fprintf (file, "Parsing context stack:\n");
409 for (i = 0, c = first; c; c = c->next, i++)
411 fprintf (file, "\t#%u: ", i);
412 cp_debug_print_context (file, c);
417 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
419 static void
420 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
422 if (flag)
423 fprintf (file, "%s: true\n", desc);
427 /* Print an unparsed function entry UF to FILE. */
429 static void
430 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
432 unsigned i;
433 cp_default_arg_entry *default_arg_fn;
434 tree fn;
436 fprintf (file, "\tFunctions with default args:\n");
437 for (i = 0;
438 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
439 i++)
441 fprintf (file, "\t\tClass type: ");
442 print_node_brief (file, "", default_arg_fn->class_type, 0);
443 fprintf (file, "\t\tDeclaration: ");
444 print_node_brief (file, "", default_arg_fn->decl, 0);
445 fprintf (file, "\n");
448 fprintf (file, "\n\tFunctions with definitions that require "
449 "post-processing\n\t\t");
450 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
452 print_node_brief (file, "", fn, 0);
453 fprintf (file, " ");
455 fprintf (file, "\n");
457 fprintf (file, "\n\tNon-static data members with initializers that require "
458 "post-processing\n\t\t");
459 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
461 print_node_brief (file, "", fn, 0);
462 fprintf (file, " ");
464 fprintf (file, "\n");
468 /* Print the stack of unparsed member functions S to FILE. */
470 static void
471 cp_debug_print_unparsed_queues (FILE *file,
472 vec<cp_unparsed_functions_entry, va_gc> *s)
474 unsigned i;
475 cp_unparsed_functions_entry *uf;
477 fprintf (file, "Unparsed functions\n");
478 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
480 fprintf (file, "#%u:\n", i);
481 cp_debug_print_unparsed_function (file, uf);
486 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487 the given PARSER. If FILE is NULL, the output is printed on stderr. */
489 static void
490 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
492 cp_token *next_token, *first_token, *start_token;
494 if (file == NULL)
495 file = stderr;
497 next_token = parser->lexer->next_token;
498 first_token = parser->lexer->buffer->address ();
499 start_token = (next_token > first_token + window_size / 2)
500 ? next_token - window_size / 2
501 : first_token;
502 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
503 next_token);
507 /* Dump debugging information for the given PARSER. If FILE is NULL,
508 the output is printed on stderr. */
510 void
511 cp_debug_parser (FILE *file, cp_parser *parser)
513 const size_t window_size = 20;
514 cp_token *token;
515 expanded_location eloc;
517 if (file == NULL)
518 file = stderr;
520 fprintf (file, "Parser state\n\n");
521 fprintf (file, "Number of tokens: %u\n",
522 vec_safe_length (parser->lexer->buffer));
523 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
524 cp_debug_print_tree_if_set (file, "Object scope",
525 parser->object_scope);
526 cp_debug_print_tree_if_set (file, "Qualifying scope",
527 parser->qualifying_scope);
528 cp_debug_print_context_stack (file, parser->context);
529 cp_debug_print_flag (file, "Allow GNU extensions",
530 parser->allow_gnu_extensions_p);
531 cp_debug_print_flag (file, "'>' token is greater-than",
532 parser->greater_than_is_operator_p);
533 cp_debug_print_flag (file, "Default args allowed in current "
534 "parameter list", parser->default_arg_ok_p);
535 cp_debug_print_flag (file, "Parsing integral constant-expression",
536 parser->integral_constant_expression_p);
537 cp_debug_print_flag (file, "Allow non-constant expression in current "
538 "constant-expression",
539 parser->allow_non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Seen non-constant expression",
541 parser->non_integral_constant_expression_p);
542 cp_debug_print_flag (file, "Local names forbidden in current context",
543 (parser->local_variables_forbidden_p
544 & LOCAL_VARS_FORBIDDEN));
545 cp_debug_print_flag (file, "'this' forbidden in current context",
546 (parser->local_variables_forbidden_p
547 & THIS_FORBIDDEN));
548 cp_debug_print_flag (file, "In unbraced linkage specification",
549 parser->in_unbraced_linkage_specification_p);
550 cp_debug_print_flag (file, "Parsing a declarator",
551 parser->in_declarator_p);
552 cp_debug_print_flag (file, "In template argument list",
553 parser->in_template_argument_list_p);
554 cp_debug_print_flag (file, "Parsing an iteration statement",
555 parser->in_statement & IN_ITERATION_STMT);
556 cp_debug_print_flag (file, "Parsing a switch statement",
557 parser->in_statement & IN_SWITCH_STMT);
558 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 parser->in_statement & IN_OMP_BLOCK);
560 cp_debug_print_flag (file, "Parsing an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "String expressions should be translated "
567 "to execution character set",
568 parser->translate_strings_p);
569 cp_debug_print_flag (file, "Parsing function body outside of a "
570 "local class", parser->in_function_body);
571 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
572 parser->colon_corrects_to_scope_p);
573 cp_debug_print_flag (file, "Colon doesn't start a class definition",
574 parser->colon_doesnt_start_class_def_p);
575 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
576 parser->objective_c_message_context_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
579 parser->type_definition_forbidden_message,
580 parser->type_definition_forbidden_message_arg
581 ? parser->type_definition_forbidden_message_arg : "<none>");
582 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
583 fprintf (file, "Number of class definitions in progress: %u\n",
584 parser->num_classes_being_defined);
585 fprintf (file, "Number of template parameter lists for the current "
586 "declaration: %u\n", parser->num_template_parameter_lists);
587 cp_debug_parser_tokens (file, parser, window_size);
588 token = parser->lexer->next_token;
589 fprintf (file, "Next token to parse:\n");
590 fprintf (file, "\tToken: ");
591 cp_lexer_print_token (file, token);
592 eloc = expand_location (token->location);
593 fprintf (file, "\n\tFile: %s\n", eloc.file);
594 fprintf (file, "\tLine: %d\n", eloc.line);
595 fprintf (file, "\tColumn: %d\n", eloc.column);
598 DEBUG_FUNCTION void
599 debug (cp_parser &ref)
601 cp_debug_parser (stderr, &ref);
604 DEBUG_FUNCTION void
605 debug (cp_parser *ptr)
607 if (ptr)
608 debug (*ptr);
609 else
610 fprintf (stderr, "<nil>\n");
613 /* Allocate memory for a new lexer object and return it. */
615 static cp_lexer *
616 cp_lexer_alloc (void)
618 /* Allocate the memory. */
619 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
621 /* Initially we are not debugging. */
622 lexer->debugging_p = false;
624 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
626 /* Create the buffer. */
627 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
629 return lexer;
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
633 preprocessor. */
635 static cp_lexer *
636 cp_lexer_new_main (void)
638 cp_token token;
640 /* It's possible that parsing the first pragma will load a PCH file,
641 which is a GC collection point. So we have to do that before
642 allocating any memory. */
643 cp_lexer_get_preprocessor_token (0, &token);
644 cp_parser_initial_pragma (&token);
645 c_common_no_more_pch ();
647 cp_lexer *lexer = cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 cp_token *tok = lexer->buffer->quick_push (token);
651 uintptr_t filter = 0;
652 if (modules_p ())
653 filter = module_token_cdtor (parse_in, filter);
655 /* Get the remaining tokens from the preprocessor. */
656 while (tok->type != CPP_EOF)
658 if (filter)
659 /* Process the previous token. */
660 module_token_lang (tok->type, tok->keyword, tok->u.value,
661 tok->location, filter);
662 tok = vec_safe_push (lexer->buffer, cp_token ());
663 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
666 lexer->next_token = lexer->buffer->address ();
667 lexer->last_token = lexer->next_token
668 + lexer->buffer->length ()
669 - 1;
671 if (lexer->buffer->length () != 1)
673 /* Set the EOF token's location to be the just after the previous
674 token's range. That way 'at-eof' diagnostics point at something
675 meaninful. */
676 auto range = get_range_from_loc (line_table, tok[-1].location);
677 tok[0].location
678 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
681 if (filter)
682 module_token_cdtor (parse_in, filter);
684 /* Subsequent preprocessor diagnostics should use compiler
685 diagnostic functions to get the compiler source location. */
686 done_lexing = true;
688 maybe_check_all_macros (parse_in);
690 gcc_assert (!lexer->next_token->purged_p);
691 return lexer;
694 /* Create a new lexer whose token stream is primed with the tokens in
695 CACHE. When these tokens are exhausted, no new tokens will be read. */
697 static cp_lexer *
698 cp_lexer_new_from_tokens (cp_token_cache *cache)
700 cp_token *first = cache->first;
701 cp_token *last = cache->last;
702 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
704 /* We do not own the buffer. */
705 lexer->buffer = NULL;
707 /* Insert an EOF token. */
708 lexer->saved_type = last->type;
709 lexer->saved_keyword = last->keyword;
710 last->type = CPP_EOF;
711 last->keyword = RID_MAX;
713 lexer->next_token = first;
714 lexer->last_token = last;
716 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
718 /* Initially we are not debugging. */
719 lexer->debugging_p = false;
721 gcc_assert (!lexer->next_token->purged_p
722 && !lexer->last_token->purged_p);
723 return lexer;
726 /* Frees all resources associated with LEXER. */
728 static void
729 cp_lexer_destroy (cp_lexer *lexer)
731 if (lexer->buffer)
732 vec_free (lexer->buffer);
733 else
735 /* Restore the token we overwrite with EOF. */
736 lexer->last_token->type = lexer->saved_type;
737 lexer->last_token->keyword = lexer->saved_keyword;
739 lexer->saved_tokens.release ();
740 ggc_free (lexer);
743 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
744 be used. The point of this flag is to help the compiler to fold away calls
745 to cp_lexer_debugging_p within this source file at compile time, when the
746 lexer is not being debugged. */
748 #define LEXER_DEBUGGING_ENABLED_P false
750 /* Returns nonzero if debugging information should be output. */
752 static inline bool
753 cp_lexer_debugging_p (cp_lexer *lexer)
755 if (!LEXER_DEBUGGING_ENABLED_P)
756 return false;
758 return lexer->debugging_p;
762 static inline cp_token_position
763 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
765 return lexer->next_token - previous_p;
768 static inline cp_token *
769 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
771 return pos;
774 static inline void
775 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
777 lexer->next_token = cp_lexer_token_at (lexer, pos);
780 static inline cp_token_position
781 cp_lexer_previous_token_position (cp_lexer *lexer)
783 return cp_lexer_token_position (lexer, true);
786 static inline cp_token *
787 cp_lexer_previous_token (cp_lexer *lexer)
789 cp_token_position tp = cp_lexer_previous_token_position (lexer);
791 /* Skip past purged tokens. */
792 while (tp->purged_p)
794 gcc_assert (tp != vec_safe_address (lexer->buffer));
795 tp--;
798 return cp_lexer_token_at (lexer, tp);
801 /* Same as above, but return NULL when the lexer doesn't own the token
802 buffer or if the next_token is at the start of the token
803 vector or if all previous tokens are purged. */
805 static cp_token *
806 cp_lexer_safe_previous_token (cp_lexer *lexer)
808 if (lexer->buffer
809 && lexer->next_token != lexer->buffer->address ())
811 cp_token_position tp = cp_lexer_previous_token_position (lexer);
813 /* Skip past purged tokens. */
814 while (tp->purged_p)
816 if (tp == lexer->buffer->address ())
817 return NULL;
818 tp--;
820 return cp_lexer_token_at (lexer, tp);
823 return NULL;
826 /* Overload for make_location, taking the lexer to mean the location of the
827 previous token. */
829 static inline location_t
830 make_location (location_t caret, location_t start, cp_lexer *lexer)
832 cp_token *t = cp_lexer_previous_token (lexer);
833 return make_location (caret, start, t->location);
836 /* Overload for make_location taking tokens instead of locations. */
838 static inline location_t
839 make_location (cp_token *caret, cp_token *start, cp_token *end)
841 return make_location (caret->location, start->location, end->location);
844 /* nonzero if we are presently saving tokens. */
846 static inline int
847 cp_lexer_saving_tokens (const cp_lexer* lexer)
849 return lexer->saved_tokens.length () != 0;
852 /* Store the next token from the preprocessor in *TOKEN. Return true
853 if we reach EOF. If LEXER is NULL, assume we are handling an
854 initial #pragma pch_preprocess, and thus want the lexer to return
855 processed strings. */
857 static void
858 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
860 static int is_extern_c = 0;
862 /* Get a new token from the preprocessor. */
863 token->type
864 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
865 flags);
866 token->keyword = RID_MAX;
867 token->purged_p = false;
868 token->error_reported = false;
869 token->tree_check_p = false;
870 /* Usually never see a zero, but just in case ... */
871 token->main_source_p = line_table->depth <= 1;
873 /* On some systems, some header files are surrounded by an
874 implicit extern "C" block. Set a flag in the token if it
875 comes from such a header. */
876 is_extern_c += pending_lang_change;
877 pending_lang_change = 0;
878 token->implicit_extern_c = is_extern_c > 0;
880 /* Check to see if this token is a keyword. */
881 if (token->type == CPP_NAME)
883 if (IDENTIFIER_KEYWORD_P (token->u.value))
885 /* Mark this token as a keyword. */
886 token->type = CPP_KEYWORD;
887 /* Record which keyword. */
888 token->keyword = C_RID_CODE (token->u.value);
890 else
892 if (warn_cxx11_compat
893 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
894 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
896 /* Warn about the C++0x keyword (but still treat it as
897 an identifier). */
898 warning_at (token->location, OPT_Wc__11_compat,
899 "identifier %qE is a keyword in C++11",
900 token->u.value);
902 /* Clear out the C_RID_CODE so we don't warn about this
903 particular identifier-turned-keyword again. */
904 C_SET_RID_CODE (token->u.value, RID_MAX);
906 if (warn_cxx20_compat
907 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
908 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
910 /* Warn about the C++20 keyword (but still treat it as
911 an identifier). */
912 warning_at (token->location, OPT_Wc__20_compat,
913 "identifier %qE is a keyword in C++20",
914 token->u.value);
916 /* Clear out the C_RID_CODE so we don't warn about this
917 particular identifier-turned-keyword again. */
918 C_SET_RID_CODE (token->u.value, RID_MAX);
921 token->keyword = RID_MAX;
924 else if (token->type == CPP_AT_NAME)
926 /* This only happens in Objective-C++; it must be a keyword. */
927 token->type = CPP_KEYWORD;
928 switch (C_RID_CODE (token->u.value))
930 /* Replace 'class' with '@class', 'private' with '@private',
931 etc. This prevents confusion with the C++ keyword
932 'class', and makes the tokens consistent with other
933 Objective-C 'AT' keywords. For example '@class' is
934 reported as RID_AT_CLASS which is consistent with
935 '@synchronized', which is reported as
936 RID_AT_SYNCHRONIZED.
938 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
939 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
940 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
941 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
942 case RID_THROW: token->keyword = RID_AT_THROW; break;
943 case RID_TRY: token->keyword = RID_AT_TRY; break;
944 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
945 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
946 default: token->keyword = C_RID_CODE (token->u.value);
951 /* Update the globals input_location and the input file stack from TOKEN. */
952 static inline void
953 cp_lexer_set_source_position_from_token (cp_token *token)
955 input_location = token->location;
958 /* Update the globals input_location and the input file stack from LEXER. */
959 static inline void
960 cp_lexer_set_source_position (cp_lexer *lexer)
962 cp_token *token = cp_lexer_peek_token (lexer);
963 cp_lexer_set_source_position_from_token (token);
966 /* Return a pointer to the next token in the token stream, but do not
967 consume it. */
969 static inline cp_token *
970 cp_lexer_peek_token (cp_lexer *lexer)
972 if (cp_lexer_debugging_p (lexer))
974 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
975 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
976 putc ('\n', cp_lexer_debug_stream);
978 return lexer->next_token;
981 /* Return true if the next token has the indicated TYPE. */
983 static inline bool
984 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
986 return cp_lexer_peek_token (lexer)->type == type;
989 /* Return true if the next token does not have the indicated TYPE. */
991 static inline bool
992 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
994 return !cp_lexer_next_token_is (lexer, type);
997 /* Return true if the next token is the indicated KEYWORD. */
999 static inline bool
1000 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1002 return cp_lexer_peek_token (lexer)->keyword == keyword;
1005 static inline bool
1006 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1008 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1011 static inline bool
1012 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1014 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1017 /* Return true if KEYWORD can start a decl-specifier. */
1019 bool
1020 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1022 switch (keyword)
1024 /* auto specifier: storage-class-specifier in C++,
1025 simple-type-specifier in C++0x. */
1026 case RID_AUTO:
1027 /* Storage classes. */
1028 case RID_REGISTER:
1029 case RID_STATIC:
1030 case RID_EXTERN:
1031 case RID_MUTABLE:
1032 case RID_THREAD:
1033 /* Elaborated type specifiers. */
1034 case RID_ENUM:
1035 case RID_CLASS:
1036 case RID_STRUCT:
1037 case RID_UNION:
1038 case RID_TYPENAME:
1039 /* Simple type specifiers. */
1040 case RID_CHAR:
1041 case RID_CHAR8:
1042 case RID_CHAR16:
1043 case RID_CHAR32:
1044 case RID_WCHAR:
1045 case RID_BOOL:
1046 case RID_SHORT:
1047 case RID_INT:
1048 case RID_LONG:
1049 case RID_SIGNED:
1050 case RID_UNSIGNED:
1051 case RID_FLOAT:
1052 case RID_DOUBLE:
1053 case RID_VOID:
1054 /* CV qualifiers. */
1055 case RID_CONST:
1056 case RID_VOLATILE:
1057 /* Function specifiers. */
1058 case RID_EXPLICIT:
1059 case RID_VIRTUAL:
1060 /* friend/typdef/inline specifiers. */
1061 case RID_FRIEND:
1062 case RID_TYPEDEF:
1063 case RID_INLINE:
1064 /* GNU extensions. */
1065 case RID_TYPEOF:
1066 /* C++11 extensions. */
1067 case RID_DECLTYPE:
1068 case RID_UNDERLYING_TYPE:
1069 case RID_CONSTEXPR:
1070 /* C++20 extensions. */
1071 case RID_CONSTINIT:
1072 case RID_CONSTEVAL:
1073 return true;
1075 default:
1076 if (keyword >= RID_FIRST_INT_N
1077 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1078 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1079 return true;
1080 return false;
1084 /* Return true if the next token is a keyword for a decl-specifier. */
1086 static bool
1087 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1089 cp_token *token;
1091 token = cp_lexer_peek_token (lexer);
1092 return cp_keyword_starts_decl_specifier_p (token->keyword);
1095 /* Returns TRUE iff the token T begins a decltype type. */
1097 static bool
1098 token_is_decltype (cp_token *t)
1100 return (t->keyword == RID_DECLTYPE
1101 || t->type == CPP_DECLTYPE);
1104 /* Returns TRUE iff the next token begins a decltype type. */
1106 static bool
1107 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1109 cp_token *t = cp_lexer_peek_token (lexer);
1110 return token_is_decltype (t);
1113 /* Called when processing a token with tree_check_value; perform or defer the
1114 associated checks and return the value. */
1116 static tree
1117 saved_checks_value (struct tree_check *check_value)
1119 /* Perform any access checks that were deferred. */
1120 vec<deferred_access_check, va_gc> *checks;
1121 deferred_access_check *chk;
1122 checks = check_value->checks;
1123 if (checks)
1125 int i;
1126 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1127 perform_or_defer_access_check (chk->binfo,
1128 chk->decl,
1129 chk->diag_decl, tf_warning_or_error);
1131 /* Return the stored value. */
1132 return check_value->value;
1135 /* Return a pointer to the Nth token in the token stream. If N is 1,
1136 then this is precisely equivalent to cp_lexer_peek_token (except
1137 that it is not inline). One would like to disallow that case, but
1138 there is one case (cp_parser_nth_token_starts_template_id) where
1139 the caller passes a variable for N and it might be 1. */
1141 static cp_token *
1142 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1144 cp_token *token;
1146 /* N is 1-based, not zero-based. */
1147 gcc_assert (n > 0);
1149 if (cp_lexer_debugging_p (lexer))
1150 fprintf (cp_lexer_debug_stream,
1151 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1153 --n;
1154 token = lexer->next_token;
1155 while (n && token->type != CPP_EOF)
1157 ++token;
1158 if (!token->purged_p)
1159 --n;
1162 if (cp_lexer_debugging_p (lexer))
1164 cp_lexer_print_token (cp_lexer_debug_stream, token);
1165 putc ('\n', cp_lexer_debug_stream);
1168 return token;
1171 /* Return the next token, and advance the lexer's next_token pointer
1172 to point to the next non-purged token. */
1174 static cp_token *
1175 cp_lexer_consume_token (cp_lexer* lexer)
1177 cp_token *token = lexer->next_token;
1181 gcc_assert (token->type != CPP_EOF);
1182 lexer->next_token++;
1184 while (lexer->next_token->purged_p);
1186 cp_lexer_set_source_position_from_token (token);
1188 /* Provide debugging output. */
1189 if (cp_lexer_debugging_p (lexer))
1191 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1192 cp_lexer_print_token (cp_lexer_debug_stream, token);
1193 putc ('\n', cp_lexer_debug_stream);
1196 return token;
1199 /* Permanently remove the next token from the token stream, and
1200 advance the next_token pointer to refer to the next non-purged
1201 token. */
1203 static void
1204 cp_lexer_purge_token (cp_lexer *lexer)
1206 cp_token *tok = lexer->next_token;
1208 gcc_assert (tok->type != CPP_EOF);
1209 tok->purged_p = true;
1210 tok->location = UNKNOWN_LOCATION;
1211 tok->u.value = NULL_TREE;
1212 tok->keyword = RID_MAX;
1215 tok++;
1216 while (tok->purged_p);
1217 lexer->next_token = tok;
1220 /* Permanently remove all tokens after TOK, up to, but not
1221 including, the token that will be returned next by
1222 cp_lexer_peek_token. */
1224 static void
1225 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1227 cp_token *peek = lexer->next_token;
1229 gcc_assert (tok < peek);
1231 for (tok++; tok != peek; tok++)
1233 tok->purged_p = true;
1234 tok->location = UNKNOWN_LOCATION;
1235 tok->u.value = NULL_TREE;
1236 tok->keyword = RID_MAX;
1240 /* Begin saving tokens. All tokens consumed after this point will be
1241 preserved. */
1243 static void
1244 cp_lexer_save_tokens (cp_lexer* lexer)
1246 /* Provide debugging output. */
1247 if (cp_lexer_debugging_p (lexer))
1248 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1250 lexer->saved_tokens.safe_push (lexer->next_token);
1253 /* Commit to the portion of the token stream most recently saved. */
1255 static void
1256 cp_lexer_commit_tokens (cp_lexer* lexer)
1258 /* Provide debugging output. */
1259 if (cp_lexer_debugging_p (lexer))
1260 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1262 lexer->saved_tokens.pop ();
1265 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1266 to the token stream. Stop saving tokens. */
1268 static void
1269 cp_lexer_rollback_tokens (cp_lexer* lexer)
1271 /* Provide debugging output. */
1272 if (cp_lexer_debugging_p (lexer))
1273 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1275 lexer->next_token = lexer->saved_tokens.pop ();
1278 /* RAII wrapper around the above functions, with sanity checking. Creating
1279 a variable saves tokens, which are committed when the variable is
1280 destroyed unless they are explicitly rolled back by calling the rollback
1281 member function. */
1283 struct saved_token_sentinel
1285 cp_lexer *lexer;
1286 unsigned len;
1287 bool commit;
1288 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1290 len = lexer->saved_tokens.length ();
1291 cp_lexer_save_tokens (lexer);
1293 void rollback ()
1295 cp_lexer_rollback_tokens (lexer);
1296 commit = false;
1298 ~saved_token_sentinel()
1300 if (commit)
1301 cp_lexer_commit_tokens (lexer);
1302 gcc_assert (lexer->saved_tokens.length () == len);
1306 /* Print a representation of the TOKEN on the STREAM. */
1308 static void
1309 cp_lexer_print_token (FILE * stream, cp_token *token)
1311 /* We don't use cpp_type2name here because the parser defines
1312 a few tokens of its own. */
1313 static const char *const token_names[] = {
1314 /* cpplib-defined token types */
1315 #define OP(e, s) #e,
1316 #define TK(e, s) #e,
1317 TTYPE_TABLE
1318 #undef OP
1319 #undef TK
1320 /* C++ parser token types - see "Manifest constants", above. */
1321 "KEYWORD",
1322 "TEMPLATE_ID",
1323 "NESTED_NAME_SPECIFIER",
1326 /* For some tokens, print the associated data. */
1327 switch (token->type)
1329 case CPP_KEYWORD:
1330 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1331 For example, `struct' is mapped to an INTEGER_CST. */
1332 if (!identifier_p (token->u.value))
1333 break;
1334 /* fall through */
1335 case CPP_NAME:
1336 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1337 break;
1339 case CPP_STRING:
1340 case CPP_STRING16:
1341 case CPP_STRING32:
1342 case CPP_WSTRING:
1343 case CPP_UTF8STRING:
1344 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1345 break;
1347 case CPP_NUMBER:
1348 print_generic_expr (stream, token->u.value);
1349 break;
1351 default:
1352 /* If we have a name for the token, print it out. Otherwise, we
1353 simply give the numeric code. */
1354 if (token->type < ARRAY_SIZE(token_names))
1355 fputs (token_names[token->type], stream);
1356 else
1357 fprintf (stream, "[%d]", token->type);
1358 break;
1362 DEBUG_FUNCTION void
1363 debug (cp_token &ref)
1365 cp_lexer_print_token (stderr, &ref);
1366 fprintf (stderr, "\n");
1369 DEBUG_FUNCTION void
1370 debug (cp_token *ptr)
1372 if (ptr)
1373 debug (*ptr);
1374 else
1375 fprintf (stderr, "<nil>\n");
1379 /* Start emitting debugging information. */
1381 static void
1382 cp_lexer_start_debugging (cp_lexer* lexer)
1384 if (!LEXER_DEBUGGING_ENABLED_P)
1385 fatal_error (input_location,
1386 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1388 lexer->debugging_p = true;
1389 cp_lexer_debug_stream = stderr;
1392 /* Stop emitting debugging information. */
1394 static void
1395 cp_lexer_stop_debugging (cp_lexer* lexer)
1397 if (!LEXER_DEBUGGING_ENABLED_P)
1398 fatal_error (input_location,
1399 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1401 lexer->debugging_p = false;
1402 cp_lexer_debug_stream = NULL;
1405 /* Create a new cp_token_cache, representing a range of tokens. */
1407 static cp_token_cache *
1408 cp_token_cache_new (cp_token *first, cp_token *last)
1410 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1411 cache->first = first;
1412 cache->last = last;
1413 return cache;
1416 /* Diagnose if #pragma omp declare simd isn't followed immediately
1417 by function declaration or definition. */
1419 static inline void
1420 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1422 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1424 error ("%<#pragma omp declare %s%> not immediately followed by "
1425 "function declaration or definition",
1426 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1427 parser->omp_declare_simd = NULL;
1431 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1432 and put that into "omp declare simd" attribute. */
1434 static inline void
1435 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1437 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1439 if (fndecl == error_mark_node)
1441 parser->omp_declare_simd = NULL;
1442 return;
1444 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1446 cp_ensure_no_omp_declare_simd (parser);
1447 return;
1452 /* Similarly, but for use in declaration parsing functions
1453 which call cp_parser_handle_directive_omp_attributes. */
1455 static inline void
1456 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1458 if (parser->omp_declare_simd != data)
1459 return;
1461 if (!parser->omp_declare_simd->error_seen
1462 && !parser->omp_declare_simd->fndecl_seen)
1463 error_at (parser->omp_declare_simd->loc,
1464 "%<declare %s%> directive not immediately followed by "
1465 "function declaration or definition",
1466 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1467 parser->omp_declare_simd = NULL;
1470 /* Diagnose if #pragma acc routine isn't followed immediately by function
1471 declaration or definition. */
1473 static inline void
1474 cp_ensure_no_oacc_routine (cp_parser *parser)
1476 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1478 error_at (parser->oacc_routine->loc,
1479 "%<#pragma acc routine%> not immediately followed by "
1480 "function declaration or definition");
1481 parser->oacc_routine = NULL;
1485 /* Decl-specifiers. */
1487 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1489 static void
1490 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1492 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1495 /* Declarators. */
1497 /* Nothing other than the parser should be creating declarators;
1498 declarators are a semi-syntactic representation of C++ entities.
1499 Other parts of the front end that need to create entities (like
1500 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1502 static cp_declarator *make_call_declarator
1503 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1504 tree, tree, tree, tree, location_t);
1505 static cp_declarator *make_array_declarator
1506 (cp_declarator *, tree);
1507 static cp_declarator *make_pointer_declarator
1508 (cp_cv_quals, cp_declarator *, tree);
1509 static cp_declarator *make_reference_declarator
1510 (cp_cv_quals, cp_declarator *, bool, tree);
1511 static cp_declarator *make_ptrmem_declarator
1512 (cp_cv_quals, tree, cp_declarator *, tree);
1514 /* An erroneous declarator. */
1515 static cp_declarator *cp_error_declarator;
1517 /* The obstack on which declarators and related data structures are
1518 allocated. */
1519 static struct obstack declarator_obstack;
1521 /* Alloc BYTES from the declarator memory pool. */
1523 static inline void *
1524 alloc_declarator (size_t bytes)
1526 return obstack_alloc (&declarator_obstack, bytes);
1529 /* Allocate a declarator of the indicated KIND. Clear fields that are
1530 common to all declarators. */
1532 static cp_declarator *
1533 make_declarator (cp_declarator_kind kind)
1535 cp_declarator *declarator;
1537 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1538 declarator->kind = kind;
1539 declarator->parenthesized = UNKNOWN_LOCATION;
1540 declarator->attributes = NULL_TREE;
1541 declarator->std_attributes = NULL_TREE;
1542 declarator->declarator = NULL;
1543 declarator->parameter_pack_p = false;
1544 declarator->id_loc = UNKNOWN_LOCATION;
1545 declarator->init_loc = UNKNOWN_LOCATION;
1547 return declarator;
1550 /* Make a declarator for a generalized identifier. If
1551 QUALIFYING_SCOPE is non-NULL, the identifier is
1552 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1553 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1554 is, if any. */
1556 static cp_declarator *
1557 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1558 special_function_kind sfk, location_t id_location)
1560 cp_declarator *declarator;
1562 /* It is valid to write:
1564 class C { void f(); };
1565 typedef C D;
1566 void D::f();
1568 The standard is not clear about whether `typedef const C D' is
1569 legal; as of 2002-09-15 the committee is considering that
1570 question. EDG 3.0 allows that syntax. Therefore, we do as
1571 well. */
1572 if (qualifying_scope && TYPE_P (qualifying_scope))
1573 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1575 gcc_assert (identifier_p (unqualified_name)
1576 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1577 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1579 declarator = make_declarator (cdk_id);
1580 declarator->u.id.qualifying_scope = qualifying_scope;
1581 declarator->u.id.unqualified_name = unqualified_name;
1582 declarator->u.id.sfk = sfk;
1583 declarator->id_loc = id_location;
1585 return declarator;
1588 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1589 of modifiers such as const or volatile to apply to the pointer
1590 type, represented as identifiers. ATTRIBUTES represent the attributes that
1591 appertain to the pointer or reference. */
1593 cp_declarator *
1594 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1595 tree attributes)
1597 cp_declarator *declarator;
1599 declarator = make_declarator (cdk_pointer);
1600 declarator->declarator = target;
1601 declarator->u.pointer.qualifiers = cv_qualifiers;
1602 declarator->u.pointer.class_type = NULL_TREE;
1603 if (target)
1605 declarator->id_loc = target->id_loc;
1606 declarator->parameter_pack_p = target->parameter_pack_p;
1607 target->parameter_pack_p = false;
1609 else
1610 declarator->parameter_pack_p = false;
1612 declarator->std_attributes = attributes;
1614 return declarator;
1617 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1618 represent the attributes that appertain to the pointer or
1619 reference. */
1621 cp_declarator *
1622 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1623 bool rvalue_ref, tree attributes)
1625 cp_declarator *declarator;
1627 declarator = make_declarator (cdk_reference);
1628 declarator->declarator = target;
1629 declarator->u.reference.qualifiers = cv_qualifiers;
1630 declarator->u.reference.rvalue_ref = rvalue_ref;
1631 if (target)
1633 declarator->id_loc = target->id_loc;
1634 declarator->parameter_pack_p = target->parameter_pack_p;
1635 target->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 declarator->std_attributes = attributes;
1642 return declarator;
1645 /* Like make_pointer_declarator -- but for a pointer to a non-static
1646 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1647 appertain to the pointer or reference. */
1649 cp_declarator *
1650 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1651 cp_declarator *pointee,
1652 tree attributes)
1654 cp_declarator *declarator;
1656 declarator = make_declarator (cdk_ptrmem);
1657 declarator->declarator = pointee;
1658 declarator->u.pointer.qualifiers = cv_qualifiers;
1659 declarator->u.pointer.class_type = class_type;
1661 if (pointee)
1663 declarator->parameter_pack_p = pointee->parameter_pack_p;
1664 pointee->parameter_pack_p = false;
1666 else
1667 declarator->parameter_pack_p = false;
1669 declarator->std_attributes = attributes;
1671 return declarator;
1674 /* Make a declarator for the function given by TARGET, with the
1675 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1676 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1677 indicates what exceptions can be thrown. */
1679 cp_declarator *
1680 make_call_declarator (cp_declarator *target,
1681 tree parms,
1682 cp_cv_quals cv_qualifiers,
1683 cp_virt_specifiers virt_specifiers,
1684 cp_ref_qualifier ref_qualifier,
1685 tree tx_qualifier,
1686 tree exception_specification,
1687 tree late_return_type,
1688 tree requires_clause,
1689 location_t parens_loc)
1691 cp_declarator *declarator;
1693 declarator = make_declarator (cdk_function);
1694 declarator->declarator = target;
1695 declarator->u.function.parameters = parms;
1696 declarator->u.function.qualifiers = cv_qualifiers;
1697 declarator->u.function.virt_specifiers = virt_specifiers;
1698 declarator->u.function.ref_qualifier = ref_qualifier;
1699 declarator->u.function.tx_qualifier = tx_qualifier;
1700 declarator->u.function.exception_specification = exception_specification;
1701 declarator->u.function.late_return_type = late_return_type;
1702 declarator->u.function.requires_clause = requires_clause;
1703 declarator->u.function.parens_loc = parens_loc;
1704 if (target)
1706 declarator->id_loc = target->id_loc;
1707 declarator->parameter_pack_p = target->parameter_pack_p;
1708 target->parameter_pack_p = false;
1710 else
1711 declarator->parameter_pack_p = false;
1713 return declarator;
1716 /* Make a declarator for an array of BOUNDS elements, each of which is
1717 defined by ELEMENT. */
1719 cp_declarator *
1720 make_array_declarator (cp_declarator *element, tree bounds)
1722 cp_declarator *declarator;
1724 declarator = make_declarator (cdk_array);
1725 declarator->declarator = element;
1726 declarator->u.array.bounds = bounds;
1727 if (element)
1729 declarator->id_loc = element->id_loc;
1730 declarator->parameter_pack_p = element->parameter_pack_p;
1731 element->parameter_pack_p = false;
1733 else
1734 declarator->parameter_pack_p = false;
1736 return declarator;
1739 /* Determine whether the declarator we've seen so far can be a
1740 parameter pack, when followed by an ellipsis. */
1741 static bool
1742 declarator_can_be_parameter_pack (cp_declarator *declarator)
1744 if (declarator && declarator->parameter_pack_p)
1745 /* We already saw an ellipsis. */
1746 return false;
1748 /* Search for a declarator name, or any other declarator that goes
1749 after the point where the ellipsis could appear in a parameter
1750 pack. If we find any of these, then this declarator cannot be
1751 made into a parameter pack. */
1752 bool found = false;
1753 while (declarator && !found)
1755 switch ((int)declarator->kind)
1757 case cdk_id:
1758 case cdk_array:
1759 case cdk_decomp:
1760 found = true;
1761 break;
1763 case cdk_error:
1764 return true;
1766 default:
1767 declarator = declarator->declarator;
1768 break;
1772 return !found;
1775 cp_parameter_declarator *no_parameters;
1777 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1778 DECLARATOR and DEFAULT_ARGUMENT. */
1780 cp_parameter_declarator *
1781 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1782 cp_declarator *declarator,
1783 tree default_argument,
1784 location_t loc,
1785 bool template_parameter_pack_p = false)
1787 cp_parameter_declarator *parameter;
1789 parameter = ((cp_parameter_declarator *)
1790 alloc_declarator (sizeof (cp_parameter_declarator)));
1791 parameter->next = NULL;
1792 if (decl_specifiers)
1793 parameter->decl_specifiers = *decl_specifiers;
1794 else
1795 clear_decl_specs (&parameter->decl_specifiers);
1796 parameter->declarator = declarator;
1797 parameter->default_argument = default_argument;
1798 parameter->template_parameter_pack_p = template_parameter_pack_p;
1799 parameter->loc = loc;
1801 return parameter;
1804 /* Returns true iff DECLARATOR is a declaration for a function. */
1806 static bool
1807 function_declarator_p (const cp_declarator *declarator)
1809 while (declarator)
1811 if (declarator->kind == cdk_function
1812 && declarator->declarator->kind == cdk_id)
1813 return true;
1814 if (declarator->kind == cdk_id
1815 || declarator->kind == cdk_decomp
1816 || declarator->kind == cdk_error)
1817 return false;
1818 declarator = declarator->declarator;
1820 return false;
1823 /* The parser. */
1825 /* Overview
1826 --------
1828 A cp_parser parses the token stream as specified by the C++
1829 grammar. Its job is purely parsing, not semantic analysis. For
1830 example, the parser breaks the token stream into declarators,
1831 expressions, statements, and other similar syntactic constructs.
1832 It does not check that the types of the expressions on either side
1833 of an assignment-statement are compatible, or that a function is
1834 not declared with a parameter of type `void'.
1836 The parser invokes routines elsewhere in the compiler to perform
1837 semantic analysis and to build up the abstract syntax tree for the
1838 code processed.
1840 The parser (and the template instantiation code, which is, in a
1841 way, a close relative of parsing) are the only parts of the
1842 compiler that should be calling push_scope and pop_scope, or
1843 related functions. The parser (and template instantiation code)
1844 keeps track of what scope is presently active; everything else
1845 should simply honor that. (The code that generates static
1846 initializers may also need to set the scope, in order to check
1847 access control correctly when emitting the initializers.)
1849 Methodology
1850 -----------
1852 The parser is of the standard recursive-descent variety. Upcoming
1853 tokens in the token stream are examined in order to determine which
1854 production to use when parsing a non-terminal. Some C++ constructs
1855 require arbitrary look ahead to disambiguate. For example, it is
1856 impossible, in the general case, to tell whether a statement is an
1857 expression or declaration without scanning the entire statement.
1858 Therefore, the parser is capable of "parsing tentatively." When the
1859 parser is not sure what construct comes next, it enters this mode.
1860 Then, while we attempt to parse the construct, the parser queues up
1861 error messages, rather than issuing them immediately, and saves the
1862 tokens it consumes. If the construct is parsed successfully, the
1863 parser "commits", i.e., it issues any queued error messages and
1864 the tokens that were being preserved are permanently discarded.
1865 If, however, the construct is not parsed successfully, the parser
1866 rolls back its state completely so that it can resume parsing using
1867 a different alternative.
1869 Future Improvements
1870 -------------------
1872 The performance of the parser could probably be improved substantially.
1873 We could often eliminate the need to parse tentatively by looking ahead
1874 a little bit. In some places, this approach might not entirely eliminate
1875 the need to parse tentatively, but it might still speed up the average
1876 case. */
1878 /* Flags that are passed to some parsing functions. These values can
1879 be bitwise-ored together. */
1881 enum
1883 /* No flags. */
1884 CP_PARSER_FLAGS_NONE = 0x0,
1885 /* The construct is optional. If it is not present, then no error
1886 should be issued. */
1887 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1888 /* When parsing a type-specifier, treat user-defined type-names
1889 as non-type identifiers. */
1890 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1891 /* When parsing a type-specifier, do not try to parse a class-specifier
1892 or enum-specifier. */
1893 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1894 /* When parsing a decl-specifier-seq, only allow type-specifier or
1895 constexpr. */
1896 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1897 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1898 for C++20 consteval. */
1899 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1900 /* When parsing a decl-specifier-seq, allow missing typename. */
1901 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1902 /* When parsing of the noexcept-specifier should be delayed. */
1903 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1904 /* When parsing a consteval declarator. */
1905 CP_PARSER_FLAGS_CONSTEVAL = 0x80
1908 /* This type is used for parameters and variables which hold
1909 combinations of the above flags. */
1910 typedef int cp_parser_flags;
1912 /* The different kinds of declarators we want to parse. */
1914 enum cp_parser_declarator_kind
1916 /* We want an abstract declarator. */
1917 CP_PARSER_DECLARATOR_ABSTRACT,
1918 /* We want a named declarator. */
1919 CP_PARSER_DECLARATOR_NAMED,
1920 /* We don't mind, but the name must be an unqualified-id. */
1921 CP_PARSER_DECLARATOR_EITHER
1924 /* The precedence values used to parse binary expressions. The minimum value
1925 of PREC must be 1, because zero is reserved to quickly discriminate
1926 binary operators from other tokens. */
1928 enum cp_parser_prec
1930 PREC_NOT_OPERATOR,
1931 PREC_LOGICAL_OR_EXPRESSION,
1932 PREC_LOGICAL_AND_EXPRESSION,
1933 PREC_INCLUSIVE_OR_EXPRESSION,
1934 PREC_EXCLUSIVE_OR_EXPRESSION,
1935 PREC_AND_EXPRESSION,
1936 PREC_EQUALITY_EXPRESSION,
1937 PREC_RELATIONAL_EXPRESSION,
1938 PREC_SPACESHIP_EXPRESSION,
1939 PREC_SHIFT_EXPRESSION,
1940 PREC_ADDITIVE_EXPRESSION,
1941 PREC_MULTIPLICATIVE_EXPRESSION,
1942 PREC_PM_EXPRESSION,
1943 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1946 /* A mapping from a token type to a corresponding tree node type, with a
1947 precedence value. */
1949 struct cp_parser_binary_operations_map_node
1951 /* The token type. */
1952 enum cpp_ttype token_type;
1953 /* The corresponding tree code. */
1954 enum tree_code tree_type;
1955 /* The precedence of this operator. */
1956 enum cp_parser_prec prec;
1959 struct cp_parser_expression_stack_entry
1961 /* Left hand side of the binary operation we are currently
1962 parsing. */
1963 cp_expr lhs;
1964 /* Original tree code for left hand side, if it was a binary
1965 expression itself (used for -Wparentheses). */
1966 enum tree_code lhs_type;
1967 /* Tree code for the binary operation we are parsing. */
1968 enum tree_code tree_type;
1969 /* Precedence of the binary operation we are parsing. */
1970 enum cp_parser_prec prec;
1971 /* Location of the binary operation we are parsing. */
1972 location_t loc;
1975 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1976 entries because precedence levels on the stack are monotonically
1977 increasing. */
1978 typedef struct cp_parser_expression_stack_entry
1979 cp_parser_expression_stack[NUM_PREC_VALUES];
1981 /* Prototypes. */
1983 /* Constructors and destructors. */
1985 static cp_parser_context *cp_parser_context_new
1986 (cp_parser_context *);
1988 /* Class variables. */
1990 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1992 /* The operator-precedence table used by cp_parser_binary_expression.
1993 Transformed into an associative array (binops_by_token) by
1994 cp_parser_new. */
1996 static const cp_parser_binary_operations_map_node binops[] = {
1997 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1998 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2000 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2001 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2002 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2004 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2005 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2007 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2008 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2010 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2012 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2013 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2014 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2015 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2017 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2018 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2020 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2022 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2024 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2026 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2028 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2031 /* The same as binops, but initialized by cp_parser_new so that
2032 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2033 for speed. */
2034 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2036 /* Constructors and destructors. */
2038 /* Construct a new context. The context below this one on the stack
2039 is given by NEXT. */
2041 static cp_parser_context *
2042 cp_parser_context_new (cp_parser_context* next)
2044 cp_parser_context *context;
2046 /* Allocate the storage. */
2047 if (cp_parser_context_free_list != NULL)
2049 /* Pull the first entry from the free list. */
2050 context = cp_parser_context_free_list;
2051 cp_parser_context_free_list = context->next;
2052 memset (context, 0, sizeof (*context));
2054 else
2055 context = ggc_cleared_alloc<cp_parser_context> ();
2057 /* No errors have occurred yet in this context. */
2058 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2059 /* If this is not the bottommost context, copy information that we
2060 need from the previous context. */
2061 if (next)
2063 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2064 expression, then we are parsing one in this context, too. */
2065 context->object_type = next->object_type;
2066 /* Thread the stack. */
2067 context->next = next;
2070 return context;
2073 /* Managing the unparsed function queues. */
2075 #define unparsed_funs_with_default_args \
2076 parser->unparsed_queues->last ().funs_with_default_args
2077 #define unparsed_funs_with_definitions \
2078 parser->unparsed_queues->last ().funs_with_definitions
2079 #define unparsed_nsdmis \
2080 parser->unparsed_queues->last ().nsdmis
2081 #define unparsed_noexcepts \
2082 parser->unparsed_queues->last ().noexcepts
2084 static void
2085 push_unparsed_function_queues (cp_parser *parser)
2087 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2088 vec_safe_push (parser->unparsed_queues, e);
2091 static void
2092 pop_unparsed_function_queues (cp_parser *parser)
2094 release_tree_vector (unparsed_funs_with_definitions);
2095 parser->unparsed_queues->pop ();
2098 /* Prototypes. */
2100 /* Constructors and destructors. */
2102 static cp_parser *cp_parser_new
2103 (cp_lexer *);
2105 /* Routines to parse various constructs.
2107 Those that return `tree' will return the error_mark_node (rather
2108 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2109 Sometimes, they will return an ordinary node if error-recovery was
2110 attempted, even though a parse error occurred. So, to check
2111 whether or not a parse error occurred, you should always use
2112 cp_parser_error_occurred. If the construct is optional (indicated
2113 either by an `_opt' in the name of the function that does the
2114 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2115 the construct is not present. */
2117 /* Lexical conventions [gram.lex] */
2119 static cp_expr cp_parser_identifier
2120 (cp_parser *);
2121 static cp_expr cp_parser_string_literal
2122 (cp_parser *, bool, bool, bool);
2123 static cp_expr cp_parser_userdef_char_literal
2124 (cp_parser *);
2125 static tree cp_parser_userdef_string_literal
2126 (tree);
2127 static cp_expr cp_parser_userdef_numeric_literal
2128 (cp_parser *);
2130 /* Basic concepts [gram.basic] */
2132 static void cp_parser_translation_unit (cp_parser *);
2134 /* Expressions [gram.expr] */
2136 static cp_expr cp_parser_primary_expression
2137 (cp_parser *, bool, bool, bool, cp_id_kind *);
2138 static cp_expr cp_parser_id_expression
2139 (cp_parser *, bool, bool, bool *, bool, bool);
2140 static cp_expr cp_parser_unqualified_id
2141 (cp_parser *, bool, bool, bool, bool);
2142 static tree cp_parser_nested_name_specifier_opt
2143 (cp_parser *, bool, bool, bool, bool, bool = false);
2144 static tree cp_parser_nested_name_specifier
2145 (cp_parser *, bool, bool, bool, bool);
2146 static tree cp_parser_qualifying_entity
2147 (cp_parser *, bool, bool, bool, bool, bool);
2148 static cp_expr cp_parser_postfix_expression
2149 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2150 static tree cp_parser_postfix_open_square_expression
2151 (cp_parser *, tree, bool, bool);
2152 static tree cp_parser_postfix_dot_deref_expression
2153 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2154 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2155 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2156 bool = false);
2157 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2158 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2159 static void cp_parser_pseudo_destructor_name
2160 (cp_parser *, tree, tree *, tree *);
2161 static cp_expr cp_parser_unary_expression
2162 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2163 static enum tree_code cp_parser_unary_operator
2164 (cp_token *);
2165 static tree cp_parser_has_attribute_expression
2166 (cp_parser *);
2167 static tree cp_parser_new_expression
2168 (cp_parser *);
2169 static vec<tree, va_gc> *cp_parser_new_placement
2170 (cp_parser *);
2171 static tree cp_parser_new_type_id
2172 (cp_parser *, tree *);
2173 static cp_declarator *cp_parser_new_declarator_opt
2174 (cp_parser *);
2175 static cp_declarator *cp_parser_direct_new_declarator
2176 (cp_parser *);
2177 static vec<tree, va_gc> *cp_parser_new_initializer
2178 (cp_parser *);
2179 static tree cp_parser_delete_expression
2180 (cp_parser *);
2181 static cp_expr cp_parser_cast_expression
2182 (cp_parser *, bool, bool, bool, cp_id_kind *);
2183 static cp_expr cp_parser_binary_expression
2184 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2185 static tree cp_parser_question_colon_clause
2186 (cp_parser *, cp_expr);
2187 static cp_expr cp_parser_assignment_expression
2188 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2189 static enum tree_code cp_parser_assignment_operator_opt
2190 (cp_parser *);
2191 static cp_expr cp_parser_expression
2192 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2193 static cp_expr cp_parser_constant_expression
2194 (cp_parser *, int = 0, bool * = NULL, bool = false);
2195 static cp_expr cp_parser_builtin_offsetof
2196 (cp_parser *);
2197 static cp_expr cp_parser_lambda_expression
2198 (cp_parser *);
2199 static void cp_parser_lambda_introducer
2200 (cp_parser *, tree);
2201 static bool cp_parser_lambda_declarator_opt
2202 (cp_parser *, tree);
2203 static void cp_parser_lambda_body
2204 (cp_parser *, tree);
2206 /* Statements [gram.stmt.stmt] */
2208 static void cp_parser_statement
2209 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2210 static void cp_parser_label_for_labeled_statement
2211 (cp_parser *, tree);
2212 static tree cp_parser_expression_statement
2213 (cp_parser *, tree);
2214 static tree cp_parser_compound_statement
2215 (cp_parser *, tree, int, bool);
2216 static void cp_parser_statement_seq_opt
2217 (cp_parser *, tree);
2218 static tree cp_parser_selection_statement
2219 (cp_parser *, bool *, vec<tree> *);
2220 static tree cp_parser_condition
2221 (cp_parser *);
2222 static tree cp_parser_iteration_statement
2223 (cp_parser *, bool *, bool, unsigned short);
2224 static bool cp_parser_init_statement
2225 (cp_parser *, tree *decl);
2226 static tree cp_parser_for
2227 (cp_parser *, bool, unsigned short);
2228 static tree cp_parser_c_for
2229 (cp_parser *, tree, tree, bool, unsigned short);
2230 static tree cp_parser_range_for
2231 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2232 static void do_range_for_auto_deduction
2233 (tree, tree);
2234 static tree cp_parser_perform_range_for_lookup
2235 (tree, tree *, tree *);
2236 static tree cp_parser_range_for_member_function
2237 (tree, tree);
2238 static tree cp_parser_jump_statement
2239 (cp_parser *);
2240 static void cp_parser_declaration_statement
2241 (cp_parser *);
2243 static tree cp_parser_implicitly_scoped_statement
2244 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2245 static void cp_parser_already_scoped_statement
2246 (cp_parser *, bool *, const token_indent_info &);
2248 /* State of module-declaration parsing. */
2249 enum module_parse
2251 MP_NOT_MODULE, /* Not a module. */
2253 _MP_UNUSED,
2255 MP_FIRST, /* First declaration of TU. */
2256 MP_GLOBAL, /* Global Module Fragment. */
2258 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2259 MP_PURVIEW, /* Purview of a named module. */
2261 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2262 MP_PRIVATE, /* Private Module Fragment. */
2265 static module_parse cp_parser_module_declaration
2266 (cp_parser *parser, module_parse, bool exporting);
2267 static void cp_parser_import_declaration
2268 (cp_parser *parser, module_parse, bool exporting);
2270 /* Declarations [gram.dcl.dcl] */
2272 static void cp_parser_declaration_seq_opt
2273 (cp_parser *);
2274 static void cp_parser_declaration
2275 (cp_parser *, tree);
2276 static void cp_parser_toplevel_declaration
2277 (cp_parser *);
2278 static void cp_parser_block_declaration
2279 (cp_parser *, bool);
2280 static void cp_parser_simple_declaration
2281 (cp_parser *, bool, tree *);
2282 static void cp_parser_decl_specifier_seq
2283 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2284 static tree cp_parser_storage_class_specifier_opt
2285 (cp_parser *);
2286 static tree cp_parser_function_specifier_opt
2287 (cp_parser *, cp_decl_specifier_seq *);
2288 static tree cp_parser_type_specifier
2289 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2290 int *, bool *);
2291 static tree cp_parser_simple_type_specifier
2292 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2293 static tree cp_parser_placeholder_type_specifier
2294 (cp_parser *, location_t, tree, bool);
2295 static tree cp_parser_type_name
2296 (cp_parser *, bool);
2297 static tree cp_parser_nonclass_name
2298 (cp_parser* parser);
2299 static tree cp_parser_elaborated_type_specifier
2300 (cp_parser *, bool, bool);
2301 static tree cp_parser_enum_specifier
2302 (cp_parser *);
2303 static void cp_parser_enumerator_list
2304 (cp_parser *, tree);
2305 static void cp_parser_enumerator_definition
2306 (cp_parser *, tree);
2307 static tree cp_parser_namespace_name
2308 (cp_parser *);
2309 static void cp_parser_namespace_definition
2310 (cp_parser *);
2311 static void cp_parser_namespace_body
2312 (cp_parser *);
2313 static tree cp_parser_qualified_namespace_specifier
2314 (cp_parser *);
2315 static void cp_parser_namespace_alias_definition
2316 (cp_parser *);
2317 static bool cp_parser_using_declaration
2318 (cp_parser *, bool);
2319 static void cp_parser_using_directive
2320 (cp_parser *);
2321 static void cp_parser_using_enum
2322 (cp_parser *);
2323 static tree cp_parser_alias_declaration
2324 (cp_parser *);
2325 static void cp_parser_asm_definition
2326 (cp_parser *);
2327 static void cp_parser_linkage_specification
2328 (cp_parser *, tree);
2329 static void cp_parser_static_assert
2330 (cp_parser *, bool);
2331 static tree cp_parser_decltype
2332 (cp_parser *);
2333 static tree cp_parser_decomposition_declaration
2334 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2336 /* Declarators [gram.dcl.decl] */
2338 static tree cp_parser_init_declarator
2339 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2340 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2341 location_t *, tree *);
2342 static cp_declarator *cp_parser_declarator
2343 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2344 bool, bool, bool);
2345 static cp_declarator *cp_parser_direct_declarator
2346 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2347 bool);
2348 static enum tree_code cp_parser_ptr_operator
2349 (cp_parser *, tree *, cp_cv_quals *, tree *);
2350 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2351 (cp_parser *);
2352 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2353 (cp_parser *);
2354 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2355 (cp_parser *);
2356 static tree cp_parser_tx_qualifier_opt
2357 (cp_parser *);
2358 static tree cp_parser_late_return_type_opt
2359 (cp_parser *, cp_declarator *, tree &);
2360 static tree cp_parser_declarator_id
2361 (cp_parser *, bool);
2362 static tree cp_parser_type_id
2363 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2364 static tree cp_parser_template_type_arg
2365 (cp_parser *);
2366 static tree cp_parser_trailing_type_id (cp_parser *);
2367 static tree cp_parser_type_id_1
2368 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2369 static void cp_parser_type_specifier_seq
2370 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2371 static tree cp_parser_parameter_declaration_clause
2372 (cp_parser *, cp_parser_flags);
2373 static tree cp_parser_parameter_declaration_list
2374 (cp_parser *, cp_parser_flags);
2375 static cp_parameter_declarator *cp_parser_parameter_declaration
2376 (cp_parser *, cp_parser_flags, bool, bool *);
2377 static tree cp_parser_default_argument
2378 (cp_parser *, bool);
2379 static void cp_parser_function_body
2380 (cp_parser *, bool);
2381 static tree cp_parser_initializer
2382 (cp_parser *, bool *, bool *, bool = false);
2383 static cp_expr cp_parser_initializer_clause
2384 (cp_parser *, bool *);
2385 static cp_expr cp_parser_braced_list
2386 (cp_parser*, bool*);
2387 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2388 (cp_parser *, bool *, bool *);
2390 static void cp_parser_ctor_initializer_opt_and_function_body
2391 (cp_parser *, bool);
2393 static tree cp_parser_late_parsing_omp_declare_simd
2394 (cp_parser *, tree);
2396 static tree cp_parser_late_parsing_oacc_routine
2397 (cp_parser *, tree);
2399 static tree synthesize_implicit_template_parm
2400 (cp_parser *, tree);
2401 static tree finish_fully_implicit_template
2402 (cp_parser *, tree);
2403 static void abort_fully_implicit_template
2404 (cp_parser *);
2406 /* Classes [gram.class] */
2408 static tree cp_parser_class_name
2409 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2410 static tree cp_parser_class_specifier
2411 (cp_parser *);
2412 static tree cp_parser_class_head
2413 (cp_parser *, bool *);
2414 static enum tag_types cp_parser_class_key
2415 (cp_parser *);
2416 static void cp_parser_type_parameter_key
2417 (cp_parser* parser);
2418 static void cp_parser_member_specification_opt
2419 (cp_parser *);
2420 static void cp_parser_member_declaration
2421 (cp_parser *);
2422 static tree cp_parser_pure_specifier
2423 (cp_parser *);
2424 static tree cp_parser_constant_initializer
2425 (cp_parser *);
2427 /* Derived classes [gram.class.derived] */
2429 static tree cp_parser_base_clause
2430 (cp_parser *);
2431 static tree cp_parser_base_specifier
2432 (cp_parser *);
2434 /* Special member functions [gram.special] */
2436 static tree cp_parser_conversion_function_id
2437 (cp_parser *);
2438 static tree cp_parser_conversion_type_id
2439 (cp_parser *);
2440 static cp_declarator *cp_parser_conversion_declarator_opt
2441 (cp_parser *);
2442 static void cp_parser_ctor_initializer_opt
2443 (cp_parser *);
2444 static void cp_parser_mem_initializer_list
2445 (cp_parser *);
2446 static tree cp_parser_mem_initializer
2447 (cp_parser *);
2448 static tree cp_parser_mem_initializer_id
2449 (cp_parser *);
2451 /* Overloading [gram.over] */
2453 static cp_expr cp_parser_operator_function_id
2454 (cp_parser *);
2455 static cp_expr cp_parser_operator
2456 (cp_parser *, location_t);
2458 /* Templates [gram.temp] */
2460 static void cp_parser_template_declaration
2461 (cp_parser *, bool);
2462 static tree cp_parser_template_parameter_list
2463 (cp_parser *);
2464 static tree cp_parser_template_parameter
2465 (cp_parser *, bool *, bool *);
2466 static tree cp_parser_type_parameter
2467 (cp_parser *, bool *);
2468 static tree cp_parser_template_id
2469 (cp_parser *, bool, bool, enum tag_types, bool);
2470 static tree cp_parser_template_id_expr
2471 (cp_parser *, bool, bool, bool);
2472 static tree cp_parser_template_name
2473 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2474 static tree cp_parser_template_argument_list
2475 (cp_parser *);
2476 static tree cp_parser_template_argument
2477 (cp_parser *);
2478 static void cp_parser_explicit_instantiation
2479 (cp_parser *);
2480 static void cp_parser_explicit_specialization
2481 (cp_parser *);
2483 /* Exception handling [gram.except] */
2485 static tree cp_parser_try_block
2486 (cp_parser *);
2487 static void cp_parser_function_try_block
2488 (cp_parser *);
2489 static void cp_parser_handler_seq
2490 (cp_parser *);
2491 static void cp_parser_handler
2492 (cp_parser *);
2493 static tree cp_parser_exception_declaration
2494 (cp_parser *);
2495 static tree cp_parser_throw_expression
2496 (cp_parser *);
2497 static tree cp_parser_exception_specification_opt
2498 (cp_parser *, cp_parser_flags);
2499 static tree cp_parser_type_id_list
2500 (cp_parser *);
2501 static tree cp_parser_noexcept_specification_opt
2502 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2504 /* GNU Extensions */
2506 static tree cp_parser_asm_specification_opt
2507 (cp_parser *);
2508 static tree cp_parser_asm_operand_list
2509 (cp_parser *);
2510 static tree cp_parser_asm_clobber_list
2511 (cp_parser *);
2512 static tree cp_parser_asm_label_list
2513 (cp_parser *);
2514 static bool cp_next_tokens_can_be_attribute_p
2515 (cp_parser *);
2516 static bool cp_next_tokens_can_be_gnu_attribute_p
2517 (cp_parser *);
2518 static bool cp_next_tokens_can_be_std_attribute_p
2519 (cp_parser *);
2520 static bool cp_nth_tokens_can_be_std_attribute_p
2521 (cp_parser *, size_t);
2522 static bool cp_nth_tokens_can_be_gnu_attribute_p
2523 (cp_parser *, size_t);
2524 static bool cp_nth_tokens_can_be_attribute_p
2525 (cp_parser *, size_t);
2526 static tree cp_parser_attributes_opt
2527 (cp_parser *);
2528 static tree cp_parser_gnu_attributes_opt
2529 (cp_parser *);
2530 static tree cp_parser_gnu_attribute_list
2531 (cp_parser *, bool = false);
2532 static tree cp_parser_std_attribute
2533 (cp_parser *, tree);
2534 static tree cp_parser_std_attribute_spec
2535 (cp_parser *);
2536 static tree cp_parser_std_attribute_spec_seq
2537 (cp_parser *);
2538 static size_t cp_parser_skip_std_attribute_spec_seq
2539 (cp_parser *, size_t);
2540 static size_t cp_parser_skip_attributes_opt
2541 (cp_parser *, size_t);
2542 static bool cp_parser_extension_opt
2543 (cp_parser *, int *);
2544 static void cp_parser_label_declaration
2545 (cp_parser *);
2547 /* Concept Extensions */
2549 static tree cp_parser_concept_definition
2550 (cp_parser *);
2551 static tree cp_parser_constraint_expression
2552 (cp_parser *);
2553 static tree cp_parser_requires_clause_opt
2554 (cp_parser *, bool);
2555 static tree cp_parser_requires_expression
2556 (cp_parser *);
2557 static tree cp_parser_requirement_parameter_list
2558 (cp_parser *);
2559 static tree cp_parser_requirement_body
2560 (cp_parser *);
2561 static tree cp_parser_requirement_seq
2562 (cp_parser *);
2563 static tree cp_parser_requirement
2564 (cp_parser *);
2565 static tree cp_parser_simple_requirement
2566 (cp_parser *);
2567 static tree cp_parser_compound_requirement
2568 (cp_parser *);
2569 static tree cp_parser_type_requirement
2570 (cp_parser *);
2571 static tree cp_parser_nested_requirement
2572 (cp_parser *);
2574 /* Transactional Memory Extensions */
2576 static tree cp_parser_transaction
2577 (cp_parser *, cp_token *);
2578 static tree cp_parser_transaction_expression
2579 (cp_parser *, enum rid);
2580 static void cp_parser_function_transaction
2581 (cp_parser *, enum rid);
2582 static tree cp_parser_transaction_cancel
2583 (cp_parser *);
2585 /* Coroutine extensions. */
2587 static tree cp_parser_yield_expression
2588 (cp_parser *);
2591 enum pragma_context {
2592 pragma_external,
2593 pragma_member,
2594 pragma_objc_icode,
2595 pragma_stmt,
2596 pragma_compound
2598 static bool cp_parser_pragma
2599 (cp_parser *, enum pragma_context, bool *);
2601 /* Objective-C++ Productions */
2603 static tree cp_parser_objc_message_receiver
2604 (cp_parser *);
2605 static tree cp_parser_objc_message_args
2606 (cp_parser *);
2607 static tree cp_parser_objc_message_expression
2608 (cp_parser *);
2609 static cp_expr cp_parser_objc_encode_expression
2610 (cp_parser *);
2611 static tree cp_parser_objc_defs_expression
2612 (cp_parser *);
2613 static tree cp_parser_objc_protocol_expression
2614 (cp_parser *);
2615 static tree cp_parser_objc_selector_expression
2616 (cp_parser *);
2617 static cp_expr cp_parser_objc_expression
2618 (cp_parser *);
2619 static bool cp_parser_objc_selector_p
2620 (enum cpp_ttype);
2621 static tree cp_parser_objc_selector
2622 (cp_parser *);
2623 static tree cp_parser_objc_protocol_refs_opt
2624 (cp_parser *);
2625 static void cp_parser_objc_declaration
2626 (cp_parser *, tree);
2627 static tree cp_parser_objc_statement
2628 (cp_parser *);
2629 static bool cp_parser_objc_valid_prefix_attributes
2630 (cp_parser *, tree *);
2631 static void cp_parser_objc_at_property_declaration
2632 (cp_parser *) ;
2633 static void cp_parser_objc_at_synthesize_declaration
2634 (cp_parser *) ;
2635 static void cp_parser_objc_at_dynamic_declaration
2636 (cp_parser *) ;
2637 static tree cp_parser_objc_struct_declaration
2638 (cp_parser *) ;
2640 /* Utility Routines */
2642 static cp_expr cp_parser_lookup_name
2643 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2644 static tree cp_parser_lookup_name_simple
2645 (cp_parser *, tree, location_t);
2646 static tree cp_parser_maybe_treat_template_as_class
2647 (tree, bool);
2648 static bool cp_parser_check_declarator_template_parameters
2649 (cp_parser *, cp_declarator *, location_t);
2650 static bool cp_parser_check_template_parameters
2651 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2652 static cp_expr cp_parser_simple_cast_expression
2653 (cp_parser *);
2654 static tree cp_parser_global_scope_opt
2655 (cp_parser *, bool);
2656 static bool cp_parser_constructor_declarator_p
2657 (cp_parser *, cp_parser_flags, bool);
2658 static tree cp_parser_function_definition_from_specifiers_and_declarator
2659 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2660 static tree cp_parser_function_definition_after_declarator
2661 (cp_parser *, bool);
2662 static bool cp_parser_template_declaration_after_export
2663 (cp_parser *, bool);
2664 static void cp_parser_perform_template_parameter_access_checks
2665 (vec<deferred_access_check, va_gc> *);
2666 static tree cp_parser_single_declaration
2667 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2668 static cp_expr cp_parser_functional_cast
2669 (cp_parser *, tree);
2670 static tree cp_parser_save_member_function_body
2671 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2672 static tree cp_parser_save_nsdmi
2673 (cp_parser *);
2674 static tree cp_parser_enclosed_template_argument_list
2675 (cp_parser *);
2676 static void cp_parser_save_default_args
2677 (cp_parser *, tree);
2678 static void cp_parser_late_parsing_for_member
2679 (cp_parser *, tree);
2680 static tree cp_parser_late_parse_one_default_arg
2681 (cp_parser *, tree, tree, tree);
2682 static void cp_parser_late_parsing_nsdmi
2683 (cp_parser *, tree);
2684 static void cp_parser_late_parsing_default_args
2685 (cp_parser *, tree);
2686 static tree cp_parser_sizeof_operand
2687 (cp_parser *, enum rid);
2688 static cp_expr cp_parser_trait_expr
2689 (cp_parser *, enum rid);
2690 static bool cp_parser_declares_only_class_p
2691 (cp_parser *);
2692 static void cp_parser_set_storage_class
2693 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2694 static void cp_parser_set_decl_spec_type
2695 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2696 static void set_and_check_decl_spec_loc
2697 (cp_decl_specifier_seq *decl_specs,
2698 cp_decl_spec ds, cp_token *);
2699 static bool cp_parser_friend_p
2700 (const cp_decl_specifier_seq *);
2701 static void cp_parser_required_error
2702 (cp_parser *, required_token, bool, location_t);
2703 static cp_token *cp_parser_require
2704 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2705 static cp_token *cp_parser_require_keyword
2706 (cp_parser *, enum rid, required_token);
2707 static bool cp_parser_token_starts_function_definition_p
2708 (cp_token *);
2709 static bool cp_parser_next_token_starts_class_definition_p
2710 (cp_parser *);
2711 static bool cp_parser_next_token_ends_template_argument_p
2712 (cp_parser *);
2713 static bool cp_parser_nth_token_starts_template_argument_list_p
2714 (cp_parser *, size_t);
2715 static enum tag_types cp_parser_token_is_class_key
2716 (cp_token *);
2717 static enum tag_types cp_parser_token_is_type_parameter_key
2718 (cp_token *);
2719 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2720 static void cp_parser_check_class_key
2721 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2722 static void cp_parser_check_access_in_redeclaration
2723 (tree type, location_t location);
2724 static bool cp_parser_optional_template_keyword
2725 (cp_parser *);
2726 static void cp_parser_pre_parsed_nested_name_specifier
2727 (cp_parser *);
2728 static bool cp_parser_cache_group
2729 (cp_parser *, enum cpp_ttype, unsigned);
2730 static tree cp_parser_cache_defarg
2731 (cp_parser *parser, bool nsdmi);
2732 static void cp_parser_parse_tentatively
2733 (cp_parser *);
2734 static void cp_parser_commit_to_tentative_parse
2735 (cp_parser *);
2736 static void cp_parser_commit_to_topmost_tentative_parse
2737 (cp_parser *);
2738 static void cp_parser_abort_tentative_parse
2739 (cp_parser *);
2740 static bool cp_parser_parse_definitely
2741 (cp_parser *);
2742 static inline bool cp_parser_parsing_tentatively
2743 (cp_parser *);
2744 static bool cp_parser_uncommitted_to_tentative_parse_p
2745 (cp_parser *);
2746 static void cp_parser_error
2747 (cp_parser *, const char *);
2748 static void cp_parser_name_lookup_error
2749 (cp_parser *, tree, tree, name_lookup_error, location_t);
2750 static bool cp_parser_simulate_error
2751 (cp_parser *);
2752 static bool cp_parser_check_type_definition
2753 (cp_parser *);
2754 static void cp_parser_check_for_definition_in_return_type
2755 (cp_declarator *, tree, location_t type_location);
2756 static void cp_parser_check_for_invalid_template_id
2757 (cp_parser *, tree, enum tag_types, location_t location);
2758 static bool cp_parser_non_integral_constant_expression
2759 (cp_parser *, non_integral_constant);
2760 static void cp_parser_diagnose_invalid_type_name
2761 (cp_parser *, tree, location_t);
2762 static bool cp_parser_parse_and_diagnose_invalid_type_name
2763 (cp_parser *);
2764 static int cp_parser_skip_to_closing_parenthesis
2765 (cp_parser *, bool, bool, bool);
2766 static void cp_parser_skip_to_end_of_statement
2767 (cp_parser *);
2768 static void cp_parser_consume_semicolon_at_end_of_statement
2769 (cp_parser *);
2770 static void cp_parser_skip_to_end_of_block_or_statement
2771 (cp_parser *);
2772 static bool cp_parser_skip_to_closing_brace
2773 (cp_parser *);
2774 static void cp_parser_skip_to_end_of_template_parameter_list
2775 (cp_parser *);
2776 static void cp_parser_skip_to_pragma_eol
2777 (cp_parser*, cp_token *);
2778 static bool cp_parser_error_occurred
2779 (cp_parser *);
2780 static bool cp_parser_allow_gnu_extensions_p
2781 (cp_parser *);
2782 static bool cp_parser_is_pure_string_literal
2783 (cp_token *);
2784 static bool cp_parser_is_string_literal
2785 (cp_token *);
2786 static bool cp_parser_is_keyword
2787 (cp_token *, enum rid);
2788 static tree cp_parser_make_typename_type
2789 (cp_parser *, tree, location_t location);
2790 static cp_declarator * cp_parser_make_indirect_declarator
2791 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2792 static bool cp_parser_compound_literal_p
2793 (cp_parser *);
2794 static bool cp_parser_array_designator_p
2795 (cp_parser *);
2796 static bool cp_parser_init_statement_p
2797 (cp_parser *);
2798 static bool cp_parser_skip_to_closing_square_bracket
2799 (cp_parser *);
2800 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2802 // -------------------------------------------------------------------------- //
2803 // Unevaluated Operand Guard
2805 // Implementation of an RAII helper for unevaluated operand parsing.
2806 cp_unevaluated::cp_unevaluated ()
2808 ++cp_unevaluated_operand;
2809 ++c_inhibit_evaluation_warnings;
2812 cp_unevaluated::~cp_unevaluated ()
2814 --c_inhibit_evaluation_warnings;
2815 --cp_unevaluated_operand;
2818 // -------------------------------------------------------------------------- //
2819 // Tentative Parsing
2821 /* Returns nonzero if we are parsing tentatively. */
2823 static inline bool
2824 cp_parser_parsing_tentatively (cp_parser* parser)
2826 return parser->context->next != NULL;
2829 /* Returns nonzero if TOKEN is a string literal. */
2831 static bool
2832 cp_parser_is_pure_string_literal (cp_token* token)
2834 return (token->type == CPP_STRING ||
2835 token->type == CPP_STRING16 ||
2836 token->type == CPP_STRING32 ||
2837 token->type == CPP_WSTRING ||
2838 token->type == CPP_UTF8STRING);
2841 /* Returns nonzero if TOKEN is a string literal
2842 of a user-defined string literal. */
2844 static bool
2845 cp_parser_is_string_literal (cp_token* token)
2847 return (cp_parser_is_pure_string_literal (token) ||
2848 token->type == CPP_STRING_USERDEF ||
2849 token->type == CPP_STRING16_USERDEF ||
2850 token->type == CPP_STRING32_USERDEF ||
2851 token->type == CPP_WSTRING_USERDEF ||
2852 token->type == CPP_UTF8STRING_USERDEF);
2855 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2857 static bool
2858 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2860 return token->keyword == keyword;
2863 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2864 PRAGMA_NONE. */
2866 static enum pragma_kind
2867 cp_parser_pragma_kind (cp_token *token)
2869 if (token->type != CPP_PRAGMA)
2870 return PRAGMA_NONE;
2871 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2872 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2875 /* Helper function for cp_parser_error.
2876 Having peeked a token of kind TOK1_KIND that might signify
2877 a conflict marker, peek successor tokens to determine
2878 if we actually do have a conflict marker.
2879 Specifically, we consider a run of 7 '<', '=' or '>' characters
2880 at the start of a line as a conflict marker.
2881 These come through the lexer as three pairs and a single,
2882 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2883 If it returns true, *OUT_LOC is written to with the location/range
2884 of the marker. */
2886 static bool
2887 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2888 location_t *out_loc)
2890 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2891 if (token2->type != tok1_kind)
2892 return false;
2893 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2894 if (token3->type != tok1_kind)
2895 return false;
2896 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2897 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2898 return false;
2900 /* It must be at the start of the line. */
2901 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2902 if (LOCATION_COLUMN (start_loc) != 1)
2903 return false;
2905 /* We have a conflict marker. Construct a location of the form:
2906 <<<<<<<
2907 ^~~~~~~
2908 with start == caret, finishing at the end of the marker. */
2909 location_t finish_loc = get_finish (token4->location);
2910 *out_loc = make_location (start_loc, start_loc, finish_loc);
2912 return true;
2915 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2916 RT_CLOSE_PAREN. */
2918 static const char *
2919 get_matching_symbol (required_token token_desc)
2921 switch (token_desc)
2923 default:
2924 gcc_unreachable ();
2925 return "";
2926 case RT_CLOSE_BRACE:
2927 return "{";
2928 case RT_CLOSE_PAREN:
2929 return "(";
2933 /* Attempt to convert TOKEN_DESC from a required_token to an
2934 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2936 static enum cpp_ttype
2937 get_required_cpp_ttype (required_token token_desc)
2939 switch (token_desc)
2941 case RT_SEMICOLON:
2942 return CPP_SEMICOLON;
2943 case RT_OPEN_PAREN:
2944 return CPP_OPEN_PAREN;
2945 case RT_CLOSE_BRACE:
2946 return CPP_CLOSE_BRACE;
2947 case RT_OPEN_BRACE:
2948 return CPP_OPEN_BRACE;
2949 case RT_CLOSE_SQUARE:
2950 return CPP_CLOSE_SQUARE;
2951 case RT_OPEN_SQUARE:
2952 return CPP_OPEN_SQUARE;
2953 case RT_COMMA:
2954 return CPP_COMMA;
2955 case RT_COLON:
2956 return CPP_COLON;
2957 case RT_CLOSE_PAREN:
2958 return CPP_CLOSE_PAREN;
2960 default:
2961 /* Use CPP_EOF as a "no completions possible" code. */
2962 return CPP_EOF;
2967 /* Subroutine of cp_parser_error and cp_parser_required_error.
2969 Issue a diagnostic of the form
2970 FILE:LINE: MESSAGE before TOKEN
2971 where TOKEN is the next token in the input stream. MESSAGE
2972 (specified by the caller) is usually of the form "expected
2973 OTHER-TOKEN".
2975 This bypasses the check for tentative passing, and potentially
2976 adds material needed by cp_parser_required_error.
2978 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2979 suggesting insertion of the missing token.
2981 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2982 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2983 location. */
2985 static void
2986 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2987 required_token missing_token_desc,
2988 location_t matching_location)
2990 cp_token *token = cp_lexer_peek_token (parser->lexer);
2991 /* This diagnostic makes more sense if it is tagged to the line
2992 of the token we just peeked at. */
2993 cp_lexer_set_source_position_from_token (token);
2995 if (token->type == CPP_PRAGMA)
2997 error_at (token->location,
2998 "%<#pragma%> is not allowed here");
2999 cp_parser_skip_to_pragma_eol (parser, token);
3000 return;
3003 /* If this is actually a conflict marker, report it as such. */
3004 if (token->type == CPP_LSHIFT
3005 || token->type == CPP_RSHIFT
3006 || token->type == CPP_EQ_EQ)
3008 location_t loc;
3009 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3011 error_at (loc, "version control conflict marker in file");
3012 expanded_location token_exploc = expand_location (token->location);
3013 /* Consume tokens until the end of the source line. */
3014 for (;;)
3016 cp_lexer_consume_token (parser->lexer);
3017 cp_token *next = cp_lexer_peek_token (parser->lexer);
3018 if (next->type == CPP_EOF)
3019 break;
3020 if (next->location == UNKNOWN_LOCATION
3021 || loc == UNKNOWN_LOCATION)
3022 break;
3024 expanded_location next_exploc = expand_location (next->location);
3025 if (next_exploc.file != token_exploc.file)
3026 break;
3027 if (next_exploc.line != token_exploc.line)
3028 break;
3030 return;
3034 auto_diagnostic_group d;
3035 gcc_rich_location richloc (input_location);
3037 bool added_matching_location = false;
3039 if (missing_token_desc != RT_NONE)
3040 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3042 /* Potentially supply a fix-it hint, suggesting to add the
3043 missing token immediately after the *previous* token.
3044 This may move the primary location within richloc. */
3045 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3046 location_t prev_token_loc = prev_token->location;
3047 maybe_suggest_missing_token_insertion (&richloc, ttype,
3048 prev_token_loc);
3050 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3051 Attempt to consolidate diagnostics by printing it as a
3052 secondary range within the main diagnostic. */
3053 if (matching_location != UNKNOWN_LOCATION)
3054 added_matching_location
3055 = richloc.add_location_if_nearby (matching_location);
3058 /* If we were parsing a string-literal and there is an unknown name
3059 token right after, then check to see if that could also have been
3060 a literal string by checking the name against a list of known
3061 standard string literal constants defined in header files. If
3062 there is one, then add that as an hint to the error message. */
3063 name_hint h;
3064 if (token->type == CPP_NAME)
3065 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3066 if (cp_parser_is_string_literal (prev_token))
3068 tree name = token->u.value;
3069 const char *token_name = IDENTIFIER_POINTER (name);
3070 const char *header_hint
3071 = get_cp_stdlib_header_for_string_macro_name (token_name);
3072 if (header_hint != NULL)
3073 h = name_hint (NULL, new suggest_missing_header (token->location,
3074 token_name,
3075 header_hint));
3078 /* Actually emit the error. */
3079 c_parse_error (gmsgid,
3080 /* Because c_parser_error does not understand
3081 CPP_KEYWORD, keywords are treated like
3082 identifiers. */
3083 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3084 token->u.value, token->flags, &richloc);
3086 if (missing_token_desc != RT_NONE)
3088 /* If we weren't able to consolidate matching_location, then
3089 print it as a secondary diagnostic. */
3090 if (matching_location != UNKNOWN_LOCATION
3091 && !added_matching_location)
3092 inform (matching_location, "to match this %qs",
3093 get_matching_symbol (missing_token_desc));
3097 /* If not parsing tentatively, issue a diagnostic of the form
3098 FILE:LINE: MESSAGE before TOKEN
3099 where TOKEN is the next token in the input stream. MESSAGE
3100 (specified by the caller) is usually of the form "expected
3101 OTHER-TOKEN". */
3103 static void
3104 cp_parser_error (cp_parser* parser, const char* gmsgid)
3106 if (!cp_parser_simulate_error (parser))
3107 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3110 /* Issue an error about name-lookup failing. NAME is the
3111 IDENTIFIER_NODE DECL is the result of
3112 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3113 the thing that we hoped to find. */
3115 static void
3116 cp_parser_name_lookup_error (cp_parser* parser,
3117 tree name,
3118 tree decl,
3119 name_lookup_error desired,
3120 location_t location)
3122 /* If name lookup completely failed, tell the user that NAME was not
3123 declared. */
3124 if (decl == error_mark_node)
3126 if (parser->scope && parser->scope != global_namespace)
3127 error_at (location, "%<%E::%E%> has not been declared",
3128 parser->scope, name);
3129 else if (parser->scope == global_namespace)
3130 error_at (location, "%<::%E%> has not been declared", name);
3131 else if (parser->object_scope
3132 && !CLASS_TYPE_P (parser->object_scope))
3133 error_at (location, "request for member %qE in non-class type %qT",
3134 name, parser->object_scope);
3135 else if (parser->object_scope)
3136 error_at (location, "%<%T::%E%> has not been declared",
3137 parser->object_scope, name);
3138 else
3139 error_at (location, "%qE has not been declared", name);
3141 else if (parser->scope && parser->scope != global_namespace)
3143 switch (desired)
3145 case NLE_TYPE:
3146 error_at (location, "%<%E::%E%> is not a type",
3147 parser->scope, name);
3148 break;
3149 case NLE_CXX98:
3150 error_at (location, "%<%E::%E%> is not a class or namespace",
3151 parser->scope, name);
3152 break;
3153 case NLE_NOT_CXX98:
3154 error_at (location,
3155 "%<%E::%E%> is not a class, namespace, or enumeration",
3156 parser->scope, name);
3157 break;
3158 default:
3159 gcc_unreachable ();
3163 else if (parser->scope == global_namespace)
3165 switch (desired)
3167 case NLE_TYPE:
3168 error_at (location, "%<::%E%> is not a type", name);
3169 break;
3170 case NLE_CXX98:
3171 error_at (location, "%<::%E%> is not a class or namespace", name);
3172 break;
3173 case NLE_NOT_CXX98:
3174 error_at (location,
3175 "%<::%E%> is not a class, namespace, or enumeration",
3176 name);
3177 break;
3178 default:
3179 gcc_unreachable ();
3182 else
3184 switch (desired)
3186 case NLE_TYPE:
3187 error_at (location, "%qE is not a type", name);
3188 break;
3189 case NLE_CXX98:
3190 error_at (location, "%qE is not a class or namespace", name);
3191 break;
3192 case NLE_NOT_CXX98:
3193 error_at (location,
3194 "%qE is not a class, namespace, or enumeration", name);
3195 break;
3196 default:
3197 gcc_unreachable ();
3202 /* If we are parsing tentatively, remember that an error has occurred
3203 during this tentative parse. Returns true if the error was
3204 simulated; false if a message should be issued by the caller. */
3206 static bool
3207 cp_parser_simulate_error (cp_parser* parser)
3209 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3211 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3212 return true;
3214 return false;
3217 /* This function is called when a type is defined. If type
3218 definitions are forbidden at this point, an error message is
3219 issued. */
3221 static bool
3222 cp_parser_check_type_definition (cp_parser* parser)
3224 /* If types are forbidden here, issue a message. */
3225 if (parser->type_definition_forbidden_message)
3227 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3228 or %qs in the message need to be interpreted. */
3229 error (parser->type_definition_forbidden_message,
3230 parser->type_definition_forbidden_message_arg);
3231 return false;
3233 return true;
3236 /* This function is called when the DECLARATOR is processed. The TYPE
3237 was a type defined in the decl-specifiers. If it is invalid to
3238 define a type in the decl-specifiers for DECLARATOR, an error is
3239 issued. TYPE_LOCATION is the location of TYPE and is used
3240 for error reporting. */
3242 static void
3243 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3244 tree type, location_t type_location)
3246 /* [dcl.fct] forbids type definitions in return types.
3247 Unfortunately, it's not easy to know whether or not we are
3248 processing a return type until after the fact. */
3249 while (declarator
3250 && (declarator->kind == cdk_pointer
3251 || declarator->kind == cdk_reference
3252 || declarator->kind == cdk_ptrmem))
3253 declarator = declarator->declarator;
3254 if (declarator
3255 && declarator->kind == cdk_function)
3257 error_at (type_location,
3258 "new types may not be defined in a return type");
3259 inform (type_location,
3260 "(perhaps a semicolon is missing after the definition of %qT)",
3261 type);
3265 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3266 "<" in any valid C++ program. If the next token is indeed "<",
3267 issue a message warning the user about what appears to be an
3268 invalid attempt to form a template-id. LOCATION is the location
3269 of the type-specifier (TYPE) */
3271 static void
3272 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3273 tree type,
3274 enum tag_types tag_type,
3275 location_t location)
3277 cp_token_position start = 0;
3279 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3281 if (TREE_CODE (type) == TYPE_DECL)
3282 type = TREE_TYPE (type);
3283 if (TYPE_P (type) && !template_placeholder_p (type))
3284 error_at (location, "%qT is not a template", type);
3285 else if (identifier_p (type))
3287 if (tag_type != none_type)
3288 error_at (location, "%qE is not a class template", type);
3289 else
3290 error_at (location, "%qE is not a template", type);
3292 else
3293 error_at (location, "invalid template-id");
3294 /* Remember the location of the invalid "<". */
3295 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3296 start = cp_lexer_token_position (parser->lexer, true);
3297 /* Consume the "<". */
3298 cp_lexer_consume_token (parser->lexer);
3299 /* Parse the template arguments. */
3300 cp_parser_enclosed_template_argument_list (parser);
3301 /* Permanently remove the invalid template arguments so that
3302 this error message is not issued again. */
3303 if (start)
3304 cp_lexer_purge_tokens_after (parser->lexer, start);
3308 /* If parsing an integral constant-expression, issue an error message
3309 about the fact that THING appeared and return true. Otherwise,
3310 return false. In either case, set
3311 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3313 static bool
3314 cp_parser_non_integral_constant_expression (cp_parser *parser,
3315 non_integral_constant thing)
3317 parser->non_integral_constant_expression_p = true;
3318 if (parser->integral_constant_expression_p)
3320 if (!parser->allow_non_integral_constant_expression_p)
3322 const char *msg = NULL;
3323 switch (thing)
3325 case NIC_FLOAT:
3326 pedwarn (input_location, OPT_Wpedantic,
3327 "ISO C++ forbids using a floating-point literal "
3328 "in a constant-expression");
3329 return true;
3330 case NIC_CAST:
3331 error ("a cast to a type other than an integral or "
3332 "enumeration type cannot appear in a "
3333 "constant-expression");
3334 return true;
3335 case NIC_TYPEID:
3336 error ("%<typeid%> operator "
3337 "cannot appear in a constant-expression");
3338 return true;
3339 case NIC_NCC:
3340 error ("non-constant compound literals "
3341 "cannot appear in a constant-expression");
3342 return true;
3343 case NIC_FUNC_CALL:
3344 error ("a function call "
3345 "cannot appear in a constant-expression");
3346 return true;
3347 case NIC_INC:
3348 error ("an increment "
3349 "cannot appear in a constant-expression");
3350 return true;
3351 case NIC_DEC:
3352 error ("an decrement "
3353 "cannot appear in a constant-expression");
3354 return true;
3355 case NIC_ARRAY_REF:
3356 error ("an array reference "
3357 "cannot appear in a constant-expression");
3358 return true;
3359 case NIC_ADDR_LABEL:
3360 error ("the address of a label "
3361 "cannot appear in a constant-expression");
3362 return true;
3363 case NIC_OVERLOADED:
3364 error ("calls to overloaded operators "
3365 "cannot appear in a constant-expression");
3366 return true;
3367 case NIC_ASSIGNMENT:
3368 error ("an assignment cannot appear in a constant-expression");
3369 return true;
3370 case NIC_COMMA:
3371 error ("a comma operator "
3372 "cannot appear in a constant-expression");
3373 return true;
3374 case NIC_CONSTRUCTOR:
3375 error ("a call to a constructor "
3376 "cannot appear in a constant-expression");
3377 return true;
3378 case NIC_TRANSACTION:
3379 error ("a transaction expression "
3380 "cannot appear in a constant-expression");
3381 return true;
3382 case NIC_THIS:
3383 msg = "this";
3384 break;
3385 case NIC_FUNC_NAME:
3386 msg = "__FUNCTION__";
3387 break;
3388 case NIC_PRETTY_FUNC:
3389 msg = "__PRETTY_FUNCTION__";
3390 break;
3391 case NIC_C99_FUNC:
3392 msg = "__func__";
3393 break;
3394 case NIC_VA_ARG:
3395 msg = "va_arg";
3396 break;
3397 case NIC_ARROW:
3398 msg = "->";
3399 break;
3400 case NIC_POINT:
3401 msg = ".";
3402 break;
3403 case NIC_STAR:
3404 msg = "*";
3405 break;
3406 case NIC_ADDR:
3407 msg = "&";
3408 break;
3409 case NIC_PREINCREMENT:
3410 msg = "++";
3411 break;
3412 case NIC_PREDECREMENT:
3413 msg = "--";
3414 break;
3415 case NIC_NEW:
3416 msg = "new";
3417 break;
3418 case NIC_DEL:
3419 msg = "delete";
3420 break;
3421 default:
3422 gcc_unreachable ();
3424 if (msg)
3425 error ("%qs cannot appear in a constant-expression", msg);
3426 return true;
3429 return false;
3432 /* Emit a diagnostic for an invalid type name. This function commits
3433 to the current active tentative parse, if any. (Otherwise, the
3434 problematic construct might be encountered again later, resulting
3435 in duplicate error messages.) LOCATION is the location of ID. */
3437 static void
3438 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3439 location_t location)
3441 tree decl, ambiguous_decls;
3442 cp_parser_commit_to_tentative_parse (parser);
3443 /* Try to lookup the identifier. */
3444 decl = cp_parser_lookup_name (parser, id, none_type,
3445 /*is_template=*/false,
3446 /*is_namespace=*/false,
3447 /*check_dependency=*/true,
3448 &ambiguous_decls, location);
3449 if (ambiguous_decls)
3450 /* If the lookup was ambiguous, an error will already have
3451 been issued. */
3452 return;
3453 /* If the lookup found a template-name, it means that the user forgot
3454 to specify an argument list. Emit a useful error message. */
3455 if (DECL_TYPE_TEMPLATE_P (decl))
3457 auto_diagnostic_group d;
3458 error_at (location,
3459 "invalid use of template-name %qE without an argument list",
3460 decl);
3461 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3462 inform (location, "class template argument deduction is only available "
3463 "with %<-std=c++17%> or %<-std=gnu++17%>");
3464 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3466 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3467 error_at (location, "invalid use of destructor %qD as a type", id);
3468 else if (TREE_CODE (decl) == TYPE_DECL)
3469 /* Something like 'unsigned A a;' */
3470 error_at (location, "invalid combination of multiple type-specifiers");
3471 else if (!parser->scope)
3473 /* Issue an error message. */
3474 auto_diagnostic_group d;
3475 name_hint hint;
3476 if (TREE_CODE (id) == IDENTIFIER_NODE)
3477 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3478 if (const char *suggestion = hint.suggestion ())
3480 gcc_rich_location richloc (location);
3481 richloc.add_fixit_replace (suggestion);
3482 error_at (&richloc,
3483 "%qE does not name a type; did you mean %qs?",
3484 id, suggestion);
3486 else
3487 error_at (location, "%qE does not name a type", id);
3488 /* If we're in a template class, it's possible that the user was
3489 referring to a type from a base class. For example:
3491 template <typename T> struct A { typedef T X; };
3492 template <typename T> struct B : public A<T> { X x; };
3494 The user should have said "typename A<T>::X". */
3495 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3496 inform (location, "C++11 %<constexpr%> only available with "
3497 "%<-std=c++11%> or %<-std=gnu++11%>");
3498 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3499 inform (location, "C++11 %<noexcept%> only available with "
3500 "%<-std=c++11%> or %<-std=gnu++11%>");
3501 else if (TREE_CODE (id) == IDENTIFIER_NODE
3502 && (id_equal (id, "module") || id_equal (id, "import")))
3504 if (modules_p ())
3505 inform (location, "%qE is not recognized as a module control-line",
3506 id);
3507 else if (cxx_dialect < cxx20)
3508 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3509 id);
3510 else
3511 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3512 ", which is not yet enabled with %<-std=c++20%>", id);
3514 else if (cxx_dialect < cxx11
3515 && TREE_CODE (id) == IDENTIFIER_NODE
3516 && id_equal (id, "thread_local"))
3517 inform (location, "C++11 %<thread_local%> only available with "
3518 "%<-std=c++11%> or %<-std=gnu++11%>");
3519 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3520 inform (location, "C++20 %<constinit%> only available with "
3521 "%<-std=c++20%> or %<-std=gnu++20%>");
3522 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3523 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3524 "%<-fconcepts%>");
3525 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3526 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3527 "%<-fconcepts%>");
3528 else if (processing_template_decl && current_class_type
3529 && TYPE_BINFO (current_class_type))
3531 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3532 b; b = TREE_CHAIN (b))
3534 tree base_type = BINFO_TYPE (b);
3535 if (CLASS_TYPE_P (base_type)
3536 && dependent_type_p (base_type))
3538 /* Go from a particular instantiation of the
3539 template (which will have an empty TYPE_FIELDs),
3540 to the main version. */
3541 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3542 for (tree field = TYPE_FIELDS (base_type);
3543 field; field = DECL_CHAIN (field))
3544 if (TREE_CODE (field) == TYPE_DECL
3545 && DECL_NAME (field) == id)
3547 inform (location,
3548 "(perhaps %<typename %T::%E%> was intended)",
3549 BINFO_TYPE (b), id);
3550 goto found;
3554 found:;
3557 /* Here we diagnose qualified-ids where the scope is actually correct,
3558 but the identifier does not resolve to a valid type name. */
3559 else if (parser->scope != error_mark_node)
3561 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3563 auto_diagnostic_group d;
3564 name_hint hint;
3565 if (decl == error_mark_node)
3566 hint = suggest_alternative_in_explicit_scope (location, id,
3567 parser->scope);
3568 const char *suggestion = hint.suggestion ();
3569 gcc_rich_location richloc (location_of (id));
3570 if (suggestion)
3571 richloc.add_fixit_replace (suggestion);
3572 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3574 if (suggestion)
3575 error_at (&richloc,
3576 "%qE in namespace %qE does not name a template"
3577 " type; did you mean %qs?",
3578 id, parser->scope, suggestion);
3579 else
3580 error_at (&richloc,
3581 "%qE in namespace %qE does not name a template type",
3582 id, parser->scope);
3584 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3586 if (suggestion)
3587 error_at (&richloc,
3588 "%qE in namespace %qE does not name a template"
3589 " type; did you mean %qs?",
3590 TREE_OPERAND (id, 0), parser->scope, suggestion);
3591 else
3592 error_at (&richloc,
3593 "%qE in namespace %qE does not name a template"
3594 " type",
3595 TREE_OPERAND (id, 0), parser->scope);
3597 else
3599 if (suggestion)
3600 error_at (&richloc,
3601 "%qE in namespace %qE does not name a type"
3602 "; did you mean %qs?",
3603 id, parser->scope, suggestion);
3604 else
3605 error_at (&richloc,
3606 "%qE in namespace %qE does not name a type",
3607 id, parser->scope);
3609 if (DECL_P (decl))
3610 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3612 else if (CLASS_TYPE_P (parser->scope)
3613 && constructor_name_p (id, parser->scope))
3615 /* A<T>::A<T>() */
3616 auto_diagnostic_group d;
3617 error_at (location, "%<%T::%E%> names the constructor, not"
3618 " the type", parser->scope, id);
3619 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3620 error_at (location, "and %qT has no template constructors",
3621 parser->scope);
3623 else if (TYPE_P (parser->scope)
3624 && dependent_scope_p (parser->scope))
3626 gcc_rich_location richloc (location);
3627 richloc.add_fixit_insert_before ("typename ");
3628 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3629 error_at (&richloc,
3630 "need %<typename%> before %<%T::%D::%E%> because "
3631 "%<%T::%D%> is a dependent scope",
3632 TYPE_CONTEXT (parser->scope),
3633 TYPENAME_TYPE_FULLNAME (parser->scope),
3635 TYPE_CONTEXT (parser->scope),
3636 TYPENAME_TYPE_FULLNAME (parser->scope));
3637 else
3638 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3639 "%qT is a dependent scope",
3640 parser->scope, id, parser->scope);
3642 else if (TYPE_P (parser->scope))
3644 auto_diagnostic_group d;
3645 if (!COMPLETE_TYPE_P (parser->scope))
3646 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3647 parser->scope);
3648 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3649 error_at (location_of (id),
3650 "%qE in %q#T does not name a template type",
3651 id, parser->scope);
3652 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3653 error_at (location_of (id),
3654 "%qE in %q#T does not name a template type",
3655 TREE_OPERAND (id, 0), parser->scope);
3656 else
3657 error_at (location_of (id),
3658 "%qE in %q#T does not name a type",
3659 id, parser->scope);
3660 if (DECL_P (decl))
3661 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3663 else
3664 gcc_unreachable ();
3668 /* Check for a common situation where a type-name should be present,
3669 but is not, and issue a sensible error message. Returns true if an
3670 invalid type-name was detected.
3672 The situation handled by this function are variable declarations of the
3673 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3674 Usually, `ID' should name a type, but if we got here it means that it
3675 does not. We try to emit the best possible error message depending on
3676 how exactly the id-expression looks like. */
3678 static bool
3679 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3681 tree id;
3682 cp_token *token = cp_lexer_peek_token (parser->lexer);
3684 /* Avoid duplicate error about ambiguous lookup. */
3685 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3687 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3688 if (next->type == CPP_NAME && next->error_reported)
3689 goto out;
3692 cp_parser_parse_tentatively (parser);
3693 id = cp_parser_id_expression (parser,
3694 /*template_keyword_p=*/false,
3695 /*check_dependency_p=*/true,
3696 /*template_p=*/NULL,
3697 /*declarator_p=*/true,
3698 /*optional_p=*/false);
3699 /* If the next token is a (, this is a function with no explicit return
3700 type, i.e. constructor, destructor or conversion op. */
3701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3702 || TREE_CODE (id) == TYPE_DECL)
3704 cp_parser_abort_tentative_parse (parser);
3705 return false;
3707 if (!cp_parser_parse_definitely (parser))
3708 return false;
3710 /* Emit a diagnostic for the invalid type. */
3711 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3712 out:
3713 /* If we aren't in the middle of a declarator (i.e. in a
3714 parameter-declaration-clause), skip to the end of the declaration;
3715 there's no point in trying to process it. */
3716 if (!parser->in_declarator_p)
3717 cp_parser_skip_to_end_of_block_or_statement (parser);
3718 return true;
3721 /* Consume tokens up to, and including, the next non-nested closing `)'.
3722 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3723 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3724 found an unnested token of that type. */
3726 static int
3727 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3728 bool recovering,
3729 cpp_ttype or_ttype,
3730 bool consume_paren)
3732 unsigned paren_depth = 0;
3733 unsigned brace_depth = 0;
3734 unsigned square_depth = 0;
3735 unsigned condop_depth = 0;
3737 if (recovering && or_ttype == CPP_EOF
3738 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3739 return 0;
3741 while (true)
3743 cp_token * token = cp_lexer_peek_token (parser->lexer);
3745 /* Have we found what we're looking for before the closing paren? */
3746 if (token->type == or_ttype && or_ttype != CPP_EOF
3747 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3748 return -1;
3750 switch (token->type)
3752 case CPP_PRAGMA_EOL:
3753 if (!parser->lexer->in_pragma)
3754 break;
3755 /* FALLTHRU */
3756 case CPP_EOF:
3757 /* If we've run out of tokens, then there is no closing `)'. */
3758 return 0;
3760 /* This is good for lambda expression capture-lists. */
3761 case CPP_OPEN_SQUARE:
3762 ++square_depth;
3763 break;
3764 case CPP_CLOSE_SQUARE:
3765 if (!square_depth--)
3766 return 0;
3767 break;
3769 case CPP_SEMICOLON:
3770 /* This matches the processing in skip_to_end_of_statement. */
3771 if (!brace_depth)
3772 return 0;
3773 break;
3775 case CPP_OPEN_BRACE:
3776 ++brace_depth;
3777 break;
3778 case CPP_CLOSE_BRACE:
3779 if (!brace_depth--)
3780 return 0;
3781 break;
3783 case CPP_OPEN_PAREN:
3784 if (!brace_depth)
3785 ++paren_depth;
3786 break;
3788 case CPP_CLOSE_PAREN:
3789 if (!brace_depth && !paren_depth--)
3791 if (consume_paren)
3792 cp_lexer_consume_token (parser->lexer);
3793 return 1;
3795 break;
3797 case CPP_QUERY:
3798 if (!brace_depth && !paren_depth && !square_depth)
3799 ++condop_depth;
3800 break;
3802 case CPP_COLON:
3803 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3804 condop_depth--;
3805 break;
3807 case CPP_KEYWORD:
3808 if (token->keyword != RID__EXPORT
3809 && token->keyword != RID__MODULE
3810 && token->keyword != RID__IMPORT)
3811 break;
3812 /* FALLTHROUGH */
3814 case CPP_PRAGMA:
3815 /* We fell into a pragma. Skip it, and continue. */
3816 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3817 continue;
3819 default:
3820 break;
3823 /* Consume the token. */
3824 cp_lexer_consume_token (parser->lexer);
3828 /* Consume tokens up to, and including, the next non-nested closing `)'.
3829 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3830 are doing error recovery. Returns -1 if OR_COMMA is true and we
3831 found an unnested token of that type. */
3833 static int
3834 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3835 bool recovering,
3836 bool or_comma,
3837 bool consume_paren)
3839 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3840 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3841 ttype, consume_paren);
3844 /* Consume tokens until we reach the end of the current statement.
3845 Normally, that will be just before consuming a `;'. However, if a
3846 non-nested `}' comes first, then we stop before consuming that. */
3848 static void
3849 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3851 unsigned nesting_depth = 0;
3853 /* Unwind generic function template scope if necessary. */
3854 if (parser->fully_implicit_function_template_p)
3855 abort_fully_implicit_template (parser);
3857 while (true)
3859 cp_token *token = cp_lexer_peek_token (parser->lexer);
3861 switch (token->type)
3863 case CPP_PRAGMA_EOL:
3864 if (!parser->lexer->in_pragma)
3865 break;
3866 /* FALLTHRU */
3867 case CPP_EOF:
3868 /* If we've run out of tokens, stop. */
3869 return;
3871 case CPP_SEMICOLON:
3872 /* If the next token is a `;', we have reached the end of the
3873 statement. */
3874 if (!nesting_depth)
3875 return;
3876 break;
3878 case CPP_CLOSE_BRACE:
3879 /* If this is a non-nested '}', stop before consuming it.
3880 That way, when confronted with something like:
3882 { 3 + }
3884 we stop before consuming the closing '}', even though we
3885 have not yet reached a `;'. */
3886 if (nesting_depth == 0)
3887 return;
3889 /* If it is the closing '}' for a block that we have
3890 scanned, stop -- but only after consuming the token.
3891 That way given:
3893 void f g () { ... }
3894 typedef int I;
3896 we will stop after the body of the erroneously declared
3897 function, but before consuming the following `typedef'
3898 declaration. */
3899 if (--nesting_depth == 0)
3901 cp_lexer_consume_token (parser->lexer);
3902 return;
3904 break;
3906 case CPP_OPEN_BRACE:
3907 ++nesting_depth;
3908 break;
3910 case CPP_KEYWORD:
3911 if (token->keyword != RID__EXPORT
3912 && token->keyword != RID__MODULE
3913 && token->keyword != RID__IMPORT)
3914 break;
3915 /* FALLTHROUGH */
3917 case CPP_PRAGMA:
3918 /* We fell into a pragma. Skip it, and continue or return. */
3919 cp_parser_skip_to_pragma_eol (parser, token);
3920 if (!nesting_depth)
3921 return;
3922 continue;
3924 default:
3925 break;
3928 /* Consume the token. */
3929 cp_lexer_consume_token (parser->lexer);
3933 /* This function is called at the end of a statement or declaration.
3934 If the next token is a semicolon, it is consumed; otherwise, error
3935 recovery is attempted. */
3937 static void
3938 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3940 /* Look for the trailing `;'. */
3941 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3943 /* If there is additional (erroneous) input, skip to the end of
3944 the statement. */
3945 cp_parser_skip_to_end_of_statement (parser);
3946 /* If the next token is now a `;', consume it. */
3947 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3948 cp_lexer_consume_token (parser->lexer);
3952 /* Skip tokens until we have consumed an entire block, or until we
3953 have consumed a non-nested `;'. */
3955 static void
3956 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3958 int nesting_depth = 0;
3960 /* Unwind generic function template scope if necessary. */
3961 if (parser->fully_implicit_function_template_p)
3962 abort_fully_implicit_template (parser);
3964 while (nesting_depth >= 0)
3966 cp_token *token = cp_lexer_peek_token (parser->lexer);
3968 switch (token->type)
3970 case CPP_PRAGMA_EOL:
3971 if (!parser->lexer->in_pragma)
3972 break;
3973 /* FALLTHRU */
3974 case CPP_EOF:
3975 /* If we've run out of tokens, stop. */
3976 return;
3978 case CPP_SEMICOLON:
3979 /* Stop if this is an unnested ';'. */
3980 if (!nesting_depth)
3981 nesting_depth = -1;
3982 break;
3984 case CPP_CLOSE_BRACE:
3985 /* Stop if this is an unnested '}', or closes the outermost
3986 nesting level. */
3987 nesting_depth--;
3988 if (nesting_depth < 0)
3989 return;
3990 if (!nesting_depth)
3991 nesting_depth = -1;
3992 break;
3994 case CPP_OPEN_BRACE:
3995 /* Nest. */
3996 nesting_depth++;
3997 break;
3999 case CPP_KEYWORD:
4000 if (token->keyword != RID__EXPORT
4001 && token->keyword != RID__MODULE
4002 && token->keyword != RID__IMPORT)
4003 break;
4004 /* FALLTHROUGH */
4006 case CPP_PRAGMA:
4007 /* Skip it, and continue or return. */
4008 cp_parser_skip_to_pragma_eol (parser, token);
4009 if (!nesting_depth)
4010 return;
4011 continue;
4013 default:
4014 break;
4017 /* Consume the token. */
4018 cp_lexer_consume_token (parser->lexer);
4022 /* Skip tokens until a non-nested closing curly brace is the next
4023 token, or there are no more tokens. Return true in the first case,
4024 false otherwise. */
4026 static bool
4027 cp_parser_skip_to_closing_brace (cp_parser *parser)
4029 unsigned nesting_depth = 0;
4031 while (true)
4033 cp_token *token = cp_lexer_peek_token (parser->lexer);
4035 switch (token->type)
4037 case CPP_PRAGMA_EOL:
4038 if (!parser->lexer->in_pragma)
4039 break;
4040 /* FALLTHRU */
4041 case CPP_EOF:
4042 /* If we've run out of tokens, stop. */
4043 return false;
4045 case CPP_CLOSE_BRACE:
4046 /* If the next token is a non-nested `}', then we have reached
4047 the end of the current block. */
4048 if (nesting_depth-- == 0)
4049 return true;
4050 break;
4052 case CPP_OPEN_BRACE:
4053 /* If it the next token is a `{', then we are entering a new
4054 block. Consume the entire block. */
4055 ++nesting_depth;
4056 break;
4058 default:
4059 break;
4062 /* Consume the token. */
4063 cp_lexer_consume_token (parser->lexer);
4067 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4068 parameter is the PRAGMA token, allowing us to purge the entire pragma
4069 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4070 forwards (not error recovery). */
4072 static void
4073 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4075 cp_token *token;
4079 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4080 before an EOF token, even when the EOF is on the pragma line.
4081 We should never get here without being inside a deferred
4082 pragma. */
4083 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4084 token = cp_lexer_consume_token (parser->lexer);
4086 while (token->type != CPP_PRAGMA_EOL);
4088 if (pragma_tok)
4090 /* Ensure that the pragma is not parsed again. */
4091 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
4092 parser->lexer->in_pragma = false;
4093 if (parser->lexer->in_omp_attribute_pragma
4094 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4096 parser->lexer = parser->lexer->next;
4097 /* Put the current source position back where it was before this
4098 lexer was pushed. */
4099 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4104 /* Require pragma end of line, resyncing with it as necessary. The
4105 arguments are as for cp_parser_skip_to_pragma_eol. */
4107 static void
4108 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4110 parser->lexer->in_pragma = false;
4111 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4113 else if (parser->lexer->in_omp_attribute_pragma
4114 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4116 parser->lexer = parser->lexer->next;
4117 /* Put the current source position back where it was before this
4118 lexer was pushed. */
4119 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4123 /* This is a simple wrapper around make_typename_type. When the id is
4124 an unresolved identifier node, we can provide a superior diagnostic
4125 using cp_parser_diagnose_invalid_type_name. */
4127 static tree
4128 cp_parser_make_typename_type (cp_parser *parser, tree id,
4129 location_t id_location)
4131 tree result;
4132 if (identifier_p (id))
4134 result = make_typename_type (parser->scope, id, typename_type,
4135 /*complain=*/tf_none);
4136 if (result == error_mark_node)
4137 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4138 return result;
4140 return make_typename_type (parser->scope, id, typename_type, tf_error);
4143 /* This is a wrapper around the
4144 make_{pointer,ptrmem,reference}_declarator functions that decides
4145 which one to call based on the CODE and CLASS_TYPE arguments. The
4146 CODE argument should be one of the values returned by
4147 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4148 appertain to the pointer or reference. */
4150 static cp_declarator *
4151 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4152 cp_cv_quals cv_qualifiers,
4153 cp_declarator *target,
4154 tree attributes)
4156 if (code == ERROR_MARK || target == cp_error_declarator)
4157 return cp_error_declarator;
4159 if (code == INDIRECT_REF)
4160 if (class_type == NULL_TREE)
4161 return make_pointer_declarator (cv_qualifiers, target, attributes);
4162 else
4163 return make_ptrmem_declarator (cv_qualifiers, class_type,
4164 target, attributes);
4165 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4166 return make_reference_declarator (cv_qualifiers, target,
4167 false, attributes);
4168 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4169 return make_reference_declarator (cv_qualifiers, target,
4170 true, attributes);
4171 gcc_unreachable ();
4174 /* Create a new C++ parser. */
4176 static cp_parser *
4177 cp_parser_new (cp_lexer *lexer)
4179 /* Initialize the binops_by_token so that we can get the tree
4180 directly from the token. */
4181 for (unsigned i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
4182 binops_by_token[binops[i].token_type] = binops[i];
4184 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4185 parser->lexer = lexer;
4186 parser->context = cp_parser_context_new (NULL);
4188 /* For now, we always accept GNU extensions. */
4189 parser->allow_gnu_extensions_p = 1;
4191 /* The `>' token is a greater-than operator, not the end of a
4192 template-id. */
4193 parser->greater_than_is_operator_p = true;
4195 parser->default_arg_ok_p = true;
4197 /* We are not parsing a constant-expression. */
4198 parser->integral_constant_expression_p = false;
4199 parser->allow_non_integral_constant_expression_p = false;
4200 parser->non_integral_constant_expression_p = false;
4202 /* Local variable names are not forbidden. */
4203 parser->local_variables_forbidden_p = 0;
4205 /* We are not processing an `extern "C"' declaration. */
4206 parser->in_unbraced_linkage_specification_p = false;
4208 /* We are not processing a declarator. */
4209 parser->in_declarator_p = false;
4211 /* We are not processing a template-argument-list. */
4212 parser->in_template_argument_list_p = false;
4214 /* We are not in an iteration statement. */
4215 parser->in_statement = 0;
4217 /* We are not in a switch statement. */
4218 parser->in_switch_statement_p = false;
4220 /* We are not parsing a type-id inside an expression. */
4221 parser->in_type_id_in_expr_p = false;
4223 /* String literals should be translated to the execution character set. */
4224 parser->translate_strings_p = true;
4226 /* We are not parsing a function body. */
4227 parser->in_function_body = false;
4229 /* We can correct until told otherwise. */
4230 parser->colon_corrects_to_scope_p = true;
4232 /* The unparsed function queue is empty. */
4233 push_unparsed_function_queues (parser);
4235 /* There are no classes being defined. */
4236 parser->num_classes_being_defined = 0;
4238 /* No template parameters apply. */
4239 parser->num_template_parameter_lists = 0;
4241 /* Special parsing data structures. */
4242 parser->omp_declare_simd = NULL;
4243 parser->oacc_routine = NULL;
4245 /* Not declaring an implicit function template. */
4246 parser->auto_is_implicit_function_template_parm_p = false;
4247 parser->fully_implicit_function_template_p = false;
4248 parser->implicit_template_parms = 0;
4249 parser->implicit_template_scope = 0;
4251 /* Allow constrained-type-specifiers. */
4252 parser->prevent_constrained_type_specifiers = 0;
4254 /* We haven't yet seen an 'extern "C"'. */
4255 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4257 return parser;
4260 /* Create a cp_lexer structure which will emit the tokens in CACHE
4261 and push it onto the parser's lexer stack. This is used for delayed
4262 parsing of in-class method bodies and default arguments, and should
4263 not be confused with tentative parsing. */
4264 static void
4265 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4267 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4268 lexer->next = parser->lexer;
4269 parser->lexer = lexer;
4271 /* Move the current source position to that of the first token in the
4272 new lexer. */
4273 cp_lexer_set_source_position_from_token (lexer->next_token);
4276 /* Pop the top lexer off the parser stack. This is never used for the
4277 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4278 static void
4279 cp_parser_pop_lexer (cp_parser *parser)
4281 cp_lexer *lexer = parser->lexer;
4282 parser->lexer = lexer->next;
4283 cp_lexer_destroy (lexer);
4285 /* Put the current source position back where it was before this
4286 lexer was pushed. */
4287 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4290 /* Lexical conventions [gram.lex] */
4292 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4293 identifier. */
4295 static cp_expr
4296 cp_parser_identifier (cp_parser* parser)
4298 cp_token *token;
4300 /* Look for the identifier. */
4301 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4302 /* Return the value. */
4303 if (token)
4304 return cp_expr (token->u.value, token->location);
4305 else
4306 return error_mark_node;
4309 /* Parse a sequence of adjacent string constants. Returns a
4310 TREE_STRING representing the combined, nul-terminated string
4311 constant. If TRANSLATE is true, translate the string to the
4312 execution character set. If WIDE_OK is true, a wide string is
4313 invalid here.
4315 C++98 [lex.string] says that if a narrow string literal token is
4316 adjacent to a wide string literal token, the behavior is undefined.
4317 However, C99 6.4.5p4 says that this results in a wide string literal.
4318 We follow C99 here, for consistency with the C front end.
4320 This code is largely lifted from lex_string() in c-lex.c.
4322 FUTURE: ObjC++ will need to handle @-strings here. */
4323 static cp_expr
4324 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4325 bool lookup_udlit = true)
4327 tree value;
4328 size_t count;
4329 struct obstack str_ob;
4330 struct obstack loc_ob;
4331 cpp_string str, istr, *strs;
4332 cp_token *tok;
4333 enum cpp_ttype type, curr_type;
4334 int have_suffix_p = 0;
4335 tree string_tree;
4336 tree suffix_id = NULL_TREE;
4337 bool curr_tok_is_userdef_p = false;
4339 tok = cp_lexer_peek_token (parser->lexer);
4340 if (!cp_parser_is_string_literal (tok))
4342 cp_parser_error (parser, "expected string-literal");
4343 return error_mark_node;
4346 location_t loc = tok->location;
4348 if (cpp_userdef_string_p (tok->type))
4350 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4351 curr_type = cpp_userdef_string_remove_type (tok->type);
4352 curr_tok_is_userdef_p = true;
4354 else
4356 string_tree = tok->u.value;
4357 curr_type = tok->type;
4359 type = curr_type;
4361 /* Try to avoid the overhead of creating and destroying an obstack
4362 for the common case of just one string. */
4363 if (!cp_parser_is_string_literal
4364 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4366 cp_lexer_consume_token (parser->lexer);
4368 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4369 str.len = TREE_STRING_LENGTH (string_tree);
4370 count = 1;
4372 if (curr_tok_is_userdef_p)
4374 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4375 have_suffix_p = 1;
4376 curr_type = cpp_userdef_string_remove_type (tok->type);
4378 else
4379 curr_type = tok->type;
4381 strs = &str;
4383 else
4385 location_t last_tok_loc = tok->location;
4386 gcc_obstack_init (&str_ob);
4387 gcc_obstack_init (&loc_ob);
4388 count = 0;
4392 cp_lexer_consume_token (parser->lexer);
4393 count++;
4394 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4395 str.len = TREE_STRING_LENGTH (string_tree);
4397 if (curr_tok_is_userdef_p)
4399 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4400 if (have_suffix_p == 0)
4402 suffix_id = curr_suffix_id;
4403 have_suffix_p = 1;
4405 else if (have_suffix_p == 1
4406 && curr_suffix_id != suffix_id)
4408 error ("inconsistent user-defined literal suffixes"
4409 " %qD and %qD in string literal",
4410 suffix_id, curr_suffix_id);
4411 have_suffix_p = -1;
4413 curr_type = cpp_userdef_string_remove_type (tok->type);
4415 else
4416 curr_type = tok->type;
4418 if (type != curr_type)
4420 if (type == CPP_STRING)
4421 type = curr_type;
4422 else if (curr_type != CPP_STRING)
4424 rich_location rich_loc (line_table, tok->location);
4425 rich_loc.add_range (last_tok_loc);
4426 error_at (&rich_loc,
4427 "concatenation of string literals with "
4428 "conflicting encoding prefixes");
4432 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4433 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4435 last_tok_loc = tok->location;
4437 tok = cp_lexer_peek_token (parser->lexer);
4438 if (cpp_userdef_string_p (tok->type))
4440 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4441 curr_type = cpp_userdef_string_remove_type (tok->type);
4442 curr_tok_is_userdef_p = true;
4444 else
4446 string_tree = tok->u.value;
4447 curr_type = tok->type;
4448 curr_tok_is_userdef_p = false;
4451 while (cp_parser_is_string_literal (tok));
4453 /* A string literal built by concatenation has its caret=start at
4454 the start of the initial string, and its finish at the finish of
4455 the final string literal. */
4456 loc = make_location (loc, loc, get_finish (last_tok_loc));
4458 strs = (cpp_string *) obstack_finish (&str_ob);
4461 if (type != CPP_STRING && !wide_ok)
4463 cp_parser_error (parser, "a wide string is invalid in this context");
4464 type = CPP_STRING;
4467 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4468 (parse_in, strs, count, &istr, type))
4470 value = build_string (istr.len, (const char *)istr.text);
4471 free (CONST_CAST (unsigned char *, istr.text));
4472 if (count > 1)
4474 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4475 gcc_assert (g_string_concat_db);
4476 g_string_concat_db->record_string_concatenation (count, locs);
4479 switch (type)
4481 default:
4482 case CPP_STRING:
4483 TREE_TYPE (value) = char_array_type_node;
4484 break;
4485 case CPP_UTF8STRING:
4486 if (flag_char8_t)
4487 TREE_TYPE (value) = char8_array_type_node;
4488 else
4489 TREE_TYPE (value) = char_array_type_node;
4490 break;
4491 case CPP_STRING16:
4492 TREE_TYPE (value) = char16_array_type_node;
4493 break;
4494 case CPP_STRING32:
4495 TREE_TYPE (value) = char32_array_type_node;
4496 break;
4497 case CPP_WSTRING:
4498 TREE_TYPE (value) = wchar_array_type_node;
4499 break;
4502 value = fix_string_type (value);
4504 if (have_suffix_p)
4506 tree literal = build_userdef_literal (suffix_id, value,
4507 OT_NONE, NULL_TREE);
4508 if (lookup_udlit)
4509 value = cp_parser_userdef_string_literal (literal);
4510 else
4511 value = literal;
4514 else
4515 /* cpp_interpret_string has issued an error. */
4516 value = error_mark_node;
4518 if (count > 1)
4520 obstack_free (&str_ob, 0);
4521 obstack_free (&loc_ob, 0);
4524 return cp_expr (value, loc);
4527 /* Look up a literal operator with the name and the exact arguments. */
4529 static tree
4530 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4532 tree decl = lookup_name (name);
4533 if (!decl || !is_overloaded_fn (decl))
4534 return error_mark_node;
4536 for (lkp_iterator iter (decl); iter; ++iter)
4538 tree fn = *iter;
4540 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4542 unsigned int ix;
4543 bool found = true;
4545 for (ix = 0;
4546 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4547 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4549 tree tparm = TREE_VALUE (parmtypes);
4550 tree targ = TREE_TYPE ((*args)[ix]);
4551 bool ptr = TYPE_PTR_P (tparm);
4552 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4553 if ((ptr || arr || !same_type_p (tparm, targ))
4554 && (!ptr || !arr
4555 || !same_type_p (TREE_TYPE (tparm),
4556 TREE_TYPE (targ))))
4557 found = false;
4560 if (found
4561 && ix == vec_safe_length (args)
4562 /* May be this should be sufficient_parms_p instead,
4563 depending on how exactly should user-defined literals
4564 work in presence of default arguments on the literal
4565 operator parameters. */
4566 && parmtypes == void_list_node)
4567 return decl;
4571 return error_mark_node;
4574 /* Parse a user-defined char constant. Returns a call to a user-defined
4575 literal operator taking the character as an argument. */
4577 static cp_expr
4578 cp_parser_userdef_char_literal (cp_parser *parser)
4580 cp_token *token = cp_lexer_consume_token (parser->lexer);
4581 tree literal = token->u.value;
4582 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4583 tree value = USERDEF_LITERAL_VALUE (literal);
4584 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4585 tree decl, result;
4587 /* Build up a call to the user-defined operator */
4588 /* Lookup the name we got back from the id-expression. */
4589 releasing_vec args;
4590 vec_safe_push (args, value);
4591 decl = lookup_literal_operator (name, args);
4592 if (!decl || decl == error_mark_node)
4594 error ("unable to find character literal operator %qD with %qT argument",
4595 name, TREE_TYPE (value));
4596 return error_mark_node;
4598 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4599 return result;
4602 /* A subroutine of cp_parser_userdef_numeric_literal to
4603 create a char... template parameter pack from a string node. */
4605 static tree
4606 make_char_string_pack (tree value)
4608 tree charvec;
4609 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4610 const char *str = TREE_STRING_POINTER (value);
4611 int i, len = TREE_STRING_LENGTH (value) - 1;
4612 tree argvec = make_tree_vec (1);
4614 /* Fill in CHARVEC with all of the parameters. */
4615 charvec = make_tree_vec (len);
4616 for (i = 0; i < len; ++i)
4618 unsigned char s[3] = { '\'', str[i], '\'' };
4619 cpp_string in = { 3, s };
4620 cpp_string out = { 0, 0 };
4621 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4622 return NULL_TREE;
4623 gcc_assert (out.len == 2);
4624 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4625 out.text[0]);
4628 /* Build the argument packs. */
4629 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4631 TREE_VEC_ELT (argvec, 0) = argpack;
4633 return argvec;
4636 /* A subroutine of cp_parser_userdef_numeric_literal to
4637 create a char... template parameter pack from a string node. */
4639 static tree
4640 make_string_pack (tree value)
4642 tree charvec;
4643 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4644 const unsigned char *str
4645 = (const unsigned char *) TREE_STRING_POINTER (value);
4646 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4647 int len = TREE_STRING_LENGTH (value) / sz - 1;
4648 tree argvec = make_tree_vec (2);
4650 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4651 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4653 /* First template parm is character type. */
4654 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4656 /* Fill in CHARVEC with all of the parameters. */
4657 charvec = make_tree_vec (len);
4658 for (int i = 0; i < len; ++i)
4659 TREE_VEC_ELT (charvec, i)
4660 = double_int_to_tree (str_char_type_node,
4661 double_int::from_buffer (str + i * sz, sz));
4663 /* Build the argument packs. */
4664 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4666 TREE_VEC_ELT (argvec, 1) = argpack;
4668 return argvec;
4671 /* Parse a user-defined numeric constant. returns a call to a user-defined
4672 literal operator. */
4674 static cp_expr
4675 cp_parser_userdef_numeric_literal (cp_parser *parser)
4677 cp_token *token = cp_lexer_consume_token (parser->lexer);
4678 tree literal = token->u.value;
4679 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4680 tree value = USERDEF_LITERAL_VALUE (literal);
4681 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4682 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4683 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4684 tree decl, result;
4686 /* Look for a literal operator taking the exact type of numeric argument
4687 as the literal value. */
4688 releasing_vec args;
4689 vec_safe_push (args, value);
4690 decl = lookup_literal_operator (name, args);
4691 if (decl && decl != error_mark_node)
4693 result = finish_call_expr (decl, &args, false, true,
4694 tf_warning_or_error);
4696 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4698 warning_at (token->location, OPT_Woverflow,
4699 "integer literal exceeds range of %qT type",
4700 long_long_unsigned_type_node);
4702 else
4704 if (overflow > 0)
4705 warning_at (token->location, OPT_Woverflow,
4706 "floating literal exceeds range of %qT type",
4707 long_double_type_node);
4708 else if (overflow < 0)
4709 warning_at (token->location, OPT_Woverflow,
4710 "floating literal truncated to zero");
4713 return result;
4716 /* If the numeric argument didn't work, look for a raw literal
4717 operator taking a const char* argument consisting of the number
4718 in string format. */
4719 args->truncate (0);
4720 vec_safe_push (args, num_string);
4721 decl = lookup_literal_operator (name, args);
4722 if (decl && decl != error_mark_node)
4724 result = finish_call_expr (decl, &args, false, true,
4725 tf_warning_or_error);
4726 return result;
4729 /* If the raw literal didn't work, look for a non-type template
4730 function with parameter pack char.... Call the function with
4731 template parameter characters representing the number. */
4732 args->truncate (0);
4733 decl = lookup_literal_operator (name, args);
4734 if (decl && decl != error_mark_node)
4736 tree tmpl_args = make_char_string_pack (num_string);
4737 if (tmpl_args == NULL_TREE)
4739 error ("failed to translate literal to execution character set %qT",
4740 num_string);
4741 return error_mark_node;
4743 decl = lookup_template_function (decl, tmpl_args);
4744 result = finish_call_expr (decl, &args, false, true,
4745 tf_warning_or_error);
4746 return result;
4749 /* In C++14 the standard library defines complex number suffixes that
4750 conflict with GNU extensions. Prefer them if <complex> is #included. */
4751 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4752 bool i14 = (cxx_dialect > cxx11
4753 && (id_equal (suffix_id, "i")
4754 || id_equal (suffix_id, "if")
4755 || id_equal (suffix_id, "il")));
4756 diagnostic_t kind = DK_ERROR;
4757 int opt = 0;
4759 if (i14 && ext)
4761 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4762 LOOK_want::NORMAL, false);
4763 if (cxlit == error_mark_node)
4765 /* No <complex>, so pedwarn and use GNU semantics. */
4766 kind = DK_PEDWARN;
4767 opt = OPT_Wpedantic;
4771 bool complained
4772 = emit_diagnostic (kind, input_location, opt,
4773 "unable to find numeric literal operator %qD", name);
4775 if (!complained)
4776 /* Don't inform either. */;
4777 else if (i14)
4779 inform (token->location, "add %<using namespace std::complex_literals%> "
4780 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4781 "suffixes");
4782 if (ext)
4783 inform (token->location, "or use %<j%> instead of %<i%> for the "
4784 "GNU built-in suffix");
4786 else if (!ext)
4787 inform (token->location, "use %<-fext-numeric-literals%> "
4788 "to enable more built-in suffixes");
4790 if (kind == DK_ERROR)
4791 value = error_mark_node;
4792 else
4794 /* Use the built-in semantics. */
4795 tree type;
4796 if (id_equal (suffix_id, "i"))
4798 if (TREE_CODE (value) == INTEGER_CST)
4799 type = integer_type_node;
4800 else
4801 type = double_type_node;
4803 else if (id_equal (suffix_id, "if"))
4804 type = float_type_node;
4805 else /* if (id_equal (suffix_id, "il")) */
4806 type = long_double_type_node;
4808 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
4809 build_zero_cst (type), fold_convert (type, value));
4812 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4813 /* Avoid repeated diagnostics. */
4814 token->u.value = value;
4815 return value;
4818 /* Parse a user-defined string constant. Returns a call to a user-defined
4819 literal operator taking a character pointer and the length of the string
4820 as arguments. */
4822 static tree
4823 cp_parser_userdef_string_literal (tree literal)
4825 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4826 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4827 tree value = USERDEF_LITERAL_VALUE (literal);
4828 int len = TREE_STRING_LENGTH (value)
4829 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4830 tree decl;
4832 /* Build up a call to the user-defined operator. */
4833 /* Lookup the name we got back from the id-expression. */
4834 releasing_vec args;
4835 vec_safe_push (args, value);
4836 vec_safe_push (args, build_int_cst (size_type_node, len));
4837 decl = lookup_literal_operator (name, args);
4839 if (decl && decl != error_mark_node)
4840 return finish_call_expr (decl, &args, false, true,
4841 tf_warning_or_error);
4843 /* Look for a suitable template function, either (C++20) with a single
4844 parameter of class type, or (N3599) with typename parameter CharT and
4845 parameter pack CharT... */
4846 args->truncate (0);
4847 decl = lookup_literal_operator (name, args);
4848 if (decl && decl != error_mark_node)
4850 /* Use resolve_nondeduced_context to try to choose one form of template
4851 or the other. */
4852 tree tmpl_args = make_tree_vec (1);
4853 TREE_VEC_ELT (tmpl_args, 0) = value;
4854 decl = lookup_template_function (decl, tmpl_args);
4855 tree res = resolve_nondeduced_context (decl, tf_none);
4856 if (DECL_P (res))
4857 decl = res;
4858 else
4860 TREE_OPERAND (decl, 1) = make_string_pack (value);
4861 res = resolve_nondeduced_context (decl, tf_none);
4862 if (DECL_P (res))
4863 decl = res;
4865 if (!DECL_P (decl) && cxx_dialect > cxx17)
4866 TREE_OPERAND (decl, 1) = tmpl_args;
4867 return finish_call_expr (decl, &args, false, true,
4868 tf_warning_or_error);
4871 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4872 name, TREE_TYPE (value), size_type_node);
4873 return error_mark_node;
4877 /* Basic concepts [gram.basic] */
4879 /* Parse a translation-unit.
4881 translation-unit:
4882 declaration-seq [opt] */
4884 static void
4885 cp_parser_translation_unit (cp_parser* parser)
4887 gcc_checking_assert (!cp_error_declarator);
4889 /* Create the declarator obstack. */
4890 gcc_obstack_init (&declarator_obstack);
4891 /* Create the error declarator. */
4892 cp_error_declarator = make_declarator (cdk_error);
4893 /* Create the empty parameter list. */
4894 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4895 UNKNOWN_LOCATION);
4896 /* Remember where the base of the declarator obstack lies. */
4897 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4899 push_deferring_access_checks (flag_access_control
4900 ? dk_no_deferred : dk_no_check);
4902 module_parse mp_state = MP_NOT_MODULE;
4903 if (modules_p () && !header_module_p ())
4904 mp_state = MP_FIRST;
4906 bool implicit_extern_c = false;
4908 /* Parse until EOF. */
4909 for (;;)
4911 cp_token *token = cp_lexer_peek_token (parser->lexer);
4913 /* If we're entering or exiting a region that's implicitly
4914 extern "C", modify the lang context appropriately. This is
4915 so horrible. Please die. */
4916 if (implicit_extern_c
4917 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4919 implicit_extern_c = !implicit_extern_c;
4920 if (implicit_extern_c)
4921 push_lang_context (lang_name_c);
4922 else
4923 pop_lang_context ();
4926 if (token->type == CPP_EOF)
4927 break;
4929 if (modules_p ())
4931 /* Top-level module declarations are ok, and change the
4932 portion of file we're in. Top-level import declarations
4933 are significant for the import portions. */
4935 cp_token *next = token;
4936 bool exporting = token->keyword == RID__EXPORT;
4937 if (exporting)
4939 cp_lexer_consume_token (parser->lexer);
4940 next = cp_lexer_peek_token (parser->lexer);
4942 if (next->keyword == RID__MODULE)
4944 mp_state
4945 = cp_parser_module_declaration (parser, mp_state, exporting);
4946 continue;
4948 else if (next->keyword == RID__IMPORT)
4950 if (mp_state == MP_FIRST)
4951 mp_state = MP_NOT_MODULE;
4952 cp_parser_import_declaration (parser, mp_state, exporting);
4953 continue;
4955 else
4956 gcc_checking_assert (!exporting);
4958 if (mp_state == MP_GLOBAL && token->main_source_p)
4960 static bool warned = false;
4961 if (!warned)
4963 warned = true;
4964 error_at (token->location,
4965 "global module fragment contents must be"
4966 " from preprocessor inclusion");
4971 /* This relies on the ordering of module_parse values. */
4972 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
4973 /* We're no longer in the import portion of a named module. */
4974 mp_state = module_parse (mp_state + 1);
4975 else if (mp_state == MP_FIRST)
4976 mp_state = MP_NOT_MODULE;
4978 if (token->type == CPP_CLOSE_BRACE)
4980 cp_parser_error (parser, "expected declaration");
4981 cp_lexer_consume_token (parser->lexer);
4982 /* If the next token is now a `;', consume it. */
4983 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4984 cp_lexer_consume_token (parser->lexer);
4986 else
4987 cp_parser_toplevel_declaration (parser);
4990 /* Get rid of the token array; we don't need it any more. */
4991 cp_lexer_destroy (parser->lexer);
4992 parser->lexer = NULL;
4994 /* The EOF should have reset this. */
4995 gcc_checking_assert (!implicit_extern_c);
4997 /* Make sure the declarator obstack was fully cleaned up. */
4998 gcc_assert (obstack_next_free (&declarator_obstack)
4999 == declarator_obstack_base);
5002 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5003 decltype context. */
5005 static inline tsubst_flags_t
5006 complain_flags (bool decltype_p)
5008 tsubst_flags_t complain = tf_warning_or_error;
5009 if (decltype_p)
5010 complain |= tf_decltype;
5011 return complain;
5014 /* We're about to parse a collection of statements. If we're currently
5015 parsing tentatively, set up a firewall so that any nested
5016 cp_parser_commit_to_tentative_parse won't affect the current context. */
5018 static cp_token_position
5019 cp_parser_start_tentative_firewall (cp_parser *parser)
5021 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5022 return 0;
5024 cp_parser_parse_tentatively (parser);
5025 cp_parser_commit_to_topmost_tentative_parse (parser);
5026 return cp_lexer_token_position (parser->lexer, false);
5029 /* We've finished parsing the collection of statements. Wrap up the
5030 firewall and replace the relevant tokens with the parsed form. */
5032 static void
5033 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5034 tree expr)
5036 if (!start)
5037 return;
5039 /* Finish the firewall level. */
5040 cp_parser_parse_definitely (parser);
5041 /* And remember the result of the parse for when we try again. */
5042 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5043 token->type = CPP_PREPARSED_EXPR;
5044 token->u.value = expr;
5045 token->keyword = RID_MAX;
5046 cp_lexer_purge_tokens_after (parser->lexer, start);
5049 /* Like the above functions, but let the user modify the tokens. Used by
5050 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5051 later parses, so it makes sense to localize the effects of
5052 cp_parser_commit_to_tentative_parse. */
5054 struct tentative_firewall
5056 cp_parser *parser;
5057 bool set;
5059 tentative_firewall (cp_parser *p): parser(p)
5061 /* If we're currently parsing tentatively, start a committed level as a
5062 firewall and then an inner tentative parse. */
5063 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5065 cp_parser_parse_tentatively (parser);
5066 cp_parser_commit_to_topmost_tentative_parse (parser);
5067 cp_parser_parse_tentatively (parser);
5071 ~tentative_firewall()
5073 if (set)
5075 /* Finish the inner tentative parse and the firewall, propagating any
5076 uncommitted error state to the outer tentative parse. */
5077 bool err = cp_parser_error_occurred (parser);
5078 cp_parser_parse_definitely (parser);
5079 cp_parser_parse_definitely (parser);
5080 if (err)
5081 cp_parser_simulate_error (parser);
5086 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5087 This class is for tracking such a matching pair of symbols.
5088 In particular, it tracks the location of the first token,
5089 so that if the second token is missing, we can highlight the
5090 location of the first token when notifying the user about the
5091 problem. */
5093 template <typename traits_t>
5094 class token_pair
5096 public:
5097 /* token_pair's ctor. */
5098 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5100 /* If the next token is the opening symbol for this pair, consume it and
5101 return true.
5102 Otherwise, issue an error and return false.
5103 In either case, record the location of the opening token. */
5105 bool require_open (cp_parser *parser)
5107 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5108 return cp_parser_require (parser, traits_t::open_token_type,
5109 traits_t::required_token_open);
5112 /* Consume the next token from PARSER, recording its location as
5113 that of the opening token within the pair. */
5115 cp_token * consume_open (cp_parser *parser)
5117 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5118 gcc_assert (tok->type == traits_t::open_token_type);
5119 m_open_loc = tok->location;
5120 return tok;
5123 /* If the next token is the closing symbol for this pair, consume it
5124 and return it.
5125 Otherwise, issue an error, highlighting the location of the
5126 corresponding opening token, and return NULL. */
5128 cp_token *require_close (cp_parser *parser) const
5130 return cp_parser_require (parser, traits_t::close_token_type,
5131 traits_t::required_token_close,
5132 m_open_loc);
5135 location_t open_location () const { return m_open_loc; }
5137 private:
5138 location_t m_open_loc;
5141 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5143 struct matching_paren_traits
5145 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5146 static const enum required_token required_token_open = RT_OPEN_PAREN;
5147 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5148 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5151 /* "matching_parens" is a token_pair<T> class for tracking matching
5152 pairs of parentheses. */
5154 typedef token_pair<matching_paren_traits> matching_parens;
5156 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5158 struct matching_brace_traits
5160 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5161 static const enum required_token required_token_open = RT_OPEN_BRACE;
5162 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5163 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5166 /* "matching_braces" is a token_pair<T> class for tracking matching
5167 pairs of braces. */
5169 typedef token_pair<matching_brace_traits> matching_braces;
5172 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5173 enclosing parentheses. */
5175 static cp_expr
5176 cp_parser_statement_expr (cp_parser *parser)
5178 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5180 /* Consume the '('. */
5181 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5182 matching_parens parens;
5183 parens.consume_open (parser);
5184 /* Start the statement-expression. */
5185 tree expr = begin_stmt_expr ();
5186 /* Parse the compound-statement. */
5187 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
5188 /* Finish up. */
5189 expr = finish_stmt_expr (expr, false);
5190 /* Consume the ')'. */
5191 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5192 if (!parens.require_close (parser))
5193 cp_parser_skip_to_end_of_statement (parser);
5195 cp_parser_end_tentative_firewall (parser, start, expr);
5196 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5197 return cp_expr (expr, combined_loc);
5200 /* Expressions [gram.expr] */
5202 /* Parse a fold-operator.
5204 fold-operator:
5205 - * / % ^ & | = < > << >>
5206 = -= *= /= %= ^= &= |= <<= >>=
5207 == != <= >= && || , .* ->*
5209 This returns the tree code corresponding to the matched operator
5210 as an int. When the current token matches a compound assignment
5211 operator, the resulting tree code is the negative value of the
5212 non-assignment operator. */
5214 static int
5215 cp_parser_fold_operator (cp_token *token)
5217 switch (token->type)
5219 case CPP_PLUS: return PLUS_EXPR;
5220 case CPP_MINUS: return MINUS_EXPR;
5221 case CPP_MULT: return MULT_EXPR;
5222 case CPP_DIV: return TRUNC_DIV_EXPR;
5223 case CPP_MOD: return TRUNC_MOD_EXPR;
5224 case CPP_XOR: return BIT_XOR_EXPR;
5225 case CPP_AND: return BIT_AND_EXPR;
5226 case CPP_OR: return BIT_IOR_EXPR;
5227 case CPP_LSHIFT: return LSHIFT_EXPR;
5228 case CPP_RSHIFT: return RSHIFT_EXPR;
5230 case CPP_EQ: return -NOP_EXPR;
5231 case CPP_PLUS_EQ: return -PLUS_EXPR;
5232 case CPP_MINUS_EQ: return -MINUS_EXPR;
5233 case CPP_MULT_EQ: return -MULT_EXPR;
5234 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5235 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5236 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5237 case CPP_AND_EQ: return -BIT_AND_EXPR;
5238 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5239 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5240 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5242 case CPP_EQ_EQ: return EQ_EXPR;
5243 case CPP_NOT_EQ: return NE_EXPR;
5244 case CPP_LESS: return LT_EXPR;
5245 case CPP_GREATER: return GT_EXPR;
5246 case CPP_LESS_EQ: return LE_EXPR;
5247 case CPP_GREATER_EQ: return GE_EXPR;
5249 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5250 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5252 case CPP_COMMA: return COMPOUND_EXPR;
5254 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5255 case CPP_DEREF_STAR: return MEMBER_REF;
5257 default: return ERROR_MARK;
5261 /* Returns true if CODE indicates a binary expression, which is not allowed in
5262 the LHS of a fold-expression. More codes will need to be added to use this
5263 function in other contexts. */
5265 static bool
5266 is_binary_op (tree_code code)
5268 switch (code)
5270 case PLUS_EXPR:
5271 case POINTER_PLUS_EXPR:
5272 case MINUS_EXPR:
5273 case MULT_EXPR:
5274 case TRUNC_DIV_EXPR:
5275 case TRUNC_MOD_EXPR:
5276 case BIT_XOR_EXPR:
5277 case BIT_AND_EXPR:
5278 case BIT_IOR_EXPR:
5279 case LSHIFT_EXPR:
5280 case RSHIFT_EXPR:
5282 case MODOP_EXPR:
5284 case EQ_EXPR:
5285 case NE_EXPR:
5286 case LE_EXPR:
5287 case GE_EXPR:
5288 case LT_EXPR:
5289 case GT_EXPR:
5291 case TRUTH_ANDIF_EXPR:
5292 case TRUTH_ORIF_EXPR:
5294 case COMPOUND_EXPR:
5296 case DOTSTAR_EXPR:
5297 case MEMBER_REF:
5298 return true;
5300 default:
5301 return false;
5305 /* If the next token is a suitable fold operator, consume it and return as
5306 the function above. */
5308 static int
5309 cp_parser_fold_operator (cp_parser *parser)
5311 cp_token* token = cp_lexer_peek_token (parser->lexer);
5312 int code = cp_parser_fold_operator (token);
5313 if (code != ERROR_MARK)
5314 cp_lexer_consume_token (parser->lexer);
5315 return code;
5318 /* Parse a fold-expression.
5320 fold-expression:
5321 ( ... folding-operator cast-expression)
5322 ( cast-expression folding-operator ... )
5323 ( cast-expression folding operator ... folding-operator cast-expression)
5325 Note that the '(' and ')' are matched in primary expression. */
5327 static cp_expr
5328 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5330 cp_id_kind pidk;
5332 // Left fold.
5333 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5335 if (expr1)
5336 return error_mark_node;
5337 cp_lexer_consume_token (parser->lexer);
5338 int op = cp_parser_fold_operator (parser);
5339 if (op == ERROR_MARK)
5341 cp_parser_error (parser, "expected binary operator");
5342 return error_mark_node;
5345 tree expr = cp_parser_cast_expression (parser, false, false,
5346 false, &pidk);
5347 if (expr == error_mark_node)
5348 return error_mark_node;
5349 return finish_left_unary_fold_expr (expr, op);
5352 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5353 int op = cp_parser_fold_operator (parser);
5354 if (op == ERROR_MARK)
5356 cp_parser_error (parser, "expected binary operator");
5357 return error_mark_node;
5360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5362 cp_parser_error (parser, "expected ...");
5363 return error_mark_node;
5365 cp_lexer_consume_token (parser->lexer);
5367 /* The operands of a fold-expression are cast-expressions, so binary or
5368 conditional expressions are not allowed. We check this here to avoid
5369 tentative parsing. */
5370 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5371 /* OK, the expression was parenthesized. */;
5372 else if (is_binary_op (TREE_CODE (expr1)))
5373 error_at (location_of (expr1),
5374 "binary expression in operand of fold-expression");
5375 else if (TREE_CODE (expr1) == COND_EXPR
5376 || (REFERENCE_REF_P (expr1)
5377 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5378 error_at (location_of (expr1),
5379 "conditional expression in operand of fold-expression");
5381 // Right fold.
5382 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5383 return finish_right_unary_fold_expr (expr1, op);
5385 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5387 cp_parser_error (parser, "mismatched operator in fold-expression");
5388 return error_mark_node;
5390 cp_lexer_consume_token (parser->lexer);
5392 // Binary left or right fold.
5393 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5394 if (expr2 == error_mark_node)
5395 return error_mark_node;
5396 return finish_binary_fold_expr (expr1, expr2, op);
5399 /* Parse a primary-expression.
5401 primary-expression:
5402 literal
5403 this
5404 ( expression )
5405 id-expression
5406 lambda-expression (C++11)
5408 GNU Extensions:
5410 primary-expression:
5411 ( compound-statement )
5412 __builtin_va_arg ( assignment-expression , type-id )
5413 __builtin_offsetof ( type-id , offsetof-expression )
5415 C++ Extensions:
5416 __has_nothrow_assign ( type-id )
5417 __has_nothrow_constructor ( type-id )
5418 __has_nothrow_copy ( type-id )
5419 __has_trivial_assign ( type-id )
5420 __has_trivial_constructor ( type-id )
5421 __has_trivial_copy ( type-id )
5422 __has_trivial_destructor ( type-id )
5423 __has_virtual_destructor ( type-id )
5424 __is_abstract ( type-id )
5425 __is_base_of ( type-id , type-id )
5426 __is_class ( type-id )
5427 __is_empty ( type-id )
5428 __is_enum ( type-id )
5429 __is_final ( type-id )
5430 __is_literal_type ( type-id )
5431 __is_pod ( type-id )
5432 __is_polymorphic ( type-id )
5433 __is_std_layout ( type-id )
5434 __is_trivial ( type-id )
5435 __is_union ( type-id )
5437 Objective-C++ Extension:
5439 primary-expression:
5440 objc-expression
5442 literal:
5443 __null
5445 ADDRESS_P is true iff this expression was immediately preceded by
5446 "&" and therefore might denote a pointer-to-member. CAST_P is true
5447 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5448 true iff this expression is a template argument.
5450 Returns a representation of the expression. Upon return, *IDK
5451 indicates what kind of id-expression (if any) was present. */
5453 static cp_expr
5454 cp_parser_primary_expression (cp_parser *parser,
5455 bool address_p,
5456 bool cast_p,
5457 bool template_arg_p,
5458 bool decltype_p,
5459 cp_id_kind *idk)
5461 cp_token *token = NULL;
5463 /* Assume the primary expression is not an id-expression. */
5464 *idk = CP_ID_KIND_NONE;
5466 /* Peek at the next token. */
5467 token = cp_lexer_peek_token (parser->lexer);
5468 switch ((int) token->type)
5470 /* literal:
5471 integer-literal
5472 character-literal
5473 floating-literal
5474 string-literal
5475 boolean-literal
5476 pointer-literal
5477 user-defined-literal */
5478 case CPP_CHAR:
5479 case CPP_CHAR16:
5480 case CPP_CHAR32:
5481 case CPP_WCHAR:
5482 case CPP_UTF8CHAR:
5483 case CPP_NUMBER:
5484 case CPP_PREPARSED_EXPR:
5485 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5486 return cp_parser_userdef_numeric_literal (parser);
5487 token = cp_lexer_consume_token (parser->lexer);
5488 if (TREE_CODE (token->u.value) == FIXED_CST)
5490 error_at (token->location,
5491 "fixed-point types not supported in C++");
5492 return error_mark_node;
5494 /* Floating-point literals are only allowed in an integral
5495 constant expression if they are cast to an integral or
5496 enumeration type. */
5497 if (TREE_CODE (token->u.value) == REAL_CST
5498 && parser->integral_constant_expression_p
5499 && pedantic)
5501 /* CAST_P will be set even in invalid code like "int(2.7 +
5502 ...)". Therefore, we have to check that the next token
5503 is sure to end the cast. */
5504 if (cast_p)
5506 cp_token *next_token;
5508 next_token = cp_lexer_peek_token (parser->lexer);
5509 if (/* The comma at the end of an
5510 enumerator-definition. */
5511 next_token->type != CPP_COMMA
5512 /* The curly brace at the end of an enum-specifier. */
5513 && next_token->type != CPP_CLOSE_BRACE
5514 /* The end of a statement. */
5515 && next_token->type != CPP_SEMICOLON
5516 /* The end of the cast-expression. */
5517 && next_token->type != CPP_CLOSE_PAREN
5518 /* The end of an array bound. */
5519 && next_token->type != CPP_CLOSE_SQUARE
5520 /* The closing ">" in a template-argument-list. */
5521 && (next_token->type != CPP_GREATER
5522 || parser->greater_than_is_operator_p)
5523 /* C++0x only: A ">>" treated like two ">" tokens,
5524 in a template-argument-list. */
5525 && (next_token->type != CPP_RSHIFT
5526 || (cxx_dialect == cxx98)
5527 || parser->greater_than_is_operator_p))
5528 cast_p = false;
5531 /* If we are within a cast, then the constraint that the
5532 cast is to an integral or enumeration type will be
5533 checked at that point. If we are not within a cast, then
5534 this code is invalid. */
5535 if (!cast_p)
5536 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5538 return (cp_expr (token->u.value, token->location)
5539 .maybe_add_location_wrapper ());
5541 case CPP_CHAR_USERDEF:
5542 case CPP_CHAR16_USERDEF:
5543 case CPP_CHAR32_USERDEF:
5544 case CPP_WCHAR_USERDEF:
5545 case CPP_UTF8CHAR_USERDEF:
5546 return cp_parser_userdef_char_literal (parser);
5548 case CPP_STRING:
5549 case CPP_STRING16:
5550 case CPP_STRING32:
5551 case CPP_WSTRING:
5552 case CPP_UTF8STRING:
5553 case CPP_STRING_USERDEF:
5554 case CPP_STRING16_USERDEF:
5555 case CPP_STRING32_USERDEF:
5556 case CPP_WSTRING_USERDEF:
5557 case CPP_UTF8STRING_USERDEF:
5558 /* ??? Should wide strings be allowed when parser->translate_strings_p
5559 is false (i.e. in attributes)? If not, we can kill the third
5560 argument to cp_parser_string_literal. */
5561 return (cp_parser_string_literal (parser,
5562 parser->translate_strings_p,
5563 true)
5564 .maybe_add_location_wrapper ());
5566 case CPP_OPEN_PAREN:
5567 /* If we see `( { ' then we are looking at the beginning of
5568 a GNU statement-expression. */
5569 if (cp_parser_allow_gnu_extensions_p (parser)
5570 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5572 /* Statement-expressions are not allowed by the standard. */
5573 pedwarn (token->location, OPT_Wpedantic,
5574 "ISO C++ forbids braced-groups within expressions");
5576 /* And they're not allowed outside of a function-body; you
5577 cannot, for example, write:
5579 int i = ({ int j = 3; j + 1; });
5581 at class or namespace scope. */
5582 if (!parser->in_function_body
5583 || parser->in_template_argument_list_p)
5585 error_at (token->location,
5586 "statement-expressions are not allowed outside "
5587 "functions nor in template-argument lists");
5588 cp_parser_skip_to_end_of_block_or_statement (parser);
5589 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5590 cp_lexer_consume_token (parser->lexer);
5591 return error_mark_node;
5593 else
5594 return cp_parser_statement_expr (parser);
5596 /* Otherwise it's a normal parenthesized expression. */
5598 cp_expr expr;
5599 bool saved_greater_than_is_operator_p;
5601 location_t open_paren_loc = token->location;
5603 /* Consume the `('. */
5604 matching_parens parens;
5605 parens.consume_open (parser);
5606 /* Within a parenthesized expression, a `>' token is always
5607 the greater-than operator. */
5608 saved_greater_than_is_operator_p
5609 = parser->greater_than_is_operator_p;
5610 parser->greater_than_is_operator_p = true;
5612 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5613 /* Left fold expression. */
5614 expr = NULL_TREE;
5615 else
5616 /* Parse the parenthesized expression. */
5617 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5619 token = cp_lexer_peek_token (parser->lexer);
5620 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5622 expr = cp_parser_fold_expression (parser, expr);
5623 if (expr != error_mark_node
5624 && cxx_dialect < cxx17)
5625 pedwarn (input_location, OPT_Wc__17_extensions,
5626 "fold-expressions only available with %<-std=c++17%> "
5627 "or %<-std=gnu++17%>");
5629 else
5630 /* Let the front end know that this expression was
5631 enclosed in parentheses. This matters in case, for
5632 example, the expression is of the form `A::B', since
5633 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5634 not. */
5635 expr = finish_parenthesized_expr (expr);
5637 /* DR 705: Wrapping an unqualified name in parentheses
5638 suppresses arg-dependent lookup. We want to pass back
5639 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5640 (c++/37862), but none of the others. */
5641 if (*idk != CP_ID_KIND_QUALIFIED)
5642 *idk = CP_ID_KIND_NONE;
5644 /* The `>' token might be the end of a template-id or
5645 template-parameter-list now. */
5646 parser->greater_than_is_operator_p
5647 = saved_greater_than_is_operator_p;
5649 /* Consume the `)'. */
5650 token = cp_lexer_peek_token (parser->lexer);
5651 location_t close_paren_loc = token->location;
5652 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5653 expr.set_range (open_paren_loc, close_paren_loc);
5654 if (no_wparens)
5655 suppress_warning (expr, OPT_Wparentheses);
5656 if (!parens.require_close (parser)
5657 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5658 cp_parser_skip_to_end_of_statement (parser);
5660 return expr;
5663 case CPP_OPEN_SQUARE:
5665 if (c_dialect_objc ())
5667 /* We might have an Objective-C++ message. */
5668 cp_parser_parse_tentatively (parser);
5669 tree msg = cp_parser_objc_message_expression (parser);
5670 /* If that works out, we're done ... */
5671 if (cp_parser_parse_definitely (parser))
5672 return msg;
5673 /* ... else, fall though to see if it's a lambda. */
5675 cp_expr lam = cp_parser_lambda_expression (parser);
5676 /* Don't warn about a failed tentative parse. */
5677 if (cp_parser_error_occurred (parser))
5678 return error_mark_node;
5679 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5680 return lam;
5683 case CPP_OBJC_STRING:
5684 if (c_dialect_objc ())
5685 /* We have an Objective-C++ string literal. */
5686 return cp_parser_objc_expression (parser);
5687 cp_parser_error (parser, "expected primary-expression");
5688 return error_mark_node;
5690 case CPP_KEYWORD:
5691 switch (token->keyword)
5693 /* These two are the boolean literals. */
5694 case RID_TRUE:
5695 cp_lexer_consume_token (parser->lexer);
5696 return cp_expr (boolean_true_node, token->location);
5697 case RID_FALSE:
5698 cp_lexer_consume_token (parser->lexer);
5699 return cp_expr (boolean_false_node, token->location);
5701 /* The `__null' literal. */
5702 case RID_NULL:
5703 cp_lexer_consume_token (parser->lexer);
5704 return cp_expr (null_node, token->location);
5706 /* The `nullptr' literal. */
5707 case RID_NULLPTR:
5708 cp_lexer_consume_token (parser->lexer);
5709 return cp_expr (nullptr_node, token->location);
5711 /* Recognize the `this' keyword. */
5712 case RID_THIS:
5713 cp_lexer_consume_token (parser->lexer);
5714 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5716 error_at (token->location,
5717 "%<this%> may not be used in this context");
5718 return error_mark_node;
5720 /* Pointers cannot appear in constant-expressions. */
5721 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5722 return error_mark_node;
5723 return cp_expr (finish_this_expr (), token->location);
5725 /* The `operator' keyword can be the beginning of an
5726 id-expression. */
5727 case RID_OPERATOR:
5728 goto id_expression;
5730 case RID_FUNCTION_NAME:
5731 case RID_PRETTY_FUNCTION_NAME:
5732 case RID_C99_FUNCTION_NAME:
5734 non_integral_constant name;
5736 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5737 __func__ are the names of variables -- but they are
5738 treated specially. Therefore, they are handled here,
5739 rather than relying on the generic id-expression logic
5740 below. Grammatically, these names are id-expressions.
5742 Consume the token. */
5743 token = cp_lexer_consume_token (parser->lexer);
5745 switch (token->keyword)
5747 case RID_FUNCTION_NAME:
5748 name = NIC_FUNC_NAME;
5749 break;
5750 case RID_PRETTY_FUNCTION_NAME:
5751 name = NIC_PRETTY_FUNC;
5752 break;
5753 case RID_C99_FUNCTION_NAME:
5754 name = NIC_C99_FUNC;
5755 break;
5756 default:
5757 gcc_unreachable ();
5760 if (cp_parser_non_integral_constant_expression (parser, name))
5761 return error_mark_node;
5763 /* Look up the name. */
5764 return finish_fname (token->u.value);
5767 case RID_VA_ARG:
5769 tree expression;
5770 tree type;
5771 location_t type_location;
5772 location_t start_loc
5773 = cp_lexer_peek_token (parser->lexer)->location;
5774 /* The `__builtin_va_arg' construct is used to handle
5775 `va_arg'. Consume the `__builtin_va_arg' token. */
5776 cp_lexer_consume_token (parser->lexer);
5777 /* Look for the opening `('. */
5778 matching_parens parens;
5779 parens.require_open (parser);
5780 /* Now, parse the assignment-expression. */
5781 expression = cp_parser_assignment_expression (parser);
5782 /* Look for the `,'. */
5783 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5784 type_location = cp_lexer_peek_token (parser->lexer)->location;
5785 /* Parse the type-id. */
5787 type_id_in_expr_sentinel s (parser);
5788 type = cp_parser_type_id (parser);
5790 /* Look for the closing `)'. */
5791 location_t finish_loc
5792 = cp_lexer_peek_token (parser->lexer)->location;
5793 parens.require_close (parser);
5794 /* Using `va_arg' in a constant-expression is not
5795 allowed. */
5796 if (cp_parser_non_integral_constant_expression (parser,
5797 NIC_VA_ARG))
5798 return error_mark_node;
5799 /* Construct a location of the form:
5800 __builtin_va_arg (v, int)
5801 ~~~~~~~~~~~~~~~~~~~~~^~~~
5802 with the caret at the type, ranging from the start of the
5803 "__builtin_va_arg" token to the close paren. */
5804 location_t combined_loc
5805 = make_location (type_location, start_loc, finish_loc);
5806 return build_x_va_arg (combined_loc, expression, type);
5809 case RID_OFFSETOF:
5810 return cp_parser_builtin_offsetof (parser);
5812 case RID_HAS_NOTHROW_ASSIGN:
5813 case RID_HAS_NOTHROW_CONSTRUCTOR:
5814 case RID_HAS_NOTHROW_COPY:
5815 case RID_HAS_TRIVIAL_ASSIGN:
5816 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5817 case RID_HAS_TRIVIAL_COPY:
5818 case RID_HAS_TRIVIAL_DESTRUCTOR:
5819 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5820 case RID_HAS_VIRTUAL_DESTRUCTOR:
5821 case RID_IS_ABSTRACT:
5822 case RID_IS_AGGREGATE:
5823 case RID_IS_BASE_OF:
5824 case RID_IS_CLASS:
5825 case RID_IS_EMPTY:
5826 case RID_IS_ENUM:
5827 case RID_IS_FINAL:
5828 case RID_IS_LAYOUT_COMPATIBLE:
5829 case RID_IS_LITERAL_TYPE:
5830 case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
5831 case RID_IS_POD:
5832 case RID_IS_POLYMORPHIC:
5833 case RID_IS_SAME_AS:
5834 case RID_IS_STD_LAYOUT:
5835 case RID_IS_TRIVIAL:
5836 case RID_IS_TRIVIALLY_ASSIGNABLE:
5837 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5838 case RID_IS_TRIVIALLY_COPYABLE:
5839 case RID_IS_UNION:
5840 case RID_IS_ASSIGNABLE:
5841 case RID_IS_CONSTRUCTIBLE:
5842 case RID_IS_NOTHROW_ASSIGNABLE:
5843 case RID_IS_NOTHROW_CONSTRUCTIBLE:
5844 return cp_parser_trait_expr (parser, token->keyword);
5846 // C++ concepts
5847 case RID_REQUIRES:
5848 return cp_parser_requires_expression (parser);
5850 /* Objective-C++ expressions. */
5851 case RID_AT_ENCODE:
5852 case RID_AT_PROTOCOL:
5853 case RID_AT_SELECTOR:
5854 return cp_parser_objc_expression (parser);
5856 case RID_TEMPLATE:
5857 if (parser->in_function_body
5858 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5859 == CPP_LESS))
5861 error_at (token->location,
5862 "a template declaration cannot appear at block scope");
5863 cp_parser_skip_to_end_of_block_or_statement (parser);
5864 return error_mark_node;
5866 /* FALLTHRU */
5867 default:
5868 cp_parser_error (parser, "expected primary-expression");
5869 return error_mark_node;
5872 /* An id-expression can start with either an identifier, a
5873 `::' as the beginning of a qualified-id, or the "operator"
5874 keyword. */
5875 case CPP_NAME:
5876 case CPP_SCOPE:
5877 case CPP_TEMPLATE_ID:
5878 case CPP_NESTED_NAME_SPECIFIER:
5880 id_expression:
5881 cp_expr id_expression;
5882 cp_expr decl;
5883 const char *error_msg;
5884 bool template_p;
5885 bool done;
5886 cp_token *id_expr_token;
5888 /* Parse the id-expression. */
5889 id_expression
5890 = cp_parser_id_expression (parser,
5891 /*template_keyword_p=*/false,
5892 /*check_dependency_p=*/true,
5893 &template_p,
5894 /*declarator_p=*/false,
5895 /*optional_p=*/false);
5896 if (id_expression == error_mark_node)
5897 return error_mark_node;
5898 id_expr_token = token;
5899 token = cp_lexer_peek_token (parser->lexer);
5900 done = (token->type != CPP_OPEN_SQUARE
5901 && token->type != CPP_OPEN_PAREN
5902 && token->type != CPP_DOT
5903 && token->type != CPP_DEREF
5904 && token->type != CPP_PLUS_PLUS
5905 && token->type != CPP_MINUS_MINUS);
5906 /* If we have a template-id, then no further lookup is
5907 required. If the template-id was for a template-class, we
5908 will sometimes have a TYPE_DECL at this point. */
5909 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5910 || TREE_CODE (id_expression) == TYPE_DECL)
5911 decl = id_expression;
5912 /* Look up the name. */
5913 else
5915 tree ambiguous_decls;
5917 /* If we already know that this lookup is ambiguous, then
5918 we've already issued an error message; there's no reason
5919 to check again. */
5920 if (id_expr_token->type == CPP_NAME
5921 && id_expr_token->error_reported)
5923 cp_parser_simulate_error (parser);
5924 return error_mark_node;
5927 decl = cp_parser_lookup_name (parser, id_expression,
5928 none_type,
5929 template_p,
5930 /*is_namespace=*/false,
5931 /*check_dependency=*/true,
5932 &ambiguous_decls,
5933 id_expression.get_location ());
5934 /* If the lookup was ambiguous, an error will already have
5935 been issued. */
5936 if (ambiguous_decls)
5937 return error_mark_node;
5939 /* In Objective-C++, we may have an Objective-C 2.0
5940 dot-syntax for classes here. */
5941 if (c_dialect_objc ()
5942 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5943 && TREE_CODE (decl) == TYPE_DECL
5944 && objc_is_class_name (decl))
5946 tree component;
5947 cp_lexer_consume_token (parser->lexer);
5948 component = cp_parser_identifier (parser);
5949 if (component == error_mark_node)
5950 return error_mark_node;
5952 tree result = objc_build_class_component_ref (id_expression,
5953 component);
5954 /* Build a location of the form:
5955 expr.component
5956 ~~~~~^~~~~~~~~
5957 with caret at the start of the component name (at
5958 input_location), ranging from the start of the id_expression
5959 to the end of the component name. */
5960 location_t combined_loc
5961 = make_location (input_location, id_expression.get_start (),
5962 get_finish (input_location));
5963 protected_set_expr_location (result, combined_loc);
5964 return result;
5967 /* In Objective-C++, an instance variable (ivar) may be preferred
5968 to whatever cp_parser_lookup_name() found.
5969 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5970 rest of c-family, we have to do a little extra work to preserve
5971 any location information in cp_expr "decl". Given that
5972 objc_lookup_ivar is implemented in "c-family" and "objc", we
5973 have a trip through the pure "tree" type, rather than cp_expr.
5974 Naively copying it back to "decl" would implicitly give the
5975 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5976 store an EXPR_LOCATION. Hence we only update "decl" (and
5977 hence its location_t) if we get back a different tree node. */
5978 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5979 id_expression);
5980 if (decl_tree != decl.get_value ())
5981 decl = cp_expr (decl_tree);
5983 /* If name lookup gives us a SCOPE_REF, then the
5984 qualifying scope was dependent. */
5985 if (TREE_CODE (decl) == SCOPE_REF)
5987 /* At this point, we do not know if DECL is a valid
5988 integral constant expression. We assume that it is
5989 in fact such an expression, so that code like:
5991 template <int N> struct A {
5992 int a[B<N>::i];
5995 is accepted. At template-instantiation time, we
5996 will check that B<N>::i is actually a constant. */
5997 return decl;
5999 /* Check to see if DECL is a local variable in a context
6000 where that is forbidden. */
6001 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6002 && local_variable_p (decl)
6003 /* DR 2082 permits local variables in unevaluated contexts
6004 within a default argument. */
6005 && !cp_unevaluated_operand)
6007 const char *msg
6008 = (TREE_CODE (decl) == PARM_DECL
6009 ? _("parameter %qD may not appear in this context")
6010 : _("local variable %qD may not appear in this context"));
6011 error_at (id_expression.get_location (), msg,
6012 decl.get_value ());
6013 return error_mark_node;
6017 decl = (finish_id_expression
6018 (id_expression, decl, parser->scope,
6019 idk,
6020 parser->integral_constant_expression_p,
6021 parser->allow_non_integral_constant_expression_p,
6022 &parser->non_integral_constant_expression_p,
6023 template_p, done, address_p,
6024 template_arg_p,
6025 &error_msg,
6026 id_expression.get_location ()));
6027 if (error_msg)
6028 cp_parser_error (parser, error_msg);
6029 /* Build a location for an id-expression of the form:
6030 ::ns::id
6031 ~~~~~~^~
6035 i.e. from the start of the first token to the end of the final
6036 token, with the caret at the start of the unqualified-id. */
6037 location_t caret_loc = get_pure_location (id_expression.get_location ());
6038 location_t start_loc = get_start (id_expr_token->location);
6039 location_t finish_loc = get_finish (id_expression.get_location ());
6040 location_t combined_loc
6041 = make_location (caret_loc, start_loc, finish_loc);
6043 decl.set_location (combined_loc);
6044 return decl;
6047 /* Anything else is an error. */
6048 default:
6049 cp_parser_error (parser, "expected primary-expression");
6050 return error_mark_node;
6054 static inline cp_expr
6055 cp_parser_primary_expression (cp_parser *parser,
6056 bool address_p,
6057 bool cast_p,
6058 bool template_arg_p,
6059 cp_id_kind *idk)
6061 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6062 /*decltype*/false, idk);
6065 /* Parse an id-expression.
6067 id-expression:
6068 unqualified-id
6069 qualified-id
6071 qualified-id:
6072 :: [opt] nested-name-specifier template [opt] unqualified-id
6073 :: identifier
6074 :: operator-function-id
6075 :: template-id
6077 Return a representation of the unqualified portion of the
6078 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6079 a `::' or nested-name-specifier.
6081 Often, if the id-expression was a qualified-id, the caller will
6082 want to make a SCOPE_REF to represent the qualified-id. This
6083 function does not do this in order to avoid wastefully creating
6084 SCOPE_REFs when they are not required.
6086 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6087 `template' keyword.
6089 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6090 uninstantiated templates.
6092 If *TEMPLATE_P is non-NULL, it is set to true iff the
6093 `template' keyword is used to explicitly indicate that the entity
6094 named is a template.
6096 If DECLARATOR_P is true, the id-expression is appearing as part of
6097 a declarator, rather than as part of an expression. */
6099 static cp_expr
6100 cp_parser_id_expression (cp_parser *parser,
6101 bool template_keyword_p,
6102 bool check_dependency_p,
6103 bool *template_p,
6104 bool declarator_p,
6105 bool optional_p)
6107 bool global_scope_p;
6108 bool nested_name_specifier_p;
6110 /* Assume the `template' keyword was not used. */
6111 if (template_p)
6112 *template_p = template_keyword_p;
6114 /* Look for the optional `::' operator. */
6115 global_scope_p
6116 = (!template_keyword_p
6117 && (cp_parser_global_scope_opt (parser,
6118 /*current_scope_valid_p=*/false)
6119 != NULL_TREE));
6121 /* Look for the optional nested-name-specifier. */
6122 nested_name_specifier_p
6123 = (cp_parser_nested_name_specifier_opt (parser,
6124 /*typename_keyword_p=*/false,
6125 check_dependency_p,
6126 /*type_p=*/false,
6127 declarator_p,
6128 template_keyword_p)
6129 != NULL_TREE);
6131 /* If there is a nested-name-specifier, then we are looking at
6132 the first qualified-id production. */
6133 if (nested_name_specifier_p)
6135 tree saved_scope;
6136 tree saved_object_scope;
6137 tree saved_qualifying_scope;
6138 cp_expr unqualified_id;
6139 bool is_template;
6141 /* See if the next token is the `template' keyword. */
6142 if (!template_p)
6143 template_p = &is_template;
6144 *template_p = cp_parser_optional_template_keyword (parser);
6145 /* Name lookup we do during the processing of the
6146 unqualified-id might obliterate SCOPE. */
6147 saved_scope = parser->scope;
6148 saved_object_scope = parser->object_scope;
6149 saved_qualifying_scope = parser->qualifying_scope;
6150 /* Process the final unqualified-id. */
6151 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
6152 check_dependency_p,
6153 declarator_p,
6154 /*optional_p=*/false);
6155 /* Restore the SAVED_SCOPE for our caller. */
6156 parser->scope = saved_scope;
6157 parser->object_scope = saved_object_scope;
6158 parser->qualifying_scope = saved_qualifying_scope;
6160 return unqualified_id;
6162 /* Otherwise, if we are in global scope, then we are looking at one
6163 of the other qualified-id productions. */
6164 else if (global_scope_p)
6166 cp_token *token;
6167 tree id;
6169 /* Peek at the next token. */
6170 token = cp_lexer_peek_token (parser->lexer);
6172 /* If it's an identifier, and the next token is not a "<", then
6173 we can avoid the template-id case. This is an optimization
6174 for this common case. */
6175 if (token->type == CPP_NAME
6176 && !cp_parser_nth_token_starts_template_argument_list_p
6177 (parser, 2))
6178 return cp_parser_identifier (parser);
6180 cp_parser_parse_tentatively (parser);
6181 /* Try a template-id. */
6182 id = cp_parser_template_id_expr (parser,
6183 /*template_keyword_p=*/false,
6184 /*check_dependency_p=*/true,
6185 declarator_p);
6186 /* If that worked, we're done. */
6187 if (cp_parser_parse_definitely (parser))
6188 return id;
6190 /* Peek at the next token. (Changes in the token buffer may
6191 have invalidated the pointer obtained above.) */
6192 token = cp_lexer_peek_token (parser->lexer);
6194 switch (token->type)
6196 case CPP_NAME:
6197 return cp_parser_identifier (parser);
6199 case CPP_KEYWORD:
6200 if (token->keyword == RID_OPERATOR)
6201 return cp_parser_operator_function_id (parser);
6202 /* Fall through. */
6204 default:
6205 cp_parser_error (parser, "expected id-expression");
6206 return error_mark_node;
6209 else
6210 return cp_parser_unqualified_id (parser, template_keyword_p,
6211 /*check_dependency_p=*/true,
6212 declarator_p,
6213 optional_p);
6216 /* Parse an unqualified-id.
6218 unqualified-id:
6219 identifier
6220 operator-function-id
6221 conversion-function-id
6222 ~ class-name
6223 template-id
6225 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6226 keyword, in a construct like `A::template ...'.
6228 Returns a representation of unqualified-id. For the `identifier'
6229 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6230 production a BIT_NOT_EXPR is returned; the operand of the
6231 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6232 other productions, see the documentation accompanying the
6233 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6234 names are looked up in uninstantiated templates. If DECLARATOR_P
6235 is true, the unqualified-id is appearing as part of a declarator,
6236 rather than as part of an expression. */
6238 static cp_expr
6239 cp_parser_unqualified_id (cp_parser* parser,
6240 bool template_keyword_p,
6241 bool check_dependency_p,
6242 bool declarator_p,
6243 bool optional_p)
6245 cp_token *token;
6247 /* Peek at the next token. */
6248 token = cp_lexer_peek_token (parser->lexer);
6250 switch ((int) token->type)
6252 case CPP_NAME:
6254 tree id;
6256 /* We don't know yet whether or not this will be a
6257 template-id. */
6258 cp_parser_parse_tentatively (parser);
6259 /* Try a template-id. */
6260 id = cp_parser_template_id_expr (parser, template_keyword_p,
6261 check_dependency_p,
6262 declarator_p);
6263 /* If it worked, we're done. */
6264 if (cp_parser_parse_definitely (parser))
6265 return id;
6266 /* Otherwise, it's an ordinary identifier. */
6267 return cp_parser_identifier (parser);
6270 case CPP_TEMPLATE_ID:
6271 return cp_parser_template_id_expr (parser, template_keyword_p,
6272 check_dependency_p,
6273 declarator_p);
6275 case CPP_COMPL:
6277 tree type_decl;
6278 tree qualifying_scope;
6279 tree object_scope;
6280 tree scope;
6281 bool done;
6282 location_t tilde_loc = token->location;
6284 /* Consume the `~' token. */
6285 cp_lexer_consume_token (parser->lexer);
6286 /* Parse the class-name. The standard, as written, seems to
6287 say that:
6289 template <typename T> struct S { ~S (); };
6290 template <typename T> S<T>::~S() {}
6292 is invalid, since `~' must be followed by a class-name, but
6293 `S<T>' is dependent, and so not known to be a class.
6294 That's not right; we need to look in uninstantiated
6295 templates. A further complication arises from:
6297 template <typename T> void f(T t) {
6298 t.T::~T();
6301 Here, it is not possible to look up `T' in the scope of `T'
6302 itself. We must look in both the current scope, and the
6303 scope of the containing complete expression.
6305 Yet another issue is:
6307 struct S {
6308 int S;
6309 ~S();
6312 S::~S() {}
6314 The standard does not seem to say that the `S' in `~S'
6315 should refer to the type `S' and not the data member
6316 `S::S'. */
6318 /* DR 244 says that we look up the name after the "~" in the
6319 same scope as we looked up the qualifying name. That idea
6320 isn't fully worked out; it's more complicated than that. */
6321 scope = parser->scope;
6322 object_scope = parser->object_scope;
6323 qualifying_scope = parser->qualifying_scope;
6325 /* Check for invalid scopes. */
6326 if (scope == error_mark_node)
6328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6329 cp_lexer_consume_token (parser->lexer);
6330 return error_mark_node;
6332 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6334 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6335 error_at (token->location,
6336 "scope %qT before %<~%> is not a class-name",
6337 scope);
6338 cp_parser_simulate_error (parser);
6339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6340 cp_lexer_consume_token (parser->lexer);
6341 return error_mark_node;
6343 if (template_keyword_p)
6345 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6346 error_at (tilde_loc, "%<template%> keyword not permitted in "
6347 "destructor name");
6348 cp_parser_simulate_error (parser);
6349 return error_mark_node;
6352 gcc_assert (!scope || TYPE_P (scope));
6354 token = cp_lexer_peek_token (parser->lexer);
6356 /* Create a location with caret == start at the tilde,
6357 finishing at the end of the peeked token, e.g:
6358 ~token
6359 ^~~~~~. */
6360 location_t loc
6361 = make_location (tilde_loc, tilde_loc, token->location);
6363 /* If the name is of the form "X::~X" it's OK even if X is a
6364 typedef. */
6366 if (scope
6367 && token->type == CPP_NAME
6368 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6369 != CPP_LESS)
6370 && (token->u.value == TYPE_IDENTIFIER (scope)
6371 || (CLASS_TYPE_P (scope)
6372 && constructor_name_p (token->u.value, scope))))
6374 cp_lexer_consume_token (parser->lexer);
6375 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6378 /* ~auto means the destructor of whatever the object is. */
6379 if (cp_parser_is_keyword (token, RID_AUTO))
6381 if (cxx_dialect < cxx14)
6382 pedwarn (loc, OPT_Wc__14_extensions,
6383 "%<~auto%> only available with "
6384 "%<-std=c++14%> or %<-std=gnu++14%>");
6385 cp_lexer_consume_token (parser->lexer);
6386 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6389 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6390 declarator-id of a constructor or destructor. */
6391 if (token->type == CPP_TEMPLATE_ID && declarator_p
6392 && cxx_dialect >= cxx20)
6394 if (!cp_parser_simulate_error (parser))
6395 error_at (tilde_loc, "template-id not allowed for destructor");
6396 return error_mark_node;
6399 /* If there was an explicit qualification (S::~T), first look
6400 in the scope given by the qualification (i.e., S).
6402 Note: in the calls to cp_parser_class_name below we pass
6403 typename_type so that lookup finds the injected-class-name
6404 rather than the constructor. */
6405 done = false;
6406 type_decl = NULL_TREE;
6407 if (scope)
6409 cp_parser_parse_tentatively (parser);
6410 type_decl = cp_parser_class_name (parser,
6411 /*typename_keyword_p=*/false,
6412 /*template_keyword_p=*/false,
6413 typename_type,
6414 /*check_dependency=*/false,
6415 /*class_head_p=*/false,
6416 declarator_p);
6417 if (cp_parser_parse_definitely (parser))
6418 done = true;
6420 /* In "N::S::~S", look in "N" as well. */
6421 if (!done && scope && qualifying_scope)
6423 cp_parser_parse_tentatively (parser);
6424 parser->scope = qualifying_scope;
6425 parser->object_scope = NULL_TREE;
6426 parser->qualifying_scope = NULL_TREE;
6427 type_decl
6428 = cp_parser_class_name (parser,
6429 /*typename_keyword_p=*/false,
6430 /*template_keyword_p=*/false,
6431 typename_type,
6432 /*check_dependency=*/false,
6433 /*class_head_p=*/false,
6434 declarator_p);
6435 if (cp_parser_parse_definitely (parser))
6436 done = true;
6438 /* In "p->S::~T", look in the scope given by "*p" as well. */
6439 else if (!done && object_scope)
6441 cp_parser_parse_tentatively (parser);
6442 parser->scope = object_scope;
6443 parser->object_scope = NULL_TREE;
6444 parser->qualifying_scope = NULL_TREE;
6445 type_decl
6446 = cp_parser_class_name (parser,
6447 /*typename_keyword_p=*/false,
6448 /*template_keyword_p=*/false,
6449 typename_type,
6450 /*check_dependency=*/false,
6451 /*class_head_p=*/false,
6452 declarator_p);
6453 if (cp_parser_parse_definitely (parser))
6454 done = true;
6456 /* Look in the surrounding context. */
6457 if (!done)
6459 parser->scope = NULL_TREE;
6460 parser->object_scope = NULL_TREE;
6461 parser->qualifying_scope = NULL_TREE;
6462 if (processing_template_decl)
6463 cp_parser_parse_tentatively (parser);
6464 type_decl
6465 = cp_parser_class_name (parser,
6466 /*typename_keyword_p=*/false,
6467 /*template_keyword_p=*/false,
6468 typename_type,
6469 /*check_dependency=*/false,
6470 /*class_head_p=*/false,
6471 declarator_p);
6472 if (processing_template_decl
6473 && ! cp_parser_parse_definitely (parser))
6475 /* We couldn't find a type with this name. If we're parsing
6476 tentatively, fail and try something else. */
6477 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6479 cp_parser_simulate_error (parser);
6480 return error_mark_node;
6482 /* Otherwise, accept it and check for a match at instantiation
6483 time. */
6484 type_decl = cp_parser_identifier (parser);
6485 if (type_decl != error_mark_node)
6486 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6487 return type_decl;
6490 /* If an error occurred, assume that the name of the
6491 destructor is the same as the name of the qualifying
6492 class. That allows us to keep parsing after running
6493 into ill-formed destructor names. */
6494 if (type_decl == error_mark_node && scope)
6495 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6496 else if (type_decl == error_mark_node)
6497 return error_mark_node;
6499 /* Check that destructor name and scope match. */
6500 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6502 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6503 error_at (loc,
6504 "declaration of %<~%T%> as member of %qT",
6505 type_decl, scope);
6506 cp_parser_simulate_error (parser);
6507 return error_mark_node;
6510 /* [class.dtor]
6512 A typedef-name that names a class shall not be used as the
6513 identifier in the declarator for a destructor declaration. */
6514 if (declarator_p
6515 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6516 && !DECL_SELF_REFERENCE_P (type_decl)
6517 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6518 error_at (loc,
6519 "typedef-name %qD used as destructor declarator",
6520 type_decl);
6522 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6525 case CPP_KEYWORD:
6526 if (token->keyword == RID_OPERATOR)
6528 cp_expr id;
6530 /* This could be a template-id, so we try that first. */
6531 cp_parser_parse_tentatively (parser);
6532 /* Try a template-id. */
6533 id = cp_parser_template_id_expr (parser, template_keyword_p,
6534 /*check_dependency_p=*/true,
6535 declarator_p);
6536 /* If that worked, we're done. */
6537 if (cp_parser_parse_definitely (parser))
6538 return id;
6539 /* We still don't know whether we're looking at an
6540 operator-function-id or a conversion-function-id. */
6541 cp_parser_parse_tentatively (parser);
6542 /* Try an operator-function-id. */
6543 id = cp_parser_operator_function_id (parser);
6544 /* If that didn't work, try a conversion-function-id. */
6545 if (!cp_parser_parse_definitely (parser))
6546 id = cp_parser_conversion_function_id (parser);
6548 return id;
6550 /* Fall through. */
6552 default:
6553 if (optional_p)
6554 return NULL_TREE;
6555 cp_parser_error (parser, "expected unqualified-id");
6556 return error_mark_node;
6560 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6561 be a template-id or the name shall refer to a class template or an
6562 alias template. */
6564 static void
6565 check_template_keyword_in_nested_name_spec (tree name)
6567 if (CLASS_TYPE_P (name)
6568 && ((CLASSTYPE_USE_TEMPLATE (name)
6569 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6570 || CLASSTYPE_IS_TEMPLATE (name)))
6571 return;
6573 if (TREE_CODE (name) == TYPENAME_TYPE
6574 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6575 return;
6576 /* Alias templates are also OK. */
6577 else if (alias_template_specialization_p (name, nt_opaque))
6578 return;
6580 permerror (input_location, TYPE_P (name)
6581 ? G_("%qT is not a template")
6582 : G_("%qD is not a template"),
6583 name);
6586 /* Parse an (optional) nested-name-specifier.
6588 nested-name-specifier: [C++98]
6589 class-or-namespace-name :: nested-name-specifier [opt]
6590 class-or-namespace-name :: template nested-name-specifier [opt]
6592 nested-name-specifier: [C++0x]
6593 type-name ::
6594 namespace-name ::
6595 nested-name-specifier identifier ::
6596 nested-name-specifier template [opt] simple-template-id ::
6598 PARSER->SCOPE should be set appropriately before this function is
6599 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6600 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6601 in name lookups.
6603 Sets PARSER->SCOPE to the class (TYPE) or namespace
6604 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6605 it unchanged if there is no nested-name-specifier. Returns the new
6606 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6608 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6610 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6611 part of a declaration and/or decl-specifier. */
6613 static tree
6614 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6615 bool typename_keyword_p,
6616 bool check_dependency_p,
6617 bool type_p,
6618 bool is_declaration,
6619 bool template_keyword_p /* = false */)
6621 bool success = false;
6622 cp_token_position start = 0;
6623 cp_token *token;
6625 /* Remember where the nested-name-specifier starts. */
6626 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6627 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6629 start = cp_lexer_token_position (parser->lexer, false);
6630 push_deferring_access_checks (dk_deferred);
6633 while (true)
6635 tree new_scope;
6636 tree old_scope;
6637 tree saved_qualifying_scope;
6639 /* Spot cases that cannot be the beginning of a
6640 nested-name-specifier. */
6641 token = cp_lexer_peek_token (parser->lexer);
6643 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6644 the already parsed nested-name-specifier. */
6645 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6647 /* Grab the nested-name-specifier and continue the loop. */
6648 cp_parser_pre_parsed_nested_name_specifier (parser);
6649 /* If we originally encountered this nested-name-specifier
6650 with CHECK_DEPENDENCY_P set to true, we will not have
6651 resolved TYPENAME_TYPEs, so we must do so here. */
6652 if (is_declaration
6653 && !check_dependency_p
6654 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6656 new_scope = resolve_typename_type (parser->scope,
6657 /*only_current_p=*/false);
6658 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6659 parser->scope = new_scope;
6661 success = true;
6662 continue;
6665 /* Spot cases that cannot be the beginning of a
6666 nested-name-specifier. On the second and subsequent times
6667 through the loop, we look for the `template' keyword. */
6668 if (success && token->keyword == RID_TEMPLATE)
6670 /* A template-id can start a nested-name-specifier. */
6671 else if (token->type == CPP_TEMPLATE_ID)
6673 /* DR 743: decltype can be used in a nested-name-specifier. */
6674 else if (token_is_decltype (token))
6676 else
6678 /* If the next token is not an identifier, then it is
6679 definitely not a type-name or namespace-name. */
6680 if (token->type != CPP_NAME)
6681 break;
6682 /* If the following token is neither a `<' (to begin a
6683 template-id), nor a `::', then we are not looking at a
6684 nested-name-specifier. */
6685 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6687 if (token->type == CPP_COLON
6688 && parser->colon_corrects_to_scope_p
6689 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6690 /* name:name is a valid sequence in an Objective C message. */
6691 && !parser->objective_c_message_context_p)
6693 gcc_rich_location richloc (token->location);
6694 richloc.add_fixit_replace ("::");
6695 error_at (&richloc,
6696 "found %<:%> in nested-name-specifier, "
6697 "expected %<::%>");
6698 token->type = CPP_SCOPE;
6701 if (token->type != CPP_SCOPE
6702 && !cp_parser_nth_token_starts_template_argument_list_p
6703 (parser, 2))
6704 break;
6707 /* The nested-name-specifier is optional, so we parse
6708 tentatively. */
6709 cp_parser_parse_tentatively (parser);
6711 /* Look for the optional `template' keyword, if this isn't the
6712 first time through the loop. */
6713 if (success)
6715 template_keyword_p = cp_parser_optional_template_keyword (parser);
6716 /* DR1710: "In a qualified-id used as the name in
6717 a typename-specifier, elaborated-type-specifier, using-declaration,
6718 or class-or-decltype, an optional keyword template appearing at
6719 the top level is ignored." */
6720 if (!template_keyword_p
6721 && typename_keyword_p
6722 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6723 template_keyword_p = true;
6726 /* Save the old scope since the name lookup we are about to do
6727 might destroy it. */
6728 old_scope = parser->scope;
6729 saved_qualifying_scope = parser->qualifying_scope;
6730 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6731 look up names in "X<T>::I" in order to determine that "Y" is
6732 a template. So, if we have a typename at this point, we make
6733 an effort to look through it. */
6734 if (is_declaration
6735 && !check_dependency_p
6736 && !typename_keyword_p
6737 && parser->scope
6738 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6739 parser->scope = resolve_typename_type (parser->scope,
6740 /*only_current_p=*/false);
6741 /* Parse the qualifying entity. */
6742 new_scope
6743 = cp_parser_qualifying_entity (parser,
6744 typename_keyword_p,
6745 template_keyword_p,
6746 check_dependency_p,
6747 type_p,
6748 is_declaration);
6749 /* Look for the `::' token. */
6750 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6752 /* If we found what we wanted, we keep going; otherwise, we're
6753 done. */
6754 if (!cp_parser_parse_definitely (parser))
6756 bool error_p = false;
6758 /* Restore the OLD_SCOPE since it was valid before the
6759 failed attempt at finding the last
6760 class-or-namespace-name. */
6761 parser->scope = old_scope;
6762 parser->qualifying_scope = saved_qualifying_scope;
6764 /* If the next token is a decltype, and the one after that is a
6765 `::', then the decltype has failed to resolve to a class or
6766 enumeration type. Give this error even when parsing
6767 tentatively since it can't possibly be valid--and we're going
6768 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6769 won't get another chance.*/
6770 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6771 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6772 == CPP_SCOPE))
6774 token = cp_lexer_consume_token (parser->lexer);
6775 tree dtype = token->u.tree_check_value->value;
6776 if (dtype != error_mark_node)
6777 error_at (token->location, "%<decltype%> evaluates to %qT, "
6778 "which is not a class or enumeration type",
6779 dtype);
6780 parser->scope = error_mark_node;
6781 error_p = true;
6782 /* As below. */
6783 success = true;
6784 cp_lexer_consume_token (parser->lexer);
6787 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6788 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6790 /* If we have a non-type template-id followed by ::, it can't
6791 possibly be valid. */
6792 token = cp_lexer_peek_token (parser->lexer);
6793 tree tid = token->u.tree_check_value->value;
6794 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6795 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6797 tree tmpl = NULL_TREE;
6798 if (is_overloaded_fn (tid))
6800 tree fns = get_fns (tid);
6801 if (OVL_SINGLE_P (fns))
6802 tmpl = OVL_FIRST (fns);
6803 if (function_concept_p (fns))
6804 error_at (token->location, "concept-id %qD "
6805 "in nested-name-specifier", tid);
6806 else
6807 error_at (token->location, "function template-id "
6808 "%qD in nested-name-specifier", tid);
6810 else
6812 tmpl = TREE_OPERAND (tid, 0);
6813 if (variable_concept_p (tmpl)
6814 || standard_concept_p (tmpl))
6815 error_at (token->location, "concept-id %qD "
6816 "in nested-name-specifier", tid);
6817 else
6819 /* Variable template. */
6820 gcc_assert (variable_template_p (tmpl));
6821 error_at (token->location, "variable template-id "
6822 "%qD in nested-name-specifier", tid);
6825 if (tmpl)
6826 inform (DECL_SOURCE_LOCATION (tmpl),
6827 "%qD declared here", tmpl);
6829 parser->scope = error_mark_node;
6830 error_p = true;
6831 /* As below. */
6832 success = true;
6833 cp_lexer_consume_token (parser->lexer);
6834 cp_lexer_consume_token (parser->lexer);
6838 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6839 break;
6840 /* If the next token is an identifier, and the one after
6841 that is a `::', then any valid interpretation would have
6842 found a class-or-namespace-name. */
6843 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6844 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6845 == CPP_SCOPE)
6846 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6847 != CPP_COMPL))
6849 token = cp_lexer_consume_token (parser->lexer);
6850 if (!error_p)
6852 if (!token->error_reported)
6854 tree decl;
6855 tree ambiguous_decls;
6857 decl = cp_parser_lookup_name (parser, token->u.value,
6858 none_type,
6859 /*is_template=*/false,
6860 /*is_namespace=*/false,
6861 /*check_dependency=*/true,
6862 &ambiguous_decls,
6863 token->location);
6864 if (TREE_CODE (decl) == TEMPLATE_DECL)
6865 error_at (token->location,
6866 "%qD used without template arguments",
6867 decl);
6868 else if (ambiguous_decls)
6870 // cp_parser_lookup_name has the same diagnostic,
6871 // thus make sure to emit it at most once.
6872 if (cp_parser_uncommitted_to_tentative_parse_p
6873 (parser))
6875 error_at (token->location,
6876 "reference to %qD is ambiguous",
6877 token->u.value);
6878 print_candidates (ambiguous_decls);
6880 decl = error_mark_node;
6882 else
6884 if (cxx_dialect != cxx98)
6885 cp_parser_name_lookup_error
6886 (parser, token->u.value, decl, NLE_NOT_CXX98,
6887 token->location);
6888 else
6889 cp_parser_name_lookup_error
6890 (parser, token->u.value, decl, NLE_CXX98,
6891 token->location);
6894 parser->scope = error_mark_node;
6895 error_p = true;
6896 /* Treat this as a successful nested-name-specifier
6897 due to:
6899 [basic.lookup.qual]
6901 If the name found is not a class-name (clause
6902 _class_) or namespace-name (_namespace.def_), the
6903 program is ill-formed. */
6904 success = true;
6906 cp_lexer_consume_token (parser->lexer);
6908 break;
6910 /* We've found one valid nested-name-specifier. */
6911 success = true;
6912 /* Name lookup always gives us a DECL. */
6913 if (TREE_CODE (new_scope) == TYPE_DECL)
6914 new_scope = TREE_TYPE (new_scope);
6915 /* Uses of "template" must be followed by actual templates. */
6916 if (template_keyword_p)
6917 check_template_keyword_in_nested_name_spec (new_scope);
6918 /* If it is a class scope, try to complete it; we are about to
6919 be looking up names inside the class. */
6920 if (TYPE_P (new_scope)
6921 /* Since checking types for dependency can be expensive,
6922 avoid doing it if the type is already complete. */
6923 && !COMPLETE_TYPE_P (new_scope)
6924 /* Do not try to complete dependent types. */
6925 && !dependent_type_p (new_scope))
6927 new_scope = complete_type (new_scope);
6928 /* If it is a typedef to current class, use the current
6929 class instead, as the typedef won't have any names inside
6930 it yet. */
6931 if (!COMPLETE_TYPE_P (new_scope)
6932 && currently_open_class (new_scope))
6933 new_scope = TYPE_MAIN_VARIANT (new_scope);
6935 /* Make sure we look in the right scope the next time through
6936 the loop. */
6937 parser->scope = new_scope;
6940 /* If parsing tentatively, replace the sequence of tokens that makes
6941 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6942 token. That way, should we re-parse the token stream, we will
6943 not have to repeat the effort required to do the parse, nor will
6944 we issue duplicate error messages. */
6945 if (success && start)
6947 cp_token *token;
6949 token = cp_lexer_token_at (parser->lexer, start);
6950 /* Reset the contents of the START token. */
6951 token->type = CPP_NESTED_NAME_SPECIFIER;
6952 /* Retrieve any deferred checks. Do not pop this access checks yet
6953 so the memory will not be reclaimed during token replacing below. */
6954 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6955 token->tree_check_p = true;
6956 token->u.tree_check_value->value = parser->scope;
6957 token->u.tree_check_value->checks = get_deferred_access_checks ();
6958 token->u.tree_check_value->qualifying_scope =
6959 parser->qualifying_scope;
6960 token->keyword = RID_MAX;
6962 /* Purge all subsequent tokens. */
6963 cp_lexer_purge_tokens_after (parser->lexer, start);
6966 if (start)
6967 pop_to_parent_deferring_access_checks ();
6969 return success ? parser->scope : NULL_TREE;
6972 /* Parse a nested-name-specifier. See
6973 cp_parser_nested_name_specifier_opt for details. This function
6974 behaves identically, except that it will an issue an error if no
6975 nested-name-specifier is present. */
6977 static tree
6978 cp_parser_nested_name_specifier (cp_parser *parser,
6979 bool typename_keyword_p,
6980 bool check_dependency_p,
6981 bool type_p,
6982 bool is_declaration)
6984 tree scope;
6986 /* Look for the nested-name-specifier. */
6987 scope = cp_parser_nested_name_specifier_opt (parser,
6988 typename_keyword_p,
6989 check_dependency_p,
6990 type_p,
6991 is_declaration);
6992 /* If it was not present, issue an error message. */
6993 if (!scope)
6995 cp_parser_error (parser, "expected nested-name-specifier");
6996 parser->scope = NULL_TREE;
6999 return scope;
7002 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7003 this is either a class-name or a namespace-name (which corresponds
7004 to the class-or-namespace-name production in the grammar). For
7005 C++0x, it can also be a type-name that refers to an enumeration
7006 type or a simple-template-id.
7008 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7009 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7010 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7011 TYPE_P is TRUE iff the next name should be taken as a class-name,
7012 even the same name is declared to be another entity in the same
7013 scope.
7015 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7016 specified by the class-or-namespace-name. If neither is found the
7017 ERROR_MARK_NODE is returned. */
7019 static tree
7020 cp_parser_qualifying_entity (cp_parser *parser,
7021 bool typename_keyword_p,
7022 bool template_keyword_p,
7023 bool check_dependency_p,
7024 bool type_p,
7025 bool is_declaration)
7027 tree saved_scope;
7028 tree saved_qualifying_scope;
7029 tree saved_object_scope;
7030 tree scope;
7031 bool only_class_p;
7032 bool successful_parse_p;
7034 /* DR 743: decltype can appear in a nested-name-specifier. */
7035 if (cp_lexer_next_token_is_decltype (parser->lexer))
7037 scope = cp_parser_decltype (parser);
7038 if (TREE_CODE (scope) != ENUMERAL_TYPE
7039 && !MAYBE_CLASS_TYPE_P (scope))
7041 cp_parser_simulate_error (parser);
7042 return error_mark_node;
7044 if (TYPE_NAME (scope))
7045 scope = TYPE_NAME (scope);
7046 return scope;
7049 /* Before we try to parse the class-name, we must save away the
7050 current PARSER->SCOPE since cp_parser_class_name will destroy
7051 it. */
7052 saved_scope = parser->scope;
7053 saved_qualifying_scope = parser->qualifying_scope;
7054 saved_object_scope = parser->object_scope;
7055 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7056 there is no need to look for a namespace-name. */
7057 only_class_p = template_keyword_p
7058 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7059 if (!only_class_p)
7060 cp_parser_parse_tentatively (parser);
7061 scope = cp_parser_class_name (parser,
7062 typename_keyword_p,
7063 template_keyword_p,
7064 type_p ? class_type : none_type,
7065 check_dependency_p,
7066 /*class_head_p=*/false,
7067 is_declaration,
7068 /*enum_ok=*/cxx_dialect > cxx98);
7069 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7070 /* If that didn't work, try for a namespace-name. */
7071 if (!only_class_p && !successful_parse_p)
7073 /* Restore the saved scope. */
7074 parser->scope = saved_scope;
7075 parser->qualifying_scope = saved_qualifying_scope;
7076 parser->object_scope = saved_object_scope;
7077 /* If we are not looking at an identifier followed by the scope
7078 resolution operator, then this is not part of a
7079 nested-name-specifier. (Note that this function is only used
7080 to parse the components of a nested-name-specifier.) */
7081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7082 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7083 return error_mark_node;
7084 scope = cp_parser_namespace_name (parser);
7087 return scope;
7090 /* Return true if we are looking at a compound-literal, false otherwise. */
7092 static bool
7093 cp_parser_compound_literal_p (cp_parser *parser)
7095 cp_lexer_save_tokens (parser->lexer);
7097 /* Skip tokens until the next token is a closing parenthesis.
7098 If we find the closing `)', and the next token is a `{', then
7099 we are looking at a compound-literal. */
7100 bool compound_literal_p
7101 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7102 /*consume_paren=*/true)
7103 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7105 /* Roll back the tokens we skipped. */
7106 cp_lexer_rollback_tokens (parser->lexer);
7108 return compound_literal_p;
7111 /* Return true if EXPR is the integer constant zero or a complex constant
7112 of zero, without any folding, but ignoring location wrappers. */
7114 bool
7115 literal_integer_zerop (const_tree expr)
7117 return (location_wrapper_p (expr)
7118 && integer_zerop (TREE_OPERAND (expr, 0)));
7121 /* Parse a postfix-expression.
7123 postfix-expression:
7124 primary-expression
7125 postfix-expression [ expression ]
7126 postfix-expression ( expression-list [opt] )
7127 simple-type-specifier ( expression-list [opt] )
7128 typename :: [opt] nested-name-specifier identifier
7129 ( expression-list [opt] )
7130 typename :: [opt] nested-name-specifier template [opt] template-id
7131 ( expression-list [opt] )
7132 postfix-expression . template [opt] id-expression
7133 postfix-expression -> template [opt] id-expression
7134 postfix-expression . pseudo-destructor-name
7135 postfix-expression -> pseudo-destructor-name
7136 postfix-expression ++
7137 postfix-expression --
7138 dynamic_cast < type-id > ( expression )
7139 static_cast < type-id > ( expression )
7140 reinterpret_cast < type-id > ( expression )
7141 const_cast < type-id > ( expression )
7142 typeid ( expression )
7143 typeid ( type-id )
7145 GNU Extension:
7147 postfix-expression:
7148 ( type-id ) { initializer-list , [opt] }
7150 This extension is a GNU version of the C99 compound-literal
7151 construct. (The C99 grammar uses `type-name' instead of `type-id',
7152 but they are essentially the same concept.)
7154 If ADDRESS_P is true, the postfix expression is the operand of the
7155 `&' operator. CAST_P is true if this expression is the target of a
7156 cast.
7158 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7159 class member access expressions [expr.ref].
7161 Returns a representation of the expression. */
7163 static cp_expr
7164 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7165 bool member_access_only_p, bool decltype_p,
7166 cp_id_kind * pidk_return)
7168 cp_token *token;
7169 location_t loc;
7170 enum rid keyword;
7171 cp_id_kind idk = CP_ID_KIND_NONE;
7172 cp_expr postfix_expression = NULL_TREE;
7173 bool is_member_access = false;
7175 /* Peek at the next token. */
7176 token = cp_lexer_peek_token (parser->lexer);
7177 loc = token->location;
7178 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7180 /* Some of the productions are determined by keywords. */
7181 keyword = token->keyword;
7182 switch (keyword)
7184 case RID_DYNCAST:
7185 case RID_STATCAST:
7186 case RID_REINTCAST:
7187 case RID_CONSTCAST:
7189 tree type;
7190 cp_expr expression;
7191 const char *saved_message;
7192 bool saved_in_type_id_in_expr_p;
7194 /* All of these can be handled in the same way from the point
7195 of view of parsing. Begin by consuming the token
7196 identifying the cast. */
7197 cp_lexer_consume_token (parser->lexer);
7199 /* New types cannot be defined in the cast. */
7200 saved_message = parser->type_definition_forbidden_message;
7201 parser->type_definition_forbidden_message
7202 = G_("types may not be defined in casts");
7204 /* Look for the opening `<'. */
7205 cp_parser_require (parser, CPP_LESS, RT_LESS);
7206 /* Parse the type to which we are casting. */
7207 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7208 parser->in_type_id_in_expr_p = true;
7209 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7210 NULL);
7211 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7212 /* Look for the closing `>'. */
7213 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7214 /* Restore the old message. */
7215 parser->type_definition_forbidden_message = saved_message;
7217 bool saved_greater_than_is_operator_p
7218 = parser->greater_than_is_operator_p;
7219 parser->greater_than_is_operator_p = true;
7221 /* And the expression which is being cast. */
7222 matching_parens parens;
7223 parens.require_open (parser);
7224 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7225 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7226 RT_CLOSE_PAREN);
7227 location_t end_loc = close_paren ?
7228 close_paren->location : UNKNOWN_LOCATION;
7230 parser->greater_than_is_operator_p
7231 = saved_greater_than_is_operator_p;
7233 /* Only type conversions to integral or enumeration types
7234 can be used in constant-expressions. */
7235 if (!cast_valid_in_integral_constant_expression_p (type)
7236 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7238 postfix_expression = error_mark_node;
7239 break;
7242 /* Construct a location e.g. :
7243 reinterpret_cast <int *> (expr)
7244 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7245 ranging from the start of the "*_cast" token to the final closing
7246 paren, with the caret at the start. */
7247 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7249 switch (keyword)
7251 case RID_DYNCAST:
7252 postfix_expression
7253 = build_dynamic_cast (cp_cast_loc, type, expression,
7254 tf_warning_or_error);
7255 break;
7256 case RID_STATCAST:
7257 postfix_expression
7258 = build_static_cast (cp_cast_loc, type, expression,
7259 tf_warning_or_error);
7260 break;
7261 case RID_REINTCAST:
7262 postfix_expression
7263 = build_reinterpret_cast (cp_cast_loc, type, expression,
7264 tf_warning_or_error);
7265 break;
7266 case RID_CONSTCAST:
7267 postfix_expression
7268 = build_const_cast (cp_cast_loc, type, expression,
7269 tf_warning_or_error);
7270 break;
7271 default:
7272 gcc_unreachable ();
7275 break;
7277 case RID_TYPEID:
7279 tree type;
7280 const char *saved_message;
7281 bool saved_in_type_id_in_expr_p;
7283 /* Consume the `typeid' token. */
7284 cp_lexer_consume_token (parser->lexer);
7285 /* Look for the `(' token. */
7286 matching_parens parens;
7287 parens.require_open (parser);
7288 /* Types cannot be defined in a `typeid' expression. */
7289 saved_message = parser->type_definition_forbidden_message;
7290 parser->type_definition_forbidden_message
7291 = G_("types may not be defined in a %<typeid%> expression");
7292 /* We can't be sure yet whether we're looking at a type-id or an
7293 expression. */
7294 cp_parser_parse_tentatively (parser);
7295 /* Try a type-id first. */
7296 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7297 parser->in_type_id_in_expr_p = true;
7298 type = cp_parser_type_id (parser);
7299 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7300 /* Look for the `)' token. Otherwise, we can't be sure that
7301 we're not looking at an expression: consider `typeid (int
7302 (3))', for example. */
7303 cp_token *close_paren = parens.require_close (parser);
7304 /* If all went well, simply lookup the type-id. */
7305 if (cp_parser_parse_definitely (parser))
7306 postfix_expression = get_typeid (type, tf_warning_or_error);
7307 /* Otherwise, fall back to the expression variant. */
7308 else
7310 tree expression;
7312 /* Look for an expression. */
7313 expression = cp_parser_expression (parser, & idk);
7314 /* Compute its typeid. */
7315 postfix_expression = build_typeid (expression, tf_warning_or_error);
7316 /* Look for the `)' token. */
7317 close_paren = parens.require_close (parser);
7319 /* Restore the saved message. */
7320 parser->type_definition_forbidden_message = saved_message;
7321 /* `typeid' may not appear in an integral constant expression. */
7322 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7323 postfix_expression = error_mark_node;
7325 /* Construct a location e.g. :
7326 typeid (expr)
7327 ^~~~~~~~~~~~~
7328 ranging from the start of the "typeid" token to the final closing
7329 paren, with the caret at the start. */
7330 if (close_paren)
7332 location_t typeid_loc
7333 = make_location (start_loc, start_loc, close_paren->location);
7334 postfix_expression.set_location (typeid_loc);
7335 postfix_expression.maybe_add_location_wrapper ();
7338 break;
7340 case RID_TYPENAME:
7342 tree type;
7343 /* The syntax permitted here is the same permitted for an
7344 elaborated-type-specifier. */
7345 ++parser->prevent_constrained_type_specifiers;
7346 type = cp_parser_elaborated_type_specifier (parser,
7347 /*is_friend=*/false,
7348 /*is_declaration=*/false);
7349 --parser->prevent_constrained_type_specifiers;
7350 postfix_expression = cp_parser_functional_cast (parser, type);
7352 break;
7354 case RID_ADDRESSOF:
7355 case RID_BUILTIN_SHUFFLE:
7356 case RID_BUILTIN_SHUFFLEVECTOR:
7357 case RID_BUILTIN_LAUNDER:
7358 case RID_BUILTIN_ASSOC_BARRIER:
7360 vec<tree, va_gc> *vec;
7362 cp_lexer_consume_token (parser->lexer);
7363 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7364 /*cast_p=*/false, /*allow_expansion_p=*/true,
7365 /*non_constant_p=*/NULL);
7366 if (vec == NULL)
7368 postfix_expression = error_mark_node;
7369 break;
7372 for (tree p : *vec)
7373 mark_exp_read (p);
7375 switch (keyword)
7377 case RID_ADDRESSOF:
7378 if (vec->length () == 1)
7379 postfix_expression
7380 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7381 else
7383 error_at (loc, "wrong number of arguments to "
7384 "%<__builtin_addressof%>");
7385 postfix_expression = error_mark_node;
7387 break;
7389 case RID_BUILTIN_LAUNDER:
7390 if (vec->length () == 1)
7391 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7392 tf_warning_or_error);
7393 else
7395 error_at (loc, "wrong number of arguments to "
7396 "%<__builtin_launder%>");
7397 postfix_expression = error_mark_node;
7399 break;
7401 case RID_BUILTIN_ASSOC_BARRIER:
7402 if (vec->length () == 1)
7403 postfix_expression = build1_loc (loc, PAREN_EXPR,
7404 TREE_TYPE ((*vec)[0]),
7405 (*vec)[0]);
7406 else
7408 error_at (loc, "wrong number of arguments to "
7409 "%<__builtin_assoc_barrier%>");
7410 postfix_expression = error_mark_node;
7412 break;
7414 case RID_BUILTIN_SHUFFLE:
7415 if (vec->length () == 2)
7416 postfix_expression
7417 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7418 (*vec)[1], tf_warning_or_error);
7419 else if (vec->length () == 3)
7420 postfix_expression
7421 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7422 (*vec)[2], tf_warning_or_error);
7423 else
7425 error_at (loc, "wrong number of arguments to "
7426 "%<__builtin_shuffle%>");
7427 postfix_expression = error_mark_node;
7429 break;
7431 case RID_BUILTIN_SHUFFLEVECTOR:
7432 if (vec->length () < 3)
7434 error_at (loc, "wrong number of arguments to "
7435 "%<__builtin_shufflevector%>");
7436 postfix_expression = error_mark_node;
7438 else
7440 postfix_expression
7441 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7443 break;
7445 default:
7446 gcc_unreachable ();
7448 break;
7451 case RID_BUILTIN_CONVERTVECTOR:
7453 tree expression;
7454 tree type;
7455 /* Consume the `__builtin_convertvector' token. */
7456 cp_lexer_consume_token (parser->lexer);
7457 /* Look for the opening `('. */
7458 matching_parens parens;
7459 parens.require_open (parser);
7460 /* Now, parse the assignment-expression. */
7461 expression = cp_parser_assignment_expression (parser);
7462 /* Look for the `,'. */
7463 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7464 location_t type_location
7465 = cp_lexer_peek_token (parser->lexer)->location;
7466 /* Parse the type-id. */
7468 type_id_in_expr_sentinel s (parser);
7469 type = cp_parser_type_id (parser);
7471 /* Look for the closing `)'. */
7472 parens.require_close (parser);
7473 return cp_build_vec_convert (expression, type_location, type,
7474 tf_warning_or_error);
7477 case RID_BUILTIN_BIT_CAST:
7479 tree expression;
7480 tree type;
7481 /* Consume the `__builtin_bit_cast' token. */
7482 cp_lexer_consume_token (parser->lexer);
7483 /* Look for the opening `('. */
7484 matching_parens parens;
7485 parens.require_open (parser);
7486 location_t type_location
7487 = cp_lexer_peek_token (parser->lexer)->location;
7488 /* Parse the type-id. */
7490 type_id_in_expr_sentinel s (parser);
7491 type = cp_parser_type_id (parser);
7493 /* Look for the `,'. */
7494 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7495 /* Now, parse the assignment-expression. */
7496 expression = cp_parser_assignment_expression (parser);
7497 /* Look for the closing `)'. */
7498 parens.require_close (parser);
7499 return cp_build_bit_cast (type_location, type, expression,
7500 tf_warning_or_error);
7503 default:
7505 tree type;
7507 /* If the next thing is a simple-type-specifier, we may be
7508 looking at a functional cast. We could also be looking at
7509 an id-expression. So, we try the functional cast, and if
7510 that doesn't work we fall back to the primary-expression. */
7511 cp_parser_parse_tentatively (parser);
7512 /* Look for the simple-type-specifier. */
7513 ++parser->prevent_constrained_type_specifiers;
7514 type = cp_parser_simple_type_specifier (parser,
7515 /*decl_specs=*/NULL,
7516 CP_PARSER_FLAGS_NONE);
7517 --parser->prevent_constrained_type_specifiers;
7518 /* Parse the cast itself. */
7519 if (!cp_parser_error_occurred (parser))
7520 postfix_expression
7521 = cp_parser_functional_cast (parser, type);
7522 /* If that worked, we're done. */
7523 if (cp_parser_parse_definitely (parser))
7524 break;
7526 /* If the functional-cast didn't work out, try a
7527 compound-literal. */
7528 if (cp_parser_allow_gnu_extensions_p (parser)
7529 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7531 cp_expr initializer = NULL_TREE;
7533 cp_parser_parse_tentatively (parser);
7535 matching_parens parens;
7536 parens.consume_open (parser);
7538 /* Avoid calling cp_parser_type_id pointlessly, see comment
7539 in cp_parser_cast_expression about c++/29234. */
7540 if (!cp_parser_compound_literal_p (parser))
7541 cp_parser_simulate_error (parser);
7542 else
7544 /* Parse the type. */
7545 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7546 parser->in_type_id_in_expr_p = true;
7547 type = cp_parser_type_id (parser);
7548 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7549 parens.require_close (parser);
7552 /* If things aren't going well, there's no need to
7553 keep going. */
7554 if (!cp_parser_error_occurred (parser))
7556 bool non_constant_p;
7557 /* Parse the brace-enclosed initializer list. */
7558 initializer = cp_parser_braced_list (parser,
7559 &non_constant_p);
7561 /* If that worked, we're definitely looking at a
7562 compound-literal expression. */
7563 if (cp_parser_parse_definitely (parser))
7565 /* Warn the user that a compound literal is not
7566 allowed in standard C++. */
7567 pedwarn (input_location, OPT_Wpedantic,
7568 "ISO C++ forbids compound-literals");
7569 /* For simplicity, we disallow compound literals in
7570 constant-expressions. We could
7571 allow compound literals of integer type, whose
7572 initializer was a constant, in constant
7573 expressions. Permitting that usage, as a further
7574 extension, would not change the meaning of any
7575 currently accepted programs. (Of course, as
7576 compound literals are not part of ISO C++, the
7577 standard has nothing to say.) */
7578 if (cp_parser_non_integral_constant_expression (parser,
7579 NIC_NCC))
7581 postfix_expression = error_mark_node;
7582 break;
7584 /* Form the representation of the compound-literal. */
7585 postfix_expression
7586 = finish_compound_literal (type, initializer,
7587 tf_warning_or_error, fcl_c99);
7588 postfix_expression.set_location (initializer.get_location ());
7589 break;
7593 /* It must be a primary-expression. */
7594 postfix_expression
7595 = cp_parser_primary_expression (parser, address_p, cast_p,
7596 /*template_arg_p=*/false,
7597 decltype_p,
7598 &idk);
7600 break;
7603 /* Note that we don't need to worry about calling build_cplus_new on a
7604 class-valued CALL_EXPR in decltype when it isn't the end of the
7605 postfix-expression; unary_complex_lvalue will take care of that for
7606 all these cases. */
7608 /* Keep looping until the postfix-expression is complete. */
7609 while (true)
7611 if (idk == CP_ID_KIND_UNQUALIFIED
7612 && identifier_p (postfix_expression)
7613 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7614 /* It is not a Koenig lookup function call. */
7615 postfix_expression
7616 = unqualified_name_lookup_error (postfix_expression);
7618 /* Peek at the next token. */
7619 token = cp_lexer_peek_token (parser->lexer);
7621 switch (token->type)
7623 case CPP_OPEN_SQUARE:
7624 if (cp_next_tokens_can_be_std_attribute_p (parser))
7626 cp_parser_error (parser,
7627 "two consecutive %<[%> shall "
7628 "only introduce an attribute");
7629 return error_mark_node;
7631 postfix_expression
7632 = cp_parser_postfix_open_square_expression (parser,
7633 postfix_expression,
7634 false,
7635 decltype_p);
7636 postfix_expression.set_range (start_loc,
7637 postfix_expression.get_location ());
7639 idk = CP_ID_KIND_NONE;
7640 is_member_access = false;
7641 break;
7643 case CPP_OPEN_PAREN:
7644 /* postfix-expression ( expression-list [opt] ) */
7646 bool koenig_p;
7647 bool is_builtin_constant_p;
7648 bool saved_integral_constant_expression_p = false;
7649 bool saved_non_integral_constant_expression_p = false;
7650 tsubst_flags_t complain = complain_flags (decltype_p);
7651 vec<tree, va_gc> *args;
7652 location_t close_paren_loc = UNKNOWN_LOCATION;
7653 location_t combined_loc = UNKNOWN_LOCATION;
7655 is_member_access = false;
7657 tree stripped_expression
7658 = tree_strip_any_location_wrapper (postfix_expression);
7659 is_builtin_constant_p
7660 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7661 if (is_builtin_constant_p)
7663 /* The whole point of __builtin_constant_p is to allow
7664 non-constant expressions to appear as arguments. */
7665 saved_integral_constant_expression_p
7666 = parser->integral_constant_expression_p;
7667 saved_non_integral_constant_expression_p
7668 = parser->non_integral_constant_expression_p;
7669 parser->integral_constant_expression_p = false;
7671 args = (cp_parser_parenthesized_expression_list
7672 (parser, non_attr,
7673 /*cast_p=*/false, /*allow_expansion_p=*/true,
7674 /*non_constant_p=*/NULL,
7675 /*close_paren_loc=*/&close_paren_loc,
7676 /*wrap_locations_p=*/true));
7677 if (is_builtin_constant_p)
7679 parser->integral_constant_expression_p
7680 = saved_integral_constant_expression_p;
7681 parser->non_integral_constant_expression_p
7682 = saved_non_integral_constant_expression_p;
7685 if (args == NULL)
7687 postfix_expression = error_mark_node;
7688 break;
7691 /* Function calls are not permitted in
7692 constant-expressions. */
7693 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7694 && cp_parser_non_integral_constant_expression (parser,
7695 NIC_FUNC_CALL))
7697 postfix_expression = error_mark_node;
7698 release_tree_vector (args);
7699 break;
7702 koenig_p = false;
7703 if (idk == CP_ID_KIND_UNQUALIFIED
7704 || idk == CP_ID_KIND_TEMPLATE_ID)
7706 if (identifier_p (postfix_expression)
7707 /* In C++20, we may need to perform ADL for a template
7708 name. */
7709 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7710 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7712 if (!args->is_empty ())
7714 koenig_p = true;
7715 if (!any_type_dependent_arguments_p (args))
7716 postfix_expression
7717 = perform_koenig_lookup (postfix_expression, args,
7718 complain);
7720 else
7721 postfix_expression
7722 = unqualified_fn_lookup_error (postfix_expression);
7724 /* We do not perform argument-dependent lookup if
7725 normal lookup finds a non-function, in accordance
7726 with the expected resolution of DR 218. */
7727 else if (!args->is_empty ()
7728 && is_overloaded_fn (postfix_expression))
7730 /* Do not do argument dependent lookup if regular
7731 lookup finds a member function or a block-scope
7732 function declaration. [basic.lookup.argdep]/3 */
7733 bool do_adl_p = true;
7734 tree fns = get_fns (postfix_expression);
7735 for (lkp_iterator iter (fns); iter; ++iter)
7737 tree fn = STRIP_TEMPLATE (*iter);
7738 if ((TREE_CODE (fn) == USING_DECL
7739 && DECL_DEPENDENT_P (fn))
7740 || DECL_FUNCTION_MEMBER_P (fn)
7741 || DECL_LOCAL_DECL_P (fn))
7743 do_adl_p = false;
7744 break;
7748 if (do_adl_p)
7750 koenig_p = true;
7751 if (!any_type_dependent_arguments_p (args))
7752 postfix_expression
7753 = perform_koenig_lookup (postfix_expression, args,
7754 complain);
7759 /* Temporarily set input_location to the combined location
7760 with call expression range, as e.g. build_out_target_exprs
7761 called from convert_default_arg relies on input_location,
7762 so updating it only when the call is fully built results
7763 in inconsistencies between location handling in templates
7764 and outside of templates. */
7765 if (close_paren_loc != UNKNOWN_LOCATION)
7766 combined_loc = make_location (token->location, start_loc,
7767 close_paren_loc);
7768 iloc_sentinel ils (combined_loc);
7770 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7772 tree instance = TREE_OPERAND (postfix_expression, 0);
7773 tree fn = TREE_OPERAND (postfix_expression, 1);
7775 if (processing_template_decl
7776 && (type_dependent_object_expression_p (instance)
7777 || (!BASELINK_P (fn)
7778 && TREE_CODE (fn) != FIELD_DECL)
7779 || type_dependent_expression_p (fn)
7780 || any_type_dependent_arguments_p (args)))
7782 maybe_generic_this_capture (instance, fn);
7783 postfix_expression
7784 = build_min_nt_call_vec (postfix_expression, args);
7786 else if (BASELINK_P (fn))
7788 postfix_expression
7789 = (build_new_method_call
7790 (instance, fn, &args, NULL_TREE,
7791 (idk == CP_ID_KIND_QUALIFIED
7792 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7793 : LOOKUP_NORMAL),
7794 /*fn_p=*/NULL,
7795 complain));
7797 else
7798 postfix_expression
7799 = finish_call_expr (postfix_expression, &args,
7800 /*disallow_virtual=*/false,
7801 /*koenig_p=*/false,
7802 complain);
7804 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7805 || TREE_CODE (postfix_expression) == MEMBER_REF
7806 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7807 postfix_expression = (build_offset_ref_call_from_tree
7808 (postfix_expression, &args,
7809 complain));
7810 else if (idk == CP_ID_KIND_QUALIFIED)
7811 /* A call to a static class member, or a namespace-scope
7812 function. */
7813 postfix_expression
7814 = finish_call_expr (postfix_expression, &args,
7815 /*disallow_virtual=*/true,
7816 koenig_p,
7817 complain);
7818 else
7819 /* All other function calls. */
7820 postfix_expression
7821 = finish_call_expr (postfix_expression, &args,
7822 /*disallow_virtual=*/false,
7823 koenig_p,
7824 complain);
7826 if (close_paren_loc != UNKNOWN_LOCATION)
7827 postfix_expression.set_location (combined_loc);
7829 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7830 idk = CP_ID_KIND_NONE;
7832 release_tree_vector (args);
7834 break;
7836 case CPP_DOT:
7837 case CPP_DEREF:
7838 /* postfix-expression . template [opt] id-expression
7839 postfix-expression . pseudo-destructor-name
7840 postfix-expression -> template [opt] id-expression
7841 postfix-expression -> pseudo-destructor-name */
7843 /* Consume the `.' or `->' operator. */
7844 cp_lexer_consume_token (parser->lexer);
7846 postfix_expression
7847 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7848 postfix_expression,
7849 false, &idk, loc);
7851 is_member_access = true;
7852 break;
7854 case CPP_PLUS_PLUS:
7855 /* postfix-expression ++ */
7856 /* Consume the `++' token. */
7857 cp_lexer_consume_token (parser->lexer);
7858 /* Generate a representation for the complete expression. */
7859 postfix_expression
7860 = finish_increment_expr (postfix_expression,
7861 POSTINCREMENT_EXPR);
7862 /* Increments may not appear in constant-expressions. */
7863 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7864 postfix_expression = error_mark_node;
7865 idk = CP_ID_KIND_NONE;
7866 is_member_access = false;
7867 break;
7869 case CPP_MINUS_MINUS:
7870 /* postfix-expression -- */
7871 /* Consume the `--' token. */
7872 cp_lexer_consume_token (parser->lexer);
7873 /* Generate a representation for the complete expression. */
7874 postfix_expression
7875 = finish_increment_expr (postfix_expression,
7876 POSTDECREMENT_EXPR);
7877 /* Decrements may not appear in constant-expressions. */
7878 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7879 postfix_expression = error_mark_node;
7880 idk = CP_ID_KIND_NONE;
7881 is_member_access = false;
7882 break;
7884 default:
7885 if (pidk_return != NULL)
7886 * pidk_return = idk;
7887 if (member_access_only_p)
7888 return is_member_access
7889 ? postfix_expression
7890 : cp_expr (error_mark_node);
7891 else
7892 return postfix_expression;
7897 /* Helper function for cp_parser_parenthesized_expression_list and
7898 cp_parser_postfix_open_square_expression. Parse a single element
7899 of parenthesized expression list. */
7901 static cp_expr
7902 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
7903 bool allow_expansion_p,
7904 bool fold_expr_p,
7905 bool *non_constant_p)
7907 cp_expr expr (NULL_TREE);
7908 bool expr_non_constant_p;
7910 /* Parse the next assignment-expression. */
7911 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7913 /* A braced-init-list. */
7914 cp_lexer_set_source_position (parser->lexer);
7915 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7916 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7917 if (non_constant_p && expr_non_constant_p)
7918 *non_constant_p = true;
7920 else if (non_constant_p)
7922 expr = cp_parser_constant_expression (parser,
7923 /*allow_non_constant_p=*/true,
7924 &expr_non_constant_p);
7925 if (expr_non_constant_p)
7926 *non_constant_p = true;
7928 else
7929 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
7931 if (fold_expr_p)
7932 expr = instantiate_non_dependent_expr (expr);
7934 /* If we have an ellipsis, then this is an expression expansion. */
7935 if (allow_expansion_p
7936 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7938 /* Consume the `...'. */
7939 cp_lexer_consume_token (parser->lexer);
7941 /* Build the argument pack. */
7942 expr = make_pack_expansion (expr);
7944 return expr;
7947 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7948 by cp_parser_builtin_offsetof. We're looking for
7950 postfix-expression [ expression ]
7951 postfix-expression [ braced-init-list ] (C++11)
7952 postfix-expression [ expression-list[opt] ] (C++23)
7954 FOR_OFFSETOF is set if we're being called in that context, which
7955 changes how we deal with integer constant expressions. */
7957 static tree
7958 cp_parser_postfix_open_square_expression (cp_parser *parser,
7959 tree postfix_expression,
7960 bool for_offsetof,
7961 bool decltype_p)
7963 tree index = NULL_TREE;
7964 releasing_vec expression_list = NULL;
7965 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7966 bool saved_greater_than_is_operator_p;
7968 /* Consume the `[' token. */
7969 cp_lexer_consume_token (parser->lexer);
7971 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7972 parser->greater_than_is_operator_p = true;
7974 /* Parse the index expression. */
7975 /* ??? For offsetof, there is a question of what to allow here. If
7976 offsetof is not being used in an integral constant expression context,
7977 then we *could* get the right answer by computing the value at runtime.
7978 If we are in an integral constant expression context, then we might
7979 could accept any constant expression; hard to say without analysis.
7980 Rather than open the barn door too wide right away, allow only integer
7981 constant expressions here. */
7982 if (for_offsetof)
7983 index = cp_parser_constant_expression (parser);
7984 else
7986 if (cxx_dialect >= cxx23
7987 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
7988 *&expression_list = make_tree_vector ();
7989 else if (cxx_dialect >= cxx23)
7991 while (true)
7993 cp_expr expr
7994 = cp_parser_parenthesized_expression_list_elt (parser,
7995 /*cast_p=*/
7996 false,
7997 /*allow_exp_p=*/
7998 true,
7999 /*fold_expr_p=*/
8000 false,
8001 /*non_cst_p=*/
8002 NULL);
8004 if (expr == error_mark_node)
8005 index = error_mark_node;
8006 else if (expression_list.get () == NULL
8007 && !PACK_EXPANSION_P (expr.get_value ()))
8008 index = expr.get_value ();
8009 else
8010 vec_safe_push (expression_list, expr.get_value ());
8012 /* If the next token isn't a `,', then we are done. */
8013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8014 break;
8016 if (expression_list.get () == NULL && index != error_mark_node)
8018 *&expression_list = make_tree_vector_single (index);
8019 index = NULL_TREE;
8022 /* Otherwise, consume the `,' and keep going. */
8023 cp_lexer_consume_token (parser->lexer);
8025 if (expression_list.get () && index == error_mark_node)
8026 expression_list.release ();
8028 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8030 bool expr_nonconst_p;
8031 cp_lexer_set_source_position (parser->lexer);
8032 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8033 index = cp_parser_braced_list (parser, &expr_nonconst_p);
8035 else
8036 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8037 /*decltype_p=*/false,
8038 /*warn_comma_p=*/warn_comma_subscript);
8041 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8043 /* Look for the closing `]'. */
8044 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8046 /* Build the ARRAY_REF. */
8047 postfix_expression = grok_array_decl (loc, postfix_expression,
8048 index, &expression_list,
8049 tf_warning_or_error
8050 | (decltype_p ? tf_decltype : 0));
8052 /* When not doing offsetof, array references are not permitted in
8053 constant-expressions. */
8054 if (!for_offsetof
8055 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8056 postfix_expression = error_mark_node;
8058 return postfix_expression;
8061 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8062 dereference of incomplete type, returns true if error_mark_node should
8063 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8064 and *DEPENDENT_P. */
8066 bool
8067 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8068 bool *dependent_p)
8070 /* In a template, be permissive by treating an object expression
8071 of incomplete type as dependent (after a pedwarn). */
8072 diagnostic_t kind = (processing_template_decl
8073 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8075 switch (TREE_CODE (*postfix_expression))
8077 case CAST_EXPR:
8078 case REINTERPRET_CAST_EXPR:
8079 case CONST_CAST_EXPR:
8080 case STATIC_CAST_EXPR:
8081 case DYNAMIC_CAST_EXPR:
8082 case IMPLICIT_CONV_EXPR:
8083 case VIEW_CONVERT_EXPR:
8084 case NON_LVALUE_EXPR:
8085 kind = DK_ERROR;
8086 break;
8087 case OVERLOAD:
8088 /* Don't emit any diagnostic for OVERLOADs. */
8089 kind = DK_IGNORED;
8090 break;
8091 default:
8092 /* Avoid clobbering e.g. DECLs. */
8093 if (!EXPR_P (*postfix_expression))
8094 kind = DK_ERROR;
8095 break;
8098 if (kind == DK_IGNORED)
8099 return false;
8101 location_t exploc = location_of (*postfix_expression);
8102 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8103 if (!MAYBE_CLASS_TYPE_P (*scope))
8104 return true;
8105 if (kind == DK_ERROR)
8106 *scope = *postfix_expression = error_mark_node;
8107 else if (processing_template_decl)
8109 *dependent_p = true;
8110 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8112 return false;
8115 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8116 by cp_parser_builtin_offsetof. We're looking for
8118 postfix-expression . template [opt] id-expression
8119 postfix-expression . pseudo-destructor-name
8120 postfix-expression -> template [opt] id-expression
8121 postfix-expression -> pseudo-destructor-name
8123 FOR_OFFSETOF is set if we're being called in that context. That sorta
8124 limits what of the above we'll actually accept, but nevermind.
8125 TOKEN_TYPE is the "." or "->" token, which will already have been
8126 removed from the stream. */
8128 static tree
8129 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8130 enum cpp_ttype token_type,
8131 cp_expr postfix_expression,
8132 bool for_offsetof, cp_id_kind *idk,
8133 location_t location)
8135 tree name;
8136 bool dependent_p;
8137 bool pseudo_destructor_p;
8138 tree scope = NULL_TREE;
8139 location_t start_loc = postfix_expression.get_start ();
8141 /* If this is a `->' operator, dereference the pointer. */
8142 if (token_type == CPP_DEREF)
8143 postfix_expression = build_x_arrow (location, postfix_expression,
8144 tf_warning_or_error);
8145 /* Check to see whether or not the expression is type-dependent and
8146 not the current instantiation. */
8147 dependent_p = type_dependent_object_expression_p (postfix_expression);
8148 /* The identifier following the `->' or `.' is not qualified. */
8149 parser->scope = NULL_TREE;
8150 parser->qualifying_scope = NULL_TREE;
8151 parser->object_scope = NULL_TREE;
8152 *idk = CP_ID_KIND_NONE;
8154 /* Enter the scope corresponding to the type of the object
8155 given by the POSTFIX_EXPRESSION. */
8156 if (!dependent_p)
8158 scope = TREE_TYPE (postfix_expression);
8159 /* According to the standard, no expression should ever have
8160 reference type. Unfortunately, we do not currently match
8161 the standard in this respect in that our internal representation
8162 of an expression may have reference type even when the standard
8163 says it does not. Therefore, we have to manually obtain the
8164 underlying type here. */
8165 scope = non_reference (scope);
8166 /* The type of the POSTFIX_EXPRESSION must be complete. */
8167 /* Unlike the object expression in other contexts, *this is not
8168 required to be of complete type for purposes of class member
8169 access (5.2.5) outside the member function body. */
8170 if (postfix_expression != current_class_ref
8171 && scope != error_mark_node
8172 && !currently_open_class (scope))
8174 scope = complete_type (scope);
8175 if (!COMPLETE_TYPE_P (scope)
8176 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8177 &dependent_p))
8178 return error_mark_node;
8181 if (!dependent_p)
8183 /* Let the name lookup machinery know that we are processing a
8184 class member access expression. */
8185 parser->context->object_type = scope;
8186 /* If something went wrong, we want to be able to discern that case,
8187 as opposed to the case where there was no SCOPE due to the type
8188 of expression being dependent. */
8189 if (!scope)
8190 scope = error_mark_node;
8191 /* If the SCOPE was erroneous, make the various semantic analysis
8192 functions exit quickly -- and without issuing additional error
8193 messages. */
8194 if (scope == error_mark_node)
8195 postfix_expression = error_mark_node;
8199 if (dependent_p)
8201 tree type = TREE_TYPE (postfix_expression);
8202 /* If we don't have a (type-dependent) object of class type, use
8203 typeof to figure out the type of the object. */
8204 if (type == NULL_TREE)
8205 type = finish_typeof (postfix_expression);
8206 parser->context->object_type = type;
8209 /* Assume this expression is not a pseudo-destructor access. */
8210 pseudo_destructor_p = false;
8212 /* If the SCOPE is a scalar type, then, if this is a valid program,
8213 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8214 is type dependent, it can be pseudo-destructor-name or something else.
8215 Try to parse it as pseudo-destructor-name first. */
8216 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8218 tree s;
8219 tree type;
8221 cp_parser_parse_tentatively (parser);
8222 /* Parse the pseudo-destructor-name. */
8223 s = NULL_TREE;
8224 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8225 &s, &type);
8226 if (dependent_p
8227 && (cp_parser_error_occurred (parser)
8228 || !SCALAR_TYPE_P (type)))
8229 cp_parser_abort_tentative_parse (parser);
8230 else if (cp_parser_parse_definitely (parser))
8232 pseudo_destructor_p = true;
8233 postfix_expression
8234 = finish_pseudo_destructor_expr (postfix_expression,
8235 s, type, location);
8239 if (!pseudo_destructor_p)
8241 /* If the SCOPE is not a scalar type, we are looking at an
8242 ordinary class member access expression, rather than a
8243 pseudo-destructor-name. */
8244 bool template_p;
8245 cp_token *token = cp_lexer_peek_token (parser->lexer);
8246 /* Parse the id-expression. */
8247 name = (cp_parser_id_expression
8248 (parser,
8249 cp_parser_optional_template_keyword (parser),
8250 /*check_dependency_p=*/true,
8251 &template_p,
8252 /*declarator_p=*/false,
8253 /*optional_p=*/false));
8254 /* In general, build a SCOPE_REF if the member name is qualified.
8255 However, if the name was not dependent and has already been
8256 resolved; there is no need to build the SCOPE_REF. For example;
8258 struct X { void f(); };
8259 template <typename T> void f(T* t) { t->X::f(); }
8261 Even though "t" is dependent, "X::f" is not and has been resolved
8262 to a BASELINK; there is no need to include scope information. */
8264 /* But we do need to remember that there was an explicit scope for
8265 virtual function calls. */
8266 if (parser->scope)
8267 *idk = CP_ID_KIND_QUALIFIED;
8269 /* If the name is a template-id that names a type, we will get a
8270 TYPE_DECL here. That is invalid code. */
8271 if (TREE_CODE (name) == TYPE_DECL)
8273 error_at (token->location, "invalid use of %qD", name);
8274 postfix_expression = error_mark_node;
8276 else
8278 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8280 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8282 error_at (token->location, "%<%D::%D%> is not a class member",
8283 parser->scope, name);
8284 postfix_expression = error_mark_node;
8286 else
8287 name = build_qualified_name (/*type=*/NULL_TREE,
8288 parser->scope,
8289 name,
8290 template_p);
8291 parser->scope = NULL_TREE;
8292 parser->qualifying_scope = NULL_TREE;
8293 parser->object_scope = NULL_TREE;
8295 if (parser->scope && name && BASELINK_P (name))
8296 adjust_result_of_qualified_name_lookup
8297 (name, parser->scope, scope);
8298 postfix_expression
8299 = finish_class_member_access_expr (postfix_expression, name,
8300 template_p,
8301 tf_warning_or_error);
8302 /* Build a location e.g.:
8303 ptr->access_expr
8304 ~~~^~~~~~~~~~~~~
8305 where the caret is at the deref token, ranging from
8306 the start of postfix_expression to the end of the access expr. */
8307 location_t combined_loc
8308 = make_location (input_location, start_loc, parser->lexer);
8309 protected_set_expr_location (postfix_expression, combined_loc);
8313 /* We no longer need to look up names in the scope of the object on
8314 the left-hand side of the `.' or `->' operator. */
8315 parser->context->object_type = NULL_TREE;
8317 /* Outside of offsetof, these operators may not appear in
8318 constant-expressions. */
8319 if (!for_offsetof
8320 && (cp_parser_non_integral_constant_expression
8321 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8322 postfix_expression = error_mark_node;
8324 return postfix_expression;
8327 /* Parse a parenthesized expression-list.
8329 expression-list:
8330 assignment-expression
8331 expression-list, assignment-expression
8333 attribute-list:
8334 expression-list
8335 identifier
8336 identifier, expression-list
8338 CAST_P is true if this expression is the target of a cast.
8340 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8341 argument pack.
8343 WRAP_LOCATIONS_P is true if expressions within this list for which
8344 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8345 their source locations.
8347 Returns a vector of trees. Each element is a representation of an
8348 assignment-expression. NULL is returned if the ( and or ) are
8349 missing. An empty, but allocated, vector is returned on no
8350 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8351 if we are parsing an attribute list for an attribute that wants a
8352 plain identifier argument, normal_attr for an attribute that wants
8353 an expression, or non_attr if we aren't parsing an attribute list. If
8354 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8355 not all of the expressions in the list were constant.
8356 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8357 will be written to with the location of the closing parenthesis. If
8358 an error occurs, it may or may not be written to. */
8360 static vec<tree, va_gc> *
8361 cp_parser_parenthesized_expression_list (cp_parser* parser,
8362 int is_attribute_list,
8363 bool cast_p,
8364 bool allow_expansion_p,
8365 bool *non_constant_p,
8366 location_t *close_paren_loc,
8367 bool wrap_locations_p)
8369 vec<tree, va_gc> *expression_list;
8370 bool fold_expr_p = is_attribute_list != non_attr;
8371 tree identifier = NULL_TREE;
8372 bool saved_greater_than_is_operator_p;
8374 /* Assume all the expressions will be constant. */
8375 if (non_constant_p)
8376 *non_constant_p = false;
8378 matching_parens parens;
8379 if (!parens.require_open (parser))
8380 return NULL;
8382 expression_list = make_tree_vector ();
8384 /* Within a parenthesized expression, a `>' token is always
8385 the greater-than operator. */
8386 saved_greater_than_is_operator_p
8387 = parser->greater_than_is_operator_p;
8388 parser->greater_than_is_operator_p = true;
8390 cp_expr expr (NULL_TREE);
8392 /* Consume expressions until there are no more. */
8393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8394 while (true)
8396 /* At the beginning of attribute lists, check to see if the
8397 next token is an identifier. */
8398 if (is_attribute_list == id_attr
8399 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8401 cp_token *token;
8403 /* Consume the identifier. */
8404 token = cp_lexer_consume_token (parser->lexer);
8405 /* Save the identifier. */
8406 identifier = token->u.value;
8408 else
8410 expr
8411 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8412 allow_expansion_p,
8413 fold_expr_p,
8414 non_constant_p);
8416 if (wrap_locations_p)
8417 expr.maybe_add_location_wrapper ();
8419 /* Add it to the list. We add error_mark_node
8420 expressions to the list, so that we can still tell if
8421 the correct form for a parenthesized expression-list
8422 is found. That gives better errors. */
8423 vec_safe_push (expression_list, expr.get_value ());
8425 if (expr == error_mark_node)
8426 goto skip_comma;
8429 /* After the first item, attribute lists look the same as
8430 expression lists. */
8431 is_attribute_list = non_attr;
8433 get_comma:;
8434 /* If the next token isn't a `,', then we are done. */
8435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8436 break;
8438 /* Otherwise, consume the `,' and keep going. */
8439 cp_lexer_consume_token (parser->lexer);
8442 if (close_paren_loc)
8443 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8445 if (!parens.require_close (parser))
8447 int ending;
8449 skip_comma:;
8450 /* We try and resync to an unnested comma, as that will give the
8451 user better diagnostics. */
8452 ending = cp_parser_skip_to_closing_parenthesis (parser,
8453 /*recovering=*/true,
8454 /*or_comma=*/true,
8455 /*consume_paren=*/true);
8456 if (ending < 0)
8457 goto get_comma;
8458 if (!ending)
8460 parser->greater_than_is_operator_p
8461 = saved_greater_than_is_operator_p;
8462 return NULL;
8466 parser->greater_than_is_operator_p
8467 = saved_greater_than_is_operator_p;
8469 if (identifier)
8470 vec_safe_insert (expression_list, 0, identifier);
8472 return expression_list;
8475 /* Parse a pseudo-destructor-name.
8477 pseudo-destructor-name:
8478 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8479 :: [opt] nested-name-specifier template template-id :: ~ type-name
8480 :: [opt] nested-name-specifier [opt] ~ type-name
8482 If either of the first two productions is used, sets *SCOPE to the
8483 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8484 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8485 or ERROR_MARK_NODE if the parse fails. */
8487 static void
8488 cp_parser_pseudo_destructor_name (cp_parser* parser,
8489 tree object,
8490 tree* scope,
8491 tree* type)
8493 bool nested_name_specifier_p;
8495 /* Handle ~auto. */
8496 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8497 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8498 && !type_dependent_expression_p (object))
8500 if (cxx_dialect < cxx14)
8501 pedwarn (input_location, OPT_Wc__14_extensions,
8502 "%<~auto%> only available with "
8503 "%<-std=c++14%> or %<-std=gnu++14%>");
8504 cp_lexer_consume_token (parser->lexer);
8505 cp_lexer_consume_token (parser->lexer);
8506 *scope = NULL_TREE;
8507 *type = TREE_TYPE (object);
8508 return;
8511 /* Assume that things will not work out. */
8512 *type = error_mark_node;
8514 /* Look for the optional `::' operator. */
8515 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8516 /* Look for the optional nested-name-specifier. */
8517 nested_name_specifier_p
8518 = (cp_parser_nested_name_specifier_opt (parser,
8519 /*typename_keyword_p=*/false,
8520 /*check_dependency_p=*/true,
8521 /*type_p=*/false,
8522 /*is_declaration=*/false)
8523 != NULL_TREE);
8524 /* Now, if we saw a nested-name-specifier, we might be doing the
8525 second production. */
8526 if (nested_name_specifier_p
8527 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8529 /* Consume the `template' keyword. */
8530 cp_lexer_consume_token (parser->lexer);
8531 /* Parse the template-id. */
8532 cp_parser_template_id (parser,
8533 /*template_keyword_p=*/true,
8534 /*check_dependency_p=*/false,
8535 class_type,
8536 /*is_declaration=*/true);
8537 /* Look for the `::' token. */
8538 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8540 /* If the next token is not a `~', then there might be some
8541 additional qualification. */
8542 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8544 /* At this point, we're looking for "type-name :: ~". The type-name
8545 must not be a class-name, since this is a pseudo-destructor. So,
8546 it must be either an enum-name, or a typedef-name -- both of which
8547 are just identifiers. So, we peek ahead to check that the "::"
8548 and "~" tokens are present; if they are not, then we can avoid
8549 calling type_name. */
8550 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8551 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8552 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8554 cp_parser_error (parser, "non-scalar type");
8555 return;
8558 /* Look for the type-name. */
8559 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8560 if (*scope == error_mark_node)
8561 return;
8563 /* Look for the `::' token. */
8564 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8566 else
8567 *scope = NULL_TREE;
8569 /* Look for the `~'. */
8570 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8572 /* Once we see the ~, this has to be a pseudo-destructor. */
8573 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8574 cp_parser_commit_to_topmost_tentative_parse (parser);
8576 /* Look for the type-name again. We are not responsible for
8577 checking that it matches the first type-name. */
8578 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8581 /* Parse a unary-expression.
8583 unary-expression:
8584 postfix-expression
8585 ++ cast-expression
8586 -- cast-expression
8587 await-expression
8588 unary-operator cast-expression
8589 sizeof unary-expression
8590 sizeof ( type-id )
8591 alignof ( type-id ) [C++0x]
8592 new-expression
8593 delete-expression
8595 GNU Extensions:
8597 unary-expression:
8598 __extension__ cast-expression
8599 __alignof__ unary-expression
8600 __alignof__ ( type-id )
8601 alignof unary-expression [C++0x]
8602 __real__ cast-expression
8603 __imag__ cast-expression
8604 && identifier
8605 sizeof ( type-id ) { initializer-list , [opt] }
8606 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8607 __alignof__ ( type-id ) { initializer-list , [opt] }
8609 ADDRESS_P is true iff the unary-expression is appearing as the
8610 operand of the `&' operator. CAST_P is true if this expression is
8611 the target of a cast.
8613 Returns a representation of the expression. */
8615 static cp_expr
8616 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8617 bool address_p, bool cast_p, bool decltype_p)
8619 cp_token *token;
8620 enum tree_code unary_operator;
8622 /* Peek at the next token. */
8623 token = cp_lexer_peek_token (parser->lexer);
8624 /* Some keywords give away the kind of expression. */
8625 if (token->type == CPP_KEYWORD)
8627 enum rid keyword = token->keyword;
8629 switch (keyword)
8631 case RID_ALIGNOF:
8632 case RID_SIZEOF:
8634 tree operand, ret;
8635 enum tree_code op;
8636 location_t start_loc = token->location;
8638 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8639 bool std_alignof = id_equal (token->u.value, "alignof");
8641 /* Consume the token. */
8642 cp_lexer_consume_token (parser->lexer);
8643 /* Parse the operand. */
8644 operand = cp_parser_sizeof_operand (parser, keyword);
8646 /* Construct a location e.g. :
8647 alignof (expr)
8648 ^~~~~~~~~~~~~~
8649 with start == caret at the start of the "alignof"/"sizeof"
8650 token, with the endpoint at the final closing paren. */
8651 location_t compound_loc
8652 = make_location (start_loc, start_loc, parser->lexer);
8654 if (TYPE_P (operand))
8655 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8656 std_alignof, true);
8657 else
8659 /* ISO C++ defines alignof only with types, not with
8660 expressions. So pedwarn if alignof is used with a non-
8661 type expression. However, __alignof__ is ok. */
8662 if (std_alignof)
8663 pedwarn (token->location, OPT_Wpedantic,
8664 "ISO C++ does not allow %<alignof%> "
8665 "with a non-type");
8667 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8668 std_alignof, true);
8670 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8671 SIZEOF_EXPR with the original operand. */
8672 if (op == SIZEOF_EXPR && ret != error_mark_node)
8674 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8676 if (!processing_template_decl && TYPE_P (operand))
8678 ret = build_min (SIZEOF_EXPR, size_type_node,
8679 build1 (NOP_EXPR, operand,
8680 error_mark_node));
8681 SIZEOF_EXPR_TYPE_P (ret) = 1;
8683 else
8684 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8685 TREE_SIDE_EFFECTS (ret) = 0;
8686 TREE_READONLY (ret) = 1;
8687 SET_EXPR_LOCATION (ret, compound_loc);
8691 cp_expr ret_expr (ret, compound_loc);
8692 ret_expr = ret_expr.maybe_add_location_wrapper ();
8693 return ret_expr;
8696 case RID_BUILTIN_HAS_ATTRIBUTE:
8697 return cp_parser_has_attribute_expression (parser);
8699 case RID_NEW:
8700 return cp_parser_new_expression (parser);
8702 case RID_DELETE:
8703 return cp_parser_delete_expression (parser);
8705 case RID_EXTENSION:
8707 /* The saved value of the PEDANTIC flag. */
8708 int saved_pedantic;
8709 tree expr;
8711 /* Save away the PEDANTIC flag. */
8712 cp_parser_extension_opt (parser, &saved_pedantic);
8713 /* Parse the cast-expression. */
8714 expr = cp_parser_simple_cast_expression (parser);
8715 /* Restore the PEDANTIC flag. */
8716 pedantic = saved_pedantic;
8718 return expr;
8721 case RID_REALPART:
8722 case RID_IMAGPART:
8724 tree expression;
8726 /* Consume the `__real__' or `__imag__' token. */
8727 cp_lexer_consume_token (parser->lexer);
8728 /* Parse the cast-expression. */
8729 expression = cp_parser_simple_cast_expression (parser);
8730 /* Create the complete representation. */
8731 return build_x_unary_op (token->location,
8732 (keyword == RID_REALPART
8733 ? REALPART_EXPR : IMAGPART_EXPR),
8734 expression, NULL_TREE,
8735 tf_warning_or_error);
8737 break;
8739 case RID_TRANSACTION_ATOMIC:
8740 case RID_TRANSACTION_RELAXED:
8741 return cp_parser_transaction_expression (parser, keyword);
8743 case RID_NOEXCEPT:
8745 tree expr;
8746 const char *saved_message;
8747 bool saved_integral_constant_expression_p;
8748 bool saved_non_integral_constant_expression_p;
8749 bool saved_greater_than_is_operator_p;
8751 location_t start_loc = token->location;
8753 cp_lexer_consume_token (parser->lexer);
8754 matching_parens parens;
8755 parens.require_open (parser);
8757 saved_message = parser->type_definition_forbidden_message;
8758 parser->type_definition_forbidden_message
8759 = G_("types may not be defined in %<noexcept%> expressions");
8761 saved_integral_constant_expression_p
8762 = parser->integral_constant_expression_p;
8763 saved_non_integral_constant_expression_p
8764 = parser->non_integral_constant_expression_p;
8765 parser->integral_constant_expression_p = false;
8767 saved_greater_than_is_operator_p
8768 = parser->greater_than_is_operator_p;
8769 parser->greater_than_is_operator_p = true;
8771 ++cp_unevaluated_operand;
8772 ++c_inhibit_evaluation_warnings;
8773 ++cp_noexcept_operand;
8774 expr = cp_parser_expression (parser);
8775 --cp_noexcept_operand;
8776 --c_inhibit_evaluation_warnings;
8777 --cp_unevaluated_operand;
8779 parser->greater_than_is_operator_p
8780 = saved_greater_than_is_operator_p;
8782 parser->integral_constant_expression_p
8783 = saved_integral_constant_expression_p;
8784 parser->non_integral_constant_expression_p
8785 = saved_non_integral_constant_expression_p;
8787 parser->type_definition_forbidden_message = saved_message;
8789 parens.require_close (parser);
8791 /* Construct a location of the form:
8792 noexcept (expr)
8793 ^~~~~~~~~~~~~~~
8794 with start == caret, finishing at the close-paren. */
8795 location_t noexcept_loc
8796 = make_location (start_loc, start_loc, parser->lexer);
8798 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8799 noexcept_loc);
8802 case RID_CO_AWAIT:
8804 tree expr;
8805 location_t kw_loc = token->location;
8807 /* Consume the `co_await' token. */
8808 cp_lexer_consume_token (parser->lexer);
8809 /* Parse its cast-expression. */
8810 expr = cp_parser_simple_cast_expression (parser);
8811 if (expr == error_mark_node)
8812 return error_mark_node;
8814 /* Handle [expr.await]. */
8815 return cp_expr (finish_co_await_expr (kw_loc, expr));
8818 default:
8819 break;
8823 /* Look for the `:: new' and `:: delete', which also signal the
8824 beginning of a new-expression, or delete-expression,
8825 respectively. If the next token is `::', then it might be one of
8826 these. */
8827 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8829 enum rid keyword;
8831 /* See if the token after the `::' is one of the keywords in
8832 which we're interested. */
8833 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8834 /* If it's `new', we have a new-expression. */
8835 if (keyword == RID_NEW)
8836 return cp_parser_new_expression (parser);
8837 /* Similarly, for `delete'. */
8838 else if (keyword == RID_DELETE)
8839 return cp_parser_delete_expression (parser);
8842 /* Look for a unary operator. */
8843 unary_operator = cp_parser_unary_operator (token);
8844 /* The `++' and `--' operators can be handled similarly, even though
8845 they are not technically unary-operators in the grammar. */
8846 if (unary_operator == ERROR_MARK)
8848 if (token->type == CPP_PLUS_PLUS)
8849 unary_operator = PREINCREMENT_EXPR;
8850 else if (token->type == CPP_MINUS_MINUS)
8851 unary_operator = PREDECREMENT_EXPR;
8852 /* Handle the GNU address-of-label extension. */
8853 else if (cp_parser_allow_gnu_extensions_p (parser)
8854 && token->type == CPP_AND_AND)
8856 tree identifier;
8857 tree expression;
8858 location_t start_loc = token->location;
8860 /* Consume the '&&' token. */
8861 cp_lexer_consume_token (parser->lexer);
8862 /* Look for the identifier. */
8863 identifier = cp_parser_identifier (parser);
8864 /* Construct a location of the form:
8865 &&label
8866 ^~~~~~~
8867 with caret==start at the "&&", finish at the end of the label. */
8868 location_t combined_loc
8869 = make_location (start_loc, start_loc, parser->lexer);
8870 /* Create an expression representing the address. */
8871 expression = finish_label_address_expr (identifier, combined_loc);
8872 if (cp_parser_non_integral_constant_expression (parser,
8873 NIC_ADDR_LABEL))
8874 expression = error_mark_node;
8875 return expression;
8878 if (unary_operator != ERROR_MARK)
8880 cp_expr cast_expression;
8881 cp_expr expression = error_mark_node;
8882 non_integral_constant non_constant_p = NIC_NONE;
8883 location_t loc = token->location;
8884 tsubst_flags_t complain = complain_flags (decltype_p);
8886 /* Consume the operator token. */
8887 token = cp_lexer_consume_token (parser->lexer);
8888 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8890 /* Parse the cast-expression. */
8891 cast_expression
8892 = cp_parser_cast_expression (parser,
8893 unary_operator == ADDR_EXPR,
8894 /*cast_p=*/false,
8895 /*decltype*/false,
8896 pidk);
8898 /* Make a location:
8899 OP_TOKEN CAST_EXPRESSION
8900 ^~~~~~~~~~~~~~~~~~~~~~~~~
8901 with start==caret at the operator token, and
8902 extending to the end of the cast_expression. */
8903 loc = make_location (loc, loc, cast_expression.get_finish ());
8905 /* Now, build an appropriate representation. */
8906 switch (unary_operator)
8908 case INDIRECT_REF:
8909 non_constant_p = NIC_STAR;
8910 expression = build_x_indirect_ref (loc, cast_expression,
8911 RO_UNARY_STAR, NULL_TREE,
8912 complain);
8913 /* TODO: build_x_indirect_ref does not always honor the
8914 location, so ensure it is set. */
8915 expression.set_location (loc);
8916 break;
8918 case ADDR_EXPR:
8919 non_constant_p = NIC_ADDR;
8920 /* Fall through. */
8921 case BIT_NOT_EXPR:
8922 expression = build_x_unary_op (loc, unary_operator,
8923 cast_expression,
8924 NULL_TREE, complain);
8925 /* TODO: build_x_unary_op does not always honor the location,
8926 so ensure it is set. */
8927 expression.set_location (loc);
8928 break;
8930 case PREINCREMENT_EXPR:
8931 case PREDECREMENT_EXPR:
8932 non_constant_p = unary_operator == PREINCREMENT_EXPR
8933 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8934 /* Fall through. */
8935 case NEGATE_EXPR:
8936 /* Immediately fold negation of a constant, unless the constant is 0
8937 (since -0 == 0) or it would overflow. */
8938 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8940 tree stripped_expr
8941 = tree_strip_any_location_wrapper (cast_expression);
8942 if (CONSTANT_CLASS_P (stripped_expr)
8943 && !integer_zerop (stripped_expr)
8944 && !TREE_OVERFLOW (stripped_expr))
8946 tree folded = fold_build1 (unary_operator,
8947 TREE_TYPE (stripped_expr),
8948 stripped_expr);
8949 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8951 expression = maybe_wrap_with_location (folded, loc);
8952 break;
8956 /* Fall through. */
8957 case UNARY_PLUS_EXPR:
8958 case TRUTH_NOT_EXPR:
8959 expression = finish_unary_op_expr (loc, unary_operator,
8960 cast_expression, complain);
8961 break;
8963 default:
8964 gcc_unreachable ();
8967 if (non_constant_p != NIC_NONE
8968 && cp_parser_non_integral_constant_expression (parser,
8969 non_constant_p))
8970 expression = error_mark_node;
8972 return expression;
8975 return cp_parser_postfix_expression (parser, address_p, cast_p,
8976 /*member_access_only_p=*/false,
8977 decltype_p,
8978 pidk);
8981 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8982 unary-operator, the corresponding tree code is returned. */
8984 static enum tree_code
8985 cp_parser_unary_operator (cp_token* token)
8987 switch (token->type)
8989 case CPP_MULT:
8990 return INDIRECT_REF;
8992 case CPP_AND:
8993 return ADDR_EXPR;
8995 case CPP_PLUS:
8996 return UNARY_PLUS_EXPR;
8998 case CPP_MINUS:
8999 return NEGATE_EXPR;
9001 case CPP_NOT:
9002 return TRUTH_NOT_EXPR;
9004 case CPP_COMPL:
9005 return BIT_NOT_EXPR;
9007 default:
9008 return ERROR_MARK;
9012 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9013 Returns a representation of the expression. */
9015 static tree
9016 cp_parser_has_attribute_expression (cp_parser *parser)
9018 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9020 /* Consume the __builtin_has_attribute token. */
9021 cp_lexer_consume_token (parser->lexer);
9023 matching_parens parens;
9024 if (!parens.require_open (parser))
9025 return error_mark_node;
9027 /* Types cannot be defined in a `sizeof' expression. Save away the
9028 old message. */
9029 const char *saved_message = parser->type_definition_forbidden_message;
9030 const char *saved_message_arg
9031 = parser->type_definition_forbidden_message_arg;
9032 parser->type_definition_forbidden_message
9033 = G_("types may not be defined in %qs expressions");
9034 parser->type_definition_forbidden_message_arg
9035 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9037 /* The restrictions on constant-expressions do not apply inside
9038 sizeof expressions. */
9039 bool saved_integral_constant_expression_p
9040 = parser->integral_constant_expression_p;
9041 bool saved_non_integral_constant_expression_p
9042 = parser->non_integral_constant_expression_p;
9043 parser->integral_constant_expression_p = false;
9045 /* Do not actually evaluate the expression. */
9046 ++cp_unevaluated_operand;
9047 ++c_inhibit_evaluation_warnings;
9049 tree oper = NULL_TREE;
9051 /* We can't be sure yet whether we're looking at a type-id or an
9052 expression. */
9053 cp_parser_parse_tentatively (parser);
9055 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9056 parser->in_type_id_in_expr_p = true;
9057 /* Look for the type-id. */
9058 oper = cp_parser_type_id (parser);
9059 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9061 cp_parser_parse_definitely (parser);
9063 /* If the type-id production did not work out, then we must be
9064 looking at an expression. */
9065 if (!oper || oper == error_mark_node)
9066 oper = cp_parser_assignment_expression (parser);
9068 STRIP_ANY_LOCATION_WRAPPER (oper);
9070 /* Go back to evaluating expressions. */
9071 --cp_unevaluated_operand;
9072 --c_inhibit_evaluation_warnings;
9074 /* And restore the old one. */
9075 parser->type_definition_forbidden_message = saved_message;
9076 parser->type_definition_forbidden_message_arg = saved_message_arg;
9077 parser->integral_constant_expression_p
9078 = saved_integral_constant_expression_p;
9079 parser->non_integral_constant_expression_p
9080 = saved_non_integral_constant_expression_p;
9082 /* Consume the comma if it's there. */
9083 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9085 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9086 /*consume_paren=*/true);
9087 return error_mark_node;
9090 /* Parse the attribute specification. */
9091 bool ret = false;
9092 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9093 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9095 if (oper == error_mark_node)
9096 /* Nothing. */;
9097 else if (processing_template_decl && uses_template_parms (oper))
9098 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9099 "not supported yet");
9100 else
9102 /* Fold constant expressions used in attributes first. */
9103 cp_check_const_attributes (attr);
9105 /* Finally, see if OPER has been declared with ATTR. */
9106 ret = has_attribute (atloc, oper, attr, default_conversion);
9109 parens.require_close (parser);
9111 else
9113 error_at (atloc, "expected identifier");
9114 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9117 /* Construct a location e.g. :
9118 __builtin_has_attribute (oper, attr)
9119 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9120 with start == caret at the start of the built-in token,
9121 and with the endpoint at the final closing paren. */
9122 location_t compound_loc
9123 = make_location (start_loc, start_loc, parser->lexer);
9125 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9126 ret_expr.set_location (compound_loc);
9127 ret_expr = ret_expr.maybe_add_location_wrapper ();
9128 return ret_expr;
9131 /* Parse a new-expression.
9133 new-expression:
9134 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9135 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9137 Returns a representation of the expression. */
9139 static tree
9140 cp_parser_new_expression (cp_parser* parser)
9142 bool global_scope_p;
9143 vec<tree, va_gc> *placement;
9144 tree type;
9145 vec<tree, va_gc> *initializer;
9146 tree nelts = NULL_TREE;
9147 tree ret;
9149 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9151 /* Look for the optional `::' operator. */
9152 global_scope_p
9153 = (cp_parser_global_scope_opt (parser,
9154 /*current_scope_valid_p=*/false)
9155 != NULL_TREE);
9156 /* Look for the `new' operator. */
9157 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9158 /* There's no easy way to tell a new-placement from the
9159 `( type-id )' construct. */
9160 cp_parser_parse_tentatively (parser);
9161 /* Look for a new-placement. */
9162 placement = cp_parser_new_placement (parser);
9163 /* If that didn't work out, there's no new-placement. */
9164 if (!cp_parser_parse_definitely (parser))
9166 if (placement != NULL)
9167 release_tree_vector (placement);
9168 placement = NULL;
9171 /* If the next token is a `(', then we have a parenthesized
9172 type-id. */
9173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9175 cp_token *token;
9176 const char *saved_message = parser->type_definition_forbidden_message;
9178 /* Consume the `('. */
9179 matching_parens parens;
9180 parens.consume_open (parser);
9182 /* Parse the type-id. */
9183 parser->type_definition_forbidden_message
9184 = G_("types may not be defined in a new-expression");
9186 type_id_in_expr_sentinel s (parser);
9187 type = cp_parser_type_id (parser);
9189 parser->type_definition_forbidden_message = saved_message;
9191 /* Look for the closing `)'. */
9192 parens.require_close (parser);
9193 token = cp_lexer_peek_token (parser->lexer);
9194 /* There should not be a direct-new-declarator in this production,
9195 but GCC used to allowed this, so we check and emit a sensible error
9196 message for this case. */
9197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9199 error_at (token->location,
9200 "array bound forbidden after parenthesized type-id");
9201 inform (token->location,
9202 "try removing the parentheses around the type-id");
9203 cp_parser_direct_new_declarator (parser);
9206 /* Otherwise, there must be a new-type-id. */
9207 else
9208 type = cp_parser_new_type_id (parser, &nelts);
9210 /* If the next token is a `(' or '{', then we have a new-initializer. */
9211 cp_token *token = cp_lexer_peek_token (parser->lexer);
9212 if (token->type == CPP_OPEN_PAREN
9213 || token->type == CPP_OPEN_BRACE)
9214 initializer = cp_parser_new_initializer (parser);
9215 else
9216 initializer = NULL;
9218 /* A new-expression may not appear in an integral constant
9219 expression. */
9220 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9221 ret = error_mark_node;
9222 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9223 of a new-type-id or type-id of a new-expression, the new-expression shall
9224 contain a new-initializer of the form ( assignment-expression )".
9225 Additionally, consistently with the spirit of DR 1467, we want to accept
9226 'new auto { 2 }' too. */
9227 else if ((ret = type_uses_auto (type))
9228 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9229 && (vec_safe_length (initializer) != 1
9230 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9231 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9233 error_at (token->location,
9234 "initialization of new-expression for type %<auto%> "
9235 "requires exactly one element");
9236 ret = error_mark_node;
9238 else
9240 /* Construct a location e.g.:
9241 ptr = new int[100]
9242 ^~~~~~~~~~~~
9243 with caret == start at the start of the "new" token, and the end
9244 at the end of the final token we consumed. */
9245 location_t combined_loc = make_location (start_loc, start_loc,
9246 parser->lexer);
9247 /* Create a representation of the new-expression. */
9248 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9249 global_scope_p, tf_warning_or_error);
9252 if (placement != NULL)
9253 release_tree_vector (placement);
9254 if (initializer != NULL)
9255 release_tree_vector (initializer);
9257 return ret;
9260 /* Parse a new-placement.
9262 new-placement:
9263 ( expression-list )
9265 Returns the same representation as for an expression-list. */
9267 static vec<tree, va_gc> *
9268 cp_parser_new_placement (cp_parser* parser)
9270 vec<tree, va_gc> *expression_list;
9272 /* Parse the expression-list. */
9273 expression_list = (cp_parser_parenthesized_expression_list
9274 (parser, non_attr, /*cast_p=*/false,
9275 /*allow_expansion_p=*/true,
9276 /*non_constant_p=*/NULL));
9278 if (expression_list && expression_list->is_empty ())
9279 error ("expected expression-list or type-id");
9281 return expression_list;
9284 /* Parse a new-type-id.
9286 new-type-id:
9287 type-specifier-seq new-declarator [opt]
9289 Returns the TYPE allocated. If the new-type-id indicates an array
9290 type, *NELTS is set to the number of elements in the last array
9291 bound; the TYPE will not include the last array bound. */
9293 static tree
9294 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9296 cp_decl_specifier_seq type_specifier_seq;
9297 cp_declarator *new_declarator;
9298 cp_declarator *declarator;
9299 cp_declarator *outer_declarator;
9300 const char *saved_message;
9302 /* The type-specifier sequence must not contain type definitions.
9303 (It cannot contain declarations of new types either, but if they
9304 are not definitions we will catch that because they are not
9305 complete.) */
9306 saved_message = parser->type_definition_forbidden_message;
9307 parser->type_definition_forbidden_message
9308 = G_("types may not be defined in a new-type-id");
9309 /* Parse the type-specifier-seq. */
9310 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9311 /*is_declaration=*/false,
9312 /*is_trailing_return=*/false,
9313 &type_specifier_seq);
9314 /* Restore the old message. */
9315 parser->type_definition_forbidden_message = saved_message;
9317 if (type_specifier_seq.type == error_mark_node)
9318 return error_mark_node;
9320 /* Parse the new-declarator. */
9321 new_declarator = cp_parser_new_declarator_opt (parser);
9323 /* Determine the number of elements in the last array dimension, if
9324 any. */
9325 *nelts = NULL_TREE;
9326 /* Skip down to the last array dimension. */
9327 declarator = new_declarator;
9328 outer_declarator = NULL;
9329 while (declarator && (declarator->kind == cdk_pointer
9330 || declarator->kind == cdk_ptrmem))
9332 outer_declarator = declarator;
9333 declarator = declarator->declarator;
9335 while (declarator
9336 && declarator->kind == cdk_array
9337 && declarator->declarator
9338 && declarator->declarator->kind == cdk_array)
9340 outer_declarator = declarator;
9341 declarator = declarator->declarator;
9344 if (declarator && declarator->kind == cdk_array)
9346 *nelts = declarator->u.array.bounds;
9347 if (*nelts == error_mark_node)
9348 *nelts = integer_one_node;
9350 if (*nelts == NULL_TREE)
9351 /* Leave [] in the declarator. */;
9352 else if (outer_declarator)
9353 outer_declarator->declarator = declarator->declarator;
9354 else
9355 new_declarator = NULL;
9358 return groktypename (&type_specifier_seq, new_declarator, false);
9361 /* Parse an (optional) new-declarator.
9363 new-declarator:
9364 ptr-operator new-declarator [opt]
9365 direct-new-declarator
9367 Returns the declarator. */
9369 static cp_declarator *
9370 cp_parser_new_declarator_opt (cp_parser* parser)
9372 enum tree_code code;
9373 tree type, std_attributes = NULL_TREE;
9374 cp_cv_quals cv_quals;
9376 /* We don't know if there's a ptr-operator next, or not. */
9377 cp_parser_parse_tentatively (parser);
9378 /* Look for a ptr-operator. */
9379 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9380 /* If that worked, look for more new-declarators. */
9381 if (cp_parser_parse_definitely (parser))
9383 cp_declarator *declarator;
9385 /* Parse another optional declarator. */
9386 declarator = cp_parser_new_declarator_opt (parser);
9388 declarator = cp_parser_make_indirect_declarator
9389 (code, type, cv_quals, declarator, std_attributes);
9391 return declarator;
9394 /* If the next token is a `[', there is a direct-new-declarator. */
9395 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9396 return cp_parser_direct_new_declarator (parser);
9398 return NULL;
9401 /* Parse a direct-new-declarator.
9403 direct-new-declarator:
9404 [ expression ]
9405 direct-new-declarator [constant-expression]
9409 static cp_declarator *
9410 cp_parser_direct_new_declarator (cp_parser* parser)
9412 cp_declarator *declarator = NULL;
9413 bool first_p = true;
9415 while (true)
9417 tree expression;
9418 cp_token *token;
9420 /* Look for the opening `['. */
9421 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9423 token = cp_lexer_peek_token (parser->lexer);
9424 if (token->type == CPP_CLOSE_SQUARE && first_p)
9425 expression = NULL_TREE;
9426 else
9427 expression = cp_parser_expression (parser);
9428 /* The standard requires that the expression have integral
9429 type. DR 74 adds enumeration types. We believe that the
9430 real intent is that these expressions be handled like the
9431 expression in a `switch' condition, which also allows
9432 classes with a single conversion to integral or
9433 enumeration type. */
9434 if (expression && !processing_template_decl)
9436 expression
9437 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9438 expression,
9439 /*complain=*/true);
9440 if (!expression)
9442 error_at (token->location,
9443 "expression in new-declarator must have integral "
9444 "or enumeration type");
9445 expression = error_mark_node;
9449 /* Look for the closing `]'. */
9450 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9452 /* Add this bound to the declarator. */
9453 declarator = make_array_declarator (declarator, expression);
9455 /* If the next token is not a `[', then there are no more
9456 bounds. */
9457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9458 break;
9459 first_p = false;
9462 return declarator;
9465 /* Parse a new-initializer.
9467 new-initializer:
9468 ( expression-list [opt] )
9469 braced-init-list
9471 Returns a representation of the expression-list. */
9473 static vec<tree, va_gc> *
9474 cp_parser_new_initializer (cp_parser* parser)
9476 vec<tree, va_gc> *expression_list;
9478 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9480 tree t;
9481 bool expr_non_constant_p;
9482 cp_lexer_set_source_position (parser->lexer);
9483 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9484 t = cp_parser_braced_list (parser, &expr_non_constant_p);
9485 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9486 expression_list = make_tree_vector_single (t);
9488 else
9489 expression_list = (cp_parser_parenthesized_expression_list
9490 (parser, non_attr, /*cast_p=*/false,
9491 /*allow_expansion_p=*/true,
9492 /*non_constant_p=*/NULL));
9494 return expression_list;
9497 /* Parse a delete-expression.
9499 delete-expression:
9500 :: [opt] delete cast-expression
9501 :: [opt] delete [ ] cast-expression
9503 Returns a representation of the expression. */
9505 static tree
9506 cp_parser_delete_expression (cp_parser* parser)
9508 bool global_scope_p;
9509 bool array_p;
9510 tree expression;
9511 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9513 /* Look for the optional `::' operator. */
9514 global_scope_p
9515 = (cp_parser_global_scope_opt (parser,
9516 /*current_scope_valid_p=*/false)
9517 != NULL_TREE);
9518 /* Look for the `delete' keyword. */
9519 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9520 /* See if the array syntax is in use. */
9521 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9523 /* Consume the `[' token. */
9524 cp_lexer_consume_token (parser->lexer);
9525 /* Look for the `]' token. */
9526 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9527 /* Remember that this is the `[]' construct. */
9528 array_p = true;
9530 else
9531 array_p = false;
9533 /* Parse the cast-expression. */
9534 expression = cp_parser_simple_cast_expression (parser);
9536 /* A delete-expression may not appear in an integral constant
9537 expression. */
9538 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9539 return error_mark_node;
9541 /* Construct a location e.g.:
9542 delete [ ] ptr
9543 ^~~~~~~~~~~~~~
9544 with caret == start at the start of the "delete" token, and
9545 the end at the end of the final token we consumed. */
9546 location_t combined_loc = make_location (start_loc, start_loc,
9547 parser->lexer);
9548 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9549 global_scope_p, tf_warning_or_error);
9551 return expression;
9554 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9555 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9556 0 otherwise. */
9558 static int
9559 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9561 cp_token *token = cp_lexer_peek_token (parser->lexer);
9562 switch (token->type)
9564 case CPP_COMMA:
9565 case CPP_SEMICOLON:
9566 case CPP_QUERY:
9567 case CPP_COLON:
9568 case CPP_CLOSE_SQUARE:
9569 case CPP_CLOSE_PAREN:
9570 case CPP_CLOSE_BRACE:
9571 case CPP_OPEN_BRACE:
9572 case CPP_DOT:
9573 case CPP_DOT_STAR:
9574 case CPP_DEREF:
9575 case CPP_DEREF_STAR:
9576 case CPP_DIV:
9577 case CPP_MOD:
9578 case CPP_LSHIFT:
9579 case CPP_RSHIFT:
9580 case CPP_LESS:
9581 case CPP_GREATER:
9582 case CPP_LESS_EQ:
9583 case CPP_GREATER_EQ:
9584 case CPP_EQ_EQ:
9585 case CPP_NOT_EQ:
9586 case CPP_EQ:
9587 case CPP_MULT_EQ:
9588 case CPP_DIV_EQ:
9589 case CPP_MOD_EQ:
9590 case CPP_PLUS_EQ:
9591 case CPP_MINUS_EQ:
9592 case CPP_RSHIFT_EQ:
9593 case CPP_LSHIFT_EQ:
9594 case CPP_AND_EQ:
9595 case CPP_XOR_EQ:
9596 case CPP_OR_EQ:
9597 case CPP_XOR:
9598 case CPP_OR:
9599 case CPP_OR_OR:
9600 case CPP_EOF:
9601 case CPP_ELLIPSIS:
9602 return 0;
9604 case CPP_OPEN_PAREN:
9605 /* In ((type ()) () the last () isn't a valid cast-expression,
9606 so the whole must be parsed as postfix-expression. */
9607 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9608 != CPP_CLOSE_PAREN;
9610 case CPP_OPEN_SQUARE:
9611 /* '[' may start a primary-expression in obj-c++ and in C++11,
9612 as a lambda-expression, eg, '(void)[]{}'. */
9613 if (cxx_dialect >= cxx11)
9614 return -1;
9615 return c_dialect_objc ();
9617 case CPP_PLUS_PLUS:
9618 case CPP_MINUS_MINUS:
9619 /* '++' and '--' may or may not start a cast-expression:
9621 struct T { void operator++(int); };
9622 void f() { (T())++; }
9626 int a;
9627 (int)++a; */
9628 return -1;
9630 default:
9631 return 1;
9635 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9636 in the order: const_cast, static_cast, reinterpret_cast.
9638 Don't suggest dynamic_cast.
9640 Return the first legal cast kind found, or NULL otherwise. */
9642 static const char *
9643 get_cast_suggestion (tree dst_type, tree orig_expr)
9645 tree trial;
9647 /* Reuse the parser logic by attempting to build the various kinds of
9648 cast, with "complain" disabled.
9649 Identify the first such cast that is valid. */
9651 /* Don't attempt to run such logic within template processing. */
9652 if (processing_template_decl)
9653 return NULL;
9655 /* First try const_cast. */
9656 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9657 if (trial != error_mark_node)
9658 return "const_cast";
9660 /* If that fails, try static_cast. */
9661 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9662 if (trial != error_mark_node)
9663 return "static_cast";
9665 /* Finally, try reinterpret_cast. */
9666 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9667 tf_none);
9668 if (trial != error_mark_node)
9669 return "reinterpret_cast";
9671 /* No such cast possible. */
9672 return NULL;
9675 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9676 suggesting how to convert a C-style cast of the form:
9678 (DST_TYPE)ORIG_EXPR
9680 to a C++-style cast.
9682 The primary range of RICHLOC is asssumed to be that of the original
9683 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9684 of the parens in the C-style cast. */
9686 static void
9687 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9688 location_t close_paren_loc, tree orig_expr,
9689 tree dst_type)
9691 /* This function is non-trivial, so bail out now if the warning isn't
9692 going to be emitted. */
9693 if (!warn_old_style_cast)
9694 return;
9696 /* Try to find a legal C++ cast, trying them in order:
9697 const_cast, static_cast, reinterpret_cast. */
9698 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9699 if (!cast_suggestion)
9700 return;
9702 /* Replace the open paren with "CAST_SUGGESTION<". */
9703 pretty_printer pp;
9704 pp_string (&pp, cast_suggestion);
9705 pp_less (&pp);
9706 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9708 /* Replace the close paren with "> (". */
9709 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9711 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9712 rich_loc->add_fixit_insert_after (")");
9716 /* Parse a cast-expression.
9718 cast-expression:
9719 unary-expression
9720 ( type-id ) cast-expression
9722 ADDRESS_P is true iff the unary-expression is appearing as the
9723 operand of the `&' operator. CAST_P is true if this expression is
9724 the target of a cast.
9726 Returns a representation of the expression. */
9728 static cp_expr
9729 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9730 bool decltype_p, cp_id_kind * pidk)
9732 /* If it's a `(', then we might be looking at a cast. */
9733 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9735 tree type = NULL_TREE;
9736 cp_expr expr (NULL_TREE);
9737 int cast_expression = 0;
9738 const char *saved_message;
9740 /* There's no way to know yet whether or not this is a cast.
9741 For example, `(int (3))' is a unary-expression, while `(int)
9742 3' is a cast. So, we resort to parsing tentatively. */
9743 cp_parser_parse_tentatively (parser);
9744 /* Types may not be defined in a cast. */
9745 saved_message = parser->type_definition_forbidden_message;
9746 parser->type_definition_forbidden_message
9747 = G_("types may not be defined in casts");
9748 /* Consume the `('. */
9749 matching_parens parens;
9750 cp_token *open_paren = parens.consume_open (parser);
9751 location_t open_paren_loc = open_paren->location;
9752 location_t close_paren_loc = UNKNOWN_LOCATION;
9754 /* A very tricky bit is that `(struct S) { 3 }' is a
9755 compound-literal (which we permit in C++ as an extension).
9756 But, that construct is not a cast-expression -- it is a
9757 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9758 is legal; if the compound-literal were a cast-expression,
9759 you'd need an extra set of parentheses.) But, if we parse
9760 the type-id, and it happens to be a class-specifier, then we
9761 will commit to the parse at that point, because we cannot
9762 undo the action that is done when creating a new class. So,
9763 then we cannot back up and do a postfix-expression.
9765 Another tricky case is the following (c++/29234):
9767 struct S { void operator () (); };
9769 void foo ()
9771 ( S()() );
9774 As a type-id we parse the parenthesized S()() as a function
9775 returning a function, groktypename complains and we cannot
9776 back up in this case either.
9778 Therefore, we scan ahead to the closing `)', and check to see
9779 if the tokens after the `)' can start a cast-expression. Otherwise
9780 we are dealing with an unary-expression, a postfix-expression
9781 or something else.
9783 Yet another tricky case, in C++11, is the following (c++/54891):
9785 (void)[]{};
9787 The issue is that usually, besides the case of lambda-expressions,
9788 the parenthesized type-id cannot be followed by '[', and, eg, we
9789 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9790 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9791 we don't commit, we try a cast-expression, then an unary-expression.
9793 Save tokens so that we can put them back. */
9794 cp_lexer_save_tokens (parser->lexer);
9796 /* We may be looking at a cast-expression. */
9797 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9798 /*consume_paren=*/true))
9799 cast_expression
9800 = cp_parser_tokens_start_cast_expression (parser);
9802 /* Roll back the tokens we skipped. */
9803 cp_lexer_rollback_tokens (parser->lexer);
9804 /* If we aren't looking at a cast-expression, simulate an error so
9805 that the call to cp_parser_error_occurred below returns true. */
9806 if (!cast_expression)
9807 cp_parser_simulate_error (parser);
9808 else
9810 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9811 parser->in_type_id_in_expr_p = true;
9812 /* Look for the type-id. */
9813 type = cp_parser_type_id (parser);
9814 /* Look for the closing `)'. */
9815 cp_token *close_paren = parens.require_close (parser);
9816 if (close_paren)
9817 close_paren_loc = close_paren->location;
9818 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9821 /* Restore the saved message. */
9822 parser->type_definition_forbidden_message = saved_message;
9824 /* At this point this can only be either a cast or a
9825 parenthesized ctor such as `(T ())' that looks like a cast to
9826 function returning T. */
9827 if (!cp_parser_error_occurred (parser))
9829 /* Only commit if the cast-expression doesn't start with
9830 '++', '--', or '[' in C++11. */
9831 if (cast_expression > 0)
9832 cp_parser_commit_to_topmost_tentative_parse (parser);
9834 expr = cp_parser_cast_expression (parser,
9835 /*address_p=*/false,
9836 /*cast_p=*/true,
9837 /*decltype_p=*/false,
9838 pidk);
9840 if (cp_parser_parse_definitely (parser))
9842 /* Warn about old-style casts, if so requested. */
9843 if (warn_old_style_cast
9844 && !in_system_header_at (input_location)
9845 && !VOID_TYPE_P (type)
9846 && current_lang_name != lang_name_c)
9848 gcc_rich_location rich_loc (input_location);
9849 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9850 expr, type);
9851 warning_at (&rich_loc, OPT_Wold_style_cast,
9852 "use of old-style cast to %q#T", type);
9855 /* Only type conversions to integral or enumeration types
9856 can be used in constant-expressions. */
9857 if (!cast_valid_in_integral_constant_expression_p (type)
9858 && cp_parser_non_integral_constant_expression (parser,
9859 NIC_CAST))
9860 return error_mark_node;
9862 /* Perform the cast. */
9863 /* Make a location:
9864 (TYPE) EXPR
9865 ^~~~~~~~~~~
9866 with start==caret at the open paren, extending to the
9867 end of "expr". */
9868 location_t cast_loc = make_location (open_paren_loc,
9869 open_paren_loc,
9870 expr.get_finish ());
9871 expr = build_c_cast (cast_loc, type, expr);
9872 return expr;
9875 else
9876 cp_parser_abort_tentative_parse (parser);
9879 /* If we get here, then it's not a cast, so it must be a
9880 unary-expression. */
9881 return cp_parser_unary_expression (parser, pidk, address_p,
9882 cast_p, decltype_p);
9885 /* Parse a binary expression of the general form:
9887 pm-expression:
9888 cast-expression
9889 pm-expression .* cast-expression
9890 pm-expression ->* cast-expression
9892 multiplicative-expression:
9893 pm-expression
9894 multiplicative-expression * pm-expression
9895 multiplicative-expression / pm-expression
9896 multiplicative-expression % pm-expression
9898 additive-expression:
9899 multiplicative-expression
9900 additive-expression + multiplicative-expression
9901 additive-expression - multiplicative-expression
9903 shift-expression:
9904 additive-expression
9905 shift-expression << additive-expression
9906 shift-expression >> additive-expression
9908 relational-expression:
9909 shift-expression
9910 relational-expression < shift-expression
9911 relational-expression > shift-expression
9912 relational-expression <= shift-expression
9913 relational-expression >= shift-expression
9915 GNU Extension:
9917 relational-expression:
9918 relational-expression <? shift-expression
9919 relational-expression >? shift-expression
9921 equality-expression:
9922 relational-expression
9923 equality-expression == relational-expression
9924 equality-expression != relational-expression
9926 and-expression:
9927 equality-expression
9928 and-expression & equality-expression
9930 exclusive-or-expression:
9931 and-expression
9932 exclusive-or-expression ^ and-expression
9934 inclusive-or-expression:
9935 exclusive-or-expression
9936 inclusive-or-expression | exclusive-or-expression
9938 logical-and-expression:
9939 inclusive-or-expression
9940 logical-and-expression && inclusive-or-expression
9942 logical-or-expression:
9943 logical-and-expression
9944 logical-or-expression || logical-and-expression
9946 All these are implemented with a single function like:
9948 binary-expression:
9949 simple-cast-expression
9950 binary-expression <token> binary-expression
9952 CAST_P is true if this expression is the target of a cast.
9954 The binops_by_token map is used to get the tree codes for each <token> type.
9955 binary-expressions are associated according to a precedence table. */
9957 #define TOKEN_PRECEDENCE(token) \
9958 (((token->type == CPP_GREATER \
9959 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9960 && !parser->greater_than_is_operator_p) \
9961 ? PREC_NOT_OPERATOR \
9962 : binops_by_token[token->type].prec)
9964 static cp_expr
9965 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9966 bool no_toplevel_fold_p,
9967 bool decltype_p,
9968 enum cp_parser_prec prec,
9969 cp_id_kind * pidk)
9971 cp_parser_expression_stack stack;
9972 cp_parser_expression_stack_entry *sp = &stack[0];
9973 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9974 cp_parser_expression_stack_entry current;
9975 cp_expr rhs;
9976 cp_token *token;
9977 enum tree_code rhs_type;
9978 enum cp_parser_prec new_prec, lookahead_prec;
9979 tree overload;
9981 /* Parse the first expression. */
9982 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9983 ? TRUTH_NOT_EXPR : ERROR_MARK);
9984 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9985 cast_p, decltype_p, pidk);
9986 current.prec = prec;
9988 if (cp_parser_error_occurred (parser))
9989 return error_mark_node;
9991 for (;;)
9993 /* Get an operator token. */
9994 token = cp_lexer_peek_token (parser->lexer);
9996 if (warn_cxx11_compat
9997 && token->type == CPP_RSHIFT
9998 && !parser->greater_than_is_operator_p)
10000 if (warning_at (token->location, OPT_Wc__11_compat,
10001 "%<>>%> operator is treated"
10002 " as two right angle brackets in C++11"))
10003 inform (token->location,
10004 "suggest parentheses around %<>>%> expression");
10007 new_prec = TOKEN_PRECEDENCE (token);
10008 if (new_prec != PREC_NOT_OPERATOR
10009 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10010 /* This is a fold-expression; handle it later. */
10011 new_prec = PREC_NOT_OPERATOR;
10013 /* Popping an entry off the stack means we completed a subexpression:
10014 - either we found a token which is not an operator (`>' where it is not
10015 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10016 will happen repeatedly;
10017 - or, we found an operator which has lower priority. This is the case
10018 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10019 parsing `3 * 4'. */
10020 if (new_prec <= current.prec)
10022 if (sp == stack)
10023 break;
10024 else
10025 goto pop;
10028 get_rhs:
10029 current.tree_type = binops_by_token[token->type].tree_type;
10030 current.loc = token->location;
10032 /* We used the operator token. */
10033 cp_lexer_consume_token (parser->lexer);
10035 /* For "false && x" or "true || x", x will never be executed;
10036 disable warnings while evaluating it. */
10037 if ((current.tree_type == TRUTH_ANDIF_EXPR
10038 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10039 || (current.tree_type == TRUTH_ORIF_EXPR
10040 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10042 disable_warnings_sp = sp;
10043 ++c_inhibit_evaluation_warnings;
10046 /* Extract another operand. It may be the RHS of this expression
10047 or the LHS of a new, higher priority expression. */
10048 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10049 ? TRUTH_NOT_EXPR : ERROR_MARK);
10050 rhs = cp_parser_simple_cast_expression (parser);
10052 /* Get another operator token. Look up its precedence to avoid
10053 building a useless (immediately popped) stack entry for common
10054 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10055 token = cp_lexer_peek_token (parser->lexer);
10056 lookahead_prec = TOKEN_PRECEDENCE (token);
10057 if (lookahead_prec != PREC_NOT_OPERATOR
10058 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10059 lookahead_prec = PREC_NOT_OPERATOR;
10060 if (lookahead_prec > new_prec)
10062 /* ... and prepare to parse the RHS of the new, higher priority
10063 expression. Since precedence levels on the stack are
10064 monotonically increasing, we do not have to care about
10065 stack overflows. */
10066 *sp = current;
10067 ++sp;
10068 current.lhs = rhs;
10069 current.lhs_type = rhs_type;
10070 current.prec = new_prec;
10071 new_prec = lookahead_prec;
10072 goto get_rhs;
10074 pop:
10075 lookahead_prec = new_prec;
10076 /* If the stack is not empty, we have parsed into LHS the right side
10077 (`4' in the example above) of an expression we had suspended.
10078 We can use the information on the stack to recover the LHS (`3')
10079 from the stack together with the tree code (`MULT_EXPR'), and
10080 the precedence of the higher level subexpression
10081 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10082 which will be used to actually build the additive expression. */
10083 rhs = current.lhs;
10084 rhs_type = current.lhs_type;
10085 --sp;
10086 current = *sp;
10089 /* Undo the disabling of warnings done above. */
10090 if (sp == disable_warnings_sp)
10092 disable_warnings_sp = NULL;
10093 --c_inhibit_evaluation_warnings;
10096 if (warn_logical_not_paren
10097 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10098 && current.lhs_type == TRUTH_NOT_EXPR
10099 /* Avoid warning for !!x == y. */
10100 && (TREE_CODE (current.lhs) != NE_EXPR
10101 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10102 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10103 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10104 /* Avoid warning for !b == y where b is boolean. */
10105 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10106 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10107 != BOOLEAN_TYPE))))
10108 /* Avoid warning for !!b == y where b is boolean. */
10109 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
10110 || TREE_TYPE (current.lhs) == NULL_TREE
10111 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10112 warn_logical_not_parentheses (current.loc, current.tree_type,
10113 current.lhs, maybe_constant_value (rhs));
10115 overload = NULL;
10117 location_t combined_loc = make_location (current.loc,
10118 current.lhs.get_start (),
10119 rhs.get_finish ());
10121 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10122 ERROR_MARK for everything that is not a binary expression.
10123 This makes warn_about_parentheses miss some warnings that
10124 involve unary operators. For unary expressions we should
10125 pass the correct tree_code unless the unary expression was
10126 surrounded by parentheses.
10128 if (no_toplevel_fold_p
10129 && lookahead_prec <= current.prec
10130 && sp == stack)
10132 if (current.lhs == error_mark_node || rhs == error_mark_node)
10133 current.lhs = error_mark_node;
10134 else
10136 current.lhs.maybe_add_location_wrapper ();
10137 rhs.maybe_add_location_wrapper ();
10138 current.lhs
10139 = build_min (current.tree_type,
10140 TREE_CODE_CLASS (current.tree_type)
10141 == tcc_comparison
10142 ? boolean_type_node : TREE_TYPE (current.lhs),
10143 current.lhs.get_value (), rhs.get_value ());
10144 SET_EXPR_LOCATION (current.lhs, combined_loc);
10147 else
10149 op_location_t op_loc (current.loc, combined_loc);
10150 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10151 current.lhs, current.lhs_type,
10152 rhs, rhs_type, NULL_TREE, &overload,
10153 complain_flags (decltype_p));
10154 /* TODO: build_x_binary_op doesn't always honor the location. */
10155 current.lhs.set_location (combined_loc);
10157 current.lhs_type = current.tree_type;
10159 /* If the binary operator required the use of an overloaded operator,
10160 then this expression cannot be an integral constant-expression.
10161 An overloaded operator can be used even if both operands are
10162 otherwise permissible in an integral constant-expression if at
10163 least one of the operands is of enumeration type. */
10165 if (overload
10166 && cp_parser_non_integral_constant_expression (parser,
10167 NIC_OVERLOADED))
10168 return error_mark_node;
10171 return current.lhs;
10174 static cp_expr
10175 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10176 bool no_toplevel_fold_p,
10177 enum cp_parser_prec prec,
10178 cp_id_kind * pidk)
10180 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10181 /*decltype*/false, prec, pidk);
10184 /* Parse the `? expression : assignment-expression' part of a
10185 conditional-expression. The LOGICAL_OR_EXPR is the
10186 logical-or-expression that started the conditional-expression.
10187 Returns a representation of the entire conditional-expression.
10189 This routine is used by cp_parser_assignment_expression.
10191 ? expression : assignment-expression
10193 GNU Extensions:
10195 ? : assignment-expression */
10197 static tree
10198 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10200 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10201 cp_expr assignment_expr;
10202 struct cp_token *token;
10203 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10205 /* Consume the `?' token. */
10206 cp_lexer_consume_token (parser->lexer);
10207 token = cp_lexer_peek_token (parser->lexer);
10208 if (cp_parser_allow_gnu_extensions_p (parser)
10209 && token->type == CPP_COLON)
10211 pedwarn (token->location, OPT_Wpedantic,
10212 "ISO C++ does not allow %<?:%> with omitted middle operand");
10213 /* Implicit true clause. */
10214 expr = NULL_TREE;
10215 c_inhibit_evaluation_warnings +=
10216 folded_logical_or_expr == truthvalue_true_node;
10217 warn_for_omitted_condop (token->location, logical_or_expr);
10219 else
10221 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10222 parser->colon_corrects_to_scope_p = false;
10223 /* Parse the expression. */
10224 c_inhibit_evaluation_warnings +=
10225 folded_logical_or_expr == truthvalue_false_node;
10226 expr = cp_parser_expression (parser);
10227 c_inhibit_evaluation_warnings +=
10228 ((folded_logical_or_expr == truthvalue_true_node)
10229 - (folded_logical_or_expr == truthvalue_false_node));
10230 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10233 /* The next token should be a `:'. */
10234 cp_parser_require (parser, CPP_COLON, RT_COLON);
10235 /* Parse the assignment-expression. */
10236 assignment_expr = cp_parser_assignment_expression (parser);
10237 c_inhibit_evaluation_warnings -=
10238 folded_logical_or_expr == truthvalue_true_node;
10240 /* Make a location:
10241 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10242 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10243 with the caret at the "?", ranging from the start of
10244 the logical_or_expr to the end of the assignment_expr. */
10245 loc = make_location (loc,
10246 logical_or_expr.get_start (),
10247 assignment_expr.get_finish ());
10249 /* Build the conditional-expression. */
10250 return build_x_conditional_expr (loc, logical_or_expr,
10251 expr,
10252 assignment_expr,
10253 tf_warning_or_error);
10256 /* Parse an assignment-expression.
10258 assignment-expression:
10259 conditional-expression
10260 logical-or-expression assignment-operator assignment_expression
10261 throw-expression
10262 yield-expression
10264 CAST_P is true if this expression is the target of a cast.
10265 DECLTYPE_P is true if this expression is the operand of decltype.
10267 Returns a representation for the expression. */
10269 static cp_expr
10270 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10271 bool cast_p, bool decltype_p)
10273 cp_expr expr;
10275 /* If the next token is the `throw' keyword, then we're looking at
10276 a throw-expression. */
10277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10278 expr = cp_parser_throw_expression (parser);
10279 /* If the next token is the `co_yield' keyword, then we're looking at
10280 a yield-expression. */
10281 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10282 expr = cp_parser_yield_expression (parser);
10283 /* Otherwise, it must be that we are looking at a
10284 logical-or-expression. */
10285 else
10287 /* Parse the binary expressions (logical-or-expression). */
10288 expr = cp_parser_binary_expression (parser, cast_p, false,
10289 decltype_p,
10290 PREC_NOT_OPERATOR, pidk);
10291 /* If the next token is a `?' then we're actually looking at a
10292 conditional-expression. */
10293 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10294 return cp_parser_question_colon_clause (parser, expr);
10295 else
10297 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10299 /* If it's an assignment-operator, we're using the second
10300 production. */
10301 enum tree_code assignment_operator
10302 = cp_parser_assignment_operator_opt (parser);
10303 if (assignment_operator != ERROR_MARK)
10305 bool non_constant_p;
10307 /* Parse the right-hand side of the assignment. */
10308 cp_expr rhs = cp_parser_initializer_clause (parser,
10309 &non_constant_p);
10311 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10312 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10314 /* An assignment may not appear in a
10315 constant-expression. */
10316 if (cp_parser_non_integral_constant_expression (parser,
10317 NIC_ASSIGNMENT))
10318 return error_mark_node;
10319 /* Build the assignment expression. Its default
10320 location:
10321 LHS = RHS
10322 ~~~~^~~~~
10323 is the location of the '=' token as the
10324 caret, ranging from the start of the lhs to the
10325 end of the rhs. */
10326 loc = make_location (loc,
10327 expr.get_start (),
10328 rhs.get_finish ());
10329 expr = build_x_modify_expr (loc, expr,
10330 assignment_operator,
10331 rhs, NULL_TREE,
10332 complain_flags (decltype_p));
10333 /* TODO: build_x_modify_expr doesn't honor the location,
10334 so we must set it here. */
10335 expr.set_location (loc);
10340 return expr;
10343 /* Parse an (optional) assignment-operator.
10345 assignment-operator: one of
10346 = *= /= %= += -= >>= <<= &= ^= |=
10348 GNU Extension:
10350 assignment-operator: one of
10351 <?= >?=
10353 If the next token is an assignment operator, the corresponding tree
10354 code is returned, and the token is consumed. For example, for
10355 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10356 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10357 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10358 operator, ERROR_MARK is returned. */
10360 static enum tree_code
10361 cp_parser_assignment_operator_opt (cp_parser* parser)
10363 enum tree_code op;
10364 cp_token *token;
10366 /* Peek at the next token. */
10367 token = cp_lexer_peek_token (parser->lexer);
10369 switch (token->type)
10371 case CPP_EQ:
10372 op = NOP_EXPR;
10373 break;
10375 case CPP_MULT_EQ:
10376 op = MULT_EXPR;
10377 break;
10379 case CPP_DIV_EQ:
10380 op = TRUNC_DIV_EXPR;
10381 break;
10383 case CPP_MOD_EQ:
10384 op = TRUNC_MOD_EXPR;
10385 break;
10387 case CPP_PLUS_EQ:
10388 op = PLUS_EXPR;
10389 break;
10391 case CPP_MINUS_EQ:
10392 op = MINUS_EXPR;
10393 break;
10395 case CPP_RSHIFT_EQ:
10396 op = RSHIFT_EXPR;
10397 break;
10399 case CPP_LSHIFT_EQ:
10400 op = LSHIFT_EXPR;
10401 break;
10403 case CPP_AND_EQ:
10404 op = BIT_AND_EXPR;
10405 break;
10407 case CPP_XOR_EQ:
10408 op = BIT_XOR_EXPR;
10409 break;
10411 case CPP_OR_EQ:
10412 op = BIT_IOR_EXPR;
10413 break;
10415 default:
10416 /* Nothing else is an assignment operator. */
10417 op = ERROR_MARK;
10420 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10421 if (op != ERROR_MARK
10422 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10423 op = ERROR_MARK;
10425 /* If it was an assignment operator, consume it. */
10426 if (op != ERROR_MARK)
10427 cp_lexer_consume_token (parser->lexer);
10429 return op;
10432 /* Parse an expression.
10434 expression:
10435 assignment-expression
10436 expression , assignment-expression
10438 CAST_P is true if this expression is the target of a cast.
10439 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10440 except possibly parenthesized or on the RHS of a comma (N3276).
10441 WARN_COMMA_P is true if a comma should be diagnosed.
10443 Returns a representation of the expression. */
10445 static cp_expr
10446 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10447 bool cast_p, bool decltype_p, bool warn_comma_p)
10449 cp_expr expression = NULL_TREE;
10450 location_t loc = UNKNOWN_LOCATION;
10452 while (true)
10454 cp_expr assignment_expression;
10456 /* Parse the next assignment-expression. */
10457 assignment_expression
10458 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10460 /* We don't create a temporary for a call that is the immediate operand
10461 of decltype or on the RHS of a comma. But when we see a comma, we
10462 need to create a temporary for a call on the LHS. */
10463 if (decltype_p && !processing_template_decl
10464 && TREE_CODE (assignment_expression) == CALL_EXPR
10465 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10466 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10467 assignment_expression
10468 = build_cplus_new (TREE_TYPE (assignment_expression),
10469 assignment_expression, tf_warning_or_error);
10471 /* If this is the first assignment-expression, we can just
10472 save it away. */
10473 if (!expression)
10474 expression = assignment_expression;
10475 else
10477 /* Create a location with caret at the comma, ranging
10478 from the start of the LHS to the end of the RHS. */
10479 loc = make_location (loc,
10480 expression.get_start (),
10481 assignment_expression.get_finish ());
10482 expression = build_x_compound_expr (loc, expression,
10483 assignment_expression, NULL_TREE,
10484 complain_flags (decltype_p));
10485 expression.set_location (loc);
10487 /* If the next token is not a comma, or we're in a fold-expression, then
10488 we are done with the expression. */
10489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10490 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10491 break;
10492 /* Consume the `,'. */
10493 loc = cp_lexer_peek_token (parser->lexer)->location;
10494 if (warn_comma_p)
10496 /* [depr.comma.subscript]: A comma expression appearing as
10497 the expr-or-braced-init-list of a subscripting expression
10498 is deprecated. A parenthesized comma expression is not
10499 deprecated. */
10500 warning_at (loc, OPT_Wcomma_subscript,
10501 "top-level comma expression in array subscript "
10502 "is deprecated");
10503 warn_comma_p = false;
10505 cp_lexer_consume_token (parser->lexer);
10506 /* A comma operator cannot appear in a constant-expression. */
10507 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10508 expression = error_mark_node;
10511 return expression;
10514 /* Parse a constant-expression.
10516 constant-expression:
10517 conditional-expression
10519 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10520 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10521 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10522 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10523 greater than 1, this isn't really a constant-expression, only a
10524 potentially constant-evaluated expression. If STRICT_P is true,
10525 only parse a conditional-expression, otherwise parse an
10526 assignment-expression. See below for rationale. */
10528 static cp_expr
10529 cp_parser_constant_expression (cp_parser* parser,
10530 int allow_non_constant_p,
10531 bool *non_constant_p,
10532 bool strict_p)
10534 bool saved_integral_constant_expression_p;
10535 bool saved_allow_non_integral_constant_expression_p;
10536 bool saved_non_integral_constant_expression_p;
10537 cp_expr expression;
10539 /* It might seem that we could simply parse the
10540 conditional-expression, and then check to see if it were
10541 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10542 one that the compiler can figure out is constant, possibly after
10543 doing some simplifications or optimizations. The standard has a
10544 precise definition of constant-expression, and we must honor
10545 that, even though it is somewhat more restrictive.
10547 For example:
10549 int i[(2, 3)];
10551 is not a legal declaration, because `(2, 3)' is not a
10552 constant-expression. The `,' operator is forbidden in a
10553 constant-expression. However, GCC's constant-folding machinery
10554 will fold this operation to an INTEGER_CST for `3'. */
10556 /* Save the old settings. */
10557 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10558 saved_allow_non_integral_constant_expression_p
10559 = parser->allow_non_integral_constant_expression_p;
10560 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10561 /* We are now parsing a constant-expression. */
10562 parser->integral_constant_expression_p = true;
10563 parser->allow_non_integral_constant_expression_p
10564 = (allow_non_constant_p || cxx_dialect >= cxx11);
10565 parser->non_integral_constant_expression_p = false;
10567 /* A manifestly constant-evaluated expression is evaluated even in an
10568 unevaluated operand. */
10569 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
10571 /* Although the grammar says "conditional-expression", when not STRICT_P,
10572 we parse an "assignment-expression", which also permits
10573 "throw-expression" and the use of assignment operators. In the case
10574 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10575 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10576 actually essential that we look for an assignment-expression.
10577 For example, cp_parser_initializer_clauses uses this function to
10578 determine whether a particular assignment-expression is in fact
10579 constant. */
10580 if (strict_p)
10582 /* Parse the binary expressions (logical-or-expression). */
10583 expression = cp_parser_binary_expression (parser, false, false, false,
10584 PREC_NOT_OPERATOR, NULL);
10585 /* If the next token is a `?' then we're actually looking at
10586 a conditional-expression; otherwise we're done. */
10587 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10588 expression = cp_parser_question_colon_clause (parser, expression);
10590 else
10591 expression = cp_parser_assignment_expression (parser);
10592 /* Restore the old settings. */
10593 parser->integral_constant_expression_p
10594 = saved_integral_constant_expression_p;
10595 parser->allow_non_integral_constant_expression_p
10596 = saved_allow_non_integral_constant_expression_p;
10597 if (cxx_dialect >= cxx11)
10599 /* Require an rvalue constant expression here; that's what our
10600 callers expect. Reference constant expressions are handled
10601 separately in e.g. cp_parser_template_argument. */
10602 tree decay = expression;
10603 if (TREE_TYPE (expression)
10604 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10605 decay = build_address (expression);
10606 bool is_const = is_rvalue_constant_expression (decay);
10607 parser->non_integral_constant_expression_p = !is_const;
10608 if (!is_const && !allow_non_constant_p)
10609 require_rvalue_constant_expression (decay);
10611 if (allow_non_constant_p)
10612 *non_constant_p = parser->non_integral_constant_expression_p;
10613 parser->non_integral_constant_expression_p
10614 = saved_non_integral_constant_expression_p;
10616 return expression;
10619 /* Parse __builtin_offsetof.
10621 offsetof-expression:
10622 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10624 offsetof-member-designator:
10625 id-expression
10626 | offsetof-member-designator "." id-expression
10627 | offsetof-member-designator "[" expression "]"
10628 | offsetof-member-designator "->" id-expression */
10630 static cp_expr
10631 cp_parser_builtin_offsetof (cp_parser *parser)
10633 int save_ice_p, save_non_ice_p;
10634 tree type;
10635 cp_expr expr;
10636 cp_id_kind dummy;
10637 cp_token *token;
10638 location_t finish_loc;
10640 /* We're about to accept non-integral-constant things, but will
10641 definitely yield an integral constant expression. Save and
10642 restore these values around our local parsing. */
10643 save_ice_p = parser->integral_constant_expression_p;
10644 save_non_ice_p = parser->non_integral_constant_expression_p;
10646 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10648 /* Consume the "__builtin_offsetof" token. */
10649 cp_lexer_consume_token (parser->lexer);
10650 /* Consume the opening `('. */
10651 matching_parens parens;
10652 parens.require_open (parser);
10653 /* Parse the type-id. */
10654 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10656 const char *saved_message = parser->type_definition_forbidden_message;
10657 parser->type_definition_forbidden_message
10658 = G_("types may not be defined within %<__builtin_offsetof%>");
10659 type = cp_parser_type_id (parser);
10660 parser->type_definition_forbidden_message = saved_message;
10662 /* Look for the `,'. */
10663 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10664 token = cp_lexer_peek_token (parser->lexer);
10666 /* Build the (type *)null that begins the traditional offsetof macro. */
10667 tree object_ptr
10668 = build_static_cast (input_location, build_pointer_type (type),
10669 null_pointer_node, tf_warning_or_error);
10671 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10672 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10673 true, &dummy, token->location);
10674 while (true)
10676 token = cp_lexer_peek_token (parser->lexer);
10677 switch (token->type)
10679 case CPP_OPEN_SQUARE:
10680 /* offsetof-member-designator "[" expression "]" */
10681 expr = cp_parser_postfix_open_square_expression (parser, expr,
10682 true, false);
10683 break;
10685 case CPP_DEREF:
10686 /* offsetof-member-designator "->" identifier */
10687 expr = grok_array_decl (token->location, expr, integer_zero_node,
10688 NULL, tf_warning_or_error);
10689 /* FALLTHRU */
10691 case CPP_DOT:
10692 /* offsetof-member-designator "." identifier */
10693 cp_lexer_consume_token (parser->lexer);
10694 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10695 expr, true, &dummy,
10696 token->location);
10697 break;
10699 case CPP_CLOSE_PAREN:
10700 /* Consume the ")" token. */
10701 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10702 cp_lexer_consume_token (parser->lexer);
10703 goto success;
10705 default:
10706 /* Error. We know the following require will fail, but
10707 that gives the proper error message. */
10708 parens.require_close (parser);
10709 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10710 expr = error_mark_node;
10711 goto failure;
10715 success:
10716 /* Make a location of the form:
10717 __builtin_offsetof (struct s, f)
10718 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10719 with caret at the type-id, ranging from the start of the
10720 "_builtin_offsetof" token to the close paren. */
10721 loc = make_location (loc, start_loc, finish_loc);
10722 /* The result will be an INTEGER_CST, so we need to explicitly
10723 preserve the location. */
10724 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10726 failure:
10727 parser->integral_constant_expression_p = save_ice_p;
10728 parser->non_integral_constant_expression_p = save_non_ice_p;
10730 expr = expr.maybe_add_location_wrapper ();
10731 return expr;
10734 /* Parse a trait expression.
10736 Returns a representation of the expression, the underlying type
10737 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10739 static cp_expr
10740 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10742 cp_trait_kind kind;
10743 tree type1, type2 = NULL_TREE;
10744 bool binary = false;
10745 bool variadic = false;
10747 switch (keyword)
10749 case RID_HAS_NOTHROW_ASSIGN:
10750 kind = CPTK_HAS_NOTHROW_ASSIGN;
10751 break;
10752 case RID_HAS_NOTHROW_CONSTRUCTOR:
10753 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10754 break;
10755 case RID_HAS_NOTHROW_COPY:
10756 kind = CPTK_HAS_NOTHROW_COPY;
10757 break;
10758 case RID_HAS_TRIVIAL_ASSIGN:
10759 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10760 break;
10761 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10762 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10763 break;
10764 case RID_HAS_TRIVIAL_COPY:
10765 kind = CPTK_HAS_TRIVIAL_COPY;
10766 break;
10767 case RID_HAS_TRIVIAL_DESTRUCTOR:
10768 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10769 break;
10770 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10771 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10772 break;
10773 case RID_HAS_VIRTUAL_DESTRUCTOR:
10774 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10775 break;
10776 case RID_IS_ABSTRACT:
10777 kind = CPTK_IS_ABSTRACT;
10778 break;
10779 case RID_IS_AGGREGATE:
10780 kind = CPTK_IS_AGGREGATE;
10781 break;
10782 case RID_IS_BASE_OF:
10783 kind = CPTK_IS_BASE_OF;
10784 binary = true;
10785 break;
10786 case RID_IS_CLASS:
10787 kind = CPTK_IS_CLASS;
10788 break;
10789 case RID_IS_EMPTY:
10790 kind = CPTK_IS_EMPTY;
10791 break;
10792 case RID_IS_ENUM:
10793 kind = CPTK_IS_ENUM;
10794 break;
10795 case RID_IS_FINAL:
10796 kind = CPTK_IS_FINAL;
10797 break;
10798 case RID_IS_LAYOUT_COMPATIBLE:
10799 kind = CPTK_IS_LAYOUT_COMPATIBLE;
10800 binary = true;
10801 break;
10802 case RID_IS_LITERAL_TYPE:
10803 kind = CPTK_IS_LITERAL_TYPE;
10804 break;
10805 case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
10806 kind = CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF;
10807 binary = true;
10808 break;
10809 case RID_IS_POD:
10810 kind = CPTK_IS_POD;
10811 break;
10812 case RID_IS_POLYMORPHIC:
10813 kind = CPTK_IS_POLYMORPHIC;
10814 break;
10815 case RID_IS_SAME_AS:
10816 kind = CPTK_IS_SAME_AS;
10817 binary = true;
10818 break;
10819 case RID_IS_STD_LAYOUT:
10820 kind = CPTK_IS_STD_LAYOUT;
10821 break;
10822 case RID_IS_TRIVIAL:
10823 kind = CPTK_IS_TRIVIAL;
10824 break;
10825 case RID_IS_TRIVIALLY_ASSIGNABLE:
10826 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10827 binary = true;
10828 break;
10829 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10830 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10831 variadic = true;
10832 break;
10833 case RID_IS_TRIVIALLY_COPYABLE:
10834 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10835 break;
10836 case RID_IS_UNION:
10837 kind = CPTK_IS_UNION;
10838 break;
10839 case RID_UNDERLYING_TYPE:
10840 kind = CPTK_UNDERLYING_TYPE;
10841 break;
10842 case RID_BASES:
10843 kind = CPTK_BASES;
10844 break;
10845 case RID_DIRECT_BASES:
10846 kind = CPTK_DIRECT_BASES;
10847 break;
10848 case RID_IS_ASSIGNABLE:
10849 kind = CPTK_IS_ASSIGNABLE;
10850 binary = true;
10851 break;
10852 case RID_IS_CONSTRUCTIBLE:
10853 kind = CPTK_IS_CONSTRUCTIBLE;
10854 variadic = true;
10855 break;
10856 case RID_IS_NOTHROW_ASSIGNABLE:
10857 kind = CPTK_IS_NOTHROW_ASSIGNABLE;
10858 binary = true;
10859 break;
10860 case RID_IS_NOTHROW_CONSTRUCTIBLE:
10861 kind = CPTK_IS_NOTHROW_CONSTRUCTIBLE;
10862 variadic = true;
10863 break;
10864 default:
10865 gcc_unreachable ();
10868 /* Get location of initial token. */
10869 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10871 /* Consume the token. */
10872 cp_lexer_consume_token (parser->lexer);
10874 matching_parens parens;
10875 parens.require_open (parser);
10878 type_id_in_expr_sentinel s (parser);
10879 type1 = cp_parser_type_id (parser);
10882 if (type1 == error_mark_node)
10883 return error_mark_node;
10885 if (binary)
10887 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10890 type_id_in_expr_sentinel s (parser);
10891 type2 = cp_parser_type_id (parser);
10894 if (type2 == error_mark_node)
10895 return error_mark_node;
10897 else if (variadic)
10899 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10901 cp_lexer_consume_token (parser->lexer);
10902 tree elt = cp_parser_type_id (parser);
10903 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10905 cp_lexer_consume_token (parser->lexer);
10906 elt = make_pack_expansion (elt);
10908 if (elt == error_mark_node)
10909 return error_mark_node;
10910 type2 = tree_cons (NULL_TREE, elt, type2);
10912 type2 = nreverse (type2);
10915 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10916 parens.require_close (parser);
10918 /* Construct a location of the form:
10919 __is_trivially_copyable(_Tp)
10920 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10921 with start == caret, finishing at the close-paren. */
10922 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10924 /* Complete the trait expression, which may mean either processing
10925 the trait expr now or saving it for template instantiation. */
10926 switch (kind)
10928 case CPTK_UNDERLYING_TYPE:
10929 return cp_expr (finish_underlying_type (type1), trait_loc);
10930 case CPTK_BASES:
10931 return cp_expr (finish_bases (type1, false), trait_loc);
10932 case CPTK_DIRECT_BASES:
10933 return cp_expr (finish_bases (type1, true), trait_loc);
10934 default:
10935 return finish_trait_expr (trait_loc, kind, type1, type2);
10939 /* Parse a lambda expression.
10941 lambda-expression:
10942 lambda-introducer lambda-declarator [opt] compound-statement
10943 lambda-introducer < template-parameter-list > requires-clause [opt]
10944 lambda-declarator [opt] compound-statement
10946 Returns a representation of the expression. */
10948 static cp_expr
10949 cp_parser_lambda_expression (cp_parser* parser)
10951 tree lambda_expr = build_lambda_expr ();
10952 tree type;
10953 bool ok = true;
10954 cp_token *token = cp_lexer_peek_token (parser->lexer);
10955 cp_token_position start = 0;
10957 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10959 if (cxx_dialect >= cxx20)
10961 /* C++20 allows lambdas in unevaluated context, but one in the type of a
10962 non-type parameter is nonsensical.
10964 Distinguish a lambda in the parameter type from a lambda in the
10965 default argument by looking at local_variables_forbidden_p, which is
10966 only set in default arguments. */
10967 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
10969 error_at (token->location,
10970 "lambda-expression in template parameter type");
10971 token->error_reported = true;
10972 ok = false;
10975 else if (cp_unevaluated_operand)
10977 if (!token->error_reported)
10979 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10980 "lambda-expression in unevaluated context"
10981 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10982 token->error_reported = true;
10984 ok = false;
10986 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10988 if (!token->error_reported)
10990 error_at (token->location, "lambda-expression in template-argument"
10991 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10992 token->error_reported = true;
10994 ok = false;
10997 /* We may be in the middle of deferred access check. Disable
10998 it now. */
10999 push_deferring_access_checks (dk_no_deferred);
11001 cp_parser_lambda_introducer (parser, lambda_expr);
11002 if (cp_parser_error_occurred (parser))
11003 return error_mark_node;
11005 type = begin_lambda_type (lambda_expr);
11006 if (type == error_mark_node)
11007 return error_mark_node;
11009 record_lambda_scope (lambda_expr);
11011 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11012 determine_visibility (TYPE_NAME (type));
11014 /* Now that we've started the type, add the capture fields for any
11015 explicit captures. */
11016 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11019 /* Inside the class, surrounding template-parameter-lists do not apply. */
11020 unsigned int saved_num_template_parameter_lists
11021 = parser->num_template_parameter_lists;
11022 unsigned char in_statement = parser->in_statement;
11023 bool in_switch_statement_p = parser->in_switch_statement_p;
11024 bool fully_implicit_function_template_p
11025 = parser->fully_implicit_function_template_p;
11026 tree implicit_template_parms = parser->implicit_template_parms;
11027 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11028 bool auto_is_implicit_function_template_parm_p
11029 = parser->auto_is_implicit_function_template_parm_p;
11031 parser->num_template_parameter_lists = 0;
11032 parser->in_statement = 0;
11033 parser->in_switch_statement_p = false;
11034 parser->fully_implicit_function_template_p = false;
11035 parser->implicit_template_parms = 0;
11036 parser->implicit_template_scope = 0;
11037 parser->auto_is_implicit_function_template_parm_p = false;
11039 /* The body of a lambda in a discarded statement is not discarded. */
11040 bool discarded = in_discarded_stmt;
11041 in_discarded_stmt = 0;
11043 /* Similarly the body of a lambda in immediate function context is not
11044 in immediate function context. */
11045 bool save_in_consteval_if_p = in_consteval_if_p;
11046 in_consteval_if_p = false;
11048 /* By virtue of defining a local class, a lambda expression has access to
11049 the private variables of enclosing classes. */
11051 if (cp_parser_start_tentative_firewall (parser))
11052 start = token;
11054 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11056 if (ok && cp_parser_error_occurred (parser))
11057 ok = false;
11059 if (ok)
11061 cp_parser_lambda_body (parser, lambda_expr);
11063 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11065 if (cp_parser_skip_to_closing_brace (parser))
11066 cp_lexer_consume_token (parser->lexer);
11069 /* The capture list was built up in reverse order; fix that now. */
11070 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11071 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11073 if (ok)
11074 maybe_add_lambda_conv_op (type);
11076 finish_struct (type, /*attributes=*/NULL_TREE);
11078 in_consteval_if_p = save_in_consteval_if_p;
11079 in_discarded_stmt = discarded;
11081 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11082 parser->in_statement = in_statement;
11083 parser->in_switch_statement_p = in_switch_statement_p;
11084 parser->fully_implicit_function_template_p
11085 = fully_implicit_function_template_p;
11086 parser->implicit_template_parms = implicit_template_parms;
11087 parser->implicit_template_scope = implicit_template_scope;
11088 parser->auto_is_implicit_function_template_parm_p
11089 = auto_is_implicit_function_template_parm_p;
11092 /* This field is only used during parsing of the lambda. */
11093 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11095 /* This lambda shouldn't have any proxies left at this point. */
11096 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11097 /* And now that we're done, push proxies for an enclosing lambda. */
11098 insert_pending_capture_proxies ();
11100 /* Update the lambda expression to a range. */
11101 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11102 token->location,
11103 parser->lexer);
11105 if (ok)
11106 lambda_expr = build_lambda_object (lambda_expr);
11107 else
11108 lambda_expr = error_mark_node;
11110 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11112 pop_deferring_access_checks ();
11114 return lambda_expr;
11117 /* Parse the beginning of a lambda expression.
11119 lambda-introducer:
11120 [ lambda-capture [opt] ]
11122 LAMBDA_EXPR is the current representation of the lambda expression. */
11124 static void
11125 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11127 /* Need commas after the first capture. */
11128 bool first = true;
11130 /* Eat the leading `['. */
11131 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11133 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11134 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11135 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11136 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11137 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11138 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11139 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11140 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11142 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11144 cp_lexer_consume_token (parser->lexer);
11145 first = false;
11147 if (!(at_function_scope_p () || parsing_nsdmi ()))
11148 error ("non-local lambda expression cannot have a capture-default");
11151 hash_set<tree, true> ids;
11152 tree first_capture_id = NULL_TREE;
11153 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11155 cp_token* capture_token;
11156 tree capture_id;
11157 tree capture_init_expr;
11158 cp_id_kind idk = CP_ID_KIND_NONE;
11159 bool explicit_init_p = false;
11161 enum capture_kind_type
11163 BY_COPY,
11164 BY_REFERENCE
11166 enum capture_kind_type capture_kind = BY_COPY;
11168 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11170 error ("expected end of capture-list");
11171 return;
11174 if (first)
11175 first = false;
11176 else
11177 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11179 /* Possibly capture `this'. */
11180 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11182 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11183 if (cxx_dialect < cxx20 && pedantic
11184 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11185 pedwarn (loc, OPT_Wc__20_extensions,
11186 "explicit by-copy capture of %<this%> "
11187 "with by-copy capture default only available with "
11188 "%<-std=c++20%> or %<-std=gnu++20%>");
11189 cp_lexer_consume_token (parser->lexer);
11190 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11191 pedwarn (input_location, 0,
11192 "already captured %qD in lambda expression",
11193 this_identifier);
11194 else
11195 add_capture (lambda_expr, /*id=*/this_identifier,
11196 /*initializer=*/finish_this_expr (),
11197 /*by_reference_p=*/true, explicit_init_p);
11198 continue;
11201 /* Possibly capture `*this'. */
11202 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11203 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11205 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11206 if (cxx_dialect < cxx17)
11207 pedwarn (loc, OPT_Wc__17_extensions,
11208 "%<*this%> capture only available with "
11209 "%<-std=c++17%> or %<-std=gnu++17%>");
11210 cp_lexer_consume_token (parser->lexer);
11211 cp_lexer_consume_token (parser->lexer);
11212 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11213 pedwarn (input_location, 0,
11214 "already captured %qD in lambda expression",
11215 this_identifier);
11216 else
11217 add_capture (lambda_expr, /*id=*/this_identifier,
11218 /*initializer=*/finish_this_expr (),
11219 /*by_reference_p=*/false, explicit_init_p);
11220 continue;
11223 /* But reject `&this'. */
11224 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11225 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11227 error_at (cp_lexer_peek_token (parser->lexer)->location,
11228 "%<this%> cannot be captured by reference");
11229 cp_lexer_consume_token (parser->lexer);
11230 cp_lexer_consume_token (parser->lexer);
11231 continue;
11234 /* Remember whether we want to capture as a reference or not. */
11235 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11237 capture_kind = BY_REFERENCE;
11238 cp_lexer_consume_token (parser->lexer);
11241 bool init_pack_expansion = false;
11242 location_t ellipsis_loc = UNKNOWN_LOCATION;
11243 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11245 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11246 if (cxx_dialect < cxx20)
11247 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11248 "pack init-capture only available with "
11249 "%<-std=c++20%> or %<-std=gnu++20%>");
11250 cp_lexer_consume_token (parser->lexer);
11251 init_pack_expansion = true;
11254 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11255 if (init_pack_expansion && capture_kind != BY_REFERENCE
11256 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11258 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11259 0, "%<&%> should come before %<...%>");
11260 capture_kind = BY_REFERENCE;
11261 cp_lexer_consume_token (parser->lexer);
11264 /* Get the identifier. */
11265 capture_token = cp_lexer_peek_token (parser->lexer);
11266 capture_id = cp_parser_identifier (parser);
11268 if (capture_id == error_mark_node)
11269 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11270 delimiters, but I modified this to stop on unnested ']' as well. It
11271 was already changed to stop on unnested '}', so the
11272 "closing_parenthesis" name is no more misleading with my change. */
11274 cp_parser_skip_to_closing_parenthesis (parser,
11275 /*recovering=*/true,
11276 /*or_comma=*/true,
11277 /*consume_paren=*/true);
11278 break;
11281 /* Find the initializer for this capture. */
11282 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11283 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11284 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11286 bool direct, non_constant;
11287 /* An explicit initializer exists. */
11288 if (cxx_dialect < cxx14)
11289 pedwarn (input_location, OPT_Wc__14_extensions,
11290 "lambda capture initializers "
11291 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11292 capture_init_expr = cp_parser_initializer (parser, &direct,
11293 &non_constant, true);
11294 explicit_init_p = true;
11295 if (capture_init_expr == NULL_TREE)
11297 error ("empty initializer for lambda init-capture");
11298 capture_init_expr = error_mark_node;
11300 if (init_pack_expansion)
11301 capture_init_expr = make_pack_expansion (capture_init_expr);
11303 else
11305 const char* error_msg;
11307 /* Turn the identifier into an id-expression. */
11308 capture_init_expr
11309 = cp_parser_lookup_name_simple (parser, capture_id,
11310 capture_token->location);
11312 if (capture_init_expr == error_mark_node)
11314 unqualified_name_lookup_error (capture_id);
11315 continue;
11317 else if (!VAR_P (capture_init_expr)
11318 && TREE_CODE (capture_init_expr) != PARM_DECL)
11320 error_at (capture_token->location,
11321 "capture of non-variable %qE",
11322 capture_init_expr);
11323 if (DECL_P (capture_init_expr))
11324 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11325 "%q#D declared here", capture_init_expr);
11326 continue;
11328 if (VAR_P (capture_init_expr)
11329 && decl_storage_duration (capture_init_expr) != dk_auto)
11331 if (pedwarn (capture_token->location, 0, "capture of variable "
11332 "%qD with non-automatic storage duration",
11333 capture_init_expr))
11334 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11335 "%q#D declared here", capture_init_expr);
11336 continue;
11339 capture_init_expr
11340 = finish_id_expression
11341 (capture_id,
11342 capture_init_expr,
11343 parser->scope,
11344 &idk,
11345 /*integral_constant_expression_p=*/false,
11346 /*allow_non_integral_constant_expression_p=*/false,
11347 /*non_integral_constant_expression_p=*/NULL,
11348 /*template_p=*/false,
11349 /*done=*/true,
11350 /*address_p=*/false,
11351 /*template_arg_p=*/false,
11352 &error_msg,
11353 capture_token->location);
11355 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11357 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11358 cp_lexer_consume_token (parser->lexer);
11359 capture_init_expr = make_pack_expansion (capture_init_expr);
11360 if (init_pack_expansion)
11362 /* If what follows is an initializer, the second '...' is
11363 invalid. But for cases like [...xs...], the first one
11364 is invalid. */
11365 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11366 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11367 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11368 ellipsis_loc = loc;
11369 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11370 continue;
11375 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11376 && !explicit_init_p)
11378 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11379 && capture_kind == BY_COPY)
11380 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11381 "of %qD redundant with by-copy capture default",
11382 capture_id);
11383 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11384 && capture_kind == BY_REFERENCE)
11385 pedwarn (capture_token->location, 0, "explicit by-reference "
11386 "capture of %qD redundant with by-reference capture "
11387 "default", capture_id);
11390 /* Check for duplicates.
11391 Optimize for the zero or one explicit captures cases and only create
11392 the hash_set after adding second capture. */
11393 bool found = false;
11394 if (!ids.is_empty ())
11395 found = ids.add (capture_id);
11396 else if (first_capture_id == NULL_TREE)
11397 first_capture_id = capture_id;
11398 else if (capture_id == first_capture_id)
11399 found = true;
11400 else
11402 ids.add (first_capture_id);
11403 ids.add (capture_id);
11405 if (found)
11406 pedwarn (input_location, 0,
11407 "already captured %qD in lambda expression", capture_id);
11408 else
11409 add_capture (lambda_expr, capture_id, capture_init_expr,
11410 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11411 explicit_init_p);
11413 /* If there is any qualification still in effect, clear it
11414 now; we will be starting fresh with the next capture. */
11415 parser->scope = NULL_TREE;
11416 parser->qualifying_scope = NULL_TREE;
11417 parser->object_scope = NULL_TREE;
11420 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11423 /* Parse the (optional) middle of a lambda expression.
11425 lambda-declarator:
11426 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11427 lambda-specifiers (C++23)
11429 lambda-specifiers:
11430 decl-specifier-seq [opt] noexcept-specifier [opt]
11431 attribute-specifier-seq [opt] trailing-return-type [opt]
11433 LAMBDA_EXPR is the current representation of the lambda expression. */
11435 static bool
11436 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11438 /* 5.1.1.4 of the standard says:
11439 If a lambda-expression does not include a lambda-declarator, it is as if
11440 the lambda-declarator were ().
11441 This means an empty parameter list, no attributes, and no exception
11442 specification. */
11443 tree param_list = void_list_node;
11444 tree std_attrs = NULL_TREE;
11445 tree gnu_attrs = NULL_TREE;
11446 tree exception_spec = NULL_TREE;
11447 tree template_param_list = NULL_TREE;
11448 tree tx_qual = NULL_TREE;
11449 tree return_type = NULL_TREE;
11450 tree trailing_requires_clause = NULL_TREE;
11451 bool has_param_list = false;
11452 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11453 cp_decl_specifier_seq lambda_specs;
11454 clear_decl_specs (&lambda_specs);
11455 /* A lambda op() is const unless explicitly 'mutable'. */
11456 cp_cv_quals quals = TYPE_QUAL_CONST;
11458 /* The template-parameter-list is optional, but must begin with
11459 an opening angle if present. */
11460 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11462 if (cxx_dialect < cxx14)
11463 pedwarn (parser->lexer->next_token->location, OPT_Wc__14_extensions,
11464 "lambda templates are only available with "
11465 "%<-std=c++14%> or %<-std=gnu++14%>");
11466 else if (pedantic && cxx_dialect < cxx20)
11467 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11468 "lambda templates are only available with "
11469 "%<-std=c++20%> or %<-std=gnu++20%>");
11471 cp_lexer_consume_token (parser->lexer);
11473 template_param_list = cp_parser_template_parameter_list (parser);
11474 cp_parser_skip_to_end_of_template_parameter_list (parser);
11476 /* We may have a constrained generic lambda; parse the requires-clause
11477 immediately after the template-parameter-list and combine with any
11478 shorthand constraints present. */
11479 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11480 if (flag_concepts)
11482 tree reqs = get_shorthand_constraints (current_template_parms);
11483 if (dreqs)
11484 reqs = combine_constraint_expressions (reqs, dreqs);
11485 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11488 /* We just processed one more parameter list. */
11489 ++parser->num_template_parameter_lists;
11492 /* Committee discussion supports allowing attributes here. */
11493 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11495 /* The parameter-declaration-clause is optional (unless
11496 template-parameter-list was given), but must begin with an
11497 opening parenthesis if present. */
11498 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11500 bool is_consteval = false;
11501 /* For C++20, before parsing the parameter list check if there is
11502 a consteval specifier in the corresponding decl-specifier-seq. */
11503 if (cxx_dialect >= cxx20)
11505 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
11506 cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
11508 if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
11509 == RID_CONSTEVAL)
11511 is_consteval = true;
11512 break;
11517 matching_parens parens;
11518 parens.consume_open (parser);
11520 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11522 if (is_consteval)
11523 current_binding_level->immediate_fn_ctx_p = true;
11525 /* Parse parameters. */
11526 param_list
11527 = cp_parser_parameter_declaration_clause
11528 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11530 /* Default arguments shall not be specified in the
11531 parameter-declaration-clause of a lambda-declarator. */
11532 if (pedantic && cxx_dialect < cxx14)
11533 for (tree t = param_list; t; t = TREE_CHAIN (t))
11534 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11535 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11536 OPT_Wc__14_extensions,
11537 "default argument specified for lambda parameter");
11539 parens.require_close (parser);
11540 has_param_list = true;
11542 else if (cxx_dialect < cxx23)
11543 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11545 /* In the decl-specifier-seq of the lambda-declarator, each
11546 decl-specifier shall either be mutable or constexpr. */
11547 int declares_class_or_enum;
11548 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11549 cp_parser_decl_specifier_seq (parser,
11550 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11551 &lambda_specs, &declares_class_or_enum);
11553 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11555 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11556 "parameter declaration before lambda declaration "
11557 "specifiers only optional with %<-std=c++2b%> or "
11558 "%<-std=gnu++2b%>");
11559 omitted_parms_loc = UNKNOWN_LOCATION;
11562 if (lambda_specs.storage_class == sc_mutable)
11564 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11565 quals = TYPE_UNQUALIFIED;
11566 if (lambda_specs.conflicting_specifiers_p)
11567 error_at (lambda_specs.locations[ds_storage_class],
11568 "duplicate %<mutable%>");
11571 tx_qual = cp_parser_tx_qualifier_opt (parser);
11572 if (omitted_parms_loc && tx_qual)
11574 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11575 "parameter declaration before lambda transaction "
11576 "qualifier only optional with %<-std=c++2b%> or "
11577 "%<-std=gnu++2b%>");
11578 omitted_parms_loc = UNKNOWN_LOCATION;
11581 /* Parse optional exception specification. */
11582 exception_spec
11583 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11585 if (omitted_parms_loc && exception_spec)
11587 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11588 "parameter declaration before lambda exception "
11589 "specification only optional with %<-std=c++2b%> or "
11590 "%<-std=gnu++2b%>");
11591 omitted_parms_loc = UNKNOWN_LOCATION;
11594 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11595 attributes that appertain to the function type. */
11596 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11597 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11598 else
11599 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11601 /* Parse optional trailing return type. */
11602 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11604 if (omitted_parms_loc)
11605 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11606 "parameter declaration before lambda trailing "
11607 "return type only optional with %<-std=c++2b%> or "
11608 "%<-std=gnu++2b%>");
11609 cp_lexer_consume_token (parser->lexer);
11610 return_type = cp_parser_trailing_type_id (parser);
11613 /* Also allow GNU attributes at the very end of the declaration, the usual
11614 place for GNU attributes. */
11615 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11616 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11618 if (has_param_list)
11620 /* Parse optional trailing requires clause. */
11621 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11623 /* The function parameters must be in scope all the way until after the
11624 trailing-return-type in case of decltype. */
11625 pop_bindings_and_leave_scope ();
11628 /* Create the function call operator.
11630 Messing with declarators like this is no uglier than building up the
11631 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11632 other code. */
11634 cp_decl_specifier_seq return_type_specs;
11635 cp_declarator* declarator;
11636 tree fco;
11637 void *p;
11639 clear_decl_specs (&return_type_specs);
11640 return_type_specs.type = make_auto ();
11642 if (lambda_specs.locations[ds_constexpr])
11644 if (cxx_dialect >= cxx17)
11645 return_type_specs.locations[ds_constexpr]
11646 = lambda_specs.locations[ds_constexpr];
11647 else
11648 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11649 "lambda only available with %<-std=c++17%> or "
11650 "%<-std=gnu++17%>");
11652 if (lambda_specs.locations[ds_consteval])
11653 return_type_specs.locations[ds_consteval]
11654 = lambda_specs.locations[ds_consteval];
11656 p = obstack_alloc (&declarator_obstack, 0);
11658 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11659 LAMBDA_EXPR_LOCATION (lambda_expr));
11661 declarator = make_call_declarator (declarator, param_list, quals,
11662 VIRT_SPEC_UNSPECIFIED,
11663 REF_QUAL_NONE,
11664 tx_qual,
11665 exception_spec,
11666 return_type,
11667 trailing_requires_clause,
11668 UNKNOWN_LOCATION);
11669 declarator->std_attributes = std_attrs;
11671 fco = grokmethod (&return_type_specs,
11672 declarator,
11673 chainon (gnu_attrs, lambda_specs.attributes));
11674 if (fco != error_mark_node)
11676 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11677 DECL_ARTIFICIAL (fco) = 1;
11678 /* Give the object parameter a different name. */
11679 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11680 DECL_SET_LAMBDA_FUNCTION (fco, true);
11682 if (template_param_list)
11684 fco = finish_member_template_decl (fco);
11685 finish_template_decl (template_param_list);
11686 --parser->num_template_parameter_lists;
11688 else if (parser->fully_implicit_function_template_p)
11689 fco = finish_fully_implicit_template (parser, fco);
11691 finish_member_declaration (fco);
11693 obstack_free (&declarator_obstack, p);
11695 return (fco != error_mark_node);
11699 /* Parse the body of a lambda expression, which is simply
11701 compound-statement
11703 but which requires special handling.
11704 LAMBDA_EXPR is the current representation of the lambda expression. */
11706 static void
11707 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11709 bool nested = (current_function_decl != NULL_TREE);
11710 unsigned char local_variables_forbidden_p
11711 = parser->local_variables_forbidden_p;
11712 bool in_function_body = parser->in_function_body;
11714 /* The body of a lambda-expression is not a subexpression of the enclosing
11715 expression. */
11716 cp_evaluated ev;
11718 if (nested)
11719 push_function_context ();
11720 else
11721 /* Still increment function_depth so that we don't GC in the
11722 middle of an expression. */
11723 ++function_depth;
11725 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
11726 auto ord = make_temp_override (parser->oacc_routine, NULL);
11727 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
11728 vec<tree> omp_privatization_save;
11729 save_omp_privatization_clauses (omp_privatization_save);
11730 /* Clear this in case we're in the middle of a default argument. */
11731 parser->local_variables_forbidden_p = 0;
11732 parser->in_function_body = true;
11735 local_specialization_stack s (lss_copy);
11736 tree fco = lambda_function (lambda_expr);
11737 tree body = start_lambda_function (fco, lambda_expr);
11739 /* Originally C++11 required us to peek for 'return expr'; and
11740 process it specially here to deduce the return type. N3638
11741 removed the need for that. */
11742 cp_parser_function_body (parser, false);
11744 finish_lambda_function (body);
11747 restore_omp_privatization_clauses (omp_privatization_save);
11748 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11749 parser->in_function_body = in_function_body;
11750 if (nested)
11751 pop_function_context();
11752 else
11753 --function_depth;
11756 /* Statements [gram.stmt.stmt] */
11758 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11760 static void
11761 add_debug_begin_stmt (location_t loc)
11763 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11764 return;
11765 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11766 /* A concept is never expanded normally. */
11767 return;
11769 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11770 SET_EXPR_LOCATION (stmt, loc);
11771 add_stmt (stmt);
11774 struct cp_omp_attribute_data
11776 cp_token_cache *tokens;
11777 const c_omp_directive *dir;
11778 c_omp_directive_kind kind;
11781 /* Handle omp::directive and omp::sequence attributes in ATTRS
11782 (if any) at the start of a statement or in attribute-declaration. */
11784 static tree
11785 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
11787 if (!flag_openmp && !flag_openmp_simd)
11788 return attrs;
11790 auto_vec<cp_omp_attribute_data, 16> vec;
11791 int cnt = 0;
11792 int tokens = 0;
11793 bool bad = false;
11794 for (tree *pa = &attrs; *pa; )
11795 if (get_attribute_namespace (*pa) == omp_identifier
11796 && is_attribute_p ("directive", get_attribute_name (*pa)))
11798 cnt++;
11799 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
11801 tree d = TREE_VALUE (a);
11802 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
11803 cp_token *first = DEFPARSE_TOKENS (d)->first;
11804 cp_token *last = DEFPARSE_TOKENS (d)->last;
11805 if (parser->omp_attrs_forbidden_p)
11807 error_at (first->location,
11808 "mixing OpenMP directives with attribute and pragma "
11809 "syntax on the same statement");
11810 parser->omp_attrs_forbidden_p = false;
11811 bad = true;
11813 const char *directive[3] = {};
11814 for (int i = 0; i < 3; i++)
11816 tree id = NULL_TREE;
11817 if (first + i == last)
11818 break;
11819 if (first[i].type == CPP_NAME)
11820 id = first[i].u.value;
11821 else if (first[i].type == CPP_KEYWORD)
11822 id = ridpointers[(int) first[i].keyword];
11823 else
11824 break;
11825 directive[i] = IDENTIFIER_POINTER (id);
11827 const c_omp_directive *dir = NULL;
11828 if (directive[0])
11829 dir = c_omp_categorize_directive (directive[0], directive[1],
11830 directive[2]);
11831 if (dir == NULL)
11833 error_at (first->location,
11834 "unknown OpenMP directive name in %<omp::directive%>"
11835 " attribute argument");
11836 continue;
11838 c_omp_directive_kind kind = dir->kind;
11839 if (dir->id == PRAGMA_OMP_ORDERED)
11841 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
11842 depend clause. */
11843 if (directive[1] && strcmp (directive[1], "depend") == 0)
11844 kind = C_OMP_DIR_STANDALONE;
11845 else if (first + 2 < last
11846 && first[1].type == CPP_COMMA
11847 && first[2].type == CPP_NAME
11848 && strcmp (IDENTIFIER_POINTER (first[2].u.value),
11849 "depend") == 0)
11850 kind = C_OMP_DIR_STANDALONE;
11852 else if (dir->id == PRAGMA_OMP_ERROR)
11854 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
11855 int paren_depth = 0;
11856 for (int i = 1; first + i < last; i++)
11857 if (first[i].type == CPP_OPEN_PAREN)
11858 paren_depth++;
11859 else if (first[i].type == CPP_CLOSE_PAREN)
11860 paren_depth--;
11861 else if (paren_depth == 0
11862 && first + i + 2 < last
11863 && first[i].type == CPP_NAME
11864 && first[i + 1].type == CPP_OPEN_PAREN
11865 && first[i + 2].type == CPP_NAME
11866 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
11867 "at")
11868 && !strcmp (IDENTIFIER_POINTER (first[i
11869 + 2].u.value),
11870 "execution"))
11872 kind = C_OMP_DIR_STANDALONE;
11873 break;
11876 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
11877 vec.safe_push (v);
11878 if (flag_openmp || dir->simd)
11879 tokens += (last - first) + 1;
11881 cp_omp_attribute_data v = {};
11882 vec.safe_push (v);
11883 *pa = TREE_CHAIN (*pa);
11885 else
11886 pa = &TREE_CHAIN (*pa);
11888 if (bad)
11889 return attrs;
11891 unsigned int i;
11892 cp_omp_attribute_data *v;
11893 cp_omp_attribute_data *construct_seen = nullptr;
11894 cp_omp_attribute_data *standalone_seen = nullptr;
11895 cp_omp_attribute_data *prev_standalone_seen = nullptr;
11896 FOR_EACH_VEC_ELT (vec, i, v)
11897 if (v->tokens)
11899 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
11900 construct_seen = v;
11901 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
11902 standalone_seen = v;
11904 else
11906 if (standalone_seen && !prev_standalone_seen)
11908 prev_standalone_seen = standalone_seen;
11909 standalone_seen = nullptr;
11913 if (cnt > 1 && construct_seen)
11915 error_at (construct_seen->tokens->first->location,
11916 "OpenMP construct among %<omp::directive%> attributes"
11917 " requires all %<omp::directive%> attributes on the"
11918 " same statement to be in the same %<omp::sequence%>");
11919 return attrs;
11921 if (cnt > 1 && standalone_seen && prev_standalone_seen)
11923 error_at (standalone_seen->tokens->first->location,
11924 "multiple OpenMP standalone directives among"
11925 " %<omp::directive%> attributes must be all within the"
11926 " same %<omp::sequence%>");
11927 return attrs;
11930 if (prev_standalone_seen)
11931 standalone_seen = prev_standalone_seen;
11932 if (standalone_seen
11933 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11935 error_at (standalone_seen->tokens->first->location,
11936 "standalone OpenMP directives in %<omp::directive%> attribute"
11937 " can only appear on an empty statement");
11938 return attrs;
11940 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
11942 cp_token *token = cp_lexer_peek_token (parser->lexer);
11943 enum pragma_kind kind = cp_parser_pragma_kind (token);
11944 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
11946 error_at (token->location,
11947 "mixing OpenMP directives with attribute and pragma "
11948 "syntax on the same statement");
11949 return attrs;
11953 if (!tokens)
11954 return attrs;
11955 tokens++;
11956 cp_lexer *lexer = cp_lexer_alloc ();
11957 lexer->debugging_p = parser->lexer->debugging_p;
11958 vec_safe_reserve (lexer->buffer, tokens, true);
11959 FOR_EACH_VEC_ELT (vec, i, v)
11961 if (!v->tokens)
11962 continue;
11963 if (!flag_openmp && !v->dir->simd)
11964 continue;
11965 cp_token *first = v->tokens->first;
11966 cp_token *last = v->tokens->last;
11967 cp_token tok = {};
11968 tok.type = CPP_PRAGMA;
11969 tok.keyword = RID_MAX;
11970 tok.u.value = build_int_cst (NULL, v->dir->id);
11971 tok.location = first->location;
11972 lexer->buffer->quick_push (tok);
11973 while (++first < last)
11974 lexer->buffer->quick_push (*first);
11975 tok = {};
11976 tok.type = CPP_PRAGMA_EOL;
11977 tok.keyword = RID_MAX;
11978 tok.location = last->location;
11979 lexer->buffer->quick_push (tok);
11981 cp_token tok = {};
11982 tok.type = CPP_EOF;
11983 tok.keyword = RID_MAX;
11984 tok.location = lexer->buffer->last ().location;
11985 lexer->buffer->quick_push (tok);
11986 lexer->next = parser->lexer;
11987 lexer->next_token = lexer->buffer->address ();
11988 lexer->last_token = lexer->next_token
11989 + lexer->buffer->length ()
11990 - 1;
11991 lexer->in_omp_attribute_pragma = true;
11992 parser->lexer = lexer;
11993 /* Move the current source position to that of the first token in the
11994 new lexer. */
11995 cp_lexer_set_source_position_from_token (lexer->next_token);
11996 return attrs;
11999 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12000 (if any) at the start or after declaration-id of a declaration. */
12002 static void
12003 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12004 cp_omp_declare_simd_data *data,
12005 bool start)
12007 if (!flag_openmp && !flag_openmp_simd)
12008 return;
12010 int cnt = 0;
12011 bool bad = false;
12012 bool variant_p = false;
12013 location_t loc = UNKNOWN_LOCATION;
12014 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12015 if (get_attribute_namespace (pa) == omp_identifier
12016 && is_attribute_p ("directive", get_attribute_name (pa)))
12018 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12020 tree d = TREE_VALUE (a);
12021 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12022 cp_token *first = DEFPARSE_TOKENS (d)->first;
12023 cp_token *last = DEFPARSE_TOKENS (d)->last;
12024 const char *directive[3] = {};
12025 for (int i = 0; i < 3; i++)
12027 tree id = NULL_TREE;
12028 if (first + i == last)
12029 break;
12030 if (first[i].type == CPP_NAME)
12031 id = first[i].u.value;
12032 else if (first[i].type == CPP_KEYWORD)
12033 id = ridpointers[(int) first[i].keyword];
12034 else
12035 break;
12036 directive[i] = IDENTIFIER_POINTER (id);
12038 const c_omp_directive *dir = NULL;
12039 if (directive[0])
12040 dir = c_omp_categorize_directive (directive[0], directive[1],
12041 directive[2]);
12042 if (dir == NULL)
12043 continue;
12044 if (dir->id == PRAGMA_OMP_DECLARE
12045 && (strcmp (directive[1], "simd") == 0
12046 || strcmp (directive[1], "variant") == 0))
12048 if (cnt++ == 0)
12050 variant_p = strcmp (directive[1], "variant") == 0;
12051 loc = first->location;
12053 if (start && parser->omp_declare_simd && !bad)
12055 error_at (first->location,
12056 "mixing OpenMP directives with attribute and "
12057 "pragma syntax on the same declaration");
12058 bad = true;
12064 if (bad)
12066 for (tree *pa = pattrs; *pa; )
12067 if (get_attribute_namespace (*pa) == omp_identifier
12068 && is_attribute_p ("directive", get_attribute_name (*pa)))
12069 *pa = TREE_CHAIN (*pa);
12070 else
12071 pa = &TREE_CHAIN (*pa);
12072 return;
12074 if (cnt == 0)
12075 return;
12077 if (parser->omp_declare_simd == NULL)
12079 data->error_seen = false;
12080 data->fndecl_seen = false;
12081 data->variant_p = variant_p;
12082 data->loc = loc;
12083 data->tokens = vNULL;
12084 data->attribs[0] = NULL;
12085 data->attribs[1] = NULL;
12086 parser->omp_declare_simd = data;
12088 parser->omp_declare_simd->attribs[!start] = pattrs;
12091 /* Parse a statement.
12093 statement:
12094 labeled-statement
12095 expression-statement
12096 compound-statement
12097 selection-statement
12098 iteration-statement
12099 jump-statement
12100 declaration-statement
12101 try-block
12103 C++11:
12105 statement:
12106 labeled-statement
12107 attribute-specifier-seq (opt) expression-statement
12108 attribute-specifier-seq (opt) compound-statement
12109 attribute-specifier-seq (opt) selection-statement
12110 attribute-specifier-seq (opt) iteration-statement
12111 attribute-specifier-seq (opt) jump-statement
12112 declaration-statement
12113 attribute-specifier-seq (opt) try-block
12115 init-statement:
12116 expression-statement
12117 simple-declaration
12118 alias-declaration
12120 TM Extension:
12122 statement:
12123 atomic-statement
12125 IN_COMPOUND is true when the statement is nested inside a
12126 cp_parser_compound_statement; this matters for certain pragmas.
12128 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12129 is a (possibly labeled) if statement which is not enclosed in braces
12130 and has an else clause. This is used to implement -Wparentheses.
12132 CHAIN is a vector of if-else-if conditions. */
12134 static void
12135 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12136 bool in_compound, bool *if_p, vec<tree> *chain,
12137 location_t *loc_after_labels)
12139 tree statement, std_attrs = NULL_TREE;
12140 cp_token *token;
12141 location_t statement_location, attrs_loc;
12142 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12143 bool has_std_attrs;
12145 restart:
12146 if (if_p != NULL)
12147 *if_p = false;
12148 /* There is no statement yet. */
12149 statement = NULL_TREE;
12151 saved_token_sentinel saved_tokens (parser->lexer);
12152 token = cp_lexer_peek_token (parser->lexer);
12153 attrs_loc = token->location;
12154 if (c_dialect_objc ())
12155 /* In obj-c++, seeing '[[' might be the either the beginning of
12156 c++11 attributes, or a nested objc-message-expression. So
12157 let's parse the c++11 attributes tentatively. */
12158 cp_parser_parse_tentatively (parser);
12159 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12160 if (std_attrs)
12161 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12162 if (c_dialect_objc ())
12164 if (!cp_parser_parse_definitely (parser))
12165 std_attrs = NULL_TREE;
12167 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12169 /* Peek at the next token. */
12170 token = cp_lexer_peek_token (parser->lexer);
12171 bool omp_attrs_forbidden_p;
12172 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12174 if (std_attrs && (flag_openmp || flag_openmp_simd))
12176 bool handle_omp_attribs = false;
12177 if (token->type == CPP_KEYWORD)
12178 switch (token->keyword)
12180 case RID_IF:
12181 case RID_SWITCH:
12182 case RID_WHILE:
12183 case RID_DO:
12184 case RID_FOR:
12185 case RID_BREAK:
12186 case RID_CONTINUE:
12187 case RID_RETURN:
12188 case RID_CO_RETURN:
12189 case RID_GOTO:
12190 case RID_AT_TRY:
12191 case RID_AT_CATCH:
12192 case RID_AT_FINALLY:
12193 case RID_AT_SYNCHRONIZED:
12194 case RID_AT_THROW:
12195 case RID_TRY:
12196 case RID_TRANSACTION_ATOMIC:
12197 case RID_TRANSACTION_RELAXED:
12198 case RID_SYNCHRONIZED:
12199 case RID_ATOMIC_NOEXCEPT:
12200 case RID_ATOMIC_CANCEL:
12201 case RID_TRANSACTION_CANCEL:
12202 handle_omp_attribs = true;
12203 break;
12204 default:
12205 break;
12207 else if (token->type == CPP_SEMICOLON
12208 || token->type == CPP_OPEN_BRACE
12209 || token->type == CPP_PRAGMA)
12210 handle_omp_attribs = true;
12211 if (handle_omp_attribs)
12213 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12214 std_attrs);
12215 token = cp_lexer_peek_token (parser->lexer);
12218 parser->omp_attrs_forbidden_p = false;
12220 /* Remember the location of the first token in the statement. */
12221 cp_token *statement_token = token;
12222 statement_location = token->location;
12223 add_debug_begin_stmt (statement_location);
12224 /* If this is a keyword, then that will often determine what kind of
12225 statement we have. */
12226 if (token->type == CPP_KEYWORD)
12228 enum rid keyword = token->keyword;
12230 switch (keyword)
12232 case RID_CASE:
12233 case RID_DEFAULT:
12234 /* Looks like a labeled-statement with a case label.
12235 Parse the label, and then use tail recursion to parse
12236 the statement. */
12237 cp_parser_label_for_labeled_statement (parser, std_attrs);
12238 in_compound = false;
12239 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12240 goto restart;
12242 case RID_IF:
12243 case RID_SWITCH:
12244 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12245 statement = cp_parser_selection_statement (parser, if_p, chain);
12246 break;
12248 case RID_WHILE:
12249 case RID_DO:
12250 case RID_FOR:
12251 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12252 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
12253 break;
12255 case RID_BREAK:
12256 case RID_CONTINUE:
12257 case RID_RETURN:
12258 case RID_CO_RETURN:
12259 case RID_GOTO:
12260 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12261 statement = cp_parser_jump_statement (parser);
12262 break;
12264 /* Objective-C++ exception-handling constructs. */
12265 case RID_AT_TRY:
12266 case RID_AT_CATCH:
12267 case RID_AT_FINALLY:
12268 case RID_AT_SYNCHRONIZED:
12269 case RID_AT_THROW:
12270 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12271 statement = cp_parser_objc_statement (parser);
12272 break;
12274 case RID_TRY:
12275 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12276 statement = cp_parser_try_block (parser);
12277 break;
12279 case RID_NAMESPACE:
12280 /* This must be a namespace alias definition. */
12281 if (has_std_attrs)
12283 /* Attributes should be parsed as part of the
12284 declaration, so let's un-parse them. */
12285 saved_tokens.rollback();
12286 std_attrs = NULL_TREE;
12288 cp_parser_declaration_statement (parser);
12289 return;
12291 case RID_TRANSACTION_ATOMIC:
12292 case RID_TRANSACTION_RELAXED:
12293 case RID_SYNCHRONIZED:
12294 case RID_ATOMIC_NOEXCEPT:
12295 case RID_ATOMIC_CANCEL:
12296 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12297 statement = cp_parser_transaction (parser, token);
12298 break;
12299 case RID_TRANSACTION_CANCEL:
12300 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12301 statement = cp_parser_transaction_cancel (parser);
12302 break;
12304 default:
12305 /* It might be a keyword like `int' that can start a
12306 declaration-statement. */
12307 break;
12310 else if (token->type == CPP_NAME)
12312 /* If the next token is a `:', then we are looking at a
12313 labeled-statement. */
12314 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12315 if (token->type == CPP_COLON)
12317 /* Looks like a labeled-statement with an ordinary label.
12318 Parse the label, and then use tail recursion to parse
12319 the statement. */
12321 cp_parser_label_for_labeled_statement (parser, std_attrs);
12322 in_compound = false;
12323 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12324 goto restart;
12327 /* Anything that starts with a `{' must be a compound-statement. */
12328 else if (token->type == CPP_OPEN_BRACE)
12330 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12331 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12333 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12334 a statement all its own. */
12335 else if (token->type == CPP_PRAGMA)
12337 do_pragma:;
12338 cp_lexer *lexer = parser->lexer;
12339 bool do_restart = false;
12340 /* Only certain OpenMP pragmas are attached to statements, and thus
12341 are considered statements themselves. All others are not. In
12342 the context of a compound, accept the pragma as a "statement" and
12343 return so that we can check for a close brace. Otherwise we
12344 require a real statement and must go back and read one. */
12345 if (in_compound)
12346 cp_parser_pragma (parser, pragma_compound, if_p);
12347 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12348 do_restart = true;
12349 if (parser->lexer != lexer
12350 && lexer->in_omp_attribute_pragma
12351 && (!in_omp_attribute_pragma || lexer->orphan_p))
12353 if (saved_tokens.lexer == lexer)
12355 if (saved_tokens.commit)
12356 cp_lexer_commit_tokens (lexer);
12357 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12358 saved_tokens.lexer = parser->lexer;
12359 saved_tokens.commit = false;
12360 saved_tokens.len = parser->lexer->saved_tokens.length ();
12362 cp_lexer_destroy (lexer);
12363 lexer = parser->lexer;
12365 if (do_restart)
12366 goto restart;
12367 if (parser->lexer == lexer
12368 && lexer->in_omp_attribute_pragma
12369 && !in_omp_attribute_pragma)
12370 parser->lexer->orphan_p = true;
12371 return;
12373 else if (token->type == CPP_EOF)
12375 cp_parser_error (parser, "expected statement");
12376 return;
12379 /* Everything else must be a declaration-statement or an
12380 expression-statement. Try for the declaration-statement
12381 first, unless we are looking at a `;', in which case we know that
12382 we have an expression-statement. */
12383 if (!statement)
12385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12387 if (has_std_attrs)
12388 /* Attributes should be parsed as part of the declaration,
12389 so let's un-parse them. */
12390 saved_tokens.rollback();
12392 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12393 cp_parser_parse_tentatively (parser);
12394 /* Try to parse the declaration-statement. */
12395 cp_parser_declaration_statement (parser);
12396 parser->omp_attrs_forbidden_p = false;
12397 /* If that worked, we're done. */
12398 if (cp_parser_parse_definitely (parser))
12399 return;
12400 /* It didn't work, restore the post-attribute position. */
12401 if (has_std_attrs)
12403 cp_lexer_set_token_position (parser->lexer, statement_token);
12404 if (flag_openmp || flag_openmp_simd)
12406 size_t i = 1;
12407 bool handle_omp_attribs = true;
12408 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12409 == RID_EXTENSION)
12410 i++;
12411 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12413 case RID_ASM:
12414 case RID_NAMESPACE:
12415 case RID_USING:
12416 case RID_LABEL:
12417 case RID_STATIC_ASSERT:
12418 /* Don't handle OpenMP attribs on keywords that
12419 always start a declaration statement but don't
12420 accept attribute before it and therefore
12421 the tentative cp_parser_declaration_statement
12422 fails to parse because of that. */
12423 handle_omp_attribs = false;
12424 break;
12425 default:
12426 break;
12429 if (handle_omp_attribs)
12431 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12432 std_attrs
12433 = cp_parser_handle_statement_omp_attributes
12434 (parser, std_attrs);
12435 parser->omp_attrs_forbidden_p = false;
12436 token = cp_lexer_peek_token (parser->lexer);
12437 if (token->type == CPP_PRAGMA)
12438 goto do_pragma;
12443 /* All preceding labels have been parsed at this point. */
12444 if (loc_after_labels != NULL)
12445 *loc_after_labels = statement_location;
12447 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12449 /* Look for an expression-statement instead. */
12450 statement = cp_parser_expression_statement (parser, in_statement_expr);
12452 /* Handle [[fallthrough]];. */
12453 if (attribute_fallthrough_p (std_attrs))
12455 /* The next token after the fallthrough attribute is ';'. */
12456 if (statement == NULL_TREE)
12458 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12459 statement = build_call_expr_internal_loc (statement_location,
12460 IFN_FALLTHROUGH,
12461 void_type_node, 0);
12462 finish_expr_stmt (statement);
12464 else
12465 warning_at (statement_location, OPT_Wattributes,
12466 "%<fallthrough%> attribute not followed by %<;%>");
12467 std_attrs = NULL_TREE;
12471 /* Set the line number for the statement. */
12472 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12473 SET_EXPR_LOCATION (statement, statement_location);
12475 /* Allow "[[fallthrough]];", but warn otherwise. */
12476 if (std_attrs != NULL_TREE)
12477 warning_at (attrs_loc,
12478 OPT_Wattributes,
12479 "attributes at the beginning of statement are ignored");
12482 /* Append ATTR to attribute list ATTRS. */
12484 static tree
12485 attr_chainon (tree attrs, tree attr)
12487 if (attrs == error_mark_node)
12488 return error_mark_node;
12489 if (attr == error_mark_node)
12490 return error_mark_node;
12491 return chainon (attrs, attr);
12494 /* Parse the label for a labeled-statement, i.e.
12496 identifier :
12497 case constant-expression :
12498 default :
12500 GNU Extension:
12501 case constant-expression ... constant-expression : statement
12503 When a label is parsed without errors, the label is added to the
12504 parse tree by the finish_* functions, so this function doesn't
12505 have to return the label. */
12507 static void
12508 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12510 cp_token *token;
12511 tree label = NULL_TREE;
12512 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12514 /* The next token should be an identifier. */
12515 token = cp_lexer_peek_token (parser->lexer);
12516 if (token->type != CPP_NAME
12517 && token->type != CPP_KEYWORD)
12519 cp_parser_error (parser, "expected labeled-statement");
12520 return;
12523 /* Remember whether this case or a user-defined label is allowed to fall
12524 through to. */
12525 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12527 parser->colon_corrects_to_scope_p = false;
12528 switch (token->keyword)
12530 case RID_CASE:
12532 tree expr, expr_hi;
12533 cp_token *ellipsis;
12535 /* Consume the `case' token. */
12536 cp_lexer_consume_token (parser->lexer);
12537 /* Parse the constant-expression. */
12538 expr = cp_parser_constant_expression (parser);
12539 if (check_for_bare_parameter_packs (expr))
12540 expr = error_mark_node;
12542 ellipsis = cp_lexer_peek_token (parser->lexer);
12543 if (ellipsis->type == CPP_ELLIPSIS)
12545 /* Consume the `...' token. */
12546 cp_lexer_consume_token (parser->lexer);
12547 expr_hi = cp_parser_constant_expression (parser);
12548 if (check_for_bare_parameter_packs (expr_hi))
12549 expr_hi = error_mark_node;
12551 /* We don't need to emit warnings here, as the common code
12552 will do this for us. */
12554 else
12555 expr_hi = NULL_TREE;
12557 if (parser->in_switch_statement_p)
12559 tree l = finish_case_label (token->location, expr, expr_hi);
12560 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12562 label = CASE_LABEL (l);
12563 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12566 else
12567 error_at (token->location,
12568 "case label %qE not within a switch statement",
12569 expr);
12571 break;
12573 case RID_DEFAULT:
12574 /* Consume the `default' token. */
12575 cp_lexer_consume_token (parser->lexer);
12577 if (parser->in_switch_statement_p)
12579 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
12580 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12582 label = CASE_LABEL (l);
12583 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12586 else
12587 error_at (token->location, "case label not within a switch statement");
12588 break;
12590 default:
12591 /* Anything else must be an ordinary label. */
12592 label = finish_label_stmt (cp_parser_identifier (parser));
12593 if (label && TREE_CODE (label) == LABEL_DECL)
12594 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12595 break;
12598 /* Require the `:' token. */
12599 cp_parser_require (parser, CPP_COLON, RT_COLON);
12601 /* An ordinary label may optionally be followed by attributes.
12602 However, this is only permitted if the attributes are then
12603 followed by a semicolon. This is because, for backward
12604 compatibility, when parsing
12605 lab: __attribute__ ((unused)) int i;
12606 we want the attribute to attach to "i", not "lab". */
12607 if (label != NULL_TREE
12608 && cp_next_tokens_can_be_gnu_attribute_p (parser))
12610 tree attrs;
12611 cp_parser_parse_tentatively (parser);
12612 attrs = cp_parser_gnu_attributes_opt (parser);
12613 if (attrs == NULL_TREE
12614 /* And fallthrough always binds to the expression-statement. */
12615 || attribute_fallthrough_p (attrs)
12616 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12617 cp_parser_abort_tentative_parse (parser);
12618 else if (!cp_parser_parse_definitely (parser))
12620 else
12621 attributes = attr_chainon (attributes, attrs);
12624 if (attributes != NULL_TREE)
12625 cplus_decl_attributes (&label, attributes, 0);
12627 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12630 /* Parse an expression-statement.
12632 expression-statement:
12633 expression [opt] ;
12635 Returns the new EXPR_STMT -- or NULL_TREE if the expression
12636 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12637 indicates whether this expression-statement is part of an
12638 expression statement. */
12640 static tree
12641 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
12643 tree statement = NULL_TREE;
12644 cp_token *token = cp_lexer_peek_token (parser->lexer);
12645 location_t loc = token->location;
12647 /* There might be attribute fallthrough. */
12648 tree attr = cp_parser_gnu_attributes_opt (parser);
12650 /* If the next token is a ';', then there is no expression
12651 statement. */
12652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12654 statement = cp_parser_expression (parser);
12655 if (statement == error_mark_node
12656 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12658 cp_parser_skip_to_end_of_block_or_statement (parser);
12659 return error_mark_node;
12663 /* Handle [[fallthrough]];. */
12664 if (attribute_fallthrough_p (attr))
12666 /* The next token after the fallthrough attribute is ';'. */
12667 if (statement == NULL_TREE)
12668 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12669 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
12670 void_type_node, 0);
12671 else
12672 warning_at (loc, OPT_Wattributes,
12673 "%<fallthrough%> attribute not followed by %<;%>");
12674 attr = NULL_TREE;
12677 /* Allow "[[fallthrough]];", but warn otherwise. */
12678 if (attr != NULL_TREE)
12679 warning_at (loc, OPT_Wattributes,
12680 "attributes at the beginning of statement are ignored");
12682 /* Give a helpful message for "A<T>::type t;" and the like. */
12683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
12684 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12686 if (TREE_CODE (statement) == SCOPE_REF)
12687 error_at (token->location, "need %<typename%> before %qE because "
12688 "%qT is a dependent scope",
12689 statement, TREE_OPERAND (statement, 0));
12690 else if (is_overloaded_fn (statement)
12691 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
12693 /* A::A a; */
12694 tree fn = get_first_fn (statement);
12695 error_at (token->location,
12696 "%<%T::%D%> names the constructor, not the type",
12697 DECL_CONTEXT (fn), DECL_NAME (fn));
12701 /* Consume the final `;'. */
12702 cp_parser_consume_semicolon_at_end_of_statement (parser);
12704 if (in_statement_expr
12705 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12706 /* This is the final expression statement of a statement
12707 expression. */
12708 statement = finish_stmt_expr_expr (statement, in_statement_expr);
12709 else if (statement)
12710 statement = finish_expr_stmt (statement);
12712 return statement;
12715 /* Parse a compound-statement.
12717 compound-statement:
12718 { statement-seq [opt] }
12720 GNU extension:
12722 compound-statement:
12723 { label-declaration-seq [opt] statement-seq [opt] }
12725 label-declaration-seq:
12726 label-declaration
12727 label-declaration-seq label-declaration
12729 Returns a tree representing the statement. */
12731 static tree
12732 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
12733 int bcs_flags, bool function_body)
12735 tree compound_stmt;
12736 matching_braces braces;
12738 /* Consume the `{'. */
12739 if (!braces.require_open (parser))
12740 return error_mark_node;
12741 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
12742 && !function_body && cxx_dialect < cxx14)
12743 pedwarn (input_location, OPT_Wpedantic,
12744 "compound-statement in %<constexpr%> function");
12745 /* Begin the compound-statement. */
12746 compound_stmt = begin_compound_stmt (bcs_flags);
12747 /* If the next keyword is `__label__' we have a label declaration. */
12748 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12749 cp_parser_label_declaration (parser);
12750 /* Parse an (optional) statement-seq. */
12751 cp_parser_statement_seq_opt (parser, in_statement_expr);
12753 if (function_body)
12754 maybe_splice_retval_cleanup (compound_stmt);
12756 /* Consume the `}'. */
12757 braces.require_close (parser);
12759 /* Finish the compound-statement. */
12760 finish_compound_stmt (compound_stmt);
12762 return compound_stmt;
12765 /* Parse an (optional) statement-seq.
12767 statement-seq:
12768 statement
12769 statement-seq [opt] statement */
12771 static void
12772 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
12774 /* Scan statements until there aren't any more. */
12775 while (true)
12777 cp_token *token = cp_lexer_peek_token (parser->lexer);
12779 /* If we are looking at a `}', then we have run out of
12780 statements; the same is true if we have reached the end
12781 of file, or have stumbled upon a stray '@end'. */
12782 if (token->type == CPP_CLOSE_BRACE
12783 || token->type == CPP_EOF
12784 || token->type == CPP_PRAGMA_EOL
12785 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
12786 break;
12788 /* If we are in a compound statement and find 'else' then
12789 something went wrong. */
12790 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
12792 if (parser->in_statement & IN_IF_STMT)
12793 break;
12794 else
12796 token = cp_lexer_consume_token (parser->lexer);
12797 error_at (token->location, "%<else%> without a previous %<if%>");
12801 /* Parse the statement. */
12802 cp_parser_statement (parser, in_statement_expr, true, NULL);
12806 /* Return true if this is the C++20 version of range-based-for with
12807 init-statement. */
12809 static bool
12810 cp_parser_range_based_for_with_init_p (cp_parser *parser)
12812 bool r = false;
12814 /* Save tokens so that we can put them back. */
12815 cp_lexer_save_tokens (parser->lexer);
12817 /* There has to be an unnested ; followed by an unnested :. */
12818 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
12819 /*recovering=*/false,
12820 CPP_SEMICOLON,
12821 /*consume_paren=*/false) != -1)
12822 goto out;
12824 /* We found the semicolon, eat it now. */
12825 cp_lexer_consume_token (parser->lexer);
12827 /* Now look for ':' that is not nested in () or {}. */
12828 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
12829 /*recovering=*/false,
12830 CPP_COLON,
12831 /*consume_paren=*/false) == -1);
12833 out:
12834 /* Roll back the tokens we skipped. */
12835 cp_lexer_rollback_tokens (parser->lexer);
12837 return r;
12840 /* Return true if we're looking at (init; cond), false otherwise. */
12842 static bool
12843 cp_parser_init_statement_p (cp_parser *parser)
12845 /* Save tokens so that we can put them back. */
12846 cp_lexer_save_tokens (parser->lexer);
12848 /* Look for ';' that is not nested in () or {}. */
12849 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
12850 /*recovering=*/false,
12851 CPP_SEMICOLON,
12852 /*consume_paren=*/false);
12854 /* Roll back the tokens we skipped. */
12855 cp_lexer_rollback_tokens (parser->lexer);
12857 return ret == -1;
12860 /* Parse a selection-statement.
12862 selection-statement:
12863 if ( init-statement [opt] condition ) statement
12864 if ( init-statement [opt] condition ) statement else statement
12865 switch ( init-statement [opt] condition ) statement
12867 Returns the new IF_STMT or SWITCH_STMT.
12869 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12870 is a (possibly labeled) if statement which is not enclosed in
12871 braces and has an else clause. This is used to implement
12872 -Wparentheses.
12874 CHAIN is a vector of if-else-if conditions. This is used to implement
12875 -Wduplicated-cond. */
12877 static tree
12878 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
12879 vec<tree> *chain)
12881 cp_token *token;
12882 enum rid keyword;
12883 token_indent_info guard_tinfo;
12885 if (if_p != NULL)
12886 *if_p = false;
12888 /* Peek at the next token. */
12889 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
12890 guard_tinfo = get_token_indent_info (token);
12892 /* See what kind of keyword it is. */
12893 keyword = token->keyword;
12894 switch (keyword)
12896 case RID_IF:
12897 case RID_SWITCH:
12899 tree statement;
12900 tree condition;
12902 bool cx = false;
12903 if (keyword == RID_IF
12904 && cp_lexer_next_token_is_keyword (parser->lexer,
12905 RID_CONSTEXPR))
12907 cx = true;
12908 cp_token *tok = cp_lexer_consume_token (parser->lexer);
12909 if (cxx_dialect < cxx17)
12910 pedwarn (tok->location, OPT_Wc__17_extensions,
12911 "%<if constexpr%> only available with "
12912 "%<-std=c++17%> or %<-std=gnu++17%>");
12914 int ce = 0;
12915 if (keyword == RID_IF && !cx)
12917 if (cp_lexer_next_token_is_keyword (parser->lexer,
12918 RID_CONSTEVAL))
12919 ce = 1;
12920 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
12921 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
12922 RID_CONSTEVAL))
12924 ce = -1;
12925 cp_lexer_consume_token (parser->lexer);
12928 if (ce)
12930 cp_token *tok = cp_lexer_consume_token (parser->lexer);
12931 if (cxx_dialect < cxx23)
12932 pedwarn (tok->location, OPT_Wc__23_extensions,
12933 "%<if consteval%> only available with "
12934 "%<-std=c++2b%> or %<-std=gnu++2b%>");
12936 bool save_in_consteval_if_p = in_consteval_if_p;
12937 statement = begin_if_stmt ();
12938 IF_STMT_CONSTEVAL_P (statement) = true;
12939 condition = finish_if_stmt_cond (boolean_false_node, statement);
12941 gcc_rich_location richloc (tok->location);
12942 bool non_compound_stmt_p = false;
12943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12945 non_compound_stmt_p = true;
12946 richloc.add_fixit_insert_after (tok->location, "{");
12949 in_consteval_if_p |= ce > 0;
12950 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12952 if (non_compound_stmt_p)
12954 location_t before_loc
12955 = cp_lexer_peek_token (parser->lexer)->location;
12956 richloc.add_fixit_insert_before (before_loc, "}");
12957 error_at (&richloc,
12958 "%<if consteval%> requires compound statement");
12959 non_compound_stmt_p = false;
12962 finish_then_clause (statement);
12964 /* If the next token is `else', parse the else-clause. */
12965 if (cp_lexer_next_token_is_keyword (parser->lexer,
12966 RID_ELSE))
12968 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
12969 gcc_rich_location else_richloc (else_tok->location);
12970 guard_tinfo = get_token_indent_info (else_tok);
12971 /* Consume the `else' keyword. */
12972 cp_lexer_consume_token (parser->lexer);
12974 begin_else_clause (statement);
12976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12978 non_compound_stmt_p = true;
12979 else_richloc.add_fixit_insert_after (else_tok->location,
12980 "{");
12983 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
12984 cp_parser_implicitly_scoped_statement (parser, NULL,
12985 guard_tinfo);
12987 if (non_compound_stmt_p)
12989 location_t before_loc
12990 = cp_lexer_peek_token (parser->lexer)->location;
12991 else_richloc.add_fixit_insert_before (before_loc, "}");
12992 error_at (&else_richloc,
12993 "%<if consteval%> requires compound statement");
12996 finish_else_clause (statement);
12999 in_consteval_if_p = save_in_consteval_if_p;
13000 if (ce < 0)
13002 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13003 if (THEN_CLAUSE (statement) == NULL_TREE)
13004 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13007 finish_if_stmt (statement);
13008 return statement;
13011 /* Look for the `('. */
13012 matching_parens parens;
13013 if (!parens.require_open (parser))
13015 cp_parser_skip_to_end_of_statement (parser);
13016 return error_mark_node;
13019 /* Begin the selection-statement. */
13020 if (keyword == RID_IF)
13022 statement = begin_if_stmt ();
13023 IF_STMT_CONSTEXPR_P (statement) = cx;
13025 else
13026 statement = begin_switch_stmt ();
13028 /* Parse the optional init-statement. */
13029 if (cp_parser_init_statement_p (parser))
13031 tree decl;
13032 if (cxx_dialect < cxx17)
13033 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13034 OPT_Wc__17_extensions,
13035 "init-statement in selection statements only available "
13036 "with %<-std=c++17%> or %<-std=gnu++17%>");
13037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13038 /* A non-empty init-statement can have arbitrary side
13039 effects. */
13040 vec_free (chain);
13041 cp_parser_init_statement (parser, &decl);
13044 /* Parse the condition. */
13045 condition = cp_parser_condition (parser);
13046 /* Look for the `)'. */
13047 if (!parens.require_close (parser))
13048 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13049 /*consume_paren=*/true);
13051 if (keyword == RID_IF)
13053 bool nested_if;
13054 unsigned char in_statement;
13056 /* Add the condition. */
13057 condition = finish_if_stmt_cond (condition, statement);
13059 if (warn_duplicated_cond)
13060 warn_duplicated_cond_add_or_warn (token->location, condition,
13061 &chain);
13063 /* Parse the then-clause. */
13064 in_statement = parser->in_statement;
13065 parser->in_statement |= IN_IF_STMT;
13067 /* Outside a template, the non-selected branch of a constexpr
13068 if is a 'discarded statement', i.e. unevaluated. */
13069 bool was_discarded = in_discarded_stmt;
13070 bool discard_then = (cx && !processing_template_decl
13071 && integer_zerop (condition));
13072 if (discard_then)
13074 in_discarded_stmt = true;
13075 ++c_inhibit_evaluation_warnings;
13078 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13079 guard_tinfo);
13081 parser->in_statement = in_statement;
13083 finish_then_clause (statement);
13085 if (discard_then)
13087 THEN_CLAUSE (statement) = NULL_TREE;
13088 in_discarded_stmt = was_discarded;
13089 --c_inhibit_evaluation_warnings;
13092 /* If the next token is `else', parse the else-clause. */
13093 if (cp_lexer_next_token_is_keyword (parser->lexer,
13094 RID_ELSE))
13096 bool discard_else = (cx && !processing_template_decl
13097 && integer_nonzerop (condition));
13098 if (discard_else)
13100 in_discarded_stmt = true;
13101 ++c_inhibit_evaluation_warnings;
13104 guard_tinfo
13105 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13106 /* Consume the `else' keyword. */
13107 cp_lexer_consume_token (parser->lexer);
13108 if (warn_duplicated_cond)
13110 if (cp_lexer_next_token_is_keyword (parser->lexer,
13111 RID_IF)
13112 && chain == NULL)
13114 /* We've got "if (COND) else if (COND2)". Start
13115 the condition chain and add COND as the first
13116 element. */
13117 chain = new vec<tree> ();
13118 if (!CONSTANT_CLASS_P (condition)
13119 && !TREE_SIDE_EFFECTS (condition))
13121 /* Wrap it in a NOP_EXPR so that we can set the
13122 location of the condition. */
13123 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13124 condition);
13125 SET_EXPR_LOCATION (e, token->location);
13126 chain->safe_push (e);
13129 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13130 RID_IF))
13131 /* This is if-else without subsequent if. Zap the
13132 condition chain; we would have already warned at
13133 this point. */
13134 vec_free (chain);
13136 begin_else_clause (statement);
13137 /* Parse the else-clause. */
13138 cp_parser_implicitly_scoped_statement (parser, NULL,
13139 guard_tinfo, chain);
13141 finish_else_clause (statement);
13143 /* If we are currently parsing a then-clause, then
13144 IF_P will not be NULL. We set it to true to
13145 indicate that this if statement has an else clause.
13146 This may trigger the Wparentheses warning below
13147 when we get back up to the parent if statement. */
13148 if (if_p != NULL)
13149 *if_p = true;
13151 if (discard_else)
13153 ELSE_CLAUSE (statement) = NULL_TREE;
13154 in_discarded_stmt = was_discarded;
13155 --c_inhibit_evaluation_warnings;
13158 else
13160 /* This if statement does not have an else clause. If
13161 NESTED_IF is true, then the then-clause has an if
13162 statement which does have an else clause. We warn
13163 about the potential ambiguity. */
13164 if (nested_if)
13165 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13166 "suggest explicit braces to avoid ambiguous"
13167 " %<else%>");
13168 if (warn_duplicated_cond)
13169 /* We don't need the condition chain anymore. */
13170 vec_free (chain);
13173 /* Now we're all done with the if-statement. */
13174 finish_if_stmt (statement);
13176 else
13178 bool in_switch_statement_p;
13179 unsigned char in_statement;
13181 /* Add the condition. */
13182 finish_switch_cond (condition, statement);
13184 /* Parse the body of the switch-statement. */
13185 in_switch_statement_p = parser->in_switch_statement_p;
13186 in_statement = parser->in_statement;
13187 parser->in_switch_statement_p = true;
13188 parser->in_statement |= IN_SWITCH_STMT;
13189 cp_parser_implicitly_scoped_statement (parser, if_p,
13190 guard_tinfo);
13191 parser->in_switch_statement_p = in_switch_statement_p;
13192 parser->in_statement = in_statement;
13194 /* Now we're all done with the switch-statement. */
13195 finish_switch_stmt (statement);
13198 return statement;
13200 break;
13202 default:
13203 cp_parser_error (parser, "expected selection-statement");
13204 return error_mark_node;
13208 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13209 If we have seen at least one decl-specifier, and the next token is not
13210 a parenthesis (after "int (" we might be looking at a functional cast)
13211 neither we are dealing with a concept-check expression then we must be
13212 looking at a declaration. */
13214 static void
13215 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13216 cp_decl_specifier_seq *decl_specs)
13218 if (decl_specs->any_specifiers_p
13219 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13220 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13221 && !cp_parser_error_occurred (parser)
13222 && !(decl_specs->type
13223 && TREE_CODE (decl_specs->type) == TYPE_DECL
13224 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13225 cp_parser_commit_to_tentative_parse (parser);
13228 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13229 The declarator shall not specify a function or an array. Returns
13230 TRUE if the declarator is valid, FALSE otherwise. */
13232 static bool
13233 cp_parser_check_condition_declarator (cp_parser* parser,
13234 cp_declarator *declarator,
13235 location_t loc)
13237 if (declarator == cp_error_declarator
13238 || function_declarator_p (declarator)
13239 || declarator->kind == cdk_array)
13241 if (declarator == cp_error_declarator)
13242 /* Already complained. */;
13243 else if (declarator->kind == cdk_array)
13244 error_at (loc, "condition declares an array");
13245 else
13246 error_at (loc, "condition declares a function");
13247 if (parser->fully_implicit_function_template_p)
13248 abort_fully_implicit_template (parser);
13249 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13250 /*or_comma=*/false,
13251 /*consume_paren=*/false);
13252 return false;
13254 else
13255 return true;
13258 /* Parse a condition.
13260 condition:
13261 expression
13262 type-specifier-seq declarator = initializer-clause
13263 type-specifier-seq declarator braced-init-list
13265 GNU Extension:
13267 condition:
13268 type-specifier-seq declarator asm-specification [opt]
13269 attributes [opt] = assignment-expression
13271 Returns the expression that should be tested. */
13273 static tree
13274 cp_parser_condition (cp_parser* parser)
13276 cp_decl_specifier_seq type_specifiers;
13277 const char *saved_message;
13278 int declares_class_or_enum;
13280 /* Try the declaration first. */
13281 cp_parser_parse_tentatively (parser);
13282 /* New types are not allowed in the type-specifier-seq for a
13283 condition. */
13284 saved_message = parser->type_definition_forbidden_message;
13285 parser->type_definition_forbidden_message
13286 = G_("types may not be defined in conditions");
13287 /* Parse the type-specifier-seq. */
13288 cp_parser_decl_specifier_seq (parser,
13289 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13290 &type_specifiers,
13291 &declares_class_or_enum);
13292 /* Restore the saved message. */
13293 parser->type_definition_forbidden_message = saved_message;
13295 /* Gather the attributes that were provided with the
13296 decl-specifiers. */
13297 tree prefix_attributes = type_specifiers.attributes;
13299 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13301 /* If all is well, we might be looking at a declaration. */
13302 if (!cp_parser_error_occurred (parser))
13304 tree decl;
13305 tree asm_specification;
13306 tree attributes;
13307 cp_declarator *declarator;
13308 tree initializer = NULL_TREE;
13309 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13311 /* Parse the declarator. */
13312 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13313 CP_PARSER_FLAGS_NONE,
13314 /*ctor_dtor_or_conv_p=*/NULL,
13315 /*parenthesized_p=*/NULL,
13316 /*member_p=*/false,
13317 /*friend_p=*/false,
13318 /*static_p=*/false);
13319 /* Parse the attributes. */
13320 attributes = cp_parser_attributes_opt (parser);
13321 /* Parse the asm-specification. */
13322 asm_specification = cp_parser_asm_specification_opt (parser);
13323 /* If the next token is not an `=' or '{', then we might still be
13324 looking at an expression. For example:
13326 if (A(a).x)
13328 looks like a decl-specifier-seq and a declarator -- but then
13329 there is no `=', so this is an expression. */
13330 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13331 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13332 cp_parser_simulate_error (parser);
13334 /* If we did see an `=' or '{', then we are looking at a declaration
13335 for sure. */
13336 if (cp_parser_parse_definitely (parser))
13338 tree pushed_scope;
13339 bool non_constant_p = false;
13340 int flags = LOOKUP_ONLYCONVERTING;
13342 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13343 return error_mark_node;
13345 /* Create the declaration. */
13346 decl = start_decl (declarator, &type_specifiers,
13347 /*initialized_p=*/true,
13348 attributes, prefix_attributes,
13349 &pushed_scope);
13351 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13352 /* Parse the initializer. */
13353 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13355 initializer = cp_parser_braced_list (parser, &non_constant_p);
13356 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13357 flags = 0;
13359 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13361 /* Consume the `='. */
13362 cp_lexer_consume_token (parser->lexer);
13363 initializer = cp_parser_initializer_clause (parser,
13364 &non_constant_p);
13366 else
13368 cp_parser_error (parser, "expected initializer");
13369 initializer = error_mark_node;
13371 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13372 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13374 /* Process the initializer. */
13375 cp_finish_decl (decl,
13376 initializer, !non_constant_p,
13377 asm_specification,
13378 flags);
13380 if (pushed_scope)
13381 pop_scope (pushed_scope);
13383 return convert_from_reference (decl);
13386 /* If we didn't even get past the declarator successfully, we are
13387 definitely not looking at a declaration. */
13388 else
13389 cp_parser_abort_tentative_parse (parser);
13391 /* Otherwise, we are looking at an expression. */
13392 return cp_parser_expression (parser);
13395 /* Parses a for-statement or range-for-statement until the closing ')',
13396 not included. */
13398 static tree
13399 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
13401 tree init, scope, decl;
13402 bool is_range_for;
13404 /* Begin the for-statement. */
13405 scope = begin_for_scope (&init);
13407 /* Maybe parse the optional init-statement in a range-based for loop. */
13408 if (cp_parser_range_based_for_with_init_p (parser)
13409 /* Checked for diagnostic purposes only. */
13410 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13412 tree dummy;
13413 cp_parser_init_statement (parser, &dummy);
13414 if (cxx_dialect < cxx20)
13416 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13417 OPT_Wc__20_extensions,
13418 "range-based %<for%> loops with initializer only "
13419 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13420 decl = error_mark_node;
13424 /* Parse the initialization. */
13425 is_range_for = cp_parser_init_statement (parser, &decl);
13427 if (is_range_for)
13428 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13429 false);
13430 else
13431 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
13434 static tree
13435 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13436 unsigned short unroll)
13438 /* Normal for loop */
13439 tree condition = NULL_TREE;
13440 tree expression = NULL_TREE;
13441 tree stmt;
13443 stmt = begin_for_stmt (scope, init);
13444 /* The init-statement has already been parsed in
13445 cp_parser_init_statement, so no work is needed here. */
13446 finish_init_stmt (stmt);
13448 /* If there's a condition, process it. */
13449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13450 condition = cp_parser_condition (parser);
13451 else if (ivdep)
13453 cp_parser_error (parser, "missing loop condition in loop with "
13454 "%<GCC ivdep%> pragma");
13455 condition = error_mark_node;
13457 else if (unroll)
13459 cp_parser_error (parser, "missing loop condition in loop with "
13460 "%<GCC unroll%> pragma");
13461 condition = error_mark_node;
13463 finish_for_cond (condition, stmt, ivdep, unroll);
13464 /* Look for the `;'. */
13465 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13467 /* If there's an expression, process it. */
13468 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13469 expression = cp_parser_expression (parser);
13470 finish_for_expr (expression, stmt);
13472 return stmt;
13475 /* Tries to parse a range-based for-statement:
13477 range-based-for:
13478 decl-specifier-seq declarator : expression
13480 The decl-specifier-seq declarator and the `:' are already parsed by
13481 cp_parser_init_statement. If processing_template_decl it returns a
13482 newly created RANGE_FOR_STMT; if not, it is converted to a
13483 regular FOR_STMT. */
13485 static tree
13486 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
13487 bool ivdep, unsigned short unroll, bool is_omp)
13489 tree stmt, range_expr;
13490 auto_vec <cxx_binding *, 16> bindings;
13491 auto_vec <tree, 16> names;
13492 tree decomp_first_name = NULL_TREE;
13493 unsigned int decomp_cnt = 0;
13495 /* Get the range declaration momentarily out of the way so that
13496 the range expression doesn't clash with it. */
13497 if (range_decl != error_mark_node)
13499 if (DECL_HAS_VALUE_EXPR_P (range_decl))
13501 tree v = DECL_VALUE_EXPR (range_decl);
13502 /* For decomposition declaration get all of the corresponding
13503 declarations out of the way. */
13504 if (TREE_CODE (v) == ARRAY_REF
13505 && VAR_P (TREE_OPERAND (v, 0))
13506 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
13508 tree d = range_decl;
13509 range_decl = TREE_OPERAND (v, 0);
13510 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
13511 decomp_first_name = d;
13512 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
13514 tree name = DECL_NAME (d);
13515 names.safe_push (name);
13516 bindings.safe_push (IDENTIFIER_BINDING (name));
13517 IDENTIFIER_BINDING (name)
13518 = IDENTIFIER_BINDING (name)->previous;
13522 if (names.is_empty ())
13524 tree name = DECL_NAME (range_decl);
13525 names.safe_push (name);
13526 bindings.safe_push (IDENTIFIER_BINDING (name));
13527 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
13531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13533 bool expr_non_constant_p;
13534 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13536 else
13537 range_expr = cp_parser_expression (parser);
13539 /* Put the range declaration(s) back into scope. */
13540 for (unsigned int i = 0; i < names.length (); i++)
13542 cxx_binding *binding = bindings[i];
13543 binding->previous = IDENTIFIER_BINDING (names[i]);
13544 IDENTIFIER_BINDING (names[i]) = binding;
13547 /* finish_omp_for has its own code for the following, so just
13548 return the range_expr instead. */
13549 if (is_omp)
13550 return range_expr;
13552 /* If in template, STMT is converted to a normal for-statement
13553 at instantiation. If not, it is done just ahead. */
13554 if (processing_template_decl)
13556 if (check_for_bare_parameter_packs (range_expr))
13557 range_expr = error_mark_node;
13558 stmt = begin_range_for_stmt (scope, init);
13559 if (ivdep)
13560 RANGE_FOR_IVDEP (stmt) = 1;
13561 if (unroll)
13562 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
13563 finish_range_for_decl (stmt, range_decl, range_expr);
13564 if (!type_dependent_expression_p (range_expr)
13565 /* do_auto_deduction doesn't mess with template init-lists. */
13566 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
13567 do_range_for_auto_deduction (range_decl, range_expr);
13569 else
13571 stmt = begin_for_stmt (scope, init);
13572 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
13573 decomp_first_name, decomp_cnt, ivdep,
13574 unroll);
13576 return stmt;
13579 /* Subroutine of cp_convert_range_for: given the initializer expression,
13580 builds up the range temporary. */
13582 static tree
13583 build_range_temp (tree range_expr)
13585 /* Find out the type deduced by the declaration
13586 `auto &&__range = range_expr'. */
13587 tree auto_node = make_auto ();
13588 tree range_type = cp_build_reference_type (auto_node, true);
13589 range_type = do_auto_deduction (range_type, range_expr, auto_node);
13591 /* Create the __range variable. */
13592 tree range_temp = build_decl (input_location, VAR_DECL,
13593 for_range__identifier, range_type);
13594 TREE_USED (range_temp) = 1;
13595 DECL_ARTIFICIAL (range_temp) = 1;
13597 return range_temp;
13600 /* Used by cp_parser_range_for in template context: we aren't going to
13601 do a full conversion yet, but we still need to resolve auto in the
13602 type of the for-range-declaration if present. This is basically
13603 a shortcut version of cp_convert_range_for. */
13605 static void
13606 do_range_for_auto_deduction (tree decl, tree range_expr)
13608 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13609 if (auto_node)
13611 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
13612 range_temp = convert_from_reference (build_range_temp (range_expr));
13613 iter_type = (cp_parser_perform_range_for_lookup
13614 (range_temp, &begin_dummy, &end_dummy));
13615 if (iter_type)
13617 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
13618 iter_type);
13619 iter_decl = build_x_indirect_ref (input_location, iter_decl,
13620 RO_UNARY_STAR, NULL_TREE,
13621 tf_warning_or_error);
13622 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
13623 iter_decl, auto_node,
13624 tf_warning_or_error,
13625 adc_variable_type);
13630 /* Warns when the loop variable should be changed to a reference type to
13631 avoid unnecessary copying. I.e., from
13633 for (const auto x : range)
13635 where range returns a reference, to
13637 for (const auto &x : range)
13639 if this version doesn't make a copy.
13641 This function also warns when the loop variable is initialized with
13642 a value of a different type resulting in a copy:
13644 int arr[10];
13645 for (const double &x : arr)
13647 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
13648 This function is never called when processing_template_decl is on. */
13650 static void
13651 warn_for_range_copy (tree decl, tree expr)
13653 if (!warn_range_loop_construct
13654 || decl == error_mark_node)
13655 return;
13657 location_t loc = DECL_SOURCE_LOCATION (decl);
13658 tree type = TREE_TYPE (decl);
13660 if (from_macro_expansion_at (loc))
13661 return;
13663 if (TYPE_REF_P (type))
13665 if (glvalue_p (expr) && !ref_conv_binds_directly_p (type, expr))
13667 auto_diagnostic_group d;
13668 if (warning_at (loc, OPT_Wrange_loop_construct,
13669 "loop variable %qD of type %qT binds to a temporary "
13670 "constructed from type %qT", decl, type,
13671 TREE_TYPE (expr)))
13673 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
13674 TYPE_QUAL_CONST);
13675 ref = cp_build_reference_type (ref, /*rval*/false);
13676 inform (loc, "use non-reference type %qT to make the copy "
13677 "explicit or %qT to prevent copying",
13678 non_reference (type), ref);
13681 return;
13683 else if (!CP_TYPE_CONST_P (type))
13684 return;
13686 /* Since small trivially copyable types are cheap to copy, we suppress the
13687 warning for them. 64B is a common size of a cache line. */
13688 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
13689 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
13690 && trivially_copyable_p (type)))
13691 return;
13693 tree rtype = cp_build_reference_type (type, /*rval*/false);
13694 /* If we could initialize the reference directly, it wouldn't involve any
13695 copies. */
13696 if (!ref_conv_binds_directly_p (rtype, expr))
13697 return;
13699 auto_diagnostic_group d;
13700 if (warning_at (loc, OPT_Wrange_loop_construct,
13701 "loop variable %qD creates a copy from type %qT",
13702 decl, type))
13704 gcc_rich_location richloc (loc);
13705 richloc.add_fixit_insert_before ("&");
13706 inform (&richloc, "use reference type to prevent copying");
13710 /* Converts a range-based for-statement into a normal
13711 for-statement, as per the definition.
13713 for (RANGE_DECL : RANGE_EXPR)
13714 BLOCK
13716 should be equivalent to:
13719 auto &&__range = RANGE_EXPR;
13720 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
13721 __begin != __end;
13722 ++__begin)
13724 RANGE_DECL = *__begin;
13725 BLOCK
13729 If RANGE_EXPR is an array:
13730 BEGIN_EXPR = __range
13731 END_EXPR = __range + ARRAY_SIZE(__range)
13732 Else if RANGE_EXPR has a member 'begin' or 'end':
13733 BEGIN_EXPR = __range.begin()
13734 END_EXPR = __range.end()
13735 Else:
13736 BEGIN_EXPR = begin(__range)
13737 END_EXPR = end(__range);
13739 If __range has a member 'begin' but not 'end', or vice versa, we must
13740 still use the second alternative (it will surely fail, however).
13741 When calling begin()/end() in the third alternative we must use
13742 argument dependent lookup, but always considering 'std' as an associated
13743 namespace. */
13745 tree
13746 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
13747 tree decomp_first_name, unsigned int decomp_cnt,
13748 bool ivdep, unsigned short unroll)
13750 tree begin, end;
13751 tree iter_type, begin_expr, end_expr;
13752 tree condition, expression;
13754 range_expr = mark_lvalue_use (range_expr);
13756 if (range_decl == error_mark_node || range_expr == error_mark_node)
13757 /* If an error happened previously do nothing or else a lot of
13758 unhelpful errors would be issued. */
13759 begin_expr = end_expr = iter_type = error_mark_node;
13760 else
13762 tree range_temp;
13764 if (VAR_P (range_expr)
13765 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
13766 /* Can't bind a reference to an array of runtime bound. */
13767 range_temp = range_expr;
13768 else
13770 range_temp = build_range_temp (range_expr);
13771 pushdecl (range_temp);
13772 cp_finish_decl (range_temp, range_expr,
13773 /*is_constant_init*/false, NULL_TREE,
13774 LOOKUP_ONLYCONVERTING);
13775 range_temp = convert_from_reference (range_temp);
13777 iter_type = cp_parser_perform_range_for_lookup (range_temp,
13778 &begin_expr, &end_expr);
13781 /* The new for initialization statement. */
13782 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
13783 iter_type);
13784 TREE_USED (begin) = 1;
13785 DECL_ARTIFICIAL (begin) = 1;
13786 pushdecl (begin);
13787 cp_finish_decl (begin, begin_expr,
13788 /*is_constant_init*/false, NULL_TREE,
13789 LOOKUP_ONLYCONVERTING);
13791 if (cxx_dialect >= cxx17)
13792 iter_type = cv_unqualified (TREE_TYPE (end_expr));
13793 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
13794 TREE_USED (end) = 1;
13795 DECL_ARTIFICIAL (end) = 1;
13796 pushdecl (end);
13797 cp_finish_decl (end, end_expr,
13798 /*is_constant_init*/false, NULL_TREE,
13799 LOOKUP_ONLYCONVERTING);
13801 finish_init_stmt (statement);
13803 /* The new for condition. */
13804 condition = build_x_binary_op (input_location, NE_EXPR,
13805 begin, ERROR_MARK,
13806 end, ERROR_MARK,
13807 NULL_TREE, NULL, tf_warning_or_error);
13808 finish_for_cond (condition, statement, ivdep, unroll);
13810 /* The new increment expression. */
13811 expression = finish_unary_op_expr (input_location,
13812 PREINCREMENT_EXPR, begin,
13813 tf_warning_or_error);
13814 finish_for_expr (expression, statement);
13816 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13817 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
13819 /* The declaration is initialized with *__begin inside the loop body. */
13820 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
13821 NULL_TREE, tf_warning_or_error);
13822 cp_finish_decl (range_decl, deref_begin,
13823 /*is_constant_init*/false, NULL_TREE,
13824 LOOKUP_ONLYCONVERTING);
13825 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13826 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
13828 warn_for_range_copy (range_decl, deref_begin);
13830 return statement;
13833 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
13834 We need to solve both at the same time because the method used
13835 depends on the existence of members begin or end.
13836 Returns the type deduced for the iterator expression. */
13838 static tree
13839 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
13841 if (error_operand_p (range))
13843 *begin = *end = error_mark_node;
13844 return error_mark_node;
13847 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
13849 error ("range-based %<for%> expression of type %qT "
13850 "has incomplete type", TREE_TYPE (range));
13851 *begin = *end = error_mark_node;
13852 return error_mark_node;
13854 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
13856 /* If RANGE is an array, we will use pointer arithmetic. */
13857 *begin = decay_conversion (range, tf_warning_or_error);
13858 *end = build_binary_op (input_location, PLUS_EXPR,
13859 range,
13860 array_type_nelts_top (TREE_TYPE (range)),
13861 false);
13862 return TREE_TYPE (*begin);
13864 else
13866 /* If it is not an array, we must do a bit of magic. */
13867 tree id_begin, id_end;
13868 tree member_begin, member_end;
13870 *begin = *end = error_mark_node;
13872 id_begin = get_identifier ("begin");
13873 id_end = get_identifier ("end");
13874 member_begin = lookup_member (TREE_TYPE (range), id_begin,
13875 /*protect=*/2, /*want_type=*/false,
13876 tf_warning_or_error);
13877 member_end = lookup_member (TREE_TYPE (range), id_end,
13878 /*protect=*/2, /*want_type=*/false,
13879 tf_warning_or_error);
13881 if (member_begin != NULL_TREE && member_end != NULL_TREE)
13883 /* Use the member functions. */
13884 *begin = cp_parser_range_for_member_function (range, id_begin);
13885 *end = cp_parser_range_for_member_function (range, id_end);
13887 else
13889 /* Use global functions with ADL. */
13890 releasing_vec vec;
13892 vec_safe_push (vec, range);
13894 member_begin = perform_koenig_lookup (id_begin, vec,
13895 tf_warning_or_error);
13896 *begin = finish_call_expr (member_begin, &vec, false, true,
13897 tf_warning_or_error);
13898 member_end = perform_koenig_lookup (id_end, vec,
13899 tf_warning_or_error);
13900 *end = finish_call_expr (member_end, &vec, false, true,
13901 tf_warning_or_error);
13904 /* Last common checks. */
13905 if (*begin == error_mark_node || *end == error_mark_node)
13907 /* If one of the expressions is an error do no more checks. */
13908 *begin = *end = error_mark_node;
13909 return error_mark_node;
13911 else if (type_dependent_expression_p (*begin)
13912 || type_dependent_expression_p (*end))
13913 /* Can happen, when, eg, in a template context, Koenig lookup
13914 can't resolve begin/end (c++/58503). */
13915 return NULL_TREE;
13916 else
13918 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
13919 /* The unqualified type of the __begin and __end temporaries should
13920 be the same, as required by the multiple auto declaration. */
13921 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
13923 if (cxx_dialect >= cxx17
13924 && (build_x_binary_op (input_location, NE_EXPR,
13925 *begin, ERROR_MARK,
13926 *end, ERROR_MARK,
13927 NULL_TREE, NULL, tf_none)
13928 != error_mark_node))
13929 /* P0184R0 allows __begin and __end to have different types,
13930 but make sure they are comparable so we can give a better
13931 diagnostic. */;
13932 else
13933 error ("inconsistent begin/end types in range-based %<for%> "
13934 "statement: %qT and %qT",
13935 TREE_TYPE (*begin), TREE_TYPE (*end));
13937 return iter_type;
13942 /* Helper function for cp_parser_perform_range_for_lookup.
13943 Builds a tree for RANGE.IDENTIFIER(). */
13945 static tree
13946 cp_parser_range_for_member_function (tree range, tree identifier)
13948 tree member, res;
13950 member = finish_class_member_access_expr (range, identifier,
13951 false, tf_warning_or_error);
13952 if (member == error_mark_node)
13953 return error_mark_node;
13955 releasing_vec vec;
13956 res = finish_call_expr (member, &vec,
13957 /*disallow_virtual=*/false,
13958 /*koenig_p=*/false,
13959 tf_warning_or_error);
13960 return res;
13963 /* Parse an iteration-statement.
13965 iteration-statement:
13966 while ( condition ) statement
13967 do statement while ( expression ) ;
13968 for ( init-statement condition [opt] ; expression [opt] )
13969 statement
13971 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
13973 static tree
13974 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
13975 unsigned short unroll)
13977 cp_token *token;
13978 enum rid keyword;
13979 tree statement;
13980 unsigned char in_statement;
13981 token_indent_info guard_tinfo;
13983 /* Peek at the next token. */
13984 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
13985 if (!token)
13986 return error_mark_node;
13988 guard_tinfo = get_token_indent_info (token);
13990 /* Remember whether or not we are already within an iteration
13991 statement. */
13992 in_statement = parser->in_statement;
13994 /* See what kind of keyword it is. */
13995 keyword = token->keyword;
13996 switch (keyword)
13998 case RID_WHILE:
14000 tree condition;
14002 /* Begin the while-statement. */
14003 statement = begin_while_stmt ();
14004 /* Look for the `('. */
14005 matching_parens parens;
14006 parens.require_open (parser);
14007 /* Parse the condition. */
14008 condition = cp_parser_condition (parser);
14009 finish_while_stmt_cond (condition, statement, ivdep, unroll);
14010 /* Look for the `)'. */
14011 parens.require_close (parser);
14012 /* Parse the dependent statement. */
14013 parser->in_statement = IN_ITERATION_STMT;
14014 bool prev = note_iteration_stmt_body_start ();
14015 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14016 note_iteration_stmt_body_end (prev);
14017 parser->in_statement = in_statement;
14018 /* We're done with the while-statement. */
14019 finish_while_stmt (statement);
14021 break;
14023 case RID_DO:
14025 tree expression;
14027 /* Begin the do-statement. */
14028 statement = begin_do_stmt ();
14029 /* Parse the body of the do-statement. */
14030 parser->in_statement = IN_ITERATION_STMT;
14031 bool prev = note_iteration_stmt_body_start ();
14032 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14033 note_iteration_stmt_body_end (prev);
14034 parser->in_statement = in_statement;
14035 finish_do_body (statement);
14036 /* Look for the `while' keyword. */
14037 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14038 /* Look for the `('. */
14039 matching_parens parens;
14040 parens.require_open (parser);
14041 /* Parse the expression. */
14042 expression = cp_parser_expression (parser);
14043 /* We're done with the do-statement. */
14044 finish_do_stmt (expression, statement, ivdep, unroll);
14045 /* Look for the `)'. */
14046 parens.require_close (parser);
14047 /* Look for the `;'. */
14048 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14050 break;
14052 case RID_FOR:
14054 /* Look for the `('. */
14055 matching_parens parens;
14056 parens.require_open (parser);
14058 statement = cp_parser_for (parser, ivdep, unroll);
14060 /* Look for the `)'. */
14061 parens.require_close (parser);
14063 /* Parse the body of the for-statement. */
14064 parser->in_statement = IN_ITERATION_STMT;
14065 bool prev = note_iteration_stmt_body_start ();
14066 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14067 note_iteration_stmt_body_end (prev);
14068 parser->in_statement = in_statement;
14070 /* We're done with the for-statement. */
14071 finish_for_stmt (statement);
14073 break;
14075 default:
14076 cp_parser_error (parser, "expected iteration-statement");
14077 statement = error_mark_node;
14078 break;
14081 return statement;
14084 /* Parse an init-statement or the declarator of a range-based-for.
14085 Returns true if a range-based-for declaration is seen.
14087 init-statement:
14088 expression-statement
14089 simple-declaration
14090 alias-declaration */
14092 static bool
14093 cp_parser_init_statement (cp_parser *parser, tree *decl)
14095 /* If the next token is a `;', then we have an empty
14096 expression-statement. Grammatically, this is also a
14097 simple-declaration, but an invalid one, because it does not
14098 declare anything. Therefore, if we did not handle this case
14099 specially, we would issue an error message about an invalid
14100 declaration. */
14101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14103 bool is_range_for = false;
14104 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14106 /* A colon is used in range-based for. */
14107 parser->colon_corrects_to_scope_p = false;
14109 /* We're going to speculatively look for a declaration, falling back
14110 to an expression, if necessary. */
14111 cp_parser_parse_tentatively (parser);
14112 bool expect_semicolon_p = true;
14113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14115 cp_parser_alias_declaration (parser);
14116 expect_semicolon_p = false;
14117 if (cxx_dialect < cxx23
14118 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14119 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14120 OPT_Wc__23_extensions,
14121 "alias-declaration in init-statement only "
14122 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14124 else
14125 /* Parse the declaration. */
14126 cp_parser_simple_declaration (parser,
14127 /*function_definition_allowed_p=*/false,
14128 decl);
14129 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14130 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14132 /* It is a range-for, consume the ':'. */
14133 cp_lexer_consume_token (parser->lexer);
14134 is_range_for = true;
14135 if (cxx_dialect < cxx11)
14136 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14137 OPT_Wc__11_extensions,
14138 "range-based %<for%> loops only available with "
14139 "%<-std=c++11%> or %<-std=gnu++11%>");
14141 else if (expect_semicolon_p)
14142 /* The ';' is not consumed yet because we told
14143 cp_parser_simple_declaration not to. */
14144 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14146 if (cp_parser_parse_definitely (parser))
14147 return is_range_for;
14148 /* If the tentative parse failed, then we shall need to look for an
14149 expression-statement. */
14151 /* If we are here, it is an expression-statement. */
14152 cp_parser_expression_statement (parser, NULL_TREE);
14153 return false;
14156 /* Parse a jump-statement.
14158 jump-statement:
14159 break ;
14160 continue ;
14161 return expression [opt] ;
14162 return braced-init-list ;
14163 coroutine-return-statement;
14164 goto identifier ;
14166 GNU extension:
14168 jump-statement:
14169 goto * expression ;
14171 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14173 static tree
14174 cp_parser_jump_statement (cp_parser* parser)
14176 tree statement = error_mark_node;
14177 cp_token *token;
14178 enum rid keyword;
14179 unsigned char in_statement;
14181 /* Peek at the next token. */
14182 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14183 if (!token)
14184 return error_mark_node;
14186 /* See what kind of keyword it is. */
14187 keyword = token->keyword;
14188 switch (keyword)
14190 case RID_BREAK:
14191 in_statement = parser->in_statement & ~IN_IF_STMT;
14192 switch (in_statement)
14194 case 0:
14195 error_at (token->location, "break statement not within loop or switch");
14196 break;
14197 default:
14198 gcc_assert ((in_statement & IN_SWITCH_STMT)
14199 || in_statement == IN_ITERATION_STMT);
14200 statement = finish_break_stmt ();
14201 if (in_statement == IN_ITERATION_STMT)
14202 break_maybe_infinite_loop ();
14203 break;
14204 case IN_OMP_BLOCK:
14205 error_at (token->location, "invalid exit from OpenMP structured block");
14206 break;
14207 case IN_OMP_FOR:
14208 error_at (token->location, "break statement used with OpenMP for loop");
14209 break;
14211 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14212 break;
14214 case RID_CONTINUE:
14215 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14217 case 0:
14218 error_at (token->location, "continue statement not within a loop");
14219 break;
14220 /* Fall through. */
14221 case IN_ITERATION_STMT:
14222 case IN_OMP_FOR:
14223 statement = finish_continue_stmt ();
14224 break;
14225 case IN_OMP_BLOCK:
14226 error_at (token->location, "invalid exit from OpenMP structured block");
14227 break;
14228 default:
14229 gcc_unreachable ();
14231 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14232 break;
14234 case RID_CO_RETURN:
14235 case RID_RETURN:
14237 tree expr;
14238 bool expr_non_constant_p;
14240 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14242 cp_lexer_set_source_position (parser->lexer);
14243 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14244 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
14246 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14247 expr = cp_parser_expression (parser);
14248 else
14249 /* If the next token is a `;', then there is no
14250 expression. */
14251 expr = NULL_TREE;
14252 /* Build the return-statement, check co-return first, since type
14253 deduction is not valid there. */
14254 if (keyword == RID_CO_RETURN)
14255 statement = finish_co_return_stmt (token->location, expr);
14256 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14257 /* Don't deduce from a discarded return statement. */;
14258 else
14259 statement = finish_return_stmt (expr);
14260 /* Look for the final `;'. */
14261 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14263 break;
14265 case RID_GOTO:
14266 if (parser->in_function_body
14267 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14268 && cxx_dialect < cxx23)
14270 error ("%<goto%> in %<constexpr%> function only available with "
14271 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14272 cp_function_chain->invalid_constexpr = true;
14275 /* Create the goto-statement. */
14276 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14278 /* Issue a warning about this use of a GNU extension. */
14279 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14280 /* Consume the '*' token. */
14281 cp_lexer_consume_token (parser->lexer);
14282 /* Parse the dependent expression. */
14283 finish_goto_stmt (cp_parser_expression (parser));
14285 else
14286 finish_goto_stmt (cp_parser_identifier (parser));
14287 /* Look for the final `;'. */
14288 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14289 break;
14291 default:
14292 cp_parser_error (parser, "expected jump-statement");
14293 break;
14296 return statement;
14299 /* Parse a declaration-statement.
14301 declaration-statement:
14302 block-declaration */
14304 static void
14305 cp_parser_declaration_statement (cp_parser* parser)
14307 void *p;
14309 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14310 p = obstack_alloc (&declarator_obstack, 0);
14312 /* Parse the block-declaration. */
14313 cp_parser_block_declaration (parser, /*statement_p=*/true);
14315 /* Free any declarators allocated. */
14316 obstack_free (&declarator_obstack, p);
14319 /* Some dependent statements (like `if (cond) statement'), are
14320 implicitly in their own scope. In other words, if the statement is
14321 a single statement (as opposed to a compound-statement), it is
14322 none-the-less treated as if it were enclosed in braces. Any
14323 declarations appearing in the dependent statement are out of scope
14324 after control passes that point. This function parses a statement,
14325 but ensures that is in its own scope, even if it is not a
14326 compound-statement.
14328 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14329 is a (possibly labeled) if statement which is not enclosed in
14330 braces and has an else clause. This is used to implement
14331 -Wparentheses.
14333 CHAIN is a vector of if-else-if conditions. This is used to implement
14334 -Wduplicated-cond.
14336 Returns the new statement. */
14338 static tree
14339 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14340 const token_indent_info &guard_tinfo,
14341 vec<tree> *chain)
14343 tree statement;
14344 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14345 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14346 token_indent_info body_tinfo
14347 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14349 if (if_p != NULL)
14350 *if_p = false;
14352 /* Mark if () ; with a special NOP_EXPR. */
14353 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14355 cp_lexer_consume_token (parser->lexer);
14356 statement = add_stmt (build_empty_stmt (body_loc));
14358 if (guard_tinfo.keyword == RID_IF
14359 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14360 warning_at (body_loc, OPT_Wempty_body,
14361 "suggest braces around empty body in an %<if%> statement");
14362 else if (guard_tinfo.keyword == RID_ELSE)
14363 warning_at (body_loc, OPT_Wempty_body,
14364 "suggest braces around empty body in an %<else%> statement");
14366 /* if a compound is opened, we simply parse the statement directly. */
14367 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14368 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14369 /* If the token is not a `{', then we must take special action. */
14370 else
14372 /* Create a compound-statement. */
14373 statement = begin_compound_stmt (0);
14374 /* Parse the dependent-statement. */
14375 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14376 &body_loc_after_labels);
14377 /* Finish the dummy compound-statement. */
14378 finish_compound_stmt (statement);
14381 token_indent_info next_tinfo
14382 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14383 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14385 if (body_loc_after_labels != UNKNOWN_LOCATION
14386 && next_tinfo.type != CPP_SEMICOLON)
14387 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14388 guard_tinfo.location, guard_tinfo.keyword);
14390 /* Return the statement. */
14391 return statement;
14394 /* For some dependent statements (like `while (cond) statement'), we
14395 have already created a scope. Therefore, even if the dependent
14396 statement is a compound-statement, we do not want to create another
14397 scope. */
14399 static void
14400 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14401 const token_indent_info &guard_tinfo)
14403 /* If the token is a `{', then we must take special action. */
14404 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14406 token_indent_info body_tinfo
14407 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14408 location_t loc_after_labels = UNKNOWN_LOCATION;
14410 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14411 &loc_after_labels);
14412 token_indent_info next_tinfo
14413 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14414 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14416 if (loc_after_labels != UNKNOWN_LOCATION
14417 && next_tinfo.type != CPP_SEMICOLON)
14418 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14419 guard_tinfo.location,
14420 guard_tinfo.keyword);
14422 else
14424 /* Avoid calling cp_parser_compound_statement, so that we
14425 don't create a new scope. Do everything else by hand. */
14426 matching_braces braces;
14427 braces.require_open (parser);
14428 /* If the next keyword is `__label__' we have a label declaration. */
14429 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14430 cp_parser_label_declaration (parser);
14431 /* Parse an (optional) statement-seq. */
14432 cp_parser_statement_seq_opt (parser, NULL_TREE);
14433 braces.require_close (parser);
14437 /* Modules */
14439 /* Parse a module-name,
14440 identifier
14441 module-name . identifier
14442 header-name
14444 Returns a pointer to module object, NULL. */
14446 static module_state *
14447 cp_parser_module_name (cp_parser *parser)
14449 cp_token *token = cp_lexer_peek_token (parser->lexer);
14450 if (token->type == CPP_HEADER_NAME)
14452 cp_lexer_consume_token (parser->lexer);
14454 return get_module (token->u.value);
14457 module_state *parent = NULL;
14458 bool partitioned = false;
14459 if (token->type == CPP_COLON && named_module_p ())
14461 partitioned = true;
14462 cp_lexer_consume_token (parser->lexer);
14465 for (;;)
14467 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
14469 cp_parser_error (parser, "expected module-name");
14470 break;
14473 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
14474 parent = get_module (name, parent, partitioned);
14475 token = cp_lexer_peek_token (parser->lexer);
14476 if (!partitioned && token->type == CPP_COLON)
14477 partitioned = true;
14478 else if (token->type != CPP_DOT)
14479 break;
14481 cp_lexer_consume_token (parser->lexer);
14484 return parent;
14487 /* Named module-declaration
14488 __module ; PRAGMA_EOL
14489 __module private ; PRAGMA_EOL (unimplemented)
14490 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14493 static module_parse
14494 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
14495 bool exporting)
14497 /* We're a pseudo pragma. */
14498 parser->lexer->in_pragma = true;
14499 cp_token *token = cp_lexer_consume_token (parser->lexer);
14501 if (flag_header_unit)
14503 error_at (token->location,
14504 "module-declaration not permitted in header-unit");
14505 goto skip_eol;
14507 else if (mp_state == MP_FIRST && !exporting
14508 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14510 /* Start global module fragment. */
14511 cp_lexer_consume_token (parser->lexer);
14512 module_kind |= MK_GLOBAL;
14513 mp_state = MP_GLOBAL;
14514 cp_parser_require_pragma_eol (parser, token);
14516 else if (!exporting
14517 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14518 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
14519 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
14521 cp_lexer_consume_token (parser->lexer);
14522 cp_lexer_consume_token (parser->lexer);
14523 cp_lexer_consume_token (parser->lexer);
14524 cp_parser_require_pragma_eol (parser, token);
14526 if (!(mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
14527 || !module_interface_p () || module_partition_p ())
14528 error_at (token->location,
14529 "private module fragment only permitted in purview"
14530 " of module interface or partition");
14531 else
14533 mp_state = MP_PRIVATE_IMPORTS;
14534 sorry_at (token->location, "private module fragment");
14537 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
14539 /* Neither the first declaration, nor in a GMF. */
14540 error_at (token->location, "module-declaration only permitted as first"
14541 " declaration, or ending a global module fragment");
14542 skip_eol:
14543 cp_parser_skip_to_pragma_eol (parser, token);
14545 else
14547 module_state *mod = cp_parser_module_name (parser);
14548 tree attrs = cp_parser_attributes_opt (parser);
14550 mp_state = MP_PURVIEW_IMPORTS;
14551 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14552 goto skip_eol;
14554 declare_module (mod, token->location, exporting, attrs, parse_in);
14555 cp_parser_require_pragma_eol (parser, token);
14558 return mp_state;
14561 /* Import-declaration
14562 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14564 static void
14565 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
14566 bool exporting)
14568 /* We're a pseudo pragma. */
14569 parser->lexer->in_pragma = true;
14570 cp_token *token = cp_lexer_consume_token (parser->lexer);
14572 if (mp_state != MP_PURVIEW_IMPORTS
14573 && mp_state != MP_PRIVATE_IMPORTS
14574 && module_purview_p ()
14575 && !global_purview_p ())
14577 error_at (token->location, "post-module-declaration"
14578 " imports must be contiguous");
14579 note_lexer:
14580 inform (token->location, "perhaps insert a line break, or other"
14581 " disambiguation, to prevent this being considered a"
14582 " module control-line");
14583 skip_eol:
14584 cp_parser_skip_to_pragma_eol (parser, token);
14586 else if (current_scope () != global_namespace)
14588 error_at (token->location, "import-declaration must be at global scope");
14589 goto note_lexer;
14591 else
14593 module_state *mod = cp_parser_module_name (parser);
14594 tree attrs = cp_parser_attributes_opt (parser);
14596 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14597 goto skip_eol;
14598 cp_parser_require_pragma_eol (parser, token);
14600 if (parser->in_unbraced_linkage_specification_p)
14601 error_at (token->location, "import cannot appear directly in"
14602 " a linkage-specification");
14604 /* Module-purview imports must not be from source inclusion
14605 [cpp.import]/7 */
14606 if (attrs && module_purview_p () && !global_purview_p ()
14607 && private_lookup_attribute ("__translated",
14608 strlen ("__translated"), attrs))
14609 error_at (token->location, "post-module-declaration imports"
14610 " must not be include-translated");
14611 else if ((mp_state == MP_PURVIEW_IMPORTS
14612 || mp_state == MP_PRIVATE_IMPORTS)
14613 && !token->main_source_p)
14614 error_at (token->location, "post-module-declaration imports"
14615 " must not be from header inclusion");
14617 import_module (mod, token->location, exporting, attrs, parse_in);
14621 /* export-declaration.
14623 export declaration
14624 export { declaration-seq-opt } */
14626 static void
14627 cp_parser_module_export (cp_parser *parser)
14629 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
14630 cp_token *token = cp_lexer_consume_token (parser->lexer);
14632 if (!module_interface_p ())
14633 error_at (token->location,
14634 "%qE may only occur after a module interface declaration",
14635 token->u.value);
14637 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
14639 unsigned mk = module_kind;
14640 if (module_exporting_p ())
14641 error_at (token->location,
14642 "%qE may only occur once in an export declaration",
14643 token->u.value);
14644 module_kind |= MK_EXPORTING;
14646 if (braced)
14648 cp_ensure_no_omp_declare_simd (parser);
14649 cp_ensure_no_oacc_routine (parser);
14651 cp_lexer_consume_token (parser->lexer);
14652 cp_parser_declaration_seq_opt (parser);
14653 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14655 else
14657 /* Explicitly check if the next tokens might be a
14658 module-directive line, so we can give a clearer error message
14659 about why the directive will be rejected. */
14660 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
14661 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
14662 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
14663 error_at (token->location, "%<export%> not part of following"
14664 " module-directive");
14665 cp_parser_declaration (parser, NULL_TREE);
14668 module_kind = mk;
14671 /* Declarations [gram.dcl.dcl] */
14673 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
14674 is the top-level declaration sequence. That affects whether we
14675 deal with module-preamble.
14677 declaration-seq:
14678 declaration
14679 declaration-seq declaration */
14681 static void
14682 cp_parser_declaration_seq_opt (cp_parser* parser)
14684 while (true)
14686 cp_token *token = cp_lexer_peek_token (parser->lexer);
14688 if (token->type == CPP_CLOSE_BRACE
14689 || token->type == CPP_EOF)
14690 break;
14691 else
14692 cp_parser_toplevel_declaration (parser);
14696 /* Parse a declaration.
14698 declaration:
14699 block-declaration
14700 function-definition
14701 template-declaration
14702 explicit-instantiation
14703 explicit-specialization
14704 linkage-specification
14705 namespace-definition
14707 C++17:
14708 deduction-guide
14710 modules:
14711 (all these are only allowed at the outermost level, check
14712 that semantically, for better diagnostics)
14713 module-declaration
14714 module-export-declaration
14715 module-import-declaration
14716 export-declaration
14718 GNU extension:
14720 declaration:
14721 __extension__ declaration */
14723 static void
14724 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
14726 int saved_pedantic;
14728 /* Check for the `__extension__' keyword. */
14729 if (cp_parser_extension_opt (parser, &saved_pedantic))
14731 /* Parse the qualified declaration. */
14732 cp_parser_declaration (parser, prefix_attrs);
14733 /* Restore the PEDANTIC flag. */
14734 pedantic = saved_pedantic;
14736 return;
14739 /* Try to figure out what kind of declaration is present. */
14740 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14741 cp_token *token2 = (token1->type == CPP_EOF
14742 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14744 if (token1->type == CPP_SEMICOLON)
14746 cp_lexer_consume_token (parser->lexer);
14747 /* A declaration consisting of a single semicolon is invalid
14748 * before C++11. Allow it unless we're being pedantic. */
14749 if (cxx_dialect < cxx11)
14750 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
14751 return;
14753 else if (cp_lexer_nth_token_is (parser->lexer,
14754 cp_parser_skip_std_attribute_spec_seq (parser,
14756 CPP_SEMICOLON))
14758 location_t attrs_loc = token1->location;
14759 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
14761 if (std_attrs && (flag_openmp || flag_openmp_simd))
14763 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
14764 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
14765 std_attrs);
14766 if (parser->lexer->in_omp_attribute_pragma)
14768 cp_lexer *lexer = parser->lexer;
14769 while (parser->lexer->in_omp_attribute_pragma)
14771 gcc_assert (cp_lexer_next_token_is (parser->lexer,
14772 CPP_PRAGMA));
14773 cp_parser_pragma (parser, pragma_external, NULL);
14775 cp_lexer_destroy (lexer);
14779 if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs))
14780 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
14781 OPT_Wattributes, "attribute ignored");
14782 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14783 cp_lexer_consume_token (parser->lexer);
14784 return;
14787 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14788 void *p = obstack_alloc (&declarator_obstack, 0);
14790 tree attributes = NULL_TREE;
14792 /* Conditionally, allow attributes to precede a linkage specification. */
14793 if (token1->keyword == RID_ATTRIBUTE)
14795 cp_lexer_save_tokens (parser->lexer);
14796 attributes = cp_parser_attributes_opt (parser);
14797 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
14798 cp_token *t2 = (t1->type == CPP_EOF
14799 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14800 if (t1->keyword == RID_EXTERN
14801 && cp_parser_is_pure_string_literal (t2))
14803 cp_lexer_commit_tokens (parser->lexer);
14804 /* We might have already been here. */
14805 if (!c_dialect_objc ())
14807 location_t where = get_finish (t2->location);
14808 warning_at (token1->location, OPT_Wattributes, "attributes are"
14809 " not permitted in this position");
14810 where = linemap_position_for_loc_and_offset (line_table,
14811 where, 1);
14812 inform (where, "attributes may be inserted here");
14813 attributes = NULL_TREE;
14815 token1 = t1;
14816 token2 = t2;
14818 else
14820 cp_lexer_rollback_tokens (parser->lexer);
14821 attributes = NULL_TREE;
14824 /* If we already had some attributes, and we've added more, then prepend.
14825 Otherwise attributes just contains any that we just read. */
14826 if (prefix_attrs)
14828 if (attributes)
14829 TREE_CHAIN (prefix_attrs) = attributes;
14830 attributes = prefix_attrs;
14833 /* If the next token is `extern' and the following token is a string
14834 literal, then we have a linkage specification. */
14835 if (token1->keyword == RID_EXTERN
14836 && cp_parser_is_pure_string_literal (token2))
14837 cp_parser_linkage_specification (parser, attributes);
14838 /* If the next token is `template', then we have either a template
14839 declaration, an explicit instantiation, or an explicit
14840 specialization. */
14841 else if (token1->keyword == RID_TEMPLATE)
14843 /* `template <>' indicates a template specialization. */
14844 if (token2->type == CPP_LESS
14845 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14846 cp_parser_explicit_specialization (parser);
14847 /* `template <' indicates a template declaration. */
14848 else if (token2->type == CPP_LESS)
14849 cp_parser_template_declaration (parser, /*member_p=*/false);
14850 /* Anything else must be an explicit instantiation. */
14851 else
14852 cp_parser_explicit_instantiation (parser);
14854 /* If the next token is `export', it's new-style modules or
14855 old-style template. */
14856 else if (token1->keyword == RID_EXPORT)
14858 if (!modules_p ())
14859 cp_parser_template_declaration (parser, /*member_p=*/false);
14860 else
14861 cp_parser_module_export (parser);
14863 else if (token1->keyword == RID__EXPORT
14864 || token1->keyword == RID__IMPORT
14865 || token1->keyword == RID__MODULE)
14867 bool exporting = token1->keyword == RID__EXPORT;
14868 cp_token *next = exporting ? token2 : token1;
14869 if (exporting)
14870 cp_lexer_consume_token (parser->lexer);
14871 if (next->keyword == RID__MODULE)
14872 cp_parser_module_declaration (parser, MP_NOT_MODULE, exporting);
14873 else
14874 cp_parser_import_declaration (parser, MP_NOT_MODULE, exporting);
14876 /* If the next token is `extern', 'static' or 'inline' and the one
14877 after that is `template', we have a GNU extended explicit
14878 instantiation directive. */
14879 else if (cp_parser_allow_gnu_extensions_p (parser)
14880 && token2->keyword == RID_TEMPLATE
14881 && (token1->keyword == RID_EXTERN
14882 || token1->keyword == RID_STATIC
14883 || token1->keyword == RID_INLINE))
14884 cp_parser_explicit_instantiation (parser);
14885 /* If the next token is `namespace', check for a named or unnamed
14886 namespace definition. */
14887 else if (token1->keyword == RID_NAMESPACE
14888 && (/* A named namespace definition. */
14889 (token2->type == CPP_NAME
14890 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
14891 != CPP_EQ))
14892 || (token2->type == CPP_OPEN_SQUARE
14893 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
14894 == CPP_OPEN_SQUARE)
14895 /* An unnamed namespace definition. */
14896 || token2->type == CPP_OPEN_BRACE
14897 || token2->keyword == RID_ATTRIBUTE))
14898 cp_parser_namespace_definition (parser);
14899 /* An inline (associated) namespace definition. */
14900 else if (token2->keyword == RID_NAMESPACE
14901 && token1->keyword == RID_INLINE)
14902 cp_parser_namespace_definition (parser);
14903 /* Objective-C++ declaration/definition. */
14904 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
14905 cp_parser_objc_declaration (parser, attributes);
14906 else if (c_dialect_objc ()
14907 && token1->keyword == RID_ATTRIBUTE
14908 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
14909 cp_parser_objc_declaration (parser, attributes);
14910 /* At this point we may have a template declared by a concept
14911 introduction. */
14912 else if (flag_concepts
14913 && cp_parser_template_declaration_after_export (parser,
14914 /*member_p=*/false))
14915 /* We did. */;
14916 else
14917 /* Try to parse a block-declaration, or a function-definition. */
14918 cp_parser_block_declaration (parser, /*statement_p=*/false);
14920 /* Free any declarators allocated. */
14921 obstack_free (&declarator_obstack, p);
14924 /* Parse a namespace-scope declaration. */
14926 static void
14927 cp_parser_toplevel_declaration (cp_parser* parser)
14929 cp_token *token = cp_lexer_peek_token (parser->lexer);
14931 if (token->type == CPP_PRAGMA)
14932 /* A top-level declaration can consist solely of a #pragma. A
14933 nested declaration cannot, so this is done here and not in
14934 cp_parser_declaration. (A #pragma at block scope is
14935 handled in cp_parser_statement.) */
14936 cp_parser_pragma (parser, pragma_external, NULL);
14937 else
14938 /* Parse the declaration itself. */
14939 cp_parser_declaration (parser, NULL_TREE);
14942 /* Parse a block-declaration.
14944 block-declaration:
14945 simple-declaration
14946 asm-definition
14947 namespace-alias-definition
14948 using-declaration
14949 using-directive
14951 GNU Extension:
14953 block-declaration:
14954 __extension__ block-declaration
14956 C++0x Extension:
14958 block-declaration:
14959 static_assert-declaration
14961 If STATEMENT_P is TRUE, then this block-declaration is occurring as
14962 part of a declaration-statement. */
14964 static void
14965 cp_parser_block_declaration (cp_parser *parser,
14966 bool statement_p)
14968 int saved_pedantic;
14970 /* Check for the `__extension__' keyword. */
14971 if (cp_parser_extension_opt (parser, &saved_pedantic))
14973 /* Parse the qualified declaration. */
14974 cp_parser_block_declaration (parser, statement_p);
14975 /* Restore the PEDANTIC flag. */
14976 pedantic = saved_pedantic;
14978 return;
14981 /* Peek at the next token to figure out which kind of declaration is
14982 present. */
14983 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14984 size_t attr_idx;
14986 /* If the next keyword is `asm', we have an asm-definition. */
14987 if (token1->keyword == RID_ASM)
14989 if (statement_p)
14990 cp_parser_commit_to_tentative_parse (parser);
14991 cp_parser_asm_definition (parser);
14993 /* If the next keyword is `namespace', we have a
14994 namespace-alias-definition. */
14995 else if (token1->keyword == RID_NAMESPACE)
14996 cp_parser_namespace_alias_definition (parser);
14997 /* If the next keyword is `using', we have a
14998 using-declaration, a using-directive, or an alias-declaration. */
14999 else if (token1->keyword == RID_USING)
15001 cp_token *token2;
15003 if (statement_p)
15004 cp_parser_commit_to_tentative_parse (parser);
15005 /* If the token after `using' is `namespace', then we have a
15006 using-directive. */
15007 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15008 if (token2->keyword == RID_NAMESPACE)
15009 cp_parser_using_directive (parser);
15010 else if (token2->keyword == RID_ENUM)
15011 cp_parser_using_enum (parser);
15012 /* If the second token after 'using' is '=', then we have an
15013 alias-declaration. */
15014 else if (cxx_dialect >= cxx11
15015 && token2->type == CPP_NAME
15016 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15017 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15018 cp_parser_alias_declaration (parser);
15019 /* Otherwise, it's a using-declaration. */
15020 else
15021 cp_parser_using_declaration (parser,
15022 /*access_declaration_p=*/false);
15024 /* If the next keyword is `__label__' we have a misplaced label
15025 declaration. */
15026 else if (token1->keyword == RID_LABEL)
15028 cp_lexer_consume_token (parser->lexer);
15029 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15030 cp_parser_skip_to_end_of_statement (parser);
15031 /* If the next token is now a `;', consume it. */
15032 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15033 cp_lexer_consume_token (parser->lexer);
15035 /* If the next token is `static_assert' we have a static assertion. */
15036 else if (token1->keyword == RID_STATIC_ASSERT)
15037 cp_parser_static_assert (parser, /*member_p=*/false);
15038 /* If the next tokens after attributes is `using namespace', then we have
15039 a using-directive. */
15040 else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
15041 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx,
15042 RID_USING)
15043 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15044 RID_NAMESPACE))
15046 if (statement_p)
15047 cp_parser_commit_to_tentative_parse (parser);
15048 cp_parser_using_directive (parser);
15050 /* Anything else must be a simple-declaration. */
15051 else
15052 cp_parser_simple_declaration (parser, !statement_p,
15053 /*maybe_range_for_decl*/NULL);
15056 /* Parse a simple-declaration.
15058 simple-declaration:
15059 decl-specifier-seq [opt] init-declarator-list [opt] ;
15060 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15061 brace-or-equal-initializer ;
15063 init-declarator-list:
15064 init-declarator
15065 init-declarator-list , init-declarator
15067 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15068 function-definition as a simple-declaration.
15070 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15071 parsed declaration if it is an uninitialized single declarator not followed
15072 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15073 if present, will not be consumed. */
15075 static void
15076 cp_parser_simple_declaration (cp_parser* parser,
15077 bool function_definition_allowed_p,
15078 tree *maybe_range_for_decl)
15080 cp_decl_specifier_seq decl_specifiers;
15081 int declares_class_or_enum;
15082 bool saw_declarator;
15083 location_t comma_loc = UNKNOWN_LOCATION;
15084 location_t init_loc = UNKNOWN_LOCATION;
15086 if (maybe_range_for_decl)
15087 *maybe_range_for_decl = NULL_TREE;
15089 /* Defer access checks until we know what is being declared; the
15090 checks for names appearing in the decl-specifier-seq should be
15091 done as if we were in the scope of the thing being declared. */
15092 push_deferring_access_checks (dk_deferred);
15094 /* Parse the decl-specifier-seq. We have to keep track of whether
15095 or not the decl-specifier-seq declares a named class or
15096 enumeration type, since that is the only case in which the
15097 init-declarator-list is allowed to be empty.
15099 [dcl.dcl]
15101 In a simple-declaration, the optional init-declarator-list can be
15102 omitted only when declaring a class or enumeration, that is when
15103 the decl-specifier-seq contains either a class-specifier, an
15104 elaborated-type-specifier, or an enum-specifier. */
15105 cp_parser_decl_specifier_seq (parser,
15106 CP_PARSER_FLAGS_OPTIONAL,
15107 &decl_specifiers,
15108 &declares_class_or_enum);
15109 /* We no longer need to defer access checks. */
15110 stop_deferring_access_checks ();
15112 cp_omp_declare_simd_data odsd;
15113 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15114 cp_parser_handle_directive_omp_attributes (parser,
15115 &decl_specifiers.attributes,
15116 &odsd, true);
15118 /* In a block scope, a valid declaration must always have a
15119 decl-specifier-seq. By not trying to parse declarators, we can
15120 resolve the declaration/expression ambiguity more quickly. */
15121 if (!function_definition_allowed_p
15122 && !decl_specifiers.any_specifiers_p)
15124 cp_parser_error (parser, "expected declaration");
15125 goto done;
15128 /* If the next two tokens are both identifiers, the code is
15129 erroneous. The usual cause of this situation is code like:
15131 T t;
15133 where "T" should name a type -- but does not. */
15134 if (!decl_specifiers.any_type_specifiers_p
15135 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15137 /* If parsing tentatively, we should commit; we really are
15138 looking at a declaration. */
15139 cp_parser_commit_to_tentative_parse (parser);
15140 /* Give up. */
15141 goto done;
15144 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15146 /* Look for C++17 decomposition declaration. */
15147 for (size_t n = 1; ; n++)
15148 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15149 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15150 continue;
15151 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15152 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15153 && decl_specifiers.any_specifiers_p)
15155 tree decl
15156 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15157 maybe_range_for_decl,
15158 &init_loc);
15160 /* The next token should be either a `,' or a `;'. */
15161 cp_token *token = cp_lexer_peek_token (parser->lexer);
15162 /* If it's a `;', we are done. */
15163 if (token->type == CPP_SEMICOLON)
15164 goto finish;
15165 else if (maybe_range_for_decl)
15167 if (*maybe_range_for_decl == NULL_TREE)
15168 *maybe_range_for_decl = error_mark_node;
15169 goto finish;
15171 /* Anything else is an error. */
15172 else
15174 /* If we have already issued an error message we don't need
15175 to issue another one. */
15176 if ((decl != error_mark_node
15177 && DECL_INITIAL (decl) != error_mark_node)
15178 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15179 cp_parser_error (parser, "expected %<;%>");
15180 /* Skip tokens until we reach the end of the statement. */
15181 cp_parser_skip_to_end_of_statement (parser);
15182 /* If the next token is now a `;', consume it. */
15183 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15184 cp_lexer_consume_token (parser->lexer);
15185 goto done;
15188 else
15189 break;
15191 tree last_type;
15192 bool auto_specifier_p;
15193 /* NULL_TREE if both variable and function declaration are allowed,
15194 error_mark_node if function declaration are not allowed and
15195 a FUNCTION_DECL that should be diagnosed if it is followed by
15196 variable declarations. */
15197 tree auto_function_declaration;
15199 last_type = NULL_TREE;
15200 auto_specifier_p
15201 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15202 auto_function_declaration = NULL_TREE;
15204 /* Keep going until we hit the `;' at the end of the simple
15205 declaration. */
15206 saw_declarator = false;
15207 while (cp_lexer_next_token_is_not (parser->lexer,
15208 CPP_SEMICOLON))
15210 cp_token *token;
15211 bool function_definition_p;
15212 tree decl;
15213 tree auto_result = NULL_TREE;
15215 if (saw_declarator)
15217 /* If we are processing next declarator, comma is expected */
15218 token = cp_lexer_peek_token (parser->lexer);
15219 gcc_assert (token->type == CPP_COMMA);
15220 cp_lexer_consume_token (parser->lexer);
15221 if (maybe_range_for_decl)
15223 *maybe_range_for_decl = error_mark_node;
15224 if (comma_loc == UNKNOWN_LOCATION)
15225 comma_loc = token->location;
15228 else
15229 saw_declarator = true;
15231 /* Parse the init-declarator. */
15232 decl = cp_parser_init_declarator (parser,
15233 CP_PARSER_FLAGS_NONE,
15234 &decl_specifiers,
15235 /*checks=*/NULL,
15236 function_definition_allowed_p,
15237 /*member_p=*/false,
15238 declares_class_or_enum,
15239 &function_definition_p,
15240 maybe_range_for_decl,
15241 &init_loc,
15242 &auto_result);
15243 /* If an error occurred while parsing tentatively, exit quickly.
15244 (That usually happens when in the body of a function; each
15245 statement is treated as a declaration-statement until proven
15246 otherwise.) */
15247 if (cp_parser_error_occurred (parser))
15248 goto done;
15250 if (auto_specifier_p && cxx_dialect >= cxx14)
15252 /* If the init-declarator-list contains more than one
15253 init-declarator, they shall all form declarations of
15254 variables. */
15255 if (auto_function_declaration == NULL_TREE)
15256 auto_function_declaration
15257 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
15258 else if (TREE_CODE (decl) == FUNCTION_DECL
15259 || auto_function_declaration != error_mark_node)
15261 error_at (decl_specifiers.locations[ds_type_spec],
15262 "non-variable %qD in declaration with more than one "
15263 "declarator with placeholder type",
15264 TREE_CODE (decl) == FUNCTION_DECL
15265 ? decl : auto_function_declaration);
15266 auto_function_declaration = error_mark_node;
15270 if (auto_result
15271 && (!processing_template_decl || !type_uses_auto (auto_result)))
15273 if (last_type
15274 && last_type != error_mark_node
15275 && !same_type_p (auto_result, last_type))
15277 /* If the list of declarators contains more than one declarator,
15278 the type of each declared variable is determined as described
15279 above. If the type deduced for the template parameter U is not
15280 the same in each deduction, the program is ill-formed. */
15281 error_at (decl_specifiers.locations[ds_type_spec],
15282 "inconsistent deduction for %qT: %qT and then %qT",
15283 decl_specifiers.type, last_type, auto_result);
15284 last_type = error_mark_node;
15286 else
15287 last_type = auto_result;
15290 /* Handle function definitions specially. */
15291 if (function_definition_p)
15293 /* If the next token is a `,', then we are probably
15294 processing something like:
15296 void f() {}, *p;
15298 which is erroneous. */
15299 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15301 cp_token *token = cp_lexer_peek_token (parser->lexer);
15302 error_at (token->location,
15303 "mixing"
15304 " declarations and function-definitions is forbidden");
15306 /* Otherwise, we're done with the list of declarators. */
15307 else
15309 pop_deferring_access_checks ();
15310 cp_finalize_omp_declare_simd (parser, &odsd);
15311 return;
15314 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15315 *maybe_range_for_decl = decl;
15316 /* The next token should be either a `,' or a `;'. */
15317 token = cp_lexer_peek_token (parser->lexer);
15318 /* If it's a `,', there are more declarators to come. */
15319 if (token->type == CPP_COMMA)
15320 /* will be consumed next time around */;
15321 /* If it's a `;', we are done. */
15322 else if (token->type == CPP_SEMICOLON)
15323 break;
15324 else if (maybe_range_for_decl)
15326 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15327 permerror (decl_specifiers.locations[ds_type_spec],
15328 "types may not be defined in a for-range-declaration");
15329 break;
15331 /* Anything else is an error. */
15332 else
15334 /* If we have already issued an error message we don't need
15335 to issue another one. */
15336 if ((decl != error_mark_node
15337 && DECL_INITIAL (decl) != error_mark_node)
15338 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15339 cp_parser_error (parser, "expected %<,%> or %<;%>");
15340 /* Skip tokens until we reach the end of the statement. */
15341 cp_parser_skip_to_end_of_statement (parser);
15342 /* If the next token is now a `;', consume it. */
15343 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15344 cp_lexer_consume_token (parser->lexer);
15345 goto done;
15347 /* After the first time around, a function-definition is not
15348 allowed -- even if it was OK at first. For example:
15350 int i, f() {}
15352 is not valid. */
15353 function_definition_allowed_p = false;
15356 /* Issue an error message if no declarators are present, and the
15357 decl-specifier-seq does not itself declare a class or
15358 enumeration: [dcl.dcl]/3. */
15359 if (!saw_declarator)
15361 if (cp_parser_declares_only_class_p (parser))
15363 if (!declares_class_or_enum
15364 && decl_specifiers.type
15365 && OVERLOAD_TYPE_P (decl_specifiers.type))
15366 /* Ensure an error is issued anyway when finish_decltype_type,
15367 called via cp_parser_decl_specifier_seq, returns a class or
15368 an enumeration (c++/51786). */
15369 decl_specifiers.type = NULL_TREE;
15370 shadow_tag (&decl_specifiers);
15372 /* Perform any deferred access checks. */
15373 perform_deferred_access_checks (tf_warning_or_error);
15376 /* Consume the `;'. */
15377 finish:
15378 if (!maybe_range_for_decl)
15379 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15380 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15382 if (init_loc != UNKNOWN_LOCATION)
15383 error_at (init_loc, "initializer in range-based %<for%> loop");
15384 if (comma_loc != UNKNOWN_LOCATION)
15385 error_at (comma_loc,
15386 "multiple declarations in range-based %<for%> loop");
15389 done:
15390 pop_deferring_access_checks ();
15391 cp_finalize_omp_declare_simd (parser, &odsd);
15394 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15395 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15396 initializer ; */
15398 static tree
15399 cp_parser_decomposition_declaration (cp_parser *parser,
15400 cp_decl_specifier_seq *decl_specifiers,
15401 tree *maybe_range_for_decl,
15402 location_t *init_loc)
15404 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15405 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15406 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15408 /* Parse the identifier-list. */
15409 auto_vec<cp_expr, 10> v;
15410 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15411 while (true)
15413 cp_expr e = cp_parser_identifier (parser);
15414 if (e.get_value () == error_mark_node)
15415 break;
15416 v.safe_push (e);
15417 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15418 break;
15419 cp_lexer_consume_token (parser->lexer);
15422 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
15423 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15425 end_loc = UNKNOWN_LOCATION;
15426 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
15427 false);
15428 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15429 cp_lexer_consume_token (parser->lexer);
15430 else
15432 cp_parser_skip_to_end_of_statement (parser);
15433 return error_mark_node;
15437 if (cxx_dialect < cxx17)
15438 pedwarn (loc, OPT_Wc__17_extensions,
15439 "structured bindings only available with "
15440 "%<-std=c++17%> or %<-std=gnu++17%>");
15442 tree pushed_scope;
15443 cp_declarator *declarator = make_declarator (cdk_decomp);
15444 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
15445 declarator->id_loc = loc;
15446 if (ref_qual != REF_QUAL_NONE)
15447 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
15448 ref_qual == REF_QUAL_RVALUE,
15449 NULL_TREE);
15450 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
15451 NULL_TREE, decl_specifiers->attributes,
15452 &pushed_scope);
15453 tree orig_decl = decl;
15455 unsigned int i;
15456 cp_expr e;
15457 cp_decl_specifier_seq decl_specs;
15458 clear_decl_specs (&decl_specs);
15459 decl_specs.type = make_auto ();
15460 tree prev = decl;
15461 FOR_EACH_VEC_ELT (v, i, e)
15463 if (i == 0)
15464 declarator = make_id_declarator (NULL_TREE, e.get_value (),
15465 sfk_none, e.get_location ());
15466 else
15468 declarator->u.id.unqualified_name = e.get_value ();
15469 declarator->id_loc = e.get_location ();
15471 tree elt_pushed_scope;
15472 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
15473 NULL_TREE, NULL_TREE, &elt_pushed_scope);
15474 if (decl2 == error_mark_node)
15475 decl = error_mark_node;
15476 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
15478 /* Ensure we've diagnosed redeclaration if we aren't creating
15479 a new VAR_DECL. */
15480 gcc_assert (errorcount);
15481 decl = error_mark_node;
15483 else
15484 prev = decl2;
15485 if (elt_pushed_scope)
15486 pop_scope (elt_pushed_scope);
15489 if (v.is_empty ())
15491 error_at (loc, "empty structured binding declaration");
15492 decl = error_mark_node;
15495 if (maybe_range_for_decl == NULL
15496 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15498 bool non_constant_p = false, is_direct_init = false;
15499 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
15500 tree initializer = cp_parser_initializer (parser, &is_direct_init,
15501 &non_constant_p);
15502 if (initializer == NULL_TREE
15503 || (TREE_CODE (initializer) == TREE_LIST
15504 && TREE_CHAIN (initializer))
15505 || (is_direct_init
15506 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
15507 && CONSTRUCTOR_NELTS (initializer) != 1))
15509 error_at (loc, "invalid initializer for structured binding "
15510 "declaration");
15511 initializer = error_mark_node;
15514 if (decl != error_mark_node)
15516 cp_maybe_mangle_decomp (decl, prev, v.length ());
15517 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
15518 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15519 cp_finish_decomp (decl, prev, v.length ());
15522 else if (decl != error_mark_node)
15524 *maybe_range_for_decl = prev;
15525 /* Ensure DECL_VALUE_EXPR is created for all the decls but
15526 the underlying DECL. */
15527 cp_finish_decomp (decl, prev, v.length ());
15530 if (pushed_scope)
15531 pop_scope (pushed_scope);
15533 if (decl == error_mark_node && DECL_P (orig_decl))
15535 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
15536 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
15539 return decl;
15542 /* Parse a decl-specifier-seq.
15544 decl-specifier-seq:
15545 decl-specifier-seq [opt] decl-specifier
15546 decl-specifier attribute-specifier-seq [opt] (C++11)
15548 decl-specifier:
15549 storage-class-specifier
15550 type-specifier
15551 function-specifier
15552 friend
15553 typedef
15555 GNU Extension:
15557 decl-specifier:
15558 attributes
15560 Concepts Extension:
15562 decl-specifier:
15563 concept
15565 Set *DECL_SPECS to a representation of the decl-specifier-seq.
15567 The parser flags FLAGS is used to control type-specifier parsing.
15569 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15570 flags:
15572 1: one of the decl-specifiers is an elaborated-type-specifier
15573 (i.e., a type declaration)
15574 2: one of the decl-specifiers is an enum-specifier or a
15575 class-specifier (i.e., a type definition)
15579 static void
15580 cp_parser_decl_specifier_seq (cp_parser* parser,
15581 cp_parser_flags flags,
15582 cp_decl_specifier_seq *decl_specs,
15583 int* declares_class_or_enum)
15585 bool constructor_possible_p = !parser->in_declarator_p;
15586 bool found_decl_spec = false;
15587 cp_token *start_token = NULL;
15588 cp_decl_spec ds;
15590 /* Clear DECL_SPECS. */
15591 clear_decl_specs (decl_specs);
15593 /* Assume no class or enumeration type is declared. */
15594 *declares_class_or_enum = 0;
15596 /* Keep reading specifiers until there are no more to read. */
15597 while (true)
15599 bool constructor_p;
15600 cp_token *token;
15601 ds = ds_last;
15603 /* Peek at the next token. */
15604 token = cp_lexer_peek_token (parser->lexer);
15606 /* Save the first token of the decl spec list for error
15607 reporting. */
15608 if (!start_token)
15609 start_token = token;
15610 /* Handle attributes. */
15611 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
15612 && cp_next_tokens_can_be_attribute_p (parser))
15614 /* Parse the attributes. */
15615 tree attrs = cp_parser_attributes_opt (parser);
15617 /* In a sequence of declaration specifiers, c++11 attributes
15618 appertain to the type that precede them. In that case
15619 [dcl.spec]/1 says:
15621 The attribute-specifier-seq affects the type only for
15622 the declaration it appears in, not other declarations
15623 involving the same type.
15625 But for now let's force the user to position the
15626 attribute either at the beginning of the declaration or
15627 after the declarator-id, which would clearly mean that it
15628 applies to the declarator. */
15629 if (cxx11_attribute_p (attrs))
15631 if (!found_decl_spec)
15632 /* The c++11 attribute is at the beginning of the
15633 declaration. It appertains to the entity being
15634 declared. */;
15635 else
15637 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
15639 /* This is an attribute following a
15640 class-specifier. */
15641 if (decl_specs->type_definition_p)
15642 warn_misplaced_attr_for_class_type (token->location,
15643 decl_specs->type);
15644 attrs = NULL_TREE;
15646 else
15648 decl_specs->std_attributes
15649 = attr_chainon (decl_specs->std_attributes, attrs);
15650 if (decl_specs->locations[ds_std_attribute] == 0)
15651 decl_specs->locations[ds_std_attribute] = token->location;
15653 continue;
15657 decl_specs->attributes
15658 = attr_chainon (decl_specs->attributes, attrs);
15659 if (decl_specs->locations[ds_attribute] == 0)
15660 decl_specs->locations[ds_attribute] = token->location;
15661 continue;
15663 /* Assume we will find a decl-specifier keyword. */
15664 found_decl_spec = true;
15665 /* If the next token is an appropriate keyword, we can simply
15666 add it to the list. */
15667 switch (token->keyword)
15669 /* decl-specifier:
15670 friend
15671 constexpr
15672 constinit */
15673 case RID_FRIEND:
15674 if (!at_class_scope_p ())
15676 gcc_rich_location richloc (token->location);
15677 richloc.add_fixit_remove ();
15678 error_at (&richloc, "%<friend%> used outside of class");
15679 cp_lexer_purge_token (parser->lexer);
15681 else
15683 ds = ds_friend;
15684 /* Consume the token. */
15685 cp_lexer_consume_token (parser->lexer);
15687 break;
15689 case RID_CONSTEXPR:
15690 ds = ds_constexpr;
15691 cp_lexer_consume_token (parser->lexer);
15692 break;
15694 case RID_CONSTINIT:
15695 ds = ds_constinit;
15696 cp_lexer_consume_token (parser->lexer);
15697 break;
15699 case RID_CONSTEVAL:
15700 ds = ds_consteval;
15701 cp_lexer_consume_token (parser->lexer);
15702 break;
15704 case RID_CONCEPT:
15705 ds = ds_concept;
15706 cp_lexer_consume_token (parser->lexer);
15708 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15709 break;
15711 /* Warn for concept as a decl-specifier. We'll rewrite these as
15712 concept declarations later. */
15713 if (!flag_concepts_ts)
15715 cp_token *next = cp_lexer_peek_token (parser->lexer);
15716 if (next->keyword == RID_BOOL)
15717 pedwarn (next->location, 0, "the %<bool%> keyword is not "
15718 "allowed in a C++20 concept definition");
15719 else
15720 pedwarn (token->location, 0, "C++20 concept definition syntax "
15721 "is %<concept <name> = <expr>%>");
15724 /* In C++20 a concept definition is just 'concept name = expr;'
15725 Support that syntax as a TS extension by pretending we've seen
15726 the 'bool' specifier. */
15727 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15728 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
15729 && !decl_specs->any_type_specifiers_p)
15731 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
15732 token, /*type_definition*/false);
15733 decl_specs->any_type_specifiers_p = true;
15735 break;
15737 /* function-specifier:
15738 inline
15739 virtual
15740 explicit */
15741 case RID_INLINE:
15742 case RID_VIRTUAL:
15743 case RID_EXPLICIT:
15744 cp_parser_function_specifier_opt (parser, decl_specs);
15745 break;
15747 /* decl-specifier:
15748 typedef */
15749 case RID_TYPEDEF:
15750 ds = ds_typedef;
15751 /* Consume the token. */
15752 cp_lexer_consume_token (parser->lexer);
15754 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15755 break;
15757 /* A constructor declarator cannot appear in a typedef. */
15758 constructor_possible_p = false;
15759 /* The "typedef" keyword can only occur in a declaration; we
15760 may as well commit at this point. */
15761 cp_parser_commit_to_tentative_parse (parser);
15763 if (decl_specs->storage_class != sc_none)
15764 decl_specs->conflicting_specifiers_p = true;
15765 break;
15767 /* storage-class-specifier:
15768 auto
15769 register
15770 static
15771 extern
15772 mutable
15774 GNU Extension:
15775 thread */
15776 case RID_AUTO:
15777 if (cxx_dialect == cxx98)
15779 /* Consume the token. */
15780 cp_lexer_consume_token (parser->lexer);
15782 /* Complain about `auto' as a storage specifier, if
15783 we're complaining about C++0x compatibility. */
15784 gcc_rich_location richloc (token->location);
15785 richloc.add_fixit_remove ();
15786 warning_at (&richloc, OPT_Wc__11_compat,
15787 "%<auto%> changes meaning in C++11; "
15788 "please remove it");
15790 /* Set the storage class anyway. */
15791 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
15792 token);
15794 else
15795 /* C++0x auto type-specifier. */
15796 found_decl_spec = false;
15797 break;
15799 case RID_REGISTER:
15800 case RID_STATIC:
15801 case RID_EXTERN:
15802 case RID_MUTABLE:
15803 /* Consume the token. */
15804 cp_lexer_consume_token (parser->lexer);
15805 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
15806 token);
15807 break;
15808 case RID_THREAD:
15809 /* Consume the token. */
15810 ds = ds_thread;
15811 cp_lexer_consume_token (parser->lexer);
15812 break;
15814 default:
15815 /* We did not yet find a decl-specifier yet. */
15816 found_decl_spec = false;
15817 break;
15820 if (found_decl_spec
15821 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
15822 && token->keyword != RID_CONSTEXPR)
15823 error ("%<decl-specifier%> invalid in condition");
15825 if (found_decl_spec
15826 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15827 && token->keyword != RID_MUTABLE
15828 && token->keyword != RID_CONSTEXPR
15829 && token->keyword != RID_CONSTEVAL)
15830 error_at (token->location, "%qD invalid in lambda",
15831 ridpointers[token->keyword]);
15833 if (ds != ds_last)
15834 set_and_check_decl_spec_loc (decl_specs, ds, token);
15836 /* Constructors are a special case. The `S' in `S()' is not a
15837 decl-specifier; it is the beginning of the declarator. */
15838 constructor_p
15839 = (!found_decl_spec
15840 && constructor_possible_p
15841 && (cp_parser_constructor_declarator_p
15842 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
15843 ds_friend))));
15845 /* If we don't have a DECL_SPEC yet, then we must be looking at
15846 a type-specifier. */
15847 if (!found_decl_spec && !constructor_p)
15849 int decl_spec_declares_class_or_enum;
15850 bool is_cv_qualifier;
15851 tree type_spec;
15853 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15854 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15856 type_spec
15857 = cp_parser_type_specifier (parser, flags,
15858 decl_specs,
15859 /*is_declaration=*/true,
15860 &decl_spec_declares_class_or_enum,
15861 &is_cv_qualifier);
15862 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
15864 /* If this type-specifier referenced a user-defined type
15865 (a typedef, class-name, etc.), then we can't allow any
15866 more such type-specifiers henceforth.
15868 [dcl.spec]
15870 The longest sequence of decl-specifiers that could
15871 possibly be a type name is taken as the
15872 decl-specifier-seq of a declaration. The sequence shall
15873 be self-consistent as described below.
15875 [dcl.type]
15877 As a general rule, at most one type-specifier is allowed
15878 in the complete decl-specifier-seq of a declaration. The
15879 only exceptions are the following:
15881 -- const or volatile can be combined with any other
15882 type-specifier.
15884 -- signed or unsigned can be combined with char, long,
15885 short, or int.
15887 -- ..
15889 Example:
15891 typedef char* Pc;
15892 void g (const int Pc);
15894 Here, Pc is *not* part of the decl-specifier seq; it's
15895 the declarator. Therefore, once we see a type-specifier
15896 (other than a cv-qualifier), we forbid any additional
15897 user-defined types. We *do* still allow things like `int
15898 int' to be considered a decl-specifier-seq, and issue the
15899 error message later. */
15900 if (type_spec && !is_cv_qualifier)
15901 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15902 /* A constructor declarator cannot follow a type-specifier. */
15903 if (type_spec)
15905 constructor_possible_p = false;
15906 found_decl_spec = true;
15907 if (!is_cv_qualifier)
15908 decl_specs->any_type_specifiers_p = true;
15910 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
15911 error_at (token->location, "type-specifier invalid in lambda");
15915 /* If we still do not have a DECL_SPEC, then there are no more
15916 decl-specifiers. */
15917 if (!found_decl_spec)
15918 break;
15920 if (decl_specs->std_attributes)
15922 error_at (decl_specs->locations[ds_std_attribute],
15923 "standard attributes in middle of decl-specifiers");
15924 inform (decl_specs->locations[ds_std_attribute],
15925 "standard attributes must precede the decl-specifiers to "
15926 "apply to the declaration, or follow them to apply to "
15927 "the type");
15930 decl_specs->any_specifiers_p = true;
15931 /* After we see one decl-specifier, further decl-specifiers are
15932 always optional. */
15933 flags |= CP_PARSER_FLAGS_OPTIONAL;
15936 /* Don't allow a friend specifier with a class definition. */
15937 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
15938 && (*declares_class_or_enum & 2))
15939 error_at (decl_specs->locations[ds_friend],
15940 "class definition may not be declared a friend");
15943 /* Parse an (optional) storage-class-specifier.
15945 storage-class-specifier:
15946 auto
15947 register
15948 static
15949 extern
15950 mutable
15952 GNU Extension:
15954 storage-class-specifier:
15955 thread
15957 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
15959 static tree
15960 cp_parser_storage_class_specifier_opt (cp_parser* parser)
15962 switch (cp_lexer_peek_token (parser->lexer)->keyword)
15964 case RID_AUTO:
15965 if (cxx_dialect != cxx98)
15966 return NULL_TREE;
15967 /* Fall through for C++98. */
15968 gcc_fallthrough ();
15970 case RID_REGISTER:
15971 case RID_STATIC:
15972 case RID_EXTERN:
15973 case RID_MUTABLE:
15974 case RID_THREAD:
15975 /* Consume the token. */
15976 return cp_lexer_consume_token (parser->lexer)->u.value;
15978 default:
15979 return NULL_TREE;
15983 /* Parse an (optional) function-specifier.
15985 function-specifier:
15986 inline
15987 virtual
15988 explicit
15990 C++20 Extension:
15991 explicit(constant-expression)
15993 Returns an IDENTIFIER_NODE corresponding to the keyword used.
15994 Updates DECL_SPECS, if it is non-NULL. */
15996 static tree
15997 cp_parser_function_specifier_opt (cp_parser* parser,
15998 cp_decl_specifier_seq *decl_specs)
16000 cp_token *token = cp_lexer_peek_token (parser->lexer);
16001 switch (token->keyword)
16003 case RID_INLINE:
16004 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16005 break;
16007 case RID_VIRTUAL:
16008 /* 14.5.2.3 [temp.mem]
16010 A member function template shall not be virtual. */
16011 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16012 && current_class_type)
16013 error_at (token->location, "templates may not be %<virtual%>");
16014 else
16015 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16016 break;
16018 case RID_EXPLICIT:
16020 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16021 /* If we see '(', it's C++20 explicit(bool). */
16022 tree expr;
16023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16025 matching_parens parens;
16026 parens.consume_open (parser);
16028 /* New types are not allowed in an explicit-specifier. */
16029 const char *saved_message
16030 = parser->type_definition_forbidden_message;
16031 parser->type_definition_forbidden_message
16032 = G_("types may not be defined in explicit-specifier");
16034 if (cxx_dialect < cxx20)
16035 pedwarn (token->location, OPT_Wc__20_extensions,
16036 "%<explicit(bool)%> only available with %<-std=c++20%> "
16037 "or %<-std=gnu++20%>");
16039 /* Parse the constant-expression. */
16040 expr = cp_parser_constant_expression (parser);
16042 /* Restore the saved message. */
16043 parser->type_definition_forbidden_message = saved_message;
16044 parens.require_close (parser);
16046 else
16047 /* The explicit-specifier explicit without a constant-expression is
16048 equivalent to the explicit-specifier explicit(true). */
16049 expr = boolean_true_node;
16051 /* [dcl.fct.spec]
16052 "the constant-expression, if supplied, shall be a contextually
16053 converted constant expression of type bool." */
16054 expr = build_explicit_specifier (expr, tf_warning_or_error);
16055 /* We could evaluate it -- mark the decl as appropriate. */
16056 if (expr == boolean_true_node)
16057 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16058 else if (expr == boolean_false_node)
16059 /* Don't mark the decl as explicit. */;
16060 else if (decl_specs)
16061 /* The expression was value-dependent. Remember it so that we can
16062 substitute it later. */
16063 decl_specs->explicit_specifier = expr;
16064 return id;
16067 default:
16068 return NULL_TREE;
16071 /* Consume the token. */
16072 return cp_lexer_consume_token (parser->lexer)->u.value;
16075 /* Parse a linkage-specification.
16077 linkage-specification:
16078 extern string-literal { declaration-seq [opt] }
16079 extern string-literal declaration */
16081 static void
16082 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16084 tree linkage;
16086 /* Look for the `extern' keyword. */
16087 cp_token *extern_token
16088 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16090 /* Look for the string-literal. */
16091 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16092 linkage = cp_parser_string_literal (parser, false, false);
16094 /* Transform the literal into an identifier. If the literal is a
16095 wide-character string, or contains embedded NULs, then we can't
16096 handle it as the user wants. */
16097 if (strlen (TREE_STRING_POINTER (linkage))
16098 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16100 cp_parser_error (parser, "invalid linkage-specification");
16101 /* Assume C++ linkage. */
16102 linkage = lang_name_cplusplus;
16104 else
16105 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16107 /* We're now using the new linkage. */
16108 push_lang_context (linkage);
16110 /* Preserve the location of the innermost linkage specification,
16111 tracking the locations of nested specifications via a local. */
16112 location_t saved_location
16113 = parser->innermost_linkage_specification_location;
16114 /* Construct a location ranging from the start of the "extern" to
16115 the end of the string-literal, with the caret at the start, e.g.:
16116 extern "C" {
16117 ^~~~~~~~~~
16119 parser->innermost_linkage_specification_location
16120 = make_location (extern_token->location,
16121 extern_token->location,
16122 get_finish (string_token->location));
16124 /* If the next token is a `{', then we're using the first
16125 production. */
16126 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16128 cp_ensure_no_omp_declare_simd (parser);
16129 cp_ensure_no_oacc_routine (parser);
16131 /* Consume the `{' token. */
16132 matching_braces braces;
16133 braces.consume_open (parser);
16134 /* Parse the declarations. */
16135 cp_parser_declaration_seq_opt (parser);
16136 /* Look for the closing `}'. */
16137 braces.require_close (parser);
16139 /* Otherwise, there's just one declaration. */
16140 else
16142 bool saved_in_unbraced_linkage_specification_p;
16144 saved_in_unbraced_linkage_specification_p
16145 = parser->in_unbraced_linkage_specification_p;
16146 parser->in_unbraced_linkage_specification_p = true;
16147 cp_parser_declaration (parser, prefix_attr);
16148 parser->in_unbraced_linkage_specification_p
16149 = saved_in_unbraced_linkage_specification_p;
16152 /* We're done with the linkage-specification. */
16153 pop_lang_context ();
16155 /* Restore location of parent linkage specification, if any. */
16156 parser->innermost_linkage_specification_location = saved_location;
16159 /* Parse a static_assert-declaration.
16161 static_assert-declaration:
16162 static_assert ( constant-expression , string-literal ) ;
16163 static_assert ( constant-expression ) ; (C++17)
16165 If MEMBER_P, this static_assert is a class member. */
16167 static void
16168 cp_parser_static_assert(cp_parser *parser, bool member_p)
16170 cp_expr condition;
16171 location_t token_loc;
16172 tree message;
16173 bool dummy;
16175 /* Peek at the `static_assert' token so we can keep track of exactly
16176 where the static assertion started. */
16177 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16179 /* Look for the `static_assert' keyword. */
16180 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16181 RT_STATIC_ASSERT))
16182 return;
16184 /* We know we are in a static assertion; commit to any tentative
16185 parse. */
16186 if (cp_parser_parsing_tentatively (parser))
16187 cp_parser_commit_to_tentative_parse (parser);
16189 /* Parse the `(' starting the static assertion condition. */
16190 matching_parens parens;
16191 parens.require_open (parser);
16193 /* Parse the constant-expression. Allow a non-constant expression
16194 here in order to give better diagnostics in finish_static_assert. */
16195 condition =
16196 cp_parser_constant_expression (parser,
16197 /*allow_non_constant_p=*/true,
16198 /*non_constant_p=*/&dummy);
16200 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16202 if (pedantic && cxx_dialect < cxx17)
16203 pedwarn (input_location, OPT_Wc__17_extensions,
16204 "%<static_assert%> without a message "
16205 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16206 /* Eat the ')' */
16207 cp_lexer_consume_token (parser->lexer);
16208 message = build_string (1, "");
16209 TREE_TYPE (message) = char_array_type_node;
16210 fix_string_type (message);
16212 else
16214 /* Parse the separating `,'. */
16215 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16217 /* Parse the string-literal message. */
16218 message = cp_parser_string_literal (parser,
16219 /*translate=*/false,
16220 /*wide_ok=*/true);
16222 /* A `)' completes the static assertion. */
16223 if (!parens.require_close (parser))
16224 cp_parser_skip_to_closing_parenthesis (parser,
16225 /*recovering=*/true,
16226 /*or_comma=*/false,
16227 /*consume_paren=*/true);
16230 /* A semicolon terminates the declaration. */
16231 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16233 /* Get the location for the static assertion. Use that of the
16234 condition if available, otherwise, use that of the "static_assert"
16235 token. */
16236 location_t assert_loc = condition.get_location ();
16237 if (assert_loc == UNKNOWN_LOCATION)
16238 assert_loc = token_loc;
16240 /* Complete the static assertion, which may mean either processing
16241 the static assert now or saving it for template instantiation. */
16242 finish_static_assert (condition, message, assert_loc, member_p,
16243 /*show_expr_p=*/false);
16246 /* Parse the expression in decltype ( expression ). */
16248 static tree
16249 cp_parser_decltype_expr (cp_parser *parser,
16250 bool &id_expression_or_member_access_p)
16252 cp_token *id_expr_start_token;
16253 tree expr;
16255 /* First, try parsing an id-expression. */
16256 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16257 cp_parser_parse_tentatively (parser);
16258 expr = cp_parser_id_expression (parser,
16259 /*template_keyword_p=*/false,
16260 /*check_dependency_p=*/true,
16261 /*template_p=*/NULL,
16262 /*declarator_p=*/false,
16263 /*optional_p=*/false);
16265 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16267 bool non_integral_constant_expression_p = false;
16268 tree id_expression = expr;
16269 cp_id_kind idk;
16270 const char *error_msg;
16272 if (identifier_p (expr))
16273 /* Lookup the name we got back from the id-expression. */
16274 expr = cp_parser_lookup_name_simple (parser, expr,
16275 id_expr_start_token->location);
16277 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
16278 /* A template without args is not a complete id-expression. */
16279 expr = error_mark_node;
16281 if (expr
16282 && expr != error_mark_node
16283 && TREE_CODE (expr) != TYPE_DECL
16284 && (TREE_CODE (expr) != BIT_NOT_EXPR
16285 || !TYPE_P (TREE_OPERAND (expr, 0)))
16286 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16288 /* Complete lookup of the id-expression. */
16289 expr = (finish_id_expression
16290 (id_expression, expr, parser->scope, &idk,
16291 /*integral_constant_expression_p=*/false,
16292 /*allow_non_integral_constant_expression_p=*/true,
16293 &non_integral_constant_expression_p,
16294 /*template_p=*/false,
16295 /*done=*/true,
16296 /*address_p=*/false,
16297 /*template_arg_p=*/false,
16298 &error_msg,
16299 id_expr_start_token->location));
16301 if (expr == error_mark_node)
16302 /* We found an id-expression, but it was something that we
16303 should not have found. This is an error, not something
16304 we can recover from, so note that we found an
16305 id-expression and we'll recover as gracefully as
16306 possible. */
16307 id_expression_or_member_access_p = true;
16310 if (expr
16311 && expr != error_mark_node
16312 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16313 /* We have an id-expression. */
16314 id_expression_or_member_access_p = true;
16317 if (!id_expression_or_member_access_p)
16319 /* Abort the id-expression parse. */
16320 cp_parser_abort_tentative_parse (parser);
16322 /* Parsing tentatively, again. */
16323 cp_parser_parse_tentatively (parser);
16325 /* Parse a class member access. */
16326 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16327 /*cast_p=*/false, /*decltype*/true,
16328 /*member_access_only_p=*/true, NULL);
16330 if (expr
16331 && expr != error_mark_node
16332 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16333 /* We have an id-expression. */
16334 id_expression_or_member_access_p = true;
16337 if (id_expression_or_member_access_p)
16338 /* We have parsed the complete id-expression or member access. */
16339 cp_parser_parse_definitely (parser);
16340 else
16342 /* Abort our attempt to parse an id-expression or member access
16343 expression. */
16344 cp_parser_abort_tentative_parse (parser);
16346 /* Parse a full expression. */
16347 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
16348 /*decltype_p=*/true);
16351 return expr;
16354 /* Parse a `decltype' type. Returns the type.
16356 decltype-specifier:
16357 decltype ( expression )
16358 C++14:
16359 decltype ( auto ) */
16361 static tree
16362 cp_parser_decltype (cp_parser *parser)
16364 bool id_expression_or_member_access_p = false;
16365 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
16367 if (start_token->type == CPP_DECLTYPE)
16369 /* Already parsed. */
16370 cp_lexer_consume_token (parser->lexer);
16371 return saved_checks_value (start_token->u.tree_check_value);
16374 /* Look for the `decltype' token. */
16375 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
16376 return error_mark_node;
16378 /* Parse the opening `('. */
16379 matching_parens parens;
16380 if (!parens.require_open (parser))
16381 return error_mark_node;
16383 /* Since we're going to preserve any side-effects from this parse, set up a
16384 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16385 in the expression. */
16386 tentative_firewall firewall (parser);
16388 /* If in_declarator_p, a reparse as an expression might succeed (60361).
16389 Otherwise, commit now for better diagnostics. */
16390 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
16391 && !parser->in_declarator_p)
16392 cp_parser_commit_to_topmost_tentative_parse (parser);
16394 push_deferring_access_checks (dk_deferred);
16396 tree expr = NULL_TREE;
16398 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
16399 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
16401 /* decltype (auto) */
16402 cp_lexer_consume_token (parser->lexer);
16403 if (cxx_dialect < cxx14)
16405 error_at (start_token->location,
16406 "%<decltype(auto)%> type specifier only available with "
16407 "%<-std=c++14%> or %<-std=gnu++14%>");
16408 expr = error_mark_node;
16411 else
16413 /* decltype (expression) */
16415 /* Types cannot be defined in a `decltype' expression. Save away the
16416 old message and set the new one. */
16417 const char *saved_message = parser->type_definition_forbidden_message;
16418 parser->type_definition_forbidden_message
16419 = G_("types may not be defined in %<decltype%> expressions");
16421 /* The restrictions on constant-expressions do not apply inside
16422 decltype expressions. */
16423 bool saved_integral_constant_expression_p
16424 = parser->integral_constant_expression_p;
16425 bool saved_non_integral_constant_expression_p
16426 = parser->non_integral_constant_expression_p;
16427 parser->integral_constant_expression_p = false;
16429 /* Within a parenthesized expression, a `>' token is always
16430 the greater-than operator. */
16431 bool saved_greater_than_is_operator_p
16432 = parser->greater_than_is_operator_p;
16433 parser->greater_than_is_operator_p = true;
16435 /* Don't synthesize an implicit template type parameter here. This
16436 could happen with C++23 code like
16438 void f(decltype(new auto{0}));
16440 where we want to deduce the auto right away so that the parameter
16441 is of type 'int *'. */
16442 auto cleanup = make_temp_override
16443 (parser->auto_is_implicit_function_template_parm_p, false);
16445 /* Do not actually evaluate the expression. */
16446 ++cp_unevaluated_operand;
16448 /* Do not warn about problems with the expression. */
16449 ++c_inhibit_evaluation_warnings;
16451 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
16452 STRIP_ANY_LOCATION_WRAPPER (expr);
16454 /* Go back to evaluating expressions. */
16455 --cp_unevaluated_operand;
16456 --c_inhibit_evaluation_warnings;
16458 /* The `>' token might be the end of a template-id or
16459 template-parameter-list now. */
16460 parser->greater_than_is_operator_p
16461 = saved_greater_than_is_operator_p;
16463 /* Restore the old message and the integral constant expression
16464 flags. */
16465 parser->type_definition_forbidden_message = saved_message;
16466 parser->integral_constant_expression_p
16467 = saved_integral_constant_expression_p;
16468 parser->non_integral_constant_expression_p
16469 = saved_non_integral_constant_expression_p;
16472 /* Parse to the closing `)'. */
16473 if (expr == error_mark_node || !parens.require_close (parser))
16475 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16476 /*consume_paren=*/true);
16477 expr = error_mark_node;
16480 /* If we got a parse error while tentative, bail out now. */
16481 if (cp_parser_error_occurred (parser))
16483 pop_deferring_access_checks ();
16484 return error_mark_node;
16487 if (!expr)
16488 /* Build auto. */
16489 expr = make_decltype_auto ();
16490 else
16491 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
16492 tf_warning_or_error);
16494 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16495 it again. */
16496 start_token->type = CPP_DECLTYPE;
16497 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16498 start_token->tree_check_p = true;
16499 start_token->u.tree_check_value->value = expr;
16500 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
16501 start_token->keyword = RID_MAX;
16503 location_t loc = start_token->location;
16504 loc = make_location (loc, loc, parser->lexer);
16505 start_token->location = loc;
16507 cp_lexer_purge_tokens_after (parser->lexer, start_token);
16509 pop_to_parent_deferring_access_checks ();
16511 return expr;
16514 /* Special member functions [gram.special] */
16516 /* Parse a conversion-function-id.
16518 conversion-function-id:
16519 operator conversion-type-id
16521 Returns an IDENTIFIER_NODE representing the operator. */
16523 static tree
16524 cp_parser_conversion_function_id (cp_parser* parser)
16526 tree type;
16527 tree saved_scope;
16528 tree saved_qualifying_scope;
16529 tree saved_object_scope;
16530 tree pushed_scope = NULL_TREE;
16532 /* Look for the `operator' token. */
16533 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16534 return error_mark_node;
16535 /* When we parse the conversion-type-id, the current scope will be
16536 reset. However, we need that information in able to look up the
16537 conversion function later, so we save it here. */
16538 saved_scope = parser->scope;
16539 saved_qualifying_scope = parser->qualifying_scope;
16540 saved_object_scope = parser->object_scope;
16541 /* We must enter the scope of the class so that the names of
16542 entities declared within the class are available in the
16543 conversion-type-id. For example, consider:
16545 struct S {
16546 typedef int I;
16547 operator I();
16550 S::operator I() { ... }
16552 In order to see that `I' is a type-name in the definition, we
16553 must be in the scope of `S'. */
16554 if (saved_scope)
16555 pushed_scope = push_scope (saved_scope);
16556 /* Parse the conversion-type-id. */
16557 type = cp_parser_conversion_type_id (parser);
16558 /* Leave the scope of the class, if any. */
16559 if (pushed_scope)
16560 pop_scope (pushed_scope);
16561 /* Restore the saved scope. */
16562 parser->scope = saved_scope;
16563 parser->qualifying_scope = saved_qualifying_scope;
16564 parser->object_scope = saved_object_scope;
16565 /* If the TYPE is invalid, indicate failure. */
16566 if (type == error_mark_node)
16567 return error_mark_node;
16568 return make_conv_op_name (type);
16571 /* Parse a conversion-type-id:
16573 conversion-type-id:
16574 type-specifier-seq conversion-declarator [opt]
16576 Returns the TYPE specified. */
16578 static tree
16579 cp_parser_conversion_type_id (cp_parser* parser)
16581 tree attributes;
16582 cp_decl_specifier_seq type_specifiers;
16583 cp_declarator *declarator;
16584 tree type_specified;
16585 const char *saved_message;
16587 /* Parse the attributes. */
16588 attributes = cp_parser_attributes_opt (parser);
16590 saved_message = parser->type_definition_forbidden_message;
16591 parser->type_definition_forbidden_message
16592 = G_("types may not be defined in a conversion-type-id");
16594 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
16595 optional in conversion-type-id. */
16596 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16597 /*is_declaration=*/false,
16598 /*is_trailing_return=*/false,
16599 &type_specifiers);
16601 parser->type_definition_forbidden_message = saved_message;
16603 /* If that didn't work, stop. */
16604 if (type_specifiers.type == error_mark_node)
16605 return error_mark_node;
16606 /* Parse the conversion-declarator. */
16607 declarator = cp_parser_conversion_declarator_opt (parser);
16609 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
16610 /*initialized=*/0, &attributes);
16611 if (attributes)
16612 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
16614 /* Don't give this error when parsing tentatively. This happens to
16615 work because we always parse this definitively once. */
16616 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
16617 && type_uses_auto (type_specified))
16619 if (cxx_dialect < cxx14)
16621 error ("invalid use of %<auto%> in conversion operator");
16622 return error_mark_node;
16624 else if (template_parm_scope_p ())
16625 warning (0, "use of %<auto%> in member template "
16626 "conversion operator can never be deduced");
16629 return type_specified;
16632 /* Parse an (optional) conversion-declarator.
16634 conversion-declarator:
16635 ptr-operator conversion-declarator [opt]
16639 static cp_declarator *
16640 cp_parser_conversion_declarator_opt (cp_parser* parser)
16642 enum tree_code code;
16643 tree class_type, std_attributes = NULL_TREE;
16644 cp_cv_quals cv_quals;
16646 /* We don't know if there's a ptr-operator next, or not. */
16647 cp_parser_parse_tentatively (parser);
16648 /* Try the ptr-operator. */
16649 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
16650 &std_attributes);
16651 /* If it worked, look for more conversion-declarators. */
16652 if (cp_parser_parse_definitely (parser))
16654 cp_declarator *declarator;
16656 /* Parse another optional declarator. */
16657 declarator = cp_parser_conversion_declarator_opt (parser);
16659 declarator = cp_parser_make_indirect_declarator
16660 (code, class_type, cv_quals, declarator, std_attributes);
16662 return declarator;
16665 return NULL;
16668 /* Parse an (optional) ctor-initializer.
16670 ctor-initializer:
16671 : mem-initializer-list */
16673 static void
16674 cp_parser_ctor_initializer_opt (cp_parser* parser)
16676 /* If the next token is not a `:', then there is no
16677 ctor-initializer. */
16678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16680 /* Do default initialization of any bases and members. */
16681 if (DECL_CONSTRUCTOR_P (current_function_decl))
16682 finish_mem_initializers (NULL_TREE);
16683 return;
16686 /* Consume the `:' token. */
16687 cp_lexer_consume_token (parser->lexer);
16688 /* And the mem-initializer-list. */
16689 cp_parser_mem_initializer_list (parser);
16692 /* Parse a mem-initializer-list.
16694 mem-initializer-list:
16695 mem-initializer ... [opt]
16696 mem-initializer ... [opt] , mem-initializer-list */
16698 static void
16699 cp_parser_mem_initializer_list (cp_parser* parser)
16701 tree mem_initializer_list = NULL_TREE;
16702 tree target_ctor = error_mark_node;
16703 cp_token *token = cp_lexer_peek_token (parser->lexer);
16705 /* Let the semantic analysis code know that we are starting the
16706 mem-initializer-list. */
16707 if (!DECL_CONSTRUCTOR_P (current_function_decl))
16708 error_at (token->location,
16709 "only constructors take member initializers");
16711 /* Loop through the list. */
16712 while (true)
16714 tree mem_initializer;
16716 token = cp_lexer_peek_token (parser->lexer);
16717 /* Parse the mem-initializer. */
16718 mem_initializer = cp_parser_mem_initializer (parser);
16719 /* If the next token is a `...', we're expanding member initializers. */
16720 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16721 if (ellipsis
16722 || (mem_initializer != error_mark_node
16723 && check_for_bare_parameter_packs (TREE_PURPOSE
16724 (mem_initializer))))
16726 /* Consume the `...'. */
16727 if (ellipsis)
16728 cp_lexer_consume_token (parser->lexer);
16730 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
16731 can be expanded but members cannot. */
16732 if (mem_initializer != error_mark_node
16733 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
16735 error_at (token->location,
16736 "cannot expand initializer for member %qD",
16737 TREE_PURPOSE (mem_initializer));
16738 mem_initializer = error_mark_node;
16741 /* Construct the pack expansion type. */
16742 if (mem_initializer != error_mark_node)
16743 mem_initializer = make_pack_expansion (mem_initializer);
16745 if (target_ctor != error_mark_node
16746 && mem_initializer != error_mark_node)
16748 error ("mem-initializer for %qD follows constructor delegation",
16749 TREE_PURPOSE (mem_initializer));
16750 mem_initializer = error_mark_node;
16752 /* Look for a target constructor. */
16753 if (mem_initializer != error_mark_node
16754 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
16755 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
16757 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
16758 if (mem_initializer_list)
16760 error ("constructor delegation follows mem-initializer for %qD",
16761 TREE_PURPOSE (mem_initializer_list));
16762 mem_initializer = error_mark_node;
16764 target_ctor = mem_initializer;
16766 /* Add it to the list, unless it was erroneous. */
16767 if (mem_initializer != error_mark_node)
16769 TREE_CHAIN (mem_initializer) = mem_initializer_list;
16770 mem_initializer_list = mem_initializer;
16772 /* If the next token is not a `,', we're done. */
16773 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16774 break;
16775 /* Consume the `,' token. */
16776 cp_lexer_consume_token (parser->lexer);
16779 /* Perform semantic analysis. */
16780 if (DECL_CONSTRUCTOR_P (current_function_decl))
16781 finish_mem_initializers (mem_initializer_list);
16784 /* Parse a mem-initializer.
16786 mem-initializer:
16787 mem-initializer-id ( expression-list [opt] )
16788 mem-initializer-id braced-init-list
16790 GNU extension:
16792 mem-initializer:
16793 ( expression-list [opt] )
16795 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
16796 class) or FIELD_DECL (for a non-static data member) to initialize;
16797 the TREE_VALUE is the expression-list. An empty initialization
16798 list is represented by void_list_node. */
16800 static tree
16801 cp_parser_mem_initializer (cp_parser* parser)
16803 tree mem_initializer_id;
16804 tree expression_list;
16805 tree member;
16806 cp_token *token = cp_lexer_peek_token (parser->lexer);
16808 /* Find out what is being initialized. */
16809 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16811 permerror (token->location,
16812 "anachronistic old-style base class initializer");
16813 mem_initializer_id = NULL_TREE;
16815 else
16817 mem_initializer_id = cp_parser_mem_initializer_id (parser);
16818 if (mem_initializer_id == error_mark_node)
16819 return mem_initializer_id;
16821 member = expand_member_init (mem_initializer_id);
16822 if (member && !DECL_P (member))
16823 in_base_initializer = 1;
16825 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16827 bool expr_non_constant_p;
16828 cp_lexer_set_source_position (parser->lexer);
16829 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16830 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
16831 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
16832 expression_list = build_tree_list (NULL_TREE, expression_list);
16834 else
16836 vec<tree, va_gc> *vec;
16837 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16838 /*cast_p=*/false,
16839 /*allow_expansion_p=*/true,
16840 /*non_constant_p=*/NULL,
16841 /*close_paren_loc=*/NULL,
16842 /*wrap_locations_p=*/true);
16843 if (vec == NULL)
16844 return error_mark_node;
16845 expression_list = build_tree_list_vec (vec);
16846 release_tree_vector (vec);
16849 if (expression_list == error_mark_node)
16850 return error_mark_node;
16851 if (!expression_list)
16852 expression_list = void_type_node;
16854 in_base_initializer = 0;
16856 if (!member)
16857 return error_mark_node;
16858 tree node = build_tree_list (member, expression_list);
16860 /* We can't attach the source location of this initializer directly to
16861 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
16862 within the TREE_TYPE of the list node. */
16863 location_t loc
16864 = make_location (token->location, token->location, parser->lexer);
16865 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
16866 SET_EXPR_LOCATION (dummy, loc);
16867 TREE_TYPE (node) = dummy;
16869 return node;
16872 /* Parse a mem-initializer-id.
16874 mem-initializer-id:
16875 :: [opt] nested-name-specifier [opt] class-name
16876 decltype-specifier (C++11)
16877 identifier
16879 Returns a TYPE indicating the class to be initialized for the first
16880 production (and the second in C++11). Returns an IDENTIFIER_NODE
16881 indicating the data member to be initialized for the last production. */
16883 static tree
16884 cp_parser_mem_initializer_id (cp_parser* parser)
16886 bool global_scope_p;
16887 bool nested_name_specifier_p;
16888 bool template_p = false;
16889 tree id;
16891 cp_token *token = cp_lexer_peek_token (parser->lexer);
16893 /* `typename' is not allowed in this context ([temp.res]). */
16894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16896 error_at (token->location,
16897 "keyword %<typename%> not allowed in this context (a qualified "
16898 "member initializer is implicitly a type)");
16899 cp_lexer_consume_token (parser->lexer);
16901 /* Look for the optional `::' operator. */
16902 global_scope_p
16903 = (cp_parser_global_scope_opt (parser,
16904 /*current_scope_valid_p=*/false)
16905 != NULL_TREE);
16906 /* Look for the optional nested-name-specifier. The simplest way to
16907 implement:
16909 [temp.res]
16911 The keyword `typename' is not permitted in a base-specifier or
16912 mem-initializer; in these contexts a qualified name that
16913 depends on a template-parameter is implicitly assumed to be a
16914 type name.
16916 is to assume that we have seen the `typename' keyword at this
16917 point. */
16918 nested_name_specifier_p
16919 = (cp_parser_nested_name_specifier_opt (parser,
16920 /*typename_keyword_p=*/true,
16921 /*check_dependency_p=*/true,
16922 /*type_p=*/true,
16923 /*is_declaration=*/true)
16924 != NULL_TREE);
16925 if (nested_name_specifier_p)
16926 template_p = cp_parser_optional_template_keyword (parser);
16927 /* If there is a `::' operator or a nested-name-specifier, then we
16928 are definitely looking for a class-name. */
16929 if (global_scope_p || nested_name_specifier_p)
16930 return cp_parser_class_name (parser,
16931 /*typename_keyword_p=*/true,
16932 /*template_keyword_p=*/template_p,
16933 typename_type,
16934 /*check_dependency_p=*/true,
16935 /*class_head_p=*/false,
16936 /*is_declaration=*/true);
16937 /* Otherwise, we could also be looking for an ordinary identifier. */
16938 cp_parser_parse_tentatively (parser);
16939 if (cp_lexer_next_token_is_decltype (parser->lexer))
16940 /* Try a decltype-specifier. */
16941 id = cp_parser_decltype (parser);
16942 else
16943 /* Otherwise, try a class-name. */
16944 id = cp_parser_class_name (parser,
16945 /*typename_keyword_p=*/true,
16946 /*template_keyword_p=*/false,
16947 none_type,
16948 /*check_dependency_p=*/true,
16949 /*class_head_p=*/false,
16950 /*is_declaration=*/true);
16951 /* If we found one, we're done. */
16952 if (cp_parser_parse_definitely (parser))
16953 return id;
16954 /* Otherwise, look for an ordinary identifier. */
16955 return cp_parser_identifier (parser);
16958 /* Overloading [gram.over] */
16960 /* Parse an operator-function-id.
16962 operator-function-id:
16963 operator operator
16965 Returns an IDENTIFIER_NODE for the operator which is a
16966 human-readable spelling of the identifier, e.g., `operator +'. */
16968 static cp_expr
16969 cp_parser_operator_function_id (cp_parser* parser)
16971 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
16972 /* Look for the `operator' keyword. */
16973 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16974 return error_mark_node;
16975 /* And then the name of the operator itself. */
16976 return cp_parser_operator (parser, start_loc);
16979 /* Return an identifier node for a user-defined literal operator.
16980 The suffix identifier is chained to the operator name identifier. */
16982 tree
16983 cp_literal_operator_id (const char* name)
16985 tree identifier;
16986 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
16987 + strlen (name) + 10);
16988 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
16989 identifier = get_identifier (buffer);
16990 XDELETEVEC (buffer);
16992 return identifier;
16995 /* Parse an operator.
16997 operator:
16998 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
16999 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17000 || ++ -- , ->* -> () []
17002 GNU Extensions:
17004 operator:
17005 <? >? <?= >?=
17007 Returns an IDENTIFIER_NODE for the operator which is a
17008 human-readable spelling of the identifier, e.g., `operator +'. */
17010 static cp_expr
17011 cp_parser_operator (cp_parser* parser, location_t start_loc)
17013 tree id = NULL_TREE;
17014 cp_token *token;
17015 bool utf8 = false;
17017 /* Peek at the next token. */
17018 token = cp_lexer_peek_token (parser->lexer);
17020 location_t end_loc = token->location;
17022 /* Figure out which operator we have. */
17023 enum tree_code op = ERROR_MARK;
17024 bool assop = false;
17025 bool consumed = false;
17026 switch (token->type)
17028 case CPP_KEYWORD:
17030 /* The keyword should be either `new', `delete' or `co_await'. */
17031 if (token->keyword == RID_NEW)
17032 op = NEW_EXPR;
17033 else if (token->keyword == RID_DELETE)
17034 op = DELETE_EXPR;
17035 else if (token->keyword == RID_CO_AWAIT)
17036 op = CO_AWAIT_EXPR;
17037 else
17038 break;
17040 /* Consume the `new', `delete' or co_await token. */
17041 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17043 /* Peek at the next token. */
17044 token = cp_lexer_peek_token (parser->lexer);
17045 /* If it's a `[' token then this is the array variant of the
17046 operator. */
17047 if (token->type == CPP_OPEN_SQUARE
17048 && op != CO_AWAIT_EXPR)
17050 /* Consume the `[' token. */
17051 cp_lexer_consume_token (parser->lexer);
17052 /* Look for the `]' token. */
17053 if (cp_token *close_token
17054 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17055 end_loc = close_token->location;
17056 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17058 consumed = true;
17059 break;
17062 case CPP_PLUS:
17063 op = PLUS_EXPR;
17064 break;
17066 case CPP_MINUS:
17067 op = MINUS_EXPR;
17068 break;
17070 case CPP_MULT:
17071 op = MULT_EXPR;
17072 break;
17074 case CPP_DIV:
17075 op = TRUNC_DIV_EXPR;
17076 break;
17078 case CPP_MOD:
17079 op = TRUNC_MOD_EXPR;
17080 break;
17082 case CPP_XOR:
17083 op = BIT_XOR_EXPR;
17084 break;
17086 case CPP_AND:
17087 op = BIT_AND_EXPR;
17088 break;
17090 case CPP_OR:
17091 op = BIT_IOR_EXPR;
17092 break;
17094 case CPP_COMPL:
17095 op = BIT_NOT_EXPR;
17096 break;
17098 case CPP_NOT:
17099 op = TRUTH_NOT_EXPR;
17100 break;
17102 case CPP_EQ:
17103 assop = true;
17104 op = NOP_EXPR;
17105 break;
17107 case CPP_LESS:
17108 op = LT_EXPR;
17109 break;
17111 case CPP_GREATER:
17112 op = GT_EXPR;
17113 break;
17115 case CPP_PLUS_EQ:
17116 assop = true;
17117 op = PLUS_EXPR;
17118 break;
17120 case CPP_MINUS_EQ:
17121 assop = true;
17122 op = MINUS_EXPR;
17123 break;
17125 case CPP_MULT_EQ:
17126 assop = true;
17127 op = MULT_EXPR;
17128 break;
17130 case CPP_DIV_EQ:
17131 assop = true;
17132 op = TRUNC_DIV_EXPR;
17133 break;
17135 case CPP_MOD_EQ:
17136 assop = true;
17137 op = TRUNC_MOD_EXPR;
17138 break;
17140 case CPP_XOR_EQ:
17141 assop = true;
17142 op = BIT_XOR_EXPR;
17143 break;
17145 case CPP_AND_EQ:
17146 assop = true;
17147 op = BIT_AND_EXPR;
17148 break;
17150 case CPP_OR_EQ:
17151 assop = true;
17152 op = BIT_IOR_EXPR;
17153 break;
17155 case CPP_LSHIFT:
17156 op = LSHIFT_EXPR;
17157 break;
17159 case CPP_RSHIFT:
17160 op = RSHIFT_EXPR;
17161 break;
17163 case CPP_LSHIFT_EQ:
17164 assop = true;
17165 op = LSHIFT_EXPR;
17166 break;
17168 case CPP_RSHIFT_EQ:
17169 assop = true;
17170 op = RSHIFT_EXPR;
17171 break;
17173 case CPP_EQ_EQ:
17174 op = EQ_EXPR;
17175 break;
17177 case CPP_NOT_EQ:
17178 op = NE_EXPR;
17179 break;
17181 case CPP_LESS_EQ:
17182 op = LE_EXPR;
17183 break;
17185 case CPP_GREATER_EQ:
17186 op = GE_EXPR;
17187 break;
17189 case CPP_SPACESHIP:
17190 op = SPACESHIP_EXPR;
17191 break;
17193 case CPP_AND_AND:
17194 op = TRUTH_ANDIF_EXPR;
17195 break;
17197 case CPP_OR_OR:
17198 op = TRUTH_ORIF_EXPR;
17199 break;
17201 case CPP_PLUS_PLUS:
17202 op = POSTINCREMENT_EXPR;
17203 break;
17205 case CPP_MINUS_MINUS:
17206 op = PREDECREMENT_EXPR;
17207 break;
17209 case CPP_COMMA:
17210 op = COMPOUND_EXPR;
17211 break;
17213 case CPP_DEREF_STAR:
17214 op = MEMBER_REF;
17215 break;
17217 case CPP_DEREF:
17218 op = COMPONENT_REF;
17219 break;
17221 case CPP_QUERY:
17222 op = COND_EXPR;
17223 /* Consume the `?'. */
17224 cp_lexer_consume_token (parser->lexer);
17225 /* Look for the matching `:'. */
17226 cp_parser_require (parser, CPP_COLON, RT_COLON);
17227 consumed = true;
17228 break;
17230 case CPP_OPEN_PAREN:
17232 /* Consume the `('. */
17233 matching_parens parens;
17234 parens.consume_open (parser);
17235 /* Look for the matching `)'. */
17236 token = parens.require_close (parser);
17237 if (token)
17238 end_loc = token->location;
17239 op = CALL_EXPR;
17240 consumed = true;
17241 break;
17244 case CPP_OPEN_SQUARE:
17245 /* Consume the `['. */
17246 cp_lexer_consume_token (parser->lexer);
17247 /* Look for the matching `]'. */
17248 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17249 if (token)
17250 end_loc = token->location;
17251 op = ARRAY_REF;
17252 consumed = true;
17253 break;
17255 case CPP_UTF8STRING:
17256 case CPP_UTF8STRING_USERDEF:
17257 utf8 = true;
17258 /* FALLTHRU */
17259 case CPP_STRING:
17260 case CPP_WSTRING:
17261 case CPP_STRING16:
17262 case CPP_STRING32:
17263 case CPP_STRING_USERDEF:
17264 case CPP_WSTRING_USERDEF:
17265 case CPP_STRING16_USERDEF:
17266 case CPP_STRING32_USERDEF:
17268 cp_expr str;
17269 tree string_tree;
17270 int sz, len;
17272 if (cxx_dialect == cxx98)
17273 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17275 /* Consume the string. */
17276 str = cp_parser_string_literal (parser, /*translate=*/true,
17277 /*wide_ok=*/true, /*lookup_udlit=*/false);
17278 if (str == error_mark_node)
17279 return error_mark_node;
17280 else if (TREE_CODE (str) == USERDEF_LITERAL)
17282 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17283 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17284 end_loc = str.get_location ();
17286 else
17288 string_tree = str;
17289 /* Look for the suffix identifier. */
17290 token = cp_lexer_peek_token (parser->lexer);
17291 if (token->type == CPP_NAME)
17293 id = cp_parser_identifier (parser);
17294 end_loc = token->location;
17296 else if (token->type == CPP_KEYWORD)
17298 error ("unexpected keyword;"
17299 " remove space between quotes and suffix identifier");
17300 return error_mark_node;
17302 else
17304 error ("expected suffix identifier");
17305 return error_mark_node;
17308 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17309 (TREE_TYPE (TREE_TYPE (string_tree))));
17310 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17311 if (len != 0)
17313 error ("expected empty string after %<operator%> keyword");
17314 return error_mark_node;
17316 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17317 != char_type_node)
17319 error ("invalid encoding prefix in literal operator");
17320 return error_mark_node;
17322 if (id != error_mark_node)
17324 const char *name = IDENTIFIER_POINTER (id);
17325 id = cp_literal_operator_id (name);
17327 /* Generate a location of the form:
17328 "" _suffix_identifier
17329 ^~~~~~~~~~~~~~~~~~~~~
17330 with caret == start at the start token, finish at the end of the
17331 suffix identifier. */
17332 location_t combined_loc
17333 = make_location (start_loc, start_loc, parser->lexer);
17334 return cp_expr (id, combined_loc);
17337 default:
17338 /* Anything else is an error. */
17339 break;
17342 /* If we have selected an identifier, we need to consume the
17343 operator token. */
17344 if (op != ERROR_MARK)
17346 id = ovl_op_identifier (assop, op);
17347 if (!consumed)
17348 cp_lexer_consume_token (parser->lexer);
17350 /* Otherwise, no valid operator name was present. */
17351 else
17353 cp_parser_error (parser, "expected operator");
17354 id = error_mark_node;
17357 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
17358 return cp_expr (id, start_loc);
17361 /* Parse a template-declaration.
17363 template-declaration:
17364 export [opt] template < template-parameter-list > declaration
17366 If MEMBER_P is TRUE, this template-declaration occurs within a
17367 class-specifier.
17369 The grammar rule given by the standard isn't correct. What
17370 is really meant is:
17372 template-declaration:
17373 export [opt] template-parameter-list-seq
17374 decl-specifier-seq [opt] init-declarator [opt] ;
17375 export [opt] template-parameter-list-seq
17376 function-definition
17378 template-parameter-list-seq:
17379 template-parameter-list-seq [opt]
17380 template < template-parameter-list >
17382 Concept Extensions:
17384 template-parameter-list-seq:
17385 template < template-parameter-list > requires-clause [opt]
17387 requires-clause:
17388 requires logical-or-expression */
17390 static void
17391 cp_parser_template_declaration (cp_parser* parser, bool member_p)
17393 /* Check for `export'. */
17394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
17396 /* Consume the `export' token. */
17397 cp_lexer_consume_token (parser->lexer);
17398 /* Warn that this use of export is deprecated. */
17399 if (cxx_dialect < cxx11)
17400 warning (0, "keyword %<export%> not implemented, and will be ignored");
17401 else if (cxx_dialect < cxx20)
17402 warning (0, "keyword %<export%> is deprecated, and is ignored");
17403 else
17404 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17407 cp_parser_template_declaration_after_export (parser, member_p);
17410 /* Parse a template-parameter-list.
17412 template-parameter-list:
17413 template-parameter
17414 template-parameter-list , template-parameter
17416 Returns a TREE_LIST. Each node represents a template parameter.
17417 The nodes are connected via their TREE_CHAINs. */
17419 static tree
17420 cp_parser_template_parameter_list (cp_parser* parser)
17422 tree parameter_list = NULL_TREE;
17424 /* Don't create wrapper nodes within a template-parameter-list,
17425 since we don't want to have different types based on the
17426 spelling location of constants and decls within them. */
17427 auto_suppress_location_wrappers sentinel;
17429 begin_template_parm_list ();
17431 /* The loop below parses the template parms. We first need to know
17432 the total number of template parms to be able to compute proper
17433 canonical types of each dependent type. So after the loop, when
17434 we know the total number of template parms,
17435 end_template_parm_list computes the proper canonical types and
17436 fixes up the dependent types accordingly. */
17437 while (true)
17439 tree parameter;
17440 bool is_non_type;
17441 bool is_parameter_pack;
17442 location_t parm_loc;
17444 /* Parse the template-parameter. */
17445 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
17446 parameter = cp_parser_template_parameter (parser,
17447 &is_non_type,
17448 &is_parameter_pack);
17449 /* Add it to the list. */
17450 if (parameter != error_mark_node)
17451 parameter_list = process_template_parm (parameter_list,
17452 parm_loc,
17453 parameter,
17454 is_non_type,
17455 is_parameter_pack);
17456 else
17458 tree err_parm = build_tree_list (parameter, parameter);
17459 parameter_list = chainon (parameter_list, err_parm);
17462 /* If the next token is not a `,', we're done. */
17463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17464 break;
17465 /* Otherwise, consume the `,' token. */
17466 cp_lexer_consume_token (parser->lexer);
17469 return end_template_parm_list (parameter_list);
17472 /* Parse a introduction-list.
17474 introduction-list:
17475 introduced-parameter
17476 introduction-list , introduced-parameter
17478 introduced-parameter:
17479 ...[opt] identifier
17481 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
17482 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
17483 WILDCARD_DECL will also have DECL_NAME set and token location in
17484 DECL_SOURCE_LOCATION. */
17486 static tree
17487 cp_parser_introduction_list (cp_parser *parser)
17489 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
17491 while (true)
17493 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17494 if (is_pack)
17495 cp_lexer_consume_token (parser->lexer);
17497 tree identifier = cp_parser_identifier (parser);
17498 if (identifier == error_mark_node)
17499 break;
17501 /* Build placeholder. */
17502 tree parm = build_nt (WILDCARD_DECL);
17503 DECL_SOURCE_LOCATION (parm)
17504 = cp_lexer_peek_token (parser->lexer)->location;
17505 DECL_NAME (parm) = identifier;
17506 WILDCARD_PACK_P (parm) = is_pack;
17507 vec_safe_push (introduction_vec, parm);
17509 /* If the next token is not a `,', we're done. */
17510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17511 break;
17512 /* Otherwise, consume the `,' token. */
17513 cp_lexer_consume_token (parser->lexer);
17516 /* Convert the vec into a TREE_VEC. */
17517 tree introduction_list = make_tree_vec (introduction_vec->length ());
17518 unsigned int n;
17519 tree parm;
17520 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
17521 TREE_VEC_ELT (introduction_list, n) = parm;
17523 release_tree_vector (introduction_vec);
17524 return introduction_list;
17527 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17528 is an abstract declarator. */
17530 static inline cp_declarator*
17531 get_id_declarator (cp_declarator *declarator)
17533 cp_declarator *d = declarator;
17534 while (d && d->kind != cdk_id)
17535 d = d->declarator;
17536 return d;
17539 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17540 is an abstract declarator. */
17542 static inline tree
17543 get_unqualified_id (cp_declarator *declarator)
17545 declarator = get_id_declarator (declarator);
17546 if (declarator)
17547 return declarator->u.id.unqualified_name;
17548 else
17549 return NULL_TREE;
17552 /* Returns true if TYPE would declare a constrained constrained-parameter. */
17554 static inline bool
17555 is_constrained_parameter (tree type)
17557 return (type
17558 && TREE_CODE (type) == TYPE_DECL
17559 && CONSTRAINED_PARM_CONCEPT (type)
17560 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
17563 /* Returns true if PARM declares a constrained-parameter. */
17565 static inline bool
17566 is_constrained_parameter (cp_parameter_declarator *parm)
17568 return is_constrained_parameter (parm->decl_specifiers.type);
17571 /* Check that the type parameter is only a declarator-id, and that its
17572 type is not cv-qualified. */
17574 bool
17575 cp_parser_check_constrained_type_parm (cp_parser *parser,
17576 cp_parameter_declarator *parm)
17578 if (!parm->declarator)
17579 return true;
17581 if (parm->declarator->kind != cdk_id)
17583 cp_parser_error (parser, "invalid constrained type parameter");
17584 return false;
17587 /* Don't allow cv-qualified type parameters. */
17588 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
17589 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
17591 cp_parser_error (parser, "cv-qualified type parameter");
17592 return false;
17595 return true;
17598 /* Finish parsing/processing a template type parameter and checking
17599 various restrictions. */
17601 static inline tree
17602 cp_parser_constrained_type_template_parm (cp_parser *parser,
17603 tree id,
17604 cp_parameter_declarator* parmdecl)
17606 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
17607 return finish_template_type_parm (class_type_node, id);
17608 else
17609 return error_mark_node;
17612 static tree
17613 finish_constrained_template_template_parm (tree proto, tree id)
17615 /* FIXME: This should probably be copied, and we may need to adjust
17616 the template parameter depths. */
17617 tree saved_parms = current_template_parms;
17618 begin_template_parm_list ();
17619 current_template_parms = DECL_TEMPLATE_PARMS (proto);
17620 end_template_parm_list ();
17622 tree parm = finish_template_template_parm (class_type_node, id);
17623 current_template_parms = saved_parms;
17625 return parm;
17628 /* Finish parsing/processing a template template parameter by borrowing
17629 the template parameter list from the prototype parameter. */
17631 static tree
17632 cp_parser_constrained_template_template_parm (cp_parser *parser,
17633 tree proto,
17634 tree id,
17635 cp_parameter_declarator *parmdecl)
17637 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
17638 return error_mark_node;
17639 return finish_constrained_template_template_parm (proto, id);
17642 /* Create a new non-type template parameter from the given PARM
17643 declarator. */
17645 static tree
17646 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
17647 cp_parameter_declarator *parm)
17649 *is_non_type = true;
17650 cp_declarator *decl = parm->declarator;
17651 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
17652 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
17653 return grokdeclarator (decl, specs, TPARM, 0, NULL);
17656 /* Build a constrained template parameter based on the PARMDECL
17657 declarator. The type of PARMDECL is the constrained type, which
17658 refers to the prototype template parameter that ultimately
17659 specifies the type of the declared parameter. */
17661 static tree
17662 finish_constrained_parameter (cp_parser *parser,
17663 cp_parameter_declarator *parmdecl,
17664 bool *is_non_type)
17666 tree decl = parmdecl->decl_specifiers.type;
17667 tree id = get_unqualified_id (parmdecl->declarator);
17668 tree def = parmdecl->default_argument;
17669 tree proto = DECL_INITIAL (decl);
17671 /* Build the parameter. Return an error if the declarator was invalid. */
17672 tree parm;
17673 if (TREE_CODE (proto) == TYPE_DECL)
17674 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
17675 else if (TREE_CODE (proto) == TEMPLATE_DECL)
17676 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
17677 parmdecl);
17678 else
17679 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
17680 if (parm == error_mark_node)
17681 return error_mark_node;
17683 /* Finish the parameter decl and create a node attaching the
17684 default argument and constraint. */
17685 parm = build_tree_list (def, parm);
17686 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
17688 return parm;
17691 /* Returns true if the parsed type actually represents the declaration
17692 of a type template-parameter. */
17694 static bool
17695 declares_constrained_type_template_parameter (tree type)
17697 return (is_constrained_parameter (type)
17698 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
17701 /* Returns true if the parsed type actually represents the declaration of
17702 a template template-parameter. */
17704 static bool
17705 declares_constrained_template_template_parameter (tree type)
17707 return (is_constrained_parameter (type)
17708 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
17711 /* Parse a default argument for a type template-parameter.
17712 Note that diagnostics are handled in cp_parser_template_parameter. */
17714 static tree
17715 cp_parser_default_type_template_argument (cp_parser *parser)
17717 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17719 /* Consume the `=' token. */
17720 cp_lexer_consume_token (parser->lexer);
17722 cp_token *token = cp_lexer_peek_token (parser->lexer);
17724 /* Tell cp_parser_lambda_expression this is a default argument. */
17725 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
17726 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
17728 /* Parse the default-argument. */
17729 push_deferring_access_checks (dk_no_deferred);
17730 tree default_argument = cp_parser_type_id (parser,
17731 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17732 NULL);
17733 pop_deferring_access_checks ();
17735 if (flag_concepts && type_uses_auto (default_argument))
17737 error_at (token->location,
17738 "invalid use of %<auto%> in default template argument");
17739 return error_mark_node;
17742 return default_argument;
17745 /* Parse a default argument for a template template-parameter. */
17747 static tree
17748 cp_parser_default_template_template_argument (cp_parser *parser)
17750 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17752 bool is_template;
17754 /* Consume the `='. */
17755 cp_lexer_consume_token (parser->lexer);
17756 /* Parse the id-expression. */
17757 push_deferring_access_checks (dk_no_deferred);
17758 /* save token before parsing the id-expression, for error
17759 reporting */
17760 const cp_token* token = cp_lexer_peek_token (parser->lexer);
17761 tree default_argument
17762 = cp_parser_id_expression (parser,
17763 /*template_keyword_p=*/false,
17764 /*check_dependency_p=*/true,
17765 /*template_p=*/&is_template,
17766 /*declarator_p=*/false,
17767 /*optional_p=*/false);
17768 if (TREE_CODE (default_argument) == TYPE_DECL)
17769 /* If the id-expression was a template-id that refers to
17770 a template-class, we already have the declaration here,
17771 so no further lookup is needed. */
17773 else
17774 /* Look up the name. */
17775 default_argument
17776 = cp_parser_lookup_name (parser, default_argument,
17777 none_type,
17778 /*is_template=*/is_template,
17779 /*is_namespace=*/false,
17780 /*check_dependency=*/true,
17781 /*ambiguous_decls=*/NULL,
17782 token->location);
17783 /* See if the default argument is valid. */
17784 default_argument = check_template_template_default_arg (default_argument);
17785 pop_deferring_access_checks ();
17786 return default_argument;
17789 /* Parse a template-parameter.
17791 template-parameter:
17792 type-parameter
17793 parameter-declaration
17795 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
17796 the parameter. The TREE_PURPOSE is the default value, if any.
17797 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
17798 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
17799 set to true iff this parameter is a parameter pack. */
17801 static tree
17802 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
17803 bool *is_parameter_pack)
17805 cp_token *token;
17806 cp_parameter_declarator *parameter_declarator;
17807 tree parm;
17809 /* Assume it is a type parameter or a template parameter. */
17810 *is_non_type = false;
17811 /* Assume it not a parameter pack. */
17812 *is_parameter_pack = false;
17813 /* Peek at the next token. */
17814 token = cp_lexer_peek_token (parser->lexer);
17815 /* If it is `template', we have a type-parameter. */
17816 if (token->keyword == RID_TEMPLATE)
17817 return cp_parser_type_parameter (parser, is_parameter_pack);
17818 /* If it is `class' or `typename' we do not know yet whether it is a
17819 type parameter or a non-type parameter. Consider:
17821 template <typename T, typename T::X X> ...
17825 template <class C, class D*> ...
17827 Here, the first parameter is a type parameter, and the second is
17828 a non-type parameter. We can tell by looking at the token after
17829 the identifier -- if it is a `,', `=', or `>' then we have a type
17830 parameter. */
17831 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
17833 /* Peek at the token after `class' or `typename'. */
17834 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17835 /* If it's an ellipsis, we have a template type parameter
17836 pack. */
17837 if (token->type == CPP_ELLIPSIS)
17838 return cp_parser_type_parameter (parser, is_parameter_pack);
17839 /* If it's an identifier, skip it. */
17840 if (token->type == CPP_NAME)
17841 token = cp_lexer_peek_nth_token (parser->lexer, 3);
17842 /* Now, see if the token looks like the end of a template
17843 parameter. */
17844 if (token->type == CPP_COMMA
17845 || token->type == CPP_EQ
17846 || token->type == CPP_GREATER)
17847 return cp_parser_type_parameter (parser, is_parameter_pack);
17850 /* Otherwise, it is a non-type parameter or a constrained parameter.
17852 [temp.param]
17854 When parsing a default template-argument for a non-type
17855 template-parameter, the first non-nested `>' is taken as the end
17856 of the template parameter-list rather than a greater-than
17857 operator. */
17858 parameter_declarator
17859 = cp_parser_parameter_declaration (parser,
17860 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17861 /*template_parm_p=*/true,
17862 /*parenthesized_p=*/NULL);
17864 if (!parameter_declarator)
17865 return error_mark_node;
17867 /* If the parameter declaration is marked as a parameter pack, set
17868 *IS_PARAMETER_PACK to notify the caller. */
17869 if (parameter_declarator->template_parameter_pack_p)
17870 *is_parameter_pack = true;
17872 if (parameter_declarator->default_argument)
17874 /* Can happen in some cases of erroneous input (c++/34892). */
17875 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17876 /* Consume the `...' for better error recovery. */
17877 cp_lexer_consume_token (parser->lexer);
17880 /* The parameter may have been constrained type parameter. */
17881 if (is_constrained_parameter (parameter_declarator))
17882 return finish_constrained_parameter (parser,
17883 parameter_declarator,
17884 is_non_type);
17886 // Now we're sure that the parameter is a non-type parameter.
17887 *is_non_type = true;
17889 parm = grokdeclarator (parameter_declarator->declarator,
17890 &parameter_declarator->decl_specifiers,
17891 TPARM, /*initialized=*/0,
17892 /*attrlist=*/NULL);
17893 if (parm == error_mark_node)
17894 return error_mark_node;
17896 return build_tree_list (parameter_declarator->default_argument, parm);
17899 /* Parse a type-parameter.
17901 type-parameter:
17902 class identifier [opt]
17903 class identifier [opt] = type-id
17904 typename identifier [opt]
17905 typename identifier [opt] = type-id
17906 template < template-parameter-list > class identifier [opt]
17907 template < template-parameter-list > class identifier [opt]
17908 = id-expression
17910 GNU Extension (variadic templates):
17912 type-parameter:
17913 class ... identifier [opt]
17914 typename ... identifier [opt]
17916 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
17917 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
17918 the declaration of the parameter.
17920 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
17922 static tree
17923 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
17925 cp_token *token;
17926 tree parameter;
17928 /* Look for a keyword to tell us what kind of parameter this is. */
17929 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
17930 if (!token)
17931 return error_mark_node;
17933 switch (token->keyword)
17935 case RID_CLASS:
17936 case RID_TYPENAME:
17938 tree identifier;
17939 tree default_argument;
17941 /* If the next token is an ellipsis, we have a template
17942 argument pack. */
17943 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17945 /* Consume the `...' token. */
17946 cp_lexer_consume_token (parser->lexer);
17947 maybe_warn_variadic_templates ();
17949 *is_parameter_pack = true;
17952 /* If the next token is an identifier, then it names the
17953 parameter. */
17954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17955 identifier = cp_parser_identifier (parser);
17956 else
17957 identifier = NULL_TREE;
17959 /* Create the parameter. */
17960 parameter = finish_template_type_parm (class_type_node, identifier);
17962 /* If the next token is an `=', we have a default argument. */
17963 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17965 default_argument
17966 = cp_parser_default_type_template_argument (parser);
17968 /* Template parameter packs cannot have default
17969 arguments. */
17970 if (*is_parameter_pack)
17972 if (identifier)
17973 error_at (token->location,
17974 "template parameter pack %qD cannot have a "
17975 "default argument", identifier);
17976 else
17977 error_at (token->location,
17978 "template parameter packs cannot have "
17979 "default arguments");
17980 default_argument = NULL_TREE;
17982 else if (check_for_bare_parameter_packs (default_argument))
17983 default_argument = error_mark_node;
17985 else
17986 default_argument = NULL_TREE;
17988 /* Create the combined representation of the parameter and the
17989 default argument. */
17990 parameter = build_tree_list (default_argument, parameter);
17992 break;
17994 case RID_TEMPLATE:
17996 tree identifier;
17997 tree default_argument;
17999 /* Look for the `<'. */
18000 cp_parser_require (parser, CPP_LESS, RT_LESS);
18001 /* Parse the template-parameter-list. */
18002 cp_parser_template_parameter_list (parser);
18003 /* Look for the `>'. */
18004 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18006 /* If template requirements are present, parse them. */
18007 if (flag_concepts)
18009 tree reqs = get_shorthand_constraints (current_template_parms);
18010 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18011 reqs = combine_constraint_expressions (reqs, dreqs);
18012 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18015 /* Look for the `class' or 'typename' keywords. */
18016 cp_parser_type_parameter_key (parser);
18017 /* If the next token is an ellipsis, we have a template
18018 argument pack. */
18019 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18021 /* Consume the `...' token. */
18022 cp_lexer_consume_token (parser->lexer);
18023 maybe_warn_variadic_templates ();
18025 *is_parameter_pack = true;
18027 /* If the next token is an `=', then there is a
18028 default-argument. If the next token is a `>', we are at
18029 the end of the parameter-list. If the next token is a `,',
18030 then we are at the end of this parameter. */
18031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18032 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18033 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18035 identifier = cp_parser_identifier (parser);
18036 /* Treat invalid names as if the parameter were nameless. */
18037 if (identifier == error_mark_node)
18038 identifier = NULL_TREE;
18040 else
18041 identifier = NULL_TREE;
18043 /* Create the template parameter. */
18044 parameter = finish_template_template_parm (class_type_node,
18045 identifier);
18047 /* If the next token is an `=', then there is a
18048 default-argument. */
18049 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18051 default_argument
18052 = cp_parser_default_template_template_argument (parser);
18054 /* Template parameter packs cannot have default
18055 arguments. */
18056 if (*is_parameter_pack)
18058 if (identifier)
18059 error_at (token->location,
18060 "template parameter pack %qD cannot "
18061 "have a default argument",
18062 identifier);
18063 else
18064 error_at (token->location, "template parameter packs cannot "
18065 "have default arguments");
18066 default_argument = NULL_TREE;
18069 else
18070 default_argument = NULL_TREE;
18072 /* Create the combined representation of the parameter and the
18073 default argument. */
18074 parameter = build_tree_list (default_argument, parameter);
18076 break;
18078 default:
18079 gcc_unreachable ();
18080 break;
18083 return parameter;
18086 /* Parse a template-id.
18088 template-id:
18089 template-name < template-argument-list [opt] >
18091 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18092 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18093 returned. Otherwise, if the template-name names a function, or set
18094 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18095 names a class, returns a TYPE_DECL for the specialization.
18097 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18098 uninstantiated templates. */
18100 static tree
18101 cp_parser_template_id (cp_parser *parser,
18102 bool template_keyword_p,
18103 bool check_dependency_p,
18104 enum tag_types tag_type,
18105 bool is_declaration)
18107 tree templ;
18108 tree arguments;
18109 tree template_id;
18110 cp_token_position start_of_id = 0;
18111 cp_token *next_token = NULL, *next_token_2 = NULL;
18112 bool is_identifier;
18114 /* If the next token corresponds to a template-id, there is no need
18115 to reparse it. */
18116 cp_token *token = cp_lexer_peek_token (parser->lexer);
18118 if (token->type == CPP_TEMPLATE_ID)
18120 cp_lexer_consume_token (parser->lexer);
18121 return saved_checks_value (token->u.tree_check_value);
18124 /* Avoid performing name lookup if there is no possibility of
18125 finding a template-id. */
18126 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18127 || (token->type == CPP_NAME
18128 && !cp_parser_nth_token_starts_template_argument_list_p
18129 (parser, 2)))
18131 cp_parser_error (parser, "expected template-id");
18132 return error_mark_node;
18135 /* Remember where the template-id starts. */
18136 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18137 start_of_id = cp_lexer_token_position (parser->lexer, false);
18139 push_deferring_access_checks (dk_deferred);
18141 /* Parse the template-name. */
18142 is_identifier = false;
18143 templ = cp_parser_template_name (parser, template_keyword_p,
18144 check_dependency_p,
18145 is_declaration,
18146 tag_type,
18147 &is_identifier);
18149 /* Push any access checks inside the firewall we're about to create. */
18150 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18151 pop_deferring_access_checks ();
18152 if (templ == error_mark_node || is_identifier)
18153 return templ;
18155 /* Since we're going to preserve any side-effects from this parse, set up a
18156 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18157 in the template arguments. */
18158 tentative_firewall firewall (parser);
18159 reopen_deferring_access_checks (checks);
18161 /* If we find the sequence `[:' after a template-name, it's probably
18162 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18163 parse correctly the argument list. */
18164 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18165 == CPP_OPEN_SQUARE)
18166 && next_token->flags & DIGRAPH
18167 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18168 == CPP_COLON)
18169 && !(next_token_2->flags & PREV_WHITE))
18171 cp_parser_parse_tentatively (parser);
18172 /* Change `:' into `::'. */
18173 next_token_2->type = CPP_SCOPE;
18174 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18175 CPP_LESS. */
18176 cp_lexer_consume_token (parser->lexer);
18178 /* Parse the arguments. */
18179 arguments = cp_parser_enclosed_template_argument_list (parser);
18180 if (!cp_parser_parse_definitely (parser))
18182 /* If we couldn't parse an argument list, then we revert our changes
18183 and return simply an error. Maybe this is not a template-id
18184 after all. */
18185 next_token_2->type = CPP_COLON;
18186 cp_parser_error (parser, "expected %<<%>");
18187 pop_deferring_access_checks ();
18188 return error_mark_node;
18190 /* Otherwise, emit an error about the invalid digraph, but continue
18191 parsing because we got our argument list. */
18192 if (permerror (next_token->location,
18193 "%<<::%> cannot begin a template-argument list"))
18195 static bool hint = false;
18196 inform (next_token->location,
18197 "%<<:%> is an alternate spelling for %<[%>."
18198 " Insert whitespace between %<<%> and %<::%>");
18199 if (!hint && !flag_permissive)
18201 inform (next_token->location, "(if you use %<-fpermissive%> "
18202 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18203 "accept your code)");
18204 hint = true;
18208 else
18210 /* Look for the `<' that starts the template-argument-list. */
18211 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18213 pop_deferring_access_checks ();
18214 return error_mark_node;
18216 /* Parse the arguments. */
18217 arguments = cp_parser_enclosed_template_argument_list (parser);
18219 if ((cxx_dialect > cxx17)
18220 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18221 && !template_keyword_p
18222 && (cp_parser_error_occurred (parser)
18223 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18225 /* This didn't go well. */
18226 if (TREE_CODE (templ) == FUNCTION_DECL)
18228 /* C++20 says that "function-name < a;" is now ill-formed. */
18229 if (cp_parser_error_occurred (parser))
18231 error_at (token->location, "invalid template-argument-list");
18232 inform (token->location, "function name as the left hand "
18233 "operand of %<<%> is ill-formed in C++20; wrap the "
18234 "function name in %<()%>");
18236 else
18237 /* We expect "f<targs>" to be followed by "(args)". */
18238 error_at (cp_lexer_peek_token (parser->lexer)->location,
18239 "expected %<(%> after template-argument-list");
18240 if (start_of_id)
18241 /* Purge all subsequent tokens. */
18242 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18244 else
18245 cp_parser_simulate_error (parser);
18246 pop_deferring_access_checks ();
18247 return error_mark_node;
18251 /* Set the location to be of the form:
18252 template-name < template-argument-list [opt] >
18253 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18254 with caret == start at the start of the template-name,
18255 ranging until the closing '>'. */
18256 location_t combined_loc
18257 = make_location (token->location, token->location, parser->lexer);
18259 /* Check for concepts autos where they don't belong. We could
18260 identify types in some cases of identifier TEMPL, looking ahead
18261 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18262 types. We reject them in functions, but if what we have is an
18263 identifier, even with none_type we can't conclude it's NOT a
18264 type, we have to wait for template substitution. */
18265 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18266 template_id = error_mark_node;
18267 /* Build a representation of the specialization. */
18268 else if (identifier_p (templ))
18269 template_id = build_min_nt_loc (combined_loc,
18270 TEMPLATE_ID_EXPR,
18271 templ, arguments);
18272 else if (DECL_TYPE_TEMPLATE_P (templ)
18273 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18275 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18276 template (rather than some instantiation thereof) only if
18277 is not nested within some other construct. For example, in
18278 "template <typename T> void f(T) { A<T>::", A<T> is just an
18279 instantiation of A. */
18280 bool entering_scope
18281 = (template_parm_scope_p ()
18282 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18283 template_id
18284 = finish_template_type (templ, arguments, entering_scope);
18286 else if (concept_definition_p (templ))
18288 /* The caller will decide whether this is a concept check or type
18289 constraint. */
18290 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18291 boolean_type_node, templ, arguments);
18293 else if (variable_template_p (templ))
18295 template_id = lookup_template_variable (templ, arguments);
18296 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18297 SET_EXPR_LOCATION (template_id, combined_loc);
18299 else if (TREE_CODE (templ) == TYPE_DECL
18300 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18302 /* Some type template in dependent scope. */
18303 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18304 name = build_min_nt_loc (combined_loc,
18305 TEMPLATE_ID_EXPR,
18306 name, arguments);
18307 template_id = templ;
18309 else
18311 /* If it's not a class-template or a template-template, it should be
18312 a function-template. */
18313 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18315 template_id = lookup_template_function (templ, arguments);
18316 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18317 SET_EXPR_LOCATION (template_id, combined_loc);
18320 /* If parsing tentatively, replace the sequence of tokens that makes
18321 up the template-id with a CPP_TEMPLATE_ID token. That way,
18322 should we re-parse the token stream, we will not have to repeat
18323 the effort required to do the parse, nor will we issue duplicate
18324 error messages about problems during instantiation of the
18325 template. */
18326 if (start_of_id
18327 /* Don't do this if we had a parse error in a declarator; re-parsing
18328 might succeed if a name changes meaning (60361). */
18329 && !(cp_parser_error_occurred (parser)
18330 && cp_parser_parsing_tentatively (parser)
18331 && parser->in_declarator_p))
18333 /* Reset the contents of the START_OF_ID token. */
18334 token->type = CPP_TEMPLATE_ID;
18335 token->location = combined_loc;
18337 /* Retrieve any deferred checks. Do not pop this access checks yet
18338 so the memory will not be reclaimed during token replacing below. */
18339 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18340 token->tree_check_p = true;
18341 token->u.tree_check_value->value = template_id;
18342 token->u.tree_check_value->checks = get_deferred_access_checks ();
18343 token->keyword = RID_MAX;
18345 /* Purge all subsequent tokens. */
18346 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18348 /* ??? Can we actually assume that, if template_id ==
18349 error_mark_node, we will have issued a diagnostic to the
18350 user, as opposed to simply marking the tentative parse as
18351 failed? */
18352 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
18353 error_at (token->location, "parse error in template argument list");
18356 pop_to_parent_deferring_access_checks ();
18357 return template_id;
18360 /* Like cp_parser_template_id, called in non-type context. */
18362 static tree
18363 cp_parser_template_id_expr (cp_parser *parser,
18364 bool template_keyword_p,
18365 bool check_dependency_p,
18366 bool is_declaration)
18368 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
18369 none_type, is_declaration);
18370 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
18371 && concept_check_p (x))
18372 /* We didn't check the arguments in cp_parser_template_id; do that now. */
18373 return build_concept_id (x);
18374 return x;
18377 /* Parse a template-name.
18379 template-name:
18380 identifier
18382 The standard should actually say:
18384 template-name:
18385 identifier
18386 operator-function-id
18388 A defect report has been filed about this issue.
18390 A conversion-function-id cannot be a template name because they cannot
18391 be part of a template-id. In fact, looking at this code:
18393 a.operator K<int>()
18395 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18396 It is impossible to call a templated conversion-function-id with an
18397 explicit argument list, since the only allowed template parameter is
18398 the type to which it is converting.
18400 If TEMPLATE_KEYWORD_P is true, then we have just seen the
18401 `template' keyword, in a construction like:
18403 T::template f<3>()
18405 In that case `f' is taken to be a template-name, even though there
18406 is no way of knowing for sure.
18408 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18409 name refers to a set of overloaded functions, at least one of which
18410 is a template, or an IDENTIFIER_NODE with the name of the template,
18411 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
18412 names are looked up inside uninstantiated templates. */
18414 static tree
18415 cp_parser_template_name (cp_parser* parser,
18416 bool template_keyword_p,
18417 bool check_dependency_p,
18418 bool is_declaration,
18419 enum tag_types tag_type,
18420 bool *is_identifier)
18422 tree identifier;
18423 tree decl;
18424 cp_token *token = cp_lexer_peek_token (parser->lexer);
18426 /* If the next token is `operator', then we have either an
18427 operator-function-id or a conversion-function-id. */
18428 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
18430 /* We don't know whether we're looking at an
18431 operator-function-id or a conversion-function-id. */
18432 cp_parser_parse_tentatively (parser);
18433 /* Try an operator-function-id. */
18434 identifier = cp_parser_operator_function_id (parser);
18435 /* If that didn't work, try a conversion-function-id. */
18436 if (!cp_parser_parse_definitely (parser))
18438 cp_parser_error (parser, "expected template-name");
18439 return error_mark_node;
18442 /* Look for the identifier. */
18443 else
18444 identifier = cp_parser_identifier (parser);
18446 /* If we didn't find an identifier, we don't have a template-id. */
18447 if (identifier == error_mark_node)
18448 return error_mark_node;
18450 /* If the name immediately followed the `template' keyword, then it
18451 is a template-name. However, if the next token is not `<', then
18452 we do not treat it as a template-name, since it is not being used
18453 as part of a template-id. This enables us to handle constructs
18454 like:
18456 template <typename T> struct S { S(); };
18457 template <typename T> S<T>::S();
18459 correctly. We would treat `S' as a template -- if it were `S<T>'
18460 -- but we do not if there is no `<'. */
18462 if (processing_template_decl
18463 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18465 /* In a declaration, in a dependent context, we pretend that the
18466 "template" keyword was present in order to improve error
18467 recovery. For example, given:
18469 template <typename T> void f(T::X<int>);
18471 we want to treat "X<int>" as a template-id. */
18472 if (is_declaration
18473 && !template_keyword_p
18474 && parser->scope && TYPE_P (parser->scope)
18475 && check_dependency_p
18476 && dependent_scope_p (parser->scope)
18477 /* Do not do this for dtors (or ctors), since they never
18478 need the template keyword before their name. */
18479 && !constructor_name_p (identifier, parser->scope))
18481 cp_token_position start = 0;
18483 /* Explain what went wrong. */
18484 error_at (token->location, "non-template %qD used as template",
18485 identifier);
18486 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
18487 parser->scope, identifier);
18488 /* If parsing tentatively, find the location of the "<" token. */
18489 if (cp_parser_simulate_error (parser))
18490 start = cp_lexer_token_position (parser->lexer, true);
18491 /* Parse the template arguments so that we can issue error
18492 messages about them. */
18493 cp_lexer_consume_token (parser->lexer);
18494 cp_parser_enclosed_template_argument_list (parser);
18495 /* Skip tokens until we find a good place from which to
18496 continue parsing. */
18497 cp_parser_skip_to_closing_parenthesis (parser,
18498 /*recovering=*/true,
18499 /*or_comma=*/true,
18500 /*consume_paren=*/false);
18501 /* If parsing tentatively, permanently remove the
18502 template argument list. That will prevent duplicate
18503 error messages from being issued about the missing
18504 "template" keyword. */
18505 if (start)
18506 cp_lexer_purge_tokens_after (parser->lexer, start);
18507 if (is_identifier)
18508 *is_identifier = true;
18509 parser->context->object_type = NULL_TREE;
18510 return identifier;
18513 /* If the "template" keyword is present, then there is generally
18514 no point in doing name-lookup, so we just return IDENTIFIER.
18515 But, if the qualifying scope is non-dependent then we can
18516 (and must) do name-lookup normally. */
18517 if (template_keyword_p)
18519 tree scope = (parser->scope ? parser->scope
18520 : parser->context->object_type);
18521 if (scope && TYPE_P (scope)
18522 && (!CLASS_TYPE_P (scope)
18523 || (check_dependency_p && dependent_type_p (scope))))
18525 /* We're optimizing away the call to cp_parser_lookup_name, but
18526 we still need to do this. */
18527 parser->object_scope = parser->context->object_type;
18528 parser->context->object_type = NULL_TREE;
18529 return identifier;
18534 /* cp_parser_lookup_name clears OBJECT_TYPE. */
18535 tree scope = (parser->scope ? parser->scope
18536 : parser->context->object_type);
18538 /* Look up the name. */
18539 decl = cp_parser_lookup_name (parser, identifier,
18540 tag_type,
18541 /*is_template=*/true,
18542 /*is_namespace=*/false,
18543 check_dependency_p,
18544 /*ambiguous_decls=*/NULL,
18545 token->location);
18547 decl = strip_using_decl (decl);
18549 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18550 template-argument-list if it follows a name that is not a
18551 conversion-function-id and
18552 - that follows the keyword template or a ~ after a nested-name-specifier or
18553 in a class member access expression, or
18554 - for which name lookup finds the injected-class-name of a class template
18555 or finds any declaration of a template, or
18556 - that is an unqualified name for which name lookup either finds one or
18557 more functions or finds nothing, or
18558 - that is a terminal name in a using-declarator (9.9), in a declarator-id
18559 (9.3.4), or in a type-only context other than a nested-name-specifier
18560 (13.8). */
18562 /* If DECL is a template, then the name was a template-name. */
18563 if (TREE_CODE (decl) == TEMPLATE_DECL)
18565 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
18566 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
18568 tree d = DECL_TEMPLATE_RESULT (decl);
18569 tree attr;
18570 if (TREE_CODE (d) == TYPE_DECL)
18571 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
18572 else
18573 attr = DECL_ATTRIBUTES (d);
18574 if (TREE_UNAVAILABLE (decl))
18576 attr = lookup_attribute ("unavailable", attr);
18577 error_unavailable_use (decl, attr);
18579 else if (TREE_DEPRECATED (decl)
18580 && deprecated_state != DEPRECATED_SUPPRESS)
18582 attr = lookup_attribute ("deprecated", attr);
18583 warn_deprecated_use (decl, attr);
18587 else
18589 /* Look through an overload set for any templates. */
18590 bool found = false;
18592 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
18593 !found && iter; ++iter)
18594 if (TREE_CODE (*iter) == TEMPLATE_DECL)
18595 found = true;
18597 /* "an unqualified name for which name lookup either finds one or more
18598 functions or finds nothing". */
18599 if (!found
18600 && (cxx_dialect > cxx17)
18601 && !scope
18602 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
18603 && tag_type == none_type)
18605 /* The "more functions" case. Just use the OVERLOAD as normally.
18606 We don't use is_overloaded_fn here to avoid considering
18607 BASELINKs. */
18608 if (TREE_CODE (decl) == OVERLOAD
18609 /* Name lookup found one function. */
18610 || TREE_CODE (decl) == FUNCTION_DECL
18611 /* Name lookup found nothing. */
18612 || decl == error_mark_node)
18613 found = true;
18616 /* "in a type-only context" */
18617 if (!found && scope
18618 && tag_type != none_type
18619 && dependentish_scope_p (scope)
18620 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18621 found = true;
18623 if (!found)
18625 /* The name does not name a template. */
18626 cp_parser_error (parser, "expected template-name");
18627 return error_mark_node;
18629 else if (decl == error_mark_node)
18630 /* Repeat the lookup at instantiation time. */
18631 decl = identifier;
18634 return decl;
18637 /* Parse a template-argument-list.
18639 template-argument-list:
18640 template-argument ... [opt]
18641 template-argument-list , template-argument ... [opt]
18643 Returns a TREE_VEC containing the arguments. */
18645 static tree
18646 cp_parser_template_argument_list (cp_parser* parser)
18648 bool saved_in_template_argument_list_p;
18649 bool saved_ice_p;
18650 bool saved_non_ice_p;
18652 /* Don't create location wrapper nodes within a template-argument-list. */
18653 auto_suppress_location_wrappers sentinel;
18655 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
18656 parser->in_template_argument_list_p = true;
18657 /* Even if the template-id appears in an integral
18658 constant-expression, the contents of the argument list do
18659 not. */
18660 saved_ice_p = parser->integral_constant_expression_p;
18661 parser->integral_constant_expression_p = false;
18662 saved_non_ice_p = parser->non_integral_constant_expression_p;
18663 parser->non_integral_constant_expression_p = false;
18665 /* Parse the arguments. */
18666 auto_vec<tree, 10> args;
18669 if (!args.is_empty ())
18670 /* Consume the comma. */
18671 cp_lexer_consume_token (parser->lexer);
18673 /* Parse the template-argument. */
18674 tree argument = cp_parser_template_argument (parser);
18676 /* If the next token is an ellipsis, we're expanding a template
18677 argument pack. */
18678 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18680 if (argument == error_mark_node)
18682 cp_token *token = cp_lexer_peek_token (parser->lexer);
18683 error_at (token->location,
18684 "expected parameter pack before %<...%>");
18686 /* Consume the `...' token. */
18687 cp_lexer_consume_token (parser->lexer);
18689 /* Make the argument into a TYPE_PACK_EXPANSION or
18690 EXPR_PACK_EXPANSION. */
18691 argument = make_pack_expansion (argument);
18694 args.safe_push (argument);
18696 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18698 int n_args = args.length ();
18699 tree vec = make_tree_vec (n_args);
18701 for (int i = 0; i < n_args; i++)
18702 TREE_VEC_ELT (vec, i) = args[i];
18704 parser->non_integral_constant_expression_p = saved_non_ice_p;
18705 parser->integral_constant_expression_p = saved_ice_p;
18706 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
18707 if (CHECKING_P)
18708 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
18709 return vec;
18712 /* Parse a template-argument.
18714 template-argument:
18715 assignment-expression
18716 type-id
18717 id-expression
18719 The representation is that of an assignment-expression, type-id, or
18720 id-expression -- except that the qualified id-expression is
18721 evaluated, so that the value returned is either a DECL or an
18722 OVERLOAD.
18724 Although the standard says "assignment-expression", it forbids
18725 throw-expressions or assignments in the template argument.
18726 Therefore, we use "conditional-expression" instead. */
18728 static tree
18729 cp_parser_template_argument (cp_parser* parser)
18731 tree argument;
18732 bool template_p;
18733 bool address_p;
18734 bool maybe_type_id = false;
18735 cp_token *token = NULL, *argument_start_token = NULL;
18736 location_t loc = 0;
18737 cp_id_kind idk;
18739 /* There's really no way to know what we're looking at, so we just
18740 try each alternative in order.
18742 [temp.arg]
18744 In a template-argument, an ambiguity between a type-id and an
18745 expression is resolved to a type-id, regardless of the form of
18746 the corresponding template-parameter.
18748 Therefore, we try a type-id first. */
18749 cp_parser_parse_tentatively (parser);
18750 argument = cp_parser_template_type_arg (parser);
18751 /* If there was no error parsing the type-id but the next token is a
18752 '>>', our behavior depends on which dialect of C++ we're
18753 parsing. In C++98, we probably found a typo for '> >'. But there
18754 are type-id which are also valid expressions. For instance:
18756 struct X { int operator >> (int); };
18757 template <int V> struct Foo {};
18758 Foo<X () >> 5> r;
18760 Here 'X()' is a valid type-id of a function type, but the user just
18761 wanted to write the expression "X() >> 5". Thus, we remember that we
18762 found a valid type-id, but we still try to parse the argument as an
18763 expression to see what happens.
18765 In C++0x, the '>>' will be considered two separate '>'
18766 tokens. */
18767 if (!cp_parser_error_occurred (parser)
18768 && cxx_dialect == cxx98
18769 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18771 maybe_type_id = true;
18772 cp_parser_abort_tentative_parse (parser);
18774 else
18776 /* If the next token isn't a `,' or a `>', then this argument wasn't
18777 really finished. This means that the argument is not a valid
18778 type-id. */
18779 if (!cp_parser_next_token_ends_template_argument_p (parser))
18780 cp_parser_error (parser, "expected template-argument");
18781 /* If that worked, we're done. */
18782 if (cp_parser_parse_definitely (parser))
18783 return argument;
18785 /* We're still not sure what the argument will be. */
18786 cp_parser_parse_tentatively (parser);
18787 /* Try a template. */
18788 argument_start_token = cp_lexer_peek_token (parser->lexer);
18789 argument = cp_parser_id_expression (parser,
18790 /*template_keyword_p=*/false,
18791 /*check_dependency_p=*/true,
18792 &template_p,
18793 /*declarator_p=*/false,
18794 /*optional_p=*/false);
18795 /* If the next token isn't a `,' or a `>', then this argument wasn't
18796 really finished. */
18797 if (!cp_parser_next_token_ends_template_argument_p (parser))
18798 cp_parser_error (parser, "expected template-argument");
18799 if (!cp_parser_error_occurred (parser))
18801 /* Figure out what is being referred to. If the id-expression
18802 was for a class template specialization, then we will have a
18803 TYPE_DECL at this point. There is no need to do name lookup
18804 at this point in that case. */
18805 if (TREE_CODE (argument) != TYPE_DECL)
18806 argument = cp_parser_lookup_name (parser, argument,
18807 none_type,
18808 /*is_template=*/template_p,
18809 /*is_namespace=*/false,
18810 /*check_dependency=*/true,
18811 /*ambiguous_decls=*/NULL,
18812 argument_start_token->location);
18813 if (TREE_CODE (argument) != TEMPLATE_DECL
18814 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
18815 cp_parser_error (parser, "expected template-name");
18817 if (cp_parser_parse_definitely (parser))
18819 if (TREE_UNAVAILABLE (argument))
18820 error_unavailable_use (argument, NULL_TREE);
18821 else if (TREE_DEPRECATED (argument))
18822 warn_deprecated_use (argument, NULL_TREE);
18823 return argument;
18825 /* It must be a non-type argument. In C++17 any constant-expression is
18826 allowed. */
18827 if (cxx_dialect > cxx14)
18828 goto general_expr;
18830 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
18832 -- an integral constant-expression of integral or enumeration
18833 type; or
18835 -- the name of a non-type template-parameter; or
18837 -- the name of an object or function with external linkage...
18839 -- the address of an object or function with external linkage...
18841 -- a pointer to member... */
18842 /* Look for a non-type template parameter. */
18843 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18845 cp_parser_parse_tentatively (parser);
18846 argument = cp_parser_primary_expression (parser,
18847 /*address_p=*/false,
18848 /*cast_p=*/false,
18849 /*template_arg_p=*/true,
18850 &idk);
18851 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
18852 || !cp_parser_next_token_ends_template_argument_p (parser))
18853 cp_parser_simulate_error (parser);
18854 if (cp_parser_parse_definitely (parser))
18855 return argument;
18858 /* If the next token is "&", the argument must be the address of an
18859 object or function with external linkage. */
18860 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
18861 if (address_p)
18863 loc = cp_lexer_peek_token (parser->lexer)->location;
18864 cp_lexer_consume_token (parser->lexer);
18866 /* See if we might have an id-expression. */
18867 token = cp_lexer_peek_token (parser->lexer);
18868 if (token->type == CPP_NAME
18869 || token->keyword == RID_OPERATOR
18870 || token->type == CPP_SCOPE
18871 || token->type == CPP_TEMPLATE_ID
18872 || token->type == CPP_NESTED_NAME_SPECIFIER)
18874 cp_parser_parse_tentatively (parser);
18875 argument = cp_parser_primary_expression (parser,
18876 address_p,
18877 /*cast_p=*/false,
18878 /*template_arg_p=*/true,
18879 &idk);
18880 if (cp_parser_error_occurred (parser)
18881 || !cp_parser_next_token_ends_template_argument_p (parser))
18882 cp_parser_abort_tentative_parse (parser);
18883 else
18885 tree probe;
18887 if (INDIRECT_REF_P (argument))
18889 /* Strip the dereference temporarily. */
18890 gcc_assert (REFERENCE_REF_P (argument));
18891 argument = TREE_OPERAND (argument, 0);
18894 /* If we're in a template, we represent a qualified-id referring
18895 to a static data member as a SCOPE_REF even if the scope isn't
18896 dependent so that we can check access control later. */
18897 probe = argument;
18898 if (TREE_CODE (probe) == SCOPE_REF)
18899 probe = TREE_OPERAND (probe, 1);
18900 if (VAR_P (probe))
18902 /* A variable without external linkage might still be a
18903 valid constant-expression, so no error is issued here
18904 if the external-linkage check fails. */
18905 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
18906 cp_parser_simulate_error (parser);
18908 else if (is_overloaded_fn (argument))
18909 /* All overloaded functions are allowed; if the external
18910 linkage test does not pass, an error will be issued
18911 later. */
18913 else if (address_p
18914 && (TREE_CODE (argument) == OFFSET_REF
18915 || TREE_CODE (argument) == SCOPE_REF))
18916 /* A pointer-to-member. */
18918 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
18920 else
18921 cp_parser_simulate_error (parser);
18923 if (cp_parser_parse_definitely (parser))
18925 if (address_p)
18926 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
18927 NULL_TREE, tf_warning_or_error);
18928 else
18929 argument = convert_from_reference (argument);
18930 return argument;
18934 /* If the argument started with "&", there are no other valid
18935 alternatives at this point. */
18936 if (address_p)
18938 cp_parser_error (parser, "invalid non-type template argument");
18939 return error_mark_node;
18942 general_expr:
18943 /* If the argument wasn't successfully parsed as a type-id followed
18944 by '>>', the argument can only be a constant expression now.
18945 Otherwise, we try parsing the constant-expression tentatively,
18946 because the argument could really be a type-id. */
18947 if (maybe_type_id)
18948 cp_parser_parse_tentatively (parser);
18950 if (cxx_dialect <= cxx14)
18951 argument = cp_parser_constant_expression (parser);
18952 else
18954 /* In C++20, we can encounter a braced-init-list. */
18955 if (cxx_dialect >= cxx20
18956 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18958 bool expr_non_constant_p;
18959 return cp_parser_braced_list (parser, &expr_non_constant_p);
18962 /* With C++17 generalized non-type template arguments we need to handle
18963 lvalue constant expressions, too. */
18964 argument = cp_parser_assignment_expression (parser);
18965 require_potential_constant_expression (argument);
18968 if (!maybe_type_id)
18969 return argument;
18970 if (!cp_parser_next_token_ends_template_argument_p (parser))
18971 cp_parser_error (parser, "expected template-argument");
18972 if (cp_parser_parse_definitely (parser))
18973 return argument;
18974 /* We did our best to parse the argument as a non type-id, but that
18975 was the only alternative that matched (albeit with a '>' after
18976 it). We can assume it's just a typo from the user, and a
18977 diagnostic will then be issued. */
18978 return cp_parser_template_type_arg (parser);
18981 /* Parse an explicit-instantiation.
18983 explicit-instantiation:
18984 template declaration
18986 Although the standard says `declaration', what it really means is:
18988 explicit-instantiation:
18989 template decl-specifier-seq [opt] declarator [opt] ;
18991 Things like `template int S<int>::i = 5, int S<double>::j;' are not
18992 supposed to be allowed. A defect report has been filed about this
18993 issue.
18995 GNU Extension:
18997 explicit-instantiation:
18998 storage-class-specifier template
18999 decl-specifier-seq [opt] declarator [opt] ;
19000 function-specifier template
19001 decl-specifier-seq [opt] declarator [opt] ; */
19003 static void
19004 cp_parser_explicit_instantiation (cp_parser* parser)
19006 int declares_class_or_enum;
19007 cp_decl_specifier_seq decl_specifiers;
19008 tree extension_specifier = NULL_TREE;
19010 timevar_push (TV_TEMPLATE_INST);
19012 /* Look for an (optional) storage-class-specifier or
19013 function-specifier. */
19014 if (cp_parser_allow_gnu_extensions_p (parser))
19016 extension_specifier
19017 = cp_parser_storage_class_specifier_opt (parser);
19018 if (!extension_specifier)
19019 extension_specifier
19020 = cp_parser_function_specifier_opt (parser,
19021 /*decl_specs=*/NULL);
19024 /* Look for the `template' keyword. */
19025 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19026 /* Let the front end know that we are processing an explicit
19027 instantiation. */
19028 begin_explicit_instantiation ();
19029 /* [temp.explicit] says that we are supposed to ignore access
19030 control while processing explicit instantiation directives. */
19031 push_deferring_access_checks (dk_no_check);
19032 /* Parse a decl-specifier-seq. */
19033 cp_parser_decl_specifier_seq (parser,
19034 CP_PARSER_FLAGS_OPTIONAL,
19035 &decl_specifiers,
19036 &declares_class_or_enum);
19038 cp_omp_declare_simd_data odsd;
19039 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19040 cp_parser_handle_directive_omp_attributes (parser,
19041 &decl_specifiers.attributes,
19042 &odsd, true);
19044 /* If there was exactly one decl-specifier, and it declared a class,
19045 and there's no declarator, then we have an explicit type
19046 instantiation. */
19047 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19049 tree type = check_tag_decl (&decl_specifiers,
19050 /*explicit_type_instantiation_p=*/true);
19051 /* Turn access control back on for names used during
19052 template instantiation. */
19053 pop_deferring_access_checks ();
19054 if (type)
19055 do_type_instantiation (type, extension_specifier,
19056 /*complain=*/tf_error);
19058 else
19060 cp_declarator *declarator;
19061 tree decl;
19063 /* Parse the declarator. */
19064 declarator
19065 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19066 CP_PARSER_FLAGS_NONE,
19067 /*ctor_dtor_or_conv_p=*/NULL,
19068 /*parenthesized_p=*/NULL,
19069 /*member_p=*/false,
19070 /*friend_p=*/false,
19071 /*static_p=*/false);
19072 if (declares_class_or_enum & 2)
19073 cp_parser_check_for_definition_in_return_type (declarator,
19074 decl_specifiers.type,
19075 decl_specifiers.locations[ds_type_spec]);
19076 if (declarator != cp_error_declarator)
19078 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19079 permerror (decl_specifiers.locations[ds_inline],
19080 "explicit instantiation shall not use"
19081 " %<inline%> specifier");
19082 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19083 permerror (decl_specifiers.locations[ds_constexpr],
19084 "explicit instantiation shall not use"
19085 " %<constexpr%> specifier");
19086 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19087 permerror (decl_specifiers.locations[ds_consteval],
19088 "explicit instantiation shall not use"
19089 " %<consteval%> specifier");
19091 decl = grokdeclarator (declarator, &decl_specifiers,
19092 NORMAL, 0, &decl_specifiers.attributes);
19093 /* Turn access control back on for names used during
19094 template instantiation. */
19095 pop_deferring_access_checks ();
19096 /* Do the explicit instantiation. */
19097 do_decl_instantiation (decl, extension_specifier);
19099 else
19101 pop_deferring_access_checks ();
19102 /* Skip the body of the explicit instantiation. */
19103 cp_parser_skip_to_end_of_statement (parser);
19106 /* We're done with the instantiation. */
19107 end_explicit_instantiation ();
19109 cp_parser_consume_semicolon_at_end_of_statement (parser);
19111 timevar_pop (TV_TEMPLATE_INST);
19113 cp_finalize_omp_declare_simd (parser, &odsd);
19116 /* Parse an explicit-specialization.
19118 explicit-specialization:
19119 template < > declaration
19121 Although the standard says `declaration', what it really means is:
19123 explicit-specialization:
19124 template <> decl-specifier [opt] init-declarator [opt] ;
19125 template <> function-definition
19126 template <> explicit-specialization
19127 template <> template-declaration */
19129 static void
19130 cp_parser_explicit_specialization (cp_parser* parser)
19132 cp_token *token = cp_lexer_peek_token (parser->lexer);
19134 /* Look for the `template' keyword. */
19135 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19136 /* Look for the `<'. */
19137 cp_parser_require (parser, CPP_LESS, RT_LESS);
19138 /* Look for the `>'. */
19139 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19140 /* We have processed another parameter list. */
19141 ++parser->num_template_parameter_lists;
19143 /* [temp]
19145 A template ... explicit specialization ... shall not have C
19146 linkage. */
19147 bool need_lang_pop = current_lang_name == lang_name_c;
19148 if (need_lang_pop)
19150 error_at (token->location, "template specialization with C linkage");
19151 maybe_show_extern_c_location ();
19153 /* Give it C++ linkage to avoid confusing other parts of the
19154 front end. */
19155 push_lang_context (lang_name_cplusplus);
19158 /* Let the front end know that we are beginning a specialization. */
19159 if (begin_specialization ())
19161 /* If the next keyword is `template', we need to figure out
19162 whether or not we're looking a template-declaration. */
19163 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19165 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19166 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19167 cp_parser_template_declaration_after_export (parser,
19168 /*member_p=*/false);
19169 else
19170 cp_parser_explicit_specialization (parser);
19172 else
19173 /* Parse the dependent declaration. */
19174 cp_parser_single_declaration (parser,
19175 /*checks=*/NULL,
19176 /*member_p=*/false,
19177 /*explicit_specialization_p=*/true,
19178 /*friend_p=*/NULL);
19181 /* We're done with the specialization. */
19182 end_specialization ();
19184 /* For the erroneous case of a template with C linkage, we pushed an
19185 implicit C++ linkage scope; exit that scope now. */
19186 if (need_lang_pop)
19187 pop_lang_context ();
19189 /* We're done with this parameter list. */
19190 --parser->num_template_parameter_lists;
19193 /* Preserve the attributes across a garbage collect (by making it a GC
19194 root), which can occur when parsing a member function. */
19196 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19198 /* Parse a type-specifier.
19200 type-specifier:
19201 simple-type-specifier
19202 class-specifier
19203 enum-specifier
19204 elaborated-type-specifier
19205 cv-qualifier
19207 GNU Extension:
19209 type-specifier:
19210 __complex__
19212 Returns a representation of the type-specifier. For a
19213 class-specifier, enum-specifier, or elaborated-type-specifier, a
19214 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19216 The parser flags FLAGS is used to control type-specifier parsing.
19218 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19219 in a decl-specifier-seq.
19221 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19222 class-specifier, enum-specifier, or elaborated-type-specifier, then
19223 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19224 if a type is declared; 2 if it is defined. Otherwise, it is set to
19225 zero.
19227 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19228 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19229 is set to FALSE. */
19231 static tree
19232 cp_parser_type_specifier (cp_parser* parser,
19233 cp_parser_flags flags,
19234 cp_decl_specifier_seq *decl_specs,
19235 bool is_declaration,
19236 int* declares_class_or_enum,
19237 bool* is_cv_qualifier)
19239 tree type_spec = NULL_TREE;
19240 cp_token *token;
19241 enum rid keyword;
19242 cp_decl_spec ds = ds_last;
19244 /* Assume this type-specifier does not declare a new type. */
19245 if (declares_class_or_enum)
19246 *declares_class_or_enum = 0;
19247 /* And that it does not specify a cv-qualifier. */
19248 if (is_cv_qualifier)
19249 *is_cv_qualifier = false;
19250 /* Peek at the next token. */
19251 token = cp_lexer_peek_token (parser->lexer);
19253 /* If we're looking at a keyword, we can use that to guide the
19254 production we choose. */
19255 keyword = token->keyword;
19256 switch (keyword)
19258 case RID_ENUM:
19259 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19260 goto elaborated_type_specifier;
19262 /* Look for the enum-specifier. */
19263 type_spec = cp_parser_enum_specifier (parser);
19264 /* If that worked, we're done. */
19265 if (type_spec)
19267 if (declares_class_or_enum)
19268 *declares_class_or_enum = 2;
19269 if (decl_specs)
19270 cp_parser_set_decl_spec_type (decl_specs,
19271 type_spec,
19272 token,
19273 /*type_definition_p=*/true);
19274 return type_spec;
19276 else
19277 goto elaborated_type_specifier;
19279 /* Any of these indicate either a class-specifier, or an
19280 elaborated-type-specifier. */
19281 case RID_CLASS:
19282 case RID_STRUCT:
19283 case RID_UNION:
19284 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19285 goto elaborated_type_specifier;
19287 /* Parse tentatively so that we can back up if we don't find a
19288 class-specifier. */
19289 cp_parser_parse_tentatively (parser);
19290 if (decl_specs->attributes)
19291 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19292 /* Look for the class-specifier. */
19293 type_spec = cp_parser_class_specifier (parser);
19294 if (decl_specs->attributes)
19295 cp_parser_decl_specs_attrs->pop ();
19296 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19297 /* If that worked, we're done. */
19298 if (cp_parser_parse_definitely (parser))
19300 if (declares_class_or_enum)
19301 *declares_class_or_enum = 2;
19302 if (decl_specs)
19303 cp_parser_set_decl_spec_type (decl_specs,
19304 type_spec,
19305 token,
19306 /*type_definition_p=*/true);
19307 return type_spec;
19310 /* Fall through. */
19311 elaborated_type_specifier:
19312 /* We're declaring (not defining) a class or enum. */
19313 if (declares_class_or_enum)
19314 *declares_class_or_enum = 1;
19316 /* Fall through. */
19317 case RID_TYPENAME:
19318 /* Look for an elaborated-type-specifier. */
19319 type_spec
19320 = (cp_parser_elaborated_type_specifier
19321 (parser,
19322 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19323 is_declaration));
19324 if (decl_specs)
19325 cp_parser_set_decl_spec_type (decl_specs,
19326 type_spec,
19327 token,
19328 /*type_definition_p=*/false);
19329 return type_spec;
19331 case RID_CONST:
19332 ds = ds_const;
19333 if (is_cv_qualifier)
19334 *is_cv_qualifier = true;
19335 break;
19337 case RID_VOLATILE:
19338 ds = ds_volatile;
19339 if (is_cv_qualifier)
19340 *is_cv_qualifier = true;
19341 break;
19343 case RID_RESTRICT:
19344 ds = ds_restrict;
19345 if (is_cv_qualifier)
19346 *is_cv_qualifier = true;
19347 break;
19349 case RID_COMPLEX:
19350 /* The `__complex__' keyword is a GNU extension. */
19351 ds = ds_complex;
19352 break;
19354 default:
19355 break;
19358 /* Handle simple keywords. */
19359 if (ds != ds_last)
19361 if (decl_specs)
19363 set_and_check_decl_spec_loc (decl_specs, ds, token);
19364 decl_specs->any_specifiers_p = true;
19366 return cp_lexer_consume_token (parser->lexer)->u.value;
19369 /* If we do not already have a type-specifier, assume we are looking
19370 at a simple-type-specifier. */
19371 type_spec = cp_parser_simple_type_specifier (parser,
19372 decl_specs,
19373 flags);
19375 /* If we didn't find a type-specifier, and a type-specifier was not
19376 optional in this context, issue an error message. */
19377 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19379 cp_parser_error (parser, "expected type specifier");
19380 return error_mark_node;
19383 return type_spec;
19386 /* Parse a simple-type-specifier.
19388 simple-type-specifier:
19389 :: [opt] nested-name-specifier [opt] type-name
19390 :: [opt] nested-name-specifier template template-id
19391 char
19392 wchar_t
19393 bool
19394 short
19396 long
19397 signed
19398 unsigned
19399 float
19400 double
19401 void
19403 C++11 Extension:
19405 simple-type-specifier:
19406 auto
19407 decltype ( expression )
19408 char16_t
19409 char32_t
19410 __underlying_type ( type-id )
19412 C++17 extension:
19414 nested-name-specifier(opt) template-name
19416 GNU Extension:
19418 simple-type-specifier:
19419 __int128
19420 __typeof__ unary-expression
19421 __typeof__ ( type-id )
19422 __typeof__ ( type-id ) { initializer-list , [opt] }
19424 Concepts Extension:
19426 simple-type-specifier:
19427 constrained-type-specifier
19429 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
19430 appropriately updated. */
19432 static tree
19433 cp_parser_simple_type_specifier (cp_parser* parser,
19434 cp_decl_specifier_seq *decl_specs,
19435 cp_parser_flags flags)
19437 tree type = NULL_TREE;
19438 cp_token *token;
19439 int idx;
19441 /* Peek at the next token. */
19442 token = cp_lexer_peek_token (parser->lexer);
19444 /* If we're looking at a keyword, things are easy. */
19445 switch (token->keyword)
19447 case RID_CHAR:
19448 if (decl_specs)
19449 decl_specs->explicit_char_p = true;
19450 type = char_type_node;
19451 break;
19452 case RID_CHAR8:
19453 type = char8_type_node;
19454 break;
19455 case RID_CHAR16:
19456 type = char16_type_node;
19457 break;
19458 case RID_CHAR32:
19459 type = char32_type_node;
19460 break;
19461 case RID_WCHAR:
19462 type = wchar_type_node;
19463 break;
19464 case RID_BOOL:
19465 type = boolean_type_node;
19466 break;
19467 case RID_SHORT:
19468 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
19469 type = short_integer_type_node;
19470 break;
19471 case RID_INT:
19472 if (decl_specs)
19473 decl_specs->explicit_int_p = true;
19474 type = integer_type_node;
19475 break;
19476 case RID_INT_N_0:
19477 case RID_INT_N_1:
19478 case RID_INT_N_2:
19479 case RID_INT_N_3:
19480 idx = token->keyword - RID_INT_N_0;
19481 if (! int_n_enabled_p [idx])
19482 break;
19483 if (decl_specs)
19485 decl_specs->explicit_intN_p = true;
19486 decl_specs->int_n_idx = idx;
19487 /* Check if the alternate "__intN__" form has been used instead of
19488 "__intN". */
19489 if (startswith (IDENTIFIER_POINTER (token->u.value)
19490 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
19491 decl_specs->int_n_alt = true;
19493 type = int_n_trees [idx].signed_type;
19494 break;
19495 case RID_LONG:
19496 if (decl_specs)
19497 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
19498 type = long_integer_type_node;
19499 break;
19500 case RID_SIGNED:
19501 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
19502 type = integer_type_node;
19503 break;
19504 case RID_UNSIGNED:
19505 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
19506 type = unsigned_type_node;
19507 break;
19508 case RID_FLOAT:
19509 type = float_type_node;
19510 break;
19511 case RID_DOUBLE:
19512 type = double_type_node;
19513 break;
19514 case RID_VOID:
19515 type = void_type_node;
19516 break;
19518 case RID_AUTO:
19519 maybe_warn_cpp0x (CPP0X_AUTO);
19520 if (parser->auto_is_implicit_function_template_parm_p)
19522 /* The 'auto' might be the placeholder return type for a function decl
19523 with trailing return type. */
19524 bool have_trailing_return_fn_decl = false;
19526 cp_parser_parse_tentatively (parser);
19527 cp_lexer_consume_token (parser->lexer);
19528 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
19529 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
19530 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19531 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
19533 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19535 cp_lexer_consume_token (parser->lexer);
19536 cp_parser_skip_to_closing_parenthesis (parser,
19537 /*recovering*/false,
19538 /*or_comma*/false,
19539 /*consume_paren*/true);
19540 continue;
19543 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
19545 have_trailing_return_fn_decl = true;
19546 break;
19549 cp_lexer_consume_token (parser->lexer);
19551 cp_parser_abort_tentative_parse (parser);
19553 if (have_trailing_return_fn_decl)
19555 type = make_auto ();
19556 break;
19559 if (cxx_dialect >= cxx14)
19561 type = synthesize_implicit_template_parm (parser, NULL_TREE);
19562 type = TREE_TYPE (type);
19564 else
19565 type = error_mark_node;
19567 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
19569 if (cxx_dialect < cxx14)
19570 error_at (token->location,
19571 "use of %<auto%> in lambda parameter declaration "
19572 "only available with "
19573 "%<-std=c++14%> or %<-std=gnu++14%>");
19575 else if (cxx_dialect < cxx14)
19576 error_at (token->location,
19577 "use of %<auto%> in parameter declaration "
19578 "only available with "
19579 "%<-std=c++14%> or %<-std=gnu++14%>");
19580 else if (!flag_concepts)
19581 pedwarn (token->location, 0,
19582 "use of %<auto%> in parameter declaration "
19583 "only available with %<-std=c++20%> or %<-fconcepts%>");
19585 else
19586 type = make_auto ();
19587 break;
19589 case RID_DECLTYPE:
19590 /* Since DR 743, decltype can either be a simple-type-specifier by
19591 itself or begin a nested-name-specifier. Parsing it will replace
19592 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19593 handling below decide what to do. */
19594 cp_parser_decltype (parser);
19595 cp_lexer_set_token_position (parser->lexer, token);
19596 break;
19598 case RID_TYPEOF:
19599 /* Consume the `typeof' token. */
19600 cp_lexer_consume_token (parser->lexer);
19601 /* Parse the operand to `typeof'. */
19602 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
19603 /* If it is not already a TYPE, take its type. */
19604 if (!TYPE_P (type))
19605 type = finish_typeof (type);
19607 if (decl_specs)
19608 cp_parser_set_decl_spec_type (decl_specs, type,
19609 token,
19610 /*type_definition_p=*/false);
19612 return type;
19614 case RID_UNDERLYING_TYPE:
19615 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
19616 if (decl_specs)
19617 cp_parser_set_decl_spec_type (decl_specs, type,
19618 token,
19619 /*type_definition_p=*/false);
19621 return type;
19623 case RID_BASES:
19624 case RID_DIRECT_BASES:
19625 type = cp_parser_trait_expr (parser, token->keyword);
19626 if (decl_specs)
19627 cp_parser_set_decl_spec_type (decl_specs, type,
19628 token,
19629 /*type_definition_p=*/false);
19630 return type;
19631 default:
19632 break;
19635 /* If token is an already-parsed decltype not followed by ::,
19636 it's a simple-type-specifier. */
19637 if (token->type == CPP_DECLTYPE
19638 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
19640 type = saved_checks_value (token->u.tree_check_value);
19641 if (decl_specs)
19643 cp_parser_set_decl_spec_type (decl_specs, type,
19644 token,
19645 /*type_definition_p=*/false);
19646 /* Remember that we are handling a decltype in order to
19647 implement the resolution of DR 1510 when the argument
19648 isn't instantiation dependent. */
19649 decl_specs->decltype_p = true;
19651 cp_lexer_consume_token (parser->lexer);
19652 return type;
19655 /* If the type-specifier was for a built-in type, we're done. */
19656 if (type)
19658 /* Record the type. */
19659 if (decl_specs
19660 && (token->keyword != RID_SIGNED
19661 && token->keyword != RID_UNSIGNED
19662 && token->keyword != RID_SHORT
19663 && token->keyword != RID_LONG))
19664 cp_parser_set_decl_spec_type (decl_specs,
19665 type,
19666 token,
19667 /*type_definition_p=*/false);
19668 if (decl_specs)
19669 decl_specs->any_specifiers_p = true;
19671 /* Consume the token. */
19672 cp_lexer_consume_token (parser->lexer);
19674 if (type == error_mark_node)
19675 return error_mark_node;
19677 /* There is no valid C++ program where a non-template type is
19678 followed by a "<". That usually indicates that the user thought
19679 that the type was a template. */
19680 cp_parser_check_for_invalid_template_id (parser, type, none_type,
19681 token->location);
19683 return TYPE_NAME (type);
19686 /* The type-specifier must be a user-defined type. */
19687 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
19689 bool qualified_p;
19690 bool global_p;
19691 const bool typename_p = (cxx_dialect >= cxx20
19692 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
19694 /* Don't gobble tokens or issue error messages if this is an
19695 optional type-specifier. */
19696 if (flags & CP_PARSER_FLAGS_OPTIONAL)
19697 cp_parser_parse_tentatively (parser);
19699 /* Remember current tentative parsing state -- if we know we need
19700 a type, we can give better diagnostics here. */
19701 bool tent = cp_parser_parsing_tentatively (parser);
19703 token = cp_lexer_peek_token (parser->lexer);
19705 /* Look for the optional `::' operator. */
19706 global_p
19707 = (cp_parser_global_scope_opt (parser,
19708 /*current_scope_valid_p=*/false)
19709 != NULL_TREE);
19710 /* Look for the nested-name specifier. */
19711 qualified_p
19712 = (cp_parser_nested_name_specifier_opt (parser,
19713 /*typename_keyword_p=*/false,
19714 /*check_dependency_p=*/true,
19715 /*type_p=*/false,
19716 /*is_declaration=*/false)
19717 != NULL_TREE);
19718 /* If we have seen a nested-name-specifier, and the next token
19719 is `template', then we are using the template-id production. */
19720 if (parser->scope
19721 && cp_parser_optional_template_keyword (parser))
19723 /* Look for the template-id. */
19724 type = cp_parser_template_id (parser,
19725 /*template_keyword_p=*/true,
19726 /*check_dependency_p=*/true,
19727 none_type,
19728 /*is_declaration=*/false);
19729 /* If the template-id did not name a type, we are out of
19730 luck. */
19731 if (TREE_CODE (type) != TYPE_DECL)
19733 /* ...unless we pretend we have seen 'typename'. */
19734 if (typename_p)
19735 type = cp_parser_make_typename_type (parser, type,
19736 token->location);
19737 else
19739 cp_parser_error (parser, "expected template-id for type");
19740 type = error_mark_node;
19744 /* DR 1812: A < following a qualified-id in a typename-specifier
19745 could safely be assumed to begin a template argument list, so
19746 the template keyword should be optional. */
19747 else if (parser->scope
19748 && qualified_p
19749 && typename_p
19750 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
19752 cp_parser_parse_tentatively (parser);
19754 type = cp_parser_template_id (parser,
19755 /*template_keyword_p=*/true,
19756 /*check_dependency_p=*/true,
19757 none_type,
19758 /*is_declaration=*/false);
19759 /* This is handled below, so back off. */
19760 if (type && concept_check_p (type))
19761 cp_parser_simulate_error (parser);
19763 if (!cp_parser_parse_definitely (parser))
19764 type = NULL_TREE;
19765 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
19766 type = make_typename_type (parser->scope, type, typename_type,
19767 /*complain=*/tf_error);
19768 else if (TREE_CODE (type) != TYPE_DECL)
19769 type = NULL_TREE;
19772 /* Otherwise, look for a type-name. */
19773 if (!type)
19775 if (cxx_dialect >= cxx17)
19776 cp_parser_parse_tentatively (parser);
19778 type = cp_parser_type_name (parser, (qualified_p && typename_p));
19780 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
19781 type = NULL_TREE;
19784 if (!type && flag_concepts && decl_specs)
19786 /* Try for a type-constraint with template arguments. We check
19787 decl_specs here to avoid trying this for a functional cast. */
19789 cp_parser_parse_tentatively (parser);
19791 type = cp_parser_template_id (parser,
19792 /*template_keyword_p=*/false,
19793 /*check_dependency_p=*/true,
19794 none_type,
19795 /*is_declaration=*/false);
19796 if (type && concept_check_p (type))
19798 location_t loc = EXPR_LOCATION (type);
19799 type = cp_parser_placeholder_type_specifier (parser, loc,
19800 type, tent);
19801 if (tent && type == error_mark_node)
19802 /* Perhaps it's a concept-check expression. */
19803 cp_parser_simulate_error (parser);
19805 else
19806 cp_parser_simulate_error (parser);
19808 if (!cp_parser_parse_definitely (parser))
19809 type = NULL_TREE;
19812 if (!type && cxx_dialect >= cxx17)
19814 /* Try class template argument deduction or type-constraint without
19815 template arguments. */
19816 tree name = cp_parser_identifier (parser);
19817 if (name && TREE_CODE (name) == IDENTIFIER_NODE
19818 && parser->scope != error_mark_node)
19820 location_t loc
19821 = cp_lexer_previous_token (parser->lexer)->location;
19822 tree tmpl = cp_parser_lookup_name (parser, name,
19823 none_type,
19824 /*is_template=*/false,
19825 /*is_namespace=*/false,
19826 /*check_dependency=*/true,
19827 /*ambiguous_decls=*/NULL,
19828 token->location);
19829 if (tmpl && tmpl != error_mark_node
19830 && ctad_template_p (tmpl))
19831 type = make_template_placeholder (tmpl);
19832 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
19833 type = cp_parser_placeholder_type_specifier (parser, loc,
19834 tmpl, tent);
19835 else
19837 type = error_mark_node;
19838 if (!cp_parser_simulate_error (parser))
19839 cp_parser_name_lookup_error (parser, name, tmpl,
19840 NLE_TYPE, token->location);
19843 else
19844 type = error_mark_node;
19847 /* If it didn't work out, we don't have a TYPE. */
19848 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
19849 && !cp_parser_parse_definitely (parser))
19850 type = NULL_TREE;
19852 /* Keep track of all name-lookups performed in class scopes. */
19853 if (type
19854 && !global_p
19855 && !qualified_p
19856 && TREE_CODE (type) == TYPE_DECL
19857 && identifier_p (DECL_NAME (type)))
19858 maybe_note_name_used_in_class (DECL_NAME (type), type);
19860 if (type && decl_specs)
19861 cp_parser_set_decl_spec_type (decl_specs, type,
19862 token,
19863 /*type_definition_p=*/false);
19866 /* If we didn't get a type-name, issue an error message. */
19867 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19869 cp_parser_error (parser, "expected type-name");
19870 return error_mark_node;
19873 if (type && type != error_mark_node)
19875 /* See if TYPE is an Objective-C type, and if so, parse and
19876 accept any protocol references following it. Do this before
19877 the cp_parser_check_for_invalid_template_id() call, because
19878 Objective-C types can be followed by '<...>' which would
19879 enclose protocol names rather than template arguments, and so
19880 everything is fine. */
19881 if (c_dialect_objc () && !parser->scope
19882 && (objc_is_id (type) || objc_is_class_name (type)))
19884 tree protos = cp_parser_objc_protocol_refs_opt (parser);
19885 tree qual_type = objc_get_protocol_qualified_type (type, protos);
19887 /* Clobber the "unqualified" type previously entered into
19888 DECL_SPECS with the new, improved protocol-qualified version. */
19889 if (decl_specs)
19890 decl_specs->type = qual_type;
19892 return qual_type;
19895 /* There is no valid C++ program where a non-template type is
19896 followed by a "<". That usually indicates that the user
19897 thought that the type was a template. */
19898 cp_parser_check_for_invalid_template_id (parser, type,
19899 none_type,
19900 token->location);
19903 return type;
19906 /* Parse the remainder of a placholder-type-specifier.
19908 placeholder-type-specifier:
19909 type-constraint_opt auto
19910 type-constraint_opt decltype(auto)
19912 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
19913 and passed as TMPL. This function converts TMPL to an actual type-constraint,
19914 parses the placeholder type, and performs some contextual syntactic analysis.
19916 LOC provides the location of the template name.
19918 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
19919 don't give an error if TMPL isn't a valid type-constraint, as the template-id
19920 might actually be a concept-check,
19922 Note that the Concepts TS allows the auto or decltype(auto) to be
19923 omitted in a constrained-type-specifier. */
19925 static tree
19926 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
19927 tree tmpl, bool tentative)
19929 if (tmpl == error_mark_node)
19930 return error_mark_node;
19932 tree orig_tmpl = tmpl;
19934 /* Get the arguments as written for subsequent analysis. */
19935 tree args = NULL_TREE;
19936 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
19938 args = TREE_OPERAND (tmpl, 1);
19939 tmpl = TREE_OPERAND (tmpl, 0);
19941 else
19942 /* A concept-name with no arguments can't be an expression. */
19943 tentative = false;
19945 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
19947 /* Get the concept and prototype parameter for the constraint. */
19948 tree_pair info = finish_type_constraints (tmpl, args, complain);
19949 tree con = info.first;
19950 tree proto = info.second;
19951 if (con == error_mark_node)
19952 return error_mark_node;
19954 /* As per the standard, require auto or decltype(auto), except in some
19955 cases (template parameter lists, -fconcepts-ts enabled). */
19956 cp_token *placeholder = NULL, *close_paren = NULL;
19957 if (cxx_dialect >= cxx20)
19959 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
19960 placeholder = cp_lexer_consume_token (parser->lexer);
19961 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
19963 placeholder = cp_lexer_consume_token (parser->lexer);
19964 matching_parens parens;
19965 parens.require_open (parser);
19966 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
19967 close_paren = parens.require_close (parser);
19971 /* A type constraint constrains a contextually determined type or type
19972 parameter pack. However, the Concepts TS does allow concepts
19973 to introduce non-type and template template parameters. */
19974 if (TREE_CODE (proto) != TYPE_DECL)
19976 if (!flag_concepts_ts
19977 || !processing_template_parmlist)
19979 if (!tentative)
19981 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
19982 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
19984 return error_mark_node;
19988 /* In a template parameter list, a type-parameter can be introduced
19989 by type-constraints alone. */
19990 if (processing_template_parmlist && !placeholder)
19991 return build_constrained_parameter (con, proto, args);
19993 /* Diagnose issues placeholder issues. */
19994 if (!flag_concepts_ts
19995 && !parser->in_result_type_constraint_p
19996 && !placeholder)
19998 if (tentative)
19999 /* Perhaps it's a concept-check expression (c++/91073). */
20000 return error_mark_node;
20002 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20003 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20004 error_at (input_location,
20005 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20006 /* Fall through. This is an error of omission. */
20008 else if (parser->in_result_type_constraint_p && placeholder)
20010 /* A trailing return type only allows type-constraints. */
20011 error_at (input_location,
20012 "unexpected placeholder in constrained result type");
20015 /* In a parameter-declaration-clause, a placeholder-type-specifier
20016 results in an invented template parameter. */
20017 if (parser->auto_is_implicit_function_template_parm_p)
20019 if (close_paren)
20021 location_t loc = make_location (placeholder->location,
20022 placeholder->location,
20023 close_paren->location);
20024 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20025 return error_mark_node;
20027 tree parm = build_constrained_parameter (con, proto, args);
20028 return synthesize_implicit_template_parm (parser, parm);
20031 /* Determine if the type should be deduced using template argument
20032 deduction or decltype deduction. Note that the latter is always
20033 used for type-constraints in trailing return types. */
20034 bool decltype_p = placeholder
20035 ? placeholder->keyword == RID_DECLTYPE
20036 : parser->in_result_type_constraint_p;
20038 /* Otherwise, this is the type of a variable or return type. */
20039 if (decltype_p)
20040 return make_constrained_decltype_auto (con, args);
20041 else
20042 return make_constrained_auto (con, args);
20045 /* Parse a type-name.
20047 type-name:
20048 class-name
20049 enum-name
20050 typedef-name
20051 simple-template-id [in c++0x]
20053 enum-name:
20054 identifier
20056 typedef-name:
20057 identifier
20059 Concepts:
20061 type-name:
20062 concept-name
20063 partial-concept-id
20065 concept-name:
20066 identifier
20068 Returns a TYPE_DECL for the type. */
20070 static tree
20071 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20073 tree type_decl;
20075 /* We can't know yet whether it is a class-name or not. */
20076 cp_parser_parse_tentatively (parser);
20077 /* Try a class-name. */
20078 type_decl = cp_parser_class_name (parser,
20079 typename_keyword_p,
20080 /*template_keyword_p=*/false,
20081 none_type,
20082 /*check_dependency_p=*/true,
20083 /*class_head_p=*/false,
20084 /*is_declaration=*/false);
20085 /* If it's not a class-name, keep looking. */
20086 if (!cp_parser_parse_definitely (parser))
20088 if (cxx_dialect < cxx11)
20089 /* It must be a typedef-name or an enum-name. */
20090 return cp_parser_nonclass_name (parser);
20092 cp_parser_parse_tentatively (parser);
20093 /* It is either a simple-template-id representing an
20094 instantiation of an alias template... */
20095 type_decl = cp_parser_template_id (parser,
20096 /*template_keyword_p=*/false,
20097 /*check_dependency_p=*/true,
20098 none_type,
20099 /*is_declaration=*/false);
20100 /* Note that this must be an instantiation of an alias template
20101 because [temp.names]/6 says:
20103 A template-id that names an alias template specialization
20104 is a type-name.
20106 Whereas [temp.names]/7 says:
20108 A simple-template-id that names a class template
20109 specialization is a class-name.
20111 With concepts, this could also be a partial-concept-id that
20112 declares a non-type template parameter. */
20113 if (type_decl != NULL_TREE
20114 && TREE_CODE (type_decl) == TYPE_DECL
20115 && TYPE_DECL_ALIAS_P (type_decl))
20116 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20117 else
20118 cp_parser_simulate_error (parser);
20120 if (!cp_parser_parse_definitely (parser))
20121 /* ... Or a typedef-name or an enum-name. */
20122 return cp_parser_nonclass_name (parser);
20125 return type_decl;
20128 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20129 or a concept-name.
20131 enum-name:
20132 identifier
20134 typedef-name:
20135 identifier
20137 concept-name:
20138 identifier
20140 Returns a TYPE_DECL for the type. */
20142 static tree
20143 cp_parser_nonclass_name (cp_parser* parser)
20145 tree type_decl;
20146 tree identifier;
20148 cp_token *token = cp_lexer_peek_token (parser->lexer);
20149 identifier = cp_parser_identifier (parser);
20150 if (identifier == error_mark_node)
20151 return error_mark_node;
20153 /* Look up the type-name. */
20154 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20156 type_decl = strip_using_decl (type_decl);
20158 if (TREE_CODE (type_decl) != TYPE_DECL
20159 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20161 /* See if this is an Objective-C type. */
20162 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20163 tree type = objc_get_protocol_qualified_type (identifier, protos);
20164 if (type)
20165 type_decl = TYPE_NAME (type);
20168 /* Issue an error if we did not find a type-name. */
20169 if (TREE_CODE (type_decl) != TYPE_DECL
20170 /* In Objective-C, we have the complication that class names are
20171 normally type names and start declarations (eg, the
20172 "NSObject" in "NSObject *object;"), but can be used in an
20173 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20174 is an expression. So, a classname followed by a dot is not a
20175 valid type-name. */
20176 || (objc_is_class_name (TREE_TYPE (type_decl))
20177 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20179 if (!cp_parser_simulate_error (parser))
20180 cp_parser_name_lookup_error (parser, identifier, type_decl,
20181 NLE_TYPE, token->location);
20182 return error_mark_node;
20184 /* Remember that the name was used in the definition of the
20185 current class so that we can check later to see if the
20186 meaning would have been different after the class was
20187 entirely defined. */
20188 else if (type_decl != error_mark_node
20189 && !parser->scope)
20190 maybe_note_name_used_in_class (identifier, type_decl);
20192 return type_decl;
20195 /* Parse an elaborated-type-specifier. Note that the grammar given
20196 here incorporates the resolution to DR68.
20198 elaborated-type-specifier:
20199 class-key :: [opt] nested-name-specifier [opt] identifier
20200 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20201 enum-key :: [opt] nested-name-specifier [opt] identifier
20202 typename :: [opt] nested-name-specifier identifier
20203 typename :: [opt] nested-name-specifier template [opt]
20204 template-id
20206 GNU extension:
20208 elaborated-type-specifier:
20209 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20210 class-key attributes :: [opt] nested-name-specifier [opt]
20211 template [opt] template-id
20212 enum attributes :: [opt] nested-name-specifier [opt] identifier
20214 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20215 declared `friend'. If IS_DECLARATION is TRUE, then this
20216 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20217 something is being declared.
20219 Returns the TYPE specified. */
20221 static tree
20222 cp_parser_elaborated_type_specifier (cp_parser* parser,
20223 bool is_friend,
20224 bool is_declaration)
20226 enum tag_types tag_type;
20227 tree identifier;
20228 tree type = NULL_TREE;
20229 tree attributes = NULL_TREE;
20230 tree globalscope;
20231 cp_token *token = NULL;
20233 /* For class and enum types the location of the class-key or enum-key. */
20234 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20235 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20236 rid scoped_key = RID_MAX;
20238 /* See if we're looking at the `enum' keyword. */
20239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20241 /* Consume the `enum' token. */
20242 cp_lexer_consume_token (parser->lexer);
20243 /* Remember that it's an enumeration type. */
20244 tag_type = enum_type;
20245 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20246 enums) is used here. */
20247 cp_token *token = cp_lexer_peek_token (parser->lexer);
20248 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20249 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20251 location_t loc = token->location;
20252 gcc_rich_location richloc (loc);
20253 richloc.add_range (input_location);
20254 richloc.add_fixit_remove ();
20255 pedwarn (&richloc, 0, "elaborated-type-specifier for "
20256 "a scoped enum must not use the %qD keyword",
20257 token->u.value);
20258 /* Consume the `struct' or `class' and parse it anyway. */
20259 cp_lexer_consume_token (parser->lexer);
20260 /* Create a combined location for the whole scoped-enum-key. */
20261 key_loc = make_location (key_loc, key_loc, loc);
20263 else
20264 scoped_key = RID_MAX;
20266 /* Parse the attributes. */
20267 attributes = cp_parser_attributes_opt (parser);
20269 /* Or, it might be `typename'. */
20270 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20271 RID_TYPENAME))
20273 /* Consume the `typename' token. */
20274 cp_lexer_consume_token (parser->lexer);
20275 /* Remember that it's a `typename' type. */
20276 tag_type = typename_type;
20278 /* Otherwise it must be a class-key. */
20279 else
20281 key_loc = cp_lexer_peek_token (parser->lexer)->location;
20282 tag_type = cp_parser_class_key (parser);
20283 if (tag_type == none_type)
20284 return error_mark_node;
20285 /* Parse the attributes. */
20286 attributes = cp_parser_attributes_opt (parser);
20289 /* Look for the `::' operator. */
20290 globalscope = cp_parser_global_scope_opt (parser,
20291 /*current_scope_valid_p=*/false);
20292 /* Look for the nested-name-specifier. */
20293 tree nested_name_specifier;
20294 if (tag_type == typename_type && !globalscope)
20296 nested_name_specifier
20297 = cp_parser_nested_name_specifier (parser,
20298 /*typename_keyword_p=*/true,
20299 /*check_dependency_p=*/true,
20300 /*type_p=*/true,
20301 is_declaration);
20302 if (!nested_name_specifier)
20303 return error_mark_node;
20305 else
20306 /* Even though `typename' is not present, the proposed resolution
20307 to Core Issue 180 says that in `class A<T>::B', `B' should be
20308 considered a type-name, even if `A<T>' is dependent. */
20309 nested_name_specifier
20310 = cp_parser_nested_name_specifier_opt (parser,
20311 /*typename_keyword_p=*/true,
20312 /*check_dependency_p=*/true,
20313 /*type_p=*/true,
20314 is_declaration);
20315 /* For everything but enumeration types, consider a template-id.
20316 For an enumeration type, consider only a plain identifier. */
20317 if (tag_type != enum_type)
20319 bool template_p = false;
20320 tree decl;
20322 /* Allow the `template' keyword. */
20323 template_p = cp_parser_optional_template_keyword (parser);
20324 /* If we didn't see `template', we don't know if there's a
20325 template-id or not. */
20326 if (!template_p)
20327 cp_parser_parse_tentatively (parser);
20328 /* The `template' keyword must follow a nested-name-specifier. */
20329 else if (!nested_name_specifier && !globalscope)
20331 cp_parser_error (parser, "%<template%> must follow a nested-"
20332 "name-specifier");
20333 return error_mark_node;
20336 /* Parse the template-id. */
20337 token = cp_lexer_peek_token (parser->lexer);
20338 decl = cp_parser_template_id (parser, template_p,
20339 /*check_dependency_p=*/true,
20340 tag_type,
20341 is_declaration);
20342 /* If we didn't find a template-id, look for an ordinary
20343 identifier. */
20344 if (!template_p && !cp_parser_parse_definitely (parser))
20346 /* We can get here when cp_parser_template_id, called by
20347 cp_parser_class_name with tag_type == none_type, succeeds
20348 and caches a BASELINK. Then, when called again here,
20349 instead of failing and returning an error_mark_node
20350 returns it (see template/typename17.C in C++11).
20351 ??? Could we diagnose this earlier? */
20352 else if (tag_type == typename_type && BASELINK_P (decl))
20354 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20355 type = error_mark_node;
20357 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20358 in effect, then we must assume that, upon instantiation, the
20359 template will correspond to a class. */
20360 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20361 && tag_type == typename_type)
20362 type = make_typename_type (parser->scope, decl,
20363 typename_type,
20364 /*complain=*/tf_error);
20365 /* If the `typename' keyword is in effect and DECL is not a type
20366 decl, then type is non existent. */
20367 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20369 else if (TREE_CODE (decl) == TYPE_DECL)
20371 type = check_elaborated_type_specifier (tag_type, decl,
20372 /*allow_template_p=*/true);
20374 /* If the next token is a semicolon, this must be a specialization,
20375 instantiation, or friend declaration. Check the scope while we
20376 still know whether or not we had a nested-name-specifier. */
20377 if (type != error_mark_node
20378 && !nested_name_specifier && !is_friend
20379 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20380 check_unqualified_spec_or_inst (type, token->location);
20382 else if (decl == error_mark_node)
20383 type = error_mark_node;
20386 if (!type)
20388 token = cp_lexer_peek_token (parser->lexer);
20389 identifier = cp_parser_identifier (parser);
20391 if (identifier == error_mark_node)
20393 parser->scope = NULL_TREE;
20394 return error_mark_node;
20397 /* For a `typename', we needn't call xref_tag. */
20398 if (tag_type == typename_type
20399 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20400 return cp_parser_make_typename_type (parser, identifier,
20401 token->location);
20403 /* Template parameter lists apply only if we are not within a
20404 function parameter list. */
20405 bool template_parm_lists_apply
20406 = parser->num_template_parameter_lists;
20407 if (template_parm_lists_apply)
20408 for (cp_binding_level *s = current_binding_level;
20409 s && s->kind != sk_template_parms;
20410 s = s->level_chain)
20411 if (s->kind == sk_function_parms)
20412 template_parm_lists_apply = false;
20414 /* Look up a qualified name in the usual way. */
20415 if (parser->scope)
20417 tree decl;
20418 tree ambiguous_decls;
20420 decl = cp_parser_lookup_name (parser, identifier,
20421 tag_type,
20422 /*is_template=*/false,
20423 /*is_namespace=*/false,
20424 /*check_dependency=*/true,
20425 &ambiguous_decls,
20426 token->location);
20428 /* If the lookup was ambiguous, an error will already have been
20429 issued. */
20430 if (ambiguous_decls)
20431 return error_mark_node;
20433 /* If we are parsing friend declaration, DECL may be a
20434 TEMPLATE_DECL tree node here. However, we need to check
20435 whether this TEMPLATE_DECL results in valid code. Consider
20436 the following example:
20438 namespace N {
20439 template <class T> class C {};
20441 class X {
20442 template <class T> friend class N::C; // #1, valid code
20444 template <class T> class Y {
20445 friend class N::C; // #2, invalid code
20448 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20449 name lookup of `N::C'. We see that friend declaration must
20450 be template for the code to be valid. Note that
20451 processing_template_decl does not work here since it is
20452 always 1 for the above two cases. */
20454 decl = (cp_parser_maybe_treat_template_as_class
20455 (decl, /*tag_name_p=*/is_friend
20456 && template_parm_lists_apply));
20458 if (TREE_CODE (decl) != TYPE_DECL)
20460 cp_parser_diagnose_invalid_type_name (parser,
20461 identifier,
20462 token->location);
20463 return error_mark_node;
20466 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20468 bool allow_template = (template_parm_lists_apply
20469 || DECL_SELF_REFERENCE_P (decl));
20470 type = check_elaborated_type_specifier (tag_type, decl,
20471 allow_template);
20473 if (type == error_mark_node)
20474 return error_mark_node;
20477 /* Forward declarations of nested types, such as
20479 class C1::C2;
20480 class C1::C2::C3;
20482 are invalid unless all components preceding the final '::'
20483 are complete. If all enclosing types are complete, these
20484 declarations become merely pointless.
20486 Invalid forward declarations of nested types are errors
20487 caught elsewhere in parsing. Those that are pointless arrive
20488 here. */
20490 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20491 && !is_friend && is_declaration
20492 && !processing_explicit_instantiation)
20493 warning (0, "declaration %qD does not declare anything", decl);
20495 type = TREE_TYPE (decl);
20497 else
20499 /* An elaborated-type-specifier sometimes introduces a new type and
20500 sometimes names an existing type. Normally, the rule is that it
20501 introduces a new type only if there is not an existing type of
20502 the same name already in scope. For example, given:
20504 struct S {};
20505 void f() { struct S s; }
20507 the `struct S' in the body of `f' is the same `struct S' as in
20508 the global scope; the existing definition is used. However, if
20509 there were no global declaration, this would introduce a new
20510 local class named `S'.
20512 An exception to this rule applies to the following code:
20514 namespace N { struct S; }
20516 Here, the elaborated-type-specifier names a new type
20517 unconditionally; even if there is already an `S' in the
20518 containing scope this declaration names a new type.
20519 This exception only applies if the elaborated-type-specifier
20520 forms the complete declaration:
20522 [class.name]
20524 A declaration consisting solely of `class-key identifier ;' is
20525 either a redeclaration of the name in the current scope or a
20526 forward declaration of the identifier as a class name. It
20527 introduces the name into the current scope.
20529 We are in this situation precisely when the next token is a `;'.
20531 An exception to the exception is that a `friend' declaration does
20532 *not* name a new type; i.e., given:
20534 struct S { friend struct T; };
20536 `T' is not a new type in the scope of `S'.
20538 Also, `new struct S' or `sizeof (struct S)' never results in the
20539 definition of a new type; a new type can only be declared in a
20540 declaration context. */
20542 TAG_how how;
20544 if (is_friend)
20545 /* Friends have special name lookup rules. */
20546 how = TAG_how::HIDDEN_FRIEND;
20547 else if (is_declaration
20548 && cp_lexer_next_token_is (parser->lexer,
20549 CPP_SEMICOLON))
20550 /* This is a `class-key identifier ;' */
20551 how = TAG_how::CURRENT_ONLY;
20552 else
20553 how = TAG_how::GLOBAL;
20555 bool template_p =
20556 (template_parm_lists_apply
20557 && (cp_parser_next_token_starts_class_definition_p (parser)
20558 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
20559 /* An unqualified name was used to reference this type, so
20560 there were no qualifying templates. */
20561 if (template_parm_lists_apply
20562 && !cp_parser_check_template_parameters (parser,
20563 /*num_templates=*/0,
20564 /*template_id*/false,
20565 token->location,
20566 /*declarator=*/NULL))
20567 return error_mark_node;
20569 type = xref_tag (tag_type, identifier, how, template_p);
20573 if (type == error_mark_node)
20574 return error_mark_node;
20576 /* Allow attributes on forward declarations of classes. */
20577 if (attributes)
20579 if (TREE_CODE (type) == TYPENAME_TYPE)
20580 warning (OPT_Wattributes,
20581 "attributes ignored on uninstantiated type");
20582 else if (tag_type != enum_type
20583 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
20584 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
20585 && ! processing_explicit_instantiation)
20586 warning (OPT_Wattributes,
20587 "attributes ignored on template instantiation");
20588 else if (is_friend && cxx11_attribute_p (attributes))
20590 if (warning (OPT_Wattributes, "attribute ignored"))
20591 inform (input_location, "an attribute that appertains to a friend "
20592 "declaration that is not a definition is ignored");
20594 else if (is_declaration && cp_parser_declares_only_class_p (parser))
20595 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
20596 else
20597 warning (OPT_Wattributes,
20598 "attributes ignored on elaborated-type-specifier that is "
20599 "not a forward declaration");
20602 if (tag_type == enum_type)
20603 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
20604 else
20606 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
20607 for alias definition. */
20608 bool decl_class = (is_declaration
20609 && cp_parser_declares_only_class_p (parser));
20610 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
20611 decl_class);
20613 /* Indicate whether this class was declared as a `class' or as a
20614 `struct'. */
20615 if (CLASS_TYPE_P (type) && !currently_open_class (type))
20616 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
20619 /* A "<" cannot follow an elaborated type specifier. If that
20620 happens, the user was probably trying to form a template-id. */
20621 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
20622 token->location);
20624 return type;
20627 /* Parse an enum-specifier.
20629 enum-specifier:
20630 enum-head { enumerator-list [opt] }
20631 enum-head { enumerator-list , } [C++0x]
20633 enum-head:
20634 enum-key identifier [opt] enum-base [opt]
20635 enum-key nested-name-specifier identifier enum-base [opt]
20637 enum-key:
20638 enum
20639 enum class [C++0x]
20640 enum struct [C++0x]
20642 enum-base: [C++0x]
20643 : type-specifier-seq
20645 opaque-enum-specifier:
20646 enum-key identifier enum-base [opt] ;
20648 GNU Extensions:
20649 enum-key attributes[opt] identifier [opt] enum-base [opt]
20650 { enumerator-list [opt] }attributes[opt]
20651 enum-key attributes[opt] identifier [opt] enum-base [opt]
20652 { enumerator-list, }attributes[opt] [C++0x]
20654 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20655 if the token stream isn't an enum-specifier after all. */
20657 static tree
20658 cp_parser_enum_specifier (cp_parser* parser)
20660 tree identifier;
20661 tree type = NULL_TREE;
20662 tree prev_scope;
20663 tree nested_name_specifier = NULL_TREE;
20664 tree attributes;
20665 bool scoped_enum_p = false;
20666 bool has_underlying_type = false;
20667 bool nested_being_defined = false;
20668 bool new_value_list = false;
20669 bool is_new_type = false;
20670 bool is_unnamed = false;
20671 tree underlying_type = NULL_TREE;
20672 cp_token *type_start_token = NULL;
20673 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
20675 /* Parse tentatively so that we can back up if we don't find a
20676 enum-specifier. */
20677 cp_parser_parse_tentatively (parser);
20679 /* Caller guarantees that the current token is 'enum', an identifier
20680 possibly follows, and the token after that is an opening brace.
20681 If we don't have an identifier, fabricate an anonymous name for
20682 the enumeration being defined. */
20683 cp_lexer_consume_token (parser->lexer);
20685 /* Parse the "class" or "struct", which indicates a scoped
20686 enumeration type in C++0x. */
20687 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
20688 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
20690 if (cxx_dialect < cxx11)
20691 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20693 /* Consume the `struct' or `class' token. */
20694 cp_lexer_consume_token (parser->lexer);
20696 scoped_enum_p = true;
20699 attributes = cp_parser_attributes_opt (parser);
20701 /* Clear the qualification. */
20702 parser->scope = NULL_TREE;
20703 parser->qualifying_scope = NULL_TREE;
20704 parser->object_scope = NULL_TREE;
20706 /* Figure out in what scope the declaration is being placed. */
20707 prev_scope = current_scope ();
20709 type_start_token = cp_lexer_peek_token (parser->lexer);
20711 push_deferring_access_checks (dk_no_check);
20712 nested_name_specifier
20713 = cp_parser_nested_name_specifier_opt (parser,
20714 /*typename_keyword_p=*/true,
20715 /*check_dependency_p=*/false,
20716 /*type_p=*/false,
20717 /*is_declaration=*/false);
20719 if (nested_name_specifier)
20721 tree name;
20723 identifier = cp_parser_identifier (parser);
20724 name = cp_parser_lookup_name (parser, identifier,
20725 enum_type,
20726 /*is_template=*/false,
20727 /*is_namespace=*/false,
20728 /*check_dependency=*/true,
20729 /*ambiguous_decls=*/NULL,
20730 input_location);
20731 if (name && name != error_mark_node)
20733 type = TREE_TYPE (name);
20734 if (TREE_CODE (type) == TYPENAME_TYPE)
20736 /* Are template enums allowed in ISO? */
20737 if (template_parm_scope_p ())
20738 pedwarn (type_start_token->location, OPT_Wpedantic,
20739 "%qD is an enumeration template", name);
20740 /* ignore a typename reference, for it will be solved by name
20741 in start_enum. */
20742 type = NULL_TREE;
20745 else if (nested_name_specifier == error_mark_node)
20746 /* We already issued an error. */;
20747 else
20749 error_at (type_start_token->location,
20750 "%qD does not name an enumeration in %qT",
20751 identifier, nested_name_specifier);
20752 nested_name_specifier = error_mark_node;
20755 else
20757 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20758 identifier = cp_parser_identifier (parser);
20759 else
20761 identifier = make_anon_name ();
20762 is_unnamed = true;
20763 if (scoped_enum_p)
20764 error_at (type_start_token->location,
20765 "unnamed scoped enum is not allowed");
20768 pop_deferring_access_checks ();
20770 /* Check for the `:' that denotes a specified underlying type in C++0x.
20771 Note that a ':' could also indicate a bitfield width, however. */
20772 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20774 cp_decl_specifier_seq type_specifiers;
20776 /* Consume the `:'. */
20777 cp_lexer_consume_token (parser->lexer);
20779 auto tdf
20780 = make_temp_override (parser->type_definition_forbidden_message,
20781 G_("types may not be defined in enum-base"));
20783 /* Parse the type-specifier-seq. */
20784 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
20785 /*is_declaration=*/false,
20786 /*is_trailing_return=*/false,
20787 &type_specifiers);
20789 /* At this point this is surely not elaborated type specifier. */
20790 if (!cp_parser_parse_definitely (parser))
20791 return NULL_TREE;
20793 if (cxx_dialect < cxx11)
20794 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20796 has_underlying_type = true;
20798 /* If that didn't work, stop. */
20799 if (type_specifiers.type != error_mark_node)
20801 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
20802 /*initialized=*/0, NULL);
20803 if (underlying_type == error_mark_node
20804 || check_for_bare_parameter_packs (underlying_type))
20805 underlying_type = NULL_TREE;
20809 /* Look for the `{' but don't consume it yet. */
20810 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20812 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
20814 if (has_underlying_type)
20815 cp_parser_commit_to_tentative_parse (parser);
20816 cp_parser_error (parser, "expected %<{%>");
20817 if (has_underlying_type)
20818 return error_mark_node;
20820 /* An opaque-enum-specifier must have a ';' here. */
20821 if ((scoped_enum_p || underlying_type)
20822 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20824 if (has_underlying_type)
20825 cp_parser_commit_to_tentative_parse (parser);
20826 cp_parser_error (parser, "expected %<;%> or %<{%>");
20827 if (has_underlying_type)
20828 return error_mark_node;
20832 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
20833 return NULL_TREE;
20835 if (nested_name_specifier)
20837 if (CLASS_TYPE_P (nested_name_specifier))
20839 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
20840 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
20841 push_scope (nested_name_specifier);
20843 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
20844 push_nested_namespace (nested_name_specifier);
20847 /* Issue an error message if type-definitions are forbidden here. */
20848 if (!cp_parser_check_type_definition (parser))
20849 type = error_mark_node;
20850 else
20851 /* Create the new type. We do this before consuming the opening
20852 brace so the enum will be recorded as being on the line of its
20853 tag (or the 'enum' keyword, if there is no tag). */
20854 type = start_enum (identifier, type, underlying_type,
20855 attributes, scoped_enum_p, &is_new_type);
20857 /* If the next token is not '{' it is an opaque-enum-specifier or an
20858 elaborated-type-specifier. */
20859 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20861 timevar_push (TV_PARSE_ENUM);
20862 if (nested_name_specifier
20863 && nested_name_specifier != error_mark_node)
20865 /* The following catches invalid code such as:
20866 enum class S<int>::E { A, B, C }; */
20867 if (!processing_specialization
20868 && CLASS_TYPE_P (nested_name_specifier)
20869 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
20870 error_at (type_start_token->location, "cannot add an enumerator "
20871 "list to a template instantiation");
20873 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
20875 error_at (type_start_token->location,
20876 "%<%T::%E%> has not been declared",
20877 TYPE_CONTEXT (nested_name_specifier),
20878 nested_name_specifier);
20879 type = error_mark_node;
20881 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
20882 && !CLASS_TYPE_P (nested_name_specifier))
20884 error_at (type_start_token->location, "nested name specifier "
20885 "%qT for enum declaration does not name a class "
20886 "or namespace", nested_name_specifier);
20887 type = error_mark_node;
20889 /* If that scope does not contain the scope in which the
20890 class was originally declared, the program is invalid. */
20891 else if (prev_scope && !is_ancestor (prev_scope,
20892 nested_name_specifier))
20894 if (at_namespace_scope_p ())
20895 error_at (type_start_token->location,
20896 "declaration of %qD in namespace %qD which does not "
20897 "enclose %qD",
20898 type, prev_scope, nested_name_specifier);
20899 else
20900 error_at (type_start_token->location,
20901 "declaration of %qD in %qD which does not "
20902 "enclose %qD",
20903 type, prev_scope, nested_name_specifier);
20904 type = error_mark_node;
20906 /* If that scope is the scope where the declaration is being placed
20907 the program is invalid. */
20908 else if (CLASS_TYPE_P (nested_name_specifier)
20909 && CLASS_TYPE_P (prev_scope)
20910 && same_type_p (nested_name_specifier, prev_scope))
20912 permerror (type_start_token->location,
20913 "extra qualification not allowed");
20914 nested_name_specifier = NULL_TREE;
20918 if (scoped_enum_p)
20919 begin_scope (sk_scoped_enum, type);
20921 /* Consume the opening brace. */
20922 matching_braces braces;
20923 braces.consume_open (parser);
20925 if (type == error_mark_node)
20926 ; /* Nothing to add */
20927 else if (OPAQUE_ENUM_P (type)
20928 || (cxx_dialect > cxx98 && processing_specialization))
20930 new_value_list = true;
20931 SET_OPAQUE_ENUM_P (type, false);
20932 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20934 else
20936 error_at (type_start_token->location,
20937 "multiple definition of %q#T", type);
20938 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
20939 "previous definition here");
20940 type = error_mark_node;
20943 if (type == error_mark_node)
20944 cp_parser_skip_to_end_of_block_or_statement (parser);
20945 /* If the next token is not '}', then there are some enumerators. */
20946 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20948 if (is_unnamed && !scoped_enum_p)
20949 pedwarn (type_start_token->location, OPT_Wpedantic,
20950 "ISO C++ forbids empty unnamed enum");
20952 else
20954 /* We've seen a '{' so we know we're in an enum-specifier.
20955 Commit to any tentative parse to get syntax errors. */
20956 cp_parser_commit_to_tentative_parse (parser);
20957 cp_parser_enumerator_list (parser, type);
20960 /* Consume the final '}'. */
20961 braces.require_close (parser);
20963 if (scoped_enum_p)
20964 finish_scope ();
20965 timevar_pop (TV_PARSE_ENUM);
20967 else
20969 /* If a ';' follows, then it is an opaque-enum-specifier
20970 and additional restrictions apply. */
20971 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20973 if (is_unnamed)
20974 error_at (type_start_token->location,
20975 "opaque-enum-specifier without name");
20976 else if (nested_name_specifier)
20977 error_at (type_start_token->location,
20978 "opaque-enum-specifier must use a simple identifier");
20982 /* Look for trailing attributes to apply to this enumeration, and
20983 apply them if appropriate. */
20984 if (cp_parser_allow_gnu_extensions_p (parser))
20986 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
20987 cplus_decl_attributes (&type,
20988 trailing_attr,
20989 (int) ATTR_FLAG_TYPE_IN_PLACE);
20992 /* Finish up the enumeration. */
20993 if (type != error_mark_node)
20995 if (new_value_list)
20996 finish_enum_value_list (type);
20997 if (is_new_type)
20998 finish_enum (type);
21001 if (nested_name_specifier)
21003 if (CLASS_TYPE_P (nested_name_specifier))
21005 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21006 pop_scope (nested_name_specifier);
21008 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21009 pop_nested_namespace (nested_name_specifier);
21011 return type;
21014 /* Parse an enumerator-list. The enumerators all have the indicated
21015 TYPE.
21017 enumerator-list:
21018 enumerator-definition
21019 enumerator-list , enumerator-definition */
21021 static void
21022 cp_parser_enumerator_list (cp_parser* parser, tree type)
21024 while (true)
21026 /* Parse an enumerator-definition. */
21027 cp_parser_enumerator_definition (parser, type);
21029 /* If the next token is not a ',', we've reached the end of
21030 the list. */
21031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21032 break;
21033 /* Otherwise, consume the `,' and keep going. */
21034 cp_lexer_consume_token (parser->lexer);
21035 /* If the next token is a `}', there is a trailing comma. */
21036 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21038 if (cxx_dialect < cxx11)
21039 pedwarn (input_location, OPT_Wpedantic,
21040 "comma at end of enumerator list");
21041 break;
21046 /* Parse an enumerator-definition. The enumerator has the indicated
21047 TYPE.
21049 enumerator-definition:
21050 enumerator
21051 enumerator = constant-expression
21053 enumerator:
21054 identifier
21056 GNU Extensions:
21058 enumerator-definition:
21059 enumerator attributes [opt]
21060 enumerator attributes [opt] = constant-expression */
21062 static void
21063 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21065 tree identifier;
21066 tree value;
21067 location_t loc;
21069 /* Save the input location because we are interested in the location
21070 of the identifier and not the location of the explicit value. */
21071 loc = cp_lexer_peek_token (parser->lexer)->location;
21073 /* Look for the identifier. */
21074 identifier = cp_parser_identifier (parser);
21075 if (identifier == error_mark_node)
21076 return;
21078 /* Parse any specified attributes. */
21079 tree attrs = cp_parser_attributes_opt (parser);
21081 /* If the next token is an '=', then there is an explicit value. */
21082 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21084 /* Consume the `=' token. */
21085 cp_lexer_consume_token (parser->lexer);
21086 /* Parse the value. */
21087 value = cp_parser_constant_expression (parser);
21089 else
21090 value = NULL_TREE;
21092 /* If we are processing a template, make sure the initializer of the
21093 enumerator doesn't contain any bare template parameter pack. */
21094 if (check_for_bare_parameter_packs (value))
21095 value = error_mark_node;
21097 /* Create the enumerator. */
21098 build_enumerator (identifier, value, type, attrs, loc);
21101 /* Parse a namespace-name.
21103 namespace-name:
21104 original-namespace-name
21105 namespace-alias
21107 Returns the NAMESPACE_DECL for the namespace. */
21109 static tree
21110 cp_parser_namespace_name (cp_parser* parser)
21112 tree identifier;
21113 tree namespace_decl;
21115 cp_token *token = cp_lexer_peek_token (parser->lexer);
21117 /* Get the name of the namespace. */
21118 identifier = cp_parser_identifier (parser);
21119 if (identifier == error_mark_node)
21120 return error_mark_node;
21122 /* Look up the identifier in the currently active scope. Look only
21123 for namespaces, due to:
21125 [basic.lookup.udir]
21127 When looking up a namespace-name in a using-directive or alias
21128 definition, only namespace names are considered.
21130 And:
21132 [basic.lookup.qual]
21134 During the lookup of a name preceding the :: scope resolution
21135 operator, object, function, and enumerator names are ignored.
21137 (Note that cp_parser_qualifying_entity only calls this
21138 function if the token after the name is the scope resolution
21139 operator.) */
21140 namespace_decl = cp_parser_lookup_name (parser, identifier,
21141 none_type,
21142 /*is_template=*/false,
21143 /*is_namespace=*/true,
21144 /*check_dependency=*/true,
21145 /*ambiguous_decls=*/NULL,
21146 token->location);
21147 /* If it's not a namespace, issue an error. */
21148 if (namespace_decl == error_mark_node
21149 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21151 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21153 auto_diagnostic_group d;
21154 name_hint hint;
21155 if (namespace_decl == error_mark_node
21156 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21157 hint = suggest_alternative_in_explicit_scope (token->location,
21158 identifier,
21159 parser->scope);
21160 if (const char *suggestion = hint.suggestion ())
21162 gcc_rich_location richloc (token->location);
21163 richloc.add_fixit_replace (suggestion);
21164 error_at (&richloc,
21165 "%qD is not a namespace-name; did you mean %qs?",
21166 identifier, suggestion);
21168 else
21169 error_at (token->location, "%qD is not a namespace-name",
21170 identifier);
21172 else
21173 cp_parser_error (parser, "expected namespace-name");
21174 namespace_decl = error_mark_node;
21177 return namespace_decl;
21180 /* Parse a namespace-definition.
21182 namespace-definition:
21183 named-namespace-definition
21184 unnamed-namespace-definition
21186 named-namespace-definition:
21187 original-namespace-definition
21188 extension-namespace-definition
21190 original-namespace-definition:
21191 namespace identifier { namespace-body }
21193 extension-namespace-definition:
21194 namespace original-namespace-name { namespace-body }
21196 unnamed-namespace-definition:
21197 namespace { namespace-body } */
21199 static void
21200 cp_parser_namespace_definition (cp_parser* parser)
21202 tree identifier;
21203 int nested_definition_count = 0;
21205 cp_ensure_no_omp_declare_simd (parser);
21206 cp_ensure_no_oacc_routine (parser);
21208 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21209 const bool topmost_inline_p = is_inline;
21211 if (is_inline)
21213 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21214 cp_lexer_consume_token (parser->lexer);
21217 /* Look for the `namespace' keyword. */
21218 cp_token* token
21219 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21221 /* Parse any specified attributes before the identifier. */
21222 tree attribs = cp_parser_attributes_opt (parser);
21224 for (;;)
21226 identifier = NULL_TREE;
21228 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21229 RID_INLINE);
21230 if (nested_inline_p && nested_definition_count != 0)
21232 if (pedantic && cxx_dialect < cxx20)
21233 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21234 OPT_Wc__20_extensions, "nested inline namespace "
21235 "definitions only available with %<-std=c++20%> or "
21236 "%<-std=gnu++20%>");
21237 cp_lexer_consume_token (parser->lexer);
21240 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21242 identifier = cp_parser_identifier (parser);
21244 if (cp_next_tokens_can_be_std_attribute_p (parser))
21245 pedwarn (input_location, OPT_Wpedantic,
21246 "standard attributes on namespaces must precede "
21247 "the namespace name");
21249 /* Parse any attributes specified after the identifier. */
21250 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21255 /* Don't forget that the innermost namespace might have been
21256 marked as inline. Use |= because we cannot overwrite
21257 IS_INLINE in case the outermost namespace is inline, but
21258 there are no nested inlines. */
21259 is_inline |= nested_inline_p;
21260 break;
21263 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21264 pedwarn (input_location, OPT_Wc__17_extensions,
21265 "nested namespace definitions only available with "
21266 "%<-std=c++17%> or %<-std=gnu++17%>");
21268 /* Nested namespace names can create new namespaces (unlike
21269 other qualified-ids). */
21270 if (int count = (identifier
21271 ? push_namespace (identifier, nested_inline_p)
21272 : 0))
21273 nested_definition_count += count;
21274 else
21275 cp_parser_error (parser, "nested namespace name required");
21276 cp_lexer_consume_token (parser->lexer);
21279 if (nested_definition_count && !identifier)
21280 cp_parser_error (parser, "namespace name required");
21282 if (nested_definition_count && attribs)
21283 error_at (token->location,
21284 "a nested namespace definition cannot have attributes");
21285 if (nested_definition_count && topmost_inline_p)
21286 error_at (token->location,
21287 "a nested namespace definition cannot be inline");
21289 /* Start the namespace. */
21290 nested_definition_count += push_namespace (identifier, is_inline);
21292 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21294 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21296 /* Look for the `{' to validate starting the namespace. */
21297 matching_braces braces;
21298 if (braces.require_open (parser))
21300 /* Parse the body of the namespace. */
21301 cp_parser_namespace_body (parser);
21303 /* Look for the final `}'. */
21304 braces.require_close (parser);
21307 if (has_visibility)
21308 pop_visibility (1);
21310 /* Pop the nested namespace definitions. */
21311 while (nested_definition_count--)
21312 pop_namespace ();
21315 /* Parse a namespace-body.
21317 namespace-body:
21318 declaration-seq [opt] */
21320 static void
21321 cp_parser_namespace_body (cp_parser* parser)
21323 cp_parser_declaration_seq_opt (parser);
21326 /* Parse a namespace-alias-definition.
21328 namespace-alias-definition:
21329 namespace identifier = qualified-namespace-specifier ; */
21331 static void
21332 cp_parser_namespace_alias_definition (cp_parser* parser)
21334 tree identifier;
21335 tree namespace_specifier;
21337 cp_token *token = cp_lexer_peek_token (parser->lexer);
21339 /* Look for the `namespace' keyword. */
21340 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21341 /* Look for the identifier. */
21342 identifier = cp_parser_identifier (parser);
21343 if (identifier == error_mark_node)
21344 return;
21345 /* Look for the `=' token. */
21346 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21347 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21349 error_at (token->location, "%<namespace%> definition is not allowed here");
21350 /* Skip the definition. */
21351 cp_lexer_consume_token (parser->lexer);
21352 if (cp_parser_skip_to_closing_brace (parser))
21353 cp_lexer_consume_token (parser->lexer);
21354 return;
21356 cp_parser_require (parser, CPP_EQ, RT_EQ);
21357 /* Look for the qualified-namespace-specifier. */
21358 namespace_specifier
21359 = cp_parser_qualified_namespace_specifier (parser);
21360 cp_warn_deprecated_use_scopes (namespace_specifier);
21361 /* Look for the `;' token. */
21362 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21364 /* Register the alias in the symbol table. */
21365 do_namespace_alias (identifier, namespace_specifier);
21368 /* Parse a qualified-namespace-specifier.
21370 qualified-namespace-specifier:
21371 :: [opt] nested-name-specifier [opt] namespace-name
21373 Returns a NAMESPACE_DECL corresponding to the specified
21374 namespace. */
21376 static tree
21377 cp_parser_qualified_namespace_specifier (cp_parser* parser)
21379 /* Look for the optional `::'. */
21380 cp_parser_global_scope_opt (parser,
21381 /*current_scope_valid_p=*/false);
21383 /* Look for the optional nested-name-specifier. */
21384 cp_parser_nested_name_specifier_opt (parser,
21385 /*typename_keyword_p=*/false,
21386 /*check_dependency_p=*/true,
21387 /*type_p=*/false,
21388 /*is_declaration=*/true);
21390 return cp_parser_namespace_name (parser);
21393 /* Subroutine of cp_parser_using_declaration. */
21395 static tree
21396 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21398 tree decl = NULL_TREE;
21399 if (at_class_scope_p ())
21401 /* Create the USING_DECL. */
21402 decl = do_class_using_decl (qscope, identifier);
21404 if (check_for_bare_parameter_packs (decl))
21405 return error_mark_node;
21407 if (decl && typename_p)
21408 USING_DECL_TYPENAME_P (decl) = 1;
21410 /* Add it to the list of members in this class. */
21411 finish_member_declaration (decl);
21413 else
21414 finish_nonmember_using_decl (qscope, identifier);
21415 return decl;
21418 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21419 access declaration.
21421 using-declaration:
21422 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21423 using :: unqualified-id ;
21425 access-declaration:
21426 qualified-id ;
21430 static bool
21431 cp_parser_using_declaration (cp_parser* parser,
21432 bool access_declaration_p)
21434 cp_token *token;
21435 bool typename_p = false;
21436 bool global_scope_p;
21437 tree identifier;
21438 tree qscope;
21439 int oldcount = errorcount;
21440 cp_token *diag_token = NULL;
21442 if (access_declaration_p)
21444 diag_token = cp_lexer_peek_token (parser->lexer);
21445 cp_parser_parse_tentatively (parser);
21447 else
21449 /* Look for the `using' keyword. */
21450 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21452 again:
21453 /* Peek at the next token. */
21454 token = cp_lexer_peek_token (parser->lexer);
21455 /* See if it's `typename'. */
21456 if (token->keyword == RID_TYPENAME)
21458 /* Remember that we've seen it. */
21459 typename_p = true;
21460 /* Consume the `typename' token. */
21461 cp_lexer_consume_token (parser->lexer);
21465 /* Look for the optional global scope qualification. */
21466 global_scope_p
21467 = (cp_parser_global_scope_opt (parser,
21468 /*current_scope_valid_p=*/false)
21469 != NULL_TREE);
21471 /* If we saw `typename', or didn't see `::', then there must be a
21472 nested-name-specifier present. */
21473 if (typename_p || !global_scope_p)
21475 qscope = cp_parser_nested_name_specifier (parser, typename_p,
21476 /*check_dependency_p=*/true,
21477 /*type_p=*/false,
21478 /*is_declaration=*/true);
21479 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21481 cp_parser_skip_to_end_of_block_or_statement (parser);
21482 return false;
21485 /* Otherwise, we could be in either of the two productions. In that
21486 case, treat the nested-name-specifier as optional. */
21487 else
21488 qscope = cp_parser_nested_name_specifier_opt (parser,
21489 /*typename_keyword_p=*/false,
21490 /*check_dependency_p=*/true,
21491 /*type_p=*/false,
21492 /*is_declaration=*/true);
21493 if (!qscope)
21494 qscope = global_namespace;
21496 cp_warn_deprecated_use_scopes (qscope);
21498 if (access_declaration_p && cp_parser_error_occurred (parser))
21499 /* Something has already gone wrong; there's no need to parse
21500 further. Since an error has occurred, the return value of
21501 cp_parser_parse_definitely will be false, as required. */
21502 return cp_parser_parse_definitely (parser);
21504 token = cp_lexer_peek_token (parser->lexer);
21505 /* Parse the unqualified-id. */
21506 identifier = cp_parser_unqualified_id (parser,
21507 /*template_keyword_p=*/false,
21508 /*check_dependency_p=*/true,
21509 /*declarator_p=*/true,
21510 /*optional_p=*/false);
21512 if (access_declaration_p)
21514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21515 cp_parser_simulate_error (parser);
21516 if (!cp_parser_parse_definitely (parser))
21517 return false;
21519 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21521 cp_token *ell = cp_lexer_consume_token (parser->lexer);
21522 if (cxx_dialect < cxx17)
21523 pedwarn (ell->location, OPT_Wc__17_extensions,
21524 "pack expansion in using-declaration only available "
21525 "with %<-std=c++17%> or %<-std=gnu++17%>");
21526 qscope = make_pack_expansion (qscope);
21529 /* The function we call to handle a using-declaration is different
21530 depending on what scope we are in. */
21531 if (qscope == error_mark_node || identifier == error_mark_node)
21533 else if (!identifier_p (identifier)
21534 && TREE_CODE (identifier) != BIT_NOT_EXPR)
21535 /* [namespace.udecl]
21537 A using declaration shall not name a template-id. */
21538 error_at (token->location,
21539 "a template-id may not appear in a using-declaration");
21540 else
21542 tree decl = finish_using_decl (qscope, identifier, typename_p);
21544 if (decl == error_mark_node)
21546 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21547 return false;
21551 if (!access_declaration_p
21552 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21554 cp_token *comma = cp_lexer_consume_token (parser->lexer);
21555 if (cxx_dialect < cxx17)
21556 pedwarn (comma->location, OPT_Wc__17_extensions,
21557 "comma-separated list in using-declaration only available "
21558 "with %<-std=c++17%> or %<-std=gnu++17%>");
21559 goto again;
21562 /* Look for the final `;'. */
21563 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21565 if (access_declaration_p && errorcount == oldcount)
21566 warning_at (diag_token->location, OPT_Wdeprecated,
21567 "access declarations are deprecated "
21568 "in favour of using-declarations; "
21569 "suggestion: add the %<using%> keyword");
21571 return true;
21574 /* C++20 using enum declaration.
21576 using-enum-declaration :
21577 using elaborated-enum-specifier ; */
21579 static void
21580 cp_parser_using_enum (cp_parser *parser)
21582 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21584 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21585 breaks one of the motivating examples in using-enum-5.C.
21586 cp_parser_simple_type_specifier seems to be closer to what we actually
21587 want, though that hasn't been properly specified yet. */
21589 /* Consume 'enum'. */
21590 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
21591 cp_lexer_consume_token (parser->lexer);
21593 cp_token *start = cp_lexer_peek_token (parser->lexer);
21595 tree type = (cp_parser_simple_type_specifier
21596 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
21598 cp_token *end = cp_lexer_previous_token (parser->lexer);
21600 if (type == error_mark_node
21601 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21603 cp_parser_skip_to_end_of_block_or_statement (parser);
21604 return;
21606 if (TREE_CODE (type) == TYPE_DECL)
21607 type = TREE_TYPE (type);
21609 /* The elaborated-enum-specifier shall not name a dependent type and the type
21610 shall have a reachable enum-specifier. */
21611 const char *msg = nullptr;
21612 if (cxx_dialect < cxx20)
21613 msg = _("%<using enum%> "
21614 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21615 else if (dependent_type_p (type))
21616 msg = _("%<using enum%> of dependent type %qT");
21617 else if (TREE_CODE (type) != ENUMERAL_TYPE)
21618 msg = _("%<using enum%> of non-enumeration type %q#T");
21619 else if (!COMPLETE_TYPE_P (type))
21620 msg = _("%<using enum%> of incomplete type %qT");
21621 else if (OPAQUE_ENUM_P (type))
21622 msg = _("%<using enum%> of %qT before its enum-specifier");
21623 if (msg)
21625 location_t loc = make_location (start, start, end);
21626 auto_diagnostic_group g;
21627 error_at (loc, msg, type);
21628 loc = location_of (type);
21629 if (cxx_dialect < cxx20 || loc == input_location)
21631 else if (OPAQUE_ENUM_P (type))
21632 inform (loc, "opaque-enum-declaration here");
21633 else
21634 inform (loc, "declared here");
21637 /* A using-enum-declaration introduces the enumerator names of the named
21638 enumeration as if by a using-declaration for each enumerator. */
21639 if (TREE_CODE (type) == ENUMERAL_TYPE)
21640 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
21641 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
21644 /* Parse an alias-declaration.
21646 alias-declaration:
21647 using identifier attribute-specifier-seq [opt] = type-id */
21649 static tree
21650 cp_parser_alias_declaration (cp_parser* parser)
21652 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
21653 location_t id_location, type_location;
21654 cp_declarator *declarator;
21655 cp_decl_specifier_seq decl_specs;
21656 bool member_p;
21657 const char *saved_message = NULL;
21659 /* Look for the `using' keyword. */
21660 cp_token *using_token
21661 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
21662 if (using_token == NULL)
21663 return error_mark_node;
21665 id_location = cp_lexer_peek_token (parser->lexer)->location;
21666 id = cp_parser_identifier (parser);
21667 if (id == error_mark_node)
21668 return error_mark_node;
21670 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
21671 attributes = cp_parser_attributes_opt (parser);
21672 if (attributes == error_mark_node)
21673 return error_mark_node;
21675 cp_parser_require (parser, CPP_EQ, RT_EQ);
21677 if (cp_parser_error_occurred (parser))
21678 return error_mark_node;
21680 cp_parser_commit_to_tentative_parse (parser);
21682 /* Now we are going to parse the type-id of the declaration. */
21685 [dcl.type]/3 says:
21687 "A type-specifier-seq shall not define a class or enumeration
21688 unless it appears in the type-id of an alias-declaration (7.1.3) that
21689 is not the declaration of a template-declaration."
21691 In other words, if we currently are in an alias template, the
21692 type-id should not define a type.
21694 So let's set parser->type_definition_forbidden_message in that
21695 case; cp_parser_check_type_definition (called by
21696 cp_parser_class_specifier) will then emit an error if a type is
21697 defined in the type-id. */
21698 if (parser->num_template_parameter_lists)
21700 saved_message = parser->type_definition_forbidden_message;
21701 parser->type_definition_forbidden_message =
21702 G_("types may not be defined in alias template declarations");
21705 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21706 &type_location);
21708 /* Restore the error message if need be. */
21709 if (parser->num_template_parameter_lists)
21710 parser->type_definition_forbidden_message = saved_message;
21712 if (type == error_mark_node
21713 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21715 cp_parser_skip_to_end_of_block_or_statement (parser);
21716 return error_mark_node;
21719 /* A typedef-name can also be introduced by an alias-declaration. The
21720 identifier following the using keyword becomes a typedef-name. It has
21721 the same semantics as if it were introduced by the typedef
21722 specifier. In particular, it does not define a new type and it shall
21723 not appear in the type-id. */
21725 clear_decl_specs (&decl_specs);
21726 decl_specs.type = type;
21727 if (attributes != NULL_TREE)
21729 decl_specs.attributes = attributes;
21730 set_and_check_decl_spec_loc (&decl_specs,
21731 ds_attribute,
21732 attrs_token);
21734 set_and_check_decl_spec_loc (&decl_specs,
21735 ds_typedef,
21736 using_token);
21737 set_and_check_decl_spec_loc (&decl_specs,
21738 ds_alias,
21739 using_token);
21740 decl_specs.locations[ds_type_spec] = type_location;
21742 if (parser->num_template_parameter_lists
21743 && !cp_parser_check_template_parameters (parser,
21744 /*num_templates=*/0,
21745 /*template_id*/false,
21746 id_location,
21747 /*declarator=*/NULL))
21748 return error_mark_node;
21750 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
21752 member_p = at_class_scope_p ();
21753 if (member_p)
21754 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
21755 NULL_TREE, attributes);
21756 else
21757 decl = start_decl (declarator, &decl_specs, 0,
21758 attributes, NULL_TREE, &pushed_scope);
21759 if (decl == error_mark_node)
21760 return decl;
21762 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
21764 if (pushed_scope)
21765 pop_scope (pushed_scope);
21767 /* If decl is a template, return its TEMPLATE_DECL so that it gets
21768 added into the symbol table; otherwise, return the TYPE_DECL. */
21769 if (DECL_LANG_SPECIFIC (decl)
21770 && DECL_TEMPLATE_INFO (decl)
21771 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
21773 decl = DECL_TI_TEMPLATE (decl);
21774 if (member_p)
21775 check_member_template (decl);
21778 return decl;
21781 /* Parse a using-directive.
21783 using-directive:
21784 attribute-specifier-seq [opt] using namespace :: [opt]
21785 nested-name-specifier [opt] namespace-name ; */
21787 static void
21788 cp_parser_using_directive (cp_parser* parser)
21790 tree namespace_decl;
21791 tree attribs = cp_parser_std_attribute_spec_seq (parser);
21792 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21794 /* Error during attribute parsing that resulted in skipping
21795 to next semicolon. */
21796 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21797 return;
21800 /* Look for the `using' keyword. */
21801 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21802 /* And the `namespace' keyword. */
21803 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21804 /* Look for the optional `::' operator. */
21805 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21806 /* And the optional nested-name-specifier. */
21807 cp_parser_nested_name_specifier_opt (parser,
21808 /*typename_keyword_p=*/false,
21809 /*check_dependency_p=*/true,
21810 /*type_p=*/false,
21811 /*is_declaration=*/true);
21812 /* Get the namespace being used. */
21813 namespace_decl = cp_parser_namespace_name (parser);
21814 cp_warn_deprecated_use_scopes (namespace_decl);
21815 /* And any specified GNU attributes. */
21816 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21817 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
21819 /* Update the symbol table. */
21820 finish_using_directive (namespace_decl, attribs);
21822 /* Look for the final `;'. */
21823 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21826 /* Parse an asm-definition.
21828 asm-qualifier:
21829 volatile
21830 inline
21831 goto
21833 asm-qualifier-list:
21834 asm-qualifier
21835 asm-qualifier-list asm-qualifier
21837 asm-definition:
21838 asm ( string-literal ) ;
21840 GNU Extension:
21842 asm-definition:
21843 asm asm-qualifier-list [opt] ( string-literal ) ;
21844 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
21845 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21846 : asm-operand-list [opt] ) ;
21847 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21848 : asm-operand-list [opt]
21849 : asm-clobber-list [opt] ) ;
21850 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
21851 : asm-clobber-list [opt]
21852 : asm-goto-list ) ;
21854 The form with asm-goto-list is valid if and only if the asm-qualifier-list
21855 contains goto, and is the only allowed form in that case. No duplicates are
21856 allowed in an asm-qualifier-list. */
21858 static void
21859 cp_parser_asm_definition (cp_parser* parser)
21861 tree string;
21862 tree outputs = NULL_TREE;
21863 tree inputs = NULL_TREE;
21864 tree clobbers = NULL_TREE;
21865 tree labels = NULL_TREE;
21866 tree asm_stmt;
21867 bool extended_p = false;
21868 bool invalid_inputs_p = false;
21869 bool invalid_outputs_p = false;
21870 required_token missing = RT_NONE;
21871 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
21873 /* Look for the `asm' keyword. */
21874 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
21876 /* In C++20, unevaluated inline assembly is permitted in constexpr
21877 functions. */
21878 if (parser->in_function_body
21879 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
21880 && cxx_dialect < cxx20)
21881 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
21882 "function only available with %<-std=c++20%> or "
21883 "%<-std=gnu++20%>");
21885 /* Handle the asm-qualifier-list. */
21886 location_t volatile_loc = UNKNOWN_LOCATION;
21887 location_t inline_loc = UNKNOWN_LOCATION;
21888 location_t goto_loc = UNKNOWN_LOCATION;
21889 location_t first_loc = UNKNOWN_LOCATION;
21891 if (cp_parser_allow_gnu_extensions_p (parser))
21892 for (;;)
21894 cp_token *token = cp_lexer_peek_token (parser->lexer);
21895 location_t loc = token->location;
21896 switch (cp_lexer_peek_token (parser->lexer)->keyword)
21898 case RID_VOLATILE:
21899 if (volatile_loc)
21901 error_at (loc, "duplicate %<asm%> qualifier %qT",
21902 token->u.value);
21903 inform (volatile_loc, "first seen here");
21905 else
21907 if (!parser->in_function_body)
21908 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
21909 "outside of function body", token->u.value);
21910 volatile_loc = loc;
21912 cp_lexer_consume_token (parser->lexer);
21913 continue;
21915 case RID_INLINE:
21916 if (inline_loc)
21918 error_at (loc, "duplicate %<asm%> qualifier %qT",
21919 token->u.value);
21920 inform (inline_loc, "first seen here");
21922 else
21923 inline_loc = loc;
21924 if (!first_loc)
21925 first_loc = loc;
21926 cp_lexer_consume_token (parser->lexer);
21927 continue;
21929 case RID_GOTO:
21930 if (goto_loc)
21932 error_at (loc, "duplicate %<asm%> qualifier %qT",
21933 token->u.value);
21934 inform (goto_loc, "first seen here");
21936 else
21937 goto_loc = loc;
21938 if (!first_loc)
21939 first_loc = loc;
21940 cp_lexer_consume_token (parser->lexer);
21941 continue;
21943 case RID_CONST:
21944 case RID_RESTRICT:
21945 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
21946 cp_lexer_consume_token (parser->lexer);
21947 continue;
21949 default:
21950 break;
21952 break;
21955 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
21956 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
21957 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
21959 if (!parser->in_function_body && (inline_p || goto_p))
21961 error_at (first_loc, "%<asm%> qualifier outside of function body");
21962 inline_p = goto_p = false;
21965 /* Look for the opening `('. */
21966 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21967 return;
21968 /* Look for the string. */
21969 string = cp_parser_string_literal (parser, false, false);
21970 if (string == error_mark_node)
21972 cp_parser_skip_to_closing_parenthesis (parser, true, false,
21973 /*consume_paren=*/true);
21974 return;
21977 /* If we're allowing GNU extensions, check for the extended assembly
21978 syntax. Unfortunately, the `:' tokens need not be separated by
21979 a space in C, and so, for compatibility, we tolerate that here
21980 too. Doing that means that we have to treat the `::' operator as
21981 two `:' tokens. */
21982 if (cp_parser_allow_gnu_extensions_p (parser)
21983 && parser->in_function_body
21984 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
21985 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
21987 bool inputs_p = false;
21988 bool clobbers_p = false;
21989 bool labels_p = false;
21991 /* The extended syntax was used. */
21992 extended_p = true;
21994 /* Look for outputs. */
21995 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21997 /* Consume the `:'. */
21998 cp_lexer_consume_token (parser->lexer);
21999 /* Parse the output-operands. */
22000 if (cp_lexer_next_token_is_not (parser->lexer,
22001 CPP_COLON)
22002 && cp_lexer_next_token_is_not (parser->lexer,
22003 CPP_SCOPE)
22004 && cp_lexer_next_token_is_not (parser->lexer,
22005 CPP_CLOSE_PAREN))
22007 outputs = cp_parser_asm_operand_list (parser);
22008 if (outputs == error_mark_node)
22009 invalid_outputs_p = true;
22012 /* If the next token is `::', there are no outputs, and the
22013 next token is the beginning of the inputs. */
22014 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22015 /* The inputs are coming next. */
22016 inputs_p = true;
22018 /* Look for inputs. */
22019 if (inputs_p
22020 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22022 /* Consume the `:' or `::'. */
22023 cp_lexer_consume_token (parser->lexer);
22024 /* Parse the output-operands. */
22025 if (cp_lexer_next_token_is_not (parser->lexer,
22026 CPP_COLON)
22027 && cp_lexer_next_token_is_not (parser->lexer,
22028 CPP_SCOPE)
22029 && cp_lexer_next_token_is_not (parser->lexer,
22030 CPP_CLOSE_PAREN))
22032 inputs = cp_parser_asm_operand_list (parser);
22033 if (inputs == error_mark_node)
22034 invalid_inputs_p = true;
22037 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22038 /* The clobbers are coming next. */
22039 clobbers_p = true;
22041 /* Look for clobbers. */
22042 if (clobbers_p
22043 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22045 clobbers_p = true;
22046 /* Consume the `:' or `::'. */
22047 cp_lexer_consume_token (parser->lexer);
22048 /* Parse the clobbers. */
22049 if (cp_lexer_next_token_is_not (parser->lexer,
22050 CPP_COLON)
22051 && cp_lexer_next_token_is_not (parser->lexer,
22052 CPP_CLOSE_PAREN))
22053 clobbers = cp_parser_asm_clobber_list (parser);
22055 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22056 /* The labels are coming next. */
22057 labels_p = true;
22059 /* Look for labels. */
22060 if (labels_p
22061 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22063 labels_p = true;
22064 /* Consume the `:' or `::'. */
22065 cp_lexer_consume_token (parser->lexer);
22066 /* Parse the labels. */
22067 labels = cp_parser_asm_label_list (parser);
22070 if (goto_p && !labels_p)
22071 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22073 else if (goto_p)
22074 missing = RT_COLON_SCOPE;
22076 /* Look for the closing `)'. */
22077 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22078 missing ? missing : RT_CLOSE_PAREN))
22079 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22080 /*consume_paren=*/true);
22081 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22083 if (!invalid_inputs_p && !invalid_outputs_p)
22085 /* Create the ASM_EXPR. */
22086 if (parser->in_function_body)
22088 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22089 inputs, clobbers, labels, inline_p);
22090 /* If the extended syntax was not used, mark the ASM_EXPR. */
22091 if (!extended_p)
22093 tree temp = asm_stmt;
22094 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22095 temp = TREE_OPERAND (temp, 0);
22097 ASM_INPUT_P (temp) = 1;
22100 else
22101 symtab->finalize_toplevel_asm (string);
22105 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22106 type that comes from the decl-specifier-seq. */
22108 static tree
22109 strip_declarator_types (tree type, cp_declarator *declarator)
22111 for (cp_declarator *d = declarator; d;)
22112 switch (d->kind)
22114 case cdk_id:
22115 case cdk_decomp:
22116 case cdk_error:
22117 d = NULL;
22118 break;
22120 default:
22121 if (TYPE_PTRMEMFUNC_P (type))
22122 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22123 type = TREE_TYPE (type);
22124 d = d->declarator;
22125 break;
22128 return type;
22131 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22132 a construct looks like a variable definition but is actually a function
22133 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22134 is the declarator for this function declaration. */
22136 static void
22137 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22138 const cp_declarator *declarator)
22140 /* Only warn if we are declaring a function at block scope. */
22141 if (!at_function_scope_p ())
22142 return;
22144 /* And only if there is no storage class specified. */
22145 if (decl_specifiers->storage_class != sc_none
22146 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22147 return;
22149 if (declarator->kind != cdk_function
22150 || !declarator->declarator
22151 || declarator->declarator->kind != cdk_id
22152 || !identifier_p (get_unqualified_id
22153 (const_cast<cp_declarator *>(declarator))))
22154 return;
22156 /* Don't warn when the whole declarator (not just the declarator-id!)
22157 was parenthesized. That is, don't warn for int(n()) but do warn
22158 for int(f)(). */
22159 if (declarator->parenthesized != UNKNOWN_LOCATION)
22160 return;
22162 tree type;
22163 if (decl_specifiers->type)
22165 type = decl_specifiers->type;
22166 if (TREE_CODE (type) == TYPE_DECL)
22167 type = TREE_TYPE (type);
22169 /* If the return type is void there is no ambiguity. */
22170 if (same_type_p (type, void_type_node))
22171 return;
22173 else if (decl_specifiers->any_type_specifiers_p)
22174 /* Code like long f(); will have null ->type. If we have any
22175 type-specifiers, pretend we've seen int. */
22176 type = integer_type_node;
22177 else
22178 return;
22180 auto_diagnostic_group d;
22181 location_t loc = declarator->u.function.parens_loc;
22182 tree params = declarator->u.function.parameters;
22183 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22185 /* The T t() case. */
22186 if (params == void_list_node)
22188 if (warning_at (loc, OPT_Wvexing_parse,
22189 "empty parentheses were disambiguated as a function "
22190 "declaration"))
22192 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22193 means value-initialization or aggregate-initialization, nothing
22194 means default-initialization. We can only suggest removing the
22195 parentheses/adding {} if T has a default constructor. */
22196 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22198 gcc_rich_location iloc (loc);
22199 iloc.add_fixit_remove ();
22200 inform (&iloc, "remove parentheses to default-initialize "
22201 "a variable");
22202 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22204 if (CP_AGGREGATE_TYPE_P (type))
22205 inform (loc, "or replace parentheses with braces to "
22206 "aggregate-initialize a variable");
22207 else
22208 inform (loc, "or replace parentheses with braces to "
22209 "value-initialize a variable");
22213 return;
22216 /* If we had (...) or the parameter-list wasn't parenthesized,
22217 we're done. */
22218 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22219 return;
22221 /* The T t(X()) case. */
22222 if (list_length (params) == 2)
22224 if (warning_at (loc, OPT_Wvexing_parse,
22225 "parentheses were disambiguated as a function "
22226 "declaration"))
22228 gcc_rich_location iloc (loc);
22229 /* {}-initialization means that we can use an initializer-list
22230 constructor if no default constructor is available, so don't
22231 suggest using {} for classes that have an initializer_list
22232 constructor. */
22233 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22235 iloc.add_fixit_replace (get_start (loc), "{");
22236 iloc.add_fixit_replace (get_finish (loc), "}");
22237 inform (&iloc, "replace parentheses with braces to declare a "
22238 "variable");
22240 else
22242 iloc.add_fixit_insert_after (get_start (loc), "(");
22243 iloc.add_fixit_insert_before (get_finish (loc), ")");
22244 inform (&iloc, "add parentheses to declare a variable");
22248 /* The T t(X(), X()) case. */
22249 else if (warning_at (loc, OPT_Wvexing_parse,
22250 "parentheses were disambiguated as a function "
22251 "declaration"))
22253 gcc_rich_location iloc (loc);
22254 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22256 iloc.add_fixit_replace (get_start (loc), "{");
22257 iloc.add_fixit_replace (get_finish (loc), "}");
22258 inform (&iloc, "replace parentheses with braces to declare a "
22259 "variable");
22264 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22265 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22266 has the same meaning as in cp_parser_declarator. */
22268 static void
22269 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22270 cp_decl_specifier_seq *decl_specs,
22271 cp_declarator *declarator,
22272 int *ctor_dtor_or_conv_p)
22274 if (cxx_dialect >= cxx17
22275 && *ctor_dtor_or_conv_p <= 0
22276 && !decl_specs->type
22277 && !decl_specs->any_type_specifiers_p
22278 && function_declarator_p (declarator))
22280 cp_declarator *id = get_id_declarator (declarator);
22281 tree name = id->u.id.unqualified_name;
22282 parser->scope = id->u.id.qualifying_scope;
22283 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22284 if (tmpl
22285 && (DECL_CLASS_TEMPLATE_P (tmpl)
22286 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22288 id->u.id.unqualified_name = dguide_name (tmpl);
22289 id->u.id.sfk = sfk_deduction_guide;
22290 *ctor_dtor_or_conv_p = 1;
22295 /* Declarators [gram.dcl.decl] */
22297 /* Parse an init-declarator.
22299 init-declarator:
22300 declarator initializer [opt]
22302 GNU Extension:
22304 init-declarator:
22305 declarator asm-specification [opt] attributes [opt] initializer [opt]
22307 function-definition:
22308 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22309 function-body
22310 decl-specifier-seq [opt] declarator function-try-block
22312 GNU Extension:
22314 function-definition:
22315 __extension__ function-definition
22317 TM Extension:
22319 function-definition:
22320 decl-specifier-seq [opt] declarator function-transaction-block
22322 The parser flags FLAGS is used to control type-specifier parsing.
22324 The DECL_SPECIFIERS apply to this declarator. Returns a
22325 representation of the entity declared. If MEMBER_P is TRUE, then
22326 this declarator appears in a class scope. The new DECL created by
22327 this declarator is returned.
22329 The CHECKS are access checks that should be performed once we know
22330 what entity is being declared (and, therefore, what classes have
22331 befriended it).
22333 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22334 for a function-definition here as well. If the declarator is a
22335 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22336 be TRUE upon return. By that point, the function-definition will
22337 have been completely parsed.
22339 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22340 is FALSE.
22342 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22343 parsed declaration if it is an uninitialized single declarator not followed
22344 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22345 if present, will not be consumed. If returned, this declarator will be
22346 created with SD_INITIALIZED but will not call cp_finish_decl.
22348 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22349 and there is an initializer, the pointed location_t is set to the
22350 location of the '=' or `(', or '{' in C++11 token introducing the
22351 initializer. */
22353 static tree
22354 cp_parser_init_declarator (cp_parser* parser,
22355 cp_parser_flags flags,
22356 cp_decl_specifier_seq *decl_specifiers,
22357 vec<deferred_access_check, va_gc> *checks,
22358 bool function_definition_allowed_p,
22359 bool member_p,
22360 int declares_class_or_enum,
22361 bool* function_definition_p,
22362 tree* maybe_range_for_decl,
22363 location_t* init_loc,
22364 tree* auto_result)
22366 cp_token *token = NULL, *asm_spec_start_token = NULL,
22367 *attributes_start_token = NULL;
22368 cp_declarator *declarator;
22369 tree prefix_attributes;
22370 tree attributes = NULL;
22371 tree asm_specification;
22372 tree initializer;
22373 tree decl = NULL_TREE;
22374 tree scope;
22375 int is_initialized;
22376 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22377 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22378 "(...)". */
22379 enum cpp_ttype initialization_kind;
22380 bool is_direct_init = false;
22381 bool is_non_constant_init;
22382 int ctor_dtor_or_conv_p;
22383 bool friend_p = cp_parser_friend_p (decl_specifiers);
22384 bool static_p = decl_specifiers->storage_class == sc_static;
22385 tree pushed_scope = NULL_TREE;
22386 bool range_for_decl_p = false;
22387 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22388 location_t tmp_init_loc = UNKNOWN_LOCATION;
22390 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22391 flags |= CP_PARSER_FLAGS_CONSTEVAL;
22393 /* Assume that this is not the declarator for a function
22394 definition. */
22395 if (function_definition_p)
22396 *function_definition_p = false;
22398 /* Default arguments are only permitted for function parameters. */
22399 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22400 parser->default_arg_ok_p = false;
22402 /* Defer access checks while parsing the declarator; we cannot know
22403 what names are accessible until we know what is being
22404 declared. */
22405 resume_deferring_access_checks ();
22407 token = cp_lexer_peek_token (parser->lexer);
22409 /* Parse the declarator. */
22410 declarator
22411 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22412 flags, &ctor_dtor_or_conv_p,
22413 /*parenthesized_p=*/NULL,
22414 member_p, friend_p, static_p);
22415 /* Gather up the deferred checks. */
22416 stop_deferring_access_checks ();
22418 parser->default_arg_ok_p = saved_default_arg_ok_p;
22420 /* If the DECLARATOR was erroneous, there's no need to go
22421 further. */
22422 if (declarator == cp_error_declarator)
22423 return error_mark_node;
22425 /* Check that the number of template-parameter-lists is OK. */
22426 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22427 token->location))
22428 return error_mark_node;
22430 if (declares_class_or_enum & 2)
22431 cp_parser_check_for_definition_in_return_type (declarator,
22432 decl_specifiers->type,
22433 decl_specifiers->locations[ds_type_spec]);
22435 /* Figure out what scope the entity declared by the DECLARATOR is
22436 located in. `grokdeclarator' sometimes changes the scope, so
22437 we compute it now. */
22438 scope = get_scope_of_declarator (declarator);
22440 /* Perform any lookups in the declared type which were thought to be
22441 dependent, but are not in the scope of the declarator. */
22442 decl_specifiers->type
22443 = maybe_update_decl_type (decl_specifiers->type, scope);
22445 /* If we're allowing GNU extensions, look for an
22446 asm-specification. */
22447 if (cp_parser_allow_gnu_extensions_p (parser))
22449 /* Look for an asm-specification. */
22450 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22451 asm_specification = cp_parser_asm_specification_opt (parser);
22453 else
22454 asm_specification = NULL_TREE;
22456 /* Gather the attributes that were provided with the
22457 decl-specifiers. */
22458 prefix_attributes = decl_specifiers->attributes;
22460 /* Look for attributes. */
22461 attributes_start_token = cp_lexer_peek_token (parser->lexer);
22462 attributes = cp_parser_attributes_opt (parser);
22464 /* Peek at the next token. */
22465 token = cp_lexer_peek_token (parser->lexer);
22467 bool bogus_implicit_tmpl = false;
22469 if (function_declarator_p (declarator))
22471 /* Handle C++17 deduction guides. Note that class-scope
22472 non-template deduction guides are instead handled in
22473 cp_parser_member_declaration. */
22474 cp_parser_maybe_adjust_declarator_for_dguide (parser,
22475 decl_specifiers,
22476 declarator,
22477 &ctor_dtor_or_conv_p);
22479 if (!member_p && !cp_parser_error_occurred (parser))
22480 warn_about_ambiguous_parse (decl_specifiers, declarator);
22482 /* Check to see if the token indicates the start of a
22483 function-definition. */
22484 if (cp_parser_token_starts_function_definition_p (token))
22486 if (!function_definition_allowed_p)
22488 /* If a function-definition should not appear here, issue an
22489 error message. */
22490 cp_parser_error (parser,
22491 "a function-definition is not allowed here");
22492 return error_mark_node;
22495 location_t func_brace_location
22496 = cp_lexer_peek_token (parser->lexer)->location;
22498 /* Neither attributes nor an asm-specification are allowed
22499 on a function-definition. */
22500 if (asm_specification)
22501 error_at (asm_spec_start_token->location,
22502 "an %<asm%> specification is not allowed "
22503 "on a function-definition");
22504 if (attributes)
22505 error_at (attributes_start_token->location,
22506 "attributes are not allowed "
22507 "on a function-definition");
22508 /* This is a function-definition. */
22509 *function_definition_p = true;
22511 /* Parse the function definition. */
22512 if (member_p)
22513 decl = cp_parser_save_member_function_body (parser,
22514 decl_specifiers,
22515 declarator,
22516 prefix_attributes);
22517 else
22518 decl =
22519 (cp_parser_function_definition_from_specifiers_and_declarator
22520 (parser, decl_specifiers, prefix_attributes, declarator));
22522 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
22524 /* This is where the prologue starts... */
22525 DECL_STRUCT_FUNCTION (decl)->function_start_locus
22526 = func_brace_location;
22529 return decl;
22532 else if (parser->fully_implicit_function_template_p)
22534 /* A non-template declaration involving a function parameter list
22535 containing an implicit template parameter will be made into a
22536 template. If the resulting declaration is not going to be an
22537 actual function then finish the template scope here to prevent it.
22538 An error message will be issued once we have a decl to talk about.
22540 FIXME probably we should do type deduction rather than create an
22541 implicit template, but the standard currently doesn't allow it. */
22542 bogus_implicit_tmpl = true;
22543 finish_fully_implicit_template (parser, NULL_TREE);
22546 /* [dcl.dcl]
22548 Only in function declarations for constructors, destructors, type
22549 conversions, and deduction guides can the decl-specifier-seq be omitted.
22551 We explicitly postpone this check past the point where we handle
22552 function-definitions because we tolerate function-definitions
22553 that are missing their return types in some modes. */
22554 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
22556 cp_parser_error (parser,
22557 "expected constructor, destructor, or type conversion");
22558 return error_mark_node;
22561 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
22562 if (token->type == CPP_EQ
22563 || token->type == CPP_OPEN_PAREN
22564 || token->type == CPP_OPEN_BRACE)
22566 is_initialized = SD_INITIALIZED;
22567 initialization_kind = token->type;
22568 declarator->init_loc = token->location;
22569 if (maybe_range_for_decl)
22570 *maybe_range_for_decl = error_mark_node;
22571 tmp_init_loc = token->location;
22572 if (init_loc && *init_loc == UNKNOWN_LOCATION)
22573 *init_loc = tmp_init_loc;
22575 if (token->type == CPP_EQ
22576 && function_declarator_p (declarator))
22578 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
22579 if (t2->keyword == RID_DEFAULT)
22580 is_initialized = SD_DEFAULTED;
22581 else if (t2->keyword == RID_DELETE)
22582 is_initialized = SD_DELETED;
22585 else
22587 /* If the init-declarator isn't initialized and isn't followed by a
22588 `,' or `;', it's not a valid init-declarator. */
22589 if (token->type != CPP_COMMA
22590 && token->type != CPP_SEMICOLON)
22592 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
22593 range_for_decl_p = true;
22594 else
22596 if (!maybe_range_for_decl)
22597 cp_parser_error (parser, "expected initializer");
22598 return error_mark_node;
22601 is_initialized = SD_UNINITIALIZED;
22602 initialization_kind = CPP_EOF;
22605 /* Because start_decl has side-effects, we should only call it if we
22606 know we're going ahead. By this point, we know that we cannot
22607 possibly be looking at any other construct. */
22608 cp_parser_commit_to_tentative_parse (parser);
22610 /* Enter the newly declared entry in the symbol table. If we're
22611 processing a declaration in a class-specifier, we wait until
22612 after processing the initializer. */
22613 if (!member_p)
22615 if (parser->in_unbraced_linkage_specification_p)
22616 decl_specifiers->storage_class = sc_extern;
22617 decl = start_decl (declarator, decl_specifiers,
22618 range_for_decl_p? SD_INITIALIZED : is_initialized,
22619 attributes, prefix_attributes, &pushed_scope);
22620 cp_finalize_omp_declare_simd (parser, decl);
22621 cp_finalize_oacc_routine (parser, decl, false);
22622 /* Adjust location of decl if declarator->id_loc is more appropriate:
22623 set, and decl wasn't merged with another decl, in which case its
22624 location would be different from input_location, and more accurate. */
22625 if (DECL_P (decl)
22626 && declarator->id_loc != UNKNOWN_LOCATION
22627 && DECL_SOURCE_LOCATION (decl) == input_location)
22628 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
22630 else if (scope)
22631 /* Enter the SCOPE. That way unqualified names appearing in the
22632 initializer will be looked up in SCOPE. */
22633 pushed_scope = push_scope (scope);
22635 /* Perform deferred access control checks, now that we know in which
22636 SCOPE the declared entity resides. */
22637 if (!member_p && decl)
22639 tree saved_current_function_decl = NULL_TREE;
22641 /* If the entity being declared is a function, pretend that we
22642 are in its scope. If it is a `friend', it may have access to
22643 things that would not otherwise be accessible. */
22644 if (TREE_CODE (decl) == FUNCTION_DECL)
22646 saved_current_function_decl = current_function_decl;
22647 current_function_decl = decl;
22650 /* Perform access checks for template parameters. */
22651 cp_parser_perform_template_parameter_access_checks (checks);
22653 /* Perform the access control checks for the declarator and the
22654 decl-specifiers. */
22655 perform_deferred_access_checks (tf_warning_or_error);
22657 /* Restore the saved value. */
22658 if (TREE_CODE (decl) == FUNCTION_DECL)
22659 current_function_decl = saved_current_function_decl;
22662 /* Parse the initializer. */
22663 initializer = NULL_TREE;
22664 is_direct_init = false;
22665 is_non_constant_init = true;
22666 if (is_initialized)
22668 if (function_declarator_p (declarator))
22670 if (initialization_kind == CPP_EQ)
22671 initializer = cp_parser_pure_specifier (parser);
22672 else
22674 /* If the declaration was erroneous, we don't really
22675 know what the user intended, so just silently
22676 consume the initializer. */
22677 if (decl != error_mark_node)
22678 error_at (tmp_init_loc, "initializer provided for function");
22679 cp_parser_skip_to_closing_parenthesis (parser,
22680 /*recovering=*/true,
22681 /*or_comma=*/false,
22682 /*consume_paren=*/true);
22685 else
22687 /* We want to record the extra mangling scope for in-class
22688 initializers of class members and initializers of static
22689 data member templates and namespace-scope initializers.
22690 The former involves deferring parsing of the initializer
22691 until end of class as with default arguments. So right
22692 here we only handle the latter two. */
22693 bool has_lambda_scope = false;
22695 if (decl != error_mark_node
22696 && !member_p
22697 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
22698 has_lambda_scope = true;
22700 if (has_lambda_scope)
22701 start_lambda_scope (decl);
22702 initializer = cp_parser_initializer (parser,
22703 &is_direct_init,
22704 &is_non_constant_init);
22705 if (has_lambda_scope)
22706 finish_lambda_scope ();
22707 if (initializer == error_mark_node)
22708 cp_parser_skip_to_end_of_statement (parser);
22712 /* The old parser allows attributes to appear after a parenthesized
22713 initializer. Mark Mitchell proposed removing this functionality
22714 on the GCC mailing lists on 2002-08-13. This parser accepts the
22715 attributes -- but ignores them. Made a permerror in GCC 8. */
22716 if (cp_parser_allow_gnu_extensions_p (parser)
22717 && initialization_kind == CPP_OPEN_PAREN
22718 && cp_parser_attributes_opt (parser)
22719 && permerror (input_location,
22720 "attributes after parenthesized initializer ignored"))
22722 static bool hint;
22723 if (flag_permissive && !hint)
22725 hint = true;
22726 inform (input_location,
22727 "this flexibility is deprecated and will be removed");
22731 /* And now complain about a non-function implicit template. */
22732 if (bogus_implicit_tmpl && decl != error_mark_node)
22733 error_at (DECL_SOURCE_LOCATION (decl),
22734 "non-function %qD declared as implicit template", decl);
22736 /* For an in-class declaration, use `grokfield' to create the
22737 declaration. */
22738 if (member_p)
22740 if (pushed_scope)
22742 pop_scope (pushed_scope);
22743 pushed_scope = NULL_TREE;
22745 decl = grokfield (declarator, decl_specifiers,
22746 initializer, !is_non_constant_init,
22747 /*asmspec=*/NULL_TREE,
22748 attr_chainon (attributes, prefix_attributes));
22749 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
22750 cp_parser_save_default_args (parser, decl);
22751 cp_finalize_omp_declare_simd (parser, decl);
22752 cp_finalize_oacc_routine (parser, decl, false);
22755 /* Finish processing the declaration. But, skip member
22756 declarations. */
22757 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
22759 cp_finish_decl (decl,
22760 initializer, !is_non_constant_init,
22761 asm_specification,
22762 /* If the initializer is in parentheses, then this is
22763 a direct-initialization, which means that an
22764 `explicit' constructor is OK. Otherwise, an
22765 `explicit' constructor cannot be used. */
22766 ((is_direct_init || !is_initialized)
22767 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
22769 else if ((cxx_dialect != cxx98) && friend_p
22770 && decl && TREE_CODE (decl) == FUNCTION_DECL)
22771 /* Core issue #226 (C++0x only): A default template-argument
22772 shall not be specified in a friend class template
22773 declaration. */
22774 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
22775 /*is_partial=*/false, /*is_friend_decl=*/1);
22777 if (!friend_p && pushed_scope)
22778 pop_scope (pushed_scope);
22780 if (function_declarator_p (declarator)
22781 && parser->fully_implicit_function_template_p)
22783 if (member_p)
22784 decl = finish_fully_implicit_template (parser, decl);
22785 else
22786 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22789 if (auto_result && is_initialized && decl_specifiers->type
22790 && type_uses_auto (decl_specifiers->type))
22791 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
22793 return decl;
22796 /* Parse a declarator.
22798 declarator:
22799 direct-declarator
22800 ptr-operator declarator
22802 abstract-declarator:
22803 ptr-operator abstract-declarator [opt]
22804 direct-abstract-declarator
22806 GNU Extensions:
22808 declarator:
22809 attributes [opt] direct-declarator
22810 attributes [opt] ptr-operator declarator
22812 abstract-declarator:
22813 attributes [opt] ptr-operator abstract-declarator [opt]
22814 attributes [opt] direct-abstract-declarator
22816 The parser flags FLAGS is used to control type-specifier parsing.
22818 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
22819 detect constructors, destructors, deduction guides, or conversion operators.
22820 It is set to -1 if the declarator is a name, and +1 if it is a
22821 function. Otherwise it is set to zero. Usually you just want to
22822 test for >0, but internally the negative value is used.
22824 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
22825 a decl-specifier-seq unless it declares a constructor, destructor,
22826 or conversion. It might seem that we could check this condition in
22827 semantic analysis, rather than parsing, but that makes it difficult
22828 to handle something like `f()'. We want to notice that there are
22829 no decl-specifiers, and therefore realize that this is an
22830 expression, not a declaration.)
22832 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
22833 the declarator is a direct-declarator of the form "(...)".
22835 MEMBER_P is true iff this declarator is a member-declarator.
22837 FRIEND_P is true iff this declarator is a friend.
22839 STATIC_P is true iff the keyword static was seen. */
22841 static cp_declarator *
22842 cp_parser_declarator (cp_parser* parser,
22843 cp_parser_declarator_kind dcl_kind,
22844 cp_parser_flags flags,
22845 int* ctor_dtor_or_conv_p,
22846 bool* parenthesized_p,
22847 bool member_p, bool friend_p, bool static_p)
22849 cp_declarator *declarator;
22850 enum tree_code code;
22851 cp_cv_quals cv_quals;
22852 tree class_type;
22853 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
22855 /* Assume this is not a constructor, destructor, or type-conversion
22856 operator. */
22857 if (ctor_dtor_or_conv_p)
22858 *ctor_dtor_or_conv_p = 0;
22860 if (cp_parser_allow_gnu_extensions_p (parser))
22861 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
22863 /* Check for the ptr-operator production. */
22864 cp_parser_parse_tentatively (parser);
22865 /* Parse the ptr-operator. */
22866 code = cp_parser_ptr_operator (parser,
22867 &class_type,
22868 &cv_quals,
22869 &std_attributes);
22871 /* If that worked, then we have a ptr-operator. */
22872 if (cp_parser_parse_definitely (parser))
22874 /* If a ptr-operator was found, then this declarator was not
22875 parenthesized. */
22876 if (parenthesized_p)
22877 *parenthesized_p = false;
22878 /* The dependent declarator is optional if we are parsing an
22879 abstract-declarator. */
22880 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
22881 cp_parser_parse_tentatively (parser);
22883 /* Parse the dependent declarator. */
22884 declarator = cp_parser_declarator (parser, dcl_kind, flags,
22885 /*ctor_dtor_or_conv_p=*/NULL,
22886 /*parenthesized_p=*/NULL,
22887 member_p, friend_p, static_p);
22889 /* If we are parsing an abstract-declarator, we must handle the
22890 case where the dependent declarator is absent. */
22891 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
22892 && !cp_parser_parse_definitely (parser))
22893 declarator = NULL;
22895 declarator = cp_parser_make_indirect_declarator
22896 (code, class_type, cv_quals, declarator, std_attributes);
22898 /* Everything else is a direct-declarator. */
22899 else
22901 if (parenthesized_p)
22902 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
22903 CPP_OPEN_PAREN);
22904 declarator = cp_parser_direct_declarator (parser, dcl_kind,
22905 flags, ctor_dtor_or_conv_p,
22906 member_p, friend_p, static_p);
22909 if (gnu_attributes && declarator && declarator != cp_error_declarator)
22910 declarator->attributes = gnu_attributes;
22911 return declarator;
22914 /* Parse a direct-declarator or direct-abstract-declarator.
22916 direct-declarator:
22917 declarator-id
22918 direct-declarator ( parameter-declaration-clause )
22919 cv-qualifier-seq [opt]
22920 ref-qualifier [opt]
22921 exception-specification [opt]
22922 direct-declarator [ constant-expression [opt] ]
22923 ( declarator )
22925 direct-abstract-declarator:
22926 direct-abstract-declarator [opt]
22927 ( parameter-declaration-clause )
22928 cv-qualifier-seq [opt]
22929 ref-qualifier [opt]
22930 exception-specification [opt]
22931 direct-abstract-declarator [opt] [ constant-expression [opt] ]
22932 ( abstract-declarator )
22934 Returns a representation of the declarator. DCL_KIND is
22935 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
22936 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
22937 we are parsing a direct-declarator. It is
22938 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
22939 of ambiguity we prefer an abstract declarator, as per
22940 [dcl.ambig.res].
22941 The parser flags FLAGS is used to control type-specifier parsing.
22942 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
22943 as for cp_parser_declarator. */
22945 static cp_declarator *
22946 cp_parser_direct_declarator (cp_parser* parser,
22947 cp_parser_declarator_kind dcl_kind,
22948 cp_parser_flags flags,
22949 int* ctor_dtor_or_conv_p,
22950 bool member_p, bool friend_p, bool static_p)
22952 cp_token *token;
22953 cp_declarator *declarator = NULL;
22954 tree scope = NULL_TREE;
22955 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22956 bool saved_in_declarator_p = parser->in_declarator_p;
22957 bool first = true;
22958 tree pushed_scope = NULL_TREE;
22959 cp_token *open_paren = NULL, *close_paren = NULL;
22961 while (true)
22963 /* Peek at the next token. */
22964 token = cp_lexer_peek_token (parser->lexer);
22965 if (token->type == CPP_OPEN_PAREN)
22967 /* This is either a parameter-declaration-clause, or a
22968 parenthesized declarator. When we know we are parsing a
22969 named declarator, it must be a parenthesized declarator
22970 if FIRST is true. For instance, `(int)' is a
22971 parameter-declaration-clause, with an omitted
22972 direct-abstract-declarator. But `((*))', is a
22973 parenthesized abstract declarator. Finally, when T is a
22974 template parameter `(T)' is a
22975 parameter-declaration-clause, and not a parenthesized
22976 named declarator.
22978 We first try and parse a parameter-declaration-clause,
22979 and then try a nested declarator (if FIRST is true).
22981 It is not an error for it not to be a
22982 parameter-declaration-clause, even when FIRST is
22983 false. Consider,
22985 int i (int);
22986 int i (3);
22988 The first is the declaration of a function while the
22989 second is the definition of a variable, including its
22990 initializer.
22992 Having seen only the parenthesis, we cannot know which of
22993 these two alternatives should be selected. Even more
22994 complex are examples like:
22996 int i (int (a));
22997 int i (int (3));
22999 The former is a function-declaration; the latter is a
23000 variable initialization.
23002 Thus again, we try a parameter-declaration-clause, and if
23003 that fails, we back out and return. */
23005 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23007 tree params;
23008 bool is_declarator = false;
23010 open_paren = NULL;
23012 /* In a member-declarator, the only valid interpretation
23013 of a parenthesis is the start of a
23014 parameter-declaration-clause. (It is invalid to
23015 initialize a static data member with a parenthesized
23016 initializer; only the "=" form of initialization is
23017 permitted.) */
23018 if (!member_p)
23019 cp_parser_parse_tentatively (parser);
23021 /* Consume the `('. */
23022 const location_t parens_start = token->location;
23023 matching_parens parens;
23024 parens.consume_open (parser);
23025 if (first)
23027 /* If this is going to be an abstract declarator, we're
23028 in a declarator and we can't have default args. */
23029 parser->default_arg_ok_p = false;
23030 parser->in_declarator_p = true;
23033 begin_scope (sk_function_parms, NULL_TREE);
23035 /* Signal we are in the immediate function context. */
23036 if (flags & CP_PARSER_FLAGS_CONSTEVAL)
23037 current_binding_level->immediate_fn_ctx_p = true;
23039 /* Parse the parameter-declaration-clause. */
23040 params
23041 = cp_parser_parameter_declaration_clause (parser, flags);
23042 const location_t parens_end
23043 = cp_lexer_peek_token (parser->lexer)->location;
23045 /* Consume the `)'. */
23046 parens.require_close (parser);
23048 /* If all went well, parse the cv-qualifier-seq,
23049 ref-qualifier and the exception-specification. */
23050 if (member_p || cp_parser_parse_definitely (parser))
23052 cp_cv_quals cv_quals;
23053 cp_virt_specifiers virt_specifiers;
23054 cp_ref_qualifier ref_qual;
23055 tree exception_specification;
23056 tree late_return;
23057 tree attrs;
23058 bool memfn = (member_p || (pushed_scope
23059 && CLASS_TYPE_P (pushed_scope)));
23060 unsigned char local_variables_forbidden_p
23061 = parser->local_variables_forbidden_p;
23062 /* 'this' is not allowed in static member functions. */
23063 if (static_p || friend_p)
23064 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23066 is_declarator = true;
23068 if (ctor_dtor_or_conv_p)
23069 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23070 first = false;
23072 /* Parse the cv-qualifier-seq. */
23073 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23074 /* Parse the ref-qualifier. */
23075 ref_qual = cp_parser_ref_qualifier_opt (parser);
23076 /* Parse the tx-qualifier. */
23077 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23079 tree save_ccp = current_class_ptr;
23080 tree save_ccr = current_class_ref;
23081 if (memfn && !friend_p && !static_p)
23082 /* DR 1207: 'this' is in scope after the cv-quals. */
23083 inject_this_parameter (current_class_type, cv_quals);
23085 /* If it turned out that this is e.g. a pointer to a
23086 function, we don't want to delay noexcept parsing. */
23087 if (declarator == NULL || declarator->kind != cdk_id)
23088 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23090 /* Parse the exception-specification. */
23091 exception_specification
23092 = cp_parser_exception_specification_opt (parser,
23093 flags);
23095 attrs = cp_parser_std_attribute_spec_seq (parser);
23097 cp_omp_declare_simd_data odsd;
23098 if ((flag_openmp || flag_openmp_simd)
23099 && declarator
23100 && declarator->std_attributes
23101 && declarator->kind == cdk_id)
23103 tree *pa = &declarator->std_attributes;
23104 cp_parser_handle_directive_omp_attributes (parser, pa,
23105 &odsd, false);
23108 /* In here, we handle cases where attribute is used after
23109 the function declaration. For example:
23110 void func (int x) __attribute__((vector(..))); */
23111 tree gnu_attrs = NULL_TREE;
23112 tree requires_clause = NULL_TREE;
23113 late_return
23114 = cp_parser_late_return_type_opt (parser, declarator,
23115 requires_clause);
23117 cp_finalize_omp_declare_simd (parser, &odsd);
23119 /* Parse the virt-specifier-seq. */
23120 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23122 location_t parens_loc = make_location (parens_start,
23123 parens_start,
23124 parens_end);
23125 /* Create the function-declarator. */
23126 declarator = make_call_declarator (declarator,
23127 params,
23128 cv_quals,
23129 virt_specifiers,
23130 ref_qual,
23131 tx_qual,
23132 exception_specification,
23133 late_return,
23134 requires_clause,
23135 parens_loc);
23136 declarator->std_attributes = attrs;
23137 declarator->attributes = gnu_attrs;
23138 /* Any subsequent parameter lists are to do with
23139 return type, so are not those of the declared
23140 function. */
23141 parser->default_arg_ok_p = false;
23143 current_class_ptr = save_ccp;
23144 current_class_ref = save_ccr;
23146 /* Restore the state of local_variables_forbidden_p. */
23147 parser->local_variables_forbidden_p
23148 = local_variables_forbidden_p;
23151 /* Remove the function parms from scope. */
23152 pop_bindings_and_leave_scope ();
23154 if (is_declarator)
23155 /* Repeat the main loop. */
23156 continue;
23159 /* If this is the first, we can try a parenthesized
23160 declarator. */
23161 if (first)
23163 bool saved_in_type_id_in_expr_p;
23165 parser->default_arg_ok_p = saved_default_arg_ok_p;
23166 parser->in_declarator_p = saved_in_declarator_p;
23168 open_paren = token;
23169 /* Consume the `('. */
23170 matching_parens parens;
23171 parens.consume_open (parser);
23172 /* Parse the nested declarator. */
23173 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23174 parser->in_type_id_in_expr_p = true;
23175 declarator
23176 = cp_parser_declarator (parser, dcl_kind, flags,
23177 ctor_dtor_or_conv_p,
23178 /*parenthesized_p=*/NULL,
23179 member_p, friend_p,
23180 /*static_p=*/false);
23181 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23182 first = false;
23183 /* Expect a `)'. */
23184 close_paren = cp_lexer_peek_token (parser->lexer);
23185 if (!parens.require_close (parser))
23186 declarator = cp_error_declarator;
23187 if (declarator == cp_error_declarator)
23188 break;
23190 goto handle_declarator;
23192 /* Otherwise, we must be done. */
23193 else
23194 break;
23196 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23197 && token->type == CPP_OPEN_SQUARE
23198 && !cp_next_tokens_can_be_attribute_p (parser))
23200 /* Parse an array-declarator. */
23201 tree bounds, attrs;
23203 if (ctor_dtor_or_conv_p)
23204 *ctor_dtor_or_conv_p = 0;
23206 open_paren = NULL;
23207 first = false;
23208 parser->default_arg_ok_p = false;
23209 parser->in_declarator_p = true;
23210 /* Consume the `['. */
23211 cp_lexer_consume_token (parser->lexer);
23212 /* Peek at the next token. */
23213 token = cp_lexer_peek_token (parser->lexer);
23214 /* If the next token is `]', then there is no
23215 constant-expression. */
23216 if (token->type != CPP_CLOSE_SQUARE)
23218 bool non_constant_p;
23219 bounds
23220 = cp_parser_constant_expression (parser,
23221 /*allow_non_constant=*/true,
23222 &non_constant_p);
23223 if (!non_constant_p)
23224 /* OK */;
23225 else if (error_operand_p (bounds))
23226 /* Already gave an error. */;
23227 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23228 /* Let compute_array_index_type diagnose this. */;
23229 else if (!parser->in_function_body
23230 || parsing_function_declarator ())
23232 /* Normally, the array bound must be an integral constant
23233 expression. However, as an extension, we allow VLAs
23234 in function scopes as long as they aren't part of a
23235 parameter declaration. */
23236 cp_parser_error (parser,
23237 "array bound is not an integer constant");
23238 bounds = error_mark_node;
23240 else if (processing_template_decl
23241 && !type_dependent_expression_p (bounds))
23243 /* Remember this wasn't a constant-expression. */
23244 bounds = build_nop (TREE_TYPE (bounds), bounds);
23245 TREE_SIDE_EFFECTS (bounds) = 1;
23248 else
23249 bounds = NULL_TREE;
23250 /* Look for the closing `]'. */
23251 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23253 declarator = cp_error_declarator;
23254 break;
23257 attrs = cp_parser_std_attribute_spec_seq (parser);
23258 declarator = make_array_declarator (declarator, bounds);
23259 declarator->std_attributes = attrs;
23261 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23264 tree qualifying_scope;
23265 tree unqualified_name;
23266 tree attrs;
23267 special_function_kind sfk;
23268 bool abstract_ok;
23269 bool pack_expansion_p = false;
23270 cp_token *declarator_id_start_token;
23272 /* Parse a declarator-id */
23273 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23274 if (abstract_ok)
23276 cp_parser_parse_tentatively (parser);
23278 /* If we see an ellipsis, we should be looking at a
23279 parameter pack. */
23280 if (token->type == CPP_ELLIPSIS)
23282 /* Consume the `...' */
23283 cp_lexer_consume_token (parser->lexer);
23285 pack_expansion_p = true;
23289 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23290 unqualified_name
23291 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23292 qualifying_scope = parser->scope;
23293 if (abstract_ok)
23295 bool okay = false;
23297 if (!unqualified_name && pack_expansion_p)
23299 /* Check whether an error occurred. */
23300 okay = !cp_parser_error_occurred (parser);
23302 /* We already consumed the ellipsis to mark a
23303 parameter pack, but we have no way to report it,
23304 so abort the tentative parse. We will be exiting
23305 immediately anyway. */
23306 cp_parser_abort_tentative_parse (parser);
23308 else
23309 okay = cp_parser_parse_definitely (parser);
23311 if (!okay)
23312 unqualified_name = error_mark_node;
23313 else if (unqualified_name
23314 && (qualifying_scope
23315 || (!identifier_p (unqualified_name))))
23317 cp_parser_error (parser, "expected unqualified-id");
23318 unqualified_name = error_mark_node;
23322 if (!unqualified_name)
23323 return NULL;
23324 if (unqualified_name == error_mark_node)
23326 declarator = cp_error_declarator;
23327 pack_expansion_p = false;
23328 declarator->parameter_pack_p = false;
23329 break;
23332 attrs = cp_parser_std_attribute_spec_seq (parser);
23334 if (qualifying_scope && at_namespace_scope_p ()
23335 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23337 /* In the declaration of a member of a template class
23338 outside of the class itself, the SCOPE will sometimes
23339 be a TYPENAME_TYPE. For example, given:
23341 template <typename T>
23342 int S<T>::R::i = 3;
23344 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23345 this context, we must resolve S<T>::R to an ordinary
23346 type, rather than a typename type.
23348 The reason we normally avoid resolving TYPENAME_TYPEs
23349 is that a specialization of `S' might render
23350 `S<T>::R' not a type. However, if `S' is
23351 specialized, then this `i' will not be used, so there
23352 is no harm in resolving the types here. */
23353 tree type;
23355 /* Resolve the TYPENAME_TYPE. */
23356 type = resolve_typename_type (qualifying_scope,
23357 /*only_current_p=*/false);
23358 /* If that failed, the declarator is invalid. */
23359 if (TREE_CODE (type) == TYPENAME_TYPE)
23361 if (typedef_variant_p (type))
23362 error_at (declarator_id_start_token->location,
23363 "cannot define member of dependent typedef "
23364 "%qT", type);
23365 else
23366 error_at (declarator_id_start_token->location,
23367 "%<%T::%E%> is not a type",
23368 TYPE_CONTEXT (qualifying_scope),
23369 TYPE_IDENTIFIER (qualifying_scope));
23371 qualifying_scope = type;
23374 sfk = sfk_none;
23376 if (unqualified_name)
23378 tree class_type;
23380 if (qualifying_scope
23381 && CLASS_TYPE_P (qualifying_scope))
23382 class_type = qualifying_scope;
23383 else
23384 class_type = current_class_type;
23386 if (TREE_CODE (unqualified_name) == TYPE_DECL)
23388 tree name_type = TREE_TYPE (unqualified_name);
23390 if (!class_type || !same_type_p (name_type, class_type))
23392 /* We do not attempt to print the declarator
23393 here because we do not have enough
23394 information about its original syntactic
23395 form. */
23396 cp_parser_error (parser, "invalid declarator");
23397 declarator = cp_error_declarator;
23398 break;
23400 else if (qualifying_scope
23401 && CLASSTYPE_USE_TEMPLATE (name_type))
23403 error_at (declarator_id_start_token->location,
23404 "invalid use of constructor as a template");
23405 inform (declarator_id_start_token->location,
23406 "use %<%T::%D%> instead of %<%T::%D%> to "
23407 "name the constructor in a qualified name",
23408 class_type,
23409 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23410 class_type, name_type);
23411 declarator = cp_error_declarator;
23412 break;
23414 unqualified_name = constructor_name (class_type);
23417 if (class_type)
23419 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23420 sfk = sfk_destructor;
23421 else if (identifier_p (unqualified_name)
23422 && IDENTIFIER_CONV_OP_P (unqualified_name))
23423 sfk = sfk_conversion;
23424 else if (/* There's no way to declare a constructor
23425 for an unnamed type, even if the type
23426 got a name for linkage purposes. */
23427 !TYPE_WAS_UNNAMED (class_type)
23428 /* Handle correctly (c++/19200):
23430 struct S {
23431 struct T{};
23432 friend void S(T);
23435 and also:
23437 namespace N {
23438 void S();
23441 struct S {
23442 friend void N::S();
23443 }; */
23444 && (!friend_p || class_type == qualifying_scope)
23445 && constructor_name_p (unqualified_name,
23446 class_type))
23447 sfk = sfk_constructor;
23448 else if (is_overloaded_fn (unqualified_name)
23449 && DECL_CONSTRUCTOR_P (get_first_fn
23450 (unqualified_name)))
23451 sfk = sfk_constructor;
23453 if (ctor_dtor_or_conv_p && sfk != sfk_none)
23454 *ctor_dtor_or_conv_p = -1;
23457 declarator = make_id_declarator (qualifying_scope,
23458 unqualified_name,
23459 sfk, token->location);
23460 declarator->std_attributes = attrs;
23461 declarator->parameter_pack_p = pack_expansion_p;
23463 if (pack_expansion_p)
23464 maybe_warn_variadic_templates ();
23466 /* We're looking for this case in [temp.res]:
23467 A qualified-id is assumed to name a type if [...]
23468 - it is a decl-specifier of the decl-specifier-seq of a
23469 parameter-declaration in a declarator of a function or
23470 function template declaration, ... */
23471 if (cxx_dialect >= cxx20
23472 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
23473 && declarator->kind == cdk_id
23474 && !at_class_scope_p ()
23475 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23477 /* ...whose declarator-id is qualified. If it isn't, never
23478 assume the parameters to refer to types. */
23479 if (qualifying_scope == NULL_TREE)
23480 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23481 else
23483 /* Now we have something like
23484 template <typename T> int C::x(S::p);
23485 which can be a function template declaration or a
23486 variable template definition. If name lookup for
23487 the declarator-id C::x finds one or more function
23488 templates, assume S::p to name a type. Otherwise,
23489 don't. */
23490 tree decl
23491 = cp_parser_lookup_name (parser, unqualified_name,
23492 none_type,
23493 /*is_template=*/false,
23494 /*is_namespace=*/false,
23495 /*check_dependency=*/false,
23496 /*ambiguous_decls=*/NULL,
23497 token->location);
23499 if (!is_overloaded_fn (decl)
23500 /* Allow
23501 template<typename T>
23502 A<T>::A(T::type) { } */
23503 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
23504 && constructor_name_p (unqualified_name,
23505 qualifying_scope)))
23506 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23511 handle_declarator:;
23512 scope = get_scope_of_declarator (declarator);
23513 if (scope)
23515 /* Any names that appear after the declarator-id for a
23516 member are looked up in the containing scope. */
23517 if (at_function_scope_p ())
23519 /* But declarations with qualified-ids can't appear in a
23520 function. */
23521 cp_parser_error (parser, "qualified-id in declaration");
23522 declarator = cp_error_declarator;
23523 break;
23525 pushed_scope = push_scope (scope);
23527 parser->in_declarator_p = true;
23528 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
23529 || (declarator && declarator->kind == cdk_id))
23530 /* Default args are only allowed on function
23531 declarations. */
23532 parser->default_arg_ok_p = saved_default_arg_ok_p;
23533 else
23534 parser->default_arg_ok_p = false;
23536 first = false;
23538 /* We're done. */
23539 else
23540 break;
23543 /* For an abstract declarator, we might wind up with nothing at this
23544 point. That's an error; the declarator is not optional. */
23545 if (!declarator)
23546 cp_parser_error (parser, "expected declarator");
23547 else if (open_paren)
23549 /* Record overly parenthesized declarator so we can give a
23550 diagnostic about confusing decl/expr disambiguation. */
23551 if (declarator->kind == cdk_array)
23553 /* If the open and close parens are on different lines, this
23554 is probably a formatting thing, so ignore. */
23555 expanded_location open = expand_location (open_paren->location);
23556 expanded_location close = expand_location (close_paren->location);
23557 if (open.line != close.line || open.file != close.file)
23558 open_paren = NULL;
23560 if (open_paren)
23561 declarator->parenthesized = make_location (open_paren->location,
23562 open_paren->location,
23563 close_paren->location);
23566 /* If we entered a scope, we must exit it now. */
23567 if (pushed_scope)
23568 pop_scope (pushed_scope);
23570 parser->default_arg_ok_p = saved_default_arg_ok_p;
23571 parser->in_declarator_p = saved_in_declarator_p;
23573 return declarator;
23576 /* Parse a ptr-operator.
23578 ptr-operator:
23579 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23580 * cv-qualifier-seq [opt]
23582 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23583 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23585 GNU Extension:
23587 ptr-operator:
23588 & cv-qualifier-seq [opt]
23590 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23591 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23592 an rvalue reference. In the case of a pointer-to-member, *TYPE is
23593 filled in with the TYPE containing the member. *CV_QUALS is
23594 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23595 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
23596 Note that the tree codes returned by this function have nothing
23597 to do with the types of trees that will be eventually be created
23598 to represent the pointer or reference type being parsed. They are
23599 just constants with suggestive names. */
23600 static enum tree_code
23601 cp_parser_ptr_operator (cp_parser* parser,
23602 tree* type,
23603 cp_cv_quals *cv_quals,
23604 tree *attributes)
23606 enum tree_code code = ERROR_MARK;
23607 cp_token *token;
23608 tree attrs = NULL_TREE;
23610 /* Assume that it's not a pointer-to-member. */
23611 *type = NULL_TREE;
23612 /* And that there are no cv-qualifiers. */
23613 *cv_quals = TYPE_UNQUALIFIED;
23615 /* Peek at the next token. */
23616 token = cp_lexer_peek_token (parser->lexer);
23618 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
23619 if (token->type == CPP_MULT)
23620 code = INDIRECT_REF;
23621 else if (token->type == CPP_AND)
23622 code = ADDR_EXPR;
23623 else if ((cxx_dialect != cxx98) &&
23624 token->type == CPP_AND_AND) /* C++0x only */
23625 code = NON_LVALUE_EXPR;
23627 if (code != ERROR_MARK)
23629 /* Consume the `*', `&' or `&&'. */
23630 cp_lexer_consume_token (parser->lexer);
23632 /* A `*' can be followed by a cv-qualifier-seq, and so can a
23633 `&', if we are allowing GNU extensions. (The only qualifier
23634 that can legally appear after `&' is `restrict', but that is
23635 enforced during semantic analysis. */
23636 if (code == INDIRECT_REF
23637 || cp_parser_allow_gnu_extensions_p (parser))
23638 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23640 attrs = cp_parser_std_attribute_spec_seq (parser);
23641 if (attributes != NULL)
23642 *attributes = attrs;
23644 else
23646 /* Try the pointer-to-member case. */
23647 cp_parser_parse_tentatively (parser);
23648 /* Look for the optional `::' operator. */
23649 cp_parser_global_scope_opt (parser,
23650 /*current_scope_valid_p=*/false);
23651 /* Look for the nested-name specifier. */
23652 token = cp_lexer_peek_token (parser->lexer);
23653 cp_parser_nested_name_specifier (parser,
23654 /*typename_keyword_p=*/false,
23655 /*check_dependency_p=*/true,
23656 /*type_p=*/false,
23657 /*is_declaration=*/false);
23658 /* If we found it, and the next token is a `*', then we are
23659 indeed looking at a pointer-to-member operator. */
23660 if (!cp_parser_error_occurred (parser)
23661 && cp_parser_require (parser, CPP_MULT, RT_MULT))
23663 /* Indicate that the `*' operator was used. */
23664 code = INDIRECT_REF;
23666 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
23667 error_at (token->location, "%qD is a namespace", parser->scope);
23668 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
23669 error_at (token->location, "cannot form pointer to member of "
23670 "non-class %q#T", parser->scope);
23671 else
23673 /* The type of which the member is a member is given by the
23674 current SCOPE. */
23675 *type = parser->scope;
23676 /* The next name will not be qualified. */
23677 parser->scope = NULL_TREE;
23678 parser->qualifying_scope = NULL_TREE;
23679 parser->object_scope = NULL_TREE;
23680 /* Look for optional c++11 attributes. */
23681 attrs = cp_parser_std_attribute_spec_seq (parser);
23682 if (attributes != NULL)
23683 *attributes = attrs;
23684 /* Look for the optional cv-qualifier-seq. */
23685 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23688 /* If that didn't work we don't have a ptr-operator. */
23689 if (!cp_parser_parse_definitely (parser))
23690 cp_parser_error (parser, "expected ptr-operator");
23693 return code;
23696 /* Parse an (optional) cv-qualifier-seq.
23698 cv-qualifier-seq:
23699 cv-qualifier cv-qualifier-seq [opt]
23701 cv-qualifier:
23702 const
23703 volatile
23705 GNU Extension:
23707 cv-qualifier:
23708 __restrict__
23710 Returns a bitmask representing the cv-qualifiers. */
23712 static cp_cv_quals
23713 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
23715 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
23717 while (true)
23719 cp_token *token;
23720 cp_cv_quals cv_qualifier;
23722 /* Peek at the next token. */
23723 token = cp_lexer_peek_token (parser->lexer);
23724 /* See if it's a cv-qualifier. */
23725 switch (token->keyword)
23727 case RID_CONST:
23728 cv_qualifier = TYPE_QUAL_CONST;
23729 break;
23731 case RID_VOLATILE:
23732 cv_qualifier = TYPE_QUAL_VOLATILE;
23733 break;
23735 case RID_RESTRICT:
23736 cv_qualifier = TYPE_QUAL_RESTRICT;
23737 break;
23739 default:
23740 cv_qualifier = TYPE_UNQUALIFIED;
23741 break;
23744 if (!cv_qualifier)
23745 break;
23747 if (cv_quals & cv_qualifier)
23749 gcc_rich_location richloc (token->location);
23750 richloc.add_fixit_remove ();
23751 error_at (&richloc, "duplicate cv-qualifier");
23752 cp_lexer_purge_token (parser->lexer);
23754 else
23756 cp_lexer_consume_token (parser->lexer);
23757 cv_quals |= cv_qualifier;
23761 return cv_quals;
23764 /* Parse an (optional) ref-qualifier
23766 ref-qualifier:
23770 Returns cp_ref_qualifier representing ref-qualifier. */
23772 static cp_ref_qualifier
23773 cp_parser_ref_qualifier_opt (cp_parser* parser)
23775 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
23777 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
23778 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
23779 return ref_qual;
23781 while (true)
23783 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
23784 cp_token *token = cp_lexer_peek_token (parser->lexer);
23786 switch (token->type)
23788 case CPP_AND:
23789 curr_ref_qual = REF_QUAL_LVALUE;
23790 break;
23792 case CPP_AND_AND:
23793 curr_ref_qual = REF_QUAL_RVALUE;
23794 break;
23796 default:
23797 curr_ref_qual = REF_QUAL_NONE;
23798 break;
23801 if (!curr_ref_qual)
23802 break;
23803 else if (ref_qual)
23805 error_at (token->location, "multiple ref-qualifiers");
23806 cp_lexer_purge_token (parser->lexer);
23808 else
23810 ref_qual = curr_ref_qual;
23811 cp_lexer_consume_token (parser->lexer);
23815 return ref_qual;
23818 /* Parse an optional tx-qualifier.
23820 tx-qualifier:
23821 transaction_safe
23822 transaction_safe_dynamic */
23824 static tree
23825 cp_parser_tx_qualifier_opt (cp_parser *parser)
23827 cp_token *token = cp_lexer_peek_token (parser->lexer);
23828 if (token->type == CPP_NAME)
23830 tree name = token->u.value;
23831 const char *p = IDENTIFIER_POINTER (name);
23832 const int len = strlen ("transaction_safe");
23833 if (startswith (p, "transaction_safe"))
23835 p += len;
23836 if (*p == '\0'
23837 || !strcmp (p, "_dynamic"))
23839 cp_lexer_consume_token (parser->lexer);
23840 if (!flag_tm)
23842 error ("%qE requires %<-fgnu-tm%>", name);
23843 return NULL_TREE;
23845 else
23846 return name;
23850 return NULL_TREE;
23853 /* Parse an (optional) virt-specifier-seq.
23855 virt-specifier-seq:
23856 virt-specifier virt-specifier-seq [opt]
23858 virt-specifier:
23859 override
23860 final
23862 Returns a bitmask representing the virt-specifiers. */
23864 static cp_virt_specifiers
23865 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
23867 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23869 while (true)
23871 cp_token *token;
23872 cp_virt_specifiers virt_specifier;
23874 /* Peek at the next token. */
23875 token = cp_lexer_peek_token (parser->lexer);
23876 /* See if it's a virt-specifier-qualifier. */
23877 if (token->type != CPP_NAME)
23878 break;
23879 if (id_equal (token->u.value, "override"))
23881 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
23882 virt_specifier = VIRT_SPEC_OVERRIDE;
23884 else if (id_equal (token->u.value, "final"))
23886 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
23887 virt_specifier = VIRT_SPEC_FINAL;
23889 else if (id_equal (token->u.value, "__final"))
23891 virt_specifier = VIRT_SPEC_FINAL;
23893 else
23894 break;
23896 if (virt_specifiers & virt_specifier)
23898 gcc_rich_location richloc (token->location);
23899 richloc.add_fixit_remove ();
23900 error_at (&richloc, "duplicate virt-specifier");
23901 cp_lexer_purge_token (parser->lexer);
23903 else
23905 cp_lexer_consume_token (parser->lexer);
23906 virt_specifiers |= virt_specifier;
23909 return virt_specifiers;
23912 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
23913 is in scope even though it isn't real. */
23915 void
23916 inject_this_parameter (tree ctype, cp_cv_quals quals)
23918 tree this_parm;
23920 if (current_class_ptr)
23922 /* We don't clear this between NSDMIs. Is it already what we want? */
23923 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
23924 if (DECL_P (current_class_ptr)
23925 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
23926 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
23927 && cp_type_quals (type) == quals)
23928 return;
23931 this_parm = build_this_parm (NULL_TREE, ctype, quals);
23932 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
23933 current_class_ptr = NULL_TREE;
23934 current_class_ref
23935 = cp_build_fold_indirect_ref (this_parm);
23936 current_class_ptr = this_parm;
23939 /* Return true iff our current scope is a non-static data member
23940 initializer. */
23942 bool
23943 parsing_nsdmi (void)
23945 /* We recognize NSDMI context by the context-less 'this' pointer set up
23946 by the function above. */
23947 if (current_class_ptr
23948 && TREE_CODE (current_class_ptr) == PARM_DECL
23949 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
23950 return true;
23951 return false;
23954 /* True if we're parsing a function declarator. */
23956 bool
23957 parsing_function_declarator ()
23959 /* this_entity is NULL for a function parameter scope while parsing the
23960 declarator; it is set when parsing the body of the function. */
23961 return (current_binding_level->kind == sk_function_parms
23962 && !current_binding_level->this_entity);
23965 /* Parse a late-specified return type, if any. This is not a separate
23966 non-terminal, but part of a function declarator, which looks like
23968 -> trailing-type-specifier-seq abstract-declarator(opt)
23970 Returns the type indicated by the type-id.
23972 In addition to this, parse any queued up #pragma omp declare simd
23973 clauses, and #pragma acc routine clauses.
23975 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
23976 function. */
23978 static tree
23979 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
23980 tree& requires_clause)
23982 cp_token *token;
23983 tree type = NULL_TREE;
23984 bool declare_simd_p = (parser->omp_declare_simd
23985 && declarator
23986 && declarator->kind == cdk_id);
23988 bool oacc_routine_p = (parser->oacc_routine
23989 && declarator
23990 && declarator->kind == cdk_id);
23992 /* Peek at the next token. */
23993 token = cp_lexer_peek_token (parser->lexer);
23994 /* A late-specified return type is indicated by an initial '->'. */
23995 if (token->type != CPP_DEREF
23996 && token->keyword != RID_REQUIRES
23997 && !(token->type == CPP_NAME
23998 && token->u.value == ridpointers[RID_REQUIRES])
23999 && !(declare_simd_p || oacc_routine_p))
24000 return NULL_TREE;
24002 if (token->type == CPP_DEREF)
24004 /* Consume the ->. */
24005 cp_lexer_consume_token (parser->lexer);
24007 type = cp_parser_trailing_type_id (parser);
24010 /* Function declarations may be followed by a trailing
24011 requires-clause. */
24012 requires_clause = cp_parser_requires_clause_opt (parser, false);
24014 if (declare_simd_p)
24015 declarator->attributes
24016 = cp_parser_late_parsing_omp_declare_simd (parser,
24017 declarator->attributes);
24018 if (oacc_routine_p)
24019 declarator->attributes
24020 = cp_parser_late_parsing_oacc_routine (parser,
24021 declarator->attributes);
24023 return type;
24026 /* Parse a declarator-id.
24028 declarator-id:
24029 id-expression
24030 :: [opt] nested-name-specifier [opt] type-name
24032 In the `id-expression' case, the value returned is as for
24033 cp_parser_id_expression if the id-expression was an unqualified-id.
24034 If the id-expression was a qualified-id, then a SCOPE_REF is
24035 returned. The first operand is the scope (either a NAMESPACE_DECL
24036 or TREE_TYPE), but the second is still just a representation of an
24037 unqualified-id. */
24039 static tree
24040 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24042 tree id;
24043 /* The expression must be an id-expression. Assume that qualified
24044 names are the names of types so that:
24046 template <class T>
24047 int S<T>::R::i = 3;
24049 will work; we must treat `S<T>::R' as the name of a type.
24050 Similarly, assume that qualified names are templates, where
24051 required, so that:
24053 template <class T>
24054 int S<T>::R<T>::i = 3;
24056 will work, too. */
24057 id = cp_parser_id_expression (parser,
24058 /*template_keyword_p=*/false,
24059 /*check_dependency_p=*/false,
24060 /*template_p=*/NULL,
24061 /*declarator_p=*/true,
24062 optional_p);
24063 if (id && BASELINK_P (id))
24064 id = BASELINK_FUNCTIONS (id);
24065 return id;
24068 /* Parse a type-id.
24070 type-id:
24071 type-specifier-seq abstract-declarator [opt]
24073 The parser flags FLAGS is used to control type-specifier parsing.
24075 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24077 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24078 i.e. we've just seen "->".
24080 Returns the TYPE specified. */
24082 static tree
24083 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24084 bool is_template_arg, bool is_trailing_return,
24085 location_t *type_location)
24087 cp_decl_specifier_seq type_specifier_seq;
24088 cp_declarator *abstract_declarator;
24090 /* Parse the type-specifier-seq. */
24091 cp_parser_type_specifier_seq (parser, flags,
24092 /*is_declaration=*/false,
24093 is_trailing_return,
24094 &type_specifier_seq);
24095 if (type_location)
24096 *type_location = type_specifier_seq.locations[ds_type_spec];
24098 if (is_template_arg && type_specifier_seq.type
24099 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24100 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24101 /* A bare template name as a template argument is a template template
24102 argument, not a placeholder, so fail parsing it as a type argument. */
24104 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24105 cp_parser_simulate_error (parser);
24106 return error_mark_node;
24108 if (type_specifier_seq.type == error_mark_node)
24109 return error_mark_node;
24111 /* There might or might not be an abstract declarator. */
24112 cp_parser_parse_tentatively (parser);
24113 /* Look for the declarator. */
24114 abstract_declarator
24115 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24116 CP_PARSER_FLAGS_NONE, NULL,
24117 /*parenthesized_p=*/NULL,
24118 /*member_p=*/false,
24119 /*friend_p=*/false,
24120 /*static_p=*/false);
24121 /* Check to see if there really was a declarator. */
24122 if (!cp_parser_parse_definitely (parser))
24123 abstract_declarator = NULL;
24125 bool auto_typeid_ok = false;
24126 /* The concepts TS allows 'auto' as a type-id. */
24127 if (flag_concepts_ts)
24128 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24129 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24130 outside the template-argument-list context here only for the sake of
24131 diagnostic: grokdeclarator then can emit a better error message for
24132 e.g. using T = auto. */
24133 else if (flag_concepts)
24134 auto_typeid_ok = (!parser->in_type_id_in_expr_p
24135 && !parser->in_template_argument_list_p);
24137 if (type_specifier_seq.type
24138 && !auto_typeid_ok
24139 /* None of the valid uses of 'auto' in C++14 involve the type-id
24140 nonterminal, but it is valid in a trailing-return-type. */
24141 && !(cxx_dialect >= cxx14 && is_trailing_return))
24142 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24144 /* A type-id with type 'auto' is only ok if the abstract declarator
24145 is a function declarator with a late-specified return type.
24147 A type-id with 'auto' is also valid in a trailing-return-type
24148 in a compound-requirement. */
24149 if (abstract_declarator
24150 && abstract_declarator->kind == cdk_function
24151 && abstract_declarator->u.function.late_return_type)
24152 /* OK */;
24153 else if (parser->in_result_type_constraint_p)
24154 /* OK */;
24155 else
24157 if (!cp_parser_simulate_error (parser))
24159 location_t loc = type_specifier_seq.locations[ds_type_spec];
24160 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24162 error_at (loc, "missing template arguments after %qT",
24163 auto_node);
24164 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24165 tmpl);
24167 else if (parser->in_template_argument_list_p)
24168 error_at (loc, "%qT not permitted in template argument",
24169 auto_node);
24170 else
24171 error_at (loc, "invalid use of %qT", auto_node);
24173 return error_mark_node;
24177 return groktypename (&type_specifier_seq, abstract_declarator,
24178 is_template_arg);
24181 /* Wrapper for cp_parser_type_id_1. */
24183 static tree
24184 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24185 location_t *type_location)
24187 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24190 /* Wrapper for cp_parser_type_id_1. */
24192 static tree
24193 cp_parser_template_type_arg (cp_parser *parser)
24195 tree r;
24196 const char *saved_message = parser->type_definition_forbidden_message;
24197 parser->type_definition_forbidden_message
24198 = G_("types may not be defined in template arguments");
24199 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24200 parser->type_definition_forbidden_message = saved_message;
24201 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
24203 error ("invalid use of %<auto%> in template argument");
24204 r = error_mark_node;
24206 return r;
24209 /* Wrapper for cp_parser_type_id_1. */
24211 static tree
24212 cp_parser_trailing_type_id (cp_parser *parser)
24214 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24215 false, true, NULL);
24218 /* Parse a type-specifier-seq.
24220 type-specifier-seq:
24221 type-specifier type-specifier-seq [opt]
24223 GNU extension:
24225 type-specifier-seq:
24226 attributes type-specifier-seq [opt]
24228 The parser flags FLAGS is used to control type-specifier parsing.
24230 If IS_DECLARATION is true, we are at the start of a "condition" or
24231 exception-declaration, so we might be followed by a declarator-id.
24233 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24234 i.e. we've just seen "->".
24236 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24238 static void
24239 cp_parser_type_specifier_seq (cp_parser* parser,
24240 cp_parser_flags flags,
24241 bool is_declaration,
24242 bool is_trailing_return,
24243 cp_decl_specifier_seq *type_specifier_seq)
24245 bool seen_type_specifier = false;
24246 cp_token *start_token = NULL;
24248 /* Clear the TYPE_SPECIFIER_SEQ. */
24249 clear_decl_specs (type_specifier_seq);
24251 flags |= CP_PARSER_FLAGS_OPTIONAL;
24252 /* In the context of a trailing return type, enum E { } is an
24253 elaborated-type-specifier followed by a function-body, not an
24254 enum-specifier. */
24255 if (is_trailing_return)
24256 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24258 /* Parse the type-specifiers and attributes. */
24259 while (true)
24261 tree type_specifier;
24262 bool is_cv_qualifier;
24264 /* Check for attributes first. */
24265 if (cp_next_tokens_can_be_attribute_p (parser))
24267 /* GNU attributes at the end of a declaration apply to the
24268 declaration as a whole, not to the trailing return type. So look
24269 ahead to see if these attributes are at the end. */
24270 if (seen_type_specifier && is_trailing_return
24271 && cp_next_tokens_can_be_gnu_attribute_p (parser))
24273 size_t n = cp_parser_skip_attributes_opt (parser, 1);
24274 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24275 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24276 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24277 break;
24279 type_specifier_seq->attributes
24280 = attr_chainon (type_specifier_seq->attributes,
24281 cp_parser_attributes_opt (parser));
24282 continue;
24285 /* record the token of the beginning of the type specifier seq,
24286 for error reporting purposes*/
24287 if (!start_token)
24288 start_token = cp_lexer_peek_token (parser->lexer);
24290 /* Look for the type-specifier. */
24291 type_specifier = cp_parser_type_specifier (parser,
24292 flags,
24293 type_specifier_seq,
24294 /*is_declaration=*/false,
24295 NULL,
24296 &is_cv_qualifier);
24297 if (!type_specifier)
24299 /* If the first type-specifier could not be found, this is not a
24300 type-specifier-seq at all. */
24301 if (!seen_type_specifier)
24303 /* Set in_declarator_p to avoid skipping to the semicolon. */
24304 int in_decl = parser->in_declarator_p;
24305 parser->in_declarator_p = true;
24307 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24308 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24309 cp_parser_error (parser, "expected type-specifier");
24311 parser->in_declarator_p = in_decl;
24313 type_specifier_seq->type = error_mark_node;
24314 return;
24316 /* If subsequent type-specifiers could not be found, the
24317 type-specifier-seq is complete. */
24318 break;
24321 seen_type_specifier = true;
24322 /* The standard says that a condition can be:
24324 type-specifier-seq declarator = assignment-expression
24326 However, given:
24328 struct S {};
24329 if (int S = ...)
24331 we should treat the "S" as a declarator, not as a
24332 type-specifier. The standard doesn't say that explicitly for
24333 type-specifier-seq, but it does say that for
24334 decl-specifier-seq in an ordinary declaration. Perhaps it
24335 would be clearer just to allow a decl-specifier-seq here, and
24336 then add a semantic restriction that if any decl-specifiers
24337 that are not type-specifiers appear, the program is invalid. */
24338 if (is_declaration && !is_cv_qualifier)
24339 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24343 /* Return whether the function currently being declared has an associated
24344 template parameter list. */
24346 static bool
24347 function_being_declared_is_template_p (cp_parser* parser)
24349 if (!current_template_parms || processing_template_parmlist)
24350 return false;
24352 if (parser->implicit_template_scope)
24353 return true;
24355 if (at_class_scope_p ()
24356 && TYPE_BEING_DEFINED (current_class_type))
24357 return parser->num_template_parameter_lists != 0;
24359 return ((int) parser->num_template_parameter_lists > template_class_depth
24360 (current_class_type));
24363 /* Parse a parameter-declaration-clause.
24365 parameter-declaration-clause:
24366 parameter-declaration-list [opt] ... [opt]
24367 parameter-declaration-list , ...
24369 The parser flags FLAGS is used to control type-specifier parsing.
24371 Returns a representation for the parameter declarations. A return
24372 value of NULL indicates a parameter-declaration-clause consisting
24373 only of an ellipsis. */
24375 static tree
24376 cp_parser_parameter_declaration_clause (cp_parser* parser,
24377 cp_parser_flags flags)
24379 tree parameters;
24380 cp_token *token;
24381 bool ellipsis_p;
24383 auto cleanup = make_temp_override
24384 (parser->auto_is_implicit_function_template_parm_p);
24386 if (!processing_specialization
24387 && !processing_template_parmlist
24388 && !processing_explicit_instantiation
24389 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24390 actual function or a random abstract declarator. */
24391 && parser->default_arg_ok_p)
24392 if (!current_function_decl
24393 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24394 parser->auto_is_implicit_function_template_parm_p = true;
24396 /* Peek at the next token. */
24397 token = cp_lexer_peek_token (parser->lexer);
24398 /* Check for trivial parameter-declaration-clauses. */
24399 if (token->type == CPP_ELLIPSIS)
24401 /* Consume the `...' token. */
24402 cp_lexer_consume_token (parser->lexer);
24403 return NULL_TREE;
24405 else if (token->type == CPP_CLOSE_PAREN)
24406 /* There are no parameters. */
24407 return void_list_node;
24408 /* Check for `(void)', too, which is a special case. */
24409 else if (token->keyword == RID_VOID
24410 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24411 == CPP_CLOSE_PAREN))
24413 /* Consume the `void' token. */
24414 cp_lexer_consume_token (parser->lexer);
24415 /* There are no parameters. */
24416 return explicit_void_list_node;
24419 /* Parse the parameter-declaration-list. */
24420 parameters = cp_parser_parameter_declaration_list (parser, flags);
24421 /* If a parse error occurred while parsing the
24422 parameter-declaration-list, then the entire
24423 parameter-declaration-clause is erroneous. */
24424 if (parameters == error_mark_node)
24425 return NULL_TREE;
24427 /* Peek at the next token. */
24428 token = cp_lexer_peek_token (parser->lexer);
24429 /* If it's a `,', the clause should terminate with an ellipsis. */
24430 if (token->type == CPP_COMMA)
24432 /* Consume the `,'. */
24433 cp_lexer_consume_token (parser->lexer);
24434 /* Expect an ellipsis. */
24435 ellipsis_p
24436 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24438 /* It might also be `...' if the optional trailing `,' was
24439 omitted. */
24440 else if (token->type == CPP_ELLIPSIS)
24442 /* Consume the `...' token. */
24443 cp_lexer_consume_token (parser->lexer);
24444 /* And remember that we saw it. */
24445 ellipsis_p = true;
24447 else
24448 ellipsis_p = false;
24450 /* Finish the parameter list. */
24451 if (!ellipsis_p)
24452 parameters = chainon (parameters, void_list_node);
24454 return parameters;
24457 /* Parse a parameter-declaration-list.
24459 parameter-declaration-list:
24460 parameter-declaration
24461 parameter-declaration-list , parameter-declaration
24463 The parser flags FLAGS is used to control type-specifier parsing.
24465 Returns a representation of the parameter-declaration-list, as for
24466 cp_parser_parameter_declaration_clause. However, the
24467 `void_list_node' is never appended to the list. */
24469 static tree
24470 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
24472 tree parameters = NULL_TREE;
24473 tree *tail = &parameters;
24474 bool saved_in_unbraced_linkage_specification_p;
24475 int index = 0;
24477 /* The special considerations that apply to a function within an
24478 unbraced linkage specifications do not apply to the parameters
24479 to the function. */
24480 saved_in_unbraced_linkage_specification_p
24481 = parser->in_unbraced_linkage_specification_p;
24482 parser->in_unbraced_linkage_specification_p = false;
24484 /* Look for more parameters. */
24485 while (true)
24487 cp_parameter_declarator *parameter;
24488 tree decl = error_mark_node;
24489 bool parenthesized_p = false;
24491 /* Parse the parameter. */
24492 parameter
24493 = cp_parser_parameter_declaration (parser, flags,
24494 /*template_parm_p=*/false,
24495 &parenthesized_p);
24497 /* We don't know yet if the enclosing context is unavailable or deprecated,
24498 so wait and deal with it in grokparms if appropriate. */
24499 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
24501 if (parameter && !cp_parser_error_occurred (parser))
24503 decl = grokdeclarator (parameter->declarator,
24504 &parameter->decl_specifiers,
24505 PARM,
24506 parameter->default_argument != NULL_TREE,
24507 &parameter->decl_specifiers.attributes);
24508 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
24509 DECL_SOURCE_LOCATION (decl) = parameter->loc;
24512 deprecated_state = DEPRECATED_NORMAL;
24514 /* If a parse error occurred parsing the parameter declaration,
24515 then the entire parameter-declaration-list is erroneous. */
24516 if (decl == error_mark_node)
24518 parameters = error_mark_node;
24519 break;
24522 if (parameter->decl_specifiers.attributes)
24523 cplus_decl_attributes (&decl,
24524 parameter->decl_specifiers.attributes,
24526 if (DECL_NAME (decl))
24527 decl = pushdecl (decl);
24529 if (decl != error_mark_node)
24531 retrofit_lang_decl (decl);
24532 DECL_PARM_INDEX (decl) = ++index;
24533 DECL_PARM_LEVEL (decl) = function_parm_depth ();
24536 /* Add the new parameter to the list. */
24537 *tail = build_tree_list (parameter->default_argument, decl);
24538 tail = &TREE_CHAIN (*tail);
24540 /* If the parameters were parenthesized, it's the case of
24541 T foo(X(x)) which looks like a variable definition but
24542 is a function declaration. */
24543 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
24544 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
24546 /* Peek at the next token. */
24547 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
24548 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
24549 /* These are for Objective-C++ */
24550 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24551 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24552 /* The parameter-declaration-list is complete. */
24553 break;
24554 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24556 cp_token *token;
24558 /* Peek at the next token. */
24559 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24560 /* If it's an ellipsis, then the list is complete. */
24561 if (token->type == CPP_ELLIPSIS)
24562 break;
24563 /* Otherwise, there must be more parameters. Consume the
24564 `,'. */
24565 cp_lexer_consume_token (parser->lexer);
24566 /* When parsing something like:
24568 int i(float f, double d)
24570 we can tell after seeing the declaration for "f" that we
24571 are not looking at an initialization of a variable "i",
24572 but rather at the declaration of a function "i".
24574 Due to the fact that the parsing of template arguments
24575 (as specified to a template-id) requires backtracking we
24576 cannot use this technique when inside a template argument
24577 list. */
24578 if (!parser->in_template_argument_list_p
24579 && !parser->in_type_id_in_expr_p
24580 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24581 /* However, a parameter-declaration of the form
24582 "float(f)" (which is a valid declaration of a
24583 parameter "f") can also be interpreted as an
24584 expression (the conversion of "f" to "float"). */
24585 && !parenthesized_p)
24586 cp_parser_commit_to_tentative_parse (parser);
24588 else
24590 cp_parser_error (parser, "expected %<,%> or %<...%>");
24591 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24592 cp_parser_skip_to_closing_parenthesis (parser,
24593 /*recovering=*/true,
24594 /*or_comma=*/false,
24595 /*consume_paren=*/false);
24596 break;
24600 parser->in_unbraced_linkage_specification_p
24601 = saved_in_unbraced_linkage_specification_p;
24603 /* Reset implicit_template_scope if we are about to leave the function
24604 parameter list that introduced it. Note that for out-of-line member
24605 definitions, there will be one or more class scopes before we get to
24606 the template parameter scope. */
24608 if (cp_binding_level *its = parser->implicit_template_scope)
24609 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
24611 while (maybe_its->kind == sk_class)
24612 maybe_its = maybe_its->level_chain;
24613 if (maybe_its == its)
24615 parser->implicit_template_parms = 0;
24616 parser->implicit_template_scope = 0;
24620 return parameters;
24623 /* Parse a parameter declaration.
24625 parameter-declaration:
24626 decl-specifier-seq ... [opt] declarator
24627 decl-specifier-seq declarator = assignment-expression
24628 decl-specifier-seq ... [opt] abstract-declarator [opt]
24629 decl-specifier-seq abstract-declarator [opt] = assignment-expression
24631 The parser flags FLAGS is used to control type-specifier parsing.
24633 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
24634 declares a template parameter. (In that case, a non-nested `>'
24635 token encountered during the parsing of the assignment-expression
24636 is not interpreted as a greater-than operator.)
24638 Returns a representation of the parameter, or NULL if an error
24639 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
24640 true iff the declarator is of the form "(p)". */
24642 static cp_parameter_declarator *
24643 cp_parser_parameter_declaration (cp_parser *parser,
24644 cp_parser_flags flags,
24645 bool template_parm_p,
24646 bool *parenthesized_p)
24648 int declares_class_or_enum;
24649 cp_decl_specifier_seq decl_specifiers;
24650 cp_declarator *declarator;
24651 tree default_argument;
24652 cp_token *token = NULL, *declarator_token_start = NULL;
24653 const char *saved_message;
24654 bool template_parameter_pack_p = false;
24656 /* In a template parameter, `>' is not an operator.
24658 [temp.param]
24660 When parsing a default template-argument for a non-type
24661 template-parameter, the first non-nested `>' is taken as the end
24662 of the template parameter-list rather than a greater-than
24663 operator. */
24665 /* Type definitions may not appear in parameter types. */
24666 saved_message = parser->type_definition_forbidden_message;
24667 parser->type_definition_forbidden_message
24668 = G_("types may not be defined in parameter types");
24670 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
24671 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
24672 (current_template_parms)) : 0);
24674 /* Parse the declaration-specifiers. */
24675 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24676 cp_parser_decl_specifier_seq (parser,
24677 flags,
24678 &decl_specifiers,
24679 &declares_class_or_enum);
24681 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
24682 type-constraint opt auto can be used as a decl-specifier of the
24683 decl-specifier-seq of a parameter-declaration of a function declaration
24684 or lambda-expression..." but we must not synthesize an implicit template
24685 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
24686 we want to synthesize only the first auto. */
24687 auto cleanup = make_temp_override
24688 (parser->auto_is_implicit_function_template_parm_p, false);
24690 /* Complain about missing 'typename' or other invalid type names. */
24691 if (!decl_specifiers.any_type_specifiers_p
24692 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24693 decl_specifiers.type = error_mark_node;
24695 /* If an error occurred, there's no reason to attempt to parse the
24696 rest of the declaration. */
24697 if (cp_parser_error_occurred (parser))
24699 parser->type_definition_forbidden_message = saved_message;
24700 return NULL;
24703 /* Peek at the next token. */
24704 token = cp_lexer_peek_token (parser->lexer);
24706 /* If the next token is a `)', `,', `=', `>', or `...', then there
24707 is no declarator. However, when variadic templates are enabled,
24708 there may be a declarator following `...'. */
24709 if (token->type == CPP_CLOSE_PAREN
24710 || token->type == CPP_COMMA
24711 || token->type == CPP_EQ
24712 || token->type == CPP_GREATER)
24714 declarator = NULL;
24715 if (parenthesized_p)
24716 *parenthesized_p = false;
24718 /* Otherwise, there should be a declarator. */
24719 else
24721 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
24722 parser->default_arg_ok_p = false;
24724 /* After seeing a decl-specifier-seq, if the next token is not a
24725 "(" or "{", there is no possibility that the code is a valid
24726 expression. Therefore, if parsing tentatively, we commit at
24727 this point. */
24728 if (!parser->in_template_argument_list_p
24729 /* In an expression context, having seen:
24731 (int((char ...
24733 we cannot be sure whether we are looking at a
24734 function-type (taking a "char" as a parameter) or a cast
24735 of some object of type "char" to "int". */
24736 && !parser->in_type_id_in_expr_p
24737 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24738 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
24740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24742 if (decl_specifiers.type
24743 && template_placeholder_p (decl_specifiers.type))
24744 /* This is a CTAD expression, not a parameter declaration. */
24745 cp_parser_simulate_error (parser);
24747 else
24748 cp_parser_commit_to_tentative_parse (parser);
24750 /* Parse the declarator. */
24751 declarator_token_start = token;
24752 declarator = cp_parser_declarator (parser,
24753 CP_PARSER_DECLARATOR_EITHER,
24754 CP_PARSER_FLAGS_NONE,
24755 /*ctor_dtor_or_conv_p=*/NULL,
24756 parenthesized_p,
24757 /*member_p=*/false,
24758 /*friend_p=*/false,
24759 /*static_p=*/false);
24760 parser->default_arg_ok_p = saved_default_arg_ok_p;
24761 /* After the declarator, allow more attributes. */
24762 decl_specifiers.attributes
24763 = attr_chainon (decl_specifiers.attributes,
24764 cp_parser_attributes_opt (parser));
24766 /* If the declarator is a template parameter pack, remember that and
24767 clear the flag in the declarator itself so we don't get errors
24768 from grokdeclarator. */
24769 if (template_parm_p && declarator && declarator->parameter_pack_p)
24771 declarator->parameter_pack_p = false;
24772 template_parameter_pack_p = true;
24776 /* If the next token is an ellipsis, and we have not seen a declarator
24777 name, and if either the type of the declarator contains parameter
24778 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
24779 for, eg, abbreviated integral type names), then we actually have a
24780 parameter pack expansion expression. Otherwise, leave the ellipsis
24781 for a C-style variadic function. */
24782 token = cp_lexer_peek_token (parser->lexer);
24784 /* If a function parameter pack was specified and an implicit template
24785 parameter was introduced during cp_parser_parameter_declaration,
24786 change any implicit parameters introduced into packs. */
24787 if (parser->implicit_template_parms
24788 && ((token->type == CPP_ELLIPSIS
24789 && declarator_can_be_parameter_pack (declarator))
24790 || (declarator && declarator->parameter_pack_p)))
24792 int latest_template_parm_idx = TREE_VEC_LENGTH
24793 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
24795 if (latest_template_parm_idx != template_parm_idx)
24796 decl_specifiers.type = convert_generic_types_to_packs
24797 (decl_specifiers.type,
24798 template_parm_idx, latest_template_parm_idx);
24801 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24803 tree type = decl_specifiers.type;
24805 if (type && DECL_P (type))
24806 type = TREE_TYPE (type);
24808 if (((type
24809 && TREE_CODE (type) != TYPE_PACK_EXPANSION
24810 && (template_parm_p || uses_parameter_packs (type)))
24811 || (!type && template_parm_p))
24812 && declarator_can_be_parameter_pack (declarator))
24814 /* Consume the `...'. */
24815 cp_lexer_consume_token (parser->lexer);
24816 maybe_warn_variadic_templates ();
24818 /* Build a pack expansion type */
24819 if (template_parm_p)
24820 template_parameter_pack_p = true;
24821 else if (declarator)
24822 declarator->parameter_pack_p = true;
24823 else
24824 decl_specifiers.type = make_pack_expansion (type);
24828 /* The restriction on defining new types applies only to the type
24829 of the parameter, not to the default argument. */
24830 parser->type_definition_forbidden_message = saved_message;
24832 /* If the next token is `=', then process a default argument. */
24833 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24835 tree type = decl_specifiers.type;
24836 token = cp_lexer_peek_token (parser->lexer);
24837 if (declarator)
24838 declarator->init_loc = token->location;
24839 /* If we are defining a class, then the tokens that make up the
24840 default argument must be saved and processed later. */
24841 if (!template_parm_p && at_class_scope_p ()
24842 && TYPE_BEING_DEFINED (current_class_type)
24843 && !LAMBDA_TYPE_P (current_class_type))
24844 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
24846 /* A constrained-type-specifier may declare a type
24847 template-parameter. */
24848 else if (declares_constrained_type_template_parameter (type))
24849 default_argument
24850 = cp_parser_default_type_template_argument (parser);
24852 /* A constrained-type-specifier may declare a
24853 template-template-parameter. */
24854 else if (declares_constrained_template_template_parameter (type))
24855 default_argument
24856 = cp_parser_default_template_template_argument (parser);
24858 /* Outside of a class definition, we can just parse the
24859 assignment-expression. */
24860 else
24861 default_argument
24862 = cp_parser_default_argument (parser, template_parm_p);
24864 if (!parser->default_arg_ok_p)
24866 permerror (token->location,
24867 "default arguments are only "
24868 "permitted for function parameters");
24870 else if ((declarator && declarator->parameter_pack_p)
24871 || template_parameter_pack_p
24872 || (decl_specifiers.type
24873 && PACK_EXPANSION_P (decl_specifiers.type)))
24875 /* Find the name of the parameter pack. */
24876 cp_declarator *id_declarator = declarator;
24877 while (id_declarator && id_declarator->kind != cdk_id)
24878 id_declarator = id_declarator->declarator;
24880 if (id_declarator && id_declarator->kind == cdk_id)
24881 error_at (declarator_token_start->location,
24882 template_parm_p
24883 ? G_("template parameter pack %qD "
24884 "cannot have a default argument")
24885 : G_("parameter pack %qD cannot have "
24886 "a default argument"),
24887 id_declarator->u.id.unqualified_name);
24888 else
24889 error_at (declarator_token_start->location,
24890 template_parm_p
24891 ? G_("template parameter pack cannot have "
24892 "a default argument")
24893 : G_("parameter pack cannot have a "
24894 "default argument"));
24896 default_argument = NULL_TREE;
24899 else
24900 default_argument = NULL_TREE;
24902 if (default_argument)
24903 STRIP_ANY_LOCATION_WRAPPER (default_argument);
24905 /* Generate a location for the parameter, ranging from the start of the
24906 initial token to the end of the final token (using input_location for
24907 the latter, set up by cp_lexer_set_source_position_from_token when
24908 consuming tokens).
24910 If we have a identifier, then use it for the caret location, e.g.
24912 extern int callee (int one, int (*two)(int, int), float three);
24913 ~~~~~~^~~~~~~~~~~~~~
24915 otherwise, reuse the start location for the caret location e.g.:
24917 extern int callee (int one, int (*)(int, int), float three);
24918 ^~~~~~~~~~~~~~~~~
24921 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
24922 ? declarator->id_loc
24923 : decl_spec_token_start->location);
24924 location_t param_loc = make_location (caret_loc,
24925 decl_spec_token_start->location,
24926 input_location);
24928 return make_parameter_declarator (&decl_specifiers,
24929 declarator,
24930 default_argument,
24931 param_loc,
24932 template_parameter_pack_p);
24935 /* Parse a default argument and return it.
24937 TEMPLATE_PARM_P is true if this is a default argument for a
24938 non-type template parameter. */
24939 static tree
24940 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
24942 tree default_argument = NULL_TREE;
24943 bool saved_greater_than_is_operator_p;
24944 unsigned char saved_local_variables_forbidden_p;
24945 bool non_constant_p, is_direct_init;
24947 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
24948 set correctly. */
24949 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
24950 parser->greater_than_is_operator_p = !template_parm_p;
24951 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
24952 auto ord = make_temp_override (parser->oacc_routine, NULL);
24953 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
24955 /* Local variable names (and the `this' keyword) may not
24956 appear in a default argument. */
24957 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24958 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
24959 /* Parse the assignment-expression. */
24960 if (template_parm_p)
24961 push_deferring_access_checks (dk_no_deferred);
24962 tree saved_class_ptr = NULL_TREE;
24963 tree saved_class_ref = NULL_TREE;
24964 /* The "this" pointer is not valid in a default argument. */
24965 if (cfun)
24967 saved_class_ptr = current_class_ptr;
24968 cp_function_chain->x_current_class_ptr = NULL_TREE;
24969 saved_class_ref = current_class_ref;
24970 cp_function_chain->x_current_class_ref = NULL_TREE;
24972 default_argument
24973 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
24974 /* Restore the "this" pointer. */
24975 if (cfun)
24977 cp_function_chain->x_current_class_ptr = saved_class_ptr;
24978 cp_function_chain->x_current_class_ref = saved_class_ref;
24980 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
24981 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24982 if (template_parm_p)
24983 pop_deferring_access_checks ();
24984 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
24985 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24987 return default_argument;
24990 /* Parse a function-body.
24992 function-body:
24993 compound_statement */
24995 static void
24996 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
24998 cp_parser_compound_statement (parser, NULL, (in_function_try_block
24999 ? BCS_TRY_BLOCK : BCS_NORMAL),
25000 true);
25003 /* Parse a ctor-initializer-opt followed by a function-body. Return
25004 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25005 is true we are parsing a function-try-block. */
25007 static void
25008 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25009 bool in_function_try_block)
25011 tree body, list;
25012 const bool check_body_p
25013 = (DECL_CONSTRUCTOR_P (current_function_decl)
25014 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25015 tree last = NULL;
25017 if (in_function_try_block
25018 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25019 && cxx_dialect < cxx20)
25021 if (DECL_CONSTRUCTOR_P (current_function_decl))
25022 pedwarn (input_location, OPT_Wc__20_extensions,
25023 "function-try-block body of %<constexpr%> constructor only "
25024 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25025 else
25026 pedwarn (input_location, OPT_Wc__20_extensions,
25027 "function-try-block body of %<constexpr%> function only "
25028 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25031 /* Begin the function body. */
25032 body = begin_function_body ();
25033 /* Parse the optional ctor-initializer. */
25034 cp_parser_ctor_initializer_opt (parser);
25036 /* If we're parsing a constexpr constructor definition, we need
25037 to check that the constructor body is indeed empty. However,
25038 before we get to cp_parser_function_body lot of junk has been
25039 generated, so we can't just check that we have an empty block.
25040 Rather we take a snapshot of the outermost block, and check whether
25041 cp_parser_function_body changed its state. */
25042 if (check_body_p)
25044 list = cur_stmt_list;
25045 if (STATEMENT_LIST_TAIL (list))
25046 last = STATEMENT_LIST_TAIL (list)->stmt;
25048 /* Parse the function-body. */
25049 cp_parser_function_body (parser, in_function_try_block);
25050 if (check_body_p)
25051 check_constexpr_ctor_body (last, list, /*complain=*/true);
25052 /* Finish the function body. */
25053 finish_function_body (body);
25056 /* Parse an initializer.
25058 initializer:
25059 = initializer-clause
25060 ( expression-list )
25062 Returns an expression representing the initializer. If no
25063 initializer is present, NULL_TREE is returned.
25065 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25066 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25067 set to TRUE if there is no initializer present. If there is an
25068 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25069 is set to true; otherwise it is set to false. */
25071 static tree
25072 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
25073 bool* non_constant_p, bool subexpression_p)
25075 cp_token *token;
25076 tree init;
25078 /* Peek at the next token. */
25079 token = cp_lexer_peek_token (parser->lexer);
25081 /* Let our caller know whether or not this initializer was
25082 parenthesized. */
25083 *is_direct_init = (token->type != CPP_EQ);
25084 /* Assume that the initializer is constant. */
25085 *non_constant_p = false;
25087 if (token->type == CPP_EQ)
25089 /* Consume the `='. */
25090 cp_lexer_consume_token (parser->lexer);
25091 /* Parse the initializer-clause. */
25092 init = cp_parser_initializer_clause (parser, non_constant_p);
25094 else if (token->type == CPP_OPEN_PAREN)
25096 vec<tree, va_gc> *vec;
25097 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25098 /*cast_p=*/false,
25099 /*allow_expansion_p=*/true,
25100 non_constant_p);
25101 if (vec == NULL)
25102 return error_mark_node;
25103 init = build_tree_list_vec (vec);
25104 release_tree_vector (vec);
25106 else if (token->type == CPP_OPEN_BRACE)
25108 cp_lexer_set_source_position (parser->lexer);
25109 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25110 init = cp_parser_braced_list (parser, non_constant_p);
25111 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25113 else
25115 /* Anything else is an error. */
25116 cp_parser_error (parser, "expected initializer");
25117 init = error_mark_node;
25120 if (!subexpression_p && check_for_bare_parameter_packs (init))
25121 init = error_mark_node;
25123 return init;
25126 /* Parse an initializer-clause.
25128 initializer-clause:
25129 assignment-expression
25130 braced-init-list
25132 Returns an expression representing the initializer.
25134 If the `assignment-expression' production is used the value
25135 returned is simply a representation for the expression.
25137 Otherwise, calls cp_parser_braced_list. */
25139 static cp_expr
25140 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25142 cp_expr initializer;
25144 /* Assume the expression is constant. */
25145 *non_constant_p = false;
25147 /* If it is not a `{', then we are looking at an
25148 assignment-expression. */
25149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25151 initializer
25152 = cp_parser_constant_expression (parser,
25153 /*allow_non_constant_p=*/2,
25154 non_constant_p);
25156 else
25157 initializer = cp_parser_braced_list (parser, non_constant_p);
25159 return initializer;
25162 /* Parse a brace-enclosed initializer list.
25164 braced-init-list:
25165 { initializer-list , [opt] }
25166 { designated-initializer-list , [opt] }
25169 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25170 the elements of the initializer-list (or NULL, if the last
25171 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25172 NULL_TREE. There is no way to detect whether or not the optional
25173 trailing `,' was provided. NON_CONSTANT_P is as for
25174 cp_parser_initializer. */
25176 static cp_expr
25177 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
25179 tree initializer;
25180 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25182 /* Consume the `{' token. */
25183 matching_braces braces;
25184 braces.require_open (parser);
25185 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25186 initializer = make_node (CONSTRUCTOR);
25187 /* If it's not a `}', then there is a non-trivial initializer. */
25188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25190 bool designated;
25191 /* Parse the initializer list. */
25192 CONSTRUCTOR_ELTS (initializer)
25193 = cp_parser_initializer_list (parser, non_constant_p, &designated);
25194 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25195 /* A trailing `,' token is allowed. */
25196 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25197 cp_lexer_consume_token (parser->lexer);
25199 else
25200 *non_constant_p = false;
25201 /* Now, there should be a trailing `}'. */
25202 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25203 braces.require_close (parser);
25204 TREE_TYPE (initializer) = init_list_type_node;
25206 cp_expr result (initializer);
25207 /* Build a location of the form:
25208 { ... }
25209 ^~~~~~~
25210 with caret==start at the open brace, finish at the close brace. */
25211 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25212 result.set_location (combined_loc);
25213 return result;
25216 /* Consume tokens up to, and including, the next non-nested closing `]'.
25217 Returns true iff we found a closing `]'. */
25219 static bool
25220 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25222 unsigned square_depth = 0;
25224 while (true)
25226 cp_token * token = cp_lexer_peek_token (parser->lexer);
25228 switch (token->type)
25230 case CPP_PRAGMA_EOL:
25231 if (!parser->lexer->in_pragma)
25232 break;
25233 /* FALLTHRU */
25234 case CPP_EOF:
25235 /* If we've run out of tokens, then there is no closing `]'. */
25236 return false;
25238 case CPP_OPEN_SQUARE:
25239 ++square_depth;
25240 break;
25242 case CPP_CLOSE_SQUARE:
25243 if (!square_depth--)
25245 cp_lexer_consume_token (parser->lexer);
25246 return true;
25248 break;
25250 default:
25251 break;
25254 /* Consume the token. */
25255 cp_lexer_consume_token (parser->lexer);
25259 /* Return true if we are looking at an array-designator, false otherwise. */
25261 static bool
25262 cp_parser_array_designator_p (cp_parser *parser)
25264 /* Consume the `['. */
25265 cp_lexer_consume_token (parser->lexer);
25267 cp_lexer_save_tokens (parser->lexer);
25269 /* Skip tokens until the next token is a closing square bracket.
25270 If we find the closing `]', and the next token is a `=', then
25271 we are looking at an array designator. */
25272 bool array_designator_p
25273 = (cp_parser_skip_to_closing_square_bracket (parser)
25274 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25276 /* Roll back the tokens we skipped. */
25277 cp_lexer_rollback_tokens (parser->lexer);
25279 return array_designator_p;
25282 /* Parse an initializer-list.
25284 initializer-list:
25285 initializer-clause ... [opt]
25286 initializer-list , initializer-clause ... [opt]
25288 C++20 Extension:
25290 designated-initializer-list:
25291 designated-initializer-clause
25292 designated-initializer-list , designated-initializer-clause
25294 designated-initializer-clause:
25295 designator brace-or-equal-initializer
25297 designator:
25298 . identifier
25300 GNU Extension:
25302 initializer-list:
25303 designation initializer-clause ...[opt]
25304 initializer-list , designation initializer-clause ...[opt]
25306 designation:
25307 . identifier =
25308 identifier :
25309 [ constant-expression ] =
25311 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25312 for the initializer. If the INDEX of the elt is non-NULL, it is the
25313 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25314 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25315 are any designators. */
25317 static vec<constructor_elt, va_gc> *
25318 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25319 bool *designated)
25321 vec<constructor_elt, va_gc> *v = NULL;
25322 bool first_p = true;
25323 tree first_designator = NULL_TREE;
25325 /* Assume all of the expressions are constant. */
25326 *non_constant_p = false;
25328 unsigned nelts = 0;
25329 int suppress = suppress_location_wrappers;
25331 /* Parse the rest of the list. */
25332 while (true)
25334 cp_token *token;
25335 tree designator;
25336 tree initializer;
25337 bool clause_non_constant_p;
25338 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25340 /* Handle the C++20 syntax, '. id ='. */
25341 if ((cxx_dialect >= cxx20
25342 || cp_parser_allow_gnu_extensions_p (parser))
25343 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25344 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25345 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25346 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25347 == CPP_OPEN_BRACE)))
25349 if (pedantic && cxx_dialect < cxx20)
25350 pedwarn (loc, OPT_Wc__20_extensions,
25351 "C++ designated initializers only available with "
25352 "%<-std=c++20%> or %<-std=gnu++20%>");
25353 /* Consume the `.'. */
25354 cp_lexer_consume_token (parser->lexer);
25355 /* Consume the identifier. */
25356 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25357 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25358 /* Consume the `='. */
25359 cp_lexer_consume_token (parser->lexer);
25361 /* Also, if the next token is an identifier and the following one is a
25362 colon, we are looking at the GNU designated-initializer
25363 syntax. */
25364 else if (cp_parser_allow_gnu_extensions_p (parser)
25365 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25366 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25367 == CPP_COLON))
25369 /* Warn the user that they are using an extension. */
25370 pedwarn (loc, OPT_Wpedantic,
25371 "ISO C++ does not allow GNU designated initializers");
25372 /* Consume the identifier. */
25373 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25374 /* Consume the `:'. */
25375 cp_lexer_consume_token (parser->lexer);
25377 /* Also handle C99 array designators, '[ const ] ='. */
25378 else if (cp_parser_allow_gnu_extensions_p (parser)
25379 && !c_dialect_objc ()
25380 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25382 /* In C++11, [ could start a lambda-introducer. */
25383 bool non_const = false;
25385 cp_parser_parse_tentatively (parser);
25387 if (!cp_parser_array_designator_p (parser))
25389 cp_parser_simulate_error (parser);
25390 designator = NULL_TREE;
25392 else
25394 designator = cp_parser_constant_expression (parser, true,
25395 &non_const);
25396 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25397 cp_parser_require (parser, CPP_EQ, RT_EQ);
25400 if (!cp_parser_parse_definitely (parser))
25401 designator = NULL_TREE;
25402 else if (non_const
25403 && (!require_potential_rvalue_constant_expression
25404 (designator)))
25405 designator = NULL_TREE;
25406 if (designator)
25407 /* Warn the user that they are using an extension. */
25408 pedwarn (loc, OPT_Wpedantic,
25409 "ISO C++ does not allow C99 designated initializers");
25411 else
25412 designator = NULL_TREE;
25414 if (first_p)
25416 first_designator = designator;
25417 first_p = false;
25419 else if (cxx_dialect >= cxx20
25420 && first_designator != error_mark_node
25421 && (!first_designator != !designator))
25423 error_at (loc, "either all initializer clauses should be designated "
25424 "or none of them should be");
25425 first_designator = error_mark_node;
25427 else if (cxx_dialect < cxx20 && !first_designator)
25428 first_designator = designator;
25430 /* Parse the initializer. */
25431 initializer = cp_parser_initializer_clause (parser,
25432 &clause_non_constant_p);
25433 /* If any clause is non-constant, so is the entire initializer. */
25434 if (clause_non_constant_p)
25435 *non_constant_p = true;
25437 /* If we have an ellipsis, this is an initializer pack
25438 expansion. */
25439 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25441 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25443 /* Consume the `...'. */
25444 cp_lexer_consume_token (parser->lexer);
25446 if (designator && cxx_dialect >= cxx20)
25447 error_at (loc,
25448 "%<...%> not allowed in designated initializer list");
25450 /* Turn the initializer into an initializer expansion. */
25451 initializer = make_pack_expansion (initializer);
25454 /* Add it to the vector. */
25455 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
25457 /* If the next token is not a comma, we have reached the end of
25458 the list. */
25459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25460 break;
25462 /* Peek at the next token. */
25463 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25464 /* If the next token is a `}', then we're still done. An
25465 initializer-clause can have a trailing `,' after the
25466 initializer-list and before the closing `}'. */
25467 if (token->type == CPP_CLOSE_BRACE)
25468 break;
25470 /* Suppress location wrappers in a long initializer to save memory
25471 (14179). The cutoff is chosen arbitrarily. */
25472 const unsigned loc_max = 256;
25473 unsigned incr = 1;
25474 if (TREE_CODE (initializer) == CONSTRUCTOR)
25475 /* Look one level down because it's easy. Looking deeper would require
25476 passing down a nelts pointer, and I don't think multi-level massive
25477 initializers are common enough to justify this. */
25478 incr = CONSTRUCTOR_NELTS (initializer);
25479 nelts += incr;
25480 if (nelts >= loc_max && (nelts - incr) < loc_max)
25481 ++suppress_location_wrappers;
25483 /* Consume the `,' token. */
25484 cp_lexer_consume_token (parser->lexer);
25487 /* The same identifier shall not appear in multiple designators
25488 of a designated-initializer-list. */
25489 if (first_designator)
25491 unsigned int i;
25492 tree designator, val;
25493 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25494 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25496 if (IDENTIFIER_MARKED (designator))
25498 error_at (cp_expr_loc_or_input_loc (val),
25499 "%<.%s%> designator used multiple times in "
25500 "the same initializer list",
25501 IDENTIFIER_POINTER (designator));
25502 (*v)[i].index = error_mark_node;
25504 else
25505 IDENTIFIER_MARKED (designator) = 1;
25507 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25508 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25509 IDENTIFIER_MARKED (designator) = 0;
25512 suppress_location_wrappers = suppress;
25514 *designated = first_designator != NULL_TREE;
25515 return v;
25518 /* Classes [gram.class] */
25520 /* Parse a class-name.
25522 class-name:
25523 identifier
25524 template-id
25526 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25527 to indicate that names looked up in dependent types should be
25528 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
25529 keyword has been used to indicate that the name that appears next
25530 is a template. TAG_TYPE indicates the explicit tag given before
25531 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
25532 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
25533 is the class being defined in a class-head. If ENUM_OK is TRUE,
25534 enum-names are also accepted.
25536 Returns the TYPE_DECL representing the class. */
25538 static tree
25539 cp_parser_class_name (cp_parser *parser,
25540 bool typename_keyword_p,
25541 bool template_keyword_p,
25542 enum tag_types tag_type,
25543 bool check_dependency_p,
25544 bool class_head_p,
25545 bool is_declaration,
25546 bool enum_ok)
25548 tree decl;
25549 tree identifier = NULL_TREE;
25551 /* All class-names start with an identifier. */
25552 cp_token *token = cp_lexer_peek_token (parser->lexer);
25553 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
25555 cp_parser_error (parser, "expected class-name");
25556 return error_mark_node;
25559 /* PARSER->SCOPE can be cleared when parsing the template-arguments
25560 to a template-id, so we save it here. Consider object scope too,
25561 so that make_typename_type below can use it (cp_parser_template_name
25562 considers object scope also). This may happen with code like
25564 p->template A<T>::a()
25566 where we first want to look up A<T>::a in the class of the object
25567 expression, as per [basic.lookup.classref]. */
25568 tree scope = parser->scope ? parser->scope : parser->context->object_type;
25569 /* This only checks parser->scope to avoid duplicate errors; if
25570 ->object_type is erroneous, go on to give a parse error. */
25571 if (parser->scope == error_mark_node)
25572 return error_mark_node;
25574 /* Any name names a type if we're following the `typename' keyword
25575 in a qualified name where the enclosing scope is type-dependent. */
25576 const bool typename_p = (typename_keyword_p
25577 && parser->scope
25578 && TYPE_P (parser->scope)
25579 && dependent_scope_p (parser->scope));
25580 /* Handle the common case (an identifier, but not a template-id)
25581 efficiently. */
25582 if (token->type == CPP_NAME
25583 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
25585 cp_token *identifier_token;
25586 bool ambiguous_p;
25588 /* Look for the identifier. */
25589 identifier_token = cp_lexer_peek_token (parser->lexer);
25590 ambiguous_p = identifier_token->error_reported;
25591 identifier = cp_parser_identifier (parser);
25592 /* If the next token isn't an identifier, we are certainly not
25593 looking at a class-name. */
25594 if (identifier == error_mark_node)
25595 decl = error_mark_node;
25596 /* If we know this is a type-name, there's no need to look it
25597 up. */
25598 else if (typename_p)
25599 decl = identifier;
25600 else
25602 tree ambiguous_decls;
25603 /* If we already know that this lookup is ambiguous, then
25604 we've already issued an error message; there's no reason
25605 to check again. */
25606 if (ambiguous_p)
25608 cp_parser_simulate_error (parser);
25609 return error_mark_node;
25611 /* If the next token is a `::', then the name must be a type
25612 name.
25614 [basic.lookup.qual]
25616 During the lookup for a name preceding the :: scope
25617 resolution operator, object, function, and enumerator
25618 names are ignored. */
25619 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25620 tag_type = scope_type;
25621 /* Look up the name. */
25622 decl = cp_parser_lookup_name (parser, identifier,
25623 tag_type,
25624 /*is_template=*/false,
25625 /*is_namespace=*/false,
25626 check_dependency_p,
25627 &ambiguous_decls,
25628 identifier_token->location);
25629 if (ambiguous_decls)
25631 if (cp_parser_parsing_tentatively (parser))
25632 cp_parser_simulate_error (parser);
25633 return error_mark_node;
25637 else
25639 /* Try a template-id. */
25640 decl = cp_parser_template_id (parser, template_keyword_p,
25641 check_dependency_p,
25642 tag_type,
25643 is_declaration);
25644 if (decl == error_mark_node)
25645 return error_mark_node;
25648 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
25650 /* If this is a typename, create a TYPENAME_TYPE. */
25651 if (typename_p && decl != error_mark_node)
25653 decl = make_typename_type (scope, decl, typename_type,
25654 /*complain=*/tf_error);
25655 if (decl != error_mark_node)
25656 decl = TYPE_NAME (decl);
25659 decl = strip_using_decl (decl);
25661 /* Check to see that it is really the name of a class. */
25662 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
25663 && identifier_p (TREE_OPERAND (decl, 0))
25664 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25665 /* Situations like this:
25667 template <typename T> struct A {
25668 typename T::template X<int>::I i;
25671 are problematic. Is `T::template X<int>' a class-name? The
25672 standard does not seem to be definitive, but there is no other
25673 valid interpretation of the following `::'. Therefore, those
25674 names are considered class-names. */
25676 decl = make_typename_type (scope, decl, tag_type, tf_error);
25677 if (decl != error_mark_node)
25678 decl = TYPE_NAME (decl);
25680 else if (TREE_CODE (decl) != TYPE_DECL
25681 || TREE_TYPE (decl) == error_mark_node
25682 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
25683 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
25684 /* In Objective-C 2.0, a classname followed by '.' starts a
25685 dot-syntax expression, and it's not a type-name. */
25686 || (c_dialect_objc ()
25687 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
25688 && objc_is_class_name (decl)))
25689 decl = error_mark_node;
25691 if (decl == error_mark_node)
25692 cp_parser_error (parser, "expected class-name");
25693 else if (identifier && !parser->scope)
25694 maybe_note_name_used_in_class (identifier, decl);
25696 return decl;
25699 /* Make sure that any member-function parameters are in scope.
25700 For instance, a function's noexcept-specifier can use the function's
25701 parameters:
25703 struct S {
25704 void fn (int p) noexcept(noexcept(p));
25707 so we need to make sure name lookup can find them. This is used
25708 when we delay parsing of the noexcept-specifier. */
25710 static void
25711 inject_parm_decls (tree decl)
25713 begin_scope (sk_function_parms, decl);
25714 tree args = DECL_ARGUMENTS (decl);
25716 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
25718 if (args && is_this_parameter (args))
25720 gcc_checking_assert (current_class_ptr == NULL_TREE);
25721 current_class_ref = cp_build_fold_indirect_ref (args);
25722 current_class_ptr = args;
25726 /* Undo the effects of inject_parm_decls. */
25728 static void
25729 pop_injected_parms (void)
25731 pop_bindings_and_leave_scope ();
25732 current_class_ptr = current_class_ref = NULL_TREE;
25735 /* Parse a class-specifier.
25737 class-specifier:
25738 class-head { member-specification [opt] }
25740 Returns the TREE_TYPE representing the class. */
25742 static tree
25743 cp_parser_class_specifier_1 (cp_parser* parser)
25745 tree type;
25746 tree attributes = NULL_TREE;
25747 bool nested_name_specifier_p;
25748 unsigned saved_num_template_parameter_lists;
25749 bool saved_in_function_body;
25750 unsigned char in_statement;
25751 bool in_switch_statement_p;
25752 bool saved_in_unbraced_linkage_specification_p;
25753 tree old_scope = NULL_TREE;
25754 tree scope = NULL_TREE;
25755 cp_token *closing_brace;
25757 push_deferring_access_checks (dk_no_deferred);
25759 /* Parse the class-head. */
25760 type = cp_parser_class_head (parser,
25761 &nested_name_specifier_p);
25762 /* If the class-head was a semantic disaster, skip the entire body
25763 of the class. */
25764 if (!type)
25766 cp_parser_skip_to_end_of_block_or_statement (parser);
25767 pop_deferring_access_checks ();
25768 return error_mark_node;
25771 /* Look for the `{'. */
25772 matching_braces braces;
25773 if (!braces.require_open (parser))
25775 pop_deferring_access_checks ();
25776 return error_mark_node;
25779 cp_ensure_no_omp_declare_simd (parser);
25780 cp_ensure_no_oacc_routine (parser);
25782 /* Issue an error message if type-definitions are forbidden here. */
25783 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
25784 /* Remember that we are defining one more class. */
25785 ++parser->num_classes_being_defined;
25786 /* Inside the class, surrounding template-parameter-lists do not
25787 apply. */
25788 saved_num_template_parameter_lists
25789 = parser->num_template_parameter_lists;
25790 parser->num_template_parameter_lists = 0;
25791 /* We are not in a function body. */
25792 saved_in_function_body = parser->in_function_body;
25793 parser->in_function_body = false;
25794 /* Or in a loop. */
25795 in_statement = parser->in_statement;
25796 parser->in_statement = 0;
25797 /* Or in a switch. */
25798 in_switch_statement_p = parser->in_switch_statement_p;
25799 parser->in_switch_statement_p = false;
25800 /* We are not immediately inside an extern "lang" block. */
25801 saved_in_unbraced_linkage_specification_p
25802 = parser->in_unbraced_linkage_specification_p;
25803 parser->in_unbraced_linkage_specification_p = false;
25805 /* Start the class. */
25806 if (nested_name_specifier_p)
25808 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
25809 /* SCOPE must be a scope nested inside current scope. */
25810 if (is_nested_namespace (current_namespace,
25811 decl_namespace_context (scope)))
25812 old_scope = push_inner_scope (scope);
25813 else
25814 nested_name_specifier_p = false;
25816 type = begin_class_definition (type);
25818 if (type == error_mark_node)
25819 /* If the type is erroneous, skip the entire body of the class. */
25820 cp_parser_skip_to_closing_brace (parser);
25821 else
25822 /* Parse the member-specification. */
25823 cp_parser_member_specification_opt (parser);
25825 /* Look for the trailing `}'. */
25826 closing_brace = braces.require_close (parser);
25827 /* Look for trailing attributes to apply to this class. */
25828 if (cp_parser_allow_gnu_extensions_p (parser))
25829 attributes = cp_parser_gnu_attributes_opt (parser);
25830 if (type != error_mark_node)
25831 type = finish_struct (type, attributes);
25832 if (nested_name_specifier_p)
25833 pop_inner_scope (old_scope, scope);
25835 /* We've finished a type definition. Check for the common syntax
25836 error of forgetting a semicolon after the definition. We need to
25837 be careful, as we can't just check for not-a-semicolon and be done
25838 with it; the user might have typed:
25840 class X { } c = ...;
25841 class X { } *p = ...;
25843 and so forth. Instead, enumerate all the possible tokens that
25844 might follow this production; if we don't see one of them, then
25845 complain and silently insert the semicolon. */
25847 cp_token *token = cp_lexer_peek_token (parser->lexer);
25848 bool want_semicolon = true;
25850 if (cp_next_tokens_can_be_std_attribute_p (parser))
25851 /* Don't try to parse c++11 attributes here. As per the
25852 grammar, that should be a task for
25853 cp_parser_decl_specifier_seq. */
25854 want_semicolon = false;
25856 switch (token->type)
25858 case CPP_NAME:
25859 case CPP_SEMICOLON:
25860 case CPP_MULT:
25861 case CPP_AND:
25862 case CPP_OPEN_PAREN:
25863 case CPP_CLOSE_PAREN:
25864 case CPP_COMMA:
25865 want_semicolon = false;
25866 break;
25868 /* While it's legal for type qualifiers and storage class
25869 specifiers to follow type definitions in the grammar, only
25870 compiler testsuites contain code like that. Assume that if
25871 we see such code, then what we're really seeing is a case
25872 like:
25874 class X { }
25875 const <type> var = ...;
25879 class Y { }
25880 static <type> func (...) ...
25882 i.e. the qualifier or specifier applies to the next
25883 declaration. To do so, however, we need to look ahead one
25884 more token to see if *that* token is a type specifier.
25886 This code could be improved to handle:
25888 class Z { }
25889 static const <type> var = ...; */
25890 case CPP_KEYWORD:
25891 if (keyword_is_decl_specifier (token->keyword))
25893 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
25895 /* Handling user-defined types here would be nice, but very
25896 tricky. */
25897 want_semicolon
25898 = (lookahead->type == CPP_KEYWORD
25899 && keyword_begins_type_specifier (lookahead->keyword));
25901 break;
25902 default:
25903 break;
25906 /* If we don't have a type, then something is very wrong and we
25907 shouldn't try to do anything clever. Likewise for not seeing the
25908 closing brace. */
25909 if (closing_brace && TYPE_P (type) && want_semicolon)
25911 /* Locate the closing brace. */
25912 cp_token_position prev
25913 = cp_lexer_previous_token_position (parser->lexer);
25914 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
25915 location_t loc = prev_token->location;
25917 /* We want to suggest insertion of a ';' immediately *after* the
25918 closing brace, so, if we can, offset the location by 1 column. */
25919 location_t next_loc = loc;
25920 if (!linemap_location_from_macro_expansion_p (line_table, loc))
25921 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
25923 rich_location richloc (line_table, next_loc);
25925 /* If we successfully offset the location, suggest the fix-it. */
25926 if (next_loc != loc)
25927 richloc.add_fixit_insert_before (next_loc, ";");
25929 if (CLASSTYPE_DECLARED_CLASS (type))
25930 error_at (&richloc,
25931 "expected %<;%> after class definition");
25932 else if (TREE_CODE (type) == RECORD_TYPE)
25933 error_at (&richloc,
25934 "expected %<;%> after struct definition");
25935 else if (TREE_CODE (type) == UNION_TYPE)
25936 error_at (&richloc,
25937 "expected %<;%> after union definition");
25938 else
25939 gcc_unreachable ();
25941 /* Unget one token and smash it to look as though we encountered
25942 a semicolon in the input stream. */
25943 cp_lexer_set_token_position (parser->lexer, prev);
25944 token = cp_lexer_peek_token (parser->lexer);
25945 token->type = CPP_SEMICOLON;
25946 token->keyword = RID_MAX;
25950 /* If this class is not itself within the scope of another class,
25951 then we need to parse the bodies of all of the queued function
25952 definitions. Note that the queued functions defined in a class
25953 are not always processed immediately following the
25954 class-specifier for that class. Consider:
25956 struct A {
25957 struct B { void f() { sizeof (A); } };
25960 If `f' were processed before the processing of `A' were
25961 completed, there would be no way to compute the size of `A'.
25962 Note that the nesting we are interested in here is lexical --
25963 not the semantic nesting given by TYPE_CONTEXT. In particular,
25964 for:
25966 struct A { struct B; };
25967 struct A::B { void f() { } };
25969 there is no need to delay the parsing of `A::B::f'. */
25970 if (--parser->num_classes_being_defined == 0)
25972 tree decl;
25973 tree class_type = NULL_TREE;
25974 tree pushed_scope = NULL_TREE;
25975 unsigned ix;
25976 cp_default_arg_entry *e;
25978 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
25980 /* Skip default arguments, NSDMIs, etc, in order to improve
25981 error recovery (c++/71169, c++/71832). */
25982 vec_safe_truncate (unparsed_funs_with_default_args, 0);
25983 vec_safe_truncate (unparsed_nsdmis, 0);
25984 vec_safe_truncate (unparsed_funs_with_definitions, 0);
25987 /* In a first pass, parse default arguments to the functions.
25988 Then, in a second pass, parse the bodies of the functions.
25989 This two-phased approach handles cases like:
25991 struct S {
25992 void f() { g(); }
25993 void g(int i = 3);
25997 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
25999 decl = e->decl;
26000 /* If there are default arguments that have not yet been processed,
26001 take care of them now. */
26002 if (class_type != e->class_type)
26004 if (pushed_scope)
26005 pop_scope (pushed_scope);
26006 class_type = e->class_type;
26007 pushed_scope = push_scope (class_type);
26009 /* Make sure that any template parameters are in scope. */
26010 maybe_begin_member_template_processing (decl);
26011 /* Parse the default argument expressions. */
26012 cp_parser_late_parsing_default_args (parser, decl);
26013 /* Remove any template parameters from the symbol table. */
26014 maybe_end_member_template_processing ();
26016 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26018 /* If there are noexcept-specifiers that have not yet been processed,
26019 take care of them now. Do this before processing NSDMIs as they
26020 may depend on noexcept-specifiers already having been processed. */
26021 tree save_ccp = current_class_ptr;
26022 tree save_ccr = current_class_ref;
26023 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26025 tree ctx = DECL_CONTEXT (decl);
26026 if (class_type != ctx)
26028 if (pushed_scope)
26029 pop_scope (pushed_scope);
26030 class_type = ctx;
26031 pushed_scope = push_scope (class_type);
26034 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26035 def_parse = TREE_PURPOSE (def_parse);
26037 /* Make sure that any template parameters are in scope. */
26038 maybe_begin_member_template_processing (decl);
26040 /* Make sure that any member-function parameters are in scope.
26041 This function doesn't expect ccp to be set. */
26042 current_class_ptr = current_class_ref = NULL_TREE;
26043 inject_parm_decls (decl);
26045 /* 'this' is not allowed in static member functions. */
26046 unsigned char local_variables_forbidden_p
26047 = parser->local_variables_forbidden_p;
26048 if (DECL_THIS_STATIC (decl))
26049 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26051 /* Now we can parse the noexcept-specifier. */
26052 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26054 if (spec == error_mark_node)
26055 spec = NULL_TREE;
26057 /* Update the fn's type directly -- it might have escaped
26058 beyond this decl :( */
26059 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26060 /* Update any instantiations we've already created. We must
26061 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26062 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26063 in the pattern. */
26064 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26065 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26066 = spec ? TREE_PURPOSE (spec) : error_mark_node;
26068 /* Restore the state of local_variables_forbidden_p. */
26069 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26071 /* The finish_struct call above performed various override checking,
26072 but it skipped unparsed noexcept-specifier operands. Now that we
26073 have resolved them, check again. */
26074 noexcept_override_late_checks (type, decl);
26076 /* Remove any member-function parameters from the symbol table. */
26077 pop_injected_parms ();
26079 /* Remove any template parameters from the symbol table. */
26080 maybe_end_member_template_processing ();
26082 vec_safe_truncate (unparsed_noexcepts, 0);
26084 /* Now parse any NSDMIs. */
26085 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26087 if (class_type != DECL_CONTEXT (decl))
26089 if (pushed_scope)
26090 pop_scope (pushed_scope);
26091 class_type = DECL_CONTEXT (decl);
26092 pushed_scope = push_scope (class_type);
26094 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26095 cp_parser_late_parsing_nsdmi (parser, decl);
26097 vec_safe_truncate (unparsed_nsdmis, 0);
26098 current_class_ptr = save_ccp;
26099 current_class_ref = save_ccr;
26100 if (pushed_scope)
26101 pop_scope (pushed_scope);
26103 /* Now parse the body of the functions. */
26104 if (flag_openmp)
26106 /* OpenMP UDRs need to be parsed before all other functions. */
26107 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26108 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26109 cp_parser_late_parsing_for_member (parser, decl);
26110 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26111 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26112 cp_parser_late_parsing_for_member (parser, decl);
26114 else
26115 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26116 cp_parser_late_parsing_for_member (parser, decl);
26117 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26120 /* Put back any saved access checks. */
26121 pop_deferring_access_checks ();
26123 /* Restore saved state. */
26124 parser->in_switch_statement_p = in_switch_statement_p;
26125 parser->in_statement = in_statement;
26126 parser->in_function_body = saved_in_function_body;
26127 parser->num_template_parameter_lists
26128 = saved_num_template_parameter_lists;
26129 parser->in_unbraced_linkage_specification_p
26130 = saved_in_unbraced_linkage_specification_p;
26132 return type;
26135 static tree
26136 cp_parser_class_specifier (cp_parser* parser)
26138 tree ret;
26139 timevar_push (TV_PARSE_STRUCT);
26140 ret = cp_parser_class_specifier_1 (parser);
26141 timevar_pop (TV_PARSE_STRUCT);
26142 return ret;
26145 /* Parse a class-head.
26147 class-head:
26148 class-key identifier [opt] base-clause [opt]
26149 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26150 class-key nested-name-specifier [opt] template-id
26151 base-clause [opt]
26153 class-virt-specifier:
26154 final
26156 GNU Extensions:
26157 class-key attributes identifier [opt] base-clause [opt]
26158 class-key attributes nested-name-specifier identifier base-clause [opt]
26159 class-key attributes nested-name-specifier [opt] template-id
26160 base-clause [opt]
26162 Upon return BASES is initialized to the list of base classes (or
26163 NULL, if there are none) in the same form returned by
26164 cp_parser_base_clause.
26166 Returns the TYPE of the indicated class. Sets
26167 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26168 involving a nested-name-specifier was used, and FALSE otherwise.
26170 Returns error_mark_node if this is not a class-head.
26172 Returns NULL_TREE if the class-head is syntactically valid, but
26173 semantically invalid in a way that means we should skip the entire
26174 body of the class. */
26176 static tree
26177 cp_parser_class_head (cp_parser* parser,
26178 bool* nested_name_specifier_p)
26180 tree nested_name_specifier;
26181 enum tag_types class_key;
26182 tree id = NULL_TREE;
26183 tree type = NULL_TREE;
26184 tree attributes;
26185 tree bases;
26186 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26187 bool template_id_p = false;
26188 bool qualified_p = false;
26189 bool invalid_nested_name_p = false;
26190 bool invalid_explicit_specialization_p = false;
26191 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26192 tree pushed_scope = NULL_TREE;
26193 unsigned num_templates;
26194 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26195 /* Assume no nested-name-specifier will be present. */
26196 *nested_name_specifier_p = false;
26197 /* Assume no template parameter lists will be used in defining the
26198 type. */
26199 num_templates = 0;
26200 parser->colon_corrects_to_scope_p = false;
26202 /* Look for the class-key. */
26203 class_key = cp_parser_class_key (parser);
26204 if (class_key == none_type)
26205 return error_mark_node;
26207 location_t class_head_start_location = input_location;
26209 /* Parse the attributes. */
26210 attributes = cp_parser_attributes_opt (parser);
26212 /* If the next token is `::', that is invalid -- but sometimes
26213 people do try to write:
26215 struct ::S {};
26217 Handle this gracefully by accepting the extra qualifier, and then
26218 issuing an error about it later if this really is a
26219 class-head. If it turns out just to be an elaborated type
26220 specifier, remain silent. */
26221 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26222 qualified_p = true;
26224 push_deferring_access_checks (dk_no_check);
26226 /* Determine the name of the class. Begin by looking for an
26227 optional nested-name-specifier. */
26228 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26229 nested_name_specifier
26230 = cp_parser_nested_name_specifier_opt (parser,
26231 /*typename_keyword_p=*/false,
26232 /*check_dependency_p=*/false,
26233 /*type_p=*/true,
26234 /*is_declaration=*/false);
26235 /* If there was a nested-name-specifier, then there *must* be an
26236 identifier. */
26238 cp_token *bad_template_keyword = NULL;
26240 if (nested_name_specifier)
26242 type_start_token = cp_lexer_peek_token (parser->lexer);
26243 /* Although the grammar says `identifier', it really means
26244 `class-name' or `template-name'. You are only allowed to
26245 define a class that has already been declared with this
26246 syntax.
26248 The proposed resolution for Core Issue 180 says that wherever
26249 you see `class T::X' you should treat `X' as a type-name.
26251 It is OK to define an inaccessible class; for example:
26253 class A { class B; };
26254 class A::B {};
26256 We do not know if we will see a class-name, or a
26257 template-name. We look for a class-name first, in case the
26258 class-name is a template-id; if we looked for the
26259 template-name first we would stop after the template-name. */
26260 cp_parser_parse_tentatively (parser);
26261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26262 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26263 type = cp_parser_class_name (parser,
26264 /*typename_keyword_p=*/false,
26265 /*template_keyword_p=*/false,
26266 class_type,
26267 /*check_dependency_p=*/false,
26268 /*class_head_p=*/true,
26269 /*is_declaration=*/false);
26270 /* If that didn't work, ignore the nested-name-specifier. */
26271 if (!cp_parser_parse_definitely (parser))
26273 invalid_nested_name_p = true;
26274 type_start_token = cp_lexer_peek_token (parser->lexer);
26275 id = cp_parser_identifier (parser);
26276 if (id == error_mark_node)
26277 id = NULL_TREE;
26279 /* If we could not find a corresponding TYPE, treat this
26280 declaration like an unqualified declaration. */
26281 if (type == error_mark_node)
26282 nested_name_specifier = NULL_TREE;
26283 /* Otherwise, count the number of templates used in TYPE and its
26284 containing scopes. */
26285 else
26286 num_templates = num_template_headers_for_class (TREE_TYPE (type));
26288 /* Otherwise, the identifier is optional. */
26289 else
26291 /* We don't know whether what comes next is a template-id,
26292 an identifier, or nothing at all. */
26293 cp_parser_parse_tentatively (parser);
26294 /* Check for a template-id. */
26295 type_start_token = cp_lexer_peek_token (parser->lexer);
26296 id = cp_parser_template_id (parser,
26297 /*template_keyword_p=*/false,
26298 /*check_dependency_p=*/true,
26299 class_key,
26300 /*is_declaration=*/true);
26301 /* If that didn't work, it could still be an identifier. */
26302 if (!cp_parser_parse_definitely (parser))
26304 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26306 type_start_token = cp_lexer_peek_token (parser->lexer);
26307 id = cp_parser_identifier (parser);
26309 else
26310 id = NULL_TREE;
26312 else
26314 template_id_p = true;
26315 ++num_templates;
26319 pop_deferring_access_checks ();
26321 if (id)
26323 cp_parser_check_for_invalid_template_id (parser, id,
26324 class_key,
26325 type_start_token->location);
26327 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
26329 /* If it's not a `:' or a `{' then we can't really be looking at a
26330 class-head, since a class-head only appears as part of a
26331 class-specifier. We have to detect this situation before calling
26332 xref_tag, since that has irreversible side-effects. */
26333 if (!cp_parser_next_token_starts_class_definition_p (parser))
26335 cp_parser_error (parser, "expected %<{%> or %<:%>");
26336 type = error_mark_node;
26337 goto out;
26340 /* At this point, we're going ahead with the class-specifier, even
26341 if some other problem occurs. */
26342 cp_parser_commit_to_tentative_parse (parser);
26343 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
26345 cp_parser_error (parser,
26346 "cannot specify %<override%> for a class");
26347 type = error_mark_node;
26348 goto out;
26350 /* Issue the error about the overly-qualified name now. */
26351 if (qualified_p)
26353 cp_parser_error (parser,
26354 "global qualification of class name is invalid");
26355 type = error_mark_node;
26356 goto out;
26358 else if (invalid_nested_name_p)
26360 cp_parser_error (parser,
26361 "qualified name does not name a class");
26362 type = error_mark_node;
26363 goto out;
26365 else if (nested_name_specifier)
26367 tree scope;
26369 if (bad_template_keyword)
26370 /* [temp.names]: in a qualified-id formed by a class-head-name, the
26371 keyword template shall not appear at the top level. */
26372 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
26373 "keyword %<template%> not allowed in class-head-name");
26375 /* Reject typedef-names in class heads. */
26376 if (!DECL_IMPLICIT_TYPEDEF_P (type))
26378 error_at (type_start_token->location,
26379 "invalid class name in declaration of %qD",
26380 type);
26381 type = NULL_TREE;
26382 goto done;
26385 /* Figure out in what scope the declaration is being placed. */
26386 scope = current_scope ();
26387 /* If that scope does not contain the scope in which the
26388 class was originally declared, the program is invalid. */
26389 if (scope && !is_ancestor (scope, nested_name_specifier))
26391 if (at_namespace_scope_p ())
26392 error_at (type_start_token->location,
26393 "declaration of %qD in namespace %qD which does not "
26394 "enclose %qD",
26395 type, scope, nested_name_specifier);
26396 else
26397 error_at (type_start_token->location,
26398 "declaration of %qD in %qD which does not enclose %qD",
26399 type, scope, nested_name_specifier);
26400 type = NULL_TREE;
26401 goto done;
26403 /* [dcl.meaning]
26405 A declarator-id shall not be qualified except for the
26406 definition of a ... nested class outside of its class
26407 ... [or] the definition or explicit instantiation of a
26408 class member of a namespace outside of its namespace. */
26409 if (scope == nested_name_specifier)
26410 permerror (nested_name_specifier_token_start->location,
26411 "extra qualification not allowed");
26413 /* An explicit-specialization must be preceded by "template <>". If
26414 it is not, try to recover gracefully. */
26415 if (at_namespace_scope_p ()
26416 && parser->num_template_parameter_lists == 0
26417 && !processing_template_parmlist
26418 && template_id_p)
26420 /* Build a location of this form:
26421 struct typename <ARGS>
26422 ^~~~~~~~~~~~~~~~~~~~~~
26423 with caret==start at the start token, and
26424 finishing at the end of the type. */
26425 location_t reported_loc
26426 = make_location (class_head_start_location,
26427 class_head_start_location,
26428 get_finish (type_start_token->location));
26429 rich_location richloc (line_table, reported_loc);
26430 richloc.add_fixit_insert_before (class_head_start_location,
26431 "template <> ");
26432 error_at (&richloc,
26433 "an explicit specialization must be preceded by"
26434 " %<template <>%>");
26435 invalid_explicit_specialization_p = true;
26436 /* Take the same action that would have been taken by
26437 cp_parser_explicit_specialization. */
26438 ++parser->num_template_parameter_lists;
26439 begin_specialization ();
26441 /* There must be no "return" statements between this point and the
26442 end of this function; set "type "to the correct return value and
26443 use "goto done;" to return. */
26444 /* Make sure that the right number of template parameters were
26445 present. */
26446 if (!cp_parser_check_template_parameters (parser, num_templates,
26447 template_id_p,
26448 type_start_token->location,
26449 /*declarator=*/NULL))
26451 /* If something went wrong, there is no point in even trying to
26452 process the class-definition. */
26453 type = NULL_TREE;
26454 goto done;
26457 /* Look up the type. */
26458 if (template_id_p)
26460 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
26461 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
26462 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
26464 error_at (type_start_token->location,
26465 "function template %qD redeclared as a class template", id);
26466 type = error_mark_node;
26468 else
26470 type = TREE_TYPE (id);
26471 type = maybe_process_partial_specialization (type);
26473 /* Check the scope while we still know whether or not we had a
26474 nested-name-specifier. */
26475 if (type != error_mark_node)
26476 check_unqualified_spec_or_inst (type, type_start_token->location);
26478 if (nested_name_specifier)
26479 pushed_scope = push_scope (nested_name_specifier);
26481 else if (nested_name_specifier)
26483 tree class_type;
26485 /* Given:
26487 template <typename T> struct S { struct T };
26488 template <typename T> struct S<T>::T { };
26490 we will get a TYPENAME_TYPE when processing the definition of
26491 `S::T'. We need to resolve it to the actual type before we
26492 try to define it. */
26493 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
26495 class_type = resolve_typename_type (TREE_TYPE (type),
26496 /*only_current_p=*/false);
26497 if (TREE_CODE (class_type) != TYPENAME_TYPE)
26498 type = TYPE_NAME (class_type);
26499 else
26501 cp_parser_error (parser, "could not resolve typename type");
26502 type = error_mark_node;
26506 if (maybe_process_partial_specialization (TREE_TYPE (type))
26507 == error_mark_node)
26509 type = NULL_TREE;
26510 goto done;
26513 class_type = current_class_type;
26514 /* Enter the scope indicated by the nested-name-specifier. */
26515 pushed_scope = push_scope (nested_name_specifier);
26516 /* Get the canonical version of this type. */
26517 type = TYPE_MAIN_DECL (TREE_TYPE (type));
26518 /* Call push_template_decl if it seems like we should be defining a
26519 template either from the template headers or the type we're
26520 defining, so that we diagnose both extra and missing headers. */
26521 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26522 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
26523 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
26525 type = push_template_decl (type);
26526 if (type == error_mark_node)
26528 type = NULL_TREE;
26529 goto done;
26533 type = TREE_TYPE (type);
26534 *nested_name_specifier_p = true;
26536 else /* The name is not a nested name. */
26538 /* If the class was unnamed, create a dummy name. */
26539 if (!id)
26540 id = make_anon_name ();
26541 TAG_how how = (parser->in_type_id_in_expr_p
26542 ? TAG_how::INNERMOST_NON_CLASS
26543 : TAG_how::CURRENT_ONLY);
26544 type = xref_tag (class_key, id, how,
26545 parser->num_template_parameter_lists);
26548 /* Diagnose class/struct/union mismatches. */
26549 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
26550 true, true);
26552 /* Indicate whether this class was declared as a `class' or as a
26553 `struct'. */
26554 if (TREE_CODE (type) == RECORD_TYPE)
26555 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
26557 /* If this type was already complete, and we see another definition,
26558 that's an error. Likewise if the type is already being defined:
26559 this can happen, eg, when it's defined from within an expression
26560 (c++/84605). */
26561 if (type != error_mark_node
26562 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
26564 error_at (type_start_token->location, "redefinition of %q#T",
26565 type);
26566 inform (location_of (type), "previous definition of %q#T",
26567 type);
26568 type = NULL_TREE;
26569 goto done;
26571 else if (type == error_mark_node)
26572 type = NULL_TREE;
26574 if (type)
26576 /* Apply attributes now, before any use of the class as a template
26577 argument in its base list. */
26578 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
26579 fixup_attribute_variants (type);
26582 /* Associate constraints with the type. */
26583 if (flag_concepts)
26584 type = associate_classtype_constraints (type);
26586 /* We will have entered the scope containing the class; the names of
26587 base classes should be looked up in that context. For example:
26589 struct A { struct B {}; struct C; };
26590 struct A::C : B {};
26592 is valid. */
26594 /* Get the list of base-classes, if there is one. Defer access checking
26595 until the entire list has been seen, as per [class.access.general]. */
26596 push_deferring_access_checks (dk_deferred);
26597 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26599 if (type)
26600 pushclass (type);
26601 bases = cp_parser_base_clause (parser);
26602 if (type)
26603 popclass ();
26605 else
26606 bases = NULL_TREE;
26608 /* If we're really defining a class, process the base classes.
26609 If they're invalid, fail. */
26610 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26611 xref_basetypes (type, bases);
26613 /* Now that all bases have been seen and attached to the class, check
26614 accessibility of the types named in the base-clause. This must be
26615 done relative to the class scope, so that we accept e.g.
26617 struct A { protected: struct B {}; };
26618 struct C : A::B, A {}; // OK: A::B is accessible via base A
26620 as per [class.access.general]. */
26621 if (type)
26622 pushclass (type);
26623 pop_to_parent_deferring_access_checks ();
26624 if (type)
26625 popclass ();
26627 done:
26628 /* Leave the scope given by the nested-name-specifier. We will
26629 enter the class scope itself while processing the members. */
26630 if (pushed_scope)
26631 pop_scope (pushed_scope);
26633 if (invalid_explicit_specialization_p)
26635 end_specialization ();
26636 --parser->num_template_parameter_lists;
26639 if (type)
26640 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
26641 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
26642 CLASSTYPE_FINAL (type) = 1;
26643 out:
26644 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26645 return type;
26648 /* Parse a class-key.
26650 class-key:
26651 class
26652 struct
26653 union
26655 Returns the kind of class-key specified, or none_type to indicate
26656 error. */
26658 static enum tag_types
26659 cp_parser_class_key (cp_parser* parser)
26661 cp_token *token;
26662 enum tag_types tag_type;
26664 /* Look for the class-key. */
26665 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
26666 if (!token)
26667 return none_type;
26669 /* Check to see if the TOKEN is a class-key. */
26670 tag_type = cp_parser_token_is_class_key (token);
26671 if (!tag_type)
26672 cp_parser_error (parser, "expected class-key");
26673 return tag_type;
26676 /* Parse a type-parameter-key.
26678 type-parameter-key:
26679 class
26680 typename
26683 static void
26684 cp_parser_type_parameter_key (cp_parser* parser)
26686 /* Look for the type-parameter-key. */
26687 enum tag_types tag_type = none_type;
26688 cp_token *token = cp_lexer_peek_token (parser->lexer);
26689 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
26691 cp_lexer_consume_token (parser->lexer);
26692 if (pedantic && tag_type == typename_type
26693 && cxx_dialect < cxx17)
26694 /* typename is not allowed in a template template parameter
26695 by the standard until C++17. */
26696 pedwarn (token->location, OPT_Wc__17_extensions,
26697 "ISO C++ forbids typename key in template template parameter;"
26698 " use %<-std=c++17%> or %<-std=gnu++17%>");
26700 else
26701 cp_parser_error (parser, "expected %<class%> or %<typename%>");
26703 return;
26706 /* Parse an (optional) member-specification.
26708 member-specification:
26709 member-declaration member-specification [opt]
26710 access-specifier : member-specification [opt] */
26712 static void
26713 cp_parser_member_specification_opt (cp_parser* parser)
26715 while (true)
26717 cp_token *token;
26718 enum rid keyword;
26720 /* Peek at the next token. */
26721 token = cp_lexer_peek_token (parser->lexer);
26722 /* If it's a `}', or EOF then we've seen all the members. */
26723 if (token->type == CPP_CLOSE_BRACE
26724 || token->type == CPP_EOF
26725 || token->type == CPP_PRAGMA_EOL)
26726 break;
26728 /* See if this token is a keyword. */
26729 keyword = token->keyword;
26730 switch (keyword)
26732 case RID_PUBLIC:
26733 case RID_PROTECTED:
26734 case RID_PRIVATE:
26735 /* Consume the access-specifier. */
26736 cp_lexer_consume_token (parser->lexer);
26737 /* Remember which access-specifier is active. */
26738 current_access_specifier = token->u.value;
26739 /* Look for the `:'. */
26740 cp_parser_require (parser, CPP_COLON, RT_COLON);
26741 break;
26743 default:
26744 /* Accept #pragmas at class scope. */
26745 if (token->type == CPP_PRAGMA)
26747 cp_parser_pragma (parser, pragma_member, NULL);
26748 break;
26751 /* Otherwise, the next construction must be a
26752 member-declaration. */
26753 cp_parser_member_declaration (parser);
26758 /* Parse a member-declaration.
26760 member-declaration:
26761 decl-specifier-seq [opt] member-declarator-list [opt] ;
26762 function-definition ; [opt]
26763 :: [opt] nested-name-specifier template [opt] unqualified-id ;
26764 using-declaration
26765 template-declaration
26766 alias-declaration
26768 member-declarator-list:
26769 member-declarator
26770 member-declarator-list , member-declarator
26772 member-declarator:
26773 declarator pure-specifier [opt]
26774 declarator constant-initializer [opt]
26775 identifier [opt] : constant-expression
26777 GNU Extensions:
26779 member-declaration:
26780 __extension__ member-declaration
26782 member-declarator:
26783 declarator attributes [opt] pure-specifier [opt]
26784 declarator attributes [opt] constant-initializer [opt]
26785 identifier [opt] attributes [opt] : constant-expression
26787 C++0x Extensions:
26789 member-declaration:
26790 static_assert-declaration */
26792 static void
26793 cp_parser_member_declaration (cp_parser* parser)
26795 cp_decl_specifier_seq decl_specifiers;
26796 tree prefix_attributes;
26797 tree decl;
26798 int declares_class_or_enum;
26799 bool friend_p;
26800 cp_token *token = NULL;
26801 cp_token *decl_spec_token_start = NULL;
26802 cp_token *initializer_token_start = NULL;
26803 int saved_pedantic;
26804 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26806 /* Check for the `__extension__' keyword. */
26807 if (cp_parser_extension_opt (parser, &saved_pedantic))
26809 /* Recurse. */
26810 cp_parser_member_declaration (parser);
26811 /* Restore the old value of the PEDANTIC flag. */
26812 pedantic = saved_pedantic;
26814 return;
26817 /* Check for a template-declaration. */
26818 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26820 /* An explicit specialization here is an error condition, and we
26821 expect the specialization handler to detect and report this. */
26822 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
26823 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
26824 cp_parser_explicit_specialization (parser);
26825 else
26826 cp_parser_template_declaration (parser, /*member_p=*/true);
26828 return;
26830 /* Check for a template introduction. */
26831 else if (cp_parser_template_declaration_after_export (parser, true))
26832 return;
26834 /* Check for a using-declaration. */
26835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26837 if (cxx_dialect < cxx11)
26838 /* Parse the using-declaration. */
26839 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
26840 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
26841 cp_parser_using_enum (parser);
26842 else
26844 tree decl;
26845 bool alias_decl_expected;
26846 cp_parser_parse_tentatively (parser);
26847 decl = cp_parser_alias_declaration (parser);
26848 /* Note that if we actually see the '=' token after the
26849 identifier, cp_parser_alias_declaration commits the
26850 tentative parse. In that case, we really expect an
26851 alias-declaration. Otherwise, we expect a using
26852 declaration. */
26853 alias_decl_expected =
26854 !cp_parser_uncommitted_to_tentative_parse_p (parser);
26855 cp_parser_parse_definitely (parser);
26857 if (alias_decl_expected)
26858 finish_member_declaration (decl);
26859 else
26860 cp_parser_using_declaration (parser,
26861 /*access_declaration_p=*/false);
26863 return;
26866 /* Check for @defs. */
26867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
26869 tree ivar, member;
26870 tree ivar_chains = cp_parser_objc_defs_expression (parser);
26871 ivar = ivar_chains;
26872 while (ivar)
26874 member = ivar;
26875 ivar = TREE_CHAIN (member);
26876 TREE_CHAIN (member) = NULL_TREE;
26877 finish_member_declaration (member);
26879 return;
26882 /* If the next token is `static_assert' we have a static assertion. */
26883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
26885 cp_parser_static_assert (parser, /*member_p=*/true);
26886 return;
26889 parser->colon_corrects_to_scope_p = false;
26891 cp_omp_declare_simd_data odsd;
26892 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
26893 goto out;
26895 /* Parse the decl-specifier-seq. */
26896 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26897 cp_parser_decl_specifier_seq (parser,
26898 (CP_PARSER_FLAGS_OPTIONAL
26899 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
26900 &decl_specifiers,
26901 &declares_class_or_enum);
26903 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
26904 cp_parser_handle_directive_omp_attributes (parser,
26905 &decl_specifiers.attributes,
26906 &odsd, true);
26908 /* Check for an invalid type-name. */
26909 if (!decl_specifiers.any_type_specifiers_p
26910 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26911 goto out;
26912 /* If there is no declarator, then the decl-specifier-seq should
26913 specify a type. */
26914 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26916 /* If there was no decl-specifier-seq, and the next token is a
26917 `;', then we have something like:
26919 struct S { ; };
26921 [class.mem]
26923 Each member-declaration shall declare at least one member
26924 name of the class. */
26925 if (!decl_specifiers.any_specifiers_p)
26927 cp_token *token = cp_lexer_peek_token (parser->lexer);
26928 if (!in_system_header_at (token->location))
26930 gcc_rich_location richloc (token->location);
26931 richloc.add_fixit_remove ();
26932 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
26935 else
26937 /* See if this declaration is a friend. */
26938 friend_p = cp_parser_friend_p (&decl_specifiers);
26939 /* If there were decl-specifiers, check to see if there was
26940 a class-declaration. */
26941 tree type = check_tag_decl (&decl_specifiers,
26942 /*explicit_type_instantiation_p=*/false);
26943 /* Nested classes have already been added to the class, but
26944 a `friend' needs to be explicitly registered. */
26945 if (friend_p)
26947 /* If the `friend' keyword was present, the friend must
26948 be introduced with a class-key. */
26949 if (!declares_class_or_enum && cxx_dialect < cxx11)
26950 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
26951 "in C++03 a class-key must be used "
26952 "when declaring a friend");
26953 /* In this case:
26955 template <typename T> struct A {
26956 friend struct A<T>::B;
26959 A<T>::B will be represented by a TYPENAME_TYPE, and
26960 therefore not recognized by check_tag_decl. */
26961 if (!type)
26963 type = decl_specifiers.type;
26964 if (type && TREE_CODE (type) == TYPE_DECL)
26965 type = TREE_TYPE (type);
26967 /* Warn if an attribute cannot appear here, as per
26968 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
26969 we ignore attributes in elaborated-type-specifiers. */
26970 if (!declares_class_or_enum
26971 && cxx11_attribute_p (decl_specifiers.attributes))
26973 decl_specifiers.attributes = NULL_TREE;
26974 if (warning_at (decl_spec_token_start->location,
26975 OPT_Wattributes, "attribute ignored"))
26976 inform (decl_spec_token_start->location, "an attribute "
26977 "that appertains to a friend declaration that "
26978 "is not a definition is ignored");
26980 if (!type || !TYPE_P (type))
26981 error_at (decl_spec_token_start->location,
26982 "friend declaration does not name a class or "
26983 "function");
26984 else
26985 make_friend_class (current_class_type, type,
26986 /*complain=*/true);
26988 /* If there is no TYPE, an error message will already have
26989 been issued. */
26990 else if (!type || type == error_mark_node)
26992 /* An anonymous aggregate has to be handled specially; such
26993 a declaration really declares a data member (with a
26994 particular type), as opposed to a nested class. */
26995 else if (ANON_AGGR_TYPE_P (type))
26997 /* C++11 9.5/6. */
26998 if (decl_specifiers.storage_class != sc_none)
26999 error_at (decl_spec_token_start->location,
27000 "a storage class on an anonymous aggregate "
27001 "in class scope is not allowed");
27003 /* Remove constructors and such from TYPE, now that we
27004 know it is an anonymous aggregate. */
27005 fixup_anonymous_aggr (type);
27006 /* And make the corresponding data member. */
27007 decl = build_decl (decl_spec_token_start->location,
27008 FIELD_DECL, NULL_TREE, type);
27009 /* Add it to the class. */
27010 finish_member_declaration (decl);
27012 else
27013 cp_parser_check_access_in_redeclaration
27014 (TYPE_NAME (type),
27015 decl_spec_token_start->location);
27018 else
27020 bool assume_semicolon = false;
27022 /* Clear attributes from the decl_specifiers but keep them
27023 around as prefix attributes that apply them to the entity
27024 being declared. */
27025 prefix_attributes = decl_specifiers.attributes;
27026 decl_specifiers.attributes = NULL_TREE;
27027 if (parser->omp_declare_simd
27028 && (parser->omp_declare_simd->attribs[0]
27029 == &decl_specifiers.attributes))
27030 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27032 /* See if these declarations will be friends. */
27033 friend_p = cp_parser_friend_p (&decl_specifiers);
27035 /* Keep going until we hit the `;' at the end of the
27036 declaration. */
27037 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27039 tree attributes = NULL_TREE;
27040 tree first_attribute;
27041 tree initializer;
27042 bool named_bitfld = false;
27044 /* Peek at the next token. */
27045 token = cp_lexer_peek_token (parser->lexer);
27047 /* The following code wants to know early if it is a bit-field
27048 or some other declaration. Attributes can appear before
27049 the `:' token. Skip over them without consuming any tokens
27050 to peek if they are followed by `:'. */
27051 if (cp_next_tokens_can_be_attribute_p (parser)
27052 || (token->type == CPP_NAME
27053 && cp_nth_tokens_can_be_attribute_p (parser, 2)
27054 && (named_bitfld = true)))
27056 size_t n
27057 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27058 token = cp_lexer_peek_nth_token (parser->lexer, n);
27061 /* Check for a bitfield declaration. */
27062 if (token->type == CPP_COLON
27063 || (token->type == CPP_NAME
27064 && token == cp_lexer_peek_token (parser->lexer)
27065 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27066 && (named_bitfld = true)))
27068 tree identifier;
27069 tree width;
27070 tree late_attributes = NULL_TREE;
27071 location_t id_location
27072 = cp_lexer_peek_token (parser->lexer)->location;
27074 if (named_bitfld)
27075 identifier = cp_parser_identifier (parser);
27076 else
27077 identifier = NULL_TREE;
27079 /* Look for attributes that apply to the bitfield. */
27080 attributes = cp_parser_attributes_opt (parser);
27082 /* Consume the `:' token. */
27083 cp_lexer_consume_token (parser->lexer);
27085 /* Get the width of the bitfield. */
27086 width = cp_parser_constant_expression (parser, false, NULL,
27087 cxx_dialect >= cxx11);
27089 /* In C++20 and as extension for C++11 and above we allow
27090 default member initializers for bit-fields. */
27091 initializer = NULL_TREE;
27092 if (cxx_dialect >= cxx11
27093 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27094 || cp_lexer_next_token_is (parser->lexer,
27095 CPP_OPEN_BRACE)))
27097 location_t loc
27098 = cp_lexer_peek_token (parser->lexer)->location;
27099 if (cxx_dialect < cxx20
27100 && identifier != NULL_TREE)
27101 pedwarn (loc, OPT_Wc__20_extensions,
27102 "default member initializers for bit-fields "
27103 "only available with %<-std=c++20%> or "
27104 "%<-std=gnu++20%>");
27106 initializer = cp_parser_save_nsdmi (parser);
27107 if (identifier == NULL_TREE)
27109 error_at (loc, "default member initializer for "
27110 "unnamed bit-field");
27111 initializer = NULL_TREE;
27114 else
27116 /* Look for attributes that apply to the bitfield after
27117 the `:' token and width. This is where GCC used to
27118 parse attributes in the past, pedwarn if there is
27119 a std attribute. */
27120 if (cp_next_tokens_can_be_std_attribute_p (parser))
27121 pedwarn (input_location, OPT_Wpedantic,
27122 "ISO C++ allows bit-field attributes only "
27123 "before the %<:%> token");
27125 late_attributes = cp_parser_attributes_opt (parser);
27128 attributes = attr_chainon (attributes, late_attributes);
27130 /* Remember which attributes are prefix attributes and
27131 which are not. */
27132 first_attribute = attributes;
27133 /* Combine the attributes. */
27134 attributes = attr_chainon (prefix_attributes, attributes);
27136 /* Create the bitfield declaration. */
27137 decl = grokbitfield (identifier
27138 ? make_id_declarator (NULL_TREE,
27139 identifier,
27140 sfk_none,
27141 id_location)
27142 : NULL,
27143 &decl_specifiers,
27144 width, initializer,
27145 attributes);
27147 else
27149 cp_declarator *declarator;
27150 tree asm_specification;
27151 int ctor_dtor_or_conv_p;
27152 bool static_p = (decl_specifiers.storage_class == sc_static);
27153 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27154 /* We can't delay parsing for friends,
27155 alias-declarations, and typedefs, even though the
27156 standard seems to require it. */
27157 if (!friend_p
27158 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27159 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27161 /* Parse the declarator. */
27162 declarator
27163 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27164 flags,
27165 &ctor_dtor_or_conv_p,
27166 /*parenthesized_p=*/NULL,
27167 /*member_p=*/true,
27168 friend_p, static_p);
27170 /* If something went wrong parsing the declarator, make sure
27171 that we at least consume some tokens. */
27172 if (declarator == cp_error_declarator)
27174 /* Skip to the end of the statement. */
27175 cp_parser_skip_to_end_of_statement (parser);
27176 /* If the next token is not a semicolon, that is
27177 probably because we just skipped over the body of
27178 a function. So, we consume a semicolon if
27179 present, but do not issue an error message if it
27180 is not present. */
27181 if (cp_lexer_next_token_is (parser->lexer,
27182 CPP_SEMICOLON))
27183 cp_lexer_consume_token (parser->lexer);
27184 goto out;
27187 /* Handle class-scope non-template C++17 deduction guides. */
27188 cp_parser_maybe_adjust_declarator_for_dguide (parser,
27189 &decl_specifiers,
27190 declarator,
27191 &ctor_dtor_or_conv_p);
27193 if (declares_class_or_enum & 2)
27194 cp_parser_check_for_definition_in_return_type
27195 (declarator, decl_specifiers.type,
27196 decl_specifiers.locations[ds_type_spec]);
27198 /* Look for an asm-specification. */
27199 asm_specification = cp_parser_asm_specification_opt (parser);
27200 /* Look for attributes that apply to the declaration. */
27201 attributes = cp_parser_attributes_opt (parser);
27202 /* Remember which attributes are prefix attributes and
27203 which are not. */
27204 first_attribute = attributes;
27205 /* Combine the attributes. */
27206 attributes = attr_chainon (prefix_attributes, attributes);
27208 /* If it's an `=', then we have a constant-initializer or a
27209 pure-specifier. It is not correct to parse the
27210 initializer before registering the member declaration
27211 since the member declaration should be in scope while
27212 its initializer is processed. However, the rest of the
27213 front end does not yet provide an interface that allows
27214 us to handle this correctly. */
27215 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27217 /* In [class.mem]:
27219 A pure-specifier shall be used only in the declaration of
27220 a virtual function.
27222 A member-declarator can contain a constant-initializer
27223 only if it declares a static member of integral or
27224 enumeration type.
27226 Therefore, if the DECLARATOR is for a function, we look
27227 for a pure-specifier; otherwise, we look for a
27228 constant-initializer. When we call `grokfield', it will
27229 perform more stringent semantics checks. */
27230 initializer_token_start = cp_lexer_peek_token (parser->lexer);
27231 declarator->init_loc = initializer_token_start->location;
27232 if (function_declarator_p (declarator)
27233 || (decl_specifiers.type
27234 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27235 && declarator->kind == cdk_id
27236 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27237 == FUNCTION_TYPE)))
27238 initializer = cp_parser_pure_specifier (parser);
27239 else if (decl_specifiers.storage_class != sc_static)
27240 initializer = cp_parser_save_nsdmi (parser);
27241 else if (cxx_dialect >= cxx11)
27243 bool nonconst;
27244 /* Don't require a constant rvalue in C++11, since we
27245 might want a reference constant. We'll enforce
27246 constancy later. */
27247 cp_lexer_consume_token (parser->lexer);
27248 /* Parse the initializer. */
27249 initializer = cp_parser_initializer_clause (parser,
27250 &nonconst);
27252 else
27253 /* Parse the initializer. */
27254 initializer = cp_parser_constant_initializer (parser);
27256 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27257 && !function_declarator_p (declarator))
27259 bool x;
27260 declarator->init_loc
27261 = cp_lexer_peek_token (parser->lexer)->location;
27262 if (decl_specifiers.storage_class != sc_static)
27263 initializer = cp_parser_save_nsdmi (parser);
27264 else
27265 initializer = cp_parser_initializer (parser, &x, &x);
27267 /* Detect invalid bit-field cases such as
27269 int *p : 4;
27270 int &&r : 3;
27272 and similar. */
27273 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27274 /* If there were no type specifiers, it was a
27275 constructor. */
27276 && decl_specifiers.any_type_specifiers_p)
27278 /* This is called for a decent diagnostic only. */
27279 tree d = grokdeclarator (declarator, &decl_specifiers,
27280 BITFIELD, /*initialized=*/false,
27281 &attributes);
27282 if (!error_operand_p (d))
27283 error_at (DECL_SOURCE_LOCATION (d),
27284 "bit-field %qD has non-integral type %qT",
27285 d, TREE_TYPE (d));
27286 cp_parser_skip_to_end_of_statement (parser);
27287 /* Avoid "extra ;" pedwarns. */
27288 if (cp_lexer_next_token_is (parser->lexer,
27289 CPP_SEMICOLON))
27290 cp_lexer_consume_token (parser->lexer);
27291 goto out;
27293 /* Otherwise, there is no initializer. */
27294 else
27295 initializer = NULL_TREE;
27297 /* See if we are probably looking at a function
27298 definition. We are certainly not looking at a
27299 member-declarator. Calling `grokfield' has
27300 side-effects, so we must not do it unless we are sure
27301 that we are looking at a member-declarator. */
27302 if (cp_parser_token_starts_function_definition_p
27303 (cp_lexer_peek_token (parser->lexer)))
27305 /* The grammar does not allow a pure-specifier to be
27306 used when a member function is defined. (It is
27307 possible that this fact is an oversight in the
27308 standard, since a pure function may be defined
27309 outside of the class-specifier. */
27310 if (initializer && initializer_token_start)
27311 error_at (initializer_token_start->location,
27312 "pure-specifier on function-definition");
27313 decl = cp_parser_save_member_function_body (parser,
27314 &decl_specifiers,
27315 declarator,
27316 attributes);
27317 if (parser->fully_implicit_function_template_p)
27318 decl = finish_fully_implicit_template (parser, decl);
27319 /* If the member was not a friend, declare it here. */
27320 if (!friend_p)
27321 finish_member_declaration (decl);
27322 /* Peek at the next token. */
27323 token = cp_lexer_peek_token (parser->lexer);
27324 /* If the next token is a semicolon, consume it. */
27325 if (token->type == CPP_SEMICOLON)
27327 location_t semicolon_loc
27328 = cp_lexer_consume_token (parser->lexer)->location;
27329 gcc_rich_location richloc (semicolon_loc);
27330 richloc.add_fixit_remove ();
27331 warning_at (&richloc, OPT_Wextra_semi,
27332 "extra %<;%> after in-class "
27333 "function definition");
27335 goto out;
27337 else
27338 if (declarator->kind == cdk_function)
27339 declarator->id_loc = token->location;
27340 /* Create the declaration. */
27341 decl = grokfield (declarator, &decl_specifiers,
27342 initializer, /*init_const_expr_p=*/true,
27343 asm_specification, attributes);
27344 if (parser->fully_implicit_function_template_p)
27346 if (friend_p)
27347 finish_fully_implicit_template (parser, 0);
27348 else
27349 decl = finish_fully_implicit_template (parser, decl);
27353 cp_finalize_omp_declare_simd (parser, decl);
27354 cp_finalize_oacc_routine (parser, decl, false);
27356 /* Reset PREFIX_ATTRIBUTES. */
27357 if (attributes != error_mark_node)
27359 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27360 attributes = TREE_CHAIN (attributes);
27361 if (attributes)
27362 TREE_CHAIN (attributes) = NULL_TREE;
27365 /* If there is any qualification still in effect, clear it
27366 now; we will be starting fresh with the next declarator. */
27367 parser->scope = NULL_TREE;
27368 parser->qualifying_scope = NULL_TREE;
27369 parser->object_scope = NULL_TREE;
27370 /* If it's a `,', then there are more declarators. */
27371 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27373 cp_lexer_consume_token (parser->lexer);
27374 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27376 cp_token *token = cp_lexer_previous_token (parser->lexer);
27377 gcc_rich_location richloc (token->location);
27378 richloc.add_fixit_remove ();
27379 error_at (&richloc, "stray %<,%> at end of "
27380 "member declaration");
27383 /* If the next token isn't a `;', then we have a parse error. */
27384 else if (cp_lexer_next_token_is_not (parser->lexer,
27385 CPP_SEMICOLON))
27387 /* The next token might be a ways away from where the
27388 actual semicolon is missing. Find the previous token
27389 and use that for our error position. */
27390 cp_token *token = cp_lexer_previous_token (parser->lexer);
27391 gcc_rich_location richloc (token->location);
27392 richloc.add_fixit_insert_after (";");
27393 error_at (&richloc, "expected %<;%> at end of "
27394 "member declaration");
27396 /* Assume that the user meant to provide a semicolon. If
27397 we were to cp_parser_skip_to_end_of_statement, we might
27398 skip to a semicolon inside a member function definition
27399 and issue nonsensical error messages. */
27400 assume_semicolon = true;
27403 if (decl)
27405 /* Add DECL to the list of members. */
27406 if (!friend_p
27407 /* Explicitly include, eg, NSDMIs, for better error
27408 recovery (c++/58650). */
27409 || !DECL_DECLARES_FUNCTION_P (decl))
27410 finish_member_declaration (decl);
27412 if (DECL_DECLARES_FUNCTION_P (decl))
27413 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
27414 else if (TREE_CODE (decl) == FIELD_DECL
27415 && DECL_INITIAL (decl))
27416 /* Add DECL to the queue of NSDMI to be parsed later. */
27417 vec_safe_push (unparsed_nsdmis, decl);
27420 if (assume_semicolon)
27421 goto out;
27425 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27426 out:
27427 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27428 cp_finalize_omp_declare_simd (parser, &odsd);
27431 /* Parse a pure-specifier.
27433 pure-specifier:
27436 Returns INTEGER_ZERO_NODE if a pure specifier is found.
27437 Otherwise, ERROR_MARK_NODE is returned. */
27439 static tree
27440 cp_parser_pure_specifier (cp_parser* parser)
27442 cp_token *token;
27444 /* Look for the `=' token. */
27445 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27446 return error_mark_node;
27447 /* Look for the `0' token. */
27448 token = cp_lexer_peek_token (parser->lexer);
27450 if (token->type == CPP_EOF
27451 || token->type == CPP_PRAGMA_EOL)
27452 return error_mark_node;
27454 cp_lexer_consume_token (parser->lexer);
27456 /* Accept = default or = delete in c++0x mode. */
27457 if (token->keyword == RID_DEFAULT
27458 || token->keyword == RID_DELETE)
27460 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
27461 return token->u.value;
27464 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
27465 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
27467 cp_parser_error (parser,
27468 "invalid pure specifier (only %<= 0%> is allowed)");
27469 cp_parser_skip_to_end_of_statement (parser);
27470 return error_mark_node;
27472 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27474 error_at (token->location, "templates may not be %<virtual%>");
27475 return error_mark_node;
27478 return integer_zero_node;
27481 /* Parse a constant-initializer.
27483 constant-initializer:
27484 = constant-expression
27486 Returns a representation of the constant-expression. */
27488 static tree
27489 cp_parser_constant_initializer (cp_parser* parser)
27491 /* Look for the `=' token. */
27492 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27493 return error_mark_node;
27495 /* It is invalid to write:
27497 struct S { static const int i = { 7 }; };
27500 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27502 cp_parser_error (parser,
27503 "a brace-enclosed initializer is not allowed here");
27504 /* Consume the opening brace. */
27505 matching_braces braces;
27506 braces.consume_open (parser);
27507 /* Skip the initializer. */
27508 cp_parser_skip_to_closing_brace (parser);
27509 /* Look for the trailing `}'. */
27510 braces.require_close (parser);
27512 return error_mark_node;
27515 return cp_parser_constant_expression (parser);
27518 /* Derived classes [gram.class.derived] */
27520 /* Parse a base-clause.
27522 base-clause:
27523 : base-specifier-list
27525 base-specifier-list:
27526 base-specifier ... [opt]
27527 base-specifier-list , base-specifier ... [opt]
27529 Returns a TREE_LIST representing the base-classes, in the order in
27530 which they were declared. The representation of each node is as
27531 described by cp_parser_base_specifier.
27533 In the case that no bases are specified, this function will return
27534 NULL_TREE, not ERROR_MARK_NODE. */
27536 static tree
27537 cp_parser_base_clause (cp_parser* parser)
27539 tree bases = NULL_TREE;
27541 /* Look for the `:' that begins the list. */
27542 cp_parser_require (parser, CPP_COLON, RT_COLON);
27544 /* Scan the base-specifier-list. */
27545 while (true)
27547 cp_token *token;
27548 tree base;
27549 bool pack_expansion_p = false;
27551 /* Look for the base-specifier. */
27552 base = cp_parser_base_specifier (parser);
27553 /* Look for the (optional) ellipsis. */
27554 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27556 /* Consume the `...'. */
27557 cp_lexer_consume_token (parser->lexer);
27559 pack_expansion_p = true;
27562 /* Add BASE to the front of the list. */
27563 if (base && base != error_mark_node)
27565 if (pack_expansion_p)
27566 /* Make this a pack expansion type. */
27567 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
27569 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
27571 TREE_CHAIN (base) = bases;
27572 bases = base;
27575 /* Peek at the next token. */
27576 token = cp_lexer_peek_token (parser->lexer);
27577 /* If it's not a comma, then the list is complete. */
27578 if (token->type != CPP_COMMA)
27579 break;
27580 /* Consume the `,'. */
27581 cp_lexer_consume_token (parser->lexer);
27584 /* PARSER->SCOPE may still be non-NULL at this point, if the last
27585 base class had a qualified name. However, the next name that
27586 appears is certainly not qualified. */
27587 parser->scope = NULL_TREE;
27588 parser->qualifying_scope = NULL_TREE;
27589 parser->object_scope = NULL_TREE;
27591 return nreverse (bases);
27594 /* Parse a base-specifier.
27596 base-specifier:
27597 :: [opt] nested-name-specifier [opt] class-name
27598 virtual access-specifier [opt] :: [opt] nested-name-specifier
27599 [opt] class-name
27600 access-specifier virtual [opt] :: [opt] nested-name-specifier
27601 [opt] class-name
27603 Returns a TREE_LIST. The TREE_PURPOSE will be one of
27604 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
27605 indicate the specifiers provided. The TREE_VALUE will be a TYPE
27606 (or the ERROR_MARK_NODE) indicating the type that was specified. */
27608 static tree
27609 cp_parser_base_specifier (cp_parser* parser)
27611 cp_token *token;
27612 bool done = false;
27613 bool virtual_p = false;
27614 bool duplicate_virtual_error_issued_p = false;
27615 bool duplicate_access_error_issued_p = false;
27616 bool class_scope_p, template_p;
27617 tree access = access_default_node;
27618 tree type;
27620 /* Process the optional `virtual' and `access-specifier'. */
27621 while (!done)
27623 /* Peek at the next token. */
27624 token = cp_lexer_peek_token (parser->lexer);
27625 /* Process `virtual'. */
27626 switch (token->keyword)
27628 case RID_VIRTUAL:
27629 /* If `virtual' appears more than once, issue an error. */
27630 if (virtual_p && !duplicate_virtual_error_issued_p)
27632 cp_parser_error (parser,
27633 "%<virtual%> specified more than once in base-specifier");
27634 duplicate_virtual_error_issued_p = true;
27637 virtual_p = true;
27639 /* Consume the `virtual' token. */
27640 cp_lexer_consume_token (parser->lexer);
27642 break;
27644 case RID_PUBLIC:
27645 case RID_PROTECTED:
27646 case RID_PRIVATE:
27647 /* If more than one access specifier appears, issue an
27648 error. */
27649 if (access != access_default_node
27650 && !duplicate_access_error_issued_p)
27652 cp_parser_error (parser,
27653 "more than one access specifier in base-specifier");
27654 duplicate_access_error_issued_p = true;
27657 access = ridpointers[(int) token->keyword];
27659 /* Consume the access-specifier. */
27660 cp_lexer_consume_token (parser->lexer);
27662 break;
27664 default:
27665 done = true;
27666 break;
27669 /* It is not uncommon to see programs mechanically, erroneously, use
27670 the 'typename' keyword to denote (dependent) qualified types
27671 as base classes. */
27672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27674 token = cp_lexer_peek_token (parser->lexer);
27675 if (!processing_template_decl)
27676 error_at (token->location,
27677 "keyword %<typename%> not allowed outside of templates");
27678 else
27679 error_at (token->location,
27680 "keyword %<typename%> not allowed in this context "
27681 "(the base class is implicitly a type)");
27682 cp_lexer_consume_token (parser->lexer);
27685 /* Look for the optional `::' operator. */
27686 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
27687 /* Look for the nested-name-specifier. The simplest way to
27688 implement:
27690 [temp.res]
27692 The keyword `typename' is not permitted in a base-specifier or
27693 mem-initializer; in these contexts a qualified name that
27694 depends on a template-parameter is implicitly assumed to be a
27695 type name.
27697 is to pretend that we have seen the `typename' keyword at this
27698 point. */
27699 cp_parser_nested_name_specifier_opt (parser,
27700 /*typename_keyword_p=*/true,
27701 /*check_dependency_p=*/true,
27702 /*type_p=*/true,
27703 /*is_declaration=*/true);
27704 /* If the base class is given by a qualified name, assume that names
27705 we see are type names or templates, as appropriate. */
27706 class_scope_p = (parser->scope && TYPE_P (parser->scope));
27707 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
27709 if (!parser->scope
27710 && cp_lexer_next_token_is_decltype (parser->lexer))
27711 /* DR 950 allows decltype as a base-specifier. */
27712 type = cp_parser_decltype (parser);
27713 else
27715 /* Otherwise, look for the class-name. */
27716 type = cp_parser_class_name (parser,
27717 class_scope_p,
27718 template_p,
27719 typename_type,
27720 /*check_dependency_p=*/true,
27721 /*class_head_p=*/false,
27722 /*is_declaration=*/true);
27723 type = TREE_TYPE (type);
27726 if (type == error_mark_node)
27727 return error_mark_node;
27729 return finish_base_specifier (type, access, virtual_p);
27732 /* Exception handling [gram.exception] */
27734 /* Save the tokens that make up the noexcept-specifier for a member-function.
27735 Returns a DEFERRED_PARSE. */
27737 static tree
27738 cp_parser_save_noexcept (cp_parser *parser)
27740 cp_token *first = parser->lexer->next_token;
27741 /* We want everything up to, including, the final ')'. */
27742 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
27743 cp_token *last = parser->lexer->next_token;
27745 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
27746 to carry the information we will need. */
27747 tree expr = make_node (DEFERRED_PARSE);
27748 /* Save away the noexcept-specifier; we will process it when the
27749 class is complete. */
27750 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
27751 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
27752 expr = build_tree_list (expr, NULL_TREE);
27753 return expr;
27756 /* Used for late processing of noexcept-specifiers of member-functions.
27757 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
27758 we saved for later; parse it now. DECL is the declaration of the
27759 member function. */
27761 static tree
27762 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
27764 /* Make sure we've gotten something that hasn't been parsed yet. */
27765 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
27767 push_unparsed_function_queues (parser);
27769 /* Push the saved tokens for the noexcept-specifier onto the parser's
27770 lexer stack. */
27771 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
27772 cp_parser_push_lexer_for_tokens (parser, tokens);
27774 /* Parse the cached noexcept-specifier. */
27775 tree parsed_arg
27776 = cp_parser_noexcept_specification_opt (parser,
27777 CP_PARSER_FLAGS_NONE,
27778 /*require_constexpr=*/true,
27779 /*consumed_expr=*/NULL,
27780 /*return_cond=*/false);
27782 /* Revert to the main lexer. */
27783 cp_parser_pop_lexer (parser);
27785 /* Restore the queue. */
27786 pop_unparsed_function_queues (parser);
27788 /* And we're done. */
27789 return parsed_arg;
27792 /* Perform late checking of overriding function with respect to their
27793 noexcept-specifiers. TYPE is the class and FNDECL is the function
27794 that potentially overrides some virtual function with the same
27795 signature. */
27797 static void
27798 noexcept_override_late_checks (tree type, tree fndecl)
27800 tree binfo = TYPE_BINFO (type);
27801 tree base_binfo;
27803 if (DECL_STATIC_FUNCTION_P (fndecl))
27804 return;
27806 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
27808 tree basetype = BINFO_TYPE (base_binfo);
27810 if (!TYPE_POLYMORPHIC_P (basetype))
27811 continue;
27813 tree fn = look_for_overrides_here (basetype, fndecl);
27814 if (fn)
27815 maybe_check_overriding_exception_spec (fndecl, fn);
27819 /* Parse an (optional) noexcept-specification.
27821 noexcept-specification:
27822 noexcept ( constant-expression ) [opt]
27824 If no noexcept-specification is present, returns NULL_TREE.
27825 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
27826 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
27827 there are no parentheses. CONSUMED_EXPR will be set accordingly.
27828 Otherwise, returns a noexcept specification unless RETURN_COND is true,
27829 in which case a boolean condition is returned instead. The parser flags
27830 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
27831 the (member) function is `const'. */
27833 static tree
27834 cp_parser_noexcept_specification_opt (cp_parser* parser,
27835 cp_parser_flags flags,
27836 bool require_constexpr,
27837 bool* consumed_expr,
27838 bool return_cond)
27840 cp_token *token;
27841 const char *saved_message;
27843 /* Peek at the next token. */
27844 token = cp_lexer_peek_token (parser->lexer);
27846 /* Is it a noexcept-specification? */
27847 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
27849 tree expr;
27851 /* [class.mem]/6 says that a noexcept-specifer (within the
27852 member-specification of the class) is a complete-class context of
27853 a class. So, if the noexcept-specifier has the optional expression,
27854 just save the tokens, and reparse this after we're done with the
27855 class. */
27857 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
27858 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
27859 /* No need to delay parsing for a number literal or true/false. */
27860 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
27861 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27862 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
27863 && at_class_scope_p ()
27864 && TYPE_BEING_DEFINED (current_class_type)
27865 && !LAMBDA_TYPE_P (current_class_type))
27866 return cp_parser_save_noexcept (parser);
27868 cp_lexer_consume_token (parser->lexer);
27870 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27872 matching_parens parens;
27873 parens.consume_open (parser);
27875 if (require_constexpr)
27877 /* Types may not be defined in an exception-specification. */
27878 saved_message = parser->type_definition_forbidden_message;
27879 parser->type_definition_forbidden_message
27880 = G_("types may not be defined in an exception-specification");
27882 bool non_constant_p;
27883 expr
27884 = cp_parser_constant_expression (parser,
27885 /*allow_non_constant=*/true,
27886 &non_constant_p);
27887 if (non_constant_p
27888 && !require_potential_rvalue_constant_expression (expr))
27890 expr = NULL_TREE;
27891 return_cond = true;
27894 /* Restore the saved message. */
27895 parser->type_definition_forbidden_message = saved_message;
27897 else
27899 expr = cp_parser_expression (parser);
27900 *consumed_expr = true;
27903 parens.require_close (parser);
27905 else
27907 expr = boolean_true_node;
27908 if (!require_constexpr)
27909 *consumed_expr = false;
27912 /* We cannot build a noexcept-spec right away because this will check
27913 that expr is a constexpr. */
27914 if (!return_cond)
27915 return build_noexcept_spec (expr, tf_warning_or_error);
27916 else
27917 return expr;
27919 else
27920 return NULL_TREE;
27923 /* Parse an (optional) exception-specification.
27925 exception-specification:
27926 throw ( type-id-list [opt] )
27928 Returns a TREE_LIST representing the exception-specification. The
27929 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
27930 control parsing. QUALS are qualifiers indicating whether the (member)
27931 function is `const'. */
27933 static tree
27934 cp_parser_exception_specification_opt (cp_parser* parser,
27935 cp_parser_flags flags)
27937 cp_token *token;
27938 tree type_id_list;
27939 const char *saved_message;
27941 /* Peek at the next token. */
27942 token = cp_lexer_peek_token (parser->lexer);
27944 /* Is it a noexcept-specification? */
27945 type_id_list
27946 = cp_parser_noexcept_specification_opt (parser, flags,
27947 /*require_constexpr=*/true,
27948 /*consumed_expr=*/NULL,
27949 /*return_cond=*/false);
27950 if (type_id_list != NULL_TREE)
27951 return type_id_list;
27953 /* If it's not `throw', then there's no exception-specification. */
27954 if (!cp_parser_is_keyword (token, RID_THROW))
27955 return NULL_TREE;
27957 location_t loc = token->location;
27959 /* Consume the `throw'. */
27960 cp_lexer_consume_token (parser->lexer);
27962 /* Look for the `('. */
27963 matching_parens parens;
27964 parens.require_open (parser);
27966 /* Peek at the next token. */
27967 token = cp_lexer_peek_token (parser->lexer);
27968 /* If it's not a `)', then there is a type-id-list. */
27969 if (token->type != CPP_CLOSE_PAREN)
27971 /* Types may not be defined in an exception-specification. */
27972 saved_message = parser->type_definition_forbidden_message;
27973 parser->type_definition_forbidden_message
27974 = G_("types may not be defined in an exception-specification");
27975 /* Parse the type-id-list. */
27976 type_id_list = cp_parser_type_id_list (parser);
27977 /* Restore the saved message. */
27978 parser->type_definition_forbidden_message = saved_message;
27980 if (cxx_dialect >= cxx17)
27982 error_at (loc, "ISO C++17 does not allow dynamic exception "
27983 "specifications");
27984 type_id_list = NULL_TREE;
27986 else if (cxx_dialect >= cxx11)
27987 warning_at (loc, OPT_Wdeprecated,
27988 "dynamic exception specifications are deprecated in "
27989 "C++11");
27991 /* In C++17, throw() is equivalent to noexcept (true). throw()
27992 is deprecated in C++11 and above as well, but is still widely used,
27993 so don't warn about it yet. */
27994 else if (cxx_dialect >= cxx17)
27995 type_id_list = noexcept_true_spec;
27996 else
27997 type_id_list = empty_except_spec;
27999 /* Look for the `)'. */
28000 parens.require_close (parser);
28002 return type_id_list;
28005 /* Parse an (optional) type-id-list.
28007 type-id-list:
28008 type-id ... [opt]
28009 type-id-list , type-id ... [opt]
28011 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28012 in the order that the types were presented. */
28014 static tree
28015 cp_parser_type_id_list (cp_parser* parser)
28017 tree types = NULL_TREE;
28019 while (true)
28021 cp_token *token;
28022 tree type;
28024 token = cp_lexer_peek_token (parser->lexer);
28026 /* Get the next type-id. */
28027 type = cp_parser_type_id (parser);
28028 /* Check for invalid 'auto'. */
28029 if (flag_concepts && type_uses_auto (type))
28031 error_at (token->location,
28032 "invalid use of %<auto%> in exception-specification");
28033 type = error_mark_node;
28035 /* Parse the optional ellipsis. */
28036 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28038 /* Consume the `...'. */
28039 cp_lexer_consume_token (parser->lexer);
28041 /* Turn the type into a pack expansion expression. */
28042 type = make_pack_expansion (type);
28044 /* Add it to the list. */
28045 types = add_exception_specifier (types, type, /*complain=*/1);
28046 /* Peek at the next token. */
28047 token = cp_lexer_peek_token (parser->lexer);
28048 /* If it is not a `,', we are done. */
28049 if (token->type != CPP_COMMA)
28050 break;
28051 /* Consume the `,'. */
28052 cp_lexer_consume_token (parser->lexer);
28055 return nreverse (types);
28058 /* Parse a try-block.
28060 try-block:
28061 try compound-statement handler-seq */
28063 static tree
28064 cp_parser_try_block (cp_parser* parser)
28066 tree try_block;
28068 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28069 if (parser->in_function_body
28070 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28071 && cxx_dialect < cxx20)
28072 pedwarn (input_location, OPT_Wc__20_extensions,
28073 "%<try%> in %<constexpr%> function only "
28074 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28076 try_block = begin_try_block ();
28077 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28078 finish_try_block (try_block);
28079 cp_parser_handler_seq (parser);
28080 finish_handler_sequence (try_block);
28082 return try_block;
28085 /* Parse a function-try-block.
28087 function-try-block:
28088 try ctor-initializer [opt] function-body handler-seq */
28090 static void
28091 cp_parser_function_try_block (cp_parser* parser)
28093 tree compound_stmt;
28094 tree try_block;
28096 /* Look for the `try' keyword. */
28097 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28098 return;
28099 /* Let the rest of the front end know where we are. */
28100 try_block = begin_function_try_block (&compound_stmt);
28101 /* Parse the function-body. */
28102 cp_parser_ctor_initializer_opt_and_function_body
28103 (parser, /*in_function_try_block=*/true);
28104 /* We're done with the `try' part. */
28105 finish_function_try_block (try_block);
28106 /* Parse the handlers. */
28107 cp_parser_handler_seq (parser);
28108 /* We're done with the handlers. */
28109 finish_function_handler_sequence (try_block, compound_stmt);
28112 /* Parse a handler-seq.
28114 handler-seq:
28115 handler handler-seq [opt] */
28117 static void
28118 cp_parser_handler_seq (cp_parser* parser)
28120 while (true)
28122 cp_token *token;
28124 /* Parse the handler. */
28125 cp_parser_handler (parser);
28126 /* Peek at the next token. */
28127 token = cp_lexer_peek_token (parser->lexer);
28128 /* If it's not `catch' then there are no more handlers. */
28129 if (!cp_parser_is_keyword (token, RID_CATCH))
28130 break;
28134 /* Parse a handler.
28136 handler:
28137 catch ( exception-declaration ) compound-statement */
28139 static void
28140 cp_parser_handler (cp_parser* parser)
28142 tree handler;
28143 tree declaration;
28145 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28146 handler = begin_handler ();
28147 matching_parens parens;
28148 parens.require_open (parser);
28149 declaration = cp_parser_exception_declaration (parser);
28150 finish_handler_parms (declaration, handler);
28151 parens.require_close (parser);
28152 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28153 finish_handler (handler);
28156 /* Parse an exception-declaration.
28158 exception-declaration:
28159 type-specifier-seq declarator
28160 type-specifier-seq abstract-declarator
28161 type-specifier-seq
28164 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28165 ellipsis variant is used. */
28167 static tree
28168 cp_parser_exception_declaration (cp_parser* parser)
28170 cp_decl_specifier_seq type_specifiers;
28171 cp_declarator *declarator;
28172 const char *saved_message;
28174 /* If it's an ellipsis, it's easy to handle. */
28175 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28177 /* Consume the `...' token. */
28178 cp_lexer_consume_token (parser->lexer);
28179 return NULL_TREE;
28182 /* Types may not be defined in exception-declarations. */
28183 saved_message = parser->type_definition_forbidden_message;
28184 parser->type_definition_forbidden_message
28185 = G_("types may not be defined in exception-declarations");
28187 /* Parse the type-specifier-seq. */
28188 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28189 /*is_declaration=*/true,
28190 /*is_trailing_return=*/false,
28191 &type_specifiers);
28192 /* If it's a `)', then there is no declarator. */
28193 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28194 declarator = NULL;
28195 else
28196 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28197 CP_PARSER_FLAGS_NONE,
28198 /*ctor_dtor_or_conv_p=*/NULL,
28199 /*parenthesized_p=*/NULL,
28200 /*member_p=*/false,
28201 /*friend_p=*/false,
28202 /*static_p=*/false);
28204 /* Restore the saved message. */
28205 parser->type_definition_forbidden_message = saved_message;
28207 if (!type_specifiers.any_specifiers_p)
28208 return error_mark_node;
28210 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28213 /* Parse a throw-expression.
28215 throw-expression:
28216 throw assignment-expression [opt]
28218 Returns a THROW_EXPR representing the throw-expression. */
28220 static tree
28221 cp_parser_throw_expression (cp_parser* parser)
28223 tree expression;
28224 cp_token* token;
28225 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28227 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28228 token = cp_lexer_peek_token (parser->lexer);
28229 /* Figure out whether or not there is an assignment-expression
28230 following the "throw" keyword. */
28231 if (token->type == CPP_COMMA
28232 || token->type == CPP_SEMICOLON
28233 || token->type == CPP_CLOSE_PAREN
28234 || token->type == CPP_CLOSE_SQUARE
28235 || token->type == CPP_CLOSE_BRACE
28236 || token->type == CPP_COLON)
28237 expression = NULL_TREE;
28238 else
28239 expression = cp_parser_assignment_expression (parser);
28241 /* Construct a location e.g.:
28242 throw x
28243 ^~~~~~~
28244 with caret == start at the start of the "throw" token, and
28245 the end at the end of the final token we consumed. */
28246 location_t combined_loc = make_location (start_loc, start_loc,
28247 parser->lexer);
28248 expression = build_throw (combined_loc, expression);
28250 return expression;
28253 /* Parse a yield-expression.
28255 yield-expression:
28256 co_yield assignment-expression
28257 co_yield braced-init-list
28259 Returns a CO_YIELD_EXPR representing the yield-expression. */
28261 static tree
28262 cp_parser_yield_expression (cp_parser* parser)
28264 tree expr;
28266 cp_token *token = cp_lexer_peek_token (parser->lexer);
28267 location_t kw_loc = token->location; /* Save for later. */
28269 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28271 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28273 bool expr_non_constant_p;
28274 cp_lexer_set_source_position (parser->lexer);
28275 /* ??? : probably a moot point? */
28276 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28277 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
28279 else
28280 expr = cp_parser_assignment_expression (parser);
28282 if (expr == error_mark_node)
28283 return expr;
28285 return finish_co_yield_expr (kw_loc, expr);
28288 /* GNU Extensions */
28290 /* Parse an (optional) asm-specification.
28292 asm-specification:
28293 asm ( string-literal )
28295 If the asm-specification is present, returns a STRING_CST
28296 corresponding to the string-literal. Otherwise, returns
28297 NULL_TREE. */
28299 static tree
28300 cp_parser_asm_specification_opt (cp_parser* parser)
28302 cp_token *token;
28303 tree asm_specification;
28305 /* Peek at the next token. */
28306 token = cp_lexer_peek_token (parser->lexer);
28307 /* If the next token isn't the `asm' keyword, then there's no
28308 asm-specification. */
28309 if (!cp_parser_is_keyword (token, RID_ASM))
28310 return NULL_TREE;
28312 /* Consume the `asm' token. */
28313 cp_lexer_consume_token (parser->lexer);
28314 /* Look for the `('. */
28315 matching_parens parens;
28316 parens.require_open (parser);
28318 /* Look for the string-literal. */
28319 asm_specification = cp_parser_string_literal (parser, false, false);
28321 /* Look for the `)'. */
28322 parens.require_close (parser);
28324 return asm_specification;
28327 /* Parse an asm-operand-list.
28329 asm-operand-list:
28330 asm-operand
28331 asm-operand-list , asm-operand
28333 asm-operand:
28334 string-literal ( expression )
28335 [ string-literal ] string-literal ( expression )
28337 Returns a TREE_LIST representing the operands. The TREE_VALUE of
28338 each node is the expression. The TREE_PURPOSE is itself a
28339 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28340 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28341 is a STRING_CST for the string literal before the parenthesis. Returns
28342 ERROR_MARK_NODE if any of the operands are invalid. */
28344 static tree
28345 cp_parser_asm_operand_list (cp_parser* parser)
28347 tree asm_operands = NULL_TREE;
28348 bool invalid_operands = false;
28350 while (true)
28352 tree string_literal;
28353 tree expression;
28354 tree name;
28356 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28358 /* Consume the `[' token. */
28359 cp_lexer_consume_token (parser->lexer);
28360 /* Read the operand name. */
28361 name = cp_parser_identifier (parser);
28362 if (name != error_mark_node)
28363 name = build_string (IDENTIFIER_LENGTH (name),
28364 IDENTIFIER_POINTER (name));
28365 /* Look for the closing `]'. */
28366 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28368 else
28369 name = NULL_TREE;
28370 /* Look for the string-literal. */
28371 string_literal = cp_parser_string_literal (parser, false, false);
28373 /* Look for the `('. */
28374 matching_parens parens;
28375 parens.require_open (parser);
28376 /* Parse the expression. */
28377 expression = cp_parser_expression (parser);
28378 /* Look for the `)'. */
28379 parens.require_close (parser);
28381 if (name == error_mark_node
28382 || string_literal == error_mark_node
28383 || expression == error_mark_node)
28384 invalid_operands = true;
28386 /* Add this operand to the list. */
28387 asm_operands = tree_cons (build_tree_list (name, string_literal),
28388 expression,
28389 asm_operands);
28390 /* If the next token is not a `,', there are no more
28391 operands. */
28392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28393 break;
28394 /* Consume the `,'. */
28395 cp_lexer_consume_token (parser->lexer);
28398 return invalid_operands ? error_mark_node : nreverse (asm_operands);
28401 /* Parse an asm-clobber-list.
28403 asm-clobber-list:
28404 string-literal
28405 asm-clobber-list , string-literal
28407 Returns a TREE_LIST, indicating the clobbers in the order that they
28408 appeared. The TREE_VALUE of each node is a STRING_CST. */
28410 static tree
28411 cp_parser_asm_clobber_list (cp_parser* parser)
28413 tree clobbers = NULL_TREE;
28415 while (true)
28417 tree string_literal;
28419 /* Look for the string literal. */
28420 string_literal = cp_parser_string_literal (parser, false, false);
28421 /* Add it to the list. */
28422 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
28423 /* If the next token is not a `,', then the list is
28424 complete. */
28425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28426 break;
28427 /* Consume the `,' token. */
28428 cp_lexer_consume_token (parser->lexer);
28431 return clobbers;
28434 /* Parse an asm-label-list.
28436 asm-label-list:
28437 identifier
28438 asm-label-list , identifier
28440 Returns a TREE_LIST, indicating the labels in the order that they
28441 appeared. The TREE_VALUE of each node is a label. */
28443 static tree
28444 cp_parser_asm_label_list (cp_parser* parser)
28446 tree labels = NULL_TREE;
28448 while (true)
28450 tree identifier, label, name;
28452 /* Look for the identifier. */
28453 identifier = cp_parser_identifier (parser);
28454 if (!error_operand_p (identifier))
28456 label = lookup_label (identifier);
28457 if (TREE_CODE (label) == LABEL_DECL)
28459 TREE_USED (label) = 1;
28460 check_goto (label);
28461 name = build_string (IDENTIFIER_LENGTH (identifier),
28462 IDENTIFIER_POINTER (identifier));
28463 labels = tree_cons (name, label, labels);
28466 /* If the next token is not a `,', then the list is
28467 complete. */
28468 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28469 break;
28470 /* Consume the `,' token. */
28471 cp_lexer_consume_token (parser->lexer);
28474 return nreverse (labels);
28477 /* Return TRUE iff the next tokens in the stream are possibly the
28478 beginning of a GNU extension attribute. */
28480 static bool
28481 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
28483 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
28486 /* Return TRUE iff the next tokens in the stream are possibly the
28487 beginning of a standard C++-11 attribute specifier. */
28489 static bool
28490 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
28492 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
28495 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28496 beginning of a standard C++-11 attribute specifier. */
28498 static bool
28499 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
28501 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28503 return (cxx_dialect >= cxx11
28504 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
28505 || (token->type == CPP_OPEN_SQUARE
28506 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
28507 && token->type == CPP_OPEN_SQUARE)));
28510 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28511 beginning of a GNU extension attribute. */
28513 static bool
28514 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
28516 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28518 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
28521 /* Return true iff the next tokens can be the beginning of either a
28522 GNU attribute list, or a standard C++11 attribute sequence. */
28524 static bool
28525 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
28527 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
28528 || cp_next_tokens_can_be_std_attribute_p (parser));
28531 /* Return true iff the next Nth tokens can be the beginning of either
28532 a GNU attribute list, or a standard C++11 attribute sequence. */
28534 static bool
28535 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
28537 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
28538 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
28541 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
28542 of GNU attributes, or return NULL. */
28544 static tree
28545 cp_parser_attributes_opt (cp_parser *parser)
28547 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
28548 return cp_parser_gnu_attributes_opt (parser);
28549 return cp_parser_std_attribute_spec_seq (parser);
28552 /* Parse an (optional) series of attributes.
28554 attributes:
28555 attributes attribute
28557 attribute:
28558 __attribute__ (( attribute-list [opt] ))
28560 The return value is as for cp_parser_gnu_attribute_list. */
28562 static tree
28563 cp_parser_gnu_attributes_opt (cp_parser* parser)
28565 tree attributes = NULL_TREE;
28567 auto cleanup = make_temp_override
28568 (parser->auto_is_implicit_function_template_parm_p, false);
28570 while (true)
28572 cp_token *token;
28573 tree attribute_list;
28574 bool ok = true;
28576 /* Peek at the next token. */
28577 token = cp_lexer_peek_token (parser->lexer);
28578 /* If it's not `__attribute__', then we're done. */
28579 if (token->keyword != RID_ATTRIBUTE)
28580 break;
28582 /* Consume the `__attribute__' keyword. */
28583 cp_lexer_consume_token (parser->lexer);
28584 /* Look for the two `(' tokens. */
28585 matching_parens outer_parens;
28586 if (!outer_parens.require_open (parser))
28587 ok = false;
28588 matching_parens inner_parens;
28589 if (!inner_parens.require_open (parser))
28590 ok = false;
28592 /* Peek at the next token. */
28593 token = cp_lexer_peek_token (parser->lexer);
28594 if (token->type != CPP_CLOSE_PAREN)
28595 /* Parse the attribute-list. */
28596 attribute_list = cp_parser_gnu_attribute_list (parser);
28597 else
28598 /* If the next token is a `)', then there is no attribute
28599 list. */
28600 attribute_list = NULL;
28602 /* Look for the two `)' tokens. */
28603 if (!inner_parens.require_close (parser))
28604 ok = false;
28605 if (!outer_parens.require_close (parser))
28606 ok = false;
28607 if (!ok)
28608 cp_parser_skip_to_end_of_statement (parser);
28610 /* Add these new attributes to the list. */
28611 attributes = attr_chainon (attributes, attribute_list);
28614 return attributes;
28617 /* Parse a GNU attribute-list.
28619 attribute-list:
28620 attribute
28621 attribute-list , attribute
28623 attribute:
28624 identifier
28625 identifier ( identifier )
28626 identifier ( identifier , expression-list )
28627 identifier ( expression-list )
28629 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
28630 to an attribute. The TREE_PURPOSE of each node is the identifier
28631 indicating which attribute is in use. The TREE_VALUE represents
28632 the arguments, if any. */
28634 static tree
28635 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
28637 tree attribute_list = NULL_TREE;
28638 bool save_translate_strings_p = parser->translate_strings_p;
28640 /* Don't create wrapper nodes within attributes: the
28641 handlers don't know how to handle them. */
28642 auto_suppress_location_wrappers sentinel;
28644 parser->translate_strings_p = false;
28645 while (true)
28647 cp_token *token;
28648 tree identifier;
28649 tree attribute;
28651 /* Look for the identifier. We also allow keywords here; for
28652 example `__attribute__ ((const))' is legal. */
28653 token = cp_lexer_peek_token (parser->lexer);
28654 if (token->type == CPP_NAME
28655 || token->type == CPP_KEYWORD)
28657 tree arguments = NULL_TREE;
28659 /* Consume the token, but save it since we need it for the
28660 SIMD enabled function parsing. */
28661 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
28663 /* Save away the identifier that indicates which attribute
28664 this is. */
28665 identifier = (token->type == CPP_KEYWORD)
28666 /* For keywords, use the canonical spelling, not the
28667 parsed identifier. */
28668 ? ridpointers[(int) token->keyword]
28669 : id_token->u.value;
28671 identifier = canonicalize_attr_name (identifier);
28672 attribute = build_tree_list (identifier, NULL_TREE);
28674 /* Peek at the next token. */
28675 token = cp_lexer_peek_token (parser->lexer);
28676 /* If it's an `(', then parse the attribute arguments. */
28677 if (token->type == CPP_OPEN_PAREN)
28679 vec<tree, va_gc> *vec;
28680 int attr_flag = (attribute_takes_identifier_p (identifier)
28681 ? id_attr : normal_attr);
28682 vec = cp_parser_parenthesized_expression_list
28683 (parser, attr_flag, /*cast_p=*/false,
28684 /*allow_expansion_p=*/false,
28685 /*non_constant_p=*/NULL);
28686 if (vec == NULL)
28687 arguments = error_mark_node;
28688 else
28690 arguments = build_tree_list_vec (vec);
28691 release_tree_vector (vec);
28693 /* Save the arguments away. */
28694 TREE_VALUE (attribute) = arguments;
28697 if (arguments != error_mark_node)
28699 /* Add this attribute to the list. */
28700 TREE_CHAIN (attribute) = attribute_list;
28701 attribute_list = attribute;
28704 token = cp_lexer_peek_token (parser->lexer);
28706 /* Unless EXACTLY_ONE is set look for more attributes.
28707 If the next token isn't a `,', we're done. */
28708 if (exactly_one || token->type != CPP_COMMA)
28709 break;
28711 /* Consume the comma and keep going. */
28712 cp_lexer_consume_token (parser->lexer);
28714 parser->translate_strings_p = save_translate_strings_p;
28716 /* We built up the list in reverse order. */
28717 return nreverse (attribute_list);
28720 /* Parse arguments of omp::directive attribute.
28722 ( directive-name ,[opt] clause-list[opt] )
28724 For directive just remember the first/last tokens for subsequent
28725 parsing. */
28727 static void
28728 cp_parser_omp_directive_args (cp_parser *parser, tree attribute)
28730 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
28731 if (first->type == CPP_CLOSE_PAREN)
28733 cp_lexer_consume_token (parser->lexer);
28734 error_at (first->location, "expected OpenMP directive name");
28735 cp_lexer_consume_token (parser->lexer);
28736 TREE_VALUE (attribute) = NULL_TREE;
28737 return;
28739 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
28740 if (n == 1)
28742 cp_lexer_consume_token (parser->lexer);
28743 error_at (first->location, "expected attribute argument as balanced "
28744 "token sequence");
28745 TREE_VALUE (attribute) = NULL_TREE;
28746 return;
28748 for (n = n - 2; n; --n)
28749 cp_lexer_consume_token (parser->lexer);
28750 cp_token *last = cp_lexer_peek_token (parser->lexer);
28751 cp_lexer_consume_token (parser->lexer);
28752 tree arg = make_node (DEFERRED_PARSE);
28753 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
28754 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
28755 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
28758 /* Parse arguments of omp::sequence attribute.
28760 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
28762 static void
28763 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
28765 matching_parens parens;
28766 parens.consume_open (parser);
28769 cp_token *token = cp_lexer_peek_token (parser->lexer);
28770 if (token->type == CPP_NAME
28771 && token->u.value == omp_identifier
28772 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
28774 cp_lexer_consume_token (parser->lexer);
28775 cp_lexer_consume_token (parser->lexer);
28776 token = cp_lexer_peek_token (parser->lexer);
28778 bool directive = false;
28779 const char *p;
28780 if (token->type != CPP_NAME)
28781 p = "";
28782 else
28783 p = IDENTIFIER_POINTER (token->u.value);
28784 if (strcmp (p, "directive") == 0)
28785 directive = true;
28786 else if (strcmp (p, "sequence") != 0)
28788 error_at (token->location, "expected %<directive%> or %<sequence%>");
28789 cp_parser_skip_to_closing_parenthesis (parser,
28790 /*recovering=*/true,
28791 /*or_comma=*/true,
28792 /*consume_paren=*/false);
28793 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28794 break;
28795 cp_lexer_consume_token (parser->lexer);
28797 cp_lexer_consume_token (parser->lexer);
28798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
28799 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
28800 UNKNOWN_LOCATION);
28801 else if (directive)
28802 cp_parser_omp_directive_args (parser, attribute);
28803 else
28804 cp_parser_omp_sequence_args (parser, attribute);
28805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28806 break;
28807 cp_lexer_consume_token (parser->lexer);
28809 while (1);
28810 if (!parens.require_close (parser))
28811 cp_parser_skip_to_closing_parenthesis (parser, true, false,
28812 /*consume_paren=*/true);
28815 /* Parse a standard C++11 attribute.
28817 The returned representation is a TREE_LIST which TREE_PURPOSE is
28818 the scoped name of the attribute, and the TREE_VALUE is its
28819 arguments list.
28821 Note that the scoped name of the attribute is itself a TREE_LIST
28822 which TREE_PURPOSE is the namespace of the attribute, and
28823 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
28824 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
28825 and which TREE_PURPOSE is directly the attribute name.
28827 Clients of the attribute code should use get_attribute_namespace
28828 and get_attribute_name to get the actual namespace and name of
28829 attributes, regardless of their being GNU or C++11 attributes.
28831 attribute:
28832 attribute-token attribute-argument-clause [opt]
28834 attribute-token:
28835 identifier
28836 attribute-scoped-token
28838 attribute-scoped-token:
28839 attribute-namespace :: identifier
28841 attribute-namespace:
28842 identifier
28844 attribute-argument-clause:
28845 ( balanced-token-seq )
28847 balanced-token-seq:
28848 balanced-token [opt]
28849 balanced-token-seq balanced-token
28851 balanced-token:
28852 ( balanced-token-seq )
28853 [ balanced-token-seq ]
28854 { balanced-token-seq }. */
28856 static tree
28857 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
28859 tree attribute, attr_id = NULL_TREE, arguments;
28860 cp_token *token;
28862 auto cleanup = make_temp_override
28863 (parser->auto_is_implicit_function_template_parm_p, false);
28865 /* First, parse name of the attribute, a.k.a attribute-token. */
28867 token = cp_lexer_peek_token (parser->lexer);
28868 if (token->type == CPP_NAME)
28869 attr_id = token->u.value;
28870 else if (token->type == CPP_KEYWORD)
28871 attr_id = ridpointers[(int) token->keyword];
28872 else if (token->flags & NAMED_OP)
28873 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
28875 if (attr_id == NULL_TREE)
28876 return NULL_TREE;
28878 cp_lexer_consume_token (parser->lexer);
28880 token = cp_lexer_peek_token (parser->lexer);
28881 if (token->type == CPP_SCOPE)
28883 /* We are seeing a scoped attribute token. */
28885 cp_lexer_consume_token (parser->lexer);
28886 if (attr_ns)
28887 error_at (token->location, "attribute using prefix used together "
28888 "with scoped attribute token");
28889 attr_ns = attr_id;
28891 token = cp_lexer_peek_token (parser->lexer);
28892 if (token->type == CPP_NAME)
28893 attr_id = token->u.value;
28894 else if (token->type == CPP_KEYWORD)
28895 attr_id = ridpointers[(int) token->keyword];
28896 else if (token->flags & NAMED_OP)
28897 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
28898 else
28900 error_at (token->location,
28901 "expected an identifier for the attribute name");
28902 return error_mark_node;
28904 cp_lexer_consume_token (parser->lexer);
28906 attr_ns = canonicalize_attr_name (attr_ns);
28907 attr_id = canonicalize_attr_name (attr_id);
28908 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
28909 NULL_TREE);
28910 token = cp_lexer_peek_token (parser->lexer);
28912 else if (attr_ns)
28914 attr_ns = canonicalize_attr_name (attr_ns);
28915 attr_id = canonicalize_attr_name (attr_id);
28916 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
28917 NULL_TREE);
28919 else
28921 attr_id = canonicalize_attr_name (attr_id);
28922 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
28923 NULL_TREE);
28924 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
28925 but no longer: we have to be able to tell [[noreturn]] and
28926 __attribute__((noreturn)) apart. */
28927 /* C++14 deprecated attribute is equivalent to GNU's. */
28928 if (is_attribute_p ("deprecated", attr_id))
28929 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
28930 /* C++17 fallthrough attribute is equivalent to GNU's. */
28931 else if (is_attribute_p ("fallthrough", attr_id))
28932 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
28933 /* Transactional Memory TS optimize_for_synchronized attribute is
28934 equivalent to GNU transaction_callable. */
28935 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
28936 TREE_PURPOSE (attribute)
28937 = get_identifier ("transaction_callable");
28938 /* Transactional Memory attributes are GNU attributes. */
28939 else if (tm_attr_to_mask (attr_id))
28940 TREE_PURPOSE (attribute) = attr_id;
28943 /* Now parse the optional argument clause of the attribute. */
28945 if (token->type != CPP_OPEN_PAREN)
28947 if ((flag_openmp || flag_openmp_simd)
28948 && attr_ns == omp_identifier
28949 && (is_attribute_p ("directive", attr_id)
28950 || is_attribute_p ("sequence", attr_id)))
28952 error_at (token->location, "%<omp::%E%> attribute requires argument",
28953 attr_id);
28954 return NULL_TREE;
28956 return attribute;
28960 vec<tree, va_gc> *vec;
28961 int attr_flag = normal_attr;
28963 /* Maybe we don't expect to see any arguments for this attribute. */
28964 const attribute_spec *as
28965 = lookup_attribute_spec (TREE_PURPOSE (attribute));
28966 if (as && as->max_length == 0)
28968 error_at (token->location, "%qE attribute does not take any arguments",
28969 attr_id);
28970 cp_parser_skip_to_closing_parenthesis (parser,
28971 /*recovering=*/true,
28972 /*or_comma=*/false,
28973 /*consume_paren=*/true);
28974 return error_mark_node;
28977 if (attr_ns == gnu_identifier
28978 && attribute_takes_identifier_p (attr_id))
28979 /* A GNU attribute that takes an identifier in parameter. */
28980 attr_flag = id_attr;
28982 /* If this is a fake attribute created to handle -Wno-attributes,
28983 we must skip parsing the arguments. */
28984 if (as == NULL || attribute_ignored_p (as))
28986 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
28988 if (is_attribute_p ("directive", attr_id))
28990 cp_parser_omp_directive_args (parser, attribute);
28991 return attribute;
28993 else if (is_attribute_p ("sequence", attr_id))
28995 TREE_VALUE (TREE_PURPOSE (attribute))
28996 = get_identifier ("directive");
28997 cp_parser_omp_sequence_args (parser, attribute);
28998 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
28999 return attribute;
29003 /* For unknown attributes, just skip balanced tokens instead of
29004 trying to parse the arguments. */
29005 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29006 cp_lexer_consume_token (parser->lexer);
29007 return attribute;
29010 vec = cp_parser_parenthesized_expression_list
29011 (parser, attr_flag, /*cast_p=*/false,
29012 /*allow_expansion_p=*/true,
29013 /*non_constant_p=*/NULL);
29014 if (vec == NULL)
29015 arguments = error_mark_node;
29016 else
29018 if (vec->is_empty ())
29019 /* e.g. [[attr()]]. */
29020 error_at (token->location, "parentheses must be omitted if "
29021 "%qE attribute argument list is empty",
29022 attr_id);
29023 arguments = build_tree_list_vec (vec);
29024 release_tree_vector (vec);
29027 if (arguments == error_mark_node)
29028 attribute = error_mark_node;
29029 else
29030 TREE_VALUE (attribute) = arguments;
29033 return attribute;
29036 /* Warn if the attribute ATTRIBUTE appears more than once in the
29037 attribute-list ATTRIBUTES. This used to be enforced for certain
29038 attributes, but the restriction was removed in P2156. Note that
29039 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
29040 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29041 found in ATTRIBUTES. */
29043 static bool
29044 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29046 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29047 "likely", "unlikely", "fallthrough",
29048 "no_unique_address" };
29049 if (attributes)
29050 for (const auto &a : alist)
29051 if (is_attribute_p (a, get_attribute_name (attribute))
29052 && lookup_attribute (a, attributes))
29054 if (!from_macro_expansion_at (loc))
29055 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29056 "multiple times", a);
29057 return false;
29059 return true;
29062 /* Parse a list of standard C++-11 attributes.
29064 attribute-list:
29065 attribute [opt]
29066 attribute-list , attribute[opt]
29067 attribute ...
29068 attribute-list , attribute ...
29071 static tree
29072 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29074 tree attributes = NULL_TREE, attribute = NULL_TREE;
29075 cp_token *token = NULL;
29077 while (true)
29079 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29080 attribute = cp_parser_std_attribute (parser, attr_ns);
29081 if (attribute == error_mark_node)
29082 break;
29083 if (attribute != NULL_TREE)
29085 if (cp_parser_check_std_attribute (loc, attributes, attribute))
29087 TREE_CHAIN (attribute) = attributes;
29088 attributes = attribute;
29091 token = cp_lexer_peek_token (parser->lexer);
29092 if (token->type == CPP_ELLIPSIS)
29094 cp_lexer_consume_token (parser->lexer);
29095 if (attribute == NULL_TREE)
29096 error_at (token->location,
29097 "expected attribute before %<...%>");
29098 else
29100 tree pack = make_pack_expansion (TREE_VALUE (attribute));
29101 if (pack == error_mark_node)
29102 return error_mark_node;
29103 TREE_VALUE (attribute) = pack;
29105 token = cp_lexer_peek_token (parser->lexer);
29107 if (token->type != CPP_COMMA)
29108 break;
29109 cp_lexer_consume_token (parser->lexer);
29111 attributes = nreverse (attributes);
29112 return attributes;
29115 /* Parse a standard C++-11 attribute specifier.
29117 attribute-specifier:
29118 [ [ attribute-using-prefix [opt] attribute-list ] ]
29119 alignment-specifier
29121 attribute-using-prefix:
29122 using attribute-namespace :
29124 alignment-specifier:
29125 alignas ( type-id ... [opt] )
29126 alignas ( alignment-expression ... [opt] ). */
29128 static tree
29129 cp_parser_std_attribute_spec (cp_parser *parser)
29131 tree attributes = NULL_TREE;
29132 cp_token *token = cp_lexer_peek_token (parser->lexer);
29134 if (token->type == CPP_OPEN_SQUARE
29135 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
29137 tree attr_ns = NULL_TREE;
29139 cp_lexer_consume_token (parser->lexer);
29140 cp_lexer_consume_token (parser->lexer);
29142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29144 token = cp_lexer_peek_nth_token (parser->lexer, 2);
29145 if (token->type == CPP_NAME)
29146 attr_ns = token->u.value;
29147 else if (token->type == CPP_KEYWORD)
29148 attr_ns = ridpointers[(int) token->keyword];
29149 else if (token->flags & NAMED_OP)
29150 attr_ns = get_identifier (cpp_type2name (token->type,
29151 token->flags));
29152 if (attr_ns
29153 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
29155 if (cxx_dialect < cxx17)
29156 pedwarn (input_location, OPT_Wc__17_extensions,
29157 "attribute using prefix only available "
29158 "with %<-std=c++17%> or %<-std=gnu++17%>");
29160 cp_lexer_consume_token (parser->lexer);
29161 cp_lexer_consume_token (parser->lexer);
29162 cp_lexer_consume_token (parser->lexer);
29164 else
29165 attr_ns = NULL_TREE;
29168 attributes = cp_parser_std_attribute_list (parser, attr_ns);
29170 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
29171 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
29172 cp_parser_skip_to_end_of_statement (parser);
29173 else
29174 /* Warn about parsing c++11 attribute in non-c++11 mode, only
29175 when we are sure that we have actually parsed them. */
29176 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29178 else
29180 tree alignas_expr;
29182 /* Look for an alignment-specifier. */
29184 token = cp_lexer_peek_token (parser->lexer);
29186 if (token->type != CPP_KEYWORD
29187 || token->keyword != RID_ALIGNAS)
29188 return NULL_TREE;
29190 cp_lexer_consume_token (parser->lexer);
29191 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29193 matching_parens parens;
29194 if (!parens.require_open (parser))
29195 return error_mark_node;
29197 cp_parser_parse_tentatively (parser);
29198 alignas_expr = cp_parser_type_id (parser);
29200 if (!cp_parser_parse_definitely (parser))
29202 alignas_expr = cp_parser_assignment_expression (parser);
29203 if (alignas_expr == error_mark_node)
29204 cp_parser_skip_to_end_of_statement (parser);
29205 if (alignas_expr == NULL_TREE
29206 || alignas_expr == error_mark_node)
29207 return alignas_expr;
29210 alignas_expr = cxx_alignas_expr (alignas_expr);
29211 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
29213 /* Handle alignas (pack...). */
29214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29216 cp_lexer_consume_token (parser->lexer);
29217 alignas_expr = make_pack_expansion (alignas_expr);
29220 /* Something went wrong, so don't build the attribute. */
29221 if (alignas_expr == error_mark_node)
29222 return error_mark_node;
29224 /* Missing ')' means the code cannot possibly be valid; go ahead
29225 and commit to make sure we issue a hard error. */
29226 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
29227 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29228 cp_parser_commit_to_tentative_parse (parser);
29230 if (!parens.require_close (parser))
29231 return error_mark_node;
29233 /* Build the C++-11 representation of an 'aligned'
29234 attribute. */
29235 attributes
29236 = build_tree_list (build_tree_list (gnu_identifier,
29237 aligned_identifier), alignas_expr);
29240 return attributes;
29243 /* Parse a standard C++-11 attribute-specifier-seq.
29245 attribute-specifier-seq:
29246 attribute-specifier-seq [opt] attribute-specifier
29249 static tree
29250 cp_parser_std_attribute_spec_seq (cp_parser *parser)
29252 tree attr_specs = NULL_TREE;
29253 tree attr_last = NULL_TREE;
29255 /* Don't create wrapper nodes within attributes: the
29256 handlers don't know how to handle them. */
29257 auto_suppress_location_wrappers sentinel;
29259 while (true)
29261 tree attr_spec = cp_parser_std_attribute_spec (parser);
29262 if (attr_spec == NULL_TREE)
29263 break;
29264 if (attr_spec == error_mark_node)
29265 return error_mark_node;
29267 if (attr_last)
29268 TREE_CHAIN (attr_last) = attr_spec;
29269 else
29270 attr_specs = attr_last = attr_spec;
29271 attr_last = tree_last (attr_last);
29274 return attr_specs;
29277 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
29278 return index of the first token after balanced-token, or N on failure. */
29280 static size_t
29281 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
29283 size_t orig_n = n;
29284 int nparens = 0, nbraces = 0, nsquares = 0;
29286 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
29288 case CPP_PRAGMA_EOL:
29289 if (!parser->lexer->in_pragma)
29290 break;
29291 /* FALLTHRU */
29292 case CPP_EOF:
29293 /* Ran out of tokens. */
29294 return orig_n;
29295 case CPP_OPEN_PAREN:
29296 ++nparens;
29297 break;
29298 case CPP_OPEN_BRACE:
29299 ++nbraces;
29300 break;
29301 case CPP_OPEN_SQUARE:
29302 ++nsquares;
29303 break;
29304 case CPP_CLOSE_PAREN:
29305 --nparens;
29306 break;
29307 case CPP_CLOSE_BRACE:
29308 --nbraces;
29309 break;
29310 case CPP_CLOSE_SQUARE:
29311 --nsquares;
29312 break;
29313 default:
29314 break;
29316 while (nparens || nbraces || nsquares);
29317 return n;
29320 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
29321 return index of the first token after the GNU attribute tokens, or N on
29322 failure. */
29324 static size_t
29325 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
29327 while (true)
29329 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
29330 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
29331 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
29332 break;
29334 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
29335 if (n2 == n + 2)
29336 break;
29337 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
29338 break;
29339 n = n2 + 1;
29341 return n;
29344 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
29345 next token), return index of the first token after the standard C++11
29346 attribute tokens, or N on failure. */
29348 static size_t
29349 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
29351 while (true)
29353 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
29354 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
29356 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29357 if (n2 == n + 1)
29358 break;
29359 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
29360 break;
29361 n = n2 + 1;
29363 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
29364 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
29366 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29367 if (n2 == n + 1)
29368 break;
29369 n = n2;
29371 else
29372 break;
29374 return n;
29377 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
29378 as the next token), return index of the first token after the attribute
29379 tokens, or N on failure. */
29381 static size_t
29382 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
29384 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
29385 return cp_parser_skip_gnu_attributes_opt (parser, n);
29386 return cp_parser_skip_std_attribute_spec_seq (parser, n);
29389 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
29390 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
29391 current value of the PEDANTIC flag, regardless of whether or not
29392 the `__extension__' keyword is present. The caller is responsible
29393 for restoring the value of the PEDANTIC flag. */
29395 static bool
29396 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
29398 /* Save the old value of the PEDANTIC flag. */
29399 *saved_pedantic = pedantic;
29401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
29403 /* Consume the `__extension__' token. */
29404 cp_lexer_consume_token (parser->lexer);
29405 /* We're not being pedantic while the `__extension__' keyword is
29406 in effect. */
29407 pedantic = 0;
29409 return true;
29412 return false;
29415 /* Parse a label declaration.
29417 label-declaration:
29418 __label__ label-declarator-seq ;
29420 label-declarator-seq:
29421 identifier , label-declarator-seq
29422 identifier */
29424 static void
29425 cp_parser_label_declaration (cp_parser* parser)
29427 /* Look for the `__label__' keyword. */
29428 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
29430 while (true)
29432 tree identifier;
29434 /* Look for an identifier. */
29435 identifier = cp_parser_identifier (parser);
29436 /* If we failed, stop. */
29437 if (identifier == error_mark_node)
29438 break;
29439 /* Declare it as a label. */
29440 finish_label_decl (identifier);
29441 /* If the next token is a `;', stop. */
29442 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29443 break;
29444 /* Look for the `,' separating the label declarations. */
29445 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
29448 /* Look for the final `;'. */
29449 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29452 // -------------------------------------------------------------------------- //
29453 // Concept definitions
29455 static tree
29456 cp_parser_concept_definition (cp_parser *parser)
29458 /* A concept definition is an unevaluated context. */
29459 cp_unevaluated u;
29461 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
29462 cp_lexer_consume_token (parser->lexer);
29464 cp_expr id = cp_parser_identifier (parser);
29465 if (id == error_mark_node)
29467 cp_parser_skip_to_end_of_statement (parser);
29468 cp_parser_consume_semicolon_at_end_of_statement (parser);
29469 return NULL_TREE;
29472 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29474 cp_parser_skip_to_end_of_statement (parser);
29475 cp_parser_consume_semicolon_at_end_of_statement (parser);
29476 return error_mark_node;
29479 processing_constraint_expression_sentinel parsing_constraint;
29480 tree init = cp_parser_constraint_expression (parser);
29481 if (init == error_mark_node)
29482 cp_parser_skip_to_end_of_statement (parser);
29484 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
29485 but continue as if it were. */
29486 cp_parser_consume_semicolon_at_end_of_statement (parser);
29488 return finish_concept_definition (id, init);
29491 // -------------------------------------------------------------------------- //
29492 // Requires Clause
29494 /* Diagnose an expression that should appear in ()'s within a requires-clause
29495 and suggest where to place those parentheses. */
29497 static void
29498 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
29500 error_at (loc, "expression must be enclosed in parentheses");
29503 static void
29504 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
29506 gcc_rich_location richloc (loc);
29507 richloc.add_fixit_insert_before ("(");
29508 richloc.add_fixit_insert_after (")");
29509 error_at (&richloc, "expression must be enclosed in parentheses");
29512 /* Characterizes the likely kind of expression intended by a mis-written
29513 primary constraint. */
29514 enum primary_constraint_error
29516 pce_ok,
29517 pce_maybe_operator,
29518 pce_maybe_postfix
29521 /* Returns true if the token(s) following a primary-expression in a
29522 constraint-logical-* expression would require parentheses. */
29524 static primary_constraint_error
29525 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
29527 cp_token *token = cp_lexer_peek_token (parser->lexer);
29528 switch (token->type)
29530 default:
29531 return pce_ok;
29533 case CPP_EQ:
29535 /* An equal sign may be part of the definition of a function,
29536 and not an assignment operator, when parsing the expression
29537 for a trailing requires-clause. For example:
29539 template<typename T>
29540 struct S {
29541 S() requires C<T> = default;
29544 Don't try to reparse this a binary operator. */
29545 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
29546 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
29547 return pce_ok;
29549 gcc_fallthrough ();
29552 /* Arithmetic operators. */
29553 case CPP_PLUS:
29554 case CPP_MINUS:
29555 case CPP_MULT:
29556 case CPP_DIV:
29557 case CPP_MOD:
29558 /* Bitwise operators. */
29559 case CPP_AND:
29560 case CPP_OR:
29561 case CPP_XOR:
29562 case CPP_RSHIFT:
29563 case CPP_LSHIFT:
29564 /* Relational operators. */
29565 case CPP_EQ_EQ:
29566 case CPP_NOT_EQ:
29567 case CPP_LESS:
29568 case CPP_GREATER:
29569 case CPP_LESS_EQ:
29570 case CPP_GREATER_EQ:
29571 case CPP_SPACESHIP:
29572 /* Pointer-to-member. */
29573 case CPP_DOT_STAR:
29574 case CPP_DEREF_STAR:
29575 /* Assignment operators. */
29576 case CPP_PLUS_EQ:
29577 case CPP_MINUS_EQ:
29578 case CPP_MULT_EQ:
29579 case CPP_DIV_EQ:
29580 case CPP_MOD_EQ:
29581 case CPP_AND_EQ:
29582 case CPP_OR_EQ:
29583 case CPP_XOR_EQ:
29584 case CPP_RSHIFT_EQ:
29585 case CPP_LSHIFT_EQ:
29586 /* Conditional operator */
29587 case CPP_QUERY:
29588 /* Unenclosed binary or conditional operator. */
29589 return pce_maybe_operator;
29591 case CPP_OPEN_PAREN:
29593 /* A primary constraint that precedes the parameter-list of a
29594 lambda expression is followed by an open paren.
29596 []<typename T> requires C (T a, T b) { ... }
29598 Don't try to re-parse this as a postfix expression. */
29599 if (lambda_p)
29600 return pce_ok;
29602 gcc_fallthrough ();
29604 case CPP_OPEN_SQUARE:
29606 /* A primary-constraint-expression followed by a '[[' is not a
29607 postfix expression. */
29608 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
29609 return pce_ok;
29611 gcc_fallthrough ();
29613 case CPP_PLUS_PLUS:
29614 case CPP_MINUS_MINUS:
29615 case CPP_DOT:
29616 /* Unenclosed postfix operator. */
29617 return pce_maybe_postfix;
29619 case CPP_DEREF:
29620 /* A primary constraint that precedes the lambda-declarator of a
29621 lambda expression is followed by trailing return type.
29623 []<typename T> requires C -> void {}
29625 Don't try to re-parse this as a postfix expression in
29626 C++23 and later. In C++20 ( needs to come in between but we
29627 allow it to be omitted with pedwarn. */
29628 if (lambda_p)
29629 return pce_ok;
29630 /* Unenclosed postfix operator. */
29631 return pce_maybe_postfix;
29635 /* Returns true if the next token begins a unary expression, preceded by
29636 an operator or keyword. */
29638 static bool
29639 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
29641 cp_token *token = cp_lexer_peek_token (parser->lexer);
29642 switch (token->type)
29644 case CPP_NOT:
29645 case CPP_PLUS:
29646 case CPP_MINUS:
29647 case CPP_MULT:
29648 case CPP_COMPL:
29649 case CPP_PLUS_PLUS:
29650 case CPP_MINUS_MINUS:
29651 return true;
29653 case CPP_KEYWORD:
29655 switch (token->keyword)
29657 case RID_STATCAST:
29658 case RID_DYNCAST:
29659 case RID_REINTCAST:
29660 case RID_CONSTCAST:
29661 case RID_TYPEID:
29662 case RID_SIZEOF:
29663 case RID_ALIGNOF:
29664 case RID_NOEXCEPT:
29665 case RID_NEW:
29666 case RID_DELETE:
29667 case RID_THROW:
29668 return true;
29670 default:
29671 break;
29675 default:
29676 break;
29679 return false;
29682 /* Parse a primary expression within a constraint. */
29684 static cp_expr
29685 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
29687 /* If this looks like a unary expression, parse it as such, but diagnose
29688 it as ill-formed; it requires parens. */
29689 if (cp_parser_unary_constraint_requires_parens (parser))
29691 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
29692 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
29693 return e;
29696 cp_lexer_save_tokens (parser->lexer);
29697 cp_id_kind idk;
29698 location_t loc = input_location;
29699 cp_expr expr = cp_parser_primary_expression (parser,
29700 /*address_p=*/false,
29701 /*cast_p=*/false,
29702 /*template_arg_p=*/false,
29703 &idk);
29704 expr.maybe_add_location_wrapper ();
29706 primary_constraint_error pce = pce_ok;
29707 if (expr != error_mark_node)
29709 /* The primary-expression could be part of an unenclosed non-logical
29710 compound expression. */
29711 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
29713 if (pce == pce_ok)
29715 cp_lexer_commit_tokens (parser->lexer);
29716 return finish_constraint_primary_expr (expr);
29719 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
29720 error, but return the expression as if it were valid. */
29721 cp_lexer_rollback_tokens (parser->lexer);
29722 cp_parser_parse_tentatively (parser);
29723 if (pce == pce_maybe_operator)
29724 expr = cp_parser_assignment_expression (parser, NULL, false, false);
29725 else
29726 expr = cp_parser_simple_cast_expression (parser);
29727 if (cp_parser_parse_definitely (parser))
29729 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
29730 return expr;
29733 /* Otherwise, something has gone very wrong, and we can't generate a more
29734 meaningful diagnostic or recover. */
29735 cp_parser_diagnose_ungrouped_constraint_plain (loc);
29736 return error_mark_node;
29739 /* Parse a constraint-logical-and-expression.
29741 constraint-logical-and-expression:
29742 primary-expression
29743 constraint-logical-and-expression '&&' primary-expression */
29745 static cp_expr
29746 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
29748 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29749 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
29751 cp_token *op = cp_lexer_consume_token (parser->lexer);
29752 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29753 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
29755 return lhs;
29758 /* Parse a constraint-logical-or-expression.
29760 constraint-logical-or-expression:
29761 constraint-logical-and-expression
29762 constraint-logical-or-expression '||' constraint-logical-and-expression */
29764 static cp_expr
29765 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
29767 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29768 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
29770 cp_token *op = cp_lexer_consume_token (parser->lexer);
29771 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29772 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
29774 return lhs;
29777 /* Parse the expression after a requires-clause. This has a different grammar
29778 than that in the concepts TS. */
29780 static tree
29781 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
29783 processing_constraint_expression_sentinel parsing_constraint;
29784 ++processing_template_decl;
29785 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
29786 --processing_template_decl;
29787 if (check_for_bare_parameter_packs (expr))
29788 expr = error_mark_node;
29789 return expr;
29792 /* Parse a expression after a requires clause.
29794 constraint-expression:
29795 logical-or-expression
29797 The required logical-or-expression must be a constant expression. Note
29798 that we don't check that the expression is constepxr here. We defer until
29799 we analyze constraints and then, we only check atomic constraints. */
29801 static tree
29802 cp_parser_constraint_expression (cp_parser *parser)
29804 processing_constraint_expression_sentinel parsing_constraint;
29805 ++processing_template_decl;
29806 cp_expr expr = cp_parser_binary_expression (parser, false, true,
29807 PREC_NOT_OPERATOR, NULL);
29808 --processing_template_decl;
29809 if (check_for_bare_parameter_packs (expr))
29810 expr = error_mark_node;
29811 expr.maybe_add_location_wrapper ();
29812 return expr;
29815 /* Optionally parse a requires clause:
29817 requires-clause:
29818 `requires` constraint-logical-or-expression.
29819 [ConceptsTS]
29820 `requires constraint-expression.
29822 LAMBDA_P is true when the requires-clause is parsed before the
29823 parameter-list of a lambda-declarator. */
29825 static tree
29826 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
29828 /* A requires clause is an unevaluated context. */
29829 cp_unevaluated u;
29831 cp_token *tok = cp_lexer_peek_token (parser->lexer);
29832 if (tok->keyword != RID_REQUIRES)
29834 if (!flag_concepts && tok->type == CPP_NAME
29835 && tok->u.value == ridpointers[RID_REQUIRES])
29837 error_at (cp_lexer_peek_token (parser->lexer)->location,
29838 "%<requires%> only available with "
29839 "%<-std=c++20%> or %<-fconcepts%>");
29840 /* Parse and discard the requires-clause. */
29841 cp_lexer_consume_token (parser->lexer);
29842 cp_parser_constraint_expression (parser);
29844 return NULL_TREE;
29847 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
29848 if (tok2->type == CPP_OPEN_BRACE)
29850 /* An opening brace following the start of a requires-clause is
29851 ill-formed; the user likely forgot the second `requires' that
29852 would start a requires-expression. */
29853 gcc_rich_location richloc (tok2->location);
29854 richloc.add_fixit_insert_after (tok->location, " requires");
29855 error_at (&richloc, "missing additional %<requires%> to start "
29856 "a requires-expression");
29857 /* Don't consume the `requires', so that it's reused as the start of a
29858 requires-expression. */
29860 else
29861 cp_lexer_consume_token (parser->lexer);
29863 if (!flag_concepts_ts)
29864 return cp_parser_requires_clause_expression (parser, lambda_p);
29865 else
29866 return cp_parser_constraint_expression (parser);
29869 /*---------------------------------------------------------------------------
29870 Requires expressions
29871 ---------------------------------------------------------------------------*/
29873 /* Parse a requires expression
29875 requirement-expression:
29876 'requires' requirement-parameter-list [opt] requirement-body */
29878 static tree
29879 cp_parser_requires_expression (cp_parser *parser)
29881 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
29882 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
29884 /* Avoid committing to outer tentative parse. */
29885 tentative_firewall firewall (parser);
29887 /* This is definitely a requires-expression. */
29888 cp_parser_commit_to_tentative_parse (parser);
29890 tree parms, reqs;
29892 /* Local parameters are delared as variables within the scope
29893 of the expression. They are not visible past the end of
29894 the expression. Expressions within the requires-expression
29895 are unevaluated. */
29896 struct scope_sentinel
29898 scope_sentinel ()
29900 ++cp_unevaluated_operand;
29901 begin_scope (sk_block, NULL_TREE);
29904 ~scope_sentinel ()
29906 pop_bindings_and_leave_scope ();
29907 --cp_unevaluated_operand;
29909 } s;
29911 /* Parse the optional parameter list. */
29912 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29914 parms = cp_parser_requirement_parameter_list (parser);
29915 if (parms == error_mark_node)
29916 return error_mark_node;
29918 else
29919 parms = NULL_TREE;
29921 /* Parse the requirement body. */
29922 ++processing_template_decl;
29923 reqs = cp_parser_requirement_body (parser);
29924 --processing_template_decl;
29925 if (reqs == error_mark_node)
29926 return error_mark_node;
29929 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
29930 the parm chain. */
29931 grokparms (parms, &parms);
29932 loc = make_location (loc, loc, parser->lexer);
29933 tree expr = finish_requires_expr (loc, parms, reqs);
29934 if (!processing_template_decl)
29936 /* Perform semantic processing now to diagnose any invalid types and
29937 expressions. */
29938 int saved_errorcount = errorcount;
29939 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
29940 if (errorcount > saved_errorcount)
29941 return error_mark_node;
29943 return expr;
29946 /* Parse a parameterized requirement.
29948 requirement-parameter-list:
29949 '(' parameter-declaration-clause ')' */
29951 static tree
29952 cp_parser_requirement_parameter_list (cp_parser *parser)
29954 matching_parens parens;
29955 if (!parens.require_open (parser))
29956 return error_mark_node;
29958 tree parms = (cp_parser_parameter_declaration_clause
29959 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
29961 if (!parens.require_close (parser))
29962 return error_mark_node;
29964 /* Modify the declared parameters by removing their context
29965 so they don't refer to the enclosing scope and explicitly
29966 indicating that they are constraint variables. */
29967 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
29969 if (parm == void_list_node || parm == explicit_void_list_node)
29970 break;
29971 tree decl = TREE_VALUE (parm);
29972 if (decl != error_mark_node)
29974 DECL_CONTEXT (decl) = NULL_TREE;
29975 CONSTRAINT_VAR_P (decl) = true;
29979 return parms;
29982 /* Parse the body of a requirement.
29984 requirement-body:
29985 '{' requirement-list '}' */
29986 static tree
29987 cp_parser_requirement_body (cp_parser *parser)
29989 matching_braces braces;
29990 if (!braces.require_open (parser))
29991 return error_mark_node;
29993 tree reqs = cp_parser_requirement_seq (parser);
29995 if (!braces.require_close (parser))
29996 return error_mark_node;
29998 return reqs;
30001 /* Parse a sequence of requirements.
30003 requirement-seq:
30004 requirement
30005 requirement-seq requirement */
30007 static tree
30008 cp_parser_requirement_seq (cp_parser *parser)
30010 tree result = NULL_TREE;
30013 tree req = cp_parser_requirement (parser);
30014 if (req != error_mark_node)
30015 result = tree_cons (NULL_TREE, req, result);
30017 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
30018 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
30020 /* If there are no valid requirements, this is not a valid expression. */
30021 if (!result)
30022 return error_mark_node;
30024 /* Reverse the order of requirements so they are analyzed in order. */
30025 return nreverse (result);
30028 /* Parse a syntactic requirement or type requirement.
30030 requirement:
30031 simple-requirement
30032 compound-requirement
30033 type-requirement
30034 nested-requirement */
30036 static tree
30037 cp_parser_requirement (cp_parser *parser)
30039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30040 return cp_parser_compound_requirement (parser);
30041 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
30042 return cp_parser_type_requirement (parser);
30043 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
30044 return cp_parser_nested_requirement (parser);
30045 else
30046 return cp_parser_simple_requirement (parser);
30049 /* Parse a simple requirement.
30051 simple-requirement:
30052 expression ';' */
30054 static tree
30055 cp_parser_simple_requirement (cp_parser *parser)
30057 location_t start = cp_lexer_peek_token (parser->lexer)->location;
30058 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
30059 if (expr == error_mark_node)
30060 cp_parser_skip_to_end_of_statement (parser);
30062 cp_parser_consume_semicolon_at_end_of_statement (parser);
30064 if (!expr || expr == error_mark_node)
30065 return error_mark_node;
30067 /* Sometimes we don't get locations, so use the cached token location
30068 as a reasonable approximation. */
30069 if (expr.get_location() == UNKNOWN_LOCATION)
30070 expr.set_location (start);
30072 for (tree t = expr; ; )
30074 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
30075 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
30077 t = TREE_OPERAND (t, 0);
30078 continue;
30080 if (concept_check_p (t))
30082 gcc_rich_location richloc (get_start (start));
30083 richloc.add_fixit_insert_before (start, "requires ");
30084 warning_at (&richloc, OPT_Wmissing_requires, "testing "
30085 "if a concept-id is a valid expression; add "
30086 "%<requires%> to check satisfaction");
30088 break;
30091 return finish_simple_requirement (expr.get_location (), expr);
30094 /* Parse a type requirement
30096 type-requirement
30097 nested-name-specifier [opt] required-type-name ';'
30099 required-type-name:
30100 type-name
30101 'template' [opt] simple-template-id */
30103 static tree
30104 cp_parser_type_requirement (cp_parser *parser)
30106 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
30107 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30109 // Save the scope before parsing name specifiers.
30110 tree saved_scope = parser->scope;
30111 tree saved_object_scope = parser->object_scope;
30112 tree saved_qualifying_scope = parser->qualifying_scope;
30113 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
30114 cp_parser_nested_name_specifier_opt (parser,
30115 /*typename_keyword_p=*/true,
30116 /*check_dependency_p=*/false,
30117 /*type_p=*/true,
30118 /*is_declaration=*/false);
30120 tree type;
30121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30123 cp_lexer_consume_token (parser->lexer);
30124 type = cp_parser_template_id (parser,
30125 /*template_keyword_p=*/true,
30126 /*check_dependency=*/false,
30127 /*tag_type=*/none_type,
30128 /*is_declaration=*/false);
30129 type = make_typename_type (parser->scope, type, typename_type,
30130 /*complain=*/tf_error);
30132 else
30133 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
30135 if (TREE_CODE (type) == TYPE_DECL)
30136 type = TREE_TYPE (type);
30138 parser->scope = saved_scope;
30139 parser->object_scope = saved_object_scope;
30140 parser->qualifying_scope = saved_qualifying_scope;
30142 if (type == error_mark_node)
30143 cp_parser_skip_to_end_of_statement (parser);
30145 cp_parser_consume_semicolon_at_end_of_statement (parser);
30147 if (type == error_mark_node)
30148 return error_mark_node;
30150 loc = make_location (loc, start_tok->location, parser->lexer);
30151 return finish_type_requirement (loc, type);
30154 /* Parse a compound requirement
30156 compound-requirement:
30157 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
30159 static tree
30160 cp_parser_compound_requirement (cp_parser *parser)
30162 /* Parse an expression enclosed in '{ }'s. */
30163 matching_braces braces;
30164 if (!braces.require_open (parser))
30165 return error_mark_node;
30167 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
30169 tree expr = cp_parser_expression (parser, NULL, false, false);
30170 if (expr == error_mark_node)
30171 cp_parser_skip_to_closing_brace (parser);
30173 if (!braces.require_close (parser))
30175 cp_parser_skip_to_end_of_statement (parser);
30176 cp_parser_consume_semicolon_at_end_of_statement (parser);
30177 return error_mark_node;
30180 /* If the expression was invalid, skip the remainder of the requirement. */
30181 if (!expr || expr == error_mark_node)
30183 cp_parser_skip_to_end_of_statement (parser);
30184 cp_parser_consume_semicolon_at_end_of_statement (parser);
30185 return error_mark_node;
30188 /* Parse the optional noexcept. */
30189 bool noexcept_p = false;
30190 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
30192 cp_lexer_consume_token (parser->lexer);
30193 noexcept_p = true;
30196 /* Parse the optional trailing return type. */
30197 tree type = NULL_TREE;
30198 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
30200 cp_lexer_consume_token (parser->lexer);
30201 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30203 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
30204 parser->in_result_type_constraint_p = true;
30205 /* C++20 allows either a type-id or a type-constraint. Parsing
30206 a type-id will subsume the parsing for a type-constraint but
30207 allow for more syntactic forms (e.g., const C<T>*). */
30208 type = cp_parser_trailing_type_id (parser);
30209 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
30210 if (type == error_mark_node)
30211 return error_mark_node;
30213 location_t type_loc = make_location (tok->location, tok->location,
30214 parser->lexer);
30216 /* Check that we haven't written something like 'const C<T>*'. */
30217 if (type_uses_auto (type))
30219 if (!is_auto (type))
30221 error_at (type_loc,
30222 "result type is not a plain type-constraint");
30223 cp_parser_consume_semicolon_at_end_of_statement (parser);
30224 return error_mark_node;
30227 else if (!flag_concepts_ts)
30228 /* P1452R2 removed the trailing-return-type option. */
30229 error_at (type_loc,
30230 "return-type-requirement is not a type-constraint");
30233 location_t loc = make_location (expr_token->location,
30234 braces.open_location (),
30235 parser->lexer);
30237 cp_parser_consume_semicolon_at_end_of_statement (parser);
30239 if (expr == error_mark_node || type == error_mark_node)
30240 return error_mark_node;
30242 return finish_compound_requirement (loc, expr, type, noexcept_p);
30245 /* Parse a nested requirement. This is the same as a requires clause.
30247 nested-requirement:
30248 requires-clause */
30250 static tree
30251 cp_parser_nested_requirement (cp_parser *parser)
30253 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30254 cp_token *tok = cp_lexer_consume_token (parser->lexer);
30255 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30256 tree req = cp_parser_constraint_expression (parser);
30257 if (req == error_mark_node)
30258 cp_parser_skip_to_end_of_statement (parser);
30259 loc = make_location (loc, tok->location, parser->lexer);
30260 cp_parser_consume_semicolon_at_end_of_statement (parser);
30261 if (req == error_mark_node)
30262 return error_mark_node;
30263 return finish_nested_requirement (loc, req);
30266 /* Support Functions */
30268 /* Return the appropriate prefer_type argument for lookup_name based on
30269 tag_type. */
30271 static inline LOOK_want
30272 prefer_type_arg (tag_types tag_type)
30274 switch (tag_type)
30276 case none_type: return LOOK_want::NORMAL; // No preference.
30277 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
30278 default: return LOOK_want::TYPE; // Type only.
30282 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
30283 NAME should have one of the representations used for an
30284 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
30285 is returned. If PARSER->SCOPE is a dependent type, then a
30286 SCOPE_REF is returned.
30288 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
30289 returned; the name was already resolved when the TEMPLATE_ID_EXPR
30290 was formed. Abstractly, such entities should not be passed to this
30291 function, because they do not need to be looked up, but it is
30292 simpler to check for this special case here, rather than at the
30293 call-sites.
30295 In cases not explicitly covered above, this function returns a
30296 DECL, OVERLOAD, or baselink representing the result of the lookup.
30297 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
30298 is returned.
30300 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
30301 (e.g., "struct") that was used. In that case bindings that do not
30302 refer to types are ignored.
30304 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
30305 ignored.
30307 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
30308 are ignored.
30310 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
30311 types.
30313 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
30314 TREE_LIST of candidates if name-lookup results in an ambiguity, and
30315 NULL_TREE otherwise. */
30317 static cp_expr
30318 cp_parser_lookup_name (cp_parser *parser, tree name,
30319 enum tag_types tag_type,
30320 bool is_template,
30321 bool is_namespace,
30322 bool check_dependency,
30323 tree *ambiguous_decls,
30324 location_t name_location)
30326 tree decl;
30327 tree object_type = parser->context->object_type;
30329 /* Assume that the lookup will be unambiguous. */
30330 if (ambiguous_decls)
30331 *ambiguous_decls = NULL_TREE;
30333 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
30334 no longer valid. Note that if we are parsing tentatively, and
30335 the parse fails, OBJECT_TYPE will be automatically restored. */
30336 parser->context->object_type = NULL_TREE;
30338 if (name == error_mark_node)
30339 return error_mark_node;
30341 /* A template-id has already been resolved; there is no lookup to
30342 do. */
30343 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
30344 return name;
30345 if (BASELINK_P (name))
30347 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
30348 == TEMPLATE_ID_EXPR);
30349 return name;
30352 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
30353 it should already have been checked to make sure that the name
30354 used matches the type being destroyed. */
30355 if (TREE_CODE (name) == BIT_NOT_EXPR)
30357 tree type;
30359 /* Figure out to which type this destructor applies. */
30360 if (parser->scope)
30361 type = parser->scope;
30362 else if (object_type)
30363 type = object_type;
30364 else
30365 type = current_class_type;
30366 /* If that's not a class type, there is no destructor. */
30367 if (!type || !CLASS_TYPE_P (type))
30368 return error_mark_node;
30370 /* In a non-static member function, check implicit this->. */
30371 if (current_class_ref)
30372 return lookup_destructor (current_class_ref, parser->scope, name,
30373 tf_warning_or_error);
30375 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
30376 lazily_declare_fn (sfk_destructor, type);
30378 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
30379 return dtor;
30381 return error_mark_node;
30384 /* By this point, the NAME should be an ordinary identifier. If
30385 the id-expression was a qualified name, the qualifying scope is
30386 stored in PARSER->SCOPE at this point. */
30387 gcc_assert (identifier_p (name));
30389 /* Perform the lookup. */
30390 if (parser->scope)
30392 bool dependent_p;
30394 if (parser->scope == error_mark_node)
30395 return error_mark_node;
30397 /* If the SCOPE is dependent, the lookup must be deferred until
30398 the template is instantiated -- unless we are explicitly
30399 looking up names in uninstantiated templates. Even then, we
30400 cannot look up the name if the scope is not a class type; it
30401 might, for example, be a template type parameter. */
30402 dependent_p = (TYPE_P (parser->scope)
30403 && dependent_scope_p (parser->scope));
30404 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
30405 && dependent_p)
30406 /* Defer lookup. */
30407 decl = error_mark_node;
30408 else
30410 tree pushed_scope = NULL_TREE;
30412 /* If PARSER->SCOPE is a dependent type, then it must be a
30413 class type, and we must not be checking dependencies;
30414 otherwise, we would have processed this lookup above. So
30415 that PARSER->SCOPE is not considered a dependent base by
30416 lookup_member, we must enter the scope here. */
30417 if (dependent_p)
30418 pushed_scope = push_scope (parser->scope);
30420 /* If the PARSER->SCOPE is a template specialization, it
30421 may be instantiated during name lookup. In that case,
30422 errors may be issued. Even if we rollback the current
30423 tentative parse, those errors are valid. */
30424 decl = lookup_qualified_name (parser->scope, name,
30425 prefer_type_arg (tag_type),
30426 /*complain=*/true);
30428 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
30429 lookup result and the nested-name-specifier nominates a class C:
30430 * if the name specified after the nested-name-specifier, when
30431 looked up in C, is the injected-class-name of C (Clause 9), or
30432 * if the name specified after the nested-name-specifier is the
30433 same as the identifier or the simple-template-id's template-
30434 name in the last component of the nested-name-specifier,
30435 the name is instead considered to name the constructor of
30436 class C. [ Note: for example, the constructor is not an
30437 acceptable lookup result in an elaborated-type-specifier so
30438 the constructor would not be used in place of the
30439 injected-class-name. --end note ] Such a constructor name
30440 shall be used only in the declarator-id of a declaration that
30441 names a constructor or in a using-declaration. */
30442 if (tag_type == none_type
30443 && DECL_SELF_REFERENCE_P (decl)
30444 && same_type_p (DECL_CONTEXT (decl), parser->scope))
30445 decl = lookup_qualified_name (parser->scope, ctor_identifier,
30446 prefer_type_arg (tag_type),
30447 /*complain=*/true);
30449 if (pushed_scope)
30450 pop_scope (pushed_scope);
30453 /* If the scope is a dependent type and either we deferred lookup or
30454 we did lookup but didn't find the name, rememeber the name. */
30455 if (decl == error_mark_node && TYPE_P (parser->scope)
30456 && dependent_type_p (parser->scope))
30458 if (tag_type)
30460 tree type;
30462 /* The resolution to Core Issue 180 says that `struct
30463 A::B' should be considered a type-name, even if `A'
30464 is dependent. */
30465 type = make_typename_type (parser->scope, name, tag_type,
30466 /*complain=*/tf_error);
30467 if (type != error_mark_node)
30468 decl = TYPE_NAME (type);
30470 else if (is_template
30471 && (cp_parser_next_token_ends_template_argument_p (parser)
30472 || cp_lexer_next_token_is (parser->lexer,
30473 CPP_CLOSE_PAREN)))
30474 decl = make_unbound_class_template (parser->scope,
30475 name, NULL_TREE,
30476 /*complain=*/tf_error);
30477 else
30478 decl = build_qualified_name (/*type=*/NULL_TREE,
30479 parser->scope, name,
30480 is_template);
30482 parser->qualifying_scope = parser->scope;
30483 parser->object_scope = NULL_TREE;
30485 else if (object_type)
30487 /* Look up the name in the scope of the OBJECT_TYPE, unless the
30488 OBJECT_TYPE is not a class. */
30489 if (CLASS_TYPE_P (object_type))
30490 /* If the OBJECT_TYPE is a template specialization, it may
30491 be instantiated during name lookup. In that case, errors
30492 may be issued. Even if we rollback the current tentative
30493 parse, those errors are valid. */
30494 decl = lookup_member (object_type,
30495 name,
30496 /*protect=*/0,
30497 /*prefer_type=*/tag_type != none_type,
30498 tf_warning_or_error);
30499 else
30500 decl = NULL_TREE;
30502 if (!decl)
30503 /* Look it up in the enclosing context. DR 141: When looking for a
30504 template-name after -> or ., only consider class templates. */
30505 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30506 /* DR 141: When looking in the
30507 current enclosing context for a
30508 template-name after -> or ., only
30509 consider class templates. */
30510 : is_template ? LOOK_want::TYPE
30511 : prefer_type_arg (tag_type));
30513 /* If we know we're looking for a type (e.g. A in p->A::x),
30514 mock up a typename. */
30515 if (!decl && object_type && tag_type != none_type
30516 && dependentish_scope_p (object_type))
30518 tree type = build_typename_type (object_type, name, name,
30519 typename_type);
30520 decl = TYPE_NAME (type);
30523 parser->object_scope = object_type;
30524 parser->qualifying_scope = NULL_TREE;
30526 else
30528 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30529 : prefer_type_arg (tag_type));
30530 parser->qualifying_scope = NULL_TREE;
30531 parser->object_scope = NULL_TREE;
30534 /* If the lookup failed, let our caller know. */
30535 if (!decl || decl == error_mark_node)
30536 return error_mark_node;
30538 /* If we have resolved the name of a member declaration, check to
30539 see if the declaration is accessible. When the name resolves to
30540 set of overloaded functions, accessibility is checked when
30541 overload resolution is done. If we have a TREE_LIST, then the lookup
30542 is either ambiguous or it found multiple injected-class-names, the
30543 accessibility of which is trivially satisfied.
30545 During an explicit instantiation, access is not checked at all,
30546 as per [temp.explicit]. */
30547 if (DECL_P (decl))
30548 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
30549 tf_warning_or_error);
30551 /* Pull out the template from an injected-class-name (or multiple). */
30552 if (is_template)
30553 decl = maybe_get_template_decl_from_type_decl (decl);
30555 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
30556 if (TREE_CODE (decl) == TREE_LIST)
30558 if (ambiguous_decls)
30559 *ambiguous_decls = decl;
30560 /* The error message we have to print is too complicated for
30561 cp_parser_error, so we incorporate its actions directly. */
30562 if (!cp_parser_simulate_error (parser))
30564 error_at (name_location, "reference to %qD is ambiguous",
30565 name);
30566 print_candidates (decl);
30568 return error_mark_node;
30571 gcc_assert (DECL_P (decl)
30572 || TREE_CODE (decl) == OVERLOAD
30573 || TREE_CODE (decl) == SCOPE_REF
30574 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
30575 || BASELINK_P (decl));
30577 maybe_record_typedef_use (decl);
30579 return cp_expr (decl, name_location);
30582 /* Like cp_parser_lookup_name, but for use in the typical case where
30583 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
30584 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
30586 static tree
30587 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
30589 return cp_parser_lookup_name (parser, name,
30590 none_type,
30591 /*is_template=*/false,
30592 /*is_namespace=*/false,
30593 /*check_dependency=*/true,
30594 /*ambiguous_decls=*/NULL,
30595 location);
30598 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
30599 the current context, return the TYPE_DECL. If TAG_NAME_P is
30600 true, the DECL indicates the class being defined in a class-head,
30601 or declared in an elaborated-type-specifier.
30603 Otherwise, return DECL. */
30605 static tree
30606 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
30608 /* If the TEMPLATE_DECL is being declared as part of a class-head,
30609 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
30611 struct A {
30612 template <typename T> struct B;
30615 template <typename T> struct A::B {};
30617 Similarly, in an elaborated-type-specifier:
30619 namespace N { struct X{}; }
30621 struct A {
30622 template <typename T> friend struct N::X;
30625 However, if the DECL refers to a class type, and we are in
30626 the scope of the class, then the name lookup automatically
30627 finds the TYPE_DECL created by build_self_reference rather
30628 than a TEMPLATE_DECL. For example, in:
30630 template <class T> struct S {
30631 S s;
30634 there is no need to handle such case. */
30636 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
30637 return DECL_TEMPLATE_RESULT (decl);
30639 return decl;
30642 /* If too many, or too few, template-parameter lists apply to the
30643 declarator, issue an error message. Returns TRUE if all went well,
30644 and FALSE otherwise. */
30646 static bool
30647 cp_parser_check_declarator_template_parameters (cp_parser* parser,
30648 cp_declarator *declarator,
30649 location_t declarator_location)
30651 switch (declarator->kind)
30653 case cdk_id:
30655 unsigned num_templates = 0;
30656 tree scope = declarator->u.id.qualifying_scope;
30657 bool template_id_p = false;
30659 if (scope)
30660 num_templates = num_template_headers_for_class (scope);
30661 else if (TREE_CODE (declarator->u.id.unqualified_name)
30662 == TEMPLATE_ID_EXPR)
30664 /* If the DECLARATOR has the form `X<y>' then it uses one
30665 additional level of template parameters. */
30666 ++num_templates;
30667 template_id_p = true;
30670 return cp_parser_check_template_parameters
30671 (parser, num_templates, template_id_p, declarator_location,
30672 declarator);
30675 case cdk_function:
30676 case cdk_array:
30677 case cdk_pointer:
30678 case cdk_reference:
30679 case cdk_ptrmem:
30680 return (cp_parser_check_declarator_template_parameters
30681 (parser, declarator->declarator, declarator_location));
30683 case cdk_decomp:
30684 case cdk_error:
30685 return true;
30687 default:
30688 gcc_unreachable ();
30690 return false;
30693 /* NUM_TEMPLATES were used in the current declaration. If that is
30694 invalid, return FALSE and issue an error messages. Otherwise,
30695 return TRUE. If DECLARATOR is non-NULL, then we are checking a
30696 declarator and we can print more accurate diagnostics. */
30698 static bool
30699 cp_parser_check_template_parameters (cp_parser* parser,
30700 unsigned num_templates,
30701 bool template_id_p,
30702 location_t location,
30703 cp_declarator *declarator)
30705 /* If there are the same number of template classes and parameter
30706 lists, that's OK. */
30707 if (parser->num_template_parameter_lists == num_templates)
30708 return true;
30709 /* If there are more, but only one more, and the name ends in an identifier,
30710 then we are declaring a primary template. That's OK too. */
30711 if (!template_id_p
30712 && parser->num_template_parameter_lists == num_templates + 1)
30713 return true;
30715 if (cp_parser_simulate_error (parser))
30716 return false;
30718 /* If there are more template classes than parameter lists, we have
30719 something like:
30721 template <class T> void S<T>::R<T>::f (); */
30722 if (parser->num_template_parameter_lists < num_templates)
30724 if (declarator && !current_function_decl)
30725 error_at (location, "specializing member %<%T::%E%> "
30726 "requires %<template<>%> syntax",
30727 declarator->u.id.qualifying_scope,
30728 declarator->u.id.unqualified_name);
30729 else if (declarator)
30730 error_at (location, "invalid declaration of %<%T::%E%>",
30731 declarator->u.id.qualifying_scope,
30732 declarator->u.id.unqualified_name);
30733 else
30734 error_at (location, "too few template-parameter-lists");
30735 return false;
30737 /* Otherwise, there are too many template parameter lists. We have
30738 something like:
30740 template <class T> template <class U> void S::f(); */
30741 error_at (location, "too many template-parameter-lists");
30742 return false;
30745 /* Parse an optional `::' token indicating that the following name is
30746 from the global namespace. If so, PARSER->SCOPE is set to the
30747 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
30748 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
30749 Returns the new value of PARSER->SCOPE, if the `::' token is
30750 present, and NULL_TREE otherwise. */
30752 static tree
30753 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
30755 cp_token *token;
30757 /* Peek at the next token. */
30758 token = cp_lexer_peek_token (parser->lexer);
30759 /* If we're looking at a `::' token then we're starting from the
30760 global namespace, not our current location. */
30761 if (token->type == CPP_SCOPE)
30763 /* Consume the `::' token. */
30764 cp_lexer_consume_token (parser->lexer);
30765 /* Set the SCOPE so that we know where to start the lookup. */
30766 parser->scope = global_namespace;
30767 parser->qualifying_scope = global_namespace;
30768 parser->object_scope = NULL_TREE;
30770 return parser->scope;
30772 else if (!current_scope_valid_p)
30774 parser->scope = NULL_TREE;
30775 parser->qualifying_scope = NULL_TREE;
30776 parser->object_scope = NULL_TREE;
30779 return NULL_TREE;
30782 /* Returns TRUE if the upcoming token sequence is the start of a
30783 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
30784 declarator is preceded by the `friend' specifier. The parser flags FLAGS
30785 is used to control type-specifier parsing. */
30787 static bool
30788 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
30789 bool friend_p)
30791 bool constructor_p;
30792 bool outside_class_specifier_p;
30793 tree nested_name_specifier;
30794 cp_token *next_token;
30796 /* The common case is that this is not a constructor declarator, so
30797 try to avoid doing lots of work if at all possible. It's not
30798 valid declare a constructor at function scope. */
30799 if (parser->in_function_body)
30800 return false;
30801 /* And only certain tokens can begin a constructor declarator. */
30802 next_token = cp_lexer_peek_token (parser->lexer);
30803 if (next_token->type != CPP_NAME
30804 && next_token->type != CPP_SCOPE
30805 && next_token->type != CPP_NESTED_NAME_SPECIFIER
30806 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
30807 declarator-id of a constructor or destructor. */
30808 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
30809 return false;
30811 /* Parse tentatively; we are going to roll back all of the tokens
30812 consumed here. */
30813 cp_parser_parse_tentatively (parser);
30814 /* Assume that we are looking at a constructor declarator. */
30815 constructor_p = true;
30817 /* Look for the optional `::' operator. */
30818 cp_parser_global_scope_opt (parser,
30819 /*current_scope_valid_p=*/false);
30820 /* Look for the nested-name-specifier. */
30821 nested_name_specifier
30822 = (cp_parser_nested_name_specifier_opt (parser,
30823 /*typename_keyword_p=*/false,
30824 /*check_dependency_p=*/false,
30825 /*type_p=*/false,
30826 /*is_declaration=*/false));
30828 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
30829 if (nested_name_specifier
30830 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
30832 tree s = resolve_typename_type (nested_name_specifier,
30833 /*only_current_p=*/false);
30834 if (TREE_CODE (s) != TYPENAME_TYPE)
30835 nested_name_specifier = s;
30838 outside_class_specifier_p = (!at_class_scope_p ()
30839 || !TYPE_BEING_DEFINED (current_class_type)
30840 || friend_p);
30842 /* Outside of a class-specifier, there must be a
30843 nested-name-specifier. Except in C++17 mode, where we
30844 might be declaring a guiding declaration. */
30845 if (!nested_name_specifier && outside_class_specifier_p
30846 && cxx_dialect < cxx17)
30847 constructor_p = false;
30848 else if (nested_name_specifier == error_mark_node)
30849 constructor_p = false;
30851 /* If we have a class scope, this is easy; DR 147 says that S::S always
30852 names the constructor, and no other qualified name could. */
30853 if (constructor_p && nested_name_specifier
30854 && CLASS_TYPE_P (nested_name_specifier))
30856 tree id = cp_parser_unqualified_id (parser,
30857 /*template_keyword_p=*/false,
30858 /*check_dependency_p=*/false,
30859 /*declarator_p=*/true,
30860 /*optional_p=*/false);
30861 if (is_overloaded_fn (id))
30862 id = DECL_NAME (get_first_fn (id));
30863 if (!constructor_name_p (id, nested_name_specifier))
30864 constructor_p = false;
30866 /* If we still think that this might be a constructor-declarator,
30867 look for a class-name. */
30868 else if (constructor_p)
30870 /* If we have:
30872 template <typename T> struct S {
30873 S();
30876 we must recognize that the nested `S' names a class. */
30877 if (cxx_dialect >= cxx17)
30878 cp_parser_parse_tentatively (parser);
30880 tree type_decl;
30881 type_decl = cp_parser_class_name (parser,
30882 /*typename_keyword_p=*/false,
30883 /*template_keyword_p=*/false,
30884 none_type,
30885 /*check_dependency_p=*/false,
30886 /*class_head_p=*/false,
30887 /*is_declaration=*/false);
30889 if (cxx_dialect >= cxx17
30890 && !cp_parser_parse_definitely (parser))
30892 type_decl = NULL_TREE;
30893 tree tmpl = cp_parser_template_name (parser,
30894 /*template_keyword*/false,
30895 /*check_dependency_p*/false,
30896 /*is_declaration*/false,
30897 none_type,
30898 /*is_identifier*/NULL);
30899 if (DECL_CLASS_TEMPLATE_P (tmpl)
30900 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
30901 /* It's a deduction guide, return true. */;
30902 else
30903 cp_parser_simulate_error (parser);
30906 /* If there was no class-name, then this is not a constructor.
30907 Otherwise, if we are in a class-specifier and we aren't
30908 handling a friend declaration, check that its type matches
30909 current_class_type (c++/38313). Note: error_mark_node
30910 is left alone for error recovery purposes. */
30911 constructor_p = (!cp_parser_error_occurred (parser)
30912 && (outside_class_specifier_p
30913 || type_decl == NULL_TREE
30914 || type_decl == error_mark_node
30915 || same_type_p (current_class_type,
30916 TREE_TYPE (type_decl))));
30918 /* If we're still considering a constructor, we have to see a `(',
30919 to begin the parameter-declaration-clause, followed by either a
30920 `)', an `...', or a decl-specifier. We need to check for a
30921 type-specifier to avoid being fooled into thinking that:
30923 S (f) (int);
30925 is a constructor. (It is actually a function named `f' that
30926 takes one parameter (of type `int') and returns a value of type
30927 `S'. */
30928 if (constructor_p
30929 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30930 constructor_p = false;
30932 if (constructor_p
30933 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
30934 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
30935 /* A parameter declaration begins with a decl-specifier,
30936 which is either the "attribute" keyword, a storage class
30937 specifier, or (usually) a type-specifier. */
30938 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
30939 /* GNU attributes can actually appear both at the start of
30940 a parameter and parenthesized declarator.
30941 S (__attribute__((unused)) int);
30942 is a constructor, but
30943 S (__attribute__((unused)) foo) (int);
30944 is a function declaration. [[attribute]] can appear in the
30945 first form too, but not in the second form. */
30946 && !cp_next_tokens_can_be_std_attribute_p (parser))
30948 tree type;
30949 tree pushed_scope = NULL_TREE;
30950 unsigned saved_num_template_parameter_lists;
30952 if (cp_parser_allow_gnu_extensions_p (parser)
30953 && cp_next_tokens_can_be_gnu_attribute_p (parser))
30955 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
30956 while (--n)
30957 cp_lexer_consume_token (parser->lexer);
30960 /* Names appearing in the type-specifier should be looked up
30961 in the scope of the class. */
30962 if (current_class_type)
30963 type = NULL_TREE;
30964 else if (type_decl)
30966 type = TREE_TYPE (type_decl);
30967 if (TREE_CODE (type) == TYPENAME_TYPE)
30969 type = resolve_typename_type (type,
30970 /*only_current_p=*/false);
30971 if (TREE_CODE (type) == TYPENAME_TYPE)
30973 cp_parser_abort_tentative_parse (parser);
30974 return false;
30977 pushed_scope = push_scope (type);
30980 /* Inside the constructor parameter list, surrounding
30981 template-parameter-lists do not apply. */
30982 saved_num_template_parameter_lists
30983 = parser->num_template_parameter_lists;
30984 parser->num_template_parameter_lists = 0;
30986 /* Look for the type-specifier. It's not optional, but its typename
30987 might be. Unless this is a friend declaration; we don't want to
30988 treat
30990 friend S (T::fn)(int);
30992 as a constructor, but with P0634, we might assume a type when
30993 looking for the type-specifier. It is actually a function named
30994 `T::fn' that takes one parameter (of type `int') and returns a
30995 value of type `S'. Constructors can be friends, but they must
30996 use a qualified name.
30998 Parse with an empty set of declaration specifiers since we're
30999 trying to match a decl-specifier-seq of the first parameter.
31000 This must be non-null so that cp_parser_simple_type_specifier
31001 will recognize a constrained placeholder type such as:
31002 'C<int> auto' where C is a type concept. */
31003 cp_decl_specifier_seq ctor_specs;
31004 clear_decl_specs (&ctor_specs);
31005 cp_parser_type_specifier (parser,
31006 (friend_p ? CP_PARSER_FLAGS_NONE
31007 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
31008 /*decl_specs=*/&ctor_specs,
31009 /*is_declarator=*/true,
31010 /*declares_class_or_enum=*/NULL,
31011 /*is_cv_qualifier=*/NULL);
31013 parser->num_template_parameter_lists
31014 = saved_num_template_parameter_lists;
31016 /* Leave the scope of the class. */
31017 if (pushed_scope)
31018 pop_scope (pushed_scope);
31020 constructor_p = !cp_parser_error_occurred (parser);
31024 /* We did not really want to consume any tokens. */
31025 cp_parser_abort_tentative_parse (parser);
31027 return constructor_p;
31030 /* Parse the definition of the function given by the DECL_SPECIFIERS,
31031 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
31032 they must be performed once we are in the scope of the function.
31034 Returns the function defined. */
31036 static tree
31037 cp_parser_function_definition_from_specifiers_and_declarator
31038 (cp_parser* parser,
31039 cp_decl_specifier_seq *decl_specifiers,
31040 tree attributes,
31041 const cp_declarator *declarator)
31043 tree fn;
31044 bool success_p;
31046 /* Begin the function-definition. */
31047 success_p = start_function (decl_specifiers, declarator, attributes);
31049 /* The things we're about to see are not directly qualified by any
31050 template headers we've seen thus far. */
31051 reset_specialization ();
31053 /* If there were names looked up in the decl-specifier-seq that we
31054 did not check, check them now. We must wait until we are in the
31055 scope of the function to perform the checks, since the function
31056 might be a friend. */
31057 perform_deferred_access_checks (tf_warning_or_error);
31059 if (success_p)
31061 cp_finalize_omp_declare_simd (parser, current_function_decl);
31062 parser->omp_declare_simd = NULL;
31063 cp_finalize_oacc_routine (parser, current_function_decl, true);
31064 parser->oacc_routine = NULL;
31067 if (!success_p)
31069 /* Skip the entire function. */
31070 cp_parser_skip_to_end_of_block_or_statement (parser);
31071 fn = error_mark_node;
31073 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
31075 /* Seen already, skip it. An error message has already been output. */
31076 cp_parser_skip_to_end_of_block_or_statement (parser);
31077 fn = current_function_decl;
31078 current_function_decl = NULL_TREE;
31079 /* If this is a function from a class, pop the nested class. */
31080 if (current_class_name)
31081 pop_nested_class ();
31083 else
31085 timevar_id_t tv;
31086 if (DECL_DECLARED_INLINE_P (current_function_decl))
31087 tv = TV_PARSE_INLINE;
31088 else
31089 tv = TV_PARSE_FUNC;
31090 timevar_push (tv);
31091 fn = cp_parser_function_definition_after_declarator (parser,
31092 /*inline_p=*/false);
31093 timevar_pop (tv);
31096 return fn;
31099 /* Parse the part of a function-definition that follows the
31100 declarator. INLINE_P is TRUE iff this function is an inline
31101 function defined within a class-specifier.
31103 Returns the function defined. */
31105 static tree
31106 cp_parser_function_definition_after_declarator (cp_parser* parser,
31107 bool inline_p)
31109 tree fn;
31110 bool saved_in_unbraced_linkage_specification_p;
31111 bool saved_in_function_body;
31112 unsigned saved_num_template_parameter_lists;
31113 cp_token *token;
31114 bool fully_implicit_function_template_p
31115 = parser->fully_implicit_function_template_p;
31116 parser->fully_implicit_function_template_p = false;
31117 tree implicit_template_parms
31118 = parser->implicit_template_parms;
31119 parser->implicit_template_parms = 0;
31120 cp_binding_level* implicit_template_scope
31121 = parser->implicit_template_scope;
31122 parser->implicit_template_scope = 0;
31124 saved_in_function_body = parser->in_function_body;
31125 parser->in_function_body = true;
31126 /* If the next token is `return', then the code may be trying to
31127 make use of the "named return value" extension that G++ used to
31128 support. */
31129 token = cp_lexer_peek_token (parser->lexer);
31130 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
31132 /* Consume the `return' keyword. */
31133 cp_lexer_consume_token (parser->lexer);
31134 /* Look for the identifier that indicates what value is to be
31135 returned. */
31136 cp_parser_identifier (parser);
31137 /* Issue an error message. */
31138 error_at (token->location,
31139 "named return values are no longer supported");
31140 /* Skip tokens until we reach the start of the function body. */
31141 while (true)
31143 cp_token *token = cp_lexer_peek_token (parser->lexer);
31144 if (token->type == CPP_OPEN_BRACE
31145 || token->type == CPP_EOF
31146 || token->type == CPP_PRAGMA_EOL)
31147 break;
31148 cp_lexer_consume_token (parser->lexer);
31151 /* The `extern' in `extern "C" void f () { ... }' does not apply to
31152 anything declared inside `f'. */
31153 saved_in_unbraced_linkage_specification_p
31154 = parser->in_unbraced_linkage_specification_p;
31155 parser->in_unbraced_linkage_specification_p = false;
31156 /* Inside the function, surrounding template-parameter-lists do not
31157 apply. */
31158 saved_num_template_parameter_lists
31159 = parser->num_template_parameter_lists;
31160 parser->num_template_parameter_lists = 0;
31162 /* If the next token is `try', `__transaction_atomic', or
31163 `__transaction_relaxed`, then we are looking at either function-try-block
31164 or function-transaction-block. Note that all of these include the
31165 function-body. */
31166 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
31167 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
31168 else if (cp_lexer_next_token_is_keyword (parser->lexer,
31169 RID_TRANSACTION_RELAXED))
31170 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
31171 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31172 cp_parser_function_try_block (parser);
31173 else
31174 cp_parser_ctor_initializer_opt_and_function_body
31175 (parser, /*in_function_try_block=*/false);
31177 /* Finish the function. */
31178 fn = finish_function (inline_p);
31180 if (modules_p ()
31181 && !inline_p
31182 && TYPE_P (DECL_CONTEXT (fn))
31183 && (DECL_DECLARED_INLINE_P (fn)
31184 || processing_template_decl))
31185 set_defining_module (fn);
31187 /* Generate code for it, if necessary. */
31188 expand_or_defer_fn (fn);
31189 /* Restore the saved values. */
31190 parser->in_unbraced_linkage_specification_p
31191 = saved_in_unbraced_linkage_specification_p;
31192 parser->num_template_parameter_lists
31193 = saved_num_template_parameter_lists;
31194 parser->in_function_body = saved_in_function_body;
31196 parser->fully_implicit_function_template_p
31197 = fully_implicit_function_template_p;
31198 parser->implicit_template_parms
31199 = implicit_template_parms;
31200 parser->implicit_template_scope
31201 = implicit_template_scope;
31203 if (parser->fully_implicit_function_template_p)
31204 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
31206 return fn;
31209 /* Parse a template-declaration body (following argument list). */
31211 static void
31212 cp_parser_template_declaration_after_parameters (cp_parser* parser,
31213 tree parameter_list,
31214 bool member_p)
31216 tree decl = NULL_TREE;
31217 bool friend_p = false;
31219 /* We just processed one more parameter list. */
31220 ++parser->num_template_parameter_lists;
31222 /* Get the deferred access checks from the parameter list. These
31223 will be checked once we know what is being declared, as for a
31224 member template the checks must be performed in the scope of the
31225 class containing the member. */
31226 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
31228 /* Tentatively parse for a new template parameter list, which can either be
31229 the template keyword or a template introduction. */
31230 if (cp_parser_template_declaration_after_export (parser, member_p))
31231 /* OK */;
31232 else if (cxx_dialect >= cxx11
31233 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
31234 decl = cp_parser_alias_declaration (parser);
31235 else if (cxx_dialect >= cxx20 /* Implies flag_concept. */
31236 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
31237 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
31238 /* Allow 'concept bool' to be handled as per the TS. */
31239 decl = cp_parser_concept_definition (parser);
31240 else
31242 cp_token *token = cp_lexer_peek_token (parser->lexer);
31243 decl = cp_parser_single_declaration (parser,
31244 checks,
31245 member_p,
31246 /*explicit_specialization_p=*/false,
31247 &friend_p);
31249 /* If this is a member template declaration, let the front
31250 end know. */
31251 if (member_p && !friend_p && decl)
31253 if (TREE_CODE (decl) == TYPE_DECL)
31254 cp_parser_check_access_in_redeclaration (decl, token->location);
31256 decl = finish_member_template_decl (decl);
31258 else if (friend_p && decl
31259 && DECL_DECLARES_TYPE_P (decl))
31260 make_friend_class (current_class_type, TREE_TYPE (decl),
31261 /*complain=*/true);
31263 /* We are done with the current parameter list. */
31264 --parser->num_template_parameter_lists;
31266 pop_deferring_access_checks ();
31268 /* Finish up. */
31269 finish_template_decl (parameter_list);
31271 /* Check the template arguments for a literal operator template. */
31272 if (decl
31273 && DECL_DECLARES_FUNCTION_P (decl)
31274 && UDLIT_OPER_P (DECL_NAME (decl)))
31276 bool ok = true;
31277 if (parameter_list == NULL_TREE)
31278 ok = false;
31279 else
31281 int num_parms = TREE_VEC_LENGTH (parameter_list);
31282 if (num_parms == 1)
31284 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
31285 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31286 if (TREE_CODE (parm) != PARM_DECL)
31287 ok = false;
31288 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
31289 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31290 /* OK, C++20 string literal operator template. We don't need
31291 to warn in lower dialects here because we will have already
31292 warned about the template parameter. */;
31293 else if (TREE_TYPE (parm) != char_type_node
31294 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31295 ok = false;
31297 else if (num_parms == 2 && cxx_dialect >= cxx14)
31299 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
31300 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
31301 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
31302 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31303 if (TREE_CODE (parm) != PARM_DECL
31304 || TREE_TYPE (parm) != TREE_TYPE (type)
31305 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31306 ok = false;
31307 else
31308 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
31309 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
31310 "ISO C++ did not adopt string literal operator templa"
31311 "tes taking an argument pack of characters");
31313 else
31314 ok = false;
31316 if (!ok)
31318 if (cxx_dialect > cxx17)
31319 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31320 "template %qD has invalid parameter list; expected "
31321 "non-type template parameter pack %<<char...>%> or "
31322 "single non-type parameter of class type",
31323 decl);
31324 else
31325 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31326 "template %qD has invalid parameter list; expected "
31327 "non-type template parameter pack %<<char...>%>",
31328 decl);
31332 /* Register member declarations. */
31333 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
31334 finish_member_declaration (decl);
31335 /* If DECL is a function template, we must return to parse it later.
31336 (Even though there is no definition, there might be default
31337 arguments that need handling.) */
31338 if (member_p && decl
31339 && DECL_DECLARES_FUNCTION_P (decl))
31340 vec_safe_push (unparsed_funs_with_definitions, decl);
31343 /* Parse a template introduction header for a template-declaration. Returns
31344 false if tentative parse fails. */
31346 static bool
31347 cp_parser_template_introduction (cp_parser* parser, bool member_p)
31349 cp_parser_parse_tentatively (parser);
31351 tree saved_scope = parser->scope;
31352 tree saved_object_scope = parser->object_scope;
31353 tree saved_qualifying_scope = parser->qualifying_scope;
31355 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
31357 /* Look for the optional `::' operator. */
31358 cp_parser_global_scope_opt (parser,
31359 /*current_scope_valid_p=*/false);
31360 /* Look for the nested-name-specifier. */
31361 cp_parser_nested_name_specifier_opt (parser,
31362 /*typename_keyword_p=*/false,
31363 /*check_dependency_p=*/true,
31364 /*type_p=*/false,
31365 /*is_declaration=*/false);
31367 cp_token *token = cp_lexer_peek_token (parser->lexer);
31368 tree concept_name = cp_parser_identifier (parser);
31370 /* Look up the concept for which we will be matching
31371 template parameters. */
31372 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
31373 token->location);
31374 parser->scope = saved_scope;
31375 parser->object_scope = saved_object_scope;
31376 parser->qualifying_scope = saved_qualifying_scope;
31378 if (concept_name == error_mark_node
31379 || (seen_error () && !concept_definition_p (tmpl_decl)))
31380 cp_parser_simulate_error (parser);
31382 /* Look for opening brace for introduction. */
31383 matching_braces braces;
31384 braces.require_open (parser);
31385 location_t open_loc = input_location;
31387 if (!cp_parser_parse_definitely (parser))
31388 return false;
31390 push_deferring_access_checks (dk_deferred);
31392 /* Build vector of placeholder parameters and grab
31393 matching identifiers. */
31394 tree introduction_list = cp_parser_introduction_list (parser);
31396 /* Look for closing brace for introduction. */
31397 if (!braces.require_close (parser))
31398 return true;
31400 /* The introduction-list shall not be empty. */
31401 int nargs = TREE_VEC_LENGTH (introduction_list);
31402 if (nargs == 0)
31404 /* In cp_parser_introduction_list we have already issued an error. */
31405 return true;
31408 if (tmpl_decl == error_mark_node)
31410 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
31411 token->location);
31412 return true;
31415 /* Build and associate the constraint. */
31416 location_t introduction_loc = make_location (open_loc,
31417 start_token->location,
31418 parser->lexer);
31419 tree parms = finish_template_introduction (tmpl_decl,
31420 introduction_list,
31421 introduction_loc);
31422 if (parms && parms != error_mark_node)
31424 if (!flag_concepts_ts)
31425 pedwarn (introduction_loc, 0, "template-introductions"
31426 " are not part of C++20 concepts; use %qs to enable",
31427 "-fconcepts-ts");
31429 cp_parser_template_declaration_after_parameters (parser, parms,
31430 member_p);
31431 return true;
31434 if (parms == NULL_TREE)
31435 error_at (token->location, "no matching concept for template-introduction");
31437 return true;
31440 /* Parse a normal template-declaration following the template keyword. */
31442 static void
31443 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
31445 tree parameter_list;
31446 bool need_lang_pop;
31447 location_t location = input_location;
31449 /* Look for the `<' token. */
31450 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
31451 return;
31452 if (at_class_scope_p () && current_function_decl)
31454 /* 14.5.2.2 [temp.mem]
31456 A local class shall not have member templates. */
31457 error_at (location,
31458 "invalid declaration of member template in local class");
31459 cp_parser_skip_to_end_of_block_or_statement (parser);
31460 return;
31462 /* [temp]
31464 A template ... shall not have C linkage. */
31465 if (current_lang_name == lang_name_c)
31467 error_at (location, "template with C linkage");
31468 maybe_show_extern_c_location ();
31469 /* Give it C++ linkage to avoid confusing other parts of the
31470 front end. */
31471 push_lang_context (lang_name_cplusplus);
31472 need_lang_pop = true;
31474 else
31475 need_lang_pop = false;
31477 /* We cannot perform access checks on the template parameter
31478 declarations until we know what is being declared, just as we
31479 cannot check the decl-specifier list. */
31480 push_deferring_access_checks (dk_deferred);
31482 /* If the next token is `>', then we have an invalid
31483 specialization. Rather than complain about an invalid template
31484 parameter, issue an error message here. */
31485 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
31487 cp_parser_error (parser, "invalid explicit specialization");
31488 begin_specialization ();
31489 parameter_list = NULL_TREE;
31491 else
31493 /* Parse the template parameters. */
31494 parameter_list = cp_parser_template_parameter_list (parser);
31497 /* Look for the `>'. */
31498 cp_parser_skip_to_end_of_template_parameter_list (parser);
31500 /* Manage template requirements */
31501 if (flag_concepts)
31503 tree reqs = get_shorthand_constraints (current_template_parms);
31504 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
31505 reqs = combine_constraint_expressions (reqs, treqs);
31506 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
31509 cp_parser_template_declaration_after_parameters (parser, parameter_list,
31510 member_p);
31512 /* For the erroneous case of a template with C linkage, we pushed an
31513 implicit C++ linkage scope; exit that scope now. */
31514 if (need_lang_pop)
31515 pop_lang_context ();
31518 /* Parse a template-declaration, assuming that the `export' (and
31519 `extern') keywords, if present, has already been scanned. MEMBER_P
31520 is as for cp_parser_template_declaration. */
31522 static bool
31523 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
31525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31527 cp_lexer_consume_token (parser->lexer);
31528 cp_parser_explicit_template_declaration (parser, member_p);
31529 return true;
31531 else if (flag_concepts)
31532 return cp_parser_template_introduction (parser, member_p);
31534 return false;
31537 /* Perform the deferred access checks from a template-parameter-list.
31538 CHECKS is a TREE_LIST of access checks, as returned by
31539 get_deferred_access_checks. */
31541 static void
31542 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
31544 ++processing_template_parmlist;
31545 perform_access_checks (checks, tf_warning_or_error);
31546 --processing_template_parmlist;
31549 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
31550 `function-definition' sequence that follows a template header.
31551 If MEMBER_P is true, this declaration appears in a class scope.
31553 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
31554 *FRIEND_P is set to TRUE iff the declaration is a friend. */
31556 static tree
31557 cp_parser_single_declaration (cp_parser* parser,
31558 vec<deferred_access_check, va_gc> *checks,
31559 bool member_p,
31560 bool explicit_specialization_p,
31561 bool* friend_p)
31563 int declares_class_or_enum;
31564 tree decl = NULL_TREE;
31565 cp_decl_specifier_seq decl_specifiers;
31566 bool function_definition_p = false;
31567 cp_token *decl_spec_token_start;
31569 /* This function is only used when processing a template
31570 declaration. */
31571 gcc_assert (innermost_scope_kind () == sk_template_parms
31572 || innermost_scope_kind () == sk_template_spec);
31574 /* Defer access checks until we know what is being declared. */
31575 push_deferring_access_checks (dk_deferred);
31577 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
31578 alternative. */
31579 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
31580 cp_parser_decl_specifier_seq (parser,
31581 (CP_PARSER_FLAGS_OPTIONAL
31582 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
31583 &decl_specifiers,
31584 &declares_class_or_enum);
31586 cp_omp_declare_simd_data odsd;
31587 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
31588 cp_parser_handle_directive_omp_attributes (parser,
31589 &decl_specifiers.attributes,
31590 &odsd, true);
31592 if (friend_p)
31593 *friend_p = cp_parser_friend_p (&decl_specifiers);
31595 /* There are no template typedefs. */
31596 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
31598 error_at (decl_spec_token_start->location,
31599 "template declaration of %<typedef%>");
31600 decl = error_mark_node;
31603 /* Gather up the access checks that occurred the
31604 decl-specifier-seq. */
31605 stop_deferring_access_checks ();
31607 /* Check for the declaration of a template class. */
31608 if (declares_class_or_enum)
31610 if (cp_parser_declares_only_class_p (parser)
31611 || (declares_class_or_enum & 2))
31613 /* If this is a declaration, but not a definition, associate
31614 any constraints with the type declaration. Constraints
31615 are associated with definitions in cp_parser_class_specifier. */
31616 if (declares_class_or_enum == 1)
31617 associate_classtype_constraints (decl_specifiers.type);
31619 decl = shadow_tag (&decl_specifiers);
31621 /* In this case:
31623 struct C {
31624 friend template <typename T> struct A<T>::B;
31627 A<T>::B will be represented by a TYPENAME_TYPE, and
31628 therefore not recognized by shadow_tag. */
31629 if (friend_p && *friend_p
31630 && !decl
31631 && decl_specifiers.type
31632 && TYPE_P (decl_specifiers.type))
31633 decl = decl_specifiers.type;
31635 if (decl && decl != error_mark_node)
31636 decl = TYPE_NAME (decl);
31637 else
31638 decl = error_mark_node;
31640 /* Perform access checks for template parameters. */
31641 cp_parser_perform_template_parameter_access_checks (checks);
31643 /* Give a helpful diagnostic for
31644 template <class T> struct A { } a;
31645 if we aren't already recovering from an error. */
31646 if (!cp_parser_declares_only_class_p (parser)
31647 && !seen_error ())
31649 error_at (cp_lexer_peek_token (parser->lexer)->location,
31650 "a class template declaration must not declare "
31651 "anything else");
31652 cp_parser_skip_to_end_of_block_or_statement (parser);
31653 goto out;
31658 /* Complain about missing 'typename' or other invalid type names. */
31659 if (!decl_specifiers.any_type_specifiers_p
31660 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
31662 /* cp_parser_parse_and_diagnose_invalid_type_name calls
31663 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
31664 the rest of this declaration. */
31665 decl = error_mark_node;
31666 goto out;
31669 /* If it's not a template class, try for a template function. If
31670 the next token is a `;', then this declaration does not declare
31671 anything. But, if there were errors in the decl-specifiers, then
31672 the error might well have come from an attempted class-specifier.
31673 In that case, there's no need to warn about a missing declarator. */
31674 if (!decl
31675 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
31676 || decl_specifiers.type != error_mark_node))
31678 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
31679 /* We don't delay parsing for friends, though CWG 2510 may change
31680 that. */
31681 if (member_p && !(friend_p && *friend_p))
31682 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
31683 decl = cp_parser_init_declarator (parser,
31684 flags,
31685 &decl_specifiers,
31686 checks,
31687 /*function_definition_allowed_p=*/true,
31688 member_p,
31689 declares_class_or_enum,
31690 &function_definition_p,
31691 NULL, NULL, NULL);
31693 /* 7.1.1-1 [dcl.stc]
31695 A storage-class-specifier shall not be specified in an explicit
31696 specialization... */
31697 if (decl
31698 && explicit_specialization_p
31699 && decl_specifiers.storage_class != sc_none)
31701 error_at (decl_spec_token_start->location,
31702 "explicit template specialization cannot have a storage class");
31703 decl = error_mark_node;
31706 if (decl && VAR_P (decl))
31707 check_template_variable (decl);
31710 /* Look for a trailing `;' after the declaration. */
31711 if (!function_definition_p
31712 && (decl == error_mark_node
31713 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
31714 cp_parser_skip_to_end_of_block_or_statement (parser);
31716 out:
31717 pop_deferring_access_checks ();
31719 /* Clear any current qualification; whatever comes next is the start
31720 of something new. */
31721 parser->scope = NULL_TREE;
31722 parser->qualifying_scope = NULL_TREE;
31723 parser->object_scope = NULL_TREE;
31725 cp_finalize_omp_declare_simd (parser, &odsd);
31727 return decl;
31730 /* Parse a cast-expression that is not the operand of a unary "&". */
31732 static cp_expr
31733 cp_parser_simple_cast_expression (cp_parser *parser)
31735 return cp_parser_cast_expression (parser, /*address_p=*/false,
31736 /*cast_p=*/false, /*decltype*/false, NULL);
31739 /* Parse a functional cast to TYPE. Returns an expression
31740 representing the cast. */
31742 static cp_expr
31743 cp_parser_functional_cast (cp_parser* parser, tree type)
31745 vec<tree, va_gc> *vec;
31746 tree expression_list;
31747 cp_expr cast;
31748 bool nonconst_p;
31750 location_t start_loc = input_location;
31752 if (!type)
31753 type = error_mark_node;
31755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31757 cp_lexer_set_source_position (parser->lexer);
31758 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
31759 expression_list = cp_parser_braced_list (parser, &nonconst_p);
31760 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
31761 if (TREE_CODE (type) == TYPE_DECL)
31762 type = TREE_TYPE (type);
31764 cast = finish_compound_literal (type, expression_list,
31765 tf_warning_or_error, fcl_functional);
31766 /* Create a location of the form:
31767 type_name{i, f}
31768 ^~~~~~~~~~~~~~~
31769 with caret == start at the start of the type name,
31770 finishing at the closing brace. */
31771 location_t combined_loc = make_location (start_loc, start_loc,
31772 parser->lexer);
31773 cast.set_location (combined_loc);
31774 return cast;
31778 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
31779 /*cast_p=*/true,
31780 /*allow_expansion_p=*/true,
31781 /*non_constant_p=*/NULL);
31782 if (vec == NULL)
31783 expression_list = error_mark_node;
31784 else
31786 expression_list = build_tree_list_vec (vec);
31787 release_tree_vector (vec);
31790 /* Create a location of the form:
31791 float(i)
31792 ^~~~~~~~
31793 with caret == start at the start of the type name,
31794 finishing at the closing paren. */
31795 location_t combined_loc = make_location (start_loc, start_loc,
31796 parser->lexer);
31797 cast = build_functional_cast (combined_loc, type, expression_list,
31798 tf_warning_or_error);
31800 /* [expr.const]/1: In an integral constant expression "only type
31801 conversions to integral or enumeration type can be used". */
31802 if (TREE_CODE (type) == TYPE_DECL)
31803 type = TREE_TYPE (type);
31804 if (cast != error_mark_node
31805 && !cast_valid_in_integral_constant_expression_p (type)
31806 && cp_parser_non_integral_constant_expression (parser,
31807 NIC_CONSTRUCTOR))
31808 return error_mark_node;
31810 return cast;
31813 /* Save the tokens that make up the body of a member function defined
31814 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
31815 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
31816 specifiers applied to the declaration. Returns the FUNCTION_DECL
31817 for the member function. */
31819 static tree
31820 cp_parser_save_member_function_body (cp_parser* parser,
31821 cp_decl_specifier_seq *decl_specifiers,
31822 cp_declarator *declarator,
31823 tree attributes)
31825 cp_token *first;
31826 cp_token *last;
31827 tree fn;
31828 bool function_try_block = false;
31830 /* Create the FUNCTION_DECL. */
31831 fn = grokmethod (decl_specifiers, declarator, attributes);
31832 cp_finalize_omp_declare_simd (parser, fn);
31833 cp_finalize_oacc_routine (parser, fn, true);
31834 /* If something went badly wrong, bail out now. */
31835 if (fn == error_mark_node)
31837 /* If there's a function-body, skip it. */
31838 if (cp_parser_token_starts_function_definition_p
31839 (cp_lexer_peek_token (parser->lexer)))
31840 cp_parser_skip_to_end_of_block_or_statement (parser);
31841 return error_mark_node;
31844 /* Remember it, if there are default args to post process. */
31845 cp_parser_save_default_args (parser, fn);
31847 /* Save away the tokens that make up the body of the
31848 function. */
31849 first = parser->lexer->next_token;
31851 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
31852 cp_lexer_consume_token (parser->lexer);
31853 else if (cp_lexer_next_token_is_keyword (parser->lexer,
31854 RID_TRANSACTION_ATOMIC))
31856 cp_lexer_consume_token (parser->lexer);
31857 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
31858 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
31859 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
31860 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
31861 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
31862 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
31863 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
31865 cp_lexer_consume_token (parser->lexer);
31866 cp_lexer_consume_token (parser->lexer);
31867 cp_lexer_consume_token (parser->lexer);
31868 cp_lexer_consume_token (parser->lexer);
31869 cp_lexer_consume_token (parser->lexer);
31871 else
31872 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
31873 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31875 cp_lexer_consume_token (parser->lexer);
31876 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
31877 break;
31881 /* Handle function try blocks. */
31882 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31884 cp_lexer_consume_token (parser->lexer);
31885 function_try_block = true;
31887 /* We can have braced-init-list mem-initializers before the fn body. */
31888 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31890 cp_lexer_consume_token (parser->lexer);
31891 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
31893 /* cache_group will stop after an un-nested { } pair, too. */
31894 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
31895 break;
31897 /* variadic mem-inits have ... after the ')'. */
31898 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31899 cp_lexer_consume_token (parser->lexer);
31902 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
31903 /* Handle function try blocks. */
31904 if (function_try_block)
31905 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
31906 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
31907 last = parser->lexer->next_token;
31909 /* Save away the inline definition; we will process it when the
31910 class is complete. */
31911 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
31912 DECL_PENDING_INLINE_P (fn) = 1;
31914 /* We need to know that this was defined in the class, so that
31915 friend templates are handled correctly. */
31916 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
31918 /* Add FN to the queue of functions to be parsed later. */
31919 vec_safe_push (unparsed_funs_with_definitions, fn);
31921 return fn;
31924 /* Save the tokens that make up the in-class initializer for a non-static
31925 data member. Returns a DEFERRED_PARSE. */
31927 static tree
31928 cp_parser_save_nsdmi (cp_parser* parser)
31930 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
31933 /* Parse a template-argument-list, as well as the trailing ">" (but
31934 not the opening "<"). See cp_parser_template_argument_list for the
31935 return value. */
31937 static tree
31938 cp_parser_enclosed_template_argument_list (cp_parser* parser)
31940 tree arguments;
31941 tree saved_scope;
31942 tree saved_qualifying_scope;
31943 tree saved_object_scope;
31944 bool saved_greater_than_is_operator_p;
31946 /* [temp.names]
31948 When parsing a template-id, the first non-nested `>' is taken as
31949 the end of the template-argument-list rather than a greater-than
31950 operator. */
31951 saved_greater_than_is_operator_p
31952 = parser->greater_than_is_operator_p;
31953 parser->greater_than_is_operator_p = false;
31954 /* Parsing the argument list may modify SCOPE, so we save it
31955 here. */
31956 saved_scope = parser->scope;
31957 saved_qualifying_scope = parser->qualifying_scope;
31958 saved_object_scope = parser->object_scope;
31959 /* We need to evaluate the template arguments, even though this
31960 template-id may be nested within a "sizeof". */
31961 cp_evaluated ev;
31962 /* Parse the template-argument-list itself. */
31963 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
31964 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
31965 arguments = NULL_TREE;
31966 else
31967 arguments = cp_parser_template_argument_list (parser);
31968 /* Look for the `>' that ends the template-argument-list. If we find
31969 a '>>' instead, it's probably just a typo. */
31970 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
31972 if (cxx_dialect != cxx98)
31974 /* In C++0x, a `>>' in a template argument list or cast
31975 expression is considered to be two separate `>'
31976 tokens. So, change the current token to a `>', but don't
31977 consume it: it will be consumed later when the outer
31978 template argument list (or cast expression) is parsed.
31979 Note that this replacement of `>' for `>>' is necessary
31980 even if we are parsing tentatively: in the tentative
31981 case, after calling
31982 cp_parser_enclosed_template_argument_list we will always
31983 throw away all of the template arguments and the first
31984 closing `>', either because the template argument list
31985 was erroneous or because we are replacing those tokens
31986 with a CPP_TEMPLATE_ID token. The second `>' (which will
31987 not have been thrown away) is needed either to close an
31988 outer template argument list or to complete a new-style
31989 cast. */
31990 cp_token *token = cp_lexer_peek_token (parser->lexer);
31991 token->type = CPP_GREATER;
31993 else if (!saved_greater_than_is_operator_p)
31995 /* If we're in a nested template argument list, the '>>' has
31996 to be a typo for '> >'. We emit the error message, but we
31997 continue parsing and we push a '>' as next token, so that
31998 the argument list will be parsed correctly. Note that the
31999 global source location is still on the token before the
32000 '>>', so we need to say explicitly where we want it. */
32001 cp_token *token = cp_lexer_peek_token (parser->lexer);
32002 gcc_rich_location richloc (token->location);
32003 richloc.add_fixit_replace ("> >");
32004 error_at (&richloc, "%<>>%> should be %<> >%> "
32005 "within a nested template argument list");
32007 token->type = CPP_GREATER;
32009 else
32011 /* If this is not a nested template argument list, the '>>'
32012 is a typo for '>'. Emit an error message and continue.
32013 Same deal about the token location, but here we can get it
32014 right by consuming the '>>' before issuing the diagnostic. */
32015 cp_token *token = cp_lexer_consume_token (parser->lexer);
32016 error_at (token->location,
32017 "spurious %<>>%>, use %<>%> to terminate "
32018 "a template argument list");
32021 else
32022 cp_parser_skip_to_end_of_template_parameter_list (parser);
32023 /* The `>' token might be a greater-than operator again now. */
32024 parser->greater_than_is_operator_p
32025 = saved_greater_than_is_operator_p;
32026 /* Restore the SAVED_SCOPE. */
32027 parser->scope = saved_scope;
32028 parser->qualifying_scope = saved_qualifying_scope;
32029 parser->object_scope = saved_object_scope;
32031 return arguments;
32034 /* MEMBER_FUNCTION is a member function, or a friend. If default
32035 arguments, or the body of the function have not yet been parsed,
32036 parse them now. */
32038 static void
32039 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
32041 timevar_push (TV_PARSE_INMETH);
32042 /* If this member is a template, get the underlying
32043 FUNCTION_DECL. */
32044 if (DECL_FUNCTION_TEMPLATE_P (member_function))
32045 member_function = DECL_TEMPLATE_RESULT (member_function);
32047 /* There should not be any class definitions in progress at this
32048 point; the bodies of members are only parsed outside of all class
32049 definitions. */
32050 gcc_assert (parser->num_classes_being_defined == 0);
32051 /* While we're parsing the member functions we might encounter more
32052 classes. We want to handle them right away, but we don't want
32053 them getting mixed up with functions that are currently in the
32054 queue. */
32055 push_unparsed_function_queues (parser);
32057 /* Make sure that any template parameters are in scope. */
32058 maybe_begin_member_template_processing (member_function);
32060 /* If the body of the function has not yet been parsed, parse it
32061 now. */
32062 if (DECL_PENDING_INLINE_P (member_function))
32064 tree function_scope;
32065 cp_token_cache *tokens;
32067 /* The function is no longer pending; we are processing it. */
32068 tokens = DECL_PENDING_INLINE_INFO (member_function);
32069 DECL_PENDING_INLINE_INFO (member_function) = NULL;
32070 DECL_PENDING_INLINE_P (member_function) = 0;
32072 /* If this is a local class, enter the scope of the containing
32073 function. */
32074 function_scope = current_function_decl;
32075 if (function_scope)
32076 push_function_context ();
32078 /* Push the body of the function onto the lexer stack. */
32079 cp_parser_push_lexer_for_tokens (parser, tokens);
32081 /* Let the front end know that we going to be defining this
32082 function. */
32083 start_preparsed_function (member_function, NULL_TREE,
32084 SF_PRE_PARSED | SF_INCLASS_INLINE);
32086 /* #pragma omp declare reduction needs special parsing. */
32087 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
32089 parser->lexer->in_pragma = true;
32090 cp_parser_omp_declare_reduction_exprs (member_function, parser);
32091 finish_function (/*inline_p=*/true);
32092 cp_check_omp_declare_reduction (member_function);
32094 else
32095 /* Now, parse the body of the function. */
32096 cp_parser_function_definition_after_declarator (parser,
32097 /*inline_p=*/true);
32099 /* Leave the scope of the containing function. */
32100 if (function_scope)
32101 pop_function_context ();
32102 cp_parser_pop_lexer (parser);
32105 /* Remove any template parameters from the symbol table. */
32106 maybe_end_member_template_processing ();
32108 /* Restore the queue. */
32109 pop_unparsed_function_queues (parser);
32110 timevar_pop (TV_PARSE_INMETH);
32113 /* If DECL contains any default args, remember it on the unparsed
32114 functions queue. */
32116 static void
32117 cp_parser_save_default_args (cp_parser* parser, tree decl)
32119 tree probe;
32121 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
32122 probe;
32123 probe = TREE_CHAIN (probe))
32124 if (TREE_PURPOSE (probe))
32126 cp_default_arg_entry entry = {current_class_type, decl};
32127 vec_safe_push (unparsed_funs_with_default_args, entry);
32128 break;
32131 /* Remember if there is a noexcept-specifier to post process. */
32132 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
32133 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
32134 vec_safe_push (unparsed_noexcepts, decl);
32137 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
32138 which is either a FIELD_DECL or PARM_DECL. Parse it and return
32139 the result. For a PARM_DECL, PARMTYPE is the corresponding type
32140 from the parameter-type-list. */
32142 static tree
32143 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
32144 tree default_arg, tree parmtype)
32146 cp_token_cache *tokens;
32147 tree parsed_arg;
32148 bool dummy;
32150 if (default_arg == error_mark_node)
32151 return error_mark_node;
32153 /* Push the saved tokens for the default argument onto the parser's
32154 lexer stack. */
32155 tokens = DEFPARSE_TOKENS (default_arg);
32156 cp_parser_push_lexer_for_tokens (parser, tokens);
32158 start_lambda_scope (decl);
32160 /* Parse the default argument. */
32161 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
32162 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
32163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32165 finish_lambda_scope ();
32167 if (parsed_arg == error_mark_node)
32168 cp_parser_skip_to_end_of_statement (parser);
32170 if (!processing_template_decl)
32172 /* In a non-template class, check conversions now. In a template,
32173 we'll wait and instantiate these as needed. */
32174 if (TREE_CODE (decl) == PARM_DECL)
32175 parsed_arg = check_default_argument (parmtype, parsed_arg,
32176 tf_warning_or_error);
32177 else if (maybe_reject_flexarray_init (decl, parsed_arg))
32178 parsed_arg = error_mark_node;
32179 else
32180 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
32183 /* If the token stream has not been completely used up, then
32184 there was extra junk after the end of the default
32185 argument. */
32186 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
32188 if (TREE_CODE (decl) == PARM_DECL)
32189 cp_parser_error (parser, "expected %<,%>");
32190 else
32191 cp_parser_error (parser, "expected %<;%>");
32194 /* Revert to the main lexer. */
32195 cp_parser_pop_lexer (parser);
32197 return parsed_arg;
32200 /* FIELD is a non-static data member with an initializer which we saved for
32201 later; parse it now. */
32203 static void
32204 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
32206 tree def;
32208 maybe_begin_member_template_processing (field);
32210 push_unparsed_function_queues (parser);
32211 def = cp_parser_late_parse_one_default_arg (parser, field,
32212 DECL_INITIAL (field),
32213 NULL_TREE);
32214 pop_unparsed_function_queues (parser);
32216 maybe_end_member_template_processing ();
32218 DECL_INITIAL (field) = def;
32221 /* FN is a FUNCTION_DECL which may contains a parameter with an
32222 unparsed DEFERRED_PARSE. Parse the default args now. This function
32223 assumes that the current scope is the scope in which the default
32224 argument should be processed. */
32226 static void
32227 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
32229 unsigned char saved_local_variables_forbidden_p;
32231 /* While we're parsing the default args, we might (due to the
32232 statement expression extension) encounter more classes. We want
32233 to handle them right away, but we don't want them getting mixed
32234 up with default args that are currently in the queue. */
32235 push_unparsed_function_queues (parser);
32237 /* Local variable names (and the `this' keyword) may not appear
32238 in a default argument. */
32239 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
32240 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
32242 push_defarg_context (fn);
32244 begin_scope (sk_function_parms, fn);
32246 /* Gather the PARM_DECLs into a vec so we can keep track of them when
32247 pushdecl clears DECL_CHAIN. */
32248 releasing_vec parms;
32249 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
32250 parmdecl = DECL_CHAIN (parmdecl))
32251 vec_safe_push (parms, parmdecl);
32253 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
32254 for (int i = 0;
32255 parm && parm != void_list_node;
32256 parm = TREE_CHAIN (parm),
32257 ++i)
32259 tree default_arg = TREE_PURPOSE (parm);
32260 tree parsed_arg;
32262 tree parmdecl = parms[i];
32263 pushdecl (parmdecl);
32265 if (!default_arg)
32266 continue;
32268 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
32269 /* This can happen for a friend declaration for a function
32270 already declared with default arguments. */
32271 continue;
32273 parsed_arg
32274 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
32275 default_arg,
32276 TREE_VALUE (parm));
32277 TREE_PURPOSE (parm) = parsed_arg;
32279 /* Update any instantiations we've already created. */
32280 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
32281 TREE_PURPOSE (copy) = parsed_arg;
32284 pop_bindings_and_leave_scope ();
32286 /* Restore DECL_CHAINs after clobbering by pushdecl. */
32287 parm = NULL_TREE;
32288 for (int i = parms->length () - 1; i >= 0; --i)
32290 DECL_CHAIN (parms[i]) = parm;
32291 parm = parms[i];
32294 pop_defarg_context ();
32296 /* Make sure no default arg is missing. */
32297 check_default_args (fn);
32299 /* Restore the state of local_variables_forbidden_p. */
32300 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
32302 /* Restore the queue. */
32303 pop_unparsed_function_queues (parser);
32306 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
32308 sizeof ... ( identifier )
32310 where the 'sizeof' token has already been consumed. */
32312 static tree
32313 cp_parser_sizeof_pack (cp_parser *parser)
32315 /* Consume the `...'. */
32316 cp_lexer_consume_token (parser->lexer);
32317 maybe_warn_variadic_templates ();
32319 matching_parens parens;
32320 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
32321 if (paren)
32322 parens.consume_open (parser);
32323 else
32324 permerror (cp_lexer_peek_token (parser->lexer)->location,
32325 "%<sizeof...%> argument must be surrounded by parentheses");
32327 cp_token *token = cp_lexer_peek_token (parser->lexer);
32328 tree name = cp_parser_identifier (parser);
32329 if (name == error_mark_node)
32330 return error_mark_node;
32331 /* The name is not qualified. */
32332 parser->scope = NULL_TREE;
32333 parser->qualifying_scope = NULL_TREE;
32334 parser->object_scope = NULL_TREE;
32335 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
32336 if (expr == error_mark_node)
32337 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
32338 token->location);
32339 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
32340 expr = TREE_TYPE (expr);
32341 else if (TREE_CODE (expr) == CONST_DECL)
32342 expr = DECL_INITIAL (expr);
32343 expr = make_pack_expansion (expr);
32344 PACK_EXPANSION_SIZEOF_P (expr) = true;
32346 if (paren)
32347 parens.require_close (parser);
32349 return expr;
32352 /* Parse the operand of `sizeof' (or a similar operator). Returns
32353 either a TYPE or an expression, depending on the form of the
32354 input. The KEYWORD indicates which kind of expression we have
32355 encountered. */
32357 static tree
32358 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
32360 tree expr = NULL_TREE;
32361 const char *saved_message;
32362 const char *saved_message_arg;
32363 bool saved_integral_constant_expression_p;
32364 bool saved_non_integral_constant_expression_p;
32366 /* If it's a `...', then we are computing the length of a parameter
32367 pack. */
32368 if (keyword == RID_SIZEOF
32369 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32370 return cp_parser_sizeof_pack (parser);
32372 /* Types cannot be defined in a `sizeof' expression. Save away the
32373 old message. */
32374 saved_message = parser->type_definition_forbidden_message;
32375 saved_message_arg = parser->type_definition_forbidden_message_arg;
32376 parser->type_definition_forbidden_message
32377 = G_("types may not be defined in %qs expressions");
32378 parser->type_definition_forbidden_message_arg
32379 = IDENTIFIER_POINTER (ridpointers[keyword]);
32381 /* The restrictions on constant-expressions do not apply inside
32382 sizeof expressions. */
32383 saved_integral_constant_expression_p
32384 = parser->integral_constant_expression_p;
32385 saved_non_integral_constant_expression_p
32386 = parser->non_integral_constant_expression_p;
32387 parser->integral_constant_expression_p = false;
32389 auto cleanup = make_temp_override
32390 (parser->auto_is_implicit_function_template_parm_p, false);
32392 /* Do not actually evaluate the expression. */
32393 ++cp_unevaluated_operand;
32394 ++c_inhibit_evaluation_warnings;
32395 /* If it's a `(', then we might be looking at the type-id
32396 construction. */
32397 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32399 tree type = NULL_TREE;
32401 tentative_firewall firewall (parser);
32403 /* We can't be sure yet whether we're looking at a type-id or an
32404 expression. */
32405 cp_parser_parse_tentatively (parser);
32407 matching_parens parens;
32408 parens.consume_open (parser);
32410 /* Note: as a GNU Extension, compound literals are considered
32411 postfix-expressions as they are in C99, so they are valid
32412 arguments to sizeof. See comment in cp_parser_cast_expression
32413 for details. */
32414 if (cp_parser_compound_literal_p (parser))
32415 cp_parser_simulate_error (parser);
32416 else
32418 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
32419 parser->in_type_id_in_expr_p = true;
32420 /* Look for the type-id. */
32421 type = cp_parser_type_id (parser);
32422 /* Look for the closing `)'. */
32423 parens.require_close (parser);
32424 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
32427 /* If all went well, then we're done. */
32428 if (cp_parser_parse_definitely (parser))
32429 expr = type;
32430 else
32432 /* Commit to the tentative_firewall so we get syntax errors. */
32433 cp_parser_commit_to_tentative_parse (parser);
32435 expr = cp_parser_unary_expression (parser);
32438 else
32439 expr = cp_parser_unary_expression (parser);
32441 /* Go back to evaluating expressions. */
32442 --cp_unevaluated_operand;
32443 --c_inhibit_evaluation_warnings;
32445 /* And restore the old one. */
32446 parser->type_definition_forbidden_message = saved_message;
32447 parser->type_definition_forbidden_message_arg = saved_message_arg;
32448 parser->integral_constant_expression_p
32449 = saved_integral_constant_expression_p;
32450 parser->non_integral_constant_expression_p
32451 = saved_non_integral_constant_expression_p;
32453 return expr;
32456 /* If the current declaration has no declarator, return true. */
32458 static bool
32459 cp_parser_declares_only_class_p (cp_parser *parser)
32461 /* If the next token is a `;' or a `,' then there is no
32462 declarator. */
32463 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32464 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
32467 /* Update the DECL_SPECS to reflect the storage class indicated by
32468 KEYWORD. */
32470 static void
32471 cp_parser_set_storage_class (cp_parser *parser,
32472 cp_decl_specifier_seq *decl_specs,
32473 enum rid keyword,
32474 cp_token *token)
32476 cp_storage_class storage_class;
32478 if (parser->in_unbraced_linkage_specification_p)
32480 error_at (token->location, "invalid use of %qD in linkage specification",
32481 ridpointers[keyword]);
32482 return;
32484 else if (decl_specs->storage_class != sc_none)
32486 decl_specs->conflicting_specifiers_p = true;
32487 return;
32490 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
32491 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
32492 && decl_specs->gnu_thread_keyword_p)
32494 pedwarn (decl_specs->locations[ds_thread], 0,
32495 "%<__thread%> before %qD", ridpointers[keyword]);
32498 switch (keyword)
32500 case RID_AUTO:
32501 storage_class = sc_auto;
32502 break;
32503 case RID_REGISTER:
32504 storage_class = sc_register;
32505 break;
32506 case RID_STATIC:
32507 storage_class = sc_static;
32508 break;
32509 case RID_EXTERN:
32510 storage_class = sc_extern;
32511 break;
32512 case RID_MUTABLE:
32513 storage_class = sc_mutable;
32514 break;
32515 default:
32516 gcc_unreachable ();
32518 decl_specs->storage_class = storage_class;
32519 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
32521 /* A storage class specifier cannot be applied alongside a typedef
32522 specifier. If there is a typedef specifier present then set
32523 conflicting_specifiers_p which will trigger an error later
32524 on in grokdeclarator. */
32525 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
32526 decl_specs->conflicting_specifiers_p = true;
32529 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
32530 is true, the type is a class or enum definition. */
32532 static void
32533 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
32534 tree type_spec,
32535 cp_token *token,
32536 bool type_definition_p)
32538 decl_specs->any_specifiers_p = true;
32540 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
32541 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
32542 this is what happened. In system headers, we ignore these
32543 declarations so that G++ can work with system headers that are not
32544 C++-safe. */
32545 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
32546 && !type_definition_p
32547 && (type_spec == boolean_type_node
32548 || type_spec == char8_type_node
32549 || type_spec == char16_type_node
32550 || type_spec == char32_type_node
32551 || type_spec == wchar_type_node)
32552 && (decl_specs->type
32553 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
32554 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
32555 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
32556 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
32558 decl_specs->redefined_builtin_type = type_spec;
32559 set_and_check_decl_spec_loc (decl_specs,
32560 ds_redefined_builtin_type_spec,
32561 token);
32562 if (!decl_specs->type)
32564 decl_specs->type = type_spec;
32565 decl_specs->type_definition_p = false;
32566 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
32569 else if (decl_specs->type)
32570 decl_specs->multiple_types_p = true;
32571 else
32573 decl_specs->type = type_spec;
32574 decl_specs->type_definition_p = type_definition_p;
32575 decl_specs->redefined_builtin_type = NULL_TREE;
32576 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
32580 /* True iff TOKEN is the GNU keyword __thread. */
32582 static bool
32583 token_is__thread (cp_token *token)
32585 gcc_assert (token->keyword == RID_THREAD);
32586 return id_equal (token->u.value, "__thread");
32589 /* Set the location for a declarator specifier and check if it is
32590 duplicated.
32592 DECL_SPECS is the sequence of declarator specifiers onto which to
32593 set the location.
32595 DS is the single declarator specifier to set which location is to
32596 be set onto the existing sequence of declarators.
32598 LOCATION is the location for the declarator specifier to
32599 consider. */
32601 static void
32602 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
32603 cp_decl_spec ds, cp_token *token)
32605 gcc_assert (ds < ds_last);
32607 if (decl_specs == NULL)
32608 return;
32610 location_t location = token->location;
32612 if (decl_specs->locations[ds] == 0)
32614 decl_specs->locations[ds] = location;
32615 if (ds == ds_thread)
32616 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
32618 else
32620 if (ds == ds_long)
32622 if (decl_specs->locations[ds_long_long] != 0)
32623 error_at (location,
32624 "%<long long long%> is too long for GCC");
32625 else
32627 decl_specs->locations[ds_long_long] = location;
32628 pedwarn_cxx98 (location,
32629 OPT_Wlong_long,
32630 "ISO C++ 1998 does not support %<long long%>");
32633 else if (ds == ds_thread)
32635 bool gnu = token_is__thread (token);
32636 gcc_rich_location richloc (location);
32637 if (gnu != decl_specs->gnu_thread_keyword_p)
32639 richloc.add_range (decl_specs->locations[ds_thread]);
32640 error_at (&richloc,
32641 "both %<__thread%> and %<thread_local%> specified");
32643 else
32645 richloc.add_fixit_remove ();
32646 error_at (&richloc, "duplicate %qD", token->u.value);
32649 else
32651 static const char *const decl_spec_names[] = {
32652 "signed",
32653 "unsigned",
32654 "short",
32655 "long",
32656 "const",
32657 "volatile",
32658 "restrict",
32659 "inline",
32660 "virtual",
32661 "explicit",
32662 "friend",
32663 "typedef",
32664 "using",
32665 "constexpr",
32666 "__complex",
32667 "constinit",
32668 "consteval"
32670 gcc_rich_location richloc (location);
32671 richloc.add_fixit_remove ();
32672 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
32677 /* Return true iff the declarator specifier DS is present in the
32678 sequence of declarator specifiers DECL_SPECS. */
32680 bool
32681 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
32682 cp_decl_spec ds)
32684 gcc_assert (ds < ds_last);
32686 if (decl_specs == NULL)
32687 return false;
32689 return decl_specs->locations[ds] != 0;
32692 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
32693 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
32695 static bool
32696 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
32698 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
32701 /* Issue an error message indicating that TOKEN_DESC was expected.
32702 If KEYWORD is true, it indicated this function is called by
32703 cp_parser_require_keword and the required token can only be
32704 a indicated keyword.
32706 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32707 within any error as the location of an "opening" token matching
32708 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32709 RT_CLOSE_PAREN). */
32711 static void
32712 cp_parser_required_error (cp_parser *parser,
32713 required_token token_desc,
32714 bool keyword,
32715 location_t matching_location)
32717 if (cp_parser_simulate_error (parser))
32718 return;
32720 const char *gmsgid = NULL;
32721 switch (token_desc)
32723 case RT_NEW:
32724 gmsgid = G_("expected %<new%>");
32725 break;
32726 case RT_DELETE:
32727 gmsgid = G_("expected %<delete%>");
32728 break;
32729 case RT_RETURN:
32730 gmsgid = G_("expected %<return%>");
32731 break;
32732 case RT_WHILE:
32733 gmsgid = G_("expected %<while%>");
32734 break;
32735 case RT_EXTERN:
32736 gmsgid = G_("expected %<extern%>");
32737 break;
32738 case RT_STATIC_ASSERT:
32739 gmsgid = G_("expected %<static_assert%>");
32740 break;
32741 case RT_DECLTYPE:
32742 gmsgid = G_("expected %<decltype%>");
32743 break;
32744 case RT_OPERATOR:
32745 gmsgid = G_("expected %<operator%>");
32746 break;
32747 case RT_CLASS:
32748 gmsgid = G_("expected %<class%>");
32749 break;
32750 case RT_TEMPLATE:
32751 gmsgid = G_("expected %<template%>");
32752 break;
32753 case RT_NAMESPACE:
32754 gmsgid = G_("expected %<namespace%>");
32755 break;
32756 case RT_USING:
32757 gmsgid = G_("expected %<using%>");
32758 break;
32759 case RT_ASM:
32760 gmsgid = G_("expected %<asm%>");
32761 break;
32762 case RT_TRY:
32763 gmsgid = G_("expected %<try%>");
32764 break;
32765 case RT_CATCH:
32766 gmsgid = G_("expected %<catch%>");
32767 break;
32768 case RT_THROW:
32769 gmsgid = G_("expected %<throw%>");
32770 break;
32771 case RT_AUTO:
32772 gmsgid = G_("expected %<auto%>");
32773 break;
32774 case RT_LABEL:
32775 gmsgid = G_("expected %<__label__%>");
32776 break;
32777 case RT_AT_TRY:
32778 gmsgid = G_("expected %<@try%>");
32779 break;
32780 case RT_AT_SYNCHRONIZED:
32781 gmsgid = G_("expected %<@synchronized%>");
32782 break;
32783 case RT_AT_THROW:
32784 gmsgid = G_("expected %<@throw%>");
32785 break;
32786 case RT_TRANSACTION_ATOMIC:
32787 gmsgid = G_("expected %<__transaction_atomic%>");
32788 break;
32789 case RT_TRANSACTION_RELAXED:
32790 gmsgid = G_("expected %<__transaction_relaxed%>");
32791 break;
32792 case RT_CO_YIELD:
32793 gmsgid = G_("expected %<co_yield%>");
32794 break;
32795 default:
32796 break;
32799 if (!gmsgid && !keyword)
32801 switch (token_desc)
32803 case RT_SEMICOLON:
32804 gmsgid = G_("expected %<;%>");
32805 break;
32806 case RT_OPEN_PAREN:
32807 gmsgid = G_("expected %<(%>");
32808 break;
32809 case RT_CLOSE_BRACE:
32810 gmsgid = G_("expected %<}%>");
32811 break;
32812 case RT_OPEN_BRACE:
32813 gmsgid = G_("expected %<{%>");
32814 break;
32815 case RT_CLOSE_SQUARE:
32816 gmsgid = G_("expected %<]%>");
32817 break;
32818 case RT_OPEN_SQUARE:
32819 gmsgid = G_("expected %<[%>");
32820 break;
32821 case RT_COMMA:
32822 gmsgid = G_("expected %<,%>");
32823 break;
32824 case RT_SCOPE:
32825 gmsgid = G_("expected %<::%>");
32826 break;
32827 case RT_LESS:
32828 gmsgid = G_("expected %<<%>");
32829 break;
32830 case RT_GREATER:
32831 gmsgid = G_("expected %<>%>");
32832 break;
32833 case RT_EQ:
32834 gmsgid = G_("expected %<=%>");
32835 break;
32836 case RT_ELLIPSIS:
32837 gmsgid = G_("expected %<...%>");
32838 break;
32839 case RT_MULT:
32840 gmsgid = G_("expected %<*%>");
32841 break;
32842 case RT_COMPL:
32843 gmsgid = G_("expected %<~%>");
32844 break;
32845 case RT_COLON:
32846 gmsgid = G_("expected %<:%>");
32847 break;
32848 case RT_COLON_SCOPE:
32849 gmsgid = G_("expected %<:%> or %<::%>");
32850 break;
32851 case RT_CLOSE_PAREN:
32852 gmsgid = G_("expected %<)%>");
32853 break;
32854 case RT_COMMA_CLOSE_PAREN:
32855 gmsgid = G_("expected %<,%> or %<)%>");
32856 break;
32857 case RT_PRAGMA_EOL:
32858 gmsgid = G_("expected end of line");
32859 break;
32860 case RT_NAME:
32861 gmsgid = G_("expected identifier");
32862 break;
32863 case RT_SELECT:
32864 gmsgid = G_("expected selection-statement");
32865 break;
32866 case RT_ITERATION:
32867 gmsgid = G_("expected iteration-statement");
32868 break;
32869 case RT_JUMP:
32870 gmsgid = G_("expected jump-statement");
32871 break;
32872 case RT_CLASS_KEY:
32873 gmsgid = G_("expected class-key");
32874 break;
32875 case RT_CLASS_TYPENAME_TEMPLATE:
32876 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
32877 break;
32878 default:
32879 gcc_unreachable ();
32883 if (gmsgid)
32884 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
32888 /* If the next token is of the indicated TYPE, consume it. Otherwise,
32889 issue an error message indicating that TOKEN_DESC was expected.
32891 Returns the token consumed, if the token had the appropriate type.
32892 Otherwise, returns NULL.
32894 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32895 within any error as the location of an "opening" token matching
32896 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32897 RT_CLOSE_PAREN). */
32899 static cp_token *
32900 cp_parser_require (cp_parser* parser,
32901 enum cpp_ttype type,
32902 required_token token_desc,
32903 location_t matching_location)
32905 if (cp_lexer_next_token_is (parser->lexer, type))
32906 return cp_lexer_consume_token (parser->lexer);
32907 else
32909 /* Output the MESSAGE -- unless we're parsing tentatively. */
32910 if (!cp_parser_simulate_error (parser))
32911 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
32912 matching_location);
32913 return NULL;
32917 /* An error message is produced if the next token is not '>'.
32918 All further tokens are skipped until the desired token is
32919 found or '{', '}', ';' or an unbalanced ')' or ']'. */
32921 static void
32922 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
32924 /* Current level of '< ... >'. */
32925 unsigned level = 0;
32926 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
32927 unsigned nesting_depth = 0;
32929 /* Are we ready, yet? If not, issue error message. */
32930 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
32931 return;
32933 /* Skip tokens until the desired token is found. */
32934 while (true)
32936 /* Peek at the next token. */
32937 switch (cp_lexer_peek_token (parser->lexer)->type)
32939 case CPP_LESS:
32940 if (!nesting_depth)
32941 ++level;
32942 break;
32944 case CPP_RSHIFT:
32945 if (cxx_dialect == cxx98)
32946 /* C++0x views the `>>' operator as two `>' tokens, but
32947 C++98 does not. */
32948 break;
32949 else if (!nesting_depth && level-- == 0)
32951 /* We've hit a `>>' where the first `>' closes the
32952 template argument list, and the second `>' is
32953 spurious. Just consume the `>>' and stop; we've
32954 already produced at least one error. */
32955 cp_lexer_consume_token (parser->lexer);
32956 return;
32958 /* Fall through for C++0x, so we handle the second `>' in
32959 the `>>'. */
32960 gcc_fallthrough ();
32962 case CPP_GREATER:
32963 if (!nesting_depth && level-- == 0)
32965 /* We've reached the token we want, consume it and stop. */
32966 cp_lexer_consume_token (parser->lexer);
32967 return;
32969 break;
32971 case CPP_OPEN_PAREN:
32972 case CPP_OPEN_SQUARE:
32973 ++nesting_depth;
32974 break;
32976 case CPP_CLOSE_PAREN:
32977 case CPP_CLOSE_SQUARE:
32978 if (nesting_depth-- == 0)
32979 return;
32980 break;
32982 case CPP_EOF:
32983 case CPP_PRAGMA_EOL:
32984 case CPP_SEMICOLON:
32985 case CPP_OPEN_BRACE:
32986 case CPP_CLOSE_BRACE:
32987 /* The '>' was probably forgotten, don't look further. */
32988 return;
32990 default:
32991 break;
32994 /* Consume this token. */
32995 cp_lexer_consume_token (parser->lexer);
32999 /* If the next token is the indicated keyword, consume it. Otherwise,
33000 issue an error message indicating that TOKEN_DESC was expected.
33002 Returns the token consumed, if the token had the appropriate type.
33003 Otherwise, returns NULL. */
33005 static cp_token *
33006 cp_parser_require_keyword (cp_parser* parser,
33007 enum rid keyword,
33008 required_token token_desc)
33010 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
33012 if (token && token->keyword != keyword)
33014 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
33015 UNKNOWN_LOCATION);
33016 return NULL;
33019 return token;
33022 /* Returns TRUE iff TOKEN is a token that can begin the body of a
33023 function-definition. */
33025 static bool
33026 cp_parser_token_starts_function_definition_p (cp_token* token)
33028 return (/* An ordinary function-body begins with an `{'. */
33029 token->type == CPP_OPEN_BRACE
33030 /* A ctor-initializer begins with a `:'. */
33031 || token->type == CPP_COLON
33032 /* A function-try-block begins with `try'. */
33033 || token->keyword == RID_TRY
33034 /* A function-transaction-block begins with `__transaction_atomic'
33035 or `__transaction_relaxed'. */
33036 || token->keyword == RID_TRANSACTION_ATOMIC
33037 || token->keyword == RID_TRANSACTION_RELAXED
33038 /* The named return value extension begins with `return'. */
33039 || token->keyword == RID_RETURN);
33042 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
33043 definition. */
33045 static bool
33046 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
33048 cp_token *token;
33050 token = cp_lexer_peek_token (parser->lexer);
33051 return (token->type == CPP_OPEN_BRACE
33052 || (token->type == CPP_COLON
33053 && !parser->colon_doesnt_start_class_def_p));
33056 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
33057 C++0x) ending a template-argument. */
33059 static bool
33060 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
33062 cp_token *token;
33064 token = cp_lexer_peek_token (parser->lexer);
33065 return (token->type == CPP_COMMA
33066 || token->type == CPP_GREATER
33067 || token->type == CPP_ELLIPSIS
33068 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
33071 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
33072 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
33074 static bool
33075 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
33076 size_t n)
33078 cp_token *token;
33080 token = cp_lexer_peek_nth_token (parser->lexer, n);
33081 if (token->type == CPP_LESS)
33082 return true;
33083 /* Check for the sequence `<::' in the original code. It would be lexed as
33084 `[:', where `[' is a digraph, and there is no whitespace before
33085 `:'. */
33086 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
33088 cp_token *token2;
33089 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
33090 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
33091 return true;
33093 return false;
33096 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
33097 or none_type otherwise. */
33099 static enum tag_types
33100 cp_parser_token_is_class_key (cp_token* token)
33102 switch (token->keyword)
33104 case RID_CLASS:
33105 return class_type;
33106 case RID_STRUCT:
33107 return record_type;
33108 case RID_UNION:
33109 return union_type;
33111 default:
33112 return none_type;
33116 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
33117 or none_type otherwise or if the token is null. */
33119 static enum tag_types
33120 cp_parser_token_is_type_parameter_key (cp_token* token)
33122 if (!token)
33123 return none_type;
33125 switch (token->keyword)
33127 case RID_CLASS:
33128 return class_type;
33129 case RID_TYPENAME:
33130 return typename_type;
33132 default:
33133 return none_type;
33137 /* Diagnose redundant enum-keys. */
33139 static void
33140 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
33141 tree type, rid scoped_key)
33143 if (!warn_redundant_tags)
33144 return;
33146 tree type_decl = TYPE_MAIN_DECL (type);
33147 tree name = DECL_NAME (type_decl);
33148 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
33149 push_deferring_access_checks (dk_no_check);
33150 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33151 pop_deferring_access_checks ();
33153 /* The enum-key is redundant for uses of the TYPE that are not
33154 declarations and for which name lookup returns just the type
33155 itself. */
33156 if (decl != type_decl)
33157 return;
33159 if (scoped_key != RID_CLASS
33160 && scoped_key != RID_STRUCT
33161 && current_lang_name != lang_name_cplusplus
33162 && current_namespace == global_namespace)
33164 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
33165 enum tag in shared C/C++ code in files (such as headers) included
33166 in the main source file. */
33167 const line_map_ordinary *map = NULL;
33168 linemap_resolve_location (line_table, key_loc,
33169 LRK_MACRO_DEFINITION_LOCATION,
33170 &map);
33171 if (!MAIN_FILE_P (map))
33172 return;
33175 gcc_rich_location richloc (key_loc);
33176 richloc.add_fixit_remove (key_loc);
33177 warning_at (&richloc, OPT_Wredundant_tags,
33178 "redundant enum-key %<enum%s%> in reference to %q#T",
33179 (scoped_key == RID_CLASS ? " class"
33180 : scoped_key == RID_STRUCT ? " struct" : ""), type);
33183 /* Describes the set of declarations of a struct, class, or class template
33184 or its specializations. Used for -Wmismatched-tags. */
33186 class class_decl_loc_t
33188 public:
33190 class_decl_loc_t ()
33191 : locvec (), idxdef (), def_class_key ()
33193 locvec.create (4);
33196 /* Constructs an object for a single declaration of a class with
33197 CLASS_KEY at the current location in the current function (or
33198 at another scope). KEY_REDUNDANT is true if the class-key may
33199 be omitted in the current context without an ambiguity with
33200 another symbol with the same name.
33201 DEF_P is true for a class declaration that is a definition.
33202 CURLOC is the associated location. */
33203 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
33204 location_t curloc = input_location)
33205 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
33207 locvec.create (4);
33208 class_key_loc_t ckl (current_function_decl, curloc, class_key,
33209 key_redundant);
33210 locvec.quick_push (ckl);
33213 /* Copy, assign, and destroy the object. Necessary because LOCVEC
33214 isn't safely copyable and assignable and doesn't release storage
33215 on its own. */
33216 class_decl_loc_t (const class_decl_loc_t &rhs)
33217 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
33218 def_class_key (rhs.def_class_key)
33221 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
33223 if (this == &rhs)
33224 return *this;
33225 locvec.release ();
33226 locvec = rhs.locvec.copy ();
33227 idxdef = rhs.idxdef;
33228 def_class_key = rhs.def_class_key;
33229 return *this;
33232 ~class_decl_loc_t ()
33234 locvec.release ();
33237 /* Issues -Wmismatched-tags for a single class. */
33238 void diag_mismatched_tags (tree);
33240 /* Issues -Wmismatched-tags for all classes. */
33241 static void diag_mismatched_tags ();
33243 /* Adds TYPE_DECL to the collection of class decls and diagnoses
33244 redundant tags (if -Wredundant-tags is enabled). */
33245 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
33247 /* Either adds this decl to the collection of class decls
33248 or diagnoses it, whichever is appropriate. */
33249 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
33251 private:
33253 tree function (unsigned i) const
33255 return locvec[i].func;
33258 location_t location (unsigned i) const
33260 return locvec[i].loc;
33263 bool key_redundant (unsigned i) const
33265 return locvec[i].key_redundant;
33268 tag_types class_key (unsigned i) const
33270 return locvec[i].class_key;
33273 /* True if a definition for the class has been seen. */
33274 bool def_p () const
33276 return idxdef < locvec.length ();
33279 /* The location of a single mention of a class type with the given
33280 class-key. */
33281 struct class_key_loc_t
33283 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
33284 : func (func), loc (loc), class_key (key), key_redundant (redundant)
33287 /* The function the type is mentioned in. */
33288 tree func;
33289 /* The exact location. */
33290 location_t loc;
33291 /* The class-key used in the mention of the type. */
33292 tag_types class_key;
33293 /* True when the class-key could be omitted at this location
33294 without an ambiguity with another symbol of the same name. */
33295 bool key_redundant;
33297 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
33298 vec <class_key_loc_t> locvec;
33299 /* LOCVEC index of the definition or UINT_MAX if none exists. */
33300 unsigned idxdef;
33301 /* The class-key the class was last declared with or none_type when
33302 it has been declared with a mismatched key. */
33303 tag_types def_class_key;
33305 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
33306 description above. */
33307 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
33308 static class_to_loc_map_t class2loc;
33311 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
33313 /* Issue an error message if the CLASS_KEY does not match the TYPE.
33314 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33315 is set for a declaration of class TYPE and clear for a reference to
33316 it that is not a declaration of it. */
33318 static void
33319 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
33320 tag_types class_key, tree type, bool def_p,
33321 bool decl_p)
33323 if (type == error_mark_node)
33324 return;
33326 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
33327 if (seen_as_union != (class_key == union_type))
33329 if (permerror (input_location, "%qs tag used in naming %q#T",
33330 class_key == union_type ? "union"
33331 : class_key == record_type ? "struct" : "class",
33332 type))
33333 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
33334 "%q#T was previously declared here", type);
33335 return;
33338 if (!warn_mismatched_tags && !warn_redundant_tags)
33339 return;
33341 /* Only consider the true class-keys below and ignore typename_type,
33342 etc. that are not C++ class-keys. */
33343 if (class_key != class_type
33344 && class_key != record_type
33345 && class_key != union_type)
33346 return;
33348 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
33351 /* Returns the template or specialization of one to which the RECORD_TYPE
33352 TYPE corresponds. */
33354 static tree
33355 specialization_of (tree type)
33357 tree ret = type;
33359 /* Determine the template or its partial specialization to which TYPE
33360 corresponds. */
33361 if (tree spec = most_specialized_partial_spec (type, tf_none))
33362 if (spec != error_mark_node)
33363 ret = TREE_TYPE (TREE_VALUE (spec));
33365 if (ret == type)
33366 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
33368 return TYPE_MAIN_DECL (ret);
33372 /* Adds the class TYPE to the collection of class decls and diagnoses
33373 redundant tags (if -Wredundant-tags is enabled).
33374 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33375 is set for a (likely, based on syntactic context) declaration of class
33376 TYPE and clear for a reference to it that is not a declaration of it. */
33378 void
33379 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
33380 tag_types class_key, tree type, bool def_p, bool decl_p)
33382 tree type_decl = TYPE_MAIN_DECL (type);
33383 tree name = DECL_NAME (type_decl);
33384 /* Look up the NAME to see if it unambiguously refers to the TYPE
33385 and set KEY_REDUNDANT if so. */
33386 push_deferring_access_checks (dk_no_check);
33387 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33388 pop_deferring_access_checks ();
33390 /* The class-key is redundant for uses of the CLASS_TYPE that are
33391 neither definitions of it nor declarations, and for which name
33392 lookup returns just the type itself. */
33393 bool key_redundant = (!def_p && !decl_p
33394 && (decl == type_decl
33395 || TREE_CODE (decl) == TEMPLATE_DECL
33396 || TYPE_BEING_DEFINED (type)));
33398 if (key_redundant
33399 && class_key != class_type
33400 && current_lang_name != lang_name_cplusplus
33401 && current_namespace == global_namespace)
33403 /* Avoid issuing the diagnostic for apparently redundant struct
33404 and union class-keys in shared C/C++ code in files (such as
33405 headers) included in the main source file. */
33406 const line_map_ordinary *map = NULL;
33407 linemap_resolve_location (line_table, key_loc,
33408 LRK_MACRO_DEFINITION_LOCATION,
33409 &map);
33410 if (!MAIN_FILE_P (map))
33411 key_redundant = false;
33414 /* Set if a declaration of TYPE has previously been seen or if it must
33415 exist in a precompiled header. */
33416 bool exist;
33417 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
33418 if (!exist)
33420 tree type = TREE_TYPE (type_decl);
33421 if (def_p || !COMPLETE_TYPE_P (type))
33423 /* TYPE_DECL is the first declaration or definition of the type
33424 (outside precompiled headers -- see below). Just create
33425 a new entry for it and return unless it's a declaration
33426 involving a template that may need to be diagnosed by
33427 -Wredundant-tags. */
33428 *rdl = class_decl_loc_t (class_key, false, def_p);
33429 if (TREE_CODE (decl) != TEMPLATE_DECL)
33430 return;
33432 else
33434 /* TYPE was previously defined in some unknown precompiled hdeader.
33435 Simply add a record of its definition at an unknown location and
33436 proceed below to add a reference to it at the current location.
33437 (Declarations in precompiled headers that are not definitions
33438 are ignored.) */
33439 tag_types def_key
33440 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
33441 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
33442 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
33443 exist = true;
33447 /* A prior declaration of TYPE_DECL has been seen. */
33449 if (key_redundant)
33451 gcc_rich_location richloc (key_loc);
33452 richloc.add_fixit_remove (key_loc);
33453 warning_at (&richloc, OPT_Wredundant_tags,
33454 "redundant class-key %qs in reference to %q#T",
33455 class_key == union_type ? "union"
33456 : class_key == record_type ? "struct" : "class",
33457 type);
33460 if (!exist)
33461 /* Do nothing if this is the first declaration of the type. */
33462 return;
33464 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
33465 /* Do nothing if the class-key in this declaration matches
33466 the definition. */
33467 return;
33469 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
33470 def_p);
33473 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
33474 of class decls or diagnoses it, whichever is appropriate. */
33476 void
33477 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
33478 tag_types class_key,
33479 bool redundant,
33480 bool def_p)
33482 /* Reset the CLASS_KEY associated with this type on mismatch.
33483 This is an optimization that lets the diagnostic code skip
33484 over classes that use the same class-key in all declarations. */
33485 if (def_class_key != class_key)
33486 def_class_key = none_type;
33488 /* Set IDXDEF to the index of the vector corresponding to
33489 the definition. */
33490 if (def_p)
33491 idxdef = locvec.length ();
33493 /* Append a record of this declaration to the vector. */
33494 class_key_loc_t ckl (current_function_decl, input_location, class_key,
33495 redundant);
33496 locvec.safe_push (ckl);
33498 if (idxdef == UINT_MAX)
33499 return;
33501 /* As a space optimization diagnose declarations of a class
33502 whose definition has been seen and purge the LOCVEC of
33503 all entries except the definition. */
33504 diag_mismatched_tags (type_decl);
33505 if (idxdef)
33507 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
33508 locvec.release ();
33509 locvec.reserve (2);
33510 locvec.safe_push (ent);
33511 idxdef = 0;
33513 else
33514 /* Pop the entry pushed above for this declaration. */
33515 locvec.pop ();
33518 /* Issues -Wmismatched-tags for a single class. */
33520 void
33521 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
33523 if (!warn_mismatched_tags)
33524 return;
33526 /* Number of uses of the class. */
33527 const unsigned ndecls = locvec.length ();
33529 /* The class (or template) declaration guiding the decisions about
33530 the diagnostic. For ordinary classes it's the same as THIS. For
33531 uses of instantiations of templates other than their declarations
33532 it points to the record for the declaration of the corresponding
33533 primary template or partial specialization. */
33534 class_decl_loc_t *cdlguide = this;
33536 tree type = TREE_TYPE (type_decl);
33537 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
33539 /* For implicit instantiations of a primary template look up
33540 the primary or partial specialization and use it as
33541 the expected class-key rather than using the class-key of
33542 the first reference to the instantiation. The primary must
33543 be (and inevitably is) at index zero. */
33544 tree spec = specialization_of (type);
33545 cdlguide = class2loc.get (spec);
33546 gcc_assert (cdlguide != NULL);
33548 else
33550 /* Skip declarations that consistently use the same class-key. */
33551 if (def_class_key != none_type)
33552 return;
33555 /* Set if a definition for the class has been seen. */
33556 const bool def_p = cdlguide->def_p ();
33558 /* The index of the declaration whose class-key this declaration
33559 is expected to match. It's either the class-key of the class
33560 definition if one exists or the first declaration otherwise. */
33561 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
33563 /* The class-key the class is expected to be declared with: it's
33564 either the key used in its definition or the first declaration
33565 if no definition has been provided.
33566 For implicit instantiations of a primary template it's
33567 the class-key used to declare the primary with. The primary
33568 must be at index zero. */
33569 const tag_types xpect_key = cdlguide->class_key (idxguide);
33571 unsigned idx = 0;
33572 /* Advance IDX to the first declaration that either is not
33573 a definition or that doesn't match the first declaration
33574 if no definition is provided. */
33575 while (class_key (idx) == xpect_key)
33576 if (++idx == ndecls)
33577 return;
33579 /* Save the current function before changing it below. */
33580 tree save_func = current_function_decl;
33581 /* Set the function declaration to print in diagnostic context. */
33582 current_function_decl = function (idx);
33584 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
33585 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
33587 location_t loc = location (idx);
33588 bool key_redundant_p = key_redundant (idx);
33589 auto_diagnostic_group d;
33590 /* Issue a warning for the first mismatched declaration.
33591 Avoid using "%#qT" since the class-key for the same type will
33592 be the same regardless of which one was used in the declaraion. */
33593 if (warning_at (loc, OPT_Wmismatched_tags,
33594 "%qT declared with a mismatched class-key %qs",
33595 type_decl, xmatchkstr))
33597 /* Suggest how to avoid the warning for each instance since
33598 the guidance may be different depending on context. */
33599 inform (loc,
33600 (key_redundant_p
33601 ? G_("remove the class-key or replace it with %qs")
33602 : G_("replace the class-key with %qs")),
33603 xpectkstr);
33605 /* Also point to the first declaration or definition that guided
33606 the decision to issue the warning above. */
33607 inform (cdlguide->location (idxguide),
33608 (def_p
33609 ? G_("%qT defined as %qs here")
33610 : G_("%qT first declared as %qs here")),
33611 type_decl, xpectkstr);
33614 /* Issue warnings for the remaining inconsistent declarations. */
33615 for (unsigned i = idx + 1; i != ndecls; ++i)
33617 tag_types clskey = class_key (i);
33618 /* Skip over the declarations that match either the definition
33619 if one was provided or the first declaration. */
33620 if (clskey == xpect_key)
33621 continue;
33623 loc = location (i);
33624 key_redundant_p = key_redundant (i);
33625 /* Set the function declaration to print in diagnostic context. */
33626 current_function_decl = function (i);
33627 if (warning_at (loc, OPT_Wmismatched_tags,
33628 "%qT declared with a mismatched class-key %qs",
33629 type_decl, xmatchkstr))
33630 /* Suggest how to avoid the warning for each instance since
33631 the guidance may be different depending on context. */
33632 inform (loc,
33633 (key_redundant_p
33634 ? G_("remove the class-key or replace it with %qs")
33635 : G_("replace the class-key with %qs")),
33636 xpectkstr);
33639 /* Restore the current function in case it was replaced above. */
33640 current_function_decl = save_func;
33643 /* Issues -Wmismatched-tags for all classes. Called at the end
33644 of processing a translation unit, after declarations of all class
33645 types and their uses have been recorded. */
33647 void
33648 class_decl_loc_t::diag_mismatched_tags ()
33650 /* CLASS2LOC should be empty if both -Wmismatched-tags and
33651 -Wredundant-tags are disabled. */
33652 gcc_assert (warn_mismatched_tags
33653 || warn_redundant_tags
33654 || class2loc.is_empty ());
33656 /* Save the current function before changing on return. It should
33657 be null at this point. */
33658 temp_override<tree> cleanup (current_function_decl);
33660 if (warn_mismatched_tags)
33662 /* Iterate over the collected class/struct/template declarations. */
33663 typedef class_to_loc_map_t::iterator iter_t;
33664 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
33666 tree type_decl = (*it).first;
33667 class_decl_loc_t &recloc = (*it).second;
33668 recloc.diag_mismatched_tags (type_decl);
33672 class2loc.empty ();
33675 /* Issue an error message if DECL is redeclared with different
33676 access than its original declaration [class.access.spec/3].
33677 This applies to nested classes, nested class templates and
33678 enumerations [class.mem/1]. */
33680 static void
33681 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
33683 if (!decl
33684 || (!CLASS_TYPE_P (TREE_TYPE (decl))
33685 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
33686 return;
33688 if ((TREE_PRIVATE (decl)
33689 != (current_access_specifier == access_private_node))
33690 || (TREE_PROTECTED (decl)
33691 != (current_access_specifier == access_protected_node)))
33692 error_at (location, "%qD redeclared with different access", decl);
33695 /* Look for the `template' keyword, as a syntactic disambiguator.
33696 Return TRUE iff it is present, in which case it will be
33697 consumed. */
33699 static bool
33700 cp_parser_optional_template_keyword (cp_parser *parser)
33702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
33704 /* In C++98 the `template' keyword can only be used within templates;
33705 outside templates the parser can always figure out what is a
33706 template and what is not. In C++11, per the resolution of DR 468,
33707 `template' is allowed in cases where it is not strictly necessary. */
33708 if (!processing_template_decl
33709 && pedantic && cxx_dialect == cxx98)
33711 cp_token *token = cp_lexer_peek_token (parser->lexer);
33712 pedwarn (token->location, OPT_Wpedantic,
33713 "in C++98 %<template%> (as a disambiguator) is only "
33714 "allowed within templates");
33715 /* If this part of the token stream is rescanned, the same
33716 error message would be generated. So, we purge the token
33717 from the stream. */
33718 cp_lexer_purge_token (parser->lexer);
33719 return false;
33721 else
33723 /* Consume the `template' keyword. */
33724 cp_lexer_consume_token (parser->lexer);
33725 return true;
33728 return false;
33731 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
33732 set PARSER->SCOPE, and perform other related actions. */
33734 static void
33735 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
33737 struct tree_check *check_value;
33739 /* Get the stored value. */
33740 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
33741 /* Set the scope from the stored value. */
33742 parser->scope = saved_checks_value (check_value);
33743 parser->qualifying_scope = check_value->qualifying_scope;
33744 parser->object_scope = parser->context->object_type;
33745 parser->context->object_type = NULL_TREE;
33748 /* Consume tokens up through a non-nested END token. Returns TRUE if we
33749 encounter the end of a block before what we were looking for. */
33751 static bool
33752 cp_parser_cache_group (cp_parser *parser,
33753 enum cpp_ttype end,
33754 unsigned depth)
33756 while (true)
33758 cp_token *token = cp_lexer_peek_token (parser->lexer);
33760 /* Abort a parenthesized expression if we encounter a semicolon. */
33761 if ((end == CPP_CLOSE_PAREN || depth == 0)
33762 && token->type == CPP_SEMICOLON)
33763 return true;
33764 /* If we've reached the end of the file, stop. */
33765 if (token->type == CPP_EOF
33766 || (end != CPP_PRAGMA_EOL
33767 && token->type == CPP_PRAGMA_EOL))
33768 return true;
33769 if (token->type == CPP_CLOSE_BRACE && depth == 0)
33770 /* We've hit the end of an enclosing block, so there's been some
33771 kind of syntax error. */
33772 return true;
33774 /* Consume the token. */
33775 cp_lexer_consume_token (parser->lexer);
33776 /* See if it starts a new group. */
33777 if (token->type == CPP_OPEN_BRACE)
33779 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
33780 /* In theory this should probably check end == '}', but
33781 cp_parser_save_member_function_body needs it to exit
33782 after either '}' or ')' when called with ')'. */
33783 if (depth == 0)
33784 return false;
33786 else if (token->type == CPP_OPEN_PAREN)
33788 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
33789 if (depth == 0 && end == CPP_CLOSE_PAREN)
33790 return false;
33792 else if (token->type == CPP_PRAGMA)
33793 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
33794 else if (token->type == end)
33795 return false;
33799 /* Like above, for caching a default argument or NSDMI. Both of these are
33800 terminated by a non-nested comma, but it can be unclear whether or not a
33801 comma is nested in a template argument list unless we do more parsing.
33802 In order to handle this ambiguity, when we encounter a ',' after a '<'
33803 we try to parse what follows as a parameter-declaration-list (in the
33804 case of a default argument) or a member-declarator (in the case of an
33805 NSDMI). If that succeeds, then we stop caching. */
33807 static tree
33808 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
33810 unsigned depth = 0;
33811 int maybe_template_id = 0;
33812 cp_token *first_token;
33813 cp_token *token;
33814 tree default_argument;
33816 /* Add tokens until we have processed the entire default
33817 argument. We add the range [first_token, token). */
33818 first_token = cp_lexer_peek_token (parser->lexer);
33819 if (first_token->type == CPP_OPEN_BRACE)
33821 /* For list-initialization, this is straightforward. */
33822 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33823 token = cp_lexer_peek_token (parser->lexer);
33825 else while (true)
33827 bool done = false;
33829 /* Peek at the next token. */
33830 token = cp_lexer_peek_token (parser->lexer);
33831 /* What we do depends on what token we have. */
33832 switch (token->type)
33834 /* In valid code, a default argument must be
33835 immediately followed by a `,' `)', or `...'. */
33836 case CPP_COMMA:
33837 if (depth == 0 && maybe_template_id)
33839 /* If we've seen a '<', we might be in a
33840 template-argument-list. Until Core issue 325 is
33841 resolved, we don't know how this situation ought
33842 to be handled, so try to DTRT. We check whether
33843 what comes after the comma is a valid parameter
33844 declaration list. If it is, then the comma ends
33845 the default argument; otherwise the default
33846 argument continues. */
33847 bool error = false;
33848 cp_token *peek;
33850 /* Set ITALP so cp_parser_parameter_declaration_list
33851 doesn't decide to commit to this parse. */
33852 bool saved_italp = parser->in_template_argument_list_p;
33853 parser->in_template_argument_list_p = true;
33855 cp_parser_parse_tentatively (parser);
33857 if (nsdmi)
33859 /* Parse declarators until we reach a non-comma or
33860 somthing that cannot be an initializer.
33861 Just checking whether we're looking at a single
33862 declarator is insufficient. Consider:
33863 int var = tuple<T,U>::x;
33864 The template parameter 'U' looks exactly like a
33865 declarator. */
33868 int ctor_dtor_or_conv_p;
33869 cp_lexer_consume_token (parser->lexer);
33870 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33871 CP_PARSER_FLAGS_NONE,
33872 &ctor_dtor_or_conv_p,
33873 /*parenthesized_p=*/NULL,
33874 /*member_p=*/true,
33875 /*friend_p=*/false,
33876 /*static_p=*/false);
33877 peek = cp_lexer_peek_token (parser->lexer);
33878 if (cp_parser_error_occurred (parser))
33879 break;
33881 while (peek->type == CPP_COMMA);
33882 /* If we met an '=' or ';' then the original comma
33883 was the end of the NSDMI. Otherwise assume
33884 we're still in the NSDMI. */
33885 error = (peek->type != CPP_EQ
33886 && peek->type != CPP_SEMICOLON);
33888 else
33890 cp_lexer_consume_token (parser->lexer);
33891 begin_scope (sk_function_parms, NULL_TREE);
33892 tree t = cp_parser_parameter_declaration_list
33893 (parser, CP_PARSER_FLAGS_NONE);
33894 if (t == error_mark_node)
33895 error = true;
33896 pop_bindings_and_leave_scope ();
33898 if (!cp_parser_error_occurred (parser) && !error)
33899 done = true;
33900 cp_parser_abort_tentative_parse (parser);
33902 parser->in_template_argument_list_p = saved_italp;
33903 break;
33905 /* FALLTHRU */
33906 case CPP_CLOSE_PAREN:
33907 case CPP_ELLIPSIS:
33908 /* If we run into a non-nested `;', `}', or `]',
33909 then the code is invalid -- but the default
33910 argument is certainly over. */
33911 case CPP_SEMICOLON:
33912 case CPP_CLOSE_BRACE:
33913 case CPP_CLOSE_SQUARE:
33914 if (depth == 0
33915 /* Handle correctly int n = sizeof ... ( p ); */
33916 && token->type != CPP_ELLIPSIS)
33917 done = true;
33918 /* Update DEPTH, if necessary. */
33919 else if (token->type == CPP_CLOSE_PAREN
33920 || token->type == CPP_CLOSE_BRACE
33921 || token->type == CPP_CLOSE_SQUARE)
33922 --depth;
33923 break;
33925 case CPP_OPEN_PAREN:
33926 case CPP_OPEN_SQUARE:
33927 case CPP_OPEN_BRACE:
33928 ++depth;
33929 break;
33931 case CPP_LESS:
33932 if (depth == 0)
33933 /* This might be the comparison operator, or it might
33934 start a template argument list. */
33935 ++maybe_template_id;
33936 break;
33938 case CPP_RSHIFT:
33939 if (cxx_dialect == cxx98)
33940 break;
33941 /* Fall through for C++0x, which treats the `>>'
33942 operator like two `>' tokens in certain
33943 cases. */
33944 gcc_fallthrough ();
33946 case CPP_GREATER:
33947 if (depth == 0)
33949 /* This might be an operator, or it might close a
33950 template argument list. But if a previous '<'
33951 started a template argument list, this will have
33952 closed it, so we can't be in one anymore. */
33953 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
33954 if (maybe_template_id < 0)
33955 maybe_template_id = 0;
33957 break;
33959 /* If we run out of tokens, issue an error message. */
33960 case CPP_EOF:
33961 case CPP_PRAGMA_EOL:
33962 error_at (token->location, "file ends in default argument");
33963 return error_mark_node;
33965 case CPP_NAME:
33966 case CPP_SCOPE:
33967 /* In these cases, we should look for template-ids.
33968 For example, if the default argument is
33969 `X<int, double>()', we need to do name lookup to
33970 figure out whether or not `X' is a template; if
33971 so, the `,' does not end the default argument.
33973 That is not yet done. */
33974 break;
33976 default:
33977 break;
33980 /* If we've reached the end, stop. */
33981 if (done)
33982 break;
33984 /* Add the token to the token block. */
33985 token = cp_lexer_consume_token (parser->lexer);
33988 /* Create a DEFERRED_PARSE to represent the unparsed default
33989 argument. */
33990 default_argument = make_node (DEFERRED_PARSE);
33991 DEFPARSE_TOKENS (default_argument)
33992 = cp_token_cache_new (first_token, token);
33993 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
33995 return default_argument;
33998 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
34000 location_t
34001 defparse_location (tree default_argument)
34003 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
34004 location_t start = tokens->first->location;
34005 location_t end = tokens->last->location;
34006 return make_location (start, start, end);
34009 /* Begin parsing tentatively. We always save tokens while parsing
34010 tentatively so that if the tentative parsing fails we can restore the
34011 tokens. */
34013 static void
34014 cp_parser_parse_tentatively (cp_parser* parser)
34016 /* Enter a new parsing context. */
34017 parser->context = cp_parser_context_new (parser->context);
34018 /* Begin saving tokens. */
34019 cp_lexer_save_tokens (parser->lexer);
34020 /* In order to avoid repetitive access control error messages,
34021 access checks are queued up until we are no longer parsing
34022 tentatively. */
34023 push_deferring_access_checks (dk_deferred);
34026 /* Commit to the currently active tentative parse. */
34028 static void
34029 cp_parser_commit_to_tentative_parse (cp_parser* parser)
34031 cp_parser_context *context;
34032 cp_lexer *lexer;
34034 /* Mark all of the levels as committed. */
34035 lexer = parser->lexer;
34036 for (context = parser->context; context->next; context = context->next)
34038 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34039 break;
34040 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34041 while (!cp_lexer_saving_tokens (lexer))
34042 lexer = lexer->next;
34043 cp_lexer_commit_tokens (lexer);
34047 /* Commit to the topmost currently active tentative parse.
34049 Note that this function shouldn't be called when there are
34050 irreversible side-effects while in a tentative state. For
34051 example, we shouldn't create a permanent entry in the symbol
34052 table, or issue an error message that might not apply if the
34053 tentative parse is aborted. */
34055 static void
34056 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
34058 cp_parser_context *context = parser->context;
34059 cp_lexer *lexer = parser->lexer;
34061 if (context)
34063 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34064 return;
34065 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34067 while (!cp_lexer_saving_tokens (lexer))
34068 lexer = lexer->next;
34069 cp_lexer_commit_tokens (lexer);
34073 /* Abort the currently active tentative parse. All consumed tokens
34074 will be rolled back, and no diagnostics will be issued. */
34076 static void
34077 cp_parser_abort_tentative_parse (cp_parser* parser)
34079 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
34080 || errorcount > 0);
34081 cp_parser_simulate_error (parser);
34082 /* Now, pretend that we want to see if the construct was
34083 successfully parsed. */
34084 cp_parser_parse_definitely (parser);
34087 /* Stop parsing tentatively. If a parse error has occurred, restore the
34088 token stream. Otherwise, commit to the tokens we have consumed.
34089 Returns true if no error occurred; false otherwise. */
34091 static bool
34092 cp_parser_parse_definitely (cp_parser* parser)
34094 bool error_occurred;
34095 cp_parser_context *context;
34097 /* Remember whether or not an error occurred, since we are about to
34098 destroy that information. */
34099 error_occurred = cp_parser_error_occurred (parser);
34100 /* Remove the topmost context from the stack. */
34101 context = parser->context;
34102 parser->context = context->next;
34103 /* If no parse errors occurred, commit to the tentative parse. */
34104 if (!error_occurred)
34106 /* Commit to the tokens read tentatively, unless that was
34107 already done. */
34108 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
34109 cp_lexer_commit_tokens (parser->lexer);
34111 pop_to_parent_deferring_access_checks ();
34113 /* Otherwise, if errors occurred, roll back our state so that things
34114 are just as they were before we began the tentative parse. */
34115 else
34117 cp_lexer_rollback_tokens (parser->lexer);
34118 pop_deferring_access_checks ();
34120 /* Add the context to the front of the free list. */
34121 context->next = cp_parser_context_free_list;
34122 cp_parser_context_free_list = context;
34124 return !error_occurred;
34127 /* Returns true if we are parsing tentatively and are not committed to
34128 this tentative parse. */
34130 static bool
34131 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
34133 return (cp_parser_parsing_tentatively (parser)
34134 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
34137 /* Returns nonzero iff an error has occurred during the most recent
34138 tentative parse. */
34140 static bool
34141 cp_parser_error_occurred (cp_parser* parser)
34143 return (cp_parser_parsing_tentatively (parser)
34144 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
34147 /* Returns nonzero if GNU extensions are allowed. */
34149 static bool
34150 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
34152 return parser->allow_gnu_extensions_p;
34155 /* Objective-C++ Productions */
34158 /* Parse an Objective-C expression, which feeds into a primary-expression
34159 above.
34161 objc-expression:
34162 objc-message-expression
34163 objc-string-literal
34164 objc-encode-expression
34165 objc-protocol-expression
34166 objc-selector-expression
34168 Returns a tree representation of the expression. */
34170 static cp_expr
34171 cp_parser_objc_expression (cp_parser* parser)
34173 /* Try to figure out what kind of declaration is present. */
34174 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34176 switch (kwd->type)
34178 case CPP_OPEN_SQUARE:
34179 return cp_parser_objc_message_expression (parser);
34181 case CPP_OBJC_STRING:
34182 kwd = cp_lexer_consume_token (parser->lexer);
34183 return objc_build_string_object (kwd->u.value);
34185 case CPP_KEYWORD:
34186 switch (kwd->keyword)
34188 case RID_AT_ENCODE:
34189 return cp_parser_objc_encode_expression (parser);
34191 case RID_AT_PROTOCOL:
34192 return cp_parser_objc_protocol_expression (parser);
34194 case RID_AT_SELECTOR:
34195 return cp_parser_objc_selector_expression (parser);
34197 default:
34198 break;
34200 /* FALLTHRU */
34201 default:
34202 error_at (kwd->location,
34203 "misplaced %<@%D%> Objective-C++ construct",
34204 kwd->u.value);
34205 cp_parser_skip_to_end_of_block_or_statement (parser);
34208 return error_mark_node;
34211 /* Parse an Objective-C message expression.
34213 objc-message-expression:
34214 [ objc-message-receiver objc-message-args ]
34216 Returns a representation of an Objective-C message. */
34218 static tree
34219 cp_parser_objc_message_expression (cp_parser* parser)
34221 tree receiver, messageargs;
34223 parser->objective_c_message_context_p = true;
34224 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34225 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
34226 receiver = cp_parser_objc_message_receiver (parser);
34227 messageargs = cp_parser_objc_message_args (parser);
34228 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
34229 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
34231 tree result = objc_build_message_expr (receiver, messageargs);
34233 /* Construct a location e.g.
34234 [self func1:5]
34235 ^~~~~~~~~~~~~~
34236 ranging from the '[' to the ']', with the caret at the start. */
34237 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
34238 protected_set_expr_location (result, combined_loc);
34240 parser->objective_c_message_context_p = false;
34241 return result;
34244 /* Parse an objc-message-receiver.
34246 objc-message-receiver:
34247 expression
34248 simple-type-specifier
34250 Returns a representation of the type or expression. */
34252 static tree
34253 cp_parser_objc_message_receiver (cp_parser* parser)
34255 tree rcv;
34257 /* An Objective-C message receiver may be either (1) a type
34258 or (2) an expression. */
34259 cp_parser_parse_tentatively (parser);
34260 rcv = cp_parser_expression (parser);
34262 /* If that worked out, fine. */
34263 if (cp_parser_parse_definitely (parser))
34264 return rcv;
34266 cp_parser_parse_tentatively (parser);
34267 rcv = cp_parser_simple_type_specifier (parser,
34268 /*decl_specs=*/NULL,
34269 CP_PARSER_FLAGS_NONE);
34271 if (cp_parser_parse_definitely (parser))
34272 return objc_get_class_reference (rcv);
34274 cp_parser_error (parser, "objective-c++ message receiver expected");
34275 return error_mark_node;
34278 /* Parse the arguments and selectors comprising an Objective-C message.
34280 objc-message-args:
34281 objc-selector
34282 objc-selector-args
34283 objc-selector-args , objc-comma-args
34285 objc-selector-args:
34286 objc-selector [opt] : assignment-expression
34287 objc-selector-args objc-selector [opt] : assignment-expression
34289 objc-comma-args:
34290 assignment-expression
34291 objc-comma-args , assignment-expression
34293 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
34294 selector arguments and TREE_VALUE containing a list of comma
34295 arguments. */
34297 static tree
34298 cp_parser_objc_message_args (cp_parser* parser)
34300 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
34301 bool maybe_unary_selector_p = true;
34302 cp_token *token = cp_lexer_peek_token (parser->lexer);
34304 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
34306 tree selector = NULL_TREE, arg;
34308 if (token->type != CPP_COLON)
34309 selector = cp_parser_objc_selector (parser);
34311 /* Detect if we have a unary selector. */
34312 if (maybe_unary_selector_p
34313 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34314 return build_tree_list (selector, NULL_TREE);
34316 maybe_unary_selector_p = false;
34317 cp_parser_require (parser, CPP_COLON, RT_COLON);
34318 arg = cp_parser_assignment_expression (parser);
34320 sel_args
34321 = chainon (sel_args,
34322 build_tree_list (selector, arg));
34324 token = cp_lexer_peek_token (parser->lexer);
34327 /* Handle non-selector arguments, if any. */
34328 while (token->type == CPP_COMMA)
34330 tree arg;
34332 cp_lexer_consume_token (parser->lexer);
34333 arg = cp_parser_assignment_expression (parser);
34335 addl_args
34336 = chainon (addl_args,
34337 build_tree_list (NULL_TREE, arg));
34339 token = cp_lexer_peek_token (parser->lexer);
34342 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
34344 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
34345 return build_tree_list (error_mark_node, error_mark_node);
34348 return build_tree_list (sel_args, addl_args);
34351 /* Parse an Objective-C encode expression.
34353 objc-encode-expression:
34354 @encode objc-typename
34356 Returns an encoded representation of the type argument. */
34358 static cp_expr
34359 cp_parser_objc_encode_expression (cp_parser* parser)
34361 tree type;
34362 cp_token *token;
34363 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34365 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
34366 matching_parens parens;
34367 parens.require_open (parser);
34368 token = cp_lexer_peek_token (parser->lexer);
34369 type = complete_type (cp_parser_type_id (parser));
34370 parens.require_close (parser);
34372 if (!type)
34374 error_at (token->location,
34375 "%<@encode%> must specify a type as an argument");
34376 return error_mark_node;
34379 /* This happens if we find @encode(T) (where T is a template
34380 typename or something dependent on a template typename) when
34381 parsing a template. In that case, we can't compile it
34382 immediately, but we rather create an AT_ENCODE_EXPR which will
34383 need to be instantiated when the template is used.
34385 if (dependent_type_p (type))
34387 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
34388 TREE_READONLY (value) = 1;
34389 return value;
34393 /* Build a location of the form:
34394 @encode(int)
34395 ^~~~~~~~~~~~
34396 with caret==start at the @ token, finishing at the close paren. */
34397 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34399 return cp_expr (objc_build_encode_expr (type), combined_loc);
34402 /* Parse an Objective-C @defs expression. */
34404 static tree
34405 cp_parser_objc_defs_expression (cp_parser *parser)
34407 tree name;
34409 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
34410 matching_parens parens;
34411 parens.require_open (parser);
34412 name = cp_parser_identifier (parser);
34413 parens.require_close (parser);
34415 return objc_get_class_ivars (name);
34418 /* Parse an Objective-C protocol expression.
34420 objc-protocol-expression:
34421 @protocol ( identifier )
34423 Returns a representation of the protocol expression. */
34425 static tree
34426 cp_parser_objc_protocol_expression (cp_parser* parser)
34428 tree proto;
34429 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34431 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
34432 matching_parens parens;
34433 parens.require_open (parser);
34434 proto = cp_parser_identifier (parser);
34435 parens.require_close (parser);
34437 /* Build a location of the form:
34438 @protocol(prot)
34439 ^~~~~~~~~~~~~~~
34440 with caret==start at the @ token, finishing at the close paren. */
34441 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34442 tree result = objc_build_protocol_expr (proto);
34443 protected_set_expr_location (result, combined_loc);
34444 return result;
34447 /* Parse an Objective-C selector expression.
34449 objc-selector-expression:
34450 @selector ( objc-method-signature )
34452 objc-method-signature:
34453 objc-selector
34454 objc-selector-seq
34456 objc-selector-seq:
34457 objc-selector :
34458 objc-selector-seq objc-selector :
34460 Returns a representation of the method selector. */
34462 static tree
34463 cp_parser_objc_selector_expression (cp_parser* parser)
34465 tree sel_seq = NULL_TREE;
34466 bool maybe_unary_selector_p = true;
34467 cp_token *token;
34468 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34470 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
34471 matching_parens parens;
34472 parens.require_open (parser);
34473 token = cp_lexer_peek_token (parser->lexer);
34475 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
34476 || token->type == CPP_SCOPE)
34478 tree selector = NULL_TREE;
34480 if (token->type != CPP_COLON
34481 || token->type == CPP_SCOPE)
34482 selector = cp_parser_objc_selector (parser);
34484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
34485 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
34487 /* Detect if we have a unary selector. */
34488 if (maybe_unary_selector_p)
34490 sel_seq = selector;
34491 goto finish_selector;
34493 else
34495 cp_parser_error (parser, "expected %<:%>");
34498 maybe_unary_selector_p = false;
34499 token = cp_lexer_consume_token (parser->lexer);
34501 if (token->type == CPP_SCOPE)
34503 sel_seq
34504 = chainon (sel_seq,
34505 build_tree_list (selector, NULL_TREE));
34506 sel_seq
34507 = chainon (sel_seq,
34508 build_tree_list (NULL_TREE, NULL_TREE));
34510 else
34511 sel_seq
34512 = chainon (sel_seq,
34513 build_tree_list (selector, NULL_TREE));
34515 token = cp_lexer_peek_token (parser->lexer);
34518 finish_selector:
34519 parens.require_close (parser);
34522 /* Build a location of the form:
34523 @selector(func)
34524 ^~~~~~~~~~~~~~~
34525 with caret==start at the @ token, finishing at the close paren. */
34526 location_t combined_loc = make_location (loc, loc, parser->lexer);
34527 tree result = objc_build_selector_expr (combined_loc, sel_seq);
34528 /* TODO: objc_build_selector_expr doesn't always honor the location. */
34529 protected_set_expr_location (result, combined_loc);
34530 return result;
34533 /* Parse a list of identifiers.
34535 objc-identifier-list:
34536 identifier
34537 objc-identifier-list , identifier
34539 Returns a TREE_LIST of identifier nodes. */
34541 static tree
34542 cp_parser_objc_identifier_list (cp_parser* parser)
34544 tree identifier;
34545 tree list;
34546 cp_token *sep;
34548 identifier = cp_parser_identifier (parser);
34549 if (identifier == error_mark_node)
34550 return error_mark_node;
34552 list = build_tree_list (NULL_TREE, identifier);
34553 sep = cp_lexer_peek_token (parser->lexer);
34555 while (sep->type == CPP_COMMA)
34557 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34558 identifier = cp_parser_identifier (parser);
34559 if (identifier == error_mark_node)
34560 return list;
34562 list = chainon (list, build_tree_list (NULL_TREE,
34563 identifier));
34564 sep = cp_lexer_peek_token (parser->lexer);
34567 return list;
34570 /* Parse an Objective-C alias declaration.
34572 objc-alias-declaration:
34573 @compatibility_alias identifier identifier ;
34575 This function registers the alias mapping with the Objective-C front end.
34576 It returns nothing. */
34578 static void
34579 cp_parser_objc_alias_declaration (cp_parser* parser)
34581 tree alias, orig;
34583 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
34584 alias = cp_parser_identifier (parser);
34585 orig = cp_parser_identifier (parser);
34586 objc_declare_alias (alias, orig);
34587 cp_parser_consume_semicolon_at_end_of_statement (parser);
34590 /* Parse an Objective-C class forward-declaration.
34592 objc-class-declaration:
34593 @class objc-identifier-list ;
34595 The function registers the forward declarations with the Objective-C
34596 front end. It returns nothing. */
34598 static void
34599 cp_parser_objc_class_declaration (cp_parser* parser)
34601 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
34602 while (true)
34604 tree id;
34606 id = cp_parser_identifier (parser);
34607 if (id == error_mark_node)
34608 break;
34610 objc_declare_class (id);
34612 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34613 cp_lexer_consume_token (parser->lexer);
34614 else
34615 break;
34617 cp_parser_consume_semicolon_at_end_of_statement (parser);
34620 /* Parse a list of Objective-C protocol references.
34622 objc-protocol-refs-opt:
34623 objc-protocol-refs [opt]
34625 objc-protocol-refs:
34626 < objc-identifier-list >
34628 Returns a TREE_LIST of identifiers, if any. */
34630 static tree
34631 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
34633 tree protorefs = NULL_TREE;
34635 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
34637 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
34638 protorefs = cp_parser_objc_identifier_list (parser);
34639 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
34642 return protorefs;
34645 /* Parse a Objective-C visibility specification. */
34647 static void
34648 cp_parser_objc_visibility_spec (cp_parser* parser)
34650 cp_token *vis = cp_lexer_peek_token (parser->lexer);
34652 switch (vis->keyword)
34654 case RID_AT_PRIVATE:
34655 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
34656 break;
34657 case RID_AT_PROTECTED:
34658 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
34659 break;
34660 case RID_AT_PUBLIC:
34661 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
34662 break;
34663 case RID_AT_PACKAGE:
34664 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
34665 break;
34666 default:
34667 return;
34670 /* Eat '@private'/'@protected'/'@public'. */
34671 cp_lexer_consume_token (parser->lexer);
34674 /* Parse an Objective-C method type. Return 'true' if it is a class
34675 (+) method, and 'false' if it is an instance (-) method. */
34677 static inline bool
34678 cp_parser_objc_method_type (cp_parser* parser)
34680 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
34681 return true;
34682 else
34683 return false;
34686 /* Parse an Objective-C protocol qualifier. */
34688 static tree
34689 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
34691 tree quals = NULL_TREE, node;
34692 cp_token *token = cp_lexer_peek_token (parser->lexer);
34694 node = token->u.value;
34696 while (node && identifier_p (node)
34697 && (node == ridpointers [(int) RID_IN]
34698 || node == ridpointers [(int) RID_OUT]
34699 || node == ridpointers [(int) RID_INOUT]
34700 || node == ridpointers [(int) RID_BYCOPY]
34701 || node == ridpointers [(int) RID_BYREF]
34702 || node == ridpointers [(int) RID_ONEWAY]))
34704 quals = tree_cons (NULL_TREE, node, quals);
34705 cp_lexer_consume_token (parser->lexer);
34706 token = cp_lexer_peek_token (parser->lexer);
34707 node = token->u.value;
34710 return quals;
34713 /* Parse an Objective-C typename. */
34715 static tree
34716 cp_parser_objc_typename (cp_parser* parser)
34718 tree type_name = NULL_TREE;
34720 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34722 tree proto_quals, cp_type = NULL_TREE;
34724 matching_parens parens;
34725 parens.consume_open (parser); /* Eat '('. */
34726 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
34728 /* An ObjC type name may consist of just protocol qualifiers, in which
34729 case the type shall default to 'id'. */
34730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34732 cp_type = cp_parser_type_id (parser);
34734 /* If the type could not be parsed, an error has already
34735 been produced. For error recovery, behave as if it had
34736 not been specified, which will use the default type
34737 'id'. */
34738 if (cp_type == error_mark_node)
34740 cp_type = NULL_TREE;
34741 /* We need to skip to the closing parenthesis as
34742 cp_parser_type_id() does not seem to do it for
34743 us. */
34744 cp_parser_skip_to_closing_parenthesis (parser,
34745 /*recovering=*/true,
34746 /*or_comma=*/false,
34747 /*consume_paren=*/false);
34751 parens.require_close (parser);
34752 type_name = build_tree_list (proto_quals, cp_type);
34755 return type_name;
34758 /* Check to see if TYPE refers to an Objective-C selector name. */
34760 static bool
34761 cp_parser_objc_selector_p (enum cpp_ttype type)
34763 return (type == CPP_NAME || type == CPP_KEYWORD
34764 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
34765 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
34766 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
34767 || type == CPP_XOR || type == CPP_XOR_EQ);
34770 /* Parse an Objective-C selector. */
34772 static tree
34773 cp_parser_objc_selector (cp_parser* parser)
34775 cp_token *token = cp_lexer_consume_token (parser->lexer);
34777 if (!cp_parser_objc_selector_p (token->type))
34779 error_at (token->location, "invalid Objective-C++ selector name");
34780 return error_mark_node;
34783 /* C++ operator names are allowed to appear in ObjC selectors. */
34784 switch (token->type)
34786 case CPP_AND_AND: return get_identifier ("and");
34787 case CPP_AND_EQ: return get_identifier ("and_eq");
34788 case CPP_AND: return get_identifier ("bitand");
34789 case CPP_OR: return get_identifier ("bitor");
34790 case CPP_COMPL: return get_identifier ("compl");
34791 case CPP_NOT: return get_identifier ("not");
34792 case CPP_NOT_EQ: return get_identifier ("not_eq");
34793 case CPP_OR_OR: return get_identifier ("or");
34794 case CPP_OR_EQ: return get_identifier ("or_eq");
34795 case CPP_XOR: return get_identifier ("xor");
34796 case CPP_XOR_EQ: return get_identifier ("xor_eq");
34797 default: return token->u.value;
34801 /* Parse an Objective-C params list. */
34803 static tree
34804 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
34806 tree params = NULL_TREE;
34807 bool maybe_unary_selector_p = true;
34808 cp_token *token = cp_lexer_peek_token (parser->lexer);
34810 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
34812 tree selector = NULL_TREE, type_name, identifier;
34813 tree parm_attr = NULL_TREE;
34815 if (token->keyword == RID_ATTRIBUTE)
34816 break;
34818 if (token->type != CPP_COLON)
34819 selector = cp_parser_objc_selector (parser);
34821 /* Detect if we have a unary selector. */
34822 if (maybe_unary_selector_p
34823 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34825 params = selector; /* Might be followed by attributes. */
34826 break;
34829 maybe_unary_selector_p = false;
34830 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34832 /* Something went quite wrong. There should be a colon
34833 here, but there is not. Stop parsing parameters. */
34834 break;
34836 type_name = cp_parser_objc_typename (parser);
34837 /* New ObjC allows attributes on parameters too. */
34838 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
34839 parm_attr = cp_parser_attributes_opt (parser);
34840 identifier = cp_parser_identifier (parser);
34842 params
34843 = chainon (params,
34844 objc_build_keyword_decl (selector,
34845 type_name,
34846 identifier,
34847 parm_attr));
34849 token = cp_lexer_peek_token (parser->lexer);
34852 if (params == NULL_TREE)
34854 cp_parser_error (parser, "objective-c++ method declaration is expected");
34855 return error_mark_node;
34858 /* We allow tail attributes for the method. */
34859 if (token->keyword == RID_ATTRIBUTE)
34861 *attributes = cp_parser_attributes_opt (parser);
34862 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
34863 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34864 return params;
34865 cp_parser_error (parser,
34866 "method attributes must be specified at the end");
34867 return error_mark_node;
34870 if (params == NULL_TREE)
34872 cp_parser_error (parser, "objective-c++ method declaration is expected");
34873 return error_mark_node;
34875 return params;
34878 /* Parse the non-keyword Objective-C params. */
34880 static tree
34881 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
34882 tree* attributes)
34884 tree params = make_node (TREE_LIST);
34885 cp_token *token = cp_lexer_peek_token (parser->lexer);
34886 *ellipsisp = false; /* Initially, assume no ellipsis. */
34888 while (token->type == CPP_COMMA)
34890 cp_parameter_declarator *parmdecl;
34891 tree parm;
34893 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34894 token = cp_lexer_peek_token (parser->lexer);
34896 if (token->type == CPP_ELLIPSIS)
34898 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
34899 *ellipsisp = true;
34900 token = cp_lexer_peek_token (parser->lexer);
34901 break;
34904 /* TODO: parse attributes for tail parameters. */
34905 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
34906 false, NULL);
34907 parm = grokdeclarator (parmdecl->declarator,
34908 &parmdecl->decl_specifiers,
34909 PARM, /*initialized=*/0,
34910 /*attrlist=*/NULL);
34912 chainon (params, build_tree_list (NULL_TREE, parm));
34913 token = cp_lexer_peek_token (parser->lexer);
34916 /* We allow tail attributes for the method. */
34917 if (token->keyword == RID_ATTRIBUTE)
34919 if (*attributes == NULL_TREE)
34921 *attributes = cp_parser_attributes_opt (parser);
34922 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
34923 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34924 return params;
34926 else
34927 /* We have an error, but parse the attributes, so that we can
34928 carry on. */
34929 *attributes = cp_parser_attributes_opt (parser);
34931 cp_parser_error (parser,
34932 "method attributes must be specified at the end");
34933 return error_mark_node;
34936 return params;
34939 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
34941 static void
34942 cp_parser_objc_interstitial_code (cp_parser* parser)
34944 cp_token *token = cp_lexer_peek_token (parser->lexer);
34946 /* If the next token is `extern' and the following token is a string
34947 literal, then we have a linkage specification. */
34948 if (token->keyword == RID_EXTERN
34949 && cp_parser_is_pure_string_literal
34950 (cp_lexer_peek_nth_token (parser->lexer, 2)))
34951 cp_parser_linkage_specification (parser, NULL_TREE);
34952 /* Handle #pragma, if any. */
34953 else if (token->type == CPP_PRAGMA)
34954 cp_parser_pragma (parser, pragma_objc_icode, NULL);
34955 /* Allow stray semicolons. */
34956 else if (token->type == CPP_SEMICOLON)
34957 cp_lexer_consume_token (parser->lexer);
34958 /* Mark methods as optional or required, when building protocols. */
34959 else if (token->keyword == RID_AT_OPTIONAL)
34961 cp_lexer_consume_token (parser->lexer);
34962 objc_set_method_opt (true);
34964 else if (token->keyword == RID_AT_REQUIRED)
34966 cp_lexer_consume_token (parser->lexer);
34967 objc_set_method_opt (false);
34969 else if (token->keyword == RID_NAMESPACE)
34970 cp_parser_namespace_definition (parser);
34971 /* Other stray characters must generate errors. */
34972 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
34974 cp_lexer_consume_token (parser->lexer);
34975 error ("stray %qs between Objective-C++ methods",
34976 token->type == CPP_OPEN_BRACE ? "{" : "}");
34978 /* Finally, try to parse a block-declaration, or a function-definition. */
34979 else
34980 cp_parser_block_declaration (parser, /*statement_p=*/false);
34983 /* Parse a method signature. */
34985 static tree
34986 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
34988 tree rettype, kwdparms, optparms;
34989 bool ellipsis = false;
34990 bool is_class_method;
34992 is_class_method = cp_parser_objc_method_type (parser);
34993 rettype = cp_parser_objc_typename (parser);
34994 *attributes = NULL_TREE;
34995 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
34996 if (kwdparms == error_mark_node)
34997 return error_mark_node;
34998 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
34999 if (optparms == error_mark_node)
35000 return error_mark_node;
35002 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
35005 static bool
35006 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
35008 tree tattr;
35009 cp_lexer_save_tokens (parser->lexer);
35010 tattr = cp_parser_attributes_opt (parser);
35011 gcc_assert (tattr) ;
35013 /* If the attributes are followed by a method introducer, this is not allowed.
35014 Dump the attributes and flag the situation. */
35015 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
35016 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
35017 return true;
35019 /* Otherwise, the attributes introduce some interstitial code, possibly so
35020 rewind to allow that check. */
35021 cp_lexer_rollback_tokens (parser->lexer);
35022 return false;
35025 /* Parse an Objective-C method prototype list. */
35027 static void
35028 cp_parser_objc_method_prototype_list (cp_parser* parser)
35030 cp_token *token = cp_lexer_peek_token (parser->lexer);
35032 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
35034 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35036 tree attributes, sig;
35037 bool is_class_method;
35038 if (token->type == CPP_PLUS)
35039 is_class_method = true;
35040 else
35041 is_class_method = false;
35042 sig = cp_parser_objc_method_signature (parser, &attributes);
35043 if (sig == error_mark_node)
35045 cp_parser_skip_to_end_of_block_or_statement (parser);
35046 token = cp_lexer_peek_token (parser->lexer);
35047 continue;
35049 objc_add_method_declaration (is_class_method, sig, attributes);
35050 cp_parser_consume_semicolon_at_end_of_statement (parser);
35052 else if (token->keyword == RID_AT_PROPERTY)
35053 cp_parser_objc_at_property_declaration (parser);
35054 else if (token->keyword == RID_ATTRIBUTE
35055 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35056 warning_at (cp_lexer_peek_token (parser->lexer)->location,
35057 OPT_Wattributes,
35058 "prefix attributes are ignored for methods");
35059 else
35060 /* Allow for interspersed non-ObjC++ code. */
35061 cp_parser_objc_interstitial_code (parser);
35063 token = cp_lexer_peek_token (parser->lexer);
35066 if (token->type != CPP_EOF)
35067 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35068 else
35069 cp_parser_error (parser, "expected %<@end%>");
35071 objc_finish_interface ();
35074 /* Parse an Objective-C method definition list. */
35076 static void
35077 cp_parser_objc_method_definition_list (cp_parser* parser)
35079 for (;;)
35081 cp_token *token = cp_lexer_peek_token (parser->lexer);
35083 if (token->keyword == RID_AT_END)
35085 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35086 break;
35088 else if (token->type == CPP_EOF)
35090 cp_parser_error (parser, "expected %<@end%>");
35091 break;
35093 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35095 bool is_class_method = token->type == CPP_PLUS;
35097 push_deferring_access_checks (dk_deferred);
35098 tree attribute;
35099 tree sig = cp_parser_objc_method_signature (parser, &attribute);
35100 if (sig == error_mark_node)
35101 cp_parser_skip_to_end_of_block_or_statement (parser);
35102 else
35104 objc_start_method_definition (is_class_method, sig,
35105 attribute, NULL_TREE);
35107 /* For historical reasons, we accept an optional semicolon. */
35108 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35109 cp_lexer_consume_token (parser->lexer);
35111 perform_deferred_access_checks (tf_warning_or_error);
35112 stop_deferring_access_checks ();
35113 tree meth
35114 = cp_parser_function_definition_after_declarator (parser, false);
35115 pop_deferring_access_checks ();
35116 objc_finish_method_definition (meth);
35119 /* The following case will be removed once @synthesize is
35120 completely implemented. */
35121 else if (token->keyword == RID_AT_PROPERTY)
35122 cp_parser_objc_at_property_declaration (parser);
35123 else if (token->keyword == RID_AT_SYNTHESIZE)
35124 cp_parser_objc_at_synthesize_declaration (parser);
35125 else if (token->keyword == RID_AT_DYNAMIC)
35126 cp_parser_objc_at_dynamic_declaration (parser);
35127 else if (token->keyword == RID_ATTRIBUTE
35128 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35129 warning_at (token->location, OPT_Wattributes,
35130 "prefix attributes are ignored for methods");
35131 else
35132 /* Allow for interspersed non-ObjC++ code. */
35133 cp_parser_objc_interstitial_code (parser);
35136 objc_finish_implementation ();
35139 /* Parse Objective-C ivars. */
35141 static void
35142 cp_parser_objc_class_ivars (cp_parser* parser)
35144 cp_token *token = cp_lexer_peek_token (parser->lexer);
35146 if (token->type != CPP_OPEN_BRACE)
35147 return; /* No ivars specified. */
35149 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
35150 token = cp_lexer_peek_token (parser->lexer);
35152 while (token->type != CPP_CLOSE_BRACE
35153 && token->keyword != RID_AT_END && token->type != CPP_EOF)
35155 cp_decl_specifier_seq declspecs;
35156 int decl_class_or_enum_p;
35157 tree prefix_attributes;
35159 cp_parser_objc_visibility_spec (parser);
35161 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35162 break;
35164 cp_parser_decl_specifier_seq (parser,
35165 CP_PARSER_FLAGS_OPTIONAL,
35166 &declspecs,
35167 &decl_class_or_enum_p);
35169 /* auto, register, static, extern, mutable. */
35170 if (declspecs.storage_class != sc_none)
35172 cp_parser_error (parser, "invalid type for instance variable");
35173 declspecs.storage_class = sc_none;
35176 /* thread_local. */
35177 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
35179 cp_parser_error (parser, "invalid type for instance variable");
35180 declspecs.locations[ds_thread] = 0;
35183 /* typedef. */
35184 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
35186 cp_parser_error (parser, "invalid type for instance variable");
35187 declspecs.locations[ds_typedef] = 0;
35190 prefix_attributes = declspecs.attributes;
35191 declspecs.attributes = NULL_TREE;
35193 /* Keep going until we hit the `;' at the end of the
35194 declaration. */
35195 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35197 tree width = NULL_TREE, attributes, first_attribute, decl;
35198 cp_declarator *declarator = NULL;
35199 int ctor_dtor_or_conv_p;
35201 /* Check for a (possibly unnamed) bitfield declaration. */
35202 token = cp_lexer_peek_token (parser->lexer);
35203 if (token->type == CPP_COLON)
35204 goto eat_colon;
35206 if (token->type == CPP_NAME
35207 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
35208 == CPP_COLON))
35210 /* Get the name of the bitfield. */
35211 declarator = make_id_declarator (NULL_TREE,
35212 cp_parser_identifier (parser),
35213 sfk_none, token->location);
35215 eat_colon:
35216 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
35217 /* Get the width of the bitfield. */
35218 width
35219 = cp_parser_constant_expression (parser);
35221 else
35223 /* Parse the declarator. */
35224 declarator
35225 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35226 CP_PARSER_FLAGS_NONE,
35227 &ctor_dtor_or_conv_p,
35228 /*parenthesized_p=*/NULL,
35229 /*member_p=*/false,
35230 /*friend_p=*/false,
35231 /*static_p=*/false);
35234 /* Look for attributes that apply to the ivar. */
35235 attributes = cp_parser_attributes_opt (parser);
35236 /* Remember which attributes are prefix attributes and
35237 which are not. */
35238 first_attribute = attributes;
35239 /* Combine the attributes. */
35240 attributes = attr_chainon (prefix_attributes, attributes);
35242 if (width)
35243 /* Create the bitfield declaration. */
35244 decl = grokbitfield (declarator, &declspecs,
35245 width, NULL_TREE, attributes);
35246 else
35247 decl = grokfield (declarator, &declspecs,
35248 NULL_TREE, /*init_const_expr_p=*/false,
35249 NULL_TREE, attributes);
35251 /* Add the instance variable. */
35252 if (decl != error_mark_node && decl != NULL_TREE)
35253 objc_add_instance_variable (decl);
35255 /* Reset PREFIX_ATTRIBUTES. */
35256 if (attributes != error_mark_node)
35258 while (attributes && TREE_CHAIN (attributes) != first_attribute)
35259 attributes = TREE_CHAIN (attributes);
35260 if (attributes)
35261 TREE_CHAIN (attributes) = NULL_TREE;
35264 token = cp_lexer_peek_token (parser->lexer);
35266 if (token->type == CPP_COMMA)
35268 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35269 continue;
35271 break;
35274 cp_parser_consume_semicolon_at_end_of_statement (parser);
35275 token = cp_lexer_peek_token (parser->lexer);
35278 if (token->keyword == RID_AT_END)
35279 cp_parser_error (parser, "expected %<}%>");
35281 /* Do not consume the RID_AT_END, so it will be read again as terminating
35282 the @interface of @implementation. */
35283 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
35284 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
35286 /* For historical reasons, we accept an optional semicolon. */
35287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35288 cp_lexer_consume_token (parser->lexer);
35291 /* Parse an Objective-C protocol declaration. */
35293 static void
35294 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
35296 tree proto, protorefs;
35297 cp_token *tok;
35299 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
35300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35302 tok = cp_lexer_peek_token (parser->lexer);
35303 error_at (tok->location, "identifier expected after %<@protocol%>");
35304 cp_parser_consume_semicolon_at_end_of_statement (parser);
35305 return;
35308 /* See if we have a forward declaration or a definition. */
35309 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
35311 /* Try a forward declaration first. */
35312 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
35314 while (true)
35316 tree id;
35318 id = cp_parser_identifier (parser);
35319 if (id == error_mark_node)
35320 break;
35322 objc_declare_protocol (id, attributes);
35324 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35325 cp_lexer_consume_token (parser->lexer);
35326 else
35327 break;
35329 cp_parser_consume_semicolon_at_end_of_statement (parser);
35332 /* Ok, we got a full-fledged definition (or at least should). */
35333 else
35335 proto = cp_parser_identifier (parser);
35336 protorefs = cp_parser_objc_protocol_refs_opt (parser);
35337 objc_start_protocol (proto, protorefs, attributes);
35338 cp_parser_objc_method_prototype_list (parser);
35342 /* Parse an Objective-C superclass or category. */
35344 static void
35345 cp_parser_objc_superclass_or_category (cp_parser *parser,
35346 bool iface_p,
35347 tree *super,
35348 tree *categ, bool *is_class_extension)
35350 cp_token *next = cp_lexer_peek_token (parser->lexer);
35352 *super = *categ = NULL_TREE;
35353 *is_class_extension = false;
35354 if (next->type == CPP_COLON)
35356 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
35357 *super = cp_parser_identifier (parser);
35359 else if (next->type == CPP_OPEN_PAREN)
35361 matching_parens parens;
35362 parens.consume_open (parser); /* Eat '('. */
35364 /* If there is no category name, and this is an @interface, we
35365 have a class extension. */
35366 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35368 *categ = NULL_TREE;
35369 *is_class_extension = true;
35371 else
35372 *categ = cp_parser_identifier (parser);
35374 parens.require_close (parser);
35378 /* Parse an Objective-C class interface. */
35380 static void
35381 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
35383 tree name, super, categ, protos;
35384 bool is_class_extension;
35386 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
35387 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
35388 name = cp_parser_identifier (parser);
35389 if (name == error_mark_node)
35391 /* It's hard to recover because even if valid @interface stuff
35392 is to follow, we can't compile it (or validate it) if we
35393 don't even know which class it refers to. Let's assume this
35394 was a stray '@interface' token in the stream and skip it.
35396 return;
35398 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
35399 &is_class_extension);
35400 protos = cp_parser_objc_protocol_refs_opt (parser);
35402 /* We have either a class or a category on our hands. */
35403 if (categ || is_class_extension)
35404 objc_start_category_interface (name, categ, protos, attributes);
35405 else
35407 objc_start_class_interface (name, nam_loc, super, protos, attributes);
35408 /* Handle instance variable declarations, if any. */
35409 cp_parser_objc_class_ivars (parser);
35410 objc_continue_interface ();
35413 cp_parser_objc_method_prototype_list (parser);
35416 /* Parse an Objective-C class implementation. */
35418 static void
35419 cp_parser_objc_class_implementation (cp_parser* parser)
35421 tree name, super, categ;
35422 bool is_class_extension;
35424 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
35425 name = cp_parser_identifier (parser);
35426 if (name == error_mark_node)
35428 /* It's hard to recover because even if valid @implementation
35429 stuff is to follow, we can't compile it (or validate it) if
35430 we don't even know which class it refers to. Let's assume
35431 this was a stray '@implementation' token in the stream and
35432 skip it.
35434 return;
35436 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
35437 &is_class_extension);
35439 /* We have either a class or a category on our hands. */
35440 if (categ)
35441 objc_start_category_implementation (name, categ);
35442 else
35444 objc_start_class_implementation (name, super);
35445 /* Handle instance variable declarations, if any. */
35446 cp_parser_objc_class_ivars (parser);
35447 objc_continue_implementation ();
35450 cp_parser_objc_method_definition_list (parser);
35453 /* Consume the @end token and finish off the implementation. */
35455 static void
35456 cp_parser_objc_end_implementation (cp_parser* parser)
35458 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35459 objc_finish_implementation ();
35462 /* Parse an Objective-C declaration. */
35464 static void
35465 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
35467 /* Try to figure out what kind of declaration is present. */
35468 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35470 if (attributes)
35471 switch (kwd->keyword)
35473 case RID_AT_ALIAS:
35474 case RID_AT_CLASS:
35475 case RID_AT_END:
35476 error_at (kwd->location, "attributes may not be specified before"
35477 " the %<@%D%> Objective-C++ keyword",
35478 kwd->u.value);
35479 attributes = NULL;
35480 break;
35481 case RID_AT_IMPLEMENTATION:
35482 warning_at (kwd->location, OPT_Wattributes,
35483 "prefix attributes are ignored before %<@%D%>",
35484 kwd->u.value);
35485 attributes = NULL;
35486 default:
35487 break;
35490 switch (kwd->keyword)
35492 case RID_AT_ALIAS:
35493 cp_parser_objc_alias_declaration (parser);
35494 break;
35495 case RID_AT_CLASS:
35496 cp_parser_objc_class_declaration (parser);
35497 break;
35498 case RID_AT_PROTOCOL:
35499 cp_parser_objc_protocol_declaration (parser, attributes);
35500 break;
35501 case RID_AT_INTERFACE:
35502 cp_parser_objc_class_interface (parser, attributes);
35503 break;
35504 case RID_AT_IMPLEMENTATION:
35505 cp_parser_objc_class_implementation (parser);
35506 break;
35507 case RID_AT_END:
35508 cp_parser_objc_end_implementation (parser);
35509 break;
35510 default:
35511 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35512 kwd->u.value);
35513 cp_parser_skip_to_end_of_block_or_statement (parser);
35517 /* Parse an Objective-C try-catch-finally statement.
35519 objc-try-catch-finally-stmt:
35520 @try compound-statement objc-catch-clause-seq [opt]
35521 objc-finally-clause [opt]
35523 objc-catch-clause-seq:
35524 objc-catch-clause objc-catch-clause-seq [opt]
35526 objc-catch-clause:
35527 @catch ( objc-exception-declaration ) compound-statement
35529 objc-finally-clause:
35530 @finally compound-statement
35532 objc-exception-declaration:
35533 parameter-declaration
35534 '...'
35536 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
35538 Returns NULL_TREE.
35540 PS: This function is identical to c_parser_objc_try_catch_finally_statement
35541 for C. Keep them in sync. */
35543 static tree
35544 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
35546 location_t location;
35547 tree stmt;
35549 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
35550 location = cp_lexer_peek_token (parser->lexer)->location;
35551 objc_maybe_warn_exceptions (location);
35552 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
35553 node, lest it get absorbed into the surrounding block. */
35554 stmt = push_stmt_list ();
35555 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35556 objc_begin_try_stmt (location, pop_stmt_list (stmt));
35558 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
35560 cp_parameter_declarator *parm;
35561 tree parameter_declaration = error_mark_node;
35562 bool seen_open_paren = false;
35563 matching_parens parens;
35565 cp_lexer_consume_token (parser->lexer);
35566 if (parens.require_open (parser))
35567 seen_open_paren = true;
35568 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
35570 /* We have "@catch (...)" (where the '...' are literally
35571 what is in the code). Skip the '...'.
35572 parameter_declaration is set to NULL_TREE, and
35573 objc_being_catch_clauses() knows that that means
35574 '...'. */
35575 cp_lexer_consume_token (parser->lexer);
35576 parameter_declaration = NULL_TREE;
35578 else
35580 /* We have "@catch (NSException *exception)" or something
35581 like that. Parse the parameter declaration. */
35582 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35583 false, NULL);
35584 if (parm == NULL)
35585 parameter_declaration = error_mark_node;
35586 else
35587 parameter_declaration = grokdeclarator (parm->declarator,
35588 &parm->decl_specifiers,
35589 PARM, /*initialized=*/0,
35590 /*attrlist=*/NULL);
35592 if (seen_open_paren)
35593 parens.require_close (parser);
35594 else
35596 /* If there was no open parenthesis, we are recovering from
35597 an error, and we are trying to figure out what mistake
35598 the user has made. */
35600 /* If there is an immediate closing parenthesis, the user
35601 probably forgot the opening one (ie, they typed "@catch
35602 NSException *e)". Parse the closing parenthesis and keep
35603 going. */
35604 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35605 cp_lexer_consume_token (parser->lexer);
35607 /* If these is no immediate closing parenthesis, the user
35608 probably doesn't know that parenthesis are required at
35609 all (ie, they typed "@catch NSException *e"). So, just
35610 forget about the closing parenthesis and keep going. */
35612 objc_begin_catch_clause (parameter_declaration);
35613 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35614 objc_finish_catch_clause ();
35616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
35618 cp_lexer_consume_token (parser->lexer);
35619 location = cp_lexer_peek_token (parser->lexer)->location;
35620 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
35621 node, lest it get absorbed into the surrounding block. */
35622 stmt = push_stmt_list ();
35623 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35624 objc_build_finally_clause (location, pop_stmt_list (stmt));
35627 return objc_finish_try_stmt ();
35630 /* Parse an Objective-C synchronized statement.
35632 objc-synchronized-stmt:
35633 @synchronized ( expression ) compound-statement
35635 Returns NULL_TREE. */
35637 static tree
35638 cp_parser_objc_synchronized_statement (cp_parser *parser)
35640 location_t location;
35641 tree lock, stmt;
35643 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
35645 location = cp_lexer_peek_token (parser->lexer)->location;
35646 objc_maybe_warn_exceptions (location);
35647 matching_parens parens;
35648 parens.require_open (parser);
35649 lock = cp_parser_expression (parser);
35650 parens.require_close (parser);
35652 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
35653 node, lest it get absorbed into the surrounding block. */
35654 stmt = push_stmt_list ();
35655 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35657 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
35660 /* Parse an Objective-C throw statement.
35662 objc-throw-stmt:
35663 @throw assignment-expression [opt] ;
35665 Returns a constructed '@throw' statement. */
35667 static tree
35668 cp_parser_objc_throw_statement (cp_parser *parser)
35670 tree expr = NULL_TREE;
35671 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35673 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
35675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35676 expr = cp_parser_expression (parser);
35678 cp_parser_consume_semicolon_at_end_of_statement (parser);
35680 return objc_build_throw_stmt (loc, expr);
35683 /* Parse an Objective-C statement. */
35685 static tree
35686 cp_parser_objc_statement (cp_parser * parser)
35688 /* Try to figure out what kind of declaration is present. */
35689 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35691 switch (kwd->keyword)
35693 case RID_AT_TRY:
35694 return cp_parser_objc_try_catch_finally_statement (parser);
35695 case RID_AT_SYNCHRONIZED:
35696 return cp_parser_objc_synchronized_statement (parser);
35697 case RID_AT_THROW:
35698 return cp_parser_objc_throw_statement (parser);
35699 default:
35700 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35701 kwd->u.value);
35702 cp_parser_skip_to_end_of_block_or_statement (parser);
35705 return error_mark_node;
35708 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
35709 look ahead to see if an objc keyword follows the attributes. This
35710 is to detect the use of prefix attributes on ObjC @interface and
35711 @protocol. */
35713 static bool
35714 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
35716 cp_lexer_save_tokens (parser->lexer);
35717 tree addon = cp_parser_attributes_opt (parser);
35718 if (addon
35719 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
35721 cp_lexer_commit_tokens (parser->lexer);
35722 if (*attrib)
35723 TREE_CHAIN (*attrib) = addon;
35724 else
35725 *attrib = addon;
35726 return true;
35728 cp_lexer_rollback_tokens (parser->lexer);
35729 return false;
35732 /* This routine is a minimal replacement for
35733 c_parser_struct_declaration () used when parsing the list of
35734 types/names or ObjC++ properties. For example, when parsing the
35735 code
35737 @property (readonly) int a, b, c;
35739 this function is responsible for parsing "int a, int b, int c" and
35740 returning the declarations as CHAIN of DECLs.
35742 TODO: Share this code with cp_parser_objc_class_ivars. It's very
35743 similar parsing. */
35744 static tree
35745 cp_parser_objc_struct_declaration (cp_parser *parser)
35747 tree decls = NULL_TREE;
35748 cp_decl_specifier_seq declspecs;
35749 int decl_class_or_enum_p;
35750 tree prefix_attributes;
35752 cp_parser_decl_specifier_seq (parser,
35753 CP_PARSER_FLAGS_NONE,
35754 &declspecs,
35755 &decl_class_or_enum_p);
35757 if (declspecs.type == error_mark_node)
35758 return error_mark_node;
35760 /* auto, register, static, extern, mutable. */
35761 if (declspecs.storage_class != sc_none)
35763 cp_parser_error (parser, "invalid type for property");
35764 declspecs.storage_class = sc_none;
35767 /* thread_local. */
35768 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
35770 cp_parser_error (parser, "invalid type for property");
35771 declspecs.locations[ds_thread] = 0;
35774 /* typedef. */
35775 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
35777 cp_parser_error (parser, "invalid type for property");
35778 declspecs.locations[ds_typedef] = 0;
35781 prefix_attributes = declspecs.attributes;
35782 declspecs.attributes = NULL_TREE;
35784 /* Keep going until we hit the `;' at the end of the declaration. */
35785 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35787 tree attributes, first_attribute, decl;
35788 cp_declarator *declarator;
35789 cp_token *token;
35791 /* Parse the declarator. */
35792 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35793 CP_PARSER_FLAGS_NONE,
35794 NULL, NULL, false, false, false);
35796 /* Look for attributes that apply to the ivar. */
35797 attributes = cp_parser_attributes_opt (parser);
35798 /* Remember which attributes are prefix attributes and
35799 which are not. */
35800 first_attribute = attributes;
35801 /* Combine the attributes. */
35802 attributes = attr_chainon (prefix_attributes, attributes);
35804 decl = grokfield (declarator, &declspecs,
35805 NULL_TREE, /*init_const_expr_p=*/false,
35806 NULL_TREE, attributes);
35808 if (decl == error_mark_node || decl == NULL_TREE)
35809 return error_mark_node;
35811 /* Reset PREFIX_ATTRIBUTES. */
35812 if (attributes != error_mark_node)
35814 while (attributes && TREE_CHAIN (attributes) != first_attribute)
35815 attributes = TREE_CHAIN (attributes);
35816 if (attributes)
35817 TREE_CHAIN (attributes) = NULL_TREE;
35820 DECL_CHAIN (decl) = decls;
35821 decls = decl;
35823 token = cp_lexer_peek_token (parser->lexer);
35824 if (token->type == CPP_COMMA)
35826 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35827 continue;
35829 else
35830 break;
35832 return decls;
35835 /* Parse an Objective-C @property declaration. The syntax is:
35837 objc-property-declaration:
35838 '@property' objc-property-attributes[opt] struct-declaration ;
35840 objc-property-attributes:
35841 '(' objc-property-attribute-list ')'
35843 objc-property-attribute-list:
35844 objc-property-attribute
35845 objc-property-attribute-list, objc-property-attribute
35847 objc-property-attribute
35848 'getter' = identifier
35849 'setter' = identifier
35850 'readonly'
35851 'readwrite'
35852 'assign'
35853 'retain'
35854 'copy'
35855 'nonatomic'
35857 For example:
35858 @property NSString *name;
35859 @property (readonly) id object;
35860 @property (retain, nonatomic, getter=getTheName) id name;
35861 @property int a, b, c;
35863 PS: This function is identical to
35864 c_parser_objc_at_property_declaration for C. Keep them in sync. */
35865 static void
35866 cp_parser_objc_at_property_declaration (cp_parser *parser)
35868 /* Parse the optional attribute list.
35870 A list of parsed, but not verified, attributes. */
35871 auto_delete_vec<property_attribute_info> prop_attr_list;
35872 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35874 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
35876 /* Parse the optional attribute list... */
35877 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35879 /* Eat the '('. */
35880 matching_parens parens;
35881 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
35882 parens.consume_open (parser);
35883 bool syntax_error = false;
35885 /* Allow empty @property attribute lists, but with a warning. */
35886 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
35887 location_t attr_comb;
35888 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35890 attr_comb = make_location (attr_end, attr_start, attr_end);
35891 warning_at (attr_comb, OPT_Wattributes,
35892 "empty property attribute list");
35894 else
35895 while (true)
35897 cp_token *token = cp_lexer_peek_token (parser->lexer);
35898 attr_start = token->location;
35899 attr_end = get_finish (token->location);
35900 attr_comb = make_location (attr_start, attr_start, attr_end);
35902 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
35904 warning_at (attr_comb, OPT_Wattributes,
35905 "missing property attribute");
35906 if (token->type == CPP_CLOSE_PAREN)
35907 break;
35908 cp_lexer_consume_token (parser->lexer);
35909 continue;
35912 tree attr_name = NULL_TREE;
35913 if (identifier_p (token->u.value))
35914 attr_name = token->u.value;
35916 enum rid keyword;
35917 if (token->type == CPP_NAME)
35918 keyword = C_RID_CODE (token->u.value);
35919 else if (token->type == CPP_KEYWORD
35920 && token->keyword == RID_CLASS)
35921 /* Account for accepting the 'class' keyword in this context. */
35922 keyword = RID_CLASS;
35923 else
35924 keyword = RID_MAX; /* By definition, an unknown property. */
35925 cp_lexer_consume_token (parser->lexer);
35927 enum objc_property_attribute_kind prop_kind
35928 = objc_prop_attr_kind_for_rid (keyword);
35929 property_attribute_info *prop
35930 = new property_attribute_info (attr_name, attr_comb, prop_kind);
35931 prop_attr_list.safe_push (prop);
35933 tree meth_name;
35934 switch (prop->prop_kind)
35936 default: break;
35937 case OBJC_PROPERTY_ATTR_UNKNOWN:
35938 if (attr_name)
35939 error_at (attr_start, "unknown property attribute %qE",
35940 attr_name);
35941 else
35942 error_at (attr_start, "unknown property attribute");
35943 prop->parse_error = syntax_error = true;
35944 break;
35946 case OBJC_PROPERTY_ATTR_GETTER:
35947 case OBJC_PROPERTY_ATTR_SETTER:
35948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35950 attr_comb = make_location (attr_end, attr_start, attr_end);
35951 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
35952 attr_name);
35953 prop->parse_error = syntax_error = true;
35954 break;
35957 token = cp_lexer_peek_token (parser->lexer);
35958 attr_end = token->location;
35959 cp_lexer_consume_token (parser->lexer); /* eat the = */
35961 if (!cp_parser_objc_selector_p
35962 (cp_lexer_peek_token (parser->lexer)->type))
35964 attr_comb = make_location (attr_end, attr_start, attr_end);
35965 error_at (attr_comb, "expected %qE selector name",
35966 attr_name);
35967 prop->parse_error = syntax_error = true;
35968 break;
35971 /* Get the end of the method name, and consume the name. */
35972 token = cp_lexer_peek_token (parser->lexer);
35973 attr_end = get_finish (token->location);
35974 /* Because method names may contain C++ keywords, we have a
35975 routine to fetch them (this also consumes the token). */
35976 meth_name = cp_parser_objc_selector (parser);
35978 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
35980 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35982 attr_comb = make_location (attr_end, attr_start,
35983 attr_end);
35984 error_at (attr_comb, "setter method names must"
35985 " terminate with %<:%>");
35986 prop->parse_error = syntax_error = true;
35988 else
35990 attr_end = get_finish (cp_lexer_peek_token
35991 (parser->lexer)->location);
35992 cp_lexer_consume_token (parser->lexer);
35994 attr_comb = make_location (attr_start, attr_start,
35995 attr_end);
35997 else
35998 attr_comb = make_location (attr_start, attr_start,
35999 attr_end);
36000 prop->ident = meth_name;
36001 /* Updated location including all that was successfully
36002 parsed. */
36003 prop->prop_loc = attr_comb;
36004 break;
36007 /* If we see a comma here, then keep going - even if we already
36008 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
36009 this makes a more useful output and avoid spurious warnings
36010 about missing attributes that are, in fact, specified after the
36011 one with the syntax error. */
36012 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36013 cp_lexer_consume_token (parser->lexer);
36014 else
36015 break;
36018 if (syntax_error || !parens.require_close (parser))
36019 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36020 /*or_comma=*/false,
36021 /*consume_paren=*/true);
36024 /* 'properties' is the list of properties that we read. Usually a
36025 single one, but maybe more (eg, in "@property int a, b, c;" there
36026 are three).
36027 TODO: Update this parsing so that it accepts (erroneous) bitfields so
36028 that we can issue a meaningful and consistent (between C/C++) error
36029 message from objc_add_property_declaration (). */
36030 tree properties = cp_parser_objc_struct_declaration (parser);
36032 if (properties == error_mark_node)
36033 cp_parser_skip_to_end_of_statement (parser);
36034 else if (properties == NULL_TREE)
36035 cp_parser_error (parser, "expected identifier");
36036 else
36038 /* Comma-separated properties are chained together in reverse order;
36039 add them one by one. */
36040 properties = nreverse (properties);
36041 for (; properties; properties = TREE_CHAIN (properties))
36042 objc_add_property_declaration (loc, copy_node (properties),
36043 prop_attr_list);
36046 cp_parser_consume_semicolon_at_end_of_statement (parser);
36049 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
36051 objc-synthesize-declaration:
36052 @synthesize objc-synthesize-identifier-list ;
36054 objc-synthesize-identifier-list:
36055 objc-synthesize-identifier
36056 objc-synthesize-identifier-list, objc-synthesize-identifier
36058 objc-synthesize-identifier
36059 identifier
36060 identifier = identifier
36062 For example:
36063 @synthesize MyProperty;
36064 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
36066 PS: This function is identical to c_parser_objc_at_synthesize_declaration
36067 for C. Keep them in sync.
36069 static void
36070 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
36072 tree list = NULL_TREE;
36073 location_t loc;
36074 loc = cp_lexer_peek_token (parser->lexer)->location;
36076 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
36077 while (true)
36079 tree property, ivar;
36080 property = cp_parser_identifier (parser);
36081 if (property == error_mark_node)
36083 cp_parser_consume_semicolon_at_end_of_statement (parser);
36084 return;
36086 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
36088 cp_lexer_consume_token (parser->lexer);
36089 ivar = cp_parser_identifier (parser);
36090 if (ivar == error_mark_node)
36092 cp_parser_consume_semicolon_at_end_of_statement (parser);
36093 return;
36096 else
36097 ivar = NULL_TREE;
36098 list = chainon (list, build_tree_list (ivar, property));
36099 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36100 cp_lexer_consume_token (parser->lexer);
36101 else
36102 break;
36104 cp_parser_consume_semicolon_at_end_of_statement (parser);
36105 objc_add_synthesize_declaration (loc, list);
36108 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
36110 objc-dynamic-declaration:
36111 @dynamic identifier-list ;
36113 For example:
36114 @dynamic MyProperty;
36115 @dynamic MyProperty, AnotherProperty;
36117 PS: This function is identical to c_parser_objc_at_dynamic_declaration
36118 for C. Keep them in sync.
36120 static void
36121 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
36123 tree list = NULL_TREE;
36124 location_t loc;
36125 loc = cp_lexer_peek_token (parser->lexer)->location;
36127 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
36128 while (true)
36130 tree property;
36131 property = cp_parser_identifier (parser);
36132 if (property == error_mark_node)
36134 cp_parser_consume_semicolon_at_end_of_statement (parser);
36135 return;
36137 list = chainon (list, build_tree_list (NULL, property));
36138 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36139 cp_lexer_consume_token (parser->lexer);
36140 else
36141 break;
36143 cp_parser_consume_semicolon_at_end_of_statement (parser);
36144 objc_add_dynamic_declaration (loc, list);
36148 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
36150 /* Returns name of the next clause.
36151 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
36152 the token is not consumed. Otherwise appropriate pragma_omp_clause is
36153 returned and the token is consumed. */
36155 static pragma_omp_clause
36156 cp_parser_omp_clause_name (cp_parser *parser)
36158 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
36160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36161 result = PRAGMA_OACC_CLAUSE_AUTO;
36162 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
36163 result = PRAGMA_OMP_CLAUSE_IF;
36164 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
36165 result = PRAGMA_OMP_CLAUSE_DEFAULT;
36166 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
36167 result = PRAGMA_OACC_CLAUSE_DELETE;
36168 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
36169 result = PRAGMA_OMP_CLAUSE_PRIVATE;
36170 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36171 result = PRAGMA_OMP_CLAUSE_FOR;
36172 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36174 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36175 const char *p = IDENTIFIER_POINTER (id);
36177 switch (p[0])
36179 case 'a':
36180 if (!strcmp ("affinity", p))
36181 result = PRAGMA_OMP_CLAUSE_AFFINITY;
36182 else if (!strcmp ("aligned", p))
36183 result = PRAGMA_OMP_CLAUSE_ALIGNED;
36184 else if (!strcmp ("allocate", p))
36185 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
36186 else if (!strcmp ("async", p))
36187 result = PRAGMA_OACC_CLAUSE_ASYNC;
36188 else if (!strcmp ("attach", p))
36189 result = PRAGMA_OACC_CLAUSE_ATTACH;
36190 break;
36191 case 'b':
36192 if (!strcmp ("bind", p))
36193 result = PRAGMA_OMP_CLAUSE_BIND;
36194 break;
36195 case 'c':
36196 if (!strcmp ("collapse", p))
36197 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
36198 else if (!strcmp ("copy", p))
36199 result = PRAGMA_OACC_CLAUSE_COPY;
36200 else if (!strcmp ("copyin", p))
36201 result = PRAGMA_OMP_CLAUSE_COPYIN;
36202 else if (!strcmp ("copyout", p))
36203 result = PRAGMA_OACC_CLAUSE_COPYOUT;
36204 else if (!strcmp ("copyprivate", p))
36205 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
36206 else if (!strcmp ("create", p))
36207 result = PRAGMA_OACC_CLAUSE_CREATE;
36208 break;
36209 case 'd':
36210 if (!strcmp ("defaultmap", p))
36211 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
36212 else if (!strcmp ("depend", p))
36213 result = PRAGMA_OMP_CLAUSE_DEPEND;
36214 else if (!strcmp ("detach", p))
36215 result = PRAGMA_OACC_CLAUSE_DETACH;
36216 else if (!strcmp ("device", p))
36217 result = PRAGMA_OMP_CLAUSE_DEVICE;
36218 else if (!strcmp ("deviceptr", p))
36219 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
36220 else if (!strcmp ("device_resident", p))
36221 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
36222 else if (!strcmp ("device_type", p))
36223 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
36224 else if (!strcmp ("dist_schedule", p))
36225 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
36226 break;
36227 case 'f':
36228 if (!strcmp ("filter", p))
36229 result = PRAGMA_OMP_CLAUSE_FILTER;
36230 else if (!strcmp ("final", p))
36231 result = PRAGMA_OMP_CLAUSE_FINAL;
36232 else if (!strcmp ("finalize", p))
36233 result = PRAGMA_OACC_CLAUSE_FINALIZE;
36234 else if (!strcmp ("firstprivate", p))
36235 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
36236 else if (!strcmp ("from", p))
36237 result = PRAGMA_OMP_CLAUSE_FROM;
36238 break;
36239 case 'g':
36240 if (!strcmp ("gang", p))
36241 result = PRAGMA_OACC_CLAUSE_GANG;
36242 else if (!strcmp ("grainsize", p))
36243 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
36244 break;
36245 case 'h':
36246 if (!strcmp ("hint", p))
36247 result = PRAGMA_OMP_CLAUSE_HINT;
36248 else if (!strcmp ("host", p))
36249 result = PRAGMA_OACC_CLAUSE_HOST;
36250 break;
36251 case 'i':
36252 if (!strcmp ("if_present", p))
36253 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
36254 else if (!strcmp ("in_reduction", p))
36255 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
36256 else if (!strcmp ("inbranch", p))
36257 result = PRAGMA_OMP_CLAUSE_INBRANCH;
36258 else if (!strcmp ("independent", p))
36259 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
36260 else if (!strcmp ("is_device_ptr", p))
36261 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
36262 break;
36263 case 'l':
36264 if (!strcmp ("lastprivate", p))
36265 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
36266 else if (!strcmp ("linear", p))
36267 result = PRAGMA_OMP_CLAUSE_LINEAR;
36268 else if (!strcmp ("link", p))
36269 result = PRAGMA_OMP_CLAUSE_LINK;
36270 break;
36271 case 'm':
36272 if (!strcmp ("map", p))
36273 result = PRAGMA_OMP_CLAUSE_MAP;
36274 else if (!strcmp ("mergeable", p))
36275 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
36276 break;
36277 case 'n':
36278 if (!strcmp ("no_create", p))
36279 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
36280 else if (!strcmp ("nogroup", p))
36281 result = PRAGMA_OMP_CLAUSE_NOGROUP;
36282 else if (!strcmp ("nohost", p))
36283 result = PRAGMA_OACC_CLAUSE_NOHOST;
36284 else if (!strcmp ("nontemporal", p))
36285 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
36286 else if (!strcmp ("notinbranch", p))
36287 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
36288 else if (!strcmp ("nowait", p))
36289 result = PRAGMA_OMP_CLAUSE_NOWAIT;
36290 else if (!strcmp ("num_gangs", p))
36291 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
36292 else if (!strcmp ("num_tasks", p))
36293 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
36294 else if (!strcmp ("num_teams", p))
36295 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
36296 else if (!strcmp ("num_threads", p))
36297 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
36298 else if (!strcmp ("num_workers", p))
36299 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
36300 break;
36301 case 'o':
36302 if (!strcmp ("ordered", p))
36303 result = PRAGMA_OMP_CLAUSE_ORDERED;
36304 else if (!strcmp ("order", p))
36305 result = PRAGMA_OMP_CLAUSE_ORDER;
36306 break;
36307 case 'p':
36308 if (!strcmp ("parallel", p))
36309 result = PRAGMA_OMP_CLAUSE_PARALLEL;
36310 else if (!strcmp ("present", p))
36311 result = PRAGMA_OACC_CLAUSE_PRESENT;
36312 else if (!strcmp ("present_or_copy", p)
36313 || !strcmp ("pcopy", p))
36314 result = PRAGMA_OACC_CLAUSE_COPY;
36315 else if (!strcmp ("present_or_copyin", p)
36316 || !strcmp ("pcopyin", p))
36317 result = PRAGMA_OACC_CLAUSE_COPYIN;
36318 else if (!strcmp ("present_or_copyout", p)
36319 || !strcmp ("pcopyout", p))
36320 result = PRAGMA_OACC_CLAUSE_COPYOUT;
36321 else if (!strcmp ("present_or_create", p)
36322 || !strcmp ("pcreate", p))
36323 result = PRAGMA_OACC_CLAUSE_CREATE;
36324 else if (!strcmp ("priority", p))
36325 result = PRAGMA_OMP_CLAUSE_PRIORITY;
36326 else if (!strcmp ("proc_bind", p))
36327 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
36328 break;
36329 case 'r':
36330 if (!strcmp ("reduction", p))
36331 result = PRAGMA_OMP_CLAUSE_REDUCTION;
36332 break;
36333 case 's':
36334 if (!strcmp ("safelen", p))
36335 result = PRAGMA_OMP_CLAUSE_SAFELEN;
36336 else if (!strcmp ("schedule", p))
36337 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
36338 else if (!strcmp ("sections", p))
36339 result = PRAGMA_OMP_CLAUSE_SECTIONS;
36340 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
36341 result = PRAGMA_OACC_CLAUSE_HOST;
36342 else if (!strcmp ("seq", p))
36343 result = PRAGMA_OACC_CLAUSE_SEQ;
36344 else if (!strcmp ("shared", p))
36345 result = PRAGMA_OMP_CLAUSE_SHARED;
36346 else if (!strcmp ("simd", p))
36347 result = PRAGMA_OMP_CLAUSE_SIMD;
36348 else if (!strcmp ("simdlen", p))
36349 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
36350 break;
36351 case 't':
36352 if (!strcmp ("task_reduction", p))
36353 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
36354 else if (!strcmp ("taskgroup", p))
36355 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
36356 else if (!strcmp ("thread_limit", p))
36357 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
36358 else if (!strcmp ("threads", p))
36359 result = PRAGMA_OMP_CLAUSE_THREADS;
36360 else if (!strcmp ("tile", p))
36361 result = PRAGMA_OACC_CLAUSE_TILE;
36362 else if (!strcmp ("to", p))
36363 result = PRAGMA_OMP_CLAUSE_TO;
36364 break;
36365 case 'u':
36366 if (!strcmp ("uniform", p))
36367 result = PRAGMA_OMP_CLAUSE_UNIFORM;
36368 else if (!strcmp ("untied", p))
36369 result = PRAGMA_OMP_CLAUSE_UNTIED;
36370 else if (!strcmp ("use_device", p))
36371 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
36372 else if (!strcmp ("use_device_addr", p))
36373 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
36374 else if (!strcmp ("use_device_ptr", p))
36375 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
36376 break;
36377 case 'v':
36378 if (!strcmp ("vector", p))
36379 result = PRAGMA_OACC_CLAUSE_VECTOR;
36380 else if (!strcmp ("vector_length", p))
36381 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
36382 break;
36383 case 'w':
36384 if (!strcmp ("wait", p))
36385 result = PRAGMA_OACC_CLAUSE_WAIT;
36386 else if (!strcmp ("worker", p))
36387 result = PRAGMA_OACC_CLAUSE_WORKER;
36388 break;
36392 if (result != PRAGMA_OMP_CLAUSE_NONE)
36393 cp_lexer_consume_token (parser->lexer);
36395 return result;
36398 /* Validate that a clause of the given type does not already exist. */
36400 static void
36401 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
36402 const char *name, location_t location)
36404 if (omp_find_clause (clauses, code))
36405 error_at (location, "too many %qs clauses", name);
36408 /* OpenMP 2.5:
36409 variable-list:
36410 identifier
36411 variable-list , identifier
36413 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
36414 colon). An opening parenthesis will have been consumed by the caller.
36416 If KIND is nonzero, create the appropriate node and install the decl
36417 in OMP_CLAUSE_DECL and add the node to the head of the list.
36419 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
36420 return the list created.
36422 COLON can be NULL if only closing parenthesis should end the list,
36423 or pointer to bool which will receive false if the list is terminated
36424 by closing parenthesis or true if the list is terminated by colon.
36426 The optional ALLOW_DEREF argument is true if list items can use the deref
36427 (->) operator. */
36429 struct omp_dim
36431 tree low_bound, length;
36432 location_t loc;
36433 bool no_colon;
36434 omp_dim (tree lb, tree len, location_t lo, bool nc)
36435 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
36438 static tree
36439 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
36440 tree list, bool *colon,
36441 bool allow_deref = false)
36443 auto_vec<omp_dim> dims;
36444 bool array_section_p;
36445 cp_token *token;
36446 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36447 if (colon)
36449 parser->colon_corrects_to_scope_p = false;
36450 *colon = false;
36452 while (1)
36454 tree name, decl;
36456 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36457 cp_parser_parse_tentatively (parser);
36458 token = cp_lexer_peek_token (parser->lexer);
36459 if (kind != 0
36460 && cp_parser_is_keyword (token, RID_THIS))
36462 decl = finish_this_expr ();
36463 if (TREE_CODE (decl) == NON_LVALUE_EXPR
36464 || CONVERT_EXPR_P (decl))
36465 decl = TREE_OPERAND (decl, 0);
36466 cp_lexer_consume_token (parser->lexer);
36468 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
36469 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
36470 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
36472 cp_id_kind idk;
36473 decl = cp_parser_primary_expression (parser, false, false, false,
36474 &idk);
36476 else
36478 name = cp_parser_id_expression (parser, /*template_p=*/false,
36479 /*check_dependency_p=*/true,
36480 /*template_p=*/NULL,
36481 /*declarator_p=*/false,
36482 /*optional_p=*/false);
36483 if (name == error_mark_node)
36485 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36486 && cp_parser_simulate_error (parser))
36487 goto depend_lvalue;
36488 goto skip_comma;
36491 if (identifier_p (name))
36492 decl = cp_parser_lookup_name_simple (parser, name, token->location);
36493 else
36494 decl = name;
36495 if (decl == error_mark_node)
36497 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36498 && cp_parser_simulate_error (parser))
36499 goto depend_lvalue;
36500 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
36501 token->location);
36504 if (outer_automatic_var_p (decl))
36505 decl = process_outer_var_ref (decl, tf_warning_or_error);
36506 if (decl == error_mark_node)
36508 else if (kind != 0)
36510 switch (kind)
36512 case OMP_CLAUSE__CACHE_:
36513 /* The OpenACC cache directive explicitly only allows "array
36514 elements or subarrays". */
36515 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
36517 error_at (token->location, "expected %<[%>");
36518 decl = error_mark_node;
36519 break;
36521 /* FALLTHROUGH. */
36522 case OMP_CLAUSE_MAP:
36523 case OMP_CLAUSE_FROM:
36524 case OMP_CLAUSE_TO:
36525 start_component_ref:
36526 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36527 || (allow_deref
36528 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
36530 cpp_ttype ttype
36531 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36532 ? CPP_DOT : CPP_DEREF;
36533 location_t loc
36534 = cp_lexer_peek_token (parser->lexer)->location;
36535 cp_id_kind idk = CP_ID_KIND_NONE;
36536 cp_lexer_consume_token (parser->lexer);
36537 decl = convert_from_reference (decl);
36538 decl
36539 = cp_parser_postfix_dot_deref_expression (parser, ttype,
36540 decl, false,
36541 &idk, loc);
36543 /* FALLTHROUGH. */
36544 case OMP_CLAUSE_AFFINITY:
36545 case OMP_CLAUSE_DEPEND:
36546 case OMP_CLAUSE_REDUCTION:
36547 case OMP_CLAUSE_IN_REDUCTION:
36548 case OMP_CLAUSE_TASK_REDUCTION:
36549 array_section_p = false;
36550 dims.truncate (0);
36551 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
36553 location_t loc = UNKNOWN_LOCATION;
36554 tree low_bound = NULL_TREE, length = NULL_TREE;
36555 bool no_colon = false;
36557 parser->colon_corrects_to_scope_p = false;
36558 cp_lexer_consume_token (parser->lexer);
36559 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36561 loc = cp_lexer_peek_token (parser->lexer)->location;
36562 low_bound = cp_parser_expression (parser);
36563 /* Later handling is not prepared to see through these. */
36564 gcc_checking_assert (!location_wrapper_p (low_bound));
36566 if (!colon)
36567 parser->colon_corrects_to_scope_p
36568 = saved_colon_corrects_to_scope_p;
36569 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
36571 length = integer_one_node;
36572 no_colon = true;
36574 else
36576 /* Look for `:'. */
36577 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36579 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36580 && cp_parser_simulate_error (parser))
36581 goto depend_lvalue;
36582 goto skip_comma;
36584 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36585 cp_parser_commit_to_tentative_parse (parser);
36586 else
36587 array_section_p = true;
36588 if (!cp_lexer_next_token_is (parser->lexer,
36589 CPP_CLOSE_SQUARE))
36591 length = cp_parser_expression (parser);
36592 /* Later handling is not prepared to see through these. */
36593 gcc_checking_assert (!location_wrapper_p (length));
36596 /* Look for the closing `]'. */
36597 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
36598 RT_CLOSE_SQUARE))
36600 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36601 && cp_parser_simulate_error (parser))
36602 goto depend_lvalue;
36603 goto skip_comma;
36606 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
36609 if ((kind == OMP_CLAUSE_MAP
36610 || kind == OMP_CLAUSE_FROM
36611 || kind == OMP_CLAUSE_TO)
36612 && !array_section_p
36613 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36614 || (allow_deref
36615 && cp_lexer_next_token_is (parser->lexer,
36616 CPP_DEREF))))
36618 for (unsigned i = 0; i < dims.length (); i++)
36620 gcc_assert (dims[i].length == integer_one_node);
36621 decl = build_array_ref (dims[i].loc,
36622 decl, dims[i].low_bound);
36624 goto start_component_ref;
36626 else
36627 for (unsigned i = 0; i < dims.length (); i++)
36628 decl = tree_cons (dims[i].low_bound, dims[i].length, decl);
36630 break;
36631 default:
36632 break;
36635 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
36638 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
36639 && cp_parser_simulate_error (parser))
36641 depend_lvalue:
36642 cp_parser_abort_tentative_parse (parser);
36643 decl = cp_parser_assignment_expression (parser, NULL,
36644 false, false);
36646 else
36647 cp_parser_parse_definitely (parser);
36650 tree u = build_omp_clause (token->location, kind);
36651 OMP_CLAUSE_DECL (u) = decl;
36652 OMP_CLAUSE_CHAIN (u) = list;
36653 list = u;
36655 else
36656 list = tree_cons (decl, NULL_TREE, list);
36658 get_comma:
36659 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
36660 break;
36661 cp_lexer_consume_token (parser->lexer);
36664 if (colon)
36665 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36667 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36669 *colon = true;
36670 cp_parser_require (parser, CPP_COLON, RT_COLON);
36671 return list;
36674 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36676 int ending;
36678 /* Try to resync to an unnested comma. Copied from
36679 cp_parser_parenthesized_expression_list. */
36680 skip_comma:
36681 if (colon)
36682 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36683 ending = cp_parser_skip_to_closing_parenthesis (parser,
36684 /*recovering=*/true,
36685 /*or_comma=*/true,
36686 /*consume_paren=*/true);
36687 if (ending < 0)
36688 goto get_comma;
36691 return list;
36694 /* Similarly, but expect leading and trailing parenthesis. This is a very
36695 common case for omp clauses. */
36697 static tree
36698 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
36699 bool allow_deref = false)
36701 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36702 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
36703 allow_deref);
36704 return list;
36707 /* OpenACC 2.0:
36708 copy ( variable-list )
36709 copyin ( variable-list )
36710 copyout ( variable-list )
36711 create ( variable-list )
36712 delete ( variable-list )
36713 present ( variable-list )
36715 OpenACC 2.6:
36716 no_create ( variable-list )
36717 attach ( variable-list )
36718 detach ( variable-list ) */
36720 static tree
36721 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
36722 tree list)
36724 enum gomp_map_kind kind;
36725 switch (c_kind)
36727 case PRAGMA_OACC_CLAUSE_ATTACH:
36728 kind = GOMP_MAP_ATTACH;
36729 break;
36730 case PRAGMA_OACC_CLAUSE_COPY:
36731 kind = GOMP_MAP_TOFROM;
36732 break;
36733 case PRAGMA_OACC_CLAUSE_COPYIN:
36734 kind = GOMP_MAP_TO;
36735 break;
36736 case PRAGMA_OACC_CLAUSE_COPYOUT:
36737 kind = GOMP_MAP_FROM;
36738 break;
36739 case PRAGMA_OACC_CLAUSE_CREATE:
36740 kind = GOMP_MAP_ALLOC;
36741 break;
36742 case PRAGMA_OACC_CLAUSE_DELETE:
36743 kind = GOMP_MAP_RELEASE;
36744 break;
36745 case PRAGMA_OACC_CLAUSE_DETACH:
36746 kind = GOMP_MAP_DETACH;
36747 break;
36748 case PRAGMA_OACC_CLAUSE_DEVICE:
36749 kind = GOMP_MAP_FORCE_TO;
36750 break;
36751 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
36752 kind = GOMP_MAP_DEVICE_RESIDENT;
36753 break;
36754 case PRAGMA_OACC_CLAUSE_HOST:
36755 kind = GOMP_MAP_FORCE_FROM;
36756 break;
36757 case PRAGMA_OACC_CLAUSE_LINK:
36758 kind = GOMP_MAP_LINK;
36759 break;
36760 case PRAGMA_OACC_CLAUSE_NO_CREATE:
36761 kind = GOMP_MAP_IF_PRESENT;
36762 break;
36763 case PRAGMA_OACC_CLAUSE_PRESENT:
36764 kind = GOMP_MAP_FORCE_PRESENT;
36765 break;
36766 default:
36767 gcc_unreachable ();
36769 tree nl, c;
36770 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
36772 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
36773 OMP_CLAUSE_SET_MAP_KIND (c, kind);
36775 return nl;
36778 /* OpenACC 2.0:
36779 deviceptr ( variable-list ) */
36781 static tree
36782 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
36784 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36785 tree vars, t;
36787 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
36788 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
36789 variable-list must only allow for pointer variables. */
36790 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36791 for (t = vars; t; t = TREE_CHAIN (t))
36793 tree v = TREE_PURPOSE (t);
36794 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
36795 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
36796 OMP_CLAUSE_DECL (u) = v;
36797 OMP_CLAUSE_CHAIN (u) = list;
36798 list = u;
36801 return list;
36804 /* OpenACC 2.5:
36805 auto
36806 finalize
36807 independent
36808 nohost
36809 seq */
36811 static tree
36812 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
36813 tree list)
36815 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
36817 tree c = build_omp_clause (loc, code);
36818 OMP_CLAUSE_CHAIN (c) = list;
36820 return c;
36823 /* OpenACC:
36824 num_gangs ( expression )
36825 num_workers ( expression )
36826 vector_length ( expression ) */
36828 static tree
36829 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
36830 const char *str, tree list)
36832 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36834 matching_parens parens;
36835 if (!parens.require_open (parser))
36836 return list;
36838 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
36840 if (t == error_mark_node
36841 || !parens.require_close (parser))
36843 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36844 /*or_comma=*/false,
36845 /*consume_paren=*/true);
36846 return list;
36849 check_no_duplicate_clause (list, code, str, loc);
36851 tree c = build_omp_clause (loc, code);
36852 OMP_CLAUSE_OPERAND (c, 0) = t;
36853 OMP_CLAUSE_CHAIN (c) = list;
36854 return c;
36857 /* OpenACC:
36859 gang [( gang-arg-list )]
36860 worker [( [num:] int-expr )]
36861 vector [( [length:] int-expr )]
36863 where gang-arg is one of:
36865 [num:] int-expr
36866 static: size-expr
36868 and size-expr may be:
36871 int-expr
36874 static tree
36875 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
36876 omp_clause_code kind,
36877 const char *str, tree list)
36879 const char *id = "num";
36880 cp_lexer *lexer = parser->lexer;
36881 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
36883 if (kind == OMP_CLAUSE_VECTOR)
36884 id = "length";
36886 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
36888 matching_parens parens;
36889 parens.consume_open (parser);
36893 cp_token *next = cp_lexer_peek_token (lexer);
36894 int idx = 0;
36896 /* Gang static argument. */
36897 if (kind == OMP_CLAUSE_GANG
36898 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
36900 cp_lexer_consume_token (lexer);
36902 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36903 goto cleanup_error;
36905 idx = 1;
36906 if (ops[idx] != NULL)
36908 cp_parser_error (parser, "too many %<static%> arguments");
36909 goto cleanup_error;
36912 /* Check for the '*' argument. */
36913 if (cp_lexer_next_token_is (lexer, CPP_MULT)
36914 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
36915 || cp_lexer_nth_token_is (parser->lexer, 2,
36916 CPP_CLOSE_PAREN)))
36918 cp_lexer_consume_token (lexer);
36919 ops[idx] = integer_minus_one_node;
36921 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
36923 cp_lexer_consume_token (lexer);
36924 continue;
36926 else break;
36929 /* Worker num: argument and vector length: arguments. */
36930 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
36931 && id_equal (next->u.value, id)
36932 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
36934 cp_lexer_consume_token (lexer); /* id */
36935 cp_lexer_consume_token (lexer); /* ':' */
36938 /* Now collect the actual argument. */
36939 if (ops[idx] != NULL_TREE)
36941 cp_parser_error (parser, "unexpected argument");
36942 goto cleanup_error;
36945 tree expr = cp_parser_assignment_expression (parser, NULL, false,
36946 false);
36947 if (expr == error_mark_node)
36948 goto cleanup_error;
36950 mark_exp_read (expr);
36951 ops[idx] = expr;
36953 if (kind == OMP_CLAUSE_GANG
36954 && cp_lexer_next_token_is (lexer, CPP_COMMA))
36956 cp_lexer_consume_token (lexer);
36957 continue;
36959 break;
36961 while (1);
36963 if (!parens.require_close (parser))
36964 goto cleanup_error;
36967 check_no_duplicate_clause (list, kind, str, loc);
36969 c = build_omp_clause (loc, kind);
36971 if (ops[1])
36972 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
36974 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
36975 OMP_CLAUSE_CHAIN (c) = list;
36977 return c;
36979 cleanup_error:
36980 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
36981 return list;
36984 /* OpenACC 2.0:
36985 tile ( size-expr-list ) */
36987 static tree
36988 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
36990 tree c, expr = error_mark_node;
36991 tree tile = NULL_TREE;
36993 /* Collapse and tile are mutually exclusive. (The spec doesn't say
36994 so, but the spec authors never considered such a case and have
36995 differing opinions on what it might mean, including 'not
36996 allowed'.) */
36997 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
36998 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
36999 clause_loc);
37001 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37002 return list;
37006 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
37007 return list;
37009 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
37010 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37011 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
37013 cp_lexer_consume_token (parser->lexer);
37014 expr = integer_zero_node;
37016 else
37017 expr = cp_parser_constant_expression (parser);
37019 tile = tree_cons (NULL_TREE, expr, tile);
37021 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
37023 /* Consume the trailing ')'. */
37024 cp_lexer_consume_token (parser->lexer);
37026 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
37027 tile = nreverse (tile);
37028 OMP_CLAUSE_TILE_LIST (c) = tile;
37029 OMP_CLAUSE_CHAIN (c) = list;
37030 return c;
37033 /* OpenACC 2.0
37034 Parse wait clause or directive parameters. */
37036 static tree
37037 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
37039 vec<tree, va_gc> *args;
37040 tree t, args_tree;
37042 args = cp_parser_parenthesized_expression_list (parser, non_attr,
37043 /*cast_p=*/false,
37044 /*allow_expansion_p=*/true,
37045 /*non_constant_p=*/NULL);
37047 if (args == NULL || args->length () == 0)
37049 if (args != NULL)
37051 cp_parser_error (parser, "expected integer expression list");
37052 release_tree_vector (args);
37054 return list;
37057 args_tree = build_tree_list_vec (args);
37059 release_tree_vector (args);
37061 for (t = args_tree; t; t = TREE_CHAIN (t))
37063 tree targ = TREE_VALUE (t);
37065 if (targ != error_mark_node)
37067 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
37068 error ("%<wait%> expression must be integral");
37069 else
37071 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
37073 targ = mark_rvalue_use (targ);
37074 OMP_CLAUSE_DECL (c) = targ;
37075 OMP_CLAUSE_CHAIN (c) = list;
37076 list = c;
37081 return list;
37084 /* OpenACC:
37085 wait [( int-expr-list )] */
37087 static tree
37088 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
37090 location_t location = cp_lexer_peek_token (parser->lexer)->location;
37092 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37093 list = cp_parser_oacc_wait_list (parser, location, list);
37094 else
37096 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
37098 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37099 OMP_CLAUSE_CHAIN (c) = list;
37100 list = c;
37103 return list;
37106 /* OpenMP 3.0:
37107 collapse ( constant-expression ) */
37109 static tree
37110 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
37112 tree c, num;
37113 location_t loc;
37114 HOST_WIDE_INT n;
37116 loc = cp_lexer_peek_token (parser->lexer)->location;
37117 matching_parens parens;
37118 if (!parens.require_open (parser))
37119 return list;
37121 num = cp_parser_constant_expression (parser);
37123 if (!parens.require_close (parser))
37124 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37125 /*or_comma=*/false,
37126 /*consume_paren=*/true);
37128 if (num == error_mark_node)
37129 return list;
37130 num = fold_non_dependent_expr (num);
37131 if (!tree_fits_shwi_p (num)
37132 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
37133 || (n = tree_to_shwi (num)) <= 0
37134 || (int) n != n)
37136 error_at (loc, "collapse argument needs positive constant integer expression");
37137 return list;
37140 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
37141 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
37142 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
37143 OMP_CLAUSE_CHAIN (c) = list;
37144 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
37146 return c;
37149 /* OpenMP 2.5:
37150 default ( none | shared )
37152 OpenMP 5.1:
37153 default ( private | firstprivate )
37155 OpenACC:
37156 default ( none | present ) */
37158 static tree
37159 cp_parser_omp_clause_default (cp_parser *parser, tree list,
37160 location_t location, bool is_oacc)
37162 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
37163 tree c;
37165 matching_parens parens;
37166 if (!parens.require_open (parser))
37167 return list;
37168 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37170 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
37171 cp_lexer_consume_token (parser->lexer);
37173 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37175 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37176 const char *p = IDENTIFIER_POINTER (id);
37178 switch (p[0])
37180 case 'n':
37181 if (strcmp ("none", p) != 0)
37182 goto invalid_kind;
37183 kind = OMP_CLAUSE_DEFAULT_NONE;
37184 break;
37186 case 'p':
37187 if (strcmp ("present", p) != 0 || !is_oacc)
37188 goto invalid_kind;
37189 kind = OMP_CLAUSE_DEFAULT_PRESENT;
37190 break;
37192 case 'f':
37193 if (strcmp ("firstprivate", p) != 0 || is_oacc)
37194 goto invalid_kind;
37195 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
37196 break;
37198 case 's':
37199 if (strcmp ("shared", p) != 0 || is_oacc)
37200 goto invalid_kind;
37201 kind = OMP_CLAUSE_DEFAULT_SHARED;
37202 break;
37204 default:
37205 goto invalid_kind;
37208 cp_lexer_consume_token (parser->lexer);
37210 else
37212 invalid_kind:
37213 if (is_oacc)
37214 cp_parser_error (parser, "expected %<none%> or %<present%>");
37215 else
37216 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
37217 "%<private%> or %<firstprivate%>");
37220 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
37221 || !parens.require_close (parser))
37222 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37223 /*or_comma=*/false,
37224 /*consume_paren=*/true);
37226 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
37227 return list;
37229 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
37230 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
37231 OMP_CLAUSE_CHAIN (c) = list;
37232 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
37234 return c;
37237 /* OpenMP 3.1:
37238 final ( expression ) */
37240 static tree
37241 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
37243 tree t, c;
37245 matching_parens parens;
37246 if (!parens.require_open (parser))
37247 return list;
37249 t = cp_parser_assignment_expression (parser);
37251 if (t == error_mark_node
37252 || !parens.require_close (parser))
37253 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37254 /*or_comma=*/false,
37255 /*consume_paren=*/true);
37257 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
37259 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
37260 OMP_CLAUSE_FINAL_EXPR (c) = t;
37261 OMP_CLAUSE_CHAIN (c) = list;
37263 return c;
37266 /* OpenMP 2.5:
37267 if ( expression )
37269 OpenMP 4.5:
37270 if ( directive-name-modifier : expression )
37272 directive-name-modifier:
37273 parallel | task | taskloop | target data | target | target update
37274 | target enter data | target exit data
37276 OpenMP 5.0:
37277 directive-name-modifier:
37278 ... | simd | cancel */
37280 static tree
37281 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
37282 bool is_omp)
37284 tree t, c;
37285 enum tree_code if_modifier = ERROR_MARK;
37287 matching_parens parens;
37288 if (!parens.require_open (parser))
37289 return list;
37291 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37293 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37294 const char *p = IDENTIFIER_POINTER (id);
37295 int n = 2;
37297 if (strcmp ("cancel", p) == 0)
37298 if_modifier = VOID_CST;
37299 else if (strcmp ("parallel", p) == 0)
37300 if_modifier = OMP_PARALLEL;
37301 else if (strcmp ("simd", p) == 0)
37302 if_modifier = OMP_SIMD;
37303 else if (strcmp ("task", p) == 0)
37304 if_modifier = OMP_TASK;
37305 else if (strcmp ("taskloop", p) == 0)
37306 if_modifier = OMP_TASKLOOP;
37307 else if (strcmp ("target", p) == 0)
37309 if_modifier = OMP_TARGET;
37310 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
37312 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
37313 p = IDENTIFIER_POINTER (id);
37314 if (strcmp ("data", p) == 0)
37315 if_modifier = OMP_TARGET_DATA;
37316 else if (strcmp ("update", p) == 0)
37317 if_modifier = OMP_TARGET_UPDATE;
37318 else if (strcmp ("enter", p) == 0)
37319 if_modifier = OMP_TARGET_ENTER_DATA;
37320 else if (strcmp ("exit", p) == 0)
37321 if_modifier = OMP_TARGET_EXIT_DATA;
37322 if (if_modifier != OMP_TARGET)
37323 n = 3;
37324 else
37326 location_t loc
37327 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
37328 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
37329 "or %<exit%>");
37330 if_modifier = ERROR_MARK;
37332 if (if_modifier == OMP_TARGET_ENTER_DATA
37333 || if_modifier == OMP_TARGET_EXIT_DATA)
37335 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
37337 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
37338 p = IDENTIFIER_POINTER (id);
37339 if (strcmp ("data", p) == 0)
37340 n = 4;
37342 if (n != 4)
37344 location_t loc
37345 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
37346 error_at (loc, "expected %<data%>");
37347 if_modifier = ERROR_MARK;
37352 if (if_modifier != ERROR_MARK)
37354 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
37356 while (n-- > 0)
37357 cp_lexer_consume_token (parser->lexer);
37359 else
37361 if (n > 2)
37363 location_t loc
37364 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
37365 error_at (loc, "expected %<:%>");
37367 if_modifier = ERROR_MARK;
37372 t = cp_parser_assignment_expression (parser);
37374 if (t == error_mark_node
37375 || !parens.require_close (parser))
37376 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37377 /*or_comma=*/false,
37378 /*consume_paren=*/true);
37380 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
37381 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
37383 if (if_modifier != ERROR_MARK
37384 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37386 const char *p = NULL;
37387 switch (if_modifier)
37389 case VOID_CST: p = "cancel"; break;
37390 case OMP_PARALLEL: p = "parallel"; break;
37391 case OMP_SIMD: p = "simd"; break;
37392 case OMP_TASK: p = "task"; break;
37393 case OMP_TASKLOOP: p = "taskloop"; break;
37394 case OMP_TARGET_DATA: p = "target data"; break;
37395 case OMP_TARGET: p = "target"; break;
37396 case OMP_TARGET_UPDATE: p = "target update"; break;
37397 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
37398 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
37399 default: gcc_unreachable ();
37401 error_at (location, "too many %<if%> clauses with %qs modifier",
37403 return list;
37405 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37407 if (!is_omp)
37408 error_at (location, "too many %<if%> clauses");
37409 else
37410 error_at (location, "too many %<if%> clauses without modifier");
37411 return list;
37413 else if (if_modifier == ERROR_MARK
37414 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
37416 error_at (location, "if any %<if%> clause has modifier, then all "
37417 "%<if%> clauses have to use modifier");
37418 return list;
37422 c = build_omp_clause (location, OMP_CLAUSE_IF);
37423 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
37424 OMP_CLAUSE_IF_EXPR (c) = t;
37425 OMP_CLAUSE_CHAIN (c) = list;
37427 return c;
37430 /* OpenMP 3.1:
37431 mergeable */
37433 static tree
37434 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
37435 tree list, location_t location)
37437 tree c;
37439 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
37440 location);
37442 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
37443 OMP_CLAUSE_CHAIN (c) = list;
37444 return c;
37447 /* OpenMP 2.5:
37448 nowait */
37450 static tree
37451 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
37452 tree list, location_t location)
37454 tree c;
37456 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
37458 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
37459 OMP_CLAUSE_CHAIN (c) = list;
37460 return c;
37463 /* OpenMP 2.5:
37464 num_threads ( expression ) */
37466 static tree
37467 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
37468 location_t location)
37470 tree t, c;
37472 matching_parens parens;
37473 if (!parens.require_open (parser))
37474 return list;
37476 t = cp_parser_assignment_expression (parser);
37478 if (t == error_mark_node
37479 || !parens.require_close (parser))
37480 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37481 /*or_comma=*/false,
37482 /*consume_paren=*/true);
37484 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
37485 "num_threads", location);
37487 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
37488 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
37489 OMP_CLAUSE_CHAIN (c) = list;
37491 return c;
37494 /* OpenMP 4.5:
37495 num_tasks ( expression )
37497 OpenMP 5.1:
37498 num_tasks ( strict : expression ) */
37500 static tree
37501 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
37502 location_t location)
37504 tree t, c;
37506 matching_parens parens;
37507 if (!parens.require_open (parser))
37508 return list;
37510 bool strict = false;
37511 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37512 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37514 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37515 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37517 strict = true;
37518 cp_lexer_consume_token (parser->lexer);
37519 cp_lexer_consume_token (parser->lexer);
37523 t = cp_parser_assignment_expression (parser);
37525 if (t == error_mark_node
37526 || !parens.require_close (parser))
37527 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37528 /*or_comma=*/false,
37529 /*consume_paren=*/true);
37531 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
37532 "num_tasks", location);
37534 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
37535 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
37536 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
37537 OMP_CLAUSE_CHAIN (c) = list;
37539 return c;
37542 /* OpenMP 4.5:
37543 grainsize ( expression )
37545 OpenMP 5.1:
37546 grainsize ( strict : expression ) */
37548 static tree
37549 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
37550 location_t location)
37552 tree t, c;
37554 matching_parens parens;
37555 if (!parens.require_open (parser))
37556 return list;
37558 bool strict = false;
37559 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37560 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37562 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37563 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37565 strict = true;
37566 cp_lexer_consume_token (parser->lexer);
37567 cp_lexer_consume_token (parser->lexer);
37571 t = cp_parser_assignment_expression (parser);
37573 if (t == error_mark_node
37574 || !parens.require_close (parser))
37575 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37576 /*or_comma=*/false,
37577 /*consume_paren=*/true);
37579 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
37580 "grainsize", location);
37582 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
37583 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
37584 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
37585 OMP_CLAUSE_CHAIN (c) = list;
37587 return c;
37590 /* OpenMP 4.5:
37591 priority ( expression ) */
37593 static tree
37594 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
37595 location_t location)
37597 tree t, c;
37599 matching_parens parens;
37600 if (!parens.require_open (parser))
37601 return list;
37603 t = cp_parser_assignment_expression (parser);
37605 if (t == error_mark_node
37606 || !parens.require_close (parser))
37607 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37608 /*or_comma=*/false,
37609 /*consume_paren=*/true);
37611 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
37612 "priority", location);
37614 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
37615 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
37616 OMP_CLAUSE_CHAIN (c) = list;
37618 return c;
37621 /* OpenMP 4.5:
37622 hint ( expression ) */
37624 static tree
37625 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
37627 tree t, c;
37629 matching_parens parens;
37630 if (!parens.require_open (parser))
37631 return list;
37633 t = cp_parser_assignment_expression (parser);
37635 if (t != error_mark_node)
37637 t = fold_non_dependent_expr (t);
37638 if (!value_dependent_expression_p (t)
37639 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
37640 || !tree_fits_shwi_p (t)
37641 || tree_int_cst_sgn (t) == -1))
37642 error_at (location, "expected constant integer expression with "
37643 "valid sync-hint value");
37645 if (t == error_mark_node
37646 || !parens.require_close (parser))
37647 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37648 /*or_comma=*/false,
37649 /*consume_paren=*/true);
37650 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
37652 c = build_omp_clause (location, OMP_CLAUSE_HINT);
37653 OMP_CLAUSE_HINT_EXPR (c) = t;
37654 OMP_CLAUSE_CHAIN (c) = list;
37656 return c;
37659 /* OpenMP 5.1:
37660 filter ( integer-expression ) */
37662 static tree
37663 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
37665 tree t, c;
37667 matching_parens parens;
37668 if (!parens.require_open (parser))
37669 return list;
37671 t = cp_parser_assignment_expression (parser);
37673 if (t == error_mark_node
37674 || !parens.require_close (parser))
37675 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37676 /*or_comma=*/false,
37677 /*consume_paren=*/true);
37678 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
37680 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
37681 OMP_CLAUSE_FILTER_EXPR (c) = t;
37682 OMP_CLAUSE_CHAIN (c) = list;
37684 return c;
37687 /* OpenMP 4.5:
37688 defaultmap ( tofrom : scalar )
37690 OpenMP 5.0:
37691 defaultmap ( implicit-behavior [ : variable-category ] ) */
37693 static tree
37694 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
37695 location_t location)
37697 tree c, id;
37698 const char *p;
37699 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
37700 enum omp_clause_defaultmap_kind category
37701 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
37703 matching_parens parens;
37704 if (!parens.require_open (parser))
37705 return list;
37707 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37708 p = "default";
37709 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37711 invalid_behavior:
37712 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
37713 "%<tofrom%>, %<firstprivate%>, %<none%> "
37714 "or %<default%>");
37715 goto out_err;
37717 else
37719 id = cp_lexer_peek_token (parser->lexer)->u.value;
37720 p = IDENTIFIER_POINTER (id);
37723 switch (p[0])
37725 case 'a':
37726 if (strcmp ("alloc", p) == 0)
37727 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
37728 else
37729 goto invalid_behavior;
37730 break;
37732 case 'd':
37733 if (strcmp ("default", p) == 0)
37734 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
37735 else
37736 goto invalid_behavior;
37737 break;
37739 case 'f':
37740 if (strcmp ("firstprivate", p) == 0)
37741 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
37742 else if (strcmp ("from", p) == 0)
37743 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
37744 else
37745 goto invalid_behavior;
37746 break;
37748 case 'n':
37749 if (strcmp ("none", p) == 0)
37750 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
37751 else
37752 goto invalid_behavior;
37753 break;
37755 case 't':
37756 if (strcmp ("tofrom", p) == 0)
37757 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
37758 else if (strcmp ("to", p) == 0)
37759 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
37760 else
37761 goto invalid_behavior;
37762 break;
37764 default:
37765 goto invalid_behavior;
37767 cp_lexer_consume_token (parser->lexer);
37769 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37771 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37772 goto out_err;
37774 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37776 invalid_category:
37777 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
37778 "%<pointer%>");
37779 goto out_err;
37781 id = cp_lexer_peek_token (parser->lexer)->u.value;
37782 p = IDENTIFIER_POINTER (id);
37784 switch (p[0])
37786 case 'a':
37787 if (strcmp ("aggregate", p) == 0)
37788 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
37789 else
37790 goto invalid_category;
37791 break;
37793 case 'p':
37794 if (strcmp ("pointer", p) == 0)
37795 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
37796 else
37797 goto invalid_category;
37798 break;
37800 case 's':
37801 if (strcmp ("scalar", p) == 0)
37802 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
37803 else
37804 goto invalid_category;
37805 break;
37807 default:
37808 goto invalid_category;
37811 cp_lexer_consume_token (parser->lexer);
37813 if (!parens.require_close (parser))
37814 goto out_err;
37816 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
37817 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
37818 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
37819 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
37820 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
37821 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
37823 enum omp_clause_defaultmap_kind cat = category;
37824 location_t loc = OMP_CLAUSE_LOCATION (c);
37825 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
37826 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
37827 p = NULL;
37828 switch (cat)
37830 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
37831 p = NULL;
37832 break;
37833 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
37834 p = "aggregate";
37835 break;
37836 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
37837 p = "pointer";
37838 break;
37839 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
37840 p = "scalar";
37841 break;
37842 default:
37843 gcc_unreachable ();
37845 if (p)
37846 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
37848 else
37849 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
37850 "category");
37851 break;
37854 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
37855 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
37856 OMP_CLAUSE_CHAIN (c) = list;
37857 return c;
37859 out_err:
37860 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37861 /*or_comma=*/false,
37862 /*consume_paren=*/true);
37863 return list;
37866 /* OpenMP 5.0:
37867 order ( concurrent )
37869 OpenMP 5.1:
37870 order ( order-modifier : concurrent )
37872 order-modifier:
37873 reproducible
37874 unconstrained */
37876 static tree
37877 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
37879 tree c, id;
37880 const char *p;
37881 bool unconstrained = false;
37882 bool reproducible = false;
37884 matching_parens parens;
37885 if (!parens.require_open (parser))
37886 return list;
37888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37889 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37891 id = cp_lexer_peek_token (parser->lexer)->u.value;
37892 p = IDENTIFIER_POINTER (id);
37893 if (strcmp (p, "unconstrained") == 0)
37894 unconstrained = true;
37895 else if (strcmp (p, "reproducible") == 0)
37896 reproducible = true;
37897 else
37899 cp_parser_error (parser, "expected %<reproducible%> or "
37900 "%<unconstrained%>");
37901 goto out_err;
37903 cp_lexer_consume_token (parser->lexer);
37904 cp_lexer_consume_token (parser->lexer);
37906 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37908 cp_parser_error (parser, "expected %<concurrent%>");
37909 goto out_err;
37911 else
37913 id = cp_lexer_peek_token (parser->lexer)->u.value;
37914 p = IDENTIFIER_POINTER (id);
37916 if (strcmp (p, "concurrent") != 0)
37918 cp_parser_error (parser, "expected %<concurrent%>");
37919 goto out_err;
37921 cp_lexer_consume_token (parser->lexer);
37922 if (!parens.require_close (parser))
37923 goto out_err;
37925 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
37926 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
37927 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
37928 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
37929 OMP_CLAUSE_CHAIN (c) = list;
37930 return c;
37932 out_err:
37933 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37934 /*or_comma=*/false,
37935 /*consume_paren=*/true);
37936 return list;
37939 /* OpenMP 5.0:
37940 bind ( teams | parallel | thread ) */
37942 static tree
37943 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
37944 location_t location)
37946 tree c;
37947 const char *p;
37948 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
37950 matching_parens parens;
37951 if (!parens.require_open (parser))
37952 return list;
37954 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37956 invalid:
37957 cp_parser_error (parser,
37958 "expected %<teams%>, %<parallel%> or %<thread%>");
37959 goto out_err;
37961 else
37963 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37964 p = IDENTIFIER_POINTER (id);
37966 if (strcmp (p, "teams") == 0)
37967 kind = OMP_CLAUSE_BIND_TEAMS;
37968 else if (strcmp (p, "parallel") == 0)
37969 kind = OMP_CLAUSE_BIND_PARALLEL;
37970 else if (strcmp (p, "thread") != 0)
37971 goto invalid;
37972 cp_lexer_consume_token (parser->lexer);
37973 if (!parens.require_close (parser))
37974 goto out_err;
37976 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
37977 c = build_omp_clause (location, OMP_CLAUSE_BIND);
37978 OMP_CLAUSE_BIND_KIND (c) = kind;
37979 OMP_CLAUSE_CHAIN (c) = list;
37980 return c;
37982 out_err:
37983 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37984 /*or_comma=*/false,
37985 /*consume_paren=*/true);
37986 return list;
37989 /* OpenMP 2.5:
37990 ordered
37992 OpenMP 4.5:
37993 ordered ( constant-expression ) */
37995 static tree
37996 cp_parser_omp_clause_ordered (cp_parser *parser,
37997 tree list, location_t location)
37999 tree c, num = NULL_TREE;
38000 HOST_WIDE_INT n;
38002 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
38003 "ordered", location);
38005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38007 matching_parens parens;
38008 parens.consume_open (parser);
38010 num = cp_parser_constant_expression (parser);
38012 if (!parens.require_close (parser))
38013 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38014 /*or_comma=*/false,
38015 /*consume_paren=*/true);
38017 if (num == error_mark_node)
38018 return list;
38019 num = fold_non_dependent_expr (num);
38020 if (!tree_fits_shwi_p (num)
38021 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38022 || (n = tree_to_shwi (num)) <= 0
38023 || (int) n != n)
38025 error_at (location,
38026 "ordered argument needs positive constant integer "
38027 "expression");
38028 return list;
38032 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
38033 OMP_CLAUSE_ORDERED_EXPR (c) = num;
38034 OMP_CLAUSE_CHAIN (c) = list;
38035 return c;
38038 /* OpenMP 2.5:
38039 reduction ( reduction-operator : variable-list )
38041 reduction-operator:
38042 One of: + * - & ^ | && ||
38044 OpenMP 3.1:
38046 reduction-operator:
38047 One of: + * - & ^ | && || min max
38049 OpenMP 4.0:
38051 reduction-operator:
38052 One of: + * - & ^ | && ||
38053 id-expression
38055 OpenMP 5.0:
38056 reduction ( reduction-modifier, reduction-operator : variable-list )
38057 in_reduction ( reduction-operator : variable-list )
38058 task_reduction ( reduction-operator : variable-list ) */
38060 static tree
38061 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
38062 bool is_omp, tree list)
38064 enum tree_code code = ERROR_MARK;
38065 tree nlist, c, id = NULL_TREE;
38066 bool task = false;
38067 bool inscan = false;
38069 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38070 return list;
38072 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
38074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
38075 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38077 cp_lexer_consume_token (parser->lexer);
38078 cp_lexer_consume_token (parser->lexer);
38080 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38081 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38083 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38084 const char *p = IDENTIFIER_POINTER (id);
38085 if (strcmp (p, "task") == 0)
38086 task = true;
38087 else if (strcmp (p, "inscan") == 0)
38088 inscan = true;
38089 if (task || inscan)
38091 cp_lexer_consume_token (parser->lexer);
38092 cp_lexer_consume_token (parser->lexer);
38097 switch (cp_lexer_peek_token (parser->lexer)->type)
38099 case CPP_PLUS: code = PLUS_EXPR; break;
38100 case CPP_MULT: code = MULT_EXPR; break;
38101 case CPP_MINUS: code = MINUS_EXPR; break;
38102 case CPP_AND: code = BIT_AND_EXPR; break;
38103 case CPP_XOR: code = BIT_XOR_EXPR; break;
38104 case CPP_OR: code = BIT_IOR_EXPR; break;
38105 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
38106 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
38107 default: break;
38110 if (code != ERROR_MARK)
38111 cp_lexer_consume_token (parser->lexer);
38112 else
38114 bool saved_colon_corrects_to_scope_p;
38115 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38116 parser->colon_corrects_to_scope_p = false;
38117 id = cp_parser_id_expression (parser, /*template_p=*/false,
38118 /*check_dependency_p=*/true,
38119 /*template_p=*/NULL,
38120 /*declarator_p=*/false,
38121 /*optional_p=*/false);
38122 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38123 if (identifier_p (id))
38125 const char *p = IDENTIFIER_POINTER (id);
38127 if (strcmp (p, "min") == 0)
38128 code = MIN_EXPR;
38129 else if (strcmp (p, "max") == 0)
38130 code = MAX_EXPR;
38131 else if (id == ovl_op_identifier (false, PLUS_EXPR))
38132 code = PLUS_EXPR;
38133 else if (id == ovl_op_identifier (false, MULT_EXPR))
38134 code = MULT_EXPR;
38135 else if (id == ovl_op_identifier (false, MINUS_EXPR))
38136 code = MINUS_EXPR;
38137 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
38138 code = BIT_AND_EXPR;
38139 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
38140 code = BIT_IOR_EXPR;
38141 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
38142 code = BIT_XOR_EXPR;
38143 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
38144 code = TRUTH_ANDIF_EXPR;
38145 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
38146 code = TRUTH_ORIF_EXPR;
38147 id = omp_reduction_id (code, id, NULL_TREE);
38148 tree scope = parser->scope;
38149 if (scope)
38150 id = build_qualified_name (NULL_TREE, scope, id, false);
38151 parser->scope = NULL_TREE;
38152 parser->qualifying_scope = NULL_TREE;
38153 parser->object_scope = NULL_TREE;
38155 else
38157 error ("invalid reduction-identifier");
38158 resync_fail:
38159 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38160 /*or_comma=*/false,
38161 /*consume_paren=*/true);
38162 return list;
38166 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38167 goto resync_fail;
38169 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
38170 NULL);
38171 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38173 OMP_CLAUSE_REDUCTION_CODE (c) = code;
38174 if (task)
38175 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
38176 else if (inscan)
38177 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
38178 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
38181 return nlist;
38184 /* OpenMP 2.5:
38185 schedule ( schedule-kind )
38186 schedule ( schedule-kind , expression )
38188 schedule-kind:
38189 static | dynamic | guided | runtime | auto
38191 OpenMP 4.5:
38192 schedule ( schedule-modifier : schedule-kind )
38193 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
38195 schedule-modifier:
38196 simd
38197 monotonic
38198 nonmonotonic */
38200 static tree
38201 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
38203 tree c, t;
38204 int modifiers = 0, nmodifiers = 0;
38206 matching_parens parens;
38207 if (!parens.require_open (parser))
38208 return list;
38210 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
38212 location_t comma = UNKNOWN_LOCATION;
38213 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38215 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38216 const char *p = IDENTIFIER_POINTER (id);
38217 if (strcmp ("simd", p) == 0)
38218 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
38219 else if (strcmp ("monotonic", p) == 0)
38220 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
38221 else if (strcmp ("nonmonotonic", p) == 0)
38222 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
38223 else
38224 break;
38225 comma = UNKNOWN_LOCATION;
38226 cp_lexer_consume_token (parser->lexer);
38227 if (nmodifiers++ == 0
38228 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38230 comma = cp_lexer_peek_token (parser->lexer)->location;
38231 cp_lexer_consume_token (parser->lexer);
38233 else
38235 cp_parser_require (parser, CPP_COLON, RT_COLON);
38236 break;
38239 if (comma != UNKNOWN_LOCATION)
38240 error_at (comma, "expected %<:%>");
38242 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38244 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38245 const char *p = IDENTIFIER_POINTER (id);
38247 switch (p[0])
38249 case 'd':
38250 if (strcmp ("dynamic", p) != 0)
38251 goto invalid_kind;
38252 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
38253 break;
38255 case 'g':
38256 if (strcmp ("guided", p) != 0)
38257 goto invalid_kind;
38258 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
38259 break;
38261 case 'r':
38262 if (strcmp ("runtime", p) != 0)
38263 goto invalid_kind;
38264 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
38265 break;
38267 default:
38268 goto invalid_kind;
38271 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
38272 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
38273 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
38274 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
38275 else
38276 goto invalid_kind;
38277 cp_lexer_consume_token (parser->lexer);
38279 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
38280 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38281 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
38282 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38284 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
38285 "specified");
38286 modifiers = 0;
38289 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38291 cp_token *token;
38292 cp_lexer_consume_token (parser->lexer);
38294 token = cp_lexer_peek_token (parser->lexer);
38295 t = cp_parser_assignment_expression (parser);
38297 if (t == error_mark_node)
38298 goto resync_fail;
38299 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
38300 error_at (token->location, "schedule %<runtime%> does not take "
38301 "a %<chunk_size%> parameter");
38302 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
38303 error_at (token->location, "schedule %<auto%> does not take "
38304 "a %<chunk_size%> parameter");
38305 else
38306 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
38308 if (!parens.require_close (parser))
38309 goto resync_fail;
38311 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
38312 goto resync_fail;
38314 OMP_CLAUSE_SCHEDULE_KIND (c)
38315 = (enum omp_clause_schedule_kind)
38316 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
38318 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
38319 OMP_CLAUSE_CHAIN (c) = list;
38320 return c;
38322 invalid_kind:
38323 cp_parser_error (parser, "invalid schedule kind");
38324 resync_fail:
38325 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38326 /*or_comma=*/false,
38327 /*consume_paren=*/true);
38328 return list;
38331 /* OpenMP 3.0:
38332 untied */
38334 static tree
38335 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
38336 tree list, location_t location)
38338 tree c;
38340 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
38342 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
38343 OMP_CLAUSE_CHAIN (c) = list;
38344 return c;
38347 /* OpenMP 4.0:
38348 inbranch
38349 notinbranch */
38351 static tree
38352 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
38353 tree list, location_t location)
38355 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38356 tree c = build_omp_clause (location, code);
38357 OMP_CLAUSE_CHAIN (c) = list;
38358 return c;
38361 /* OpenMP 4.0:
38362 parallel
38364 sections
38365 taskgroup */
38367 static tree
38368 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
38369 enum omp_clause_code code,
38370 tree list, location_t location)
38372 tree c = build_omp_clause (location, code);
38373 OMP_CLAUSE_CHAIN (c) = list;
38374 return c;
38377 /* OpenMP 4.5:
38378 nogroup */
38380 static tree
38381 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
38382 tree list, location_t location)
38384 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
38385 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
38386 OMP_CLAUSE_CHAIN (c) = list;
38387 return c;
38390 /* OpenMP 4.5:
38391 simd
38392 threads */
38394 static tree
38395 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
38396 enum omp_clause_code code,
38397 tree list, location_t location)
38399 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38400 tree c = build_omp_clause (location, code);
38401 OMP_CLAUSE_CHAIN (c) = list;
38402 return c;
38405 /* OpenMP 4.0:
38406 num_teams ( expression )
38408 OpenMP 5.1:
38409 num_teams ( expression : expression ) */
38411 static tree
38412 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
38413 location_t location)
38415 tree upper, lower = NULL_TREE, c;
38417 matching_parens parens;
38418 if (!parens.require_open (parser))
38419 return list;
38421 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38422 parser->colon_corrects_to_scope_p = false;
38423 upper = cp_parser_assignment_expression (parser);
38424 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38426 if (upper != error_mark_node
38427 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38429 lower = upper;
38430 cp_lexer_consume_token (parser->lexer);
38431 upper = cp_parser_assignment_expression (parser);
38434 if (upper == error_mark_node
38435 || !parens.require_close (parser))
38436 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38437 /*or_comma=*/false,
38438 /*consume_paren=*/true);
38440 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
38441 "num_teams", location);
38443 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
38444 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
38445 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
38446 OMP_CLAUSE_CHAIN (c) = list;
38448 return c;
38451 /* OpenMP 4.0:
38452 thread_limit ( expression ) */
38454 static tree
38455 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
38456 location_t location)
38458 tree t, c;
38460 matching_parens parens;
38461 if (!parens.require_open (parser))
38462 return list;
38464 t = cp_parser_assignment_expression (parser);
38466 if (t == error_mark_node
38467 || !parens.require_close (parser))
38468 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38469 /*or_comma=*/false,
38470 /*consume_paren=*/true);
38472 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
38473 "thread_limit", location);
38475 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
38476 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
38477 OMP_CLAUSE_CHAIN (c) = list;
38479 return c;
38482 /* OpenMP 4.0:
38483 aligned ( variable-list )
38484 aligned ( variable-list : constant-expression ) */
38486 static tree
38487 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
38489 tree nlist, c, alignment = NULL_TREE;
38490 bool colon;
38492 matching_parens parens;
38493 if (!parens.require_open (parser))
38494 return list;
38496 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
38497 &colon);
38499 if (colon)
38501 alignment = cp_parser_constant_expression (parser);
38503 if (!parens.require_close (parser))
38504 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38505 /*or_comma=*/false,
38506 /*consume_paren=*/true);
38508 if (alignment == error_mark_node)
38509 alignment = NULL_TREE;
38512 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38513 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
38515 return nlist;
38518 /* OpenMP 5.0:
38519 allocate ( variable-list )
38520 allocate ( expression : variable-list )
38522 OpenMP 5.1:
38523 allocate ( allocator-modifier : variable-list )
38524 allocate ( allocator-modifier , allocator-modifier : variable-list )
38526 allocator-modifier:
38527 allocator ( expression )
38528 align ( expression ) */
38530 static tree
38531 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
38533 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
38534 bool colon, has_modifiers = false;
38536 matching_parens parens;
38537 if (!parens.require_open (parser))
38538 return list;
38540 cp_parser_parse_tentatively (parser);
38541 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38542 parser->colon_corrects_to_scope_p = false;
38543 for (int mod = 0; mod < 2; mod++)
38544 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38545 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38547 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38548 const char *p = IDENTIFIER_POINTER (id);
38549 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
38550 break;
38551 cp_lexer_consume_token (parser->lexer);
38552 matching_parens parens2;
38553 if (!parens2.require_open (parser))
38554 break;
38555 if (strcmp (p, "allocator") == 0)
38557 if (allocator != NULL_TREE)
38558 break;
38559 allocator = cp_parser_assignment_expression (parser);
38561 else
38563 if (align != NULL_TREE)
38564 break;
38565 align = cp_parser_assignment_expression (parser);
38567 if (!parens2.require_close (parser))
38568 break;
38569 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38571 has_modifiers = true;
38572 break;
38574 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38575 break;
38576 cp_lexer_consume_token (parser->lexer);
38578 else
38579 break;
38580 if (!has_modifiers)
38582 cp_parser_abort_tentative_parse (parser);
38583 align = NULL_TREE;
38584 allocator = NULL_TREE;
38585 cp_parser_parse_tentatively (parser);
38586 allocator = cp_parser_assignment_expression (parser);
38588 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38589 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38591 cp_parser_parse_definitely (parser);
38592 cp_lexer_consume_token (parser->lexer);
38593 if (allocator == error_mark_node)
38594 allocator = NULL_TREE;
38595 if (align == error_mark_node)
38596 align = NULL_TREE;
38598 else
38600 cp_parser_abort_tentative_parse (parser);
38601 allocator = NULL_TREE;
38602 align = NULL_TREE;
38605 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
38606 &colon);
38608 if (allocator || align)
38609 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38611 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
38612 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
38615 return nlist;
38618 /* OpenMP 2.5:
38619 lastprivate ( variable-list )
38621 OpenMP 5.0:
38622 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
38624 static tree
38625 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
38627 bool conditional = false;
38629 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38630 return list;
38632 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38633 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38635 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38636 const char *p = IDENTIFIER_POINTER (id);
38638 if (strcmp ("conditional", p) == 0)
38640 conditional = true;
38641 cp_lexer_consume_token (parser->lexer);
38642 cp_lexer_consume_token (parser->lexer);
38646 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
38647 list, NULL);
38649 if (conditional)
38650 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38651 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
38652 return nlist;
38655 /* OpenMP 4.0:
38656 linear ( variable-list )
38657 linear ( variable-list : expression )
38659 OpenMP 4.5:
38660 linear ( modifier ( variable-list ) )
38661 linear ( modifier ( variable-list ) : expression ) */
38663 static tree
38664 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
38665 bool declare_simd)
38667 tree nlist, c, step = integer_one_node;
38668 bool colon;
38669 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
38671 matching_parens parens;
38672 if (!parens.require_open (parser))
38673 return list;
38675 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38677 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38678 const char *p = IDENTIFIER_POINTER (id);
38680 if (strcmp ("ref", p) == 0)
38681 kind = OMP_CLAUSE_LINEAR_REF;
38682 else if (strcmp ("val", p) == 0)
38683 kind = OMP_CLAUSE_LINEAR_VAL;
38684 else if (strcmp ("uval", p) == 0)
38685 kind = OMP_CLAUSE_LINEAR_UVAL;
38686 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38687 cp_lexer_consume_token (parser->lexer);
38688 else
38689 kind = OMP_CLAUSE_LINEAR_DEFAULT;
38692 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
38693 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
38694 &colon);
38695 else
38697 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
38698 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
38699 if (colon)
38700 cp_parser_require (parser, CPP_COLON, RT_COLON);
38701 else if (!parens.require_close (parser))
38702 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38703 /*or_comma=*/false,
38704 /*consume_paren=*/true);
38707 if (colon)
38709 step = NULL_TREE;
38710 if (declare_simd
38711 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38712 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
38714 cp_token *token = cp_lexer_peek_token (parser->lexer);
38715 cp_parser_parse_tentatively (parser);
38716 step = cp_parser_id_expression (parser, /*template_p=*/false,
38717 /*check_dependency_p=*/true,
38718 /*template_p=*/NULL,
38719 /*declarator_p=*/false,
38720 /*optional_p=*/false);
38721 if (step != error_mark_node)
38722 step = cp_parser_lookup_name_simple (parser, step, token->location);
38723 if (step == error_mark_node)
38725 step = NULL_TREE;
38726 cp_parser_abort_tentative_parse (parser);
38728 else if (!cp_parser_parse_definitely (parser))
38729 step = NULL_TREE;
38731 if (!step)
38732 step = cp_parser_assignment_expression (parser);
38734 if (!parens.require_close (parser))
38735 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38736 /*or_comma=*/false,
38737 /*consume_paren=*/true);
38739 if (step == error_mark_node)
38740 return list;
38743 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38745 OMP_CLAUSE_LINEAR_STEP (c) = step;
38746 OMP_CLAUSE_LINEAR_KIND (c) = kind;
38749 return nlist;
38752 /* OpenMP 4.0:
38753 safelen ( constant-expression ) */
38755 static tree
38756 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
38757 location_t location)
38759 tree t, c;
38761 matching_parens parens;
38762 if (!parens.require_open (parser))
38763 return list;
38765 t = cp_parser_constant_expression (parser);
38767 if (t == error_mark_node
38768 || !parens.require_close (parser))
38769 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38770 /*or_comma=*/false,
38771 /*consume_paren=*/true);
38773 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
38775 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
38776 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
38777 OMP_CLAUSE_CHAIN (c) = list;
38779 return c;
38782 /* OpenMP 4.0:
38783 simdlen ( constant-expression ) */
38785 static tree
38786 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
38787 location_t location)
38789 tree t, c;
38791 matching_parens parens;
38792 if (!parens.require_open (parser))
38793 return list;
38795 t = cp_parser_constant_expression (parser);
38797 if (t == error_mark_node
38798 || !parens.require_close (parser))
38799 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38800 /*or_comma=*/false,
38801 /*consume_paren=*/true);
38803 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
38805 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
38806 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
38807 OMP_CLAUSE_CHAIN (c) = list;
38809 return c;
38812 /* OpenMP 4.5:
38813 vec:
38814 identifier [+/- integer]
38815 vec , identifier [+/- integer]
38818 static tree
38819 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
38820 tree list)
38822 tree vec = NULL;
38824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38826 cp_parser_error (parser, "expected identifier");
38827 return list;
38830 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38832 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
38833 tree t, identifier = cp_parser_identifier (parser);
38834 tree addend = NULL;
38836 if (identifier == error_mark_node)
38837 t = error_mark_node;
38838 else
38840 t = cp_parser_lookup_name_simple
38841 (parser, identifier,
38842 cp_lexer_peek_token (parser->lexer)->location);
38843 if (t == error_mark_node)
38844 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
38845 id_loc);
38848 bool neg = false;
38849 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
38850 neg = true;
38851 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
38853 addend = integer_zero_node;
38854 goto add_to_vector;
38856 cp_lexer_consume_token (parser->lexer);
38858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
38860 cp_parser_error (parser, "expected integer");
38861 return list;
38864 addend = cp_lexer_peek_token (parser->lexer)->u.value;
38865 if (TREE_CODE (addend) != INTEGER_CST)
38867 cp_parser_error (parser, "expected integer");
38868 return list;
38870 cp_lexer_consume_token (parser->lexer);
38872 add_to_vector:
38873 if (t != error_mark_node)
38875 vec = tree_cons (addend, t, vec);
38876 if (neg)
38877 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
38880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
38881 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38882 break;
38884 cp_lexer_consume_token (parser->lexer);
38887 if (vec)
38889 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
38890 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
38891 OMP_CLAUSE_DECL (u) = nreverse (vec);
38892 OMP_CLAUSE_CHAIN (u) = list;
38893 return u;
38895 return list;
38898 /* OpenMP 5.0:
38899 detach ( event-handle ) */
38901 static tree
38902 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
38904 matching_parens parens;
38906 if (!parens.require_open (parser))
38907 return list;
38909 cp_token *token;
38910 tree name, decl;
38912 token = cp_lexer_peek_token (parser->lexer);
38913 name = cp_parser_id_expression (parser, /*template_p=*/false,
38914 /*check_dependency_p=*/true,
38915 /*template_p=*/NULL,
38916 /*declarator_p=*/false,
38917 /*optional_p=*/false);
38918 if (name == error_mark_node)
38919 decl = error_mark_node;
38920 else
38922 if (identifier_p (name))
38923 decl = cp_parser_lookup_name_simple (parser, name, token->location);
38924 else
38925 decl = name;
38926 if (decl == error_mark_node)
38927 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
38928 token->location);
38931 if (decl == error_mark_node
38932 || !parens.require_close (parser))
38933 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38934 /*or_comma=*/false,
38935 /*consume_paren=*/true);
38937 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
38938 OMP_CLAUSE_DECL (u) = decl;
38939 OMP_CLAUSE_CHAIN (u) = list;
38941 return u;
38944 /* OpenMP 5.0:
38945 iterators ( iterators-definition )
38947 iterators-definition:
38948 iterator-specifier
38949 iterator-specifier , iterators-definition
38951 iterator-specifier:
38952 identifier = range-specification
38953 iterator-type identifier = range-specification
38955 range-specification:
38956 begin : end
38957 begin : end : step */
38959 static tree
38960 cp_parser_omp_iterators (cp_parser *parser)
38962 tree ret = NULL_TREE, *last = &ret;
38963 cp_lexer_consume_token (parser->lexer);
38965 matching_parens parens;
38966 if (!parens.require_open (parser))
38967 return error_mark_node;
38969 bool saved_colon_corrects_to_scope_p
38970 = parser->colon_corrects_to_scope_p;
38971 bool saved_colon_doesnt_start_class_def_p
38972 = parser->colon_doesnt_start_class_def_p;
38976 tree iter_type;
38977 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38978 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
38979 iter_type = integer_type_node;
38980 else
38982 const char *saved_message
38983 = parser->type_definition_forbidden_message;
38984 parser->type_definition_forbidden_message
38985 = G_("types may not be defined in iterator type");
38987 iter_type = cp_parser_type_id (parser);
38989 parser->type_definition_forbidden_message = saved_message;
38992 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38995 cp_parser_error (parser, "expected identifier");
38996 break;
38999 tree id = cp_parser_identifier (parser);
39000 if (id == error_mark_node)
39001 break;
39003 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
39004 break;
39006 parser->colon_corrects_to_scope_p = false;
39007 parser->colon_doesnt_start_class_def_p = true;
39008 tree begin = cp_parser_assignment_expression (parser);
39010 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39011 break;
39013 tree end = cp_parser_assignment_expression (parser);
39015 tree step = integer_one_node;
39016 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39018 cp_lexer_consume_token (parser->lexer);
39019 step = cp_parser_assignment_expression (parser);
39022 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
39023 DECL_ARTIFICIAL (iter_var) = 1;
39024 DECL_CONTEXT (iter_var) = current_function_decl;
39025 pushdecl (iter_var);
39027 *last = make_tree_vec (6);
39028 TREE_VEC_ELT (*last, 0) = iter_var;
39029 TREE_VEC_ELT (*last, 1) = begin;
39030 TREE_VEC_ELT (*last, 2) = end;
39031 TREE_VEC_ELT (*last, 3) = step;
39032 last = &TREE_CHAIN (*last);
39034 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39036 cp_lexer_consume_token (parser->lexer);
39037 continue;
39039 break;
39041 while (1);
39043 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39044 parser->colon_doesnt_start_class_def_p
39045 = saved_colon_doesnt_start_class_def_p;
39047 if (!parens.require_close (parser))
39048 cp_parser_skip_to_closing_parenthesis (parser,
39049 /*recovering=*/true,
39050 /*or_comma=*/false,
39051 /*consume_paren=*/true);
39053 return ret ? ret : error_mark_node;
39056 /* OpenMP 5.0:
39057 affinity ( [aff-modifier :] variable-list )
39058 aff-modifier:
39059 iterator ( iterators-definition ) */
39061 static tree
39062 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
39064 tree nlist, c, iterators = NULL_TREE;
39066 matching_parens parens;
39067 if (!parens.require_open (parser))
39068 return list;
39070 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39072 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39073 const char *p = IDENTIFIER_POINTER (id);
39074 bool parse_iter = ((strcmp ("iterator", p) == 0)
39075 && (cp_lexer_nth_token_is (parser->lexer, 2,
39076 CPP_OPEN_PAREN)));
39077 if (parse_iter)
39079 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
39080 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
39082 if (parse_iter)
39084 begin_scope (sk_omp, NULL);
39085 iterators = cp_parser_omp_iterators (parser);
39086 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39088 if (iterators)
39089 poplevel (0, 1, 0);
39090 cp_parser_skip_to_closing_parenthesis (parser,
39091 /*recovering=*/true,
39092 /*or_comma=*/false,
39093 /*consume_paren=*/true);
39094 return list;
39098 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
39099 list, NULL);
39100 if (iterators)
39102 tree block = poplevel (1, 1, 0);
39103 if (iterators != error_mark_node)
39105 TREE_VEC_ELT (iterators, 5) = block;
39106 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39107 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
39108 OMP_CLAUSE_DECL (c));
39111 return nlist;
39114 /* OpenMP 4.0:
39115 depend ( depend-kind : variable-list )
39117 depend-kind:
39118 in | out | inout
39120 OpenMP 4.5:
39121 depend ( source )
39123 depend ( sink : vec )
39125 OpenMP 5.0:
39126 depend ( depend-modifier , depend-kind: variable-list )
39128 depend-kind:
39129 in | out | inout | mutexinoutset | depobj
39131 depend-modifier:
39132 iterator ( iterators-definition ) */
39134 static tree
39135 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
39137 tree nlist, c, iterators = NULL_TREE;
39138 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
39140 matching_parens parens;
39141 if (!parens.require_open (parser))
39142 return list;
39146 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39147 goto invalid_kind;
39149 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39150 const char *p = IDENTIFIER_POINTER (id);
39152 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
39154 begin_scope (sk_omp, NULL);
39155 iterators = cp_parser_omp_iterators (parser);
39156 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
39157 continue;
39159 if (strcmp ("in", p) == 0)
39160 kind = OMP_CLAUSE_DEPEND_IN;
39161 else if (strcmp ("inout", p) == 0)
39162 kind = OMP_CLAUSE_DEPEND_INOUT;
39163 else if (strcmp ("mutexinoutset", p) == 0)
39164 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
39165 else if (strcmp ("out", p) == 0)
39166 kind = OMP_CLAUSE_DEPEND_OUT;
39167 else if (strcmp ("depobj", p) == 0)
39168 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
39169 else if (strcmp ("sink", p) == 0)
39170 kind = OMP_CLAUSE_DEPEND_SINK;
39171 else if (strcmp ("source", p) == 0)
39172 kind = OMP_CLAUSE_DEPEND_SOURCE;
39173 else
39174 goto invalid_kind;
39175 break;
39177 while (1);
39179 cp_lexer_consume_token (parser->lexer);
39181 if (iterators
39182 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
39184 poplevel (0, 1, 0);
39185 error_at (loc, "%<iterator%> modifier incompatible with %qs",
39186 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
39187 iterators = NULL_TREE;
39190 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
39192 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
39193 OMP_CLAUSE_DEPEND_KIND (c) = kind;
39194 OMP_CLAUSE_DECL (c) = NULL_TREE;
39195 OMP_CLAUSE_CHAIN (c) = list;
39196 if (!parens.require_close (parser))
39197 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39198 /*or_comma=*/false,
39199 /*consume_paren=*/true);
39200 return c;
39203 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39204 goto resync_fail;
39206 if (kind == OMP_CLAUSE_DEPEND_SINK)
39208 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
39209 if (!parens.require_close (parser))
39210 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39211 /*or_comma=*/false,
39212 /*consume_paren=*/true);
39214 else
39216 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
39217 list, NULL);
39219 if (iterators)
39221 tree block = poplevel (1, 1, 0);
39222 if (iterators == error_mark_node)
39223 iterators = NULL_TREE;
39224 else
39225 TREE_VEC_ELT (iterators, 5) = block;
39228 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39230 OMP_CLAUSE_DEPEND_KIND (c) = kind;
39231 if (iterators)
39232 OMP_CLAUSE_DECL (c)
39233 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
39236 return nlist;
39238 invalid_kind:
39239 cp_parser_error (parser, "invalid depend kind");
39240 resync_fail:
39241 if (iterators)
39242 poplevel (0, 1, 0);
39243 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39244 /*or_comma=*/false,
39245 /*consume_paren=*/true);
39246 return list;
39249 /* OpenMP 4.0:
39250 map ( map-kind : variable-list )
39251 map ( variable-list )
39253 map-kind:
39254 alloc | to | from | tofrom
39256 OpenMP 4.5:
39257 map-kind:
39258 alloc | to | from | tofrom | release | delete
39260 map ( always [,] map-kind: variable-list )
39262 OpenMP 5.0:
39263 map ( [map-type-modifier[,] ...] map-kind: variable-list )
39265 map-type-modifier:
39266 always | close */
39268 static tree
39269 cp_parser_omp_clause_map (cp_parser *parser, tree list)
39271 tree nlist, c;
39272 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
39274 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39275 return list;
39277 int pos = 1;
39278 int map_kind_pos = 0;
39279 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
39280 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
39282 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
39284 map_kind_pos = pos;
39285 break;
39288 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
39289 pos++;
39290 pos++;
39293 bool always_modifier = false;
39294 bool close_modifier = false;
39295 for (int pos = 1; pos < map_kind_pos; ++pos)
39297 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39298 if (tok->type == CPP_COMMA)
39300 cp_lexer_consume_token (parser->lexer);
39301 continue;
39304 const char *p = IDENTIFIER_POINTER (tok->u.value);
39305 if (strcmp ("always", p) == 0)
39307 if (always_modifier)
39309 cp_parser_error (parser, "too many %<always%> modifiers");
39310 cp_parser_skip_to_closing_parenthesis (parser,
39311 /*recovering=*/true,
39312 /*or_comma=*/false,
39313 /*consume_paren=*/true);
39314 return list;
39316 always_modifier = true;
39318 else if (strcmp ("close", p) == 0)
39320 if (close_modifier)
39322 cp_parser_error (parser, "too many %<close%> modifiers");
39323 cp_parser_skip_to_closing_parenthesis (parser,
39324 /*recovering=*/true,
39325 /*or_comma=*/false,
39326 /*consume_paren=*/true);
39327 return list;
39329 close_modifier = true;
39331 else
39333 cp_parser_error (parser, "%<#pragma omp target%> with "
39334 "modifier other than %<always%> or %<close%>"
39335 "on %<map%> clause");
39336 cp_parser_skip_to_closing_parenthesis (parser,
39337 /*recovering=*/true,
39338 /*or_comma=*/false,
39339 /*consume_paren=*/true);
39340 return list;
39343 cp_lexer_consume_token (parser->lexer);
39346 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39347 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39349 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39350 const char *p = IDENTIFIER_POINTER (id);
39352 if (strcmp ("alloc", p) == 0)
39353 kind = GOMP_MAP_ALLOC;
39354 else if (strcmp ("to", p) == 0)
39355 kind = always_modifier ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
39356 else if (strcmp ("from", p) == 0)
39357 kind = always_modifier ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
39358 else if (strcmp ("tofrom", p) == 0)
39359 kind = always_modifier ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
39360 else if (strcmp ("release", p) == 0)
39361 kind = GOMP_MAP_RELEASE;
39362 else
39364 cp_parser_error (parser, "invalid map kind");
39365 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39366 /*or_comma=*/false,
39367 /*consume_paren=*/true);
39368 return list;
39370 cp_lexer_consume_token (parser->lexer);
39371 cp_lexer_consume_token (parser->lexer);
39373 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
39374 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39376 kind = GOMP_MAP_DELETE;
39377 cp_lexer_consume_token (parser->lexer);
39378 cp_lexer_consume_token (parser->lexer);
39381 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
39382 NULL, true);
39384 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39385 OMP_CLAUSE_SET_MAP_KIND (c, kind);
39387 return nlist;
39390 /* OpenMP 4.0:
39391 device ( expression )
39393 OpenMP 5.0:
39394 device ( [device-modifier :] integer-expression )
39396 device-modifier:
39397 ancestor | device_num */
39399 static tree
39400 cp_parser_omp_clause_device (cp_parser *parser, tree list,
39401 location_t location)
39403 tree t, c;
39404 bool ancestor = false;
39406 matching_parens parens;
39407 if (!parens.require_open (parser))
39408 return list;
39410 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39411 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39413 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39414 const char *p = IDENTIFIER_POINTER (tok->u.value);
39415 if (strcmp ("ancestor", p) == 0)
39417 ancestor = true;
39419 /* A requires directive with the reverse_offload clause must be
39420 specified. */
39421 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
39423 error_at (tok->location, "%<ancestor%> device modifier not "
39424 "preceded by %<requires%> directive "
39425 "with %<reverse_offload%> clause");
39426 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39427 return list;
39430 else if (strcmp ("device_num", p) == 0)
39432 else
39434 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
39435 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39436 return list;
39438 cp_lexer_consume_token (parser->lexer);
39439 cp_lexer_consume_token (parser->lexer);
39442 t = cp_parser_assignment_expression (parser);
39444 if (t == error_mark_node
39445 || !parens.require_close (parser))
39446 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39447 /*or_comma=*/false,
39448 /*consume_paren=*/true);
39450 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
39451 "device", location);
39453 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
39454 OMP_CLAUSE_DEVICE_ID (c) = t;
39455 OMP_CLAUSE_CHAIN (c) = list;
39456 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
39458 return c;
39461 /* OpenMP 4.0:
39462 dist_schedule ( static )
39463 dist_schedule ( static , expression ) */
39465 static tree
39466 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
39467 location_t location)
39469 tree c, t;
39471 matching_parens parens;
39472 if (!parens.require_open (parser))
39473 return list;
39475 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
39477 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39478 goto invalid_kind;
39479 cp_lexer_consume_token (parser->lexer);
39481 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39483 cp_lexer_consume_token (parser->lexer);
39485 t = cp_parser_assignment_expression (parser);
39487 if (t == error_mark_node)
39488 goto resync_fail;
39489 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
39491 if (!parens.require_close (parser))
39492 goto resync_fail;
39494 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39495 goto resync_fail;
39497 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
39498 "dist_schedule", location); */
39499 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
39500 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
39501 OMP_CLAUSE_CHAIN (c) = list;
39502 return c;
39504 invalid_kind:
39505 cp_parser_error (parser, "invalid dist_schedule kind");
39506 resync_fail:
39507 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39508 /*or_comma=*/false,
39509 /*consume_paren=*/true);
39510 return list;
39513 /* OpenMP 4.0:
39514 proc_bind ( proc-bind-kind )
39516 proc-bind-kind:
39517 primary | master | close | spread
39518 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
39520 static tree
39521 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
39522 location_t location)
39524 tree c;
39525 enum omp_clause_proc_bind_kind kind;
39527 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39528 return list;
39530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39532 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39533 const char *p = IDENTIFIER_POINTER (id);
39535 if (strcmp ("primary", p) == 0)
39536 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
39537 else if (strcmp ("master", p) == 0)
39538 kind = OMP_CLAUSE_PROC_BIND_MASTER;
39539 else if (strcmp ("close", p) == 0)
39540 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
39541 else if (strcmp ("spread", p) == 0)
39542 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
39543 else
39544 goto invalid_kind;
39546 else
39547 goto invalid_kind;
39549 cp_lexer_consume_token (parser->lexer);
39550 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39551 goto resync_fail;
39553 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
39554 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
39555 location);
39556 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
39557 OMP_CLAUSE_CHAIN (c) = list;
39558 return c;
39560 invalid_kind:
39561 cp_parser_error (parser, "invalid depend kind");
39562 resync_fail:
39563 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39564 /*or_comma=*/false,
39565 /*consume_paren=*/true);
39566 return list;
39569 /* OpenMP 5.0:
39570 device_type ( host | nohost | any ) */
39572 static tree
39573 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
39574 location_t location)
39576 tree c;
39577 enum omp_clause_device_type_kind kind;
39579 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39580 return list;
39582 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39584 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39585 const char *p = IDENTIFIER_POINTER (id);
39587 if (strcmp ("host", p) == 0)
39588 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
39589 else if (strcmp ("nohost", p) == 0)
39590 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
39591 else if (strcmp ("any", p) == 0)
39592 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
39593 else
39594 goto invalid_kind;
39596 else
39597 goto invalid_kind;
39599 cp_lexer_consume_token (parser->lexer);
39600 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39601 goto resync_fail;
39603 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
39604 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
39605 location); */
39606 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
39607 OMP_CLAUSE_CHAIN (c) = list;
39608 return c;
39610 invalid_kind:
39611 cp_parser_error (parser, "invalid depend kind");
39612 resync_fail:
39613 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39614 /*or_comma=*/false,
39615 /*consume_paren=*/true);
39616 return list;
39619 /* OpenACC:
39620 async [( int-expr )] */
39622 static tree
39623 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
39625 tree c, t;
39626 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39628 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
39630 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39632 matching_parens parens;
39633 parens.consume_open (parser);
39635 t = cp_parser_assignment_expression (parser);
39636 if (t == error_mark_node
39637 || !parens.require_close (parser))
39638 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39639 /*or_comma=*/false,
39640 /*consume_paren=*/true);
39643 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
39645 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
39646 OMP_CLAUSE_ASYNC_EXPR (c) = t;
39647 OMP_CLAUSE_CHAIN (c) = list;
39648 list = c;
39650 return list;
39653 /* Parse all OpenACC clauses. The set clauses allowed by the directive
39654 is a bitmask in MASK. Return the list of clauses found. */
39656 static tree
39657 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
39658 const char *where, cp_token *pragma_tok,
39659 bool finish_p = true)
39661 tree clauses = NULL;
39662 bool first = true;
39664 /* Don't create location wrapper nodes within OpenACC clauses. */
39665 auto_suppress_location_wrappers sentinel;
39667 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39669 location_t here;
39670 pragma_omp_clause c_kind;
39671 omp_clause_code code;
39672 const char *c_name;
39673 tree prev = clauses;
39675 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39676 cp_lexer_consume_token (parser->lexer);
39678 here = cp_lexer_peek_token (parser->lexer)->location;
39679 c_kind = cp_parser_omp_clause_name (parser);
39681 switch (c_kind)
39683 case PRAGMA_OACC_CLAUSE_ASYNC:
39684 clauses = cp_parser_oacc_clause_async (parser, clauses);
39685 c_name = "async";
39686 break;
39687 case PRAGMA_OACC_CLAUSE_AUTO:
39688 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
39689 clauses);
39690 c_name = "auto";
39691 break;
39692 case PRAGMA_OACC_CLAUSE_ATTACH:
39693 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39694 c_name = "attach";
39695 break;
39696 case PRAGMA_OACC_CLAUSE_COLLAPSE:
39697 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
39698 c_name = "collapse";
39699 break;
39700 case PRAGMA_OACC_CLAUSE_COPY:
39701 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39702 c_name = "copy";
39703 break;
39704 case PRAGMA_OACC_CLAUSE_COPYIN:
39705 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39706 c_name = "copyin";
39707 break;
39708 case PRAGMA_OACC_CLAUSE_COPYOUT:
39709 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39710 c_name = "copyout";
39711 break;
39712 case PRAGMA_OACC_CLAUSE_CREATE:
39713 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39714 c_name = "create";
39715 break;
39716 case PRAGMA_OACC_CLAUSE_DELETE:
39717 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39718 c_name = "delete";
39719 break;
39720 case PRAGMA_OMP_CLAUSE_DEFAULT:
39721 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
39722 c_name = "default";
39723 break;
39724 case PRAGMA_OACC_CLAUSE_DETACH:
39725 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39726 c_name = "detach";
39727 break;
39728 case PRAGMA_OACC_CLAUSE_DEVICE:
39729 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39730 c_name = "device";
39731 break;
39732 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
39733 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
39734 c_name = "deviceptr";
39735 break;
39736 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
39737 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39738 c_name = "device_resident";
39739 break;
39740 case PRAGMA_OACC_CLAUSE_FINALIZE:
39741 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
39742 clauses);
39743 c_name = "finalize";
39744 break;
39745 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
39746 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39747 clauses);
39748 c_name = "firstprivate";
39749 break;
39750 case PRAGMA_OACC_CLAUSE_GANG:
39751 c_name = "gang";
39752 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
39753 c_name, clauses);
39754 break;
39755 case PRAGMA_OACC_CLAUSE_HOST:
39756 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39757 c_name = "host";
39758 break;
39759 case PRAGMA_OACC_CLAUSE_IF:
39760 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
39761 c_name = "if";
39762 break;
39763 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
39764 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
39765 clauses);
39766 c_name = "if_present";
39767 break;
39768 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
39769 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
39770 clauses);
39771 c_name = "independent";
39772 break;
39773 case PRAGMA_OACC_CLAUSE_LINK:
39774 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39775 c_name = "link";
39776 break;
39777 case PRAGMA_OACC_CLAUSE_NO_CREATE:
39778 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39779 c_name = "no_create";
39780 break;
39781 case PRAGMA_OACC_CLAUSE_NOHOST:
39782 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
39783 clauses);
39784 c_name = "nohost";
39785 break;
39786 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
39787 code = OMP_CLAUSE_NUM_GANGS;
39788 c_name = "num_gangs";
39789 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39790 clauses);
39791 break;
39792 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
39793 c_name = "num_workers";
39794 code = OMP_CLAUSE_NUM_WORKERS;
39795 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39796 clauses);
39797 break;
39798 case PRAGMA_OACC_CLAUSE_PRESENT:
39799 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39800 c_name = "present";
39801 break;
39802 case PRAGMA_OACC_CLAUSE_PRIVATE:
39803 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
39804 clauses);
39805 c_name = "private";
39806 break;
39807 case PRAGMA_OACC_CLAUSE_REDUCTION:
39808 clauses
39809 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
39810 false, clauses);
39811 c_name = "reduction";
39812 break;
39813 case PRAGMA_OACC_CLAUSE_SEQ:
39814 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
39815 clauses);
39816 c_name = "seq";
39817 break;
39818 case PRAGMA_OACC_CLAUSE_TILE:
39819 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
39820 c_name = "tile";
39821 break;
39822 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
39823 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
39824 clauses);
39825 c_name = "use_device";
39826 break;
39827 case PRAGMA_OACC_CLAUSE_VECTOR:
39828 c_name = "vector";
39829 clauses = cp_parser_oacc_shape_clause (parser, here,
39830 OMP_CLAUSE_VECTOR,
39831 c_name, clauses);
39832 break;
39833 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
39834 c_name = "vector_length";
39835 code = OMP_CLAUSE_VECTOR_LENGTH;
39836 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39837 clauses);
39838 break;
39839 case PRAGMA_OACC_CLAUSE_WAIT:
39840 clauses = cp_parser_oacc_clause_wait (parser, clauses);
39841 c_name = "wait";
39842 break;
39843 case PRAGMA_OACC_CLAUSE_WORKER:
39844 c_name = "worker";
39845 clauses = cp_parser_oacc_shape_clause (parser, here,
39846 OMP_CLAUSE_WORKER,
39847 c_name, clauses);
39848 break;
39849 default:
39850 cp_parser_error (parser, "expected %<#pragma acc%> clause");
39851 goto saw_error;
39854 first = false;
39856 if (((mask >> c_kind) & 1) == 0)
39858 /* Remove the invalid clause(s) from the list to avoid
39859 confusing the rest of the compiler. */
39860 clauses = prev;
39861 error_at (here, "%qs is not valid for %qs", c_name, where);
39865 saw_error:
39866 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39868 if (finish_p)
39869 return finish_omp_clauses (clauses, C_ORT_ACC);
39871 return clauses;
39874 /* Parse all OpenMP clauses. The set clauses allowed by the directive
39875 is a bitmask in MASK. Return the list of clauses found.
39876 FINISH_P set if finish_omp_clauses should be called.
39877 NESTED non-zero if clauses should be terminated by closing paren instead
39878 of end of pragma. If it is 2, additionally commas are required in between
39879 the clauses. */
39881 static tree
39882 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
39883 const char *where, cp_token *pragma_tok,
39884 bool finish_p = true, int nested = 0)
39886 tree clauses = NULL;
39887 bool first = true;
39888 cp_token *token = NULL;
39890 /* Don't create location wrapper nodes within OpenMP clauses. */
39891 auto_suppress_location_wrappers sentinel;
39893 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39895 pragma_omp_clause c_kind;
39896 const char *c_name;
39897 tree prev = clauses;
39899 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39900 break;
39902 if (!first
39903 /* OpenMP 5.1 allows optional comma in between directive-name and
39904 clauses everywhere, but as we aren't done with OpenMP 5.0
39905 implementation yet, let's allow it for now only in C++11
39906 attributes. */
39907 || (parser->lexer->in_omp_attribute_pragma && nested != 2))
39909 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39910 cp_lexer_consume_token (parser->lexer);
39911 else if (nested == 2)
39912 error_at (cp_lexer_peek_token (parser->lexer)->location,
39913 "clauses in %<simd%> trait should be separated "
39914 "by %<,%>");
39917 token = cp_lexer_peek_token (parser->lexer);
39918 c_kind = cp_parser_omp_clause_name (parser);
39920 switch (c_kind)
39922 case PRAGMA_OMP_CLAUSE_BIND:
39923 clauses = cp_parser_omp_clause_bind (parser, clauses,
39924 token->location);
39925 c_name = "bind";
39926 break;
39927 case PRAGMA_OMP_CLAUSE_COLLAPSE:
39928 clauses = cp_parser_omp_clause_collapse (parser, clauses,
39929 token->location);
39930 c_name = "collapse";
39931 break;
39932 case PRAGMA_OMP_CLAUSE_COPYIN:
39933 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
39934 c_name = "copyin";
39935 break;
39936 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
39937 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
39938 clauses);
39939 c_name = "copyprivate";
39940 break;
39941 case PRAGMA_OMP_CLAUSE_DEFAULT:
39942 clauses = cp_parser_omp_clause_default (parser, clauses,
39943 token->location, false);
39944 c_name = "default";
39945 break;
39946 case PRAGMA_OMP_CLAUSE_FILTER:
39947 clauses = cp_parser_omp_clause_filter (parser, clauses,
39948 token->location);
39949 c_name = "filter";
39950 break;
39951 case PRAGMA_OMP_CLAUSE_FINAL:
39952 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
39953 c_name = "final";
39954 break;
39955 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
39956 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39957 clauses);
39958 c_name = "firstprivate";
39959 break;
39960 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
39961 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
39962 token->location);
39963 c_name = "grainsize";
39964 break;
39965 case PRAGMA_OMP_CLAUSE_HINT:
39966 clauses = cp_parser_omp_clause_hint (parser, clauses,
39967 token->location);
39968 c_name = "hint";
39969 break;
39970 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
39971 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
39972 token->location);
39973 c_name = "defaultmap";
39974 break;
39975 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
39976 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
39977 clauses);
39978 c_name = "use_device_ptr";
39979 break;
39980 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
39981 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
39982 clauses);
39983 c_name = "use_device_addr";
39984 break;
39985 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
39986 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
39987 clauses);
39988 c_name = "is_device_ptr";
39989 break;
39990 case PRAGMA_OMP_CLAUSE_IF:
39991 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
39992 true);
39993 c_name = "if";
39994 break;
39995 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
39996 clauses
39997 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
39998 true, clauses);
39999 c_name = "in_reduction";
40000 break;
40001 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
40002 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
40003 c_name = "lastprivate";
40004 break;
40005 case PRAGMA_OMP_CLAUSE_MERGEABLE:
40006 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
40007 token->location);
40008 c_name = "mergeable";
40009 break;
40010 case PRAGMA_OMP_CLAUSE_NOWAIT:
40011 clauses = cp_parser_omp_clause_nowait (parser, clauses,
40012 token->location);
40013 c_name = "nowait";
40014 break;
40015 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
40016 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
40017 token->location);
40018 c_name = "num_tasks";
40019 break;
40020 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
40021 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
40022 token->location);
40023 c_name = "num_threads";
40024 break;
40025 case PRAGMA_OMP_CLAUSE_ORDER:
40026 clauses = cp_parser_omp_clause_order (parser, clauses,
40027 token->location);
40028 c_name = "order";
40029 break;
40030 case PRAGMA_OMP_CLAUSE_ORDERED:
40031 clauses = cp_parser_omp_clause_ordered (parser, clauses,
40032 token->location);
40033 c_name = "ordered";
40034 break;
40035 case PRAGMA_OMP_CLAUSE_PRIORITY:
40036 clauses = cp_parser_omp_clause_priority (parser, clauses,
40037 token->location);
40038 c_name = "priority";
40039 break;
40040 case PRAGMA_OMP_CLAUSE_PRIVATE:
40041 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
40042 clauses);
40043 c_name = "private";
40044 break;
40045 case PRAGMA_OMP_CLAUSE_REDUCTION:
40046 clauses
40047 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
40048 true, clauses);
40049 c_name = "reduction";
40050 break;
40051 case PRAGMA_OMP_CLAUSE_SCHEDULE:
40052 clauses = cp_parser_omp_clause_schedule (parser, clauses,
40053 token->location);
40054 c_name = "schedule";
40055 break;
40056 case PRAGMA_OMP_CLAUSE_SHARED:
40057 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
40058 clauses);
40059 c_name = "shared";
40060 break;
40061 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
40062 clauses
40063 = cp_parser_omp_clause_reduction (parser,
40064 OMP_CLAUSE_TASK_REDUCTION,
40065 true, clauses);
40066 c_name = "task_reduction";
40067 break;
40068 case PRAGMA_OMP_CLAUSE_UNTIED:
40069 clauses = cp_parser_omp_clause_untied (parser, clauses,
40070 token->location);
40071 c_name = "untied";
40072 break;
40073 case PRAGMA_OMP_CLAUSE_INBRANCH:
40074 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
40075 clauses, token->location);
40076 c_name = "inbranch";
40077 break;
40078 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
40079 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
40080 clauses);
40081 c_name = "nontemporal";
40082 break;
40083 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
40084 clauses = cp_parser_omp_clause_branch (parser,
40085 OMP_CLAUSE_NOTINBRANCH,
40086 clauses, token->location);
40087 c_name = "notinbranch";
40088 break;
40089 case PRAGMA_OMP_CLAUSE_PARALLEL:
40090 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
40091 clauses, token->location);
40092 c_name = "parallel";
40093 if (!first)
40095 clause_not_first:
40096 error_at (token->location, "%qs must be the first clause of %qs",
40097 c_name, where);
40098 clauses = prev;
40100 break;
40101 case PRAGMA_OMP_CLAUSE_FOR:
40102 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
40103 clauses, token->location);
40104 c_name = "for";
40105 if (!first)
40106 goto clause_not_first;
40107 break;
40108 case PRAGMA_OMP_CLAUSE_SECTIONS:
40109 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
40110 clauses, token->location);
40111 c_name = "sections";
40112 if (!first)
40113 goto clause_not_first;
40114 break;
40115 case PRAGMA_OMP_CLAUSE_TASKGROUP:
40116 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
40117 clauses, token->location);
40118 c_name = "taskgroup";
40119 if (!first)
40120 goto clause_not_first;
40121 break;
40122 case PRAGMA_OMP_CLAUSE_LINK:
40123 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
40124 c_name = "to";
40125 break;
40126 case PRAGMA_OMP_CLAUSE_TO:
40127 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
40128 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
40129 clauses);
40130 else
40131 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses,
40132 true);
40133 c_name = "to";
40134 break;
40135 case PRAGMA_OMP_CLAUSE_FROM:
40136 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses,
40137 true);
40138 c_name = "from";
40139 break;
40140 case PRAGMA_OMP_CLAUSE_UNIFORM:
40141 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
40142 clauses);
40143 c_name = "uniform";
40144 break;
40145 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
40146 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
40147 token->location);
40148 c_name = "num_teams";
40149 break;
40150 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
40151 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
40152 token->location);
40153 c_name = "thread_limit";
40154 break;
40155 case PRAGMA_OMP_CLAUSE_ALIGNED:
40156 clauses = cp_parser_omp_clause_aligned (parser, clauses);
40157 c_name = "aligned";
40158 break;
40159 case PRAGMA_OMP_CLAUSE_ALLOCATE:
40160 clauses = cp_parser_omp_clause_allocate (parser, clauses);
40161 c_name = "allocate";
40162 break;
40163 case PRAGMA_OMP_CLAUSE_LINEAR:
40165 bool declare_simd = false;
40166 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
40167 declare_simd = true;
40168 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
40170 c_name = "linear";
40171 break;
40172 case PRAGMA_OMP_CLAUSE_AFFINITY:
40173 clauses = cp_parser_omp_clause_affinity (parser, clauses);
40174 c_name = "affinity";
40175 break;
40176 case PRAGMA_OMP_CLAUSE_DEPEND:
40177 clauses = cp_parser_omp_clause_depend (parser, clauses,
40178 token->location);
40179 c_name = "depend";
40180 break;
40181 case PRAGMA_OMP_CLAUSE_DETACH:
40182 clauses = cp_parser_omp_clause_detach (parser, clauses);
40183 c_name = "detach";
40184 break;
40185 case PRAGMA_OMP_CLAUSE_MAP:
40186 clauses = cp_parser_omp_clause_map (parser, clauses);
40187 c_name = "map";
40188 break;
40189 case PRAGMA_OMP_CLAUSE_DEVICE:
40190 clauses = cp_parser_omp_clause_device (parser, clauses,
40191 token->location);
40192 c_name = "device";
40193 break;
40194 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
40195 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
40196 token->location);
40197 c_name = "dist_schedule";
40198 break;
40199 case PRAGMA_OMP_CLAUSE_PROC_BIND:
40200 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
40201 token->location);
40202 c_name = "proc_bind";
40203 break;
40204 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
40205 clauses = cp_parser_omp_clause_device_type (parser, clauses,
40206 token->location);
40207 c_name = "device_type";
40208 break;
40209 case PRAGMA_OMP_CLAUSE_SAFELEN:
40210 clauses = cp_parser_omp_clause_safelen (parser, clauses,
40211 token->location);
40212 c_name = "safelen";
40213 break;
40214 case PRAGMA_OMP_CLAUSE_SIMDLEN:
40215 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
40216 token->location);
40217 c_name = "simdlen";
40218 break;
40219 case PRAGMA_OMP_CLAUSE_NOGROUP:
40220 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
40221 token->location);
40222 c_name = "nogroup";
40223 break;
40224 case PRAGMA_OMP_CLAUSE_THREADS:
40225 clauses
40226 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
40227 clauses, token->location);
40228 c_name = "threads";
40229 break;
40230 case PRAGMA_OMP_CLAUSE_SIMD:
40231 clauses
40232 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
40233 clauses, token->location);
40234 c_name = "simd";
40235 break;
40236 default:
40237 cp_parser_error (parser, "expected %<#pragma omp%> clause");
40238 goto saw_error;
40241 first = false;
40243 if (((mask >> c_kind) & 1) == 0)
40245 /* Remove the invalid clause(s) from the list to avoid
40246 confusing the rest of the compiler. */
40247 clauses = prev;
40248 error_at (token->location, "%qs is not valid for %qs", c_name, where);
40251 saw_error:
40252 if (!nested)
40253 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40254 if (finish_p)
40256 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
40257 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
40258 else
40259 return finish_omp_clauses (clauses, C_ORT_OMP);
40261 return clauses;
40264 /* OpenMP 2.5:
40265 structured-block:
40266 statement
40268 In practice, we're also interested in adding the statement to an
40269 outer node. So it is convenient if we work around the fact that
40270 cp_parser_statement calls add_stmt. */
40272 static unsigned
40273 cp_parser_begin_omp_structured_block (cp_parser *parser)
40275 unsigned save = parser->in_statement;
40277 /* Only move the values to IN_OMP_BLOCK if they weren't false.
40278 This preserves the "not within loop or switch" style error messages
40279 for nonsense cases like
40280 void foo() {
40281 #pragma omp single
40282 break;
40285 if (parser->in_statement)
40286 parser->in_statement = IN_OMP_BLOCK;
40288 return save;
40291 static void
40292 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
40294 parser->in_statement = save;
40297 static tree
40298 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
40300 tree stmt = begin_omp_structured_block ();
40301 unsigned int save = cp_parser_begin_omp_structured_block (parser);
40303 parser->omp_attrs_forbidden_p = true;
40304 cp_parser_statement (parser, NULL_TREE, false, if_p);
40306 cp_parser_end_omp_structured_block (parser, save);
40307 return finish_omp_structured_block (stmt);
40310 /* OpenMP 5.0:
40311 # pragma omp allocate (list) [allocator(allocator)] */
40313 static void
40314 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
40316 tree allocator = NULL_TREE;
40317 location_t loc = pragma_tok->location;
40318 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
40320 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
40321 if (parser->lexer->in_omp_attribute_pragma
40322 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40323 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40324 cp_lexer_consume_token (parser->lexer);
40326 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40328 matching_parens parens;
40329 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40330 const char *p = IDENTIFIER_POINTER (id);
40331 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40332 cp_lexer_consume_token (parser->lexer);
40333 if (strcmp (p, "allocator") != 0)
40334 error_at (cloc, "expected %<allocator%>");
40335 else if (parens.require_open (parser))
40337 allocator = cp_parser_assignment_expression (parser);
40338 if (allocator == error_mark_node)
40339 allocator = NULL_TREE;
40340 parens.require_close (parser);
40343 cp_parser_require_pragma_eol (parser, pragma_tok);
40345 if (allocator)
40346 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
40347 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40349 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
40352 /* OpenMP 2.5:
40353 # pragma omp atomic new-line
40354 expression-stmt
40356 expression-stmt:
40357 x binop= expr | x++ | ++x | x-- | --x
40358 binop:
40359 +, *, -, /, &, ^, |, <<, >>
40361 where x is an lvalue expression with scalar type.
40363 OpenMP 3.1:
40364 # pragma omp atomic new-line
40365 update-stmt
40367 # pragma omp atomic read new-line
40368 read-stmt
40370 # pragma omp atomic write new-line
40371 write-stmt
40373 # pragma omp atomic update new-line
40374 update-stmt
40376 # pragma omp atomic capture new-line
40377 capture-stmt
40379 # pragma omp atomic capture new-line
40380 capture-block
40382 read-stmt:
40383 v = x
40384 write-stmt:
40385 x = expr
40386 update-stmt:
40387 expression-stmt | x = x binop expr
40388 capture-stmt:
40389 v = expression-stmt
40390 capture-block:
40391 { v = x; update-stmt; } | { update-stmt; v = x; }
40393 OpenMP 4.0:
40394 update-stmt:
40395 expression-stmt | x = x binop expr | x = expr binop x
40396 capture-stmt:
40397 v = update-stmt
40398 capture-block:
40399 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
40401 OpenMP 5.1:
40402 # pragma omp atomic compare new-line
40403 conditional-update-atomic
40405 # pragma omp atomic compare capture new-line
40406 conditional-update-capture-atomic
40408 conditional-update-atomic:
40409 cond-expr-stmt | cond-update-stmt
40410 cond-expr-stmt:
40411 x = expr ordop x ? expr : x;
40412 x = x ordop expr ? expr : x;
40413 x = x == e ? d : x;
40414 cond-update-stmt:
40415 if (expr ordop x) { x = expr; }
40416 if (x ordop expr) { x = expr; }
40417 if (x == e) { x = d; }
40418 ordop:
40419 <, >
40420 conditional-update-capture-atomic:
40421 v = cond-expr-stmt
40422 { v = x; cond-expr-stmt }
40423 { cond-expr-stmt v = x; }
40424 { v = x; cond-update-stmt }
40425 { cond-update-stmt v = x; }
40426 if (x == e) { x = d; } else { v = x; }
40427 { r = x == e; if (r) { x = d; } }
40428 { r = x == e; if (r) { x = d; } else { v = x; } }
40430 where x, r and v are lvalue expressions with scalar type,
40431 expr, e and d are expressions with scalar type and e might be
40432 the same as v. */
40434 static void
40435 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
40437 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
40438 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
40439 location_t loc = pragma_tok->location;
40440 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
40441 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
40442 bool structured_block = false;
40443 bool first = true;
40444 tree clauses = NULL_TREE;
40445 bool capture = false;
40446 bool compare = false;
40447 bool weak = false;
40448 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40449 bool no_semicolon = false;
40450 bool extra_scope = false;
40452 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40454 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
40455 if ((!first || parser->lexer->in_omp_attribute_pragma)
40456 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40457 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40458 cp_lexer_consume_token (parser->lexer);
40460 first = false;
40462 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40464 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40465 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40466 const char *p = IDENTIFIER_POINTER (id);
40467 enum tree_code new_code = ERROR_MARK;
40468 enum omp_memory_order new_memory_order
40469 = OMP_MEMORY_ORDER_UNSPECIFIED;
40470 bool new_capture = false;
40471 bool new_compare = false;
40472 bool new_weak = false;
40473 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40475 if (!strcmp (p, "read"))
40476 new_code = OMP_ATOMIC_READ;
40477 else if (!strcmp (p, "write"))
40478 new_code = NOP_EXPR;
40479 else if (!strcmp (p, "update"))
40480 new_code = OMP_ATOMIC;
40481 else if (openacc && !strcmp (p, "capture"))
40482 new_code = OMP_ATOMIC_CAPTURE_NEW;
40483 else if (openacc)
40485 p = NULL;
40486 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
40487 "or %<capture%> clause");
40489 else if (!strcmp (p, "capture"))
40490 new_capture = true;
40491 else if (!strcmp (p, "compare"))
40492 new_compare = true;
40493 else if (!strcmp (p, "weak"))
40494 new_weak = true;
40495 else if (!strcmp (p, "fail"))
40497 matching_parens parens;
40499 cp_lexer_consume_token (parser->lexer);
40500 if (!parens.require_open (parser))
40501 continue;
40503 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40505 id = cp_lexer_peek_token (parser->lexer)->u.value;
40506 const char *q = IDENTIFIER_POINTER (id);
40508 if (!strcmp (q, "seq_cst"))
40509 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
40510 else if (!strcmp (q, "acquire"))
40511 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
40512 else if (!strcmp (q, "relaxed"))
40513 new_fail = OMP_MEMORY_ORDER_RELAXED;
40516 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40518 cp_lexer_consume_token (parser->lexer);
40519 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40520 error_at (cloc, "too many %qs clauses", "fail");
40521 else
40522 fail = new_fail;
40524 else
40525 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
40526 "or %<relaxed%>");
40527 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
40528 || !parens.require_close (parser))
40529 cp_parser_skip_to_closing_parenthesis (parser,
40530 /*recovering=*/true,
40531 /*or_comma=*/false,
40532 /*consume_paren=*/true);
40533 continue;
40535 else if (!strcmp (p, "seq_cst"))
40536 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40537 else if (!strcmp (p, "acq_rel"))
40538 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
40539 else if (!strcmp (p, "release"))
40540 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
40541 else if (!strcmp (p, "acquire"))
40542 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40543 else if (!strcmp (p, "relaxed"))
40544 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
40545 else if (!strcmp (p, "hint"))
40547 cp_lexer_consume_token (parser->lexer);
40548 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
40549 continue;
40551 else
40553 p = NULL;
40554 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
40555 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
40556 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
40557 "%<relaxed%> or %<hint%> clause");
40559 if (p)
40561 if (new_code != ERROR_MARK)
40563 /* OpenACC permits 'update capture'. */
40564 if (openacc
40565 && code == OMP_ATOMIC
40566 && new_code == OMP_ATOMIC_CAPTURE_NEW)
40567 code = new_code;
40568 else if (code != ERROR_MARK)
40569 error_at (cloc, "too many atomic clauses");
40570 else
40571 code = new_code;
40573 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
40575 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
40576 error_at (cloc, "too many memory order clauses");
40577 else
40578 memory_order = new_memory_order;
40580 else if (new_capture)
40582 if (capture)
40583 error_at (cloc, "too many %qs clauses", "capture");
40584 else
40585 capture = true;
40587 else if (new_compare)
40589 if (compare)
40590 error_at (cloc, "too many %qs clauses", "compare");
40591 else
40592 compare = true;
40594 else if (new_weak)
40596 if (weak)
40597 error_at (cloc, "too many %qs clauses", "weak");
40598 else
40599 weak = true;
40601 cp_lexer_consume_token (parser->lexer);
40602 continue;
40605 break;
40607 cp_parser_require_pragma_eol (parser, pragma_tok);
40609 if (code == ERROR_MARK)
40610 code = OMP_ATOMIC;
40611 if (capture)
40613 if (code != OMP_ATOMIC)
40614 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
40615 "clauses", "capture");
40616 else
40617 code = OMP_ATOMIC_CAPTURE_NEW;
40619 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
40621 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
40622 "clauses", "compare");
40623 compare = false;
40625 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
40627 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
40628 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40630 if (weak && !compare)
40632 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
40633 weak = false;
40635 if (openacc)
40636 memory_order = OMP_MEMORY_ORDER_RELAXED;
40637 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
40639 omp_requires_mask
40640 = (enum omp_requires) (omp_requires_mask
40641 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
40642 switch ((enum omp_memory_order)
40643 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
40645 case OMP_MEMORY_ORDER_UNSPECIFIED:
40646 case OMP_MEMORY_ORDER_RELAXED:
40647 memory_order = OMP_MEMORY_ORDER_RELAXED;
40648 break;
40649 case OMP_MEMORY_ORDER_SEQ_CST:
40650 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40651 break;
40652 case OMP_MEMORY_ORDER_ACQ_REL:
40653 switch (code)
40655 case OMP_ATOMIC_READ:
40656 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40657 break;
40658 case NOP_EXPR: /* atomic write */
40659 memory_order = OMP_MEMORY_ORDER_RELEASE;
40660 break;
40661 default:
40662 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
40663 break;
40665 break;
40666 default:
40667 gcc_unreachable ();
40670 else
40671 switch (code)
40673 case OMP_ATOMIC_READ:
40674 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
40676 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
40677 "%<release%> clause");
40678 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40680 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
40681 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40682 break;
40683 case NOP_EXPR: /* atomic write */
40684 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
40686 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
40687 "%<acquire%> clause");
40688 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40690 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
40691 memory_order = OMP_MEMORY_ORDER_RELEASE;
40692 break;
40693 default:
40694 break;
40696 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40697 memory_order
40698 = (enum omp_memory_order) (memory_order
40699 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
40701 switch (code)
40703 case OMP_ATOMIC_READ:
40704 case NOP_EXPR: /* atomic write */
40705 v = cp_parser_unary_expression (parser);
40706 if (v == error_mark_node)
40707 goto saw_error;
40708 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40709 goto saw_error;
40710 if (code == NOP_EXPR)
40711 lhs = cp_parser_expression (parser);
40712 else
40713 lhs = cp_parser_unary_expression (parser);
40714 if (lhs == error_mark_node)
40715 goto saw_error;
40716 if (code == NOP_EXPR)
40718 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
40719 opcode. */
40720 code = OMP_ATOMIC;
40721 rhs = lhs;
40722 lhs = v;
40723 v = NULL_TREE;
40725 goto done;
40726 case OMP_ATOMIC_CAPTURE_NEW:
40727 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
40729 cp_lexer_consume_token (parser->lexer);
40730 structured_block = true;
40732 else if (compare
40733 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40734 break;
40735 else
40737 v = cp_parser_unary_expression (parser);
40738 if (v == error_mark_node)
40739 goto saw_error;
40740 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40741 goto saw_error;
40742 if (compare
40743 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40745 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
40746 error_at (eloc, "expected expression");
40747 goto saw_error;
40750 default:
40751 break;
40754 restart:
40755 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40757 cp_lexer_consume_token (parser->lexer);
40759 matching_parens parens;
40760 if (!parens.require_open (parser))
40761 goto saw_error;
40762 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
40763 tree cmp_expr;
40764 if (r)
40765 cmp_expr = cp_parser_unary_expression (parser);
40766 else
40767 cmp_expr = cp_parser_binary_expression (parser, false, true,
40768 PREC_NOT_OPERATOR, NULL);
40769 if (!parens.require_close (parser))
40770 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40771 if (cmp_expr == error_mark_node)
40772 goto saw_error;
40773 if (r)
40775 if (!cp_tree_equal (cmp_expr, r))
40776 goto bad_if;
40777 cmp_expr = rhs;
40778 rhs = NULL_TREE;
40779 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
40781 if (TREE_CODE (cmp_expr) == EQ_EXPR)
40783 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
40785 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
40786 "expected %<==%> comparison in %<if%> condition");
40787 goto saw_error;
40789 else if (TREE_CODE (cmp_expr) != GT_EXPR
40790 && TREE_CODE (cmp_expr) != LT_EXPR)
40792 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
40793 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
40794 "condition");
40795 goto saw_error;
40797 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
40798 goto saw_error;
40800 extra_scope = true;
40801 eloc = cp_lexer_peek_token (parser->lexer)->location;
40802 lhs = cp_parser_unary_expression (parser);
40803 orig_lhs = lhs;
40804 if (lhs == error_mark_node)
40805 goto saw_error;
40806 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
40808 cp_parser_error (parser, "expected %<=%>");
40809 goto saw_error;
40811 cp_lexer_consume_token (parser->lexer);
40812 eloc = cp_lexer_peek_token (parser->lexer)->location;
40813 if (TREE_CODE (cmp_expr) == EQ_EXPR)
40814 rhs1 = cp_parser_expression (parser);
40815 else
40816 rhs1 = cp_parser_simple_cast_expression (parser);
40818 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
40819 goto saw_error;
40821 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
40822 goto saw_error;
40824 extra_scope = false;
40825 no_semicolon = true;
40827 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
40829 if (TREE_CODE (cmp_expr) == EQ_EXPR)
40831 opcode = COND_EXPR;
40832 rhs = TREE_OPERAND (cmp_expr, 1);
40834 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
40836 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
40837 ? MIN_EXPR : MAX_EXPR);
40838 rhs = rhs1;
40839 rhs1 = TREE_OPERAND (cmp_expr, 0);
40841 else
40842 goto bad_if;
40844 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
40845 goto bad_if;
40846 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
40847 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
40849 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
40850 ? MAX_EXPR : MIN_EXPR);
40851 rhs = rhs1;
40852 rhs1 = TREE_OPERAND (cmp_expr, 1);
40854 else
40856 bad_if:
40857 cp_parser_error (parser,
40858 "invalid form of %<#pragma omp atomic compare%>");
40859 goto saw_error;
40862 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
40864 if (code != OMP_ATOMIC_CAPTURE_NEW
40865 || (structured_block && r == NULL_TREE)
40866 || TREE_CODE (cmp_expr) != EQ_EXPR)
40868 eloc = cp_lexer_peek_token (parser->lexer)->location;
40869 error_at (eloc, "unexpected %<else%>");
40870 goto saw_error;
40873 cp_lexer_consume_token (parser->lexer);
40875 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
40876 goto saw_error;
40878 extra_scope = true;
40879 v = cp_parser_unary_expression (parser);
40880 if (v == error_mark_node)
40881 goto saw_error;
40882 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40883 goto saw_error;
40885 tree expr = cp_parser_simple_cast_expression (parser);
40887 if (!cp_tree_equal (expr, lhs))
40888 goto bad_if;
40890 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
40891 goto saw_error;
40893 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
40894 goto saw_error;
40896 extra_scope = false;
40897 code = OMP_ATOMIC_CAPTURE_OLD;
40898 if (r == NULL_TREE)
40899 /* Signal to c_finish_omp_atomic that in
40900 if (x == e) { x = d; } else { v = x; }
40901 case the store to v should be conditional. */
40902 r = void_list_node;
40904 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
40906 cp_parser_error (parser, "expected %<else%>");
40907 goto saw_error;
40909 else if (code == OMP_ATOMIC_CAPTURE_NEW
40910 && r != NULL_TREE
40911 && v == NULL_TREE)
40912 code = OMP_ATOMIC;
40913 goto stmt_done;
40915 lhs = cp_parser_unary_expression (parser);
40916 orig_lhs = lhs;
40917 switch (TREE_CODE (lhs))
40919 case ERROR_MARK:
40920 goto saw_error;
40922 case POSTINCREMENT_EXPR:
40923 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
40924 code = OMP_ATOMIC_CAPTURE_OLD;
40925 /* FALLTHROUGH */
40926 case PREINCREMENT_EXPR:
40927 lhs = TREE_OPERAND (lhs, 0);
40928 opcode = PLUS_EXPR;
40929 rhs = integer_one_node;
40930 if (compare)
40931 goto invalid_compare;
40932 break;
40934 case POSTDECREMENT_EXPR:
40935 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
40936 code = OMP_ATOMIC_CAPTURE_OLD;
40937 /* FALLTHROUGH */
40938 case PREDECREMENT_EXPR:
40939 lhs = TREE_OPERAND (lhs, 0);
40940 opcode = MINUS_EXPR;
40941 rhs = integer_one_node;
40942 if (compare)
40943 goto invalid_compare;
40944 break;
40946 case COMPOUND_EXPR:
40947 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
40948 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
40949 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
40950 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
40951 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
40952 (TREE_OPERAND (lhs, 1), 0), 0)))
40953 == BOOLEAN_TYPE)
40954 /* Undo effects of boolean_increment for post {in,de}crement. */
40955 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
40956 /* FALLTHRU */
40957 case MODIFY_EXPR:
40958 if (TREE_CODE (lhs) == MODIFY_EXPR
40959 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
40961 /* Undo effects of boolean_increment. */
40962 if (integer_onep (TREE_OPERAND (lhs, 1)))
40964 /* This is pre or post increment. */
40965 rhs = TREE_OPERAND (lhs, 1);
40966 lhs = TREE_OPERAND (lhs, 0);
40967 opcode = NOP_EXPR;
40968 if (code == OMP_ATOMIC_CAPTURE_NEW
40969 && !structured_block
40970 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
40971 code = OMP_ATOMIC_CAPTURE_OLD;
40972 if (compare)
40973 goto invalid_compare;
40974 break;
40977 /* FALLTHRU */
40978 default:
40979 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
40981 cp_parser_error (parser, "expected %<=%>");
40982 goto saw_error;
40984 switch (cp_lexer_peek_token (parser->lexer)->type)
40986 case CPP_MULT_EQ:
40987 opcode = MULT_EXPR;
40988 break;
40989 case CPP_DIV_EQ:
40990 opcode = TRUNC_DIV_EXPR;
40991 break;
40992 case CPP_PLUS_EQ:
40993 opcode = PLUS_EXPR;
40994 break;
40995 case CPP_MINUS_EQ:
40996 opcode = MINUS_EXPR;
40997 break;
40998 case CPP_LSHIFT_EQ:
40999 opcode = LSHIFT_EXPR;
41000 break;
41001 case CPP_RSHIFT_EQ:
41002 opcode = RSHIFT_EXPR;
41003 break;
41004 case CPP_AND_EQ:
41005 opcode = BIT_AND_EXPR;
41006 break;
41007 case CPP_OR_EQ:
41008 opcode = BIT_IOR_EXPR;
41009 break;
41010 case CPP_XOR_EQ:
41011 opcode = BIT_XOR_EXPR;
41012 break;
41013 case CPP_EQ:
41014 enum cp_parser_prec oprec;
41015 cp_token *token;
41016 cp_lexer_consume_token (parser->lexer);
41017 cp_parser_parse_tentatively (parser);
41018 rhs1 = cp_parser_simple_cast_expression (parser);
41019 if (rhs1 == error_mark_node)
41021 cp_parser_abort_tentative_parse (parser);
41022 cp_parser_simple_cast_expression (parser);
41023 goto saw_error;
41025 token = cp_lexer_peek_token (parser->lexer);
41026 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
41028 cp_parser_abort_tentative_parse (parser);
41029 cp_parser_parse_tentatively (parser);
41030 rhs = cp_parser_binary_expression (parser, false, true,
41031 PREC_NOT_OPERATOR, NULL);
41032 if (rhs == error_mark_node)
41034 cp_parser_abort_tentative_parse (parser);
41035 cp_parser_binary_expression (parser, false, true,
41036 PREC_NOT_OPERATOR, NULL);
41037 goto saw_error;
41039 switch (TREE_CODE (rhs))
41041 case MULT_EXPR:
41042 case TRUNC_DIV_EXPR:
41043 case RDIV_EXPR:
41044 case PLUS_EXPR:
41045 case MINUS_EXPR:
41046 case LSHIFT_EXPR:
41047 case RSHIFT_EXPR:
41048 case BIT_AND_EXPR:
41049 case BIT_IOR_EXPR:
41050 case BIT_XOR_EXPR:
41051 if (compare)
41052 break;
41053 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
41055 if (cp_parser_parse_definitely (parser))
41057 opcode = TREE_CODE (rhs);
41058 rhs1 = TREE_OPERAND (rhs, 0);
41059 rhs = TREE_OPERAND (rhs, 1);
41060 goto stmt_done;
41062 else
41063 goto saw_error;
41065 break;
41066 case EQ_EXPR:
41067 if (!compare
41068 || code != OMP_ATOMIC_CAPTURE_NEW
41069 || !structured_block
41070 || v
41071 || r)
41072 break;
41073 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
41074 && cp_lexer_nth_token_is_keyword (parser->lexer,
41075 2, RID_IF))
41077 if (cp_parser_parse_definitely (parser))
41079 r = lhs;
41080 lhs = NULL_TREE;
41081 rhs1 = NULL_TREE;
41082 cp_lexer_consume_token (parser->lexer);
41083 goto restart;
41086 break;
41087 case GT_EXPR:
41088 case LT_EXPR:
41089 if (compare
41090 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
41091 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
41092 && cp_parser_parse_definitely (parser))
41094 opcode = TREE_CODE (rhs);
41095 rhs1 = TREE_OPERAND (rhs, 0);
41096 rhs = TREE_OPERAND (rhs, 1);
41097 cond_expr:
41098 cp_lexer_consume_token (parser->lexer);
41099 bool saved_colon_corrects_to_scope_p
41100 = parser->colon_corrects_to_scope_p;
41101 parser->colon_corrects_to_scope_p = false;
41102 tree e1 = cp_parser_expression (parser);
41103 parser->colon_corrects_to_scope_p
41104 = saved_colon_corrects_to_scope_p;
41105 cp_parser_require (parser, CPP_COLON, RT_COLON);
41106 tree e2 = cp_parser_simple_cast_expression (parser);
41107 if (cp_tree_equal (lhs, e2))
41109 if (cp_tree_equal (lhs, rhs1))
41111 if (opcode == EQ_EXPR)
41113 opcode = COND_EXPR;
41114 rhs1 = e1;
41115 goto stmt_done;
41117 if (cp_tree_equal (rhs, e1))
41119 opcode
41120 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
41121 rhs = e1;
41122 goto stmt_done;
41125 else
41127 gcc_assert (opcode != EQ_EXPR);
41128 if (cp_tree_equal (rhs1, e1))
41130 opcode
41131 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
41132 rhs1 = rhs;
41133 rhs = e1;
41134 goto stmt_done;
41138 cp_parser_error (parser,
41139 "invalid form of "
41140 "%<#pragma omp atomic compare%>");
41141 goto saw_error;
41143 break;
41144 default:
41145 break;
41147 cp_parser_abort_tentative_parse (parser);
41148 if (structured_block
41149 && code == OMP_ATOMIC_CAPTURE_OLD
41150 && !compare)
41152 rhs = cp_parser_expression (parser);
41153 if (rhs == error_mark_node)
41154 goto saw_error;
41155 opcode = NOP_EXPR;
41156 rhs1 = NULL_TREE;
41157 goto stmt_done;
41159 cp_parser_error (parser,
41160 "invalid form of %<#pragma omp atomic%>");
41161 goto saw_error;
41163 if (!cp_parser_parse_definitely (parser))
41164 goto saw_error;
41165 switch (token->type)
41167 case CPP_SEMICOLON:
41168 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
41170 code = OMP_ATOMIC_CAPTURE_OLD;
41171 v = lhs;
41172 lhs = NULL_TREE;
41173 lhs1 = rhs1;
41174 rhs1 = NULL_TREE;
41175 cp_lexer_consume_token (parser->lexer);
41176 goto restart;
41178 else if (structured_block && !compare)
41180 opcode = NOP_EXPR;
41181 rhs = rhs1;
41182 rhs1 = NULL_TREE;
41183 goto stmt_done;
41185 cp_parser_error (parser,
41186 "invalid form of %<#pragma omp atomic%>");
41187 goto saw_error;
41188 case CPP_MULT:
41189 opcode = MULT_EXPR;
41190 break;
41191 case CPP_DIV:
41192 opcode = TRUNC_DIV_EXPR;
41193 break;
41194 case CPP_PLUS:
41195 opcode = PLUS_EXPR;
41196 break;
41197 case CPP_MINUS:
41198 opcode = MINUS_EXPR;
41199 break;
41200 case CPP_LSHIFT:
41201 opcode = LSHIFT_EXPR;
41202 break;
41203 case CPP_RSHIFT:
41204 opcode = RSHIFT_EXPR;
41205 break;
41206 case CPP_AND:
41207 opcode = BIT_AND_EXPR;
41208 break;
41209 case CPP_OR:
41210 opcode = BIT_IOR_EXPR;
41211 break;
41212 case CPP_XOR:
41213 opcode = BIT_XOR_EXPR;
41214 break;
41215 case CPP_EQ_EQ:
41216 opcode = EQ_EXPR;
41217 break;
41218 case CPP_GREATER:
41219 opcode = GT_EXPR;
41220 break;
41221 case CPP_LESS:
41222 opcode = LT_EXPR;
41223 break;
41224 default:
41225 cp_parser_error (parser,
41226 "invalid operator for %<#pragma omp atomic%>");
41227 goto saw_error;
41229 if (compare
41230 && TREE_CODE_CLASS (opcode) != tcc_comparison)
41232 cp_parser_error (parser,
41233 "invalid form of "
41234 "%<#pragma omp atomic compare%>");
41235 goto saw_error;
41237 oprec = TOKEN_PRECEDENCE (token);
41238 gcc_assert (oprec != PREC_NOT_OPERATOR);
41239 if (commutative_tree_code (opcode))
41240 oprec = (enum cp_parser_prec) (oprec - 1);
41241 cp_lexer_consume_token (parser->lexer);
41242 rhs = cp_parser_binary_expression (parser, false, false,
41243 oprec, NULL);
41244 if (rhs == error_mark_node)
41245 goto saw_error;
41246 if (compare)
41248 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
41250 cp_parser_error (parser,
41251 "invalid form of "
41252 "%<#pragma omp atomic compare%>");
41253 goto saw_error;
41255 goto cond_expr;
41257 goto stmt_done;
41258 default:
41259 cp_parser_error (parser,
41260 "invalid operator for %<#pragma omp atomic%>");
41261 goto saw_error;
41263 cp_lexer_consume_token (parser->lexer);
41265 rhs = cp_parser_expression (parser);
41266 if (rhs == error_mark_node)
41267 goto saw_error;
41268 break;
41270 stmt_done:
41271 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
41273 if (!no_semicolon
41274 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41275 goto saw_error;
41276 no_semicolon = false;
41277 v = cp_parser_unary_expression (parser);
41278 if (v == error_mark_node)
41279 goto saw_error;
41280 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41281 goto saw_error;
41282 lhs1 = cp_parser_unary_expression (parser);
41283 if (lhs1 == error_mark_node)
41284 goto saw_error;
41286 if (structured_block)
41288 if (!no_semicolon)
41289 cp_parser_consume_semicolon_at_end_of_statement (parser);
41290 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
41292 done:
41293 if (weak && opcode != COND_EXPR)
41295 error_at (loc, "%<weak%> clause requires atomic equality comparison");
41296 weak = false;
41298 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
41299 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
41300 rhs1, r, clauses, memory_order, weak);
41301 if (!structured_block && !no_semicolon)
41302 cp_parser_consume_semicolon_at_end_of_statement (parser);
41303 return;
41305 invalid_compare:
41306 error ("invalid form of %<pragma omp atomic compare%>");
41307 /* FALLTHRU */
41308 saw_error:
41309 cp_parser_skip_to_end_of_block_or_statement (parser);
41310 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41311 cp_lexer_consume_token (parser->lexer);
41312 if (structured_block)
41314 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41315 cp_lexer_consume_token (parser->lexer);
41316 else if (code == OMP_ATOMIC_CAPTURE_NEW)
41318 cp_parser_skip_to_end_of_block_or_statement (parser);
41319 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41320 cp_lexer_consume_token (parser->lexer);
41326 /* OpenMP 2.5:
41327 # pragma omp barrier new-line */
41329 static void
41330 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
41332 cp_parser_require_pragma_eol (parser, pragma_tok);
41333 finish_omp_barrier ();
41336 /* OpenMP 2.5:
41337 # pragma omp critical [(name)] new-line
41338 structured-block
41340 OpenMP 4.5:
41341 # pragma omp critical [(name) [hint(expression)]] new-line
41342 structured-block */
41344 #define OMP_CRITICAL_CLAUSE_MASK \
41345 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
41347 static tree
41348 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41350 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
41352 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41354 matching_parens parens;
41355 parens.consume_open (parser);
41357 name = cp_parser_identifier (parser);
41359 if (name == error_mark_node
41360 || !parens.require_close (parser))
41361 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41362 /*or_comma=*/false,
41363 /*consume_paren=*/true);
41364 if (name == error_mark_node)
41365 name = NULL;
41367 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41368 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41369 cp_lexer_consume_token (parser->lexer);
41372 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
41373 "#pragma omp critical", pragma_tok);
41375 stmt = cp_parser_omp_structured_block (parser, if_p);
41376 return c_finish_omp_critical (input_location, stmt, name, clauses);
41379 /* OpenMP 5.0:
41380 # pragma omp depobj ( depobj ) depobj-clause new-line
41382 depobj-clause:
41383 depend (dependence-type : locator)
41384 destroy
41385 update (dependence-type)
41387 dependence-type:
41390 inout
41391 mutexinout */
41393 static void
41394 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
41396 location_t loc = pragma_tok->location;
41397 matching_parens parens;
41398 if (!parens.require_open (parser))
41400 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41401 return;
41404 tree depobj = cp_parser_assignment_expression (parser);
41406 if (!parens.require_close (parser))
41407 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41408 /*or_comma=*/false,
41409 /*consume_paren=*/true);
41411 tree clause = NULL_TREE;
41412 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
41413 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
41414 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41415 if (parser->lexer->in_omp_attribute_pragma
41416 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41417 cp_lexer_consume_token (parser->lexer);
41418 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41420 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41421 const char *p = IDENTIFIER_POINTER (id);
41423 cp_lexer_consume_token (parser->lexer);
41424 if (!strcmp ("depend", p))
41426 /* Don't create location wrapper nodes within the depend clause. */
41427 auto_suppress_location_wrappers sentinel;
41428 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
41429 if (clause)
41430 clause = finish_omp_clauses (clause, C_ORT_OMP);
41431 if (!clause)
41432 clause = error_mark_node;
41434 else if (!strcmp ("destroy", p))
41435 kind = OMP_CLAUSE_DEPEND_LAST;
41436 else if (!strcmp ("update", p))
41438 matching_parens c_parens;
41439 if (c_parens.require_open (parser))
41441 location_t c2_loc
41442 = cp_lexer_peek_token (parser->lexer)->location;
41443 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41445 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
41446 const char *p2 = IDENTIFIER_POINTER (id2);
41448 cp_lexer_consume_token (parser->lexer);
41449 if (!strcmp ("in", p2))
41450 kind = OMP_CLAUSE_DEPEND_IN;
41451 else if (!strcmp ("out", p2))
41452 kind = OMP_CLAUSE_DEPEND_OUT;
41453 else if (!strcmp ("inout", p2))
41454 kind = OMP_CLAUSE_DEPEND_INOUT;
41455 else if (!strcmp ("mutexinoutset", p2))
41456 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
41458 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
41460 clause = error_mark_node;
41461 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
41462 "%<mutexinoutset%>");
41464 if (!c_parens.require_close (parser))
41465 cp_parser_skip_to_closing_parenthesis (parser,
41466 /*recovering=*/true,
41467 /*or_comma=*/false,
41468 /*consume_paren=*/true);
41470 else
41471 clause = error_mark_node;
41474 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
41476 clause = error_mark_node;
41477 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
41479 cp_parser_require_pragma_eol (parser, pragma_tok);
41481 finish_omp_depobj (loc, depobj, kind, clause);
41485 /* OpenMP 2.5:
41486 # pragma omp flush flush-vars[opt] new-line
41488 flush-vars:
41489 ( variable-list )
41491 OpenMP 5.0:
41492 # pragma omp flush memory-order-clause new-line */
41494 static void
41495 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
41497 enum memmodel mo = MEMMODEL_LAST;
41498 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41499 if (parser->lexer->in_omp_attribute_pragma
41500 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41501 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41502 cp_lexer_consume_token (parser->lexer);
41503 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41505 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41506 const char *p = IDENTIFIER_POINTER (id);
41507 if (!strcmp (p, "seq_cst"))
41508 mo = MEMMODEL_SEQ_CST;
41509 else if (!strcmp (p, "acq_rel"))
41510 mo = MEMMODEL_ACQ_REL;
41511 else if (!strcmp (p, "release"))
41512 mo = MEMMODEL_RELEASE;
41513 else if (!strcmp (p, "acquire"))
41514 mo = MEMMODEL_ACQUIRE;
41515 else
41516 error_at (cp_lexer_peek_token (parser->lexer)->location,
41517 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
41518 "%<acquire%>");
41519 cp_lexer_consume_token (parser->lexer);
41521 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41523 if (mo != MEMMODEL_LAST)
41524 error_at (cp_lexer_peek_token (parser->lexer)->location,
41525 "%<flush%> list specified together with memory order "
41526 "clause");
41527 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
41529 cp_parser_require_pragma_eol (parser, pragma_tok);
41531 finish_omp_flush (mo);
41534 /* Helper function, to parse omp for increment expression. */
41536 static tree
41537 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
41539 tree cond = cp_parser_binary_expression (parser, false, true,
41540 PREC_NOT_OPERATOR, NULL);
41541 if (cond == error_mark_node
41542 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
41544 cp_parser_skip_to_end_of_statement (parser);
41545 return error_mark_node;
41548 switch (TREE_CODE (cond))
41550 case GT_EXPR:
41551 case GE_EXPR:
41552 case LT_EXPR:
41553 case LE_EXPR:
41554 break;
41555 case NE_EXPR:
41556 if (code != OACC_LOOP)
41557 break;
41558 gcc_fallthrough ();
41559 default:
41560 return error_mark_node;
41563 /* If decl is an iterator, preserve LHS and RHS of the relational
41564 expr until finish_omp_for. */
41565 if (decl
41566 && (type_dependent_expression_p (decl)
41567 || CLASS_TYPE_P (TREE_TYPE (decl))))
41568 return cond;
41570 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
41571 TREE_CODE (cond),
41572 TREE_OPERAND (cond, 0), ERROR_MARK,
41573 TREE_OPERAND (cond, 1), ERROR_MARK,
41574 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
41577 /* Helper function, to parse omp for increment expression. */
41579 static tree
41580 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
41582 cp_token *token = cp_lexer_peek_token (parser->lexer);
41583 enum tree_code op;
41584 tree lhs, rhs;
41585 cp_id_kind idk;
41586 bool decl_first;
41588 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
41590 op = (token->type == CPP_PLUS_PLUS
41591 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
41592 cp_lexer_consume_token (parser->lexer);
41593 lhs = cp_parser_simple_cast_expression (parser);
41594 if (lhs != decl
41595 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
41596 return error_mark_node;
41597 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
41600 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
41601 if (lhs != decl
41602 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
41603 return error_mark_node;
41605 token = cp_lexer_peek_token (parser->lexer);
41606 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
41608 op = (token->type == CPP_PLUS_PLUS
41609 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
41610 cp_lexer_consume_token (parser->lexer);
41611 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
41614 op = cp_parser_assignment_operator_opt (parser);
41615 if (op == ERROR_MARK)
41616 return error_mark_node;
41618 if (op != NOP_EXPR)
41620 rhs = cp_parser_assignment_expression (parser);
41621 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
41622 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
41625 lhs = cp_parser_binary_expression (parser, false, false,
41626 PREC_ADDITIVE_EXPRESSION, NULL);
41627 token = cp_lexer_peek_token (parser->lexer);
41628 decl_first = (lhs == decl
41629 || (processing_template_decl && cp_tree_equal (lhs, decl)));
41630 if (decl_first)
41631 lhs = NULL_TREE;
41632 if (token->type != CPP_PLUS
41633 && token->type != CPP_MINUS)
41634 return error_mark_node;
41638 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
41639 cp_lexer_consume_token (parser->lexer);
41640 rhs = cp_parser_binary_expression (parser, false, false,
41641 PREC_ADDITIVE_EXPRESSION, NULL);
41642 token = cp_lexer_peek_token (parser->lexer);
41643 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
41645 if (lhs == NULL_TREE)
41647 if (op == PLUS_EXPR)
41648 lhs = rhs;
41649 else
41650 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
41651 NULL_TREE, tf_warning_or_error);
41653 else
41654 lhs = build_x_binary_op (input_location, op,
41655 lhs, ERROR_MARK,
41656 rhs, ERROR_MARK,
41657 NULL_TREE, NULL, tf_warning_or_error);
41660 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
41662 if (!decl_first)
41664 if ((rhs != decl
41665 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
41666 || op == MINUS_EXPR)
41667 return error_mark_node;
41668 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
41670 else
41671 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
41673 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
41676 /* Parse the initialization statement of an OpenMP for loop.
41678 Return true if the resulting construct should have an
41679 OMP_CLAUSE_PRIVATE added to it. */
41681 static tree
41682 cp_parser_omp_for_loop_init (cp_parser *parser,
41683 tree &this_pre_body,
41684 releasing_vec &for_block,
41685 tree &init,
41686 tree &orig_init,
41687 tree &decl,
41688 tree &real_decl)
41690 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
41691 return NULL_TREE;
41693 tree add_private_clause = NULL_TREE;
41695 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
41697 init-expr:
41698 var = lb
41699 integer-type var = lb
41700 random-access-iterator-type var = lb
41701 pointer-type var = lb
41703 cp_decl_specifier_seq type_specifiers;
41705 /* First, try to parse as an initialized declaration. See
41706 cp_parser_condition, from whence the bulk of this is copied. */
41708 cp_parser_parse_tentatively (parser);
41709 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
41710 /*is_declaration=*/true,
41711 /*is_trailing_return=*/false,
41712 &type_specifiers);
41713 if (cp_parser_parse_definitely (parser))
41715 /* If parsing a type specifier seq succeeded, then this
41716 MUST be a initialized declaration. */
41717 tree asm_specification, attributes;
41718 cp_declarator *declarator;
41720 declarator = cp_parser_declarator (parser,
41721 CP_PARSER_DECLARATOR_NAMED,
41722 CP_PARSER_FLAGS_NONE,
41723 /*ctor_dtor_or_conv_p=*/NULL,
41724 /*parenthesized_p=*/NULL,
41725 /*member_p=*/false,
41726 /*friend_p=*/false,
41727 /*static_p=*/false);
41728 attributes = cp_parser_attributes_opt (parser);
41729 asm_specification = cp_parser_asm_specification_opt (parser);
41731 if (declarator == cp_error_declarator)
41732 cp_parser_skip_to_end_of_statement (parser);
41734 else
41736 tree pushed_scope, auto_node;
41738 decl = start_decl (declarator, &type_specifiers,
41739 SD_INITIALIZED, attributes,
41740 /*prefix_attributes=*/NULL_TREE,
41741 &pushed_scope);
41743 auto_node = type_uses_auto (TREE_TYPE (decl));
41744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
41746 if (cp_lexer_next_token_is (parser->lexer,
41747 CPP_OPEN_PAREN))
41748 error ("parenthesized initialization is not allowed in "
41749 "OpenMP %<for%> loop");
41750 else
41751 /* Trigger an error. */
41752 cp_parser_require (parser, CPP_EQ, RT_EQ);
41754 init = error_mark_node;
41755 cp_parser_skip_to_end_of_statement (parser);
41757 else if (CLASS_TYPE_P (TREE_TYPE (decl))
41758 || type_dependent_expression_p (decl)
41759 || auto_node)
41761 bool is_direct_init, is_non_constant_init;
41763 init = cp_parser_initializer (parser,
41764 &is_direct_init,
41765 &is_non_constant_init);
41767 if (auto_node)
41769 TREE_TYPE (decl)
41770 = do_auto_deduction (TREE_TYPE (decl), init,
41771 auto_node);
41773 if (!CLASS_TYPE_P (TREE_TYPE (decl))
41774 && !type_dependent_expression_p (decl))
41775 goto non_class;
41778 cp_finish_decl (decl, init, !is_non_constant_init,
41779 asm_specification,
41780 LOOKUP_ONLYCONVERTING);
41781 orig_init = init;
41782 if (CLASS_TYPE_P (TREE_TYPE (decl)))
41784 vec_safe_push (for_block, this_pre_body);
41785 init = NULL_TREE;
41787 else
41789 init = pop_stmt_list (this_pre_body);
41790 if (init && TREE_CODE (init) == STATEMENT_LIST)
41792 tree_stmt_iterator i = tsi_start (init);
41793 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
41794 while (!tsi_end_p (i))
41796 tree t = tsi_stmt (i);
41797 if (TREE_CODE (t) == DECL_EXPR
41798 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
41800 tsi_delink (&i);
41801 vec_safe_push (for_block, t);
41802 continue;
41804 break;
41806 if (tsi_one_before_end_p (i))
41808 tree t = tsi_stmt (i);
41809 tsi_delink (&i);
41810 free_stmt_list (init);
41811 init = t;
41815 this_pre_body = NULL_TREE;
41817 else
41819 /* Consume '='. */
41820 cp_lexer_consume_token (parser->lexer);
41821 init = cp_parser_assignment_expression (parser);
41823 non_class:
41824 if (TYPE_REF_P (TREE_TYPE (decl)))
41825 init = error_mark_node;
41826 else
41827 cp_finish_decl (decl, NULL_TREE,
41828 /*init_const_expr_p=*/false,
41829 asm_specification,
41830 LOOKUP_ONLYCONVERTING);
41833 if (pushed_scope)
41834 pop_scope (pushed_scope);
41837 else
41839 cp_id_kind idk;
41840 /* If parsing a type specifier sequence failed, then
41841 this MUST be a simple expression. */
41842 cp_parser_parse_tentatively (parser);
41843 decl = cp_parser_primary_expression (parser, false, false,
41844 false, &idk);
41845 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
41846 if (!cp_parser_error_occurred (parser)
41847 && decl
41848 && (TREE_CODE (decl) == COMPONENT_REF
41849 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
41851 cp_parser_abort_tentative_parse (parser);
41852 cp_parser_parse_tentatively (parser);
41853 cp_token *token = cp_lexer_peek_token (parser->lexer);
41854 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
41855 /*check_dependency_p=*/true,
41856 /*template_p=*/NULL,
41857 /*declarator_p=*/false,
41858 /*optional_p=*/false);
41859 if (name != error_mark_node
41860 && last_tok == cp_lexer_peek_token (parser->lexer))
41862 decl = cp_parser_lookup_name_simple (parser, name,
41863 token->location);
41864 if (TREE_CODE (decl) == FIELD_DECL)
41865 add_private_clause = omp_privatize_field (decl, false);
41867 cp_parser_abort_tentative_parse (parser);
41868 cp_parser_parse_tentatively (parser);
41869 decl = cp_parser_primary_expression (parser, false, false,
41870 false, &idk);
41872 if (!cp_parser_error_occurred (parser)
41873 && decl
41874 && DECL_P (decl)
41875 && CLASS_TYPE_P (TREE_TYPE (decl)))
41877 tree rhs;
41879 cp_parser_parse_definitely (parser);
41880 cp_parser_require (parser, CPP_EQ, RT_EQ);
41881 rhs = cp_parser_assignment_expression (parser);
41882 orig_init = rhs;
41883 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
41884 decl, NOP_EXPR,
41885 rhs, NULL_TREE,
41886 tf_warning_or_error));
41887 if (!add_private_clause)
41888 add_private_clause = decl;
41890 else
41892 decl = NULL;
41893 cp_parser_abort_tentative_parse (parser);
41894 init = cp_parser_expression (parser);
41895 if (init)
41897 if (TREE_CODE (init) == MODIFY_EXPR
41898 || TREE_CODE (init) == MODOP_EXPR)
41899 real_decl = TREE_OPERAND (init, 0);
41903 return add_private_clause;
41906 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
41908 void
41909 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
41910 tree &decl, tree &orig_decl, tree &init,
41911 tree &orig_init, tree &cond, tree &incr)
41913 tree begin, end, range_temp_decl = NULL_TREE;
41914 tree iter_type, begin_expr, end_expr;
41916 if (processing_template_decl)
41918 if (check_for_bare_parameter_packs (init))
41919 init = error_mark_node;
41920 if (!type_dependent_expression_p (init)
41921 /* do_auto_deduction doesn't mess with template init-lists. */
41922 && !BRACE_ENCLOSED_INITIALIZER_P (init))
41924 tree d = decl;
41925 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
41927 tree v = DECL_VALUE_EXPR (decl);
41928 if (TREE_CODE (v) == ARRAY_REF
41929 && VAR_P (TREE_OPERAND (v, 0))
41930 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
41931 d = TREE_OPERAND (v, 0);
41933 do_range_for_auto_deduction (d, init);
41935 cond = global_namespace;
41936 incr = NULL_TREE;
41937 orig_init = init;
41938 if (this_pre_body)
41939 this_pre_body = pop_stmt_list (this_pre_body);
41940 return;
41943 init = mark_lvalue_use (init);
41945 if (decl == error_mark_node || init == error_mark_node)
41946 /* If an error happened previously do nothing or else a lot of
41947 unhelpful errors would be issued. */
41948 begin_expr = end_expr = iter_type = error_mark_node;
41949 else
41951 tree range_temp;
41953 if (VAR_P (init)
41954 && array_of_runtime_bound_p (TREE_TYPE (init)))
41955 /* Can't bind a reference to an array of runtime bound. */
41956 range_temp = init;
41957 else
41959 range_temp = build_range_temp (init);
41960 DECL_NAME (range_temp) = NULL_TREE;
41961 pushdecl (range_temp);
41962 cp_finish_decl (range_temp, init,
41963 /*is_constant_init*/false, NULL_TREE,
41964 LOOKUP_ONLYCONVERTING);
41965 range_temp_decl = range_temp;
41966 range_temp = convert_from_reference (range_temp);
41968 iter_type = cp_parser_perform_range_for_lookup (range_temp,
41969 &begin_expr, &end_expr);
41972 tree end_iter_type = iter_type;
41973 if (cxx_dialect >= cxx17)
41974 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
41975 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
41976 TREE_USED (end) = 1;
41977 DECL_ARTIFICIAL (end) = 1;
41978 pushdecl (end);
41979 cp_finish_decl (end, end_expr,
41980 /*is_constant_init*/false, NULL_TREE,
41981 LOOKUP_ONLYCONVERTING);
41983 /* The new for initialization statement. */
41984 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
41985 TREE_USED (begin) = 1;
41986 DECL_ARTIFICIAL (begin) = 1;
41987 pushdecl (begin);
41988 orig_init = init;
41989 if (CLASS_TYPE_P (iter_type))
41990 init = NULL_TREE;
41991 else
41993 init = begin_expr;
41994 begin_expr = NULL_TREE;
41996 cp_finish_decl (begin, begin_expr,
41997 /*is_constant_init*/false, NULL_TREE,
41998 LOOKUP_ONLYCONVERTING);
42000 /* The new for condition. */
42001 if (CLASS_TYPE_P (iter_type))
42002 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
42003 else
42004 cond = build_x_binary_op (input_location, NE_EXPR,
42005 begin, ERROR_MARK,
42006 end, ERROR_MARK,
42007 NULL_TREE, NULL, tf_warning_or_error);
42009 /* The new increment expression. */
42010 if (CLASS_TYPE_P (iter_type))
42011 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
42012 else
42013 incr = finish_unary_op_expr (input_location,
42014 PREINCREMENT_EXPR, begin,
42015 tf_warning_or_error);
42017 orig_decl = decl;
42018 decl = begin;
42019 if (for_block)
42021 vec_safe_push (for_block, this_pre_body);
42022 this_pre_body = NULL_TREE;
42025 tree decomp_first_name = NULL_TREE;
42026 unsigned decomp_cnt = 0;
42027 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
42029 tree v = DECL_VALUE_EXPR (orig_decl);
42030 if (TREE_CODE (v) == ARRAY_REF
42031 && VAR_P (TREE_OPERAND (v, 0))
42032 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
42034 tree d = orig_decl;
42035 orig_decl = TREE_OPERAND (v, 0);
42036 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
42037 decomp_first_name = d;
42041 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
42042 if (auto_node)
42044 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42045 NULL_TREE, tf_none);
42046 if (!error_operand_p (t))
42047 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
42048 t, auto_node);
42051 tree v = make_tree_vec (decomp_cnt + 3);
42052 TREE_VEC_ELT (v, 0) = range_temp_decl;
42053 TREE_VEC_ELT (v, 1) = end;
42054 TREE_VEC_ELT (v, 2) = orig_decl;
42055 for (unsigned i = 0; i < decomp_cnt; i++)
42057 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
42058 decomp_first_name = DECL_CHAIN (decomp_first_name);
42060 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
42063 /* Helper for cp_parser_omp_for_loop, finalize part of range for
42064 inside of the collapsed body. */
42066 void
42067 cp_finish_omp_range_for (tree orig, tree begin)
42069 gcc_assert (TREE_CODE (orig) == TREE_LIST
42070 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
42071 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
42072 tree decomp_first_name = NULL_TREE;
42073 unsigned int decomp_cnt = 0;
42075 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42077 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
42078 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
42079 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
42082 /* The declaration is initialized with *__begin inside the loop body. */
42083 cp_finish_decl (decl,
42084 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42085 NULL_TREE, tf_warning_or_error),
42086 /*is_constant_init*/false, NULL_TREE,
42087 LOOKUP_ONLYCONVERTING);
42088 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42089 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
42092 /* Return true if next tokens contain a standard attribute that contains
42093 omp::directive (DIRECTIVE). */
42095 static bool
42096 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
42097 bool tentative)
42099 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
42100 if (n < 10)
42101 return false;
42102 for (i = 5; i < n - 4; i++)
42103 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
42104 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
42105 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
42107 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
42108 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
42109 if (strcmp (IDENTIFIER_POINTER (first), "directive"))
42110 continue;
42111 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
42112 break;
42114 if (i == n - 4)
42115 return false;
42116 cp_parser_parse_tentatively (parser);
42117 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
42118 location_t last_loc
42119 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
42120 location_t middle_loc = UNKNOWN_LOCATION;
42121 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
42122 int cnt = 0;
42123 bool seen = false;
42124 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
42125 if (get_attribute_namespace (attr) == omp_identifier
42126 && is_attribute_p ("directive", get_attribute_name (attr)))
42128 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
42130 tree d = TREE_VALUE (a);
42131 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
42132 cp_token *first = DEFPARSE_TOKENS (d)->first;
42133 cnt++;
42134 if (first->type == CPP_NAME
42135 && strcmp (IDENTIFIER_POINTER (first->u.value),
42136 directive) == 0)
42138 seen = true;
42139 if (middle_loc == UNKNOWN_LOCATION)
42140 middle_loc = first->location;
42144 if (!seen || tentative)
42146 cp_parser_abort_tentative_parse (parser);
42147 return seen;
42149 if (cnt != 1 || TREE_CHAIN (std_attrs))
42151 error_at (make_location (first_loc, last_loc, middle_loc),
42152 "%<[[omp::directive(%s)]]%> must be the only specified "
42153 "attribute on a statement", directive);
42154 cp_parser_abort_tentative_parse (parser);
42155 return false;
42157 if (!cp_parser_parse_definitely (parser))
42158 return false;
42159 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
42160 return true;
42163 /* Parse an OpenMP structured block sequence. KIND is the corresponding
42164 separating directive. */
42166 static tree
42167 cp_parser_omp_structured_block_sequence (cp_parser *parser,
42168 enum pragma_kind kind)
42170 tree stmt = begin_omp_structured_block ();
42171 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42173 cp_parser_statement (parser, NULL_TREE, false, NULL);
42174 while (true)
42176 cp_token *token = cp_lexer_peek_token (parser->lexer);
42178 if (token->type == CPP_CLOSE_BRACE
42179 || token->type == CPP_EOF
42180 || token->type == CPP_PRAGMA_EOL
42181 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
42182 || (kind != PRAGMA_NONE
42183 && cp_parser_pragma_kind (token) == kind))
42184 break;
42186 if (kind != PRAGMA_NONE
42187 && cp_parser_omp_section_scan (parser,
42188 kind == PRAGMA_OMP_SCAN
42189 ? "scan" : "section", false))
42190 break;
42192 cp_parser_statement (parser, NULL_TREE, false, NULL);
42195 cp_parser_end_omp_structured_block (parser, save);
42196 return finish_omp_structured_block (stmt);
42200 /* OpenMP 5.0:
42202 scan-loop-body:
42203 { structured-block scan-directive structured-block } */
42205 static void
42206 cp_parser_omp_scan_loop_body (cp_parser *parser)
42208 tree substmt, clauses = NULL_TREE;
42210 matching_braces braces;
42211 if (!braces.require_open (parser))
42212 return;
42214 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
42215 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
42216 add_stmt (substmt);
42218 cp_token *tok = cp_lexer_peek_token (parser->lexer);
42219 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
42221 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
42223 cp_lexer_consume_token (parser->lexer);
42225 if (parser->lexer->in_omp_attribute_pragma
42226 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42227 cp_lexer_consume_token (parser->lexer);
42229 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42231 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42232 const char *p = IDENTIFIER_POINTER (id);
42233 if (strcmp (p, "inclusive") == 0)
42234 clause = OMP_CLAUSE_INCLUSIVE;
42235 else if (strcmp (p, "exclusive") == 0)
42236 clause = OMP_CLAUSE_EXCLUSIVE;
42238 if (clause != OMP_CLAUSE_ERROR)
42240 cp_lexer_consume_token (parser->lexer);
42241 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
42243 else
42244 cp_parser_error (parser, "expected %<inclusive%> or "
42245 "%<exclusive%> clause");
42247 cp_parser_require_pragma_eol (parser, tok);
42249 else
42250 error ("expected %<#pragma omp scan%>");
42252 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42253 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
42254 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
42255 clauses);
42256 add_stmt (substmt);
42258 braces.require_close (parser);
42261 /* Parse the restricted form of the for statement allowed by OpenMP. */
42263 static tree
42264 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
42265 tree *cclauses, bool *if_p)
42267 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
42268 tree orig_decl;
42269 tree real_decl, initv, condv, incrv, declv, orig_declv;
42270 tree this_pre_body, cl, ordered_cl = NULL_TREE;
42271 location_t loc_first;
42272 bool collapse_err = false;
42273 int i, collapse = 1, ordered = 0, count, nbraces = 0;
42274 releasing_vec for_block;
42275 auto_vec<tree, 4> orig_inits;
42276 bool tiling = false;
42277 bool inscan = false;
42279 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
42280 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
42281 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
42282 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
42284 tiling = true;
42285 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
42287 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
42288 && OMP_CLAUSE_ORDERED_EXPR (cl))
42290 ordered_cl = cl;
42291 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
42293 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
42294 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
42295 && (code == OMP_SIMD || code == OMP_FOR))
42296 inscan = true;
42298 if (ordered && ordered < collapse)
42300 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42301 "%<ordered%> clause parameter is less than %<collapse%>");
42302 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
42303 = build_int_cst (NULL_TREE, collapse);
42304 ordered = collapse;
42306 if (ordered)
42308 for (tree *pc = &clauses; *pc; )
42309 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
42311 error_at (OMP_CLAUSE_LOCATION (*pc),
42312 "%<linear%> clause may not be specified together "
42313 "with %<ordered%> clause with a parameter");
42314 *pc = OMP_CLAUSE_CHAIN (*pc);
42316 else
42317 pc = &OMP_CLAUSE_CHAIN (*pc);
42320 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
42321 count = ordered ? ordered : collapse;
42323 declv = make_tree_vec (count);
42324 initv = make_tree_vec (count);
42325 condv = make_tree_vec (count);
42326 incrv = make_tree_vec (count);
42327 orig_declv = NULL_TREE;
42329 loc_first = cp_lexer_peek_token (parser->lexer)->location;
42331 for (i = 0; i < count; i++)
42333 int bracecount = 0;
42334 tree add_private_clause = NULL_TREE;
42335 location_t loc;
42337 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42339 if (!collapse_err)
42340 cp_parser_error (parser, "for statement expected");
42341 return NULL;
42343 loc = cp_lexer_consume_token (parser->lexer)->location;
42345 /* Don't create location wrapper nodes within an OpenMP "for"
42346 statement. */
42347 auto_suppress_location_wrappers sentinel;
42349 matching_parens parens;
42350 if (!parens.require_open (parser))
42351 return NULL;
42353 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
42354 this_pre_body = push_stmt_list ();
42356 if (code != OACC_LOOP && cxx_dialect >= cxx11)
42358 /* Save tokens so that we can put them back. */
42359 cp_lexer_save_tokens (parser->lexer);
42361 /* Look for ':' that is not nested in () or {}. */
42362 bool is_range_for
42363 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
42364 /*recovering=*/false,
42365 CPP_COLON,
42366 /*consume_paren=*/
42367 false) == -1);
42369 /* Roll back the tokens we skipped. */
42370 cp_lexer_rollback_tokens (parser->lexer);
42372 if (is_range_for)
42374 bool saved_colon_corrects_to_scope_p
42375 = parser->colon_corrects_to_scope_p;
42377 /* A colon is used in range-based for. */
42378 parser->colon_corrects_to_scope_p = false;
42380 /* Parse the declaration. */
42381 cp_parser_simple_declaration (parser,
42382 /*function_definition_allowed_p=*/
42383 false, &decl);
42384 parser->colon_corrects_to_scope_p
42385 = saved_colon_corrects_to_scope_p;
42387 cp_parser_require (parser, CPP_COLON, RT_COLON);
42389 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
42390 false, 0, true);
42392 cp_convert_omp_range_for (this_pre_body, for_block, decl,
42393 orig_decl, init, orig_init,
42394 cond, incr);
42395 if (this_pre_body)
42397 if (pre_body)
42399 tree t = pre_body;
42400 pre_body = push_stmt_list ();
42401 add_stmt (t);
42402 add_stmt (this_pre_body);
42403 pre_body = pop_stmt_list (pre_body);
42405 else
42406 pre_body = this_pre_body;
42409 if (ordered_cl)
42410 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42411 "%<ordered%> clause with parameter on "
42412 "range-based %<for%> loop");
42414 goto parse_close_paren;
42418 add_private_clause
42419 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
42420 init, orig_init, decl, real_decl);
42422 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42423 if (this_pre_body)
42425 this_pre_body = pop_stmt_list (this_pre_body);
42426 if (pre_body)
42428 tree t = pre_body;
42429 pre_body = push_stmt_list ();
42430 add_stmt (t);
42431 add_stmt (this_pre_body);
42432 pre_body = pop_stmt_list (pre_body);
42434 else
42435 pre_body = this_pre_body;
42438 if (decl)
42439 real_decl = decl;
42440 if (cclauses != NULL
42441 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
42442 && real_decl != NULL_TREE
42443 && code != OMP_LOOP)
42445 tree *c;
42446 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
42447 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
42448 && OMP_CLAUSE_DECL (*c) == real_decl)
42450 error_at (loc, "iteration variable %qD"
42451 " should not be firstprivate", real_decl);
42452 *c = OMP_CLAUSE_CHAIN (*c);
42454 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
42455 && OMP_CLAUSE_DECL (*c) == real_decl)
42457 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
42458 tree l = *c;
42459 *c = OMP_CLAUSE_CHAIN (*c);
42460 if (code == OMP_SIMD)
42462 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
42463 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
42465 else
42467 OMP_CLAUSE_CHAIN (l) = clauses;
42468 clauses = l;
42470 add_private_clause = NULL_TREE;
42472 else
42474 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
42475 && OMP_CLAUSE_DECL (*c) == real_decl)
42476 add_private_clause = NULL_TREE;
42477 c = &OMP_CLAUSE_CHAIN (*c);
42481 if (add_private_clause)
42483 tree c;
42484 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
42486 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
42487 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
42488 && OMP_CLAUSE_DECL (c) == decl)
42489 break;
42490 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
42491 && OMP_CLAUSE_DECL (c) == decl)
42492 error_at (loc, "iteration variable %qD "
42493 "should not be firstprivate",
42494 decl);
42495 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
42496 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
42497 && OMP_CLAUSE_DECL (c) == decl)
42498 error_at (loc, "iteration variable %qD should not be reduction",
42499 decl);
42501 if (c == NULL)
42503 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
42504 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
42505 else if (code != OMP_SIMD)
42506 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
42507 else
42508 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
42509 OMP_CLAUSE_DECL (c) = add_private_clause;
42510 c = finish_omp_clauses (c, C_ORT_OMP);
42511 if (c)
42513 OMP_CLAUSE_CHAIN (c) = clauses;
42514 clauses = c;
42515 /* For linear, signal that we need to fill up
42516 the so far unknown linear step. */
42517 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
42518 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
42523 cond = NULL;
42524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
42525 cond = cp_parser_omp_for_cond (parser, decl, code);
42526 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42528 incr = NULL;
42529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
42531 /* If decl is an iterator, preserve the operator on decl
42532 until finish_omp_for. */
42533 if (real_decl
42534 && ((processing_template_decl
42535 && (TREE_TYPE (real_decl) == NULL_TREE
42536 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
42537 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
42538 incr = cp_parser_omp_for_incr (parser, real_decl);
42539 else
42540 incr = cp_parser_expression (parser);
42541 protected_set_expr_location_if_unset (incr, input_location);
42544 parse_close_paren:
42545 if (!parens.require_close (parser))
42546 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42547 /*or_comma=*/false,
42548 /*consume_paren=*/true);
42550 TREE_VEC_ELT (declv, i) = decl;
42551 TREE_VEC_ELT (initv, i) = init;
42552 TREE_VEC_ELT (condv, i) = cond;
42553 TREE_VEC_ELT (incrv, i) = incr;
42554 if (orig_init)
42556 orig_inits.safe_grow_cleared (i + 1, true);
42557 orig_inits[i] = orig_init;
42559 if (orig_decl)
42561 if (!orig_declv)
42562 orig_declv = copy_node (declv);
42563 TREE_VEC_ELT (orig_declv, i) = orig_decl;
42565 else if (orig_declv)
42566 TREE_VEC_ELT (orig_declv, i) = decl;
42568 if (i == count - 1)
42569 break;
42571 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
42572 in between the collapsed for loops to be still considered perfectly
42573 nested. Hopefully the final version clarifies this.
42574 For now handle (multiple) {'s and empty statements. */
42575 cp_parser_parse_tentatively (parser);
42576 for (;;)
42578 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42579 break;
42580 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42582 cp_lexer_consume_token (parser->lexer);
42583 bracecount++;
42585 else if (bracecount
42586 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42587 cp_lexer_consume_token (parser->lexer);
42588 else
42590 loc = cp_lexer_peek_token (parser->lexer)->location;
42591 error_at (loc, "not enough for loops to collapse");
42592 collapse_err = true;
42593 cp_parser_abort_tentative_parse (parser);
42594 declv = NULL_TREE;
42595 break;
42599 if (declv)
42601 cp_parser_parse_definitely (parser);
42602 nbraces += bracecount;
42606 if (nbraces)
42607 if_p = NULL;
42609 /* Note that we saved the original contents of this flag when we entered
42610 the structured block, and so we don't need to re-save it here. */
42611 parser->in_statement = IN_OMP_FOR;
42613 /* Note that the grammar doesn't call for a structured block here,
42614 though the loop as a whole is a structured block. */
42615 if (orig_declv)
42617 body = begin_omp_structured_block ();
42618 for (i = 0; i < count; i++)
42619 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
42620 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
42621 TREE_VEC_ELT (declv, i));
42623 else
42624 body = push_stmt_list ();
42625 if (inscan)
42626 cp_parser_omp_scan_loop_body (parser);
42627 else
42628 cp_parser_statement (parser, NULL_TREE, false, if_p);
42629 if (orig_declv)
42630 body = finish_omp_structured_block (body);
42631 else
42632 body = pop_stmt_list (body);
42634 if (declv == NULL_TREE)
42635 ret = NULL_TREE;
42636 else
42637 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
42638 incrv, body, pre_body, &orig_inits, clauses);
42640 while (nbraces)
42642 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42644 cp_lexer_consume_token (parser->lexer);
42645 nbraces--;
42647 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42648 cp_lexer_consume_token (parser->lexer);
42649 else
42651 if (!collapse_err)
42653 error_at (cp_lexer_peek_token (parser->lexer)->location,
42654 "collapsed loops not perfectly nested");
42656 collapse_err = true;
42657 cp_parser_statement_seq_opt (parser, NULL);
42658 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
42659 break;
42663 while (!for_block->is_empty ())
42665 tree t = for_block->pop ();
42666 if (TREE_CODE (t) == STATEMENT_LIST)
42667 add_stmt (pop_stmt_list (t));
42668 else
42669 add_stmt (t);
42672 return ret;
42675 /* Helper function for OpenMP parsing, split clauses and call
42676 finish_omp_clauses on each of the set of clauses afterwards. */
42678 static void
42679 cp_omp_split_clauses (location_t loc, enum tree_code code,
42680 omp_clause_mask mask, tree clauses, tree *cclauses)
42682 int i;
42683 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
42684 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
42685 if (cclauses[i])
42686 cclauses[i] = finish_omp_clauses (cclauses[i],
42687 i == C_OMP_CLAUSE_SPLIT_TARGET
42688 ? C_ORT_OMP_TARGET : C_ORT_OMP);
42691 /* OpenMP 5.0:
42692 #pragma omp loop loop-clause[optseq] new-line
42693 for-loop */
42695 #define OMP_LOOP_CLAUSE_MASK \
42696 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
42701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42703 static tree
42704 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
42705 char *p_name, omp_clause_mask mask, tree *cclauses,
42706 bool *if_p)
42708 tree clauses, sb, ret;
42709 unsigned int save;
42710 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42712 strcat (p_name, " loop");
42713 mask |= OMP_LOOP_CLAUSE_MASK;
42715 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42716 cclauses == NULL);
42717 if (cclauses)
42719 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
42720 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
42723 keep_next_level (true);
42724 sb = begin_omp_structured_block ();
42725 save = cp_parser_begin_omp_structured_block (parser);
42727 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
42729 cp_parser_end_omp_structured_block (parser, save);
42730 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42732 return ret;
42735 /* OpenMP 4.0:
42736 #pragma omp simd simd-clause[optseq] new-line
42737 for-loop */
42739 #define OMP_SIMD_CLAUSE_MASK \
42740 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
42741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
42742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
42743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
42744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
42749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
42750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42752 static tree
42753 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
42754 char *p_name, omp_clause_mask mask, tree *cclauses,
42755 bool *if_p)
42757 tree clauses, sb, ret;
42758 unsigned int save;
42759 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42761 strcat (p_name, " simd");
42762 mask |= OMP_SIMD_CLAUSE_MASK;
42764 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42765 cclauses == NULL);
42766 if (cclauses)
42768 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
42769 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
42770 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
42771 OMP_CLAUSE_ORDERED);
42772 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
42774 error_at (OMP_CLAUSE_LOCATION (c),
42775 "%<ordered%> clause with parameter may not be specified "
42776 "on %qs construct", p_name);
42777 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
42781 keep_next_level (true);
42782 sb = begin_omp_structured_block ();
42783 save = cp_parser_begin_omp_structured_block (parser);
42785 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
42787 cp_parser_end_omp_structured_block (parser, save);
42788 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42790 return ret;
42793 /* OpenMP 2.5:
42794 #pragma omp for for-clause[optseq] new-line
42795 for-loop
42797 OpenMP 4.0:
42798 #pragma omp for simd for-simd-clause[optseq] new-line
42799 for-loop */
42801 #define OMP_FOR_CLAUSE_MASK \
42802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
42804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
42806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
42808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
42809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
42810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
42812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42814 static tree
42815 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
42816 char *p_name, omp_clause_mask mask, tree *cclauses,
42817 bool *if_p)
42819 tree clauses, sb, ret;
42820 unsigned int save;
42821 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42823 strcat (p_name, " for");
42824 mask |= OMP_FOR_CLAUSE_MASK;
42825 /* parallel for{, simd} disallows nowait clause, but for
42826 target {teams distribute ,}parallel for{, simd} it should be accepted. */
42827 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
42828 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
42829 /* Composite distribute parallel for{, simd} disallows ordered clause. */
42830 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
42831 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
42833 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42835 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42836 const char *p = IDENTIFIER_POINTER (id);
42838 if (strcmp (p, "simd") == 0)
42840 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
42841 if (cclauses == NULL)
42842 cclauses = cclauses_buf;
42844 cp_lexer_consume_token (parser->lexer);
42845 if (!flag_openmp) /* flag_openmp_simd */
42846 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
42847 cclauses, if_p);
42848 sb = begin_omp_structured_block ();
42849 save = cp_parser_begin_omp_structured_block (parser);
42850 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
42851 cclauses, if_p);
42852 cp_parser_end_omp_structured_block (parser, save);
42853 tree body = finish_omp_structured_block (sb);
42854 if (ret == NULL)
42855 return ret;
42856 ret = make_node (OMP_FOR);
42857 TREE_TYPE (ret) = void_type_node;
42858 OMP_FOR_BODY (ret) = body;
42859 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
42860 SET_EXPR_LOCATION (ret, loc);
42861 add_stmt (ret);
42862 return ret;
42865 if (!flag_openmp) /* flag_openmp_simd */
42867 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42868 return NULL_TREE;
42871 /* Composite distribute parallel for disallows linear clause. */
42872 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
42873 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
42875 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42876 cclauses == NULL);
42877 if (cclauses)
42879 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
42880 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
42883 keep_next_level (true);
42884 sb = begin_omp_structured_block ();
42885 save = cp_parser_begin_omp_structured_block (parser);
42887 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
42889 cp_parser_end_omp_structured_block (parser, save);
42890 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42892 return ret;
42895 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
42896 omp_clause_mask, tree *, bool *);
42898 /* OpenMP 2.5:
42899 # pragma omp master new-line
42900 structured-block */
42902 static tree
42903 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
42904 char *p_name, omp_clause_mask mask, tree *cclauses,
42905 bool *if_p)
42907 tree clauses, sb, ret;
42908 unsigned int save;
42909 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42911 strcat (p_name, " master");
42913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42915 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42916 const char *p = IDENTIFIER_POINTER (id);
42918 if (strcmp (p, "taskloop") == 0)
42920 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
42921 if (cclauses == NULL)
42922 cclauses = cclauses_buf;
42924 cp_lexer_consume_token (parser->lexer);
42925 if (!flag_openmp) /* flag_openmp_simd */
42926 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
42927 cclauses, if_p);
42928 sb = begin_omp_structured_block ();
42929 save = cp_parser_begin_omp_structured_block (parser);
42930 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
42931 cclauses, if_p);
42932 cp_parser_end_omp_structured_block (parser, save);
42933 tree body = finish_omp_structured_block (sb);
42934 if (ret == NULL)
42935 return ret;
42936 ret = c_finish_omp_master (loc, body);
42937 OMP_MASTER_COMBINED (ret) = 1;
42938 return ret;
42941 if (!flag_openmp) /* flag_openmp_simd */
42943 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42944 return NULL_TREE;
42947 if (cclauses)
42949 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42950 false);
42951 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
42953 else
42954 cp_parser_require_pragma_eol (parser, pragma_tok);
42956 return c_finish_omp_master (loc,
42957 cp_parser_omp_structured_block (parser, if_p));
42960 /* OpenMP 5.1:
42961 # pragma omp masked masked-clauses new-line
42962 structured-block */
42964 #define OMP_MASKED_CLAUSE_MASK \
42965 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
42967 static tree
42968 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
42969 char *p_name, omp_clause_mask mask, tree *cclauses,
42970 bool *if_p)
42972 tree clauses, sb, ret;
42973 unsigned int save;
42974 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42976 strcat (p_name, " masked");
42977 mask |= OMP_MASKED_CLAUSE_MASK;
42979 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42981 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42982 const char *p = IDENTIFIER_POINTER (id);
42984 if (strcmp (p, "taskloop") == 0)
42986 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
42987 if (cclauses == NULL)
42988 cclauses = cclauses_buf;
42990 cp_lexer_consume_token (parser->lexer);
42991 if (!flag_openmp) /* flag_openmp_simd */
42992 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
42993 cclauses, if_p);
42994 sb = begin_omp_structured_block ();
42995 save = cp_parser_begin_omp_structured_block (parser);
42996 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
42997 cclauses, if_p);
42998 cp_parser_end_omp_structured_block (parser, save);
42999 tree body = finish_omp_structured_block (sb);
43000 if (ret == NULL)
43001 return ret;
43002 ret = c_finish_omp_masked (loc, body,
43003 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
43004 OMP_MASKED_COMBINED (ret) = 1;
43005 return ret;
43008 if (!flag_openmp) /* flag_openmp_simd */
43010 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43011 return NULL_TREE;
43014 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43015 cclauses == NULL);
43016 if (cclauses)
43018 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
43019 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
43022 return c_finish_omp_masked (loc,
43023 cp_parser_omp_structured_block (parser, if_p),
43024 clauses);
43027 /* OpenMP 2.5:
43028 # pragma omp ordered new-line
43029 structured-block
43031 OpenMP 4.5:
43032 # pragma omp ordered ordered-clauses new-line
43033 structured-block */
43035 #define OMP_ORDERED_CLAUSE_MASK \
43036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
43037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
43039 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
43040 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
43042 static bool
43043 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
43044 enum pragma_context context, bool *if_p)
43046 location_t loc = pragma_tok->location;
43047 int n = 1;
43049 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
43050 if (parser->lexer->in_omp_attribute_pragma
43051 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43052 n = 2;
43054 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
43056 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
43057 const char *p = IDENTIFIER_POINTER (id);
43059 if (strcmp (p, "depend") == 0)
43061 if (!flag_openmp) /* flag_openmp_simd */
43063 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43064 return false;
43066 if (context == pragma_stmt)
43068 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
43069 "%<depend%> clause may only be used in compound "
43070 "statements");
43071 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43072 return true;
43074 tree clauses
43075 = cp_parser_omp_all_clauses (parser,
43076 OMP_ORDERED_DEPEND_CLAUSE_MASK,
43077 "#pragma omp ordered", pragma_tok);
43078 c_finish_omp_ordered (loc, clauses, NULL_TREE);
43079 return false;
43083 tree clauses
43084 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
43085 "#pragma omp ordered", pragma_tok);
43087 if (!flag_openmp /* flag_openmp_simd */
43088 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
43089 return false;
43091 c_finish_omp_ordered (loc, clauses,
43092 cp_parser_omp_structured_block (parser, if_p));
43093 return true;
43096 /* OpenMP 2.5:
43098 section-scope:
43099 { section-sequence }
43101 section-sequence:
43102 section-directive[opt] structured-block
43103 section-sequence section-directive structured-block */
43105 static tree
43106 cp_parser_omp_sections_scope (cp_parser *parser)
43108 tree stmt, substmt;
43109 bool error_suppress = false;
43110 cp_token *tok;
43112 matching_braces braces;
43113 if (!braces.require_open (parser))
43114 return NULL_TREE;
43116 stmt = push_stmt_list ();
43118 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
43119 != PRAGMA_OMP_SECTION
43120 && !cp_parser_omp_section_scan (parser, "section", true))
43122 substmt = cp_parser_omp_structured_block_sequence (parser,
43123 PRAGMA_OMP_SECTION);
43124 substmt = build1 (OMP_SECTION, void_type_node, substmt);
43125 add_stmt (substmt);
43128 while (1)
43130 tok = cp_lexer_peek_token (parser->lexer);
43131 if (tok->type == CPP_CLOSE_BRACE)
43132 break;
43133 if (tok->type == CPP_EOF)
43134 break;
43136 if (cp_parser_omp_section_scan (parser, "section", false))
43137 tok = cp_lexer_peek_token (parser->lexer);
43138 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
43140 cp_lexer_consume_token (parser->lexer);
43141 cp_parser_require_pragma_eol (parser, tok);
43142 error_suppress = false;
43144 else if (!error_suppress)
43146 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
43147 error_suppress = true;
43150 substmt = cp_parser_omp_structured_block_sequence (parser,
43151 PRAGMA_OMP_SECTION);
43152 substmt = build1 (OMP_SECTION, void_type_node, substmt);
43153 add_stmt (substmt);
43155 braces.require_close (parser);
43157 substmt = pop_stmt_list (stmt);
43159 stmt = make_node (OMP_SECTIONS);
43160 TREE_TYPE (stmt) = void_type_node;
43161 OMP_SECTIONS_BODY (stmt) = substmt;
43163 add_stmt (stmt);
43164 return stmt;
43167 /* OpenMP 2.5:
43168 # pragma omp sections sections-clause[optseq] newline
43169 sections-scope */
43171 #define OMP_SECTIONS_CLAUSE_MASK \
43172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43179 static tree
43180 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
43181 char *p_name, omp_clause_mask mask, tree *cclauses)
43183 tree clauses, ret;
43184 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43186 strcat (p_name, " sections");
43187 mask |= OMP_SECTIONS_CLAUSE_MASK;
43188 if (cclauses)
43189 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
43191 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43192 cclauses == NULL);
43193 if (cclauses)
43195 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
43196 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
43199 ret = cp_parser_omp_sections_scope (parser);
43200 if (ret)
43201 OMP_SECTIONS_CLAUSES (ret) = clauses;
43203 return ret;
43206 /* OpenMP 2.5:
43207 # pragma omp parallel parallel-clause[optseq] new-line
43208 structured-block
43209 # pragma omp parallel for parallel-for-clause[optseq] new-line
43210 structured-block
43211 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
43212 structured-block
43214 OpenMP 4.0:
43215 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
43216 structured-block */
43218 #define OMP_PARALLEL_CLAUSE_MASK \
43219 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
43225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
43227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
43230 static tree
43231 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
43232 char *p_name, omp_clause_mask mask, tree *cclauses,
43233 bool *if_p)
43235 tree stmt, clauses, block;
43236 unsigned int save;
43237 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43239 strcat (p_name, " parallel");
43240 mask |= OMP_PARALLEL_CLAUSE_MASK;
43241 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
43242 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
43243 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
43244 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
43246 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43248 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43249 if (cclauses == NULL)
43250 cclauses = cclauses_buf;
43252 cp_lexer_consume_token (parser->lexer);
43253 if (!flag_openmp) /* flag_openmp_simd */
43254 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43255 if_p);
43256 block = begin_omp_parallel ();
43257 save = cp_parser_begin_omp_structured_block (parser);
43258 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43259 if_p);
43260 cp_parser_end_omp_structured_block (parser, save);
43261 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43262 block);
43263 if (ret == NULL_TREE)
43264 return ret;
43265 OMP_PARALLEL_COMBINED (stmt) = 1;
43266 return stmt;
43268 /* When combined with distribute, parallel has to be followed by for.
43269 #pragma omp target parallel is allowed though. */
43270 else if (cclauses
43271 && (mask & (OMP_CLAUSE_MASK_1
43272 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43274 error_at (loc, "expected %<for%> after %qs", p_name);
43275 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43276 return NULL_TREE;
43278 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43280 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43281 const char *p = IDENTIFIER_POINTER (id);
43282 if (cclauses == NULL && strcmp (p, "masked") == 0)
43284 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43285 cclauses = cclauses_buf;
43287 cp_lexer_consume_token (parser->lexer);
43288 if (!flag_openmp) /* flag_openmp_simd */
43289 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43290 cclauses, if_p);
43291 block = begin_omp_parallel ();
43292 save = cp_parser_begin_omp_structured_block (parser);
43293 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43294 cclauses, if_p);
43295 cp_parser_end_omp_structured_block (parser, save);
43296 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43297 block);
43298 if (ret == NULL_TREE)
43299 return ret;
43300 /* masked does have just filter clause, but during gimplification
43301 isn't represented by a gimplification omp context, so for
43302 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
43303 so that
43304 #pragma omp parallel masked
43305 #pragma omp taskloop simd lastprivate (x)
43306 isn't confused with
43307 #pragma omp parallel masked taskloop simd lastprivate (x) */
43308 if (OMP_MASKED_COMBINED (ret))
43309 OMP_PARALLEL_COMBINED (stmt) = 1;
43310 return stmt;
43312 else if (cclauses == NULL && strcmp (p, "master") == 0)
43314 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43315 cclauses = cclauses_buf;
43317 cp_lexer_consume_token (parser->lexer);
43318 if (!flag_openmp) /* flag_openmp_simd */
43319 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43320 cclauses, if_p);
43321 block = begin_omp_parallel ();
43322 save = cp_parser_begin_omp_structured_block (parser);
43323 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43324 cclauses, if_p);
43325 cp_parser_end_omp_structured_block (parser, save);
43326 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43327 block);
43328 if (ret == NULL_TREE)
43329 return ret;
43330 /* master doesn't have any clauses and during gimplification
43331 isn't represented by a gimplification omp context, so for
43332 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
43333 so that
43334 #pragma omp parallel master
43335 #pragma omp taskloop simd lastprivate (x)
43336 isn't confused with
43337 #pragma omp parallel master taskloop simd lastprivate (x) */
43338 if (OMP_MASTER_COMBINED (ret))
43339 OMP_PARALLEL_COMBINED (stmt) = 1;
43340 return stmt;
43342 else if (strcmp (p, "loop") == 0)
43344 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43345 if (cclauses == NULL)
43346 cclauses = cclauses_buf;
43348 cp_lexer_consume_token (parser->lexer);
43349 if (!flag_openmp) /* flag_openmp_simd */
43350 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43351 cclauses, if_p);
43352 block = begin_omp_parallel ();
43353 save = cp_parser_begin_omp_structured_block (parser);
43354 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43355 cclauses, if_p);
43356 cp_parser_end_omp_structured_block (parser, save);
43357 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43358 block);
43359 if (ret == NULL_TREE)
43360 return ret;
43361 OMP_PARALLEL_COMBINED (stmt) = 1;
43362 return stmt;
43364 else if (!flag_openmp) /* flag_openmp_simd */
43366 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43367 return NULL_TREE;
43369 else if (cclauses == NULL && strcmp (p, "sections") == 0)
43371 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43372 cclauses = cclauses_buf;
43374 cp_lexer_consume_token (parser->lexer);
43375 block = begin_omp_parallel ();
43376 save = cp_parser_begin_omp_structured_block (parser);
43377 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
43378 cp_parser_end_omp_structured_block (parser, save);
43379 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43380 block);
43381 OMP_PARALLEL_COMBINED (stmt) = 1;
43382 return stmt;
43385 else if (!flag_openmp) /* flag_openmp_simd */
43387 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43388 return NULL_TREE;
43391 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43392 cclauses == NULL);
43393 if (cclauses)
43395 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
43396 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
43399 block = begin_omp_parallel ();
43400 save = cp_parser_begin_omp_structured_block (parser);
43401 parser->omp_attrs_forbidden_p = true;
43402 cp_parser_statement (parser, NULL_TREE, false, if_p);
43403 cp_parser_end_omp_structured_block (parser, save);
43404 stmt = finish_omp_parallel (clauses, block);
43405 return stmt;
43408 /* OpenMP 2.5:
43409 # pragma omp single single-clause[optseq] new-line
43410 structured-block */
43412 #define OMP_SINGLE_CLAUSE_MASK \
43413 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
43416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43419 static tree
43420 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43422 tree stmt = make_node (OMP_SINGLE);
43423 TREE_TYPE (stmt) = void_type_node;
43424 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43426 OMP_SINGLE_CLAUSES (stmt)
43427 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
43428 "#pragma omp single", pragma_tok);
43429 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43431 return add_stmt (stmt);
43434 /* OpenMP 5.1:
43435 # pragma omp scope scope-clause[optseq] new-line
43436 structured-block */
43438 #define OMP_SCOPE_CLAUSE_MASK \
43439 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43443 static tree
43444 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43446 tree stmt = make_node (OMP_SCOPE);
43447 TREE_TYPE (stmt) = void_type_node;
43448 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43450 OMP_SCOPE_CLAUSES (stmt)
43451 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
43452 "#pragma omp scope", pragma_tok);
43453 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43455 return add_stmt (stmt);
43458 /* OpenMP 3.0:
43459 # pragma omp task task-clause[optseq] new-line
43460 structured-block */
43462 #define OMP_TASK_CLAUSE_MASK \
43463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
43465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
43470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
43471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
43473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
43475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
43476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
43478 static tree
43479 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43481 tree clauses, block;
43482 unsigned int save;
43484 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
43485 "#pragma omp task", pragma_tok);
43486 block = begin_omp_task ();
43487 save = cp_parser_begin_omp_structured_block (parser);
43488 parser->omp_attrs_forbidden_p = true;
43489 cp_parser_statement (parser, NULL_TREE, false, if_p);
43490 cp_parser_end_omp_structured_block (parser, save);
43491 return finish_omp_task (clauses, block);
43494 /* OpenMP 3.0:
43495 # pragma omp taskwait new-line
43497 OpenMP 5.0:
43498 # pragma omp taskwait taskwait-clause[opt] new-line */
43500 #define OMP_TASKWAIT_CLAUSE_MASK \
43501 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
43503 static void
43504 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
43506 tree clauses
43507 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
43508 "#pragma omp taskwait", pragma_tok);
43510 if (clauses)
43512 tree stmt = make_node (OMP_TASK);
43513 TREE_TYPE (stmt) = void_node;
43514 OMP_TASK_CLAUSES (stmt) = clauses;
43515 OMP_TASK_BODY (stmt) = NULL_TREE;
43516 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43517 add_stmt (stmt);
43519 else
43520 finish_omp_taskwait ();
43523 /* OpenMP 3.1:
43524 # pragma omp taskyield new-line */
43526 static void
43527 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
43529 cp_parser_require_pragma_eol (parser, pragma_tok);
43530 finish_omp_taskyield ();
43533 /* OpenMP 4.0:
43534 # pragma omp taskgroup new-line
43535 structured-block
43537 OpenMP 5.0:
43538 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
43540 #define OMP_TASKGROUP_CLAUSE_MASK \
43541 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
43544 static tree
43545 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43547 tree clauses
43548 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
43549 "#pragma omp taskgroup", pragma_tok);
43550 return c_finish_omp_taskgroup (input_location,
43551 cp_parser_omp_structured_block (parser,
43552 if_p),
43553 clauses);
43557 /* OpenMP 2.5:
43558 # pragma omp threadprivate (variable-list) */
43560 static void
43561 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
43563 tree vars;
43565 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43566 cp_parser_require_pragma_eol (parser, pragma_tok);
43568 finish_omp_threadprivate (vars);
43571 /* OpenMP 4.0:
43572 # pragma omp cancel cancel-clause[optseq] new-line */
43574 #define OMP_CANCEL_CLAUSE_MASK \
43575 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
43576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
43577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
43578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
43579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
43581 static void
43582 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
43584 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
43585 "#pragma omp cancel", pragma_tok);
43586 finish_omp_cancel (clauses);
43589 /* OpenMP 4.0:
43590 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
43592 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
43593 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
43594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
43595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
43596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
43598 static bool
43599 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
43600 enum pragma_context context)
43602 tree clauses;
43603 bool point_seen = false;
43605 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43607 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43608 const char *p = IDENTIFIER_POINTER (id);
43610 if (strcmp (p, "point") == 0)
43612 cp_lexer_consume_token (parser->lexer);
43613 point_seen = true;
43616 if (!point_seen)
43618 cp_parser_error (parser, "expected %<point%>");
43619 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43620 return false;
43623 if (context != pragma_compound)
43625 if (context == pragma_stmt)
43626 error_at (pragma_tok->location,
43627 "%<#pragma %s%> may only be used in compound statements",
43628 "omp cancellation point");
43629 else
43630 cp_parser_error (parser, "expected declaration specifiers");
43631 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43632 return true;
43635 clauses = cp_parser_omp_all_clauses (parser,
43636 OMP_CANCELLATION_POINT_CLAUSE_MASK,
43637 "#pragma omp cancellation point",
43638 pragma_tok);
43639 finish_omp_cancellation_point (clauses);
43640 return true;
43643 /* OpenMP 4.0:
43644 #pragma omp distribute distribute-clause[optseq] new-line
43645 for-loop */
43647 #define OMP_DISTRIBUTE_CLAUSE_MASK \
43648 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
43652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43656 static tree
43657 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
43658 char *p_name, omp_clause_mask mask, tree *cclauses,
43659 bool *if_p)
43661 tree clauses, sb, ret;
43662 unsigned int save;
43663 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43665 strcat (p_name, " distribute");
43666 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
43668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43670 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43671 const char *p = IDENTIFIER_POINTER (id);
43672 bool simd = false;
43673 bool parallel = false;
43675 if (strcmp (p, "simd") == 0)
43676 simd = true;
43677 else
43678 parallel = strcmp (p, "parallel") == 0;
43679 if (parallel || simd)
43681 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43682 if (cclauses == NULL)
43683 cclauses = cclauses_buf;
43684 cp_lexer_consume_token (parser->lexer);
43685 if (!flag_openmp) /* flag_openmp_simd */
43687 if (simd)
43688 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43689 cclauses, if_p);
43690 else
43691 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
43692 cclauses, if_p);
43694 sb = begin_omp_structured_block ();
43695 save = cp_parser_begin_omp_structured_block (parser);
43696 if (simd)
43697 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43698 cclauses, if_p);
43699 else
43700 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
43701 cclauses, if_p);
43702 cp_parser_end_omp_structured_block (parser, save);
43703 tree body = finish_omp_structured_block (sb);
43704 if (ret == NULL)
43705 return ret;
43706 ret = make_node (OMP_DISTRIBUTE);
43707 TREE_TYPE (ret) = void_type_node;
43708 OMP_FOR_BODY (ret) = body;
43709 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
43710 SET_EXPR_LOCATION (ret, loc);
43711 add_stmt (ret);
43712 return ret;
43715 if (!flag_openmp) /* flag_openmp_simd */
43717 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43718 return NULL_TREE;
43721 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43722 cclauses == NULL);
43723 if (cclauses)
43725 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
43726 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
43729 keep_next_level (true);
43730 sb = begin_omp_structured_block ();
43731 save = cp_parser_begin_omp_structured_block (parser);
43733 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
43735 cp_parser_end_omp_structured_block (parser, save);
43736 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43738 return ret;
43741 /* OpenMP 4.0:
43742 # pragma omp teams teams-clause[optseq] new-line
43743 structured-block */
43745 #define OMP_TEAMS_CLAUSE_MASK \
43746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
43751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
43752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
43755 static tree
43756 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
43757 char *p_name, omp_clause_mask mask, tree *cclauses,
43758 bool *if_p)
43760 tree clauses, sb, ret;
43761 unsigned int save;
43762 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43764 strcat (p_name, " teams");
43765 mask |= OMP_TEAMS_CLAUSE_MASK;
43767 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43769 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43770 const char *p = IDENTIFIER_POINTER (id);
43771 if (strcmp (p, "distribute") == 0)
43773 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43774 if (cclauses == NULL)
43775 cclauses = cclauses_buf;
43777 cp_lexer_consume_token (parser->lexer);
43778 if (!flag_openmp) /* flag_openmp_simd */
43779 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
43780 cclauses, if_p);
43781 keep_next_level (true);
43782 sb = begin_omp_structured_block ();
43783 save = cp_parser_begin_omp_structured_block (parser);
43784 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
43785 cclauses, if_p);
43786 cp_parser_end_omp_structured_block (parser, save);
43787 tree body = finish_omp_structured_block (sb);
43788 if (ret == NULL)
43789 return ret;
43790 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43791 ret = make_node (OMP_TEAMS);
43792 TREE_TYPE (ret) = void_type_node;
43793 OMP_TEAMS_CLAUSES (ret) = clauses;
43794 OMP_TEAMS_BODY (ret) = body;
43795 OMP_TEAMS_COMBINED (ret) = 1;
43796 SET_EXPR_LOCATION (ret, loc);
43797 return add_stmt (ret);
43799 else if (strcmp (p, "loop") == 0)
43801 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43802 if (cclauses == NULL)
43803 cclauses = cclauses_buf;
43805 cp_lexer_consume_token (parser->lexer);
43806 if (!flag_openmp) /* flag_openmp_simd */
43807 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43808 cclauses, if_p);
43809 keep_next_level (true);
43810 sb = begin_omp_structured_block ();
43811 save = cp_parser_begin_omp_structured_block (parser);
43812 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43813 cclauses, if_p);
43814 cp_parser_end_omp_structured_block (parser, save);
43815 tree body = finish_omp_structured_block (sb);
43816 if (ret == NULL)
43817 return ret;
43818 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43819 ret = make_node (OMP_TEAMS);
43820 TREE_TYPE (ret) = void_type_node;
43821 OMP_TEAMS_CLAUSES (ret) = clauses;
43822 OMP_TEAMS_BODY (ret) = body;
43823 OMP_TEAMS_COMBINED (ret) = 1;
43824 SET_EXPR_LOCATION (ret, loc);
43825 return add_stmt (ret);
43828 if (!flag_openmp) /* flag_openmp_simd */
43830 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43831 return NULL_TREE;
43834 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43835 cclauses == NULL);
43836 if (cclauses)
43838 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
43839 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43842 tree stmt = make_node (OMP_TEAMS);
43843 TREE_TYPE (stmt) = void_type_node;
43844 OMP_TEAMS_CLAUSES (stmt) = clauses;
43845 keep_next_level (true);
43846 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43847 SET_EXPR_LOCATION (stmt, loc);
43849 return add_stmt (stmt);
43852 /* OpenMP 4.0:
43853 # pragma omp target data target-data-clause[optseq] new-line
43854 structured-block */
43856 #define OMP_TARGET_DATA_CLAUSE_MASK \
43857 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
43858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
43859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
43861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
43863 static tree
43864 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43866 tree clauses
43867 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
43868 "#pragma omp target data", pragma_tok);
43869 c_omp_adjust_map_clauses (clauses, false);
43870 int map_seen = 0;
43871 for (tree *pc = &clauses; *pc;)
43873 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
43874 switch (OMP_CLAUSE_MAP_KIND (*pc))
43876 case GOMP_MAP_TO:
43877 case GOMP_MAP_ALWAYS_TO:
43878 case GOMP_MAP_FROM:
43879 case GOMP_MAP_ALWAYS_FROM:
43880 case GOMP_MAP_TOFROM:
43881 case GOMP_MAP_ALWAYS_TOFROM:
43882 case GOMP_MAP_ALLOC:
43883 map_seen = 3;
43884 break;
43885 case GOMP_MAP_FIRSTPRIVATE_POINTER:
43886 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
43887 case GOMP_MAP_ALWAYS_POINTER:
43888 case GOMP_MAP_ATTACH_DETACH:
43889 break;
43890 default:
43891 map_seen |= 1;
43892 error_at (OMP_CLAUSE_LOCATION (*pc),
43893 "%<#pragma omp target data%> with map-type other "
43894 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
43895 "on %<map%> clause");
43896 *pc = OMP_CLAUSE_CHAIN (*pc);
43897 continue;
43899 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
43900 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
43901 map_seen = 3;
43902 pc = &OMP_CLAUSE_CHAIN (*pc);
43905 if (map_seen != 3)
43907 if (map_seen == 0)
43908 error_at (pragma_tok->location,
43909 "%<#pragma omp target data%> must contain at least "
43910 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
43911 "clause");
43912 return NULL_TREE;
43915 tree stmt = make_node (OMP_TARGET_DATA);
43916 TREE_TYPE (stmt) = void_type_node;
43917 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
43919 keep_next_level (true);
43920 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43922 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43923 return add_stmt (stmt);
43926 /* OpenMP 4.5:
43927 # pragma omp target enter data target-enter-data-clause[optseq] new-line
43928 structured-block */
43930 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
43931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
43932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
43933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43937 static bool
43938 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
43939 enum pragma_context context)
43941 bool data_seen = false;
43942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43944 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43945 const char *p = IDENTIFIER_POINTER (id);
43947 if (strcmp (p, "data") == 0)
43949 cp_lexer_consume_token (parser->lexer);
43950 data_seen = true;
43953 if (!data_seen)
43955 cp_parser_error (parser, "expected %<data%>");
43956 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43957 return false;
43960 if (context == pragma_stmt)
43962 error_at (pragma_tok->location,
43963 "%<#pragma %s%> may only be used in compound statements",
43964 "omp target enter data");
43965 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43966 return true;
43969 tree clauses
43970 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
43971 "#pragma omp target enter data", pragma_tok);
43972 c_omp_adjust_map_clauses (clauses, false);
43973 int map_seen = 0;
43974 for (tree *pc = &clauses; *pc;)
43976 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
43977 switch (OMP_CLAUSE_MAP_KIND (*pc))
43979 case GOMP_MAP_TO:
43980 case GOMP_MAP_ALWAYS_TO:
43981 case GOMP_MAP_ALLOC:
43982 map_seen = 3;
43983 break;
43984 case GOMP_MAP_FIRSTPRIVATE_POINTER:
43985 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
43986 case GOMP_MAP_ALWAYS_POINTER:
43987 case GOMP_MAP_ATTACH_DETACH:
43988 break;
43989 default:
43990 map_seen |= 1;
43991 error_at (OMP_CLAUSE_LOCATION (*pc),
43992 "%<#pragma omp target enter data%> with map-type other "
43993 "than %<to%> or %<alloc%> on %<map%> clause");
43994 *pc = OMP_CLAUSE_CHAIN (*pc);
43995 continue;
43997 pc = &OMP_CLAUSE_CHAIN (*pc);
44000 if (map_seen != 3)
44002 if (map_seen == 0)
44003 error_at (pragma_tok->location,
44004 "%<#pragma omp target enter data%> must contain at least "
44005 "one %<map%> clause");
44006 return true;
44009 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
44010 TREE_TYPE (stmt) = void_type_node;
44011 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
44012 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44013 add_stmt (stmt);
44014 return true;
44017 /* OpenMP 4.5:
44018 # pragma omp target exit data target-enter-data-clause[optseq] new-line
44019 structured-block */
44021 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
44022 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44028 static bool
44029 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
44030 enum pragma_context context)
44032 bool data_seen = false;
44033 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44035 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44036 const char *p = IDENTIFIER_POINTER (id);
44038 if (strcmp (p, "data") == 0)
44040 cp_lexer_consume_token (parser->lexer);
44041 data_seen = true;
44044 if (!data_seen)
44046 cp_parser_error (parser, "expected %<data%>");
44047 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44048 return false;
44051 if (context == pragma_stmt)
44053 error_at (pragma_tok->location,
44054 "%<#pragma %s%> may only be used in compound statements",
44055 "omp target exit data");
44056 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44057 return true;
44060 tree clauses
44061 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
44062 "#pragma omp target exit data", pragma_tok);
44063 c_omp_adjust_map_clauses (clauses, false);
44064 int map_seen = 0;
44065 for (tree *pc = &clauses; *pc;)
44067 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44068 switch (OMP_CLAUSE_MAP_KIND (*pc))
44070 case GOMP_MAP_FROM:
44071 case GOMP_MAP_ALWAYS_FROM:
44072 case GOMP_MAP_RELEASE:
44073 case GOMP_MAP_DELETE:
44074 map_seen = 3;
44075 break;
44076 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44077 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44078 case GOMP_MAP_ALWAYS_POINTER:
44079 case GOMP_MAP_ATTACH_DETACH:
44080 break;
44081 default:
44082 map_seen |= 1;
44083 error_at (OMP_CLAUSE_LOCATION (*pc),
44084 "%<#pragma omp target exit data%> with map-type other "
44085 "than %<from%>, %<release%> or %<delete%> on %<map%>"
44086 " clause");
44087 *pc = OMP_CLAUSE_CHAIN (*pc);
44088 continue;
44090 pc = &OMP_CLAUSE_CHAIN (*pc);
44093 if (map_seen != 3)
44095 if (map_seen == 0)
44096 error_at (pragma_tok->location,
44097 "%<#pragma omp target exit data%> must contain at least "
44098 "one %<map%> clause");
44099 return true;
44102 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
44103 TREE_TYPE (stmt) = void_type_node;
44104 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
44105 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44106 add_stmt (stmt);
44107 return true;
44110 /* OpenMP 4.0:
44111 # pragma omp target update target-update-clause[optseq] new-line */
44113 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
44114 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
44115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
44116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44121 static bool
44122 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
44123 enum pragma_context context)
44125 if (context == pragma_stmt)
44127 error_at (pragma_tok->location,
44128 "%<#pragma %s%> may only be used in compound statements",
44129 "omp target update");
44130 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44131 return true;
44134 tree clauses
44135 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
44136 "#pragma omp target update", pragma_tok);
44137 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
44138 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
44140 error_at (pragma_tok->location,
44141 "%<#pragma omp target update%> must contain at least one "
44142 "%<from%> or %<to%> clauses");
44143 return true;
44146 tree stmt = make_node (OMP_TARGET_UPDATE);
44147 TREE_TYPE (stmt) = void_type_node;
44148 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
44149 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44150 add_stmt (stmt);
44151 return true;
44154 /* OpenMP 4.0:
44155 # pragma omp target target-clause[optseq] new-line
44156 structured-block */
44158 #define OMP_TARGET_CLAUSE_MASK \
44159 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
44164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
44167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
44170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
44172 static bool
44173 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
44174 enum pragma_context context, bool *if_p)
44176 if (flag_openmp)
44177 omp_requires_mask
44178 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44182 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44183 const char *p = IDENTIFIER_POINTER (id);
44184 enum tree_code ccode = ERROR_MARK;
44186 if (strcmp (p, "teams") == 0)
44187 ccode = OMP_TEAMS;
44188 else if (strcmp (p, "parallel") == 0)
44189 ccode = OMP_PARALLEL;
44190 else if (strcmp (p, "simd") == 0)
44191 ccode = OMP_SIMD;
44192 if (ccode != ERROR_MARK)
44194 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
44195 char p_name[sizeof ("#pragma omp target teams distribute "
44196 "parallel for simd")];
44198 cp_lexer_consume_token (parser->lexer);
44199 strcpy (p_name, "#pragma omp target");
44200 if (!flag_openmp) /* flag_openmp_simd */
44202 tree stmt;
44203 switch (ccode)
44205 case OMP_TEAMS:
44206 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
44207 OMP_TARGET_CLAUSE_MASK,
44208 cclauses, if_p);
44209 break;
44210 case OMP_PARALLEL:
44211 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44212 OMP_TARGET_CLAUSE_MASK,
44213 cclauses, if_p);
44214 break;
44215 case OMP_SIMD:
44216 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
44217 OMP_TARGET_CLAUSE_MASK,
44218 cclauses, if_p);
44219 break;
44220 default:
44221 gcc_unreachable ();
44223 return stmt != NULL_TREE;
44225 keep_next_level (true);
44226 tree sb = begin_omp_structured_block (), ret;
44227 unsigned save = cp_parser_begin_omp_structured_block (parser);
44228 switch (ccode)
44230 case OMP_TEAMS:
44231 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
44232 OMP_TARGET_CLAUSE_MASK, cclauses,
44233 if_p);
44234 break;
44235 case OMP_PARALLEL:
44236 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44237 OMP_TARGET_CLAUSE_MASK, cclauses,
44238 if_p);
44239 break;
44240 case OMP_SIMD:
44241 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
44242 OMP_TARGET_CLAUSE_MASK, cclauses,
44243 if_p);
44244 break;
44245 default:
44246 gcc_unreachable ();
44248 cp_parser_end_omp_structured_block (parser, save);
44249 tree body = finish_omp_structured_block (sb);
44250 if (ret == NULL_TREE)
44251 return false;
44252 if (ccode == OMP_TEAMS && !processing_template_decl)
44253 /* For combined target teams, ensure the num_teams and
44254 thread_limit clause expressions are evaluated on the host,
44255 before entering the target construct. */
44256 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44257 c; c = OMP_CLAUSE_CHAIN (c))
44258 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
44259 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
44260 for (int i = 0;
44261 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
44262 if (OMP_CLAUSE_OPERAND (c, i)
44263 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
44265 tree expr = OMP_CLAUSE_OPERAND (c, i);
44266 expr = force_target_expr (TREE_TYPE (expr), expr,
44267 tf_none);
44268 if (expr == error_mark_node)
44269 continue;
44270 tree tmp = TARGET_EXPR_SLOT (expr);
44271 add_stmt (expr);
44272 OMP_CLAUSE_OPERAND (c, i) = expr;
44273 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
44274 OMP_CLAUSE_FIRSTPRIVATE);
44275 OMP_CLAUSE_DECL (tc) = tmp;
44276 OMP_CLAUSE_CHAIN (tc)
44277 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
44278 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
44280 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
44281 finish_omp_target (pragma_tok->location,
44282 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
44283 return true;
44285 else if (!flag_openmp) /* flag_openmp_simd */
44287 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44288 return false;
44290 else if (strcmp (p, "data") == 0)
44292 cp_lexer_consume_token (parser->lexer);
44293 cp_parser_omp_target_data (parser, pragma_tok, if_p);
44294 return true;
44296 else if (strcmp (p, "enter") == 0)
44298 cp_lexer_consume_token (parser->lexer);
44299 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
44301 else if (strcmp (p, "exit") == 0)
44303 cp_lexer_consume_token (parser->lexer);
44304 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
44306 else if (strcmp (p, "update") == 0)
44308 cp_lexer_consume_token (parser->lexer);
44309 return cp_parser_omp_target_update (parser, pragma_tok, context);
44312 if (!flag_openmp) /* flag_openmp_simd */
44314 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44315 return false;
44318 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
44319 "#pragma omp target", pragma_tok,
44320 false);
44321 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
44322 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44324 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
44325 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
44326 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
44327 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
44328 OMP_CLAUSE_CHAIN (c) = nc;
44330 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
44332 c_omp_adjust_map_clauses (clauses, true);
44333 keep_next_level (true);
44334 tree body = cp_parser_omp_structured_block (parser, if_p);
44336 finish_omp_target (pragma_tok->location, clauses, body, false);
44337 return true;
44340 /* OpenACC 2.0:
44341 # pragma acc cache (variable-list) new-line
44344 static tree
44345 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
44347 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
44348 clauses. */
44349 auto_suppress_location_wrappers sentinel;
44351 tree stmt, clauses;
44353 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
44354 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
44356 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
44358 stmt = make_node (OACC_CACHE);
44359 TREE_TYPE (stmt) = void_type_node;
44360 OACC_CACHE_CLAUSES (stmt) = clauses;
44361 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44362 add_stmt (stmt);
44364 return stmt;
44367 /* OpenACC 2.0:
44368 # pragma acc data oacc-data-clause[optseq] new-line
44369 structured-block */
44371 #define OACC_DATA_CLAUSE_MASK \
44372 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
44378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44383 static tree
44384 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44386 tree stmt, clauses, block;
44387 unsigned int save;
44389 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
44390 "#pragma acc data", pragma_tok);
44392 block = begin_omp_parallel ();
44393 save = cp_parser_begin_omp_structured_block (parser);
44394 cp_parser_statement (parser, NULL_TREE, false, if_p);
44395 cp_parser_end_omp_structured_block (parser, save);
44396 stmt = finish_oacc_data (clauses, block);
44397 return stmt;
44400 /* OpenACC 2.0:
44401 # pragma acc host_data <clauses> new-line
44402 structured-block */
44404 #define OACC_HOST_DATA_CLAUSE_MASK \
44405 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
44406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
44409 static tree
44410 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44412 tree stmt, clauses, block;
44413 unsigned int save;
44415 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
44416 "#pragma acc host_data", pragma_tok);
44418 block = begin_omp_parallel ();
44419 save = cp_parser_begin_omp_structured_block (parser);
44420 cp_parser_statement (parser, NULL_TREE, false, if_p);
44421 cp_parser_end_omp_structured_block (parser, save);
44422 stmt = finish_oacc_host_data (clauses, block);
44423 return stmt;
44426 /* OpenACC 2.0:
44427 # pragma acc declare oacc-data-clause[optseq] new-line
44430 #define OACC_DECLARE_CLAUSE_MASK \
44431 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
44437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
44438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44440 static tree
44441 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
44443 tree clauses, stmt;
44444 bool error = false;
44445 bool found_in_scope = global_bindings_p ();
44447 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
44448 "#pragma acc declare", pragma_tok, true);
44451 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44453 error_at (pragma_tok->location,
44454 "no valid clauses specified in %<#pragma acc declare%>");
44455 return NULL_TREE;
44458 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
44460 location_t loc = OMP_CLAUSE_LOCATION (t);
44461 tree decl = OMP_CLAUSE_DECL (t);
44462 if (!DECL_P (decl))
44464 error_at (loc, "array section in %<#pragma acc declare%>");
44465 error = true;
44466 continue;
44468 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
44469 switch (OMP_CLAUSE_MAP_KIND (t))
44471 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44472 case GOMP_MAP_ALLOC:
44473 case GOMP_MAP_TO:
44474 case GOMP_MAP_FORCE_DEVICEPTR:
44475 case GOMP_MAP_DEVICE_RESIDENT:
44476 break;
44478 case GOMP_MAP_LINK:
44479 if (!global_bindings_p ()
44480 && (TREE_STATIC (decl)
44481 || !DECL_EXTERNAL (decl)))
44483 error_at (loc,
44484 "%qD must be a global variable in "
44485 "%<#pragma acc declare link%>",
44486 decl);
44487 error = true;
44488 continue;
44490 break;
44492 default:
44493 if (global_bindings_p ())
44495 error_at (loc, "invalid OpenACC clause at file scope");
44496 error = true;
44497 continue;
44499 if (DECL_EXTERNAL (decl))
44501 error_at (loc,
44502 "invalid use of %<extern%> variable %qD "
44503 "in %<#pragma acc declare%>", decl);
44504 error = true;
44505 continue;
44507 else if (TREE_PUBLIC (decl))
44509 error_at (loc,
44510 "invalid use of %<global%> variable %qD "
44511 "in %<#pragma acc declare%>", decl);
44512 error = true;
44513 continue;
44515 break;
44518 if (!found_in_scope)
44519 /* This seems to ignore the existence of cleanup scopes?
44520 What is the meaning for local extern decls? The local
44521 extern is in this scope, but it is referring to a decl that
44522 is namespace scope. */
44523 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
44524 if (d == decl)
44526 found_in_scope = true;
44527 break;
44529 if (!found_in_scope)
44531 error_at (loc,
44532 "%qD must be a variable declared in the same scope as "
44533 "%<#pragma acc declare%>", decl);
44534 error = true;
44535 continue;
44538 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
44539 || lookup_attribute ("omp declare target link",
44540 DECL_ATTRIBUTES (decl)))
44542 error_at (loc, "variable %qD used more than once with "
44543 "%<#pragma acc declare%>", decl);
44544 error = true;
44545 continue;
44548 if (!error)
44550 tree id;
44552 if (DECL_LOCAL_DECL_P (decl))
44553 /* We need to mark the aliased decl, as that is the entity
44554 that is being referred to. This won't work for
44555 dependent variables, but it didn't work for them before
44556 DECL_LOCAL_DECL_P was a thing either. But then
44557 dependent local extern variable decls are as rare as
44558 hen's teeth. */
44559 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
44560 if (alias != error_mark_node)
44561 decl = alias;
44563 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
44564 id = get_identifier ("omp declare target link");
44565 else
44566 id = get_identifier ("omp declare target");
44568 DECL_ATTRIBUTES (decl)
44569 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
44570 if (current_binding_level->kind == sk_namespace)
44572 symtab_node *node = symtab_node::get (decl);
44573 if (node != NULL)
44575 node->offloadable = 1;
44576 if (ENABLE_OFFLOADING)
44578 g->have_offload = true;
44579 if (is_a <varpool_node *> (node))
44580 vec_safe_push (offload_vars, decl);
44587 if (error || current_binding_level->kind == sk_namespace)
44588 return NULL_TREE;
44590 stmt = make_node (OACC_DECLARE);
44591 TREE_TYPE (stmt) = void_type_node;
44592 OACC_DECLARE_CLAUSES (stmt) = clauses;
44593 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44595 add_stmt (stmt);
44597 return NULL_TREE;
44600 /* OpenACC 2.0:
44601 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
44605 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
44607 LOC is the location of the #pragma token.
44610 #define OACC_ENTER_DATA_CLAUSE_MASK \
44611 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44618 #define OACC_EXIT_DATA_CLAUSE_MASK \
44619 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
44623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
44624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
44625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44627 static tree
44628 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
44629 bool enter)
44631 location_t loc = pragma_tok->location;
44632 tree stmt, clauses;
44633 const char *p = "";
44635 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44636 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
44638 if (strcmp (p, "data") != 0)
44640 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
44641 enter ? "enter" : "exit");
44642 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44643 return NULL_TREE;
44646 cp_lexer_consume_token (parser->lexer);
44648 if (enter)
44649 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
44650 "#pragma acc enter data", pragma_tok);
44651 else
44652 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
44653 "#pragma acc exit data", pragma_tok);
44655 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44657 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
44658 enter ? "enter" : "exit");
44659 return NULL_TREE;
44662 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
44663 TREE_TYPE (stmt) = void_type_node;
44664 OMP_STANDALONE_CLAUSES (stmt) = clauses;
44665 SET_EXPR_LOCATION (stmt, loc);
44666 add_stmt (stmt);
44667 return stmt;
44670 /* OpenACC 2.0:
44671 # pragma acc loop oacc-loop-clause[optseq] new-line
44672 structured-block */
44674 #define OACC_LOOP_CLAUSE_MASK \
44675 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
44676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
44677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
44678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
44679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
44680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
44681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
44682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
44683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
44684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
44686 static tree
44687 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
44688 omp_clause_mask mask, tree *cclauses, bool *if_p)
44690 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
44692 strcat (p_name, " loop");
44693 mask |= OACC_LOOP_CLAUSE_MASK;
44695 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
44696 cclauses == NULL);
44697 if (cclauses)
44699 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
44700 if (*cclauses)
44701 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
44702 if (clauses)
44703 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
44706 tree block = begin_omp_structured_block ();
44707 int save = cp_parser_begin_omp_structured_block (parser);
44708 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
44709 cp_parser_end_omp_structured_block (parser, save);
44710 add_stmt (finish_omp_structured_block (block));
44712 return stmt;
44715 /* OpenACC 2.0:
44716 # pragma acc kernels oacc-kernels-clause[optseq] new-line
44717 structured-block
44721 # pragma acc parallel oacc-parallel-clause[optseq] new-line
44722 structured-block
44724 OpenACC 2.6:
44726 # pragma acc serial oacc-serial-clause[optseq] new-line
44729 #define OACC_KERNELS_CLAUSE_MASK \
44730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
44737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
44741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
44742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
44743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
44744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44746 #define OACC_PARALLEL_CLAUSE_MASK \
44747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
44754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
44756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
44759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
44760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
44761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
44762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
44763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
44764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44766 #define OACC_SERIAL_CLAUSE_MASK \
44767 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
44774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
44778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
44779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
44780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
44781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44783 static tree
44784 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
44785 char *p_name, bool *if_p)
44787 omp_clause_mask mask;
44788 enum tree_code code;
44789 switch (cp_parser_pragma_kind (pragma_tok))
44791 case PRAGMA_OACC_KERNELS:
44792 strcat (p_name, " kernels");
44793 mask = OACC_KERNELS_CLAUSE_MASK;
44794 code = OACC_KERNELS;
44795 break;
44796 case PRAGMA_OACC_PARALLEL:
44797 strcat (p_name, " parallel");
44798 mask = OACC_PARALLEL_CLAUSE_MASK;
44799 code = OACC_PARALLEL;
44800 break;
44801 case PRAGMA_OACC_SERIAL:
44802 strcat (p_name, " serial");
44803 mask = OACC_SERIAL_CLAUSE_MASK;
44804 code = OACC_SERIAL;
44805 break;
44806 default:
44807 gcc_unreachable ();
44810 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44812 const char *p
44813 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
44814 if (strcmp (p, "loop") == 0)
44816 cp_lexer_consume_token (parser->lexer);
44817 tree block = begin_omp_parallel ();
44818 tree clauses;
44819 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
44820 &clauses, if_p);
44821 protected_set_expr_location (stmt, pragma_tok->location);
44822 return finish_omp_construct (code, block, clauses);
44826 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
44828 tree block = begin_omp_parallel ();
44829 unsigned int save = cp_parser_begin_omp_structured_block (parser);
44830 cp_parser_statement (parser, NULL_TREE, false, if_p);
44831 cp_parser_end_omp_structured_block (parser, save);
44832 return finish_omp_construct (code, block, clauses);
44835 /* OpenACC 2.0:
44836 # pragma acc update oacc-update-clause[optseq] new-line
44839 #define OACC_UPDATE_CLAUSE_MASK \
44840 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
44842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
44843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
44845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
44847 static tree
44848 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
44850 tree stmt, clauses;
44852 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
44853 "#pragma acc update", pragma_tok);
44855 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44857 error_at (pragma_tok->location,
44858 "%<#pragma acc update%> must contain at least one "
44859 "%<device%> or %<host%> or %<self%> clause");
44860 return NULL_TREE;
44863 stmt = make_node (OACC_UPDATE);
44864 TREE_TYPE (stmt) = void_type_node;
44865 OACC_UPDATE_CLAUSES (stmt) = clauses;
44866 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44867 add_stmt (stmt);
44868 return stmt;
44871 /* OpenACC 2.0:
44872 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
44874 LOC is the location of the #pragma token.
44877 #define OACC_WAIT_CLAUSE_MASK \
44878 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
44880 static tree
44881 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
44883 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
44884 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44886 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
44887 list = cp_parser_oacc_wait_list (parser, loc, list);
44889 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
44890 "#pragma acc wait", pragma_tok);
44892 stmt = c_finish_oacc_wait (loc, list, clauses);
44893 stmt = finish_expr_stmt (stmt);
44895 return stmt;
44898 /* OpenMP 4.0:
44899 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
44901 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
44902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
44903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
44905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
44906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
44907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
44909 static void
44910 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
44911 enum pragma_context context,
44912 bool variant_p)
44914 bool first_p = parser->omp_declare_simd == NULL;
44915 cp_omp_declare_simd_data data;
44916 if (first_p)
44918 data.error_seen = false;
44919 data.fndecl_seen = false;
44920 data.variant_p = variant_p;
44921 data.tokens = vNULL;
44922 data.attribs[0] = NULL;
44923 data.attribs[1] = NULL;
44924 data.loc = UNKNOWN_LOCATION;
44925 /* It is safe to take the address of a local variable; it will only be
44926 used while this scope is live. */
44927 parser->omp_declare_simd = &data;
44929 else if (parser->omp_declare_simd->variant_p != variant_p)
44931 error_at (pragma_tok->location,
44932 "%<#pragma omp declare %s%> followed by "
44933 "%<#pragma omp declare %s%>",
44934 parser->omp_declare_simd->variant_p ? "variant" : "simd",
44935 parser->omp_declare_simd->variant_p ? "simd" : "variant");
44936 parser->omp_declare_simd->error_seen = true;
44939 /* Store away all pragma tokens. */
44940 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
44941 cp_lexer_consume_token (parser->lexer);
44942 cp_parser_require_pragma_eol (parser, pragma_tok);
44943 struct cp_token_cache *cp
44944 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
44945 parser->omp_declare_simd->tokens.safe_push (cp);
44947 if (first_p)
44949 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
44950 cp_parser_pragma (parser, context, NULL);
44951 switch (context)
44953 case pragma_external:
44954 cp_parser_declaration (parser, NULL_TREE);
44955 break;
44956 case pragma_member:
44957 cp_parser_member_declaration (parser);
44958 break;
44959 case pragma_objc_icode:
44960 cp_parser_block_declaration (parser, /*statement_p=*/false);
44961 break;
44962 default:
44963 cp_parser_declaration_statement (parser);
44964 break;
44966 if (parser->omp_declare_simd
44967 && !parser->omp_declare_simd->error_seen
44968 && !parser->omp_declare_simd->fndecl_seen)
44969 error_at (pragma_tok->location,
44970 "%<#pragma omp declare %s%> not immediately followed by "
44971 "function declaration or definition",
44972 parser->omp_declare_simd->variant_p ? "variant" : "simd");
44973 data.tokens.release ();
44974 parser->omp_declare_simd = NULL;
44978 static const char *const omp_construct_selectors[] = {
44979 "simd", "target", "teams", "parallel", "for", NULL };
44980 static const char *const omp_device_selectors[] = {
44981 "kind", "isa", "arch", NULL };
44982 static const char *const omp_implementation_selectors[] = {
44983 "vendor", "extension", "atomic_default_mem_order", "unified_address",
44984 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
44985 static const char *const omp_user_selectors[] = {
44986 "condition", NULL };
44988 /* OpenMP 5.0:
44990 trait-selector:
44991 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
44993 trait-score:
44994 score(score-expression) */
44996 static tree
44997 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
44999 tree ret = NULL_TREE;
45002 tree selector;
45003 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45004 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45005 selector = cp_lexer_peek_token (parser->lexer)->u.value;
45006 else
45008 cp_parser_error (parser, "expected trait selector name");
45009 return error_mark_node;
45012 tree properties = NULL_TREE;
45013 const char *const *selectors = NULL;
45014 bool allow_score = true;
45015 bool allow_user = false;
45016 int property_limit = 0;
45017 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
45018 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
45019 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
45020 switch (IDENTIFIER_POINTER (set)[0])
45022 case 'c': /* construct */
45023 selectors = omp_construct_selectors;
45024 allow_score = false;
45025 property_limit = 1;
45026 property_kind = CTX_PROPERTY_SIMD;
45027 break;
45028 case 'd': /* device */
45029 selectors = omp_device_selectors;
45030 allow_score = false;
45031 allow_user = true;
45032 property_limit = 3;
45033 property_kind = CTX_PROPERTY_NAME_LIST;
45034 break;
45035 case 'i': /* implementation */
45036 selectors = omp_implementation_selectors;
45037 allow_user = true;
45038 property_limit = 3;
45039 property_kind = CTX_PROPERTY_NAME_LIST;
45040 break;
45041 case 'u': /* user */
45042 selectors = omp_user_selectors;
45043 property_limit = 1;
45044 property_kind = CTX_PROPERTY_EXPR;
45045 break;
45046 default:
45047 gcc_unreachable ();
45049 for (int i = 0; ; i++)
45051 if (selectors[i] == NULL)
45053 if (allow_user)
45055 property_kind = CTX_PROPERTY_USER;
45056 break;
45058 else
45060 error ("selector %qs not allowed for context selector "
45061 "set %qs", IDENTIFIER_POINTER (selector),
45062 IDENTIFIER_POINTER (set));
45063 cp_lexer_consume_token (parser->lexer);
45064 return error_mark_node;
45067 if (i == property_limit)
45068 property_kind = CTX_PROPERTY_NONE;
45069 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
45070 break;
45072 if (property_kind == CTX_PROPERTY_NAME_LIST
45073 && IDENTIFIER_POINTER (set)[0] == 'i'
45074 && strcmp (IDENTIFIER_POINTER (selector),
45075 "atomic_default_mem_order") == 0)
45076 property_kind = CTX_PROPERTY_ID;
45078 cp_lexer_consume_token (parser->lexer);
45080 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
45082 if (property_kind == CTX_PROPERTY_NONE)
45084 error ("selector %qs does not accept any properties",
45085 IDENTIFIER_POINTER (selector));
45086 return error_mark_node;
45089 matching_parens parens;
45090 parens.consume_open (parser);
45092 cp_token *token = cp_lexer_peek_token (parser->lexer);
45093 if (allow_score
45094 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
45095 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
45096 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
45098 cp_lexer_save_tokens (parser->lexer);
45099 cp_lexer_consume_token (parser->lexer);
45100 cp_lexer_consume_token (parser->lexer);
45101 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
45102 true)
45103 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
45105 cp_lexer_rollback_tokens (parser->lexer);
45106 cp_lexer_consume_token (parser->lexer);
45108 matching_parens parens2;
45109 parens2.require_open (parser);
45110 tree score = cp_parser_constant_expression (parser);
45111 if (!parens2.require_close (parser))
45112 cp_parser_skip_to_closing_parenthesis (parser, true,
45113 false, true);
45114 cp_parser_require (parser, CPP_COLON, RT_COLON);
45115 if (score != error_mark_node)
45117 score = fold_non_dependent_expr (score);
45118 if (value_dependent_expression_p (score))
45119 properties = tree_cons (get_identifier (" score"),
45120 score, properties);
45121 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
45122 || TREE_CODE (score) != INTEGER_CST)
45123 error_at (token->location, "score argument must be "
45124 "constant integer expression");
45125 else if (tree_int_cst_sgn (score) < 0)
45126 error_at (token->location, "score argument must be "
45127 "non-negative");
45128 else
45129 properties = tree_cons (get_identifier (" score"),
45130 score, properties);
45133 else
45134 cp_lexer_rollback_tokens (parser->lexer);
45136 token = cp_lexer_peek_token (parser->lexer);
45139 switch (property_kind)
45141 tree t;
45142 case CTX_PROPERTY_USER:
45145 t = cp_parser_constant_expression (parser);
45146 if (t != error_mark_node)
45148 t = fold_non_dependent_expr (t);
45149 if (TREE_CODE (t) == STRING_CST)
45150 properties = tree_cons (NULL_TREE, t, properties);
45151 else if (!value_dependent_expression_p (t)
45152 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45153 || !tree_fits_shwi_p (t)))
45154 error_at (token->location, "property must be "
45155 "constant integer expression or string "
45156 "literal");
45157 else
45158 properties = tree_cons (NULL_TREE, t, properties);
45160 else
45161 return error_mark_node;
45163 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45164 cp_lexer_consume_token (parser->lexer);
45165 else
45166 break;
45168 while (1);
45169 break;
45170 case CTX_PROPERTY_ID:
45171 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45172 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45174 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
45175 cp_lexer_consume_token (parser->lexer);
45176 properties = tree_cons (prop, NULL_TREE, properties);
45178 else
45180 cp_parser_error (parser, "expected identifier");
45181 return error_mark_node;
45183 break;
45184 case CTX_PROPERTY_NAME_LIST:
45187 tree prop = NULL_TREE, value = NULL_TREE;
45188 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45189 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45191 prop = cp_lexer_peek_token (parser->lexer)->u.value;
45192 cp_lexer_consume_token (parser->lexer);
45194 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
45195 value = cp_parser_string_literal (parser, false, false);
45196 else
45198 cp_parser_error (parser, "expected identifier or "
45199 "string literal");
45200 return error_mark_node;
45203 properties = tree_cons (prop, value, properties);
45205 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45206 cp_lexer_consume_token (parser->lexer);
45207 else
45208 break;
45210 while (1);
45211 break;
45212 case CTX_PROPERTY_EXPR:
45213 t = cp_parser_constant_expression (parser);
45214 if (t != error_mark_node)
45216 t = fold_non_dependent_expr (t);
45217 if (!value_dependent_expression_p (t)
45218 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45219 || !tree_fits_shwi_p (t)))
45220 error_at (token->location, "property must be "
45221 "constant integer expression");
45222 else
45223 properties = tree_cons (NULL_TREE, t, properties);
45225 else
45226 return error_mark_node;
45227 break;
45228 case CTX_PROPERTY_SIMD:
45229 if (!has_parms_p)
45231 error_at (token->location, "properties for %<simd%> "
45232 "selector may not be specified in "
45233 "%<metadirective%>");
45234 return error_mark_node;
45236 properties
45237 = cp_parser_omp_all_clauses (parser,
45238 OMP_DECLARE_SIMD_CLAUSE_MASK,
45239 "simd", NULL, true, 2);
45240 break;
45241 default:
45242 gcc_unreachable ();
45245 if (!parens.require_close (parser))
45246 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
45248 properties = nreverse (properties);
45250 else if (property_kind == CTX_PROPERTY_NAME_LIST
45251 || property_kind == CTX_PROPERTY_ID
45252 || property_kind == CTX_PROPERTY_EXPR)
45254 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
45255 return error_mark_node;
45258 ret = tree_cons (selector, properties, ret);
45260 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45261 cp_lexer_consume_token (parser->lexer);
45262 else
45263 break;
45265 while (1);
45267 return nreverse (ret);
45270 /* OpenMP 5.0:
45272 trait-set-selector[,trait-set-selector[,...]]
45274 trait-set-selector:
45275 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
45277 trait-set-selector-name:
45278 constructor
45279 device
45280 implementation
45281 user */
45283 static tree
45284 cp_parser_omp_context_selector_specification (cp_parser *parser,
45285 bool has_parms_p)
45287 tree ret = NULL_TREE;
45290 const char *setp = "";
45291 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45292 setp
45293 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45294 switch (setp[0])
45296 case 'c':
45297 if (strcmp (setp, "construct") == 0)
45298 setp = NULL;
45299 break;
45300 case 'd':
45301 if (strcmp (setp, "device") == 0)
45302 setp = NULL;
45303 break;
45304 case 'i':
45305 if (strcmp (setp, "implementation") == 0)
45306 setp = NULL;
45307 break;
45308 case 'u':
45309 if (strcmp (setp, "user") == 0)
45310 setp = NULL;
45311 break;
45312 default:
45313 break;
45315 if (setp)
45317 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
45318 "%<implementation%> or %<user%>");
45319 return error_mark_node;
45322 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
45323 cp_lexer_consume_token (parser->lexer);
45325 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
45326 return error_mark_node;
45328 matching_braces braces;
45329 if (!braces.require_open (parser))
45330 return error_mark_node;
45332 tree selectors
45333 = cp_parser_omp_context_selector (parser, set, has_parms_p);
45334 if (selectors == error_mark_node)
45336 cp_parser_skip_to_closing_brace (parser);
45337 ret = error_mark_node;
45339 else if (ret != error_mark_node)
45340 ret = tree_cons (set, selectors, ret);
45342 braces.require_close (parser);
45344 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45345 cp_lexer_consume_token (parser->lexer);
45346 else
45347 break;
45349 while (1);
45351 if (ret == error_mark_node)
45352 return ret;
45353 return nreverse (ret);
45356 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
45357 that into "omp declare variant base" attribute. */
45359 static tree
45360 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
45361 tree attrs)
45363 matching_parens parens;
45364 if (!parens.require_open (parser))
45366 fail:
45367 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45368 return attrs;
45371 bool template_p;
45372 cp_id_kind idk = CP_ID_KIND_NONE;
45373 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
45374 cp_expr varid
45375 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
45376 /*check_dependency_p=*/true,
45377 /*template_p=*/&template_p,
45378 /*declarator_p=*/false,
45379 /*optional_p=*/false);
45380 parens.require_close (parser);
45382 tree variant;
45383 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
45384 || TREE_CODE (varid) == TYPE_DECL
45385 || varid == error_mark_node)
45386 variant = varid;
45387 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
45388 variant = NULL_TREE;
45389 else
45391 tree ambiguous_decls;
45392 variant = cp_parser_lookup_name (parser, varid, none_type,
45393 template_p, /*is_namespace=*/false,
45394 /*check_dependency=*/true,
45395 &ambiguous_decls,
45396 varid.get_location ());
45397 if (ambiguous_decls)
45398 variant = NULL_TREE;
45400 if (variant == NULL_TREE)
45401 variant = error_mark_node;
45402 else if (TREE_CODE (variant) != SCOPE_REF)
45404 const char *error_msg;
45405 variant
45406 = finish_id_expression (varid, variant, parser->scope,
45407 &idk, false, true,
45408 &parser->non_integral_constant_expression_p,
45409 template_p, true, false, false, &error_msg,
45410 varid.get_location ());
45411 if (error_msg)
45412 cp_parser_error (parser, error_msg);
45414 location_t caret_loc = get_pure_location (varid.get_location ());
45415 location_t start_loc = get_start (varid_token->location);
45416 location_t finish_loc = get_finish (varid.get_location ());
45417 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
45419 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45420 if (parser->lexer->in_omp_attribute_pragma
45421 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45422 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45423 cp_lexer_consume_token (parser->lexer);
45425 const char *clause = "";
45426 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
45427 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45428 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45429 if (strcmp (clause, "match"))
45431 cp_parser_error (parser, "expected %<match%>");
45432 goto fail;
45435 cp_lexer_consume_token (parser->lexer);
45437 if (!parens.require_open (parser))
45438 goto fail;
45440 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
45441 if (ctx == error_mark_node)
45442 goto fail;
45443 ctx = omp_check_context_selector (match_loc, ctx);
45444 if (ctx != error_mark_node && variant != error_mark_node)
45446 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
45447 match_loc);
45448 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
45449 loc_node = tree_cons (match_loc_node,
45450 build_int_cst (integer_type_node, idk),
45451 build_tree_list (loc_node, integer_zero_node));
45452 attrs = tree_cons (get_identifier ("omp declare variant base"),
45453 tree_cons (variant, ctx, loc_node), attrs);
45454 if (processing_template_decl)
45455 ATTR_IS_DEPENDENT (attrs) = 1;
45458 parens.require_close (parser);
45459 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45460 return attrs;
45464 /* Finalize #pragma omp declare simd clauses after direct declarator has
45465 been parsed, and put that into "omp declare simd" attribute. */
45467 static tree
45468 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
45470 struct cp_token_cache *ce;
45471 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
45472 int i;
45474 if (!data->error_seen && data->fndecl_seen)
45476 error ("%<#pragma omp declare %s%> not immediately followed by "
45477 "a single function declaration or definition",
45478 data->variant_p ? "variant" : "simd");
45479 data->error_seen = true;
45481 if (data->error_seen)
45482 return attrs;
45484 FOR_EACH_VEC_ELT (data->tokens, i, ce)
45486 tree c, cl;
45488 cp_parser_push_lexer_for_tokens (parser, ce);
45489 parser->lexer->in_pragma = true;
45490 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
45491 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
45492 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45493 const char *kind = IDENTIFIER_POINTER (id);
45494 cp_lexer_consume_token (parser->lexer);
45495 if (strcmp (kind, "simd") == 0)
45497 /* For now only in C++ attributes, do it always for OpenMP 5.1.
45498 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45499 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45500 cp_lexer_consume_token (parser->lexer); */
45502 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
45503 "#pragma omp declare simd",
45504 pragma_tok);
45505 if (cl)
45506 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
45507 c = build_tree_list (get_identifier ("omp declare simd"), cl);
45508 TREE_CHAIN (c) = attrs;
45509 if (processing_template_decl)
45510 ATTR_IS_DEPENDENT (c) = 1;
45511 attrs = c;
45513 else
45515 gcc_assert (strcmp (kind, "variant") == 0);
45516 attrs
45517 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
45519 cp_parser_pop_lexer (parser);
45522 cp_lexer *lexer = NULL;
45523 for (int i = 0; i < 2; i++)
45525 if (data->attribs[i] == NULL)
45526 continue;
45527 for (tree *pa = data->attribs[i]; *pa; )
45528 if (get_attribute_namespace (*pa) == omp_identifier
45529 && is_attribute_p ("directive", get_attribute_name (*pa)))
45531 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
45533 tree d = TREE_VALUE (a);
45534 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
45535 cp_token *first = DEFPARSE_TOKENS (d)->first;
45536 cp_token *last = DEFPARSE_TOKENS (d)->last;
45537 const char *directive[3] = {};
45538 for (int j = 0; j < 3; j++)
45540 tree id = NULL_TREE;
45541 if (first + j == last)
45542 break;
45543 if (first[j].type == CPP_NAME)
45544 id = first[j].u.value;
45545 else if (first[j].type == CPP_KEYWORD)
45546 id = ridpointers[(int) first[j].keyword];
45547 else
45548 break;
45549 directive[j] = IDENTIFIER_POINTER (id);
45551 const c_omp_directive *dir = NULL;
45552 if (directive[0])
45553 dir = c_omp_categorize_directive (directive[0], directive[1],
45554 directive[2]);
45555 if (dir == NULL)
45557 error_at (first->location,
45558 "unknown OpenMP directive name in "
45559 "%<omp::directive%> attribute argument");
45560 continue;
45562 if (dir->id != PRAGMA_OMP_DECLARE
45563 || (strcmp (directive[1], "simd") != 0
45564 && strcmp (directive[1], "variant") != 0))
45566 error_at (first->location,
45567 "OpenMP directive other than %<declare simd%> "
45568 "or %<declare variant%> appertains to a "
45569 "declaration");
45570 continue;
45573 if (parser->omp_attrs_forbidden_p)
45575 error_at (first->location,
45576 "mixing OpenMP directives with attribute and "
45577 "pragma syntax on the same statement");
45578 parser->omp_attrs_forbidden_p = false;
45581 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
45582 continue;
45583 if (lexer == NULL)
45585 lexer = cp_lexer_alloc ();
45586 lexer->debugging_p = parser->lexer->debugging_p;
45588 vec_safe_reserve (lexer->buffer, (last - first) + 2);
45589 cp_token tok = {};
45590 tok.type = CPP_PRAGMA;
45591 tok.keyword = RID_MAX;
45592 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
45593 tok.location = first->location;
45594 lexer->buffer->quick_push (tok);
45595 while (++first < last)
45596 lexer->buffer->quick_push (*first);
45597 tok = {};
45598 tok.type = CPP_PRAGMA_EOL;
45599 tok.keyword = RID_MAX;
45600 tok.location = last->location;
45601 lexer->buffer->quick_push (tok);
45602 tok = {};
45603 tok.type = CPP_EOF;
45604 tok.keyword = RID_MAX;
45605 tok.location = last->location;
45606 lexer->buffer->quick_push (tok);
45607 lexer->next = parser->lexer;
45608 lexer->next_token = lexer->buffer->address ();
45609 lexer->last_token = lexer->next_token
45610 + lexer->buffer->length ()
45611 - 1;
45612 lexer->in_omp_attribute_pragma = true;
45613 parser->lexer = lexer;
45614 /* Move the current source position to that of the first token
45615 in the new lexer. */
45616 cp_lexer_set_source_position_from_token (lexer->next_token);
45618 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
45619 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45620 const char *kind = IDENTIFIER_POINTER (id);
45621 cp_lexer_consume_token (parser->lexer);
45623 tree c, cl;
45624 if (strcmp (kind, "simd") == 0)
45626 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45627 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45628 cp_lexer_consume_token (parser->lexer);
45630 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
45631 cl = cp_parser_omp_all_clauses (parser, mask,
45632 "#pragma omp declare simd",
45633 pragma_tok);
45634 if (cl)
45635 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
45636 c = build_tree_list (get_identifier ("omp declare simd"),
45637 cl);
45638 TREE_CHAIN (c) = attrs;
45639 if (processing_template_decl)
45640 ATTR_IS_DEPENDENT (c) = 1;
45641 attrs = c;
45643 else
45645 gcc_assert (strcmp (kind, "variant") == 0);
45646 attrs
45647 = cp_finish_omp_declare_variant (parser, pragma_tok,
45648 attrs);
45650 gcc_assert (parser->lexer != lexer);
45651 vec_safe_truncate (lexer->buffer, 0);
45653 *pa = TREE_CHAIN (*pa);
45655 else
45656 pa = &TREE_CHAIN (*pa);
45658 if (lexer)
45659 cp_lexer_destroy (lexer);
45661 data->fndecl_seen = true;
45662 return attrs;
45665 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
45666 on #pragma omp declare target. Return false if errors were reported. */
45668 static bool
45669 handle_omp_declare_target_clause (tree c, tree t, int device_type)
45671 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
45672 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
45673 tree id;
45674 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
45676 id = get_identifier ("omp declare target link");
45677 std::swap (at1, at2);
45679 else
45680 id = get_identifier ("omp declare target");
45681 if (at2)
45683 error_at (OMP_CLAUSE_LOCATION (c),
45684 "%qD specified both in declare target %<link%> and %<to%>"
45685 " clauses", t);
45686 return false;
45688 if (!at1)
45690 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45691 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
45692 return true;
45694 symtab_node *node = symtab_node::get (t);
45695 if (node != NULL)
45697 node->offloadable = 1;
45698 if (ENABLE_OFFLOADING)
45700 g->have_offload = true;
45701 if (is_a <varpool_node *> (node))
45702 vec_safe_push (offload_vars, t);
45706 if (TREE_CODE (t) != FUNCTION_DECL)
45707 return true;
45708 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
45710 tree at3 = lookup_attribute ("omp declare target host",
45711 DECL_ATTRIBUTES (t));
45712 if (at3 == NULL_TREE)
45714 id = get_identifier ("omp declare target host");
45715 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45718 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
45720 tree at3 = lookup_attribute ("omp declare target nohost",
45721 DECL_ATTRIBUTES (t));
45722 if (at3 == NULL_TREE)
45724 id = get_identifier ("omp declare target nohost");
45725 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45728 return true;
45731 /* OpenMP 4.0:
45732 # pragma omp declare target new-line
45733 declarations and definitions
45734 # pragma omp end declare target new-line
45736 OpenMP 4.5:
45737 # pragma omp declare target ( extended-list ) new-line
45739 # pragma omp declare target declare-target-clauses[seq] new-line */
45741 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
45742 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
45743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
45744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
45746 static void
45747 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
45749 tree clauses = NULL_TREE;
45750 int device_type = 0;
45751 bool only_device_type = true;
45752 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
45753 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45754 || (parser->lexer->in_omp_attribute_pragma
45755 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45756 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
45757 clauses
45758 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
45759 "#pragma omp declare target", pragma_tok);
45760 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
45762 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
45763 clauses);
45764 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
45765 cp_parser_require_pragma_eol (parser, pragma_tok);
45767 else
45769 struct omp_declare_target_attr a
45770 = { parser->lexer->in_omp_attribute_pragma };
45771 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
45772 cp_parser_require_pragma_eol (parser, pragma_tok);
45773 return;
45775 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45776 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
45777 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
45778 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45780 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
45781 continue;
45782 tree t = OMP_CLAUSE_DECL (c);
45783 only_device_type = false;
45784 if (!handle_omp_declare_target_clause (c, t, device_type))
45785 continue;
45786 if (VAR_OR_FUNCTION_DECL_P (t)
45787 && DECL_LOCAL_DECL_P (t)
45788 && DECL_LANG_SPECIFIC (t)
45789 && DECL_LOCAL_DECL_ALIAS (t)
45790 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
45791 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
45792 device_type);
45794 if (device_type && only_device_type)
45795 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
45796 "directive with only %<device_type%> clauses ignored");
45799 static void
45800 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
45802 const char *p = "";
45803 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
45804 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45806 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45807 p = IDENTIFIER_POINTER (id);
45809 if (strcmp (p, "declare") == 0)
45811 cp_lexer_consume_token (parser->lexer);
45812 p = "";
45813 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45815 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45816 p = IDENTIFIER_POINTER (id);
45818 if (strcmp (p, "target") == 0)
45819 cp_lexer_consume_token (parser->lexer);
45820 else
45822 cp_parser_error (parser, "expected %<target%>");
45823 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45824 return;
45827 else
45829 cp_parser_error (parser, "expected %<declare%>");
45830 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45831 return;
45833 cp_parser_require_pragma_eol (parser, pragma_tok);
45834 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
45835 error_at (pragma_tok->location,
45836 "%<#pragma omp end declare target%> without corresponding "
45837 "%<#pragma omp declare target%>");
45838 else
45840 omp_declare_target_attr
45841 a = scope_chain->omp_declare_target_attribute->pop ();
45842 if (a.attr_syntax != in_omp_attribute_pragma)
45844 if (a.attr_syntax)
45845 error_at (pragma_tok->location,
45846 "%<declare target%> in attribute syntax terminated "
45847 "with %<end declare target%> in pragma syntax");
45848 else
45849 error_at (pragma_tok->location,
45850 "%<declare target%> in pragma syntax terminated "
45851 "with %<end declare target%> in attribute syntax");
45856 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
45857 expression and optional initializer clause of
45858 #pragma omp declare reduction. We store the expression(s) as
45859 either 3, 6 or 7 special statements inside of the artificial function's
45860 body. The first two statements are DECL_EXPRs for the artificial
45861 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
45862 expression that uses those variables.
45863 If there was any INITIALIZER clause, this is followed by further statements,
45864 the fourth and fifth statements are DECL_EXPRs for the artificial
45865 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
45866 constructor variant (first token after open paren is not omp_priv),
45867 then the sixth statement is a statement with the function call expression
45868 that uses the OMP_PRIV and optionally OMP_ORIG variable.
45869 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
45870 to initialize the OMP_PRIV artificial variable and there is seventh
45871 statement, a DECL_EXPR of the OMP_PRIV statement again. */
45873 static bool
45874 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
45876 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
45877 gcc_assert (TYPE_REF_P (type));
45878 type = TREE_TYPE (type);
45879 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
45880 DECL_ARTIFICIAL (omp_out) = 1;
45881 pushdecl (omp_out);
45882 add_decl_expr (omp_out);
45883 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
45884 DECL_ARTIFICIAL (omp_in) = 1;
45885 pushdecl (omp_in);
45886 add_decl_expr (omp_in);
45887 tree combiner;
45888 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
45890 keep_next_level (true);
45891 tree block = begin_omp_structured_block ();
45892 combiner = cp_parser_expression (parser);
45893 finish_expr_stmt (combiner);
45894 block = finish_omp_structured_block (block);
45895 if (processing_template_decl)
45896 block = build_stmt (input_location, EXPR_STMT, block);
45897 add_stmt (block);
45899 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
45900 return false;
45902 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45903 if (parser->lexer->in_omp_attribute_pragma
45904 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45905 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45906 cp_lexer_consume_token (parser->lexer);
45908 const char *p = "";
45909 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45911 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45912 p = IDENTIFIER_POINTER (id);
45915 if (strcmp (p, "initializer") == 0)
45917 cp_lexer_consume_token (parser->lexer);
45918 matching_parens parens;
45919 if (!parens.require_open (parser))
45920 return false;
45922 p = "";
45923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45925 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45926 p = IDENTIFIER_POINTER (id);
45929 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
45930 DECL_ARTIFICIAL (omp_priv) = 1;
45931 pushdecl (omp_priv);
45932 add_decl_expr (omp_priv);
45933 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
45934 DECL_ARTIFICIAL (omp_orig) = 1;
45935 pushdecl (omp_orig);
45936 add_decl_expr (omp_orig);
45938 keep_next_level (true);
45939 block = begin_omp_structured_block ();
45941 bool ctor = false;
45942 if (strcmp (p, "omp_priv") == 0)
45944 bool is_direct_init, is_non_constant_init;
45945 ctor = true;
45946 cp_lexer_consume_token (parser->lexer);
45947 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
45948 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
45949 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
45950 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
45951 == CPP_CLOSE_PAREN
45952 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
45953 == CPP_CLOSE_PAREN))
45955 finish_omp_structured_block (block);
45956 error ("invalid initializer clause");
45957 return false;
45959 initializer = cp_parser_initializer (parser, &is_direct_init,
45960 &is_non_constant_init);
45961 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
45962 NULL_TREE, LOOKUP_ONLYCONVERTING);
45964 else
45966 cp_parser_parse_tentatively (parser);
45967 /* Don't create location wrapper nodes here. */
45968 auto_suppress_location_wrappers sentinel;
45969 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
45970 /*check_dependency_p=*/true,
45971 /*template_p=*/NULL,
45972 /*declarator_p=*/false,
45973 /*optional_p=*/false);
45974 vec<tree, va_gc> *args;
45975 if (fn_name == error_mark_node
45976 || cp_parser_error_occurred (parser)
45977 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
45978 || ((args = cp_parser_parenthesized_expression_list
45979 (parser, non_attr, /*cast_p=*/false,
45980 /*allow_expansion_p=*/true,
45981 /*non_constant_p=*/NULL)),
45982 cp_parser_error_occurred (parser)))
45984 finish_omp_structured_block (block);
45985 cp_parser_abort_tentative_parse (parser);
45986 cp_parser_error (parser, "expected id-expression (arguments)");
45987 return false;
45989 unsigned int i;
45990 tree arg;
45991 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
45992 if (arg == omp_priv
45993 || (TREE_CODE (arg) == ADDR_EXPR
45994 && TREE_OPERAND (arg, 0) == omp_priv))
45995 break;
45996 cp_parser_abort_tentative_parse (parser);
45997 if (arg == NULL_TREE)
45998 error ("one of the initializer call arguments should be %<omp_priv%>"
45999 " or %<&omp_priv%>");
46000 initializer = cp_parser_postfix_expression (parser, false, false, false,
46001 false, NULL);
46002 finish_expr_stmt (initializer);
46005 block = finish_omp_structured_block (block);
46006 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
46007 if (processing_template_decl)
46008 block = build_stmt (input_location, EXPR_STMT, block);
46009 add_stmt (block);
46011 if (ctor)
46012 add_decl_expr (omp_orig);
46014 if (!parens.require_close (parser))
46015 return false;
46018 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
46019 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
46020 UNKNOWN_LOCATION);
46022 return true;
46025 /* OpenMP 4.0
46026 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46027 initializer-clause[opt] new-line
46029 initializer-clause:
46030 initializer (omp_priv initializer)
46031 initializer (function-name (argument-list)) */
46033 static void
46034 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
46035 enum pragma_context)
46037 auto_vec<tree> types;
46038 enum tree_code reduc_code = ERROR_MARK;
46039 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
46040 unsigned int i;
46041 cp_token *first_token;
46042 cp_token_cache *cp;
46043 int errs;
46044 void *p;
46046 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
46047 p = obstack_alloc (&declarator_obstack, 0);
46049 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
46050 goto fail;
46052 switch (cp_lexer_peek_token (parser->lexer)->type)
46054 case CPP_PLUS:
46055 reduc_code = PLUS_EXPR;
46056 break;
46057 case CPP_MULT:
46058 reduc_code = MULT_EXPR;
46059 break;
46060 case CPP_MINUS:
46061 reduc_code = MINUS_EXPR;
46062 break;
46063 case CPP_AND:
46064 reduc_code = BIT_AND_EXPR;
46065 break;
46066 case CPP_XOR:
46067 reduc_code = BIT_XOR_EXPR;
46068 break;
46069 case CPP_OR:
46070 reduc_code = BIT_IOR_EXPR;
46071 break;
46072 case CPP_AND_AND:
46073 reduc_code = TRUTH_ANDIF_EXPR;
46074 break;
46075 case CPP_OR_OR:
46076 reduc_code = TRUTH_ORIF_EXPR;
46077 break;
46078 case CPP_NAME:
46079 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
46080 break;
46081 default:
46082 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
46083 "%<|%>, %<&&%>, %<||%> or identifier");
46084 goto fail;
46087 if (reduc_code != ERROR_MARK)
46088 cp_lexer_consume_token (parser->lexer);
46090 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
46091 if (reduc_id == error_mark_node)
46092 goto fail;
46094 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
46095 goto fail;
46097 /* Types may not be defined in declare reduction type list. */
46098 const char *saved_message;
46099 saved_message = parser->type_definition_forbidden_message;
46100 parser->type_definition_forbidden_message
46101 = G_("types may not be defined in declare reduction type list");
46102 bool saved_colon_corrects_to_scope_p;
46103 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
46104 parser->colon_corrects_to_scope_p = false;
46105 bool saved_colon_doesnt_start_class_def_p;
46106 saved_colon_doesnt_start_class_def_p
46107 = parser->colon_doesnt_start_class_def_p;
46108 parser->colon_doesnt_start_class_def_p = true;
46110 while (true)
46112 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46113 type = cp_parser_type_id (parser);
46114 if (type == error_mark_node)
46116 else if (ARITHMETIC_TYPE_P (type)
46117 && (orig_reduc_id == NULL_TREE
46118 || (TREE_CODE (type) != COMPLEX_TYPE
46119 && (id_equal (orig_reduc_id, "min")
46120 || id_equal (orig_reduc_id, "max")))))
46121 error_at (loc, "predeclared arithmetic type %qT in "
46122 "%<#pragma omp declare reduction%>", type);
46123 else if (FUNC_OR_METHOD_TYPE_P (type)
46124 || TREE_CODE (type) == ARRAY_TYPE)
46125 error_at (loc, "function or array type %qT in "
46126 "%<#pragma omp declare reduction%>", type);
46127 else if (TYPE_REF_P (type))
46128 error_at (loc, "reference type %qT in "
46129 "%<#pragma omp declare reduction%>", type);
46130 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
46131 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
46132 "type %qT in %<#pragma omp declare reduction%>", type);
46133 else
46134 types.safe_push (type);
46136 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46137 cp_lexer_consume_token (parser->lexer);
46138 else
46139 break;
46142 /* Restore the saved message. */
46143 parser->type_definition_forbidden_message = saved_message;
46144 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
46145 parser->colon_doesnt_start_class_def_p
46146 = saved_colon_doesnt_start_class_def_p;
46148 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
46149 || types.is_empty ())
46151 fail:
46152 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46153 goto done;
46156 first_token = cp_lexer_peek_token (parser->lexer);
46157 cp = NULL;
46158 errs = errorcount;
46159 FOR_EACH_VEC_ELT (types, i, type)
46161 tree fntype
46162 = build_function_type_list (void_type_node,
46163 cp_build_reference_type (type, false),
46164 NULL_TREE);
46165 tree this_reduc_id = reduc_id;
46166 if (!dependent_type_p (type))
46167 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
46168 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
46169 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
46170 DECL_ARTIFICIAL (fndecl) = 1;
46171 DECL_EXTERNAL (fndecl) = 1;
46172 DECL_DECLARED_INLINE_P (fndecl) = 1;
46173 DECL_IGNORED_P (fndecl) = 1;
46174 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
46175 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
46176 DECL_ATTRIBUTES (fndecl)
46177 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
46178 DECL_ATTRIBUTES (fndecl));
46179 bool block_scope = false;
46180 if (current_function_decl)
46182 block_scope = true;
46183 DECL_CONTEXT (fndecl) = current_function_decl;
46184 DECL_LOCAL_DECL_P (fndecl) = true;
46187 if (processing_template_decl)
46188 fndecl = push_template_decl (fndecl);
46190 if (block_scope)
46192 if (!processing_template_decl)
46193 pushdecl (fndecl);
46195 else if (current_class_type)
46197 if (cp == NULL)
46199 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46200 cp_lexer_consume_token (parser->lexer);
46201 cp = cp_token_cache_new (first_token,
46202 cp_lexer_peek_nth_token (parser->lexer,
46203 2));
46205 DECL_STATIC_FUNCTION_P (fndecl) = 1;
46206 finish_member_declaration (fndecl);
46207 DECL_PENDING_INLINE_INFO (fndecl) = cp;
46208 DECL_PENDING_INLINE_P (fndecl) = 1;
46209 vec_safe_push (unparsed_funs_with_definitions, fndecl);
46210 continue;
46212 else
46214 DECL_CONTEXT (fndecl) = current_namespace;
46215 tree d = pushdecl (fndecl);
46216 /* We should never meet a matched duplicate decl. */
46217 gcc_checking_assert (d == error_mark_node || d == fndecl);
46220 tree block = NULL_TREE;
46221 if (!block_scope)
46222 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
46223 else
46224 block = begin_omp_structured_block ();
46225 if (cp)
46227 cp_parser_push_lexer_for_tokens (parser, cp);
46228 parser->lexer->in_pragma = true;
46231 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
46233 if (cp)
46234 cp_parser_pop_lexer (parser);
46235 if (!block_scope)
46236 finish_function (/*inline_p=*/false);
46237 else
46239 DECL_CONTEXT (fndecl) = current_function_decl;
46240 if (DECL_TEMPLATE_INFO (fndecl))
46241 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
46243 if (!ok)
46244 goto fail;
46246 if (block_scope)
46248 block = finish_omp_structured_block (block);
46249 if (TREE_CODE (block) == BIND_EXPR)
46250 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
46251 else if (TREE_CODE (block) == STATEMENT_LIST)
46252 DECL_SAVED_TREE (fndecl) = block;
46253 if (processing_template_decl)
46254 add_decl_expr (fndecl);
46257 cp_check_omp_declare_reduction (fndecl);
46258 if (cp == NULL && types.length () > 1)
46259 cp = cp_token_cache_new (first_token,
46260 cp_lexer_peek_nth_token (parser->lexer, 2));
46261 if (errs != errorcount)
46262 break;
46265 cp_parser_require_pragma_eol (parser, pragma_tok);
46267 done:
46268 /* Free any declarators allocated. */
46269 obstack_free (&declarator_obstack, p);
46272 /* OpenMP 4.0
46273 #pragma omp declare simd declare-simd-clauses[optseq] new-line
46274 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46275 initializer-clause[opt] new-line
46276 #pragma omp declare target new-line
46278 OpenMP 5.0
46279 #pragma omp declare variant (identifier) match (context-selector) */
46281 static bool
46282 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
46283 enum pragma_context context)
46285 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46287 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46288 const char *p = IDENTIFIER_POINTER (id);
46290 if (strcmp (p, "simd") == 0)
46292 cp_lexer_consume_token (parser->lexer);
46293 cp_parser_omp_declare_simd (parser, pragma_tok,
46294 context, false);
46295 return true;
46297 if (flag_openmp && strcmp (p, "variant") == 0)
46299 cp_lexer_consume_token (parser->lexer);
46300 cp_parser_omp_declare_simd (parser, pragma_tok,
46301 context, true);
46302 return true;
46304 cp_ensure_no_omp_declare_simd (parser);
46305 if (strcmp (p, "reduction") == 0)
46307 cp_lexer_consume_token (parser->lexer);
46308 cp_parser_omp_declare_reduction (parser, pragma_tok,
46309 context);
46310 return false;
46312 if (!flag_openmp) /* flag_openmp_simd */
46314 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46315 return false;
46317 if (strcmp (p, "target") == 0)
46319 cp_lexer_consume_token (parser->lexer);
46320 cp_parser_omp_declare_target (parser, pragma_tok);
46321 return false;
46324 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
46325 "%<target%> or %<variant%>");
46326 cp_parser_require_pragma_eol (parser, pragma_tok);
46327 return false;
46330 /* OpenMP 5.0
46331 #pragma omp requires clauses[optseq] new-line */
46333 static bool
46334 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
46336 bool first = true;
46337 enum omp_requires new_req = (enum omp_requires) 0;
46339 location_t loc = pragma_tok->location;
46340 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46342 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46343 if ((!first || parser->lexer->in_omp_attribute_pragma)
46344 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46345 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46346 cp_lexer_consume_token (parser->lexer);
46348 first = false;
46350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46352 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46353 const char *p = IDENTIFIER_POINTER (id);
46354 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46355 enum omp_requires this_req = (enum omp_requires) 0;
46357 if (!strcmp (p, "unified_address"))
46358 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
46359 else if (!strcmp (p, "unified_shared_memory"))
46360 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
46361 else if (!strcmp (p, "dynamic_allocators"))
46362 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
46363 else if (!strcmp (p, "reverse_offload"))
46364 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
46365 else if (!strcmp (p, "atomic_default_mem_order"))
46367 cp_lexer_consume_token (parser->lexer);
46369 matching_parens parens;
46370 if (parens.require_open (parser))
46372 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46374 id = cp_lexer_peek_token (parser->lexer)->u.value;
46375 p = IDENTIFIER_POINTER (id);
46377 if (!strcmp (p, "seq_cst"))
46378 this_req
46379 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
46380 else if (!strcmp (p, "relaxed"))
46381 this_req
46382 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
46383 else if (!strcmp (p, "acq_rel"))
46384 this_req
46385 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
46387 if (this_req == 0)
46389 error_at (cp_lexer_peek_token (parser->lexer)->location,
46390 "expected %<seq_cst%>, %<relaxed%> or "
46391 "%<acq_rel%>");
46392 switch (cp_lexer_peek_token (parser->lexer)->type)
46394 case CPP_EOF:
46395 case CPP_PRAGMA_EOL:
46396 case CPP_CLOSE_PAREN:
46397 break;
46398 default:
46399 if (cp_lexer_nth_token_is (parser->lexer, 2,
46400 CPP_CLOSE_PAREN))
46401 cp_lexer_consume_token (parser->lexer);
46402 break;
46405 else
46406 cp_lexer_consume_token (parser->lexer);
46408 if (!parens.require_close (parser))
46409 cp_parser_skip_to_closing_parenthesis (parser,
46410 /*recovering=*/true,
46411 /*or_comma=*/false,
46412 /*consume_paren=*/
46413 true);
46415 if (this_req == 0)
46417 cp_parser_require_pragma_eol (parser, pragma_tok);
46418 return false;
46421 p = NULL;
46423 else
46425 error_at (cloc, "expected %<unified_address%>, "
46426 "%<unified_shared_memory%>, "
46427 "%<dynamic_allocators%>, "
46428 "%<reverse_offload%> "
46429 "or %<atomic_default_mem_order%> clause");
46430 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46431 return false;
46433 if (p)
46434 sorry_at (cloc, "%qs clause on %<requires%> directive not "
46435 "supported yet", p);
46436 if (p)
46437 cp_lexer_consume_token (parser->lexer);
46438 if (this_req)
46440 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46442 if ((this_req & new_req) != 0)
46443 error_at (cloc, "too many %qs clauses", p);
46444 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
46445 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
46446 error_at (cloc, "%qs clause used lexically after first "
46447 "target construct or offloading API", p);
46449 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46451 error_at (cloc, "too many %qs clauses",
46452 "atomic_default_mem_order");
46453 this_req = (enum omp_requires) 0;
46455 else if ((omp_requires_mask
46456 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46458 error_at (cloc, "more than one %<atomic_default_mem_order%>"
46459 " clause in a single compilation unit");
46460 this_req
46461 = (enum omp_requires)
46462 (omp_requires_mask
46463 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
46465 else if ((omp_requires_mask
46466 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
46467 error_at (cloc, "%<atomic_default_mem_order%> clause used "
46468 "lexically after first %<atomic%> construct "
46469 "without memory order clause");
46470 new_req = (enum omp_requires) (new_req | this_req);
46471 omp_requires_mask
46472 = (enum omp_requires) (omp_requires_mask | this_req);
46473 continue;
46476 break;
46478 cp_parser_require_pragma_eol (parser, pragma_tok);
46480 if (new_req == 0)
46481 error_at (loc, "%<pragma omp requires%> requires at least one clause");
46482 return false;
46486 /* OpenMP 5.1:
46487 #pragma omp nothing new-line */
46489 static void
46490 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
46492 cp_parser_require_pragma_eol (parser, pragma_tok);
46496 /* OpenMP 5.1
46497 #pragma omp error clauses[optseq] new-line */
46499 static bool
46500 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
46501 enum pragma_context context)
46503 int at_compilation = -1;
46504 int severity_fatal = -1;
46505 tree message = NULL_TREE;
46506 bool first = true;
46507 bool bad = false;
46508 location_t loc = pragma_tok->location;
46510 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46512 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46513 if ((!first || parser->lexer->in_omp_attribute_pragma)
46514 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46515 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46516 cp_lexer_consume_token (parser->lexer);
46518 first = false;
46520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
46521 break;
46523 const char *p
46524 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46525 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46526 static const char *args[] = {
46527 "execution", "compilation", "warning", "fatal"
46529 int *v = NULL;
46530 int idx = 0, n = -1;
46531 tree m = NULL_TREE;
46533 if (!strcmp (p, "at"))
46534 v = &at_compilation;
46535 else if (!strcmp (p, "severity"))
46537 v = &severity_fatal;
46538 idx += 2;
46540 else if (strcmp (p, "message"))
46542 error_at (cloc,
46543 "expected %<at%>, %<severity%> or %<message%> clause");
46544 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46545 return false;
46548 cp_lexer_consume_token (parser->lexer);
46550 matching_parens parens;
46551 if (parens.require_open (parser))
46553 if (v == NULL)
46555 m = cp_parser_assignment_expression (parser);
46556 if (type_dependent_expression_p (m))
46557 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
46558 else
46559 m = perform_implicit_conversion_flags (const_string_type_node, m,
46560 tf_warning_or_error,
46561 LOOKUP_NORMAL);
46563 else
46565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46567 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
46568 const char *q = IDENTIFIER_POINTER (val);
46570 if (!strcmp (q, args[idx]))
46571 n = 0;
46572 else if (!strcmp (q, args[idx + 1]))
46573 n = 1;
46575 if (n == -1)
46577 error_at (cp_lexer_peek_token (parser->lexer)->location,
46578 "expected %qs or %qs", args[idx], args[idx + 1]);
46579 bad = true;
46580 switch (cp_lexer_peek_token (parser->lexer)->type)
46582 case CPP_EOF:
46583 case CPP_PRAGMA_EOL:
46584 case CPP_CLOSE_PAREN:
46585 break;
46586 default:
46587 if (cp_lexer_nth_token_is (parser->lexer, 2,
46588 CPP_CLOSE_PAREN))
46589 cp_lexer_consume_token (parser->lexer);
46590 break;
46593 else
46594 cp_lexer_consume_token (parser->lexer);
46597 if (!parens.require_close (parser))
46598 cp_parser_skip_to_closing_parenthesis (parser,
46599 /*recovering=*/true,
46600 /*or_comma=*/false,
46601 /*consume_paren=*/
46602 true);
46604 if (v == NULL)
46606 if (message)
46608 error_at (cloc, "too many %qs clauses", p);
46609 bad = true;
46611 else
46612 message = m;
46614 else if (n != -1)
46616 if (*v != -1)
46618 error_at (cloc, "too many %qs clauses", p);
46619 bad = true;
46621 else
46622 *v = n;
46625 else
46626 bad = true;
46628 cp_parser_require_pragma_eol (parser, pragma_tok);
46629 if (bad)
46630 return true;
46632 if (at_compilation == -1)
46633 at_compilation = 1;
46634 if (severity_fatal == -1)
46635 severity_fatal = 1;
46636 if (!at_compilation)
46638 if (context != pragma_compound)
46640 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
46641 "may only be used in compound statements");
46642 return true;
46644 tree fndecl
46645 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
46646 : BUILT_IN_GOMP_WARNING);
46647 if (!message)
46648 message = build_zero_cst (const_string_type_node);
46649 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
46650 build_all_ones_cst (size_type_node));
46651 add_stmt (stmt);
46652 return true;
46655 if (in_discarded_stmt)
46656 return false;
46658 const char *msg = NULL;
46659 if (message)
46661 msg = c_getstr (fold_for_warn (message));
46662 if (msg == NULL)
46663 msg = _("<message unknown at compile time>");
46665 if (msg)
46666 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
46667 "%<pragma omp error%> encountered: %s", msg);
46668 else
46669 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
46670 "%<pragma omp error%> encountered");
46671 return false;
46674 /* OpenMP 4.5:
46675 #pragma omp taskloop taskloop-clause[optseq] new-line
46676 for-loop
46678 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
46679 for-loop */
46681 #define OMP_TASKLOOP_CLAUSE_MASK \
46682 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
46687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
46688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
46689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
46690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
46691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
46693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
46694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
46695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
46696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
46700 static tree
46701 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
46702 char *p_name, omp_clause_mask mask, tree *cclauses,
46703 bool *if_p)
46705 tree clauses, sb, ret;
46706 unsigned int save;
46707 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46709 strcat (p_name, " taskloop");
46710 mask |= OMP_TASKLOOP_CLAUSE_MASK;
46711 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
46712 clause. */
46713 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
46714 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
46716 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46718 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46719 const char *p = IDENTIFIER_POINTER (id);
46721 if (strcmp (p, "simd") == 0)
46723 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46724 if (cclauses == NULL)
46725 cclauses = cclauses_buf;
46727 cp_lexer_consume_token (parser->lexer);
46728 if (!flag_openmp) /* flag_openmp_simd */
46729 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46730 cclauses, if_p);
46731 sb = begin_omp_structured_block ();
46732 save = cp_parser_begin_omp_structured_block (parser);
46733 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46734 cclauses, if_p);
46735 cp_parser_end_omp_structured_block (parser, save);
46736 tree body = finish_omp_structured_block (sb);
46737 if (ret == NULL)
46738 return ret;
46739 ret = make_node (OMP_TASKLOOP);
46740 TREE_TYPE (ret) = void_type_node;
46741 OMP_FOR_BODY (ret) = body;
46742 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
46743 SET_EXPR_LOCATION (ret, loc);
46744 add_stmt (ret);
46745 return ret;
46748 if (!flag_openmp) /* flag_openmp_simd */
46750 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46751 return NULL_TREE;
46754 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46755 cclauses == NULL);
46756 if (cclauses)
46758 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
46759 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
46762 keep_next_level (true);
46763 sb = begin_omp_structured_block ();
46764 save = cp_parser_begin_omp_structured_block (parser);
46766 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
46767 if_p);
46769 cp_parser_end_omp_structured_block (parser, save);
46770 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
46772 return ret;
46776 /* OpenACC 2.0:
46777 # pragma acc routine oacc-routine-clause[optseq] new-line
46778 function-definition
46780 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
46783 #define OACC_ROUTINE_CLAUSE_MASK \
46784 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
46785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
46786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
46787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
46788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
46790 /* Parse the OpenACC routine pragma. This has an optional '( name )'
46791 component, which must resolve to a declared namespace-scope
46792 function. The clauses are either processed directly (for a named
46793 function), or defered until the immediatley following declaration
46794 is parsed. */
46796 static void
46797 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
46798 enum pragma_context context)
46800 gcc_checking_assert (context == pragma_external);
46801 /* The checking for "another pragma following this one" in the "no optional
46802 '( name )'" case makes sure that we dont re-enter. */
46803 gcc_checking_assert (parser->oacc_routine == NULL);
46805 cp_oacc_routine_data data;
46806 data.error_seen = false;
46807 data.fndecl_seen = false;
46808 data.tokens = vNULL;
46809 data.clauses = NULL_TREE;
46810 data.loc = pragma_tok->location;
46811 /* It is safe to take the address of a local variable; it will only be
46812 used while this scope is live. */
46813 parser->oacc_routine = &data;
46815 /* Look for optional '( name )'. */
46816 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46818 matching_parens parens;
46819 parens.consume_open (parser); /* '(' */
46821 /* We parse the name as an id-expression. If it resolves to
46822 anything other than a non-overloaded function at namespace
46823 scope, it's an error. */
46824 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
46825 tree name = cp_parser_id_expression (parser,
46826 /*template_keyword_p=*/false,
46827 /*check_dependency_p=*/false,
46828 /*template_p=*/NULL,
46829 /*declarator_p=*/false,
46830 /*optional_p=*/false);
46831 tree decl = (identifier_p (name)
46832 ? cp_parser_lookup_name_simple (parser, name, name_loc)
46833 : name);
46834 if (name != error_mark_node && decl == error_mark_node)
46835 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
46837 if (decl == error_mark_node
46838 || !parens.require_close (parser))
46840 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46841 parser->oacc_routine = NULL;
46842 return;
46845 data.clauses
46846 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
46847 "#pragma acc routine",
46848 cp_lexer_peek_token (parser->lexer));
46849 /* The clauses are in reverse order; fix that to make later diagnostic
46850 emission easier. */
46851 data.clauses = nreverse (data.clauses);
46853 if (decl && is_overloaded_fn (decl)
46854 && (TREE_CODE (decl) != FUNCTION_DECL
46855 || DECL_FUNCTION_TEMPLATE_P (decl)))
46857 error_at (name_loc,
46858 "%<#pragma acc routine%> names a set of overloads");
46859 parser->oacc_routine = NULL;
46860 return;
46863 /* Perhaps we should use the same rule as declarations in different
46864 namespaces? */
46865 if (!DECL_NAMESPACE_SCOPE_P (decl))
46867 error_at (name_loc,
46868 "%qD does not refer to a namespace scope function", decl);
46869 parser->oacc_routine = NULL;
46870 return;
46873 if (TREE_CODE (decl) != FUNCTION_DECL)
46875 error_at (name_loc, "%qD does not refer to a function", decl);
46876 parser->oacc_routine = NULL;
46877 return;
46880 cp_finalize_oacc_routine (parser, decl, false);
46881 parser->oacc_routine = NULL;
46883 else /* No optional '( name )'. */
46885 /* Store away all pragma tokens. */
46886 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46887 cp_lexer_consume_token (parser->lexer);
46888 cp_parser_require_pragma_eol (parser, pragma_tok);
46889 struct cp_token_cache *cp
46890 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
46891 parser->oacc_routine->tokens.safe_push (cp);
46893 /* Emit a helpful diagnostic if there's another pragma following this
46894 one. */
46895 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
46897 cp_ensure_no_oacc_routine (parser);
46898 data.tokens.release ();
46899 /* ..., and then just keep going. */
46900 return;
46903 /* We only have to consider the pragma_external case here. */
46904 cp_parser_declaration (parser, NULL_TREE);
46905 if (parser->oacc_routine
46906 && !parser->oacc_routine->fndecl_seen)
46907 cp_ensure_no_oacc_routine (parser);
46908 else
46909 parser->oacc_routine = NULL;
46910 data.tokens.release ();
46914 /* Finalize #pragma acc routine clauses after direct declarator has
46915 been parsed. */
46917 static tree
46918 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
46920 struct cp_token_cache *ce;
46921 cp_oacc_routine_data *data = parser->oacc_routine;
46923 if (!data->error_seen && data->fndecl_seen)
46925 error_at (data->loc,
46926 "%<#pragma acc routine%> not immediately followed by "
46927 "a single function declaration or definition");
46928 data->error_seen = true;
46930 if (data->error_seen)
46931 return attrs;
46933 gcc_checking_assert (data->tokens.length () == 1);
46934 ce = data->tokens[0];
46936 cp_parser_push_lexer_for_tokens (parser, ce);
46937 parser->lexer->in_pragma = true;
46938 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
46940 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
46941 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
46942 parser->oacc_routine->clauses
46943 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
46944 "#pragma acc routine", pragma_tok);
46945 /* The clauses are in reverse order; fix that to make later diagnostic
46946 emission easier. */
46947 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
46948 cp_parser_pop_lexer (parser);
46949 /* Later, cp_finalize_oacc_routine will process the clauses. */
46950 parser->oacc_routine->fndecl_seen = true;
46952 return attrs;
46955 /* Apply any saved OpenACC routine clauses to a just-parsed
46956 declaration. */
46958 static void
46959 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
46961 if (__builtin_expect (parser->oacc_routine != NULL, 0))
46963 /* Keep going if we're in error reporting mode. */
46964 if (parser->oacc_routine->error_seen
46965 || fndecl == error_mark_node)
46966 return;
46968 if (TREE_CODE (fndecl) != FUNCTION_DECL)
46970 if (parser->oacc_routine->fndecl_seen)
46972 error_at (parser->oacc_routine->loc,
46973 "%<#pragma acc routine%> not immediately followed by"
46974 " a single function declaration or definition");
46975 parser->oacc_routine = NULL;
46976 return;
46979 cp_ensure_no_oacc_routine (parser);
46980 return;
46983 int compatible
46984 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
46985 parser->oacc_routine->loc,
46986 "#pragma acc routine");
46987 if (compatible < 0)
46989 parser->oacc_routine = NULL;
46990 return;
46992 if (compatible > 0)
46995 else
46997 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
46999 error_at (parser->oacc_routine->loc,
47000 TREE_USED (fndecl)
47001 ? G_("%<#pragma acc routine%> must be applied before"
47002 " use")
47003 : G_("%<#pragma acc routine%> must be applied before"
47004 " definition"));
47005 parser->oacc_routine = NULL;
47006 return;
47009 /* Set the routine's level of parallelism. */
47010 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
47011 oacc_replace_fn_attrib (fndecl, dims);
47013 /* Add an "omp declare target" attribute. */
47014 DECL_ATTRIBUTES (fndecl)
47015 = tree_cons (get_identifier ("omp declare target"),
47016 parser->oacc_routine->clauses,
47017 DECL_ATTRIBUTES (fndecl));
47022 /* Main entry point to OpenMP statement pragmas. */
47024 static void
47025 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47027 tree stmt;
47028 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
47029 omp_clause_mask mask (0);
47031 switch (cp_parser_pragma_kind (pragma_tok))
47033 case PRAGMA_OACC_ATOMIC:
47034 cp_parser_omp_atomic (parser, pragma_tok, true);
47035 return;
47036 case PRAGMA_OACC_CACHE:
47037 stmt = cp_parser_oacc_cache (parser, pragma_tok);
47038 break;
47039 case PRAGMA_OACC_DATA:
47040 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
47041 break;
47042 case PRAGMA_OACC_ENTER_DATA:
47043 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
47044 break;
47045 case PRAGMA_OACC_EXIT_DATA:
47046 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
47047 break;
47048 case PRAGMA_OACC_HOST_DATA:
47049 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
47050 break;
47051 case PRAGMA_OACC_KERNELS:
47052 case PRAGMA_OACC_PARALLEL:
47053 case PRAGMA_OACC_SERIAL:
47054 strcpy (p_name, "#pragma acc");
47055 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
47056 break;
47057 case PRAGMA_OACC_LOOP:
47058 strcpy (p_name, "#pragma acc");
47059 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
47060 if_p);
47061 break;
47062 case PRAGMA_OACC_UPDATE:
47063 stmt = cp_parser_oacc_update (parser, pragma_tok);
47064 break;
47065 case PRAGMA_OACC_WAIT:
47066 stmt = cp_parser_oacc_wait (parser, pragma_tok);
47067 break;
47068 case PRAGMA_OMP_ALLOCATE:
47069 cp_parser_omp_allocate (parser, pragma_tok);
47070 return;
47071 case PRAGMA_OMP_ATOMIC:
47072 cp_parser_omp_atomic (parser, pragma_tok, false);
47073 return;
47074 case PRAGMA_OMP_CRITICAL:
47075 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
47076 break;
47077 case PRAGMA_OMP_DISTRIBUTE:
47078 strcpy (p_name, "#pragma omp");
47079 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
47080 if_p);
47081 break;
47082 case PRAGMA_OMP_FOR:
47083 strcpy (p_name, "#pragma omp");
47084 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
47085 if_p);
47086 break;
47087 case PRAGMA_OMP_LOOP:
47088 strcpy (p_name, "#pragma omp");
47089 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
47090 if_p);
47091 break;
47092 case PRAGMA_OMP_MASKED:
47093 strcpy (p_name, "#pragma omp");
47094 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
47095 if_p);
47096 break;
47097 case PRAGMA_OMP_MASTER:
47098 strcpy (p_name, "#pragma omp");
47099 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
47100 if_p);
47101 break;
47102 case PRAGMA_OMP_PARALLEL:
47103 strcpy (p_name, "#pragma omp");
47104 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
47105 if_p);
47106 break;
47107 case PRAGMA_OMP_SCOPE:
47108 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
47109 break;
47110 case PRAGMA_OMP_SECTIONS:
47111 strcpy (p_name, "#pragma omp");
47112 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
47113 break;
47114 case PRAGMA_OMP_SIMD:
47115 strcpy (p_name, "#pragma omp");
47116 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
47117 if_p);
47118 break;
47119 case PRAGMA_OMP_SINGLE:
47120 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
47121 break;
47122 case PRAGMA_OMP_TASK:
47123 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
47124 break;
47125 case PRAGMA_OMP_TASKGROUP:
47126 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
47127 break;
47128 case PRAGMA_OMP_TASKLOOP:
47129 strcpy (p_name, "#pragma omp");
47130 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
47131 if_p);
47132 break;
47133 case PRAGMA_OMP_TEAMS:
47134 strcpy (p_name, "#pragma omp");
47135 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
47136 if_p);
47137 break;
47138 default:
47139 gcc_unreachable ();
47142 protected_set_expr_location (stmt, pragma_tok->location);
47145 /* Transactional Memory parsing routines. */
47147 /* Parse a transaction attribute.
47149 txn-attribute:
47150 attribute
47151 [ [ identifier ] ]
47153 We use this instead of cp_parser_attributes_opt for transactions to avoid
47154 the pedwarn in C++98 mode. */
47156 static tree
47157 cp_parser_txn_attribute_opt (cp_parser *parser)
47159 cp_token *token;
47160 tree attr_name, attr = NULL;
47162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
47163 return cp_parser_attributes_opt (parser);
47165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
47166 return NULL_TREE;
47167 cp_lexer_consume_token (parser->lexer);
47168 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
47169 goto error1;
47171 token = cp_lexer_peek_token (parser->lexer);
47172 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
47174 token = cp_lexer_consume_token (parser->lexer);
47176 attr_name = (token->type == CPP_KEYWORD
47177 /* For keywords, use the canonical spelling,
47178 not the parsed identifier. */
47179 ? ridpointers[(int) token->keyword]
47180 : token->u.value);
47181 attr = build_tree_list (attr_name, NULL_TREE);
47183 else
47184 cp_parser_error (parser, "expected identifier");
47186 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
47187 error1:
47188 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
47189 return attr;
47192 /* Parse a __transaction_atomic or __transaction_relaxed statement.
47194 transaction-statement:
47195 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
47196 compound-statement
47197 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
47200 static tree
47201 cp_parser_transaction (cp_parser *parser, cp_token *token)
47203 unsigned char old_in = parser->in_transaction;
47204 unsigned char this_in = 1, new_in;
47205 enum rid keyword = token->keyword;
47206 tree stmt, attrs, noex;
47208 cp_lexer_consume_token (parser->lexer);
47210 if (keyword == RID_TRANSACTION_RELAXED
47211 || keyword == RID_SYNCHRONIZED)
47212 this_in |= TM_STMT_ATTR_RELAXED;
47213 else
47215 attrs = cp_parser_txn_attribute_opt (parser);
47216 if (attrs)
47217 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
47220 /* Parse a noexcept specification. */
47221 if (keyword == RID_ATOMIC_NOEXCEPT)
47222 noex = boolean_true_node;
47223 else if (keyword == RID_ATOMIC_CANCEL)
47225 /* cancel-and-throw is unimplemented. */
47226 sorry ("%<atomic_cancel%>");
47227 noex = NULL_TREE;
47229 else
47230 noex = cp_parser_noexcept_specification_opt (parser,
47231 CP_PARSER_FLAGS_NONE,
47232 /*require_constexpr=*/true,
47233 /*consumed_expr=*/NULL,
47234 /*return_cond=*/true);
47236 /* Keep track if we're in the lexical scope of an outer transaction. */
47237 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
47239 stmt = begin_transaction_stmt (token->location, NULL, this_in);
47241 parser->in_transaction = new_in;
47242 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
47243 parser->in_transaction = old_in;
47245 finish_transaction_stmt (stmt, NULL, this_in, noex);
47247 return stmt;
47250 /* Parse a __transaction_atomic or __transaction_relaxed expression.
47252 transaction-expression:
47253 __transaction_atomic txn-noexcept-spec[opt] ( expression )
47254 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
47257 static tree
47258 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
47260 unsigned char old_in = parser->in_transaction;
47261 unsigned char this_in = 1;
47262 cp_token *token;
47263 tree expr, noex;
47264 bool noex_expr;
47265 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47267 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
47268 || keyword == RID_TRANSACTION_RELAXED);
47270 if (!flag_tm)
47271 error_at (loc,
47272 keyword == RID_TRANSACTION_RELAXED
47273 ? G_("%<__transaction_relaxed%> without transactional memory "
47274 "support enabled")
47275 : G_("%<__transaction_atomic%> without transactional memory "
47276 "support enabled"));
47278 token = cp_parser_require_keyword (parser, keyword,
47279 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
47280 : RT_TRANSACTION_RELAXED));
47281 gcc_assert (token != NULL);
47283 if (keyword == RID_TRANSACTION_RELAXED)
47284 this_in |= TM_STMT_ATTR_RELAXED;
47286 /* Set this early. This might mean that we allow transaction_cancel in
47287 an expression that we find out later actually has to be a constexpr.
47288 However, we expect that cxx_constant_value will be able to deal with
47289 this; also, if the noexcept has no constexpr, then what we parse next
47290 really is a transaction's body. */
47291 parser->in_transaction = this_in;
47293 /* Parse a noexcept specification. */
47294 noex = cp_parser_noexcept_specification_opt (parser,
47295 CP_PARSER_FLAGS_NONE,
47296 /*require_constexpr=*/false,
47297 &noex_expr,
47298 /*return_cond=*/true);
47300 if (!noex || !noex_expr
47301 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
47303 matching_parens parens;
47304 parens.require_open (parser);
47306 expr = cp_parser_expression (parser);
47307 expr = finish_parenthesized_expr (expr);
47309 parens.require_close (parser);
47311 else
47313 /* The only expression that is available got parsed for the noexcept
47314 already. noexcept is true then. */
47315 expr = noex;
47316 noex = boolean_true_node;
47319 expr = build_transaction_expr (token->location, expr, this_in, noex);
47320 parser->in_transaction = old_in;
47322 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
47323 return error_mark_node;
47325 return (flag_tm ? expr : error_mark_node);
47328 /* Parse a function-transaction-block.
47330 function-transaction-block:
47331 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
47332 function-body
47333 __transaction_atomic txn-attribute[opt] function-try-block
47334 __transaction_relaxed ctor-initializer[opt] function-body
47335 __transaction_relaxed function-try-block
47338 static void
47339 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
47341 unsigned char old_in = parser->in_transaction;
47342 unsigned char new_in = 1;
47343 tree compound_stmt, stmt, attrs;
47344 cp_token *token;
47346 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
47347 || keyword == RID_TRANSACTION_RELAXED);
47348 token = cp_parser_require_keyword (parser, keyword,
47349 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
47350 : RT_TRANSACTION_RELAXED));
47351 gcc_assert (token != NULL);
47353 if (keyword == RID_TRANSACTION_RELAXED)
47354 new_in |= TM_STMT_ATTR_RELAXED;
47355 else
47357 attrs = cp_parser_txn_attribute_opt (parser);
47358 if (attrs)
47359 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
47362 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
47364 parser->in_transaction = new_in;
47366 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
47367 cp_parser_function_try_block (parser);
47368 else
47369 cp_parser_ctor_initializer_opt_and_function_body
47370 (parser, /*in_function_try_block=*/false);
47372 parser->in_transaction = old_in;
47374 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
47377 /* Parse a __transaction_cancel statement.
47379 cancel-statement:
47380 __transaction_cancel txn-attribute[opt] ;
47381 __transaction_cancel txn-attribute[opt] throw-expression ;
47383 ??? Cancel and throw is not yet implemented. */
47385 static tree
47386 cp_parser_transaction_cancel (cp_parser *parser)
47388 cp_token *token;
47389 bool is_outer = false;
47390 tree stmt, attrs;
47392 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
47393 RT_TRANSACTION_CANCEL);
47394 gcc_assert (token != NULL);
47396 attrs = cp_parser_txn_attribute_opt (parser);
47397 if (attrs)
47398 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
47400 /* ??? Parse cancel-and-throw here. */
47402 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
47404 if (!flag_tm)
47406 error_at (token->location, "%<__transaction_cancel%> without "
47407 "transactional memory support enabled");
47408 return error_mark_node;
47410 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
47412 error_at (token->location, "%<__transaction_cancel%> within a "
47413 "%<__transaction_relaxed%>");
47414 return error_mark_node;
47416 else if (is_outer)
47418 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
47419 && !is_tm_may_cancel_outer (current_function_decl))
47421 error_at (token->location, "outer %<__transaction_cancel%> not "
47422 "within outer %<__transaction_atomic%>");
47423 error_at (token->location,
47424 " or a %<transaction_may_cancel_outer%> function");
47425 return error_mark_node;
47428 else if (parser->in_transaction == 0)
47430 error_at (token->location, "%<__transaction_cancel%> not within "
47431 "%<__transaction_atomic%>");
47432 return error_mark_node;
47435 stmt = build_tm_abort_call (token->location, is_outer);
47436 add_stmt (stmt);
47438 return stmt;
47441 /* The parser. */
47443 static GTY (()) cp_parser *the_parser;
47446 /* Special handling for the first token or line in the file. The first
47447 thing in the file might be #pragma GCC pch_preprocess, which loads a
47448 PCH file, which is a GC collection point. So we need to handle this
47449 first pragma without benefit of an existing lexer structure.
47451 Always returns one token to the caller in *FIRST_TOKEN. This is
47452 either the true first token of the file, or the first token after
47453 the initial pragma. */
47455 static void
47456 cp_parser_initial_pragma (cp_token *first_token)
47458 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
47459 return;
47461 cp_lexer_get_preprocessor_token (0, first_token);
47463 tree name = NULL;
47464 if (first_token->type == CPP_STRING)
47466 name = first_token->u.value;
47468 cp_lexer_get_preprocessor_token (0, first_token);
47471 /* Skip to the end of the pragma. */
47472 if (first_token->type != CPP_PRAGMA_EOL)
47474 error_at (first_token->location,
47475 "malformed %<#pragma GCC pch_preprocess%>");
47477 cp_lexer_get_preprocessor_token (0, first_token);
47478 while (first_token->type != CPP_PRAGMA_EOL);
47481 /* Now actually load the PCH file. */
47482 if (name)
47483 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
47485 /* Read one more token to return to our caller. We have to do this
47486 after reading the PCH file in, since its pointers have to be
47487 live. */
47488 cp_lexer_get_preprocessor_token (0, first_token);
47491 /* Parse a pragma GCC ivdep. */
47493 static bool
47494 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
47496 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47497 return true;
47500 /* Parse a pragma GCC unroll. */
47502 static unsigned short
47503 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
47505 location_t location = cp_lexer_peek_token (parser->lexer)->location;
47506 tree expr = cp_parser_constant_expression (parser);
47507 unsigned short unroll;
47508 expr = maybe_constant_value (expr);
47509 HOST_WIDE_INT lunroll = 0;
47510 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
47511 || TREE_CODE (expr) != INTEGER_CST
47512 || (lunroll = tree_to_shwi (expr)) < 0
47513 || lunroll >= USHRT_MAX)
47515 error_at (location, "%<#pragma GCC unroll%> requires an"
47516 " assignment-expression that evaluates to a non-negative"
47517 " integral constant less than %u", USHRT_MAX);
47518 unroll = 0;
47520 else
47522 unroll = (unsigned short)lunroll;
47523 if (unroll == 0)
47524 unroll = 1;
47526 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47527 return unroll;
47530 /* Normal parsing of a pragma token. Here we can (and must) use the
47531 regular lexer. */
47533 static bool
47534 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
47536 cp_token *pragma_tok;
47537 unsigned int id;
47538 tree stmt;
47539 bool ret = false;
47541 pragma_tok = cp_lexer_consume_token (parser->lexer);
47542 gcc_assert (pragma_tok->type == CPP_PRAGMA);
47543 parser->lexer->in_pragma = true;
47545 id = cp_parser_pragma_kind (pragma_tok);
47546 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
47547 cp_ensure_no_omp_declare_simd (parser);
47548 switch (id)
47550 case PRAGMA_GCC_PCH_PREPROCESS:
47551 error_at (pragma_tok->location,
47552 "%<#pragma GCC pch_preprocess%> must be first");
47553 break;
47555 case PRAGMA_OMP_BARRIER:
47556 switch (context)
47558 case pragma_compound:
47559 cp_parser_omp_barrier (parser, pragma_tok);
47560 return false;
47561 case pragma_stmt:
47562 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47563 "used in compound statements", "omp barrier");
47564 ret = true;
47565 break;
47566 default:
47567 goto bad_stmt;
47569 break;
47571 case PRAGMA_OMP_DEPOBJ:
47572 switch (context)
47574 case pragma_compound:
47575 cp_parser_omp_depobj (parser, pragma_tok);
47576 return false;
47577 case pragma_stmt:
47578 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47579 "used in compound statements", "omp depobj");
47580 ret = true;
47581 break;
47582 default:
47583 goto bad_stmt;
47585 break;
47587 case PRAGMA_OMP_FLUSH:
47588 switch (context)
47590 case pragma_compound:
47591 cp_parser_omp_flush (parser, pragma_tok);
47592 return false;
47593 case pragma_stmt:
47594 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47595 "used in compound statements", "omp flush");
47596 ret = true;
47597 break;
47598 default:
47599 goto bad_stmt;
47601 break;
47603 case PRAGMA_OMP_TASKWAIT:
47604 switch (context)
47606 case pragma_compound:
47607 cp_parser_omp_taskwait (parser, pragma_tok);
47608 return false;
47609 case pragma_stmt:
47610 error_at (pragma_tok->location,
47611 "%<#pragma %s%> may only be used in compound statements",
47612 "omp taskwait");
47613 ret = true;
47614 break;
47615 default:
47616 goto bad_stmt;
47618 break;
47620 case PRAGMA_OMP_TASKYIELD:
47621 switch (context)
47623 case pragma_compound:
47624 cp_parser_omp_taskyield (parser, pragma_tok);
47625 return false;
47626 case pragma_stmt:
47627 error_at (pragma_tok->location,
47628 "%<#pragma %s%> may only be used in compound statements",
47629 "omp taskyield");
47630 ret = true;
47631 break;
47632 default:
47633 goto bad_stmt;
47635 break;
47637 case PRAGMA_OMP_CANCEL:
47638 switch (context)
47640 case pragma_compound:
47641 cp_parser_omp_cancel (parser, pragma_tok);
47642 return false;
47643 case pragma_stmt:
47644 error_at (pragma_tok->location,
47645 "%<#pragma %s%> may only be used in compound statements",
47646 "omp cancel");
47647 ret = true;
47648 break;
47649 default:
47650 goto bad_stmt;
47652 break;
47654 case PRAGMA_OMP_CANCELLATION_POINT:
47655 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
47657 case PRAGMA_OMP_THREADPRIVATE:
47658 cp_parser_omp_threadprivate (parser, pragma_tok);
47659 return false;
47661 case PRAGMA_OMP_DECLARE:
47662 return cp_parser_omp_declare (parser, pragma_tok, context);
47664 case PRAGMA_OACC_DECLARE:
47665 cp_parser_oacc_declare (parser, pragma_tok);
47666 return false;
47668 case PRAGMA_OACC_ENTER_DATA:
47669 if (context == pragma_stmt)
47671 error_at (pragma_tok->location,
47672 "%<#pragma %s%> may only be used in compound statements",
47673 "acc enter data");
47674 ret = true;
47675 break;
47677 else if (context != pragma_compound)
47678 goto bad_stmt;
47679 cp_parser_omp_construct (parser, pragma_tok, if_p);
47680 return true;
47682 case PRAGMA_OACC_EXIT_DATA:
47683 if (context == pragma_stmt)
47685 error_at (pragma_tok->location,
47686 "%<#pragma %s%> may only be used in compound statements",
47687 "acc exit data");
47688 ret = true;
47689 break;
47691 else if (context != pragma_compound)
47692 goto bad_stmt;
47693 cp_parser_omp_construct (parser, pragma_tok, if_p);
47694 return true;
47696 case PRAGMA_OACC_ROUTINE:
47697 if (context != pragma_external)
47699 error_at (pragma_tok->location,
47700 "%<#pragma acc routine%> must be at file scope");
47701 ret = true;
47702 break;
47704 cp_parser_oacc_routine (parser, pragma_tok, context);
47705 return false;
47707 case PRAGMA_OACC_UPDATE:
47708 if (context == pragma_stmt)
47710 error_at (pragma_tok->location,
47711 "%<#pragma %s%> may only be used in compound statements",
47712 "acc update");
47713 ret = true;
47714 break;
47716 else if (context != pragma_compound)
47717 goto bad_stmt;
47718 cp_parser_omp_construct (parser, pragma_tok, if_p);
47719 return true;
47721 case PRAGMA_OACC_WAIT:
47722 if (context == pragma_stmt)
47724 error_at (pragma_tok->location,
47725 "%<#pragma %s%> may only be used in compound statements",
47726 "acc wait");
47727 ret = true;
47728 break;
47730 else if (context != pragma_compound)
47731 goto bad_stmt;
47732 cp_parser_omp_construct (parser, pragma_tok, if_p);
47733 return true;
47734 case PRAGMA_OMP_ALLOCATE:
47735 cp_parser_omp_allocate (parser, pragma_tok);
47736 return false;
47737 case PRAGMA_OACC_ATOMIC:
47738 case PRAGMA_OACC_CACHE:
47739 case PRAGMA_OACC_DATA:
47740 case PRAGMA_OACC_HOST_DATA:
47741 case PRAGMA_OACC_KERNELS:
47742 case PRAGMA_OACC_LOOP:
47743 case PRAGMA_OACC_PARALLEL:
47744 case PRAGMA_OACC_SERIAL:
47745 case PRAGMA_OMP_ATOMIC:
47746 case PRAGMA_OMP_CRITICAL:
47747 case PRAGMA_OMP_DISTRIBUTE:
47748 case PRAGMA_OMP_FOR:
47749 case PRAGMA_OMP_LOOP:
47750 case PRAGMA_OMP_MASKED:
47751 case PRAGMA_OMP_MASTER:
47752 case PRAGMA_OMP_PARALLEL:
47753 case PRAGMA_OMP_SCOPE:
47754 case PRAGMA_OMP_SECTIONS:
47755 case PRAGMA_OMP_SIMD:
47756 case PRAGMA_OMP_SINGLE:
47757 case PRAGMA_OMP_TASK:
47758 case PRAGMA_OMP_TASKGROUP:
47759 case PRAGMA_OMP_TASKLOOP:
47760 case PRAGMA_OMP_TEAMS:
47761 if (context != pragma_stmt && context != pragma_compound)
47762 goto bad_stmt;
47763 stmt = push_omp_privatization_clauses (false);
47764 cp_parser_omp_construct (parser, pragma_tok, if_p);
47765 pop_omp_privatization_clauses (stmt);
47766 return true;
47768 case PRAGMA_OMP_REQUIRES:
47769 if (context != pragma_external)
47771 error_at (pragma_tok->location,
47772 "%<#pragma omp requires%> may only be used at file or "
47773 "namespace scope");
47774 ret = true;
47775 break;
47777 return cp_parser_omp_requires (parser, pragma_tok);
47779 case PRAGMA_OMP_NOTHING:
47780 cp_parser_omp_nothing (parser, pragma_tok);
47781 return false;
47783 case PRAGMA_OMP_ERROR:
47784 return cp_parser_omp_error (parser, pragma_tok, context);
47786 case PRAGMA_OMP_ORDERED:
47787 if (context != pragma_stmt && context != pragma_compound)
47788 goto bad_stmt;
47789 stmt = push_omp_privatization_clauses (false);
47790 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
47791 pop_omp_privatization_clauses (stmt);
47792 return ret;
47794 case PRAGMA_OMP_TARGET:
47795 if (context != pragma_stmt && context != pragma_compound)
47796 goto bad_stmt;
47797 stmt = push_omp_privatization_clauses (false);
47798 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
47799 pop_omp_privatization_clauses (stmt);
47800 return ret;
47802 case PRAGMA_OMP_END_DECLARE_TARGET:
47803 cp_parser_omp_end_declare_target (parser, pragma_tok);
47804 return false;
47806 case PRAGMA_OMP_SCAN:
47807 error_at (pragma_tok->location,
47808 "%<#pragma omp scan%> may only be used in "
47809 "a loop construct with %<inscan%> %<reduction%> clause");
47810 break;
47812 case PRAGMA_OMP_SECTION:
47813 error_at (pragma_tok->location,
47814 "%<#pragma omp section%> may only be used in "
47815 "%<#pragma omp sections%> construct");
47816 break;
47818 case PRAGMA_IVDEP:
47820 if (context == pragma_external)
47822 error_at (pragma_tok->location,
47823 "%<#pragma GCC ivdep%> must be inside a function");
47824 break;
47826 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
47827 unsigned short unroll;
47828 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
47829 if (tok->type == CPP_PRAGMA
47830 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
47832 tok = cp_lexer_consume_token (parser->lexer);
47833 unroll = cp_parser_pragma_unroll (parser, tok);
47834 tok = cp_lexer_peek_token (the_parser->lexer);
47836 else
47837 unroll = 0;
47838 if (tok->type != CPP_KEYWORD
47839 || (tok->keyword != RID_FOR
47840 && tok->keyword != RID_WHILE
47841 && tok->keyword != RID_DO))
47843 cp_parser_error (parser, "for, while or do statement expected");
47844 return false;
47846 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
47847 return true;
47850 case PRAGMA_UNROLL:
47852 if (context == pragma_external)
47854 error_at (pragma_tok->location,
47855 "%<#pragma GCC unroll%> must be inside a function");
47856 break;
47858 const unsigned short unroll
47859 = cp_parser_pragma_unroll (parser, pragma_tok);
47860 bool ivdep;
47861 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
47862 if (tok->type == CPP_PRAGMA
47863 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
47865 tok = cp_lexer_consume_token (parser->lexer);
47866 ivdep = cp_parser_pragma_ivdep (parser, tok);
47867 tok = cp_lexer_peek_token (the_parser->lexer);
47869 else
47870 ivdep = false;
47871 if (tok->type != CPP_KEYWORD
47872 || (tok->keyword != RID_FOR
47873 && tok->keyword != RID_WHILE
47874 && tok->keyword != RID_DO))
47876 cp_parser_error (parser, "for, while or do statement expected");
47877 return false;
47879 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
47880 return true;
47883 default:
47884 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
47885 c_invoke_pragma_handler (id);
47886 break;
47888 bad_stmt:
47889 cp_parser_error (parser, "expected declaration specifiers");
47890 break;
47893 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47894 return ret;
47897 /* The interface the pragma parsers have to the lexer. */
47899 enum cpp_ttype
47900 pragma_lex (tree *value, location_t *loc)
47902 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
47903 enum cpp_ttype ret = tok->type;
47905 *value = tok->u.value;
47906 if (loc)
47907 *loc = tok->location;
47909 if (ret == CPP_PRAGMA_EOL)
47910 ret = CPP_EOF;
47911 else if (ret == CPP_STRING)
47912 *value = cp_parser_string_literal (the_parser, false, false);
47913 else
47915 if (ret == CPP_KEYWORD)
47916 ret = CPP_NAME;
47917 cp_lexer_consume_token (the_parser->lexer);
47920 return ret;
47924 /* External interface. */
47926 /* Parse one entire translation unit. */
47928 void
47929 c_parse_file (void)
47931 static bool already_called = false;
47933 if (already_called)
47934 fatal_error (input_location,
47935 "multi-source compilation not implemented for C++");
47936 already_called = true;
47938 /* cp_lexer_new_main is called before doing any GC allocation
47939 because tokenization might load a PCH file. */
47940 cp_lexer *lexer = cp_lexer_new_main ();
47942 the_parser = cp_parser_new (lexer);
47944 cp_parser_translation_unit (the_parser);
47945 class_decl_loc_t::diag_mismatched_tags ();
47947 the_parser = NULL;
47949 finish_translation_unit ();
47952 /* Create an identifier for a generic parameter type (a synthesized
47953 template parameter implied by `auto' or a concept identifier). */
47955 static GTY(()) int generic_parm_count;
47956 static tree
47957 make_generic_type_name ()
47959 char buf[32];
47960 sprintf (buf, "auto:%d", ++generic_parm_count);
47961 return get_identifier (buf);
47964 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
47965 (creating a new template parameter list if necessary). Returns the newly
47966 created template type parm. */
47968 static tree
47969 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
47971 /* A requires-clause is not a function and cannot have placeholders. */
47972 if (current_binding_level->kind == sk_block)
47974 error ("placeholder type not allowed in this context");
47975 return error_mark_node;
47978 gcc_assert (current_binding_level->kind == sk_function_parms);
47980 /* We are either continuing a function template that already contains implicit
47981 template parameters, creating a new fully-implicit function template, or
47982 extending an existing explicit function template with implicit template
47983 parameters. */
47985 cp_binding_level *const entry_scope = current_binding_level;
47987 bool become_template = false;
47988 cp_binding_level *parent_scope = 0;
47990 if (parser->implicit_template_scope)
47992 gcc_assert (parser->implicit_template_parms);
47994 current_binding_level = parser->implicit_template_scope;
47996 else
47998 /* Roll back to the existing template parameter scope (in the case of
47999 extending an explicit function template) or introduce a new template
48000 parameter scope ahead of the function parameter scope (or class scope
48001 in the case of out-of-line member definitions). The function scope is
48002 added back after template parameter synthesis below. */
48004 cp_binding_level *scope = entry_scope;
48006 while (scope->kind == sk_function_parms)
48008 parent_scope = scope;
48009 scope = scope->level_chain;
48011 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
48013 /* If not defining a class, then any class scope is a scope level in
48014 an out-of-line member definition. In this case simply wind back
48015 beyond the first such scope to inject the template parameter list.
48016 Otherwise wind back to the class being defined. The latter can
48017 occur in class member friend declarations such as:
48019 class A {
48020 void foo (auto);
48022 class B {
48023 friend void A::foo (auto);
48026 The template parameter list synthesized for the friend declaration
48027 must be injected in the scope of 'B'. This can also occur in
48028 erroneous cases such as:
48030 struct A {
48031 struct B {
48032 void foo (auto);
48034 void B::foo (auto) {}
48037 Here the attempted definition of 'B::foo' within 'A' is ill-formed
48038 but, nevertheless, the template parameter list synthesized for the
48039 declarator should be injected into the scope of 'A' as if the
48040 ill-formed template was specified explicitly. */
48042 while (scope->kind == sk_class && !scope->defining_class_p)
48044 parent_scope = scope;
48045 scope = scope->level_chain;
48049 current_binding_level = scope;
48051 if (scope->kind != sk_template_parms
48052 || !function_being_declared_is_template_p (parser))
48054 /* Introduce a new template parameter list for implicit template
48055 parameters. */
48057 become_template = true;
48059 parser->implicit_template_scope
48060 = begin_scope (sk_template_parms, NULL);
48062 ++processing_template_decl;
48064 parser->fully_implicit_function_template_p = true;
48065 ++parser->num_template_parameter_lists;
48067 else
48069 /* Synthesize implicit template parameters at the end of the explicit
48070 template parameter list. */
48072 gcc_assert (current_template_parms);
48074 parser->implicit_template_scope = scope;
48076 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48077 parser->implicit_template_parms
48078 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
48082 /* Synthesize a new template parameter and track the current template
48083 parameter chain with implicit_template_parms. */
48085 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
48086 tree synth_id = make_generic_type_name ();
48087 tree synth_tmpl_parm;
48088 bool non_type = false;
48090 /* Synthesize the type template parameter. */
48091 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
48092 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
48094 if (become_template)
48095 current_template_parms = tree_cons (size_int (current_template_depth + 1),
48096 NULL_TREE, current_template_parms);
48098 /* Attach the constraint to the parm before processing. */
48099 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
48100 TREE_TYPE (node) = constr;
48101 tree new_parm
48102 = process_template_parm (parser->implicit_template_parms,
48103 input_location,
48104 node,
48105 /*non_type=*/non_type,
48106 /*param_pack=*/false);
48108 /* Mark the synthetic declaration "virtual". This is used when
48109 comparing template-heads to determine if whether an abbreviated
48110 function template is equivalent to an explicit template.
48112 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
48113 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
48115 // Chain the new parameter to the list of implicit parameters.
48116 if (parser->implicit_template_parms)
48117 parser->implicit_template_parms
48118 = TREE_CHAIN (parser->implicit_template_parms);
48119 else
48120 parser->implicit_template_parms = new_parm;
48122 tree new_decl = get_local_decls ();
48123 if (non_type)
48124 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
48125 new_decl = DECL_INITIAL (new_decl);
48127 /* If creating a fully implicit function template, start the new implicit
48128 template parameter list with this synthesized type, otherwise grow the
48129 current template parameter list. */
48131 if (become_template)
48133 parent_scope->level_chain = current_binding_level;
48135 tree new_parms = make_tree_vec (1);
48136 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
48137 TREE_VALUE (current_template_parms) = new_parms;
48139 else
48141 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48142 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
48143 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
48144 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
48147 /* If the new parameter was constrained, we need to add that to the
48148 constraints in the template parameter list. */
48149 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
48151 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
48152 reqs = combine_constraint_expressions (reqs, req);
48153 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
48156 current_binding_level = entry_scope;
48158 return new_decl;
48161 /* Finish the declaration of a fully implicit function template. Such a
48162 template has no explicit template parameter list so has not been through the
48163 normal template head and tail processing. synthesize_implicit_template_parm
48164 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
48165 provided if the declaration is a class member such that its template
48166 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
48167 form is returned. Otherwise NULL_TREE is returned. */
48169 static tree
48170 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
48172 gcc_assert (parser->fully_implicit_function_template_p);
48174 if (member_decl_opt && member_decl_opt != error_mark_node
48175 && DECL_VIRTUAL_P (member_decl_opt))
48177 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
48178 "implicit templates may not be %<virtual%>");
48179 DECL_VIRTUAL_P (member_decl_opt) = false;
48182 if (member_decl_opt)
48183 member_decl_opt = finish_member_template_decl (member_decl_opt);
48184 end_template_decl ();
48186 parser->fully_implicit_function_template_p = false;
48187 parser->implicit_template_parms = 0;
48188 parser->implicit_template_scope = 0;
48189 --parser->num_template_parameter_lists;
48191 return member_decl_opt;
48194 /* Like finish_fully_implicit_template, but to be used in error
48195 recovery, rearranging scopes so that we restore the state we had
48196 before synthesize_implicit_template_parm inserted the implement
48197 template parms scope. */
48199 static void
48200 abort_fully_implicit_template (cp_parser *parser)
48202 cp_binding_level *return_to_scope = current_binding_level;
48204 if (parser->implicit_template_scope
48205 && return_to_scope != parser->implicit_template_scope)
48207 cp_binding_level *child = return_to_scope;
48208 for (cp_binding_level *scope = child->level_chain;
48209 scope != parser->implicit_template_scope;
48210 scope = child->level_chain)
48211 child = scope;
48212 child->level_chain = parser->implicit_template_scope->level_chain;
48213 parser->implicit_template_scope->level_chain = return_to_scope;
48214 current_binding_level = parser->implicit_template_scope;
48216 else
48217 return_to_scope = return_to_scope->level_chain;
48219 finish_fully_implicit_template (parser, NULL);
48221 gcc_assert (current_binding_level == return_to_scope);
48224 /* Helper function for diagnostics that have complained about things
48225 being used with 'extern "C"' linkage.
48227 Attempt to issue a note showing where the 'extern "C"' linkage began. */
48229 void
48230 maybe_show_extern_c_location (void)
48232 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
48233 inform (the_parser->innermost_linkage_specification_location,
48234 "%<extern \"C\"%> linkage started here");
48237 #include "gt-cp-parser.h"