c++: Fix ICE with -Wmismatched-tags [PR105725]
[official-gcc.git] / gcc / cp / parser.cc
blob9a9f859974a0c6bfcd478b370ad106778df8e2d4
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2022 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.cc) 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 /* Determines what saved_token_sentinel does when going out of scope. */
1280 enum saved_token_sentinel_mode {
1281 STS_COMMIT,
1282 STS_ROLLBACK,
1283 STS_DONOTHING
1286 /* RAII wrapper around the above functions, with sanity checking (the token
1287 stream should be the same at the point of instantiation as it is at the
1288 point of destruction).
1290 Creating a variable saves tokens. MODE determines what happens when the
1291 object is destroyed. STS_COMMIT commits tokens (default),
1292 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1293 rollback() will immediately roll-back tokens and set MODE to
1294 STS_DONOTHING. */
1296 struct saved_token_sentinel
1298 cp_lexer *lexer;
1299 unsigned len;
1300 saved_token_sentinel_mode mode;
1301 saved_token_sentinel (cp_lexer *_lexer,
1302 saved_token_sentinel_mode _mode = STS_COMMIT)
1303 : lexer (_lexer), mode (_mode)
1305 len = lexer->saved_tokens.length ();
1306 cp_lexer_save_tokens (lexer);
1308 void rollback ()
1310 cp_lexer_rollback_tokens (lexer);
1311 cp_lexer_set_source_position_from_token
1312 (cp_lexer_previous_token (lexer));
1313 mode = STS_DONOTHING;
1315 ~saved_token_sentinel ()
1317 if (mode == STS_COMMIT)
1318 cp_lexer_commit_tokens (lexer);
1319 else if (mode == STS_ROLLBACK)
1320 rollback ();
1322 gcc_assert (lexer->saved_tokens.length () == len);
1326 /* Print a representation of the TOKEN on the STREAM. */
1328 static void
1329 cp_lexer_print_token (FILE * stream, cp_token *token)
1331 /* We don't use cpp_type2name here because the parser defines
1332 a few tokens of its own. */
1333 static const char *const token_names[] = {
1334 /* cpplib-defined token types */
1335 #define OP(e, s) #e,
1336 #define TK(e, s) #e,
1337 TTYPE_TABLE
1338 #undef OP
1339 #undef TK
1340 /* C++ parser token types - see "Manifest constants", above. */
1341 "KEYWORD",
1342 "TEMPLATE_ID",
1343 "NESTED_NAME_SPECIFIER",
1346 /* For some tokens, print the associated data. */
1347 switch (token->type)
1349 case CPP_KEYWORD:
1350 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1351 For example, `struct' is mapped to an INTEGER_CST. */
1352 if (!identifier_p (token->u.value))
1353 break;
1354 /* fall through */
1355 case CPP_NAME:
1356 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1357 break;
1359 case CPP_STRING:
1360 case CPP_STRING16:
1361 case CPP_STRING32:
1362 case CPP_WSTRING:
1363 case CPP_UTF8STRING:
1364 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1365 break;
1367 case CPP_NUMBER:
1368 print_generic_expr (stream, token->u.value);
1369 break;
1371 default:
1372 /* If we have a name for the token, print it out. Otherwise, we
1373 simply give the numeric code. */
1374 if (token->type < ARRAY_SIZE(token_names))
1375 fputs (token_names[token->type], stream);
1376 else
1377 fprintf (stream, "[%d]", token->type);
1378 break;
1382 DEBUG_FUNCTION void
1383 debug (cp_token &ref)
1385 cp_lexer_print_token (stderr, &ref);
1386 fprintf (stderr, "\n");
1389 DEBUG_FUNCTION void
1390 debug (cp_token *ptr)
1392 if (ptr)
1393 debug (*ptr);
1394 else
1395 fprintf (stderr, "<nil>\n");
1399 /* Start emitting debugging information. */
1401 static void
1402 cp_lexer_start_debugging (cp_lexer* lexer)
1404 if (!LEXER_DEBUGGING_ENABLED_P)
1405 fatal_error (input_location,
1406 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1408 lexer->debugging_p = true;
1409 cp_lexer_debug_stream = stderr;
1412 /* Stop emitting debugging information. */
1414 static void
1415 cp_lexer_stop_debugging (cp_lexer* lexer)
1417 if (!LEXER_DEBUGGING_ENABLED_P)
1418 fatal_error (input_location,
1419 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1421 lexer->debugging_p = false;
1422 cp_lexer_debug_stream = NULL;
1425 /* Create a new cp_token_cache, representing a range of tokens. */
1427 static cp_token_cache *
1428 cp_token_cache_new (cp_token *first, cp_token *last)
1430 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1431 cache->first = first;
1432 cache->last = last;
1433 return cache;
1436 /* Diagnose if #pragma omp declare simd isn't followed immediately
1437 by function declaration or definition. */
1439 static inline void
1440 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1442 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1444 error ("%<#pragma omp declare %s%> not immediately followed by "
1445 "function declaration or definition",
1446 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1447 parser->omp_declare_simd = NULL;
1451 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1452 and put that into "omp declare simd" attribute. */
1454 static inline void
1455 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1457 if (UNLIKELY (parser->omp_declare_simd != NULL))
1459 if (fndecl == error_mark_node)
1461 parser->omp_declare_simd = NULL;
1462 return;
1464 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1466 cp_ensure_no_omp_declare_simd (parser);
1467 return;
1472 /* Similarly, but for use in declaration parsing functions
1473 which call cp_parser_handle_directive_omp_attributes. */
1475 static inline void
1476 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1478 if (parser->omp_declare_simd != data)
1479 return;
1481 if (!parser->omp_declare_simd->error_seen
1482 && !parser->omp_declare_simd->fndecl_seen)
1483 error_at (parser->omp_declare_simd->loc,
1484 "%<declare %s%> directive not immediately followed by "
1485 "function declaration or definition",
1486 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1487 parser->omp_declare_simd = NULL;
1490 /* Diagnose if #pragma acc routine isn't followed immediately by function
1491 declaration or definition. */
1493 static inline void
1494 cp_ensure_no_oacc_routine (cp_parser *parser)
1496 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1498 error_at (parser->oacc_routine->loc,
1499 "%<#pragma acc routine%> not immediately followed by "
1500 "function declaration or definition");
1501 parser->oacc_routine = NULL;
1505 /* Decl-specifiers. */
1507 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1509 static void
1510 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1512 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1515 /* Declarators. */
1517 /* Nothing other than the parser should be creating declarators;
1518 declarators are a semi-syntactic representation of C++ entities.
1519 Other parts of the front end that need to create entities (like
1520 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1522 static cp_declarator *make_call_declarator
1523 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1524 tree, tree, tree, tree, location_t);
1525 static cp_declarator *make_array_declarator
1526 (cp_declarator *, tree);
1527 static cp_declarator *make_pointer_declarator
1528 (cp_cv_quals, cp_declarator *, tree);
1529 static cp_declarator *make_reference_declarator
1530 (cp_cv_quals, cp_declarator *, bool, tree);
1531 static cp_declarator *make_ptrmem_declarator
1532 (cp_cv_quals, tree, cp_declarator *, tree);
1534 /* An erroneous declarator. */
1535 static cp_declarator *cp_error_declarator;
1537 /* The obstack on which declarators and related data structures are
1538 allocated. */
1539 static struct obstack declarator_obstack;
1541 /* Alloc BYTES from the declarator memory pool. */
1543 static inline void *
1544 alloc_declarator (size_t bytes)
1546 return obstack_alloc (&declarator_obstack, bytes);
1549 /* Allocate a declarator of the indicated KIND. Clear fields that are
1550 common to all declarators. */
1552 static cp_declarator *
1553 make_declarator (cp_declarator_kind kind)
1555 cp_declarator *declarator;
1557 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1558 declarator->kind = kind;
1559 declarator->parenthesized = UNKNOWN_LOCATION;
1560 declarator->attributes = NULL_TREE;
1561 declarator->std_attributes = NULL_TREE;
1562 declarator->declarator = NULL;
1563 declarator->parameter_pack_p = false;
1564 declarator->id_loc = UNKNOWN_LOCATION;
1565 declarator->init_loc = UNKNOWN_LOCATION;
1567 return declarator;
1570 /* Make a declarator for a generalized identifier. If
1571 QUALIFYING_SCOPE is non-NULL, the identifier is
1572 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1573 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1574 is, if any. */
1576 static cp_declarator *
1577 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1578 special_function_kind sfk, location_t id_location)
1580 cp_declarator *declarator;
1582 /* It is valid to write:
1584 class C { void f(); };
1585 typedef C D;
1586 void D::f();
1588 The standard is not clear about whether `typedef const C D' is
1589 legal; as of 2002-09-15 the committee is considering that
1590 question. EDG 3.0 allows that syntax. Therefore, we do as
1591 well. */
1592 if (qualifying_scope && TYPE_P (qualifying_scope))
1593 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1595 gcc_assert (identifier_p (unqualified_name)
1596 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1597 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1599 declarator = make_declarator (cdk_id);
1600 declarator->u.id.qualifying_scope = qualifying_scope;
1601 declarator->u.id.unqualified_name = unqualified_name;
1602 declarator->u.id.sfk = sfk;
1603 declarator->id_loc = id_location;
1605 return declarator;
1608 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1609 of modifiers such as const or volatile to apply to the pointer
1610 type, represented as identifiers. ATTRIBUTES represent the attributes that
1611 appertain to the pointer or reference. */
1613 cp_declarator *
1614 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1615 tree attributes)
1617 cp_declarator *declarator;
1619 declarator = make_declarator (cdk_pointer);
1620 declarator->declarator = target;
1621 declarator->u.pointer.qualifiers = cv_qualifiers;
1622 declarator->u.pointer.class_type = NULL_TREE;
1623 if (target)
1625 declarator->id_loc = target->id_loc;
1626 declarator->parameter_pack_p = target->parameter_pack_p;
1627 target->parameter_pack_p = false;
1629 else
1630 declarator->parameter_pack_p = false;
1632 declarator->std_attributes = attributes;
1634 return declarator;
1637 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1638 represent the attributes that appertain to the pointer or
1639 reference. */
1641 cp_declarator *
1642 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1643 bool rvalue_ref, tree attributes)
1645 cp_declarator *declarator;
1647 declarator = make_declarator (cdk_reference);
1648 declarator->declarator = target;
1649 declarator->u.reference.qualifiers = cv_qualifiers;
1650 declarator->u.reference.rvalue_ref = rvalue_ref;
1651 if (target)
1653 declarator->id_loc = target->id_loc;
1654 declarator->parameter_pack_p = target->parameter_pack_p;
1655 target->parameter_pack_p = false;
1657 else
1658 declarator->parameter_pack_p = false;
1660 declarator->std_attributes = attributes;
1662 return declarator;
1665 /* Like make_pointer_declarator -- but for a pointer to a non-static
1666 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1667 appertain to the pointer or reference. */
1669 cp_declarator *
1670 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1671 cp_declarator *pointee,
1672 tree attributes)
1674 cp_declarator *declarator;
1676 declarator = make_declarator (cdk_ptrmem);
1677 declarator->declarator = pointee;
1678 declarator->u.pointer.qualifiers = cv_qualifiers;
1679 declarator->u.pointer.class_type = class_type;
1681 if (pointee)
1683 declarator->parameter_pack_p = pointee->parameter_pack_p;
1684 pointee->parameter_pack_p = false;
1686 else
1687 declarator->parameter_pack_p = false;
1689 declarator->std_attributes = attributes;
1691 return declarator;
1694 /* Make a declarator for the function given by TARGET, with the
1695 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1696 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1697 indicates what exceptions can be thrown. */
1699 cp_declarator *
1700 make_call_declarator (cp_declarator *target,
1701 tree parms,
1702 cp_cv_quals cv_qualifiers,
1703 cp_virt_specifiers virt_specifiers,
1704 cp_ref_qualifier ref_qualifier,
1705 tree tx_qualifier,
1706 tree exception_specification,
1707 tree late_return_type,
1708 tree requires_clause,
1709 location_t parens_loc)
1711 cp_declarator *declarator;
1713 declarator = make_declarator (cdk_function);
1714 declarator->declarator = target;
1715 declarator->u.function.parameters = parms;
1716 declarator->u.function.qualifiers = cv_qualifiers;
1717 declarator->u.function.virt_specifiers = virt_specifiers;
1718 declarator->u.function.ref_qualifier = ref_qualifier;
1719 declarator->u.function.tx_qualifier = tx_qualifier;
1720 declarator->u.function.exception_specification = exception_specification;
1721 declarator->u.function.late_return_type = late_return_type;
1722 declarator->u.function.requires_clause = requires_clause;
1723 declarator->u.function.parens_loc = parens_loc;
1724 if (target)
1726 declarator->id_loc = target->id_loc;
1727 declarator->parameter_pack_p = target->parameter_pack_p;
1728 target->parameter_pack_p = false;
1730 else
1731 declarator->parameter_pack_p = false;
1733 return declarator;
1736 /* Make a declarator for an array of BOUNDS elements, each of which is
1737 defined by ELEMENT. */
1739 cp_declarator *
1740 make_array_declarator (cp_declarator *element, tree bounds)
1742 cp_declarator *declarator;
1744 declarator = make_declarator (cdk_array);
1745 declarator->declarator = element;
1746 declarator->u.array.bounds = bounds;
1747 if (element)
1749 declarator->id_loc = element->id_loc;
1750 declarator->parameter_pack_p = element->parameter_pack_p;
1751 element->parameter_pack_p = false;
1753 else
1754 declarator->parameter_pack_p = false;
1756 return declarator;
1759 /* Determine whether the declarator we've seen so far can be a
1760 parameter pack, when followed by an ellipsis. */
1761 static bool
1762 declarator_can_be_parameter_pack (cp_declarator *declarator)
1764 if (declarator && declarator->parameter_pack_p)
1765 /* We already saw an ellipsis. */
1766 return false;
1768 /* Search for a declarator name, or any other declarator that goes
1769 after the point where the ellipsis could appear in a parameter
1770 pack. If we find any of these, then this declarator cannot be
1771 made into a parameter pack. */
1772 bool found = false;
1773 while (declarator && !found)
1775 switch ((int)declarator->kind)
1777 case cdk_id:
1778 case cdk_array:
1779 case cdk_decomp:
1780 found = true;
1781 break;
1783 case cdk_error:
1784 return true;
1786 default:
1787 declarator = declarator->declarator;
1788 break;
1792 return !found;
1795 cp_parameter_declarator *no_parameters;
1797 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1798 DECLARATOR and DEFAULT_ARGUMENT. */
1800 cp_parameter_declarator *
1801 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1802 cp_declarator *declarator,
1803 tree default_argument,
1804 location_t loc,
1805 bool template_parameter_pack_p = false)
1807 cp_parameter_declarator *parameter;
1809 parameter = ((cp_parameter_declarator *)
1810 alloc_declarator (sizeof (cp_parameter_declarator)));
1811 parameter->next = NULL;
1812 if (decl_specifiers)
1813 parameter->decl_specifiers = *decl_specifiers;
1814 else
1815 clear_decl_specs (&parameter->decl_specifiers);
1816 parameter->declarator = declarator;
1817 parameter->default_argument = default_argument;
1818 parameter->template_parameter_pack_p = template_parameter_pack_p;
1819 parameter->loc = loc;
1821 return parameter;
1824 /* Returns true iff DECLARATOR is a declaration for a function. */
1826 static bool
1827 function_declarator_p (const cp_declarator *declarator)
1829 while (declarator)
1831 if (declarator->kind == cdk_function
1832 && declarator->declarator->kind == cdk_id)
1833 return true;
1834 if (declarator->kind == cdk_id
1835 || declarator->kind == cdk_decomp
1836 || declarator->kind == cdk_error)
1837 return false;
1838 declarator = declarator->declarator;
1840 return false;
1843 /* The parser. */
1845 /* Overview
1846 --------
1848 A cp_parser parses the token stream as specified by the C++
1849 grammar. Its job is purely parsing, not semantic analysis. For
1850 example, the parser breaks the token stream into declarators,
1851 expressions, statements, and other similar syntactic constructs.
1852 It does not check that the types of the expressions on either side
1853 of an assignment-statement are compatible, or that a function is
1854 not declared with a parameter of type `void'.
1856 The parser invokes routines elsewhere in the compiler to perform
1857 semantic analysis and to build up the abstract syntax tree for the
1858 code processed.
1860 The parser (and the template instantiation code, which is, in a
1861 way, a close relative of parsing) are the only parts of the
1862 compiler that should be calling push_scope and pop_scope, or
1863 related functions. The parser (and template instantiation code)
1864 keeps track of what scope is presently active; everything else
1865 should simply honor that. (The code that generates static
1866 initializers may also need to set the scope, in order to check
1867 access control correctly when emitting the initializers.)
1869 Methodology
1870 -----------
1872 The parser is of the standard recursive-descent variety. Upcoming
1873 tokens in the token stream are examined in order to determine which
1874 production to use when parsing a non-terminal. Some C++ constructs
1875 require arbitrary look ahead to disambiguate. For example, it is
1876 impossible, in the general case, to tell whether a statement is an
1877 expression or declaration without scanning the entire statement.
1878 Therefore, the parser is capable of "parsing tentatively." When the
1879 parser is not sure what construct comes next, it enters this mode.
1880 Then, while we attempt to parse the construct, the parser queues up
1881 error messages, rather than issuing them immediately, and saves the
1882 tokens it consumes. If the construct is parsed successfully, the
1883 parser "commits", i.e., it issues any queued error messages and
1884 the tokens that were being preserved are permanently discarded.
1885 If, however, the construct is not parsed successfully, the parser
1886 rolls back its state completely so that it can resume parsing using
1887 a different alternative.
1889 Future Improvements
1890 -------------------
1892 The performance of the parser could probably be improved substantially.
1893 We could often eliminate the need to parse tentatively by looking ahead
1894 a little bit. In some places, this approach might not entirely eliminate
1895 the need to parse tentatively, but it might still speed up the average
1896 case. */
1898 /* Flags that are passed to some parsing functions. These values can
1899 be bitwise-ored together. */
1901 enum
1903 /* No flags. */
1904 CP_PARSER_FLAGS_NONE = 0x0,
1905 /* The construct is optional. If it is not present, then no error
1906 should be issued. */
1907 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1908 /* When parsing a type-specifier, treat user-defined type-names
1909 as non-type identifiers. */
1910 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1911 /* When parsing a type-specifier, do not try to parse a class-specifier
1912 or enum-specifier. */
1913 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1914 /* When parsing a decl-specifier-seq, only allow type-specifier or
1915 constexpr. */
1916 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1917 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1918 for C++20 consteval. */
1919 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1920 /* When parsing a decl-specifier-seq, allow missing typename. */
1921 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1922 /* When parsing of the noexcept-specifier should be delayed. */
1923 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1924 /* When parsing a consteval declarator. */
1925 CP_PARSER_FLAGS_CONSTEVAL = 0x80
1928 /* This type is used for parameters and variables which hold
1929 combinations of the above flags. */
1930 typedef int cp_parser_flags;
1932 /* The different kinds of declarators we want to parse. */
1934 enum cp_parser_declarator_kind
1936 /* We want an abstract declarator. */
1937 CP_PARSER_DECLARATOR_ABSTRACT,
1938 /* We want a named declarator. */
1939 CP_PARSER_DECLARATOR_NAMED,
1940 /* We don't mind, but the name must be an unqualified-id. */
1941 CP_PARSER_DECLARATOR_EITHER
1944 /* The precedence values used to parse binary expressions. The minimum value
1945 of PREC must be 1, because zero is reserved to quickly discriminate
1946 binary operators from other tokens. */
1948 enum cp_parser_prec
1950 PREC_NOT_OPERATOR,
1951 PREC_LOGICAL_OR_EXPRESSION,
1952 PREC_LOGICAL_AND_EXPRESSION,
1953 PREC_INCLUSIVE_OR_EXPRESSION,
1954 PREC_EXCLUSIVE_OR_EXPRESSION,
1955 PREC_AND_EXPRESSION,
1956 PREC_EQUALITY_EXPRESSION,
1957 PREC_RELATIONAL_EXPRESSION,
1958 PREC_SPACESHIP_EXPRESSION,
1959 PREC_SHIFT_EXPRESSION,
1960 PREC_ADDITIVE_EXPRESSION,
1961 PREC_MULTIPLICATIVE_EXPRESSION,
1962 PREC_PM_EXPRESSION,
1963 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1966 /* A mapping from a token type to a corresponding tree node type, with a
1967 precedence value. */
1969 struct cp_parser_binary_operations_map_node
1971 /* The token type. */
1972 enum cpp_ttype token_type;
1973 /* The corresponding tree code. */
1974 enum tree_code tree_type;
1975 /* The precedence of this operator. */
1976 enum cp_parser_prec prec;
1979 struct cp_parser_expression_stack_entry
1981 /* Left hand side of the binary operation we are currently
1982 parsing. */
1983 cp_expr lhs;
1984 /* Original tree code for left hand side, if it was a binary
1985 expression itself (used for -Wparentheses). */
1986 enum tree_code lhs_type;
1987 /* Tree code for the binary operation we are parsing. */
1988 enum tree_code tree_type;
1989 /* Precedence of the binary operation we are parsing. */
1990 enum cp_parser_prec prec;
1991 /* Location of the binary operation we are parsing. */
1992 location_t loc;
1995 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1996 entries because precedence levels on the stack are monotonically
1997 increasing. */
1998 typedef struct cp_parser_expression_stack_entry
1999 cp_parser_expression_stack[NUM_PREC_VALUES];
2001 /* Prototypes. */
2003 /* Constructors and destructors. */
2005 static cp_parser_context *cp_parser_context_new
2006 (cp_parser_context *);
2008 /* Class variables. */
2010 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2012 /* The operator-precedence table used by cp_parser_binary_expression.
2013 Transformed into an associative array (binops_by_token) by
2014 cp_parser_new. */
2016 static const cp_parser_binary_operations_map_node binops[] = {
2017 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2018 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2020 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2021 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2022 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2024 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2025 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2027 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2028 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2030 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2032 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2033 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2034 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2035 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2037 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2038 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2040 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2042 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2044 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2046 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2048 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2051 /* The same as binops, but initialized by cp_parser_new so that
2052 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2053 for speed. */
2054 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2056 /* Constructors and destructors. */
2058 /* Construct a new context. The context below this one on the stack
2059 is given by NEXT. */
2061 static cp_parser_context *
2062 cp_parser_context_new (cp_parser_context* next)
2064 cp_parser_context *context;
2066 /* Allocate the storage. */
2067 if (cp_parser_context_free_list != NULL)
2069 /* Pull the first entry from the free list. */
2070 context = cp_parser_context_free_list;
2071 cp_parser_context_free_list = context->next;
2072 memset (context, 0, sizeof (*context));
2074 else
2075 context = ggc_cleared_alloc<cp_parser_context> ();
2077 /* No errors have occurred yet in this context. */
2078 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2079 /* If this is not the bottommost context, copy information that we
2080 need from the previous context. */
2081 if (next)
2083 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2084 expression, then we are parsing one in this context, too. */
2085 context->object_type = next->object_type;
2086 /* Thread the stack. */
2087 context->next = next;
2090 return context;
2093 /* Managing the unparsed function queues. */
2095 #define unparsed_funs_with_default_args \
2096 parser->unparsed_queues->last ().funs_with_default_args
2097 #define unparsed_funs_with_definitions \
2098 parser->unparsed_queues->last ().funs_with_definitions
2099 #define unparsed_nsdmis \
2100 parser->unparsed_queues->last ().nsdmis
2101 #define unparsed_noexcepts \
2102 parser->unparsed_queues->last ().noexcepts
2104 static void
2105 push_unparsed_function_queues (cp_parser *parser)
2107 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2108 vec_safe_push (parser->unparsed_queues, e);
2111 static void
2112 pop_unparsed_function_queues (cp_parser *parser)
2114 release_tree_vector (unparsed_funs_with_definitions);
2115 parser->unparsed_queues->pop ();
2118 /* Prototypes. */
2120 /* Constructors and destructors. */
2122 static cp_parser *cp_parser_new
2123 (cp_lexer *);
2125 /* Routines to parse various constructs.
2127 Those that return `tree' will return the error_mark_node (rather
2128 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2129 Sometimes, they will return an ordinary node if error-recovery was
2130 attempted, even though a parse error occurred. So, to check
2131 whether or not a parse error occurred, you should always use
2132 cp_parser_error_occurred. If the construct is optional (indicated
2133 either by an `_opt' in the name of the function that does the
2134 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2135 the construct is not present. */
2137 /* Lexical conventions [gram.lex] */
2139 static cp_expr cp_parser_identifier
2140 (cp_parser *);
2141 static cp_expr cp_parser_string_literal
2142 (cp_parser *, bool, bool, bool);
2143 static cp_expr cp_parser_userdef_char_literal
2144 (cp_parser *);
2145 static tree cp_parser_userdef_string_literal
2146 (tree);
2147 static cp_expr cp_parser_userdef_numeric_literal
2148 (cp_parser *);
2150 /* Basic concepts [gram.basic] */
2152 static void cp_parser_translation_unit (cp_parser *);
2154 /* Expressions [gram.expr] */
2156 static cp_expr cp_parser_primary_expression
2157 (cp_parser *, bool, bool, bool, cp_id_kind *);
2158 static cp_expr cp_parser_id_expression
2159 (cp_parser *, bool, bool, bool *, bool, bool);
2160 static cp_expr cp_parser_unqualified_id
2161 (cp_parser *, bool, bool, bool, bool);
2162 static tree cp_parser_nested_name_specifier_opt
2163 (cp_parser *, bool, bool, bool, bool, bool = false);
2164 static tree cp_parser_nested_name_specifier
2165 (cp_parser *, bool, bool, bool, bool);
2166 static tree cp_parser_qualifying_entity
2167 (cp_parser *, bool, bool, bool, bool, bool);
2168 static cp_expr cp_parser_postfix_expression
2169 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2170 static tree cp_parser_postfix_open_square_expression
2171 (cp_parser *, tree, bool, bool);
2172 static tree cp_parser_postfix_dot_deref_expression
2173 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2174 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2175 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2176 bool = false);
2177 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2178 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2179 static void cp_parser_pseudo_destructor_name
2180 (cp_parser *, tree, tree *, tree *);
2181 static cp_expr cp_parser_unary_expression
2182 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2183 static enum tree_code cp_parser_unary_operator
2184 (cp_token *);
2185 static tree cp_parser_has_attribute_expression
2186 (cp_parser *);
2187 static tree cp_parser_new_expression
2188 (cp_parser *);
2189 static vec<tree, va_gc> *cp_parser_new_placement
2190 (cp_parser *);
2191 static tree cp_parser_new_type_id
2192 (cp_parser *, tree *);
2193 static cp_declarator *cp_parser_new_declarator_opt
2194 (cp_parser *);
2195 static cp_declarator *cp_parser_direct_new_declarator
2196 (cp_parser *);
2197 static vec<tree, va_gc> *cp_parser_new_initializer
2198 (cp_parser *);
2199 static tree cp_parser_delete_expression
2200 (cp_parser *);
2201 static cp_expr cp_parser_cast_expression
2202 (cp_parser *, bool, bool, bool, cp_id_kind *);
2203 static cp_expr cp_parser_binary_expression
2204 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2205 static tree cp_parser_question_colon_clause
2206 (cp_parser *, cp_expr);
2207 static cp_expr cp_parser_assignment_expression
2208 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2209 static enum tree_code cp_parser_assignment_operator_opt
2210 (cp_parser *);
2211 static cp_expr cp_parser_expression
2212 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2213 static cp_expr cp_parser_constant_expression
2214 (cp_parser *, int = 0, bool * = NULL, bool = false);
2215 static cp_expr cp_parser_builtin_offsetof
2216 (cp_parser *);
2217 static cp_expr cp_parser_lambda_expression
2218 (cp_parser *);
2219 static void cp_parser_lambda_introducer
2220 (cp_parser *, tree);
2221 static bool cp_parser_lambda_declarator_opt
2222 (cp_parser *, tree);
2223 static void cp_parser_lambda_body
2224 (cp_parser *, tree);
2226 /* Statements [gram.stmt.stmt] */
2228 static void cp_parser_statement
2229 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2230 static void cp_parser_label_for_labeled_statement
2231 (cp_parser *, tree);
2232 static tree cp_parser_expression_statement
2233 (cp_parser *, tree);
2234 static tree cp_parser_compound_statement
2235 (cp_parser *, tree, int, bool);
2236 static void cp_parser_statement_seq_opt
2237 (cp_parser *, tree);
2238 static tree cp_parser_selection_statement
2239 (cp_parser *, bool *, vec<tree> *);
2240 static tree cp_parser_condition
2241 (cp_parser *);
2242 static tree cp_parser_iteration_statement
2243 (cp_parser *, bool *, bool, unsigned short);
2244 static bool cp_parser_init_statement
2245 (cp_parser *, tree *decl);
2246 static tree cp_parser_for
2247 (cp_parser *, bool, unsigned short);
2248 static tree cp_parser_c_for
2249 (cp_parser *, tree, tree, bool, unsigned short);
2250 static tree cp_parser_range_for
2251 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2252 static void do_range_for_auto_deduction
2253 (tree, tree);
2254 static tree cp_parser_perform_range_for_lookup
2255 (tree, tree *, tree *);
2256 static tree cp_parser_range_for_member_function
2257 (tree, tree);
2258 static tree cp_parser_jump_statement
2259 (cp_parser *);
2260 static void cp_parser_declaration_statement
2261 (cp_parser *);
2263 static tree cp_parser_implicitly_scoped_statement
2264 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2265 static void cp_parser_already_scoped_statement
2266 (cp_parser *, bool *, const token_indent_info &);
2268 /* State of module-declaration parsing. */
2269 enum module_parse
2271 MP_NOT_MODULE, /* Not a module. */
2273 _MP_UNUSED,
2275 MP_FIRST, /* First declaration of TU. */
2276 MP_GLOBAL, /* Global Module Fragment. */
2278 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2279 MP_PURVIEW, /* Purview of a named module. */
2281 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2282 MP_PRIVATE, /* Private Module Fragment. */
2285 static module_parse cp_parser_module_declaration
2286 (cp_parser *parser, module_parse, bool exporting);
2287 static void cp_parser_import_declaration
2288 (cp_parser *parser, module_parse, bool exporting);
2290 /* Declarations [gram.dcl.dcl] */
2292 static void cp_parser_declaration_seq_opt
2293 (cp_parser *);
2294 static void cp_parser_declaration
2295 (cp_parser *, tree);
2296 static void cp_parser_toplevel_declaration
2297 (cp_parser *);
2298 static void cp_parser_block_declaration
2299 (cp_parser *, bool);
2300 static void cp_parser_simple_declaration
2301 (cp_parser *, bool, tree *);
2302 static void cp_parser_decl_specifier_seq
2303 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2304 static tree cp_parser_storage_class_specifier_opt
2305 (cp_parser *);
2306 static tree cp_parser_function_specifier_opt
2307 (cp_parser *, cp_decl_specifier_seq *);
2308 static tree cp_parser_type_specifier
2309 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2310 int *, bool *);
2311 static tree cp_parser_simple_type_specifier
2312 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2313 static tree cp_parser_placeholder_type_specifier
2314 (cp_parser *, location_t, tree, bool);
2315 static tree cp_parser_type_name
2316 (cp_parser *, bool);
2317 static tree cp_parser_nonclass_name
2318 (cp_parser* parser);
2319 static tree cp_parser_elaborated_type_specifier
2320 (cp_parser *, bool, bool);
2321 static tree cp_parser_enum_specifier
2322 (cp_parser *);
2323 static void cp_parser_enumerator_list
2324 (cp_parser *, tree);
2325 static void cp_parser_enumerator_definition
2326 (cp_parser *, tree);
2327 static tree cp_parser_namespace_name
2328 (cp_parser *);
2329 static void cp_parser_namespace_definition
2330 (cp_parser *);
2331 static void cp_parser_namespace_body
2332 (cp_parser *);
2333 static tree cp_parser_qualified_namespace_specifier
2334 (cp_parser *);
2335 static void cp_parser_namespace_alias_definition
2336 (cp_parser *);
2337 static bool cp_parser_using_declaration
2338 (cp_parser *, bool);
2339 static void cp_parser_using_directive
2340 (cp_parser *);
2341 static void cp_parser_using_enum
2342 (cp_parser *);
2343 static tree cp_parser_alias_declaration
2344 (cp_parser *);
2345 static void cp_parser_asm_definition
2346 (cp_parser *);
2347 static void cp_parser_linkage_specification
2348 (cp_parser *, tree);
2349 static void cp_parser_static_assert
2350 (cp_parser *, bool);
2351 static tree cp_parser_decltype
2352 (cp_parser *);
2353 static tree cp_parser_decomposition_declaration
2354 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2356 /* Declarators [gram.dcl.decl] */
2358 static tree cp_parser_init_declarator
2359 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2360 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2361 location_t *, tree *);
2362 static cp_declarator *cp_parser_declarator
2363 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2364 bool, bool, bool);
2365 static cp_declarator *cp_parser_direct_declarator
2366 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2367 bool);
2368 static enum tree_code cp_parser_ptr_operator
2369 (cp_parser *, tree *, cp_cv_quals *, tree *);
2370 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2371 (cp_parser *);
2372 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2373 (cp_parser *);
2374 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2375 (cp_parser *);
2376 static tree cp_parser_tx_qualifier_opt
2377 (cp_parser *);
2378 static tree cp_parser_late_return_type_opt
2379 (cp_parser *, cp_declarator *, tree &);
2380 static tree cp_parser_declarator_id
2381 (cp_parser *, bool);
2382 static tree cp_parser_type_id
2383 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2384 static tree cp_parser_template_type_arg
2385 (cp_parser *);
2386 static tree cp_parser_trailing_type_id (cp_parser *);
2387 static tree cp_parser_type_id_1
2388 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2389 static void cp_parser_type_specifier_seq
2390 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2391 static tree cp_parser_parameter_declaration_clause
2392 (cp_parser *, cp_parser_flags);
2393 static tree cp_parser_parameter_declaration_list
2394 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2395 static cp_parameter_declarator *cp_parser_parameter_declaration
2396 (cp_parser *, cp_parser_flags, bool, bool *);
2397 static tree cp_parser_default_argument
2398 (cp_parser *, bool);
2399 static void cp_parser_function_body
2400 (cp_parser *, bool);
2401 static tree cp_parser_initializer
2402 (cp_parser *, bool *, bool *, bool = false);
2403 static cp_expr cp_parser_initializer_clause
2404 (cp_parser *, bool *);
2405 static cp_expr cp_parser_braced_list
2406 (cp_parser*, bool*);
2407 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2408 (cp_parser *, bool *, bool *);
2410 static void cp_parser_ctor_initializer_opt_and_function_body
2411 (cp_parser *, bool);
2413 static tree cp_parser_late_parsing_omp_declare_simd
2414 (cp_parser *, tree);
2416 static tree cp_parser_late_parsing_oacc_routine
2417 (cp_parser *, tree);
2419 static tree synthesize_implicit_template_parm
2420 (cp_parser *, tree);
2421 static tree finish_fully_implicit_template
2422 (cp_parser *, tree);
2423 static void abort_fully_implicit_template
2424 (cp_parser *);
2426 /* Classes [gram.class] */
2428 static tree cp_parser_class_name
2429 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2430 static tree cp_parser_class_specifier
2431 (cp_parser *);
2432 static tree cp_parser_class_head
2433 (cp_parser *, bool *);
2434 static enum tag_types cp_parser_class_key
2435 (cp_parser *);
2436 static void cp_parser_type_parameter_key
2437 (cp_parser* parser);
2438 static void cp_parser_member_specification_opt
2439 (cp_parser *);
2440 static void cp_parser_member_declaration
2441 (cp_parser *);
2442 static tree cp_parser_pure_specifier
2443 (cp_parser *);
2444 static tree cp_parser_constant_initializer
2445 (cp_parser *);
2447 /* Derived classes [gram.class.derived] */
2449 static tree cp_parser_base_clause
2450 (cp_parser *);
2451 static tree cp_parser_base_specifier
2452 (cp_parser *);
2454 /* Special member functions [gram.special] */
2456 static tree cp_parser_conversion_function_id
2457 (cp_parser *);
2458 static tree cp_parser_conversion_type_id
2459 (cp_parser *);
2460 static cp_declarator *cp_parser_conversion_declarator_opt
2461 (cp_parser *);
2462 static void cp_parser_ctor_initializer_opt
2463 (cp_parser *);
2464 static void cp_parser_mem_initializer_list
2465 (cp_parser *);
2466 static tree cp_parser_mem_initializer
2467 (cp_parser *);
2468 static tree cp_parser_mem_initializer_id
2469 (cp_parser *);
2471 /* Overloading [gram.over] */
2473 static cp_expr cp_parser_operator_function_id
2474 (cp_parser *);
2475 static cp_expr cp_parser_operator
2476 (cp_parser *, location_t);
2478 /* Templates [gram.temp] */
2480 static void cp_parser_template_declaration
2481 (cp_parser *, bool);
2482 static tree cp_parser_template_parameter_list
2483 (cp_parser *);
2484 static tree cp_parser_template_parameter
2485 (cp_parser *, bool *, bool *);
2486 static tree cp_parser_type_parameter
2487 (cp_parser *, bool *);
2488 static tree cp_parser_template_id
2489 (cp_parser *, bool, bool, enum tag_types, bool);
2490 static tree cp_parser_template_id_expr
2491 (cp_parser *, bool, bool, bool);
2492 static tree cp_parser_template_name
2493 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2494 static tree cp_parser_template_argument_list
2495 (cp_parser *);
2496 static tree cp_parser_template_argument
2497 (cp_parser *);
2498 static void cp_parser_explicit_instantiation
2499 (cp_parser *);
2500 static void cp_parser_explicit_specialization
2501 (cp_parser *);
2503 /* Exception handling [gram.except] */
2505 static tree cp_parser_try_block
2506 (cp_parser *);
2507 static void cp_parser_function_try_block
2508 (cp_parser *);
2509 static void cp_parser_handler_seq
2510 (cp_parser *);
2511 static void cp_parser_handler
2512 (cp_parser *);
2513 static tree cp_parser_exception_declaration
2514 (cp_parser *);
2515 static tree cp_parser_throw_expression
2516 (cp_parser *);
2517 static tree cp_parser_exception_specification_opt
2518 (cp_parser *, cp_parser_flags);
2519 static tree cp_parser_type_id_list
2520 (cp_parser *);
2521 static tree cp_parser_noexcept_specification_opt
2522 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2524 /* GNU Extensions */
2526 static tree cp_parser_asm_specification_opt
2527 (cp_parser *);
2528 static tree cp_parser_asm_operand_list
2529 (cp_parser *);
2530 static tree cp_parser_asm_clobber_list
2531 (cp_parser *);
2532 static tree cp_parser_asm_label_list
2533 (cp_parser *);
2534 static bool cp_next_tokens_can_be_attribute_p
2535 (cp_parser *);
2536 static bool cp_next_tokens_can_be_gnu_attribute_p
2537 (cp_parser *);
2538 static bool cp_next_tokens_can_be_std_attribute_p
2539 (cp_parser *);
2540 static bool cp_nth_tokens_can_be_std_attribute_p
2541 (cp_parser *, size_t);
2542 static bool cp_nth_tokens_can_be_gnu_attribute_p
2543 (cp_parser *, size_t);
2544 static bool cp_nth_tokens_can_be_attribute_p
2545 (cp_parser *, size_t);
2546 static tree cp_parser_attributes_opt
2547 (cp_parser *);
2548 static tree cp_parser_gnu_attributes_opt
2549 (cp_parser *);
2550 static tree cp_parser_gnu_attribute_list
2551 (cp_parser *, bool = false);
2552 static tree cp_parser_std_attribute
2553 (cp_parser *, tree);
2554 static tree cp_parser_std_attribute_spec
2555 (cp_parser *);
2556 static tree cp_parser_std_attribute_spec_seq
2557 (cp_parser *);
2558 static size_t cp_parser_skip_std_attribute_spec_seq
2559 (cp_parser *, size_t);
2560 static size_t cp_parser_skip_attributes_opt
2561 (cp_parser *, size_t);
2562 static bool cp_parser_extension_opt
2563 (cp_parser *, int *);
2564 static void cp_parser_label_declaration
2565 (cp_parser *);
2567 /* Concept Extensions */
2569 static tree cp_parser_concept_definition
2570 (cp_parser *);
2571 static tree cp_parser_constraint_expression
2572 (cp_parser *);
2573 static tree cp_parser_requires_clause_opt
2574 (cp_parser *, bool);
2575 static tree cp_parser_requires_expression
2576 (cp_parser *);
2577 static tree cp_parser_requirement_parameter_list
2578 (cp_parser *);
2579 static tree cp_parser_requirement_body
2580 (cp_parser *);
2581 static tree cp_parser_requirement_seq
2582 (cp_parser *);
2583 static tree cp_parser_requirement
2584 (cp_parser *);
2585 static tree cp_parser_simple_requirement
2586 (cp_parser *);
2587 static tree cp_parser_compound_requirement
2588 (cp_parser *);
2589 static tree cp_parser_type_requirement
2590 (cp_parser *);
2591 static tree cp_parser_nested_requirement
2592 (cp_parser *);
2594 /* Transactional Memory Extensions */
2596 static tree cp_parser_transaction
2597 (cp_parser *, cp_token *);
2598 static tree cp_parser_transaction_expression
2599 (cp_parser *, enum rid);
2600 static void cp_parser_function_transaction
2601 (cp_parser *, enum rid);
2602 static tree cp_parser_transaction_cancel
2603 (cp_parser *);
2605 /* Coroutine extensions. */
2607 static tree cp_parser_yield_expression
2608 (cp_parser *);
2611 enum pragma_context {
2612 pragma_external,
2613 pragma_member,
2614 pragma_objc_icode,
2615 pragma_stmt,
2616 pragma_compound
2618 static bool cp_parser_pragma
2619 (cp_parser *, enum pragma_context, bool *);
2621 /* Objective-C++ Productions */
2623 static tree cp_parser_objc_message_receiver
2624 (cp_parser *);
2625 static tree cp_parser_objc_message_args
2626 (cp_parser *);
2627 static tree cp_parser_objc_message_expression
2628 (cp_parser *);
2629 static cp_expr cp_parser_objc_encode_expression
2630 (cp_parser *);
2631 static tree cp_parser_objc_defs_expression
2632 (cp_parser *);
2633 static tree cp_parser_objc_protocol_expression
2634 (cp_parser *);
2635 static tree cp_parser_objc_selector_expression
2636 (cp_parser *);
2637 static cp_expr cp_parser_objc_expression
2638 (cp_parser *);
2639 static bool cp_parser_objc_selector_p
2640 (enum cpp_ttype);
2641 static tree cp_parser_objc_selector
2642 (cp_parser *);
2643 static tree cp_parser_objc_protocol_refs_opt
2644 (cp_parser *);
2645 static void cp_parser_objc_declaration
2646 (cp_parser *, tree);
2647 static tree cp_parser_objc_statement
2648 (cp_parser *);
2649 static bool cp_parser_objc_valid_prefix_attributes
2650 (cp_parser *, tree *);
2651 static void cp_parser_objc_at_property_declaration
2652 (cp_parser *) ;
2653 static void cp_parser_objc_at_synthesize_declaration
2654 (cp_parser *) ;
2655 static void cp_parser_objc_at_dynamic_declaration
2656 (cp_parser *) ;
2657 static tree cp_parser_objc_struct_declaration
2658 (cp_parser *) ;
2660 /* Utility Routines */
2662 static cp_expr cp_parser_lookup_name
2663 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2664 static tree cp_parser_lookup_name_simple
2665 (cp_parser *, tree, location_t);
2666 static tree cp_parser_maybe_treat_template_as_class
2667 (tree, bool);
2668 static bool cp_parser_check_declarator_template_parameters
2669 (cp_parser *, cp_declarator *, location_t);
2670 static bool cp_parser_check_template_parameters
2671 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2672 static cp_expr cp_parser_simple_cast_expression
2673 (cp_parser *);
2674 static tree cp_parser_global_scope_opt
2675 (cp_parser *, bool);
2676 static bool cp_parser_constructor_declarator_p
2677 (cp_parser *, cp_parser_flags, bool);
2678 static tree cp_parser_function_definition_from_specifiers_and_declarator
2679 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2680 static tree cp_parser_function_definition_after_declarator
2681 (cp_parser *, bool);
2682 static bool cp_parser_template_declaration_after_export
2683 (cp_parser *, bool);
2684 static void cp_parser_perform_template_parameter_access_checks
2685 (vec<deferred_access_check, va_gc> *);
2686 static tree cp_parser_single_declaration
2687 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2688 static cp_expr cp_parser_functional_cast
2689 (cp_parser *, tree);
2690 static tree cp_parser_save_member_function_body
2691 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2692 static tree cp_parser_save_nsdmi
2693 (cp_parser *);
2694 static tree cp_parser_enclosed_template_argument_list
2695 (cp_parser *);
2696 static void cp_parser_save_default_args
2697 (cp_parser *, tree);
2698 static void cp_parser_late_parsing_for_member
2699 (cp_parser *, tree);
2700 static tree cp_parser_late_parse_one_default_arg
2701 (cp_parser *, tree, tree, tree);
2702 static void cp_parser_late_parsing_nsdmi
2703 (cp_parser *, tree);
2704 static void cp_parser_late_parsing_default_args
2705 (cp_parser *, tree);
2706 static tree cp_parser_sizeof_operand
2707 (cp_parser *, enum rid);
2708 static cp_expr cp_parser_trait_expr
2709 (cp_parser *, enum rid);
2710 static bool cp_parser_declares_only_class_p
2711 (cp_parser *);
2712 static void cp_parser_set_storage_class
2713 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2714 static void cp_parser_set_decl_spec_type
2715 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2716 static void set_and_check_decl_spec_loc
2717 (cp_decl_specifier_seq *decl_specs,
2718 cp_decl_spec ds, cp_token *);
2719 static bool cp_parser_friend_p
2720 (const cp_decl_specifier_seq *);
2721 static void cp_parser_required_error
2722 (cp_parser *, required_token, bool, location_t);
2723 static cp_token *cp_parser_require
2724 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2725 static cp_token *cp_parser_require_keyword
2726 (cp_parser *, enum rid, required_token);
2727 static bool cp_parser_token_starts_function_definition_p
2728 (cp_token *);
2729 static bool cp_parser_next_token_starts_class_definition_p
2730 (cp_parser *);
2731 static bool cp_parser_next_token_ends_template_argument_p
2732 (cp_parser *);
2733 static bool cp_parser_nth_token_starts_template_argument_list_p
2734 (cp_parser *, size_t);
2735 static enum tag_types cp_parser_token_is_class_key
2736 (cp_token *);
2737 static enum tag_types cp_parser_token_is_type_parameter_key
2738 (cp_token *);
2739 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2740 static void cp_parser_check_class_key
2741 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2742 static void cp_parser_check_access_in_redeclaration
2743 (tree type, location_t location);
2744 static bool cp_parser_optional_template_keyword
2745 (cp_parser *);
2746 static void cp_parser_pre_parsed_nested_name_specifier
2747 (cp_parser *);
2748 static bool cp_parser_cache_group
2749 (cp_parser *, enum cpp_ttype, unsigned);
2750 static tree cp_parser_cache_defarg
2751 (cp_parser *parser, bool nsdmi);
2752 static void cp_parser_parse_tentatively
2753 (cp_parser *);
2754 static void cp_parser_commit_to_tentative_parse
2755 (cp_parser *);
2756 static void cp_parser_commit_to_topmost_tentative_parse
2757 (cp_parser *);
2758 static void cp_parser_abort_tentative_parse
2759 (cp_parser *);
2760 static bool cp_parser_parse_definitely
2761 (cp_parser *);
2762 static inline bool cp_parser_parsing_tentatively
2763 (cp_parser *);
2764 static bool cp_parser_uncommitted_to_tentative_parse_p
2765 (cp_parser *);
2766 static void cp_parser_error
2767 (cp_parser *, const char *);
2768 static void cp_parser_name_lookup_error
2769 (cp_parser *, tree, tree, name_lookup_error, location_t);
2770 static bool cp_parser_simulate_error
2771 (cp_parser *);
2772 static bool cp_parser_check_type_definition
2773 (cp_parser *);
2774 static void cp_parser_check_for_definition_in_return_type
2775 (cp_declarator *, tree, location_t type_location);
2776 static void cp_parser_check_for_invalid_template_id
2777 (cp_parser *, tree, enum tag_types, location_t location);
2778 static bool cp_parser_non_integral_constant_expression
2779 (cp_parser *, non_integral_constant);
2780 static void cp_parser_diagnose_invalid_type_name
2781 (cp_parser *, tree, location_t);
2782 static bool cp_parser_parse_and_diagnose_invalid_type_name
2783 (cp_parser *);
2784 static int cp_parser_skip_to_closing_parenthesis
2785 (cp_parser *, bool, bool, bool);
2786 static void cp_parser_skip_to_end_of_statement
2787 (cp_parser *);
2788 static void cp_parser_consume_semicolon_at_end_of_statement
2789 (cp_parser *);
2790 static void cp_parser_skip_to_end_of_block_or_statement
2791 (cp_parser *);
2792 static bool cp_parser_skip_to_closing_brace
2793 (cp_parser *);
2794 static bool cp_parser_skip_entire_template_parameter_list
2795 (cp_parser *);
2796 static void cp_parser_require_end_of_template_parameter_list
2797 (cp_parser *);
2798 static bool cp_parser_skip_to_end_of_template_parameter_list
2799 (cp_parser *);
2800 static void cp_parser_skip_to_pragma_eol
2801 (cp_parser*, cp_token *);
2802 static bool cp_parser_error_occurred
2803 (cp_parser *);
2804 static bool cp_parser_allow_gnu_extensions_p
2805 (cp_parser *);
2806 static bool cp_parser_is_pure_string_literal
2807 (cp_token *);
2808 static bool cp_parser_is_string_literal
2809 (cp_token *);
2810 static bool cp_parser_is_keyword
2811 (cp_token *, enum rid);
2812 static tree cp_parser_make_typename_type
2813 (cp_parser *, tree, location_t location);
2814 static cp_declarator * cp_parser_make_indirect_declarator
2815 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2816 static bool cp_parser_compound_literal_p
2817 (cp_parser *);
2818 static bool cp_parser_array_designator_p
2819 (cp_parser *);
2820 static bool cp_parser_init_statement_p
2821 (cp_parser *);
2822 static bool cp_parser_skip_to_closing_square_bracket
2823 (cp_parser *);
2824 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2826 // -------------------------------------------------------------------------- //
2827 // Unevaluated Operand Guard
2829 // Implementation of an RAII helper for unevaluated operand parsing.
2830 cp_unevaluated::cp_unevaluated ()
2832 ++cp_unevaluated_operand;
2833 ++c_inhibit_evaluation_warnings;
2836 cp_unevaluated::~cp_unevaluated ()
2838 --c_inhibit_evaluation_warnings;
2839 --cp_unevaluated_operand;
2842 // -------------------------------------------------------------------------- //
2843 // Tentative Parsing
2845 /* Returns nonzero if we are parsing tentatively. */
2847 static inline bool
2848 cp_parser_parsing_tentatively (cp_parser* parser)
2850 return parser->context->next != NULL;
2853 /* Returns nonzero if TOKEN is a string literal. */
2855 static bool
2856 cp_parser_is_pure_string_literal (cp_token* token)
2858 return (token->type == CPP_STRING ||
2859 token->type == CPP_STRING16 ||
2860 token->type == CPP_STRING32 ||
2861 token->type == CPP_WSTRING ||
2862 token->type == CPP_UTF8STRING);
2865 /* Returns nonzero if TOKEN is a string literal
2866 of a user-defined string literal. */
2868 static bool
2869 cp_parser_is_string_literal (cp_token* token)
2871 return (cp_parser_is_pure_string_literal (token) ||
2872 token->type == CPP_STRING_USERDEF ||
2873 token->type == CPP_STRING16_USERDEF ||
2874 token->type == CPP_STRING32_USERDEF ||
2875 token->type == CPP_WSTRING_USERDEF ||
2876 token->type == CPP_UTF8STRING_USERDEF);
2879 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2881 static bool
2882 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2884 return token->keyword == keyword;
2887 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2888 PRAGMA_NONE. */
2890 static enum pragma_kind
2891 cp_parser_pragma_kind (cp_token *token)
2893 if (token->type != CPP_PRAGMA)
2894 return PRAGMA_NONE;
2895 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2896 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2899 /* Helper function for cp_parser_error.
2900 Having peeked a token of kind TOK1_KIND that might signify
2901 a conflict marker, peek successor tokens to determine
2902 if we actually do have a conflict marker.
2903 Specifically, we consider a run of 7 '<', '=' or '>' characters
2904 at the start of a line as a conflict marker.
2905 These come through the lexer as three pairs and a single,
2906 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2907 If it returns true, *OUT_LOC is written to with the location/range
2908 of the marker. */
2910 static bool
2911 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2912 location_t *out_loc)
2914 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2915 if (token2->type != tok1_kind)
2916 return false;
2917 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2918 if (token3->type != tok1_kind)
2919 return false;
2920 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2921 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2922 return false;
2924 /* It must be at the start of the line. */
2925 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2926 if (LOCATION_COLUMN (start_loc) != 1)
2927 return false;
2929 /* We have a conflict marker. Construct a location of the form:
2930 <<<<<<<
2931 ^~~~~~~
2932 with start == caret, finishing at the end of the marker. */
2933 location_t finish_loc = get_finish (token4->location);
2934 *out_loc = make_location (start_loc, start_loc, finish_loc);
2936 return true;
2939 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2940 RT_CLOSE_PAREN. */
2942 static const char *
2943 get_matching_symbol (required_token token_desc)
2945 switch (token_desc)
2947 default:
2948 gcc_unreachable ();
2949 return "";
2950 case RT_CLOSE_BRACE:
2951 return "{";
2952 case RT_CLOSE_PAREN:
2953 return "(";
2957 /* Attempt to convert TOKEN_DESC from a required_token to an
2958 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2960 static enum cpp_ttype
2961 get_required_cpp_ttype (required_token token_desc)
2963 switch (token_desc)
2965 case RT_SEMICOLON:
2966 return CPP_SEMICOLON;
2967 case RT_OPEN_PAREN:
2968 return CPP_OPEN_PAREN;
2969 case RT_CLOSE_BRACE:
2970 return CPP_CLOSE_BRACE;
2971 case RT_OPEN_BRACE:
2972 return CPP_OPEN_BRACE;
2973 case RT_CLOSE_SQUARE:
2974 return CPP_CLOSE_SQUARE;
2975 case RT_OPEN_SQUARE:
2976 return CPP_OPEN_SQUARE;
2977 case RT_COMMA:
2978 return CPP_COMMA;
2979 case RT_COLON:
2980 return CPP_COLON;
2981 case RT_CLOSE_PAREN:
2982 return CPP_CLOSE_PAREN;
2984 default:
2985 /* Use CPP_EOF as a "no completions possible" code. */
2986 return CPP_EOF;
2991 /* Subroutine of cp_parser_error and cp_parser_required_error.
2993 Issue a diagnostic of the form
2994 FILE:LINE: MESSAGE before TOKEN
2995 where TOKEN is the next token in the input stream. MESSAGE
2996 (specified by the caller) is usually of the form "expected
2997 OTHER-TOKEN".
2999 This bypasses the check for tentative passing, and potentially
3000 adds material needed by cp_parser_required_error.
3002 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3003 suggesting insertion of the missing token.
3005 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3006 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3007 location. */
3009 static void
3010 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3011 required_token missing_token_desc,
3012 location_t matching_location)
3014 cp_token *token = cp_lexer_peek_token (parser->lexer);
3015 /* This diagnostic makes more sense if it is tagged to the line
3016 of the token we just peeked at. */
3017 cp_lexer_set_source_position_from_token (token);
3019 if (token->type == CPP_PRAGMA)
3021 error_at (token->location,
3022 "%<#pragma%> is not allowed here");
3023 cp_parser_skip_to_pragma_eol (parser, token);
3024 return;
3027 /* If this is actually a conflict marker, report it as such. */
3028 if (token->type == CPP_LSHIFT
3029 || token->type == CPP_RSHIFT
3030 || token->type == CPP_EQ_EQ)
3032 location_t loc;
3033 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3035 error_at (loc, "version control conflict marker in file");
3036 expanded_location token_exploc = expand_location (token->location);
3037 /* Consume tokens until the end of the source line. */
3038 for (;;)
3040 cp_lexer_consume_token (parser->lexer);
3041 cp_token *next = cp_lexer_peek_token (parser->lexer);
3042 if (next->type == CPP_EOF)
3043 break;
3044 if (next->location == UNKNOWN_LOCATION
3045 || loc == UNKNOWN_LOCATION)
3046 break;
3048 expanded_location next_exploc = expand_location (next->location);
3049 if (next_exploc.file != token_exploc.file)
3050 break;
3051 if (next_exploc.line != token_exploc.line)
3052 break;
3054 return;
3058 auto_diagnostic_group d;
3059 gcc_rich_location richloc (input_location);
3061 bool added_matching_location = false;
3063 if (missing_token_desc != RT_NONE)
3064 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3066 /* Potentially supply a fix-it hint, suggesting to add the
3067 missing token immediately after the *previous* token.
3068 This may move the primary location within richloc. */
3069 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3070 location_t prev_token_loc = prev_token->location;
3071 maybe_suggest_missing_token_insertion (&richloc, ttype,
3072 prev_token_loc);
3074 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3075 Attempt to consolidate diagnostics by printing it as a
3076 secondary range within the main diagnostic. */
3077 if (matching_location != UNKNOWN_LOCATION)
3078 added_matching_location
3079 = richloc.add_location_if_nearby (matching_location);
3082 /* If we were parsing a string-literal and there is an unknown name
3083 token right after, then check to see if that could also have been
3084 a literal string by checking the name against a list of known
3085 standard string literal constants defined in header files. If
3086 there is one, then add that as an hint to the error message. */
3087 name_hint h;
3088 if (token->type == CPP_NAME)
3089 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3090 if (cp_parser_is_string_literal (prev_token))
3092 tree name = token->u.value;
3093 const char *token_name = IDENTIFIER_POINTER (name);
3094 const char *header_hint
3095 = get_cp_stdlib_header_for_string_macro_name (token_name);
3096 if (header_hint != NULL)
3097 h = name_hint (NULL, new suggest_missing_header (token->location,
3098 token_name,
3099 header_hint));
3102 /* Actually emit the error. */
3103 c_parse_error (gmsgid,
3104 /* Because c_parser_error does not understand
3105 CPP_KEYWORD, keywords are treated like
3106 identifiers. */
3107 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3108 token->u.value, token->flags, &richloc);
3110 if (missing_token_desc != RT_NONE)
3112 /* If we weren't able to consolidate matching_location, then
3113 print it as a secondary diagnostic. */
3114 if (matching_location != UNKNOWN_LOCATION
3115 && !added_matching_location)
3116 inform (matching_location, "to match this %qs",
3117 get_matching_symbol (missing_token_desc));
3121 /* If not parsing tentatively, issue a diagnostic of the form
3122 FILE:LINE: MESSAGE before TOKEN
3123 where TOKEN is the next token in the input stream. MESSAGE
3124 (specified by the caller) is usually of the form "expected
3125 OTHER-TOKEN". */
3127 static void
3128 cp_parser_error (cp_parser* parser, const char* gmsgid)
3130 if (!cp_parser_simulate_error (parser))
3131 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3134 /* Issue an error about name-lookup failing. NAME is the
3135 IDENTIFIER_NODE DECL is the result of
3136 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3137 the thing that we hoped to find. */
3139 static void
3140 cp_parser_name_lookup_error (cp_parser* parser,
3141 tree name,
3142 tree decl,
3143 name_lookup_error desired,
3144 location_t location)
3146 /* If name lookup completely failed, tell the user that NAME was not
3147 declared. */
3148 if (decl == error_mark_node)
3150 if (parser->scope && parser->scope != global_namespace)
3151 error_at (location, "%<%E::%E%> has not been declared",
3152 parser->scope, name);
3153 else if (parser->scope == global_namespace)
3154 error_at (location, "%<::%E%> has not been declared", name);
3155 else if (parser->object_scope
3156 && !CLASS_TYPE_P (parser->object_scope))
3157 error_at (location, "request for member %qE in non-class type %qT",
3158 name, parser->object_scope);
3159 else if (parser->object_scope)
3160 error_at (location, "%<%T::%E%> has not been declared",
3161 parser->object_scope, name);
3162 else
3163 error_at (location, "%qE has not been declared", name);
3165 else if (parser->scope && parser->scope != global_namespace)
3167 switch (desired)
3169 case NLE_TYPE:
3170 error_at (location, "%<%E::%E%> is not a type",
3171 parser->scope, name);
3172 break;
3173 case NLE_CXX98:
3174 error_at (location, "%<%E::%E%> is not a class or namespace",
3175 parser->scope, name);
3176 break;
3177 case NLE_NOT_CXX98:
3178 error_at (location,
3179 "%<%E::%E%> is not a class, namespace, or enumeration",
3180 parser->scope, name);
3181 break;
3182 default:
3183 gcc_unreachable ();
3187 else if (parser->scope == global_namespace)
3189 switch (desired)
3191 case NLE_TYPE:
3192 error_at (location, "%<::%E%> is not a type", name);
3193 break;
3194 case NLE_CXX98:
3195 error_at (location, "%<::%E%> is not a class or namespace", name);
3196 break;
3197 case NLE_NOT_CXX98:
3198 error_at (location,
3199 "%<::%E%> is not a class, namespace, or enumeration",
3200 name);
3201 break;
3202 default:
3203 gcc_unreachable ();
3206 else
3208 switch (desired)
3210 case NLE_TYPE:
3211 error_at (location, "%qE is not a type", name);
3212 break;
3213 case NLE_CXX98:
3214 error_at (location, "%qE is not a class or namespace", name);
3215 break;
3216 case NLE_NOT_CXX98:
3217 error_at (location,
3218 "%qE is not a class, namespace, or enumeration", name);
3219 break;
3220 default:
3221 gcc_unreachable ();
3226 /* If we are parsing tentatively, remember that an error has occurred
3227 during this tentative parse. Returns true if the error was
3228 simulated; false if a message should be issued by the caller. */
3230 static bool
3231 cp_parser_simulate_error (cp_parser* parser)
3233 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3235 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3236 return true;
3238 return false;
3241 /* This function is called when a type is defined. If type
3242 definitions are forbidden at this point, an error message is
3243 issued. */
3245 static bool
3246 cp_parser_check_type_definition (cp_parser* parser)
3248 /* If types are forbidden here, issue a message. */
3249 if (parser->type_definition_forbidden_message)
3251 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3252 or %qs in the message need to be interpreted. */
3253 error (parser->type_definition_forbidden_message,
3254 parser->type_definition_forbidden_message_arg);
3255 return false;
3257 return true;
3260 /* This function is called when the DECLARATOR is processed. The TYPE
3261 was a type defined in the decl-specifiers. If it is invalid to
3262 define a type in the decl-specifiers for DECLARATOR, an error is
3263 issued. TYPE_LOCATION is the location of TYPE and is used
3264 for error reporting. */
3266 static void
3267 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3268 tree type, location_t type_location)
3270 /* [dcl.fct] forbids type definitions in return types.
3271 Unfortunately, it's not easy to know whether or not we are
3272 processing a return type until after the fact. */
3273 while (declarator
3274 && (declarator->kind == cdk_pointer
3275 || declarator->kind == cdk_reference
3276 || declarator->kind == cdk_ptrmem))
3277 declarator = declarator->declarator;
3278 if (declarator
3279 && declarator->kind == cdk_function)
3281 error_at (type_location,
3282 "new types may not be defined in a return type");
3283 inform (type_location,
3284 "(perhaps a semicolon is missing after the definition of %qT)",
3285 type);
3289 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3290 "<" in any valid C++ program. If the next token is indeed "<",
3291 issue a message warning the user about what appears to be an
3292 invalid attempt to form a template-id. LOCATION is the location
3293 of the type-specifier (TYPE) */
3295 static void
3296 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3297 tree type,
3298 enum tag_types tag_type,
3299 location_t location)
3301 cp_token_position start = 0;
3303 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3305 if (TREE_CODE (type) == TYPE_DECL)
3306 type = TREE_TYPE (type);
3307 if (TYPE_P (type) && !template_placeholder_p (type))
3308 error_at (location, "%qT is not a template", type);
3309 else if (identifier_p (type))
3311 if (tag_type != none_type)
3312 error_at (location, "%qE is not a class template", type);
3313 else
3314 error_at (location, "%qE is not a template", type);
3316 else
3317 error_at (location, "invalid template-id");
3318 /* Remember the location of the invalid "<". */
3319 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3320 start = cp_lexer_token_position (parser->lexer, true);
3321 /* Consume the "<". */
3322 cp_lexer_consume_token (parser->lexer);
3323 /* Parse the template arguments. */
3324 cp_parser_enclosed_template_argument_list (parser);
3325 /* Permanently remove the invalid template arguments so that
3326 this error message is not issued again. */
3327 if (start)
3328 cp_lexer_purge_tokens_after (parser->lexer, start);
3332 /* If parsing an integral constant-expression, issue an error message
3333 about the fact that THING appeared and return true. Otherwise,
3334 return false. In either case, set
3335 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3337 static bool
3338 cp_parser_non_integral_constant_expression (cp_parser *parser,
3339 non_integral_constant thing)
3341 parser->non_integral_constant_expression_p = true;
3342 if (parser->integral_constant_expression_p)
3344 if (!parser->allow_non_integral_constant_expression_p)
3346 const char *msg = NULL;
3347 switch (thing)
3349 case NIC_FLOAT:
3350 pedwarn (input_location, OPT_Wpedantic,
3351 "ISO C++ forbids using a floating-point literal "
3352 "in a constant-expression");
3353 return true;
3354 case NIC_CAST:
3355 error ("a cast to a type other than an integral or "
3356 "enumeration type cannot appear in a "
3357 "constant-expression");
3358 return true;
3359 case NIC_TYPEID:
3360 error ("%<typeid%> operator "
3361 "cannot appear in a constant-expression");
3362 return true;
3363 case NIC_NCC:
3364 error ("non-constant compound literals "
3365 "cannot appear in a constant-expression");
3366 return true;
3367 case NIC_FUNC_CALL:
3368 error ("a function call "
3369 "cannot appear in a constant-expression");
3370 return true;
3371 case NIC_INC:
3372 error ("an increment "
3373 "cannot appear in a constant-expression");
3374 return true;
3375 case NIC_DEC:
3376 error ("an decrement "
3377 "cannot appear in a constant-expression");
3378 return true;
3379 case NIC_ARRAY_REF:
3380 error ("an array reference "
3381 "cannot appear in a constant-expression");
3382 return true;
3383 case NIC_ADDR_LABEL:
3384 error ("the address of a label "
3385 "cannot appear in a constant-expression");
3386 return true;
3387 case NIC_OVERLOADED:
3388 error ("calls to overloaded operators "
3389 "cannot appear in a constant-expression");
3390 return true;
3391 case NIC_ASSIGNMENT:
3392 error ("an assignment cannot appear in a constant-expression");
3393 return true;
3394 case NIC_COMMA:
3395 error ("a comma operator "
3396 "cannot appear in a constant-expression");
3397 return true;
3398 case NIC_CONSTRUCTOR:
3399 error ("a call to a constructor "
3400 "cannot appear in a constant-expression");
3401 return true;
3402 case NIC_TRANSACTION:
3403 error ("a transaction expression "
3404 "cannot appear in a constant-expression");
3405 return true;
3406 case NIC_THIS:
3407 msg = "this";
3408 break;
3409 case NIC_FUNC_NAME:
3410 msg = "__FUNCTION__";
3411 break;
3412 case NIC_PRETTY_FUNC:
3413 msg = "__PRETTY_FUNCTION__";
3414 break;
3415 case NIC_C99_FUNC:
3416 msg = "__func__";
3417 break;
3418 case NIC_VA_ARG:
3419 msg = "va_arg";
3420 break;
3421 case NIC_ARROW:
3422 msg = "->";
3423 break;
3424 case NIC_POINT:
3425 msg = ".";
3426 break;
3427 case NIC_STAR:
3428 msg = "*";
3429 break;
3430 case NIC_ADDR:
3431 msg = "&";
3432 break;
3433 case NIC_PREINCREMENT:
3434 msg = "++";
3435 break;
3436 case NIC_PREDECREMENT:
3437 msg = "--";
3438 break;
3439 case NIC_NEW:
3440 msg = "new";
3441 break;
3442 case NIC_DEL:
3443 msg = "delete";
3444 break;
3445 default:
3446 gcc_unreachable ();
3448 if (msg)
3449 error ("%qs cannot appear in a constant-expression", msg);
3450 return true;
3453 return false;
3456 /* Emit a diagnostic for an invalid type name. This function commits
3457 to the current active tentative parse, if any. (Otherwise, the
3458 problematic construct might be encountered again later, resulting
3459 in duplicate error messages.) LOCATION is the location of ID. */
3461 static void
3462 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3463 location_t location)
3465 tree decl, ambiguous_decls;
3466 cp_parser_commit_to_tentative_parse (parser);
3467 /* Try to lookup the identifier. */
3468 decl = cp_parser_lookup_name (parser, id, none_type,
3469 /*is_template=*/false,
3470 /*is_namespace=*/false,
3471 /*check_dependency=*/true,
3472 &ambiguous_decls, location);
3473 if (ambiguous_decls)
3474 /* If the lookup was ambiguous, an error will already have
3475 been issued. */
3476 return;
3477 /* If the lookup found a template-name, it means that the user forgot
3478 to specify an argument list. Emit a useful error message. */
3479 if (DECL_TYPE_TEMPLATE_P (decl))
3481 auto_diagnostic_group d;
3482 error_at (location,
3483 "invalid use of template-name %qE without an argument list",
3484 decl);
3485 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3486 inform (location, "class template argument deduction is only available "
3487 "with %<-std=c++17%> or %<-std=gnu++17%>");
3488 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3490 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3491 error_at (location, "invalid use of destructor %qD as a type", id);
3492 else if (TREE_CODE (decl) == TYPE_DECL)
3493 /* Something like 'unsigned A a;' */
3494 error_at (location, "invalid combination of multiple type-specifiers");
3495 else if (!parser->scope)
3497 /* Issue an error message. */
3498 auto_diagnostic_group d;
3499 name_hint hint;
3500 if (TREE_CODE (id) == IDENTIFIER_NODE)
3501 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3502 if (const char *suggestion = hint.suggestion ())
3504 gcc_rich_location richloc (location);
3505 richloc.add_fixit_replace (suggestion);
3506 error_at (&richloc,
3507 "%qE does not name a type; did you mean %qs?",
3508 id, suggestion);
3510 else
3511 error_at (location, "%qE does not name a type", id);
3512 /* If we're in a template class, it's possible that the user was
3513 referring to a type from a base class. For example:
3515 template <typename T> struct A { typedef T X; };
3516 template <typename T> struct B : public A<T> { X x; };
3518 The user should have said "typename A<T>::X". */
3519 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3520 inform (location, "C++11 %<constexpr%> only available with "
3521 "%<-std=c++11%> or %<-std=gnu++11%>");
3522 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3523 inform (location, "C++11 %<noexcept%> only available with "
3524 "%<-std=c++11%> or %<-std=gnu++11%>");
3525 else if (TREE_CODE (id) == IDENTIFIER_NODE
3526 && (id_equal (id, "module") || id_equal (id, "import")))
3528 if (modules_p ())
3529 inform (location, "%qE is not recognized as a module control-line",
3530 id);
3531 else if (cxx_dialect < cxx20)
3532 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3533 id);
3534 else
3535 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3536 ", which is not yet enabled with %<-std=c++20%>", id);
3538 else if (cxx_dialect < cxx11
3539 && TREE_CODE (id) == IDENTIFIER_NODE
3540 && id_equal (id, "thread_local"))
3541 inform (location, "C++11 %<thread_local%> only available with "
3542 "%<-std=c++11%> or %<-std=gnu++11%>");
3543 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3544 inform (location, "C++20 %<constinit%> only available with "
3545 "%<-std=c++20%> or %<-std=gnu++20%>");
3546 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3547 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3548 "%<-fconcepts%>");
3549 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3550 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3551 "%<-fconcepts%>");
3552 else if (processing_template_decl && current_class_type
3553 && TYPE_BINFO (current_class_type))
3555 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3556 b; b = TREE_CHAIN (b))
3558 tree base_type = BINFO_TYPE (b);
3559 if (CLASS_TYPE_P (base_type)
3560 && dependent_type_p (base_type))
3562 /* Go from a particular instantiation of the
3563 template (which will have an empty TYPE_FIELDs),
3564 to the main version. */
3565 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3566 for (tree field = TYPE_FIELDS (base_type);
3567 field; field = DECL_CHAIN (field))
3568 if (TREE_CODE (field) == TYPE_DECL
3569 && DECL_NAME (field) == id)
3571 inform (location,
3572 "(perhaps %<typename %T::%E%> was intended)",
3573 BINFO_TYPE (b), id);
3574 goto found;
3578 found:;
3581 /* Here we diagnose qualified-ids where the scope is actually correct,
3582 but the identifier does not resolve to a valid type name. */
3583 else if (parser->scope != error_mark_node)
3585 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3587 auto_diagnostic_group d;
3588 name_hint hint;
3589 if (decl == error_mark_node)
3590 hint = suggest_alternative_in_explicit_scope (location, id,
3591 parser->scope);
3592 const char *suggestion = hint.suggestion ();
3593 gcc_rich_location richloc (location_of (id));
3594 if (suggestion)
3595 richloc.add_fixit_replace (suggestion);
3596 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3598 if (suggestion)
3599 error_at (&richloc,
3600 "%qE in namespace %qE does not name a template"
3601 " type; did you mean %qs?",
3602 id, parser->scope, suggestion);
3603 else
3604 error_at (&richloc,
3605 "%qE in namespace %qE does not name a template type",
3606 id, parser->scope);
3608 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3610 if (suggestion)
3611 error_at (&richloc,
3612 "%qE in namespace %qE does not name a template"
3613 " type; did you mean %qs?",
3614 TREE_OPERAND (id, 0), parser->scope, suggestion);
3615 else
3616 error_at (&richloc,
3617 "%qE in namespace %qE does not name a template"
3618 " type",
3619 TREE_OPERAND (id, 0), parser->scope);
3621 else
3623 if (suggestion)
3624 error_at (&richloc,
3625 "%qE in namespace %qE does not name a type"
3626 "; did you mean %qs?",
3627 id, parser->scope, suggestion);
3628 else
3629 error_at (&richloc,
3630 "%qE in namespace %qE does not name a type",
3631 id, parser->scope);
3633 if (DECL_P (decl))
3634 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3636 else if (CLASS_TYPE_P (parser->scope)
3637 && constructor_name_p (id, parser->scope))
3639 /* A<T>::A<T>() */
3640 auto_diagnostic_group d;
3641 error_at (location, "%<%T::%E%> names the constructor, not"
3642 " the type", parser->scope, id);
3643 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3644 error_at (location, "and %qT has no template constructors",
3645 parser->scope);
3647 else if (TYPE_P (parser->scope)
3648 && dependent_scope_p (parser->scope))
3650 gcc_rich_location richloc (location);
3651 richloc.add_fixit_insert_before ("typename ");
3652 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3653 error_at (&richloc,
3654 "need %<typename%> before %<%T::%D::%E%> because "
3655 "%<%T::%D%> is a dependent scope",
3656 TYPE_CONTEXT (parser->scope),
3657 TYPENAME_TYPE_FULLNAME (parser->scope),
3659 TYPE_CONTEXT (parser->scope),
3660 TYPENAME_TYPE_FULLNAME (parser->scope));
3661 else
3662 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3663 "%qT is a dependent scope",
3664 parser->scope, id, parser->scope);
3666 else if (TYPE_P (parser->scope))
3668 auto_diagnostic_group d;
3669 if (!COMPLETE_TYPE_P (parser->scope))
3670 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3671 parser->scope);
3672 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3673 error_at (location_of (id),
3674 "%qE in %q#T does not name a template type",
3675 id, parser->scope);
3676 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3677 error_at (location_of (id),
3678 "%qE in %q#T does not name a template type",
3679 TREE_OPERAND (id, 0), parser->scope);
3680 else
3681 error_at (location_of (id),
3682 "%qE in %q#T does not name a type",
3683 id, parser->scope);
3684 if (DECL_P (decl))
3685 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3687 else
3688 gcc_unreachable ();
3692 /* Check for a common situation where a type-name should be present,
3693 but is not, and issue a sensible error message. Returns true if an
3694 invalid type-name was detected.
3696 The situation handled by this function are variable declarations of the
3697 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3698 Usually, `ID' should name a type, but if we got here it means that it
3699 does not. We try to emit the best possible error message depending on
3700 how exactly the id-expression looks like. */
3702 static bool
3703 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3705 tree id;
3706 cp_token *token = cp_lexer_peek_token (parser->lexer);
3708 /* Avoid duplicate error about ambiguous lookup. */
3709 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3711 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3712 if (next->type == CPP_NAME && next->error_reported)
3713 goto out;
3716 cp_parser_parse_tentatively (parser);
3717 id = cp_parser_id_expression (parser,
3718 /*template_keyword_p=*/false,
3719 /*check_dependency_p=*/true,
3720 /*template_p=*/NULL,
3721 /*declarator_p=*/true,
3722 /*optional_p=*/false);
3723 /* If the next token is a (, this is a function with no explicit return
3724 type, i.e. constructor, destructor or conversion op. */
3725 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3726 || TREE_CODE (id) == TYPE_DECL)
3728 cp_parser_abort_tentative_parse (parser);
3729 return false;
3731 if (!cp_parser_parse_definitely (parser))
3732 return false;
3734 /* Emit a diagnostic for the invalid type. */
3735 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3736 out:
3737 /* If we aren't in the middle of a declarator (i.e. in a
3738 parameter-declaration-clause), skip to the end of the declaration;
3739 there's no point in trying to process it. */
3740 if (!parser->in_declarator_p)
3741 cp_parser_skip_to_end_of_block_or_statement (parser);
3742 return true;
3745 /* Consume tokens up to, and including, the next non-nested closing `)'.
3746 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3747 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3748 found an unnested token of that type. */
3750 static int
3751 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3752 bool recovering,
3753 cpp_ttype or_ttype,
3754 bool consume_paren)
3756 unsigned paren_depth = 0;
3757 unsigned brace_depth = 0;
3758 unsigned square_depth = 0;
3759 unsigned condop_depth = 0;
3761 if (recovering && or_ttype == CPP_EOF
3762 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3763 return 0;
3765 while (true)
3767 cp_token * token = cp_lexer_peek_token (parser->lexer);
3769 /* Have we found what we're looking for before the closing paren? */
3770 if (token->type == or_ttype && or_ttype != CPP_EOF
3771 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3772 return -1;
3774 switch (token->type)
3776 case CPP_PRAGMA_EOL:
3777 if (!parser->lexer->in_pragma)
3778 break;
3779 /* FALLTHRU */
3780 case CPP_EOF:
3781 /* If we've run out of tokens, then there is no closing `)'. */
3782 return 0;
3784 /* This is good for lambda expression capture-lists. */
3785 case CPP_OPEN_SQUARE:
3786 ++square_depth;
3787 break;
3788 case CPP_CLOSE_SQUARE:
3789 if (!square_depth--)
3790 return 0;
3791 break;
3793 case CPP_SEMICOLON:
3794 /* This matches the processing in skip_to_end_of_statement. */
3795 if (!brace_depth)
3796 return 0;
3797 break;
3799 case CPP_OPEN_BRACE:
3800 ++brace_depth;
3801 break;
3802 case CPP_CLOSE_BRACE:
3803 if (!brace_depth--)
3804 return 0;
3805 break;
3807 case CPP_OPEN_PAREN:
3808 if (!brace_depth)
3809 ++paren_depth;
3810 break;
3812 case CPP_CLOSE_PAREN:
3813 if (!brace_depth && !paren_depth--)
3815 if (consume_paren)
3816 cp_lexer_consume_token (parser->lexer);
3817 return 1;
3819 break;
3821 case CPP_QUERY:
3822 if (!brace_depth && !paren_depth && !square_depth)
3823 ++condop_depth;
3824 break;
3826 case CPP_COLON:
3827 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3828 condop_depth--;
3829 break;
3831 case CPP_KEYWORD:
3832 if (token->keyword != RID__EXPORT
3833 && token->keyword != RID__MODULE
3834 && token->keyword != RID__IMPORT)
3835 break;
3836 /* FALLTHROUGH */
3838 case CPP_PRAGMA:
3839 /* We fell into a pragma. Skip it, and continue. */
3840 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3841 continue;
3843 default:
3844 break;
3847 /* Consume the token. */
3848 cp_lexer_consume_token (parser->lexer);
3852 /* Consume tokens up to, and including, the next non-nested closing `)'.
3853 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3854 are doing error recovery. Returns -1 if OR_COMMA is true and we
3855 found an unnested token of that type. */
3857 static int
3858 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3859 bool recovering,
3860 bool or_comma,
3861 bool consume_paren)
3863 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3864 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3865 ttype, consume_paren);
3868 /* Consume tokens until we reach the end of the current statement.
3869 Normally, that will be just before consuming a `;'. However, if a
3870 non-nested `}' comes first, then we stop before consuming that. */
3872 static void
3873 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3875 unsigned nesting_depth = 0;
3877 /* Unwind generic function template scope if necessary. */
3878 if (parser->fully_implicit_function_template_p)
3879 abort_fully_implicit_template (parser);
3881 while (true)
3883 cp_token *token = cp_lexer_peek_token (parser->lexer);
3885 switch (token->type)
3887 case CPP_PRAGMA_EOL:
3888 if (!parser->lexer->in_pragma)
3889 break;
3890 /* FALLTHRU */
3891 case CPP_EOF:
3892 /* If we've run out of tokens, stop. */
3893 return;
3895 case CPP_SEMICOLON:
3896 /* If the next token is a `;', we have reached the end of the
3897 statement. */
3898 if (!nesting_depth)
3899 return;
3900 break;
3902 case CPP_CLOSE_BRACE:
3903 /* If this is a non-nested '}', stop before consuming it.
3904 That way, when confronted with something like:
3906 { 3 + }
3908 we stop before consuming the closing '}', even though we
3909 have not yet reached a `;'. */
3910 if (nesting_depth == 0)
3911 return;
3913 /* If it is the closing '}' for a block that we have
3914 scanned, stop -- but only after consuming the token.
3915 That way given:
3917 void f g () { ... }
3918 typedef int I;
3920 we will stop after the body of the erroneously declared
3921 function, but before consuming the following `typedef'
3922 declaration. */
3923 if (--nesting_depth == 0)
3925 cp_lexer_consume_token (parser->lexer);
3926 return;
3928 break;
3930 case CPP_OPEN_BRACE:
3931 ++nesting_depth;
3932 break;
3934 case CPP_KEYWORD:
3935 if (token->keyword != RID__EXPORT
3936 && token->keyword != RID__MODULE
3937 && token->keyword != RID__IMPORT)
3938 break;
3939 /* FALLTHROUGH */
3941 case CPP_PRAGMA:
3942 /* We fell into a pragma. Skip it, and continue or return. */
3943 cp_parser_skip_to_pragma_eol (parser, token);
3944 if (!nesting_depth)
3945 return;
3946 continue;
3948 default:
3949 break;
3952 /* Consume the token. */
3953 cp_lexer_consume_token (parser->lexer);
3957 /* This function is called at the end of a statement or declaration.
3958 If the next token is a semicolon, it is consumed; otherwise, error
3959 recovery is attempted. */
3961 static void
3962 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3964 /* Look for the trailing `;'. */
3965 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3967 /* If there is additional (erroneous) input, skip to the end of
3968 the statement. */
3969 cp_parser_skip_to_end_of_statement (parser);
3970 /* If the next token is now a `;', consume it. */
3971 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3972 cp_lexer_consume_token (parser->lexer);
3976 /* Skip tokens until we have consumed an entire block, or until we
3977 have consumed a non-nested `;'. */
3979 static void
3980 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3982 int nesting_depth = 0;
3984 /* Unwind generic function template scope if necessary. */
3985 if (parser->fully_implicit_function_template_p)
3986 abort_fully_implicit_template (parser);
3988 while (nesting_depth >= 0)
3990 cp_token *token = cp_lexer_peek_token (parser->lexer);
3992 switch (token->type)
3994 case CPP_PRAGMA_EOL:
3995 if (!parser->lexer->in_pragma)
3996 break;
3997 /* FALLTHRU */
3998 case CPP_EOF:
3999 /* If we've run out of tokens, stop. */
4000 return;
4002 case CPP_SEMICOLON:
4003 /* Stop if this is an unnested ';'. */
4004 if (!nesting_depth)
4005 nesting_depth = -1;
4006 break;
4008 case CPP_CLOSE_BRACE:
4009 /* Stop if this is an unnested '}', or closes the outermost
4010 nesting level. */
4011 nesting_depth--;
4012 if (nesting_depth < 0)
4013 return;
4014 if (!nesting_depth)
4015 nesting_depth = -1;
4016 break;
4018 case CPP_OPEN_BRACE:
4019 /* Nest. */
4020 nesting_depth++;
4021 break;
4023 case CPP_KEYWORD:
4024 if (token->keyword != RID__EXPORT
4025 && token->keyword != RID__MODULE
4026 && token->keyword != RID__IMPORT)
4027 break;
4028 /* FALLTHROUGH */
4030 case CPP_PRAGMA:
4031 /* Skip it, and continue or return. */
4032 cp_parser_skip_to_pragma_eol (parser, token);
4033 if (!nesting_depth)
4034 return;
4035 continue;
4037 default:
4038 break;
4041 /* Consume the token. */
4042 cp_lexer_consume_token (parser->lexer);
4046 /* Skip tokens until a non-nested closing curly brace is the next
4047 token, or there are no more tokens. Return true in the first case,
4048 false otherwise. */
4050 static bool
4051 cp_parser_skip_to_closing_brace (cp_parser *parser)
4053 unsigned nesting_depth = 0;
4055 while (true)
4057 cp_token *token = cp_lexer_peek_token (parser->lexer);
4059 switch (token->type)
4061 case CPP_PRAGMA_EOL:
4062 if (!parser->lexer->in_pragma)
4063 break;
4064 /* FALLTHRU */
4065 case CPP_EOF:
4066 /* If we've run out of tokens, stop. */
4067 return false;
4069 case CPP_CLOSE_BRACE:
4070 /* If the next token is a non-nested `}', then we have reached
4071 the end of the current block. */
4072 if (nesting_depth-- == 0)
4073 return true;
4074 break;
4076 case CPP_OPEN_BRACE:
4077 /* If it the next token is a `{', then we are entering a new
4078 block. Consume the entire block. */
4079 ++nesting_depth;
4080 break;
4082 default:
4083 break;
4086 /* Consume the token. */
4087 cp_lexer_consume_token (parser->lexer);
4091 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4092 parameter is the PRAGMA token, allowing us to purge the entire pragma
4093 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4094 forwards (not error recovery). */
4096 static void
4097 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4099 cp_token *token;
4103 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4104 before an EOF token, even when the EOF is on the pragma line.
4105 We should never get here without being inside a deferred
4106 pragma. */
4107 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4108 token = cp_lexer_consume_token (parser->lexer);
4110 while (token->type != CPP_PRAGMA_EOL);
4112 if (pragma_tok)
4114 parser->lexer->in_pragma = false;
4115 if (parser->lexer->in_omp_attribute_pragma
4116 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4118 parser->lexer = parser->lexer->next;
4119 /* Put the current source position back where it was before this
4120 lexer was pushed. */
4121 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4126 /* Require pragma end of line, resyncing with it as necessary. The
4127 arguments are as for cp_parser_skip_to_pragma_eol. */
4129 static void
4130 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4132 parser->lexer->in_pragma = false;
4133 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4134 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4135 else if (parser->lexer->in_omp_attribute_pragma
4136 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4138 parser->lexer = parser->lexer->next;
4139 /* Put the current source position back where it was before this
4140 lexer was pushed. */
4141 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4145 /* This is a simple wrapper around make_typename_type. When the id is
4146 an unresolved identifier node, we can provide a superior diagnostic
4147 using cp_parser_diagnose_invalid_type_name. */
4149 static tree
4150 cp_parser_make_typename_type (cp_parser *parser, tree id,
4151 location_t id_location)
4153 tree result;
4154 if (identifier_p (id))
4156 result = make_typename_type (parser->scope, id, typename_type,
4157 /*complain=*/tf_none);
4158 if (result == error_mark_node)
4159 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4160 return result;
4162 return make_typename_type (parser->scope, id, typename_type, tf_error);
4165 /* This is a wrapper around the
4166 make_{pointer,ptrmem,reference}_declarator functions that decides
4167 which one to call based on the CODE and CLASS_TYPE arguments. The
4168 CODE argument should be one of the values returned by
4169 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4170 appertain to the pointer or reference. */
4172 static cp_declarator *
4173 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4174 cp_cv_quals cv_qualifiers,
4175 cp_declarator *target,
4176 tree attributes)
4178 if (code == ERROR_MARK || target == cp_error_declarator)
4179 return cp_error_declarator;
4181 if (code == INDIRECT_REF)
4182 if (class_type == NULL_TREE)
4183 return make_pointer_declarator (cv_qualifiers, target, attributes);
4184 else
4185 return make_ptrmem_declarator (cv_qualifiers, class_type,
4186 target, attributes);
4187 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4188 return make_reference_declarator (cv_qualifiers, target,
4189 false, attributes);
4190 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4191 return make_reference_declarator (cv_qualifiers, target,
4192 true, attributes);
4193 gcc_unreachable ();
4196 /* Create a new C++ parser. */
4198 static cp_parser *
4199 cp_parser_new (cp_lexer *lexer)
4201 /* Initialize the binops_by_token so that we can get the tree
4202 directly from the token. */
4203 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4204 binops_by_token[binops[i].token_type] = binops[i];
4206 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4207 parser->lexer = lexer;
4208 parser->context = cp_parser_context_new (NULL);
4210 /* For now, we always accept GNU extensions. */
4211 parser->allow_gnu_extensions_p = 1;
4213 /* The `>' token is a greater-than operator, not the end of a
4214 template-id. */
4215 parser->greater_than_is_operator_p = true;
4217 parser->default_arg_ok_p = true;
4219 /* We are not parsing a constant-expression. */
4220 parser->integral_constant_expression_p = false;
4221 parser->allow_non_integral_constant_expression_p = false;
4222 parser->non_integral_constant_expression_p = false;
4224 /* Local variable names are not forbidden. */
4225 parser->local_variables_forbidden_p = 0;
4227 /* We are not processing an `extern "C"' declaration. */
4228 parser->in_unbraced_linkage_specification_p = false;
4230 /* We are not processing a declarator. */
4231 parser->in_declarator_p = false;
4233 /* We are not processing a template-argument-list. */
4234 parser->in_template_argument_list_p = false;
4236 /* We are not in an iteration statement. */
4237 parser->in_statement = 0;
4239 /* We are not in a switch statement. */
4240 parser->in_switch_statement_p = false;
4242 /* We are not parsing a type-id inside an expression. */
4243 parser->in_type_id_in_expr_p = false;
4245 /* String literals should be translated to the execution character set. */
4246 parser->translate_strings_p = true;
4248 /* We are not parsing a function body. */
4249 parser->in_function_body = false;
4251 /* We can correct until told otherwise. */
4252 parser->colon_corrects_to_scope_p = true;
4254 /* The unparsed function queue is empty. */
4255 push_unparsed_function_queues (parser);
4257 /* There are no classes being defined. */
4258 parser->num_classes_being_defined = 0;
4260 /* No template parameters apply. */
4261 parser->num_template_parameter_lists = 0;
4263 /* Special parsing data structures. */
4264 parser->omp_declare_simd = NULL;
4265 parser->oacc_routine = NULL;
4267 /* Not declaring an implicit function template. */
4268 parser->auto_is_implicit_function_template_parm_p = false;
4269 parser->fully_implicit_function_template_p = false;
4270 parser->implicit_template_parms = 0;
4271 parser->implicit_template_scope = 0;
4273 /* Allow constrained-type-specifiers. */
4274 parser->prevent_constrained_type_specifiers = 0;
4276 /* We haven't yet seen an 'extern "C"'. */
4277 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4279 return parser;
4282 /* Create a cp_lexer structure which will emit the tokens in CACHE
4283 and push it onto the parser's lexer stack. This is used for delayed
4284 parsing of in-class method bodies and default arguments, and should
4285 not be confused with tentative parsing. */
4286 static void
4287 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4289 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4290 lexer->next = parser->lexer;
4291 parser->lexer = lexer;
4293 /* Move the current source position to that of the first token in the
4294 new lexer. */
4295 cp_lexer_set_source_position_from_token (lexer->next_token);
4298 /* Pop the top lexer off the parser stack. This is never used for the
4299 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4300 static void
4301 cp_parser_pop_lexer (cp_parser *parser)
4303 cp_lexer *lexer = parser->lexer;
4304 parser->lexer = lexer->next;
4305 cp_lexer_destroy (lexer);
4307 /* Put the current source position back where it was before this
4308 lexer was pushed. */
4309 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4312 /* Lexical conventions [gram.lex] */
4314 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4315 identifier. */
4317 static cp_expr
4318 cp_parser_identifier (cp_parser* parser)
4320 cp_token *token;
4322 /* Look for the identifier. */
4323 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4324 /* Return the value. */
4325 if (token)
4326 return cp_expr (token->u.value, token->location);
4327 else
4328 return error_mark_node;
4331 /* Parse a sequence of adjacent string constants. Returns a
4332 TREE_STRING representing the combined, nul-terminated string
4333 constant. If TRANSLATE is true, translate the string to the
4334 execution character set. If WIDE_OK is true, a wide string is
4335 invalid here.
4337 C++98 [lex.string] says that if a narrow string literal token is
4338 adjacent to a wide string literal token, the behavior is undefined.
4339 However, C99 6.4.5p4 says that this results in a wide string literal.
4340 We follow C99 here, for consistency with the C front end.
4342 This code is largely lifted from lex_string() in c-lex.cc.
4344 FUTURE: ObjC++ will need to handle @-strings here. */
4345 static cp_expr
4346 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4347 bool lookup_udlit = true)
4349 tree value;
4350 size_t count;
4351 struct obstack str_ob;
4352 struct obstack loc_ob;
4353 cpp_string str, istr, *strs;
4354 cp_token *tok;
4355 enum cpp_ttype type, curr_type;
4356 int have_suffix_p = 0;
4357 tree string_tree;
4358 tree suffix_id = NULL_TREE;
4359 bool curr_tok_is_userdef_p = false;
4361 tok = cp_lexer_peek_token (parser->lexer);
4362 if (!cp_parser_is_string_literal (tok))
4364 cp_parser_error (parser, "expected string-literal");
4365 return error_mark_node;
4368 location_t loc = tok->location;
4370 if (cpp_userdef_string_p (tok->type))
4372 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4373 curr_type = cpp_userdef_string_remove_type (tok->type);
4374 curr_tok_is_userdef_p = true;
4376 else
4378 string_tree = tok->u.value;
4379 curr_type = tok->type;
4381 type = curr_type;
4383 /* Try to avoid the overhead of creating and destroying an obstack
4384 for the common case of just one string. */
4385 if (!cp_parser_is_string_literal
4386 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4388 cp_lexer_consume_token (parser->lexer);
4390 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4391 str.len = TREE_STRING_LENGTH (string_tree);
4392 count = 1;
4394 if (curr_tok_is_userdef_p)
4396 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4397 have_suffix_p = 1;
4398 curr_type = cpp_userdef_string_remove_type (tok->type);
4400 else
4401 curr_type = tok->type;
4403 strs = &str;
4405 else
4407 location_t last_tok_loc = tok->location;
4408 gcc_obstack_init (&str_ob);
4409 gcc_obstack_init (&loc_ob);
4410 count = 0;
4414 cp_lexer_consume_token (parser->lexer);
4415 count++;
4416 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4417 str.len = TREE_STRING_LENGTH (string_tree);
4419 if (curr_tok_is_userdef_p)
4421 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4422 if (have_suffix_p == 0)
4424 suffix_id = curr_suffix_id;
4425 have_suffix_p = 1;
4427 else if (have_suffix_p == 1
4428 && curr_suffix_id != suffix_id)
4430 error ("inconsistent user-defined literal suffixes"
4431 " %qD and %qD in string literal",
4432 suffix_id, curr_suffix_id);
4433 have_suffix_p = -1;
4435 curr_type = cpp_userdef_string_remove_type (tok->type);
4437 else
4438 curr_type = tok->type;
4440 if (type != curr_type)
4442 if (type == CPP_STRING)
4443 type = curr_type;
4444 else if (curr_type != CPP_STRING)
4446 rich_location rich_loc (line_table, tok->location);
4447 rich_loc.add_range (last_tok_loc);
4448 error_at (&rich_loc,
4449 "concatenation of string literals with "
4450 "conflicting encoding prefixes");
4454 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4455 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4457 last_tok_loc = tok->location;
4459 tok = cp_lexer_peek_token (parser->lexer);
4460 if (cpp_userdef_string_p (tok->type))
4462 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4463 curr_type = cpp_userdef_string_remove_type (tok->type);
4464 curr_tok_is_userdef_p = true;
4466 else
4468 string_tree = tok->u.value;
4469 curr_type = tok->type;
4470 curr_tok_is_userdef_p = false;
4473 while (cp_parser_is_string_literal (tok));
4475 /* A string literal built by concatenation has its caret=start at
4476 the start of the initial string, and its finish at the finish of
4477 the final string literal. */
4478 loc = make_location (loc, loc, get_finish (last_tok_loc));
4480 strs = (cpp_string *) obstack_finish (&str_ob);
4483 if (type != CPP_STRING && !wide_ok)
4485 cp_parser_error (parser, "a wide string is invalid in this context");
4486 type = CPP_STRING;
4489 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4490 (parse_in, strs, count, &istr, type))
4492 value = build_string (istr.len, (const char *)istr.text);
4493 free (CONST_CAST (unsigned char *, istr.text));
4494 if (count > 1)
4496 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4497 gcc_assert (g_string_concat_db);
4498 g_string_concat_db->record_string_concatenation (count, locs);
4501 switch (type)
4503 default:
4504 case CPP_STRING:
4505 TREE_TYPE (value) = char_array_type_node;
4506 break;
4507 case CPP_UTF8STRING:
4508 if (flag_char8_t)
4509 TREE_TYPE (value) = char8_array_type_node;
4510 else
4511 TREE_TYPE (value) = char_array_type_node;
4512 break;
4513 case CPP_STRING16:
4514 TREE_TYPE (value) = char16_array_type_node;
4515 break;
4516 case CPP_STRING32:
4517 TREE_TYPE (value) = char32_array_type_node;
4518 break;
4519 case CPP_WSTRING:
4520 TREE_TYPE (value) = wchar_array_type_node;
4521 break;
4524 value = fix_string_type (value);
4526 if (have_suffix_p)
4528 tree literal = build_userdef_literal (suffix_id, value,
4529 OT_NONE, NULL_TREE);
4530 if (lookup_udlit)
4531 value = cp_parser_userdef_string_literal (literal);
4532 else
4533 value = literal;
4536 else
4537 /* cpp_interpret_string has issued an error. */
4538 value = error_mark_node;
4540 if (count > 1)
4542 obstack_free (&str_ob, 0);
4543 obstack_free (&loc_ob, 0);
4546 return cp_expr (value, loc);
4549 /* Look up a literal operator with the name and the exact arguments. */
4551 static tree
4552 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4554 tree decl = lookup_name (name);
4555 if (!decl || !is_overloaded_fn (decl))
4556 return error_mark_node;
4558 for (lkp_iterator iter (decl); iter; ++iter)
4560 tree fn = *iter;
4562 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4564 unsigned int ix;
4565 bool found = true;
4567 for (ix = 0;
4568 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4569 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4571 tree tparm = TREE_VALUE (parmtypes);
4572 tree targ = TREE_TYPE ((*args)[ix]);
4573 bool ptr = TYPE_PTR_P (tparm);
4574 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4575 if ((ptr || arr || !same_type_p (tparm, targ))
4576 && (!ptr || !arr
4577 || !same_type_p (TREE_TYPE (tparm),
4578 TREE_TYPE (targ))))
4579 found = false;
4582 if (found
4583 && ix == vec_safe_length (args)
4584 /* May be this should be sufficient_parms_p instead,
4585 depending on how exactly should user-defined literals
4586 work in presence of default arguments on the literal
4587 operator parameters. */
4588 && parmtypes == void_list_node)
4589 return decl;
4593 return error_mark_node;
4596 /* Parse a user-defined char constant. Returns a call to a user-defined
4597 literal operator taking the character as an argument. */
4599 static cp_expr
4600 cp_parser_userdef_char_literal (cp_parser *parser)
4602 cp_token *token = cp_lexer_consume_token (parser->lexer);
4603 tree literal = token->u.value;
4604 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4605 tree value = USERDEF_LITERAL_VALUE (literal);
4606 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4607 tree decl, result;
4609 /* Build up a call to the user-defined operator */
4610 /* Lookup the name we got back from the id-expression. */
4611 releasing_vec args;
4612 vec_safe_push (args, value);
4613 decl = lookup_literal_operator (name, args);
4614 if (!decl || decl == error_mark_node)
4616 error ("unable to find character literal operator %qD with %qT argument",
4617 name, TREE_TYPE (value));
4618 return error_mark_node;
4620 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4621 return result;
4624 /* A subroutine of cp_parser_userdef_numeric_literal to
4625 create a char... template parameter pack from a string node. */
4627 static tree
4628 make_char_string_pack (tree value)
4630 tree charvec;
4631 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4632 const unsigned char *str
4633 = (const unsigned char *) TREE_STRING_POINTER (value);
4634 int i, len = TREE_STRING_LENGTH (value) - 1;
4635 tree argvec = make_tree_vec (1);
4637 /* Fill in CHARVEC with all of the parameters. */
4638 charvec = make_tree_vec (len);
4639 for (i = 0; i < len; ++i)
4641 unsigned char s[3] = { '\'', str[i], '\'' };
4642 cpp_string in = { 3, s };
4643 cpp_string out = { 0, 0 };
4644 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4645 return NULL_TREE;
4646 gcc_assert (out.len == 2);
4647 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4648 out.text[0]);
4651 /* Build the argument packs. */
4652 ARGUMENT_PACK_ARGS (argpack) = charvec;
4654 TREE_VEC_ELT (argvec, 0) = argpack;
4656 return argvec;
4659 /* A subroutine of cp_parser_userdef_numeric_literal to
4660 create a char... template parameter pack from a string node. */
4662 static tree
4663 make_string_pack (tree value)
4665 tree charvec;
4666 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4667 const unsigned char *str
4668 = (const unsigned char *) TREE_STRING_POINTER (value);
4669 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4670 int len = TREE_STRING_LENGTH (value) / sz - 1;
4671 tree argvec = make_tree_vec (2);
4673 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4674 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4676 /* First template parm is character type. */
4677 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4679 /* Fill in CHARVEC with all of the parameters. */
4680 charvec = make_tree_vec (len);
4681 for (int i = 0; i < len; ++i)
4682 TREE_VEC_ELT (charvec, i)
4683 = double_int_to_tree (str_char_type_node,
4684 double_int::from_buffer (str + i * sz, sz));
4686 /* Build the argument packs. */
4687 ARGUMENT_PACK_ARGS (argpack) = charvec;
4689 TREE_VEC_ELT (argvec, 1) = argpack;
4691 return argvec;
4694 /* Parse a user-defined numeric constant. returns a call to a user-defined
4695 literal operator. */
4697 static cp_expr
4698 cp_parser_userdef_numeric_literal (cp_parser *parser)
4700 cp_token *token = cp_lexer_consume_token (parser->lexer);
4701 tree literal = token->u.value;
4702 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4703 tree value = USERDEF_LITERAL_VALUE (literal);
4704 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4705 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4706 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4707 tree decl, result;
4709 /* Look for a literal operator taking the exact type of numeric argument
4710 as the literal value. */
4711 releasing_vec args;
4712 vec_safe_push (args, value);
4713 decl = lookup_literal_operator (name, args);
4714 if (decl && decl != error_mark_node)
4716 result = finish_call_expr (decl, &args, false, true,
4717 tf_warning_or_error);
4719 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4721 warning_at (token->location, OPT_Woverflow,
4722 "integer literal exceeds range of %qT type",
4723 long_long_unsigned_type_node);
4725 else
4727 if (overflow > 0)
4728 warning_at (token->location, OPT_Woverflow,
4729 "floating literal exceeds range of %qT type",
4730 long_double_type_node);
4731 else if (overflow < 0)
4732 warning_at (token->location, OPT_Woverflow,
4733 "floating literal truncated to zero");
4736 return result;
4739 /* If the numeric argument didn't work, look for a raw literal
4740 operator taking a const char* argument consisting of the number
4741 in string format. */
4742 args->truncate (0);
4743 vec_safe_push (args, num_string);
4744 decl = lookup_literal_operator (name, args);
4745 if (decl && decl != error_mark_node)
4747 result = finish_call_expr (decl, &args, false, true,
4748 tf_warning_or_error);
4749 return result;
4752 /* If the raw literal didn't work, look for a non-type template
4753 function with parameter pack char.... Call the function with
4754 template parameter characters representing the number. */
4755 args->truncate (0);
4756 decl = lookup_literal_operator (name, args);
4757 if (decl && decl != error_mark_node)
4759 tree tmpl_args = make_char_string_pack (num_string);
4760 if (tmpl_args == NULL_TREE)
4762 error ("failed to translate literal to execution character set %qT",
4763 num_string);
4764 return error_mark_node;
4766 decl = lookup_template_function (decl, tmpl_args);
4767 result = finish_call_expr (decl, &args, false, true,
4768 tf_warning_or_error);
4769 return result;
4772 /* In C++14 the standard library defines complex number suffixes that
4773 conflict with GNU extensions. Prefer them if <complex> is #included. */
4774 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4775 bool i14 = (cxx_dialect > cxx11
4776 && (id_equal (suffix_id, "i")
4777 || id_equal (suffix_id, "if")
4778 || id_equal (suffix_id, "il")));
4779 diagnostic_t kind = DK_ERROR;
4780 int opt = 0;
4782 if (i14 && ext)
4784 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4785 LOOK_want::NORMAL, false);
4786 if (cxlit == error_mark_node)
4788 /* No <complex>, so pedwarn and use GNU semantics. */
4789 kind = DK_PEDWARN;
4790 opt = OPT_Wpedantic;
4794 bool complained
4795 = emit_diagnostic (kind, input_location, opt,
4796 "unable to find numeric literal operator %qD", name);
4798 if (!complained)
4799 /* Don't inform either. */;
4800 else if (i14)
4802 inform (token->location, "add %<using namespace std::complex_literals%> "
4803 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4804 "suffixes");
4805 if (ext)
4806 inform (token->location, "or use %<j%> instead of %<i%> for the "
4807 "GNU built-in suffix");
4809 else if (!ext)
4810 inform (token->location, "use %<-fext-numeric-literals%> "
4811 "to enable more built-in suffixes");
4813 if (kind == DK_ERROR)
4814 value = error_mark_node;
4815 else
4817 /* Use the built-in semantics. */
4818 tree type;
4819 if (id_equal (suffix_id, "i"))
4821 if (TREE_CODE (value) == INTEGER_CST)
4822 type = integer_type_node;
4823 else
4824 type = double_type_node;
4826 else if (id_equal (suffix_id, "if"))
4827 type = float_type_node;
4828 else /* if (id_equal (suffix_id, "il")) */
4829 type = long_double_type_node;
4831 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
4832 build_zero_cst (type), fold_convert (type, value));
4835 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4836 /* Avoid repeated diagnostics. */
4837 token->u.value = value;
4838 return value;
4841 /* Parse a user-defined string constant. Returns a call to a user-defined
4842 literal operator taking a character pointer and the length of the string
4843 as arguments. */
4845 static tree
4846 cp_parser_userdef_string_literal (tree literal)
4848 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4849 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4850 tree value = USERDEF_LITERAL_VALUE (literal);
4851 int len = TREE_STRING_LENGTH (value)
4852 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4853 tree decl;
4855 /* Build up a call to the user-defined operator. */
4856 /* Lookup the name we got back from the id-expression. */
4857 releasing_vec args;
4858 vec_safe_push (args, value);
4859 vec_safe_push (args, build_int_cst (size_type_node, len));
4860 decl = lookup_literal_operator (name, args);
4862 if (decl && decl != error_mark_node)
4863 return finish_call_expr (decl, &args, false, true,
4864 tf_warning_or_error);
4866 /* Look for a suitable template function, either (C++20) with a single
4867 parameter of class type, or (N3599) with typename parameter CharT and
4868 parameter pack CharT... */
4869 args->truncate (0);
4870 decl = lookup_literal_operator (name, args);
4871 if (decl && decl != error_mark_node)
4873 /* Use resolve_nondeduced_context to try to choose one form of template
4874 or the other. */
4875 tree tmpl_args = make_tree_vec (1);
4876 TREE_VEC_ELT (tmpl_args, 0) = value;
4877 decl = lookup_template_function (decl, tmpl_args);
4878 tree res = resolve_nondeduced_context (decl, tf_none);
4879 if (DECL_P (res))
4880 decl = res;
4881 else
4883 TREE_OPERAND (decl, 1) = make_string_pack (value);
4884 res = resolve_nondeduced_context (decl, tf_none);
4885 if (DECL_P (res))
4886 decl = res;
4888 if (!DECL_P (decl) && cxx_dialect > cxx17)
4889 TREE_OPERAND (decl, 1) = tmpl_args;
4890 return finish_call_expr (decl, &args, false, true,
4891 tf_warning_or_error);
4894 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4895 name, TREE_TYPE (value), size_type_node);
4896 return error_mark_node;
4900 /* Basic concepts [gram.basic] */
4902 /* Parse a translation-unit.
4904 translation-unit:
4905 declaration-seq [opt] */
4907 static void
4908 cp_parser_translation_unit (cp_parser* parser)
4910 gcc_checking_assert (!cp_error_declarator);
4912 /* Create the declarator obstack. */
4913 gcc_obstack_init (&declarator_obstack);
4914 /* Create the error declarator. */
4915 cp_error_declarator = make_declarator (cdk_error);
4916 /* Create the empty parameter list. */
4917 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4918 UNKNOWN_LOCATION);
4919 /* Remember where the base of the declarator obstack lies. */
4920 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4922 push_deferring_access_checks (flag_access_control
4923 ? dk_no_deferred : dk_no_check);
4925 module_parse mp_state = MP_NOT_MODULE;
4926 if (modules_p () && !header_module_p ())
4927 mp_state = MP_FIRST;
4929 bool implicit_extern_c = false;
4931 /* Parse until EOF. */
4932 for (;;)
4934 cp_token *token = cp_lexer_peek_token (parser->lexer);
4936 /* If we're entering or exiting a region that's implicitly
4937 extern "C", modify the lang context appropriately. This is
4938 so horrible. Please die. */
4939 if (implicit_extern_c
4940 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4942 implicit_extern_c = !implicit_extern_c;
4943 if (implicit_extern_c)
4944 push_lang_context (lang_name_c);
4945 else
4946 pop_lang_context ();
4949 if (token->type == CPP_EOF)
4950 break;
4952 if (modules_p ())
4954 /* Top-level module declarations are ok, and change the
4955 portion of file we're in. Top-level import declarations
4956 are significant for the import portions. */
4958 cp_token *next = token;
4959 bool exporting = token->keyword == RID__EXPORT;
4960 if (exporting)
4962 cp_lexer_consume_token (parser->lexer);
4963 next = cp_lexer_peek_token (parser->lexer);
4965 if (next->keyword == RID__MODULE)
4967 mp_state
4968 = cp_parser_module_declaration (parser, mp_state, exporting);
4969 continue;
4971 else if (next->keyword == RID__IMPORT)
4973 if (mp_state == MP_FIRST)
4974 mp_state = MP_NOT_MODULE;
4975 cp_parser_import_declaration (parser, mp_state, exporting);
4976 continue;
4978 else
4979 gcc_checking_assert (!exporting);
4981 if (mp_state == MP_GLOBAL && token->main_source_p)
4983 static bool warned = false;
4984 if (!warned)
4986 warned = true;
4987 error_at (token->location,
4988 "global module fragment contents must be"
4989 " from preprocessor inclusion");
4994 /* This relies on the ordering of module_parse values. */
4995 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
4996 /* We're no longer in the import portion of a named module. */
4997 mp_state = module_parse (mp_state + 1);
4998 else if (mp_state == MP_FIRST)
4999 mp_state = MP_NOT_MODULE;
5001 if (token->type == CPP_CLOSE_BRACE)
5003 cp_parser_error (parser, "expected declaration");
5004 cp_lexer_consume_token (parser->lexer);
5005 /* If the next token is now a `;', consume it. */
5006 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5007 cp_lexer_consume_token (parser->lexer);
5009 else
5010 cp_parser_toplevel_declaration (parser);
5013 /* Get rid of the token array; we don't need it any more. */
5014 cp_lexer_destroy (parser->lexer);
5015 parser->lexer = NULL;
5017 /* The EOF should have reset this. */
5018 gcc_checking_assert (!implicit_extern_c);
5020 /* Make sure the declarator obstack was fully cleaned up. */
5021 gcc_assert (obstack_next_free (&declarator_obstack)
5022 == declarator_obstack_base);
5025 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5026 decltype context. */
5028 static inline tsubst_flags_t
5029 complain_flags (bool decltype_p)
5031 tsubst_flags_t complain = tf_warning_or_error;
5032 if (decltype_p)
5033 complain |= tf_decltype;
5034 return complain;
5037 /* We're about to parse a collection of statements. If we're currently
5038 parsing tentatively, set up a firewall so that any nested
5039 cp_parser_commit_to_tentative_parse won't affect the current context. */
5041 static cp_token_position
5042 cp_parser_start_tentative_firewall (cp_parser *parser)
5044 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5045 return 0;
5047 cp_parser_parse_tentatively (parser);
5048 cp_parser_commit_to_topmost_tentative_parse (parser);
5049 return cp_lexer_token_position (parser->lexer, false);
5052 /* We've finished parsing the collection of statements. Wrap up the
5053 firewall and replace the relevant tokens with the parsed form. */
5055 static void
5056 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5057 tree expr)
5059 if (!start)
5060 return;
5062 /* Finish the firewall level. */
5063 cp_parser_parse_definitely (parser);
5064 /* And remember the result of the parse for when we try again. */
5065 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5066 token->type = CPP_PREPARSED_EXPR;
5067 token->u.value = expr;
5068 token->keyword = RID_MAX;
5069 cp_lexer_purge_tokens_after (parser->lexer, start);
5072 /* Like the above functions, but let the user modify the tokens. Used by
5073 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5074 later parses, so it makes sense to localize the effects of
5075 cp_parser_commit_to_tentative_parse. */
5077 struct tentative_firewall
5079 cp_parser *parser;
5080 bool set;
5082 tentative_firewall (cp_parser *p): parser(p)
5084 /* If we're currently parsing tentatively, start a committed level as a
5085 firewall and then an inner tentative parse. */
5086 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5088 cp_parser_parse_tentatively (parser);
5089 cp_parser_commit_to_topmost_tentative_parse (parser);
5090 cp_parser_parse_tentatively (parser);
5094 ~tentative_firewall()
5096 if (set)
5098 /* Finish the inner tentative parse and the firewall, propagating any
5099 uncommitted error state to the outer tentative parse. */
5100 bool err = cp_parser_error_occurred (parser);
5101 cp_parser_parse_definitely (parser);
5102 cp_parser_parse_definitely (parser);
5103 if (err)
5104 cp_parser_simulate_error (parser);
5109 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5110 This class is for tracking such a matching pair of symbols.
5111 In particular, it tracks the location of the first token,
5112 so that if the second token is missing, we can highlight the
5113 location of the first token when notifying the user about the
5114 problem. */
5116 template <typename traits_t>
5117 class token_pair
5119 public:
5120 /* token_pair's ctor. */
5121 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5123 /* If the next token is the opening symbol for this pair, consume it and
5124 return true.
5125 Otherwise, issue an error and return false.
5126 In either case, record the location of the opening token. */
5128 bool require_open (cp_parser *parser)
5130 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5131 return cp_parser_require (parser, traits_t::open_token_type,
5132 traits_t::required_token_open);
5135 /* Consume the next token from PARSER, recording its location as
5136 that of the opening token within the pair. */
5138 cp_token * consume_open (cp_parser *parser)
5140 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5141 gcc_assert (tok->type == traits_t::open_token_type);
5142 m_open_loc = tok->location;
5143 return tok;
5146 /* If the next token is the closing symbol for this pair, consume it
5147 and return it.
5148 Otherwise, issue an error, highlighting the location of the
5149 corresponding opening token, and return NULL. */
5151 cp_token *require_close (cp_parser *parser) const
5153 return cp_parser_require (parser, traits_t::close_token_type,
5154 traits_t::required_token_close,
5155 m_open_loc);
5158 location_t open_location () const { return m_open_loc; }
5160 private:
5161 location_t m_open_loc;
5164 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5166 struct matching_paren_traits
5168 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5169 static const enum required_token required_token_open = RT_OPEN_PAREN;
5170 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5171 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5174 /* "matching_parens" is a token_pair<T> class for tracking matching
5175 pairs of parentheses. */
5177 typedef token_pair<matching_paren_traits> matching_parens;
5179 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5181 struct matching_brace_traits
5183 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5184 static const enum required_token required_token_open = RT_OPEN_BRACE;
5185 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5186 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5189 /* "matching_braces" is a token_pair<T> class for tracking matching
5190 pairs of braces. */
5192 typedef token_pair<matching_brace_traits> matching_braces;
5195 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5196 enclosing parentheses. */
5198 static cp_expr
5199 cp_parser_statement_expr (cp_parser *parser)
5201 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5203 /* Consume the '('. */
5204 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5205 matching_parens parens;
5206 parens.consume_open (parser);
5207 /* Start the statement-expression. */
5208 tree expr = begin_stmt_expr ();
5209 /* Parse the compound-statement. */
5210 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
5211 /* Finish up. */
5212 expr = finish_stmt_expr (expr, false);
5213 /* Consume the ')'. */
5214 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5215 if (!parens.require_close (parser))
5216 cp_parser_skip_to_end_of_statement (parser);
5218 cp_parser_end_tentative_firewall (parser, start, expr);
5219 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5220 return cp_expr (expr, combined_loc);
5223 /* Expressions [gram.expr] */
5225 /* Parse a fold-operator.
5227 fold-operator:
5228 - * / % ^ & | = < > << >>
5229 = -= *= /= %= ^= &= |= <<= >>=
5230 == != <= >= && || , .* ->*
5232 This returns the tree code corresponding to the matched operator
5233 as an int. When the current token matches a compound assignment
5234 operator, the resulting tree code is the negative value of the
5235 non-assignment operator. */
5237 static int
5238 cp_parser_fold_operator (cp_token *token)
5240 switch (token->type)
5242 case CPP_PLUS: return PLUS_EXPR;
5243 case CPP_MINUS: return MINUS_EXPR;
5244 case CPP_MULT: return MULT_EXPR;
5245 case CPP_DIV: return TRUNC_DIV_EXPR;
5246 case CPP_MOD: return TRUNC_MOD_EXPR;
5247 case CPP_XOR: return BIT_XOR_EXPR;
5248 case CPP_AND: return BIT_AND_EXPR;
5249 case CPP_OR: return BIT_IOR_EXPR;
5250 case CPP_LSHIFT: return LSHIFT_EXPR;
5251 case CPP_RSHIFT: return RSHIFT_EXPR;
5253 case CPP_EQ: return -NOP_EXPR;
5254 case CPP_PLUS_EQ: return -PLUS_EXPR;
5255 case CPP_MINUS_EQ: return -MINUS_EXPR;
5256 case CPP_MULT_EQ: return -MULT_EXPR;
5257 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5258 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5259 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5260 case CPP_AND_EQ: return -BIT_AND_EXPR;
5261 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5262 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5263 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5265 case CPP_EQ_EQ: return EQ_EXPR;
5266 case CPP_NOT_EQ: return NE_EXPR;
5267 case CPP_LESS: return LT_EXPR;
5268 case CPP_GREATER: return GT_EXPR;
5269 case CPP_LESS_EQ: return LE_EXPR;
5270 case CPP_GREATER_EQ: return GE_EXPR;
5272 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5273 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5275 case CPP_COMMA: return COMPOUND_EXPR;
5277 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5278 case CPP_DEREF_STAR: return MEMBER_REF;
5280 default: return ERROR_MARK;
5284 /* Returns true if CODE indicates a binary expression, which is not allowed in
5285 the LHS of a fold-expression. More codes will need to be added to use this
5286 function in other contexts. */
5288 static bool
5289 is_binary_op (tree_code code)
5291 switch (code)
5293 case PLUS_EXPR:
5294 case POINTER_PLUS_EXPR:
5295 case MINUS_EXPR:
5296 case MULT_EXPR:
5297 case TRUNC_DIV_EXPR:
5298 case TRUNC_MOD_EXPR:
5299 case BIT_XOR_EXPR:
5300 case BIT_AND_EXPR:
5301 case BIT_IOR_EXPR:
5302 case LSHIFT_EXPR:
5303 case RSHIFT_EXPR:
5305 case MODOP_EXPR:
5307 case EQ_EXPR:
5308 case NE_EXPR:
5309 case LE_EXPR:
5310 case GE_EXPR:
5311 case LT_EXPR:
5312 case GT_EXPR:
5314 case TRUTH_ANDIF_EXPR:
5315 case TRUTH_ORIF_EXPR:
5317 case COMPOUND_EXPR:
5319 case DOTSTAR_EXPR:
5320 case MEMBER_REF:
5321 return true;
5323 default:
5324 return false;
5328 /* If the next token is a suitable fold operator, consume it and return as
5329 the function above. */
5331 static int
5332 cp_parser_fold_operator (cp_parser *parser)
5334 cp_token* token = cp_lexer_peek_token (parser->lexer);
5335 int code = cp_parser_fold_operator (token);
5336 if (code != ERROR_MARK)
5337 cp_lexer_consume_token (parser->lexer);
5338 return code;
5341 /* Parse a fold-expression.
5343 fold-expression:
5344 ( ... folding-operator cast-expression)
5345 ( cast-expression folding-operator ... )
5346 ( cast-expression folding operator ... folding-operator cast-expression)
5348 Note that the '(' and ')' are matched in primary expression. */
5350 static cp_expr
5351 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5353 cp_id_kind pidk;
5355 // Left fold.
5356 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5358 if (expr1)
5359 return error_mark_node;
5360 cp_lexer_consume_token (parser->lexer);
5361 int op = cp_parser_fold_operator (parser);
5362 if (op == ERROR_MARK)
5364 cp_parser_error (parser, "expected binary operator");
5365 return error_mark_node;
5368 tree expr = cp_parser_cast_expression (parser, false, false,
5369 false, &pidk);
5370 if (expr == error_mark_node)
5371 return error_mark_node;
5372 return finish_left_unary_fold_expr (expr, op);
5375 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5376 int op = cp_parser_fold_operator (parser);
5377 if (op == ERROR_MARK)
5379 cp_parser_error (parser, "expected binary operator");
5380 return error_mark_node;
5383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5385 cp_parser_error (parser, "expected ...");
5386 return error_mark_node;
5388 cp_lexer_consume_token (parser->lexer);
5390 /* The operands of a fold-expression are cast-expressions, so binary or
5391 conditional expressions are not allowed. We check this here to avoid
5392 tentative parsing. */
5393 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5394 /* OK, the expression was parenthesized. */;
5395 else if (is_binary_op (TREE_CODE (expr1)))
5396 error_at (location_of (expr1),
5397 "binary expression in operand of fold-expression");
5398 else if (TREE_CODE (expr1) == COND_EXPR
5399 || (REFERENCE_REF_P (expr1)
5400 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5401 error_at (location_of (expr1),
5402 "conditional expression in operand of fold-expression");
5404 // Right fold.
5405 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5406 return finish_right_unary_fold_expr (expr1, op);
5408 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5410 cp_parser_error (parser, "mismatched operator in fold-expression");
5411 return error_mark_node;
5413 cp_lexer_consume_token (parser->lexer);
5415 // Binary left or right fold.
5416 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5417 if (expr2 == error_mark_node)
5418 return error_mark_node;
5419 return finish_binary_fold_expr (expr1, expr2, op);
5422 /* Parse a primary-expression.
5424 primary-expression:
5425 literal
5426 this
5427 ( expression )
5428 id-expression
5429 lambda-expression (C++11)
5431 GNU Extensions:
5433 primary-expression:
5434 ( compound-statement )
5435 __builtin_va_arg ( assignment-expression , type-id )
5436 __builtin_offsetof ( type-id , offsetof-expression )
5438 C++ Extensions:
5439 __has_nothrow_assign ( type-id )
5440 __has_nothrow_constructor ( type-id )
5441 __has_nothrow_copy ( type-id )
5442 __has_trivial_assign ( type-id )
5443 __has_trivial_constructor ( type-id )
5444 __has_trivial_copy ( type-id )
5445 __has_trivial_destructor ( type-id )
5446 __has_virtual_destructor ( type-id )
5447 __is_abstract ( type-id )
5448 __is_base_of ( type-id , type-id )
5449 __is_class ( type-id )
5450 __is_empty ( type-id )
5451 __is_enum ( type-id )
5452 __is_final ( type-id )
5453 __is_literal_type ( type-id )
5454 __is_pod ( type-id )
5455 __is_polymorphic ( type-id )
5456 __is_std_layout ( type-id )
5457 __is_trivial ( type-id )
5458 __is_union ( type-id )
5460 Objective-C++ Extension:
5462 primary-expression:
5463 objc-expression
5465 literal:
5466 __null
5468 ADDRESS_P is true iff this expression was immediately preceded by
5469 "&" and therefore might denote a pointer-to-member. CAST_P is true
5470 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5471 true iff this expression is a template argument.
5473 Returns a representation of the expression. Upon return, *IDK
5474 indicates what kind of id-expression (if any) was present. */
5476 static cp_expr
5477 cp_parser_primary_expression (cp_parser *parser,
5478 bool address_p,
5479 bool cast_p,
5480 bool template_arg_p,
5481 bool decltype_p,
5482 cp_id_kind *idk)
5484 cp_token *token = NULL;
5486 /* Assume the primary expression is not an id-expression. */
5487 *idk = CP_ID_KIND_NONE;
5489 /* Peek at the next token. */
5490 token = cp_lexer_peek_token (parser->lexer);
5491 switch ((int) token->type)
5493 /* literal:
5494 integer-literal
5495 character-literal
5496 floating-literal
5497 string-literal
5498 boolean-literal
5499 pointer-literal
5500 user-defined-literal */
5501 case CPP_CHAR:
5502 case CPP_CHAR16:
5503 case CPP_CHAR32:
5504 case CPP_WCHAR:
5505 case CPP_UTF8CHAR:
5506 case CPP_NUMBER:
5507 case CPP_PREPARSED_EXPR:
5508 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5509 return cp_parser_userdef_numeric_literal (parser);
5510 token = cp_lexer_consume_token (parser->lexer);
5511 if (TREE_CODE (token->u.value) == FIXED_CST)
5513 error_at (token->location,
5514 "fixed-point types not supported in C++");
5515 return error_mark_node;
5517 /* Floating-point literals are only allowed in an integral
5518 constant expression if they are cast to an integral or
5519 enumeration type. */
5520 if (TREE_CODE (token->u.value) == REAL_CST
5521 && parser->integral_constant_expression_p
5522 && pedantic)
5524 /* CAST_P will be set even in invalid code like "int(2.7 +
5525 ...)". Therefore, we have to check that the next token
5526 is sure to end the cast. */
5527 if (cast_p)
5529 cp_token *next_token;
5531 next_token = cp_lexer_peek_token (parser->lexer);
5532 if (/* The comma at the end of an
5533 enumerator-definition. */
5534 next_token->type != CPP_COMMA
5535 /* The curly brace at the end of an enum-specifier. */
5536 && next_token->type != CPP_CLOSE_BRACE
5537 /* The end of a statement. */
5538 && next_token->type != CPP_SEMICOLON
5539 /* The end of the cast-expression. */
5540 && next_token->type != CPP_CLOSE_PAREN
5541 /* The end of an array bound. */
5542 && next_token->type != CPP_CLOSE_SQUARE
5543 /* The closing ">" in a template-argument-list. */
5544 && (next_token->type != CPP_GREATER
5545 || parser->greater_than_is_operator_p)
5546 /* C++0x only: A ">>" treated like two ">" tokens,
5547 in a template-argument-list. */
5548 && (next_token->type != CPP_RSHIFT
5549 || (cxx_dialect == cxx98)
5550 || parser->greater_than_is_operator_p))
5551 cast_p = false;
5554 /* If we are within a cast, then the constraint that the
5555 cast is to an integral or enumeration type will be
5556 checked at that point. If we are not within a cast, then
5557 this code is invalid. */
5558 if (!cast_p)
5559 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5561 return (cp_expr (token->u.value, token->location)
5562 .maybe_add_location_wrapper ());
5564 case CPP_CHAR_USERDEF:
5565 case CPP_CHAR16_USERDEF:
5566 case CPP_CHAR32_USERDEF:
5567 case CPP_WCHAR_USERDEF:
5568 case CPP_UTF8CHAR_USERDEF:
5569 return cp_parser_userdef_char_literal (parser);
5571 case CPP_STRING:
5572 case CPP_STRING16:
5573 case CPP_STRING32:
5574 case CPP_WSTRING:
5575 case CPP_UTF8STRING:
5576 case CPP_STRING_USERDEF:
5577 case CPP_STRING16_USERDEF:
5578 case CPP_STRING32_USERDEF:
5579 case CPP_WSTRING_USERDEF:
5580 case CPP_UTF8STRING_USERDEF:
5581 /* ??? Should wide strings be allowed when parser->translate_strings_p
5582 is false (i.e. in attributes)? If not, we can kill the third
5583 argument to cp_parser_string_literal. */
5584 return (cp_parser_string_literal (parser,
5585 parser->translate_strings_p,
5586 true)
5587 .maybe_add_location_wrapper ());
5589 case CPP_OPEN_PAREN:
5590 /* If we see `( { ' then we are looking at the beginning of
5591 a GNU statement-expression. */
5592 if (cp_parser_allow_gnu_extensions_p (parser)
5593 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5595 /* Statement-expressions are not allowed by the standard. */
5596 pedwarn (token->location, OPT_Wpedantic,
5597 "ISO C++ forbids braced-groups within expressions");
5599 /* And they're not allowed outside of a function-body; you
5600 cannot, for example, write:
5602 int i = ({ int j = 3; j + 1; });
5604 at class or namespace scope. */
5605 if (!parser->in_function_body
5606 || parser->in_template_argument_list_p)
5608 error_at (token->location,
5609 "statement-expressions are not allowed outside "
5610 "functions nor in template-argument lists");
5611 cp_parser_skip_to_end_of_block_or_statement (parser);
5612 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5613 cp_lexer_consume_token (parser->lexer);
5614 return error_mark_node;
5616 else
5617 return cp_parser_statement_expr (parser);
5619 /* Otherwise it's a normal parenthesized expression. */
5621 cp_expr expr;
5622 bool saved_greater_than_is_operator_p;
5624 location_t open_paren_loc = token->location;
5626 /* Consume the `('. */
5627 matching_parens parens;
5628 parens.consume_open (parser);
5629 /* Within a parenthesized expression, a `>' token is always
5630 the greater-than operator. */
5631 saved_greater_than_is_operator_p
5632 = parser->greater_than_is_operator_p;
5633 parser->greater_than_is_operator_p = true;
5635 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5636 /* Left fold expression. */
5637 expr = NULL_TREE;
5638 else
5639 /* Parse the parenthesized expression. */
5640 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5642 token = cp_lexer_peek_token (parser->lexer);
5643 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5645 expr = cp_parser_fold_expression (parser, expr);
5646 if (expr != error_mark_node
5647 && cxx_dialect < cxx17)
5648 pedwarn (input_location, OPT_Wc__17_extensions,
5649 "fold-expressions only available with %<-std=c++17%> "
5650 "or %<-std=gnu++17%>");
5652 else
5653 /* Let the front end know that this expression was
5654 enclosed in parentheses. This matters in case, for
5655 example, the expression is of the form `A::B', since
5656 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5657 not. */
5658 expr = finish_parenthesized_expr (expr);
5660 /* DR 705: Wrapping an unqualified name in parentheses
5661 suppresses arg-dependent lookup. We want to pass back
5662 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5663 (c++/37862), but none of the others. */
5664 if (*idk != CP_ID_KIND_QUALIFIED)
5665 *idk = CP_ID_KIND_NONE;
5667 /* The `>' token might be the end of a template-id or
5668 template-parameter-list now. */
5669 parser->greater_than_is_operator_p
5670 = saved_greater_than_is_operator_p;
5672 /* Consume the `)'. */
5673 token = cp_lexer_peek_token (parser->lexer);
5674 location_t close_paren_loc = token->location;
5675 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5676 expr.set_range (open_paren_loc, close_paren_loc);
5677 if (no_wparens)
5678 suppress_warning (expr, OPT_Wparentheses);
5679 if (!parens.require_close (parser)
5680 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5681 cp_parser_skip_to_end_of_statement (parser);
5683 return expr;
5686 case CPP_OPEN_SQUARE:
5688 if (c_dialect_objc ())
5690 /* We might have an Objective-C++ message. */
5691 cp_parser_parse_tentatively (parser);
5692 tree msg = cp_parser_objc_message_expression (parser);
5693 /* If that works out, we're done ... */
5694 if (cp_parser_parse_definitely (parser))
5695 return msg;
5696 /* ... else, fall though to see if it's a lambda. */
5698 cp_expr lam = cp_parser_lambda_expression (parser);
5699 /* Don't warn about a failed tentative parse. */
5700 if (cp_parser_error_occurred (parser))
5701 return error_mark_node;
5702 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5703 return lam;
5706 case CPP_OBJC_STRING:
5707 if (c_dialect_objc ())
5708 /* We have an Objective-C++ string literal. */
5709 return cp_parser_objc_expression (parser);
5710 cp_parser_error (parser, "expected primary-expression");
5711 return error_mark_node;
5713 case CPP_KEYWORD:
5714 switch (token->keyword)
5716 /* These two are the boolean literals. */
5717 case RID_TRUE:
5718 cp_lexer_consume_token (parser->lexer);
5719 return cp_expr (boolean_true_node, token->location);
5720 case RID_FALSE:
5721 cp_lexer_consume_token (parser->lexer);
5722 return cp_expr (boolean_false_node, token->location);
5724 /* The `__null' literal. */
5725 case RID_NULL:
5726 cp_lexer_consume_token (parser->lexer);
5727 return cp_expr (null_node, token->location);
5729 /* The `nullptr' literal. */
5730 case RID_NULLPTR:
5731 cp_lexer_consume_token (parser->lexer);
5732 return cp_expr (nullptr_node, token->location);
5734 /* Recognize the `this' keyword. */
5735 case RID_THIS:
5736 cp_lexer_consume_token (parser->lexer);
5737 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5739 error_at (token->location,
5740 "%<this%> may not be used in this context");
5741 return error_mark_node;
5743 /* Pointers cannot appear in constant-expressions. */
5744 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5745 return error_mark_node;
5746 return cp_expr (finish_this_expr (), token->location);
5748 /* The `operator' keyword can be the beginning of an
5749 id-expression. */
5750 case RID_OPERATOR:
5751 goto id_expression;
5753 case RID_FUNCTION_NAME:
5754 case RID_PRETTY_FUNCTION_NAME:
5755 case RID_C99_FUNCTION_NAME:
5757 non_integral_constant name;
5759 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5760 __func__ are the names of variables -- but they are
5761 treated specially. Therefore, they are handled here,
5762 rather than relying on the generic id-expression logic
5763 below. Grammatically, these names are id-expressions.
5765 Consume the token. */
5766 token = cp_lexer_consume_token (parser->lexer);
5768 switch (token->keyword)
5770 case RID_FUNCTION_NAME:
5771 name = NIC_FUNC_NAME;
5772 break;
5773 case RID_PRETTY_FUNCTION_NAME:
5774 name = NIC_PRETTY_FUNC;
5775 break;
5776 case RID_C99_FUNCTION_NAME:
5777 name = NIC_C99_FUNC;
5778 break;
5779 default:
5780 gcc_unreachable ();
5783 if (cp_parser_non_integral_constant_expression (parser, name))
5784 return error_mark_node;
5786 /* Look up the name. */
5787 return finish_fname (token->u.value);
5790 case RID_VA_ARG:
5792 tree expression;
5793 tree type;
5794 location_t type_location;
5795 location_t start_loc
5796 = cp_lexer_peek_token (parser->lexer)->location;
5797 /* The `__builtin_va_arg' construct is used to handle
5798 `va_arg'. Consume the `__builtin_va_arg' token. */
5799 cp_lexer_consume_token (parser->lexer);
5800 /* Look for the opening `('. */
5801 matching_parens parens;
5802 parens.require_open (parser);
5803 /* Now, parse the assignment-expression. */
5804 expression = cp_parser_assignment_expression (parser);
5805 /* Look for the `,'. */
5806 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5807 type_location = cp_lexer_peek_token (parser->lexer)->location;
5808 /* Parse the type-id. */
5810 type_id_in_expr_sentinel s (parser);
5811 type = cp_parser_type_id (parser);
5813 /* Look for the closing `)'. */
5814 location_t finish_loc
5815 = cp_lexer_peek_token (parser->lexer)->location;
5816 parens.require_close (parser);
5817 /* Using `va_arg' in a constant-expression is not
5818 allowed. */
5819 if (cp_parser_non_integral_constant_expression (parser,
5820 NIC_VA_ARG))
5821 return error_mark_node;
5822 /* Construct a location of the form:
5823 __builtin_va_arg (v, int)
5824 ~~~~~~~~~~~~~~~~~~~~~^~~~
5825 with the caret at the type, ranging from the start of the
5826 "__builtin_va_arg" token to the close paren. */
5827 location_t combined_loc
5828 = make_location (type_location, start_loc, finish_loc);
5829 return build_x_va_arg (combined_loc, expression, type);
5832 case RID_OFFSETOF:
5833 return cp_parser_builtin_offsetof (parser);
5835 case RID_HAS_NOTHROW_ASSIGN:
5836 case RID_HAS_NOTHROW_CONSTRUCTOR:
5837 case RID_HAS_NOTHROW_COPY:
5838 case RID_HAS_TRIVIAL_ASSIGN:
5839 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5840 case RID_HAS_TRIVIAL_COPY:
5841 case RID_HAS_TRIVIAL_DESTRUCTOR:
5842 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5843 case RID_HAS_VIRTUAL_DESTRUCTOR:
5844 case RID_IS_ABSTRACT:
5845 case RID_IS_AGGREGATE:
5846 case RID_IS_BASE_OF:
5847 case RID_IS_CLASS:
5848 case RID_IS_EMPTY:
5849 case RID_IS_ENUM:
5850 case RID_IS_FINAL:
5851 case RID_IS_LAYOUT_COMPATIBLE:
5852 case RID_IS_LITERAL_TYPE:
5853 case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
5854 case RID_IS_POD:
5855 case RID_IS_POLYMORPHIC:
5856 case RID_IS_SAME_AS:
5857 case RID_IS_STD_LAYOUT:
5858 case RID_IS_TRIVIAL:
5859 case RID_IS_TRIVIALLY_ASSIGNABLE:
5860 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5861 case RID_IS_TRIVIALLY_COPYABLE:
5862 case RID_IS_UNION:
5863 case RID_IS_ASSIGNABLE:
5864 case RID_IS_CONSTRUCTIBLE:
5865 case RID_IS_NOTHROW_ASSIGNABLE:
5866 case RID_IS_NOTHROW_CONSTRUCTIBLE:
5867 return cp_parser_trait_expr (parser, token->keyword);
5869 // C++ concepts
5870 case RID_REQUIRES:
5871 return cp_parser_requires_expression (parser);
5873 /* Objective-C++ expressions. */
5874 case RID_AT_ENCODE:
5875 case RID_AT_PROTOCOL:
5876 case RID_AT_SELECTOR:
5877 return cp_parser_objc_expression (parser);
5879 case RID_OMP_ALL_MEMORY:
5880 gcc_assert (flag_openmp);
5881 cp_lexer_consume_token (parser->lexer);
5882 error_at (token->location,
5883 "%<omp_all_memory%> may only be used in OpenMP "
5884 "%<depend%> clause");
5885 return error_mark_node;
5887 case RID_TEMPLATE:
5888 if (parser->in_function_body
5889 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5890 == CPP_LESS))
5892 error_at (token->location,
5893 "a template declaration cannot appear at block scope");
5894 cp_parser_skip_to_end_of_block_or_statement (parser);
5895 return error_mark_node;
5897 /* FALLTHRU */
5898 default:
5899 cp_parser_error (parser, "expected primary-expression");
5900 return error_mark_node;
5903 /* An id-expression can start with either an identifier, a
5904 `::' as the beginning of a qualified-id, or the "operator"
5905 keyword. */
5906 case CPP_NAME:
5907 case CPP_SCOPE:
5908 case CPP_TEMPLATE_ID:
5909 case CPP_NESTED_NAME_SPECIFIER:
5911 id_expression:
5912 cp_expr id_expression;
5913 cp_expr decl;
5914 const char *error_msg;
5915 bool template_p;
5916 bool done;
5917 cp_token *id_expr_token;
5919 /* Parse the id-expression. */
5920 id_expression
5921 = cp_parser_id_expression (parser,
5922 /*template_keyword_p=*/false,
5923 /*check_dependency_p=*/true,
5924 &template_p,
5925 /*declarator_p=*/false,
5926 /*optional_p=*/false);
5927 if (id_expression == error_mark_node)
5928 return error_mark_node;
5929 id_expr_token = token;
5930 token = cp_lexer_peek_token (parser->lexer);
5931 done = (token->type != CPP_OPEN_SQUARE
5932 && token->type != CPP_OPEN_PAREN
5933 && token->type != CPP_DOT
5934 && token->type != CPP_DEREF
5935 && token->type != CPP_PLUS_PLUS
5936 && token->type != CPP_MINUS_MINUS);
5937 /* If we have a template-id, then no further lookup is
5938 required. If the template-id was for a template-class, we
5939 will sometimes have a TYPE_DECL at this point. */
5940 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5941 || TREE_CODE (id_expression) == TYPE_DECL)
5942 decl = id_expression;
5943 /* Look up the name. */
5944 else
5946 tree ambiguous_decls;
5948 /* If we already know that this lookup is ambiguous, then
5949 we've already issued an error message; there's no reason
5950 to check again. */
5951 if (id_expr_token->type == CPP_NAME
5952 && id_expr_token->error_reported)
5954 cp_parser_simulate_error (parser);
5955 return error_mark_node;
5958 decl = cp_parser_lookup_name (parser, id_expression,
5959 none_type,
5960 template_p,
5961 /*is_namespace=*/false,
5962 /*check_dependency=*/true,
5963 &ambiguous_decls,
5964 id_expression.get_location ());
5965 /* If the lookup was ambiguous, an error will already have
5966 been issued. */
5967 if (ambiguous_decls)
5968 return error_mark_node;
5970 /* In Objective-C++, we may have an Objective-C 2.0
5971 dot-syntax for classes here. */
5972 if (c_dialect_objc ()
5973 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5974 && TREE_CODE (decl) == TYPE_DECL
5975 && objc_is_class_name (decl))
5977 tree component;
5978 cp_lexer_consume_token (parser->lexer);
5979 component = cp_parser_identifier (parser);
5980 if (component == error_mark_node)
5981 return error_mark_node;
5983 tree result = objc_build_class_component_ref (id_expression,
5984 component);
5985 /* Build a location of the form:
5986 expr.component
5987 ~~~~~^~~~~~~~~
5988 with caret at the start of the component name (at
5989 input_location), ranging from the start of the id_expression
5990 to the end of the component name. */
5991 location_t combined_loc
5992 = make_location (input_location, id_expression.get_start (),
5993 get_finish (input_location));
5994 protected_set_expr_location (result, combined_loc);
5995 return result;
5998 /* In Objective-C++, an instance variable (ivar) may be preferred
5999 to whatever cp_parser_lookup_name() found.
6000 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6001 rest of c-family, we have to do a little extra work to preserve
6002 any location information in cp_expr "decl". Given that
6003 objc_lookup_ivar is implemented in "c-family" and "objc", we
6004 have a trip through the pure "tree" type, rather than cp_expr.
6005 Naively copying it back to "decl" would implicitly give the
6006 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6007 store an EXPR_LOCATION. Hence we only update "decl" (and
6008 hence its location_t) if we get back a different tree node. */
6009 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6010 id_expression);
6011 if (decl_tree != decl.get_value ())
6012 decl = cp_expr (decl_tree);
6014 /* If name lookup gives us a SCOPE_REF, then the
6015 qualifying scope was dependent. */
6016 if (TREE_CODE (decl) == SCOPE_REF)
6018 /* At this point, we do not know if DECL is a valid
6019 integral constant expression. We assume that it is
6020 in fact such an expression, so that code like:
6022 template <int N> struct A {
6023 int a[B<N>::i];
6026 is accepted. At template-instantiation time, we
6027 will check that B<N>::i is actually a constant. */
6028 return decl;
6030 /* Check to see if DECL is a local variable in a context
6031 where that is forbidden. */
6032 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6033 && local_variable_p (decl)
6034 /* DR 2082 permits local variables in unevaluated contexts
6035 within a default argument. */
6036 && !cp_unevaluated_operand)
6038 const char *msg
6039 = (TREE_CODE (decl) == PARM_DECL
6040 ? _("parameter %qD may not appear in this context")
6041 : _("local variable %qD may not appear in this context"));
6042 error_at (id_expression.get_location (), msg,
6043 decl.get_value ());
6044 return error_mark_node;
6048 decl = (finish_id_expression
6049 (id_expression, decl, parser->scope,
6050 idk,
6051 parser->integral_constant_expression_p,
6052 parser->allow_non_integral_constant_expression_p,
6053 &parser->non_integral_constant_expression_p,
6054 template_p, done, address_p,
6055 template_arg_p,
6056 &error_msg,
6057 id_expression.get_location ()));
6058 if (error_msg)
6059 cp_parser_error (parser, error_msg);
6060 /* Build a location for an id-expression of the form:
6061 ::ns::id
6062 ~~~~~~^~
6066 i.e. from the start of the first token to the end of the final
6067 token, with the caret at the start of the unqualified-id. */
6068 location_t caret_loc = get_pure_location (id_expression.get_location ());
6069 location_t start_loc = get_start (id_expr_token->location);
6070 location_t finish_loc = get_finish (id_expression.get_location ());
6071 location_t combined_loc
6072 = make_location (caret_loc, start_loc, finish_loc);
6074 decl.set_location (combined_loc);
6075 return decl;
6078 /* Anything else is an error. */
6079 default:
6080 cp_parser_error (parser, "expected primary-expression");
6081 return error_mark_node;
6085 static inline cp_expr
6086 cp_parser_primary_expression (cp_parser *parser,
6087 bool address_p,
6088 bool cast_p,
6089 bool template_arg_p,
6090 cp_id_kind *idk)
6092 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6093 /*decltype*/false, idk);
6096 /* Parse an id-expression.
6098 id-expression:
6099 unqualified-id
6100 qualified-id
6102 qualified-id:
6103 :: [opt] nested-name-specifier template [opt] unqualified-id
6104 :: identifier
6105 :: operator-function-id
6106 :: template-id
6108 Return a representation of the unqualified portion of the
6109 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6110 a `::' or nested-name-specifier.
6112 Often, if the id-expression was a qualified-id, the caller will
6113 want to make a SCOPE_REF to represent the qualified-id. This
6114 function does not do this in order to avoid wastefully creating
6115 SCOPE_REFs when they are not required.
6117 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6118 `template' keyword.
6120 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6121 uninstantiated templates.
6123 If *TEMPLATE_P is non-NULL, it is set to true iff the
6124 `template' keyword is used to explicitly indicate that the entity
6125 named is a template.
6127 If DECLARATOR_P is true, the id-expression is appearing as part of
6128 a declarator, rather than as part of an expression. */
6130 static cp_expr
6131 cp_parser_id_expression (cp_parser *parser,
6132 bool template_keyword_p,
6133 bool check_dependency_p,
6134 bool *template_p,
6135 bool declarator_p,
6136 bool optional_p)
6138 bool global_scope_p;
6139 bool nested_name_specifier_p;
6141 /* Assume the `template' keyword was not used. */
6142 if (template_p)
6143 *template_p = template_keyword_p;
6145 /* Look for the optional `::' operator. */
6146 global_scope_p
6147 = (!template_keyword_p
6148 && (cp_parser_global_scope_opt (parser,
6149 /*current_scope_valid_p=*/false)
6150 != NULL_TREE));
6152 /* Look for the optional nested-name-specifier. */
6153 nested_name_specifier_p
6154 = (cp_parser_nested_name_specifier_opt (parser,
6155 /*typename_keyword_p=*/false,
6156 check_dependency_p,
6157 /*type_p=*/false,
6158 declarator_p,
6159 template_keyword_p)
6160 != NULL_TREE);
6162 cp_expr id = NULL_TREE;
6163 tree scope = parser->scope;
6165 /* Peek at the next token. */
6166 cp_token *token = cp_lexer_peek_token (parser->lexer);
6168 /* If there is a nested-name-specifier, then we are looking at
6169 the first qualified-id production. */
6170 if (nested_name_specifier_p)
6172 tree saved_object_scope;
6173 tree saved_qualifying_scope;
6175 /* See if the next token is the `template' keyword. */
6176 if (!template_p)
6177 template_p = &template_keyword_p;
6178 *template_p = cp_parser_optional_template_keyword (parser);
6179 /* Name lookup we do during the processing of the
6180 unqualified-id might obliterate SCOPE. */
6181 saved_object_scope = parser->object_scope;
6182 saved_qualifying_scope = parser->qualifying_scope;
6183 /* Process the final unqualified-id. */
6184 id = cp_parser_unqualified_id (parser, *template_p,
6185 check_dependency_p,
6186 declarator_p,
6187 /*optional_p=*/false);
6188 /* Restore the SAVED_SCOPE for our caller. */
6189 parser->scope = scope;
6190 parser->object_scope = saved_object_scope;
6191 parser->qualifying_scope = saved_qualifying_scope;
6193 /* Otherwise, if we are in global scope, then we are looking at one
6194 of the other qualified-id productions. */
6195 else if (global_scope_p)
6197 /* If it's an identifier, and the next token is not a "<", then
6198 we can avoid the template-id case. This is an optimization
6199 for this common case. */
6200 if (token->type == CPP_NAME
6201 && !cp_parser_nth_token_starts_template_argument_list_p
6202 (parser, 2))
6203 return cp_parser_identifier (parser);
6205 cp_parser_parse_tentatively (parser);
6206 /* Try a template-id. */
6207 id = cp_parser_template_id_expr (parser,
6208 /*template_keyword_p=*/false,
6209 /*check_dependency_p=*/true,
6210 declarator_p);
6211 /* If that worked, we're done. */
6212 if (cp_parser_parse_definitely (parser))
6213 return id;
6215 /* Peek at the next token. (Changes in the token buffer may
6216 have invalidated the pointer obtained above.) */
6217 token = cp_lexer_peek_token (parser->lexer);
6219 switch (token->type)
6221 case CPP_NAME:
6222 id = cp_parser_identifier (parser);
6223 break;
6225 case CPP_KEYWORD:
6226 if (token->keyword == RID_OPERATOR)
6228 id = cp_parser_operator_function_id (parser);
6229 break;
6231 /* Fall through. */
6233 default:
6234 cp_parser_error (parser, "expected id-expression");
6235 return error_mark_node;
6238 else
6240 if (!scope)
6241 scope = parser->context->object_type;
6242 id = cp_parser_unqualified_id (parser, template_keyword_p,
6243 /*check_dependency_p=*/true,
6244 declarator_p,
6245 optional_p);
6248 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6249 && warn_missing_template_keyword
6250 && !template_keyword_p
6251 /* Don't warn if we're looking inside templates. */
6252 && check_dependency_p
6253 /* In a template argument list a > could be closing
6254 the enclosing targs. */
6255 && !parser->in_template_argument_list_p
6256 && scope && dependentish_scope_p (scope)
6257 /* Don't confuse an ill-formed constructor declarator for a missing
6258 template keyword in a return type. */
6259 && !(declarator_p && constructor_name_p (id, scope))
6260 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6261 && warning_enabled_at (token->location,
6262 OPT_Wmissing_template_keyword))
6264 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6265 if (cp_parser_skip_entire_template_parameter_list (parser)
6266 /* An operator after the > suggests that the > ends a
6267 template-id; a name or literal suggests that the > is an
6268 operator. */
6269 && (cp_lexer_peek_token (parser->lexer)->type
6270 <= CPP_LAST_PUNCTUATOR))
6271 warning_at (token->location, OPT_Wmissing_template_keyword,
6272 "expected %qs keyword before dependent "
6273 "template name", "template");
6276 return id;
6279 /* Parse an unqualified-id.
6281 unqualified-id:
6282 identifier
6283 operator-function-id
6284 conversion-function-id
6285 ~ class-name
6286 template-id
6288 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6289 keyword, in a construct like `A::template ...'.
6291 Returns a representation of unqualified-id. For the `identifier'
6292 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6293 production a BIT_NOT_EXPR is returned; the operand of the
6294 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6295 other productions, see the documentation accompanying the
6296 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6297 names are looked up in uninstantiated templates. If DECLARATOR_P
6298 is true, the unqualified-id is appearing as part of a declarator,
6299 rather than as part of an expression. */
6301 static cp_expr
6302 cp_parser_unqualified_id (cp_parser* parser,
6303 bool template_keyword_p,
6304 bool check_dependency_p,
6305 bool declarator_p,
6306 bool optional_p)
6308 cp_token *token;
6310 /* Peek at the next token. */
6311 token = cp_lexer_peek_token (parser->lexer);
6313 switch ((int) token->type)
6315 case CPP_NAME:
6317 tree id;
6319 /* We don't know yet whether or not this will be a
6320 template-id. */
6321 cp_parser_parse_tentatively (parser);
6322 /* Try a template-id. */
6323 id = cp_parser_template_id_expr (parser, template_keyword_p,
6324 check_dependency_p,
6325 declarator_p);
6326 /* If it worked, we're done. */
6327 if (cp_parser_parse_definitely (parser))
6328 return id;
6329 /* Otherwise, it's an ordinary identifier. */
6330 return cp_parser_identifier (parser);
6333 case CPP_TEMPLATE_ID:
6334 return cp_parser_template_id_expr (parser, template_keyword_p,
6335 check_dependency_p,
6336 declarator_p);
6338 case CPP_COMPL:
6340 tree type_decl;
6341 tree qualifying_scope;
6342 tree object_scope;
6343 tree scope;
6344 bool done;
6345 location_t tilde_loc = token->location;
6347 /* Consume the `~' token. */
6348 cp_lexer_consume_token (parser->lexer);
6349 /* Parse the class-name. The standard, as written, seems to
6350 say that:
6352 template <typename T> struct S { ~S (); };
6353 template <typename T> S<T>::~S() {}
6355 is invalid, since `~' must be followed by a class-name, but
6356 `S<T>' is dependent, and so not known to be a class.
6357 That's not right; we need to look in uninstantiated
6358 templates. A further complication arises from:
6360 template <typename T> void f(T t) {
6361 t.T::~T();
6364 Here, it is not possible to look up `T' in the scope of `T'
6365 itself. We must look in both the current scope, and the
6366 scope of the containing complete expression.
6368 Yet another issue is:
6370 struct S {
6371 int S;
6372 ~S();
6375 S::~S() {}
6377 The standard does not seem to say that the `S' in `~S'
6378 should refer to the type `S' and not the data member
6379 `S::S'. */
6381 /* DR 244 says that we look up the name after the "~" in the
6382 same scope as we looked up the qualifying name. That idea
6383 isn't fully worked out; it's more complicated than that. */
6384 scope = parser->scope;
6385 object_scope = parser->object_scope;
6386 qualifying_scope = parser->qualifying_scope;
6388 /* Check for invalid scopes. */
6389 if (scope == error_mark_node)
6391 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6392 cp_lexer_consume_token (parser->lexer);
6393 return error_mark_node;
6395 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6397 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6398 error_at (token->location,
6399 "scope %qT before %<~%> is not a class-name",
6400 scope);
6401 cp_parser_simulate_error (parser);
6402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6403 cp_lexer_consume_token (parser->lexer);
6404 return error_mark_node;
6406 if (template_keyword_p)
6408 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6409 error_at (tilde_loc, "%<template%> keyword not permitted in "
6410 "destructor name");
6411 cp_parser_simulate_error (parser);
6412 return error_mark_node;
6415 gcc_assert (!scope || TYPE_P (scope));
6417 token = cp_lexer_peek_token (parser->lexer);
6419 /* Create a location with caret == start at the tilde,
6420 finishing at the end of the peeked token, e.g:
6421 ~token
6422 ^~~~~~. */
6423 location_t loc
6424 = make_location (tilde_loc, tilde_loc, token->location);
6426 /* If the name is of the form "X::~X" it's OK even if X is a
6427 typedef. */
6429 if (scope
6430 && token->type == CPP_NAME
6431 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6432 != CPP_LESS)
6433 && (token->u.value == TYPE_IDENTIFIER (scope)
6434 || (CLASS_TYPE_P (scope)
6435 && constructor_name_p (token->u.value, scope))))
6437 cp_lexer_consume_token (parser->lexer);
6438 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6441 /* ~auto means the destructor of whatever the object is. */
6442 if (cp_parser_is_keyword (token, RID_AUTO))
6444 if (cxx_dialect < cxx14)
6445 pedwarn (loc, OPT_Wc__14_extensions,
6446 "%<~auto%> only available with "
6447 "%<-std=c++14%> or %<-std=gnu++14%>");
6448 cp_lexer_consume_token (parser->lexer);
6449 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6452 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6453 declarator-id of a constructor or destructor. */
6454 if (token->type == CPP_TEMPLATE_ID && declarator_p
6455 && cxx_dialect >= cxx20)
6457 if (!cp_parser_simulate_error (parser))
6458 error_at (tilde_loc, "template-id not allowed for destructor");
6459 return error_mark_node;
6462 /* If there was an explicit qualification (S::~T), first look
6463 in the scope given by the qualification (i.e., S).
6465 Note: in the calls to cp_parser_class_name below we pass
6466 typename_type so that lookup finds the injected-class-name
6467 rather than the constructor. */
6468 done = false;
6469 type_decl = NULL_TREE;
6470 if (scope)
6472 cp_parser_parse_tentatively (parser);
6473 type_decl = cp_parser_class_name (parser,
6474 /*typename_keyword_p=*/false,
6475 /*template_keyword_p=*/false,
6476 typename_type,
6477 /*check_dependency=*/false,
6478 /*class_head_p=*/false,
6479 declarator_p);
6480 if (cp_parser_parse_definitely (parser))
6481 done = true;
6483 /* In "N::S::~S", look in "N" as well. */
6484 if (!done && scope && qualifying_scope)
6486 cp_parser_parse_tentatively (parser);
6487 parser->scope = qualifying_scope;
6488 parser->object_scope = NULL_TREE;
6489 parser->qualifying_scope = NULL_TREE;
6490 type_decl
6491 = cp_parser_class_name (parser,
6492 /*typename_keyword_p=*/false,
6493 /*template_keyword_p=*/false,
6494 typename_type,
6495 /*check_dependency=*/false,
6496 /*class_head_p=*/false,
6497 declarator_p);
6498 if (cp_parser_parse_definitely (parser))
6499 done = true;
6501 /* In "p->S::~T", look in the scope given by "*p" as well. */
6502 else if (!done && object_scope)
6504 cp_parser_parse_tentatively (parser);
6505 parser->scope = object_scope;
6506 parser->object_scope = NULL_TREE;
6507 parser->qualifying_scope = NULL_TREE;
6508 type_decl
6509 = cp_parser_class_name (parser,
6510 /*typename_keyword_p=*/false,
6511 /*template_keyword_p=*/false,
6512 typename_type,
6513 /*check_dependency=*/false,
6514 /*class_head_p=*/false,
6515 declarator_p);
6516 if (cp_parser_parse_definitely (parser))
6517 done = true;
6519 /* Look in the surrounding context. */
6520 if (!done)
6522 parser->scope = NULL_TREE;
6523 parser->object_scope = NULL_TREE;
6524 parser->qualifying_scope = NULL_TREE;
6525 if (processing_template_decl)
6526 cp_parser_parse_tentatively (parser);
6527 type_decl
6528 = cp_parser_class_name (parser,
6529 /*typename_keyword_p=*/false,
6530 /*template_keyword_p=*/false,
6531 typename_type,
6532 /*check_dependency=*/false,
6533 /*class_head_p=*/false,
6534 declarator_p);
6535 if (processing_template_decl
6536 && ! cp_parser_parse_definitely (parser))
6538 /* We couldn't find a type with this name. If we're parsing
6539 tentatively, fail and try something else. */
6540 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6542 cp_parser_simulate_error (parser);
6543 return error_mark_node;
6545 /* Otherwise, accept it and check for a match at instantiation
6546 time. */
6547 type_decl = cp_parser_identifier (parser);
6548 if (type_decl != error_mark_node)
6549 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6550 return type_decl;
6553 /* If an error occurred, assume that the name of the
6554 destructor is the same as the name of the qualifying
6555 class. That allows us to keep parsing after running
6556 into ill-formed destructor names. */
6557 if (type_decl == error_mark_node && scope)
6558 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6559 else if (type_decl == error_mark_node)
6560 return error_mark_node;
6562 /* Check that destructor name and scope match. */
6563 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6565 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6566 error_at (loc,
6567 "declaration of %<~%T%> as member of %qT",
6568 type_decl, scope);
6569 cp_parser_simulate_error (parser);
6570 return error_mark_node;
6573 /* [class.dtor]
6575 A typedef-name that names a class shall not be used as the
6576 identifier in the declarator for a destructor declaration. */
6577 if (declarator_p
6578 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6579 && !DECL_SELF_REFERENCE_P (type_decl)
6580 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6581 error_at (loc,
6582 "typedef-name %qD used as destructor declarator",
6583 type_decl);
6585 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6588 case CPP_KEYWORD:
6589 if (token->keyword == RID_OPERATOR)
6591 cp_expr id;
6593 /* This could be a template-id, so we try that first. */
6594 cp_parser_parse_tentatively (parser);
6595 /* Try a template-id. */
6596 id = cp_parser_template_id_expr (parser, template_keyword_p,
6597 /*check_dependency_p=*/true,
6598 declarator_p);
6599 /* If that worked, we're done. */
6600 if (cp_parser_parse_definitely (parser))
6601 return id;
6602 /* We still don't know whether we're looking at an
6603 operator-function-id or a conversion-function-id. */
6604 cp_parser_parse_tentatively (parser);
6605 /* Try an operator-function-id. */
6606 id = cp_parser_operator_function_id (parser);
6607 /* If that didn't work, try a conversion-function-id. */
6608 if (!cp_parser_parse_definitely (parser))
6609 id = cp_parser_conversion_function_id (parser);
6611 return id;
6613 /* Fall through. */
6615 default:
6616 if (optional_p)
6617 return NULL_TREE;
6618 cp_parser_error (parser, "expected unqualified-id");
6619 return error_mark_node;
6623 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6624 be a template-id or the name shall refer to a class template or an
6625 alias template. */
6627 static void
6628 check_template_keyword_in_nested_name_spec (tree name)
6630 if (CLASS_TYPE_P (name)
6631 && ((CLASSTYPE_USE_TEMPLATE (name)
6632 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6633 || CLASSTYPE_IS_TEMPLATE (name)))
6634 return;
6636 if (TREE_CODE (name) == TYPENAME_TYPE
6637 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6638 return;
6639 /* Alias templates are also OK. */
6640 else if (alias_template_specialization_p (name, nt_opaque))
6641 return;
6643 permerror (input_location, TYPE_P (name)
6644 ? G_("%qT is not a template")
6645 : G_("%qD is not a template"),
6646 name);
6649 /* Parse an (optional) nested-name-specifier.
6651 nested-name-specifier: [C++98]
6652 class-or-namespace-name :: nested-name-specifier [opt]
6653 class-or-namespace-name :: template nested-name-specifier [opt]
6655 nested-name-specifier: [C++0x]
6656 type-name ::
6657 namespace-name ::
6658 nested-name-specifier identifier ::
6659 nested-name-specifier template [opt] simple-template-id ::
6661 PARSER->SCOPE should be set appropriately before this function is
6662 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6663 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6664 in name lookups.
6666 Sets PARSER->SCOPE to the class (TYPE) or namespace
6667 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6668 it unchanged if there is no nested-name-specifier. Returns the new
6669 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6671 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6673 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6674 part of a declaration and/or decl-specifier. */
6676 static tree
6677 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6678 bool typename_keyword_p,
6679 bool check_dependency_p,
6680 bool type_p,
6681 bool is_declaration,
6682 bool template_keyword_p /* = false */)
6684 bool success = false;
6685 cp_token_position start = 0;
6686 cp_token *token;
6688 /* Remember where the nested-name-specifier starts. */
6689 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6690 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6692 start = cp_lexer_token_position (parser->lexer, false);
6693 push_deferring_access_checks (dk_deferred);
6696 while (true)
6698 tree new_scope;
6699 tree old_scope;
6700 tree saved_qualifying_scope;
6702 /* Spot cases that cannot be the beginning of a
6703 nested-name-specifier. */
6704 token = cp_lexer_peek_token (parser->lexer);
6706 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6707 the already parsed nested-name-specifier. */
6708 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6710 /* Grab the nested-name-specifier and continue the loop. */
6711 cp_parser_pre_parsed_nested_name_specifier (parser);
6712 /* If we originally encountered this nested-name-specifier
6713 with CHECK_DEPENDENCY_P set to true, we will not have
6714 resolved TYPENAME_TYPEs, so we must do so here. */
6715 if (is_declaration
6716 && !check_dependency_p
6717 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6719 new_scope = resolve_typename_type (parser->scope,
6720 /*only_current_p=*/false);
6721 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6722 parser->scope = new_scope;
6724 success = true;
6725 continue;
6728 /* Spot cases that cannot be the beginning of a
6729 nested-name-specifier. On the second and subsequent times
6730 through the loop, we look for the `template' keyword. */
6731 if (success && token->keyword == RID_TEMPLATE)
6733 /* A template-id can start a nested-name-specifier. */
6734 else if (token->type == CPP_TEMPLATE_ID)
6736 /* DR 743: decltype can be used in a nested-name-specifier. */
6737 else if (token_is_decltype (token))
6739 else
6741 /* If the next token is not an identifier, then it is
6742 definitely not a type-name or namespace-name. */
6743 if (token->type != CPP_NAME)
6744 break;
6745 /* If the following token is neither a `<' (to begin a
6746 template-id), nor a `::', then we are not looking at a
6747 nested-name-specifier. */
6748 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6750 if (token->type == CPP_COLON
6751 && parser->colon_corrects_to_scope_p
6752 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6753 /* name:name is a valid sequence in an Objective C message. */
6754 && !parser->objective_c_message_context_p)
6756 gcc_rich_location richloc (token->location);
6757 richloc.add_fixit_replace ("::");
6758 error_at (&richloc,
6759 "found %<:%> in nested-name-specifier, "
6760 "expected %<::%>");
6761 token->type = CPP_SCOPE;
6764 if (token->type != CPP_SCOPE
6765 && !cp_parser_nth_token_starts_template_argument_list_p
6766 (parser, 2))
6767 break;
6770 /* The nested-name-specifier is optional, so we parse
6771 tentatively. */
6772 cp_parser_parse_tentatively (parser);
6774 /* Look for the optional `template' keyword, if this isn't the
6775 first time through the loop. */
6776 if (success)
6778 template_keyword_p = cp_parser_optional_template_keyword (parser);
6779 /* DR1710: "In a qualified-id used as the name in
6780 a typename-specifier, elaborated-type-specifier, using-declaration,
6781 or class-or-decltype, an optional keyword template appearing at
6782 the top level is ignored." */
6783 if (!template_keyword_p
6784 && typename_keyword_p
6785 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6786 template_keyword_p = true;
6789 /* Save the old scope since the name lookup we are about to do
6790 might destroy it. */
6791 old_scope = parser->scope;
6792 saved_qualifying_scope = parser->qualifying_scope;
6793 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6794 look up names in "X<T>::I" in order to determine that "Y" is
6795 a template. So, if we have a typename at this point, we make
6796 an effort to look through it. */
6797 if (is_declaration
6798 && !check_dependency_p
6799 && !typename_keyword_p
6800 && parser->scope
6801 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6802 parser->scope = resolve_typename_type (parser->scope,
6803 /*only_current_p=*/false);
6804 /* Parse the qualifying entity. */
6805 new_scope
6806 = cp_parser_qualifying_entity (parser,
6807 typename_keyword_p,
6808 template_keyword_p,
6809 check_dependency_p,
6810 type_p,
6811 is_declaration);
6812 /* Look for the `::' token. */
6813 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6815 /* If we found what we wanted, we keep going; otherwise, we're
6816 done. */
6817 if (!cp_parser_parse_definitely (parser))
6819 bool error_p = false;
6821 /* Restore the OLD_SCOPE since it was valid before the
6822 failed attempt at finding the last
6823 class-or-namespace-name. */
6824 parser->scope = old_scope;
6825 parser->qualifying_scope = saved_qualifying_scope;
6827 /* If the next token is a decltype, and the one after that is a
6828 `::', then the decltype has failed to resolve to a class or
6829 enumeration type. Give this error even when parsing
6830 tentatively since it can't possibly be valid--and we're going
6831 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6832 won't get another chance.*/
6833 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6834 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6835 == CPP_SCOPE))
6837 token = cp_lexer_consume_token (parser->lexer);
6838 tree dtype = token->u.tree_check_value->value;
6839 if (dtype != error_mark_node)
6840 error_at (token->location, "%<decltype%> evaluates to %qT, "
6841 "which is not a class or enumeration type",
6842 dtype);
6843 parser->scope = error_mark_node;
6844 error_p = true;
6845 /* As below. */
6846 success = true;
6847 cp_lexer_consume_token (parser->lexer);
6850 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6851 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6853 /* If we have a non-type template-id followed by ::, it can't
6854 possibly be valid. */
6855 token = cp_lexer_peek_token (parser->lexer);
6856 tree tid = token->u.tree_check_value->value;
6857 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6858 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6860 tree tmpl = NULL_TREE;
6861 if (is_overloaded_fn (tid))
6863 tree fns = get_fns (tid);
6864 if (OVL_SINGLE_P (fns))
6865 tmpl = OVL_FIRST (fns);
6866 if (function_concept_p (fns))
6867 error_at (token->location, "concept-id %qD "
6868 "in nested-name-specifier", tid);
6869 else
6870 error_at (token->location, "function template-id "
6871 "%qD in nested-name-specifier", tid);
6873 else
6875 tmpl = TREE_OPERAND (tid, 0);
6876 if (variable_concept_p (tmpl)
6877 || standard_concept_p (tmpl))
6878 error_at (token->location, "concept-id %qD "
6879 "in nested-name-specifier", tid);
6880 else
6882 /* Variable template. */
6883 gcc_assert (variable_template_p (tmpl));
6884 error_at (token->location, "variable template-id "
6885 "%qD in nested-name-specifier", tid);
6888 if (tmpl)
6889 inform (DECL_SOURCE_LOCATION (tmpl),
6890 "%qD declared here", tmpl);
6892 parser->scope = error_mark_node;
6893 error_p = true;
6894 /* As below. */
6895 success = true;
6896 cp_lexer_consume_token (parser->lexer);
6897 cp_lexer_consume_token (parser->lexer);
6901 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6902 break;
6903 /* If the next token is an identifier, and the one after
6904 that is a `::', then any valid interpretation would have
6905 found a class-or-namespace-name. */
6906 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6907 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6908 == CPP_SCOPE)
6909 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6910 != CPP_COMPL))
6912 token = cp_lexer_consume_token (parser->lexer);
6913 if (!error_p)
6915 if (!token->error_reported)
6917 tree decl;
6918 tree ambiguous_decls;
6920 decl = cp_parser_lookup_name (parser, token->u.value,
6921 none_type,
6922 /*is_template=*/false,
6923 /*is_namespace=*/false,
6924 /*check_dependency=*/true,
6925 &ambiguous_decls,
6926 token->location);
6927 if (TREE_CODE (decl) == TEMPLATE_DECL)
6928 error_at (token->location,
6929 "%qD used without template arguments",
6930 decl);
6931 else if (ambiguous_decls)
6933 // cp_parser_lookup_name has the same diagnostic,
6934 // thus make sure to emit it at most once.
6935 if (cp_parser_uncommitted_to_tentative_parse_p
6936 (parser))
6938 error_at (token->location,
6939 "reference to %qD is ambiguous",
6940 token->u.value);
6941 print_candidates (ambiguous_decls);
6943 decl = error_mark_node;
6945 else
6947 if (cxx_dialect != cxx98)
6948 cp_parser_name_lookup_error
6949 (parser, token->u.value, decl, NLE_NOT_CXX98,
6950 token->location);
6951 else
6952 cp_parser_name_lookup_error
6953 (parser, token->u.value, decl, NLE_CXX98,
6954 token->location);
6957 parser->scope = error_mark_node;
6958 error_p = true;
6959 /* Treat this as a successful nested-name-specifier
6960 due to:
6962 [basic.lookup.qual]
6964 If the name found is not a class-name (clause
6965 _class_) or namespace-name (_namespace.def_), the
6966 program is ill-formed. */
6967 success = true;
6969 cp_lexer_consume_token (parser->lexer);
6971 break;
6973 /* We've found one valid nested-name-specifier. */
6974 success = true;
6975 /* Name lookup always gives us a DECL. */
6976 if (TREE_CODE (new_scope) == TYPE_DECL)
6977 new_scope = TREE_TYPE (new_scope);
6978 /* Uses of "template" must be followed by actual templates. */
6979 if (template_keyword_p)
6980 check_template_keyword_in_nested_name_spec (new_scope);
6981 /* If it is a class scope, try to complete it; we are about to
6982 be looking up names inside the class. */
6983 if (TYPE_P (new_scope)
6984 /* Since checking types for dependency can be expensive,
6985 avoid doing it if the type is already complete. */
6986 && !COMPLETE_TYPE_P (new_scope)
6987 /* Do not try to complete dependent types. */
6988 && !dependent_type_p (new_scope))
6990 new_scope = complete_type (new_scope);
6991 /* If it is a typedef to current class, use the current
6992 class instead, as the typedef won't have any names inside
6993 it yet. */
6994 if (!COMPLETE_TYPE_P (new_scope)
6995 && currently_open_class (new_scope))
6996 new_scope = TYPE_MAIN_VARIANT (new_scope);
6998 /* Make sure we look in the right scope the next time through
6999 the loop. */
7000 parser->scope = new_scope;
7003 /* If parsing tentatively, replace the sequence of tokens that makes
7004 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7005 token. That way, should we re-parse the token stream, we will
7006 not have to repeat the effort required to do the parse, nor will
7007 we issue duplicate error messages. */
7008 if (success && start)
7010 cp_token *token;
7012 token = cp_lexer_token_at (parser->lexer, start);
7013 /* Reset the contents of the START token. */
7014 token->type = CPP_NESTED_NAME_SPECIFIER;
7015 /* Retrieve any deferred checks. Do not pop this access checks yet
7016 so the memory will not be reclaimed during token replacing below. */
7017 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7018 token->tree_check_p = true;
7019 token->u.tree_check_value->value = parser->scope;
7020 token->u.tree_check_value->checks = get_deferred_access_checks ();
7021 token->u.tree_check_value->qualifying_scope =
7022 parser->qualifying_scope;
7023 token->keyword = RID_MAX;
7025 /* Purge all subsequent tokens. */
7026 cp_lexer_purge_tokens_after (parser->lexer, start);
7029 if (start)
7030 pop_to_parent_deferring_access_checks ();
7032 return success ? parser->scope : NULL_TREE;
7035 /* Parse a nested-name-specifier. See
7036 cp_parser_nested_name_specifier_opt for details. This function
7037 behaves identically, except that it will an issue an error if no
7038 nested-name-specifier is present. */
7040 static tree
7041 cp_parser_nested_name_specifier (cp_parser *parser,
7042 bool typename_keyword_p,
7043 bool check_dependency_p,
7044 bool type_p,
7045 bool is_declaration)
7047 tree scope;
7049 /* Look for the nested-name-specifier. */
7050 scope = cp_parser_nested_name_specifier_opt (parser,
7051 typename_keyword_p,
7052 check_dependency_p,
7053 type_p,
7054 is_declaration);
7055 /* If it was not present, issue an error message. */
7056 if (!scope)
7058 cp_parser_error (parser, "expected nested-name-specifier");
7059 parser->scope = NULL_TREE;
7062 return scope;
7065 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7066 this is either a class-name or a namespace-name (which corresponds
7067 to the class-or-namespace-name production in the grammar). For
7068 C++0x, it can also be a type-name that refers to an enumeration
7069 type or a simple-template-id.
7071 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7072 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7073 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7074 TYPE_P is TRUE iff the next name should be taken as a class-name,
7075 even the same name is declared to be another entity in the same
7076 scope.
7078 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7079 specified by the class-or-namespace-name. If neither is found the
7080 ERROR_MARK_NODE is returned. */
7082 static tree
7083 cp_parser_qualifying_entity (cp_parser *parser,
7084 bool typename_keyword_p,
7085 bool template_keyword_p,
7086 bool check_dependency_p,
7087 bool type_p,
7088 bool is_declaration)
7090 tree saved_scope;
7091 tree saved_qualifying_scope;
7092 tree saved_object_scope;
7093 tree scope;
7094 bool only_class_p;
7095 bool successful_parse_p;
7097 /* DR 743: decltype can appear in a nested-name-specifier. */
7098 if (cp_lexer_next_token_is_decltype (parser->lexer))
7100 scope = cp_parser_decltype (parser);
7101 if (TREE_CODE (scope) != ENUMERAL_TYPE
7102 && !MAYBE_CLASS_TYPE_P (scope))
7104 cp_parser_simulate_error (parser);
7105 return error_mark_node;
7107 if (TYPE_NAME (scope))
7108 scope = TYPE_NAME (scope);
7109 return scope;
7112 /* Before we try to parse the class-name, we must save away the
7113 current PARSER->SCOPE since cp_parser_class_name will destroy
7114 it. */
7115 saved_scope = parser->scope;
7116 saved_qualifying_scope = parser->qualifying_scope;
7117 saved_object_scope = parser->object_scope;
7118 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7119 there is no need to look for a namespace-name. */
7120 only_class_p = template_keyword_p
7121 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7122 if (!only_class_p)
7123 cp_parser_parse_tentatively (parser);
7124 scope = cp_parser_class_name (parser,
7125 typename_keyword_p,
7126 template_keyword_p,
7127 type_p ? class_type : none_type,
7128 check_dependency_p,
7129 /*class_head_p=*/false,
7130 is_declaration,
7131 /*enum_ok=*/cxx_dialect > cxx98);
7132 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7133 /* If that didn't work, try for a namespace-name. */
7134 if (!only_class_p && !successful_parse_p)
7136 /* Restore the saved scope. */
7137 parser->scope = saved_scope;
7138 parser->qualifying_scope = saved_qualifying_scope;
7139 parser->object_scope = saved_object_scope;
7140 /* If we are not looking at an identifier followed by the scope
7141 resolution operator, then this is not part of a
7142 nested-name-specifier. (Note that this function is only used
7143 to parse the components of a nested-name-specifier.) */
7144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7145 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7146 return error_mark_node;
7147 scope = cp_parser_namespace_name (parser);
7150 return scope;
7153 /* Return true if we are looking at a compound-literal, false otherwise. */
7155 static bool
7156 cp_parser_compound_literal_p (cp_parser *parser)
7158 cp_lexer_save_tokens (parser->lexer);
7160 /* Skip tokens until the next token is a closing parenthesis.
7161 If we find the closing `)', and the next token is a `{', then
7162 we are looking at a compound-literal. */
7163 bool compound_literal_p
7164 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7165 /*consume_paren=*/true)
7166 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7168 /* Roll back the tokens we skipped. */
7169 cp_lexer_rollback_tokens (parser->lexer);
7171 return compound_literal_p;
7174 /* Return true if EXPR is the integer constant zero or a complex constant
7175 of zero, without any folding, but ignoring location wrappers. */
7177 bool
7178 literal_integer_zerop (const_tree expr)
7180 return (location_wrapper_p (expr)
7181 && integer_zerop (TREE_OPERAND (expr, 0)));
7184 /* Parse a postfix-expression.
7186 postfix-expression:
7187 primary-expression
7188 postfix-expression [ expression ]
7189 postfix-expression ( expression-list [opt] )
7190 simple-type-specifier ( expression-list [opt] )
7191 typename :: [opt] nested-name-specifier identifier
7192 ( expression-list [opt] )
7193 typename :: [opt] nested-name-specifier template [opt] template-id
7194 ( expression-list [opt] )
7195 postfix-expression . template [opt] id-expression
7196 postfix-expression -> template [opt] id-expression
7197 postfix-expression . pseudo-destructor-name
7198 postfix-expression -> pseudo-destructor-name
7199 postfix-expression ++
7200 postfix-expression --
7201 dynamic_cast < type-id > ( expression )
7202 static_cast < type-id > ( expression )
7203 reinterpret_cast < type-id > ( expression )
7204 const_cast < type-id > ( expression )
7205 typeid ( expression )
7206 typeid ( type-id )
7208 GNU Extension:
7210 postfix-expression:
7211 ( type-id ) { initializer-list , [opt] }
7213 This extension is a GNU version of the C99 compound-literal
7214 construct. (The C99 grammar uses `type-name' instead of `type-id',
7215 but they are essentially the same concept.)
7217 If ADDRESS_P is true, the postfix expression is the operand of the
7218 `&' operator. CAST_P is true if this expression is the target of a
7219 cast.
7221 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7222 class member access expressions [expr.ref].
7224 Returns a representation of the expression. */
7226 static cp_expr
7227 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7228 bool member_access_only_p, bool decltype_p,
7229 cp_id_kind * pidk_return)
7231 cp_token *token;
7232 location_t loc;
7233 enum rid keyword;
7234 cp_id_kind idk = CP_ID_KIND_NONE;
7235 cp_expr postfix_expression = NULL_TREE;
7236 bool is_member_access = false;
7238 /* Peek at the next token. */
7239 token = cp_lexer_peek_token (parser->lexer);
7240 loc = token->location;
7241 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7243 /* Some of the productions are determined by keywords. */
7244 keyword = token->keyword;
7245 switch (keyword)
7247 case RID_DYNCAST:
7248 case RID_STATCAST:
7249 case RID_REINTCAST:
7250 case RID_CONSTCAST:
7252 tree type;
7253 cp_expr expression;
7254 const char *saved_message;
7255 bool saved_in_type_id_in_expr_p;
7257 /* All of these can be handled in the same way from the point
7258 of view of parsing. Begin by consuming the token
7259 identifying the cast. */
7260 cp_lexer_consume_token (parser->lexer);
7262 /* New types cannot be defined in the cast. */
7263 saved_message = parser->type_definition_forbidden_message;
7264 parser->type_definition_forbidden_message
7265 = G_("types may not be defined in casts");
7267 /* Look for the opening `<'. */
7268 cp_parser_require (parser, CPP_LESS, RT_LESS);
7269 /* Parse the type to which we are casting. */
7270 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7271 parser->in_type_id_in_expr_p = true;
7272 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7273 NULL);
7274 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7275 /* Look for the closing `>'. */
7276 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7277 /* Restore the old message. */
7278 parser->type_definition_forbidden_message = saved_message;
7280 bool saved_greater_than_is_operator_p
7281 = parser->greater_than_is_operator_p;
7282 parser->greater_than_is_operator_p = true;
7284 /* And the expression which is being cast. */
7285 matching_parens parens;
7286 parens.require_open (parser);
7287 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7288 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7289 RT_CLOSE_PAREN);
7290 location_t end_loc = close_paren ?
7291 close_paren->location : UNKNOWN_LOCATION;
7293 parser->greater_than_is_operator_p
7294 = saved_greater_than_is_operator_p;
7296 /* Only type conversions to integral or enumeration types
7297 can be used in constant-expressions. */
7298 if (!cast_valid_in_integral_constant_expression_p (type)
7299 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7301 postfix_expression = error_mark_node;
7302 break;
7305 /* Construct a location e.g. :
7306 reinterpret_cast <int *> (expr)
7307 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7308 ranging from the start of the "*_cast" token to the final closing
7309 paren, with the caret at the start. */
7310 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7312 switch (keyword)
7314 case RID_DYNCAST:
7315 postfix_expression
7316 = build_dynamic_cast (cp_cast_loc, type, expression,
7317 tf_warning_or_error);
7318 break;
7319 case RID_STATCAST:
7320 postfix_expression
7321 = build_static_cast (cp_cast_loc, type, expression,
7322 tf_warning_or_error);
7323 break;
7324 case RID_REINTCAST:
7325 postfix_expression
7326 = build_reinterpret_cast (cp_cast_loc, type, expression,
7327 tf_warning_or_error);
7328 break;
7329 case RID_CONSTCAST:
7330 postfix_expression
7331 = build_const_cast (cp_cast_loc, type, expression,
7332 tf_warning_or_error);
7333 break;
7334 default:
7335 gcc_unreachable ();
7338 break;
7340 case RID_TYPEID:
7342 tree type;
7343 const char *saved_message;
7344 bool saved_in_type_id_in_expr_p;
7346 /* Consume the `typeid' token. */
7347 cp_lexer_consume_token (parser->lexer);
7348 /* Look for the `(' token. */
7349 matching_parens parens;
7350 parens.require_open (parser);
7351 /* Types cannot be defined in a `typeid' expression. */
7352 saved_message = parser->type_definition_forbidden_message;
7353 parser->type_definition_forbidden_message
7354 = G_("types may not be defined in a %<typeid%> expression");
7355 /* We can't be sure yet whether we're looking at a type-id or an
7356 expression. */
7357 cp_parser_parse_tentatively (parser);
7358 /* Try a type-id first. */
7359 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7360 parser->in_type_id_in_expr_p = true;
7361 type = cp_parser_type_id (parser);
7362 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7363 /* Look for the `)' token. Otherwise, we can't be sure that
7364 we're not looking at an expression: consider `typeid (int
7365 (3))', for example. */
7366 cp_token *close_paren = parens.require_close (parser);
7367 /* If all went well, simply lookup the type-id. */
7368 if (cp_parser_parse_definitely (parser))
7369 postfix_expression = get_typeid (type, tf_warning_or_error);
7370 /* Otherwise, fall back to the expression variant. */
7371 else
7373 tree expression;
7375 /* Look for an expression. */
7376 expression = cp_parser_expression (parser, & idk);
7377 /* Compute its typeid. */
7378 postfix_expression = build_typeid (expression, tf_warning_or_error);
7379 /* Look for the `)' token. */
7380 close_paren = parens.require_close (parser);
7382 /* Restore the saved message. */
7383 parser->type_definition_forbidden_message = saved_message;
7384 /* `typeid' may not appear in an integral constant expression. */
7385 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7386 postfix_expression = error_mark_node;
7388 /* Construct a location e.g. :
7389 typeid (expr)
7390 ^~~~~~~~~~~~~
7391 ranging from the start of the "typeid" token to the final closing
7392 paren, with the caret at the start. */
7393 if (close_paren)
7395 location_t typeid_loc
7396 = make_location (start_loc, start_loc, close_paren->location);
7397 postfix_expression.set_location (typeid_loc);
7398 postfix_expression.maybe_add_location_wrapper ();
7401 break;
7403 case RID_TYPENAME:
7405 tree type;
7406 /* The syntax permitted here is the same permitted for an
7407 elaborated-type-specifier. */
7408 ++parser->prevent_constrained_type_specifiers;
7409 type = cp_parser_elaborated_type_specifier (parser,
7410 /*is_friend=*/false,
7411 /*is_declaration=*/false);
7412 --parser->prevent_constrained_type_specifiers;
7413 postfix_expression = cp_parser_functional_cast (parser, type);
7415 break;
7417 case RID_ADDRESSOF:
7418 case RID_BUILTIN_SHUFFLE:
7419 case RID_BUILTIN_SHUFFLEVECTOR:
7420 case RID_BUILTIN_LAUNDER:
7421 case RID_BUILTIN_ASSOC_BARRIER:
7423 vec<tree, va_gc> *vec;
7425 cp_lexer_consume_token (parser->lexer);
7426 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7427 /*cast_p=*/false, /*allow_expansion_p=*/true,
7428 /*non_constant_p=*/NULL);
7429 if (vec == NULL)
7431 postfix_expression = error_mark_node;
7432 break;
7435 for (tree p : *vec)
7436 mark_exp_read (p);
7438 switch (keyword)
7440 case RID_ADDRESSOF:
7441 if (vec->length () == 1)
7442 postfix_expression
7443 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7444 else
7446 error_at (loc, "wrong number of arguments to "
7447 "%<__builtin_addressof%>");
7448 postfix_expression = error_mark_node;
7450 break;
7452 case RID_BUILTIN_LAUNDER:
7453 if (vec->length () == 1)
7454 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7455 tf_warning_or_error);
7456 else
7458 error_at (loc, "wrong number of arguments to "
7459 "%<__builtin_launder%>");
7460 postfix_expression = error_mark_node;
7462 break;
7464 case RID_BUILTIN_ASSOC_BARRIER:
7465 if (vec->length () == 1)
7466 postfix_expression = build1_loc (loc, PAREN_EXPR,
7467 TREE_TYPE ((*vec)[0]),
7468 (*vec)[0]);
7469 else
7471 error_at (loc, "wrong number of arguments to "
7472 "%<__builtin_assoc_barrier%>");
7473 postfix_expression = error_mark_node;
7475 break;
7477 case RID_BUILTIN_SHUFFLE:
7478 if (vec->length () == 2)
7479 postfix_expression
7480 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7481 (*vec)[1], tf_warning_or_error);
7482 else if (vec->length () == 3)
7483 postfix_expression
7484 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7485 (*vec)[2], tf_warning_or_error);
7486 else
7488 error_at (loc, "wrong number of arguments to "
7489 "%<__builtin_shuffle%>");
7490 postfix_expression = error_mark_node;
7492 break;
7494 case RID_BUILTIN_SHUFFLEVECTOR:
7495 if (vec->length () < 3)
7497 error_at (loc, "wrong number of arguments to "
7498 "%<__builtin_shufflevector%>");
7499 postfix_expression = error_mark_node;
7501 else
7503 postfix_expression
7504 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7506 break;
7508 default:
7509 gcc_unreachable ();
7511 break;
7514 case RID_BUILTIN_CONVERTVECTOR:
7516 tree expression;
7517 tree type;
7518 /* Consume the `__builtin_convertvector' token. */
7519 cp_lexer_consume_token (parser->lexer);
7520 /* Look for the opening `('. */
7521 matching_parens parens;
7522 parens.require_open (parser);
7523 /* Now, parse the assignment-expression. */
7524 expression = cp_parser_assignment_expression (parser);
7525 /* Look for the `,'. */
7526 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7527 location_t type_location
7528 = cp_lexer_peek_token (parser->lexer)->location;
7529 /* Parse the type-id. */
7531 type_id_in_expr_sentinel s (parser);
7532 type = cp_parser_type_id (parser);
7534 /* Look for the closing `)'. */
7535 parens.require_close (parser);
7536 postfix_expression
7537 = cp_build_vec_convert (expression, type_location, type,
7538 tf_warning_or_error);
7539 break;
7542 case RID_BUILTIN_BIT_CAST:
7544 tree expression;
7545 tree type;
7546 /* Consume the `__builtin_bit_cast' token. */
7547 cp_lexer_consume_token (parser->lexer);
7548 /* Look for the opening `('. */
7549 matching_parens parens;
7550 parens.require_open (parser);
7551 location_t type_location
7552 = cp_lexer_peek_token (parser->lexer)->location;
7553 /* Parse the type-id. */
7555 type_id_in_expr_sentinel s (parser);
7556 type = cp_parser_type_id (parser);
7558 /* Look for the `,'. */
7559 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7560 /* Now, parse the assignment-expression. */
7561 expression = cp_parser_assignment_expression (parser);
7562 /* Look for the closing `)'. */
7563 parens.require_close (parser);
7564 postfix_expression
7565 = cp_build_bit_cast (type_location, type, expression,
7566 tf_warning_or_error);
7567 break;
7570 default:
7572 tree type;
7574 /* If the next thing is a simple-type-specifier, we may be
7575 looking at a functional cast. We could also be looking at
7576 an id-expression. So, we try the functional cast, and if
7577 that doesn't work we fall back to the primary-expression. */
7578 cp_parser_parse_tentatively (parser);
7579 /* Look for the simple-type-specifier. */
7580 ++parser->prevent_constrained_type_specifiers;
7581 type = cp_parser_simple_type_specifier (parser,
7582 /*decl_specs=*/NULL,
7583 CP_PARSER_FLAGS_NONE);
7584 --parser->prevent_constrained_type_specifiers;
7585 /* Parse the cast itself. */
7586 if (!cp_parser_error_occurred (parser))
7587 postfix_expression
7588 = cp_parser_functional_cast (parser, type);
7589 /* If that worked, we're done. */
7590 if (cp_parser_parse_definitely (parser))
7591 break;
7593 /* If the functional-cast didn't work out, try a
7594 compound-literal. */
7595 if (cp_parser_allow_gnu_extensions_p (parser)
7596 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7598 cp_expr initializer = NULL_TREE;
7600 cp_parser_parse_tentatively (parser);
7602 matching_parens parens;
7603 parens.consume_open (parser);
7605 /* Avoid calling cp_parser_type_id pointlessly, see comment
7606 in cp_parser_cast_expression about c++/29234. */
7607 if (!cp_parser_compound_literal_p (parser))
7608 cp_parser_simulate_error (parser);
7609 else
7611 /* Parse the type. */
7612 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7613 parser->in_type_id_in_expr_p = true;
7614 type = cp_parser_type_id (parser);
7615 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7616 parens.require_close (parser);
7619 /* If things aren't going well, there's no need to
7620 keep going. */
7621 if (!cp_parser_error_occurred (parser))
7623 bool non_constant_p;
7624 /* Parse the brace-enclosed initializer list. */
7625 initializer = cp_parser_braced_list (parser,
7626 &non_constant_p);
7628 /* If that worked, we're definitely looking at a
7629 compound-literal expression. */
7630 if (cp_parser_parse_definitely (parser))
7632 /* Warn the user that a compound literal is not
7633 allowed in standard C++. */
7634 pedwarn (input_location, OPT_Wpedantic,
7635 "ISO C++ forbids compound-literals");
7636 /* For simplicity, we disallow compound literals in
7637 constant-expressions. We could
7638 allow compound literals of integer type, whose
7639 initializer was a constant, in constant
7640 expressions. Permitting that usage, as a further
7641 extension, would not change the meaning of any
7642 currently accepted programs. (Of course, as
7643 compound literals are not part of ISO C++, the
7644 standard has nothing to say.) */
7645 if (cp_parser_non_integral_constant_expression (parser,
7646 NIC_NCC))
7648 postfix_expression = error_mark_node;
7649 break;
7651 /* Form the representation of the compound-literal. */
7652 postfix_expression
7653 = finish_compound_literal (type, initializer,
7654 tf_warning_or_error, fcl_c99);
7655 postfix_expression.set_location (initializer.get_location ());
7656 break;
7660 /* It must be a primary-expression. */
7661 postfix_expression
7662 = cp_parser_primary_expression (parser, address_p, cast_p,
7663 /*template_arg_p=*/false,
7664 decltype_p,
7665 &idk);
7667 break;
7670 /* Note that we don't need to worry about calling build_cplus_new on a
7671 class-valued CALL_EXPR in decltype when it isn't the end of the
7672 postfix-expression; unary_complex_lvalue will take care of that for
7673 all these cases. */
7675 /* Keep looping until the postfix-expression is complete. */
7676 while (true)
7678 if (idk == CP_ID_KIND_UNQUALIFIED
7679 && identifier_p (postfix_expression)
7680 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7681 /* It is not a Koenig lookup function call. */
7682 postfix_expression
7683 = unqualified_name_lookup_error (postfix_expression);
7685 /* Peek at the next token. */
7686 token = cp_lexer_peek_token (parser->lexer);
7688 switch (token->type)
7690 case CPP_OPEN_SQUARE:
7691 if (cp_next_tokens_can_be_std_attribute_p (parser))
7693 cp_parser_error (parser,
7694 "two consecutive %<[%> shall "
7695 "only introduce an attribute");
7696 return error_mark_node;
7698 postfix_expression
7699 = cp_parser_postfix_open_square_expression (parser,
7700 postfix_expression,
7701 false,
7702 decltype_p);
7703 postfix_expression.set_range (start_loc,
7704 postfix_expression.get_location ());
7706 idk = CP_ID_KIND_NONE;
7707 is_member_access = false;
7708 break;
7710 case CPP_OPEN_PAREN:
7711 /* postfix-expression ( expression-list [opt] ) */
7713 bool koenig_p;
7714 bool is_builtin_constant_p;
7715 bool saved_integral_constant_expression_p = false;
7716 bool saved_non_integral_constant_expression_p = false;
7717 tsubst_flags_t complain = complain_flags (decltype_p);
7718 vec<tree, va_gc> *args;
7719 location_t close_paren_loc = UNKNOWN_LOCATION;
7720 location_t combined_loc = UNKNOWN_LOCATION;
7722 is_member_access = false;
7724 tree stripped_expression
7725 = tree_strip_any_location_wrapper (postfix_expression);
7726 is_builtin_constant_p
7727 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7728 if (is_builtin_constant_p)
7730 /* The whole point of __builtin_constant_p is to allow
7731 non-constant expressions to appear as arguments. */
7732 saved_integral_constant_expression_p
7733 = parser->integral_constant_expression_p;
7734 saved_non_integral_constant_expression_p
7735 = parser->non_integral_constant_expression_p;
7736 parser->integral_constant_expression_p = false;
7738 args = (cp_parser_parenthesized_expression_list
7739 (parser, non_attr,
7740 /*cast_p=*/false, /*allow_expansion_p=*/true,
7741 /*non_constant_p=*/NULL,
7742 /*close_paren_loc=*/&close_paren_loc,
7743 /*wrap_locations_p=*/true));
7744 if (is_builtin_constant_p)
7746 parser->integral_constant_expression_p
7747 = saved_integral_constant_expression_p;
7748 parser->non_integral_constant_expression_p
7749 = saved_non_integral_constant_expression_p;
7752 if (args == NULL)
7754 postfix_expression = error_mark_node;
7755 break;
7758 /* Function calls are not permitted in
7759 constant-expressions. */
7760 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7761 && cp_parser_non_integral_constant_expression (parser,
7762 NIC_FUNC_CALL))
7764 postfix_expression = error_mark_node;
7765 release_tree_vector (args);
7766 break;
7769 koenig_p = false;
7770 if (idk == CP_ID_KIND_UNQUALIFIED
7771 || idk == CP_ID_KIND_TEMPLATE_ID)
7773 if (identifier_p (postfix_expression)
7774 /* In C++20, we may need to perform ADL for a template
7775 name. */
7776 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7777 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7779 if (!args->is_empty ())
7781 koenig_p = true;
7782 if (!any_type_dependent_arguments_p (args))
7783 postfix_expression
7784 = perform_koenig_lookup (postfix_expression, args,
7785 complain);
7787 else
7788 postfix_expression
7789 = unqualified_fn_lookup_error (postfix_expression);
7791 /* We do not perform argument-dependent lookup if
7792 normal lookup finds a non-function, in accordance
7793 with the expected resolution of DR 218. */
7794 else if (!args->is_empty ()
7795 && is_overloaded_fn (postfix_expression))
7797 /* Do not do argument dependent lookup if regular
7798 lookup finds a member function or a block-scope
7799 function declaration. [basic.lookup.argdep]/3 */
7800 bool do_adl_p = true;
7801 tree fns = get_fns (postfix_expression);
7802 for (lkp_iterator iter (fns); iter; ++iter)
7804 tree fn = STRIP_TEMPLATE (*iter);
7805 if ((TREE_CODE (fn) == USING_DECL
7806 && DECL_DEPENDENT_P (fn))
7807 || DECL_FUNCTION_MEMBER_P (fn)
7808 || DECL_LOCAL_DECL_P (fn))
7810 do_adl_p = false;
7811 break;
7815 if (do_adl_p)
7817 koenig_p = true;
7818 if (!any_type_dependent_arguments_p (args))
7819 postfix_expression
7820 = perform_koenig_lookup (postfix_expression, args,
7821 complain);
7826 /* Temporarily set input_location to the combined location
7827 with call expression range, as e.g. build_out_target_exprs
7828 called from convert_default_arg relies on input_location,
7829 so updating it only when the call is fully built results
7830 in inconsistencies between location handling in templates
7831 and outside of templates. */
7832 if (close_paren_loc != UNKNOWN_LOCATION)
7833 combined_loc = make_location (token->location, start_loc,
7834 close_paren_loc);
7835 iloc_sentinel ils (combined_loc);
7837 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7839 tree instance = TREE_OPERAND (postfix_expression, 0);
7840 tree fn = TREE_OPERAND (postfix_expression, 1);
7842 if (processing_template_decl
7843 && (type_dependent_object_expression_p (instance)
7844 || (!BASELINK_P (fn)
7845 && TREE_CODE (fn) != FIELD_DECL)
7846 || type_dependent_expression_p (fn)
7847 || any_type_dependent_arguments_p (args)))
7849 maybe_generic_this_capture (instance, fn);
7850 postfix_expression
7851 = build_min_nt_call_vec (postfix_expression, args);
7853 else if (BASELINK_P (fn))
7855 postfix_expression
7856 = (build_new_method_call
7857 (instance, fn, &args, NULL_TREE,
7858 (idk == CP_ID_KIND_QUALIFIED
7859 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7860 : LOOKUP_NORMAL),
7861 /*fn_p=*/NULL,
7862 complain));
7864 else
7865 postfix_expression
7866 = finish_call_expr (postfix_expression, &args,
7867 /*disallow_virtual=*/false,
7868 /*koenig_p=*/false,
7869 complain);
7871 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7872 || TREE_CODE (postfix_expression) == MEMBER_REF
7873 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7874 postfix_expression = (build_offset_ref_call_from_tree
7875 (postfix_expression, &args,
7876 complain));
7877 else if (idk == CP_ID_KIND_QUALIFIED)
7878 /* A call to a static class member, or a namespace-scope
7879 function. */
7880 postfix_expression
7881 = finish_call_expr (postfix_expression, &args,
7882 /*disallow_virtual=*/true,
7883 koenig_p,
7884 complain);
7885 else
7886 /* All other function calls. */
7887 postfix_expression
7888 = finish_call_expr (postfix_expression, &args,
7889 /*disallow_virtual=*/false,
7890 koenig_p,
7891 complain);
7893 if (close_paren_loc != UNKNOWN_LOCATION)
7894 postfix_expression.set_location (combined_loc);
7896 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7897 idk = CP_ID_KIND_NONE;
7899 release_tree_vector (args);
7901 break;
7903 case CPP_DOT:
7904 case CPP_DEREF:
7905 /* postfix-expression . template [opt] id-expression
7906 postfix-expression . pseudo-destructor-name
7907 postfix-expression -> template [opt] id-expression
7908 postfix-expression -> pseudo-destructor-name */
7910 /* Consume the `.' or `->' operator. */
7911 cp_lexer_consume_token (parser->lexer);
7913 postfix_expression
7914 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7915 postfix_expression,
7916 false, &idk, loc);
7918 is_member_access = true;
7919 break;
7921 case CPP_PLUS_PLUS:
7922 /* postfix-expression ++ */
7923 /* Consume the `++' token. */
7924 cp_lexer_consume_token (parser->lexer);
7925 /* Generate a representation for the complete expression. */
7926 postfix_expression
7927 = finish_increment_expr (postfix_expression,
7928 POSTINCREMENT_EXPR);
7929 /* Increments may not appear in constant-expressions. */
7930 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7931 postfix_expression = error_mark_node;
7932 idk = CP_ID_KIND_NONE;
7933 is_member_access = false;
7934 break;
7936 case CPP_MINUS_MINUS:
7937 /* postfix-expression -- */
7938 /* Consume the `--' token. */
7939 cp_lexer_consume_token (parser->lexer);
7940 /* Generate a representation for the complete expression. */
7941 postfix_expression
7942 = finish_increment_expr (postfix_expression,
7943 POSTDECREMENT_EXPR);
7944 /* Decrements may not appear in constant-expressions. */
7945 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7946 postfix_expression = error_mark_node;
7947 idk = CP_ID_KIND_NONE;
7948 is_member_access = false;
7949 break;
7951 default:
7952 if (pidk_return != NULL)
7953 * pidk_return = idk;
7954 if (member_access_only_p)
7955 return is_member_access
7956 ? postfix_expression
7957 : cp_expr (error_mark_node);
7958 else
7959 return postfix_expression;
7964 /* Helper function for cp_parser_parenthesized_expression_list and
7965 cp_parser_postfix_open_square_expression. Parse a single element
7966 of parenthesized expression list. */
7968 static cp_expr
7969 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
7970 bool allow_expansion_p,
7971 bool *non_constant_p)
7973 cp_expr expr (NULL_TREE);
7974 bool expr_non_constant_p;
7976 /* Parse the next assignment-expression. */
7977 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7979 /* A braced-init-list. */
7980 cp_lexer_set_source_position (parser->lexer);
7981 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7982 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7983 if (non_constant_p && expr_non_constant_p)
7984 *non_constant_p = true;
7986 else if (non_constant_p)
7988 expr = cp_parser_constant_expression (parser,
7989 /*allow_non_constant_p=*/true,
7990 &expr_non_constant_p);
7991 if (expr_non_constant_p)
7992 *non_constant_p = true;
7994 else
7995 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
7997 /* If we have an ellipsis, then this is an expression expansion. */
7998 if (allow_expansion_p
7999 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8001 /* Consume the `...'. */
8002 cp_lexer_consume_token (parser->lexer);
8004 /* Build the argument pack. */
8005 expr = make_pack_expansion (expr);
8007 return expr;
8010 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8011 by cp_parser_builtin_offsetof. We're looking for
8013 postfix-expression [ expression ]
8014 postfix-expression [ braced-init-list ] (C++11)
8015 postfix-expression [ expression-list[opt] ] (C++23)
8017 FOR_OFFSETOF is set if we're being called in that context, which
8018 changes how we deal with integer constant expressions. */
8020 static tree
8021 cp_parser_postfix_open_square_expression (cp_parser *parser,
8022 tree postfix_expression,
8023 bool for_offsetof,
8024 bool decltype_p)
8026 tree index = NULL_TREE;
8027 releasing_vec expression_list = NULL;
8028 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8029 bool saved_greater_than_is_operator_p;
8031 /* Consume the `[' token. */
8032 cp_lexer_consume_token (parser->lexer);
8034 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8035 parser->greater_than_is_operator_p = true;
8037 /* Parse the index expression. */
8038 /* ??? For offsetof, there is a question of what to allow here. If
8039 offsetof is not being used in an integral constant expression context,
8040 then we *could* get the right answer by computing the value at runtime.
8041 If we are in an integral constant expression context, then we might
8042 could accept any constant expression; hard to say without analysis.
8043 Rather than open the barn door too wide right away, allow only integer
8044 constant expressions here. */
8045 if (for_offsetof)
8046 index = cp_parser_constant_expression (parser);
8047 else
8049 if (cxx_dialect >= cxx23
8050 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8051 *&expression_list = make_tree_vector ();
8052 else if (cxx_dialect >= cxx23)
8054 while (true)
8056 cp_expr expr
8057 = cp_parser_parenthesized_expression_list_elt (parser,
8058 /*cast_p=*/
8059 false,
8060 /*allow_exp_p=*/
8061 true,
8062 /*non_cst_p=*/
8063 NULL);
8065 if (expr == error_mark_node)
8066 index = error_mark_node;
8067 else if (expression_list.get () == NULL
8068 && !PACK_EXPANSION_P (expr.get_value ()))
8069 index = expr.get_value ();
8070 else
8071 vec_safe_push (expression_list, expr.get_value ());
8073 /* If the next token isn't a `,', then we are done. */
8074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8075 break;
8077 if (expression_list.get () == NULL && index != error_mark_node)
8079 *&expression_list = make_tree_vector_single (index);
8080 index = NULL_TREE;
8083 /* Otherwise, consume the `,' and keep going. */
8084 cp_lexer_consume_token (parser->lexer);
8086 if (expression_list.get () && index == error_mark_node)
8087 expression_list.release ();
8089 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8091 bool expr_nonconst_p;
8092 cp_lexer_set_source_position (parser->lexer);
8093 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8094 index = cp_parser_braced_list (parser, &expr_nonconst_p);
8096 else
8097 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8098 /*decltype_p=*/false,
8099 /*warn_comma_p=*/warn_comma_subscript);
8102 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8104 /* Look for the closing `]'. */
8105 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8107 /* Build the ARRAY_REF. */
8108 postfix_expression = grok_array_decl (loc, postfix_expression,
8109 index, &expression_list,
8110 tf_warning_or_error
8111 | (decltype_p ? tf_decltype : 0));
8113 /* When not doing offsetof, array references are not permitted in
8114 constant-expressions. */
8115 if (!for_offsetof
8116 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8117 postfix_expression = error_mark_node;
8119 return postfix_expression;
8122 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8123 dereference of incomplete type, returns true if error_mark_node should
8124 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8125 and *DEPENDENT_P. */
8127 bool
8128 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8129 bool *dependent_p)
8131 /* In a template, be permissive by treating an object expression
8132 of incomplete type as dependent (after a pedwarn). */
8133 diagnostic_t kind = (processing_template_decl
8134 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8136 switch (TREE_CODE (*postfix_expression))
8138 case CAST_EXPR:
8139 case REINTERPRET_CAST_EXPR:
8140 case CONST_CAST_EXPR:
8141 case STATIC_CAST_EXPR:
8142 case DYNAMIC_CAST_EXPR:
8143 case IMPLICIT_CONV_EXPR:
8144 case VIEW_CONVERT_EXPR:
8145 case NON_LVALUE_EXPR:
8146 kind = DK_ERROR;
8147 break;
8148 case OVERLOAD:
8149 /* Don't emit any diagnostic for OVERLOADs. */
8150 kind = DK_IGNORED;
8151 break;
8152 default:
8153 /* Avoid clobbering e.g. DECLs. */
8154 if (!EXPR_P (*postfix_expression))
8155 kind = DK_ERROR;
8156 break;
8159 if (kind == DK_IGNORED)
8160 return false;
8162 location_t exploc = location_of (*postfix_expression);
8163 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8164 if (!MAYBE_CLASS_TYPE_P (*scope))
8165 return true;
8166 if (kind == DK_ERROR)
8167 *scope = *postfix_expression = error_mark_node;
8168 else if (processing_template_decl)
8170 *dependent_p = true;
8171 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8173 return false;
8176 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8177 by cp_parser_builtin_offsetof. We're looking for
8179 postfix-expression . template [opt] id-expression
8180 postfix-expression . pseudo-destructor-name
8181 postfix-expression -> template [opt] id-expression
8182 postfix-expression -> pseudo-destructor-name
8184 FOR_OFFSETOF is set if we're being called in that context. That sorta
8185 limits what of the above we'll actually accept, but nevermind.
8186 TOKEN_TYPE is the "." or "->" token, which will already have been
8187 removed from the stream. */
8189 static tree
8190 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8191 enum cpp_ttype token_type,
8192 cp_expr postfix_expression,
8193 bool for_offsetof, cp_id_kind *idk,
8194 location_t location)
8196 tree name;
8197 bool dependent_p;
8198 bool pseudo_destructor_p;
8199 tree scope = NULL_TREE;
8200 location_t start_loc = postfix_expression.get_start ();
8202 /* If this is a `->' operator, dereference the pointer. */
8203 if (token_type == CPP_DEREF)
8204 postfix_expression = build_x_arrow (location, postfix_expression,
8205 tf_warning_or_error);
8206 /* Check to see whether or not the expression is type-dependent and
8207 not the current instantiation. */
8208 dependent_p = type_dependent_object_expression_p (postfix_expression);
8209 /* The identifier following the `->' or `.' is not qualified. */
8210 parser->scope = NULL_TREE;
8211 parser->qualifying_scope = NULL_TREE;
8212 parser->object_scope = NULL_TREE;
8213 *idk = CP_ID_KIND_NONE;
8215 /* Enter the scope corresponding to the type of the object
8216 given by the POSTFIX_EXPRESSION. */
8217 if (!dependent_p)
8219 scope = TREE_TYPE (postfix_expression);
8220 /* According to the standard, no expression should ever have
8221 reference type. Unfortunately, we do not currently match
8222 the standard in this respect in that our internal representation
8223 of an expression may have reference type even when the standard
8224 says it does not. Therefore, we have to manually obtain the
8225 underlying type here. */
8226 scope = non_reference (scope);
8227 /* The type of the POSTFIX_EXPRESSION must be complete. */
8228 /* Unlike the object expression in other contexts, *this is not
8229 required to be of complete type for purposes of class member
8230 access (5.2.5) outside the member function body. */
8231 if (postfix_expression != current_class_ref
8232 && scope != error_mark_node
8233 && !currently_open_class (scope))
8235 scope = complete_type (scope);
8236 if (!COMPLETE_TYPE_P (scope)
8237 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8238 &dependent_p))
8239 return error_mark_node;
8242 if (!dependent_p)
8244 /* Let the name lookup machinery know that we are processing a
8245 class member access expression. */
8246 parser->context->object_type = scope;
8247 /* If something went wrong, we want to be able to discern that case,
8248 as opposed to the case where there was no SCOPE due to the type
8249 of expression being dependent. */
8250 if (!scope)
8251 scope = error_mark_node;
8252 /* If the SCOPE was erroneous, make the various semantic analysis
8253 functions exit quickly -- and without issuing additional error
8254 messages. */
8255 if (scope == error_mark_node)
8256 postfix_expression = error_mark_node;
8260 if (dependent_p)
8262 tree type = TREE_TYPE (postfix_expression);
8263 /* If we don't have a (type-dependent) object of class type, use
8264 typeof to figure out the type of the object. */
8265 if (type == NULL_TREE)
8266 type = finish_typeof (postfix_expression);
8267 parser->context->object_type = type;
8270 /* Assume this expression is not a pseudo-destructor access. */
8271 pseudo_destructor_p = false;
8273 /* If the SCOPE is a scalar type, then, if this is a valid program,
8274 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8275 is type dependent, it can be pseudo-destructor-name or something else.
8276 Try to parse it as pseudo-destructor-name first. */
8277 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8279 tree s;
8280 tree type;
8282 cp_parser_parse_tentatively (parser);
8283 /* Parse the pseudo-destructor-name. */
8284 s = NULL_TREE;
8285 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8286 &s, &type);
8287 if (dependent_p
8288 && (cp_parser_error_occurred (parser)
8289 || !SCALAR_TYPE_P (type)))
8290 cp_parser_abort_tentative_parse (parser);
8291 else if (cp_parser_parse_definitely (parser))
8293 pseudo_destructor_p = true;
8294 postfix_expression
8295 = finish_pseudo_destructor_expr (postfix_expression,
8296 s, type, location);
8300 if (!pseudo_destructor_p)
8302 /* If the SCOPE is not a scalar type, we are looking at an
8303 ordinary class member access expression, rather than a
8304 pseudo-destructor-name. */
8305 bool template_p;
8306 cp_token *token = cp_lexer_peek_token (parser->lexer);
8307 /* Parse the id-expression. */
8308 name = (cp_parser_id_expression
8309 (parser,
8310 cp_parser_optional_template_keyword (parser),
8311 /*check_dependency_p=*/true,
8312 &template_p,
8313 /*declarator_p=*/false,
8314 /*optional_p=*/false));
8315 /* In general, build a SCOPE_REF if the member name is qualified.
8316 However, if the name was not dependent and has already been
8317 resolved; there is no need to build the SCOPE_REF. For example;
8319 struct X { void f(); };
8320 template <typename T> void f(T* t) { t->X::f(); }
8322 Even though "t" is dependent, "X::f" is not and has been resolved
8323 to a BASELINK; there is no need to include scope information. */
8325 /* But we do need to remember that there was an explicit scope for
8326 virtual function calls. */
8327 if (parser->scope)
8328 *idk = CP_ID_KIND_QUALIFIED;
8330 /* If the name is a template-id that names a type, we will get a
8331 TYPE_DECL here. That is invalid code. */
8332 if (TREE_CODE (name) == TYPE_DECL)
8334 error_at (token->location, "invalid use of %qD", name);
8335 postfix_expression = error_mark_node;
8337 else
8339 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8341 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8343 error_at (token->location, "%<%D::%D%> is not a class member",
8344 parser->scope, name);
8345 postfix_expression = error_mark_node;
8347 else
8348 name = build_qualified_name (/*type=*/NULL_TREE,
8349 parser->scope,
8350 name,
8351 template_p);
8352 parser->scope = NULL_TREE;
8353 parser->qualifying_scope = NULL_TREE;
8354 parser->object_scope = NULL_TREE;
8356 if (parser->scope && name && BASELINK_P (name))
8357 adjust_result_of_qualified_name_lookup
8358 (name, parser->scope, scope);
8359 postfix_expression
8360 = finish_class_member_access_expr (postfix_expression, name,
8361 template_p,
8362 tf_warning_or_error);
8363 /* Build a location e.g.:
8364 ptr->access_expr
8365 ~~~^~~~~~~~~~~~~
8366 where the caret is at the deref token, ranging from
8367 the start of postfix_expression to the end of the access expr. */
8368 location_t combined_loc
8369 = make_location (input_location, start_loc, parser->lexer);
8370 protected_set_expr_location (postfix_expression, combined_loc);
8374 /* We no longer need to look up names in the scope of the object on
8375 the left-hand side of the `.' or `->' operator. */
8376 parser->context->object_type = NULL_TREE;
8378 /* Outside of offsetof, these operators may not appear in
8379 constant-expressions. */
8380 if (!for_offsetof
8381 && (cp_parser_non_integral_constant_expression
8382 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8383 postfix_expression = error_mark_node;
8385 return postfix_expression;
8388 /* Parse a parenthesized expression-list.
8390 expression-list:
8391 assignment-expression
8392 expression-list, assignment-expression
8394 attribute-list:
8395 expression-list
8396 identifier
8397 identifier, expression-list
8399 CAST_P is true if this expression is the target of a cast.
8401 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8402 argument pack.
8404 WRAP_LOCATIONS_P is true if expressions within this list for which
8405 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8406 their source locations.
8408 Returns a vector of trees. Each element is a representation of an
8409 assignment-expression. NULL is returned if the ( and or ) are
8410 missing. An empty, but allocated, vector is returned on no
8411 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8412 if we are parsing an attribute list for an attribute that wants a
8413 plain identifier argument, normal_attr for an attribute that wants
8414 an expression, or non_attr if we aren't parsing an attribute list. If
8415 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8416 not all of the expressions in the list were constant.
8417 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8418 will be written to with the location of the closing parenthesis. If
8419 an error occurs, it may or may not be written to. */
8421 static vec<tree, va_gc> *
8422 cp_parser_parenthesized_expression_list (cp_parser* parser,
8423 int is_attribute_list,
8424 bool cast_p,
8425 bool allow_expansion_p,
8426 bool *non_constant_p,
8427 location_t *close_paren_loc,
8428 bool wrap_locations_p)
8430 vec<tree, va_gc> *expression_list;
8431 tree identifier = NULL_TREE;
8432 bool saved_greater_than_is_operator_p;
8434 /* Assume all the expressions will be constant. */
8435 if (non_constant_p)
8436 *non_constant_p = false;
8438 matching_parens parens;
8439 if (!parens.require_open (parser))
8440 return NULL;
8442 expression_list = make_tree_vector ();
8444 /* Within a parenthesized expression, a `>' token is always
8445 the greater-than operator. */
8446 saved_greater_than_is_operator_p
8447 = parser->greater_than_is_operator_p;
8448 parser->greater_than_is_operator_p = true;
8450 cp_expr expr (NULL_TREE);
8452 /* Consume expressions until there are no more. */
8453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8454 while (true)
8456 /* At the beginning of attribute lists, check to see if the
8457 next token is an identifier. */
8458 if (is_attribute_list == id_attr
8459 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8461 cp_token *token;
8463 /* Consume the identifier. */
8464 token = cp_lexer_consume_token (parser->lexer);
8465 /* Save the identifier. */
8466 identifier = token->u.value;
8468 else
8470 expr
8471 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8472 allow_expansion_p,
8473 non_constant_p);
8475 if (wrap_locations_p)
8476 expr.maybe_add_location_wrapper ();
8478 /* Add it to the list. We add error_mark_node
8479 expressions to the list, so that we can still tell if
8480 the correct form for a parenthesized expression-list
8481 is found. That gives better errors. */
8482 vec_safe_push (expression_list, expr.get_value ());
8484 if (expr == error_mark_node)
8485 goto skip_comma;
8488 /* After the first item, attribute lists look the same as
8489 expression lists. */
8490 is_attribute_list = non_attr;
8492 get_comma:;
8493 /* If the next token isn't a `,', then we are done. */
8494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8495 break;
8497 /* Otherwise, consume the `,' and keep going. */
8498 cp_lexer_consume_token (parser->lexer);
8501 if (close_paren_loc)
8502 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8504 if (!parens.require_close (parser))
8506 int ending;
8508 skip_comma:;
8509 /* We try and resync to an unnested comma, as that will give the
8510 user better diagnostics. */
8511 ending = cp_parser_skip_to_closing_parenthesis (parser,
8512 /*recovering=*/true,
8513 /*or_comma=*/true,
8514 /*consume_paren=*/true);
8515 if (ending < 0)
8516 goto get_comma;
8517 if (!ending)
8519 parser->greater_than_is_operator_p
8520 = saved_greater_than_is_operator_p;
8521 return NULL;
8525 parser->greater_than_is_operator_p
8526 = saved_greater_than_is_operator_p;
8528 if (identifier)
8529 vec_safe_insert (expression_list, 0, identifier);
8531 return expression_list;
8534 /* Parse a pseudo-destructor-name.
8536 pseudo-destructor-name:
8537 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8538 :: [opt] nested-name-specifier template template-id :: ~ type-name
8539 :: [opt] nested-name-specifier [opt] ~ type-name
8541 If either of the first two productions is used, sets *SCOPE to the
8542 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8543 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8544 or ERROR_MARK_NODE if the parse fails. */
8546 static void
8547 cp_parser_pseudo_destructor_name (cp_parser* parser,
8548 tree object,
8549 tree* scope,
8550 tree* type)
8552 bool nested_name_specifier_p;
8554 /* Handle ~auto. */
8555 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8556 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8557 && !type_dependent_expression_p (object))
8559 if (cxx_dialect < cxx14)
8560 pedwarn (input_location, OPT_Wc__14_extensions,
8561 "%<~auto%> only available with "
8562 "%<-std=c++14%> or %<-std=gnu++14%>");
8563 cp_lexer_consume_token (parser->lexer);
8564 cp_lexer_consume_token (parser->lexer);
8565 *scope = NULL_TREE;
8566 *type = TREE_TYPE (object);
8567 return;
8570 /* Assume that things will not work out. */
8571 *type = error_mark_node;
8573 /* Look for the optional `::' operator. */
8574 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8575 /* Look for the optional nested-name-specifier. */
8576 nested_name_specifier_p
8577 = (cp_parser_nested_name_specifier_opt (parser,
8578 /*typename_keyword_p=*/false,
8579 /*check_dependency_p=*/true,
8580 /*type_p=*/false,
8581 /*is_declaration=*/false)
8582 != NULL_TREE);
8583 /* Now, if we saw a nested-name-specifier, we might be doing the
8584 second production. */
8585 if (nested_name_specifier_p
8586 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8588 /* Consume the `template' keyword. */
8589 cp_lexer_consume_token (parser->lexer);
8590 /* Parse the template-id. */
8591 cp_parser_template_id (parser,
8592 /*template_keyword_p=*/true,
8593 /*check_dependency_p=*/false,
8594 class_type,
8595 /*is_declaration=*/true);
8596 /* Look for the `::' token. */
8597 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8599 /* If the next token is not a `~', then there might be some
8600 additional qualification. */
8601 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8603 /* At this point, we're looking for "type-name :: ~". The type-name
8604 must not be a class-name, since this is a pseudo-destructor. So,
8605 it must be either an enum-name, or a typedef-name -- both of which
8606 are just identifiers. So, we peek ahead to check that the "::"
8607 and "~" tokens are present; if they are not, then we can avoid
8608 calling type_name. */
8609 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8610 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8611 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8613 cp_parser_error (parser, "non-scalar type");
8614 return;
8617 /* Look for the type-name. */
8618 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8619 if (*scope == error_mark_node)
8620 return;
8622 /* Look for the `::' token. */
8623 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8625 else
8626 *scope = NULL_TREE;
8628 /* Look for the `~'. */
8629 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8631 /* Once we see the ~, this has to be a pseudo-destructor. */
8632 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8633 cp_parser_commit_to_topmost_tentative_parse (parser);
8635 /* Look for the type-name again. We are not responsible for
8636 checking that it matches the first type-name. */
8637 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8640 /* Parse a unary-expression.
8642 unary-expression:
8643 postfix-expression
8644 ++ cast-expression
8645 -- cast-expression
8646 await-expression
8647 unary-operator cast-expression
8648 sizeof unary-expression
8649 sizeof ( type-id )
8650 alignof ( type-id ) [C++0x]
8651 new-expression
8652 delete-expression
8654 GNU Extensions:
8656 unary-expression:
8657 __extension__ cast-expression
8658 __alignof__ unary-expression
8659 __alignof__ ( type-id )
8660 alignof unary-expression [C++0x]
8661 __real__ cast-expression
8662 __imag__ cast-expression
8663 && identifier
8664 sizeof ( type-id ) { initializer-list , [opt] }
8665 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8666 __alignof__ ( type-id ) { initializer-list , [opt] }
8668 ADDRESS_P is true iff the unary-expression is appearing as the
8669 operand of the `&' operator. CAST_P is true if this expression is
8670 the target of a cast.
8672 Returns a representation of the expression. */
8674 static cp_expr
8675 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8676 bool address_p, bool cast_p, bool decltype_p)
8678 cp_token *token;
8679 enum tree_code unary_operator;
8681 /* Peek at the next token. */
8682 token = cp_lexer_peek_token (parser->lexer);
8683 /* Some keywords give away the kind of expression. */
8684 if (token->type == CPP_KEYWORD)
8686 enum rid keyword = token->keyword;
8688 switch (keyword)
8690 case RID_ALIGNOF:
8691 case RID_SIZEOF:
8693 tree operand, ret;
8694 enum tree_code op;
8695 location_t start_loc = token->location;
8697 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8698 bool std_alignof = id_equal (token->u.value, "alignof");
8700 /* Consume the token. */
8701 cp_lexer_consume_token (parser->lexer);
8702 /* Parse the operand. */
8703 operand = cp_parser_sizeof_operand (parser, keyword);
8705 /* Construct a location e.g. :
8706 alignof (expr)
8707 ^~~~~~~~~~~~~~
8708 with start == caret at the start of the "alignof"/"sizeof"
8709 token, with the endpoint at the final closing paren. */
8710 location_t compound_loc
8711 = make_location (start_loc, start_loc, parser->lexer);
8713 if (TYPE_P (operand))
8714 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8715 std_alignof, true);
8716 else
8718 /* ISO C++ defines alignof only with types, not with
8719 expressions. So pedwarn if alignof is used with a non-
8720 type expression. However, __alignof__ is ok. */
8721 if (std_alignof)
8722 pedwarn (token->location, OPT_Wpedantic,
8723 "ISO C++ does not allow %<alignof%> "
8724 "with a non-type");
8726 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8727 std_alignof, true);
8729 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8730 SIZEOF_EXPR with the original operand. */
8731 if (op == SIZEOF_EXPR && ret != error_mark_node)
8733 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8735 if (!processing_template_decl && TYPE_P (operand))
8737 ret = build_min (SIZEOF_EXPR, size_type_node,
8738 build1 (NOP_EXPR, operand,
8739 error_mark_node));
8740 SIZEOF_EXPR_TYPE_P (ret) = 1;
8742 else
8743 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8744 TREE_SIDE_EFFECTS (ret) = 0;
8745 TREE_READONLY (ret) = 1;
8746 SET_EXPR_LOCATION (ret, compound_loc);
8750 cp_expr ret_expr (ret, compound_loc);
8751 ret_expr = ret_expr.maybe_add_location_wrapper ();
8752 return ret_expr;
8755 case RID_BUILTIN_HAS_ATTRIBUTE:
8756 return cp_parser_has_attribute_expression (parser);
8758 case RID_NEW:
8759 return cp_parser_new_expression (parser);
8761 case RID_DELETE:
8762 return cp_parser_delete_expression (parser);
8764 case RID_EXTENSION:
8766 /* The saved value of the PEDANTIC flag. */
8767 int saved_pedantic;
8768 tree expr;
8770 /* Save away the PEDANTIC flag. */
8771 cp_parser_extension_opt (parser, &saved_pedantic);
8772 /* Parse the cast-expression. */
8773 expr = cp_parser_simple_cast_expression (parser);
8774 /* Restore the PEDANTIC flag. */
8775 pedantic = saved_pedantic;
8777 return expr;
8780 case RID_REALPART:
8781 case RID_IMAGPART:
8783 tree expression;
8785 /* Consume the `__real__' or `__imag__' token. */
8786 cp_lexer_consume_token (parser->lexer);
8787 /* Parse the cast-expression. */
8788 expression = cp_parser_simple_cast_expression (parser);
8789 /* Create the complete representation. */
8790 return build_x_unary_op (token->location,
8791 (keyword == RID_REALPART
8792 ? REALPART_EXPR : IMAGPART_EXPR),
8793 expression, NULL_TREE,
8794 tf_warning_or_error);
8796 break;
8798 case RID_TRANSACTION_ATOMIC:
8799 case RID_TRANSACTION_RELAXED:
8800 return cp_parser_transaction_expression (parser, keyword);
8802 case RID_NOEXCEPT:
8804 tree expr;
8805 const char *saved_message;
8806 bool saved_integral_constant_expression_p;
8807 bool saved_non_integral_constant_expression_p;
8808 bool saved_greater_than_is_operator_p;
8810 location_t start_loc = token->location;
8812 cp_lexer_consume_token (parser->lexer);
8813 matching_parens parens;
8814 parens.require_open (parser);
8816 saved_message = parser->type_definition_forbidden_message;
8817 parser->type_definition_forbidden_message
8818 = G_("types may not be defined in %<noexcept%> expressions");
8820 saved_integral_constant_expression_p
8821 = parser->integral_constant_expression_p;
8822 saved_non_integral_constant_expression_p
8823 = parser->non_integral_constant_expression_p;
8824 parser->integral_constant_expression_p = false;
8826 saved_greater_than_is_operator_p
8827 = parser->greater_than_is_operator_p;
8828 parser->greater_than_is_operator_p = true;
8830 ++cp_unevaluated_operand;
8831 ++c_inhibit_evaluation_warnings;
8832 ++cp_noexcept_operand;
8833 expr = cp_parser_expression (parser);
8834 --cp_noexcept_operand;
8835 --c_inhibit_evaluation_warnings;
8836 --cp_unevaluated_operand;
8838 parser->greater_than_is_operator_p
8839 = saved_greater_than_is_operator_p;
8841 parser->integral_constant_expression_p
8842 = saved_integral_constant_expression_p;
8843 parser->non_integral_constant_expression_p
8844 = saved_non_integral_constant_expression_p;
8846 parser->type_definition_forbidden_message = saved_message;
8848 parens.require_close (parser);
8850 /* Construct a location of the form:
8851 noexcept (expr)
8852 ^~~~~~~~~~~~~~~
8853 with start == caret, finishing at the close-paren. */
8854 location_t noexcept_loc
8855 = make_location (start_loc, start_loc, parser->lexer);
8857 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8858 noexcept_loc);
8861 case RID_CO_AWAIT:
8863 tree expr;
8864 location_t kw_loc = token->location;
8866 /* Consume the `co_await' token. */
8867 cp_lexer_consume_token (parser->lexer);
8868 /* Parse its cast-expression. */
8869 expr = cp_parser_simple_cast_expression (parser);
8870 if (expr == error_mark_node)
8871 return error_mark_node;
8873 /* Handle [expr.await]. */
8874 return cp_expr (finish_co_await_expr (kw_loc, expr));
8877 default:
8878 break;
8882 /* Look for the `:: new' and `:: delete', which also signal the
8883 beginning of a new-expression, or delete-expression,
8884 respectively. If the next token is `::', then it might be one of
8885 these. */
8886 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8888 enum rid keyword;
8890 /* See if the token after the `::' is one of the keywords in
8891 which we're interested. */
8892 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8893 /* If it's `new', we have a new-expression. */
8894 if (keyword == RID_NEW)
8895 return cp_parser_new_expression (parser);
8896 /* Similarly, for `delete'. */
8897 else if (keyword == RID_DELETE)
8898 return cp_parser_delete_expression (parser);
8901 /* Look for a unary operator. */
8902 unary_operator = cp_parser_unary_operator (token);
8903 /* The `++' and `--' operators can be handled similarly, even though
8904 they are not technically unary-operators in the grammar. */
8905 if (unary_operator == ERROR_MARK)
8907 if (token->type == CPP_PLUS_PLUS)
8908 unary_operator = PREINCREMENT_EXPR;
8909 else if (token->type == CPP_MINUS_MINUS)
8910 unary_operator = PREDECREMENT_EXPR;
8911 /* Handle the GNU address-of-label extension. */
8912 else if (cp_parser_allow_gnu_extensions_p (parser)
8913 && token->type == CPP_AND_AND)
8915 tree identifier;
8916 tree expression;
8917 location_t start_loc = token->location;
8919 /* Consume the '&&' token. */
8920 cp_lexer_consume_token (parser->lexer);
8921 /* Look for the identifier. */
8922 identifier = cp_parser_identifier (parser);
8923 /* Construct a location of the form:
8924 &&label
8925 ^~~~~~~
8926 with caret==start at the "&&", finish at the end of the label. */
8927 location_t combined_loc
8928 = make_location (start_loc, start_loc, parser->lexer);
8929 /* Create an expression representing the address. */
8930 expression = finish_label_address_expr (identifier, combined_loc);
8931 if (cp_parser_non_integral_constant_expression (parser,
8932 NIC_ADDR_LABEL))
8933 expression = error_mark_node;
8934 return expression;
8937 if (unary_operator != ERROR_MARK)
8939 cp_expr cast_expression;
8940 cp_expr expression = error_mark_node;
8941 non_integral_constant non_constant_p = NIC_NONE;
8942 location_t loc = token->location;
8943 tsubst_flags_t complain = complain_flags (decltype_p);
8945 /* Consume the operator token. */
8946 token = cp_lexer_consume_token (parser->lexer);
8947 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8949 /* Parse the cast-expression. */
8950 cast_expression
8951 = cp_parser_cast_expression (parser,
8952 unary_operator == ADDR_EXPR,
8953 /*cast_p=*/false,
8954 /*decltype*/false,
8955 pidk);
8957 /* Make a location:
8958 OP_TOKEN CAST_EXPRESSION
8959 ^~~~~~~~~~~~~~~~~~~~~~~~~
8960 with start==caret at the operator token, and
8961 extending to the end of the cast_expression. */
8962 loc = make_location (loc, loc, cast_expression.get_finish ());
8964 /* Now, build an appropriate representation. */
8965 switch (unary_operator)
8967 case INDIRECT_REF:
8968 non_constant_p = NIC_STAR;
8969 expression = build_x_indirect_ref (loc, cast_expression,
8970 RO_UNARY_STAR, NULL_TREE,
8971 complain);
8972 /* TODO: build_x_indirect_ref does not always honor the
8973 location, so ensure it is set. */
8974 expression.set_location (loc);
8975 break;
8977 case ADDR_EXPR:
8978 non_constant_p = NIC_ADDR;
8979 /* Fall through. */
8980 case BIT_NOT_EXPR:
8981 expression = build_x_unary_op (loc, unary_operator,
8982 cast_expression,
8983 NULL_TREE, complain);
8984 /* TODO: build_x_unary_op does not always honor the location,
8985 so ensure it is set. */
8986 expression.set_location (loc);
8987 break;
8989 case PREINCREMENT_EXPR:
8990 case PREDECREMENT_EXPR:
8991 non_constant_p = unary_operator == PREINCREMENT_EXPR
8992 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8993 /* Fall through. */
8994 case NEGATE_EXPR:
8995 /* Immediately fold negation of a constant, unless the constant is 0
8996 (since -0 == 0) or it would overflow. */
8997 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8999 tree stripped_expr
9000 = tree_strip_any_location_wrapper (cast_expression);
9001 if (CONSTANT_CLASS_P (stripped_expr)
9002 && !integer_zerop (stripped_expr)
9003 && !TREE_OVERFLOW (stripped_expr))
9005 tree folded = fold_build1 (unary_operator,
9006 TREE_TYPE (stripped_expr),
9007 stripped_expr);
9008 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9010 expression = maybe_wrap_with_location (folded, loc);
9011 break;
9015 /* Fall through. */
9016 case UNARY_PLUS_EXPR:
9017 case TRUTH_NOT_EXPR:
9018 expression = finish_unary_op_expr (loc, unary_operator,
9019 cast_expression, complain);
9020 break;
9022 default:
9023 gcc_unreachable ();
9026 if (non_constant_p != NIC_NONE
9027 && cp_parser_non_integral_constant_expression (parser,
9028 non_constant_p))
9029 expression = error_mark_node;
9031 return expression;
9034 return cp_parser_postfix_expression (parser, address_p, cast_p,
9035 /*member_access_only_p=*/false,
9036 decltype_p,
9037 pidk);
9040 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9041 unary-operator, the corresponding tree code is returned. */
9043 static enum tree_code
9044 cp_parser_unary_operator (cp_token* token)
9046 switch (token->type)
9048 case CPP_MULT:
9049 return INDIRECT_REF;
9051 case CPP_AND:
9052 return ADDR_EXPR;
9054 case CPP_PLUS:
9055 return UNARY_PLUS_EXPR;
9057 case CPP_MINUS:
9058 return NEGATE_EXPR;
9060 case CPP_NOT:
9061 return TRUTH_NOT_EXPR;
9063 case CPP_COMPL:
9064 return BIT_NOT_EXPR;
9066 default:
9067 return ERROR_MARK;
9071 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9072 Returns a representation of the expression. */
9074 static tree
9075 cp_parser_has_attribute_expression (cp_parser *parser)
9077 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9079 /* Consume the __builtin_has_attribute token. */
9080 cp_lexer_consume_token (parser->lexer);
9082 matching_parens parens;
9083 if (!parens.require_open (parser))
9084 return error_mark_node;
9086 /* Types cannot be defined in a `sizeof' expression. Save away the
9087 old message. */
9088 const char *saved_message = parser->type_definition_forbidden_message;
9089 const char *saved_message_arg
9090 = parser->type_definition_forbidden_message_arg;
9091 parser->type_definition_forbidden_message
9092 = G_("types may not be defined in %qs expressions");
9093 parser->type_definition_forbidden_message_arg
9094 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9096 /* The restrictions on constant-expressions do not apply inside
9097 sizeof expressions. */
9098 bool saved_integral_constant_expression_p
9099 = parser->integral_constant_expression_p;
9100 bool saved_non_integral_constant_expression_p
9101 = parser->non_integral_constant_expression_p;
9102 parser->integral_constant_expression_p = false;
9104 /* Do not actually evaluate the expression. */
9105 ++cp_unevaluated_operand;
9106 ++c_inhibit_evaluation_warnings;
9108 tree oper = NULL_TREE;
9110 /* We can't be sure yet whether we're looking at a type-id or an
9111 expression. */
9112 cp_parser_parse_tentatively (parser);
9114 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9115 parser->in_type_id_in_expr_p = true;
9116 /* Look for the type-id. */
9117 oper = cp_parser_type_id (parser);
9118 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9120 cp_parser_parse_definitely (parser);
9122 /* If the type-id production did not work out, then we must be
9123 looking at an expression. */
9124 if (!oper || oper == error_mark_node)
9125 oper = cp_parser_assignment_expression (parser);
9127 STRIP_ANY_LOCATION_WRAPPER (oper);
9129 /* Go back to evaluating expressions. */
9130 --cp_unevaluated_operand;
9131 --c_inhibit_evaluation_warnings;
9133 /* And restore the old one. */
9134 parser->type_definition_forbidden_message = saved_message;
9135 parser->type_definition_forbidden_message_arg = saved_message_arg;
9136 parser->integral_constant_expression_p
9137 = saved_integral_constant_expression_p;
9138 parser->non_integral_constant_expression_p
9139 = saved_non_integral_constant_expression_p;
9141 /* Consume the comma if it's there. */
9142 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9144 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9145 /*consume_paren=*/true);
9146 return error_mark_node;
9149 /* Parse the attribute specification. */
9150 bool ret = false;
9151 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9152 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9154 if (oper == error_mark_node)
9155 /* Nothing. */;
9156 else if (processing_template_decl && uses_template_parms (oper))
9157 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9158 "not supported yet");
9159 else
9161 /* Fold constant expressions used in attributes first. */
9162 cp_check_const_attributes (attr);
9164 /* Finally, see if OPER has been declared with ATTR. */
9165 ret = has_attribute (atloc, oper, attr, default_conversion);
9168 parens.require_close (parser);
9170 else
9172 error_at (atloc, "expected identifier");
9173 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9176 /* Construct a location e.g. :
9177 __builtin_has_attribute (oper, attr)
9178 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9179 with start == caret at the start of the built-in token,
9180 and with the endpoint at the final closing paren. */
9181 location_t compound_loc
9182 = make_location (start_loc, start_loc, parser->lexer);
9184 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9185 ret_expr.set_location (compound_loc);
9186 ret_expr = ret_expr.maybe_add_location_wrapper ();
9187 return ret_expr;
9190 /* Parse a new-expression.
9192 new-expression:
9193 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9194 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9196 Returns a representation of the expression. */
9198 static tree
9199 cp_parser_new_expression (cp_parser* parser)
9201 bool global_scope_p;
9202 vec<tree, va_gc> *placement;
9203 tree type;
9204 vec<tree, va_gc> *initializer;
9205 tree nelts = NULL_TREE;
9206 tree ret;
9208 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9210 /* Look for the optional `::' operator. */
9211 global_scope_p
9212 = (cp_parser_global_scope_opt (parser,
9213 /*current_scope_valid_p=*/false)
9214 != NULL_TREE);
9215 /* Look for the `new' operator. */
9216 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9217 /* There's no easy way to tell a new-placement from the
9218 `( type-id )' construct. */
9219 cp_parser_parse_tentatively (parser);
9220 /* Look for a new-placement. */
9221 placement = cp_parser_new_placement (parser);
9222 /* If that didn't work out, there's no new-placement. */
9223 if (!cp_parser_parse_definitely (parser))
9225 if (placement != NULL)
9226 release_tree_vector (placement);
9227 placement = NULL;
9230 /* If the next token is a `(', then we have a parenthesized
9231 type-id. */
9232 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9234 cp_token *token;
9235 const char *saved_message = parser->type_definition_forbidden_message;
9237 /* Consume the `('. */
9238 matching_parens parens;
9239 parens.consume_open (parser);
9241 /* Parse the type-id. */
9242 parser->type_definition_forbidden_message
9243 = G_("types may not be defined in a new-expression");
9245 type_id_in_expr_sentinel s (parser);
9246 type = cp_parser_type_id (parser);
9248 parser->type_definition_forbidden_message = saved_message;
9250 /* Look for the closing `)'. */
9251 parens.require_close (parser);
9252 token = cp_lexer_peek_token (parser->lexer);
9253 /* There should not be a direct-new-declarator in this production,
9254 but GCC used to allowed this, so we check and emit a sensible error
9255 message for this case. */
9256 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9258 error_at (token->location,
9259 "array bound forbidden after parenthesized type-id");
9260 inform (token->location,
9261 "try removing the parentheses around the type-id");
9262 cp_parser_direct_new_declarator (parser);
9265 /* Otherwise, there must be a new-type-id. */
9266 else
9267 type = cp_parser_new_type_id (parser, &nelts);
9269 /* If the next token is a `(' or '{', then we have a new-initializer. */
9270 cp_token *token = cp_lexer_peek_token (parser->lexer);
9271 if (token->type == CPP_OPEN_PAREN
9272 || token->type == CPP_OPEN_BRACE)
9273 initializer = cp_parser_new_initializer (parser);
9274 else
9275 initializer = NULL;
9277 /* A new-expression may not appear in an integral constant
9278 expression. */
9279 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9280 ret = error_mark_node;
9281 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9282 of a new-type-id or type-id of a new-expression, the new-expression shall
9283 contain a new-initializer of the form ( assignment-expression )".
9284 Additionally, consistently with the spirit of DR 1467, we want to accept
9285 'new auto { 2 }' too. */
9286 else if ((ret = type_uses_auto (type))
9287 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9288 && (vec_safe_length (initializer) != 1
9289 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9290 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9292 error_at (token->location,
9293 "initialization of new-expression for type %<auto%> "
9294 "requires exactly one element");
9295 ret = error_mark_node;
9297 else
9299 /* Construct a location e.g.:
9300 ptr = new int[100]
9301 ^~~~~~~~~~~~
9302 with caret == start at the start of the "new" token, and the end
9303 at the end of the final token we consumed. */
9304 location_t combined_loc = make_location (start_loc, start_loc,
9305 parser->lexer);
9306 /* Create a representation of the new-expression. */
9307 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9308 global_scope_p, tf_warning_or_error);
9311 if (placement != NULL)
9312 release_tree_vector (placement);
9313 if (initializer != NULL)
9314 release_tree_vector (initializer);
9316 return ret;
9319 /* Parse a new-placement.
9321 new-placement:
9322 ( expression-list )
9324 Returns the same representation as for an expression-list. */
9326 static vec<tree, va_gc> *
9327 cp_parser_new_placement (cp_parser* parser)
9329 vec<tree, va_gc> *expression_list;
9331 /* Parse the expression-list. */
9332 expression_list = (cp_parser_parenthesized_expression_list
9333 (parser, non_attr, /*cast_p=*/false,
9334 /*allow_expansion_p=*/true,
9335 /*non_constant_p=*/NULL));
9337 if (expression_list && expression_list->is_empty ())
9338 error ("expected expression-list or type-id");
9340 return expression_list;
9343 /* Parse a new-type-id.
9345 new-type-id:
9346 type-specifier-seq new-declarator [opt]
9348 Returns the TYPE allocated. If the new-type-id indicates an array
9349 type, *NELTS is set to the number of elements in the last array
9350 bound; the TYPE will not include the last array bound. */
9352 static tree
9353 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9355 cp_decl_specifier_seq type_specifier_seq;
9356 cp_declarator *new_declarator;
9357 cp_declarator *declarator;
9358 cp_declarator *outer_declarator;
9359 const char *saved_message;
9361 /* The type-specifier sequence must not contain type definitions.
9362 (It cannot contain declarations of new types either, but if they
9363 are not definitions we will catch that because they are not
9364 complete.) */
9365 saved_message = parser->type_definition_forbidden_message;
9366 parser->type_definition_forbidden_message
9367 = G_("types may not be defined in a new-type-id");
9368 /* Parse the type-specifier-seq. */
9369 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9370 /*is_declaration=*/false,
9371 /*is_trailing_return=*/false,
9372 &type_specifier_seq);
9373 /* Restore the old message. */
9374 parser->type_definition_forbidden_message = saved_message;
9376 if (type_specifier_seq.type == error_mark_node)
9377 return error_mark_node;
9379 /* Parse the new-declarator. */
9380 new_declarator = cp_parser_new_declarator_opt (parser);
9382 /* Determine the number of elements in the last array dimension, if
9383 any. */
9384 *nelts = NULL_TREE;
9385 /* Skip down to the last array dimension. */
9386 declarator = new_declarator;
9387 outer_declarator = NULL;
9388 while (declarator && (declarator->kind == cdk_pointer
9389 || declarator->kind == cdk_ptrmem))
9391 outer_declarator = declarator;
9392 declarator = declarator->declarator;
9394 while (declarator
9395 && declarator->kind == cdk_array
9396 && declarator->declarator
9397 && declarator->declarator->kind == cdk_array)
9399 outer_declarator = declarator;
9400 declarator = declarator->declarator;
9403 if (declarator && declarator->kind == cdk_array)
9405 *nelts = declarator->u.array.bounds;
9406 if (*nelts == error_mark_node)
9407 *nelts = integer_one_node;
9409 if (*nelts == NULL_TREE)
9410 /* Leave [] in the declarator. */;
9411 else if (outer_declarator)
9412 outer_declarator->declarator = declarator->declarator;
9413 else
9414 new_declarator = NULL;
9417 return groktypename (&type_specifier_seq, new_declarator, false);
9420 /* Parse an (optional) new-declarator.
9422 new-declarator:
9423 ptr-operator new-declarator [opt]
9424 direct-new-declarator
9426 Returns the declarator. */
9428 static cp_declarator *
9429 cp_parser_new_declarator_opt (cp_parser* parser)
9431 enum tree_code code;
9432 tree type, std_attributes = NULL_TREE;
9433 cp_cv_quals cv_quals;
9435 /* We don't know if there's a ptr-operator next, or not. */
9436 cp_parser_parse_tentatively (parser);
9437 /* Look for a ptr-operator. */
9438 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9439 /* If that worked, look for more new-declarators. */
9440 if (cp_parser_parse_definitely (parser))
9442 cp_declarator *declarator;
9444 /* Parse another optional declarator. */
9445 declarator = cp_parser_new_declarator_opt (parser);
9447 declarator = cp_parser_make_indirect_declarator
9448 (code, type, cv_quals, declarator, std_attributes);
9450 return declarator;
9453 /* If the next token is a `[', there is a direct-new-declarator. */
9454 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9455 return cp_parser_direct_new_declarator (parser);
9457 return NULL;
9460 /* Parse a direct-new-declarator.
9462 direct-new-declarator:
9463 [ expression ]
9464 direct-new-declarator [constant-expression]
9468 static cp_declarator *
9469 cp_parser_direct_new_declarator (cp_parser* parser)
9471 cp_declarator *declarator = NULL;
9472 bool first_p = true;
9474 while (true)
9476 tree expression;
9477 cp_token *token;
9479 /* Look for the opening `['. */
9480 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9482 token = cp_lexer_peek_token (parser->lexer);
9483 if (token->type == CPP_CLOSE_SQUARE && first_p)
9484 expression = NULL_TREE;
9485 else
9486 expression = cp_parser_expression (parser);
9487 /* The standard requires that the expression have integral
9488 type. DR 74 adds enumeration types. We believe that the
9489 real intent is that these expressions be handled like the
9490 expression in a `switch' condition, which also allows
9491 classes with a single conversion to integral or
9492 enumeration type. */
9493 if (expression && !processing_template_decl)
9495 expression
9496 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9497 expression,
9498 /*complain=*/true);
9499 if (!expression)
9501 error_at (token->location,
9502 "expression in new-declarator must have integral "
9503 "or enumeration type");
9504 expression = error_mark_node;
9508 /* Look for the closing `]'. */
9509 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9511 /* Add this bound to the declarator. */
9512 declarator = make_array_declarator (declarator, expression);
9514 /* If the next token is not a `[', then there are no more
9515 bounds. */
9516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9517 break;
9518 first_p = false;
9521 return declarator;
9524 /* Parse a new-initializer.
9526 new-initializer:
9527 ( expression-list [opt] )
9528 braced-init-list
9530 Returns a representation of the expression-list. */
9532 static vec<tree, va_gc> *
9533 cp_parser_new_initializer (cp_parser* parser)
9535 vec<tree, va_gc> *expression_list;
9537 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9539 tree t;
9540 bool expr_non_constant_p;
9541 cp_lexer_set_source_position (parser->lexer);
9542 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9543 t = cp_parser_braced_list (parser, &expr_non_constant_p);
9544 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9545 expression_list = make_tree_vector_single (t);
9547 else
9548 expression_list = (cp_parser_parenthesized_expression_list
9549 (parser, non_attr, /*cast_p=*/false,
9550 /*allow_expansion_p=*/true,
9551 /*non_constant_p=*/NULL));
9553 return expression_list;
9556 /* Parse a delete-expression.
9558 delete-expression:
9559 :: [opt] delete cast-expression
9560 :: [opt] delete [ ] cast-expression
9562 Returns a representation of the expression. */
9564 static tree
9565 cp_parser_delete_expression (cp_parser* parser)
9567 bool global_scope_p;
9568 bool array_p;
9569 tree expression;
9570 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9572 /* Look for the optional `::' operator. */
9573 global_scope_p
9574 = (cp_parser_global_scope_opt (parser,
9575 /*current_scope_valid_p=*/false)
9576 != NULL_TREE);
9577 /* Look for the `delete' keyword. */
9578 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9579 /* See if the array syntax is in use. */
9580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9582 /* Consume the `[' token. */
9583 cp_lexer_consume_token (parser->lexer);
9584 /* Look for the `]' token. */
9585 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9586 /* Remember that this is the `[]' construct. */
9587 array_p = true;
9589 else
9590 array_p = false;
9592 /* Parse the cast-expression. */
9593 expression = cp_parser_simple_cast_expression (parser);
9595 /* A delete-expression may not appear in an integral constant
9596 expression. */
9597 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9598 return error_mark_node;
9600 /* Construct a location e.g.:
9601 delete [ ] ptr
9602 ^~~~~~~~~~~~~~
9603 with caret == start at the start of the "delete" token, and
9604 the end at the end of the final token we consumed. */
9605 location_t combined_loc = make_location (start_loc, start_loc,
9606 parser->lexer);
9607 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9608 global_scope_p, tf_warning_or_error);
9610 return expression;
9613 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9614 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9615 0 otherwise. */
9617 static int
9618 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9620 cp_token *token = cp_lexer_peek_token (parser->lexer);
9621 switch (token->type)
9623 case CPP_COMMA:
9624 case CPP_SEMICOLON:
9625 case CPP_QUERY:
9626 case CPP_COLON:
9627 case CPP_CLOSE_SQUARE:
9628 case CPP_CLOSE_PAREN:
9629 case CPP_CLOSE_BRACE:
9630 case CPP_OPEN_BRACE:
9631 case CPP_DOT:
9632 case CPP_DOT_STAR:
9633 case CPP_DEREF:
9634 case CPP_DEREF_STAR:
9635 case CPP_DIV:
9636 case CPP_MOD:
9637 case CPP_LSHIFT:
9638 case CPP_RSHIFT:
9639 case CPP_LESS:
9640 case CPP_GREATER:
9641 case CPP_LESS_EQ:
9642 case CPP_GREATER_EQ:
9643 case CPP_EQ_EQ:
9644 case CPP_NOT_EQ:
9645 case CPP_EQ:
9646 case CPP_MULT_EQ:
9647 case CPP_DIV_EQ:
9648 case CPP_MOD_EQ:
9649 case CPP_PLUS_EQ:
9650 case CPP_MINUS_EQ:
9651 case CPP_RSHIFT_EQ:
9652 case CPP_LSHIFT_EQ:
9653 case CPP_AND_EQ:
9654 case CPP_XOR_EQ:
9655 case CPP_OR_EQ:
9656 case CPP_XOR:
9657 case CPP_OR:
9658 case CPP_OR_OR:
9659 case CPP_EOF:
9660 case CPP_ELLIPSIS:
9661 return 0;
9663 case CPP_OPEN_PAREN:
9664 /* In ((type ()) () the last () isn't a valid cast-expression,
9665 so the whole must be parsed as postfix-expression. */
9666 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9667 != CPP_CLOSE_PAREN;
9669 case CPP_OPEN_SQUARE:
9670 /* '[' may start a primary-expression in obj-c++ and in C++11,
9671 as a lambda-expression, eg, '(void)[]{}'. */
9672 if (cxx_dialect >= cxx11)
9673 return -1;
9674 return c_dialect_objc ();
9676 case CPP_PLUS_PLUS:
9677 case CPP_MINUS_MINUS:
9678 /* '++' and '--' may or may not start a cast-expression:
9680 struct T { void operator++(int); };
9681 void f() { (T())++; }
9685 int a;
9686 (int)++a; */
9687 return -1;
9689 default:
9690 return 1;
9694 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9695 in the order: const_cast, static_cast, reinterpret_cast.
9697 Don't suggest dynamic_cast.
9699 Return the first legal cast kind found, or NULL otherwise. */
9701 static const char *
9702 get_cast_suggestion (tree dst_type, tree orig_expr)
9704 tree trial;
9706 /* Reuse the parser logic by attempting to build the various kinds of
9707 cast, with "complain" disabled.
9708 Identify the first such cast that is valid. */
9710 /* Don't attempt to run such logic within template processing. */
9711 if (processing_template_decl)
9712 return NULL;
9714 /* First try const_cast. */
9715 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9716 if (trial != error_mark_node)
9717 return "const_cast";
9719 /* If that fails, try static_cast. */
9720 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9721 if (trial != error_mark_node)
9722 return "static_cast";
9724 /* Finally, try reinterpret_cast. */
9725 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9726 tf_none);
9727 if (trial != error_mark_node)
9728 return "reinterpret_cast";
9730 /* No such cast possible. */
9731 return NULL;
9734 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9735 suggesting how to convert a C-style cast of the form:
9737 (DST_TYPE)ORIG_EXPR
9739 to a C++-style cast.
9741 The primary range of RICHLOC is asssumed to be that of the original
9742 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9743 of the parens in the C-style cast. */
9745 static void
9746 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9747 location_t close_paren_loc, tree orig_expr,
9748 tree dst_type)
9750 /* This function is non-trivial, so bail out now if the warning isn't
9751 going to be emitted. */
9752 if (!warn_old_style_cast)
9753 return;
9755 /* Try to find a legal C++ cast, trying them in order:
9756 const_cast, static_cast, reinterpret_cast. */
9757 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9758 if (!cast_suggestion)
9759 return;
9761 /* Replace the open paren with "CAST_SUGGESTION<". */
9762 pretty_printer pp;
9763 pp_string (&pp, cast_suggestion);
9764 pp_less (&pp);
9765 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9767 /* Replace the close paren with "> (". */
9768 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9770 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9771 rich_loc->add_fixit_insert_after (")");
9775 /* Parse a cast-expression.
9777 cast-expression:
9778 unary-expression
9779 ( type-id ) cast-expression
9781 ADDRESS_P is true iff the unary-expression is appearing as the
9782 operand of the `&' operator. CAST_P is true if this expression is
9783 the target of a cast.
9785 Returns a representation of the expression. */
9787 static cp_expr
9788 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9789 bool decltype_p, cp_id_kind * pidk)
9791 /* If it's a `(', then we might be looking at a cast. */
9792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9794 tree type = NULL_TREE;
9795 cp_expr expr (NULL_TREE);
9796 int cast_expression = 0;
9797 const char *saved_message;
9799 /* There's no way to know yet whether or not this is a cast.
9800 For example, `(int (3))' is a unary-expression, while `(int)
9801 3' is a cast. So, we resort to parsing tentatively. */
9802 cp_parser_parse_tentatively (parser);
9803 /* Types may not be defined in a cast. */
9804 saved_message = parser->type_definition_forbidden_message;
9805 parser->type_definition_forbidden_message
9806 = G_("types may not be defined in casts");
9807 /* Consume the `('. */
9808 matching_parens parens;
9809 cp_token *open_paren = parens.consume_open (parser);
9810 location_t open_paren_loc = open_paren->location;
9811 location_t close_paren_loc = UNKNOWN_LOCATION;
9813 /* A very tricky bit is that `(struct S) { 3 }' is a
9814 compound-literal (which we permit in C++ as an extension).
9815 But, that construct is not a cast-expression -- it is a
9816 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9817 is legal; if the compound-literal were a cast-expression,
9818 you'd need an extra set of parentheses.) But, if we parse
9819 the type-id, and it happens to be a class-specifier, then we
9820 will commit to the parse at that point, because we cannot
9821 undo the action that is done when creating a new class. So,
9822 then we cannot back up and do a postfix-expression.
9824 Another tricky case is the following (c++/29234):
9826 struct S { void operator () (); };
9828 void foo ()
9830 ( S()() );
9833 As a type-id we parse the parenthesized S()() as a function
9834 returning a function, groktypename complains and we cannot
9835 back up in this case either.
9837 Therefore, we scan ahead to the closing `)', and check to see
9838 if the tokens after the `)' can start a cast-expression. Otherwise
9839 we are dealing with an unary-expression, a postfix-expression
9840 or something else.
9842 Yet another tricky case, in C++11, is the following (c++/54891):
9844 (void)[]{};
9846 The issue is that usually, besides the case of lambda-expressions,
9847 the parenthesized type-id cannot be followed by '[', and, eg, we
9848 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9849 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9850 we don't commit, we try a cast-expression, then an unary-expression.
9852 Save tokens so that we can put them back. */
9853 cp_lexer_save_tokens (parser->lexer);
9855 /* We may be looking at a cast-expression. */
9856 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9857 /*consume_paren=*/true))
9858 cast_expression
9859 = cp_parser_tokens_start_cast_expression (parser);
9861 /* Roll back the tokens we skipped. */
9862 cp_lexer_rollback_tokens (parser->lexer);
9863 /* If we aren't looking at a cast-expression, simulate an error so
9864 that the call to cp_parser_error_occurred below returns true. */
9865 if (!cast_expression)
9866 cp_parser_simulate_error (parser);
9867 else
9869 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9870 parser->in_type_id_in_expr_p = true;
9871 /* Look for the type-id. */
9872 type = cp_parser_type_id (parser);
9873 /* Look for the closing `)'. */
9874 cp_token *close_paren = parens.require_close (parser);
9875 if (close_paren)
9876 close_paren_loc = close_paren->location;
9877 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9880 /* Restore the saved message. */
9881 parser->type_definition_forbidden_message = saved_message;
9883 /* At this point this can only be either a cast or a
9884 parenthesized ctor such as `(T ())' that looks like a cast to
9885 function returning T. */
9886 if (!cp_parser_error_occurred (parser))
9888 /* Only commit if the cast-expression doesn't start with
9889 '++', '--', or '[' in C++11. */
9890 if (cast_expression > 0)
9891 cp_parser_commit_to_topmost_tentative_parse (parser);
9893 expr = cp_parser_cast_expression (parser,
9894 /*address_p=*/false,
9895 /*cast_p=*/true,
9896 /*decltype_p=*/false,
9897 pidk);
9899 if (cp_parser_parse_definitely (parser))
9901 /* Warn about old-style casts, if so requested. */
9902 if (warn_old_style_cast
9903 && !in_system_header_at (input_location)
9904 && !VOID_TYPE_P (type)
9905 && current_lang_name != lang_name_c)
9907 gcc_rich_location rich_loc (input_location);
9908 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9909 expr, type);
9910 warning_at (&rich_loc, OPT_Wold_style_cast,
9911 "use of old-style cast to %q#T", type);
9914 /* Only type conversions to integral or enumeration types
9915 can be used in constant-expressions. */
9916 if (!cast_valid_in_integral_constant_expression_p (type)
9917 && cp_parser_non_integral_constant_expression (parser,
9918 NIC_CAST))
9919 return error_mark_node;
9921 /* Perform the cast. */
9922 /* Make a location:
9923 (TYPE) EXPR
9924 ^~~~~~~~~~~
9925 with start==caret at the open paren, extending to the
9926 end of "expr". */
9927 location_t cast_loc = make_location (open_paren_loc,
9928 open_paren_loc,
9929 expr.get_finish ());
9930 expr = build_c_cast (cast_loc, type, expr);
9931 return expr;
9934 else
9935 cp_parser_abort_tentative_parse (parser);
9938 /* If we get here, then it's not a cast, so it must be a
9939 unary-expression. */
9940 return cp_parser_unary_expression (parser, pidk, address_p,
9941 cast_p, decltype_p);
9944 /* Parse a binary expression of the general form:
9946 pm-expression:
9947 cast-expression
9948 pm-expression .* cast-expression
9949 pm-expression ->* cast-expression
9951 multiplicative-expression:
9952 pm-expression
9953 multiplicative-expression * pm-expression
9954 multiplicative-expression / pm-expression
9955 multiplicative-expression % pm-expression
9957 additive-expression:
9958 multiplicative-expression
9959 additive-expression + multiplicative-expression
9960 additive-expression - multiplicative-expression
9962 shift-expression:
9963 additive-expression
9964 shift-expression << additive-expression
9965 shift-expression >> additive-expression
9967 relational-expression:
9968 shift-expression
9969 relational-expression < shift-expression
9970 relational-expression > shift-expression
9971 relational-expression <= shift-expression
9972 relational-expression >= shift-expression
9974 GNU Extension:
9976 relational-expression:
9977 relational-expression <? shift-expression
9978 relational-expression >? shift-expression
9980 equality-expression:
9981 relational-expression
9982 equality-expression == relational-expression
9983 equality-expression != relational-expression
9985 and-expression:
9986 equality-expression
9987 and-expression & equality-expression
9989 exclusive-or-expression:
9990 and-expression
9991 exclusive-or-expression ^ and-expression
9993 inclusive-or-expression:
9994 exclusive-or-expression
9995 inclusive-or-expression | exclusive-or-expression
9997 logical-and-expression:
9998 inclusive-or-expression
9999 logical-and-expression && inclusive-or-expression
10001 logical-or-expression:
10002 logical-and-expression
10003 logical-or-expression || logical-and-expression
10005 All these are implemented with a single function like:
10007 binary-expression:
10008 simple-cast-expression
10009 binary-expression <token> binary-expression
10011 CAST_P is true if this expression is the target of a cast.
10013 The binops_by_token map is used to get the tree codes for each <token> type.
10014 binary-expressions are associated according to a precedence table. */
10016 #define TOKEN_PRECEDENCE(token) \
10017 (((token->type == CPP_GREATER \
10018 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10019 && !parser->greater_than_is_operator_p) \
10020 ? PREC_NOT_OPERATOR \
10021 : binops_by_token[token->type].prec)
10023 static cp_expr
10024 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10025 bool no_toplevel_fold_p,
10026 bool decltype_p,
10027 enum cp_parser_prec prec,
10028 cp_id_kind * pidk)
10030 cp_parser_expression_stack stack;
10031 cp_parser_expression_stack_entry *sp = &stack[0];
10032 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10033 cp_parser_expression_stack_entry current;
10034 cp_expr rhs;
10035 cp_token *token;
10036 enum tree_code rhs_type;
10037 enum cp_parser_prec new_prec, lookahead_prec;
10038 tree overload;
10040 /* Parse the first expression. */
10041 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10042 ? TRUTH_NOT_EXPR : ERROR_MARK);
10043 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10044 cast_p, decltype_p, pidk);
10045 current.prec = prec;
10047 if (cp_parser_error_occurred (parser))
10048 return error_mark_node;
10050 for (;;)
10052 /* Get an operator token. */
10053 token = cp_lexer_peek_token (parser->lexer);
10055 if (warn_cxx11_compat
10056 && token->type == CPP_RSHIFT
10057 && !parser->greater_than_is_operator_p)
10059 if (warning_at (token->location, OPT_Wc__11_compat,
10060 "%<>>%> operator is treated"
10061 " as two right angle brackets in C++11"))
10062 inform (token->location,
10063 "suggest parentheses around %<>>%> expression");
10066 new_prec = TOKEN_PRECEDENCE (token);
10067 if (new_prec != PREC_NOT_OPERATOR
10068 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10069 /* This is a fold-expression; handle it later. */
10070 new_prec = PREC_NOT_OPERATOR;
10072 /* Popping an entry off the stack means we completed a subexpression:
10073 - either we found a token which is not an operator (`>' where it is not
10074 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10075 will happen repeatedly;
10076 - or, we found an operator which has lower priority. This is the case
10077 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10078 parsing `3 * 4'. */
10079 if (new_prec <= current.prec)
10081 if (sp == stack)
10082 break;
10083 else
10084 goto pop;
10087 get_rhs:
10088 current.tree_type = binops_by_token[token->type].tree_type;
10089 current.loc = token->location;
10091 /* We used the operator token. */
10092 cp_lexer_consume_token (parser->lexer);
10094 /* For "false && x" or "true || x", x will never be executed;
10095 disable warnings while evaluating it. */
10096 if ((current.tree_type == TRUTH_ANDIF_EXPR
10097 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10098 || (current.tree_type == TRUTH_ORIF_EXPR
10099 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10101 disable_warnings_sp = sp;
10102 ++c_inhibit_evaluation_warnings;
10105 /* Extract another operand. It may be the RHS of this expression
10106 or the LHS of a new, higher priority expression. */
10107 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10108 ? TRUTH_NOT_EXPR : ERROR_MARK);
10109 rhs = cp_parser_simple_cast_expression (parser);
10111 /* Get another operator token. Look up its precedence to avoid
10112 building a useless (immediately popped) stack entry for common
10113 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10114 token = cp_lexer_peek_token (parser->lexer);
10115 lookahead_prec = TOKEN_PRECEDENCE (token);
10116 if (lookahead_prec != PREC_NOT_OPERATOR
10117 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10118 lookahead_prec = PREC_NOT_OPERATOR;
10119 if (lookahead_prec > new_prec)
10121 /* ... and prepare to parse the RHS of the new, higher priority
10122 expression. Since precedence levels on the stack are
10123 monotonically increasing, we do not have to care about
10124 stack overflows. */
10125 *sp = current;
10126 ++sp;
10127 current.lhs = rhs;
10128 current.lhs_type = rhs_type;
10129 current.prec = new_prec;
10130 new_prec = lookahead_prec;
10131 goto get_rhs;
10133 pop:
10134 lookahead_prec = new_prec;
10135 /* If the stack is not empty, we have parsed into LHS the right side
10136 (`4' in the example above) of an expression we had suspended.
10137 We can use the information on the stack to recover the LHS (`3')
10138 from the stack together with the tree code (`MULT_EXPR'), and
10139 the precedence of the higher level subexpression
10140 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10141 which will be used to actually build the additive expression. */
10142 rhs = current.lhs;
10143 rhs_type = current.lhs_type;
10144 --sp;
10145 current = *sp;
10148 /* Undo the disabling of warnings done above. */
10149 if (sp == disable_warnings_sp)
10151 disable_warnings_sp = NULL;
10152 --c_inhibit_evaluation_warnings;
10155 if (warn_logical_not_paren
10156 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10157 && current.lhs_type == TRUTH_NOT_EXPR
10158 /* Avoid warning for !!x == y. */
10159 && (TREE_CODE (current.lhs) != NE_EXPR
10160 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10161 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10162 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10163 /* Avoid warning for !b == y where b is boolean. */
10164 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10165 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10166 != BOOLEAN_TYPE))))
10167 /* Avoid warning for !!b == y where b is boolean. */
10168 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
10169 || TREE_TYPE (current.lhs) == NULL_TREE
10170 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10171 warn_logical_not_parentheses (current.loc, current.tree_type,
10172 current.lhs, maybe_constant_value (rhs));
10174 overload = NULL;
10176 location_t combined_loc = make_location (current.loc,
10177 current.lhs.get_start (),
10178 rhs.get_finish ());
10180 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10181 ERROR_MARK for everything that is not a binary expression.
10182 This makes warn_about_parentheses miss some warnings that
10183 involve unary operators. For unary expressions we should
10184 pass the correct tree_code unless the unary expression was
10185 surrounded by parentheses.
10187 if (no_toplevel_fold_p
10188 && lookahead_prec <= current.prec
10189 && sp == stack)
10191 if (current.lhs == error_mark_node || rhs == error_mark_node)
10192 current.lhs = error_mark_node;
10193 else
10195 current.lhs.maybe_add_location_wrapper ();
10196 rhs.maybe_add_location_wrapper ();
10197 current.lhs
10198 = build_min (current.tree_type,
10199 TREE_CODE_CLASS (current.tree_type)
10200 == tcc_comparison
10201 ? boolean_type_node : TREE_TYPE (current.lhs),
10202 current.lhs.get_value (), rhs.get_value ());
10203 SET_EXPR_LOCATION (current.lhs, combined_loc);
10206 else
10208 op_location_t op_loc (current.loc, combined_loc);
10209 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10210 current.lhs, current.lhs_type,
10211 rhs, rhs_type, NULL_TREE, &overload,
10212 complain_flags (decltype_p));
10213 /* TODO: build_x_binary_op doesn't always honor the location. */
10214 current.lhs.set_location (combined_loc);
10216 current.lhs_type = current.tree_type;
10218 /* If the binary operator required the use of an overloaded operator,
10219 then this expression cannot be an integral constant-expression.
10220 An overloaded operator can be used even if both operands are
10221 otherwise permissible in an integral constant-expression if at
10222 least one of the operands is of enumeration type. */
10224 if (overload
10225 && cp_parser_non_integral_constant_expression (parser,
10226 NIC_OVERLOADED))
10227 return error_mark_node;
10230 return current.lhs;
10233 static cp_expr
10234 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10235 bool no_toplevel_fold_p,
10236 enum cp_parser_prec prec,
10237 cp_id_kind * pidk)
10239 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10240 /*decltype*/false, prec, pidk);
10243 /* Parse the `? expression : assignment-expression' part of a
10244 conditional-expression. The LOGICAL_OR_EXPR is the
10245 logical-or-expression that started the conditional-expression.
10246 Returns a representation of the entire conditional-expression.
10248 This routine is used by cp_parser_assignment_expression.
10250 ? expression : assignment-expression
10252 GNU Extensions:
10254 ? : assignment-expression */
10256 static tree
10257 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10259 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10260 cp_expr assignment_expr;
10261 struct cp_token *token;
10262 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10264 /* Consume the `?' token. */
10265 cp_lexer_consume_token (parser->lexer);
10266 token = cp_lexer_peek_token (parser->lexer);
10267 if (cp_parser_allow_gnu_extensions_p (parser)
10268 && token->type == CPP_COLON)
10270 pedwarn (token->location, OPT_Wpedantic,
10271 "ISO C++ does not allow %<?:%> with omitted middle operand");
10272 /* Implicit true clause. */
10273 expr = NULL_TREE;
10274 c_inhibit_evaluation_warnings +=
10275 folded_logical_or_expr == truthvalue_true_node;
10276 warn_for_omitted_condop (token->location, logical_or_expr);
10278 else
10280 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10281 parser->colon_corrects_to_scope_p = false;
10282 /* Parse the expression. */
10283 c_inhibit_evaluation_warnings +=
10284 folded_logical_or_expr == truthvalue_false_node;
10285 expr = cp_parser_expression (parser);
10286 c_inhibit_evaluation_warnings +=
10287 ((folded_logical_or_expr == truthvalue_true_node)
10288 - (folded_logical_or_expr == truthvalue_false_node));
10289 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10292 /* The next token should be a `:'. */
10293 cp_parser_require (parser, CPP_COLON, RT_COLON);
10294 /* Parse the assignment-expression. */
10295 assignment_expr = cp_parser_assignment_expression (parser);
10296 c_inhibit_evaluation_warnings -=
10297 folded_logical_or_expr == truthvalue_true_node;
10299 /* Make a location:
10300 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10301 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10302 with the caret at the "?", ranging from the start of
10303 the logical_or_expr to the end of the assignment_expr. */
10304 loc = make_location (loc,
10305 logical_or_expr.get_start (),
10306 assignment_expr.get_finish ());
10308 /* Build the conditional-expression. */
10309 return build_x_conditional_expr (loc, logical_or_expr,
10310 expr,
10311 assignment_expr,
10312 tf_warning_or_error);
10315 /* Parse an assignment-expression.
10317 assignment-expression:
10318 conditional-expression
10319 logical-or-expression assignment-operator assignment_expression
10320 throw-expression
10321 yield-expression
10323 CAST_P is true if this expression is the target of a cast.
10324 DECLTYPE_P is true if this expression is the operand of decltype.
10326 Returns a representation for the expression. */
10328 static cp_expr
10329 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10330 bool cast_p, bool decltype_p)
10332 cp_expr expr;
10334 /* If the next token is the `throw' keyword, then we're looking at
10335 a throw-expression. */
10336 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10337 expr = cp_parser_throw_expression (parser);
10338 /* If the next token is the `co_yield' keyword, then we're looking at
10339 a yield-expression. */
10340 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10341 expr = cp_parser_yield_expression (parser);
10342 /* Otherwise, it must be that we are looking at a
10343 logical-or-expression. */
10344 else
10346 /* Parse the binary expressions (logical-or-expression). */
10347 expr = cp_parser_binary_expression (parser, cast_p, false,
10348 decltype_p,
10349 PREC_NOT_OPERATOR, pidk);
10350 /* If the next token is a `?' then we're actually looking at a
10351 conditional-expression. */
10352 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10353 return cp_parser_question_colon_clause (parser, expr);
10354 else
10356 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10358 /* If it's an assignment-operator, we're using the second
10359 production. */
10360 enum tree_code assignment_operator
10361 = cp_parser_assignment_operator_opt (parser);
10362 if (assignment_operator != ERROR_MARK)
10364 bool non_constant_p;
10366 /* Parse the right-hand side of the assignment. */
10367 cp_expr rhs = cp_parser_initializer_clause (parser,
10368 &non_constant_p);
10370 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10371 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10373 /* An assignment may not appear in a
10374 constant-expression. */
10375 if (cp_parser_non_integral_constant_expression (parser,
10376 NIC_ASSIGNMENT))
10377 return error_mark_node;
10378 /* Build the assignment expression. Its default
10379 location:
10380 LHS = RHS
10381 ~~~~^~~~~
10382 is the location of the '=' token as the
10383 caret, ranging from the start of the lhs to the
10384 end of the rhs. */
10385 loc = make_location (loc,
10386 expr.get_start (),
10387 rhs.get_finish ());
10388 expr = build_x_modify_expr (loc, expr,
10389 assignment_operator,
10390 rhs, NULL_TREE,
10391 complain_flags (decltype_p));
10392 /* TODO: build_x_modify_expr doesn't honor the location,
10393 so we must set it here. */
10394 expr.set_location (loc);
10399 return expr;
10402 /* Parse an (optional) assignment-operator.
10404 assignment-operator: one of
10405 = *= /= %= += -= >>= <<= &= ^= |=
10407 GNU Extension:
10409 assignment-operator: one of
10410 <?= >?=
10412 If the next token is an assignment operator, the corresponding tree
10413 code is returned, and the token is consumed. For example, for
10414 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10415 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10416 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10417 operator, ERROR_MARK is returned. */
10419 static enum tree_code
10420 cp_parser_assignment_operator_opt (cp_parser* parser)
10422 enum tree_code op;
10423 cp_token *token;
10425 /* Peek at the next token. */
10426 token = cp_lexer_peek_token (parser->lexer);
10428 switch (token->type)
10430 case CPP_EQ:
10431 op = NOP_EXPR;
10432 break;
10434 case CPP_MULT_EQ:
10435 op = MULT_EXPR;
10436 break;
10438 case CPP_DIV_EQ:
10439 op = TRUNC_DIV_EXPR;
10440 break;
10442 case CPP_MOD_EQ:
10443 op = TRUNC_MOD_EXPR;
10444 break;
10446 case CPP_PLUS_EQ:
10447 op = PLUS_EXPR;
10448 break;
10450 case CPP_MINUS_EQ:
10451 op = MINUS_EXPR;
10452 break;
10454 case CPP_RSHIFT_EQ:
10455 op = RSHIFT_EXPR;
10456 break;
10458 case CPP_LSHIFT_EQ:
10459 op = LSHIFT_EXPR;
10460 break;
10462 case CPP_AND_EQ:
10463 op = BIT_AND_EXPR;
10464 break;
10466 case CPP_XOR_EQ:
10467 op = BIT_XOR_EXPR;
10468 break;
10470 case CPP_OR_EQ:
10471 op = BIT_IOR_EXPR;
10472 break;
10474 default:
10475 /* Nothing else is an assignment operator. */
10476 op = ERROR_MARK;
10479 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10480 if (op != ERROR_MARK
10481 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10482 op = ERROR_MARK;
10484 /* If it was an assignment operator, consume it. */
10485 if (op != ERROR_MARK)
10486 cp_lexer_consume_token (parser->lexer);
10488 return op;
10491 /* Parse an expression.
10493 expression:
10494 assignment-expression
10495 expression , assignment-expression
10497 CAST_P is true if this expression is the target of a cast.
10498 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10499 except possibly parenthesized or on the RHS of a comma (N3276).
10500 WARN_COMMA_P is true if a comma should be diagnosed.
10502 Returns a representation of the expression. */
10504 static cp_expr
10505 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10506 bool cast_p, bool decltype_p, bool warn_comma_p)
10508 cp_expr expression = NULL_TREE;
10509 location_t loc = UNKNOWN_LOCATION;
10511 while (true)
10513 cp_expr assignment_expression;
10515 /* Parse the next assignment-expression. */
10516 assignment_expression
10517 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10519 /* We don't create a temporary for a call that is the immediate operand
10520 of decltype or on the RHS of a comma. But when we see a comma, we
10521 need to create a temporary for a call on the LHS. */
10522 if (decltype_p && !processing_template_decl
10523 && TREE_CODE (assignment_expression) == CALL_EXPR
10524 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10525 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10526 assignment_expression
10527 = build_cplus_new (TREE_TYPE (assignment_expression),
10528 assignment_expression, tf_warning_or_error);
10530 /* If this is the first assignment-expression, we can just
10531 save it away. */
10532 if (!expression)
10533 expression = assignment_expression;
10534 else
10536 /* Create a location with caret at the comma, ranging
10537 from the start of the LHS to the end of the RHS. */
10538 loc = make_location (loc,
10539 expression.get_start (),
10540 assignment_expression.get_finish ());
10541 expression = build_x_compound_expr (loc, expression,
10542 assignment_expression, NULL_TREE,
10543 complain_flags (decltype_p));
10544 expression.set_location (loc);
10546 /* If the next token is not a comma, or we're in a fold-expression, then
10547 we are done with the expression. */
10548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10549 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10550 break;
10551 /* Consume the `,'. */
10552 loc = cp_lexer_peek_token (parser->lexer)->location;
10553 if (warn_comma_p)
10555 /* [depr.comma.subscript]: A comma expression appearing as
10556 the expr-or-braced-init-list of a subscripting expression
10557 is deprecated. A parenthesized comma expression is not
10558 deprecated. */
10559 warning_at (loc, OPT_Wcomma_subscript,
10560 "top-level comma expression in array subscript "
10561 "is deprecated");
10562 warn_comma_p = false;
10564 cp_lexer_consume_token (parser->lexer);
10565 /* A comma operator cannot appear in a constant-expression. */
10566 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10567 expression = error_mark_node;
10570 return expression;
10573 /* Parse a constant-expression.
10575 constant-expression:
10576 conditional-expression
10578 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10579 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10580 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10581 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10582 greater than 1, this isn't really a constant-expression, only a
10583 potentially constant-evaluated expression. If STRICT_P is true,
10584 only parse a conditional-expression, otherwise parse an
10585 assignment-expression. See below for rationale. */
10587 static cp_expr
10588 cp_parser_constant_expression (cp_parser* parser,
10589 int allow_non_constant_p,
10590 bool *non_constant_p,
10591 bool strict_p)
10593 bool saved_integral_constant_expression_p;
10594 bool saved_allow_non_integral_constant_expression_p;
10595 bool saved_non_integral_constant_expression_p;
10596 cp_expr expression;
10598 /* It might seem that we could simply parse the
10599 conditional-expression, and then check to see if it were
10600 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10601 one that the compiler can figure out is constant, possibly after
10602 doing some simplifications or optimizations. The standard has a
10603 precise definition of constant-expression, and we must honor
10604 that, even though it is somewhat more restrictive.
10606 For example:
10608 int i[(2, 3)];
10610 is not a legal declaration, because `(2, 3)' is not a
10611 constant-expression. The `,' operator is forbidden in a
10612 constant-expression. However, GCC's constant-folding machinery
10613 will fold this operation to an INTEGER_CST for `3'. */
10615 /* Save the old settings. */
10616 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10617 saved_allow_non_integral_constant_expression_p
10618 = parser->allow_non_integral_constant_expression_p;
10619 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10620 /* We are now parsing a constant-expression. */
10621 parser->integral_constant_expression_p = true;
10622 parser->allow_non_integral_constant_expression_p
10623 = (allow_non_constant_p || cxx_dialect >= cxx11);
10624 parser->non_integral_constant_expression_p = false;
10626 /* A manifestly constant-evaluated expression is evaluated even in an
10627 unevaluated operand. */
10628 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
10630 /* Although the grammar says "conditional-expression", when not STRICT_P,
10631 we parse an "assignment-expression", which also permits
10632 "throw-expression" and the use of assignment operators. In the case
10633 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10634 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10635 actually essential that we look for an assignment-expression.
10636 For example, cp_parser_initializer_clauses uses this function to
10637 determine whether a particular assignment-expression is in fact
10638 constant. */
10639 if (strict_p)
10641 /* Parse the binary expressions (logical-or-expression). */
10642 expression = cp_parser_binary_expression (parser, false, false, false,
10643 PREC_NOT_OPERATOR, NULL);
10644 /* If the next token is a `?' then we're actually looking at
10645 a conditional-expression; otherwise we're done. */
10646 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10647 expression = cp_parser_question_colon_clause (parser, expression);
10649 else
10650 expression = cp_parser_assignment_expression (parser);
10651 /* Restore the old settings. */
10652 parser->integral_constant_expression_p
10653 = saved_integral_constant_expression_p;
10654 parser->allow_non_integral_constant_expression_p
10655 = saved_allow_non_integral_constant_expression_p;
10656 if (cxx_dialect >= cxx11)
10658 /* Require an rvalue constant expression here; that's what our
10659 callers expect. Reference constant expressions are handled
10660 separately in e.g. cp_parser_template_argument. */
10661 tree decay = expression;
10662 if (TREE_TYPE (expression)
10663 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10664 decay = build_address (expression);
10665 bool is_const = is_rvalue_constant_expression (decay);
10666 parser->non_integral_constant_expression_p = !is_const;
10667 if (!is_const && !allow_non_constant_p)
10668 require_rvalue_constant_expression (decay);
10670 if (allow_non_constant_p)
10671 *non_constant_p = parser->non_integral_constant_expression_p;
10672 parser->non_integral_constant_expression_p
10673 = saved_non_integral_constant_expression_p;
10675 return expression;
10678 /* Parse __builtin_offsetof.
10680 offsetof-expression:
10681 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10683 offsetof-member-designator:
10684 id-expression
10685 | offsetof-member-designator "." id-expression
10686 | offsetof-member-designator "[" expression "]"
10687 | offsetof-member-designator "->" id-expression */
10689 static cp_expr
10690 cp_parser_builtin_offsetof (cp_parser *parser)
10692 int save_ice_p, save_non_ice_p;
10693 tree type;
10694 cp_expr expr;
10695 cp_id_kind dummy;
10696 cp_token *token;
10697 location_t finish_loc;
10699 /* We're about to accept non-integral-constant things, but will
10700 definitely yield an integral constant expression. Save and
10701 restore these values around our local parsing. */
10702 save_ice_p = parser->integral_constant_expression_p;
10703 save_non_ice_p = parser->non_integral_constant_expression_p;
10705 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10707 /* Consume the "__builtin_offsetof" token. */
10708 cp_lexer_consume_token (parser->lexer);
10709 /* Consume the opening `('. */
10710 matching_parens parens;
10711 parens.require_open (parser);
10712 /* Parse the type-id. */
10713 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10715 const char *saved_message = parser->type_definition_forbidden_message;
10716 parser->type_definition_forbidden_message
10717 = G_("types may not be defined within %<__builtin_offsetof%>");
10718 type = cp_parser_type_id (parser);
10719 parser->type_definition_forbidden_message = saved_message;
10721 /* Look for the `,'. */
10722 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10723 token = cp_lexer_peek_token (parser->lexer);
10725 /* Build the (type *)null that begins the traditional offsetof macro. */
10726 tree object_ptr
10727 = build_static_cast (input_location, build_pointer_type (type),
10728 null_pointer_node, tf_warning_or_error);
10730 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10731 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10732 true, &dummy, token->location);
10733 while (true)
10735 token = cp_lexer_peek_token (parser->lexer);
10736 switch (token->type)
10738 case CPP_OPEN_SQUARE:
10739 /* offsetof-member-designator "[" expression "]" */
10740 expr = cp_parser_postfix_open_square_expression (parser, expr,
10741 true, false);
10742 break;
10744 case CPP_DEREF:
10745 /* offsetof-member-designator "->" identifier */
10746 expr = grok_array_decl (token->location, expr, integer_zero_node,
10747 NULL, tf_warning_or_error);
10748 /* FALLTHRU */
10750 case CPP_DOT:
10751 /* offsetof-member-designator "." identifier */
10752 cp_lexer_consume_token (parser->lexer);
10753 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10754 expr, true, &dummy,
10755 token->location);
10756 break;
10758 case CPP_CLOSE_PAREN:
10759 /* Consume the ")" token. */
10760 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10761 cp_lexer_consume_token (parser->lexer);
10762 goto success;
10764 default:
10765 /* Error. We know the following require will fail, but
10766 that gives the proper error message. */
10767 parens.require_close (parser);
10768 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10769 expr = error_mark_node;
10770 goto failure;
10774 success:
10775 /* Make a location of the form:
10776 __builtin_offsetof (struct s, f)
10777 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10778 with caret at the type-id, ranging from the start of the
10779 "_builtin_offsetof" token to the close paren. */
10780 loc = make_location (loc, start_loc, finish_loc);
10781 /* The result will be an INTEGER_CST, so we need to explicitly
10782 preserve the location. */
10783 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10785 failure:
10786 parser->integral_constant_expression_p = save_ice_p;
10787 parser->non_integral_constant_expression_p = save_non_ice_p;
10789 expr = expr.maybe_add_location_wrapper ();
10790 return expr;
10793 /* Parse a trait expression.
10795 Returns a representation of the expression, the underlying type
10796 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10798 static cp_expr
10799 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10801 cp_trait_kind kind;
10802 tree type1, type2 = NULL_TREE;
10803 bool binary = false;
10804 bool variadic = false;
10806 switch (keyword)
10808 case RID_HAS_NOTHROW_ASSIGN:
10809 kind = CPTK_HAS_NOTHROW_ASSIGN;
10810 break;
10811 case RID_HAS_NOTHROW_CONSTRUCTOR:
10812 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10813 break;
10814 case RID_HAS_NOTHROW_COPY:
10815 kind = CPTK_HAS_NOTHROW_COPY;
10816 break;
10817 case RID_HAS_TRIVIAL_ASSIGN:
10818 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10819 break;
10820 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10821 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10822 break;
10823 case RID_HAS_TRIVIAL_COPY:
10824 kind = CPTK_HAS_TRIVIAL_COPY;
10825 break;
10826 case RID_HAS_TRIVIAL_DESTRUCTOR:
10827 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10828 break;
10829 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10830 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10831 break;
10832 case RID_HAS_VIRTUAL_DESTRUCTOR:
10833 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10834 break;
10835 case RID_IS_ABSTRACT:
10836 kind = CPTK_IS_ABSTRACT;
10837 break;
10838 case RID_IS_AGGREGATE:
10839 kind = CPTK_IS_AGGREGATE;
10840 break;
10841 case RID_IS_BASE_OF:
10842 kind = CPTK_IS_BASE_OF;
10843 binary = true;
10844 break;
10845 case RID_IS_CLASS:
10846 kind = CPTK_IS_CLASS;
10847 break;
10848 case RID_IS_EMPTY:
10849 kind = CPTK_IS_EMPTY;
10850 break;
10851 case RID_IS_ENUM:
10852 kind = CPTK_IS_ENUM;
10853 break;
10854 case RID_IS_FINAL:
10855 kind = CPTK_IS_FINAL;
10856 break;
10857 case RID_IS_LAYOUT_COMPATIBLE:
10858 kind = CPTK_IS_LAYOUT_COMPATIBLE;
10859 binary = true;
10860 break;
10861 case RID_IS_LITERAL_TYPE:
10862 kind = CPTK_IS_LITERAL_TYPE;
10863 break;
10864 case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
10865 kind = CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF;
10866 binary = true;
10867 break;
10868 case RID_IS_POD:
10869 kind = CPTK_IS_POD;
10870 break;
10871 case RID_IS_POLYMORPHIC:
10872 kind = CPTK_IS_POLYMORPHIC;
10873 break;
10874 case RID_IS_SAME_AS:
10875 kind = CPTK_IS_SAME_AS;
10876 binary = true;
10877 break;
10878 case RID_IS_STD_LAYOUT:
10879 kind = CPTK_IS_STD_LAYOUT;
10880 break;
10881 case RID_IS_TRIVIAL:
10882 kind = CPTK_IS_TRIVIAL;
10883 break;
10884 case RID_IS_TRIVIALLY_ASSIGNABLE:
10885 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10886 binary = true;
10887 break;
10888 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10889 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10890 variadic = true;
10891 break;
10892 case RID_IS_TRIVIALLY_COPYABLE:
10893 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10894 break;
10895 case RID_IS_UNION:
10896 kind = CPTK_IS_UNION;
10897 break;
10898 case RID_UNDERLYING_TYPE:
10899 kind = CPTK_UNDERLYING_TYPE;
10900 break;
10901 case RID_BASES:
10902 kind = CPTK_BASES;
10903 break;
10904 case RID_DIRECT_BASES:
10905 kind = CPTK_DIRECT_BASES;
10906 break;
10907 case RID_IS_ASSIGNABLE:
10908 kind = CPTK_IS_ASSIGNABLE;
10909 binary = true;
10910 break;
10911 case RID_IS_CONSTRUCTIBLE:
10912 kind = CPTK_IS_CONSTRUCTIBLE;
10913 variadic = true;
10914 break;
10915 case RID_IS_NOTHROW_ASSIGNABLE:
10916 kind = CPTK_IS_NOTHROW_ASSIGNABLE;
10917 binary = true;
10918 break;
10919 case RID_IS_NOTHROW_CONSTRUCTIBLE:
10920 kind = CPTK_IS_NOTHROW_CONSTRUCTIBLE;
10921 variadic = true;
10922 break;
10923 default:
10924 gcc_unreachable ();
10927 /* Get location of initial token. */
10928 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10930 /* Consume the token. */
10931 cp_lexer_consume_token (parser->lexer);
10933 matching_parens parens;
10934 parens.require_open (parser);
10937 type_id_in_expr_sentinel s (parser);
10938 type1 = cp_parser_type_id (parser);
10941 if (type1 == error_mark_node)
10942 return error_mark_node;
10944 if (binary)
10946 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10949 type_id_in_expr_sentinel s (parser);
10950 type2 = cp_parser_type_id (parser);
10953 if (type2 == error_mark_node)
10954 return error_mark_node;
10956 else if (variadic)
10958 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10960 cp_lexer_consume_token (parser->lexer);
10961 tree elt = cp_parser_type_id (parser);
10962 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10964 cp_lexer_consume_token (parser->lexer);
10965 elt = make_pack_expansion (elt);
10967 if (elt == error_mark_node)
10968 return error_mark_node;
10969 type2 = tree_cons (NULL_TREE, elt, type2);
10971 type2 = nreverse (type2);
10974 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10975 parens.require_close (parser);
10977 /* Construct a location of the form:
10978 __is_trivially_copyable(_Tp)
10979 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10980 with start == caret, finishing at the close-paren. */
10981 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10983 /* Complete the trait expression, which may mean either processing
10984 the trait expr now or saving it for template instantiation. */
10985 switch (kind)
10987 case CPTK_UNDERLYING_TYPE:
10988 return cp_expr (finish_underlying_type (type1), trait_loc);
10989 case CPTK_BASES:
10990 return cp_expr (finish_bases (type1, false), trait_loc);
10991 case CPTK_DIRECT_BASES:
10992 return cp_expr (finish_bases (type1, true), trait_loc);
10993 default:
10994 return finish_trait_expr (trait_loc, kind, type1, type2);
10998 /* Parse a lambda expression.
11000 lambda-expression:
11001 lambda-introducer lambda-declarator [opt] compound-statement
11002 lambda-introducer < template-parameter-list > requires-clause [opt]
11003 lambda-declarator [opt] compound-statement
11005 Returns a representation of the expression. */
11007 static cp_expr
11008 cp_parser_lambda_expression (cp_parser* parser)
11010 tree lambda_expr = build_lambda_expr ();
11011 tree type;
11012 bool ok = true;
11013 cp_token *token = cp_lexer_peek_token (parser->lexer);
11014 cp_token_position start = 0;
11016 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11018 if (cxx_dialect >= cxx20)
11020 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11021 non-type parameter is nonsensical.
11023 Distinguish a lambda in the parameter type from a lambda in the
11024 default argument by looking at local_variables_forbidden_p, which is
11025 only set in default arguments. */
11026 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11028 error_at (token->location,
11029 "lambda-expression in template parameter type");
11030 token->error_reported = true;
11031 ok = false;
11034 else if (cp_unevaluated_operand)
11036 if (!token->error_reported)
11038 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11039 "lambda-expression in unevaluated context"
11040 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11041 token->error_reported = true;
11043 ok = false;
11045 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11047 if (!token->error_reported)
11049 error_at (token->location, "lambda-expression in template-argument"
11050 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11051 token->error_reported = true;
11053 ok = false;
11056 /* We may be in the middle of deferred access check. Disable
11057 it now. */
11058 push_deferring_access_checks (dk_no_deferred);
11060 cp_parser_lambda_introducer (parser, lambda_expr);
11061 if (cp_parser_error_occurred (parser))
11062 return error_mark_node;
11064 type = begin_lambda_type (lambda_expr);
11065 if (type == error_mark_node)
11066 return error_mark_node;
11068 record_lambda_scope (lambda_expr);
11070 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11071 determine_visibility (TYPE_NAME (type));
11073 /* Now that we've started the type, add the capture fields for any
11074 explicit captures. */
11075 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11078 /* Inside the class, surrounding template-parameter-lists do not apply. */
11079 unsigned int saved_num_template_parameter_lists
11080 = parser->num_template_parameter_lists;
11081 unsigned char in_statement = parser->in_statement;
11082 bool in_switch_statement_p = parser->in_switch_statement_p;
11083 bool fully_implicit_function_template_p
11084 = parser->fully_implicit_function_template_p;
11085 tree implicit_template_parms = parser->implicit_template_parms;
11086 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11087 bool auto_is_implicit_function_template_parm_p
11088 = parser->auto_is_implicit_function_template_parm_p;
11090 parser->num_template_parameter_lists = 0;
11091 parser->in_statement = 0;
11092 parser->in_switch_statement_p = false;
11093 parser->fully_implicit_function_template_p = false;
11094 parser->implicit_template_parms = 0;
11095 parser->implicit_template_scope = 0;
11096 parser->auto_is_implicit_function_template_parm_p = false;
11098 /* The body of a lambda in a discarded statement is not discarded. */
11099 bool discarded = in_discarded_stmt;
11100 in_discarded_stmt = 0;
11102 /* Similarly the body of a lambda in immediate function context is not
11103 in immediate function context. */
11104 bool save_in_consteval_if_p = in_consteval_if_p;
11105 in_consteval_if_p = false;
11107 /* By virtue of defining a local class, a lambda expression has access to
11108 the private variables of enclosing classes. */
11110 if (cp_parser_start_tentative_firewall (parser))
11111 start = token;
11113 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11115 if (ok && cp_parser_error_occurred (parser))
11116 ok = false;
11118 if (ok)
11120 cp_parser_lambda_body (parser, lambda_expr);
11122 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11124 if (cp_parser_skip_to_closing_brace (parser))
11125 cp_lexer_consume_token (parser->lexer);
11128 /* The capture list was built up in reverse order; fix that now. */
11129 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11130 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11132 if (ok)
11133 maybe_add_lambda_conv_op (type);
11135 finish_struct (type, /*attributes=*/NULL_TREE);
11137 in_consteval_if_p = save_in_consteval_if_p;
11138 in_discarded_stmt = discarded;
11140 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11141 parser->in_statement = in_statement;
11142 parser->in_switch_statement_p = in_switch_statement_p;
11143 parser->fully_implicit_function_template_p
11144 = fully_implicit_function_template_p;
11145 parser->implicit_template_parms = implicit_template_parms;
11146 parser->implicit_template_scope = implicit_template_scope;
11147 parser->auto_is_implicit_function_template_parm_p
11148 = auto_is_implicit_function_template_parm_p;
11151 /* This field is only used during parsing of the lambda. */
11152 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11154 /* This lambda shouldn't have any proxies left at this point. */
11155 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11156 /* And now that we're done, push proxies for an enclosing lambda. */
11157 insert_pending_capture_proxies ();
11159 /* Update the lambda expression to a range. */
11160 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11161 token->location,
11162 parser->lexer);
11164 if (ok)
11165 lambda_expr = build_lambda_object (lambda_expr);
11166 else
11167 lambda_expr = error_mark_node;
11169 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11171 pop_deferring_access_checks ();
11173 return lambda_expr;
11176 /* Parse the beginning of a lambda expression.
11178 lambda-introducer:
11179 [ lambda-capture [opt] ]
11181 LAMBDA_EXPR is the current representation of the lambda expression. */
11183 static void
11184 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11186 /* Need commas after the first capture. */
11187 bool first = true;
11189 /* Eat the leading `['. */
11190 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11192 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11193 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11194 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11195 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11196 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11197 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11198 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11199 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11201 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11203 cp_lexer_consume_token (parser->lexer);
11204 first = false;
11206 if (!(at_function_scope_p () || parsing_nsdmi ()))
11207 error ("non-local lambda expression cannot have a capture-default");
11210 hash_set<tree, true> ids;
11211 tree first_capture_id = NULL_TREE;
11212 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11214 cp_token* capture_token;
11215 tree capture_id;
11216 tree capture_init_expr;
11217 cp_id_kind idk = CP_ID_KIND_NONE;
11218 bool explicit_init_p = false;
11220 enum capture_kind_type
11222 BY_COPY,
11223 BY_REFERENCE
11225 enum capture_kind_type capture_kind = BY_COPY;
11227 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11229 error ("expected end of capture-list");
11230 return;
11233 if (first)
11234 first = false;
11235 else
11236 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11238 /* Possibly capture `this'. */
11239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11241 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11242 if (cxx_dialect < cxx20 && pedantic
11243 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11244 pedwarn (loc, OPT_Wc__20_extensions,
11245 "explicit by-copy capture of %<this%> "
11246 "with by-copy capture default only available with "
11247 "%<-std=c++20%> or %<-std=gnu++20%>");
11248 cp_lexer_consume_token (parser->lexer);
11249 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11250 pedwarn (input_location, 0,
11251 "already captured %qD in lambda expression",
11252 this_identifier);
11253 else
11254 add_capture (lambda_expr, /*id=*/this_identifier,
11255 /*initializer=*/finish_this_expr (),
11256 /*by_reference_p=*/true, explicit_init_p);
11257 continue;
11260 /* Possibly capture `*this'. */
11261 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11262 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11264 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11265 if (cxx_dialect < cxx17)
11266 pedwarn (loc, OPT_Wc__17_extensions,
11267 "%<*this%> capture only available with "
11268 "%<-std=c++17%> or %<-std=gnu++17%>");
11269 cp_lexer_consume_token (parser->lexer);
11270 cp_lexer_consume_token (parser->lexer);
11271 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11272 pedwarn (input_location, 0,
11273 "already captured %qD in lambda expression",
11274 this_identifier);
11275 else
11276 add_capture (lambda_expr, /*id=*/this_identifier,
11277 /*initializer=*/finish_this_expr (),
11278 /*by_reference_p=*/false, explicit_init_p);
11279 continue;
11282 /* But reject `&this'. */
11283 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11284 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11286 error_at (cp_lexer_peek_token (parser->lexer)->location,
11287 "%<this%> cannot be captured by reference");
11288 cp_lexer_consume_token (parser->lexer);
11289 cp_lexer_consume_token (parser->lexer);
11290 continue;
11293 /* Remember whether we want to capture as a reference or not. */
11294 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11296 capture_kind = BY_REFERENCE;
11297 cp_lexer_consume_token (parser->lexer);
11300 bool init_pack_expansion = false;
11301 location_t ellipsis_loc = UNKNOWN_LOCATION;
11302 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11304 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11305 if (cxx_dialect < cxx20)
11306 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11307 "pack init-capture only available with "
11308 "%<-std=c++20%> or %<-std=gnu++20%>");
11309 cp_lexer_consume_token (parser->lexer);
11310 init_pack_expansion = true;
11313 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11314 if (init_pack_expansion && capture_kind != BY_REFERENCE
11315 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11317 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11318 0, "%<&%> should come before %<...%>");
11319 capture_kind = BY_REFERENCE;
11320 cp_lexer_consume_token (parser->lexer);
11323 /* Get the identifier. */
11324 capture_token = cp_lexer_peek_token (parser->lexer);
11325 capture_id = cp_parser_identifier (parser);
11327 if (capture_id == error_mark_node)
11328 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11329 delimiters, but I modified this to stop on unnested ']' as well. It
11330 was already changed to stop on unnested '}', so the
11331 "closing_parenthesis" name is no more misleading with my change. */
11333 cp_parser_skip_to_closing_parenthesis (parser,
11334 /*recovering=*/true,
11335 /*or_comma=*/true,
11336 /*consume_paren=*/true);
11337 break;
11340 /* Find the initializer for this capture. */
11341 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11342 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11343 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11345 bool direct, non_constant;
11346 /* An explicit initializer exists. */
11347 if (cxx_dialect < cxx14)
11348 pedwarn (input_location, OPT_Wc__14_extensions,
11349 "lambda capture initializers "
11350 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11351 capture_init_expr = cp_parser_initializer (parser, &direct,
11352 &non_constant, true);
11353 explicit_init_p = true;
11354 if (capture_init_expr == NULL_TREE)
11356 error ("empty initializer for lambda init-capture");
11357 capture_init_expr = error_mark_node;
11359 if (init_pack_expansion)
11360 capture_init_expr = make_pack_expansion (capture_init_expr);
11362 else
11364 const char* error_msg;
11366 /* Turn the identifier into an id-expression. */
11367 capture_init_expr
11368 = cp_parser_lookup_name_simple (parser, capture_id,
11369 capture_token->location);
11371 if (capture_init_expr == error_mark_node)
11373 unqualified_name_lookup_error (capture_id);
11374 continue;
11376 else if (!VAR_P (capture_init_expr)
11377 && TREE_CODE (capture_init_expr) != PARM_DECL)
11379 error_at (capture_token->location,
11380 "capture of non-variable %qE",
11381 capture_init_expr);
11382 if (DECL_P (capture_init_expr))
11383 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11384 "%q#D declared here", capture_init_expr);
11385 continue;
11387 if (VAR_P (capture_init_expr)
11388 && decl_storage_duration (capture_init_expr) != dk_auto)
11390 if (pedwarn (capture_token->location, 0, "capture of variable "
11391 "%qD with non-automatic storage duration",
11392 capture_init_expr))
11393 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11394 "%q#D declared here", capture_init_expr);
11395 continue;
11398 capture_init_expr
11399 = finish_id_expression
11400 (capture_id,
11401 capture_init_expr,
11402 parser->scope,
11403 &idk,
11404 /*integral_constant_expression_p=*/false,
11405 /*allow_non_integral_constant_expression_p=*/false,
11406 /*non_integral_constant_expression_p=*/NULL,
11407 /*template_p=*/false,
11408 /*done=*/true,
11409 /*address_p=*/false,
11410 /*template_arg_p=*/false,
11411 &error_msg,
11412 capture_token->location);
11414 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11416 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11417 cp_lexer_consume_token (parser->lexer);
11418 capture_init_expr = make_pack_expansion (capture_init_expr);
11419 if (init_pack_expansion)
11421 /* If what follows is an initializer, the second '...' is
11422 invalid. But for cases like [...xs...], the first one
11423 is invalid. */
11424 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11425 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11426 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11427 ellipsis_loc = loc;
11428 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11429 continue;
11434 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11435 && !explicit_init_p)
11437 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11438 && capture_kind == BY_COPY)
11439 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11440 "of %qD redundant with by-copy capture default",
11441 capture_id);
11442 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11443 && capture_kind == BY_REFERENCE)
11444 pedwarn (capture_token->location, 0, "explicit by-reference "
11445 "capture of %qD redundant with by-reference capture "
11446 "default", capture_id);
11449 /* Check for duplicates.
11450 Optimize for the zero or one explicit captures cases and only create
11451 the hash_set after adding second capture. */
11452 bool found = false;
11453 if (!ids.is_empty ())
11454 found = ids.add (capture_id);
11455 else if (first_capture_id == NULL_TREE)
11456 first_capture_id = capture_id;
11457 else if (capture_id == first_capture_id)
11458 found = true;
11459 else
11461 ids.add (first_capture_id);
11462 ids.add (capture_id);
11464 if (found)
11465 pedwarn (input_location, 0,
11466 "already captured %qD in lambda expression", capture_id);
11467 else
11468 add_capture (lambda_expr, capture_id, capture_init_expr,
11469 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11470 explicit_init_p);
11472 /* If there is any qualification still in effect, clear it
11473 now; we will be starting fresh with the next capture. */
11474 parser->scope = NULL_TREE;
11475 parser->qualifying_scope = NULL_TREE;
11476 parser->object_scope = NULL_TREE;
11479 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11482 /* Parse the (optional) middle of a lambda expression.
11484 lambda-declarator:
11485 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11486 lambda-specifiers (C++23)
11488 lambda-specifiers:
11489 decl-specifier-seq [opt] noexcept-specifier [opt]
11490 attribute-specifier-seq [opt] trailing-return-type [opt]
11492 LAMBDA_EXPR is the current representation of the lambda expression. */
11494 static bool
11495 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11497 /* 5.1.1.4 of the standard says:
11498 If a lambda-expression does not include a lambda-declarator, it is as if
11499 the lambda-declarator were ().
11500 This means an empty parameter list, no attributes, and no exception
11501 specification. */
11502 tree param_list = void_list_node;
11503 tree std_attrs = NULL_TREE;
11504 tree gnu_attrs = NULL_TREE;
11505 tree exception_spec = NULL_TREE;
11506 tree template_param_list = NULL_TREE;
11507 tree tx_qual = NULL_TREE;
11508 tree return_type = NULL_TREE;
11509 tree trailing_requires_clause = NULL_TREE;
11510 bool has_param_list = false;
11511 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11512 cp_decl_specifier_seq lambda_specs;
11513 clear_decl_specs (&lambda_specs);
11514 /* A lambda op() is const unless explicitly 'mutable'. */
11515 cp_cv_quals quals = TYPE_QUAL_CONST;
11517 /* The template-parameter-list is optional, but must begin with
11518 an opening angle if present. */
11519 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11521 if (cxx_dialect < cxx14)
11522 pedwarn (parser->lexer->next_token->location, OPT_Wc__14_extensions,
11523 "lambda templates are only available with "
11524 "%<-std=c++14%> or %<-std=gnu++14%>");
11525 else if (pedantic && cxx_dialect < cxx20)
11526 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11527 "lambda templates are only available with "
11528 "%<-std=c++20%> or %<-std=gnu++20%>");
11530 cp_lexer_consume_token (parser->lexer);
11532 template_param_list = cp_parser_template_parameter_list (parser);
11533 cp_parser_require_end_of_template_parameter_list (parser);
11535 /* We may have a constrained generic lambda; parse the requires-clause
11536 immediately after the template-parameter-list and combine with any
11537 shorthand constraints present. */
11538 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11539 if (flag_concepts)
11541 tree reqs = get_shorthand_constraints (current_template_parms);
11542 if (dreqs)
11543 reqs = combine_constraint_expressions (reqs, dreqs);
11544 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11547 /* We just processed one more parameter list. */
11548 ++parser->num_template_parameter_lists;
11551 /* Committee discussion supports allowing attributes here. */
11552 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11554 /* The parameter-declaration-clause is optional (unless
11555 template-parameter-list was given), but must begin with an
11556 opening parenthesis if present. */
11557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11559 bool is_consteval = false;
11560 /* For C++20, before parsing the parameter list check if there is
11561 a consteval specifier in the corresponding decl-specifier-seq. */
11562 if (cxx_dialect >= cxx20)
11564 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
11565 cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
11567 if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
11568 == RID_CONSTEVAL)
11570 is_consteval = true;
11571 break;
11576 matching_parens parens;
11577 parens.consume_open (parser);
11579 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11581 if (is_consteval)
11582 current_binding_level->immediate_fn_ctx_p = true;
11584 /* Parse parameters. */
11585 param_list
11586 = cp_parser_parameter_declaration_clause
11587 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11589 /* Default arguments shall not be specified in the
11590 parameter-declaration-clause of a lambda-declarator. */
11591 if (pedantic && cxx_dialect < cxx14)
11592 for (tree t = param_list; t; t = TREE_CHAIN (t))
11593 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11594 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11595 OPT_Wc__14_extensions,
11596 "default argument specified for lambda parameter");
11598 parens.require_close (parser);
11599 has_param_list = true;
11601 else if (cxx_dialect < cxx23)
11602 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11604 /* In the decl-specifier-seq of the lambda-declarator, each
11605 decl-specifier shall either be mutable or constexpr. */
11606 int declares_class_or_enum;
11607 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11608 cp_parser_decl_specifier_seq (parser,
11609 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11610 &lambda_specs, &declares_class_or_enum);
11612 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11614 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11615 "parameter declaration before lambda declaration "
11616 "specifiers only optional with %<-std=c++2b%> or "
11617 "%<-std=gnu++2b%>");
11618 omitted_parms_loc = UNKNOWN_LOCATION;
11621 if (lambda_specs.storage_class == sc_mutable)
11623 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11624 quals = TYPE_UNQUALIFIED;
11625 if (lambda_specs.conflicting_specifiers_p)
11626 error_at (lambda_specs.locations[ds_storage_class],
11627 "duplicate %<mutable%>");
11630 tx_qual = cp_parser_tx_qualifier_opt (parser);
11631 if (omitted_parms_loc && tx_qual)
11633 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11634 "parameter declaration before lambda transaction "
11635 "qualifier only optional with %<-std=c++2b%> or "
11636 "%<-std=gnu++2b%>");
11637 omitted_parms_loc = UNKNOWN_LOCATION;
11640 /* Parse optional exception specification. */
11641 exception_spec
11642 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11644 if (omitted_parms_loc && exception_spec)
11646 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11647 "parameter declaration before lambda exception "
11648 "specification only optional with %<-std=c++2b%> or "
11649 "%<-std=gnu++2b%>");
11650 omitted_parms_loc = UNKNOWN_LOCATION;
11653 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11654 attributes that appertain to the function type. */
11655 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11656 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11657 else
11658 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11660 /* Parse optional trailing return type. */
11661 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11663 if (omitted_parms_loc)
11664 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11665 "parameter declaration before lambda trailing "
11666 "return type only optional with %<-std=c++2b%> or "
11667 "%<-std=gnu++2b%>");
11668 cp_lexer_consume_token (parser->lexer);
11669 return_type = cp_parser_trailing_type_id (parser);
11672 /* Also allow GNU attributes at the very end of the declaration, the usual
11673 place for GNU attributes. */
11674 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11675 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11677 if (has_param_list)
11679 /* Parse optional trailing requires clause. */
11680 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11682 /* The function parameters must be in scope all the way until after the
11683 trailing-return-type in case of decltype. */
11684 pop_bindings_and_leave_scope ();
11687 /* Create the function call operator.
11689 Messing with declarators like this is no uglier than building up the
11690 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11691 other code. */
11693 cp_decl_specifier_seq return_type_specs;
11694 cp_declarator* declarator;
11695 tree fco;
11696 void *p;
11698 clear_decl_specs (&return_type_specs);
11699 return_type_specs.type = make_auto ();
11701 if (lambda_specs.locations[ds_constexpr])
11703 if (cxx_dialect >= cxx17)
11704 return_type_specs.locations[ds_constexpr]
11705 = lambda_specs.locations[ds_constexpr];
11706 else
11707 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11708 "lambda only available with %<-std=c++17%> or "
11709 "%<-std=gnu++17%>");
11711 if (lambda_specs.locations[ds_consteval])
11712 return_type_specs.locations[ds_consteval]
11713 = lambda_specs.locations[ds_consteval];
11715 p = obstack_alloc (&declarator_obstack, 0);
11717 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11718 LAMBDA_EXPR_LOCATION (lambda_expr));
11720 declarator = make_call_declarator (declarator, param_list, quals,
11721 VIRT_SPEC_UNSPECIFIED,
11722 REF_QUAL_NONE,
11723 tx_qual,
11724 exception_spec,
11725 return_type,
11726 trailing_requires_clause,
11727 UNKNOWN_LOCATION);
11728 declarator->std_attributes = std_attrs;
11730 fco = grokmethod (&return_type_specs,
11731 declarator,
11732 chainon (gnu_attrs, lambda_specs.attributes));
11733 if (fco != error_mark_node)
11735 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11736 DECL_ARTIFICIAL (fco) = 1;
11737 /* Give the object parameter a different name. */
11738 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11739 DECL_SET_LAMBDA_FUNCTION (fco, true);
11741 if (template_param_list)
11743 fco = finish_member_template_decl (fco);
11744 finish_template_decl (template_param_list);
11745 --parser->num_template_parameter_lists;
11747 else if (parser->fully_implicit_function_template_p)
11748 fco = finish_fully_implicit_template (parser, fco);
11750 finish_member_declaration (fco);
11752 obstack_free (&declarator_obstack, p);
11754 return (fco != error_mark_node);
11758 /* Parse the body of a lambda expression, which is simply
11760 compound-statement
11762 but which requires special handling.
11763 LAMBDA_EXPR is the current representation of the lambda expression. */
11765 static void
11766 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11768 bool nested = (current_function_decl != NULL_TREE);
11769 unsigned char local_variables_forbidden_p
11770 = parser->local_variables_forbidden_p;
11771 bool in_function_body = parser->in_function_body;
11773 /* The body of a lambda-expression is not a subexpression of the enclosing
11774 expression. */
11775 cp_evaluated ev;
11777 if (nested)
11778 push_function_context ();
11779 else
11780 /* Still increment function_depth so that we don't GC in the
11781 middle of an expression. */
11782 ++function_depth;
11784 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
11785 auto ord = make_temp_override (parser->oacc_routine, NULL);
11786 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
11787 vec<tree> omp_privatization_save;
11788 save_omp_privatization_clauses (omp_privatization_save);
11789 /* Clear this in case we're in the middle of a default argument. */
11790 parser->local_variables_forbidden_p = 0;
11791 parser->in_function_body = true;
11794 local_specialization_stack s (lss_copy);
11795 tree fco = lambda_function (lambda_expr);
11796 tree body = start_lambda_function (fco, lambda_expr);
11798 /* Originally C++11 required us to peek for 'return expr'; and
11799 process it specially here to deduce the return type. N3638
11800 removed the need for that. */
11801 cp_parser_function_body (parser, false);
11803 finish_lambda_function (body);
11806 restore_omp_privatization_clauses (omp_privatization_save);
11807 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11808 parser->in_function_body = in_function_body;
11809 if (nested)
11810 pop_function_context();
11811 else
11812 --function_depth;
11815 /* Statements [gram.stmt.stmt] */
11817 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11819 static void
11820 add_debug_begin_stmt (location_t loc)
11822 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11823 return;
11824 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11825 /* A concept is never expanded normally. */
11826 return;
11828 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11829 SET_EXPR_LOCATION (stmt, loc);
11830 add_stmt (stmt);
11833 struct cp_omp_attribute_data
11835 cp_token_cache *tokens;
11836 const c_omp_directive *dir;
11837 c_omp_directive_kind kind;
11840 /* Handle omp::directive and omp::sequence attributes in ATTRS
11841 (if any) at the start of a statement or in attribute-declaration. */
11843 static tree
11844 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
11846 if (!flag_openmp && !flag_openmp_simd)
11847 return attrs;
11849 auto_vec<cp_omp_attribute_data, 16> vec;
11850 int cnt = 0;
11851 int tokens = 0;
11852 bool bad = false;
11853 for (tree *pa = &attrs; *pa; )
11854 if (get_attribute_namespace (*pa) == omp_identifier
11855 && is_attribute_p ("directive", get_attribute_name (*pa)))
11857 cnt++;
11858 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
11860 tree d = TREE_VALUE (a);
11861 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
11862 cp_token *first = DEFPARSE_TOKENS (d)->first;
11863 cp_token *last = DEFPARSE_TOKENS (d)->last;
11864 if (parser->omp_attrs_forbidden_p)
11866 error_at (first->location,
11867 "mixing OpenMP directives with attribute and pragma "
11868 "syntax on the same statement");
11869 parser->omp_attrs_forbidden_p = false;
11870 bad = true;
11872 const char *directive[3] = {};
11873 for (int i = 0; i < 3; i++)
11875 tree id = NULL_TREE;
11876 if (first + i == last)
11877 break;
11878 if (first[i].type == CPP_NAME)
11879 id = first[i].u.value;
11880 else if (first[i].type == CPP_KEYWORD)
11881 id = ridpointers[(int) first[i].keyword];
11882 else
11883 break;
11884 directive[i] = IDENTIFIER_POINTER (id);
11886 const c_omp_directive *dir = NULL;
11887 if (directive[0])
11888 dir = c_omp_categorize_directive (directive[0], directive[1],
11889 directive[2]);
11890 if (dir == NULL)
11892 error_at (first->location,
11893 "unknown OpenMP directive name in %<omp::directive%>"
11894 " attribute argument");
11895 continue;
11897 c_omp_directive_kind kind = dir->kind;
11898 if (dir->id == PRAGMA_OMP_ORDERED)
11900 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
11901 depend clause. */
11902 if (directive[1] && strcmp (directive[1], "depend") == 0)
11903 kind = C_OMP_DIR_STANDALONE;
11904 else if (first + 2 < last
11905 && first[1].type == CPP_COMMA
11906 && first[2].type == CPP_NAME
11907 && strcmp (IDENTIFIER_POINTER (first[2].u.value),
11908 "depend") == 0)
11909 kind = C_OMP_DIR_STANDALONE;
11911 else if (dir->id == PRAGMA_OMP_ERROR)
11913 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
11914 int paren_depth = 0;
11915 for (int i = 1; first + i < last; i++)
11916 if (first[i].type == CPP_OPEN_PAREN)
11917 paren_depth++;
11918 else if (first[i].type == CPP_CLOSE_PAREN)
11919 paren_depth--;
11920 else if (paren_depth == 0
11921 && first + i + 2 < last
11922 && first[i].type == CPP_NAME
11923 && first[i + 1].type == CPP_OPEN_PAREN
11924 && first[i + 2].type == CPP_NAME
11925 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
11926 "at")
11927 && !strcmp (IDENTIFIER_POINTER (first[i
11928 + 2].u.value),
11929 "execution"))
11931 kind = C_OMP_DIR_STANDALONE;
11932 break;
11935 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
11936 vec.safe_push (v);
11937 if (flag_openmp || dir->simd)
11938 tokens += (last - first) + 1;
11940 cp_omp_attribute_data v = {};
11941 vec.safe_push (v);
11942 *pa = TREE_CHAIN (*pa);
11944 else
11945 pa = &TREE_CHAIN (*pa);
11947 if (bad)
11948 return attrs;
11950 unsigned int i;
11951 cp_omp_attribute_data *v;
11952 cp_omp_attribute_data *construct_seen = nullptr;
11953 cp_omp_attribute_data *standalone_seen = nullptr;
11954 cp_omp_attribute_data *prev_standalone_seen = nullptr;
11955 FOR_EACH_VEC_ELT (vec, i, v)
11956 if (v->tokens)
11958 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
11959 construct_seen = v;
11960 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
11961 standalone_seen = v;
11963 else
11965 if (standalone_seen && !prev_standalone_seen)
11967 prev_standalone_seen = standalone_seen;
11968 standalone_seen = nullptr;
11972 if (cnt > 1 && construct_seen)
11974 error_at (construct_seen->tokens->first->location,
11975 "OpenMP construct among %<omp::directive%> attributes"
11976 " requires all %<omp::directive%> attributes on the"
11977 " same statement to be in the same %<omp::sequence%>");
11978 return attrs;
11980 if (cnt > 1 && standalone_seen && prev_standalone_seen)
11982 error_at (standalone_seen->tokens->first->location,
11983 "multiple OpenMP standalone directives among"
11984 " %<omp::directive%> attributes must be all within the"
11985 " same %<omp::sequence%>");
11986 return attrs;
11989 if (prev_standalone_seen)
11990 standalone_seen = prev_standalone_seen;
11991 if (standalone_seen
11992 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11994 error_at (standalone_seen->tokens->first->location,
11995 "standalone OpenMP directives in %<omp::directive%> attribute"
11996 " can only appear on an empty statement");
11997 return attrs;
11999 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
12001 cp_token *token = cp_lexer_peek_token (parser->lexer);
12002 enum pragma_kind kind = cp_parser_pragma_kind (token);
12003 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
12005 error_at (token->location,
12006 "mixing OpenMP directives with attribute and pragma "
12007 "syntax on the same statement");
12008 return attrs;
12012 if (!tokens)
12013 return attrs;
12014 tokens++;
12015 cp_lexer *lexer = cp_lexer_alloc ();
12016 lexer->debugging_p = parser->lexer->debugging_p;
12017 vec_safe_reserve (lexer->buffer, tokens, true);
12018 FOR_EACH_VEC_ELT (vec, i, v)
12020 if (!v->tokens)
12021 continue;
12022 if (!flag_openmp && !v->dir->simd)
12023 continue;
12024 cp_token *first = v->tokens->first;
12025 cp_token *last = v->tokens->last;
12026 cp_token tok = {};
12027 tok.type = CPP_PRAGMA;
12028 tok.keyword = RID_MAX;
12029 tok.u.value = build_int_cst (NULL, v->dir->id);
12030 tok.location = first->location;
12031 lexer->buffer->quick_push (tok);
12032 while (++first < last)
12033 lexer->buffer->quick_push (*first);
12034 tok = {};
12035 tok.type = CPP_PRAGMA_EOL;
12036 tok.keyword = RID_MAX;
12037 tok.location = last->location;
12038 lexer->buffer->quick_push (tok);
12040 cp_token tok = {};
12041 tok.type = CPP_EOF;
12042 tok.keyword = RID_MAX;
12043 tok.location = lexer->buffer->last ().location;
12044 lexer->buffer->quick_push (tok);
12045 lexer->next = parser->lexer;
12046 lexer->next_token = lexer->buffer->address ();
12047 lexer->last_token = lexer->next_token
12048 + lexer->buffer->length ()
12049 - 1;
12050 lexer->in_omp_attribute_pragma = true;
12051 parser->lexer = lexer;
12052 /* Move the current source position to that of the first token in the
12053 new lexer. */
12054 cp_lexer_set_source_position_from_token (lexer->next_token);
12055 return attrs;
12058 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12059 (if any) at the start or after declaration-id of a declaration. */
12061 static void
12062 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12063 cp_omp_declare_simd_data *data,
12064 bool start)
12066 if (!flag_openmp && !flag_openmp_simd)
12067 return;
12069 int cnt = 0;
12070 bool bad = false;
12071 bool variant_p = false;
12072 location_t loc = UNKNOWN_LOCATION;
12073 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12074 if (get_attribute_namespace (pa) == omp_identifier
12075 && is_attribute_p ("directive", get_attribute_name (pa)))
12077 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12079 tree d = TREE_VALUE (a);
12080 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12081 cp_token *first = DEFPARSE_TOKENS (d)->first;
12082 cp_token *last = DEFPARSE_TOKENS (d)->last;
12083 const char *directive[3] = {};
12084 for (int i = 0; i < 3; i++)
12086 tree id = NULL_TREE;
12087 if (first + i == last)
12088 break;
12089 if (first[i].type == CPP_NAME)
12090 id = first[i].u.value;
12091 else if (first[i].type == CPP_KEYWORD)
12092 id = ridpointers[(int) first[i].keyword];
12093 else
12094 break;
12095 directive[i] = IDENTIFIER_POINTER (id);
12097 const c_omp_directive *dir = NULL;
12098 if (directive[0])
12099 dir = c_omp_categorize_directive (directive[0], directive[1],
12100 directive[2]);
12101 if (dir == NULL)
12102 continue;
12103 if (dir->id == PRAGMA_OMP_DECLARE
12104 && (strcmp (directive[1], "simd") == 0
12105 || strcmp (directive[1], "variant") == 0))
12107 if (cnt++ == 0)
12109 variant_p = strcmp (directive[1], "variant") == 0;
12110 loc = first->location;
12112 if (start && parser->omp_declare_simd && !bad)
12114 error_at (first->location,
12115 "mixing OpenMP directives with attribute and "
12116 "pragma syntax on the same declaration");
12117 bad = true;
12123 if (bad)
12125 for (tree *pa = pattrs; *pa; )
12126 if (get_attribute_namespace (*pa) == omp_identifier
12127 && is_attribute_p ("directive", get_attribute_name (*pa)))
12128 *pa = TREE_CHAIN (*pa);
12129 else
12130 pa = &TREE_CHAIN (*pa);
12131 return;
12133 if (cnt == 0)
12134 return;
12136 if (parser->omp_declare_simd == NULL)
12138 data->error_seen = false;
12139 data->fndecl_seen = false;
12140 data->variant_p = variant_p;
12141 data->loc = loc;
12142 data->tokens = vNULL;
12143 data->attribs[0] = NULL;
12144 data->attribs[1] = NULL;
12145 parser->omp_declare_simd = data;
12147 parser->omp_declare_simd->attribs[!start] = pattrs;
12150 /* Parse a statement.
12152 statement:
12153 labeled-statement
12154 expression-statement
12155 compound-statement
12156 selection-statement
12157 iteration-statement
12158 jump-statement
12159 declaration-statement
12160 try-block
12162 C++11:
12164 statement:
12165 labeled-statement
12166 attribute-specifier-seq (opt) expression-statement
12167 attribute-specifier-seq (opt) compound-statement
12168 attribute-specifier-seq (opt) selection-statement
12169 attribute-specifier-seq (opt) iteration-statement
12170 attribute-specifier-seq (opt) jump-statement
12171 declaration-statement
12172 attribute-specifier-seq (opt) try-block
12174 init-statement:
12175 expression-statement
12176 simple-declaration
12177 alias-declaration
12179 TM Extension:
12181 statement:
12182 atomic-statement
12184 IN_COMPOUND is true when the statement is nested inside a
12185 cp_parser_compound_statement.
12187 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12188 is a (possibly labeled) if statement which is not enclosed in braces
12189 and has an else clause. This is used to implement -Wparentheses.
12191 CHAIN is a vector of if-else-if conditions. */
12193 static void
12194 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12195 const bool in_compound, bool *if_p, vec<tree> *chain,
12196 location_t *loc_after_labels)
12198 tree statement, std_attrs = NULL_TREE;
12199 cp_token *token;
12200 location_t statement_location, attrs_loc;
12201 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12202 bool has_std_attrs;
12203 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12204 This matters for certain pragmas. */
12205 bool in_compound_for_pragma = in_compound;
12207 restart:
12208 if (if_p != NULL)
12209 *if_p = false;
12210 /* There is no statement yet. */
12211 statement = NULL_TREE;
12213 saved_token_sentinel saved_tokens (parser->lexer);
12214 token = cp_lexer_peek_token (parser->lexer);
12215 attrs_loc = token->location;
12216 if (c_dialect_objc ())
12217 /* In obj-c++, seeing '[[' might be the either the beginning of
12218 c++11 attributes, or a nested objc-message-expression. So
12219 let's parse the c++11 attributes tentatively. */
12220 cp_parser_parse_tentatively (parser);
12221 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12222 if (std_attrs)
12223 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12224 if (c_dialect_objc ())
12226 if (!cp_parser_parse_definitely (parser))
12227 std_attrs = NULL_TREE;
12229 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12231 /* Peek at the next token. */
12232 token = cp_lexer_peek_token (parser->lexer);
12233 bool omp_attrs_forbidden_p;
12234 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12236 if (std_attrs && (flag_openmp || flag_openmp_simd))
12238 bool handle_omp_attribs = false;
12239 if (token->type == CPP_KEYWORD)
12240 switch (token->keyword)
12242 case RID_IF:
12243 case RID_SWITCH:
12244 case RID_WHILE:
12245 case RID_DO:
12246 case RID_FOR:
12247 case RID_BREAK:
12248 case RID_CONTINUE:
12249 case RID_RETURN:
12250 case RID_CO_RETURN:
12251 case RID_GOTO:
12252 case RID_AT_TRY:
12253 case RID_AT_CATCH:
12254 case RID_AT_FINALLY:
12255 case RID_AT_SYNCHRONIZED:
12256 case RID_AT_THROW:
12257 case RID_TRY:
12258 case RID_TRANSACTION_ATOMIC:
12259 case RID_TRANSACTION_RELAXED:
12260 case RID_SYNCHRONIZED:
12261 case RID_ATOMIC_NOEXCEPT:
12262 case RID_ATOMIC_CANCEL:
12263 case RID_TRANSACTION_CANCEL:
12264 handle_omp_attribs = true;
12265 break;
12266 default:
12267 break;
12269 else if (token->type == CPP_SEMICOLON
12270 || token->type == CPP_OPEN_BRACE
12271 || token->type == CPP_PRAGMA)
12272 handle_omp_attribs = true;
12273 if (handle_omp_attribs)
12275 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12276 std_attrs);
12277 token = cp_lexer_peek_token (parser->lexer);
12280 parser->omp_attrs_forbidden_p = false;
12282 /* Remember the location of the first token in the statement. */
12283 cp_token *statement_token = token;
12284 statement_location = token->location;
12285 add_debug_begin_stmt (statement_location);
12286 /* If this is a keyword, then that will often determine what kind of
12287 statement we have. */
12288 if (token->type == CPP_KEYWORD)
12290 enum rid keyword = token->keyword;
12292 switch (keyword)
12294 case RID_CASE:
12295 case RID_DEFAULT:
12296 /* Looks like a labeled-statement with a case label.
12297 Parse the label, and then use tail recursion to parse
12298 the statement. */
12299 cp_parser_label_for_labeled_statement (parser, std_attrs);
12300 in_compound_for_pragma = false;
12301 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12302 goto restart;
12304 case RID_IF:
12305 case RID_SWITCH:
12306 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12307 statement = cp_parser_selection_statement (parser, if_p, chain);
12308 break;
12310 case RID_WHILE:
12311 case RID_DO:
12312 case RID_FOR:
12313 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12314 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
12315 break;
12317 case RID_BREAK:
12318 case RID_CONTINUE:
12319 case RID_RETURN:
12320 case RID_CO_RETURN:
12321 case RID_GOTO:
12322 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12323 statement = cp_parser_jump_statement (parser);
12324 break;
12326 /* Objective-C++ exception-handling constructs. */
12327 case RID_AT_TRY:
12328 case RID_AT_CATCH:
12329 case RID_AT_FINALLY:
12330 case RID_AT_SYNCHRONIZED:
12331 case RID_AT_THROW:
12332 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12333 statement = cp_parser_objc_statement (parser);
12334 break;
12336 case RID_TRY:
12337 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12338 statement = cp_parser_try_block (parser);
12339 break;
12341 case RID_NAMESPACE:
12342 /* This must be a namespace alias definition. */
12343 if (has_std_attrs)
12345 /* Attributes should be parsed as part of the
12346 declaration, so let's un-parse them. */
12347 saved_tokens.rollback();
12348 std_attrs = NULL_TREE;
12350 cp_parser_declaration_statement (parser);
12351 return;
12353 case RID_TRANSACTION_ATOMIC:
12354 case RID_TRANSACTION_RELAXED:
12355 case RID_SYNCHRONIZED:
12356 case RID_ATOMIC_NOEXCEPT:
12357 case RID_ATOMIC_CANCEL:
12358 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12359 statement = cp_parser_transaction (parser, token);
12360 break;
12361 case RID_TRANSACTION_CANCEL:
12362 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12363 statement = cp_parser_transaction_cancel (parser);
12364 break;
12366 default:
12367 /* It might be a keyword like `int' that can start a
12368 declaration-statement. */
12369 break;
12372 else if (token->type == CPP_NAME)
12374 /* If the next token is a `:', then we are looking at a
12375 labeled-statement. */
12376 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12377 if (token->type == CPP_COLON)
12379 /* Looks like a labeled-statement with an ordinary label.
12380 Parse the label, and then use tail recursion to parse
12381 the statement. */
12383 cp_parser_label_for_labeled_statement (parser, std_attrs);
12385 /* If there's no statement, it's not a labeled-statement, just
12386 a label. That's allowed in C++23, but only if we're at the
12387 end of a compound-statement. */
12388 if (in_compound
12389 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12391 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12392 if (cxx_dialect < cxx23)
12393 pedwarn (loc, OPT_Wc__23_extensions,
12394 "label at end of compound statement only available "
12395 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12396 return;
12398 in_compound_for_pragma = false;
12399 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12400 goto restart;
12403 /* Anything that starts with a `{' must be a compound-statement. */
12404 else if (token->type == CPP_OPEN_BRACE)
12406 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12407 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12409 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12410 a statement all its own. */
12411 else if (token->type == CPP_PRAGMA)
12413 do_pragma:;
12414 cp_lexer *lexer = parser->lexer;
12415 bool do_restart = false;
12416 /* Only certain OpenMP pragmas are attached to statements, and thus
12417 are considered statements themselves. All others are not. In
12418 the context of a compound, accept the pragma as a "statement" and
12419 return so that we can check for a close brace. Otherwise we
12420 require a real statement and must go back and read one. */
12421 if (in_compound_for_pragma)
12422 cp_parser_pragma (parser, pragma_compound, if_p);
12423 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12424 do_restart = true;
12425 if (parser->lexer != lexer
12426 && lexer->in_omp_attribute_pragma
12427 && (!in_omp_attribute_pragma || lexer->orphan_p))
12429 if (saved_tokens.lexer == lexer)
12431 if (saved_tokens.mode == STS_COMMIT)
12432 cp_lexer_commit_tokens (lexer);
12433 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12434 saved_tokens.lexer = parser->lexer;
12435 saved_tokens.mode = STS_DONOTHING;
12436 saved_tokens.len = parser->lexer->saved_tokens.length ();
12438 cp_lexer_destroy (lexer);
12439 lexer = parser->lexer;
12441 if (do_restart)
12442 goto restart;
12443 if (parser->lexer == lexer
12444 && lexer->in_omp_attribute_pragma
12445 && !in_omp_attribute_pragma)
12446 parser->lexer->orphan_p = true;
12447 return;
12449 else if (token->type == CPP_EOF)
12451 cp_parser_error (parser, "expected statement");
12452 return;
12455 /* Everything else must be a declaration-statement or an
12456 expression-statement. Try for the declaration-statement
12457 first, unless we are looking at a `;', in which case we know that
12458 we have an expression-statement. */
12459 if (!statement)
12461 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12463 if (has_std_attrs)
12464 /* Attributes should be parsed as part of the declaration,
12465 so let's un-parse them. */
12466 saved_tokens.rollback();
12468 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12469 cp_parser_parse_tentatively (parser);
12470 /* Try to parse the declaration-statement. */
12471 cp_parser_declaration_statement (parser);
12472 parser->omp_attrs_forbidden_p = false;
12473 /* If that worked, we're done. */
12474 if (cp_parser_parse_definitely (parser))
12475 return;
12476 /* It didn't work, restore the post-attribute position. */
12477 if (has_std_attrs)
12479 cp_lexer_set_token_position (parser->lexer, statement_token);
12480 if (flag_openmp || flag_openmp_simd)
12482 size_t i = 1;
12483 bool handle_omp_attribs = true;
12484 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12485 == RID_EXTENSION)
12486 i++;
12487 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12489 case RID_ASM:
12490 case RID_NAMESPACE:
12491 case RID_USING:
12492 case RID_LABEL:
12493 case RID_STATIC_ASSERT:
12494 /* Don't handle OpenMP attribs on keywords that
12495 always start a declaration statement but don't
12496 accept attribute before it and therefore
12497 the tentative cp_parser_declaration_statement
12498 fails to parse because of that. */
12499 handle_omp_attribs = false;
12500 break;
12501 default:
12502 break;
12505 if (handle_omp_attribs)
12507 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12508 std_attrs
12509 = cp_parser_handle_statement_omp_attributes
12510 (parser, std_attrs);
12511 parser->omp_attrs_forbidden_p = false;
12512 token = cp_lexer_peek_token (parser->lexer);
12513 if (token->type == CPP_PRAGMA)
12514 goto do_pragma;
12519 /* All preceding labels have been parsed at this point. */
12520 if (loc_after_labels != NULL)
12521 *loc_after_labels = statement_location;
12523 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12525 /* Look for an expression-statement instead. */
12526 statement = cp_parser_expression_statement (parser, in_statement_expr);
12528 /* Handle [[fallthrough]];. */
12529 if (attribute_fallthrough_p (std_attrs))
12531 /* The next token after the fallthrough attribute is ';'. */
12532 if (statement == NULL_TREE)
12534 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12535 statement = build_call_expr_internal_loc (statement_location,
12536 IFN_FALLTHROUGH,
12537 void_type_node, 0);
12538 finish_expr_stmt (statement);
12540 else
12541 warning_at (statement_location, OPT_Wattributes,
12542 "%<fallthrough%> attribute not followed by %<;%>");
12543 std_attrs = NULL_TREE;
12547 /* Set the line number for the statement. */
12548 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12549 SET_EXPR_LOCATION (statement, statement_location);
12551 /* Allow "[[fallthrough]];", but warn otherwise. */
12552 if (std_attrs != NULL_TREE)
12553 warning_at (attrs_loc,
12554 OPT_Wattributes,
12555 "attributes at the beginning of statement are ignored");
12558 /* Append ATTR to attribute list ATTRS. */
12560 tree
12561 attr_chainon (tree attrs, tree attr)
12563 if (attrs == error_mark_node)
12564 return error_mark_node;
12565 if (attr == error_mark_node)
12566 return error_mark_node;
12567 return chainon (attrs, attr);
12570 /* Parse the label for a labeled-statement, i.e.
12572 label:
12573 attribute-specifier-seq[opt] identifier :
12574 attribute-specifier-seq[opt] case constant-expression :
12575 attribute-specifier-seq[opt] default :
12577 labeled-statement:
12578 label statement
12580 GNU Extension:
12581 case constant-expression ... constant-expression : statement
12583 When a label is parsed without errors, the label is added to the
12584 parse tree by the finish_* functions, so this function doesn't
12585 have to return the label. */
12587 static void
12588 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12590 cp_token *token;
12591 tree label = NULL_TREE;
12592 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12594 /* The next token should be an identifier. */
12595 token = cp_lexer_peek_token (parser->lexer);
12596 if (token->type != CPP_NAME
12597 && token->type != CPP_KEYWORD)
12599 cp_parser_error (parser, "expected labeled-statement");
12600 return;
12603 /* Remember whether this case or a user-defined label is allowed to fall
12604 through to. */
12605 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12607 parser->colon_corrects_to_scope_p = false;
12608 switch (token->keyword)
12610 case RID_CASE:
12612 tree expr, expr_hi;
12613 cp_token *ellipsis;
12615 /* Consume the `case' token. */
12616 cp_lexer_consume_token (parser->lexer);
12617 /* Parse the constant-expression. */
12618 expr = cp_parser_constant_expression (parser);
12619 if (check_for_bare_parameter_packs (expr))
12620 expr = error_mark_node;
12622 ellipsis = cp_lexer_peek_token (parser->lexer);
12623 if (ellipsis->type == CPP_ELLIPSIS)
12625 /* Consume the `...' token. */
12626 cp_lexer_consume_token (parser->lexer);
12627 expr_hi = cp_parser_constant_expression (parser);
12628 if (check_for_bare_parameter_packs (expr_hi))
12629 expr_hi = error_mark_node;
12631 /* We don't need to emit warnings here, as the common code
12632 will do this for us. */
12634 else
12635 expr_hi = NULL_TREE;
12637 if (parser->in_switch_statement_p)
12639 tree l = finish_case_label (token->location, expr, expr_hi);
12640 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12642 label = CASE_LABEL (l);
12643 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12646 else
12647 error_at (token->location,
12648 "case label %qE not within a switch statement",
12649 expr);
12651 break;
12653 case RID_DEFAULT:
12654 /* Consume the `default' token. */
12655 cp_lexer_consume_token (parser->lexer);
12657 if (parser->in_switch_statement_p)
12659 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
12660 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12662 label = CASE_LABEL (l);
12663 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12666 else
12667 error_at (token->location, "case label not within a switch statement");
12668 break;
12670 default:
12671 /* Anything else must be an ordinary label. */
12672 label = finish_label_stmt (cp_parser_identifier (parser));
12673 if (label && TREE_CODE (label) == LABEL_DECL)
12674 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12675 break;
12678 /* Require the `:' token. */
12679 cp_parser_require (parser, CPP_COLON, RT_COLON);
12681 /* An ordinary label may optionally be followed by attributes.
12682 However, this is only permitted if the attributes are then
12683 followed by a semicolon. This is because, for backward
12684 compatibility, when parsing
12685 lab: __attribute__ ((unused)) int i;
12686 we want the attribute to attach to "i", not "lab". */
12687 if (label != NULL_TREE
12688 && cp_next_tokens_can_be_gnu_attribute_p (parser))
12690 tree attrs;
12691 cp_parser_parse_tentatively (parser);
12692 attrs = cp_parser_gnu_attributes_opt (parser);
12693 if (attrs == NULL_TREE
12694 /* And fallthrough always binds to the expression-statement. */
12695 || attribute_fallthrough_p (attrs)
12696 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12697 cp_parser_abort_tentative_parse (parser);
12698 else if (!cp_parser_parse_definitely (parser))
12700 else
12701 attributes = attr_chainon (attributes, attrs);
12704 if (attributes != NULL_TREE)
12705 cplus_decl_attributes (&label, attributes, 0);
12707 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12710 /* Parse an expression-statement.
12712 expression-statement:
12713 expression [opt] ;
12715 Returns the new EXPR_STMT -- or NULL_TREE if the expression
12716 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12717 indicates whether this expression-statement is part of an
12718 expression statement. */
12720 static tree
12721 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
12723 tree statement = NULL_TREE;
12724 cp_token *token = cp_lexer_peek_token (parser->lexer);
12725 location_t loc = token->location;
12727 /* There might be attribute fallthrough. */
12728 tree attr = cp_parser_gnu_attributes_opt (parser);
12730 /* If the next token is a ';', then there is no expression
12731 statement. */
12732 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12734 statement = cp_parser_expression (parser);
12735 if (statement == error_mark_node
12736 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12738 cp_parser_skip_to_end_of_block_or_statement (parser);
12739 return error_mark_node;
12743 /* Handle [[fallthrough]];. */
12744 if (attribute_fallthrough_p (attr))
12746 /* The next token after the fallthrough attribute is ';'. */
12747 if (statement == NULL_TREE)
12748 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12749 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
12750 void_type_node, 0);
12751 else
12752 warning_at (loc, OPT_Wattributes,
12753 "%<fallthrough%> attribute not followed by %<;%>");
12754 attr = NULL_TREE;
12757 /* Allow "[[fallthrough]];", but warn otherwise. */
12758 if (attr != NULL_TREE)
12759 warning_at (loc, OPT_Wattributes,
12760 "attributes at the beginning of statement are ignored");
12762 /* Give a helpful message for "A<T>::type t;" and the like. */
12763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
12764 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12766 if (TREE_CODE (statement) == SCOPE_REF)
12767 error_at (token->location, "need %<typename%> before %qE because "
12768 "%qT is a dependent scope",
12769 statement, TREE_OPERAND (statement, 0));
12770 else if (is_overloaded_fn (statement)
12771 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
12773 /* A::A a; */
12774 tree fn = get_first_fn (statement);
12775 error_at (token->location,
12776 "%<%T::%D%> names the constructor, not the type",
12777 DECL_CONTEXT (fn), DECL_NAME (fn));
12781 /* Consume the final `;'. */
12782 cp_parser_consume_semicolon_at_end_of_statement (parser);
12784 if (in_statement_expr
12785 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12786 /* This is the final expression statement of a statement
12787 expression. */
12788 statement = finish_stmt_expr_expr (statement, in_statement_expr);
12789 else if (statement)
12790 statement = finish_expr_stmt (statement);
12792 return statement;
12795 /* Parse a compound-statement.
12797 compound-statement:
12798 { statement-seq [opt] label-seq [opt] }
12800 label-seq:
12801 label
12802 label-seq label
12804 GNU extension:
12806 compound-statement:
12807 { label-declaration-seq [opt] statement-seq [opt] }
12809 label-declaration-seq:
12810 label-declaration
12811 label-declaration-seq label-declaration
12813 Returns a tree representing the statement. */
12815 static tree
12816 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
12817 int bcs_flags, bool function_body)
12819 tree compound_stmt;
12820 matching_braces braces;
12822 /* Consume the `{'. */
12823 if (!braces.require_open (parser))
12824 return error_mark_node;
12825 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
12826 && !function_body && cxx_dialect < cxx14)
12827 pedwarn (input_location, OPT_Wpedantic,
12828 "compound-statement in %<constexpr%> function");
12829 /* Begin the compound-statement. */
12830 compound_stmt = begin_compound_stmt (bcs_flags);
12831 /* If the next keyword is `__label__' we have a label declaration. */
12832 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12833 cp_parser_label_declaration (parser);
12834 /* Parse an (optional) statement-seq. */
12835 cp_parser_statement_seq_opt (parser, in_statement_expr);
12837 /* Consume the `}'. */
12838 braces.require_close (parser);
12840 /* Finish the compound-statement. */
12841 finish_compound_stmt (compound_stmt);
12843 return compound_stmt;
12846 /* Parse an (optional) statement-seq.
12848 statement-seq:
12849 statement
12850 statement-seq [opt] statement */
12852 static void
12853 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
12855 /* Scan statements until there aren't any more. */
12856 while (true)
12858 cp_token *token = cp_lexer_peek_token (parser->lexer);
12860 /* If we are looking at a `}', then we have run out of
12861 statements; the same is true if we have reached the end
12862 of file, or have stumbled upon a stray '@end'. */
12863 if (token->type == CPP_CLOSE_BRACE
12864 || token->type == CPP_EOF
12865 || token->type == CPP_PRAGMA_EOL
12866 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
12867 break;
12869 /* If we are in a compound statement and find 'else' then
12870 something went wrong. */
12871 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
12873 if (parser->in_statement & IN_IF_STMT)
12874 break;
12875 else
12877 token = cp_lexer_consume_token (parser->lexer);
12878 error_at (token->location, "%<else%> without a previous %<if%>");
12882 /* Parse the statement. */
12883 cp_parser_statement (parser, in_statement_expr, true, NULL);
12887 /* Return true if this is the C++20 version of range-based-for with
12888 init-statement. */
12890 static bool
12891 cp_parser_range_based_for_with_init_p (cp_parser *parser)
12893 bool r = false;
12895 /* Save tokens so that we can put them back. */
12896 cp_lexer_save_tokens (parser->lexer);
12898 /* There has to be an unnested ; followed by an unnested :. */
12899 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
12900 /*recovering=*/false,
12901 CPP_SEMICOLON,
12902 /*consume_paren=*/false) != -1)
12903 goto out;
12905 /* We found the semicolon, eat it now. */
12906 cp_lexer_consume_token (parser->lexer);
12908 /* Now look for ':' that is not nested in () or {}. */
12909 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
12910 /*recovering=*/false,
12911 CPP_COLON,
12912 /*consume_paren=*/false) == -1);
12914 out:
12915 /* Roll back the tokens we skipped. */
12916 cp_lexer_rollback_tokens (parser->lexer);
12918 return r;
12921 /* Return true if we're looking at (init; cond), false otherwise. */
12923 static bool
12924 cp_parser_init_statement_p (cp_parser *parser)
12926 /* Save tokens so that we can put them back. */
12927 cp_lexer_save_tokens (parser->lexer);
12929 /* Look for ';' that is not nested in () or {}. */
12930 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
12931 /*recovering=*/false,
12932 CPP_SEMICOLON,
12933 /*consume_paren=*/false);
12935 /* Roll back the tokens we skipped. */
12936 cp_lexer_rollback_tokens (parser->lexer);
12938 return ret == -1;
12941 /* Parse a selection-statement.
12943 selection-statement:
12944 if ( init-statement [opt] condition ) statement
12945 if ( init-statement [opt] condition ) statement else statement
12946 switch ( init-statement [opt] condition ) statement
12948 Returns the new IF_STMT or SWITCH_STMT.
12950 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12951 is a (possibly labeled) if statement which is not enclosed in
12952 braces and has an else clause. This is used to implement
12953 -Wparentheses.
12955 CHAIN is a vector of if-else-if conditions. This is used to implement
12956 -Wduplicated-cond. */
12958 static tree
12959 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
12960 vec<tree> *chain)
12962 cp_token *token;
12963 enum rid keyword;
12964 token_indent_info guard_tinfo;
12966 if (if_p != NULL)
12967 *if_p = false;
12969 /* Peek at the next token. */
12970 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
12971 guard_tinfo = get_token_indent_info (token);
12973 /* See what kind of keyword it is. */
12974 keyword = token->keyword;
12975 switch (keyword)
12977 case RID_IF:
12978 case RID_SWITCH:
12980 tree statement;
12981 tree condition;
12983 bool cx = false;
12984 if (keyword == RID_IF
12985 && cp_lexer_next_token_is_keyword (parser->lexer,
12986 RID_CONSTEXPR))
12988 cx = true;
12989 cp_token *tok = cp_lexer_consume_token (parser->lexer);
12990 if (cxx_dialect < cxx17)
12991 pedwarn (tok->location, OPT_Wc__17_extensions,
12992 "%<if constexpr%> only available with "
12993 "%<-std=c++17%> or %<-std=gnu++17%>");
12995 int ce = 0;
12996 if (keyword == RID_IF && !cx)
12998 if (cp_lexer_next_token_is_keyword (parser->lexer,
12999 RID_CONSTEVAL))
13000 ce = 1;
13001 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
13002 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
13003 RID_CONSTEVAL))
13005 ce = -1;
13006 cp_lexer_consume_token (parser->lexer);
13009 if (ce)
13011 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13012 if (cxx_dialect < cxx23)
13013 pedwarn (tok->location, OPT_Wc__23_extensions,
13014 "%<if consteval%> only available with "
13015 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13017 bool save_in_consteval_if_p = in_consteval_if_p;
13018 statement = begin_if_stmt ();
13019 IF_STMT_CONSTEVAL_P (statement) = true;
13020 condition = finish_if_stmt_cond (boolean_false_node, statement);
13022 gcc_rich_location richloc (tok->location);
13023 bool non_compound_stmt_p = false;
13024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13026 non_compound_stmt_p = true;
13027 richloc.add_fixit_insert_after (tok->location, "{");
13030 in_consteval_if_p |= ce > 0;
13031 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13033 if (non_compound_stmt_p)
13035 location_t before_loc
13036 = cp_lexer_peek_token (parser->lexer)->location;
13037 richloc.add_fixit_insert_before (before_loc, "}");
13038 error_at (&richloc,
13039 "%<if consteval%> requires compound statement");
13040 non_compound_stmt_p = false;
13043 finish_then_clause (statement);
13045 /* If the next token is `else', parse the else-clause. */
13046 if (cp_lexer_next_token_is_keyword (parser->lexer,
13047 RID_ELSE))
13049 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13050 gcc_rich_location else_richloc (else_tok->location);
13051 guard_tinfo = get_token_indent_info (else_tok);
13052 /* Consume the `else' keyword. */
13053 cp_lexer_consume_token (parser->lexer);
13055 begin_else_clause (statement);
13057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13059 non_compound_stmt_p = true;
13060 else_richloc.add_fixit_insert_after (else_tok->location,
13061 "{");
13064 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13065 cp_parser_implicitly_scoped_statement (parser, NULL,
13066 guard_tinfo);
13068 if (non_compound_stmt_p)
13070 location_t before_loc
13071 = cp_lexer_peek_token (parser->lexer)->location;
13072 else_richloc.add_fixit_insert_before (before_loc, "}");
13073 error_at (&else_richloc,
13074 "%<if consteval%> requires compound statement");
13077 finish_else_clause (statement);
13080 in_consteval_if_p = save_in_consteval_if_p;
13081 if (ce < 0)
13083 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13084 if (THEN_CLAUSE (statement) == NULL_TREE)
13085 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13088 finish_if_stmt (statement);
13089 return statement;
13092 /* Look for the `('. */
13093 matching_parens parens;
13094 if (!parens.require_open (parser))
13096 cp_parser_skip_to_end_of_statement (parser);
13097 return error_mark_node;
13100 /* Begin the selection-statement. */
13101 if (keyword == RID_IF)
13103 statement = begin_if_stmt ();
13104 IF_STMT_CONSTEXPR_P (statement) = cx;
13106 else
13107 statement = begin_switch_stmt ();
13109 /* Parse the optional init-statement. */
13110 if (cp_parser_init_statement_p (parser))
13112 tree decl;
13113 if (cxx_dialect < cxx17)
13114 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13115 OPT_Wc__17_extensions,
13116 "init-statement in selection statements only available "
13117 "with %<-std=c++17%> or %<-std=gnu++17%>");
13118 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13119 /* A non-empty init-statement can have arbitrary side
13120 effects. */
13121 vec_free (chain);
13122 cp_parser_init_statement (parser, &decl);
13125 /* Parse the condition. */
13126 condition = cp_parser_condition (parser);
13127 /* Look for the `)'. */
13128 if (!parens.require_close (parser))
13129 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13130 /*consume_paren=*/true);
13132 if (keyword == RID_IF)
13134 bool nested_if;
13135 unsigned char in_statement;
13137 /* Add the condition. */
13138 condition = finish_if_stmt_cond (condition, statement);
13140 if (warn_duplicated_cond)
13141 warn_duplicated_cond_add_or_warn (token->location, condition,
13142 &chain);
13144 /* Parse the then-clause. */
13145 in_statement = parser->in_statement;
13146 parser->in_statement |= IN_IF_STMT;
13148 /* Outside a template, the non-selected branch of a constexpr
13149 if is a 'discarded statement', i.e. unevaluated. */
13150 bool was_discarded = in_discarded_stmt;
13151 bool discard_then = (cx && !processing_template_decl
13152 && integer_zerop (condition));
13153 if (discard_then)
13155 in_discarded_stmt = true;
13156 ++c_inhibit_evaluation_warnings;
13159 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13160 guard_tinfo);
13162 parser->in_statement = in_statement;
13164 finish_then_clause (statement);
13166 if (discard_then)
13168 THEN_CLAUSE (statement) = NULL_TREE;
13169 in_discarded_stmt = was_discarded;
13170 --c_inhibit_evaluation_warnings;
13173 /* If the next token is `else', parse the else-clause. */
13174 if (cp_lexer_next_token_is_keyword (parser->lexer,
13175 RID_ELSE))
13177 bool discard_else = (cx && !processing_template_decl
13178 && integer_nonzerop (condition));
13179 if (discard_else)
13181 in_discarded_stmt = true;
13182 ++c_inhibit_evaluation_warnings;
13185 guard_tinfo
13186 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13187 /* Consume the `else' keyword. */
13188 cp_lexer_consume_token (parser->lexer);
13189 if (warn_duplicated_cond)
13191 if (cp_lexer_next_token_is_keyword (parser->lexer,
13192 RID_IF)
13193 && chain == NULL)
13195 /* We've got "if (COND) else if (COND2)". Start
13196 the condition chain and add COND as the first
13197 element. */
13198 chain = new vec<tree> ();
13199 if (!CONSTANT_CLASS_P (condition)
13200 && !TREE_SIDE_EFFECTS (condition))
13202 /* Wrap it in a NOP_EXPR so that we can set the
13203 location of the condition. */
13204 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13205 condition);
13206 SET_EXPR_LOCATION (e, token->location);
13207 chain->safe_push (e);
13210 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13211 RID_IF))
13212 /* This is if-else without subsequent if. Zap the
13213 condition chain; we would have already warned at
13214 this point. */
13215 vec_free (chain);
13217 begin_else_clause (statement);
13218 /* Parse the else-clause. */
13219 cp_parser_implicitly_scoped_statement (parser, NULL,
13220 guard_tinfo, chain);
13222 finish_else_clause (statement);
13224 /* If we are currently parsing a then-clause, then
13225 IF_P will not be NULL. We set it to true to
13226 indicate that this if statement has an else clause.
13227 This may trigger the Wparentheses warning below
13228 when we get back up to the parent if statement. */
13229 if (if_p != NULL)
13230 *if_p = true;
13232 if (discard_else)
13234 ELSE_CLAUSE (statement) = NULL_TREE;
13235 in_discarded_stmt = was_discarded;
13236 --c_inhibit_evaluation_warnings;
13239 else
13241 /* This if statement does not have an else clause. If
13242 NESTED_IF is true, then the then-clause has an if
13243 statement which does have an else clause. We warn
13244 about the potential ambiguity. */
13245 if (nested_if)
13246 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13247 "suggest explicit braces to avoid ambiguous"
13248 " %<else%>");
13249 if (warn_duplicated_cond)
13250 /* We don't need the condition chain anymore. */
13251 vec_free (chain);
13254 /* Now we're all done with the if-statement. */
13255 finish_if_stmt (statement);
13257 else
13259 bool in_switch_statement_p;
13260 unsigned char in_statement;
13262 /* Add the condition. */
13263 finish_switch_cond (condition, statement);
13265 /* Parse the body of the switch-statement. */
13266 in_switch_statement_p = parser->in_switch_statement_p;
13267 in_statement = parser->in_statement;
13268 parser->in_switch_statement_p = true;
13269 parser->in_statement |= IN_SWITCH_STMT;
13270 cp_parser_implicitly_scoped_statement (parser, if_p,
13271 guard_tinfo);
13272 parser->in_switch_statement_p = in_switch_statement_p;
13273 parser->in_statement = in_statement;
13275 /* Now we're all done with the switch-statement. */
13276 finish_switch_stmt (statement);
13279 return statement;
13281 break;
13283 default:
13284 cp_parser_error (parser, "expected selection-statement");
13285 return error_mark_node;
13289 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13290 If we have seen at least one decl-specifier, and the next token is not
13291 a parenthesis (after "int (" we might be looking at a functional cast)
13292 neither we are dealing with a concept-check expression then we must be
13293 looking at a declaration. */
13295 static void
13296 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13297 cp_decl_specifier_seq *decl_specs)
13299 if (decl_specs->any_specifiers_p
13300 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13301 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13302 && !cp_parser_error_occurred (parser)
13303 && !(decl_specs->type
13304 && TREE_CODE (decl_specs->type) == TYPE_DECL
13305 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13306 cp_parser_commit_to_tentative_parse (parser);
13309 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13310 The declarator shall not specify a function or an array. Returns
13311 TRUE if the declarator is valid, FALSE otherwise. */
13313 static bool
13314 cp_parser_check_condition_declarator (cp_parser* parser,
13315 cp_declarator *declarator,
13316 location_t loc)
13318 if (declarator == cp_error_declarator
13319 || function_declarator_p (declarator)
13320 || declarator->kind == cdk_array)
13322 if (declarator == cp_error_declarator)
13323 /* Already complained. */;
13324 else if (declarator->kind == cdk_array)
13325 error_at (loc, "condition declares an array");
13326 else
13327 error_at (loc, "condition declares a function");
13328 if (parser->fully_implicit_function_template_p)
13329 abort_fully_implicit_template (parser);
13330 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13331 /*or_comma=*/false,
13332 /*consume_paren=*/false);
13333 return false;
13335 else
13336 return true;
13339 /* Parse a condition.
13341 condition:
13342 expression
13343 type-specifier-seq declarator = initializer-clause
13344 type-specifier-seq declarator braced-init-list
13346 GNU Extension:
13348 condition:
13349 type-specifier-seq declarator asm-specification [opt]
13350 attributes [opt] = assignment-expression
13352 Returns the expression that should be tested. */
13354 static tree
13355 cp_parser_condition (cp_parser* parser)
13357 cp_decl_specifier_seq type_specifiers;
13358 const char *saved_message;
13359 int declares_class_or_enum;
13361 /* Try the declaration first. */
13362 cp_parser_parse_tentatively (parser);
13363 /* New types are not allowed in the type-specifier-seq for a
13364 condition. */
13365 saved_message = parser->type_definition_forbidden_message;
13366 parser->type_definition_forbidden_message
13367 = G_("types may not be defined in conditions");
13368 /* Parse the type-specifier-seq. */
13369 cp_parser_decl_specifier_seq (parser,
13370 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13371 &type_specifiers,
13372 &declares_class_or_enum);
13373 /* Restore the saved message. */
13374 parser->type_definition_forbidden_message = saved_message;
13376 /* Gather the attributes that were provided with the
13377 decl-specifiers. */
13378 tree prefix_attributes = type_specifiers.attributes;
13380 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13382 /* If all is well, we might be looking at a declaration. */
13383 if (!cp_parser_error_occurred (parser))
13385 tree decl;
13386 tree asm_specification;
13387 tree attributes;
13388 cp_declarator *declarator;
13389 tree initializer = NULL_TREE;
13390 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13392 /* Parse the declarator. */
13393 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13394 CP_PARSER_FLAGS_NONE,
13395 /*ctor_dtor_or_conv_p=*/NULL,
13396 /*parenthesized_p=*/NULL,
13397 /*member_p=*/false,
13398 /*friend_p=*/false,
13399 /*static_p=*/false);
13400 /* Parse the attributes. */
13401 attributes = cp_parser_attributes_opt (parser);
13402 /* Parse the asm-specification. */
13403 asm_specification = cp_parser_asm_specification_opt (parser);
13404 /* If the next token is not an `=' or '{', then we might still be
13405 looking at an expression. For example:
13407 if (A(a).x)
13409 looks like a decl-specifier-seq and a declarator -- but then
13410 there is no `=', so this is an expression. */
13411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13412 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13413 cp_parser_simulate_error (parser);
13415 /* If we did see an `=' or '{', then we are looking at a declaration
13416 for sure. */
13417 if (cp_parser_parse_definitely (parser))
13419 tree pushed_scope;
13420 bool non_constant_p = false;
13421 int flags = LOOKUP_ONLYCONVERTING;
13423 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13424 return error_mark_node;
13426 /* Create the declaration. */
13427 decl = start_decl (declarator, &type_specifiers,
13428 /*initialized_p=*/true,
13429 attributes, prefix_attributes,
13430 &pushed_scope);
13432 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13433 /* Parse the initializer. */
13434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13436 initializer = cp_parser_braced_list (parser, &non_constant_p);
13437 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13438 flags = 0;
13440 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13442 /* Consume the `='. */
13443 cp_lexer_consume_token (parser->lexer);
13444 initializer = cp_parser_initializer_clause (parser,
13445 &non_constant_p);
13447 else
13449 cp_parser_error (parser, "expected initializer");
13450 initializer = error_mark_node;
13452 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13453 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13455 /* Process the initializer. */
13456 cp_finish_decl (decl,
13457 initializer, !non_constant_p,
13458 asm_specification,
13459 flags);
13461 if (pushed_scope)
13462 pop_scope (pushed_scope);
13464 return convert_from_reference (decl);
13467 /* If we didn't even get past the declarator successfully, we are
13468 definitely not looking at a declaration. */
13469 else
13470 cp_parser_abort_tentative_parse (parser);
13472 /* Otherwise, we are looking at an expression. */
13473 return cp_parser_expression (parser);
13476 /* Parses a for-statement or range-for-statement until the closing ')',
13477 not included. */
13479 static tree
13480 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
13482 tree init, scope, decl;
13483 bool is_range_for;
13485 /* Begin the for-statement. */
13486 scope = begin_for_scope (&init);
13488 /* Maybe parse the optional init-statement in a range-based for loop. */
13489 if (cp_parser_range_based_for_with_init_p (parser)
13490 /* Checked for diagnostic purposes only. */
13491 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13493 tree dummy;
13494 cp_parser_init_statement (parser, &dummy);
13495 if (cxx_dialect < cxx20)
13497 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13498 OPT_Wc__20_extensions,
13499 "range-based %<for%> loops with initializer only "
13500 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13501 decl = error_mark_node;
13505 /* Parse the initialization. */
13506 is_range_for = cp_parser_init_statement (parser, &decl);
13508 if (is_range_for)
13509 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13510 false);
13511 else
13512 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
13515 static tree
13516 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13517 unsigned short unroll)
13519 /* Normal for loop */
13520 tree condition = NULL_TREE;
13521 tree expression = NULL_TREE;
13522 tree stmt;
13524 stmt = begin_for_stmt (scope, init);
13525 /* The init-statement has already been parsed in
13526 cp_parser_init_statement, so no work is needed here. */
13527 finish_init_stmt (stmt);
13529 /* If there's a condition, process it. */
13530 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13531 condition = cp_parser_condition (parser);
13532 else if (ivdep)
13534 cp_parser_error (parser, "missing loop condition in loop with "
13535 "%<GCC ivdep%> pragma");
13536 condition = error_mark_node;
13538 else if (unroll)
13540 cp_parser_error (parser, "missing loop condition in loop with "
13541 "%<GCC unroll%> pragma");
13542 condition = error_mark_node;
13544 finish_for_cond (condition, stmt, ivdep, unroll);
13545 /* Look for the `;'. */
13546 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13548 /* If there's an expression, process it. */
13549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13550 expression = cp_parser_expression (parser);
13551 finish_for_expr (expression, stmt);
13553 return stmt;
13556 /* Tries to parse a range-based for-statement:
13558 range-based-for:
13559 decl-specifier-seq declarator : expression
13561 The decl-specifier-seq declarator and the `:' are already parsed by
13562 cp_parser_init_statement. If processing_template_decl it returns a
13563 newly created RANGE_FOR_STMT; if not, it is converted to a
13564 regular FOR_STMT. */
13566 static tree
13567 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
13568 bool ivdep, unsigned short unroll, bool is_omp)
13570 tree stmt, range_expr;
13571 auto_vec <cxx_binding *, 16> bindings;
13572 auto_vec <tree, 16> names;
13573 tree decomp_first_name = NULL_TREE;
13574 unsigned int decomp_cnt = 0;
13576 /* Get the range declaration momentarily out of the way so that
13577 the range expression doesn't clash with it. */
13578 if (range_decl != error_mark_node)
13580 if (DECL_HAS_VALUE_EXPR_P (range_decl))
13582 tree v = DECL_VALUE_EXPR (range_decl);
13583 /* For decomposition declaration get all of the corresponding
13584 declarations out of the way. */
13585 if (TREE_CODE (v) == ARRAY_REF
13586 && VAR_P (TREE_OPERAND (v, 0))
13587 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
13589 tree d = range_decl;
13590 range_decl = TREE_OPERAND (v, 0);
13591 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
13592 decomp_first_name = d;
13593 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
13595 tree name = DECL_NAME (d);
13596 names.safe_push (name);
13597 bindings.safe_push (IDENTIFIER_BINDING (name));
13598 IDENTIFIER_BINDING (name)
13599 = IDENTIFIER_BINDING (name)->previous;
13603 if (names.is_empty ())
13605 tree name = DECL_NAME (range_decl);
13606 names.safe_push (name);
13607 bindings.safe_push (IDENTIFIER_BINDING (name));
13608 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
13612 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13614 bool expr_non_constant_p;
13615 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13617 else
13618 range_expr = cp_parser_expression (parser);
13620 /* Put the range declaration(s) back into scope. */
13621 for (unsigned int i = 0; i < names.length (); i++)
13623 cxx_binding *binding = bindings[i];
13624 binding->previous = IDENTIFIER_BINDING (names[i]);
13625 IDENTIFIER_BINDING (names[i]) = binding;
13628 /* finish_omp_for has its own code for the following, so just
13629 return the range_expr instead. */
13630 if (is_omp)
13631 return range_expr;
13633 /* If in template, STMT is converted to a normal for-statement
13634 at instantiation. If not, it is done just ahead. */
13635 if (processing_template_decl)
13637 if (check_for_bare_parameter_packs (range_expr))
13638 range_expr = error_mark_node;
13639 stmt = begin_range_for_stmt (scope, init);
13640 if (ivdep)
13641 RANGE_FOR_IVDEP (stmt) = 1;
13642 if (unroll)
13643 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
13644 finish_range_for_decl (stmt, range_decl, range_expr);
13645 if (!type_dependent_expression_p (range_expr)
13646 /* do_auto_deduction doesn't mess with template init-lists. */
13647 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
13648 do_range_for_auto_deduction (range_decl, range_expr);
13650 else
13652 stmt = begin_for_stmt (scope, init);
13653 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
13654 decomp_first_name, decomp_cnt, ivdep,
13655 unroll);
13657 return stmt;
13660 /* Subroutine of cp_convert_range_for: given the initializer expression,
13661 builds up the range temporary. */
13663 static tree
13664 build_range_temp (tree range_expr)
13666 /* Find out the type deduced by the declaration
13667 `auto &&__range = range_expr'. */
13668 tree auto_node = make_auto ();
13669 tree range_type = cp_build_reference_type (auto_node, true);
13670 range_type = do_auto_deduction (range_type, range_expr, auto_node);
13672 /* Create the __range variable. */
13673 tree range_temp = build_decl (input_location, VAR_DECL,
13674 for_range__identifier, range_type);
13675 TREE_USED (range_temp) = 1;
13676 DECL_ARTIFICIAL (range_temp) = 1;
13678 return range_temp;
13681 /* Used by cp_parser_range_for in template context: we aren't going to
13682 do a full conversion yet, but we still need to resolve auto in the
13683 type of the for-range-declaration if present. This is basically
13684 a shortcut version of cp_convert_range_for. */
13686 static void
13687 do_range_for_auto_deduction (tree decl, tree range_expr)
13689 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13690 if (auto_node)
13692 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
13693 range_temp = convert_from_reference (build_range_temp (range_expr));
13694 iter_type = (cp_parser_perform_range_for_lookup
13695 (range_temp, &begin_dummy, &end_dummy));
13696 if (iter_type)
13698 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
13699 iter_type);
13700 iter_decl = build_x_indirect_ref (input_location, iter_decl,
13701 RO_UNARY_STAR, NULL_TREE,
13702 tf_warning_or_error);
13703 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
13704 iter_decl, auto_node,
13705 tf_warning_or_error,
13706 adc_variable_type);
13711 /* Warns when the loop variable should be changed to a reference type to
13712 avoid unnecessary copying. I.e., from
13714 for (const auto x : range)
13716 where range returns a reference, to
13718 for (const auto &x : range)
13720 if this version doesn't make a copy.
13722 This function also warns when the loop variable is initialized with
13723 a value of a different type resulting in a copy:
13725 int arr[10];
13726 for (const double &x : arr)
13728 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
13729 This function is never called when processing_template_decl is on. */
13731 static void
13732 warn_for_range_copy (tree decl, tree expr)
13734 if (!warn_range_loop_construct
13735 || decl == error_mark_node)
13736 return;
13738 location_t loc = DECL_SOURCE_LOCATION (decl);
13739 tree type = TREE_TYPE (decl);
13741 if (from_macro_expansion_at (loc))
13742 return;
13744 if (TYPE_REF_P (type))
13746 if (glvalue_p (expr) && !ref_conv_binds_directly_p (type, expr))
13748 auto_diagnostic_group d;
13749 if (warning_at (loc, OPT_Wrange_loop_construct,
13750 "loop variable %qD of type %qT binds to a temporary "
13751 "constructed from type %qT", decl, type,
13752 TREE_TYPE (expr)))
13754 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
13755 TYPE_QUAL_CONST);
13756 ref = cp_build_reference_type (ref, /*rval*/false);
13757 inform (loc, "use non-reference type %qT to make the copy "
13758 "explicit or %qT to prevent copying",
13759 non_reference (type), ref);
13762 return;
13764 else if (!CP_TYPE_CONST_P (type))
13765 return;
13767 /* Since small trivially copyable types are cheap to copy, we suppress the
13768 warning for them. 64B is a common size of a cache line. */
13769 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
13770 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
13771 && trivially_copyable_p (type)))
13772 return;
13774 tree rtype = cp_build_reference_type (type, /*rval*/false);
13775 /* If we could initialize the reference directly, it wouldn't involve any
13776 copies. */
13777 if (!ref_conv_binds_directly_p (rtype, expr))
13778 return;
13780 auto_diagnostic_group d;
13781 if (warning_at (loc, OPT_Wrange_loop_construct,
13782 "loop variable %qD creates a copy from type %qT",
13783 decl, type))
13785 gcc_rich_location richloc (loc);
13786 richloc.add_fixit_insert_before ("&");
13787 inform (&richloc, "use reference type to prevent copying");
13791 /* Converts a range-based for-statement into a normal
13792 for-statement, as per the definition.
13794 for (RANGE_DECL : RANGE_EXPR)
13795 BLOCK
13797 should be equivalent to:
13800 auto &&__range = RANGE_EXPR;
13801 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
13802 __begin != __end;
13803 ++__begin)
13805 RANGE_DECL = *__begin;
13806 BLOCK
13810 If RANGE_EXPR is an array:
13811 BEGIN_EXPR = __range
13812 END_EXPR = __range + ARRAY_SIZE(__range)
13813 Else if RANGE_EXPR has a member 'begin' or 'end':
13814 BEGIN_EXPR = __range.begin()
13815 END_EXPR = __range.end()
13816 Else:
13817 BEGIN_EXPR = begin(__range)
13818 END_EXPR = end(__range);
13820 If __range has a member 'begin' but not 'end', or vice versa, we must
13821 still use the second alternative (it will surely fail, however).
13822 When calling begin()/end() in the third alternative we must use
13823 argument dependent lookup, but always considering 'std' as an associated
13824 namespace. */
13826 tree
13827 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
13828 tree decomp_first_name, unsigned int decomp_cnt,
13829 bool ivdep, unsigned short unroll)
13831 tree begin, end;
13832 tree iter_type, begin_expr, end_expr;
13833 tree condition, expression;
13835 range_expr = mark_lvalue_use (range_expr);
13837 if (range_decl == error_mark_node || range_expr == error_mark_node)
13838 /* If an error happened previously do nothing or else a lot of
13839 unhelpful errors would be issued. */
13840 begin_expr = end_expr = iter_type = error_mark_node;
13841 else
13843 tree range_temp;
13845 if (VAR_P (range_expr)
13846 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
13847 /* Can't bind a reference to an array of runtime bound. */
13848 range_temp = range_expr;
13849 else
13851 range_temp = build_range_temp (range_expr);
13852 pushdecl (range_temp);
13853 cp_finish_decl (range_temp, range_expr,
13854 /*is_constant_init*/false, NULL_TREE,
13855 LOOKUP_ONLYCONVERTING);
13856 range_temp = convert_from_reference (range_temp);
13858 iter_type = cp_parser_perform_range_for_lookup (range_temp,
13859 &begin_expr, &end_expr);
13862 /* The new for initialization statement. */
13863 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
13864 iter_type);
13865 TREE_USED (begin) = 1;
13866 DECL_ARTIFICIAL (begin) = 1;
13867 pushdecl (begin);
13868 cp_finish_decl (begin, begin_expr,
13869 /*is_constant_init*/false, NULL_TREE,
13870 LOOKUP_ONLYCONVERTING);
13872 if (cxx_dialect >= cxx17)
13873 iter_type = cv_unqualified (TREE_TYPE (end_expr));
13874 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
13875 TREE_USED (end) = 1;
13876 DECL_ARTIFICIAL (end) = 1;
13877 pushdecl (end);
13878 cp_finish_decl (end, end_expr,
13879 /*is_constant_init*/false, NULL_TREE,
13880 LOOKUP_ONLYCONVERTING);
13882 finish_init_stmt (statement);
13884 /* The new for condition. */
13885 condition = build_x_binary_op (input_location, NE_EXPR,
13886 begin, ERROR_MARK,
13887 end, ERROR_MARK,
13888 NULL_TREE, NULL, tf_warning_or_error);
13889 finish_for_cond (condition, statement, ivdep, unroll);
13891 /* The new increment expression. */
13892 expression = finish_unary_op_expr (input_location,
13893 PREINCREMENT_EXPR, begin,
13894 tf_warning_or_error);
13895 finish_for_expr (expression, statement);
13897 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13898 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
13900 /* The declaration is initialized with *__begin inside the loop body. */
13901 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
13902 NULL_TREE, tf_warning_or_error);
13903 cp_finish_decl (range_decl, deref_begin,
13904 /*is_constant_init*/false, NULL_TREE,
13905 LOOKUP_ONLYCONVERTING);
13906 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13907 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
13909 warn_for_range_copy (range_decl, deref_begin);
13911 return statement;
13914 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
13915 We need to solve both at the same time because the method used
13916 depends on the existence of members begin or end.
13917 Returns the type deduced for the iterator expression. */
13919 static tree
13920 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
13922 if (error_operand_p (range))
13924 *begin = *end = error_mark_node;
13925 return error_mark_node;
13928 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
13930 error ("range-based %<for%> expression of type %qT "
13931 "has incomplete type", TREE_TYPE (range));
13932 *begin = *end = error_mark_node;
13933 return error_mark_node;
13935 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
13937 /* If RANGE is an array, we will use pointer arithmetic. */
13938 *begin = decay_conversion (range, tf_warning_or_error);
13939 *end = build_binary_op (input_location, PLUS_EXPR,
13940 range,
13941 array_type_nelts_top (TREE_TYPE (range)),
13942 false);
13943 return TREE_TYPE (*begin);
13945 else
13947 /* If it is not an array, we must do a bit of magic. */
13948 tree id_begin, id_end;
13949 tree member_begin, member_end;
13951 *begin = *end = error_mark_node;
13953 id_begin = get_identifier ("begin");
13954 id_end = get_identifier ("end");
13955 member_begin = lookup_member (TREE_TYPE (range), id_begin,
13956 /*protect=*/2, /*want_type=*/false,
13957 tf_warning_or_error);
13958 member_end = lookup_member (TREE_TYPE (range), id_end,
13959 /*protect=*/2, /*want_type=*/false,
13960 tf_warning_or_error);
13962 if (member_begin != NULL_TREE && member_end != NULL_TREE)
13964 /* Use the member functions. */
13965 *begin = cp_parser_range_for_member_function (range, id_begin);
13966 *end = cp_parser_range_for_member_function (range, id_end);
13968 else
13970 /* Use global functions with ADL. */
13971 releasing_vec vec;
13973 vec_safe_push (vec, range);
13975 member_begin = perform_koenig_lookup (id_begin, vec,
13976 tf_warning_or_error);
13977 *begin = finish_call_expr (member_begin, &vec, false, true,
13978 tf_warning_or_error);
13979 member_end = perform_koenig_lookup (id_end, vec,
13980 tf_warning_or_error);
13981 *end = finish_call_expr (member_end, &vec, false, true,
13982 tf_warning_or_error);
13985 /* Last common checks. */
13986 if (*begin == error_mark_node || *end == error_mark_node)
13988 /* If one of the expressions is an error do no more checks. */
13989 *begin = *end = error_mark_node;
13990 return error_mark_node;
13992 else if (type_dependent_expression_p (*begin)
13993 || type_dependent_expression_p (*end))
13994 /* Can happen, when, eg, in a template context, Koenig lookup
13995 can't resolve begin/end (c++/58503). */
13996 return NULL_TREE;
13997 else
13999 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
14000 /* The unqualified type of the __begin and __end temporaries should
14001 be the same, as required by the multiple auto declaration. */
14002 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
14004 if (cxx_dialect >= cxx17
14005 && (build_x_binary_op (input_location, NE_EXPR,
14006 *begin, ERROR_MARK,
14007 *end, ERROR_MARK,
14008 NULL_TREE, NULL, tf_none)
14009 != error_mark_node))
14010 /* P0184R0 allows __begin and __end to have different types,
14011 but make sure they are comparable so we can give a better
14012 diagnostic. */;
14013 else
14014 error ("inconsistent begin/end types in range-based %<for%> "
14015 "statement: %qT and %qT",
14016 TREE_TYPE (*begin), TREE_TYPE (*end));
14018 return iter_type;
14023 /* Helper function for cp_parser_perform_range_for_lookup.
14024 Builds a tree for RANGE.IDENTIFIER(). */
14026 static tree
14027 cp_parser_range_for_member_function (tree range, tree identifier)
14029 tree member, res;
14031 member = finish_class_member_access_expr (range, identifier,
14032 false, tf_warning_or_error);
14033 if (member == error_mark_node)
14034 return error_mark_node;
14036 releasing_vec vec;
14037 res = finish_call_expr (member, &vec,
14038 /*disallow_virtual=*/false,
14039 /*koenig_p=*/false,
14040 tf_warning_or_error);
14041 return res;
14044 /* Parse an iteration-statement.
14046 iteration-statement:
14047 while ( condition ) statement
14048 do statement while ( expression ) ;
14049 for ( init-statement condition [opt] ; expression [opt] )
14050 statement
14052 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14054 static tree
14055 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14056 unsigned short unroll)
14058 cp_token *token;
14059 enum rid keyword;
14060 tree statement;
14061 unsigned char in_statement;
14062 token_indent_info guard_tinfo;
14064 /* Peek at the next token. */
14065 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14066 if (!token)
14067 return error_mark_node;
14069 guard_tinfo = get_token_indent_info (token);
14071 /* Remember whether or not we are already within an iteration
14072 statement. */
14073 in_statement = parser->in_statement;
14075 /* See what kind of keyword it is. */
14076 keyword = token->keyword;
14077 switch (keyword)
14079 case RID_WHILE:
14081 tree condition;
14083 /* Begin the while-statement. */
14084 statement = begin_while_stmt ();
14085 /* Look for the `('. */
14086 matching_parens parens;
14087 parens.require_open (parser);
14088 /* Parse the condition. */
14089 condition = cp_parser_condition (parser);
14090 finish_while_stmt_cond (condition, statement, ivdep, unroll);
14091 /* Look for the `)'. */
14092 parens.require_close (parser);
14093 /* Parse the dependent statement. */
14094 parser->in_statement = IN_ITERATION_STMT;
14095 bool prev = note_iteration_stmt_body_start ();
14096 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14097 note_iteration_stmt_body_end (prev);
14098 parser->in_statement = in_statement;
14099 /* We're done with the while-statement. */
14100 finish_while_stmt (statement);
14102 break;
14104 case RID_DO:
14106 tree expression;
14108 /* Begin the do-statement. */
14109 statement = begin_do_stmt ();
14110 /* Parse the body of the do-statement. */
14111 parser->in_statement = IN_ITERATION_STMT;
14112 bool prev = note_iteration_stmt_body_start ();
14113 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14114 note_iteration_stmt_body_end (prev);
14115 parser->in_statement = in_statement;
14116 finish_do_body (statement);
14117 /* Look for the `while' keyword. */
14118 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14119 /* Look for the `('. */
14120 matching_parens parens;
14121 parens.require_open (parser);
14122 /* Parse the expression. */
14123 expression = cp_parser_expression (parser);
14124 /* We're done with the do-statement. */
14125 finish_do_stmt (expression, statement, ivdep, unroll);
14126 /* Look for the `)'. */
14127 parens.require_close (parser);
14128 /* Look for the `;'. */
14129 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14131 break;
14133 case RID_FOR:
14135 /* Look for the `('. */
14136 matching_parens parens;
14137 parens.require_open (parser);
14139 statement = cp_parser_for (parser, ivdep, unroll);
14141 /* Look for the `)'. */
14142 parens.require_close (parser);
14144 /* Parse the body of the for-statement. */
14145 parser->in_statement = IN_ITERATION_STMT;
14146 bool prev = note_iteration_stmt_body_start ();
14147 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14148 note_iteration_stmt_body_end (prev);
14149 parser->in_statement = in_statement;
14151 /* We're done with the for-statement. */
14152 finish_for_stmt (statement);
14154 break;
14156 default:
14157 cp_parser_error (parser, "expected iteration-statement");
14158 statement = error_mark_node;
14159 break;
14162 return statement;
14165 /* Parse an init-statement or the declarator of a range-based-for.
14166 Returns true if a range-based-for declaration is seen.
14168 init-statement:
14169 expression-statement
14170 simple-declaration
14171 alias-declaration */
14173 static bool
14174 cp_parser_init_statement (cp_parser *parser, tree *decl)
14176 /* If the next token is a `;', then we have an empty
14177 expression-statement. Grammatically, this is also a
14178 simple-declaration, but an invalid one, because it does not
14179 declare anything. Therefore, if we did not handle this case
14180 specially, we would issue an error message about an invalid
14181 declaration. */
14182 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14184 bool is_range_for = false;
14185 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14187 /* A colon is used in range-based for. */
14188 parser->colon_corrects_to_scope_p = false;
14190 /* We're going to speculatively look for a declaration, falling back
14191 to an expression, if necessary. */
14192 cp_parser_parse_tentatively (parser);
14193 bool expect_semicolon_p = true;
14194 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14196 cp_parser_alias_declaration (parser);
14197 expect_semicolon_p = false;
14198 if (cxx_dialect < cxx23
14199 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14200 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14201 OPT_Wc__23_extensions,
14202 "alias-declaration in init-statement only "
14203 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14205 else
14206 /* Parse the declaration. */
14207 cp_parser_simple_declaration (parser,
14208 /*function_definition_allowed_p=*/false,
14209 decl);
14210 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14211 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14213 /* It is a range-for, consume the ':'. */
14214 cp_lexer_consume_token (parser->lexer);
14215 is_range_for = true;
14216 if (cxx_dialect < cxx11)
14217 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14218 OPT_Wc__11_extensions,
14219 "range-based %<for%> loops only available with "
14220 "%<-std=c++11%> or %<-std=gnu++11%>");
14222 else if (expect_semicolon_p)
14223 /* The ';' is not consumed yet because we told
14224 cp_parser_simple_declaration not to. */
14225 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14227 if (cp_parser_parse_definitely (parser))
14228 return is_range_for;
14229 /* If the tentative parse failed, then we shall need to look for an
14230 expression-statement. */
14232 /* If we are here, it is an expression-statement. */
14233 cp_parser_expression_statement (parser, NULL_TREE);
14234 return false;
14237 /* Parse a jump-statement.
14239 jump-statement:
14240 break ;
14241 continue ;
14242 return expression [opt] ;
14243 return braced-init-list ;
14244 coroutine-return-statement;
14245 goto identifier ;
14247 GNU extension:
14249 jump-statement:
14250 goto * expression ;
14252 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14254 static tree
14255 cp_parser_jump_statement (cp_parser* parser)
14257 tree statement = error_mark_node;
14258 cp_token *token;
14259 enum rid keyword;
14260 unsigned char in_statement;
14262 /* Peek at the next token. */
14263 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14264 if (!token)
14265 return error_mark_node;
14267 /* See what kind of keyword it is. */
14268 keyword = token->keyword;
14269 switch (keyword)
14271 case RID_BREAK:
14272 in_statement = parser->in_statement & ~IN_IF_STMT;
14273 switch (in_statement)
14275 case 0:
14276 error_at (token->location, "break statement not within loop or switch");
14277 break;
14278 default:
14279 gcc_assert ((in_statement & IN_SWITCH_STMT)
14280 || in_statement == IN_ITERATION_STMT);
14281 statement = finish_break_stmt ();
14282 if (in_statement == IN_ITERATION_STMT)
14283 break_maybe_infinite_loop ();
14284 break;
14285 case IN_OMP_BLOCK:
14286 error_at (token->location, "invalid exit from OpenMP structured block");
14287 break;
14288 case IN_OMP_FOR:
14289 error_at (token->location, "break statement used with OpenMP for loop");
14290 break;
14292 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14293 break;
14295 case RID_CONTINUE:
14296 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14298 case 0:
14299 error_at (token->location, "continue statement not within a loop");
14300 break;
14301 /* Fall through. */
14302 case IN_ITERATION_STMT:
14303 case IN_OMP_FOR:
14304 statement = finish_continue_stmt ();
14305 break;
14306 case IN_OMP_BLOCK:
14307 error_at (token->location, "invalid exit from OpenMP structured block");
14308 break;
14309 default:
14310 gcc_unreachable ();
14312 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14313 break;
14315 case RID_CO_RETURN:
14316 case RID_RETURN:
14318 tree expr;
14319 bool expr_non_constant_p;
14321 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14323 cp_lexer_set_source_position (parser->lexer);
14324 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14325 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
14327 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14328 expr = cp_parser_expression (parser);
14329 else
14330 /* If the next token is a `;', then there is no
14331 expression. */
14332 expr = NULL_TREE;
14333 /* Build the return-statement, check co-return first, since type
14334 deduction is not valid there. */
14335 if (keyword == RID_CO_RETURN)
14336 statement = finish_co_return_stmt (token->location, expr);
14337 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14338 /* Don't deduce from a discarded return statement. */;
14339 else
14340 statement = finish_return_stmt (expr);
14341 /* Look for the final `;'. */
14342 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14344 break;
14346 case RID_GOTO:
14347 if (parser->in_function_body
14348 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14349 && cxx_dialect < cxx23)
14351 error ("%<goto%> in %<constexpr%> function only available with "
14352 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14353 cp_function_chain->invalid_constexpr = true;
14356 /* Create the goto-statement. */
14357 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14359 /* Issue a warning about this use of a GNU extension. */
14360 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14361 /* Consume the '*' token. */
14362 cp_lexer_consume_token (parser->lexer);
14363 /* Parse the dependent expression. */
14364 finish_goto_stmt (cp_parser_expression (parser));
14366 else
14367 finish_goto_stmt (cp_parser_identifier (parser));
14368 /* Look for the final `;'. */
14369 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14370 break;
14372 default:
14373 cp_parser_error (parser, "expected jump-statement");
14374 break;
14377 return statement;
14380 /* Parse a declaration-statement.
14382 declaration-statement:
14383 block-declaration */
14385 static void
14386 cp_parser_declaration_statement (cp_parser* parser)
14388 void *p;
14390 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14391 p = obstack_alloc (&declarator_obstack, 0);
14393 /* Parse the block-declaration. */
14394 cp_parser_block_declaration (parser, /*statement_p=*/true);
14396 /* Free any declarators allocated. */
14397 obstack_free (&declarator_obstack, p);
14400 /* Some dependent statements (like `if (cond) statement'), are
14401 implicitly in their own scope. In other words, if the statement is
14402 a single statement (as opposed to a compound-statement), it is
14403 none-the-less treated as if it were enclosed in braces. Any
14404 declarations appearing in the dependent statement are out of scope
14405 after control passes that point. This function parses a statement,
14406 but ensures that is in its own scope, even if it is not a
14407 compound-statement.
14409 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14410 is a (possibly labeled) if statement which is not enclosed in
14411 braces and has an else clause. This is used to implement
14412 -Wparentheses.
14414 CHAIN is a vector of if-else-if conditions. This is used to implement
14415 -Wduplicated-cond.
14417 Returns the new statement. */
14419 static tree
14420 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14421 const token_indent_info &guard_tinfo,
14422 vec<tree> *chain)
14424 tree statement;
14425 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14426 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14427 token_indent_info body_tinfo
14428 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14430 if (if_p != NULL)
14431 *if_p = false;
14433 /* Mark if () ; with a special NOP_EXPR. */
14434 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14436 cp_lexer_consume_token (parser->lexer);
14437 statement = add_stmt (build_empty_stmt (body_loc));
14439 if (guard_tinfo.keyword == RID_IF
14440 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14441 warning_at (body_loc, OPT_Wempty_body,
14442 "suggest braces around empty body in an %<if%> statement");
14443 else if (guard_tinfo.keyword == RID_ELSE)
14444 warning_at (body_loc, OPT_Wempty_body,
14445 "suggest braces around empty body in an %<else%> statement");
14447 /* if a compound is opened, we simply parse the statement directly. */
14448 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14449 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14450 /* If the token is not a `{', then we must take special action. */
14451 else
14453 /* Create a compound-statement. */
14454 statement = begin_compound_stmt (0);
14455 /* Parse the dependent-statement. */
14456 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14457 &body_loc_after_labels);
14458 /* Finish the dummy compound-statement. */
14459 finish_compound_stmt (statement);
14462 token_indent_info next_tinfo
14463 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14464 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14466 if (body_loc_after_labels != UNKNOWN_LOCATION
14467 && next_tinfo.type != CPP_SEMICOLON)
14468 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14469 guard_tinfo.location, guard_tinfo.keyword);
14471 /* Return the statement. */
14472 return statement;
14475 /* For some dependent statements (like `while (cond) statement'), we
14476 have already created a scope. Therefore, even if the dependent
14477 statement is a compound-statement, we do not want to create another
14478 scope. */
14480 static void
14481 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14482 const token_indent_info &guard_tinfo)
14484 /* If the token is a `{', then we must take special action. */
14485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14487 token_indent_info body_tinfo
14488 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14489 location_t loc_after_labels = UNKNOWN_LOCATION;
14491 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14492 &loc_after_labels);
14493 token_indent_info next_tinfo
14494 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14495 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14497 if (loc_after_labels != UNKNOWN_LOCATION
14498 && next_tinfo.type != CPP_SEMICOLON)
14499 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14500 guard_tinfo.location,
14501 guard_tinfo.keyword);
14503 else
14505 /* Avoid calling cp_parser_compound_statement, so that we
14506 don't create a new scope. Do everything else by hand. */
14507 matching_braces braces;
14508 braces.require_open (parser);
14509 /* If the next keyword is `__label__' we have a label declaration. */
14510 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14511 cp_parser_label_declaration (parser);
14512 /* Parse an (optional) statement-seq. */
14513 cp_parser_statement_seq_opt (parser, NULL_TREE);
14514 braces.require_close (parser);
14518 /* Modules */
14520 /* Parse a module-name,
14521 identifier
14522 module-name . identifier
14523 header-name
14525 Returns a pointer to module object, NULL. */
14527 static module_state *
14528 cp_parser_module_name (cp_parser *parser)
14530 cp_token *token = cp_lexer_peek_token (parser->lexer);
14531 if (token->type == CPP_HEADER_NAME)
14533 cp_lexer_consume_token (parser->lexer);
14535 return get_module (token->u.value);
14538 module_state *parent = NULL;
14539 bool partitioned = false;
14540 if (token->type == CPP_COLON && named_module_p ())
14542 partitioned = true;
14543 cp_lexer_consume_token (parser->lexer);
14546 for (;;)
14548 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
14550 cp_parser_error (parser, "expected module-name");
14551 break;
14554 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
14555 parent = get_module (name, parent, partitioned);
14556 token = cp_lexer_peek_token (parser->lexer);
14557 if (!partitioned && token->type == CPP_COLON)
14558 partitioned = true;
14559 else if (token->type != CPP_DOT)
14560 break;
14562 cp_lexer_consume_token (parser->lexer);
14565 return parent;
14568 /* Named module-declaration
14569 __module ; PRAGMA_EOL
14570 __module private ; PRAGMA_EOL (unimplemented)
14571 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14574 static module_parse
14575 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
14576 bool exporting)
14578 /* We're a pseudo pragma. */
14579 parser->lexer->in_pragma = true;
14580 cp_token *token = cp_lexer_consume_token (parser->lexer);
14582 if (flag_header_unit)
14584 error_at (token->location,
14585 "module-declaration not permitted in header-unit");
14586 goto skip_eol;
14588 else if (mp_state == MP_FIRST && !exporting
14589 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14591 /* Start global module fragment. */
14592 cp_lexer_consume_token (parser->lexer);
14593 module_kind = MK_NAMED;
14594 mp_state = MP_GLOBAL;
14595 cp_parser_require_pragma_eol (parser, token);
14597 else if (!exporting
14598 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14599 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
14600 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
14602 cp_lexer_consume_token (parser->lexer);
14603 cp_lexer_consume_token (parser->lexer);
14604 cp_lexer_consume_token (parser->lexer);
14605 cp_parser_require_pragma_eol (parser, token);
14607 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
14608 && module_has_cmi_p ())
14610 mp_state = MP_PRIVATE_IMPORTS;
14611 sorry_at (token->location, "private module fragment");
14613 else
14614 error_at (token->location,
14615 "private module fragment only permitted in purview"
14616 " of module interface or partition");
14618 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
14620 /* Neither the first declaration, nor in a GMF. */
14621 error_at (token->location, "module-declaration only permitted as first"
14622 " declaration, or ending a global module fragment");
14623 skip_eol:
14624 cp_parser_skip_to_pragma_eol (parser, token);
14626 else
14628 module_state *mod = cp_parser_module_name (parser);
14629 tree attrs = cp_parser_attributes_opt (parser);
14631 mp_state = MP_PURVIEW_IMPORTS;
14632 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14633 goto skip_eol;
14635 declare_module (mod, token->location, exporting, attrs, parse_in);
14636 cp_parser_require_pragma_eol (parser, token);
14639 return mp_state;
14642 /* Import-declaration
14643 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14645 static void
14646 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
14647 bool exporting)
14649 /* We're a pseudo pragma. */
14650 parser->lexer->in_pragma = true;
14651 cp_token *token = cp_lexer_consume_token (parser->lexer);
14653 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
14655 error_at (token->location, "post-module-declaration"
14656 " imports must be contiguous");
14657 note_lexer:
14658 inform (token->location, "perhaps insert a line break, or other"
14659 " disambiguation, to prevent this being considered a"
14660 " module control-line");
14661 skip_eol:
14662 cp_parser_skip_to_pragma_eol (parser, token);
14664 else if (current_scope () != global_namespace)
14666 error_at (token->location, "import-declaration must be at global scope");
14667 goto note_lexer;
14669 else
14671 module_state *mod = cp_parser_module_name (parser);
14672 tree attrs = cp_parser_attributes_opt (parser);
14674 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14675 goto skip_eol;
14676 cp_parser_require_pragma_eol (parser, token);
14678 if (parser->in_unbraced_linkage_specification_p)
14679 error_at (token->location, "import cannot appear directly in"
14680 " a linkage-specification");
14682 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
14684 /* Module-purview imports must not be from source inclusion
14685 [cpp.import]/7 */
14686 if (attrs
14687 && private_lookup_attribute ("__translated",
14688 strlen ("__translated"), attrs))
14689 error_at (token->location, "post-module-declaration imports"
14690 " must not be include-translated");
14691 else if (!token->main_source_p)
14692 error_at (token->location, "post-module-declaration imports"
14693 " must not be from header inclusion");
14696 import_module (mod, token->location, exporting, attrs, parse_in);
14700 /* export-declaration.
14702 export declaration
14703 export { declaration-seq-opt } */
14705 static void
14706 cp_parser_module_export (cp_parser *parser)
14708 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
14709 cp_token *token = cp_lexer_consume_token (parser->lexer);
14711 if (!module_interface_p ())
14712 error_at (token->location,
14713 "%qE may only occur after a module interface declaration",
14714 token->u.value);
14716 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
14718 unsigned mk = module_kind;
14719 if (module_exporting_p ())
14720 error_at (token->location,
14721 "%qE may only occur once in an export declaration",
14722 token->u.value);
14723 module_kind |= MK_EXPORTING;
14725 if (braced)
14727 cp_ensure_no_omp_declare_simd (parser);
14728 cp_ensure_no_oacc_routine (parser);
14730 cp_lexer_consume_token (parser->lexer);
14731 cp_parser_declaration_seq_opt (parser);
14732 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14734 else
14736 /* Explicitly check if the next tokens might be a
14737 module-directive line, so we can give a clearer error message
14738 about why the directive will be rejected. */
14739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
14740 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
14741 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
14742 error_at (token->location, "%<export%> not part of following"
14743 " module-directive");
14744 cp_parser_declaration (parser, NULL_TREE);
14747 module_kind = mk;
14750 /* Declarations [gram.dcl.dcl] */
14752 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
14753 is the top-level declaration sequence. That affects whether we
14754 deal with module-preamble.
14756 declaration-seq:
14757 declaration
14758 declaration-seq declaration */
14760 static void
14761 cp_parser_declaration_seq_opt (cp_parser* parser)
14763 while (true)
14765 cp_token *token = cp_lexer_peek_token (parser->lexer);
14767 if (token->type == CPP_CLOSE_BRACE
14768 || token->type == CPP_EOF)
14769 break;
14770 else
14771 cp_parser_toplevel_declaration (parser);
14775 /* Parse a declaration.
14777 declaration:
14778 block-declaration
14779 function-definition
14780 template-declaration
14781 explicit-instantiation
14782 explicit-specialization
14783 linkage-specification
14784 namespace-definition
14786 C++17:
14787 deduction-guide
14789 modules:
14790 (all these are only allowed at the outermost level, check
14791 that semantically, for better diagnostics)
14792 module-declaration
14793 module-export-declaration
14794 module-import-declaration
14795 export-declaration
14797 GNU extension:
14799 declaration:
14800 __extension__ declaration */
14802 static void
14803 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
14805 int saved_pedantic;
14807 /* Check for the `__extension__' keyword. */
14808 if (cp_parser_extension_opt (parser, &saved_pedantic))
14810 /* Parse the qualified declaration. */
14811 cp_parser_declaration (parser, prefix_attrs);
14812 /* Restore the PEDANTIC flag. */
14813 pedantic = saved_pedantic;
14815 return;
14818 /* Try to figure out what kind of declaration is present. */
14819 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14820 cp_token *token2 = (token1->type == CPP_EOF
14821 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14823 if (token1->type == CPP_SEMICOLON)
14825 cp_lexer_consume_token (parser->lexer);
14826 /* A declaration consisting of a single semicolon is invalid
14827 * before C++11. Allow it unless we're being pedantic. */
14828 if (cxx_dialect < cxx11)
14829 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
14830 return;
14832 else if (cp_lexer_nth_token_is (parser->lexer,
14833 cp_parser_skip_std_attribute_spec_seq (parser,
14835 CPP_SEMICOLON))
14837 location_t attrs_loc = token1->location;
14838 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
14840 if (std_attrs && (flag_openmp || flag_openmp_simd))
14842 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
14843 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
14844 std_attrs);
14845 if (parser->lexer->in_omp_attribute_pragma)
14847 cp_lexer *lexer = parser->lexer;
14848 while (parser->lexer->in_omp_attribute_pragma)
14850 gcc_assert (cp_lexer_next_token_is (parser->lexer,
14851 CPP_PRAGMA));
14852 cp_parser_pragma (parser, pragma_external, NULL);
14854 cp_lexer_destroy (lexer);
14858 if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs))
14859 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
14860 OPT_Wattributes, "attribute ignored");
14861 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14862 cp_lexer_consume_token (parser->lexer);
14863 return;
14866 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14867 void *p = obstack_alloc (&declarator_obstack, 0);
14869 tree attributes = NULL_TREE;
14871 /* Conditionally, allow attributes to precede a linkage specification. */
14872 if (token1->keyword == RID_ATTRIBUTE)
14874 cp_lexer_save_tokens (parser->lexer);
14875 attributes = cp_parser_attributes_opt (parser);
14876 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
14877 cp_token *t2 = (t1->type == CPP_EOF
14878 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14879 if (t1->keyword == RID_EXTERN
14880 && cp_parser_is_pure_string_literal (t2))
14882 cp_lexer_commit_tokens (parser->lexer);
14883 /* We might have already been here. */
14884 if (!c_dialect_objc ())
14886 location_t where = get_finish (t2->location);
14887 warning_at (token1->location, OPT_Wattributes, "attributes are"
14888 " not permitted in this position");
14889 where = linemap_position_for_loc_and_offset (line_table,
14890 where, 1);
14891 inform (where, "attributes may be inserted here");
14892 attributes = NULL_TREE;
14894 token1 = t1;
14895 token2 = t2;
14897 else
14899 cp_lexer_rollback_tokens (parser->lexer);
14900 attributes = NULL_TREE;
14903 /* If we already had some attributes, and we've added more, then prepend.
14904 Otherwise attributes just contains any that we just read. */
14905 if (prefix_attrs)
14907 if (attributes)
14908 TREE_CHAIN (prefix_attrs) = attributes;
14909 attributes = prefix_attrs;
14912 /* If the next token is `extern' and the following token is a string
14913 literal, then we have a linkage specification. */
14914 if (token1->keyword == RID_EXTERN
14915 && cp_parser_is_pure_string_literal (token2))
14916 cp_parser_linkage_specification (parser, attributes);
14917 /* If the next token is `template', then we have either a template
14918 declaration, an explicit instantiation, or an explicit
14919 specialization. */
14920 else if (token1->keyword == RID_TEMPLATE)
14922 /* `template <>' indicates a template specialization. */
14923 if (token2->type == CPP_LESS
14924 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14925 cp_parser_explicit_specialization (parser);
14926 /* `template <' indicates a template declaration. */
14927 else if (token2->type == CPP_LESS)
14928 cp_parser_template_declaration (parser, /*member_p=*/false);
14929 /* Anything else must be an explicit instantiation. */
14930 else
14931 cp_parser_explicit_instantiation (parser);
14933 /* If the next token is `export', it's new-style modules or
14934 old-style template. */
14935 else if (token1->keyword == RID_EXPORT)
14937 if (!modules_p ())
14938 cp_parser_template_declaration (parser, /*member_p=*/false);
14939 else
14940 cp_parser_module_export (parser);
14942 else if (token1->keyword == RID__EXPORT
14943 || token1->keyword == RID__IMPORT
14944 || token1->keyword == RID__MODULE)
14946 bool exporting = token1->keyword == RID__EXPORT;
14947 cp_token *next = exporting ? token2 : token1;
14948 if (exporting)
14949 cp_lexer_consume_token (parser->lexer);
14950 // In module purview this will be ill-formed.
14951 auto state = (!named_module_p () ? MP_NOT_MODULE
14952 : module_purview_p () ? MP_PURVIEW
14953 : MP_GLOBAL);
14954 if (next->keyword == RID__MODULE)
14955 cp_parser_module_declaration (parser, state, exporting);
14956 else
14957 cp_parser_import_declaration (parser, state, exporting);
14959 /* If the next token is `extern', 'static' or 'inline' and the one
14960 after that is `template', we have a GNU extended explicit
14961 instantiation directive. */
14962 else if (cp_parser_allow_gnu_extensions_p (parser)
14963 && token2->keyword == RID_TEMPLATE
14964 && (token1->keyword == RID_EXTERN
14965 || token1->keyword == RID_STATIC
14966 || token1->keyword == RID_INLINE))
14967 cp_parser_explicit_instantiation (parser);
14968 /* If the next token is `namespace', check for a named or unnamed
14969 namespace definition. */
14970 else if (token1->keyword == RID_NAMESPACE
14971 && (/* A named namespace definition. */
14972 (token2->type == CPP_NAME
14973 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
14974 != CPP_EQ))
14975 || (token2->type == CPP_OPEN_SQUARE
14976 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
14977 == CPP_OPEN_SQUARE)
14978 /* An unnamed namespace definition. */
14979 || token2->type == CPP_OPEN_BRACE
14980 || token2->keyword == RID_ATTRIBUTE))
14981 cp_parser_namespace_definition (parser);
14982 /* An inline (associated) namespace definition. */
14983 else if (token2->keyword == RID_NAMESPACE
14984 && token1->keyword == RID_INLINE)
14985 cp_parser_namespace_definition (parser);
14986 /* Objective-C++ declaration/definition. */
14987 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
14988 cp_parser_objc_declaration (parser, attributes);
14989 else if (c_dialect_objc ()
14990 && token1->keyword == RID_ATTRIBUTE
14991 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
14992 cp_parser_objc_declaration (parser, attributes);
14993 /* At this point we may have a template declared by a concept
14994 introduction. */
14995 else if (flag_concepts
14996 && cp_parser_template_declaration_after_export (parser,
14997 /*member_p=*/false))
14998 /* We did. */;
14999 else
15000 /* Try to parse a block-declaration, or a function-definition. */
15001 cp_parser_block_declaration (parser, /*statement_p=*/false);
15003 /* Free any declarators allocated. */
15004 obstack_free (&declarator_obstack, p);
15007 /* Parse a namespace-scope declaration. */
15009 static void
15010 cp_parser_toplevel_declaration (cp_parser* parser)
15012 cp_token *token = cp_lexer_peek_token (parser->lexer);
15014 if (token->type == CPP_PRAGMA)
15015 /* A top-level declaration can consist solely of a #pragma. A
15016 nested declaration cannot, so this is done here and not in
15017 cp_parser_declaration. (A #pragma at block scope is
15018 handled in cp_parser_statement.) */
15019 cp_parser_pragma (parser, pragma_external, NULL);
15020 else
15021 /* Parse the declaration itself. */
15022 cp_parser_declaration (parser, NULL_TREE);
15025 /* Parse a block-declaration.
15027 block-declaration:
15028 simple-declaration
15029 asm-definition
15030 namespace-alias-definition
15031 using-declaration
15032 using-directive
15034 GNU Extension:
15036 block-declaration:
15037 __extension__ block-declaration
15039 C++0x Extension:
15041 block-declaration:
15042 static_assert-declaration
15044 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15045 part of a declaration-statement. */
15047 static void
15048 cp_parser_block_declaration (cp_parser *parser,
15049 bool statement_p)
15051 int saved_pedantic;
15053 /* Check for the `__extension__' keyword. */
15054 if (cp_parser_extension_opt (parser, &saved_pedantic))
15056 /* Parse the qualified declaration. */
15057 cp_parser_block_declaration (parser, statement_p);
15058 /* Restore the PEDANTIC flag. */
15059 pedantic = saved_pedantic;
15061 return;
15064 /* Peek at the next token to figure out which kind of declaration is
15065 present. */
15066 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15067 size_t attr_idx;
15069 /* If the next keyword is `asm', we have an asm-definition. */
15070 if (token1->keyword == RID_ASM)
15072 if (statement_p)
15073 cp_parser_commit_to_tentative_parse (parser);
15074 cp_parser_asm_definition (parser);
15076 /* If the next keyword is `namespace', we have a
15077 namespace-alias-definition. */
15078 else if (token1->keyword == RID_NAMESPACE)
15079 cp_parser_namespace_alias_definition (parser);
15080 /* If the next keyword is `using', we have a
15081 using-declaration, a using-directive, or an alias-declaration. */
15082 else if (token1->keyword == RID_USING)
15084 cp_token *token2;
15086 if (statement_p)
15087 cp_parser_commit_to_tentative_parse (parser);
15088 /* If the token after `using' is `namespace', then we have a
15089 using-directive. */
15090 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15091 if (token2->keyword == RID_NAMESPACE)
15092 cp_parser_using_directive (parser);
15093 else if (token2->keyword == RID_ENUM)
15094 cp_parser_using_enum (parser);
15095 /* If the second token after 'using' is '=', then we have an
15096 alias-declaration. */
15097 else if (cxx_dialect >= cxx11
15098 && token2->type == CPP_NAME
15099 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15100 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15101 cp_parser_alias_declaration (parser);
15102 /* Otherwise, it's a using-declaration. */
15103 else
15104 cp_parser_using_declaration (parser,
15105 /*access_declaration_p=*/false);
15107 /* If the next keyword is `__label__' we have a misplaced label
15108 declaration. */
15109 else if (token1->keyword == RID_LABEL)
15111 cp_lexer_consume_token (parser->lexer);
15112 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15113 cp_parser_skip_to_end_of_statement (parser);
15114 /* If the next token is now a `;', consume it. */
15115 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15116 cp_lexer_consume_token (parser->lexer);
15118 /* If the next token is `static_assert' we have a static assertion. */
15119 else if (token1->keyword == RID_STATIC_ASSERT)
15120 cp_parser_static_assert (parser, /*member_p=*/false);
15121 /* If the next tokens after attributes is `using namespace', then we have
15122 a using-directive. */
15123 else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
15124 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx,
15125 RID_USING)
15126 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15127 RID_NAMESPACE))
15129 if (statement_p)
15130 cp_parser_commit_to_tentative_parse (parser);
15131 cp_parser_using_directive (parser);
15133 /* Anything else must be a simple-declaration. */
15134 else
15135 cp_parser_simple_declaration (parser, !statement_p,
15136 /*maybe_range_for_decl*/NULL);
15139 /* Parse a simple-declaration.
15141 simple-declaration:
15142 decl-specifier-seq [opt] init-declarator-list [opt] ;
15143 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15144 brace-or-equal-initializer ;
15146 init-declarator-list:
15147 init-declarator
15148 init-declarator-list , init-declarator
15150 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15151 function-definition as a simple-declaration.
15153 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15154 parsed declaration if it is an uninitialized single declarator not followed
15155 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15156 if present, will not be consumed. */
15158 static void
15159 cp_parser_simple_declaration (cp_parser* parser,
15160 bool function_definition_allowed_p,
15161 tree *maybe_range_for_decl)
15163 cp_decl_specifier_seq decl_specifiers;
15164 int declares_class_or_enum;
15165 bool saw_declarator;
15166 location_t comma_loc = UNKNOWN_LOCATION;
15167 location_t init_loc = UNKNOWN_LOCATION;
15169 if (maybe_range_for_decl)
15170 *maybe_range_for_decl = NULL_TREE;
15172 /* Defer access checks until we know what is being declared; the
15173 checks for names appearing in the decl-specifier-seq should be
15174 done as if we were in the scope of the thing being declared. */
15175 push_deferring_access_checks (dk_deferred);
15177 /* Parse the decl-specifier-seq. We have to keep track of whether
15178 or not the decl-specifier-seq declares a named class or
15179 enumeration type, since that is the only case in which the
15180 init-declarator-list is allowed to be empty.
15182 [dcl.dcl]
15184 In a simple-declaration, the optional init-declarator-list can be
15185 omitted only when declaring a class or enumeration, that is when
15186 the decl-specifier-seq contains either a class-specifier, an
15187 elaborated-type-specifier, or an enum-specifier. */
15188 cp_parser_decl_specifier_seq (parser,
15189 CP_PARSER_FLAGS_OPTIONAL,
15190 &decl_specifiers,
15191 &declares_class_or_enum);
15192 /* We no longer need to defer access checks. */
15193 stop_deferring_access_checks ();
15195 cp_omp_declare_simd_data odsd;
15196 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15197 cp_parser_handle_directive_omp_attributes (parser,
15198 &decl_specifiers.attributes,
15199 &odsd, true);
15201 /* In a block scope, a valid declaration must always have a
15202 decl-specifier-seq. By not trying to parse declarators, we can
15203 resolve the declaration/expression ambiguity more quickly. */
15204 if (!function_definition_allowed_p
15205 && !decl_specifiers.any_specifiers_p)
15207 cp_parser_error (parser, "expected declaration");
15208 goto done;
15211 /* If the next two tokens are both identifiers, the code is
15212 erroneous. The usual cause of this situation is code like:
15214 T t;
15216 where "T" should name a type -- but does not. */
15217 if (!decl_specifiers.any_type_specifiers_p
15218 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15220 /* If parsing tentatively, we should commit; we really are
15221 looking at a declaration. */
15222 cp_parser_commit_to_tentative_parse (parser);
15223 /* Give up. */
15224 goto done;
15227 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15229 /* Look for C++17 decomposition declaration. */
15230 for (size_t n = 1; ; n++)
15231 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15232 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15233 continue;
15234 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15235 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15236 && decl_specifiers.any_specifiers_p)
15238 tree decl
15239 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15240 maybe_range_for_decl,
15241 &init_loc);
15243 /* The next token should be either a `,' or a `;'. */
15244 cp_token *token = cp_lexer_peek_token (parser->lexer);
15245 /* If it's a `;', we are done. */
15246 if (token->type == CPP_SEMICOLON)
15247 goto finish;
15248 else if (maybe_range_for_decl)
15250 if (*maybe_range_for_decl == NULL_TREE)
15251 *maybe_range_for_decl = error_mark_node;
15252 goto finish;
15254 /* Anything else is an error. */
15255 else
15257 /* If we have already issued an error message we don't need
15258 to issue another one. */
15259 if ((decl != error_mark_node
15260 && DECL_INITIAL (decl) != error_mark_node)
15261 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15262 cp_parser_error (parser, "expected %<;%>");
15263 /* Skip tokens until we reach the end of the statement. */
15264 cp_parser_skip_to_end_of_statement (parser);
15265 /* If the next token is now a `;', consume it. */
15266 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15267 cp_lexer_consume_token (parser->lexer);
15268 goto done;
15271 else
15272 break;
15274 tree last_type;
15275 bool auto_specifier_p;
15276 /* NULL_TREE if both variable and function declaration are allowed,
15277 error_mark_node if function declaration are not allowed and
15278 a FUNCTION_DECL that should be diagnosed if it is followed by
15279 variable declarations. */
15280 tree auto_function_declaration;
15282 last_type = NULL_TREE;
15283 auto_specifier_p
15284 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15285 auto_function_declaration = NULL_TREE;
15287 /* Keep going until we hit the `;' at the end of the simple
15288 declaration. */
15289 saw_declarator = false;
15290 while (cp_lexer_next_token_is_not (parser->lexer,
15291 CPP_SEMICOLON))
15293 cp_token *token;
15294 bool function_definition_p;
15295 tree decl;
15296 tree auto_result = NULL_TREE;
15298 if (saw_declarator)
15300 /* If we are processing next declarator, comma is expected */
15301 token = cp_lexer_peek_token (parser->lexer);
15302 gcc_assert (token->type == CPP_COMMA);
15303 cp_lexer_consume_token (parser->lexer);
15304 if (maybe_range_for_decl)
15306 *maybe_range_for_decl = error_mark_node;
15307 if (comma_loc == UNKNOWN_LOCATION)
15308 comma_loc = token->location;
15311 else
15312 saw_declarator = true;
15314 /* Parse the init-declarator. */
15315 decl = cp_parser_init_declarator (parser,
15316 CP_PARSER_FLAGS_NONE,
15317 &decl_specifiers,
15318 /*checks=*/NULL,
15319 function_definition_allowed_p,
15320 /*member_p=*/false,
15321 declares_class_or_enum,
15322 &function_definition_p,
15323 maybe_range_for_decl,
15324 &init_loc,
15325 &auto_result);
15326 /* If an error occurred while parsing tentatively, exit quickly.
15327 (That usually happens when in the body of a function; each
15328 statement is treated as a declaration-statement until proven
15329 otherwise.) */
15330 if (cp_parser_error_occurred (parser))
15331 goto done;
15333 if (auto_specifier_p && cxx_dialect >= cxx14)
15335 /* If the init-declarator-list contains more than one
15336 init-declarator, they shall all form declarations of
15337 variables. */
15338 if (auto_function_declaration == NULL_TREE)
15339 auto_function_declaration
15340 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
15341 else if (TREE_CODE (decl) == FUNCTION_DECL
15342 || auto_function_declaration != error_mark_node)
15344 error_at (decl_specifiers.locations[ds_type_spec],
15345 "non-variable %qD in declaration with more than one "
15346 "declarator with placeholder type",
15347 TREE_CODE (decl) == FUNCTION_DECL
15348 ? decl : auto_function_declaration);
15349 auto_function_declaration = error_mark_node;
15353 if (auto_result
15354 && (!processing_template_decl || !type_uses_auto (auto_result)))
15356 if (last_type
15357 && last_type != error_mark_node
15358 && !same_type_p (auto_result, last_type))
15360 /* If the list of declarators contains more than one declarator,
15361 the type of each declared variable is determined as described
15362 above. If the type deduced for the template parameter U is not
15363 the same in each deduction, the program is ill-formed. */
15364 error_at (decl_specifiers.locations[ds_type_spec],
15365 "inconsistent deduction for %qT: %qT and then %qT",
15366 decl_specifiers.type, last_type, auto_result);
15367 last_type = error_mark_node;
15369 else
15370 last_type = auto_result;
15373 /* Handle function definitions specially. */
15374 if (function_definition_p)
15376 /* If the next token is a `,', then we are probably
15377 processing something like:
15379 void f() {}, *p;
15381 which is erroneous. */
15382 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15384 cp_token *token = cp_lexer_peek_token (parser->lexer);
15385 error_at (token->location,
15386 "mixing"
15387 " declarations and function-definitions is forbidden");
15389 /* Otherwise, we're done with the list of declarators. */
15390 else
15392 pop_deferring_access_checks ();
15393 cp_finalize_omp_declare_simd (parser, &odsd);
15394 return;
15397 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15398 *maybe_range_for_decl = decl;
15399 /* The next token should be either a `,' or a `;'. */
15400 token = cp_lexer_peek_token (parser->lexer);
15401 /* If it's a `,', there are more declarators to come. */
15402 if (token->type == CPP_COMMA)
15403 /* will be consumed next time around */;
15404 /* If it's a `;', we are done. */
15405 else if (token->type == CPP_SEMICOLON)
15406 break;
15407 else if (maybe_range_for_decl)
15409 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15410 permerror (decl_specifiers.locations[ds_type_spec],
15411 "types may not be defined in a for-range-declaration");
15412 break;
15414 /* Anything else is an error. */
15415 else
15417 /* If we have already issued an error message we don't need
15418 to issue another one. */
15419 if ((decl != error_mark_node
15420 && DECL_INITIAL (decl) != error_mark_node)
15421 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15422 cp_parser_error (parser, "expected %<,%> or %<;%>");
15423 /* Skip tokens until we reach the end of the statement. */
15424 cp_parser_skip_to_end_of_statement (parser);
15425 /* If the next token is now a `;', consume it. */
15426 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15427 cp_lexer_consume_token (parser->lexer);
15428 goto done;
15430 /* After the first time around, a function-definition is not
15431 allowed -- even if it was OK at first. For example:
15433 int i, f() {}
15435 is not valid. */
15436 function_definition_allowed_p = false;
15439 /* Issue an error message if no declarators are present, and the
15440 decl-specifier-seq does not itself declare a class or
15441 enumeration: [dcl.dcl]/3. */
15442 if (!saw_declarator)
15444 if (cp_parser_declares_only_class_p (parser))
15446 if (!declares_class_or_enum
15447 && decl_specifiers.type
15448 && OVERLOAD_TYPE_P (decl_specifiers.type))
15449 /* Ensure an error is issued anyway when finish_decltype_type,
15450 called via cp_parser_decl_specifier_seq, returns a class or
15451 an enumeration (c++/51786). */
15452 decl_specifiers.type = NULL_TREE;
15453 shadow_tag (&decl_specifiers);
15455 /* Perform any deferred access checks. */
15456 perform_deferred_access_checks (tf_warning_or_error);
15459 /* Consume the `;'. */
15460 finish:
15461 if (!maybe_range_for_decl)
15462 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15463 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15465 if (init_loc != UNKNOWN_LOCATION)
15466 error_at (init_loc, "initializer in range-based %<for%> loop");
15467 if (comma_loc != UNKNOWN_LOCATION)
15468 error_at (comma_loc,
15469 "multiple declarations in range-based %<for%> loop");
15472 done:
15473 pop_deferring_access_checks ();
15474 cp_finalize_omp_declare_simd (parser, &odsd);
15477 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15478 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15479 initializer ; */
15481 static tree
15482 cp_parser_decomposition_declaration (cp_parser *parser,
15483 cp_decl_specifier_seq *decl_specifiers,
15484 tree *maybe_range_for_decl,
15485 location_t *init_loc)
15487 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15488 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15489 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15491 /* Parse the identifier-list. */
15492 auto_vec<cp_expr, 10> v;
15493 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15494 while (true)
15496 cp_expr e = cp_parser_identifier (parser);
15497 if (e.get_value () == error_mark_node)
15498 break;
15499 v.safe_push (e);
15500 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15501 break;
15502 cp_lexer_consume_token (parser->lexer);
15505 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
15506 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15508 end_loc = UNKNOWN_LOCATION;
15509 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
15510 false);
15511 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15512 cp_lexer_consume_token (parser->lexer);
15513 else
15515 cp_parser_skip_to_end_of_statement (parser);
15516 return error_mark_node;
15520 if (cxx_dialect < cxx17)
15521 pedwarn (loc, OPT_Wc__17_extensions,
15522 "structured bindings only available with "
15523 "%<-std=c++17%> or %<-std=gnu++17%>");
15525 tree pushed_scope;
15526 cp_declarator *declarator = make_declarator (cdk_decomp);
15527 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
15528 declarator->id_loc = loc;
15529 if (ref_qual != REF_QUAL_NONE)
15530 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
15531 ref_qual == REF_QUAL_RVALUE,
15532 NULL_TREE);
15533 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
15534 NULL_TREE, decl_specifiers->attributes,
15535 &pushed_scope);
15536 tree orig_decl = decl;
15538 unsigned int i;
15539 cp_expr e;
15540 cp_decl_specifier_seq decl_specs;
15541 clear_decl_specs (&decl_specs);
15542 decl_specs.type = make_auto ();
15543 tree prev = decl;
15544 FOR_EACH_VEC_ELT (v, i, e)
15546 if (i == 0)
15547 declarator = make_id_declarator (NULL_TREE, e.get_value (),
15548 sfk_none, e.get_location ());
15549 else
15551 declarator->u.id.unqualified_name = e.get_value ();
15552 declarator->id_loc = e.get_location ();
15554 tree elt_pushed_scope;
15555 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
15556 NULL_TREE, NULL_TREE, &elt_pushed_scope);
15557 if (decl2 == error_mark_node)
15558 decl = error_mark_node;
15559 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
15561 /* Ensure we've diagnosed redeclaration if we aren't creating
15562 a new VAR_DECL. */
15563 gcc_assert (errorcount);
15564 decl = error_mark_node;
15566 else
15567 prev = decl2;
15568 if (elt_pushed_scope)
15569 pop_scope (elt_pushed_scope);
15572 if (v.is_empty ())
15574 error_at (loc, "empty structured binding declaration");
15575 decl = error_mark_node;
15578 if (maybe_range_for_decl == NULL
15579 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15581 bool non_constant_p = false, is_direct_init = false;
15582 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
15583 tree initializer = cp_parser_initializer (parser, &is_direct_init,
15584 &non_constant_p);
15585 if (initializer == NULL_TREE
15586 || (TREE_CODE (initializer) == TREE_LIST
15587 && TREE_CHAIN (initializer))
15588 || (is_direct_init
15589 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
15590 && CONSTRUCTOR_NELTS (initializer) != 1))
15592 error_at (loc, "invalid initializer for structured binding "
15593 "declaration");
15594 initializer = error_mark_node;
15597 if (decl != error_mark_node)
15599 cp_maybe_mangle_decomp (decl, prev, v.length ());
15600 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
15601 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15602 cp_finish_decomp (decl, prev, v.length ());
15605 else if (decl != error_mark_node)
15607 *maybe_range_for_decl = prev;
15608 /* Ensure DECL_VALUE_EXPR is created for all the decls but
15609 the underlying DECL. */
15610 cp_finish_decomp (decl, prev, v.length ());
15613 if (pushed_scope)
15614 pop_scope (pushed_scope);
15616 if (decl == error_mark_node && DECL_P (orig_decl))
15618 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
15619 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
15622 return decl;
15625 /* Parse a decl-specifier-seq.
15627 decl-specifier-seq:
15628 decl-specifier-seq [opt] decl-specifier
15629 decl-specifier attribute-specifier-seq [opt] (C++11)
15631 decl-specifier:
15632 storage-class-specifier
15633 type-specifier
15634 function-specifier
15635 friend
15636 typedef
15638 GNU Extension:
15640 decl-specifier:
15641 attributes
15643 Concepts Extension:
15645 decl-specifier:
15646 concept
15648 Set *DECL_SPECS to a representation of the decl-specifier-seq.
15650 The parser flags FLAGS is used to control type-specifier parsing.
15652 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15653 flags:
15655 1: one of the decl-specifiers is an elaborated-type-specifier
15656 (i.e., a type declaration)
15657 2: one of the decl-specifiers is an enum-specifier or a
15658 class-specifier (i.e., a type definition)
15662 static void
15663 cp_parser_decl_specifier_seq (cp_parser* parser,
15664 cp_parser_flags flags,
15665 cp_decl_specifier_seq *decl_specs,
15666 int* declares_class_or_enum)
15668 bool constructor_possible_p = !parser->in_declarator_p;
15669 bool found_decl_spec = false;
15670 cp_token *start_token = NULL;
15671 cp_decl_spec ds;
15673 /* Clear DECL_SPECS. */
15674 clear_decl_specs (decl_specs);
15676 /* Assume no class or enumeration type is declared. */
15677 *declares_class_or_enum = 0;
15679 /* Keep reading specifiers until there are no more to read. */
15680 while (true)
15682 bool constructor_p;
15683 cp_token *token;
15684 ds = ds_last;
15686 /* Peek at the next token. */
15687 token = cp_lexer_peek_token (parser->lexer);
15689 /* Save the first token of the decl spec list for error
15690 reporting. */
15691 if (!start_token)
15692 start_token = token;
15693 /* Handle attributes. */
15694 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
15695 && cp_next_tokens_can_be_attribute_p (parser))
15697 /* Parse the attributes. */
15698 tree attrs = cp_parser_attributes_opt (parser);
15700 /* In a sequence of declaration specifiers, c++11 attributes
15701 appertain to the type that precede them. In that case
15702 [dcl.spec]/1 says:
15704 The attribute-specifier-seq affects the type only for
15705 the declaration it appears in, not other declarations
15706 involving the same type.
15708 But for now let's force the user to position the
15709 attribute either at the beginning of the declaration or
15710 after the declarator-id, which would clearly mean that it
15711 applies to the declarator. */
15712 if (cxx11_attribute_p (attrs))
15714 if (!found_decl_spec)
15715 /* The c++11 attribute is at the beginning of the
15716 declaration. It appertains to the entity being
15717 declared. */;
15718 else
15720 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
15722 /* This is an attribute following a
15723 class-specifier. */
15724 if (decl_specs->type_definition_p)
15725 warn_misplaced_attr_for_class_type (token->location,
15726 decl_specs->type);
15727 attrs = NULL_TREE;
15729 else
15731 decl_specs->std_attributes
15732 = attr_chainon (decl_specs->std_attributes, attrs);
15733 if (decl_specs->locations[ds_std_attribute] == 0)
15734 decl_specs->locations[ds_std_attribute] = token->location;
15736 continue;
15740 decl_specs->attributes
15741 = attr_chainon (decl_specs->attributes, attrs);
15742 if (decl_specs->locations[ds_attribute] == 0)
15743 decl_specs->locations[ds_attribute] = token->location;
15744 continue;
15746 /* Assume we will find a decl-specifier keyword. */
15747 found_decl_spec = true;
15748 /* If the next token is an appropriate keyword, we can simply
15749 add it to the list. */
15750 switch (token->keyword)
15752 /* decl-specifier:
15753 friend
15754 constexpr
15755 constinit */
15756 case RID_FRIEND:
15757 if (!at_class_scope_p ())
15759 gcc_rich_location richloc (token->location);
15760 richloc.add_fixit_remove ();
15761 error_at (&richloc, "%<friend%> used outside of class");
15762 cp_lexer_purge_token (parser->lexer);
15764 else
15766 ds = ds_friend;
15767 /* Consume the token. */
15768 cp_lexer_consume_token (parser->lexer);
15770 break;
15772 case RID_CONSTEXPR:
15773 ds = ds_constexpr;
15774 cp_lexer_consume_token (parser->lexer);
15775 break;
15777 case RID_CONSTINIT:
15778 ds = ds_constinit;
15779 cp_lexer_consume_token (parser->lexer);
15780 break;
15782 case RID_CONSTEVAL:
15783 ds = ds_consteval;
15784 cp_lexer_consume_token (parser->lexer);
15785 break;
15787 case RID_CONCEPT:
15788 ds = ds_concept;
15789 cp_lexer_consume_token (parser->lexer);
15791 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15792 break;
15794 /* Warn for concept as a decl-specifier. We'll rewrite these as
15795 concept declarations later. */
15796 if (!flag_concepts_ts)
15798 cp_token *next = cp_lexer_peek_token (parser->lexer);
15799 if (next->keyword == RID_BOOL)
15800 pedwarn (next->location, 0, "the %<bool%> keyword is not "
15801 "allowed in a C++20 concept definition");
15802 else
15803 pedwarn (token->location, 0, "C++20 concept definition syntax "
15804 "is %<concept <name> = <expr>%>");
15807 /* In C++20 a concept definition is just 'concept name = expr;'
15808 Support that syntax as a TS extension by pretending we've seen
15809 the 'bool' specifier. */
15810 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15811 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
15812 && !decl_specs->any_type_specifiers_p)
15814 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
15815 token, /*type_definition*/false);
15816 decl_specs->any_type_specifiers_p = true;
15818 break;
15820 /* function-specifier:
15821 inline
15822 virtual
15823 explicit */
15824 case RID_INLINE:
15825 case RID_VIRTUAL:
15826 case RID_EXPLICIT:
15827 cp_parser_function_specifier_opt (parser, decl_specs);
15828 break;
15830 /* decl-specifier:
15831 typedef */
15832 case RID_TYPEDEF:
15833 ds = ds_typedef;
15834 /* Consume the token. */
15835 cp_lexer_consume_token (parser->lexer);
15837 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15838 break;
15840 /* A constructor declarator cannot appear in a typedef. */
15841 constructor_possible_p = false;
15842 /* The "typedef" keyword can only occur in a declaration; we
15843 may as well commit at this point. */
15844 cp_parser_commit_to_tentative_parse (parser);
15846 if (decl_specs->storage_class != sc_none)
15847 decl_specs->conflicting_specifiers_p = true;
15848 break;
15850 /* storage-class-specifier:
15851 auto
15852 register
15853 static
15854 extern
15855 mutable
15857 GNU Extension:
15858 thread */
15859 case RID_AUTO:
15860 if (cxx_dialect == cxx98)
15862 /* Consume the token. */
15863 cp_lexer_consume_token (parser->lexer);
15865 /* Complain about `auto' as a storage specifier, if
15866 we're complaining about C++0x compatibility. */
15867 gcc_rich_location richloc (token->location);
15868 richloc.add_fixit_remove ();
15869 warning_at (&richloc, OPT_Wc__11_compat,
15870 "%<auto%> changes meaning in C++11; "
15871 "please remove it");
15873 /* Set the storage class anyway. */
15874 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
15875 token);
15877 else
15878 /* C++0x auto type-specifier. */
15879 found_decl_spec = false;
15880 break;
15882 case RID_REGISTER:
15883 case RID_STATIC:
15884 case RID_EXTERN:
15885 case RID_MUTABLE:
15886 /* Consume the token. */
15887 cp_lexer_consume_token (parser->lexer);
15888 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
15889 token);
15890 break;
15891 case RID_THREAD:
15892 /* Consume the token. */
15893 ds = ds_thread;
15894 cp_lexer_consume_token (parser->lexer);
15895 break;
15897 default:
15898 /* We did not yet find a decl-specifier yet. */
15899 found_decl_spec = false;
15900 break;
15903 if (found_decl_spec
15904 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
15905 && token->keyword != RID_CONSTEXPR)
15906 error ("%qD invalid in condition", ridpointers[token->keyword]);
15908 if (found_decl_spec
15909 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15910 && token->keyword != RID_MUTABLE
15911 && token->keyword != RID_CONSTEXPR
15912 && token->keyword != RID_CONSTEVAL)
15913 error_at (token->location, "%qD invalid in lambda",
15914 ridpointers[token->keyword]);
15916 if (ds != ds_last)
15917 set_and_check_decl_spec_loc (decl_specs, ds, token);
15919 /* Constructors are a special case. The `S' in `S()' is not a
15920 decl-specifier; it is the beginning of the declarator. */
15921 constructor_p
15922 = (!found_decl_spec
15923 && constructor_possible_p
15924 && (cp_parser_constructor_declarator_p
15925 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
15926 ds_friend))));
15928 /* If we don't have a DECL_SPEC yet, then we must be looking at
15929 a type-specifier. */
15930 if (!found_decl_spec && !constructor_p)
15932 int decl_spec_declares_class_or_enum;
15933 bool is_cv_qualifier;
15934 tree type_spec;
15936 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15937 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15939 type_spec
15940 = cp_parser_type_specifier (parser, flags,
15941 decl_specs,
15942 /*is_declaration=*/true,
15943 &decl_spec_declares_class_or_enum,
15944 &is_cv_qualifier);
15945 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
15947 /* If this type-specifier referenced a user-defined type
15948 (a typedef, class-name, etc.), then we can't allow any
15949 more such type-specifiers henceforth.
15951 [dcl.spec]
15953 The longest sequence of decl-specifiers that could
15954 possibly be a type name is taken as the
15955 decl-specifier-seq of a declaration. The sequence shall
15956 be self-consistent as described below.
15958 [dcl.type]
15960 As a general rule, at most one type-specifier is allowed
15961 in the complete decl-specifier-seq of a declaration. The
15962 only exceptions are the following:
15964 -- const or volatile can be combined with any other
15965 type-specifier.
15967 -- signed or unsigned can be combined with char, long,
15968 short, or int.
15970 -- ..
15972 Example:
15974 typedef char* Pc;
15975 void g (const int Pc);
15977 Here, Pc is *not* part of the decl-specifier seq; it's
15978 the declarator. Therefore, once we see a type-specifier
15979 (other than a cv-qualifier), we forbid any additional
15980 user-defined types. We *do* still allow things like `int
15981 int' to be considered a decl-specifier-seq, and issue the
15982 error message later. */
15983 if (type_spec && !is_cv_qualifier)
15984 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15985 /* A constructor declarator cannot follow a type-specifier. */
15986 if (type_spec)
15988 constructor_possible_p = false;
15989 found_decl_spec = true;
15990 if (!is_cv_qualifier)
15991 decl_specs->any_type_specifiers_p = true;
15993 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
15994 error_at (token->location, "type-specifier invalid in lambda");
15998 /* If we still do not have a DECL_SPEC, then there are no more
15999 decl-specifiers. */
16000 if (!found_decl_spec)
16001 break;
16003 if (decl_specs->std_attributes)
16005 error_at (decl_specs->locations[ds_std_attribute],
16006 "standard attributes in middle of decl-specifiers");
16007 inform (decl_specs->locations[ds_std_attribute],
16008 "standard attributes must precede the decl-specifiers to "
16009 "apply to the declaration, or follow them to apply to "
16010 "the type");
16013 decl_specs->any_specifiers_p = true;
16014 /* After we see one decl-specifier, further decl-specifiers are
16015 always optional. */
16016 flags |= CP_PARSER_FLAGS_OPTIONAL;
16019 /* Don't allow a friend specifier with a class definition. */
16020 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16021 && (*declares_class_or_enum & 2))
16022 error_at (decl_specs->locations[ds_friend],
16023 "class definition may not be declared a friend");
16026 /* Parse an (optional) storage-class-specifier.
16028 storage-class-specifier:
16029 auto
16030 register
16031 static
16032 extern
16033 mutable
16035 GNU Extension:
16037 storage-class-specifier:
16038 thread
16040 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16042 static tree
16043 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16045 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16047 case RID_AUTO:
16048 if (cxx_dialect != cxx98)
16049 return NULL_TREE;
16050 /* Fall through for C++98. */
16051 gcc_fallthrough ();
16053 case RID_REGISTER:
16054 case RID_STATIC:
16055 case RID_EXTERN:
16056 case RID_MUTABLE:
16057 case RID_THREAD:
16058 /* Consume the token. */
16059 return cp_lexer_consume_token (parser->lexer)->u.value;
16061 default:
16062 return NULL_TREE;
16066 /* Parse an (optional) function-specifier.
16068 function-specifier:
16069 inline
16070 virtual
16071 explicit
16073 C++20 Extension:
16074 explicit(constant-expression)
16076 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16077 Updates DECL_SPECS, if it is non-NULL. */
16079 static tree
16080 cp_parser_function_specifier_opt (cp_parser* parser,
16081 cp_decl_specifier_seq *decl_specs)
16083 cp_token *token = cp_lexer_peek_token (parser->lexer);
16084 switch (token->keyword)
16086 case RID_INLINE:
16087 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16088 break;
16090 case RID_VIRTUAL:
16091 /* 14.5.2.3 [temp.mem]
16093 A member function template shall not be virtual. */
16094 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16095 && current_class_type)
16096 error_at (token->location, "templates may not be %<virtual%>");
16097 else
16098 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16099 break;
16101 case RID_EXPLICIT:
16103 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16104 /* If we see '(', it's C++20 explicit(bool). */
16105 tree expr;
16106 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16108 matching_parens parens;
16109 parens.consume_open (parser);
16111 /* New types are not allowed in an explicit-specifier. */
16112 const char *saved_message
16113 = parser->type_definition_forbidden_message;
16114 parser->type_definition_forbidden_message
16115 = G_("types may not be defined in explicit-specifier");
16117 if (cxx_dialect < cxx20)
16118 pedwarn (token->location, OPT_Wc__20_extensions,
16119 "%<explicit(bool)%> only available with %<-std=c++20%> "
16120 "or %<-std=gnu++20%>");
16122 /* Parse the constant-expression. */
16123 expr = cp_parser_constant_expression (parser);
16125 /* Restore the saved message. */
16126 parser->type_definition_forbidden_message = saved_message;
16127 parens.require_close (parser);
16129 else
16130 /* The explicit-specifier explicit without a constant-expression is
16131 equivalent to the explicit-specifier explicit(true). */
16132 expr = boolean_true_node;
16134 /* [dcl.fct.spec]
16135 "the constant-expression, if supplied, shall be a contextually
16136 converted constant expression of type bool." */
16137 expr = build_explicit_specifier (expr, tf_warning_or_error);
16138 /* We could evaluate it -- mark the decl as appropriate. */
16139 if (expr == boolean_true_node)
16140 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16141 else if (expr == boolean_false_node)
16142 /* Don't mark the decl as explicit. */;
16143 else if (decl_specs)
16144 /* The expression was value-dependent. Remember it so that we can
16145 substitute it later. */
16146 decl_specs->explicit_specifier = expr;
16147 return id;
16150 default:
16151 return NULL_TREE;
16154 /* Consume the token. */
16155 return cp_lexer_consume_token (parser->lexer)->u.value;
16158 /* Parse a linkage-specification.
16160 linkage-specification:
16161 extern string-literal { declaration-seq [opt] }
16162 extern string-literal declaration */
16164 static void
16165 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16167 tree linkage;
16169 /* Look for the `extern' keyword. */
16170 cp_token *extern_token
16171 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16173 /* Look for the string-literal. */
16174 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16175 linkage = cp_parser_string_literal (parser, false, false);
16177 /* Transform the literal into an identifier. If the literal is a
16178 wide-character string, or contains embedded NULs, then we can't
16179 handle it as the user wants. */
16180 if (linkage == error_mark_node
16181 || strlen (TREE_STRING_POINTER (linkage))
16182 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16184 cp_parser_error (parser, "invalid linkage-specification");
16185 /* Assume C++ linkage. */
16186 linkage = lang_name_cplusplus;
16188 else
16189 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16191 /* We're now using the new linkage. */
16192 unsigned saved_module = module_kind;
16193 module_kind &= ~MK_ATTACH;
16194 push_lang_context (linkage);
16196 /* Preserve the location of the innermost linkage specification,
16197 tracking the locations of nested specifications via a local. */
16198 location_t saved_location
16199 = parser->innermost_linkage_specification_location;
16200 /* Construct a location ranging from the start of the "extern" to
16201 the end of the string-literal, with the caret at the start, e.g.:
16202 extern "C" {
16203 ^~~~~~~~~~
16205 parser->innermost_linkage_specification_location
16206 = make_location (extern_token->location,
16207 extern_token->location,
16208 get_finish (string_token->location));
16210 /* If the next token is a `{', then we're using the first
16211 production. */
16212 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16214 cp_ensure_no_omp_declare_simd (parser);
16215 cp_ensure_no_oacc_routine (parser);
16217 /* Consume the `{' token. */
16218 matching_braces braces;
16219 braces.consume_open (parser);
16220 /* Parse the declarations. */
16221 cp_parser_declaration_seq_opt (parser);
16222 /* Look for the closing `}'. */
16223 braces.require_close (parser);
16225 /* Otherwise, there's just one declaration. */
16226 else
16228 bool saved_in_unbraced_linkage_specification_p;
16230 saved_in_unbraced_linkage_specification_p
16231 = parser->in_unbraced_linkage_specification_p;
16232 parser->in_unbraced_linkage_specification_p = true;
16233 cp_parser_declaration (parser, prefix_attr);
16234 parser->in_unbraced_linkage_specification_p
16235 = saved_in_unbraced_linkage_specification_p;
16238 /* We're done with the linkage-specification. */
16239 pop_lang_context ();
16240 module_kind = saved_module;
16242 /* Restore location of parent linkage specification, if any. */
16243 parser->innermost_linkage_specification_location = saved_location;
16246 /* Parse a static_assert-declaration.
16248 static_assert-declaration:
16249 static_assert ( constant-expression , string-literal ) ;
16250 static_assert ( constant-expression ) ; (C++17)
16252 If MEMBER_P, this static_assert is a class member. */
16254 static void
16255 cp_parser_static_assert(cp_parser *parser, bool member_p)
16257 cp_expr condition;
16258 location_t token_loc;
16259 tree message;
16260 bool dummy;
16262 /* Peek at the `static_assert' token so we can keep track of exactly
16263 where the static assertion started. */
16264 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16266 /* Look for the `static_assert' keyword. */
16267 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16268 RT_STATIC_ASSERT))
16269 return;
16271 /* We know we are in a static assertion; commit to any tentative
16272 parse. */
16273 if (cp_parser_parsing_tentatively (parser))
16274 cp_parser_commit_to_tentative_parse (parser);
16276 /* Parse the `(' starting the static assertion condition. */
16277 matching_parens parens;
16278 parens.require_open (parser);
16280 /* Parse the constant-expression. Allow a non-constant expression
16281 here in order to give better diagnostics in finish_static_assert. */
16282 condition =
16283 cp_parser_constant_expression (parser,
16284 /*allow_non_constant_p=*/true,
16285 /*non_constant_p=*/&dummy);
16287 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16289 if (pedantic && cxx_dialect < cxx17)
16290 pedwarn (input_location, OPT_Wc__17_extensions,
16291 "%<static_assert%> without a message "
16292 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16293 /* Eat the ')' */
16294 cp_lexer_consume_token (parser->lexer);
16295 message = build_string (1, "");
16296 TREE_TYPE (message) = char_array_type_node;
16297 fix_string_type (message);
16299 else
16301 /* Parse the separating `,'. */
16302 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16304 /* Parse the string-literal message. */
16305 message = cp_parser_string_literal (parser,
16306 /*translate=*/false,
16307 /*wide_ok=*/true);
16309 /* A `)' completes the static assertion. */
16310 if (!parens.require_close (parser))
16311 cp_parser_skip_to_closing_parenthesis (parser,
16312 /*recovering=*/true,
16313 /*or_comma=*/false,
16314 /*consume_paren=*/true);
16317 /* A semicolon terminates the declaration. */
16318 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16320 /* Get the location for the static assertion. Use that of the
16321 condition if available, otherwise, use that of the "static_assert"
16322 token. */
16323 location_t assert_loc = condition.get_location ();
16324 if (assert_loc == UNKNOWN_LOCATION)
16325 assert_loc = token_loc;
16327 /* Complete the static assertion, which may mean either processing
16328 the static assert now or saving it for template instantiation. */
16329 finish_static_assert (condition, message, assert_loc, member_p,
16330 /*show_expr_p=*/false);
16333 /* Parse the expression in decltype ( expression ). */
16335 static tree
16336 cp_parser_decltype_expr (cp_parser *parser,
16337 bool &id_expression_or_member_access_p)
16339 cp_token *id_expr_start_token;
16340 tree expr;
16342 /* First, try parsing an id-expression. */
16343 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16344 cp_parser_parse_tentatively (parser);
16345 expr = cp_parser_id_expression (parser,
16346 /*template_keyword_p=*/false,
16347 /*check_dependency_p=*/true,
16348 /*template_p=*/NULL,
16349 /*declarator_p=*/false,
16350 /*optional_p=*/false);
16352 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16354 bool non_integral_constant_expression_p = false;
16355 tree id_expression = expr;
16356 cp_id_kind idk;
16357 const char *error_msg;
16359 if (identifier_p (expr))
16360 /* Lookup the name we got back from the id-expression. */
16361 expr = cp_parser_lookup_name_simple (parser, expr,
16362 id_expr_start_token->location);
16364 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
16365 /* A template without args is not a complete id-expression. */
16366 expr = error_mark_node;
16368 if (expr
16369 && expr != error_mark_node
16370 && TREE_CODE (expr) != TYPE_DECL
16371 && (TREE_CODE (expr) != BIT_NOT_EXPR
16372 || !TYPE_P (TREE_OPERAND (expr, 0)))
16373 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16375 /* Complete lookup of the id-expression. */
16376 expr = (finish_id_expression
16377 (id_expression, expr, parser->scope, &idk,
16378 /*integral_constant_expression_p=*/false,
16379 /*allow_non_integral_constant_expression_p=*/true,
16380 &non_integral_constant_expression_p,
16381 /*template_p=*/false,
16382 /*done=*/true,
16383 /*address_p=*/false,
16384 /*template_arg_p=*/false,
16385 &error_msg,
16386 id_expr_start_token->location));
16388 if (expr == error_mark_node)
16389 /* We found an id-expression, but it was something that we
16390 should not have found. This is an error, not something
16391 we can recover from, so note that we found an
16392 id-expression and we'll recover as gracefully as
16393 possible. */
16394 id_expression_or_member_access_p = true;
16397 if (expr
16398 && expr != error_mark_node
16399 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16400 /* We have an id-expression. */
16401 id_expression_or_member_access_p = true;
16404 if (!id_expression_or_member_access_p)
16406 /* Abort the id-expression parse. */
16407 cp_parser_abort_tentative_parse (parser);
16409 /* Parsing tentatively, again. */
16410 cp_parser_parse_tentatively (parser);
16412 /* Parse a class member access. */
16413 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16414 /*cast_p=*/false, /*decltype*/true,
16415 /*member_access_only_p=*/true, NULL);
16417 if (expr
16418 && expr != error_mark_node
16419 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16420 /* We have an id-expression. */
16421 id_expression_or_member_access_p = true;
16424 if (id_expression_or_member_access_p)
16425 /* We have parsed the complete id-expression or member access. */
16426 cp_parser_parse_definitely (parser);
16427 else
16429 /* Abort our attempt to parse an id-expression or member access
16430 expression. */
16431 cp_parser_abort_tentative_parse (parser);
16433 /* Parse a full expression. */
16434 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
16435 /*decltype_p=*/true);
16438 return expr;
16441 /* Parse a `decltype' type. Returns the type.
16443 decltype-specifier:
16444 decltype ( expression )
16445 C++14:
16446 decltype ( auto ) */
16448 static tree
16449 cp_parser_decltype (cp_parser *parser)
16451 bool id_expression_or_member_access_p = false;
16452 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
16454 if (start_token->type == CPP_DECLTYPE)
16456 /* Already parsed. */
16457 cp_lexer_consume_token (parser->lexer);
16458 return saved_checks_value (start_token->u.tree_check_value);
16461 /* Look for the `decltype' token. */
16462 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
16463 return error_mark_node;
16465 /* Parse the opening `('. */
16466 matching_parens parens;
16467 if (!parens.require_open (parser))
16468 return error_mark_node;
16470 /* Since we're going to preserve any side-effects from this parse, set up a
16471 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16472 in the expression. */
16473 tentative_firewall firewall (parser);
16475 /* If in_declarator_p, a reparse as an expression might succeed (60361).
16476 Otherwise, commit now for better diagnostics. */
16477 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
16478 && !parser->in_declarator_p)
16479 cp_parser_commit_to_topmost_tentative_parse (parser);
16481 push_deferring_access_checks (dk_deferred);
16483 tree expr = NULL_TREE;
16485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
16486 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
16488 /* decltype (auto) */
16489 cp_lexer_consume_token (parser->lexer);
16490 if (cxx_dialect < cxx14)
16492 error_at (start_token->location,
16493 "%<decltype(auto)%> type specifier only available with "
16494 "%<-std=c++14%> or %<-std=gnu++14%>");
16495 expr = error_mark_node;
16498 else
16500 /* decltype (expression) */
16502 /* Types cannot be defined in a `decltype' expression. Save away the
16503 old message and set the new one. */
16504 const char *saved_message = parser->type_definition_forbidden_message;
16505 parser->type_definition_forbidden_message
16506 = G_("types may not be defined in %<decltype%> expressions");
16508 /* The restrictions on constant-expressions do not apply inside
16509 decltype expressions. */
16510 bool saved_integral_constant_expression_p
16511 = parser->integral_constant_expression_p;
16512 bool saved_non_integral_constant_expression_p
16513 = parser->non_integral_constant_expression_p;
16514 parser->integral_constant_expression_p = false;
16516 /* Within a parenthesized expression, a `>' token is always
16517 the greater-than operator. */
16518 bool saved_greater_than_is_operator_p
16519 = parser->greater_than_is_operator_p;
16520 parser->greater_than_is_operator_p = true;
16522 /* Don't synthesize an implicit template type parameter here. This
16523 could happen with C++23 code like
16525 void f(decltype(new auto{0}));
16527 where we want to deduce the auto right away so that the parameter
16528 is of type 'int *'. */
16529 auto cleanup = make_temp_override
16530 (parser->auto_is_implicit_function_template_parm_p, false);
16532 /* Do not actually evaluate the expression. */
16533 ++cp_unevaluated_operand;
16535 /* Do not warn about problems with the expression. */
16536 ++c_inhibit_evaluation_warnings;
16538 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
16539 STRIP_ANY_LOCATION_WRAPPER (expr);
16541 /* Go back to evaluating expressions. */
16542 --cp_unevaluated_operand;
16543 --c_inhibit_evaluation_warnings;
16545 /* The `>' token might be the end of a template-id or
16546 template-parameter-list now. */
16547 parser->greater_than_is_operator_p
16548 = saved_greater_than_is_operator_p;
16550 /* Restore the old message and the integral constant expression
16551 flags. */
16552 parser->type_definition_forbidden_message = saved_message;
16553 parser->integral_constant_expression_p
16554 = saved_integral_constant_expression_p;
16555 parser->non_integral_constant_expression_p
16556 = saved_non_integral_constant_expression_p;
16559 /* Parse to the closing `)'. */
16560 if (expr == error_mark_node || !parens.require_close (parser))
16562 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16563 /*consume_paren=*/true);
16564 expr = error_mark_node;
16567 /* If we got a parse error while tentative, bail out now. */
16568 if (cp_parser_error_occurred (parser))
16570 pop_deferring_access_checks ();
16571 return error_mark_node;
16574 if (!expr)
16575 /* Build auto. */
16576 expr = make_decltype_auto ();
16577 else
16578 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
16579 tf_warning_or_error);
16581 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16582 it again. */
16583 start_token->type = CPP_DECLTYPE;
16584 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16585 start_token->tree_check_p = true;
16586 start_token->u.tree_check_value->value = expr;
16587 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
16588 start_token->keyword = RID_MAX;
16590 location_t loc = start_token->location;
16591 loc = make_location (loc, loc, parser->lexer);
16592 start_token->location = loc;
16594 cp_lexer_purge_tokens_after (parser->lexer, start_token);
16596 pop_to_parent_deferring_access_checks ();
16598 return expr;
16601 /* Special member functions [gram.special] */
16603 /* Parse a conversion-function-id.
16605 conversion-function-id:
16606 operator conversion-type-id
16608 Returns an IDENTIFIER_NODE representing the operator. */
16610 static tree
16611 cp_parser_conversion_function_id (cp_parser* parser)
16613 tree type;
16614 tree saved_scope;
16615 tree saved_qualifying_scope;
16616 tree saved_object_scope;
16617 tree pushed_scope = NULL_TREE;
16619 /* Look for the `operator' token. */
16620 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16621 return error_mark_node;
16622 /* When we parse the conversion-type-id, the current scope will be
16623 reset. However, we need that information in able to look up the
16624 conversion function later, so we save it here. */
16625 saved_scope = parser->scope;
16626 saved_qualifying_scope = parser->qualifying_scope;
16627 saved_object_scope = parser->object_scope;
16628 /* We must enter the scope of the class so that the names of
16629 entities declared within the class are available in the
16630 conversion-type-id. For example, consider:
16632 struct S {
16633 typedef int I;
16634 operator I();
16637 S::operator I() { ... }
16639 In order to see that `I' is a type-name in the definition, we
16640 must be in the scope of `S'. */
16641 if (saved_scope)
16642 pushed_scope = push_scope (saved_scope);
16643 /* Parse the conversion-type-id. */
16644 type = cp_parser_conversion_type_id (parser);
16645 /* Leave the scope of the class, if any. */
16646 if (pushed_scope)
16647 pop_scope (pushed_scope);
16648 /* Restore the saved scope. */
16649 parser->scope = saved_scope;
16650 parser->qualifying_scope = saved_qualifying_scope;
16651 parser->object_scope = saved_object_scope;
16652 /* If the TYPE is invalid, indicate failure. */
16653 if (type == error_mark_node)
16654 return error_mark_node;
16655 return make_conv_op_name (type);
16658 /* Parse a conversion-type-id:
16660 conversion-type-id:
16661 type-specifier-seq conversion-declarator [opt]
16663 Returns the TYPE specified. */
16665 static tree
16666 cp_parser_conversion_type_id (cp_parser* parser)
16668 tree attributes;
16669 cp_decl_specifier_seq type_specifiers;
16670 cp_declarator *declarator;
16671 tree type_specified;
16672 const char *saved_message;
16674 /* Parse the attributes. */
16675 attributes = cp_parser_attributes_opt (parser);
16677 saved_message = parser->type_definition_forbidden_message;
16678 parser->type_definition_forbidden_message
16679 = G_("types may not be defined in a conversion-type-id");
16681 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
16682 optional in conversion-type-id. */
16683 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16684 /*is_declaration=*/false,
16685 /*is_trailing_return=*/false,
16686 &type_specifiers);
16688 parser->type_definition_forbidden_message = saved_message;
16690 /* If that didn't work, stop. */
16691 if (type_specifiers.type == error_mark_node)
16692 return error_mark_node;
16693 /* Parse the conversion-declarator. */
16694 declarator = cp_parser_conversion_declarator_opt (parser);
16696 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
16697 /*initialized=*/0, &attributes);
16698 if (attributes)
16699 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
16701 /* Don't give this error when parsing tentatively. This happens to
16702 work because we always parse this definitively once. */
16703 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
16704 && type_uses_auto (type_specified))
16706 if (cxx_dialect < cxx14)
16708 error ("invalid use of %<auto%> in conversion operator");
16709 return error_mark_node;
16711 else if (template_parm_scope_p ())
16712 warning (0, "use of %<auto%> in member template "
16713 "conversion operator can never be deduced");
16716 return type_specified;
16719 /* Parse an (optional) conversion-declarator.
16721 conversion-declarator:
16722 ptr-operator conversion-declarator [opt]
16726 static cp_declarator *
16727 cp_parser_conversion_declarator_opt (cp_parser* parser)
16729 enum tree_code code;
16730 tree class_type, std_attributes = NULL_TREE;
16731 cp_cv_quals cv_quals;
16733 /* We don't know if there's a ptr-operator next, or not. */
16734 cp_parser_parse_tentatively (parser);
16735 /* Try the ptr-operator. */
16736 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
16737 &std_attributes);
16738 /* If it worked, look for more conversion-declarators. */
16739 if (cp_parser_parse_definitely (parser))
16741 cp_declarator *declarator;
16743 /* Parse another optional declarator. */
16744 declarator = cp_parser_conversion_declarator_opt (parser);
16746 declarator = cp_parser_make_indirect_declarator
16747 (code, class_type, cv_quals, declarator, std_attributes);
16749 return declarator;
16752 return NULL;
16755 /* Parse an (optional) ctor-initializer.
16757 ctor-initializer:
16758 : mem-initializer-list */
16760 static void
16761 cp_parser_ctor_initializer_opt (cp_parser* parser)
16763 /* If the next token is not a `:', then there is no
16764 ctor-initializer. */
16765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16767 /* Do default initialization of any bases and members. */
16768 if (DECL_CONSTRUCTOR_P (current_function_decl))
16769 finish_mem_initializers (NULL_TREE);
16770 return;
16773 /* Consume the `:' token. */
16774 cp_lexer_consume_token (parser->lexer);
16775 /* And the mem-initializer-list. */
16776 cp_parser_mem_initializer_list (parser);
16779 /* Parse a mem-initializer-list.
16781 mem-initializer-list:
16782 mem-initializer ... [opt]
16783 mem-initializer ... [opt] , mem-initializer-list */
16785 static void
16786 cp_parser_mem_initializer_list (cp_parser* parser)
16788 tree mem_initializer_list = NULL_TREE;
16789 tree target_ctor = error_mark_node;
16790 cp_token *token = cp_lexer_peek_token (parser->lexer);
16792 /* Let the semantic analysis code know that we are starting the
16793 mem-initializer-list. */
16794 if (!DECL_CONSTRUCTOR_P (current_function_decl))
16795 error_at (token->location,
16796 "only constructors take member initializers");
16798 /* Loop through the list. */
16799 while (true)
16801 tree mem_initializer;
16803 token = cp_lexer_peek_token (parser->lexer);
16804 /* Parse the mem-initializer. */
16805 mem_initializer = cp_parser_mem_initializer (parser);
16806 /* If the next token is a `...', we're expanding member initializers. */
16807 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16808 if (ellipsis
16809 || (mem_initializer != error_mark_node
16810 && check_for_bare_parameter_packs (TREE_PURPOSE
16811 (mem_initializer))))
16813 /* Consume the `...'. */
16814 if (ellipsis)
16815 cp_lexer_consume_token (parser->lexer);
16817 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
16818 can be expanded but members cannot. */
16819 if (mem_initializer != error_mark_node
16820 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
16822 error_at (token->location,
16823 "cannot expand initializer for member %qD",
16824 TREE_PURPOSE (mem_initializer));
16825 mem_initializer = error_mark_node;
16828 /* Construct the pack expansion type. */
16829 if (mem_initializer != error_mark_node)
16830 mem_initializer = make_pack_expansion (mem_initializer);
16832 if (target_ctor != error_mark_node
16833 && mem_initializer != error_mark_node)
16835 error ("mem-initializer for %qD follows constructor delegation",
16836 TREE_PURPOSE (mem_initializer));
16837 mem_initializer = error_mark_node;
16839 /* Look for a target constructor. */
16840 if (mem_initializer != error_mark_node
16841 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
16842 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
16844 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
16845 if (mem_initializer_list)
16847 error ("constructor delegation follows mem-initializer for %qD",
16848 TREE_PURPOSE (mem_initializer_list));
16849 mem_initializer = error_mark_node;
16851 target_ctor = mem_initializer;
16853 /* Add it to the list, unless it was erroneous. */
16854 if (mem_initializer != error_mark_node)
16856 TREE_CHAIN (mem_initializer) = mem_initializer_list;
16857 mem_initializer_list = mem_initializer;
16859 /* If the next token is not a `,', we're done. */
16860 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16861 break;
16862 /* Consume the `,' token. */
16863 cp_lexer_consume_token (parser->lexer);
16866 /* Perform semantic analysis. */
16867 if (DECL_CONSTRUCTOR_P (current_function_decl))
16868 finish_mem_initializers (mem_initializer_list);
16871 /* Parse a mem-initializer.
16873 mem-initializer:
16874 mem-initializer-id ( expression-list [opt] )
16875 mem-initializer-id braced-init-list
16877 GNU extension:
16879 mem-initializer:
16880 ( expression-list [opt] )
16882 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
16883 class) or FIELD_DECL (for a non-static data member) to initialize;
16884 the TREE_VALUE is the expression-list. An empty initialization
16885 list is represented by void_list_node. */
16887 static tree
16888 cp_parser_mem_initializer (cp_parser* parser)
16890 tree mem_initializer_id;
16891 tree expression_list;
16892 tree member;
16893 cp_token *token = cp_lexer_peek_token (parser->lexer);
16895 /* Find out what is being initialized. */
16896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16898 permerror (token->location,
16899 "anachronistic old-style base class initializer");
16900 mem_initializer_id = NULL_TREE;
16902 else
16904 mem_initializer_id = cp_parser_mem_initializer_id (parser);
16905 if (mem_initializer_id == error_mark_node)
16906 return mem_initializer_id;
16908 member = expand_member_init (mem_initializer_id);
16909 if (member && !DECL_P (member))
16910 in_base_initializer = 1;
16912 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16914 bool expr_non_constant_p;
16915 cp_lexer_set_source_position (parser->lexer);
16916 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16917 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
16918 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
16919 expression_list = build_tree_list (NULL_TREE, expression_list);
16921 else
16923 vec<tree, va_gc> *vec;
16924 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16925 /*cast_p=*/false,
16926 /*allow_expansion_p=*/true,
16927 /*non_constant_p=*/NULL,
16928 /*close_paren_loc=*/NULL,
16929 /*wrap_locations_p=*/true);
16930 if (vec == NULL)
16931 return error_mark_node;
16932 expression_list = build_tree_list_vec (vec);
16933 release_tree_vector (vec);
16936 if (expression_list == error_mark_node)
16937 return error_mark_node;
16938 if (!expression_list)
16939 expression_list = void_type_node;
16941 in_base_initializer = 0;
16943 if (!member)
16944 return error_mark_node;
16945 tree node = build_tree_list (member, expression_list);
16947 /* We can't attach the source location of this initializer directly to
16948 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
16949 within the TREE_TYPE of the list node. */
16950 location_t loc
16951 = make_location (token->location, token->location, parser->lexer);
16952 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
16953 SET_EXPR_LOCATION (dummy, loc);
16954 TREE_TYPE (node) = dummy;
16956 return node;
16959 /* Parse a mem-initializer-id.
16961 mem-initializer-id:
16962 :: [opt] nested-name-specifier [opt] class-name
16963 decltype-specifier (C++11)
16964 identifier
16966 Returns a TYPE indicating the class to be initialized for the first
16967 production (and the second in C++11). Returns an IDENTIFIER_NODE
16968 indicating the data member to be initialized for the last production. */
16970 static tree
16971 cp_parser_mem_initializer_id (cp_parser* parser)
16973 bool global_scope_p;
16974 bool nested_name_specifier_p;
16975 bool template_p = false;
16976 tree id;
16978 cp_token *token = cp_lexer_peek_token (parser->lexer);
16980 /* `typename' is not allowed in this context ([temp.res]). */
16981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16983 error_at (token->location,
16984 "keyword %<typename%> not allowed in this context (a qualified "
16985 "member initializer is implicitly a type)");
16986 cp_lexer_consume_token (parser->lexer);
16988 /* Look for the optional `::' operator. */
16989 global_scope_p
16990 = (cp_parser_global_scope_opt (parser,
16991 /*current_scope_valid_p=*/false)
16992 != NULL_TREE);
16993 /* Look for the optional nested-name-specifier. The simplest way to
16994 implement:
16996 [temp.res]
16998 The keyword `typename' is not permitted in a base-specifier or
16999 mem-initializer; in these contexts a qualified name that
17000 depends on a template-parameter is implicitly assumed to be a
17001 type name.
17003 is to assume that we have seen the `typename' keyword at this
17004 point. */
17005 nested_name_specifier_p
17006 = (cp_parser_nested_name_specifier_opt (parser,
17007 /*typename_keyword_p=*/true,
17008 /*check_dependency_p=*/true,
17009 /*type_p=*/true,
17010 /*is_declaration=*/true)
17011 != NULL_TREE);
17012 if (nested_name_specifier_p)
17013 template_p = cp_parser_optional_template_keyword (parser);
17014 /* If there is a `::' operator or a nested-name-specifier, then we
17015 are definitely looking for a class-name. */
17016 if (global_scope_p || nested_name_specifier_p)
17017 return cp_parser_class_name (parser,
17018 /*typename_keyword_p=*/true,
17019 /*template_keyword_p=*/template_p,
17020 typename_type,
17021 /*check_dependency_p=*/true,
17022 /*class_head_p=*/false,
17023 /*is_declaration=*/true);
17024 /* Otherwise, we could also be looking for an ordinary identifier. */
17025 cp_parser_parse_tentatively (parser);
17026 if (cp_lexer_next_token_is_decltype (parser->lexer))
17027 /* Try a decltype-specifier. */
17028 id = cp_parser_decltype (parser);
17029 else
17030 /* Otherwise, try a class-name. */
17031 id = cp_parser_class_name (parser,
17032 /*typename_keyword_p=*/true,
17033 /*template_keyword_p=*/false,
17034 none_type,
17035 /*check_dependency_p=*/true,
17036 /*class_head_p=*/false,
17037 /*is_declaration=*/true);
17038 /* If we found one, we're done. */
17039 if (cp_parser_parse_definitely (parser))
17040 return id;
17041 /* Otherwise, look for an ordinary identifier. */
17042 return cp_parser_identifier (parser);
17045 /* Overloading [gram.over] */
17047 /* Parse an operator-function-id.
17049 operator-function-id:
17050 operator operator
17052 Returns an IDENTIFIER_NODE for the operator which is a
17053 human-readable spelling of the identifier, e.g., `operator +'. */
17055 static cp_expr
17056 cp_parser_operator_function_id (cp_parser* parser)
17058 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17059 /* Look for the `operator' keyword. */
17060 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17061 return error_mark_node;
17062 /* And then the name of the operator itself. */
17063 return cp_parser_operator (parser, start_loc);
17066 /* Return an identifier node for a user-defined literal operator.
17067 The suffix identifier is chained to the operator name identifier. */
17069 tree
17070 cp_literal_operator_id (const char* name)
17072 tree identifier;
17073 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17074 + strlen (name) + 10);
17075 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17076 identifier = get_identifier (buffer);
17077 XDELETEVEC (buffer);
17079 return identifier;
17082 /* Parse an operator.
17084 operator:
17085 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17086 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17087 || ++ -- , ->* -> () []
17089 GNU Extensions:
17091 operator:
17092 <? >? <?= >?=
17094 Returns an IDENTIFIER_NODE for the operator which is a
17095 human-readable spelling of the identifier, e.g., `operator +'. */
17097 static cp_expr
17098 cp_parser_operator (cp_parser* parser, location_t start_loc)
17100 tree id = NULL_TREE;
17101 cp_token *token;
17102 bool utf8 = false;
17104 /* Peek at the next token. */
17105 token = cp_lexer_peek_token (parser->lexer);
17107 location_t end_loc = token->location;
17109 /* Figure out which operator we have. */
17110 enum tree_code op = ERROR_MARK;
17111 bool assop = false;
17112 bool consumed = false;
17113 switch (token->type)
17115 case CPP_KEYWORD:
17117 /* The keyword should be either `new', `delete' or `co_await'. */
17118 if (token->keyword == RID_NEW)
17119 op = NEW_EXPR;
17120 else if (token->keyword == RID_DELETE)
17121 op = DELETE_EXPR;
17122 else if (token->keyword == RID_CO_AWAIT)
17123 op = CO_AWAIT_EXPR;
17124 else
17125 break;
17127 /* Consume the `new', `delete' or co_await token. */
17128 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17130 /* Peek at the next token. */
17131 token = cp_lexer_peek_token (parser->lexer);
17132 /* If it's a `[' token then this is the array variant of the
17133 operator. */
17134 if (token->type == CPP_OPEN_SQUARE
17135 && op != CO_AWAIT_EXPR)
17137 /* Consume the `[' token. */
17138 cp_lexer_consume_token (parser->lexer);
17139 /* Look for the `]' token. */
17140 if (cp_token *close_token
17141 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17142 end_loc = close_token->location;
17143 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17145 consumed = true;
17146 break;
17149 case CPP_PLUS:
17150 op = PLUS_EXPR;
17151 break;
17153 case CPP_MINUS:
17154 op = MINUS_EXPR;
17155 break;
17157 case CPP_MULT:
17158 op = MULT_EXPR;
17159 break;
17161 case CPP_DIV:
17162 op = TRUNC_DIV_EXPR;
17163 break;
17165 case CPP_MOD:
17166 op = TRUNC_MOD_EXPR;
17167 break;
17169 case CPP_XOR:
17170 op = BIT_XOR_EXPR;
17171 break;
17173 case CPP_AND:
17174 op = BIT_AND_EXPR;
17175 break;
17177 case CPP_OR:
17178 op = BIT_IOR_EXPR;
17179 break;
17181 case CPP_COMPL:
17182 op = BIT_NOT_EXPR;
17183 break;
17185 case CPP_NOT:
17186 op = TRUTH_NOT_EXPR;
17187 break;
17189 case CPP_EQ:
17190 assop = true;
17191 op = NOP_EXPR;
17192 break;
17194 case CPP_LESS:
17195 op = LT_EXPR;
17196 break;
17198 case CPP_GREATER:
17199 op = GT_EXPR;
17200 break;
17202 case CPP_PLUS_EQ:
17203 assop = true;
17204 op = PLUS_EXPR;
17205 break;
17207 case CPP_MINUS_EQ:
17208 assop = true;
17209 op = MINUS_EXPR;
17210 break;
17212 case CPP_MULT_EQ:
17213 assop = true;
17214 op = MULT_EXPR;
17215 break;
17217 case CPP_DIV_EQ:
17218 assop = true;
17219 op = TRUNC_DIV_EXPR;
17220 break;
17222 case CPP_MOD_EQ:
17223 assop = true;
17224 op = TRUNC_MOD_EXPR;
17225 break;
17227 case CPP_XOR_EQ:
17228 assop = true;
17229 op = BIT_XOR_EXPR;
17230 break;
17232 case CPP_AND_EQ:
17233 assop = true;
17234 op = BIT_AND_EXPR;
17235 break;
17237 case CPP_OR_EQ:
17238 assop = true;
17239 op = BIT_IOR_EXPR;
17240 break;
17242 case CPP_LSHIFT:
17243 op = LSHIFT_EXPR;
17244 break;
17246 case CPP_RSHIFT:
17247 op = RSHIFT_EXPR;
17248 break;
17250 case CPP_LSHIFT_EQ:
17251 assop = true;
17252 op = LSHIFT_EXPR;
17253 break;
17255 case CPP_RSHIFT_EQ:
17256 assop = true;
17257 op = RSHIFT_EXPR;
17258 break;
17260 case CPP_EQ_EQ:
17261 op = EQ_EXPR;
17262 break;
17264 case CPP_NOT_EQ:
17265 op = NE_EXPR;
17266 break;
17268 case CPP_LESS_EQ:
17269 op = LE_EXPR;
17270 break;
17272 case CPP_GREATER_EQ:
17273 op = GE_EXPR;
17274 break;
17276 case CPP_SPACESHIP:
17277 op = SPACESHIP_EXPR;
17278 break;
17280 case CPP_AND_AND:
17281 op = TRUTH_ANDIF_EXPR;
17282 break;
17284 case CPP_OR_OR:
17285 op = TRUTH_ORIF_EXPR;
17286 break;
17288 case CPP_PLUS_PLUS:
17289 op = POSTINCREMENT_EXPR;
17290 break;
17292 case CPP_MINUS_MINUS:
17293 op = PREDECREMENT_EXPR;
17294 break;
17296 case CPP_COMMA:
17297 op = COMPOUND_EXPR;
17298 break;
17300 case CPP_DEREF_STAR:
17301 op = MEMBER_REF;
17302 break;
17304 case CPP_DEREF:
17305 op = COMPONENT_REF;
17306 break;
17308 case CPP_QUERY:
17309 op = COND_EXPR;
17310 /* Consume the `?'. */
17311 cp_lexer_consume_token (parser->lexer);
17312 /* Look for the matching `:'. */
17313 cp_parser_require (parser, CPP_COLON, RT_COLON);
17314 consumed = true;
17315 break;
17317 case CPP_OPEN_PAREN:
17319 /* Consume the `('. */
17320 matching_parens parens;
17321 parens.consume_open (parser);
17322 /* Look for the matching `)'. */
17323 token = parens.require_close (parser);
17324 if (token)
17325 end_loc = token->location;
17326 op = CALL_EXPR;
17327 consumed = true;
17328 break;
17331 case CPP_OPEN_SQUARE:
17332 /* Consume the `['. */
17333 cp_lexer_consume_token (parser->lexer);
17334 /* Look for the matching `]'. */
17335 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17336 if (token)
17337 end_loc = token->location;
17338 op = ARRAY_REF;
17339 consumed = true;
17340 break;
17342 case CPP_UTF8STRING:
17343 case CPP_UTF8STRING_USERDEF:
17344 utf8 = true;
17345 /* FALLTHRU */
17346 case CPP_STRING:
17347 case CPP_WSTRING:
17348 case CPP_STRING16:
17349 case CPP_STRING32:
17350 case CPP_STRING_USERDEF:
17351 case CPP_WSTRING_USERDEF:
17352 case CPP_STRING16_USERDEF:
17353 case CPP_STRING32_USERDEF:
17355 cp_expr str;
17356 tree string_tree;
17357 int sz, len;
17359 if (cxx_dialect == cxx98)
17360 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17362 /* Consume the string. */
17363 str = cp_parser_string_literal (parser, /*translate=*/true,
17364 /*wide_ok=*/true, /*lookup_udlit=*/false);
17365 if (str == error_mark_node)
17366 return error_mark_node;
17367 else if (TREE_CODE (str) == USERDEF_LITERAL)
17369 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17370 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17371 end_loc = str.get_location ();
17373 else
17375 string_tree = str;
17376 /* Look for the suffix identifier. */
17377 token = cp_lexer_peek_token (parser->lexer);
17378 if (token->type == CPP_NAME)
17380 id = cp_parser_identifier (parser);
17381 end_loc = token->location;
17383 else if (token->type == CPP_KEYWORD)
17385 error ("unexpected keyword;"
17386 " remove space between quotes and suffix identifier");
17387 return error_mark_node;
17389 else
17391 error ("expected suffix identifier");
17392 return error_mark_node;
17395 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17396 (TREE_TYPE (TREE_TYPE (string_tree))));
17397 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17398 if (len != 0)
17400 error ("expected empty string after %<operator%> keyword");
17401 return error_mark_node;
17403 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17404 != char_type_node)
17406 error ("invalid encoding prefix in literal operator");
17407 return error_mark_node;
17409 if (id != error_mark_node)
17411 const char *name = IDENTIFIER_POINTER (id);
17412 id = cp_literal_operator_id (name);
17414 /* Generate a location of the form:
17415 "" _suffix_identifier
17416 ^~~~~~~~~~~~~~~~~~~~~
17417 with caret == start at the start token, finish at the end of the
17418 suffix identifier. */
17419 location_t combined_loc
17420 = make_location (start_loc, start_loc, parser->lexer);
17421 return cp_expr (id, combined_loc);
17424 default:
17425 /* Anything else is an error. */
17426 break;
17429 /* If we have selected an identifier, we need to consume the
17430 operator token. */
17431 if (op != ERROR_MARK)
17433 id = ovl_op_identifier (assop, op);
17434 if (!consumed)
17435 cp_lexer_consume_token (parser->lexer);
17437 /* Otherwise, no valid operator name was present. */
17438 else
17440 cp_parser_error (parser, "expected operator");
17441 id = error_mark_node;
17444 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
17445 return cp_expr (id, start_loc);
17448 /* Parse a template-declaration.
17450 template-declaration:
17451 export [opt] template < template-parameter-list > declaration
17453 If MEMBER_P is TRUE, this template-declaration occurs within a
17454 class-specifier.
17456 The grammar rule given by the standard isn't correct. What
17457 is really meant is:
17459 template-declaration:
17460 export [opt] template-parameter-list-seq
17461 decl-specifier-seq [opt] init-declarator [opt] ;
17462 export [opt] template-parameter-list-seq
17463 function-definition
17465 template-parameter-list-seq:
17466 template-parameter-list-seq [opt]
17467 template < template-parameter-list >
17469 Concept Extensions:
17471 template-parameter-list-seq:
17472 template < template-parameter-list > requires-clause [opt]
17474 requires-clause:
17475 requires logical-or-expression */
17477 static void
17478 cp_parser_template_declaration (cp_parser* parser, bool member_p)
17480 /* Check for `export'. */
17481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
17483 /* Consume the `export' token. */
17484 cp_lexer_consume_token (parser->lexer);
17485 /* Warn that this use of export is deprecated. */
17486 if (cxx_dialect < cxx11)
17487 warning (0, "keyword %<export%> not implemented, and will be ignored");
17488 else if (cxx_dialect < cxx20)
17489 warning (0, "keyword %<export%> is deprecated, and is ignored");
17490 else
17491 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17494 cp_parser_template_declaration_after_export (parser, member_p);
17497 /* Parse a template-parameter-list.
17499 template-parameter-list:
17500 template-parameter
17501 template-parameter-list , template-parameter
17503 Returns a TREE_LIST. Each node represents a template parameter.
17504 The nodes are connected via their TREE_CHAINs. */
17506 static tree
17507 cp_parser_template_parameter_list (cp_parser* parser)
17509 tree parameter_list = NULL_TREE;
17511 /* Don't create wrapper nodes within a template-parameter-list,
17512 since we don't want to have different types based on the
17513 spelling location of constants and decls within them. */
17514 auto_suppress_location_wrappers sentinel;
17516 begin_template_parm_list ();
17518 /* The loop below parses the template parms. We first need to know
17519 the total number of template parms to be able to compute proper
17520 canonical types of each dependent type. So after the loop, when
17521 we know the total number of template parms,
17522 end_template_parm_list computes the proper canonical types and
17523 fixes up the dependent types accordingly. */
17524 while (true)
17526 tree parameter;
17527 bool is_non_type;
17528 bool is_parameter_pack;
17529 location_t parm_loc;
17531 /* Parse the template-parameter. */
17532 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
17533 parameter = cp_parser_template_parameter (parser,
17534 &is_non_type,
17535 &is_parameter_pack);
17536 /* Add it to the list. */
17537 if (parameter != error_mark_node)
17538 parameter_list = process_template_parm (parameter_list,
17539 parm_loc,
17540 parameter,
17541 is_non_type,
17542 is_parameter_pack);
17543 else
17545 tree err_parm = build_tree_list (parameter, parameter);
17546 parameter_list = chainon (parameter_list, err_parm);
17549 /* If the next token is not a `,', we're done. */
17550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17551 break;
17552 /* Otherwise, consume the `,' token. */
17553 cp_lexer_consume_token (parser->lexer);
17556 return end_template_parm_list (parameter_list);
17559 /* Parse a introduction-list.
17561 introduction-list:
17562 introduced-parameter
17563 introduction-list , introduced-parameter
17565 introduced-parameter:
17566 ...[opt] identifier
17568 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
17569 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
17570 WILDCARD_DECL will also have DECL_NAME set and token location in
17571 DECL_SOURCE_LOCATION. */
17573 static tree
17574 cp_parser_introduction_list (cp_parser *parser)
17576 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
17578 while (true)
17580 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17581 if (is_pack)
17582 cp_lexer_consume_token (parser->lexer);
17584 tree identifier = cp_parser_identifier (parser);
17585 if (identifier == error_mark_node)
17586 break;
17588 /* Build placeholder. */
17589 tree parm = build_nt (WILDCARD_DECL);
17590 DECL_SOURCE_LOCATION (parm)
17591 = cp_lexer_peek_token (parser->lexer)->location;
17592 DECL_NAME (parm) = identifier;
17593 WILDCARD_PACK_P (parm) = is_pack;
17594 vec_safe_push (introduction_vec, parm);
17596 /* If the next token is not a `,', we're done. */
17597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17598 break;
17599 /* Otherwise, consume the `,' token. */
17600 cp_lexer_consume_token (parser->lexer);
17603 /* Convert the vec into a TREE_VEC. */
17604 tree introduction_list = make_tree_vec (introduction_vec->length ());
17605 unsigned int n;
17606 tree parm;
17607 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
17608 TREE_VEC_ELT (introduction_list, n) = parm;
17610 release_tree_vector (introduction_vec);
17611 return introduction_list;
17614 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17615 is an abstract declarator. */
17617 static inline cp_declarator*
17618 get_id_declarator (cp_declarator *declarator)
17620 cp_declarator *d = declarator;
17621 while (d && d->kind != cdk_id)
17622 d = d->declarator;
17623 return d;
17626 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17627 is an abstract declarator. */
17629 static inline tree
17630 get_unqualified_id (cp_declarator *declarator)
17632 declarator = get_id_declarator (declarator);
17633 if (declarator)
17634 return declarator->u.id.unqualified_name;
17635 else
17636 return NULL_TREE;
17639 /* Returns true if TYPE would declare a constrained constrained-parameter. */
17641 static inline bool
17642 is_constrained_parameter (tree type)
17644 return (type
17645 && TREE_CODE (type) == TYPE_DECL
17646 && CONSTRAINED_PARM_CONCEPT (type)
17647 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
17650 /* Returns true if PARM declares a constrained-parameter. */
17652 static inline bool
17653 is_constrained_parameter (cp_parameter_declarator *parm)
17655 return is_constrained_parameter (parm->decl_specifiers.type);
17658 /* Check that the type parameter is only a declarator-id, and that its
17659 type is not cv-qualified. */
17661 bool
17662 cp_parser_check_constrained_type_parm (cp_parser *parser,
17663 cp_parameter_declarator *parm)
17665 if (!parm->declarator)
17666 return true;
17668 if (parm->declarator->kind != cdk_id)
17670 cp_parser_error (parser, "invalid constrained type parameter");
17671 return false;
17674 /* Don't allow cv-qualified type parameters. */
17675 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
17676 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
17678 cp_parser_error (parser, "cv-qualified type parameter");
17679 return false;
17682 return true;
17685 /* Finish parsing/processing a template type parameter and checking
17686 various restrictions. */
17688 static inline tree
17689 cp_parser_constrained_type_template_parm (cp_parser *parser,
17690 tree id,
17691 cp_parameter_declarator* parmdecl)
17693 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
17694 return finish_template_type_parm (class_type_node, id);
17695 else
17696 return error_mark_node;
17699 static tree
17700 finish_constrained_template_template_parm (tree proto, tree id)
17702 /* FIXME: This should probably be copied, and we may need to adjust
17703 the template parameter depths. */
17704 tree saved_parms = current_template_parms;
17705 begin_template_parm_list ();
17706 current_template_parms = DECL_TEMPLATE_PARMS (proto);
17707 end_template_parm_list ();
17709 tree parm = finish_template_template_parm (class_type_node, id);
17710 current_template_parms = saved_parms;
17712 return parm;
17715 /* Finish parsing/processing a template template parameter by borrowing
17716 the template parameter list from the prototype parameter. */
17718 static tree
17719 cp_parser_constrained_template_template_parm (cp_parser *parser,
17720 tree proto,
17721 tree id,
17722 cp_parameter_declarator *parmdecl)
17724 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
17725 return error_mark_node;
17726 return finish_constrained_template_template_parm (proto, id);
17729 /* Create a new non-type template parameter from the given PARM
17730 declarator. */
17732 static tree
17733 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
17734 cp_parameter_declarator *parm)
17736 *is_non_type = true;
17737 cp_declarator *decl = parm->declarator;
17738 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
17739 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
17740 return grokdeclarator (decl, specs, TPARM, 0, NULL);
17743 /* Build a constrained template parameter based on the PARMDECL
17744 declarator. The type of PARMDECL is the constrained type, which
17745 refers to the prototype template parameter that ultimately
17746 specifies the type of the declared parameter. */
17748 static tree
17749 finish_constrained_parameter (cp_parser *parser,
17750 cp_parameter_declarator *parmdecl,
17751 bool *is_non_type)
17753 tree decl = parmdecl->decl_specifiers.type;
17754 tree id = get_unqualified_id (parmdecl->declarator);
17755 tree def = parmdecl->default_argument;
17756 tree proto = DECL_INITIAL (decl);
17758 /* Build the parameter. Return an error if the declarator was invalid. */
17759 tree parm;
17760 if (TREE_CODE (proto) == TYPE_DECL)
17761 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
17762 else if (TREE_CODE (proto) == TEMPLATE_DECL)
17763 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
17764 parmdecl);
17765 else
17766 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
17767 if (parm == error_mark_node)
17768 return error_mark_node;
17770 /* Finish the parameter decl and create a node attaching the
17771 default argument and constraint. */
17772 parm = build_tree_list (def, parm);
17773 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
17775 return parm;
17778 /* Returns true if the parsed type actually represents the declaration
17779 of a type template-parameter. */
17781 static bool
17782 declares_constrained_type_template_parameter (tree type)
17784 return (is_constrained_parameter (type)
17785 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
17788 /* Returns true if the parsed type actually represents the declaration of
17789 a template template-parameter. */
17791 static bool
17792 declares_constrained_template_template_parameter (tree type)
17794 return (is_constrained_parameter (type)
17795 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
17798 /* Parse a default argument for a type template-parameter.
17799 Note that diagnostics are handled in cp_parser_template_parameter. */
17801 static tree
17802 cp_parser_default_type_template_argument (cp_parser *parser)
17804 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17806 /* Consume the `=' token. */
17807 cp_lexer_consume_token (parser->lexer);
17809 cp_token *token = cp_lexer_peek_token (parser->lexer);
17811 /* Tell cp_parser_lambda_expression this is a default argument. */
17812 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
17813 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
17815 /* Parse the default-argument. */
17816 push_deferring_access_checks (dk_no_deferred);
17817 tree default_argument = cp_parser_type_id (parser,
17818 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17819 NULL);
17820 pop_deferring_access_checks ();
17822 if (flag_concepts && type_uses_auto (default_argument))
17824 error_at (token->location,
17825 "invalid use of %<auto%> in default template argument");
17826 return error_mark_node;
17829 return default_argument;
17832 /* Parse a default argument for a template template-parameter. */
17834 static tree
17835 cp_parser_default_template_template_argument (cp_parser *parser)
17837 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17839 bool is_template;
17841 /* Consume the `='. */
17842 cp_lexer_consume_token (parser->lexer);
17843 /* Parse the id-expression. */
17844 push_deferring_access_checks (dk_no_deferred);
17845 /* save token before parsing the id-expression, for error
17846 reporting */
17847 const cp_token* token = cp_lexer_peek_token (parser->lexer);
17848 tree default_argument
17849 = cp_parser_id_expression (parser,
17850 /*template_keyword_p=*/false,
17851 /*check_dependency_p=*/true,
17852 /*template_p=*/&is_template,
17853 /*declarator_p=*/false,
17854 /*optional_p=*/false);
17855 if (TREE_CODE (default_argument) == TYPE_DECL)
17856 /* If the id-expression was a template-id that refers to
17857 a template-class, we already have the declaration here,
17858 so no further lookup is needed. */
17860 else
17861 /* Look up the name. */
17862 default_argument
17863 = cp_parser_lookup_name (parser, default_argument,
17864 none_type,
17865 /*is_template=*/is_template,
17866 /*is_namespace=*/false,
17867 /*check_dependency=*/true,
17868 /*ambiguous_decls=*/NULL,
17869 token->location);
17870 /* See if the default argument is valid. */
17871 default_argument = check_template_template_default_arg (default_argument);
17872 pop_deferring_access_checks ();
17873 return default_argument;
17876 /* Parse a template-parameter.
17878 template-parameter:
17879 type-parameter
17880 parameter-declaration
17882 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
17883 the parameter. The TREE_PURPOSE is the default value, if any.
17884 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
17885 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
17886 set to true iff this parameter is a parameter pack. */
17888 static tree
17889 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
17890 bool *is_parameter_pack)
17892 cp_token *token;
17893 cp_parameter_declarator *parameter_declarator;
17894 tree parm;
17896 /* Assume it is a type parameter or a template parameter. */
17897 *is_non_type = false;
17898 /* Assume it not a parameter pack. */
17899 *is_parameter_pack = false;
17900 /* Peek at the next token. */
17901 token = cp_lexer_peek_token (parser->lexer);
17902 /* If it is `template', we have a type-parameter. */
17903 if (token->keyword == RID_TEMPLATE)
17904 return cp_parser_type_parameter (parser, is_parameter_pack);
17905 /* If it is `class' or `typename' we do not know yet whether it is a
17906 type parameter or a non-type parameter. Consider:
17908 template <typename T, typename T::X X> ...
17912 template <class C, class D*> ...
17914 Here, the first parameter is a type parameter, and the second is
17915 a non-type parameter. We can tell by looking at the token after
17916 the identifier -- if it is a `,', `=', or `>' then we have a type
17917 parameter. */
17918 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
17920 /* Peek at the token after `class' or `typename'. */
17921 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17922 /* If it's an ellipsis, we have a template type parameter
17923 pack. */
17924 if (token->type == CPP_ELLIPSIS)
17925 return cp_parser_type_parameter (parser, is_parameter_pack);
17926 /* If it's an identifier, skip it. */
17927 if (token->type == CPP_NAME)
17928 token = cp_lexer_peek_nth_token (parser->lexer, 3);
17929 /* Now, see if the token looks like the end of a template
17930 parameter. */
17931 if (token->type == CPP_COMMA
17932 || token->type == CPP_EQ
17933 || token->type == CPP_GREATER)
17934 return cp_parser_type_parameter (parser, is_parameter_pack);
17937 /* Otherwise, it is a non-type parameter or a constrained parameter.
17939 [temp.param]
17941 When parsing a default template-argument for a non-type
17942 template-parameter, the first non-nested `>' is taken as the end
17943 of the template parameter-list rather than a greater-than
17944 operator. */
17945 parameter_declarator
17946 = cp_parser_parameter_declaration (parser,
17947 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17948 /*template_parm_p=*/true,
17949 /*parenthesized_p=*/NULL);
17951 if (!parameter_declarator)
17952 return error_mark_node;
17954 /* If the parameter declaration is marked as a parameter pack, set
17955 *IS_PARAMETER_PACK to notify the caller. */
17956 if (parameter_declarator->template_parameter_pack_p)
17957 *is_parameter_pack = true;
17959 if (parameter_declarator->default_argument)
17961 /* Can happen in some cases of erroneous input (c++/34892). */
17962 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17963 /* Consume the `...' for better error recovery. */
17964 cp_lexer_consume_token (parser->lexer);
17967 /* The parameter may have been constrained type parameter. */
17968 if (is_constrained_parameter (parameter_declarator))
17969 return finish_constrained_parameter (parser,
17970 parameter_declarator,
17971 is_non_type);
17973 // Now we're sure that the parameter is a non-type parameter.
17974 *is_non_type = true;
17976 parm = grokdeclarator (parameter_declarator->declarator,
17977 &parameter_declarator->decl_specifiers,
17978 TPARM, /*initialized=*/0,
17979 /*attrlist=*/NULL);
17980 if (parm == error_mark_node)
17981 return error_mark_node;
17983 return build_tree_list (parameter_declarator->default_argument, parm);
17986 /* Parse a type-parameter.
17988 type-parameter:
17989 class identifier [opt]
17990 class identifier [opt] = type-id
17991 typename identifier [opt]
17992 typename identifier [opt] = type-id
17993 template < template-parameter-list > class identifier [opt]
17994 template < template-parameter-list > class identifier [opt]
17995 = id-expression
17997 GNU Extension (variadic templates):
17999 type-parameter:
18000 class ... identifier [opt]
18001 typename ... identifier [opt]
18003 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18004 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18005 the declaration of the parameter.
18007 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18009 static tree
18010 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18012 cp_token *token;
18013 tree parameter;
18015 /* Look for a keyword to tell us what kind of parameter this is. */
18016 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18017 if (!token)
18018 return error_mark_node;
18020 switch (token->keyword)
18022 case RID_CLASS:
18023 case RID_TYPENAME:
18025 tree identifier;
18026 tree default_argument;
18028 /* If the next token is an ellipsis, we have a template
18029 argument pack. */
18030 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18032 /* Consume the `...' token. */
18033 cp_lexer_consume_token (parser->lexer);
18034 maybe_warn_variadic_templates ();
18036 *is_parameter_pack = true;
18039 /* If the next token is an identifier, then it names the
18040 parameter. */
18041 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18042 identifier = cp_parser_identifier (parser);
18043 else
18044 identifier = NULL_TREE;
18046 /* Create the parameter. */
18047 parameter = finish_template_type_parm (class_type_node, identifier);
18049 /* If the next token is an `=', we have a default argument. */
18050 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18052 default_argument
18053 = cp_parser_default_type_template_argument (parser);
18055 /* Template parameter packs cannot have default
18056 arguments. */
18057 if (*is_parameter_pack)
18059 if (identifier)
18060 error_at (token->location,
18061 "template parameter pack %qD cannot have a "
18062 "default argument", identifier);
18063 else
18064 error_at (token->location,
18065 "template parameter packs cannot have "
18066 "default arguments");
18067 default_argument = NULL_TREE;
18069 else if (check_for_bare_parameter_packs (default_argument))
18070 default_argument = error_mark_node;
18072 else
18073 default_argument = NULL_TREE;
18075 /* Create the combined representation of the parameter and the
18076 default argument. */
18077 parameter = build_tree_list (default_argument, parameter);
18079 break;
18081 case RID_TEMPLATE:
18083 tree identifier;
18084 tree default_argument;
18086 /* Look for the `<'. */
18087 cp_parser_require (parser, CPP_LESS, RT_LESS);
18088 /* Parse the template-parameter-list. */
18089 cp_parser_template_parameter_list (parser);
18090 /* Look for the `>'. */
18091 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18093 /* If template requirements are present, parse them. */
18094 if (flag_concepts)
18096 tree reqs = get_shorthand_constraints (current_template_parms);
18097 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18098 reqs = combine_constraint_expressions (reqs, dreqs);
18099 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18102 /* Look for the `class' or 'typename' keywords. */
18103 cp_parser_type_parameter_key (parser);
18104 /* If the next token is an ellipsis, we have a template
18105 argument pack. */
18106 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18108 /* Consume the `...' token. */
18109 cp_lexer_consume_token (parser->lexer);
18110 maybe_warn_variadic_templates ();
18112 *is_parameter_pack = true;
18114 /* If the next token is an `=', then there is a
18115 default-argument. If the next token is a `>', we are at
18116 the end of the parameter-list. If the next token is a `,',
18117 then we are at the end of this parameter. */
18118 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18119 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18120 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18122 identifier = cp_parser_identifier (parser);
18123 /* Treat invalid names as if the parameter were nameless. */
18124 if (identifier == error_mark_node)
18125 identifier = NULL_TREE;
18127 else
18128 identifier = NULL_TREE;
18130 /* Create the template parameter. */
18131 parameter = finish_template_template_parm (class_type_node,
18132 identifier);
18134 /* If the next token is an `=', then there is a
18135 default-argument. */
18136 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18138 default_argument
18139 = cp_parser_default_template_template_argument (parser);
18141 /* Template parameter packs cannot have default
18142 arguments. */
18143 if (*is_parameter_pack)
18145 if (identifier)
18146 error_at (token->location,
18147 "template parameter pack %qD cannot "
18148 "have a default argument",
18149 identifier);
18150 else
18151 error_at (token->location, "template parameter packs cannot "
18152 "have default arguments");
18153 default_argument = NULL_TREE;
18156 else
18157 default_argument = NULL_TREE;
18159 /* Create the combined representation of the parameter and the
18160 default argument. */
18161 parameter = build_tree_list (default_argument, parameter);
18163 break;
18165 default:
18166 gcc_unreachable ();
18167 break;
18170 return parameter;
18173 /* Parse a template-id.
18175 template-id:
18176 template-name < template-argument-list [opt] >
18178 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18179 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18180 returned. Otherwise, if the template-name names a function, or set
18181 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18182 names a class, returns a TYPE_DECL for the specialization.
18184 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18185 uninstantiated templates. */
18187 static tree
18188 cp_parser_template_id (cp_parser *parser,
18189 bool template_keyword_p,
18190 bool check_dependency_p,
18191 enum tag_types tag_type,
18192 bool is_declaration)
18194 tree templ;
18195 tree arguments;
18196 tree template_id;
18197 cp_token_position start_of_id = 0;
18198 cp_token *next_token = NULL, *next_token_2 = NULL;
18199 bool is_identifier;
18201 /* If the next token corresponds to a template-id, there is no need
18202 to reparse it. */
18203 cp_token *token = cp_lexer_peek_token (parser->lexer);
18205 if (token->type == CPP_TEMPLATE_ID)
18207 cp_lexer_consume_token (parser->lexer);
18208 return saved_checks_value (token->u.tree_check_value);
18211 /* Avoid performing name lookup if there is no possibility of
18212 finding a template-id. */
18213 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18214 || (token->type == CPP_NAME
18215 && !cp_parser_nth_token_starts_template_argument_list_p
18216 (parser, 2)))
18218 cp_parser_error (parser, "expected template-id");
18219 return error_mark_node;
18222 /* Remember where the template-id starts. */
18223 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18224 start_of_id = cp_lexer_token_position (parser->lexer, false);
18226 push_deferring_access_checks (dk_deferred);
18228 /* Parse the template-name. */
18229 is_identifier = false;
18230 templ = cp_parser_template_name (parser, template_keyword_p,
18231 check_dependency_p,
18232 is_declaration,
18233 tag_type,
18234 &is_identifier);
18236 /* Push any access checks inside the firewall we're about to create. */
18237 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18238 pop_deferring_access_checks ();
18239 if (templ == error_mark_node || is_identifier)
18240 return templ;
18242 /* Since we're going to preserve any side-effects from this parse, set up a
18243 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18244 in the template arguments. */
18245 tentative_firewall firewall (parser);
18246 reopen_deferring_access_checks (checks);
18248 /* If we find the sequence `[:' after a template-name, it's probably
18249 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18250 parse correctly the argument list. */
18251 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18252 == CPP_OPEN_SQUARE)
18253 && next_token->flags & DIGRAPH
18254 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18255 == CPP_COLON)
18256 && !(next_token_2->flags & PREV_WHITE))
18258 cp_parser_parse_tentatively (parser);
18259 /* Change `:' into `::'. */
18260 next_token_2->type = CPP_SCOPE;
18261 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18262 CPP_LESS. */
18263 cp_lexer_consume_token (parser->lexer);
18265 /* Parse the arguments. */
18266 arguments = cp_parser_enclosed_template_argument_list (parser);
18267 if (!cp_parser_parse_definitely (parser))
18269 /* If we couldn't parse an argument list, then we revert our changes
18270 and return simply an error. Maybe this is not a template-id
18271 after all. */
18272 next_token_2->type = CPP_COLON;
18273 cp_parser_error (parser, "expected %<<%>");
18274 pop_deferring_access_checks ();
18275 return error_mark_node;
18277 /* Otherwise, emit an error about the invalid digraph, but continue
18278 parsing because we got our argument list. */
18279 if (permerror (next_token->location,
18280 "%<<::%> cannot begin a template-argument list"))
18282 static bool hint = false;
18283 inform (next_token->location,
18284 "%<<:%> is an alternate spelling for %<[%>."
18285 " Insert whitespace between %<<%> and %<::%>");
18286 if (!hint && !flag_permissive)
18288 inform (next_token->location, "(if you use %<-fpermissive%> "
18289 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18290 "accept your code)");
18291 hint = true;
18295 else
18297 /* Look for the `<' that starts the template-argument-list. */
18298 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18300 pop_deferring_access_checks ();
18301 return error_mark_node;
18303 /* Parse the arguments. */
18304 arguments = cp_parser_enclosed_template_argument_list (parser);
18306 if ((cxx_dialect > cxx17)
18307 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18308 && !template_keyword_p
18309 && (cp_parser_error_occurred (parser)
18310 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18312 /* This didn't go well. */
18313 if (TREE_CODE (templ) == FUNCTION_DECL)
18315 /* C++20 says that "function-name < a;" is now ill-formed. */
18316 if (cp_parser_error_occurred (parser))
18318 error_at (token->location, "invalid template-argument-list");
18319 inform (token->location, "function name as the left hand "
18320 "operand of %<<%> is ill-formed in C++20; wrap the "
18321 "function name in %<()%>");
18323 else
18324 /* We expect "f<targs>" to be followed by "(args)". */
18325 error_at (cp_lexer_peek_token (parser->lexer)->location,
18326 "expected %<(%> after template-argument-list");
18327 if (start_of_id)
18328 /* Purge all subsequent tokens. */
18329 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18331 else
18332 cp_parser_simulate_error (parser);
18333 pop_deferring_access_checks ();
18334 return error_mark_node;
18338 /* Set the location to be of the form:
18339 template-name < template-argument-list [opt] >
18340 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18341 with caret == start at the start of the template-name,
18342 ranging until the closing '>'. */
18343 location_t combined_loc
18344 = make_location (token->location, token->location, parser->lexer);
18346 /* Check for concepts autos where they don't belong. We could
18347 identify types in some cases of identifier TEMPL, looking ahead
18348 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18349 types. We reject them in functions, but if what we have is an
18350 identifier, even with none_type we can't conclude it's NOT a
18351 type, we have to wait for template substitution. */
18352 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18353 template_id = error_mark_node;
18354 /* Build a representation of the specialization. */
18355 else if (identifier_p (templ))
18356 template_id = build_min_nt_loc (combined_loc,
18357 TEMPLATE_ID_EXPR,
18358 templ, arguments);
18359 else if (DECL_TYPE_TEMPLATE_P (templ)
18360 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18362 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18363 template (rather than some instantiation thereof) only if
18364 is not nested within some other construct. For example, in
18365 "template <typename T> void f(T) { A<T>::", A<T> is just an
18366 instantiation of A. */
18367 bool entering_scope
18368 = (template_parm_scope_p ()
18369 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18370 template_id
18371 = finish_template_type (templ, arguments, entering_scope);
18373 else if (concept_definition_p (templ))
18375 /* The caller will decide whether this is a concept check or type
18376 constraint. */
18377 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18378 boolean_type_node, templ, arguments);
18380 else if (variable_template_p (templ))
18382 template_id = lookup_template_variable (templ, arguments);
18383 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18384 SET_EXPR_LOCATION (template_id, combined_loc);
18386 else if (TREE_CODE (templ) == TYPE_DECL
18387 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18389 /* Some type template in dependent scope. */
18390 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18391 name = build_min_nt_loc (combined_loc,
18392 TEMPLATE_ID_EXPR,
18393 name, arguments);
18394 template_id = templ;
18396 else
18398 /* If it's not a class-template or a template-template, it should be
18399 a function-template. */
18400 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18402 template_id = lookup_template_function (templ, arguments);
18403 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18404 SET_EXPR_LOCATION (template_id, combined_loc);
18407 /* If parsing tentatively, replace the sequence of tokens that makes
18408 up the template-id with a CPP_TEMPLATE_ID token. That way,
18409 should we re-parse the token stream, we will not have to repeat
18410 the effort required to do the parse, nor will we issue duplicate
18411 error messages about problems during instantiation of the
18412 template. */
18413 if (start_of_id
18414 /* Don't do this if we had a parse error in a declarator; re-parsing
18415 might succeed if a name changes meaning (60361). */
18416 && !(cp_parser_error_occurred (parser)
18417 && cp_parser_parsing_tentatively (parser)
18418 && parser->in_declarator_p))
18420 /* Reset the contents of the START_OF_ID token. */
18421 token->type = CPP_TEMPLATE_ID;
18422 token->location = combined_loc;
18424 /* Retrieve any deferred checks. Do not pop this access checks yet
18425 so the memory will not be reclaimed during token replacing below. */
18426 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18427 token->tree_check_p = true;
18428 token->u.tree_check_value->value = template_id;
18429 token->u.tree_check_value->checks = get_deferred_access_checks ();
18430 token->keyword = RID_MAX;
18432 /* Purge all subsequent tokens. */
18433 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18435 /* ??? Can we actually assume that, if template_id ==
18436 error_mark_node, we will have issued a diagnostic to the
18437 user, as opposed to simply marking the tentative parse as
18438 failed? */
18439 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
18440 error_at (token->location, "parse error in template argument list");
18443 pop_to_parent_deferring_access_checks ();
18444 return template_id;
18447 /* Like cp_parser_template_id, called in non-type context. */
18449 static tree
18450 cp_parser_template_id_expr (cp_parser *parser,
18451 bool template_keyword_p,
18452 bool check_dependency_p,
18453 bool is_declaration)
18455 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
18456 none_type, is_declaration);
18457 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
18458 && concept_check_p (x))
18459 /* We didn't check the arguments in cp_parser_template_id; do that now. */
18460 return build_concept_id (x);
18461 return x;
18464 /* Parse a template-name.
18466 template-name:
18467 identifier
18469 The standard should actually say:
18471 template-name:
18472 identifier
18473 operator-function-id
18475 A defect report has been filed about this issue.
18477 A conversion-function-id cannot be a template name because they cannot
18478 be part of a template-id. In fact, looking at this code:
18480 a.operator K<int>()
18482 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18483 It is impossible to call a templated conversion-function-id with an
18484 explicit argument list, since the only allowed template parameter is
18485 the type to which it is converting.
18487 If TEMPLATE_KEYWORD_P is true, then we have just seen the
18488 `template' keyword, in a construction like:
18490 T::template f<3>()
18492 In that case `f' is taken to be a template-name, even though there
18493 is no way of knowing for sure.
18495 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18496 name refers to a set of overloaded functions, at least one of which
18497 is a template, or an IDENTIFIER_NODE with the name of the template,
18498 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
18499 names are looked up inside uninstantiated templates. */
18501 static tree
18502 cp_parser_template_name (cp_parser* parser,
18503 bool template_keyword_p,
18504 bool check_dependency_p,
18505 bool is_declaration,
18506 enum tag_types tag_type,
18507 bool *is_identifier)
18509 tree identifier;
18510 tree decl;
18511 cp_token *token = cp_lexer_peek_token (parser->lexer);
18513 /* If the next token is `operator', then we have either an
18514 operator-function-id or a conversion-function-id. */
18515 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
18517 /* We don't know whether we're looking at an
18518 operator-function-id or a conversion-function-id. */
18519 cp_parser_parse_tentatively (parser);
18520 /* Try an operator-function-id. */
18521 identifier = cp_parser_operator_function_id (parser);
18522 /* If that didn't work, try a conversion-function-id. */
18523 if (!cp_parser_parse_definitely (parser))
18525 cp_parser_error (parser, "expected template-name");
18526 return error_mark_node;
18529 /* Look for the identifier. */
18530 else
18531 identifier = cp_parser_identifier (parser);
18533 /* If we didn't find an identifier, we don't have a template-id. */
18534 if (identifier == error_mark_node)
18535 return error_mark_node;
18537 /* If the name immediately followed the `template' keyword, then it
18538 is a template-name. However, if the next token is not `<', then
18539 we do not treat it as a template-name, since it is not being used
18540 as part of a template-id. This enables us to handle constructs
18541 like:
18543 template <typename T> struct S { S(); };
18544 template <typename T> S<T>::S();
18546 correctly. We would treat `S' as a template -- if it were `S<T>'
18547 -- but we do not if there is no `<'. */
18549 if (processing_template_decl
18550 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18552 /* In a declaration, in a dependent context, we pretend that the
18553 "template" keyword was present in order to improve error
18554 recovery. For example, given:
18556 template <typename T> void f(T::X<int>);
18558 we want to treat "X<int>" as a template-id. */
18559 if (is_declaration
18560 && !template_keyword_p
18561 && parser->scope && TYPE_P (parser->scope)
18562 && check_dependency_p
18563 && dependent_scope_p (parser->scope)
18564 /* Do not do this for dtors (or ctors), since they never
18565 need the template keyword before their name. */
18566 && !constructor_name_p (identifier, parser->scope))
18568 cp_token_position start = 0;
18570 /* Explain what went wrong. */
18571 error_at (token->location, "non-template %qD used as template",
18572 identifier);
18573 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
18574 parser->scope, identifier);
18575 /* If parsing tentatively, find the location of the "<" token. */
18576 if (cp_parser_simulate_error (parser))
18577 start = cp_lexer_token_position (parser->lexer, true);
18578 /* Parse the template arguments so that we can issue error
18579 messages about them. */
18580 cp_lexer_consume_token (parser->lexer);
18581 cp_parser_enclosed_template_argument_list (parser);
18582 /* Skip tokens until we find a good place from which to
18583 continue parsing. */
18584 cp_parser_skip_to_closing_parenthesis (parser,
18585 /*recovering=*/true,
18586 /*or_comma=*/true,
18587 /*consume_paren=*/false);
18588 /* If parsing tentatively, permanently remove the
18589 template argument list. That will prevent duplicate
18590 error messages from being issued about the missing
18591 "template" keyword. */
18592 if (start)
18593 cp_lexer_purge_tokens_after (parser->lexer, start);
18594 if (is_identifier)
18595 *is_identifier = true;
18596 parser->context->object_type = NULL_TREE;
18597 return identifier;
18600 /* If the "template" keyword is present, then there is generally
18601 no point in doing name-lookup, so we just return IDENTIFIER.
18602 But, if the qualifying scope is non-dependent then we can
18603 (and must) do name-lookup normally. */
18604 if (template_keyword_p)
18606 tree scope = (parser->scope ? parser->scope
18607 : parser->context->object_type);
18608 if (scope && TYPE_P (scope)
18609 && (!CLASS_TYPE_P (scope)
18610 || (check_dependency_p && dependent_scope_p (scope))))
18612 /* We're optimizing away the call to cp_parser_lookup_name, but
18613 we still need to do this. */
18614 parser->object_scope = parser->context->object_type;
18615 parser->context->object_type = NULL_TREE;
18616 return identifier;
18621 /* cp_parser_lookup_name clears OBJECT_TYPE. */
18622 tree scope = (parser->scope ? parser->scope
18623 : parser->context->object_type);
18625 /* Look up the name. */
18626 decl = cp_parser_lookup_name (parser, identifier,
18627 tag_type,
18628 /*is_template=*/true,
18629 /*is_namespace=*/false,
18630 check_dependency_p,
18631 /*ambiguous_decls=*/NULL,
18632 token->location);
18634 decl = strip_using_decl (decl);
18636 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18637 template-argument-list if it follows a name that is not a
18638 conversion-function-id and
18639 - that follows the keyword template or a ~ after a nested-name-specifier or
18640 in a class member access expression, or
18641 - for which name lookup finds the injected-class-name of a class template
18642 or finds any declaration of a template, or
18643 - that is an unqualified name for which name lookup either finds one or
18644 more functions or finds nothing, or
18645 - that is a terminal name in a using-declarator (9.9), in a declarator-id
18646 (9.3.4), or in a type-only context other than a nested-name-specifier
18647 (13.8). */
18649 /* Handle injected-class-name. */
18650 decl = maybe_get_template_decl_from_type_decl (decl);
18652 /* If DECL is a template, then the name was a template-name. */
18653 if (TREE_CODE (decl) == TEMPLATE_DECL)
18655 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
18656 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
18658 tree d = DECL_TEMPLATE_RESULT (decl);
18659 tree attr;
18660 if (TREE_CODE (d) == TYPE_DECL)
18661 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
18662 else
18663 attr = DECL_ATTRIBUTES (d);
18664 if (TREE_UNAVAILABLE (decl))
18666 attr = lookup_attribute ("unavailable", attr);
18667 error_unavailable_use (decl, attr);
18669 else if (TREE_DEPRECATED (decl)
18670 && deprecated_state != DEPRECATED_SUPPRESS)
18672 attr = lookup_attribute ("deprecated", attr);
18673 warn_deprecated_use (decl, attr);
18677 else
18679 /* Look through an overload set for any templates. */
18680 bool found = false;
18682 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
18683 !found && iter; ++iter)
18684 if (TREE_CODE (*iter) == TEMPLATE_DECL)
18685 found = true;
18687 /* "an unqualified name for which name lookup either finds one or more
18688 functions or finds nothing". */
18689 if (!found
18690 && (cxx_dialect > cxx17)
18691 && !scope
18692 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
18693 && tag_type == none_type)
18695 /* The "more functions" case. Just use the OVERLOAD as normally.
18696 We don't use is_overloaded_fn here to avoid considering
18697 BASELINKs. */
18698 if (TREE_CODE (decl) == OVERLOAD
18699 /* Name lookup found one function. */
18700 || TREE_CODE (decl) == FUNCTION_DECL
18701 /* Name lookup found nothing. */
18702 || decl == error_mark_node)
18703 found = true;
18706 /* "that follows the keyword template"..."in a type-only context" */
18707 if (!found && scope
18708 && (template_keyword_p || tag_type != none_type)
18709 && dependentish_scope_p (scope)
18710 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18711 found = true;
18713 if (!found)
18715 /* The name does not name a template. */
18716 cp_parser_error (parser, "expected template-name");
18717 return error_mark_node;
18719 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
18720 || TREE_CODE (decl) == USING_DECL
18721 /* cp_parser_template_id can only handle some TYPE_DECLs. */
18722 || (TREE_CODE (decl) == TYPE_DECL
18723 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
18724 /* Repeat the lookup at instantiation time. */
18725 decl = identifier;
18728 return decl;
18731 /* Parse a template-argument-list.
18733 template-argument-list:
18734 template-argument ... [opt]
18735 template-argument-list , template-argument ... [opt]
18737 Returns a TREE_VEC containing the arguments. */
18739 static tree
18740 cp_parser_template_argument_list (cp_parser* parser)
18742 bool saved_in_template_argument_list_p;
18743 bool saved_ice_p;
18744 bool saved_non_ice_p;
18746 /* Don't create location wrapper nodes within a template-argument-list. */
18747 auto_suppress_location_wrappers sentinel;
18749 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
18750 parser->in_template_argument_list_p = true;
18751 /* Even if the template-id appears in an integral
18752 constant-expression, the contents of the argument list do
18753 not. */
18754 saved_ice_p = parser->integral_constant_expression_p;
18755 parser->integral_constant_expression_p = false;
18756 saved_non_ice_p = parser->non_integral_constant_expression_p;
18757 parser->non_integral_constant_expression_p = false;
18759 /* Parse the arguments. */
18760 auto_vec<tree, 10> args;
18763 if (!args.is_empty ())
18764 /* Consume the comma. */
18765 cp_lexer_consume_token (parser->lexer);
18767 /* Parse the template-argument. */
18768 tree argument = cp_parser_template_argument (parser);
18770 /* If the next token is an ellipsis, we're expanding a template
18771 argument pack. */
18772 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18774 if (argument == error_mark_node)
18776 cp_token *token = cp_lexer_peek_token (parser->lexer);
18777 error_at (token->location,
18778 "expected parameter pack before %<...%>");
18780 /* Consume the `...' token. */
18781 cp_lexer_consume_token (parser->lexer);
18783 /* Make the argument into a TYPE_PACK_EXPANSION or
18784 EXPR_PACK_EXPANSION. */
18785 argument = make_pack_expansion (argument);
18788 args.safe_push (argument);
18790 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18792 int n_args = args.length ();
18793 tree vec = make_tree_vec (n_args);
18795 for (int i = 0; i < n_args; i++)
18796 TREE_VEC_ELT (vec, i) = args[i];
18798 parser->non_integral_constant_expression_p = saved_non_ice_p;
18799 parser->integral_constant_expression_p = saved_ice_p;
18800 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
18801 if (CHECKING_P)
18802 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
18803 return vec;
18806 /* Parse a template-argument.
18808 template-argument:
18809 assignment-expression
18810 type-id
18811 id-expression
18813 The representation is that of an assignment-expression, type-id, or
18814 id-expression -- except that the qualified id-expression is
18815 evaluated, so that the value returned is either a DECL or an
18816 OVERLOAD.
18818 Although the standard says "assignment-expression", it forbids
18819 throw-expressions or assignments in the template argument.
18820 Therefore, we use "conditional-expression" instead. */
18822 static tree
18823 cp_parser_template_argument (cp_parser* parser)
18825 tree argument;
18826 bool template_p;
18827 bool address_p;
18828 bool maybe_type_id = false;
18829 cp_token *token = NULL, *argument_start_token = NULL;
18830 location_t loc = 0;
18831 cp_id_kind idk;
18833 /* There's really no way to know what we're looking at, so we just
18834 try each alternative in order.
18836 [temp.arg]
18838 In a template-argument, an ambiguity between a type-id and an
18839 expression is resolved to a type-id, regardless of the form of
18840 the corresponding template-parameter.
18842 Therefore, we try a type-id first. */
18843 cp_parser_parse_tentatively (parser);
18844 argument = cp_parser_template_type_arg (parser);
18845 /* If there was no error parsing the type-id but the next token is a
18846 '>>', our behavior depends on which dialect of C++ we're
18847 parsing. In C++98, we probably found a typo for '> >'. But there
18848 are type-id which are also valid expressions. For instance:
18850 struct X { int operator >> (int); };
18851 template <int V> struct Foo {};
18852 Foo<X () >> 5> r;
18854 Here 'X()' is a valid type-id of a function type, but the user just
18855 wanted to write the expression "X() >> 5". Thus, we remember that we
18856 found a valid type-id, but we still try to parse the argument as an
18857 expression to see what happens.
18859 In C++0x, the '>>' will be considered two separate '>'
18860 tokens. */
18861 if (!cp_parser_error_occurred (parser)
18862 && ((cxx_dialect == cxx98
18863 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18864 /* Similarly for >= which
18865 cp_parser_next_token_ends_template_argument_p treats for
18866 diagnostics purposes as mistyped > =, but can be valid
18867 after a type-id. */
18868 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
18870 maybe_type_id = true;
18871 cp_parser_abort_tentative_parse (parser);
18873 else
18875 /* If the next token isn't a `,' or a `>', then this argument wasn't
18876 really finished. This means that the argument is not a valid
18877 type-id. */
18878 if (!cp_parser_next_token_ends_template_argument_p (parser))
18879 cp_parser_error (parser, "expected template-argument");
18880 /* If that worked, we're done. */
18881 if (cp_parser_parse_definitely (parser))
18882 return argument;
18884 /* We're still not sure what the argument will be. */
18885 cp_parser_parse_tentatively (parser);
18886 /* Try a template. */
18887 argument_start_token = cp_lexer_peek_token (parser->lexer);
18888 argument = cp_parser_id_expression (parser,
18889 /*template_keyword_p=*/false,
18890 /*check_dependency_p=*/true,
18891 &template_p,
18892 /*declarator_p=*/false,
18893 /*optional_p=*/false);
18894 /* If the next token isn't a `,' or a `>', then this argument wasn't
18895 really finished. */
18896 if (!cp_parser_next_token_ends_template_argument_p (parser))
18897 cp_parser_error (parser, "expected template-argument");
18898 if (!cp_parser_error_occurred (parser))
18900 /* Figure out what is being referred to. If the id-expression
18901 was for a class template specialization, then we will have a
18902 TYPE_DECL at this point. There is no need to do name lookup
18903 at this point in that case. */
18904 if (TREE_CODE (argument) != TYPE_DECL)
18905 argument = cp_parser_lookup_name (parser, argument,
18906 none_type,
18907 /*is_template=*/template_p,
18908 /*is_namespace=*/false,
18909 /*check_dependency=*/true,
18910 /*ambiguous_decls=*/NULL,
18911 argument_start_token->location);
18912 if (TREE_CODE (argument) != TEMPLATE_DECL
18913 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
18914 cp_parser_error (parser, "expected template-name");
18916 if (cp_parser_parse_definitely (parser))
18918 if (TREE_UNAVAILABLE (argument))
18919 error_unavailable_use (argument, NULL_TREE);
18920 else if (TREE_DEPRECATED (argument))
18921 warn_deprecated_use (argument, NULL_TREE);
18922 return argument;
18924 /* It must be a non-type argument. In C++17 any constant-expression is
18925 allowed. */
18926 if (cxx_dialect > cxx14)
18927 goto general_expr;
18929 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
18931 -- an integral constant-expression of integral or enumeration
18932 type; or
18934 -- the name of a non-type template-parameter; or
18936 -- the name of an object or function with external linkage...
18938 -- the address of an object or function with external linkage...
18940 -- a pointer to member... */
18941 /* Look for a non-type template parameter. */
18942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18944 cp_parser_parse_tentatively (parser);
18945 argument = cp_parser_primary_expression (parser,
18946 /*address_p=*/false,
18947 /*cast_p=*/false,
18948 /*template_arg_p=*/true,
18949 &idk);
18950 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
18951 || !cp_parser_next_token_ends_template_argument_p (parser))
18952 cp_parser_simulate_error (parser);
18953 if (cp_parser_parse_definitely (parser))
18954 return argument;
18957 /* If the next token is "&", the argument must be the address of an
18958 object or function with external linkage. */
18959 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
18960 if (address_p)
18962 loc = cp_lexer_peek_token (parser->lexer)->location;
18963 cp_lexer_consume_token (parser->lexer);
18965 /* See if we might have an id-expression. */
18966 token = cp_lexer_peek_token (parser->lexer);
18967 if (token->type == CPP_NAME
18968 || token->keyword == RID_OPERATOR
18969 || token->type == CPP_SCOPE
18970 || token->type == CPP_TEMPLATE_ID
18971 || token->type == CPP_NESTED_NAME_SPECIFIER)
18973 cp_parser_parse_tentatively (parser);
18974 argument = cp_parser_primary_expression (parser,
18975 address_p,
18976 /*cast_p=*/false,
18977 /*template_arg_p=*/true,
18978 &idk);
18979 if (cp_parser_error_occurred (parser)
18980 || !cp_parser_next_token_ends_template_argument_p (parser))
18981 cp_parser_abort_tentative_parse (parser);
18982 else
18984 tree probe;
18986 if (INDIRECT_REF_P (argument))
18988 /* Strip the dereference temporarily. */
18989 gcc_assert (REFERENCE_REF_P (argument));
18990 argument = TREE_OPERAND (argument, 0);
18993 /* If we're in a template, we represent a qualified-id referring
18994 to a static data member as a SCOPE_REF even if the scope isn't
18995 dependent so that we can check access control later. */
18996 probe = argument;
18997 if (TREE_CODE (probe) == SCOPE_REF)
18998 probe = TREE_OPERAND (probe, 1);
18999 if (VAR_P (probe))
19001 /* A variable without external linkage might still be a
19002 valid constant-expression, so no error is issued here
19003 if the external-linkage check fails. */
19004 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
19005 cp_parser_simulate_error (parser);
19007 else if (is_overloaded_fn (argument))
19008 /* All overloaded functions are allowed; if the external
19009 linkage test does not pass, an error will be issued
19010 later. */
19012 else if (address_p
19013 && (TREE_CODE (argument) == OFFSET_REF
19014 || TREE_CODE (argument) == SCOPE_REF))
19015 /* A pointer-to-member. */
19017 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19019 else
19020 cp_parser_simulate_error (parser);
19022 if (cp_parser_parse_definitely (parser))
19024 if (address_p)
19025 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19026 NULL_TREE, tf_warning_or_error);
19027 else
19028 argument = convert_from_reference (argument);
19029 return argument;
19033 /* If the argument started with "&", there are no other valid
19034 alternatives at this point. */
19035 if (address_p)
19037 cp_parser_error (parser, "invalid non-type template argument");
19038 return error_mark_node;
19041 general_expr:
19042 /* If the argument wasn't successfully parsed as a type-id followed
19043 by '>>', the argument can only be a constant expression now.
19044 Otherwise, we try parsing the constant-expression tentatively,
19045 because the argument could really be a type-id. */
19046 if (maybe_type_id)
19047 cp_parser_parse_tentatively (parser);
19049 if (cxx_dialect <= cxx14)
19050 argument = cp_parser_constant_expression (parser);
19051 else
19053 /* In C++20, we can encounter a braced-init-list. */
19054 if (cxx_dialect >= cxx20
19055 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19057 bool expr_non_constant_p;
19058 return cp_parser_braced_list (parser, &expr_non_constant_p);
19061 /* With C++17 generalized non-type template arguments we need to handle
19062 lvalue constant expressions, too. */
19063 argument = cp_parser_assignment_expression (parser);
19064 require_potential_constant_expression (argument);
19067 if (!maybe_type_id)
19068 return argument;
19069 if (!cp_parser_next_token_ends_template_argument_p (parser))
19070 cp_parser_error (parser, "expected template-argument");
19071 if (cp_parser_parse_definitely (parser))
19072 return argument;
19073 /* We did our best to parse the argument as a non type-id, but that
19074 was the only alternative that matched (albeit with a '>' after
19075 it). We can assume it's just a typo from the user, and a
19076 diagnostic will then be issued. */
19077 return cp_parser_template_type_arg (parser);
19080 /* Parse an explicit-instantiation.
19082 explicit-instantiation:
19083 template declaration
19085 Although the standard says `declaration', what it really means is:
19087 explicit-instantiation:
19088 template decl-specifier-seq [opt] declarator [opt] ;
19090 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19091 supposed to be allowed. A defect report has been filed about this
19092 issue.
19094 GNU Extension:
19096 explicit-instantiation:
19097 storage-class-specifier template
19098 decl-specifier-seq [opt] declarator [opt] ;
19099 function-specifier template
19100 decl-specifier-seq [opt] declarator [opt] ; */
19102 static void
19103 cp_parser_explicit_instantiation (cp_parser* parser)
19105 int declares_class_or_enum;
19106 cp_decl_specifier_seq decl_specifiers;
19107 tree extension_specifier = NULL_TREE;
19109 timevar_push (TV_TEMPLATE_INST);
19111 /* Look for an (optional) storage-class-specifier or
19112 function-specifier. */
19113 if (cp_parser_allow_gnu_extensions_p (parser))
19115 extension_specifier
19116 = cp_parser_storage_class_specifier_opt (parser);
19117 if (!extension_specifier)
19118 extension_specifier
19119 = cp_parser_function_specifier_opt (parser,
19120 /*decl_specs=*/NULL);
19123 /* Look for the `template' keyword. */
19124 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19125 /* Let the front end know that we are processing an explicit
19126 instantiation. */
19127 begin_explicit_instantiation ();
19128 /* [temp.explicit] says that we are supposed to ignore access
19129 control while processing explicit instantiation directives. */
19130 push_deferring_access_checks (dk_no_check);
19131 /* Parse a decl-specifier-seq. */
19132 cp_parser_decl_specifier_seq (parser,
19133 CP_PARSER_FLAGS_OPTIONAL,
19134 &decl_specifiers,
19135 &declares_class_or_enum);
19137 cp_omp_declare_simd_data odsd;
19138 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19139 cp_parser_handle_directive_omp_attributes (parser,
19140 &decl_specifiers.attributes,
19141 &odsd, true);
19143 /* If there was exactly one decl-specifier, and it declared a class,
19144 and there's no declarator, then we have an explicit type
19145 instantiation. */
19146 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19148 tree type = check_tag_decl (&decl_specifiers,
19149 /*explicit_type_instantiation_p=*/true);
19150 /* Turn access control back on for names used during
19151 template instantiation. */
19152 pop_deferring_access_checks ();
19153 if (type)
19154 do_type_instantiation (type, extension_specifier,
19155 /*complain=*/tf_error);
19157 else
19159 cp_declarator *declarator;
19160 tree decl;
19162 /* Parse the declarator. */
19163 declarator
19164 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19165 CP_PARSER_FLAGS_NONE,
19166 /*ctor_dtor_or_conv_p=*/NULL,
19167 /*parenthesized_p=*/NULL,
19168 /*member_p=*/false,
19169 /*friend_p=*/false,
19170 /*static_p=*/false);
19171 if (declares_class_or_enum & 2)
19172 cp_parser_check_for_definition_in_return_type (declarator,
19173 decl_specifiers.type,
19174 decl_specifiers.locations[ds_type_spec]);
19175 if (declarator != cp_error_declarator)
19177 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19178 permerror (decl_specifiers.locations[ds_inline],
19179 "explicit instantiation shall not use"
19180 " %<inline%> specifier");
19181 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19182 permerror (decl_specifiers.locations[ds_constexpr],
19183 "explicit instantiation shall not use"
19184 " %<constexpr%> specifier");
19185 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19186 permerror (decl_specifiers.locations[ds_consteval],
19187 "explicit instantiation shall not use"
19188 " %<consteval%> specifier");
19190 decl = grokdeclarator (declarator, &decl_specifiers,
19191 NORMAL, 0, &decl_specifiers.attributes);
19192 /* Turn access control back on for names used during
19193 template instantiation. */
19194 pop_deferring_access_checks ();
19195 /* Do the explicit instantiation. */
19196 do_decl_instantiation (decl, extension_specifier);
19198 else
19200 pop_deferring_access_checks ();
19201 /* Skip the body of the explicit instantiation. */
19202 cp_parser_skip_to_end_of_statement (parser);
19205 /* We're done with the instantiation. */
19206 end_explicit_instantiation ();
19208 cp_parser_consume_semicolon_at_end_of_statement (parser);
19210 timevar_pop (TV_TEMPLATE_INST);
19212 cp_finalize_omp_declare_simd (parser, &odsd);
19215 /* Parse an explicit-specialization.
19217 explicit-specialization:
19218 template < > declaration
19220 Although the standard says `declaration', what it really means is:
19222 explicit-specialization:
19223 template <> decl-specifier [opt] init-declarator [opt] ;
19224 template <> function-definition
19225 template <> explicit-specialization
19226 template <> template-declaration */
19228 static void
19229 cp_parser_explicit_specialization (cp_parser* parser)
19231 cp_token *token = cp_lexer_peek_token (parser->lexer);
19233 /* Look for the `template' keyword. */
19234 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19235 /* Look for the `<'. */
19236 cp_parser_require (parser, CPP_LESS, RT_LESS);
19237 /* Look for the `>'. */
19238 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19239 /* We have processed another parameter list. */
19240 ++parser->num_template_parameter_lists;
19242 /* [temp]
19244 A template ... explicit specialization ... shall not have C
19245 linkage. */
19246 bool need_lang_pop = current_lang_name == lang_name_c;
19247 if (need_lang_pop)
19249 error_at (token->location, "template specialization with C linkage");
19250 maybe_show_extern_c_location ();
19252 /* Give it C++ linkage to avoid confusing other parts of the
19253 front end. */
19254 push_lang_context (lang_name_cplusplus);
19257 /* Let the front end know that we are beginning a specialization. */
19258 if (begin_specialization ())
19260 /* If the next keyword is `template', we need to figure out
19261 whether or not we're looking a template-declaration. */
19262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19264 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19265 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19266 cp_parser_template_declaration_after_export (parser,
19267 /*member_p=*/false);
19268 else
19269 cp_parser_explicit_specialization (parser);
19271 else
19272 /* Parse the dependent declaration. */
19273 cp_parser_single_declaration (parser,
19274 /*checks=*/NULL,
19275 /*member_p=*/false,
19276 /*explicit_specialization_p=*/true,
19277 /*friend_p=*/NULL);
19280 /* We're done with the specialization. */
19281 end_specialization ();
19283 /* For the erroneous case of a template with C linkage, we pushed an
19284 implicit C++ linkage scope; exit that scope now. */
19285 if (need_lang_pop)
19286 pop_lang_context ();
19288 /* We're done with this parameter list. */
19289 --parser->num_template_parameter_lists;
19292 /* Preserve the attributes across a garbage collect (by making it a GC
19293 root), which can occur when parsing a member function. */
19295 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19297 /* Parse a type-specifier.
19299 type-specifier:
19300 simple-type-specifier
19301 class-specifier
19302 enum-specifier
19303 elaborated-type-specifier
19304 cv-qualifier
19306 GNU Extension:
19308 type-specifier:
19309 __complex__
19311 Returns a representation of the type-specifier. For a
19312 class-specifier, enum-specifier, or elaborated-type-specifier, a
19313 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19315 The parser flags FLAGS is used to control type-specifier parsing.
19317 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19318 in a decl-specifier-seq.
19320 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19321 class-specifier, enum-specifier, or elaborated-type-specifier, then
19322 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19323 if a type is declared; 2 if it is defined. Otherwise, it is set to
19324 zero.
19326 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19327 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19328 is set to FALSE. */
19330 static tree
19331 cp_parser_type_specifier (cp_parser* parser,
19332 cp_parser_flags flags,
19333 cp_decl_specifier_seq *decl_specs,
19334 bool is_declaration,
19335 int* declares_class_or_enum,
19336 bool* is_cv_qualifier)
19338 tree type_spec = NULL_TREE;
19339 cp_token *token;
19340 enum rid keyword;
19341 cp_decl_spec ds = ds_last;
19343 /* Assume this type-specifier does not declare a new type. */
19344 if (declares_class_or_enum)
19345 *declares_class_or_enum = 0;
19346 /* And that it does not specify a cv-qualifier. */
19347 if (is_cv_qualifier)
19348 *is_cv_qualifier = false;
19349 /* Peek at the next token. */
19350 token = cp_lexer_peek_token (parser->lexer);
19352 /* If we're looking at a keyword, we can use that to guide the
19353 production we choose. */
19354 keyword = token->keyword;
19355 switch (keyword)
19357 case RID_ENUM:
19358 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19359 goto elaborated_type_specifier;
19361 /* Look for the enum-specifier. */
19362 type_spec = cp_parser_enum_specifier (parser);
19363 /* If that worked, we're done. */
19364 if (type_spec)
19366 if (declares_class_or_enum)
19367 *declares_class_or_enum = 2;
19368 if (decl_specs)
19369 cp_parser_set_decl_spec_type (decl_specs,
19370 type_spec,
19371 token,
19372 /*type_definition_p=*/true);
19373 return type_spec;
19375 else
19376 goto elaborated_type_specifier;
19378 /* Any of these indicate either a class-specifier, or an
19379 elaborated-type-specifier. */
19380 case RID_CLASS:
19381 case RID_STRUCT:
19382 case RID_UNION:
19383 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19384 goto elaborated_type_specifier;
19386 /* Parse tentatively so that we can back up if we don't find a
19387 class-specifier. */
19388 cp_parser_parse_tentatively (parser);
19389 if (decl_specs->attributes)
19390 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19391 /* Look for the class-specifier. */
19392 type_spec = cp_parser_class_specifier (parser);
19393 if (decl_specs->attributes)
19394 cp_parser_decl_specs_attrs->pop ();
19395 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19396 /* If that worked, we're done. */
19397 if (cp_parser_parse_definitely (parser))
19399 if (declares_class_or_enum)
19400 *declares_class_or_enum = 2;
19401 if (decl_specs)
19402 cp_parser_set_decl_spec_type (decl_specs,
19403 type_spec,
19404 token,
19405 /*type_definition_p=*/true);
19406 return type_spec;
19409 /* Fall through. */
19410 elaborated_type_specifier:
19411 /* We're declaring (not defining) a class or enum. */
19412 if (declares_class_or_enum)
19413 *declares_class_or_enum = 1;
19415 /* Fall through. */
19416 case RID_TYPENAME:
19417 /* Look for an elaborated-type-specifier. */
19418 type_spec
19419 = (cp_parser_elaborated_type_specifier
19420 (parser,
19421 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19422 is_declaration));
19423 if (decl_specs)
19424 cp_parser_set_decl_spec_type (decl_specs,
19425 type_spec,
19426 token,
19427 /*type_definition_p=*/false);
19428 return type_spec;
19430 case RID_CONST:
19431 ds = ds_const;
19432 if (is_cv_qualifier)
19433 *is_cv_qualifier = true;
19434 break;
19436 case RID_VOLATILE:
19437 ds = ds_volatile;
19438 if (is_cv_qualifier)
19439 *is_cv_qualifier = true;
19440 break;
19442 case RID_RESTRICT:
19443 ds = ds_restrict;
19444 if (is_cv_qualifier)
19445 *is_cv_qualifier = true;
19446 break;
19448 case RID_COMPLEX:
19449 /* The `__complex__' keyword is a GNU extension. */
19450 ds = ds_complex;
19451 break;
19453 default:
19454 break;
19457 /* Handle simple keywords. */
19458 if (ds != ds_last)
19460 if (decl_specs)
19462 set_and_check_decl_spec_loc (decl_specs, ds, token);
19463 decl_specs->any_specifiers_p = true;
19465 return cp_lexer_consume_token (parser->lexer)->u.value;
19468 /* If we do not already have a type-specifier, assume we are looking
19469 at a simple-type-specifier. */
19470 type_spec = cp_parser_simple_type_specifier (parser,
19471 decl_specs,
19472 flags);
19474 /* If we didn't find a type-specifier, and a type-specifier was not
19475 optional in this context, issue an error message. */
19476 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19478 cp_parser_error (parser, "expected type specifier");
19479 return error_mark_node;
19482 return type_spec;
19485 /* Parse a simple-type-specifier.
19487 simple-type-specifier:
19488 :: [opt] nested-name-specifier [opt] type-name
19489 :: [opt] nested-name-specifier template template-id
19490 char
19491 wchar_t
19492 bool
19493 short
19495 long
19496 signed
19497 unsigned
19498 float
19499 double
19500 void
19502 C++11 Extension:
19504 simple-type-specifier:
19505 auto
19506 decltype ( expression )
19507 char16_t
19508 char32_t
19509 __underlying_type ( type-id )
19511 C++17 extension:
19513 nested-name-specifier(opt) template-name
19515 GNU Extension:
19517 simple-type-specifier:
19518 __int128
19519 __typeof__ unary-expression
19520 __typeof__ ( type-id )
19521 __typeof__ ( type-id ) { initializer-list , [opt] }
19523 Concepts Extension:
19525 simple-type-specifier:
19526 constrained-type-specifier
19528 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
19529 appropriately updated. */
19531 static tree
19532 cp_parser_simple_type_specifier (cp_parser* parser,
19533 cp_decl_specifier_seq *decl_specs,
19534 cp_parser_flags flags)
19536 tree type = NULL_TREE;
19537 cp_token *token;
19538 int idx;
19540 /* Peek at the next token. */
19541 token = cp_lexer_peek_token (parser->lexer);
19543 /* If we're looking at a keyword, things are easy. */
19544 switch (token->keyword)
19546 case RID_CHAR:
19547 if (decl_specs)
19548 decl_specs->explicit_char_p = true;
19549 type = char_type_node;
19550 break;
19551 case RID_CHAR8:
19552 type = char8_type_node;
19553 break;
19554 case RID_CHAR16:
19555 type = char16_type_node;
19556 break;
19557 case RID_CHAR32:
19558 type = char32_type_node;
19559 break;
19560 case RID_WCHAR:
19561 type = wchar_type_node;
19562 break;
19563 case RID_BOOL:
19564 type = boolean_type_node;
19565 break;
19566 case RID_SHORT:
19567 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
19568 type = short_integer_type_node;
19569 break;
19570 case RID_INT:
19571 if (decl_specs)
19572 decl_specs->explicit_int_p = true;
19573 type = integer_type_node;
19574 break;
19575 case RID_INT_N_0:
19576 case RID_INT_N_1:
19577 case RID_INT_N_2:
19578 case RID_INT_N_3:
19579 idx = token->keyword - RID_INT_N_0;
19580 if (! int_n_enabled_p [idx])
19581 break;
19582 if (decl_specs)
19584 decl_specs->explicit_intN_p = true;
19585 decl_specs->int_n_idx = idx;
19586 /* Check if the alternate "__intN__" form has been used instead of
19587 "__intN". */
19588 if (startswith (IDENTIFIER_POINTER (token->u.value)
19589 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
19590 decl_specs->int_n_alt = true;
19592 type = int_n_trees [idx].signed_type;
19593 break;
19594 case RID_LONG:
19595 if (decl_specs)
19596 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
19597 type = long_integer_type_node;
19598 break;
19599 case RID_SIGNED:
19600 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
19601 type = integer_type_node;
19602 break;
19603 case RID_UNSIGNED:
19604 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
19605 type = unsigned_type_node;
19606 break;
19607 case RID_FLOAT:
19608 type = float_type_node;
19609 break;
19610 case RID_DOUBLE:
19611 type = double_type_node;
19612 break;
19613 case RID_VOID:
19614 type = void_type_node;
19615 break;
19617 case RID_AUTO:
19618 maybe_warn_cpp0x (CPP0X_AUTO);
19619 if (parser->auto_is_implicit_function_template_parm_p)
19621 /* The 'auto' might be the placeholder return type for a function decl
19622 with trailing return type. */
19623 bool have_trailing_return_fn_decl = false;
19625 cp_parser_parse_tentatively (parser);
19626 cp_lexer_consume_token (parser->lexer);
19627 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
19628 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
19629 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19630 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
19632 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19634 cp_lexer_consume_token (parser->lexer);
19635 cp_parser_skip_to_closing_parenthesis (parser,
19636 /*recovering*/false,
19637 /*or_comma*/false,
19638 /*consume_paren*/true);
19639 continue;
19642 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
19644 have_trailing_return_fn_decl = true;
19645 break;
19648 cp_lexer_consume_token (parser->lexer);
19650 cp_parser_abort_tentative_parse (parser);
19652 if (have_trailing_return_fn_decl)
19654 type = make_auto ();
19655 break;
19658 if (cxx_dialect >= cxx14)
19660 type = synthesize_implicit_template_parm (parser, NULL_TREE);
19661 type = TREE_TYPE (type);
19663 else
19664 type = error_mark_node;
19666 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
19668 if (cxx_dialect < cxx14)
19669 error_at (token->location,
19670 "use of %<auto%> in lambda parameter declaration "
19671 "only available with "
19672 "%<-std=c++14%> or %<-std=gnu++14%>");
19674 else if (cxx_dialect < cxx14)
19675 error_at (token->location,
19676 "use of %<auto%> in parameter declaration "
19677 "only available with "
19678 "%<-std=c++14%> or %<-std=gnu++14%>");
19679 else if (!flag_concepts)
19680 pedwarn (token->location, 0,
19681 "use of %<auto%> in parameter declaration "
19682 "only available with %<-std=c++20%> or %<-fconcepts%>");
19684 else
19685 type = make_auto ();
19686 break;
19688 case RID_DECLTYPE:
19689 /* Since DR 743, decltype can either be a simple-type-specifier by
19690 itself or begin a nested-name-specifier. Parsing it will replace
19691 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19692 handling below decide what to do. */
19693 cp_parser_decltype (parser);
19694 cp_lexer_set_token_position (parser->lexer, token);
19695 break;
19697 case RID_TYPEOF:
19698 /* Consume the `typeof' token. */
19699 cp_lexer_consume_token (parser->lexer);
19700 /* Parse the operand to `typeof'. */
19701 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
19702 /* If it is not already a TYPE, take its type. */
19703 if (!TYPE_P (type))
19704 type = finish_typeof (type);
19706 if (decl_specs)
19707 cp_parser_set_decl_spec_type (decl_specs, type,
19708 token,
19709 /*type_definition_p=*/false);
19711 return type;
19713 case RID_UNDERLYING_TYPE:
19714 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
19715 if (decl_specs)
19716 cp_parser_set_decl_spec_type (decl_specs, type,
19717 token,
19718 /*type_definition_p=*/false);
19720 return type;
19722 case RID_BASES:
19723 case RID_DIRECT_BASES:
19724 type = cp_parser_trait_expr (parser, token->keyword);
19725 if (decl_specs)
19726 cp_parser_set_decl_spec_type (decl_specs, type,
19727 token,
19728 /*type_definition_p=*/false);
19729 return type;
19730 default:
19731 break;
19734 /* If token is an already-parsed decltype not followed by ::,
19735 it's a simple-type-specifier. */
19736 if (token->type == CPP_DECLTYPE
19737 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
19739 type = saved_checks_value (token->u.tree_check_value);
19740 if (decl_specs)
19742 cp_parser_set_decl_spec_type (decl_specs, type,
19743 token,
19744 /*type_definition_p=*/false);
19745 /* Remember that we are handling a decltype in order to
19746 implement the resolution of DR 1510 when the argument
19747 isn't instantiation dependent. */
19748 decl_specs->decltype_p = true;
19750 cp_lexer_consume_token (parser->lexer);
19751 return type;
19754 /* If the type-specifier was for a built-in type, we're done. */
19755 if (type)
19757 /* Record the type. */
19758 if (decl_specs
19759 && (token->keyword != RID_SIGNED
19760 && token->keyword != RID_UNSIGNED
19761 && token->keyword != RID_SHORT
19762 && token->keyword != RID_LONG))
19763 cp_parser_set_decl_spec_type (decl_specs,
19764 type,
19765 token,
19766 /*type_definition_p=*/false);
19767 if (decl_specs)
19768 decl_specs->any_specifiers_p = true;
19770 /* Consume the token. */
19771 cp_lexer_consume_token (parser->lexer);
19773 if (type == error_mark_node)
19774 return error_mark_node;
19776 /* There is no valid C++ program where a non-template type is
19777 followed by a "<". That usually indicates that the user thought
19778 that the type was a template. */
19779 cp_parser_check_for_invalid_template_id (parser, type, none_type,
19780 token->location);
19782 return TYPE_NAME (type);
19785 /* The type-specifier must be a user-defined type. */
19786 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
19788 bool qualified_p;
19789 bool global_p;
19790 const bool typename_p = (cxx_dialect >= cxx20
19791 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
19793 /* Don't gobble tokens or issue error messages if this is an
19794 optional type-specifier. */
19795 if (flags & CP_PARSER_FLAGS_OPTIONAL)
19796 cp_parser_parse_tentatively (parser);
19798 /* Remember current tentative parsing state -- if we know we need
19799 a type, we can give better diagnostics here. */
19800 bool tent = cp_parser_parsing_tentatively (parser);
19802 token = cp_lexer_peek_token (parser->lexer);
19804 /* Look for the optional `::' operator. */
19805 global_p
19806 = (cp_parser_global_scope_opt (parser,
19807 /*current_scope_valid_p=*/false)
19808 != NULL_TREE);
19809 /* Look for the nested-name specifier. */
19810 qualified_p
19811 = (cp_parser_nested_name_specifier_opt (parser,
19812 /*typename_keyword_p=*/false,
19813 /*check_dependency_p=*/true,
19814 /*type_p=*/false,
19815 /*is_declaration=*/false)
19816 != NULL_TREE);
19817 /* If we have seen a nested-name-specifier, and the next token
19818 is `template', then we are using the template-id production. */
19819 if (parser->scope
19820 && cp_parser_optional_template_keyword (parser))
19822 /* Look for the template-id. */
19823 type = cp_parser_template_id (parser,
19824 /*template_keyword_p=*/true,
19825 /*check_dependency_p=*/true,
19826 none_type,
19827 /*is_declaration=*/false);
19828 /* If the template-id did not name a type, we are out of
19829 luck. */
19830 if (TREE_CODE (type) != TYPE_DECL)
19832 /* ...unless we pretend we have seen 'typename'. */
19833 if (typename_p)
19834 type = cp_parser_make_typename_type (parser, type,
19835 token->location);
19836 else
19838 cp_parser_error (parser, "expected template-id for type");
19839 type = error_mark_node;
19843 /* DR 1812: A < following a qualified-id in a typename-specifier
19844 could safely be assumed to begin a template argument list, so
19845 the template keyword should be optional. */
19846 else if (parser->scope
19847 && qualified_p
19848 && typename_p
19849 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
19851 cp_parser_parse_tentatively (parser);
19853 type = cp_parser_template_id (parser,
19854 /*template_keyword_p=*/true,
19855 /*check_dependency_p=*/true,
19856 none_type,
19857 /*is_declaration=*/false);
19858 /* This is handled below, so back off. */
19859 if (type && concept_check_p (type))
19860 cp_parser_simulate_error (parser);
19862 if (!cp_parser_parse_definitely (parser))
19863 type = NULL_TREE;
19864 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
19865 type = make_typename_type (parser->scope, type, typename_type,
19866 /*complain=*/tf_error);
19867 else if (TREE_CODE (type) != TYPE_DECL)
19868 type = NULL_TREE;
19871 /* Otherwise, look for a type-name. */
19872 if (!type)
19874 if (cxx_dialect >= cxx17)
19875 cp_parser_parse_tentatively (parser);
19877 type = cp_parser_type_name (parser, (qualified_p && typename_p));
19879 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
19880 type = NULL_TREE;
19883 if (!type && flag_concepts && decl_specs)
19885 /* Try for a type-constraint with template arguments. We check
19886 decl_specs here to avoid trying this for a functional cast. */
19888 cp_parser_parse_tentatively (parser);
19890 type = cp_parser_template_id (parser,
19891 /*template_keyword_p=*/false,
19892 /*check_dependency_p=*/true,
19893 none_type,
19894 /*is_declaration=*/false);
19895 if (type && concept_check_p (type))
19897 location_t loc = EXPR_LOCATION (type);
19898 type = cp_parser_placeholder_type_specifier (parser, loc,
19899 type, tent);
19900 if (tent && type == error_mark_node)
19901 /* Perhaps it's a concept-check expression. */
19902 cp_parser_simulate_error (parser);
19904 else
19905 cp_parser_simulate_error (parser);
19907 if (!cp_parser_parse_definitely (parser))
19908 type = NULL_TREE;
19911 if (!type && cxx_dialect >= cxx17)
19913 /* Try class template argument deduction or type-constraint without
19914 template arguments. */
19915 tree name = cp_parser_identifier (parser);
19916 if (name && TREE_CODE (name) == IDENTIFIER_NODE
19917 && parser->scope != error_mark_node)
19919 location_t loc
19920 = cp_lexer_previous_token (parser->lexer)->location;
19921 tree tmpl = cp_parser_lookup_name (parser, name,
19922 none_type,
19923 /*is_template=*/false,
19924 /*is_namespace=*/false,
19925 /*check_dependency=*/true,
19926 /*ambiguous_decls=*/NULL,
19927 token->location);
19928 if (tmpl && tmpl != error_mark_node
19929 && ctad_template_p (tmpl))
19930 type = make_template_placeholder (tmpl);
19931 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
19932 type = cp_parser_placeholder_type_specifier (parser, loc,
19933 tmpl, tent);
19934 else
19936 type = error_mark_node;
19937 if (!cp_parser_simulate_error (parser))
19938 cp_parser_name_lookup_error (parser, name, tmpl,
19939 NLE_TYPE, token->location);
19942 else
19943 type = error_mark_node;
19946 /* If it didn't work out, we don't have a TYPE. */
19947 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
19948 && !cp_parser_parse_definitely (parser))
19949 type = NULL_TREE;
19951 /* Keep track of all name-lookups performed in class scopes. */
19952 if (type
19953 && !global_p
19954 && !qualified_p
19955 && TREE_CODE (type) == TYPE_DECL
19956 && identifier_p (DECL_NAME (type)))
19957 maybe_note_name_used_in_class (DECL_NAME (type), type);
19959 if (type && decl_specs)
19960 cp_parser_set_decl_spec_type (decl_specs, type,
19961 token,
19962 /*type_definition_p=*/false);
19965 /* If we didn't get a type-name, issue an error message. */
19966 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19968 cp_parser_error (parser, "expected type-name");
19969 return error_mark_node;
19972 if (type && type != error_mark_node)
19974 /* See if TYPE is an Objective-C type, and if so, parse and
19975 accept any protocol references following it. Do this before
19976 the cp_parser_check_for_invalid_template_id() call, because
19977 Objective-C types can be followed by '<...>' which would
19978 enclose protocol names rather than template arguments, and so
19979 everything is fine. */
19980 if (c_dialect_objc () && !parser->scope
19981 && (objc_is_id (type) || objc_is_class_name (type)))
19983 tree protos = cp_parser_objc_protocol_refs_opt (parser);
19984 tree qual_type = objc_get_protocol_qualified_type (type, protos);
19986 /* Clobber the "unqualified" type previously entered into
19987 DECL_SPECS with the new, improved protocol-qualified version. */
19988 if (decl_specs)
19989 decl_specs->type = qual_type;
19991 return qual_type;
19994 /* There is no valid C++ program where a non-template type is
19995 followed by a "<". That usually indicates that the user
19996 thought that the type was a template. */
19997 cp_parser_check_for_invalid_template_id (parser, type,
19998 none_type,
19999 token->location);
20002 return type;
20005 /* Parse the remainder of a placholder-type-specifier.
20007 placeholder-type-specifier:
20008 type-constraint_opt auto
20009 type-constraint_opt decltype(auto)
20011 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20012 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20013 parses the placeholder type, and performs some contextual syntactic analysis.
20015 LOC provides the location of the template name.
20017 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20018 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20019 might actually be a concept-check,
20021 Note that the Concepts TS allows the auto or decltype(auto) to be
20022 omitted in a constrained-type-specifier. */
20024 static tree
20025 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20026 tree tmpl, bool tentative)
20028 if (tmpl == error_mark_node)
20029 return error_mark_node;
20031 tree orig_tmpl = tmpl;
20033 /* Get the arguments as written for subsequent analysis. */
20034 tree args = NULL_TREE;
20035 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20037 args = TREE_OPERAND (tmpl, 1);
20038 tmpl = TREE_OPERAND (tmpl, 0);
20040 else
20041 /* A concept-name with no arguments can't be an expression. */
20042 tentative = false;
20044 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20046 /* Get the concept and prototype parameter for the constraint. */
20047 tree_pair info = finish_type_constraints (tmpl, args, complain);
20048 tree con = info.first;
20049 tree proto = info.second;
20050 if (con == error_mark_node)
20051 return error_mark_node;
20053 /* As per the standard, require auto or decltype(auto), except in some
20054 cases (template parameter lists, -fconcepts-ts enabled). */
20055 cp_token *placeholder = NULL, *close_paren = NULL;
20056 if (cxx_dialect >= cxx20)
20058 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20059 placeholder = cp_lexer_consume_token (parser->lexer);
20060 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20062 placeholder = cp_lexer_consume_token (parser->lexer);
20063 matching_parens parens;
20064 parens.require_open (parser);
20065 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20066 close_paren = parens.require_close (parser);
20070 /* A type constraint constrains a contextually determined type or type
20071 parameter pack. However, the Concepts TS does allow concepts
20072 to introduce non-type and template template parameters. */
20073 if (TREE_CODE (proto) != TYPE_DECL)
20075 if (!flag_concepts_ts
20076 || !processing_template_parmlist)
20078 if (!tentative)
20080 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20081 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20083 return error_mark_node;
20087 /* In a template parameter list, a type-parameter can be introduced
20088 by type-constraints alone. */
20089 if (processing_template_parmlist && !placeholder)
20091 /* In a default argument we may not be creating new parameters. */
20092 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20094 /* If this assert turns out to be false, do error() instead. */
20095 gcc_assert (tentative);
20096 return error_mark_node;
20098 return build_constrained_parameter (con, proto, args);
20101 /* Diagnose issues placeholder issues. */
20102 if (!flag_concepts_ts
20103 && !parser->in_result_type_constraint_p
20104 && !placeholder)
20106 if (tentative)
20107 /* Perhaps it's a concept-check expression (c++/91073). */
20108 return error_mark_node;
20110 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20111 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20112 error_at (input_location,
20113 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20114 /* Fall through. This is an error of omission. */
20116 else if (parser->in_result_type_constraint_p && placeholder)
20118 /* A trailing return type only allows type-constraints. */
20119 error_at (input_location,
20120 "unexpected placeholder in constrained result type");
20123 /* In a parameter-declaration-clause, a placeholder-type-specifier
20124 results in an invented template parameter. */
20125 if (parser->auto_is_implicit_function_template_parm_p)
20127 if (close_paren)
20129 location_t loc = make_location (placeholder->location,
20130 placeholder->location,
20131 close_paren->location);
20132 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20133 return error_mark_node;
20135 tree parm = build_constrained_parameter (con, proto, args);
20136 return synthesize_implicit_template_parm (parser, parm);
20139 /* Determine if the type should be deduced using template argument
20140 deduction or decltype deduction. Note that the latter is always
20141 used for type-constraints in trailing return types. */
20142 bool decltype_p = placeholder
20143 ? placeholder->keyword == RID_DECLTYPE
20144 : parser->in_result_type_constraint_p;
20146 /* Otherwise, this is the type of a variable or return type. */
20147 if (decltype_p)
20148 return make_constrained_decltype_auto (con, args);
20149 else
20150 return make_constrained_auto (con, args);
20153 /* Parse a type-name.
20155 type-name:
20156 class-name
20157 enum-name
20158 typedef-name
20159 simple-template-id [in c++0x]
20161 enum-name:
20162 identifier
20164 typedef-name:
20165 identifier
20167 Concepts:
20169 type-name:
20170 concept-name
20171 partial-concept-id
20173 concept-name:
20174 identifier
20176 Returns a TYPE_DECL for the type. */
20178 static tree
20179 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20181 tree type_decl;
20183 /* We can't know yet whether it is a class-name or not. */
20184 cp_parser_parse_tentatively (parser);
20185 /* Try a class-name. */
20186 type_decl = cp_parser_class_name (parser,
20187 typename_keyword_p,
20188 /*template_keyword_p=*/false,
20189 none_type,
20190 /*check_dependency_p=*/true,
20191 /*class_head_p=*/false,
20192 /*is_declaration=*/false);
20193 /* If it's not a class-name, keep looking. */
20194 if (!cp_parser_parse_definitely (parser))
20196 if (cxx_dialect < cxx11)
20197 /* It must be a typedef-name or an enum-name. */
20198 return cp_parser_nonclass_name (parser);
20200 cp_parser_parse_tentatively (parser);
20201 /* It is either a simple-template-id representing an
20202 instantiation of an alias template... */
20203 type_decl = cp_parser_template_id (parser,
20204 /*template_keyword_p=*/false,
20205 /*check_dependency_p=*/true,
20206 none_type,
20207 /*is_declaration=*/false);
20208 /* Note that this must be an instantiation of an alias template
20209 because [temp.names]/6 says:
20211 A template-id that names an alias template specialization
20212 is a type-name.
20214 Whereas [temp.names]/7 says:
20216 A simple-template-id that names a class template
20217 specialization is a class-name.
20219 With concepts, this could also be a partial-concept-id that
20220 declares a non-type template parameter. */
20221 if (type_decl != NULL_TREE
20222 && TREE_CODE (type_decl) == TYPE_DECL
20223 && TYPE_DECL_ALIAS_P (type_decl))
20224 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20225 else
20226 cp_parser_simulate_error (parser);
20228 if (!cp_parser_parse_definitely (parser))
20229 /* ... Or a typedef-name or an enum-name. */
20230 return cp_parser_nonclass_name (parser);
20233 return type_decl;
20236 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20237 or a concept-name.
20239 enum-name:
20240 identifier
20242 typedef-name:
20243 identifier
20245 concept-name:
20246 identifier
20248 Returns a TYPE_DECL for the type. */
20250 static tree
20251 cp_parser_nonclass_name (cp_parser* parser)
20253 tree type_decl;
20254 tree identifier;
20256 cp_token *token = cp_lexer_peek_token (parser->lexer);
20257 identifier = cp_parser_identifier (parser);
20258 if (identifier == error_mark_node)
20259 return error_mark_node;
20261 /* Look up the type-name. */
20262 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20264 type_decl = strip_using_decl (type_decl);
20266 if (TREE_CODE (type_decl) != TYPE_DECL
20267 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20269 /* See if this is an Objective-C type. */
20270 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20271 tree type = objc_get_protocol_qualified_type (identifier, protos);
20272 if (type)
20273 type_decl = TYPE_NAME (type);
20276 /* Issue an error if we did not find a type-name. */
20277 if (TREE_CODE (type_decl) != TYPE_DECL
20278 /* In Objective-C, we have the complication that class names are
20279 normally type names and start declarations (eg, the
20280 "NSObject" in "NSObject *object;"), but can be used in an
20281 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20282 is an expression. So, a classname followed by a dot is not a
20283 valid type-name. */
20284 || (objc_is_class_name (TREE_TYPE (type_decl))
20285 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20287 if (!cp_parser_simulate_error (parser))
20288 cp_parser_name_lookup_error (parser, identifier, type_decl,
20289 NLE_TYPE, token->location);
20290 return error_mark_node;
20292 /* Remember that the name was used in the definition of the
20293 current class so that we can check later to see if the
20294 meaning would have been different after the class was
20295 entirely defined. */
20296 else if (type_decl != error_mark_node
20297 && !parser->scope)
20298 maybe_note_name_used_in_class (identifier, type_decl);
20300 return type_decl;
20303 /* Parse an elaborated-type-specifier. Note that the grammar given
20304 here incorporates the resolution to DR68.
20306 elaborated-type-specifier:
20307 class-key :: [opt] nested-name-specifier [opt] identifier
20308 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20309 enum-key :: [opt] nested-name-specifier [opt] identifier
20310 typename :: [opt] nested-name-specifier identifier
20311 typename :: [opt] nested-name-specifier template [opt]
20312 template-id
20314 GNU extension:
20316 elaborated-type-specifier:
20317 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20318 class-key attributes :: [opt] nested-name-specifier [opt]
20319 template [opt] template-id
20320 enum attributes :: [opt] nested-name-specifier [opt] identifier
20322 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20323 declared `friend'. If IS_DECLARATION is TRUE, then this
20324 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20325 something is being declared.
20327 Returns the TYPE specified. */
20329 static tree
20330 cp_parser_elaborated_type_specifier (cp_parser* parser,
20331 bool is_friend,
20332 bool is_declaration)
20334 enum tag_types tag_type;
20335 tree identifier;
20336 tree type = NULL_TREE;
20337 tree attributes = NULL_TREE;
20338 tree globalscope;
20339 cp_token *token = NULL;
20341 /* For class and enum types the location of the class-key or enum-key. */
20342 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20343 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20344 rid scoped_key = RID_MAX;
20346 /* See if we're looking at the `enum' keyword. */
20347 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20349 /* Consume the `enum' token. */
20350 cp_lexer_consume_token (parser->lexer);
20351 /* Remember that it's an enumeration type. */
20352 tag_type = enum_type;
20353 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20354 enums) is used here. */
20355 cp_token *token = cp_lexer_peek_token (parser->lexer);
20356 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20357 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20359 location_t loc = token->location;
20360 gcc_rich_location richloc (loc);
20361 richloc.add_range (input_location);
20362 richloc.add_fixit_remove ();
20363 pedwarn (&richloc, 0, "elaborated-type-specifier for "
20364 "a scoped enum must not use the %qD keyword",
20365 token->u.value);
20366 /* Consume the `struct' or `class' and parse it anyway. */
20367 cp_lexer_consume_token (parser->lexer);
20368 /* Create a combined location for the whole scoped-enum-key. */
20369 key_loc = make_location (key_loc, key_loc, loc);
20371 else
20372 scoped_key = RID_MAX;
20374 /* Parse the attributes. */
20375 attributes = cp_parser_attributes_opt (parser);
20377 /* Or, it might be `typename'. */
20378 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20379 RID_TYPENAME))
20381 /* Consume the `typename' token. */
20382 cp_lexer_consume_token (parser->lexer);
20383 /* Remember that it's a `typename' type. */
20384 tag_type = typename_type;
20386 /* Otherwise it must be a class-key. */
20387 else
20389 key_loc = cp_lexer_peek_token (parser->lexer)->location;
20390 tag_type = cp_parser_class_key (parser);
20391 if (tag_type == none_type)
20392 return error_mark_node;
20393 /* Parse the attributes. */
20394 attributes = cp_parser_attributes_opt (parser);
20397 /* Look for the `::' operator. */
20398 globalscope = cp_parser_global_scope_opt (parser,
20399 /*current_scope_valid_p=*/false);
20400 /* Look for the nested-name-specifier. */
20401 tree nested_name_specifier;
20402 if (tag_type == typename_type && !globalscope)
20404 nested_name_specifier
20405 = cp_parser_nested_name_specifier (parser,
20406 /*typename_keyword_p=*/true,
20407 /*check_dependency_p=*/true,
20408 /*type_p=*/true,
20409 is_declaration);
20410 if (!nested_name_specifier)
20411 return error_mark_node;
20413 else
20414 /* Even though `typename' is not present, the proposed resolution
20415 to Core Issue 180 says that in `class A<T>::B', `B' should be
20416 considered a type-name, even if `A<T>' is dependent. */
20417 nested_name_specifier
20418 = cp_parser_nested_name_specifier_opt (parser,
20419 /*typename_keyword_p=*/true,
20420 /*check_dependency_p=*/true,
20421 /*type_p=*/true,
20422 is_declaration);
20423 /* For everything but enumeration types, consider a template-id.
20424 For an enumeration type, consider only a plain identifier. */
20425 if (tag_type != enum_type)
20427 bool template_p = false;
20428 tree decl;
20430 /* Allow the `template' keyword. */
20431 template_p = cp_parser_optional_template_keyword (parser);
20432 /* If we didn't see `template', we don't know if there's a
20433 template-id or not. */
20434 if (!template_p)
20435 cp_parser_parse_tentatively (parser);
20436 /* The `template' keyword must follow a nested-name-specifier. */
20437 else if (!nested_name_specifier && !globalscope)
20439 cp_parser_error (parser, "%<template%> must follow a nested-"
20440 "name-specifier");
20441 return error_mark_node;
20444 /* Parse the template-id. */
20445 token = cp_lexer_peek_token (parser->lexer);
20446 decl = cp_parser_template_id (parser, template_p,
20447 /*check_dependency_p=*/true,
20448 tag_type,
20449 is_declaration);
20450 /* If we didn't find a template-id, look for an ordinary
20451 identifier. */
20452 if (!template_p && !cp_parser_parse_definitely (parser))
20454 /* We can get here when cp_parser_template_id, called by
20455 cp_parser_class_name with tag_type == none_type, succeeds
20456 and caches a BASELINK. Then, when called again here,
20457 instead of failing and returning an error_mark_node
20458 returns it (see template/typename17.C in C++11).
20459 ??? Could we diagnose this earlier? */
20460 else if (tag_type == typename_type && BASELINK_P (decl))
20462 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20463 type = error_mark_node;
20465 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20466 in effect, then we must assume that, upon instantiation, the
20467 template will correspond to a class. */
20468 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20469 && tag_type == typename_type)
20470 type = make_typename_type (parser->scope, decl,
20471 typename_type,
20472 /*complain=*/tf_error);
20473 /* If the `typename' keyword is in effect and DECL is not a type
20474 decl, then type is non existent. */
20475 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20477 else if (TREE_CODE (decl) == TYPE_DECL)
20479 type = check_elaborated_type_specifier (tag_type, decl,
20480 /*allow_template_p=*/true);
20482 /* If the next token is a semicolon, this must be a specialization,
20483 instantiation, or friend declaration. Check the scope while we
20484 still know whether or not we had a nested-name-specifier. */
20485 if (type != error_mark_node
20486 && !nested_name_specifier && !is_friend
20487 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20488 check_unqualified_spec_or_inst (type, token->location);
20490 else if (decl == error_mark_node)
20491 type = error_mark_node;
20494 if (!type)
20496 token = cp_lexer_peek_token (parser->lexer);
20497 identifier = cp_parser_identifier (parser);
20499 if (identifier == error_mark_node)
20501 parser->scope = NULL_TREE;
20502 return error_mark_node;
20505 /* For a `typename', we needn't call xref_tag. */
20506 if (tag_type == typename_type
20507 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20508 return cp_parser_make_typename_type (parser, identifier,
20509 token->location);
20511 /* Template parameter lists apply only if we are not within a
20512 function parameter list. */
20513 bool template_parm_lists_apply
20514 = parser->num_template_parameter_lists;
20515 if (template_parm_lists_apply)
20516 for (cp_binding_level *s = current_binding_level;
20517 s && s->kind != sk_template_parms;
20518 s = s->level_chain)
20519 if (s->kind == sk_function_parms)
20520 template_parm_lists_apply = false;
20522 /* Look up a qualified name in the usual way. */
20523 if (parser->scope)
20525 tree decl;
20526 tree ambiguous_decls;
20528 decl = cp_parser_lookup_name (parser, identifier,
20529 tag_type,
20530 /*is_template=*/false,
20531 /*is_namespace=*/false,
20532 /*check_dependency=*/true,
20533 &ambiguous_decls,
20534 token->location);
20536 /* If the lookup was ambiguous, an error will already have been
20537 issued. */
20538 if (ambiguous_decls)
20539 return error_mark_node;
20541 /* If we are parsing friend declaration, DECL may be a
20542 TEMPLATE_DECL tree node here. However, we need to check
20543 whether this TEMPLATE_DECL results in valid code. Consider
20544 the following example:
20546 namespace N {
20547 template <class T> class C {};
20549 class X {
20550 template <class T> friend class N::C; // #1, valid code
20552 template <class T> class Y {
20553 friend class N::C; // #2, invalid code
20556 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20557 name lookup of `N::C'. We see that friend declaration must
20558 be template for the code to be valid. Note that
20559 processing_template_decl does not work here since it is
20560 always 1 for the above two cases. */
20562 decl = (cp_parser_maybe_treat_template_as_class
20563 (decl, /*tag_name_p=*/is_friend
20564 && template_parm_lists_apply));
20566 if (TREE_CODE (decl) != TYPE_DECL)
20568 cp_parser_diagnose_invalid_type_name (parser,
20569 identifier,
20570 token->location);
20571 return error_mark_node;
20574 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20576 bool allow_template = (template_parm_lists_apply
20577 || DECL_SELF_REFERENCE_P (decl));
20578 type = check_elaborated_type_specifier (tag_type, decl,
20579 allow_template);
20581 if (type == error_mark_node)
20582 return error_mark_node;
20585 /* Forward declarations of nested types, such as
20587 class C1::C2;
20588 class C1::C2::C3;
20590 are invalid unless all components preceding the final '::'
20591 are complete. If all enclosing types are complete, these
20592 declarations become merely pointless.
20594 Invalid forward declarations of nested types are errors
20595 caught elsewhere in parsing. Those that are pointless arrive
20596 here. */
20598 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20599 && !is_friend && is_declaration
20600 && !processing_explicit_instantiation)
20601 warning (0, "declaration %qD does not declare anything", decl);
20603 type = TREE_TYPE (decl);
20605 else
20607 /* An elaborated-type-specifier sometimes introduces a new type and
20608 sometimes names an existing type. Normally, the rule is that it
20609 introduces a new type only if there is not an existing type of
20610 the same name already in scope. For example, given:
20612 struct S {};
20613 void f() { struct S s; }
20615 the `struct S' in the body of `f' is the same `struct S' as in
20616 the global scope; the existing definition is used. However, if
20617 there were no global declaration, this would introduce a new
20618 local class named `S'.
20620 An exception to this rule applies to the following code:
20622 namespace N { struct S; }
20624 Here, the elaborated-type-specifier names a new type
20625 unconditionally; even if there is already an `S' in the
20626 containing scope this declaration names a new type.
20627 This exception only applies if the elaborated-type-specifier
20628 forms the complete declaration:
20630 [class.name]
20632 A declaration consisting solely of `class-key identifier ;' is
20633 either a redeclaration of the name in the current scope or a
20634 forward declaration of the identifier as a class name. It
20635 introduces the name into the current scope.
20637 We are in this situation precisely when the next token is a `;'.
20639 An exception to the exception is that a `friend' declaration does
20640 *not* name a new type; i.e., given:
20642 struct S { friend struct T; };
20644 `T' is not a new type in the scope of `S'.
20646 Also, `new struct S' or `sizeof (struct S)' never results in the
20647 definition of a new type; a new type can only be declared in a
20648 declaration context. */
20650 TAG_how how;
20652 if (is_friend)
20653 /* Friends have special name lookup rules. */
20654 how = TAG_how::HIDDEN_FRIEND;
20655 else if (is_declaration
20656 && cp_lexer_next_token_is (parser->lexer,
20657 CPP_SEMICOLON))
20658 /* This is a `class-key identifier ;' */
20659 how = TAG_how::CURRENT_ONLY;
20660 else
20661 how = TAG_how::GLOBAL;
20663 bool template_p =
20664 (template_parm_lists_apply
20665 && (cp_parser_next_token_starts_class_definition_p (parser)
20666 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
20667 /* An unqualified name was used to reference this type, so
20668 there were no qualifying templates. */
20669 if (template_parm_lists_apply
20670 && !cp_parser_check_template_parameters (parser,
20671 /*num_templates=*/0,
20672 /*template_id*/false,
20673 token->location,
20674 /*declarator=*/NULL))
20675 return error_mark_node;
20677 type = xref_tag (tag_type, identifier, how, template_p);
20681 if (type == error_mark_node)
20682 return error_mark_node;
20684 /* Allow attributes on forward declarations of classes. */
20685 if (attributes)
20687 if (TREE_CODE (type) == TYPENAME_TYPE)
20688 warning (OPT_Wattributes,
20689 "attributes ignored on uninstantiated type");
20690 else if (tag_type != enum_type
20691 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
20692 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
20693 && ! processing_explicit_instantiation)
20694 warning (OPT_Wattributes,
20695 "attributes ignored on template instantiation");
20696 else if (is_friend && cxx11_attribute_p (attributes))
20698 if (warning (OPT_Wattributes, "attribute ignored"))
20699 inform (input_location, "an attribute that appertains to a friend "
20700 "declaration that is not a definition is ignored");
20702 else if (is_declaration && cp_parser_declares_only_class_p (parser))
20703 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
20704 else
20705 warning (OPT_Wattributes,
20706 "attributes ignored on elaborated-type-specifier that is "
20707 "not a forward declaration");
20710 if (tag_type == enum_type)
20711 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
20712 else
20714 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
20715 for alias definition. */
20716 bool decl_class = (is_declaration
20717 && cp_parser_declares_only_class_p (parser));
20718 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
20719 decl_class);
20721 /* Indicate whether this class was declared as a `class' or as a
20722 `struct'. */
20723 if (CLASS_TYPE_P (type) && !currently_open_class (type))
20724 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
20727 /* A "<" cannot follow an elaborated type specifier. If that
20728 happens, the user was probably trying to form a template-id. */
20729 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
20730 token->location);
20732 return type;
20735 /* Parse an enum-specifier.
20737 enum-specifier:
20738 enum-head { enumerator-list [opt] }
20739 enum-head { enumerator-list , } [C++0x]
20741 enum-head:
20742 enum-key identifier [opt] enum-base [opt]
20743 enum-key nested-name-specifier identifier enum-base [opt]
20745 enum-key:
20746 enum
20747 enum class [C++0x]
20748 enum struct [C++0x]
20750 enum-base: [C++0x]
20751 : type-specifier-seq
20753 opaque-enum-specifier:
20754 enum-key identifier enum-base [opt] ;
20756 GNU Extensions:
20757 enum-key attributes[opt] identifier [opt] enum-base [opt]
20758 { enumerator-list [opt] }attributes[opt]
20759 enum-key attributes[opt] identifier [opt] enum-base [opt]
20760 { enumerator-list, }attributes[opt] [C++0x]
20762 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20763 if the token stream isn't an enum-specifier after all. */
20765 static tree
20766 cp_parser_enum_specifier (cp_parser* parser)
20768 tree identifier;
20769 tree type = NULL_TREE;
20770 tree prev_scope;
20771 tree nested_name_specifier = NULL_TREE;
20772 tree attributes;
20773 bool scoped_enum_p = false;
20774 bool has_underlying_type = false;
20775 bool nested_being_defined = false;
20776 bool new_value_list = false;
20777 bool is_new_type = false;
20778 bool is_unnamed = false;
20779 tree underlying_type = NULL_TREE;
20780 cp_token *type_start_token = NULL;
20781 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
20783 /* Parse tentatively so that we can back up if we don't find a
20784 enum-specifier. */
20785 cp_parser_parse_tentatively (parser);
20787 /* Caller guarantees that the current token is 'enum', an identifier
20788 possibly follows, and the token after that is an opening brace.
20789 If we don't have an identifier, fabricate an anonymous name for
20790 the enumeration being defined. */
20791 cp_lexer_consume_token (parser->lexer);
20793 /* Parse the "class" or "struct", which indicates a scoped
20794 enumeration type in C++0x. */
20795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
20796 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
20798 if (cxx_dialect < cxx11)
20799 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20801 /* Consume the `struct' or `class' token. */
20802 cp_lexer_consume_token (parser->lexer);
20804 scoped_enum_p = true;
20807 attributes = cp_parser_attributes_opt (parser);
20809 /* Clear the qualification. */
20810 parser->scope = NULL_TREE;
20811 parser->qualifying_scope = NULL_TREE;
20812 parser->object_scope = NULL_TREE;
20814 /* Figure out in what scope the declaration is being placed. */
20815 prev_scope = current_scope ();
20817 type_start_token = cp_lexer_peek_token (parser->lexer);
20819 push_deferring_access_checks (dk_no_check);
20820 nested_name_specifier
20821 = cp_parser_nested_name_specifier_opt (parser,
20822 /*typename_keyword_p=*/true,
20823 /*check_dependency_p=*/false,
20824 /*type_p=*/false,
20825 /*is_declaration=*/false);
20827 if (nested_name_specifier)
20829 tree name;
20831 identifier = cp_parser_identifier (parser);
20832 name = cp_parser_lookup_name (parser, identifier,
20833 enum_type,
20834 /*is_template=*/false,
20835 /*is_namespace=*/false,
20836 /*check_dependency=*/true,
20837 /*ambiguous_decls=*/NULL,
20838 input_location);
20839 if (name && name != error_mark_node)
20841 type = TREE_TYPE (name);
20842 if (TREE_CODE (type) == TYPENAME_TYPE)
20844 /* Are template enums allowed in ISO? */
20845 if (template_parm_scope_p ())
20846 pedwarn (type_start_token->location, OPT_Wpedantic,
20847 "%qD is an enumeration template", name);
20848 /* ignore a typename reference, for it will be solved by name
20849 in start_enum. */
20850 type = NULL_TREE;
20853 else if (nested_name_specifier == error_mark_node)
20854 /* We already issued an error. */;
20855 else
20857 error_at (type_start_token->location,
20858 "%qD does not name an enumeration in %qT",
20859 identifier, nested_name_specifier);
20860 nested_name_specifier = error_mark_node;
20863 else
20865 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20866 identifier = cp_parser_identifier (parser);
20867 else
20869 identifier = make_anon_name ();
20870 is_unnamed = true;
20871 if (scoped_enum_p)
20872 error_at (type_start_token->location,
20873 "unnamed scoped enum is not allowed");
20876 pop_deferring_access_checks ();
20878 /* Check for the `:' that denotes a specified underlying type in C++0x.
20879 Note that a ':' could also indicate a bitfield width, however. */
20880 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20882 cp_decl_specifier_seq type_specifiers;
20884 /* Consume the `:'. */
20885 cp_lexer_consume_token (parser->lexer);
20887 auto tdf
20888 = make_temp_override (parser->type_definition_forbidden_message,
20889 G_("types may not be defined in enum-base"));
20891 /* Parse the type-specifier-seq. */
20892 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
20893 /*is_declaration=*/false,
20894 /*is_trailing_return=*/false,
20895 &type_specifiers);
20897 /* At this point this is surely not elaborated type specifier. */
20898 if (!cp_parser_parse_definitely (parser))
20899 return NULL_TREE;
20901 if (cxx_dialect < cxx11)
20902 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20904 has_underlying_type = true;
20906 /* If that didn't work, stop. */
20907 if (type_specifiers.type != error_mark_node)
20909 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
20910 /*initialized=*/0, NULL);
20911 if (underlying_type == error_mark_node
20912 || check_for_bare_parameter_packs (underlying_type))
20913 underlying_type = NULL_TREE;
20917 /* Look for the `{' but don't consume it yet. */
20918 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20920 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
20922 if (has_underlying_type)
20923 cp_parser_commit_to_tentative_parse (parser);
20924 cp_parser_error (parser, "expected %<{%>");
20925 if (has_underlying_type)
20926 return error_mark_node;
20928 /* An opaque-enum-specifier must have a ';' here. */
20929 if ((scoped_enum_p || underlying_type)
20930 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20932 if (has_underlying_type)
20933 cp_parser_commit_to_tentative_parse (parser);
20934 cp_parser_error (parser, "expected %<;%> or %<{%>");
20935 if (has_underlying_type)
20936 return error_mark_node;
20940 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
20941 return NULL_TREE;
20943 if (nested_name_specifier)
20945 if (CLASS_TYPE_P (nested_name_specifier))
20947 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
20948 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
20949 push_scope (nested_name_specifier);
20951 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
20952 push_nested_namespace (nested_name_specifier);
20955 /* Issue an error message if type-definitions are forbidden here. */
20956 if (!cp_parser_check_type_definition (parser))
20957 type = error_mark_node;
20958 else
20959 /* Create the new type. We do this before consuming the opening
20960 brace so the enum will be recorded as being on the line of its
20961 tag (or the 'enum' keyword, if there is no tag). */
20962 type = start_enum (identifier, type, underlying_type,
20963 attributes, scoped_enum_p, &is_new_type);
20965 /* If the next token is not '{' it is an opaque-enum-specifier or an
20966 elaborated-type-specifier. */
20967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20969 timevar_push (TV_PARSE_ENUM);
20970 if (nested_name_specifier
20971 && nested_name_specifier != error_mark_node)
20973 /* The following catches invalid code such as:
20974 enum class S<int>::E { A, B, C }; */
20975 if (!processing_specialization
20976 && CLASS_TYPE_P (nested_name_specifier)
20977 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
20978 error_at (type_start_token->location, "cannot add an enumerator "
20979 "list to a template instantiation");
20981 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
20983 error_at (type_start_token->location,
20984 "%<%T::%E%> has not been declared",
20985 TYPE_CONTEXT (nested_name_specifier),
20986 nested_name_specifier);
20987 type = error_mark_node;
20989 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
20990 && !CLASS_TYPE_P (nested_name_specifier))
20992 error_at (type_start_token->location, "nested name specifier "
20993 "%qT for enum declaration does not name a class "
20994 "or namespace", nested_name_specifier);
20995 type = error_mark_node;
20997 /* If that scope does not contain the scope in which the
20998 class was originally declared, the program is invalid. */
20999 else if (prev_scope && !is_ancestor (prev_scope,
21000 nested_name_specifier))
21002 if (at_namespace_scope_p ())
21003 error_at (type_start_token->location,
21004 "declaration of %qD in namespace %qD which does not "
21005 "enclose %qD",
21006 type, prev_scope, nested_name_specifier);
21007 else
21008 error_at (type_start_token->location,
21009 "declaration of %qD in %qD which does not "
21010 "enclose %qD",
21011 type, prev_scope, nested_name_specifier);
21012 type = error_mark_node;
21014 /* If that scope is the scope where the declaration is being placed
21015 the program is invalid. */
21016 else if (CLASS_TYPE_P (nested_name_specifier)
21017 && CLASS_TYPE_P (prev_scope)
21018 && same_type_p (nested_name_specifier, prev_scope))
21020 permerror (type_start_token->location,
21021 "extra qualification not allowed");
21022 nested_name_specifier = NULL_TREE;
21026 if (scoped_enum_p)
21027 begin_scope (sk_scoped_enum, type);
21029 /* Consume the opening brace. */
21030 matching_braces braces;
21031 braces.consume_open (parser);
21033 if (type == error_mark_node)
21034 ; /* Nothing to add */
21035 else if (OPAQUE_ENUM_P (type)
21036 || (cxx_dialect > cxx98 && processing_specialization))
21038 new_value_list = true;
21039 SET_OPAQUE_ENUM_P (type, false);
21040 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21042 else
21044 error_at (type_start_token->location,
21045 "multiple definition of %q#T", type);
21046 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21047 "previous definition here");
21048 type = error_mark_node;
21051 if (type == error_mark_node)
21052 cp_parser_skip_to_end_of_block_or_statement (parser);
21053 /* If the next token is not '}', then there are some enumerators. */
21054 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21056 if (is_unnamed && !scoped_enum_p
21057 /* Don't warn for enum {} a; here. */
21058 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21059 pedwarn (type_start_token->location, OPT_Wpedantic,
21060 "ISO C++ forbids empty unnamed enum");
21062 else
21064 /* We've seen a '{' so we know we're in an enum-specifier.
21065 Commit to any tentative parse to get syntax errors. */
21066 cp_parser_commit_to_tentative_parse (parser);
21067 cp_parser_enumerator_list (parser, type);
21070 /* Consume the final '}'. */
21071 braces.require_close (parser);
21073 if (scoped_enum_p)
21074 finish_scope ();
21075 timevar_pop (TV_PARSE_ENUM);
21077 else
21079 /* If a ';' follows, then it is an opaque-enum-specifier
21080 and additional restrictions apply. */
21081 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21083 if (is_unnamed)
21084 error_at (type_start_token->location,
21085 "opaque-enum-specifier without name");
21086 else if (nested_name_specifier)
21087 error_at (type_start_token->location,
21088 "opaque-enum-specifier must use a simple identifier");
21092 /* Look for trailing attributes to apply to this enumeration, and
21093 apply them if appropriate. */
21094 if (cp_parser_allow_gnu_extensions_p (parser))
21096 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21097 cplus_decl_attributes (&type,
21098 trailing_attr,
21099 (int) ATTR_FLAG_TYPE_IN_PLACE);
21102 /* Finish up the enumeration. */
21103 if (type != error_mark_node)
21105 if (new_value_list)
21106 finish_enum_value_list (type);
21107 if (is_new_type)
21108 finish_enum (type);
21111 if (nested_name_specifier)
21113 if (CLASS_TYPE_P (nested_name_specifier))
21115 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21116 pop_scope (nested_name_specifier);
21118 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21119 pop_nested_namespace (nested_name_specifier);
21121 return type;
21124 /* Parse an enumerator-list. The enumerators all have the indicated
21125 TYPE.
21127 enumerator-list:
21128 enumerator-definition
21129 enumerator-list , enumerator-definition */
21131 static void
21132 cp_parser_enumerator_list (cp_parser* parser, tree type)
21134 while (true)
21136 /* Parse an enumerator-definition. */
21137 cp_parser_enumerator_definition (parser, type);
21139 /* If the next token is not a ',', we've reached the end of
21140 the list. */
21141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21142 break;
21143 /* Otherwise, consume the `,' and keep going. */
21144 cp_lexer_consume_token (parser->lexer);
21145 /* If the next token is a `}', there is a trailing comma. */
21146 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21148 if (cxx_dialect < cxx11)
21149 pedwarn (input_location, OPT_Wpedantic,
21150 "comma at end of enumerator list");
21151 break;
21156 /* Parse an enumerator-definition. The enumerator has the indicated
21157 TYPE.
21159 enumerator-definition:
21160 enumerator
21161 enumerator = constant-expression
21163 enumerator:
21164 identifier
21166 GNU Extensions:
21168 enumerator-definition:
21169 enumerator attributes [opt]
21170 enumerator attributes [opt] = constant-expression */
21172 static void
21173 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21175 tree identifier;
21176 tree value;
21177 location_t loc;
21179 /* Save the input location because we are interested in the location
21180 of the identifier and not the location of the explicit value. */
21181 loc = cp_lexer_peek_token (parser->lexer)->location;
21183 /* Look for the identifier. */
21184 identifier = cp_parser_identifier (parser);
21185 if (identifier == error_mark_node)
21186 return;
21188 /* Parse any specified attributes. */
21189 tree attrs = cp_parser_attributes_opt (parser);
21191 /* If the next token is an '=', then there is an explicit value. */
21192 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21194 /* Consume the `=' token. */
21195 cp_lexer_consume_token (parser->lexer);
21196 /* Parse the value. */
21197 value = cp_parser_constant_expression (parser);
21199 else
21200 value = NULL_TREE;
21202 /* If we are processing a template, make sure the initializer of the
21203 enumerator doesn't contain any bare template parameter pack. */
21204 if (current_lambda_expr ())
21206 /* In a lambda it should work, but doesn't currently. */
21207 if (uses_parameter_packs (value))
21209 sorry ("unexpanded parameter pack in enumerator in lambda");
21210 value = error_mark_node;
21213 else if (check_for_bare_parameter_packs (value))
21214 value = error_mark_node;
21216 /* Create the enumerator. */
21217 build_enumerator (identifier, value, type, attrs, loc);
21220 /* Parse a namespace-name.
21222 namespace-name:
21223 original-namespace-name
21224 namespace-alias
21226 Returns the NAMESPACE_DECL for the namespace. */
21228 static tree
21229 cp_parser_namespace_name (cp_parser* parser)
21231 tree identifier;
21232 tree namespace_decl;
21234 cp_token *token = cp_lexer_peek_token (parser->lexer);
21236 /* Get the name of the namespace. */
21237 identifier = cp_parser_identifier (parser);
21238 if (identifier == error_mark_node)
21239 return error_mark_node;
21241 /* Look up the identifier in the currently active scope. Look only
21242 for namespaces, due to:
21244 [basic.lookup.udir]
21246 When looking up a namespace-name in a using-directive or alias
21247 definition, only namespace names are considered.
21249 And:
21251 [basic.lookup.qual]
21253 During the lookup of a name preceding the :: scope resolution
21254 operator, object, function, and enumerator names are ignored.
21256 (Note that cp_parser_qualifying_entity only calls this
21257 function if the token after the name is the scope resolution
21258 operator.) */
21259 namespace_decl = cp_parser_lookup_name (parser, identifier,
21260 none_type,
21261 /*is_template=*/false,
21262 /*is_namespace=*/true,
21263 /*check_dependency=*/true,
21264 /*ambiguous_decls=*/NULL,
21265 token->location);
21266 /* If it's not a namespace, issue an error. */
21267 if (namespace_decl == error_mark_node
21268 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21270 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21272 auto_diagnostic_group d;
21273 name_hint hint;
21274 if (namespace_decl == error_mark_node
21275 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21276 hint = suggest_alternative_in_explicit_scope (token->location,
21277 identifier,
21278 parser->scope);
21279 if (const char *suggestion = hint.suggestion ())
21281 gcc_rich_location richloc (token->location);
21282 richloc.add_fixit_replace (suggestion);
21283 error_at (&richloc,
21284 "%qD is not a namespace-name; did you mean %qs?",
21285 identifier, suggestion);
21287 else
21288 error_at (token->location, "%qD is not a namespace-name",
21289 identifier);
21291 else
21292 cp_parser_error (parser, "expected namespace-name");
21293 namespace_decl = error_mark_node;
21296 return namespace_decl;
21299 /* Parse a namespace-definition.
21301 namespace-definition:
21302 named-namespace-definition
21303 unnamed-namespace-definition
21305 named-namespace-definition:
21306 original-namespace-definition
21307 extension-namespace-definition
21309 original-namespace-definition:
21310 namespace identifier { namespace-body }
21312 extension-namespace-definition:
21313 namespace original-namespace-name { namespace-body }
21315 unnamed-namespace-definition:
21316 namespace { namespace-body } */
21318 static void
21319 cp_parser_namespace_definition (cp_parser* parser)
21321 tree identifier;
21322 int nested_definition_count = 0;
21324 cp_ensure_no_omp_declare_simd (parser);
21325 cp_ensure_no_oacc_routine (parser);
21327 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21328 const bool topmost_inline_p = is_inline;
21330 if (is_inline)
21332 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21333 cp_lexer_consume_token (parser->lexer);
21336 /* Look for the `namespace' keyword. */
21337 cp_token* token
21338 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21340 /* Parse any specified attributes before the identifier. */
21341 tree attribs = cp_parser_attributes_opt (parser);
21343 for (;;)
21345 identifier = NULL_TREE;
21347 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21348 RID_INLINE);
21349 if (nested_inline_p && nested_definition_count != 0)
21351 if (pedantic && cxx_dialect < cxx20)
21352 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21353 OPT_Wc__20_extensions, "nested inline namespace "
21354 "definitions only available with %<-std=c++20%> or "
21355 "%<-std=gnu++20%>");
21356 cp_lexer_consume_token (parser->lexer);
21359 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21361 identifier = cp_parser_identifier (parser);
21363 if (cp_next_tokens_can_be_std_attribute_p (parser))
21364 pedwarn (input_location, OPT_Wpedantic,
21365 "standard attributes on namespaces must precede "
21366 "the namespace name");
21368 /* Parse any attributes specified after the identifier. */
21369 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21374 /* Don't forget that the innermost namespace might have been
21375 marked as inline. Use |= because we cannot overwrite
21376 IS_INLINE in case the outermost namespace is inline, but
21377 there are no nested inlines. */
21378 is_inline |= nested_inline_p;
21379 break;
21382 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21383 pedwarn (input_location, OPT_Wc__17_extensions,
21384 "nested namespace definitions only available with "
21385 "%<-std=c++17%> or %<-std=gnu++17%>");
21387 /* Nested namespace names can create new namespaces (unlike
21388 other qualified-ids). */
21389 if (int count = (identifier
21390 ? push_namespace (identifier, nested_inline_p)
21391 : 0))
21392 nested_definition_count += count;
21393 else
21394 cp_parser_error (parser, "nested namespace name required");
21395 cp_lexer_consume_token (parser->lexer);
21398 if (nested_definition_count && !identifier)
21399 cp_parser_error (parser, "namespace name required");
21401 if (nested_definition_count && attribs)
21402 error_at (token->location,
21403 "a nested namespace definition cannot have attributes");
21404 if (nested_definition_count && topmost_inline_p)
21405 error_at (token->location,
21406 "a nested namespace definition cannot be inline");
21408 /* Start the namespace. */
21409 nested_definition_count += push_namespace (identifier, is_inline);
21411 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21413 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21415 /* Look for the `{' to validate starting the namespace. */
21416 matching_braces braces;
21417 if (braces.require_open (parser))
21419 /* Parse the body of the namespace. */
21420 cp_parser_namespace_body (parser);
21422 /* Look for the final `}'. */
21423 braces.require_close (parser);
21426 if (has_visibility)
21427 pop_visibility (1);
21429 /* Pop the nested namespace definitions. */
21430 while (nested_definition_count--)
21431 pop_namespace ();
21434 /* Parse a namespace-body.
21436 namespace-body:
21437 declaration-seq [opt] */
21439 static void
21440 cp_parser_namespace_body (cp_parser* parser)
21442 cp_parser_declaration_seq_opt (parser);
21445 /* Parse a namespace-alias-definition.
21447 namespace-alias-definition:
21448 namespace identifier = qualified-namespace-specifier ; */
21450 static void
21451 cp_parser_namespace_alias_definition (cp_parser* parser)
21453 tree identifier;
21454 tree namespace_specifier;
21456 cp_token *token = cp_lexer_peek_token (parser->lexer);
21458 /* Look for the `namespace' keyword. */
21459 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21460 /* Look for the identifier. */
21461 identifier = cp_parser_identifier (parser);
21462 if (identifier == error_mark_node)
21463 return;
21464 /* Look for the `=' token. */
21465 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21466 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21468 error_at (token->location, "%<namespace%> definition is not allowed here");
21469 /* Skip the definition. */
21470 cp_lexer_consume_token (parser->lexer);
21471 if (cp_parser_skip_to_closing_brace (parser))
21472 cp_lexer_consume_token (parser->lexer);
21473 return;
21475 cp_parser_require (parser, CPP_EQ, RT_EQ);
21476 /* Look for the qualified-namespace-specifier. */
21477 namespace_specifier
21478 = cp_parser_qualified_namespace_specifier (parser);
21479 cp_warn_deprecated_use_scopes (namespace_specifier);
21480 /* Look for the `;' token. */
21481 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21483 /* Register the alias in the symbol table. */
21484 do_namespace_alias (identifier, namespace_specifier);
21487 /* Parse a qualified-namespace-specifier.
21489 qualified-namespace-specifier:
21490 :: [opt] nested-name-specifier [opt] namespace-name
21492 Returns a NAMESPACE_DECL corresponding to the specified
21493 namespace. */
21495 static tree
21496 cp_parser_qualified_namespace_specifier (cp_parser* parser)
21498 /* Look for the optional `::'. */
21499 cp_parser_global_scope_opt (parser,
21500 /*current_scope_valid_p=*/false);
21502 /* Look for the optional nested-name-specifier. */
21503 cp_parser_nested_name_specifier_opt (parser,
21504 /*typename_keyword_p=*/false,
21505 /*check_dependency_p=*/true,
21506 /*type_p=*/false,
21507 /*is_declaration=*/true);
21509 return cp_parser_namespace_name (parser);
21512 /* Subroutine of cp_parser_using_declaration. */
21514 static tree
21515 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21517 tree decl = NULL_TREE;
21518 if (at_class_scope_p ())
21520 /* Create the USING_DECL. */
21521 decl = do_class_using_decl (qscope, identifier);
21523 if (check_for_bare_parameter_packs (decl))
21524 return error_mark_node;
21526 if (decl && typename_p)
21527 USING_DECL_TYPENAME_P (decl) = 1;
21529 /* Add it to the list of members in this class. */
21530 finish_member_declaration (decl);
21532 else
21533 finish_nonmember_using_decl (qscope, identifier);
21534 return decl;
21537 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21538 access declaration.
21540 using-declaration:
21541 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21542 using :: unqualified-id ;
21544 access-declaration:
21545 qualified-id ;
21549 static bool
21550 cp_parser_using_declaration (cp_parser* parser,
21551 bool access_declaration_p)
21553 cp_token *token;
21554 bool typename_p = false;
21555 bool global_scope_p;
21556 tree identifier;
21557 tree qscope;
21558 int oldcount = errorcount;
21559 cp_token *diag_token = NULL;
21561 if (access_declaration_p)
21563 diag_token = cp_lexer_peek_token (parser->lexer);
21564 cp_parser_parse_tentatively (parser);
21566 else
21568 /* Look for the `using' keyword. */
21569 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21571 again:
21572 /* Peek at the next token. */
21573 token = cp_lexer_peek_token (parser->lexer);
21574 /* See if it's `typename'. */
21575 if (token->keyword == RID_TYPENAME)
21577 /* Remember that we've seen it. */
21578 typename_p = true;
21579 /* Consume the `typename' token. */
21580 cp_lexer_consume_token (parser->lexer);
21584 /* Look for the optional global scope qualification. */
21585 global_scope_p
21586 = (cp_parser_global_scope_opt (parser,
21587 /*current_scope_valid_p=*/false)
21588 != NULL_TREE);
21590 /* If we saw `typename', or didn't see `::', then there must be a
21591 nested-name-specifier present. */
21592 if (typename_p || !global_scope_p)
21594 qscope = cp_parser_nested_name_specifier (parser, typename_p,
21595 /*check_dependency_p=*/true,
21596 /*type_p=*/false,
21597 /*is_declaration=*/true);
21598 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21600 cp_parser_skip_to_end_of_block_or_statement (parser);
21601 return false;
21604 /* Otherwise, we could be in either of the two productions. In that
21605 case, treat the nested-name-specifier as optional. */
21606 else
21607 qscope = cp_parser_nested_name_specifier_opt (parser,
21608 /*typename_keyword_p=*/false,
21609 /*check_dependency_p=*/true,
21610 /*type_p=*/false,
21611 /*is_declaration=*/true);
21612 if (!qscope)
21613 qscope = global_namespace;
21615 cp_warn_deprecated_use_scopes (qscope);
21617 if (access_declaration_p && cp_parser_error_occurred (parser))
21618 /* Something has already gone wrong; there's no need to parse
21619 further. Since an error has occurred, the return value of
21620 cp_parser_parse_definitely will be false, as required. */
21621 return cp_parser_parse_definitely (parser);
21623 token = cp_lexer_peek_token (parser->lexer);
21624 /* Parse the unqualified-id. */
21625 identifier = cp_parser_unqualified_id (parser,
21626 /*template_keyword_p=*/false,
21627 /*check_dependency_p=*/true,
21628 /*declarator_p=*/true,
21629 /*optional_p=*/false);
21631 if (access_declaration_p)
21633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21634 cp_parser_simulate_error (parser);
21635 if (!cp_parser_parse_definitely (parser))
21636 return false;
21638 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21640 cp_token *ell = cp_lexer_consume_token (parser->lexer);
21641 if (cxx_dialect < cxx17)
21642 pedwarn (ell->location, OPT_Wc__17_extensions,
21643 "pack expansion in using-declaration only available "
21644 "with %<-std=c++17%> or %<-std=gnu++17%>");
21645 qscope = make_pack_expansion (qscope);
21648 /* The function we call to handle a using-declaration is different
21649 depending on what scope we are in. */
21650 if (qscope == error_mark_node || identifier == error_mark_node)
21652 else if (!identifier_p (identifier)
21653 && TREE_CODE (identifier) != BIT_NOT_EXPR)
21654 /* [namespace.udecl]
21656 A using declaration shall not name a template-id. */
21657 error_at (token->location,
21658 "a template-id may not appear in a using-declaration");
21659 else
21661 tree decl = finish_using_decl (qscope, identifier, typename_p);
21663 if (decl == error_mark_node)
21665 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21666 return false;
21670 if (!access_declaration_p
21671 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21673 cp_token *comma = cp_lexer_consume_token (parser->lexer);
21674 if (cxx_dialect < cxx17)
21675 pedwarn (comma->location, OPT_Wc__17_extensions,
21676 "comma-separated list in using-declaration only available "
21677 "with %<-std=c++17%> or %<-std=gnu++17%>");
21678 goto again;
21681 /* Look for the final `;'. */
21682 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21684 if (access_declaration_p && errorcount == oldcount)
21685 warning_at (diag_token->location, OPT_Wdeprecated,
21686 "access declarations are deprecated "
21687 "in favour of using-declarations; "
21688 "suggestion: add the %<using%> keyword");
21690 return true;
21693 /* C++20 using enum declaration.
21695 using-enum-declaration :
21696 using elaborated-enum-specifier ; */
21698 static void
21699 cp_parser_using_enum (cp_parser *parser)
21701 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21703 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21704 breaks one of the motivating examples in using-enum-5.C.
21705 cp_parser_simple_type_specifier seems to be closer to what we actually
21706 want, though that hasn't been properly specified yet. */
21708 /* Consume 'enum'. */
21709 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
21710 cp_lexer_consume_token (parser->lexer);
21712 cp_token *start = cp_lexer_peek_token (parser->lexer);
21714 tree type = (cp_parser_simple_type_specifier
21715 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
21717 cp_token *end = cp_lexer_previous_token (parser->lexer);
21719 if (type == error_mark_node
21720 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21722 cp_parser_skip_to_end_of_block_or_statement (parser);
21723 return;
21725 if (TREE_CODE (type) == TYPE_DECL)
21726 type = TREE_TYPE (type);
21728 /* The elaborated-enum-specifier shall not name a dependent type and the type
21729 shall have a reachable enum-specifier. */
21730 const char *msg = nullptr;
21731 if (cxx_dialect < cxx20)
21732 msg = _("%<using enum%> "
21733 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21734 else if (dependent_type_p (type))
21735 msg = _("%<using enum%> of dependent type %qT");
21736 else if (TREE_CODE (type) != ENUMERAL_TYPE)
21737 msg = _("%<using enum%> of non-enumeration type %q#T");
21738 else if (!COMPLETE_TYPE_P (type))
21739 msg = _("%<using enum%> of incomplete type %qT");
21740 else if (OPAQUE_ENUM_P (type))
21741 msg = _("%<using enum%> of %qT before its enum-specifier");
21742 if (msg)
21744 location_t loc = make_location (start, start, end);
21745 auto_diagnostic_group g;
21746 error_at (loc, msg, type);
21747 loc = location_of (type);
21748 if (cxx_dialect < cxx20 || loc == input_location)
21750 else if (OPAQUE_ENUM_P (type))
21751 inform (loc, "opaque-enum-declaration here");
21752 else
21753 inform (loc, "declared here");
21756 /* A using-enum-declaration introduces the enumerator names of the named
21757 enumeration as if by a using-declaration for each enumerator. */
21758 if (TREE_CODE (type) == ENUMERAL_TYPE)
21759 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
21760 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
21763 /* Parse an alias-declaration.
21765 alias-declaration:
21766 using identifier attribute-specifier-seq [opt] = type-id */
21768 static tree
21769 cp_parser_alias_declaration (cp_parser* parser)
21771 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
21772 location_t id_location, type_location;
21773 cp_declarator *declarator;
21774 cp_decl_specifier_seq decl_specs;
21775 bool member_p;
21776 const char *saved_message = NULL;
21778 /* Look for the `using' keyword. */
21779 cp_token *using_token
21780 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
21781 if (using_token == NULL)
21782 return error_mark_node;
21784 id_location = cp_lexer_peek_token (parser->lexer)->location;
21785 id = cp_parser_identifier (parser);
21786 if (id == error_mark_node)
21787 return error_mark_node;
21789 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
21790 attributes = cp_parser_attributes_opt (parser);
21791 if (attributes == error_mark_node)
21792 return error_mark_node;
21794 cp_parser_require (parser, CPP_EQ, RT_EQ);
21796 if (cp_parser_error_occurred (parser))
21797 return error_mark_node;
21799 cp_parser_commit_to_tentative_parse (parser);
21801 /* Now we are going to parse the type-id of the declaration. */
21804 [dcl.type]/3 says:
21806 "A type-specifier-seq shall not define a class or enumeration
21807 unless it appears in the type-id of an alias-declaration (7.1.3) that
21808 is not the declaration of a template-declaration."
21810 In other words, if we currently are in an alias template, the
21811 type-id should not define a type.
21813 So let's set parser->type_definition_forbidden_message in that
21814 case; cp_parser_check_type_definition (called by
21815 cp_parser_class_specifier) will then emit an error if a type is
21816 defined in the type-id. */
21817 if (parser->num_template_parameter_lists)
21819 saved_message = parser->type_definition_forbidden_message;
21820 parser->type_definition_forbidden_message =
21821 G_("types may not be defined in alias template declarations");
21824 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21825 &type_location);
21827 /* Restore the error message if need be. */
21828 if (parser->num_template_parameter_lists)
21829 parser->type_definition_forbidden_message = saved_message;
21831 if (type == error_mark_node
21832 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21834 cp_parser_skip_to_end_of_block_or_statement (parser);
21835 return error_mark_node;
21838 /* A typedef-name can also be introduced by an alias-declaration. The
21839 identifier following the using keyword becomes a typedef-name. It has
21840 the same semantics as if it were introduced by the typedef
21841 specifier. In particular, it does not define a new type and it shall
21842 not appear in the type-id. */
21844 clear_decl_specs (&decl_specs);
21845 decl_specs.type = type;
21846 if (attributes != NULL_TREE)
21848 decl_specs.attributes = attributes;
21849 set_and_check_decl_spec_loc (&decl_specs,
21850 ds_attribute,
21851 attrs_token);
21853 set_and_check_decl_spec_loc (&decl_specs,
21854 ds_typedef,
21855 using_token);
21856 set_and_check_decl_spec_loc (&decl_specs,
21857 ds_alias,
21858 using_token);
21859 decl_specs.locations[ds_type_spec] = type_location;
21861 if (parser->num_template_parameter_lists
21862 && !cp_parser_check_template_parameters (parser,
21863 /*num_templates=*/0,
21864 /*template_id*/false,
21865 id_location,
21866 /*declarator=*/NULL))
21867 return error_mark_node;
21869 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
21871 member_p = at_class_scope_p ();
21872 if (member_p)
21873 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
21874 NULL_TREE, attributes);
21875 else
21876 decl = start_decl (declarator, &decl_specs, 0,
21877 attributes, NULL_TREE, &pushed_scope);
21878 if (decl == error_mark_node)
21879 return decl;
21881 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
21883 if (pushed_scope)
21884 pop_scope (pushed_scope);
21886 /* If decl is a template, return its TEMPLATE_DECL so that it gets
21887 added into the symbol table; otherwise, return the TYPE_DECL. */
21888 if (DECL_LANG_SPECIFIC (decl)
21889 && DECL_TEMPLATE_INFO (decl)
21890 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
21892 decl = DECL_TI_TEMPLATE (decl);
21893 if (member_p)
21894 check_member_template (decl);
21897 return decl;
21900 /* Parse a using-directive.
21902 using-directive:
21903 attribute-specifier-seq [opt] using namespace :: [opt]
21904 nested-name-specifier [opt] namespace-name ; */
21906 static void
21907 cp_parser_using_directive (cp_parser* parser)
21909 tree namespace_decl;
21910 tree attribs = cp_parser_std_attribute_spec_seq (parser);
21911 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21913 /* Error during attribute parsing that resulted in skipping
21914 to next semicolon. */
21915 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21916 return;
21919 /* Look for the `using' keyword. */
21920 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21921 /* And the `namespace' keyword. */
21922 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21923 /* Look for the optional `::' operator. */
21924 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21925 /* And the optional nested-name-specifier. */
21926 cp_parser_nested_name_specifier_opt (parser,
21927 /*typename_keyword_p=*/false,
21928 /*check_dependency_p=*/true,
21929 /*type_p=*/false,
21930 /*is_declaration=*/true);
21931 /* Get the namespace being used. */
21932 namespace_decl = cp_parser_namespace_name (parser);
21933 cp_warn_deprecated_use_scopes (namespace_decl);
21934 /* And any specified GNU attributes. */
21935 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21936 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
21938 /* Update the symbol table. */
21939 finish_using_directive (namespace_decl, attribs);
21941 /* Look for the final `;'. */
21942 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21945 /* Parse an asm-definition.
21947 asm-qualifier:
21948 volatile
21949 inline
21950 goto
21952 asm-qualifier-list:
21953 asm-qualifier
21954 asm-qualifier-list asm-qualifier
21956 asm-definition:
21957 asm ( string-literal ) ;
21959 GNU Extension:
21961 asm-definition:
21962 asm asm-qualifier-list [opt] ( string-literal ) ;
21963 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
21964 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21965 : asm-operand-list [opt] ) ;
21966 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21967 : asm-operand-list [opt]
21968 : asm-clobber-list [opt] ) ;
21969 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
21970 : asm-clobber-list [opt]
21971 : asm-goto-list ) ;
21973 The form with asm-goto-list is valid if and only if the asm-qualifier-list
21974 contains goto, and is the only allowed form in that case. No duplicates are
21975 allowed in an asm-qualifier-list. */
21977 static void
21978 cp_parser_asm_definition (cp_parser* parser)
21980 tree string;
21981 tree outputs = NULL_TREE;
21982 tree inputs = NULL_TREE;
21983 tree clobbers = NULL_TREE;
21984 tree labels = NULL_TREE;
21985 tree asm_stmt;
21986 bool extended_p = false;
21987 bool invalid_inputs_p = false;
21988 bool invalid_outputs_p = false;
21989 required_token missing = RT_NONE;
21990 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
21992 /* Look for the `asm' keyword. */
21993 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
21995 /* In C++20, unevaluated inline assembly is permitted in constexpr
21996 functions. */
21997 if (parser->in_function_body
21998 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
21999 && cxx_dialect < cxx20)
22000 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22001 "function only available with %<-std=c++20%> or "
22002 "%<-std=gnu++20%>");
22004 /* Handle the asm-qualifier-list. */
22005 location_t volatile_loc = UNKNOWN_LOCATION;
22006 location_t inline_loc = UNKNOWN_LOCATION;
22007 location_t goto_loc = UNKNOWN_LOCATION;
22008 location_t first_loc = UNKNOWN_LOCATION;
22010 if (cp_parser_allow_gnu_extensions_p (parser))
22011 for (;;)
22013 cp_token *token = cp_lexer_peek_token (parser->lexer);
22014 location_t loc = token->location;
22015 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22017 case RID_VOLATILE:
22018 if (volatile_loc)
22020 error_at (loc, "duplicate %<asm%> qualifier %qT",
22021 token->u.value);
22022 inform (volatile_loc, "first seen here");
22024 else
22026 if (!parser->in_function_body)
22027 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22028 "outside of function body", token->u.value);
22029 volatile_loc = loc;
22031 cp_lexer_consume_token (parser->lexer);
22032 continue;
22034 case RID_INLINE:
22035 if (inline_loc)
22037 error_at (loc, "duplicate %<asm%> qualifier %qT",
22038 token->u.value);
22039 inform (inline_loc, "first seen here");
22041 else
22042 inline_loc = loc;
22043 if (!first_loc)
22044 first_loc = loc;
22045 cp_lexer_consume_token (parser->lexer);
22046 continue;
22048 case RID_GOTO:
22049 if (goto_loc)
22051 error_at (loc, "duplicate %<asm%> qualifier %qT",
22052 token->u.value);
22053 inform (goto_loc, "first seen here");
22055 else
22056 goto_loc = loc;
22057 if (!first_loc)
22058 first_loc = loc;
22059 cp_lexer_consume_token (parser->lexer);
22060 continue;
22062 case RID_CONST:
22063 case RID_RESTRICT:
22064 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22065 cp_lexer_consume_token (parser->lexer);
22066 continue;
22068 default:
22069 break;
22071 break;
22074 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22075 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22076 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22078 if (!parser->in_function_body && (inline_p || goto_p))
22080 error_at (first_loc, "%<asm%> qualifier outside of function body");
22081 inline_p = goto_p = false;
22084 /* Look for the opening `('. */
22085 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22086 return;
22087 /* Look for the string. */
22088 string = cp_parser_string_literal (parser, false, false);
22089 if (string == error_mark_node)
22091 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22092 /*consume_paren=*/true);
22093 return;
22096 /* If we're allowing GNU extensions, check for the extended assembly
22097 syntax. Unfortunately, the `:' tokens need not be separated by
22098 a space in C, and so, for compatibility, we tolerate that here
22099 too. Doing that means that we have to treat the `::' operator as
22100 two `:' tokens. */
22101 if (cp_parser_allow_gnu_extensions_p (parser)
22102 && parser->in_function_body
22103 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22104 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22106 bool inputs_p = false;
22107 bool clobbers_p = false;
22108 bool labels_p = false;
22110 /* The extended syntax was used. */
22111 extended_p = true;
22113 /* Look for outputs. */
22114 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22116 /* Consume the `:'. */
22117 cp_lexer_consume_token (parser->lexer);
22118 /* Parse the output-operands. */
22119 if (cp_lexer_next_token_is_not (parser->lexer,
22120 CPP_COLON)
22121 && cp_lexer_next_token_is_not (parser->lexer,
22122 CPP_SCOPE)
22123 && cp_lexer_next_token_is_not (parser->lexer,
22124 CPP_CLOSE_PAREN))
22126 outputs = cp_parser_asm_operand_list (parser);
22127 if (outputs == error_mark_node)
22128 invalid_outputs_p = true;
22131 /* If the next token is `::', there are no outputs, and the
22132 next token is the beginning of the inputs. */
22133 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22134 /* The inputs are coming next. */
22135 inputs_p = true;
22137 /* Look for inputs. */
22138 if (inputs_p
22139 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22141 /* Consume the `:' or `::'. */
22142 cp_lexer_consume_token (parser->lexer);
22143 /* Parse the output-operands. */
22144 if (cp_lexer_next_token_is_not (parser->lexer,
22145 CPP_COLON)
22146 && cp_lexer_next_token_is_not (parser->lexer,
22147 CPP_SCOPE)
22148 && cp_lexer_next_token_is_not (parser->lexer,
22149 CPP_CLOSE_PAREN))
22151 inputs = cp_parser_asm_operand_list (parser);
22152 if (inputs == error_mark_node)
22153 invalid_inputs_p = true;
22156 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22157 /* The clobbers are coming next. */
22158 clobbers_p = true;
22160 /* Look for clobbers. */
22161 if (clobbers_p
22162 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22164 clobbers_p = true;
22165 /* Consume the `:' or `::'. */
22166 cp_lexer_consume_token (parser->lexer);
22167 /* Parse the clobbers. */
22168 if (cp_lexer_next_token_is_not (parser->lexer,
22169 CPP_COLON)
22170 && cp_lexer_next_token_is_not (parser->lexer,
22171 CPP_CLOSE_PAREN))
22172 clobbers = cp_parser_asm_clobber_list (parser);
22174 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22175 /* The labels are coming next. */
22176 labels_p = true;
22178 /* Look for labels. */
22179 if (labels_p
22180 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22182 labels_p = true;
22183 /* Consume the `:' or `::'. */
22184 cp_lexer_consume_token (parser->lexer);
22185 /* Parse the labels. */
22186 labels = cp_parser_asm_label_list (parser);
22189 if (goto_p && !labels_p)
22190 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22192 else if (goto_p)
22193 missing = RT_COLON_SCOPE;
22195 /* Look for the closing `)'. */
22196 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22197 missing ? missing : RT_CLOSE_PAREN))
22198 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22199 /*consume_paren=*/true);
22200 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22202 if (!invalid_inputs_p && !invalid_outputs_p)
22204 /* Create the ASM_EXPR. */
22205 if (parser->in_function_body)
22207 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22208 inputs, clobbers, labels, inline_p);
22209 /* If the extended syntax was not used, mark the ASM_EXPR. */
22210 if (!extended_p)
22212 tree temp = asm_stmt;
22213 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22214 temp = TREE_OPERAND (temp, 0);
22216 ASM_INPUT_P (temp) = 1;
22219 else
22220 symtab->finalize_toplevel_asm (string);
22224 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22225 type that comes from the decl-specifier-seq. */
22227 static tree
22228 strip_declarator_types (tree type, cp_declarator *declarator)
22230 for (cp_declarator *d = declarator; d;)
22231 switch (d->kind)
22233 case cdk_id:
22234 case cdk_decomp:
22235 case cdk_error:
22236 d = NULL;
22237 break;
22239 default:
22240 if (TYPE_PTRMEMFUNC_P (type))
22241 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22242 type = TREE_TYPE (type);
22243 d = d->declarator;
22244 break;
22247 return type;
22250 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22251 a construct looks like a variable definition but is actually a function
22252 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22253 is the declarator for this function declaration. */
22255 static void
22256 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22257 const cp_declarator *declarator)
22259 /* Only warn if we are declaring a function at block scope. */
22260 if (!at_function_scope_p ())
22261 return;
22263 /* And only if there is no storage class specified. */
22264 if (decl_specifiers->storage_class != sc_none
22265 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22266 return;
22268 if (declarator->kind != cdk_function
22269 || !declarator->declarator
22270 || declarator->declarator->kind != cdk_id
22271 || !identifier_p (get_unqualified_id
22272 (const_cast<cp_declarator *>(declarator))))
22273 return;
22275 /* Don't warn when the whole declarator (not just the declarator-id!)
22276 was parenthesized. That is, don't warn for int(n()) but do warn
22277 for int(f)(). */
22278 if (declarator->parenthesized != UNKNOWN_LOCATION)
22279 return;
22281 tree type;
22282 if (decl_specifiers->type)
22284 type = decl_specifiers->type;
22285 if (TREE_CODE (type) == TYPE_DECL)
22286 type = TREE_TYPE (type);
22288 /* If the return type is void there is no ambiguity. */
22289 if (same_type_p (type, void_type_node))
22290 return;
22292 else if (decl_specifiers->any_type_specifiers_p)
22293 /* Code like long f(); will have null ->type. If we have any
22294 type-specifiers, pretend we've seen int. */
22295 type = integer_type_node;
22296 else
22297 return;
22299 auto_diagnostic_group d;
22300 location_t loc = declarator->u.function.parens_loc;
22301 tree params = declarator->u.function.parameters;
22302 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22304 /* The T t() case. */
22305 if (params == void_list_node)
22307 if (warning_at (loc, OPT_Wvexing_parse,
22308 "empty parentheses were disambiguated as a function "
22309 "declaration"))
22311 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22312 means value-initialization or aggregate-initialization, nothing
22313 means default-initialization. We can only suggest removing the
22314 parentheses/adding {} if T has a default constructor. */
22315 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22317 gcc_rich_location iloc (loc);
22318 iloc.add_fixit_remove ();
22319 inform (&iloc, "remove parentheses to default-initialize "
22320 "a variable");
22321 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22323 if (CP_AGGREGATE_TYPE_P (type))
22324 inform (loc, "or replace parentheses with braces to "
22325 "aggregate-initialize a variable");
22326 else
22327 inform (loc, "or replace parentheses with braces to "
22328 "value-initialize a variable");
22332 return;
22335 /* If we had (...) or the parameter-list wasn't parenthesized,
22336 we're done. */
22337 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22338 return;
22340 /* The T t(X()) case. */
22341 if (list_length (params) == 2)
22343 if (warning_at (loc, OPT_Wvexing_parse,
22344 "parentheses were disambiguated as a function "
22345 "declaration"))
22347 gcc_rich_location iloc (loc);
22348 /* {}-initialization means that we can use an initializer-list
22349 constructor if no default constructor is available, so don't
22350 suggest using {} for classes that have an initializer_list
22351 constructor. */
22352 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22354 iloc.add_fixit_replace (get_start (loc), "{");
22355 iloc.add_fixit_replace (get_finish (loc), "}");
22356 inform (&iloc, "replace parentheses with braces to declare a "
22357 "variable");
22359 else
22361 iloc.add_fixit_insert_after (get_start (loc), "(");
22362 iloc.add_fixit_insert_before (get_finish (loc), ")");
22363 inform (&iloc, "add parentheses to declare a variable");
22367 /* The T t(X(), X()) case. */
22368 else if (warning_at (loc, OPT_Wvexing_parse,
22369 "parentheses were disambiguated as a function "
22370 "declaration"))
22372 gcc_rich_location iloc (loc);
22373 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22375 iloc.add_fixit_replace (get_start (loc), "{");
22376 iloc.add_fixit_replace (get_finish (loc), "}");
22377 inform (&iloc, "replace parentheses with braces to declare a "
22378 "variable");
22383 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22384 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22385 has the same meaning as in cp_parser_declarator. */
22387 static void
22388 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22389 cp_decl_specifier_seq *decl_specs,
22390 cp_declarator *declarator,
22391 int *ctor_dtor_or_conv_p)
22393 if (cxx_dialect >= cxx17
22394 && *ctor_dtor_or_conv_p <= 0
22395 && !decl_specs->type
22396 && !decl_specs->any_type_specifiers_p
22397 && function_declarator_p (declarator))
22399 cp_declarator *id = get_id_declarator (declarator);
22400 tree name = id->u.id.unqualified_name;
22401 parser->scope = id->u.id.qualifying_scope;
22402 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22403 if (tmpl
22404 && (DECL_CLASS_TEMPLATE_P (tmpl)
22405 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22407 id->u.id.unqualified_name = dguide_name (tmpl);
22408 id->u.id.sfk = sfk_deduction_guide;
22409 *ctor_dtor_or_conv_p = 1;
22414 /* Declarators [gram.dcl.decl] */
22416 /* Parse an init-declarator.
22418 init-declarator:
22419 declarator initializer [opt]
22421 GNU Extension:
22423 init-declarator:
22424 declarator asm-specification [opt] attributes [opt] initializer [opt]
22426 function-definition:
22427 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22428 function-body
22429 decl-specifier-seq [opt] declarator function-try-block
22431 GNU Extension:
22433 function-definition:
22434 __extension__ function-definition
22436 TM Extension:
22438 function-definition:
22439 decl-specifier-seq [opt] declarator function-transaction-block
22441 The parser flags FLAGS is used to control type-specifier parsing.
22443 The DECL_SPECIFIERS apply to this declarator. Returns a
22444 representation of the entity declared. If MEMBER_P is TRUE, then
22445 this declarator appears in a class scope. The new DECL created by
22446 this declarator is returned.
22448 The CHECKS are access checks that should be performed once we know
22449 what entity is being declared (and, therefore, what classes have
22450 befriended it).
22452 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22453 for a function-definition here as well. If the declarator is a
22454 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22455 be TRUE upon return. By that point, the function-definition will
22456 have been completely parsed.
22458 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22459 is FALSE.
22461 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22462 parsed declaration if it is an uninitialized single declarator not followed
22463 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22464 if present, will not be consumed. If returned, this declarator will be
22465 created with SD_INITIALIZED but will not call cp_finish_decl.
22467 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22468 and there is an initializer, the pointed location_t is set to the
22469 location of the '=' or `(', or '{' in C++11 token introducing the
22470 initializer. */
22472 static tree
22473 cp_parser_init_declarator (cp_parser* parser,
22474 cp_parser_flags flags,
22475 cp_decl_specifier_seq *decl_specifiers,
22476 vec<deferred_access_check, va_gc> *checks,
22477 bool function_definition_allowed_p,
22478 bool member_p,
22479 int declares_class_or_enum,
22480 bool* function_definition_p,
22481 tree* maybe_range_for_decl,
22482 location_t* init_loc,
22483 tree* auto_result)
22485 cp_token *token = NULL, *asm_spec_start_token = NULL,
22486 *attributes_start_token = NULL;
22487 cp_declarator *declarator;
22488 tree prefix_attributes;
22489 tree attributes = NULL;
22490 tree asm_specification;
22491 tree initializer;
22492 tree decl = NULL_TREE;
22493 tree scope;
22494 int is_initialized;
22495 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22496 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22497 "(...)". */
22498 enum cpp_ttype initialization_kind;
22499 bool is_direct_init = false;
22500 bool is_non_constant_init;
22501 int ctor_dtor_or_conv_p;
22502 bool friend_p = cp_parser_friend_p (decl_specifiers);
22503 bool static_p = decl_specifiers->storage_class == sc_static;
22504 tree pushed_scope = NULL_TREE;
22505 bool range_for_decl_p = false;
22506 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22507 location_t tmp_init_loc = UNKNOWN_LOCATION;
22509 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22510 flags |= CP_PARSER_FLAGS_CONSTEVAL;
22512 /* Assume that this is not the declarator for a function
22513 definition. */
22514 if (function_definition_p)
22515 *function_definition_p = false;
22517 /* Default arguments are only permitted for function parameters. */
22518 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22519 parser->default_arg_ok_p = false;
22521 /* Defer access checks while parsing the declarator; we cannot know
22522 what names are accessible until we know what is being
22523 declared. */
22524 resume_deferring_access_checks ();
22526 token = cp_lexer_peek_token (parser->lexer);
22528 /* Parse the declarator. */
22529 declarator
22530 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22531 flags, &ctor_dtor_or_conv_p,
22532 /*parenthesized_p=*/NULL,
22533 member_p, friend_p, static_p);
22534 /* Gather up the deferred checks. */
22535 stop_deferring_access_checks ();
22537 parser->default_arg_ok_p = saved_default_arg_ok_p;
22539 /* If the DECLARATOR was erroneous, there's no need to go
22540 further. */
22541 if (declarator == cp_error_declarator)
22542 return error_mark_node;
22544 /* Check that the number of template-parameter-lists is OK. */
22545 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22546 token->location))
22547 return error_mark_node;
22549 if (declares_class_or_enum & 2)
22550 cp_parser_check_for_definition_in_return_type (declarator,
22551 decl_specifiers->type,
22552 decl_specifiers->locations[ds_type_spec]);
22554 /* Figure out what scope the entity declared by the DECLARATOR is
22555 located in. `grokdeclarator' sometimes changes the scope, so
22556 we compute it now. */
22557 scope = get_scope_of_declarator (declarator);
22559 /* Perform any lookups in the declared type which were thought to be
22560 dependent, but are not in the scope of the declarator. */
22561 decl_specifiers->type
22562 = maybe_update_decl_type (decl_specifiers->type, scope);
22564 /* If we're allowing GNU extensions, look for an
22565 asm-specification. */
22566 if (cp_parser_allow_gnu_extensions_p (parser))
22568 /* Look for an asm-specification. */
22569 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22570 asm_specification = cp_parser_asm_specification_opt (parser);
22572 else
22573 asm_specification = NULL_TREE;
22575 /* Gather the attributes that were provided with the
22576 decl-specifiers. */
22577 prefix_attributes = decl_specifiers->attributes;
22579 /* Look for attributes. */
22580 attributes_start_token = cp_lexer_peek_token (parser->lexer);
22581 attributes = cp_parser_attributes_opt (parser);
22583 /* Peek at the next token. */
22584 token = cp_lexer_peek_token (parser->lexer);
22586 bool bogus_implicit_tmpl = false;
22588 if (function_declarator_p (declarator))
22590 /* Handle C++17 deduction guides. Note that class-scope
22591 non-template deduction guides are instead handled in
22592 cp_parser_member_declaration. */
22593 cp_parser_maybe_adjust_declarator_for_dguide (parser,
22594 decl_specifiers,
22595 declarator,
22596 &ctor_dtor_or_conv_p);
22598 if (!member_p && !cp_parser_error_occurred (parser))
22599 warn_about_ambiguous_parse (decl_specifiers, declarator);
22601 /* Check to see if the token indicates the start of a
22602 function-definition. */
22603 if (cp_parser_token_starts_function_definition_p (token))
22605 if (!function_definition_allowed_p)
22607 /* If a function-definition should not appear here, issue an
22608 error message. */
22609 cp_parser_error (parser,
22610 "a function-definition is not allowed here");
22611 return error_mark_node;
22614 location_t func_brace_location
22615 = cp_lexer_peek_token (parser->lexer)->location;
22617 /* Neither attributes nor an asm-specification are allowed
22618 on a function-definition. */
22619 if (asm_specification)
22620 error_at (asm_spec_start_token->location,
22621 "an %<asm%> specification is not allowed "
22622 "on a function-definition");
22623 if (attributes)
22624 error_at (attributes_start_token->location,
22625 "attributes are not allowed "
22626 "on a function-definition");
22627 /* This is a function-definition. */
22628 *function_definition_p = true;
22630 /* Parse the function definition. */
22631 if (member_p)
22632 decl = cp_parser_save_member_function_body (parser,
22633 decl_specifiers,
22634 declarator,
22635 prefix_attributes);
22636 else
22637 decl =
22638 (cp_parser_function_definition_from_specifiers_and_declarator
22639 (parser, decl_specifiers, prefix_attributes, declarator));
22641 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
22643 /* This is where the prologue starts... */
22644 DECL_STRUCT_FUNCTION (decl)->function_start_locus
22645 = func_brace_location;
22648 return decl;
22651 else if (parser->fully_implicit_function_template_p)
22653 /* A non-template declaration involving a function parameter list
22654 containing an implicit template parameter will be made into a
22655 template. If the resulting declaration is not going to be an
22656 actual function then finish the template scope here to prevent it.
22657 An error message will be issued once we have a decl to talk about.
22659 FIXME probably we should do type deduction rather than create an
22660 implicit template, but the standard currently doesn't allow it. */
22661 bogus_implicit_tmpl = true;
22662 finish_fully_implicit_template (parser, NULL_TREE);
22665 /* [dcl.dcl]
22667 Only in function declarations for constructors, destructors, type
22668 conversions, and deduction guides can the decl-specifier-seq be omitted.
22670 We explicitly postpone this check past the point where we handle
22671 function-definitions because we tolerate function-definitions
22672 that are missing their return types in some modes. */
22673 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
22675 cp_parser_error (parser,
22676 "expected constructor, destructor, or type conversion");
22677 return error_mark_node;
22680 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
22681 an initializer as well. */
22682 if (token->type == CPP_EQ
22683 || token->type == CPP_OPEN_PAREN
22684 || token->type == CPP_OPEN_BRACE)
22686 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
22687 It isn't; it's an expression. (Here '(i)' would have already been
22688 parsed as a declarator.) */
22689 if (token->type == CPP_OPEN_PAREN
22690 && cp_parser_uncommitted_to_tentative_parse_p (parser))
22692 cp_lexer_save_tokens (parser->lexer);
22693 cp_lexer_consume_token (parser->lexer);
22694 cp_parser_skip_to_closing_parenthesis (parser,
22695 /*recovering*/false,
22696 /*or_comma*/false,
22697 /*consume_paren*/true);
22698 /* If this is an initializer, only a ',' or ';' can follow: either
22699 we have another init-declarator, or we're at the end of an
22700 init-declarator-list which can only be followed by a ';'. */
22701 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22702 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22703 cp_lexer_rollback_tokens (parser->lexer);
22704 if (UNLIKELY (!ok))
22705 /* Not an init-declarator. */
22706 return error_mark_node;
22708 is_initialized = SD_INITIALIZED;
22709 initialization_kind = token->type;
22710 declarator->init_loc = token->location;
22711 if (maybe_range_for_decl)
22712 *maybe_range_for_decl = error_mark_node;
22713 tmp_init_loc = token->location;
22714 if (init_loc && *init_loc == UNKNOWN_LOCATION)
22715 *init_loc = tmp_init_loc;
22717 if (token->type == CPP_EQ
22718 && function_declarator_p (declarator))
22720 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
22721 if (t2->keyword == RID_DEFAULT)
22722 is_initialized = SD_DEFAULTED;
22723 else if (t2->keyword == RID_DELETE)
22724 is_initialized = SD_DELETED;
22727 else
22729 /* If the init-declarator isn't initialized and isn't followed by a
22730 `,' or `;', it's not a valid init-declarator. */
22731 if (token->type != CPP_COMMA
22732 && token->type != CPP_SEMICOLON)
22734 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
22735 range_for_decl_p = true;
22736 else
22738 if (!maybe_range_for_decl)
22739 cp_parser_error (parser, "expected initializer");
22740 return error_mark_node;
22743 is_initialized = SD_UNINITIALIZED;
22744 initialization_kind = CPP_EOF;
22747 /* Because start_decl has side-effects, we should only call it if we
22748 know we're going ahead. By this point, we know that we cannot
22749 possibly be looking at any other construct. */
22750 cp_parser_commit_to_tentative_parse (parser);
22752 /* Enter the newly declared entry in the symbol table. If we're
22753 processing a declaration in a class-specifier, we wait until
22754 after processing the initializer. */
22755 if (!member_p)
22757 if (parser->in_unbraced_linkage_specification_p)
22758 decl_specifiers->storage_class = sc_extern;
22759 decl = start_decl (declarator, decl_specifiers,
22760 range_for_decl_p? SD_INITIALIZED : is_initialized,
22761 attributes, prefix_attributes, &pushed_scope);
22762 cp_finalize_omp_declare_simd (parser, decl);
22763 cp_finalize_oacc_routine (parser, decl, false);
22764 /* Adjust location of decl if declarator->id_loc is more appropriate:
22765 set, and decl wasn't merged with another decl, in which case its
22766 location would be different from input_location, and more accurate. */
22767 if (DECL_P (decl)
22768 && declarator->id_loc != UNKNOWN_LOCATION
22769 && DECL_SOURCE_LOCATION (decl) == input_location)
22770 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
22772 else if (scope)
22773 /* Enter the SCOPE. That way unqualified names appearing in the
22774 initializer will be looked up in SCOPE. */
22775 pushed_scope = push_scope (scope);
22777 /* Perform deferred access control checks, now that we know in which
22778 SCOPE the declared entity resides. */
22779 if (!member_p && decl)
22781 tree saved_current_function_decl = NULL_TREE;
22783 /* If the entity being declared is a function, pretend that we
22784 are in its scope. If it is a `friend', it may have access to
22785 things that would not otherwise be accessible. */
22786 if (TREE_CODE (decl) == FUNCTION_DECL)
22788 saved_current_function_decl = current_function_decl;
22789 current_function_decl = decl;
22792 /* Perform access checks for template parameters. */
22793 cp_parser_perform_template_parameter_access_checks (checks);
22795 /* Perform the access control checks for the declarator and the
22796 decl-specifiers. */
22797 perform_deferred_access_checks (tf_warning_or_error);
22799 /* Restore the saved value. */
22800 if (TREE_CODE (decl) == FUNCTION_DECL)
22801 current_function_decl = saved_current_function_decl;
22804 /* Parse the initializer. */
22805 initializer = NULL_TREE;
22806 is_direct_init = false;
22807 is_non_constant_init = true;
22808 if (is_initialized)
22810 if (function_declarator_p (declarator))
22812 if (initialization_kind == CPP_EQ)
22813 initializer = cp_parser_pure_specifier (parser);
22814 else
22816 /* If the declaration was erroneous, we don't really
22817 know what the user intended, so just silently
22818 consume the initializer. */
22819 if (decl != error_mark_node)
22820 error_at (tmp_init_loc, "initializer provided for function");
22821 cp_parser_skip_to_closing_parenthesis (parser,
22822 /*recovering=*/true,
22823 /*or_comma=*/false,
22824 /*consume_paren=*/true);
22827 else
22829 /* We want to record the extra mangling scope for in-class
22830 initializers of class members and initializers of static
22831 data member templates and namespace-scope initializers.
22832 The former involves deferring parsing of the initializer
22833 until end of class as with default arguments. So right
22834 here we only handle the latter two. */
22835 bool has_lambda_scope = false;
22837 if (decl != error_mark_node
22838 && !member_p
22839 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
22840 has_lambda_scope = true;
22842 if (has_lambda_scope)
22843 start_lambda_scope (decl);
22844 initializer = cp_parser_initializer (parser,
22845 &is_direct_init,
22846 &is_non_constant_init);
22847 if (has_lambda_scope)
22848 finish_lambda_scope ();
22849 if (initializer == error_mark_node)
22850 cp_parser_skip_to_end_of_statement (parser);
22854 /* The old parser allows attributes to appear after a parenthesized
22855 initializer. Mark Mitchell proposed removing this functionality
22856 on the GCC mailing lists on 2002-08-13. This parser accepts the
22857 attributes -- but ignores them. Made a permerror in GCC 8. */
22858 if (cp_parser_allow_gnu_extensions_p (parser)
22859 && initialization_kind == CPP_OPEN_PAREN
22860 && cp_parser_attributes_opt (parser)
22861 && permerror (input_location,
22862 "attributes after parenthesized initializer ignored"))
22864 static bool hint;
22865 if (flag_permissive && !hint)
22867 hint = true;
22868 inform (input_location,
22869 "this flexibility is deprecated and will be removed");
22873 /* And now complain about a non-function implicit template. */
22874 if (bogus_implicit_tmpl && decl != error_mark_node)
22875 error_at (DECL_SOURCE_LOCATION (decl),
22876 "non-function %qD declared as implicit template", decl);
22878 /* For an in-class declaration, use `grokfield' to create the
22879 declaration. */
22880 if (member_p)
22882 if (pushed_scope)
22884 pop_scope (pushed_scope);
22885 pushed_scope = NULL_TREE;
22887 decl = grokfield (declarator, decl_specifiers,
22888 initializer, !is_non_constant_init,
22889 /*asmspec=*/NULL_TREE,
22890 attr_chainon (attributes, prefix_attributes));
22891 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
22892 cp_parser_save_default_args (parser, decl);
22893 cp_finalize_omp_declare_simd (parser, decl);
22894 cp_finalize_oacc_routine (parser, decl, false);
22897 /* Finish processing the declaration. But, skip member
22898 declarations. */
22899 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
22901 cp_finish_decl (decl,
22902 initializer, !is_non_constant_init,
22903 asm_specification,
22904 /* If the initializer is in parentheses, then this is
22905 a direct-initialization, which means that an
22906 `explicit' constructor is OK. Otherwise, an
22907 `explicit' constructor cannot be used. */
22908 ((is_direct_init || !is_initialized)
22909 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
22911 else if ((cxx_dialect != cxx98) && friend_p
22912 && decl && TREE_CODE (decl) == FUNCTION_DECL)
22913 /* Core issue #226 (C++0x only): A default template-argument
22914 shall not be specified in a friend class template
22915 declaration. */
22916 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
22917 /*is_partial=*/false, /*is_friend_decl=*/1);
22919 if (!friend_p && pushed_scope)
22920 pop_scope (pushed_scope);
22922 if (function_declarator_p (declarator)
22923 && parser->fully_implicit_function_template_p)
22925 if (member_p)
22926 decl = finish_fully_implicit_template (parser, decl);
22927 else
22928 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22931 if (auto_result && is_initialized && decl_specifiers->type
22932 && type_uses_auto (decl_specifiers->type))
22933 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
22935 return decl;
22938 /* Parse a declarator.
22940 declarator:
22941 direct-declarator
22942 ptr-operator declarator
22944 abstract-declarator:
22945 ptr-operator abstract-declarator [opt]
22946 direct-abstract-declarator
22948 GNU Extensions:
22950 declarator:
22951 attributes [opt] direct-declarator
22952 attributes [opt] ptr-operator declarator
22954 abstract-declarator:
22955 attributes [opt] ptr-operator abstract-declarator [opt]
22956 attributes [opt] direct-abstract-declarator
22958 The parser flags FLAGS is used to control type-specifier parsing.
22960 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
22961 detect constructors, destructors, deduction guides, or conversion operators.
22962 It is set to -1 if the declarator is a name, and +1 if it is a
22963 function. Otherwise it is set to zero. Usually you just want to
22964 test for >0, but internally the negative value is used.
22966 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
22967 a decl-specifier-seq unless it declares a constructor, destructor,
22968 or conversion. It might seem that we could check this condition in
22969 semantic analysis, rather than parsing, but that makes it difficult
22970 to handle something like `f()'. We want to notice that there are
22971 no decl-specifiers, and therefore realize that this is an
22972 expression, not a declaration.)
22974 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
22975 the declarator is a direct-declarator of the form "(...)".
22977 MEMBER_P is true iff this declarator is a member-declarator.
22979 FRIEND_P is true iff this declarator is a friend.
22981 STATIC_P is true iff the keyword static was seen. */
22983 static cp_declarator *
22984 cp_parser_declarator (cp_parser* parser,
22985 cp_parser_declarator_kind dcl_kind,
22986 cp_parser_flags flags,
22987 int* ctor_dtor_or_conv_p,
22988 bool* parenthesized_p,
22989 bool member_p, bool friend_p, bool static_p)
22991 cp_declarator *declarator;
22992 enum tree_code code;
22993 cp_cv_quals cv_quals;
22994 tree class_type;
22995 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
22997 /* Assume this is not a constructor, destructor, or type-conversion
22998 operator. */
22999 if (ctor_dtor_or_conv_p)
23000 *ctor_dtor_or_conv_p = 0;
23002 if (cp_parser_allow_gnu_extensions_p (parser))
23003 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23005 /* Check for the ptr-operator production. */
23006 cp_parser_parse_tentatively (parser);
23007 /* Parse the ptr-operator. */
23008 code = cp_parser_ptr_operator (parser,
23009 &class_type,
23010 &cv_quals,
23011 &std_attributes);
23013 /* If that worked, then we have a ptr-operator. */
23014 if (cp_parser_parse_definitely (parser))
23016 /* If a ptr-operator was found, then this declarator was not
23017 parenthesized. */
23018 if (parenthesized_p)
23019 *parenthesized_p = false;
23020 /* The dependent declarator is optional if we are parsing an
23021 abstract-declarator. */
23022 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23023 cp_parser_parse_tentatively (parser);
23025 /* Parse the dependent declarator. */
23026 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23027 /*ctor_dtor_or_conv_p=*/NULL,
23028 /*parenthesized_p=*/NULL,
23029 member_p, friend_p, static_p);
23031 /* If we are parsing an abstract-declarator, we must handle the
23032 case where the dependent declarator is absent. */
23033 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23034 && !cp_parser_parse_definitely (parser))
23035 declarator = NULL;
23037 declarator = cp_parser_make_indirect_declarator
23038 (code, class_type, cv_quals, declarator, std_attributes);
23040 /* Everything else is a direct-declarator. */
23041 else
23043 if (parenthesized_p)
23044 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23045 CPP_OPEN_PAREN);
23046 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23047 flags, ctor_dtor_or_conv_p,
23048 member_p, friend_p, static_p);
23051 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23052 declarator->attributes = gnu_attributes;
23053 return declarator;
23056 /* Parse a direct-declarator or direct-abstract-declarator.
23058 direct-declarator:
23059 declarator-id
23060 direct-declarator ( parameter-declaration-clause )
23061 cv-qualifier-seq [opt]
23062 ref-qualifier [opt]
23063 exception-specification [opt]
23064 direct-declarator [ constant-expression [opt] ]
23065 ( declarator )
23067 direct-abstract-declarator:
23068 direct-abstract-declarator [opt]
23069 ( parameter-declaration-clause )
23070 cv-qualifier-seq [opt]
23071 ref-qualifier [opt]
23072 exception-specification [opt]
23073 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23074 ( abstract-declarator )
23076 Returns a representation of the declarator. DCL_KIND is
23077 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23078 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23079 we are parsing a direct-declarator. It is
23080 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23081 of ambiguity we prefer an abstract declarator, as per
23082 [dcl.ambig.res].
23083 The parser flags FLAGS is used to control type-specifier parsing.
23084 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23085 as for cp_parser_declarator. */
23087 static cp_declarator *
23088 cp_parser_direct_declarator (cp_parser* parser,
23089 cp_parser_declarator_kind dcl_kind,
23090 cp_parser_flags flags,
23091 int* ctor_dtor_or_conv_p,
23092 bool member_p, bool friend_p, bool static_p)
23094 cp_token *token;
23095 cp_declarator *declarator = NULL;
23096 tree scope = NULL_TREE;
23097 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23098 bool saved_in_declarator_p = parser->in_declarator_p;
23099 bool first = true;
23100 tree pushed_scope = NULL_TREE;
23101 cp_token *open_paren = NULL, *close_paren = NULL;
23103 while (true)
23105 /* Peek at the next token. */
23106 token = cp_lexer_peek_token (parser->lexer);
23107 if (token->type == CPP_OPEN_PAREN)
23109 /* This is either a parameter-declaration-clause, or a
23110 parenthesized declarator. When we know we are parsing a
23111 named declarator, it must be a parenthesized declarator
23112 if FIRST is true. For instance, `(int)' is a
23113 parameter-declaration-clause, with an omitted
23114 direct-abstract-declarator. But `((*))', is a
23115 parenthesized abstract declarator. Finally, when T is a
23116 template parameter `(T)' is a
23117 parameter-declaration-clause, and not a parenthesized
23118 named declarator.
23120 We first try and parse a parameter-declaration-clause,
23121 and then try a nested declarator (if FIRST is true).
23123 It is not an error for it not to be a
23124 parameter-declaration-clause, even when FIRST is
23125 false. Consider,
23127 int i (int);
23128 int i (3);
23130 The first is the declaration of a function while the
23131 second is the definition of a variable, including its
23132 initializer.
23134 Having seen only the parenthesis, we cannot know which of
23135 these two alternatives should be selected. Even more
23136 complex are examples like:
23138 int i (int (a));
23139 int i (int (3));
23141 The former is a function-declaration; the latter is a
23142 variable initialization.
23144 Thus again, we try a parameter-declaration-clause, and if
23145 that fails, we back out and return. */
23147 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23149 tree params;
23150 bool is_declarator = false;
23152 open_paren = NULL;
23154 /* In a member-declarator, the only valid interpretation
23155 of a parenthesis is the start of a
23156 parameter-declaration-clause. (It is invalid to
23157 initialize a static data member with a parenthesized
23158 initializer; only the "=" form of initialization is
23159 permitted.) */
23160 if (!member_p)
23161 cp_parser_parse_tentatively (parser);
23163 /* Consume the `('. */
23164 const location_t parens_start = token->location;
23165 matching_parens parens;
23166 parens.consume_open (parser);
23167 if (first)
23169 /* If this is going to be an abstract declarator, we're
23170 in a declarator and we can't have default args. */
23171 parser->default_arg_ok_p = false;
23172 parser->in_declarator_p = true;
23175 begin_scope (sk_function_parms, NULL_TREE);
23177 /* Signal we are in the immediate function context. */
23178 if (flags & CP_PARSER_FLAGS_CONSTEVAL)
23179 current_binding_level->immediate_fn_ctx_p = true;
23181 /* Parse the parameter-declaration-clause. */
23182 params
23183 = cp_parser_parameter_declaration_clause (parser, flags);
23184 const location_t parens_end
23185 = cp_lexer_peek_token (parser->lexer)->location;
23187 /* Consume the `)'. */
23188 parens.require_close (parser);
23190 /* If all went well, parse the cv-qualifier-seq,
23191 ref-qualifier and the exception-specification. */
23192 if (member_p || cp_parser_parse_definitely (parser))
23194 cp_cv_quals cv_quals;
23195 cp_virt_specifiers virt_specifiers;
23196 cp_ref_qualifier ref_qual;
23197 tree exception_specification;
23198 tree late_return;
23199 tree attrs;
23200 bool memfn = (member_p || (pushed_scope
23201 && CLASS_TYPE_P (pushed_scope)));
23202 unsigned char local_variables_forbidden_p
23203 = parser->local_variables_forbidden_p;
23204 /* 'this' is not allowed in static member functions. */
23205 if (static_p || friend_p)
23206 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23208 is_declarator = true;
23210 if (ctor_dtor_or_conv_p)
23211 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23212 first = false;
23214 /* Parse the cv-qualifier-seq. */
23215 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23216 /* Parse the ref-qualifier. */
23217 ref_qual = cp_parser_ref_qualifier_opt (parser);
23218 /* Parse the tx-qualifier. */
23219 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23221 tree save_ccp = current_class_ptr;
23222 tree save_ccr = current_class_ref;
23223 if (memfn && !friend_p && !static_p)
23224 /* DR 1207: 'this' is in scope after the cv-quals. */
23225 inject_this_parameter (current_class_type, cv_quals);
23227 /* If it turned out that this is e.g. a pointer to a
23228 function, we don't want to delay noexcept parsing. */
23229 if (declarator == NULL || declarator->kind != cdk_id)
23230 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23232 /* Parse the exception-specification. */
23233 exception_specification
23234 = cp_parser_exception_specification_opt (parser,
23235 flags);
23237 attrs = cp_parser_std_attribute_spec_seq (parser);
23239 cp_omp_declare_simd_data odsd;
23240 if ((flag_openmp || flag_openmp_simd)
23241 && declarator
23242 && declarator->std_attributes
23243 && declarator->kind == cdk_id)
23245 tree *pa = &declarator->std_attributes;
23246 cp_parser_handle_directive_omp_attributes (parser, pa,
23247 &odsd, false);
23250 /* In here, we handle cases where attribute is used after
23251 the function declaration. For example:
23252 void func (int x) __attribute__((vector(..))); */
23253 tree gnu_attrs = NULL_TREE;
23254 tree requires_clause = NULL_TREE;
23255 late_return
23256 = cp_parser_late_return_type_opt (parser, declarator,
23257 requires_clause);
23259 cp_finalize_omp_declare_simd (parser, &odsd);
23261 /* Parse the virt-specifier-seq. */
23262 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23264 location_t parens_loc = make_location (parens_start,
23265 parens_start,
23266 parens_end);
23267 /* Create the function-declarator. */
23268 declarator = make_call_declarator (declarator,
23269 params,
23270 cv_quals,
23271 virt_specifiers,
23272 ref_qual,
23273 tx_qual,
23274 exception_specification,
23275 late_return,
23276 requires_clause,
23277 parens_loc);
23278 declarator->std_attributes = attrs;
23279 declarator->attributes = gnu_attrs;
23280 /* Any subsequent parameter lists are to do with
23281 return type, so are not those of the declared
23282 function. */
23283 parser->default_arg_ok_p = false;
23285 current_class_ptr = save_ccp;
23286 current_class_ref = save_ccr;
23288 /* Restore the state of local_variables_forbidden_p. */
23289 parser->local_variables_forbidden_p
23290 = local_variables_forbidden_p;
23293 /* Remove the function parms from scope. */
23294 pop_bindings_and_leave_scope ();
23296 if (is_declarator)
23297 /* Repeat the main loop. */
23298 continue;
23301 /* If this is the first, we can try a parenthesized
23302 declarator. */
23303 if (first)
23305 bool saved_in_type_id_in_expr_p;
23307 parser->default_arg_ok_p = saved_default_arg_ok_p;
23308 parser->in_declarator_p = saved_in_declarator_p;
23310 open_paren = token;
23311 /* Consume the `('. */
23312 matching_parens parens;
23313 parens.consume_open (parser);
23314 /* Parse the nested declarator. */
23315 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23316 parser->in_type_id_in_expr_p = true;
23317 declarator
23318 = cp_parser_declarator (parser, dcl_kind, flags,
23319 ctor_dtor_or_conv_p,
23320 /*parenthesized_p=*/NULL,
23321 member_p, friend_p,
23322 /*static_p=*/false);
23323 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23324 first = false;
23325 /* Expect a `)'. */
23326 close_paren = cp_lexer_peek_token (parser->lexer);
23327 if (!parens.require_close (parser))
23328 declarator = cp_error_declarator;
23329 if (declarator == cp_error_declarator)
23330 break;
23332 goto handle_declarator;
23334 /* Otherwise, we must be done. */
23335 else
23336 break;
23338 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23339 && token->type == CPP_OPEN_SQUARE
23340 && !cp_next_tokens_can_be_attribute_p (parser))
23342 /* Parse an array-declarator. */
23343 tree bounds, attrs;
23345 if (ctor_dtor_or_conv_p)
23346 *ctor_dtor_or_conv_p = 0;
23348 open_paren = NULL;
23349 first = false;
23350 parser->default_arg_ok_p = false;
23351 parser->in_declarator_p = true;
23352 /* Consume the `['. */
23353 cp_lexer_consume_token (parser->lexer);
23354 /* Peek at the next token. */
23355 token = cp_lexer_peek_token (parser->lexer);
23356 /* If the next token is `]', then there is no
23357 constant-expression. */
23358 if (token->type != CPP_CLOSE_SQUARE)
23360 bool non_constant_p;
23361 bounds
23362 = cp_parser_constant_expression (parser,
23363 /*allow_non_constant=*/true,
23364 &non_constant_p);
23365 if (!non_constant_p)
23366 /* OK */;
23367 else if (error_operand_p (bounds))
23368 /* Already gave an error. */;
23369 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23370 /* Let compute_array_index_type diagnose this. */;
23371 else if (!parser->in_function_body
23372 || parsing_function_declarator ())
23374 /* Normally, the array bound must be an integral constant
23375 expression. However, as an extension, we allow VLAs
23376 in function scopes as long as they aren't part of a
23377 parameter declaration. */
23378 cp_parser_error (parser,
23379 "array bound is not an integer constant");
23380 bounds = error_mark_node;
23382 else if (processing_template_decl
23383 && !type_dependent_expression_p (bounds))
23385 /* Remember this wasn't a constant-expression. */
23386 bounds = build_nop (TREE_TYPE (bounds), bounds);
23387 TREE_SIDE_EFFECTS (bounds) = 1;
23390 else
23391 bounds = NULL_TREE;
23392 /* Look for the closing `]'. */
23393 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23395 declarator = cp_error_declarator;
23396 break;
23399 attrs = cp_parser_std_attribute_spec_seq (parser);
23400 declarator = make_array_declarator (declarator, bounds);
23401 declarator->std_attributes = attrs;
23403 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23406 tree qualifying_scope;
23407 tree unqualified_name;
23408 tree attrs;
23409 special_function_kind sfk;
23410 bool abstract_ok;
23411 bool pack_expansion_p = false;
23412 cp_token *declarator_id_start_token;
23414 /* Parse a declarator-id */
23415 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23416 if (abstract_ok)
23418 cp_parser_parse_tentatively (parser);
23420 /* If we see an ellipsis, we should be looking at a
23421 parameter pack. */
23422 if (token->type == CPP_ELLIPSIS)
23424 /* Consume the `...' */
23425 cp_lexer_consume_token (parser->lexer);
23427 pack_expansion_p = true;
23431 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23432 unqualified_name
23433 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23434 qualifying_scope = parser->scope;
23435 if (abstract_ok)
23437 bool okay = false;
23439 if (!unqualified_name && pack_expansion_p)
23441 /* Check whether an error occurred. */
23442 okay = !cp_parser_error_occurred (parser);
23444 /* We already consumed the ellipsis to mark a
23445 parameter pack, but we have no way to report it,
23446 so abort the tentative parse. We will be exiting
23447 immediately anyway. */
23448 cp_parser_abort_tentative_parse (parser);
23450 else
23451 okay = cp_parser_parse_definitely (parser);
23453 if (!okay)
23454 unqualified_name = error_mark_node;
23455 else if (unqualified_name
23456 && (qualifying_scope
23457 || (!identifier_p (unqualified_name))))
23459 cp_parser_error (parser, "expected unqualified-id");
23460 unqualified_name = error_mark_node;
23464 if (!unqualified_name)
23465 return NULL;
23466 if (unqualified_name == error_mark_node)
23468 declarator = cp_error_declarator;
23469 pack_expansion_p = false;
23470 declarator->parameter_pack_p = false;
23471 break;
23474 attrs = cp_parser_std_attribute_spec_seq (parser);
23476 if (qualifying_scope && at_namespace_scope_p ()
23477 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23479 /* In the declaration of a member of a template class
23480 outside of the class itself, the SCOPE will sometimes
23481 be a TYPENAME_TYPE. For example, given:
23483 template <typename T>
23484 int S<T>::R::i = 3;
23486 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23487 this context, we must resolve S<T>::R to an ordinary
23488 type, rather than a typename type.
23490 The reason we normally avoid resolving TYPENAME_TYPEs
23491 is that a specialization of `S' might render
23492 `S<T>::R' not a type. However, if `S' is
23493 specialized, then this `i' will not be used, so there
23494 is no harm in resolving the types here. */
23495 tree type;
23497 /* Resolve the TYPENAME_TYPE. */
23498 type = resolve_typename_type (qualifying_scope,
23499 /*only_current_p=*/false);
23500 /* If that failed, the declarator is invalid. */
23501 if (TREE_CODE (type) == TYPENAME_TYPE)
23503 if (typedef_variant_p (type))
23504 error_at (declarator_id_start_token->location,
23505 "cannot define member of dependent typedef "
23506 "%qT", type);
23507 else
23508 error_at (declarator_id_start_token->location,
23509 "%<%T::%E%> is not a type",
23510 TYPE_CONTEXT (qualifying_scope),
23511 TYPE_IDENTIFIER (qualifying_scope));
23513 qualifying_scope = type;
23516 sfk = sfk_none;
23518 if (unqualified_name)
23520 tree class_type;
23522 if (qualifying_scope
23523 && CLASS_TYPE_P (qualifying_scope))
23524 class_type = qualifying_scope;
23525 else
23526 class_type = current_class_type;
23528 if (TREE_CODE (unqualified_name) == TYPE_DECL)
23530 tree name_type = TREE_TYPE (unqualified_name);
23532 if (!class_type || !same_type_p (name_type, class_type))
23534 /* We do not attempt to print the declarator
23535 here because we do not have enough
23536 information about its original syntactic
23537 form. */
23538 cp_parser_error (parser, "invalid declarator");
23539 declarator = cp_error_declarator;
23540 break;
23542 else if (qualifying_scope
23543 && CLASSTYPE_USE_TEMPLATE (name_type))
23545 error_at (declarator_id_start_token->location,
23546 "invalid use of constructor as a template");
23547 inform (declarator_id_start_token->location,
23548 "use %<%T::%D%> instead of %<%T::%D%> to "
23549 "name the constructor in a qualified name",
23550 class_type,
23551 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23552 class_type, name_type);
23553 declarator = cp_error_declarator;
23554 break;
23556 unqualified_name = constructor_name (class_type);
23559 if (class_type)
23561 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23562 sfk = sfk_destructor;
23563 else if (identifier_p (unqualified_name)
23564 && IDENTIFIER_CONV_OP_P (unqualified_name))
23565 sfk = sfk_conversion;
23566 else if (/* There's no way to declare a constructor
23567 for an unnamed type, even if the type
23568 got a name for linkage purposes. */
23569 !TYPE_WAS_UNNAMED (class_type)
23570 /* Handle correctly (c++/19200):
23572 struct S {
23573 struct T{};
23574 friend void S(T);
23577 and also:
23579 namespace N {
23580 void S();
23583 struct S {
23584 friend void N::S();
23585 }; */
23586 && (!friend_p || class_type == qualifying_scope)
23587 && constructor_name_p (unqualified_name,
23588 class_type))
23589 sfk = sfk_constructor;
23590 else if (is_overloaded_fn (unqualified_name)
23591 && DECL_CONSTRUCTOR_P (get_first_fn
23592 (unqualified_name)))
23593 sfk = sfk_constructor;
23595 if (ctor_dtor_or_conv_p && sfk != sfk_none)
23596 *ctor_dtor_or_conv_p = -1;
23599 declarator = make_id_declarator (qualifying_scope,
23600 unqualified_name,
23601 sfk, token->location);
23602 declarator->std_attributes = attrs;
23603 declarator->parameter_pack_p = pack_expansion_p;
23605 if (pack_expansion_p)
23606 maybe_warn_variadic_templates ();
23608 /* We're looking for this case in [temp.res]:
23609 A qualified-id is assumed to name a type if [...]
23610 - it is a decl-specifier of the decl-specifier-seq of a
23611 parameter-declaration in a declarator of a function or
23612 function template declaration, ... */
23613 if (cxx_dialect >= cxx20
23614 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
23615 && declarator->kind == cdk_id
23616 && !at_class_scope_p ()
23617 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23619 /* ...whose declarator-id is qualified. If it isn't, never
23620 assume the parameters to refer to types. */
23621 if (qualifying_scope == NULL_TREE)
23622 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23623 else
23625 /* Now we have something like
23626 template <typename T> int C::x(S::p);
23627 which can be a function template declaration or a
23628 variable template definition. If name lookup for
23629 the declarator-id C::x finds one or more function
23630 templates, assume S::p to name a type. Otherwise,
23631 don't. */
23632 tree decl
23633 = cp_parser_lookup_name (parser, unqualified_name,
23634 none_type,
23635 /*is_template=*/false,
23636 /*is_namespace=*/false,
23637 /*check_dependency=*/false,
23638 /*ambiguous_decls=*/NULL,
23639 token->location);
23641 if (!is_overloaded_fn (decl)
23642 /* Allow
23643 template<typename T>
23644 A<T>::A(T::type) { } */
23645 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
23646 && constructor_name_p (unqualified_name,
23647 qualifying_scope)))
23648 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23653 handle_declarator:;
23654 scope = get_scope_of_declarator (declarator);
23655 if (scope)
23657 /* Any names that appear after the declarator-id for a
23658 member are looked up in the containing scope. */
23659 if (at_function_scope_p ())
23661 /* But declarations with qualified-ids can't appear in a
23662 function. */
23663 cp_parser_error (parser, "qualified-id in declaration");
23664 declarator = cp_error_declarator;
23665 break;
23667 pushed_scope = push_scope (scope);
23669 parser->in_declarator_p = true;
23670 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
23671 || (declarator && declarator->kind == cdk_id))
23672 /* Default args are only allowed on function
23673 declarations. */
23674 parser->default_arg_ok_p = saved_default_arg_ok_p;
23675 else
23676 parser->default_arg_ok_p = false;
23678 first = false;
23680 /* We're done. */
23681 else
23682 break;
23685 /* For an abstract declarator, we might wind up with nothing at this
23686 point. That's an error; the declarator is not optional. */
23687 if (!declarator)
23688 cp_parser_error (parser, "expected declarator");
23689 else if (open_paren)
23691 /* Record overly parenthesized declarator so we can give a
23692 diagnostic about confusing decl/expr disambiguation. */
23693 if (declarator->kind == cdk_array)
23695 /* If the open and close parens are on different lines, this
23696 is probably a formatting thing, so ignore. */
23697 expanded_location open = expand_location (open_paren->location);
23698 expanded_location close = expand_location (close_paren->location);
23699 if (open.line != close.line || open.file != close.file)
23700 open_paren = NULL;
23702 if (open_paren)
23703 declarator->parenthesized = make_location (open_paren->location,
23704 open_paren->location,
23705 close_paren->location);
23708 /* If we entered a scope, we must exit it now. */
23709 if (pushed_scope)
23710 pop_scope (pushed_scope);
23712 parser->default_arg_ok_p = saved_default_arg_ok_p;
23713 parser->in_declarator_p = saved_in_declarator_p;
23715 return declarator;
23718 /* Parse a ptr-operator.
23720 ptr-operator:
23721 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23722 * cv-qualifier-seq [opt]
23724 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23725 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23727 GNU Extension:
23729 ptr-operator:
23730 & cv-qualifier-seq [opt]
23732 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23733 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23734 an rvalue reference. In the case of a pointer-to-member, *TYPE is
23735 filled in with the TYPE containing the member. *CV_QUALS is
23736 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23737 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
23738 Note that the tree codes returned by this function have nothing
23739 to do with the types of trees that will be eventually be created
23740 to represent the pointer or reference type being parsed. They are
23741 just constants with suggestive names. */
23742 static enum tree_code
23743 cp_parser_ptr_operator (cp_parser* parser,
23744 tree* type,
23745 cp_cv_quals *cv_quals,
23746 tree *attributes)
23748 enum tree_code code = ERROR_MARK;
23749 cp_token *token;
23750 tree attrs = NULL_TREE;
23752 /* Assume that it's not a pointer-to-member. */
23753 *type = NULL_TREE;
23754 /* And that there are no cv-qualifiers. */
23755 *cv_quals = TYPE_UNQUALIFIED;
23757 /* Peek at the next token. */
23758 token = cp_lexer_peek_token (parser->lexer);
23760 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
23761 if (token->type == CPP_MULT)
23762 code = INDIRECT_REF;
23763 else if (token->type == CPP_AND)
23764 code = ADDR_EXPR;
23765 else if ((cxx_dialect != cxx98) &&
23766 token->type == CPP_AND_AND) /* C++0x only */
23767 code = NON_LVALUE_EXPR;
23769 if (code != ERROR_MARK)
23771 /* Consume the `*', `&' or `&&'. */
23772 cp_lexer_consume_token (parser->lexer);
23774 /* A `*' can be followed by a cv-qualifier-seq, and so can a
23775 `&', if we are allowing GNU extensions. (The only qualifier
23776 that can legally appear after `&' is `restrict', but that is
23777 enforced during semantic analysis. */
23778 if (code == INDIRECT_REF
23779 || cp_parser_allow_gnu_extensions_p (parser))
23780 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23782 attrs = cp_parser_std_attribute_spec_seq (parser);
23783 if (attributes != NULL)
23784 *attributes = attrs;
23786 else
23788 /* Try the pointer-to-member case. */
23789 cp_parser_parse_tentatively (parser);
23790 /* Look for the optional `::' operator. */
23791 cp_parser_global_scope_opt (parser,
23792 /*current_scope_valid_p=*/false);
23793 /* Look for the nested-name specifier. */
23794 token = cp_lexer_peek_token (parser->lexer);
23795 cp_parser_nested_name_specifier (parser,
23796 /*typename_keyword_p=*/false,
23797 /*check_dependency_p=*/true,
23798 /*type_p=*/false,
23799 /*is_declaration=*/false);
23800 /* If we found it, and the next token is a `*', then we are
23801 indeed looking at a pointer-to-member operator. */
23802 if (!cp_parser_error_occurred (parser)
23803 && cp_parser_require (parser, CPP_MULT, RT_MULT))
23805 /* Indicate that the `*' operator was used. */
23806 code = INDIRECT_REF;
23808 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
23809 error_at (token->location, "%qD is a namespace", parser->scope);
23810 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
23811 error_at (token->location, "cannot form pointer to member of "
23812 "non-class %q#T", parser->scope);
23813 else
23815 /* The type of which the member is a member is given by the
23816 current SCOPE. */
23817 *type = parser->scope;
23818 /* The next name will not be qualified. */
23819 parser->scope = NULL_TREE;
23820 parser->qualifying_scope = NULL_TREE;
23821 parser->object_scope = NULL_TREE;
23822 /* Look for optional c++11 attributes. */
23823 attrs = cp_parser_std_attribute_spec_seq (parser);
23824 if (attributes != NULL)
23825 *attributes = attrs;
23826 /* Look for the optional cv-qualifier-seq. */
23827 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23830 /* If that didn't work we don't have a ptr-operator. */
23831 if (!cp_parser_parse_definitely (parser))
23832 cp_parser_error (parser, "expected ptr-operator");
23835 return code;
23838 /* Parse an (optional) cv-qualifier-seq.
23840 cv-qualifier-seq:
23841 cv-qualifier cv-qualifier-seq [opt]
23843 cv-qualifier:
23844 const
23845 volatile
23847 GNU Extension:
23849 cv-qualifier:
23850 __restrict__
23852 Returns a bitmask representing the cv-qualifiers. */
23854 static cp_cv_quals
23855 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
23857 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
23859 while (true)
23861 cp_token *token;
23862 cp_cv_quals cv_qualifier;
23864 /* Peek at the next token. */
23865 token = cp_lexer_peek_token (parser->lexer);
23866 /* See if it's a cv-qualifier. */
23867 switch (token->keyword)
23869 case RID_CONST:
23870 cv_qualifier = TYPE_QUAL_CONST;
23871 break;
23873 case RID_VOLATILE:
23874 cv_qualifier = TYPE_QUAL_VOLATILE;
23875 break;
23877 case RID_RESTRICT:
23878 cv_qualifier = TYPE_QUAL_RESTRICT;
23879 break;
23881 default:
23882 cv_qualifier = TYPE_UNQUALIFIED;
23883 break;
23886 if (!cv_qualifier)
23887 break;
23889 if (cv_quals & cv_qualifier)
23891 gcc_rich_location richloc (token->location);
23892 richloc.add_fixit_remove ();
23893 error_at (&richloc, "duplicate cv-qualifier");
23894 cp_lexer_purge_token (parser->lexer);
23896 else
23898 cp_lexer_consume_token (parser->lexer);
23899 cv_quals |= cv_qualifier;
23903 return cv_quals;
23906 /* Parse an (optional) ref-qualifier
23908 ref-qualifier:
23912 Returns cp_ref_qualifier representing ref-qualifier. */
23914 static cp_ref_qualifier
23915 cp_parser_ref_qualifier_opt (cp_parser* parser)
23917 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
23919 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
23920 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
23921 return ref_qual;
23923 while (true)
23925 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
23926 cp_token *token = cp_lexer_peek_token (parser->lexer);
23928 switch (token->type)
23930 case CPP_AND:
23931 curr_ref_qual = REF_QUAL_LVALUE;
23932 break;
23934 case CPP_AND_AND:
23935 curr_ref_qual = REF_QUAL_RVALUE;
23936 break;
23938 default:
23939 curr_ref_qual = REF_QUAL_NONE;
23940 break;
23943 if (!curr_ref_qual)
23944 break;
23945 else if (ref_qual)
23947 error_at (token->location, "multiple ref-qualifiers");
23948 cp_lexer_purge_token (parser->lexer);
23950 else
23952 ref_qual = curr_ref_qual;
23953 cp_lexer_consume_token (parser->lexer);
23957 return ref_qual;
23960 /* Parse an optional tx-qualifier.
23962 tx-qualifier:
23963 transaction_safe
23964 transaction_safe_dynamic */
23966 static tree
23967 cp_parser_tx_qualifier_opt (cp_parser *parser)
23969 cp_token *token = cp_lexer_peek_token (parser->lexer);
23970 if (token->type == CPP_NAME)
23972 tree name = token->u.value;
23973 const char *p = IDENTIFIER_POINTER (name);
23974 const int len = strlen ("transaction_safe");
23975 if (startswith (p, "transaction_safe"))
23977 p += len;
23978 if (*p == '\0'
23979 || !strcmp (p, "_dynamic"))
23981 cp_lexer_consume_token (parser->lexer);
23982 if (!flag_tm)
23984 error ("%qE requires %<-fgnu-tm%>", name);
23985 return NULL_TREE;
23987 else
23988 return name;
23992 return NULL_TREE;
23995 /* Parse an (optional) virt-specifier-seq.
23997 virt-specifier-seq:
23998 virt-specifier virt-specifier-seq [opt]
24000 virt-specifier:
24001 override
24002 final
24004 Returns a bitmask representing the virt-specifiers. */
24006 static cp_virt_specifiers
24007 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24009 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24011 while (true)
24013 cp_token *token;
24014 cp_virt_specifiers virt_specifier;
24016 /* Peek at the next token. */
24017 token = cp_lexer_peek_token (parser->lexer);
24018 /* See if it's a virt-specifier-qualifier. */
24019 if (token->type != CPP_NAME)
24020 break;
24021 if (id_equal (token->u.value, "override"))
24023 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24024 virt_specifier = VIRT_SPEC_OVERRIDE;
24026 else if (id_equal (token->u.value, "final"))
24028 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24029 virt_specifier = VIRT_SPEC_FINAL;
24031 else if (id_equal (token->u.value, "__final"))
24033 virt_specifier = VIRT_SPEC_FINAL;
24035 else
24036 break;
24038 if (virt_specifiers & virt_specifier)
24040 gcc_rich_location richloc (token->location);
24041 richloc.add_fixit_remove ();
24042 error_at (&richloc, "duplicate virt-specifier");
24043 cp_lexer_purge_token (parser->lexer);
24045 else
24047 cp_lexer_consume_token (parser->lexer);
24048 virt_specifiers |= virt_specifier;
24051 return virt_specifiers;
24054 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24055 is in scope even though it isn't real. */
24057 void
24058 inject_this_parameter (tree ctype, cp_cv_quals quals)
24060 tree this_parm;
24062 if (current_class_ptr)
24064 /* We don't clear this between NSDMIs. Is it already what we want? */
24065 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24066 if (DECL_P (current_class_ptr)
24067 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24068 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24069 && cp_type_quals (type) == quals)
24070 return;
24073 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24074 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24075 current_class_ptr = NULL_TREE;
24076 current_class_ref
24077 = cp_build_fold_indirect_ref (this_parm);
24078 current_class_ptr = this_parm;
24081 /* Return true iff our current scope is a non-static data member
24082 initializer. */
24084 bool
24085 parsing_nsdmi (void)
24087 /* We recognize NSDMI context by the context-less 'this' pointer set up
24088 by the function above. */
24089 if (current_class_ptr
24090 && TREE_CODE (current_class_ptr) == PARM_DECL
24091 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24092 return true;
24093 return false;
24096 /* True if we're parsing a function declarator. */
24098 bool
24099 parsing_function_declarator ()
24101 /* this_entity is NULL for a function parameter scope while parsing the
24102 declarator; it is set when parsing the body of the function. */
24103 return (current_binding_level->kind == sk_function_parms
24104 && !current_binding_level->this_entity);
24107 /* Parse a late-specified return type, if any. This is not a separate
24108 non-terminal, but part of a function declarator, which looks like
24110 -> trailing-type-specifier-seq abstract-declarator(opt)
24112 Returns the type indicated by the type-id.
24114 In addition to this, parse any queued up #pragma omp declare simd
24115 clauses, and #pragma acc routine clauses.
24117 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24118 function. */
24120 static tree
24121 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24122 tree& requires_clause)
24124 cp_token *token;
24125 tree type = NULL_TREE;
24126 bool declare_simd_p = (parser->omp_declare_simd
24127 && declarator
24128 && declarator->kind == cdk_id);
24130 bool oacc_routine_p = (parser->oacc_routine
24131 && declarator
24132 && declarator->kind == cdk_id);
24134 /* Peek at the next token. */
24135 token = cp_lexer_peek_token (parser->lexer);
24136 /* A late-specified return type is indicated by an initial '->'. */
24137 if (token->type != CPP_DEREF
24138 && token->keyword != RID_REQUIRES
24139 && !(token->type == CPP_NAME
24140 && token->u.value == ridpointers[RID_REQUIRES])
24141 && !(declare_simd_p || oacc_routine_p))
24142 return NULL_TREE;
24144 if (token->type == CPP_DEREF)
24146 /* Consume the ->. */
24147 cp_lexer_consume_token (parser->lexer);
24149 type = cp_parser_trailing_type_id (parser);
24152 /* Function declarations may be followed by a trailing
24153 requires-clause. */
24154 requires_clause = cp_parser_requires_clause_opt (parser, false);
24156 if (declare_simd_p)
24157 declarator->attributes
24158 = cp_parser_late_parsing_omp_declare_simd (parser,
24159 declarator->attributes);
24160 if (oacc_routine_p)
24161 declarator->attributes
24162 = cp_parser_late_parsing_oacc_routine (parser,
24163 declarator->attributes);
24165 return type;
24168 /* Parse a declarator-id.
24170 declarator-id:
24171 id-expression
24172 :: [opt] nested-name-specifier [opt] type-name
24174 In the `id-expression' case, the value returned is as for
24175 cp_parser_id_expression if the id-expression was an unqualified-id.
24176 If the id-expression was a qualified-id, then a SCOPE_REF is
24177 returned. The first operand is the scope (either a NAMESPACE_DECL
24178 or TREE_TYPE), but the second is still just a representation of an
24179 unqualified-id. */
24181 static tree
24182 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24184 tree id;
24185 /* The expression must be an id-expression. Assume that qualified
24186 names are the names of types so that:
24188 template <class T>
24189 int S<T>::R::i = 3;
24191 will work; we must treat `S<T>::R' as the name of a type.
24192 Similarly, assume that qualified names are templates, where
24193 required, so that:
24195 template <class T>
24196 int S<T>::R<T>::i = 3;
24198 will work, too. */
24199 id = cp_parser_id_expression (parser,
24200 /*template_keyword_p=*/false,
24201 /*check_dependency_p=*/false,
24202 /*template_p=*/NULL,
24203 /*declarator_p=*/true,
24204 optional_p);
24205 if (id && BASELINK_P (id))
24206 id = BASELINK_FUNCTIONS (id);
24207 return id;
24210 /* Parse a type-id.
24212 type-id:
24213 type-specifier-seq abstract-declarator [opt]
24215 The parser flags FLAGS is used to control type-specifier parsing.
24217 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24219 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24220 i.e. we've just seen "->".
24222 Returns the TYPE specified. */
24224 static tree
24225 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24226 bool is_template_arg, bool is_trailing_return,
24227 location_t *type_location)
24229 cp_decl_specifier_seq type_specifier_seq;
24230 cp_declarator *abstract_declarator;
24232 /* Parse the type-specifier-seq. */
24233 cp_parser_type_specifier_seq (parser, flags,
24234 /*is_declaration=*/false,
24235 is_trailing_return,
24236 &type_specifier_seq);
24237 if (type_location)
24238 *type_location = type_specifier_seq.locations[ds_type_spec];
24240 if (is_template_arg && type_specifier_seq.type
24241 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24242 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24243 /* A bare template name as a template argument is a template template
24244 argument, not a placeholder, so fail parsing it as a type argument. */
24246 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24247 cp_parser_simulate_error (parser);
24248 return error_mark_node;
24250 if (type_specifier_seq.type == error_mark_node)
24251 return error_mark_node;
24253 /* There might or might not be an abstract declarator. */
24254 cp_parser_parse_tentatively (parser);
24255 /* Look for the declarator. */
24256 abstract_declarator
24257 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24258 CP_PARSER_FLAGS_NONE, NULL,
24259 /*parenthesized_p=*/NULL,
24260 /*member_p=*/false,
24261 /*friend_p=*/false,
24262 /*static_p=*/false);
24263 /* Check to see if there really was a declarator. */
24264 if (!cp_parser_parse_definitely (parser))
24265 abstract_declarator = NULL;
24267 bool auto_typeid_ok = false;
24268 /* The concepts TS allows 'auto' as a type-id. */
24269 if (flag_concepts_ts)
24270 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24271 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24272 outside the template-argument-list context here only for the sake of
24273 diagnostic: grokdeclarator then can emit a better error message for
24274 e.g. using T = auto. */
24275 else if (flag_concepts)
24276 auto_typeid_ok = (!parser->in_type_id_in_expr_p
24277 && !parser->in_template_argument_list_p);
24279 if (type_specifier_seq.type
24280 && !auto_typeid_ok
24281 /* None of the valid uses of 'auto' in C++14 involve the type-id
24282 nonterminal, but it is valid in a trailing-return-type. */
24283 && !(cxx_dialect >= cxx14 && is_trailing_return))
24284 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24286 /* A type-id with type 'auto' is only ok if the abstract declarator
24287 is a function declarator with a late-specified return type.
24289 A type-id with 'auto' is also valid in a trailing-return-type
24290 in a compound-requirement. */
24291 if (abstract_declarator
24292 && abstract_declarator->kind == cdk_function
24293 && abstract_declarator->u.function.late_return_type)
24294 /* OK */;
24295 else if (parser->in_result_type_constraint_p)
24296 /* OK */;
24297 else
24299 if (!cp_parser_simulate_error (parser))
24301 location_t loc = type_specifier_seq.locations[ds_type_spec];
24302 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24304 error_at (loc, "missing template arguments after %qT",
24305 auto_node);
24306 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24307 tmpl);
24309 else if (parser->in_template_argument_list_p)
24310 error_at (loc, "%qT not permitted in template argument",
24311 auto_node);
24312 else
24313 error_at (loc, "invalid use of %qT", auto_node);
24315 return error_mark_node;
24319 return groktypename (&type_specifier_seq, abstract_declarator,
24320 is_template_arg);
24323 /* Wrapper for cp_parser_type_id_1. */
24325 static tree
24326 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24327 location_t *type_location)
24329 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24332 /* Wrapper for cp_parser_type_id_1. */
24334 static tree
24335 cp_parser_template_type_arg (cp_parser *parser)
24337 tree r;
24338 const char *saved_message = parser->type_definition_forbidden_message;
24339 parser->type_definition_forbidden_message
24340 = G_("types may not be defined in template arguments");
24341 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24342 parser->type_definition_forbidden_message = saved_message;
24343 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
24345 error ("invalid use of %<auto%> in template argument");
24346 r = error_mark_node;
24348 return r;
24351 /* Wrapper for cp_parser_type_id_1. */
24353 static tree
24354 cp_parser_trailing_type_id (cp_parser *parser)
24356 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24357 false, true, NULL);
24360 /* Parse a type-specifier-seq.
24362 type-specifier-seq:
24363 type-specifier type-specifier-seq [opt]
24365 GNU extension:
24367 type-specifier-seq:
24368 attributes type-specifier-seq [opt]
24370 The parser flags FLAGS is used to control type-specifier parsing.
24372 If IS_DECLARATION is true, we are at the start of a "condition" or
24373 exception-declaration, so we might be followed by a declarator-id.
24375 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24376 i.e. we've just seen "->".
24378 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24380 static void
24381 cp_parser_type_specifier_seq (cp_parser* parser,
24382 cp_parser_flags flags,
24383 bool is_declaration,
24384 bool is_trailing_return,
24385 cp_decl_specifier_seq *type_specifier_seq)
24387 bool seen_type_specifier = false;
24388 cp_token *start_token = NULL;
24390 /* Clear the TYPE_SPECIFIER_SEQ. */
24391 clear_decl_specs (type_specifier_seq);
24393 flags |= CP_PARSER_FLAGS_OPTIONAL;
24394 /* In the context of a trailing return type, enum E { } is an
24395 elaborated-type-specifier followed by a function-body, not an
24396 enum-specifier. */
24397 if (is_trailing_return)
24398 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24400 /* Parse the type-specifiers and attributes. */
24401 while (true)
24403 tree type_specifier;
24404 bool is_cv_qualifier;
24406 /* Check for attributes first. */
24407 if (cp_next_tokens_can_be_attribute_p (parser))
24409 /* GNU attributes at the end of a declaration apply to the
24410 declaration as a whole, not to the trailing return type. So look
24411 ahead to see if these attributes are at the end. */
24412 if (seen_type_specifier && is_trailing_return
24413 && cp_next_tokens_can_be_gnu_attribute_p (parser))
24415 size_t n = cp_parser_skip_attributes_opt (parser, 1);
24416 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24417 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24418 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24419 break;
24421 type_specifier_seq->attributes
24422 = attr_chainon (type_specifier_seq->attributes,
24423 cp_parser_attributes_opt (parser));
24424 continue;
24427 /* record the token of the beginning of the type specifier seq,
24428 for error reporting purposes*/
24429 if (!start_token)
24430 start_token = cp_lexer_peek_token (parser->lexer);
24432 /* Look for the type-specifier. */
24433 type_specifier = cp_parser_type_specifier (parser,
24434 flags,
24435 type_specifier_seq,
24436 /*is_declaration=*/false,
24437 NULL,
24438 &is_cv_qualifier);
24439 if (!type_specifier)
24441 /* If the first type-specifier could not be found, this is not a
24442 type-specifier-seq at all. */
24443 if (!seen_type_specifier)
24445 /* Set in_declarator_p to avoid skipping to the semicolon. */
24446 int in_decl = parser->in_declarator_p;
24447 parser->in_declarator_p = true;
24449 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24450 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24451 cp_parser_error (parser, "expected type-specifier");
24453 parser->in_declarator_p = in_decl;
24455 type_specifier_seq->type = error_mark_node;
24456 return;
24458 /* If subsequent type-specifiers could not be found, the
24459 type-specifier-seq is complete. */
24460 break;
24463 seen_type_specifier = true;
24464 /* The standard says that a condition can be:
24466 type-specifier-seq declarator = assignment-expression
24468 However, given:
24470 struct S {};
24471 if (int S = ...)
24473 we should treat the "S" as a declarator, not as a
24474 type-specifier. The standard doesn't say that explicitly for
24475 type-specifier-seq, but it does say that for
24476 decl-specifier-seq in an ordinary declaration. Perhaps it
24477 would be clearer just to allow a decl-specifier-seq here, and
24478 then add a semantic restriction that if any decl-specifiers
24479 that are not type-specifiers appear, the program is invalid. */
24480 if (is_declaration && !is_cv_qualifier)
24481 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24485 /* Return whether the function currently being declared has an associated
24486 template parameter list. */
24488 static bool
24489 function_being_declared_is_template_p (cp_parser* parser)
24491 if (!current_template_parms || processing_template_parmlist)
24492 return false;
24494 if (parser->implicit_template_scope)
24495 return true;
24497 if (at_class_scope_p ()
24498 && TYPE_BEING_DEFINED (current_class_type))
24499 return parser->num_template_parameter_lists != 0;
24501 return ((int) parser->num_template_parameter_lists > template_class_depth
24502 (current_class_type));
24505 /* Parse a parameter-declaration-clause.
24507 parameter-declaration-clause:
24508 parameter-declaration-list [opt] ... [opt]
24509 parameter-declaration-list , ...
24511 The parser flags FLAGS is used to control type-specifier parsing.
24513 Returns a representation for the parameter declarations. A return
24514 value of NULL indicates a parameter-declaration-clause consisting
24515 only of an ellipsis. */
24517 static tree
24518 cp_parser_parameter_declaration_clause (cp_parser* parser,
24519 cp_parser_flags flags)
24521 tree parameters;
24522 cp_token *token;
24523 bool ellipsis_p;
24525 auto cleanup = make_temp_override
24526 (parser->auto_is_implicit_function_template_parm_p);
24528 if (!processing_specialization
24529 && !processing_template_parmlist
24530 && !processing_explicit_instantiation
24531 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24532 actual function or a random abstract declarator. */
24533 && parser->default_arg_ok_p)
24534 if (!current_function_decl
24535 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24536 parser->auto_is_implicit_function_template_parm_p = true;
24538 /* Peek at the next token. */
24539 token = cp_lexer_peek_token (parser->lexer);
24540 /* Check for trivial parameter-declaration-clauses. */
24541 if (token->type == CPP_ELLIPSIS)
24543 /* Consume the `...' token. */
24544 cp_lexer_consume_token (parser->lexer);
24545 return NULL_TREE;
24547 else if (token->type == CPP_CLOSE_PAREN)
24548 /* There are no parameters. */
24549 return void_list_node;
24550 /* Check for `(void)', too, which is a special case. */
24551 else if (token->keyword == RID_VOID
24552 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24553 == CPP_CLOSE_PAREN))
24555 /* Consume the `void' token. */
24556 cp_lexer_consume_token (parser->lexer);
24557 /* There are no parameters. */
24558 return explicit_void_list_node;
24561 /* A vector of parameters that haven't been pushed yet. */
24562 auto_vec<tree> pending_decls;
24564 /* Parse the parameter-declaration-list. */
24565 parameters = cp_parser_parameter_declaration_list (parser, flags,
24566 &pending_decls);
24567 /* If a parse error occurred while parsing the
24568 parameter-declaration-list, then the entire
24569 parameter-declaration-clause is erroneous. */
24570 if (parameters == error_mark_node)
24571 return NULL_TREE;
24573 /* Peek at the next token. */
24574 token = cp_lexer_peek_token (parser->lexer);
24575 /* If it's a `,', the clause should terminate with an ellipsis. */
24576 if (token->type == CPP_COMMA)
24578 /* Consume the `,'. */
24579 cp_lexer_consume_token (parser->lexer);
24580 /* Expect an ellipsis. */
24581 ellipsis_p
24582 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24584 /* It might also be `...' if the optional trailing `,' was
24585 omitted. */
24586 else if (token->type == CPP_ELLIPSIS)
24588 /* Consume the `...' token. */
24589 cp_lexer_consume_token (parser->lexer);
24590 /* And remember that we saw it. */
24591 ellipsis_p = true;
24593 else
24594 ellipsis_p = false;
24596 /* A valid parameter-declaration-clause can only be followed by a ')'.
24597 So it's time to push all the parameters we have seen now that we
24598 know we have a valid declaration. Note that here we may not have
24599 committed yet, nor should we. Pushing here will detect the error
24600 of redefining a parameter. */
24601 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24602 for (tree p : pending_decls)
24603 pushdecl (p);
24605 /* Finish the parameter list. */
24606 if (!ellipsis_p)
24607 parameters = chainon (parameters, void_list_node);
24609 return parameters;
24612 /* Parse a parameter-declaration-list.
24614 parameter-declaration-list:
24615 parameter-declaration
24616 parameter-declaration-list , parameter-declaration
24618 The parser flags FLAGS is used to control type-specifier parsing.
24619 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
24621 Returns a representation of the parameter-declaration-list, as for
24622 cp_parser_parameter_declaration_clause. However, the
24623 `void_list_node' is never appended to the list. */
24625 static tree
24626 cp_parser_parameter_declaration_list (cp_parser* parser,
24627 cp_parser_flags flags,
24628 auto_vec<tree> *pending_decls)
24630 tree parameters = NULL_TREE;
24631 tree *tail = &parameters;
24632 bool saved_in_unbraced_linkage_specification_p;
24633 int index = 0;
24635 /* The special considerations that apply to a function within an
24636 unbraced linkage specifications do not apply to the parameters
24637 to the function. */
24638 saved_in_unbraced_linkage_specification_p
24639 = parser->in_unbraced_linkage_specification_p;
24640 parser->in_unbraced_linkage_specification_p = false;
24642 /* Look for more parameters. */
24643 while (true)
24645 cp_parameter_declarator *parameter;
24646 tree decl = error_mark_node;
24647 bool parenthesized_p = false;
24649 /* Parse the parameter. */
24650 parameter
24651 = cp_parser_parameter_declaration (parser, flags,
24652 /*template_parm_p=*/false,
24653 &parenthesized_p);
24655 /* We don't know yet if the enclosing context is unavailable or deprecated,
24656 so wait and deal with it in grokparms if appropriate. */
24657 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
24659 if (parameter && !cp_parser_error_occurred (parser))
24661 decl = grokdeclarator (parameter->declarator,
24662 &parameter->decl_specifiers,
24663 PARM,
24664 parameter->default_argument != NULL_TREE,
24665 &parameter->decl_specifiers.attributes);
24666 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
24667 DECL_SOURCE_LOCATION (decl) = parameter->loc;
24670 deprecated_state = DEPRECATED_NORMAL;
24672 /* If a parse error occurred parsing the parameter declaration,
24673 then the entire parameter-declaration-list is erroneous. */
24674 if (decl == error_mark_node)
24676 parameters = error_mark_node;
24677 break;
24680 if (parameter->decl_specifiers.attributes)
24681 cplus_decl_attributes (&decl,
24682 parameter->decl_specifiers.attributes,
24684 if (DECL_NAME (decl))
24686 /* We cannot always pushdecl while parsing tentatively because
24687 it may have side effects and we can't be sure yet if we're
24688 parsing a declaration, e.g.:
24690 S foo(int(x), int(x), int{x});
24692 where it's not clear if we're dealing with a constructor call
24693 or a function declaration until we've seen the last argument
24694 which breaks it up.
24695 It's safe to pushdecl so long as it doesn't result in a clash
24696 with an already-pushed parameter. But we don't delay pushing
24697 different parameters to handle
24699 S foo(int(i), decltype(i) j = 42);
24701 which is valid. */
24702 if (pending_decls
24703 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24704 /* See if PARAMETERS already contains a parameter with the same
24705 DECL_NAME as DECL. */
24706 && [parameters, decl] {
24707 for (tree p = parameters; p; p = TREE_CHAIN (p))
24708 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
24709 return true;
24710 return false;
24711 }())
24712 pending_decls->safe_push (decl);
24713 else
24714 decl = pushdecl (decl);
24717 if (decl != error_mark_node)
24719 retrofit_lang_decl (decl);
24720 DECL_PARM_INDEX (decl) = ++index;
24721 DECL_PARM_LEVEL (decl) = function_parm_depth ();
24724 /* Add the new parameter to the list. */
24725 *tail = build_tree_list (parameter->default_argument, decl);
24726 tail = &TREE_CHAIN (*tail);
24728 /* If the parameters were parenthesized, it's the case of
24729 T foo(X(x)) which looks like a variable definition but
24730 is a function declaration. */
24731 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
24732 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
24734 /* Peek at the next token. */
24735 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
24736 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
24737 /* These are for Objective-C++ */
24738 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24739 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24740 /* The parameter-declaration-list is complete. */
24741 break;
24742 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24744 cp_token *token;
24746 /* Peek at the next token. */
24747 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24748 /* If it's an ellipsis, then the list is complete. */
24749 if (token->type == CPP_ELLIPSIS)
24750 break;
24751 /* Otherwise, there must be more parameters. Consume the
24752 `,'. */
24753 cp_lexer_consume_token (parser->lexer);
24754 /* When parsing something like:
24756 int i(float f, double d)
24758 we can tell after seeing the declaration for "f" that we
24759 are not looking at an initialization of a variable "i",
24760 but rather at the declaration of a function "i".
24762 Due to the fact that the parsing of template arguments
24763 (as specified to a template-id) requires backtracking we
24764 cannot use this technique when inside a template argument
24765 list. */
24766 if (!parser->in_template_argument_list_p
24767 && !parser->in_type_id_in_expr_p
24768 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24769 /* However, a parameter-declaration of the form
24770 "float(f)" (which is a valid declaration of a
24771 parameter "f") can also be interpreted as an
24772 expression (the conversion of "f" to "float"). */
24773 && !parenthesized_p)
24774 cp_parser_commit_to_tentative_parse (parser);
24776 else
24778 cp_parser_error (parser, "expected %<,%> or %<...%>");
24779 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24780 cp_parser_skip_to_closing_parenthesis (parser,
24781 /*recovering=*/true,
24782 /*or_comma=*/false,
24783 /*consume_paren=*/false);
24784 break;
24788 parser->in_unbraced_linkage_specification_p
24789 = saved_in_unbraced_linkage_specification_p;
24791 /* Reset implicit_template_scope if we are about to leave the function
24792 parameter list that introduced it. Note that for out-of-line member
24793 definitions, there will be one or more class scopes before we get to
24794 the template parameter scope. */
24796 if (cp_binding_level *its = parser->implicit_template_scope)
24797 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
24799 while (maybe_its->kind == sk_class)
24800 maybe_its = maybe_its->level_chain;
24801 if (maybe_its == its)
24803 parser->implicit_template_parms = 0;
24804 parser->implicit_template_scope = 0;
24808 return parameters;
24811 /* Parse a parameter declaration.
24813 parameter-declaration:
24814 decl-specifier-seq ... [opt] declarator
24815 decl-specifier-seq declarator = assignment-expression
24816 decl-specifier-seq ... [opt] abstract-declarator [opt]
24817 decl-specifier-seq abstract-declarator [opt] = assignment-expression
24819 The parser flags FLAGS is used to control type-specifier parsing.
24821 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
24822 declares a template parameter. (In that case, a non-nested `>'
24823 token encountered during the parsing of the assignment-expression
24824 is not interpreted as a greater-than operator.)
24826 Returns a representation of the parameter, or NULL if an error
24827 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
24828 true iff the declarator is of the form "(p)". */
24830 static cp_parameter_declarator *
24831 cp_parser_parameter_declaration (cp_parser *parser,
24832 cp_parser_flags flags,
24833 bool template_parm_p,
24834 bool *parenthesized_p)
24836 int declares_class_or_enum;
24837 cp_decl_specifier_seq decl_specifiers;
24838 cp_declarator *declarator;
24839 tree default_argument;
24840 cp_token *token = NULL, *declarator_token_start = NULL;
24841 const char *saved_message;
24842 bool template_parameter_pack_p = false;
24844 /* In a template parameter, `>' is not an operator.
24846 [temp.param]
24848 When parsing a default template-argument for a non-type
24849 template-parameter, the first non-nested `>' is taken as the end
24850 of the template parameter-list rather than a greater-than
24851 operator. */
24853 /* Type definitions may not appear in parameter types. */
24854 saved_message = parser->type_definition_forbidden_message;
24855 parser->type_definition_forbidden_message
24856 = G_("types may not be defined in parameter types");
24858 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
24859 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
24860 (current_template_parms)) : 0);
24862 /* Parse the declaration-specifiers. */
24863 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24864 cp_parser_decl_specifier_seq (parser,
24865 flags,
24866 &decl_specifiers,
24867 &declares_class_or_enum);
24869 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
24870 type-constraint opt auto can be used as a decl-specifier of the
24871 decl-specifier-seq of a parameter-declaration of a function declaration
24872 or lambda-expression..." but we must not synthesize an implicit template
24873 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
24874 we want to synthesize only the first auto. */
24875 auto cleanup = make_temp_override
24876 (parser->auto_is_implicit_function_template_parm_p, false);
24878 /* Complain about missing 'typename' or other invalid type names. */
24879 if (!decl_specifiers.any_type_specifiers_p
24880 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24881 decl_specifiers.type = error_mark_node;
24883 /* If an error occurred, there's no reason to attempt to parse the
24884 rest of the declaration. */
24885 if (cp_parser_error_occurred (parser))
24887 parser->type_definition_forbidden_message = saved_message;
24888 return NULL;
24891 /* Peek at the next token. */
24892 token = cp_lexer_peek_token (parser->lexer);
24894 /* If the next token is a `)', `,', `=', `>', or `...', then there
24895 is no declarator. However, when variadic templates are enabled,
24896 there may be a declarator following `...'. */
24897 if (token->type == CPP_CLOSE_PAREN
24898 || token->type == CPP_COMMA
24899 || token->type == CPP_EQ
24900 || token->type == CPP_GREATER)
24902 declarator = NULL;
24903 if (parenthesized_p)
24904 *parenthesized_p = false;
24906 /* Otherwise, there should be a declarator. */
24907 else
24909 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
24910 parser->default_arg_ok_p = false;
24912 /* After seeing a decl-specifier-seq, if the next token is not a
24913 "(" or "{", there is no possibility that the code is a valid
24914 expression. Therefore, if parsing tentatively, we commit at
24915 this point. */
24916 if (!parser->in_template_argument_list_p
24917 /* In an expression context, having seen:
24919 (int((char ...
24921 we cannot be sure whether we are looking at a
24922 function-type (taking a "char" as a parameter) or a cast
24923 of some object of type "char" to "int". */
24924 && !parser->in_type_id_in_expr_p
24925 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24926 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
24928 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24930 if (decl_specifiers.type
24931 && template_placeholder_p (decl_specifiers.type))
24932 /* This is a CTAD expression, not a parameter declaration. */
24933 cp_parser_simulate_error (parser);
24935 else
24936 cp_parser_commit_to_tentative_parse (parser);
24938 /* Parse the declarator. */
24939 declarator_token_start = token;
24940 declarator = cp_parser_declarator (parser,
24941 CP_PARSER_DECLARATOR_EITHER,
24942 CP_PARSER_FLAGS_NONE,
24943 /*ctor_dtor_or_conv_p=*/NULL,
24944 parenthesized_p,
24945 /*member_p=*/false,
24946 /*friend_p=*/false,
24947 /*static_p=*/false);
24948 parser->default_arg_ok_p = saved_default_arg_ok_p;
24949 /* After the declarator, allow more attributes. */
24950 decl_specifiers.attributes
24951 = attr_chainon (decl_specifiers.attributes,
24952 cp_parser_attributes_opt (parser));
24954 /* If the declarator is a template parameter pack, remember that and
24955 clear the flag in the declarator itself so we don't get errors
24956 from grokdeclarator. */
24957 if (template_parm_p && declarator && declarator->parameter_pack_p)
24959 declarator->parameter_pack_p = false;
24960 template_parameter_pack_p = true;
24964 /* If the next token is an ellipsis, and we have not seen a declarator
24965 name, and if either the type of the declarator contains parameter
24966 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
24967 for, eg, abbreviated integral type names), then we actually have a
24968 parameter pack expansion expression. Otherwise, leave the ellipsis
24969 for a C-style variadic function. */
24970 token = cp_lexer_peek_token (parser->lexer);
24972 /* If a function parameter pack was specified and an implicit template
24973 parameter was introduced during cp_parser_parameter_declaration,
24974 change any implicit parameters introduced into packs. */
24975 if (parser->implicit_template_parms
24976 && ((token->type == CPP_ELLIPSIS
24977 && declarator_can_be_parameter_pack (declarator))
24978 || (declarator && declarator->parameter_pack_p)))
24980 int latest_template_parm_idx = TREE_VEC_LENGTH
24981 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
24983 if (latest_template_parm_idx != template_parm_idx)
24984 decl_specifiers.type = convert_generic_types_to_packs
24985 (decl_specifiers.type,
24986 template_parm_idx, latest_template_parm_idx);
24989 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24991 tree type = decl_specifiers.type;
24993 if (type && DECL_P (type))
24994 type = TREE_TYPE (type);
24996 if (((type
24997 && TREE_CODE (type) != TYPE_PACK_EXPANSION
24998 && (template_parm_p || uses_parameter_packs (type)))
24999 || (!type && template_parm_p))
25000 && declarator_can_be_parameter_pack (declarator))
25002 /* Consume the `...'. */
25003 cp_lexer_consume_token (parser->lexer);
25004 maybe_warn_variadic_templates ();
25006 /* Build a pack expansion type */
25007 if (template_parm_p)
25008 template_parameter_pack_p = true;
25009 else if (declarator)
25010 declarator->parameter_pack_p = true;
25011 else
25012 decl_specifiers.type = make_pack_expansion (type);
25016 /* The restriction on defining new types applies only to the type
25017 of the parameter, not to the default argument. */
25018 parser->type_definition_forbidden_message = saved_message;
25020 /* If the next token is `=', then process a default argument. */
25021 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25023 tree type = decl_specifiers.type;
25024 token = cp_lexer_peek_token (parser->lexer);
25025 if (declarator)
25026 declarator->init_loc = token->location;
25027 /* If we are defining a class, then the tokens that make up the
25028 default argument must be saved and processed later. */
25029 if (!template_parm_p && at_class_scope_p ()
25030 && TYPE_BEING_DEFINED (current_class_type)
25031 && !LAMBDA_TYPE_P (current_class_type))
25032 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25034 /* A constrained-type-specifier may declare a type
25035 template-parameter. */
25036 else if (declares_constrained_type_template_parameter (type))
25037 default_argument
25038 = cp_parser_default_type_template_argument (parser);
25040 /* A constrained-type-specifier may declare a
25041 template-template-parameter. */
25042 else if (declares_constrained_template_template_parameter (type))
25043 default_argument
25044 = cp_parser_default_template_template_argument (parser);
25046 /* Outside of a class definition, we can just parse the
25047 assignment-expression. */
25048 else
25049 default_argument
25050 = cp_parser_default_argument (parser, template_parm_p);
25052 if (!parser->default_arg_ok_p)
25054 permerror (token->location,
25055 "default arguments are only "
25056 "permitted for function parameters");
25058 else if ((declarator && declarator->parameter_pack_p)
25059 || template_parameter_pack_p
25060 || (decl_specifiers.type
25061 && PACK_EXPANSION_P (decl_specifiers.type)))
25063 /* Find the name of the parameter pack. */
25064 cp_declarator *id_declarator = declarator;
25065 while (id_declarator && id_declarator->kind != cdk_id)
25066 id_declarator = id_declarator->declarator;
25068 if (id_declarator && id_declarator->kind == cdk_id)
25069 error_at (declarator_token_start->location,
25070 template_parm_p
25071 ? G_("template parameter pack %qD "
25072 "cannot have a default argument")
25073 : G_("parameter pack %qD cannot have "
25074 "a default argument"),
25075 id_declarator->u.id.unqualified_name);
25076 else
25077 error_at (declarator_token_start->location,
25078 template_parm_p
25079 ? G_("template parameter pack cannot have "
25080 "a default argument")
25081 : G_("parameter pack cannot have a "
25082 "default argument"));
25084 default_argument = NULL_TREE;
25087 else
25088 default_argument = NULL_TREE;
25090 if (default_argument)
25091 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25093 /* Generate a location for the parameter, ranging from the start of the
25094 initial token to the end of the final token (using input_location for
25095 the latter, set up by cp_lexer_set_source_position_from_token when
25096 consuming tokens).
25098 If we have a identifier, then use it for the caret location, e.g.
25100 extern int callee (int one, int (*two)(int, int), float three);
25101 ~~~~~~^~~~~~~~~~~~~~
25103 otherwise, reuse the start location for the caret location e.g.:
25105 extern int callee (int one, int (*)(int, int), float three);
25106 ^~~~~~~~~~~~~~~~~
25109 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25110 ? declarator->id_loc
25111 : decl_spec_token_start->location);
25112 location_t param_loc = make_location (caret_loc,
25113 decl_spec_token_start->location,
25114 input_location);
25116 return make_parameter_declarator (&decl_specifiers,
25117 declarator,
25118 default_argument,
25119 param_loc,
25120 template_parameter_pack_p);
25123 /* Parse a default argument and return it.
25125 TEMPLATE_PARM_P is true if this is a default argument for a
25126 non-type template parameter. */
25127 static tree
25128 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25130 tree default_argument = NULL_TREE;
25131 bool saved_greater_than_is_operator_p;
25132 unsigned char saved_local_variables_forbidden_p;
25133 bool non_constant_p, is_direct_init;
25135 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25136 set correctly. */
25137 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25138 parser->greater_than_is_operator_p = !template_parm_p;
25139 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25140 auto ord = make_temp_override (parser->oacc_routine, NULL);
25141 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25143 /* Local variable names (and the `this' keyword) may not
25144 appear in a default argument. */
25145 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25146 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25147 /* Parse the assignment-expression. */
25148 if (template_parm_p)
25149 push_deferring_access_checks (dk_no_deferred);
25150 tree saved_class_ptr = NULL_TREE;
25151 tree saved_class_ref = NULL_TREE;
25152 /* The "this" pointer is not valid in a default argument. */
25153 if (cfun)
25155 saved_class_ptr = current_class_ptr;
25156 cp_function_chain->x_current_class_ptr = NULL_TREE;
25157 saved_class_ref = current_class_ref;
25158 cp_function_chain->x_current_class_ref = NULL_TREE;
25160 default_argument
25161 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
25162 /* Restore the "this" pointer. */
25163 if (cfun)
25165 cp_function_chain->x_current_class_ptr = saved_class_ptr;
25166 cp_function_chain->x_current_class_ref = saved_class_ref;
25168 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25169 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25170 if (template_parm_p)
25171 pop_deferring_access_checks ();
25172 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25173 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25175 return default_argument;
25178 /* Parse a function-body.
25180 function-body:
25181 compound_statement */
25183 static void
25184 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25186 cp_parser_compound_statement (parser, NULL, (in_function_try_block
25187 ? BCS_TRY_BLOCK : BCS_NORMAL),
25188 true);
25191 /* Parse a ctor-initializer-opt followed by a function-body. Return
25192 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25193 is true we are parsing a function-try-block. */
25195 static void
25196 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25197 bool in_function_try_block)
25199 tree body, list;
25200 const bool check_body_p
25201 = (DECL_CONSTRUCTOR_P (current_function_decl)
25202 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25203 tree last = NULL;
25205 if (in_function_try_block
25206 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25207 && cxx_dialect < cxx20)
25209 if (DECL_CONSTRUCTOR_P (current_function_decl))
25210 pedwarn (input_location, OPT_Wc__20_extensions,
25211 "function-try-block body of %<constexpr%> constructor only "
25212 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25213 else
25214 pedwarn (input_location, OPT_Wc__20_extensions,
25215 "function-try-block body of %<constexpr%> function only "
25216 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25219 /* Begin the function body. */
25220 body = begin_function_body ();
25221 /* Parse the optional ctor-initializer. */
25222 cp_parser_ctor_initializer_opt (parser);
25224 /* If we're parsing a constexpr constructor definition, we need
25225 to check that the constructor body is indeed empty. However,
25226 before we get to cp_parser_function_body lot of junk has been
25227 generated, so we can't just check that we have an empty block.
25228 Rather we take a snapshot of the outermost block, and check whether
25229 cp_parser_function_body changed its state. */
25230 if (check_body_p)
25232 list = cur_stmt_list;
25233 if (STATEMENT_LIST_TAIL (list))
25234 last = STATEMENT_LIST_TAIL (list)->stmt;
25236 /* Parse the function-body. */
25237 cp_parser_function_body (parser, in_function_try_block);
25238 if (check_body_p)
25239 check_constexpr_ctor_body (last, list, /*complain=*/true);
25240 /* Finish the function body. */
25241 finish_function_body (body);
25244 /* Parse an initializer.
25246 initializer:
25247 = initializer-clause
25248 ( expression-list )
25250 Returns an expression representing the initializer. If no
25251 initializer is present, NULL_TREE is returned.
25253 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25254 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25255 set to TRUE if there is no initializer present. If there is an
25256 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25257 is set to true; otherwise it is set to false. */
25259 static tree
25260 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
25261 bool* non_constant_p, bool subexpression_p)
25263 cp_token *token;
25264 tree init;
25266 /* Peek at the next token. */
25267 token = cp_lexer_peek_token (parser->lexer);
25269 /* Let our caller know whether or not this initializer was
25270 parenthesized. */
25271 *is_direct_init = (token->type != CPP_EQ);
25272 /* Assume that the initializer is constant. */
25273 *non_constant_p = false;
25275 if (token->type == CPP_EQ)
25277 /* Consume the `='. */
25278 cp_lexer_consume_token (parser->lexer);
25279 /* Parse the initializer-clause. */
25280 init = cp_parser_initializer_clause (parser, non_constant_p);
25282 else if (token->type == CPP_OPEN_PAREN)
25284 vec<tree, va_gc> *vec;
25285 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25286 /*cast_p=*/false,
25287 /*allow_expansion_p=*/true,
25288 non_constant_p);
25289 if (vec == NULL)
25290 return error_mark_node;
25291 init = build_tree_list_vec (vec);
25292 release_tree_vector (vec);
25294 else if (token->type == CPP_OPEN_BRACE)
25296 cp_lexer_set_source_position (parser->lexer);
25297 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25298 init = cp_parser_braced_list (parser, non_constant_p);
25299 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25301 else
25303 /* Anything else is an error. */
25304 cp_parser_error (parser, "expected initializer");
25305 init = error_mark_node;
25308 if (!subexpression_p && check_for_bare_parameter_packs (init))
25309 init = error_mark_node;
25311 return init;
25314 /* Parse an initializer-clause.
25316 initializer-clause:
25317 assignment-expression
25318 braced-init-list
25320 Returns an expression representing the initializer.
25322 If the `assignment-expression' production is used the value
25323 returned is simply a representation for the expression.
25325 Otherwise, calls cp_parser_braced_list. */
25327 static cp_expr
25328 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25330 cp_expr initializer;
25332 /* Assume the expression is constant. */
25333 *non_constant_p = false;
25335 /* If it is not a `{', then we are looking at an
25336 assignment-expression. */
25337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25339 initializer
25340 = cp_parser_constant_expression (parser,
25341 /*allow_non_constant_p=*/2,
25342 non_constant_p);
25344 else
25345 initializer = cp_parser_braced_list (parser, non_constant_p);
25347 return initializer;
25350 /* Parse a brace-enclosed initializer list.
25352 braced-init-list:
25353 { initializer-list , [opt] }
25354 { designated-initializer-list , [opt] }
25357 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25358 the elements of the initializer-list (or NULL, if the last
25359 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25360 NULL_TREE. There is no way to detect whether or not the optional
25361 trailing `,' was provided. NON_CONSTANT_P is as for
25362 cp_parser_initializer. */
25364 static cp_expr
25365 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
25367 tree initializer;
25368 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25370 /* Consume the `{' token. */
25371 matching_braces braces;
25372 braces.require_open (parser);
25373 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25374 initializer = make_node (CONSTRUCTOR);
25375 /* If it's not a `}', then there is a non-trivial initializer. */
25376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25378 bool designated;
25379 /* Parse the initializer list. */
25380 CONSTRUCTOR_ELTS (initializer)
25381 = cp_parser_initializer_list (parser, non_constant_p, &designated);
25382 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25383 /* A trailing `,' token is allowed. */
25384 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25385 cp_lexer_consume_token (parser->lexer);
25387 else
25388 *non_constant_p = false;
25389 /* Now, there should be a trailing `}'. */
25390 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25391 braces.require_close (parser);
25392 TREE_TYPE (initializer) = init_list_type_node;
25394 cp_expr result (initializer);
25395 /* Build a location of the form:
25396 { ... }
25397 ^~~~~~~
25398 with caret==start at the open brace, finish at the close brace. */
25399 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25400 result.set_location (combined_loc);
25401 return result;
25404 /* Consume tokens up to, and including, the next non-nested closing `]'.
25405 Returns true iff we found a closing `]'. */
25407 static bool
25408 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25410 unsigned square_depth = 0;
25412 while (true)
25414 cp_token * token = cp_lexer_peek_token (parser->lexer);
25416 switch (token->type)
25418 case CPP_PRAGMA_EOL:
25419 if (!parser->lexer->in_pragma)
25420 break;
25421 /* FALLTHRU */
25422 case CPP_EOF:
25423 /* If we've run out of tokens, then there is no closing `]'. */
25424 return false;
25426 case CPP_OPEN_SQUARE:
25427 ++square_depth;
25428 break;
25430 case CPP_CLOSE_SQUARE:
25431 if (!square_depth--)
25433 cp_lexer_consume_token (parser->lexer);
25434 return true;
25436 break;
25438 default:
25439 break;
25442 /* Consume the token. */
25443 cp_lexer_consume_token (parser->lexer);
25447 /* Return true if we are looking at an array-designator, false otherwise. */
25449 static bool
25450 cp_parser_array_designator_p (cp_parser *parser)
25452 /* Consume the `['. */
25453 cp_lexer_consume_token (parser->lexer);
25455 cp_lexer_save_tokens (parser->lexer);
25457 /* Skip tokens until the next token is a closing square bracket.
25458 If we find the closing `]', and the next token is a `=', then
25459 we are looking at an array designator. */
25460 bool array_designator_p
25461 = (cp_parser_skip_to_closing_square_bracket (parser)
25462 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25464 /* Roll back the tokens we skipped. */
25465 cp_lexer_rollback_tokens (parser->lexer);
25467 return array_designator_p;
25470 /* Parse an initializer-list.
25472 initializer-list:
25473 initializer-clause ... [opt]
25474 initializer-list , initializer-clause ... [opt]
25476 C++20 Extension:
25478 designated-initializer-list:
25479 designated-initializer-clause
25480 designated-initializer-list , designated-initializer-clause
25482 designated-initializer-clause:
25483 designator brace-or-equal-initializer
25485 designator:
25486 . identifier
25488 GNU Extension:
25490 initializer-list:
25491 designation initializer-clause ...[opt]
25492 initializer-list , designation initializer-clause ...[opt]
25494 designation:
25495 . identifier =
25496 identifier :
25497 [ constant-expression ] =
25499 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25500 for the initializer. If the INDEX of the elt is non-NULL, it is the
25501 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25502 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25503 are any designators. */
25505 static vec<constructor_elt, va_gc> *
25506 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25507 bool *designated)
25509 vec<constructor_elt, va_gc> *v = NULL;
25510 bool first_p = true;
25511 tree first_designator = NULL_TREE;
25513 /* Assume all of the expressions are constant. */
25514 *non_constant_p = false;
25516 unsigned nelts = 0;
25517 int suppress = suppress_location_wrappers;
25519 /* Parse the rest of the list. */
25520 while (true)
25522 cp_token *token;
25523 tree designator;
25524 tree initializer;
25525 bool clause_non_constant_p;
25526 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25528 /* Handle the C++20 syntax, '. id ='. */
25529 if ((cxx_dialect >= cxx20
25530 || cp_parser_allow_gnu_extensions_p (parser))
25531 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25532 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25533 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25534 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25535 == CPP_OPEN_BRACE)))
25537 if (pedantic && cxx_dialect < cxx20)
25538 pedwarn (loc, OPT_Wc__20_extensions,
25539 "C++ designated initializers only available with "
25540 "%<-std=c++20%> or %<-std=gnu++20%>");
25541 /* Consume the `.'. */
25542 cp_lexer_consume_token (parser->lexer);
25543 /* Consume the identifier. */
25544 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25545 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25546 /* Consume the `='. */
25547 cp_lexer_consume_token (parser->lexer);
25549 /* Also, if the next token is an identifier and the following one is a
25550 colon, we are looking at the GNU designated-initializer
25551 syntax. */
25552 else if (cp_parser_allow_gnu_extensions_p (parser)
25553 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25554 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25555 == CPP_COLON))
25557 /* Warn the user that they are using an extension. */
25558 pedwarn (loc, OPT_Wpedantic,
25559 "ISO C++ does not allow GNU designated initializers");
25560 /* Consume the identifier. */
25561 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25562 /* Consume the `:'. */
25563 cp_lexer_consume_token (parser->lexer);
25565 /* Also handle C99 array designators, '[ const ] ='. */
25566 else if (cp_parser_allow_gnu_extensions_p (parser)
25567 && !c_dialect_objc ()
25568 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25570 /* In C++11, [ could start a lambda-introducer. */
25571 bool non_const = false;
25573 cp_parser_parse_tentatively (parser);
25575 if (!cp_parser_array_designator_p (parser))
25577 cp_parser_simulate_error (parser);
25578 designator = NULL_TREE;
25580 else
25582 designator = cp_parser_constant_expression (parser, true,
25583 &non_const);
25584 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25585 cp_parser_require (parser, CPP_EQ, RT_EQ);
25588 if (!cp_parser_parse_definitely (parser))
25589 designator = NULL_TREE;
25590 else if (non_const
25591 && (!require_potential_rvalue_constant_expression
25592 (designator)))
25593 designator = NULL_TREE;
25594 if (designator)
25595 /* Warn the user that they are using an extension. */
25596 pedwarn (loc, OPT_Wpedantic,
25597 "ISO C++ does not allow C99 designated initializers");
25599 else
25600 designator = NULL_TREE;
25602 if (first_p)
25604 first_designator = designator;
25605 first_p = false;
25607 else if (cxx_dialect >= cxx20
25608 && first_designator != error_mark_node
25609 && (!first_designator != !designator))
25611 error_at (loc, "either all initializer clauses should be designated "
25612 "or none of them should be");
25613 first_designator = error_mark_node;
25615 else if (cxx_dialect < cxx20 && !first_designator)
25616 first_designator = designator;
25618 /* Parse the initializer. */
25619 initializer = cp_parser_initializer_clause (parser,
25620 &clause_non_constant_p);
25621 /* If any clause is non-constant, so is the entire initializer. */
25622 if (clause_non_constant_p)
25623 *non_constant_p = true;
25625 /* If we have an ellipsis, this is an initializer pack
25626 expansion. */
25627 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25629 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25631 /* Consume the `...'. */
25632 cp_lexer_consume_token (parser->lexer);
25634 if (designator && cxx_dialect >= cxx20)
25635 error_at (loc,
25636 "%<...%> not allowed in designated initializer list");
25638 /* Turn the initializer into an initializer expansion. */
25639 initializer = make_pack_expansion (initializer);
25642 /* Add it to the vector. */
25643 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
25645 /* If the next token is not a comma, we have reached the end of
25646 the list. */
25647 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25648 break;
25650 /* Peek at the next token. */
25651 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25652 /* If the next token is a `}', then we're still done. An
25653 initializer-clause can have a trailing `,' after the
25654 initializer-list and before the closing `}'. */
25655 if (token->type == CPP_CLOSE_BRACE)
25656 break;
25658 /* Suppress location wrappers in a long initializer to save memory
25659 (14179). The cutoff is chosen arbitrarily. */
25660 const unsigned loc_max = 256;
25661 unsigned incr = 1;
25662 if (TREE_CODE (initializer) == CONSTRUCTOR)
25663 /* Look one level down because it's easy. Looking deeper would require
25664 passing down a nelts pointer, and I don't think multi-level massive
25665 initializers are common enough to justify this. */
25666 incr = CONSTRUCTOR_NELTS (initializer);
25667 nelts += incr;
25668 if (nelts >= loc_max && (nelts - incr) < loc_max)
25669 ++suppress_location_wrappers;
25671 /* Consume the `,' token. */
25672 cp_lexer_consume_token (parser->lexer);
25675 /* The same identifier shall not appear in multiple designators
25676 of a designated-initializer-list. */
25677 if (first_designator)
25679 unsigned int i;
25680 tree designator, val;
25681 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25682 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25684 if (IDENTIFIER_MARKED (designator))
25686 error_at (cp_expr_loc_or_input_loc (val),
25687 "%<.%s%> designator used multiple times in "
25688 "the same initializer list",
25689 IDENTIFIER_POINTER (designator));
25690 (*v)[i].index = error_mark_node;
25692 else
25693 IDENTIFIER_MARKED (designator) = 1;
25695 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25696 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25697 IDENTIFIER_MARKED (designator) = 0;
25700 suppress_location_wrappers = suppress;
25702 *designated = first_designator != NULL_TREE;
25703 return v;
25706 /* Classes [gram.class] */
25708 /* Parse a class-name.
25710 class-name:
25711 identifier
25712 template-id
25714 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25715 to indicate that names looked up in dependent types should be
25716 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
25717 keyword has been used to indicate that the name that appears next
25718 is a template. TAG_TYPE indicates the explicit tag given before
25719 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
25720 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
25721 is the class being defined in a class-head. If ENUM_OK is TRUE,
25722 enum-names are also accepted.
25724 Returns the TYPE_DECL representing the class. */
25726 static tree
25727 cp_parser_class_name (cp_parser *parser,
25728 bool typename_keyword_p,
25729 bool template_keyword_p,
25730 enum tag_types tag_type,
25731 bool check_dependency_p,
25732 bool class_head_p,
25733 bool is_declaration,
25734 bool enum_ok)
25736 tree decl;
25737 tree identifier = NULL_TREE;
25739 /* All class-names start with an identifier. */
25740 cp_token *token = cp_lexer_peek_token (parser->lexer);
25741 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
25743 cp_parser_error (parser, "expected class-name");
25744 return error_mark_node;
25747 /* PARSER->SCOPE can be cleared when parsing the template-arguments
25748 to a template-id, so we save it here. Consider object scope too,
25749 so that make_typename_type below can use it (cp_parser_template_name
25750 considers object scope also). This may happen with code like
25752 p->template A<T>::a()
25754 where we first want to look up A<T>::a in the class of the object
25755 expression, as per [basic.lookup.classref]. */
25756 tree scope = parser->scope ? parser->scope : parser->context->object_type;
25757 /* This only checks parser->scope to avoid duplicate errors; if
25758 ->object_type is erroneous, go on to give a parse error. */
25759 if (parser->scope == error_mark_node)
25760 return error_mark_node;
25762 /* Any name names a type if we're following the `typename' keyword
25763 in a qualified name where the enclosing scope is type-dependent. */
25764 const bool typename_p = (typename_keyword_p
25765 && parser->scope
25766 && TYPE_P (parser->scope)
25767 && dependent_scope_p (parser->scope));
25768 /* Handle the common case (an identifier, but not a template-id)
25769 efficiently. */
25770 if (token->type == CPP_NAME
25771 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
25773 cp_token *identifier_token;
25774 bool ambiguous_p;
25776 /* Look for the identifier. */
25777 identifier_token = cp_lexer_peek_token (parser->lexer);
25778 ambiguous_p = identifier_token->error_reported;
25779 identifier = cp_parser_identifier (parser);
25780 /* If the next token isn't an identifier, we are certainly not
25781 looking at a class-name. */
25782 if (identifier == error_mark_node)
25783 decl = error_mark_node;
25784 /* If we know this is a type-name, there's no need to look it
25785 up. */
25786 else if (typename_p)
25787 decl = identifier;
25788 else
25790 tree ambiguous_decls;
25791 /* If we already know that this lookup is ambiguous, then
25792 we've already issued an error message; there's no reason
25793 to check again. */
25794 if (ambiguous_p)
25796 cp_parser_simulate_error (parser);
25797 return error_mark_node;
25799 /* If the next token is a `::', then the name must be a type
25800 name.
25802 [basic.lookup.qual]
25804 During the lookup for a name preceding the :: scope
25805 resolution operator, object, function, and enumerator
25806 names are ignored. */
25807 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25808 tag_type = scope_type;
25809 /* Look up the name. */
25810 decl = cp_parser_lookup_name (parser, identifier,
25811 tag_type,
25812 /*is_template=*/false,
25813 /*is_namespace=*/false,
25814 check_dependency_p,
25815 &ambiguous_decls,
25816 identifier_token->location);
25817 if (ambiguous_decls)
25819 if (cp_parser_parsing_tentatively (parser))
25820 cp_parser_simulate_error (parser);
25821 return error_mark_node;
25825 else
25827 /* Try a template-id. */
25828 decl = cp_parser_template_id (parser, template_keyword_p,
25829 check_dependency_p,
25830 tag_type,
25831 is_declaration);
25832 if (decl == error_mark_node)
25833 return error_mark_node;
25836 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
25838 /* If this is a typename, create a TYPENAME_TYPE. */
25839 if (typename_p && decl != error_mark_node)
25841 decl = make_typename_type (scope, decl, typename_type,
25842 /*complain=*/tf_error);
25843 if (decl != error_mark_node)
25844 decl = TYPE_NAME (decl);
25847 decl = strip_using_decl (decl);
25849 /* Check to see that it is really the name of a class. */
25850 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
25851 && identifier_p (TREE_OPERAND (decl, 0))
25852 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25853 /* Situations like this:
25855 template <typename T> struct A {
25856 typename T::template X<int>::I i;
25859 are problematic. Is `T::template X<int>' a class-name? The
25860 standard does not seem to be definitive, but there is no other
25861 valid interpretation of the following `::'. Therefore, those
25862 names are considered class-names. */
25864 decl = make_typename_type (scope, decl, tag_type, tf_error);
25865 if (decl != error_mark_node)
25866 decl = TYPE_NAME (decl);
25868 else if (TREE_CODE (decl) != TYPE_DECL
25869 || TREE_TYPE (decl) == error_mark_node
25870 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
25871 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
25872 /* In Objective-C 2.0, a classname followed by '.' starts a
25873 dot-syntax expression, and it's not a type-name. */
25874 || (c_dialect_objc ()
25875 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
25876 && objc_is_class_name (decl)))
25877 decl = error_mark_node;
25879 if (decl == error_mark_node)
25880 cp_parser_error (parser, "expected class-name");
25881 else if (identifier && !parser->scope)
25882 maybe_note_name_used_in_class (identifier, decl);
25884 return decl;
25887 /* Make sure that any member-function parameters are in scope.
25888 For instance, a function's noexcept-specifier can use the function's
25889 parameters:
25891 struct S {
25892 void fn (int p) noexcept(noexcept(p));
25895 so we need to make sure name lookup can find them. This is used
25896 when we delay parsing of the noexcept-specifier. */
25898 static void
25899 inject_parm_decls (tree decl)
25901 begin_scope (sk_function_parms, decl);
25902 tree args = DECL_ARGUMENTS (decl);
25904 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
25906 if (args && is_this_parameter (args))
25908 gcc_checking_assert (current_class_ptr == NULL_TREE);
25909 current_class_ref = cp_build_fold_indirect_ref (args);
25910 current_class_ptr = args;
25914 /* Undo the effects of inject_parm_decls. */
25916 static void
25917 pop_injected_parms (void)
25919 pop_bindings_and_leave_scope ();
25920 current_class_ptr = current_class_ref = NULL_TREE;
25923 /* Parse a class-specifier.
25925 class-specifier:
25926 class-head { member-specification [opt] }
25928 Returns the TREE_TYPE representing the class. */
25930 static tree
25931 cp_parser_class_specifier_1 (cp_parser* parser)
25933 tree type;
25934 tree attributes = NULL_TREE;
25935 bool nested_name_specifier_p;
25936 unsigned saved_num_template_parameter_lists;
25937 bool saved_in_function_body;
25938 unsigned char in_statement;
25939 bool in_switch_statement_p;
25940 bool saved_in_unbraced_linkage_specification_p;
25941 tree old_scope = NULL_TREE;
25942 tree scope = NULL_TREE;
25943 cp_token *closing_brace;
25945 push_deferring_access_checks (dk_no_deferred);
25947 /* Parse the class-head. */
25948 type = cp_parser_class_head (parser,
25949 &nested_name_specifier_p);
25950 /* If the class-head was a semantic disaster, skip the entire body
25951 of the class. */
25952 if (!type)
25954 cp_parser_skip_to_end_of_block_or_statement (parser);
25955 pop_deferring_access_checks ();
25956 return error_mark_node;
25959 /* Look for the `{'. */
25960 matching_braces braces;
25961 if (!braces.require_open (parser))
25963 pop_deferring_access_checks ();
25964 return error_mark_node;
25967 cp_ensure_no_omp_declare_simd (parser);
25968 cp_ensure_no_oacc_routine (parser);
25970 /* Issue an error message if type-definitions are forbidden here. */
25971 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
25972 /* Remember that we are defining one more class. */
25973 ++parser->num_classes_being_defined;
25974 /* Inside the class, surrounding template-parameter-lists do not
25975 apply. */
25976 saved_num_template_parameter_lists
25977 = parser->num_template_parameter_lists;
25978 parser->num_template_parameter_lists = 0;
25979 /* We are not in a function body. */
25980 saved_in_function_body = parser->in_function_body;
25981 parser->in_function_body = false;
25982 /* Or in a loop. */
25983 in_statement = parser->in_statement;
25984 parser->in_statement = 0;
25985 /* Or in a switch. */
25986 in_switch_statement_p = parser->in_switch_statement_p;
25987 parser->in_switch_statement_p = false;
25988 /* We are not immediately inside an extern "lang" block. */
25989 saved_in_unbraced_linkage_specification_p
25990 = parser->in_unbraced_linkage_specification_p;
25991 parser->in_unbraced_linkage_specification_p = false;
25993 /* Start the class. */
25994 if (nested_name_specifier_p)
25996 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
25997 /* SCOPE must be a scope nested inside current scope. */
25998 if (is_nested_namespace (current_namespace,
25999 decl_namespace_context (scope)))
26000 old_scope = push_inner_scope (scope);
26001 else
26002 nested_name_specifier_p = false;
26004 type = begin_class_definition (type);
26006 if (type == error_mark_node)
26007 /* If the type is erroneous, skip the entire body of the class. */
26008 cp_parser_skip_to_closing_brace (parser);
26009 else
26010 /* Parse the member-specification. */
26011 cp_parser_member_specification_opt (parser);
26013 /* Look for the trailing `}'. */
26014 closing_brace = braces.require_close (parser);
26015 /* Look for trailing attributes to apply to this class. */
26016 if (cp_parser_allow_gnu_extensions_p (parser))
26017 attributes = cp_parser_gnu_attributes_opt (parser);
26018 if (type != error_mark_node)
26019 type = finish_struct (type, attributes);
26020 if (nested_name_specifier_p)
26021 pop_inner_scope (old_scope, scope);
26023 /* We've finished a type definition. Check for the common syntax
26024 error of forgetting a semicolon after the definition. We need to
26025 be careful, as we can't just check for not-a-semicolon and be done
26026 with it; the user might have typed:
26028 class X { } c = ...;
26029 class X { } *p = ...;
26031 and so forth. Instead, enumerate all the possible tokens that
26032 might follow this production; if we don't see one of them, then
26033 complain and silently insert the semicolon. */
26035 cp_token *token = cp_lexer_peek_token (parser->lexer);
26036 bool want_semicolon = true;
26038 if (cp_next_tokens_can_be_std_attribute_p (parser))
26039 /* Don't try to parse c++11 attributes here. As per the
26040 grammar, that should be a task for
26041 cp_parser_decl_specifier_seq. */
26042 want_semicolon = false;
26044 switch (token->type)
26046 case CPP_NAME:
26047 case CPP_SEMICOLON:
26048 case CPP_MULT:
26049 case CPP_AND:
26050 case CPP_OPEN_PAREN:
26051 case CPP_CLOSE_PAREN:
26052 case CPP_COMMA:
26053 case CPP_SCOPE:
26054 want_semicolon = false;
26055 break;
26057 /* While it's legal for type qualifiers and storage class
26058 specifiers to follow type definitions in the grammar, only
26059 compiler testsuites contain code like that. Assume that if
26060 we see such code, then what we're really seeing is a case
26061 like:
26063 class X { }
26064 const <type> var = ...;
26068 class Y { }
26069 static <type> func (...) ...
26071 i.e. the qualifier or specifier applies to the next
26072 declaration. To do so, however, we need to look ahead one
26073 more token to see if *that* token is a type specifier.
26075 This code could be improved to handle:
26077 class Z { }
26078 static const <type> var = ...; */
26079 case CPP_KEYWORD:
26080 if (keyword_is_decl_specifier (token->keyword))
26082 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26084 /* Handling user-defined types here would be nice, but very
26085 tricky. */
26086 want_semicolon
26087 = (lookahead->type == CPP_KEYWORD
26088 && keyword_begins_type_specifier (lookahead->keyword));
26090 break;
26091 default:
26092 break;
26095 /* If we don't have a type, then something is very wrong and we
26096 shouldn't try to do anything clever. Likewise for not seeing the
26097 closing brace. */
26098 if (closing_brace && TYPE_P (type) && want_semicolon)
26100 /* Locate the closing brace. */
26101 cp_token_position prev
26102 = cp_lexer_previous_token_position (parser->lexer);
26103 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
26104 location_t loc = prev_token->location;
26106 /* We want to suggest insertion of a ';' immediately *after* the
26107 closing brace, so, if we can, offset the location by 1 column. */
26108 location_t next_loc = loc;
26109 if (!linemap_location_from_macro_expansion_p (line_table, loc))
26110 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26112 rich_location richloc (line_table, next_loc);
26114 /* If we successfully offset the location, suggest the fix-it. */
26115 if (next_loc != loc)
26116 richloc.add_fixit_insert_before (next_loc, ";");
26118 if (CLASSTYPE_DECLARED_CLASS (type))
26119 error_at (&richloc,
26120 "expected %<;%> after class definition");
26121 else if (TREE_CODE (type) == RECORD_TYPE)
26122 error_at (&richloc,
26123 "expected %<;%> after struct definition");
26124 else if (TREE_CODE (type) == UNION_TYPE)
26125 error_at (&richloc,
26126 "expected %<;%> after union definition");
26127 else
26128 gcc_unreachable ();
26130 /* Unget one token and smash it to look as though we encountered
26131 a semicolon in the input stream. */
26132 cp_lexer_set_token_position (parser->lexer, prev);
26133 token = cp_lexer_peek_token (parser->lexer);
26134 token->type = CPP_SEMICOLON;
26135 token->keyword = RID_MAX;
26139 /* If this class is not itself within the scope of another class,
26140 then we need to parse the bodies of all of the queued function
26141 definitions. Note that the queued functions defined in a class
26142 are not always processed immediately following the
26143 class-specifier for that class. Consider:
26145 struct A {
26146 struct B { void f() { sizeof (A); } };
26149 If `f' were processed before the processing of `A' were
26150 completed, there would be no way to compute the size of `A'.
26151 Note that the nesting we are interested in here is lexical --
26152 not the semantic nesting given by TYPE_CONTEXT. In particular,
26153 for:
26155 struct A { struct B; };
26156 struct A::B { void f() { } };
26158 there is no need to delay the parsing of `A::B::f'. */
26159 if (--parser->num_classes_being_defined == 0)
26161 tree decl;
26162 tree class_type = NULL_TREE;
26163 tree pushed_scope = NULL_TREE;
26164 unsigned ix;
26165 cp_default_arg_entry *e;
26167 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26169 /* Skip default arguments, NSDMIs, etc, in order to improve
26170 error recovery (c++/71169, c++/71832). */
26171 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26172 vec_safe_truncate (unparsed_nsdmis, 0);
26173 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26176 /* In a first pass, parse default arguments to the functions.
26177 Then, in a second pass, parse the bodies of the functions.
26178 This two-phased approach handles cases like:
26180 struct S {
26181 void f() { g(); }
26182 void g(int i = 3);
26186 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26188 decl = e->decl;
26189 /* If there are default arguments that have not yet been processed,
26190 take care of them now. */
26191 if (class_type != e->class_type)
26193 if (pushed_scope)
26194 pop_scope (pushed_scope);
26195 class_type = e->class_type;
26196 pushed_scope = push_scope (class_type);
26198 /* Make sure that any template parameters are in scope. */
26199 maybe_begin_member_template_processing (decl);
26200 /* Parse the default argument expressions. */
26201 cp_parser_late_parsing_default_args (parser, decl);
26202 /* Remove any template parameters from the symbol table. */
26203 maybe_end_member_template_processing ();
26205 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26207 /* If there are noexcept-specifiers that have not yet been processed,
26208 take care of them now. Do this before processing NSDMIs as they
26209 may depend on noexcept-specifiers already having been processed. */
26210 tree save_ccp = current_class_ptr;
26211 tree save_ccr = current_class_ref;
26212 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26214 tree ctx = DECL_CONTEXT (decl);
26215 if (class_type != ctx)
26217 if (pushed_scope)
26218 pop_scope (pushed_scope);
26219 class_type = ctx;
26220 pushed_scope = push_scope (class_type);
26223 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26224 def_parse = TREE_PURPOSE (def_parse);
26226 /* Make sure that any template parameters are in scope. */
26227 maybe_begin_member_template_processing (decl);
26229 /* Make sure that any member-function parameters are in scope.
26230 This function doesn't expect ccp to be set. */
26231 current_class_ptr = current_class_ref = NULL_TREE;
26232 inject_parm_decls (decl);
26234 /* 'this' is not allowed in static member functions. */
26235 unsigned char local_variables_forbidden_p
26236 = parser->local_variables_forbidden_p;
26237 if (DECL_THIS_STATIC (decl))
26238 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26240 /* Now we can parse the noexcept-specifier. */
26241 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26243 if (spec == error_mark_node)
26244 spec = NULL_TREE;
26246 /* Update the fn's type directly -- it might have escaped
26247 beyond this decl :( */
26248 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26249 /* Update any instantiations we've already created. We must
26250 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26251 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26252 in the pattern. */
26253 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26254 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26255 = spec ? TREE_PURPOSE (spec) : error_mark_node;
26257 /* Restore the state of local_variables_forbidden_p. */
26258 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26260 /* The finish_struct call above performed various override checking,
26261 but it skipped unparsed noexcept-specifier operands. Now that we
26262 have resolved them, check again. */
26263 noexcept_override_late_checks (type, decl);
26265 /* Remove any member-function parameters from the symbol table. */
26266 pop_injected_parms ();
26268 /* Remove any template parameters from the symbol table. */
26269 maybe_end_member_template_processing ();
26271 vec_safe_truncate (unparsed_noexcepts, 0);
26273 /* Now parse any NSDMIs. */
26274 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26276 if (class_type != DECL_CONTEXT (decl))
26278 if (pushed_scope)
26279 pop_scope (pushed_scope);
26280 class_type = DECL_CONTEXT (decl);
26281 pushed_scope = push_scope (class_type);
26283 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26284 cp_parser_late_parsing_nsdmi (parser, decl);
26286 vec_safe_truncate (unparsed_nsdmis, 0);
26287 current_class_ptr = save_ccp;
26288 current_class_ref = save_ccr;
26289 if (pushed_scope)
26290 pop_scope (pushed_scope);
26292 /* Now parse the body of the functions. */
26293 if (flag_openmp)
26295 /* OpenMP UDRs need to be parsed before all other functions. */
26296 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26297 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26298 cp_parser_late_parsing_for_member (parser, decl);
26299 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26300 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26301 cp_parser_late_parsing_for_member (parser, decl);
26303 else
26304 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26305 cp_parser_late_parsing_for_member (parser, decl);
26306 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26309 /* Put back any saved access checks. */
26310 pop_deferring_access_checks ();
26312 /* Restore saved state. */
26313 parser->in_switch_statement_p = in_switch_statement_p;
26314 parser->in_statement = in_statement;
26315 parser->in_function_body = saved_in_function_body;
26316 parser->num_template_parameter_lists
26317 = saved_num_template_parameter_lists;
26318 parser->in_unbraced_linkage_specification_p
26319 = saved_in_unbraced_linkage_specification_p;
26321 return type;
26324 static tree
26325 cp_parser_class_specifier (cp_parser* parser)
26327 tree ret;
26328 timevar_push (TV_PARSE_STRUCT);
26329 ret = cp_parser_class_specifier_1 (parser);
26330 timevar_pop (TV_PARSE_STRUCT);
26331 return ret;
26334 /* Parse a class-head.
26336 class-head:
26337 class-key identifier [opt] base-clause [opt]
26338 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26339 class-key nested-name-specifier [opt] template-id
26340 base-clause [opt]
26342 class-virt-specifier:
26343 final
26345 GNU Extensions:
26346 class-key attributes identifier [opt] base-clause [opt]
26347 class-key attributes nested-name-specifier identifier base-clause [opt]
26348 class-key attributes nested-name-specifier [opt] template-id
26349 base-clause [opt]
26351 Upon return BASES is initialized to the list of base classes (or
26352 NULL, if there are none) in the same form returned by
26353 cp_parser_base_clause.
26355 Returns the TYPE of the indicated class. Sets
26356 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26357 involving a nested-name-specifier was used, and FALSE otherwise.
26359 Returns error_mark_node if this is not a class-head.
26361 Returns NULL_TREE if the class-head is syntactically valid, but
26362 semantically invalid in a way that means we should skip the entire
26363 body of the class. */
26365 static tree
26366 cp_parser_class_head (cp_parser* parser,
26367 bool* nested_name_specifier_p)
26369 tree nested_name_specifier;
26370 enum tag_types class_key;
26371 tree id = NULL_TREE;
26372 tree type = NULL_TREE;
26373 tree attributes;
26374 tree bases;
26375 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26376 bool template_id_p = false;
26377 bool qualified_p = false;
26378 bool invalid_nested_name_p = false;
26379 bool invalid_explicit_specialization_p = false;
26380 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26381 tree pushed_scope = NULL_TREE;
26382 unsigned num_templates;
26383 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26384 /* Assume no nested-name-specifier will be present. */
26385 *nested_name_specifier_p = false;
26386 /* Assume no template parameter lists will be used in defining the
26387 type. */
26388 num_templates = 0;
26389 parser->colon_corrects_to_scope_p = false;
26391 /* Look for the class-key. */
26392 class_key = cp_parser_class_key (parser);
26393 if (class_key == none_type)
26394 return error_mark_node;
26396 location_t class_head_start_location = input_location;
26398 /* Parse the attributes. */
26399 attributes = cp_parser_attributes_opt (parser);
26401 /* If the next token is `::', that is invalid -- but sometimes
26402 people do try to write:
26404 struct ::S {};
26406 Handle this gracefully by accepting the extra qualifier, and then
26407 issuing an error about it later if this really is a
26408 class-head. If it turns out just to be an elaborated type
26409 specifier, remain silent. */
26410 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26411 qualified_p = true;
26413 push_deferring_access_checks (dk_no_check);
26415 /* Determine the name of the class. Begin by looking for an
26416 optional nested-name-specifier. */
26417 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26418 nested_name_specifier
26419 = cp_parser_nested_name_specifier_opt (parser,
26420 /*typename_keyword_p=*/false,
26421 /*check_dependency_p=*/false,
26422 /*type_p=*/true,
26423 /*is_declaration=*/false);
26424 /* If there was a nested-name-specifier, then there *must* be an
26425 identifier. */
26427 cp_token *bad_template_keyword = NULL;
26429 if (nested_name_specifier)
26431 type_start_token = cp_lexer_peek_token (parser->lexer);
26432 /* Although the grammar says `identifier', it really means
26433 `class-name' or `template-name'. You are only allowed to
26434 define a class that has already been declared with this
26435 syntax.
26437 The proposed resolution for Core Issue 180 says that wherever
26438 you see `class T::X' you should treat `X' as a type-name.
26440 It is OK to define an inaccessible class; for example:
26442 class A { class B; };
26443 class A::B {};
26445 We do not know if we will see a class-name, or a
26446 template-name. We look for a class-name first, in case the
26447 class-name is a template-id; if we looked for the
26448 template-name first we would stop after the template-name. */
26449 cp_parser_parse_tentatively (parser);
26450 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26451 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26452 type = cp_parser_class_name (parser,
26453 /*typename_keyword_p=*/false,
26454 /*template_keyword_p=*/false,
26455 class_type,
26456 /*check_dependency_p=*/false,
26457 /*class_head_p=*/true,
26458 /*is_declaration=*/false);
26459 /* If that didn't work, ignore the nested-name-specifier. */
26460 if (!cp_parser_parse_definitely (parser))
26462 invalid_nested_name_p = true;
26463 type_start_token = cp_lexer_peek_token (parser->lexer);
26464 id = cp_parser_identifier (parser);
26465 if (id == error_mark_node)
26466 id = NULL_TREE;
26468 /* If we could not find a corresponding TYPE, treat this
26469 declaration like an unqualified declaration. */
26470 if (type == error_mark_node)
26471 nested_name_specifier = NULL_TREE;
26472 /* Otherwise, count the number of templates used in TYPE and its
26473 containing scopes. */
26474 else
26475 num_templates = num_template_headers_for_class (TREE_TYPE (type));
26477 /* Otherwise, the identifier is optional. */
26478 else
26480 /* We don't know whether what comes next is a template-id,
26481 an identifier, or nothing at all. */
26482 cp_parser_parse_tentatively (parser);
26483 /* Check for a template-id. */
26484 type_start_token = cp_lexer_peek_token (parser->lexer);
26485 id = cp_parser_template_id (parser,
26486 /*template_keyword_p=*/false,
26487 /*check_dependency_p=*/true,
26488 class_key,
26489 /*is_declaration=*/true);
26490 /* If that didn't work, it could still be an identifier. */
26491 if (!cp_parser_parse_definitely (parser))
26493 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26495 type_start_token = cp_lexer_peek_token (parser->lexer);
26496 id = cp_parser_identifier (parser);
26498 else
26499 id = NULL_TREE;
26501 else
26503 template_id_p = true;
26504 ++num_templates;
26508 pop_deferring_access_checks ();
26510 if (id)
26512 cp_parser_check_for_invalid_template_id (parser, id,
26513 class_key,
26514 type_start_token->location);
26516 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
26518 /* If it's not a `:' or a `{' then we can't really be looking at a
26519 class-head, since a class-head only appears as part of a
26520 class-specifier. We have to detect this situation before calling
26521 xref_tag, since that has irreversible side-effects. */
26522 if (!cp_parser_next_token_starts_class_definition_p (parser))
26524 cp_parser_error (parser, "expected %<{%> or %<:%>");
26525 type = error_mark_node;
26526 goto out;
26529 /* At this point, we're going ahead with the class-specifier, even
26530 if some other problem occurs. */
26531 cp_parser_commit_to_tentative_parse (parser);
26532 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
26534 cp_parser_error (parser,
26535 "cannot specify %<override%> for a class");
26536 type = error_mark_node;
26537 goto out;
26539 /* Issue the error about the overly-qualified name now. */
26540 if (qualified_p)
26542 cp_parser_error (parser,
26543 "global qualification of class name is invalid");
26544 type = error_mark_node;
26545 goto out;
26547 else if (invalid_nested_name_p)
26549 cp_parser_error (parser,
26550 "qualified name does not name a class");
26551 type = error_mark_node;
26552 goto out;
26554 else if (nested_name_specifier)
26556 tree scope;
26558 if (bad_template_keyword)
26559 /* [temp.names]: in a qualified-id formed by a class-head-name, the
26560 keyword template shall not appear at the top level. */
26561 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
26562 "keyword %<template%> not allowed in class-head-name");
26564 /* Reject typedef-names in class heads. */
26565 if (!DECL_IMPLICIT_TYPEDEF_P (type))
26567 error_at (type_start_token->location,
26568 "invalid class name in declaration of %qD",
26569 type);
26570 type = NULL_TREE;
26571 goto done;
26574 /* Figure out in what scope the declaration is being placed. */
26575 scope = current_scope ();
26576 /* If that scope does not contain the scope in which the
26577 class was originally declared, the program is invalid. */
26578 if (scope && !is_ancestor (scope, nested_name_specifier))
26580 if (at_namespace_scope_p ())
26581 error_at (type_start_token->location,
26582 "declaration of %qD in namespace %qD which does not "
26583 "enclose %qD",
26584 type, scope, nested_name_specifier);
26585 else
26586 error_at (type_start_token->location,
26587 "declaration of %qD in %qD which does not enclose %qD",
26588 type, scope, nested_name_specifier);
26589 type = NULL_TREE;
26590 goto done;
26592 /* [dcl.meaning]
26594 A declarator-id shall not be qualified except for the
26595 definition of a ... nested class outside of its class
26596 ... [or] the definition or explicit instantiation of a
26597 class member of a namespace outside of its namespace. */
26598 if (scope == nested_name_specifier)
26599 permerror (nested_name_specifier_token_start->location,
26600 "extra qualification not allowed");
26602 /* An explicit-specialization must be preceded by "template <>". If
26603 it is not, try to recover gracefully. */
26604 if (at_namespace_scope_p ()
26605 && parser->num_template_parameter_lists == 0
26606 && !processing_template_parmlist
26607 && template_id_p)
26609 /* Build a location of this form:
26610 struct typename <ARGS>
26611 ^~~~~~~~~~~~~~~~~~~~~~
26612 with caret==start at the start token, and
26613 finishing at the end of the type. */
26614 location_t reported_loc
26615 = make_location (class_head_start_location,
26616 class_head_start_location,
26617 get_finish (type_start_token->location));
26618 rich_location richloc (line_table, reported_loc);
26619 richloc.add_fixit_insert_before (class_head_start_location,
26620 "template <> ");
26621 error_at (&richloc,
26622 "an explicit specialization must be preceded by"
26623 " %<template <>%>");
26624 invalid_explicit_specialization_p = true;
26625 /* Take the same action that would have been taken by
26626 cp_parser_explicit_specialization. */
26627 ++parser->num_template_parameter_lists;
26628 begin_specialization ();
26630 /* There must be no "return" statements between this point and the
26631 end of this function; set "type "to the correct return value and
26632 use "goto done;" to return. */
26633 /* Make sure that the right number of template parameters were
26634 present. */
26635 if (!cp_parser_check_template_parameters (parser, num_templates,
26636 template_id_p,
26637 type_start_token->location,
26638 /*declarator=*/NULL))
26640 /* If something went wrong, there is no point in even trying to
26641 process the class-definition. */
26642 type = NULL_TREE;
26643 goto done;
26646 /* Look up the type. */
26647 if (template_id_p)
26649 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
26650 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
26651 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
26653 error_at (type_start_token->location,
26654 "function template %qD redeclared as a class template", id);
26655 type = error_mark_node;
26657 else
26659 type = TREE_TYPE (id);
26660 type = maybe_process_partial_specialization (type);
26662 /* Check the scope while we still know whether or not we had a
26663 nested-name-specifier. */
26664 if (type != error_mark_node)
26665 check_unqualified_spec_or_inst (type, type_start_token->location);
26667 if (nested_name_specifier)
26668 pushed_scope = push_scope (nested_name_specifier);
26670 else if (nested_name_specifier)
26672 type = TREE_TYPE (type);
26674 /* Given:
26676 template <typename T> struct S { struct T };
26677 template <typename T> struct S<T>::T { };
26679 we will get a TYPENAME_TYPE when processing the definition of
26680 `S::T'. We need to resolve it to the actual type before we
26681 try to define it. */
26682 if (TREE_CODE (type) == TYPENAME_TYPE)
26684 type = resolve_typename_type (type, /*only_current_p=*/false);
26685 if (TREE_CODE (type) == TYPENAME_TYPE)
26687 cp_parser_error (parser, "could not resolve typename type");
26688 type = error_mark_node;
26692 type = maybe_process_partial_specialization (type);
26693 if (type == error_mark_node)
26695 type = NULL_TREE;
26696 goto done;
26699 /* Enter the scope indicated by the nested-name-specifier. */
26700 pushed_scope = push_scope (nested_name_specifier);
26701 /* Get the canonical version of this type. */
26702 type = TYPE_MAIN_DECL (type);
26703 /* Call push_template_decl if it seems like we should be defining a
26704 template either from the template headers or the type we're
26705 defining, so that we diagnose both extra and missing headers. */
26706 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26707 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
26708 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
26710 type = push_template_decl (type);
26711 if (type == error_mark_node)
26713 type = NULL_TREE;
26714 goto done;
26718 type = TREE_TYPE (type);
26719 *nested_name_specifier_p = true;
26721 else /* The name is not a nested name. */
26723 /* If the class was unnamed, create a dummy name. */
26724 if (!id)
26725 id = make_anon_name ();
26726 TAG_how how = (parser->in_type_id_in_expr_p
26727 ? TAG_how::INNERMOST_NON_CLASS
26728 : TAG_how::CURRENT_ONLY);
26729 type = xref_tag (class_key, id, how,
26730 parser->num_template_parameter_lists);
26733 /* Diagnose class/struct/union mismatches. */
26734 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
26735 true, true);
26737 /* Indicate whether this class was declared as a `class' or as a
26738 `struct'. */
26739 if (TREE_CODE (type) == RECORD_TYPE)
26740 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
26742 /* If this type was already complete, and we see another definition,
26743 that's an error. Likewise if the type is already being defined:
26744 this can happen, eg, when it's defined from within an expression
26745 (c++/84605). */
26746 if (type != error_mark_node
26747 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
26749 error_at (type_start_token->location, "redefinition of %q#T",
26750 type);
26751 inform (location_of (type), "previous definition of %q#T",
26752 type);
26753 type = NULL_TREE;
26754 goto done;
26756 else if (type == error_mark_node)
26757 type = NULL_TREE;
26759 if (type)
26761 if (current_lambda_expr ()
26762 && uses_parameter_packs (attributes))
26764 /* In a lambda this should work, but doesn't currently. */
26765 sorry ("unexpanded parameter pack in local class in lambda");
26766 attributes = NULL_TREE;
26769 /* Apply attributes now, before any use of the class as a template
26770 argument in its base list. */
26771 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
26772 fixup_attribute_variants (type);
26775 /* Associate constraints with the type. */
26776 if (flag_concepts)
26777 type = associate_classtype_constraints (type);
26779 /* We will have entered the scope containing the class; the names of
26780 base classes should be looked up in that context. For example:
26782 struct A { struct B {}; struct C; };
26783 struct A::C : B {};
26785 is valid. */
26787 /* Get the list of base-classes, if there is one. Defer access checking
26788 until the entire list has been seen, as per [class.access.general]. */
26789 push_deferring_access_checks (dk_deferred);
26790 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26792 if (type)
26793 pushclass (type);
26794 bases = cp_parser_base_clause (parser);
26795 if (type)
26796 popclass ();
26798 else
26799 bases = NULL_TREE;
26801 /* If we're really defining a class, process the base classes.
26802 If they're invalid, fail. */
26803 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26804 xref_basetypes (type, bases);
26806 /* Now that all bases have been seen and attached to the class, check
26807 accessibility of the types named in the base-clause. This must be
26808 done relative to the class scope, so that we accept e.g.
26810 struct A { protected: struct B {}; };
26811 struct C : A::B, A {}; // OK: A::B is accessible via base A
26813 as per [class.access.general]. */
26814 if (type)
26815 pushclass (type);
26816 pop_to_parent_deferring_access_checks ();
26817 if (type)
26818 popclass ();
26820 done:
26821 /* Leave the scope given by the nested-name-specifier. We will
26822 enter the class scope itself while processing the members. */
26823 if (pushed_scope)
26824 pop_scope (pushed_scope);
26826 if (invalid_explicit_specialization_p)
26828 end_specialization ();
26829 --parser->num_template_parameter_lists;
26832 if (type)
26833 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
26834 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
26835 CLASSTYPE_FINAL (type) = 1;
26836 out:
26837 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26838 return type;
26841 /* Parse a class-key.
26843 class-key:
26844 class
26845 struct
26846 union
26848 Returns the kind of class-key specified, or none_type to indicate
26849 error. */
26851 static enum tag_types
26852 cp_parser_class_key (cp_parser* parser)
26854 cp_token *token;
26855 enum tag_types tag_type;
26857 /* Look for the class-key. */
26858 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
26859 if (!token)
26860 return none_type;
26862 /* Check to see if the TOKEN is a class-key. */
26863 tag_type = cp_parser_token_is_class_key (token);
26864 if (!tag_type)
26865 cp_parser_error (parser, "expected class-key");
26866 return tag_type;
26869 /* Parse a type-parameter-key.
26871 type-parameter-key:
26872 class
26873 typename
26876 static void
26877 cp_parser_type_parameter_key (cp_parser* parser)
26879 /* Look for the type-parameter-key. */
26880 enum tag_types tag_type = none_type;
26881 cp_token *token = cp_lexer_peek_token (parser->lexer);
26882 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
26884 cp_lexer_consume_token (parser->lexer);
26885 if (pedantic && tag_type == typename_type
26886 && cxx_dialect < cxx17)
26887 /* typename is not allowed in a template template parameter
26888 by the standard until C++17. */
26889 pedwarn (token->location, OPT_Wc__17_extensions,
26890 "ISO C++ forbids typename key in template template parameter;"
26891 " use %<-std=c++17%> or %<-std=gnu++17%>");
26893 else
26894 cp_parser_error (parser, "expected %<class%> or %<typename%>");
26896 return;
26899 /* Parse an (optional) member-specification.
26901 member-specification:
26902 member-declaration member-specification [opt]
26903 access-specifier : member-specification [opt] */
26905 static void
26906 cp_parser_member_specification_opt (cp_parser* parser)
26908 while (true)
26910 cp_token *token;
26911 enum rid keyword;
26913 /* Peek at the next token. */
26914 token = cp_lexer_peek_token (parser->lexer);
26915 /* If it's a `}', or EOF then we've seen all the members. */
26916 if (token->type == CPP_CLOSE_BRACE
26917 || token->type == CPP_EOF
26918 || token->type == CPP_PRAGMA_EOL)
26919 break;
26921 /* See if this token is a keyword. */
26922 keyword = token->keyword;
26923 switch (keyword)
26925 case RID_PUBLIC:
26926 case RID_PROTECTED:
26927 case RID_PRIVATE:
26928 /* Consume the access-specifier. */
26929 cp_lexer_consume_token (parser->lexer);
26930 /* Remember which access-specifier is active. */
26931 current_access_specifier = token->u.value;
26932 /* Look for the `:'. */
26933 cp_parser_require (parser, CPP_COLON, RT_COLON);
26934 break;
26936 default:
26937 /* Accept #pragmas at class scope. */
26938 if (token->type == CPP_PRAGMA)
26940 cp_parser_pragma (parser, pragma_member, NULL);
26941 break;
26944 /* Otherwise, the next construction must be a
26945 member-declaration. */
26946 cp_parser_member_declaration (parser);
26951 /* Parse a member-declaration.
26953 member-declaration:
26954 decl-specifier-seq [opt] member-declarator-list [opt] ;
26955 function-definition ; [opt]
26956 :: [opt] nested-name-specifier template [opt] unqualified-id ;
26957 using-declaration
26958 template-declaration
26959 alias-declaration
26961 member-declarator-list:
26962 member-declarator
26963 member-declarator-list , member-declarator
26965 member-declarator:
26966 declarator pure-specifier [opt]
26967 declarator constant-initializer [opt]
26968 identifier [opt] : constant-expression
26970 GNU Extensions:
26972 member-declaration:
26973 __extension__ member-declaration
26975 member-declarator:
26976 declarator attributes [opt] pure-specifier [opt]
26977 declarator attributes [opt] constant-initializer [opt]
26978 identifier [opt] attributes [opt] : constant-expression
26980 C++0x Extensions:
26982 member-declaration:
26983 static_assert-declaration */
26985 static void
26986 cp_parser_member_declaration (cp_parser* parser)
26988 cp_decl_specifier_seq decl_specifiers;
26989 tree prefix_attributes;
26990 tree decl;
26991 int declares_class_or_enum;
26992 bool friend_p;
26993 cp_token *token = NULL;
26994 cp_token *decl_spec_token_start = NULL;
26995 cp_token *initializer_token_start = NULL;
26996 int saved_pedantic;
26997 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26999 /* Check for the `__extension__' keyword. */
27000 if (cp_parser_extension_opt (parser, &saved_pedantic))
27002 /* Recurse. */
27003 cp_parser_member_declaration (parser);
27004 /* Restore the old value of the PEDANTIC flag. */
27005 pedantic = saved_pedantic;
27007 return;
27010 /* Check for a template-declaration. */
27011 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27013 /* An explicit specialization here is an error condition, and we
27014 expect the specialization handler to detect and report this. */
27015 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27016 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
27017 cp_parser_explicit_specialization (parser);
27018 else
27019 cp_parser_template_declaration (parser, /*member_p=*/true);
27021 return;
27023 /* Check for a template introduction. */
27024 else if (cp_parser_template_declaration_after_export (parser, true))
27025 return;
27027 /* Check for a using-declaration. */
27028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27030 if (cxx_dialect < cxx11)
27031 /* Parse the using-declaration. */
27032 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
27033 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
27034 cp_parser_using_enum (parser);
27035 else
27037 tree decl;
27038 bool alias_decl_expected;
27039 cp_parser_parse_tentatively (parser);
27040 decl = cp_parser_alias_declaration (parser);
27041 /* Note that if we actually see the '=' token after the
27042 identifier, cp_parser_alias_declaration commits the
27043 tentative parse. In that case, we really expect an
27044 alias-declaration. Otherwise, we expect a using
27045 declaration. */
27046 alias_decl_expected =
27047 !cp_parser_uncommitted_to_tentative_parse_p (parser);
27048 cp_parser_parse_definitely (parser);
27050 if (alias_decl_expected)
27051 finish_member_declaration (decl);
27052 else
27053 cp_parser_using_declaration (parser,
27054 /*access_declaration_p=*/false);
27056 return;
27059 /* Check for @defs. */
27060 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
27062 tree ivar, member;
27063 tree ivar_chains = cp_parser_objc_defs_expression (parser);
27064 ivar = ivar_chains;
27065 while (ivar)
27067 member = ivar;
27068 ivar = TREE_CHAIN (member);
27069 TREE_CHAIN (member) = NULL_TREE;
27070 finish_member_declaration (member);
27072 return;
27075 /* If the next token is `static_assert' we have a static assertion. */
27076 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
27078 cp_parser_static_assert (parser, /*member_p=*/true);
27079 return;
27082 parser->colon_corrects_to_scope_p = false;
27084 cp_omp_declare_simd_data odsd;
27085 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
27086 goto out;
27088 /* Parse the decl-specifier-seq. */
27089 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27090 cp_parser_decl_specifier_seq (parser,
27091 (CP_PARSER_FLAGS_OPTIONAL
27092 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27093 &decl_specifiers,
27094 &declares_class_or_enum);
27096 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
27097 cp_parser_handle_directive_omp_attributes (parser,
27098 &decl_specifiers.attributes,
27099 &odsd, true);
27101 /* Check for an invalid type-name. */
27102 if (!decl_specifiers.any_type_specifiers_p
27103 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27104 goto out;
27105 /* If there is no declarator, then the decl-specifier-seq should
27106 specify a type. */
27107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27109 /* If there was no decl-specifier-seq, and the next token is a
27110 `;', then we have something like:
27112 struct S { ; };
27114 [class.mem]
27116 Each member-declaration shall declare at least one member
27117 name of the class. */
27118 if (!decl_specifiers.any_specifiers_p)
27120 cp_token *token = cp_lexer_peek_token (parser->lexer);
27121 if (!in_system_header_at (token->location))
27123 gcc_rich_location richloc (token->location);
27124 richloc.add_fixit_remove ();
27125 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27128 else
27130 /* See if this declaration is a friend. */
27131 friend_p = cp_parser_friend_p (&decl_specifiers);
27132 /* If there were decl-specifiers, check to see if there was
27133 a class-declaration. */
27134 tree type = check_tag_decl (&decl_specifiers,
27135 /*explicit_type_instantiation_p=*/false);
27136 /* Nested classes have already been added to the class, but
27137 a `friend' needs to be explicitly registered. */
27138 if (friend_p)
27140 /* If the `friend' keyword was present, the friend must
27141 be introduced with a class-key. */
27142 if (!declares_class_or_enum && cxx_dialect < cxx11)
27143 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27144 "in C++03 a class-key must be used "
27145 "when declaring a friend");
27146 /* In this case:
27148 template <typename T> struct A {
27149 friend struct A<T>::B;
27152 A<T>::B will be represented by a TYPENAME_TYPE, and
27153 therefore not recognized by check_tag_decl. */
27154 if (!type)
27156 type = decl_specifiers.type;
27157 if (type && TREE_CODE (type) == TYPE_DECL)
27158 type = TREE_TYPE (type);
27160 /* Warn if an attribute cannot appear here, as per
27161 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
27162 we ignore attributes in elaborated-type-specifiers. */
27163 if (!declares_class_or_enum
27164 && cxx11_attribute_p (decl_specifiers.attributes))
27166 decl_specifiers.attributes = NULL_TREE;
27167 if (warning_at (decl_spec_token_start->location,
27168 OPT_Wattributes, "attribute ignored"))
27169 inform (decl_spec_token_start->location, "an attribute "
27170 "that appertains to a friend declaration that "
27171 "is not a definition is ignored");
27173 if (!type || !TYPE_P (type))
27174 error_at (decl_spec_token_start->location,
27175 "friend declaration does not name a class or "
27176 "function");
27177 else
27178 make_friend_class (current_class_type, type,
27179 /*complain=*/true);
27181 /* If there is no TYPE, an error message will already have
27182 been issued. */
27183 else if (!type || type == error_mark_node)
27185 /* An anonymous aggregate has to be handled specially; such
27186 a declaration really declares a data member (with a
27187 particular type), as opposed to a nested class. */
27188 else if (ANON_AGGR_TYPE_P (type))
27190 /* C++11 9.5/6. */
27191 if (decl_specifiers.storage_class != sc_none)
27192 error_at (decl_spec_token_start->location,
27193 "a storage class on an anonymous aggregate "
27194 "in class scope is not allowed");
27196 /* Remove constructors and such from TYPE, now that we
27197 know it is an anonymous aggregate. */
27198 fixup_anonymous_aggr (type);
27199 /* And make the corresponding data member. */
27200 decl = build_decl (decl_spec_token_start->location,
27201 FIELD_DECL, NULL_TREE, type);
27202 /* Add it to the class. */
27203 finish_member_declaration (decl);
27205 else
27206 cp_parser_check_access_in_redeclaration
27207 (TYPE_NAME (type),
27208 decl_spec_token_start->location);
27211 else
27213 bool assume_semicolon = false;
27215 /* Clear attributes from the decl_specifiers but keep them
27216 around as prefix attributes that apply them to the entity
27217 being declared. */
27218 prefix_attributes = decl_specifiers.attributes;
27219 decl_specifiers.attributes = NULL_TREE;
27220 if (parser->omp_declare_simd
27221 && (parser->omp_declare_simd->attribs[0]
27222 == &decl_specifiers.attributes))
27223 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27225 /* See if these declarations will be friends. */
27226 friend_p = cp_parser_friend_p (&decl_specifiers);
27228 /* Keep going until we hit the `;' at the end of the
27229 declaration. */
27230 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27232 tree attributes = NULL_TREE;
27233 tree first_attribute;
27234 tree initializer;
27235 bool named_bitfld = false;
27237 /* Peek at the next token. */
27238 token = cp_lexer_peek_token (parser->lexer);
27240 /* The following code wants to know early if it is a bit-field
27241 or some other declaration. Attributes can appear before
27242 the `:' token. Skip over them without consuming any tokens
27243 to peek if they are followed by `:'. */
27244 if (cp_next_tokens_can_be_attribute_p (parser)
27245 || (token->type == CPP_NAME
27246 && cp_nth_tokens_can_be_attribute_p (parser, 2)
27247 && (named_bitfld = true)))
27249 size_t n
27250 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27251 token = cp_lexer_peek_nth_token (parser->lexer, n);
27254 /* Check for a bitfield declaration. */
27255 if (token->type == CPP_COLON
27256 || (token->type == CPP_NAME
27257 && token == cp_lexer_peek_token (parser->lexer)
27258 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27259 && (named_bitfld = true)))
27261 tree identifier;
27262 tree width;
27263 tree late_attributes = NULL_TREE;
27264 location_t id_location
27265 = cp_lexer_peek_token (parser->lexer)->location;
27267 if (named_bitfld)
27268 identifier = cp_parser_identifier (parser);
27269 else
27270 identifier = NULL_TREE;
27272 /* Look for attributes that apply to the bitfield. */
27273 attributes = cp_parser_attributes_opt (parser);
27275 /* Consume the `:' token. */
27276 cp_lexer_consume_token (parser->lexer);
27278 /* Get the width of the bitfield. */
27279 width = cp_parser_constant_expression (parser, false, NULL,
27280 cxx_dialect >= cxx11);
27282 /* In C++20 and as extension for C++11 and above we allow
27283 default member initializers for bit-fields. */
27284 initializer = NULL_TREE;
27285 if (cxx_dialect >= cxx11
27286 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27287 || cp_lexer_next_token_is (parser->lexer,
27288 CPP_OPEN_BRACE)))
27290 location_t loc
27291 = cp_lexer_peek_token (parser->lexer)->location;
27292 if (cxx_dialect < cxx20
27293 && identifier != NULL_TREE)
27294 pedwarn (loc, OPT_Wc__20_extensions,
27295 "default member initializers for bit-fields "
27296 "only available with %<-std=c++20%> or "
27297 "%<-std=gnu++20%>");
27299 initializer = cp_parser_save_nsdmi (parser);
27300 if (identifier == NULL_TREE)
27302 error_at (loc, "default member initializer for "
27303 "unnamed bit-field");
27304 initializer = NULL_TREE;
27307 else
27309 /* Look for attributes that apply to the bitfield after
27310 the `:' token and width. This is where GCC used to
27311 parse attributes in the past, pedwarn if there is
27312 a std attribute. */
27313 if (cp_next_tokens_can_be_std_attribute_p (parser))
27314 pedwarn (input_location, OPT_Wpedantic,
27315 "ISO C++ allows bit-field attributes only "
27316 "before the %<:%> token");
27318 late_attributes = cp_parser_attributes_opt (parser);
27321 attributes = attr_chainon (attributes, late_attributes);
27323 /* Remember which attributes are prefix attributes and
27324 which are not. */
27325 first_attribute = attributes;
27326 /* Combine the attributes. */
27327 attributes = attr_chainon (prefix_attributes, attributes);
27329 /* Create the bitfield declaration. */
27330 decl = grokbitfield (identifier
27331 ? make_id_declarator (NULL_TREE,
27332 identifier,
27333 sfk_none,
27334 id_location)
27335 : NULL,
27336 &decl_specifiers,
27337 width, initializer,
27338 attributes);
27340 else
27342 cp_declarator *declarator;
27343 tree asm_specification;
27344 int ctor_dtor_or_conv_p;
27345 bool static_p = (decl_specifiers.storage_class == sc_static);
27346 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27347 /* We can't delay parsing for friends,
27348 alias-declarations, and typedefs, even though the
27349 standard seems to require it. */
27350 if (!friend_p
27351 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27352 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27354 /* Parse the declarator. */
27355 declarator
27356 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27357 flags,
27358 &ctor_dtor_or_conv_p,
27359 /*parenthesized_p=*/NULL,
27360 /*member_p=*/true,
27361 friend_p, static_p);
27363 /* If something went wrong parsing the declarator, make sure
27364 that we at least consume some tokens. */
27365 if (declarator == cp_error_declarator)
27367 /* Skip to the end of the statement. */
27368 cp_parser_skip_to_end_of_statement (parser);
27369 /* If the next token is not a semicolon, that is
27370 probably because we just skipped over the body of
27371 a function. So, we consume a semicolon if
27372 present, but do not issue an error message if it
27373 is not present. */
27374 if (cp_lexer_next_token_is (parser->lexer,
27375 CPP_SEMICOLON))
27376 cp_lexer_consume_token (parser->lexer);
27377 goto out;
27380 /* Handle class-scope non-template C++17 deduction guides. */
27381 cp_parser_maybe_adjust_declarator_for_dguide (parser,
27382 &decl_specifiers,
27383 declarator,
27384 &ctor_dtor_or_conv_p);
27386 if (declares_class_or_enum & 2)
27387 cp_parser_check_for_definition_in_return_type
27388 (declarator, decl_specifiers.type,
27389 decl_specifiers.locations[ds_type_spec]);
27391 /* Look for an asm-specification. */
27392 asm_specification = cp_parser_asm_specification_opt (parser);
27393 /* Look for attributes that apply to the declaration. */
27394 attributes = cp_parser_attributes_opt (parser);
27395 /* Remember which attributes are prefix attributes and
27396 which are not. */
27397 first_attribute = attributes;
27398 /* Combine the attributes. */
27399 attributes = attr_chainon (prefix_attributes, attributes);
27401 /* If it's an `=', then we have a constant-initializer or a
27402 pure-specifier. It is not correct to parse the
27403 initializer before registering the member declaration
27404 since the member declaration should be in scope while
27405 its initializer is processed. However, the rest of the
27406 front end does not yet provide an interface that allows
27407 us to handle this correctly. */
27408 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27410 /* In [class.mem]:
27412 A pure-specifier shall be used only in the declaration of
27413 a virtual function.
27415 A member-declarator can contain a constant-initializer
27416 only if it declares a static member of integral or
27417 enumeration type.
27419 Therefore, if the DECLARATOR is for a function, we look
27420 for a pure-specifier; otherwise, we look for a
27421 constant-initializer. When we call `grokfield', it will
27422 perform more stringent semantics checks. */
27423 initializer_token_start = cp_lexer_peek_token (parser->lexer);
27424 declarator->init_loc = initializer_token_start->location;
27425 if (function_declarator_p (declarator)
27426 || (decl_specifiers.type
27427 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27428 && declarator->kind == cdk_id
27429 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27430 == FUNCTION_TYPE)))
27431 initializer = cp_parser_pure_specifier (parser);
27432 else if (decl_specifiers.storage_class != sc_static)
27433 initializer = cp_parser_save_nsdmi (parser);
27434 else if (cxx_dialect >= cxx11)
27436 bool nonconst;
27437 /* Don't require a constant rvalue in C++11, since we
27438 might want a reference constant. We'll enforce
27439 constancy later. */
27440 cp_lexer_consume_token (parser->lexer);
27441 /* Parse the initializer. */
27442 initializer = cp_parser_initializer_clause (parser,
27443 &nonconst);
27445 else
27446 /* Parse the initializer. */
27447 initializer = cp_parser_constant_initializer (parser);
27449 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27450 && !function_declarator_p (declarator))
27452 bool x;
27453 declarator->init_loc
27454 = cp_lexer_peek_token (parser->lexer)->location;
27455 if (decl_specifiers.storage_class != sc_static)
27456 initializer = cp_parser_save_nsdmi (parser);
27457 else
27458 initializer = cp_parser_initializer (parser, &x, &x);
27460 /* Detect invalid bit-field cases such as
27462 int *p : 4;
27463 int &&r : 3;
27465 and similar. */
27466 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27467 /* If there were no type specifiers, it was a
27468 constructor. */
27469 && decl_specifiers.any_type_specifiers_p)
27471 /* This is called for a decent diagnostic only. */
27472 tree d = grokdeclarator (declarator, &decl_specifiers,
27473 BITFIELD, /*initialized=*/false,
27474 &attributes);
27475 if (!error_operand_p (d))
27476 error_at (DECL_SOURCE_LOCATION (d),
27477 "bit-field %qD has non-integral type %qT",
27478 d, TREE_TYPE (d));
27479 cp_parser_skip_to_end_of_statement (parser);
27480 /* Avoid "extra ;" pedwarns. */
27481 if (cp_lexer_next_token_is (parser->lexer,
27482 CPP_SEMICOLON))
27483 cp_lexer_consume_token (parser->lexer);
27484 goto out;
27486 /* Otherwise, there is no initializer. */
27487 else
27488 initializer = NULL_TREE;
27490 /* See if we are probably looking at a function
27491 definition. We are certainly not looking at a
27492 member-declarator. Calling `grokfield' has
27493 side-effects, so we must not do it unless we are sure
27494 that we are looking at a member-declarator. */
27495 if (cp_parser_token_starts_function_definition_p
27496 (cp_lexer_peek_token (parser->lexer)))
27498 /* The grammar does not allow a pure-specifier to be
27499 used when a member function is defined. (It is
27500 possible that this fact is an oversight in the
27501 standard, since a pure function may be defined
27502 outside of the class-specifier. */
27503 if (initializer && initializer_token_start)
27504 error_at (initializer_token_start->location,
27505 "pure-specifier on function-definition");
27506 decl = cp_parser_save_member_function_body (parser,
27507 &decl_specifiers,
27508 declarator,
27509 attributes);
27510 if (parser->fully_implicit_function_template_p)
27511 decl = finish_fully_implicit_template (parser, decl);
27512 /* If the member was not a friend, declare it here. */
27513 if (!friend_p)
27514 finish_member_declaration (decl);
27515 /* Peek at the next token. */
27516 token = cp_lexer_peek_token (parser->lexer);
27517 /* If the next token is a semicolon, consume it. */
27518 if (token->type == CPP_SEMICOLON)
27520 location_t semicolon_loc
27521 = cp_lexer_consume_token (parser->lexer)->location;
27522 gcc_rich_location richloc (semicolon_loc);
27523 richloc.add_fixit_remove ();
27524 warning_at (&richloc, OPT_Wextra_semi,
27525 "extra %<;%> after in-class "
27526 "function definition");
27528 goto out;
27530 else
27531 if (declarator->kind == cdk_function)
27532 declarator->id_loc = token->location;
27533 /* Create the declaration. */
27534 decl = grokfield (declarator, &decl_specifiers,
27535 initializer, /*init_const_expr_p=*/true,
27536 asm_specification, attributes);
27537 if (parser->fully_implicit_function_template_p)
27539 if (friend_p)
27540 finish_fully_implicit_template (parser, 0);
27541 else
27542 decl = finish_fully_implicit_template (parser, decl);
27546 cp_finalize_omp_declare_simd (parser, decl);
27547 cp_finalize_oacc_routine (parser, decl, false);
27549 /* Reset PREFIX_ATTRIBUTES. */
27550 if (attributes != error_mark_node)
27552 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27553 attributes = TREE_CHAIN (attributes);
27554 if (attributes)
27555 TREE_CHAIN (attributes) = NULL_TREE;
27558 /* If there is any qualification still in effect, clear it
27559 now; we will be starting fresh with the next declarator. */
27560 parser->scope = NULL_TREE;
27561 parser->qualifying_scope = NULL_TREE;
27562 parser->object_scope = NULL_TREE;
27563 /* If it's a `,', then there are more declarators. */
27564 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27566 cp_lexer_consume_token (parser->lexer);
27567 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27569 cp_token *token = cp_lexer_previous_token (parser->lexer);
27570 gcc_rich_location richloc (token->location);
27571 richloc.add_fixit_remove ();
27572 error_at (&richloc, "stray %<,%> at end of "
27573 "member declaration");
27576 /* If the next token isn't a `;', then we have a parse error. */
27577 else if (cp_lexer_next_token_is_not (parser->lexer,
27578 CPP_SEMICOLON))
27580 /* The next token might be a ways away from where the
27581 actual semicolon is missing. Find the previous token
27582 and use that for our error position. */
27583 cp_token *token = cp_lexer_previous_token (parser->lexer);
27584 gcc_rich_location richloc (token->location);
27585 richloc.add_fixit_insert_after (";");
27586 error_at (&richloc, "expected %<;%> at end of "
27587 "member declaration");
27589 /* Assume that the user meant to provide a semicolon. If
27590 we were to cp_parser_skip_to_end_of_statement, we might
27591 skip to a semicolon inside a member function definition
27592 and issue nonsensical error messages. */
27593 assume_semicolon = true;
27596 if (decl)
27598 /* Add DECL to the list of members. */
27599 if (!friend_p
27600 /* Explicitly include, eg, NSDMIs, for better error
27601 recovery (c++/58650). */
27602 || !DECL_DECLARES_FUNCTION_P (decl))
27603 finish_member_declaration (decl);
27605 if (DECL_DECLARES_FUNCTION_P (decl))
27606 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
27607 else if (TREE_CODE (decl) == FIELD_DECL
27608 && DECL_INITIAL (decl))
27609 /* Add DECL to the queue of NSDMI to be parsed later. */
27610 vec_safe_push (unparsed_nsdmis, decl);
27613 if (assume_semicolon)
27614 goto out;
27618 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27619 out:
27620 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27621 cp_finalize_omp_declare_simd (parser, &odsd);
27624 /* Parse a pure-specifier.
27626 pure-specifier:
27629 Returns INTEGER_ZERO_NODE if a pure specifier is found.
27630 Otherwise, ERROR_MARK_NODE is returned. */
27632 static tree
27633 cp_parser_pure_specifier (cp_parser* parser)
27635 cp_token *token;
27637 /* Look for the `=' token. */
27638 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27639 return error_mark_node;
27640 /* Look for the `0' token. */
27641 token = cp_lexer_peek_token (parser->lexer);
27643 if (token->type == CPP_EOF
27644 || token->type == CPP_PRAGMA_EOL)
27645 return error_mark_node;
27647 cp_lexer_consume_token (parser->lexer);
27649 /* Accept = default or = delete in c++0x mode. */
27650 if (token->keyword == RID_DEFAULT
27651 || token->keyword == RID_DELETE)
27653 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
27654 return token->u.value;
27657 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
27658 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
27660 cp_parser_error (parser,
27661 "invalid pure specifier (only %<= 0%> is allowed)");
27662 cp_parser_skip_to_end_of_statement (parser);
27663 return error_mark_node;
27665 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27667 error_at (token->location, "templates may not be %<virtual%>");
27668 return error_mark_node;
27671 return integer_zero_node;
27674 /* Parse a constant-initializer.
27676 constant-initializer:
27677 = constant-expression
27679 Returns a representation of the constant-expression. */
27681 static tree
27682 cp_parser_constant_initializer (cp_parser* parser)
27684 /* Look for the `=' token. */
27685 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27686 return error_mark_node;
27688 /* It is invalid to write:
27690 struct S { static const int i = { 7 }; };
27693 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27695 cp_parser_error (parser,
27696 "a brace-enclosed initializer is not allowed here");
27697 /* Consume the opening brace. */
27698 matching_braces braces;
27699 braces.consume_open (parser);
27700 /* Skip the initializer. */
27701 cp_parser_skip_to_closing_brace (parser);
27702 /* Look for the trailing `}'. */
27703 braces.require_close (parser);
27705 return error_mark_node;
27708 return cp_parser_constant_expression (parser);
27711 /* Derived classes [gram.class.derived] */
27713 /* Parse a base-clause.
27715 base-clause:
27716 : base-specifier-list
27718 base-specifier-list:
27719 base-specifier ... [opt]
27720 base-specifier-list , base-specifier ... [opt]
27722 Returns a TREE_LIST representing the base-classes, in the order in
27723 which they were declared. The representation of each node is as
27724 described by cp_parser_base_specifier.
27726 In the case that no bases are specified, this function will return
27727 NULL_TREE, not ERROR_MARK_NODE. */
27729 static tree
27730 cp_parser_base_clause (cp_parser* parser)
27732 tree bases = NULL_TREE;
27734 /* Look for the `:' that begins the list. */
27735 cp_parser_require (parser, CPP_COLON, RT_COLON);
27737 /* Scan the base-specifier-list. */
27738 while (true)
27740 cp_token *token;
27741 tree base;
27742 bool pack_expansion_p = false;
27744 /* Look for the base-specifier. */
27745 base = cp_parser_base_specifier (parser);
27746 /* Look for the (optional) ellipsis. */
27747 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27749 /* Consume the `...'. */
27750 cp_lexer_consume_token (parser->lexer);
27752 pack_expansion_p = true;
27755 /* Add BASE to the front of the list. */
27756 if (base && base != error_mark_node)
27758 if (pack_expansion_p)
27759 /* Make this a pack expansion type. */
27760 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
27762 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
27764 TREE_CHAIN (base) = bases;
27765 bases = base;
27768 /* Peek at the next token. */
27769 token = cp_lexer_peek_token (parser->lexer);
27770 /* If it's not a comma, then the list is complete. */
27771 if (token->type != CPP_COMMA)
27772 break;
27773 /* Consume the `,'. */
27774 cp_lexer_consume_token (parser->lexer);
27777 /* PARSER->SCOPE may still be non-NULL at this point, if the last
27778 base class had a qualified name. However, the next name that
27779 appears is certainly not qualified. */
27780 parser->scope = NULL_TREE;
27781 parser->qualifying_scope = NULL_TREE;
27782 parser->object_scope = NULL_TREE;
27784 return nreverse (bases);
27787 /* Parse a base-specifier.
27789 base-specifier:
27790 :: [opt] nested-name-specifier [opt] class-name
27791 virtual access-specifier [opt] :: [opt] nested-name-specifier
27792 [opt] class-name
27793 access-specifier virtual [opt] :: [opt] nested-name-specifier
27794 [opt] class-name
27796 Returns a TREE_LIST. The TREE_PURPOSE will be one of
27797 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
27798 indicate the specifiers provided. The TREE_VALUE will be a TYPE
27799 (or the ERROR_MARK_NODE) indicating the type that was specified. */
27801 static tree
27802 cp_parser_base_specifier (cp_parser* parser)
27804 cp_token *token;
27805 bool done = false;
27806 bool virtual_p = false;
27807 bool duplicate_virtual_error_issued_p = false;
27808 bool duplicate_access_error_issued_p = false;
27809 bool class_scope_p, template_p;
27810 tree access = access_default_node;
27811 tree type;
27813 /* Process the optional `virtual' and `access-specifier'. */
27814 while (!done)
27816 /* Peek at the next token. */
27817 token = cp_lexer_peek_token (parser->lexer);
27818 /* Process `virtual'. */
27819 switch (token->keyword)
27821 case RID_VIRTUAL:
27822 /* If `virtual' appears more than once, issue an error. */
27823 if (virtual_p && !duplicate_virtual_error_issued_p)
27825 cp_parser_error (parser,
27826 "%<virtual%> specified more than once in base-specifier");
27827 duplicate_virtual_error_issued_p = true;
27830 virtual_p = true;
27832 /* Consume the `virtual' token. */
27833 cp_lexer_consume_token (parser->lexer);
27835 break;
27837 case RID_PUBLIC:
27838 case RID_PROTECTED:
27839 case RID_PRIVATE:
27840 /* If more than one access specifier appears, issue an
27841 error. */
27842 if (access != access_default_node
27843 && !duplicate_access_error_issued_p)
27845 cp_parser_error (parser,
27846 "more than one access specifier in base-specifier");
27847 duplicate_access_error_issued_p = true;
27850 access = ridpointers[(int) token->keyword];
27852 /* Consume the access-specifier. */
27853 cp_lexer_consume_token (parser->lexer);
27855 break;
27857 default:
27858 done = true;
27859 break;
27862 /* It is not uncommon to see programs mechanically, erroneously, use
27863 the 'typename' keyword to denote (dependent) qualified types
27864 as base classes. */
27865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27867 token = cp_lexer_peek_token (parser->lexer);
27868 if (!processing_template_decl)
27869 error_at (token->location,
27870 "keyword %<typename%> not allowed outside of templates");
27871 else
27872 error_at (token->location,
27873 "keyword %<typename%> not allowed in this context "
27874 "(the base class is implicitly a type)");
27875 cp_lexer_consume_token (parser->lexer);
27878 /* Look for the optional `::' operator. */
27879 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
27880 /* Look for the nested-name-specifier. The simplest way to
27881 implement:
27883 [temp.res]
27885 The keyword `typename' is not permitted in a base-specifier or
27886 mem-initializer; in these contexts a qualified name that
27887 depends on a template-parameter is implicitly assumed to be a
27888 type name.
27890 is to pretend that we have seen the `typename' keyword at this
27891 point. */
27892 cp_parser_nested_name_specifier_opt (parser,
27893 /*typename_keyword_p=*/true,
27894 /*check_dependency_p=*/true,
27895 /*type_p=*/true,
27896 /*is_declaration=*/true);
27897 /* If the base class is given by a qualified name, assume that names
27898 we see are type names or templates, as appropriate. */
27899 class_scope_p = (parser->scope && TYPE_P (parser->scope));
27900 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
27902 if (!parser->scope
27903 && cp_lexer_next_token_is_decltype (parser->lexer))
27904 /* DR 950 allows decltype as a base-specifier. */
27905 type = cp_parser_decltype (parser);
27906 else
27908 /* Otherwise, look for the class-name. */
27909 type = cp_parser_class_name (parser,
27910 class_scope_p,
27911 template_p,
27912 typename_type,
27913 /*check_dependency_p=*/true,
27914 /*class_head_p=*/false,
27915 /*is_declaration=*/true);
27916 type = TREE_TYPE (type);
27919 if (type == error_mark_node)
27920 return error_mark_node;
27922 return finish_base_specifier (type, access, virtual_p);
27925 /* Exception handling [gram.exception] */
27927 /* Save the tokens that make up the noexcept-specifier for a member-function.
27928 Returns a DEFERRED_PARSE. */
27930 static tree
27931 cp_parser_save_noexcept (cp_parser *parser)
27933 cp_token *first = parser->lexer->next_token;
27934 /* We want everything up to, including, the final ')'. */
27935 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
27936 cp_token *last = parser->lexer->next_token;
27938 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
27939 to carry the information we will need. */
27940 tree expr = make_node (DEFERRED_PARSE);
27941 /* Save away the noexcept-specifier; we will process it when the
27942 class is complete. */
27943 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
27944 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
27945 expr = build_tree_list (expr, NULL_TREE);
27946 return expr;
27949 /* Used for late processing of noexcept-specifiers of member-functions.
27950 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
27951 we saved for later; parse it now. DECL is the declaration of the
27952 member function. */
27954 static tree
27955 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
27957 /* Make sure we've gotten something that hasn't been parsed yet. */
27958 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
27960 push_unparsed_function_queues (parser);
27962 /* Push the saved tokens for the noexcept-specifier onto the parser's
27963 lexer stack. */
27964 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
27965 cp_parser_push_lexer_for_tokens (parser, tokens);
27967 /* Parse the cached noexcept-specifier. */
27968 tree parsed_arg
27969 = cp_parser_noexcept_specification_opt (parser,
27970 CP_PARSER_FLAGS_NONE,
27971 /*require_constexpr=*/true,
27972 /*consumed_expr=*/NULL,
27973 /*return_cond=*/false);
27975 /* Revert to the main lexer. */
27976 cp_parser_pop_lexer (parser);
27978 /* Restore the queue. */
27979 pop_unparsed_function_queues (parser);
27981 /* And we're done. */
27982 return parsed_arg;
27985 /* Perform late checking of overriding function with respect to their
27986 noexcept-specifiers. TYPE is the class and FNDECL is the function
27987 that potentially overrides some virtual function with the same
27988 signature. */
27990 static void
27991 noexcept_override_late_checks (tree type, tree fndecl)
27993 tree binfo = TYPE_BINFO (type);
27994 tree base_binfo;
27996 if (DECL_STATIC_FUNCTION_P (fndecl))
27997 return;
27999 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
28001 tree basetype = BINFO_TYPE (base_binfo);
28003 if (!TYPE_POLYMORPHIC_P (basetype))
28004 continue;
28006 tree fn = look_for_overrides_here (basetype, fndecl);
28007 if (fn)
28008 maybe_check_overriding_exception_spec (fndecl, fn);
28012 /* Parse an (optional) noexcept-specification.
28014 noexcept-specification:
28015 noexcept ( constant-expression ) [opt]
28017 If no noexcept-specification is present, returns NULL_TREE.
28018 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28019 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28020 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28021 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28022 in which case a boolean condition is returned instead. The parser flags
28023 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28024 the (member) function is `const'. */
28026 static tree
28027 cp_parser_noexcept_specification_opt (cp_parser* parser,
28028 cp_parser_flags flags,
28029 bool require_constexpr,
28030 bool* consumed_expr,
28031 bool return_cond)
28033 cp_token *token;
28034 const char *saved_message;
28036 /* Peek at the next token. */
28037 token = cp_lexer_peek_token (parser->lexer);
28039 /* Is it a noexcept-specification? */
28040 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
28042 tree expr;
28044 /* [class.mem]/6 says that a noexcept-specifer (within the
28045 member-specification of the class) is a complete-class context of
28046 a class. So, if the noexcept-specifier has the optional expression,
28047 just save the tokens, and reparse this after we're done with the
28048 class. */
28050 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
28051 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
28052 /* No need to delay parsing for a number literal or true/false. */
28053 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
28054 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28055 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
28056 && at_class_scope_p ()
28057 && TYPE_BEING_DEFINED (current_class_type)
28058 && !LAMBDA_TYPE_P (current_class_type))
28059 return cp_parser_save_noexcept (parser);
28061 cp_lexer_consume_token (parser->lexer);
28063 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28065 matching_parens parens;
28066 parens.consume_open (parser);
28068 if (require_constexpr)
28070 /* Types may not be defined in an exception-specification. */
28071 saved_message = parser->type_definition_forbidden_message;
28072 parser->type_definition_forbidden_message
28073 = G_("types may not be defined in an exception-specification");
28075 bool non_constant_p;
28076 expr
28077 = cp_parser_constant_expression (parser,
28078 /*allow_non_constant=*/true,
28079 &non_constant_p);
28080 if (non_constant_p
28081 && !require_potential_rvalue_constant_expression (expr))
28083 expr = NULL_TREE;
28084 return_cond = true;
28087 /* Restore the saved message. */
28088 parser->type_definition_forbidden_message = saved_message;
28090 else
28092 expr = cp_parser_expression (parser);
28093 *consumed_expr = true;
28096 parens.require_close (parser);
28098 else
28100 expr = boolean_true_node;
28101 if (!require_constexpr)
28102 *consumed_expr = false;
28105 /* We cannot build a noexcept-spec right away because this will check
28106 that expr is a constexpr. */
28107 if (!return_cond)
28108 return build_noexcept_spec (expr, tf_warning_or_error);
28109 else
28110 return expr;
28112 else
28113 return NULL_TREE;
28116 /* Parse an (optional) exception-specification.
28118 exception-specification:
28119 throw ( type-id-list [opt] )
28121 Returns a TREE_LIST representing the exception-specification. The
28122 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
28123 control parsing. QUALS are qualifiers indicating whether the (member)
28124 function is `const'. */
28126 static tree
28127 cp_parser_exception_specification_opt (cp_parser* parser,
28128 cp_parser_flags flags)
28130 cp_token *token;
28131 tree type_id_list;
28132 const char *saved_message;
28134 /* Peek at the next token. */
28135 token = cp_lexer_peek_token (parser->lexer);
28137 /* Is it a noexcept-specification? */
28138 type_id_list
28139 = cp_parser_noexcept_specification_opt (parser, flags,
28140 /*require_constexpr=*/true,
28141 /*consumed_expr=*/NULL,
28142 /*return_cond=*/false);
28143 if (type_id_list != NULL_TREE)
28144 return type_id_list;
28146 /* If it's not `throw', then there's no exception-specification. */
28147 if (!cp_parser_is_keyword (token, RID_THROW))
28148 return NULL_TREE;
28150 location_t loc = token->location;
28152 /* Consume the `throw'. */
28153 cp_lexer_consume_token (parser->lexer);
28155 /* Look for the `('. */
28156 matching_parens parens;
28157 parens.require_open (parser);
28159 /* Peek at the next token. */
28160 token = cp_lexer_peek_token (parser->lexer);
28161 /* If it's not a `)', then there is a type-id-list. */
28162 if (token->type != CPP_CLOSE_PAREN)
28164 /* Types may not be defined in an exception-specification. */
28165 saved_message = parser->type_definition_forbidden_message;
28166 parser->type_definition_forbidden_message
28167 = G_("types may not be defined in an exception-specification");
28168 /* Parse the type-id-list. */
28169 type_id_list = cp_parser_type_id_list (parser);
28170 /* Restore the saved message. */
28171 parser->type_definition_forbidden_message = saved_message;
28173 if (cxx_dialect >= cxx17)
28175 error_at (loc, "ISO C++17 does not allow dynamic exception "
28176 "specifications");
28177 type_id_list = NULL_TREE;
28179 else if (cxx_dialect >= cxx11)
28180 warning_at (loc, OPT_Wdeprecated,
28181 "dynamic exception specifications are deprecated in "
28182 "C++11");
28184 /* In C++17, throw() is equivalent to noexcept (true). throw()
28185 is deprecated in C++11 and above as well, but is still widely used,
28186 so don't warn about it yet. */
28187 else if (cxx_dialect >= cxx17)
28188 type_id_list = noexcept_true_spec;
28189 else
28190 type_id_list = empty_except_spec;
28192 /* Look for the `)'. */
28193 parens.require_close (parser);
28195 return type_id_list;
28198 /* Parse an (optional) type-id-list.
28200 type-id-list:
28201 type-id ... [opt]
28202 type-id-list , type-id ... [opt]
28204 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28205 in the order that the types were presented. */
28207 static tree
28208 cp_parser_type_id_list (cp_parser* parser)
28210 tree types = NULL_TREE;
28212 while (true)
28214 cp_token *token;
28215 tree type;
28217 token = cp_lexer_peek_token (parser->lexer);
28219 /* Get the next type-id. */
28220 type = cp_parser_type_id (parser);
28221 /* Check for invalid 'auto'. */
28222 if (flag_concepts && type_uses_auto (type))
28224 error_at (token->location,
28225 "invalid use of %<auto%> in exception-specification");
28226 type = error_mark_node;
28228 /* Parse the optional ellipsis. */
28229 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28231 /* Consume the `...'. */
28232 cp_lexer_consume_token (parser->lexer);
28234 /* Turn the type into a pack expansion expression. */
28235 type = make_pack_expansion (type);
28237 /* Add it to the list. */
28238 types = add_exception_specifier (types, type, /*complain=*/1);
28239 /* Peek at the next token. */
28240 token = cp_lexer_peek_token (parser->lexer);
28241 /* If it is not a `,', we are done. */
28242 if (token->type != CPP_COMMA)
28243 break;
28244 /* Consume the `,'. */
28245 cp_lexer_consume_token (parser->lexer);
28248 return nreverse (types);
28251 /* Parse a try-block.
28253 try-block:
28254 try compound-statement handler-seq */
28256 static tree
28257 cp_parser_try_block (cp_parser* parser)
28259 tree try_block;
28261 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28262 if (parser->in_function_body
28263 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28264 && cxx_dialect < cxx20)
28265 pedwarn (input_location, OPT_Wc__20_extensions,
28266 "%<try%> in %<constexpr%> function only "
28267 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28269 try_block = begin_try_block ();
28270 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28271 finish_try_block (try_block);
28272 cp_parser_handler_seq (parser);
28273 finish_handler_sequence (try_block);
28275 return try_block;
28278 /* Parse a function-try-block.
28280 function-try-block:
28281 try ctor-initializer [opt] function-body handler-seq */
28283 static void
28284 cp_parser_function_try_block (cp_parser* parser)
28286 tree compound_stmt;
28287 tree try_block;
28289 /* Look for the `try' keyword. */
28290 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28291 return;
28292 /* Let the rest of the front end know where we are. */
28293 try_block = begin_function_try_block (&compound_stmt);
28294 /* Parse the function-body. */
28295 cp_parser_ctor_initializer_opt_and_function_body
28296 (parser, /*in_function_try_block=*/true);
28297 /* We're done with the `try' part. */
28298 finish_function_try_block (try_block);
28299 /* Parse the handlers. */
28300 cp_parser_handler_seq (parser);
28301 /* We're done with the handlers. */
28302 finish_function_handler_sequence (try_block, compound_stmt);
28305 /* Parse a handler-seq.
28307 handler-seq:
28308 handler handler-seq [opt] */
28310 static void
28311 cp_parser_handler_seq (cp_parser* parser)
28313 while (true)
28315 cp_token *token;
28317 /* Parse the handler. */
28318 cp_parser_handler (parser);
28319 /* Peek at the next token. */
28320 token = cp_lexer_peek_token (parser->lexer);
28321 /* If it's not `catch' then there are no more handlers. */
28322 if (!cp_parser_is_keyword (token, RID_CATCH))
28323 break;
28327 /* Parse a handler.
28329 handler:
28330 catch ( exception-declaration ) compound-statement */
28332 static void
28333 cp_parser_handler (cp_parser* parser)
28335 tree handler;
28336 tree declaration;
28338 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28339 handler = begin_handler ();
28340 matching_parens parens;
28341 parens.require_open (parser);
28342 declaration = cp_parser_exception_declaration (parser);
28343 finish_handler_parms (declaration, handler);
28344 parens.require_close (parser);
28345 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28346 finish_handler (handler);
28349 /* Parse an exception-declaration.
28351 exception-declaration:
28352 type-specifier-seq declarator
28353 type-specifier-seq abstract-declarator
28354 type-specifier-seq
28357 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28358 ellipsis variant is used. */
28360 static tree
28361 cp_parser_exception_declaration (cp_parser* parser)
28363 cp_decl_specifier_seq type_specifiers;
28364 cp_declarator *declarator;
28365 const char *saved_message;
28367 /* If it's an ellipsis, it's easy to handle. */
28368 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28370 /* Consume the `...' token. */
28371 cp_lexer_consume_token (parser->lexer);
28372 return NULL_TREE;
28375 /* Types may not be defined in exception-declarations. */
28376 saved_message = parser->type_definition_forbidden_message;
28377 parser->type_definition_forbidden_message
28378 = G_("types may not be defined in exception-declarations");
28380 /* Parse the type-specifier-seq. */
28381 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28382 /*is_declaration=*/true,
28383 /*is_trailing_return=*/false,
28384 &type_specifiers);
28385 /* If it's a `)', then there is no declarator. */
28386 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28387 declarator = NULL;
28388 else
28389 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28390 CP_PARSER_FLAGS_NONE,
28391 /*ctor_dtor_or_conv_p=*/NULL,
28392 /*parenthesized_p=*/NULL,
28393 /*member_p=*/false,
28394 /*friend_p=*/false,
28395 /*static_p=*/false);
28397 /* Restore the saved message. */
28398 parser->type_definition_forbidden_message = saved_message;
28400 if (!type_specifiers.any_specifiers_p)
28401 return error_mark_node;
28403 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28406 /* Parse a throw-expression.
28408 throw-expression:
28409 throw assignment-expression [opt]
28411 Returns a THROW_EXPR representing the throw-expression. */
28413 static tree
28414 cp_parser_throw_expression (cp_parser* parser)
28416 tree expression;
28417 cp_token* token;
28418 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28420 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28421 token = cp_lexer_peek_token (parser->lexer);
28422 /* Figure out whether or not there is an assignment-expression
28423 following the "throw" keyword. */
28424 if (token->type == CPP_COMMA
28425 || token->type == CPP_SEMICOLON
28426 || token->type == CPP_CLOSE_PAREN
28427 || token->type == CPP_CLOSE_SQUARE
28428 || token->type == CPP_CLOSE_BRACE
28429 || token->type == CPP_COLON)
28430 expression = NULL_TREE;
28431 else
28432 expression = cp_parser_assignment_expression (parser);
28434 /* Construct a location e.g.:
28435 throw x
28436 ^~~~~~~
28437 with caret == start at the start of the "throw" token, and
28438 the end at the end of the final token we consumed. */
28439 location_t combined_loc = make_location (start_loc, start_loc,
28440 parser->lexer);
28441 expression = build_throw (combined_loc, expression);
28443 return expression;
28446 /* Parse a yield-expression.
28448 yield-expression:
28449 co_yield assignment-expression
28450 co_yield braced-init-list
28452 Returns a CO_YIELD_EXPR representing the yield-expression. */
28454 static tree
28455 cp_parser_yield_expression (cp_parser* parser)
28457 tree expr;
28459 cp_token *token = cp_lexer_peek_token (parser->lexer);
28460 location_t kw_loc = token->location; /* Save for later. */
28462 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28466 bool expr_non_constant_p;
28467 cp_lexer_set_source_position (parser->lexer);
28468 /* ??? : probably a moot point? */
28469 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28470 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
28472 else
28473 expr = cp_parser_assignment_expression (parser);
28475 if (expr == error_mark_node)
28476 return expr;
28478 return finish_co_yield_expr (kw_loc, expr);
28481 /* GNU Extensions */
28483 /* Parse an (optional) asm-specification.
28485 asm-specification:
28486 asm ( string-literal )
28488 If the asm-specification is present, returns a STRING_CST
28489 corresponding to the string-literal. Otherwise, returns
28490 NULL_TREE. */
28492 static tree
28493 cp_parser_asm_specification_opt (cp_parser* parser)
28495 cp_token *token;
28496 tree asm_specification;
28498 /* Peek at the next token. */
28499 token = cp_lexer_peek_token (parser->lexer);
28500 /* If the next token isn't the `asm' keyword, then there's no
28501 asm-specification. */
28502 if (!cp_parser_is_keyword (token, RID_ASM))
28503 return NULL_TREE;
28505 /* Consume the `asm' token. */
28506 cp_lexer_consume_token (parser->lexer);
28507 /* Look for the `('. */
28508 matching_parens parens;
28509 parens.require_open (parser);
28511 /* Look for the string-literal. */
28512 asm_specification = cp_parser_string_literal (parser, false, false);
28514 /* Look for the `)'. */
28515 parens.require_close (parser);
28517 return asm_specification;
28520 /* Parse an asm-operand-list.
28522 asm-operand-list:
28523 asm-operand
28524 asm-operand-list , asm-operand
28526 asm-operand:
28527 string-literal ( expression )
28528 [ string-literal ] string-literal ( expression )
28530 Returns a TREE_LIST representing the operands. The TREE_VALUE of
28531 each node is the expression. The TREE_PURPOSE is itself a
28532 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28533 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28534 is a STRING_CST for the string literal before the parenthesis. Returns
28535 ERROR_MARK_NODE if any of the operands are invalid. */
28537 static tree
28538 cp_parser_asm_operand_list (cp_parser* parser)
28540 tree asm_operands = NULL_TREE;
28541 bool invalid_operands = false;
28543 while (true)
28545 tree string_literal;
28546 tree expression;
28547 tree name;
28549 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28551 /* Consume the `[' token. */
28552 cp_lexer_consume_token (parser->lexer);
28553 /* Read the operand name. */
28554 name = cp_parser_identifier (parser);
28555 if (name != error_mark_node)
28556 name = build_string (IDENTIFIER_LENGTH (name),
28557 IDENTIFIER_POINTER (name));
28558 /* Look for the closing `]'. */
28559 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28561 else
28562 name = NULL_TREE;
28563 /* Look for the string-literal. */
28564 string_literal = cp_parser_string_literal (parser, false, false);
28566 /* Look for the `('. */
28567 matching_parens parens;
28568 parens.require_open (parser);
28569 /* Parse the expression. */
28570 expression = cp_parser_expression (parser);
28571 /* Look for the `)'. */
28572 parens.require_close (parser);
28574 if (name == error_mark_node
28575 || string_literal == error_mark_node
28576 || expression == error_mark_node)
28577 invalid_operands = true;
28579 /* Add this operand to the list. */
28580 asm_operands = tree_cons (build_tree_list (name, string_literal),
28581 expression,
28582 asm_operands);
28583 /* If the next token is not a `,', there are no more
28584 operands. */
28585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28586 break;
28587 /* Consume the `,'. */
28588 cp_lexer_consume_token (parser->lexer);
28591 return invalid_operands ? error_mark_node : nreverse (asm_operands);
28594 /* Parse an asm-clobber-list.
28596 asm-clobber-list:
28597 string-literal
28598 asm-clobber-list , string-literal
28600 Returns a TREE_LIST, indicating the clobbers in the order that they
28601 appeared. The TREE_VALUE of each node is a STRING_CST. */
28603 static tree
28604 cp_parser_asm_clobber_list (cp_parser* parser)
28606 tree clobbers = NULL_TREE;
28608 while (true)
28610 tree string_literal;
28612 /* Look for the string literal. */
28613 string_literal = cp_parser_string_literal (parser, false, false);
28614 /* Add it to the list. */
28615 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
28616 /* If the next token is not a `,', then the list is
28617 complete. */
28618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28619 break;
28620 /* Consume the `,' token. */
28621 cp_lexer_consume_token (parser->lexer);
28624 return clobbers;
28627 /* Parse an asm-label-list.
28629 asm-label-list:
28630 identifier
28631 asm-label-list , identifier
28633 Returns a TREE_LIST, indicating the labels in the order that they
28634 appeared. The TREE_VALUE of each node is a label. */
28636 static tree
28637 cp_parser_asm_label_list (cp_parser* parser)
28639 tree labels = NULL_TREE;
28641 while (true)
28643 tree identifier, label, name;
28645 /* Look for the identifier. */
28646 identifier = cp_parser_identifier (parser);
28647 if (!error_operand_p (identifier))
28649 label = lookup_label (identifier);
28650 if (TREE_CODE (label) == LABEL_DECL)
28652 TREE_USED (label) = 1;
28653 check_goto (label);
28654 name = build_string (IDENTIFIER_LENGTH (identifier),
28655 IDENTIFIER_POINTER (identifier));
28656 labels = tree_cons (name, label, labels);
28659 /* If the next token is not a `,', then the list is
28660 complete. */
28661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28662 break;
28663 /* Consume the `,' token. */
28664 cp_lexer_consume_token (parser->lexer);
28667 return nreverse (labels);
28670 /* Return TRUE iff the next tokens in the stream are possibly the
28671 beginning of a GNU extension attribute. */
28673 static bool
28674 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
28676 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
28679 /* Return TRUE iff the next tokens in the stream are possibly the
28680 beginning of a standard C++-11 attribute specifier. */
28682 static bool
28683 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
28685 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
28688 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28689 beginning of a standard C++-11 attribute specifier. */
28691 static bool
28692 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
28694 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28696 return (cxx_dialect >= cxx11
28697 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
28698 || (token->type == CPP_OPEN_SQUARE
28699 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
28700 && token->type == CPP_OPEN_SQUARE)));
28703 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28704 beginning of a GNU extension attribute. */
28706 static bool
28707 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
28709 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28711 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
28714 /* Return true iff the next tokens can be the beginning of either a
28715 GNU attribute list, or a standard C++11 attribute sequence. */
28717 static bool
28718 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
28720 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
28721 || cp_next_tokens_can_be_std_attribute_p (parser));
28724 /* Return true iff the next Nth tokens can be the beginning of either
28725 a GNU attribute list, or a standard C++11 attribute sequence. */
28727 static bool
28728 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
28730 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
28731 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
28734 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
28735 of GNU attributes, or return NULL. */
28737 static tree
28738 cp_parser_attributes_opt (cp_parser *parser)
28740 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
28741 return cp_parser_gnu_attributes_opt (parser);
28742 return cp_parser_std_attribute_spec_seq (parser);
28745 /* Parse an (optional) series of attributes.
28747 attributes:
28748 attributes attribute
28750 attribute:
28751 __attribute__ (( attribute-list [opt] ))
28753 The return value is as for cp_parser_gnu_attribute_list. */
28755 static tree
28756 cp_parser_gnu_attributes_opt (cp_parser* parser)
28758 tree attributes = NULL_TREE;
28760 auto cleanup = make_temp_override
28761 (parser->auto_is_implicit_function_template_parm_p, false);
28763 while (true)
28765 cp_token *token;
28766 tree attribute_list;
28767 bool ok = true;
28769 /* Peek at the next token. */
28770 token = cp_lexer_peek_token (parser->lexer);
28771 /* If it's not `__attribute__', then we're done. */
28772 if (token->keyword != RID_ATTRIBUTE)
28773 break;
28775 /* Consume the `__attribute__' keyword. */
28776 cp_lexer_consume_token (parser->lexer);
28777 /* Look for the two `(' tokens. */
28778 matching_parens outer_parens;
28779 if (!outer_parens.require_open (parser))
28780 ok = false;
28781 matching_parens inner_parens;
28782 if (!inner_parens.require_open (parser))
28783 ok = false;
28785 /* Peek at the next token. */
28786 token = cp_lexer_peek_token (parser->lexer);
28787 if (token->type != CPP_CLOSE_PAREN)
28788 /* Parse the attribute-list. */
28789 attribute_list = cp_parser_gnu_attribute_list (parser);
28790 else
28791 /* If the next token is a `)', then there is no attribute
28792 list. */
28793 attribute_list = NULL;
28795 /* Look for the two `)' tokens. */
28796 if (!inner_parens.require_close (parser))
28797 ok = false;
28798 if (!outer_parens.require_close (parser))
28799 ok = false;
28800 if (!ok)
28801 cp_parser_skip_to_end_of_statement (parser);
28803 /* Add these new attributes to the list. */
28804 attributes = attr_chainon (attributes, attribute_list);
28807 return attributes;
28810 /* Parse a GNU attribute-list.
28812 attribute-list:
28813 attribute
28814 attribute-list , attribute
28816 attribute:
28817 identifier
28818 identifier ( identifier )
28819 identifier ( identifier , expression-list )
28820 identifier ( expression-list )
28822 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
28823 to an attribute. The TREE_PURPOSE of each node is the identifier
28824 indicating which attribute is in use. The TREE_VALUE represents
28825 the arguments, if any. */
28827 static tree
28828 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
28830 tree attribute_list = NULL_TREE;
28831 bool save_translate_strings_p = parser->translate_strings_p;
28833 /* Don't create wrapper nodes within attributes: the
28834 handlers don't know how to handle them. */
28835 auto_suppress_location_wrappers sentinel;
28837 parser->translate_strings_p = false;
28838 while (true)
28840 cp_token *token;
28841 tree identifier;
28842 tree attribute;
28844 /* Look for the identifier. We also allow keywords here; for
28845 example `__attribute__ ((const))' is legal. */
28846 token = cp_lexer_peek_token (parser->lexer);
28847 if (token->type == CPP_NAME
28848 || token->type == CPP_KEYWORD)
28850 tree arguments = NULL_TREE;
28852 /* Consume the token, but save it since we need it for the
28853 SIMD enabled function parsing. */
28854 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
28856 /* Save away the identifier that indicates which attribute
28857 this is. */
28858 identifier = (token->type == CPP_KEYWORD)
28859 /* For keywords, use the canonical spelling, not the
28860 parsed identifier. */
28861 ? ridpointers[(int) token->keyword]
28862 : id_token->u.value;
28864 identifier = canonicalize_attr_name (identifier);
28865 attribute = build_tree_list (identifier, NULL_TREE);
28867 /* Peek at the next token. */
28868 token = cp_lexer_peek_token (parser->lexer);
28869 /* If it's an `(', then parse the attribute arguments. */
28870 if (token->type == CPP_OPEN_PAREN)
28872 vec<tree, va_gc> *vec;
28873 int attr_flag = (attribute_takes_identifier_p (identifier)
28874 ? id_attr : normal_attr);
28875 vec = cp_parser_parenthesized_expression_list
28876 (parser, attr_flag, /*cast_p=*/false,
28877 /*allow_expansion_p=*/false,
28878 /*non_constant_p=*/NULL);
28879 if (vec == NULL)
28880 arguments = error_mark_node;
28881 else
28883 arguments = build_tree_list_vec (vec);
28884 release_tree_vector (vec);
28886 /* Save the arguments away. */
28887 TREE_VALUE (attribute) = arguments;
28890 if (arguments != error_mark_node)
28892 /* Add this attribute to the list. */
28893 TREE_CHAIN (attribute) = attribute_list;
28894 attribute_list = attribute;
28897 token = cp_lexer_peek_token (parser->lexer);
28899 /* Unless EXACTLY_ONE is set look for more attributes.
28900 If the next token isn't a `,', we're done. */
28901 if (exactly_one || token->type != CPP_COMMA)
28902 break;
28904 /* Consume the comma and keep going. */
28905 cp_lexer_consume_token (parser->lexer);
28907 parser->translate_strings_p = save_translate_strings_p;
28909 /* We built up the list in reverse order. */
28910 return nreverse (attribute_list);
28913 /* Parse arguments of omp::directive attribute.
28915 ( directive-name ,[opt] clause-list[opt] )
28917 For directive just remember the first/last tokens for subsequent
28918 parsing. */
28920 static void
28921 cp_parser_omp_directive_args (cp_parser *parser, tree attribute)
28923 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
28924 if (first->type == CPP_CLOSE_PAREN)
28926 cp_lexer_consume_token (parser->lexer);
28927 error_at (first->location, "expected OpenMP directive name");
28928 cp_lexer_consume_token (parser->lexer);
28929 TREE_VALUE (attribute) = NULL_TREE;
28930 return;
28932 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
28933 if (n == 1)
28935 cp_lexer_consume_token (parser->lexer);
28936 error_at (first->location, "expected attribute argument as balanced "
28937 "token sequence");
28938 TREE_VALUE (attribute) = NULL_TREE;
28939 return;
28941 for (n = n - 2; n; --n)
28942 cp_lexer_consume_token (parser->lexer);
28943 cp_token *last = cp_lexer_peek_token (parser->lexer);
28944 cp_lexer_consume_token (parser->lexer);
28945 tree arg = make_node (DEFERRED_PARSE);
28946 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
28947 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
28948 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
28951 /* Parse arguments of omp::sequence attribute.
28953 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
28955 static void
28956 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
28958 matching_parens parens;
28959 parens.consume_open (parser);
28962 cp_token *token = cp_lexer_peek_token (parser->lexer);
28963 if (token->type == CPP_NAME
28964 && token->u.value == omp_identifier
28965 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
28967 cp_lexer_consume_token (parser->lexer);
28968 cp_lexer_consume_token (parser->lexer);
28969 token = cp_lexer_peek_token (parser->lexer);
28971 bool directive = false;
28972 const char *p;
28973 if (token->type != CPP_NAME)
28974 p = "";
28975 else
28976 p = IDENTIFIER_POINTER (token->u.value);
28977 if (strcmp (p, "directive") == 0)
28978 directive = true;
28979 else if (strcmp (p, "sequence") != 0)
28981 error_at (token->location, "expected %<directive%> or %<sequence%>");
28982 cp_parser_skip_to_closing_parenthesis (parser,
28983 /*recovering=*/true,
28984 /*or_comma=*/true,
28985 /*consume_paren=*/false);
28986 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28987 break;
28988 cp_lexer_consume_token (parser->lexer);
28990 cp_lexer_consume_token (parser->lexer);
28991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
28992 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
28993 UNKNOWN_LOCATION);
28994 else if (directive)
28995 cp_parser_omp_directive_args (parser, attribute);
28996 else
28997 cp_parser_omp_sequence_args (parser, attribute);
28998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28999 break;
29000 cp_lexer_consume_token (parser->lexer);
29002 while (1);
29003 if (!parens.require_close (parser))
29004 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29005 /*consume_paren=*/true);
29008 /* Parse a standard C++11 attribute.
29010 The returned representation is a TREE_LIST which TREE_PURPOSE is
29011 the scoped name of the attribute, and the TREE_VALUE is its
29012 arguments list.
29014 Note that the scoped name of the attribute is itself a TREE_LIST
29015 which TREE_PURPOSE is the namespace of the attribute, and
29016 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29017 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29018 and which TREE_PURPOSE is directly the attribute name.
29020 Clients of the attribute code should use get_attribute_namespace
29021 and get_attribute_name to get the actual namespace and name of
29022 attributes, regardless of their being GNU or C++11 attributes.
29024 attribute:
29025 attribute-token attribute-argument-clause [opt]
29027 attribute-token:
29028 identifier
29029 attribute-scoped-token
29031 attribute-scoped-token:
29032 attribute-namespace :: identifier
29034 attribute-namespace:
29035 identifier
29037 attribute-argument-clause:
29038 ( balanced-token-seq )
29040 balanced-token-seq:
29041 balanced-token [opt]
29042 balanced-token-seq balanced-token
29044 balanced-token:
29045 ( balanced-token-seq )
29046 [ balanced-token-seq ]
29047 { balanced-token-seq }. */
29049 static tree
29050 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
29052 tree attribute, attr_id = NULL_TREE, arguments;
29053 cp_token *token;
29055 auto cleanup = make_temp_override
29056 (parser->auto_is_implicit_function_template_parm_p, false);
29058 /* First, parse name of the attribute, a.k.a attribute-token. */
29060 token = cp_lexer_peek_token (parser->lexer);
29061 if (token->type == CPP_NAME)
29062 attr_id = token->u.value;
29063 else if (token->type == CPP_KEYWORD)
29064 attr_id = ridpointers[(int) token->keyword];
29065 else if (token->flags & NAMED_OP)
29066 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29068 if (attr_id == NULL_TREE)
29069 return NULL_TREE;
29071 cp_lexer_consume_token (parser->lexer);
29073 token = cp_lexer_peek_token (parser->lexer);
29074 if (token->type == CPP_SCOPE)
29076 /* We are seeing a scoped attribute token. */
29078 cp_lexer_consume_token (parser->lexer);
29079 if (attr_ns)
29080 error_at (token->location, "attribute using prefix used together "
29081 "with scoped attribute token");
29082 attr_ns = attr_id;
29084 token = cp_lexer_peek_token (parser->lexer);
29085 if (token->type == CPP_NAME)
29086 attr_id = token->u.value;
29087 else if (token->type == CPP_KEYWORD)
29088 attr_id = ridpointers[(int) token->keyword];
29089 else if (token->flags & NAMED_OP)
29090 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29091 else
29093 error_at (token->location,
29094 "expected an identifier for the attribute name");
29095 return error_mark_node;
29097 cp_lexer_consume_token (parser->lexer);
29099 attr_ns = canonicalize_attr_name (attr_ns);
29100 attr_id = canonicalize_attr_name (attr_id);
29101 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29102 NULL_TREE);
29103 token = cp_lexer_peek_token (parser->lexer);
29105 else if (attr_ns)
29107 attr_ns = canonicalize_attr_name (attr_ns);
29108 attr_id = canonicalize_attr_name (attr_id);
29109 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29110 NULL_TREE);
29112 else
29114 attr_id = canonicalize_attr_name (attr_id);
29115 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29116 NULL_TREE);
29117 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29118 but no longer: we have to be able to tell [[noreturn]] and
29119 __attribute__((noreturn)) apart. */
29120 /* C++14 deprecated attribute is equivalent to GNU's. */
29121 if (is_attribute_p ("deprecated", attr_id))
29122 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29123 /* C++17 fallthrough attribute is equivalent to GNU's. */
29124 else if (is_attribute_p ("fallthrough", attr_id))
29125 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29126 /* Transactional Memory TS optimize_for_synchronized attribute is
29127 equivalent to GNU transaction_callable. */
29128 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29129 TREE_PURPOSE (attribute)
29130 = get_identifier ("transaction_callable");
29131 /* Transactional Memory attributes are GNU attributes. */
29132 else if (tm_attr_to_mask (attr_id))
29133 TREE_PURPOSE (attribute) = attr_id;
29136 /* Now parse the optional argument clause of the attribute. */
29138 if (token->type != CPP_OPEN_PAREN)
29140 if ((flag_openmp || flag_openmp_simd)
29141 && attr_ns == omp_identifier
29142 && (is_attribute_p ("directive", attr_id)
29143 || is_attribute_p ("sequence", attr_id)))
29145 error_at (token->location, "%<omp::%E%> attribute requires argument",
29146 attr_id);
29147 return NULL_TREE;
29149 return attribute;
29153 vec<tree, va_gc> *vec;
29154 int attr_flag = normal_attr;
29156 /* Maybe we don't expect to see any arguments for this attribute. */
29157 const attribute_spec *as
29158 = lookup_attribute_spec (TREE_PURPOSE (attribute));
29159 if (as && as->max_length == 0)
29161 error_at (token->location, "%qE attribute does not take any arguments",
29162 attr_id);
29163 cp_parser_skip_to_closing_parenthesis (parser,
29164 /*recovering=*/true,
29165 /*or_comma=*/false,
29166 /*consume_paren=*/true);
29167 return error_mark_node;
29170 if (attr_ns == gnu_identifier
29171 && attribute_takes_identifier_p (attr_id))
29172 /* A GNU attribute that takes an identifier in parameter. */
29173 attr_flag = id_attr;
29175 /* If this is a fake attribute created to handle -Wno-attributes,
29176 we must skip parsing the arguments. */
29177 if (as == NULL || attribute_ignored_p (as))
29179 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
29181 if (is_attribute_p ("directive", attr_id))
29183 cp_parser_omp_directive_args (parser, attribute);
29184 return attribute;
29186 else if (is_attribute_p ("sequence", attr_id))
29188 TREE_VALUE (TREE_PURPOSE (attribute))
29189 = get_identifier ("directive");
29190 cp_parser_omp_sequence_args (parser, attribute);
29191 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
29192 return attribute;
29196 /* For unknown attributes, just skip balanced tokens instead of
29197 trying to parse the arguments. */
29198 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29199 cp_lexer_consume_token (parser->lexer);
29200 return attribute;
29203 vec = cp_parser_parenthesized_expression_list
29204 (parser, attr_flag, /*cast_p=*/false,
29205 /*allow_expansion_p=*/true,
29206 /*non_constant_p=*/NULL);
29207 if (vec == NULL)
29208 arguments = error_mark_node;
29209 else
29211 if (vec->is_empty ())
29212 /* e.g. [[attr()]]. */
29213 error_at (token->location, "parentheses must be omitted if "
29214 "%qE attribute argument list is empty",
29215 attr_id);
29216 arguments = build_tree_list_vec (vec);
29217 release_tree_vector (vec);
29220 if (arguments == error_mark_node)
29221 attribute = error_mark_node;
29222 else
29223 TREE_VALUE (attribute) = arguments;
29226 return attribute;
29229 /* Warn if the attribute ATTRIBUTE appears more than once in the
29230 attribute-list ATTRIBUTES. This used to be enforced for certain
29231 attributes, but the restriction was removed in P2156. Note that
29232 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
29233 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29234 found in ATTRIBUTES. */
29236 static bool
29237 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29239 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29240 "likely", "unlikely", "fallthrough",
29241 "no_unique_address" };
29242 if (attributes)
29243 for (const auto &a : alist)
29244 if (is_attribute_p (a, get_attribute_name (attribute))
29245 && lookup_attribute (a, attributes))
29247 if (!from_macro_expansion_at (loc))
29248 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29249 "multiple times", a);
29250 return false;
29252 return true;
29255 /* Parse a list of standard C++-11 attributes.
29257 attribute-list:
29258 attribute [opt]
29259 attribute-list , attribute[opt]
29260 attribute ...
29261 attribute-list , attribute ...
29264 static tree
29265 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29267 tree attributes = NULL_TREE, attribute = NULL_TREE;
29268 cp_token *token = NULL;
29270 while (true)
29272 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29273 attribute = cp_parser_std_attribute (parser, attr_ns);
29274 if (attribute == error_mark_node)
29275 break;
29276 if (attribute != NULL_TREE)
29278 if (cp_parser_check_std_attribute (loc, attributes, attribute))
29280 TREE_CHAIN (attribute) = attributes;
29281 attributes = attribute;
29284 token = cp_lexer_peek_token (parser->lexer);
29285 if (token->type == CPP_ELLIPSIS)
29287 cp_lexer_consume_token (parser->lexer);
29288 if (attribute == NULL_TREE)
29289 error_at (token->location,
29290 "expected attribute before %<...%>");
29291 else
29293 tree pack = make_pack_expansion (TREE_VALUE (attribute));
29294 if (pack == error_mark_node)
29295 return error_mark_node;
29296 TREE_VALUE (attribute) = pack;
29298 token = cp_lexer_peek_token (parser->lexer);
29300 if (token->type != CPP_COMMA)
29301 break;
29302 cp_lexer_consume_token (parser->lexer);
29304 attributes = nreverse (attributes);
29305 return attributes;
29308 /* Parse a standard C++-11 attribute specifier.
29310 attribute-specifier:
29311 [ [ attribute-using-prefix [opt] attribute-list ] ]
29312 alignment-specifier
29314 attribute-using-prefix:
29315 using attribute-namespace :
29317 alignment-specifier:
29318 alignas ( type-id ... [opt] )
29319 alignas ( alignment-expression ... [opt] ). */
29321 static tree
29322 cp_parser_std_attribute_spec (cp_parser *parser)
29324 tree attributes = NULL_TREE;
29325 cp_token *token = cp_lexer_peek_token (parser->lexer);
29327 if (token->type == CPP_OPEN_SQUARE
29328 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
29330 tree attr_ns = NULL_TREE;
29332 cp_lexer_consume_token (parser->lexer);
29333 cp_lexer_consume_token (parser->lexer);
29335 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29337 token = cp_lexer_peek_nth_token (parser->lexer, 2);
29338 if (token->type == CPP_NAME)
29339 attr_ns = token->u.value;
29340 else if (token->type == CPP_KEYWORD)
29341 attr_ns = ridpointers[(int) token->keyword];
29342 else if (token->flags & NAMED_OP)
29343 attr_ns = get_identifier (cpp_type2name (token->type,
29344 token->flags));
29345 if (attr_ns
29346 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
29348 if (cxx_dialect < cxx17)
29349 pedwarn (input_location, OPT_Wc__17_extensions,
29350 "attribute using prefix only available "
29351 "with %<-std=c++17%> or %<-std=gnu++17%>");
29353 cp_lexer_consume_token (parser->lexer);
29354 cp_lexer_consume_token (parser->lexer);
29355 cp_lexer_consume_token (parser->lexer);
29357 else
29358 attr_ns = NULL_TREE;
29361 attributes = cp_parser_std_attribute_list (parser, attr_ns);
29363 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
29364 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
29365 cp_parser_skip_to_end_of_statement (parser);
29366 else
29367 /* Warn about parsing c++11 attribute in non-c++11 mode, only
29368 when we are sure that we have actually parsed them. */
29369 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29371 else
29373 tree alignas_expr;
29375 /* Look for an alignment-specifier. */
29377 token = cp_lexer_peek_token (parser->lexer);
29379 if (token->type != CPP_KEYWORD
29380 || token->keyword != RID_ALIGNAS)
29381 return NULL_TREE;
29383 cp_lexer_consume_token (parser->lexer);
29384 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29386 matching_parens parens;
29387 if (!parens.require_open (parser))
29388 return error_mark_node;
29390 cp_parser_parse_tentatively (parser);
29391 alignas_expr = cp_parser_type_id (parser);
29393 if (!cp_parser_parse_definitely (parser))
29395 alignas_expr = cp_parser_assignment_expression (parser);
29396 if (alignas_expr == error_mark_node)
29397 cp_parser_skip_to_end_of_statement (parser);
29398 if (alignas_expr == NULL_TREE
29399 || alignas_expr == error_mark_node)
29400 return alignas_expr;
29403 alignas_expr = cxx_alignas_expr (alignas_expr);
29404 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
29406 /* Handle alignas (pack...). */
29407 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29409 cp_lexer_consume_token (parser->lexer);
29410 alignas_expr = make_pack_expansion (alignas_expr);
29413 /* Something went wrong, so don't build the attribute. */
29414 if (alignas_expr == error_mark_node)
29415 return error_mark_node;
29417 /* Missing ')' means the code cannot possibly be valid; go ahead
29418 and commit to make sure we issue a hard error. */
29419 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
29420 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29421 cp_parser_commit_to_tentative_parse (parser);
29423 if (!parens.require_close (parser))
29424 return error_mark_node;
29426 /* Build the C++-11 representation of an 'aligned'
29427 attribute. */
29428 attributes
29429 = build_tree_list (build_tree_list (gnu_identifier,
29430 aligned_identifier), alignas_expr);
29433 return attributes;
29436 /* Parse a standard C++-11 attribute-specifier-seq.
29438 attribute-specifier-seq:
29439 attribute-specifier-seq [opt] attribute-specifier
29442 static tree
29443 cp_parser_std_attribute_spec_seq (cp_parser *parser)
29445 tree attr_specs = NULL_TREE;
29446 tree attr_last = NULL_TREE;
29448 /* Don't create wrapper nodes within attributes: the
29449 handlers don't know how to handle them. */
29450 auto_suppress_location_wrappers sentinel;
29452 while (true)
29454 tree attr_spec = cp_parser_std_attribute_spec (parser);
29455 if (attr_spec == NULL_TREE)
29456 break;
29457 if (attr_spec == error_mark_node)
29458 return error_mark_node;
29460 if (attr_last)
29461 TREE_CHAIN (attr_last) = attr_spec;
29462 else
29463 attr_specs = attr_last = attr_spec;
29464 attr_last = tree_last (attr_last);
29467 return attr_specs;
29470 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
29471 return index of the first token after balanced-token, or N on failure. */
29473 static size_t
29474 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
29476 size_t orig_n = n;
29477 int nparens = 0, nbraces = 0, nsquares = 0;
29479 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
29481 case CPP_PRAGMA_EOL:
29482 if (!parser->lexer->in_pragma)
29483 break;
29484 /* FALLTHRU */
29485 case CPP_EOF:
29486 /* Ran out of tokens. */
29487 return orig_n;
29488 case CPP_OPEN_PAREN:
29489 ++nparens;
29490 break;
29491 case CPP_OPEN_BRACE:
29492 ++nbraces;
29493 break;
29494 case CPP_OPEN_SQUARE:
29495 ++nsquares;
29496 break;
29497 case CPP_CLOSE_PAREN:
29498 --nparens;
29499 break;
29500 case CPP_CLOSE_BRACE:
29501 --nbraces;
29502 break;
29503 case CPP_CLOSE_SQUARE:
29504 --nsquares;
29505 break;
29506 default:
29507 break;
29509 while (nparens || nbraces || nsquares);
29510 return n;
29513 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
29514 return index of the first token after the GNU attribute tokens, or N on
29515 failure. */
29517 static size_t
29518 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
29520 while (true)
29522 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
29523 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
29524 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
29525 break;
29527 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
29528 if (n2 == n + 2)
29529 break;
29530 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
29531 break;
29532 n = n2 + 1;
29534 return n;
29537 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
29538 next token), return index of the first token after the standard C++11
29539 attribute tokens, or N on failure. */
29541 static size_t
29542 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
29544 while (true)
29546 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
29547 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
29549 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29550 if (n2 == n + 1)
29551 break;
29552 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
29553 break;
29554 n = n2 + 1;
29556 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
29557 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
29559 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29560 if (n2 == n + 1)
29561 break;
29562 n = n2;
29564 else
29565 break;
29567 return n;
29570 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
29571 as the next token), return index of the first token after the attribute
29572 tokens, or N on failure. */
29574 static size_t
29575 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
29577 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
29578 return cp_parser_skip_gnu_attributes_opt (parser, n);
29579 return cp_parser_skip_std_attribute_spec_seq (parser, n);
29582 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
29583 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
29584 current value of the PEDANTIC flag, regardless of whether or not
29585 the `__extension__' keyword is present. The caller is responsible
29586 for restoring the value of the PEDANTIC flag. */
29588 static bool
29589 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
29591 /* Save the old value of the PEDANTIC flag. */
29592 *saved_pedantic = pedantic;
29594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
29596 /* Consume the `__extension__' token. */
29597 cp_lexer_consume_token (parser->lexer);
29598 /* We're not being pedantic while the `__extension__' keyword is
29599 in effect. */
29600 pedantic = 0;
29602 return true;
29605 return false;
29608 /* Parse a label declaration.
29610 label-declaration:
29611 __label__ label-declarator-seq ;
29613 label-declarator-seq:
29614 identifier , label-declarator-seq
29615 identifier */
29617 static void
29618 cp_parser_label_declaration (cp_parser* parser)
29620 /* Look for the `__label__' keyword. */
29621 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
29623 while (true)
29625 tree identifier;
29627 /* Look for an identifier. */
29628 identifier = cp_parser_identifier (parser);
29629 /* If we failed, stop. */
29630 if (identifier == error_mark_node)
29631 break;
29632 /* Declare it as a label. */
29633 finish_label_decl (identifier);
29634 /* If the next token is a `;', stop. */
29635 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29636 break;
29637 /* Look for the `,' separating the label declarations. */
29638 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
29641 /* Look for the final `;'. */
29642 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29645 // -------------------------------------------------------------------------- //
29646 // Concept definitions
29648 static tree
29649 cp_parser_concept_definition (cp_parser *parser)
29651 /* A concept definition is an unevaluated context. */
29652 cp_unevaluated u;
29654 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
29655 cp_lexer_consume_token (parser->lexer);
29657 cp_expr id = cp_parser_identifier (parser);
29658 if (id == error_mark_node)
29660 cp_parser_skip_to_end_of_statement (parser);
29661 cp_parser_consume_semicolon_at_end_of_statement (parser);
29662 return NULL_TREE;
29665 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29667 cp_parser_skip_to_end_of_statement (parser);
29668 cp_parser_consume_semicolon_at_end_of_statement (parser);
29669 return error_mark_node;
29672 processing_constraint_expression_sentinel parsing_constraint;
29673 tree init = cp_parser_constraint_expression (parser);
29674 if (init == error_mark_node)
29675 cp_parser_skip_to_end_of_statement (parser);
29677 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
29678 but continue as if it were. */
29679 cp_parser_consume_semicolon_at_end_of_statement (parser);
29681 return finish_concept_definition (id, init);
29684 // -------------------------------------------------------------------------- //
29685 // Requires Clause
29687 /* Diagnose an expression that should appear in ()'s within a requires-clause
29688 and suggest where to place those parentheses. */
29690 static void
29691 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
29693 error_at (loc, "expression must be enclosed in parentheses");
29696 static void
29697 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
29699 gcc_rich_location richloc (loc);
29700 richloc.add_fixit_insert_before ("(");
29701 richloc.add_fixit_insert_after (")");
29702 error_at (&richloc, "expression must be enclosed in parentheses");
29705 /* Characterizes the likely kind of expression intended by a mis-written
29706 primary constraint. */
29707 enum primary_constraint_error
29709 pce_ok,
29710 pce_maybe_operator,
29711 pce_maybe_postfix
29714 /* Returns true if the token(s) following a primary-expression in a
29715 constraint-logical-* expression would require parentheses. */
29717 static primary_constraint_error
29718 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
29720 cp_token *token = cp_lexer_peek_token (parser->lexer);
29721 switch (token->type)
29723 default:
29724 return pce_ok;
29726 case CPP_EQ:
29728 /* An equal sign may be part of the definition of a function,
29729 and not an assignment operator, when parsing the expression
29730 for a trailing requires-clause. For example:
29732 template<typename T>
29733 struct S {
29734 S() requires C<T> = default;
29737 Don't try to reparse this a binary operator. */
29738 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
29739 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
29740 return pce_ok;
29742 gcc_fallthrough ();
29745 /* Arithmetic operators. */
29746 case CPP_PLUS:
29747 case CPP_MINUS:
29748 case CPP_MULT:
29749 case CPP_DIV:
29750 case CPP_MOD:
29751 /* Bitwise operators. */
29752 case CPP_AND:
29753 case CPP_OR:
29754 case CPP_XOR:
29755 case CPP_RSHIFT:
29756 case CPP_LSHIFT:
29757 /* Relational operators. */
29758 case CPP_EQ_EQ:
29759 case CPP_NOT_EQ:
29760 case CPP_LESS:
29761 case CPP_GREATER:
29762 case CPP_LESS_EQ:
29763 case CPP_GREATER_EQ:
29764 case CPP_SPACESHIP:
29765 /* Pointer-to-member. */
29766 case CPP_DOT_STAR:
29767 case CPP_DEREF_STAR:
29768 /* Assignment operators. */
29769 case CPP_PLUS_EQ:
29770 case CPP_MINUS_EQ:
29771 case CPP_MULT_EQ:
29772 case CPP_DIV_EQ:
29773 case CPP_MOD_EQ:
29774 case CPP_AND_EQ:
29775 case CPP_OR_EQ:
29776 case CPP_XOR_EQ:
29777 case CPP_RSHIFT_EQ:
29778 case CPP_LSHIFT_EQ:
29779 /* Conditional operator */
29780 case CPP_QUERY:
29781 /* Unenclosed binary or conditional operator. */
29782 return pce_maybe_operator;
29784 case CPP_OPEN_PAREN:
29786 /* A primary constraint that precedes the parameter-list of a
29787 lambda expression is followed by an open paren.
29789 []<typename T> requires C (T a, T b) { ... }
29791 Don't try to re-parse this as a postfix expression. */
29792 if (lambda_p)
29793 return pce_ok;
29795 gcc_fallthrough ();
29797 case CPP_OPEN_SQUARE:
29799 /* A primary-constraint-expression followed by a '[[' is not a
29800 postfix expression. */
29801 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
29802 return pce_ok;
29804 gcc_fallthrough ();
29806 case CPP_PLUS_PLUS:
29807 case CPP_MINUS_MINUS:
29808 case CPP_DOT:
29809 /* Unenclosed postfix operator. */
29810 return pce_maybe_postfix;
29812 case CPP_DEREF:
29813 /* A primary constraint that precedes the lambda-declarator of a
29814 lambda expression is followed by trailing return type.
29816 []<typename T> requires C -> void {}
29818 Don't try to re-parse this as a postfix expression in
29819 C++23 and later. In C++20 ( needs to come in between but we
29820 allow it to be omitted with pedwarn. */
29821 if (lambda_p)
29822 return pce_ok;
29823 /* Unenclosed postfix operator. */
29824 return pce_maybe_postfix;
29828 /* Returns true if the next token begins a unary expression, preceded by
29829 an operator or keyword. */
29831 static bool
29832 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
29834 cp_token *token = cp_lexer_peek_token (parser->lexer);
29835 switch (token->type)
29837 case CPP_NOT:
29838 case CPP_PLUS:
29839 case CPP_MINUS:
29840 case CPP_MULT:
29841 case CPP_COMPL:
29842 case CPP_PLUS_PLUS:
29843 case CPP_MINUS_MINUS:
29844 return true;
29846 case CPP_KEYWORD:
29848 switch (token->keyword)
29850 case RID_STATCAST:
29851 case RID_DYNCAST:
29852 case RID_REINTCAST:
29853 case RID_CONSTCAST:
29854 case RID_TYPEID:
29855 case RID_SIZEOF:
29856 case RID_ALIGNOF:
29857 case RID_NOEXCEPT:
29858 case RID_NEW:
29859 case RID_DELETE:
29860 case RID_THROW:
29861 return true;
29863 default:
29864 break;
29868 default:
29869 break;
29872 return false;
29875 /* Parse a primary expression within a constraint. */
29877 static cp_expr
29878 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
29880 /* If this looks like a unary expression, parse it as such, but diagnose
29881 it as ill-formed; it requires parens. */
29882 if (cp_parser_unary_constraint_requires_parens (parser))
29884 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
29885 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
29886 return e;
29889 cp_lexer_save_tokens (parser->lexer);
29890 cp_id_kind idk;
29891 location_t loc = input_location;
29892 cp_expr expr = cp_parser_primary_expression (parser,
29893 /*address_p=*/false,
29894 /*cast_p=*/false,
29895 /*template_arg_p=*/false,
29896 &idk);
29897 expr.maybe_add_location_wrapper ();
29899 primary_constraint_error pce = pce_ok;
29900 if (expr != error_mark_node)
29902 /* The primary-expression could be part of an unenclosed non-logical
29903 compound expression. */
29904 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
29906 if (pce == pce_ok)
29908 cp_lexer_commit_tokens (parser->lexer);
29909 return finish_constraint_primary_expr (expr);
29912 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
29913 error, but return the expression as if it were valid. */
29914 cp_lexer_rollback_tokens (parser->lexer);
29915 cp_parser_parse_tentatively (parser);
29916 if (pce == pce_maybe_operator)
29917 expr = cp_parser_assignment_expression (parser, NULL, false, false);
29918 else
29919 expr = cp_parser_simple_cast_expression (parser);
29920 if (cp_parser_parse_definitely (parser))
29922 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
29923 return expr;
29926 /* Otherwise, something has gone very wrong, and we can't generate a more
29927 meaningful diagnostic or recover. */
29928 cp_parser_diagnose_ungrouped_constraint_plain (loc);
29929 return error_mark_node;
29932 /* Parse a constraint-logical-and-expression.
29934 constraint-logical-and-expression:
29935 primary-expression
29936 constraint-logical-and-expression '&&' primary-expression */
29938 static cp_expr
29939 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
29941 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29942 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
29944 cp_token *op = cp_lexer_consume_token (parser->lexer);
29945 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29946 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
29948 return lhs;
29951 /* Parse a constraint-logical-or-expression.
29953 constraint-logical-or-expression:
29954 constraint-logical-and-expression
29955 constraint-logical-or-expression '||' constraint-logical-and-expression */
29957 static cp_expr
29958 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
29960 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29961 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
29963 cp_token *op = cp_lexer_consume_token (parser->lexer);
29964 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29965 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
29967 return lhs;
29970 /* Parse the expression after a requires-clause. This has a different grammar
29971 than that in the concepts TS. */
29973 static tree
29974 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
29976 processing_constraint_expression_sentinel parsing_constraint;
29977 ++processing_template_decl;
29978 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
29979 --processing_template_decl;
29980 if (check_for_bare_parameter_packs (expr))
29981 expr = error_mark_node;
29982 return expr;
29985 /* Parse a expression after a requires clause.
29987 constraint-expression:
29988 logical-or-expression
29990 The required logical-or-expression must be a constant expression. Note
29991 that we don't check that the expression is constepxr here. We defer until
29992 we analyze constraints and then, we only check atomic constraints. */
29994 static tree
29995 cp_parser_constraint_expression (cp_parser *parser)
29997 processing_constraint_expression_sentinel parsing_constraint;
29998 ++processing_template_decl;
29999 cp_expr expr = cp_parser_binary_expression (parser, false, true,
30000 PREC_NOT_OPERATOR, NULL);
30001 --processing_template_decl;
30002 if (check_for_bare_parameter_packs (expr))
30003 expr = error_mark_node;
30004 expr.maybe_add_location_wrapper ();
30005 return expr;
30008 /* Optionally parse a requires clause:
30010 requires-clause:
30011 `requires` constraint-logical-or-expression.
30012 [ConceptsTS]
30013 `requires constraint-expression.
30015 LAMBDA_P is true when the requires-clause is parsed before the
30016 parameter-list of a lambda-declarator. */
30018 static tree
30019 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
30021 /* A requires clause is an unevaluated context. */
30022 cp_unevaluated u;
30024 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30025 if (tok->keyword != RID_REQUIRES)
30027 if (!flag_concepts && tok->type == CPP_NAME
30028 && tok->u.value == ridpointers[RID_REQUIRES])
30030 error_at (cp_lexer_peek_token (parser->lexer)->location,
30031 "%<requires%> only available with "
30032 "%<-std=c++20%> or %<-fconcepts%>");
30033 /* Parse and discard the requires-clause. */
30034 cp_lexer_consume_token (parser->lexer);
30035 cp_parser_constraint_expression (parser);
30037 return NULL_TREE;
30040 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
30041 if (tok2->type == CPP_OPEN_BRACE)
30043 /* An opening brace following the start of a requires-clause is
30044 ill-formed; the user likely forgot the second `requires' that
30045 would start a requires-expression. */
30046 gcc_rich_location richloc (tok2->location);
30047 richloc.add_fixit_insert_after (tok->location, " requires");
30048 error_at (&richloc, "missing additional %<requires%> to start "
30049 "a requires-expression");
30050 /* Don't consume the `requires', so that it's reused as the start of a
30051 requires-expression. */
30053 else
30054 cp_lexer_consume_token (parser->lexer);
30056 if (!flag_concepts_ts)
30057 return cp_parser_requires_clause_expression (parser, lambda_p);
30058 else
30059 return cp_parser_constraint_expression (parser);
30062 /*---------------------------------------------------------------------------
30063 Requires expressions
30064 ---------------------------------------------------------------------------*/
30066 /* Parse a requires expression
30068 requirement-expression:
30069 'requires' requirement-parameter-list [opt] requirement-body */
30071 static tree
30072 cp_parser_requires_expression (cp_parser *parser)
30074 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30075 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
30077 /* Avoid committing to outer tentative parse. */
30078 tentative_firewall firewall (parser);
30080 /* This is definitely a requires-expression. */
30081 cp_parser_commit_to_tentative_parse (parser);
30083 tree parms, reqs;
30085 /* Local parameters are delared as variables within the scope
30086 of the expression. They are not visible past the end of
30087 the expression. Expressions within the requires-expression
30088 are unevaluated. */
30089 struct scope_sentinel
30091 scope_sentinel ()
30093 ++cp_unevaluated_operand;
30094 begin_scope (sk_function_parms, NULL_TREE);
30095 current_binding_level->requires_expression = true;
30098 ~scope_sentinel ()
30100 pop_bindings_and_leave_scope ();
30101 --cp_unevaluated_operand;
30103 } s;
30105 /* Parse the optional parameter list. */
30106 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30108 parms = cp_parser_requirement_parameter_list (parser);
30109 if (parms == error_mark_node)
30110 return error_mark_node;
30112 else
30113 parms = NULL_TREE;
30115 /* Parse the requirement body. */
30116 ++processing_template_decl;
30117 reqs = cp_parser_requirement_body (parser);
30118 --processing_template_decl;
30119 if (reqs == error_mark_node)
30120 return error_mark_node;
30123 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
30124 the parm chain. */
30125 grokparms (parms, &parms);
30126 loc = make_location (loc, loc, parser->lexer);
30127 tree expr = finish_requires_expr (loc, parms, reqs);
30128 if (!processing_template_decl)
30130 /* Perform semantic processing now to diagnose any invalid types and
30131 expressions. */
30132 int saved_errorcount = errorcount;
30133 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
30134 if (errorcount > saved_errorcount)
30135 return error_mark_node;
30137 return expr;
30140 /* Parse a parameterized requirement.
30142 requirement-parameter-list:
30143 '(' parameter-declaration-clause ')' */
30145 static tree
30146 cp_parser_requirement_parameter_list (cp_parser *parser)
30148 matching_parens parens;
30149 if (!parens.require_open (parser))
30150 return error_mark_node;
30152 tree parms = (cp_parser_parameter_declaration_clause
30153 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
30155 if (!parens.require_close (parser))
30156 return error_mark_node;
30158 /* Modify the declared parameters by removing their context
30159 so they don't refer to the enclosing scope and explicitly
30160 indicating that they are constraint variables. */
30161 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
30163 if (parm == void_list_node || parm == explicit_void_list_node)
30164 break;
30165 tree decl = TREE_VALUE (parm);
30166 if (decl != error_mark_node)
30168 DECL_CONTEXT (decl) = NULL_TREE;
30169 CONSTRAINT_VAR_P (decl) = true;
30173 return parms;
30176 /* Parse the body of a requirement.
30178 requirement-body:
30179 '{' requirement-list '}' */
30180 static tree
30181 cp_parser_requirement_body (cp_parser *parser)
30183 matching_braces braces;
30184 if (!braces.require_open (parser))
30185 return error_mark_node;
30187 tree reqs = cp_parser_requirement_seq (parser);
30189 if (!braces.require_close (parser))
30190 return error_mark_node;
30192 return reqs;
30195 /* Parse a sequence of requirements.
30197 requirement-seq:
30198 requirement
30199 requirement-seq requirement */
30201 static tree
30202 cp_parser_requirement_seq (cp_parser *parser)
30204 tree result = NULL_TREE;
30207 tree req = cp_parser_requirement (parser);
30208 if (req != error_mark_node)
30209 result = tree_cons (NULL_TREE, req, result);
30211 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
30212 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
30214 /* If there are no valid requirements, this is not a valid expression. */
30215 if (!result)
30216 return error_mark_node;
30218 /* Reverse the order of requirements so they are analyzed in order. */
30219 return nreverse (result);
30222 /* Parse a syntactic requirement or type requirement.
30224 requirement:
30225 simple-requirement
30226 compound-requirement
30227 type-requirement
30228 nested-requirement */
30230 static tree
30231 cp_parser_requirement (cp_parser *parser)
30233 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30234 return cp_parser_compound_requirement (parser);
30235 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
30236 return cp_parser_type_requirement (parser);
30237 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
30238 return cp_parser_nested_requirement (parser);
30239 else
30240 return cp_parser_simple_requirement (parser);
30243 /* Parse a simple requirement.
30245 simple-requirement:
30246 expression ';' */
30248 static tree
30249 cp_parser_simple_requirement (cp_parser *parser)
30251 location_t start = cp_lexer_peek_token (parser->lexer)->location;
30252 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
30253 if (expr == error_mark_node)
30254 cp_parser_skip_to_end_of_statement (parser);
30256 cp_parser_consume_semicolon_at_end_of_statement (parser);
30258 if (!expr || expr == error_mark_node)
30259 return error_mark_node;
30261 /* Sometimes we don't get locations, so use the cached token location
30262 as a reasonable approximation. */
30263 if (expr.get_location() == UNKNOWN_LOCATION)
30264 expr.set_location (start);
30266 for (tree t = expr; ; )
30268 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
30269 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
30271 t = TREE_OPERAND (t, 0);
30272 continue;
30274 if (concept_check_p (t))
30276 gcc_rich_location richloc (get_start (start));
30277 richloc.add_fixit_insert_before (start, "requires ");
30278 warning_at (&richloc, OPT_Wmissing_requires, "testing "
30279 "if a concept-id is a valid expression; add "
30280 "%<requires%> to check satisfaction");
30282 break;
30285 return finish_simple_requirement (expr.get_location (), expr);
30288 /* Parse a type requirement
30290 type-requirement
30291 nested-name-specifier [opt] required-type-name ';'
30293 required-type-name:
30294 type-name
30295 'template' [opt] simple-template-id */
30297 static tree
30298 cp_parser_type_requirement (cp_parser *parser)
30300 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
30301 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30303 // Save the scope before parsing name specifiers.
30304 tree saved_scope = parser->scope;
30305 tree saved_object_scope = parser->object_scope;
30306 tree saved_qualifying_scope = parser->qualifying_scope;
30307 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
30308 cp_parser_nested_name_specifier_opt (parser,
30309 /*typename_keyword_p=*/true,
30310 /*check_dependency_p=*/false,
30311 /*type_p=*/true,
30312 /*is_declaration=*/false);
30314 tree type;
30315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30317 cp_lexer_consume_token (parser->lexer);
30318 type = cp_parser_template_id (parser,
30319 /*template_keyword_p=*/true,
30320 /*check_dependency=*/false,
30321 /*tag_type=*/none_type,
30322 /*is_declaration=*/false);
30323 type = make_typename_type (parser->scope, type, typename_type,
30324 /*complain=*/tf_error);
30326 else
30327 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
30329 if (TREE_CODE (type) == TYPE_DECL)
30330 type = TREE_TYPE (type);
30332 parser->scope = saved_scope;
30333 parser->object_scope = saved_object_scope;
30334 parser->qualifying_scope = saved_qualifying_scope;
30336 if (type == error_mark_node)
30337 cp_parser_skip_to_end_of_statement (parser);
30339 cp_parser_consume_semicolon_at_end_of_statement (parser);
30341 if (type == error_mark_node)
30342 return error_mark_node;
30344 loc = make_location (loc, start_tok->location, parser->lexer);
30345 return finish_type_requirement (loc, type);
30348 /* Parse a compound requirement
30350 compound-requirement:
30351 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
30353 static tree
30354 cp_parser_compound_requirement (cp_parser *parser)
30356 /* Parse an expression enclosed in '{ }'s. */
30357 matching_braces braces;
30358 if (!braces.require_open (parser))
30359 return error_mark_node;
30361 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
30363 tree expr = cp_parser_expression (parser, NULL, false, false);
30364 if (expr == error_mark_node)
30365 cp_parser_skip_to_closing_brace (parser);
30367 if (!braces.require_close (parser))
30369 cp_parser_skip_to_end_of_statement (parser);
30370 cp_parser_consume_semicolon_at_end_of_statement (parser);
30371 return error_mark_node;
30374 /* If the expression was invalid, skip the remainder of the requirement. */
30375 if (!expr || expr == error_mark_node)
30377 cp_parser_skip_to_end_of_statement (parser);
30378 cp_parser_consume_semicolon_at_end_of_statement (parser);
30379 return error_mark_node;
30382 /* Parse the optional noexcept. */
30383 bool noexcept_p = false;
30384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
30386 cp_lexer_consume_token (parser->lexer);
30387 noexcept_p = true;
30390 /* Parse the optional trailing return type. */
30391 tree type = NULL_TREE;
30392 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
30394 cp_lexer_consume_token (parser->lexer);
30395 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30397 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
30398 parser->in_result_type_constraint_p = true;
30399 /* C++20 allows either a type-id or a type-constraint. Parsing
30400 a type-id will subsume the parsing for a type-constraint but
30401 allow for more syntactic forms (e.g., const C<T>*). */
30402 type = cp_parser_trailing_type_id (parser);
30403 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
30404 if (type == error_mark_node)
30405 return error_mark_node;
30407 location_t type_loc = make_location (tok->location, tok->location,
30408 parser->lexer);
30410 /* Check that we haven't written something like 'const C<T>*'. */
30411 if (type_uses_auto (type))
30413 if (!is_auto (type))
30415 error_at (type_loc,
30416 "result type is not a plain type-constraint");
30417 cp_parser_consume_semicolon_at_end_of_statement (parser);
30418 return error_mark_node;
30421 else if (!flag_concepts_ts)
30422 /* P1452R2 removed the trailing-return-type option. */
30423 error_at (type_loc,
30424 "return-type-requirement is not a type-constraint");
30427 location_t loc = make_location (expr_token->location,
30428 braces.open_location (),
30429 parser->lexer);
30431 cp_parser_consume_semicolon_at_end_of_statement (parser);
30433 if (expr == error_mark_node || type == error_mark_node)
30434 return error_mark_node;
30436 return finish_compound_requirement (loc, expr, type, noexcept_p);
30439 /* Parse a nested requirement. This is the same as a requires clause.
30441 nested-requirement:
30442 requires-clause */
30444 static tree
30445 cp_parser_nested_requirement (cp_parser *parser)
30447 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30448 cp_token *tok = cp_lexer_consume_token (parser->lexer);
30449 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30450 tree req = cp_parser_constraint_expression (parser);
30451 if (req == error_mark_node)
30452 cp_parser_skip_to_end_of_statement (parser);
30453 loc = make_location (loc, tok->location, parser->lexer);
30454 cp_parser_consume_semicolon_at_end_of_statement (parser);
30455 if (req == error_mark_node)
30456 return error_mark_node;
30457 return finish_nested_requirement (loc, req);
30460 /* Support Functions */
30462 /* Return the appropriate prefer_type argument for lookup_name based on
30463 tag_type. */
30465 static inline LOOK_want
30466 prefer_type_arg (tag_types tag_type)
30468 switch (tag_type)
30470 case none_type: return LOOK_want::NORMAL; // No preference.
30471 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
30472 default: return LOOK_want::TYPE; // Type only.
30476 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
30477 NAME should have one of the representations used for an
30478 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
30479 is returned. If PARSER->SCOPE is a dependent type, then a
30480 SCOPE_REF is returned.
30482 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
30483 returned; the name was already resolved when the TEMPLATE_ID_EXPR
30484 was formed. Abstractly, such entities should not be passed to this
30485 function, because they do not need to be looked up, but it is
30486 simpler to check for this special case here, rather than at the
30487 call-sites.
30489 In cases not explicitly covered above, this function returns a
30490 DECL, OVERLOAD, or baselink representing the result of the lookup.
30491 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
30492 is returned.
30494 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
30495 (e.g., "struct") that was used. In that case bindings that do not
30496 refer to types are ignored.
30498 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
30499 ignored.
30501 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
30502 are ignored.
30504 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
30505 types.
30507 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
30508 TREE_LIST of candidates if name-lookup results in an ambiguity, and
30509 NULL_TREE otherwise. */
30511 static cp_expr
30512 cp_parser_lookup_name (cp_parser *parser, tree name,
30513 enum tag_types tag_type,
30514 bool is_template,
30515 bool is_namespace,
30516 bool check_dependency,
30517 tree *ambiguous_decls,
30518 location_t name_location)
30520 tree decl;
30521 tree object_type = parser->context->object_type;
30523 /* Assume that the lookup will be unambiguous. */
30524 if (ambiguous_decls)
30525 *ambiguous_decls = NULL_TREE;
30527 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
30528 no longer valid. Note that if we are parsing tentatively, and
30529 the parse fails, OBJECT_TYPE will be automatically restored. */
30530 parser->context->object_type = NULL_TREE;
30532 if (name == error_mark_node)
30533 return error_mark_node;
30535 /* A template-id has already been resolved; there is no lookup to
30536 do. */
30537 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
30538 return name;
30539 if (BASELINK_P (name))
30541 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
30542 == TEMPLATE_ID_EXPR);
30543 return name;
30546 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
30547 it should already have been checked to make sure that the name
30548 used matches the type being destroyed. */
30549 if (TREE_CODE (name) == BIT_NOT_EXPR)
30551 tree type;
30553 /* Figure out to which type this destructor applies. */
30554 if (parser->scope)
30555 type = parser->scope;
30556 else if (object_type)
30557 type = object_type;
30558 else
30559 type = current_class_type;
30560 /* If that's not a class type, there is no destructor. */
30561 if (!type || !CLASS_TYPE_P (type))
30562 return error_mark_node;
30564 /* In a non-static member function, check implicit this->. */
30565 if (current_class_ref)
30566 return lookup_destructor (current_class_ref, parser->scope, name,
30567 tf_warning_or_error);
30569 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
30570 lazily_declare_fn (sfk_destructor, type);
30572 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
30573 return dtor;
30575 return error_mark_node;
30578 /* By this point, the NAME should be an ordinary identifier. If
30579 the id-expression was a qualified name, the qualifying scope is
30580 stored in PARSER->SCOPE at this point. */
30581 gcc_assert (identifier_p (name));
30583 /* Perform the lookup. */
30584 if (parser->scope)
30586 bool dependent_p;
30588 if (parser->scope == error_mark_node)
30589 return error_mark_node;
30591 /* If the SCOPE is dependent, the lookup must be deferred until
30592 the template is instantiated -- unless we are explicitly
30593 looking up names in uninstantiated templates. Even then, we
30594 cannot look up the name if the scope is not a class type; it
30595 might, for example, be a template type parameter. */
30596 dependent_p = (TYPE_P (parser->scope)
30597 && dependent_scope_p (parser->scope));
30598 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
30599 && dependent_p)
30600 /* Defer lookup. */
30601 decl = error_mark_node;
30602 else
30604 tree pushed_scope = NULL_TREE;
30606 /* If PARSER->SCOPE is a dependent type, then it must be a
30607 class type, and we must not be checking dependencies;
30608 otherwise, we would have processed this lookup above. So
30609 that PARSER->SCOPE is not considered a dependent base by
30610 lookup_member, we must enter the scope here. */
30611 if (dependent_p)
30612 pushed_scope = push_scope (parser->scope);
30614 /* If the PARSER->SCOPE is a template specialization, it
30615 may be instantiated during name lookup. In that case,
30616 errors may be issued. Even if we rollback the current
30617 tentative parse, those errors are valid. */
30618 decl = lookup_qualified_name (parser->scope, name,
30619 prefer_type_arg (tag_type),
30620 /*complain=*/true);
30622 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
30623 lookup result and the nested-name-specifier nominates a class C:
30624 * if the name specified after the nested-name-specifier, when
30625 looked up in C, is the injected-class-name of C (Clause 9), or
30626 * if the name specified after the nested-name-specifier is the
30627 same as the identifier or the simple-template-id's template-
30628 name in the last component of the nested-name-specifier,
30629 the name is instead considered to name the constructor of
30630 class C. [ Note: for example, the constructor is not an
30631 acceptable lookup result in an elaborated-type-specifier so
30632 the constructor would not be used in place of the
30633 injected-class-name. --end note ] Such a constructor name
30634 shall be used only in the declarator-id of a declaration that
30635 names a constructor or in a using-declaration. */
30636 if (tag_type == none_type
30637 && DECL_SELF_REFERENCE_P (decl)
30638 && same_type_p (DECL_CONTEXT (decl), parser->scope))
30639 decl = lookup_qualified_name (parser->scope, ctor_identifier,
30640 prefer_type_arg (tag_type),
30641 /*complain=*/true);
30643 if (pushed_scope)
30644 pop_scope (pushed_scope);
30647 /* If the scope is a dependent type and either we deferred lookup or
30648 we did lookup but didn't find the name, rememeber the name. */
30649 if (decl == error_mark_node && TYPE_P (parser->scope)
30650 && dependent_type_p (parser->scope))
30652 if (tag_type)
30654 tree type;
30656 /* The resolution to Core Issue 180 says that `struct
30657 A::B' should be considered a type-name, even if `A'
30658 is dependent. */
30659 type = make_typename_type (parser->scope, name, tag_type,
30660 /*complain=*/tf_error);
30661 if (type != error_mark_node)
30662 decl = TYPE_NAME (type);
30664 else if (is_template
30665 && (cp_parser_next_token_ends_template_argument_p (parser)
30666 || cp_lexer_next_token_is (parser->lexer,
30667 CPP_CLOSE_PAREN)))
30668 decl = make_unbound_class_template (parser->scope,
30669 name, NULL_TREE,
30670 /*complain=*/tf_error);
30671 else
30672 decl = build_qualified_name (/*type=*/NULL_TREE,
30673 parser->scope, name,
30674 is_template);
30676 parser->qualifying_scope = parser->scope;
30677 parser->object_scope = NULL_TREE;
30679 else if (object_type)
30681 /* Look up the name in the scope of the OBJECT_TYPE, unless the
30682 OBJECT_TYPE is not a class. */
30683 if (CLASS_TYPE_P (object_type))
30684 /* If the OBJECT_TYPE is a template specialization, it may
30685 be instantiated during name lookup. In that case, errors
30686 may be issued. Even if we rollback the current tentative
30687 parse, those errors are valid. */
30688 decl = lookup_member (object_type,
30689 name,
30690 /*protect=*/0,
30691 /*prefer_type=*/tag_type != none_type,
30692 tf_warning_or_error);
30693 else
30694 decl = NULL_TREE;
30696 if (!decl)
30697 /* Look it up in the enclosing context. DR 141: When looking for a
30698 template-name after -> or ., only consider class templates. */
30699 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30700 /* DR 141: When looking in the
30701 current enclosing context for a
30702 template-name after -> or ., only
30703 consider class templates. */
30704 : is_template ? LOOK_want::TYPE
30705 : prefer_type_arg (tag_type));
30707 /* If we know we're looking for a type (e.g. A in p->A::x),
30708 mock up a typename. */
30709 if (!decl && object_type && tag_type != none_type
30710 && dependentish_scope_p (object_type))
30712 tree type = build_typename_type (object_type, name, name,
30713 typename_type);
30714 decl = TYPE_NAME (type);
30717 parser->object_scope = object_type;
30718 parser->qualifying_scope = NULL_TREE;
30720 else
30722 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30723 : prefer_type_arg (tag_type));
30724 parser->qualifying_scope = NULL_TREE;
30725 parser->object_scope = NULL_TREE;
30728 /* If the lookup failed, let our caller know. */
30729 if (!decl || decl == error_mark_node)
30730 return error_mark_node;
30732 /* If we have resolved the name of a member declaration, check to
30733 see if the declaration is accessible. When the name resolves to
30734 set of overloaded functions, accessibility is checked when
30735 overload resolution is done. If we have a TREE_LIST, then the lookup
30736 is either ambiguous or it found multiple injected-class-names, the
30737 accessibility of which is trivially satisfied.
30739 During an explicit instantiation, access is not checked at all,
30740 as per [temp.explicit]. */
30741 if (DECL_P (decl))
30742 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
30743 tf_warning_or_error);
30745 /* Pull out the template from an injected-class-name (or multiple). */
30746 if (is_template)
30747 decl = maybe_get_template_decl_from_type_decl (decl);
30749 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
30750 if (TREE_CODE (decl) == TREE_LIST)
30752 if (ambiguous_decls)
30753 *ambiguous_decls = decl;
30754 /* The error message we have to print is too complicated for
30755 cp_parser_error, so we incorporate its actions directly. */
30756 if (!cp_parser_simulate_error (parser))
30758 error_at (name_location, "reference to %qD is ambiguous",
30759 name);
30760 print_candidates (decl);
30762 return error_mark_node;
30765 gcc_assert (DECL_P (decl)
30766 || TREE_CODE (decl) == OVERLOAD
30767 || TREE_CODE (decl) == SCOPE_REF
30768 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
30769 || BASELINK_P (decl));
30771 maybe_record_typedef_use (decl);
30773 return cp_expr (decl, name_location);
30776 /* Like cp_parser_lookup_name, but for use in the typical case where
30777 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
30778 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
30780 static tree
30781 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
30783 return cp_parser_lookup_name (parser, name,
30784 none_type,
30785 /*is_template=*/false,
30786 /*is_namespace=*/false,
30787 /*check_dependency=*/true,
30788 /*ambiguous_decls=*/NULL,
30789 location);
30792 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
30793 the current context, return the TYPE_DECL. If TAG_NAME_P is
30794 true, the DECL indicates the class being defined in a class-head,
30795 or declared in an elaborated-type-specifier.
30797 Otherwise, return DECL. */
30799 static tree
30800 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
30802 /* If the TEMPLATE_DECL is being declared as part of a class-head,
30803 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
30805 struct A {
30806 template <typename T> struct B;
30809 template <typename T> struct A::B {};
30811 Similarly, in an elaborated-type-specifier:
30813 namespace N { struct X{}; }
30815 struct A {
30816 template <typename T> friend struct N::X;
30819 However, if the DECL refers to a class type, and we are in
30820 the scope of the class, then the name lookup automatically
30821 finds the TYPE_DECL created by build_self_reference rather
30822 than a TEMPLATE_DECL. For example, in:
30824 template <class T> struct S {
30825 S s;
30828 there is no need to handle such case. */
30830 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
30831 return DECL_TEMPLATE_RESULT (decl);
30833 return decl;
30836 /* If too many, or too few, template-parameter lists apply to the
30837 declarator, issue an error message. Returns TRUE if all went well,
30838 and FALSE otherwise. */
30840 static bool
30841 cp_parser_check_declarator_template_parameters (cp_parser* parser,
30842 cp_declarator *declarator,
30843 location_t declarator_location)
30845 switch (declarator->kind)
30847 case cdk_id:
30849 unsigned num_templates = 0;
30850 tree scope = declarator->u.id.qualifying_scope;
30851 bool template_id_p = false;
30853 if (scope)
30854 num_templates = num_template_headers_for_class (scope);
30855 else if (TREE_CODE (declarator->u.id.unqualified_name)
30856 == TEMPLATE_ID_EXPR)
30858 /* If the DECLARATOR has the form `X<y>' then it uses one
30859 additional level of template parameters. */
30860 ++num_templates;
30861 template_id_p = true;
30864 return cp_parser_check_template_parameters
30865 (parser, num_templates, template_id_p, declarator_location,
30866 declarator);
30869 case cdk_function:
30870 case cdk_array:
30871 case cdk_pointer:
30872 case cdk_reference:
30873 case cdk_ptrmem:
30874 return (cp_parser_check_declarator_template_parameters
30875 (parser, declarator->declarator, declarator_location));
30877 case cdk_decomp:
30878 case cdk_error:
30879 return true;
30881 default:
30882 gcc_unreachable ();
30884 return false;
30887 /* NUM_TEMPLATES were used in the current declaration. If that is
30888 invalid, return FALSE and issue an error messages. Otherwise,
30889 return TRUE. If DECLARATOR is non-NULL, then we are checking a
30890 declarator and we can print more accurate diagnostics. */
30892 static bool
30893 cp_parser_check_template_parameters (cp_parser* parser,
30894 unsigned num_templates,
30895 bool template_id_p,
30896 location_t location,
30897 cp_declarator *declarator)
30899 /* If there are the same number of template classes and parameter
30900 lists, that's OK. */
30901 if (parser->num_template_parameter_lists == num_templates)
30902 return true;
30903 /* If there are more, but only one more, and the name ends in an identifier,
30904 then we are declaring a primary template. That's OK too. */
30905 if (!template_id_p
30906 && parser->num_template_parameter_lists == num_templates + 1)
30907 return true;
30909 if (cp_parser_simulate_error (parser))
30910 return false;
30912 /* If there are more template classes than parameter lists, we have
30913 something like:
30915 template <class T> void S<T>::R<T>::f (); */
30916 if (parser->num_template_parameter_lists < num_templates)
30918 if (declarator && !current_function_decl)
30919 error_at (location, "specializing member %<%T::%E%> "
30920 "requires %<template<>%> syntax",
30921 declarator->u.id.qualifying_scope,
30922 declarator->u.id.unqualified_name);
30923 else if (declarator)
30924 error_at (location, "invalid declaration of %<%T::%E%>",
30925 declarator->u.id.qualifying_scope,
30926 declarator->u.id.unqualified_name);
30927 else
30928 error_at (location, "too few template-parameter-lists");
30929 return false;
30931 /* Otherwise, there are too many template parameter lists. We have
30932 something like:
30934 template <class T> template <class U> void S::f(); */
30935 error_at (location, "too many template-parameter-lists");
30936 return false;
30939 /* Parse an optional `::' token indicating that the following name is
30940 from the global namespace. If so, PARSER->SCOPE is set to the
30941 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
30942 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
30943 Returns the new value of PARSER->SCOPE, if the `::' token is
30944 present, and NULL_TREE otherwise. */
30946 static tree
30947 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
30949 cp_token *token;
30951 /* Peek at the next token. */
30952 token = cp_lexer_peek_token (parser->lexer);
30953 /* If we're looking at a `::' token then we're starting from the
30954 global namespace, not our current location. */
30955 if (token->type == CPP_SCOPE)
30957 /* Consume the `::' token. */
30958 cp_lexer_consume_token (parser->lexer);
30959 /* Set the SCOPE so that we know where to start the lookup. */
30960 parser->scope = global_namespace;
30961 parser->qualifying_scope = global_namespace;
30962 parser->object_scope = NULL_TREE;
30964 return parser->scope;
30966 else if (!current_scope_valid_p)
30968 parser->scope = NULL_TREE;
30969 parser->qualifying_scope = NULL_TREE;
30970 parser->object_scope = NULL_TREE;
30973 return NULL_TREE;
30976 /* Returns TRUE if the upcoming token sequence is the start of a
30977 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
30978 declarator is preceded by the `friend' specifier. The parser flags FLAGS
30979 is used to control type-specifier parsing. */
30981 static bool
30982 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
30983 bool friend_p)
30985 bool constructor_p;
30986 bool outside_class_specifier_p;
30987 tree nested_name_specifier;
30988 cp_token *next_token;
30990 /* The common case is that this is not a constructor declarator, so
30991 try to avoid doing lots of work if at all possible. It's not
30992 valid declare a constructor at function scope. */
30993 if (parser->in_function_body)
30994 return false;
30995 /* And only certain tokens can begin a constructor declarator. */
30996 next_token = cp_lexer_peek_token (parser->lexer);
30997 if (next_token->type != CPP_NAME
30998 && next_token->type != CPP_SCOPE
30999 && next_token->type != CPP_NESTED_NAME_SPECIFIER
31000 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
31001 declarator-id of a constructor or destructor. */
31002 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
31003 return false;
31005 /* Parse tentatively; we are going to roll back all of the tokens
31006 consumed here. */
31007 cp_parser_parse_tentatively (parser);
31008 /* Assume that we are looking at a constructor declarator. */
31009 constructor_p = true;
31011 /* Look for the optional `::' operator. */
31012 cp_parser_global_scope_opt (parser,
31013 /*current_scope_valid_p=*/false);
31014 /* Look for the nested-name-specifier. */
31015 nested_name_specifier
31016 = (cp_parser_nested_name_specifier_opt (parser,
31017 /*typename_keyword_p=*/false,
31018 /*check_dependency_p=*/false,
31019 /*type_p=*/false,
31020 /*is_declaration=*/false));
31022 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
31023 if (nested_name_specifier
31024 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
31026 tree s = resolve_typename_type (nested_name_specifier,
31027 /*only_current_p=*/false);
31028 if (TREE_CODE (s) != TYPENAME_TYPE)
31029 nested_name_specifier = s;
31032 outside_class_specifier_p = (!at_class_scope_p ()
31033 || !TYPE_BEING_DEFINED (current_class_type)
31034 || friend_p);
31036 /* Outside of a class-specifier, there must be a
31037 nested-name-specifier. Except in C++17 mode, where we
31038 might be declaring a guiding declaration. */
31039 if (!nested_name_specifier && outside_class_specifier_p
31040 && cxx_dialect < cxx17)
31041 constructor_p = false;
31042 else if (nested_name_specifier == error_mark_node)
31043 constructor_p = false;
31045 /* If we have a class scope, this is easy; DR 147 says that S::S always
31046 names the constructor, and no other qualified name could. */
31047 if (constructor_p && nested_name_specifier
31048 && CLASS_TYPE_P (nested_name_specifier))
31050 tree id = cp_parser_unqualified_id (parser,
31051 /*template_keyword_p=*/false,
31052 /*check_dependency_p=*/false,
31053 /*declarator_p=*/true,
31054 /*optional_p=*/false);
31055 if (is_overloaded_fn (id))
31056 id = DECL_NAME (get_first_fn (id));
31057 if (!constructor_name_p (id, nested_name_specifier))
31058 constructor_p = false;
31060 /* If we still think that this might be a constructor-declarator,
31061 look for a class-name. */
31062 else if (constructor_p)
31064 /* If we have:
31066 template <typename T> struct S {
31067 S();
31070 we must recognize that the nested `S' names a class. */
31071 if (cxx_dialect >= cxx17)
31072 cp_parser_parse_tentatively (parser);
31074 tree type_decl;
31075 type_decl = cp_parser_class_name (parser,
31076 /*typename_keyword_p=*/false,
31077 /*template_keyword_p=*/false,
31078 none_type,
31079 /*check_dependency_p=*/false,
31080 /*class_head_p=*/false,
31081 /*is_declaration=*/false);
31083 if (cxx_dialect >= cxx17
31084 && !cp_parser_parse_definitely (parser))
31086 type_decl = NULL_TREE;
31087 tree tmpl = cp_parser_template_name (parser,
31088 /*template_keyword*/false,
31089 /*check_dependency_p*/false,
31090 /*is_declaration*/false,
31091 none_type,
31092 /*is_identifier*/NULL);
31093 if (DECL_CLASS_TEMPLATE_P (tmpl)
31094 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
31095 /* It's a deduction guide, return true. */;
31096 else
31097 cp_parser_simulate_error (parser);
31100 /* If there was no class-name, then this is not a constructor.
31101 Otherwise, if we are in a class-specifier and we aren't
31102 handling a friend declaration, check that its type matches
31103 current_class_type (c++/38313). Note: error_mark_node
31104 is left alone for error recovery purposes. */
31105 constructor_p = (!cp_parser_error_occurred (parser)
31106 && (outside_class_specifier_p
31107 || type_decl == NULL_TREE
31108 || type_decl == error_mark_node
31109 || same_type_p (current_class_type,
31110 TREE_TYPE (type_decl))));
31112 /* If we're still considering a constructor, we have to see a `(',
31113 to begin the parameter-declaration-clause, followed by either a
31114 `)', an `...', or a decl-specifier. We need to check for a
31115 type-specifier to avoid being fooled into thinking that:
31117 S (f) (int);
31119 is a constructor. (It is actually a function named `f' that
31120 takes one parameter (of type `int') and returns a value of type
31121 `S'. */
31122 if (constructor_p
31123 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31124 constructor_p = false;
31126 if (constructor_p
31127 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31128 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
31129 /* A parameter declaration begins with a decl-specifier,
31130 which is either the "attribute" keyword, a storage class
31131 specifier, or (usually) a type-specifier. */
31132 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
31133 /* GNU attributes can actually appear both at the start of
31134 a parameter and parenthesized declarator.
31135 S (__attribute__((unused)) int);
31136 is a constructor, but
31137 S (__attribute__((unused)) foo) (int);
31138 is a function declaration. [[attribute]] can appear in the
31139 first form too, but not in the second form. */
31140 && !cp_next_tokens_can_be_std_attribute_p (parser))
31142 tree type;
31143 tree pushed_scope = NULL_TREE;
31144 unsigned saved_num_template_parameter_lists;
31146 if (cp_parser_allow_gnu_extensions_p (parser)
31147 && cp_next_tokens_can_be_gnu_attribute_p (parser))
31149 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
31150 while (--n)
31151 cp_lexer_consume_token (parser->lexer);
31154 /* Names appearing in the type-specifier should be looked up
31155 in the scope of the class. */
31156 if (current_class_type)
31157 type = NULL_TREE;
31158 else if (type_decl)
31160 type = TREE_TYPE (type_decl);
31161 if (TREE_CODE (type) == TYPENAME_TYPE)
31163 type = resolve_typename_type (type,
31164 /*only_current_p=*/false);
31165 if (TREE_CODE (type) == TYPENAME_TYPE)
31167 cp_parser_abort_tentative_parse (parser);
31168 return false;
31171 pushed_scope = push_scope (type);
31174 /* Inside the constructor parameter list, surrounding
31175 template-parameter-lists do not apply. */
31176 saved_num_template_parameter_lists
31177 = parser->num_template_parameter_lists;
31178 parser->num_template_parameter_lists = 0;
31180 /* Look for the type-specifier. It's not optional, but its typename
31181 might be. Unless this is a friend declaration; we don't want to
31182 treat
31184 friend S (T::fn)(int);
31186 as a constructor, but with P0634, we might assume a type when
31187 looking for the type-specifier. It is actually a function named
31188 `T::fn' that takes one parameter (of type `int') and returns a
31189 value of type `S'. Constructors can be friends, but they must
31190 use a qualified name.
31192 Parse with an empty set of declaration specifiers since we're
31193 trying to match a decl-specifier-seq of the first parameter.
31194 This must be non-null so that cp_parser_simple_type_specifier
31195 will recognize a constrained placeholder type such as:
31196 'C<int> auto' where C is a type concept. */
31197 cp_decl_specifier_seq ctor_specs;
31198 clear_decl_specs (&ctor_specs);
31199 cp_parser_type_specifier (parser,
31200 (friend_p ? CP_PARSER_FLAGS_NONE
31201 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
31202 /*decl_specs=*/&ctor_specs,
31203 /*is_declarator=*/true,
31204 /*declares_class_or_enum=*/NULL,
31205 /*is_cv_qualifier=*/NULL);
31207 parser->num_template_parameter_lists
31208 = saved_num_template_parameter_lists;
31210 /* Leave the scope of the class. */
31211 if (pushed_scope)
31212 pop_scope (pushed_scope);
31214 constructor_p = !cp_parser_error_occurred (parser);
31218 /* We did not really want to consume any tokens. */
31219 cp_parser_abort_tentative_parse (parser);
31221 return constructor_p;
31224 /* Parse the definition of the function given by the DECL_SPECIFIERS,
31225 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
31226 they must be performed once we are in the scope of the function.
31228 Returns the function defined. */
31230 static tree
31231 cp_parser_function_definition_from_specifiers_and_declarator
31232 (cp_parser* parser,
31233 cp_decl_specifier_seq *decl_specifiers,
31234 tree attributes,
31235 const cp_declarator *declarator)
31237 tree fn;
31238 bool success_p;
31240 /* Begin the function-definition. */
31241 success_p = start_function (decl_specifiers, declarator, attributes);
31243 /* The things we're about to see are not directly qualified by any
31244 template headers we've seen thus far. */
31245 reset_specialization ();
31247 /* If there were names looked up in the decl-specifier-seq that we
31248 did not check, check them now. We must wait until we are in the
31249 scope of the function to perform the checks, since the function
31250 might be a friend. */
31251 perform_deferred_access_checks (tf_warning_or_error);
31253 if (success_p)
31255 cp_finalize_omp_declare_simd (parser, current_function_decl);
31256 parser->omp_declare_simd = NULL;
31257 cp_finalize_oacc_routine (parser, current_function_decl, true);
31258 parser->oacc_routine = NULL;
31261 if (!success_p)
31263 /* Skip the entire function. */
31264 cp_parser_skip_to_end_of_block_or_statement (parser);
31265 fn = error_mark_node;
31267 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
31269 /* Seen already, skip it. An error message has already been output. */
31270 cp_parser_skip_to_end_of_block_or_statement (parser);
31271 fn = current_function_decl;
31272 current_function_decl = NULL_TREE;
31273 /* If this is a function from a class, pop the nested class. */
31274 if (current_class_name)
31275 pop_nested_class ();
31277 else
31279 timevar_id_t tv;
31280 if (DECL_DECLARED_INLINE_P (current_function_decl))
31281 tv = TV_PARSE_INLINE;
31282 else
31283 tv = TV_PARSE_FUNC;
31284 timevar_push (tv);
31285 fn = cp_parser_function_definition_after_declarator (parser,
31286 /*inline_p=*/false);
31287 timevar_pop (tv);
31290 return fn;
31293 /* Parse the part of a function-definition that follows the
31294 declarator. INLINE_P is TRUE iff this function is an inline
31295 function defined within a class-specifier.
31297 Returns the function defined. */
31299 static tree
31300 cp_parser_function_definition_after_declarator (cp_parser* parser,
31301 bool inline_p)
31303 tree fn;
31304 bool saved_in_unbraced_linkage_specification_p;
31305 bool saved_in_function_body;
31306 unsigned saved_num_template_parameter_lists;
31307 cp_token *token;
31308 bool fully_implicit_function_template_p
31309 = parser->fully_implicit_function_template_p;
31310 parser->fully_implicit_function_template_p = false;
31311 tree implicit_template_parms
31312 = parser->implicit_template_parms;
31313 parser->implicit_template_parms = 0;
31314 cp_binding_level* implicit_template_scope
31315 = parser->implicit_template_scope;
31316 parser->implicit_template_scope = 0;
31318 saved_in_function_body = parser->in_function_body;
31319 parser->in_function_body = true;
31320 /* If the next token is `return', then the code may be trying to
31321 make use of the "named return value" extension that G++ used to
31322 support. */
31323 token = cp_lexer_peek_token (parser->lexer);
31324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
31326 /* Consume the `return' keyword. */
31327 cp_lexer_consume_token (parser->lexer);
31328 /* Look for the identifier that indicates what value is to be
31329 returned. */
31330 cp_parser_identifier (parser);
31331 /* Issue an error message. */
31332 error_at (token->location,
31333 "named return values are no longer supported");
31334 /* Skip tokens until we reach the start of the function body. */
31335 while (true)
31337 cp_token *token = cp_lexer_peek_token (parser->lexer);
31338 if (token->type == CPP_OPEN_BRACE
31339 || token->type == CPP_EOF
31340 || token->type == CPP_PRAGMA_EOL)
31341 break;
31342 cp_lexer_consume_token (parser->lexer);
31345 /* The `extern' in `extern "C" void f () { ... }' does not apply to
31346 anything declared inside `f'. */
31347 saved_in_unbraced_linkage_specification_p
31348 = parser->in_unbraced_linkage_specification_p;
31349 parser->in_unbraced_linkage_specification_p = false;
31350 /* Inside the function, surrounding template-parameter-lists do not
31351 apply. */
31352 saved_num_template_parameter_lists
31353 = parser->num_template_parameter_lists;
31354 parser->num_template_parameter_lists = 0;
31356 /* If the next token is `try', `__transaction_atomic', or
31357 `__transaction_relaxed`, then we are looking at either function-try-block
31358 or function-transaction-block. Note that all of these include the
31359 function-body. */
31360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
31361 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
31362 else if (cp_lexer_next_token_is_keyword (parser->lexer,
31363 RID_TRANSACTION_RELAXED))
31364 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
31365 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31366 cp_parser_function_try_block (parser);
31367 else
31368 cp_parser_ctor_initializer_opt_and_function_body
31369 (parser, /*in_function_try_block=*/false);
31371 /* Finish the function. */
31372 fn = finish_function (inline_p);
31374 if (modules_p ()
31375 && !inline_p
31376 && TYPE_P (DECL_CONTEXT (fn))
31377 && (DECL_DECLARED_INLINE_P (fn)
31378 || processing_template_decl))
31379 set_defining_module (fn);
31381 /* Generate code for it, if necessary. */
31382 expand_or_defer_fn (fn);
31383 /* Restore the saved values. */
31384 parser->in_unbraced_linkage_specification_p
31385 = saved_in_unbraced_linkage_specification_p;
31386 parser->num_template_parameter_lists
31387 = saved_num_template_parameter_lists;
31388 parser->in_function_body = saved_in_function_body;
31390 parser->fully_implicit_function_template_p
31391 = fully_implicit_function_template_p;
31392 parser->implicit_template_parms
31393 = implicit_template_parms;
31394 parser->implicit_template_scope
31395 = implicit_template_scope;
31397 if (parser->fully_implicit_function_template_p)
31398 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
31400 return fn;
31403 /* Parse a template-declaration body (following argument list). */
31405 static void
31406 cp_parser_template_declaration_after_parameters (cp_parser* parser,
31407 tree parameter_list,
31408 bool member_p)
31410 tree decl = NULL_TREE;
31411 bool friend_p = false;
31413 /* We just processed one more parameter list. */
31414 ++parser->num_template_parameter_lists;
31416 /* Get the deferred access checks from the parameter list. These
31417 will be checked once we know what is being declared, as for a
31418 member template the checks must be performed in the scope of the
31419 class containing the member. */
31420 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
31422 /* Tentatively parse for a new template parameter list, which can either be
31423 the template keyword or a template introduction. */
31424 if (cp_parser_template_declaration_after_export (parser, member_p))
31425 /* OK */;
31426 else if (cxx_dialect >= cxx11
31427 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
31428 decl = cp_parser_alias_declaration (parser);
31429 else if (cxx_dialect >= cxx20 /* Implies flag_concept. */
31430 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
31431 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
31432 /* Allow 'concept bool' to be handled as per the TS. */
31433 decl = cp_parser_concept_definition (parser);
31434 else
31436 cp_token *token = cp_lexer_peek_token (parser->lexer);
31437 decl = cp_parser_single_declaration (parser,
31438 checks,
31439 member_p,
31440 /*explicit_specialization_p=*/false,
31441 &friend_p);
31443 /* If this is a member template declaration, let the front
31444 end know. */
31445 if (member_p && !friend_p && decl)
31447 if (TREE_CODE (decl) == TYPE_DECL)
31448 cp_parser_check_access_in_redeclaration (decl, token->location);
31450 decl = finish_member_template_decl (decl);
31452 else if (friend_p && decl
31453 && DECL_DECLARES_TYPE_P (decl))
31454 make_friend_class (current_class_type, TREE_TYPE (decl),
31455 /*complain=*/true);
31457 /* We are done with the current parameter list. */
31458 --parser->num_template_parameter_lists;
31460 pop_deferring_access_checks ();
31462 /* Finish up. */
31463 finish_template_decl (parameter_list);
31465 /* Check the template arguments for a literal operator template. */
31466 if (decl
31467 && DECL_DECLARES_FUNCTION_P (decl)
31468 && UDLIT_OPER_P (DECL_NAME (decl)))
31470 bool ok = true;
31471 if (parameter_list == NULL_TREE)
31472 ok = false;
31473 else
31475 int num_parms = TREE_VEC_LENGTH (parameter_list);
31476 if (num_parms == 1)
31478 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
31479 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31480 if (TREE_CODE (parm) != PARM_DECL)
31481 ok = false;
31482 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
31483 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31484 /* OK, C++20 string literal operator template. We don't need
31485 to warn in lower dialects here because we will have already
31486 warned about the template parameter. */;
31487 else if (TREE_TYPE (parm) != char_type_node
31488 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31489 ok = false;
31491 else if (num_parms == 2 && cxx_dialect >= cxx14)
31493 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
31494 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
31495 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
31496 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31497 if (TREE_CODE (parm) != PARM_DECL
31498 || TREE_TYPE (parm) != TREE_TYPE (type)
31499 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31500 ok = false;
31501 else
31502 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
31503 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
31504 "ISO C++ did not adopt string literal operator templa"
31505 "tes taking an argument pack of characters");
31507 else
31508 ok = false;
31510 if (!ok)
31512 if (cxx_dialect > cxx17)
31513 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31514 "template %qD has invalid parameter list; expected "
31515 "non-type template parameter pack %<<char...>%> or "
31516 "single non-type parameter of class type",
31517 decl);
31518 else
31519 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31520 "template %qD has invalid parameter list; expected "
31521 "non-type template parameter pack %<<char...>%>",
31522 decl);
31526 /* Register member declarations. */
31527 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
31528 finish_member_declaration (decl);
31529 /* If DECL is a function template, we must return to parse it later.
31530 (Even though there is no definition, there might be default
31531 arguments that need handling.) */
31532 if (member_p && decl
31533 && DECL_DECLARES_FUNCTION_P (decl))
31534 vec_safe_push (unparsed_funs_with_definitions, decl);
31537 /* Parse a template introduction header for a template-declaration. Returns
31538 false if tentative parse fails. */
31540 static bool
31541 cp_parser_template_introduction (cp_parser* parser, bool member_p)
31543 cp_parser_parse_tentatively (parser);
31545 tree saved_scope = parser->scope;
31546 tree saved_object_scope = parser->object_scope;
31547 tree saved_qualifying_scope = parser->qualifying_scope;
31548 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31550 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
31552 /* In classes don't parse valid unnamed bitfields as invalid
31553 template introductions. */
31554 if (member_p)
31555 parser->colon_corrects_to_scope_p = false;
31557 /* Look for the optional `::' operator. */
31558 cp_parser_global_scope_opt (parser,
31559 /*current_scope_valid_p=*/false);
31560 /* Look for the nested-name-specifier. */
31561 cp_parser_nested_name_specifier_opt (parser,
31562 /*typename_keyword_p=*/false,
31563 /*check_dependency_p=*/true,
31564 /*type_p=*/false,
31565 /*is_declaration=*/false);
31567 cp_token *token = cp_lexer_peek_token (parser->lexer);
31568 tree concept_name = cp_parser_identifier (parser);
31570 /* Look up the concept for which we will be matching
31571 template parameters. */
31572 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
31573 token->location);
31574 parser->scope = saved_scope;
31575 parser->object_scope = saved_object_scope;
31576 parser->qualifying_scope = saved_qualifying_scope;
31577 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31579 if (concept_name == error_mark_node
31580 || (seen_error () && !concept_definition_p (tmpl_decl)))
31581 cp_parser_simulate_error (parser);
31583 /* Look for opening brace for introduction. */
31584 matching_braces braces;
31585 braces.require_open (parser);
31586 location_t open_loc = input_location;
31588 if (!cp_parser_parse_definitely (parser))
31589 return false;
31591 push_deferring_access_checks (dk_deferred);
31593 /* Build vector of placeholder parameters and grab
31594 matching identifiers. */
31595 tree introduction_list = cp_parser_introduction_list (parser);
31597 /* Look for closing brace for introduction. */
31598 if (!braces.require_close (parser))
31599 return true;
31601 /* The introduction-list shall not be empty. */
31602 int nargs = TREE_VEC_LENGTH (introduction_list);
31603 if (nargs == 0)
31605 /* In cp_parser_introduction_list we have already issued an error. */
31606 return true;
31609 if (tmpl_decl == error_mark_node)
31611 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
31612 token->location);
31613 return true;
31616 /* Build and associate the constraint. */
31617 location_t introduction_loc = make_location (open_loc,
31618 start_token->location,
31619 parser->lexer);
31620 tree parms = finish_template_introduction (tmpl_decl,
31621 introduction_list,
31622 introduction_loc);
31623 if (parms && parms != error_mark_node)
31625 if (!flag_concepts_ts)
31626 pedwarn (introduction_loc, 0, "template-introductions"
31627 " are not part of C++20 concepts; use %qs to enable",
31628 "-fconcepts-ts");
31630 cp_parser_template_declaration_after_parameters (parser, parms,
31631 member_p);
31632 return true;
31635 if (parms == NULL_TREE)
31636 error_at (token->location, "no matching concept for template-introduction");
31638 return true;
31641 /* Parse a normal template-declaration following the template keyword. */
31643 static void
31644 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
31646 tree parameter_list;
31647 bool need_lang_pop;
31648 location_t location = input_location;
31650 /* Look for the `<' token. */
31651 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
31652 return;
31653 if (at_class_scope_p () && current_function_decl)
31655 /* 14.5.2.2 [temp.mem]
31657 A local class shall not have member templates. */
31658 error_at (location,
31659 "invalid declaration of member template in local class");
31660 cp_parser_skip_to_end_of_block_or_statement (parser);
31661 return;
31663 /* [temp]
31665 A template ... shall not have C linkage. */
31666 if (current_lang_name == lang_name_c)
31668 error_at (location, "template with C linkage");
31669 maybe_show_extern_c_location ();
31670 /* Give it C++ linkage to avoid confusing other parts of the
31671 front end. */
31672 push_lang_context (lang_name_cplusplus);
31673 need_lang_pop = true;
31675 else
31676 need_lang_pop = false;
31678 /* We cannot perform access checks on the template parameter
31679 declarations until we know what is being declared, just as we
31680 cannot check the decl-specifier list. */
31681 push_deferring_access_checks (dk_deferred);
31683 /* If the next token is `>', then we have an invalid
31684 specialization. Rather than complain about an invalid template
31685 parameter, issue an error message here. */
31686 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
31688 cp_parser_error (parser, "invalid explicit specialization");
31689 begin_specialization ();
31690 parameter_list = NULL_TREE;
31692 else
31694 /* Parse the template parameters. */
31695 parameter_list = cp_parser_template_parameter_list (parser);
31698 /* Look for the `>'. */
31699 cp_parser_require_end_of_template_parameter_list (parser);
31701 /* Manage template requirements */
31702 if (flag_concepts)
31704 tree reqs = get_shorthand_constraints (current_template_parms);
31705 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
31706 reqs = combine_constraint_expressions (reqs, treqs);
31707 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
31710 cp_parser_template_declaration_after_parameters (parser, parameter_list,
31711 member_p);
31713 /* For the erroneous case of a template with C linkage, we pushed an
31714 implicit C++ linkage scope; exit that scope now. */
31715 if (need_lang_pop)
31716 pop_lang_context ();
31719 /* Parse a template-declaration, assuming that the `export' (and
31720 `extern') keywords, if present, has already been scanned. MEMBER_P
31721 is as for cp_parser_template_declaration. */
31723 static bool
31724 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
31726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31728 cp_lexer_consume_token (parser->lexer);
31729 cp_parser_explicit_template_declaration (parser, member_p);
31730 return true;
31732 else if (flag_concepts)
31733 return cp_parser_template_introduction (parser, member_p);
31735 return false;
31738 /* Perform the deferred access checks from a template-parameter-list.
31739 CHECKS is a TREE_LIST of access checks, as returned by
31740 get_deferred_access_checks. */
31742 static void
31743 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
31745 ++processing_template_parmlist;
31746 perform_access_checks (checks, tf_warning_or_error);
31747 --processing_template_parmlist;
31750 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
31751 `function-definition' sequence that follows a template header.
31752 If MEMBER_P is true, this declaration appears in a class scope.
31754 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
31755 *FRIEND_P is set to TRUE iff the declaration is a friend. */
31757 static tree
31758 cp_parser_single_declaration (cp_parser* parser,
31759 vec<deferred_access_check, va_gc> *checks,
31760 bool member_p,
31761 bool explicit_specialization_p,
31762 bool* friend_p)
31764 int declares_class_or_enum;
31765 tree decl = NULL_TREE;
31766 cp_decl_specifier_seq decl_specifiers;
31767 bool function_definition_p = false;
31768 cp_token *decl_spec_token_start;
31770 /* This function is only used when processing a template
31771 declaration. */
31772 gcc_assert (innermost_scope_kind () == sk_template_parms
31773 || innermost_scope_kind () == sk_template_spec);
31775 /* Defer access checks until we know what is being declared. */
31776 push_deferring_access_checks (dk_deferred);
31778 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
31779 alternative. */
31780 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
31781 cp_parser_decl_specifier_seq (parser,
31782 (CP_PARSER_FLAGS_OPTIONAL
31783 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
31784 &decl_specifiers,
31785 &declares_class_or_enum);
31787 cp_omp_declare_simd_data odsd;
31788 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
31789 cp_parser_handle_directive_omp_attributes (parser,
31790 &decl_specifiers.attributes,
31791 &odsd, true);
31793 if (friend_p)
31794 *friend_p = cp_parser_friend_p (&decl_specifiers);
31796 /* There are no template typedefs. */
31797 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
31799 error_at (decl_spec_token_start->location,
31800 "template declaration of %<typedef%>");
31801 decl = error_mark_node;
31804 /* Gather up the access checks that occurred the
31805 decl-specifier-seq. */
31806 stop_deferring_access_checks ();
31808 /* Check for the declaration of a template class. */
31809 if (declares_class_or_enum)
31811 if (cp_parser_declares_only_class_p (parser)
31812 || (declares_class_or_enum & 2))
31814 decl = shadow_tag (&decl_specifiers);
31816 /* In this case:
31818 struct C {
31819 friend template <typename T> struct A<T>::B;
31822 A<T>::B will be represented by a TYPENAME_TYPE, and
31823 therefore not recognized by shadow_tag. */
31824 if (friend_p && *friend_p
31825 && !decl
31826 && decl_specifiers.type
31827 && TYPE_P (decl_specifiers.type))
31828 decl = decl_specifiers.type;
31830 if (decl && decl != error_mark_node)
31831 decl = TYPE_NAME (decl);
31832 else
31833 decl = error_mark_node;
31835 /* If this is a declaration, but not a definition, associate
31836 any constraints with the type declaration. Constraints
31837 are associated with definitions in cp_parser_class_specifier. */
31838 if (declares_class_or_enum == 1)
31839 associate_classtype_constraints (TREE_TYPE (decl));
31841 /* Perform access checks for template parameters. */
31842 cp_parser_perform_template_parameter_access_checks (checks);
31844 /* Give a helpful diagnostic for
31845 template <class T> struct A { } a;
31846 if we aren't already recovering from an error. */
31847 if (!cp_parser_declares_only_class_p (parser)
31848 && !seen_error ())
31850 error_at (cp_lexer_peek_token (parser->lexer)->location,
31851 "a class template declaration must not declare "
31852 "anything else");
31853 cp_parser_skip_to_end_of_block_or_statement (parser);
31854 goto out;
31859 /* Complain about missing 'typename' or other invalid type names. */
31860 if (!decl_specifiers.any_type_specifiers_p
31861 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
31863 /* cp_parser_parse_and_diagnose_invalid_type_name calls
31864 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
31865 the rest of this declaration. */
31866 decl = error_mark_node;
31867 goto out;
31870 /* If it's not a template class, try for a template function. If
31871 the next token is a `;', then this declaration does not declare
31872 anything. But, if there were errors in the decl-specifiers, then
31873 the error might well have come from an attempted class-specifier.
31874 In that case, there's no need to warn about a missing declarator. */
31875 if (!decl
31876 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
31877 || decl_specifiers.type != error_mark_node))
31879 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
31880 /* We don't delay parsing for friends, though CWG 2510 may change
31881 that. */
31882 if (member_p && !(friend_p && *friend_p))
31883 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
31884 decl = cp_parser_init_declarator (parser,
31885 flags,
31886 &decl_specifiers,
31887 checks,
31888 /*function_definition_allowed_p=*/true,
31889 member_p,
31890 declares_class_or_enum,
31891 &function_definition_p,
31892 NULL, NULL, NULL);
31894 /* 7.1.1-1 [dcl.stc]
31896 A storage-class-specifier shall not be specified in an explicit
31897 specialization... */
31898 if (decl
31899 && explicit_specialization_p
31900 && decl_specifiers.storage_class != sc_none)
31902 error_at (decl_spec_token_start->location,
31903 "explicit template specialization cannot have a storage class");
31904 decl = error_mark_node;
31907 if (decl && VAR_P (decl))
31908 check_template_variable (decl);
31911 /* Look for a trailing `;' after the declaration. */
31912 if (!function_definition_p
31913 && (decl == error_mark_node
31914 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
31915 cp_parser_skip_to_end_of_block_or_statement (parser);
31917 out:
31918 pop_deferring_access_checks ();
31920 /* Clear any current qualification; whatever comes next is the start
31921 of something new. */
31922 parser->scope = NULL_TREE;
31923 parser->qualifying_scope = NULL_TREE;
31924 parser->object_scope = NULL_TREE;
31926 cp_finalize_omp_declare_simd (parser, &odsd);
31928 return decl;
31931 /* Parse a cast-expression that is not the operand of a unary "&". */
31933 static cp_expr
31934 cp_parser_simple_cast_expression (cp_parser *parser)
31936 return cp_parser_cast_expression (parser, /*address_p=*/false,
31937 /*cast_p=*/false, /*decltype*/false, NULL);
31940 /* Parse a functional cast to TYPE. Returns an expression
31941 representing the cast. */
31943 static cp_expr
31944 cp_parser_functional_cast (cp_parser* parser, tree type)
31946 vec<tree, va_gc> *vec;
31947 tree expression_list;
31948 cp_expr cast;
31949 bool nonconst_p;
31951 location_t start_loc = input_location;
31953 if (!type)
31954 type = error_mark_node;
31956 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31958 cp_lexer_set_source_position (parser->lexer);
31959 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
31960 expression_list = cp_parser_braced_list (parser, &nonconst_p);
31961 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
31962 if (TREE_CODE (type) == TYPE_DECL)
31963 type = TREE_TYPE (type);
31965 cast = finish_compound_literal (type, expression_list,
31966 tf_warning_or_error, fcl_functional);
31967 /* Create a location of the form:
31968 type_name{i, f}
31969 ^~~~~~~~~~~~~~~
31970 with caret == start at the start of the type name,
31971 finishing at the closing brace. */
31972 location_t combined_loc = make_location (start_loc, start_loc,
31973 parser->lexer);
31974 cast.set_location (combined_loc);
31975 return cast;
31979 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
31980 /*cast_p=*/true,
31981 /*allow_expansion_p=*/true,
31982 /*non_constant_p=*/NULL);
31983 if (vec == NULL)
31984 expression_list = error_mark_node;
31985 else
31987 expression_list = build_tree_list_vec (vec);
31988 release_tree_vector (vec);
31991 /* Create a location of the form:
31992 float(i)
31993 ^~~~~~~~
31994 with caret == start at the start of the type name,
31995 finishing at the closing paren. */
31996 location_t combined_loc = make_location (start_loc, start_loc,
31997 parser->lexer);
31998 cast = build_functional_cast (combined_loc, type, expression_list,
31999 tf_warning_or_error);
32001 /* [expr.const]/1: In an integral constant expression "only type
32002 conversions to integral or enumeration type can be used". */
32003 if (TREE_CODE (type) == TYPE_DECL)
32004 type = TREE_TYPE (type);
32005 if (cast != error_mark_node
32006 && !cast_valid_in_integral_constant_expression_p (type)
32007 && cp_parser_non_integral_constant_expression (parser,
32008 NIC_CONSTRUCTOR))
32009 return error_mark_node;
32011 return cast;
32014 /* Save the tokens that make up the body of a member function defined
32015 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
32016 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
32017 specifiers applied to the declaration. Returns the FUNCTION_DECL
32018 for the member function. */
32020 static tree
32021 cp_parser_save_member_function_body (cp_parser* parser,
32022 cp_decl_specifier_seq *decl_specifiers,
32023 cp_declarator *declarator,
32024 tree attributes)
32026 cp_token *first;
32027 cp_token *last;
32028 tree fn;
32029 bool function_try_block = false;
32031 /* Create the FUNCTION_DECL. */
32032 fn = grokmethod (decl_specifiers, declarator, attributes);
32033 cp_finalize_omp_declare_simd (parser, fn);
32034 cp_finalize_oacc_routine (parser, fn, true);
32035 /* If something went badly wrong, bail out now. */
32036 if (fn == error_mark_node)
32038 /* If there's a function-body, skip it. */
32039 if (cp_parser_token_starts_function_definition_p
32040 (cp_lexer_peek_token (parser->lexer)))
32041 cp_parser_skip_to_end_of_block_or_statement (parser);
32042 return error_mark_node;
32045 /* Remember it, if there are default args to post process. */
32046 cp_parser_save_default_args (parser, fn);
32048 /* Save away the tokens that make up the body of the
32049 function. */
32050 first = parser->lexer->next_token;
32052 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
32053 cp_lexer_consume_token (parser->lexer);
32054 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32055 RID_TRANSACTION_ATOMIC))
32057 cp_lexer_consume_token (parser->lexer);
32058 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
32059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
32060 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
32061 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
32062 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
32063 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
32064 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
32066 cp_lexer_consume_token (parser->lexer);
32067 cp_lexer_consume_token (parser->lexer);
32068 cp_lexer_consume_token (parser->lexer);
32069 cp_lexer_consume_token (parser->lexer);
32070 cp_lexer_consume_token (parser->lexer);
32072 else
32073 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
32074 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32076 cp_lexer_consume_token (parser->lexer);
32077 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32078 break;
32082 /* Handle function try blocks. */
32083 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32085 cp_lexer_consume_token (parser->lexer);
32086 function_try_block = true;
32088 /* We can have braced-init-list mem-initializers before the fn body. */
32089 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32091 cp_lexer_consume_token (parser->lexer);
32092 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
32094 /* cache_group will stop after an un-nested { } pair, too. */
32095 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32096 break;
32098 /* variadic mem-inits have ... after the ')'. */
32099 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32100 cp_lexer_consume_token (parser->lexer);
32103 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32104 /* Handle function try blocks. */
32105 if (function_try_block)
32106 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
32107 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32108 last = parser->lexer->next_token;
32110 /* Save away the inline definition; we will process it when the
32111 class is complete. */
32112 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
32113 DECL_PENDING_INLINE_P (fn) = 1;
32115 /* We need to know that this was defined in the class, so that
32116 friend templates are handled correctly. */
32117 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
32119 /* Add FN to the queue of functions to be parsed later. */
32120 vec_safe_push (unparsed_funs_with_definitions, fn);
32122 return fn;
32125 /* Save the tokens that make up the in-class initializer for a non-static
32126 data member. Returns a DEFERRED_PARSE. */
32128 static tree
32129 cp_parser_save_nsdmi (cp_parser* parser)
32131 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
32134 /* Parse a template-argument-list, as well as the trailing ">" (but
32135 not the opening "<"). See cp_parser_template_argument_list for the
32136 return value. */
32138 static tree
32139 cp_parser_enclosed_template_argument_list (cp_parser* parser)
32141 tree arguments;
32142 tree saved_scope;
32143 tree saved_qualifying_scope;
32144 tree saved_object_scope;
32145 bool saved_greater_than_is_operator_p;
32147 /* [temp.names]
32149 When parsing a template-id, the first non-nested `>' is taken as
32150 the end of the template-argument-list rather than a greater-than
32151 operator. */
32152 saved_greater_than_is_operator_p
32153 = parser->greater_than_is_operator_p;
32154 parser->greater_than_is_operator_p = false;
32155 /* Parsing the argument list may modify SCOPE, so we save it
32156 here. */
32157 saved_scope = parser->scope;
32158 saved_qualifying_scope = parser->qualifying_scope;
32159 saved_object_scope = parser->object_scope;
32160 /* We need to evaluate the template arguments, even though this
32161 template-id may be nested within a "sizeof". */
32162 cp_evaluated ev;
32163 /* Parse the template-argument-list itself. */
32164 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
32165 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
32166 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
32167 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
32169 arguments = make_tree_vec (0);
32170 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
32172 else
32173 arguments = cp_parser_template_argument_list (parser);
32174 /* Look for the `>' that ends the template-argument-list. If we find
32175 a '>>' instead, it's probably just a typo. */
32176 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
32178 if (cxx_dialect != cxx98)
32180 /* In C++0x, a `>>' in a template argument list or cast
32181 expression is considered to be two separate `>'
32182 tokens. So, change the current token to a `>', but don't
32183 consume it: it will be consumed later when the outer
32184 template argument list (or cast expression) is parsed.
32185 Note that this replacement of `>' for `>>' is necessary
32186 even if we are parsing tentatively: in the tentative
32187 case, after calling
32188 cp_parser_enclosed_template_argument_list we will always
32189 throw away all of the template arguments and the first
32190 closing `>', either because the template argument list
32191 was erroneous or because we are replacing those tokens
32192 with a CPP_TEMPLATE_ID token. The second `>' (which will
32193 not have been thrown away) is needed either to close an
32194 outer template argument list or to complete a new-style
32195 cast. */
32196 cp_token *token = cp_lexer_peek_token (parser->lexer);
32197 token->type = CPP_GREATER;
32199 else if (!saved_greater_than_is_operator_p)
32201 /* If we're in a nested template argument list, the '>>' has
32202 to be a typo for '> >'. We emit the error message, but we
32203 continue parsing and we push a '>' as next token, so that
32204 the argument list will be parsed correctly. Note that the
32205 global source location is still on the token before the
32206 '>>', so we need to say explicitly where we want it. */
32207 cp_token *token = cp_lexer_peek_token (parser->lexer);
32208 gcc_rich_location richloc (token->location);
32209 richloc.add_fixit_replace ("> >");
32210 error_at (&richloc, "%<>>%> should be %<> >%> "
32211 "within a nested template argument list");
32213 token->type = CPP_GREATER;
32215 else
32217 /* If this is not a nested template argument list, the '>>'
32218 is a typo for '>'. Emit an error message and continue.
32219 Same deal about the token location, but here we can get it
32220 right by consuming the '>>' before issuing the diagnostic. */
32221 cp_token *token = cp_lexer_consume_token (parser->lexer);
32222 error_at (token->location,
32223 "spurious %<>>%>, use %<>%> to terminate "
32224 "a template argument list");
32227 /* Similarly for >>= and >=. */
32228 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
32229 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
32231 cp_token *token = cp_lexer_consume_token (parser->lexer);
32232 gcc_rich_location richloc (token->location);
32233 enum cpp_ttype new_type;
32234 const char *replacement;
32235 if (token->type == CPP_GREATER_EQ)
32237 replacement = "> =";
32238 new_type = CPP_EQ;
32240 else if (!saved_greater_than_is_operator_p)
32242 if (cxx_dialect != cxx98)
32243 replacement = ">> =";
32244 else
32245 replacement = "> > =";
32246 new_type = CPP_GREATER;
32248 else
32250 replacement = "> >=";
32251 new_type = CPP_GREATER_EQ;
32253 richloc.add_fixit_replace (replacement);
32254 error_at (&richloc, "%qs should be %qs to terminate a template "
32255 "argument list",
32256 cpp_type2name (token->type, token->flags), replacement);
32257 token->type = new_type;
32259 else
32260 cp_parser_require_end_of_template_parameter_list (parser);
32261 /* The `>' token might be a greater-than operator again now. */
32262 parser->greater_than_is_operator_p
32263 = saved_greater_than_is_operator_p;
32264 /* Restore the SAVED_SCOPE. */
32265 parser->scope = saved_scope;
32266 parser->qualifying_scope = saved_qualifying_scope;
32267 parser->object_scope = saved_object_scope;
32269 return arguments;
32272 /* MEMBER_FUNCTION is a member function, or a friend. If default
32273 arguments, or the body of the function have not yet been parsed,
32274 parse them now. */
32276 static void
32277 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
32279 timevar_push (TV_PARSE_INMETH);
32280 /* If this member is a template, get the underlying
32281 FUNCTION_DECL. */
32282 if (DECL_FUNCTION_TEMPLATE_P (member_function))
32283 member_function = DECL_TEMPLATE_RESULT (member_function);
32285 /* There should not be any class definitions in progress at this
32286 point; the bodies of members are only parsed outside of all class
32287 definitions. */
32288 gcc_assert (parser->num_classes_being_defined == 0);
32289 /* While we're parsing the member functions we might encounter more
32290 classes. We want to handle them right away, but we don't want
32291 them getting mixed up with functions that are currently in the
32292 queue. */
32293 push_unparsed_function_queues (parser);
32295 /* Make sure that any template parameters are in scope. */
32296 maybe_begin_member_template_processing (member_function);
32298 /* If the body of the function has not yet been parsed, parse it
32299 now. Except if the tokens have been purged (PR c++/39751). */
32300 if (DECL_PENDING_INLINE_P (member_function)
32301 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
32303 tree function_scope;
32304 cp_token_cache *tokens;
32306 /* The function is no longer pending; we are processing it. */
32307 tokens = DECL_PENDING_INLINE_INFO (member_function);
32308 DECL_PENDING_INLINE_INFO (member_function) = NULL;
32309 DECL_PENDING_INLINE_P (member_function) = 0;
32311 /* If this is a local class, enter the scope of the containing
32312 function. */
32313 function_scope = current_function_decl;
32314 if (function_scope)
32315 push_function_context ();
32317 /* Push the body of the function onto the lexer stack. */
32318 cp_parser_push_lexer_for_tokens (parser, tokens);
32320 /* Let the front end know that we going to be defining this
32321 function. */
32322 start_preparsed_function (member_function, NULL_TREE,
32323 SF_PRE_PARSED | SF_INCLASS_INLINE);
32325 /* #pragma omp declare reduction needs special parsing. */
32326 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
32328 parser->lexer->in_pragma = true;
32329 cp_parser_omp_declare_reduction_exprs (member_function, parser);
32330 finish_function (/*inline_p=*/true);
32331 cp_check_omp_declare_reduction (member_function);
32333 else
32334 /* Now, parse the body of the function. */
32335 cp_parser_function_definition_after_declarator (parser,
32336 /*inline_p=*/true);
32338 /* Leave the scope of the containing function. */
32339 if (function_scope)
32340 pop_function_context ();
32341 cp_parser_pop_lexer (parser);
32344 /* Remove any template parameters from the symbol table. */
32345 maybe_end_member_template_processing ();
32347 /* Restore the queue. */
32348 pop_unparsed_function_queues (parser);
32349 timevar_pop (TV_PARSE_INMETH);
32352 /* If DECL contains any default args, remember it on the unparsed
32353 functions queue. */
32355 static void
32356 cp_parser_save_default_args (cp_parser* parser, tree decl)
32358 tree probe;
32360 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
32361 probe;
32362 probe = TREE_CHAIN (probe))
32363 if (TREE_PURPOSE (probe))
32365 cp_default_arg_entry entry = {current_class_type, decl};
32366 vec_safe_push (unparsed_funs_with_default_args, entry);
32367 break;
32370 /* Remember if there is a noexcept-specifier to post process. */
32371 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
32372 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
32373 vec_safe_push (unparsed_noexcepts, decl);
32376 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
32377 which is either a FIELD_DECL or PARM_DECL. Parse it and return
32378 the result. For a PARM_DECL, PARMTYPE is the corresponding type
32379 from the parameter-type-list. */
32381 static tree
32382 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
32383 tree default_arg, tree parmtype)
32385 cp_token_cache *tokens;
32386 tree parsed_arg;
32387 bool dummy;
32389 if (default_arg == error_mark_node)
32390 return error_mark_node;
32392 /* Push the saved tokens for the default argument onto the parser's
32393 lexer stack. */
32394 tokens = DEFPARSE_TOKENS (default_arg);
32395 cp_parser_push_lexer_for_tokens (parser, tokens);
32397 start_lambda_scope (decl);
32399 /* Parse the default argument. */
32400 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
32401 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
32402 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32404 finish_lambda_scope ();
32406 if (parsed_arg == error_mark_node)
32407 cp_parser_skip_to_end_of_statement (parser);
32409 if (!processing_template_decl)
32411 /* In a non-template class, check conversions now. In a template,
32412 we'll wait and instantiate these as needed. */
32413 if (TREE_CODE (decl) == PARM_DECL)
32414 parsed_arg = check_default_argument (parmtype, parsed_arg,
32415 tf_warning_or_error);
32416 else if (maybe_reject_flexarray_init (decl, parsed_arg))
32417 parsed_arg = error_mark_node;
32418 else
32419 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
32422 /* If the token stream has not been completely used up, then
32423 there was extra junk after the end of the default
32424 argument. */
32425 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
32427 if (TREE_CODE (decl) == PARM_DECL)
32428 cp_parser_error (parser, "expected %<,%>");
32429 else
32430 cp_parser_error (parser, "expected %<;%>");
32433 /* Revert to the main lexer. */
32434 cp_parser_pop_lexer (parser);
32436 return parsed_arg;
32439 /* FIELD is a non-static data member with an initializer which we saved for
32440 later; parse it now. */
32442 static void
32443 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
32445 tree def;
32447 maybe_begin_member_template_processing (field);
32449 push_unparsed_function_queues (parser);
32450 def = cp_parser_late_parse_one_default_arg (parser, field,
32451 DECL_INITIAL (field),
32452 NULL_TREE);
32453 pop_unparsed_function_queues (parser);
32455 maybe_end_member_template_processing ();
32457 DECL_INITIAL (field) = def;
32460 /* FN is a FUNCTION_DECL which may contains a parameter with an
32461 unparsed DEFERRED_PARSE. Parse the default args now. This function
32462 assumes that the current scope is the scope in which the default
32463 argument should be processed. */
32465 static void
32466 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
32468 unsigned char saved_local_variables_forbidden_p;
32470 /* While we're parsing the default args, we might (due to the
32471 statement expression extension) encounter more classes. We want
32472 to handle them right away, but we don't want them getting mixed
32473 up with default args that are currently in the queue. */
32474 push_unparsed_function_queues (parser);
32476 /* Local variable names (and the `this' keyword) may not appear
32477 in a default argument. */
32478 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
32479 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
32481 push_defarg_context (fn);
32483 begin_scope (sk_function_parms, fn);
32485 /* Gather the PARM_DECLs into a vec so we can keep track of them when
32486 pushdecl clears DECL_CHAIN. */
32487 releasing_vec parms;
32488 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
32489 parmdecl = DECL_CHAIN (parmdecl))
32490 vec_safe_push (parms, parmdecl);
32492 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
32493 for (int i = 0;
32494 parm && parm != void_list_node;
32495 parm = TREE_CHAIN (parm),
32496 ++i)
32498 tree default_arg = TREE_PURPOSE (parm);
32499 tree parsed_arg;
32501 tree parmdecl = parms[i];
32502 pushdecl (parmdecl);
32504 if (!default_arg)
32505 continue;
32507 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
32508 /* This can happen for a friend declaration for a function
32509 already declared with default arguments. */
32510 continue;
32512 parsed_arg
32513 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
32514 default_arg,
32515 TREE_VALUE (parm));
32516 TREE_PURPOSE (parm) = parsed_arg;
32518 /* Update any instantiations we've already created. */
32519 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
32520 TREE_PURPOSE (copy) = parsed_arg;
32523 pop_bindings_and_leave_scope ();
32525 /* Restore DECL_CHAINs after clobbering by pushdecl. */
32526 parm = NULL_TREE;
32527 for (int i = parms->length () - 1; i >= 0; --i)
32529 DECL_CHAIN (parms[i]) = parm;
32530 parm = parms[i];
32533 pop_defarg_context ();
32535 /* Make sure no default arg is missing. */
32536 check_default_args (fn);
32538 /* Restore the state of local_variables_forbidden_p. */
32539 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
32541 /* Restore the queue. */
32542 pop_unparsed_function_queues (parser);
32545 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
32547 sizeof ... ( identifier )
32549 where the 'sizeof' token has already been consumed. */
32551 static tree
32552 cp_parser_sizeof_pack (cp_parser *parser)
32554 /* Consume the `...'. */
32555 cp_lexer_consume_token (parser->lexer);
32556 maybe_warn_variadic_templates ();
32558 matching_parens parens;
32559 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
32560 if (paren)
32561 parens.consume_open (parser);
32562 else
32563 permerror (cp_lexer_peek_token (parser->lexer)->location,
32564 "%<sizeof...%> argument must be surrounded by parentheses");
32566 cp_token *token = cp_lexer_peek_token (parser->lexer);
32567 tree name = cp_parser_identifier (parser);
32568 if (name == error_mark_node)
32569 return error_mark_node;
32570 /* The name is not qualified. */
32571 parser->scope = NULL_TREE;
32572 parser->qualifying_scope = NULL_TREE;
32573 parser->object_scope = NULL_TREE;
32574 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
32575 if (expr == error_mark_node)
32576 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
32577 token->location);
32578 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
32579 expr = TREE_TYPE (expr);
32580 else if (TREE_CODE (expr) == CONST_DECL)
32581 expr = DECL_INITIAL (expr);
32582 expr = make_pack_expansion (expr);
32583 if (expr != error_mark_node)
32584 PACK_EXPANSION_SIZEOF_P (expr) = true;
32586 if (paren)
32587 parens.require_close (parser);
32589 return expr;
32592 /* Parse the operand of `sizeof' (or a similar operator). Returns
32593 either a TYPE or an expression, depending on the form of the
32594 input. The KEYWORD indicates which kind of expression we have
32595 encountered. */
32597 static tree
32598 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
32600 tree expr = NULL_TREE;
32601 const char *saved_message;
32602 const char *saved_message_arg;
32603 bool saved_integral_constant_expression_p;
32604 bool saved_non_integral_constant_expression_p;
32606 /* If it's a `...', then we are computing the length of a parameter
32607 pack. */
32608 if (keyword == RID_SIZEOF
32609 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32610 return cp_parser_sizeof_pack (parser);
32612 /* Types cannot be defined in a `sizeof' expression. Save away the
32613 old message. */
32614 saved_message = parser->type_definition_forbidden_message;
32615 saved_message_arg = parser->type_definition_forbidden_message_arg;
32616 parser->type_definition_forbidden_message
32617 = G_("types may not be defined in %qs expressions");
32618 parser->type_definition_forbidden_message_arg
32619 = IDENTIFIER_POINTER (ridpointers[keyword]);
32621 /* The restrictions on constant-expressions do not apply inside
32622 sizeof expressions. */
32623 saved_integral_constant_expression_p
32624 = parser->integral_constant_expression_p;
32625 saved_non_integral_constant_expression_p
32626 = parser->non_integral_constant_expression_p;
32627 parser->integral_constant_expression_p = false;
32629 auto cleanup = make_temp_override
32630 (parser->auto_is_implicit_function_template_parm_p, false);
32632 /* Do not actually evaluate the expression. */
32633 ++cp_unevaluated_operand;
32634 ++c_inhibit_evaluation_warnings;
32635 /* If it's a `(', then we might be looking at the type-id
32636 construction. */
32637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32639 tree type = NULL_TREE;
32641 tentative_firewall firewall (parser);
32643 /* We can't be sure yet whether we're looking at a type-id or an
32644 expression. */
32645 cp_parser_parse_tentatively (parser);
32647 matching_parens parens;
32648 parens.consume_open (parser);
32650 /* Note: as a GNU Extension, compound literals are considered
32651 postfix-expressions as they are in C99, so they are valid
32652 arguments to sizeof. See comment in cp_parser_cast_expression
32653 for details. */
32654 if (cp_parser_compound_literal_p (parser))
32655 cp_parser_simulate_error (parser);
32656 else
32658 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
32659 parser->in_type_id_in_expr_p = true;
32660 /* Look for the type-id. */
32661 type = cp_parser_type_id (parser);
32662 /* Look for the closing `)'. */
32663 parens.require_close (parser);
32664 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
32667 /* If all went well, then we're done. */
32668 if (cp_parser_parse_definitely (parser))
32669 expr = type;
32670 else
32672 /* Commit to the tentative_firewall so we get syntax errors. */
32673 cp_parser_commit_to_tentative_parse (parser);
32675 expr = cp_parser_unary_expression (parser);
32678 else
32679 expr = cp_parser_unary_expression (parser);
32681 /* Go back to evaluating expressions. */
32682 --cp_unevaluated_operand;
32683 --c_inhibit_evaluation_warnings;
32685 /* And restore the old one. */
32686 parser->type_definition_forbidden_message = saved_message;
32687 parser->type_definition_forbidden_message_arg = saved_message_arg;
32688 parser->integral_constant_expression_p
32689 = saved_integral_constant_expression_p;
32690 parser->non_integral_constant_expression_p
32691 = saved_non_integral_constant_expression_p;
32693 return expr;
32696 /* If the current declaration has no declarator, return true. */
32698 static bool
32699 cp_parser_declares_only_class_p (cp_parser *parser)
32701 /* If the next token is a `;' or a `,' then there is no
32702 declarator. */
32703 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32704 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
32707 /* Update the DECL_SPECS to reflect the storage class indicated by
32708 KEYWORD. */
32710 static void
32711 cp_parser_set_storage_class (cp_parser *parser,
32712 cp_decl_specifier_seq *decl_specs,
32713 enum rid keyword,
32714 cp_token *token)
32716 cp_storage_class storage_class;
32718 if (parser->in_unbraced_linkage_specification_p)
32720 error_at (token->location, "invalid use of %qD in linkage specification",
32721 ridpointers[keyword]);
32722 return;
32724 else if (decl_specs->storage_class != sc_none)
32726 decl_specs->conflicting_specifiers_p = true;
32727 return;
32730 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
32731 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
32732 && decl_specs->gnu_thread_keyword_p)
32734 pedwarn (decl_specs->locations[ds_thread], 0,
32735 "%<__thread%> before %qD", ridpointers[keyword]);
32738 switch (keyword)
32740 case RID_AUTO:
32741 storage_class = sc_auto;
32742 break;
32743 case RID_REGISTER:
32744 storage_class = sc_register;
32745 break;
32746 case RID_STATIC:
32747 storage_class = sc_static;
32748 break;
32749 case RID_EXTERN:
32750 storage_class = sc_extern;
32751 break;
32752 case RID_MUTABLE:
32753 storage_class = sc_mutable;
32754 break;
32755 default:
32756 gcc_unreachable ();
32758 decl_specs->storage_class = storage_class;
32759 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
32761 /* A storage class specifier cannot be applied alongside a typedef
32762 specifier. If there is a typedef specifier present then set
32763 conflicting_specifiers_p which will trigger an error later
32764 on in grokdeclarator. */
32765 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
32766 decl_specs->conflicting_specifiers_p = true;
32769 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
32770 is true, the type is a class or enum definition. */
32772 static void
32773 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
32774 tree type_spec,
32775 cp_token *token,
32776 bool type_definition_p)
32778 decl_specs->any_specifiers_p = true;
32780 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
32781 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
32782 this is what happened. In system headers, we ignore these
32783 declarations so that G++ can work with system headers that are not
32784 C++-safe. */
32785 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
32786 && !type_definition_p
32787 && (type_spec == boolean_type_node
32788 || type_spec == char8_type_node
32789 || type_spec == char16_type_node
32790 || type_spec == char32_type_node
32791 || type_spec == wchar_type_node)
32792 && (decl_specs->type
32793 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
32794 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
32795 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
32796 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
32798 decl_specs->redefined_builtin_type = type_spec;
32799 set_and_check_decl_spec_loc (decl_specs,
32800 ds_redefined_builtin_type_spec,
32801 token);
32802 if (!decl_specs->type)
32804 decl_specs->type = type_spec;
32805 decl_specs->type_definition_p = false;
32806 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
32809 else if (decl_specs->type)
32810 decl_specs->multiple_types_p = true;
32811 else
32813 decl_specs->type = type_spec;
32814 decl_specs->type_definition_p = type_definition_p;
32815 decl_specs->redefined_builtin_type = NULL_TREE;
32816 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
32820 /* True iff TOKEN is the GNU keyword __thread. */
32822 static bool
32823 token_is__thread (cp_token *token)
32825 gcc_assert (token->keyword == RID_THREAD);
32826 return id_equal (token->u.value, "__thread");
32829 /* Set the location for a declarator specifier and check if it is
32830 duplicated.
32832 DECL_SPECS is the sequence of declarator specifiers onto which to
32833 set the location.
32835 DS is the single declarator specifier to set which location is to
32836 be set onto the existing sequence of declarators.
32838 LOCATION is the location for the declarator specifier to
32839 consider. */
32841 static void
32842 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
32843 cp_decl_spec ds, cp_token *token)
32845 gcc_assert (ds < ds_last);
32847 if (decl_specs == NULL)
32848 return;
32850 location_t location = token->location;
32852 if (decl_specs->locations[ds] == 0)
32854 decl_specs->locations[ds] = location;
32855 if (ds == ds_thread)
32856 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
32858 else
32860 if (ds == ds_long)
32862 if (decl_specs->locations[ds_long_long] != 0)
32863 error_at (location,
32864 "%<long long long%> is too long for GCC");
32865 else
32867 decl_specs->locations[ds_long_long] = location;
32868 pedwarn_cxx98 (location,
32869 OPT_Wlong_long,
32870 "ISO C++ 1998 does not support %<long long%>");
32873 else if (ds == ds_thread)
32875 bool gnu = token_is__thread (token);
32876 gcc_rich_location richloc (location);
32877 if (gnu != decl_specs->gnu_thread_keyword_p)
32879 richloc.add_range (decl_specs->locations[ds_thread]);
32880 error_at (&richloc,
32881 "both %<__thread%> and %<thread_local%> specified");
32883 else
32885 richloc.add_fixit_remove ();
32886 error_at (&richloc, "duplicate %qD", token->u.value);
32889 else
32891 static const char *const decl_spec_names[] = {
32892 "signed",
32893 "unsigned",
32894 "short",
32895 "long",
32896 "const",
32897 "volatile",
32898 "restrict",
32899 "inline",
32900 "virtual",
32901 "explicit",
32902 "friend",
32903 "typedef",
32904 "using",
32905 "constexpr",
32906 "__complex",
32907 "constinit",
32908 "consteval"
32910 gcc_rich_location richloc (location);
32911 richloc.add_fixit_remove ();
32912 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
32917 /* Return true iff the declarator specifier DS is present in the
32918 sequence of declarator specifiers DECL_SPECS. */
32920 bool
32921 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
32922 cp_decl_spec ds)
32924 gcc_assert (ds < ds_last);
32926 if (decl_specs == NULL)
32927 return false;
32929 return decl_specs->locations[ds] != 0;
32932 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
32933 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
32935 static bool
32936 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
32938 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
32941 /* Issue an error message indicating that TOKEN_DESC was expected.
32942 If KEYWORD is true, it indicated this function is called by
32943 cp_parser_require_keword and the required token can only be
32944 a indicated keyword.
32946 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32947 within any error as the location of an "opening" token matching
32948 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32949 RT_CLOSE_PAREN). */
32951 static void
32952 cp_parser_required_error (cp_parser *parser,
32953 required_token token_desc,
32954 bool keyword,
32955 location_t matching_location)
32957 if (cp_parser_simulate_error (parser))
32958 return;
32960 const char *gmsgid = NULL;
32961 switch (token_desc)
32963 case RT_NEW:
32964 gmsgid = G_("expected %<new%>");
32965 break;
32966 case RT_DELETE:
32967 gmsgid = G_("expected %<delete%>");
32968 break;
32969 case RT_RETURN:
32970 gmsgid = G_("expected %<return%>");
32971 break;
32972 case RT_WHILE:
32973 gmsgid = G_("expected %<while%>");
32974 break;
32975 case RT_EXTERN:
32976 gmsgid = G_("expected %<extern%>");
32977 break;
32978 case RT_STATIC_ASSERT:
32979 gmsgid = G_("expected %<static_assert%>");
32980 break;
32981 case RT_DECLTYPE:
32982 gmsgid = G_("expected %<decltype%>");
32983 break;
32984 case RT_OPERATOR:
32985 gmsgid = G_("expected %<operator%>");
32986 break;
32987 case RT_CLASS:
32988 gmsgid = G_("expected %<class%>");
32989 break;
32990 case RT_TEMPLATE:
32991 gmsgid = G_("expected %<template%>");
32992 break;
32993 case RT_NAMESPACE:
32994 gmsgid = G_("expected %<namespace%>");
32995 break;
32996 case RT_USING:
32997 gmsgid = G_("expected %<using%>");
32998 break;
32999 case RT_ASM:
33000 gmsgid = G_("expected %<asm%>");
33001 break;
33002 case RT_TRY:
33003 gmsgid = G_("expected %<try%>");
33004 break;
33005 case RT_CATCH:
33006 gmsgid = G_("expected %<catch%>");
33007 break;
33008 case RT_THROW:
33009 gmsgid = G_("expected %<throw%>");
33010 break;
33011 case RT_AUTO:
33012 gmsgid = G_("expected %<auto%>");
33013 break;
33014 case RT_LABEL:
33015 gmsgid = G_("expected %<__label__%>");
33016 break;
33017 case RT_AT_TRY:
33018 gmsgid = G_("expected %<@try%>");
33019 break;
33020 case RT_AT_SYNCHRONIZED:
33021 gmsgid = G_("expected %<@synchronized%>");
33022 break;
33023 case RT_AT_THROW:
33024 gmsgid = G_("expected %<@throw%>");
33025 break;
33026 case RT_TRANSACTION_ATOMIC:
33027 gmsgid = G_("expected %<__transaction_atomic%>");
33028 break;
33029 case RT_TRANSACTION_RELAXED:
33030 gmsgid = G_("expected %<__transaction_relaxed%>");
33031 break;
33032 case RT_CO_YIELD:
33033 gmsgid = G_("expected %<co_yield%>");
33034 break;
33035 default:
33036 break;
33039 if (!gmsgid && !keyword)
33041 switch (token_desc)
33043 case RT_SEMICOLON:
33044 gmsgid = G_("expected %<;%>");
33045 break;
33046 case RT_OPEN_PAREN:
33047 gmsgid = G_("expected %<(%>");
33048 break;
33049 case RT_CLOSE_BRACE:
33050 gmsgid = G_("expected %<}%>");
33051 break;
33052 case RT_OPEN_BRACE:
33053 gmsgid = G_("expected %<{%>");
33054 break;
33055 case RT_CLOSE_SQUARE:
33056 gmsgid = G_("expected %<]%>");
33057 break;
33058 case RT_OPEN_SQUARE:
33059 gmsgid = G_("expected %<[%>");
33060 break;
33061 case RT_COMMA:
33062 gmsgid = G_("expected %<,%>");
33063 break;
33064 case RT_SCOPE:
33065 gmsgid = G_("expected %<::%>");
33066 break;
33067 case RT_LESS:
33068 gmsgid = G_("expected %<<%>");
33069 break;
33070 case RT_GREATER:
33071 gmsgid = G_("expected %<>%>");
33072 break;
33073 case RT_EQ:
33074 gmsgid = G_("expected %<=%>");
33075 break;
33076 case RT_ELLIPSIS:
33077 gmsgid = G_("expected %<...%>");
33078 break;
33079 case RT_MULT:
33080 gmsgid = G_("expected %<*%>");
33081 break;
33082 case RT_COMPL:
33083 gmsgid = G_("expected %<~%>");
33084 break;
33085 case RT_COLON:
33086 gmsgid = G_("expected %<:%>");
33087 break;
33088 case RT_COLON_SCOPE:
33089 gmsgid = G_("expected %<:%> or %<::%>");
33090 break;
33091 case RT_CLOSE_PAREN:
33092 gmsgid = G_("expected %<)%>");
33093 break;
33094 case RT_COMMA_CLOSE_PAREN:
33095 gmsgid = G_("expected %<,%> or %<)%>");
33096 break;
33097 case RT_PRAGMA_EOL:
33098 gmsgid = G_("expected end of line");
33099 break;
33100 case RT_NAME:
33101 gmsgid = G_("expected identifier");
33102 break;
33103 case RT_SELECT:
33104 gmsgid = G_("expected selection-statement");
33105 break;
33106 case RT_ITERATION:
33107 gmsgid = G_("expected iteration-statement");
33108 break;
33109 case RT_JUMP:
33110 gmsgid = G_("expected jump-statement");
33111 break;
33112 case RT_CLASS_KEY:
33113 gmsgid = G_("expected class-key");
33114 break;
33115 case RT_CLASS_TYPENAME_TEMPLATE:
33116 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
33117 break;
33118 default:
33119 gcc_unreachable ();
33123 if (gmsgid)
33124 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
33128 /* If the next token is of the indicated TYPE, consume it. Otherwise,
33129 issue an error message indicating that TOKEN_DESC was expected.
33131 Returns the token consumed, if the token had the appropriate type.
33132 Otherwise, returns NULL.
33134 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33135 within any error as the location of an "opening" token matching
33136 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33137 RT_CLOSE_PAREN). */
33139 static cp_token *
33140 cp_parser_require (cp_parser* parser,
33141 enum cpp_ttype type,
33142 required_token token_desc,
33143 location_t matching_location)
33145 if (cp_lexer_next_token_is (parser->lexer, type))
33146 return cp_lexer_consume_token (parser->lexer);
33147 else
33149 /* Output the MESSAGE -- unless we're parsing tentatively. */
33150 if (!cp_parser_simulate_error (parser))
33151 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
33152 matching_location);
33153 return NULL;
33157 /* Skip an entire parameter list from start to finish. The next token must
33158 be the initial "<" of the parameter list. Returns true on success and
33159 false otherwise. */
33161 static bool
33162 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
33164 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
33165 requires it. */
33166 cp_lexer_consume_token (parser->lexer);
33167 return cp_parser_skip_to_end_of_template_parameter_list (parser);
33170 /* Ensure we are at the end of a template parameter list. If we are, return.
33171 If we are not, something has gone wrong, in which case issue an error and
33172 skip to end of the parameter list. */
33174 static void
33175 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
33177 /* Are we ready, yet? If not, issue error message. */
33178 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
33179 return;
33181 cp_parser_skip_to_end_of_template_parameter_list (parser);
33184 /* You should only call this function from inside a template parameter list
33185 (i.e. the current token should at least be the initial "<" of the
33186 parameter list). If you are skipping the entire list, it may be better to
33187 use cp_parser_skip_entire_template_parameter_list.
33189 Tokens are skipped until the final ">" is found, or if we see
33190 '{', '}', ';', or if we find an unbalanced ')' or ']'.
33192 Returns true if we successfully reached the end, and false if
33193 something unexpected happened (e.g. end of file). */
33195 static bool
33196 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
33198 /* Current level of '< ... >'. */
33199 unsigned level = 0;
33200 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
33201 unsigned nesting_depth = 0;
33203 /* Skip tokens until the desired token is found. */
33204 while (true)
33206 /* Peek at the next token. */
33207 switch (cp_lexer_peek_token (parser->lexer)->type)
33209 case CPP_LESS:
33210 if (!nesting_depth)
33211 ++level;
33212 break;
33214 case CPP_RSHIFT:
33215 if (cxx_dialect == cxx98)
33216 /* C++0x views the `>>' operator as two `>' tokens, but
33217 C++98 does not. */
33218 break;
33219 else if (!nesting_depth && level-- == 0)
33221 /* We've hit a `>>' where the first `>' closes the
33222 template argument list, and the second `>' is
33223 spurious. Just consume the `>>' and stop; we've
33224 already produced at least one error. */
33225 cp_lexer_consume_token (parser->lexer);
33226 return false;
33228 /* Fall through for C++0x, so we handle the second `>' in
33229 the `>>'. */
33230 gcc_fallthrough ();
33232 case CPP_GREATER:
33233 if (!nesting_depth && level-- == 0)
33235 /* We've reached the token we want, consume it and stop. */
33236 cp_lexer_consume_token (parser->lexer);
33237 return true;
33239 break;
33241 case CPP_OPEN_PAREN:
33242 case CPP_OPEN_SQUARE:
33243 ++nesting_depth;
33244 break;
33246 case CPP_CLOSE_PAREN:
33247 case CPP_CLOSE_SQUARE:
33248 if (nesting_depth-- == 0)
33249 return false;
33250 break;
33252 case CPP_EOF:
33253 case CPP_PRAGMA_EOL:
33254 case CPP_SEMICOLON:
33255 case CPP_OPEN_BRACE:
33256 case CPP_CLOSE_BRACE:
33257 /* The '>' was probably forgotten, don't look further. */
33258 return false;
33260 default:
33261 break;
33264 /* Consume this token. */
33265 cp_lexer_consume_token (parser->lexer);
33269 /* If the next token is the indicated keyword, consume it. Otherwise,
33270 issue an error message indicating that TOKEN_DESC was expected.
33272 Returns the token consumed, if the token had the appropriate type.
33273 Otherwise, returns NULL. */
33275 static cp_token *
33276 cp_parser_require_keyword (cp_parser* parser,
33277 enum rid keyword,
33278 required_token token_desc)
33280 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
33282 if (token && token->keyword != keyword)
33284 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
33285 UNKNOWN_LOCATION);
33286 return NULL;
33289 return token;
33292 /* Returns TRUE iff TOKEN is a token that can begin the body of a
33293 function-definition. */
33295 static bool
33296 cp_parser_token_starts_function_definition_p (cp_token* token)
33298 return (/* An ordinary function-body begins with an `{'. */
33299 token->type == CPP_OPEN_BRACE
33300 /* A ctor-initializer begins with a `:'. */
33301 || token->type == CPP_COLON
33302 /* A function-try-block begins with `try'. */
33303 || token->keyword == RID_TRY
33304 /* A function-transaction-block begins with `__transaction_atomic'
33305 or `__transaction_relaxed'. */
33306 || token->keyword == RID_TRANSACTION_ATOMIC
33307 || token->keyword == RID_TRANSACTION_RELAXED
33308 /* The named return value extension begins with `return'. */
33309 || token->keyword == RID_RETURN);
33312 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
33313 definition. */
33315 static bool
33316 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
33318 cp_token *token;
33320 token = cp_lexer_peek_token (parser->lexer);
33321 return (token->type == CPP_OPEN_BRACE
33322 || (token->type == CPP_COLON
33323 && !parser->colon_doesnt_start_class_def_p));
33326 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
33327 C++0x) ending a template-argument. */
33329 static bool
33330 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
33332 cp_token *token;
33334 token = cp_lexer_peek_token (parser->lexer);
33335 return (token->type == CPP_COMMA
33336 || token->type == CPP_GREATER
33337 || token->type == CPP_ELLIPSIS
33338 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
33339 /* For better diagnostics, treat >>= like that too, that
33340 shouldn't appear non-nested in template arguments. */
33341 || token->type == CPP_RSHIFT_EQ);
33344 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
33345 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
33347 static bool
33348 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
33349 size_t n)
33351 cp_token *token;
33353 token = cp_lexer_peek_nth_token (parser->lexer, n);
33354 if (token->type == CPP_LESS)
33355 return true;
33356 /* Check for the sequence `<::' in the original code. It would be lexed as
33357 `[:', where `[' is a digraph, and there is no whitespace before
33358 `:'. */
33359 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
33361 cp_token *token2;
33362 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
33363 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
33364 return true;
33366 return false;
33369 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
33370 or none_type otherwise. */
33372 static enum tag_types
33373 cp_parser_token_is_class_key (cp_token* token)
33375 switch (token->keyword)
33377 case RID_CLASS:
33378 return class_type;
33379 case RID_STRUCT:
33380 return record_type;
33381 case RID_UNION:
33382 return union_type;
33384 default:
33385 return none_type;
33389 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
33390 or none_type otherwise or if the token is null. */
33392 static enum tag_types
33393 cp_parser_token_is_type_parameter_key (cp_token* token)
33395 if (!token)
33396 return none_type;
33398 switch (token->keyword)
33400 case RID_CLASS:
33401 return class_type;
33402 case RID_TYPENAME:
33403 return typename_type;
33405 default:
33406 return none_type;
33410 /* Diagnose redundant enum-keys. */
33412 static void
33413 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
33414 tree type, rid scoped_key)
33416 if (!warn_redundant_tags)
33417 return;
33419 tree type_decl = TYPE_MAIN_DECL (type);
33420 tree name = DECL_NAME (type_decl);
33421 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
33422 push_deferring_access_checks (dk_no_check);
33423 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33424 pop_deferring_access_checks ();
33426 /* The enum-key is redundant for uses of the TYPE that are not
33427 declarations and for which name lookup returns just the type
33428 itself. */
33429 if (decl != type_decl)
33430 return;
33432 if (scoped_key != RID_CLASS
33433 && scoped_key != RID_STRUCT
33434 && current_lang_name != lang_name_cplusplus
33435 && current_namespace == global_namespace)
33437 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
33438 enum tag in shared C/C++ code in files (such as headers) included
33439 in the main source file. */
33440 const line_map_ordinary *map = NULL;
33441 linemap_resolve_location (line_table, key_loc,
33442 LRK_MACRO_DEFINITION_LOCATION,
33443 &map);
33444 if (!MAIN_FILE_P (map))
33445 return;
33448 gcc_rich_location richloc (key_loc);
33449 richloc.add_fixit_remove (key_loc);
33450 warning_at (&richloc, OPT_Wredundant_tags,
33451 "redundant enum-key %<enum%s%> in reference to %q#T",
33452 (scoped_key == RID_CLASS ? " class"
33453 : scoped_key == RID_STRUCT ? " struct" : ""), type);
33456 /* Describes the set of declarations of a struct, class, or class template
33457 or its specializations. Used for -Wmismatched-tags. */
33459 class class_decl_loc_t
33461 public:
33463 class_decl_loc_t ()
33464 : locvec (), idxdef (), def_class_key ()
33466 locvec.create (4);
33469 /* Constructs an object for a single declaration of a class with
33470 CLASS_KEY at the current location in the current function (or
33471 at another scope). KEY_REDUNDANT is true if the class-key may
33472 be omitted in the current context without an ambiguity with
33473 another symbol with the same name.
33474 DEF_P is true for a class declaration that is a definition.
33475 CURLOC is the associated location. */
33476 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
33477 location_t curloc = input_location)
33478 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
33480 locvec.create (4);
33481 class_key_loc_t ckl (current_function_decl, curloc, class_key,
33482 key_redundant);
33483 locvec.quick_push (ckl);
33486 /* Copy, assign, and destroy the object. Necessary because LOCVEC
33487 isn't safely copyable and assignable and doesn't release storage
33488 on its own. */
33489 class_decl_loc_t (const class_decl_loc_t &rhs)
33490 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
33491 def_class_key (rhs.def_class_key)
33494 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
33496 if (this == &rhs)
33497 return *this;
33498 locvec.release ();
33499 locvec = rhs.locvec.copy ();
33500 idxdef = rhs.idxdef;
33501 def_class_key = rhs.def_class_key;
33502 return *this;
33505 ~class_decl_loc_t ()
33507 locvec.release ();
33510 /* Issues -Wmismatched-tags for a single class. */
33511 void diag_mismatched_tags (tree);
33513 /* Issues -Wmismatched-tags for all classes. */
33514 static void diag_mismatched_tags ();
33516 /* Adds TYPE_DECL to the collection of class decls and diagnoses
33517 redundant tags (if -Wredundant-tags is enabled). */
33518 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
33520 /* Either adds this decl to the collection of class decls
33521 or diagnoses it, whichever is appropriate. */
33522 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
33524 private:
33526 tree function (unsigned i) const
33528 return locvec[i].func;
33531 location_t location (unsigned i) const
33533 return locvec[i].loc;
33536 bool key_redundant (unsigned i) const
33538 return locvec[i].key_redundant;
33541 tag_types class_key (unsigned i) const
33543 return locvec[i].class_key;
33546 /* True if a definition for the class has been seen. */
33547 bool def_p () const
33549 return idxdef < locvec.length ();
33552 /* The location of a single mention of a class type with the given
33553 class-key. */
33554 struct class_key_loc_t
33556 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
33557 : func (func), loc (loc), class_key (key), key_redundant (redundant)
33560 /* The function the type is mentioned in. */
33561 tree func;
33562 /* The exact location. */
33563 location_t loc;
33564 /* The class-key used in the mention of the type. */
33565 tag_types class_key;
33566 /* True when the class-key could be omitted at this location
33567 without an ambiguity with another symbol of the same name. */
33568 bool key_redundant;
33570 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
33571 vec <class_key_loc_t> locvec;
33572 /* LOCVEC index of the definition or UINT_MAX if none exists. */
33573 unsigned idxdef;
33574 /* The class-key the class was last declared with or none_type when
33575 it has been declared with a mismatched key. */
33576 tag_types def_class_key;
33578 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
33579 description above. */
33580 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
33581 static class_to_loc_map_t class2loc;
33584 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
33586 /* Issue an error message if the CLASS_KEY does not match the TYPE.
33587 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33588 is set for a declaration of class TYPE and clear for a reference to
33589 it that is not a declaration of it. */
33591 static void
33592 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
33593 tag_types class_key, tree type, bool def_p,
33594 bool decl_p)
33596 if (type == error_mark_node)
33597 return;
33599 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
33600 if (seen_as_union != (class_key == union_type))
33602 if (permerror (input_location, "%qs tag used in naming %q#T",
33603 class_key == union_type ? "union"
33604 : class_key == record_type ? "struct" : "class",
33605 type))
33606 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
33607 "%q#T was previously declared here", type);
33608 return;
33611 if (!warn_mismatched_tags && !warn_redundant_tags)
33612 return;
33614 /* Only consider the true class-keys below and ignore typename_type,
33615 etc. that are not C++ class-keys. */
33616 if (class_key != class_type
33617 && class_key != record_type
33618 && class_key != union_type)
33619 return;
33621 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
33624 /* Returns the template or specialization of one to which the RECORD_TYPE
33625 TYPE corresponds. */
33627 static tree
33628 specialization_of (tree type)
33630 tree ret = type;
33632 /* Determine the template or its partial specialization to which TYPE
33633 corresponds. */
33634 if (tree spec = most_specialized_partial_spec (type, tf_none))
33635 if (spec != error_mark_node)
33636 ret = TREE_TYPE (TREE_VALUE (spec));
33638 if (ret == type)
33639 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
33641 return TYPE_MAIN_DECL (ret);
33645 /* Adds the class TYPE to the collection of class decls and diagnoses
33646 redundant tags (if -Wredundant-tags is enabled).
33647 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33648 is set for a (likely, based on syntactic context) declaration of class
33649 TYPE and clear for a reference to it that is not a declaration of it. */
33651 void
33652 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
33653 tag_types class_key, tree type, bool def_p, bool decl_p)
33655 tree type_decl = TYPE_MAIN_DECL (type);
33656 tree name = DECL_NAME (type_decl);
33657 /* Look up the NAME to see if it unambiguously refers to the TYPE
33658 and set KEY_REDUNDANT if so. */
33659 push_deferring_access_checks (dk_no_check);
33660 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33661 pop_deferring_access_checks ();
33663 /* The class-key is redundant for uses of the CLASS_TYPE that are
33664 neither definitions of it nor declarations, and for which name
33665 lookup returns just the type itself. */
33666 bool key_redundant = (!def_p && !decl_p
33667 && (decl == type_decl
33668 || TREE_CODE (decl) == TEMPLATE_DECL
33669 || (CLASS_TYPE_P (type)
33670 && TYPE_BEING_DEFINED (type))));
33672 if (key_redundant
33673 && class_key != class_type
33674 && current_lang_name != lang_name_cplusplus
33675 && current_namespace == global_namespace)
33677 /* Avoid issuing the diagnostic for apparently redundant struct
33678 and union class-keys in shared C/C++ code in files (such as
33679 headers) included in the main source file. */
33680 const line_map_ordinary *map = NULL;
33681 linemap_resolve_location (line_table, key_loc,
33682 LRK_MACRO_DEFINITION_LOCATION,
33683 &map);
33684 if (!MAIN_FILE_P (map))
33685 key_redundant = false;
33688 /* Set if a declaration of TYPE has previously been seen or if it must
33689 exist in a precompiled header. */
33690 bool exist;
33691 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
33692 if (!exist)
33694 tree type = TREE_TYPE (type_decl);
33695 if (def_p || !COMPLETE_TYPE_P (type))
33697 /* TYPE_DECL is the first declaration or definition of the type
33698 (outside precompiled headers -- see below). Just create
33699 a new entry for it and return unless it's a declaration
33700 involving a template that may need to be diagnosed by
33701 -Wredundant-tags. */
33702 *rdl = class_decl_loc_t (class_key, false, def_p);
33703 if (TREE_CODE (decl) != TEMPLATE_DECL)
33704 return;
33706 else
33708 /* TYPE was previously defined in some unknown precompiled header.
33709 Simply add a record of its definition at an unknown location and
33710 proceed below to add a reference to it at the current location.
33711 (Declarations in precompiled headers that are not definitions
33712 are ignored.) */
33713 tag_types def_key
33714 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
33715 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
33716 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
33717 exist = true;
33721 /* A prior declaration of TYPE_DECL has been seen. */
33723 if (key_redundant)
33725 gcc_rich_location richloc (key_loc);
33726 richloc.add_fixit_remove (key_loc);
33727 warning_at (&richloc, OPT_Wredundant_tags,
33728 "redundant class-key %qs in reference to %q#T",
33729 class_key == union_type ? "union"
33730 : class_key == record_type ? "struct" : "class",
33731 type);
33734 if (!exist)
33735 /* Do nothing if this is the first declaration of the type. */
33736 return;
33738 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
33739 /* Do nothing if the class-key in this declaration matches
33740 the definition. */
33741 return;
33743 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
33744 def_p);
33747 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
33748 of class decls or diagnoses it, whichever is appropriate. */
33750 void
33751 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
33752 tag_types class_key,
33753 bool redundant,
33754 bool def_p)
33756 /* Reset the CLASS_KEY associated with this type on mismatch.
33757 This is an optimization that lets the diagnostic code skip
33758 over classes that use the same class-key in all declarations. */
33759 if (def_class_key != class_key)
33760 def_class_key = none_type;
33762 /* Set IDXDEF to the index of the vector corresponding to
33763 the definition. */
33764 if (def_p)
33765 idxdef = locvec.length ();
33767 /* Append a record of this declaration to the vector. */
33768 class_key_loc_t ckl (current_function_decl, input_location, class_key,
33769 redundant);
33770 locvec.safe_push (ckl);
33772 if (idxdef == UINT_MAX)
33773 return;
33775 /* As a space optimization diagnose declarations of a class
33776 whose definition has been seen and purge the LOCVEC of
33777 all entries except the definition. */
33778 diag_mismatched_tags (type_decl);
33779 if (idxdef)
33781 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
33782 locvec.release ();
33783 locvec.reserve (2);
33784 locvec.safe_push (ent);
33785 idxdef = 0;
33787 else
33788 /* Pop the entry pushed above for this declaration. */
33789 locvec.pop ();
33792 /* Issues -Wmismatched-tags for a single class. */
33794 void
33795 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
33797 if (!warn_mismatched_tags)
33798 return;
33800 /* Number of uses of the class. */
33801 const unsigned ndecls = locvec.length ();
33803 /* The class (or template) declaration guiding the decisions about
33804 the diagnostic. For ordinary classes it's the same as THIS. For
33805 uses of instantiations of templates other than their declarations
33806 it points to the record for the declaration of the corresponding
33807 primary template or partial specialization. */
33808 class_decl_loc_t *cdlguide = this;
33810 tree type = TREE_TYPE (type_decl);
33811 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
33813 /* For implicit instantiations of a primary template look up
33814 the primary or partial specialization and use it as
33815 the expected class-key rather than using the class-key of
33816 the first reference to the instantiation. The primary must
33817 be (and inevitably is) at index zero. */
33818 tree spec = specialization_of (type);
33819 cdlguide = class2loc.get (spec);
33820 gcc_assert (cdlguide != NULL);
33822 else
33824 /* Skip declarations that consistently use the same class-key. */
33825 if (def_class_key != none_type)
33826 return;
33829 /* Set if a definition for the class has been seen. */
33830 const bool def_p = cdlguide->def_p ();
33832 /* The index of the declaration whose class-key this declaration
33833 is expected to match. It's either the class-key of the class
33834 definition if one exists or the first declaration otherwise. */
33835 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
33837 /* The class-key the class is expected to be declared with: it's
33838 either the key used in its definition or the first declaration
33839 if no definition has been provided.
33840 For implicit instantiations of a primary template it's
33841 the class-key used to declare the primary with. The primary
33842 must be at index zero. */
33843 const tag_types xpect_key = cdlguide->class_key (idxguide);
33845 unsigned idx = 0;
33846 /* Advance IDX to the first declaration that either is not
33847 a definition or that doesn't match the first declaration
33848 if no definition is provided. */
33849 while (class_key (idx) == xpect_key)
33850 if (++idx == ndecls)
33851 return;
33853 /* Save the current function before changing it below. */
33854 tree save_func = current_function_decl;
33855 /* Set the function declaration to print in diagnostic context. */
33856 current_function_decl = function (idx);
33858 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
33859 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
33861 location_t loc = location (idx);
33862 bool key_redundant_p = key_redundant (idx);
33863 auto_diagnostic_group d;
33864 /* Issue a warning for the first mismatched declaration.
33865 Avoid using "%#qT" since the class-key for the same type will
33866 be the same regardless of which one was used in the declaraion. */
33867 if (warning_at (loc, OPT_Wmismatched_tags,
33868 "%qT declared with a mismatched class-key %qs",
33869 type_decl, xmatchkstr))
33871 /* Suggest how to avoid the warning for each instance since
33872 the guidance may be different depending on context. */
33873 inform (loc,
33874 (key_redundant_p
33875 ? G_("remove the class-key or replace it with %qs")
33876 : G_("replace the class-key with %qs")),
33877 xpectkstr);
33879 /* Also point to the first declaration or definition that guided
33880 the decision to issue the warning above. */
33881 inform (cdlguide->location (idxguide),
33882 (def_p
33883 ? G_("%qT defined as %qs here")
33884 : G_("%qT first declared as %qs here")),
33885 type_decl, xpectkstr);
33888 /* Issue warnings for the remaining inconsistent declarations. */
33889 for (unsigned i = idx + 1; i != ndecls; ++i)
33891 tag_types clskey = class_key (i);
33892 /* Skip over the declarations that match either the definition
33893 if one was provided or the first declaration. */
33894 if (clskey == xpect_key)
33895 continue;
33897 loc = location (i);
33898 key_redundant_p = key_redundant (i);
33899 /* Set the function declaration to print in diagnostic context. */
33900 current_function_decl = function (i);
33901 if (warning_at (loc, OPT_Wmismatched_tags,
33902 "%qT declared with a mismatched class-key %qs",
33903 type_decl, xmatchkstr))
33904 /* Suggest how to avoid the warning for each instance since
33905 the guidance may be different depending on context. */
33906 inform (loc,
33907 (key_redundant_p
33908 ? G_("remove the class-key or replace it with %qs")
33909 : G_("replace the class-key with %qs")),
33910 xpectkstr);
33913 /* Restore the current function in case it was replaced above. */
33914 current_function_decl = save_func;
33917 /* Issues -Wmismatched-tags for all classes. Called at the end
33918 of processing a translation unit, after declarations of all class
33919 types and their uses have been recorded. */
33921 void
33922 class_decl_loc_t::diag_mismatched_tags ()
33924 /* CLASS2LOC should be empty if both -Wmismatched-tags and
33925 -Wredundant-tags are disabled. */
33926 gcc_assert (warn_mismatched_tags
33927 || warn_redundant_tags
33928 || class2loc.is_empty ());
33930 /* Save the current function before changing on return. It should
33931 be null at this point. */
33932 temp_override<tree> cleanup (current_function_decl);
33934 if (warn_mismatched_tags)
33936 /* Iterate over the collected class/struct/template declarations. */
33937 typedef class_to_loc_map_t::iterator iter_t;
33938 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
33940 tree type_decl = (*it).first;
33941 class_decl_loc_t &recloc = (*it).second;
33942 recloc.diag_mismatched_tags (type_decl);
33946 class2loc.empty ();
33949 /* Issue an error message if DECL is redeclared with different
33950 access than its original declaration [class.access.spec/3].
33951 This applies to nested classes, nested class templates and
33952 enumerations [class.mem/1]. */
33954 static void
33955 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
33957 if (!decl
33958 || (!CLASS_TYPE_P (TREE_TYPE (decl))
33959 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
33960 return;
33962 if ((TREE_PRIVATE (decl)
33963 != (current_access_specifier == access_private_node))
33964 || (TREE_PROTECTED (decl)
33965 != (current_access_specifier == access_protected_node)))
33966 error_at (location, "%qD redeclared with different access", decl);
33969 /* Look for the `template' keyword, as a syntactic disambiguator.
33970 Return TRUE iff it is present, in which case it will be
33971 consumed. */
33973 static bool
33974 cp_parser_optional_template_keyword (cp_parser *parser)
33976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
33978 /* In C++98 the `template' keyword can only be used within templates;
33979 outside templates the parser can always figure out what is a
33980 template and what is not. In C++11, per the resolution of DR 468,
33981 `template' is allowed in cases where it is not strictly necessary. */
33982 if (!processing_template_decl
33983 && pedantic && cxx_dialect == cxx98)
33985 cp_token *token = cp_lexer_peek_token (parser->lexer);
33986 pedwarn (token->location, OPT_Wpedantic,
33987 "in C++98 %<template%> (as a disambiguator) is only "
33988 "allowed within templates");
33989 /* If this part of the token stream is rescanned, the same
33990 error message would be generated. So, we purge the token
33991 from the stream. */
33992 cp_lexer_purge_token (parser->lexer);
33993 return false;
33995 else
33997 /* Consume the `template' keyword. */
33998 cp_lexer_consume_token (parser->lexer);
33999 return true;
34002 return false;
34005 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
34006 set PARSER->SCOPE, and perform other related actions. */
34008 static void
34009 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
34011 struct tree_check *check_value;
34013 /* Get the stored value. */
34014 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
34015 /* Set the scope from the stored value. */
34016 parser->scope = saved_checks_value (check_value);
34017 parser->qualifying_scope = check_value->qualifying_scope;
34018 parser->object_scope = parser->context->object_type;
34019 parser->context->object_type = NULL_TREE;
34022 /* Consume tokens up through a non-nested END token. Returns TRUE if we
34023 encounter the end of a block before what we were looking for. */
34025 static bool
34026 cp_parser_cache_group (cp_parser *parser,
34027 enum cpp_ttype end,
34028 unsigned depth)
34030 while (true)
34032 cp_token *token = cp_lexer_peek_token (parser->lexer);
34034 /* Abort a parenthesized expression if we encounter a semicolon. */
34035 if ((end == CPP_CLOSE_PAREN || depth == 0)
34036 && token->type == CPP_SEMICOLON)
34037 return true;
34038 /* If we've reached the end of the file, stop. */
34039 if (token->type == CPP_EOF
34040 || (end != CPP_PRAGMA_EOL
34041 && token->type == CPP_PRAGMA_EOL))
34042 return true;
34043 if (token->type == CPP_CLOSE_BRACE && depth == 0)
34044 /* We've hit the end of an enclosing block, so there's been some
34045 kind of syntax error. */
34046 return true;
34048 /* Consume the token. */
34049 cp_lexer_consume_token (parser->lexer);
34050 /* See if it starts a new group. */
34051 if (token->type == CPP_OPEN_BRACE)
34053 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
34054 /* In theory this should probably check end == '}', but
34055 cp_parser_save_member_function_body needs it to exit
34056 after either '}' or ')' when called with ')'. */
34057 if (depth == 0)
34058 return false;
34060 else if (token->type == CPP_OPEN_PAREN)
34062 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
34063 if (depth == 0 && end == CPP_CLOSE_PAREN)
34064 return false;
34066 else if (token->type == CPP_PRAGMA)
34067 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
34068 else if (token->type == end)
34069 return false;
34073 /* Like above, for caching a default argument or NSDMI. Both of these are
34074 terminated by a non-nested comma, but it can be unclear whether or not a
34075 comma is nested in a template argument list unless we do more parsing.
34076 In order to handle this ambiguity, when we encounter a ',' after a '<'
34077 we try to parse what follows as a parameter-declaration-list (in the
34078 case of a default argument) or a member-declarator (in the case of an
34079 NSDMI). If that succeeds, then we stop caching. */
34081 static tree
34082 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
34084 unsigned depth = 0;
34085 int maybe_template_id = 0;
34086 cp_token *first_token;
34087 cp_token *token;
34088 tree default_argument;
34090 /* Add tokens until we have processed the entire default
34091 argument. We add the range [first_token, token). */
34092 first_token = cp_lexer_peek_token (parser->lexer);
34093 if (first_token->type == CPP_OPEN_BRACE)
34095 /* For list-initialization, this is straightforward. */
34096 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
34097 token = cp_lexer_peek_token (parser->lexer);
34099 else while (true)
34101 bool done = false;
34103 /* Peek at the next token. */
34104 token = cp_lexer_peek_token (parser->lexer);
34105 /* What we do depends on what token we have. */
34106 switch (token->type)
34108 /* In valid code, a default argument must be
34109 immediately followed by a `,' `)', or `...'. */
34110 case CPP_COMMA:
34111 if (depth == 0 && maybe_template_id)
34113 /* If we've seen a '<', we might be in a
34114 template-argument-list. Until Core issue 325 is
34115 resolved, we don't know how this situation ought
34116 to be handled, so try to DTRT. We check whether
34117 what comes after the comma is a valid parameter
34118 declaration list. If it is, then the comma ends
34119 the default argument; otherwise the default
34120 argument continues. */
34121 bool error = false;
34122 cp_token *peek;
34124 /* Set ITALP so cp_parser_parameter_declaration_list
34125 doesn't decide to commit to this parse. */
34126 bool saved_italp = parser->in_template_argument_list_p;
34127 parser->in_template_argument_list_p = true;
34129 cp_parser_parse_tentatively (parser);
34131 if (nsdmi)
34133 /* Parse declarators until we reach a non-comma or
34134 somthing that cannot be an initializer.
34135 Just checking whether we're looking at a single
34136 declarator is insufficient. Consider:
34137 int var = tuple<T,U>::x;
34138 The template parameter 'U' looks exactly like a
34139 declarator. */
34142 int ctor_dtor_or_conv_p;
34143 cp_lexer_consume_token (parser->lexer);
34144 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
34145 CP_PARSER_FLAGS_NONE,
34146 &ctor_dtor_or_conv_p,
34147 /*parenthesized_p=*/NULL,
34148 /*member_p=*/true,
34149 /*friend_p=*/false,
34150 /*static_p=*/false);
34151 peek = cp_lexer_peek_token (parser->lexer);
34152 if (cp_parser_error_occurred (parser))
34153 break;
34155 while (peek->type == CPP_COMMA);
34156 /* If we met an '=' or ';' then the original comma
34157 was the end of the NSDMI. Otherwise assume
34158 we're still in the NSDMI. */
34159 error = (peek->type != CPP_EQ
34160 && peek->type != CPP_SEMICOLON);
34162 else
34164 cp_lexer_consume_token (parser->lexer);
34165 begin_scope (sk_function_parms, NULL_TREE);
34166 tree t = cp_parser_parameter_declaration_list
34167 (parser, CP_PARSER_FLAGS_NONE,
34168 /*pending_decls*/nullptr);
34169 if (t == error_mark_node)
34170 error = true;
34171 pop_bindings_and_leave_scope ();
34173 if (!cp_parser_error_occurred (parser) && !error)
34174 done = true;
34175 cp_parser_abort_tentative_parse (parser);
34177 parser->in_template_argument_list_p = saved_italp;
34178 break;
34180 /* FALLTHRU */
34181 case CPP_CLOSE_PAREN:
34182 case CPP_ELLIPSIS:
34183 /* If we run into a non-nested `;', `}', or `]',
34184 then the code is invalid -- but the default
34185 argument is certainly over. */
34186 case CPP_SEMICOLON:
34187 case CPP_CLOSE_BRACE:
34188 case CPP_CLOSE_SQUARE:
34189 if (depth == 0
34190 /* Handle correctly int n = sizeof ... ( p ); */
34191 && token->type != CPP_ELLIPSIS)
34192 done = true;
34193 /* Update DEPTH, if necessary. */
34194 else if (token->type == CPP_CLOSE_PAREN
34195 || token->type == CPP_CLOSE_BRACE
34196 || token->type == CPP_CLOSE_SQUARE)
34197 --depth;
34198 break;
34200 case CPP_OPEN_PAREN:
34201 case CPP_OPEN_SQUARE:
34202 case CPP_OPEN_BRACE:
34203 ++depth;
34204 break;
34206 case CPP_LESS:
34207 if (depth == 0)
34208 /* This might be the comparison operator, or it might
34209 start a template argument list. */
34210 ++maybe_template_id;
34211 break;
34213 case CPP_RSHIFT:
34214 if (cxx_dialect == cxx98)
34215 break;
34216 /* Fall through for C++0x, which treats the `>>'
34217 operator like two `>' tokens in certain
34218 cases. */
34219 gcc_fallthrough ();
34221 case CPP_GREATER:
34222 if (depth == 0)
34224 /* This might be an operator, or it might close a
34225 template argument list. But if a previous '<'
34226 started a template argument list, this will have
34227 closed it, so we can't be in one anymore. */
34228 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
34229 if (maybe_template_id < 0)
34230 maybe_template_id = 0;
34232 break;
34234 /* If we run out of tokens, issue an error message. */
34235 case CPP_EOF:
34236 case CPP_PRAGMA_EOL:
34237 error_at (token->location, "file ends in default argument");
34238 return error_mark_node;
34240 case CPP_NAME:
34241 case CPP_SCOPE:
34242 /* In these cases, we should look for template-ids.
34243 For example, if the default argument is
34244 `X<int, double>()', we need to do name lookup to
34245 figure out whether or not `X' is a template; if
34246 so, the `,' does not end the default argument.
34248 That is not yet done. */
34249 break;
34251 default:
34252 break;
34255 /* If we've reached the end, stop. */
34256 if (done)
34257 break;
34259 /* Add the token to the token block. */
34260 token = cp_lexer_consume_token (parser->lexer);
34263 /* Create a DEFERRED_PARSE to represent the unparsed default
34264 argument. */
34265 default_argument = make_node (DEFERRED_PARSE);
34266 DEFPARSE_TOKENS (default_argument)
34267 = cp_token_cache_new (first_token, token);
34268 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
34270 return default_argument;
34273 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
34275 location_t
34276 defparse_location (tree default_argument)
34278 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
34279 location_t start = tokens->first->location;
34280 location_t end = tokens->last->location;
34281 return make_location (start, start, end);
34284 /* Begin parsing tentatively. We always save tokens while parsing
34285 tentatively so that if the tentative parsing fails we can restore the
34286 tokens. */
34288 static void
34289 cp_parser_parse_tentatively (cp_parser* parser)
34291 /* Enter a new parsing context. */
34292 parser->context = cp_parser_context_new (parser->context);
34293 /* Begin saving tokens. */
34294 cp_lexer_save_tokens (parser->lexer);
34295 /* In order to avoid repetitive access control error messages,
34296 access checks are queued up until we are no longer parsing
34297 tentatively. */
34298 push_deferring_access_checks (dk_deferred);
34301 /* Commit to the currently active tentative parse. */
34303 static void
34304 cp_parser_commit_to_tentative_parse (cp_parser* parser)
34306 cp_parser_context *context;
34307 cp_lexer *lexer;
34309 /* Mark all of the levels as committed. */
34310 lexer = parser->lexer;
34311 for (context = parser->context; context->next; context = context->next)
34313 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34314 break;
34315 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34316 while (!cp_lexer_saving_tokens (lexer))
34317 lexer = lexer->next;
34318 cp_lexer_commit_tokens (lexer);
34322 /* Commit to the topmost currently active tentative parse.
34324 Note that this function shouldn't be called when there are
34325 irreversible side-effects while in a tentative state. For
34326 example, we shouldn't create a permanent entry in the symbol
34327 table, or issue an error message that might not apply if the
34328 tentative parse is aborted. */
34330 static void
34331 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
34333 cp_parser_context *context = parser->context;
34334 cp_lexer *lexer = parser->lexer;
34336 if (context)
34338 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34339 return;
34340 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34342 while (!cp_lexer_saving_tokens (lexer))
34343 lexer = lexer->next;
34344 cp_lexer_commit_tokens (lexer);
34348 /* Abort the currently active tentative parse. All consumed tokens
34349 will be rolled back, and no diagnostics will be issued. */
34351 static void
34352 cp_parser_abort_tentative_parse (cp_parser* parser)
34354 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
34355 || errorcount > 0);
34356 cp_parser_simulate_error (parser);
34357 /* Now, pretend that we want to see if the construct was
34358 successfully parsed. */
34359 cp_parser_parse_definitely (parser);
34362 /* Stop parsing tentatively. If a parse error has occurred, restore the
34363 token stream. Otherwise, commit to the tokens we have consumed.
34364 Returns true if no error occurred; false otherwise. */
34366 static bool
34367 cp_parser_parse_definitely (cp_parser* parser)
34369 bool error_occurred;
34370 cp_parser_context *context;
34372 /* Remember whether or not an error occurred, since we are about to
34373 destroy that information. */
34374 error_occurred = cp_parser_error_occurred (parser);
34375 /* Remove the topmost context from the stack. */
34376 context = parser->context;
34377 parser->context = context->next;
34378 /* If no parse errors occurred, commit to the tentative parse. */
34379 if (!error_occurred)
34381 /* Commit to the tokens read tentatively, unless that was
34382 already done. */
34383 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
34384 cp_lexer_commit_tokens (parser->lexer);
34386 pop_to_parent_deferring_access_checks ();
34388 /* Otherwise, if errors occurred, roll back our state so that things
34389 are just as they were before we began the tentative parse. */
34390 else
34392 cp_lexer_rollback_tokens (parser->lexer);
34393 pop_deferring_access_checks ();
34395 /* Add the context to the front of the free list. */
34396 context->next = cp_parser_context_free_list;
34397 cp_parser_context_free_list = context;
34399 return !error_occurred;
34402 /* Returns true if we are parsing tentatively and are not committed to
34403 this tentative parse. */
34405 static bool
34406 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
34408 return (cp_parser_parsing_tentatively (parser)
34409 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
34412 /* Returns nonzero iff an error has occurred during the most recent
34413 tentative parse. */
34415 static bool
34416 cp_parser_error_occurred (cp_parser* parser)
34418 return (cp_parser_parsing_tentatively (parser)
34419 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
34422 /* Returns nonzero if GNU extensions are allowed. */
34424 static bool
34425 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
34427 return parser->allow_gnu_extensions_p;
34430 /* Objective-C++ Productions */
34433 /* Parse an Objective-C expression, which feeds into a primary-expression
34434 above.
34436 objc-expression:
34437 objc-message-expression
34438 objc-string-literal
34439 objc-encode-expression
34440 objc-protocol-expression
34441 objc-selector-expression
34443 Returns a tree representation of the expression. */
34445 static cp_expr
34446 cp_parser_objc_expression (cp_parser* parser)
34448 /* Try to figure out what kind of declaration is present. */
34449 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34451 switch (kwd->type)
34453 case CPP_OPEN_SQUARE:
34454 return cp_parser_objc_message_expression (parser);
34456 case CPP_OBJC_STRING:
34457 kwd = cp_lexer_consume_token (parser->lexer);
34458 return objc_build_string_object (kwd->u.value);
34460 case CPP_KEYWORD:
34461 switch (kwd->keyword)
34463 case RID_AT_ENCODE:
34464 return cp_parser_objc_encode_expression (parser);
34466 case RID_AT_PROTOCOL:
34467 return cp_parser_objc_protocol_expression (parser);
34469 case RID_AT_SELECTOR:
34470 return cp_parser_objc_selector_expression (parser);
34472 default:
34473 break;
34475 /* FALLTHRU */
34476 default:
34477 error_at (kwd->location,
34478 "misplaced %<@%D%> Objective-C++ construct",
34479 kwd->u.value);
34480 cp_parser_skip_to_end_of_block_or_statement (parser);
34483 return error_mark_node;
34486 /* Parse an Objective-C message expression.
34488 objc-message-expression:
34489 [ objc-message-receiver objc-message-args ]
34491 Returns a representation of an Objective-C message. */
34493 static tree
34494 cp_parser_objc_message_expression (cp_parser* parser)
34496 tree receiver, messageargs;
34498 parser->objective_c_message_context_p = true;
34499 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34500 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
34501 receiver = cp_parser_objc_message_receiver (parser);
34502 messageargs = cp_parser_objc_message_args (parser);
34503 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
34504 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
34506 tree result = objc_build_message_expr (receiver, messageargs);
34508 /* Construct a location e.g.
34509 [self func1:5]
34510 ^~~~~~~~~~~~~~
34511 ranging from the '[' to the ']', with the caret at the start. */
34512 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
34513 protected_set_expr_location (result, combined_loc);
34515 parser->objective_c_message_context_p = false;
34516 return result;
34519 /* Parse an objc-message-receiver.
34521 objc-message-receiver:
34522 expression
34523 simple-type-specifier
34525 Returns a representation of the type or expression. */
34527 static tree
34528 cp_parser_objc_message_receiver (cp_parser* parser)
34530 tree rcv;
34532 /* An Objective-C message receiver may be either (1) a type
34533 or (2) an expression. */
34534 cp_parser_parse_tentatively (parser);
34535 rcv = cp_parser_expression (parser);
34537 /* If that worked out, fine. */
34538 if (cp_parser_parse_definitely (parser))
34539 return rcv;
34541 cp_parser_parse_tentatively (parser);
34542 rcv = cp_parser_simple_type_specifier (parser,
34543 /*decl_specs=*/NULL,
34544 CP_PARSER_FLAGS_NONE);
34546 if (cp_parser_parse_definitely (parser))
34547 return objc_get_class_reference (rcv);
34549 cp_parser_error (parser, "objective-c++ message receiver expected");
34550 return error_mark_node;
34553 /* Parse the arguments and selectors comprising an Objective-C message.
34555 objc-message-args:
34556 objc-selector
34557 objc-selector-args
34558 objc-selector-args , objc-comma-args
34560 objc-selector-args:
34561 objc-selector [opt] : assignment-expression
34562 objc-selector-args objc-selector [opt] : assignment-expression
34564 objc-comma-args:
34565 assignment-expression
34566 objc-comma-args , assignment-expression
34568 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
34569 selector arguments and TREE_VALUE containing a list of comma
34570 arguments. */
34572 static tree
34573 cp_parser_objc_message_args (cp_parser* parser)
34575 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
34576 bool maybe_unary_selector_p = true;
34577 cp_token *token = cp_lexer_peek_token (parser->lexer);
34579 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
34581 tree selector = NULL_TREE, arg;
34583 if (token->type != CPP_COLON)
34584 selector = cp_parser_objc_selector (parser);
34586 /* Detect if we have a unary selector. */
34587 if (maybe_unary_selector_p
34588 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34589 return build_tree_list (selector, NULL_TREE);
34591 maybe_unary_selector_p = false;
34592 cp_parser_require (parser, CPP_COLON, RT_COLON);
34593 arg = cp_parser_assignment_expression (parser);
34595 sel_args
34596 = chainon (sel_args,
34597 build_tree_list (selector, arg));
34599 token = cp_lexer_peek_token (parser->lexer);
34602 /* Handle non-selector arguments, if any. */
34603 while (token->type == CPP_COMMA)
34605 tree arg;
34607 cp_lexer_consume_token (parser->lexer);
34608 arg = cp_parser_assignment_expression (parser);
34610 addl_args
34611 = chainon (addl_args,
34612 build_tree_list (NULL_TREE, arg));
34614 token = cp_lexer_peek_token (parser->lexer);
34617 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
34619 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
34620 return build_tree_list (error_mark_node, error_mark_node);
34623 return build_tree_list (sel_args, addl_args);
34626 /* Parse an Objective-C encode expression.
34628 objc-encode-expression:
34629 @encode objc-typename
34631 Returns an encoded representation of the type argument. */
34633 static cp_expr
34634 cp_parser_objc_encode_expression (cp_parser* parser)
34636 tree type;
34637 cp_token *token;
34638 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34640 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
34641 matching_parens parens;
34642 parens.require_open (parser);
34643 token = cp_lexer_peek_token (parser->lexer);
34644 type = complete_type (cp_parser_type_id (parser));
34645 parens.require_close (parser);
34647 if (!type)
34649 error_at (token->location,
34650 "%<@encode%> must specify a type as an argument");
34651 return error_mark_node;
34654 /* This happens if we find @encode(T) (where T is a template
34655 typename or something dependent on a template typename) when
34656 parsing a template. In that case, we can't compile it
34657 immediately, but we rather create an AT_ENCODE_EXPR which will
34658 need to be instantiated when the template is used.
34660 if (dependent_type_p (type))
34662 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
34663 TREE_READONLY (value) = 1;
34664 return value;
34668 /* Build a location of the form:
34669 @encode(int)
34670 ^~~~~~~~~~~~
34671 with caret==start at the @ token, finishing at the close paren. */
34672 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34674 return cp_expr (objc_build_encode_expr (type), combined_loc);
34677 /* Parse an Objective-C @defs expression. */
34679 static tree
34680 cp_parser_objc_defs_expression (cp_parser *parser)
34682 tree name;
34684 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
34685 matching_parens parens;
34686 parens.require_open (parser);
34687 name = cp_parser_identifier (parser);
34688 parens.require_close (parser);
34690 return objc_get_class_ivars (name);
34693 /* Parse an Objective-C protocol expression.
34695 objc-protocol-expression:
34696 @protocol ( identifier )
34698 Returns a representation of the protocol expression. */
34700 static tree
34701 cp_parser_objc_protocol_expression (cp_parser* parser)
34703 tree proto;
34704 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34706 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
34707 matching_parens parens;
34708 parens.require_open (parser);
34709 proto = cp_parser_identifier (parser);
34710 parens.require_close (parser);
34712 /* Build a location of the form:
34713 @protocol(prot)
34714 ^~~~~~~~~~~~~~~
34715 with caret==start at the @ token, finishing at the close paren. */
34716 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34717 tree result = objc_build_protocol_expr (proto);
34718 protected_set_expr_location (result, combined_loc);
34719 return result;
34722 /* Parse an Objective-C selector expression.
34724 objc-selector-expression:
34725 @selector ( objc-method-signature )
34727 objc-method-signature:
34728 objc-selector
34729 objc-selector-seq
34731 objc-selector-seq:
34732 objc-selector :
34733 objc-selector-seq objc-selector :
34735 Returns a representation of the method selector. */
34737 static tree
34738 cp_parser_objc_selector_expression (cp_parser* parser)
34740 tree sel_seq = NULL_TREE;
34741 bool maybe_unary_selector_p = true;
34742 cp_token *token;
34743 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34745 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
34746 matching_parens parens;
34747 parens.require_open (parser);
34748 token = cp_lexer_peek_token (parser->lexer);
34750 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
34751 || token->type == CPP_SCOPE)
34753 tree selector = NULL_TREE;
34755 if (token->type != CPP_COLON
34756 || token->type == CPP_SCOPE)
34757 selector = cp_parser_objc_selector (parser);
34759 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
34760 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
34762 /* Detect if we have a unary selector. */
34763 if (maybe_unary_selector_p)
34765 sel_seq = selector;
34766 goto finish_selector;
34768 else
34770 cp_parser_error (parser, "expected %<:%>");
34773 maybe_unary_selector_p = false;
34774 token = cp_lexer_consume_token (parser->lexer);
34776 if (token->type == CPP_SCOPE)
34778 sel_seq
34779 = chainon (sel_seq,
34780 build_tree_list (selector, NULL_TREE));
34781 sel_seq
34782 = chainon (sel_seq,
34783 build_tree_list (NULL_TREE, NULL_TREE));
34785 else
34786 sel_seq
34787 = chainon (sel_seq,
34788 build_tree_list (selector, NULL_TREE));
34790 token = cp_lexer_peek_token (parser->lexer);
34793 finish_selector:
34794 parens.require_close (parser);
34797 /* Build a location of the form:
34798 @selector(func)
34799 ^~~~~~~~~~~~~~~
34800 with caret==start at the @ token, finishing at the close paren. */
34801 location_t combined_loc = make_location (loc, loc, parser->lexer);
34802 tree result = objc_build_selector_expr (combined_loc, sel_seq);
34803 /* TODO: objc_build_selector_expr doesn't always honor the location. */
34804 protected_set_expr_location (result, combined_loc);
34805 return result;
34808 /* Parse a list of identifiers.
34810 objc-identifier-list:
34811 identifier
34812 objc-identifier-list , identifier
34814 Returns a TREE_LIST of identifier nodes. */
34816 static tree
34817 cp_parser_objc_identifier_list (cp_parser* parser)
34819 tree identifier;
34820 tree list;
34821 cp_token *sep;
34823 identifier = cp_parser_identifier (parser);
34824 if (identifier == error_mark_node)
34825 return error_mark_node;
34827 list = build_tree_list (NULL_TREE, identifier);
34828 sep = cp_lexer_peek_token (parser->lexer);
34830 while (sep->type == CPP_COMMA)
34832 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34833 identifier = cp_parser_identifier (parser);
34834 if (identifier == error_mark_node)
34835 return list;
34837 list = chainon (list, build_tree_list (NULL_TREE,
34838 identifier));
34839 sep = cp_lexer_peek_token (parser->lexer);
34842 return list;
34845 /* Parse an Objective-C alias declaration.
34847 objc-alias-declaration:
34848 @compatibility_alias identifier identifier ;
34850 This function registers the alias mapping with the Objective-C front end.
34851 It returns nothing. */
34853 static void
34854 cp_parser_objc_alias_declaration (cp_parser* parser)
34856 tree alias, orig;
34858 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
34859 alias = cp_parser_identifier (parser);
34860 orig = cp_parser_identifier (parser);
34861 objc_declare_alias (alias, orig);
34862 cp_parser_consume_semicolon_at_end_of_statement (parser);
34865 /* Parse an Objective-C class forward-declaration.
34867 objc-class-declaration:
34868 @class objc-identifier-list ;
34870 The function registers the forward declarations with the Objective-C
34871 front end. It returns nothing. */
34873 static void
34874 cp_parser_objc_class_declaration (cp_parser* parser)
34876 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
34877 while (true)
34879 tree id;
34881 id = cp_parser_identifier (parser);
34882 if (id == error_mark_node)
34883 break;
34885 objc_declare_class (id);
34887 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34888 cp_lexer_consume_token (parser->lexer);
34889 else
34890 break;
34892 cp_parser_consume_semicolon_at_end_of_statement (parser);
34895 /* Parse a list of Objective-C protocol references.
34897 objc-protocol-refs-opt:
34898 objc-protocol-refs [opt]
34900 objc-protocol-refs:
34901 < objc-identifier-list >
34903 Returns a TREE_LIST of identifiers, if any. */
34905 static tree
34906 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
34908 tree protorefs = NULL_TREE;
34910 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
34912 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
34913 protorefs = cp_parser_objc_identifier_list (parser);
34914 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
34917 return protorefs;
34920 /* Parse a Objective-C visibility specification. */
34922 static void
34923 cp_parser_objc_visibility_spec (cp_parser* parser)
34925 cp_token *vis = cp_lexer_peek_token (parser->lexer);
34927 switch (vis->keyword)
34929 case RID_AT_PRIVATE:
34930 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
34931 break;
34932 case RID_AT_PROTECTED:
34933 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
34934 break;
34935 case RID_AT_PUBLIC:
34936 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
34937 break;
34938 case RID_AT_PACKAGE:
34939 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
34940 break;
34941 default:
34942 return;
34945 /* Eat '@private'/'@protected'/'@public'. */
34946 cp_lexer_consume_token (parser->lexer);
34949 /* Parse an Objective-C method type. Return 'true' if it is a class
34950 (+) method, and 'false' if it is an instance (-) method. */
34952 static inline bool
34953 cp_parser_objc_method_type (cp_parser* parser)
34955 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
34956 return true;
34957 else
34958 return false;
34961 /* Parse an Objective-C protocol qualifier. */
34963 static tree
34964 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
34966 tree quals = NULL_TREE, node;
34967 cp_token *token = cp_lexer_peek_token (parser->lexer);
34969 node = token->u.value;
34971 while (node && identifier_p (node)
34972 && (node == ridpointers [(int) RID_IN]
34973 || node == ridpointers [(int) RID_OUT]
34974 || node == ridpointers [(int) RID_INOUT]
34975 || node == ridpointers [(int) RID_BYCOPY]
34976 || node == ridpointers [(int) RID_BYREF]
34977 || node == ridpointers [(int) RID_ONEWAY]))
34979 quals = tree_cons (NULL_TREE, node, quals);
34980 cp_lexer_consume_token (parser->lexer);
34981 token = cp_lexer_peek_token (parser->lexer);
34982 node = token->u.value;
34985 return quals;
34988 /* Parse an Objective-C typename. */
34990 static tree
34991 cp_parser_objc_typename (cp_parser* parser)
34993 tree type_name = NULL_TREE;
34995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34997 tree proto_quals, cp_type = NULL_TREE;
34999 matching_parens parens;
35000 parens.consume_open (parser); /* Eat '('. */
35001 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
35003 /* An ObjC type name may consist of just protocol qualifiers, in which
35004 case the type shall default to 'id'. */
35005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35007 cp_type = cp_parser_type_id (parser);
35009 /* If the type could not be parsed, an error has already
35010 been produced. For error recovery, behave as if it had
35011 not been specified, which will use the default type
35012 'id'. */
35013 if (cp_type == error_mark_node)
35015 cp_type = NULL_TREE;
35016 /* We need to skip to the closing parenthesis as
35017 cp_parser_type_id() does not seem to do it for
35018 us. */
35019 cp_parser_skip_to_closing_parenthesis (parser,
35020 /*recovering=*/true,
35021 /*or_comma=*/false,
35022 /*consume_paren=*/false);
35026 parens.require_close (parser);
35027 type_name = build_tree_list (proto_quals, cp_type);
35030 return type_name;
35033 /* Check to see if TYPE refers to an Objective-C selector name. */
35035 static bool
35036 cp_parser_objc_selector_p (enum cpp_ttype type)
35038 return (type == CPP_NAME || type == CPP_KEYWORD
35039 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
35040 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
35041 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
35042 || type == CPP_XOR || type == CPP_XOR_EQ);
35045 /* Parse an Objective-C selector. */
35047 static tree
35048 cp_parser_objc_selector (cp_parser* parser)
35050 cp_token *token = cp_lexer_consume_token (parser->lexer);
35052 if (!cp_parser_objc_selector_p (token->type))
35054 error_at (token->location, "invalid Objective-C++ selector name");
35055 return error_mark_node;
35058 /* C++ operator names are allowed to appear in ObjC selectors. */
35059 switch (token->type)
35061 case CPP_AND_AND: return get_identifier ("and");
35062 case CPP_AND_EQ: return get_identifier ("and_eq");
35063 case CPP_AND: return get_identifier ("bitand");
35064 case CPP_OR: return get_identifier ("bitor");
35065 case CPP_COMPL: return get_identifier ("compl");
35066 case CPP_NOT: return get_identifier ("not");
35067 case CPP_NOT_EQ: return get_identifier ("not_eq");
35068 case CPP_OR_OR: return get_identifier ("or");
35069 case CPP_OR_EQ: return get_identifier ("or_eq");
35070 case CPP_XOR: return get_identifier ("xor");
35071 case CPP_XOR_EQ: return get_identifier ("xor_eq");
35072 default: return token->u.value;
35076 /* Parse an Objective-C params list. */
35078 static tree
35079 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
35081 tree params = NULL_TREE;
35082 bool maybe_unary_selector_p = true;
35083 cp_token *token = cp_lexer_peek_token (parser->lexer);
35085 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35087 tree selector = NULL_TREE, type_name, identifier;
35088 tree parm_attr = NULL_TREE;
35090 if (token->keyword == RID_ATTRIBUTE)
35091 break;
35093 if (token->type != CPP_COLON)
35094 selector = cp_parser_objc_selector (parser);
35096 /* Detect if we have a unary selector. */
35097 if (maybe_unary_selector_p
35098 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35100 params = selector; /* Might be followed by attributes. */
35101 break;
35104 maybe_unary_selector_p = false;
35105 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35107 /* Something went quite wrong. There should be a colon
35108 here, but there is not. Stop parsing parameters. */
35109 break;
35111 type_name = cp_parser_objc_typename (parser);
35112 /* New ObjC allows attributes on parameters too. */
35113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
35114 parm_attr = cp_parser_attributes_opt (parser);
35115 identifier = cp_parser_identifier (parser);
35117 params
35118 = chainon (params,
35119 objc_build_keyword_decl (selector,
35120 type_name,
35121 identifier,
35122 parm_attr));
35124 token = cp_lexer_peek_token (parser->lexer);
35127 if (params == NULL_TREE)
35129 cp_parser_error (parser, "objective-c++ method declaration is expected");
35130 return error_mark_node;
35133 /* We allow tail attributes for the method. */
35134 if (token->keyword == RID_ATTRIBUTE)
35136 *attributes = cp_parser_attributes_opt (parser);
35137 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35138 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35139 return params;
35140 cp_parser_error (parser,
35141 "method attributes must be specified at the end");
35142 return error_mark_node;
35145 if (params == NULL_TREE)
35147 cp_parser_error (parser, "objective-c++ method declaration is expected");
35148 return error_mark_node;
35150 return params;
35153 /* Parse the non-keyword Objective-C params. */
35155 static tree
35156 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
35157 tree* attributes)
35159 tree params = make_node (TREE_LIST);
35160 cp_token *token = cp_lexer_peek_token (parser->lexer);
35161 *ellipsisp = false; /* Initially, assume no ellipsis. */
35163 while (token->type == CPP_COMMA)
35165 cp_parameter_declarator *parmdecl;
35166 tree parm;
35168 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35169 token = cp_lexer_peek_token (parser->lexer);
35171 if (token->type == CPP_ELLIPSIS)
35173 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
35174 *ellipsisp = true;
35175 token = cp_lexer_peek_token (parser->lexer);
35176 break;
35179 /* TODO: parse attributes for tail parameters. */
35180 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35181 false, NULL);
35182 parm = grokdeclarator (parmdecl->declarator,
35183 &parmdecl->decl_specifiers,
35184 PARM, /*initialized=*/0,
35185 /*attrlist=*/NULL);
35187 chainon (params, build_tree_list (NULL_TREE, parm));
35188 token = cp_lexer_peek_token (parser->lexer);
35191 /* We allow tail attributes for the method. */
35192 if (token->keyword == RID_ATTRIBUTE)
35194 if (*attributes == NULL_TREE)
35196 *attributes = cp_parser_attributes_opt (parser);
35197 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35198 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35199 return params;
35201 else
35202 /* We have an error, but parse the attributes, so that we can
35203 carry on. */
35204 *attributes = cp_parser_attributes_opt (parser);
35206 cp_parser_error (parser,
35207 "method attributes must be specified at the end");
35208 return error_mark_node;
35211 return params;
35214 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
35216 static void
35217 cp_parser_objc_interstitial_code (cp_parser* parser)
35219 cp_token *token = cp_lexer_peek_token (parser->lexer);
35221 /* If the next token is `extern' and the following token is a string
35222 literal, then we have a linkage specification. */
35223 if (token->keyword == RID_EXTERN
35224 && cp_parser_is_pure_string_literal
35225 (cp_lexer_peek_nth_token (parser->lexer, 2)))
35226 cp_parser_linkage_specification (parser, NULL_TREE);
35227 /* Handle #pragma, if any. */
35228 else if (token->type == CPP_PRAGMA)
35229 cp_parser_pragma (parser, pragma_objc_icode, NULL);
35230 /* Allow stray semicolons. */
35231 else if (token->type == CPP_SEMICOLON)
35232 cp_lexer_consume_token (parser->lexer);
35233 /* Mark methods as optional or required, when building protocols. */
35234 else if (token->keyword == RID_AT_OPTIONAL)
35236 cp_lexer_consume_token (parser->lexer);
35237 objc_set_method_opt (true);
35239 else if (token->keyword == RID_AT_REQUIRED)
35241 cp_lexer_consume_token (parser->lexer);
35242 objc_set_method_opt (false);
35244 else if (token->keyword == RID_NAMESPACE)
35245 cp_parser_namespace_definition (parser);
35246 /* Other stray characters must generate errors. */
35247 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
35249 cp_lexer_consume_token (parser->lexer);
35250 error ("stray %qs between Objective-C++ methods",
35251 token->type == CPP_OPEN_BRACE ? "{" : "}");
35253 /* Finally, try to parse a block-declaration, or a function-definition. */
35254 else
35255 cp_parser_block_declaration (parser, /*statement_p=*/false);
35258 /* Parse a method signature. */
35260 static tree
35261 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
35263 tree rettype, kwdparms, optparms;
35264 bool ellipsis = false;
35265 bool is_class_method;
35267 is_class_method = cp_parser_objc_method_type (parser);
35268 rettype = cp_parser_objc_typename (parser);
35269 *attributes = NULL_TREE;
35270 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
35271 if (kwdparms == error_mark_node)
35272 return error_mark_node;
35273 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
35274 if (optparms == error_mark_node)
35275 return error_mark_node;
35277 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
35280 static bool
35281 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
35283 tree tattr;
35284 cp_lexer_save_tokens (parser->lexer);
35285 tattr = cp_parser_attributes_opt (parser);
35286 gcc_assert (tattr) ;
35288 /* If the attributes are followed by a method introducer, this is not allowed.
35289 Dump the attributes and flag the situation. */
35290 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
35291 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
35292 return true;
35294 /* Otherwise, the attributes introduce some interstitial code, possibly so
35295 rewind to allow that check. */
35296 cp_lexer_rollback_tokens (parser->lexer);
35297 return false;
35300 /* Parse an Objective-C method prototype list. */
35302 static void
35303 cp_parser_objc_method_prototype_list (cp_parser* parser)
35305 cp_token *token = cp_lexer_peek_token (parser->lexer);
35307 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
35309 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35311 tree attributes, sig;
35312 bool is_class_method;
35313 if (token->type == CPP_PLUS)
35314 is_class_method = true;
35315 else
35316 is_class_method = false;
35317 sig = cp_parser_objc_method_signature (parser, &attributes);
35318 if (sig == error_mark_node)
35320 cp_parser_skip_to_end_of_block_or_statement (parser);
35321 token = cp_lexer_peek_token (parser->lexer);
35322 continue;
35324 objc_add_method_declaration (is_class_method, sig, attributes);
35325 cp_parser_consume_semicolon_at_end_of_statement (parser);
35327 else if (token->keyword == RID_AT_PROPERTY)
35328 cp_parser_objc_at_property_declaration (parser);
35329 else if (token->keyword == RID_ATTRIBUTE
35330 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35331 warning_at (cp_lexer_peek_token (parser->lexer)->location,
35332 OPT_Wattributes,
35333 "prefix attributes are ignored for methods");
35334 else
35335 /* Allow for interspersed non-ObjC++ code. */
35336 cp_parser_objc_interstitial_code (parser);
35338 token = cp_lexer_peek_token (parser->lexer);
35341 if (token->type != CPP_EOF)
35342 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35343 else
35344 cp_parser_error (parser, "expected %<@end%>");
35346 objc_finish_interface ();
35349 /* Parse an Objective-C method definition list. */
35351 static void
35352 cp_parser_objc_method_definition_list (cp_parser* parser)
35354 for (;;)
35356 cp_token *token = cp_lexer_peek_token (parser->lexer);
35358 if (token->keyword == RID_AT_END)
35360 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35361 break;
35363 else if (token->type == CPP_EOF)
35365 cp_parser_error (parser, "expected %<@end%>");
35366 break;
35368 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35370 bool is_class_method = token->type == CPP_PLUS;
35372 push_deferring_access_checks (dk_deferred);
35373 tree attribute;
35374 tree sig = cp_parser_objc_method_signature (parser, &attribute);
35375 if (sig == error_mark_node)
35376 cp_parser_skip_to_end_of_block_or_statement (parser);
35377 else
35379 objc_start_method_definition (is_class_method, sig,
35380 attribute, NULL_TREE);
35382 /* For historical reasons, we accept an optional semicolon. */
35383 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35384 cp_lexer_consume_token (parser->lexer);
35386 perform_deferred_access_checks (tf_warning_or_error);
35387 stop_deferring_access_checks ();
35388 tree meth
35389 = cp_parser_function_definition_after_declarator (parser, false);
35390 pop_deferring_access_checks ();
35391 objc_finish_method_definition (meth);
35394 /* The following case will be removed once @synthesize is
35395 completely implemented. */
35396 else if (token->keyword == RID_AT_PROPERTY)
35397 cp_parser_objc_at_property_declaration (parser);
35398 else if (token->keyword == RID_AT_SYNTHESIZE)
35399 cp_parser_objc_at_synthesize_declaration (parser);
35400 else if (token->keyword == RID_AT_DYNAMIC)
35401 cp_parser_objc_at_dynamic_declaration (parser);
35402 else if (token->keyword == RID_ATTRIBUTE
35403 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35404 warning_at (token->location, OPT_Wattributes,
35405 "prefix attributes are ignored for methods");
35406 else
35407 /* Allow for interspersed non-ObjC++ code. */
35408 cp_parser_objc_interstitial_code (parser);
35411 objc_finish_implementation ();
35414 /* Parse Objective-C ivars. */
35416 static void
35417 cp_parser_objc_class_ivars (cp_parser* parser)
35419 cp_token *token = cp_lexer_peek_token (parser->lexer);
35421 if (token->type != CPP_OPEN_BRACE)
35422 return; /* No ivars specified. */
35424 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
35425 token = cp_lexer_peek_token (parser->lexer);
35427 while (token->type != CPP_CLOSE_BRACE
35428 && token->keyword != RID_AT_END && token->type != CPP_EOF)
35430 cp_decl_specifier_seq declspecs;
35431 int decl_class_or_enum_p;
35432 tree prefix_attributes;
35434 cp_parser_objc_visibility_spec (parser);
35436 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35437 break;
35439 cp_parser_decl_specifier_seq (parser,
35440 CP_PARSER_FLAGS_OPTIONAL,
35441 &declspecs,
35442 &decl_class_or_enum_p);
35444 /* auto, register, static, extern, mutable. */
35445 if (declspecs.storage_class != sc_none)
35447 cp_parser_error (parser, "invalid type for instance variable");
35448 declspecs.storage_class = sc_none;
35451 /* thread_local. */
35452 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
35454 cp_parser_error (parser, "invalid type for instance variable");
35455 declspecs.locations[ds_thread] = 0;
35458 /* typedef. */
35459 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
35461 cp_parser_error (parser, "invalid type for instance variable");
35462 declspecs.locations[ds_typedef] = 0;
35465 prefix_attributes = declspecs.attributes;
35466 declspecs.attributes = NULL_TREE;
35468 /* Keep going until we hit the `;' at the end of the
35469 declaration. */
35470 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35472 tree width = NULL_TREE, attributes, first_attribute, decl;
35473 cp_declarator *declarator = NULL;
35474 int ctor_dtor_or_conv_p;
35476 /* Check for a (possibly unnamed) bitfield declaration. */
35477 token = cp_lexer_peek_token (parser->lexer);
35478 if (token->type == CPP_COLON)
35479 goto eat_colon;
35481 if (token->type == CPP_NAME
35482 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
35483 == CPP_COLON))
35485 /* Get the name of the bitfield. */
35486 declarator = make_id_declarator (NULL_TREE,
35487 cp_parser_identifier (parser),
35488 sfk_none, token->location);
35490 eat_colon:
35491 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
35492 /* Get the width of the bitfield. */
35493 width
35494 = cp_parser_constant_expression (parser);
35496 else
35498 /* Parse the declarator. */
35499 declarator
35500 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35501 CP_PARSER_FLAGS_NONE,
35502 &ctor_dtor_or_conv_p,
35503 /*parenthesized_p=*/NULL,
35504 /*member_p=*/false,
35505 /*friend_p=*/false,
35506 /*static_p=*/false);
35509 /* Look for attributes that apply to the ivar. */
35510 attributes = cp_parser_attributes_opt (parser);
35511 /* Remember which attributes are prefix attributes and
35512 which are not. */
35513 first_attribute = attributes;
35514 /* Combine the attributes. */
35515 attributes = attr_chainon (prefix_attributes, attributes);
35517 if (width)
35518 /* Create the bitfield declaration. */
35519 decl = grokbitfield (declarator, &declspecs,
35520 width, NULL_TREE, attributes);
35521 else
35522 decl = grokfield (declarator, &declspecs,
35523 NULL_TREE, /*init_const_expr_p=*/false,
35524 NULL_TREE, attributes);
35526 /* Add the instance variable. */
35527 if (decl != error_mark_node && decl != NULL_TREE)
35528 objc_add_instance_variable (decl);
35530 /* Reset PREFIX_ATTRIBUTES. */
35531 if (attributes != error_mark_node)
35533 while (attributes && TREE_CHAIN (attributes) != first_attribute)
35534 attributes = TREE_CHAIN (attributes);
35535 if (attributes)
35536 TREE_CHAIN (attributes) = NULL_TREE;
35539 token = cp_lexer_peek_token (parser->lexer);
35541 if (token->type == CPP_COMMA)
35543 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35544 continue;
35546 break;
35549 cp_parser_consume_semicolon_at_end_of_statement (parser);
35550 token = cp_lexer_peek_token (parser->lexer);
35553 if (token->keyword == RID_AT_END)
35554 cp_parser_error (parser, "expected %<}%>");
35556 /* Do not consume the RID_AT_END, so it will be read again as terminating
35557 the @interface of @implementation. */
35558 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
35559 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
35561 /* For historical reasons, we accept an optional semicolon. */
35562 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35563 cp_lexer_consume_token (parser->lexer);
35566 /* Parse an Objective-C protocol declaration. */
35568 static void
35569 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
35571 tree proto, protorefs;
35572 cp_token *tok;
35574 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
35575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35577 tok = cp_lexer_peek_token (parser->lexer);
35578 error_at (tok->location, "identifier expected after %<@protocol%>");
35579 cp_parser_consume_semicolon_at_end_of_statement (parser);
35580 return;
35583 /* See if we have a forward declaration or a definition. */
35584 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
35586 /* Try a forward declaration first. */
35587 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
35589 while (true)
35591 tree id;
35593 id = cp_parser_identifier (parser);
35594 if (id == error_mark_node)
35595 break;
35597 objc_declare_protocol (id, attributes);
35599 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35600 cp_lexer_consume_token (parser->lexer);
35601 else
35602 break;
35604 cp_parser_consume_semicolon_at_end_of_statement (parser);
35607 /* Ok, we got a full-fledged definition (or at least should). */
35608 else
35610 proto = cp_parser_identifier (parser);
35611 protorefs = cp_parser_objc_protocol_refs_opt (parser);
35612 objc_start_protocol (proto, protorefs, attributes);
35613 cp_parser_objc_method_prototype_list (parser);
35617 /* Parse an Objective-C superclass or category. */
35619 static void
35620 cp_parser_objc_superclass_or_category (cp_parser *parser,
35621 bool iface_p,
35622 tree *super,
35623 tree *categ, bool *is_class_extension)
35625 cp_token *next = cp_lexer_peek_token (parser->lexer);
35627 *super = *categ = NULL_TREE;
35628 *is_class_extension = false;
35629 if (next->type == CPP_COLON)
35631 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
35632 *super = cp_parser_identifier (parser);
35634 else if (next->type == CPP_OPEN_PAREN)
35636 matching_parens parens;
35637 parens.consume_open (parser); /* Eat '('. */
35639 /* If there is no category name, and this is an @interface, we
35640 have a class extension. */
35641 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35643 *categ = NULL_TREE;
35644 *is_class_extension = true;
35646 else
35647 *categ = cp_parser_identifier (parser);
35649 parens.require_close (parser);
35653 /* Parse an Objective-C class interface. */
35655 static void
35656 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
35658 tree name, super, categ, protos;
35659 bool is_class_extension;
35661 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
35662 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
35663 name = cp_parser_identifier (parser);
35664 if (name == error_mark_node)
35666 /* It's hard to recover because even if valid @interface stuff
35667 is to follow, we can't compile it (or validate it) if we
35668 don't even know which class it refers to. Let's assume this
35669 was a stray '@interface' token in the stream and skip it.
35671 return;
35673 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
35674 &is_class_extension);
35675 protos = cp_parser_objc_protocol_refs_opt (parser);
35677 /* We have either a class or a category on our hands. */
35678 if (categ || is_class_extension)
35679 objc_start_category_interface (name, categ, protos, attributes);
35680 else
35682 objc_start_class_interface (name, nam_loc, super, protos, attributes);
35683 /* Handle instance variable declarations, if any. */
35684 cp_parser_objc_class_ivars (parser);
35685 objc_continue_interface ();
35688 cp_parser_objc_method_prototype_list (parser);
35691 /* Parse an Objective-C class implementation. */
35693 static void
35694 cp_parser_objc_class_implementation (cp_parser* parser)
35696 tree name, super, categ;
35697 bool is_class_extension;
35699 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
35700 name = cp_parser_identifier (parser);
35701 if (name == error_mark_node)
35703 /* It's hard to recover because even if valid @implementation
35704 stuff is to follow, we can't compile it (or validate it) if
35705 we don't even know which class it refers to. Let's assume
35706 this was a stray '@implementation' token in the stream and
35707 skip it.
35709 return;
35711 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
35712 &is_class_extension);
35714 /* We have either a class or a category on our hands. */
35715 if (categ)
35716 objc_start_category_implementation (name, categ);
35717 else
35719 objc_start_class_implementation (name, super);
35720 /* Handle instance variable declarations, if any. */
35721 cp_parser_objc_class_ivars (parser);
35722 objc_continue_implementation ();
35725 cp_parser_objc_method_definition_list (parser);
35728 /* Consume the @end token and finish off the implementation. */
35730 static void
35731 cp_parser_objc_end_implementation (cp_parser* parser)
35733 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35734 objc_finish_implementation ();
35737 /* Parse an Objective-C declaration. */
35739 static void
35740 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
35742 /* Try to figure out what kind of declaration is present. */
35743 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35745 if (attributes)
35746 switch (kwd->keyword)
35748 case RID_AT_ALIAS:
35749 case RID_AT_CLASS:
35750 case RID_AT_END:
35751 error_at (kwd->location, "attributes may not be specified before"
35752 " the %<@%D%> Objective-C++ keyword",
35753 kwd->u.value);
35754 attributes = NULL;
35755 break;
35756 case RID_AT_IMPLEMENTATION:
35757 warning_at (kwd->location, OPT_Wattributes,
35758 "prefix attributes are ignored before %<@%D%>",
35759 kwd->u.value);
35760 attributes = NULL;
35761 default:
35762 break;
35765 switch (kwd->keyword)
35767 case RID_AT_ALIAS:
35768 cp_parser_objc_alias_declaration (parser);
35769 break;
35770 case RID_AT_CLASS:
35771 cp_parser_objc_class_declaration (parser);
35772 break;
35773 case RID_AT_PROTOCOL:
35774 cp_parser_objc_protocol_declaration (parser, attributes);
35775 break;
35776 case RID_AT_INTERFACE:
35777 cp_parser_objc_class_interface (parser, attributes);
35778 break;
35779 case RID_AT_IMPLEMENTATION:
35780 cp_parser_objc_class_implementation (parser);
35781 break;
35782 case RID_AT_END:
35783 cp_parser_objc_end_implementation (parser);
35784 break;
35785 default:
35786 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35787 kwd->u.value);
35788 cp_parser_skip_to_end_of_block_or_statement (parser);
35792 /* Parse an Objective-C try-catch-finally statement.
35794 objc-try-catch-finally-stmt:
35795 @try compound-statement objc-catch-clause-seq [opt]
35796 objc-finally-clause [opt]
35798 objc-catch-clause-seq:
35799 objc-catch-clause objc-catch-clause-seq [opt]
35801 objc-catch-clause:
35802 @catch ( objc-exception-declaration ) compound-statement
35804 objc-finally-clause:
35805 @finally compound-statement
35807 objc-exception-declaration:
35808 parameter-declaration
35809 '...'
35811 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
35813 Returns NULL_TREE.
35815 PS: This function is identical to c_parser_objc_try_catch_finally_statement
35816 for C. Keep them in sync. */
35818 static tree
35819 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
35821 location_t location;
35822 tree stmt;
35824 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
35825 location = cp_lexer_peek_token (parser->lexer)->location;
35826 objc_maybe_warn_exceptions (location);
35827 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
35828 node, lest it get absorbed into the surrounding block. */
35829 stmt = push_stmt_list ();
35830 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35831 objc_begin_try_stmt (location, pop_stmt_list (stmt));
35833 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
35835 cp_parameter_declarator *parm;
35836 tree parameter_declaration = error_mark_node;
35837 bool seen_open_paren = false;
35838 matching_parens parens;
35840 cp_lexer_consume_token (parser->lexer);
35841 if (parens.require_open (parser))
35842 seen_open_paren = true;
35843 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
35845 /* We have "@catch (...)" (where the '...' are literally
35846 what is in the code). Skip the '...'.
35847 parameter_declaration is set to NULL_TREE, and
35848 objc_being_catch_clauses() knows that that means
35849 '...'. */
35850 cp_lexer_consume_token (parser->lexer);
35851 parameter_declaration = NULL_TREE;
35853 else
35855 /* We have "@catch (NSException *exception)" or something
35856 like that. Parse the parameter declaration. */
35857 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35858 false, NULL);
35859 if (parm == NULL)
35860 parameter_declaration = error_mark_node;
35861 else
35862 parameter_declaration = grokdeclarator (parm->declarator,
35863 &parm->decl_specifiers,
35864 PARM, /*initialized=*/0,
35865 /*attrlist=*/NULL);
35867 if (seen_open_paren)
35868 parens.require_close (parser);
35869 else
35871 /* If there was no open parenthesis, we are recovering from
35872 an error, and we are trying to figure out what mistake
35873 the user has made. */
35875 /* If there is an immediate closing parenthesis, the user
35876 probably forgot the opening one (ie, they typed "@catch
35877 NSException *e)". Parse the closing parenthesis and keep
35878 going. */
35879 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35880 cp_lexer_consume_token (parser->lexer);
35882 /* If these is no immediate closing parenthesis, the user
35883 probably doesn't know that parenthesis are required at
35884 all (ie, they typed "@catch NSException *e"). So, just
35885 forget about the closing parenthesis and keep going. */
35887 objc_begin_catch_clause (parameter_declaration);
35888 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35889 objc_finish_catch_clause ();
35891 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
35893 cp_lexer_consume_token (parser->lexer);
35894 location = cp_lexer_peek_token (parser->lexer)->location;
35895 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
35896 node, lest it get absorbed into the surrounding block. */
35897 stmt = push_stmt_list ();
35898 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35899 objc_build_finally_clause (location, pop_stmt_list (stmt));
35902 return objc_finish_try_stmt ();
35905 /* Parse an Objective-C synchronized statement.
35907 objc-synchronized-stmt:
35908 @synchronized ( expression ) compound-statement
35910 Returns NULL_TREE. */
35912 static tree
35913 cp_parser_objc_synchronized_statement (cp_parser *parser)
35915 location_t location;
35916 tree lock, stmt;
35918 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
35920 location = cp_lexer_peek_token (parser->lexer)->location;
35921 objc_maybe_warn_exceptions (location);
35922 matching_parens parens;
35923 parens.require_open (parser);
35924 lock = cp_parser_expression (parser);
35925 parens.require_close (parser);
35927 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
35928 node, lest it get absorbed into the surrounding block. */
35929 stmt = push_stmt_list ();
35930 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35932 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
35935 /* Parse an Objective-C throw statement.
35937 objc-throw-stmt:
35938 @throw assignment-expression [opt] ;
35940 Returns a constructed '@throw' statement. */
35942 static tree
35943 cp_parser_objc_throw_statement (cp_parser *parser)
35945 tree expr = NULL_TREE;
35946 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35948 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
35950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35951 expr = cp_parser_expression (parser);
35953 cp_parser_consume_semicolon_at_end_of_statement (parser);
35955 return objc_build_throw_stmt (loc, expr);
35958 /* Parse an Objective-C statement. */
35960 static tree
35961 cp_parser_objc_statement (cp_parser * parser)
35963 /* Try to figure out what kind of declaration is present. */
35964 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35966 switch (kwd->keyword)
35968 case RID_AT_TRY:
35969 return cp_parser_objc_try_catch_finally_statement (parser);
35970 case RID_AT_SYNCHRONIZED:
35971 return cp_parser_objc_synchronized_statement (parser);
35972 case RID_AT_THROW:
35973 return cp_parser_objc_throw_statement (parser);
35974 default:
35975 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35976 kwd->u.value);
35977 cp_parser_skip_to_end_of_block_or_statement (parser);
35980 return error_mark_node;
35983 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
35984 look ahead to see if an objc keyword follows the attributes. This
35985 is to detect the use of prefix attributes on ObjC @interface and
35986 @protocol. */
35988 static bool
35989 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
35991 cp_lexer_save_tokens (parser->lexer);
35992 tree addon = cp_parser_attributes_opt (parser);
35993 if (addon
35994 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
35996 cp_lexer_commit_tokens (parser->lexer);
35997 if (*attrib)
35998 TREE_CHAIN (*attrib) = addon;
35999 else
36000 *attrib = addon;
36001 return true;
36003 cp_lexer_rollback_tokens (parser->lexer);
36004 return false;
36007 /* This routine is a minimal replacement for
36008 c_parser_struct_declaration () used when parsing the list of
36009 types/names or ObjC++ properties. For example, when parsing the
36010 code
36012 @property (readonly) int a, b, c;
36014 this function is responsible for parsing "int a, int b, int c" and
36015 returning the declarations as CHAIN of DECLs.
36017 TODO: Share this code with cp_parser_objc_class_ivars. It's very
36018 similar parsing. */
36019 static tree
36020 cp_parser_objc_struct_declaration (cp_parser *parser)
36022 tree decls = NULL_TREE;
36023 cp_decl_specifier_seq declspecs;
36024 int decl_class_or_enum_p;
36025 tree prefix_attributes;
36027 cp_parser_decl_specifier_seq (parser,
36028 CP_PARSER_FLAGS_NONE,
36029 &declspecs,
36030 &decl_class_or_enum_p);
36032 if (declspecs.type == error_mark_node)
36033 return error_mark_node;
36035 /* auto, register, static, extern, mutable. */
36036 if (declspecs.storage_class != sc_none)
36038 cp_parser_error (parser, "invalid type for property");
36039 declspecs.storage_class = sc_none;
36042 /* thread_local. */
36043 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36045 cp_parser_error (parser, "invalid type for property");
36046 declspecs.locations[ds_thread] = 0;
36049 /* typedef. */
36050 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36052 cp_parser_error (parser, "invalid type for property");
36053 declspecs.locations[ds_typedef] = 0;
36056 prefix_attributes = declspecs.attributes;
36057 declspecs.attributes = NULL_TREE;
36059 /* Keep going until we hit the `;' at the end of the declaration. */
36060 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36062 tree attributes, first_attribute, decl;
36063 cp_declarator *declarator;
36064 cp_token *token;
36066 /* Parse the declarator. */
36067 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36068 CP_PARSER_FLAGS_NONE,
36069 NULL, NULL, false, false, false);
36071 /* Look for attributes that apply to the ivar. */
36072 attributes = cp_parser_attributes_opt (parser);
36073 /* Remember which attributes are prefix attributes and
36074 which are not. */
36075 first_attribute = attributes;
36076 /* Combine the attributes. */
36077 attributes = attr_chainon (prefix_attributes, attributes);
36079 decl = grokfield (declarator, &declspecs,
36080 NULL_TREE, /*init_const_expr_p=*/false,
36081 NULL_TREE, attributes);
36083 if (decl == error_mark_node || decl == NULL_TREE)
36084 return error_mark_node;
36086 /* Reset PREFIX_ATTRIBUTES. */
36087 if (attributes != error_mark_node)
36089 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36090 attributes = TREE_CHAIN (attributes);
36091 if (attributes)
36092 TREE_CHAIN (attributes) = NULL_TREE;
36095 DECL_CHAIN (decl) = decls;
36096 decls = decl;
36098 token = cp_lexer_peek_token (parser->lexer);
36099 if (token->type == CPP_COMMA)
36101 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36102 continue;
36104 else
36105 break;
36107 return decls;
36110 /* Parse an Objective-C @property declaration. The syntax is:
36112 objc-property-declaration:
36113 '@property' objc-property-attributes[opt] struct-declaration ;
36115 objc-property-attributes:
36116 '(' objc-property-attribute-list ')'
36118 objc-property-attribute-list:
36119 objc-property-attribute
36120 objc-property-attribute-list, objc-property-attribute
36122 objc-property-attribute
36123 'getter' = identifier
36124 'setter' = identifier
36125 'readonly'
36126 'readwrite'
36127 'assign'
36128 'retain'
36129 'copy'
36130 'nonatomic'
36132 For example:
36133 @property NSString *name;
36134 @property (readonly) id object;
36135 @property (retain, nonatomic, getter=getTheName) id name;
36136 @property int a, b, c;
36138 PS: This function is identical to
36139 c_parser_objc_at_property_declaration for C. Keep them in sync. */
36140 static void
36141 cp_parser_objc_at_property_declaration (cp_parser *parser)
36143 /* Parse the optional attribute list.
36145 A list of parsed, but not verified, attributes. */
36146 auto_delete_vec<property_attribute_info> prop_attr_list;
36147 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36149 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
36151 /* Parse the optional attribute list... */
36152 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36154 /* Eat the '('. */
36155 matching_parens parens;
36156 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
36157 parens.consume_open (parser);
36158 bool syntax_error = false;
36160 /* Allow empty @property attribute lists, but with a warning. */
36161 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
36162 location_t attr_comb;
36163 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36165 attr_comb = make_location (attr_end, attr_start, attr_end);
36166 warning_at (attr_comb, OPT_Wattributes,
36167 "empty property attribute list");
36169 else
36170 while (true)
36172 cp_token *token = cp_lexer_peek_token (parser->lexer);
36173 attr_start = token->location;
36174 attr_end = get_finish (token->location);
36175 attr_comb = make_location (attr_start, attr_start, attr_end);
36177 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
36179 warning_at (attr_comb, OPT_Wattributes,
36180 "missing property attribute");
36181 if (token->type == CPP_CLOSE_PAREN)
36182 break;
36183 cp_lexer_consume_token (parser->lexer);
36184 continue;
36187 tree attr_name = NULL_TREE;
36188 if (identifier_p (token->u.value))
36189 attr_name = token->u.value;
36191 enum rid keyword;
36192 if (token->type == CPP_NAME)
36193 keyword = C_RID_CODE (token->u.value);
36194 else if (token->type == CPP_KEYWORD
36195 && token->keyword == RID_CLASS)
36196 /* Account for accepting the 'class' keyword in this context. */
36197 keyword = RID_CLASS;
36198 else
36199 keyword = RID_MAX; /* By definition, an unknown property. */
36200 cp_lexer_consume_token (parser->lexer);
36202 enum objc_property_attribute_kind prop_kind
36203 = objc_prop_attr_kind_for_rid (keyword);
36204 property_attribute_info *prop
36205 = new property_attribute_info (attr_name, attr_comb, prop_kind);
36206 prop_attr_list.safe_push (prop);
36208 tree meth_name;
36209 switch (prop->prop_kind)
36211 default: break;
36212 case OBJC_PROPERTY_ATTR_UNKNOWN:
36213 if (attr_name)
36214 error_at (attr_start, "unknown property attribute %qE",
36215 attr_name);
36216 else
36217 error_at (attr_start, "unknown property attribute");
36218 prop->parse_error = syntax_error = true;
36219 break;
36221 case OBJC_PROPERTY_ATTR_GETTER:
36222 case OBJC_PROPERTY_ATTR_SETTER:
36223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36225 attr_comb = make_location (attr_end, attr_start, attr_end);
36226 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
36227 attr_name);
36228 prop->parse_error = syntax_error = true;
36229 break;
36232 token = cp_lexer_peek_token (parser->lexer);
36233 attr_end = token->location;
36234 cp_lexer_consume_token (parser->lexer); /* eat the = */
36236 if (!cp_parser_objc_selector_p
36237 (cp_lexer_peek_token (parser->lexer)->type))
36239 attr_comb = make_location (attr_end, attr_start, attr_end);
36240 error_at (attr_comb, "expected %qE selector name",
36241 attr_name);
36242 prop->parse_error = syntax_error = true;
36243 break;
36246 /* Get the end of the method name, and consume the name. */
36247 token = cp_lexer_peek_token (parser->lexer);
36248 attr_end = get_finish (token->location);
36249 /* Because method names may contain C++ keywords, we have a
36250 routine to fetch them (this also consumes the token). */
36251 meth_name = cp_parser_objc_selector (parser);
36253 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
36255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36257 attr_comb = make_location (attr_end, attr_start,
36258 attr_end);
36259 error_at (attr_comb, "setter method names must"
36260 " terminate with %<:%>");
36261 prop->parse_error = syntax_error = true;
36263 else
36265 attr_end = get_finish (cp_lexer_peek_token
36266 (parser->lexer)->location);
36267 cp_lexer_consume_token (parser->lexer);
36269 attr_comb = make_location (attr_start, attr_start,
36270 attr_end);
36272 else
36273 attr_comb = make_location (attr_start, attr_start,
36274 attr_end);
36275 prop->ident = meth_name;
36276 /* Updated location including all that was successfully
36277 parsed. */
36278 prop->prop_loc = attr_comb;
36279 break;
36282 /* If we see a comma here, then keep going - even if we already
36283 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
36284 this makes a more useful output and avoid spurious warnings
36285 about missing attributes that are, in fact, specified after the
36286 one with the syntax error. */
36287 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36288 cp_lexer_consume_token (parser->lexer);
36289 else
36290 break;
36293 if (syntax_error || !parens.require_close (parser))
36294 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36295 /*or_comma=*/false,
36296 /*consume_paren=*/true);
36299 /* 'properties' is the list of properties that we read. Usually a
36300 single one, but maybe more (eg, in "@property int a, b, c;" there
36301 are three).
36302 TODO: Update this parsing so that it accepts (erroneous) bitfields so
36303 that we can issue a meaningful and consistent (between C/C++) error
36304 message from objc_add_property_declaration (). */
36305 tree properties = cp_parser_objc_struct_declaration (parser);
36307 if (properties == error_mark_node)
36308 cp_parser_skip_to_end_of_statement (parser);
36309 else if (properties == NULL_TREE)
36310 cp_parser_error (parser, "expected identifier");
36311 else
36313 /* Comma-separated properties are chained together in reverse order;
36314 add them one by one. */
36315 properties = nreverse (properties);
36316 for (; properties; properties = TREE_CHAIN (properties))
36317 objc_add_property_declaration (loc, copy_node (properties),
36318 prop_attr_list);
36321 cp_parser_consume_semicolon_at_end_of_statement (parser);
36324 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
36326 objc-synthesize-declaration:
36327 @synthesize objc-synthesize-identifier-list ;
36329 objc-synthesize-identifier-list:
36330 objc-synthesize-identifier
36331 objc-synthesize-identifier-list, objc-synthesize-identifier
36333 objc-synthesize-identifier
36334 identifier
36335 identifier = identifier
36337 For example:
36338 @synthesize MyProperty;
36339 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
36341 PS: This function is identical to c_parser_objc_at_synthesize_declaration
36342 for C. Keep them in sync.
36344 static void
36345 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
36347 tree list = NULL_TREE;
36348 location_t loc;
36349 loc = cp_lexer_peek_token (parser->lexer)->location;
36351 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
36352 while (true)
36354 tree property, ivar;
36355 property = cp_parser_identifier (parser);
36356 if (property == error_mark_node)
36358 cp_parser_consume_semicolon_at_end_of_statement (parser);
36359 return;
36361 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
36363 cp_lexer_consume_token (parser->lexer);
36364 ivar = cp_parser_identifier (parser);
36365 if (ivar == error_mark_node)
36367 cp_parser_consume_semicolon_at_end_of_statement (parser);
36368 return;
36371 else
36372 ivar = NULL_TREE;
36373 list = chainon (list, build_tree_list (ivar, property));
36374 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36375 cp_lexer_consume_token (parser->lexer);
36376 else
36377 break;
36379 cp_parser_consume_semicolon_at_end_of_statement (parser);
36380 objc_add_synthesize_declaration (loc, list);
36383 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
36385 objc-dynamic-declaration:
36386 @dynamic identifier-list ;
36388 For example:
36389 @dynamic MyProperty;
36390 @dynamic MyProperty, AnotherProperty;
36392 PS: This function is identical to c_parser_objc_at_dynamic_declaration
36393 for C. Keep them in sync.
36395 static void
36396 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
36398 tree list = NULL_TREE;
36399 location_t loc;
36400 loc = cp_lexer_peek_token (parser->lexer)->location;
36402 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
36403 while (true)
36405 tree property;
36406 property = cp_parser_identifier (parser);
36407 if (property == error_mark_node)
36409 cp_parser_consume_semicolon_at_end_of_statement (parser);
36410 return;
36412 list = chainon (list, build_tree_list (NULL, property));
36413 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36414 cp_lexer_consume_token (parser->lexer);
36415 else
36416 break;
36418 cp_parser_consume_semicolon_at_end_of_statement (parser);
36419 objc_add_dynamic_declaration (loc, list);
36423 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
36425 /* Returns name of the next clause.
36426 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
36427 the token is not consumed. Otherwise appropriate pragma_omp_clause is
36428 returned and the token is consumed. */
36430 static pragma_omp_clause
36431 cp_parser_omp_clause_name (cp_parser *parser)
36433 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
36435 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36436 result = PRAGMA_OACC_CLAUSE_AUTO;
36437 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
36438 result = PRAGMA_OMP_CLAUSE_IF;
36439 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
36440 result = PRAGMA_OMP_CLAUSE_DEFAULT;
36441 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
36442 result = PRAGMA_OACC_CLAUSE_DELETE;
36443 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
36444 result = PRAGMA_OMP_CLAUSE_PRIVATE;
36445 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36446 result = PRAGMA_OMP_CLAUSE_FOR;
36447 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36449 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36450 const char *p = IDENTIFIER_POINTER (id);
36452 switch (p[0])
36454 case 'a':
36455 if (!strcmp ("affinity", p))
36456 result = PRAGMA_OMP_CLAUSE_AFFINITY;
36457 else if (!strcmp ("aligned", p))
36458 result = PRAGMA_OMP_CLAUSE_ALIGNED;
36459 else if (!strcmp ("allocate", p))
36460 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
36461 else if (!strcmp ("async", p))
36462 result = PRAGMA_OACC_CLAUSE_ASYNC;
36463 else if (!strcmp ("attach", p))
36464 result = PRAGMA_OACC_CLAUSE_ATTACH;
36465 break;
36466 case 'b':
36467 if (!strcmp ("bind", p))
36468 result = PRAGMA_OMP_CLAUSE_BIND;
36469 break;
36470 case 'c':
36471 if (!strcmp ("collapse", p))
36472 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
36473 else if (!strcmp ("copy", p))
36474 result = PRAGMA_OACC_CLAUSE_COPY;
36475 else if (!strcmp ("copyin", p))
36476 result = PRAGMA_OMP_CLAUSE_COPYIN;
36477 else if (!strcmp ("copyout", p))
36478 result = PRAGMA_OACC_CLAUSE_COPYOUT;
36479 else if (!strcmp ("copyprivate", p))
36480 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
36481 else if (!strcmp ("create", p))
36482 result = PRAGMA_OACC_CLAUSE_CREATE;
36483 break;
36484 case 'd':
36485 if (!strcmp ("defaultmap", p))
36486 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
36487 else if (!strcmp ("depend", p))
36488 result = PRAGMA_OMP_CLAUSE_DEPEND;
36489 else if (!strcmp ("detach", p))
36490 result = PRAGMA_OACC_CLAUSE_DETACH;
36491 else if (!strcmp ("device", p))
36492 result = PRAGMA_OMP_CLAUSE_DEVICE;
36493 else if (!strcmp ("deviceptr", p))
36494 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
36495 else if (!strcmp ("device_resident", p))
36496 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
36497 else if (!strcmp ("device_type", p))
36498 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
36499 else if (!strcmp ("dist_schedule", p))
36500 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
36501 break;
36502 case 'e':
36503 if (!strcmp ("enter", p))
36504 result = PRAGMA_OMP_CLAUSE_ENTER;
36505 break;
36506 case 'f':
36507 if (!strcmp ("filter", p))
36508 result = PRAGMA_OMP_CLAUSE_FILTER;
36509 else if (!strcmp ("final", p))
36510 result = PRAGMA_OMP_CLAUSE_FINAL;
36511 else if (!strcmp ("finalize", p))
36512 result = PRAGMA_OACC_CLAUSE_FINALIZE;
36513 else if (!strcmp ("firstprivate", p))
36514 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
36515 else if (!strcmp ("from", p))
36516 result = PRAGMA_OMP_CLAUSE_FROM;
36517 break;
36518 case 'g':
36519 if (!strcmp ("gang", p))
36520 result = PRAGMA_OACC_CLAUSE_GANG;
36521 else if (!strcmp ("grainsize", p))
36522 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
36523 break;
36524 case 'h':
36525 if (!strcmp ("has_device_addr", p))
36526 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
36527 else if (!strcmp ("hint", p))
36528 result = PRAGMA_OMP_CLAUSE_HINT;
36529 else if (!strcmp ("host", p))
36530 result = PRAGMA_OACC_CLAUSE_HOST;
36531 break;
36532 case 'i':
36533 if (!strcmp ("if_present", p))
36534 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
36535 else if (!strcmp ("in_reduction", p))
36536 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
36537 else if (!strcmp ("inbranch", p))
36538 result = PRAGMA_OMP_CLAUSE_INBRANCH;
36539 else if (!strcmp ("independent", p))
36540 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
36541 else if (!strcmp ("is_device_ptr", p))
36542 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
36543 break;
36544 case 'l':
36545 if (!strcmp ("lastprivate", p))
36546 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
36547 else if (!strcmp ("linear", p))
36548 result = PRAGMA_OMP_CLAUSE_LINEAR;
36549 else if (!strcmp ("link", p))
36550 result = PRAGMA_OMP_CLAUSE_LINK;
36551 break;
36552 case 'm':
36553 if (!strcmp ("map", p))
36554 result = PRAGMA_OMP_CLAUSE_MAP;
36555 else if (!strcmp ("mergeable", p))
36556 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
36557 break;
36558 case 'n':
36559 if (!strcmp ("no_create", p))
36560 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
36561 else if (!strcmp ("nogroup", p))
36562 result = PRAGMA_OMP_CLAUSE_NOGROUP;
36563 else if (!strcmp ("nohost", p))
36564 result = PRAGMA_OACC_CLAUSE_NOHOST;
36565 else if (!strcmp ("nontemporal", p))
36566 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
36567 else if (!strcmp ("notinbranch", p))
36568 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
36569 else if (!strcmp ("nowait", p))
36570 result = PRAGMA_OMP_CLAUSE_NOWAIT;
36571 else if (!strcmp ("num_gangs", p))
36572 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
36573 else if (!strcmp ("num_tasks", p))
36574 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
36575 else if (!strcmp ("num_teams", p))
36576 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
36577 else if (!strcmp ("num_threads", p))
36578 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
36579 else if (!strcmp ("num_workers", p))
36580 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
36581 break;
36582 case 'o':
36583 if (!strcmp ("ordered", p))
36584 result = PRAGMA_OMP_CLAUSE_ORDERED;
36585 else if (!strcmp ("order", p))
36586 result = PRAGMA_OMP_CLAUSE_ORDER;
36587 break;
36588 case 'p':
36589 if (!strcmp ("parallel", p))
36590 result = PRAGMA_OMP_CLAUSE_PARALLEL;
36591 else if (!strcmp ("present", p))
36592 result = PRAGMA_OACC_CLAUSE_PRESENT;
36593 else if (!strcmp ("present_or_copy", p)
36594 || !strcmp ("pcopy", p))
36595 result = PRAGMA_OACC_CLAUSE_COPY;
36596 else if (!strcmp ("present_or_copyin", p)
36597 || !strcmp ("pcopyin", p))
36598 result = PRAGMA_OACC_CLAUSE_COPYIN;
36599 else if (!strcmp ("present_or_copyout", p)
36600 || !strcmp ("pcopyout", p))
36601 result = PRAGMA_OACC_CLAUSE_COPYOUT;
36602 else if (!strcmp ("present_or_create", p)
36603 || !strcmp ("pcreate", p))
36604 result = PRAGMA_OACC_CLAUSE_CREATE;
36605 else if (!strcmp ("priority", p))
36606 result = PRAGMA_OMP_CLAUSE_PRIORITY;
36607 else if (!strcmp ("proc_bind", p))
36608 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
36609 break;
36610 case 'r':
36611 if (!strcmp ("reduction", p))
36612 result = PRAGMA_OMP_CLAUSE_REDUCTION;
36613 break;
36614 case 's':
36615 if (!strcmp ("safelen", p))
36616 result = PRAGMA_OMP_CLAUSE_SAFELEN;
36617 else if (!strcmp ("schedule", p))
36618 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
36619 else if (!strcmp ("sections", p))
36620 result = PRAGMA_OMP_CLAUSE_SECTIONS;
36621 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
36622 result = PRAGMA_OACC_CLAUSE_HOST;
36623 else if (!strcmp ("seq", p))
36624 result = PRAGMA_OACC_CLAUSE_SEQ;
36625 else if (!strcmp ("shared", p))
36626 result = PRAGMA_OMP_CLAUSE_SHARED;
36627 else if (!strcmp ("simd", p))
36628 result = PRAGMA_OMP_CLAUSE_SIMD;
36629 else if (!strcmp ("simdlen", p))
36630 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
36631 break;
36632 case 't':
36633 if (!strcmp ("task_reduction", p))
36634 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
36635 else if (!strcmp ("taskgroup", p))
36636 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
36637 else if (!strcmp ("thread_limit", p))
36638 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
36639 else if (!strcmp ("threads", p))
36640 result = PRAGMA_OMP_CLAUSE_THREADS;
36641 else if (!strcmp ("tile", p))
36642 result = PRAGMA_OACC_CLAUSE_TILE;
36643 else if (!strcmp ("to", p))
36644 result = PRAGMA_OMP_CLAUSE_TO;
36645 break;
36646 case 'u':
36647 if (!strcmp ("uniform", p))
36648 result = PRAGMA_OMP_CLAUSE_UNIFORM;
36649 else if (!strcmp ("untied", p))
36650 result = PRAGMA_OMP_CLAUSE_UNTIED;
36651 else if (!strcmp ("use_device", p))
36652 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
36653 else if (!strcmp ("use_device_addr", p))
36654 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
36655 else if (!strcmp ("use_device_ptr", p))
36656 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
36657 break;
36658 case 'v':
36659 if (!strcmp ("vector", p))
36660 result = PRAGMA_OACC_CLAUSE_VECTOR;
36661 else if (!strcmp ("vector_length", p))
36662 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
36663 break;
36664 case 'w':
36665 if (!strcmp ("wait", p))
36666 result = PRAGMA_OACC_CLAUSE_WAIT;
36667 else if (!strcmp ("worker", p))
36668 result = PRAGMA_OACC_CLAUSE_WORKER;
36669 break;
36673 if (result != PRAGMA_OMP_CLAUSE_NONE)
36674 cp_lexer_consume_token (parser->lexer);
36676 return result;
36679 /* Validate that a clause of the given type does not already exist. */
36681 static void
36682 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
36683 const char *name, location_t location)
36685 if (omp_find_clause (clauses, code))
36686 error_at (location, "too many %qs clauses", name);
36689 /* OpenMP 2.5:
36690 variable-list:
36691 identifier
36692 variable-list , identifier
36694 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
36695 colon). An opening parenthesis will have been consumed by the caller.
36697 If KIND is nonzero, create the appropriate node and install the decl
36698 in OMP_CLAUSE_DECL and add the node to the head of the list.
36700 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
36701 return the list created.
36703 COLON can be NULL if only closing parenthesis should end the list,
36704 or pointer to bool which will receive false if the list is terminated
36705 by closing parenthesis or true if the list is terminated by colon.
36707 The optional ALLOW_DEREF argument is true if list items can use the deref
36708 (->) operator. */
36710 struct omp_dim
36712 tree low_bound, length;
36713 location_t loc;
36714 bool no_colon;
36715 omp_dim (tree lb, tree len, location_t lo, bool nc)
36716 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
36719 static tree
36720 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
36721 tree list, bool *colon,
36722 bool allow_deref = false)
36724 auto_vec<omp_dim> dims;
36725 bool array_section_p;
36726 cp_token *token;
36727 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36728 if (colon)
36730 parser->colon_corrects_to_scope_p = false;
36731 *colon = false;
36733 while (1)
36735 tree name, decl;
36737 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36738 cp_parser_parse_tentatively (parser);
36739 token = cp_lexer_peek_token (parser->lexer);
36740 if (kind != 0
36741 && cp_parser_is_keyword (token, RID_THIS))
36743 decl = finish_this_expr ();
36744 if (TREE_CODE (decl) == NON_LVALUE_EXPR
36745 || CONVERT_EXPR_P (decl))
36746 decl = TREE_OPERAND (decl, 0);
36747 cp_lexer_consume_token (parser->lexer);
36749 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
36750 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
36751 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
36753 cp_id_kind idk;
36754 decl = cp_parser_primary_expression (parser, false, false, false,
36755 &idk);
36757 else if (kind == OMP_CLAUSE_DEPEND
36758 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
36759 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
36760 || cp_lexer_nth_token_is (parser->lexer, 2,
36761 CPP_CLOSE_PAREN)))
36763 decl = ridpointers[RID_OMP_ALL_MEMORY];
36764 cp_lexer_consume_token (parser->lexer);
36766 else
36768 name = cp_parser_id_expression (parser, /*template_p=*/false,
36769 /*check_dependency_p=*/true,
36770 /*template_p=*/NULL,
36771 /*declarator_p=*/false,
36772 /*optional_p=*/false);
36773 if (name == error_mark_node)
36775 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36776 && cp_parser_simulate_error (parser))
36777 goto depend_lvalue;
36778 goto skip_comma;
36781 if (identifier_p (name))
36782 decl = cp_parser_lookup_name_simple (parser, name, token->location);
36783 else
36784 decl = name;
36785 if (decl == error_mark_node)
36787 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36788 && cp_parser_simulate_error (parser))
36789 goto depend_lvalue;
36790 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
36791 token->location);
36794 if (outer_automatic_var_p (decl))
36795 decl = process_outer_var_ref (decl, tf_warning_or_error);
36796 if (decl == error_mark_node)
36798 else if (kind != 0)
36800 switch (kind)
36802 case OMP_CLAUSE__CACHE_:
36803 /* The OpenACC cache directive explicitly only allows "array
36804 elements or subarrays". */
36805 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
36807 error_at (token->location, "expected %<[%>");
36808 decl = error_mark_node;
36809 break;
36811 /* FALLTHROUGH. */
36812 case OMP_CLAUSE_MAP:
36813 case OMP_CLAUSE_FROM:
36814 case OMP_CLAUSE_TO:
36815 start_component_ref:
36816 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36817 || (allow_deref
36818 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
36820 cpp_ttype ttype
36821 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36822 ? CPP_DOT : CPP_DEREF;
36823 location_t loc
36824 = cp_lexer_peek_token (parser->lexer)->location;
36825 cp_id_kind idk = CP_ID_KIND_NONE;
36826 cp_lexer_consume_token (parser->lexer);
36827 decl = convert_from_reference (decl);
36828 decl
36829 = cp_parser_postfix_dot_deref_expression (parser, ttype,
36830 decl, false,
36831 &idk, loc);
36833 /* FALLTHROUGH. */
36834 case OMP_CLAUSE_AFFINITY:
36835 case OMP_CLAUSE_DEPEND:
36836 case OMP_CLAUSE_REDUCTION:
36837 case OMP_CLAUSE_IN_REDUCTION:
36838 case OMP_CLAUSE_TASK_REDUCTION:
36839 case OMP_CLAUSE_HAS_DEVICE_ADDR:
36840 array_section_p = false;
36841 dims.truncate (0);
36842 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
36844 location_t loc = UNKNOWN_LOCATION;
36845 tree low_bound = NULL_TREE, length = NULL_TREE;
36846 bool no_colon = false;
36848 parser->colon_corrects_to_scope_p = false;
36849 cp_lexer_consume_token (parser->lexer);
36850 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36852 loc = cp_lexer_peek_token (parser->lexer)->location;
36853 low_bound = cp_parser_expression (parser);
36854 /* Later handling is not prepared to see through these. */
36855 gcc_checking_assert (!location_wrapper_p (low_bound));
36857 if (!colon)
36858 parser->colon_corrects_to_scope_p
36859 = saved_colon_corrects_to_scope_p;
36860 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
36862 length = integer_one_node;
36863 no_colon = true;
36865 else
36867 /* Look for `:'. */
36868 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36870 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36871 && cp_parser_simulate_error (parser))
36872 goto depend_lvalue;
36873 goto skip_comma;
36875 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36876 cp_parser_commit_to_tentative_parse (parser);
36877 else
36878 array_section_p = true;
36879 if (!cp_lexer_next_token_is (parser->lexer,
36880 CPP_CLOSE_SQUARE))
36882 length = cp_parser_expression (parser);
36883 /* Later handling is not prepared to see through these. */
36884 gcc_checking_assert (!location_wrapper_p (length));
36887 /* Look for the closing `]'. */
36888 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
36889 RT_CLOSE_SQUARE))
36891 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36892 && cp_parser_simulate_error (parser))
36893 goto depend_lvalue;
36894 goto skip_comma;
36897 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
36900 if ((kind == OMP_CLAUSE_MAP
36901 || kind == OMP_CLAUSE_FROM
36902 || kind == OMP_CLAUSE_TO)
36903 && !array_section_p
36904 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36905 || (allow_deref
36906 && cp_lexer_next_token_is (parser->lexer,
36907 CPP_DEREF))))
36909 for (unsigned i = 0; i < dims.length (); i++)
36911 gcc_assert (dims[i].length == integer_one_node);
36912 decl = build_array_ref (dims[i].loc,
36913 decl, dims[i].low_bound);
36915 goto start_component_ref;
36917 else
36918 for (unsigned i = 0; i < dims.length (); i++)
36919 decl = tree_cons (dims[i].low_bound, dims[i].length, decl);
36921 break;
36922 default:
36923 break;
36926 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
36929 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
36930 && cp_parser_simulate_error (parser))
36932 depend_lvalue:
36933 cp_parser_abort_tentative_parse (parser);
36934 decl = cp_parser_assignment_expression (parser, NULL,
36935 false, false);
36937 else
36938 cp_parser_parse_definitely (parser);
36941 tree u = build_omp_clause (token->location, kind);
36942 OMP_CLAUSE_DECL (u) = decl;
36943 OMP_CLAUSE_CHAIN (u) = list;
36944 list = u;
36946 else
36947 list = tree_cons (decl, NULL_TREE, list);
36949 get_comma:
36950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
36951 break;
36952 cp_lexer_consume_token (parser->lexer);
36955 if (colon)
36956 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36958 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36960 *colon = true;
36961 cp_parser_require (parser, CPP_COLON, RT_COLON);
36962 return list;
36965 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36967 int ending;
36969 /* Try to resync to an unnested comma. Copied from
36970 cp_parser_parenthesized_expression_list. */
36971 skip_comma:
36972 if (colon)
36973 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36974 ending = cp_parser_skip_to_closing_parenthesis (parser,
36975 /*recovering=*/true,
36976 /*or_comma=*/true,
36977 /*consume_paren=*/true);
36978 if (ending < 0)
36979 goto get_comma;
36982 return list;
36985 /* Similarly, but expect leading and trailing parenthesis. This is a very
36986 common case for omp clauses. */
36988 static tree
36989 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
36990 bool allow_deref = false)
36992 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36993 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
36994 allow_deref);
36995 return list;
36998 /* OpenACC 2.0:
36999 copy ( variable-list )
37000 copyin ( variable-list )
37001 copyout ( variable-list )
37002 create ( variable-list )
37003 delete ( variable-list )
37004 present ( variable-list )
37006 OpenACC 2.6:
37007 no_create ( variable-list )
37008 attach ( variable-list )
37009 detach ( variable-list ) */
37011 static tree
37012 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
37013 tree list)
37015 enum gomp_map_kind kind;
37016 switch (c_kind)
37018 case PRAGMA_OACC_CLAUSE_ATTACH:
37019 kind = GOMP_MAP_ATTACH;
37020 break;
37021 case PRAGMA_OACC_CLAUSE_COPY:
37022 kind = GOMP_MAP_TOFROM;
37023 break;
37024 case PRAGMA_OACC_CLAUSE_COPYIN:
37025 kind = GOMP_MAP_TO;
37026 break;
37027 case PRAGMA_OACC_CLAUSE_COPYOUT:
37028 kind = GOMP_MAP_FROM;
37029 break;
37030 case PRAGMA_OACC_CLAUSE_CREATE:
37031 kind = GOMP_MAP_ALLOC;
37032 break;
37033 case PRAGMA_OACC_CLAUSE_DELETE:
37034 kind = GOMP_MAP_RELEASE;
37035 break;
37036 case PRAGMA_OACC_CLAUSE_DETACH:
37037 kind = GOMP_MAP_DETACH;
37038 break;
37039 case PRAGMA_OACC_CLAUSE_DEVICE:
37040 kind = GOMP_MAP_FORCE_TO;
37041 break;
37042 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
37043 kind = GOMP_MAP_DEVICE_RESIDENT;
37044 break;
37045 case PRAGMA_OACC_CLAUSE_HOST:
37046 kind = GOMP_MAP_FORCE_FROM;
37047 break;
37048 case PRAGMA_OACC_CLAUSE_LINK:
37049 kind = GOMP_MAP_LINK;
37050 break;
37051 case PRAGMA_OACC_CLAUSE_NO_CREATE:
37052 kind = GOMP_MAP_IF_PRESENT;
37053 break;
37054 case PRAGMA_OACC_CLAUSE_PRESENT:
37055 kind = GOMP_MAP_FORCE_PRESENT;
37056 break;
37057 default:
37058 gcc_unreachable ();
37060 tree nl, c;
37061 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
37063 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
37064 OMP_CLAUSE_SET_MAP_KIND (c, kind);
37066 return nl;
37069 /* OpenACC 2.0:
37070 deviceptr ( variable-list ) */
37072 static tree
37073 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
37075 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37076 tree vars, t;
37078 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
37079 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
37080 variable-list must only allow for pointer variables. */
37081 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37082 for (t = vars; t; t = TREE_CHAIN (t))
37084 tree v = TREE_PURPOSE (t);
37085 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
37086 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
37087 OMP_CLAUSE_DECL (u) = v;
37088 OMP_CLAUSE_CHAIN (u) = list;
37089 list = u;
37092 return list;
37095 /* OpenACC 2.5:
37096 auto
37097 finalize
37098 independent
37099 nohost
37100 seq */
37102 static tree
37103 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
37104 tree list)
37106 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
37108 tree c = build_omp_clause (loc, code);
37109 OMP_CLAUSE_CHAIN (c) = list;
37111 return c;
37114 /* OpenACC:
37115 num_gangs ( expression )
37116 num_workers ( expression )
37117 vector_length ( expression ) */
37119 static tree
37120 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
37121 const char *str, tree list)
37123 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37125 matching_parens parens;
37126 if (!parens.require_open (parser))
37127 return list;
37129 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
37131 if (t == error_mark_node
37132 || !parens.require_close (parser))
37134 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37135 /*or_comma=*/false,
37136 /*consume_paren=*/true);
37137 return list;
37140 check_no_duplicate_clause (list, code, str, loc);
37142 tree c = build_omp_clause (loc, code);
37143 OMP_CLAUSE_OPERAND (c, 0) = t;
37144 OMP_CLAUSE_CHAIN (c) = list;
37145 return c;
37148 /* OpenACC:
37150 gang [( gang-arg-list )]
37151 worker [( [num:] int-expr )]
37152 vector [( [length:] int-expr )]
37154 where gang-arg is one of:
37156 [num:] int-expr
37157 static: size-expr
37159 and size-expr may be:
37162 int-expr
37165 static tree
37166 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
37167 omp_clause_code kind,
37168 const char *str, tree list)
37170 const char *id = "num";
37171 cp_lexer *lexer = parser->lexer;
37172 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
37174 if (kind == OMP_CLAUSE_VECTOR)
37175 id = "length";
37177 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
37179 matching_parens parens;
37180 parens.consume_open (parser);
37184 cp_token *next = cp_lexer_peek_token (lexer);
37185 int idx = 0;
37187 /* Gang static argument. */
37188 if (kind == OMP_CLAUSE_GANG
37189 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
37191 cp_lexer_consume_token (lexer);
37193 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37194 goto cleanup_error;
37196 idx = 1;
37197 if (ops[idx] != NULL)
37199 cp_parser_error (parser, "too many %<static%> arguments");
37200 goto cleanup_error;
37203 /* Check for the '*' argument. */
37204 if (cp_lexer_next_token_is (lexer, CPP_MULT)
37205 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37206 || cp_lexer_nth_token_is (parser->lexer, 2,
37207 CPP_CLOSE_PAREN)))
37209 cp_lexer_consume_token (lexer);
37210 ops[idx] = integer_minus_one_node;
37212 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
37214 cp_lexer_consume_token (lexer);
37215 continue;
37217 else break;
37220 /* Worker num: argument and vector length: arguments. */
37221 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
37222 && id_equal (next->u.value, id)
37223 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
37225 cp_lexer_consume_token (lexer); /* id */
37226 cp_lexer_consume_token (lexer); /* ':' */
37229 /* Now collect the actual argument. */
37230 if (ops[idx] != NULL_TREE)
37232 cp_parser_error (parser, "unexpected argument");
37233 goto cleanup_error;
37236 tree expr = cp_parser_assignment_expression (parser, NULL, false,
37237 false);
37238 if (expr == error_mark_node)
37239 goto cleanup_error;
37241 mark_exp_read (expr);
37242 ops[idx] = expr;
37244 if (kind == OMP_CLAUSE_GANG
37245 && cp_lexer_next_token_is (lexer, CPP_COMMA))
37247 cp_lexer_consume_token (lexer);
37248 continue;
37250 break;
37252 while (1);
37254 if (!parens.require_close (parser))
37255 goto cleanup_error;
37258 check_no_duplicate_clause (list, kind, str, loc);
37260 c = build_omp_clause (loc, kind);
37262 if (ops[1])
37263 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
37265 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
37266 OMP_CLAUSE_CHAIN (c) = list;
37268 return c;
37270 cleanup_error:
37271 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37272 return list;
37275 /* OpenACC 2.0:
37276 tile ( size-expr-list ) */
37278 static tree
37279 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
37281 tree c, expr = error_mark_node;
37282 tree tile = NULL_TREE;
37284 /* Collapse and tile are mutually exclusive. (The spec doesn't say
37285 so, but the spec authors never considered such a case and have
37286 differing opinions on what it might mean, including 'not
37287 allowed'.) */
37288 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
37289 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
37290 clause_loc);
37292 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37293 return list;
37297 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
37298 return list;
37300 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
37301 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37302 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
37304 cp_lexer_consume_token (parser->lexer);
37305 expr = integer_zero_node;
37307 else
37308 expr = cp_parser_constant_expression (parser);
37310 tile = tree_cons (NULL_TREE, expr, tile);
37312 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
37314 /* Consume the trailing ')'. */
37315 cp_lexer_consume_token (parser->lexer);
37317 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
37318 tile = nreverse (tile);
37319 OMP_CLAUSE_TILE_LIST (c) = tile;
37320 OMP_CLAUSE_CHAIN (c) = list;
37321 return c;
37324 /* OpenACC 2.0
37325 Parse wait clause or directive parameters. */
37327 static tree
37328 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
37330 vec<tree, va_gc> *args;
37331 tree t, args_tree;
37333 args = cp_parser_parenthesized_expression_list (parser, non_attr,
37334 /*cast_p=*/false,
37335 /*allow_expansion_p=*/true,
37336 /*non_constant_p=*/NULL);
37338 if (args == NULL || args->length () == 0)
37340 if (args != NULL)
37342 cp_parser_error (parser, "expected integer expression list");
37343 release_tree_vector (args);
37345 return list;
37348 args_tree = build_tree_list_vec (args);
37350 release_tree_vector (args);
37352 for (t = args_tree; t; t = TREE_CHAIN (t))
37354 tree targ = TREE_VALUE (t);
37356 if (targ != error_mark_node)
37358 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
37359 error ("%<wait%> expression must be integral");
37360 else
37362 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
37364 targ = mark_rvalue_use (targ);
37365 OMP_CLAUSE_DECL (c) = targ;
37366 OMP_CLAUSE_CHAIN (c) = list;
37367 list = c;
37372 return list;
37375 /* OpenACC:
37376 wait [( int-expr-list )] */
37378 static tree
37379 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
37381 location_t location = cp_lexer_peek_token (parser->lexer)->location;
37383 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37384 list = cp_parser_oacc_wait_list (parser, location, list);
37385 else
37387 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
37389 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37390 OMP_CLAUSE_CHAIN (c) = list;
37391 list = c;
37394 return list;
37397 /* OpenMP 3.0:
37398 collapse ( constant-expression ) */
37400 static tree
37401 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
37403 tree c, num;
37404 location_t loc;
37405 HOST_WIDE_INT n;
37407 loc = cp_lexer_peek_token (parser->lexer)->location;
37408 matching_parens parens;
37409 if (!parens.require_open (parser))
37410 return list;
37412 num = cp_parser_constant_expression (parser);
37414 if (!parens.require_close (parser))
37415 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37416 /*or_comma=*/false,
37417 /*consume_paren=*/true);
37419 if (num == error_mark_node)
37420 return list;
37421 num = fold_non_dependent_expr (num);
37422 if (!tree_fits_shwi_p (num)
37423 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
37424 || (n = tree_to_shwi (num)) <= 0
37425 || (int) n != n)
37427 error_at (loc, "collapse argument needs positive constant integer expression");
37428 return list;
37431 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
37432 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
37433 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
37434 OMP_CLAUSE_CHAIN (c) = list;
37435 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
37437 return c;
37440 /* OpenMP 2.5:
37441 default ( none | shared )
37443 OpenMP 5.1:
37444 default ( private | firstprivate )
37446 OpenACC:
37447 default ( none | present ) */
37449 static tree
37450 cp_parser_omp_clause_default (cp_parser *parser, tree list,
37451 location_t location, bool is_oacc)
37453 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
37454 tree c;
37456 matching_parens parens;
37457 if (!parens.require_open (parser))
37458 return list;
37459 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37461 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
37462 cp_lexer_consume_token (parser->lexer);
37464 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37466 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37467 const char *p = IDENTIFIER_POINTER (id);
37469 switch (p[0])
37471 case 'n':
37472 if (strcmp ("none", p) != 0)
37473 goto invalid_kind;
37474 kind = OMP_CLAUSE_DEFAULT_NONE;
37475 break;
37477 case 'p':
37478 if (strcmp ("present", p) != 0 || !is_oacc)
37479 goto invalid_kind;
37480 kind = OMP_CLAUSE_DEFAULT_PRESENT;
37481 break;
37483 case 'f':
37484 if (strcmp ("firstprivate", p) != 0 || is_oacc)
37485 goto invalid_kind;
37486 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
37487 break;
37489 case 's':
37490 if (strcmp ("shared", p) != 0 || is_oacc)
37491 goto invalid_kind;
37492 kind = OMP_CLAUSE_DEFAULT_SHARED;
37493 break;
37495 default:
37496 goto invalid_kind;
37499 cp_lexer_consume_token (parser->lexer);
37501 else
37503 invalid_kind:
37504 if (is_oacc)
37505 cp_parser_error (parser, "expected %<none%> or %<present%>");
37506 else
37507 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
37508 "%<private%> or %<firstprivate%>");
37511 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
37512 || !parens.require_close (parser))
37513 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37514 /*or_comma=*/false,
37515 /*consume_paren=*/true);
37517 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
37518 return list;
37520 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
37521 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
37522 OMP_CLAUSE_CHAIN (c) = list;
37523 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
37525 return c;
37528 /* OpenMP 3.1:
37529 final ( expression ) */
37531 static tree
37532 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
37534 tree t, c;
37536 matching_parens parens;
37537 if (!parens.require_open (parser))
37538 return list;
37540 t = cp_parser_assignment_expression (parser);
37542 if (t == error_mark_node
37543 || !parens.require_close (parser))
37544 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37545 /*or_comma=*/false,
37546 /*consume_paren=*/true);
37548 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
37550 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
37551 OMP_CLAUSE_FINAL_EXPR (c) = t;
37552 OMP_CLAUSE_CHAIN (c) = list;
37554 return c;
37557 /* OpenMP 2.5:
37558 if ( expression )
37560 OpenMP 4.5:
37561 if ( directive-name-modifier : expression )
37563 directive-name-modifier:
37564 parallel | task | taskloop | target data | target | target update
37565 | target enter data | target exit data
37567 OpenMP 5.0:
37568 directive-name-modifier:
37569 ... | simd | cancel */
37571 static tree
37572 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
37573 bool is_omp)
37575 tree t, c;
37576 enum tree_code if_modifier = ERROR_MARK;
37578 matching_parens parens;
37579 if (!parens.require_open (parser))
37580 return list;
37582 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37584 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37585 const char *p = IDENTIFIER_POINTER (id);
37586 int n = 2;
37588 if (strcmp ("cancel", p) == 0)
37589 if_modifier = VOID_CST;
37590 else if (strcmp ("parallel", p) == 0)
37591 if_modifier = OMP_PARALLEL;
37592 else if (strcmp ("simd", p) == 0)
37593 if_modifier = OMP_SIMD;
37594 else if (strcmp ("task", p) == 0)
37595 if_modifier = OMP_TASK;
37596 else if (strcmp ("taskloop", p) == 0)
37597 if_modifier = OMP_TASKLOOP;
37598 else if (strcmp ("target", p) == 0)
37600 if_modifier = OMP_TARGET;
37601 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
37603 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
37604 p = IDENTIFIER_POINTER (id);
37605 if (strcmp ("data", p) == 0)
37606 if_modifier = OMP_TARGET_DATA;
37607 else if (strcmp ("update", p) == 0)
37608 if_modifier = OMP_TARGET_UPDATE;
37609 else if (strcmp ("enter", p) == 0)
37610 if_modifier = OMP_TARGET_ENTER_DATA;
37611 else if (strcmp ("exit", p) == 0)
37612 if_modifier = OMP_TARGET_EXIT_DATA;
37613 if (if_modifier != OMP_TARGET)
37614 n = 3;
37615 else
37617 location_t loc
37618 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
37619 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
37620 "or %<exit%>");
37621 if_modifier = ERROR_MARK;
37623 if (if_modifier == OMP_TARGET_ENTER_DATA
37624 || if_modifier == OMP_TARGET_EXIT_DATA)
37626 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
37628 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
37629 p = IDENTIFIER_POINTER (id);
37630 if (strcmp ("data", p) == 0)
37631 n = 4;
37633 if (n != 4)
37635 location_t loc
37636 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
37637 error_at (loc, "expected %<data%>");
37638 if_modifier = ERROR_MARK;
37643 if (if_modifier != ERROR_MARK)
37645 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
37647 while (n-- > 0)
37648 cp_lexer_consume_token (parser->lexer);
37650 else
37652 if (n > 2)
37654 location_t loc
37655 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
37656 error_at (loc, "expected %<:%>");
37658 if_modifier = ERROR_MARK;
37663 t = cp_parser_assignment_expression (parser);
37665 if (t == error_mark_node
37666 || !parens.require_close (parser))
37667 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37668 /*or_comma=*/false,
37669 /*consume_paren=*/true);
37671 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
37672 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
37674 if (if_modifier != ERROR_MARK
37675 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37677 const char *p = NULL;
37678 switch (if_modifier)
37680 case VOID_CST: p = "cancel"; break;
37681 case OMP_PARALLEL: p = "parallel"; break;
37682 case OMP_SIMD: p = "simd"; break;
37683 case OMP_TASK: p = "task"; break;
37684 case OMP_TASKLOOP: p = "taskloop"; break;
37685 case OMP_TARGET_DATA: p = "target data"; break;
37686 case OMP_TARGET: p = "target"; break;
37687 case OMP_TARGET_UPDATE: p = "target update"; break;
37688 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
37689 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
37690 default: gcc_unreachable ();
37692 error_at (location, "too many %<if%> clauses with %qs modifier",
37694 return list;
37696 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37698 if (!is_omp)
37699 error_at (location, "too many %<if%> clauses");
37700 else
37701 error_at (location, "too many %<if%> clauses without modifier");
37702 return list;
37704 else if (if_modifier == ERROR_MARK
37705 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
37707 error_at (location, "if any %<if%> clause has modifier, then all "
37708 "%<if%> clauses have to use modifier");
37709 return list;
37713 c = build_omp_clause (location, OMP_CLAUSE_IF);
37714 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
37715 OMP_CLAUSE_IF_EXPR (c) = t;
37716 OMP_CLAUSE_CHAIN (c) = list;
37718 return c;
37721 /* OpenMP 3.1:
37722 mergeable */
37724 static tree
37725 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
37726 tree list, location_t location)
37728 tree c;
37730 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
37731 location);
37733 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
37734 OMP_CLAUSE_CHAIN (c) = list;
37735 return c;
37738 /* OpenMP 2.5:
37739 nowait */
37741 static tree
37742 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
37743 tree list, location_t location)
37745 tree c;
37747 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
37749 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
37750 OMP_CLAUSE_CHAIN (c) = list;
37751 return c;
37754 /* OpenMP 2.5:
37755 num_threads ( expression ) */
37757 static tree
37758 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
37759 location_t location)
37761 tree t, c;
37763 matching_parens parens;
37764 if (!parens.require_open (parser))
37765 return list;
37767 t = cp_parser_assignment_expression (parser);
37769 if (t == error_mark_node
37770 || !parens.require_close (parser))
37771 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37772 /*or_comma=*/false,
37773 /*consume_paren=*/true);
37775 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
37776 "num_threads", location);
37778 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
37779 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
37780 OMP_CLAUSE_CHAIN (c) = list;
37782 return c;
37785 /* OpenMP 4.5:
37786 num_tasks ( expression )
37788 OpenMP 5.1:
37789 num_tasks ( strict : expression ) */
37791 static tree
37792 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
37793 location_t location)
37795 tree t, c;
37797 matching_parens parens;
37798 if (!parens.require_open (parser))
37799 return list;
37801 bool strict = false;
37802 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37803 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37805 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37806 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37808 strict = true;
37809 cp_lexer_consume_token (parser->lexer);
37810 cp_lexer_consume_token (parser->lexer);
37814 t = cp_parser_assignment_expression (parser);
37816 if (t == error_mark_node
37817 || !parens.require_close (parser))
37818 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37819 /*or_comma=*/false,
37820 /*consume_paren=*/true);
37822 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
37823 "num_tasks", location);
37825 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
37826 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
37827 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
37828 OMP_CLAUSE_CHAIN (c) = list;
37830 return c;
37833 /* OpenMP 4.5:
37834 grainsize ( expression )
37836 OpenMP 5.1:
37837 grainsize ( strict : expression ) */
37839 static tree
37840 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
37841 location_t location)
37843 tree t, c;
37845 matching_parens parens;
37846 if (!parens.require_open (parser))
37847 return list;
37849 bool strict = false;
37850 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37851 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37853 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37854 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37856 strict = true;
37857 cp_lexer_consume_token (parser->lexer);
37858 cp_lexer_consume_token (parser->lexer);
37862 t = cp_parser_assignment_expression (parser);
37864 if (t == error_mark_node
37865 || !parens.require_close (parser))
37866 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37867 /*or_comma=*/false,
37868 /*consume_paren=*/true);
37870 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
37871 "grainsize", location);
37873 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
37874 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
37875 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
37876 OMP_CLAUSE_CHAIN (c) = list;
37878 return c;
37881 /* OpenMP 4.5:
37882 priority ( expression ) */
37884 static tree
37885 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
37886 location_t location)
37888 tree t, c;
37890 matching_parens parens;
37891 if (!parens.require_open (parser))
37892 return list;
37894 t = cp_parser_assignment_expression (parser);
37896 if (t == error_mark_node
37897 || !parens.require_close (parser))
37898 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37899 /*or_comma=*/false,
37900 /*consume_paren=*/true);
37902 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
37903 "priority", location);
37905 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
37906 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
37907 OMP_CLAUSE_CHAIN (c) = list;
37909 return c;
37912 /* OpenMP 4.5:
37913 hint ( expression ) */
37915 static tree
37916 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
37918 tree t, c;
37920 matching_parens parens;
37921 if (!parens.require_open (parser))
37922 return list;
37924 t = cp_parser_assignment_expression (parser);
37926 if (t != error_mark_node)
37928 t = fold_non_dependent_expr (t);
37929 if (!value_dependent_expression_p (t)
37930 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
37931 || !tree_fits_shwi_p (t)
37932 || tree_int_cst_sgn (t) == -1))
37933 error_at (location, "expected constant integer expression with "
37934 "valid sync-hint value");
37936 if (t == error_mark_node
37937 || !parens.require_close (parser))
37938 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37939 /*or_comma=*/false,
37940 /*consume_paren=*/true);
37941 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
37943 c = build_omp_clause (location, OMP_CLAUSE_HINT);
37944 OMP_CLAUSE_HINT_EXPR (c) = t;
37945 OMP_CLAUSE_CHAIN (c) = list;
37947 return c;
37950 /* OpenMP 5.1:
37951 filter ( integer-expression ) */
37953 static tree
37954 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
37956 tree t, c;
37958 matching_parens parens;
37959 if (!parens.require_open (parser))
37960 return list;
37962 t = cp_parser_assignment_expression (parser);
37964 if (t == error_mark_node
37965 || !parens.require_close (parser))
37966 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37967 /*or_comma=*/false,
37968 /*consume_paren=*/true);
37969 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
37971 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
37972 OMP_CLAUSE_FILTER_EXPR (c) = t;
37973 OMP_CLAUSE_CHAIN (c) = list;
37975 return c;
37978 /* OpenMP 4.5:
37979 defaultmap ( tofrom : scalar )
37981 OpenMP 5.0:
37982 defaultmap ( implicit-behavior [ : variable-category ] ) */
37984 static tree
37985 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
37986 location_t location)
37988 tree c, id;
37989 const char *p;
37990 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
37991 enum omp_clause_defaultmap_kind category
37992 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
37994 matching_parens parens;
37995 if (!parens.require_open (parser))
37996 return list;
37998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37999 p = "default";
38000 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38002 invalid_behavior:
38003 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
38004 "%<tofrom%>, %<firstprivate%>, %<none%> "
38005 "or %<default%>");
38006 goto out_err;
38008 else
38010 id = cp_lexer_peek_token (parser->lexer)->u.value;
38011 p = IDENTIFIER_POINTER (id);
38014 switch (p[0])
38016 case 'a':
38017 if (strcmp ("alloc", p) == 0)
38018 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
38019 else
38020 goto invalid_behavior;
38021 break;
38023 case 'd':
38024 if (strcmp ("default", p) == 0)
38025 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38026 else
38027 goto invalid_behavior;
38028 break;
38030 case 'f':
38031 if (strcmp ("firstprivate", p) == 0)
38032 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
38033 else if (strcmp ("from", p) == 0)
38034 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
38035 else
38036 goto invalid_behavior;
38037 break;
38039 case 'n':
38040 if (strcmp ("none", p) == 0)
38041 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
38042 else
38043 goto invalid_behavior;
38044 break;
38046 case 't':
38047 if (strcmp ("tofrom", p) == 0)
38048 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
38049 else if (strcmp ("to", p) == 0)
38050 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
38051 else
38052 goto invalid_behavior;
38053 break;
38055 default:
38056 goto invalid_behavior;
38058 cp_lexer_consume_token (parser->lexer);
38060 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38062 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38063 goto out_err;
38065 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38067 invalid_category:
38068 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
38069 "%<pointer%>");
38070 goto out_err;
38072 id = cp_lexer_peek_token (parser->lexer)->u.value;
38073 p = IDENTIFIER_POINTER (id);
38075 switch (p[0])
38077 case 'a':
38078 if (strcmp ("aggregate", p) == 0)
38079 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
38080 else
38081 goto invalid_category;
38082 break;
38084 case 'p':
38085 if (strcmp ("pointer", p) == 0)
38086 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
38087 else
38088 goto invalid_category;
38089 break;
38091 case 's':
38092 if (strcmp ("scalar", p) == 0)
38093 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
38094 else
38095 goto invalid_category;
38096 break;
38098 default:
38099 goto invalid_category;
38102 cp_lexer_consume_token (parser->lexer);
38104 if (!parens.require_close (parser))
38105 goto out_err;
38107 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
38108 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
38109 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
38110 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
38111 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
38112 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
38114 enum omp_clause_defaultmap_kind cat = category;
38115 location_t loc = OMP_CLAUSE_LOCATION (c);
38116 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
38117 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
38118 p = NULL;
38119 switch (cat)
38121 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
38122 p = NULL;
38123 break;
38124 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
38125 p = "aggregate";
38126 break;
38127 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
38128 p = "pointer";
38129 break;
38130 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
38131 p = "scalar";
38132 break;
38133 default:
38134 gcc_unreachable ();
38136 if (p)
38137 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
38139 else
38140 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
38141 "category");
38142 break;
38145 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
38146 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
38147 OMP_CLAUSE_CHAIN (c) = list;
38148 return c;
38150 out_err:
38151 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38152 /*or_comma=*/false,
38153 /*consume_paren=*/true);
38154 return list;
38157 /* OpenMP 5.0:
38158 order ( concurrent )
38160 OpenMP 5.1:
38161 order ( order-modifier : concurrent )
38163 order-modifier:
38164 reproducible
38165 unconstrained */
38167 static tree
38168 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
38170 tree c, id;
38171 const char *p;
38172 bool unconstrained = false;
38173 bool reproducible = false;
38175 matching_parens parens;
38176 if (!parens.require_open (parser))
38177 return list;
38179 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38180 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38182 id = cp_lexer_peek_token (parser->lexer)->u.value;
38183 p = IDENTIFIER_POINTER (id);
38184 if (strcmp (p, "unconstrained") == 0)
38185 unconstrained = true;
38186 else if (strcmp (p, "reproducible") == 0)
38187 reproducible = true;
38188 else
38190 cp_parser_error (parser, "expected %<reproducible%> or "
38191 "%<unconstrained%>");
38192 goto out_err;
38194 cp_lexer_consume_token (parser->lexer);
38195 cp_lexer_consume_token (parser->lexer);
38197 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38199 cp_parser_error (parser, "expected %<concurrent%>");
38200 goto out_err;
38202 else
38204 id = cp_lexer_peek_token (parser->lexer)->u.value;
38205 p = IDENTIFIER_POINTER (id);
38207 if (strcmp (p, "concurrent") != 0)
38209 cp_parser_error (parser, "expected %<concurrent%>");
38210 goto out_err;
38212 cp_lexer_consume_token (parser->lexer);
38213 if (!parens.require_close (parser))
38214 goto out_err;
38216 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
38217 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
38218 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
38219 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
38220 OMP_CLAUSE_CHAIN (c) = list;
38221 return c;
38223 out_err:
38224 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38225 /*or_comma=*/false,
38226 /*consume_paren=*/true);
38227 return list;
38230 /* OpenMP 5.0:
38231 bind ( teams | parallel | thread ) */
38233 static tree
38234 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
38235 location_t location)
38237 tree c;
38238 const char *p;
38239 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
38241 matching_parens parens;
38242 if (!parens.require_open (parser))
38243 return list;
38245 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38247 invalid:
38248 cp_parser_error (parser,
38249 "expected %<teams%>, %<parallel%> or %<thread%>");
38250 goto out_err;
38252 else
38254 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38255 p = IDENTIFIER_POINTER (id);
38257 if (strcmp (p, "teams") == 0)
38258 kind = OMP_CLAUSE_BIND_TEAMS;
38259 else if (strcmp (p, "parallel") == 0)
38260 kind = OMP_CLAUSE_BIND_PARALLEL;
38261 else if (strcmp (p, "thread") != 0)
38262 goto invalid;
38263 cp_lexer_consume_token (parser->lexer);
38264 if (!parens.require_close (parser))
38265 goto out_err;
38267 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
38268 c = build_omp_clause (location, OMP_CLAUSE_BIND);
38269 OMP_CLAUSE_BIND_KIND (c) = kind;
38270 OMP_CLAUSE_CHAIN (c) = list;
38271 return c;
38273 out_err:
38274 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38275 /*or_comma=*/false,
38276 /*consume_paren=*/true);
38277 return list;
38280 /* OpenMP 2.5:
38281 ordered
38283 OpenMP 4.5:
38284 ordered ( constant-expression ) */
38286 static tree
38287 cp_parser_omp_clause_ordered (cp_parser *parser,
38288 tree list, location_t location)
38290 tree c, num = NULL_TREE;
38291 HOST_WIDE_INT n;
38293 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
38294 "ordered", location);
38296 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38298 matching_parens parens;
38299 parens.consume_open (parser);
38301 num = cp_parser_constant_expression (parser);
38303 if (!parens.require_close (parser))
38304 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38305 /*or_comma=*/false,
38306 /*consume_paren=*/true);
38308 if (num == error_mark_node)
38309 return list;
38310 num = fold_non_dependent_expr (num);
38311 if (!tree_fits_shwi_p (num)
38312 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38313 || (n = tree_to_shwi (num)) <= 0
38314 || (int) n != n)
38316 error_at (location,
38317 "ordered argument needs positive constant integer "
38318 "expression");
38319 return list;
38323 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
38324 OMP_CLAUSE_ORDERED_EXPR (c) = num;
38325 OMP_CLAUSE_CHAIN (c) = list;
38326 return c;
38329 /* OpenMP 2.5:
38330 reduction ( reduction-operator : variable-list )
38332 reduction-operator:
38333 One of: + * - & ^ | && ||
38335 OpenMP 3.1:
38337 reduction-operator:
38338 One of: + * - & ^ | && || min max
38340 OpenMP 4.0:
38342 reduction-operator:
38343 One of: + * - & ^ | && ||
38344 id-expression
38346 OpenMP 5.0:
38347 reduction ( reduction-modifier, reduction-operator : variable-list )
38348 in_reduction ( reduction-operator : variable-list )
38349 task_reduction ( reduction-operator : variable-list ) */
38351 static tree
38352 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
38353 bool is_omp, tree list)
38355 enum tree_code code = ERROR_MARK;
38356 tree nlist, c, id = NULL_TREE;
38357 bool task = false;
38358 bool inscan = false;
38360 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38361 return list;
38363 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
38365 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
38366 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38368 cp_lexer_consume_token (parser->lexer);
38369 cp_lexer_consume_token (parser->lexer);
38371 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38372 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38374 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38375 const char *p = IDENTIFIER_POINTER (id);
38376 if (strcmp (p, "task") == 0)
38377 task = true;
38378 else if (strcmp (p, "inscan") == 0)
38379 inscan = true;
38380 if (task || inscan)
38382 cp_lexer_consume_token (parser->lexer);
38383 cp_lexer_consume_token (parser->lexer);
38388 switch (cp_lexer_peek_token (parser->lexer)->type)
38390 case CPP_PLUS: code = PLUS_EXPR; break;
38391 case CPP_MULT: code = MULT_EXPR; break;
38392 case CPP_MINUS: code = MINUS_EXPR; break;
38393 case CPP_AND: code = BIT_AND_EXPR; break;
38394 case CPP_XOR: code = BIT_XOR_EXPR; break;
38395 case CPP_OR: code = BIT_IOR_EXPR; break;
38396 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
38397 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
38398 default: break;
38401 if (code != ERROR_MARK)
38402 cp_lexer_consume_token (parser->lexer);
38403 else
38405 bool saved_colon_corrects_to_scope_p;
38406 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38407 parser->colon_corrects_to_scope_p = false;
38408 id = cp_parser_id_expression (parser, /*template_p=*/false,
38409 /*check_dependency_p=*/true,
38410 /*template_p=*/NULL,
38411 /*declarator_p=*/false,
38412 /*optional_p=*/false);
38413 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38414 if (identifier_p (id))
38416 const char *p = IDENTIFIER_POINTER (id);
38418 if (strcmp (p, "min") == 0)
38419 code = MIN_EXPR;
38420 else if (strcmp (p, "max") == 0)
38421 code = MAX_EXPR;
38422 else if (id == ovl_op_identifier (false, PLUS_EXPR))
38423 code = PLUS_EXPR;
38424 else if (id == ovl_op_identifier (false, MULT_EXPR))
38425 code = MULT_EXPR;
38426 else if (id == ovl_op_identifier (false, MINUS_EXPR))
38427 code = MINUS_EXPR;
38428 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
38429 code = BIT_AND_EXPR;
38430 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
38431 code = BIT_IOR_EXPR;
38432 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
38433 code = BIT_XOR_EXPR;
38434 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
38435 code = TRUTH_ANDIF_EXPR;
38436 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
38437 code = TRUTH_ORIF_EXPR;
38438 id = omp_reduction_id (code, id, NULL_TREE);
38439 tree scope = parser->scope;
38440 if (scope)
38441 id = build_qualified_name (NULL_TREE, scope, id, false);
38442 parser->scope = NULL_TREE;
38443 parser->qualifying_scope = NULL_TREE;
38444 parser->object_scope = NULL_TREE;
38446 else
38448 error ("invalid reduction-identifier");
38449 resync_fail:
38450 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38451 /*or_comma=*/false,
38452 /*consume_paren=*/true);
38453 return list;
38457 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38458 goto resync_fail;
38460 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
38461 NULL);
38462 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38464 OMP_CLAUSE_REDUCTION_CODE (c) = code;
38465 if (task)
38466 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
38467 else if (inscan)
38468 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
38469 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
38472 return nlist;
38475 /* OpenMP 2.5:
38476 schedule ( schedule-kind )
38477 schedule ( schedule-kind , expression )
38479 schedule-kind:
38480 static | dynamic | guided | runtime | auto
38482 OpenMP 4.5:
38483 schedule ( schedule-modifier : schedule-kind )
38484 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
38486 schedule-modifier:
38487 simd
38488 monotonic
38489 nonmonotonic */
38491 static tree
38492 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
38494 tree c, t;
38495 int modifiers = 0, nmodifiers = 0;
38497 matching_parens parens;
38498 if (!parens.require_open (parser))
38499 return list;
38501 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
38503 location_t comma = UNKNOWN_LOCATION;
38504 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38506 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38507 const char *p = IDENTIFIER_POINTER (id);
38508 if (strcmp ("simd", p) == 0)
38509 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
38510 else if (strcmp ("monotonic", p) == 0)
38511 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
38512 else if (strcmp ("nonmonotonic", p) == 0)
38513 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
38514 else
38515 break;
38516 comma = UNKNOWN_LOCATION;
38517 cp_lexer_consume_token (parser->lexer);
38518 if (nmodifiers++ == 0
38519 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38521 comma = cp_lexer_peek_token (parser->lexer)->location;
38522 cp_lexer_consume_token (parser->lexer);
38524 else
38526 cp_parser_require (parser, CPP_COLON, RT_COLON);
38527 break;
38530 if (comma != UNKNOWN_LOCATION)
38531 error_at (comma, "expected %<:%>");
38533 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38535 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38536 const char *p = IDENTIFIER_POINTER (id);
38538 switch (p[0])
38540 case 'd':
38541 if (strcmp ("dynamic", p) != 0)
38542 goto invalid_kind;
38543 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
38544 break;
38546 case 'g':
38547 if (strcmp ("guided", p) != 0)
38548 goto invalid_kind;
38549 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
38550 break;
38552 case 'r':
38553 if (strcmp ("runtime", p) != 0)
38554 goto invalid_kind;
38555 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
38556 break;
38558 default:
38559 goto invalid_kind;
38562 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
38563 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
38564 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
38565 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
38566 else
38567 goto invalid_kind;
38568 cp_lexer_consume_token (parser->lexer);
38570 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
38571 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38572 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
38573 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38575 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
38576 "specified");
38577 modifiers = 0;
38580 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38582 cp_token *token;
38583 cp_lexer_consume_token (parser->lexer);
38585 token = cp_lexer_peek_token (parser->lexer);
38586 t = cp_parser_assignment_expression (parser);
38588 if (t == error_mark_node)
38589 goto resync_fail;
38590 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
38591 error_at (token->location, "schedule %<runtime%> does not take "
38592 "a %<chunk_size%> parameter");
38593 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
38594 error_at (token->location, "schedule %<auto%> does not take "
38595 "a %<chunk_size%> parameter");
38596 else
38597 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
38599 if (!parens.require_close (parser))
38600 goto resync_fail;
38602 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
38603 goto resync_fail;
38605 OMP_CLAUSE_SCHEDULE_KIND (c)
38606 = (enum omp_clause_schedule_kind)
38607 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
38609 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
38610 OMP_CLAUSE_CHAIN (c) = list;
38611 return c;
38613 invalid_kind:
38614 cp_parser_error (parser, "invalid schedule kind");
38615 resync_fail:
38616 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38617 /*or_comma=*/false,
38618 /*consume_paren=*/true);
38619 return list;
38622 /* OpenMP 3.0:
38623 untied */
38625 static tree
38626 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
38627 tree list, location_t location)
38629 tree c;
38631 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
38633 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
38634 OMP_CLAUSE_CHAIN (c) = list;
38635 return c;
38638 /* OpenMP 4.0:
38639 inbranch
38640 notinbranch */
38642 static tree
38643 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
38644 tree list, location_t location)
38646 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38647 tree c = build_omp_clause (location, code);
38648 OMP_CLAUSE_CHAIN (c) = list;
38649 return c;
38652 /* OpenMP 4.0:
38653 parallel
38655 sections
38656 taskgroup */
38658 static tree
38659 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
38660 enum omp_clause_code code,
38661 tree list, location_t location)
38663 tree c = build_omp_clause (location, code);
38664 OMP_CLAUSE_CHAIN (c) = list;
38665 return c;
38668 /* OpenMP 4.5:
38669 nogroup */
38671 static tree
38672 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
38673 tree list, location_t location)
38675 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
38676 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
38677 OMP_CLAUSE_CHAIN (c) = list;
38678 return c;
38681 /* OpenMP 4.5:
38682 simd
38683 threads */
38685 static tree
38686 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
38687 enum omp_clause_code code,
38688 tree list, location_t location)
38690 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38691 tree c = build_omp_clause (location, code);
38692 OMP_CLAUSE_CHAIN (c) = list;
38693 return c;
38696 /* OpenMP 4.0:
38697 num_teams ( expression )
38699 OpenMP 5.1:
38700 num_teams ( expression : expression ) */
38702 static tree
38703 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
38704 location_t location)
38706 tree upper, lower = NULL_TREE, c;
38708 matching_parens parens;
38709 if (!parens.require_open (parser))
38710 return list;
38712 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38713 parser->colon_corrects_to_scope_p = false;
38714 upper = cp_parser_assignment_expression (parser);
38715 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38717 if (upper != error_mark_node
38718 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38720 lower = upper;
38721 cp_lexer_consume_token (parser->lexer);
38722 upper = cp_parser_assignment_expression (parser);
38725 if (upper == error_mark_node
38726 || !parens.require_close (parser))
38727 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38728 /*or_comma=*/false,
38729 /*consume_paren=*/true);
38731 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
38732 "num_teams", location);
38734 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
38735 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
38736 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
38737 OMP_CLAUSE_CHAIN (c) = list;
38739 return c;
38742 /* OpenMP 4.0:
38743 thread_limit ( expression ) */
38745 static tree
38746 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
38747 location_t location)
38749 tree t, c;
38751 matching_parens parens;
38752 if (!parens.require_open (parser))
38753 return list;
38755 t = cp_parser_assignment_expression (parser);
38757 if (t == error_mark_node
38758 || !parens.require_close (parser))
38759 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38760 /*or_comma=*/false,
38761 /*consume_paren=*/true);
38763 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
38764 "thread_limit", location);
38766 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
38767 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
38768 OMP_CLAUSE_CHAIN (c) = list;
38770 return c;
38773 /* OpenMP 4.0:
38774 aligned ( variable-list )
38775 aligned ( variable-list : constant-expression ) */
38777 static tree
38778 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
38780 tree nlist, c, alignment = NULL_TREE;
38781 bool colon;
38783 matching_parens parens;
38784 if (!parens.require_open (parser))
38785 return list;
38787 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
38788 &colon);
38790 if (colon)
38792 alignment = cp_parser_constant_expression (parser);
38794 if (!parens.require_close (parser))
38795 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38796 /*or_comma=*/false,
38797 /*consume_paren=*/true);
38799 if (alignment == error_mark_node)
38800 alignment = NULL_TREE;
38803 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38804 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
38806 return nlist;
38809 /* OpenMP 5.0:
38810 allocate ( variable-list )
38811 allocate ( expression : variable-list )
38813 OpenMP 5.1:
38814 allocate ( allocator-modifier : variable-list )
38815 allocate ( allocator-modifier , allocator-modifier : variable-list )
38817 allocator-modifier:
38818 allocator ( expression )
38819 align ( expression ) */
38821 static tree
38822 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
38824 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
38825 bool colon, has_modifiers = false;
38827 matching_parens parens;
38828 if (!parens.require_open (parser))
38829 return list;
38831 cp_parser_parse_tentatively (parser);
38832 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38833 parser->colon_corrects_to_scope_p = false;
38834 for (int mod = 0; mod < 2; mod++)
38835 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38836 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38838 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38839 const char *p = IDENTIFIER_POINTER (id);
38840 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
38841 break;
38842 cp_lexer_consume_token (parser->lexer);
38843 matching_parens parens2;
38844 if (!parens2.require_open (parser))
38845 break;
38846 if (strcmp (p, "allocator") == 0)
38848 if (allocator != NULL_TREE)
38849 break;
38850 allocator = cp_parser_assignment_expression (parser);
38852 else
38854 if (align != NULL_TREE)
38855 break;
38856 align = cp_parser_assignment_expression (parser);
38858 if (!parens2.require_close (parser))
38859 break;
38860 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38862 has_modifiers = true;
38863 break;
38865 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38866 break;
38867 cp_lexer_consume_token (parser->lexer);
38869 else
38870 break;
38871 if (!has_modifiers)
38873 cp_parser_abort_tentative_parse (parser);
38874 align = NULL_TREE;
38875 allocator = NULL_TREE;
38876 cp_parser_parse_tentatively (parser);
38877 allocator = cp_parser_assignment_expression (parser);
38879 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38880 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38882 cp_parser_parse_definitely (parser);
38883 cp_lexer_consume_token (parser->lexer);
38884 if (allocator == error_mark_node)
38885 allocator = NULL_TREE;
38886 if (align == error_mark_node)
38887 align = NULL_TREE;
38889 else
38891 cp_parser_abort_tentative_parse (parser);
38892 allocator = NULL_TREE;
38893 align = NULL_TREE;
38896 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
38897 &colon);
38899 if (allocator || align)
38900 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38902 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
38903 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
38906 return nlist;
38909 /* OpenMP 2.5:
38910 lastprivate ( variable-list )
38912 OpenMP 5.0:
38913 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
38915 static tree
38916 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
38918 bool conditional = false;
38920 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38921 return list;
38923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38924 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38926 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38927 const char *p = IDENTIFIER_POINTER (id);
38929 if (strcmp ("conditional", p) == 0)
38931 conditional = true;
38932 cp_lexer_consume_token (parser->lexer);
38933 cp_lexer_consume_token (parser->lexer);
38937 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
38938 list, NULL);
38940 if (conditional)
38941 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38942 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
38943 return nlist;
38946 /* OpenMP 4.0:
38947 linear ( variable-list )
38948 linear ( variable-list : expression )
38950 OpenMP 4.5:
38951 linear ( modifier ( variable-list ) )
38952 linear ( modifier ( variable-list ) : expression ) */
38954 static tree
38955 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
38956 bool declare_simd)
38958 tree nlist, c, step = integer_one_node;
38959 bool colon;
38960 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
38962 matching_parens parens;
38963 if (!parens.require_open (parser))
38964 return list;
38966 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38968 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38969 const char *p = IDENTIFIER_POINTER (id);
38971 if (strcmp ("ref", p) == 0)
38972 kind = OMP_CLAUSE_LINEAR_REF;
38973 else if (strcmp ("val", p) == 0)
38974 kind = OMP_CLAUSE_LINEAR_VAL;
38975 else if (strcmp ("uval", p) == 0)
38976 kind = OMP_CLAUSE_LINEAR_UVAL;
38977 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38978 cp_lexer_consume_token (parser->lexer);
38979 else
38980 kind = OMP_CLAUSE_LINEAR_DEFAULT;
38983 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
38984 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
38985 &colon);
38986 else
38988 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
38989 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
38990 if (colon)
38991 cp_parser_require (parser, CPP_COLON, RT_COLON);
38992 else if (!parens.require_close (parser))
38993 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38994 /*or_comma=*/false,
38995 /*consume_paren=*/true);
38998 if (colon)
39000 step = NULL_TREE;
39001 if (declare_simd
39002 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39003 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
39005 cp_token *token = cp_lexer_peek_token (parser->lexer);
39006 cp_parser_parse_tentatively (parser);
39007 step = cp_parser_id_expression (parser, /*template_p=*/false,
39008 /*check_dependency_p=*/true,
39009 /*template_p=*/NULL,
39010 /*declarator_p=*/false,
39011 /*optional_p=*/false);
39012 if (step != error_mark_node)
39013 step = cp_parser_lookup_name_simple (parser, step, token->location);
39014 if (step == error_mark_node)
39016 step = NULL_TREE;
39017 cp_parser_abort_tentative_parse (parser);
39019 else if (!cp_parser_parse_definitely (parser))
39020 step = NULL_TREE;
39022 if (!step)
39023 step = cp_parser_assignment_expression (parser);
39025 if (!parens.require_close (parser))
39026 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39027 /*or_comma=*/false,
39028 /*consume_paren=*/true);
39030 if (step == error_mark_node)
39031 return list;
39034 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39036 OMP_CLAUSE_LINEAR_STEP (c) = step;
39037 OMP_CLAUSE_LINEAR_KIND (c) = kind;
39040 return nlist;
39043 /* OpenMP 4.0:
39044 safelen ( constant-expression ) */
39046 static tree
39047 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
39048 location_t location)
39050 tree t, c;
39052 matching_parens parens;
39053 if (!parens.require_open (parser))
39054 return list;
39056 t = cp_parser_constant_expression (parser);
39058 if (t == error_mark_node
39059 || !parens.require_close (parser))
39060 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39061 /*or_comma=*/false,
39062 /*consume_paren=*/true);
39064 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
39066 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
39067 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
39068 OMP_CLAUSE_CHAIN (c) = list;
39070 return c;
39073 /* OpenMP 4.0:
39074 simdlen ( constant-expression ) */
39076 static tree
39077 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
39078 location_t location)
39080 tree t, c;
39082 matching_parens parens;
39083 if (!parens.require_open (parser))
39084 return list;
39086 t = cp_parser_constant_expression (parser);
39088 if (t == error_mark_node
39089 || !parens.require_close (parser))
39090 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39091 /*or_comma=*/false,
39092 /*consume_paren=*/true);
39094 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
39096 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
39097 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
39098 OMP_CLAUSE_CHAIN (c) = list;
39100 return c;
39103 /* OpenMP 4.5:
39104 vec:
39105 identifier [+/- integer]
39106 vec , identifier [+/- integer]
39109 static tree
39110 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
39111 tree list)
39113 tree vec = NULL;
39115 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39117 cp_parser_error (parser, "expected identifier");
39118 return list;
39121 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39123 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
39124 tree t, identifier = cp_parser_identifier (parser);
39125 tree addend = NULL;
39127 if (identifier == error_mark_node)
39128 t = error_mark_node;
39129 else
39131 t = cp_parser_lookup_name_simple
39132 (parser, identifier,
39133 cp_lexer_peek_token (parser->lexer)->location);
39134 if (t == error_mark_node)
39135 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
39136 id_loc);
39139 bool neg = false;
39140 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
39141 neg = true;
39142 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
39144 addend = integer_zero_node;
39145 goto add_to_vector;
39147 cp_lexer_consume_token (parser->lexer);
39149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
39151 cp_parser_error (parser, "expected integer");
39152 return list;
39155 addend = cp_lexer_peek_token (parser->lexer)->u.value;
39156 if (TREE_CODE (addend) != INTEGER_CST)
39158 cp_parser_error (parser, "expected integer");
39159 return list;
39161 cp_lexer_consume_token (parser->lexer);
39163 add_to_vector:
39164 if (t != error_mark_node)
39166 vec = tree_cons (addend, t, vec);
39167 if (neg)
39168 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
39171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
39172 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39173 break;
39175 cp_lexer_consume_token (parser->lexer);
39178 if (vec)
39180 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
39181 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
39182 OMP_CLAUSE_DECL (u) = nreverse (vec);
39183 OMP_CLAUSE_CHAIN (u) = list;
39184 return u;
39186 return list;
39189 /* OpenMP 5.0:
39190 detach ( event-handle ) */
39192 static tree
39193 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
39195 matching_parens parens;
39197 if (!parens.require_open (parser))
39198 return list;
39200 cp_token *token;
39201 tree name, decl;
39203 token = cp_lexer_peek_token (parser->lexer);
39204 name = cp_parser_id_expression (parser, /*template_p=*/false,
39205 /*check_dependency_p=*/true,
39206 /*template_p=*/NULL,
39207 /*declarator_p=*/false,
39208 /*optional_p=*/false);
39209 if (name == error_mark_node)
39210 decl = error_mark_node;
39211 else
39213 if (identifier_p (name))
39214 decl = cp_parser_lookup_name_simple (parser, name, token->location);
39215 else
39216 decl = name;
39217 if (decl == error_mark_node)
39218 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
39219 token->location);
39222 if (decl == error_mark_node
39223 || !parens.require_close (parser))
39224 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39225 /*or_comma=*/false,
39226 /*consume_paren=*/true);
39228 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
39229 OMP_CLAUSE_DECL (u) = decl;
39230 OMP_CLAUSE_CHAIN (u) = list;
39232 return u;
39235 /* OpenMP 5.0:
39236 iterators ( iterators-definition )
39238 iterators-definition:
39239 iterator-specifier
39240 iterator-specifier , iterators-definition
39242 iterator-specifier:
39243 identifier = range-specification
39244 iterator-type identifier = range-specification
39246 range-specification:
39247 begin : end
39248 begin : end : step */
39250 static tree
39251 cp_parser_omp_iterators (cp_parser *parser)
39253 tree ret = NULL_TREE, *last = &ret;
39254 cp_lexer_consume_token (parser->lexer);
39256 matching_parens parens;
39257 if (!parens.require_open (parser))
39258 return error_mark_node;
39260 bool saved_colon_corrects_to_scope_p
39261 = parser->colon_corrects_to_scope_p;
39262 bool saved_colon_doesnt_start_class_def_p
39263 = parser->colon_doesnt_start_class_def_p;
39267 tree iter_type;
39268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39269 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
39270 iter_type = integer_type_node;
39271 else
39273 const char *saved_message
39274 = parser->type_definition_forbidden_message;
39275 parser->type_definition_forbidden_message
39276 = G_("types may not be defined in iterator type");
39278 iter_type = cp_parser_type_id (parser);
39280 parser->type_definition_forbidden_message = saved_message;
39283 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39286 cp_parser_error (parser, "expected identifier");
39287 break;
39290 tree id = cp_parser_identifier (parser);
39291 if (id == error_mark_node)
39292 break;
39294 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
39295 break;
39297 parser->colon_corrects_to_scope_p = false;
39298 parser->colon_doesnt_start_class_def_p = true;
39299 tree begin = cp_parser_assignment_expression (parser);
39301 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39302 break;
39304 tree end = cp_parser_assignment_expression (parser);
39306 tree step = integer_one_node;
39307 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39309 cp_lexer_consume_token (parser->lexer);
39310 step = cp_parser_assignment_expression (parser);
39313 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
39314 DECL_ARTIFICIAL (iter_var) = 1;
39315 DECL_CONTEXT (iter_var) = current_function_decl;
39316 pushdecl (iter_var);
39318 *last = make_tree_vec (6);
39319 TREE_VEC_ELT (*last, 0) = iter_var;
39320 TREE_VEC_ELT (*last, 1) = begin;
39321 TREE_VEC_ELT (*last, 2) = end;
39322 TREE_VEC_ELT (*last, 3) = step;
39323 last = &TREE_CHAIN (*last);
39325 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39327 cp_lexer_consume_token (parser->lexer);
39328 continue;
39330 break;
39332 while (1);
39334 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39335 parser->colon_doesnt_start_class_def_p
39336 = saved_colon_doesnt_start_class_def_p;
39338 if (!parens.require_close (parser))
39339 cp_parser_skip_to_closing_parenthesis (parser,
39340 /*recovering=*/true,
39341 /*or_comma=*/false,
39342 /*consume_paren=*/true);
39344 return ret ? ret : error_mark_node;
39347 /* OpenMP 5.0:
39348 affinity ( [aff-modifier :] variable-list )
39349 aff-modifier:
39350 iterator ( iterators-definition ) */
39352 static tree
39353 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
39355 tree nlist, c, iterators = NULL_TREE;
39357 matching_parens parens;
39358 if (!parens.require_open (parser))
39359 return list;
39361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39363 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39364 const char *p = IDENTIFIER_POINTER (id);
39365 bool parse_iter = ((strcmp ("iterator", p) == 0)
39366 && (cp_lexer_nth_token_is (parser->lexer, 2,
39367 CPP_OPEN_PAREN)));
39368 if (parse_iter)
39370 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
39371 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
39373 if (parse_iter)
39375 begin_scope (sk_omp, NULL);
39376 iterators = cp_parser_omp_iterators (parser);
39377 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39379 if (iterators)
39380 poplevel (0, 1, 0);
39381 cp_parser_skip_to_closing_parenthesis (parser,
39382 /*recovering=*/true,
39383 /*or_comma=*/false,
39384 /*consume_paren=*/true);
39385 return list;
39389 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
39390 list, NULL);
39391 if (iterators)
39393 tree block = poplevel (1, 1, 0);
39394 if (iterators != error_mark_node)
39396 TREE_VEC_ELT (iterators, 5) = block;
39397 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39398 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
39399 OMP_CLAUSE_DECL (c));
39402 return nlist;
39405 /* OpenMP 4.0:
39406 depend ( depend-kind : variable-list )
39408 depend-kind:
39409 in | out | inout
39411 OpenMP 4.5:
39412 depend ( source )
39414 depend ( sink : vec )
39416 OpenMP 5.0:
39417 depend ( depend-modifier , depend-kind: variable-list )
39419 depend-kind:
39420 in | out | inout | mutexinoutset | depobj
39422 depend-modifier:
39423 iterator ( iterators-definition ) */
39425 static tree
39426 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
39428 tree nlist, c, iterators = NULL_TREE;
39429 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
39431 matching_parens parens;
39432 if (!parens.require_open (parser))
39433 return list;
39437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39438 goto invalid_kind;
39440 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39441 const char *p = IDENTIFIER_POINTER (id);
39443 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
39445 begin_scope (sk_omp, NULL);
39446 iterators = cp_parser_omp_iterators (parser);
39447 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
39448 continue;
39450 if (strcmp ("in", p) == 0)
39451 kind = OMP_CLAUSE_DEPEND_IN;
39452 else if (strcmp ("inout", p) == 0)
39453 kind = OMP_CLAUSE_DEPEND_INOUT;
39454 else if (strcmp ("inoutset", p) == 0)
39455 kind = OMP_CLAUSE_DEPEND_INOUTSET;
39456 else if (strcmp ("mutexinoutset", p) == 0)
39457 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
39458 else if (strcmp ("out", p) == 0)
39459 kind = OMP_CLAUSE_DEPEND_OUT;
39460 else if (strcmp ("depobj", p) == 0)
39461 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
39462 else if (strcmp ("sink", p) == 0)
39463 kind = OMP_CLAUSE_DEPEND_SINK;
39464 else if (strcmp ("source", p) == 0)
39465 kind = OMP_CLAUSE_DEPEND_SOURCE;
39466 else
39467 goto invalid_kind;
39468 break;
39470 while (1);
39472 cp_lexer_consume_token (parser->lexer);
39474 if (iterators
39475 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
39477 poplevel (0, 1, 0);
39478 error_at (loc, "%<iterator%> modifier incompatible with %qs",
39479 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
39480 iterators = NULL_TREE;
39483 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
39485 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
39486 OMP_CLAUSE_DEPEND_KIND (c) = kind;
39487 OMP_CLAUSE_DECL (c) = NULL_TREE;
39488 OMP_CLAUSE_CHAIN (c) = list;
39489 if (!parens.require_close (parser))
39490 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39491 /*or_comma=*/false,
39492 /*consume_paren=*/true);
39493 return c;
39496 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39497 goto resync_fail;
39499 if (kind == OMP_CLAUSE_DEPEND_SINK)
39501 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
39502 if (!parens.require_close (parser))
39503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39504 /*or_comma=*/false,
39505 /*consume_paren=*/true);
39507 else
39509 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
39510 list, NULL);
39512 if (iterators)
39514 tree block = poplevel (1, 1, 0);
39515 if (iterators == error_mark_node)
39516 iterators = NULL_TREE;
39517 else
39518 TREE_VEC_ELT (iterators, 5) = block;
39521 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39523 OMP_CLAUSE_DEPEND_KIND (c) = kind;
39524 if (iterators)
39525 OMP_CLAUSE_DECL (c)
39526 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
39529 return nlist;
39531 invalid_kind:
39532 cp_parser_error (parser, "invalid depend kind");
39533 resync_fail:
39534 if (iterators)
39535 poplevel (0, 1, 0);
39536 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39537 /*or_comma=*/false,
39538 /*consume_paren=*/true);
39539 return list;
39542 /* OpenMP 4.0:
39543 map ( map-kind : variable-list )
39544 map ( variable-list )
39546 map-kind:
39547 alloc | to | from | tofrom
39549 OpenMP 4.5:
39550 map-kind:
39551 alloc | to | from | tofrom | release | delete
39553 map ( always [,] map-kind: variable-list )
39555 OpenMP 5.0:
39556 map ( [map-type-modifier[,] ...] map-kind: variable-list )
39558 map-type-modifier:
39559 always | close */
39561 static tree
39562 cp_parser_omp_clause_map (cp_parser *parser, tree list)
39564 tree nlist, c;
39565 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
39567 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39568 return list;
39570 int pos = 1;
39571 int map_kind_pos = 0;
39572 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
39573 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
39575 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
39577 map_kind_pos = pos;
39578 break;
39581 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
39582 pos++;
39583 pos++;
39586 bool always_modifier = false;
39587 bool close_modifier = false;
39588 for (int pos = 1; pos < map_kind_pos; ++pos)
39590 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39591 if (tok->type == CPP_COMMA)
39593 cp_lexer_consume_token (parser->lexer);
39594 continue;
39597 const char *p = IDENTIFIER_POINTER (tok->u.value);
39598 if (strcmp ("always", p) == 0)
39600 if (always_modifier)
39602 cp_parser_error (parser, "too many %<always%> modifiers");
39603 cp_parser_skip_to_closing_parenthesis (parser,
39604 /*recovering=*/true,
39605 /*or_comma=*/false,
39606 /*consume_paren=*/true);
39607 return list;
39609 always_modifier = true;
39611 else if (strcmp ("close", p) == 0)
39613 if (close_modifier)
39615 cp_parser_error (parser, "too many %<close%> modifiers");
39616 cp_parser_skip_to_closing_parenthesis (parser,
39617 /*recovering=*/true,
39618 /*or_comma=*/false,
39619 /*consume_paren=*/true);
39620 return list;
39622 close_modifier = true;
39624 else
39626 cp_parser_error (parser, "%<#pragma omp target%> with "
39627 "modifier other than %<always%> or "
39628 "%<close%> on %<map%> clause");
39629 cp_parser_skip_to_closing_parenthesis (parser,
39630 /*recovering=*/true,
39631 /*or_comma=*/false,
39632 /*consume_paren=*/true);
39633 return list;
39636 cp_lexer_consume_token (parser->lexer);
39639 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39640 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39642 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39643 const char *p = IDENTIFIER_POINTER (id);
39645 if (strcmp ("alloc", p) == 0)
39646 kind = GOMP_MAP_ALLOC;
39647 else if (strcmp ("to", p) == 0)
39648 kind = always_modifier ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
39649 else if (strcmp ("from", p) == 0)
39650 kind = always_modifier ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
39651 else if (strcmp ("tofrom", p) == 0)
39652 kind = always_modifier ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
39653 else if (strcmp ("release", p) == 0)
39654 kind = GOMP_MAP_RELEASE;
39655 else
39657 cp_parser_error (parser, "invalid map kind");
39658 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39659 /*or_comma=*/false,
39660 /*consume_paren=*/true);
39661 return list;
39663 cp_lexer_consume_token (parser->lexer);
39664 cp_lexer_consume_token (parser->lexer);
39666 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
39667 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39669 kind = GOMP_MAP_DELETE;
39670 cp_lexer_consume_token (parser->lexer);
39671 cp_lexer_consume_token (parser->lexer);
39674 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
39675 NULL, true);
39677 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39678 OMP_CLAUSE_SET_MAP_KIND (c, kind);
39680 return nlist;
39683 /* OpenMP 4.0:
39684 device ( expression )
39686 OpenMP 5.0:
39687 device ( [device-modifier :] integer-expression )
39689 device-modifier:
39690 ancestor | device_num */
39692 static tree
39693 cp_parser_omp_clause_device (cp_parser *parser, tree list,
39694 location_t location)
39696 tree t, c;
39697 bool ancestor = false;
39699 matching_parens parens;
39700 if (!parens.require_open (parser))
39701 return list;
39703 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39704 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39706 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39707 const char *p = IDENTIFIER_POINTER (tok->u.value);
39708 if (strcmp ("ancestor", p) == 0)
39710 ancestor = true;
39712 /* A requires directive with the reverse_offload clause must be
39713 specified. */
39714 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
39716 error_at (tok->location, "%<ancestor%> device modifier not "
39717 "preceded by %<requires%> directive "
39718 "with %<reverse_offload%> clause");
39719 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39720 return list;
39723 else if (strcmp ("device_num", p) == 0)
39725 else
39727 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
39728 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39729 return list;
39731 cp_lexer_consume_token (parser->lexer);
39732 cp_lexer_consume_token (parser->lexer);
39735 t = cp_parser_assignment_expression (parser);
39737 if (t == error_mark_node
39738 || !parens.require_close (parser))
39739 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39740 /*or_comma=*/false,
39741 /*consume_paren=*/true);
39743 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
39744 "device", location);
39746 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
39747 OMP_CLAUSE_DEVICE_ID (c) = t;
39748 OMP_CLAUSE_CHAIN (c) = list;
39749 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
39751 return c;
39754 /* OpenMP 4.0:
39755 dist_schedule ( static )
39756 dist_schedule ( static , expression ) */
39758 static tree
39759 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
39760 location_t location)
39762 tree c, t;
39764 matching_parens parens;
39765 if (!parens.require_open (parser))
39766 return list;
39768 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
39770 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39771 goto invalid_kind;
39772 cp_lexer_consume_token (parser->lexer);
39774 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39776 cp_lexer_consume_token (parser->lexer);
39778 t = cp_parser_assignment_expression (parser);
39780 if (t == error_mark_node)
39781 goto resync_fail;
39782 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
39784 if (!parens.require_close (parser))
39785 goto resync_fail;
39787 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39788 goto resync_fail;
39790 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
39791 "dist_schedule", location); */
39792 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
39793 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
39794 OMP_CLAUSE_CHAIN (c) = list;
39795 return c;
39797 invalid_kind:
39798 cp_parser_error (parser, "invalid dist_schedule kind");
39799 resync_fail:
39800 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39801 /*or_comma=*/false,
39802 /*consume_paren=*/true);
39803 return list;
39806 /* OpenMP 4.0:
39807 proc_bind ( proc-bind-kind )
39809 proc-bind-kind:
39810 primary | master | close | spread
39811 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
39813 static tree
39814 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
39815 location_t location)
39817 tree c;
39818 enum omp_clause_proc_bind_kind kind;
39820 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39821 return list;
39823 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39825 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39826 const char *p = IDENTIFIER_POINTER (id);
39828 if (strcmp ("primary", p) == 0)
39829 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
39830 else if (strcmp ("master", p) == 0)
39831 kind = OMP_CLAUSE_PROC_BIND_MASTER;
39832 else if (strcmp ("close", p) == 0)
39833 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
39834 else if (strcmp ("spread", p) == 0)
39835 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
39836 else
39837 goto invalid_kind;
39839 else
39840 goto invalid_kind;
39842 cp_lexer_consume_token (parser->lexer);
39843 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39844 goto resync_fail;
39846 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
39847 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
39848 location);
39849 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
39850 OMP_CLAUSE_CHAIN (c) = list;
39851 return c;
39853 invalid_kind:
39854 cp_parser_error (parser, "invalid depend kind");
39855 resync_fail:
39856 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39857 /*or_comma=*/false,
39858 /*consume_paren=*/true);
39859 return list;
39862 /* OpenMP 5.0:
39863 device_type ( host | nohost | any ) */
39865 static tree
39866 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
39867 location_t location)
39869 tree c;
39870 enum omp_clause_device_type_kind kind;
39872 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39873 return list;
39875 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39877 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39878 const char *p = IDENTIFIER_POINTER (id);
39880 if (strcmp ("host", p) == 0)
39881 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
39882 else if (strcmp ("nohost", p) == 0)
39883 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
39884 else if (strcmp ("any", p) == 0)
39885 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
39886 else
39887 goto invalid_kind;
39889 else
39890 goto invalid_kind;
39892 cp_lexer_consume_token (parser->lexer);
39893 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39894 goto resync_fail;
39896 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
39897 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
39898 location); */
39899 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
39900 OMP_CLAUSE_CHAIN (c) = list;
39901 return c;
39903 invalid_kind:
39904 cp_parser_error (parser, "invalid depend kind");
39905 resync_fail:
39906 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39907 /*or_comma=*/false,
39908 /*consume_paren=*/true);
39909 return list;
39912 /* OpenACC:
39913 async [( int-expr )] */
39915 static tree
39916 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
39918 tree c, t;
39919 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39921 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
39923 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39925 matching_parens parens;
39926 parens.consume_open (parser);
39928 t = cp_parser_assignment_expression (parser);
39929 if (t == error_mark_node
39930 || !parens.require_close (parser))
39931 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39932 /*or_comma=*/false,
39933 /*consume_paren=*/true);
39936 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
39938 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
39939 OMP_CLAUSE_ASYNC_EXPR (c) = t;
39940 OMP_CLAUSE_CHAIN (c) = list;
39941 list = c;
39943 return list;
39946 /* Parse all OpenACC clauses. The set clauses allowed by the directive
39947 is a bitmask in MASK. Return the list of clauses found. */
39949 static tree
39950 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
39951 const char *where, cp_token *pragma_tok,
39952 bool finish_p = true)
39954 tree clauses = NULL;
39955 bool first = true;
39957 /* Don't create location wrapper nodes within OpenACC clauses. */
39958 auto_suppress_location_wrappers sentinel;
39960 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39962 location_t here;
39963 pragma_omp_clause c_kind;
39964 omp_clause_code code;
39965 const char *c_name;
39966 tree prev = clauses;
39968 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39969 cp_lexer_consume_token (parser->lexer);
39971 here = cp_lexer_peek_token (parser->lexer)->location;
39972 c_kind = cp_parser_omp_clause_name (parser);
39974 switch (c_kind)
39976 case PRAGMA_OACC_CLAUSE_ASYNC:
39977 clauses = cp_parser_oacc_clause_async (parser, clauses);
39978 c_name = "async";
39979 break;
39980 case PRAGMA_OACC_CLAUSE_AUTO:
39981 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
39982 clauses);
39983 c_name = "auto";
39984 break;
39985 case PRAGMA_OACC_CLAUSE_ATTACH:
39986 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39987 c_name = "attach";
39988 break;
39989 case PRAGMA_OACC_CLAUSE_COLLAPSE:
39990 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
39991 c_name = "collapse";
39992 break;
39993 case PRAGMA_OACC_CLAUSE_COPY:
39994 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39995 c_name = "copy";
39996 break;
39997 case PRAGMA_OACC_CLAUSE_COPYIN:
39998 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39999 c_name = "copyin";
40000 break;
40001 case PRAGMA_OACC_CLAUSE_COPYOUT:
40002 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40003 c_name = "copyout";
40004 break;
40005 case PRAGMA_OACC_CLAUSE_CREATE:
40006 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40007 c_name = "create";
40008 break;
40009 case PRAGMA_OACC_CLAUSE_DELETE:
40010 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40011 c_name = "delete";
40012 break;
40013 case PRAGMA_OMP_CLAUSE_DEFAULT:
40014 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
40015 c_name = "default";
40016 break;
40017 case PRAGMA_OACC_CLAUSE_DETACH:
40018 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40019 c_name = "detach";
40020 break;
40021 case PRAGMA_OACC_CLAUSE_DEVICE:
40022 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40023 c_name = "device";
40024 break;
40025 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
40026 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
40027 c_name = "deviceptr";
40028 break;
40029 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
40030 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40031 c_name = "device_resident";
40032 break;
40033 case PRAGMA_OACC_CLAUSE_FINALIZE:
40034 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
40035 clauses);
40036 c_name = "finalize";
40037 break;
40038 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
40039 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
40040 clauses);
40041 c_name = "firstprivate";
40042 break;
40043 case PRAGMA_OACC_CLAUSE_GANG:
40044 c_name = "gang";
40045 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
40046 c_name, clauses);
40047 break;
40048 case PRAGMA_OACC_CLAUSE_HOST:
40049 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40050 c_name = "host";
40051 break;
40052 case PRAGMA_OACC_CLAUSE_IF:
40053 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
40054 c_name = "if";
40055 break;
40056 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
40057 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
40058 clauses);
40059 c_name = "if_present";
40060 break;
40061 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
40062 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
40063 clauses);
40064 c_name = "independent";
40065 break;
40066 case PRAGMA_OACC_CLAUSE_LINK:
40067 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40068 c_name = "link";
40069 break;
40070 case PRAGMA_OACC_CLAUSE_NO_CREATE:
40071 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40072 c_name = "no_create";
40073 break;
40074 case PRAGMA_OACC_CLAUSE_NOHOST:
40075 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
40076 clauses);
40077 c_name = "nohost";
40078 break;
40079 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
40080 code = OMP_CLAUSE_NUM_GANGS;
40081 c_name = "num_gangs";
40082 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
40083 clauses);
40084 break;
40085 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
40086 c_name = "num_workers";
40087 code = OMP_CLAUSE_NUM_WORKERS;
40088 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
40089 clauses);
40090 break;
40091 case PRAGMA_OACC_CLAUSE_PRESENT:
40092 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40093 c_name = "present";
40094 break;
40095 case PRAGMA_OACC_CLAUSE_PRIVATE:
40096 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
40097 clauses);
40098 c_name = "private";
40099 break;
40100 case PRAGMA_OACC_CLAUSE_REDUCTION:
40101 clauses
40102 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
40103 false, clauses);
40104 c_name = "reduction";
40105 break;
40106 case PRAGMA_OACC_CLAUSE_SEQ:
40107 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
40108 clauses);
40109 c_name = "seq";
40110 break;
40111 case PRAGMA_OACC_CLAUSE_TILE:
40112 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
40113 c_name = "tile";
40114 break;
40115 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
40116 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
40117 clauses);
40118 c_name = "use_device";
40119 break;
40120 case PRAGMA_OACC_CLAUSE_VECTOR:
40121 c_name = "vector";
40122 clauses = cp_parser_oacc_shape_clause (parser, here,
40123 OMP_CLAUSE_VECTOR,
40124 c_name, clauses);
40125 break;
40126 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
40127 c_name = "vector_length";
40128 code = OMP_CLAUSE_VECTOR_LENGTH;
40129 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
40130 clauses);
40131 break;
40132 case PRAGMA_OACC_CLAUSE_WAIT:
40133 clauses = cp_parser_oacc_clause_wait (parser, clauses);
40134 c_name = "wait";
40135 break;
40136 case PRAGMA_OACC_CLAUSE_WORKER:
40137 c_name = "worker";
40138 clauses = cp_parser_oacc_shape_clause (parser, here,
40139 OMP_CLAUSE_WORKER,
40140 c_name, clauses);
40141 break;
40142 default:
40143 cp_parser_error (parser, "expected %<#pragma acc%> clause");
40144 goto saw_error;
40147 first = false;
40149 if (((mask >> c_kind) & 1) == 0)
40151 /* Remove the invalid clause(s) from the list to avoid
40152 confusing the rest of the compiler. */
40153 clauses = prev;
40154 error_at (here, "%qs is not valid for %qs", c_name, where);
40158 saw_error:
40159 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40161 if (finish_p)
40162 return finish_omp_clauses (clauses, C_ORT_ACC);
40164 return clauses;
40167 /* Parse all OpenMP clauses. The set clauses allowed by the directive
40168 is a bitmask in MASK. Return the list of clauses found.
40169 FINISH_P set if finish_omp_clauses should be called.
40170 NESTED non-zero if clauses should be terminated by closing paren instead
40171 of end of pragma. If it is 2, additionally commas are required in between
40172 the clauses. */
40174 static tree
40175 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
40176 const char *where, cp_token *pragma_tok,
40177 bool finish_p = true, int nested = 0)
40179 tree clauses = NULL;
40180 bool first = true;
40181 cp_token *token = NULL;
40183 /* Don't create location wrapper nodes within OpenMP clauses. */
40184 auto_suppress_location_wrappers sentinel;
40186 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40188 pragma_omp_clause c_kind;
40189 const char *c_name;
40190 tree prev = clauses;
40192 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
40193 break;
40195 if (!first
40196 /* OpenMP 5.1 allows optional comma in between directive-name and
40197 clauses everywhere, but as we aren't done with OpenMP 5.0
40198 implementation yet, let's allow it for now only in C++11
40199 attributes. */
40200 || (parser->lexer->in_omp_attribute_pragma && nested != 2))
40202 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40203 cp_lexer_consume_token (parser->lexer);
40204 else if (nested == 2)
40205 error_at (cp_lexer_peek_token (parser->lexer)->location,
40206 "clauses in %<simd%> trait should be separated "
40207 "by %<,%>");
40210 token = cp_lexer_peek_token (parser->lexer);
40211 c_kind = cp_parser_omp_clause_name (parser);
40213 switch (c_kind)
40215 case PRAGMA_OMP_CLAUSE_BIND:
40216 clauses = cp_parser_omp_clause_bind (parser, clauses,
40217 token->location);
40218 c_name = "bind";
40219 break;
40220 case PRAGMA_OMP_CLAUSE_COLLAPSE:
40221 clauses = cp_parser_omp_clause_collapse (parser, clauses,
40222 token->location);
40223 c_name = "collapse";
40224 break;
40225 case PRAGMA_OMP_CLAUSE_COPYIN:
40226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
40227 c_name = "copyin";
40228 break;
40229 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
40230 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
40231 clauses);
40232 c_name = "copyprivate";
40233 break;
40234 case PRAGMA_OMP_CLAUSE_DEFAULT:
40235 clauses = cp_parser_omp_clause_default (parser, clauses,
40236 token->location, false);
40237 c_name = "default";
40238 break;
40239 case PRAGMA_OMP_CLAUSE_FILTER:
40240 clauses = cp_parser_omp_clause_filter (parser, clauses,
40241 token->location);
40242 c_name = "filter";
40243 break;
40244 case PRAGMA_OMP_CLAUSE_FINAL:
40245 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
40246 c_name = "final";
40247 break;
40248 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
40249 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
40250 clauses);
40251 c_name = "firstprivate";
40252 break;
40253 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
40254 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
40255 token->location);
40256 c_name = "grainsize";
40257 break;
40258 case PRAGMA_OMP_CLAUSE_HINT:
40259 clauses = cp_parser_omp_clause_hint (parser, clauses,
40260 token->location);
40261 c_name = "hint";
40262 break;
40263 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
40264 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
40265 token->location);
40266 c_name = "defaultmap";
40267 break;
40268 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
40269 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
40270 clauses);
40271 c_name = "use_device_ptr";
40272 break;
40273 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
40274 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
40275 clauses);
40276 c_name = "use_device_addr";
40277 break;
40278 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
40279 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
40280 clauses);
40281 c_name = "is_device_ptr";
40282 break;
40283 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
40284 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
40285 clauses);
40286 c_name = "has_device_addr";
40287 break;
40288 case PRAGMA_OMP_CLAUSE_IF:
40289 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
40290 true);
40291 c_name = "if";
40292 break;
40293 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
40294 clauses
40295 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
40296 true, clauses);
40297 c_name = "in_reduction";
40298 break;
40299 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
40300 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
40301 c_name = "lastprivate";
40302 break;
40303 case PRAGMA_OMP_CLAUSE_MERGEABLE:
40304 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
40305 token->location);
40306 c_name = "mergeable";
40307 break;
40308 case PRAGMA_OMP_CLAUSE_NOWAIT:
40309 clauses = cp_parser_omp_clause_nowait (parser, clauses,
40310 token->location);
40311 c_name = "nowait";
40312 break;
40313 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
40314 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
40315 token->location);
40316 c_name = "num_tasks";
40317 break;
40318 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
40319 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
40320 token->location);
40321 c_name = "num_threads";
40322 break;
40323 case PRAGMA_OMP_CLAUSE_ORDER:
40324 clauses = cp_parser_omp_clause_order (parser, clauses,
40325 token->location);
40326 c_name = "order";
40327 break;
40328 case PRAGMA_OMP_CLAUSE_ORDERED:
40329 clauses = cp_parser_omp_clause_ordered (parser, clauses,
40330 token->location);
40331 c_name = "ordered";
40332 break;
40333 case PRAGMA_OMP_CLAUSE_PRIORITY:
40334 clauses = cp_parser_omp_clause_priority (parser, clauses,
40335 token->location);
40336 c_name = "priority";
40337 break;
40338 case PRAGMA_OMP_CLAUSE_PRIVATE:
40339 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
40340 clauses);
40341 c_name = "private";
40342 break;
40343 case PRAGMA_OMP_CLAUSE_REDUCTION:
40344 clauses
40345 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
40346 true, clauses);
40347 c_name = "reduction";
40348 break;
40349 case PRAGMA_OMP_CLAUSE_SCHEDULE:
40350 clauses = cp_parser_omp_clause_schedule (parser, clauses,
40351 token->location);
40352 c_name = "schedule";
40353 break;
40354 case PRAGMA_OMP_CLAUSE_SHARED:
40355 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
40356 clauses);
40357 c_name = "shared";
40358 break;
40359 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
40360 clauses
40361 = cp_parser_omp_clause_reduction (parser,
40362 OMP_CLAUSE_TASK_REDUCTION,
40363 true, clauses);
40364 c_name = "task_reduction";
40365 break;
40366 case PRAGMA_OMP_CLAUSE_UNTIED:
40367 clauses = cp_parser_omp_clause_untied (parser, clauses,
40368 token->location);
40369 c_name = "untied";
40370 break;
40371 case PRAGMA_OMP_CLAUSE_INBRANCH:
40372 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
40373 clauses, token->location);
40374 c_name = "inbranch";
40375 break;
40376 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
40377 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
40378 clauses);
40379 c_name = "nontemporal";
40380 break;
40381 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
40382 clauses = cp_parser_omp_clause_branch (parser,
40383 OMP_CLAUSE_NOTINBRANCH,
40384 clauses, token->location);
40385 c_name = "notinbranch";
40386 break;
40387 case PRAGMA_OMP_CLAUSE_PARALLEL:
40388 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
40389 clauses, token->location);
40390 c_name = "parallel";
40391 if (!first)
40393 clause_not_first:
40394 error_at (token->location, "%qs must be the first clause of %qs",
40395 c_name, where);
40396 clauses = prev;
40398 break;
40399 case PRAGMA_OMP_CLAUSE_FOR:
40400 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
40401 clauses, token->location);
40402 c_name = "for";
40403 if (!first)
40404 goto clause_not_first;
40405 break;
40406 case PRAGMA_OMP_CLAUSE_SECTIONS:
40407 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
40408 clauses, token->location);
40409 c_name = "sections";
40410 if (!first)
40411 goto clause_not_first;
40412 break;
40413 case PRAGMA_OMP_CLAUSE_TASKGROUP:
40414 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
40415 clauses, token->location);
40416 c_name = "taskgroup";
40417 if (!first)
40418 goto clause_not_first;
40419 break;
40420 case PRAGMA_OMP_CLAUSE_LINK:
40421 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
40422 c_name = "to";
40423 break;
40424 case PRAGMA_OMP_CLAUSE_TO:
40425 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
40427 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
40428 clauses);
40429 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
40430 OMP_CLAUSE_ENTER_TO (c) = 1;
40431 clauses = nl;
40433 else
40434 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses,
40435 true);
40436 c_name = "to";
40437 break;
40438 case PRAGMA_OMP_CLAUSE_FROM:
40439 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses,
40440 true);
40441 c_name = "from";
40442 break;
40443 case PRAGMA_OMP_CLAUSE_UNIFORM:
40444 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
40445 clauses);
40446 c_name = "uniform";
40447 break;
40448 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
40449 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
40450 token->location);
40451 c_name = "num_teams";
40452 break;
40453 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
40454 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
40455 token->location);
40456 c_name = "thread_limit";
40457 break;
40458 case PRAGMA_OMP_CLAUSE_ALIGNED:
40459 clauses = cp_parser_omp_clause_aligned (parser, clauses);
40460 c_name = "aligned";
40461 break;
40462 case PRAGMA_OMP_CLAUSE_ALLOCATE:
40463 clauses = cp_parser_omp_clause_allocate (parser, clauses);
40464 c_name = "allocate";
40465 break;
40466 case PRAGMA_OMP_CLAUSE_LINEAR:
40468 bool declare_simd = false;
40469 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
40470 declare_simd = true;
40471 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
40473 c_name = "linear";
40474 break;
40475 case PRAGMA_OMP_CLAUSE_AFFINITY:
40476 clauses = cp_parser_omp_clause_affinity (parser, clauses);
40477 c_name = "affinity";
40478 break;
40479 case PRAGMA_OMP_CLAUSE_DEPEND:
40480 clauses = cp_parser_omp_clause_depend (parser, clauses,
40481 token->location);
40482 c_name = "depend";
40483 break;
40484 case PRAGMA_OMP_CLAUSE_DETACH:
40485 clauses = cp_parser_omp_clause_detach (parser, clauses);
40486 c_name = "detach";
40487 break;
40488 case PRAGMA_OMP_CLAUSE_MAP:
40489 clauses = cp_parser_omp_clause_map (parser, clauses);
40490 c_name = "map";
40491 break;
40492 case PRAGMA_OMP_CLAUSE_DEVICE:
40493 clauses = cp_parser_omp_clause_device (parser, clauses,
40494 token->location);
40495 c_name = "device";
40496 break;
40497 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
40498 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
40499 token->location);
40500 c_name = "dist_schedule";
40501 break;
40502 case PRAGMA_OMP_CLAUSE_PROC_BIND:
40503 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
40504 token->location);
40505 c_name = "proc_bind";
40506 break;
40507 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
40508 clauses = cp_parser_omp_clause_device_type (parser, clauses,
40509 token->location);
40510 c_name = "device_type";
40511 break;
40512 case PRAGMA_OMP_CLAUSE_SAFELEN:
40513 clauses = cp_parser_omp_clause_safelen (parser, clauses,
40514 token->location);
40515 c_name = "safelen";
40516 break;
40517 case PRAGMA_OMP_CLAUSE_SIMDLEN:
40518 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
40519 token->location);
40520 c_name = "simdlen";
40521 break;
40522 case PRAGMA_OMP_CLAUSE_NOGROUP:
40523 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
40524 token->location);
40525 c_name = "nogroup";
40526 break;
40527 case PRAGMA_OMP_CLAUSE_THREADS:
40528 clauses
40529 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
40530 clauses, token->location);
40531 c_name = "threads";
40532 break;
40533 case PRAGMA_OMP_CLAUSE_SIMD:
40534 clauses
40535 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
40536 clauses, token->location);
40537 c_name = "simd";
40538 break;
40539 case PRAGMA_OMP_CLAUSE_ENTER:
40540 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
40541 clauses);
40542 c_name = "enter";
40543 break;
40544 default:
40545 cp_parser_error (parser, "expected %<#pragma omp%> clause");
40546 goto saw_error;
40549 first = false;
40551 if (((mask >> c_kind) & 1) == 0)
40553 /* Remove the invalid clause(s) from the list to avoid
40554 confusing the rest of the compiler. */
40555 clauses = prev;
40556 error_at (token->location, "%qs is not valid for %qs", c_name, where);
40559 saw_error:
40560 if (!nested)
40561 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40562 if (finish_p)
40564 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
40565 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
40566 else
40567 return finish_omp_clauses (clauses, C_ORT_OMP);
40569 return clauses;
40572 /* OpenMP 2.5:
40573 structured-block:
40574 statement
40576 In practice, we're also interested in adding the statement to an
40577 outer node. So it is convenient if we work around the fact that
40578 cp_parser_statement calls add_stmt. */
40580 static unsigned
40581 cp_parser_begin_omp_structured_block (cp_parser *parser)
40583 unsigned save = parser->in_statement;
40585 /* Only move the values to IN_OMP_BLOCK if they weren't false.
40586 This preserves the "not within loop or switch" style error messages
40587 for nonsense cases like
40588 void foo() {
40589 #pragma omp single
40590 break;
40593 if (parser->in_statement)
40594 parser->in_statement = IN_OMP_BLOCK;
40596 return save;
40599 static void
40600 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
40602 parser->in_statement = save;
40605 static tree
40606 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
40608 tree stmt = begin_omp_structured_block ();
40609 unsigned int save = cp_parser_begin_omp_structured_block (parser);
40611 parser->omp_attrs_forbidden_p = true;
40612 cp_parser_statement (parser, NULL_TREE, false, if_p);
40614 cp_parser_end_omp_structured_block (parser, save);
40615 return finish_omp_structured_block (stmt);
40618 /* OpenMP 5.0:
40619 # pragma omp allocate (list) [allocator(allocator)] */
40621 static void
40622 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
40624 tree allocator = NULL_TREE;
40625 location_t loc = pragma_tok->location;
40626 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
40628 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
40629 if (parser->lexer->in_omp_attribute_pragma
40630 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40631 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40632 cp_lexer_consume_token (parser->lexer);
40634 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40636 matching_parens parens;
40637 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40638 const char *p = IDENTIFIER_POINTER (id);
40639 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40640 cp_lexer_consume_token (parser->lexer);
40641 if (strcmp (p, "allocator") != 0)
40642 error_at (cloc, "expected %<allocator%>");
40643 else if (parens.require_open (parser))
40645 allocator = cp_parser_assignment_expression (parser);
40646 if (allocator == error_mark_node)
40647 allocator = NULL_TREE;
40648 parens.require_close (parser);
40651 cp_parser_require_pragma_eol (parser, pragma_tok);
40653 if (allocator)
40654 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
40655 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40657 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
40660 /* OpenMP 2.5:
40661 # pragma omp atomic new-line
40662 expression-stmt
40664 expression-stmt:
40665 x binop= expr | x++ | ++x | x-- | --x
40666 binop:
40667 +, *, -, /, &, ^, |, <<, >>
40669 where x is an lvalue expression with scalar type.
40671 OpenMP 3.1:
40672 # pragma omp atomic new-line
40673 update-stmt
40675 # pragma omp atomic read new-line
40676 read-stmt
40678 # pragma omp atomic write new-line
40679 write-stmt
40681 # pragma omp atomic update new-line
40682 update-stmt
40684 # pragma omp atomic capture new-line
40685 capture-stmt
40687 # pragma omp atomic capture new-line
40688 capture-block
40690 read-stmt:
40691 v = x
40692 write-stmt:
40693 x = expr
40694 update-stmt:
40695 expression-stmt | x = x binop expr
40696 capture-stmt:
40697 v = expression-stmt
40698 capture-block:
40699 { v = x; update-stmt; } | { update-stmt; v = x; }
40701 OpenMP 4.0:
40702 update-stmt:
40703 expression-stmt | x = x binop expr | x = expr binop x
40704 capture-stmt:
40705 v = update-stmt
40706 capture-block:
40707 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
40709 OpenMP 5.1:
40710 # pragma omp atomic compare new-line
40711 conditional-update-atomic
40713 # pragma omp atomic compare capture new-line
40714 conditional-update-capture-atomic
40716 conditional-update-atomic:
40717 cond-expr-stmt | cond-update-stmt
40718 cond-expr-stmt:
40719 x = expr ordop x ? expr : x;
40720 x = x ordop expr ? expr : x;
40721 x = x == e ? d : x;
40722 cond-update-stmt:
40723 if (expr ordop x) { x = expr; }
40724 if (x ordop expr) { x = expr; }
40725 if (x == e) { x = d; }
40726 ordop:
40727 <, >
40728 conditional-update-capture-atomic:
40729 v = cond-expr-stmt
40730 { v = x; cond-expr-stmt }
40731 { cond-expr-stmt v = x; }
40732 { v = x; cond-update-stmt }
40733 { cond-update-stmt v = x; }
40734 if (x == e) { x = d; } else { v = x; }
40735 { r = x == e; if (r) { x = d; } }
40736 { r = x == e; if (r) { x = d; } else { v = x; } }
40738 where x, r and v are lvalue expressions with scalar type,
40739 expr, e and d are expressions with scalar type and e might be
40740 the same as v. */
40742 static void
40743 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
40745 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
40746 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
40747 location_t loc = pragma_tok->location;
40748 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
40749 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
40750 bool structured_block = false;
40751 bool first = true;
40752 tree clauses = NULL_TREE;
40753 bool capture = false;
40754 bool compare = false;
40755 bool weak = false;
40756 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40757 bool no_semicolon = false;
40758 bool extra_scope = false;
40760 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40762 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
40763 if ((!first || parser->lexer->in_omp_attribute_pragma)
40764 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40765 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40766 cp_lexer_consume_token (parser->lexer);
40768 first = false;
40770 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40772 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40773 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40774 const char *p = IDENTIFIER_POINTER (id);
40775 enum tree_code new_code = ERROR_MARK;
40776 enum omp_memory_order new_memory_order
40777 = OMP_MEMORY_ORDER_UNSPECIFIED;
40778 bool new_capture = false;
40779 bool new_compare = false;
40780 bool new_weak = false;
40781 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40783 if (!strcmp (p, "read"))
40784 new_code = OMP_ATOMIC_READ;
40785 else if (!strcmp (p, "write"))
40786 new_code = NOP_EXPR;
40787 else if (!strcmp (p, "update"))
40788 new_code = OMP_ATOMIC;
40789 else if (openacc && !strcmp (p, "capture"))
40790 new_code = OMP_ATOMIC_CAPTURE_NEW;
40791 else if (openacc)
40793 p = NULL;
40794 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
40795 "or %<capture%> clause");
40797 else if (!strcmp (p, "capture"))
40798 new_capture = true;
40799 else if (!strcmp (p, "compare"))
40800 new_compare = true;
40801 else if (!strcmp (p, "weak"))
40802 new_weak = true;
40803 else if (!strcmp (p, "fail"))
40805 matching_parens parens;
40807 cp_lexer_consume_token (parser->lexer);
40808 if (!parens.require_open (parser))
40809 continue;
40811 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40813 id = cp_lexer_peek_token (parser->lexer)->u.value;
40814 const char *q = IDENTIFIER_POINTER (id);
40816 if (!strcmp (q, "seq_cst"))
40817 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
40818 else if (!strcmp (q, "acquire"))
40819 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
40820 else if (!strcmp (q, "relaxed"))
40821 new_fail = OMP_MEMORY_ORDER_RELAXED;
40824 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40826 cp_lexer_consume_token (parser->lexer);
40827 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40828 error_at (cloc, "too many %qs clauses", "fail");
40829 else
40830 fail = new_fail;
40832 else
40833 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
40834 "or %<relaxed%>");
40835 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
40836 || !parens.require_close (parser))
40837 cp_parser_skip_to_closing_parenthesis (parser,
40838 /*recovering=*/true,
40839 /*or_comma=*/false,
40840 /*consume_paren=*/true);
40841 continue;
40843 else if (!strcmp (p, "seq_cst"))
40844 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40845 else if (!strcmp (p, "acq_rel"))
40846 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
40847 else if (!strcmp (p, "release"))
40848 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
40849 else if (!strcmp (p, "acquire"))
40850 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40851 else if (!strcmp (p, "relaxed"))
40852 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
40853 else if (!strcmp (p, "hint"))
40855 cp_lexer_consume_token (parser->lexer);
40856 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
40857 continue;
40859 else
40861 p = NULL;
40862 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
40863 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
40864 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
40865 "%<relaxed%> or %<hint%> clause");
40867 if (p)
40869 if (new_code != ERROR_MARK)
40871 /* OpenACC permits 'update capture'. */
40872 if (openacc
40873 && code == OMP_ATOMIC
40874 && new_code == OMP_ATOMIC_CAPTURE_NEW)
40875 code = new_code;
40876 else if (code != ERROR_MARK)
40877 error_at (cloc, "too many atomic clauses");
40878 else
40879 code = new_code;
40881 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
40883 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
40884 error_at (cloc, "too many memory order clauses");
40885 else
40886 memory_order = new_memory_order;
40888 else if (new_capture)
40890 if (capture)
40891 error_at (cloc, "too many %qs clauses", "capture");
40892 else
40893 capture = true;
40895 else if (new_compare)
40897 if (compare)
40898 error_at (cloc, "too many %qs clauses", "compare");
40899 else
40900 compare = true;
40902 else if (new_weak)
40904 if (weak)
40905 error_at (cloc, "too many %qs clauses", "weak");
40906 else
40907 weak = true;
40909 cp_lexer_consume_token (parser->lexer);
40910 continue;
40913 break;
40915 cp_parser_require_pragma_eol (parser, pragma_tok);
40917 if (code == ERROR_MARK)
40918 code = OMP_ATOMIC;
40919 if (capture)
40921 if (code != OMP_ATOMIC)
40922 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
40923 "clauses", "capture");
40924 else
40925 code = OMP_ATOMIC_CAPTURE_NEW;
40927 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
40929 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
40930 "clauses", "compare");
40931 compare = false;
40933 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
40935 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
40936 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40938 if (weak && !compare)
40940 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
40941 weak = false;
40943 if (openacc)
40944 memory_order = OMP_MEMORY_ORDER_RELAXED;
40945 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
40947 omp_requires_mask
40948 = (enum omp_requires) (omp_requires_mask
40949 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
40950 switch ((enum omp_memory_order)
40951 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
40953 case OMP_MEMORY_ORDER_UNSPECIFIED:
40954 case OMP_MEMORY_ORDER_RELAXED:
40955 memory_order = OMP_MEMORY_ORDER_RELAXED;
40956 break;
40957 case OMP_MEMORY_ORDER_SEQ_CST:
40958 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40959 break;
40960 case OMP_MEMORY_ORDER_ACQ_REL:
40961 switch (code)
40963 case OMP_ATOMIC_READ:
40964 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40965 break;
40966 case NOP_EXPR: /* atomic write */
40967 memory_order = OMP_MEMORY_ORDER_RELEASE;
40968 break;
40969 default:
40970 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
40971 break;
40973 break;
40974 default:
40975 gcc_unreachable ();
40978 else
40979 switch (code)
40981 case OMP_ATOMIC_READ:
40982 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
40984 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
40985 "%<release%> clause");
40986 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40988 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
40989 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40990 break;
40991 case NOP_EXPR: /* atomic write */
40992 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
40994 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
40995 "%<acquire%> clause");
40996 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40998 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
40999 memory_order = OMP_MEMORY_ORDER_RELEASE;
41000 break;
41001 default:
41002 break;
41004 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41005 memory_order
41006 = (enum omp_memory_order) (memory_order
41007 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
41009 switch (code)
41011 case OMP_ATOMIC_READ:
41012 case NOP_EXPR: /* atomic write */
41013 v = cp_parser_unary_expression (parser);
41014 if (v == error_mark_node)
41015 goto saw_error;
41016 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41017 goto saw_error;
41018 if (code == NOP_EXPR)
41019 lhs = cp_parser_expression (parser);
41020 else
41021 lhs = cp_parser_unary_expression (parser);
41022 if (lhs == error_mark_node)
41023 goto saw_error;
41024 if (code == NOP_EXPR)
41026 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
41027 opcode. */
41028 code = OMP_ATOMIC;
41029 rhs = lhs;
41030 lhs = v;
41031 v = NULL_TREE;
41033 goto done;
41034 case OMP_ATOMIC_CAPTURE_NEW:
41035 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
41037 cp_lexer_consume_token (parser->lexer);
41038 structured_block = true;
41040 else if (compare
41041 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
41042 break;
41043 else
41045 v = cp_parser_unary_expression (parser);
41046 if (v == error_mark_node)
41047 goto saw_error;
41048 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41049 goto saw_error;
41050 if (compare
41051 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
41053 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
41054 error_at (eloc, "expected expression");
41055 goto saw_error;
41058 default:
41059 break;
41062 restart:
41063 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
41065 cp_lexer_consume_token (parser->lexer);
41067 matching_parens parens;
41068 if (!parens.require_open (parser))
41069 goto saw_error;
41070 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
41071 tree cmp_expr;
41072 if (r)
41073 cmp_expr = cp_parser_unary_expression (parser);
41074 else
41075 cmp_expr = cp_parser_binary_expression (parser, false, true,
41076 PREC_NOT_OPERATOR, NULL);
41077 if (!parens.require_close (parser))
41078 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41079 if (cmp_expr == error_mark_node)
41080 goto saw_error;
41081 if (r)
41083 if (!cp_tree_equal (cmp_expr, r))
41084 goto bad_if;
41085 cmp_expr = rhs;
41086 rhs = NULL_TREE;
41087 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
41089 if (TREE_CODE (cmp_expr) == EQ_EXPR)
41091 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
41093 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
41094 "expected %<==%> comparison in %<if%> condition");
41095 goto saw_error;
41097 else if (TREE_CODE (cmp_expr) != GT_EXPR
41098 && TREE_CODE (cmp_expr) != LT_EXPR)
41100 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
41101 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
41102 "condition");
41103 goto saw_error;
41105 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
41106 goto saw_error;
41108 extra_scope = true;
41109 eloc = cp_lexer_peek_token (parser->lexer)->location;
41110 lhs = cp_parser_unary_expression (parser);
41111 orig_lhs = lhs;
41112 if (lhs == error_mark_node)
41113 goto saw_error;
41114 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
41116 cp_parser_error (parser, "expected %<=%>");
41117 goto saw_error;
41119 cp_lexer_consume_token (parser->lexer);
41120 eloc = cp_lexer_peek_token (parser->lexer)->location;
41121 if (TREE_CODE (cmp_expr) == EQ_EXPR)
41122 rhs1 = cp_parser_expression (parser);
41123 else
41124 rhs1 = cp_parser_simple_cast_expression (parser);
41126 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41127 goto saw_error;
41129 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
41130 goto saw_error;
41132 extra_scope = false;
41133 no_semicolon = true;
41135 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
41137 if (TREE_CODE (cmp_expr) == EQ_EXPR)
41139 opcode = COND_EXPR;
41140 rhs = TREE_OPERAND (cmp_expr, 1);
41142 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
41144 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
41145 ? MIN_EXPR : MAX_EXPR);
41146 rhs = rhs1;
41147 rhs1 = TREE_OPERAND (cmp_expr, 0);
41149 else
41150 goto bad_if;
41152 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
41153 goto bad_if;
41154 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
41155 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
41157 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
41158 ? MAX_EXPR : MIN_EXPR);
41159 rhs = rhs1;
41160 rhs1 = TREE_OPERAND (cmp_expr, 1);
41162 else
41164 bad_if:
41165 cp_parser_error (parser,
41166 "invalid form of %<#pragma omp atomic compare%>");
41167 goto saw_error;
41170 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
41172 if (code != OMP_ATOMIC_CAPTURE_NEW
41173 || (structured_block && r == NULL_TREE)
41174 || TREE_CODE (cmp_expr) != EQ_EXPR)
41176 eloc = cp_lexer_peek_token (parser->lexer)->location;
41177 error_at (eloc, "unexpected %<else%>");
41178 goto saw_error;
41181 cp_lexer_consume_token (parser->lexer);
41183 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
41184 goto saw_error;
41186 extra_scope = true;
41187 v = cp_parser_unary_expression (parser);
41188 if (v == error_mark_node)
41189 goto saw_error;
41190 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41191 goto saw_error;
41193 tree expr = cp_parser_simple_cast_expression (parser);
41195 if (!cp_tree_equal (expr, lhs))
41196 goto bad_if;
41198 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41199 goto saw_error;
41201 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
41202 goto saw_error;
41204 extra_scope = false;
41205 code = OMP_ATOMIC_CAPTURE_OLD;
41206 if (r == NULL_TREE)
41207 /* Signal to c_finish_omp_atomic that in
41208 if (x == e) { x = d; } else { v = x; }
41209 case the store to v should be conditional. */
41210 r = void_list_node;
41212 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41214 cp_parser_error (parser, "expected %<else%>");
41215 goto saw_error;
41217 else if (code == OMP_ATOMIC_CAPTURE_NEW
41218 && r != NULL_TREE
41219 && v == NULL_TREE)
41220 code = OMP_ATOMIC;
41221 goto stmt_done;
41223 lhs = cp_parser_unary_expression (parser);
41224 orig_lhs = lhs;
41225 switch (TREE_CODE (lhs))
41227 case ERROR_MARK:
41228 goto saw_error;
41230 case POSTINCREMENT_EXPR:
41231 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41232 code = OMP_ATOMIC_CAPTURE_OLD;
41233 /* FALLTHROUGH */
41234 case PREINCREMENT_EXPR:
41235 lhs = TREE_OPERAND (lhs, 0);
41236 opcode = PLUS_EXPR;
41237 rhs = integer_one_node;
41238 if (compare)
41239 goto invalid_compare;
41240 break;
41242 case POSTDECREMENT_EXPR:
41243 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41244 code = OMP_ATOMIC_CAPTURE_OLD;
41245 /* FALLTHROUGH */
41246 case PREDECREMENT_EXPR:
41247 lhs = TREE_OPERAND (lhs, 0);
41248 opcode = MINUS_EXPR;
41249 rhs = integer_one_node;
41250 if (compare)
41251 goto invalid_compare;
41252 break;
41254 case COMPOUND_EXPR:
41255 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
41256 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
41257 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
41258 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
41259 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
41260 (TREE_OPERAND (lhs, 1), 0), 0)))
41261 == BOOLEAN_TYPE)
41262 /* Undo effects of boolean_increment for post {in,de}crement. */
41263 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
41264 /* FALLTHRU */
41265 case MODIFY_EXPR:
41266 if (TREE_CODE (lhs) == MODIFY_EXPR
41267 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
41269 /* Undo effects of boolean_increment. */
41270 if (integer_onep (TREE_OPERAND (lhs, 1)))
41272 /* This is pre or post increment. */
41273 rhs = TREE_OPERAND (lhs, 1);
41274 lhs = TREE_OPERAND (lhs, 0);
41275 opcode = NOP_EXPR;
41276 if (code == OMP_ATOMIC_CAPTURE_NEW
41277 && !structured_block
41278 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
41279 code = OMP_ATOMIC_CAPTURE_OLD;
41280 if (compare)
41281 goto invalid_compare;
41282 break;
41285 /* FALLTHRU */
41286 default:
41287 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
41289 cp_parser_error (parser, "expected %<=%>");
41290 goto saw_error;
41292 switch (cp_lexer_peek_token (parser->lexer)->type)
41294 case CPP_MULT_EQ:
41295 opcode = MULT_EXPR;
41296 break;
41297 case CPP_DIV_EQ:
41298 opcode = TRUNC_DIV_EXPR;
41299 break;
41300 case CPP_PLUS_EQ:
41301 opcode = PLUS_EXPR;
41302 break;
41303 case CPP_MINUS_EQ:
41304 opcode = MINUS_EXPR;
41305 break;
41306 case CPP_LSHIFT_EQ:
41307 opcode = LSHIFT_EXPR;
41308 break;
41309 case CPP_RSHIFT_EQ:
41310 opcode = RSHIFT_EXPR;
41311 break;
41312 case CPP_AND_EQ:
41313 opcode = BIT_AND_EXPR;
41314 break;
41315 case CPP_OR_EQ:
41316 opcode = BIT_IOR_EXPR;
41317 break;
41318 case CPP_XOR_EQ:
41319 opcode = BIT_XOR_EXPR;
41320 break;
41321 case CPP_EQ:
41322 enum cp_parser_prec oprec;
41323 cp_token *token;
41324 cp_lexer_consume_token (parser->lexer);
41325 cp_parser_parse_tentatively (parser);
41326 rhs1 = cp_parser_simple_cast_expression (parser);
41327 if (rhs1 == error_mark_node)
41329 cp_parser_abort_tentative_parse (parser);
41330 cp_parser_simple_cast_expression (parser);
41331 goto saw_error;
41333 token = cp_lexer_peek_token (parser->lexer);
41334 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
41336 cp_parser_abort_tentative_parse (parser);
41337 cp_parser_parse_tentatively (parser);
41338 rhs = cp_parser_binary_expression (parser, false, true,
41339 PREC_NOT_OPERATOR, NULL);
41340 if (rhs == error_mark_node)
41342 cp_parser_abort_tentative_parse (parser);
41343 cp_parser_binary_expression (parser, false, true,
41344 PREC_NOT_OPERATOR, NULL);
41345 goto saw_error;
41347 switch (TREE_CODE (rhs))
41349 case MULT_EXPR:
41350 case TRUNC_DIV_EXPR:
41351 case RDIV_EXPR:
41352 case PLUS_EXPR:
41353 case MINUS_EXPR:
41354 case LSHIFT_EXPR:
41355 case RSHIFT_EXPR:
41356 case BIT_AND_EXPR:
41357 case BIT_IOR_EXPR:
41358 case BIT_XOR_EXPR:
41359 if (compare)
41360 break;
41361 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
41363 if (cp_parser_parse_definitely (parser))
41365 opcode = TREE_CODE (rhs);
41366 rhs1 = TREE_OPERAND (rhs, 0);
41367 rhs = TREE_OPERAND (rhs, 1);
41368 goto stmt_done;
41370 else
41371 goto saw_error;
41373 break;
41374 case EQ_EXPR:
41375 if (!compare
41376 || code != OMP_ATOMIC_CAPTURE_NEW
41377 || !structured_block
41378 || v
41379 || r)
41380 break;
41381 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
41382 && cp_lexer_nth_token_is_keyword (parser->lexer,
41383 2, RID_IF))
41385 if (cp_parser_parse_definitely (parser))
41387 r = lhs;
41388 lhs = NULL_TREE;
41389 rhs1 = NULL_TREE;
41390 cp_lexer_consume_token (parser->lexer);
41391 goto restart;
41394 break;
41395 case GT_EXPR:
41396 case LT_EXPR:
41397 if (compare
41398 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
41399 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
41400 && cp_parser_parse_definitely (parser))
41402 opcode = TREE_CODE (rhs);
41403 rhs1 = TREE_OPERAND (rhs, 0);
41404 rhs = TREE_OPERAND (rhs, 1);
41405 cond_expr:
41406 cp_lexer_consume_token (parser->lexer);
41407 bool saved_colon_corrects_to_scope_p
41408 = parser->colon_corrects_to_scope_p;
41409 parser->colon_corrects_to_scope_p = false;
41410 tree e1 = cp_parser_expression (parser);
41411 parser->colon_corrects_to_scope_p
41412 = saved_colon_corrects_to_scope_p;
41413 cp_parser_require (parser, CPP_COLON, RT_COLON);
41414 tree e2 = cp_parser_simple_cast_expression (parser);
41415 if (cp_tree_equal (lhs, e2))
41417 if (cp_tree_equal (lhs, rhs1))
41419 if (opcode == EQ_EXPR)
41421 opcode = COND_EXPR;
41422 rhs1 = e1;
41423 goto stmt_done;
41425 if (cp_tree_equal (rhs, e1))
41427 opcode
41428 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
41429 rhs = e1;
41430 goto stmt_done;
41433 else
41435 gcc_assert (opcode != EQ_EXPR);
41436 if (cp_tree_equal (rhs1, e1))
41438 opcode
41439 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
41440 rhs1 = rhs;
41441 rhs = e1;
41442 goto stmt_done;
41446 cp_parser_error (parser,
41447 "invalid form of "
41448 "%<#pragma omp atomic compare%>");
41449 goto saw_error;
41451 break;
41452 default:
41453 break;
41455 cp_parser_abort_tentative_parse (parser);
41456 if (structured_block
41457 && code == OMP_ATOMIC_CAPTURE_OLD
41458 && !compare)
41460 rhs = cp_parser_expression (parser);
41461 if (rhs == error_mark_node)
41462 goto saw_error;
41463 opcode = NOP_EXPR;
41464 rhs1 = NULL_TREE;
41465 goto stmt_done;
41467 cp_parser_error (parser,
41468 "invalid form of %<#pragma omp atomic%>");
41469 goto saw_error;
41471 if (!cp_parser_parse_definitely (parser))
41472 goto saw_error;
41473 switch (token->type)
41475 case CPP_SEMICOLON:
41476 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
41478 code = OMP_ATOMIC_CAPTURE_OLD;
41479 v = lhs;
41480 lhs = NULL_TREE;
41481 lhs1 = rhs1;
41482 rhs1 = NULL_TREE;
41483 cp_lexer_consume_token (parser->lexer);
41484 goto restart;
41486 else if (structured_block && !compare)
41488 opcode = NOP_EXPR;
41489 rhs = rhs1;
41490 rhs1 = NULL_TREE;
41491 goto stmt_done;
41493 cp_parser_error (parser,
41494 "invalid form of %<#pragma omp atomic%>");
41495 goto saw_error;
41496 case CPP_MULT:
41497 opcode = MULT_EXPR;
41498 break;
41499 case CPP_DIV:
41500 opcode = TRUNC_DIV_EXPR;
41501 break;
41502 case CPP_PLUS:
41503 opcode = PLUS_EXPR;
41504 break;
41505 case CPP_MINUS:
41506 opcode = MINUS_EXPR;
41507 break;
41508 case CPP_LSHIFT:
41509 opcode = LSHIFT_EXPR;
41510 break;
41511 case CPP_RSHIFT:
41512 opcode = RSHIFT_EXPR;
41513 break;
41514 case CPP_AND:
41515 opcode = BIT_AND_EXPR;
41516 break;
41517 case CPP_OR:
41518 opcode = BIT_IOR_EXPR;
41519 break;
41520 case CPP_XOR:
41521 opcode = BIT_XOR_EXPR;
41522 break;
41523 case CPP_EQ_EQ:
41524 opcode = EQ_EXPR;
41525 break;
41526 case CPP_GREATER:
41527 opcode = GT_EXPR;
41528 break;
41529 case CPP_LESS:
41530 opcode = LT_EXPR;
41531 break;
41532 default:
41533 cp_parser_error (parser,
41534 "invalid operator for %<#pragma omp atomic%>");
41535 goto saw_error;
41537 if (compare
41538 && TREE_CODE_CLASS (opcode) != tcc_comparison)
41540 cp_parser_error (parser,
41541 "invalid form of "
41542 "%<#pragma omp atomic compare%>");
41543 goto saw_error;
41545 oprec = TOKEN_PRECEDENCE (token);
41546 gcc_assert (oprec != PREC_NOT_OPERATOR);
41547 if (commutative_tree_code (opcode))
41548 oprec = (enum cp_parser_prec) (oprec - 1);
41549 cp_lexer_consume_token (parser->lexer);
41550 rhs = cp_parser_binary_expression (parser, false, false,
41551 oprec, NULL);
41552 if (rhs == error_mark_node)
41553 goto saw_error;
41554 if (compare)
41556 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
41558 cp_parser_error (parser,
41559 "invalid form of "
41560 "%<#pragma omp atomic compare%>");
41561 goto saw_error;
41563 goto cond_expr;
41565 goto stmt_done;
41566 default:
41567 cp_parser_error (parser,
41568 "invalid operator for %<#pragma omp atomic%>");
41569 goto saw_error;
41571 cp_lexer_consume_token (parser->lexer);
41573 rhs = cp_parser_expression (parser);
41574 if (rhs == error_mark_node)
41575 goto saw_error;
41576 break;
41578 stmt_done:
41579 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
41581 if (!no_semicolon
41582 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41583 goto saw_error;
41584 no_semicolon = false;
41585 v = cp_parser_unary_expression (parser);
41586 if (v == error_mark_node)
41587 goto saw_error;
41588 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41589 goto saw_error;
41590 lhs1 = cp_parser_unary_expression (parser);
41591 if (lhs1 == error_mark_node)
41592 goto saw_error;
41594 if (structured_block)
41596 if (!no_semicolon)
41597 cp_parser_consume_semicolon_at_end_of_statement (parser);
41598 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
41600 done:
41601 if (weak && opcode != COND_EXPR)
41603 error_at (loc, "%<weak%> clause requires atomic equality comparison");
41604 weak = false;
41606 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
41607 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
41608 rhs1, r, clauses, memory_order, weak);
41609 if (!structured_block && !no_semicolon)
41610 cp_parser_consume_semicolon_at_end_of_statement (parser);
41611 return;
41613 invalid_compare:
41614 error ("invalid form of %<pragma omp atomic compare%>");
41615 /* FALLTHRU */
41616 saw_error:
41617 cp_parser_skip_to_end_of_block_or_statement (parser);
41618 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41619 cp_lexer_consume_token (parser->lexer);
41620 if (structured_block)
41622 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41623 cp_lexer_consume_token (parser->lexer);
41624 else if (code == OMP_ATOMIC_CAPTURE_NEW)
41626 cp_parser_skip_to_end_of_block_or_statement (parser);
41627 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41628 cp_lexer_consume_token (parser->lexer);
41634 /* OpenMP 2.5:
41635 # pragma omp barrier new-line */
41637 static void
41638 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
41640 cp_parser_require_pragma_eol (parser, pragma_tok);
41641 finish_omp_barrier ();
41644 /* OpenMP 2.5:
41645 # pragma omp critical [(name)] new-line
41646 structured-block
41648 OpenMP 4.5:
41649 # pragma omp critical [(name) [hint(expression)]] new-line
41650 structured-block */
41652 #define OMP_CRITICAL_CLAUSE_MASK \
41653 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
41655 static tree
41656 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41658 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
41660 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41662 matching_parens parens;
41663 parens.consume_open (parser);
41665 name = cp_parser_identifier (parser);
41667 if (name == error_mark_node
41668 || !parens.require_close (parser))
41669 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41670 /*or_comma=*/false,
41671 /*consume_paren=*/true);
41672 if (name == error_mark_node)
41673 name = NULL;
41675 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41676 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41677 cp_lexer_consume_token (parser->lexer);
41680 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
41681 "#pragma omp critical", pragma_tok);
41683 stmt = cp_parser_omp_structured_block (parser, if_p);
41684 return c_finish_omp_critical (input_location, stmt, name, clauses);
41687 /* OpenMP 5.0:
41688 # pragma omp depobj ( depobj ) depobj-clause new-line
41690 depobj-clause:
41691 depend (dependence-type : locator)
41692 destroy
41693 update (dependence-type)
41695 dependence-type:
41698 inout
41699 mutexinout */
41701 static void
41702 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
41704 location_t loc = pragma_tok->location;
41705 matching_parens parens;
41706 if (!parens.require_open (parser))
41708 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41709 return;
41712 tree depobj = cp_parser_assignment_expression (parser);
41714 if (!parens.require_close (parser))
41715 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41716 /*or_comma=*/false,
41717 /*consume_paren=*/true);
41719 tree clause = NULL_TREE;
41720 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
41721 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
41722 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41723 if (parser->lexer->in_omp_attribute_pragma
41724 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41725 cp_lexer_consume_token (parser->lexer);
41726 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41728 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41729 const char *p = IDENTIFIER_POINTER (id);
41731 cp_lexer_consume_token (parser->lexer);
41732 if (!strcmp ("depend", p))
41734 /* Don't create location wrapper nodes within the depend clause. */
41735 auto_suppress_location_wrappers sentinel;
41736 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
41737 if (clause)
41738 clause = finish_omp_clauses (clause, C_ORT_OMP);
41739 if (!clause)
41740 clause = error_mark_node;
41742 else if (!strcmp ("destroy", p))
41743 kind = OMP_CLAUSE_DEPEND_LAST;
41744 else if (!strcmp ("update", p))
41746 matching_parens c_parens;
41747 if (c_parens.require_open (parser))
41749 location_t c2_loc
41750 = cp_lexer_peek_token (parser->lexer)->location;
41751 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41753 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
41754 const char *p2 = IDENTIFIER_POINTER (id2);
41756 cp_lexer_consume_token (parser->lexer);
41757 if (!strcmp ("in", p2))
41758 kind = OMP_CLAUSE_DEPEND_IN;
41759 else if (!strcmp ("out", p2))
41760 kind = OMP_CLAUSE_DEPEND_OUT;
41761 else if (!strcmp ("inout", p2))
41762 kind = OMP_CLAUSE_DEPEND_INOUT;
41763 else if (!strcmp ("mutexinoutset", p2))
41764 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
41765 else if (!strcmp ("inoutset", p2))
41766 kind = OMP_CLAUSE_DEPEND_INOUTSET;
41768 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
41770 clause = error_mark_node;
41771 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
41772 "%<mutexinoutset%> or %<inoutset%>");
41774 if (!c_parens.require_close (parser))
41775 cp_parser_skip_to_closing_parenthesis (parser,
41776 /*recovering=*/true,
41777 /*or_comma=*/false,
41778 /*consume_paren=*/true);
41780 else
41781 clause = error_mark_node;
41784 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
41786 clause = error_mark_node;
41787 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
41789 cp_parser_require_pragma_eol (parser, pragma_tok);
41791 finish_omp_depobj (loc, depobj, kind, clause);
41795 /* OpenMP 2.5:
41796 # pragma omp flush flush-vars[opt] new-line
41798 flush-vars:
41799 ( variable-list )
41801 OpenMP 5.0:
41802 # pragma omp flush memory-order-clause new-line */
41804 static void
41805 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
41807 enum memmodel mo = MEMMODEL_LAST;
41808 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41809 if (parser->lexer->in_omp_attribute_pragma
41810 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41811 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41812 cp_lexer_consume_token (parser->lexer);
41813 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41815 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41816 const char *p = IDENTIFIER_POINTER (id);
41817 if (!strcmp (p, "seq_cst"))
41818 mo = MEMMODEL_SEQ_CST;
41819 else if (!strcmp (p, "acq_rel"))
41820 mo = MEMMODEL_ACQ_REL;
41821 else if (!strcmp (p, "release"))
41822 mo = MEMMODEL_RELEASE;
41823 else if (!strcmp (p, "acquire"))
41824 mo = MEMMODEL_ACQUIRE;
41825 else
41826 error_at (cp_lexer_peek_token (parser->lexer)->location,
41827 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
41828 "%<acquire%>");
41829 cp_lexer_consume_token (parser->lexer);
41831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41833 if (mo != MEMMODEL_LAST)
41834 error_at (cp_lexer_peek_token (parser->lexer)->location,
41835 "%<flush%> list specified together with memory order "
41836 "clause");
41837 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
41839 cp_parser_require_pragma_eol (parser, pragma_tok);
41841 finish_omp_flush (mo);
41844 /* Helper function, to parse omp for increment expression. */
41846 static tree
41847 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
41849 tree cond = cp_parser_binary_expression (parser, false, true,
41850 PREC_NOT_OPERATOR, NULL);
41851 if (cond == error_mark_node
41852 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
41854 cp_parser_skip_to_end_of_statement (parser);
41855 return error_mark_node;
41858 switch (TREE_CODE (cond))
41860 case GT_EXPR:
41861 case GE_EXPR:
41862 case LT_EXPR:
41863 case LE_EXPR:
41864 break;
41865 case NE_EXPR:
41866 if (code != OACC_LOOP)
41867 break;
41868 gcc_fallthrough ();
41869 default:
41870 return error_mark_node;
41873 /* If decl is an iterator, preserve LHS and RHS of the relational
41874 expr until finish_omp_for. */
41875 if (decl
41876 && (type_dependent_expression_p (decl)
41877 || CLASS_TYPE_P (TREE_TYPE (decl))))
41878 return cond;
41880 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
41881 TREE_CODE (cond),
41882 TREE_OPERAND (cond, 0), ERROR_MARK,
41883 TREE_OPERAND (cond, 1), ERROR_MARK,
41884 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
41887 /* Helper function, to parse omp for increment expression. */
41889 static tree
41890 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
41892 cp_token *token = cp_lexer_peek_token (parser->lexer);
41893 enum tree_code op;
41894 tree lhs, rhs;
41895 cp_id_kind idk;
41896 bool decl_first;
41898 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
41900 op = (token->type == CPP_PLUS_PLUS
41901 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
41902 cp_lexer_consume_token (parser->lexer);
41903 lhs = cp_parser_simple_cast_expression (parser);
41904 if (lhs != decl
41905 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
41906 return error_mark_node;
41907 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
41910 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
41911 if (lhs != decl
41912 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
41913 return error_mark_node;
41915 token = cp_lexer_peek_token (parser->lexer);
41916 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
41918 op = (token->type == CPP_PLUS_PLUS
41919 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
41920 cp_lexer_consume_token (parser->lexer);
41921 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
41924 op = cp_parser_assignment_operator_opt (parser);
41925 if (op == ERROR_MARK)
41926 return error_mark_node;
41928 if (op != NOP_EXPR)
41930 rhs = cp_parser_assignment_expression (parser);
41931 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
41932 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
41935 lhs = cp_parser_binary_expression (parser, false, false,
41936 PREC_ADDITIVE_EXPRESSION, NULL);
41937 token = cp_lexer_peek_token (parser->lexer);
41938 decl_first = (lhs == decl
41939 || (processing_template_decl && cp_tree_equal (lhs, decl)));
41940 if (decl_first)
41941 lhs = NULL_TREE;
41942 if (token->type != CPP_PLUS
41943 && token->type != CPP_MINUS)
41944 return error_mark_node;
41948 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
41949 cp_lexer_consume_token (parser->lexer);
41950 rhs = cp_parser_binary_expression (parser, false, false,
41951 PREC_ADDITIVE_EXPRESSION, NULL);
41952 token = cp_lexer_peek_token (parser->lexer);
41953 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
41955 if (lhs == NULL_TREE)
41957 if (op == PLUS_EXPR)
41958 lhs = rhs;
41959 else
41960 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
41961 NULL_TREE, tf_warning_or_error);
41963 else
41964 lhs = build_x_binary_op (input_location, op,
41965 lhs, ERROR_MARK,
41966 rhs, ERROR_MARK,
41967 NULL_TREE, NULL, tf_warning_or_error);
41970 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
41972 if (!decl_first)
41974 if ((rhs != decl
41975 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
41976 || op == MINUS_EXPR)
41977 return error_mark_node;
41978 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
41980 else
41981 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
41983 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
41986 /* Parse the initialization statement of an OpenMP for loop.
41988 Return true if the resulting construct should have an
41989 OMP_CLAUSE_PRIVATE added to it. */
41991 static tree
41992 cp_parser_omp_for_loop_init (cp_parser *parser,
41993 tree &this_pre_body,
41994 releasing_vec &for_block,
41995 tree &init,
41996 tree &orig_init,
41997 tree &decl,
41998 tree &real_decl)
42000 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42001 return NULL_TREE;
42003 tree add_private_clause = NULL_TREE;
42005 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
42007 init-expr:
42008 var = lb
42009 integer-type var = lb
42010 random-access-iterator-type var = lb
42011 pointer-type var = lb
42013 cp_decl_specifier_seq type_specifiers;
42015 /* First, try to parse as an initialized declaration. See
42016 cp_parser_condition, from whence the bulk of this is copied. */
42018 cp_parser_parse_tentatively (parser);
42019 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
42020 /*is_declaration=*/true,
42021 /*is_trailing_return=*/false,
42022 &type_specifiers);
42023 if (cp_parser_parse_definitely (parser))
42025 /* If parsing a type specifier seq succeeded, then this
42026 MUST be a initialized declaration. */
42027 tree asm_specification, attributes;
42028 cp_declarator *declarator;
42030 declarator = cp_parser_declarator (parser,
42031 CP_PARSER_DECLARATOR_NAMED,
42032 CP_PARSER_FLAGS_NONE,
42033 /*ctor_dtor_or_conv_p=*/NULL,
42034 /*parenthesized_p=*/NULL,
42035 /*member_p=*/false,
42036 /*friend_p=*/false,
42037 /*static_p=*/false);
42038 attributes = cp_parser_attributes_opt (parser);
42039 asm_specification = cp_parser_asm_specification_opt (parser);
42041 if (declarator == cp_error_declarator)
42042 cp_parser_skip_to_end_of_statement (parser);
42044 else
42046 tree pushed_scope, auto_node;
42048 decl = start_decl (declarator, &type_specifiers,
42049 SD_INITIALIZED, attributes,
42050 /*prefix_attributes=*/NULL_TREE,
42051 &pushed_scope);
42053 auto_node = type_uses_auto (TREE_TYPE (decl));
42054 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
42056 if (cp_lexer_next_token_is (parser->lexer,
42057 CPP_OPEN_PAREN))
42058 error ("parenthesized initialization is not allowed in "
42059 "OpenMP %<for%> loop");
42060 else
42061 /* Trigger an error. */
42062 cp_parser_require (parser, CPP_EQ, RT_EQ);
42064 init = error_mark_node;
42065 cp_parser_skip_to_end_of_statement (parser);
42067 else if (CLASS_TYPE_P (TREE_TYPE (decl))
42068 || type_dependent_expression_p (decl)
42069 || auto_node)
42071 bool is_direct_init, is_non_constant_init;
42073 init = cp_parser_initializer (parser,
42074 &is_direct_init,
42075 &is_non_constant_init);
42077 if (auto_node)
42079 TREE_TYPE (decl)
42080 = do_auto_deduction (TREE_TYPE (decl), init,
42081 auto_node);
42083 if (!CLASS_TYPE_P (TREE_TYPE (decl))
42084 && !type_dependent_expression_p (decl))
42085 goto non_class;
42088 cp_finish_decl (decl, init, !is_non_constant_init,
42089 asm_specification,
42090 LOOKUP_ONLYCONVERTING);
42091 orig_init = init;
42092 if (CLASS_TYPE_P (TREE_TYPE (decl)))
42094 vec_safe_push (for_block, this_pre_body);
42095 init = NULL_TREE;
42097 else
42099 init = pop_stmt_list (this_pre_body);
42100 if (init && TREE_CODE (init) == STATEMENT_LIST)
42102 tree_stmt_iterator i = tsi_start (init);
42103 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
42104 while (!tsi_end_p (i))
42106 tree t = tsi_stmt (i);
42107 if (TREE_CODE (t) == DECL_EXPR
42108 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
42110 tsi_delink (&i);
42111 vec_safe_push (for_block, t);
42112 continue;
42114 break;
42116 if (tsi_one_before_end_p (i))
42118 tree t = tsi_stmt (i);
42119 tsi_delink (&i);
42120 free_stmt_list (init);
42121 init = t;
42125 this_pre_body = NULL_TREE;
42127 else
42129 /* Consume '='. */
42130 cp_lexer_consume_token (parser->lexer);
42131 init = cp_parser_assignment_expression (parser);
42133 non_class:
42134 if (TYPE_REF_P (TREE_TYPE (decl)))
42135 init = error_mark_node;
42136 else
42137 cp_finish_decl (decl, NULL_TREE,
42138 /*init_const_expr_p=*/false,
42139 asm_specification,
42140 LOOKUP_ONLYCONVERTING);
42143 if (pushed_scope)
42144 pop_scope (pushed_scope);
42147 else
42149 cp_id_kind idk;
42150 /* If parsing a type specifier sequence failed, then
42151 this MUST be a simple expression. */
42152 cp_parser_parse_tentatively (parser);
42153 decl = cp_parser_primary_expression (parser, false, false,
42154 false, &idk);
42155 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
42156 if (!cp_parser_error_occurred (parser)
42157 && decl
42158 && (TREE_CODE (decl) == COMPONENT_REF
42159 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
42161 cp_parser_abort_tentative_parse (parser);
42162 cp_parser_parse_tentatively (parser);
42163 cp_token *token = cp_lexer_peek_token (parser->lexer);
42164 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
42165 /*check_dependency_p=*/true,
42166 /*template_p=*/NULL,
42167 /*declarator_p=*/false,
42168 /*optional_p=*/false);
42169 if (name != error_mark_node
42170 && last_tok == cp_lexer_peek_token (parser->lexer))
42172 decl = cp_parser_lookup_name_simple (parser, name,
42173 token->location);
42174 if (TREE_CODE (decl) == FIELD_DECL)
42175 add_private_clause = omp_privatize_field (decl, false);
42177 cp_parser_abort_tentative_parse (parser);
42178 cp_parser_parse_tentatively (parser);
42179 decl = cp_parser_primary_expression (parser, false, false,
42180 false, &idk);
42182 if (!cp_parser_error_occurred (parser)
42183 && decl
42184 && DECL_P (decl)
42185 && CLASS_TYPE_P (TREE_TYPE (decl)))
42187 tree rhs;
42189 cp_parser_parse_definitely (parser);
42190 cp_parser_require (parser, CPP_EQ, RT_EQ);
42191 rhs = cp_parser_assignment_expression (parser);
42192 orig_init = rhs;
42193 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
42194 decl, NOP_EXPR,
42195 rhs, NULL_TREE,
42196 tf_warning_or_error));
42197 if (!add_private_clause)
42198 add_private_clause = decl;
42200 else
42202 decl = NULL;
42203 cp_parser_abort_tentative_parse (parser);
42204 init = cp_parser_expression (parser);
42205 if (init)
42207 if (TREE_CODE (init) == MODIFY_EXPR
42208 || TREE_CODE (init) == MODOP_EXPR)
42209 real_decl = TREE_OPERAND (init, 0);
42213 return add_private_clause;
42216 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
42218 void
42219 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
42220 tree &decl, tree &orig_decl, tree &init,
42221 tree &orig_init, tree &cond, tree &incr)
42223 tree begin, end, range_temp_decl = NULL_TREE;
42224 tree iter_type, begin_expr, end_expr;
42226 if (processing_template_decl)
42228 if (check_for_bare_parameter_packs (init))
42229 init = error_mark_node;
42230 if (!type_dependent_expression_p (init)
42231 /* do_auto_deduction doesn't mess with template init-lists. */
42232 && !BRACE_ENCLOSED_INITIALIZER_P (init))
42234 tree d = decl;
42235 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
42237 tree v = DECL_VALUE_EXPR (decl);
42238 if (TREE_CODE (v) == ARRAY_REF
42239 && VAR_P (TREE_OPERAND (v, 0))
42240 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
42241 d = TREE_OPERAND (v, 0);
42243 do_range_for_auto_deduction (d, init);
42245 cond = global_namespace;
42246 incr = NULL_TREE;
42247 orig_init = init;
42248 if (this_pre_body)
42249 this_pre_body = pop_stmt_list (this_pre_body);
42250 return;
42253 init = mark_lvalue_use (init);
42255 if (decl == error_mark_node || init == error_mark_node)
42256 /* If an error happened previously do nothing or else a lot of
42257 unhelpful errors would be issued. */
42258 begin_expr = end_expr = iter_type = error_mark_node;
42259 else
42261 tree range_temp;
42263 if (VAR_P (init)
42264 && array_of_runtime_bound_p (TREE_TYPE (init)))
42265 /* Can't bind a reference to an array of runtime bound. */
42266 range_temp = init;
42267 else
42269 range_temp = build_range_temp (init);
42270 DECL_NAME (range_temp) = NULL_TREE;
42271 pushdecl (range_temp);
42272 cp_finish_decl (range_temp, init,
42273 /*is_constant_init*/false, NULL_TREE,
42274 LOOKUP_ONLYCONVERTING);
42275 range_temp_decl = range_temp;
42276 range_temp = convert_from_reference (range_temp);
42278 iter_type = cp_parser_perform_range_for_lookup (range_temp,
42279 &begin_expr, &end_expr);
42282 tree end_iter_type = iter_type;
42283 if (cxx_dialect >= cxx17)
42284 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
42285 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
42286 TREE_USED (end) = 1;
42287 DECL_ARTIFICIAL (end) = 1;
42288 pushdecl (end);
42289 cp_finish_decl (end, end_expr,
42290 /*is_constant_init*/false, NULL_TREE,
42291 LOOKUP_ONLYCONVERTING);
42293 /* The new for initialization statement. */
42294 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
42295 TREE_USED (begin) = 1;
42296 DECL_ARTIFICIAL (begin) = 1;
42297 pushdecl (begin);
42298 orig_init = init;
42299 if (CLASS_TYPE_P (iter_type))
42300 init = NULL_TREE;
42301 else
42303 init = begin_expr;
42304 begin_expr = NULL_TREE;
42306 cp_finish_decl (begin, begin_expr,
42307 /*is_constant_init*/false, NULL_TREE,
42308 LOOKUP_ONLYCONVERTING);
42310 /* The new for condition. */
42311 if (CLASS_TYPE_P (iter_type))
42312 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
42313 else
42314 cond = build_x_binary_op (input_location, NE_EXPR,
42315 begin, ERROR_MARK,
42316 end, ERROR_MARK,
42317 NULL_TREE, NULL, tf_warning_or_error);
42319 /* The new increment expression. */
42320 if (CLASS_TYPE_P (iter_type))
42321 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
42322 else
42323 incr = finish_unary_op_expr (input_location,
42324 PREINCREMENT_EXPR, begin,
42325 tf_warning_or_error);
42327 orig_decl = decl;
42328 decl = begin;
42329 if (for_block)
42331 vec_safe_push (for_block, this_pre_body);
42332 this_pre_body = NULL_TREE;
42335 tree decomp_first_name = NULL_TREE;
42336 unsigned decomp_cnt = 0;
42337 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
42339 tree v = DECL_VALUE_EXPR (orig_decl);
42340 if (TREE_CODE (v) == ARRAY_REF
42341 && VAR_P (TREE_OPERAND (v, 0))
42342 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
42344 tree d = orig_decl;
42345 orig_decl = TREE_OPERAND (v, 0);
42346 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
42347 decomp_first_name = d;
42351 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
42352 if (auto_node)
42354 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42355 NULL_TREE, tf_none);
42356 if (!error_operand_p (t))
42357 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
42358 t, auto_node);
42361 tree v = make_tree_vec (decomp_cnt + 3);
42362 TREE_VEC_ELT (v, 0) = range_temp_decl;
42363 TREE_VEC_ELT (v, 1) = end;
42364 TREE_VEC_ELT (v, 2) = orig_decl;
42365 for (unsigned i = 0; i < decomp_cnt; i++)
42367 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
42368 decomp_first_name = DECL_CHAIN (decomp_first_name);
42370 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
42373 /* Helper for cp_parser_omp_for_loop, finalize part of range for
42374 inside of the collapsed body. */
42376 void
42377 cp_finish_omp_range_for (tree orig, tree begin)
42379 gcc_assert (TREE_CODE (orig) == TREE_LIST
42380 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
42381 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
42382 tree decomp_first_name = NULL_TREE;
42383 unsigned int decomp_cnt = 0;
42385 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42387 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
42388 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
42389 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
42392 /* The declaration is initialized with *__begin inside the loop body. */
42393 cp_finish_decl (decl,
42394 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42395 NULL_TREE, tf_warning_or_error),
42396 /*is_constant_init*/false, NULL_TREE,
42397 LOOKUP_ONLYCONVERTING);
42398 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42399 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
42402 /* Return true if next tokens contain a standard attribute that contains
42403 omp::directive (DIRECTIVE). */
42405 static bool
42406 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
42407 bool tentative)
42409 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
42410 if (n < 10)
42411 return false;
42412 for (i = 5; i < n - 4; i++)
42413 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
42414 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
42415 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
42417 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
42418 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
42419 if (strcmp (IDENTIFIER_POINTER (first), "directive"))
42420 continue;
42421 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
42422 break;
42424 if (i == n - 4)
42425 return false;
42426 cp_parser_parse_tentatively (parser);
42427 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
42428 location_t last_loc
42429 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
42430 location_t middle_loc = UNKNOWN_LOCATION;
42431 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
42432 int cnt = 0;
42433 bool seen = false;
42434 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
42435 if (get_attribute_namespace (attr) == omp_identifier
42436 && is_attribute_p ("directive", get_attribute_name (attr)))
42438 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
42440 tree d = TREE_VALUE (a);
42441 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
42442 cp_token *first = DEFPARSE_TOKENS (d)->first;
42443 cnt++;
42444 if (first->type == CPP_NAME
42445 && strcmp (IDENTIFIER_POINTER (first->u.value),
42446 directive) == 0)
42448 seen = true;
42449 if (middle_loc == UNKNOWN_LOCATION)
42450 middle_loc = first->location;
42454 if (!seen || tentative)
42456 cp_parser_abort_tentative_parse (parser);
42457 return seen;
42459 if (cnt != 1 || TREE_CHAIN (std_attrs))
42461 error_at (make_location (first_loc, last_loc, middle_loc),
42462 "%<[[omp::directive(%s)]]%> must be the only specified "
42463 "attribute on a statement", directive);
42464 cp_parser_abort_tentative_parse (parser);
42465 return false;
42467 if (!cp_parser_parse_definitely (parser))
42468 return false;
42469 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
42470 return true;
42473 /* Parse an OpenMP structured block sequence. KIND is the corresponding
42474 separating directive. */
42476 static tree
42477 cp_parser_omp_structured_block_sequence (cp_parser *parser,
42478 enum pragma_kind kind)
42480 tree stmt = begin_omp_structured_block ();
42481 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42483 cp_parser_statement (parser, NULL_TREE, false, NULL);
42484 while (true)
42486 cp_token *token = cp_lexer_peek_token (parser->lexer);
42488 if (token->type == CPP_CLOSE_BRACE
42489 || token->type == CPP_EOF
42490 || token->type == CPP_PRAGMA_EOL
42491 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
42492 || (kind != PRAGMA_NONE
42493 && cp_parser_pragma_kind (token) == kind))
42494 break;
42496 if (kind != PRAGMA_NONE
42497 && cp_parser_omp_section_scan (parser,
42498 kind == PRAGMA_OMP_SCAN
42499 ? "scan" : "section", false))
42500 break;
42502 cp_parser_statement (parser, NULL_TREE, false, NULL);
42505 cp_parser_end_omp_structured_block (parser, save);
42506 return finish_omp_structured_block (stmt);
42510 /* OpenMP 5.0:
42512 scan-loop-body:
42513 { structured-block scan-directive structured-block } */
42515 static void
42516 cp_parser_omp_scan_loop_body (cp_parser *parser)
42518 tree substmt, clauses = NULL_TREE;
42520 matching_braces braces;
42521 if (!braces.require_open (parser))
42522 return;
42524 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
42525 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
42526 add_stmt (substmt);
42528 cp_token *tok = cp_lexer_peek_token (parser->lexer);
42529 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
42531 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
42533 cp_lexer_consume_token (parser->lexer);
42535 if (parser->lexer->in_omp_attribute_pragma
42536 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42537 cp_lexer_consume_token (parser->lexer);
42539 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42541 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42542 const char *p = IDENTIFIER_POINTER (id);
42543 if (strcmp (p, "inclusive") == 0)
42544 clause = OMP_CLAUSE_INCLUSIVE;
42545 else if (strcmp (p, "exclusive") == 0)
42546 clause = OMP_CLAUSE_EXCLUSIVE;
42548 if (clause != OMP_CLAUSE_ERROR)
42550 cp_lexer_consume_token (parser->lexer);
42551 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
42553 else
42554 cp_parser_error (parser, "expected %<inclusive%> or "
42555 "%<exclusive%> clause");
42557 cp_parser_require_pragma_eol (parser, tok);
42559 else
42560 error ("expected %<#pragma omp scan%>");
42562 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42563 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
42564 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
42565 clauses);
42566 add_stmt (substmt);
42568 braces.require_close (parser);
42571 /* Parse the restricted form of the for statement allowed by OpenMP. */
42573 static tree
42574 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
42575 tree *cclauses, bool *if_p)
42577 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
42578 tree orig_decl;
42579 tree real_decl, initv, condv, incrv, declv, orig_declv;
42580 tree this_pre_body, cl, ordered_cl = NULL_TREE;
42581 location_t loc_first;
42582 bool collapse_err = false;
42583 int i, collapse = 1, ordered = 0, count, nbraces = 0;
42584 releasing_vec for_block;
42585 auto_vec<tree, 4> orig_inits;
42586 bool tiling = false;
42587 bool inscan = false;
42589 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
42590 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
42591 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
42592 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
42594 tiling = true;
42595 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
42597 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
42598 && OMP_CLAUSE_ORDERED_EXPR (cl))
42600 ordered_cl = cl;
42601 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
42603 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
42604 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
42605 && (code == OMP_SIMD || code == OMP_FOR))
42606 inscan = true;
42608 if (ordered && ordered < collapse)
42610 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42611 "%<ordered%> clause parameter is less than %<collapse%>");
42612 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
42613 = build_int_cst (NULL_TREE, collapse);
42614 ordered = collapse;
42616 if (ordered)
42618 for (tree *pc = &clauses; *pc; )
42619 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
42621 error_at (OMP_CLAUSE_LOCATION (*pc),
42622 "%<linear%> clause may not be specified together "
42623 "with %<ordered%> clause with a parameter");
42624 *pc = OMP_CLAUSE_CHAIN (*pc);
42626 else
42627 pc = &OMP_CLAUSE_CHAIN (*pc);
42630 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
42631 count = ordered ? ordered : collapse;
42633 declv = make_tree_vec (count);
42634 initv = make_tree_vec (count);
42635 condv = make_tree_vec (count);
42636 incrv = make_tree_vec (count);
42637 orig_declv = NULL_TREE;
42639 loc_first = cp_lexer_peek_token (parser->lexer)->location;
42641 for (i = 0; i < count; i++)
42643 int bracecount = 0;
42644 tree add_private_clause = NULL_TREE;
42645 location_t loc;
42647 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42649 if (!collapse_err)
42650 cp_parser_error (parser, "for statement expected");
42651 return NULL;
42653 loc = cp_lexer_consume_token (parser->lexer)->location;
42655 /* Don't create location wrapper nodes within an OpenMP "for"
42656 statement. */
42657 auto_suppress_location_wrappers sentinel;
42659 matching_parens parens;
42660 if (!parens.require_open (parser))
42661 return NULL;
42663 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
42664 this_pre_body = push_stmt_list ();
42666 if (code != OACC_LOOP && cxx_dialect >= cxx11)
42668 /* Save tokens so that we can put them back. */
42669 cp_lexer_save_tokens (parser->lexer);
42671 /* Look for ':' that is not nested in () or {}. */
42672 bool is_range_for
42673 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
42674 /*recovering=*/false,
42675 CPP_COLON,
42676 /*consume_paren=*/
42677 false) == -1);
42679 /* Roll back the tokens we skipped. */
42680 cp_lexer_rollback_tokens (parser->lexer);
42682 if (is_range_for)
42684 bool saved_colon_corrects_to_scope_p
42685 = parser->colon_corrects_to_scope_p;
42687 /* A colon is used in range-based for. */
42688 parser->colon_corrects_to_scope_p = false;
42690 /* Parse the declaration. */
42691 cp_parser_simple_declaration (parser,
42692 /*function_definition_allowed_p=*/
42693 false, &decl);
42694 parser->colon_corrects_to_scope_p
42695 = saved_colon_corrects_to_scope_p;
42697 cp_parser_require (parser, CPP_COLON, RT_COLON);
42699 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
42700 false, 0, true);
42702 cp_convert_omp_range_for (this_pre_body, for_block, decl,
42703 orig_decl, init, orig_init,
42704 cond, incr);
42705 if (this_pre_body)
42707 if (pre_body)
42709 tree t = pre_body;
42710 pre_body = push_stmt_list ();
42711 add_stmt (t);
42712 add_stmt (this_pre_body);
42713 pre_body = pop_stmt_list (pre_body);
42715 else
42716 pre_body = this_pre_body;
42719 if (ordered_cl)
42720 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42721 "%<ordered%> clause with parameter on "
42722 "range-based %<for%> loop");
42724 goto parse_close_paren;
42728 add_private_clause
42729 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
42730 init, orig_init, decl, real_decl);
42732 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42733 if (this_pre_body)
42735 this_pre_body = pop_stmt_list (this_pre_body);
42736 if (pre_body)
42738 tree t = pre_body;
42739 pre_body = push_stmt_list ();
42740 add_stmt (t);
42741 add_stmt (this_pre_body);
42742 pre_body = pop_stmt_list (pre_body);
42744 else
42745 pre_body = this_pre_body;
42748 if (decl)
42749 real_decl = decl;
42750 if (cclauses != NULL
42751 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
42752 && real_decl != NULL_TREE
42753 && code != OMP_LOOP)
42755 tree *c;
42756 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
42757 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
42758 && OMP_CLAUSE_DECL (*c) == real_decl)
42760 error_at (loc, "iteration variable %qD"
42761 " should not be firstprivate", real_decl);
42762 *c = OMP_CLAUSE_CHAIN (*c);
42764 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
42765 && OMP_CLAUSE_DECL (*c) == real_decl)
42767 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
42768 tree l = *c;
42769 *c = OMP_CLAUSE_CHAIN (*c);
42770 if (code == OMP_SIMD)
42772 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
42773 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
42775 else
42777 OMP_CLAUSE_CHAIN (l) = clauses;
42778 clauses = l;
42780 add_private_clause = NULL_TREE;
42782 else
42784 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
42785 && OMP_CLAUSE_DECL (*c) == real_decl)
42786 add_private_clause = NULL_TREE;
42787 c = &OMP_CLAUSE_CHAIN (*c);
42791 if (add_private_clause)
42793 tree c;
42794 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
42796 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
42797 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
42798 && OMP_CLAUSE_DECL (c) == decl)
42799 break;
42800 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
42801 && OMP_CLAUSE_DECL (c) == decl)
42802 error_at (loc, "iteration variable %qD "
42803 "should not be firstprivate",
42804 decl);
42805 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
42806 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
42807 && OMP_CLAUSE_DECL (c) == decl)
42808 error_at (loc, "iteration variable %qD should not be reduction",
42809 decl);
42811 if (c == NULL)
42813 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
42814 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
42815 else if (code != OMP_SIMD)
42816 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
42817 else
42818 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
42819 OMP_CLAUSE_DECL (c) = add_private_clause;
42820 c = finish_omp_clauses (c, C_ORT_OMP);
42821 if (c)
42823 OMP_CLAUSE_CHAIN (c) = clauses;
42824 clauses = c;
42825 /* For linear, signal that we need to fill up
42826 the so far unknown linear step. */
42827 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
42828 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
42833 cond = NULL;
42834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
42835 cond = cp_parser_omp_for_cond (parser, decl, code);
42836 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42838 incr = NULL;
42839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
42841 /* If decl is an iterator, preserve the operator on decl
42842 until finish_omp_for. */
42843 if (real_decl
42844 && ((processing_template_decl
42845 && (TREE_TYPE (real_decl) == NULL_TREE
42846 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
42847 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
42848 incr = cp_parser_omp_for_incr (parser, real_decl);
42849 else
42850 incr = cp_parser_expression (parser);
42851 protected_set_expr_location_if_unset (incr, input_location);
42854 parse_close_paren:
42855 if (!parens.require_close (parser))
42856 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42857 /*or_comma=*/false,
42858 /*consume_paren=*/true);
42860 TREE_VEC_ELT (declv, i) = decl;
42861 TREE_VEC_ELT (initv, i) = init;
42862 TREE_VEC_ELT (condv, i) = cond;
42863 TREE_VEC_ELT (incrv, i) = incr;
42864 if (orig_init)
42866 orig_inits.safe_grow_cleared (i + 1, true);
42867 orig_inits[i] = orig_init;
42869 if (orig_decl)
42871 if (!orig_declv)
42872 orig_declv = copy_node (declv);
42873 TREE_VEC_ELT (orig_declv, i) = orig_decl;
42875 else if (orig_declv)
42876 TREE_VEC_ELT (orig_declv, i) = decl;
42878 if (i == count - 1)
42879 break;
42881 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
42882 in between the collapsed for loops to be still considered perfectly
42883 nested. Hopefully the final version clarifies this.
42884 For now handle (multiple) {'s and empty statements. */
42885 cp_parser_parse_tentatively (parser);
42886 for (;;)
42888 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42889 break;
42890 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42892 cp_lexer_consume_token (parser->lexer);
42893 bracecount++;
42895 else if (bracecount
42896 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42897 cp_lexer_consume_token (parser->lexer);
42898 else
42900 loc = cp_lexer_peek_token (parser->lexer)->location;
42901 error_at (loc, "not enough for loops to collapse");
42902 collapse_err = true;
42903 cp_parser_abort_tentative_parse (parser);
42904 declv = NULL_TREE;
42905 break;
42909 if (declv)
42911 cp_parser_parse_definitely (parser);
42912 nbraces += bracecount;
42916 if (nbraces)
42917 if_p = NULL;
42919 /* Note that we saved the original contents of this flag when we entered
42920 the structured block, and so we don't need to re-save it here. */
42921 parser->in_statement = IN_OMP_FOR;
42923 /* Note that the grammar doesn't call for a structured block here,
42924 though the loop as a whole is a structured block. */
42925 if (orig_declv)
42927 body = begin_omp_structured_block ();
42928 for (i = 0; i < count; i++)
42929 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
42930 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
42931 TREE_VEC_ELT (declv, i));
42933 else
42934 body = push_stmt_list ();
42935 if (inscan)
42936 cp_parser_omp_scan_loop_body (parser);
42937 else
42938 cp_parser_statement (parser, NULL_TREE, false, if_p);
42939 if (orig_declv)
42940 body = finish_omp_structured_block (body);
42941 else
42942 body = pop_stmt_list (body);
42944 if (declv == NULL_TREE)
42945 ret = NULL_TREE;
42946 else
42947 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
42948 incrv, body, pre_body, &orig_inits, clauses);
42950 while (nbraces)
42952 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42954 cp_lexer_consume_token (parser->lexer);
42955 nbraces--;
42957 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42958 cp_lexer_consume_token (parser->lexer);
42959 else
42961 if (!collapse_err)
42963 error_at (cp_lexer_peek_token (parser->lexer)->location,
42964 "collapsed loops not perfectly nested");
42966 collapse_err = true;
42967 cp_parser_statement_seq_opt (parser, NULL);
42968 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
42969 break;
42973 while (!for_block->is_empty ())
42975 tree t = for_block->pop ();
42976 if (TREE_CODE (t) == STATEMENT_LIST)
42977 add_stmt (pop_stmt_list (t));
42978 else
42979 add_stmt (t);
42982 return ret;
42985 /* Helper function for OpenMP parsing, split clauses and call
42986 finish_omp_clauses on each of the set of clauses afterwards. */
42988 static void
42989 cp_omp_split_clauses (location_t loc, enum tree_code code,
42990 omp_clause_mask mask, tree clauses, tree *cclauses)
42992 int i;
42993 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
42994 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
42995 if (cclauses[i])
42996 cclauses[i] = finish_omp_clauses (cclauses[i],
42997 i == C_OMP_CLAUSE_SPLIT_TARGET
42998 ? C_ORT_OMP_TARGET : C_ORT_OMP);
43001 /* OpenMP 5.0:
43002 #pragma omp loop loop-clause[optseq] new-line
43003 for-loop */
43005 #define OMP_LOOP_CLAUSE_MASK \
43006 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
43011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43013 static tree
43014 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
43015 char *p_name, omp_clause_mask mask, tree *cclauses,
43016 bool *if_p)
43018 tree clauses, sb, ret;
43019 unsigned int save;
43020 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43022 strcat (p_name, " loop");
43023 mask |= OMP_LOOP_CLAUSE_MASK;
43025 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43026 cclauses == NULL);
43027 if (cclauses)
43029 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
43030 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
43033 keep_next_level (true);
43034 sb = begin_omp_structured_block ();
43035 save = cp_parser_begin_omp_structured_block (parser);
43037 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
43039 cp_parser_end_omp_structured_block (parser, save);
43040 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43042 return ret;
43045 /* OpenMP 4.0:
43046 #pragma omp simd simd-clause[optseq] new-line
43047 for-loop */
43049 #define OMP_SIMD_CLAUSE_MASK \
43050 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
43051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
43052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
43053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
43054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
43060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43062 static tree
43063 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
43064 char *p_name, omp_clause_mask mask, tree *cclauses,
43065 bool *if_p)
43067 tree clauses, sb, ret;
43068 unsigned int save;
43069 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43071 strcat (p_name, " simd");
43072 mask |= OMP_SIMD_CLAUSE_MASK;
43074 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43075 cclauses == NULL);
43076 if (cclauses)
43078 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
43079 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
43080 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
43081 OMP_CLAUSE_ORDERED);
43082 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
43084 error_at (OMP_CLAUSE_LOCATION (c),
43085 "%<ordered%> clause with parameter may not be specified "
43086 "on %qs construct", p_name);
43087 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
43091 keep_next_level (true);
43092 sb = begin_omp_structured_block ();
43093 save = cp_parser_begin_omp_structured_block (parser);
43095 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
43097 cp_parser_end_omp_structured_block (parser, save);
43098 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43100 return ret;
43103 /* OpenMP 2.5:
43104 #pragma omp for for-clause[optseq] new-line
43105 for-loop
43107 OpenMP 4.0:
43108 #pragma omp for simd for-simd-clause[optseq] new-line
43109 for-loop */
43111 #define OMP_FOR_CLAUSE_MASK \
43112 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
43116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
43118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
43119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
43120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43124 static tree
43125 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
43126 char *p_name, omp_clause_mask mask, tree *cclauses,
43127 bool *if_p)
43129 tree clauses, sb, ret;
43130 unsigned int save;
43131 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43133 strcat (p_name, " for");
43134 mask |= OMP_FOR_CLAUSE_MASK;
43135 /* parallel for{, simd} disallows nowait clause, but for
43136 target {teams distribute ,}parallel for{, simd} it should be accepted. */
43137 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
43138 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
43139 /* Composite distribute parallel for{, simd} disallows ordered clause. */
43140 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43141 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
43143 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43145 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43146 const char *p = IDENTIFIER_POINTER (id);
43148 if (strcmp (p, "simd") == 0)
43150 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43151 if (cclauses == NULL)
43152 cclauses = cclauses_buf;
43154 cp_lexer_consume_token (parser->lexer);
43155 if (!flag_openmp) /* flag_openmp_simd */
43156 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43157 cclauses, if_p);
43158 sb = begin_omp_structured_block ();
43159 save = cp_parser_begin_omp_structured_block (parser);
43160 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43161 cclauses, if_p);
43162 cp_parser_end_omp_structured_block (parser, save);
43163 tree body = finish_omp_structured_block (sb);
43164 if (ret == NULL)
43165 return ret;
43166 ret = make_node (OMP_FOR);
43167 TREE_TYPE (ret) = void_type_node;
43168 OMP_FOR_BODY (ret) = body;
43169 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43170 SET_EXPR_LOCATION (ret, loc);
43171 add_stmt (ret);
43172 return ret;
43175 if (!flag_openmp) /* flag_openmp_simd */
43177 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43178 return NULL_TREE;
43181 /* Composite distribute parallel for disallows linear clause. */
43182 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43183 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
43185 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43186 cclauses == NULL);
43187 if (cclauses)
43189 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
43190 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43193 keep_next_level (true);
43194 sb = begin_omp_structured_block ();
43195 save = cp_parser_begin_omp_structured_block (parser);
43197 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
43199 cp_parser_end_omp_structured_block (parser, save);
43200 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43202 return ret;
43205 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
43206 omp_clause_mask, tree *, bool *);
43208 /* OpenMP 2.5:
43209 # pragma omp master new-line
43210 structured-block */
43212 static tree
43213 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
43214 char *p_name, omp_clause_mask mask, tree *cclauses,
43215 bool *if_p)
43217 tree clauses, sb, ret;
43218 unsigned int save;
43219 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43221 strcat (p_name, " master");
43223 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43225 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43226 const char *p = IDENTIFIER_POINTER (id);
43228 if (strcmp (p, "taskloop") == 0)
43230 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43231 if (cclauses == NULL)
43232 cclauses = cclauses_buf;
43234 cp_lexer_consume_token (parser->lexer);
43235 if (!flag_openmp) /* flag_openmp_simd */
43236 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43237 cclauses, if_p);
43238 sb = begin_omp_structured_block ();
43239 save = cp_parser_begin_omp_structured_block (parser);
43240 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43241 cclauses, if_p);
43242 cp_parser_end_omp_structured_block (parser, save);
43243 tree body = finish_omp_structured_block (sb);
43244 if (ret == NULL)
43245 return ret;
43246 ret = c_finish_omp_master (loc, body);
43247 OMP_MASTER_COMBINED (ret) = 1;
43248 return ret;
43251 if (!flag_openmp) /* flag_openmp_simd */
43253 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43254 return NULL_TREE;
43257 if (cclauses)
43259 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43260 false);
43261 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
43263 else
43264 cp_parser_require_pragma_eol (parser, pragma_tok);
43266 return c_finish_omp_master (loc,
43267 cp_parser_omp_structured_block (parser, if_p));
43270 /* OpenMP 5.1:
43271 # pragma omp masked masked-clauses new-line
43272 structured-block */
43274 #define OMP_MASKED_CLAUSE_MASK \
43275 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
43277 static tree
43278 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
43279 char *p_name, omp_clause_mask mask, tree *cclauses,
43280 bool *if_p)
43282 tree clauses, sb, ret;
43283 unsigned int save;
43284 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43286 strcat (p_name, " masked");
43287 mask |= OMP_MASKED_CLAUSE_MASK;
43289 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43291 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43292 const char *p = IDENTIFIER_POINTER (id);
43294 if (strcmp (p, "taskloop") == 0)
43296 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43297 if (cclauses == NULL)
43298 cclauses = cclauses_buf;
43300 cp_lexer_consume_token (parser->lexer);
43301 if (!flag_openmp) /* flag_openmp_simd */
43302 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43303 cclauses, if_p);
43304 sb = begin_omp_structured_block ();
43305 save = cp_parser_begin_omp_structured_block (parser);
43306 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43307 cclauses, if_p);
43308 cp_parser_end_omp_structured_block (parser, save);
43309 tree body = finish_omp_structured_block (sb);
43310 if (ret == NULL)
43311 return ret;
43312 ret = c_finish_omp_masked (loc, body,
43313 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
43314 OMP_MASKED_COMBINED (ret) = 1;
43315 return ret;
43318 if (!flag_openmp) /* flag_openmp_simd */
43320 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43321 return NULL_TREE;
43324 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43325 cclauses == NULL);
43326 if (cclauses)
43328 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
43329 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
43332 return c_finish_omp_masked (loc,
43333 cp_parser_omp_structured_block (parser, if_p),
43334 clauses);
43337 /* OpenMP 2.5:
43338 # pragma omp ordered new-line
43339 structured-block
43341 OpenMP 4.5:
43342 # pragma omp ordered ordered-clauses new-line
43343 structured-block */
43345 #define OMP_ORDERED_CLAUSE_MASK \
43346 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
43347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
43349 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
43350 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
43352 static bool
43353 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
43354 enum pragma_context context, bool *if_p)
43356 location_t loc = pragma_tok->location;
43357 int n = 1;
43359 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
43360 if (parser->lexer->in_omp_attribute_pragma
43361 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43362 n = 2;
43364 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
43366 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
43367 const char *p = IDENTIFIER_POINTER (id);
43369 if (strcmp (p, "depend") == 0)
43371 if (!flag_openmp) /* flag_openmp_simd */
43373 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43374 return false;
43376 if (context == pragma_stmt)
43378 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
43379 "%<depend%> clause may only be used in compound "
43380 "statements");
43381 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43382 return true;
43384 tree clauses
43385 = cp_parser_omp_all_clauses (parser,
43386 OMP_ORDERED_DEPEND_CLAUSE_MASK,
43387 "#pragma omp ordered", pragma_tok);
43388 c_finish_omp_ordered (loc, clauses, NULL_TREE);
43389 return false;
43393 tree clauses
43394 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
43395 "#pragma omp ordered", pragma_tok);
43397 if (!flag_openmp /* flag_openmp_simd */
43398 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
43399 return false;
43401 c_finish_omp_ordered (loc, clauses,
43402 cp_parser_omp_structured_block (parser, if_p));
43403 return true;
43406 /* OpenMP 2.5:
43408 section-scope:
43409 { section-sequence }
43411 section-sequence:
43412 section-directive[opt] structured-block
43413 section-sequence section-directive structured-block */
43415 static tree
43416 cp_parser_omp_sections_scope (cp_parser *parser)
43418 tree stmt, substmt;
43419 bool error_suppress = false;
43420 cp_token *tok;
43422 matching_braces braces;
43423 if (!braces.require_open (parser))
43424 return NULL_TREE;
43426 stmt = push_stmt_list ();
43428 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
43429 != PRAGMA_OMP_SECTION
43430 && !cp_parser_omp_section_scan (parser, "section", true))
43432 substmt = cp_parser_omp_structured_block_sequence (parser,
43433 PRAGMA_OMP_SECTION);
43434 substmt = build1 (OMP_SECTION, void_type_node, substmt);
43435 add_stmt (substmt);
43438 while (1)
43440 tok = cp_lexer_peek_token (parser->lexer);
43441 if (tok->type == CPP_CLOSE_BRACE)
43442 break;
43443 if (tok->type == CPP_EOF)
43444 break;
43446 if (cp_parser_omp_section_scan (parser, "section", false))
43447 tok = cp_lexer_peek_token (parser->lexer);
43448 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
43450 cp_lexer_consume_token (parser->lexer);
43451 cp_parser_require_pragma_eol (parser, tok);
43452 error_suppress = false;
43454 else if (!error_suppress)
43456 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
43457 error_suppress = true;
43460 substmt = cp_parser_omp_structured_block_sequence (parser,
43461 PRAGMA_OMP_SECTION);
43462 substmt = build1 (OMP_SECTION, void_type_node, substmt);
43463 add_stmt (substmt);
43465 braces.require_close (parser);
43467 substmt = pop_stmt_list (stmt);
43469 stmt = make_node (OMP_SECTIONS);
43470 TREE_TYPE (stmt) = void_type_node;
43471 OMP_SECTIONS_BODY (stmt) = substmt;
43473 add_stmt (stmt);
43474 return stmt;
43477 /* OpenMP 2.5:
43478 # pragma omp sections sections-clause[optseq] newline
43479 sections-scope */
43481 #define OMP_SECTIONS_CLAUSE_MASK \
43482 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43489 static tree
43490 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
43491 char *p_name, omp_clause_mask mask, tree *cclauses)
43493 tree clauses, ret;
43494 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43496 strcat (p_name, " sections");
43497 mask |= OMP_SECTIONS_CLAUSE_MASK;
43498 if (cclauses)
43499 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
43501 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43502 cclauses == NULL);
43503 if (cclauses)
43505 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
43506 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
43509 ret = cp_parser_omp_sections_scope (parser);
43510 if (ret)
43511 OMP_SECTIONS_CLAUSES (ret) = clauses;
43513 return ret;
43516 /* OpenMP 2.5:
43517 # pragma omp parallel parallel-clause[optseq] new-line
43518 structured-block
43519 # pragma omp parallel for parallel-for-clause[optseq] new-line
43520 structured-block
43521 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
43522 structured-block
43524 OpenMP 4.0:
43525 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
43526 structured-block */
43528 #define OMP_PARALLEL_CLAUSE_MASK \
43529 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
43535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
43537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
43540 static tree
43541 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
43542 char *p_name, omp_clause_mask mask, tree *cclauses,
43543 bool *if_p)
43545 tree stmt, clauses, block;
43546 unsigned int save;
43547 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43549 strcat (p_name, " parallel");
43550 mask |= OMP_PARALLEL_CLAUSE_MASK;
43551 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
43552 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
43553 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
43554 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
43556 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43558 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43559 if (cclauses == NULL)
43560 cclauses = cclauses_buf;
43562 cp_lexer_consume_token (parser->lexer);
43563 if (!flag_openmp) /* flag_openmp_simd */
43564 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43565 if_p);
43566 block = begin_omp_parallel ();
43567 save = cp_parser_begin_omp_structured_block (parser);
43568 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43569 if_p);
43570 cp_parser_end_omp_structured_block (parser, save);
43571 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43572 block);
43573 if (ret == NULL_TREE)
43574 return ret;
43575 OMP_PARALLEL_COMBINED (stmt) = 1;
43576 return stmt;
43578 /* When combined with distribute, parallel has to be followed by for.
43579 #pragma omp target parallel is allowed though. */
43580 else if (cclauses
43581 && (mask & (OMP_CLAUSE_MASK_1
43582 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43584 error_at (loc, "expected %<for%> after %qs", p_name);
43585 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43586 return NULL_TREE;
43588 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43590 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43591 const char *p = IDENTIFIER_POINTER (id);
43592 if (cclauses == NULL && strcmp (p, "masked") == 0)
43594 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43595 cclauses = cclauses_buf;
43597 cp_lexer_consume_token (parser->lexer);
43598 if (!flag_openmp) /* flag_openmp_simd */
43599 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43600 cclauses, if_p);
43601 block = begin_omp_parallel ();
43602 save = cp_parser_begin_omp_structured_block (parser);
43603 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43604 cclauses, if_p);
43605 cp_parser_end_omp_structured_block (parser, save);
43606 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43607 block);
43608 if (ret == NULL_TREE)
43609 return ret;
43610 /* masked does have just filter clause, but during gimplification
43611 isn't represented by a gimplification omp context, so for
43612 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
43613 so that
43614 #pragma omp parallel masked
43615 #pragma omp taskloop simd lastprivate (x)
43616 isn't confused with
43617 #pragma omp parallel masked taskloop simd lastprivate (x) */
43618 if (OMP_MASKED_COMBINED (ret))
43619 OMP_PARALLEL_COMBINED (stmt) = 1;
43620 return stmt;
43622 else if (cclauses == NULL && strcmp (p, "master") == 0)
43624 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43625 cclauses = cclauses_buf;
43627 cp_lexer_consume_token (parser->lexer);
43628 if (!flag_openmp) /* flag_openmp_simd */
43629 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43630 cclauses, if_p);
43631 block = begin_omp_parallel ();
43632 save = cp_parser_begin_omp_structured_block (parser);
43633 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43634 cclauses, if_p);
43635 cp_parser_end_omp_structured_block (parser, save);
43636 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43637 block);
43638 if (ret == NULL_TREE)
43639 return ret;
43640 /* master doesn't have any clauses and during gimplification
43641 isn't represented by a gimplification omp context, so for
43642 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
43643 so that
43644 #pragma omp parallel master
43645 #pragma omp taskloop simd lastprivate (x)
43646 isn't confused with
43647 #pragma omp parallel master taskloop simd lastprivate (x) */
43648 if (OMP_MASTER_COMBINED (ret))
43649 OMP_PARALLEL_COMBINED (stmt) = 1;
43650 return stmt;
43652 else if (strcmp (p, "loop") == 0)
43654 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43655 if (cclauses == NULL)
43656 cclauses = cclauses_buf;
43658 cp_lexer_consume_token (parser->lexer);
43659 if (!flag_openmp) /* flag_openmp_simd */
43660 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43661 cclauses, if_p);
43662 block = begin_omp_parallel ();
43663 save = cp_parser_begin_omp_structured_block (parser);
43664 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43665 cclauses, if_p);
43666 cp_parser_end_omp_structured_block (parser, save);
43667 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43668 block);
43669 if (ret == NULL_TREE)
43670 return ret;
43671 OMP_PARALLEL_COMBINED (stmt) = 1;
43672 return stmt;
43674 else if (!flag_openmp) /* flag_openmp_simd */
43676 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43677 return NULL_TREE;
43679 else if (cclauses == NULL && strcmp (p, "sections") == 0)
43681 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43682 cclauses = cclauses_buf;
43684 cp_lexer_consume_token (parser->lexer);
43685 block = begin_omp_parallel ();
43686 save = cp_parser_begin_omp_structured_block (parser);
43687 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
43688 cp_parser_end_omp_structured_block (parser, save);
43689 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43690 block);
43691 OMP_PARALLEL_COMBINED (stmt) = 1;
43692 return stmt;
43695 else if (!flag_openmp) /* flag_openmp_simd */
43697 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43698 return NULL_TREE;
43701 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43702 cclauses == NULL);
43703 if (cclauses)
43705 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
43706 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
43709 block = begin_omp_parallel ();
43710 save = cp_parser_begin_omp_structured_block (parser);
43711 parser->omp_attrs_forbidden_p = true;
43712 cp_parser_statement (parser, NULL_TREE, false, if_p);
43713 cp_parser_end_omp_structured_block (parser, save);
43714 stmt = finish_omp_parallel (clauses, block);
43715 return stmt;
43718 /* OpenMP 2.5:
43719 # pragma omp single single-clause[optseq] new-line
43720 structured-block */
43722 #define OMP_SINGLE_CLAUSE_MASK \
43723 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
43726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43729 static tree
43730 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43732 tree stmt = make_node (OMP_SINGLE);
43733 TREE_TYPE (stmt) = void_type_node;
43734 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43736 OMP_SINGLE_CLAUSES (stmt)
43737 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
43738 "#pragma omp single", pragma_tok);
43739 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43741 return add_stmt (stmt);
43744 /* OpenMP 5.1:
43745 # pragma omp scope scope-clause[optseq] new-line
43746 structured-block */
43748 #define OMP_SCOPE_CLAUSE_MASK \
43749 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43753 static tree
43754 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43756 tree stmt = make_node (OMP_SCOPE);
43757 TREE_TYPE (stmt) = void_type_node;
43758 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43760 OMP_SCOPE_CLAUSES (stmt)
43761 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
43762 "#pragma omp scope", pragma_tok);
43763 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43765 return add_stmt (stmt);
43768 /* OpenMP 3.0:
43769 # pragma omp task task-clause[optseq] new-line
43770 structured-block */
43772 #define OMP_TASK_CLAUSE_MASK \
43773 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
43775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
43780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
43781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
43783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
43785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
43786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
43788 static tree
43789 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43791 tree clauses, block;
43792 unsigned int save;
43794 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
43795 "#pragma omp task", pragma_tok);
43796 block = begin_omp_task ();
43797 save = cp_parser_begin_omp_structured_block (parser);
43798 parser->omp_attrs_forbidden_p = true;
43799 cp_parser_statement (parser, NULL_TREE, false, if_p);
43800 cp_parser_end_omp_structured_block (parser, save);
43801 return finish_omp_task (clauses, block);
43804 /* OpenMP 3.0:
43805 # pragma omp taskwait new-line
43807 OpenMP 5.0:
43808 # pragma omp taskwait taskwait-clause[opt] new-line */
43810 #define OMP_TASKWAIT_CLAUSE_MASK \
43811 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43814 static void
43815 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
43817 tree clauses
43818 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
43819 "#pragma omp taskwait", pragma_tok);
43821 if (clauses)
43823 tree stmt = make_node (OMP_TASK);
43824 TREE_TYPE (stmt) = void_node;
43825 OMP_TASK_CLAUSES (stmt) = clauses;
43826 OMP_TASK_BODY (stmt) = NULL_TREE;
43827 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43828 add_stmt (stmt);
43830 else
43831 finish_omp_taskwait ();
43834 /* OpenMP 3.1:
43835 # pragma omp taskyield new-line */
43837 static void
43838 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
43840 cp_parser_require_pragma_eol (parser, pragma_tok);
43841 finish_omp_taskyield ();
43844 /* OpenMP 4.0:
43845 # pragma omp taskgroup new-line
43846 structured-block
43848 OpenMP 5.0:
43849 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
43851 #define OMP_TASKGROUP_CLAUSE_MASK \
43852 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
43855 static tree
43856 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43858 tree clauses
43859 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
43860 "#pragma omp taskgroup", pragma_tok);
43861 return c_finish_omp_taskgroup (input_location,
43862 cp_parser_omp_structured_block (parser,
43863 if_p),
43864 clauses);
43868 /* OpenMP 2.5:
43869 # pragma omp threadprivate (variable-list) */
43871 static void
43872 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
43874 tree vars;
43876 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43877 cp_parser_require_pragma_eol (parser, pragma_tok);
43879 finish_omp_threadprivate (vars);
43882 /* OpenMP 4.0:
43883 # pragma omp cancel cancel-clause[optseq] new-line */
43885 #define OMP_CANCEL_CLAUSE_MASK \
43886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
43887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
43888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
43889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
43890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
43892 static void
43893 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
43895 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
43896 "#pragma omp cancel", pragma_tok);
43897 finish_omp_cancel (clauses);
43900 /* OpenMP 4.0:
43901 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
43903 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
43904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
43905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
43906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
43907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
43909 static bool
43910 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
43911 enum pragma_context context)
43913 tree clauses;
43914 bool point_seen = false;
43916 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43918 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43919 const char *p = IDENTIFIER_POINTER (id);
43921 if (strcmp (p, "point") == 0)
43923 cp_lexer_consume_token (parser->lexer);
43924 point_seen = true;
43927 if (!point_seen)
43929 cp_parser_error (parser, "expected %<point%>");
43930 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43931 return false;
43934 if (context != pragma_compound)
43936 if (context == pragma_stmt)
43937 error_at (pragma_tok->location,
43938 "%<#pragma %s%> may only be used in compound statements",
43939 "omp cancellation point");
43940 else
43941 cp_parser_error (parser, "expected declaration specifiers");
43942 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43943 return true;
43946 clauses = cp_parser_omp_all_clauses (parser,
43947 OMP_CANCELLATION_POINT_CLAUSE_MASK,
43948 "#pragma omp cancellation point",
43949 pragma_tok);
43950 finish_omp_cancellation_point (clauses);
43951 return true;
43954 /* OpenMP 4.0:
43955 #pragma omp distribute distribute-clause[optseq] new-line
43956 for-loop */
43958 #define OMP_DISTRIBUTE_CLAUSE_MASK \
43959 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
43963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43967 static tree
43968 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
43969 char *p_name, omp_clause_mask mask, tree *cclauses,
43970 bool *if_p)
43972 tree clauses, sb, ret;
43973 unsigned int save;
43974 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43976 strcat (p_name, " distribute");
43977 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
43979 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43981 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43982 const char *p = IDENTIFIER_POINTER (id);
43983 bool simd = false;
43984 bool parallel = false;
43986 if (strcmp (p, "simd") == 0)
43987 simd = true;
43988 else
43989 parallel = strcmp (p, "parallel") == 0;
43990 if (parallel || simd)
43992 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43993 if (cclauses == NULL)
43994 cclauses = cclauses_buf;
43995 cp_lexer_consume_token (parser->lexer);
43996 if (!flag_openmp) /* flag_openmp_simd */
43998 if (simd)
43999 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44000 cclauses, if_p);
44001 else
44002 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
44003 cclauses, if_p);
44005 sb = begin_omp_structured_block ();
44006 save = cp_parser_begin_omp_structured_block (parser);
44007 if (simd)
44008 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44009 cclauses, if_p);
44010 else
44011 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
44012 cclauses, if_p);
44013 cp_parser_end_omp_structured_block (parser, save);
44014 tree body = finish_omp_structured_block (sb);
44015 if (ret == NULL)
44016 return ret;
44017 ret = make_node (OMP_DISTRIBUTE);
44018 TREE_TYPE (ret) = void_type_node;
44019 OMP_FOR_BODY (ret) = body;
44020 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
44021 SET_EXPR_LOCATION (ret, loc);
44022 add_stmt (ret);
44023 return ret;
44026 if (!flag_openmp) /* flag_openmp_simd */
44028 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44029 return NULL_TREE;
44032 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44033 cclauses == NULL);
44034 if (cclauses)
44036 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
44037 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
44040 keep_next_level (true);
44041 sb = begin_omp_structured_block ();
44042 save = cp_parser_begin_omp_structured_block (parser);
44044 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
44046 cp_parser_end_omp_structured_block (parser, save);
44047 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44049 return ret;
44052 /* OpenMP 4.0:
44053 # pragma omp teams teams-clause[optseq] new-line
44054 structured-block */
44056 #define OMP_TEAMS_CLAUSE_MASK \
44057 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
44062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
44063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
44066 static tree
44067 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
44068 char *p_name, omp_clause_mask mask, tree *cclauses,
44069 bool *if_p)
44071 tree clauses, sb, ret;
44072 unsigned int save;
44073 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44075 strcat (p_name, " teams");
44076 mask |= OMP_TEAMS_CLAUSE_MASK;
44078 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44080 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44081 const char *p = IDENTIFIER_POINTER (id);
44082 if (strcmp (p, "distribute") == 0)
44084 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44085 if (cclauses == NULL)
44086 cclauses = cclauses_buf;
44088 cp_lexer_consume_token (parser->lexer);
44089 if (!flag_openmp) /* flag_openmp_simd */
44090 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
44091 cclauses, if_p);
44092 keep_next_level (true);
44093 sb = begin_omp_structured_block ();
44094 save = cp_parser_begin_omp_structured_block (parser);
44095 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
44096 cclauses, if_p);
44097 cp_parser_end_omp_structured_block (parser, save);
44098 tree body = finish_omp_structured_block (sb);
44099 if (ret == NULL)
44100 return ret;
44101 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44102 ret = make_node (OMP_TEAMS);
44103 TREE_TYPE (ret) = void_type_node;
44104 OMP_TEAMS_CLAUSES (ret) = clauses;
44105 OMP_TEAMS_BODY (ret) = body;
44106 OMP_TEAMS_COMBINED (ret) = 1;
44107 SET_EXPR_LOCATION (ret, loc);
44108 return add_stmt (ret);
44110 else if (strcmp (p, "loop") == 0)
44112 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44113 if (cclauses == NULL)
44114 cclauses = cclauses_buf;
44116 cp_lexer_consume_token (parser->lexer);
44117 if (!flag_openmp) /* flag_openmp_simd */
44118 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44119 cclauses, if_p);
44120 keep_next_level (true);
44121 sb = begin_omp_structured_block ();
44122 save = cp_parser_begin_omp_structured_block (parser);
44123 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44124 cclauses, if_p);
44125 cp_parser_end_omp_structured_block (parser, save);
44126 tree body = finish_omp_structured_block (sb);
44127 if (ret == NULL)
44128 return ret;
44129 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44130 ret = make_node (OMP_TEAMS);
44131 TREE_TYPE (ret) = void_type_node;
44132 OMP_TEAMS_CLAUSES (ret) = clauses;
44133 OMP_TEAMS_BODY (ret) = body;
44134 OMP_TEAMS_COMBINED (ret) = 1;
44135 SET_EXPR_LOCATION (ret, loc);
44136 return add_stmt (ret);
44139 if (!flag_openmp) /* flag_openmp_simd */
44141 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44142 return NULL_TREE;
44145 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44146 cclauses == NULL);
44147 if (cclauses)
44149 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
44150 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44153 tree stmt = make_node (OMP_TEAMS);
44154 TREE_TYPE (stmt) = void_type_node;
44155 OMP_TEAMS_CLAUSES (stmt) = clauses;
44156 keep_next_level (true);
44157 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44158 SET_EXPR_LOCATION (stmt, loc);
44160 return add_stmt (stmt);
44163 /* OpenMP 4.0:
44164 # pragma omp target data target-data-clause[optseq] new-line
44165 structured-block */
44167 #define OMP_TARGET_DATA_CLAUSE_MASK \
44168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
44172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
44174 static tree
44175 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44177 tree clauses
44178 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
44179 "#pragma omp target data", pragma_tok);
44180 c_omp_adjust_map_clauses (clauses, false);
44181 int map_seen = 0;
44182 for (tree *pc = &clauses; *pc;)
44184 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44185 switch (OMP_CLAUSE_MAP_KIND (*pc))
44187 case GOMP_MAP_TO:
44188 case GOMP_MAP_ALWAYS_TO:
44189 case GOMP_MAP_FROM:
44190 case GOMP_MAP_ALWAYS_FROM:
44191 case GOMP_MAP_TOFROM:
44192 case GOMP_MAP_ALWAYS_TOFROM:
44193 case GOMP_MAP_ALLOC:
44194 map_seen = 3;
44195 break;
44196 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44197 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44198 case GOMP_MAP_ALWAYS_POINTER:
44199 case GOMP_MAP_ATTACH_DETACH:
44200 break;
44201 default:
44202 map_seen |= 1;
44203 error_at (OMP_CLAUSE_LOCATION (*pc),
44204 "%<#pragma omp target data%> with map-type other "
44205 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
44206 "on %<map%> clause");
44207 *pc = OMP_CLAUSE_CHAIN (*pc);
44208 continue;
44210 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
44211 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
44212 map_seen = 3;
44213 pc = &OMP_CLAUSE_CHAIN (*pc);
44216 if (map_seen != 3)
44218 if (map_seen == 0)
44219 error_at (pragma_tok->location,
44220 "%<#pragma omp target data%> must contain at least "
44221 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
44222 "clause");
44223 return NULL_TREE;
44226 tree stmt = make_node (OMP_TARGET_DATA);
44227 TREE_TYPE (stmt) = void_type_node;
44228 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
44230 keep_next_level (true);
44231 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44233 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44234 return add_stmt (stmt);
44237 /* OpenMP 4.5:
44238 # pragma omp target enter data target-enter-data-clause[optseq] new-line
44239 structured-block */
44241 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
44242 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44248 static bool
44249 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
44250 enum pragma_context context)
44252 bool data_seen = false;
44253 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44255 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44256 const char *p = IDENTIFIER_POINTER (id);
44258 if (strcmp (p, "data") == 0)
44260 cp_lexer_consume_token (parser->lexer);
44261 data_seen = true;
44264 if (!data_seen)
44266 cp_parser_error (parser, "expected %<data%>");
44267 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44268 return false;
44271 if (context == pragma_stmt)
44273 error_at (pragma_tok->location,
44274 "%<#pragma %s%> may only be used in compound statements",
44275 "omp target enter data");
44276 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44277 return true;
44280 tree clauses
44281 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
44282 "#pragma omp target enter data", pragma_tok);
44283 c_omp_adjust_map_clauses (clauses, false);
44284 int map_seen = 0;
44285 for (tree *pc = &clauses; *pc;)
44287 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44288 switch (OMP_CLAUSE_MAP_KIND (*pc))
44290 case GOMP_MAP_TO:
44291 case GOMP_MAP_ALWAYS_TO:
44292 case GOMP_MAP_ALLOC:
44293 map_seen = 3;
44294 break;
44295 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44296 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44297 case GOMP_MAP_ALWAYS_POINTER:
44298 case GOMP_MAP_ATTACH_DETACH:
44299 break;
44300 default:
44301 map_seen |= 1;
44302 error_at (OMP_CLAUSE_LOCATION (*pc),
44303 "%<#pragma omp target enter data%> with map-type other "
44304 "than %<to%> or %<alloc%> on %<map%> clause");
44305 *pc = OMP_CLAUSE_CHAIN (*pc);
44306 continue;
44308 pc = &OMP_CLAUSE_CHAIN (*pc);
44311 if (map_seen != 3)
44313 if (map_seen == 0)
44314 error_at (pragma_tok->location,
44315 "%<#pragma omp target enter data%> must contain at least "
44316 "one %<map%> clause");
44317 return true;
44320 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
44321 TREE_TYPE (stmt) = void_type_node;
44322 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
44323 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44324 add_stmt (stmt);
44325 return true;
44328 /* OpenMP 4.5:
44329 # pragma omp target exit data target-enter-data-clause[optseq] new-line
44330 structured-block */
44332 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
44333 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44339 static bool
44340 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
44341 enum pragma_context context)
44343 bool data_seen = false;
44344 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44346 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44347 const char *p = IDENTIFIER_POINTER (id);
44349 if (strcmp (p, "data") == 0)
44351 cp_lexer_consume_token (parser->lexer);
44352 data_seen = true;
44355 if (!data_seen)
44357 cp_parser_error (parser, "expected %<data%>");
44358 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44359 return false;
44362 if (context == pragma_stmt)
44364 error_at (pragma_tok->location,
44365 "%<#pragma %s%> may only be used in compound statements",
44366 "omp target exit data");
44367 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44368 return true;
44371 tree clauses
44372 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
44373 "#pragma omp target exit data", pragma_tok);
44374 c_omp_adjust_map_clauses (clauses, false);
44375 int map_seen = 0;
44376 for (tree *pc = &clauses; *pc;)
44378 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44379 switch (OMP_CLAUSE_MAP_KIND (*pc))
44381 case GOMP_MAP_FROM:
44382 case GOMP_MAP_ALWAYS_FROM:
44383 case GOMP_MAP_RELEASE:
44384 case GOMP_MAP_DELETE:
44385 map_seen = 3;
44386 break;
44387 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44388 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44389 case GOMP_MAP_ALWAYS_POINTER:
44390 case GOMP_MAP_ATTACH_DETACH:
44391 break;
44392 default:
44393 map_seen |= 1;
44394 error_at (OMP_CLAUSE_LOCATION (*pc),
44395 "%<#pragma omp target exit data%> with map-type other "
44396 "than %<from%>, %<release%> or %<delete%> on %<map%>"
44397 " clause");
44398 *pc = OMP_CLAUSE_CHAIN (*pc);
44399 continue;
44401 pc = &OMP_CLAUSE_CHAIN (*pc);
44404 if (map_seen != 3)
44406 if (map_seen == 0)
44407 error_at (pragma_tok->location,
44408 "%<#pragma omp target exit data%> must contain at least "
44409 "one %<map%> clause");
44410 return true;
44413 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
44414 TREE_TYPE (stmt) = void_type_node;
44415 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
44416 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44417 add_stmt (stmt);
44418 return true;
44421 /* OpenMP 4.0:
44422 # pragma omp target update target-update-clause[optseq] new-line */
44424 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
44425 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
44426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
44427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44432 static bool
44433 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
44434 enum pragma_context context)
44436 if (context == pragma_stmt)
44438 error_at (pragma_tok->location,
44439 "%<#pragma %s%> may only be used in compound statements",
44440 "omp target update");
44441 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44442 return true;
44445 tree clauses
44446 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
44447 "#pragma omp target update", pragma_tok);
44448 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
44449 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
44451 error_at (pragma_tok->location,
44452 "%<#pragma omp target update%> must contain at least one "
44453 "%<from%> or %<to%> clauses");
44454 return true;
44457 tree stmt = make_node (OMP_TARGET_UPDATE);
44458 TREE_TYPE (stmt) = void_type_node;
44459 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
44460 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44461 add_stmt (stmt);
44462 return true;
44465 /* OpenMP 4.0:
44466 # pragma omp target target-clause[optseq] new-line
44467 structured-block */
44469 #define OMP_TARGET_CLAUSE_MASK \
44470 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
44475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
44478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
44481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
44482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
44484 static bool
44485 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
44486 enum pragma_context context, bool *if_p)
44488 if (flag_openmp)
44489 omp_requires_mask
44490 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44492 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44494 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44495 const char *p = IDENTIFIER_POINTER (id);
44496 enum tree_code ccode = ERROR_MARK;
44498 if (strcmp (p, "teams") == 0)
44499 ccode = OMP_TEAMS;
44500 else if (strcmp (p, "parallel") == 0)
44501 ccode = OMP_PARALLEL;
44502 else if (strcmp (p, "simd") == 0)
44503 ccode = OMP_SIMD;
44504 if (ccode != ERROR_MARK)
44506 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
44507 char p_name[sizeof ("#pragma omp target teams distribute "
44508 "parallel for simd")];
44510 cp_lexer_consume_token (parser->lexer);
44511 strcpy (p_name, "#pragma omp target");
44512 if (!flag_openmp) /* flag_openmp_simd */
44514 tree stmt;
44515 switch (ccode)
44517 case OMP_TEAMS:
44518 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
44519 OMP_TARGET_CLAUSE_MASK,
44520 cclauses, if_p);
44521 break;
44522 case OMP_PARALLEL:
44523 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44524 OMP_TARGET_CLAUSE_MASK,
44525 cclauses, if_p);
44526 break;
44527 case OMP_SIMD:
44528 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
44529 OMP_TARGET_CLAUSE_MASK,
44530 cclauses, if_p);
44531 break;
44532 default:
44533 gcc_unreachable ();
44535 return stmt != NULL_TREE;
44537 keep_next_level (true);
44538 tree sb = begin_omp_structured_block (), ret;
44539 unsigned save = cp_parser_begin_omp_structured_block (parser);
44540 switch (ccode)
44542 case OMP_TEAMS:
44543 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
44544 OMP_TARGET_CLAUSE_MASK, cclauses,
44545 if_p);
44546 break;
44547 case OMP_PARALLEL:
44548 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44549 OMP_TARGET_CLAUSE_MASK, cclauses,
44550 if_p);
44551 break;
44552 case OMP_SIMD:
44553 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
44554 OMP_TARGET_CLAUSE_MASK, cclauses,
44555 if_p);
44556 break;
44557 default:
44558 gcc_unreachable ();
44560 cp_parser_end_omp_structured_block (parser, save);
44561 tree body = finish_omp_structured_block (sb);
44562 if (ret == NULL_TREE)
44563 return false;
44564 if (ccode == OMP_TEAMS && !processing_template_decl)
44565 /* For combined target teams, ensure the num_teams and
44566 thread_limit clause expressions are evaluated on the host,
44567 before entering the target construct. */
44568 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44569 c; c = OMP_CLAUSE_CHAIN (c))
44570 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
44571 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
44572 for (int i = 0;
44573 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
44574 if (OMP_CLAUSE_OPERAND (c, i)
44575 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
44577 tree expr = OMP_CLAUSE_OPERAND (c, i);
44578 expr = force_target_expr (TREE_TYPE (expr), expr,
44579 tf_none);
44580 if (expr == error_mark_node)
44581 continue;
44582 tree tmp = TARGET_EXPR_SLOT (expr);
44583 add_stmt (expr);
44584 OMP_CLAUSE_OPERAND (c, i) = expr;
44585 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
44586 OMP_CLAUSE_FIRSTPRIVATE);
44587 OMP_CLAUSE_DECL (tc) = tmp;
44588 OMP_CLAUSE_CHAIN (tc)
44589 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
44590 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
44592 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
44593 finish_omp_target (pragma_tok->location,
44594 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
44595 return true;
44597 else if (!flag_openmp) /* flag_openmp_simd */
44599 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44600 return false;
44602 else if (strcmp (p, "data") == 0)
44604 cp_lexer_consume_token (parser->lexer);
44605 cp_parser_omp_target_data (parser, pragma_tok, if_p);
44606 return true;
44608 else if (strcmp (p, "enter") == 0)
44610 cp_lexer_consume_token (parser->lexer);
44611 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
44613 else if (strcmp (p, "exit") == 0)
44615 cp_lexer_consume_token (parser->lexer);
44616 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
44618 else if (strcmp (p, "update") == 0)
44620 cp_lexer_consume_token (parser->lexer);
44621 return cp_parser_omp_target_update (parser, pragma_tok, context);
44624 if (!flag_openmp) /* flag_openmp_simd */
44626 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44627 return false;
44630 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
44631 "#pragma omp target", pragma_tok,
44632 false);
44633 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
44634 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44636 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
44637 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
44638 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
44639 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
44640 OMP_CLAUSE_CHAIN (c) = nc;
44642 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
44644 c_omp_adjust_map_clauses (clauses, true);
44645 keep_next_level (true);
44646 tree body = cp_parser_omp_structured_block (parser, if_p);
44648 finish_omp_target (pragma_tok->location, clauses, body, false);
44649 return true;
44652 /* OpenACC 2.0:
44653 # pragma acc cache (variable-list) new-line
44656 static tree
44657 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
44659 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
44660 clauses. */
44661 auto_suppress_location_wrappers sentinel;
44663 tree stmt, clauses;
44665 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
44666 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
44668 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
44670 stmt = make_node (OACC_CACHE);
44671 TREE_TYPE (stmt) = void_type_node;
44672 OACC_CACHE_CLAUSES (stmt) = clauses;
44673 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44674 add_stmt (stmt);
44676 return stmt;
44679 /* OpenACC 2.0:
44680 # pragma acc data oacc-data-clause[optseq] new-line
44681 structured-block */
44683 #define OACC_DATA_CLAUSE_MASK \
44684 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
44690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44695 static tree
44696 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44698 tree stmt, clauses, block;
44699 unsigned int save;
44701 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
44702 "#pragma acc data", pragma_tok);
44704 block = begin_omp_parallel ();
44705 save = cp_parser_begin_omp_structured_block (parser);
44706 cp_parser_statement (parser, NULL_TREE, false, if_p);
44707 cp_parser_end_omp_structured_block (parser, save);
44708 stmt = finish_oacc_data (clauses, block);
44709 return stmt;
44712 /* OpenACC 2.0:
44713 # pragma acc host_data <clauses> new-line
44714 structured-block */
44716 #define OACC_HOST_DATA_CLAUSE_MASK \
44717 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
44718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
44721 static tree
44722 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44724 tree stmt, clauses, block;
44725 unsigned int save;
44727 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
44728 "#pragma acc host_data", pragma_tok);
44730 block = begin_omp_parallel ();
44731 save = cp_parser_begin_omp_structured_block (parser);
44732 cp_parser_statement (parser, NULL_TREE, false, if_p);
44733 cp_parser_end_omp_structured_block (parser, save);
44734 stmt = finish_oacc_host_data (clauses, block);
44735 return stmt;
44738 /* OpenACC 2.0:
44739 # pragma acc declare oacc-data-clause[optseq] new-line
44742 #define OACC_DECLARE_CLAUSE_MASK \
44743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
44749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
44750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44752 static tree
44753 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
44755 tree clauses, stmt;
44756 bool error = false;
44757 bool found_in_scope = global_bindings_p ();
44759 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
44760 "#pragma acc declare", pragma_tok, true);
44763 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44765 error_at (pragma_tok->location,
44766 "no valid clauses specified in %<#pragma acc declare%>");
44767 return NULL_TREE;
44770 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
44772 location_t loc = OMP_CLAUSE_LOCATION (t);
44773 tree decl = OMP_CLAUSE_DECL (t);
44774 if (!DECL_P (decl))
44776 error_at (loc, "array section in %<#pragma acc declare%>");
44777 error = true;
44778 continue;
44780 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
44781 switch (OMP_CLAUSE_MAP_KIND (t))
44783 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44784 case GOMP_MAP_ALLOC:
44785 case GOMP_MAP_TO:
44786 case GOMP_MAP_FORCE_DEVICEPTR:
44787 case GOMP_MAP_DEVICE_RESIDENT:
44788 break;
44790 case GOMP_MAP_LINK:
44791 if (!global_bindings_p ()
44792 && (TREE_STATIC (decl)
44793 || !DECL_EXTERNAL (decl)))
44795 error_at (loc,
44796 "%qD must be a global variable in "
44797 "%<#pragma acc declare link%>",
44798 decl);
44799 error = true;
44800 continue;
44802 break;
44804 default:
44805 if (global_bindings_p ())
44807 error_at (loc, "invalid OpenACC clause at file scope");
44808 error = true;
44809 continue;
44811 if (DECL_EXTERNAL (decl))
44813 error_at (loc,
44814 "invalid use of %<extern%> variable %qD "
44815 "in %<#pragma acc declare%>", decl);
44816 error = true;
44817 continue;
44819 else if (TREE_PUBLIC (decl))
44821 error_at (loc,
44822 "invalid use of %<global%> variable %qD "
44823 "in %<#pragma acc declare%>", decl);
44824 error = true;
44825 continue;
44827 break;
44830 if (!found_in_scope)
44831 /* This seems to ignore the existence of cleanup scopes?
44832 What is the meaning for local extern decls? The local
44833 extern is in this scope, but it is referring to a decl that
44834 is namespace scope. */
44835 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
44836 if (d == decl)
44838 found_in_scope = true;
44839 break;
44841 if (!found_in_scope)
44843 error_at (loc,
44844 "%qD must be a variable declared in the same scope as "
44845 "%<#pragma acc declare%>", decl);
44846 error = true;
44847 continue;
44850 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
44851 || lookup_attribute ("omp declare target link",
44852 DECL_ATTRIBUTES (decl)))
44854 error_at (loc, "variable %qD used more than once with "
44855 "%<#pragma acc declare%>", decl);
44856 error = true;
44857 continue;
44860 if (!error)
44862 tree id;
44864 if (DECL_LOCAL_DECL_P (decl))
44865 /* We need to mark the aliased decl, as that is the entity
44866 that is being referred to. This won't work for
44867 dependent variables, but it didn't work for them before
44868 DECL_LOCAL_DECL_P was a thing either. But then
44869 dependent local extern variable decls are as rare as
44870 hen's teeth. */
44871 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
44872 if (alias != error_mark_node)
44873 decl = alias;
44875 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
44876 id = get_identifier ("omp declare target link");
44877 else
44878 id = get_identifier ("omp declare target");
44880 DECL_ATTRIBUTES (decl)
44881 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
44882 if (current_binding_level->kind == sk_namespace)
44884 symtab_node *node = symtab_node::get (decl);
44885 if (node != NULL)
44887 node->offloadable = 1;
44888 if (ENABLE_OFFLOADING)
44890 g->have_offload = true;
44891 if (is_a <varpool_node *> (node))
44892 vec_safe_push (offload_vars, decl);
44899 if (error || current_binding_level->kind == sk_namespace)
44900 return NULL_TREE;
44902 stmt = make_node (OACC_DECLARE);
44903 TREE_TYPE (stmt) = void_type_node;
44904 OACC_DECLARE_CLAUSES (stmt) = clauses;
44905 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44907 add_stmt (stmt);
44909 return NULL_TREE;
44912 /* OpenACC 2.0:
44913 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
44917 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
44919 LOC is the location of the #pragma token.
44922 #define OACC_ENTER_DATA_CLAUSE_MASK \
44923 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44930 #define OACC_EXIT_DATA_CLAUSE_MASK \
44931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
44935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
44936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
44937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44939 static tree
44940 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
44941 bool enter)
44943 location_t loc = pragma_tok->location;
44944 tree stmt, clauses;
44945 const char *p = "";
44947 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44948 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
44950 if (strcmp (p, "data") != 0)
44952 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
44953 enter ? "enter" : "exit");
44954 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44955 return NULL_TREE;
44958 cp_lexer_consume_token (parser->lexer);
44960 if (enter)
44961 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
44962 "#pragma acc enter data", pragma_tok);
44963 else
44964 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
44965 "#pragma acc exit data", pragma_tok);
44967 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44969 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
44970 enter ? "enter" : "exit");
44971 return NULL_TREE;
44974 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
44975 TREE_TYPE (stmt) = void_type_node;
44976 OMP_STANDALONE_CLAUSES (stmt) = clauses;
44977 SET_EXPR_LOCATION (stmt, loc);
44978 add_stmt (stmt);
44979 return stmt;
44982 /* OpenACC 2.0:
44983 # pragma acc loop oacc-loop-clause[optseq] new-line
44984 structured-block */
44986 #define OACC_LOOP_CLAUSE_MASK \
44987 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
44988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
44989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
44990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
44991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
44992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
44993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
44994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
44995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
44996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
44998 static tree
44999 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
45000 omp_clause_mask mask, tree *cclauses, bool *if_p)
45002 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
45004 strcat (p_name, " loop");
45005 mask |= OACC_LOOP_CLAUSE_MASK;
45007 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
45008 cclauses == NULL);
45009 if (cclauses)
45011 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
45012 if (*cclauses)
45013 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
45014 if (clauses)
45015 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
45018 tree block = begin_omp_structured_block ();
45019 int save = cp_parser_begin_omp_structured_block (parser);
45020 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
45021 cp_parser_end_omp_structured_block (parser, save);
45022 add_stmt (finish_omp_structured_block (block));
45024 return stmt;
45027 /* OpenACC 2.0:
45028 # pragma acc kernels oacc-kernels-clause[optseq] new-line
45029 structured-block
45033 # pragma acc parallel oacc-parallel-clause[optseq] new-line
45034 structured-block
45036 OpenACC 2.6:
45038 # pragma acc serial oacc-serial-clause[optseq] new-line
45041 #define OACC_KERNELS_CLAUSE_MASK \
45042 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
45053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
45054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
45056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45058 #define OACC_PARALLEL_CLAUSE_MASK \
45059 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
45068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
45071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
45072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
45076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45078 #define OACC_SERIAL_CLAUSE_MASK \
45079 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
45091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45095 static tree
45096 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
45097 char *p_name, bool *if_p)
45099 omp_clause_mask mask;
45100 enum tree_code code;
45101 switch (cp_parser_pragma_kind (pragma_tok))
45103 case PRAGMA_OACC_KERNELS:
45104 strcat (p_name, " kernels");
45105 mask = OACC_KERNELS_CLAUSE_MASK;
45106 code = OACC_KERNELS;
45107 break;
45108 case PRAGMA_OACC_PARALLEL:
45109 strcat (p_name, " parallel");
45110 mask = OACC_PARALLEL_CLAUSE_MASK;
45111 code = OACC_PARALLEL;
45112 break;
45113 case PRAGMA_OACC_SERIAL:
45114 strcat (p_name, " serial");
45115 mask = OACC_SERIAL_CLAUSE_MASK;
45116 code = OACC_SERIAL;
45117 break;
45118 default:
45119 gcc_unreachable ();
45122 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45124 const char *p
45125 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45126 if (strcmp (p, "loop") == 0)
45128 cp_lexer_consume_token (parser->lexer);
45129 tree block = begin_omp_parallel ();
45130 tree clauses;
45131 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
45132 &clauses, if_p);
45133 protected_set_expr_location (stmt, pragma_tok->location);
45134 return finish_omp_construct (code, block, clauses);
45138 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
45140 tree block = begin_omp_parallel ();
45141 unsigned int save = cp_parser_begin_omp_structured_block (parser);
45142 cp_parser_statement (parser, NULL_TREE, false, if_p);
45143 cp_parser_end_omp_structured_block (parser, save);
45144 return finish_omp_construct (code, block, clauses);
45147 /* OpenACC 2.0:
45148 # pragma acc update oacc-update-clause[optseq] new-line
45151 #define OACC_UPDATE_CLAUSE_MASK \
45152 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
45154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
45155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
45157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
45159 static tree
45160 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
45162 tree stmt, clauses;
45164 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
45165 "#pragma acc update", pragma_tok);
45167 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45169 error_at (pragma_tok->location,
45170 "%<#pragma acc update%> must contain at least one "
45171 "%<device%> or %<host%> or %<self%> clause");
45172 return NULL_TREE;
45175 stmt = make_node (OACC_UPDATE);
45176 TREE_TYPE (stmt) = void_type_node;
45177 OACC_UPDATE_CLAUSES (stmt) = clauses;
45178 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45179 add_stmt (stmt);
45180 return stmt;
45183 /* OpenACC 2.0:
45184 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
45186 LOC is the location of the #pragma token.
45189 #define OACC_WAIT_CLAUSE_MASK \
45190 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
45192 static tree
45193 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
45195 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
45196 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45198 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
45199 list = cp_parser_oacc_wait_list (parser, loc, list);
45201 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
45202 "#pragma acc wait", pragma_tok);
45204 stmt = c_finish_oacc_wait (loc, list, clauses);
45205 stmt = finish_expr_stmt (stmt);
45207 return stmt;
45210 /* OpenMP 4.0:
45211 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
45213 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
45214 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
45215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
45217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
45218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
45219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
45221 static void
45222 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
45223 enum pragma_context context,
45224 bool variant_p)
45226 bool first_p = parser->omp_declare_simd == NULL;
45227 cp_omp_declare_simd_data data;
45228 if (first_p)
45230 data.error_seen = false;
45231 data.fndecl_seen = false;
45232 data.variant_p = variant_p;
45233 data.tokens = vNULL;
45234 data.attribs[0] = NULL;
45235 data.attribs[1] = NULL;
45236 data.loc = UNKNOWN_LOCATION;
45237 /* It is safe to take the address of a local variable; it will only be
45238 used while this scope is live. */
45239 parser->omp_declare_simd = &data;
45241 else if (parser->omp_declare_simd->variant_p != variant_p)
45243 error_at (pragma_tok->location,
45244 "%<#pragma omp declare %s%> followed by "
45245 "%<#pragma omp declare %s%>",
45246 parser->omp_declare_simd->variant_p ? "variant" : "simd",
45247 parser->omp_declare_simd->variant_p ? "simd" : "variant");
45248 parser->omp_declare_simd->error_seen = true;
45251 /* Store away all pragma tokens. */
45252 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
45253 cp_lexer_consume_token (parser->lexer);
45254 cp_parser_require_pragma_eol (parser, pragma_tok);
45255 struct cp_token_cache *cp
45256 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
45257 parser->omp_declare_simd->tokens.safe_push (cp);
45259 if (first_p)
45261 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
45262 cp_parser_pragma (parser, context, NULL);
45263 switch (context)
45265 case pragma_external:
45266 cp_parser_declaration (parser, NULL_TREE);
45267 break;
45268 case pragma_member:
45269 cp_parser_member_declaration (parser);
45270 break;
45271 case pragma_objc_icode:
45272 cp_parser_block_declaration (parser, /*statement_p=*/false);
45273 break;
45274 default:
45275 cp_parser_declaration_statement (parser);
45276 break;
45278 if (parser->omp_declare_simd
45279 && !parser->omp_declare_simd->error_seen
45280 && !parser->omp_declare_simd->fndecl_seen)
45281 error_at (pragma_tok->location,
45282 "%<#pragma omp declare %s%> not immediately followed by "
45283 "function declaration or definition",
45284 parser->omp_declare_simd->variant_p ? "variant" : "simd");
45285 data.tokens.release ();
45286 parser->omp_declare_simd = NULL;
45290 static const char *const omp_construct_selectors[] = {
45291 "simd", "target", "teams", "parallel", "for", NULL };
45292 static const char *const omp_device_selectors[] = {
45293 "kind", "isa", "arch", NULL };
45294 static const char *const omp_implementation_selectors[] = {
45295 "vendor", "extension", "atomic_default_mem_order", "unified_address",
45296 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
45297 static const char *const omp_user_selectors[] = {
45298 "condition", NULL };
45300 /* OpenMP 5.0:
45302 trait-selector:
45303 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
45305 trait-score:
45306 score(score-expression) */
45308 static tree
45309 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
45311 tree ret = NULL_TREE;
45314 tree selector;
45315 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45316 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45317 selector = cp_lexer_peek_token (parser->lexer)->u.value;
45318 else
45320 cp_parser_error (parser, "expected trait selector name");
45321 return error_mark_node;
45324 tree properties = NULL_TREE;
45325 const char *const *selectors = NULL;
45326 bool allow_score = true;
45327 bool allow_user = false;
45328 int property_limit = 0;
45329 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
45330 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
45331 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
45332 switch (IDENTIFIER_POINTER (set)[0])
45334 case 'c': /* construct */
45335 selectors = omp_construct_selectors;
45336 allow_score = false;
45337 property_limit = 1;
45338 property_kind = CTX_PROPERTY_SIMD;
45339 break;
45340 case 'd': /* device */
45341 selectors = omp_device_selectors;
45342 allow_score = false;
45343 allow_user = true;
45344 property_limit = 3;
45345 property_kind = CTX_PROPERTY_NAME_LIST;
45346 break;
45347 case 'i': /* implementation */
45348 selectors = omp_implementation_selectors;
45349 allow_user = true;
45350 property_limit = 3;
45351 property_kind = CTX_PROPERTY_NAME_LIST;
45352 break;
45353 case 'u': /* user */
45354 selectors = omp_user_selectors;
45355 property_limit = 1;
45356 property_kind = CTX_PROPERTY_EXPR;
45357 break;
45358 default:
45359 gcc_unreachable ();
45361 for (int i = 0; ; i++)
45363 if (selectors[i] == NULL)
45365 if (allow_user)
45367 property_kind = CTX_PROPERTY_USER;
45368 break;
45370 else
45372 error ("selector %qs not allowed for context selector "
45373 "set %qs", IDENTIFIER_POINTER (selector),
45374 IDENTIFIER_POINTER (set));
45375 cp_lexer_consume_token (parser->lexer);
45376 return error_mark_node;
45379 if (i == property_limit)
45380 property_kind = CTX_PROPERTY_NONE;
45381 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
45382 break;
45384 if (property_kind == CTX_PROPERTY_NAME_LIST
45385 && IDENTIFIER_POINTER (set)[0] == 'i'
45386 && strcmp (IDENTIFIER_POINTER (selector),
45387 "atomic_default_mem_order") == 0)
45388 property_kind = CTX_PROPERTY_ID;
45390 cp_lexer_consume_token (parser->lexer);
45392 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
45394 if (property_kind == CTX_PROPERTY_NONE)
45396 error ("selector %qs does not accept any properties",
45397 IDENTIFIER_POINTER (selector));
45398 return error_mark_node;
45401 matching_parens parens;
45402 parens.consume_open (parser);
45404 cp_token *token = cp_lexer_peek_token (parser->lexer);
45405 if (allow_score
45406 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
45407 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
45408 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
45410 cp_lexer_save_tokens (parser->lexer);
45411 cp_lexer_consume_token (parser->lexer);
45412 cp_lexer_consume_token (parser->lexer);
45413 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
45414 true)
45415 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
45417 cp_lexer_rollback_tokens (parser->lexer);
45418 cp_lexer_consume_token (parser->lexer);
45420 matching_parens parens2;
45421 parens2.require_open (parser);
45422 tree score = cp_parser_constant_expression (parser);
45423 if (!parens2.require_close (parser))
45424 cp_parser_skip_to_closing_parenthesis (parser, true,
45425 false, true);
45426 cp_parser_require (parser, CPP_COLON, RT_COLON);
45427 if (score != error_mark_node)
45429 score = fold_non_dependent_expr (score);
45430 if (value_dependent_expression_p (score))
45431 properties = tree_cons (get_identifier (" score"),
45432 score, properties);
45433 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
45434 || TREE_CODE (score) != INTEGER_CST)
45435 error_at (token->location, "score argument must be "
45436 "constant integer expression");
45437 else if (tree_int_cst_sgn (score) < 0)
45438 error_at (token->location, "score argument must be "
45439 "non-negative");
45440 else
45441 properties = tree_cons (get_identifier (" score"),
45442 score, properties);
45445 else
45446 cp_lexer_rollback_tokens (parser->lexer);
45448 token = cp_lexer_peek_token (parser->lexer);
45451 switch (property_kind)
45453 tree t;
45454 case CTX_PROPERTY_USER:
45457 t = cp_parser_constant_expression (parser);
45458 if (t != error_mark_node)
45460 t = fold_non_dependent_expr (t);
45461 if (TREE_CODE (t) == STRING_CST)
45462 properties = tree_cons (NULL_TREE, t, properties);
45463 else if (!value_dependent_expression_p (t)
45464 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45465 || !tree_fits_shwi_p (t)))
45466 error_at (token->location, "property must be "
45467 "constant integer expression or string "
45468 "literal");
45469 else
45470 properties = tree_cons (NULL_TREE, t, properties);
45472 else
45473 return error_mark_node;
45475 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45476 cp_lexer_consume_token (parser->lexer);
45477 else
45478 break;
45480 while (1);
45481 break;
45482 case CTX_PROPERTY_ID:
45483 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45484 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45486 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
45487 cp_lexer_consume_token (parser->lexer);
45488 properties = tree_cons (prop, NULL_TREE, properties);
45490 else
45492 cp_parser_error (parser, "expected identifier");
45493 return error_mark_node;
45495 break;
45496 case CTX_PROPERTY_NAME_LIST:
45499 tree prop = NULL_TREE, value = NULL_TREE;
45500 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45501 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45503 prop = cp_lexer_peek_token (parser->lexer)->u.value;
45504 cp_lexer_consume_token (parser->lexer);
45506 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
45507 value = cp_parser_string_literal (parser, false, false);
45508 else
45510 cp_parser_error (parser, "expected identifier or "
45511 "string literal");
45512 return error_mark_node;
45515 properties = tree_cons (prop, value, properties);
45517 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45518 cp_lexer_consume_token (parser->lexer);
45519 else
45520 break;
45522 while (1);
45523 break;
45524 case CTX_PROPERTY_EXPR:
45525 t = cp_parser_constant_expression (parser);
45526 if (t != error_mark_node)
45528 t = fold_non_dependent_expr (t);
45529 if (!value_dependent_expression_p (t)
45530 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45531 || !tree_fits_shwi_p (t)))
45532 error_at (token->location, "property must be "
45533 "constant integer expression");
45534 else
45535 properties = tree_cons (NULL_TREE, t, properties);
45537 else
45538 return error_mark_node;
45539 break;
45540 case CTX_PROPERTY_SIMD:
45541 if (!has_parms_p)
45543 error_at (token->location, "properties for %<simd%> "
45544 "selector may not be specified in "
45545 "%<metadirective%>");
45546 return error_mark_node;
45548 properties
45549 = cp_parser_omp_all_clauses (parser,
45550 OMP_DECLARE_SIMD_CLAUSE_MASK,
45551 "simd", NULL, true, 2);
45552 break;
45553 default:
45554 gcc_unreachable ();
45557 if (!parens.require_close (parser))
45558 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
45560 properties = nreverse (properties);
45562 else if (property_kind == CTX_PROPERTY_NAME_LIST
45563 || property_kind == CTX_PROPERTY_ID
45564 || property_kind == CTX_PROPERTY_EXPR)
45566 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
45567 return error_mark_node;
45570 ret = tree_cons (selector, properties, ret);
45572 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45573 cp_lexer_consume_token (parser->lexer);
45574 else
45575 break;
45577 while (1);
45579 return nreverse (ret);
45582 /* OpenMP 5.0:
45584 trait-set-selector[,trait-set-selector[,...]]
45586 trait-set-selector:
45587 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
45589 trait-set-selector-name:
45590 constructor
45591 device
45592 implementation
45593 user */
45595 static tree
45596 cp_parser_omp_context_selector_specification (cp_parser *parser,
45597 bool has_parms_p)
45599 tree ret = NULL_TREE;
45602 const char *setp = "";
45603 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45604 setp
45605 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45606 switch (setp[0])
45608 case 'c':
45609 if (strcmp (setp, "construct") == 0)
45610 setp = NULL;
45611 break;
45612 case 'd':
45613 if (strcmp (setp, "device") == 0)
45614 setp = NULL;
45615 break;
45616 case 'i':
45617 if (strcmp (setp, "implementation") == 0)
45618 setp = NULL;
45619 break;
45620 case 'u':
45621 if (strcmp (setp, "user") == 0)
45622 setp = NULL;
45623 break;
45624 default:
45625 break;
45627 if (setp)
45629 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
45630 "%<implementation%> or %<user%>");
45631 return error_mark_node;
45634 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
45635 cp_lexer_consume_token (parser->lexer);
45637 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
45638 return error_mark_node;
45640 matching_braces braces;
45641 if (!braces.require_open (parser))
45642 return error_mark_node;
45644 tree selectors
45645 = cp_parser_omp_context_selector (parser, set, has_parms_p);
45646 if (selectors == error_mark_node)
45648 cp_parser_skip_to_closing_brace (parser);
45649 ret = error_mark_node;
45651 else if (ret != error_mark_node)
45652 ret = tree_cons (set, selectors, ret);
45654 braces.require_close (parser);
45656 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45657 cp_lexer_consume_token (parser->lexer);
45658 else
45659 break;
45661 while (1);
45663 if (ret == error_mark_node)
45664 return ret;
45665 return nreverse (ret);
45668 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
45669 that into "omp declare variant base" attribute. */
45671 static tree
45672 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
45673 tree attrs)
45675 matching_parens parens;
45676 if (!parens.require_open (parser))
45678 fail:
45679 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45680 return attrs;
45683 bool template_p;
45684 cp_id_kind idk = CP_ID_KIND_NONE;
45685 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
45686 cp_expr varid
45687 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
45688 /*check_dependency_p=*/true,
45689 /*template_p=*/&template_p,
45690 /*declarator_p=*/false,
45691 /*optional_p=*/false);
45692 parens.require_close (parser);
45694 tree variant;
45695 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
45696 || TREE_CODE (varid) == TYPE_DECL
45697 || varid == error_mark_node)
45698 variant = varid;
45699 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
45700 variant = NULL_TREE;
45701 else
45703 tree ambiguous_decls;
45704 variant = cp_parser_lookup_name (parser, varid, none_type,
45705 template_p, /*is_namespace=*/false,
45706 /*check_dependency=*/true,
45707 &ambiguous_decls,
45708 varid.get_location ());
45709 if (ambiguous_decls)
45710 variant = NULL_TREE;
45712 if (variant == NULL_TREE)
45713 variant = error_mark_node;
45714 else if (TREE_CODE (variant) != SCOPE_REF)
45716 const char *error_msg;
45717 variant
45718 = finish_id_expression (varid, variant, parser->scope,
45719 &idk, false, true,
45720 &parser->non_integral_constant_expression_p,
45721 template_p, true, false, false, &error_msg,
45722 varid.get_location ());
45723 if (error_msg)
45724 cp_parser_error (parser, error_msg);
45726 location_t caret_loc = get_pure_location (varid.get_location ());
45727 location_t start_loc = get_start (varid_token->location);
45728 location_t finish_loc = get_finish (varid.get_location ());
45729 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
45731 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45732 if (parser->lexer->in_omp_attribute_pragma
45733 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45734 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45735 cp_lexer_consume_token (parser->lexer);
45737 const char *clause = "";
45738 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
45739 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45740 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45741 if (strcmp (clause, "match"))
45743 cp_parser_error (parser, "expected %<match%>");
45744 goto fail;
45747 cp_lexer_consume_token (parser->lexer);
45749 if (!parens.require_open (parser))
45750 goto fail;
45752 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
45753 if (ctx == error_mark_node)
45754 goto fail;
45755 ctx = omp_check_context_selector (match_loc, ctx);
45756 if (ctx != error_mark_node && variant != error_mark_node)
45758 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
45759 match_loc);
45760 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
45761 loc_node = tree_cons (match_loc_node,
45762 build_int_cst (integer_type_node, idk),
45763 build_tree_list (loc_node, integer_zero_node));
45764 attrs = tree_cons (get_identifier ("omp declare variant base"),
45765 tree_cons (variant, ctx, loc_node), attrs);
45766 if (processing_template_decl)
45767 ATTR_IS_DEPENDENT (attrs) = 1;
45770 parens.require_close (parser);
45771 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45772 return attrs;
45776 /* Finalize #pragma omp declare simd clauses after direct declarator has
45777 been parsed, and put that into "omp declare simd" attribute. */
45779 static tree
45780 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
45782 struct cp_token_cache *ce;
45783 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
45784 int i;
45786 if (!data->error_seen && data->fndecl_seen)
45788 error ("%<#pragma omp declare %s%> not immediately followed by "
45789 "a single function declaration or definition",
45790 data->variant_p ? "variant" : "simd");
45791 data->error_seen = true;
45793 if (data->error_seen)
45794 return attrs;
45796 FOR_EACH_VEC_ELT (data->tokens, i, ce)
45798 tree c, cl;
45800 cp_parser_push_lexer_for_tokens (parser, ce);
45801 parser->lexer->in_pragma = true;
45802 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
45803 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
45804 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45805 const char *kind = IDENTIFIER_POINTER (id);
45806 cp_lexer_consume_token (parser->lexer);
45807 if (strcmp (kind, "simd") == 0)
45809 /* For now only in C++ attributes, do it always for OpenMP 5.1.
45810 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45811 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45812 cp_lexer_consume_token (parser->lexer); */
45814 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
45815 "#pragma omp declare simd",
45816 pragma_tok);
45817 if (cl)
45818 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
45819 c = build_tree_list (get_identifier ("omp declare simd"), cl);
45820 TREE_CHAIN (c) = attrs;
45821 if (processing_template_decl)
45822 ATTR_IS_DEPENDENT (c) = 1;
45823 attrs = c;
45825 else
45827 gcc_assert (strcmp (kind, "variant") == 0);
45828 attrs
45829 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
45831 cp_parser_pop_lexer (parser);
45834 cp_lexer *lexer = NULL;
45835 for (int i = 0; i < 2; i++)
45837 if (data->attribs[i] == NULL)
45838 continue;
45839 for (tree *pa = data->attribs[i]; *pa; )
45840 if (get_attribute_namespace (*pa) == omp_identifier
45841 && is_attribute_p ("directive", get_attribute_name (*pa)))
45843 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
45845 tree d = TREE_VALUE (a);
45846 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
45847 cp_token *first = DEFPARSE_TOKENS (d)->first;
45848 cp_token *last = DEFPARSE_TOKENS (d)->last;
45849 const char *directive[3] = {};
45850 for (int j = 0; j < 3; j++)
45852 tree id = NULL_TREE;
45853 if (first + j == last)
45854 break;
45855 if (first[j].type == CPP_NAME)
45856 id = first[j].u.value;
45857 else if (first[j].type == CPP_KEYWORD)
45858 id = ridpointers[(int) first[j].keyword];
45859 else
45860 break;
45861 directive[j] = IDENTIFIER_POINTER (id);
45863 const c_omp_directive *dir = NULL;
45864 if (directive[0])
45865 dir = c_omp_categorize_directive (directive[0], directive[1],
45866 directive[2]);
45867 if (dir == NULL)
45869 error_at (first->location,
45870 "unknown OpenMP directive name in "
45871 "%<omp::directive%> attribute argument");
45872 continue;
45874 if (dir->id != PRAGMA_OMP_DECLARE
45875 || (strcmp (directive[1], "simd") != 0
45876 && strcmp (directive[1], "variant") != 0))
45878 error_at (first->location,
45879 "OpenMP directive other than %<declare simd%> "
45880 "or %<declare variant%> appertains to a "
45881 "declaration");
45882 continue;
45885 if (parser->omp_attrs_forbidden_p)
45887 error_at (first->location,
45888 "mixing OpenMP directives with attribute and "
45889 "pragma syntax on the same statement");
45890 parser->omp_attrs_forbidden_p = false;
45893 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
45894 continue;
45895 if (lexer == NULL)
45897 lexer = cp_lexer_alloc ();
45898 lexer->debugging_p = parser->lexer->debugging_p;
45900 vec_safe_reserve (lexer->buffer, (last - first) + 2);
45901 cp_token tok = {};
45902 tok.type = CPP_PRAGMA;
45903 tok.keyword = RID_MAX;
45904 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
45905 tok.location = first->location;
45906 lexer->buffer->quick_push (tok);
45907 while (++first < last)
45908 lexer->buffer->quick_push (*first);
45909 tok = {};
45910 tok.type = CPP_PRAGMA_EOL;
45911 tok.keyword = RID_MAX;
45912 tok.location = last->location;
45913 lexer->buffer->quick_push (tok);
45914 tok = {};
45915 tok.type = CPP_EOF;
45916 tok.keyword = RID_MAX;
45917 tok.location = last->location;
45918 lexer->buffer->quick_push (tok);
45919 lexer->next = parser->lexer;
45920 lexer->next_token = lexer->buffer->address ();
45921 lexer->last_token = lexer->next_token
45922 + lexer->buffer->length ()
45923 - 1;
45924 lexer->in_omp_attribute_pragma = true;
45925 parser->lexer = lexer;
45926 /* Move the current source position to that of the first token
45927 in the new lexer. */
45928 cp_lexer_set_source_position_from_token (lexer->next_token);
45930 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
45931 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45932 const char *kind = IDENTIFIER_POINTER (id);
45933 cp_lexer_consume_token (parser->lexer);
45935 tree c, cl;
45936 if (strcmp (kind, "simd") == 0)
45938 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45939 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45940 cp_lexer_consume_token (parser->lexer);
45942 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
45943 cl = cp_parser_omp_all_clauses (parser, mask,
45944 "#pragma omp declare simd",
45945 pragma_tok);
45946 if (cl)
45947 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
45948 c = build_tree_list (get_identifier ("omp declare simd"),
45949 cl);
45950 TREE_CHAIN (c) = attrs;
45951 if (processing_template_decl)
45952 ATTR_IS_DEPENDENT (c) = 1;
45953 attrs = c;
45955 else
45957 gcc_assert (strcmp (kind, "variant") == 0);
45958 attrs
45959 = cp_finish_omp_declare_variant (parser, pragma_tok,
45960 attrs);
45962 gcc_assert (parser->lexer != lexer);
45963 vec_safe_truncate (lexer->buffer, 0);
45965 *pa = TREE_CHAIN (*pa);
45967 else
45968 pa = &TREE_CHAIN (*pa);
45970 if (lexer)
45971 cp_lexer_destroy (lexer);
45973 data->fndecl_seen = true;
45974 return attrs;
45977 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
45978 on #pragma omp declare target. Return false if errors were reported. */
45980 static bool
45981 handle_omp_declare_target_clause (tree c, tree t, int device_type)
45983 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
45984 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
45985 tree id;
45986 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
45988 id = get_identifier ("omp declare target link");
45989 std::swap (at1, at2);
45991 else
45992 id = get_identifier ("omp declare target");
45993 if (at2)
45995 error_at (OMP_CLAUSE_LOCATION (c),
45996 "%qD specified both in declare target %<link%> and %<to%>"
45997 " clauses", t);
45998 return false;
46000 if (!at1)
46002 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
46003 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
46004 return true;
46006 symtab_node *node = symtab_node::get (t);
46007 if (node != NULL)
46009 node->offloadable = 1;
46010 if (ENABLE_OFFLOADING)
46012 g->have_offload = true;
46013 if (is_a <varpool_node *> (node))
46014 vec_safe_push (offload_vars, t);
46018 if (TREE_CODE (t) != FUNCTION_DECL)
46019 return true;
46020 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
46022 tree at3 = lookup_attribute ("omp declare target host",
46023 DECL_ATTRIBUTES (t));
46024 if (at3 == NULL_TREE)
46026 id = get_identifier ("omp declare target host");
46027 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
46030 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
46032 tree at3 = lookup_attribute ("omp declare target nohost",
46033 DECL_ATTRIBUTES (t));
46034 if (at3 == NULL_TREE)
46036 id = get_identifier ("omp declare target nohost");
46037 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
46040 return true;
46043 /* OpenMP 4.0:
46044 # pragma omp declare target new-line
46045 declarations and definitions
46046 # pragma omp end declare target new-line
46048 OpenMP 4.5:
46049 # pragma omp declare target ( extended-list ) new-line
46051 # pragma omp declare target declare-target-clauses[seq] new-line */
46053 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
46054 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
46055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
46056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
46057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
46059 static void
46060 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
46062 tree clauses = NULL_TREE;
46063 int device_type = 0;
46064 bool only_device_type = true;
46065 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
46066 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46067 || (parser->lexer->in_omp_attribute_pragma
46068 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46069 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
46070 clauses
46071 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
46072 "#pragma omp declare target", pragma_tok);
46073 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46075 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
46076 clauses);
46077 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
46078 cp_parser_require_pragma_eol (parser, pragma_tok);
46080 else
46082 struct omp_declare_target_attr a
46083 = { parser->lexer->in_omp_attribute_pragma };
46084 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
46085 cp_parser_require_pragma_eol (parser, pragma_tok);
46086 return;
46088 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
46089 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
46090 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
46091 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
46093 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
46094 continue;
46095 tree t = OMP_CLAUSE_DECL (c);
46096 only_device_type = false;
46097 if (!handle_omp_declare_target_clause (c, t, device_type))
46098 continue;
46099 if (VAR_OR_FUNCTION_DECL_P (t)
46100 && DECL_LOCAL_DECL_P (t)
46101 && DECL_LANG_SPECIFIC (t)
46102 && DECL_LOCAL_DECL_ALIAS (t)
46103 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
46104 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
46105 device_type);
46107 if (device_type && only_device_type)
46108 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
46109 "directive with only %<device_type%> clauses ignored");
46112 static void
46113 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
46115 const char *p = "";
46116 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
46117 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46119 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46120 p = IDENTIFIER_POINTER (id);
46122 if (strcmp (p, "declare") == 0)
46124 cp_lexer_consume_token (parser->lexer);
46125 p = "";
46126 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46128 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46129 p = IDENTIFIER_POINTER (id);
46131 if (strcmp (p, "target") == 0)
46132 cp_lexer_consume_token (parser->lexer);
46133 else
46135 cp_parser_error (parser, "expected %<target%>");
46136 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46137 return;
46140 else
46142 cp_parser_error (parser, "expected %<declare%>");
46143 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46144 return;
46146 cp_parser_require_pragma_eol (parser, pragma_tok);
46147 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
46148 error_at (pragma_tok->location,
46149 "%<#pragma omp end declare target%> without corresponding "
46150 "%<#pragma omp declare target%>");
46151 else
46153 omp_declare_target_attr
46154 a = scope_chain->omp_declare_target_attribute->pop ();
46155 if (a.attr_syntax != in_omp_attribute_pragma)
46157 if (a.attr_syntax)
46158 error_at (pragma_tok->location,
46159 "%<declare target%> in attribute syntax terminated "
46160 "with %<end declare target%> in pragma syntax");
46161 else
46162 error_at (pragma_tok->location,
46163 "%<declare target%> in pragma syntax terminated "
46164 "with %<end declare target%> in attribute syntax");
46169 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
46170 expression and optional initializer clause of
46171 #pragma omp declare reduction. We store the expression(s) as
46172 either 3, 6 or 7 special statements inside of the artificial function's
46173 body. The first two statements are DECL_EXPRs for the artificial
46174 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
46175 expression that uses those variables.
46176 If there was any INITIALIZER clause, this is followed by further statements,
46177 the fourth and fifth statements are DECL_EXPRs for the artificial
46178 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
46179 constructor variant (first token after open paren is not omp_priv),
46180 then the sixth statement is a statement with the function call expression
46181 that uses the OMP_PRIV and optionally OMP_ORIG variable.
46182 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
46183 to initialize the OMP_PRIV artificial variable and there is seventh
46184 statement, a DECL_EXPR of the OMP_PRIV statement again. */
46186 static bool
46187 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
46189 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
46190 gcc_assert (TYPE_REF_P (type));
46191 type = TREE_TYPE (type);
46192 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
46193 DECL_ARTIFICIAL (omp_out) = 1;
46194 pushdecl (omp_out);
46195 add_decl_expr (omp_out);
46196 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
46197 DECL_ARTIFICIAL (omp_in) = 1;
46198 pushdecl (omp_in);
46199 add_decl_expr (omp_in);
46200 tree combiner;
46201 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
46203 keep_next_level (true);
46204 tree block = begin_omp_structured_block ();
46205 combiner = cp_parser_expression (parser);
46206 finish_expr_stmt (combiner);
46207 block = finish_omp_structured_block (block);
46208 if (processing_template_decl)
46209 block = build_stmt (input_location, EXPR_STMT, block);
46210 add_stmt (block);
46212 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
46213 return false;
46215 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46216 if (parser->lexer->in_omp_attribute_pragma
46217 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46218 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46219 cp_lexer_consume_token (parser->lexer);
46221 const char *p = "";
46222 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46224 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46225 p = IDENTIFIER_POINTER (id);
46228 if (strcmp (p, "initializer") == 0)
46230 cp_lexer_consume_token (parser->lexer);
46231 matching_parens parens;
46232 if (!parens.require_open (parser))
46233 return false;
46235 p = "";
46236 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46238 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46239 p = IDENTIFIER_POINTER (id);
46242 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
46243 DECL_ARTIFICIAL (omp_priv) = 1;
46244 pushdecl (omp_priv);
46245 add_decl_expr (omp_priv);
46246 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
46247 DECL_ARTIFICIAL (omp_orig) = 1;
46248 pushdecl (omp_orig);
46249 add_decl_expr (omp_orig);
46251 keep_next_level (true);
46252 block = begin_omp_structured_block ();
46254 bool ctor = false;
46255 if (strcmp (p, "omp_priv") == 0)
46257 bool is_direct_init, is_non_constant_init;
46258 ctor = true;
46259 cp_lexer_consume_token (parser->lexer);
46260 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
46261 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
46262 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
46263 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
46264 == CPP_CLOSE_PAREN
46265 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
46266 == CPP_CLOSE_PAREN))
46268 finish_omp_structured_block (block);
46269 error ("invalid initializer clause");
46270 return false;
46272 initializer = cp_parser_initializer (parser, &is_direct_init,
46273 &is_non_constant_init);
46274 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
46275 NULL_TREE, LOOKUP_ONLYCONVERTING);
46277 else
46279 cp_parser_parse_tentatively (parser);
46280 /* Don't create location wrapper nodes here. */
46281 auto_suppress_location_wrappers sentinel;
46282 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
46283 /*check_dependency_p=*/true,
46284 /*template_p=*/NULL,
46285 /*declarator_p=*/false,
46286 /*optional_p=*/false);
46287 vec<tree, va_gc> *args;
46288 if (fn_name == error_mark_node
46289 || cp_parser_error_occurred (parser)
46290 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
46291 || ((args = cp_parser_parenthesized_expression_list
46292 (parser, non_attr, /*cast_p=*/false,
46293 /*allow_expansion_p=*/true,
46294 /*non_constant_p=*/NULL)),
46295 cp_parser_error_occurred (parser)))
46297 finish_omp_structured_block (block);
46298 cp_parser_abort_tentative_parse (parser);
46299 cp_parser_error (parser, "expected id-expression (arguments)");
46300 return false;
46302 unsigned int i;
46303 tree arg;
46304 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
46305 if (arg == omp_priv
46306 || (TREE_CODE (arg) == ADDR_EXPR
46307 && TREE_OPERAND (arg, 0) == omp_priv))
46308 break;
46309 cp_parser_abort_tentative_parse (parser);
46310 if (arg == NULL_TREE)
46311 error ("one of the initializer call arguments should be %<omp_priv%>"
46312 " or %<&omp_priv%>");
46313 initializer = cp_parser_postfix_expression (parser, false, false, false,
46314 false, NULL);
46315 finish_expr_stmt (initializer);
46318 block = finish_omp_structured_block (block);
46319 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
46320 if (processing_template_decl)
46321 block = build_stmt (input_location, EXPR_STMT, block);
46322 add_stmt (block);
46324 if (ctor)
46325 add_decl_expr (omp_orig);
46327 if (!parens.require_close (parser))
46328 return false;
46331 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
46332 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
46333 UNKNOWN_LOCATION);
46335 return true;
46338 /* OpenMP 4.0
46339 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46340 initializer-clause[opt] new-line
46342 initializer-clause:
46343 initializer (omp_priv initializer)
46344 initializer (function-name (argument-list)) */
46346 static void
46347 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
46348 enum pragma_context)
46350 auto_vec<tree> types;
46351 enum tree_code reduc_code = ERROR_MARK;
46352 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
46353 unsigned int i;
46354 cp_token *first_token;
46355 cp_token_cache *cp;
46356 int errs;
46357 void *p;
46359 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
46360 p = obstack_alloc (&declarator_obstack, 0);
46362 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
46363 goto fail;
46365 switch (cp_lexer_peek_token (parser->lexer)->type)
46367 case CPP_PLUS:
46368 reduc_code = PLUS_EXPR;
46369 break;
46370 case CPP_MULT:
46371 reduc_code = MULT_EXPR;
46372 break;
46373 case CPP_MINUS:
46374 reduc_code = MINUS_EXPR;
46375 break;
46376 case CPP_AND:
46377 reduc_code = BIT_AND_EXPR;
46378 break;
46379 case CPP_XOR:
46380 reduc_code = BIT_XOR_EXPR;
46381 break;
46382 case CPP_OR:
46383 reduc_code = BIT_IOR_EXPR;
46384 break;
46385 case CPP_AND_AND:
46386 reduc_code = TRUTH_ANDIF_EXPR;
46387 break;
46388 case CPP_OR_OR:
46389 reduc_code = TRUTH_ORIF_EXPR;
46390 break;
46391 case CPP_NAME:
46392 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
46393 break;
46394 default:
46395 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
46396 "%<|%>, %<&&%>, %<||%> or identifier");
46397 goto fail;
46400 if (reduc_code != ERROR_MARK)
46401 cp_lexer_consume_token (parser->lexer);
46403 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
46404 if (reduc_id == error_mark_node)
46405 goto fail;
46407 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
46408 goto fail;
46410 /* Types may not be defined in declare reduction type list. */
46411 const char *saved_message;
46412 saved_message = parser->type_definition_forbidden_message;
46413 parser->type_definition_forbidden_message
46414 = G_("types may not be defined in declare reduction type list");
46415 bool saved_colon_corrects_to_scope_p;
46416 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
46417 parser->colon_corrects_to_scope_p = false;
46418 bool saved_colon_doesnt_start_class_def_p;
46419 saved_colon_doesnt_start_class_def_p
46420 = parser->colon_doesnt_start_class_def_p;
46421 parser->colon_doesnt_start_class_def_p = true;
46423 while (true)
46425 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46426 type = cp_parser_type_id (parser);
46427 if (type == error_mark_node)
46429 else if (ARITHMETIC_TYPE_P (type)
46430 && (orig_reduc_id == NULL_TREE
46431 || (TREE_CODE (type) != COMPLEX_TYPE
46432 && (id_equal (orig_reduc_id, "min")
46433 || id_equal (orig_reduc_id, "max")))))
46434 error_at (loc, "predeclared arithmetic type %qT in "
46435 "%<#pragma omp declare reduction%>", type);
46436 else if (FUNC_OR_METHOD_TYPE_P (type)
46437 || TREE_CODE (type) == ARRAY_TYPE)
46438 error_at (loc, "function or array type %qT in "
46439 "%<#pragma omp declare reduction%>", type);
46440 else if (TYPE_REF_P (type))
46441 error_at (loc, "reference type %qT in "
46442 "%<#pragma omp declare reduction%>", type);
46443 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
46444 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
46445 "type %qT in %<#pragma omp declare reduction%>", type);
46446 else
46447 types.safe_push (type);
46449 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46450 cp_lexer_consume_token (parser->lexer);
46451 else
46452 break;
46455 /* Restore the saved message. */
46456 parser->type_definition_forbidden_message = saved_message;
46457 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
46458 parser->colon_doesnt_start_class_def_p
46459 = saved_colon_doesnt_start_class_def_p;
46461 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
46462 || types.is_empty ())
46464 fail:
46465 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46466 goto done;
46469 first_token = cp_lexer_peek_token (parser->lexer);
46470 cp = NULL;
46471 errs = errorcount;
46472 FOR_EACH_VEC_ELT (types, i, type)
46474 tree fntype
46475 = build_function_type_list (void_type_node,
46476 cp_build_reference_type (type, false),
46477 NULL_TREE);
46478 tree this_reduc_id = reduc_id;
46479 if (!dependent_type_p (type))
46480 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
46481 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
46482 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
46483 DECL_ARTIFICIAL (fndecl) = 1;
46484 DECL_EXTERNAL (fndecl) = 1;
46485 DECL_DECLARED_INLINE_P (fndecl) = 1;
46486 DECL_IGNORED_P (fndecl) = 1;
46487 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
46488 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
46489 DECL_ATTRIBUTES (fndecl)
46490 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
46491 DECL_ATTRIBUTES (fndecl));
46492 bool block_scope = false;
46493 if (current_function_decl)
46495 block_scope = true;
46496 DECL_CONTEXT (fndecl) = current_function_decl;
46497 DECL_LOCAL_DECL_P (fndecl) = true;
46500 if (processing_template_decl)
46501 fndecl = push_template_decl (fndecl);
46503 if (block_scope)
46505 if (!processing_template_decl)
46506 pushdecl (fndecl);
46508 else if (current_class_type)
46510 if (cp == NULL)
46512 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46513 cp_lexer_consume_token (parser->lexer);
46514 cp = cp_token_cache_new (first_token,
46515 cp_lexer_peek_nth_token (parser->lexer,
46516 2));
46518 DECL_STATIC_FUNCTION_P (fndecl) = 1;
46519 finish_member_declaration (fndecl);
46520 DECL_PENDING_INLINE_INFO (fndecl) = cp;
46521 DECL_PENDING_INLINE_P (fndecl) = 1;
46522 vec_safe_push (unparsed_funs_with_definitions, fndecl);
46523 continue;
46525 else
46527 DECL_CONTEXT (fndecl) = current_namespace;
46528 tree d = pushdecl (fndecl);
46529 /* We should never meet a matched duplicate decl. */
46530 gcc_checking_assert (d == error_mark_node || d == fndecl);
46533 tree block = NULL_TREE;
46534 if (!block_scope)
46535 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
46536 else
46537 block = begin_omp_structured_block ();
46538 if (cp)
46540 cp_parser_push_lexer_for_tokens (parser, cp);
46541 parser->lexer->in_pragma = true;
46544 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
46546 if (cp)
46547 cp_parser_pop_lexer (parser);
46548 if (!block_scope)
46549 finish_function (/*inline_p=*/false);
46550 else
46552 DECL_CONTEXT (fndecl) = current_function_decl;
46553 if (DECL_TEMPLATE_INFO (fndecl))
46554 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
46556 if (!ok)
46557 goto fail;
46559 if (block_scope)
46561 block = finish_omp_structured_block (block);
46562 if (TREE_CODE (block) == BIND_EXPR)
46563 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
46564 else if (TREE_CODE (block) == STATEMENT_LIST)
46565 DECL_SAVED_TREE (fndecl) = block;
46566 if (processing_template_decl)
46567 add_decl_expr (fndecl);
46570 cp_check_omp_declare_reduction (fndecl);
46571 if (cp == NULL && types.length () > 1)
46572 cp = cp_token_cache_new (first_token,
46573 cp_lexer_peek_nth_token (parser->lexer, 2));
46574 if (errs != errorcount)
46575 break;
46578 cp_parser_require_pragma_eol (parser, pragma_tok);
46580 done:
46581 /* Free any declarators allocated. */
46582 obstack_free (&declarator_obstack, p);
46585 /* OpenMP 4.0
46586 #pragma omp declare simd declare-simd-clauses[optseq] new-line
46587 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46588 initializer-clause[opt] new-line
46589 #pragma omp declare target new-line
46591 OpenMP 5.0
46592 #pragma omp declare variant (identifier) match (context-selector) */
46594 static bool
46595 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
46596 enum pragma_context context)
46598 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46600 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46601 const char *p = IDENTIFIER_POINTER (id);
46603 if (strcmp (p, "simd") == 0)
46605 cp_lexer_consume_token (parser->lexer);
46606 cp_parser_omp_declare_simd (parser, pragma_tok,
46607 context, false);
46608 return true;
46610 if (flag_openmp && strcmp (p, "variant") == 0)
46612 cp_lexer_consume_token (parser->lexer);
46613 cp_parser_omp_declare_simd (parser, pragma_tok,
46614 context, true);
46615 return true;
46617 cp_ensure_no_omp_declare_simd (parser);
46618 if (strcmp (p, "reduction") == 0)
46620 cp_lexer_consume_token (parser->lexer);
46621 cp_parser_omp_declare_reduction (parser, pragma_tok,
46622 context);
46623 return false;
46625 if (!flag_openmp) /* flag_openmp_simd */
46627 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46628 return false;
46630 if (strcmp (p, "target") == 0)
46632 cp_lexer_consume_token (parser->lexer);
46633 cp_parser_omp_declare_target (parser, pragma_tok);
46634 return false;
46637 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
46638 "%<target%> or %<variant%>");
46639 cp_parser_require_pragma_eol (parser, pragma_tok);
46640 return false;
46643 /* OpenMP 5.0
46644 #pragma omp requires clauses[optseq] new-line */
46646 static bool
46647 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
46649 bool first = true;
46650 enum omp_requires new_req = (enum omp_requires) 0;
46652 location_t loc = pragma_tok->location;
46653 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46655 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46656 if ((!first || parser->lexer->in_omp_attribute_pragma)
46657 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46658 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46659 cp_lexer_consume_token (parser->lexer);
46661 first = false;
46663 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46665 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46666 const char *p = IDENTIFIER_POINTER (id);
46667 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46668 enum omp_requires this_req = (enum omp_requires) 0;
46670 if (!strcmp (p, "unified_address"))
46671 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
46672 else if (!strcmp (p, "unified_shared_memory"))
46673 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
46674 else if (!strcmp (p, "dynamic_allocators"))
46675 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
46676 else if (!strcmp (p, "reverse_offload"))
46677 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
46678 else if (!strcmp (p, "atomic_default_mem_order"))
46680 cp_lexer_consume_token (parser->lexer);
46682 matching_parens parens;
46683 if (parens.require_open (parser))
46685 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46687 id = cp_lexer_peek_token (parser->lexer)->u.value;
46688 p = IDENTIFIER_POINTER (id);
46690 if (!strcmp (p, "seq_cst"))
46691 this_req
46692 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
46693 else if (!strcmp (p, "relaxed"))
46694 this_req
46695 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
46696 else if (!strcmp (p, "acq_rel"))
46697 this_req
46698 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
46700 if (this_req == 0)
46702 error_at (cp_lexer_peek_token (parser->lexer)->location,
46703 "expected %<seq_cst%>, %<relaxed%> or "
46704 "%<acq_rel%>");
46705 switch (cp_lexer_peek_token (parser->lexer)->type)
46707 case CPP_EOF:
46708 case CPP_PRAGMA_EOL:
46709 case CPP_CLOSE_PAREN:
46710 break;
46711 default:
46712 if (cp_lexer_nth_token_is (parser->lexer, 2,
46713 CPP_CLOSE_PAREN))
46714 cp_lexer_consume_token (parser->lexer);
46715 break;
46718 else
46719 cp_lexer_consume_token (parser->lexer);
46721 if (!parens.require_close (parser))
46722 cp_parser_skip_to_closing_parenthesis (parser,
46723 /*recovering=*/true,
46724 /*or_comma=*/false,
46725 /*consume_paren=*/
46726 true);
46728 if (this_req == 0)
46730 cp_parser_require_pragma_eol (parser, pragma_tok);
46731 return false;
46734 p = NULL;
46736 else
46738 error_at (cloc, "expected %<unified_address%>, "
46739 "%<unified_shared_memory%>, "
46740 "%<dynamic_allocators%>, "
46741 "%<reverse_offload%> "
46742 "or %<atomic_default_mem_order%> clause");
46743 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46744 return false;
46746 if (p && this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS)
46747 sorry_at (cloc, "%qs clause on %<requires%> directive not "
46748 "supported yet", p);
46749 if (p)
46750 cp_lexer_consume_token (parser->lexer);
46751 if (this_req)
46753 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46755 if ((this_req & new_req) != 0)
46756 error_at (cloc, "too many %qs clauses", p);
46757 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
46758 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
46759 error_at (cloc, "%qs clause used lexically after first "
46760 "target construct or offloading API", p);
46762 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46764 error_at (cloc, "too many %qs clauses",
46765 "atomic_default_mem_order");
46766 this_req = (enum omp_requires) 0;
46768 else if ((omp_requires_mask
46769 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46771 error_at (cloc, "more than one %<atomic_default_mem_order%>"
46772 " clause in a single compilation unit");
46773 this_req
46774 = (enum omp_requires)
46775 (omp_requires_mask
46776 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
46778 else if ((omp_requires_mask
46779 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
46780 error_at (cloc, "%<atomic_default_mem_order%> clause used "
46781 "lexically after first %<atomic%> construct "
46782 "without memory order clause");
46783 new_req = (enum omp_requires) (new_req | this_req);
46784 omp_requires_mask
46785 = (enum omp_requires) (omp_requires_mask | this_req);
46786 continue;
46789 break;
46791 cp_parser_require_pragma_eol (parser, pragma_tok);
46793 if (new_req == 0)
46794 error_at (loc, "%<pragma omp requires%> requires at least one clause");
46795 return false;
46799 /* OpenMP 5.1:
46800 #pragma omp nothing new-line */
46802 static void
46803 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
46805 cp_parser_require_pragma_eol (parser, pragma_tok);
46809 /* OpenMP 5.1
46810 #pragma omp error clauses[optseq] new-line */
46812 static bool
46813 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
46814 enum pragma_context context)
46816 int at_compilation = -1;
46817 int severity_fatal = -1;
46818 tree message = NULL_TREE;
46819 bool first = true;
46820 bool bad = false;
46821 location_t loc = pragma_tok->location;
46823 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46825 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46826 if ((!first || parser->lexer->in_omp_attribute_pragma)
46827 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46828 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46829 cp_lexer_consume_token (parser->lexer);
46831 first = false;
46833 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
46834 break;
46836 const char *p
46837 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46838 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46839 static const char *args[] = {
46840 "execution", "compilation", "warning", "fatal"
46842 int *v = NULL;
46843 int idx = 0, n = -1;
46844 tree m = NULL_TREE;
46846 if (!strcmp (p, "at"))
46847 v = &at_compilation;
46848 else if (!strcmp (p, "severity"))
46850 v = &severity_fatal;
46851 idx += 2;
46853 else if (strcmp (p, "message"))
46855 error_at (cloc,
46856 "expected %<at%>, %<severity%> or %<message%> clause");
46857 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46858 return false;
46861 cp_lexer_consume_token (parser->lexer);
46863 matching_parens parens;
46864 if (parens.require_open (parser))
46866 if (v == NULL)
46868 m = cp_parser_assignment_expression (parser);
46869 if (type_dependent_expression_p (m))
46870 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
46871 else
46872 m = perform_implicit_conversion_flags (const_string_type_node, m,
46873 tf_warning_or_error,
46874 LOOKUP_NORMAL);
46876 else
46878 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46880 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
46881 const char *q = IDENTIFIER_POINTER (val);
46883 if (!strcmp (q, args[idx]))
46884 n = 0;
46885 else if (!strcmp (q, args[idx + 1]))
46886 n = 1;
46888 if (n == -1)
46890 error_at (cp_lexer_peek_token (parser->lexer)->location,
46891 "expected %qs or %qs", args[idx], args[idx + 1]);
46892 bad = true;
46893 switch (cp_lexer_peek_token (parser->lexer)->type)
46895 case CPP_EOF:
46896 case CPP_PRAGMA_EOL:
46897 case CPP_CLOSE_PAREN:
46898 break;
46899 default:
46900 if (cp_lexer_nth_token_is (parser->lexer, 2,
46901 CPP_CLOSE_PAREN))
46902 cp_lexer_consume_token (parser->lexer);
46903 break;
46906 else
46907 cp_lexer_consume_token (parser->lexer);
46910 if (!parens.require_close (parser))
46911 cp_parser_skip_to_closing_parenthesis (parser,
46912 /*recovering=*/true,
46913 /*or_comma=*/false,
46914 /*consume_paren=*/
46915 true);
46917 if (v == NULL)
46919 if (message)
46921 error_at (cloc, "too many %qs clauses", p);
46922 bad = true;
46924 else
46925 message = m;
46927 else if (n != -1)
46929 if (*v != -1)
46931 error_at (cloc, "too many %qs clauses", p);
46932 bad = true;
46934 else
46935 *v = n;
46938 else
46939 bad = true;
46941 cp_parser_require_pragma_eol (parser, pragma_tok);
46942 if (bad)
46943 return true;
46945 if (at_compilation == -1)
46946 at_compilation = 1;
46947 if (severity_fatal == -1)
46948 severity_fatal = 1;
46949 if (!at_compilation)
46951 if (context != pragma_compound)
46953 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
46954 "may only be used in compound statements");
46955 return true;
46957 tree fndecl
46958 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
46959 : BUILT_IN_GOMP_WARNING);
46960 if (!message)
46961 message = build_zero_cst (const_string_type_node);
46962 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
46963 build_all_ones_cst (size_type_node));
46964 add_stmt (stmt);
46965 return true;
46968 if (in_discarded_stmt)
46969 return false;
46971 const char *msg = NULL;
46972 if (message)
46974 msg = c_getstr (fold_for_warn (message));
46975 if (msg == NULL)
46976 msg = _("<message unknown at compile time>");
46978 if (msg)
46979 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
46980 "%<pragma omp error%> encountered: %s", msg);
46981 else
46982 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
46983 "%<pragma omp error%> encountered");
46984 return false;
46987 /* OpenMP 4.5:
46988 #pragma omp taskloop taskloop-clause[optseq] new-line
46989 for-loop
46991 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
46992 for-loop */
46994 #define OMP_TASKLOOP_CLAUSE_MASK \
46995 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
47000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
47001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
47002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
47003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
47004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
47005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
47006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
47007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
47008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
47009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
47010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
47011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
47013 static tree
47014 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
47015 char *p_name, omp_clause_mask mask, tree *cclauses,
47016 bool *if_p)
47018 tree clauses, sb, ret;
47019 unsigned int save;
47020 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47022 strcat (p_name, " taskloop");
47023 mask |= OMP_TASKLOOP_CLAUSE_MASK;
47024 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
47025 clause. */
47026 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
47027 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
47029 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47031 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47032 const char *p = IDENTIFIER_POINTER (id);
47034 if (strcmp (p, "simd") == 0)
47036 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
47037 if (cclauses == NULL)
47038 cclauses = cclauses_buf;
47040 cp_lexer_consume_token (parser->lexer);
47041 if (!flag_openmp) /* flag_openmp_simd */
47042 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
47043 cclauses, if_p);
47044 sb = begin_omp_structured_block ();
47045 save = cp_parser_begin_omp_structured_block (parser);
47046 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
47047 cclauses, if_p);
47048 cp_parser_end_omp_structured_block (parser, save);
47049 tree body = finish_omp_structured_block (sb);
47050 if (ret == NULL)
47051 return ret;
47052 ret = make_node (OMP_TASKLOOP);
47053 TREE_TYPE (ret) = void_type_node;
47054 OMP_FOR_BODY (ret) = body;
47055 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
47056 SET_EXPR_LOCATION (ret, loc);
47057 add_stmt (ret);
47058 return ret;
47061 if (!flag_openmp) /* flag_openmp_simd */
47063 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47064 return NULL_TREE;
47067 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
47068 cclauses == NULL);
47069 if (cclauses)
47071 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
47072 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
47075 keep_next_level (true);
47076 sb = begin_omp_structured_block ();
47077 save = cp_parser_begin_omp_structured_block (parser);
47079 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
47080 if_p);
47082 cp_parser_end_omp_structured_block (parser, save);
47083 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
47085 return ret;
47089 /* OpenACC 2.0:
47090 # pragma acc routine oacc-routine-clause[optseq] new-line
47091 function-definition
47093 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
47096 #define OACC_ROUTINE_CLAUSE_MASK \
47097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
47098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
47099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
47100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
47101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
47103 /* Parse the OpenACC routine pragma. This has an optional '( name )'
47104 component, which must resolve to a declared namespace-scope
47105 function. The clauses are either processed directly (for a named
47106 function), or defered until the immediatley following declaration
47107 is parsed. */
47109 static void
47110 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
47111 enum pragma_context context)
47113 gcc_checking_assert (context == pragma_external);
47114 /* The checking for "another pragma following this one" in the "no optional
47115 '( name )'" case makes sure that we dont re-enter. */
47116 gcc_checking_assert (parser->oacc_routine == NULL);
47118 cp_oacc_routine_data data;
47119 data.error_seen = false;
47120 data.fndecl_seen = false;
47121 data.tokens = vNULL;
47122 data.clauses = NULL_TREE;
47123 data.loc = pragma_tok->location;
47124 /* It is safe to take the address of a local variable; it will only be
47125 used while this scope is live. */
47126 parser->oacc_routine = &data;
47128 /* Look for optional '( name )'. */
47129 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47131 matching_parens parens;
47132 parens.consume_open (parser); /* '(' */
47134 /* We parse the name as an id-expression. If it resolves to
47135 anything other than a non-overloaded function at namespace
47136 scope, it's an error. */
47137 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
47138 tree name = cp_parser_id_expression (parser,
47139 /*template_keyword_p=*/false,
47140 /*check_dependency_p=*/false,
47141 /*template_p=*/NULL,
47142 /*declarator_p=*/false,
47143 /*optional_p=*/false);
47144 tree decl = (identifier_p (name)
47145 ? cp_parser_lookup_name_simple (parser, name, name_loc)
47146 : name);
47147 if (name != error_mark_node && decl == error_mark_node)
47148 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
47150 if (decl == error_mark_node
47151 || !parens.require_close (parser))
47153 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47154 parser->oacc_routine = NULL;
47155 return;
47158 data.clauses
47159 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
47160 "#pragma acc routine",
47161 cp_lexer_peek_token (parser->lexer));
47162 /* The clauses are in reverse order; fix that to make later diagnostic
47163 emission easier. */
47164 data.clauses = nreverse (data.clauses);
47166 if (decl && is_overloaded_fn (decl)
47167 && (TREE_CODE (decl) != FUNCTION_DECL
47168 || DECL_FUNCTION_TEMPLATE_P (decl)))
47170 error_at (name_loc,
47171 "%<#pragma acc routine%> names a set of overloads");
47172 parser->oacc_routine = NULL;
47173 return;
47176 /* Perhaps we should use the same rule as declarations in different
47177 namespaces? */
47178 if (!DECL_NAMESPACE_SCOPE_P (decl))
47180 error_at (name_loc,
47181 "%qD does not refer to a namespace scope function", decl);
47182 parser->oacc_routine = NULL;
47183 return;
47186 if (TREE_CODE (decl) != FUNCTION_DECL)
47188 error_at (name_loc, "%qD does not refer to a function", decl);
47189 parser->oacc_routine = NULL;
47190 return;
47193 cp_finalize_oacc_routine (parser, decl, false);
47194 parser->oacc_routine = NULL;
47196 else /* No optional '( name )'. */
47198 /* Store away all pragma tokens. */
47199 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47200 cp_lexer_consume_token (parser->lexer);
47201 cp_parser_require_pragma_eol (parser, pragma_tok);
47202 struct cp_token_cache *cp
47203 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
47204 parser->oacc_routine->tokens.safe_push (cp);
47206 /* Emit a helpful diagnostic if there's another pragma following this
47207 one. */
47208 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
47210 cp_ensure_no_oacc_routine (parser);
47211 data.tokens.release ();
47212 /* ..., and then just keep going. */
47213 return;
47216 /* We only have to consider the pragma_external case here. */
47217 cp_parser_declaration (parser, NULL_TREE);
47218 if (parser->oacc_routine
47219 && !parser->oacc_routine->fndecl_seen)
47220 cp_ensure_no_oacc_routine (parser);
47221 else
47222 parser->oacc_routine = NULL;
47223 data.tokens.release ();
47227 /* Finalize #pragma acc routine clauses after direct declarator has
47228 been parsed. */
47230 static tree
47231 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
47233 struct cp_token_cache *ce;
47234 cp_oacc_routine_data *data = parser->oacc_routine;
47236 if (!data->error_seen && data->fndecl_seen)
47238 error_at (data->loc,
47239 "%<#pragma acc routine%> not immediately followed by "
47240 "a single function declaration or definition");
47241 data->error_seen = true;
47243 if (data->error_seen)
47244 return attrs;
47246 gcc_checking_assert (data->tokens.length () == 1);
47247 ce = data->tokens[0];
47249 cp_parser_push_lexer_for_tokens (parser, ce);
47250 parser->lexer->in_pragma = true;
47251 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
47253 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47254 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
47255 parser->oacc_routine->clauses
47256 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
47257 "#pragma acc routine", pragma_tok);
47258 /* The clauses are in reverse order; fix that to make later diagnostic
47259 emission easier. */
47260 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
47261 cp_parser_pop_lexer (parser);
47262 /* Later, cp_finalize_oacc_routine will process the clauses. */
47263 parser->oacc_routine->fndecl_seen = true;
47265 return attrs;
47268 /* Apply any saved OpenACC routine clauses to a just-parsed
47269 declaration. */
47271 static void
47272 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
47274 if (UNLIKELY (parser->oacc_routine != NULL))
47276 /* Keep going if we're in error reporting mode. */
47277 if (parser->oacc_routine->error_seen
47278 || fndecl == error_mark_node)
47279 return;
47281 if (TREE_CODE (fndecl) != FUNCTION_DECL)
47283 if (parser->oacc_routine->fndecl_seen)
47285 error_at (parser->oacc_routine->loc,
47286 "%<#pragma acc routine%> not immediately followed by"
47287 " a single function declaration or definition");
47288 parser->oacc_routine = NULL;
47289 return;
47292 cp_ensure_no_oacc_routine (parser);
47293 return;
47296 int compatible
47297 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
47298 parser->oacc_routine->loc,
47299 "#pragma acc routine");
47300 if (compatible < 0)
47302 parser->oacc_routine = NULL;
47303 return;
47305 if (compatible > 0)
47308 else
47310 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
47312 error_at (parser->oacc_routine->loc,
47313 TREE_USED (fndecl)
47314 ? G_("%<#pragma acc routine%> must be applied before"
47315 " use")
47316 : G_("%<#pragma acc routine%> must be applied before"
47317 " definition"));
47318 parser->oacc_routine = NULL;
47319 return;
47322 /* Set the routine's level of parallelism. */
47323 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
47324 oacc_replace_fn_attrib (fndecl, dims);
47326 /* Add an "omp declare target" attribute. */
47327 DECL_ATTRIBUTES (fndecl)
47328 = tree_cons (get_identifier ("omp declare target"),
47329 parser->oacc_routine->clauses,
47330 DECL_ATTRIBUTES (fndecl));
47335 /* Main entry point to OpenMP statement pragmas. */
47337 static void
47338 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47340 tree stmt;
47341 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
47342 omp_clause_mask mask (0);
47344 switch (cp_parser_pragma_kind (pragma_tok))
47346 case PRAGMA_OACC_ATOMIC:
47347 cp_parser_omp_atomic (parser, pragma_tok, true);
47348 return;
47349 case PRAGMA_OACC_CACHE:
47350 stmt = cp_parser_oacc_cache (parser, pragma_tok);
47351 break;
47352 case PRAGMA_OACC_DATA:
47353 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
47354 break;
47355 case PRAGMA_OACC_ENTER_DATA:
47356 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
47357 break;
47358 case PRAGMA_OACC_EXIT_DATA:
47359 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
47360 break;
47361 case PRAGMA_OACC_HOST_DATA:
47362 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
47363 break;
47364 case PRAGMA_OACC_KERNELS:
47365 case PRAGMA_OACC_PARALLEL:
47366 case PRAGMA_OACC_SERIAL:
47367 strcpy (p_name, "#pragma acc");
47368 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
47369 break;
47370 case PRAGMA_OACC_LOOP:
47371 strcpy (p_name, "#pragma acc");
47372 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
47373 if_p);
47374 break;
47375 case PRAGMA_OACC_UPDATE:
47376 stmt = cp_parser_oacc_update (parser, pragma_tok);
47377 break;
47378 case PRAGMA_OACC_WAIT:
47379 stmt = cp_parser_oacc_wait (parser, pragma_tok);
47380 break;
47381 case PRAGMA_OMP_ALLOCATE:
47382 cp_parser_omp_allocate (parser, pragma_tok);
47383 return;
47384 case PRAGMA_OMP_ATOMIC:
47385 cp_parser_omp_atomic (parser, pragma_tok, false);
47386 return;
47387 case PRAGMA_OMP_CRITICAL:
47388 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
47389 break;
47390 case PRAGMA_OMP_DISTRIBUTE:
47391 strcpy (p_name, "#pragma omp");
47392 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
47393 if_p);
47394 break;
47395 case PRAGMA_OMP_FOR:
47396 strcpy (p_name, "#pragma omp");
47397 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
47398 if_p);
47399 break;
47400 case PRAGMA_OMP_LOOP:
47401 strcpy (p_name, "#pragma omp");
47402 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
47403 if_p);
47404 break;
47405 case PRAGMA_OMP_MASKED:
47406 strcpy (p_name, "#pragma omp");
47407 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
47408 if_p);
47409 break;
47410 case PRAGMA_OMP_MASTER:
47411 strcpy (p_name, "#pragma omp");
47412 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
47413 if_p);
47414 break;
47415 case PRAGMA_OMP_PARALLEL:
47416 strcpy (p_name, "#pragma omp");
47417 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
47418 if_p);
47419 break;
47420 case PRAGMA_OMP_SCOPE:
47421 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
47422 break;
47423 case PRAGMA_OMP_SECTIONS:
47424 strcpy (p_name, "#pragma omp");
47425 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
47426 break;
47427 case PRAGMA_OMP_SIMD:
47428 strcpy (p_name, "#pragma omp");
47429 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
47430 if_p);
47431 break;
47432 case PRAGMA_OMP_SINGLE:
47433 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
47434 break;
47435 case PRAGMA_OMP_TASK:
47436 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
47437 break;
47438 case PRAGMA_OMP_TASKGROUP:
47439 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
47440 break;
47441 case PRAGMA_OMP_TASKLOOP:
47442 strcpy (p_name, "#pragma omp");
47443 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
47444 if_p);
47445 break;
47446 case PRAGMA_OMP_TEAMS:
47447 strcpy (p_name, "#pragma omp");
47448 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
47449 if_p);
47450 break;
47451 default:
47452 gcc_unreachable ();
47455 protected_set_expr_location (stmt, pragma_tok->location);
47458 /* Transactional Memory parsing routines. */
47460 /* Parse a transaction attribute.
47462 txn-attribute:
47463 attribute
47464 [ [ identifier ] ]
47466 We use this instead of cp_parser_attributes_opt for transactions to avoid
47467 the pedwarn in C++98 mode. */
47469 static tree
47470 cp_parser_txn_attribute_opt (cp_parser *parser)
47472 cp_token *token;
47473 tree attr_name, attr = NULL;
47475 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
47476 return cp_parser_attributes_opt (parser);
47478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
47479 return NULL_TREE;
47480 cp_lexer_consume_token (parser->lexer);
47481 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
47482 goto error1;
47484 token = cp_lexer_peek_token (parser->lexer);
47485 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
47487 token = cp_lexer_consume_token (parser->lexer);
47489 attr_name = (token->type == CPP_KEYWORD
47490 /* For keywords, use the canonical spelling,
47491 not the parsed identifier. */
47492 ? ridpointers[(int) token->keyword]
47493 : token->u.value);
47494 attr = build_tree_list (attr_name, NULL_TREE);
47496 else
47497 cp_parser_error (parser, "expected identifier");
47499 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
47500 error1:
47501 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
47502 return attr;
47505 /* Parse a __transaction_atomic or __transaction_relaxed statement.
47507 transaction-statement:
47508 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
47509 compound-statement
47510 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
47513 static tree
47514 cp_parser_transaction (cp_parser *parser, cp_token *token)
47516 unsigned char old_in = parser->in_transaction;
47517 unsigned char this_in = 1, new_in;
47518 enum rid keyword = token->keyword;
47519 tree stmt, attrs, noex;
47521 cp_lexer_consume_token (parser->lexer);
47523 if (keyword == RID_TRANSACTION_RELAXED
47524 || keyword == RID_SYNCHRONIZED)
47525 this_in |= TM_STMT_ATTR_RELAXED;
47526 else
47528 attrs = cp_parser_txn_attribute_opt (parser);
47529 if (attrs)
47530 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
47533 /* Parse a noexcept specification. */
47534 if (keyword == RID_ATOMIC_NOEXCEPT)
47535 noex = boolean_true_node;
47536 else if (keyword == RID_ATOMIC_CANCEL)
47538 /* cancel-and-throw is unimplemented. */
47539 sorry ("%<atomic_cancel%>");
47540 noex = NULL_TREE;
47542 else
47543 noex = cp_parser_noexcept_specification_opt (parser,
47544 CP_PARSER_FLAGS_NONE,
47545 /*require_constexpr=*/true,
47546 /*consumed_expr=*/NULL,
47547 /*return_cond=*/true);
47549 /* Keep track if we're in the lexical scope of an outer transaction. */
47550 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
47552 stmt = begin_transaction_stmt (token->location, NULL, this_in);
47554 parser->in_transaction = new_in;
47555 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
47556 parser->in_transaction = old_in;
47558 finish_transaction_stmt (stmt, NULL, this_in, noex);
47560 return stmt;
47563 /* Parse a __transaction_atomic or __transaction_relaxed expression.
47565 transaction-expression:
47566 __transaction_atomic txn-noexcept-spec[opt] ( expression )
47567 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
47570 static tree
47571 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
47573 unsigned char old_in = parser->in_transaction;
47574 unsigned char this_in = 1;
47575 cp_token *token;
47576 tree expr, noex;
47577 bool noex_expr;
47578 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47580 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
47581 || keyword == RID_TRANSACTION_RELAXED);
47583 if (!flag_tm)
47584 error_at (loc,
47585 keyword == RID_TRANSACTION_RELAXED
47586 ? G_("%<__transaction_relaxed%> without transactional memory "
47587 "support enabled")
47588 : G_("%<__transaction_atomic%> without transactional memory "
47589 "support enabled"));
47591 token = cp_parser_require_keyword (parser, keyword,
47592 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
47593 : RT_TRANSACTION_RELAXED));
47594 gcc_assert (token != NULL);
47596 if (keyword == RID_TRANSACTION_RELAXED)
47597 this_in |= TM_STMT_ATTR_RELAXED;
47599 /* Set this early. This might mean that we allow transaction_cancel in
47600 an expression that we find out later actually has to be a constexpr.
47601 However, we expect that cxx_constant_value will be able to deal with
47602 this; also, if the noexcept has no constexpr, then what we parse next
47603 really is a transaction's body. */
47604 parser->in_transaction = this_in;
47606 /* Parse a noexcept specification. */
47607 noex = cp_parser_noexcept_specification_opt (parser,
47608 CP_PARSER_FLAGS_NONE,
47609 /*require_constexpr=*/false,
47610 &noex_expr,
47611 /*return_cond=*/true);
47613 if (!noex || !noex_expr
47614 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
47616 matching_parens parens;
47617 parens.require_open (parser);
47619 expr = cp_parser_expression (parser);
47620 expr = finish_parenthesized_expr (expr);
47622 parens.require_close (parser);
47624 else
47626 /* The only expression that is available got parsed for the noexcept
47627 already. noexcept is true then. */
47628 expr = noex;
47629 noex = boolean_true_node;
47632 expr = build_transaction_expr (token->location, expr, this_in, noex);
47633 parser->in_transaction = old_in;
47635 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
47636 return error_mark_node;
47638 return (flag_tm ? expr : error_mark_node);
47641 /* Parse a function-transaction-block.
47643 function-transaction-block:
47644 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
47645 function-body
47646 __transaction_atomic txn-attribute[opt] function-try-block
47647 __transaction_relaxed ctor-initializer[opt] function-body
47648 __transaction_relaxed function-try-block
47651 static void
47652 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
47654 unsigned char old_in = parser->in_transaction;
47655 unsigned char new_in = 1;
47656 tree compound_stmt, stmt, attrs;
47657 cp_token *token;
47659 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
47660 || keyword == RID_TRANSACTION_RELAXED);
47661 token = cp_parser_require_keyword (parser, keyword,
47662 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
47663 : RT_TRANSACTION_RELAXED));
47664 gcc_assert (token != NULL);
47666 if (keyword == RID_TRANSACTION_RELAXED)
47667 new_in |= TM_STMT_ATTR_RELAXED;
47668 else
47670 attrs = cp_parser_txn_attribute_opt (parser);
47671 if (attrs)
47672 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
47675 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
47677 parser->in_transaction = new_in;
47679 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
47680 cp_parser_function_try_block (parser);
47681 else
47682 cp_parser_ctor_initializer_opt_and_function_body
47683 (parser, /*in_function_try_block=*/false);
47685 parser->in_transaction = old_in;
47687 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
47690 /* Parse a __transaction_cancel statement.
47692 cancel-statement:
47693 __transaction_cancel txn-attribute[opt] ;
47694 __transaction_cancel txn-attribute[opt] throw-expression ;
47696 ??? Cancel and throw is not yet implemented. */
47698 static tree
47699 cp_parser_transaction_cancel (cp_parser *parser)
47701 cp_token *token;
47702 bool is_outer = false;
47703 tree stmt, attrs;
47705 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
47706 RT_TRANSACTION_CANCEL);
47707 gcc_assert (token != NULL);
47709 attrs = cp_parser_txn_attribute_opt (parser);
47710 if (attrs)
47711 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
47713 /* ??? Parse cancel-and-throw here. */
47715 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
47717 if (!flag_tm)
47719 error_at (token->location, "%<__transaction_cancel%> without "
47720 "transactional memory support enabled");
47721 return error_mark_node;
47723 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
47725 error_at (token->location, "%<__transaction_cancel%> within a "
47726 "%<__transaction_relaxed%>");
47727 return error_mark_node;
47729 else if (is_outer)
47731 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
47732 && !is_tm_may_cancel_outer (current_function_decl))
47734 error_at (token->location, "outer %<__transaction_cancel%> not "
47735 "within outer %<__transaction_atomic%>");
47736 error_at (token->location,
47737 " or a %<transaction_may_cancel_outer%> function");
47738 return error_mark_node;
47741 else if (parser->in_transaction == 0)
47743 error_at (token->location, "%<__transaction_cancel%> not within "
47744 "%<__transaction_atomic%>");
47745 return error_mark_node;
47748 stmt = build_tm_abort_call (token->location, is_outer);
47749 add_stmt (stmt);
47751 return stmt;
47754 /* The parser. */
47756 static GTY (()) cp_parser *the_parser;
47759 /* Special handling for the first token or line in the file. The first
47760 thing in the file might be #pragma GCC pch_preprocess, which loads a
47761 PCH file, which is a GC collection point. So we need to handle this
47762 first pragma without benefit of an existing lexer structure.
47764 Always returns one token to the caller in *FIRST_TOKEN. This is
47765 either the true first token of the file, or the first token after
47766 the initial pragma. */
47768 static void
47769 cp_parser_initial_pragma (cp_token *first_token)
47771 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
47772 return;
47774 cp_lexer_get_preprocessor_token (0, first_token);
47776 tree name = NULL;
47777 if (first_token->type == CPP_STRING)
47779 name = first_token->u.value;
47781 cp_lexer_get_preprocessor_token (0, first_token);
47784 /* Skip to the end of the pragma. */
47785 if (first_token->type != CPP_PRAGMA_EOL)
47787 error_at (first_token->location,
47788 "malformed %<#pragma GCC pch_preprocess%>");
47790 cp_lexer_get_preprocessor_token (0, first_token);
47791 while (first_token->type != CPP_PRAGMA_EOL);
47794 /* Now actually load the PCH file. */
47795 if (name)
47796 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
47798 /* Read one more token to return to our caller. We have to do this
47799 after reading the PCH file in, since its pointers have to be
47800 live. */
47801 cp_lexer_get_preprocessor_token (0, first_token);
47804 /* Parse a pragma GCC ivdep. */
47806 static bool
47807 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
47809 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47810 return true;
47813 /* Parse a pragma GCC unroll. */
47815 static unsigned short
47816 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
47818 location_t location = cp_lexer_peek_token (parser->lexer)->location;
47819 tree expr = cp_parser_constant_expression (parser);
47820 unsigned short unroll;
47821 expr = maybe_constant_value (expr);
47822 HOST_WIDE_INT lunroll = 0;
47823 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
47824 || TREE_CODE (expr) != INTEGER_CST
47825 || (lunroll = tree_to_shwi (expr)) < 0
47826 || lunroll >= USHRT_MAX)
47828 error_at (location, "%<#pragma GCC unroll%> requires an"
47829 " assignment-expression that evaluates to a non-negative"
47830 " integral constant less than %u", USHRT_MAX);
47831 unroll = 0;
47833 else
47835 unroll = (unsigned short)lunroll;
47836 if (unroll == 0)
47837 unroll = 1;
47839 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47840 return unroll;
47843 /* Normal parsing of a pragma token. Here we can (and must) use the
47844 regular lexer. */
47846 static bool
47847 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
47849 cp_token *pragma_tok;
47850 unsigned int id;
47851 tree stmt;
47852 bool ret = false;
47854 pragma_tok = cp_lexer_consume_token (parser->lexer);
47855 gcc_assert (pragma_tok->type == CPP_PRAGMA);
47856 parser->lexer->in_pragma = true;
47858 id = cp_parser_pragma_kind (pragma_tok);
47859 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
47860 cp_ensure_no_omp_declare_simd (parser);
47861 switch (id)
47863 case PRAGMA_GCC_PCH_PREPROCESS:
47864 error_at (pragma_tok->location,
47865 "%<#pragma GCC pch_preprocess%> must be first");
47866 break;
47868 case PRAGMA_OMP_BARRIER:
47869 switch (context)
47871 case pragma_compound:
47872 cp_parser_omp_barrier (parser, pragma_tok);
47873 return false;
47874 case pragma_stmt:
47875 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47876 "used in compound statements", "omp barrier");
47877 ret = true;
47878 break;
47879 default:
47880 goto bad_stmt;
47882 break;
47884 case PRAGMA_OMP_DEPOBJ:
47885 switch (context)
47887 case pragma_compound:
47888 cp_parser_omp_depobj (parser, pragma_tok);
47889 return false;
47890 case pragma_stmt:
47891 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47892 "used in compound statements", "omp depobj");
47893 ret = true;
47894 break;
47895 default:
47896 goto bad_stmt;
47898 break;
47900 case PRAGMA_OMP_FLUSH:
47901 switch (context)
47903 case pragma_compound:
47904 cp_parser_omp_flush (parser, pragma_tok);
47905 return false;
47906 case pragma_stmt:
47907 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47908 "used in compound statements", "omp flush");
47909 ret = true;
47910 break;
47911 default:
47912 goto bad_stmt;
47914 break;
47916 case PRAGMA_OMP_TASKWAIT:
47917 switch (context)
47919 case pragma_compound:
47920 cp_parser_omp_taskwait (parser, pragma_tok);
47921 return false;
47922 case pragma_stmt:
47923 error_at (pragma_tok->location,
47924 "%<#pragma %s%> may only be used in compound statements",
47925 "omp taskwait");
47926 ret = true;
47927 break;
47928 default:
47929 goto bad_stmt;
47931 break;
47933 case PRAGMA_OMP_TASKYIELD:
47934 switch (context)
47936 case pragma_compound:
47937 cp_parser_omp_taskyield (parser, pragma_tok);
47938 return false;
47939 case pragma_stmt:
47940 error_at (pragma_tok->location,
47941 "%<#pragma %s%> may only be used in compound statements",
47942 "omp taskyield");
47943 ret = true;
47944 break;
47945 default:
47946 goto bad_stmt;
47948 break;
47950 case PRAGMA_OMP_CANCEL:
47951 switch (context)
47953 case pragma_compound:
47954 cp_parser_omp_cancel (parser, pragma_tok);
47955 return false;
47956 case pragma_stmt:
47957 error_at (pragma_tok->location,
47958 "%<#pragma %s%> may only be used in compound statements",
47959 "omp cancel");
47960 ret = true;
47961 break;
47962 default:
47963 goto bad_stmt;
47965 break;
47967 case PRAGMA_OMP_CANCELLATION_POINT:
47968 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
47970 case PRAGMA_OMP_THREADPRIVATE:
47971 cp_parser_omp_threadprivate (parser, pragma_tok);
47972 return false;
47974 case PRAGMA_OMP_DECLARE:
47975 return cp_parser_omp_declare (parser, pragma_tok, context);
47977 case PRAGMA_OACC_DECLARE:
47978 cp_parser_oacc_declare (parser, pragma_tok);
47979 return false;
47981 case PRAGMA_OACC_ENTER_DATA:
47982 if (context == pragma_stmt)
47984 error_at (pragma_tok->location,
47985 "%<#pragma %s%> may only be used in compound statements",
47986 "acc enter data");
47987 ret = true;
47988 break;
47990 else if (context != pragma_compound)
47991 goto bad_stmt;
47992 cp_parser_omp_construct (parser, pragma_tok, if_p);
47993 return true;
47995 case PRAGMA_OACC_EXIT_DATA:
47996 if (context == pragma_stmt)
47998 error_at (pragma_tok->location,
47999 "%<#pragma %s%> may only be used in compound statements",
48000 "acc exit data");
48001 ret = true;
48002 break;
48004 else if (context != pragma_compound)
48005 goto bad_stmt;
48006 cp_parser_omp_construct (parser, pragma_tok, if_p);
48007 return true;
48009 case PRAGMA_OACC_ROUTINE:
48010 if (context != pragma_external)
48012 error_at (pragma_tok->location,
48013 "%<#pragma acc routine%> must be at file scope");
48014 ret = true;
48015 break;
48017 cp_parser_oacc_routine (parser, pragma_tok, context);
48018 return false;
48020 case PRAGMA_OACC_UPDATE:
48021 if (context == pragma_stmt)
48023 error_at (pragma_tok->location,
48024 "%<#pragma %s%> may only be used in compound statements",
48025 "acc update");
48026 ret = true;
48027 break;
48029 else if (context != pragma_compound)
48030 goto bad_stmt;
48031 cp_parser_omp_construct (parser, pragma_tok, if_p);
48032 return true;
48034 case PRAGMA_OACC_WAIT:
48035 if (context == pragma_stmt)
48037 error_at (pragma_tok->location,
48038 "%<#pragma %s%> may only be used in compound statements",
48039 "acc wait");
48040 ret = true;
48041 break;
48043 else if (context != pragma_compound)
48044 goto bad_stmt;
48045 cp_parser_omp_construct (parser, pragma_tok, if_p);
48046 return true;
48047 case PRAGMA_OMP_ALLOCATE:
48048 cp_parser_omp_allocate (parser, pragma_tok);
48049 return false;
48050 case PRAGMA_OACC_ATOMIC:
48051 case PRAGMA_OACC_CACHE:
48052 case PRAGMA_OACC_DATA:
48053 case PRAGMA_OACC_HOST_DATA:
48054 case PRAGMA_OACC_KERNELS:
48055 case PRAGMA_OACC_LOOP:
48056 case PRAGMA_OACC_PARALLEL:
48057 case PRAGMA_OACC_SERIAL:
48058 case PRAGMA_OMP_ATOMIC:
48059 case PRAGMA_OMP_CRITICAL:
48060 case PRAGMA_OMP_DISTRIBUTE:
48061 case PRAGMA_OMP_FOR:
48062 case PRAGMA_OMP_LOOP:
48063 case PRAGMA_OMP_MASKED:
48064 case PRAGMA_OMP_MASTER:
48065 case PRAGMA_OMP_PARALLEL:
48066 case PRAGMA_OMP_SCOPE:
48067 case PRAGMA_OMP_SECTIONS:
48068 case PRAGMA_OMP_SIMD:
48069 case PRAGMA_OMP_SINGLE:
48070 case PRAGMA_OMP_TASK:
48071 case PRAGMA_OMP_TASKGROUP:
48072 case PRAGMA_OMP_TASKLOOP:
48073 case PRAGMA_OMP_TEAMS:
48074 if (context != pragma_stmt && context != pragma_compound)
48075 goto bad_stmt;
48076 stmt = push_omp_privatization_clauses (false);
48077 cp_parser_omp_construct (parser, pragma_tok, if_p);
48078 pop_omp_privatization_clauses (stmt);
48079 return true;
48081 case PRAGMA_OMP_REQUIRES:
48082 if (context != pragma_external)
48084 error_at (pragma_tok->location,
48085 "%<#pragma omp requires%> may only be used at file or "
48086 "namespace scope");
48087 ret = true;
48088 break;
48090 return cp_parser_omp_requires (parser, pragma_tok);
48092 case PRAGMA_OMP_NOTHING:
48093 cp_parser_omp_nothing (parser, pragma_tok);
48094 return false;
48096 case PRAGMA_OMP_ERROR:
48097 return cp_parser_omp_error (parser, pragma_tok, context);
48099 case PRAGMA_OMP_ORDERED:
48100 if (context != pragma_stmt && context != pragma_compound)
48101 goto bad_stmt;
48102 stmt = push_omp_privatization_clauses (false);
48103 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
48104 pop_omp_privatization_clauses (stmt);
48105 return ret;
48107 case PRAGMA_OMP_TARGET:
48108 if (context != pragma_stmt && context != pragma_compound)
48109 goto bad_stmt;
48110 stmt = push_omp_privatization_clauses (false);
48111 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
48112 pop_omp_privatization_clauses (stmt);
48113 return ret;
48115 case PRAGMA_OMP_END_DECLARE_TARGET:
48116 cp_parser_omp_end_declare_target (parser, pragma_tok);
48117 return false;
48119 case PRAGMA_OMP_SCAN:
48120 error_at (pragma_tok->location,
48121 "%<#pragma omp scan%> may only be used in "
48122 "a loop construct with %<inscan%> %<reduction%> clause");
48123 break;
48125 case PRAGMA_OMP_SECTION:
48126 error_at (pragma_tok->location,
48127 "%<#pragma omp section%> may only be used in "
48128 "%<#pragma omp sections%> construct");
48129 break;
48131 case PRAGMA_IVDEP:
48133 if (context == pragma_external)
48135 error_at (pragma_tok->location,
48136 "%<#pragma GCC ivdep%> must be inside a function");
48137 break;
48139 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
48140 unsigned short unroll;
48141 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
48142 if (tok->type == CPP_PRAGMA
48143 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
48145 tok = cp_lexer_consume_token (parser->lexer);
48146 unroll = cp_parser_pragma_unroll (parser, tok);
48147 tok = cp_lexer_peek_token (the_parser->lexer);
48149 else
48150 unroll = 0;
48151 if (tok->type != CPP_KEYWORD
48152 || (tok->keyword != RID_FOR
48153 && tok->keyword != RID_WHILE
48154 && tok->keyword != RID_DO))
48156 cp_parser_error (parser, "for, while or do statement expected");
48157 return false;
48159 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
48160 return true;
48163 case PRAGMA_UNROLL:
48165 if (context == pragma_external)
48167 error_at (pragma_tok->location,
48168 "%<#pragma GCC unroll%> must be inside a function");
48169 break;
48171 const unsigned short unroll
48172 = cp_parser_pragma_unroll (parser, pragma_tok);
48173 bool ivdep;
48174 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
48175 if (tok->type == CPP_PRAGMA
48176 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
48178 tok = cp_lexer_consume_token (parser->lexer);
48179 ivdep = cp_parser_pragma_ivdep (parser, tok);
48180 tok = cp_lexer_peek_token (the_parser->lexer);
48182 else
48183 ivdep = false;
48184 if (tok->type != CPP_KEYWORD
48185 || (tok->keyword != RID_FOR
48186 && tok->keyword != RID_WHILE
48187 && tok->keyword != RID_DO))
48189 cp_parser_error (parser, "for, while or do statement expected");
48190 return false;
48192 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
48193 return true;
48196 default:
48197 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
48198 c_invoke_pragma_handler (id);
48199 break;
48201 bad_stmt:
48202 cp_parser_error (parser, "expected declaration specifiers");
48203 break;
48206 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48207 return ret;
48210 /* The interface the pragma parsers have to the lexer. */
48212 enum cpp_ttype
48213 pragma_lex (tree *value, location_t *loc)
48215 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
48216 enum cpp_ttype ret = tok->type;
48218 *value = tok->u.value;
48219 if (loc)
48220 *loc = tok->location;
48222 if (ret == CPP_PRAGMA_EOL)
48223 ret = CPP_EOF;
48224 else if (ret == CPP_STRING)
48225 *value = cp_parser_string_literal (the_parser, false, false);
48226 else
48228 if (ret == CPP_KEYWORD)
48229 ret = CPP_NAME;
48230 cp_lexer_consume_token (the_parser->lexer);
48233 return ret;
48237 /* External interface. */
48239 /* Parse one entire translation unit. */
48241 void
48242 c_parse_file (void)
48244 static bool already_called = false;
48246 if (already_called)
48247 fatal_error (input_location,
48248 "multi-source compilation not implemented for C++");
48249 already_called = true;
48251 /* cp_lexer_new_main is called before doing any GC allocation
48252 because tokenization might load a PCH file. */
48253 cp_lexer *lexer = cp_lexer_new_main ();
48255 the_parser = cp_parser_new (lexer);
48257 cp_parser_translation_unit (the_parser);
48258 class_decl_loc_t::diag_mismatched_tags ();
48260 the_parser = NULL;
48262 finish_translation_unit ();
48265 /* Create an identifier for a generic parameter type (a synthesized
48266 template parameter implied by `auto' or a concept identifier). */
48268 static GTY(()) int generic_parm_count;
48269 static tree
48270 make_generic_type_name ()
48272 char buf[32];
48273 sprintf (buf, "auto:%d", ++generic_parm_count);
48274 return get_identifier (buf);
48277 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
48278 (creating a new template parameter list if necessary). Returns the newly
48279 created template type parm. */
48281 static tree
48282 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
48284 /* A requires-clause is not a function and cannot have placeholders. */
48285 if (current_binding_level->requires_expression)
48287 error ("placeholder type not allowed in this context");
48288 return error_mark_node;
48291 gcc_assert (current_binding_level->kind == sk_function_parms);
48293 /* We are either continuing a function template that already contains implicit
48294 template parameters, creating a new fully-implicit function template, or
48295 extending an existing explicit function template with implicit template
48296 parameters. */
48298 cp_binding_level *const entry_scope = current_binding_level;
48300 bool become_template = false;
48301 cp_binding_level *parent_scope = 0;
48303 if (parser->implicit_template_scope)
48305 gcc_assert (parser->implicit_template_parms);
48307 current_binding_level = parser->implicit_template_scope;
48309 else
48311 /* Roll back to the existing template parameter scope (in the case of
48312 extending an explicit function template) or introduce a new template
48313 parameter scope ahead of the function parameter scope (or class scope
48314 in the case of out-of-line member definitions). The function scope is
48315 added back after template parameter synthesis below. */
48317 cp_binding_level *scope = entry_scope;
48319 while (scope->kind == sk_function_parms)
48321 parent_scope = scope;
48322 scope = scope->level_chain;
48324 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
48326 /* If not defining a class, then any class scope is a scope level in
48327 an out-of-line member definition. In this case simply wind back
48328 beyond the first such scope to inject the template parameter list.
48329 Otherwise wind back to the class being defined. The latter can
48330 occur in class member friend declarations such as:
48332 class A {
48333 void foo (auto);
48335 class B {
48336 friend void A::foo (auto);
48339 The template parameter list synthesized for the friend declaration
48340 must be injected in the scope of 'B'. This can also occur in
48341 erroneous cases such as:
48343 struct A {
48344 struct B {
48345 void foo (auto);
48347 void B::foo (auto) {}
48350 Here the attempted definition of 'B::foo' within 'A' is ill-formed
48351 but, nevertheless, the template parameter list synthesized for the
48352 declarator should be injected into the scope of 'A' as if the
48353 ill-formed template was specified explicitly. */
48355 while (scope->kind == sk_class && !scope->defining_class_p)
48357 parent_scope = scope;
48358 scope = scope->level_chain;
48362 current_binding_level = scope;
48364 if (scope->kind != sk_template_parms
48365 || !function_being_declared_is_template_p (parser))
48367 /* Introduce a new template parameter list for implicit template
48368 parameters. */
48370 become_template = true;
48372 parser->implicit_template_scope
48373 = begin_scope (sk_template_parms, NULL);
48375 ++processing_template_decl;
48377 parser->fully_implicit_function_template_p = true;
48378 ++parser->num_template_parameter_lists;
48380 else
48382 /* Synthesize implicit template parameters at the end of the explicit
48383 template parameter list. */
48385 gcc_assert (current_template_parms);
48387 parser->implicit_template_scope = scope;
48389 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48390 parser->implicit_template_parms
48391 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
48395 /* Synthesize a new template parameter and track the current template
48396 parameter chain with implicit_template_parms. */
48398 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
48399 tree synth_id = make_generic_type_name ();
48400 tree synth_tmpl_parm;
48401 bool non_type = false;
48403 /* Synthesize the type template parameter. */
48404 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
48405 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
48407 if (become_template)
48408 current_template_parms = tree_cons (size_int (current_template_depth + 1),
48409 NULL_TREE, current_template_parms);
48411 /* Attach the constraint to the parm before processing. */
48412 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
48413 TREE_TYPE (node) = constr;
48414 tree new_parm
48415 = process_template_parm (parser->implicit_template_parms,
48416 input_location,
48417 node,
48418 /*non_type=*/non_type,
48419 /*param_pack=*/false);
48421 /* Mark the synthetic declaration "virtual". This is used when
48422 comparing template-heads to determine if whether an abbreviated
48423 function template is equivalent to an explicit template.
48425 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
48426 if (TREE_VALUE (new_parm) != error_mark_node)
48427 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
48429 // Chain the new parameter to the list of implicit parameters.
48430 if (parser->implicit_template_parms)
48431 parser->implicit_template_parms
48432 = TREE_CHAIN (parser->implicit_template_parms);
48433 else
48434 parser->implicit_template_parms = new_parm;
48436 tree new_decl = get_local_decls ();
48437 if (non_type)
48438 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
48439 new_decl = DECL_INITIAL (new_decl);
48441 /* If creating a fully implicit function template, start the new implicit
48442 template parameter list with this synthesized type, otherwise grow the
48443 current template parameter list. */
48445 if (become_template)
48447 parent_scope->level_chain = current_binding_level;
48449 tree new_parms = make_tree_vec (1);
48450 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
48451 TREE_VALUE (current_template_parms) = new_parms;
48453 else
48455 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48456 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
48457 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
48458 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
48461 /* If the new parameter was constrained, we need to add that to the
48462 constraints in the template parameter list. */
48463 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
48465 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
48466 reqs = combine_constraint_expressions (reqs, req);
48467 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
48470 current_binding_level = entry_scope;
48472 return new_decl;
48475 /* Finish the declaration of a fully implicit function template. Such a
48476 template has no explicit template parameter list so has not been through the
48477 normal template head and tail processing. synthesize_implicit_template_parm
48478 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
48479 provided if the declaration is a class member such that its template
48480 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
48481 form is returned. Otherwise NULL_TREE is returned. */
48483 static tree
48484 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
48486 gcc_assert (parser->fully_implicit_function_template_p);
48488 if (member_decl_opt && member_decl_opt != error_mark_node
48489 && DECL_VIRTUAL_P (member_decl_opt))
48491 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
48492 "implicit templates may not be %<virtual%>");
48493 DECL_VIRTUAL_P (member_decl_opt) = false;
48496 if (member_decl_opt)
48497 member_decl_opt = finish_member_template_decl (member_decl_opt);
48498 end_template_decl ();
48500 parser->fully_implicit_function_template_p = false;
48501 parser->implicit_template_parms = 0;
48502 parser->implicit_template_scope = 0;
48503 --parser->num_template_parameter_lists;
48505 return member_decl_opt;
48508 /* Like finish_fully_implicit_template, but to be used in error
48509 recovery, rearranging scopes so that we restore the state we had
48510 before synthesize_implicit_template_parm inserted the implement
48511 template parms scope. */
48513 static void
48514 abort_fully_implicit_template (cp_parser *parser)
48516 cp_binding_level *return_to_scope = current_binding_level;
48518 if (parser->implicit_template_scope
48519 && return_to_scope != parser->implicit_template_scope)
48521 cp_binding_level *child = return_to_scope;
48522 for (cp_binding_level *scope = child->level_chain;
48523 scope != parser->implicit_template_scope;
48524 scope = child->level_chain)
48525 child = scope;
48526 child->level_chain = parser->implicit_template_scope->level_chain;
48527 parser->implicit_template_scope->level_chain = return_to_scope;
48528 current_binding_level = parser->implicit_template_scope;
48530 else
48531 return_to_scope = return_to_scope->level_chain;
48533 finish_fully_implicit_template (parser, NULL);
48535 gcc_assert (current_binding_level == return_to_scope);
48538 /* Helper function for diagnostics that have complained about things
48539 being used with 'extern "C"' linkage.
48541 Attempt to issue a note showing where the 'extern "C"' linkage began. */
48543 void
48544 maybe_show_extern_c_location (void)
48546 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
48547 inform (the_parser->innermost_linkage_specification_location,
48548 "%<extern \"C\"%> linkage started here");
48551 #include "gt-cp-parser.h"