Fix typo in t-dimode
[official-gcc.git] / gcc / cp / parser.c
blob0bd585257261b0abbd917be193118b0bc5c76b26
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48 #include "c-family/known-headers.h"
51 /* The lexer. */
53 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
54 and c-lex.c) and the C++ parser. */
56 /* The various kinds of non integral constant we encounter. */
57 enum non_integral_constant {
58 NIC_NONE,
59 /* floating-point literal */
60 NIC_FLOAT,
61 /* %<this%> */
62 NIC_THIS,
63 /* %<__FUNCTION__%> */
64 NIC_FUNC_NAME,
65 /* %<__PRETTY_FUNCTION__%> */
66 NIC_PRETTY_FUNC,
67 /* %<__func__%> */
68 NIC_C99_FUNC,
69 /* "%<va_arg%> */
70 NIC_VA_ARG,
71 /* a cast */
72 NIC_CAST,
73 /* %<typeid%> operator */
74 NIC_TYPEID,
75 /* non-constant compound literals */
76 NIC_NCC,
77 /* a function call */
78 NIC_FUNC_CALL,
79 /* an increment */
80 NIC_INC,
81 /* an decrement */
82 NIC_DEC,
83 /* an array reference */
84 NIC_ARRAY_REF,
85 /* %<->%> */
86 NIC_ARROW,
87 /* %<.%> */
88 NIC_POINT,
89 /* the address of a label */
90 NIC_ADDR_LABEL,
91 /* %<*%> */
92 NIC_STAR,
93 /* %<&%> */
94 NIC_ADDR,
95 /* %<++%> */
96 NIC_PREINCREMENT,
97 /* %<--%> */
98 NIC_PREDECREMENT,
99 /* %<new%> */
100 NIC_NEW,
101 /* %<delete%> */
102 NIC_DEL,
103 /* calls to overloaded operators */
104 NIC_OVERLOADED,
105 /* an assignment */
106 NIC_ASSIGNMENT,
107 /* a comma operator */
108 NIC_COMMA,
109 /* a call to a constructor */
110 NIC_CONSTRUCTOR,
111 /* a transaction expression */
112 NIC_TRANSACTION
115 /* The various kinds of errors about name-lookup failing. */
116 enum name_lookup_error {
117 /* NULL */
118 NLE_NULL,
119 /* is not a type */
120 NLE_TYPE,
121 /* is not a class or namespace */
122 NLE_CXX98,
123 /* is not a class, namespace, or enumeration */
124 NLE_NOT_CXX98
127 /* The various kinds of required token */
128 enum required_token {
129 RT_NONE,
130 RT_SEMICOLON, /* ';' */
131 RT_OPEN_PAREN, /* '(' */
132 RT_CLOSE_BRACE, /* '}' */
133 RT_OPEN_BRACE, /* '{' */
134 RT_CLOSE_SQUARE, /* ']' */
135 RT_OPEN_SQUARE, /* '[' */
136 RT_COMMA, /* ',' */
137 RT_SCOPE, /* '::' */
138 RT_LESS, /* '<' */
139 RT_GREATER, /* '>' */
140 RT_EQ, /* '=' */
141 RT_ELLIPSIS, /* '...' */
142 RT_MULT, /* '*' */
143 RT_COMPL, /* '~' */
144 RT_COLON, /* ':' */
145 RT_COLON_SCOPE, /* ':' or '::' */
146 RT_CLOSE_PAREN, /* ')' */
147 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
148 RT_PRAGMA_EOL, /* end of line */
149 RT_NAME, /* identifier */
151 /* The type is CPP_KEYWORD */
152 RT_NEW, /* new */
153 RT_DELETE, /* delete */
154 RT_RETURN, /* return */
155 RT_WHILE, /* while */
156 RT_EXTERN, /* extern */
157 RT_STATIC_ASSERT, /* static_assert */
158 RT_DECLTYPE, /* decltype */
159 RT_OPERATOR, /* operator */
160 RT_CLASS, /* class */
161 RT_TEMPLATE, /* template */
162 RT_NAMESPACE, /* namespace */
163 RT_USING, /* using */
164 RT_ASM, /* asm */
165 RT_TRY, /* try */
166 RT_CATCH, /* catch */
167 RT_THROW, /* throw */
168 RT_AUTO, /* auto */
169 RT_LABEL, /* __label__ */
170 RT_AT_TRY, /* @try */
171 RT_AT_SYNCHRONIZED, /* @synchronized */
172 RT_AT_THROW, /* @throw */
174 RT_SELECT, /* selection-statement */
175 RT_ITERATION, /* iteration-statement */
176 RT_JUMP, /* jump-statement */
177 RT_CLASS_KEY, /* class-key */
178 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
179 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
180 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
181 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
183 RT_CO_YIELD /* co_yield */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (unsigned, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
249 static tree cp_parser_late_noexcept_specifier
250 (cp_parser *, tree);
251 static void noexcept_override_late_checks
252 (tree, tree);
254 static void cp_parser_initial_pragma
255 (cp_token *);
257 static bool cp_parser_omp_declare_reduction_exprs
258 (tree, cp_parser *);
259 static void cp_finalize_oacc_routine
260 (cp_parser *, tree, bool);
262 /* Manifest constants. */
263 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264 #define CP_SAVED_TOKEN_STACK 5
266 /* Variables. */
268 /* The stream to which debugging output should be written. */
269 static FILE *cp_lexer_debug_stream;
271 /* Nonzero if we are parsing an unevaluated operand: an operand to
272 sizeof, typeof, or alignof. */
273 int cp_unevaluated_operand;
275 /* Dump up to NUM tokens in BUFFER to FILE starting with token
276 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
277 first token in BUFFER. If NUM is 0, dump all the tokens. If
278 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279 highlighted by surrounding it in [[ ]]. */
281 static void
282 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
283 cp_token *start_token, unsigned num,
284 cp_token *curr_token)
286 unsigned i, nprinted;
287 cp_token *token;
288 bool do_print;
290 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
292 if (buffer == NULL)
293 return;
295 if (num == 0)
296 num = buffer->length ();
298 if (start_token == NULL)
299 start_token = buffer->address ();
301 if (start_token > buffer->address ())
303 cp_lexer_print_token (file, &(*buffer)[0]);
304 fprintf (file, " ... ");
307 do_print = false;
308 nprinted = 0;
309 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
311 if (token == start_token)
312 do_print = true;
314 if (!do_print)
315 continue;
317 nprinted++;
318 if (token == curr_token)
319 fprintf (file, "[[");
321 cp_lexer_print_token (file, token);
323 if (token == curr_token)
324 fprintf (file, "]]");
326 switch (token->type)
328 case CPP_SEMICOLON:
329 case CPP_OPEN_BRACE:
330 case CPP_CLOSE_BRACE:
331 case CPP_EOF:
332 fputc ('\n', file);
333 break;
335 default:
336 fputc (' ', file);
340 if (i == num && i < buffer->length ())
342 fprintf (file, " ... ");
343 cp_lexer_print_token (file, &buffer->last ());
346 fprintf (file, "\n");
350 /* Dump all tokens in BUFFER to stderr. */
352 void
353 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
355 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
358 DEBUG_FUNCTION void
359 debug (vec<cp_token, va_gc> &ref)
361 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
364 DEBUG_FUNCTION void
365 debug (vec<cp_token, va_gc> *ptr)
367 if (ptr)
368 debug (*ptr);
369 else
370 fprintf (stderr, "<nil>\n");
374 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
375 description for T. */
377 static void
378 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
380 if (t)
382 fprintf (file, "%s: ", desc);
383 print_node_brief (file, "", t, 0);
388 /* Dump parser context C to FILE. */
390 static void
391 cp_debug_print_context (FILE *file, cp_parser_context *c)
393 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
394 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
395 print_node_brief (file, "", c->object_type, 0);
396 fprintf (file, "}\n");
400 /* Print the stack of parsing contexts to FILE starting with FIRST. */
402 static void
403 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
405 unsigned i;
406 cp_parser_context *c;
408 fprintf (file, "Parsing context stack:\n");
409 for (i = 0, c = first; c; c = c->next, i++)
411 fprintf (file, "\t#%u: ", i);
412 cp_debug_print_context (file, c);
417 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
419 static void
420 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
422 if (flag)
423 fprintf (file, "%s: true\n", desc);
427 /* Print an unparsed function entry UF to FILE. */
429 static void
430 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
432 unsigned i;
433 cp_default_arg_entry *default_arg_fn;
434 tree fn;
436 fprintf (file, "\tFunctions with default args:\n");
437 for (i = 0;
438 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
439 i++)
441 fprintf (file, "\t\tClass type: ");
442 print_node_brief (file, "", default_arg_fn->class_type, 0);
443 fprintf (file, "\t\tDeclaration: ");
444 print_node_brief (file, "", default_arg_fn->decl, 0);
445 fprintf (file, "\n");
448 fprintf (file, "\n\tFunctions with definitions that require "
449 "post-processing\n\t\t");
450 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
452 print_node_brief (file, "", fn, 0);
453 fprintf (file, " ");
455 fprintf (file, "\n");
457 fprintf (file, "\n\tNon-static data members with initializers that require "
458 "post-processing\n\t\t");
459 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
461 print_node_brief (file, "", fn, 0);
462 fprintf (file, " ");
464 fprintf (file, "\n");
468 /* Print the stack of unparsed member functions S to FILE. */
470 static void
471 cp_debug_print_unparsed_queues (FILE *file,
472 vec<cp_unparsed_functions_entry, va_gc> *s)
474 unsigned i;
475 cp_unparsed_functions_entry *uf;
477 fprintf (file, "Unparsed functions\n");
478 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
480 fprintf (file, "#%u:\n", i);
481 cp_debug_print_unparsed_function (file, uf);
486 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487 the given PARSER. If FILE is NULL, the output is printed on stderr. */
489 static void
490 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
492 cp_token *next_token, *first_token, *start_token;
494 if (file == NULL)
495 file = stderr;
497 next_token = parser->lexer->next_token;
498 first_token = parser->lexer->buffer->address ();
499 start_token = (next_token > first_token + window_size / 2)
500 ? next_token - window_size / 2
501 : first_token;
502 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
503 next_token);
507 /* Dump debugging information for the given PARSER. If FILE is NULL,
508 the output is printed on stderr. */
510 void
511 cp_debug_parser (FILE *file, cp_parser *parser)
513 const size_t window_size = 20;
514 cp_token *token;
515 expanded_location eloc;
517 if (file == NULL)
518 file = stderr;
520 fprintf (file, "Parser state\n\n");
521 fprintf (file, "Number of tokens: %u\n",
522 vec_safe_length (parser->lexer->buffer));
523 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
524 cp_debug_print_tree_if_set (file, "Object scope",
525 parser->object_scope);
526 cp_debug_print_tree_if_set (file, "Qualifying scope",
527 parser->qualifying_scope);
528 cp_debug_print_context_stack (file, parser->context);
529 cp_debug_print_flag (file, "Allow GNU extensions",
530 parser->allow_gnu_extensions_p);
531 cp_debug_print_flag (file, "'>' token is greater-than",
532 parser->greater_than_is_operator_p);
533 cp_debug_print_flag (file, "Default args allowed in current "
534 "parameter list", parser->default_arg_ok_p);
535 cp_debug_print_flag (file, "Parsing integral constant-expression",
536 parser->integral_constant_expression_p);
537 cp_debug_print_flag (file, "Allow non-constant expression in current "
538 "constant-expression",
539 parser->allow_non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Seen non-constant expression",
541 parser->non_integral_constant_expression_p);
542 cp_debug_print_flag (file, "Local names forbidden in current context",
543 (parser->local_variables_forbidden_p
544 & LOCAL_VARS_FORBIDDEN));
545 cp_debug_print_flag (file, "'this' forbidden in current context",
546 (parser->local_variables_forbidden_p
547 & THIS_FORBIDDEN));
548 cp_debug_print_flag (file, "In unbraced linkage specification",
549 parser->in_unbraced_linkage_specification_p);
550 cp_debug_print_flag (file, "Parsing a declarator",
551 parser->in_declarator_p);
552 cp_debug_print_flag (file, "In template argument list",
553 parser->in_template_argument_list_p);
554 cp_debug_print_flag (file, "Parsing an iteration statement",
555 parser->in_statement & IN_ITERATION_STMT);
556 cp_debug_print_flag (file, "Parsing a switch statement",
557 parser->in_statement & IN_SWITCH_STMT);
558 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 parser->in_statement & IN_OMP_BLOCK);
560 cp_debug_print_flag (file, "Parsing an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "String expressions should be translated "
567 "to execution character set",
568 parser->translate_strings_p);
569 cp_debug_print_flag (file, "Parsing function body outside of a "
570 "local class", parser->in_function_body);
571 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
572 parser->colon_corrects_to_scope_p);
573 cp_debug_print_flag (file, "Colon doesn't start a class definition",
574 parser->colon_doesnt_start_class_def_p);
575 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
576 parser->objective_c_message_context_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
579 parser->type_definition_forbidden_message,
580 parser->type_definition_forbidden_message_arg
581 ? parser->type_definition_forbidden_message_arg : "<none>");
582 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
583 fprintf (file, "Number of class definitions in progress: %u\n",
584 parser->num_classes_being_defined);
585 fprintf (file, "Number of template parameter lists for the current "
586 "declaration: %u\n", parser->num_template_parameter_lists);
587 cp_debug_parser_tokens (file, parser, window_size);
588 token = parser->lexer->next_token;
589 fprintf (file, "Next token to parse:\n");
590 fprintf (file, "\tToken: ");
591 cp_lexer_print_token (file, token);
592 eloc = expand_location (token->location);
593 fprintf (file, "\n\tFile: %s\n", eloc.file);
594 fprintf (file, "\tLine: %d\n", eloc.line);
595 fprintf (file, "\tColumn: %d\n", eloc.column);
598 DEBUG_FUNCTION void
599 debug (cp_parser &ref)
601 cp_debug_parser (stderr, &ref);
604 DEBUG_FUNCTION void
605 debug (cp_parser *ptr)
607 if (ptr)
608 debug (*ptr);
609 else
610 fprintf (stderr, "<nil>\n");
613 /* Allocate memory for a new lexer object and return it. */
615 static cp_lexer *
616 cp_lexer_alloc (void)
618 /* Allocate the memory. */
619 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
621 /* Initially we are not debugging. */
622 lexer->debugging_p = false;
624 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
626 /* Create the buffer. */
627 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
629 return lexer;
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
633 preprocessor. */
635 static cp_lexer *
636 cp_lexer_new_main (void)
638 cp_token token;
640 /* It's possible that parsing the first pragma will load a PCH file,
641 which is a GC collection point. So we have to do that before
642 allocating any memory. */
643 cp_lexer_get_preprocessor_token (0, &token);
644 cp_parser_initial_pragma (&token);
645 c_common_no_more_pch ();
647 cp_lexer *lexer = cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 cp_token *tok = lexer->buffer->quick_push (token);
651 uintptr_t filter = 0;
652 if (modules_p ())
653 filter = module_token_cdtor (parse_in, filter);
655 /* Get the remaining tokens from the preprocessor. */
656 while (tok->type != CPP_EOF)
658 if (filter)
659 /* Process the previous token. */
660 module_token_lang (tok->type, tok->keyword, tok->u.value,
661 tok->location, filter);
662 tok = vec_safe_push (lexer->buffer, cp_token ());
663 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
666 lexer->next_token = lexer->buffer->address ();
667 lexer->last_token = lexer->next_token
668 + lexer->buffer->length ()
669 - 1;
671 if (lexer->buffer->length () != 1)
673 /* Set the EOF token's location to be the just after the previous
674 token's range. That way 'at-eof' diagnostics point at something
675 meaninful. */
676 auto range = get_range_from_loc (line_table, tok[-1].location);
677 tok[0].location
678 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
681 if (filter)
682 module_token_cdtor (parse_in, filter);
684 /* Subsequent preprocessor diagnostics should use compiler
685 diagnostic functions to get the compiler source location. */
686 done_lexing = true;
688 maybe_check_all_macros (parse_in);
690 gcc_assert (!lexer->next_token->purged_p);
691 return lexer;
694 /* Create a new lexer whose token stream is primed with the tokens in
695 CACHE. When these tokens are exhausted, no new tokens will be read. */
697 static cp_lexer *
698 cp_lexer_new_from_tokens (cp_token_cache *cache)
700 cp_token *first = cache->first;
701 cp_token *last = cache->last;
702 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
704 /* We do not own the buffer. */
705 lexer->buffer = NULL;
707 /* Insert an EOF token. */
708 lexer->saved_type = last->type;
709 lexer->saved_keyword = last->keyword;
710 last->type = CPP_EOF;
711 last->keyword = RID_MAX;
713 lexer->next_token = first;
714 lexer->last_token = last;
716 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
718 /* Initially we are not debugging. */
719 lexer->debugging_p = false;
721 gcc_assert (!lexer->next_token->purged_p
722 && !lexer->last_token->purged_p);
723 return lexer;
726 /* Frees all resources associated with LEXER. */
728 static void
729 cp_lexer_destroy (cp_lexer *lexer)
731 if (lexer->buffer)
732 vec_free (lexer->buffer);
733 else
735 /* Restore the token we overwrite with EOF. */
736 lexer->last_token->type = lexer->saved_type;
737 lexer->last_token->keyword = lexer->saved_keyword;
739 lexer->saved_tokens.release ();
740 ggc_free (lexer);
743 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
744 be used. The point of this flag is to help the compiler to fold away calls
745 to cp_lexer_debugging_p within this source file at compile time, when the
746 lexer is not being debugged. */
748 #define LEXER_DEBUGGING_ENABLED_P false
750 /* Returns nonzero if debugging information should be output. */
752 static inline bool
753 cp_lexer_debugging_p (cp_lexer *lexer)
755 if (!LEXER_DEBUGGING_ENABLED_P)
756 return false;
758 return lexer->debugging_p;
762 static inline cp_token_position
763 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
765 return lexer->next_token - previous_p;
768 static inline cp_token *
769 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
771 return pos;
774 static inline void
775 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
777 lexer->next_token = cp_lexer_token_at (lexer, pos);
780 static inline cp_token_position
781 cp_lexer_previous_token_position (cp_lexer *lexer)
783 return cp_lexer_token_position (lexer, true);
786 static inline cp_token *
787 cp_lexer_previous_token (cp_lexer *lexer)
789 cp_token_position tp = cp_lexer_previous_token_position (lexer);
791 /* Skip past purged tokens. */
792 while (tp->purged_p)
794 gcc_assert (tp != vec_safe_address (lexer->buffer));
795 tp--;
798 return cp_lexer_token_at (lexer, tp);
801 /* Same as above, but return NULL when the lexer doesn't own the token
802 buffer or if the next_token is at the start of the token
803 vector or if all previous tokens are purged. */
805 static cp_token *
806 cp_lexer_safe_previous_token (cp_lexer *lexer)
808 if (lexer->buffer
809 && lexer->next_token != lexer->buffer->address ())
811 cp_token_position tp = cp_lexer_previous_token_position (lexer);
813 /* Skip past purged tokens. */
814 while (tp->purged_p)
816 if (tp == lexer->buffer->address ())
817 return NULL;
818 tp--;
820 return cp_lexer_token_at (lexer, tp);
823 return NULL;
826 /* Overload for make_location, taking the lexer to mean the location of the
827 previous token. */
829 static inline location_t
830 make_location (location_t caret, location_t start, cp_lexer *lexer)
832 cp_token *t = cp_lexer_previous_token (lexer);
833 return make_location (caret, start, t->location);
836 /* Overload for make_location taking tokens instead of locations. */
838 static inline location_t
839 make_location (cp_token *caret, cp_token *start, cp_token *end)
841 return make_location (caret->location, start->location, end->location);
844 /* nonzero if we are presently saving tokens. */
846 static inline int
847 cp_lexer_saving_tokens (const cp_lexer* lexer)
849 return lexer->saved_tokens.length () != 0;
852 /* Store the next token from the preprocessor in *TOKEN. Return true
853 if we reach EOF. If LEXER is NULL, assume we are handling an
854 initial #pragma pch_preprocess, and thus want the lexer to return
855 processed strings. */
857 static void
858 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
860 static int is_extern_c = 0;
862 /* Get a new token from the preprocessor. */
863 token->type
864 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
865 flags);
866 token->keyword = RID_MAX;
867 token->purged_p = false;
868 token->error_reported = false;
869 token->tree_check_p = false;
870 /* Usually never see a zero, but just in case ... */
871 token->main_source_p = line_table->depth <= 1;
873 /* On some systems, some header files are surrounded by an
874 implicit extern "C" block. Set a flag in the token if it
875 comes from such a header. */
876 is_extern_c += pending_lang_change;
877 pending_lang_change = 0;
878 token->implicit_extern_c = is_extern_c > 0;
880 /* Check to see if this token is a keyword. */
881 if (token->type == CPP_NAME)
883 if (IDENTIFIER_KEYWORD_P (token->u.value))
885 /* Mark this token as a keyword. */
886 token->type = CPP_KEYWORD;
887 /* Record which keyword. */
888 token->keyword = C_RID_CODE (token->u.value);
890 else
892 if (warn_cxx11_compat
893 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
894 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
896 /* Warn about the C++0x keyword (but still treat it as
897 an identifier). */
898 warning_at (token->location, OPT_Wc__11_compat,
899 "identifier %qE is a keyword in C++11",
900 token->u.value);
902 /* Clear out the C_RID_CODE so we don't warn about this
903 particular identifier-turned-keyword again. */
904 C_SET_RID_CODE (token->u.value, RID_MAX);
906 if (warn_cxx20_compat
907 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
908 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
910 /* Warn about the C++20 keyword (but still treat it as
911 an identifier). */
912 warning_at (token->location, OPT_Wc__20_compat,
913 "identifier %qE is a keyword in C++20",
914 token->u.value);
916 /* Clear out the C_RID_CODE so we don't warn about this
917 particular identifier-turned-keyword again. */
918 C_SET_RID_CODE (token->u.value, RID_MAX);
921 token->keyword = RID_MAX;
924 else if (token->type == CPP_AT_NAME)
926 /* This only happens in Objective-C++; it must be a keyword. */
927 token->type = CPP_KEYWORD;
928 switch (C_RID_CODE (token->u.value))
930 /* Replace 'class' with '@class', 'private' with '@private',
931 etc. This prevents confusion with the C++ keyword
932 'class', and makes the tokens consistent with other
933 Objective-C 'AT' keywords. For example '@class' is
934 reported as RID_AT_CLASS which is consistent with
935 '@synchronized', which is reported as
936 RID_AT_SYNCHRONIZED.
938 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
939 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
940 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
941 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
942 case RID_THROW: token->keyword = RID_AT_THROW; break;
943 case RID_TRY: token->keyword = RID_AT_TRY; break;
944 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
945 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
946 default: token->keyword = C_RID_CODE (token->u.value);
951 /* Update the globals input_location and the input file stack from TOKEN. */
952 static inline void
953 cp_lexer_set_source_position_from_token (cp_token *token)
955 input_location = token->location;
958 /* Update the globals input_location and the input file stack from LEXER. */
959 static inline void
960 cp_lexer_set_source_position (cp_lexer *lexer)
962 cp_token *token = cp_lexer_peek_token (lexer);
963 cp_lexer_set_source_position_from_token (token);
966 /* Return a pointer to the next token in the token stream, but do not
967 consume it. */
969 static inline cp_token *
970 cp_lexer_peek_token (cp_lexer *lexer)
972 if (cp_lexer_debugging_p (lexer))
974 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
975 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
976 putc ('\n', cp_lexer_debug_stream);
978 return lexer->next_token;
981 /* Return true if the next token has the indicated TYPE. */
983 static inline bool
984 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
986 return cp_lexer_peek_token (lexer)->type == type;
989 /* Return true if the next token does not have the indicated TYPE. */
991 static inline bool
992 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
994 return !cp_lexer_next_token_is (lexer, type);
997 /* Return true if the next token is the indicated KEYWORD. */
999 static inline bool
1000 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1002 return cp_lexer_peek_token (lexer)->keyword == keyword;
1005 static inline bool
1006 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1008 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1011 static inline bool
1012 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1014 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1017 /* Return true if KEYWORD can start a decl-specifier. */
1019 bool
1020 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1022 switch (keyword)
1024 /* auto specifier: storage-class-specifier in C++,
1025 simple-type-specifier in C++0x. */
1026 case RID_AUTO:
1027 /* Storage classes. */
1028 case RID_REGISTER:
1029 case RID_STATIC:
1030 case RID_EXTERN:
1031 case RID_MUTABLE:
1032 case RID_THREAD:
1033 /* Elaborated type specifiers. */
1034 case RID_ENUM:
1035 case RID_CLASS:
1036 case RID_STRUCT:
1037 case RID_UNION:
1038 case RID_TYPENAME:
1039 /* Simple type specifiers. */
1040 case RID_CHAR:
1041 case RID_CHAR8:
1042 case RID_CHAR16:
1043 case RID_CHAR32:
1044 case RID_WCHAR:
1045 case RID_BOOL:
1046 case RID_SHORT:
1047 case RID_INT:
1048 case RID_LONG:
1049 case RID_SIGNED:
1050 case RID_UNSIGNED:
1051 case RID_FLOAT:
1052 case RID_DOUBLE:
1053 case RID_VOID:
1054 /* CV qualifiers. */
1055 case RID_CONST:
1056 case RID_VOLATILE:
1057 /* Function specifiers. */
1058 case RID_EXPLICIT:
1059 case RID_VIRTUAL:
1060 /* friend/typdef/inline specifiers. */
1061 case RID_FRIEND:
1062 case RID_TYPEDEF:
1063 case RID_INLINE:
1064 /* GNU extensions. */
1065 case RID_TYPEOF:
1066 /* C++11 extensions. */
1067 case RID_DECLTYPE:
1068 case RID_UNDERLYING_TYPE:
1069 case RID_CONSTEXPR:
1070 /* C++20 extensions. */
1071 case RID_CONSTINIT:
1072 case RID_CONSTEVAL:
1073 return true;
1075 default:
1076 if (keyword >= RID_FIRST_INT_N
1077 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1078 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1079 return true;
1080 return false;
1084 /* Return true if the next token is a keyword for a decl-specifier. */
1086 static bool
1087 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1089 cp_token *token;
1091 token = cp_lexer_peek_token (lexer);
1092 return cp_keyword_starts_decl_specifier_p (token->keyword);
1095 /* Returns TRUE iff the token T begins a decltype type. */
1097 static bool
1098 token_is_decltype (cp_token *t)
1100 return (t->keyword == RID_DECLTYPE
1101 || t->type == CPP_DECLTYPE);
1104 /* Returns TRUE iff the next token begins a decltype type. */
1106 static bool
1107 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1109 cp_token *t = cp_lexer_peek_token (lexer);
1110 return token_is_decltype (t);
1113 /* Called when processing a token with tree_check_value; perform or defer the
1114 associated checks and return the value. */
1116 static tree
1117 saved_checks_value (struct tree_check *check_value)
1119 /* Perform any access checks that were deferred. */
1120 vec<deferred_access_check, va_gc> *checks;
1121 deferred_access_check *chk;
1122 checks = check_value->checks;
1123 if (checks)
1125 int i;
1126 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1127 perform_or_defer_access_check (chk->binfo,
1128 chk->decl,
1129 chk->diag_decl, tf_warning_or_error);
1131 /* Return the stored value. */
1132 return check_value->value;
1135 /* Return a pointer to the Nth token in the token stream. If N is 1,
1136 then this is precisely equivalent to cp_lexer_peek_token (except
1137 that it is not inline). One would like to disallow that case, but
1138 there is one case (cp_parser_nth_token_starts_template_id) where
1139 the caller passes a variable for N and it might be 1. */
1141 static cp_token *
1142 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1144 cp_token *token;
1146 /* N is 1-based, not zero-based. */
1147 gcc_assert (n > 0);
1149 if (cp_lexer_debugging_p (lexer))
1150 fprintf (cp_lexer_debug_stream,
1151 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1153 --n;
1154 token = lexer->next_token;
1155 while (n && token->type != CPP_EOF)
1157 ++token;
1158 if (!token->purged_p)
1159 --n;
1162 if (cp_lexer_debugging_p (lexer))
1164 cp_lexer_print_token (cp_lexer_debug_stream, token);
1165 putc ('\n', cp_lexer_debug_stream);
1168 return token;
1171 /* Return the next token, and advance the lexer's next_token pointer
1172 to point to the next non-purged token. */
1174 static cp_token *
1175 cp_lexer_consume_token (cp_lexer* lexer)
1177 cp_token *token = lexer->next_token;
1181 gcc_assert (token->type != CPP_EOF);
1182 lexer->next_token++;
1184 while (lexer->next_token->purged_p);
1186 cp_lexer_set_source_position_from_token (token);
1188 /* Provide debugging output. */
1189 if (cp_lexer_debugging_p (lexer))
1191 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1192 cp_lexer_print_token (cp_lexer_debug_stream, token);
1193 putc ('\n', cp_lexer_debug_stream);
1196 return token;
1199 /* Permanently remove the next token from the token stream, and
1200 advance the next_token pointer to refer to the next non-purged
1201 token. */
1203 static void
1204 cp_lexer_purge_token (cp_lexer *lexer)
1206 cp_token *tok = lexer->next_token;
1208 gcc_assert (tok->type != CPP_EOF);
1209 tok->purged_p = true;
1210 tok->location = UNKNOWN_LOCATION;
1211 tok->u.value = NULL_TREE;
1212 tok->keyword = RID_MAX;
1215 tok++;
1216 while (tok->purged_p);
1217 lexer->next_token = tok;
1220 /* Permanently remove all tokens after TOK, up to, but not
1221 including, the token that will be returned next by
1222 cp_lexer_peek_token. */
1224 static void
1225 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1227 cp_token *peek = lexer->next_token;
1229 gcc_assert (tok < peek);
1231 for (tok++; tok != peek; tok++)
1233 tok->purged_p = true;
1234 tok->location = UNKNOWN_LOCATION;
1235 tok->u.value = NULL_TREE;
1236 tok->keyword = RID_MAX;
1240 /* Begin saving tokens. All tokens consumed after this point will be
1241 preserved. */
1243 static void
1244 cp_lexer_save_tokens (cp_lexer* lexer)
1246 /* Provide debugging output. */
1247 if (cp_lexer_debugging_p (lexer))
1248 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1250 lexer->saved_tokens.safe_push (lexer->next_token);
1253 /* Commit to the portion of the token stream most recently saved. */
1255 static void
1256 cp_lexer_commit_tokens (cp_lexer* lexer)
1258 /* Provide debugging output. */
1259 if (cp_lexer_debugging_p (lexer))
1260 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1262 lexer->saved_tokens.pop ();
1265 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1266 to the token stream. Stop saving tokens. */
1268 static void
1269 cp_lexer_rollback_tokens (cp_lexer* lexer)
1271 /* Provide debugging output. */
1272 if (cp_lexer_debugging_p (lexer))
1273 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1275 lexer->next_token = lexer->saved_tokens.pop ();
1278 /* RAII wrapper around the above functions, with sanity checking. Creating
1279 a variable saves tokens, which are committed when the variable is
1280 destroyed unless they are explicitly rolled back by calling the rollback
1281 member function. */
1283 struct saved_token_sentinel
1285 cp_lexer *lexer;
1286 unsigned len;
1287 bool commit;
1288 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1290 len = lexer->saved_tokens.length ();
1291 cp_lexer_save_tokens (lexer);
1293 void rollback ()
1295 cp_lexer_rollback_tokens (lexer);
1296 commit = false;
1298 ~saved_token_sentinel()
1300 if (commit)
1301 cp_lexer_commit_tokens (lexer);
1302 gcc_assert (lexer->saved_tokens.length () == len);
1306 /* Print a representation of the TOKEN on the STREAM. */
1308 static void
1309 cp_lexer_print_token (FILE * stream, cp_token *token)
1311 /* We don't use cpp_type2name here because the parser defines
1312 a few tokens of its own. */
1313 static const char *const token_names[] = {
1314 /* cpplib-defined token types */
1315 #define OP(e, s) #e,
1316 #define TK(e, s) #e,
1317 TTYPE_TABLE
1318 #undef OP
1319 #undef TK
1320 /* C++ parser token types - see "Manifest constants", above. */
1321 "KEYWORD",
1322 "TEMPLATE_ID",
1323 "NESTED_NAME_SPECIFIER",
1326 /* For some tokens, print the associated data. */
1327 switch (token->type)
1329 case CPP_KEYWORD:
1330 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1331 For example, `struct' is mapped to an INTEGER_CST. */
1332 if (!identifier_p (token->u.value))
1333 break;
1334 /* fall through */
1335 case CPP_NAME:
1336 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1337 break;
1339 case CPP_STRING:
1340 case CPP_STRING16:
1341 case CPP_STRING32:
1342 case CPP_WSTRING:
1343 case CPP_UTF8STRING:
1344 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1345 break;
1347 case CPP_NUMBER:
1348 print_generic_expr (stream, token->u.value);
1349 break;
1351 default:
1352 /* If we have a name for the token, print it out. Otherwise, we
1353 simply give the numeric code. */
1354 if (token->type < ARRAY_SIZE(token_names))
1355 fputs (token_names[token->type], stream);
1356 else
1357 fprintf (stream, "[%d]", token->type);
1358 break;
1362 DEBUG_FUNCTION void
1363 debug (cp_token &ref)
1365 cp_lexer_print_token (stderr, &ref);
1366 fprintf (stderr, "\n");
1369 DEBUG_FUNCTION void
1370 debug (cp_token *ptr)
1372 if (ptr)
1373 debug (*ptr);
1374 else
1375 fprintf (stderr, "<nil>\n");
1379 /* Start emitting debugging information. */
1381 static void
1382 cp_lexer_start_debugging (cp_lexer* lexer)
1384 if (!LEXER_DEBUGGING_ENABLED_P)
1385 fatal_error (input_location,
1386 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1388 lexer->debugging_p = true;
1389 cp_lexer_debug_stream = stderr;
1392 /* Stop emitting debugging information. */
1394 static void
1395 cp_lexer_stop_debugging (cp_lexer* lexer)
1397 if (!LEXER_DEBUGGING_ENABLED_P)
1398 fatal_error (input_location,
1399 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1401 lexer->debugging_p = false;
1402 cp_lexer_debug_stream = NULL;
1405 /* Create a new cp_token_cache, representing a range of tokens. */
1407 static cp_token_cache *
1408 cp_token_cache_new (cp_token *first, cp_token *last)
1410 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1411 cache->first = first;
1412 cache->last = last;
1413 return cache;
1416 /* Diagnose if #pragma omp declare simd isn't followed immediately
1417 by function declaration or definition. */
1419 static inline void
1420 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1422 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1424 error ("%<#pragma omp declare %s%> not immediately followed by "
1425 "function declaration or definition",
1426 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1427 parser->omp_declare_simd = NULL;
1431 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1432 and put that into "omp declare simd" attribute. */
1434 static inline void
1435 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1437 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1439 if (fndecl == error_mark_node)
1441 parser->omp_declare_simd = NULL;
1442 return;
1444 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1446 cp_ensure_no_omp_declare_simd (parser);
1447 return;
1452 /* Similarly, but for use in declaration parsing functions
1453 which call cp_parser_handle_directive_omp_attributes. */
1455 static inline void
1456 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1458 if (parser->omp_declare_simd != data)
1459 return;
1461 if (!parser->omp_declare_simd->error_seen
1462 && !parser->omp_declare_simd->fndecl_seen)
1463 error_at (parser->omp_declare_simd->loc,
1464 "%<declare %s%> directive not immediately followed by "
1465 "function declaration or definition",
1466 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1467 parser->omp_declare_simd = NULL;
1470 /* Diagnose if #pragma acc routine isn't followed immediately by function
1471 declaration or definition. */
1473 static inline void
1474 cp_ensure_no_oacc_routine (cp_parser *parser)
1476 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1478 error_at (parser->oacc_routine->loc,
1479 "%<#pragma acc routine%> not immediately followed by "
1480 "function declaration or definition");
1481 parser->oacc_routine = NULL;
1485 /* Decl-specifiers. */
1487 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1489 static void
1490 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1492 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1495 /* Declarators. */
1497 /* Nothing other than the parser should be creating declarators;
1498 declarators are a semi-syntactic representation of C++ entities.
1499 Other parts of the front end that need to create entities (like
1500 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1502 static cp_declarator *make_call_declarator
1503 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1504 tree, tree, tree, tree, location_t);
1505 static cp_declarator *make_array_declarator
1506 (cp_declarator *, tree);
1507 static cp_declarator *make_pointer_declarator
1508 (cp_cv_quals, cp_declarator *, tree);
1509 static cp_declarator *make_reference_declarator
1510 (cp_cv_quals, cp_declarator *, bool, tree);
1511 static cp_declarator *make_ptrmem_declarator
1512 (cp_cv_quals, tree, cp_declarator *, tree);
1514 /* An erroneous declarator. */
1515 static cp_declarator *cp_error_declarator;
1517 /* The obstack on which declarators and related data structures are
1518 allocated. */
1519 static struct obstack declarator_obstack;
1521 /* Alloc BYTES from the declarator memory pool. */
1523 static inline void *
1524 alloc_declarator (size_t bytes)
1526 return obstack_alloc (&declarator_obstack, bytes);
1529 /* Allocate a declarator of the indicated KIND. Clear fields that are
1530 common to all declarators. */
1532 static cp_declarator *
1533 make_declarator (cp_declarator_kind kind)
1535 cp_declarator *declarator;
1537 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1538 declarator->kind = kind;
1539 declarator->parenthesized = UNKNOWN_LOCATION;
1540 declarator->attributes = NULL_TREE;
1541 declarator->std_attributes = NULL_TREE;
1542 declarator->declarator = NULL;
1543 declarator->parameter_pack_p = false;
1544 declarator->id_loc = UNKNOWN_LOCATION;
1545 declarator->init_loc = UNKNOWN_LOCATION;
1547 return declarator;
1550 /* Make a declarator for a generalized identifier. If
1551 QUALIFYING_SCOPE is non-NULL, the identifier is
1552 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1553 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1554 is, if any. */
1556 static cp_declarator *
1557 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1558 special_function_kind sfk, location_t id_location)
1560 cp_declarator *declarator;
1562 /* It is valid to write:
1564 class C { void f(); };
1565 typedef C D;
1566 void D::f();
1568 The standard is not clear about whether `typedef const C D' is
1569 legal; as of 2002-09-15 the committee is considering that
1570 question. EDG 3.0 allows that syntax. Therefore, we do as
1571 well. */
1572 if (qualifying_scope && TYPE_P (qualifying_scope))
1573 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1575 gcc_assert (identifier_p (unqualified_name)
1576 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1577 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1579 declarator = make_declarator (cdk_id);
1580 declarator->u.id.qualifying_scope = qualifying_scope;
1581 declarator->u.id.unqualified_name = unqualified_name;
1582 declarator->u.id.sfk = sfk;
1583 declarator->id_loc = id_location;
1585 return declarator;
1588 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1589 of modifiers such as const or volatile to apply to the pointer
1590 type, represented as identifiers. ATTRIBUTES represent the attributes that
1591 appertain to the pointer or reference. */
1593 cp_declarator *
1594 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1595 tree attributes)
1597 cp_declarator *declarator;
1599 declarator = make_declarator (cdk_pointer);
1600 declarator->declarator = target;
1601 declarator->u.pointer.qualifiers = cv_qualifiers;
1602 declarator->u.pointer.class_type = NULL_TREE;
1603 if (target)
1605 declarator->id_loc = target->id_loc;
1606 declarator->parameter_pack_p = target->parameter_pack_p;
1607 target->parameter_pack_p = false;
1609 else
1610 declarator->parameter_pack_p = false;
1612 declarator->std_attributes = attributes;
1614 return declarator;
1617 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1618 represent the attributes that appertain to the pointer or
1619 reference. */
1621 cp_declarator *
1622 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1623 bool rvalue_ref, tree attributes)
1625 cp_declarator *declarator;
1627 declarator = make_declarator (cdk_reference);
1628 declarator->declarator = target;
1629 declarator->u.reference.qualifiers = cv_qualifiers;
1630 declarator->u.reference.rvalue_ref = rvalue_ref;
1631 if (target)
1633 declarator->id_loc = target->id_loc;
1634 declarator->parameter_pack_p = target->parameter_pack_p;
1635 target->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 declarator->std_attributes = attributes;
1642 return declarator;
1645 /* Like make_pointer_declarator -- but for a pointer to a non-static
1646 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1647 appertain to the pointer or reference. */
1649 cp_declarator *
1650 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1651 cp_declarator *pointee,
1652 tree attributes)
1654 cp_declarator *declarator;
1656 declarator = make_declarator (cdk_ptrmem);
1657 declarator->declarator = pointee;
1658 declarator->u.pointer.qualifiers = cv_qualifiers;
1659 declarator->u.pointer.class_type = class_type;
1661 if (pointee)
1663 declarator->parameter_pack_p = pointee->parameter_pack_p;
1664 pointee->parameter_pack_p = false;
1666 else
1667 declarator->parameter_pack_p = false;
1669 declarator->std_attributes = attributes;
1671 return declarator;
1674 /* Make a declarator for the function given by TARGET, with the
1675 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1676 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1677 indicates what exceptions can be thrown. */
1679 cp_declarator *
1680 make_call_declarator (cp_declarator *target,
1681 tree parms,
1682 cp_cv_quals cv_qualifiers,
1683 cp_virt_specifiers virt_specifiers,
1684 cp_ref_qualifier ref_qualifier,
1685 tree tx_qualifier,
1686 tree exception_specification,
1687 tree late_return_type,
1688 tree requires_clause,
1689 location_t parens_loc)
1691 cp_declarator *declarator;
1693 declarator = make_declarator (cdk_function);
1694 declarator->declarator = target;
1695 declarator->u.function.parameters = parms;
1696 declarator->u.function.qualifiers = cv_qualifiers;
1697 declarator->u.function.virt_specifiers = virt_specifiers;
1698 declarator->u.function.ref_qualifier = ref_qualifier;
1699 declarator->u.function.tx_qualifier = tx_qualifier;
1700 declarator->u.function.exception_specification = exception_specification;
1701 declarator->u.function.late_return_type = late_return_type;
1702 declarator->u.function.requires_clause = requires_clause;
1703 declarator->u.function.parens_loc = parens_loc;
1704 if (target)
1706 declarator->id_loc = target->id_loc;
1707 declarator->parameter_pack_p = target->parameter_pack_p;
1708 target->parameter_pack_p = false;
1710 else
1711 declarator->parameter_pack_p = false;
1713 return declarator;
1716 /* Make a declarator for an array of BOUNDS elements, each of which is
1717 defined by ELEMENT. */
1719 cp_declarator *
1720 make_array_declarator (cp_declarator *element, tree bounds)
1722 cp_declarator *declarator;
1724 declarator = make_declarator (cdk_array);
1725 declarator->declarator = element;
1726 declarator->u.array.bounds = bounds;
1727 if (element)
1729 declarator->id_loc = element->id_loc;
1730 declarator->parameter_pack_p = element->parameter_pack_p;
1731 element->parameter_pack_p = false;
1733 else
1734 declarator->parameter_pack_p = false;
1736 return declarator;
1739 /* Determine whether the declarator we've seen so far can be a
1740 parameter pack, when followed by an ellipsis. */
1741 static bool
1742 declarator_can_be_parameter_pack (cp_declarator *declarator)
1744 if (declarator && declarator->parameter_pack_p)
1745 /* We already saw an ellipsis. */
1746 return false;
1748 /* Search for a declarator name, or any other declarator that goes
1749 after the point where the ellipsis could appear in a parameter
1750 pack. If we find any of these, then this declarator cannot be
1751 made into a parameter pack. */
1752 bool found = false;
1753 while (declarator && !found)
1755 switch ((int)declarator->kind)
1757 case cdk_id:
1758 case cdk_array:
1759 case cdk_decomp:
1760 found = true;
1761 break;
1763 case cdk_error:
1764 return true;
1766 default:
1767 declarator = declarator->declarator;
1768 break;
1772 return !found;
1775 cp_parameter_declarator *no_parameters;
1777 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1778 DECLARATOR and DEFAULT_ARGUMENT. */
1780 cp_parameter_declarator *
1781 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1782 cp_declarator *declarator,
1783 tree default_argument,
1784 location_t loc,
1785 bool template_parameter_pack_p = false)
1787 cp_parameter_declarator *parameter;
1789 parameter = ((cp_parameter_declarator *)
1790 alloc_declarator (sizeof (cp_parameter_declarator)));
1791 parameter->next = NULL;
1792 if (decl_specifiers)
1793 parameter->decl_specifiers = *decl_specifiers;
1794 else
1795 clear_decl_specs (&parameter->decl_specifiers);
1796 parameter->declarator = declarator;
1797 parameter->default_argument = default_argument;
1798 parameter->template_parameter_pack_p = template_parameter_pack_p;
1799 parameter->loc = loc;
1801 return parameter;
1804 /* Returns true iff DECLARATOR is a declaration for a function. */
1806 static bool
1807 function_declarator_p (const cp_declarator *declarator)
1809 while (declarator)
1811 if (declarator->kind == cdk_function
1812 && declarator->declarator->kind == cdk_id)
1813 return true;
1814 if (declarator->kind == cdk_id
1815 || declarator->kind == cdk_decomp
1816 || declarator->kind == cdk_error)
1817 return false;
1818 declarator = declarator->declarator;
1820 return false;
1823 /* The parser. */
1825 /* Overview
1826 --------
1828 A cp_parser parses the token stream as specified by the C++
1829 grammar. Its job is purely parsing, not semantic analysis. For
1830 example, the parser breaks the token stream into declarators,
1831 expressions, statements, and other similar syntactic constructs.
1832 It does not check that the types of the expressions on either side
1833 of an assignment-statement are compatible, or that a function is
1834 not declared with a parameter of type `void'.
1836 The parser invokes routines elsewhere in the compiler to perform
1837 semantic analysis and to build up the abstract syntax tree for the
1838 code processed.
1840 The parser (and the template instantiation code, which is, in a
1841 way, a close relative of parsing) are the only parts of the
1842 compiler that should be calling push_scope and pop_scope, or
1843 related functions. The parser (and template instantiation code)
1844 keeps track of what scope is presently active; everything else
1845 should simply honor that. (The code that generates static
1846 initializers may also need to set the scope, in order to check
1847 access control correctly when emitting the initializers.)
1849 Methodology
1850 -----------
1852 The parser is of the standard recursive-descent variety. Upcoming
1853 tokens in the token stream are examined in order to determine which
1854 production to use when parsing a non-terminal. Some C++ constructs
1855 require arbitrary look ahead to disambiguate. For example, it is
1856 impossible, in the general case, to tell whether a statement is an
1857 expression or declaration without scanning the entire statement.
1858 Therefore, the parser is capable of "parsing tentatively." When the
1859 parser is not sure what construct comes next, it enters this mode.
1860 Then, while we attempt to parse the construct, the parser queues up
1861 error messages, rather than issuing them immediately, and saves the
1862 tokens it consumes. If the construct is parsed successfully, the
1863 parser "commits", i.e., it issues any queued error messages and
1864 the tokens that were being preserved are permanently discarded.
1865 If, however, the construct is not parsed successfully, the parser
1866 rolls back its state completely so that it can resume parsing using
1867 a different alternative.
1869 Future Improvements
1870 -------------------
1872 The performance of the parser could probably be improved substantially.
1873 We could often eliminate the need to parse tentatively by looking ahead
1874 a little bit. In some places, this approach might not entirely eliminate
1875 the need to parse tentatively, but it might still speed up the average
1876 case. */
1878 /* Flags that are passed to some parsing functions. These values can
1879 be bitwise-ored together. */
1881 enum
1883 /* No flags. */
1884 CP_PARSER_FLAGS_NONE = 0x0,
1885 /* The construct is optional. If it is not present, then no error
1886 should be issued. */
1887 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1888 /* When parsing a type-specifier, treat user-defined type-names
1889 as non-type identifiers. */
1890 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1891 /* When parsing a type-specifier, do not try to parse a class-specifier
1892 or enum-specifier. */
1893 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1894 /* When parsing a decl-specifier-seq, only allow type-specifier or
1895 constexpr. */
1896 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1897 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1898 for C++20 consteval. */
1899 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1900 /* When parsing a decl-specifier-seq, allow missing typename. */
1901 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1902 /* When parsing of the noexcept-specifier should be delayed. */
1903 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1904 /* When parsing a consteval declarator. */
1905 CP_PARSER_FLAGS_CONSTEVAL = 0x80
1908 /* This type is used for parameters and variables which hold
1909 combinations of the above flags. */
1910 typedef int cp_parser_flags;
1912 /* The different kinds of declarators we want to parse. */
1914 enum cp_parser_declarator_kind
1916 /* We want an abstract declarator. */
1917 CP_PARSER_DECLARATOR_ABSTRACT,
1918 /* We want a named declarator. */
1919 CP_PARSER_DECLARATOR_NAMED,
1920 /* We don't mind, but the name must be an unqualified-id. */
1921 CP_PARSER_DECLARATOR_EITHER
1924 /* The precedence values used to parse binary expressions. The minimum value
1925 of PREC must be 1, because zero is reserved to quickly discriminate
1926 binary operators from other tokens. */
1928 enum cp_parser_prec
1930 PREC_NOT_OPERATOR,
1931 PREC_LOGICAL_OR_EXPRESSION,
1932 PREC_LOGICAL_AND_EXPRESSION,
1933 PREC_INCLUSIVE_OR_EXPRESSION,
1934 PREC_EXCLUSIVE_OR_EXPRESSION,
1935 PREC_AND_EXPRESSION,
1936 PREC_EQUALITY_EXPRESSION,
1937 PREC_RELATIONAL_EXPRESSION,
1938 PREC_SPACESHIP_EXPRESSION,
1939 PREC_SHIFT_EXPRESSION,
1940 PREC_ADDITIVE_EXPRESSION,
1941 PREC_MULTIPLICATIVE_EXPRESSION,
1942 PREC_PM_EXPRESSION,
1943 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1946 /* A mapping from a token type to a corresponding tree node type, with a
1947 precedence value. */
1949 struct cp_parser_binary_operations_map_node
1951 /* The token type. */
1952 enum cpp_ttype token_type;
1953 /* The corresponding tree code. */
1954 enum tree_code tree_type;
1955 /* The precedence of this operator. */
1956 enum cp_parser_prec prec;
1959 struct cp_parser_expression_stack_entry
1961 /* Left hand side of the binary operation we are currently
1962 parsing. */
1963 cp_expr lhs;
1964 /* Original tree code for left hand side, if it was a binary
1965 expression itself (used for -Wparentheses). */
1966 enum tree_code lhs_type;
1967 /* Tree code for the binary operation we are parsing. */
1968 enum tree_code tree_type;
1969 /* Precedence of the binary operation we are parsing. */
1970 enum cp_parser_prec prec;
1971 /* Location of the binary operation we are parsing. */
1972 location_t loc;
1975 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1976 entries because precedence levels on the stack are monotonically
1977 increasing. */
1978 typedef struct cp_parser_expression_stack_entry
1979 cp_parser_expression_stack[NUM_PREC_VALUES];
1981 /* Prototypes. */
1983 /* Constructors and destructors. */
1985 static cp_parser_context *cp_parser_context_new
1986 (cp_parser_context *);
1988 /* Class variables. */
1990 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1992 /* The operator-precedence table used by cp_parser_binary_expression.
1993 Transformed into an associative array (binops_by_token) by
1994 cp_parser_new. */
1996 static const cp_parser_binary_operations_map_node binops[] = {
1997 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1998 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2000 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2001 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2002 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2004 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2005 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2007 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2008 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2010 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2012 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2013 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2014 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2015 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2017 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2018 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2020 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2022 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2024 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2026 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2028 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2031 /* The same as binops, but initialized by cp_parser_new so that
2032 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2033 for speed. */
2034 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2036 /* Constructors and destructors. */
2038 /* Construct a new context. The context below this one on the stack
2039 is given by NEXT. */
2041 static cp_parser_context *
2042 cp_parser_context_new (cp_parser_context* next)
2044 cp_parser_context *context;
2046 /* Allocate the storage. */
2047 if (cp_parser_context_free_list != NULL)
2049 /* Pull the first entry from the free list. */
2050 context = cp_parser_context_free_list;
2051 cp_parser_context_free_list = context->next;
2052 memset (context, 0, sizeof (*context));
2054 else
2055 context = ggc_cleared_alloc<cp_parser_context> ();
2057 /* No errors have occurred yet in this context. */
2058 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2059 /* If this is not the bottommost context, copy information that we
2060 need from the previous context. */
2061 if (next)
2063 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2064 expression, then we are parsing one in this context, too. */
2065 context->object_type = next->object_type;
2066 /* Thread the stack. */
2067 context->next = next;
2070 return context;
2073 /* Managing the unparsed function queues. */
2075 #define unparsed_funs_with_default_args \
2076 parser->unparsed_queues->last ().funs_with_default_args
2077 #define unparsed_funs_with_definitions \
2078 parser->unparsed_queues->last ().funs_with_definitions
2079 #define unparsed_nsdmis \
2080 parser->unparsed_queues->last ().nsdmis
2081 #define unparsed_noexcepts \
2082 parser->unparsed_queues->last ().noexcepts
2084 static void
2085 push_unparsed_function_queues (cp_parser *parser)
2087 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2088 vec_safe_push (parser->unparsed_queues, e);
2091 static void
2092 pop_unparsed_function_queues (cp_parser *parser)
2094 release_tree_vector (unparsed_funs_with_definitions);
2095 parser->unparsed_queues->pop ();
2098 /* Prototypes. */
2100 /* Constructors and destructors. */
2102 static cp_parser *cp_parser_new
2103 (cp_lexer *);
2105 /* Routines to parse various constructs.
2107 Those that return `tree' will return the error_mark_node (rather
2108 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2109 Sometimes, they will return an ordinary node if error-recovery was
2110 attempted, even though a parse error occurred. So, to check
2111 whether or not a parse error occurred, you should always use
2112 cp_parser_error_occurred. If the construct is optional (indicated
2113 either by an `_opt' in the name of the function that does the
2114 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2115 the construct is not present. */
2117 /* Lexical conventions [gram.lex] */
2119 static cp_expr cp_parser_identifier
2120 (cp_parser *);
2121 static cp_expr cp_parser_string_literal
2122 (cp_parser *, bool, bool, bool);
2123 static cp_expr cp_parser_userdef_char_literal
2124 (cp_parser *);
2125 static tree cp_parser_userdef_string_literal
2126 (tree);
2127 static cp_expr cp_parser_userdef_numeric_literal
2128 (cp_parser *);
2130 /* Basic concepts [gram.basic] */
2132 static void cp_parser_translation_unit (cp_parser *);
2134 /* Expressions [gram.expr] */
2136 static cp_expr cp_parser_primary_expression
2137 (cp_parser *, bool, bool, bool, cp_id_kind *);
2138 static cp_expr cp_parser_id_expression
2139 (cp_parser *, bool, bool, bool *, bool, bool);
2140 static cp_expr cp_parser_unqualified_id
2141 (cp_parser *, bool, bool, bool, bool);
2142 static tree cp_parser_nested_name_specifier_opt
2143 (cp_parser *, bool, bool, bool, bool, bool = false);
2144 static tree cp_parser_nested_name_specifier
2145 (cp_parser *, bool, bool, bool, bool);
2146 static tree cp_parser_qualifying_entity
2147 (cp_parser *, bool, bool, bool, bool, bool);
2148 static cp_expr cp_parser_postfix_expression
2149 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2150 static tree cp_parser_postfix_open_square_expression
2151 (cp_parser *, tree, bool, bool);
2152 static tree cp_parser_postfix_dot_deref_expression
2153 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2154 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2155 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2156 bool = false);
2157 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2158 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2159 static void cp_parser_pseudo_destructor_name
2160 (cp_parser *, tree, tree *, tree *);
2161 static cp_expr cp_parser_unary_expression
2162 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2163 static enum tree_code cp_parser_unary_operator
2164 (cp_token *);
2165 static tree cp_parser_has_attribute_expression
2166 (cp_parser *);
2167 static tree cp_parser_new_expression
2168 (cp_parser *);
2169 static vec<tree, va_gc> *cp_parser_new_placement
2170 (cp_parser *);
2171 static tree cp_parser_new_type_id
2172 (cp_parser *, tree *);
2173 static cp_declarator *cp_parser_new_declarator_opt
2174 (cp_parser *);
2175 static cp_declarator *cp_parser_direct_new_declarator
2176 (cp_parser *);
2177 static vec<tree, va_gc> *cp_parser_new_initializer
2178 (cp_parser *);
2179 static tree cp_parser_delete_expression
2180 (cp_parser *);
2181 static cp_expr cp_parser_cast_expression
2182 (cp_parser *, bool, bool, bool, cp_id_kind *);
2183 static cp_expr cp_parser_binary_expression
2184 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2185 static tree cp_parser_question_colon_clause
2186 (cp_parser *, cp_expr);
2187 static cp_expr cp_parser_assignment_expression
2188 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2189 static enum tree_code cp_parser_assignment_operator_opt
2190 (cp_parser *);
2191 static cp_expr cp_parser_expression
2192 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2193 static cp_expr cp_parser_constant_expression
2194 (cp_parser *, int = 0, bool * = NULL, bool = false);
2195 static cp_expr cp_parser_builtin_offsetof
2196 (cp_parser *);
2197 static cp_expr cp_parser_lambda_expression
2198 (cp_parser *);
2199 static void cp_parser_lambda_introducer
2200 (cp_parser *, tree);
2201 static bool cp_parser_lambda_declarator_opt
2202 (cp_parser *, tree);
2203 static void cp_parser_lambda_body
2204 (cp_parser *, tree);
2206 /* Statements [gram.stmt.stmt] */
2208 static void cp_parser_statement
2209 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2210 static void cp_parser_label_for_labeled_statement
2211 (cp_parser *, tree);
2212 static tree cp_parser_expression_statement
2213 (cp_parser *, tree);
2214 static tree cp_parser_compound_statement
2215 (cp_parser *, tree, int, bool);
2216 static void cp_parser_statement_seq_opt
2217 (cp_parser *, tree);
2218 static tree cp_parser_selection_statement
2219 (cp_parser *, bool *, vec<tree> *);
2220 static tree cp_parser_condition
2221 (cp_parser *);
2222 static tree cp_parser_iteration_statement
2223 (cp_parser *, bool *, bool, unsigned short);
2224 static bool cp_parser_init_statement
2225 (cp_parser *, tree *decl);
2226 static tree cp_parser_for
2227 (cp_parser *, bool, unsigned short);
2228 static tree cp_parser_c_for
2229 (cp_parser *, tree, tree, bool, unsigned short);
2230 static tree cp_parser_range_for
2231 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2232 static void do_range_for_auto_deduction
2233 (tree, tree);
2234 static tree cp_parser_perform_range_for_lookup
2235 (tree, tree *, tree *);
2236 static tree cp_parser_range_for_member_function
2237 (tree, tree);
2238 static tree cp_parser_jump_statement
2239 (cp_parser *);
2240 static void cp_parser_declaration_statement
2241 (cp_parser *);
2243 static tree cp_parser_implicitly_scoped_statement
2244 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2245 static void cp_parser_already_scoped_statement
2246 (cp_parser *, bool *, const token_indent_info &);
2248 /* State of module-declaration parsing. */
2249 enum module_parse
2251 MP_NOT_MODULE, /* Not a module. */
2253 _MP_UNUSED,
2255 MP_FIRST, /* First declaration of TU. */
2256 MP_GLOBAL, /* Global Module Fragment. */
2258 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2259 MP_PURVIEW, /* Purview of a named module. */
2261 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2262 MP_PRIVATE, /* Private Module Fragment. */
2265 static module_parse cp_parser_module_declaration
2266 (cp_parser *parser, module_parse, bool exporting);
2267 static void cp_parser_import_declaration
2268 (cp_parser *parser, module_parse, bool exporting);
2270 /* Declarations [gram.dcl.dcl] */
2272 static void cp_parser_declaration_seq_opt
2273 (cp_parser *);
2274 static void cp_parser_declaration
2275 (cp_parser *, tree);
2276 static void cp_parser_toplevel_declaration
2277 (cp_parser *);
2278 static void cp_parser_block_declaration
2279 (cp_parser *, bool);
2280 static void cp_parser_simple_declaration
2281 (cp_parser *, bool, tree *);
2282 static void cp_parser_decl_specifier_seq
2283 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2284 static tree cp_parser_storage_class_specifier_opt
2285 (cp_parser *);
2286 static tree cp_parser_function_specifier_opt
2287 (cp_parser *, cp_decl_specifier_seq *);
2288 static tree cp_parser_type_specifier
2289 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2290 int *, bool *);
2291 static tree cp_parser_simple_type_specifier
2292 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2293 static tree cp_parser_placeholder_type_specifier
2294 (cp_parser *, location_t, tree, bool);
2295 static tree cp_parser_type_name
2296 (cp_parser *, bool);
2297 static tree cp_parser_nonclass_name
2298 (cp_parser* parser);
2299 static tree cp_parser_elaborated_type_specifier
2300 (cp_parser *, bool, bool);
2301 static tree cp_parser_enum_specifier
2302 (cp_parser *);
2303 static void cp_parser_enumerator_list
2304 (cp_parser *, tree);
2305 static void cp_parser_enumerator_definition
2306 (cp_parser *, tree);
2307 static tree cp_parser_namespace_name
2308 (cp_parser *);
2309 static void cp_parser_namespace_definition
2310 (cp_parser *);
2311 static void cp_parser_namespace_body
2312 (cp_parser *);
2313 static tree cp_parser_qualified_namespace_specifier
2314 (cp_parser *);
2315 static void cp_parser_namespace_alias_definition
2316 (cp_parser *);
2317 static bool cp_parser_using_declaration
2318 (cp_parser *, bool);
2319 static void cp_parser_using_directive
2320 (cp_parser *);
2321 static void cp_parser_using_enum
2322 (cp_parser *);
2323 static tree cp_parser_alias_declaration
2324 (cp_parser *);
2325 static void cp_parser_asm_definition
2326 (cp_parser *);
2327 static void cp_parser_linkage_specification
2328 (cp_parser *, tree);
2329 static void cp_parser_static_assert
2330 (cp_parser *, bool);
2331 static tree cp_parser_decltype
2332 (cp_parser *);
2333 static tree cp_parser_decomposition_declaration
2334 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2336 /* Declarators [gram.dcl.decl] */
2338 static tree cp_parser_init_declarator
2339 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2340 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2341 location_t *, tree *);
2342 static cp_declarator *cp_parser_declarator
2343 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2344 bool, bool, bool);
2345 static cp_declarator *cp_parser_direct_declarator
2346 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2347 bool);
2348 static enum tree_code cp_parser_ptr_operator
2349 (cp_parser *, tree *, cp_cv_quals *, tree *);
2350 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2351 (cp_parser *);
2352 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2353 (cp_parser *);
2354 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2355 (cp_parser *);
2356 static tree cp_parser_tx_qualifier_opt
2357 (cp_parser *);
2358 static tree cp_parser_late_return_type_opt
2359 (cp_parser *, cp_declarator *, tree &);
2360 static tree cp_parser_declarator_id
2361 (cp_parser *, bool);
2362 static tree cp_parser_type_id
2363 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2364 static tree cp_parser_template_type_arg
2365 (cp_parser *);
2366 static tree cp_parser_trailing_type_id (cp_parser *);
2367 static tree cp_parser_type_id_1
2368 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2369 static void cp_parser_type_specifier_seq
2370 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2371 static tree cp_parser_parameter_declaration_clause
2372 (cp_parser *, cp_parser_flags);
2373 static tree cp_parser_parameter_declaration_list
2374 (cp_parser *, cp_parser_flags);
2375 static cp_parameter_declarator *cp_parser_parameter_declaration
2376 (cp_parser *, cp_parser_flags, bool, bool *);
2377 static tree cp_parser_default_argument
2378 (cp_parser *, bool);
2379 static void cp_parser_function_body
2380 (cp_parser *, bool);
2381 static tree cp_parser_initializer
2382 (cp_parser *, bool *, bool *, bool = false);
2383 static cp_expr cp_parser_initializer_clause
2384 (cp_parser *, bool *);
2385 static cp_expr cp_parser_braced_list
2386 (cp_parser*, bool*);
2387 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2388 (cp_parser *, bool *, bool *);
2390 static void cp_parser_ctor_initializer_opt_and_function_body
2391 (cp_parser *, bool);
2393 static tree cp_parser_late_parsing_omp_declare_simd
2394 (cp_parser *, tree);
2396 static tree cp_parser_late_parsing_oacc_routine
2397 (cp_parser *, tree);
2399 static tree synthesize_implicit_template_parm
2400 (cp_parser *, tree);
2401 static tree finish_fully_implicit_template
2402 (cp_parser *, tree);
2403 static void abort_fully_implicit_template
2404 (cp_parser *);
2406 /* Classes [gram.class] */
2408 static tree cp_parser_class_name
2409 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2410 static tree cp_parser_class_specifier
2411 (cp_parser *);
2412 static tree cp_parser_class_head
2413 (cp_parser *, bool *);
2414 static enum tag_types cp_parser_class_key
2415 (cp_parser *);
2416 static void cp_parser_type_parameter_key
2417 (cp_parser* parser);
2418 static void cp_parser_member_specification_opt
2419 (cp_parser *);
2420 static void cp_parser_member_declaration
2421 (cp_parser *);
2422 static tree cp_parser_pure_specifier
2423 (cp_parser *);
2424 static tree cp_parser_constant_initializer
2425 (cp_parser *);
2427 /* Derived classes [gram.class.derived] */
2429 static tree cp_parser_base_clause
2430 (cp_parser *);
2431 static tree cp_parser_base_specifier
2432 (cp_parser *);
2434 /* Special member functions [gram.special] */
2436 static tree cp_parser_conversion_function_id
2437 (cp_parser *);
2438 static tree cp_parser_conversion_type_id
2439 (cp_parser *);
2440 static cp_declarator *cp_parser_conversion_declarator_opt
2441 (cp_parser *);
2442 static void cp_parser_ctor_initializer_opt
2443 (cp_parser *);
2444 static void cp_parser_mem_initializer_list
2445 (cp_parser *);
2446 static tree cp_parser_mem_initializer
2447 (cp_parser *);
2448 static tree cp_parser_mem_initializer_id
2449 (cp_parser *);
2451 /* Overloading [gram.over] */
2453 static cp_expr cp_parser_operator_function_id
2454 (cp_parser *);
2455 static cp_expr cp_parser_operator
2456 (cp_parser *, location_t);
2458 /* Templates [gram.temp] */
2460 static void cp_parser_template_declaration
2461 (cp_parser *, bool);
2462 static tree cp_parser_template_parameter_list
2463 (cp_parser *);
2464 static tree cp_parser_template_parameter
2465 (cp_parser *, bool *, bool *);
2466 static tree cp_parser_type_parameter
2467 (cp_parser *, bool *);
2468 static tree cp_parser_template_id
2469 (cp_parser *, bool, bool, enum tag_types, bool);
2470 static tree cp_parser_template_id_expr
2471 (cp_parser *, bool, bool, bool);
2472 static tree cp_parser_template_name
2473 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2474 static tree cp_parser_template_argument_list
2475 (cp_parser *);
2476 static tree cp_parser_template_argument
2477 (cp_parser *);
2478 static void cp_parser_explicit_instantiation
2479 (cp_parser *);
2480 static void cp_parser_explicit_specialization
2481 (cp_parser *);
2483 /* Exception handling [gram.except] */
2485 static tree cp_parser_try_block
2486 (cp_parser *);
2487 static void cp_parser_function_try_block
2488 (cp_parser *);
2489 static void cp_parser_handler_seq
2490 (cp_parser *);
2491 static void cp_parser_handler
2492 (cp_parser *);
2493 static tree cp_parser_exception_declaration
2494 (cp_parser *);
2495 static tree cp_parser_throw_expression
2496 (cp_parser *);
2497 static tree cp_parser_exception_specification_opt
2498 (cp_parser *, cp_parser_flags);
2499 static tree cp_parser_type_id_list
2500 (cp_parser *);
2501 static tree cp_parser_noexcept_specification_opt
2502 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2504 /* GNU Extensions */
2506 static tree cp_parser_asm_specification_opt
2507 (cp_parser *);
2508 static tree cp_parser_asm_operand_list
2509 (cp_parser *);
2510 static tree cp_parser_asm_clobber_list
2511 (cp_parser *);
2512 static tree cp_parser_asm_label_list
2513 (cp_parser *);
2514 static bool cp_next_tokens_can_be_attribute_p
2515 (cp_parser *);
2516 static bool cp_next_tokens_can_be_gnu_attribute_p
2517 (cp_parser *);
2518 static bool cp_next_tokens_can_be_std_attribute_p
2519 (cp_parser *);
2520 static bool cp_nth_tokens_can_be_std_attribute_p
2521 (cp_parser *, size_t);
2522 static bool cp_nth_tokens_can_be_gnu_attribute_p
2523 (cp_parser *, size_t);
2524 static bool cp_nth_tokens_can_be_attribute_p
2525 (cp_parser *, size_t);
2526 static tree cp_parser_attributes_opt
2527 (cp_parser *);
2528 static tree cp_parser_gnu_attributes_opt
2529 (cp_parser *);
2530 static tree cp_parser_gnu_attribute_list
2531 (cp_parser *, bool = false);
2532 static tree cp_parser_std_attribute
2533 (cp_parser *, tree);
2534 static tree cp_parser_std_attribute_spec
2535 (cp_parser *);
2536 static tree cp_parser_std_attribute_spec_seq
2537 (cp_parser *);
2538 static size_t cp_parser_skip_std_attribute_spec_seq
2539 (cp_parser *, size_t);
2540 static size_t cp_parser_skip_attributes_opt
2541 (cp_parser *, size_t);
2542 static bool cp_parser_extension_opt
2543 (cp_parser *, int *);
2544 static void cp_parser_label_declaration
2545 (cp_parser *);
2547 /* Concept Extensions */
2549 static tree cp_parser_concept_definition
2550 (cp_parser *);
2551 static tree cp_parser_constraint_expression
2552 (cp_parser *);
2553 static tree cp_parser_requires_clause_opt
2554 (cp_parser *, bool);
2555 static tree cp_parser_requires_expression
2556 (cp_parser *);
2557 static tree cp_parser_requirement_parameter_list
2558 (cp_parser *);
2559 static tree cp_parser_requirement_body
2560 (cp_parser *);
2561 static tree cp_parser_requirement_seq
2562 (cp_parser *);
2563 static tree cp_parser_requirement
2564 (cp_parser *);
2565 static tree cp_parser_simple_requirement
2566 (cp_parser *);
2567 static tree cp_parser_compound_requirement
2568 (cp_parser *);
2569 static tree cp_parser_type_requirement
2570 (cp_parser *);
2571 static tree cp_parser_nested_requirement
2572 (cp_parser *);
2574 /* Transactional Memory Extensions */
2576 static tree cp_parser_transaction
2577 (cp_parser *, cp_token *);
2578 static tree cp_parser_transaction_expression
2579 (cp_parser *, enum rid);
2580 static void cp_parser_function_transaction
2581 (cp_parser *, enum rid);
2582 static tree cp_parser_transaction_cancel
2583 (cp_parser *);
2585 /* Coroutine extensions. */
2587 static tree cp_parser_yield_expression
2588 (cp_parser *);
2591 enum pragma_context {
2592 pragma_external,
2593 pragma_member,
2594 pragma_objc_icode,
2595 pragma_stmt,
2596 pragma_compound
2598 static bool cp_parser_pragma
2599 (cp_parser *, enum pragma_context, bool *);
2601 /* Objective-C++ Productions */
2603 static tree cp_parser_objc_message_receiver
2604 (cp_parser *);
2605 static tree cp_parser_objc_message_args
2606 (cp_parser *);
2607 static tree cp_parser_objc_message_expression
2608 (cp_parser *);
2609 static cp_expr cp_parser_objc_encode_expression
2610 (cp_parser *);
2611 static tree cp_parser_objc_defs_expression
2612 (cp_parser *);
2613 static tree cp_parser_objc_protocol_expression
2614 (cp_parser *);
2615 static tree cp_parser_objc_selector_expression
2616 (cp_parser *);
2617 static cp_expr cp_parser_objc_expression
2618 (cp_parser *);
2619 static bool cp_parser_objc_selector_p
2620 (enum cpp_ttype);
2621 static tree cp_parser_objc_selector
2622 (cp_parser *);
2623 static tree cp_parser_objc_protocol_refs_opt
2624 (cp_parser *);
2625 static void cp_parser_objc_declaration
2626 (cp_parser *, tree);
2627 static tree cp_parser_objc_statement
2628 (cp_parser *);
2629 static bool cp_parser_objc_valid_prefix_attributes
2630 (cp_parser *, tree *);
2631 static void cp_parser_objc_at_property_declaration
2632 (cp_parser *) ;
2633 static void cp_parser_objc_at_synthesize_declaration
2634 (cp_parser *) ;
2635 static void cp_parser_objc_at_dynamic_declaration
2636 (cp_parser *) ;
2637 static tree cp_parser_objc_struct_declaration
2638 (cp_parser *) ;
2640 /* Utility Routines */
2642 static cp_expr cp_parser_lookup_name
2643 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2644 static tree cp_parser_lookup_name_simple
2645 (cp_parser *, tree, location_t);
2646 static tree cp_parser_maybe_treat_template_as_class
2647 (tree, bool);
2648 static bool cp_parser_check_declarator_template_parameters
2649 (cp_parser *, cp_declarator *, location_t);
2650 static bool cp_parser_check_template_parameters
2651 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2652 static cp_expr cp_parser_simple_cast_expression
2653 (cp_parser *);
2654 static tree cp_parser_global_scope_opt
2655 (cp_parser *, bool);
2656 static bool cp_parser_constructor_declarator_p
2657 (cp_parser *, cp_parser_flags, bool);
2658 static tree cp_parser_function_definition_from_specifiers_and_declarator
2659 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2660 static tree cp_parser_function_definition_after_declarator
2661 (cp_parser *, bool);
2662 static bool cp_parser_template_declaration_after_export
2663 (cp_parser *, bool);
2664 static void cp_parser_perform_template_parameter_access_checks
2665 (vec<deferred_access_check, va_gc> *);
2666 static tree cp_parser_single_declaration
2667 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2668 static cp_expr cp_parser_functional_cast
2669 (cp_parser *, tree);
2670 static tree cp_parser_save_member_function_body
2671 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2672 static tree cp_parser_save_nsdmi
2673 (cp_parser *);
2674 static tree cp_parser_enclosed_template_argument_list
2675 (cp_parser *);
2676 static void cp_parser_save_default_args
2677 (cp_parser *, tree);
2678 static void cp_parser_late_parsing_for_member
2679 (cp_parser *, tree);
2680 static tree cp_parser_late_parse_one_default_arg
2681 (cp_parser *, tree, tree, tree);
2682 static void cp_parser_late_parsing_nsdmi
2683 (cp_parser *, tree);
2684 static void cp_parser_late_parsing_default_args
2685 (cp_parser *, tree);
2686 static tree cp_parser_sizeof_operand
2687 (cp_parser *, enum rid);
2688 static cp_expr cp_parser_trait_expr
2689 (cp_parser *, enum rid);
2690 static bool cp_parser_declares_only_class_p
2691 (cp_parser *);
2692 static void cp_parser_set_storage_class
2693 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2694 static void cp_parser_set_decl_spec_type
2695 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2696 static void set_and_check_decl_spec_loc
2697 (cp_decl_specifier_seq *decl_specs,
2698 cp_decl_spec ds, cp_token *);
2699 static bool cp_parser_friend_p
2700 (const cp_decl_specifier_seq *);
2701 static void cp_parser_required_error
2702 (cp_parser *, required_token, bool, location_t);
2703 static cp_token *cp_parser_require
2704 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2705 static cp_token *cp_parser_require_keyword
2706 (cp_parser *, enum rid, required_token);
2707 static bool cp_parser_token_starts_function_definition_p
2708 (cp_token *);
2709 static bool cp_parser_next_token_starts_class_definition_p
2710 (cp_parser *);
2711 static bool cp_parser_next_token_ends_template_argument_p
2712 (cp_parser *);
2713 static bool cp_parser_nth_token_starts_template_argument_list_p
2714 (cp_parser *, size_t);
2715 static enum tag_types cp_parser_token_is_class_key
2716 (cp_token *);
2717 static enum tag_types cp_parser_token_is_type_parameter_key
2718 (cp_token *);
2719 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2720 static void cp_parser_check_class_key
2721 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2722 static void cp_parser_check_access_in_redeclaration
2723 (tree type, location_t location);
2724 static bool cp_parser_optional_template_keyword
2725 (cp_parser *);
2726 static void cp_parser_pre_parsed_nested_name_specifier
2727 (cp_parser *);
2728 static bool cp_parser_cache_group
2729 (cp_parser *, enum cpp_ttype, unsigned);
2730 static tree cp_parser_cache_defarg
2731 (cp_parser *parser, bool nsdmi);
2732 static void cp_parser_parse_tentatively
2733 (cp_parser *);
2734 static void cp_parser_commit_to_tentative_parse
2735 (cp_parser *);
2736 static void cp_parser_commit_to_topmost_tentative_parse
2737 (cp_parser *);
2738 static void cp_parser_abort_tentative_parse
2739 (cp_parser *);
2740 static bool cp_parser_parse_definitely
2741 (cp_parser *);
2742 static inline bool cp_parser_parsing_tentatively
2743 (cp_parser *);
2744 static bool cp_parser_uncommitted_to_tentative_parse_p
2745 (cp_parser *);
2746 static void cp_parser_error
2747 (cp_parser *, const char *);
2748 static void cp_parser_name_lookup_error
2749 (cp_parser *, tree, tree, name_lookup_error, location_t);
2750 static bool cp_parser_simulate_error
2751 (cp_parser *);
2752 static bool cp_parser_check_type_definition
2753 (cp_parser *);
2754 static void cp_parser_check_for_definition_in_return_type
2755 (cp_declarator *, tree, location_t type_location);
2756 static void cp_parser_check_for_invalid_template_id
2757 (cp_parser *, tree, enum tag_types, location_t location);
2758 static bool cp_parser_non_integral_constant_expression
2759 (cp_parser *, non_integral_constant);
2760 static void cp_parser_diagnose_invalid_type_name
2761 (cp_parser *, tree, location_t);
2762 static bool cp_parser_parse_and_diagnose_invalid_type_name
2763 (cp_parser *);
2764 static int cp_parser_skip_to_closing_parenthesis
2765 (cp_parser *, bool, bool, bool);
2766 static void cp_parser_skip_to_end_of_statement
2767 (cp_parser *);
2768 static void cp_parser_consume_semicolon_at_end_of_statement
2769 (cp_parser *);
2770 static void cp_parser_skip_to_end_of_block_or_statement
2771 (cp_parser *);
2772 static bool cp_parser_skip_to_closing_brace
2773 (cp_parser *);
2774 static void cp_parser_skip_to_end_of_template_parameter_list
2775 (cp_parser *);
2776 static void cp_parser_skip_to_pragma_eol
2777 (cp_parser*, cp_token *);
2778 static bool cp_parser_error_occurred
2779 (cp_parser *);
2780 static bool cp_parser_allow_gnu_extensions_p
2781 (cp_parser *);
2782 static bool cp_parser_is_pure_string_literal
2783 (cp_token *);
2784 static bool cp_parser_is_string_literal
2785 (cp_token *);
2786 static bool cp_parser_is_keyword
2787 (cp_token *, enum rid);
2788 static tree cp_parser_make_typename_type
2789 (cp_parser *, tree, location_t location);
2790 static cp_declarator * cp_parser_make_indirect_declarator
2791 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2792 static bool cp_parser_compound_literal_p
2793 (cp_parser *);
2794 static bool cp_parser_array_designator_p
2795 (cp_parser *);
2796 static bool cp_parser_init_statement_p
2797 (cp_parser *);
2798 static bool cp_parser_skip_to_closing_square_bracket
2799 (cp_parser *);
2800 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2802 // -------------------------------------------------------------------------- //
2803 // Unevaluated Operand Guard
2805 // Implementation of an RAII helper for unevaluated operand parsing.
2806 cp_unevaluated::cp_unevaluated ()
2808 ++cp_unevaluated_operand;
2809 ++c_inhibit_evaluation_warnings;
2812 cp_unevaluated::~cp_unevaluated ()
2814 --c_inhibit_evaluation_warnings;
2815 --cp_unevaluated_operand;
2818 // -------------------------------------------------------------------------- //
2819 // Tentative Parsing
2821 /* Returns nonzero if we are parsing tentatively. */
2823 static inline bool
2824 cp_parser_parsing_tentatively (cp_parser* parser)
2826 return parser->context->next != NULL;
2829 /* Returns nonzero if TOKEN is a string literal. */
2831 static bool
2832 cp_parser_is_pure_string_literal (cp_token* token)
2834 return (token->type == CPP_STRING ||
2835 token->type == CPP_STRING16 ||
2836 token->type == CPP_STRING32 ||
2837 token->type == CPP_WSTRING ||
2838 token->type == CPP_UTF8STRING);
2841 /* Returns nonzero if TOKEN is a string literal
2842 of a user-defined string literal. */
2844 static bool
2845 cp_parser_is_string_literal (cp_token* token)
2847 return (cp_parser_is_pure_string_literal (token) ||
2848 token->type == CPP_STRING_USERDEF ||
2849 token->type == CPP_STRING16_USERDEF ||
2850 token->type == CPP_STRING32_USERDEF ||
2851 token->type == CPP_WSTRING_USERDEF ||
2852 token->type == CPP_UTF8STRING_USERDEF);
2855 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2857 static bool
2858 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2860 return token->keyword == keyword;
2863 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2864 PRAGMA_NONE. */
2866 static enum pragma_kind
2867 cp_parser_pragma_kind (cp_token *token)
2869 if (token->type != CPP_PRAGMA)
2870 return PRAGMA_NONE;
2871 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2872 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2875 /* Helper function for cp_parser_error.
2876 Having peeked a token of kind TOK1_KIND that might signify
2877 a conflict marker, peek successor tokens to determine
2878 if we actually do have a conflict marker.
2879 Specifically, we consider a run of 7 '<', '=' or '>' characters
2880 at the start of a line as a conflict marker.
2881 These come through the lexer as three pairs and a single,
2882 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2883 If it returns true, *OUT_LOC is written to with the location/range
2884 of the marker. */
2886 static bool
2887 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2888 location_t *out_loc)
2890 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2891 if (token2->type != tok1_kind)
2892 return false;
2893 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2894 if (token3->type != tok1_kind)
2895 return false;
2896 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2897 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2898 return false;
2900 /* It must be at the start of the line. */
2901 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2902 if (LOCATION_COLUMN (start_loc) != 1)
2903 return false;
2905 /* We have a conflict marker. Construct a location of the form:
2906 <<<<<<<
2907 ^~~~~~~
2908 with start == caret, finishing at the end of the marker. */
2909 location_t finish_loc = get_finish (token4->location);
2910 *out_loc = make_location (start_loc, start_loc, finish_loc);
2912 return true;
2915 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2916 RT_CLOSE_PAREN. */
2918 static const char *
2919 get_matching_symbol (required_token token_desc)
2921 switch (token_desc)
2923 default:
2924 gcc_unreachable ();
2925 return "";
2926 case RT_CLOSE_BRACE:
2927 return "{";
2928 case RT_CLOSE_PAREN:
2929 return "(";
2933 /* Attempt to convert TOKEN_DESC from a required_token to an
2934 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2936 static enum cpp_ttype
2937 get_required_cpp_ttype (required_token token_desc)
2939 switch (token_desc)
2941 case RT_SEMICOLON:
2942 return CPP_SEMICOLON;
2943 case RT_OPEN_PAREN:
2944 return CPP_OPEN_PAREN;
2945 case RT_CLOSE_BRACE:
2946 return CPP_CLOSE_BRACE;
2947 case RT_OPEN_BRACE:
2948 return CPP_OPEN_BRACE;
2949 case RT_CLOSE_SQUARE:
2950 return CPP_CLOSE_SQUARE;
2951 case RT_OPEN_SQUARE:
2952 return CPP_OPEN_SQUARE;
2953 case RT_COMMA:
2954 return CPP_COMMA;
2955 case RT_COLON:
2956 return CPP_COLON;
2957 case RT_CLOSE_PAREN:
2958 return CPP_CLOSE_PAREN;
2960 default:
2961 /* Use CPP_EOF as a "no completions possible" code. */
2962 return CPP_EOF;
2967 /* Subroutine of cp_parser_error and cp_parser_required_error.
2969 Issue a diagnostic of the form
2970 FILE:LINE: MESSAGE before TOKEN
2971 where TOKEN is the next token in the input stream. MESSAGE
2972 (specified by the caller) is usually of the form "expected
2973 OTHER-TOKEN".
2975 This bypasses the check for tentative passing, and potentially
2976 adds material needed by cp_parser_required_error.
2978 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2979 suggesting insertion of the missing token.
2981 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2982 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2983 location. */
2985 static void
2986 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2987 required_token missing_token_desc,
2988 location_t matching_location)
2990 cp_token *token = cp_lexer_peek_token (parser->lexer);
2991 /* This diagnostic makes more sense if it is tagged to the line
2992 of the token we just peeked at. */
2993 cp_lexer_set_source_position_from_token (token);
2995 if (token->type == CPP_PRAGMA)
2997 error_at (token->location,
2998 "%<#pragma%> is not allowed here");
2999 cp_parser_skip_to_pragma_eol (parser, token);
3000 return;
3003 /* If this is actually a conflict marker, report it as such. */
3004 if (token->type == CPP_LSHIFT
3005 || token->type == CPP_RSHIFT
3006 || token->type == CPP_EQ_EQ)
3008 location_t loc;
3009 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3011 error_at (loc, "version control conflict marker in file");
3012 expanded_location token_exploc = expand_location (token->location);
3013 /* Consume tokens until the end of the source line. */
3014 for (;;)
3016 cp_lexer_consume_token (parser->lexer);
3017 cp_token *next = cp_lexer_peek_token (parser->lexer);
3018 if (next->type == CPP_EOF)
3019 break;
3020 if (next->location == UNKNOWN_LOCATION
3021 || loc == UNKNOWN_LOCATION)
3022 break;
3024 expanded_location next_exploc = expand_location (next->location);
3025 if (next_exploc.file != token_exploc.file)
3026 break;
3027 if (next_exploc.line != token_exploc.line)
3028 break;
3030 return;
3034 auto_diagnostic_group d;
3035 gcc_rich_location richloc (input_location);
3037 bool added_matching_location = false;
3039 if (missing_token_desc != RT_NONE)
3040 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3042 /* Potentially supply a fix-it hint, suggesting to add the
3043 missing token immediately after the *previous* token.
3044 This may move the primary location within richloc. */
3045 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3046 location_t prev_token_loc = prev_token->location;
3047 maybe_suggest_missing_token_insertion (&richloc, ttype,
3048 prev_token_loc);
3050 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3051 Attempt to consolidate diagnostics by printing it as a
3052 secondary range within the main diagnostic. */
3053 if (matching_location != UNKNOWN_LOCATION)
3054 added_matching_location
3055 = richloc.add_location_if_nearby (matching_location);
3058 /* If we were parsing a string-literal and there is an unknown name
3059 token right after, then check to see if that could also have been
3060 a literal string by checking the name against a list of known
3061 standard string literal constants defined in header files. If
3062 there is one, then add that as an hint to the error message. */
3063 name_hint h;
3064 if (token->type == CPP_NAME)
3065 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3066 if (cp_parser_is_string_literal (prev_token))
3068 tree name = token->u.value;
3069 const char *token_name = IDENTIFIER_POINTER (name);
3070 const char *header_hint
3071 = get_cp_stdlib_header_for_string_macro_name (token_name);
3072 if (header_hint != NULL)
3073 h = name_hint (NULL, new suggest_missing_header (token->location,
3074 token_name,
3075 header_hint));
3078 /* Actually emit the error. */
3079 c_parse_error (gmsgid,
3080 /* Because c_parser_error does not understand
3081 CPP_KEYWORD, keywords are treated like
3082 identifiers. */
3083 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3084 token->u.value, token->flags, &richloc);
3086 if (missing_token_desc != RT_NONE)
3088 /* If we weren't able to consolidate matching_location, then
3089 print it as a secondary diagnostic. */
3090 if (matching_location != UNKNOWN_LOCATION
3091 && !added_matching_location)
3092 inform (matching_location, "to match this %qs",
3093 get_matching_symbol (missing_token_desc));
3097 /* If not parsing tentatively, issue a diagnostic of the form
3098 FILE:LINE: MESSAGE before TOKEN
3099 where TOKEN is the next token in the input stream. MESSAGE
3100 (specified by the caller) is usually of the form "expected
3101 OTHER-TOKEN". */
3103 static void
3104 cp_parser_error (cp_parser* parser, const char* gmsgid)
3106 if (!cp_parser_simulate_error (parser))
3107 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3110 /* Issue an error about name-lookup failing. NAME is the
3111 IDENTIFIER_NODE DECL is the result of
3112 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3113 the thing that we hoped to find. */
3115 static void
3116 cp_parser_name_lookup_error (cp_parser* parser,
3117 tree name,
3118 tree decl,
3119 name_lookup_error desired,
3120 location_t location)
3122 /* If name lookup completely failed, tell the user that NAME was not
3123 declared. */
3124 if (decl == error_mark_node)
3126 if (parser->scope && parser->scope != global_namespace)
3127 error_at (location, "%<%E::%E%> has not been declared",
3128 parser->scope, name);
3129 else if (parser->scope == global_namespace)
3130 error_at (location, "%<::%E%> has not been declared", name);
3131 else if (parser->object_scope
3132 && !CLASS_TYPE_P (parser->object_scope))
3133 error_at (location, "request for member %qE in non-class type %qT",
3134 name, parser->object_scope);
3135 else if (parser->object_scope)
3136 error_at (location, "%<%T::%E%> has not been declared",
3137 parser->object_scope, name);
3138 else
3139 error_at (location, "%qE has not been declared", name);
3141 else if (parser->scope && parser->scope != global_namespace)
3143 switch (desired)
3145 case NLE_TYPE:
3146 error_at (location, "%<%E::%E%> is not a type",
3147 parser->scope, name);
3148 break;
3149 case NLE_CXX98:
3150 error_at (location, "%<%E::%E%> is not a class or namespace",
3151 parser->scope, name);
3152 break;
3153 case NLE_NOT_CXX98:
3154 error_at (location,
3155 "%<%E::%E%> is not a class, namespace, or enumeration",
3156 parser->scope, name);
3157 break;
3158 default:
3159 gcc_unreachable ();
3163 else if (parser->scope == global_namespace)
3165 switch (desired)
3167 case NLE_TYPE:
3168 error_at (location, "%<::%E%> is not a type", name);
3169 break;
3170 case NLE_CXX98:
3171 error_at (location, "%<::%E%> is not a class or namespace", name);
3172 break;
3173 case NLE_NOT_CXX98:
3174 error_at (location,
3175 "%<::%E%> is not a class, namespace, or enumeration",
3176 name);
3177 break;
3178 default:
3179 gcc_unreachable ();
3182 else
3184 switch (desired)
3186 case NLE_TYPE:
3187 error_at (location, "%qE is not a type", name);
3188 break;
3189 case NLE_CXX98:
3190 error_at (location, "%qE is not a class or namespace", name);
3191 break;
3192 case NLE_NOT_CXX98:
3193 error_at (location,
3194 "%qE is not a class, namespace, or enumeration", name);
3195 break;
3196 default:
3197 gcc_unreachable ();
3202 /* If we are parsing tentatively, remember that an error has occurred
3203 during this tentative parse. Returns true if the error was
3204 simulated; false if a message should be issued by the caller. */
3206 static bool
3207 cp_parser_simulate_error (cp_parser* parser)
3209 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3211 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3212 return true;
3214 return false;
3217 /* This function is called when a type is defined. If type
3218 definitions are forbidden at this point, an error message is
3219 issued. */
3221 static bool
3222 cp_parser_check_type_definition (cp_parser* parser)
3224 /* If types are forbidden here, issue a message. */
3225 if (parser->type_definition_forbidden_message)
3227 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3228 or %qs in the message need to be interpreted. */
3229 error (parser->type_definition_forbidden_message,
3230 parser->type_definition_forbidden_message_arg);
3231 return false;
3233 return true;
3236 /* This function is called when the DECLARATOR is processed. The TYPE
3237 was a type defined in the decl-specifiers. If it is invalid to
3238 define a type in the decl-specifiers for DECLARATOR, an error is
3239 issued. TYPE_LOCATION is the location of TYPE and is used
3240 for error reporting. */
3242 static void
3243 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3244 tree type, location_t type_location)
3246 /* [dcl.fct] forbids type definitions in return types.
3247 Unfortunately, it's not easy to know whether or not we are
3248 processing a return type until after the fact. */
3249 while (declarator
3250 && (declarator->kind == cdk_pointer
3251 || declarator->kind == cdk_reference
3252 || declarator->kind == cdk_ptrmem))
3253 declarator = declarator->declarator;
3254 if (declarator
3255 && declarator->kind == cdk_function)
3257 error_at (type_location,
3258 "new types may not be defined in a return type");
3259 inform (type_location,
3260 "(perhaps a semicolon is missing after the definition of %qT)",
3261 type);
3265 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3266 "<" in any valid C++ program. If the next token is indeed "<",
3267 issue a message warning the user about what appears to be an
3268 invalid attempt to form a template-id. LOCATION is the location
3269 of the type-specifier (TYPE) */
3271 static void
3272 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3273 tree type,
3274 enum tag_types tag_type,
3275 location_t location)
3277 cp_token_position start = 0;
3279 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3281 if (TREE_CODE (type) == TYPE_DECL)
3282 type = TREE_TYPE (type);
3283 if (TYPE_P (type) && !template_placeholder_p (type))
3284 error_at (location, "%qT is not a template", type);
3285 else if (identifier_p (type))
3287 if (tag_type != none_type)
3288 error_at (location, "%qE is not a class template", type);
3289 else
3290 error_at (location, "%qE is not a template", type);
3292 else
3293 error_at (location, "invalid template-id");
3294 /* Remember the location of the invalid "<". */
3295 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3296 start = cp_lexer_token_position (parser->lexer, true);
3297 /* Consume the "<". */
3298 cp_lexer_consume_token (parser->lexer);
3299 /* Parse the template arguments. */
3300 cp_parser_enclosed_template_argument_list (parser);
3301 /* Permanently remove the invalid template arguments so that
3302 this error message is not issued again. */
3303 if (start)
3304 cp_lexer_purge_tokens_after (parser->lexer, start);
3308 /* If parsing an integral constant-expression, issue an error message
3309 about the fact that THING appeared and return true. Otherwise,
3310 return false. In either case, set
3311 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3313 static bool
3314 cp_parser_non_integral_constant_expression (cp_parser *parser,
3315 non_integral_constant thing)
3317 parser->non_integral_constant_expression_p = true;
3318 if (parser->integral_constant_expression_p)
3320 if (!parser->allow_non_integral_constant_expression_p)
3322 const char *msg = NULL;
3323 switch (thing)
3325 case NIC_FLOAT:
3326 pedwarn (input_location, OPT_Wpedantic,
3327 "ISO C++ forbids using a floating-point literal "
3328 "in a constant-expression");
3329 return true;
3330 case NIC_CAST:
3331 error ("a cast to a type other than an integral or "
3332 "enumeration type cannot appear in a "
3333 "constant-expression");
3334 return true;
3335 case NIC_TYPEID:
3336 error ("%<typeid%> operator "
3337 "cannot appear in a constant-expression");
3338 return true;
3339 case NIC_NCC:
3340 error ("non-constant compound literals "
3341 "cannot appear in a constant-expression");
3342 return true;
3343 case NIC_FUNC_CALL:
3344 error ("a function call "
3345 "cannot appear in a constant-expression");
3346 return true;
3347 case NIC_INC:
3348 error ("an increment "
3349 "cannot appear in a constant-expression");
3350 return true;
3351 case NIC_DEC:
3352 error ("an decrement "
3353 "cannot appear in a constant-expression");
3354 return true;
3355 case NIC_ARRAY_REF:
3356 error ("an array reference "
3357 "cannot appear in a constant-expression");
3358 return true;
3359 case NIC_ADDR_LABEL:
3360 error ("the address of a label "
3361 "cannot appear in a constant-expression");
3362 return true;
3363 case NIC_OVERLOADED:
3364 error ("calls to overloaded operators "
3365 "cannot appear in a constant-expression");
3366 return true;
3367 case NIC_ASSIGNMENT:
3368 error ("an assignment cannot appear in a constant-expression");
3369 return true;
3370 case NIC_COMMA:
3371 error ("a comma operator "
3372 "cannot appear in a constant-expression");
3373 return true;
3374 case NIC_CONSTRUCTOR:
3375 error ("a call to a constructor "
3376 "cannot appear in a constant-expression");
3377 return true;
3378 case NIC_TRANSACTION:
3379 error ("a transaction expression "
3380 "cannot appear in a constant-expression");
3381 return true;
3382 case NIC_THIS:
3383 msg = "this";
3384 break;
3385 case NIC_FUNC_NAME:
3386 msg = "__FUNCTION__";
3387 break;
3388 case NIC_PRETTY_FUNC:
3389 msg = "__PRETTY_FUNCTION__";
3390 break;
3391 case NIC_C99_FUNC:
3392 msg = "__func__";
3393 break;
3394 case NIC_VA_ARG:
3395 msg = "va_arg";
3396 break;
3397 case NIC_ARROW:
3398 msg = "->";
3399 break;
3400 case NIC_POINT:
3401 msg = ".";
3402 break;
3403 case NIC_STAR:
3404 msg = "*";
3405 break;
3406 case NIC_ADDR:
3407 msg = "&";
3408 break;
3409 case NIC_PREINCREMENT:
3410 msg = "++";
3411 break;
3412 case NIC_PREDECREMENT:
3413 msg = "--";
3414 break;
3415 case NIC_NEW:
3416 msg = "new";
3417 break;
3418 case NIC_DEL:
3419 msg = "delete";
3420 break;
3421 default:
3422 gcc_unreachable ();
3424 if (msg)
3425 error ("%qs cannot appear in a constant-expression", msg);
3426 return true;
3429 return false;
3432 /* Emit a diagnostic for an invalid type name. This function commits
3433 to the current active tentative parse, if any. (Otherwise, the
3434 problematic construct might be encountered again later, resulting
3435 in duplicate error messages.) LOCATION is the location of ID. */
3437 static void
3438 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3439 location_t location)
3441 tree decl, ambiguous_decls;
3442 cp_parser_commit_to_tentative_parse (parser);
3443 /* Try to lookup the identifier. */
3444 decl = cp_parser_lookup_name (parser, id, none_type,
3445 /*is_template=*/false,
3446 /*is_namespace=*/false,
3447 /*check_dependency=*/true,
3448 &ambiguous_decls, location);
3449 if (ambiguous_decls)
3450 /* If the lookup was ambiguous, an error will already have
3451 been issued. */
3452 return;
3453 /* If the lookup found a template-name, it means that the user forgot
3454 to specify an argument list. Emit a useful error message. */
3455 if (DECL_TYPE_TEMPLATE_P (decl))
3457 auto_diagnostic_group d;
3458 error_at (location,
3459 "invalid use of template-name %qE without an argument list",
3460 decl);
3461 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3462 inform (location, "class template argument deduction is only available "
3463 "with %<-std=c++17%> or %<-std=gnu++17%>");
3464 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3466 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3467 error_at (location, "invalid use of destructor %qD as a type", id);
3468 else if (TREE_CODE (decl) == TYPE_DECL)
3469 /* Something like 'unsigned A a;' */
3470 error_at (location, "invalid combination of multiple type-specifiers");
3471 else if (!parser->scope)
3473 /* Issue an error message. */
3474 auto_diagnostic_group d;
3475 name_hint hint;
3476 if (TREE_CODE (id) == IDENTIFIER_NODE)
3477 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3478 if (const char *suggestion = hint.suggestion ())
3480 gcc_rich_location richloc (location);
3481 richloc.add_fixit_replace (suggestion);
3482 error_at (&richloc,
3483 "%qE does not name a type; did you mean %qs?",
3484 id, suggestion);
3486 else
3487 error_at (location, "%qE does not name a type", id);
3488 /* If we're in a template class, it's possible that the user was
3489 referring to a type from a base class. For example:
3491 template <typename T> struct A { typedef T X; };
3492 template <typename T> struct B : public A<T> { X x; };
3494 The user should have said "typename A<T>::X". */
3495 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3496 inform (location, "C++11 %<constexpr%> only available with "
3497 "%<-std=c++11%> or %<-std=gnu++11%>");
3498 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3499 inform (location, "C++11 %<noexcept%> only available with "
3500 "%<-std=c++11%> or %<-std=gnu++11%>");
3501 else if (TREE_CODE (id) == IDENTIFIER_NODE
3502 && (id_equal (id, "module") || id_equal (id, "import")))
3504 if (modules_p ())
3505 inform (location, "%qE is not recognized as a module control-line",
3506 id);
3507 else if (cxx_dialect < cxx20)
3508 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3509 id);
3510 else
3511 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3512 ", which is not yet enabled with %<-std=c++20%>", id);
3514 else if (cxx_dialect < cxx11
3515 && TREE_CODE (id) == IDENTIFIER_NODE
3516 && id_equal (id, "thread_local"))
3517 inform (location, "C++11 %<thread_local%> only available with "
3518 "%<-std=c++11%> or %<-std=gnu++11%>");
3519 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3520 inform (location, "C++20 %<constinit%> only available with "
3521 "%<-std=c++20%> or %<-std=gnu++20%>");
3522 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3523 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3524 "%<-fconcepts%>");
3525 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3526 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3527 "%<-fconcepts%>");
3528 else if (processing_template_decl && current_class_type
3529 && TYPE_BINFO (current_class_type))
3531 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3532 b; b = TREE_CHAIN (b))
3534 tree base_type = BINFO_TYPE (b);
3535 if (CLASS_TYPE_P (base_type)
3536 && dependent_type_p (base_type))
3538 /* Go from a particular instantiation of the
3539 template (which will have an empty TYPE_FIELDs),
3540 to the main version. */
3541 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3542 for (tree field = TYPE_FIELDS (base_type);
3543 field; field = DECL_CHAIN (field))
3544 if (TREE_CODE (field) == TYPE_DECL
3545 && DECL_NAME (field) == id)
3547 inform (location,
3548 "(perhaps %<typename %T::%E%> was intended)",
3549 BINFO_TYPE (b), id);
3550 goto found;
3554 found:;
3557 /* Here we diagnose qualified-ids where the scope is actually correct,
3558 but the identifier does not resolve to a valid type name. */
3559 else if (parser->scope != error_mark_node)
3561 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3563 auto_diagnostic_group d;
3564 name_hint hint;
3565 if (decl == error_mark_node)
3566 hint = suggest_alternative_in_explicit_scope (location, id,
3567 parser->scope);
3568 const char *suggestion = hint.suggestion ();
3569 gcc_rich_location richloc (location_of (id));
3570 if (suggestion)
3571 richloc.add_fixit_replace (suggestion);
3572 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3574 if (suggestion)
3575 error_at (&richloc,
3576 "%qE in namespace %qE does not name a template"
3577 " type; did you mean %qs?",
3578 id, parser->scope, suggestion);
3579 else
3580 error_at (&richloc,
3581 "%qE in namespace %qE does not name a template type",
3582 id, parser->scope);
3584 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3586 if (suggestion)
3587 error_at (&richloc,
3588 "%qE in namespace %qE does not name a template"
3589 " type; did you mean %qs?",
3590 TREE_OPERAND (id, 0), parser->scope, suggestion);
3591 else
3592 error_at (&richloc,
3593 "%qE in namespace %qE does not name a template"
3594 " type",
3595 TREE_OPERAND (id, 0), parser->scope);
3597 else
3599 if (suggestion)
3600 error_at (&richloc,
3601 "%qE in namespace %qE does not name a type"
3602 "; did you mean %qs?",
3603 id, parser->scope, suggestion);
3604 else
3605 error_at (&richloc,
3606 "%qE in namespace %qE does not name a type",
3607 id, parser->scope);
3609 if (DECL_P (decl))
3610 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3612 else if (CLASS_TYPE_P (parser->scope)
3613 && constructor_name_p (id, parser->scope))
3615 /* A<T>::A<T>() */
3616 auto_diagnostic_group d;
3617 error_at (location, "%<%T::%E%> names the constructor, not"
3618 " the type", parser->scope, id);
3619 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3620 error_at (location, "and %qT has no template constructors",
3621 parser->scope);
3623 else if (TYPE_P (parser->scope)
3624 && dependent_scope_p (parser->scope))
3626 gcc_rich_location richloc (location);
3627 richloc.add_fixit_insert_before ("typename ");
3628 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3629 error_at (&richloc,
3630 "need %<typename%> before %<%T::%D::%E%> because "
3631 "%<%T::%D%> is a dependent scope",
3632 TYPE_CONTEXT (parser->scope),
3633 TYPENAME_TYPE_FULLNAME (parser->scope),
3635 TYPE_CONTEXT (parser->scope),
3636 TYPENAME_TYPE_FULLNAME (parser->scope));
3637 else
3638 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3639 "%qT is a dependent scope",
3640 parser->scope, id, parser->scope);
3642 else if (TYPE_P (parser->scope))
3644 auto_diagnostic_group d;
3645 if (!COMPLETE_TYPE_P (parser->scope))
3646 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3647 parser->scope);
3648 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3649 error_at (location_of (id),
3650 "%qE in %q#T does not name a template type",
3651 id, parser->scope);
3652 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3653 error_at (location_of (id),
3654 "%qE in %q#T does not name a template type",
3655 TREE_OPERAND (id, 0), parser->scope);
3656 else
3657 error_at (location_of (id),
3658 "%qE in %q#T does not name a type",
3659 id, parser->scope);
3660 if (DECL_P (decl))
3661 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3663 else
3664 gcc_unreachable ();
3668 /* Check for a common situation where a type-name should be present,
3669 but is not, and issue a sensible error message. Returns true if an
3670 invalid type-name was detected.
3672 The situation handled by this function are variable declarations of the
3673 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3674 Usually, `ID' should name a type, but if we got here it means that it
3675 does not. We try to emit the best possible error message depending on
3676 how exactly the id-expression looks like. */
3678 static bool
3679 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3681 tree id;
3682 cp_token *token = cp_lexer_peek_token (parser->lexer);
3684 /* Avoid duplicate error about ambiguous lookup. */
3685 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3687 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3688 if (next->type == CPP_NAME && next->error_reported)
3689 goto out;
3692 cp_parser_parse_tentatively (parser);
3693 id = cp_parser_id_expression (parser,
3694 /*template_keyword_p=*/false,
3695 /*check_dependency_p=*/true,
3696 /*template_p=*/NULL,
3697 /*declarator_p=*/true,
3698 /*optional_p=*/false);
3699 /* If the next token is a (, this is a function with no explicit return
3700 type, i.e. constructor, destructor or conversion op. */
3701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3702 || TREE_CODE (id) == TYPE_DECL)
3704 cp_parser_abort_tentative_parse (parser);
3705 return false;
3707 if (!cp_parser_parse_definitely (parser))
3708 return false;
3710 /* Emit a diagnostic for the invalid type. */
3711 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3712 out:
3713 /* If we aren't in the middle of a declarator (i.e. in a
3714 parameter-declaration-clause), skip to the end of the declaration;
3715 there's no point in trying to process it. */
3716 if (!parser->in_declarator_p)
3717 cp_parser_skip_to_end_of_block_or_statement (parser);
3718 return true;
3721 /* Consume tokens up to, and including, the next non-nested closing `)'.
3722 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3723 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3724 found an unnested token of that type. */
3726 static int
3727 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3728 bool recovering,
3729 cpp_ttype or_ttype,
3730 bool consume_paren)
3732 unsigned paren_depth = 0;
3733 unsigned brace_depth = 0;
3734 unsigned square_depth = 0;
3735 unsigned condop_depth = 0;
3737 if (recovering && or_ttype == CPP_EOF
3738 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3739 return 0;
3741 while (true)
3743 cp_token * token = cp_lexer_peek_token (parser->lexer);
3745 /* Have we found what we're looking for before the closing paren? */
3746 if (token->type == or_ttype && or_ttype != CPP_EOF
3747 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3748 return -1;
3750 switch (token->type)
3752 case CPP_PRAGMA_EOL:
3753 if (!parser->lexer->in_pragma)
3754 break;
3755 /* FALLTHRU */
3756 case CPP_EOF:
3757 /* If we've run out of tokens, then there is no closing `)'. */
3758 return 0;
3760 /* This is good for lambda expression capture-lists. */
3761 case CPP_OPEN_SQUARE:
3762 ++square_depth;
3763 break;
3764 case CPP_CLOSE_SQUARE:
3765 if (!square_depth--)
3766 return 0;
3767 break;
3769 case CPP_SEMICOLON:
3770 /* This matches the processing in skip_to_end_of_statement. */
3771 if (!brace_depth)
3772 return 0;
3773 break;
3775 case CPP_OPEN_BRACE:
3776 ++brace_depth;
3777 break;
3778 case CPP_CLOSE_BRACE:
3779 if (!brace_depth--)
3780 return 0;
3781 break;
3783 case CPP_OPEN_PAREN:
3784 if (!brace_depth)
3785 ++paren_depth;
3786 break;
3788 case CPP_CLOSE_PAREN:
3789 if (!brace_depth && !paren_depth--)
3791 if (consume_paren)
3792 cp_lexer_consume_token (parser->lexer);
3793 return 1;
3795 break;
3797 case CPP_QUERY:
3798 if (!brace_depth && !paren_depth && !square_depth)
3799 ++condop_depth;
3800 break;
3802 case CPP_COLON:
3803 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3804 condop_depth--;
3805 break;
3807 case CPP_KEYWORD:
3808 if (token->keyword != RID__EXPORT
3809 && token->keyword != RID__MODULE
3810 && token->keyword != RID__IMPORT)
3811 break;
3812 /* FALLTHROUGH */
3814 case CPP_PRAGMA:
3815 /* We fell into a pragma. Skip it, and continue. */
3816 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3817 continue;
3819 default:
3820 break;
3823 /* Consume the token. */
3824 cp_lexer_consume_token (parser->lexer);
3828 /* Consume tokens up to, and including, the next non-nested closing `)'.
3829 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3830 are doing error recovery. Returns -1 if OR_COMMA is true and we
3831 found an unnested token of that type. */
3833 static int
3834 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3835 bool recovering,
3836 bool or_comma,
3837 bool consume_paren)
3839 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3840 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3841 ttype, consume_paren);
3844 /* Consume tokens until we reach the end of the current statement.
3845 Normally, that will be just before consuming a `;'. However, if a
3846 non-nested `}' comes first, then we stop before consuming that. */
3848 static void
3849 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3851 unsigned nesting_depth = 0;
3853 /* Unwind generic function template scope if necessary. */
3854 if (parser->fully_implicit_function_template_p)
3855 abort_fully_implicit_template (parser);
3857 while (true)
3859 cp_token *token = cp_lexer_peek_token (parser->lexer);
3861 switch (token->type)
3863 case CPP_PRAGMA_EOL:
3864 if (!parser->lexer->in_pragma)
3865 break;
3866 /* FALLTHRU */
3867 case CPP_EOF:
3868 /* If we've run out of tokens, stop. */
3869 return;
3871 case CPP_SEMICOLON:
3872 /* If the next token is a `;', we have reached the end of the
3873 statement. */
3874 if (!nesting_depth)
3875 return;
3876 break;
3878 case CPP_CLOSE_BRACE:
3879 /* If this is a non-nested '}', stop before consuming it.
3880 That way, when confronted with something like:
3882 { 3 + }
3884 we stop before consuming the closing '}', even though we
3885 have not yet reached a `;'. */
3886 if (nesting_depth == 0)
3887 return;
3889 /* If it is the closing '}' for a block that we have
3890 scanned, stop -- but only after consuming the token.
3891 That way given:
3893 void f g () { ... }
3894 typedef int I;
3896 we will stop after the body of the erroneously declared
3897 function, but before consuming the following `typedef'
3898 declaration. */
3899 if (--nesting_depth == 0)
3901 cp_lexer_consume_token (parser->lexer);
3902 return;
3904 break;
3906 case CPP_OPEN_BRACE:
3907 ++nesting_depth;
3908 break;
3910 case CPP_KEYWORD:
3911 if (token->keyword != RID__EXPORT
3912 && token->keyword != RID__MODULE
3913 && token->keyword != RID__IMPORT)
3914 break;
3915 /* FALLTHROUGH */
3917 case CPP_PRAGMA:
3918 /* We fell into a pragma. Skip it, and continue or return. */
3919 cp_parser_skip_to_pragma_eol (parser, token);
3920 if (!nesting_depth)
3921 return;
3922 continue;
3924 default:
3925 break;
3928 /* Consume the token. */
3929 cp_lexer_consume_token (parser->lexer);
3933 /* This function is called at the end of a statement or declaration.
3934 If the next token is a semicolon, it is consumed; otherwise, error
3935 recovery is attempted. */
3937 static void
3938 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3940 /* Look for the trailing `;'. */
3941 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3943 /* If there is additional (erroneous) input, skip to the end of
3944 the statement. */
3945 cp_parser_skip_to_end_of_statement (parser);
3946 /* If the next token is now a `;', consume it. */
3947 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3948 cp_lexer_consume_token (parser->lexer);
3952 /* Skip tokens until we have consumed an entire block, or until we
3953 have consumed a non-nested `;'. */
3955 static void
3956 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3958 int nesting_depth = 0;
3960 /* Unwind generic function template scope if necessary. */
3961 if (parser->fully_implicit_function_template_p)
3962 abort_fully_implicit_template (parser);
3964 while (nesting_depth >= 0)
3966 cp_token *token = cp_lexer_peek_token (parser->lexer);
3968 switch (token->type)
3970 case CPP_PRAGMA_EOL:
3971 if (!parser->lexer->in_pragma)
3972 break;
3973 /* FALLTHRU */
3974 case CPP_EOF:
3975 /* If we've run out of tokens, stop. */
3976 return;
3978 case CPP_SEMICOLON:
3979 /* Stop if this is an unnested ';'. */
3980 if (!nesting_depth)
3981 nesting_depth = -1;
3982 break;
3984 case CPP_CLOSE_BRACE:
3985 /* Stop if this is an unnested '}', or closes the outermost
3986 nesting level. */
3987 nesting_depth--;
3988 if (nesting_depth < 0)
3989 return;
3990 if (!nesting_depth)
3991 nesting_depth = -1;
3992 break;
3994 case CPP_OPEN_BRACE:
3995 /* Nest. */
3996 nesting_depth++;
3997 break;
3999 case CPP_KEYWORD:
4000 if (token->keyword != RID__EXPORT
4001 && token->keyword != RID__MODULE
4002 && token->keyword != RID__IMPORT)
4003 break;
4004 /* FALLTHROUGH */
4006 case CPP_PRAGMA:
4007 /* Skip it, and continue or return. */
4008 cp_parser_skip_to_pragma_eol (parser, token);
4009 if (!nesting_depth)
4010 return;
4011 continue;
4013 default:
4014 break;
4017 /* Consume the token. */
4018 cp_lexer_consume_token (parser->lexer);
4022 /* Skip tokens until a non-nested closing curly brace is the next
4023 token, or there are no more tokens. Return true in the first case,
4024 false otherwise. */
4026 static bool
4027 cp_parser_skip_to_closing_brace (cp_parser *parser)
4029 unsigned nesting_depth = 0;
4031 while (true)
4033 cp_token *token = cp_lexer_peek_token (parser->lexer);
4035 switch (token->type)
4037 case CPP_PRAGMA_EOL:
4038 if (!parser->lexer->in_pragma)
4039 break;
4040 /* FALLTHRU */
4041 case CPP_EOF:
4042 /* If we've run out of tokens, stop. */
4043 return false;
4045 case CPP_CLOSE_BRACE:
4046 /* If the next token is a non-nested `}', then we have reached
4047 the end of the current block. */
4048 if (nesting_depth-- == 0)
4049 return true;
4050 break;
4052 case CPP_OPEN_BRACE:
4053 /* If it the next token is a `{', then we are entering a new
4054 block. Consume the entire block. */
4055 ++nesting_depth;
4056 break;
4058 default:
4059 break;
4062 /* Consume the token. */
4063 cp_lexer_consume_token (parser->lexer);
4067 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4068 parameter is the PRAGMA token, allowing us to purge the entire pragma
4069 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4070 forwards (not error recovery). */
4072 static void
4073 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4075 cp_token *token;
4079 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4080 before an EOF token, even when the EOF is on the pragma line.
4081 We should never get here without being inside a deferred
4082 pragma. */
4083 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4084 token = cp_lexer_consume_token (parser->lexer);
4086 while (token->type != CPP_PRAGMA_EOL);
4088 if (pragma_tok)
4090 /* Ensure that the pragma is not parsed again. */
4091 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
4092 parser->lexer->in_pragma = false;
4093 if (parser->lexer->in_omp_attribute_pragma
4094 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4096 parser->lexer = parser->lexer->next;
4097 /* Put the current source position back where it was before this
4098 lexer was pushed. */
4099 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4104 /* Require pragma end of line, resyncing with it as necessary. The
4105 arguments are as for cp_parser_skip_to_pragma_eol. */
4107 static void
4108 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4110 parser->lexer->in_pragma = false;
4111 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4113 else if (parser->lexer->in_omp_attribute_pragma
4114 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4116 parser->lexer = parser->lexer->next;
4117 /* Put the current source position back where it was before this
4118 lexer was pushed. */
4119 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4123 /* This is a simple wrapper around make_typename_type. When the id is
4124 an unresolved identifier node, we can provide a superior diagnostic
4125 using cp_parser_diagnose_invalid_type_name. */
4127 static tree
4128 cp_parser_make_typename_type (cp_parser *parser, tree id,
4129 location_t id_location)
4131 tree result;
4132 if (identifier_p (id))
4134 result = make_typename_type (parser->scope, id, typename_type,
4135 /*complain=*/tf_none);
4136 if (result == error_mark_node)
4137 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4138 return result;
4140 return make_typename_type (parser->scope, id, typename_type, tf_error);
4143 /* This is a wrapper around the
4144 make_{pointer,ptrmem,reference}_declarator functions that decides
4145 which one to call based on the CODE and CLASS_TYPE arguments. The
4146 CODE argument should be one of the values returned by
4147 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4148 appertain to the pointer or reference. */
4150 static cp_declarator *
4151 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4152 cp_cv_quals cv_qualifiers,
4153 cp_declarator *target,
4154 tree attributes)
4156 if (code == ERROR_MARK || target == cp_error_declarator)
4157 return cp_error_declarator;
4159 if (code == INDIRECT_REF)
4160 if (class_type == NULL_TREE)
4161 return make_pointer_declarator (cv_qualifiers, target, attributes);
4162 else
4163 return make_ptrmem_declarator (cv_qualifiers, class_type,
4164 target, attributes);
4165 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4166 return make_reference_declarator (cv_qualifiers, target,
4167 false, attributes);
4168 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4169 return make_reference_declarator (cv_qualifiers, target,
4170 true, attributes);
4171 gcc_unreachable ();
4174 /* Create a new C++ parser. */
4176 static cp_parser *
4177 cp_parser_new (cp_lexer *lexer)
4179 /* Initialize the binops_by_token so that we can get the tree
4180 directly from the token. */
4181 for (unsigned i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
4182 binops_by_token[binops[i].token_type] = binops[i];
4184 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4185 parser->lexer = lexer;
4186 parser->context = cp_parser_context_new (NULL);
4188 /* For now, we always accept GNU extensions. */
4189 parser->allow_gnu_extensions_p = 1;
4191 /* The `>' token is a greater-than operator, not the end of a
4192 template-id. */
4193 parser->greater_than_is_operator_p = true;
4195 parser->default_arg_ok_p = true;
4197 /* We are not parsing a constant-expression. */
4198 parser->integral_constant_expression_p = false;
4199 parser->allow_non_integral_constant_expression_p = false;
4200 parser->non_integral_constant_expression_p = false;
4202 /* Local variable names are not forbidden. */
4203 parser->local_variables_forbidden_p = 0;
4205 /* We are not processing an `extern "C"' declaration. */
4206 parser->in_unbraced_linkage_specification_p = false;
4208 /* We are not processing a declarator. */
4209 parser->in_declarator_p = false;
4211 /* We are not processing a template-argument-list. */
4212 parser->in_template_argument_list_p = false;
4214 /* We are not in an iteration statement. */
4215 parser->in_statement = 0;
4217 /* We are not in a switch statement. */
4218 parser->in_switch_statement_p = false;
4220 /* We are not parsing a type-id inside an expression. */
4221 parser->in_type_id_in_expr_p = false;
4223 /* String literals should be translated to the execution character set. */
4224 parser->translate_strings_p = true;
4226 /* We are not parsing a function body. */
4227 parser->in_function_body = false;
4229 /* We can correct until told otherwise. */
4230 parser->colon_corrects_to_scope_p = true;
4232 /* The unparsed function queue is empty. */
4233 push_unparsed_function_queues (parser);
4235 /* There are no classes being defined. */
4236 parser->num_classes_being_defined = 0;
4238 /* No template parameters apply. */
4239 parser->num_template_parameter_lists = 0;
4241 /* Special parsing data structures. */
4242 parser->omp_declare_simd = NULL;
4243 parser->oacc_routine = NULL;
4245 /* Not declaring an implicit function template. */
4246 parser->auto_is_implicit_function_template_parm_p = false;
4247 parser->fully_implicit_function_template_p = false;
4248 parser->implicit_template_parms = 0;
4249 parser->implicit_template_scope = 0;
4251 /* Allow constrained-type-specifiers. */
4252 parser->prevent_constrained_type_specifiers = 0;
4254 /* We haven't yet seen an 'extern "C"'. */
4255 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4257 return parser;
4260 /* Create a cp_lexer structure which will emit the tokens in CACHE
4261 and push it onto the parser's lexer stack. This is used for delayed
4262 parsing of in-class method bodies and default arguments, and should
4263 not be confused with tentative parsing. */
4264 static void
4265 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4267 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4268 lexer->next = parser->lexer;
4269 parser->lexer = lexer;
4271 /* Move the current source position to that of the first token in the
4272 new lexer. */
4273 cp_lexer_set_source_position_from_token (lexer->next_token);
4276 /* Pop the top lexer off the parser stack. This is never used for the
4277 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4278 static void
4279 cp_parser_pop_lexer (cp_parser *parser)
4281 cp_lexer *lexer = parser->lexer;
4282 parser->lexer = lexer->next;
4283 cp_lexer_destroy (lexer);
4285 /* Put the current source position back where it was before this
4286 lexer was pushed. */
4287 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4290 /* Lexical conventions [gram.lex] */
4292 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4293 identifier. */
4295 static cp_expr
4296 cp_parser_identifier (cp_parser* parser)
4298 cp_token *token;
4300 /* Look for the identifier. */
4301 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4302 /* Return the value. */
4303 if (token)
4304 return cp_expr (token->u.value, token->location);
4305 else
4306 return error_mark_node;
4309 /* Parse a sequence of adjacent string constants. Returns a
4310 TREE_STRING representing the combined, nul-terminated string
4311 constant. If TRANSLATE is true, translate the string to the
4312 execution character set. If WIDE_OK is true, a wide string is
4313 invalid here.
4315 C++98 [lex.string] says that if a narrow string literal token is
4316 adjacent to a wide string literal token, the behavior is undefined.
4317 However, C99 6.4.5p4 says that this results in a wide string literal.
4318 We follow C99 here, for consistency with the C front end.
4320 This code is largely lifted from lex_string() in c-lex.c.
4322 FUTURE: ObjC++ will need to handle @-strings here. */
4323 static cp_expr
4324 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4325 bool lookup_udlit = true)
4327 tree value;
4328 size_t count;
4329 struct obstack str_ob;
4330 struct obstack loc_ob;
4331 cpp_string str, istr, *strs;
4332 cp_token *tok;
4333 enum cpp_ttype type, curr_type;
4334 int have_suffix_p = 0;
4335 tree string_tree;
4336 tree suffix_id = NULL_TREE;
4337 bool curr_tok_is_userdef_p = false;
4339 tok = cp_lexer_peek_token (parser->lexer);
4340 if (!cp_parser_is_string_literal (tok))
4342 cp_parser_error (parser, "expected string-literal");
4343 return error_mark_node;
4346 location_t loc = tok->location;
4348 if (cpp_userdef_string_p (tok->type))
4350 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4351 curr_type = cpp_userdef_string_remove_type (tok->type);
4352 curr_tok_is_userdef_p = true;
4354 else
4356 string_tree = tok->u.value;
4357 curr_type = tok->type;
4359 type = curr_type;
4361 /* Try to avoid the overhead of creating and destroying an obstack
4362 for the common case of just one string. */
4363 if (!cp_parser_is_string_literal
4364 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4366 cp_lexer_consume_token (parser->lexer);
4368 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4369 str.len = TREE_STRING_LENGTH (string_tree);
4370 count = 1;
4372 if (curr_tok_is_userdef_p)
4374 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4375 have_suffix_p = 1;
4376 curr_type = cpp_userdef_string_remove_type (tok->type);
4378 else
4379 curr_type = tok->type;
4381 strs = &str;
4383 else
4385 location_t last_tok_loc = tok->location;
4386 gcc_obstack_init (&str_ob);
4387 gcc_obstack_init (&loc_ob);
4388 count = 0;
4392 cp_lexer_consume_token (parser->lexer);
4393 count++;
4394 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4395 str.len = TREE_STRING_LENGTH (string_tree);
4397 if (curr_tok_is_userdef_p)
4399 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4400 if (have_suffix_p == 0)
4402 suffix_id = curr_suffix_id;
4403 have_suffix_p = 1;
4405 else if (have_suffix_p == 1
4406 && curr_suffix_id != suffix_id)
4408 error ("inconsistent user-defined literal suffixes"
4409 " %qD and %qD in string literal",
4410 suffix_id, curr_suffix_id);
4411 have_suffix_p = -1;
4413 curr_type = cpp_userdef_string_remove_type (tok->type);
4415 else
4416 curr_type = tok->type;
4418 if (type != curr_type)
4420 if (type == CPP_STRING)
4421 type = curr_type;
4422 else if (curr_type != CPP_STRING)
4424 rich_location rich_loc (line_table, tok->location);
4425 rich_loc.add_range (last_tok_loc);
4426 error_at (&rich_loc,
4427 "concatenation of string literals with "
4428 "conflicting encoding prefixes");
4432 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4433 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4435 last_tok_loc = tok->location;
4437 tok = cp_lexer_peek_token (parser->lexer);
4438 if (cpp_userdef_string_p (tok->type))
4440 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4441 curr_type = cpp_userdef_string_remove_type (tok->type);
4442 curr_tok_is_userdef_p = true;
4444 else
4446 string_tree = tok->u.value;
4447 curr_type = tok->type;
4448 curr_tok_is_userdef_p = false;
4451 while (cp_parser_is_string_literal (tok));
4453 /* A string literal built by concatenation has its caret=start at
4454 the start of the initial string, and its finish at the finish of
4455 the final string literal. */
4456 loc = make_location (loc, loc, get_finish (last_tok_loc));
4458 strs = (cpp_string *) obstack_finish (&str_ob);
4461 if (type != CPP_STRING && !wide_ok)
4463 cp_parser_error (parser, "a wide string is invalid in this context");
4464 type = CPP_STRING;
4467 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4468 (parse_in, strs, count, &istr, type))
4470 value = build_string (istr.len, (const char *)istr.text);
4471 free (CONST_CAST (unsigned char *, istr.text));
4472 if (count > 1)
4474 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4475 gcc_assert (g_string_concat_db);
4476 g_string_concat_db->record_string_concatenation (count, locs);
4479 switch (type)
4481 default:
4482 case CPP_STRING:
4483 TREE_TYPE (value) = char_array_type_node;
4484 break;
4485 case CPP_UTF8STRING:
4486 if (flag_char8_t)
4487 TREE_TYPE (value) = char8_array_type_node;
4488 else
4489 TREE_TYPE (value) = char_array_type_node;
4490 break;
4491 case CPP_STRING16:
4492 TREE_TYPE (value) = char16_array_type_node;
4493 break;
4494 case CPP_STRING32:
4495 TREE_TYPE (value) = char32_array_type_node;
4496 break;
4497 case CPP_WSTRING:
4498 TREE_TYPE (value) = wchar_array_type_node;
4499 break;
4502 value = fix_string_type (value);
4504 if (have_suffix_p)
4506 tree literal = build_userdef_literal (suffix_id, value,
4507 OT_NONE, NULL_TREE);
4508 if (lookup_udlit)
4509 value = cp_parser_userdef_string_literal (literal);
4510 else
4511 value = literal;
4514 else
4515 /* cpp_interpret_string has issued an error. */
4516 value = error_mark_node;
4518 if (count > 1)
4520 obstack_free (&str_ob, 0);
4521 obstack_free (&loc_ob, 0);
4524 return cp_expr (value, loc);
4527 /* Look up a literal operator with the name and the exact arguments. */
4529 static tree
4530 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4532 tree decl = lookup_name (name);
4533 if (!decl || !is_overloaded_fn (decl))
4534 return error_mark_node;
4536 for (lkp_iterator iter (decl); iter; ++iter)
4538 tree fn = *iter;
4540 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4542 unsigned int ix;
4543 bool found = true;
4545 for (ix = 0;
4546 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4547 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4549 tree tparm = TREE_VALUE (parmtypes);
4550 tree targ = TREE_TYPE ((*args)[ix]);
4551 bool ptr = TYPE_PTR_P (tparm);
4552 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4553 if ((ptr || arr || !same_type_p (tparm, targ))
4554 && (!ptr || !arr
4555 || !same_type_p (TREE_TYPE (tparm),
4556 TREE_TYPE (targ))))
4557 found = false;
4560 if (found
4561 && ix == vec_safe_length (args)
4562 /* May be this should be sufficient_parms_p instead,
4563 depending on how exactly should user-defined literals
4564 work in presence of default arguments on the literal
4565 operator parameters. */
4566 && parmtypes == void_list_node)
4567 return decl;
4571 return error_mark_node;
4574 /* Parse a user-defined char constant. Returns a call to a user-defined
4575 literal operator taking the character as an argument. */
4577 static cp_expr
4578 cp_parser_userdef_char_literal (cp_parser *parser)
4580 cp_token *token = cp_lexer_consume_token (parser->lexer);
4581 tree literal = token->u.value;
4582 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4583 tree value = USERDEF_LITERAL_VALUE (literal);
4584 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4585 tree decl, result;
4587 /* Build up a call to the user-defined operator */
4588 /* Lookup the name we got back from the id-expression. */
4589 releasing_vec args;
4590 vec_safe_push (args, value);
4591 decl = lookup_literal_operator (name, args);
4592 if (!decl || decl == error_mark_node)
4594 error ("unable to find character literal operator %qD with %qT argument",
4595 name, TREE_TYPE (value));
4596 return error_mark_node;
4598 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4599 return result;
4602 /* A subroutine of cp_parser_userdef_numeric_literal to
4603 create a char... template parameter pack from a string node. */
4605 static tree
4606 make_char_string_pack (tree value)
4608 tree charvec;
4609 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4610 const char *str = TREE_STRING_POINTER (value);
4611 int i, len = TREE_STRING_LENGTH (value) - 1;
4612 tree argvec = make_tree_vec (1);
4614 /* Fill in CHARVEC with all of the parameters. */
4615 charvec = make_tree_vec (len);
4616 for (i = 0; i < len; ++i)
4618 unsigned char s[3] = { '\'', str[i], '\'' };
4619 cpp_string in = { 3, s };
4620 cpp_string out = { 0, 0 };
4621 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4622 return NULL_TREE;
4623 gcc_assert (out.len == 2);
4624 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4625 out.text[0]);
4628 /* Build the argument packs. */
4629 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4631 TREE_VEC_ELT (argvec, 0) = argpack;
4633 return argvec;
4636 /* A subroutine of cp_parser_userdef_numeric_literal to
4637 create a char... template parameter pack from a string node. */
4639 static tree
4640 make_string_pack (tree value)
4642 tree charvec;
4643 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4644 const unsigned char *str
4645 = (const unsigned char *) TREE_STRING_POINTER (value);
4646 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4647 int len = TREE_STRING_LENGTH (value) / sz - 1;
4648 tree argvec = make_tree_vec (2);
4650 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4651 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4653 /* First template parm is character type. */
4654 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4656 /* Fill in CHARVEC with all of the parameters. */
4657 charvec = make_tree_vec (len);
4658 for (int i = 0; i < len; ++i)
4659 TREE_VEC_ELT (charvec, i)
4660 = double_int_to_tree (str_char_type_node,
4661 double_int::from_buffer (str + i * sz, sz));
4663 /* Build the argument packs. */
4664 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4666 TREE_VEC_ELT (argvec, 1) = argpack;
4668 return argvec;
4671 /* Parse a user-defined numeric constant. returns a call to a user-defined
4672 literal operator. */
4674 static cp_expr
4675 cp_parser_userdef_numeric_literal (cp_parser *parser)
4677 cp_token *token = cp_lexer_consume_token (parser->lexer);
4678 tree literal = token->u.value;
4679 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4680 tree value = USERDEF_LITERAL_VALUE (literal);
4681 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4682 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4683 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4684 tree decl, result;
4686 /* Look for a literal operator taking the exact type of numeric argument
4687 as the literal value. */
4688 releasing_vec args;
4689 vec_safe_push (args, value);
4690 decl = lookup_literal_operator (name, args);
4691 if (decl && decl != error_mark_node)
4693 result = finish_call_expr (decl, &args, false, true,
4694 tf_warning_or_error);
4696 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4698 warning_at (token->location, OPT_Woverflow,
4699 "integer literal exceeds range of %qT type",
4700 long_long_unsigned_type_node);
4702 else
4704 if (overflow > 0)
4705 warning_at (token->location, OPT_Woverflow,
4706 "floating literal exceeds range of %qT type",
4707 long_double_type_node);
4708 else if (overflow < 0)
4709 warning_at (token->location, OPT_Woverflow,
4710 "floating literal truncated to zero");
4713 return result;
4716 /* If the numeric argument didn't work, look for a raw literal
4717 operator taking a const char* argument consisting of the number
4718 in string format. */
4719 args->truncate (0);
4720 vec_safe_push (args, num_string);
4721 decl = lookup_literal_operator (name, args);
4722 if (decl && decl != error_mark_node)
4724 result = finish_call_expr (decl, &args, false, true,
4725 tf_warning_or_error);
4726 return result;
4729 /* If the raw literal didn't work, look for a non-type template
4730 function with parameter pack char.... Call the function with
4731 template parameter characters representing the number. */
4732 args->truncate (0);
4733 decl = lookup_literal_operator (name, args);
4734 if (decl && decl != error_mark_node)
4736 tree tmpl_args = make_char_string_pack (num_string);
4737 if (tmpl_args == NULL_TREE)
4739 error ("failed to translate literal to execution character set %qT",
4740 num_string);
4741 return error_mark_node;
4743 decl = lookup_template_function (decl, tmpl_args);
4744 result = finish_call_expr (decl, &args, false, true,
4745 tf_warning_or_error);
4746 return result;
4749 /* In C++14 the standard library defines complex number suffixes that
4750 conflict with GNU extensions. Prefer them if <complex> is #included. */
4751 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4752 bool i14 = (cxx_dialect > cxx11
4753 && (id_equal (suffix_id, "i")
4754 || id_equal (suffix_id, "if")
4755 || id_equal (suffix_id, "il")));
4756 diagnostic_t kind = DK_ERROR;
4757 int opt = 0;
4759 if (i14 && ext)
4761 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4762 LOOK_want::NORMAL, false);
4763 if (cxlit == error_mark_node)
4765 /* No <complex>, so pedwarn and use GNU semantics. */
4766 kind = DK_PEDWARN;
4767 opt = OPT_Wpedantic;
4771 bool complained
4772 = emit_diagnostic (kind, input_location, opt,
4773 "unable to find numeric literal operator %qD", name);
4775 if (!complained)
4776 /* Don't inform either. */;
4777 else if (i14)
4779 inform (token->location, "add %<using namespace std::complex_literals%> "
4780 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4781 "suffixes");
4782 if (ext)
4783 inform (token->location, "or use %<j%> instead of %<i%> for the "
4784 "GNU built-in suffix");
4786 else if (!ext)
4787 inform (token->location, "use %<-fext-numeric-literals%> "
4788 "to enable more built-in suffixes");
4790 if (kind == DK_ERROR)
4791 value = error_mark_node;
4792 else
4794 /* Use the built-in semantics. */
4795 tree type;
4796 if (id_equal (suffix_id, "i"))
4798 if (TREE_CODE (value) == INTEGER_CST)
4799 type = integer_type_node;
4800 else
4801 type = double_type_node;
4803 else if (id_equal (suffix_id, "if"))
4804 type = float_type_node;
4805 else /* if (id_equal (suffix_id, "il")) */
4806 type = long_double_type_node;
4808 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
4809 build_zero_cst (type), fold_convert (type, value));
4812 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4813 /* Avoid repeated diagnostics. */
4814 token->u.value = value;
4815 return value;
4818 /* Parse a user-defined string constant. Returns a call to a user-defined
4819 literal operator taking a character pointer and the length of the string
4820 as arguments. */
4822 static tree
4823 cp_parser_userdef_string_literal (tree literal)
4825 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4826 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4827 tree value = USERDEF_LITERAL_VALUE (literal);
4828 int len = TREE_STRING_LENGTH (value)
4829 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4830 tree decl;
4832 /* Build up a call to the user-defined operator. */
4833 /* Lookup the name we got back from the id-expression. */
4834 releasing_vec args;
4835 vec_safe_push (args, value);
4836 vec_safe_push (args, build_int_cst (size_type_node, len));
4837 decl = lookup_literal_operator (name, args);
4839 if (decl && decl != error_mark_node)
4840 return finish_call_expr (decl, &args, false, true,
4841 tf_warning_or_error);
4843 /* Look for a suitable template function, either (C++20) with a single
4844 parameter of class type, or (N3599) with typename parameter CharT and
4845 parameter pack CharT... */
4846 args->truncate (0);
4847 decl = lookup_literal_operator (name, args);
4848 if (decl && decl != error_mark_node)
4850 /* Use resolve_nondeduced_context to try to choose one form of template
4851 or the other. */
4852 tree tmpl_args = make_tree_vec (1);
4853 TREE_VEC_ELT (tmpl_args, 0) = value;
4854 decl = lookup_template_function (decl, tmpl_args);
4855 tree res = resolve_nondeduced_context (decl, tf_none);
4856 if (DECL_P (res))
4857 decl = res;
4858 else
4860 TREE_OPERAND (decl, 1) = make_string_pack (value);
4861 res = resolve_nondeduced_context (decl, tf_none);
4862 if (DECL_P (res))
4863 decl = res;
4865 if (!DECL_P (decl) && cxx_dialect > cxx17)
4866 TREE_OPERAND (decl, 1) = tmpl_args;
4867 return finish_call_expr (decl, &args, false, true,
4868 tf_warning_or_error);
4871 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4872 name, TREE_TYPE (value), size_type_node);
4873 return error_mark_node;
4877 /* Basic concepts [gram.basic] */
4879 /* Parse a translation-unit.
4881 translation-unit:
4882 declaration-seq [opt] */
4884 static void
4885 cp_parser_translation_unit (cp_parser* parser)
4887 gcc_checking_assert (!cp_error_declarator);
4889 /* Create the declarator obstack. */
4890 gcc_obstack_init (&declarator_obstack);
4891 /* Create the error declarator. */
4892 cp_error_declarator = make_declarator (cdk_error);
4893 /* Create the empty parameter list. */
4894 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4895 UNKNOWN_LOCATION);
4896 /* Remember where the base of the declarator obstack lies. */
4897 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4899 push_deferring_access_checks (flag_access_control
4900 ? dk_no_deferred : dk_no_check);
4902 module_parse mp_state = MP_NOT_MODULE;
4903 if (modules_p () && !header_module_p ())
4904 mp_state = MP_FIRST;
4906 bool implicit_extern_c = false;
4908 /* Parse until EOF. */
4909 for (;;)
4911 cp_token *token = cp_lexer_peek_token (parser->lexer);
4913 /* If we're entering or exiting a region that's implicitly
4914 extern "C", modify the lang context appropriately. This is
4915 so horrible. Please die. */
4916 if (implicit_extern_c
4917 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4919 implicit_extern_c = !implicit_extern_c;
4920 if (implicit_extern_c)
4921 push_lang_context (lang_name_c);
4922 else
4923 pop_lang_context ();
4926 if (token->type == CPP_EOF)
4927 break;
4929 if (modules_p ())
4931 /* Top-level module declarations are ok, and change the
4932 portion of file we're in. Top-level import declarations
4933 are significant for the import portions. */
4935 cp_token *next = token;
4936 bool exporting = token->keyword == RID__EXPORT;
4937 if (exporting)
4939 cp_lexer_consume_token (parser->lexer);
4940 next = cp_lexer_peek_token (parser->lexer);
4942 if (next->keyword == RID__MODULE)
4944 mp_state
4945 = cp_parser_module_declaration (parser, mp_state, exporting);
4946 continue;
4948 else if (next->keyword == RID__IMPORT)
4950 if (mp_state == MP_FIRST)
4951 mp_state = MP_NOT_MODULE;
4952 cp_parser_import_declaration (parser, mp_state, exporting);
4953 continue;
4955 else
4956 gcc_checking_assert (!exporting);
4958 if (mp_state == MP_GLOBAL && token->main_source_p)
4960 static bool warned = false;
4961 if (!warned)
4963 warned = true;
4964 error_at (token->location,
4965 "global module fragment contents must be"
4966 " from preprocessor inclusion");
4971 /* This relies on the ordering of module_parse values. */
4972 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
4973 /* We're no longer in the import portion of a named module. */
4974 mp_state = module_parse (mp_state + 1);
4975 else if (mp_state == MP_FIRST)
4976 mp_state = MP_NOT_MODULE;
4978 if (token->type == CPP_CLOSE_BRACE)
4980 cp_parser_error (parser, "expected declaration");
4981 cp_lexer_consume_token (parser->lexer);
4982 /* If the next token is now a `;', consume it. */
4983 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4984 cp_lexer_consume_token (parser->lexer);
4986 else
4987 cp_parser_toplevel_declaration (parser);
4990 /* Get rid of the token array; we don't need it any more. */
4991 cp_lexer_destroy (parser->lexer);
4992 parser->lexer = NULL;
4994 /* The EOF should have reset this. */
4995 gcc_checking_assert (!implicit_extern_c);
4997 /* Make sure the declarator obstack was fully cleaned up. */
4998 gcc_assert (obstack_next_free (&declarator_obstack)
4999 == declarator_obstack_base);
5002 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5003 decltype context. */
5005 static inline tsubst_flags_t
5006 complain_flags (bool decltype_p)
5008 tsubst_flags_t complain = tf_warning_or_error;
5009 if (decltype_p)
5010 complain |= tf_decltype;
5011 return complain;
5014 /* We're about to parse a collection of statements. If we're currently
5015 parsing tentatively, set up a firewall so that any nested
5016 cp_parser_commit_to_tentative_parse won't affect the current context. */
5018 static cp_token_position
5019 cp_parser_start_tentative_firewall (cp_parser *parser)
5021 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5022 return 0;
5024 cp_parser_parse_tentatively (parser);
5025 cp_parser_commit_to_topmost_tentative_parse (parser);
5026 return cp_lexer_token_position (parser->lexer, false);
5029 /* We've finished parsing the collection of statements. Wrap up the
5030 firewall and replace the relevant tokens with the parsed form. */
5032 static void
5033 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5034 tree expr)
5036 if (!start)
5037 return;
5039 /* Finish the firewall level. */
5040 cp_parser_parse_definitely (parser);
5041 /* And remember the result of the parse for when we try again. */
5042 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5043 token->type = CPP_PREPARSED_EXPR;
5044 token->u.value = expr;
5045 token->keyword = RID_MAX;
5046 cp_lexer_purge_tokens_after (parser->lexer, start);
5049 /* Like the above functions, but let the user modify the tokens. Used by
5050 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5051 later parses, so it makes sense to localize the effects of
5052 cp_parser_commit_to_tentative_parse. */
5054 struct tentative_firewall
5056 cp_parser *parser;
5057 bool set;
5059 tentative_firewall (cp_parser *p): parser(p)
5061 /* If we're currently parsing tentatively, start a committed level as a
5062 firewall and then an inner tentative parse. */
5063 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5065 cp_parser_parse_tentatively (parser);
5066 cp_parser_commit_to_topmost_tentative_parse (parser);
5067 cp_parser_parse_tentatively (parser);
5071 ~tentative_firewall()
5073 if (set)
5075 /* Finish the inner tentative parse and the firewall, propagating any
5076 uncommitted error state to the outer tentative parse. */
5077 bool err = cp_parser_error_occurred (parser);
5078 cp_parser_parse_definitely (parser);
5079 cp_parser_parse_definitely (parser);
5080 if (err)
5081 cp_parser_simulate_error (parser);
5086 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5087 This class is for tracking such a matching pair of symbols.
5088 In particular, it tracks the location of the first token,
5089 so that if the second token is missing, we can highlight the
5090 location of the first token when notifying the user about the
5091 problem. */
5093 template <typename traits_t>
5094 class token_pair
5096 public:
5097 /* token_pair's ctor. */
5098 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5100 /* If the next token is the opening symbol for this pair, consume it and
5101 return true.
5102 Otherwise, issue an error and return false.
5103 In either case, record the location of the opening token. */
5105 bool require_open (cp_parser *parser)
5107 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5108 return cp_parser_require (parser, traits_t::open_token_type,
5109 traits_t::required_token_open);
5112 /* Consume the next token from PARSER, recording its location as
5113 that of the opening token within the pair. */
5115 cp_token * consume_open (cp_parser *parser)
5117 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5118 gcc_assert (tok->type == traits_t::open_token_type);
5119 m_open_loc = tok->location;
5120 return tok;
5123 /* If the next token is the closing symbol for this pair, consume it
5124 and return it.
5125 Otherwise, issue an error, highlighting the location of the
5126 corresponding opening token, and return NULL. */
5128 cp_token *require_close (cp_parser *parser) const
5130 return cp_parser_require (parser, traits_t::close_token_type,
5131 traits_t::required_token_close,
5132 m_open_loc);
5135 location_t open_location () const { return m_open_loc; }
5137 private:
5138 location_t m_open_loc;
5141 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5143 struct matching_paren_traits
5145 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5146 static const enum required_token required_token_open = RT_OPEN_PAREN;
5147 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5148 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5151 /* "matching_parens" is a token_pair<T> class for tracking matching
5152 pairs of parentheses. */
5154 typedef token_pair<matching_paren_traits> matching_parens;
5156 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5158 struct matching_brace_traits
5160 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5161 static const enum required_token required_token_open = RT_OPEN_BRACE;
5162 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5163 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5166 /* "matching_braces" is a token_pair<T> class for tracking matching
5167 pairs of braces. */
5169 typedef token_pair<matching_brace_traits> matching_braces;
5172 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5173 enclosing parentheses. */
5175 static cp_expr
5176 cp_parser_statement_expr (cp_parser *parser)
5178 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5180 /* Consume the '('. */
5181 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5182 matching_parens parens;
5183 parens.consume_open (parser);
5184 /* Start the statement-expression. */
5185 tree expr = begin_stmt_expr ();
5186 /* Parse the compound-statement. */
5187 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
5188 /* Finish up. */
5189 expr = finish_stmt_expr (expr, false);
5190 /* Consume the ')'. */
5191 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5192 if (!parens.require_close (parser))
5193 cp_parser_skip_to_end_of_statement (parser);
5195 cp_parser_end_tentative_firewall (parser, start, expr);
5196 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5197 return cp_expr (expr, combined_loc);
5200 /* Expressions [gram.expr] */
5202 /* Parse a fold-operator.
5204 fold-operator:
5205 - * / % ^ & | = < > << >>
5206 = -= *= /= %= ^= &= |= <<= >>=
5207 == != <= >= && || , .* ->*
5209 This returns the tree code corresponding to the matched operator
5210 as an int. When the current token matches a compound assignment
5211 operator, the resulting tree code is the negative value of the
5212 non-assignment operator. */
5214 static int
5215 cp_parser_fold_operator (cp_token *token)
5217 switch (token->type)
5219 case CPP_PLUS: return PLUS_EXPR;
5220 case CPP_MINUS: return MINUS_EXPR;
5221 case CPP_MULT: return MULT_EXPR;
5222 case CPP_DIV: return TRUNC_DIV_EXPR;
5223 case CPP_MOD: return TRUNC_MOD_EXPR;
5224 case CPP_XOR: return BIT_XOR_EXPR;
5225 case CPP_AND: return BIT_AND_EXPR;
5226 case CPP_OR: return BIT_IOR_EXPR;
5227 case CPP_LSHIFT: return LSHIFT_EXPR;
5228 case CPP_RSHIFT: return RSHIFT_EXPR;
5230 case CPP_EQ: return -NOP_EXPR;
5231 case CPP_PLUS_EQ: return -PLUS_EXPR;
5232 case CPP_MINUS_EQ: return -MINUS_EXPR;
5233 case CPP_MULT_EQ: return -MULT_EXPR;
5234 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5235 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5236 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5237 case CPP_AND_EQ: return -BIT_AND_EXPR;
5238 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5239 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5240 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5242 case CPP_EQ_EQ: return EQ_EXPR;
5243 case CPP_NOT_EQ: return NE_EXPR;
5244 case CPP_LESS: return LT_EXPR;
5245 case CPP_GREATER: return GT_EXPR;
5246 case CPP_LESS_EQ: return LE_EXPR;
5247 case CPP_GREATER_EQ: return GE_EXPR;
5249 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5250 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5252 case CPP_COMMA: return COMPOUND_EXPR;
5254 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5255 case CPP_DEREF_STAR: return MEMBER_REF;
5257 default: return ERROR_MARK;
5261 /* Returns true if CODE indicates a binary expression, which is not allowed in
5262 the LHS of a fold-expression. More codes will need to be added to use this
5263 function in other contexts. */
5265 static bool
5266 is_binary_op (tree_code code)
5268 switch (code)
5270 case PLUS_EXPR:
5271 case POINTER_PLUS_EXPR:
5272 case MINUS_EXPR:
5273 case MULT_EXPR:
5274 case TRUNC_DIV_EXPR:
5275 case TRUNC_MOD_EXPR:
5276 case BIT_XOR_EXPR:
5277 case BIT_AND_EXPR:
5278 case BIT_IOR_EXPR:
5279 case LSHIFT_EXPR:
5280 case RSHIFT_EXPR:
5282 case MODOP_EXPR:
5284 case EQ_EXPR:
5285 case NE_EXPR:
5286 case LE_EXPR:
5287 case GE_EXPR:
5288 case LT_EXPR:
5289 case GT_EXPR:
5291 case TRUTH_ANDIF_EXPR:
5292 case TRUTH_ORIF_EXPR:
5294 case COMPOUND_EXPR:
5296 case DOTSTAR_EXPR:
5297 case MEMBER_REF:
5298 return true;
5300 default:
5301 return false;
5305 /* If the next token is a suitable fold operator, consume it and return as
5306 the function above. */
5308 static int
5309 cp_parser_fold_operator (cp_parser *parser)
5311 cp_token* token = cp_lexer_peek_token (parser->lexer);
5312 int code = cp_parser_fold_operator (token);
5313 if (code != ERROR_MARK)
5314 cp_lexer_consume_token (parser->lexer);
5315 return code;
5318 /* Parse a fold-expression.
5320 fold-expression:
5321 ( ... folding-operator cast-expression)
5322 ( cast-expression folding-operator ... )
5323 ( cast-expression folding operator ... folding-operator cast-expression)
5325 Note that the '(' and ')' are matched in primary expression. */
5327 static cp_expr
5328 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5330 cp_id_kind pidk;
5332 // Left fold.
5333 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5335 if (expr1)
5336 return error_mark_node;
5337 cp_lexer_consume_token (parser->lexer);
5338 int op = cp_parser_fold_operator (parser);
5339 if (op == ERROR_MARK)
5341 cp_parser_error (parser, "expected binary operator");
5342 return error_mark_node;
5345 tree expr = cp_parser_cast_expression (parser, false, false,
5346 false, &pidk);
5347 if (expr == error_mark_node)
5348 return error_mark_node;
5349 return finish_left_unary_fold_expr (expr, op);
5352 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5353 int op = cp_parser_fold_operator (parser);
5354 if (op == ERROR_MARK)
5356 cp_parser_error (parser, "expected binary operator");
5357 return error_mark_node;
5360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5362 cp_parser_error (parser, "expected ...");
5363 return error_mark_node;
5365 cp_lexer_consume_token (parser->lexer);
5367 /* The operands of a fold-expression are cast-expressions, so binary or
5368 conditional expressions are not allowed. We check this here to avoid
5369 tentative parsing. */
5370 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5371 /* OK, the expression was parenthesized. */;
5372 else if (is_binary_op (TREE_CODE (expr1)))
5373 error_at (location_of (expr1),
5374 "binary expression in operand of fold-expression");
5375 else if (TREE_CODE (expr1) == COND_EXPR
5376 || (REFERENCE_REF_P (expr1)
5377 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5378 error_at (location_of (expr1),
5379 "conditional expression in operand of fold-expression");
5381 // Right fold.
5382 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5383 return finish_right_unary_fold_expr (expr1, op);
5385 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5387 cp_parser_error (parser, "mismatched operator in fold-expression");
5388 return error_mark_node;
5390 cp_lexer_consume_token (parser->lexer);
5392 // Binary left or right fold.
5393 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5394 if (expr2 == error_mark_node)
5395 return error_mark_node;
5396 return finish_binary_fold_expr (expr1, expr2, op);
5399 /* Parse a primary-expression.
5401 primary-expression:
5402 literal
5403 this
5404 ( expression )
5405 id-expression
5406 lambda-expression (C++11)
5408 GNU Extensions:
5410 primary-expression:
5411 ( compound-statement )
5412 __builtin_va_arg ( assignment-expression , type-id )
5413 __builtin_offsetof ( type-id , offsetof-expression )
5415 C++ Extensions:
5416 __has_nothrow_assign ( type-id )
5417 __has_nothrow_constructor ( type-id )
5418 __has_nothrow_copy ( type-id )
5419 __has_trivial_assign ( type-id )
5420 __has_trivial_constructor ( type-id )
5421 __has_trivial_copy ( type-id )
5422 __has_trivial_destructor ( type-id )
5423 __has_virtual_destructor ( type-id )
5424 __is_abstract ( type-id )
5425 __is_base_of ( type-id , type-id )
5426 __is_class ( type-id )
5427 __is_empty ( type-id )
5428 __is_enum ( type-id )
5429 __is_final ( type-id )
5430 __is_literal_type ( type-id )
5431 __is_pod ( type-id )
5432 __is_polymorphic ( type-id )
5433 __is_std_layout ( type-id )
5434 __is_trivial ( type-id )
5435 __is_union ( type-id )
5437 Objective-C++ Extension:
5439 primary-expression:
5440 objc-expression
5442 literal:
5443 __null
5445 ADDRESS_P is true iff this expression was immediately preceded by
5446 "&" and therefore might denote a pointer-to-member. CAST_P is true
5447 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5448 true iff this expression is a template argument.
5450 Returns a representation of the expression. Upon return, *IDK
5451 indicates what kind of id-expression (if any) was present. */
5453 static cp_expr
5454 cp_parser_primary_expression (cp_parser *parser,
5455 bool address_p,
5456 bool cast_p,
5457 bool template_arg_p,
5458 bool decltype_p,
5459 cp_id_kind *idk)
5461 cp_token *token = NULL;
5463 /* Assume the primary expression is not an id-expression. */
5464 *idk = CP_ID_KIND_NONE;
5466 /* Peek at the next token. */
5467 token = cp_lexer_peek_token (parser->lexer);
5468 switch ((int) token->type)
5470 /* literal:
5471 integer-literal
5472 character-literal
5473 floating-literal
5474 string-literal
5475 boolean-literal
5476 pointer-literal
5477 user-defined-literal */
5478 case CPP_CHAR:
5479 case CPP_CHAR16:
5480 case CPP_CHAR32:
5481 case CPP_WCHAR:
5482 case CPP_UTF8CHAR:
5483 case CPP_NUMBER:
5484 case CPP_PREPARSED_EXPR:
5485 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5486 return cp_parser_userdef_numeric_literal (parser);
5487 token = cp_lexer_consume_token (parser->lexer);
5488 if (TREE_CODE (token->u.value) == FIXED_CST)
5490 error_at (token->location,
5491 "fixed-point types not supported in C++");
5492 return error_mark_node;
5494 /* Floating-point literals are only allowed in an integral
5495 constant expression if they are cast to an integral or
5496 enumeration type. */
5497 if (TREE_CODE (token->u.value) == REAL_CST
5498 && parser->integral_constant_expression_p
5499 && pedantic)
5501 /* CAST_P will be set even in invalid code like "int(2.7 +
5502 ...)". Therefore, we have to check that the next token
5503 is sure to end the cast. */
5504 if (cast_p)
5506 cp_token *next_token;
5508 next_token = cp_lexer_peek_token (parser->lexer);
5509 if (/* The comma at the end of an
5510 enumerator-definition. */
5511 next_token->type != CPP_COMMA
5512 /* The curly brace at the end of an enum-specifier. */
5513 && next_token->type != CPP_CLOSE_BRACE
5514 /* The end of a statement. */
5515 && next_token->type != CPP_SEMICOLON
5516 /* The end of the cast-expression. */
5517 && next_token->type != CPP_CLOSE_PAREN
5518 /* The end of an array bound. */
5519 && next_token->type != CPP_CLOSE_SQUARE
5520 /* The closing ">" in a template-argument-list. */
5521 && (next_token->type != CPP_GREATER
5522 || parser->greater_than_is_operator_p)
5523 /* C++0x only: A ">>" treated like two ">" tokens,
5524 in a template-argument-list. */
5525 && (next_token->type != CPP_RSHIFT
5526 || (cxx_dialect == cxx98)
5527 || parser->greater_than_is_operator_p))
5528 cast_p = false;
5531 /* If we are within a cast, then the constraint that the
5532 cast is to an integral or enumeration type will be
5533 checked at that point. If we are not within a cast, then
5534 this code is invalid. */
5535 if (!cast_p)
5536 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5538 return (cp_expr (token->u.value, token->location)
5539 .maybe_add_location_wrapper ());
5541 case CPP_CHAR_USERDEF:
5542 case CPP_CHAR16_USERDEF:
5543 case CPP_CHAR32_USERDEF:
5544 case CPP_WCHAR_USERDEF:
5545 case CPP_UTF8CHAR_USERDEF:
5546 return cp_parser_userdef_char_literal (parser);
5548 case CPP_STRING:
5549 case CPP_STRING16:
5550 case CPP_STRING32:
5551 case CPP_WSTRING:
5552 case CPP_UTF8STRING:
5553 case CPP_STRING_USERDEF:
5554 case CPP_STRING16_USERDEF:
5555 case CPP_STRING32_USERDEF:
5556 case CPP_WSTRING_USERDEF:
5557 case CPP_UTF8STRING_USERDEF:
5558 /* ??? Should wide strings be allowed when parser->translate_strings_p
5559 is false (i.e. in attributes)? If not, we can kill the third
5560 argument to cp_parser_string_literal. */
5561 return (cp_parser_string_literal (parser,
5562 parser->translate_strings_p,
5563 true)
5564 .maybe_add_location_wrapper ());
5566 case CPP_OPEN_PAREN:
5567 /* If we see `( { ' then we are looking at the beginning of
5568 a GNU statement-expression. */
5569 if (cp_parser_allow_gnu_extensions_p (parser)
5570 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5572 /* Statement-expressions are not allowed by the standard. */
5573 pedwarn (token->location, OPT_Wpedantic,
5574 "ISO C++ forbids braced-groups within expressions");
5576 /* And they're not allowed outside of a function-body; you
5577 cannot, for example, write:
5579 int i = ({ int j = 3; j + 1; });
5581 at class or namespace scope. */
5582 if (!parser->in_function_body
5583 || parser->in_template_argument_list_p)
5585 error_at (token->location,
5586 "statement-expressions are not allowed outside "
5587 "functions nor in template-argument lists");
5588 cp_parser_skip_to_end_of_block_or_statement (parser);
5589 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5590 cp_lexer_consume_token (parser->lexer);
5591 return error_mark_node;
5593 else
5594 return cp_parser_statement_expr (parser);
5596 /* Otherwise it's a normal parenthesized expression. */
5598 cp_expr expr;
5599 bool saved_greater_than_is_operator_p;
5601 location_t open_paren_loc = token->location;
5603 /* Consume the `('. */
5604 matching_parens parens;
5605 parens.consume_open (parser);
5606 /* Within a parenthesized expression, a `>' token is always
5607 the greater-than operator. */
5608 saved_greater_than_is_operator_p
5609 = parser->greater_than_is_operator_p;
5610 parser->greater_than_is_operator_p = true;
5612 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5613 /* Left fold expression. */
5614 expr = NULL_TREE;
5615 else
5616 /* Parse the parenthesized expression. */
5617 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5619 token = cp_lexer_peek_token (parser->lexer);
5620 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5622 expr = cp_parser_fold_expression (parser, expr);
5623 if (expr != error_mark_node
5624 && cxx_dialect < cxx17)
5625 pedwarn (input_location, OPT_Wc__17_extensions,
5626 "fold-expressions only available with %<-std=c++17%> "
5627 "or %<-std=gnu++17%>");
5629 else
5630 /* Let the front end know that this expression was
5631 enclosed in parentheses. This matters in case, for
5632 example, the expression is of the form `A::B', since
5633 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5634 not. */
5635 expr = finish_parenthesized_expr (expr);
5637 /* DR 705: Wrapping an unqualified name in parentheses
5638 suppresses arg-dependent lookup. We want to pass back
5639 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5640 (c++/37862), but none of the others. */
5641 if (*idk != CP_ID_KIND_QUALIFIED)
5642 *idk = CP_ID_KIND_NONE;
5644 /* The `>' token might be the end of a template-id or
5645 template-parameter-list now. */
5646 parser->greater_than_is_operator_p
5647 = saved_greater_than_is_operator_p;
5649 /* Consume the `)'. */
5650 token = cp_lexer_peek_token (parser->lexer);
5651 location_t close_paren_loc = token->location;
5652 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5653 expr.set_range (open_paren_loc, close_paren_loc);
5654 if (no_wparens)
5655 suppress_warning (expr, OPT_Wparentheses);
5656 if (!parens.require_close (parser)
5657 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5658 cp_parser_skip_to_end_of_statement (parser);
5660 return expr;
5663 case CPP_OPEN_SQUARE:
5665 if (c_dialect_objc ())
5667 /* We might have an Objective-C++ message. */
5668 cp_parser_parse_tentatively (parser);
5669 tree msg = cp_parser_objc_message_expression (parser);
5670 /* If that works out, we're done ... */
5671 if (cp_parser_parse_definitely (parser))
5672 return msg;
5673 /* ... else, fall though to see if it's a lambda. */
5675 cp_expr lam = cp_parser_lambda_expression (parser);
5676 /* Don't warn about a failed tentative parse. */
5677 if (cp_parser_error_occurred (parser))
5678 return error_mark_node;
5679 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5680 return lam;
5683 case CPP_OBJC_STRING:
5684 if (c_dialect_objc ())
5685 /* We have an Objective-C++ string literal. */
5686 return cp_parser_objc_expression (parser);
5687 cp_parser_error (parser, "expected primary-expression");
5688 return error_mark_node;
5690 case CPP_KEYWORD:
5691 switch (token->keyword)
5693 /* These two are the boolean literals. */
5694 case RID_TRUE:
5695 cp_lexer_consume_token (parser->lexer);
5696 return cp_expr (boolean_true_node, token->location);
5697 case RID_FALSE:
5698 cp_lexer_consume_token (parser->lexer);
5699 return cp_expr (boolean_false_node, token->location);
5701 /* The `__null' literal. */
5702 case RID_NULL:
5703 cp_lexer_consume_token (parser->lexer);
5704 return cp_expr (null_node, token->location);
5706 /* The `nullptr' literal. */
5707 case RID_NULLPTR:
5708 cp_lexer_consume_token (parser->lexer);
5709 return cp_expr (nullptr_node, token->location);
5711 /* Recognize the `this' keyword. */
5712 case RID_THIS:
5713 cp_lexer_consume_token (parser->lexer);
5714 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5716 error_at (token->location,
5717 "%<this%> may not be used in this context");
5718 return error_mark_node;
5720 /* Pointers cannot appear in constant-expressions. */
5721 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5722 return error_mark_node;
5723 return cp_expr (finish_this_expr (), token->location);
5725 /* The `operator' keyword can be the beginning of an
5726 id-expression. */
5727 case RID_OPERATOR:
5728 goto id_expression;
5730 case RID_FUNCTION_NAME:
5731 case RID_PRETTY_FUNCTION_NAME:
5732 case RID_C99_FUNCTION_NAME:
5734 non_integral_constant name;
5736 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5737 __func__ are the names of variables -- but they are
5738 treated specially. Therefore, they are handled here,
5739 rather than relying on the generic id-expression logic
5740 below. Grammatically, these names are id-expressions.
5742 Consume the token. */
5743 token = cp_lexer_consume_token (parser->lexer);
5745 switch (token->keyword)
5747 case RID_FUNCTION_NAME:
5748 name = NIC_FUNC_NAME;
5749 break;
5750 case RID_PRETTY_FUNCTION_NAME:
5751 name = NIC_PRETTY_FUNC;
5752 break;
5753 case RID_C99_FUNCTION_NAME:
5754 name = NIC_C99_FUNC;
5755 break;
5756 default:
5757 gcc_unreachable ();
5760 if (cp_parser_non_integral_constant_expression (parser, name))
5761 return error_mark_node;
5763 /* Look up the name. */
5764 return finish_fname (token->u.value);
5767 case RID_VA_ARG:
5769 tree expression;
5770 tree type;
5771 location_t type_location;
5772 location_t start_loc
5773 = cp_lexer_peek_token (parser->lexer)->location;
5774 /* The `__builtin_va_arg' construct is used to handle
5775 `va_arg'. Consume the `__builtin_va_arg' token. */
5776 cp_lexer_consume_token (parser->lexer);
5777 /* Look for the opening `('. */
5778 matching_parens parens;
5779 parens.require_open (parser);
5780 /* Now, parse the assignment-expression. */
5781 expression = cp_parser_assignment_expression (parser);
5782 /* Look for the `,'. */
5783 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5784 type_location = cp_lexer_peek_token (parser->lexer)->location;
5785 /* Parse the type-id. */
5787 type_id_in_expr_sentinel s (parser);
5788 type = cp_parser_type_id (parser);
5790 /* Look for the closing `)'. */
5791 location_t finish_loc
5792 = cp_lexer_peek_token (parser->lexer)->location;
5793 parens.require_close (parser);
5794 /* Using `va_arg' in a constant-expression is not
5795 allowed. */
5796 if (cp_parser_non_integral_constant_expression (parser,
5797 NIC_VA_ARG))
5798 return error_mark_node;
5799 /* Construct a location of the form:
5800 __builtin_va_arg (v, int)
5801 ~~~~~~~~~~~~~~~~~~~~~^~~~
5802 with the caret at the type, ranging from the start of the
5803 "__builtin_va_arg" token to the close paren. */
5804 location_t combined_loc
5805 = make_location (type_location, start_loc, finish_loc);
5806 return build_x_va_arg (combined_loc, expression, type);
5809 case RID_OFFSETOF:
5810 return cp_parser_builtin_offsetof (parser);
5812 case RID_HAS_NOTHROW_ASSIGN:
5813 case RID_HAS_NOTHROW_CONSTRUCTOR:
5814 case RID_HAS_NOTHROW_COPY:
5815 case RID_HAS_TRIVIAL_ASSIGN:
5816 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5817 case RID_HAS_TRIVIAL_COPY:
5818 case RID_HAS_TRIVIAL_DESTRUCTOR:
5819 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5820 case RID_HAS_VIRTUAL_DESTRUCTOR:
5821 case RID_IS_ABSTRACT:
5822 case RID_IS_AGGREGATE:
5823 case RID_IS_BASE_OF:
5824 case RID_IS_CLASS:
5825 case RID_IS_EMPTY:
5826 case RID_IS_ENUM:
5827 case RID_IS_FINAL:
5828 case RID_IS_LAYOUT_COMPATIBLE:
5829 case RID_IS_LITERAL_TYPE:
5830 case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
5831 case RID_IS_POD:
5832 case RID_IS_POLYMORPHIC:
5833 case RID_IS_SAME_AS:
5834 case RID_IS_STD_LAYOUT:
5835 case RID_IS_TRIVIAL:
5836 case RID_IS_TRIVIALLY_ASSIGNABLE:
5837 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5838 case RID_IS_TRIVIALLY_COPYABLE:
5839 case RID_IS_UNION:
5840 case RID_IS_ASSIGNABLE:
5841 case RID_IS_CONSTRUCTIBLE:
5842 case RID_IS_NOTHROW_ASSIGNABLE:
5843 case RID_IS_NOTHROW_CONSTRUCTIBLE:
5844 return cp_parser_trait_expr (parser, token->keyword);
5846 // C++ concepts
5847 case RID_REQUIRES:
5848 return cp_parser_requires_expression (parser);
5850 /* Objective-C++ expressions. */
5851 case RID_AT_ENCODE:
5852 case RID_AT_PROTOCOL:
5853 case RID_AT_SELECTOR:
5854 return cp_parser_objc_expression (parser);
5856 case RID_TEMPLATE:
5857 if (parser->in_function_body
5858 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5859 == CPP_LESS))
5861 error_at (token->location,
5862 "a template declaration cannot appear at block scope");
5863 cp_parser_skip_to_end_of_block_or_statement (parser);
5864 return error_mark_node;
5866 /* FALLTHRU */
5867 default:
5868 cp_parser_error (parser, "expected primary-expression");
5869 return error_mark_node;
5872 /* An id-expression can start with either an identifier, a
5873 `::' as the beginning of a qualified-id, or the "operator"
5874 keyword. */
5875 case CPP_NAME:
5876 case CPP_SCOPE:
5877 case CPP_TEMPLATE_ID:
5878 case CPP_NESTED_NAME_SPECIFIER:
5880 id_expression:
5881 cp_expr id_expression;
5882 cp_expr decl;
5883 const char *error_msg;
5884 bool template_p;
5885 bool done;
5886 cp_token *id_expr_token;
5888 /* Parse the id-expression. */
5889 id_expression
5890 = cp_parser_id_expression (parser,
5891 /*template_keyword_p=*/false,
5892 /*check_dependency_p=*/true,
5893 &template_p,
5894 /*declarator_p=*/false,
5895 /*optional_p=*/false);
5896 if (id_expression == error_mark_node)
5897 return error_mark_node;
5898 id_expr_token = token;
5899 token = cp_lexer_peek_token (parser->lexer);
5900 done = (token->type != CPP_OPEN_SQUARE
5901 && token->type != CPP_OPEN_PAREN
5902 && token->type != CPP_DOT
5903 && token->type != CPP_DEREF
5904 && token->type != CPP_PLUS_PLUS
5905 && token->type != CPP_MINUS_MINUS);
5906 /* If we have a template-id, then no further lookup is
5907 required. If the template-id was for a template-class, we
5908 will sometimes have a TYPE_DECL at this point. */
5909 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5910 || TREE_CODE (id_expression) == TYPE_DECL)
5911 decl = id_expression;
5912 /* Look up the name. */
5913 else
5915 tree ambiguous_decls;
5917 /* If we already know that this lookup is ambiguous, then
5918 we've already issued an error message; there's no reason
5919 to check again. */
5920 if (id_expr_token->type == CPP_NAME
5921 && id_expr_token->error_reported)
5923 cp_parser_simulate_error (parser);
5924 return error_mark_node;
5927 decl = cp_parser_lookup_name (parser, id_expression,
5928 none_type,
5929 template_p,
5930 /*is_namespace=*/false,
5931 /*check_dependency=*/true,
5932 &ambiguous_decls,
5933 id_expression.get_location ());
5934 /* If the lookup was ambiguous, an error will already have
5935 been issued. */
5936 if (ambiguous_decls)
5937 return error_mark_node;
5939 /* In Objective-C++, we may have an Objective-C 2.0
5940 dot-syntax for classes here. */
5941 if (c_dialect_objc ()
5942 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5943 && TREE_CODE (decl) == TYPE_DECL
5944 && objc_is_class_name (decl))
5946 tree component;
5947 cp_lexer_consume_token (parser->lexer);
5948 component = cp_parser_identifier (parser);
5949 if (component == error_mark_node)
5950 return error_mark_node;
5952 tree result = objc_build_class_component_ref (id_expression,
5953 component);
5954 /* Build a location of the form:
5955 expr.component
5956 ~~~~~^~~~~~~~~
5957 with caret at the start of the component name (at
5958 input_location), ranging from the start of the id_expression
5959 to the end of the component name. */
5960 location_t combined_loc
5961 = make_location (input_location, id_expression.get_start (),
5962 get_finish (input_location));
5963 protected_set_expr_location (result, combined_loc);
5964 return result;
5967 /* In Objective-C++, an instance variable (ivar) may be preferred
5968 to whatever cp_parser_lookup_name() found.
5969 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5970 rest of c-family, we have to do a little extra work to preserve
5971 any location information in cp_expr "decl". Given that
5972 objc_lookup_ivar is implemented in "c-family" and "objc", we
5973 have a trip through the pure "tree" type, rather than cp_expr.
5974 Naively copying it back to "decl" would implicitly give the
5975 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5976 store an EXPR_LOCATION. Hence we only update "decl" (and
5977 hence its location_t) if we get back a different tree node. */
5978 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5979 id_expression);
5980 if (decl_tree != decl.get_value ())
5981 decl = cp_expr (decl_tree);
5983 /* If name lookup gives us a SCOPE_REF, then the
5984 qualifying scope was dependent. */
5985 if (TREE_CODE (decl) == SCOPE_REF)
5987 /* At this point, we do not know if DECL is a valid
5988 integral constant expression. We assume that it is
5989 in fact such an expression, so that code like:
5991 template <int N> struct A {
5992 int a[B<N>::i];
5995 is accepted. At template-instantiation time, we
5996 will check that B<N>::i is actually a constant. */
5997 return decl;
5999 /* Check to see if DECL is a local variable in a context
6000 where that is forbidden. */
6001 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6002 && local_variable_p (decl)
6003 /* DR 2082 permits local variables in unevaluated contexts
6004 within a default argument. */
6005 && !cp_unevaluated_operand)
6007 const char *msg
6008 = (TREE_CODE (decl) == PARM_DECL
6009 ? _("parameter %qD may not appear in this context")
6010 : _("local variable %qD may not appear in this context"));
6011 error_at (id_expression.get_location (), msg,
6012 decl.get_value ());
6013 return error_mark_node;
6017 decl = (finish_id_expression
6018 (id_expression, decl, parser->scope,
6019 idk,
6020 parser->integral_constant_expression_p,
6021 parser->allow_non_integral_constant_expression_p,
6022 &parser->non_integral_constant_expression_p,
6023 template_p, done, address_p,
6024 template_arg_p,
6025 &error_msg,
6026 id_expression.get_location ()));
6027 if (error_msg)
6028 cp_parser_error (parser, error_msg);
6029 /* Build a location for an id-expression of the form:
6030 ::ns::id
6031 ~~~~~~^~
6035 i.e. from the start of the first token to the end of the final
6036 token, with the caret at the start of the unqualified-id. */
6037 location_t caret_loc = get_pure_location (id_expression.get_location ());
6038 location_t start_loc = get_start (id_expr_token->location);
6039 location_t finish_loc = get_finish (id_expression.get_location ());
6040 location_t combined_loc
6041 = make_location (caret_loc, start_loc, finish_loc);
6043 decl.set_location (combined_loc);
6044 return decl;
6047 /* Anything else is an error. */
6048 default:
6049 cp_parser_error (parser, "expected primary-expression");
6050 return error_mark_node;
6054 static inline cp_expr
6055 cp_parser_primary_expression (cp_parser *parser,
6056 bool address_p,
6057 bool cast_p,
6058 bool template_arg_p,
6059 cp_id_kind *idk)
6061 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6062 /*decltype*/false, idk);
6065 /* Parse an id-expression.
6067 id-expression:
6068 unqualified-id
6069 qualified-id
6071 qualified-id:
6072 :: [opt] nested-name-specifier template [opt] unqualified-id
6073 :: identifier
6074 :: operator-function-id
6075 :: template-id
6077 Return a representation of the unqualified portion of the
6078 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6079 a `::' or nested-name-specifier.
6081 Often, if the id-expression was a qualified-id, the caller will
6082 want to make a SCOPE_REF to represent the qualified-id. This
6083 function does not do this in order to avoid wastefully creating
6084 SCOPE_REFs when they are not required.
6086 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6087 `template' keyword.
6089 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6090 uninstantiated templates.
6092 If *TEMPLATE_P is non-NULL, it is set to true iff the
6093 `template' keyword is used to explicitly indicate that the entity
6094 named is a template.
6096 If DECLARATOR_P is true, the id-expression is appearing as part of
6097 a declarator, rather than as part of an expression. */
6099 static cp_expr
6100 cp_parser_id_expression (cp_parser *parser,
6101 bool template_keyword_p,
6102 bool check_dependency_p,
6103 bool *template_p,
6104 bool declarator_p,
6105 bool optional_p)
6107 bool global_scope_p;
6108 bool nested_name_specifier_p;
6110 /* Assume the `template' keyword was not used. */
6111 if (template_p)
6112 *template_p = template_keyword_p;
6114 /* Look for the optional `::' operator. */
6115 global_scope_p
6116 = (!template_keyword_p
6117 && (cp_parser_global_scope_opt (parser,
6118 /*current_scope_valid_p=*/false)
6119 != NULL_TREE));
6121 /* Look for the optional nested-name-specifier. */
6122 nested_name_specifier_p
6123 = (cp_parser_nested_name_specifier_opt (parser,
6124 /*typename_keyword_p=*/false,
6125 check_dependency_p,
6126 /*type_p=*/false,
6127 declarator_p,
6128 template_keyword_p)
6129 != NULL_TREE);
6131 /* If there is a nested-name-specifier, then we are looking at
6132 the first qualified-id production. */
6133 if (nested_name_specifier_p)
6135 tree saved_scope;
6136 tree saved_object_scope;
6137 tree saved_qualifying_scope;
6138 cp_expr unqualified_id;
6139 bool is_template;
6141 /* See if the next token is the `template' keyword. */
6142 if (!template_p)
6143 template_p = &is_template;
6144 *template_p = cp_parser_optional_template_keyword (parser);
6145 /* Name lookup we do during the processing of the
6146 unqualified-id might obliterate SCOPE. */
6147 saved_scope = parser->scope;
6148 saved_object_scope = parser->object_scope;
6149 saved_qualifying_scope = parser->qualifying_scope;
6150 /* Process the final unqualified-id. */
6151 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
6152 check_dependency_p,
6153 declarator_p,
6154 /*optional_p=*/false);
6155 /* Restore the SAVED_SCOPE for our caller. */
6156 parser->scope = saved_scope;
6157 parser->object_scope = saved_object_scope;
6158 parser->qualifying_scope = saved_qualifying_scope;
6160 return unqualified_id;
6162 /* Otherwise, if we are in global scope, then we are looking at one
6163 of the other qualified-id productions. */
6164 else if (global_scope_p)
6166 cp_token *token;
6167 tree id;
6169 /* Peek at the next token. */
6170 token = cp_lexer_peek_token (parser->lexer);
6172 /* If it's an identifier, and the next token is not a "<", then
6173 we can avoid the template-id case. This is an optimization
6174 for this common case. */
6175 if (token->type == CPP_NAME
6176 && !cp_parser_nth_token_starts_template_argument_list_p
6177 (parser, 2))
6178 return cp_parser_identifier (parser);
6180 cp_parser_parse_tentatively (parser);
6181 /* Try a template-id. */
6182 id = cp_parser_template_id_expr (parser,
6183 /*template_keyword_p=*/false,
6184 /*check_dependency_p=*/true,
6185 declarator_p);
6186 /* If that worked, we're done. */
6187 if (cp_parser_parse_definitely (parser))
6188 return id;
6190 /* Peek at the next token. (Changes in the token buffer may
6191 have invalidated the pointer obtained above.) */
6192 token = cp_lexer_peek_token (parser->lexer);
6194 switch (token->type)
6196 case CPP_NAME:
6197 return cp_parser_identifier (parser);
6199 case CPP_KEYWORD:
6200 if (token->keyword == RID_OPERATOR)
6201 return cp_parser_operator_function_id (parser);
6202 /* Fall through. */
6204 default:
6205 cp_parser_error (parser, "expected id-expression");
6206 return error_mark_node;
6209 else
6210 return cp_parser_unqualified_id (parser, template_keyword_p,
6211 /*check_dependency_p=*/true,
6212 declarator_p,
6213 optional_p);
6216 /* Parse an unqualified-id.
6218 unqualified-id:
6219 identifier
6220 operator-function-id
6221 conversion-function-id
6222 ~ class-name
6223 template-id
6225 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6226 keyword, in a construct like `A::template ...'.
6228 Returns a representation of unqualified-id. For the `identifier'
6229 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6230 production a BIT_NOT_EXPR is returned; the operand of the
6231 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6232 other productions, see the documentation accompanying the
6233 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6234 names are looked up in uninstantiated templates. If DECLARATOR_P
6235 is true, the unqualified-id is appearing as part of a declarator,
6236 rather than as part of an expression. */
6238 static cp_expr
6239 cp_parser_unqualified_id (cp_parser* parser,
6240 bool template_keyword_p,
6241 bool check_dependency_p,
6242 bool declarator_p,
6243 bool optional_p)
6245 cp_token *token;
6247 /* Peek at the next token. */
6248 token = cp_lexer_peek_token (parser->lexer);
6250 switch ((int) token->type)
6252 case CPP_NAME:
6254 tree id;
6256 /* We don't know yet whether or not this will be a
6257 template-id. */
6258 cp_parser_parse_tentatively (parser);
6259 /* Try a template-id. */
6260 id = cp_parser_template_id_expr (parser, template_keyword_p,
6261 check_dependency_p,
6262 declarator_p);
6263 /* If it worked, we're done. */
6264 if (cp_parser_parse_definitely (parser))
6265 return id;
6266 /* Otherwise, it's an ordinary identifier. */
6267 return cp_parser_identifier (parser);
6270 case CPP_TEMPLATE_ID:
6271 return cp_parser_template_id_expr (parser, template_keyword_p,
6272 check_dependency_p,
6273 declarator_p);
6275 case CPP_COMPL:
6277 tree type_decl;
6278 tree qualifying_scope;
6279 tree object_scope;
6280 tree scope;
6281 bool done;
6282 location_t tilde_loc = token->location;
6284 /* Consume the `~' token. */
6285 cp_lexer_consume_token (parser->lexer);
6286 /* Parse the class-name. The standard, as written, seems to
6287 say that:
6289 template <typename T> struct S { ~S (); };
6290 template <typename T> S<T>::~S() {}
6292 is invalid, since `~' must be followed by a class-name, but
6293 `S<T>' is dependent, and so not known to be a class.
6294 That's not right; we need to look in uninstantiated
6295 templates. A further complication arises from:
6297 template <typename T> void f(T t) {
6298 t.T::~T();
6301 Here, it is not possible to look up `T' in the scope of `T'
6302 itself. We must look in both the current scope, and the
6303 scope of the containing complete expression.
6305 Yet another issue is:
6307 struct S {
6308 int S;
6309 ~S();
6312 S::~S() {}
6314 The standard does not seem to say that the `S' in `~S'
6315 should refer to the type `S' and not the data member
6316 `S::S'. */
6318 /* DR 244 says that we look up the name after the "~" in the
6319 same scope as we looked up the qualifying name. That idea
6320 isn't fully worked out; it's more complicated than that. */
6321 scope = parser->scope;
6322 object_scope = parser->object_scope;
6323 qualifying_scope = parser->qualifying_scope;
6325 /* Check for invalid scopes. */
6326 if (scope == error_mark_node)
6328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6329 cp_lexer_consume_token (parser->lexer);
6330 return error_mark_node;
6332 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6334 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6335 error_at (token->location,
6336 "scope %qT before %<~%> is not a class-name",
6337 scope);
6338 cp_parser_simulate_error (parser);
6339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6340 cp_lexer_consume_token (parser->lexer);
6341 return error_mark_node;
6343 if (template_keyword_p)
6345 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6346 error_at (tilde_loc, "%<template%> keyword not permitted in "
6347 "destructor name");
6348 cp_parser_simulate_error (parser);
6349 return error_mark_node;
6352 gcc_assert (!scope || TYPE_P (scope));
6354 token = cp_lexer_peek_token (parser->lexer);
6356 /* Create a location with caret == start at the tilde,
6357 finishing at the end of the peeked token, e.g:
6358 ~token
6359 ^~~~~~. */
6360 location_t loc
6361 = make_location (tilde_loc, tilde_loc, token->location);
6363 /* If the name is of the form "X::~X" it's OK even if X is a
6364 typedef. */
6366 if (scope
6367 && token->type == CPP_NAME
6368 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6369 != CPP_LESS)
6370 && (token->u.value == TYPE_IDENTIFIER (scope)
6371 || (CLASS_TYPE_P (scope)
6372 && constructor_name_p (token->u.value, scope))))
6374 cp_lexer_consume_token (parser->lexer);
6375 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6378 /* ~auto means the destructor of whatever the object is. */
6379 if (cp_parser_is_keyword (token, RID_AUTO))
6381 if (cxx_dialect < cxx14)
6382 pedwarn (loc, OPT_Wc__14_extensions,
6383 "%<~auto%> only available with "
6384 "%<-std=c++14%> or %<-std=gnu++14%>");
6385 cp_lexer_consume_token (parser->lexer);
6386 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6389 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6390 declarator-id of a constructor or destructor. */
6391 if (token->type == CPP_TEMPLATE_ID && declarator_p
6392 && cxx_dialect >= cxx20)
6394 if (!cp_parser_simulate_error (parser))
6395 error_at (tilde_loc, "template-id not allowed for destructor");
6396 return error_mark_node;
6399 /* If there was an explicit qualification (S::~T), first look
6400 in the scope given by the qualification (i.e., S).
6402 Note: in the calls to cp_parser_class_name below we pass
6403 typename_type so that lookup finds the injected-class-name
6404 rather than the constructor. */
6405 done = false;
6406 type_decl = NULL_TREE;
6407 if (scope)
6409 cp_parser_parse_tentatively (parser);
6410 type_decl = cp_parser_class_name (parser,
6411 /*typename_keyword_p=*/false,
6412 /*template_keyword_p=*/false,
6413 typename_type,
6414 /*check_dependency=*/false,
6415 /*class_head_p=*/false,
6416 declarator_p);
6417 if (cp_parser_parse_definitely (parser))
6418 done = true;
6420 /* In "N::S::~S", look in "N" as well. */
6421 if (!done && scope && qualifying_scope)
6423 cp_parser_parse_tentatively (parser);
6424 parser->scope = qualifying_scope;
6425 parser->object_scope = NULL_TREE;
6426 parser->qualifying_scope = NULL_TREE;
6427 type_decl
6428 = cp_parser_class_name (parser,
6429 /*typename_keyword_p=*/false,
6430 /*template_keyword_p=*/false,
6431 typename_type,
6432 /*check_dependency=*/false,
6433 /*class_head_p=*/false,
6434 declarator_p);
6435 if (cp_parser_parse_definitely (parser))
6436 done = true;
6438 /* In "p->S::~T", look in the scope given by "*p" as well. */
6439 else if (!done && object_scope)
6441 cp_parser_parse_tentatively (parser);
6442 parser->scope = object_scope;
6443 parser->object_scope = NULL_TREE;
6444 parser->qualifying_scope = NULL_TREE;
6445 type_decl
6446 = cp_parser_class_name (parser,
6447 /*typename_keyword_p=*/false,
6448 /*template_keyword_p=*/false,
6449 typename_type,
6450 /*check_dependency=*/false,
6451 /*class_head_p=*/false,
6452 declarator_p);
6453 if (cp_parser_parse_definitely (parser))
6454 done = true;
6456 /* Look in the surrounding context. */
6457 if (!done)
6459 parser->scope = NULL_TREE;
6460 parser->object_scope = NULL_TREE;
6461 parser->qualifying_scope = NULL_TREE;
6462 if (processing_template_decl)
6463 cp_parser_parse_tentatively (parser);
6464 type_decl
6465 = cp_parser_class_name (parser,
6466 /*typename_keyword_p=*/false,
6467 /*template_keyword_p=*/false,
6468 typename_type,
6469 /*check_dependency=*/false,
6470 /*class_head_p=*/false,
6471 declarator_p);
6472 if (processing_template_decl
6473 && ! cp_parser_parse_definitely (parser))
6475 /* We couldn't find a type with this name. If we're parsing
6476 tentatively, fail and try something else. */
6477 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6479 cp_parser_simulate_error (parser);
6480 return error_mark_node;
6482 /* Otherwise, accept it and check for a match at instantiation
6483 time. */
6484 type_decl = cp_parser_identifier (parser);
6485 if (type_decl != error_mark_node)
6486 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6487 return type_decl;
6490 /* If an error occurred, assume that the name of the
6491 destructor is the same as the name of the qualifying
6492 class. That allows us to keep parsing after running
6493 into ill-formed destructor names. */
6494 if (type_decl == error_mark_node && scope)
6495 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6496 else if (type_decl == error_mark_node)
6497 return error_mark_node;
6499 /* Check that destructor name and scope match. */
6500 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6502 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6503 error_at (loc,
6504 "declaration of %<~%T%> as member of %qT",
6505 type_decl, scope);
6506 cp_parser_simulate_error (parser);
6507 return error_mark_node;
6510 /* [class.dtor]
6512 A typedef-name that names a class shall not be used as the
6513 identifier in the declarator for a destructor declaration. */
6514 if (declarator_p
6515 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6516 && !DECL_SELF_REFERENCE_P (type_decl)
6517 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6518 error_at (loc,
6519 "typedef-name %qD used as destructor declarator",
6520 type_decl);
6522 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6525 case CPP_KEYWORD:
6526 if (token->keyword == RID_OPERATOR)
6528 cp_expr id;
6530 /* This could be a template-id, so we try that first. */
6531 cp_parser_parse_tentatively (parser);
6532 /* Try a template-id. */
6533 id = cp_parser_template_id_expr (parser, template_keyword_p,
6534 /*check_dependency_p=*/true,
6535 declarator_p);
6536 /* If that worked, we're done. */
6537 if (cp_parser_parse_definitely (parser))
6538 return id;
6539 /* We still don't know whether we're looking at an
6540 operator-function-id or a conversion-function-id. */
6541 cp_parser_parse_tentatively (parser);
6542 /* Try an operator-function-id. */
6543 id = cp_parser_operator_function_id (parser);
6544 /* If that didn't work, try a conversion-function-id. */
6545 if (!cp_parser_parse_definitely (parser))
6546 id = cp_parser_conversion_function_id (parser);
6548 return id;
6550 /* Fall through. */
6552 default:
6553 if (optional_p)
6554 return NULL_TREE;
6555 cp_parser_error (parser, "expected unqualified-id");
6556 return error_mark_node;
6560 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6561 be a template-id or the name shall refer to a class template or an
6562 alias template. */
6564 static void
6565 check_template_keyword_in_nested_name_spec (tree name)
6567 if (CLASS_TYPE_P (name)
6568 && ((CLASSTYPE_USE_TEMPLATE (name)
6569 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6570 || CLASSTYPE_IS_TEMPLATE (name)))
6571 return;
6573 if (TREE_CODE (name) == TYPENAME_TYPE
6574 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6575 return;
6576 /* Alias templates are also OK. */
6577 else if (alias_template_specialization_p (name, nt_opaque))
6578 return;
6580 permerror (input_location, TYPE_P (name)
6581 ? G_("%qT is not a template")
6582 : G_("%qD is not a template"),
6583 name);
6586 /* Parse an (optional) nested-name-specifier.
6588 nested-name-specifier: [C++98]
6589 class-or-namespace-name :: nested-name-specifier [opt]
6590 class-or-namespace-name :: template nested-name-specifier [opt]
6592 nested-name-specifier: [C++0x]
6593 type-name ::
6594 namespace-name ::
6595 nested-name-specifier identifier ::
6596 nested-name-specifier template [opt] simple-template-id ::
6598 PARSER->SCOPE should be set appropriately before this function is
6599 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6600 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6601 in name lookups.
6603 Sets PARSER->SCOPE to the class (TYPE) or namespace
6604 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6605 it unchanged if there is no nested-name-specifier. Returns the new
6606 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6608 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6610 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6611 part of a declaration and/or decl-specifier. */
6613 static tree
6614 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6615 bool typename_keyword_p,
6616 bool check_dependency_p,
6617 bool type_p,
6618 bool is_declaration,
6619 bool template_keyword_p /* = false */)
6621 bool success = false;
6622 cp_token_position start = 0;
6623 cp_token *token;
6625 /* Remember where the nested-name-specifier starts. */
6626 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6627 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6629 start = cp_lexer_token_position (parser->lexer, false);
6630 push_deferring_access_checks (dk_deferred);
6633 while (true)
6635 tree new_scope;
6636 tree old_scope;
6637 tree saved_qualifying_scope;
6639 /* Spot cases that cannot be the beginning of a
6640 nested-name-specifier. */
6641 token = cp_lexer_peek_token (parser->lexer);
6643 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6644 the already parsed nested-name-specifier. */
6645 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6647 /* Grab the nested-name-specifier and continue the loop. */
6648 cp_parser_pre_parsed_nested_name_specifier (parser);
6649 /* If we originally encountered this nested-name-specifier
6650 with CHECK_DEPENDENCY_P set to true, we will not have
6651 resolved TYPENAME_TYPEs, so we must do so here. */
6652 if (is_declaration
6653 && !check_dependency_p
6654 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6656 new_scope = resolve_typename_type (parser->scope,
6657 /*only_current_p=*/false);
6658 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6659 parser->scope = new_scope;
6661 success = true;
6662 continue;
6665 /* Spot cases that cannot be the beginning of a
6666 nested-name-specifier. On the second and subsequent times
6667 through the loop, we look for the `template' keyword. */
6668 if (success && token->keyword == RID_TEMPLATE)
6670 /* A template-id can start a nested-name-specifier. */
6671 else if (token->type == CPP_TEMPLATE_ID)
6673 /* DR 743: decltype can be used in a nested-name-specifier. */
6674 else if (token_is_decltype (token))
6676 else
6678 /* If the next token is not an identifier, then it is
6679 definitely not a type-name or namespace-name. */
6680 if (token->type != CPP_NAME)
6681 break;
6682 /* If the following token is neither a `<' (to begin a
6683 template-id), nor a `::', then we are not looking at a
6684 nested-name-specifier. */
6685 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6687 if (token->type == CPP_COLON
6688 && parser->colon_corrects_to_scope_p
6689 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6690 /* name:name is a valid sequence in an Objective C message. */
6691 && !parser->objective_c_message_context_p)
6693 gcc_rich_location richloc (token->location);
6694 richloc.add_fixit_replace ("::");
6695 error_at (&richloc,
6696 "found %<:%> in nested-name-specifier, "
6697 "expected %<::%>");
6698 token->type = CPP_SCOPE;
6701 if (token->type != CPP_SCOPE
6702 && !cp_parser_nth_token_starts_template_argument_list_p
6703 (parser, 2))
6704 break;
6707 /* The nested-name-specifier is optional, so we parse
6708 tentatively. */
6709 cp_parser_parse_tentatively (parser);
6711 /* Look for the optional `template' keyword, if this isn't the
6712 first time through the loop. */
6713 if (success)
6715 template_keyword_p = cp_parser_optional_template_keyword (parser);
6716 /* DR1710: "In a qualified-id used as the name in
6717 a typename-specifier, elaborated-type-specifier, using-declaration,
6718 or class-or-decltype, an optional keyword template appearing at
6719 the top level is ignored." */
6720 if (!template_keyword_p
6721 && typename_keyword_p
6722 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6723 template_keyword_p = true;
6726 /* Save the old scope since the name lookup we are about to do
6727 might destroy it. */
6728 old_scope = parser->scope;
6729 saved_qualifying_scope = parser->qualifying_scope;
6730 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6731 look up names in "X<T>::I" in order to determine that "Y" is
6732 a template. So, if we have a typename at this point, we make
6733 an effort to look through it. */
6734 if (is_declaration
6735 && !check_dependency_p
6736 && !typename_keyword_p
6737 && parser->scope
6738 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6739 parser->scope = resolve_typename_type (parser->scope,
6740 /*only_current_p=*/false);
6741 /* Parse the qualifying entity. */
6742 new_scope
6743 = cp_parser_qualifying_entity (parser,
6744 typename_keyword_p,
6745 template_keyword_p,
6746 check_dependency_p,
6747 type_p,
6748 is_declaration);
6749 /* Look for the `::' token. */
6750 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6752 /* If we found what we wanted, we keep going; otherwise, we're
6753 done. */
6754 if (!cp_parser_parse_definitely (parser))
6756 bool error_p = false;
6758 /* Restore the OLD_SCOPE since it was valid before the
6759 failed attempt at finding the last
6760 class-or-namespace-name. */
6761 parser->scope = old_scope;
6762 parser->qualifying_scope = saved_qualifying_scope;
6764 /* If the next token is a decltype, and the one after that is a
6765 `::', then the decltype has failed to resolve to a class or
6766 enumeration type. Give this error even when parsing
6767 tentatively since it can't possibly be valid--and we're going
6768 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6769 won't get another chance.*/
6770 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6771 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6772 == CPP_SCOPE))
6774 token = cp_lexer_consume_token (parser->lexer);
6775 tree dtype = token->u.tree_check_value->value;
6776 if (dtype != error_mark_node)
6777 error_at (token->location, "%<decltype%> evaluates to %qT, "
6778 "which is not a class or enumeration type",
6779 dtype);
6780 parser->scope = error_mark_node;
6781 error_p = true;
6782 /* As below. */
6783 success = true;
6784 cp_lexer_consume_token (parser->lexer);
6787 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6788 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6790 /* If we have a non-type template-id followed by ::, it can't
6791 possibly be valid. */
6792 token = cp_lexer_peek_token (parser->lexer);
6793 tree tid = token->u.tree_check_value->value;
6794 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6795 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6797 tree tmpl = NULL_TREE;
6798 if (is_overloaded_fn (tid))
6800 tree fns = get_fns (tid);
6801 if (OVL_SINGLE_P (fns))
6802 tmpl = OVL_FIRST (fns);
6803 if (function_concept_p (fns))
6804 error_at (token->location, "concept-id %qD "
6805 "in nested-name-specifier", tid);
6806 else
6807 error_at (token->location, "function template-id "
6808 "%qD in nested-name-specifier", tid);
6810 else
6812 tmpl = TREE_OPERAND (tid, 0);
6813 if (variable_concept_p (tmpl)
6814 || standard_concept_p (tmpl))
6815 error_at (token->location, "concept-id %qD "
6816 "in nested-name-specifier", tid);
6817 else
6819 /* Variable template. */
6820 gcc_assert (variable_template_p (tmpl));
6821 error_at (token->location, "variable template-id "
6822 "%qD in nested-name-specifier", tid);
6825 if (tmpl)
6826 inform (DECL_SOURCE_LOCATION (tmpl),
6827 "%qD declared here", tmpl);
6829 parser->scope = error_mark_node;
6830 error_p = true;
6831 /* As below. */
6832 success = true;
6833 cp_lexer_consume_token (parser->lexer);
6834 cp_lexer_consume_token (parser->lexer);
6838 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6839 break;
6840 /* If the next token is an identifier, and the one after
6841 that is a `::', then any valid interpretation would have
6842 found a class-or-namespace-name. */
6843 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6844 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6845 == CPP_SCOPE)
6846 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6847 != CPP_COMPL))
6849 token = cp_lexer_consume_token (parser->lexer);
6850 if (!error_p)
6852 if (!token->error_reported)
6854 tree decl;
6855 tree ambiguous_decls;
6857 decl = cp_parser_lookup_name (parser, token->u.value,
6858 none_type,
6859 /*is_template=*/false,
6860 /*is_namespace=*/false,
6861 /*check_dependency=*/true,
6862 &ambiguous_decls,
6863 token->location);
6864 if (TREE_CODE (decl) == TEMPLATE_DECL)
6865 error_at (token->location,
6866 "%qD used without template arguments",
6867 decl);
6868 else if (ambiguous_decls)
6870 // cp_parser_lookup_name has the same diagnostic,
6871 // thus make sure to emit it at most once.
6872 if (cp_parser_uncommitted_to_tentative_parse_p
6873 (parser))
6875 error_at (token->location,
6876 "reference to %qD is ambiguous",
6877 token->u.value);
6878 print_candidates (ambiguous_decls);
6880 decl = error_mark_node;
6882 else
6884 if (cxx_dialect != cxx98)
6885 cp_parser_name_lookup_error
6886 (parser, token->u.value, decl, NLE_NOT_CXX98,
6887 token->location);
6888 else
6889 cp_parser_name_lookup_error
6890 (parser, token->u.value, decl, NLE_CXX98,
6891 token->location);
6894 parser->scope = error_mark_node;
6895 error_p = true;
6896 /* Treat this as a successful nested-name-specifier
6897 due to:
6899 [basic.lookup.qual]
6901 If the name found is not a class-name (clause
6902 _class_) or namespace-name (_namespace.def_), the
6903 program is ill-formed. */
6904 success = true;
6906 cp_lexer_consume_token (parser->lexer);
6908 break;
6910 /* We've found one valid nested-name-specifier. */
6911 success = true;
6912 /* Name lookup always gives us a DECL. */
6913 if (TREE_CODE (new_scope) == TYPE_DECL)
6914 new_scope = TREE_TYPE (new_scope);
6915 /* Uses of "template" must be followed by actual templates. */
6916 if (template_keyword_p)
6917 check_template_keyword_in_nested_name_spec (new_scope);
6918 /* If it is a class scope, try to complete it; we are about to
6919 be looking up names inside the class. */
6920 if (TYPE_P (new_scope)
6921 /* Since checking types for dependency can be expensive,
6922 avoid doing it if the type is already complete. */
6923 && !COMPLETE_TYPE_P (new_scope)
6924 /* Do not try to complete dependent types. */
6925 && !dependent_type_p (new_scope))
6927 new_scope = complete_type (new_scope);
6928 /* If it is a typedef to current class, use the current
6929 class instead, as the typedef won't have any names inside
6930 it yet. */
6931 if (!COMPLETE_TYPE_P (new_scope)
6932 && currently_open_class (new_scope))
6933 new_scope = TYPE_MAIN_VARIANT (new_scope);
6935 /* Make sure we look in the right scope the next time through
6936 the loop. */
6937 parser->scope = new_scope;
6940 /* If parsing tentatively, replace the sequence of tokens that makes
6941 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6942 token. That way, should we re-parse the token stream, we will
6943 not have to repeat the effort required to do the parse, nor will
6944 we issue duplicate error messages. */
6945 if (success && start)
6947 cp_token *token;
6949 token = cp_lexer_token_at (parser->lexer, start);
6950 /* Reset the contents of the START token. */
6951 token->type = CPP_NESTED_NAME_SPECIFIER;
6952 /* Retrieve any deferred checks. Do not pop this access checks yet
6953 so the memory will not be reclaimed during token replacing below. */
6954 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6955 token->tree_check_p = true;
6956 token->u.tree_check_value->value = parser->scope;
6957 token->u.tree_check_value->checks = get_deferred_access_checks ();
6958 token->u.tree_check_value->qualifying_scope =
6959 parser->qualifying_scope;
6960 token->keyword = RID_MAX;
6962 /* Purge all subsequent tokens. */
6963 cp_lexer_purge_tokens_after (parser->lexer, start);
6966 if (start)
6967 pop_to_parent_deferring_access_checks ();
6969 return success ? parser->scope : NULL_TREE;
6972 /* Parse a nested-name-specifier. See
6973 cp_parser_nested_name_specifier_opt for details. This function
6974 behaves identically, except that it will an issue an error if no
6975 nested-name-specifier is present. */
6977 static tree
6978 cp_parser_nested_name_specifier (cp_parser *parser,
6979 bool typename_keyword_p,
6980 bool check_dependency_p,
6981 bool type_p,
6982 bool is_declaration)
6984 tree scope;
6986 /* Look for the nested-name-specifier. */
6987 scope = cp_parser_nested_name_specifier_opt (parser,
6988 typename_keyword_p,
6989 check_dependency_p,
6990 type_p,
6991 is_declaration);
6992 /* If it was not present, issue an error message. */
6993 if (!scope)
6995 cp_parser_error (parser, "expected nested-name-specifier");
6996 parser->scope = NULL_TREE;
6999 return scope;
7002 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7003 this is either a class-name or a namespace-name (which corresponds
7004 to the class-or-namespace-name production in the grammar). For
7005 C++0x, it can also be a type-name that refers to an enumeration
7006 type or a simple-template-id.
7008 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7009 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7010 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7011 TYPE_P is TRUE iff the next name should be taken as a class-name,
7012 even the same name is declared to be another entity in the same
7013 scope.
7015 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7016 specified by the class-or-namespace-name. If neither is found the
7017 ERROR_MARK_NODE is returned. */
7019 static tree
7020 cp_parser_qualifying_entity (cp_parser *parser,
7021 bool typename_keyword_p,
7022 bool template_keyword_p,
7023 bool check_dependency_p,
7024 bool type_p,
7025 bool is_declaration)
7027 tree saved_scope;
7028 tree saved_qualifying_scope;
7029 tree saved_object_scope;
7030 tree scope;
7031 bool only_class_p;
7032 bool successful_parse_p;
7034 /* DR 743: decltype can appear in a nested-name-specifier. */
7035 if (cp_lexer_next_token_is_decltype (parser->lexer))
7037 scope = cp_parser_decltype (parser);
7038 if (TREE_CODE (scope) != ENUMERAL_TYPE
7039 && !MAYBE_CLASS_TYPE_P (scope))
7041 cp_parser_simulate_error (parser);
7042 return error_mark_node;
7044 if (TYPE_NAME (scope))
7045 scope = TYPE_NAME (scope);
7046 return scope;
7049 /* Before we try to parse the class-name, we must save away the
7050 current PARSER->SCOPE since cp_parser_class_name will destroy
7051 it. */
7052 saved_scope = parser->scope;
7053 saved_qualifying_scope = parser->qualifying_scope;
7054 saved_object_scope = parser->object_scope;
7055 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7056 there is no need to look for a namespace-name. */
7057 only_class_p = template_keyword_p
7058 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7059 if (!only_class_p)
7060 cp_parser_parse_tentatively (parser);
7061 scope = cp_parser_class_name (parser,
7062 typename_keyword_p,
7063 template_keyword_p,
7064 type_p ? class_type : none_type,
7065 check_dependency_p,
7066 /*class_head_p=*/false,
7067 is_declaration,
7068 /*enum_ok=*/cxx_dialect > cxx98);
7069 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7070 /* If that didn't work, try for a namespace-name. */
7071 if (!only_class_p && !successful_parse_p)
7073 /* Restore the saved scope. */
7074 parser->scope = saved_scope;
7075 parser->qualifying_scope = saved_qualifying_scope;
7076 parser->object_scope = saved_object_scope;
7077 /* If we are not looking at an identifier followed by the scope
7078 resolution operator, then this is not part of a
7079 nested-name-specifier. (Note that this function is only used
7080 to parse the components of a nested-name-specifier.) */
7081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7082 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7083 return error_mark_node;
7084 scope = cp_parser_namespace_name (parser);
7087 return scope;
7090 /* Return true if we are looking at a compound-literal, false otherwise. */
7092 static bool
7093 cp_parser_compound_literal_p (cp_parser *parser)
7095 cp_lexer_save_tokens (parser->lexer);
7097 /* Skip tokens until the next token is a closing parenthesis.
7098 If we find the closing `)', and the next token is a `{', then
7099 we are looking at a compound-literal. */
7100 bool compound_literal_p
7101 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7102 /*consume_paren=*/true)
7103 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7105 /* Roll back the tokens we skipped. */
7106 cp_lexer_rollback_tokens (parser->lexer);
7108 return compound_literal_p;
7111 /* Return true if EXPR is the integer constant zero or a complex constant
7112 of zero, without any folding, but ignoring location wrappers. */
7114 bool
7115 literal_integer_zerop (const_tree expr)
7117 return (location_wrapper_p (expr)
7118 && integer_zerop (TREE_OPERAND (expr, 0)));
7121 /* Parse a postfix-expression.
7123 postfix-expression:
7124 primary-expression
7125 postfix-expression [ expression ]
7126 postfix-expression ( expression-list [opt] )
7127 simple-type-specifier ( expression-list [opt] )
7128 typename :: [opt] nested-name-specifier identifier
7129 ( expression-list [opt] )
7130 typename :: [opt] nested-name-specifier template [opt] template-id
7131 ( expression-list [opt] )
7132 postfix-expression . template [opt] id-expression
7133 postfix-expression -> template [opt] id-expression
7134 postfix-expression . pseudo-destructor-name
7135 postfix-expression -> pseudo-destructor-name
7136 postfix-expression ++
7137 postfix-expression --
7138 dynamic_cast < type-id > ( expression )
7139 static_cast < type-id > ( expression )
7140 reinterpret_cast < type-id > ( expression )
7141 const_cast < type-id > ( expression )
7142 typeid ( expression )
7143 typeid ( type-id )
7145 GNU Extension:
7147 postfix-expression:
7148 ( type-id ) { initializer-list , [opt] }
7150 This extension is a GNU version of the C99 compound-literal
7151 construct. (The C99 grammar uses `type-name' instead of `type-id',
7152 but they are essentially the same concept.)
7154 If ADDRESS_P is true, the postfix expression is the operand of the
7155 `&' operator. CAST_P is true if this expression is the target of a
7156 cast.
7158 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7159 class member access expressions [expr.ref].
7161 Returns a representation of the expression. */
7163 static cp_expr
7164 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7165 bool member_access_only_p, bool decltype_p,
7166 cp_id_kind * pidk_return)
7168 cp_token *token;
7169 location_t loc;
7170 enum rid keyword;
7171 cp_id_kind idk = CP_ID_KIND_NONE;
7172 cp_expr postfix_expression = NULL_TREE;
7173 bool is_member_access = false;
7175 /* Peek at the next token. */
7176 token = cp_lexer_peek_token (parser->lexer);
7177 loc = token->location;
7178 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7180 /* Some of the productions are determined by keywords. */
7181 keyword = token->keyword;
7182 switch (keyword)
7184 case RID_DYNCAST:
7185 case RID_STATCAST:
7186 case RID_REINTCAST:
7187 case RID_CONSTCAST:
7189 tree type;
7190 cp_expr expression;
7191 const char *saved_message;
7192 bool saved_in_type_id_in_expr_p;
7194 /* All of these can be handled in the same way from the point
7195 of view of parsing. Begin by consuming the token
7196 identifying the cast. */
7197 cp_lexer_consume_token (parser->lexer);
7199 /* New types cannot be defined in the cast. */
7200 saved_message = parser->type_definition_forbidden_message;
7201 parser->type_definition_forbidden_message
7202 = G_("types may not be defined in casts");
7204 /* Look for the opening `<'. */
7205 cp_parser_require (parser, CPP_LESS, RT_LESS);
7206 /* Parse the type to which we are casting. */
7207 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7208 parser->in_type_id_in_expr_p = true;
7209 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7210 NULL);
7211 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7212 /* Look for the closing `>'. */
7213 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7214 /* Restore the old message. */
7215 parser->type_definition_forbidden_message = saved_message;
7217 bool saved_greater_than_is_operator_p
7218 = parser->greater_than_is_operator_p;
7219 parser->greater_than_is_operator_p = true;
7221 /* And the expression which is being cast. */
7222 matching_parens parens;
7223 parens.require_open (parser);
7224 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7225 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7226 RT_CLOSE_PAREN);
7227 location_t end_loc = close_paren ?
7228 close_paren->location : UNKNOWN_LOCATION;
7230 parser->greater_than_is_operator_p
7231 = saved_greater_than_is_operator_p;
7233 /* Only type conversions to integral or enumeration types
7234 can be used in constant-expressions. */
7235 if (!cast_valid_in_integral_constant_expression_p (type)
7236 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7238 postfix_expression = error_mark_node;
7239 break;
7242 /* Construct a location e.g. :
7243 reinterpret_cast <int *> (expr)
7244 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7245 ranging from the start of the "*_cast" token to the final closing
7246 paren, with the caret at the start. */
7247 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7249 switch (keyword)
7251 case RID_DYNCAST:
7252 postfix_expression
7253 = build_dynamic_cast (cp_cast_loc, type, expression,
7254 tf_warning_or_error);
7255 break;
7256 case RID_STATCAST:
7257 postfix_expression
7258 = build_static_cast (cp_cast_loc, type, expression,
7259 tf_warning_or_error);
7260 break;
7261 case RID_REINTCAST:
7262 postfix_expression
7263 = build_reinterpret_cast (cp_cast_loc, type, expression,
7264 tf_warning_or_error);
7265 break;
7266 case RID_CONSTCAST:
7267 postfix_expression
7268 = build_const_cast (cp_cast_loc, type, expression,
7269 tf_warning_or_error);
7270 break;
7271 default:
7272 gcc_unreachable ();
7275 break;
7277 case RID_TYPEID:
7279 tree type;
7280 const char *saved_message;
7281 bool saved_in_type_id_in_expr_p;
7283 /* Consume the `typeid' token. */
7284 cp_lexer_consume_token (parser->lexer);
7285 /* Look for the `(' token. */
7286 matching_parens parens;
7287 parens.require_open (parser);
7288 /* Types cannot be defined in a `typeid' expression. */
7289 saved_message = parser->type_definition_forbidden_message;
7290 parser->type_definition_forbidden_message
7291 = G_("types may not be defined in a %<typeid%> expression");
7292 /* We can't be sure yet whether we're looking at a type-id or an
7293 expression. */
7294 cp_parser_parse_tentatively (parser);
7295 /* Try a type-id first. */
7296 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7297 parser->in_type_id_in_expr_p = true;
7298 type = cp_parser_type_id (parser);
7299 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7300 /* Look for the `)' token. Otherwise, we can't be sure that
7301 we're not looking at an expression: consider `typeid (int
7302 (3))', for example. */
7303 cp_token *close_paren = parens.require_close (parser);
7304 /* If all went well, simply lookup the type-id. */
7305 if (cp_parser_parse_definitely (parser))
7306 postfix_expression = get_typeid (type, tf_warning_or_error);
7307 /* Otherwise, fall back to the expression variant. */
7308 else
7310 tree expression;
7312 /* Look for an expression. */
7313 expression = cp_parser_expression (parser, & idk);
7314 /* Compute its typeid. */
7315 postfix_expression = build_typeid (expression, tf_warning_or_error);
7316 /* Look for the `)' token. */
7317 close_paren = parens.require_close (parser);
7319 /* Restore the saved message. */
7320 parser->type_definition_forbidden_message = saved_message;
7321 /* `typeid' may not appear in an integral constant expression. */
7322 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7323 postfix_expression = error_mark_node;
7325 /* Construct a location e.g. :
7326 typeid (expr)
7327 ^~~~~~~~~~~~~
7328 ranging from the start of the "typeid" token to the final closing
7329 paren, with the caret at the start. */
7330 if (close_paren)
7332 location_t typeid_loc
7333 = make_location (start_loc, start_loc, close_paren->location);
7334 postfix_expression.set_location (typeid_loc);
7335 postfix_expression.maybe_add_location_wrapper ();
7338 break;
7340 case RID_TYPENAME:
7342 tree type;
7343 /* The syntax permitted here is the same permitted for an
7344 elaborated-type-specifier. */
7345 ++parser->prevent_constrained_type_specifiers;
7346 type = cp_parser_elaborated_type_specifier (parser,
7347 /*is_friend=*/false,
7348 /*is_declaration=*/false);
7349 --parser->prevent_constrained_type_specifiers;
7350 postfix_expression = cp_parser_functional_cast (parser, type);
7352 break;
7354 case RID_ADDRESSOF:
7355 case RID_BUILTIN_SHUFFLE:
7356 case RID_BUILTIN_SHUFFLEVECTOR:
7357 case RID_BUILTIN_LAUNDER:
7358 case RID_BUILTIN_ASSOC_BARRIER:
7360 vec<tree, va_gc> *vec;
7362 cp_lexer_consume_token (parser->lexer);
7363 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7364 /*cast_p=*/false, /*allow_expansion_p=*/true,
7365 /*non_constant_p=*/NULL);
7366 if (vec == NULL)
7368 postfix_expression = error_mark_node;
7369 break;
7372 for (tree p : *vec)
7373 mark_exp_read (p);
7375 switch (keyword)
7377 case RID_ADDRESSOF:
7378 if (vec->length () == 1)
7379 postfix_expression
7380 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7381 else
7383 error_at (loc, "wrong number of arguments to "
7384 "%<__builtin_addressof%>");
7385 postfix_expression = error_mark_node;
7387 break;
7389 case RID_BUILTIN_LAUNDER:
7390 if (vec->length () == 1)
7391 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7392 tf_warning_or_error);
7393 else
7395 error_at (loc, "wrong number of arguments to "
7396 "%<__builtin_launder%>");
7397 postfix_expression = error_mark_node;
7399 break;
7401 case RID_BUILTIN_ASSOC_BARRIER:
7402 if (vec->length () == 1)
7403 postfix_expression = build1_loc (loc, PAREN_EXPR,
7404 TREE_TYPE ((*vec)[0]),
7405 (*vec)[0]);
7406 else
7408 error_at (loc, "wrong number of arguments to "
7409 "%<__builtin_assoc_barrier%>");
7410 postfix_expression = error_mark_node;
7412 break;
7414 case RID_BUILTIN_SHUFFLE:
7415 if (vec->length () == 2)
7416 postfix_expression
7417 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7418 (*vec)[1], tf_warning_or_error);
7419 else if (vec->length () == 3)
7420 postfix_expression
7421 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7422 (*vec)[2], tf_warning_or_error);
7423 else
7425 error_at (loc, "wrong number of arguments to "
7426 "%<__builtin_shuffle%>");
7427 postfix_expression = error_mark_node;
7429 break;
7431 case RID_BUILTIN_SHUFFLEVECTOR:
7432 if (vec->length () < 3)
7434 error_at (loc, "wrong number of arguments to "
7435 "%<__builtin_shufflevector%>");
7436 postfix_expression = error_mark_node;
7438 else
7440 postfix_expression
7441 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7443 break;
7445 default:
7446 gcc_unreachable ();
7448 break;
7451 case RID_BUILTIN_CONVERTVECTOR:
7453 tree expression;
7454 tree type;
7455 /* Consume the `__builtin_convertvector' token. */
7456 cp_lexer_consume_token (parser->lexer);
7457 /* Look for the opening `('. */
7458 matching_parens parens;
7459 parens.require_open (parser);
7460 /* Now, parse the assignment-expression. */
7461 expression = cp_parser_assignment_expression (parser);
7462 /* Look for the `,'. */
7463 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7464 location_t type_location
7465 = cp_lexer_peek_token (parser->lexer)->location;
7466 /* Parse the type-id. */
7468 type_id_in_expr_sentinel s (parser);
7469 type = cp_parser_type_id (parser);
7471 /* Look for the closing `)'. */
7472 parens.require_close (parser);
7473 return cp_build_vec_convert (expression, type_location, type,
7474 tf_warning_or_error);
7477 case RID_BUILTIN_BIT_CAST:
7479 tree expression;
7480 tree type;
7481 /* Consume the `__builtin_bit_cast' token. */
7482 cp_lexer_consume_token (parser->lexer);
7483 /* Look for the opening `('. */
7484 matching_parens parens;
7485 parens.require_open (parser);
7486 location_t type_location
7487 = cp_lexer_peek_token (parser->lexer)->location;
7488 /* Parse the type-id. */
7490 type_id_in_expr_sentinel s (parser);
7491 type = cp_parser_type_id (parser);
7493 /* Look for the `,'. */
7494 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7495 /* Now, parse the assignment-expression. */
7496 expression = cp_parser_assignment_expression (parser);
7497 /* Look for the closing `)'. */
7498 parens.require_close (parser);
7499 return cp_build_bit_cast (type_location, type, expression,
7500 tf_warning_or_error);
7503 default:
7505 tree type;
7507 /* If the next thing is a simple-type-specifier, we may be
7508 looking at a functional cast. We could also be looking at
7509 an id-expression. So, we try the functional cast, and if
7510 that doesn't work we fall back to the primary-expression. */
7511 cp_parser_parse_tentatively (parser);
7512 /* Look for the simple-type-specifier. */
7513 ++parser->prevent_constrained_type_specifiers;
7514 type = cp_parser_simple_type_specifier (parser,
7515 /*decl_specs=*/NULL,
7516 CP_PARSER_FLAGS_NONE);
7517 --parser->prevent_constrained_type_specifiers;
7518 /* Parse the cast itself. */
7519 if (!cp_parser_error_occurred (parser))
7520 postfix_expression
7521 = cp_parser_functional_cast (parser, type);
7522 /* If that worked, we're done. */
7523 if (cp_parser_parse_definitely (parser))
7524 break;
7526 /* If the functional-cast didn't work out, try a
7527 compound-literal. */
7528 if (cp_parser_allow_gnu_extensions_p (parser)
7529 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7531 cp_expr initializer = NULL_TREE;
7533 cp_parser_parse_tentatively (parser);
7535 matching_parens parens;
7536 parens.consume_open (parser);
7538 /* Avoid calling cp_parser_type_id pointlessly, see comment
7539 in cp_parser_cast_expression about c++/29234. */
7540 if (!cp_parser_compound_literal_p (parser))
7541 cp_parser_simulate_error (parser);
7542 else
7544 /* Parse the type. */
7545 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7546 parser->in_type_id_in_expr_p = true;
7547 type = cp_parser_type_id (parser);
7548 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7549 parens.require_close (parser);
7552 /* If things aren't going well, there's no need to
7553 keep going. */
7554 if (!cp_parser_error_occurred (parser))
7556 bool non_constant_p;
7557 /* Parse the brace-enclosed initializer list. */
7558 initializer = cp_parser_braced_list (parser,
7559 &non_constant_p);
7561 /* If that worked, we're definitely looking at a
7562 compound-literal expression. */
7563 if (cp_parser_parse_definitely (parser))
7565 /* Warn the user that a compound literal is not
7566 allowed in standard C++. */
7567 pedwarn (input_location, OPT_Wpedantic,
7568 "ISO C++ forbids compound-literals");
7569 /* For simplicity, we disallow compound literals in
7570 constant-expressions. We could
7571 allow compound literals of integer type, whose
7572 initializer was a constant, in constant
7573 expressions. Permitting that usage, as a further
7574 extension, would not change the meaning of any
7575 currently accepted programs. (Of course, as
7576 compound literals are not part of ISO C++, the
7577 standard has nothing to say.) */
7578 if (cp_parser_non_integral_constant_expression (parser,
7579 NIC_NCC))
7581 postfix_expression = error_mark_node;
7582 break;
7584 /* Form the representation of the compound-literal. */
7585 postfix_expression
7586 = finish_compound_literal (type, initializer,
7587 tf_warning_or_error, fcl_c99);
7588 postfix_expression.set_location (initializer.get_location ());
7589 break;
7593 /* It must be a primary-expression. */
7594 postfix_expression
7595 = cp_parser_primary_expression (parser, address_p, cast_p,
7596 /*template_arg_p=*/false,
7597 decltype_p,
7598 &idk);
7600 break;
7603 /* Note that we don't need to worry about calling build_cplus_new on a
7604 class-valued CALL_EXPR in decltype when it isn't the end of the
7605 postfix-expression; unary_complex_lvalue will take care of that for
7606 all these cases. */
7608 /* Keep looping until the postfix-expression is complete. */
7609 while (true)
7611 if (idk == CP_ID_KIND_UNQUALIFIED
7612 && identifier_p (postfix_expression)
7613 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7614 /* It is not a Koenig lookup function call. */
7615 postfix_expression
7616 = unqualified_name_lookup_error (postfix_expression);
7618 /* Peek at the next token. */
7619 token = cp_lexer_peek_token (parser->lexer);
7621 switch (token->type)
7623 case CPP_OPEN_SQUARE:
7624 if (cp_next_tokens_can_be_std_attribute_p (parser))
7626 cp_parser_error (parser,
7627 "two consecutive %<[%> shall "
7628 "only introduce an attribute");
7629 return error_mark_node;
7631 postfix_expression
7632 = cp_parser_postfix_open_square_expression (parser,
7633 postfix_expression,
7634 false,
7635 decltype_p);
7636 postfix_expression.set_range (start_loc,
7637 postfix_expression.get_location ());
7639 idk = CP_ID_KIND_NONE;
7640 is_member_access = false;
7641 break;
7643 case CPP_OPEN_PAREN:
7644 /* postfix-expression ( expression-list [opt] ) */
7646 bool koenig_p;
7647 bool is_builtin_constant_p;
7648 bool saved_integral_constant_expression_p = false;
7649 bool saved_non_integral_constant_expression_p = false;
7650 tsubst_flags_t complain = complain_flags (decltype_p);
7651 vec<tree, va_gc> *args;
7652 location_t close_paren_loc = UNKNOWN_LOCATION;
7653 location_t combined_loc = UNKNOWN_LOCATION;
7655 is_member_access = false;
7657 tree stripped_expression
7658 = tree_strip_any_location_wrapper (postfix_expression);
7659 is_builtin_constant_p
7660 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7661 if (is_builtin_constant_p)
7663 /* The whole point of __builtin_constant_p is to allow
7664 non-constant expressions to appear as arguments. */
7665 saved_integral_constant_expression_p
7666 = parser->integral_constant_expression_p;
7667 saved_non_integral_constant_expression_p
7668 = parser->non_integral_constant_expression_p;
7669 parser->integral_constant_expression_p = false;
7671 args = (cp_parser_parenthesized_expression_list
7672 (parser, non_attr,
7673 /*cast_p=*/false, /*allow_expansion_p=*/true,
7674 /*non_constant_p=*/NULL,
7675 /*close_paren_loc=*/&close_paren_loc,
7676 /*wrap_locations_p=*/true));
7677 if (is_builtin_constant_p)
7679 parser->integral_constant_expression_p
7680 = saved_integral_constant_expression_p;
7681 parser->non_integral_constant_expression_p
7682 = saved_non_integral_constant_expression_p;
7685 if (args == NULL)
7687 postfix_expression = error_mark_node;
7688 break;
7691 /* Function calls are not permitted in
7692 constant-expressions. */
7693 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7694 && cp_parser_non_integral_constant_expression (parser,
7695 NIC_FUNC_CALL))
7697 postfix_expression = error_mark_node;
7698 release_tree_vector (args);
7699 break;
7702 koenig_p = false;
7703 if (idk == CP_ID_KIND_UNQUALIFIED
7704 || idk == CP_ID_KIND_TEMPLATE_ID)
7706 if (identifier_p (postfix_expression)
7707 /* In C++20, we may need to perform ADL for a template
7708 name. */
7709 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7710 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7712 if (!args->is_empty ())
7714 koenig_p = true;
7715 if (!any_type_dependent_arguments_p (args))
7716 postfix_expression
7717 = perform_koenig_lookup (postfix_expression, args,
7718 complain);
7720 else
7721 postfix_expression
7722 = unqualified_fn_lookup_error (postfix_expression);
7724 /* We do not perform argument-dependent lookup if
7725 normal lookup finds a non-function, in accordance
7726 with the expected resolution of DR 218. */
7727 else if (!args->is_empty ()
7728 && is_overloaded_fn (postfix_expression))
7730 /* Do not do argument dependent lookup if regular
7731 lookup finds a member function or a block-scope
7732 function declaration. [basic.lookup.argdep]/3 */
7733 bool do_adl_p = true;
7734 tree fns = get_fns (postfix_expression);
7735 for (lkp_iterator iter (fns); iter; ++iter)
7737 tree fn = STRIP_TEMPLATE (*iter);
7738 if ((TREE_CODE (fn) == USING_DECL
7739 && DECL_DEPENDENT_P (fn))
7740 || DECL_FUNCTION_MEMBER_P (fn)
7741 || DECL_LOCAL_DECL_P (fn))
7743 do_adl_p = false;
7744 break;
7748 if (do_adl_p)
7750 koenig_p = true;
7751 if (!any_type_dependent_arguments_p (args))
7752 postfix_expression
7753 = perform_koenig_lookup (postfix_expression, args,
7754 complain);
7759 /* Temporarily set input_location to the combined location
7760 with call expression range, as e.g. build_out_target_exprs
7761 called from convert_default_arg relies on input_location,
7762 so updating it only when the call is fully built results
7763 in inconsistencies between location handling in templates
7764 and outside of templates. */
7765 if (close_paren_loc != UNKNOWN_LOCATION)
7766 combined_loc = make_location (token->location, start_loc,
7767 close_paren_loc);
7768 iloc_sentinel ils (combined_loc);
7770 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7772 tree instance = TREE_OPERAND (postfix_expression, 0);
7773 tree fn = TREE_OPERAND (postfix_expression, 1);
7775 if (processing_template_decl
7776 && (type_dependent_object_expression_p (instance)
7777 || (!BASELINK_P (fn)
7778 && TREE_CODE (fn) != FIELD_DECL)
7779 || type_dependent_expression_p (fn)
7780 || any_type_dependent_arguments_p (args)))
7782 maybe_generic_this_capture (instance, fn);
7783 postfix_expression
7784 = build_min_nt_call_vec (postfix_expression, args);
7786 else if (BASELINK_P (fn))
7788 postfix_expression
7789 = (build_new_method_call
7790 (instance, fn, &args, NULL_TREE,
7791 (idk == CP_ID_KIND_QUALIFIED
7792 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7793 : LOOKUP_NORMAL),
7794 /*fn_p=*/NULL,
7795 complain));
7797 else
7798 postfix_expression
7799 = finish_call_expr (postfix_expression, &args,
7800 /*disallow_virtual=*/false,
7801 /*koenig_p=*/false,
7802 complain);
7804 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7805 || TREE_CODE (postfix_expression) == MEMBER_REF
7806 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7807 postfix_expression = (build_offset_ref_call_from_tree
7808 (postfix_expression, &args,
7809 complain));
7810 else if (idk == CP_ID_KIND_QUALIFIED)
7811 /* A call to a static class member, or a namespace-scope
7812 function. */
7813 postfix_expression
7814 = finish_call_expr (postfix_expression, &args,
7815 /*disallow_virtual=*/true,
7816 koenig_p,
7817 complain);
7818 else
7819 /* All other function calls. */
7820 postfix_expression
7821 = finish_call_expr (postfix_expression, &args,
7822 /*disallow_virtual=*/false,
7823 koenig_p,
7824 complain);
7826 if (close_paren_loc != UNKNOWN_LOCATION)
7827 postfix_expression.set_location (combined_loc);
7829 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7830 idk = CP_ID_KIND_NONE;
7832 release_tree_vector (args);
7834 break;
7836 case CPP_DOT:
7837 case CPP_DEREF:
7838 /* postfix-expression . template [opt] id-expression
7839 postfix-expression . pseudo-destructor-name
7840 postfix-expression -> template [opt] id-expression
7841 postfix-expression -> pseudo-destructor-name */
7843 /* Consume the `.' or `->' operator. */
7844 cp_lexer_consume_token (parser->lexer);
7846 postfix_expression
7847 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7848 postfix_expression,
7849 false, &idk, loc);
7851 is_member_access = true;
7852 break;
7854 case CPP_PLUS_PLUS:
7855 /* postfix-expression ++ */
7856 /* Consume the `++' token. */
7857 cp_lexer_consume_token (parser->lexer);
7858 /* Generate a representation for the complete expression. */
7859 postfix_expression
7860 = finish_increment_expr (postfix_expression,
7861 POSTINCREMENT_EXPR);
7862 /* Increments may not appear in constant-expressions. */
7863 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7864 postfix_expression = error_mark_node;
7865 idk = CP_ID_KIND_NONE;
7866 is_member_access = false;
7867 break;
7869 case CPP_MINUS_MINUS:
7870 /* postfix-expression -- */
7871 /* Consume the `--' token. */
7872 cp_lexer_consume_token (parser->lexer);
7873 /* Generate a representation for the complete expression. */
7874 postfix_expression
7875 = finish_increment_expr (postfix_expression,
7876 POSTDECREMENT_EXPR);
7877 /* Decrements may not appear in constant-expressions. */
7878 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7879 postfix_expression = error_mark_node;
7880 idk = CP_ID_KIND_NONE;
7881 is_member_access = false;
7882 break;
7884 default:
7885 if (pidk_return != NULL)
7886 * pidk_return = idk;
7887 if (member_access_only_p)
7888 return is_member_access
7889 ? postfix_expression
7890 : cp_expr (error_mark_node);
7891 else
7892 return postfix_expression;
7896 /* We should never get here. */
7897 gcc_unreachable ();
7898 return error_mark_node;
7901 /* Helper function for cp_parser_parenthesized_expression_list and
7902 cp_parser_postfix_open_square_expression. Parse a single element
7903 of parenthesized expression list. */
7905 static cp_expr
7906 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
7907 bool allow_expansion_p,
7908 bool fold_expr_p,
7909 bool *non_constant_p)
7911 cp_expr expr (NULL_TREE);
7912 bool expr_non_constant_p;
7914 /* Parse the next assignment-expression. */
7915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7917 /* A braced-init-list. */
7918 cp_lexer_set_source_position (parser->lexer);
7919 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7920 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7921 if (non_constant_p && expr_non_constant_p)
7922 *non_constant_p = true;
7924 else if (non_constant_p)
7926 expr = cp_parser_constant_expression (parser,
7927 /*allow_non_constant_p=*/true,
7928 &expr_non_constant_p);
7929 if (expr_non_constant_p)
7930 *non_constant_p = true;
7932 else
7933 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
7935 if (fold_expr_p)
7936 expr = instantiate_non_dependent_expr (expr);
7938 /* If we have an ellipsis, then this is an expression expansion. */
7939 if (allow_expansion_p
7940 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7942 /* Consume the `...'. */
7943 cp_lexer_consume_token (parser->lexer);
7945 /* Build the argument pack. */
7946 expr = make_pack_expansion (expr);
7948 return expr;
7951 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7952 by cp_parser_builtin_offsetof. We're looking for
7954 postfix-expression [ expression ]
7955 postfix-expression [ braced-init-list ] (C++11)
7956 postfix-expression [ expression-list[opt] ] (C++23)
7958 FOR_OFFSETOF is set if we're being called in that context, which
7959 changes how we deal with integer constant expressions. */
7961 static tree
7962 cp_parser_postfix_open_square_expression (cp_parser *parser,
7963 tree postfix_expression,
7964 bool for_offsetof,
7965 bool decltype_p)
7967 tree index = NULL_TREE;
7968 releasing_vec expression_list = NULL;
7969 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7970 bool saved_greater_than_is_operator_p;
7972 /* Consume the `[' token. */
7973 cp_lexer_consume_token (parser->lexer);
7975 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7976 parser->greater_than_is_operator_p = true;
7978 /* Parse the index expression. */
7979 /* ??? For offsetof, there is a question of what to allow here. If
7980 offsetof is not being used in an integral constant expression context,
7981 then we *could* get the right answer by computing the value at runtime.
7982 If we are in an integral constant expression context, then we might
7983 could accept any constant expression; hard to say without analysis.
7984 Rather than open the barn door too wide right away, allow only integer
7985 constant expressions here. */
7986 if (for_offsetof)
7987 index = cp_parser_constant_expression (parser);
7988 else
7990 if (cxx_dialect >= cxx23
7991 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
7992 *&expression_list = make_tree_vector ();
7993 else if (cxx_dialect >= cxx23)
7995 while (true)
7997 cp_expr expr
7998 = cp_parser_parenthesized_expression_list_elt (parser,
7999 /*cast_p=*/
8000 false,
8001 /*allow_exp_p=*/
8002 true,
8003 /*fold_expr_p=*/
8004 false,
8005 /*non_cst_p=*/
8006 NULL);
8008 if (expr == error_mark_node)
8009 index = error_mark_node;
8010 else if (expression_list.get () == NULL
8011 && !PACK_EXPANSION_P (expr.get_value ()))
8012 index = expr.get_value ();
8013 else
8014 vec_safe_push (expression_list, expr.get_value ());
8016 /* If the next token isn't a `,', then we are done. */
8017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8018 break;
8020 if (expression_list.get () == NULL && index != error_mark_node)
8022 *&expression_list = make_tree_vector_single (index);
8023 index = NULL_TREE;
8026 /* Otherwise, consume the `,' and keep going. */
8027 cp_lexer_consume_token (parser->lexer);
8029 if (expression_list.get () && index == error_mark_node)
8030 expression_list.release ();
8032 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8034 bool expr_nonconst_p;
8035 cp_lexer_set_source_position (parser->lexer);
8036 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8037 index = cp_parser_braced_list (parser, &expr_nonconst_p);
8039 else
8040 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8041 /*decltype_p=*/false,
8042 /*warn_comma_p=*/warn_comma_subscript);
8045 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8047 /* Look for the closing `]'. */
8048 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8050 /* Build the ARRAY_REF. */
8051 postfix_expression = grok_array_decl (loc, postfix_expression,
8052 index, &expression_list,
8053 tf_warning_or_error
8054 | (decltype_p ? tf_decltype : 0));
8056 /* When not doing offsetof, array references are not permitted in
8057 constant-expressions. */
8058 if (!for_offsetof
8059 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8060 postfix_expression = error_mark_node;
8062 return postfix_expression;
8065 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8066 dereference of incomplete type, returns true if error_mark_node should
8067 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8068 and *DEPENDENT_P. */
8070 bool
8071 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8072 bool *dependent_p)
8074 /* In a template, be permissive by treating an object expression
8075 of incomplete type as dependent (after a pedwarn). */
8076 diagnostic_t kind = (processing_template_decl
8077 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8079 switch (TREE_CODE (*postfix_expression))
8081 case CAST_EXPR:
8082 case REINTERPRET_CAST_EXPR:
8083 case CONST_CAST_EXPR:
8084 case STATIC_CAST_EXPR:
8085 case DYNAMIC_CAST_EXPR:
8086 case IMPLICIT_CONV_EXPR:
8087 case VIEW_CONVERT_EXPR:
8088 case NON_LVALUE_EXPR:
8089 kind = DK_ERROR;
8090 break;
8091 case OVERLOAD:
8092 /* Don't emit any diagnostic for OVERLOADs. */
8093 kind = DK_IGNORED;
8094 break;
8095 default:
8096 /* Avoid clobbering e.g. DECLs. */
8097 if (!EXPR_P (*postfix_expression))
8098 kind = DK_ERROR;
8099 break;
8102 if (kind == DK_IGNORED)
8103 return false;
8105 location_t exploc = location_of (*postfix_expression);
8106 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8107 if (!MAYBE_CLASS_TYPE_P (*scope))
8108 return true;
8109 if (kind == DK_ERROR)
8110 *scope = *postfix_expression = error_mark_node;
8111 else if (processing_template_decl)
8113 *dependent_p = true;
8114 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8116 return false;
8119 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8120 by cp_parser_builtin_offsetof. We're looking for
8122 postfix-expression . template [opt] id-expression
8123 postfix-expression . pseudo-destructor-name
8124 postfix-expression -> template [opt] id-expression
8125 postfix-expression -> pseudo-destructor-name
8127 FOR_OFFSETOF is set if we're being called in that context. That sorta
8128 limits what of the above we'll actually accept, but nevermind.
8129 TOKEN_TYPE is the "." or "->" token, which will already have been
8130 removed from the stream. */
8132 static tree
8133 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8134 enum cpp_ttype token_type,
8135 cp_expr postfix_expression,
8136 bool for_offsetof, cp_id_kind *idk,
8137 location_t location)
8139 tree name;
8140 bool dependent_p;
8141 bool pseudo_destructor_p;
8142 tree scope = NULL_TREE;
8143 location_t start_loc = postfix_expression.get_start ();
8145 /* If this is a `->' operator, dereference the pointer. */
8146 if (token_type == CPP_DEREF)
8147 postfix_expression = build_x_arrow (location, postfix_expression,
8148 tf_warning_or_error);
8149 /* Check to see whether or not the expression is type-dependent and
8150 not the current instantiation. */
8151 dependent_p = type_dependent_object_expression_p (postfix_expression);
8152 /* The identifier following the `->' or `.' is not qualified. */
8153 parser->scope = NULL_TREE;
8154 parser->qualifying_scope = NULL_TREE;
8155 parser->object_scope = NULL_TREE;
8156 *idk = CP_ID_KIND_NONE;
8158 /* Enter the scope corresponding to the type of the object
8159 given by the POSTFIX_EXPRESSION. */
8160 if (!dependent_p)
8162 scope = TREE_TYPE (postfix_expression);
8163 /* According to the standard, no expression should ever have
8164 reference type. Unfortunately, we do not currently match
8165 the standard in this respect in that our internal representation
8166 of an expression may have reference type even when the standard
8167 says it does not. Therefore, we have to manually obtain the
8168 underlying type here. */
8169 scope = non_reference (scope);
8170 /* The type of the POSTFIX_EXPRESSION must be complete. */
8171 /* Unlike the object expression in other contexts, *this is not
8172 required to be of complete type for purposes of class member
8173 access (5.2.5) outside the member function body. */
8174 if (postfix_expression != current_class_ref
8175 && scope != error_mark_node
8176 && !currently_open_class (scope))
8178 scope = complete_type (scope);
8179 if (!COMPLETE_TYPE_P (scope)
8180 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8181 &dependent_p))
8182 return error_mark_node;
8185 if (!dependent_p)
8187 /* Let the name lookup machinery know that we are processing a
8188 class member access expression. */
8189 parser->context->object_type = scope;
8190 /* If something went wrong, we want to be able to discern that case,
8191 as opposed to the case where there was no SCOPE due to the type
8192 of expression being dependent. */
8193 if (!scope)
8194 scope = error_mark_node;
8195 /* If the SCOPE was erroneous, make the various semantic analysis
8196 functions exit quickly -- and without issuing additional error
8197 messages. */
8198 if (scope == error_mark_node)
8199 postfix_expression = error_mark_node;
8203 if (dependent_p)
8205 tree type = TREE_TYPE (postfix_expression);
8206 /* If we don't have a (type-dependent) object of class type, use
8207 typeof to figure out the type of the object. */
8208 if (type == NULL_TREE)
8209 type = finish_typeof (postfix_expression);
8210 parser->context->object_type = type;
8213 /* Assume this expression is not a pseudo-destructor access. */
8214 pseudo_destructor_p = false;
8216 /* If the SCOPE is a scalar type, then, if this is a valid program,
8217 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8218 is type dependent, it can be pseudo-destructor-name or something else.
8219 Try to parse it as pseudo-destructor-name first. */
8220 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8222 tree s;
8223 tree type;
8225 cp_parser_parse_tentatively (parser);
8226 /* Parse the pseudo-destructor-name. */
8227 s = NULL_TREE;
8228 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8229 &s, &type);
8230 if (dependent_p
8231 && (cp_parser_error_occurred (parser)
8232 || !SCALAR_TYPE_P (type)))
8233 cp_parser_abort_tentative_parse (parser);
8234 else if (cp_parser_parse_definitely (parser))
8236 pseudo_destructor_p = true;
8237 postfix_expression
8238 = finish_pseudo_destructor_expr (postfix_expression,
8239 s, type, location);
8243 if (!pseudo_destructor_p)
8245 /* If the SCOPE is not a scalar type, we are looking at an
8246 ordinary class member access expression, rather than a
8247 pseudo-destructor-name. */
8248 bool template_p;
8249 cp_token *token = cp_lexer_peek_token (parser->lexer);
8250 /* Parse the id-expression. */
8251 name = (cp_parser_id_expression
8252 (parser,
8253 cp_parser_optional_template_keyword (parser),
8254 /*check_dependency_p=*/true,
8255 &template_p,
8256 /*declarator_p=*/false,
8257 /*optional_p=*/false));
8258 /* In general, build a SCOPE_REF if the member name is qualified.
8259 However, if the name was not dependent and has already been
8260 resolved; there is no need to build the SCOPE_REF. For example;
8262 struct X { void f(); };
8263 template <typename T> void f(T* t) { t->X::f(); }
8265 Even though "t" is dependent, "X::f" is not and has been resolved
8266 to a BASELINK; there is no need to include scope information. */
8268 /* But we do need to remember that there was an explicit scope for
8269 virtual function calls. */
8270 if (parser->scope)
8271 *idk = CP_ID_KIND_QUALIFIED;
8273 /* If the name is a template-id that names a type, we will get a
8274 TYPE_DECL here. That is invalid code. */
8275 if (TREE_CODE (name) == TYPE_DECL)
8277 error_at (token->location, "invalid use of %qD", name);
8278 postfix_expression = error_mark_node;
8280 else
8282 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8284 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8286 error_at (token->location, "%<%D::%D%> is not a class member",
8287 parser->scope, name);
8288 postfix_expression = error_mark_node;
8290 else
8291 name = build_qualified_name (/*type=*/NULL_TREE,
8292 parser->scope,
8293 name,
8294 template_p);
8295 parser->scope = NULL_TREE;
8296 parser->qualifying_scope = NULL_TREE;
8297 parser->object_scope = NULL_TREE;
8299 if (parser->scope && name && BASELINK_P (name))
8300 adjust_result_of_qualified_name_lookup
8301 (name, parser->scope, scope);
8302 postfix_expression
8303 = finish_class_member_access_expr (postfix_expression, name,
8304 template_p,
8305 tf_warning_or_error);
8306 /* Build a location e.g.:
8307 ptr->access_expr
8308 ~~~^~~~~~~~~~~~~
8309 where the caret is at the deref token, ranging from
8310 the start of postfix_expression to the end of the access expr. */
8311 location_t combined_loc
8312 = make_location (input_location, start_loc, parser->lexer);
8313 protected_set_expr_location (postfix_expression, combined_loc);
8317 /* We no longer need to look up names in the scope of the object on
8318 the left-hand side of the `.' or `->' operator. */
8319 parser->context->object_type = NULL_TREE;
8321 /* Outside of offsetof, these operators may not appear in
8322 constant-expressions. */
8323 if (!for_offsetof
8324 && (cp_parser_non_integral_constant_expression
8325 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8326 postfix_expression = error_mark_node;
8328 return postfix_expression;
8331 /* Parse a parenthesized expression-list.
8333 expression-list:
8334 assignment-expression
8335 expression-list, assignment-expression
8337 attribute-list:
8338 expression-list
8339 identifier
8340 identifier, expression-list
8342 CAST_P is true if this expression is the target of a cast.
8344 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8345 argument pack.
8347 WRAP_LOCATIONS_P is true if expressions within this list for which
8348 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8349 their source locations.
8351 Returns a vector of trees. Each element is a representation of an
8352 assignment-expression. NULL is returned if the ( and or ) are
8353 missing. An empty, but allocated, vector is returned on no
8354 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8355 if we are parsing an attribute list for an attribute that wants a
8356 plain identifier argument, normal_attr for an attribute that wants
8357 an expression, or non_attr if we aren't parsing an attribute list. If
8358 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8359 not all of the expressions in the list were constant.
8360 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8361 will be written to with the location of the closing parenthesis. If
8362 an error occurs, it may or may not be written to. */
8364 static vec<tree, va_gc> *
8365 cp_parser_parenthesized_expression_list (cp_parser* parser,
8366 int is_attribute_list,
8367 bool cast_p,
8368 bool allow_expansion_p,
8369 bool *non_constant_p,
8370 location_t *close_paren_loc,
8371 bool wrap_locations_p)
8373 vec<tree, va_gc> *expression_list;
8374 bool fold_expr_p = is_attribute_list != non_attr;
8375 tree identifier = NULL_TREE;
8376 bool saved_greater_than_is_operator_p;
8378 /* Assume all the expressions will be constant. */
8379 if (non_constant_p)
8380 *non_constant_p = false;
8382 matching_parens parens;
8383 if (!parens.require_open (parser))
8384 return NULL;
8386 expression_list = make_tree_vector ();
8388 /* Within a parenthesized expression, a `>' token is always
8389 the greater-than operator. */
8390 saved_greater_than_is_operator_p
8391 = parser->greater_than_is_operator_p;
8392 parser->greater_than_is_operator_p = true;
8394 cp_expr expr (NULL_TREE);
8396 /* Consume expressions until there are no more. */
8397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8398 while (true)
8400 /* At the beginning of attribute lists, check to see if the
8401 next token is an identifier. */
8402 if (is_attribute_list == id_attr
8403 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8405 cp_token *token;
8407 /* Consume the identifier. */
8408 token = cp_lexer_consume_token (parser->lexer);
8409 /* Save the identifier. */
8410 identifier = token->u.value;
8412 else
8414 expr
8415 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8416 allow_expansion_p,
8417 fold_expr_p,
8418 non_constant_p);
8420 if (wrap_locations_p)
8421 expr.maybe_add_location_wrapper ();
8423 /* Add it to the list. We add error_mark_node
8424 expressions to the list, so that we can still tell if
8425 the correct form for a parenthesized expression-list
8426 is found. That gives better errors. */
8427 vec_safe_push (expression_list, expr.get_value ());
8429 if (expr == error_mark_node)
8430 goto skip_comma;
8433 /* After the first item, attribute lists look the same as
8434 expression lists. */
8435 is_attribute_list = non_attr;
8437 get_comma:;
8438 /* If the next token isn't a `,', then we are done. */
8439 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8440 break;
8442 /* Otherwise, consume the `,' and keep going. */
8443 cp_lexer_consume_token (parser->lexer);
8446 if (close_paren_loc)
8447 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8449 if (!parens.require_close (parser))
8451 int ending;
8453 skip_comma:;
8454 /* We try and resync to an unnested comma, as that will give the
8455 user better diagnostics. */
8456 ending = cp_parser_skip_to_closing_parenthesis (parser,
8457 /*recovering=*/true,
8458 /*or_comma=*/true,
8459 /*consume_paren=*/true);
8460 if (ending < 0)
8461 goto get_comma;
8462 if (!ending)
8464 parser->greater_than_is_operator_p
8465 = saved_greater_than_is_operator_p;
8466 return NULL;
8470 parser->greater_than_is_operator_p
8471 = saved_greater_than_is_operator_p;
8473 if (identifier)
8474 vec_safe_insert (expression_list, 0, identifier);
8476 return expression_list;
8479 /* Parse a pseudo-destructor-name.
8481 pseudo-destructor-name:
8482 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8483 :: [opt] nested-name-specifier template template-id :: ~ type-name
8484 :: [opt] nested-name-specifier [opt] ~ type-name
8486 If either of the first two productions is used, sets *SCOPE to the
8487 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8488 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8489 or ERROR_MARK_NODE if the parse fails. */
8491 static void
8492 cp_parser_pseudo_destructor_name (cp_parser* parser,
8493 tree object,
8494 tree* scope,
8495 tree* type)
8497 bool nested_name_specifier_p;
8499 /* Handle ~auto. */
8500 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8501 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8502 && !type_dependent_expression_p (object))
8504 if (cxx_dialect < cxx14)
8505 pedwarn (input_location, OPT_Wc__14_extensions,
8506 "%<~auto%> only available with "
8507 "%<-std=c++14%> or %<-std=gnu++14%>");
8508 cp_lexer_consume_token (parser->lexer);
8509 cp_lexer_consume_token (parser->lexer);
8510 *scope = NULL_TREE;
8511 *type = TREE_TYPE (object);
8512 return;
8515 /* Assume that things will not work out. */
8516 *type = error_mark_node;
8518 /* Look for the optional `::' operator. */
8519 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8520 /* Look for the optional nested-name-specifier. */
8521 nested_name_specifier_p
8522 = (cp_parser_nested_name_specifier_opt (parser,
8523 /*typename_keyword_p=*/false,
8524 /*check_dependency_p=*/true,
8525 /*type_p=*/false,
8526 /*is_declaration=*/false)
8527 != NULL_TREE);
8528 /* Now, if we saw a nested-name-specifier, we might be doing the
8529 second production. */
8530 if (nested_name_specifier_p
8531 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8533 /* Consume the `template' keyword. */
8534 cp_lexer_consume_token (parser->lexer);
8535 /* Parse the template-id. */
8536 cp_parser_template_id (parser,
8537 /*template_keyword_p=*/true,
8538 /*check_dependency_p=*/false,
8539 class_type,
8540 /*is_declaration=*/true);
8541 /* Look for the `::' token. */
8542 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8544 /* If the next token is not a `~', then there might be some
8545 additional qualification. */
8546 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8548 /* At this point, we're looking for "type-name :: ~". The type-name
8549 must not be a class-name, since this is a pseudo-destructor. So,
8550 it must be either an enum-name, or a typedef-name -- both of which
8551 are just identifiers. So, we peek ahead to check that the "::"
8552 and "~" tokens are present; if they are not, then we can avoid
8553 calling type_name. */
8554 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8555 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8556 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8558 cp_parser_error (parser, "non-scalar type");
8559 return;
8562 /* Look for the type-name. */
8563 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8564 if (*scope == error_mark_node)
8565 return;
8567 /* Look for the `::' token. */
8568 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8570 else
8571 *scope = NULL_TREE;
8573 /* Look for the `~'. */
8574 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8576 /* Once we see the ~, this has to be a pseudo-destructor. */
8577 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8578 cp_parser_commit_to_topmost_tentative_parse (parser);
8580 /* Look for the type-name again. We are not responsible for
8581 checking that it matches the first type-name. */
8582 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8585 /* Parse a unary-expression.
8587 unary-expression:
8588 postfix-expression
8589 ++ cast-expression
8590 -- cast-expression
8591 await-expression
8592 unary-operator cast-expression
8593 sizeof unary-expression
8594 sizeof ( type-id )
8595 alignof ( type-id ) [C++0x]
8596 new-expression
8597 delete-expression
8599 GNU Extensions:
8601 unary-expression:
8602 __extension__ cast-expression
8603 __alignof__ unary-expression
8604 __alignof__ ( type-id )
8605 alignof unary-expression [C++0x]
8606 __real__ cast-expression
8607 __imag__ cast-expression
8608 && identifier
8609 sizeof ( type-id ) { initializer-list , [opt] }
8610 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8611 __alignof__ ( type-id ) { initializer-list , [opt] }
8613 ADDRESS_P is true iff the unary-expression is appearing as the
8614 operand of the `&' operator. CAST_P is true if this expression is
8615 the target of a cast.
8617 Returns a representation of the expression. */
8619 static cp_expr
8620 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8621 bool address_p, bool cast_p, bool decltype_p)
8623 cp_token *token;
8624 enum tree_code unary_operator;
8626 /* Peek at the next token. */
8627 token = cp_lexer_peek_token (parser->lexer);
8628 /* Some keywords give away the kind of expression. */
8629 if (token->type == CPP_KEYWORD)
8631 enum rid keyword = token->keyword;
8633 switch (keyword)
8635 case RID_ALIGNOF:
8636 case RID_SIZEOF:
8638 tree operand, ret;
8639 enum tree_code op;
8640 location_t start_loc = token->location;
8642 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8643 bool std_alignof = id_equal (token->u.value, "alignof");
8645 /* Consume the token. */
8646 cp_lexer_consume_token (parser->lexer);
8647 /* Parse the operand. */
8648 operand = cp_parser_sizeof_operand (parser, keyword);
8650 /* Construct a location e.g. :
8651 alignof (expr)
8652 ^~~~~~~~~~~~~~
8653 with start == caret at the start of the "alignof"/"sizeof"
8654 token, with the endpoint at the final closing paren. */
8655 location_t compound_loc
8656 = make_location (start_loc, start_loc, parser->lexer);
8658 if (TYPE_P (operand))
8659 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8660 std_alignof, true);
8661 else
8663 /* ISO C++ defines alignof only with types, not with
8664 expressions. So pedwarn if alignof is used with a non-
8665 type expression. However, __alignof__ is ok. */
8666 if (std_alignof)
8667 pedwarn (token->location, OPT_Wpedantic,
8668 "ISO C++ does not allow %<alignof%> "
8669 "with a non-type");
8671 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8672 std_alignof, true);
8674 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8675 SIZEOF_EXPR with the original operand. */
8676 if (op == SIZEOF_EXPR && ret != error_mark_node)
8678 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8680 if (!processing_template_decl && TYPE_P (operand))
8682 ret = build_min (SIZEOF_EXPR, size_type_node,
8683 build1 (NOP_EXPR, operand,
8684 error_mark_node));
8685 SIZEOF_EXPR_TYPE_P (ret) = 1;
8687 else
8688 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8689 TREE_SIDE_EFFECTS (ret) = 0;
8690 TREE_READONLY (ret) = 1;
8691 SET_EXPR_LOCATION (ret, compound_loc);
8695 cp_expr ret_expr (ret, compound_loc);
8696 ret_expr = ret_expr.maybe_add_location_wrapper ();
8697 return ret_expr;
8700 case RID_BUILTIN_HAS_ATTRIBUTE:
8701 return cp_parser_has_attribute_expression (parser);
8703 case RID_NEW:
8704 return cp_parser_new_expression (parser);
8706 case RID_DELETE:
8707 return cp_parser_delete_expression (parser);
8709 case RID_EXTENSION:
8711 /* The saved value of the PEDANTIC flag. */
8712 int saved_pedantic;
8713 tree expr;
8715 /* Save away the PEDANTIC flag. */
8716 cp_parser_extension_opt (parser, &saved_pedantic);
8717 /* Parse the cast-expression. */
8718 expr = cp_parser_simple_cast_expression (parser);
8719 /* Restore the PEDANTIC flag. */
8720 pedantic = saved_pedantic;
8722 return expr;
8725 case RID_REALPART:
8726 case RID_IMAGPART:
8728 tree expression;
8730 /* Consume the `__real__' or `__imag__' token. */
8731 cp_lexer_consume_token (parser->lexer);
8732 /* Parse the cast-expression. */
8733 expression = cp_parser_simple_cast_expression (parser);
8734 /* Create the complete representation. */
8735 return build_x_unary_op (token->location,
8736 (keyword == RID_REALPART
8737 ? REALPART_EXPR : IMAGPART_EXPR),
8738 expression,
8739 tf_warning_or_error);
8741 break;
8743 case RID_TRANSACTION_ATOMIC:
8744 case RID_TRANSACTION_RELAXED:
8745 return cp_parser_transaction_expression (parser, keyword);
8747 case RID_NOEXCEPT:
8749 tree expr;
8750 const char *saved_message;
8751 bool saved_integral_constant_expression_p;
8752 bool saved_non_integral_constant_expression_p;
8753 bool saved_greater_than_is_operator_p;
8755 location_t start_loc = token->location;
8757 cp_lexer_consume_token (parser->lexer);
8758 matching_parens parens;
8759 parens.require_open (parser);
8761 saved_message = parser->type_definition_forbidden_message;
8762 parser->type_definition_forbidden_message
8763 = G_("types may not be defined in %<noexcept%> expressions");
8765 saved_integral_constant_expression_p
8766 = parser->integral_constant_expression_p;
8767 saved_non_integral_constant_expression_p
8768 = parser->non_integral_constant_expression_p;
8769 parser->integral_constant_expression_p = false;
8771 saved_greater_than_is_operator_p
8772 = parser->greater_than_is_operator_p;
8773 parser->greater_than_is_operator_p = true;
8775 ++cp_unevaluated_operand;
8776 ++c_inhibit_evaluation_warnings;
8777 ++cp_noexcept_operand;
8778 expr = cp_parser_expression (parser);
8779 --cp_noexcept_operand;
8780 --c_inhibit_evaluation_warnings;
8781 --cp_unevaluated_operand;
8783 parser->greater_than_is_operator_p
8784 = saved_greater_than_is_operator_p;
8786 parser->integral_constant_expression_p
8787 = saved_integral_constant_expression_p;
8788 parser->non_integral_constant_expression_p
8789 = saved_non_integral_constant_expression_p;
8791 parser->type_definition_forbidden_message = saved_message;
8793 parens.require_close (parser);
8795 /* Construct a location of the form:
8796 noexcept (expr)
8797 ^~~~~~~~~~~~~~~
8798 with start == caret, finishing at the close-paren. */
8799 location_t noexcept_loc
8800 = make_location (start_loc, start_loc, parser->lexer);
8802 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8803 noexcept_loc);
8806 case RID_CO_AWAIT:
8808 tree expr;
8809 location_t kw_loc = token->location;
8811 /* Consume the `co_await' token. */
8812 cp_lexer_consume_token (parser->lexer);
8813 /* Parse its cast-expression. */
8814 expr = cp_parser_simple_cast_expression (parser);
8815 if (expr == error_mark_node)
8816 return error_mark_node;
8818 /* Handle [expr.await]. */
8819 return cp_expr (finish_co_await_expr (kw_loc, expr));
8822 default:
8823 break;
8827 /* Look for the `:: new' and `:: delete', which also signal the
8828 beginning of a new-expression, or delete-expression,
8829 respectively. If the next token is `::', then it might be one of
8830 these. */
8831 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8833 enum rid keyword;
8835 /* See if the token after the `::' is one of the keywords in
8836 which we're interested. */
8837 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8838 /* If it's `new', we have a new-expression. */
8839 if (keyword == RID_NEW)
8840 return cp_parser_new_expression (parser);
8841 /* Similarly, for `delete'. */
8842 else if (keyword == RID_DELETE)
8843 return cp_parser_delete_expression (parser);
8846 /* Look for a unary operator. */
8847 unary_operator = cp_parser_unary_operator (token);
8848 /* The `++' and `--' operators can be handled similarly, even though
8849 they are not technically unary-operators in the grammar. */
8850 if (unary_operator == ERROR_MARK)
8852 if (token->type == CPP_PLUS_PLUS)
8853 unary_operator = PREINCREMENT_EXPR;
8854 else if (token->type == CPP_MINUS_MINUS)
8855 unary_operator = PREDECREMENT_EXPR;
8856 /* Handle the GNU address-of-label extension. */
8857 else if (cp_parser_allow_gnu_extensions_p (parser)
8858 && token->type == CPP_AND_AND)
8860 tree identifier;
8861 tree expression;
8862 location_t start_loc = token->location;
8864 /* Consume the '&&' token. */
8865 cp_lexer_consume_token (parser->lexer);
8866 /* Look for the identifier. */
8867 identifier = cp_parser_identifier (parser);
8868 /* Construct a location of the form:
8869 &&label
8870 ^~~~~~~
8871 with caret==start at the "&&", finish at the end of the label. */
8872 location_t combined_loc
8873 = make_location (start_loc, start_loc, parser->lexer);
8874 /* Create an expression representing the address. */
8875 expression = finish_label_address_expr (identifier, combined_loc);
8876 if (cp_parser_non_integral_constant_expression (parser,
8877 NIC_ADDR_LABEL))
8878 expression = error_mark_node;
8879 return expression;
8882 if (unary_operator != ERROR_MARK)
8884 cp_expr cast_expression;
8885 cp_expr expression = error_mark_node;
8886 non_integral_constant non_constant_p = NIC_NONE;
8887 location_t loc = token->location;
8888 tsubst_flags_t complain = complain_flags (decltype_p);
8890 /* Consume the operator token. */
8891 token = cp_lexer_consume_token (parser->lexer);
8892 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8894 /* Parse the cast-expression. */
8895 cast_expression
8896 = cp_parser_cast_expression (parser,
8897 unary_operator == ADDR_EXPR,
8898 /*cast_p=*/false,
8899 /*decltype*/false,
8900 pidk);
8902 /* Make a location:
8903 OP_TOKEN CAST_EXPRESSION
8904 ^~~~~~~~~~~~~~~~~~~~~~~~~
8905 with start==caret at the operator token, and
8906 extending to the end of the cast_expression. */
8907 loc = make_location (loc, loc, cast_expression.get_finish ());
8909 /* Now, build an appropriate representation. */
8910 switch (unary_operator)
8912 case INDIRECT_REF:
8913 non_constant_p = NIC_STAR;
8914 expression = build_x_indirect_ref (loc, cast_expression,
8915 RO_UNARY_STAR,
8916 complain);
8917 /* TODO: build_x_indirect_ref does not always honor the
8918 location, so ensure it is set. */
8919 expression.set_location (loc);
8920 break;
8922 case ADDR_EXPR:
8923 non_constant_p = NIC_ADDR;
8924 /* Fall through. */
8925 case BIT_NOT_EXPR:
8926 expression = build_x_unary_op (loc, unary_operator,
8927 cast_expression,
8928 complain);
8929 /* TODO: build_x_unary_op does not always honor the location,
8930 so ensure it is set. */
8931 expression.set_location (loc);
8932 break;
8934 case PREINCREMENT_EXPR:
8935 case PREDECREMENT_EXPR:
8936 non_constant_p = unary_operator == PREINCREMENT_EXPR
8937 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8938 /* Fall through. */
8939 case NEGATE_EXPR:
8940 /* Immediately fold negation of a constant, unless the constant is 0
8941 (since -0 == 0) or it would overflow. */
8942 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8944 tree stripped_expr
8945 = tree_strip_any_location_wrapper (cast_expression);
8946 if (CONSTANT_CLASS_P (stripped_expr)
8947 && !integer_zerop (stripped_expr)
8948 && !TREE_OVERFLOW (stripped_expr))
8950 tree folded = fold_build1 (unary_operator,
8951 TREE_TYPE (stripped_expr),
8952 stripped_expr);
8953 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8955 expression = maybe_wrap_with_location (folded, loc);
8956 break;
8960 /* Fall through. */
8961 case UNARY_PLUS_EXPR:
8962 case TRUTH_NOT_EXPR:
8963 expression = finish_unary_op_expr (loc, unary_operator,
8964 cast_expression, complain);
8965 break;
8967 default:
8968 gcc_unreachable ();
8971 if (non_constant_p != NIC_NONE
8972 && cp_parser_non_integral_constant_expression (parser,
8973 non_constant_p))
8974 expression = error_mark_node;
8976 return expression;
8979 return cp_parser_postfix_expression (parser, address_p, cast_p,
8980 /*member_access_only_p=*/false,
8981 decltype_p,
8982 pidk);
8985 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8986 unary-operator, the corresponding tree code is returned. */
8988 static enum tree_code
8989 cp_parser_unary_operator (cp_token* token)
8991 switch (token->type)
8993 case CPP_MULT:
8994 return INDIRECT_REF;
8996 case CPP_AND:
8997 return ADDR_EXPR;
8999 case CPP_PLUS:
9000 return UNARY_PLUS_EXPR;
9002 case CPP_MINUS:
9003 return NEGATE_EXPR;
9005 case CPP_NOT:
9006 return TRUTH_NOT_EXPR;
9008 case CPP_COMPL:
9009 return BIT_NOT_EXPR;
9011 default:
9012 return ERROR_MARK;
9016 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9017 Returns a representation of the expression. */
9019 static tree
9020 cp_parser_has_attribute_expression (cp_parser *parser)
9022 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9024 /* Consume the __builtin_has_attribute token. */
9025 cp_lexer_consume_token (parser->lexer);
9027 matching_parens parens;
9028 if (!parens.require_open (parser))
9029 return error_mark_node;
9031 /* Types cannot be defined in a `sizeof' expression. Save away the
9032 old message. */
9033 const char *saved_message = parser->type_definition_forbidden_message;
9034 const char *saved_message_arg
9035 = parser->type_definition_forbidden_message_arg;
9036 parser->type_definition_forbidden_message
9037 = G_("types may not be defined in %qs expressions");
9038 parser->type_definition_forbidden_message_arg
9039 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9041 /* The restrictions on constant-expressions do not apply inside
9042 sizeof expressions. */
9043 bool saved_integral_constant_expression_p
9044 = parser->integral_constant_expression_p;
9045 bool saved_non_integral_constant_expression_p
9046 = parser->non_integral_constant_expression_p;
9047 parser->integral_constant_expression_p = false;
9049 /* Do not actually evaluate the expression. */
9050 ++cp_unevaluated_operand;
9051 ++c_inhibit_evaluation_warnings;
9053 tree oper = NULL_TREE;
9055 /* We can't be sure yet whether we're looking at a type-id or an
9056 expression. */
9057 cp_parser_parse_tentatively (parser);
9059 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9060 parser->in_type_id_in_expr_p = true;
9061 /* Look for the type-id. */
9062 oper = cp_parser_type_id (parser);
9063 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9065 cp_parser_parse_definitely (parser);
9067 /* If the type-id production did not work out, then we must be
9068 looking at an expression. */
9069 if (!oper || oper == error_mark_node)
9070 oper = cp_parser_assignment_expression (parser);
9072 STRIP_ANY_LOCATION_WRAPPER (oper);
9074 /* Go back to evaluating expressions. */
9075 --cp_unevaluated_operand;
9076 --c_inhibit_evaluation_warnings;
9078 /* And restore the old one. */
9079 parser->type_definition_forbidden_message = saved_message;
9080 parser->type_definition_forbidden_message_arg = saved_message_arg;
9081 parser->integral_constant_expression_p
9082 = saved_integral_constant_expression_p;
9083 parser->non_integral_constant_expression_p
9084 = saved_non_integral_constant_expression_p;
9086 /* Consume the comma if it's there. */
9087 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9089 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9090 /*consume_paren=*/true);
9091 return error_mark_node;
9094 /* Parse the attribute specification. */
9095 bool ret = false;
9096 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9097 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9099 if (oper == error_mark_node)
9100 /* Nothing. */;
9101 else if (processing_template_decl && uses_template_parms (oper))
9102 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9103 "not supported yet");
9104 else
9106 /* Fold constant expressions used in attributes first. */
9107 cp_check_const_attributes (attr);
9109 /* Finally, see if OPER has been declared with ATTR. */
9110 ret = has_attribute (atloc, oper, attr, default_conversion);
9113 parens.require_close (parser);
9115 else
9117 error_at (atloc, "expected identifier");
9118 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9121 /* Construct a location e.g. :
9122 __builtin_has_attribute (oper, attr)
9123 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9124 with start == caret at the start of the built-in token,
9125 and with the endpoint at the final closing paren. */
9126 location_t compound_loc
9127 = make_location (start_loc, start_loc, parser->lexer);
9129 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9130 ret_expr.set_location (compound_loc);
9131 ret_expr = ret_expr.maybe_add_location_wrapper ();
9132 return ret_expr;
9135 /* Parse a new-expression.
9137 new-expression:
9138 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9139 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9141 Returns a representation of the expression. */
9143 static tree
9144 cp_parser_new_expression (cp_parser* parser)
9146 bool global_scope_p;
9147 vec<tree, va_gc> *placement;
9148 tree type;
9149 vec<tree, va_gc> *initializer;
9150 tree nelts = NULL_TREE;
9151 tree ret;
9153 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9155 /* Look for the optional `::' operator. */
9156 global_scope_p
9157 = (cp_parser_global_scope_opt (parser,
9158 /*current_scope_valid_p=*/false)
9159 != NULL_TREE);
9160 /* Look for the `new' operator. */
9161 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9162 /* There's no easy way to tell a new-placement from the
9163 `( type-id )' construct. */
9164 cp_parser_parse_tentatively (parser);
9165 /* Look for a new-placement. */
9166 placement = cp_parser_new_placement (parser);
9167 /* If that didn't work out, there's no new-placement. */
9168 if (!cp_parser_parse_definitely (parser))
9170 if (placement != NULL)
9171 release_tree_vector (placement);
9172 placement = NULL;
9175 /* If the next token is a `(', then we have a parenthesized
9176 type-id. */
9177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9179 cp_token *token;
9180 const char *saved_message = parser->type_definition_forbidden_message;
9182 /* Consume the `('. */
9183 matching_parens parens;
9184 parens.consume_open (parser);
9186 /* Parse the type-id. */
9187 parser->type_definition_forbidden_message
9188 = G_("types may not be defined in a new-expression");
9190 type_id_in_expr_sentinel s (parser);
9191 type = cp_parser_type_id (parser);
9193 parser->type_definition_forbidden_message = saved_message;
9195 /* Look for the closing `)'. */
9196 parens.require_close (parser);
9197 token = cp_lexer_peek_token (parser->lexer);
9198 /* There should not be a direct-new-declarator in this production,
9199 but GCC used to allowed this, so we check and emit a sensible error
9200 message for this case. */
9201 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9203 error_at (token->location,
9204 "array bound forbidden after parenthesized type-id");
9205 inform (token->location,
9206 "try removing the parentheses around the type-id");
9207 cp_parser_direct_new_declarator (parser);
9210 /* Otherwise, there must be a new-type-id. */
9211 else
9212 type = cp_parser_new_type_id (parser, &nelts);
9214 /* If the next token is a `(' or '{', then we have a new-initializer. */
9215 cp_token *token = cp_lexer_peek_token (parser->lexer);
9216 if (token->type == CPP_OPEN_PAREN
9217 || token->type == CPP_OPEN_BRACE)
9218 initializer = cp_parser_new_initializer (parser);
9219 else
9220 initializer = NULL;
9222 /* A new-expression may not appear in an integral constant
9223 expression. */
9224 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9225 ret = error_mark_node;
9226 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9227 of a new-type-id or type-id of a new-expression, the new-expression shall
9228 contain a new-initializer of the form ( assignment-expression )".
9229 Additionally, consistently with the spirit of DR 1467, we want to accept
9230 'new auto { 2 }' too. */
9231 else if ((ret = type_uses_auto (type))
9232 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9233 && (vec_safe_length (initializer) != 1
9234 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9235 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9237 error_at (token->location,
9238 "initialization of new-expression for type %<auto%> "
9239 "requires exactly one element");
9240 ret = error_mark_node;
9242 else
9244 /* Construct a location e.g.:
9245 ptr = new int[100]
9246 ^~~~~~~~~~~~
9247 with caret == start at the start of the "new" token, and the end
9248 at the end of the final token we consumed. */
9249 location_t combined_loc = make_location (start_loc, start_loc,
9250 parser->lexer);
9251 /* Create a representation of the new-expression. */
9252 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9253 global_scope_p, tf_warning_or_error);
9256 if (placement != NULL)
9257 release_tree_vector (placement);
9258 if (initializer != NULL)
9259 release_tree_vector (initializer);
9261 return ret;
9264 /* Parse a new-placement.
9266 new-placement:
9267 ( expression-list )
9269 Returns the same representation as for an expression-list. */
9271 static vec<tree, va_gc> *
9272 cp_parser_new_placement (cp_parser* parser)
9274 vec<tree, va_gc> *expression_list;
9276 /* Parse the expression-list. */
9277 expression_list = (cp_parser_parenthesized_expression_list
9278 (parser, non_attr, /*cast_p=*/false,
9279 /*allow_expansion_p=*/true,
9280 /*non_constant_p=*/NULL));
9282 if (expression_list && expression_list->is_empty ())
9283 error ("expected expression-list or type-id");
9285 return expression_list;
9288 /* Parse a new-type-id.
9290 new-type-id:
9291 type-specifier-seq new-declarator [opt]
9293 Returns the TYPE allocated. If the new-type-id indicates an array
9294 type, *NELTS is set to the number of elements in the last array
9295 bound; the TYPE will not include the last array bound. */
9297 static tree
9298 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9300 cp_decl_specifier_seq type_specifier_seq;
9301 cp_declarator *new_declarator;
9302 cp_declarator *declarator;
9303 cp_declarator *outer_declarator;
9304 const char *saved_message;
9306 /* The type-specifier sequence must not contain type definitions.
9307 (It cannot contain declarations of new types either, but if they
9308 are not definitions we will catch that because they are not
9309 complete.) */
9310 saved_message = parser->type_definition_forbidden_message;
9311 parser->type_definition_forbidden_message
9312 = G_("types may not be defined in a new-type-id");
9313 /* Parse the type-specifier-seq. */
9314 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9315 /*is_declaration=*/false,
9316 /*is_trailing_return=*/false,
9317 &type_specifier_seq);
9318 /* Restore the old message. */
9319 parser->type_definition_forbidden_message = saved_message;
9321 if (type_specifier_seq.type == error_mark_node)
9322 return error_mark_node;
9324 /* Parse the new-declarator. */
9325 new_declarator = cp_parser_new_declarator_opt (parser);
9327 /* Determine the number of elements in the last array dimension, if
9328 any. */
9329 *nelts = NULL_TREE;
9330 /* Skip down to the last array dimension. */
9331 declarator = new_declarator;
9332 outer_declarator = NULL;
9333 while (declarator && (declarator->kind == cdk_pointer
9334 || declarator->kind == cdk_ptrmem))
9336 outer_declarator = declarator;
9337 declarator = declarator->declarator;
9339 while (declarator
9340 && declarator->kind == cdk_array
9341 && declarator->declarator
9342 && declarator->declarator->kind == cdk_array)
9344 outer_declarator = declarator;
9345 declarator = declarator->declarator;
9348 if (declarator && declarator->kind == cdk_array)
9350 *nelts = declarator->u.array.bounds;
9351 if (*nelts == error_mark_node)
9352 *nelts = integer_one_node;
9354 if (*nelts == NULL_TREE)
9355 /* Leave [] in the declarator. */;
9356 else if (outer_declarator)
9357 outer_declarator->declarator = declarator->declarator;
9358 else
9359 new_declarator = NULL;
9362 return groktypename (&type_specifier_seq, new_declarator, false);
9365 /* Parse an (optional) new-declarator.
9367 new-declarator:
9368 ptr-operator new-declarator [opt]
9369 direct-new-declarator
9371 Returns the declarator. */
9373 static cp_declarator *
9374 cp_parser_new_declarator_opt (cp_parser* parser)
9376 enum tree_code code;
9377 tree type, std_attributes = NULL_TREE;
9378 cp_cv_quals cv_quals;
9380 /* We don't know if there's a ptr-operator next, or not. */
9381 cp_parser_parse_tentatively (parser);
9382 /* Look for a ptr-operator. */
9383 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9384 /* If that worked, look for more new-declarators. */
9385 if (cp_parser_parse_definitely (parser))
9387 cp_declarator *declarator;
9389 /* Parse another optional declarator. */
9390 declarator = cp_parser_new_declarator_opt (parser);
9392 declarator = cp_parser_make_indirect_declarator
9393 (code, type, cv_quals, declarator, std_attributes);
9395 return declarator;
9398 /* If the next token is a `[', there is a direct-new-declarator. */
9399 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9400 return cp_parser_direct_new_declarator (parser);
9402 return NULL;
9405 /* Parse a direct-new-declarator.
9407 direct-new-declarator:
9408 [ expression ]
9409 direct-new-declarator [constant-expression]
9413 static cp_declarator *
9414 cp_parser_direct_new_declarator (cp_parser* parser)
9416 cp_declarator *declarator = NULL;
9417 bool first_p = true;
9419 while (true)
9421 tree expression;
9422 cp_token *token;
9424 /* Look for the opening `['. */
9425 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9427 token = cp_lexer_peek_token (parser->lexer);
9428 if (token->type == CPP_CLOSE_SQUARE && first_p)
9429 expression = NULL_TREE;
9430 else
9431 expression = cp_parser_expression (parser);
9432 /* The standard requires that the expression have integral
9433 type. DR 74 adds enumeration types. We believe that the
9434 real intent is that these expressions be handled like the
9435 expression in a `switch' condition, which also allows
9436 classes with a single conversion to integral or
9437 enumeration type. */
9438 if (expression && !processing_template_decl)
9440 expression
9441 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9442 expression,
9443 /*complain=*/true);
9444 if (!expression)
9446 error_at (token->location,
9447 "expression in new-declarator must have integral "
9448 "or enumeration type");
9449 expression = error_mark_node;
9453 /* Look for the closing `]'. */
9454 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9456 /* Add this bound to the declarator. */
9457 declarator = make_array_declarator (declarator, expression);
9459 /* If the next token is not a `[', then there are no more
9460 bounds. */
9461 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9462 break;
9463 first_p = false;
9466 return declarator;
9469 /* Parse a new-initializer.
9471 new-initializer:
9472 ( expression-list [opt] )
9473 braced-init-list
9475 Returns a representation of the expression-list. */
9477 static vec<tree, va_gc> *
9478 cp_parser_new_initializer (cp_parser* parser)
9480 vec<tree, va_gc> *expression_list;
9482 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9484 tree t;
9485 bool expr_non_constant_p;
9486 cp_lexer_set_source_position (parser->lexer);
9487 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9488 t = cp_parser_braced_list (parser, &expr_non_constant_p);
9489 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9490 expression_list = make_tree_vector_single (t);
9492 else
9493 expression_list = (cp_parser_parenthesized_expression_list
9494 (parser, non_attr, /*cast_p=*/false,
9495 /*allow_expansion_p=*/true,
9496 /*non_constant_p=*/NULL));
9498 return expression_list;
9501 /* Parse a delete-expression.
9503 delete-expression:
9504 :: [opt] delete cast-expression
9505 :: [opt] delete [ ] cast-expression
9507 Returns a representation of the expression. */
9509 static tree
9510 cp_parser_delete_expression (cp_parser* parser)
9512 bool global_scope_p;
9513 bool array_p;
9514 tree expression;
9515 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9517 /* Look for the optional `::' operator. */
9518 global_scope_p
9519 = (cp_parser_global_scope_opt (parser,
9520 /*current_scope_valid_p=*/false)
9521 != NULL_TREE);
9522 /* Look for the `delete' keyword. */
9523 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9524 /* See if the array syntax is in use. */
9525 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9527 /* Consume the `[' token. */
9528 cp_lexer_consume_token (parser->lexer);
9529 /* Look for the `]' token. */
9530 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9531 /* Remember that this is the `[]' construct. */
9532 array_p = true;
9534 else
9535 array_p = false;
9537 /* Parse the cast-expression. */
9538 expression = cp_parser_simple_cast_expression (parser);
9540 /* A delete-expression may not appear in an integral constant
9541 expression. */
9542 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9543 return error_mark_node;
9545 /* Construct a location e.g.:
9546 delete [ ] ptr
9547 ^~~~~~~~~~~~~~
9548 with caret == start at the start of the "delete" token, and
9549 the end at the end of the final token we consumed. */
9550 location_t combined_loc = make_location (start_loc, start_loc,
9551 parser->lexer);
9552 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9553 global_scope_p, tf_warning_or_error);
9555 return expression;
9558 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9559 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9560 0 otherwise. */
9562 static int
9563 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9565 cp_token *token = cp_lexer_peek_token (parser->lexer);
9566 switch (token->type)
9568 case CPP_COMMA:
9569 case CPP_SEMICOLON:
9570 case CPP_QUERY:
9571 case CPP_COLON:
9572 case CPP_CLOSE_SQUARE:
9573 case CPP_CLOSE_PAREN:
9574 case CPP_CLOSE_BRACE:
9575 case CPP_OPEN_BRACE:
9576 case CPP_DOT:
9577 case CPP_DOT_STAR:
9578 case CPP_DEREF:
9579 case CPP_DEREF_STAR:
9580 case CPP_DIV:
9581 case CPP_MOD:
9582 case CPP_LSHIFT:
9583 case CPP_RSHIFT:
9584 case CPP_LESS:
9585 case CPP_GREATER:
9586 case CPP_LESS_EQ:
9587 case CPP_GREATER_EQ:
9588 case CPP_EQ_EQ:
9589 case CPP_NOT_EQ:
9590 case CPP_EQ:
9591 case CPP_MULT_EQ:
9592 case CPP_DIV_EQ:
9593 case CPP_MOD_EQ:
9594 case CPP_PLUS_EQ:
9595 case CPP_MINUS_EQ:
9596 case CPP_RSHIFT_EQ:
9597 case CPP_LSHIFT_EQ:
9598 case CPP_AND_EQ:
9599 case CPP_XOR_EQ:
9600 case CPP_OR_EQ:
9601 case CPP_XOR:
9602 case CPP_OR:
9603 case CPP_OR_OR:
9604 case CPP_EOF:
9605 case CPP_ELLIPSIS:
9606 return 0;
9608 case CPP_OPEN_PAREN:
9609 /* In ((type ()) () the last () isn't a valid cast-expression,
9610 so the whole must be parsed as postfix-expression. */
9611 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9612 != CPP_CLOSE_PAREN;
9614 case CPP_OPEN_SQUARE:
9615 /* '[' may start a primary-expression in obj-c++ and in C++11,
9616 as a lambda-expression, eg, '(void)[]{}'. */
9617 if (cxx_dialect >= cxx11)
9618 return -1;
9619 return c_dialect_objc ();
9621 case CPP_PLUS_PLUS:
9622 case CPP_MINUS_MINUS:
9623 /* '++' and '--' may or may not start a cast-expression:
9625 struct T { void operator++(int); };
9626 void f() { (T())++; }
9630 int a;
9631 (int)++a; */
9632 return -1;
9634 default:
9635 return 1;
9639 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9640 in the order: const_cast, static_cast, reinterpret_cast.
9642 Don't suggest dynamic_cast.
9644 Return the first legal cast kind found, or NULL otherwise. */
9646 static const char *
9647 get_cast_suggestion (tree dst_type, tree orig_expr)
9649 tree trial;
9651 /* Reuse the parser logic by attempting to build the various kinds of
9652 cast, with "complain" disabled.
9653 Identify the first such cast that is valid. */
9655 /* Don't attempt to run such logic within template processing. */
9656 if (processing_template_decl)
9657 return NULL;
9659 /* First try const_cast. */
9660 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9661 if (trial != error_mark_node)
9662 return "const_cast";
9664 /* If that fails, try static_cast. */
9665 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9666 if (trial != error_mark_node)
9667 return "static_cast";
9669 /* Finally, try reinterpret_cast. */
9670 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9671 tf_none);
9672 if (trial != error_mark_node)
9673 return "reinterpret_cast";
9675 /* No such cast possible. */
9676 return NULL;
9679 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9680 suggesting how to convert a C-style cast of the form:
9682 (DST_TYPE)ORIG_EXPR
9684 to a C++-style cast.
9686 The primary range of RICHLOC is asssumed to be that of the original
9687 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9688 of the parens in the C-style cast. */
9690 static void
9691 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9692 location_t close_paren_loc, tree orig_expr,
9693 tree dst_type)
9695 /* This function is non-trivial, so bail out now if the warning isn't
9696 going to be emitted. */
9697 if (!warn_old_style_cast)
9698 return;
9700 /* Try to find a legal C++ cast, trying them in order:
9701 const_cast, static_cast, reinterpret_cast. */
9702 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9703 if (!cast_suggestion)
9704 return;
9706 /* Replace the open paren with "CAST_SUGGESTION<". */
9707 pretty_printer pp;
9708 pp_string (&pp, cast_suggestion);
9709 pp_less (&pp);
9710 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9712 /* Replace the close paren with "> (". */
9713 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9715 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9716 rich_loc->add_fixit_insert_after (")");
9720 /* Parse a cast-expression.
9722 cast-expression:
9723 unary-expression
9724 ( type-id ) cast-expression
9726 ADDRESS_P is true iff the unary-expression is appearing as the
9727 operand of the `&' operator. CAST_P is true if this expression is
9728 the target of a cast.
9730 Returns a representation of the expression. */
9732 static cp_expr
9733 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9734 bool decltype_p, cp_id_kind * pidk)
9736 /* If it's a `(', then we might be looking at a cast. */
9737 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9739 tree type = NULL_TREE;
9740 cp_expr expr (NULL_TREE);
9741 int cast_expression = 0;
9742 const char *saved_message;
9744 /* There's no way to know yet whether or not this is a cast.
9745 For example, `(int (3))' is a unary-expression, while `(int)
9746 3' is a cast. So, we resort to parsing tentatively. */
9747 cp_parser_parse_tentatively (parser);
9748 /* Types may not be defined in a cast. */
9749 saved_message = parser->type_definition_forbidden_message;
9750 parser->type_definition_forbidden_message
9751 = G_("types may not be defined in casts");
9752 /* Consume the `('. */
9753 matching_parens parens;
9754 cp_token *open_paren = parens.consume_open (parser);
9755 location_t open_paren_loc = open_paren->location;
9756 location_t close_paren_loc = UNKNOWN_LOCATION;
9758 /* A very tricky bit is that `(struct S) { 3 }' is a
9759 compound-literal (which we permit in C++ as an extension).
9760 But, that construct is not a cast-expression -- it is a
9761 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9762 is legal; if the compound-literal were a cast-expression,
9763 you'd need an extra set of parentheses.) But, if we parse
9764 the type-id, and it happens to be a class-specifier, then we
9765 will commit to the parse at that point, because we cannot
9766 undo the action that is done when creating a new class. So,
9767 then we cannot back up and do a postfix-expression.
9769 Another tricky case is the following (c++/29234):
9771 struct S { void operator () (); };
9773 void foo ()
9775 ( S()() );
9778 As a type-id we parse the parenthesized S()() as a function
9779 returning a function, groktypename complains and we cannot
9780 back up in this case either.
9782 Therefore, we scan ahead to the closing `)', and check to see
9783 if the tokens after the `)' can start a cast-expression. Otherwise
9784 we are dealing with an unary-expression, a postfix-expression
9785 or something else.
9787 Yet another tricky case, in C++11, is the following (c++/54891):
9789 (void)[]{};
9791 The issue is that usually, besides the case of lambda-expressions,
9792 the parenthesized type-id cannot be followed by '[', and, eg, we
9793 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9794 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9795 we don't commit, we try a cast-expression, then an unary-expression.
9797 Save tokens so that we can put them back. */
9798 cp_lexer_save_tokens (parser->lexer);
9800 /* We may be looking at a cast-expression. */
9801 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9802 /*consume_paren=*/true))
9803 cast_expression
9804 = cp_parser_tokens_start_cast_expression (parser);
9806 /* Roll back the tokens we skipped. */
9807 cp_lexer_rollback_tokens (parser->lexer);
9808 /* If we aren't looking at a cast-expression, simulate an error so
9809 that the call to cp_parser_error_occurred below returns true. */
9810 if (!cast_expression)
9811 cp_parser_simulate_error (parser);
9812 else
9814 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9815 parser->in_type_id_in_expr_p = true;
9816 /* Look for the type-id. */
9817 type = cp_parser_type_id (parser);
9818 /* Look for the closing `)'. */
9819 cp_token *close_paren = parens.require_close (parser);
9820 if (close_paren)
9821 close_paren_loc = close_paren->location;
9822 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9825 /* Restore the saved message. */
9826 parser->type_definition_forbidden_message = saved_message;
9828 /* At this point this can only be either a cast or a
9829 parenthesized ctor such as `(T ())' that looks like a cast to
9830 function returning T. */
9831 if (!cp_parser_error_occurred (parser))
9833 /* Only commit if the cast-expression doesn't start with
9834 '++', '--', or '[' in C++11. */
9835 if (cast_expression > 0)
9836 cp_parser_commit_to_topmost_tentative_parse (parser);
9838 expr = cp_parser_cast_expression (parser,
9839 /*address_p=*/false,
9840 /*cast_p=*/true,
9841 /*decltype_p=*/false,
9842 pidk);
9844 if (cp_parser_parse_definitely (parser))
9846 /* Warn about old-style casts, if so requested. */
9847 if (warn_old_style_cast
9848 && !in_system_header_at (input_location)
9849 && !VOID_TYPE_P (type)
9850 && current_lang_name != lang_name_c)
9852 gcc_rich_location rich_loc (input_location);
9853 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9854 expr, type);
9855 warning_at (&rich_loc, OPT_Wold_style_cast,
9856 "use of old-style cast to %q#T", type);
9859 /* Only type conversions to integral or enumeration types
9860 can be used in constant-expressions. */
9861 if (!cast_valid_in_integral_constant_expression_p (type)
9862 && cp_parser_non_integral_constant_expression (parser,
9863 NIC_CAST))
9864 return error_mark_node;
9866 /* Perform the cast. */
9867 /* Make a location:
9868 (TYPE) EXPR
9869 ^~~~~~~~~~~
9870 with start==caret at the open paren, extending to the
9871 end of "expr". */
9872 location_t cast_loc = make_location (open_paren_loc,
9873 open_paren_loc,
9874 expr.get_finish ());
9875 expr = build_c_cast (cast_loc, type, expr);
9876 return expr;
9879 else
9880 cp_parser_abort_tentative_parse (parser);
9883 /* If we get here, then it's not a cast, so it must be a
9884 unary-expression. */
9885 return cp_parser_unary_expression (parser, pidk, address_p,
9886 cast_p, decltype_p);
9889 /* Parse a binary expression of the general form:
9891 pm-expression:
9892 cast-expression
9893 pm-expression .* cast-expression
9894 pm-expression ->* cast-expression
9896 multiplicative-expression:
9897 pm-expression
9898 multiplicative-expression * pm-expression
9899 multiplicative-expression / pm-expression
9900 multiplicative-expression % pm-expression
9902 additive-expression:
9903 multiplicative-expression
9904 additive-expression + multiplicative-expression
9905 additive-expression - multiplicative-expression
9907 shift-expression:
9908 additive-expression
9909 shift-expression << additive-expression
9910 shift-expression >> additive-expression
9912 relational-expression:
9913 shift-expression
9914 relational-expression < shift-expression
9915 relational-expression > shift-expression
9916 relational-expression <= shift-expression
9917 relational-expression >= shift-expression
9919 GNU Extension:
9921 relational-expression:
9922 relational-expression <? shift-expression
9923 relational-expression >? shift-expression
9925 equality-expression:
9926 relational-expression
9927 equality-expression == relational-expression
9928 equality-expression != relational-expression
9930 and-expression:
9931 equality-expression
9932 and-expression & equality-expression
9934 exclusive-or-expression:
9935 and-expression
9936 exclusive-or-expression ^ and-expression
9938 inclusive-or-expression:
9939 exclusive-or-expression
9940 inclusive-or-expression | exclusive-or-expression
9942 logical-and-expression:
9943 inclusive-or-expression
9944 logical-and-expression && inclusive-or-expression
9946 logical-or-expression:
9947 logical-and-expression
9948 logical-or-expression || logical-and-expression
9950 All these are implemented with a single function like:
9952 binary-expression:
9953 simple-cast-expression
9954 binary-expression <token> binary-expression
9956 CAST_P is true if this expression is the target of a cast.
9958 The binops_by_token map is used to get the tree codes for each <token> type.
9959 binary-expressions are associated according to a precedence table. */
9961 #define TOKEN_PRECEDENCE(token) \
9962 (((token->type == CPP_GREATER \
9963 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9964 && !parser->greater_than_is_operator_p) \
9965 ? PREC_NOT_OPERATOR \
9966 : binops_by_token[token->type].prec)
9968 static cp_expr
9969 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9970 bool no_toplevel_fold_p,
9971 bool decltype_p,
9972 enum cp_parser_prec prec,
9973 cp_id_kind * pidk)
9975 cp_parser_expression_stack stack;
9976 cp_parser_expression_stack_entry *sp = &stack[0];
9977 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9978 cp_parser_expression_stack_entry current;
9979 cp_expr rhs;
9980 cp_token *token;
9981 enum tree_code rhs_type;
9982 enum cp_parser_prec new_prec, lookahead_prec;
9983 tree overload;
9985 /* Parse the first expression. */
9986 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9987 ? TRUTH_NOT_EXPR : ERROR_MARK);
9988 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9989 cast_p, decltype_p, pidk);
9990 current.prec = prec;
9992 if (cp_parser_error_occurred (parser))
9993 return error_mark_node;
9995 for (;;)
9997 /* Get an operator token. */
9998 token = cp_lexer_peek_token (parser->lexer);
10000 if (warn_cxx11_compat
10001 && token->type == CPP_RSHIFT
10002 && !parser->greater_than_is_operator_p)
10004 if (warning_at (token->location, OPT_Wc__11_compat,
10005 "%<>>%> operator is treated"
10006 " as two right angle brackets in C++11"))
10007 inform (token->location,
10008 "suggest parentheses around %<>>%> expression");
10011 new_prec = TOKEN_PRECEDENCE (token);
10012 if (new_prec != PREC_NOT_OPERATOR
10013 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10014 /* This is a fold-expression; handle it later. */
10015 new_prec = PREC_NOT_OPERATOR;
10017 /* Popping an entry off the stack means we completed a subexpression:
10018 - either we found a token which is not an operator (`>' where it is not
10019 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10020 will happen repeatedly;
10021 - or, we found an operator which has lower priority. This is the case
10022 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10023 parsing `3 * 4'. */
10024 if (new_prec <= current.prec)
10026 if (sp == stack)
10027 break;
10028 else
10029 goto pop;
10032 get_rhs:
10033 current.tree_type = binops_by_token[token->type].tree_type;
10034 current.loc = token->location;
10036 /* We used the operator token. */
10037 cp_lexer_consume_token (parser->lexer);
10039 /* For "false && x" or "true || x", x will never be executed;
10040 disable warnings while evaluating it. */
10041 if ((current.tree_type == TRUTH_ANDIF_EXPR
10042 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10043 || (current.tree_type == TRUTH_ORIF_EXPR
10044 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10046 disable_warnings_sp = sp;
10047 ++c_inhibit_evaluation_warnings;
10050 /* Extract another operand. It may be the RHS of this expression
10051 or the LHS of a new, higher priority expression. */
10052 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10053 ? TRUTH_NOT_EXPR : ERROR_MARK);
10054 rhs = cp_parser_simple_cast_expression (parser);
10056 /* Get another operator token. Look up its precedence to avoid
10057 building a useless (immediately popped) stack entry for common
10058 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10059 token = cp_lexer_peek_token (parser->lexer);
10060 lookahead_prec = TOKEN_PRECEDENCE (token);
10061 if (lookahead_prec != PREC_NOT_OPERATOR
10062 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10063 lookahead_prec = PREC_NOT_OPERATOR;
10064 if (lookahead_prec > new_prec)
10066 /* ... and prepare to parse the RHS of the new, higher priority
10067 expression. Since precedence levels on the stack are
10068 monotonically increasing, we do not have to care about
10069 stack overflows. */
10070 *sp = current;
10071 ++sp;
10072 current.lhs = rhs;
10073 current.lhs_type = rhs_type;
10074 current.prec = new_prec;
10075 new_prec = lookahead_prec;
10076 goto get_rhs;
10078 pop:
10079 lookahead_prec = new_prec;
10080 /* If the stack is not empty, we have parsed into LHS the right side
10081 (`4' in the example above) of an expression we had suspended.
10082 We can use the information on the stack to recover the LHS (`3')
10083 from the stack together with the tree code (`MULT_EXPR'), and
10084 the precedence of the higher level subexpression
10085 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10086 which will be used to actually build the additive expression. */
10087 rhs = current.lhs;
10088 rhs_type = current.lhs_type;
10089 --sp;
10090 current = *sp;
10093 /* Undo the disabling of warnings done above. */
10094 if (sp == disable_warnings_sp)
10096 disable_warnings_sp = NULL;
10097 --c_inhibit_evaluation_warnings;
10100 if (warn_logical_not_paren
10101 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10102 && current.lhs_type == TRUTH_NOT_EXPR
10103 /* Avoid warning for !!x == y. */
10104 && (TREE_CODE (current.lhs) != NE_EXPR
10105 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10106 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10107 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10108 /* Avoid warning for !b == y where b is boolean. */
10109 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10110 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10111 != BOOLEAN_TYPE))))
10112 /* Avoid warning for !!b == y where b is boolean. */
10113 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
10114 || TREE_TYPE (current.lhs) == NULL_TREE
10115 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10116 warn_logical_not_parentheses (current.loc, current.tree_type,
10117 current.lhs, maybe_constant_value (rhs));
10119 overload = NULL;
10121 location_t combined_loc = make_location (current.loc,
10122 current.lhs.get_start (),
10123 rhs.get_finish ());
10125 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10126 ERROR_MARK for everything that is not a binary expression.
10127 This makes warn_about_parentheses miss some warnings that
10128 involve unary operators. For unary expressions we should
10129 pass the correct tree_code unless the unary expression was
10130 surrounded by parentheses.
10132 if (no_toplevel_fold_p
10133 && lookahead_prec <= current.prec
10134 && sp == stack)
10136 if (current.lhs == error_mark_node || rhs == error_mark_node)
10137 current.lhs = error_mark_node;
10138 else
10140 current.lhs.maybe_add_location_wrapper ();
10141 rhs.maybe_add_location_wrapper ();
10142 current.lhs
10143 = build_min (current.tree_type,
10144 TREE_CODE_CLASS (current.tree_type)
10145 == tcc_comparison
10146 ? boolean_type_node : TREE_TYPE (current.lhs),
10147 current.lhs.get_value (), rhs.get_value ());
10148 SET_EXPR_LOCATION (current.lhs, combined_loc);
10151 else
10153 op_location_t op_loc (current.loc, combined_loc);
10154 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10155 current.lhs, current.lhs_type,
10156 rhs, rhs_type, &overload,
10157 complain_flags (decltype_p));
10158 /* TODO: build_x_binary_op doesn't always honor the location. */
10159 current.lhs.set_location (combined_loc);
10161 current.lhs_type = current.tree_type;
10163 /* If the binary operator required the use of an overloaded operator,
10164 then this expression cannot be an integral constant-expression.
10165 An overloaded operator can be used even if both operands are
10166 otherwise permissible in an integral constant-expression if at
10167 least one of the operands is of enumeration type. */
10169 if (overload
10170 && cp_parser_non_integral_constant_expression (parser,
10171 NIC_OVERLOADED))
10172 return error_mark_node;
10175 return current.lhs;
10178 static cp_expr
10179 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10180 bool no_toplevel_fold_p,
10181 enum cp_parser_prec prec,
10182 cp_id_kind * pidk)
10184 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10185 /*decltype*/false, prec, pidk);
10188 /* Parse the `? expression : assignment-expression' part of a
10189 conditional-expression. The LOGICAL_OR_EXPR is the
10190 logical-or-expression that started the conditional-expression.
10191 Returns a representation of the entire conditional-expression.
10193 This routine is used by cp_parser_assignment_expression.
10195 ? expression : assignment-expression
10197 GNU Extensions:
10199 ? : assignment-expression */
10201 static tree
10202 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10204 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10205 cp_expr assignment_expr;
10206 struct cp_token *token;
10207 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10209 /* Consume the `?' token. */
10210 cp_lexer_consume_token (parser->lexer);
10211 token = cp_lexer_peek_token (parser->lexer);
10212 if (cp_parser_allow_gnu_extensions_p (parser)
10213 && token->type == CPP_COLON)
10215 pedwarn (token->location, OPT_Wpedantic,
10216 "ISO C++ does not allow %<?:%> with omitted middle operand");
10217 /* Implicit true clause. */
10218 expr = NULL_TREE;
10219 c_inhibit_evaluation_warnings +=
10220 folded_logical_or_expr == truthvalue_true_node;
10221 warn_for_omitted_condop (token->location, logical_or_expr);
10223 else
10225 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10226 parser->colon_corrects_to_scope_p = false;
10227 /* Parse the expression. */
10228 c_inhibit_evaluation_warnings +=
10229 folded_logical_or_expr == truthvalue_false_node;
10230 expr = cp_parser_expression (parser);
10231 c_inhibit_evaluation_warnings +=
10232 ((folded_logical_or_expr == truthvalue_true_node)
10233 - (folded_logical_or_expr == truthvalue_false_node));
10234 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10237 /* The next token should be a `:'. */
10238 cp_parser_require (parser, CPP_COLON, RT_COLON);
10239 /* Parse the assignment-expression. */
10240 assignment_expr = cp_parser_assignment_expression (parser);
10241 c_inhibit_evaluation_warnings -=
10242 folded_logical_or_expr == truthvalue_true_node;
10244 /* Make a location:
10245 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10246 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10247 with the caret at the "?", ranging from the start of
10248 the logical_or_expr to the end of the assignment_expr. */
10249 loc = make_location (loc,
10250 logical_or_expr.get_start (),
10251 assignment_expr.get_finish ());
10253 /* Build the conditional-expression. */
10254 return build_x_conditional_expr (loc, logical_or_expr,
10255 expr,
10256 assignment_expr,
10257 tf_warning_or_error);
10260 /* Parse an assignment-expression.
10262 assignment-expression:
10263 conditional-expression
10264 logical-or-expression assignment-operator assignment_expression
10265 throw-expression
10266 yield-expression
10268 CAST_P is true if this expression is the target of a cast.
10269 DECLTYPE_P is true if this expression is the operand of decltype.
10271 Returns a representation for the expression. */
10273 static cp_expr
10274 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10275 bool cast_p, bool decltype_p)
10277 cp_expr expr;
10279 /* If the next token is the `throw' keyword, then we're looking at
10280 a throw-expression. */
10281 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10282 expr = cp_parser_throw_expression (parser);
10283 /* If the next token is the `co_yield' keyword, then we're looking at
10284 a yield-expression. */
10285 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10286 expr = cp_parser_yield_expression (parser);
10287 /* Otherwise, it must be that we are looking at a
10288 logical-or-expression. */
10289 else
10291 /* Parse the binary expressions (logical-or-expression). */
10292 expr = cp_parser_binary_expression (parser, cast_p, false,
10293 decltype_p,
10294 PREC_NOT_OPERATOR, pidk);
10295 /* If the next token is a `?' then we're actually looking at a
10296 conditional-expression. */
10297 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10298 return cp_parser_question_colon_clause (parser, expr);
10299 else
10301 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10303 /* If it's an assignment-operator, we're using the second
10304 production. */
10305 enum tree_code assignment_operator
10306 = cp_parser_assignment_operator_opt (parser);
10307 if (assignment_operator != ERROR_MARK)
10309 bool non_constant_p;
10311 /* Parse the right-hand side of the assignment. */
10312 cp_expr rhs = cp_parser_initializer_clause (parser,
10313 &non_constant_p);
10315 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10316 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10318 /* An assignment may not appear in a
10319 constant-expression. */
10320 if (cp_parser_non_integral_constant_expression (parser,
10321 NIC_ASSIGNMENT))
10322 return error_mark_node;
10323 /* Build the assignment expression. Its default
10324 location:
10325 LHS = RHS
10326 ~~~~^~~~~
10327 is the location of the '=' token as the
10328 caret, ranging from the start of the lhs to the
10329 end of the rhs. */
10330 loc = make_location (loc,
10331 expr.get_start (),
10332 rhs.get_finish ());
10333 expr = build_x_modify_expr (loc, expr,
10334 assignment_operator,
10335 rhs,
10336 complain_flags (decltype_p));
10337 /* TODO: build_x_modify_expr doesn't honor the location,
10338 so we must set it here. */
10339 expr.set_location (loc);
10344 return expr;
10347 /* Parse an (optional) assignment-operator.
10349 assignment-operator: one of
10350 = *= /= %= += -= >>= <<= &= ^= |=
10352 GNU Extension:
10354 assignment-operator: one of
10355 <?= >?=
10357 If the next token is an assignment operator, the corresponding tree
10358 code is returned, and the token is consumed. For example, for
10359 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10360 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10361 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10362 operator, ERROR_MARK is returned. */
10364 static enum tree_code
10365 cp_parser_assignment_operator_opt (cp_parser* parser)
10367 enum tree_code op;
10368 cp_token *token;
10370 /* Peek at the next token. */
10371 token = cp_lexer_peek_token (parser->lexer);
10373 switch (token->type)
10375 case CPP_EQ:
10376 op = NOP_EXPR;
10377 break;
10379 case CPP_MULT_EQ:
10380 op = MULT_EXPR;
10381 break;
10383 case CPP_DIV_EQ:
10384 op = TRUNC_DIV_EXPR;
10385 break;
10387 case CPP_MOD_EQ:
10388 op = TRUNC_MOD_EXPR;
10389 break;
10391 case CPP_PLUS_EQ:
10392 op = PLUS_EXPR;
10393 break;
10395 case CPP_MINUS_EQ:
10396 op = MINUS_EXPR;
10397 break;
10399 case CPP_RSHIFT_EQ:
10400 op = RSHIFT_EXPR;
10401 break;
10403 case CPP_LSHIFT_EQ:
10404 op = LSHIFT_EXPR;
10405 break;
10407 case CPP_AND_EQ:
10408 op = BIT_AND_EXPR;
10409 break;
10411 case CPP_XOR_EQ:
10412 op = BIT_XOR_EXPR;
10413 break;
10415 case CPP_OR_EQ:
10416 op = BIT_IOR_EXPR;
10417 break;
10419 default:
10420 /* Nothing else is an assignment operator. */
10421 op = ERROR_MARK;
10424 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10425 if (op != ERROR_MARK
10426 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10427 op = ERROR_MARK;
10429 /* If it was an assignment operator, consume it. */
10430 if (op != ERROR_MARK)
10431 cp_lexer_consume_token (parser->lexer);
10433 return op;
10436 /* Parse an expression.
10438 expression:
10439 assignment-expression
10440 expression , assignment-expression
10442 CAST_P is true if this expression is the target of a cast.
10443 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10444 except possibly parenthesized or on the RHS of a comma (N3276).
10445 WARN_COMMA_P is true if a comma should be diagnosed.
10447 Returns a representation of the expression. */
10449 static cp_expr
10450 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10451 bool cast_p, bool decltype_p, bool warn_comma_p)
10453 cp_expr expression = NULL_TREE;
10454 location_t loc = UNKNOWN_LOCATION;
10456 while (true)
10458 cp_expr assignment_expression;
10460 /* Parse the next assignment-expression. */
10461 assignment_expression
10462 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10464 /* We don't create a temporary for a call that is the immediate operand
10465 of decltype or on the RHS of a comma. But when we see a comma, we
10466 need to create a temporary for a call on the LHS. */
10467 if (decltype_p && !processing_template_decl
10468 && TREE_CODE (assignment_expression) == CALL_EXPR
10469 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10470 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10471 assignment_expression
10472 = build_cplus_new (TREE_TYPE (assignment_expression),
10473 assignment_expression, tf_warning_or_error);
10475 /* If this is the first assignment-expression, we can just
10476 save it away. */
10477 if (!expression)
10478 expression = assignment_expression;
10479 else
10481 /* Create a location with caret at the comma, ranging
10482 from the start of the LHS to the end of the RHS. */
10483 loc = make_location (loc,
10484 expression.get_start (),
10485 assignment_expression.get_finish ());
10486 expression = build_x_compound_expr (loc, expression,
10487 assignment_expression,
10488 complain_flags (decltype_p));
10489 expression.set_location (loc);
10491 /* If the next token is not a comma, or we're in a fold-expression, then
10492 we are done with the expression. */
10493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10494 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10495 break;
10496 /* Consume the `,'. */
10497 loc = cp_lexer_peek_token (parser->lexer)->location;
10498 if (warn_comma_p)
10500 /* [depr.comma.subscript]: A comma expression appearing as
10501 the expr-or-braced-init-list of a subscripting expression
10502 is deprecated. A parenthesized comma expression is not
10503 deprecated. */
10504 warning_at (loc, OPT_Wcomma_subscript,
10505 "top-level comma expression in array subscript "
10506 "is deprecated");
10507 warn_comma_p = false;
10509 cp_lexer_consume_token (parser->lexer);
10510 /* A comma operator cannot appear in a constant-expression. */
10511 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10512 expression = error_mark_node;
10515 return expression;
10518 /* Parse a constant-expression.
10520 constant-expression:
10521 conditional-expression
10523 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10524 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10525 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10526 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10527 greater than 1, this isn't really a constant-expression, only a
10528 potentially constant-evaluated expression. If STRICT_P is true,
10529 only parse a conditional-expression, otherwise parse an
10530 assignment-expression. See below for rationale. */
10532 static cp_expr
10533 cp_parser_constant_expression (cp_parser* parser,
10534 int allow_non_constant_p,
10535 bool *non_constant_p,
10536 bool strict_p)
10538 bool saved_integral_constant_expression_p;
10539 bool saved_allow_non_integral_constant_expression_p;
10540 bool saved_non_integral_constant_expression_p;
10541 cp_expr expression;
10543 /* It might seem that we could simply parse the
10544 conditional-expression, and then check to see if it were
10545 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10546 one that the compiler can figure out is constant, possibly after
10547 doing some simplifications or optimizations. The standard has a
10548 precise definition of constant-expression, and we must honor
10549 that, even though it is somewhat more restrictive.
10551 For example:
10553 int i[(2, 3)];
10555 is not a legal declaration, because `(2, 3)' is not a
10556 constant-expression. The `,' operator is forbidden in a
10557 constant-expression. However, GCC's constant-folding machinery
10558 will fold this operation to an INTEGER_CST for `3'. */
10560 /* Save the old settings. */
10561 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10562 saved_allow_non_integral_constant_expression_p
10563 = parser->allow_non_integral_constant_expression_p;
10564 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10565 /* We are now parsing a constant-expression. */
10566 parser->integral_constant_expression_p = true;
10567 parser->allow_non_integral_constant_expression_p
10568 = (allow_non_constant_p || cxx_dialect >= cxx11);
10569 parser->non_integral_constant_expression_p = false;
10571 /* A manifestly constant-evaluated expression is evaluated even in an
10572 unevaluated operand. */
10573 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
10575 /* Although the grammar says "conditional-expression", when not STRICT_P,
10576 we parse an "assignment-expression", which also permits
10577 "throw-expression" and the use of assignment operators. In the case
10578 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10579 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10580 actually essential that we look for an assignment-expression.
10581 For example, cp_parser_initializer_clauses uses this function to
10582 determine whether a particular assignment-expression is in fact
10583 constant. */
10584 if (strict_p)
10586 /* Parse the binary expressions (logical-or-expression). */
10587 expression = cp_parser_binary_expression (parser, false, false, false,
10588 PREC_NOT_OPERATOR, NULL);
10589 /* If the next token is a `?' then we're actually looking at
10590 a conditional-expression; otherwise we're done. */
10591 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10592 expression = cp_parser_question_colon_clause (parser, expression);
10594 else
10595 expression = cp_parser_assignment_expression (parser);
10596 /* Restore the old settings. */
10597 parser->integral_constant_expression_p
10598 = saved_integral_constant_expression_p;
10599 parser->allow_non_integral_constant_expression_p
10600 = saved_allow_non_integral_constant_expression_p;
10601 if (cxx_dialect >= cxx11)
10603 /* Require an rvalue constant expression here; that's what our
10604 callers expect. Reference constant expressions are handled
10605 separately in e.g. cp_parser_template_argument. */
10606 tree decay = expression;
10607 if (TREE_TYPE (expression)
10608 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10609 decay = build_address (expression);
10610 bool is_const = is_rvalue_constant_expression (decay);
10611 parser->non_integral_constant_expression_p = !is_const;
10612 if (!is_const && !allow_non_constant_p)
10613 require_rvalue_constant_expression (decay);
10615 if (allow_non_constant_p)
10616 *non_constant_p = parser->non_integral_constant_expression_p;
10617 parser->non_integral_constant_expression_p
10618 = saved_non_integral_constant_expression_p;
10620 return expression;
10623 /* Parse __builtin_offsetof.
10625 offsetof-expression:
10626 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10628 offsetof-member-designator:
10629 id-expression
10630 | offsetof-member-designator "." id-expression
10631 | offsetof-member-designator "[" expression "]"
10632 | offsetof-member-designator "->" id-expression */
10634 static cp_expr
10635 cp_parser_builtin_offsetof (cp_parser *parser)
10637 int save_ice_p, save_non_ice_p;
10638 tree type;
10639 cp_expr expr;
10640 cp_id_kind dummy;
10641 cp_token *token;
10642 location_t finish_loc;
10644 /* We're about to accept non-integral-constant things, but will
10645 definitely yield an integral constant expression. Save and
10646 restore these values around our local parsing. */
10647 save_ice_p = parser->integral_constant_expression_p;
10648 save_non_ice_p = parser->non_integral_constant_expression_p;
10650 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10652 /* Consume the "__builtin_offsetof" token. */
10653 cp_lexer_consume_token (parser->lexer);
10654 /* Consume the opening `('. */
10655 matching_parens parens;
10656 parens.require_open (parser);
10657 /* Parse the type-id. */
10658 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10660 const char *saved_message = parser->type_definition_forbidden_message;
10661 parser->type_definition_forbidden_message
10662 = G_("types may not be defined within %<__builtin_offsetof%>");
10663 type = cp_parser_type_id (parser);
10664 parser->type_definition_forbidden_message = saved_message;
10666 /* Look for the `,'. */
10667 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10668 token = cp_lexer_peek_token (parser->lexer);
10670 /* Build the (type *)null that begins the traditional offsetof macro. */
10671 tree object_ptr
10672 = build_static_cast (input_location, build_pointer_type (type),
10673 null_pointer_node, tf_warning_or_error);
10675 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10676 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10677 true, &dummy, token->location);
10678 while (true)
10680 token = cp_lexer_peek_token (parser->lexer);
10681 switch (token->type)
10683 case CPP_OPEN_SQUARE:
10684 /* offsetof-member-designator "[" expression "]" */
10685 expr = cp_parser_postfix_open_square_expression (parser, expr,
10686 true, false);
10687 break;
10689 case CPP_DEREF:
10690 /* offsetof-member-designator "->" identifier */
10691 expr = grok_array_decl (token->location, expr, integer_zero_node,
10692 NULL, tf_warning_or_error);
10693 /* FALLTHRU */
10695 case CPP_DOT:
10696 /* offsetof-member-designator "." identifier */
10697 cp_lexer_consume_token (parser->lexer);
10698 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10699 expr, true, &dummy,
10700 token->location);
10701 break;
10703 case CPP_CLOSE_PAREN:
10704 /* Consume the ")" token. */
10705 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10706 cp_lexer_consume_token (parser->lexer);
10707 goto success;
10709 default:
10710 /* Error. We know the following require will fail, but
10711 that gives the proper error message. */
10712 parens.require_close (parser);
10713 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10714 expr = error_mark_node;
10715 goto failure;
10719 success:
10720 /* Make a location of the form:
10721 __builtin_offsetof (struct s, f)
10722 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10723 with caret at the type-id, ranging from the start of the
10724 "_builtin_offsetof" token to the close paren. */
10725 loc = make_location (loc, start_loc, finish_loc);
10726 /* The result will be an INTEGER_CST, so we need to explicitly
10727 preserve the location. */
10728 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10730 failure:
10731 parser->integral_constant_expression_p = save_ice_p;
10732 parser->non_integral_constant_expression_p = save_non_ice_p;
10734 expr = expr.maybe_add_location_wrapper ();
10735 return expr;
10738 /* Parse a trait expression.
10740 Returns a representation of the expression, the underlying type
10741 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10743 static cp_expr
10744 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10746 cp_trait_kind kind;
10747 tree type1, type2 = NULL_TREE;
10748 bool binary = false;
10749 bool variadic = false;
10751 switch (keyword)
10753 case RID_HAS_NOTHROW_ASSIGN:
10754 kind = CPTK_HAS_NOTHROW_ASSIGN;
10755 break;
10756 case RID_HAS_NOTHROW_CONSTRUCTOR:
10757 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10758 break;
10759 case RID_HAS_NOTHROW_COPY:
10760 kind = CPTK_HAS_NOTHROW_COPY;
10761 break;
10762 case RID_HAS_TRIVIAL_ASSIGN:
10763 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10764 break;
10765 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10766 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10767 break;
10768 case RID_HAS_TRIVIAL_COPY:
10769 kind = CPTK_HAS_TRIVIAL_COPY;
10770 break;
10771 case RID_HAS_TRIVIAL_DESTRUCTOR:
10772 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10773 break;
10774 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10775 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10776 break;
10777 case RID_HAS_VIRTUAL_DESTRUCTOR:
10778 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10779 break;
10780 case RID_IS_ABSTRACT:
10781 kind = CPTK_IS_ABSTRACT;
10782 break;
10783 case RID_IS_AGGREGATE:
10784 kind = CPTK_IS_AGGREGATE;
10785 break;
10786 case RID_IS_BASE_OF:
10787 kind = CPTK_IS_BASE_OF;
10788 binary = true;
10789 break;
10790 case RID_IS_CLASS:
10791 kind = CPTK_IS_CLASS;
10792 break;
10793 case RID_IS_EMPTY:
10794 kind = CPTK_IS_EMPTY;
10795 break;
10796 case RID_IS_ENUM:
10797 kind = CPTK_IS_ENUM;
10798 break;
10799 case RID_IS_FINAL:
10800 kind = CPTK_IS_FINAL;
10801 break;
10802 case RID_IS_LAYOUT_COMPATIBLE:
10803 kind = CPTK_IS_LAYOUT_COMPATIBLE;
10804 binary = true;
10805 break;
10806 case RID_IS_LITERAL_TYPE:
10807 kind = CPTK_IS_LITERAL_TYPE;
10808 break;
10809 case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
10810 kind = CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF;
10811 binary = true;
10812 break;
10813 case RID_IS_POD:
10814 kind = CPTK_IS_POD;
10815 break;
10816 case RID_IS_POLYMORPHIC:
10817 kind = CPTK_IS_POLYMORPHIC;
10818 break;
10819 case RID_IS_SAME_AS:
10820 kind = CPTK_IS_SAME_AS;
10821 binary = true;
10822 break;
10823 case RID_IS_STD_LAYOUT:
10824 kind = CPTK_IS_STD_LAYOUT;
10825 break;
10826 case RID_IS_TRIVIAL:
10827 kind = CPTK_IS_TRIVIAL;
10828 break;
10829 case RID_IS_TRIVIALLY_ASSIGNABLE:
10830 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10831 binary = true;
10832 break;
10833 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10834 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10835 variadic = true;
10836 break;
10837 case RID_IS_TRIVIALLY_COPYABLE:
10838 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10839 break;
10840 case RID_IS_UNION:
10841 kind = CPTK_IS_UNION;
10842 break;
10843 case RID_UNDERLYING_TYPE:
10844 kind = CPTK_UNDERLYING_TYPE;
10845 break;
10846 case RID_BASES:
10847 kind = CPTK_BASES;
10848 break;
10849 case RID_DIRECT_BASES:
10850 kind = CPTK_DIRECT_BASES;
10851 break;
10852 case RID_IS_ASSIGNABLE:
10853 kind = CPTK_IS_ASSIGNABLE;
10854 binary = true;
10855 break;
10856 case RID_IS_CONSTRUCTIBLE:
10857 kind = CPTK_IS_CONSTRUCTIBLE;
10858 variadic = true;
10859 break;
10860 case RID_IS_NOTHROW_ASSIGNABLE:
10861 kind = CPTK_IS_NOTHROW_ASSIGNABLE;
10862 binary = true;
10863 break;
10864 case RID_IS_NOTHROW_CONSTRUCTIBLE:
10865 kind = CPTK_IS_NOTHROW_CONSTRUCTIBLE;
10866 variadic = true;
10867 break;
10868 default:
10869 gcc_unreachable ();
10872 /* Get location of initial token. */
10873 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10875 /* Consume the token. */
10876 cp_lexer_consume_token (parser->lexer);
10878 matching_parens parens;
10879 parens.require_open (parser);
10882 type_id_in_expr_sentinel s (parser);
10883 type1 = cp_parser_type_id (parser);
10886 if (type1 == error_mark_node)
10887 return error_mark_node;
10889 if (binary)
10891 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10894 type_id_in_expr_sentinel s (parser);
10895 type2 = cp_parser_type_id (parser);
10898 if (type2 == error_mark_node)
10899 return error_mark_node;
10901 else if (variadic)
10903 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10905 cp_lexer_consume_token (parser->lexer);
10906 tree elt = cp_parser_type_id (parser);
10907 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10909 cp_lexer_consume_token (parser->lexer);
10910 elt = make_pack_expansion (elt);
10912 if (elt == error_mark_node)
10913 return error_mark_node;
10914 type2 = tree_cons (NULL_TREE, elt, type2);
10916 type2 = nreverse (type2);
10919 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10920 parens.require_close (parser);
10922 /* Construct a location of the form:
10923 __is_trivially_copyable(_Tp)
10924 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10925 with start == caret, finishing at the close-paren. */
10926 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10928 /* Complete the trait expression, which may mean either processing
10929 the trait expr now or saving it for template instantiation. */
10930 switch (kind)
10932 case CPTK_UNDERLYING_TYPE:
10933 return cp_expr (finish_underlying_type (type1), trait_loc);
10934 case CPTK_BASES:
10935 return cp_expr (finish_bases (type1, false), trait_loc);
10936 case CPTK_DIRECT_BASES:
10937 return cp_expr (finish_bases (type1, true), trait_loc);
10938 default:
10939 return finish_trait_expr (trait_loc, kind, type1, type2);
10943 /* Parse a lambda expression.
10945 lambda-expression:
10946 lambda-introducer lambda-declarator [opt] compound-statement
10947 lambda-introducer < template-parameter-list > requires-clause [opt]
10948 lambda-declarator [opt] compound-statement
10950 Returns a representation of the expression. */
10952 static cp_expr
10953 cp_parser_lambda_expression (cp_parser* parser)
10955 tree lambda_expr = build_lambda_expr ();
10956 tree type;
10957 bool ok = true;
10958 cp_token *token = cp_lexer_peek_token (parser->lexer);
10959 cp_token_position start = 0;
10961 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10963 if (cxx_dialect >= cxx20)
10965 /* C++20 allows lambdas in unevaluated context, but one in the type of a
10966 non-type parameter is nonsensical.
10968 Distinguish a lambda in the parameter type from a lambda in the
10969 default argument by looking at local_variables_forbidden_p, which is
10970 only set in default arguments. */
10971 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
10973 error_at (token->location,
10974 "lambda-expression in template parameter type");
10975 token->error_reported = true;
10976 ok = false;
10979 else if (cp_unevaluated_operand)
10981 if (!token->error_reported)
10983 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10984 "lambda-expression in unevaluated context"
10985 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10986 token->error_reported = true;
10988 ok = false;
10990 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10992 if (!token->error_reported)
10994 error_at (token->location, "lambda-expression in template-argument"
10995 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10996 token->error_reported = true;
10998 ok = false;
11001 /* We may be in the middle of deferred access check. Disable
11002 it now. */
11003 push_deferring_access_checks (dk_no_deferred);
11005 cp_parser_lambda_introducer (parser, lambda_expr);
11006 if (cp_parser_error_occurred (parser))
11007 return error_mark_node;
11009 type = begin_lambda_type (lambda_expr);
11010 if (type == error_mark_node)
11011 return error_mark_node;
11013 record_lambda_scope (lambda_expr);
11015 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11016 determine_visibility (TYPE_NAME (type));
11018 /* Now that we've started the type, add the capture fields for any
11019 explicit captures. */
11020 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11023 /* Inside the class, surrounding template-parameter-lists do not apply. */
11024 unsigned int saved_num_template_parameter_lists
11025 = parser->num_template_parameter_lists;
11026 unsigned char in_statement = parser->in_statement;
11027 bool in_switch_statement_p = parser->in_switch_statement_p;
11028 bool fully_implicit_function_template_p
11029 = parser->fully_implicit_function_template_p;
11030 tree implicit_template_parms = parser->implicit_template_parms;
11031 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11032 bool auto_is_implicit_function_template_parm_p
11033 = parser->auto_is_implicit_function_template_parm_p;
11035 parser->num_template_parameter_lists = 0;
11036 parser->in_statement = 0;
11037 parser->in_switch_statement_p = false;
11038 parser->fully_implicit_function_template_p = false;
11039 parser->implicit_template_parms = 0;
11040 parser->implicit_template_scope = 0;
11041 parser->auto_is_implicit_function_template_parm_p = false;
11043 /* The body of a lambda in a discarded statement is not discarded. */
11044 bool discarded = in_discarded_stmt;
11045 in_discarded_stmt = 0;
11047 /* Similarly the body of a lambda in immediate function context is not
11048 in immediate function context. */
11049 bool save_in_consteval_if_p = in_consteval_if_p;
11050 in_consteval_if_p = false;
11052 /* By virtue of defining a local class, a lambda expression has access to
11053 the private variables of enclosing classes. */
11055 if (cp_parser_start_tentative_firewall (parser))
11056 start = token;
11058 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11060 if (ok && cp_parser_error_occurred (parser))
11061 ok = false;
11063 if (ok)
11065 cp_parser_lambda_body (parser, lambda_expr);
11067 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11069 if (cp_parser_skip_to_closing_brace (parser))
11070 cp_lexer_consume_token (parser->lexer);
11073 /* The capture list was built up in reverse order; fix that now. */
11074 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11075 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11077 if (ok)
11078 maybe_add_lambda_conv_op (type);
11080 finish_struct (type, /*attributes=*/NULL_TREE);
11082 in_consteval_if_p = save_in_consteval_if_p;
11083 in_discarded_stmt = discarded;
11085 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11086 parser->in_statement = in_statement;
11087 parser->in_switch_statement_p = in_switch_statement_p;
11088 parser->fully_implicit_function_template_p
11089 = fully_implicit_function_template_p;
11090 parser->implicit_template_parms = implicit_template_parms;
11091 parser->implicit_template_scope = implicit_template_scope;
11092 parser->auto_is_implicit_function_template_parm_p
11093 = auto_is_implicit_function_template_parm_p;
11096 /* This field is only used during parsing of the lambda. */
11097 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11099 /* This lambda shouldn't have any proxies left at this point. */
11100 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11101 /* And now that we're done, push proxies for an enclosing lambda. */
11102 insert_pending_capture_proxies ();
11104 /* Update the lambda expression to a range. */
11105 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11106 token->location,
11107 parser->lexer);
11109 if (ok)
11110 lambda_expr = build_lambda_object (lambda_expr);
11111 else
11112 lambda_expr = error_mark_node;
11114 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11116 pop_deferring_access_checks ();
11118 return lambda_expr;
11121 /* Parse the beginning of a lambda expression.
11123 lambda-introducer:
11124 [ lambda-capture [opt] ]
11126 LAMBDA_EXPR is the current representation of the lambda expression. */
11128 static void
11129 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11131 /* Need commas after the first capture. */
11132 bool first = true;
11134 /* Eat the leading `['. */
11135 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11137 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11138 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11139 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11140 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11141 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11142 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11143 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11144 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11146 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11148 cp_lexer_consume_token (parser->lexer);
11149 first = false;
11151 if (!(at_function_scope_p () || parsing_nsdmi ()))
11152 error ("non-local lambda expression cannot have a capture-default");
11155 hash_set<tree, true> ids;
11156 tree first_capture_id = NULL_TREE;
11157 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11159 cp_token* capture_token;
11160 tree capture_id;
11161 tree capture_init_expr;
11162 cp_id_kind idk = CP_ID_KIND_NONE;
11163 bool explicit_init_p = false;
11165 enum capture_kind_type
11167 BY_COPY,
11168 BY_REFERENCE
11170 enum capture_kind_type capture_kind = BY_COPY;
11172 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11174 error ("expected end of capture-list");
11175 return;
11178 if (first)
11179 first = false;
11180 else
11181 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11183 /* Possibly capture `this'. */
11184 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11186 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11187 if (cxx_dialect < cxx20
11188 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11189 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
11190 "with by-copy capture default");
11191 cp_lexer_consume_token (parser->lexer);
11192 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11193 pedwarn (input_location, 0,
11194 "already captured %qD in lambda expression",
11195 this_identifier);
11196 else
11197 add_capture (lambda_expr, /*id=*/this_identifier,
11198 /*initializer=*/finish_this_expr (),
11199 /*by_reference_p=*/true, explicit_init_p);
11200 continue;
11203 /* Possibly capture `*this'. */
11204 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11205 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11207 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11208 if (cxx_dialect < cxx17)
11209 pedwarn (loc, OPT_Wc__17_extensions,
11210 "%<*this%> capture only available with "
11211 "%<-std=c++17%> or %<-std=gnu++17%>");
11212 cp_lexer_consume_token (parser->lexer);
11213 cp_lexer_consume_token (parser->lexer);
11214 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11215 pedwarn (input_location, 0,
11216 "already captured %qD in lambda expression",
11217 this_identifier);
11218 else
11219 add_capture (lambda_expr, /*id=*/this_identifier,
11220 /*initializer=*/finish_this_expr (),
11221 /*by_reference_p=*/false, explicit_init_p);
11222 continue;
11225 /* But reject `&this'. */
11226 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11227 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11229 error_at (cp_lexer_peek_token (parser->lexer)->location,
11230 "%<this%> cannot be captured by reference");
11231 cp_lexer_consume_token (parser->lexer);
11232 cp_lexer_consume_token (parser->lexer);
11233 continue;
11236 /* Remember whether we want to capture as a reference or not. */
11237 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11239 capture_kind = BY_REFERENCE;
11240 cp_lexer_consume_token (parser->lexer);
11243 bool init_pack_expansion = false;
11244 location_t ellipsis_loc = UNKNOWN_LOCATION;
11245 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11247 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11248 if (cxx_dialect < cxx20)
11249 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11250 "pack init-capture only available with "
11251 "%<-std=c++20%> or %<-std=gnu++20%>");
11252 cp_lexer_consume_token (parser->lexer);
11253 init_pack_expansion = true;
11256 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11257 if (init_pack_expansion && capture_kind != BY_REFERENCE
11258 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11260 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11261 0, "%<&%> should come before %<...%>");
11262 capture_kind = BY_REFERENCE;
11263 cp_lexer_consume_token (parser->lexer);
11266 /* Get the identifier. */
11267 capture_token = cp_lexer_peek_token (parser->lexer);
11268 capture_id = cp_parser_identifier (parser);
11270 if (capture_id == error_mark_node)
11271 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11272 delimiters, but I modified this to stop on unnested ']' as well. It
11273 was already changed to stop on unnested '}', so the
11274 "closing_parenthesis" name is no more misleading with my change. */
11276 cp_parser_skip_to_closing_parenthesis (parser,
11277 /*recovering=*/true,
11278 /*or_comma=*/true,
11279 /*consume_paren=*/true);
11280 break;
11283 /* Find the initializer for this capture. */
11284 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11285 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11286 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11288 bool direct, non_constant;
11289 /* An explicit initializer exists. */
11290 if (cxx_dialect < cxx14)
11291 pedwarn (input_location, OPT_Wc__14_extensions,
11292 "lambda capture initializers "
11293 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11294 capture_init_expr = cp_parser_initializer (parser, &direct,
11295 &non_constant, true);
11296 explicit_init_p = true;
11297 if (capture_init_expr == NULL_TREE)
11299 error ("empty initializer for lambda init-capture");
11300 capture_init_expr = error_mark_node;
11302 if (init_pack_expansion)
11303 capture_init_expr = make_pack_expansion (capture_init_expr);
11305 else
11307 const char* error_msg;
11309 /* Turn the identifier into an id-expression. */
11310 capture_init_expr
11311 = cp_parser_lookup_name_simple (parser, capture_id,
11312 capture_token->location);
11314 if (capture_init_expr == error_mark_node)
11316 unqualified_name_lookup_error (capture_id);
11317 continue;
11319 else if (!VAR_P (capture_init_expr)
11320 && TREE_CODE (capture_init_expr) != PARM_DECL)
11322 error_at (capture_token->location,
11323 "capture of non-variable %qE",
11324 capture_init_expr);
11325 if (DECL_P (capture_init_expr))
11326 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11327 "%q#D declared here", capture_init_expr);
11328 continue;
11330 if (VAR_P (capture_init_expr)
11331 && decl_storage_duration (capture_init_expr) != dk_auto)
11333 if (pedwarn (capture_token->location, 0, "capture of variable "
11334 "%qD with non-automatic storage duration",
11335 capture_init_expr))
11336 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11337 "%q#D declared here", capture_init_expr);
11338 continue;
11341 capture_init_expr
11342 = finish_id_expression
11343 (capture_id,
11344 capture_init_expr,
11345 parser->scope,
11346 &idk,
11347 /*integral_constant_expression_p=*/false,
11348 /*allow_non_integral_constant_expression_p=*/false,
11349 /*non_integral_constant_expression_p=*/NULL,
11350 /*template_p=*/false,
11351 /*done=*/true,
11352 /*address_p=*/false,
11353 /*template_arg_p=*/false,
11354 &error_msg,
11355 capture_token->location);
11357 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11359 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11360 cp_lexer_consume_token (parser->lexer);
11361 capture_init_expr = make_pack_expansion (capture_init_expr);
11362 if (init_pack_expansion)
11364 /* If what follows is an initializer, the second '...' is
11365 invalid. But for cases like [...xs...], the first one
11366 is invalid. */
11367 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11368 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11369 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11370 ellipsis_loc = loc;
11371 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11372 continue;
11377 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11378 && !explicit_init_p)
11380 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11381 && capture_kind == BY_COPY)
11382 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11383 "of %qD redundant with by-copy capture default",
11384 capture_id);
11385 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11386 && capture_kind == BY_REFERENCE)
11387 pedwarn (capture_token->location, 0, "explicit by-reference "
11388 "capture of %qD redundant with by-reference capture "
11389 "default", capture_id);
11392 /* Check for duplicates.
11393 Optimize for the zero or one explicit captures cases and only create
11394 the hash_set after adding second capture. */
11395 bool found = false;
11396 if (!ids.is_empty ())
11397 found = ids.add (capture_id);
11398 else if (first_capture_id == NULL_TREE)
11399 first_capture_id = capture_id;
11400 else if (capture_id == first_capture_id)
11401 found = true;
11402 else
11404 ids.add (first_capture_id);
11405 ids.add (capture_id);
11407 if (found)
11408 pedwarn (input_location, 0,
11409 "already captured %qD in lambda expression", capture_id);
11410 else
11411 add_capture (lambda_expr, capture_id, capture_init_expr,
11412 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11413 explicit_init_p);
11415 /* If there is any qualification still in effect, clear it
11416 now; we will be starting fresh with the next capture. */
11417 parser->scope = NULL_TREE;
11418 parser->qualifying_scope = NULL_TREE;
11419 parser->object_scope = NULL_TREE;
11422 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11425 /* Parse the (optional) middle of a lambda expression.
11427 lambda-declarator:
11428 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11429 lambda-specifiers (C++23)
11431 lambda-specifiers:
11432 decl-specifier-seq [opt] noexcept-specifier [opt]
11433 attribute-specifier-seq [opt] trailing-return-type [opt]
11435 LAMBDA_EXPR is the current representation of the lambda expression. */
11437 static bool
11438 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11440 /* 5.1.1.4 of the standard says:
11441 If a lambda-expression does not include a lambda-declarator, it is as if
11442 the lambda-declarator were ().
11443 This means an empty parameter list, no attributes, and no exception
11444 specification. */
11445 tree param_list = void_list_node;
11446 tree std_attrs = NULL_TREE;
11447 tree gnu_attrs = NULL_TREE;
11448 tree exception_spec = NULL_TREE;
11449 tree template_param_list = NULL_TREE;
11450 tree tx_qual = NULL_TREE;
11451 tree return_type = NULL_TREE;
11452 tree trailing_requires_clause = NULL_TREE;
11453 bool has_param_list = false;
11454 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11455 cp_decl_specifier_seq lambda_specs;
11456 clear_decl_specs (&lambda_specs);
11457 /* A lambda op() is const unless explicitly 'mutable'. */
11458 cp_cv_quals quals = TYPE_QUAL_CONST;
11460 /* The template-parameter-list is optional, but must begin with
11461 an opening angle if present. */
11462 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11464 if (cxx_dialect < cxx14)
11465 pedwarn (parser->lexer->next_token->location, OPT_Wc__14_extensions,
11466 "lambda templates are only available with "
11467 "%<-std=c++14%> or %<-std=gnu++14%>");
11468 else if (pedantic && cxx_dialect < cxx20)
11469 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11470 "lambda templates are only available with "
11471 "%<-std=c++20%> or %<-std=gnu++20%>");
11473 cp_lexer_consume_token (parser->lexer);
11475 template_param_list = cp_parser_template_parameter_list (parser);
11476 cp_parser_skip_to_end_of_template_parameter_list (parser);
11478 /* We may have a constrained generic lambda; parse the requires-clause
11479 immediately after the template-parameter-list and combine with any
11480 shorthand constraints present. */
11481 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11482 if (flag_concepts)
11484 tree reqs = get_shorthand_constraints (current_template_parms);
11485 if (dreqs)
11486 reqs = combine_constraint_expressions (reqs, dreqs);
11487 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11490 /* We just processed one more parameter list. */
11491 ++parser->num_template_parameter_lists;
11494 /* Committee discussion supports allowing attributes here. */
11495 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11497 /* The parameter-declaration-clause is optional (unless
11498 template-parameter-list was given), but must begin with an
11499 opening parenthesis if present. */
11500 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11502 bool is_consteval = false;
11503 /* For C++20, before parsing the parameter list check if there is
11504 a consteval specifier in the corresponding decl-specifier-seq. */
11505 if (cxx_dialect >= cxx20)
11507 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
11508 cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
11510 if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
11511 == RID_CONSTEVAL)
11513 is_consteval = true;
11514 break;
11519 matching_parens parens;
11520 parens.consume_open (parser);
11522 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11524 if (is_consteval)
11525 current_binding_level->immediate_fn_ctx_p = true;
11527 /* Parse parameters. */
11528 param_list
11529 = cp_parser_parameter_declaration_clause
11530 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11532 /* Default arguments shall not be specified in the
11533 parameter-declaration-clause of a lambda-declarator. */
11534 if (pedantic && cxx_dialect < cxx14)
11535 for (tree t = param_list; t; t = TREE_CHAIN (t))
11536 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11537 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11538 OPT_Wc__14_extensions,
11539 "default argument specified for lambda parameter");
11541 parens.require_close (parser);
11542 has_param_list = true;
11544 else if (cxx_dialect < cxx23)
11545 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11547 /* In the decl-specifier-seq of the lambda-declarator, each
11548 decl-specifier shall either be mutable or constexpr. */
11549 int declares_class_or_enum;
11550 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11551 cp_parser_decl_specifier_seq (parser,
11552 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11553 &lambda_specs, &declares_class_or_enum);
11555 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11557 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11558 "parameter declaration before lambda declaration "
11559 "specifiers only optional with %<-std=c++2b%> or "
11560 "%<-std=gnu++2b%>");
11561 omitted_parms_loc = UNKNOWN_LOCATION;
11564 if (lambda_specs.storage_class == sc_mutable)
11566 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11567 quals = TYPE_UNQUALIFIED;
11568 if (lambda_specs.conflicting_specifiers_p)
11569 error_at (lambda_specs.locations[ds_storage_class],
11570 "duplicate %<mutable%>");
11573 tx_qual = cp_parser_tx_qualifier_opt (parser);
11574 if (omitted_parms_loc && tx_qual)
11576 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11577 "parameter declaration before lambda transaction "
11578 "qualifier only optional with %<-std=c++2b%> or "
11579 "%<-std=gnu++2b%>");
11580 omitted_parms_loc = UNKNOWN_LOCATION;
11583 /* Parse optional exception specification. */
11584 exception_spec
11585 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11587 if (omitted_parms_loc && exception_spec)
11589 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11590 "parameter declaration before lambda exception "
11591 "specification only optional with %<-std=c++2b%> or "
11592 "%<-std=gnu++2b%>");
11593 omitted_parms_loc = UNKNOWN_LOCATION;
11596 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11597 attributes that appertain to the function type. */
11598 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11599 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11600 else
11601 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11603 /* Parse optional trailing return type. */
11604 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11606 if (omitted_parms_loc)
11607 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11608 "parameter declaration before lambda trailing "
11609 "return type only optional with %<-std=c++2b%> or "
11610 "%<-std=gnu++2b%>");
11611 cp_lexer_consume_token (parser->lexer);
11612 return_type = cp_parser_trailing_type_id (parser);
11615 /* Also allow GNU attributes at the very end of the declaration, the usual
11616 place for GNU attributes. */
11617 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11618 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11620 if (has_param_list)
11622 /* Parse optional trailing requires clause. */
11623 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11625 /* The function parameters must be in scope all the way until after the
11626 trailing-return-type in case of decltype. */
11627 pop_bindings_and_leave_scope ();
11630 /* Create the function call operator.
11632 Messing with declarators like this is no uglier than building up the
11633 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11634 other code. */
11636 cp_decl_specifier_seq return_type_specs;
11637 cp_declarator* declarator;
11638 tree fco;
11639 void *p;
11641 clear_decl_specs (&return_type_specs);
11642 return_type_specs.type = make_auto ();
11644 if (lambda_specs.locations[ds_constexpr])
11646 if (cxx_dialect >= cxx17)
11647 return_type_specs.locations[ds_constexpr]
11648 = lambda_specs.locations[ds_constexpr];
11649 else
11650 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11651 "lambda only available with %<-std=c++17%> or "
11652 "%<-std=gnu++17%>");
11654 if (lambda_specs.locations[ds_consteval])
11655 return_type_specs.locations[ds_consteval]
11656 = lambda_specs.locations[ds_consteval];
11658 p = obstack_alloc (&declarator_obstack, 0);
11660 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11661 LAMBDA_EXPR_LOCATION (lambda_expr));
11663 declarator = make_call_declarator (declarator, param_list, quals,
11664 VIRT_SPEC_UNSPECIFIED,
11665 REF_QUAL_NONE,
11666 tx_qual,
11667 exception_spec,
11668 return_type,
11669 trailing_requires_clause,
11670 UNKNOWN_LOCATION);
11671 declarator->std_attributes = std_attrs;
11673 fco = grokmethod (&return_type_specs,
11674 declarator,
11675 chainon (gnu_attrs, lambda_specs.attributes));
11676 if (fco != error_mark_node)
11678 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11679 DECL_ARTIFICIAL (fco) = 1;
11680 /* Give the object parameter a different name. */
11681 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11682 DECL_SET_LAMBDA_FUNCTION (fco, true);
11684 if (template_param_list)
11686 fco = finish_member_template_decl (fco);
11687 finish_template_decl (template_param_list);
11688 --parser->num_template_parameter_lists;
11690 else if (parser->fully_implicit_function_template_p)
11691 fco = finish_fully_implicit_template (parser, fco);
11693 finish_member_declaration (fco);
11695 obstack_free (&declarator_obstack, p);
11697 return (fco != error_mark_node);
11701 /* Parse the body of a lambda expression, which is simply
11703 compound-statement
11705 but which requires special handling.
11706 LAMBDA_EXPR is the current representation of the lambda expression. */
11708 static void
11709 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11711 bool nested = (current_function_decl != NULL_TREE);
11712 unsigned char local_variables_forbidden_p
11713 = parser->local_variables_forbidden_p;
11714 bool in_function_body = parser->in_function_body;
11716 /* The body of a lambda-expression is not a subexpression of the enclosing
11717 expression. */
11718 cp_evaluated ev;
11720 if (nested)
11721 push_function_context ();
11722 else
11723 /* Still increment function_depth so that we don't GC in the
11724 middle of an expression. */
11725 ++function_depth;
11727 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
11728 auto ord = make_temp_override (parser->oacc_routine, NULL);
11729 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
11730 vec<tree> omp_privatization_save;
11731 save_omp_privatization_clauses (omp_privatization_save);
11732 /* Clear this in case we're in the middle of a default argument. */
11733 parser->local_variables_forbidden_p = 0;
11734 parser->in_function_body = true;
11737 local_specialization_stack s (lss_copy);
11738 tree fco = lambda_function (lambda_expr);
11739 tree body = start_lambda_function (fco, lambda_expr);
11741 /* Originally C++11 required us to peek for 'return expr'; and
11742 process it specially here to deduce the return type. N3638
11743 removed the need for that. */
11744 cp_parser_function_body (parser, false);
11746 finish_lambda_function (body);
11749 restore_omp_privatization_clauses (omp_privatization_save);
11750 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11751 parser->in_function_body = in_function_body;
11752 if (nested)
11753 pop_function_context();
11754 else
11755 --function_depth;
11758 /* Statements [gram.stmt.stmt] */
11760 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11762 static void
11763 add_debug_begin_stmt (location_t loc)
11765 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11766 return;
11767 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11768 /* A concept is never expanded normally. */
11769 return;
11771 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11772 SET_EXPR_LOCATION (stmt, loc);
11773 add_stmt (stmt);
11776 struct cp_omp_attribute_data
11778 cp_token_cache *tokens;
11779 const c_omp_directive *dir;
11780 c_omp_directive_kind kind;
11783 /* Handle omp::directive and omp::sequence attributes in ATTRS
11784 (if any) at the start of a statement or in attribute-declaration. */
11786 static tree
11787 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
11789 if (!flag_openmp && !flag_openmp_simd)
11790 return attrs;
11792 auto_vec<cp_omp_attribute_data, 16> vec;
11793 int cnt = 0;
11794 int tokens = 0;
11795 bool bad = false;
11796 for (tree *pa = &attrs; *pa; )
11797 if (get_attribute_namespace (*pa) == omp_identifier
11798 && is_attribute_p ("directive", get_attribute_name (*pa)))
11800 cnt++;
11801 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
11803 tree d = TREE_VALUE (a);
11804 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
11805 cp_token *first = DEFPARSE_TOKENS (d)->first;
11806 cp_token *last = DEFPARSE_TOKENS (d)->last;
11807 if (parser->omp_attrs_forbidden_p)
11809 error_at (first->location,
11810 "mixing OpenMP directives with attribute and pragma "
11811 "syntax on the same statement");
11812 parser->omp_attrs_forbidden_p = false;
11813 bad = true;
11815 const char *directive[3] = {};
11816 for (int i = 0; i < 3; i++)
11818 tree id = NULL_TREE;
11819 if (first + i == last)
11820 break;
11821 if (first[i].type == CPP_NAME)
11822 id = first[i].u.value;
11823 else if (first[i].type == CPP_KEYWORD)
11824 id = ridpointers[(int) first[i].keyword];
11825 else
11826 break;
11827 directive[i] = IDENTIFIER_POINTER (id);
11829 const c_omp_directive *dir = NULL;
11830 if (directive[0])
11831 dir = c_omp_categorize_directive (directive[0], directive[1],
11832 directive[2]);
11833 if (dir == NULL)
11835 error_at (first->location,
11836 "unknown OpenMP directive name in %<omp::directive%>"
11837 " attribute argument");
11838 continue;
11840 c_omp_directive_kind kind = dir->kind;
11841 if (dir->id == PRAGMA_OMP_ORDERED)
11843 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
11844 depend clause. */
11845 if (directive[1] && strcmp (directive[1], "depend") == 0)
11846 kind = C_OMP_DIR_STANDALONE;
11847 else if (first + 2 < last
11848 && first[1].type == CPP_COMMA
11849 && first[2].type == CPP_NAME
11850 && strcmp (IDENTIFIER_POINTER (first[2].u.value),
11851 "depend") == 0)
11852 kind = C_OMP_DIR_STANDALONE;
11854 else if (dir->id == PRAGMA_OMP_ERROR)
11856 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
11857 int paren_depth = 0;
11858 for (int i = 1; first + i < last; i++)
11859 if (first[i].type == CPP_OPEN_PAREN)
11860 paren_depth++;
11861 else if (first[i].type == CPP_CLOSE_PAREN)
11862 paren_depth--;
11863 else if (paren_depth == 0
11864 && first + i + 2 < last
11865 && first[i].type == CPP_NAME
11866 && first[i + 1].type == CPP_OPEN_PAREN
11867 && first[i + 2].type == CPP_NAME
11868 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
11869 "at")
11870 && !strcmp (IDENTIFIER_POINTER (first[i
11871 + 2].u.value),
11872 "execution"))
11874 kind = C_OMP_DIR_STANDALONE;
11875 break;
11878 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
11879 vec.safe_push (v);
11880 if (flag_openmp || dir->simd)
11881 tokens += (last - first) + 1;
11883 cp_omp_attribute_data v = {};
11884 vec.safe_push (v);
11885 *pa = TREE_CHAIN (*pa);
11887 else
11888 pa = &TREE_CHAIN (*pa);
11890 if (bad)
11891 return attrs;
11893 unsigned int i;
11894 cp_omp_attribute_data *v;
11895 cp_omp_attribute_data *construct_seen = nullptr;
11896 cp_omp_attribute_data *standalone_seen = nullptr;
11897 cp_omp_attribute_data *prev_standalone_seen = nullptr;
11898 FOR_EACH_VEC_ELT (vec, i, v)
11899 if (v->tokens)
11901 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
11902 construct_seen = v;
11903 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
11904 standalone_seen = v;
11906 else
11908 if (standalone_seen && !prev_standalone_seen)
11910 prev_standalone_seen = standalone_seen;
11911 standalone_seen = nullptr;
11915 if (cnt > 1 && construct_seen)
11917 error_at (construct_seen->tokens->first->location,
11918 "OpenMP construct among %<omp::directive%> attributes"
11919 " requires all %<omp::directive%> attributes on the"
11920 " same statement to be in the same %<omp::sequence%>");
11921 return attrs;
11923 if (cnt > 1 && standalone_seen && prev_standalone_seen)
11925 error_at (standalone_seen->tokens->first->location,
11926 "multiple OpenMP standalone directives among"
11927 " %<omp::directive%> attributes must be all within the"
11928 " same %<omp::sequence%>");
11929 return attrs;
11932 if (prev_standalone_seen)
11933 standalone_seen = prev_standalone_seen;
11934 if (standalone_seen
11935 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11937 error_at (standalone_seen->tokens->first->location,
11938 "standalone OpenMP directives in %<omp::directive%> attribute"
11939 " can only appear on an empty statement");
11940 return attrs;
11942 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
11944 cp_token *token = cp_lexer_peek_token (parser->lexer);
11945 enum pragma_kind kind = cp_parser_pragma_kind (token);
11946 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
11948 error_at (token->location,
11949 "mixing OpenMP directives with attribute and pragma "
11950 "syntax on the same statement");
11951 return attrs;
11955 if (!tokens)
11956 return attrs;
11957 tokens++;
11958 cp_lexer *lexer = cp_lexer_alloc ();
11959 lexer->debugging_p = parser->lexer->debugging_p;
11960 vec_safe_reserve (lexer->buffer, tokens, true);
11961 FOR_EACH_VEC_ELT (vec, i, v)
11963 if (!v->tokens)
11964 continue;
11965 if (!flag_openmp && !v->dir->simd)
11966 continue;
11967 cp_token *first = v->tokens->first;
11968 cp_token *last = v->tokens->last;
11969 cp_token tok = {};
11970 tok.type = CPP_PRAGMA;
11971 tok.keyword = RID_MAX;
11972 tok.u.value = build_int_cst (NULL, v->dir->id);
11973 tok.location = first->location;
11974 lexer->buffer->quick_push (tok);
11975 while (++first < last)
11976 lexer->buffer->quick_push (*first);
11977 tok = {};
11978 tok.type = CPP_PRAGMA_EOL;
11979 tok.keyword = RID_MAX;
11980 tok.location = last->location;
11981 lexer->buffer->quick_push (tok);
11983 cp_token tok = {};
11984 tok.type = CPP_EOF;
11985 tok.keyword = RID_MAX;
11986 tok.location = lexer->buffer->last ().location;
11987 lexer->buffer->quick_push (tok);
11988 lexer->next = parser->lexer;
11989 lexer->next_token = lexer->buffer->address ();
11990 lexer->last_token = lexer->next_token
11991 + lexer->buffer->length ()
11992 - 1;
11993 lexer->in_omp_attribute_pragma = true;
11994 parser->lexer = lexer;
11995 /* Move the current source position to that of the first token in the
11996 new lexer. */
11997 cp_lexer_set_source_position_from_token (lexer->next_token);
11998 return attrs;
12001 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12002 (if any) at the start or after declaration-id of a declaration. */
12004 static void
12005 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12006 cp_omp_declare_simd_data *data,
12007 bool start)
12009 if (!flag_openmp && !flag_openmp_simd)
12010 return;
12012 int cnt = 0;
12013 bool bad = false;
12014 bool variant_p = false;
12015 location_t loc = UNKNOWN_LOCATION;
12016 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12017 if (get_attribute_namespace (pa) == omp_identifier
12018 && is_attribute_p ("directive", get_attribute_name (pa)))
12020 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12022 tree d = TREE_VALUE (a);
12023 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12024 cp_token *first = DEFPARSE_TOKENS (d)->first;
12025 cp_token *last = DEFPARSE_TOKENS (d)->last;
12026 const char *directive[3] = {};
12027 for (int i = 0; i < 3; i++)
12029 tree id = NULL_TREE;
12030 if (first + i == last)
12031 break;
12032 if (first[i].type == CPP_NAME)
12033 id = first[i].u.value;
12034 else if (first[i].type == CPP_KEYWORD)
12035 id = ridpointers[(int) first[i].keyword];
12036 else
12037 break;
12038 directive[i] = IDENTIFIER_POINTER (id);
12040 const c_omp_directive *dir = NULL;
12041 if (directive[0])
12042 dir = c_omp_categorize_directive (directive[0], directive[1],
12043 directive[2]);
12044 if (dir == NULL)
12045 continue;
12046 if (dir->id == PRAGMA_OMP_DECLARE
12047 && (strcmp (directive[1], "simd") == 0
12048 || strcmp (directive[1], "variant") == 0))
12050 if (cnt++ == 0)
12052 variant_p = strcmp (directive[1], "variant") == 0;
12053 loc = first->location;
12055 if (start && parser->omp_declare_simd && !bad)
12057 error_at (first->location,
12058 "mixing OpenMP directives with attribute and "
12059 "pragma syntax on the same declaration");
12060 bad = true;
12066 if (bad)
12068 for (tree *pa = pattrs; *pa; )
12069 if (get_attribute_namespace (*pa) == omp_identifier
12070 && is_attribute_p ("directive", get_attribute_name (*pa)))
12071 *pa = TREE_CHAIN (*pa);
12072 else
12073 pa = &TREE_CHAIN (*pa);
12074 return;
12076 if (cnt == 0)
12077 return;
12079 if (parser->omp_declare_simd == NULL)
12081 data->error_seen = false;
12082 data->fndecl_seen = false;
12083 data->variant_p = variant_p;
12084 data->loc = loc;
12085 data->tokens = vNULL;
12086 data->attribs[0] = NULL;
12087 data->attribs[1] = NULL;
12088 parser->omp_declare_simd = data;
12090 parser->omp_declare_simd->attribs[!start] = pattrs;
12093 /* Parse a statement.
12095 statement:
12096 labeled-statement
12097 expression-statement
12098 compound-statement
12099 selection-statement
12100 iteration-statement
12101 jump-statement
12102 declaration-statement
12103 try-block
12105 C++11:
12107 statement:
12108 labeled-statement
12109 attribute-specifier-seq (opt) expression-statement
12110 attribute-specifier-seq (opt) compound-statement
12111 attribute-specifier-seq (opt) selection-statement
12112 attribute-specifier-seq (opt) iteration-statement
12113 attribute-specifier-seq (opt) jump-statement
12114 declaration-statement
12115 attribute-specifier-seq (opt) try-block
12117 init-statement:
12118 expression-statement
12119 simple-declaration
12120 alias-declaration
12122 TM Extension:
12124 statement:
12125 atomic-statement
12127 IN_COMPOUND is true when the statement is nested inside a
12128 cp_parser_compound_statement; this matters for certain pragmas.
12130 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12131 is a (possibly labeled) if statement which is not enclosed in braces
12132 and has an else clause. This is used to implement -Wparentheses.
12134 CHAIN is a vector of if-else-if conditions. */
12136 static void
12137 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12138 bool in_compound, bool *if_p, vec<tree> *chain,
12139 location_t *loc_after_labels)
12141 tree statement, std_attrs = NULL_TREE;
12142 cp_token *token;
12143 location_t statement_location, attrs_loc;
12144 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12145 bool has_std_attrs;
12147 restart:
12148 if (if_p != NULL)
12149 *if_p = false;
12150 /* There is no statement yet. */
12151 statement = NULL_TREE;
12153 saved_token_sentinel saved_tokens (parser->lexer);
12154 token = cp_lexer_peek_token (parser->lexer);
12155 attrs_loc = token->location;
12156 if (c_dialect_objc ())
12157 /* In obj-c++, seeing '[[' might be the either the beginning of
12158 c++11 attributes, or a nested objc-message-expression. So
12159 let's parse the c++11 attributes tentatively. */
12160 cp_parser_parse_tentatively (parser);
12161 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12162 if (std_attrs)
12163 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12164 if (c_dialect_objc ())
12166 if (!cp_parser_parse_definitely (parser))
12167 std_attrs = NULL_TREE;
12169 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12171 /* Peek at the next token. */
12172 token = cp_lexer_peek_token (parser->lexer);
12173 bool omp_attrs_forbidden_p;
12174 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12176 if (std_attrs && (flag_openmp || flag_openmp_simd))
12178 bool handle_omp_attribs = false;
12179 if (token->type == CPP_KEYWORD)
12180 switch (token->keyword)
12182 case RID_IF:
12183 case RID_SWITCH:
12184 case RID_WHILE:
12185 case RID_DO:
12186 case RID_FOR:
12187 case RID_BREAK:
12188 case RID_CONTINUE:
12189 case RID_RETURN:
12190 case RID_CO_RETURN:
12191 case RID_GOTO:
12192 case RID_AT_TRY:
12193 case RID_AT_CATCH:
12194 case RID_AT_FINALLY:
12195 case RID_AT_SYNCHRONIZED:
12196 case RID_AT_THROW:
12197 case RID_TRY:
12198 case RID_TRANSACTION_ATOMIC:
12199 case RID_TRANSACTION_RELAXED:
12200 case RID_SYNCHRONIZED:
12201 case RID_ATOMIC_NOEXCEPT:
12202 case RID_ATOMIC_CANCEL:
12203 case RID_TRANSACTION_CANCEL:
12204 handle_omp_attribs = true;
12205 break;
12206 default:
12207 break;
12209 else if (token->type == CPP_SEMICOLON
12210 || token->type == CPP_OPEN_BRACE
12211 || token->type == CPP_PRAGMA)
12212 handle_omp_attribs = true;
12213 if (handle_omp_attribs)
12215 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12216 std_attrs);
12217 token = cp_lexer_peek_token (parser->lexer);
12220 parser->omp_attrs_forbidden_p = false;
12222 /* Remember the location of the first token in the statement. */
12223 cp_token *statement_token = token;
12224 statement_location = token->location;
12225 add_debug_begin_stmt (statement_location);
12226 /* If this is a keyword, then that will often determine what kind of
12227 statement we have. */
12228 if (token->type == CPP_KEYWORD)
12230 enum rid keyword = token->keyword;
12232 switch (keyword)
12234 case RID_CASE:
12235 case RID_DEFAULT:
12236 /* Looks like a labeled-statement with a case label.
12237 Parse the label, and then use tail recursion to parse
12238 the statement. */
12239 cp_parser_label_for_labeled_statement (parser, std_attrs);
12240 in_compound = false;
12241 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12242 goto restart;
12244 case RID_IF:
12245 case RID_SWITCH:
12246 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12247 statement = cp_parser_selection_statement (parser, if_p, chain);
12248 break;
12250 case RID_WHILE:
12251 case RID_DO:
12252 case RID_FOR:
12253 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12254 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
12255 break;
12257 case RID_BREAK:
12258 case RID_CONTINUE:
12259 case RID_RETURN:
12260 case RID_CO_RETURN:
12261 case RID_GOTO:
12262 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12263 statement = cp_parser_jump_statement (parser);
12264 break;
12266 /* Objective-C++ exception-handling constructs. */
12267 case RID_AT_TRY:
12268 case RID_AT_CATCH:
12269 case RID_AT_FINALLY:
12270 case RID_AT_SYNCHRONIZED:
12271 case RID_AT_THROW:
12272 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12273 statement = cp_parser_objc_statement (parser);
12274 break;
12276 case RID_TRY:
12277 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12278 statement = cp_parser_try_block (parser);
12279 break;
12281 case RID_NAMESPACE:
12282 /* This must be a namespace alias definition. */
12283 if (has_std_attrs)
12285 /* Attributes should be parsed as part of the
12286 declaration, so let's un-parse them. */
12287 saved_tokens.rollback();
12288 std_attrs = NULL_TREE;
12290 cp_parser_declaration_statement (parser);
12291 return;
12293 case RID_TRANSACTION_ATOMIC:
12294 case RID_TRANSACTION_RELAXED:
12295 case RID_SYNCHRONIZED:
12296 case RID_ATOMIC_NOEXCEPT:
12297 case RID_ATOMIC_CANCEL:
12298 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12299 statement = cp_parser_transaction (parser, token);
12300 break;
12301 case RID_TRANSACTION_CANCEL:
12302 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12303 statement = cp_parser_transaction_cancel (parser);
12304 break;
12306 default:
12307 /* It might be a keyword like `int' that can start a
12308 declaration-statement. */
12309 break;
12312 else if (token->type == CPP_NAME)
12314 /* If the next token is a `:', then we are looking at a
12315 labeled-statement. */
12316 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12317 if (token->type == CPP_COLON)
12319 /* Looks like a labeled-statement with an ordinary label.
12320 Parse the label, and then use tail recursion to parse
12321 the statement. */
12323 cp_parser_label_for_labeled_statement (parser, std_attrs);
12324 in_compound = false;
12325 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12326 goto restart;
12329 /* Anything that starts with a `{' must be a compound-statement. */
12330 else if (token->type == CPP_OPEN_BRACE)
12332 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12333 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12335 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12336 a statement all its own. */
12337 else if (token->type == CPP_PRAGMA)
12339 do_pragma:;
12340 cp_lexer *lexer = parser->lexer;
12341 bool do_restart = false;
12342 /* Only certain OpenMP pragmas are attached to statements, and thus
12343 are considered statements themselves. All others are not. In
12344 the context of a compound, accept the pragma as a "statement" and
12345 return so that we can check for a close brace. Otherwise we
12346 require a real statement and must go back and read one. */
12347 if (in_compound)
12348 cp_parser_pragma (parser, pragma_compound, if_p);
12349 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12350 do_restart = true;
12351 if (parser->lexer != lexer
12352 && lexer->in_omp_attribute_pragma
12353 && (!in_omp_attribute_pragma || lexer->orphan_p))
12355 if (saved_tokens.lexer == lexer)
12357 if (saved_tokens.commit)
12358 cp_lexer_commit_tokens (lexer);
12359 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12360 saved_tokens.lexer = parser->lexer;
12361 saved_tokens.commit = false;
12362 saved_tokens.len = parser->lexer->saved_tokens.length ();
12364 cp_lexer_destroy (lexer);
12365 lexer = parser->lexer;
12367 if (do_restart)
12368 goto restart;
12369 if (parser->lexer == lexer
12370 && lexer->in_omp_attribute_pragma
12371 && !in_omp_attribute_pragma)
12372 parser->lexer->orphan_p = true;
12373 return;
12375 else if (token->type == CPP_EOF)
12377 cp_parser_error (parser, "expected statement");
12378 return;
12381 /* Everything else must be a declaration-statement or an
12382 expression-statement. Try for the declaration-statement
12383 first, unless we are looking at a `;', in which case we know that
12384 we have an expression-statement. */
12385 if (!statement)
12387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12389 if (has_std_attrs)
12390 /* Attributes should be parsed as part of the declaration,
12391 so let's un-parse them. */
12392 saved_tokens.rollback();
12394 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12395 cp_parser_parse_tentatively (parser);
12396 /* Try to parse the declaration-statement. */
12397 cp_parser_declaration_statement (parser);
12398 parser->omp_attrs_forbidden_p = false;
12399 /* If that worked, we're done. */
12400 if (cp_parser_parse_definitely (parser))
12401 return;
12402 /* It didn't work, restore the post-attribute position. */
12403 if (has_std_attrs)
12405 cp_lexer_set_token_position (parser->lexer, statement_token);
12406 if (flag_openmp || flag_openmp_simd)
12408 size_t i = 1;
12409 bool handle_omp_attribs = true;
12410 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12411 == RID_EXTENSION)
12412 i++;
12413 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12415 case RID_ASM:
12416 case RID_NAMESPACE:
12417 case RID_USING:
12418 case RID_LABEL:
12419 case RID_STATIC_ASSERT:
12420 /* Don't handle OpenMP attribs on keywords that
12421 always start a declaration statement but don't
12422 accept attribute before it and therefore
12423 the tentative cp_parser_declaration_statement
12424 fails to parse because of that. */
12425 handle_omp_attribs = false;
12426 break;
12427 default:
12428 break;
12431 if (handle_omp_attribs)
12433 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12434 std_attrs
12435 = cp_parser_handle_statement_omp_attributes
12436 (parser, std_attrs);
12437 parser->omp_attrs_forbidden_p = false;
12438 token = cp_lexer_peek_token (parser->lexer);
12439 if (token->type == CPP_PRAGMA)
12440 goto do_pragma;
12445 /* All preceding labels have been parsed at this point. */
12446 if (loc_after_labels != NULL)
12447 *loc_after_labels = statement_location;
12449 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12451 /* Look for an expression-statement instead. */
12452 statement = cp_parser_expression_statement (parser, in_statement_expr);
12454 /* Handle [[fallthrough]];. */
12455 if (attribute_fallthrough_p (std_attrs))
12457 /* The next token after the fallthrough attribute is ';'. */
12458 if (statement == NULL_TREE)
12460 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12461 statement = build_call_expr_internal_loc (statement_location,
12462 IFN_FALLTHROUGH,
12463 void_type_node, 0);
12464 finish_expr_stmt (statement);
12466 else
12467 warning_at (statement_location, OPT_Wattributes,
12468 "%<fallthrough%> attribute not followed by %<;%>");
12469 std_attrs = NULL_TREE;
12473 /* Set the line number for the statement. */
12474 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12475 SET_EXPR_LOCATION (statement, statement_location);
12477 /* Allow "[[fallthrough]];", but warn otherwise. */
12478 if (std_attrs != NULL_TREE)
12479 warning_at (attrs_loc,
12480 OPT_Wattributes,
12481 "attributes at the beginning of statement are ignored");
12484 /* Append ATTR to attribute list ATTRS. */
12486 static tree
12487 attr_chainon (tree attrs, tree attr)
12489 if (attrs == error_mark_node)
12490 return error_mark_node;
12491 if (attr == error_mark_node)
12492 return error_mark_node;
12493 return chainon (attrs, attr);
12496 /* Parse the label for a labeled-statement, i.e.
12498 identifier :
12499 case constant-expression :
12500 default :
12502 GNU Extension:
12503 case constant-expression ... constant-expression : statement
12505 When a label is parsed without errors, the label is added to the
12506 parse tree by the finish_* functions, so this function doesn't
12507 have to return the label. */
12509 static void
12510 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12512 cp_token *token;
12513 tree label = NULL_TREE;
12514 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12516 /* The next token should be an identifier. */
12517 token = cp_lexer_peek_token (parser->lexer);
12518 if (token->type != CPP_NAME
12519 && token->type != CPP_KEYWORD)
12521 cp_parser_error (parser, "expected labeled-statement");
12522 return;
12525 /* Remember whether this case or a user-defined label is allowed to fall
12526 through to. */
12527 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12529 parser->colon_corrects_to_scope_p = false;
12530 switch (token->keyword)
12532 case RID_CASE:
12534 tree expr, expr_hi;
12535 cp_token *ellipsis;
12537 /* Consume the `case' token. */
12538 cp_lexer_consume_token (parser->lexer);
12539 /* Parse the constant-expression. */
12540 expr = cp_parser_constant_expression (parser);
12541 if (check_for_bare_parameter_packs (expr))
12542 expr = error_mark_node;
12544 ellipsis = cp_lexer_peek_token (parser->lexer);
12545 if (ellipsis->type == CPP_ELLIPSIS)
12547 /* Consume the `...' token. */
12548 cp_lexer_consume_token (parser->lexer);
12549 expr_hi = cp_parser_constant_expression (parser);
12550 if (check_for_bare_parameter_packs (expr_hi))
12551 expr_hi = error_mark_node;
12553 /* We don't need to emit warnings here, as the common code
12554 will do this for us. */
12556 else
12557 expr_hi = NULL_TREE;
12559 if (parser->in_switch_statement_p)
12561 tree l = finish_case_label (token->location, expr, expr_hi);
12562 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12564 label = CASE_LABEL (l);
12565 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12568 else
12569 error_at (token->location,
12570 "case label %qE not within a switch statement",
12571 expr);
12573 break;
12575 case RID_DEFAULT:
12576 /* Consume the `default' token. */
12577 cp_lexer_consume_token (parser->lexer);
12579 if (parser->in_switch_statement_p)
12581 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
12582 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12584 label = CASE_LABEL (l);
12585 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12588 else
12589 error_at (token->location, "case label not within a switch statement");
12590 break;
12592 default:
12593 /* Anything else must be an ordinary label. */
12594 label = finish_label_stmt (cp_parser_identifier (parser));
12595 if (label && TREE_CODE (label) == LABEL_DECL)
12596 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12597 break;
12600 /* Require the `:' token. */
12601 cp_parser_require (parser, CPP_COLON, RT_COLON);
12603 /* An ordinary label may optionally be followed by attributes.
12604 However, this is only permitted if the attributes are then
12605 followed by a semicolon. This is because, for backward
12606 compatibility, when parsing
12607 lab: __attribute__ ((unused)) int i;
12608 we want the attribute to attach to "i", not "lab". */
12609 if (label != NULL_TREE
12610 && cp_next_tokens_can_be_gnu_attribute_p (parser))
12612 tree attrs;
12613 cp_parser_parse_tentatively (parser);
12614 attrs = cp_parser_gnu_attributes_opt (parser);
12615 if (attrs == NULL_TREE
12616 /* And fallthrough always binds to the expression-statement. */
12617 || attribute_fallthrough_p (attrs)
12618 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12619 cp_parser_abort_tentative_parse (parser);
12620 else if (!cp_parser_parse_definitely (parser))
12622 else
12623 attributes = attr_chainon (attributes, attrs);
12626 if (attributes != NULL_TREE)
12627 cplus_decl_attributes (&label, attributes, 0);
12629 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12632 /* Parse an expression-statement.
12634 expression-statement:
12635 expression [opt] ;
12637 Returns the new EXPR_STMT -- or NULL_TREE if the expression
12638 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12639 indicates whether this expression-statement is part of an
12640 expression statement. */
12642 static tree
12643 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
12645 tree statement = NULL_TREE;
12646 cp_token *token = cp_lexer_peek_token (parser->lexer);
12647 location_t loc = token->location;
12649 /* There might be attribute fallthrough. */
12650 tree attr = cp_parser_gnu_attributes_opt (parser);
12652 /* If the next token is a ';', then there is no expression
12653 statement. */
12654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12656 statement = cp_parser_expression (parser);
12657 if (statement == error_mark_node
12658 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12660 cp_parser_skip_to_end_of_block_or_statement (parser);
12661 return error_mark_node;
12665 /* Handle [[fallthrough]];. */
12666 if (attribute_fallthrough_p (attr))
12668 /* The next token after the fallthrough attribute is ';'. */
12669 if (statement == NULL_TREE)
12670 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12671 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
12672 void_type_node, 0);
12673 else
12674 warning_at (loc, OPT_Wattributes,
12675 "%<fallthrough%> attribute not followed by %<;%>");
12676 attr = NULL_TREE;
12679 /* Allow "[[fallthrough]];", but warn otherwise. */
12680 if (attr != NULL_TREE)
12681 warning_at (loc, OPT_Wattributes,
12682 "attributes at the beginning of statement are ignored");
12684 /* Give a helpful message for "A<T>::type t;" and the like. */
12685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
12686 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12688 if (TREE_CODE (statement) == SCOPE_REF)
12689 error_at (token->location, "need %<typename%> before %qE because "
12690 "%qT is a dependent scope",
12691 statement, TREE_OPERAND (statement, 0));
12692 else if (is_overloaded_fn (statement)
12693 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
12695 /* A::A a; */
12696 tree fn = get_first_fn (statement);
12697 error_at (token->location,
12698 "%<%T::%D%> names the constructor, not the type",
12699 DECL_CONTEXT (fn), DECL_NAME (fn));
12703 /* Consume the final `;'. */
12704 cp_parser_consume_semicolon_at_end_of_statement (parser);
12706 if (in_statement_expr
12707 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12708 /* This is the final expression statement of a statement
12709 expression. */
12710 statement = finish_stmt_expr_expr (statement, in_statement_expr);
12711 else if (statement)
12712 statement = finish_expr_stmt (statement);
12714 return statement;
12717 /* Parse a compound-statement.
12719 compound-statement:
12720 { statement-seq [opt] }
12722 GNU extension:
12724 compound-statement:
12725 { label-declaration-seq [opt] statement-seq [opt] }
12727 label-declaration-seq:
12728 label-declaration
12729 label-declaration-seq label-declaration
12731 Returns a tree representing the statement. */
12733 static tree
12734 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
12735 int bcs_flags, bool function_body)
12737 tree compound_stmt;
12738 matching_braces braces;
12740 /* Consume the `{'. */
12741 if (!braces.require_open (parser))
12742 return error_mark_node;
12743 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
12744 && !function_body && cxx_dialect < cxx14)
12745 pedwarn (input_location, OPT_Wpedantic,
12746 "compound-statement in %<constexpr%> function");
12747 /* Begin the compound-statement. */
12748 compound_stmt = begin_compound_stmt (bcs_flags);
12749 /* If the next keyword is `__label__' we have a label declaration. */
12750 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12751 cp_parser_label_declaration (parser);
12752 /* Parse an (optional) statement-seq. */
12753 cp_parser_statement_seq_opt (parser, in_statement_expr);
12755 if (function_body)
12756 maybe_splice_retval_cleanup (compound_stmt);
12758 /* Consume the `}'. */
12759 braces.require_close (parser);
12761 /* Finish the compound-statement. */
12762 finish_compound_stmt (compound_stmt);
12764 return compound_stmt;
12767 /* Parse an (optional) statement-seq.
12769 statement-seq:
12770 statement
12771 statement-seq [opt] statement */
12773 static void
12774 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
12776 /* Scan statements until there aren't any more. */
12777 while (true)
12779 cp_token *token = cp_lexer_peek_token (parser->lexer);
12781 /* If we are looking at a `}', then we have run out of
12782 statements; the same is true if we have reached the end
12783 of file, or have stumbled upon a stray '@end'. */
12784 if (token->type == CPP_CLOSE_BRACE
12785 || token->type == CPP_EOF
12786 || token->type == CPP_PRAGMA_EOL
12787 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
12788 break;
12790 /* If we are in a compound statement and find 'else' then
12791 something went wrong. */
12792 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
12794 if (parser->in_statement & IN_IF_STMT)
12795 break;
12796 else
12798 token = cp_lexer_consume_token (parser->lexer);
12799 error_at (token->location, "%<else%> without a previous %<if%>");
12803 /* Parse the statement. */
12804 cp_parser_statement (parser, in_statement_expr, true, NULL);
12808 /* Return true if this is the C++20 version of range-based-for with
12809 init-statement. */
12811 static bool
12812 cp_parser_range_based_for_with_init_p (cp_parser *parser)
12814 bool r = false;
12816 /* Save tokens so that we can put them back. */
12817 cp_lexer_save_tokens (parser->lexer);
12819 /* There has to be an unnested ; followed by an unnested :. */
12820 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
12821 /*recovering=*/false,
12822 CPP_SEMICOLON,
12823 /*consume_paren=*/false) != -1)
12824 goto out;
12826 /* We found the semicolon, eat it now. */
12827 cp_lexer_consume_token (parser->lexer);
12829 /* Now look for ':' that is not nested in () or {}. */
12830 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
12831 /*recovering=*/false,
12832 CPP_COLON,
12833 /*consume_paren=*/false) == -1);
12835 out:
12836 /* Roll back the tokens we skipped. */
12837 cp_lexer_rollback_tokens (parser->lexer);
12839 return r;
12842 /* Return true if we're looking at (init; cond), false otherwise. */
12844 static bool
12845 cp_parser_init_statement_p (cp_parser *parser)
12847 /* Save tokens so that we can put them back. */
12848 cp_lexer_save_tokens (parser->lexer);
12850 /* Look for ';' that is not nested in () or {}. */
12851 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
12852 /*recovering=*/false,
12853 CPP_SEMICOLON,
12854 /*consume_paren=*/false);
12856 /* Roll back the tokens we skipped. */
12857 cp_lexer_rollback_tokens (parser->lexer);
12859 return ret == -1;
12862 /* Parse a selection-statement.
12864 selection-statement:
12865 if ( init-statement [opt] condition ) statement
12866 if ( init-statement [opt] condition ) statement else statement
12867 switch ( init-statement [opt] condition ) statement
12869 Returns the new IF_STMT or SWITCH_STMT.
12871 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12872 is a (possibly labeled) if statement which is not enclosed in
12873 braces and has an else clause. This is used to implement
12874 -Wparentheses.
12876 CHAIN is a vector of if-else-if conditions. This is used to implement
12877 -Wduplicated-cond. */
12879 static tree
12880 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
12881 vec<tree> *chain)
12883 cp_token *token;
12884 enum rid keyword;
12885 token_indent_info guard_tinfo;
12887 if (if_p != NULL)
12888 *if_p = false;
12890 /* Peek at the next token. */
12891 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
12892 guard_tinfo = get_token_indent_info (token);
12894 /* See what kind of keyword it is. */
12895 keyword = token->keyword;
12896 switch (keyword)
12898 case RID_IF:
12899 case RID_SWITCH:
12901 tree statement;
12902 tree condition;
12904 bool cx = false;
12905 if (keyword == RID_IF
12906 && cp_lexer_next_token_is_keyword (parser->lexer,
12907 RID_CONSTEXPR))
12909 cx = true;
12910 cp_token *tok = cp_lexer_consume_token (parser->lexer);
12911 if (cxx_dialect < cxx17)
12912 pedwarn (tok->location, OPT_Wc__17_extensions,
12913 "%<if constexpr%> only available with "
12914 "%<-std=c++17%> or %<-std=gnu++17%>");
12916 int ce = 0;
12917 if (keyword == RID_IF && !cx)
12919 if (cp_lexer_next_token_is_keyword (parser->lexer,
12920 RID_CONSTEVAL))
12921 ce = 1;
12922 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
12923 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
12924 RID_CONSTEVAL))
12926 ce = -1;
12927 cp_lexer_consume_token (parser->lexer);
12930 if (ce)
12932 cp_token *tok = cp_lexer_consume_token (parser->lexer);
12933 if (cxx_dialect < cxx23)
12934 pedwarn (tok->location, OPT_Wc__23_extensions,
12935 "%<if consteval%> only available with "
12936 "%<-std=c++2b%> or %<-std=gnu++2b%>");
12938 bool save_in_consteval_if_p = in_consteval_if_p;
12939 statement = begin_if_stmt ();
12940 IF_STMT_CONSTEVAL_P (statement) = true;
12941 condition = finish_if_stmt_cond (boolean_false_node, statement);
12943 gcc_rich_location richloc (tok->location);
12944 bool non_compound_stmt_p = false;
12945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12947 non_compound_stmt_p = true;
12948 richloc.add_fixit_insert_after (tok->location, "{");
12951 in_consteval_if_p |= ce > 0;
12952 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12954 if (non_compound_stmt_p)
12956 location_t before_loc
12957 = cp_lexer_peek_token (parser->lexer)->location;
12958 richloc.add_fixit_insert_before (before_loc, "}");
12959 error_at (&richloc,
12960 "%<if consteval%> requires compound statement");
12961 non_compound_stmt_p = false;
12964 finish_then_clause (statement);
12966 /* If the next token is `else', parse the else-clause. */
12967 if (cp_lexer_next_token_is_keyword (parser->lexer,
12968 RID_ELSE))
12970 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
12971 gcc_rich_location else_richloc (else_tok->location);
12972 guard_tinfo = get_token_indent_info (else_tok);
12973 /* Consume the `else' keyword. */
12974 cp_lexer_consume_token (parser->lexer);
12976 begin_else_clause (statement);
12978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12980 non_compound_stmt_p = true;
12981 else_richloc.add_fixit_insert_after (else_tok->location,
12982 "{");
12985 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
12986 cp_parser_implicitly_scoped_statement (parser, NULL,
12987 guard_tinfo);
12989 if (non_compound_stmt_p)
12991 location_t before_loc
12992 = cp_lexer_peek_token (parser->lexer)->location;
12993 else_richloc.add_fixit_insert_before (before_loc, "}");
12994 error_at (&else_richloc,
12995 "%<if consteval%> requires compound statement");
12998 finish_else_clause (statement);
13001 in_consteval_if_p = save_in_consteval_if_p;
13002 if (ce < 0)
13004 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13005 if (THEN_CLAUSE (statement) == NULL_TREE)
13006 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13009 finish_if_stmt (statement);
13010 return statement;
13013 /* Look for the `('. */
13014 matching_parens parens;
13015 if (!parens.require_open (parser))
13017 cp_parser_skip_to_end_of_statement (parser);
13018 return error_mark_node;
13021 /* Begin the selection-statement. */
13022 if (keyword == RID_IF)
13024 statement = begin_if_stmt ();
13025 IF_STMT_CONSTEXPR_P (statement) = cx;
13027 else
13028 statement = begin_switch_stmt ();
13030 /* Parse the optional init-statement. */
13031 if (cp_parser_init_statement_p (parser))
13033 tree decl;
13034 if (cxx_dialect < cxx17)
13035 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13036 OPT_Wc__17_extensions,
13037 "init-statement in selection statements only available "
13038 "with %<-std=c++17%> or %<-std=gnu++17%>");
13039 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13040 /* A non-empty init-statement can have arbitrary side
13041 effects. */
13042 vec_free (chain);
13043 cp_parser_init_statement (parser, &decl);
13046 /* Parse the condition. */
13047 condition = cp_parser_condition (parser);
13048 /* Look for the `)'. */
13049 if (!parens.require_close (parser))
13050 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13051 /*consume_paren=*/true);
13053 if (keyword == RID_IF)
13055 bool nested_if;
13056 unsigned char in_statement;
13058 /* Add the condition. */
13059 condition = finish_if_stmt_cond (condition, statement);
13061 if (warn_duplicated_cond)
13062 warn_duplicated_cond_add_or_warn (token->location, condition,
13063 &chain);
13065 /* Parse the then-clause. */
13066 in_statement = parser->in_statement;
13067 parser->in_statement |= IN_IF_STMT;
13069 /* Outside a template, the non-selected branch of a constexpr
13070 if is a 'discarded statement', i.e. unevaluated. */
13071 bool was_discarded = in_discarded_stmt;
13072 bool discard_then = (cx && !processing_template_decl
13073 && integer_zerop (condition));
13074 if (discard_then)
13076 in_discarded_stmt = true;
13077 ++c_inhibit_evaluation_warnings;
13080 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13081 guard_tinfo);
13083 parser->in_statement = in_statement;
13085 finish_then_clause (statement);
13087 if (discard_then)
13089 THEN_CLAUSE (statement) = NULL_TREE;
13090 in_discarded_stmt = was_discarded;
13091 --c_inhibit_evaluation_warnings;
13094 /* If the next token is `else', parse the else-clause. */
13095 if (cp_lexer_next_token_is_keyword (parser->lexer,
13096 RID_ELSE))
13098 bool discard_else = (cx && !processing_template_decl
13099 && integer_nonzerop (condition));
13100 if (discard_else)
13102 in_discarded_stmt = true;
13103 ++c_inhibit_evaluation_warnings;
13106 guard_tinfo
13107 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13108 /* Consume the `else' keyword. */
13109 cp_lexer_consume_token (parser->lexer);
13110 if (warn_duplicated_cond)
13112 if (cp_lexer_next_token_is_keyword (parser->lexer,
13113 RID_IF)
13114 && chain == NULL)
13116 /* We've got "if (COND) else if (COND2)". Start
13117 the condition chain and add COND as the first
13118 element. */
13119 chain = new vec<tree> ();
13120 if (!CONSTANT_CLASS_P (condition)
13121 && !TREE_SIDE_EFFECTS (condition))
13123 /* Wrap it in a NOP_EXPR so that we can set the
13124 location of the condition. */
13125 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13126 condition);
13127 SET_EXPR_LOCATION (e, token->location);
13128 chain->safe_push (e);
13131 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13132 RID_IF))
13133 /* This is if-else without subsequent if. Zap the
13134 condition chain; we would have already warned at
13135 this point. */
13136 vec_free (chain);
13138 begin_else_clause (statement);
13139 /* Parse the else-clause. */
13140 cp_parser_implicitly_scoped_statement (parser, NULL,
13141 guard_tinfo, chain);
13143 finish_else_clause (statement);
13145 /* If we are currently parsing a then-clause, then
13146 IF_P will not be NULL. We set it to true to
13147 indicate that this if statement has an else clause.
13148 This may trigger the Wparentheses warning below
13149 when we get back up to the parent if statement. */
13150 if (if_p != NULL)
13151 *if_p = true;
13153 if (discard_else)
13155 ELSE_CLAUSE (statement) = NULL_TREE;
13156 in_discarded_stmt = was_discarded;
13157 --c_inhibit_evaluation_warnings;
13160 else
13162 /* This if statement does not have an else clause. If
13163 NESTED_IF is true, then the then-clause has an if
13164 statement which does have an else clause. We warn
13165 about the potential ambiguity. */
13166 if (nested_if)
13167 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13168 "suggest explicit braces to avoid ambiguous"
13169 " %<else%>");
13170 if (warn_duplicated_cond)
13171 /* We don't need the condition chain anymore. */
13172 vec_free (chain);
13175 /* Now we're all done with the if-statement. */
13176 finish_if_stmt (statement);
13178 else
13180 bool in_switch_statement_p;
13181 unsigned char in_statement;
13183 /* Add the condition. */
13184 finish_switch_cond (condition, statement);
13186 /* Parse the body of the switch-statement. */
13187 in_switch_statement_p = parser->in_switch_statement_p;
13188 in_statement = parser->in_statement;
13189 parser->in_switch_statement_p = true;
13190 parser->in_statement |= IN_SWITCH_STMT;
13191 cp_parser_implicitly_scoped_statement (parser, if_p,
13192 guard_tinfo);
13193 parser->in_switch_statement_p = in_switch_statement_p;
13194 parser->in_statement = in_statement;
13196 /* Now we're all done with the switch-statement. */
13197 finish_switch_stmt (statement);
13200 return statement;
13202 break;
13204 default:
13205 cp_parser_error (parser, "expected selection-statement");
13206 return error_mark_node;
13210 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13211 If we have seen at least one decl-specifier, and the next token is not
13212 a parenthesis (after "int (" we might be looking at a functional cast)
13213 neither we are dealing with a concept-check expression then we must be
13214 looking at a declaration. */
13216 static void
13217 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13218 cp_decl_specifier_seq *decl_specs)
13220 if (decl_specs->any_specifiers_p
13221 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13222 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13223 && !cp_parser_error_occurred (parser)
13224 && !(decl_specs->type
13225 && TREE_CODE (decl_specs->type) == TYPE_DECL
13226 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13227 cp_parser_commit_to_tentative_parse (parser);
13230 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13231 The declarator shall not specify a function or an array. Returns
13232 TRUE if the declarator is valid, FALSE otherwise. */
13234 static bool
13235 cp_parser_check_condition_declarator (cp_parser* parser,
13236 cp_declarator *declarator,
13237 location_t loc)
13239 if (declarator == cp_error_declarator
13240 || function_declarator_p (declarator)
13241 || declarator->kind == cdk_array)
13243 if (declarator == cp_error_declarator)
13244 /* Already complained. */;
13245 else if (declarator->kind == cdk_array)
13246 error_at (loc, "condition declares an array");
13247 else
13248 error_at (loc, "condition declares a function");
13249 if (parser->fully_implicit_function_template_p)
13250 abort_fully_implicit_template (parser);
13251 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13252 /*or_comma=*/false,
13253 /*consume_paren=*/false);
13254 return false;
13256 else
13257 return true;
13260 /* Parse a condition.
13262 condition:
13263 expression
13264 type-specifier-seq declarator = initializer-clause
13265 type-specifier-seq declarator braced-init-list
13267 GNU Extension:
13269 condition:
13270 type-specifier-seq declarator asm-specification [opt]
13271 attributes [opt] = assignment-expression
13273 Returns the expression that should be tested. */
13275 static tree
13276 cp_parser_condition (cp_parser* parser)
13278 cp_decl_specifier_seq type_specifiers;
13279 const char *saved_message;
13280 int declares_class_or_enum;
13282 /* Try the declaration first. */
13283 cp_parser_parse_tentatively (parser);
13284 /* New types are not allowed in the type-specifier-seq for a
13285 condition. */
13286 saved_message = parser->type_definition_forbidden_message;
13287 parser->type_definition_forbidden_message
13288 = G_("types may not be defined in conditions");
13289 /* Parse the type-specifier-seq. */
13290 cp_parser_decl_specifier_seq (parser,
13291 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13292 &type_specifiers,
13293 &declares_class_or_enum);
13294 /* Restore the saved message. */
13295 parser->type_definition_forbidden_message = saved_message;
13297 /* Gather the attributes that were provided with the
13298 decl-specifiers. */
13299 tree prefix_attributes = type_specifiers.attributes;
13301 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13303 /* If all is well, we might be looking at a declaration. */
13304 if (!cp_parser_error_occurred (parser))
13306 tree decl;
13307 tree asm_specification;
13308 tree attributes;
13309 cp_declarator *declarator;
13310 tree initializer = NULL_TREE;
13311 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13313 /* Parse the declarator. */
13314 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13315 CP_PARSER_FLAGS_NONE,
13316 /*ctor_dtor_or_conv_p=*/NULL,
13317 /*parenthesized_p=*/NULL,
13318 /*member_p=*/false,
13319 /*friend_p=*/false,
13320 /*static_p=*/false);
13321 /* Parse the attributes. */
13322 attributes = cp_parser_attributes_opt (parser);
13323 /* Parse the asm-specification. */
13324 asm_specification = cp_parser_asm_specification_opt (parser);
13325 /* If the next token is not an `=' or '{', then we might still be
13326 looking at an expression. For example:
13328 if (A(a).x)
13330 looks like a decl-specifier-seq and a declarator -- but then
13331 there is no `=', so this is an expression. */
13332 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13333 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13334 cp_parser_simulate_error (parser);
13336 /* If we did see an `=' or '{', then we are looking at a declaration
13337 for sure. */
13338 if (cp_parser_parse_definitely (parser))
13340 tree pushed_scope;
13341 bool non_constant_p = false;
13342 int flags = LOOKUP_ONLYCONVERTING;
13344 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13345 return error_mark_node;
13347 /* Create the declaration. */
13348 decl = start_decl (declarator, &type_specifiers,
13349 /*initialized_p=*/true,
13350 attributes, prefix_attributes,
13351 &pushed_scope);
13353 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13354 /* Parse the initializer. */
13355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13357 initializer = cp_parser_braced_list (parser, &non_constant_p);
13358 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13359 flags = 0;
13361 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13363 /* Consume the `='. */
13364 cp_lexer_consume_token (parser->lexer);
13365 initializer = cp_parser_initializer_clause (parser,
13366 &non_constant_p);
13368 else
13370 cp_parser_error (parser, "expected initializer");
13371 initializer = error_mark_node;
13373 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13374 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13376 /* Process the initializer. */
13377 cp_finish_decl (decl,
13378 initializer, !non_constant_p,
13379 asm_specification,
13380 flags);
13382 if (pushed_scope)
13383 pop_scope (pushed_scope);
13385 return convert_from_reference (decl);
13388 /* If we didn't even get past the declarator successfully, we are
13389 definitely not looking at a declaration. */
13390 else
13391 cp_parser_abort_tentative_parse (parser);
13393 /* Otherwise, we are looking at an expression. */
13394 return cp_parser_expression (parser);
13397 /* Parses a for-statement or range-for-statement until the closing ')',
13398 not included. */
13400 static tree
13401 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
13403 tree init, scope, decl;
13404 bool is_range_for;
13406 /* Begin the for-statement. */
13407 scope = begin_for_scope (&init);
13409 /* Maybe parse the optional init-statement in a range-based for loop. */
13410 if (cp_parser_range_based_for_with_init_p (parser)
13411 /* Checked for diagnostic purposes only. */
13412 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13414 tree dummy;
13415 cp_parser_init_statement (parser, &dummy);
13416 if (cxx_dialect < cxx20)
13418 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13419 OPT_Wc__20_extensions,
13420 "range-based %<for%> loops with initializer only "
13421 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13422 decl = error_mark_node;
13426 /* Parse the initialization. */
13427 is_range_for = cp_parser_init_statement (parser, &decl);
13429 if (is_range_for)
13430 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13431 false);
13432 else
13433 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
13436 static tree
13437 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13438 unsigned short unroll)
13440 /* Normal for loop */
13441 tree condition = NULL_TREE;
13442 tree expression = NULL_TREE;
13443 tree stmt;
13445 stmt = begin_for_stmt (scope, init);
13446 /* The init-statement has already been parsed in
13447 cp_parser_init_statement, so no work is needed here. */
13448 finish_init_stmt (stmt);
13450 /* If there's a condition, process it. */
13451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13452 condition = cp_parser_condition (parser);
13453 else if (ivdep)
13455 cp_parser_error (parser, "missing loop condition in loop with "
13456 "%<GCC ivdep%> pragma");
13457 condition = error_mark_node;
13459 else if (unroll)
13461 cp_parser_error (parser, "missing loop condition in loop with "
13462 "%<GCC unroll%> pragma");
13463 condition = error_mark_node;
13465 finish_for_cond (condition, stmt, ivdep, unroll);
13466 /* Look for the `;'. */
13467 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13469 /* If there's an expression, process it. */
13470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13471 expression = cp_parser_expression (parser);
13472 finish_for_expr (expression, stmt);
13474 return stmt;
13477 /* Tries to parse a range-based for-statement:
13479 range-based-for:
13480 decl-specifier-seq declarator : expression
13482 The decl-specifier-seq declarator and the `:' are already parsed by
13483 cp_parser_init_statement. If processing_template_decl it returns a
13484 newly created RANGE_FOR_STMT; if not, it is converted to a
13485 regular FOR_STMT. */
13487 static tree
13488 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
13489 bool ivdep, unsigned short unroll, bool is_omp)
13491 tree stmt, range_expr;
13492 auto_vec <cxx_binding *, 16> bindings;
13493 auto_vec <tree, 16> names;
13494 tree decomp_first_name = NULL_TREE;
13495 unsigned int decomp_cnt = 0;
13497 /* Get the range declaration momentarily out of the way so that
13498 the range expression doesn't clash with it. */
13499 if (range_decl != error_mark_node)
13501 if (DECL_HAS_VALUE_EXPR_P (range_decl))
13503 tree v = DECL_VALUE_EXPR (range_decl);
13504 /* For decomposition declaration get all of the corresponding
13505 declarations out of the way. */
13506 if (TREE_CODE (v) == ARRAY_REF
13507 && VAR_P (TREE_OPERAND (v, 0))
13508 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
13510 tree d = range_decl;
13511 range_decl = TREE_OPERAND (v, 0);
13512 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
13513 decomp_first_name = d;
13514 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
13516 tree name = DECL_NAME (d);
13517 names.safe_push (name);
13518 bindings.safe_push (IDENTIFIER_BINDING (name));
13519 IDENTIFIER_BINDING (name)
13520 = IDENTIFIER_BINDING (name)->previous;
13524 if (names.is_empty ())
13526 tree name = DECL_NAME (range_decl);
13527 names.safe_push (name);
13528 bindings.safe_push (IDENTIFIER_BINDING (name));
13529 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
13533 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13535 bool expr_non_constant_p;
13536 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13538 else
13539 range_expr = cp_parser_expression (parser);
13541 /* Put the range declaration(s) back into scope. */
13542 for (unsigned int i = 0; i < names.length (); i++)
13544 cxx_binding *binding = bindings[i];
13545 binding->previous = IDENTIFIER_BINDING (names[i]);
13546 IDENTIFIER_BINDING (names[i]) = binding;
13549 /* finish_omp_for has its own code for the following, so just
13550 return the range_expr instead. */
13551 if (is_omp)
13552 return range_expr;
13554 /* If in template, STMT is converted to a normal for-statement
13555 at instantiation. If not, it is done just ahead. */
13556 if (processing_template_decl)
13558 if (check_for_bare_parameter_packs (range_expr))
13559 range_expr = error_mark_node;
13560 stmt = begin_range_for_stmt (scope, init);
13561 if (ivdep)
13562 RANGE_FOR_IVDEP (stmt) = 1;
13563 if (unroll)
13564 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
13565 finish_range_for_decl (stmt, range_decl, range_expr);
13566 if (!type_dependent_expression_p (range_expr)
13567 /* do_auto_deduction doesn't mess with template init-lists. */
13568 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
13569 do_range_for_auto_deduction (range_decl, range_expr);
13571 else
13573 stmt = begin_for_stmt (scope, init);
13574 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
13575 decomp_first_name, decomp_cnt, ivdep,
13576 unroll);
13578 return stmt;
13581 /* Subroutine of cp_convert_range_for: given the initializer expression,
13582 builds up the range temporary. */
13584 static tree
13585 build_range_temp (tree range_expr)
13587 /* Find out the type deduced by the declaration
13588 `auto &&__range = range_expr'. */
13589 tree auto_node = make_auto ();
13590 tree range_type = cp_build_reference_type (auto_node, true);
13591 range_type = do_auto_deduction (range_type, range_expr, auto_node);
13593 /* Create the __range variable. */
13594 tree range_temp = build_decl (input_location, VAR_DECL,
13595 for_range__identifier, range_type);
13596 TREE_USED (range_temp) = 1;
13597 DECL_ARTIFICIAL (range_temp) = 1;
13599 return range_temp;
13602 /* Used by cp_parser_range_for in template context: we aren't going to
13603 do a full conversion yet, but we still need to resolve auto in the
13604 type of the for-range-declaration if present. This is basically
13605 a shortcut version of cp_convert_range_for. */
13607 static void
13608 do_range_for_auto_deduction (tree decl, tree range_expr)
13610 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13611 if (auto_node)
13613 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
13614 range_temp = convert_from_reference (build_range_temp (range_expr));
13615 iter_type = (cp_parser_perform_range_for_lookup
13616 (range_temp, &begin_dummy, &end_dummy));
13617 if (iter_type)
13619 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
13620 iter_type);
13621 iter_decl = build_x_indirect_ref (input_location, iter_decl,
13622 RO_UNARY_STAR,
13623 tf_warning_or_error);
13624 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
13625 iter_decl, auto_node,
13626 tf_warning_or_error,
13627 adc_variable_type);
13632 /* Warns when the loop variable should be changed to a reference type to
13633 avoid unnecessary copying. I.e., from
13635 for (const auto x : range)
13637 where range returns a reference, to
13639 for (const auto &x : range)
13641 if this version doesn't make a copy.
13643 This function also warns when the loop variable is initialized with
13644 a value of a different type resulting in a copy:
13646 int arr[10];
13647 for (const double &x : arr)
13649 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
13650 This function is never called when processing_template_decl is on. */
13652 static void
13653 warn_for_range_copy (tree decl, tree expr)
13655 if (!warn_range_loop_construct
13656 || decl == error_mark_node)
13657 return;
13659 location_t loc = DECL_SOURCE_LOCATION (decl);
13660 tree type = TREE_TYPE (decl);
13662 if (from_macro_expansion_at (loc))
13663 return;
13665 if (TYPE_REF_P (type))
13667 if (glvalue_p (expr) && !ref_conv_binds_directly_p (type, expr))
13669 auto_diagnostic_group d;
13670 if (warning_at (loc, OPT_Wrange_loop_construct,
13671 "loop variable %qD of type %qT binds to a temporary "
13672 "constructed from type %qT", decl, type,
13673 TREE_TYPE (expr)))
13675 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
13676 TYPE_QUAL_CONST);
13677 ref = cp_build_reference_type (ref, /*rval*/false);
13678 inform (loc, "use non-reference type %qT to make the copy "
13679 "explicit or %qT to prevent copying",
13680 non_reference (type), ref);
13683 return;
13685 else if (!CP_TYPE_CONST_P (type))
13686 return;
13688 /* Since small trivially copyable types are cheap to copy, we suppress the
13689 warning for them. 64B is a common size of a cache line. */
13690 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
13691 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
13692 && trivially_copyable_p (type)))
13693 return;
13695 tree rtype = cp_build_reference_type (type, /*rval*/false);
13696 /* If we could initialize the reference directly, it wouldn't involve any
13697 copies. */
13698 if (!ref_conv_binds_directly_p (rtype, expr))
13699 return;
13701 auto_diagnostic_group d;
13702 if (warning_at (loc, OPT_Wrange_loop_construct,
13703 "loop variable %qD creates a copy from type %qT",
13704 decl, type))
13706 gcc_rich_location richloc (loc);
13707 richloc.add_fixit_insert_before ("&");
13708 inform (&richloc, "use reference type to prevent copying");
13712 /* Converts a range-based for-statement into a normal
13713 for-statement, as per the definition.
13715 for (RANGE_DECL : RANGE_EXPR)
13716 BLOCK
13718 should be equivalent to:
13721 auto &&__range = RANGE_EXPR;
13722 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
13723 __begin != __end;
13724 ++__begin)
13726 RANGE_DECL = *__begin;
13727 BLOCK
13731 If RANGE_EXPR is an array:
13732 BEGIN_EXPR = __range
13733 END_EXPR = __range + ARRAY_SIZE(__range)
13734 Else if RANGE_EXPR has a member 'begin' or 'end':
13735 BEGIN_EXPR = __range.begin()
13736 END_EXPR = __range.end()
13737 Else:
13738 BEGIN_EXPR = begin(__range)
13739 END_EXPR = end(__range);
13741 If __range has a member 'begin' but not 'end', or vice versa, we must
13742 still use the second alternative (it will surely fail, however).
13743 When calling begin()/end() in the third alternative we must use
13744 argument dependent lookup, but always considering 'std' as an associated
13745 namespace. */
13747 tree
13748 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
13749 tree decomp_first_name, unsigned int decomp_cnt,
13750 bool ivdep, unsigned short unroll)
13752 tree begin, end;
13753 tree iter_type, begin_expr, end_expr;
13754 tree condition, expression;
13756 range_expr = mark_lvalue_use (range_expr);
13758 if (range_decl == error_mark_node || range_expr == error_mark_node)
13759 /* If an error happened previously do nothing or else a lot of
13760 unhelpful errors would be issued. */
13761 begin_expr = end_expr = iter_type = error_mark_node;
13762 else
13764 tree range_temp;
13766 if (VAR_P (range_expr)
13767 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
13768 /* Can't bind a reference to an array of runtime bound. */
13769 range_temp = range_expr;
13770 else
13772 range_temp = build_range_temp (range_expr);
13773 pushdecl (range_temp);
13774 cp_finish_decl (range_temp, range_expr,
13775 /*is_constant_init*/false, NULL_TREE,
13776 LOOKUP_ONLYCONVERTING);
13777 range_temp = convert_from_reference (range_temp);
13779 iter_type = cp_parser_perform_range_for_lookup (range_temp,
13780 &begin_expr, &end_expr);
13783 /* The new for initialization statement. */
13784 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
13785 iter_type);
13786 TREE_USED (begin) = 1;
13787 DECL_ARTIFICIAL (begin) = 1;
13788 pushdecl (begin);
13789 cp_finish_decl (begin, begin_expr,
13790 /*is_constant_init*/false, NULL_TREE,
13791 LOOKUP_ONLYCONVERTING);
13793 if (cxx_dialect >= cxx17)
13794 iter_type = cv_unqualified (TREE_TYPE (end_expr));
13795 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
13796 TREE_USED (end) = 1;
13797 DECL_ARTIFICIAL (end) = 1;
13798 pushdecl (end);
13799 cp_finish_decl (end, end_expr,
13800 /*is_constant_init*/false, NULL_TREE,
13801 LOOKUP_ONLYCONVERTING);
13803 finish_init_stmt (statement);
13805 /* The new for condition. */
13806 condition = build_x_binary_op (input_location, NE_EXPR,
13807 begin, ERROR_MARK,
13808 end, ERROR_MARK,
13809 NULL, tf_warning_or_error);
13810 finish_for_cond (condition, statement, ivdep, unroll);
13812 /* The new increment expression. */
13813 expression = finish_unary_op_expr (input_location,
13814 PREINCREMENT_EXPR, begin,
13815 tf_warning_or_error);
13816 finish_for_expr (expression, statement);
13818 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13819 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
13821 /* The declaration is initialized with *__begin inside the loop body. */
13822 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
13823 tf_warning_or_error);
13824 cp_finish_decl (range_decl, deref_begin,
13825 /*is_constant_init*/false, NULL_TREE,
13826 LOOKUP_ONLYCONVERTING);
13827 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13828 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
13830 warn_for_range_copy (range_decl, deref_begin);
13832 return statement;
13835 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
13836 We need to solve both at the same time because the method used
13837 depends on the existence of members begin or end.
13838 Returns the type deduced for the iterator expression. */
13840 static tree
13841 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
13843 if (error_operand_p (range))
13845 *begin = *end = error_mark_node;
13846 return error_mark_node;
13849 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
13851 error ("range-based %<for%> expression of type %qT "
13852 "has incomplete type", TREE_TYPE (range));
13853 *begin = *end = error_mark_node;
13854 return error_mark_node;
13856 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
13858 /* If RANGE is an array, we will use pointer arithmetic. */
13859 *begin = decay_conversion (range, tf_warning_or_error);
13860 *end = build_binary_op (input_location, PLUS_EXPR,
13861 range,
13862 array_type_nelts_top (TREE_TYPE (range)),
13863 false);
13864 return TREE_TYPE (*begin);
13866 else
13868 /* If it is not an array, we must do a bit of magic. */
13869 tree id_begin, id_end;
13870 tree member_begin, member_end;
13872 *begin = *end = error_mark_node;
13874 id_begin = get_identifier ("begin");
13875 id_end = get_identifier ("end");
13876 member_begin = lookup_member (TREE_TYPE (range), id_begin,
13877 /*protect=*/2, /*want_type=*/false,
13878 tf_warning_or_error);
13879 member_end = lookup_member (TREE_TYPE (range), id_end,
13880 /*protect=*/2, /*want_type=*/false,
13881 tf_warning_or_error);
13883 if (member_begin != NULL_TREE && member_end != NULL_TREE)
13885 /* Use the member functions. */
13886 *begin = cp_parser_range_for_member_function (range, id_begin);
13887 *end = cp_parser_range_for_member_function (range, id_end);
13889 else
13891 /* Use global functions with ADL. */
13892 releasing_vec vec;
13894 vec_safe_push (vec, range);
13896 member_begin = perform_koenig_lookup (id_begin, vec,
13897 tf_warning_or_error);
13898 *begin = finish_call_expr (member_begin, &vec, false, true,
13899 tf_warning_or_error);
13900 member_end = perform_koenig_lookup (id_end, vec,
13901 tf_warning_or_error);
13902 *end = finish_call_expr (member_end, &vec, false, true,
13903 tf_warning_or_error);
13906 /* Last common checks. */
13907 if (*begin == error_mark_node || *end == error_mark_node)
13909 /* If one of the expressions is an error do no more checks. */
13910 *begin = *end = error_mark_node;
13911 return error_mark_node;
13913 else if (type_dependent_expression_p (*begin)
13914 || type_dependent_expression_p (*end))
13915 /* Can happen, when, eg, in a template context, Koenig lookup
13916 can't resolve begin/end (c++/58503). */
13917 return NULL_TREE;
13918 else
13920 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
13921 /* The unqualified type of the __begin and __end temporaries should
13922 be the same, as required by the multiple auto declaration. */
13923 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
13925 if (cxx_dialect >= cxx17
13926 && (build_x_binary_op (input_location, NE_EXPR,
13927 *begin, ERROR_MARK,
13928 *end, ERROR_MARK,
13929 NULL, tf_none)
13930 != error_mark_node))
13931 /* P0184R0 allows __begin and __end to have different types,
13932 but make sure they are comparable so we can give a better
13933 diagnostic. */;
13934 else
13935 error ("inconsistent begin/end types in range-based %<for%> "
13936 "statement: %qT and %qT",
13937 TREE_TYPE (*begin), TREE_TYPE (*end));
13939 return iter_type;
13944 /* Helper function for cp_parser_perform_range_for_lookup.
13945 Builds a tree for RANGE.IDENTIFIER(). */
13947 static tree
13948 cp_parser_range_for_member_function (tree range, tree identifier)
13950 tree member, res;
13952 member = finish_class_member_access_expr (range, identifier,
13953 false, tf_warning_or_error);
13954 if (member == error_mark_node)
13955 return error_mark_node;
13957 releasing_vec vec;
13958 res = finish_call_expr (member, &vec,
13959 /*disallow_virtual=*/false,
13960 /*koenig_p=*/false,
13961 tf_warning_or_error);
13962 return res;
13965 /* Parse an iteration-statement.
13967 iteration-statement:
13968 while ( condition ) statement
13969 do statement while ( expression ) ;
13970 for ( init-statement condition [opt] ; expression [opt] )
13971 statement
13973 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
13975 static tree
13976 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
13977 unsigned short unroll)
13979 cp_token *token;
13980 enum rid keyword;
13981 tree statement;
13982 unsigned char in_statement;
13983 token_indent_info guard_tinfo;
13985 /* Peek at the next token. */
13986 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
13987 if (!token)
13988 return error_mark_node;
13990 guard_tinfo = get_token_indent_info (token);
13992 /* Remember whether or not we are already within an iteration
13993 statement. */
13994 in_statement = parser->in_statement;
13996 /* See what kind of keyword it is. */
13997 keyword = token->keyword;
13998 switch (keyword)
14000 case RID_WHILE:
14002 tree condition;
14004 /* Begin the while-statement. */
14005 statement = begin_while_stmt ();
14006 /* Look for the `('. */
14007 matching_parens parens;
14008 parens.require_open (parser);
14009 /* Parse the condition. */
14010 condition = cp_parser_condition (parser);
14011 finish_while_stmt_cond (condition, statement, ivdep, unroll);
14012 /* Look for the `)'. */
14013 parens.require_close (parser);
14014 /* Parse the dependent statement. */
14015 parser->in_statement = IN_ITERATION_STMT;
14016 bool prev = note_iteration_stmt_body_start ();
14017 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14018 note_iteration_stmt_body_end (prev);
14019 parser->in_statement = in_statement;
14020 /* We're done with the while-statement. */
14021 finish_while_stmt (statement);
14023 break;
14025 case RID_DO:
14027 tree expression;
14029 /* Begin the do-statement. */
14030 statement = begin_do_stmt ();
14031 /* Parse the body of the do-statement. */
14032 parser->in_statement = IN_ITERATION_STMT;
14033 bool prev = note_iteration_stmt_body_start ();
14034 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14035 note_iteration_stmt_body_end (prev);
14036 parser->in_statement = in_statement;
14037 finish_do_body (statement);
14038 /* Look for the `while' keyword. */
14039 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14040 /* Look for the `('. */
14041 matching_parens parens;
14042 parens.require_open (parser);
14043 /* Parse the expression. */
14044 expression = cp_parser_expression (parser);
14045 /* We're done with the do-statement. */
14046 finish_do_stmt (expression, statement, ivdep, unroll);
14047 /* Look for the `)'. */
14048 parens.require_close (parser);
14049 /* Look for the `;'. */
14050 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14052 break;
14054 case RID_FOR:
14056 /* Look for the `('. */
14057 matching_parens parens;
14058 parens.require_open (parser);
14060 statement = cp_parser_for (parser, ivdep, unroll);
14062 /* Look for the `)'. */
14063 parens.require_close (parser);
14065 /* Parse the body of the for-statement. */
14066 parser->in_statement = IN_ITERATION_STMT;
14067 bool prev = note_iteration_stmt_body_start ();
14068 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14069 note_iteration_stmt_body_end (prev);
14070 parser->in_statement = in_statement;
14072 /* We're done with the for-statement. */
14073 finish_for_stmt (statement);
14075 break;
14077 default:
14078 cp_parser_error (parser, "expected iteration-statement");
14079 statement = error_mark_node;
14080 break;
14083 return statement;
14086 /* Parse an init-statement or the declarator of a range-based-for.
14087 Returns true if a range-based-for declaration is seen.
14089 init-statement:
14090 expression-statement
14091 simple-declaration
14092 alias-declaration */
14094 static bool
14095 cp_parser_init_statement (cp_parser *parser, tree *decl)
14097 /* If the next token is a `;', then we have an empty
14098 expression-statement. Grammatically, this is also a
14099 simple-declaration, but an invalid one, because it does not
14100 declare anything. Therefore, if we did not handle this case
14101 specially, we would issue an error message about an invalid
14102 declaration. */
14103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14105 bool is_range_for = false;
14106 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14108 /* A colon is used in range-based for. */
14109 parser->colon_corrects_to_scope_p = false;
14111 /* We're going to speculatively look for a declaration, falling back
14112 to an expression, if necessary. */
14113 cp_parser_parse_tentatively (parser);
14114 bool expect_semicolon_p = true;
14115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14117 cp_parser_alias_declaration (parser);
14118 expect_semicolon_p = false;
14119 if (cxx_dialect < cxx23
14120 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14121 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14122 OPT_Wc__23_extensions,
14123 "alias-declaration in init-statement only "
14124 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14126 else
14127 /* Parse the declaration. */
14128 cp_parser_simple_declaration (parser,
14129 /*function_definition_allowed_p=*/false,
14130 decl);
14131 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14132 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14134 /* It is a range-for, consume the ':'. */
14135 cp_lexer_consume_token (parser->lexer);
14136 is_range_for = true;
14137 if (cxx_dialect < cxx11)
14138 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14139 OPT_Wc__11_extensions,
14140 "range-based %<for%> loops only available with "
14141 "%<-std=c++11%> or %<-std=gnu++11%>");
14143 else if (expect_semicolon_p)
14144 /* The ';' is not consumed yet because we told
14145 cp_parser_simple_declaration not to. */
14146 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14148 if (cp_parser_parse_definitely (parser))
14149 return is_range_for;
14150 /* If the tentative parse failed, then we shall need to look for an
14151 expression-statement. */
14153 /* If we are here, it is an expression-statement. */
14154 cp_parser_expression_statement (parser, NULL_TREE);
14155 return false;
14158 /* Parse a jump-statement.
14160 jump-statement:
14161 break ;
14162 continue ;
14163 return expression [opt] ;
14164 return braced-init-list ;
14165 coroutine-return-statement;
14166 goto identifier ;
14168 GNU extension:
14170 jump-statement:
14171 goto * expression ;
14173 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14175 static tree
14176 cp_parser_jump_statement (cp_parser* parser)
14178 tree statement = error_mark_node;
14179 cp_token *token;
14180 enum rid keyword;
14181 unsigned char in_statement;
14183 /* Peek at the next token. */
14184 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14185 if (!token)
14186 return error_mark_node;
14188 /* See what kind of keyword it is. */
14189 keyword = token->keyword;
14190 switch (keyword)
14192 case RID_BREAK:
14193 in_statement = parser->in_statement & ~IN_IF_STMT;
14194 switch (in_statement)
14196 case 0:
14197 error_at (token->location, "break statement not within loop or switch");
14198 break;
14199 default:
14200 gcc_assert ((in_statement & IN_SWITCH_STMT)
14201 || in_statement == IN_ITERATION_STMT);
14202 statement = finish_break_stmt ();
14203 if (in_statement == IN_ITERATION_STMT)
14204 break_maybe_infinite_loop ();
14205 break;
14206 case IN_OMP_BLOCK:
14207 error_at (token->location, "invalid exit from OpenMP structured block");
14208 break;
14209 case IN_OMP_FOR:
14210 error_at (token->location, "break statement used with OpenMP for loop");
14211 break;
14213 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14214 break;
14216 case RID_CONTINUE:
14217 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14219 case 0:
14220 error_at (token->location, "continue statement not within a loop");
14221 break;
14222 /* Fall through. */
14223 case IN_ITERATION_STMT:
14224 case IN_OMP_FOR:
14225 statement = finish_continue_stmt ();
14226 break;
14227 case IN_OMP_BLOCK:
14228 error_at (token->location, "invalid exit from OpenMP structured block");
14229 break;
14230 default:
14231 gcc_unreachable ();
14233 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14234 break;
14236 case RID_CO_RETURN:
14237 case RID_RETURN:
14239 tree expr;
14240 bool expr_non_constant_p;
14242 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14244 cp_lexer_set_source_position (parser->lexer);
14245 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14246 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
14248 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14249 expr = cp_parser_expression (parser);
14250 else
14251 /* If the next token is a `;', then there is no
14252 expression. */
14253 expr = NULL_TREE;
14254 /* Build the return-statement, check co-return first, since type
14255 deduction is not valid there. */
14256 if (keyword == RID_CO_RETURN)
14257 statement = finish_co_return_stmt (token->location, expr);
14258 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14259 /* Don't deduce from a discarded return statement. */;
14260 else
14261 statement = finish_return_stmt (expr);
14262 /* Look for the final `;'. */
14263 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14265 break;
14267 case RID_GOTO:
14268 if (parser->in_function_body
14269 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14270 && cxx_dialect < cxx23)
14272 error ("%<goto%> in %<constexpr%> function only available with "
14273 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14274 cp_function_chain->invalid_constexpr = true;
14277 /* Create the goto-statement. */
14278 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14280 /* Issue a warning about this use of a GNU extension. */
14281 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14282 /* Consume the '*' token. */
14283 cp_lexer_consume_token (parser->lexer);
14284 /* Parse the dependent expression. */
14285 finish_goto_stmt (cp_parser_expression (parser));
14287 else
14288 finish_goto_stmt (cp_parser_identifier (parser));
14289 /* Look for the final `;'. */
14290 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14291 break;
14293 default:
14294 cp_parser_error (parser, "expected jump-statement");
14295 break;
14298 return statement;
14301 /* Parse a declaration-statement.
14303 declaration-statement:
14304 block-declaration */
14306 static void
14307 cp_parser_declaration_statement (cp_parser* parser)
14309 void *p;
14311 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14312 p = obstack_alloc (&declarator_obstack, 0);
14314 /* Parse the block-declaration. */
14315 cp_parser_block_declaration (parser, /*statement_p=*/true);
14317 /* Free any declarators allocated. */
14318 obstack_free (&declarator_obstack, p);
14321 /* Some dependent statements (like `if (cond) statement'), are
14322 implicitly in their own scope. In other words, if the statement is
14323 a single statement (as opposed to a compound-statement), it is
14324 none-the-less treated as if it were enclosed in braces. Any
14325 declarations appearing in the dependent statement are out of scope
14326 after control passes that point. This function parses a statement,
14327 but ensures that is in its own scope, even if it is not a
14328 compound-statement.
14330 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14331 is a (possibly labeled) if statement which is not enclosed in
14332 braces and has an else clause. This is used to implement
14333 -Wparentheses.
14335 CHAIN is a vector of if-else-if conditions. This is used to implement
14336 -Wduplicated-cond.
14338 Returns the new statement. */
14340 static tree
14341 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14342 const token_indent_info &guard_tinfo,
14343 vec<tree> *chain)
14345 tree statement;
14346 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14347 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14348 token_indent_info body_tinfo
14349 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14351 if (if_p != NULL)
14352 *if_p = false;
14354 /* Mark if () ; with a special NOP_EXPR. */
14355 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14357 cp_lexer_consume_token (parser->lexer);
14358 statement = add_stmt (build_empty_stmt (body_loc));
14360 if (guard_tinfo.keyword == RID_IF
14361 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14362 warning_at (body_loc, OPT_Wempty_body,
14363 "suggest braces around empty body in an %<if%> statement");
14364 else if (guard_tinfo.keyword == RID_ELSE)
14365 warning_at (body_loc, OPT_Wempty_body,
14366 "suggest braces around empty body in an %<else%> statement");
14368 /* if a compound is opened, we simply parse the statement directly. */
14369 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14370 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14371 /* If the token is not a `{', then we must take special action. */
14372 else
14374 /* Create a compound-statement. */
14375 statement = begin_compound_stmt (0);
14376 /* Parse the dependent-statement. */
14377 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14378 &body_loc_after_labels);
14379 /* Finish the dummy compound-statement. */
14380 finish_compound_stmt (statement);
14383 token_indent_info next_tinfo
14384 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14385 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14387 if (body_loc_after_labels != UNKNOWN_LOCATION
14388 && next_tinfo.type != CPP_SEMICOLON)
14389 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14390 guard_tinfo.location, guard_tinfo.keyword);
14392 /* Return the statement. */
14393 return statement;
14396 /* For some dependent statements (like `while (cond) statement'), we
14397 have already created a scope. Therefore, even if the dependent
14398 statement is a compound-statement, we do not want to create another
14399 scope. */
14401 static void
14402 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14403 const token_indent_info &guard_tinfo)
14405 /* If the token is a `{', then we must take special action. */
14406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14408 token_indent_info body_tinfo
14409 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14410 location_t loc_after_labels = UNKNOWN_LOCATION;
14412 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14413 &loc_after_labels);
14414 token_indent_info next_tinfo
14415 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14416 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14418 if (loc_after_labels != UNKNOWN_LOCATION
14419 && next_tinfo.type != CPP_SEMICOLON)
14420 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14421 guard_tinfo.location,
14422 guard_tinfo.keyword);
14424 else
14426 /* Avoid calling cp_parser_compound_statement, so that we
14427 don't create a new scope. Do everything else by hand. */
14428 matching_braces braces;
14429 braces.require_open (parser);
14430 /* If the next keyword is `__label__' we have a label declaration. */
14431 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14432 cp_parser_label_declaration (parser);
14433 /* Parse an (optional) statement-seq. */
14434 cp_parser_statement_seq_opt (parser, NULL_TREE);
14435 braces.require_close (parser);
14439 /* Modules */
14441 /* Parse a module-name,
14442 identifier
14443 module-name . identifier
14444 header-name
14446 Returns a pointer to module object, NULL. */
14448 static module_state *
14449 cp_parser_module_name (cp_parser *parser)
14451 cp_token *token = cp_lexer_peek_token (parser->lexer);
14452 if (token->type == CPP_HEADER_NAME)
14454 cp_lexer_consume_token (parser->lexer);
14456 return get_module (token->u.value);
14459 module_state *parent = NULL;
14460 bool partitioned = false;
14461 if (token->type == CPP_COLON && named_module_p ())
14463 partitioned = true;
14464 cp_lexer_consume_token (parser->lexer);
14467 for (;;)
14469 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
14471 cp_parser_error (parser, "expected module-name");
14472 break;
14475 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
14476 parent = get_module (name, parent, partitioned);
14477 token = cp_lexer_peek_token (parser->lexer);
14478 if (!partitioned && token->type == CPP_COLON)
14479 partitioned = true;
14480 else if (token->type != CPP_DOT)
14481 break;
14483 cp_lexer_consume_token (parser->lexer);
14486 return parent;
14489 /* Named module-declaration
14490 __module ; PRAGMA_EOL
14491 __module private ; PRAGMA_EOL (unimplemented)
14492 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14495 static module_parse
14496 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
14497 bool exporting)
14499 /* We're a pseudo pragma. */
14500 parser->lexer->in_pragma = true;
14501 cp_token *token = cp_lexer_consume_token (parser->lexer);
14503 if (flag_header_unit)
14505 error_at (token->location,
14506 "module-declaration not permitted in header-unit");
14507 goto skip_eol;
14509 else if (mp_state == MP_FIRST && !exporting
14510 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14512 /* Start global module fragment. */
14513 cp_lexer_consume_token (parser->lexer);
14514 module_kind |= MK_GLOBAL;
14515 mp_state = MP_GLOBAL;
14516 cp_parser_require_pragma_eol (parser, token);
14518 else if (!exporting
14519 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14520 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
14521 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
14523 cp_lexer_consume_token (parser->lexer);
14524 cp_lexer_consume_token (parser->lexer);
14525 cp_lexer_consume_token (parser->lexer);
14526 cp_parser_require_pragma_eol (parser, token);
14528 if (!(mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
14529 || !module_interface_p () || module_partition_p ())
14530 error_at (token->location,
14531 "private module fragment only permitted in purview"
14532 " of module interface or partition");
14533 else
14535 mp_state = MP_PRIVATE_IMPORTS;
14536 sorry_at (token->location, "private module fragment");
14539 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
14541 /* Neither the first declaration, nor in a GMF. */
14542 error_at (token->location, "module-declaration only permitted as first"
14543 " declaration, or ending a global module fragment");
14544 skip_eol:
14545 cp_parser_skip_to_pragma_eol (parser, token);
14547 else
14549 module_state *mod = cp_parser_module_name (parser);
14550 tree attrs = cp_parser_attributes_opt (parser);
14552 mp_state = MP_PURVIEW_IMPORTS;
14553 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14554 goto skip_eol;
14556 declare_module (mod, token->location, exporting, attrs, parse_in);
14557 cp_parser_require_pragma_eol (parser, token);
14560 return mp_state;
14563 /* Import-declaration
14564 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14566 static void
14567 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
14568 bool exporting)
14570 /* We're a pseudo pragma. */
14571 parser->lexer->in_pragma = true;
14572 cp_token *token = cp_lexer_consume_token (parser->lexer);
14574 if (mp_state != MP_PURVIEW_IMPORTS
14575 && mp_state != MP_PRIVATE_IMPORTS
14576 && module_purview_p ()
14577 && !global_purview_p ())
14579 error_at (token->location, "post-module-declaration"
14580 " imports must be contiguous");
14581 note_lexer:
14582 inform (token->location, "perhaps insert a line break, or other"
14583 " disambiguation, to prevent this being considered a"
14584 " module control-line");
14585 skip_eol:
14586 cp_parser_skip_to_pragma_eol (parser, token);
14588 else if (current_scope () != global_namespace)
14590 error_at (token->location, "import-declaration must be at global scope");
14591 goto note_lexer;
14593 else
14595 module_state *mod = cp_parser_module_name (parser);
14596 tree attrs = cp_parser_attributes_opt (parser);
14598 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14599 goto skip_eol;
14600 cp_parser_require_pragma_eol (parser, token);
14602 if (parser->in_unbraced_linkage_specification_p)
14603 error_at (token->location, "import cannot appear directly in"
14604 " a linkage-specification");
14606 /* Module-purview imports must not be from source inclusion
14607 [cpp.import]/7 */
14608 if (attrs && module_purview_p () && !global_purview_p ()
14609 && private_lookup_attribute ("__translated",
14610 strlen ("__translated"), attrs))
14611 error_at (token->location, "post-module-declaration imports"
14612 " must not be include-translated");
14613 else if ((mp_state == MP_PURVIEW_IMPORTS
14614 || mp_state == MP_PRIVATE_IMPORTS)
14615 && !token->main_source_p)
14616 error_at (token->location, "post-module-declaration imports"
14617 " must not be from header inclusion");
14619 import_module (mod, token->location, exporting, attrs, parse_in);
14623 /* export-declaration.
14625 export declaration
14626 export { declaration-seq-opt } */
14628 static void
14629 cp_parser_module_export (cp_parser *parser)
14631 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
14632 cp_token *token = cp_lexer_consume_token (parser->lexer);
14634 if (!module_interface_p ())
14635 error_at (token->location,
14636 "%qE may only occur after a module interface declaration",
14637 token->u.value);
14639 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
14641 unsigned mk = module_kind;
14642 if (module_exporting_p ())
14643 error_at (token->location,
14644 "%qE may only occur once in an export declaration",
14645 token->u.value);
14646 module_kind |= MK_EXPORTING;
14648 if (braced)
14650 cp_ensure_no_omp_declare_simd (parser);
14651 cp_ensure_no_oacc_routine (parser);
14653 cp_lexer_consume_token (parser->lexer);
14654 cp_parser_declaration_seq_opt (parser);
14655 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14657 else
14659 /* Explicitly check if the next tokens might be a
14660 module-directive line, so we can give a clearer error message
14661 about why the directive will be rejected. */
14662 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
14663 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
14664 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
14665 error_at (token->location, "%<export%> not part of following"
14666 " module-directive");
14667 cp_parser_declaration (parser, NULL_TREE);
14670 module_kind = mk;
14673 /* Declarations [gram.dcl.dcl] */
14675 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
14676 is the top-level declaration sequence. That affects whether we
14677 deal with module-preamble.
14679 declaration-seq:
14680 declaration
14681 declaration-seq declaration */
14683 static void
14684 cp_parser_declaration_seq_opt (cp_parser* parser)
14686 while (true)
14688 cp_token *token = cp_lexer_peek_token (parser->lexer);
14690 if (token->type == CPP_CLOSE_BRACE
14691 || token->type == CPP_EOF)
14692 break;
14693 else
14694 cp_parser_toplevel_declaration (parser);
14698 /* Parse a declaration.
14700 declaration:
14701 block-declaration
14702 function-definition
14703 template-declaration
14704 explicit-instantiation
14705 explicit-specialization
14706 linkage-specification
14707 namespace-definition
14709 C++17:
14710 deduction-guide
14712 modules:
14713 (all these are only allowed at the outermost level, check
14714 that semantically, for better diagnostics)
14715 module-declaration
14716 module-export-declaration
14717 module-import-declaration
14718 export-declaration
14720 GNU extension:
14722 declaration:
14723 __extension__ declaration */
14725 static void
14726 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
14728 int saved_pedantic;
14730 /* Check for the `__extension__' keyword. */
14731 if (cp_parser_extension_opt (parser, &saved_pedantic))
14733 /* Parse the qualified declaration. */
14734 cp_parser_declaration (parser, prefix_attrs);
14735 /* Restore the PEDANTIC flag. */
14736 pedantic = saved_pedantic;
14738 return;
14741 /* Try to figure out what kind of declaration is present. */
14742 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14743 cp_token *token2 = (token1->type == CPP_EOF
14744 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14746 if (token1->type == CPP_SEMICOLON)
14748 cp_lexer_consume_token (parser->lexer);
14749 /* A declaration consisting of a single semicolon is invalid
14750 * before C++11. Allow it unless we're being pedantic. */
14751 if (cxx_dialect < cxx11)
14752 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
14753 return;
14755 else if (cp_lexer_nth_token_is (parser->lexer,
14756 cp_parser_skip_std_attribute_spec_seq (parser,
14758 CPP_SEMICOLON))
14760 location_t attrs_loc = token1->location;
14761 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
14763 if (std_attrs && (flag_openmp || flag_openmp_simd))
14765 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
14766 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
14767 std_attrs);
14768 if (parser->lexer->in_omp_attribute_pragma)
14770 cp_lexer *lexer = parser->lexer;
14771 while (parser->lexer->in_omp_attribute_pragma)
14773 gcc_assert (cp_lexer_next_token_is (parser->lexer,
14774 CPP_PRAGMA));
14775 cp_parser_pragma (parser, pragma_external, NULL);
14777 cp_lexer_destroy (lexer);
14781 if (std_attrs != NULL_TREE)
14782 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
14783 OPT_Wattributes, "attribute ignored");
14784 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14785 cp_lexer_consume_token (parser->lexer);
14786 return;
14789 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14790 void *p = obstack_alloc (&declarator_obstack, 0);
14792 tree attributes = NULL_TREE;
14794 /* Conditionally, allow attributes to precede a linkage specification. */
14795 if (token1->keyword == RID_ATTRIBUTE)
14797 cp_lexer_save_tokens (parser->lexer);
14798 attributes = cp_parser_attributes_opt (parser);
14799 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
14800 cp_token *t2 = (t1->type == CPP_EOF
14801 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14802 if (t1->keyword == RID_EXTERN
14803 && cp_parser_is_pure_string_literal (t2))
14805 cp_lexer_commit_tokens (parser->lexer);
14806 /* We might have already been here. */
14807 if (!c_dialect_objc ())
14809 location_t where = get_finish (t2->location);
14810 warning_at (token1->location, OPT_Wattributes, "attributes are"
14811 " not permitted in this position");
14812 where = linemap_position_for_loc_and_offset (line_table,
14813 where, 1);
14814 inform (where, "attributes may be inserted here");
14815 attributes = NULL_TREE;
14817 token1 = t1;
14818 token2 = t2;
14820 else
14822 cp_lexer_rollback_tokens (parser->lexer);
14823 attributes = NULL_TREE;
14826 /* If we already had some attributes, and we've added more, then prepend.
14827 Otherwise attributes just contains any that we just read. */
14828 if (prefix_attrs)
14830 if (attributes)
14831 TREE_CHAIN (prefix_attrs) = attributes;
14832 attributes = prefix_attrs;
14835 /* If the next token is `extern' and the following token is a string
14836 literal, then we have a linkage specification. */
14837 if (token1->keyword == RID_EXTERN
14838 && cp_parser_is_pure_string_literal (token2))
14839 cp_parser_linkage_specification (parser, attributes);
14840 /* If the next token is `template', then we have either a template
14841 declaration, an explicit instantiation, or an explicit
14842 specialization. */
14843 else if (token1->keyword == RID_TEMPLATE)
14845 /* `template <>' indicates a template specialization. */
14846 if (token2->type == CPP_LESS
14847 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14848 cp_parser_explicit_specialization (parser);
14849 /* `template <' indicates a template declaration. */
14850 else if (token2->type == CPP_LESS)
14851 cp_parser_template_declaration (parser, /*member_p=*/false);
14852 /* Anything else must be an explicit instantiation. */
14853 else
14854 cp_parser_explicit_instantiation (parser);
14856 /* If the next token is `export', it's new-style modules or
14857 old-style template. */
14858 else if (token1->keyword == RID_EXPORT)
14860 if (!modules_p ())
14861 cp_parser_template_declaration (parser, /*member_p=*/false);
14862 else
14863 cp_parser_module_export (parser);
14865 else if (token1->keyword == RID__EXPORT
14866 || token1->keyword == RID__IMPORT
14867 || token1->keyword == RID__MODULE)
14869 bool exporting = token1->keyword == RID__EXPORT;
14870 cp_token *next = exporting ? token2 : token1;
14871 if (exporting)
14872 cp_lexer_consume_token (parser->lexer);
14873 if (next->keyword == RID__MODULE)
14874 cp_parser_module_declaration (parser, MP_NOT_MODULE, exporting);
14875 else
14876 cp_parser_import_declaration (parser, MP_NOT_MODULE, exporting);
14878 /* If the next token is `extern', 'static' or 'inline' and the one
14879 after that is `template', we have a GNU extended explicit
14880 instantiation directive. */
14881 else if (cp_parser_allow_gnu_extensions_p (parser)
14882 && token2->keyword == RID_TEMPLATE
14883 && (token1->keyword == RID_EXTERN
14884 || token1->keyword == RID_STATIC
14885 || token1->keyword == RID_INLINE))
14886 cp_parser_explicit_instantiation (parser);
14887 /* If the next token is `namespace', check for a named or unnamed
14888 namespace definition. */
14889 else if (token1->keyword == RID_NAMESPACE
14890 && (/* A named namespace definition. */
14891 (token2->type == CPP_NAME
14892 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
14893 != CPP_EQ))
14894 || (token2->type == CPP_OPEN_SQUARE
14895 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
14896 == CPP_OPEN_SQUARE)
14897 /* An unnamed namespace definition. */
14898 || token2->type == CPP_OPEN_BRACE
14899 || token2->keyword == RID_ATTRIBUTE))
14900 cp_parser_namespace_definition (parser);
14901 /* An inline (associated) namespace definition. */
14902 else if (token2->keyword == RID_NAMESPACE
14903 && token1->keyword == RID_INLINE)
14904 cp_parser_namespace_definition (parser);
14905 /* Objective-C++ declaration/definition. */
14906 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
14907 cp_parser_objc_declaration (parser, attributes);
14908 else if (c_dialect_objc ()
14909 && token1->keyword == RID_ATTRIBUTE
14910 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
14911 cp_parser_objc_declaration (parser, attributes);
14912 /* At this point we may have a template declared by a concept
14913 introduction. */
14914 else if (flag_concepts
14915 && cp_parser_template_declaration_after_export (parser,
14916 /*member_p=*/false))
14917 /* We did. */;
14918 else
14919 /* Try to parse a block-declaration, or a function-definition. */
14920 cp_parser_block_declaration (parser, /*statement_p=*/false);
14922 /* Free any declarators allocated. */
14923 obstack_free (&declarator_obstack, p);
14926 /* Parse a namespace-scope declaration. */
14928 static void
14929 cp_parser_toplevel_declaration (cp_parser* parser)
14931 cp_token *token = cp_lexer_peek_token (parser->lexer);
14933 if (token->type == CPP_PRAGMA)
14934 /* A top-level declaration can consist solely of a #pragma. A
14935 nested declaration cannot, so this is done here and not in
14936 cp_parser_declaration. (A #pragma at block scope is
14937 handled in cp_parser_statement.) */
14938 cp_parser_pragma (parser, pragma_external, NULL);
14939 else
14940 /* Parse the declaration itself. */
14941 cp_parser_declaration (parser, NULL_TREE);
14944 /* Parse a block-declaration.
14946 block-declaration:
14947 simple-declaration
14948 asm-definition
14949 namespace-alias-definition
14950 using-declaration
14951 using-directive
14953 GNU Extension:
14955 block-declaration:
14956 __extension__ block-declaration
14958 C++0x Extension:
14960 block-declaration:
14961 static_assert-declaration
14963 If STATEMENT_P is TRUE, then this block-declaration is occurring as
14964 part of a declaration-statement. */
14966 static void
14967 cp_parser_block_declaration (cp_parser *parser,
14968 bool statement_p)
14970 int saved_pedantic;
14972 /* Check for the `__extension__' keyword. */
14973 if (cp_parser_extension_opt (parser, &saved_pedantic))
14975 /* Parse the qualified declaration. */
14976 cp_parser_block_declaration (parser, statement_p);
14977 /* Restore the PEDANTIC flag. */
14978 pedantic = saved_pedantic;
14980 return;
14983 /* Peek at the next token to figure out which kind of declaration is
14984 present. */
14985 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14986 size_t attr_idx;
14988 /* If the next keyword is `asm', we have an asm-definition. */
14989 if (token1->keyword == RID_ASM)
14991 if (statement_p)
14992 cp_parser_commit_to_tentative_parse (parser);
14993 cp_parser_asm_definition (parser);
14995 /* If the next keyword is `namespace', we have a
14996 namespace-alias-definition. */
14997 else if (token1->keyword == RID_NAMESPACE)
14998 cp_parser_namespace_alias_definition (parser);
14999 /* If the next keyword is `using', we have a
15000 using-declaration, a using-directive, or an alias-declaration. */
15001 else if (token1->keyword == RID_USING)
15003 cp_token *token2;
15005 if (statement_p)
15006 cp_parser_commit_to_tentative_parse (parser);
15007 /* If the token after `using' is `namespace', then we have a
15008 using-directive. */
15009 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15010 if (token2->keyword == RID_NAMESPACE)
15011 cp_parser_using_directive (parser);
15012 else if (token2->keyword == RID_ENUM)
15013 cp_parser_using_enum (parser);
15014 /* If the second token after 'using' is '=', then we have an
15015 alias-declaration. */
15016 else if (cxx_dialect >= cxx11
15017 && token2->type == CPP_NAME
15018 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15019 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15020 cp_parser_alias_declaration (parser);
15021 /* Otherwise, it's a using-declaration. */
15022 else
15023 cp_parser_using_declaration (parser,
15024 /*access_declaration_p=*/false);
15026 /* If the next keyword is `__label__' we have a misplaced label
15027 declaration. */
15028 else if (token1->keyword == RID_LABEL)
15030 cp_lexer_consume_token (parser->lexer);
15031 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15032 cp_parser_skip_to_end_of_statement (parser);
15033 /* If the next token is now a `;', consume it. */
15034 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15035 cp_lexer_consume_token (parser->lexer);
15037 /* If the next token is `static_assert' we have a static assertion. */
15038 else if (token1->keyword == RID_STATIC_ASSERT)
15039 cp_parser_static_assert (parser, /*member_p=*/false);
15040 /* If the next tokens after attributes is `using namespace', then we have
15041 a using-directive. */
15042 else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
15043 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx,
15044 RID_USING)
15045 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15046 RID_NAMESPACE))
15048 if (statement_p)
15049 cp_parser_commit_to_tentative_parse (parser);
15050 cp_parser_using_directive (parser);
15052 /* Anything else must be a simple-declaration. */
15053 else
15054 cp_parser_simple_declaration (parser, !statement_p,
15055 /*maybe_range_for_decl*/NULL);
15058 /* Parse a simple-declaration.
15060 simple-declaration:
15061 decl-specifier-seq [opt] init-declarator-list [opt] ;
15062 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15063 brace-or-equal-initializer ;
15065 init-declarator-list:
15066 init-declarator
15067 init-declarator-list , init-declarator
15069 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15070 function-definition as a simple-declaration.
15072 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15073 parsed declaration if it is an uninitialized single declarator not followed
15074 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15075 if present, will not be consumed. */
15077 static void
15078 cp_parser_simple_declaration (cp_parser* parser,
15079 bool function_definition_allowed_p,
15080 tree *maybe_range_for_decl)
15082 cp_decl_specifier_seq decl_specifiers;
15083 int declares_class_or_enum;
15084 bool saw_declarator;
15085 location_t comma_loc = UNKNOWN_LOCATION;
15086 location_t init_loc = UNKNOWN_LOCATION;
15088 if (maybe_range_for_decl)
15089 *maybe_range_for_decl = NULL_TREE;
15091 /* Defer access checks until we know what is being declared; the
15092 checks for names appearing in the decl-specifier-seq should be
15093 done as if we were in the scope of the thing being declared. */
15094 push_deferring_access_checks (dk_deferred);
15096 /* Parse the decl-specifier-seq. We have to keep track of whether
15097 or not the decl-specifier-seq declares a named class or
15098 enumeration type, since that is the only case in which the
15099 init-declarator-list is allowed to be empty.
15101 [dcl.dcl]
15103 In a simple-declaration, the optional init-declarator-list can be
15104 omitted only when declaring a class or enumeration, that is when
15105 the decl-specifier-seq contains either a class-specifier, an
15106 elaborated-type-specifier, or an enum-specifier. */
15107 cp_parser_decl_specifier_seq (parser,
15108 CP_PARSER_FLAGS_OPTIONAL,
15109 &decl_specifiers,
15110 &declares_class_or_enum);
15111 /* We no longer need to defer access checks. */
15112 stop_deferring_access_checks ();
15114 cp_omp_declare_simd_data odsd;
15115 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15116 cp_parser_handle_directive_omp_attributes (parser,
15117 &decl_specifiers.attributes,
15118 &odsd, true);
15120 /* In a block scope, a valid declaration must always have a
15121 decl-specifier-seq. By not trying to parse declarators, we can
15122 resolve the declaration/expression ambiguity more quickly. */
15123 if (!function_definition_allowed_p
15124 && !decl_specifiers.any_specifiers_p)
15126 cp_parser_error (parser, "expected declaration");
15127 goto done;
15130 /* If the next two tokens are both identifiers, the code is
15131 erroneous. The usual cause of this situation is code like:
15133 T t;
15135 where "T" should name a type -- but does not. */
15136 if (!decl_specifiers.any_type_specifiers_p
15137 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15139 /* If parsing tentatively, we should commit; we really are
15140 looking at a declaration. */
15141 cp_parser_commit_to_tentative_parse (parser);
15142 /* Give up. */
15143 goto done;
15146 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15148 /* Look for C++17 decomposition declaration. */
15149 for (size_t n = 1; ; n++)
15150 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15151 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15152 continue;
15153 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15154 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15155 && decl_specifiers.any_specifiers_p)
15157 tree decl
15158 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15159 maybe_range_for_decl,
15160 &init_loc);
15162 /* The next token should be either a `,' or a `;'. */
15163 cp_token *token = cp_lexer_peek_token (parser->lexer);
15164 /* If it's a `;', we are done. */
15165 if (token->type == CPP_SEMICOLON)
15166 goto finish;
15167 else if (maybe_range_for_decl)
15169 if (*maybe_range_for_decl == NULL_TREE)
15170 *maybe_range_for_decl = error_mark_node;
15171 goto finish;
15173 /* Anything else is an error. */
15174 else
15176 /* If we have already issued an error message we don't need
15177 to issue another one. */
15178 if ((decl != error_mark_node
15179 && DECL_INITIAL (decl) != error_mark_node)
15180 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15181 cp_parser_error (parser, "expected %<;%>");
15182 /* Skip tokens until we reach the end of the statement. */
15183 cp_parser_skip_to_end_of_statement (parser);
15184 /* If the next token is now a `;', consume it. */
15185 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15186 cp_lexer_consume_token (parser->lexer);
15187 goto done;
15190 else
15191 break;
15193 tree last_type;
15194 bool auto_specifier_p;
15195 /* NULL_TREE if both variable and function declaration are allowed,
15196 error_mark_node if function declaration are not allowed and
15197 a FUNCTION_DECL that should be diagnosed if it is followed by
15198 variable declarations. */
15199 tree auto_function_declaration;
15201 last_type = NULL_TREE;
15202 auto_specifier_p
15203 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15204 auto_function_declaration = NULL_TREE;
15206 /* Keep going until we hit the `;' at the end of the simple
15207 declaration. */
15208 saw_declarator = false;
15209 while (cp_lexer_next_token_is_not (parser->lexer,
15210 CPP_SEMICOLON))
15212 cp_token *token;
15213 bool function_definition_p;
15214 tree decl;
15215 tree auto_result = NULL_TREE;
15217 if (saw_declarator)
15219 /* If we are processing next declarator, comma is expected */
15220 token = cp_lexer_peek_token (parser->lexer);
15221 gcc_assert (token->type == CPP_COMMA);
15222 cp_lexer_consume_token (parser->lexer);
15223 if (maybe_range_for_decl)
15225 *maybe_range_for_decl = error_mark_node;
15226 if (comma_loc == UNKNOWN_LOCATION)
15227 comma_loc = token->location;
15230 else
15231 saw_declarator = true;
15233 /* Parse the init-declarator. */
15234 decl = cp_parser_init_declarator (parser,
15235 CP_PARSER_FLAGS_NONE,
15236 &decl_specifiers,
15237 /*checks=*/NULL,
15238 function_definition_allowed_p,
15239 /*member_p=*/false,
15240 declares_class_or_enum,
15241 &function_definition_p,
15242 maybe_range_for_decl,
15243 &init_loc,
15244 &auto_result);
15245 /* If an error occurred while parsing tentatively, exit quickly.
15246 (That usually happens when in the body of a function; each
15247 statement is treated as a declaration-statement until proven
15248 otherwise.) */
15249 if (cp_parser_error_occurred (parser))
15250 goto done;
15252 if (auto_specifier_p && cxx_dialect >= cxx14)
15254 /* If the init-declarator-list contains more than one
15255 init-declarator, they shall all form declarations of
15256 variables. */
15257 if (auto_function_declaration == NULL_TREE)
15258 auto_function_declaration
15259 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
15260 else if (TREE_CODE (decl) == FUNCTION_DECL
15261 || auto_function_declaration != error_mark_node)
15263 error_at (decl_specifiers.locations[ds_type_spec],
15264 "non-variable %qD in declaration with more than one "
15265 "declarator with placeholder type",
15266 TREE_CODE (decl) == FUNCTION_DECL
15267 ? decl : auto_function_declaration);
15268 auto_function_declaration = error_mark_node;
15272 if (auto_result
15273 && (!processing_template_decl || !type_uses_auto (auto_result)))
15275 if (last_type
15276 && last_type != error_mark_node
15277 && !same_type_p (auto_result, last_type))
15279 /* If the list of declarators contains more than one declarator,
15280 the type of each declared variable is determined as described
15281 above. If the type deduced for the template parameter U is not
15282 the same in each deduction, the program is ill-formed. */
15283 error_at (decl_specifiers.locations[ds_type_spec],
15284 "inconsistent deduction for %qT: %qT and then %qT",
15285 decl_specifiers.type, last_type, auto_result);
15286 last_type = error_mark_node;
15288 else
15289 last_type = auto_result;
15292 /* Handle function definitions specially. */
15293 if (function_definition_p)
15295 /* If the next token is a `,', then we are probably
15296 processing something like:
15298 void f() {}, *p;
15300 which is erroneous. */
15301 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15303 cp_token *token = cp_lexer_peek_token (parser->lexer);
15304 error_at (token->location,
15305 "mixing"
15306 " declarations and function-definitions is forbidden");
15308 /* Otherwise, we're done with the list of declarators. */
15309 else
15311 pop_deferring_access_checks ();
15312 cp_finalize_omp_declare_simd (parser, &odsd);
15313 return;
15316 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15317 *maybe_range_for_decl = decl;
15318 /* The next token should be either a `,' or a `;'. */
15319 token = cp_lexer_peek_token (parser->lexer);
15320 /* If it's a `,', there are more declarators to come. */
15321 if (token->type == CPP_COMMA)
15322 /* will be consumed next time around */;
15323 /* If it's a `;', we are done. */
15324 else if (token->type == CPP_SEMICOLON)
15325 break;
15326 else if (maybe_range_for_decl)
15328 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15329 permerror (decl_specifiers.locations[ds_type_spec],
15330 "types may not be defined in a for-range-declaration");
15331 break;
15333 /* Anything else is an error. */
15334 else
15336 /* If we have already issued an error message we don't need
15337 to issue another one. */
15338 if ((decl != error_mark_node
15339 && DECL_INITIAL (decl) != error_mark_node)
15340 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15341 cp_parser_error (parser, "expected %<,%> or %<;%>");
15342 /* Skip tokens until we reach the end of the statement. */
15343 cp_parser_skip_to_end_of_statement (parser);
15344 /* If the next token is now a `;', consume it. */
15345 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15346 cp_lexer_consume_token (parser->lexer);
15347 goto done;
15349 /* After the first time around, a function-definition is not
15350 allowed -- even if it was OK at first. For example:
15352 int i, f() {}
15354 is not valid. */
15355 function_definition_allowed_p = false;
15358 /* Issue an error message if no declarators are present, and the
15359 decl-specifier-seq does not itself declare a class or
15360 enumeration: [dcl.dcl]/3. */
15361 if (!saw_declarator)
15363 if (cp_parser_declares_only_class_p (parser))
15365 if (!declares_class_or_enum
15366 && decl_specifiers.type
15367 && OVERLOAD_TYPE_P (decl_specifiers.type))
15368 /* Ensure an error is issued anyway when finish_decltype_type,
15369 called via cp_parser_decl_specifier_seq, returns a class or
15370 an enumeration (c++/51786). */
15371 decl_specifiers.type = NULL_TREE;
15372 shadow_tag (&decl_specifiers);
15374 /* Perform any deferred access checks. */
15375 perform_deferred_access_checks (tf_warning_or_error);
15378 /* Consume the `;'. */
15379 finish:
15380 if (!maybe_range_for_decl)
15381 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15382 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15384 if (init_loc != UNKNOWN_LOCATION)
15385 error_at (init_loc, "initializer in range-based %<for%> loop");
15386 if (comma_loc != UNKNOWN_LOCATION)
15387 error_at (comma_loc,
15388 "multiple declarations in range-based %<for%> loop");
15391 done:
15392 pop_deferring_access_checks ();
15393 cp_finalize_omp_declare_simd (parser, &odsd);
15396 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15397 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15398 initializer ; */
15400 static tree
15401 cp_parser_decomposition_declaration (cp_parser *parser,
15402 cp_decl_specifier_seq *decl_specifiers,
15403 tree *maybe_range_for_decl,
15404 location_t *init_loc)
15406 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15407 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15408 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15410 /* Parse the identifier-list. */
15411 auto_vec<cp_expr, 10> v;
15412 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15413 while (true)
15415 cp_expr e = cp_parser_identifier (parser);
15416 if (e.get_value () == error_mark_node)
15417 break;
15418 v.safe_push (e);
15419 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15420 break;
15421 cp_lexer_consume_token (parser->lexer);
15424 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
15425 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15427 end_loc = UNKNOWN_LOCATION;
15428 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
15429 false);
15430 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15431 cp_lexer_consume_token (parser->lexer);
15432 else
15434 cp_parser_skip_to_end_of_statement (parser);
15435 return error_mark_node;
15439 if (cxx_dialect < cxx17)
15440 pedwarn (loc, OPT_Wc__17_extensions,
15441 "structured bindings only available with "
15442 "%<-std=c++17%> or %<-std=gnu++17%>");
15444 tree pushed_scope;
15445 cp_declarator *declarator = make_declarator (cdk_decomp);
15446 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
15447 declarator->id_loc = loc;
15448 if (ref_qual != REF_QUAL_NONE)
15449 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
15450 ref_qual == REF_QUAL_RVALUE,
15451 NULL_TREE);
15452 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
15453 NULL_TREE, decl_specifiers->attributes,
15454 &pushed_scope);
15455 tree orig_decl = decl;
15457 unsigned int i;
15458 cp_expr e;
15459 cp_decl_specifier_seq decl_specs;
15460 clear_decl_specs (&decl_specs);
15461 decl_specs.type = make_auto ();
15462 tree prev = decl;
15463 FOR_EACH_VEC_ELT (v, i, e)
15465 if (i == 0)
15466 declarator = make_id_declarator (NULL_TREE, e.get_value (),
15467 sfk_none, e.get_location ());
15468 else
15470 declarator->u.id.unqualified_name = e.get_value ();
15471 declarator->id_loc = e.get_location ();
15473 tree elt_pushed_scope;
15474 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
15475 NULL_TREE, NULL_TREE, &elt_pushed_scope);
15476 if (decl2 == error_mark_node)
15477 decl = error_mark_node;
15478 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
15480 /* Ensure we've diagnosed redeclaration if we aren't creating
15481 a new VAR_DECL. */
15482 gcc_assert (errorcount);
15483 decl = error_mark_node;
15485 else
15486 prev = decl2;
15487 if (elt_pushed_scope)
15488 pop_scope (elt_pushed_scope);
15491 if (v.is_empty ())
15493 error_at (loc, "empty structured binding declaration");
15494 decl = error_mark_node;
15497 if (maybe_range_for_decl == NULL
15498 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15500 bool non_constant_p = false, is_direct_init = false;
15501 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
15502 tree initializer = cp_parser_initializer (parser, &is_direct_init,
15503 &non_constant_p);
15504 if (initializer == NULL_TREE
15505 || (TREE_CODE (initializer) == TREE_LIST
15506 && TREE_CHAIN (initializer))
15507 || (is_direct_init
15508 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
15509 && CONSTRUCTOR_NELTS (initializer) != 1))
15511 error_at (loc, "invalid initializer for structured binding "
15512 "declaration");
15513 initializer = error_mark_node;
15516 if (decl != error_mark_node)
15518 cp_maybe_mangle_decomp (decl, prev, v.length ());
15519 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
15520 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15521 cp_finish_decomp (decl, prev, v.length ());
15524 else if (decl != error_mark_node)
15526 *maybe_range_for_decl = prev;
15527 /* Ensure DECL_VALUE_EXPR is created for all the decls but
15528 the underlying DECL. */
15529 cp_finish_decomp (decl, prev, v.length ());
15532 if (pushed_scope)
15533 pop_scope (pushed_scope);
15535 if (decl == error_mark_node && DECL_P (orig_decl))
15537 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
15538 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
15541 return decl;
15544 /* Parse a decl-specifier-seq.
15546 decl-specifier-seq:
15547 decl-specifier-seq [opt] decl-specifier
15548 decl-specifier attribute-specifier-seq [opt] (C++11)
15550 decl-specifier:
15551 storage-class-specifier
15552 type-specifier
15553 function-specifier
15554 friend
15555 typedef
15557 GNU Extension:
15559 decl-specifier:
15560 attributes
15562 Concepts Extension:
15564 decl-specifier:
15565 concept
15567 Set *DECL_SPECS to a representation of the decl-specifier-seq.
15569 The parser flags FLAGS is used to control type-specifier parsing.
15571 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15572 flags:
15574 1: one of the decl-specifiers is an elaborated-type-specifier
15575 (i.e., a type declaration)
15576 2: one of the decl-specifiers is an enum-specifier or a
15577 class-specifier (i.e., a type definition)
15581 static void
15582 cp_parser_decl_specifier_seq (cp_parser* parser,
15583 cp_parser_flags flags,
15584 cp_decl_specifier_seq *decl_specs,
15585 int* declares_class_or_enum)
15587 bool constructor_possible_p = !parser->in_declarator_p;
15588 bool found_decl_spec = false;
15589 cp_token *start_token = NULL;
15590 cp_decl_spec ds;
15592 /* Clear DECL_SPECS. */
15593 clear_decl_specs (decl_specs);
15595 /* Assume no class or enumeration type is declared. */
15596 *declares_class_or_enum = 0;
15598 /* Keep reading specifiers until there are no more to read. */
15599 while (true)
15601 bool constructor_p;
15602 cp_token *token;
15603 ds = ds_last;
15605 /* Peek at the next token. */
15606 token = cp_lexer_peek_token (parser->lexer);
15608 /* Save the first token of the decl spec list for error
15609 reporting. */
15610 if (!start_token)
15611 start_token = token;
15612 /* Handle attributes. */
15613 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
15614 && cp_next_tokens_can_be_attribute_p (parser))
15616 /* Parse the attributes. */
15617 tree attrs = cp_parser_attributes_opt (parser);
15619 /* In a sequence of declaration specifiers, c++11 attributes
15620 appertain to the type that precede them. In that case
15621 [dcl.spec]/1 says:
15623 The attribute-specifier-seq affects the type only for
15624 the declaration it appears in, not other declarations
15625 involving the same type.
15627 But for now let's force the user to position the
15628 attribute either at the beginning of the declaration or
15629 after the declarator-id, which would clearly mean that it
15630 applies to the declarator. */
15631 if (cxx11_attribute_p (attrs))
15633 if (!found_decl_spec)
15634 /* The c++11 attribute is at the beginning of the
15635 declaration. It appertains to the entity being
15636 declared. */;
15637 else
15639 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
15641 /* This is an attribute following a
15642 class-specifier. */
15643 if (decl_specs->type_definition_p)
15644 warn_misplaced_attr_for_class_type (token->location,
15645 decl_specs->type);
15646 attrs = NULL_TREE;
15648 else
15650 decl_specs->std_attributes
15651 = attr_chainon (decl_specs->std_attributes, attrs);
15652 if (decl_specs->locations[ds_std_attribute] == 0)
15653 decl_specs->locations[ds_std_attribute] = token->location;
15655 continue;
15659 decl_specs->attributes
15660 = attr_chainon (decl_specs->attributes, attrs);
15661 if (decl_specs->locations[ds_attribute] == 0)
15662 decl_specs->locations[ds_attribute] = token->location;
15663 continue;
15665 /* Assume we will find a decl-specifier keyword. */
15666 found_decl_spec = true;
15667 /* If the next token is an appropriate keyword, we can simply
15668 add it to the list. */
15669 switch (token->keyword)
15671 /* decl-specifier:
15672 friend
15673 constexpr
15674 constinit */
15675 case RID_FRIEND:
15676 if (!at_class_scope_p ())
15678 gcc_rich_location richloc (token->location);
15679 richloc.add_fixit_remove ();
15680 error_at (&richloc, "%<friend%> used outside of class");
15681 cp_lexer_purge_token (parser->lexer);
15683 else
15685 ds = ds_friend;
15686 /* Consume the token. */
15687 cp_lexer_consume_token (parser->lexer);
15689 break;
15691 case RID_CONSTEXPR:
15692 ds = ds_constexpr;
15693 cp_lexer_consume_token (parser->lexer);
15694 break;
15696 case RID_CONSTINIT:
15697 ds = ds_constinit;
15698 cp_lexer_consume_token (parser->lexer);
15699 break;
15701 case RID_CONSTEVAL:
15702 ds = ds_consteval;
15703 cp_lexer_consume_token (parser->lexer);
15704 break;
15706 case RID_CONCEPT:
15707 ds = ds_concept;
15708 cp_lexer_consume_token (parser->lexer);
15710 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15711 break;
15713 /* Warn for concept as a decl-specifier. We'll rewrite these as
15714 concept declarations later. */
15715 if (!flag_concepts_ts)
15717 cp_token *next = cp_lexer_peek_token (parser->lexer);
15718 if (next->keyword == RID_BOOL)
15719 pedwarn (next->location, 0, "the %<bool%> keyword is not "
15720 "allowed in a C++20 concept definition");
15721 else
15722 pedwarn (token->location, 0, "C++20 concept definition syntax "
15723 "is %<concept <name> = <expr>%>");
15726 /* In C++20 a concept definition is just 'concept name = expr;'
15727 Support that syntax as a TS extension by pretending we've seen
15728 the 'bool' specifier. */
15729 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15730 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
15731 && !decl_specs->any_type_specifiers_p)
15733 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
15734 token, /*type_definition*/false);
15735 decl_specs->any_type_specifiers_p = true;
15737 break;
15739 /* function-specifier:
15740 inline
15741 virtual
15742 explicit */
15743 case RID_INLINE:
15744 case RID_VIRTUAL:
15745 case RID_EXPLICIT:
15746 cp_parser_function_specifier_opt (parser, decl_specs);
15747 break;
15749 /* decl-specifier:
15750 typedef */
15751 case RID_TYPEDEF:
15752 ds = ds_typedef;
15753 /* Consume the token. */
15754 cp_lexer_consume_token (parser->lexer);
15756 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15757 break;
15759 /* A constructor declarator cannot appear in a typedef. */
15760 constructor_possible_p = false;
15761 /* The "typedef" keyword can only occur in a declaration; we
15762 may as well commit at this point. */
15763 cp_parser_commit_to_tentative_parse (parser);
15765 if (decl_specs->storage_class != sc_none)
15766 decl_specs->conflicting_specifiers_p = true;
15767 break;
15769 /* storage-class-specifier:
15770 auto
15771 register
15772 static
15773 extern
15774 mutable
15776 GNU Extension:
15777 thread */
15778 case RID_AUTO:
15779 if (cxx_dialect == cxx98)
15781 /* Consume the token. */
15782 cp_lexer_consume_token (parser->lexer);
15784 /* Complain about `auto' as a storage specifier, if
15785 we're complaining about C++0x compatibility. */
15786 gcc_rich_location richloc (token->location);
15787 richloc.add_fixit_remove ();
15788 warning_at (&richloc, OPT_Wc__11_compat,
15789 "%<auto%> changes meaning in C++11; "
15790 "please remove it");
15792 /* Set the storage class anyway. */
15793 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
15794 token);
15796 else
15797 /* C++0x auto type-specifier. */
15798 found_decl_spec = false;
15799 break;
15801 case RID_REGISTER:
15802 case RID_STATIC:
15803 case RID_EXTERN:
15804 case RID_MUTABLE:
15805 /* Consume the token. */
15806 cp_lexer_consume_token (parser->lexer);
15807 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
15808 token);
15809 break;
15810 case RID_THREAD:
15811 /* Consume the token. */
15812 ds = ds_thread;
15813 cp_lexer_consume_token (parser->lexer);
15814 break;
15816 default:
15817 /* We did not yet find a decl-specifier yet. */
15818 found_decl_spec = false;
15819 break;
15822 if (found_decl_spec
15823 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
15824 && token->keyword != RID_CONSTEXPR)
15825 error ("%<decl-specifier%> invalid in condition");
15827 if (found_decl_spec
15828 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15829 && token->keyword != RID_MUTABLE
15830 && token->keyword != RID_CONSTEXPR
15831 && token->keyword != RID_CONSTEVAL)
15832 error_at (token->location, "%qD invalid in lambda",
15833 ridpointers[token->keyword]);
15835 if (ds != ds_last)
15836 set_and_check_decl_spec_loc (decl_specs, ds, token);
15838 /* Constructors are a special case. The `S' in `S()' is not a
15839 decl-specifier; it is the beginning of the declarator. */
15840 constructor_p
15841 = (!found_decl_spec
15842 && constructor_possible_p
15843 && (cp_parser_constructor_declarator_p
15844 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
15845 ds_friend))));
15847 /* If we don't have a DECL_SPEC yet, then we must be looking at
15848 a type-specifier. */
15849 if (!found_decl_spec && !constructor_p)
15851 int decl_spec_declares_class_or_enum;
15852 bool is_cv_qualifier;
15853 tree type_spec;
15855 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15856 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15858 type_spec
15859 = cp_parser_type_specifier (parser, flags,
15860 decl_specs,
15861 /*is_declaration=*/true,
15862 &decl_spec_declares_class_or_enum,
15863 &is_cv_qualifier);
15864 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
15866 /* If this type-specifier referenced a user-defined type
15867 (a typedef, class-name, etc.), then we can't allow any
15868 more such type-specifiers henceforth.
15870 [dcl.spec]
15872 The longest sequence of decl-specifiers that could
15873 possibly be a type name is taken as the
15874 decl-specifier-seq of a declaration. The sequence shall
15875 be self-consistent as described below.
15877 [dcl.type]
15879 As a general rule, at most one type-specifier is allowed
15880 in the complete decl-specifier-seq of a declaration. The
15881 only exceptions are the following:
15883 -- const or volatile can be combined with any other
15884 type-specifier.
15886 -- signed or unsigned can be combined with char, long,
15887 short, or int.
15889 -- ..
15891 Example:
15893 typedef char* Pc;
15894 void g (const int Pc);
15896 Here, Pc is *not* part of the decl-specifier seq; it's
15897 the declarator. Therefore, once we see a type-specifier
15898 (other than a cv-qualifier), we forbid any additional
15899 user-defined types. We *do* still allow things like `int
15900 int' to be considered a decl-specifier-seq, and issue the
15901 error message later. */
15902 if (type_spec && !is_cv_qualifier)
15903 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15904 /* A constructor declarator cannot follow a type-specifier. */
15905 if (type_spec)
15907 constructor_possible_p = false;
15908 found_decl_spec = true;
15909 if (!is_cv_qualifier)
15910 decl_specs->any_type_specifiers_p = true;
15912 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
15913 error_at (token->location, "type-specifier invalid in lambda");
15917 /* If we still do not have a DECL_SPEC, then there are no more
15918 decl-specifiers. */
15919 if (!found_decl_spec)
15920 break;
15922 if (decl_specs->std_attributes)
15924 error_at (decl_specs->locations[ds_std_attribute],
15925 "standard attributes in middle of decl-specifiers");
15926 inform (decl_specs->locations[ds_std_attribute],
15927 "standard attributes must precede the decl-specifiers to "
15928 "apply to the declaration, or follow them to apply to "
15929 "the type");
15932 decl_specs->any_specifiers_p = true;
15933 /* After we see one decl-specifier, further decl-specifiers are
15934 always optional. */
15935 flags |= CP_PARSER_FLAGS_OPTIONAL;
15938 /* Don't allow a friend specifier with a class definition. */
15939 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
15940 && (*declares_class_or_enum & 2))
15941 error_at (decl_specs->locations[ds_friend],
15942 "class definition may not be declared a friend");
15945 /* Parse an (optional) storage-class-specifier.
15947 storage-class-specifier:
15948 auto
15949 register
15950 static
15951 extern
15952 mutable
15954 GNU Extension:
15956 storage-class-specifier:
15957 thread
15959 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
15961 static tree
15962 cp_parser_storage_class_specifier_opt (cp_parser* parser)
15964 switch (cp_lexer_peek_token (parser->lexer)->keyword)
15966 case RID_AUTO:
15967 if (cxx_dialect != cxx98)
15968 return NULL_TREE;
15969 /* Fall through for C++98. */
15970 gcc_fallthrough ();
15972 case RID_REGISTER:
15973 case RID_STATIC:
15974 case RID_EXTERN:
15975 case RID_MUTABLE:
15976 case RID_THREAD:
15977 /* Consume the token. */
15978 return cp_lexer_consume_token (parser->lexer)->u.value;
15980 default:
15981 return NULL_TREE;
15985 /* Parse an (optional) function-specifier.
15987 function-specifier:
15988 inline
15989 virtual
15990 explicit
15992 C++20 Extension:
15993 explicit(constant-expression)
15995 Returns an IDENTIFIER_NODE corresponding to the keyword used.
15996 Updates DECL_SPECS, if it is non-NULL. */
15998 static tree
15999 cp_parser_function_specifier_opt (cp_parser* parser,
16000 cp_decl_specifier_seq *decl_specs)
16002 cp_token *token = cp_lexer_peek_token (parser->lexer);
16003 switch (token->keyword)
16005 case RID_INLINE:
16006 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16007 break;
16009 case RID_VIRTUAL:
16010 /* 14.5.2.3 [temp.mem]
16012 A member function template shall not be virtual. */
16013 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16014 && current_class_type)
16015 error_at (token->location, "templates may not be %<virtual%>");
16016 else
16017 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16018 break;
16020 case RID_EXPLICIT:
16022 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16023 /* If we see '(', it's C++20 explicit(bool). */
16024 tree expr;
16025 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16027 matching_parens parens;
16028 parens.consume_open (parser);
16030 /* New types are not allowed in an explicit-specifier. */
16031 const char *saved_message
16032 = parser->type_definition_forbidden_message;
16033 parser->type_definition_forbidden_message
16034 = G_("types may not be defined in explicit-specifier");
16036 if (cxx_dialect < cxx20)
16037 pedwarn (token->location, OPT_Wc__20_extensions,
16038 "%<explicit(bool)%> only available with %<-std=c++20%> "
16039 "or %<-std=gnu++20%>");
16041 /* Parse the constant-expression. */
16042 expr = cp_parser_constant_expression (parser);
16044 /* Restore the saved message. */
16045 parser->type_definition_forbidden_message = saved_message;
16046 parens.require_close (parser);
16048 else
16049 /* The explicit-specifier explicit without a constant-expression is
16050 equivalent to the explicit-specifier explicit(true). */
16051 expr = boolean_true_node;
16053 /* [dcl.fct.spec]
16054 "the constant-expression, if supplied, shall be a contextually
16055 converted constant expression of type bool." */
16056 expr = build_explicit_specifier (expr, tf_warning_or_error);
16057 /* We could evaluate it -- mark the decl as appropriate. */
16058 if (expr == boolean_true_node)
16059 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16060 else if (expr == boolean_false_node)
16061 /* Don't mark the decl as explicit. */;
16062 else if (decl_specs)
16063 /* The expression was value-dependent. Remember it so that we can
16064 substitute it later. */
16065 decl_specs->explicit_specifier = expr;
16066 return id;
16069 default:
16070 return NULL_TREE;
16073 /* Consume the token. */
16074 return cp_lexer_consume_token (parser->lexer)->u.value;
16077 /* Parse a linkage-specification.
16079 linkage-specification:
16080 extern string-literal { declaration-seq [opt] }
16081 extern string-literal declaration */
16083 static void
16084 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16086 tree linkage;
16088 /* Look for the `extern' keyword. */
16089 cp_token *extern_token
16090 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16092 /* Look for the string-literal. */
16093 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16094 linkage = cp_parser_string_literal (parser, false, false);
16096 /* Transform the literal into an identifier. If the literal is a
16097 wide-character string, or contains embedded NULs, then we can't
16098 handle it as the user wants. */
16099 if (strlen (TREE_STRING_POINTER (linkage))
16100 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16102 cp_parser_error (parser, "invalid linkage-specification");
16103 /* Assume C++ linkage. */
16104 linkage = lang_name_cplusplus;
16106 else
16107 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16109 /* We're now using the new linkage. */
16110 push_lang_context (linkage);
16112 /* Preserve the location of the innermost linkage specification,
16113 tracking the locations of nested specifications via a local. */
16114 location_t saved_location
16115 = parser->innermost_linkage_specification_location;
16116 /* Construct a location ranging from the start of the "extern" to
16117 the end of the string-literal, with the caret at the start, e.g.:
16118 extern "C" {
16119 ^~~~~~~~~~
16121 parser->innermost_linkage_specification_location
16122 = make_location (extern_token->location,
16123 extern_token->location,
16124 get_finish (string_token->location));
16126 /* If the next token is a `{', then we're using the first
16127 production. */
16128 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16130 cp_ensure_no_omp_declare_simd (parser);
16131 cp_ensure_no_oacc_routine (parser);
16133 /* Consume the `{' token. */
16134 matching_braces braces;
16135 braces.consume_open (parser);
16136 /* Parse the declarations. */
16137 cp_parser_declaration_seq_opt (parser);
16138 /* Look for the closing `}'. */
16139 braces.require_close (parser);
16141 /* Otherwise, there's just one declaration. */
16142 else
16144 bool saved_in_unbraced_linkage_specification_p;
16146 saved_in_unbraced_linkage_specification_p
16147 = parser->in_unbraced_linkage_specification_p;
16148 parser->in_unbraced_linkage_specification_p = true;
16149 cp_parser_declaration (parser, prefix_attr);
16150 parser->in_unbraced_linkage_specification_p
16151 = saved_in_unbraced_linkage_specification_p;
16154 /* We're done with the linkage-specification. */
16155 pop_lang_context ();
16157 /* Restore location of parent linkage specification, if any. */
16158 parser->innermost_linkage_specification_location = saved_location;
16161 /* Parse a static_assert-declaration.
16163 static_assert-declaration:
16164 static_assert ( constant-expression , string-literal ) ;
16165 static_assert ( constant-expression ) ; (C++17)
16167 If MEMBER_P, this static_assert is a class member. */
16169 static void
16170 cp_parser_static_assert(cp_parser *parser, bool member_p)
16172 cp_expr condition;
16173 location_t token_loc;
16174 tree message;
16175 bool dummy;
16177 /* Peek at the `static_assert' token so we can keep track of exactly
16178 where the static assertion started. */
16179 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16181 /* Look for the `static_assert' keyword. */
16182 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16183 RT_STATIC_ASSERT))
16184 return;
16186 /* We know we are in a static assertion; commit to any tentative
16187 parse. */
16188 if (cp_parser_parsing_tentatively (parser))
16189 cp_parser_commit_to_tentative_parse (parser);
16191 /* Parse the `(' starting the static assertion condition. */
16192 matching_parens parens;
16193 parens.require_open (parser);
16195 /* Parse the constant-expression. Allow a non-constant expression
16196 here in order to give better diagnostics in finish_static_assert. */
16197 condition =
16198 cp_parser_constant_expression (parser,
16199 /*allow_non_constant_p=*/true,
16200 /*non_constant_p=*/&dummy);
16202 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16204 if (pedantic && cxx_dialect < cxx17)
16205 pedwarn (input_location, OPT_Wc__17_extensions,
16206 "%<static_assert%> without a message "
16207 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16208 /* Eat the ')' */
16209 cp_lexer_consume_token (parser->lexer);
16210 message = build_string (1, "");
16211 TREE_TYPE (message) = char_array_type_node;
16212 fix_string_type (message);
16214 else
16216 /* Parse the separating `,'. */
16217 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16219 /* Parse the string-literal message. */
16220 message = cp_parser_string_literal (parser,
16221 /*translate=*/false,
16222 /*wide_ok=*/true);
16224 /* A `)' completes the static assertion. */
16225 if (!parens.require_close (parser))
16226 cp_parser_skip_to_closing_parenthesis (parser,
16227 /*recovering=*/true,
16228 /*or_comma=*/false,
16229 /*consume_paren=*/true);
16232 /* A semicolon terminates the declaration. */
16233 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16235 /* Get the location for the static assertion. Use that of the
16236 condition if available, otherwise, use that of the "static_assert"
16237 token. */
16238 location_t assert_loc = condition.get_location ();
16239 if (assert_loc == UNKNOWN_LOCATION)
16240 assert_loc = token_loc;
16242 /* Complete the static assertion, which may mean either processing
16243 the static assert now or saving it for template instantiation. */
16244 finish_static_assert (condition, message, assert_loc, member_p,
16245 /*show_expr_p=*/false);
16248 /* Parse the expression in decltype ( expression ). */
16250 static tree
16251 cp_parser_decltype_expr (cp_parser *parser,
16252 bool &id_expression_or_member_access_p)
16254 cp_token *id_expr_start_token;
16255 tree expr;
16257 /* First, try parsing an id-expression. */
16258 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16259 cp_parser_parse_tentatively (parser);
16260 expr = cp_parser_id_expression (parser,
16261 /*template_keyword_p=*/false,
16262 /*check_dependency_p=*/true,
16263 /*template_p=*/NULL,
16264 /*declarator_p=*/false,
16265 /*optional_p=*/false);
16267 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16269 bool non_integral_constant_expression_p = false;
16270 tree id_expression = expr;
16271 cp_id_kind idk;
16272 const char *error_msg;
16274 if (identifier_p (expr))
16275 /* Lookup the name we got back from the id-expression. */
16276 expr = cp_parser_lookup_name_simple (parser, expr,
16277 id_expr_start_token->location);
16279 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
16280 /* A template without args is not a complete id-expression. */
16281 expr = error_mark_node;
16283 if (expr
16284 && expr != error_mark_node
16285 && TREE_CODE (expr) != TYPE_DECL
16286 && (TREE_CODE (expr) != BIT_NOT_EXPR
16287 || !TYPE_P (TREE_OPERAND (expr, 0)))
16288 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16290 /* Complete lookup of the id-expression. */
16291 expr = (finish_id_expression
16292 (id_expression, expr, parser->scope, &idk,
16293 /*integral_constant_expression_p=*/false,
16294 /*allow_non_integral_constant_expression_p=*/true,
16295 &non_integral_constant_expression_p,
16296 /*template_p=*/false,
16297 /*done=*/true,
16298 /*address_p=*/false,
16299 /*template_arg_p=*/false,
16300 &error_msg,
16301 id_expr_start_token->location));
16303 if (expr == error_mark_node)
16304 /* We found an id-expression, but it was something that we
16305 should not have found. This is an error, not something
16306 we can recover from, so note that we found an
16307 id-expression and we'll recover as gracefully as
16308 possible. */
16309 id_expression_or_member_access_p = true;
16312 if (expr
16313 && expr != error_mark_node
16314 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16315 /* We have an id-expression. */
16316 id_expression_or_member_access_p = true;
16319 if (!id_expression_or_member_access_p)
16321 /* Abort the id-expression parse. */
16322 cp_parser_abort_tentative_parse (parser);
16324 /* Parsing tentatively, again. */
16325 cp_parser_parse_tentatively (parser);
16327 /* Parse a class member access. */
16328 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16329 /*cast_p=*/false, /*decltype*/true,
16330 /*member_access_only_p=*/true, NULL);
16332 if (expr
16333 && expr != error_mark_node
16334 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16335 /* We have an id-expression. */
16336 id_expression_or_member_access_p = true;
16339 if (id_expression_or_member_access_p)
16340 /* We have parsed the complete id-expression or member access. */
16341 cp_parser_parse_definitely (parser);
16342 else
16344 /* Abort our attempt to parse an id-expression or member access
16345 expression. */
16346 cp_parser_abort_tentative_parse (parser);
16348 /* Parse a full expression. */
16349 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
16350 /*decltype_p=*/true);
16353 return expr;
16356 /* Parse a `decltype' type. Returns the type.
16358 decltype-specifier:
16359 decltype ( expression )
16360 C++14:
16361 decltype ( auto ) */
16363 static tree
16364 cp_parser_decltype (cp_parser *parser)
16366 bool id_expression_or_member_access_p = false;
16367 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
16369 if (start_token->type == CPP_DECLTYPE)
16371 /* Already parsed. */
16372 cp_lexer_consume_token (parser->lexer);
16373 return saved_checks_value (start_token->u.tree_check_value);
16376 /* Look for the `decltype' token. */
16377 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
16378 return error_mark_node;
16380 /* Parse the opening `('. */
16381 matching_parens parens;
16382 if (!parens.require_open (parser))
16383 return error_mark_node;
16385 /* Since we're going to preserve any side-effects from this parse, set up a
16386 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16387 in the expression. */
16388 tentative_firewall firewall (parser);
16390 /* If in_declarator_p, a reparse as an expression might succeed (60361).
16391 Otherwise, commit now for better diagnostics. */
16392 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
16393 && !parser->in_declarator_p)
16394 cp_parser_commit_to_topmost_tentative_parse (parser);
16396 push_deferring_access_checks (dk_deferred);
16398 tree expr = NULL_TREE;
16400 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
16401 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
16403 /* decltype (auto) */
16404 cp_lexer_consume_token (parser->lexer);
16405 if (cxx_dialect < cxx14)
16407 error_at (start_token->location,
16408 "%<decltype(auto)%> type specifier only available with "
16409 "%<-std=c++14%> or %<-std=gnu++14%>");
16410 expr = error_mark_node;
16413 else
16415 /* decltype (expression) */
16417 /* Types cannot be defined in a `decltype' expression. Save away the
16418 old message and set the new one. */
16419 const char *saved_message = parser->type_definition_forbidden_message;
16420 parser->type_definition_forbidden_message
16421 = G_("types may not be defined in %<decltype%> expressions");
16423 /* The restrictions on constant-expressions do not apply inside
16424 decltype expressions. */
16425 bool saved_integral_constant_expression_p
16426 = parser->integral_constant_expression_p;
16427 bool saved_non_integral_constant_expression_p
16428 = parser->non_integral_constant_expression_p;
16429 parser->integral_constant_expression_p = false;
16431 /* Within a parenthesized expression, a `>' token is always
16432 the greater-than operator. */
16433 bool saved_greater_than_is_operator_p
16434 = parser->greater_than_is_operator_p;
16435 parser->greater_than_is_operator_p = true;
16437 /* Do not actually evaluate the expression. */
16438 ++cp_unevaluated_operand;
16440 /* Do not warn about problems with the expression. */
16441 ++c_inhibit_evaluation_warnings;
16443 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
16444 STRIP_ANY_LOCATION_WRAPPER (expr);
16446 /* Go back to evaluating expressions. */
16447 --cp_unevaluated_operand;
16448 --c_inhibit_evaluation_warnings;
16450 /* The `>' token might be the end of a template-id or
16451 template-parameter-list now. */
16452 parser->greater_than_is_operator_p
16453 = saved_greater_than_is_operator_p;
16455 /* Restore the old message and the integral constant expression
16456 flags. */
16457 parser->type_definition_forbidden_message = saved_message;
16458 parser->integral_constant_expression_p
16459 = saved_integral_constant_expression_p;
16460 parser->non_integral_constant_expression_p
16461 = saved_non_integral_constant_expression_p;
16464 /* Parse to the closing `)'. */
16465 if (expr == error_mark_node || !parens.require_close (parser))
16467 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16468 /*consume_paren=*/true);
16469 expr = error_mark_node;
16472 /* If we got a parse error while tentative, bail out now. */
16473 if (cp_parser_error_occurred (parser))
16475 pop_deferring_access_checks ();
16476 return error_mark_node;
16479 if (!expr)
16480 /* Build auto. */
16481 expr = make_decltype_auto ();
16482 else
16483 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
16484 tf_warning_or_error);
16486 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16487 it again. */
16488 start_token->type = CPP_DECLTYPE;
16489 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16490 start_token->tree_check_p = true;
16491 start_token->u.tree_check_value->value = expr;
16492 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
16493 start_token->keyword = RID_MAX;
16495 location_t loc = start_token->location;
16496 loc = make_location (loc, loc, parser->lexer);
16497 start_token->location = loc;
16499 cp_lexer_purge_tokens_after (parser->lexer, start_token);
16501 pop_to_parent_deferring_access_checks ();
16503 return expr;
16506 /* Special member functions [gram.special] */
16508 /* Parse a conversion-function-id.
16510 conversion-function-id:
16511 operator conversion-type-id
16513 Returns an IDENTIFIER_NODE representing the operator. */
16515 static tree
16516 cp_parser_conversion_function_id (cp_parser* parser)
16518 tree type;
16519 tree saved_scope;
16520 tree saved_qualifying_scope;
16521 tree saved_object_scope;
16522 tree pushed_scope = NULL_TREE;
16524 /* Look for the `operator' token. */
16525 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16526 return error_mark_node;
16527 /* When we parse the conversion-type-id, the current scope will be
16528 reset. However, we need that information in able to look up the
16529 conversion function later, so we save it here. */
16530 saved_scope = parser->scope;
16531 saved_qualifying_scope = parser->qualifying_scope;
16532 saved_object_scope = parser->object_scope;
16533 /* We must enter the scope of the class so that the names of
16534 entities declared within the class are available in the
16535 conversion-type-id. For example, consider:
16537 struct S {
16538 typedef int I;
16539 operator I();
16542 S::operator I() { ... }
16544 In order to see that `I' is a type-name in the definition, we
16545 must be in the scope of `S'. */
16546 if (saved_scope)
16547 pushed_scope = push_scope (saved_scope);
16548 /* Parse the conversion-type-id. */
16549 type = cp_parser_conversion_type_id (parser);
16550 /* Leave the scope of the class, if any. */
16551 if (pushed_scope)
16552 pop_scope (pushed_scope);
16553 /* Restore the saved scope. */
16554 parser->scope = saved_scope;
16555 parser->qualifying_scope = saved_qualifying_scope;
16556 parser->object_scope = saved_object_scope;
16557 /* If the TYPE is invalid, indicate failure. */
16558 if (type == error_mark_node)
16559 return error_mark_node;
16560 return make_conv_op_name (type);
16563 /* Parse a conversion-type-id:
16565 conversion-type-id:
16566 type-specifier-seq conversion-declarator [opt]
16568 Returns the TYPE specified. */
16570 static tree
16571 cp_parser_conversion_type_id (cp_parser* parser)
16573 tree attributes;
16574 cp_decl_specifier_seq type_specifiers;
16575 cp_declarator *declarator;
16576 tree type_specified;
16577 const char *saved_message;
16579 /* Parse the attributes. */
16580 attributes = cp_parser_attributes_opt (parser);
16582 saved_message = parser->type_definition_forbidden_message;
16583 parser->type_definition_forbidden_message
16584 = G_("types may not be defined in a conversion-type-id");
16586 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
16587 optional in conversion-type-id. */
16588 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16589 /*is_declaration=*/false,
16590 /*is_trailing_return=*/false,
16591 &type_specifiers);
16593 parser->type_definition_forbidden_message = saved_message;
16595 /* If that didn't work, stop. */
16596 if (type_specifiers.type == error_mark_node)
16597 return error_mark_node;
16598 /* Parse the conversion-declarator. */
16599 declarator = cp_parser_conversion_declarator_opt (parser);
16601 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
16602 /*initialized=*/0, &attributes);
16603 if (attributes)
16604 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
16606 /* Don't give this error when parsing tentatively. This happens to
16607 work because we always parse this definitively once. */
16608 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
16609 && type_uses_auto (type_specified))
16611 if (cxx_dialect < cxx14)
16613 error ("invalid use of %<auto%> in conversion operator");
16614 return error_mark_node;
16616 else if (template_parm_scope_p ())
16617 warning (0, "use of %<auto%> in member template "
16618 "conversion operator can never be deduced");
16621 return type_specified;
16624 /* Parse an (optional) conversion-declarator.
16626 conversion-declarator:
16627 ptr-operator conversion-declarator [opt]
16631 static cp_declarator *
16632 cp_parser_conversion_declarator_opt (cp_parser* parser)
16634 enum tree_code code;
16635 tree class_type, std_attributes = NULL_TREE;
16636 cp_cv_quals cv_quals;
16638 /* We don't know if there's a ptr-operator next, or not. */
16639 cp_parser_parse_tentatively (parser);
16640 /* Try the ptr-operator. */
16641 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
16642 &std_attributes);
16643 /* If it worked, look for more conversion-declarators. */
16644 if (cp_parser_parse_definitely (parser))
16646 cp_declarator *declarator;
16648 /* Parse another optional declarator. */
16649 declarator = cp_parser_conversion_declarator_opt (parser);
16651 declarator = cp_parser_make_indirect_declarator
16652 (code, class_type, cv_quals, declarator, std_attributes);
16654 return declarator;
16657 return NULL;
16660 /* Parse an (optional) ctor-initializer.
16662 ctor-initializer:
16663 : mem-initializer-list */
16665 static void
16666 cp_parser_ctor_initializer_opt (cp_parser* parser)
16668 /* If the next token is not a `:', then there is no
16669 ctor-initializer. */
16670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16672 /* Do default initialization of any bases and members. */
16673 if (DECL_CONSTRUCTOR_P (current_function_decl))
16674 finish_mem_initializers (NULL_TREE);
16675 return;
16678 /* Consume the `:' token. */
16679 cp_lexer_consume_token (parser->lexer);
16680 /* And the mem-initializer-list. */
16681 cp_parser_mem_initializer_list (parser);
16684 /* Parse a mem-initializer-list.
16686 mem-initializer-list:
16687 mem-initializer ... [opt]
16688 mem-initializer ... [opt] , mem-initializer-list */
16690 static void
16691 cp_parser_mem_initializer_list (cp_parser* parser)
16693 tree mem_initializer_list = NULL_TREE;
16694 tree target_ctor = error_mark_node;
16695 cp_token *token = cp_lexer_peek_token (parser->lexer);
16697 /* Let the semantic analysis code know that we are starting the
16698 mem-initializer-list. */
16699 if (!DECL_CONSTRUCTOR_P (current_function_decl))
16700 error_at (token->location,
16701 "only constructors take member initializers");
16703 /* Loop through the list. */
16704 while (true)
16706 tree mem_initializer;
16708 token = cp_lexer_peek_token (parser->lexer);
16709 /* Parse the mem-initializer. */
16710 mem_initializer = cp_parser_mem_initializer (parser);
16711 /* If the next token is a `...', we're expanding member initializers. */
16712 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16713 if (ellipsis
16714 || (mem_initializer != error_mark_node
16715 && check_for_bare_parameter_packs (TREE_PURPOSE
16716 (mem_initializer))))
16718 /* Consume the `...'. */
16719 if (ellipsis)
16720 cp_lexer_consume_token (parser->lexer);
16722 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
16723 can be expanded but members cannot. */
16724 if (mem_initializer != error_mark_node
16725 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
16727 error_at (token->location,
16728 "cannot expand initializer for member %qD",
16729 TREE_PURPOSE (mem_initializer));
16730 mem_initializer = error_mark_node;
16733 /* Construct the pack expansion type. */
16734 if (mem_initializer != error_mark_node)
16735 mem_initializer = make_pack_expansion (mem_initializer);
16737 if (target_ctor != error_mark_node
16738 && mem_initializer != error_mark_node)
16740 error ("mem-initializer for %qD follows constructor delegation",
16741 TREE_PURPOSE (mem_initializer));
16742 mem_initializer = error_mark_node;
16744 /* Look for a target constructor. */
16745 if (mem_initializer != error_mark_node
16746 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
16747 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
16749 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
16750 if (mem_initializer_list)
16752 error ("constructor delegation follows mem-initializer for %qD",
16753 TREE_PURPOSE (mem_initializer_list));
16754 mem_initializer = error_mark_node;
16756 target_ctor = mem_initializer;
16758 /* Add it to the list, unless it was erroneous. */
16759 if (mem_initializer != error_mark_node)
16761 TREE_CHAIN (mem_initializer) = mem_initializer_list;
16762 mem_initializer_list = mem_initializer;
16764 /* If the next token is not a `,', we're done. */
16765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16766 break;
16767 /* Consume the `,' token. */
16768 cp_lexer_consume_token (parser->lexer);
16771 /* Perform semantic analysis. */
16772 if (DECL_CONSTRUCTOR_P (current_function_decl))
16773 finish_mem_initializers (mem_initializer_list);
16776 /* Parse a mem-initializer.
16778 mem-initializer:
16779 mem-initializer-id ( expression-list [opt] )
16780 mem-initializer-id braced-init-list
16782 GNU extension:
16784 mem-initializer:
16785 ( expression-list [opt] )
16787 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
16788 class) or FIELD_DECL (for a non-static data member) to initialize;
16789 the TREE_VALUE is the expression-list. An empty initialization
16790 list is represented by void_list_node. */
16792 static tree
16793 cp_parser_mem_initializer (cp_parser* parser)
16795 tree mem_initializer_id;
16796 tree expression_list;
16797 tree member;
16798 cp_token *token = cp_lexer_peek_token (parser->lexer);
16800 /* Find out what is being initialized. */
16801 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16803 permerror (token->location,
16804 "anachronistic old-style base class initializer");
16805 mem_initializer_id = NULL_TREE;
16807 else
16809 mem_initializer_id = cp_parser_mem_initializer_id (parser);
16810 if (mem_initializer_id == error_mark_node)
16811 return mem_initializer_id;
16813 member = expand_member_init (mem_initializer_id);
16814 if (member && !DECL_P (member))
16815 in_base_initializer = 1;
16817 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16819 bool expr_non_constant_p;
16820 cp_lexer_set_source_position (parser->lexer);
16821 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16822 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
16823 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
16824 expression_list = build_tree_list (NULL_TREE, expression_list);
16826 else
16828 vec<tree, va_gc> *vec;
16829 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16830 /*cast_p=*/false,
16831 /*allow_expansion_p=*/true,
16832 /*non_constant_p=*/NULL,
16833 /*close_paren_loc=*/NULL,
16834 /*wrap_locations_p=*/true);
16835 if (vec == NULL)
16836 return error_mark_node;
16837 expression_list = build_tree_list_vec (vec);
16838 release_tree_vector (vec);
16841 if (expression_list == error_mark_node)
16842 return error_mark_node;
16843 if (!expression_list)
16844 expression_list = void_type_node;
16846 in_base_initializer = 0;
16848 if (!member)
16849 return error_mark_node;
16850 tree node = build_tree_list (member, expression_list);
16852 /* We can't attach the source location of this initializer directly to
16853 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
16854 within the TREE_TYPE of the list node. */
16855 location_t loc
16856 = make_location (token->location, token->location, parser->lexer);
16857 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
16858 SET_EXPR_LOCATION (dummy, loc);
16859 TREE_TYPE (node) = dummy;
16861 return node;
16864 /* Parse a mem-initializer-id.
16866 mem-initializer-id:
16867 :: [opt] nested-name-specifier [opt] class-name
16868 decltype-specifier (C++11)
16869 identifier
16871 Returns a TYPE indicating the class to be initialized for the first
16872 production (and the second in C++11). Returns an IDENTIFIER_NODE
16873 indicating the data member to be initialized for the last production. */
16875 static tree
16876 cp_parser_mem_initializer_id (cp_parser* parser)
16878 bool global_scope_p;
16879 bool nested_name_specifier_p;
16880 bool template_p = false;
16881 tree id;
16883 cp_token *token = cp_lexer_peek_token (parser->lexer);
16885 /* `typename' is not allowed in this context ([temp.res]). */
16886 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16888 error_at (token->location,
16889 "keyword %<typename%> not allowed in this context (a qualified "
16890 "member initializer is implicitly a type)");
16891 cp_lexer_consume_token (parser->lexer);
16893 /* Look for the optional `::' operator. */
16894 global_scope_p
16895 = (cp_parser_global_scope_opt (parser,
16896 /*current_scope_valid_p=*/false)
16897 != NULL_TREE);
16898 /* Look for the optional nested-name-specifier. The simplest way to
16899 implement:
16901 [temp.res]
16903 The keyword `typename' is not permitted in a base-specifier or
16904 mem-initializer; in these contexts a qualified name that
16905 depends on a template-parameter is implicitly assumed to be a
16906 type name.
16908 is to assume that we have seen the `typename' keyword at this
16909 point. */
16910 nested_name_specifier_p
16911 = (cp_parser_nested_name_specifier_opt (parser,
16912 /*typename_keyword_p=*/true,
16913 /*check_dependency_p=*/true,
16914 /*type_p=*/true,
16915 /*is_declaration=*/true)
16916 != NULL_TREE);
16917 if (nested_name_specifier_p)
16918 template_p = cp_parser_optional_template_keyword (parser);
16919 /* If there is a `::' operator or a nested-name-specifier, then we
16920 are definitely looking for a class-name. */
16921 if (global_scope_p || nested_name_specifier_p)
16922 return cp_parser_class_name (parser,
16923 /*typename_keyword_p=*/true,
16924 /*template_keyword_p=*/template_p,
16925 typename_type,
16926 /*check_dependency_p=*/true,
16927 /*class_head_p=*/false,
16928 /*is_declaration=*/true);
16929 /* Otherwise, we could also be looking for an ordinary identifier. */
16930 cp_parser_parse_tentatively (parser);
16931 if (cp_lexer_next_token_is_decltype (parser->lexer))
16932 /* Try a decltype-specifier. */
16933 id = cp_parser_decltype (parser);
16934 else
16935 /* Otherwise, try a class-name. */
16936 id = cp_parser_class_name (parser,
16937 /*typename_keyword_p=*/true,
16938 /*template_keyword_p=*/false,
16939 none_type,
16940 /*check_dependency_p=*/true,
16941 /*class_head_p=*/false,
16942 /*is_declaration=*/true);
16943 /* If we found one, we're done. */
16944 if (cp_parser_parse_definitely (parser))
16945 return id;
16946 /* Otherwise, look for an ordinary identifier. */
16947 return cp_parser_identifier (parser);
16950 /* Overloading [gram.over] */
16952 /* Parse an operator-function-id.
16954 operator-function-id:
16955 operator operator
16957 Returns an IDENTIFIER_NODE for the operator which is a
16958 human-readable spelling of the identifier, e.g., `operator +'. */
16960 static cp_expr
16961 cp_parser_operator_function_id (cp_parser* parser)
16963 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
16964 /* Look for the `operator' keyword. */
16965 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16966 return error_mark_node;
16967 /* And then the name of the operator itself. */
16968 return cp_parser_operator (parser, start_loc);
16971 /* Return an identifier node for a user-defined literal operator.
16972 The suffix identifier is chained to the operator name identifier. */
16974 tree
16975 cp_literal_operator_id (const char* name)
16977 tree identifier;
16978 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
16979 + strlen (name) + 10);
16980 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
16981 identifier = get_identifier (buffer);
16982 XDELETEVEC (buffer);
16984 return identifier;
16987 /* Parse an operator.
16989 operator:
16990 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
16991 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
16992 || ++ -- , ->* -> () []
16994 GNU Extensions:
16996 operator:
16997 <? >? <?= >?=
16999 Returns an IDENTIFIER_NODE for the operator which is a
17000 human-readable spelling of the identifier, e.g., `operator +'. */
17002 static cp_expr
17003 cp_parser_operator (cp_parser* parser, location_t start_loc)
17005 tree id = NULL_TREE;
17006 cp_token *token;
17007 bool utf8 = false;
17009 /* Peek at the next token. */
17010 token = cp_lexer_peek_token (parser->lexer);
17012 location_t end_loc = token->location;
17014 /* Figure out which operator we have. */
17015 enum tree_code op = ERROR_MARK;
17016 bool assop = false;
17017 bool consumed = false;
17018 switch (token->type)
17020 case CPP_KEYWORD:
17022 /* The keyword should be either `new', `delete' or `co_await'. */
17023 if (token->keyword == RID_NEW)
17024 op = NEW_EXPR;
17025 else if (token->keyword == RID_DELETE)
17026 op = DELETE_EXPR;
17027 else if (token->keyword == RID_CO_AWAIT)
17028 op = CO_AWAIT_EXPR;
17029 else
17030 break;
17032 /* Consume the `new', `delete' or co_await token. */
17033 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17035 /* Peek at the next token. */
17036 token = cp_lexer_peek_token (parser->lexer);
17037 /* If it's a `[' token then this is the array variant of the
17038 operator. */
17039 if (token->type == CPP_OPEN_SQUARE
17040 && op != CO_AWAIT_EXPR)
17042 /* Consume the `[' token. */
17043 cp_lexer_consume_token (parser->lexer);
17044 /* Look for the `]' token. */
17045 if (cp_token *close_token
17046 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17047 end_loc = close_token->location;
17048 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17050 consumed = true;
17051 break;
17054 case CPP_PLUS:
17055 op = PLUS_EXPR;
17056 break;
17058 case CPP_MINUS:
17059 op = MINUS_EXPR;
17060 break;
17062 case CPP_MULT:
17063 op = MULT_EXPR;
17064 break;
17066 case CPP_DIV:
17067 op = TRUNC_DIV_EXPR;
17068 break;
17070 case CPP_MOD:
17071 op = TRUNC_MOD_EXPR;
17072 break;
17074 case CPP_XOR:
17075 op = BIT_XOR_EXPR;
17076 break;
17078 case CPP_AND:
17079 op = BIT_AND_EXPR;
17080 break;
17082 case CPP_OR:
17083 op = BIT_IOR_EXPR;
17084 break;
17086 case CPP_COMPL:
17087 op = BIT_NOT_EXPR;
17088 break;
17090 case CPP_NOT:
17091 op = TRUTH_NOT_EXPR;
17092 break;
17094 case CPP_EQ:
17095 assop = true;
17096 op = NOP_EXPR;
17097 break;
17099 case CPP_LESS:
17100 op = LT_EXPR;
17101 break;
17103 case CPP_GREATER:
17104 op = GT_EXPR;
17105 break;
17107 case CPP_PLUS_EQ:
17108 assop = true;
17109 op = PLUS_EXPR;
17110 break;
17112 case CPP_MINUS_EQ:
17113 assop = true;
17114 op = MINUS_EXPR;
17115 break;
17117 case CPP_MULT_EQ:
17118 assop = true;
17119 op = MULT_EXPR;
17120 break;
17122 case CPP_DIV_EQ:
17123 assop = true;
17124 op = TRUNC_DIV_EXPR;
17125 break;
17127 case CPP_MOD_EQ:
17128 assop = true;
17129 op = TRUNC_MOD_EXPR;
17130 break;
17132 case CPP_XOR_EQ:
17133 assop = true;
17134 op = BIT_XOR_EXPR;
17135 break;
17137 case CPP_AND_EQ:
17138 assop = true;
17139 op = BIT_AND_EXPR;
17140 break;
17142 case CPP_OR_EQ:
17143 assop = true;
17144 op = BIT_IOR_EXPR;
17145 break;
17147 case CPP_LSHIFT:
17148 op = LSHIFT_EXPR;
17149 break;
17151 case CPP_RSHIFT:
17152 op = RSHIFT_EXPR;
17153 break;
17155 case CPP_LSHIFT_EQ:
17156 assop = true;
17157 op = LSHIFT_EXPR;
17158 break;
17160 case CPP_RSHIFT_EQ:
17161 assop = true;
17162 op = RSHIFT_EXPR;
17163 break;
17165 case CPP_EQ_EQ:
17166 op = EQ_EXPR;
17167 break;
17169 case CPP_NOT_EQ:
17170 op = NE_EXPR;
17171 break;
17173 case CPP_LESS_EQ:
17174 op = LE_EXPR;
17175 break;
17177 case CPP_GREATER_EQ:
17178 op = GE_EXPR;
17179 break;
17181 case CPP_SPACESHIP:
17182 op = SPACESHIP_EXPR;
17183 break;
17185 case CPP_AND_AND:
17186 op = TRUTH_ANDIF_EXPR;
17187 break;
17189 case CPP_OR_OR:
17190 op = TRUTH_ORIF_EXPR;
17191 break;
17193 case CPP_PLUS_PLUS:
17194 op = POSTINCREMENT_EXPR;
17195 break;
17197 case CPP_MINUS_MINUS:
17198 op = PREDECREMENT_EXPR;
17199 break;
17201 case CPP_COMMA:
17202 op = COMPOUND_EXPR;
17203 break;
17205 case CPP_DEREF_STAR:
17206 op = MEMBER_REF;
17207 break;
17209 case CPP_DEREF:
17210 op = COMPONENT_REF;
17211 break;
17213 case CPP_QUERY:
17214 op = COND_EXPR;
17215 /* Consume the `?'. */
17216 cp_lexer_consume_token (parser->lexer);
17217 /* Look for the matching `:'. */
17218 cp_parser_require (parser, CPP_COLON, RT_COLON);
17219 consumed = true;
17220 break;
17222 case CPP_OPEN_PAREN:
17224 /* Consume the `('. */
17225 matching_parens parens;
17226 parens.consume_open (parser);
17227 /* Look for the matching `)'. */
17228 token = parens.require_close (parser);
17229 if (token)
17230 end_loc = token->location;
17231 op = CALL_EXPR;
17232 consumed = true;
17233 break;
17236 case CPP_OPEN_SQUARE:
17237 /* Consume the `['. */
17238 cp_lexer_consume_token (parser->lexer);
17239 /* Look for the matching `]'. */
17240 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17241 if (token)
17242 end_loc = token->location;
17243 op = ARRAY_REF;
17244 consumed = true;
17245 break;
17247 case CPP_UTF8STRING:
17248 case CPP_UTF8STRING_USERDEF:
17249 utf8 = true;
17250 /* FALLTHRU */
17251 case CPP_STRING:
17252 case CPP_WSTRING:
17253 case CPP_STRING16:
17254 case CPP_STRING32:
17255 case CPP_STRING_USERDEF:
17256 case CPP_WSTRING_USERDEF:
17257 case CPP_STRING16_USERDEF:
17258 case CPP_STRING32_USERDEF:
17260 cp_expr str;
17261 tree string_tree;
17262 int sz, len;
17264 if (cxx_dialect == cxx98)
17265 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17267 /* Consume the string. */
17268 str = cp_parser_string_literal (parser, /*translate=*/true,
17269 /*wide_ok=*/true, /*lookup_udlit=*/false);
17270 if (str == error_mark_node)
17271 return error_mark_node;
17272 else if (TREE_CODE (str) == USERDEF_LITERAL)
17274 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17275 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17276 end_loc = str.get_location ();
17278 else
17280 string_tree = str;
17281 /* Look for the suffix identifier. */
17282 token = cp_lexer_peek_token (parser->lexer);
17283 if (token->type == CPP_NAME)
17285 id = cp_parser_identifier (parser);
17286 end_loc = token->location;
17288 else if (token->type == CPP_KEYWORD)
17290 error ("unexpected keyword;"
17291 " remove space between quotes and suffix identifier");
17292 return error_mark_node;
17294 else
17296 error ("expected suffix identifier");
17297 return error_mark_node;
17300 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17301 (TREE_TYPE (TREE_TYPE (string_tree))));
17302 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17303 if (len != 0)
17305 error ("expected empty string after %<operator%> keyword");
17306 return error_mark_node;
17308 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17309 != char_type_node)
17311 error ("invalid encoding prefix in literal operator");
17312 return error_mark_node;
17314 if (id != error_mark_node)
17316 const char *name = IDENTIFIER_POINTER (id);
17317 id = cp_literal_operator_id (name);
17319 /* Generate a location of the form:
17320 "" _suffix_identifier
17321 ^~~~~~~~~~~~~~~~~~~~~
17322 with caret == start at the start token, finish at the end of the
17323 suffix identifier. */
17324 location_t combined_loc
17325 = make_location (start_loc, start_loc, parser->lexer);
17326 return cp_expr (id, combined_loc);
17329 default:
17330 /* Anything else is an error. */
17331 break;
17334 /* If we have selected an identifier, we need to consume the
17335 operator token. */
17336 if (op != ERROR_MARK)
17338 id = ovl_op_identifier (assop, op);
17339 if (!consumed)
17340 cp_lexer_consume_token (parser->lexer);
17342 /* Otherwise, no valid operator name was present. */
17343 else
17345 cp_parser_error (parser, "expected operator");
17346 id = error_mark_node;
17349 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
17350 return cp_expr (id, start_loc);
17353 /* Parse a template-declaration.
17355 template-declaration:
17356 export [opt] template < template-parameter-list > declaration
17358 If MEMBER_P is TRUE, this template-declaration occurs within a
17359 class-specifier.
17361 The grammar rule given by the standard isn't correct. What
17362 is really meant is:
17364 template-declaration:
17365 export [opt] template-parameter-list-seq
17366 decl-specifier-seq [opt] init-declarator [opt] ;
17367 export [opt] template-parameter-list-seq
17368 function-definition
17370 template-parameter-list-seq:
17371 template-parameter-list-seq [opt]
17372 template < template-parameter-list >
17374 Concept Extensions:
17376 template-parameter-list-seq:
17377 template < template-parameter-list > requires-clause [opt]
17379 requires-clause:
17380 requires logical-or-expression */
17382 static void
17383 cp_parser_template_declaration (cp_parser* parser, bool member_p)
17385 /* Check for `export'. */
17386 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
17388 /* Consume the `export' token. */
17389 cp_lexer_consume_token (parser->lexer);
17390 /* Warn that this use of export is deprecated. */
17391 if (cxx_dialect < cxx11)
17392 warning (0, "keyword %<export%> not implemented, and will be ignored");
17393 else if (cxx_dialect < cxx20)
17394 warning (0, "keyword %<export%> is deprecated, and is ignored");
17395 else
17396 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17399 cp_parser_template_declaration_after_export (parser, member_p);
17402 /* Parse a template-parameter-list.
17404 template-parameter-list:
17405 template-parameter
17406 template-parameter-list , template-parameter
17408 Returns a TREE_LIST. Each node represents a template parameter.
17409 The nodes are connected via their TREE_CHAINs. */
17411 static tree
17412 cp_parser_template_parameter_list (cp_parser* parser)
17414 tree parameter_list = NULL_TREE;
17416 /* Don't create wrapper nodes within a template-parameter-list,
17417 since we don't want to have different types based on the
17418 spelling location of constants and decls within them. */
17419 auto_suppress_location_wrappers sentinel;
17421 begin_template_parm_list ();
17423 /* The loop below parses the template parms. We first need to know
17424 the total number of template parms to be able to compute proper
17425 canonical types of each dependent type. So after the loop, when
17426 we know the total number of template parms,
17427 end_template_parm_list computes the proper canonical types and
17428 fixes up the dependent types accordingly. */
17429 while (true)
17431 tree parameter;
17432 bool is_non_type;
17433 bool is_parameter_pack;
17434 location_t parm_loc;
17436 /* Parse the template-parameter. */
17437 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
17438 parameter = cp_parser_template_parameter (parser,
17439 &is_non_type,
17440 &is_parameter_pack);
17441 /* Add it to the list. */
17442 if (parameter != error_mark_node)
17443 parameter_list = process_template_parm (parameter_list,
17444 parm_loc,
17445 parameter,
17446 is_non_type,
17447 is_parameter_pack);
17448 else
17450 tree err_parm = build_tree_list (parameter, parameter);
17451 parameter_list = chainon (parameter_list, err_parm);
17454 /* If the next token is not a `,', we're done. */
17455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17456 break;
17457 /* Otherwise, consume the `,' token. */
17458 cp_lexer_consume_token (parser->lexer);
17461 return end_template_parm_list (parameter_list);
17464 /* Parse a introduction-list.
17466 introduction-list:
17467 introduced-parameter
17468 introduction-list , introduced-parameter
17470 introduced-parameter:
17471 ...[opt] identifier
17473 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
17474 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
17475 WILDCARD_DECL will also have DECL_NAME set and token location in
17476 DECL_SOURCE_LOCATION. */
17478 static tree
17479 cp_parser_introduction_list (cp_parser *parser)
17481 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
17483 while (true)
17485 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17486 if (is_pack)
17487 cp_lexer_consume_token (parser->lexer);
17489 tree identifier = cp_parser_identifier (parser);
17490 if (identifier == error_mark_node)
17491 break;
17493 /* Build placeholder. */
17494 tree parm = build_nt (WILDCARD_DECL);
17495 DECL_SOURCE_LOCATION (parm)
17496 = cp_lexer_peek_token (parser->lexer)->location;
17497 DECL_NAME (parm) = identifier;
17498 WILDCARD_PACK_P (parm) = is_pack;
17499 vec_safe_push (introduction_vec, parm);
17501 /* If the next token is not a `,', we're done. */
17502 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17503 break;
17504 /* Otherwise, consume the `,' token. */
17505 cp_lexer_consume_token (parser->lexer);
17508 /* Convert the vec into a TREE_VEC. */
17509 tree introduction_list = make_tree_vec (introduction_vec->length ());
17510 unsigned int n;
17511 tree parm;
17512 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
17513 TREE_VEC_ELT (introduction_list, n) = parm;
17515 release_tree_vector (introduction_vec);
17516 return introduction_list;
17519 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17520 is an abstract declarator. */
17522 static inline cp_declarator*
17523 get_id_declarator (cp_declarator *declarator)
17525 cp_declarator *d = declarator;
17526 while (d && d->kind != cdk_id)
17527 d = d->declarator;
17528 return d;
17531 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17532 is an abstract declarator. */
17534 static inline tree
17535 get_unqualified_id (cp_declarator *declarator)
17537 declarator = get_id_declarator (declarator);
17538 if (declarator)
17539 return declarator->u.id.unqualified_name;
17540 else
17541 return NULL_TREE;
17544 /* Returns true if TYPE would declare a constrained constrained-parameter. */
17546 static inline bool
17547 is_constrained_parameter (tree type)
17549 return (type
17550 && TREE_CODE (type) == TYPE_DECL
17551 && CONSTRAINED_PARM_CONCEPT (type)
17552 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
17555 /* Returns true if PARM declares a constrained-parameter. */
17557 static inline bool
17558 is_constrained_parameter (cp_parameter_declarator *parm)
17560 return is_constrained_parameter (parm->decl_specifiers.type);
17563 /* Check that the type parameter is only a declarator-id, and that its
17564 type is not cv-qualified. */
17566 bool
17567 cp_parser_check_constrained_type_parm (cp_parser *parser,
17568 cp_parameter_declarator *parm)
17570 if (!parm->declarator)
17571 return true;
17573 if (parm->declarator->kind != cdk_id)
17575 cp_parser_error (parser, "invalid constrained type parameter");
17576 return false;
17579 /* Don't allow cv-qualified type parameters. */
17580 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
17581 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
17583 cp_parser_error (parser, "cv-qualified type parameter");
17584 return false;
17587 return true;
17590 /* Finish parsing/processing a template type parameter and checking
17591 various restrictions. */
17593 static inline tree
17594 cp_parser_constrained_type_template_parm (cp_parser *parser,
17595 tree id,
17596 cp_parameter_declarator* parmdecl)
17598 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
17599 return finish_template_type_parm (class_type_node, id);
17600 else
17601 return error_mark_node;
17604 static tree
17605 finish_constrained_template_template_parm (tree proto, tree id)
17607 /* FIXME: This should probably be copied, and we may need to adjust
17608 the template parameter depths. */
17609 tree saved_parms = current_template_parms;
17610 begin_template_parm_list ();
17611 current_template_parms = DECL_TEMPLATE_PARMS (proto);
17612 end_template_parm_list ();
17614 tree parm = finish_template_template_parm (class_type_node, id);
17615 current_template_parms = saved_parms;
17617 return parm;
17620 /* Finish parsing/processing a template template parameter by borrowing
17621 the template parameter list from the prototype parameter. */
17623 static tree
17624 cp_parser_constrained_template_template_parm (cp_parser *parser,
17625 tree proto,
17626 tree id,
17627 cp_parameter_declarator *parmdecl)
17629 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
17630 return error_mark_node;
17631 return finish_constrained_template_template_parm (proto, id);
17634 /* Create a new non-type template parameter from the given PARM
17635 declarator. */
17637 static tree
17638 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
17639 cp_parameter_declarator *parm)
17641 *is_non_type = true;
17642 cp_declarator *decl = parm->declarator;
17643 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
17644 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
17645 return grokdeclarator (decl, specs, TPARM, 0, NULL);
17648 /* Build a constrained template parameter based on the PARMDECL
17649 declarator. The type of PARMDECL is the constrained type, which
17650 refers to the prototype template parameter that ultimately
17651 specifies the type of the declared parameter. */
17653 static tree
17654 finish_constrained_parameter (cp_parser *parser,
17655 cp_parameter_declarator *parmdecl,
17656 bool *is_non_type)
17658 tree decl = parmdecl->decl_specifiers.type;
17659 tree id = get_unqualified_id (parmdecl->declarator);
17660 tree def = parmdecl->default_argument;
17661 tree proto = DECL_INITIAL (decl);
17663 /* Build the parameter. Return an error if the declarator was invalid. */
17664 tree parm;
17665 if (TREE_CODE (proto) == TYPE_DECL)
17666 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
17667 else if (TREE_CODE (proto) == TEMPLATE_DECL)
17668 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
17669 parmdecl);
17670 else
17671 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
17672 if (parm == error_mark_node)
17673 return error_mark_node;
17675 /* Finish the parameter decl and create a node attaching the
17676 default argument and constraint. */
17677 parm = build_tree_list (def, parm);
17678 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
17680 return parm;
17683 /* Returns true if the parsed type actually represents the declaration
17684 of a type template-parameter. */
17686 static bool
17687 declares_constrained_type_template_parameter (tree type)
17689 return (is_constrained_parameter (type)
17690 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
17693 /* Returns true if the parsed type actually represents the declaration of
17694 a template template-parameter. */
17696 static bool
17697 declares_constrained_template_template_parameter (tree type)
17699 return (is_constrained_parameter (type)
17700 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
17703 /* Parse a default argument for a type template-parameter.
17704 Note that diagnostics are handled in cp_parser_template_parameter. */
17706 static tree
17707 cp_parser_default_type_template_argument (cp_parser *parser)
17709 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17711 /* Consume the `=' token. */
17712 cp_lexer_consume_token (parser->lexer);
17714 cp_token *token = cp_lexer_peek_token (parser->lexer);
17716 /* Tell cp_parser_lambda_expression this is a default argument. */
17717 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
17718 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
17720 /* Parse the default-argument. */
17721 push_deferring_access_checks (dk_no_deferred);
17722 tree default_argument = cp_parser_type_id (parser,
17723 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17724 NULL);
17725 pop_deferring_access_checks ();
17727 if (flag_concepts && type_uses_auto (default_argument))
17729 error_at (token->location,
17730 "invalid use of %<auto%> in default template argument");
17731 return error_mark_node;
17734 return default_argument;
17737 /* Parse a default argument for a template template-parameter. */
17739 static tree
17740 cp_parser_default_template_template_argument (cp_parser *parser)
17742 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17744 bool is_template;
17746 /* Consume the `='. */
17747 cp_lexer_consume_token (parser->lexer);
17748 /* Parse the id-expression. */
17749 push_deferring_access_checks (dk_no_deferred);
17750 /* save token before parsing the id-expression, for error
17751 reporting */
17752 const cp_token* token = cp_lexer_peek_token (parser->lexer);
17753 tree default_argument
17754 = cp_parser_id_expression (parser,
17755 /*template_keyword_p=*/false,
17756 /*check_dependency_p=*/true,
17757 /*template_p=*/&is_template,
17758 /*declarator_p=*/false,
17759 /*optional_p=*/false);
17760 if (TREE_CODE (default_argument) == TYPE_DECL)
17761 /* If the id-expression was a template-id that refers to
17762 a template-class, we already have the declaration here,
17763 so no further lookup is needed. */
17765 else
17766 /* Look up the name. */
17767 default_argument
17768 = cp_parser_lookup_name (parser, default_argument,
17769 none_type,
17770 /*is_template=*/is_template,
17771 /*is_namespace=*/false,
17772 /*check_dependency=*/true,
17773 /*ambiguous_decls=*/NULL,
17774 token->location);
17775 /* See if the default argument is valid. */
17776 default_argument = check_template_template_default_arg (default_argument);
17777 pop_deferring_access_checks ();
17778 return default_argument;
17781 /* Parse a template-parameter.
17783 template-parameter:
17784 type-parameter
17785 parameter-declaration
17787 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
17788 the parameter. The TREE_PURPOSE is the default value, if any.
17789 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
17790 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
17791 set to true iff this parameter is a parameter pack. */
17793 static tree
17794 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
17795 bool *is_parameter_pack)
17797 cp_token *token;
17798 cp_parameter_declarator *parameter_declarator;
17799 tree parm;
17801 /* Assume it is a type parameter or a template parameter. */
17802 *is_non_type = false;
17803 /* Assume it not a parameter pack. */
17804 *is_parameter_pack = false;
17805 /* Peek at the next token. */
17806 token = cp_lexer_peek_token (parser->lexer);
17807 /* If it is `template', we have a type-parameter. */
17808 if (token->keyword == RID_TEMPLATE)
17809 return cp_parser_type_parameter (parser, is_parameter_pack);
17810 /* If it is `class' or `typename' we do not know yet whether it is a
17811 type parameter or a non-type parameter. Consider:
17813 template <typename T, typename T::X X> ...
17817 template <class C, class D*> ...
17819 Here, the first parameter is a type parameter, and the second is
17820 a non-type parameter. We can tell by looking at the token after
17821 the identifier -- if it is a `,', `=', or `>' then we have a type
17822 parameter. */
17823 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
17825 /* Peek at the token after `class' or `typename'. */
17826 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17827 /* If it's an ellipsis, we have a template type parameter
17828 pack. */
17829 if (token->type == CPP_ELLIPSIS)
17830 return cp_parser_type_parameter (parser, is_parameter_pack);
17831 /* If it's an identifier, skip it. */
17832 if (token->type == CPP_NAME)
17833 token = cp_lexer_peek_nth_token (parser->lexer, 3);
17834 /* Now, see if the token looks like the end of a template
17835 parameter. */
17836 if (token->type == CPP_COMMA
17837 || token->type == CPP_EQ
17838 || token->type == CPP_GREATER)
17839 return cp_parser_type_parameter (parser, is_parameter_pack);
17842 /* Otherwise, it is a non-type parameter or a constrained parameter.
17844 [temp.param]
17846 When parsing a default template-argument for a non-type
17847 template-parameter, the first non-nested `>' is taken as the end
17848 of the template parameter-list rather than a greater-than
17849 operator. */
17850 parameter_declarator
17851 = cp_parser_parameter_declaration (parser,
17852 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17853 /*template_parm_p=*/true,
17854 /*parenthesized_p=*/NULL);
17856 if (!parameter_declarator)
17857 return error_mark_node;
17859 /* If the parameter declaration is marked as a parameter pack, set
17860 *IS_PARAMETER_PACK to notify the caller. */
17861 if (parameter_declarator->template_parameter_pack_p)
17862 *is_parameter_pack = true;
17864 if (parameter_declarator->default_argument)
17866 /* Can happen in some cases of erroneous input (c++/34892). */
17867 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17868 /* Consume the `...' for better error recovery. */
17869 cp_lexer_consume_token (parser->lexer);
17872 /* The parameter may have been constrained type parameter. */
17873 if (is_constrained_parameter (parameter_declarator))
17874 return finish_constrained_parameter (parser,
17875 parameter_declarator,
17876 is_non_type);
17878 // Now we're sure that the parameter is a non-type parameter.
17879 *is_non_type = true;
17881 parm = grokdeclarator (parameter_declarator->declarator,
17882 &parameter_declarator->decl_specifiers,
17883 TPARM, /*initialized=*/0,
17884 /*attrlist=*/NULL);
17885 if (parm == error_mark_node)
17886 return error_mark_node;
17888 return build_tree_list (parameter_declarator->default_argument, parm);
17891 /* Parse a type-parameter.
17893 type-parameter:
17894 class identifier [opt]
17895 class identifier [opt] = type-id
17896 typename identifier [opt]
17897 typename identifier [opt] = type-id
17898 template < template-parameter-list > class identifier [opt]
17899 template < template-parameter-list > class identifier [opt]
17900 = id-expression
17902 GNU Extension (variadic templates):
17904 type-parameter:
17905 class ... identifier [opt]
17906 typename ... identifier [opt]
17908 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
17909 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
17910 the declaration of the parameter.
17912 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
17914 static tree
17915 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
17917 cp_token *token;
17918 tree parameter;
17920 /* Look for a keyword to tell us what kind of parameter this is. */
17921 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
17922 if (!token)
17923 return error_mark_node;
17925 switch (token->keyword)
17927 case RID_CLASS:
17928 case RID_TYPENAME:
17930 tree identifier;
17931 tree default_argument;
17933 /* If the next token is an ellipsis, we have a template
17934 argument pack. */
17935 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17937 /* Consume the `...' token. */
17938 cp_lexer_consume_token (parser->lexer);
17939 maybe_warn_variadic_templates ();
17941 *is_parameter_pack = true;
17944 /* If the next token is an identifier, then it names the
17945 parameter. */
17946 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17947 identifier = cp_parser_identifier (parser);
17948 else
17949 identifier = NULL_TREE;
17951 /* Create the parameter. */
17952 parameter = finish_template_type_parm (class_type_node, identifier);
17954 /* If the next token is an `=', we have a default argument. */
17955 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17957 default_argument
17958 = cp_parser_default_type_template_argument (parser);
17960 /* Template parameter packs cannot have default
17961 arguments. */
17962 if (*is_parameter_pack)
17964 if (identifier)
17965 error_at (token->location,
17966 "template parameter pack %qD cannot have a "
17967 "default argument", identifier);
17968 else
17969 error_at (token->location,
17970 "template parameter packs cannot have "
17971 "default arguments");
17972 default_argument = NULL_TREE;
17974 else if (check_for_bare_parameter_packs (default_argument))
17975 default_argument = error_mark_node;
17977 else
17978 default_argument = NULL_TREE;
17980 /* Create the combined representation of the parameter and the
17981 default argument. */
17982 parameter = build_tree_list (default_argument, parameter);
17984 break;
17986 case RID_TEMPLATE:
17988 tree identifier;
17989 tree default_argument;
17991 /* Look for the `<'. */
17992 cp_parser_require (parser, CPP_LESS, RT_LESS);
17993 /* Parse the template-parameter-list. */
17994 cp_parser_template_parameter_list (parser);
17995 /* Look for the `>'. */
17996 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17998 /* If template requirements are present, parse them. */
17999 if (flag_concepts)
18001 tree reqs = get_shorthand_constraints (current_template_parms);
18002 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18003 reqs = combine_constraint_expressions (reqs, dreqs);
18004 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18007 /* Look for the `class' or 'typename' keywords. */
18008 cp_parser_type_parameter_key (parser);
18009 /* If the next token is an ellipsis, we have a template
18010 argument pack. */
18011 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18013 /* Consume the `...' token. */
18014 cp_lexer_consume_token (parser->lexer);
18015 maybe_warn_variadic_templates ();
18017 *is_parameter_pack = true;
18019 /* If the next token is an `=', then there is a
18020 default-argument. If the next token is a `>', we are at
18021 the end of the parameter-list. If the next token is a `,',
18022 then we are at the end of this parameter. */
18023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18024 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18025 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18027 identifier = cp_parser_identifier (parser);
18028 /* Treat invalid names as if the parameter were nameless. */
18029 if (identifier == error_mark_node)
18030 identifier = NULL_TREE;
18032 else
18033 identifier = NULL_TREE;
18035 /* Create the template parameter. */
18036 parameter = finish_template_template_parm (class_type_node,
18037 identifier);
18039 /* If the next token is an `=', then there is a
18040 default-argument. */
18041 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18043 default_argument
18044 = cp_parser_default_template_template_argument (parser);
18046 /* Template parameter packs cannot have default
18047 arguments. */
18048 if (*is_parameter_pack)
18050 if (identifier)
18051 error_at (token->location,
18052 "template parameter pack %qD cannot "
18053 "have a default argument",
18054 identifier);
18055 else
18056 error_at (token->location, "template parameter packs cannot "
18057 "have default arguments");
18058 default_argument = NULL_TREE;
18061 else
18062 default_argument = NULL_TREE;
18064 /* Create the combined representation of the parameter and the
18065 default argument. */
18066 parameter = build_tree_list (default_argument, parameter);
18068 break;
18070 default:
18071 gcc_unreachable ();
18072 break;
18075 return parameter;
18078 /* Parse a template-id.
18080 template-id:
18081 template-name < template-argument-list [opt] >
18083 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18084 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18085 returned. Otherwise, if the template-name names a function, or set
18086 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18087 names a class, returns a TYPE_DECL for the specialization.
18089 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18090 uninstantiated templates. */
18092 static tree
18093 cp_parser_template_id (cp_parser *parser,
18094 bool template_keyword_p,
18095 bool check_dependency_p,
18096 enum tag_types tag_type,
18097 bool is_declaration)
18099 tree templ;
18100 tree arguments;
18101 tree template_id;
18102 cp_token_position start_of_id = 0;
18103 cp_token *next_token = NULL, *next_token_2 = NULL;
18104 bool is_identifier;
18106 /* If the next token corresponds to a template-id, there is no need
18107 to reparse it. */
18108 cp_token *token = cp_lexer_peek_token (parser->lexer);
18110 if (token->type == CPP_TEMPLATE_ID)
18112 cp_lexer_consume_token (parser->lexer);
18113 return saved_checks_value (token->u.tree_check_value);
18116 /* Avoid performing name lookup if there is no possibility of
18117 finding a template-id. */
18118 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18119 || (token->type == CPP_NAME
18120 && !cp_parser_nth_token_starts_template_argument_list_p
18121 (parser, 2)))
18123 cp_parser_error (parser, "expected template-id");
18124 return error_mark_node;
18127 /* Remember where the template-id starts. */
18128 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18129 start_of_id = cp_lexer_token_position (parser->lexer, false);
18131 push_deferring_access_checks (dk_deferred);
18133 /* Parse the template-name. */
18134 is_identifier = false;
18135 templ = cp_parser_template_name (parser, template_keyword_p,
18136 check_dependency_p,
18137 is_declaration,
18138 tag_type,
18139 &is_identifier);
18141 /* Push any access checks inside the firewall we're about to create. */
18142 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18143 pop_deferring_access_checks ();
18144 if (templ == error_mark_node || is_identifier)
18145 return templ;
18147 /* Since we're going to preserve any side-effects from this parse, set up a
18148 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18149 in the template arguments. */
18150 tentative_firewall firewall (parser);
18151 reopen_deferring_access_checks (checks);
18153 /* If we find the sequence `[:' after a template-name, it's probably
18154 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18155 parse correctly the argument list. */
18156 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18157 == CPP_OPEN_SQUARE)
18158 && next_token->flags & DIGRAPH
18159 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18160 == CPP_COLON)
18161 && !(next_token_2->flags & PREV_WHITE))
18163 cp_parser_parse_tentatively (parser);
18164 /* Change `:' into `::'. */
18165 next_token_2->type = CPP_SCOPE;
18166 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18167 CPP_LESS. */
18168 cp_lexer_consume_token (parser->lexer);
18170 /* Parse the arguments. */
18171 arguments = cp_parser_enclosed_template_argument_list (parser);
18172 if (!cp_parser_parse_definitely (parser))
18174 /* If we couldn't parse an argument list, then we revert our changes
18175 and return simply an error. Maybe this is not a template-id
18176 after all. */
18177 next_token_2->type = CPP_COLON;
18178 cp_parser_error (parser, "expected %<<%>");
18179 pop_deferring_access_checks ();
18180 return error_mark_node;
18182 /* Otherwise, emit an error about the invalid digraph, but continue
18183 parsing because we got our argument list. */
18184 if (permerror (next_token->location,
18185 "%<<::%> cannot begin a template-argument list"))
18187 static bool hint = false;
18188 inform (next_token->location,
18189 "%<<:%> is an alternate spelling for %<[%>."
18190 " Insert whitespace between %<<%> and %<::%>");
18191 if (!hint && !flag_permissive)
18193 inform (next_token->location, "(if you use %<-fpermissive%> "
18194 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18195 "accept your code)");
18196 hint = true;
18200 else
18202 /* Look for the `<' that starts the template-argument-list. */
18203 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18205 pop_deferring_access_checks ();
18206 return error_mark_node;
18208 /* Parse the arguments. */
18209 arguments = cp_parser_enclosed_template_argument_list (parser);
18211 if ((cxx_dialect > cxx17)
18212 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18213 && !template_keyword_p
18214 && (cp_parser_error_occurred (parser)
18215 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18217 /* This didn't go well. */
18218 if (TREE_CODE (templ) == FUNCTION_DECL)
18220 /* C++20 says that "function-name < a;" is now ill-formed. */
18221 if (cp_parser_error_occurred (parser))
18223 error_at (token->location, "invalid template-argument-list");
18224 inform (token->location, "function name as the left hand "
18225 "operand of %<<%> is ill-formed in C++20; wrap the "
18226 "function name in %<()%>");
18228 else
18229 /* We expect "f<targs>" to be followed by "(args)". */
18230 error_at (cp_lexer_peek_token (parser->lexer)->location,
18231 "expected %<(%> after template-argument-list");
18232 if (start_of_id)
18233 /* Purge all subsequent tokens. */
18234 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18236 else
18237 cp_parser_simulate_error (parser);
18238 pop_deferring_access_checks ();
18239 return error_mark_node;
18243 /* Set the location to be of the form:
18244 template-name < template-argument-list [opt] >
18245 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18246 with caret == start at the start of the template-name,
18247 ranging until the closing '>'. */
18248 location_t combined_loc
18249 = make_location (token->location, token->location, parser->lexer);
18251 /* Check for concepts autos where they don't belong. We could
18252 identify types in some cases of identifier TEMPL, looking ahead
18253 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18254 types. We reject them in functions, but if what we have is an
18255 identifier, even with none_type we can't conclude it's NOT a
18256 type, we have to wait for template substitution. */
18257 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18258 template_id = error_mark_node;
18259 /* Build a representation of the specialization. */
18260 else if (identifier_p (templ))
18261 template_id = build_min_nt_loc (combined_loc,
18262 TEMPLATE_ID_EXPR,
18263 templ, arguments);
18264 else if (DECL_TYPE_TEMPLATE_P (templ)
18265 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18267 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18268 template (rather than some instantiation thereof) only if
18269 is not nested within some other construct. For example, in
18270 "template <typename T> void f(T) { A<T>::", A<T> is just an
18271 instantiation of A. */
18272 bool entering_scope
18273 = (template_parm_scope_p ()
18274 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18275 template_id
18276 = finish_template_type (templ, arguments, entering_scope);
18278 else if (concept_definition_p (templ))
18280 /* The caller will decide whether this is a concept check or type
18281 constraint. */
18282 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18283 boolean_type_node, templ, arguments);
18285 else if (variable_template_p (templ))
18287 template_id = lookup_template_variable (templ, arguments);
18288 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18289 SET_EXPR_LOCATION (template_id, combined_loc);
18291 else if (TREE_CODE (templ) == TYPE_DECL
18292 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18294 /* Some type template in dependent scope. */
18295 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18296 name = build_min_nt_loc (combined_loc,
18297 TEMPLATE_ID_EXPR,
18298 name, arguments);
18299 template_id = templ;
18301 else
18303 /* If it's not a class-template or a template-template, it should be
18304 a function-template. */
18305 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18307 template_id = lookup_template_function (templ, arguments);
18308 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18309 SET_EXPR_LOCATION (template_id, combined_loc);
18312 /* If parsing tentatively, replace the sequence of tokens that makes
18313 up the template-id with a CPP_TEMPLATE_ID token. That way,
18314 should we re-parse the token stream, we will not have to repeat
18315 the effort required to do the parse, nor will we issue duplicate
18316 error messages about problems during instantiation of the
18317 template. */
18318 if (start_of_id
18319 /* Don't do this if we had a parse error in a declarator; re-parsing
18320 might succeed if a name changes meaning (60361). */
18321 && !(cp_parser_error_occurred (parser)
18322 && cp_parser_parsing_tentatively (parser)
18323 && parser->in_declarator_p))
18325 /* Reset the contents of the START_OF_ID token. */
18326 token->type = CPP_TEMPLATE_ID;
18327 token->location = combined_loc;
18329 /* Retrieve any deferred checks. Do not pop this access checks yet
18330 so the memory will not be reclaimed during token replacing below. */
18331 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18332 token->tree_check_p = true;
18333 token->u.tree_check_value->value = template_id;
18334 token->u.tree_check_value->checks = get_deferred_access_checks ();
18335 token->keyword = RID_MAX;
18337 /* Purge all subsequent tokens. */
18338 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18340 /* ??? Can we actually assume that, if template_id ==
18341 error_mark_node, we will have issued a diagnostic to the
18342 user, as opposed to simply marking the tentative parse as
18343 failed? */
18344 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
18345 error_at (token->location, "parse error in template argument list");
18348 pop_to_parent_deferring_access_checks ();
18349 return template_id;
18352 /* Like cp_parser_template_id, called in non-type context. */
18354 static tree
18355 cp_parser_template_id_expr (cp_parser *parser,
18356 bool template_keyword_p,
18357 bool check_dependency_p,
18358 bool is_declaration)
18360 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
18361 none_type, is_declaration);
18362 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
18363 && concept_check_p (x))
18364 /* We didn't check the arguments in cp_parser_template_id; do that now. */
18365 return build_concept_id (x);
18366 return x;
18369 /* Parse a template-name.
18371 template-name:
18372 identifier
18374 The standard should actually say:
18376 template-name:
18377 identifier
18378 operator-function-id
18380 A defect report has been filed about this issue.
18382 A conversion-function-id cannot be a template name because they cannot
18383 be part of a template-id. In fact, looking at this code:
18385 a.operator K<int>()
18387 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18388 It is impossible to call a templated conversion-function-id with an
18389 explicit argument list, since the only allowed template parameter is
18390 the type to which it is converting.
18392 If TEMPLATE_KEYWORD_P is true, then we have just seen the
18393 `template' keyword, in a construction like:
18395 T::template f<3>()
18397 In that case `f' is taken to be a template-name, even though there
18398 is no way of knowing for sure.
18400 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18401 name refers to a set of overloaded functions, at least one of which
18402 is a template, or an IDENTIFIER_NODE with the name of the template,
18403 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
18404 names are looked up inside uninstantiated templates. */
18406 static tree
18407 cp_parser_template_name (cp_parser* parser,
18408 bool template_keyword_p,
18409 bool check_dependency_p,
18410 bool is_declaration,
18411 enum tag_types tag_type,
18412 bool *is_identifier)
18414 tree identifier;
18415 tree decl;
18416 cp_token *token = cp_lexer_peek_token (parser->lexer);
18418 /* If the next token is `operator', then we have either an
18419 operator-function-id or a conversion-function-id. */
18420 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
18422 /* We don't know whether we're looking at an
18423 operator-function-id or a conversion-function-id. */
18424 cp_parser_parse_tentatively (parser);
18425 /* Try an operator-function-id. */
18426 identifier = cp_parser_operator_function_id (parser);
18427 /* If that didn't work, try a conversion-function-id. */
18428 if (!cp_parser_parse_definitely (parser))
18430 cp_parser_error (parser, "expected template-name");
18431 return error_mark_node;
18434 /* Look for the identifier. */
18435 else
18436 identifier = cp_parser_identifier (parser);
18438 /* If we didn't find an identifier, we don't have a template-id. */
18439 if (identifier == error_mark_node)
18440 return error_mark_node;
18442 /* If the name immediately followed the `template' keyword, then it
18443 is a template-name. However, if the next token is not `<', then
18444 we do not treat it as a template-name, since it is not being used
18445 as part of a template-id. This enables us to handle constructs
18446 like:
18448 template <typename T> struct S { S(); };
18449 template <typename T> S<T>::S();
18451 correctly. We would treat `S' as a template -- if it were `S<T>'
18452 -- but we do not if there is no `<'. */
18454 if (processing_template_decl
18455 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18457 /* In a declaration, in a dependent context, we pretend that the
18458 "template" keyword was present in order to improve error
18459 recovery. For example, given:
18461 template <typename T> void f(T::X<int>);
18463 we want to treat "X<int>" as a template-id. */
18464 if (is_declaration
18465 && !template_keyword_p
18466 && parser->scope && TYPE_P (parser->scope)
18467 && check_dependency_p
18468 && dependent_scope_p (parser->scope)
18469 /* Do not do this for dtors (or ctors), since they never
18470 need the template keyword before their name. */
18471 && !constructor_name_p (identifier, parser->scope))
18473 cp_token_position start = 0;
18475 /* Explain what went wrong. */
18476 error_at (token->location, "non-template %qD used as template",
18477 identifier);
18478 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
18479 parser->scope, identifier);
18480 /* If parsing tentatively, find the location of the "<" token. */
18481 if (cp_parser_simulate_error (parser))
18482 start = cp_lexer_token_position (parser->lexer, true);
18483 /* Parse the template arguments so that we can issue error
18484 messages about them. */
18485 cp_lexer_consume_token (parser->lexer);
18486 cp_parser_enclosed_template_argument_list (parser);
18487 /* Skip tokens until we find a good place from which to
18488 continue parsing. */
18489 cp_parser_skip_to_closing_parenthesis (parser,
18490 /*recovering=*/true,
18491 /*or_comma=*/true,
18492 /*consume_paren=*/false);
18493 /* If parsing tentatively, permanently remove the
18494 template argument list. That will prevent duplicate
18495 error messages from being issued about the missing
18496 "template" keyword. */
18497 if (start)
18498 cp_lexer_purge_tokens_after (parser->lexer, start);
18499 if (is_identifier)
18500 *is_identifier = true;
18501 parser->context->object_type = NULL_TREE;
18502 return identifier;
18505 /* If the "template" keyword is present, then there is generally
18506 no point in doing name-lookup, so we just return IDENTIFIER.
18507 But, if the qualifying scope is non-dependent then we can
18508 (and must) do name-lookup normally. */
18509 if (template_keyword_p)
18511 tree scope = (parser->scope ? parser->scope
18512 : parser->context->object_type);
18513 if (scope && TYPE_P (scope)
18514 && (!CLASS_TYPE_P (scope)
18515 || (check_dependency_p && dependent_type_p (scope))))
18517 /* We're optimizing away the call to cp_parser_lookup_name, but
18518 we still need to do this. */
18519 parser->object_scope = parser->context->object_type;
18520 parser->context->object_type = NULL_TREE;
18521 return identifier;
18526 /* cp_parser_lookup_name clears OBJECT_TYPE. */
18527 tree scope = (parser->scope ? parser->scope
18528 : parser->context->object_type);
18530 /* Look up the name. */
18531 decl = cp_parser_lookup_name (parser, identifier,
18532 tag_type,
18533 /*is_template=*/true,
18534 /*is_namespace=*/false,
18535 check_dependency_p,
18536 /*ambiguous_decls=*/NULL,
18537 token->location);
18539 decl = strip_using_decl (decl);
18541 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18542 template-argument-list if it follows a name that is not a
18543 conversion-function-id and
18544 - that follows the keyword template or a ~ after a nested-name-specifier or
18545 in a class member access expression, or
18546 - for which name lookup finds the injected-class-name of a class template
18547 or finds any declaration of a template, or
18548 - that is an unqualified name for which name lookup either finds one or
18549 more functions or finds nothing, or
18550 - that is a terminal name in a using-declarator (9.9), in a declarator-id
18551 (9.3.4), or in a type-only context other than a nested-name-specifier
18552 (13.8). */
18554 /* If DECL is a template, then the name was a template-name. */
18555 if (TREE_CODE (decl) == TEMPLATE_DECL)
18557 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
18558 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
18560 tree d = DECL_TEMPLATE_RESULT (decl);
18561 tree attr;
18562 if (TREE_CODE (d) == TYPE_DECL)
18563 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
18564 else
18565 attr = DECL_ATTRIBUTES (d);
18566 if (TREE_UNAVAILABLE (decl))
18568 attr = lookup_attribute ("unavailable", attr);
18569 error_unavailable_use (decl, attr);
18571 else if (TREE_DEPRECATED (decl)
18572 && deprecated_state != DEPRECATED_SUPPRESS)
18574 attr = lookup_attribute ("deprecated", attr);
18575 warn_deprecated_use (decl, attr);
18579 else
18581 /* Look through an overload set for any templates. */
18582 bool found = false;
18584 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
18585 !found && iter; ++iter)
18586 if (TREE_CODE (*iter) == TEMPLATE_DECL)
18587 found = true;
18589 /* "an unqualified name for which name lookup either finds one or more
18590 functions or finds nothing". */
18591 if (!found
18592 && (cxx_dialect > cxx17)
18593 && !scope
18594 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
18595 && tag_type == none_type)
18597 /* The "more functions" case. Just use the OVERLOAD as normally.
18598 We don't use is_overloaded_fn here to avoid considering
18599 BASELINKs. */
18600 if (TREE_CODE (decl) == OVERLOAD
18601 /* Name lookup found one function. */
18602 || TREE_CODE (decl) == FUNCTION_DECL
18603 /* Name lookup found nothing. */
18604 || decl == error_mark_node)
18605 found = true;
18608 /* "in a type-only context" */
18609 if (!found && scope
18610 && tag_type != none_type
18611 && dependentish_scope_p (scope)
18612 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18613 found = true;
18615 if (!found)
18617 /* The name does not name a template. */
18618 cp_parser_error (parser, "expected template-name");
18619 return error_mark_node;
18621 else if (decl == error_mark_node)
18622 /* Repeat the lookup at instantiation time. */
18623 decl = identifier;
18626 return decl;
18629 /* Parse a template-argument-list.
18631 template-argument-list:
18632 template-argument ... [opt]
18633 template-argument-list , template-argument ... [opt]
18635 Returns a TREE_VEC containing the arguments. */
18637 static tree
18638 cp_parser_template_argument_list (cp_parser* parser)
18640 bool saved_in_template_argument_list_p;
18641 bool saved_ice_p;
18642 bool saved_non_ice_p;
18644 /* Don't create location wrapper nodes within a template-argument-list. */
18645 auto_suppress_location_wrappers sentinel;
18647 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
18648 parser->in_template_argument_list_p = true;
18649 /* Even if the template-id appears in an integral
18650 constant-expression, the contents of the argument list do
18651 not. */
18652 saved_ice_p = parser->integral_constant_expression_p;
18653 parser->integral_constant_expression_p = false;
18654 saved_non_ice_p = parser->non_integral_constant_expression_p;
18655 parser->non_integral_constant_expression_p = false;
18657 /* Parse the arguments. */
18658 auto_vec<tree, 10> args;
18661 if (!args.is_empty ())
18662 /* Consume the comma. */
18663 cp_lexer_consume_token (parser->lexer);
18665 /* Parse the template-argument. */
18666 tree argument = cp_parser_template_argument (parser);
18668 /* If the next token is an ellipsis, we're expanding a template
18669 argument pack. */
18670 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18672 if (argument == error_mark_node)
18674 cp_token *token = cp_lexer_peek_token (parser->lexer);
18675 error_at (token->location,
18676 "expected parameter pack before %<...%>");
18678 /* Consume the `...' token. */
18679 cp_lexer_consume_token (parser->lexer);
18681 /* Make the argument into a TYPE_PACK_EXPANSION or
18682 EXPR_PACK_EXPANSION. */
18683 argument = make_pack_expansion (argument);
18686 args.safe_push (argument);
18688 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18690 int n_args = args.length ();
18691 tree vec = make_tree_vec (n_args);
18693 for (int i = 0; i < n_args; i++)
18694 TREE_VEC_ELT (vec, i) = args[i];
18696 parser->non_integral_constant_expression_p = saved_non_ice_p;
18697 parser->integral_constant_expression_p = saved_ice_p;
18698 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
18699 if (CHECKING_P)
18700 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
18701 return vec;
18704 /* Parse a template-argument.
18706 template-argument:
18707 assignment-expression
18708 type-id
18709 id-expression
18711 The representation is that of an assignment-expression, type-id, or
18712 id-expression -- except that the qualified id-expression is
18713 evaluated, so that the value returned is either a DECL or an
18714 OVERLOAD.
18716 Although the standard says "assignment-expression", it forbids
18717 throw-expressions or assignments in the template argument.
18718 Therefore, we use "conditional-expression" instead. */
18720 static tree
18721 cp_parser_template_argument (cp_parser* parser)
18723 tree argument;
18724 bool template_p;
18725 bool address_p;
18726 bool maybe_type_id = false;
18727 cp_token *token = NULL, *argument_start_token = NULL;
18728 location_t loc = 0;
18729 cp_id_kind idk;
18731 /* There's really no way to know what we're looking at, so we just
18732 try each alternative in order.
18734 [temp.arg]
18736 In a template-argument, an ambiguity between a type-id and an
18737 expression is resolved to a type-id, regardless of the form of
18738 the corresponding template-parameter.
18740 Therefore, we try a type-id first. */
18741 cp_parser_parse_tentatively (parser);
18742 argument = cp_parser_template_type_arg (parser);
18743 /* If there was no error parsing the type-id but the next token is a
18744 '>>', our behavior depends on which dialect of C++ we're
18745 parsing. In C++98, we probably found a typo for '> >'. But there
18746 are type-id which are also valid expressions. For instance:
18748 struct X { int operator >> (int); };
18749 template <int V> struct Foo {};
18750 Foo<X () >> 5> r;
18752 Here 'X()' is a valid type-id of a function type, but the user just
18753 wanted to write the expression "X() >> 5". Thus, we remember that we
18754 found a valid type-id, but we still try to parse the argument as an
18755 expression to see what happens.
18757 In C++0x, the '>>' will be considered two separate '>'
18758 tokens. */
18759 if (!cp_parser_error_occurred (parser)
18760 && cxx_dialect == cxx98
18761 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18763 maybe_type_id = true;
18764 cp_parser_abort_tentative_parse (parser);
18766 else
18768 /* If the next token isn't a `,' or a `>', then this argument wasn't
18769 really finished. This means that the argument is not a valid
18770 type-id. */
18771 if (!cp_parser_next_token_ends_template_argument_p (parser))
18772 cp_parser_error (parser, "expected template-argument");
18773 /* If that worked, we're done. */
18774 if (cp_parser_parse_definitely (parser))
18775 return argument;
18777 /* We're still not sure what the argument will be. */
18778 cp_parser_parse_tentatively (parser);
18779 /* Try a template. */
18780 argument_start_token = cp_lexer_peek_token (parser->lexer);
18781 argument = cp_parser_id_expression (parser,
18782 /*template_keyword_p=*/false,
18783 /*check_dependency_p=*/true,
18784 &template_p,
18785 /*declarator_p=*/false,
18786 /*optional_p=*/false);
18787 /* If the next token isn't a `,' or a `>', then this argument wasn't
18788 really finished. */
18789 if (!cp_parser_next_token_ends_template_argument_p (parser))
18790 cp_parser_error (parser, "expected template-argument");
18791 if (!cp_parser_error_occurred (parser))
18793 /* Figure out what is being referred to. If the id-expression
18794 was for a class template specialization, then we will have a
18795 TYPE_DECL at this point. There is no need to do name lookup
18796 at this point in that case. */
18797 if (TREE_CODE (argument) != TYPE_DECL)
18798 argument = cp_parser_lookup_name (parser, argument,
18799 none_type,
18800 /*is_template=*/template_p,
18801 /*is_namespace=*/false,
18802 /*check_dependency=*/true,
18803 /*ambiguous_decls=*/NULL,
18804 argument_start_token->location);
18805 if (TREE_CODE (argument) != TEMPLATE_DECL
18806 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
18807 cp_parser_error (parser, "expected template-name");
18809 if (cp_parser_parse_definitely (parser))
18811 if (TREE_UNAVAILABLE (argument))
18812 error_unavailable_use (argument, NULL_TREE);
18813 else if (TREE_DEPRECATED (argument))
18814 warn_deprecated_use (argument, NULL_TREE);
18815 return argument;
18817 /* It must be a non-type argument. In C++17 any constant-expression is
18818 allowed. */
18819 if (cxx_dialect > cxx14)
18820 goto general_expr;
18822 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
18824 -- an integral constant-expression of integral or enumeration
18825 type; or
18827 -- the name of a non-type template-parameter; or
18829 -- the name of an object or function with external linkage...
18831 -- the address of an object or function with external linkage...
18833 -- a pointer to member... */
18834 /* Look for a non-type template parameter. */
18835 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18837 cp_parser_parse_tentatively (parser);
18838 argument = cp_parser_primary_expression (parser,
18839 /*address_p=*/false,
18840 /*cast_p=*/false,
18841 /*template_arg_p=*/true,
18842 &idk);
18843 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
18844 || !cp_parser_next_token_ends_template_argument_p (parser))
18845 cp_parser_simulate_error (parser);
18846 if (cp_parser_parse_definitely (parser))
18847 return argument;
18850 /* If the next token is "&", the argument must be the address of an
18851 object or function with external linkage. */
18852 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
18853 if (address_p)
18855 loc = cp_lexer_peek_token (parser->lexer)->location;
18856 cp_lexer_consume_token (parser->lexer);
18858 /* See if we might have an id-expression. */
18859 token = cp_lexer_peek_token (parser->lexer);
18860 if (token->type == CPP_NAME
18861 || token->keyword == RID_OPERATOR
18862 || token->type == CPP_SCOPE
18863 || token->type == CPP_TEMPLATE_ID
18864 || token->type == CPP_NESTED_NAME_SPECIFIER)
18866 cp_parser_parse_tentatively (parser);
18867 argument = cp_parser_primary_expression (parser,
18868 address_p,
18869 /*cast_p=*/false,
18870 /*template_arg_p=*/true,
18871 &idk);
18872 if (cp_parser_error_occurred (parser)
18873 || !cp_parser_next_token_ends_template_argument_p (parser))
18874 cp_parser_abort_tentative_parse (parser);
18875 else
18877 tree probe;
18879 if (INDIRECT_REF_P (argument))
18881 /* Strip the dereference temporarily. */
18882 gcc_assert (REFERENCE_REF_P (argument));
18883 argument = TREE_OPERAND (argument, 0);
18886 /* If we're in a template, we represent a qualified-id referring
18887 to a static data member as a SCOPE_REF even if the scope isn't
18888 dependent so that we can check access control later. */
18889 probe = argument;
18890 if (TREE_CODE (probe) == SCOPE_REF)
18891 probe = TREE_OPERAND (probe, 1);
18892 if (VAR_P (probe))
18894 /* A variable without external linkage might still be a
18895 valid constant-expression, so no error is issued here
18896 if the external-linkage check fails. */
18897 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
18898 cp_parser_simulate_error (parser);
18900 else if (is_overloaded_fn (argument))
18901 /* All overloaded functions are allowed; if the external
18902 linkage test does not pass, an error will be issued
18903 later. */
18905 else if (address_p
18906 && (TREE_CODE (argument) == OFFSET_REF
18907 || TREE_CODE (argument) == SCOPE_REF))
18908 /* A pointer-to-member. */
18910 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
18912 else
18913 cp_parser_simulate_error (parser);
18915 if (cp_parser_parse_definitely (parser))
18917 if (address_p)
18918 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
18919 tf_warning_or_error);
18920 else
18921 argument = convert_from_reference (argument);
18922 return argument;
18926 /* If the argument started with "&", there are no other valid
18927 alternatives at this point. */
18928 if (address_p)
18930 cp_parser_error (parser, "invalid non-type template argument");
18931 return error_mark_node;
18934 general_expr:
18935 /* If the argument wasn't successfully parsed as a type-id followed
18936 by '>>', the argument can only be a constant expression now.
18937 Otherwise, we try parsing the constant-expression tentatively,
18938 because the argument could really be a type-id. */
18939 if (maybe_type_id)
18940 cp_parser_parse_tentatively (parser);
18942 if (cxx_dialect <= cxx14)
18943 argument = cp_parser_constant_expression (parser);
18944 else
18946 /* In C++20, we can encounter a braced-init-list. */
18947 if (cxx_dialect >= cxx20
18948 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18950 bool expr_non_constant_p;
18951 return cp_parser_braced_list (parser, &expr_non_constant_p);
18954 /* With C++17 generalized non-type template arguments we need to handle
18955 lvalue constant expressions, too. */
18956 argument = cp_parser_assignment_expression (parser);
18957 require_potential_constant_expression (argument);
18960 if (!maybe_type_id)
18961 return argument;
18962 if (!cp_parser_next_token_ends_template_argument_p (parser))
18963 cp_parser_error (parser, "expected template-argument");
18964 if (cp_parser_parse_definitely (parser))
18965 return argument;
18966 /* We did our best to parse the argument as a non type-id, but that
18967 was the only alternative that matched (albeit with a '>' after
18968 it). We can assume it's just a typo from the user, and a
18969 diagnostic will then be issued. */
18970 return cp_parser_template_type_arg (parser);
18973 /* Parse an explicit-instantiation.
18975 explicit-instantiation:
18976 template declaration
18978 Although the standard says `declaration', what it really means is:
18980 explicit-instantiation:
18981 template decl-specifier-seq [opt] declarator [opt] ;
18983 Things like `template int S<int>::i = 5, int S<double>::j;' are not
18984 supposed to be allowed. A defect report has been filed about this
18985 issue.
18987 GNU Extension:
18989 explicit-instantiation:
18990 storage-class-specifier template
18991 decl-specifier-seq [opt] declarator [opt] ;
18992 function-specifier template
18993 decl-specifier-seq [opt] declarator [opt] ; */
18995 static void
18996 cp_parser_explicit_instantiation (cp_parser* parser)
18998 int declares_class_or_enum;
18999 cp_decl_specifier_seq decl_specifiers;
19000 tree extension_specifier = NULL_TREE;
19002 timevar_push (TV_TEMPLATE_INST);
19004 /* Look for an (optional) storage-class-specifier or
19005 function-specifier. */
19006 if (cp_parser_allow_gnu_extensions_p (parser))
19008 extension_specifier
19009 = cp_parser_storage_class_specifier_opt (parser);
19010 if (!extension_specifier)
19011 extension_specifier
19012 = cp_parser_function_specifier_opt (parser,
19013 /*decl_specs=*/NULL);
19016 /* Look for the `template' keyword. */
19017 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19018 /* Let the front end know that we are processing an explicit
19019 instantiation. */
19020 begin_explicit_instantiation ();
19021 /* [temp.explicit] says that we are supposed to ignore access
19022 control while processing explicit instantiation directives. */
19023 push_deferring_access_checks (dk_no_check);
19024 /* Parse a decl-specifier-seq. */
19025 cp_parser_decl_specifier_seq (parser,
19026 CP_PARSER_FLAGS_OPTIONAL,
19027 &decl_specifiers,
19028 &declares_class_or_enum);
19030 cp_omp_declare_simd_data odsd;
19031 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19032 cp_parser_handle_directive_omp_attributes (parser,
19033 &decl_specifiers.attributes,
19034 &odsd, true);
19036 /* If there was exactly one decl-specifier, and it declared a class,
19037 and there's no declarator, then we have an explicit type
19038 instantiation. */
19039 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19041 tree type = check_tag_decl (&decl_specifiers,
19042 /*explicit_type_instantiation_p=*/true);
19043 /* Turn access control back on for names used during
19044 template instantiation. */
19045 pop_deferring_access_checks ();
19046 if (type)
19047 do_type_instantiation (type, extension_specifier,
19048 /*complain=*/tf_error);
19050 else
19052 cp_declarator *declarator;
19053 tree decl;
19055 /* Parse the declarator. */
19056 declarator
19057 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19058 CP_PARSER_FLAGS_NONE,
19059 /*ctor_dtor_or_conv_p=*/NULL,
19060 /*parenthesized_p=*/NULL,
19061 /*member_p=*/false,
19062 /*friend_p=*/false,
19063 /*static_p=*/false);
19064 if (declares_class_or_enum & 2)
19065 cp_parser_check_for_definition_in_return_type (declarator,
19066 decl_specifiers.type,
19067 decl_specifiers.locations[ds_type_spec]);
19068 if (declarator != cp_error_declarator)
19070 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19071 permerror (decl_specifiers.locations[ds_inline],
19072 "explicit instantiation shall not use"
19073 " %<inline%> specifier");
19074 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19075 permerror (decl_specifiers.locations[ds_constexpr],
19076 "explicit instantiation shall not use"
19077 " %<constexpr%> specifier");
19078 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19079 permerror (decl_specifiers.locations[ds_consteval],
19080 "explicit instantiation shall not use"
19081 " %<consteval%> specifier");
19083 decl = grokdeclarator (declarator, &decl_specifiers,
19084 NORMAL, 0, &decl_specifiers.attributes);
19085 /* Turn access control back on for names used during
19086 template instantiation. */
19087 pop_deferring_access_checks ();
19088 /* Do the explicit instantiation. */
19089 do_decl_instantiation (decl, extension_specifier);
19091 else
19093 pop_deferring_access_checks ();
19094 /* Skip the body of the explicit instantiation. */
19095 cp_parser_skip_to_end_of_statement (parser);
19098 /* We're done with the instantiation. */
19099 end_explicit_instantiation ();
19101 cp_parser_consume_semicolon_at_end_of_statement (parser);
19103 timevar_pop (TV_TEMPLATE_INST);
19105 cp_finalize_omp_declare_simd (parser, &odsd);
19108 /* Parse an explicit-specialization.
19110 explicit-specialization:
19111 template < > declaration
19113 Although the standard says `declaration', what it really means is:
19115 explicit-specialization:
19116 template <> decl-specifier [opt] init-declarator [opt] ;
19117 template <> function-definition
19118 template <> explicit-specialization
19119 template <> template-declaration */
19121 static void
19122 cp_parser_explicit_specialization (cp_parser* parser)
19124 cp_token *token = cp_lexer_peek_token (parser->lexer);
19126 /* Look for the `template' keyword. */
19127 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19128 /* Look for the `<'. */
19129 cp_parser_require (parser, CPP_LESS, RT_LESS);
19130 /* Look for the `>'. */
19131 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19132 /* We have processed another parameter list. */
19133 ++parser->num_template_parameter_lists;
19135 /* [temp]
19137 A template ... explicit specialization ... shall not have C
19138 linkage. */
19139 bool need_lang_pop = current_lang_name == lang_name_c;
19140 if (need_lang_pop)
19142 error_at (token->location, "template specialization with C linkage");
19143 maybe_show_extern_c_location ();
19145 /* Give it C++ linkage to avoid confusing other parts of the
19146 front end. */
19147 push_lang_context (lang_name_cplusplus);
19150 /* Let the front end know that we are beginning a specialization. */
19151 if (begin_specialization ())
19153 /* If the next keyword is `template', we need to figure out
19154 whether or not we're looking a template-declaration. */
19155 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19157 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19158 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19159 cp_parser_template_declaration_after_export (parser,
19160 /*member_p=*/false);
19161 else
19162 cp_parser_explicit_specialization (parser);
19164 else
19165 /* Parse the dependent declaration. */
19166 cp_parser_single_declaration (parser,
19167 /*checks=*/NULL,
19168 /*member_p=*/false,
19169 /*explicit_specialization_p=*/true,
19170 /*friend_p=*/NULL);
19173 /* We're done with the specialization. */
19174 end_specialization ();
19176 /* For the erroneous case of a template with C linkage, we pushed an
19177 implicit C++ linkage scope; exit that scope now. */
19178 if (need_lang_pop)
19179 pop_lang_context ();
19181 /* We're done with this parameter list. */
19182 --parser->num_template_parameter_lists;
19185 /* Preserve the attributes across a garbage collect (by making it a GC
19186 root), which can occur when parsing a member function. */
19188 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19190 /* Parse a type-specifier.
19192 type-specifier:
19193 simple-type-specifier
19194 class-specifier
19195 enum-specifier
19196 elaborated-type-specifier
19197 cv-qualifier
19199 GNU Extension:
19201 type-specifier:
19202 __complex__
19204 Returns a representation of the type-specifier. For a
19205 class-specifier, enum-specifier, or elaborated-type-specifier, a
19206 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19208 The parser flags FLAGS is used to control type-specifier parsing.
19210 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19211 in a decl-specifier-seq.
19213 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19214 class-specifier, enum-specifier, or elaborated-type-specifier, then
19215 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19216 if a type is declared; 2 if it is defined. Otherwise, it is set to
19217 zero.
19219 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19220 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19221 is set to FALSE. */
19223 static tree
19224 cp_parser_type_specifier (cp_parser* parser,
19225 cp_parser_flags flags,
19226 cp_decl_specifier_seq *decl_specs,
19227 bool is_declaration,
19228 int* declares_class_or_enum,
19229 bool* is_cv_qualifier)
19231 tree type_spec = NULL_TREE;
19232 cp_token *token;
19233 enum rid keyword;
19234 cp_decl_spec ds = ds_last;
19236 /* Assume this type-specifier does not declare a new type. */
19237 if (declares_class_or_enum)
19238 *declares_class_or_enum = 0;
19239 /* And that it does not specify a cv-qualifier. */
19240 if (is_cv_qualifier)
19241 *is_cv_qualifier = false;
19242 /* Peek at the next token. */
19243 token = cp_lexer_peek_token (parser->lexer);
19245 /* If we're looking at a keyword, we can use that to guide the
19246 production we choose. */
19247 keyword = token->keyword;
19248 switch (keyword)
19250 case RID_ENUM:
19251 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19252 goto elaborated_type_specifier;
19254 /* Look for the enum-specifier. */
19255 type_spec = cp_parser_enum_specifier (parser);
19256 /* If that worked, we're done. */
19257 if (type_spec)
19259 if (declares_class_or_enum)
19260 *declares_class_or_enum = 2;
19261 if (decl_specs)
19262 cp_parser_set_decl_spec_type (decl_specs,
19263 type_spec,
19264 token,
19265 /*type_definition_p=*/true);
19266 return type_spec;
19268 else
19269 goto elaborated_type_specifier;
19271 /* Any of these indicate either a class-specifier, or an
19272 elaborated-type-specifier. */
19273 case RID_CLASS:
19274 case RID_STRUCT:
19275 case RID_UNION:
19276 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19277 goto elaborated_type_specifier;
19279 /* Parse tentatively so that we can back up if we don't find a
19280 class-specifier. */
19281 cp_parser_parse_tentatively (parser);
19282 if (decl_specs->attributes)
19283 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19284 /* Look for the class-specifier. */
19285 type_spec = cp_parser_class_specifier (parser);
19286 if (decl_specs->attributes)
19287 cp_parser_decl_specs_attrs->pop ();
19288 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19289 /* If that worked, we're done. */
19290 if (cp_parser_parse_definitely (parser))
19292 if (declares_class_or_enum)
19293 *declares_class_or_enum = 2;
19294 if (decl_specs)
19295 cp_parser_set_decl_spec_type (decl_specs,
19296 type_spec,
19297 token,
19298 /*type_definition_p=*/true);
19299 return type_spec;
19302 /* Fall through. */
19303 elaborated_type_specifier:
19304 /* We're declaring (not defining) a class or enum. */
19305 if (declares_class_or_enum)
19306 *declares_class_or_enum = 1;
19308 /* Fall through. */
19309 case RID_TYPENAME:
19310 /* Look for an elaborated-type-specifier. */
19311 type_spec
19312 = (cp_parser_elaborated_type_specifier
19313 (parser,
19314 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19315 is_declaration));
19316 if (decl_specs)
19317 cp_parser_set_decl_spec_type (decl_specs,
19318 type_spec,
19319 token,
19320 /*type_definition_p=*/false);
19321 return type_spec;
19323 case RID_CONST:
19324 ds = ds_const;
19325 if (is_cv_qualifier)
19326 *is_cv_qualifier = true;
19327 break;
19329 case RID_VOLATILE:
19330 ds = ds_volatile;
19331 if (is_cv_qualifier)
19332 *is_cv_qualifier = true;
19333 break;
19335 case RID_RESTRICT:
19336 ds = ds_restrict;
19337 if (is_cv_qualifier)
19338 *is_cv_qualifier = true;
19339 break;
19341 case RID_COMPLEX:
19342 /* The `__complex__' keyword is a GNU extension. */
19343 ds = ds_complex;
19344 break;
19346 default:
19347 break;
19350 /* Handle simple keywords. */
19351 if (ds != ds_last)
19353 if (decl_specs)
19355 set_and_check_decl_spec_loc (decl_specs, ds, token);
19356 decl_specs->any_specifiers_p = true;
19358 return cp_lexer_consume_token (parser->lexer)->u.value;
19361 /* If we do not already have a type-specifier, assume we are looking
19362 at a simple-type-specifier. */
19363 type_spec = cp_parser_simple_type_specifier (parser,
19364 decl_specs,
19365 flags);
19367 /* If we didn't find a type-specifier, and a type-specifier was not
19368 optional in this context, issue an error message. */
19369 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19371 cp_parser_error (parser, "expected type specifier");
19372 return error_mark_node;
19375 return type_spec;
19378 /* Parse a simple-type-specifier.
19380 simple-type-specifier:
19381 :: [opt] nested-name-specifier [opt] type-name
19382 :: [opt] nested-name-specifier template template-id
19383 char
19384 wchar_t
19385 bool
19386 short
19388 long
19389 signed
19390 unsigned
19391 float
19392 double
19393 void
19395 C++11 Extension:
19397 simple-type-specifier:
19398 auto
19399 decltype ( expression )
19400 char16_t
19401 char32_t
19402 __underlying_type ( type-id )
19404 C++17 extension:
19406 nested-name-specifier(opt) template-name
19408 GNU Extension:
19410 simple-type-specifier:
19411 __int128
19412 __typeof__ unary-expression
19413 __typeof__ ( type-id )
19414 __typeof__ ( type-id ) { initializer-list , [opt] }
19416 Concepts Extension:
19418 simple-type-specifier:
19419 constrained-type-specifier
19421 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
19422 appropriately updated. */
19424 static tree
19425 cp_parser_simple_type_specifier (cp_parser* parser,
19426 cp_decl_specifier_seq *decl_specs,
19427 cp_parser_flags flags)
19429 tree type = NULL_TREE;
19430 cp_token *token;
19431 int idx;
19433 /* Peek at the next token. */
19434 token = cp_lexer_peek_token (parser->lexer);
19436 /* If we're looking at a keyword, things are easy. */
19437 switch (token->keyword)
19439 case RID_CHAR:
19440 if (decl_specs)
19441 decl_specs->explicit_char_p = true;
19442 type = char_type_node;
19443 break;
19444 case RID_CHAR8:
19445 type = char8_type_node;
19446 break;
19447 case RID_CHAR16:
19448 type = char16_type_node;
19449 break;
19450 case RID_CHAR32:
19451 type = char32_type_node;
19452 break;
19453 case RID_WCHAR:
19454 type = wchar_type_node;
19455 break;
19456 case RID_BOOL:
19457 type = boolean_type_node;
19458 break;
19459 case RID_SHORT:
19460 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
19461 type = short_integer_type_node;
19462 break;
19463 case RID_INT:
19464 if (decl_specs)
19465 decl_specs->explicit_int_p = true;
19466 type = integer_type_node;
19467 break;
19468 case RID_INT_N_0:
19469 case RID_INT_N_1:
19470 case RID_INT_N_2:
19471 case RID_INT_N_3:
19472 idx = token->keyword - RID_INT_N_0;
19473 if (! int_n_enabled_p [idx])
19474 break;
19475 if (decl_specs)
19477 decl_specs->explicit_intN_p = true;
19478 decl_specs->int_n_idx = idx;
19479 /* Check if the alternate "__intN__" form has been used instead of
19480 "__intN". */
19481 if (startswith (IDENTIFIER_POINTER (token->u.value)
19482 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
19483 decl_specs->int_n_alt = true;
19485 type = int_n_trees [idx].signed_type;
19486 break;
19487 case RID_LONG:
19488 if (decl_specs)
19489 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
19490 type = long_integer_type_node;
19491 break;
19492 case RID_SIGNED:
19493 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
19494 type = integer_type_node;
19495 break;
19496 case RID_UNSIGNED:
19497 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
19498 type = unsigned_type_node;
19499 break;
19500 case RID_FLOAT:
19501 type = float_type_node;
19502 break;
19503 case RID_DOUBLE:
19504 type = double_type_node;
19505 break;
19506 case RID_VOID:
19507 type = void_type_node;
19508 break;
19510 case RID_AUTO:
19511 maybe_warn_cpp0x (CPP0X_AUTO);
19512 if (parser->auto_is_implicit_function_template_parm_p)
19514 /* The 'auto' might be the placeholder return type for a function decl
19515 with trailing return type. */
19516 bool have_trailing_return_fn_decl = false;
19518 cp_parser_parse_tentatively (parser);
19519 cp_lexer_consume_token (parser->lexer);
19520 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
19521 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
19522 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19523 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
19525 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19527 cp_lexer_consume_token (parser->lexer);
19528 cp_parser_skip_to_closing_parenthesis (parser,
19529 /*recovering*/false,
19530 /*or_comma*/false,
19531 /*consume_paren*/true);
19532 continue;
19535 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
19537 have_trailing_return_fn_decl = true;
19538 break;
19541 cp_lexer_consume_token (parser->lexer);
19543 cp_parser_abort_tentative_parse (parser);
19545 if (have_trailing_return_fn_decl)
19547 type = make_auto ();
19548 break;
19551 if (cxx_dialect >= cxx14)
19553 type = synthesize_implicit_template_parm (parser, NULL_TREE);
19554 type = TREE_TYPE (type);
19556 else
19557 type = error_mark_node;
19559 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
19561 if (cxx_dialect < cxx14)
19562 error_at (token->location,
19563 "use of %<auto%> in lambda parameter declaration "
19564 "only available with "
19565 "%<-std=c++14%> or %<-std=gnu++14%>");
19567 else if (cxx_dialect < cxx14)
19568 error_at (token->location,
19569 "use of %<auto%> in parameter declaration "
19570 "only available with "
19571 "%<-std=c++14%> or %<-std=gnu++14%>");
19572 else if (!flag_concepts)
19573 pedwarn (token->location, 0,
19574 "use of %<auto%> in parameter declaration "
19575 "only available with %<-std=c++20%> or %<-fconcepts%>");
19577 else
19578 type = make_auto ();
19579 break;
19581 case RID_DECLTYPE:
19582 /* Since DR 743, decltype can either be a simple-type-specifier by
19583 itself or begin a nested-name-specifier. Parsing it will replace
19584 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19585 handling below decide what to do. */
19586 cp_parser_decltype (parser);
19587 cp_lexer_set_token_position (parser->lexer, token);
19588 break;
19590 case RID_TYPEOF:
19591 /* Consume the `typeof' token. */
19592 cp_lexer_consume_token (parser->lexer);
19593 /* Parse the operand to `typeof'. */
19594 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
19595 /* If it is not already a TYPE, take its type. */
19596 if (!TYPE_P (type))
19597 type = finish_typeof (type);
19599 if (decl_specs)
19600 cp_parser_set_decl_spec_type (decl_specs, type,
19601 token,
19602 /*type_definition_p=*/false);
19604 return type;
19606 case RID_UNDERLYING_TYPE:
19607 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
19608 if (decl_specs)
19609 cp_parser_set_decl_spec_type (decl_specs, type,
19610 token,
19611 /*type_definition_p=*/false);
19613 return type;
19615 case RID_BASES:
19616 case RID_DIRECT_BASES:
19617 type = cp_parser_trait_expr (parser, token->keyword);
19618 if (decl_specs)
19619 cp_parser_set_decl_spec_type (decl_specs, type,
19620 token,
19621 /*type_definition_p=*/false);
19622 return type;
19623 default:
19624 break;
19627 /* If token is an already-parsed decltype not followed by ::,
19628 it's a simple-type-specifier. */
19629 if (token->type == CPP_DECLTYPE
19630 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
19632 type = saved_checks_value (token->u.tree_check_value);
19633 if (decl_specs)
19635 cp_parser_set_decl_spec_type (decl_specs, type,
19636 token,
19637 /*type_definition_p=*/false);
19638 /* Remember that we are handling a decltype in order to
19639 implement the resolution of DR 1510 when the argument
19640 isn't instantiation dependent. */
19641 decl_specs->decltype_p = true;
19643 cp_lexer_consume_token (parser->lexer);
19644 return type;
19647 /* If the type-specifier was for a built-in type, we're done. */
19648 if (type)
19650 /* Record the type. */
19651 if (decl_specs
19652 && (token->keyword != RID_SIGNED
19653 && token->keyword != RID_UNSIGNED
19654 && token->keyword != RID_SHORT
19655 && token->keyword != RID_LONG))
19656 cp_parser_set_decl_spec_type (decl_specs,
19657 type,
19658 token,
19659 /*type_definition_p=*/false);
19660 if (decl_specs)
19661 decl_specs->any_specifiers_p = true;
19663 /* Consume the token. */
19664 cp_lexer_consume_token (parser->lexer);
19666 if (type == error_mark_node)
19667 return error_mark_node;
19669 /* There is no valid C++ program where a non-template type is
19670 followed by a "<". That usually indicates that the user thought
19671 that the type was a template. */
19672 cp_parser_check_for_invalid_template_id (parser, type, none_type,
19673 token->location);
19675 return TYPE_NAME (type);
19678 /* The type-specifier must be a user-defined type. */
19679 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
19681 bool qualified_p;
19682 bool global_p;
19683 const bool typename_p = (cxx_dialect >= cxx20
19684 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
19686 /* Don't gobble tokens or issue error messages if this is an
19687 optional type-specifier. */
19688 if (flags & CP_PARSER_FLAGS_OPTIONAL)
19689 cp_parser_parse_tentatively (parser);
19691 /* Remember current tentative parsing state -- if we know we need
19692 a type, we can give better diagnostics here. */
19693 bool tent = cp_parser_parsing_tentatively (parser);
19695 token = cp_lexer_peek_token (parser->lexer);
19697 /* Look for the optional `::' operator. */
19698 global_p
19699 = (cp_parser_global_scope_opt (parser,
19700 /*current_scope_valid_p=*/false)
19701 != NULL_TREE);
19702 /* Look for the nested-name specifier. */
19703 qualified_p
19704 = (cp_parser_nested_name_specifier_opt (parser,
19705 /*typename_keyword_p=*/false,
19706 /*check_dependency_p=*/true,
19707 /*type_p=*/false,
19708 /*is_declaration=*/false)
19709 != NULL_TREE);
19710 /* If we have seen a nested-name-specifier, and the next token
19711 is `template', then we are using the template-id production. */
19712 if (parser->scope
19713 && cp_parser_optional_template_keyword (parser))
19715 /* Look for the template-id. */
19716 type = cp_parser_template_id (parser,
19717 /*template_keyword_p=*/true,
19718 /*check_dependency_p=*/true,
19719 none_type,
19720 /*is_declaration=*/false);
19721 /* If the template-id did not name a type, we are out of
19722 luck. */
19723 if (TREE_CODE (type) != TYPE_DECL)
19725 /* ...unless we pretend we have seen 'typename'. */
19726 if (typename_p)
19727 type = cp_parser_make_typename_type (parser, type,
19728 token->location);
19729 else
19731 cp_parser_error (parser, "expected template-id for type");
19732 type = error_mark_node;
19736 /* DR 1812: A < following a qualified-id in a typename-specifier
19737 could safely be assumed to begin a template argument list, so
19738 the template keyword should be optional. */
19739 else if (parser->scope
19740 && qualified_p
19741 && typename_p
19742 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
19744 cp_parser_parse_tentatively (parser);
19746 type = cp_parser_template_id (parser,
19747 /*template_keyword_p=*/true,
19748 /*check_dependency_p=*/true,
19749 none_type,
19750 /*is_declaration=*/false);
19751 /* This is handled below, so back off. */
19752 if (type && concept_check_p (type))
19753 cp_parser_simulate_error (parser);
19755 if (!cp_parser_parse_definitely (parser))
19756 type = NULL_TREE;
19757 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
19758 type = make_typename_type (parser->scope, type, typename_type,
19759 /*complain=*/tf_error);
19760 else if (TREE_CODE (type) != TYPE_DECL)
19761 type = NULL_TREE;
19764 /* Otherwise, look for a type-name. */
19765 if (!type)
19767 if (cxx_dialect >= cxx17)
19768 cp_parser_parse_tentatively (parser);
19770 type = cp_parser_type_name (parser, (qualified_p && typename_p));
19772 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
19773 type = NULL_TREE;
19776 if (!type && flag_concepts && decl_specs)
19778 /* Try for a type-constraint with template arguments. We check
19779 decl_specs here to avoid trying this for a functional cast. */
19781 cp_parser_parse_tentatively (parser);
19783 type = cp_parser_template_id (parser,
19784 /*template_keyword_p=*/false,
19785 /*check_dependency_p=*/true,
19786 none_type,
19787 /*is_declaration=*/false);
19788 if (type && concept_check_p (type))
19790 location_t loc = EXPR_LOCATION (type);
19791 type = cp_parser_placeholder_type_specifier (parser, loc,
19792 type, tent);
19793 if (tent && type == error_mark_node)
19794 /* Perhaps it's a concept-check expression. */
19795 cp_parser_simulate_error (parser);
19797 else
19798 cp_parser_simulate_error (parser);
19800 if (!cp_parser_parse_definitely (parser))
19801 type = NULL_TREE;
19804 if (!type && cxx_dialect >= cxx17)
19806 /* Try class template argument deduction or type-constraint without
19807 template arguments. */
19808 tree name = cp_parser_identifier (parser);
19809 if (name && TREE_CODE (name) == IDENTIFIER_NODE
19810 && parser->scope != error_mark_node)
19812 location_t loc
19813 = cp_lexer_previous_token (parser->lexer)->location;
19814 tree tmpl = cp_parser_lookup_name (parser, name,
19815 none_type,
19816 /*is_template=*/false,
19817 /*is_namespace=*/false,
19818 /*check_dependency=*/true,
19819 /*ambiguous_decls=*/NULL,
19820 token->location);
19821 if (tmpl && tmpl != error_mark_node
19822 && ctad_template_p (tmpl))
19823 type = make_template_placeholder (tmpl);
19824 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
19825 type = cp_parser_placeholder_type_specifier (parser, loc,
19826 tmpl, tent);
19827 else
19829 type = error_mark_node;
19830 if (!cp_parser_simulate_error (parser))
19831 cp_parser_name_lookup_error (parser, name, tmpl,
19832 NLE_TYPE, token->location);
19835 else
19836 type = error_mark_node;
19839 /* If it didn't work out, we don't have a TYPE. */
19840 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
19841 && !cp_parser_parse_definitely (parser))
19842 type = NULL_TREE;
19844 /* Keep track of all name-lookups performed in class scopes. */
19845 if (type
19846 && !global_p
19847 && !qualified_p
19848 && TREE_CODE (type) == TYPE_DECL
19849 && identifier_p (DECL_NAME (type)))
19850 maybe_note_name_used_in_class (DECL_NAME (type), type);
19852 if (type && decl_specs)
19853 cp_parser_set_decl_spec_type (decl_specs, type,
19854 token,
19855 /*type_definition_p=*/false);
19858 /* If we didn't get a type-name, issue an error message. */
19859 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19861 cp_parser_error (parser, "expected type-name");
19862 return error_mark_node;
19865 if (type && type != error_mark_node)
19867 /* See if TYPE is an Objective-C type, and if so, parse and
19868 accept any protocol references following it. Do this before
19869 the cp_parser_check_for_invalid_template_id() call, because
19870 Objective-C types can be followed by '<...>' which would
19871 enclose protocol names rather than template arguments, and so
19872 everything is fine. */
19873 if (c_dialect_objc () && !parser->scope
19874 && (objc_is_id (type) || objc_is_class_name (type)))
19876 tree protos = cp_parser_objc_protocol_refs_opt (parser);
19877 tree qual_type = objc_get_protocol_qualified_type (type, protos);
19879 /* Clobber the "unqualified" type previously entered into
19880 DECL_SPECS with the new, improved protocol-qualified version. */
19881 if (decl_specs)
19882 decl_specs->type = qual_type;
19884 return qual_type;
19887 /* There is no valid C++ program where a non-template type is
19888 followed by a "<". That usually indicates that the user
19889 thought that the type was a template. */
19890 cp_parser_check_for_invalid_template_id (parser, type,
19891 none_type,
19892 token->location);
19895 return type;
19898 /* Parse the remainder of a placholder-type-specifier.
19900 placeholder-type-specifier:
19901 type-constraint_opt auto
19902 type-constraint_opt decltype(auto)
19904 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
19905 and passed as TMPL. This function converts TMPL to an actual type-constraint,
19906 parses the placeholder type, and performs some contextual syntactic analysis.
19908 LOC provides the location of the template name.
19910 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
19911 don't give an error if TMPL isn't a valid type-constraint, as the template-id
19912 might actually be a concept-check,
19914 Note that the Concepts TS allows the auto or decltype(auto) to be
19915 omitted in a constrained-type-specifier. */
19917 static tree
19918 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
19919 tree tmpl, bool tentative)
19921 if (tmpl == error_mark_node)
19922 return error_mark_node;
19924 tree orig_tmpl = tmpl;
19926 /* Get the arguments as written for subsequent analysis. */
19927 tree args = NULL_TREE;
19928 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
19930 args = TREE_OPERAND (tmpl, 1);
19931 tmpl = TREE_OPERAND (tmpl, 0);
19933 else
19934 /* A concept-name with no arguments can't be an expression. */
19935 tentative = false;
19937 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
19939 /* Get the concept and prototype parameter for the constraint. */
19940 tree_pair info = finish_type_constraints (tmpl, args, complain);
19941 tree con = info.first;
19942 tree proto = info.second;
19943 if (con == error_mark_node)
19944 return error_mark_node;
19946 /* As per the standard, require auto or decltype(auto), except in some
19947 cases (template parameter lists, -fconcepts-ts enabled). */
19948 cp_token *placeholder = NULL, *close_paren = NULL;
19949 if (cxx_dialect >= cxx20)
19951 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
19952 placeholder = cp_lexer_consume_token (parser->lexer);
19953 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
19955 placeholder = cp_lexer_consume_token (parser->lexer);
19956 matching_parens parens;
19957 parens.require_open (parser);
19958 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
19959 close_paren = parens.require_close (parser);
19963 /* A type constraint constrains a contextually determined type or type
19964 parameter pack. However, the Concepts TS does allow concepts
19965 to introduce non-type and template template parameters. */
19966 if (TREE_CODE (proto) != TYPE_DECL)
19968 if (!flag_concepts_ts
19969 || !processing_template_parmlist)
19971 if (!tentative)
19973 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
19974 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
19976 return error_mark_node;
19980 /* In a template parameter list, a type-parameter can be introduced
19981 by type-constraints alone. */
19982 if (processing_template_parmlist && !placeholder)
19983 return build_constrained_parameter (con, proto, args);
19985 /* Diagnose issues placeholder issues. */
19986 if (!flag_concepts_ts
19987 && !parser->in_result_type_constraint_p
19988 && !placeholder)
19990 if (tentative)
19991 /* Perhaps it's a concept-check expression (c++/91073). */
19992 return error_mark_node;
19994 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
19995 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
19996 error_at (input_location,
19997 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
19998 /* Fall through. This is an error of omission. */
20000 else if (parser->in_result_type_constraint_p && placeholder)
20002 /* A trailing return type only allows type-constraints. */
20003 error_at (input_location,
20004 "unexpected placeholder in constrained result type");
20007 /* In a parameter-declaration-clause, a placeholder-type-specifier
20008 results in an invented template parameter. */
20009 if (parser->auto_is_implicit_function_template_parm_p)
20011 if (close_paren)
20013 location_t loc = make_location (placeholder->location,
20014 placeholder->location,
20015 close_paren->location);
20016 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20017 return error_mark_node;
20019 tree parm = build_constrained_parameter (con, proto, args);
20020 return synthesize_implicit_template_parm (parser, parm);
20023 /* Determine if the type should be deduced using template argument
20024 deduction or decltype deduction. Note that the latter is always
20025 used for type-constraints in trailing return types. */
20026 bool decltype_p = placeholder
20027 ? placeholder->keyword == RID_DECLTYPE
20028 : parser->in_result_type_constraint_p;
20030 /* Otherwise, this is the type of a variable or return type. */
20031 if (decltype_p)
20032 return make_constrained_decltype_auto (con, args);
20033 else
20034 return make_constrained_auto (con, args);
20037 /* Parse a type-name.
20039 type-name:
20040 class-name
20041 enum-name
20042 typedef-name
20043 simple-template-id [in c++0x]
20045 enum-name:
20046 identifier
20048 typedef-name:
20049 identifier
20051 Concepts:
20053 type-name:
20054 concept-name
20055 partial-concept-id
20057 concept-name:
20058 identifier
20060 Returns a TYPE_DECL for the type. */
20062 static tree
20063 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20065 tree type_decl;
20067 /* We can't know yet whether it is a class-name or not. */
20068 cp_parser_parse_tentatively (parser);
20069 /* Try a class-name. */
20070 type_decl = cp_parser_class_name (parser,
20071 typename_keyword_p,
20072 /*template_keyword_p=*/false,
20073 none_type,
20074 /*check_dependency_p=*/true,
20075 /*class_head_p=*/false,
20076 /*is_declaration=*/false);
20077 /* If it's not a class-name, keep looking. */
20078 if (!cp_parser_parse_definitely (parser))
20080 if (cxx_dialect < cxx11)
20081 /* It must be a typedef-name or an enum-name. */
20082 return cp_parser_nonclass_name (parser);
20084 cp_parser_parse_tentatively (parser);
20085 /* It is either a simple-template-id representing an
20086 instantiation of an alias template... */
20087 type_decl = cp_parser_template_id (parser,
20088 /*template_keyword_p=*/false,
20089 /*check_dependency_p=*/true,
20090 none_type,
20091 /*is_declaration=*/false);
20092 /* Note that this must be an instantiation of an alias template
20093 because [temp.names]/6 says:
20095 A template-id that names an alias template specialization
20096 is a type-name.
20098 Whereas [temp.names]/7 says:
20100 A simple-template-id that names a class template
20101 specialization is a class-name.
20103 With concepts, this could also be a partial-concept-id that
20104 declares a non-type template parameter. */
20105 if (type_decl != NULL_TREE
20106 && TREE_CODE (type_decl) == TYPE_DECL
20107 && TYPE_DECL_ALIAS_P (type_decl))
20108 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20109 else
20110 cp_parser_simulate_error (parser);
20112 if (!cp_parser_parse_definitely (parser))
20113 /* ... Or a typedef-name or an enum-name. */
20114 return cp_parser_nonclass_name (parser);
20117 return type_decl;
20120 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20121 or a concept-name.
20123 enum-name:
20124 identifier
20126 typedef-name:
20127 identifier
20129 concept-name:
20130 identifier
20132 Returns a TYPE_DECL for the type. */
20134 static tree
20135 cp_parser_nonclass_name (cp_parser* parser)
20137 tree type_decl;
20138 tree identifier;
20140 cp_token *token = cp_lexer_peek_token (parser->lexer);
20141 identifier = cp_parser_identifier (parser);
20142 if (identifier == error_mark_node)
20143 return error_mark_node;
20145 /* Look up the type-name. */
20146 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20148 type_decl = strip_using_decl (type_decl);
20150 if (TREE_CODE (type_decl) != TYPE_DECL
20151 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20153 /* See if this is an Objective-C type. */
20154 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20155 tree type = objc_get_protocol_qualified_type (identifier, protos);
20156 if (type)
20157 type_decl = TYPE_NAME (type);
20160 /* Issue an error if we did not find a type-name. */
20161 if (TREE_CODE (type_decl) != TYPE_DECL
20162 /* In Objective-C, we have the complication that class names are
20163 normally type names and start declarations (eg, the
20164 "NSObject" in "NSObject *object;"), but can be used in an
20165 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20166 is an expression. So, a classname followed by a dot is not a
20167 valid type-name. */
20168 || (objc_is_class_name (TREE_TYPE (type_decl))
20169 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20171 if (!cp_parser_simulate_error (parser))
20172 cp_parser_name_lookup_error (parser, identifier, type_decl,
20173 NLE_TYPE, token->location);
20174 return error_mark_node;
20176 /* Remember that the name was used in the definition of the
20177 current class so that we can check later to see if the
20178 meaning would have been different after the class was
20179 entirely defined. */
20180 else if (type_decl != error_mark_node
20181 && !parser->scope)
20182 maybe_note_name_used_in_class (identifier, type_decl);
20184 return type_decl;
20187 /* Parse an elaborated-type-specifier. Note that the grammar given
20188 here incorporates the resolution to DR68.
20190 elaborated-type-specifier:
20191 class-key :: [opt] nested-name-specifier [opt] identifier
20192 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20193 enum-key :: [opt] nested-name-specifier [opt] identifier
20194 typename :: [opt] nested-name-specifier identifier
20195 typename :: [opt] nested-name-specifier template [opt]
20196 template-id
20198 GNU extension:
20200 elaborated-type-specifier:
20201 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20202 class-key attributes :: [opt] nested-name-specifier [opt]
20203 template [opt] template-id
20204 enum attributes :: [opt] nested-name-specifier [opt] identifier
20206 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20207 declared `friend'. If IS_DECLARATION is TRUE, then this
20208 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20209 something is being declared.
20211 Returns the TYPE specified. */
20213 static tree
20214 cp_parser_elaborated_type_specifier (cp_parser* parser,
20215 bool is_friend,
20216 bool is_declaration)
20218 enum tag_types tag_type;
20219 tree identifier;
20220 tree type = NULL_TREE;
20221 tree attributes = NULL_TREE;
20222 tree globalscope;
20223 cp_token *token = NULL;
20225 /* For class and enum types the location of the class-key or enum-key. */
20226 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20227 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20228 rid scoped_key = RID_MAX;
20230 /* See if we're looking at the `enum' keyword. */
20231 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20233 /* Consume the `enum' token. */
20234 cp_lexer_consume_token (parser->lexer);
20235 /* Remember that it's an enumeration type. */
20236 tag_type = enum_type;
20237 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20238 enums) is used here. */
20239 cp_token *token = cp_lexer_peek_token (parser->lexer);
20240 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20241 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20243 location_t loc = token->location;
20244 gcc_rich_location richloc (loc);
20245 richloc.add_range (input_location);
20246 richloc.add_fixit_remove ();
20247 pedwarn (&richloc, 0, "elaborated-type-specifier for "
20248 "a scoped enum must not use the %qD keyword",
20249 token->u.value);
20250 /* Consume the `struct' or `class' and parse it anyway. */
20251 cp_lexer_consume_token (parser->lexer);
20252 /* Create a combined location for the whole scoped-enum-key. */
20253 key_loc = make_location (key_loc, key_loc, loc);
20255 else
20256 scoped_key = RID_MAX;
20258 /* Parse the attributes. */
20259 attributes = cp_parser_attributes_opt (parser);
20261 /* Or, it might be `typename'. */
20262 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20263 RID_TYPENAME))
20265 /* Consume the `typename' token. */
20266 cp_lexer_consume_token (parser->lexer);
20267 /* Remember that it's a `typename' type. */
20268 tag_type = typename_type;
20270 /* Otherwise it must be a class-key. */
20271 else
20273 key_loc = cp_lexer_peek_token (parser->lexer)->location;
20274 tag_type = cp_parser_class_key (parser);
20275 if (tag_type == none_type)
20276 return error_mark_node;
20277 /* Parse the attributes. */
20278 attributes = cp_parser_attributes_opt (parser);
20281 /* Look for the `::' operator. */
20282 globalscope = cp_parser_global_scope_opt (parser,
20283 /*current_scope_valid_p=*/false);
20284 /* Look for the nested-name-specifier. */
20285 tree nested_name_specifier;
20286 if (tag_type == typename_type && !globalscope)
20288 nested_name_specifier
20289 = cp_parser_nested_name_specifier (parser,
20290 /*typename_keyword_p=*/true,
20291 /*check_dependency_p=*/true,
20292 /*type_p=*/true,
20293 is_declaration);
20294 if (!nested_name_specifier)
20295 return error_mark_node;
20297 else
20298 /* Even though `typename' is not present, the proposed resolution
20299 to Core Issue 180 says that in `class A<T>::B', `B' should be
20300 considered a type-name, even if `A<T>' is dependent. */
20301 nested_name_specifier
20302 = cp_parser_nested_name_specifier_opt (parser,
20303 /*typename_keyword_p=*/true,
20304 /*check_dependency_p=*/true,
20305 /*type_p=*/true,
20306 is_declaration);
20307 /* For everything but enumeration types, consider a template-id.
20308 For an enumeration type, consider only a plain identifier. */
20309 if (tag_type != enum_type)
20311 bool template_p = false;
20312 tree decl;
20314 /* Allow the `template' keyword. */
20315 template_p = cp_parser_optional_template_keyword (parser);
20316 /* If we didn't see `template', we don't know if there's a
20317 template-id or not. */
20318 if (!template_p)
20319 cp_parser_parse_tentatively (parser);
20320 /* The `template' keyword must follow a nested-name-specifier. */
20321 else if (!nested_name_specifier && !globalscope)
20323 cp_parser_error (parser, "%<template%> must follow a nested-"
20324 "name-specifier");
20325 return error_mark_node;
20328 /* Parse the template-id. */
20329 token = cp_lexer_peek_token (parser->lexer);
20330 decl = cp_parser_template_id (parser, template_p,
20331 /*check_dependency_p=*/true,
20332 tag_type,
20333 is_declaration);
20334 /* If we didn't find a template-id, look for an ordinary
20335 identifier. */
20336 if (!template_p && !cp_parser_parse_definitely (parser))
20338 /* We can get here when cp_parser_template_id, called by
20339 cp_parser_class_name with tag_type == none_type, succeeds
20340 and caches a BASELINK. Then, when called again here,
20341 instead of failing and returning an error_mark_node
20342 returns it (see template/typename17.C in C++11).
20343 ??? Could we diagnose this earlier? */
20344 else if (tag_type == typename_type && BASELINK_P (decl))
20346 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20347 type = error_mark_node;
20349 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20350 in effect, then we must assume that, upon instantiation, the
20351 template will correspond to a class. */
20352 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20353 && tag_type == typename_type)
20354 type = make_typename_type (parser->scope, decl,
20355 typename_type,
20356 /*complain=*/tf_error);
20357 /* If the `typename' keyword is in effect and DECL is not a type
20358 decl, then type is non existent. */
20359 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20361 else if (TREE_CODE (decl) == TYPE_DECL)
20363 type = check_elaborated_type_specifier (tag_type, decl,
20364 /*allow_template_p=*/true);
20366 /* If the next token is a semicolon, this must be a specialization,
20367 instantiation, or friend declaration. Check the scope while we
20368 still know whether or not we had a nested-name-specifier. */
20369 if (type != error_mark_node
20370 && !nested_name_specifier && !is_friend
20371 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20372 check_unqualified_spec_or_inst (type, token->location);
20374 else if (decl == error_mark_node)
20375 type = error_mark_node;
20378 if (!type)
20380 token = cp_lexer_peek_token (parser->lexer);
20381 identifier = cp_parser_identifier (parser);
20383 if (identifier == error_mark_node)
20385 parser->scope = NULL_TREE;
20386 return error_mark_node;
20389 /* For a `typename', we needn't call xref_tag. */
20390 if (tag_type == typename_type
20391 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20392 return cp_parser_make_typename_type (parser, identifier,
20393 token->location);
20395 /* Template parameter lists apply only if we are not within a
20396 function parameter list. */
20397 bool template_parm_lists_apply
20398 = parser->num_template_parameter_lists;
20399 if (template_parm_lists_apply)
20400 for (cp_binding_level *s = current_binding_level;
20401 s && s->kind != sk_template_parms;
20402 s = s->level_chain)
20403 if (s->kind == sk_function_parms)
20404 template_parm_lists_apply = false;
20406 /* Look up a qualified name in the usual way. */
20407 if (parser->scope)
20409 tree decl;
20410 tree ambiguous_decls;
20412 decl = cp_parser_lookup_name (parser, identifier,
20413 tag_type,
20414 /*is_template=*/false,
20415 /*is_namespace=*/false,
20416 /*check_dependency=*/true,
20417 &ambiguous_decls,
20418 token->location);
20420 /* If the lookup was ambiguous, an error will already have been
20421 issued. */
20422 if (ambiguous_decls)
20423 return error_mark_node;
20425 /* If we are parsing friend declaration, DECL may be a
20426 TEMPLATE_DECL tree node here. However, we need to check
20427 whether this TEMPLATE_DECL results in valid code. Consider
20428 the following example:
20430 namespace N {
20431 template <class T> class C {};
20433 class X {
20434 template <class T> friend class N::C; // #1, valid code
20436 template <class T> class Y {
20437 friend class N::C; // #2, invalid code
20440 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20441 name lookup of `N::C'. We see that friend declaration must
20442 be template for the code to be valid. Note that
20443 processing_template_decl does not work here since it is
20444 always 1 for the above two cases. */
20446 decl = (cp_parser_maybe_treat_template_as_class
20447 (decl, /*tag_name_p=*/is_friend
20448 && template_parm_lists_apply));
20450 if (TREE_CODE (decl) != TYPE_DECL)
20452 cp_parser_diagnose_invalid_type_name (parser,
20453 identifier,
20454 token->location);
20455 return error_mark_node;
20458 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20460 bool allow_template = (template_parm_lists_apply
20461 || DECL_SELF_REFERENCE_P (decl));
20462 type = check_elaborated_type_specifier (tag_type, decl,
20463 allow_template);
20465 if (type == error_mark_node)
20466 return error_mark_node;
20469 /* Forward declarations of nested types, such as
20471 class C1::C2;
20472 class C1::C2::C3;
20474 are invalid unless all components preceding the final '::'
20475 are complete. If all enclosing types are complete, these
20476 declarations become merely pointless.
20478 Invalid forward declarations of nested types are errors
20479 caught elsewhere in parsing. Those that are pointless arrive
20480 here. */
20482 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20483 && !is_friend && is_declaration
20484 && !processing_explicit_instantiation)
20485 warning (0, "declaration %qD does not declare anything", decl);
20487 type = TREE_TYPE (decl);
20489 else
20491 /* An elaborated-type-specifier sometimes introduces a new type and
20492 sometimes names an existing type. Normally, the rule is that it
20493 introduces a new type only if there is not an existing type of
20494 the same name already in scope. For example, given:
20496 struct S {};
20497 void f() { struct S s; }
20499 the `struct S' in the body of `f' is the same `struct S' as in
20500 the global scope; the existing definition is used. However, if
20501 there were no global declaration, this would introduce a new
20502 local class named `S'.
20504 An exception to this rule applies to the following code:
20506 namespace N { struct S; }
20508 Here, the elaborated-type-specifier names a new type
20509 unconditionally; even if there is already an `S' in the
20510 containing scope this declaration names a new type.
20511 This exception only applies if the elaborated-type-specifier
20512 forms the complete declaration:
20514 [class.name]
20516 A declaration consisting solely of `class-key identifier ;' is
20517 either a redeclaration of the name in the current scope or a
20518 forward declaration of the identifier as a class name. It
20519 introduces the name into the current scope.
20521 We are in this situation precisely when the next token is a `;'.
20523 An exception to the exception is that a `friend' declaration does
20524 *not* name a new type; i.e., given:
20526 struct S { friend struct T; };
20528 `T' is not a new type in the scope of `S'.
20530 Also, `new struct S' or `sizeof (struct S)' never results in the
20531 definition of a new type; a new type can only be declared in a
20532 declaration context. */
20534 TAG_how how;
20536 if (is_friend)
20537 /* Friends have special name lookup rules. */
20538 how = TAG_how::HIDDEN_FRIEND;
20539 else if (is_declaration
20540 && cp_lexer_next_token_is (parser->lexer,
20541 CPP_SEMICOLON))
20542 /* This is a `class-key identifier ;' */
20543 how = TAG_how::CURRENT_ONLY;
20544 else
20545 how = TAG_how::GLOBAL;
20547 bool template_p =
20548 (template_parm_lists_apply
20549 && (cp_parser_next_token_starts_class_definition_p (parser)
20550 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
20551 /* An unqualified name was used to reference this type, so
20552 there were no qualifying templates. */
20553 if (template_parm_lists_apply
20554 && !cp_parser_check_template_parameters (parser,
20555 /*num_templates=*/0,
20556 /*template_id*/false,
20557 token->location,
20558 /*declarator=*/NULL))
20559 return error_mark_node;
20561 type = xref_tag (tag_type, identifier, how, template_p);
20565 if (type == error_mark_node)
20566 return error_mark_node;
20568 /* Allow attributes on forward declarations of classes. */
20569 if (attributes)
20571 if (TREE_CODE (type) == TYPENAME_TYPE)
20572 warning (OPT_Wattributes,
20573 "attributes ignored on uninstantiated type");
20574 else if (tag_type != enum_type
20575 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
20576 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
20577 && ! processing_explicit_instantiation)
20578 warning (OPT_Wattributes,
20579 "attributes ignored on template instantiation");
20580 else if (is_friend && cxx11_attribute_p (attributes))
20582 if (warning (OPT_Wattributes, "attribute ignored"))
20583 inform (input_location, "an attribute that appertains to a friend "
20584 "declaration that is not a definition is ignored");
20586 else if (is_declaration && cp_parser_declares_only_class_p (parser))
20587 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
20588 else
20589 warning (OPT_Wattributes,
20590 "attributes ignored on elaborated-type-specifier that is "
20591 "not a forward declaration");
20594 if (tag_type == enum_type)
20595 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
20596 else
20598 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
20599 for alias definition. */
20600 bool decl_class = (is_declaration
20601 && cp_parser_declares_only_class_p (parser));
20602 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
20603 decl_class);
20605 /* Indicate whether this class was declared as a `class' or as a
20606 `struct'. */
20607 if (CLASS_TYPE_P (type) && !currently_open_class (type))
20608 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
20611 /* A "<" cannot follow an elaborated type specifier. If that
20612 happens, the user was probably trying to form a template-id. */
20613 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
20614 token->location);
20616 return type;
20619 /* Parse an enum-specifier.
20621 enum-specifier:
20622 enum-head { enumerator-list [opt] }
20623 enum-head { enumerator-list , } [C++0x]
20625 enum-head:
20626 enum-key identifier [opt] enum-base [opt]
20627 enum-key nested-name-specifier identifier enum-base [opt]
20629 enum-key:
20630 enum
20631 enum class [C++0x]
20632 enum struct [C++0x]
20634 enum-base: [C++0x]
20635 : type-specifier-seq
20637 opaque-enum-specifier:
20638 enum-key identifier enum-base [opt] ;
20640 GNU Extensions:
20641 enum-key attributes[opt] identifier [opt] enum-base [opt]
20642 { enumerator-list [opt] }attributes[opt]
20643 enum-key attributes[opt] identifier [opt] enum-base [opt]
20644 { enumerator-list, }attributes[opt] [C++0x]
20646 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20647 if the token stream isn't an enum-specifier after all. */
20649 static tree
20650 cp_parser_enum_specifier (cp_parser* parser)
20652 tree identifier;
20653 tree type = NULL_TREE;
20654 tree prev_scope;
20655 tree nested_name_specifier = NULL_TREE;
20656 tree attributes;
20657 bool scoped_enum_p = false;
20658 bool has_underlying_type = false;
20659 bool nested_being_defined = false;
20660 bool new_value_list = false;
20661 bool is_new_type = false;
20662 bool is_unnamed = false;
20663 tree underlying_type = NULL_TREE;
20664 cp_token *type_start_token = NULL;
20665 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
20667 /* Parse tentatively so that we can back up if we don't find a
20668 enum-specifier. */
20669 cp_parser_parse_tentatively (parser);
20671 /* Caller guarantees that the current token is 'enum', an identifier
20672 possibly follows, and the token after that is an opening brace.
20673 If we don't have an identifier, fabricate an anonymous name for
20674 the enumeration being defined. */
20675 cp_lexer_consume_token (parser->lexer);
20677 /* Parse the "class" or "struct", which indicates a scoped
20678 enumeration type in C++0x. */
20679 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
20680 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
20682 if (cxx_dialect < cxx11)
20683 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20685 /* Consume the `struct' or `class' token. */
20686 cp_lexer_consume_token (parser->lexer);
20688 scoped_enum_p = true;
20691 attributes = cp_parser_attributes_opt (parser);
20693 /* Clear the qualification. */
20694 parser->scope = NULL_TREE;
20695 parser->qualifying_scope = NULL_TREE;
20696 parser->object_scope = NULL_TREE;
20698 /* Figure out in what scope the declaration is being placed. */
20699 prev_scope = current_scope ();
20701 type_start_token = cp_lexer_peek_token (parser->lexer);
20703 push_deferring_access_checks (dk_no_check);
20704 nested_name_specifier
20705 = cp_parser_nested_name_specifier_opt (parser,
20706 /*typename_keyword_p=*/true,
20707 /*check_dependency_p=*/false,
20708 /*type_p=*/false,
20709 /*is_declaration=*/false);
20711 if (nested_name_specifier)
20713 tree name;
20715 identifier = cp_parser_identifier (parser);
20716 name = cp_parser_lookup_name (parser, identifier,
20717 enum_type,
20718 /*is_template=*/false,
20719 /*is_namespace=*/false,
20720 /*check_dependency=*/true,
20721 /*ambiguous_decls=*/NULL,
20722 input_location);
20723 if (name && name != error_mark_node)
20725 type = TREE_TYPE (name);
20726 if (TREE_CODE (type) == TYPENAME_TYPE)
20728 /* Are template enums allowed in ISO? */
20729 if (template_parm_scope_p ())
20730 pedwarn (type_start_token->location, OPT_Wpedantic,
20731 "%qD is an enumeration template", name);
20732 /* ignore a typename reference, for it will be solved by name
20733 in start_enum. */
20734 type = NULL_TREE;
20737 else if (nested_name_specifier == error_mark_node)
20738 /* We already issued an error. */;
20739 else
20741 error_at (type_start_token->location,
20742 "%qD does not name an enumeration in %qT",
20743 identifier, nested_name_specifier);
20744 nested_name_specifier = error_mark_node;
20747 else
20749 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20750 identifier = cp_parser_identifier (parser);
20751 else
20753 identifier = make_anon_name ();
20754 is_unnamed = true;
20755 if (scoped_enum_p)
20756 error_at (type_start_token->location,
20757 "unnamed scoped enum is not allowed");
20760 pop_deferring_access_checks ();
20762 /* Check for the `:' that denotes a specified underlying type in C++0x.
20763 Note that a ':' could also indicate a bitfield width, however. */
20764 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20766 cp_decl_specifier_seq type_specifiers;
20768 /* Consume the `:'. */
20769 cp_lexer_consume_token (parser->lexer);
20771 auto tdf
20772 = make_temp_override (parser->type_definition_forbidden_message,
20773 G_("types may not be defined in enum-base"));
20775 /* Parse the type-specifier-seq. */
20776 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
20777 /*is_declaration=*/false,
20778 /*is_trailing_return=*/false,
20779 &type_specifiers);
20781 /* At this point this is surely not elaborated type specifier. */
20782 if (!cp_parser_parse_definitely (parser))
20783 return NULL_TREE;
20785 if (cxx_dialect < cxx11)
20786 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20788 has_underlying_type = true;
20790 /* If that didn't work, stop. */
20791 if (type_specifiers.type != error_mark_node)
20793 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
20794 /*initialized=*/0, NULL);
20795 if (underlying_type == error_mark_node
20796 || check_for_bare_parameter_packs (underlying_type))
20797 underlying_type = NULL_TREE;
20801 /* Look for the `{' but don't consume it yet. */
20802 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20804 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
20806 if (has_underlying_type)
20807 cp_parser_commit_to_tentative_parse (parser);
20808 cp_parser_error (parser, "expected %<{%>");
20809 if (has_underlying_type)
20810 return error_mark_node;
20812 /* An opaque-enum-specifier must have a ';' here. */
20813 if ((scoped_enum_p || underlying_type)
20814 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20816 if (has_underlying_type)
20817 cp_parser_commit_to_tentative_parse (parser);
20818 cp_parser_error (parser, "expected %<;%> or %<{%>");
20819 if (has_underlying_type)
20820 return error_mark_node;
20824 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
20825 return NULL_TREE;
20827 if (nested_name_specifier)
20829 if (CLASS_TYPE_P (nested_name_specifier))
20831 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
20832 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
20833 push_scope (nested_name_specifier);
20835 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
20836 push_nested_namespace (nested_name_specifier);
20839 /* Issue an error message if type-definitions are forbidden here. */
20840 if (!cp_parser_check_type_definition (parser))
20841 type = error_mark_node;
20842 else
20843 /* Create the new type. We do this before consuming the opening
20844 brace so the enum will be recorded as being on the line of its
20845 tag (or the 'enum' keyword, if there is no tag). */
20846 type = start_enum (identifier, type, underlying_type,
20847 attributes, scoped_enum_p, &is_new_type);
20849 /* If the next token is not '{' it is an opaque-enum-specifier or an
20850 elaborated-type-specifier. */
20851 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20853 timevar_push (TV_PARSE_ENUM);
20854 if (nested_name_specifier
20855 && nested_name_specifier != error_mark_node)
20857 /* The following catches invalid code such as:
20858 enum class S<int>::E { A, B, C }; */
20859 if (!processing_specialization
20860 && CLASS_TYPE_P (nested_name_specifier)
20861 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
20862 error_at (type_start_token->location, "cannot add an enumerator "
20863 "list to a template instantiation");
20865 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
20867 error_at (type_start_token->location,
20868 "%<%T::%E%> has not been declared",
20869 TYPE_CONTEXT (nested_name_specifier),
20870 nested_name_specifier);
20871 type = error_mark_node;
20873 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
20874 && !CLASS_TYPE_P (nested_name_specifier))
20876 error_at (type_start_token->location, "nested name specifier "
20877 "%qT for enum declaration does not name a class "
20878 "or namespace", nested_name_specifier);
20879 type = error_mark_node;
20881 /* If that scope does not contain the scope in which the
20882 class was originally declared, the program is invalid. */
20883 else if (prev_scope && !is_ancestor (prev_scope,
20884 nested_name_specifier))
20886 if (at_namespace_scope_p ())
20887 error_at (type_start_token->location,
20888 "declaration of %qD in namespace %qD which does not "
20889 "enclose %qD",
20890 type, prev_scope, nested_name_specifier);
20891 else
20892 error_at (type_start_token->location,
20893 "declaration of %qD in %qD which does not "
20894 "enclose %qD",
20895 type, prev_scope, nested_name_specifier);
20896 type = error_mark_node;
20898 /* If that scope is the scope where the declaration is being placed
20899 the program is invalid. */
20900 else if (CLASS_TYPE_P (nested_name_specifier)
20901 && CLASS_TYPE_P (prev_scope)
20902 && same_type_p (nested_name_specifier, prev_scope))
20904 permerror (type_start_token->location,
20905 "extra qualification not allowed");
20906 nested_name_specifier = NULL_TREE;
20910 if (scoped_enum_p)
20911 begin_scope (sk_scoped_enum, type);
20913 /* Consume the opening brace. */
20914 matching_braces braces;
20915 braces.consume_open (parser);
20917 if (type == error_mark_node)
20918 ; /* Nothing to add */
20919 else if (OPAQUE_ENUM_P (type)
20920 || (cxx_dialect > cxx98 && processing_specialization))
20922 new_value_list = true;
20923 SET_OPAQUE_ENUM_P (type, false);
20924 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20926 else
20928 error_at (type_start_token->location,
20929 "multiple definition of %q#T", type);
20930 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
20931 "previous definition here");
20932 type = error_mark_node;
20935 if (type == error_mark_node)
20936 cp_parser_skip_to_end_of_block_or_statement (parser);
20937 /* If the next token is not '}', then there are some enumerators. */
20938 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20940 if (is_unnamed && !scoped_enum_p)
20941 pedwarn (type_start_token->location, OPT_Wpedantic,
20942 "ISO C++ forbids empty unnamed enum");
20944 else
20946 /* We've seen a '{' so we know we're in an enum-specifier.
20947 Commit to any tentative parse to get syntax errors. */
20948 cp_parser_commit_to_tentative_parse (parser);
20949 cp_parser_enumerator_list (parser, type);
20952 /* Consume the final '}'. */
20953 braces.require_close (parser);
20955 if (scoped_enum_p)
20956 finish_scope ();
20957 timevar_pop (TV_PARSE_ENUM);
20959 else
20961 /* If a ';' follows, then it is an opaque-enum-specifier
20962 and additional restrictions apply. */
20963 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20965 if (is_unnamed)
20966 error_at (type_start_token->location,
20967 "opaque-enum-specifier without name");
20968 else if (nested_name_specifier)
20969 error_at (type_start_token->location,
20970 "opaque-enum-specifier must use a simple identifier");
20974 /* Look for trailing attributes to apply to this enumeration, and
20975 apply them if appropriate. */
20976 if (cp_parser_allow_gnu_extensions_p (parser))
20978 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
20979 cplus_decl_attributes (&type,
20980 trailing_attr,
20981 (int) ATTR_FLAG_TYPE_IN_PLACE);
20984 /* Finish up the enumeration. */
20985 if (type != error_mark_node)
20987 if (new_value_list)
20988 finish_enum_value_list (type);
20989 if (is_new_type)
20990 finish_enum (type);
20993 if (nested_name_specifier)
20995 if (CLASS_TYPE_P (nested_name_specifier))
20997 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
20998 pop_scope (nested_name_specifier);
21000 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21001 pop_nested_namespace (nested_name_specifier);
21003 return type;
21006 /* Parse an enumerator-list. The enumerators all have the indicated
21007 TYPE.
21009 enumerator-list:
21010 enumerator-definition
21011 enumerator-list , enumerator-definition */
21013 static void
21014 cp_parser_enumerator_list (cp_parser* parser, tree type)
21016 while (true)
21018 /* Parse an enumerator-definition. */
21019 cp_parser_enumerator_definition (parser, type);
21021 /* If the next token is not a ',', we've reached the end of
21022 the list. */
21023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21024 break;
21025 /* Otherwise, consume the `,' and keep going. */
21026 cp_lexer_consume_token (parser->lexer);
21027 /* If the next token is a `}', there is a trailing comma. */
21028 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21030 if (cxx_dialect < cxx11)
21031 pedwarn (input_location, OPT_Wpedantic,
21032 "comma at end of enumerator list");
21033 break;
21038 /* Parse an enumerator-definition. The enumerator has the indicated
21039 TYPE.
21041 enumerator-definition:
21042 enumerator
21043 enumerator = constant-expression
21045 enumerator:
21046 identifier
21048 GNU Extensions:
21050 enumerator-definition:
21051 enumerator attributes [opt]
21052 enumerator attributes [opt] = constant-expression */
21054 static void
21055 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21057 tree identifier;
21058 tree value;
21059 location_t loc;
21061 /* Save the input location because we are interested in the location
21062 of the identifier and not the location of the explicit value. */
21063 loc = cp_lexer_peek_token (parser->lexer)->location;
21065 /* Look for the identifier. */
21066 identifier = cp_parser_identifier (parser);
21067 if (identifier == error_mark_node)
21068 return;
21070 /* Parse any specified attributes. */
21071 tree attrs = cp_parser_attributes_opt (parser);
21073 /* If the next token is an '=', then there is an explicit value. */
21074 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21076 /* Consume the `=' token. */
21077 cp_lexer_consume_token (parser->lexer);
21078 /* Parse the value. */
21079 value = cp_parser_constant_expression (parser);
21081 else
21082 value = NULL_TREE;
21084 /* If we are processing a template, make sure the initializer of the
21085 enumerator doesn't contain any bare template parameter pack. */
21086 if (check_for_bare_parameter_packs (value))
21087 value = error_mark_node;
21089 /* Create the enumerator. */
21090 build_enumerator (identifier, value, type, attrs, loc);
21093 /* Parse a namespace-name.
21095 namespace-name:
21096 original-namespace-name
21097 namespace-alias
21099 Returns the NAMESPACE_DECL for the namespace. */
21101 static tree
21102 cp_parser_namespace_name (cp_parser* parser)
21104 tree identifier;
21105 tree namespace_decl;
21107 cp_token *token = cp_lexer_peek_token (parser->lexer);
21109 /* Get the name of the namespace. */
21110 identifier = cp_parser_identifier (parser);
21111 if (identifier == error_mark_node)
21112 return error_mark_node;
21114 /* Look up the identifier in the currently active scope. Look only
21115 for namespaces, due to:
21117 [basic.lookup.udir]
21119 When looking up a namespace-name in a using-directive or alias
21120 definition, only namespace names are considered.
21122 And:
21124 [basic.lookup.qual]
21126 During the lookup of a name preceding the :: scope resolution
21127 operator, object, function, and enumerator names are ignored.
21129 (Note that cp_parser_qualifying_entity only calls this
21130 function if the token after the name is the scope resolution
21131 operator.) */
21132 namespace_decl = cp_parser_lookup_name (parser, identifier,
21133 none_type,
21134 /*is_template=*/false,
21135 /*is_namespace=*/true,
21136 /*check_dependency=*/true,
21137 /*ambiguous_decls=*/NULL,
21138 token->location);
21139 /* If it's not a namespace, issue an error. */
21140 if (namespace_decl == error_mark_node
21141 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21143 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21145 auto_diagnostic_group d;
21146 name_hint hint;
21147 if (namespace_decl == error_mark_node
21148 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21149 hint = suggest_alternative_in_explicit_scope (token->location,
21150 identifier,
21151 parser->scope);
21152 if (const char *suggestion = hint.suggestion ())
21154 gcc_rich_location richloc (token->location);
21155 richloc.add_fixit_replace (suggestion);
21156 error_at (&richloc,
21157 "%qD is not a namespace-name; did you mean %qs?",
21158 identifier, suggestion);
21160 else
21161 error_at (token->location, "%qD is not a namespace-name",
21162 identifier);
21164 else
21165 cp_parser_error (parser, "expected namespace-name");
21166 namespace_decl = error_mark_node;
21169 return namespace_decl;
21172 /* Parse a namespace-definition.
21174 namespace-definition:
21175 named-namespace-definition
21176 unnamed-namespace-definition
21178 named-namespace-definition:
21179 original-namespace-definition
21180 extension-namespace-definition
21182 original-namespace-definition:
21183 namespace identifier { namespace-body }
21185 extension-namespace-definition:
21186 namespace original-namespace-name { namespace-body }
21188 unnamed-namespace-definition:
21189 namespace { namespace-body } */
21191 static void
21192 cp_parser_namespace_definition (cp_parser* parser)
21194 tree identifier;
21195 int nested_definition_count = 0;
21197 cp_ensure_no_omp_declare_simd (parser);
21198 cp_ensure_no_oacc_routine (parser);
21200 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21201 const bool topmost_inline_p = is_inline;
21203 if (is_inline)
21205 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21206 cp_lexer_consume_token (parser->lexer);
21209 /* Look for the `namespace' keyword. */
21210 cp_token* token
21211 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21213 /* Parse any specified attributes before the identifier. */
21214 tree attribs = cp_parser_attributes_opt (parser);
21216 for (;;)
21218 identifier = NULL_TREE;
21220 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21221 RID_INLINE);
21222 if (nested_inline_p && nested_definition_count != 0)
21224 if (pedantic && cxx_dialect < cxx20)
21225 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21226 OPT_Wc__20_extensions, "nested inline namespace "
21227 "definitions only available with %<-std=c++20%> or "
21228 "%<-std=gnu++20%>");
21229 cp_lexer_consume_token (parser->lexer);
21232 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21234 identifier = cp_parser_identifier (parser);
21236 if (cp_next_tokens_can_be_std_attribute_p (parser))
21237 pedwarn (input_location, OPT_Wpedantic,
21238 "standard attributes on namespaces must precede "
21239 "the namespace name");
21241 /* Parse any attributes specified after the identifier. */
21242 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21247 /* Don't forget that the innermost namespace might have been
21248 marked as inline. Use |= because we cannot overwrite
21249 IS_INLINE in case the outermost namespace is inline, but
21250 there are no nested inlines. */
21251 is_inline |= nested_inline_p;
21252 break;
21255 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21256 pedwarn (input_location, OPT_Wc__17_extensions,
21257 "nested namespace definitions only available with "
21258 "%<-std=c++17%> or %<-std=gnu++17%>");
21260 /* Nested namespace names can create new namespaces (unlike
21261 other qualified-ids). */
21262 if (int count = (identifier
21263 ? push_namespace (identifier, nested_inline_p)
21264 : 0))
21265 nested_definition_count += count;
21266 else
21267 cp_parser_error (parser, "nested namespace name required");
21268 cp_lexer_consume_token (parser->lexer);
21271 if (nested_definition_count && !identifier)
21272 cp_parser_error (parser, "namespace name required");
21274 if (nested_definition_count && attribs)
21275 error_at (token->location,
21276 "a nested namespace definition cannot have attributes");
21277 if (nested_definition_count && topmost_inline_p)
21278 error_at (token->location,
21279 "a nested namespace definition cannot be inline");
21281 /* Start the namespace. */
21282 nested_definition_count += push_namespace (identifier, is_inline);
21284 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21286 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21288 /* Look for the `{' to validate starting the namespace. */
21289 matching_braces braces;
21290 if (braces.require_open (parser))
21292 /* Parse the body of the namespace. */
21293 cp_parser_namespace_body (parser);
21295 /* Look for the final `}'. */
21296 braces.require_close (parser);
21299 if (has_visibility)
21300 pop_visibility (1);
21302 /* Pop the nested namespace definitions. */
21303 while (nested_definition_count--)
21304 pop_namespace ();
21307 /* Parse a namespace-body.
21309 namespace-body:
21310 declaration-seq [opt] */
21312 static void
21313 cp_parser_namespace_body (cp_parser* parser)
21315 cp_parser_declaration_seq_opt (parser);
21318 /* Parse a namespace-alias-definition.
21320 namespace-alias-definition:
21321 namespace identifier = qualified-namespace-specifier ; */
21323 static void
21324 cp_parser_namespace_alias_definition (cp_parser* parser)
21326 tree identifier;
21327 tree namespace_specifier;
21329 cp_token *token = cp_lexer_peek_token (parser->lexer);
21331 /* Look for the `namespace' keyword. */
21332 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21333 /* Look for the identifier. */
21334 identifier = cp_parser_identifier (parser);
21335 if (identifier == error_mark_node)
21336 return;
21337 /* Look for the `=' token. */
21338 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21339 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21341 error_at (token->location, "%<namespace%> definition is not allowed here");
21342 /* Skip the definition. */
21343 cp_lexer_consume_token (parser->lexer);
21344 if (cp_parser_skip_to_closing_brace (parser))
21345 cp_lexer_consume_token (parser->lexer);
21346 return;
21348 cp_parser_require (parser, CPP_EQ, RT_EQ);
21349 /* Look for the qualified-namespace-specifier. */
21350 namespace_specifier
21351 = cp_parser_qualified_namespace_specifier (parser);
21352 cp_warn_deprecated_use_scopes (namespace_specifier);
21353 /* Look for the `;' token. */
21354 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21356 /* Register the alias in the symbol table. */
21357 do_namespace_alias (identifier, namespace_specifier);
21360 /* Parse a qualified-namespace-specifier.
21362 qualified-namespace-specifier:
21363 :: [opt] nested-name-specifier [opt] namespace-name
21365 Returns a NAMESPACE_DECL corresponding to the specified
21366 namespace. */
21368 static tree
21369 cp_parser_qualified_namespace_specifier (cp_parser* parser)
21371 /* Look for the optional `::'. */
21372 cp_parser_global_scope_opt (parser,
21373 /*current_scope_valid_p=*/false);
21375 /* Look for the optional nested-name-specifier. */
21376 cp_parser_nested_name_specifier_opt (parser,
21377 /*typename_keyword_p=*/false,
21378 /*check_dependency_p=*/true,
21379 /*type_p=*/false,
21380 /*is_declaration=*/true);
21382 return cp_parser_namespace_name (parser);
21385 /* Subroutine of cp_parser_using_declaration. */
21387 static tree
21388 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21390 tree decl = NULL_TREE;
21391 if (at_class_scope_p ())
21393 /* Create the USING_DECL. */
21394 decl = do_class_using_decl (qscope, identifier);
21396 if (check_for_bare_parameter_packs (decl))
21397 return error_mark_node;
21399 if (decl && typename_p)
21400 USING_DECL_TYPENAME_P (decl) = 1;
21402 /* Add it to the list of members in this class. */
21403 finish_member_declaration (decl);
21405 else
21406 finish_nonmember_using_decl (qscope, identifier);
21407 return decl;
21410 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21411 access declaration.
21413 using-declaration:
21414 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21415 using :: unqualified-id ;
21417 access-declaration:
21418 qualified-id ;
21422 static bool
21423 cp_parser_using_declaration (cp_parser* parser,
21424 bool access_declaration_p)
21426 cp_token *token;
21427 bool typename_p = false;
21428 bool global_scope_p;
21429 tree identifier;
21430 tree qscope;
21431 int oldcount = errorcount;
21432 cp_token *diag_token = NULL;
21434 if (access_declaration_p)
21436 diag_token = cp_lexer_peek_token (parser->lexer);
21437 cp_parser_parse_tentatively (parser);
21439 else
21441 /* Look for the `using' keyword. */
21442 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21444 again:
21445 /* Peek at the next token. */
21446 token = cp_lexer_peek_token (parser->lexer);
21447 /* See if it's `typename'. */
21448 if (token->keyword == RID_TYPENAME)
21450 /* Remember that we've seen it. */
21451 typename_p = true;
21452 /* Consume the `typename' token. */
21453 cp_lexer_consume_token (parser->lexer);
21457 /* Look for the optional global scope qualification. */
21458 global_scope_p
21459 = (cp_parser_global_scope_opt (parser,
21460 /*current_scope_valid_p=*/false)
21461 != NULL_TREE);
21463 /* If we saw `typename', or didn't see `::', then there must be a
21464 nested-name-specifier present. */
21465 if (typename_p || !global_scope_p)
21467 qscope = cp_parser_nested_name_specifier (parser, typename_p,
21468 /*check_dependency_p=*/true,
21469 /*type_p=*/false,
21470 /*is_declaration=*/true);
21471 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21473 cp_parser_skip_to_end_of_block_or_statement (parser);
21474 return false;
21477 /* Otherwise, we could be in either of the two productions. In that
21478 case, treat the nested-name-specifier as optional. */
21479 else
21480 qscope = cp_parser_nested_name_specifier_opt (parser,
21481 /*typename_keyword_p=*/false,
21482 /*check_dependency_p=*/true,
21483 /*type_p=*/false,
21484 /*is_declaration=*/true);
21485 if (!qscope)
21486 qscope = global_namespace;
21488 cp_warn_deprecated_use_scopes (qscope);
21490 if (access_declaration_p && cp_parser_error_occurred (parser))
21491 /* Something has already gone wrong; there's no need to parse
21492 further. Since an error has occurred, the return value of
21493 cp_parser_parse_definitely will be false, as required. */
21494 return cp_parser_parse_definitely (parser);
21496 token = cp_lexer_peek_token (parser->lexer);
21497 /* Parse the unqualified-id. */
21498 identifier = cp_parser_unqualified_id (parser,
21499 /*template_keyword_p=*/false,
21500 /*check_dependency_p=*/true,
21501 /*declarator_p=*/true,
21502 /*optional_p=*/false);
21504 if (access_declaration_p)
21506 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21507 cp_parser_simulate_error (parser);
21508 if (!cp_parser_parse_definitely (parser))
21509 return false;
21511 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21513 cp_token *ell = cp_lexer_consume_token (parser->lexer);
21514 if (cxx_dialect < cxx17)
21515 pedwarn (ell->location, OPT_Wc__17_extensions,
21516 "pack expansion in using-declaration only available "
21517 "with %<-std=c++17%> or %<-std=gnu++17%>");
21518 qscope = make_pack_expansion (qscope);
21521 /* The function we call to handle a using-declaration is different
21522 depending on what scope we are in. */
21523 if (qscope == error_mark_node || identifier == error_mark_node)
21525 else if (!identifier_p (identifier)
21526 && TREE_CODE (identifier) != BIT_NOT_EXPR)
21527 /* [namespace.udecl]
21529 A using declaration shall not name a template-id. */
21530 error_at (token->location,
21531 "a template-id may not appear in a using-declaration");
21532 else
21534 tree decl = finish_using_decl (qscope, identifier, typename_p);
21536 if (decl == error_mark_node)
21538 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21539 return false;
21543 if (!access_declaration_p
21544 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21546 cp_token *comma = cp_lexer_consume_token (parser->lexer);
21547 if (cxx_dialect < cxx17)
21548 pedwarn (comma->location, OPT_Wc__17_extensions,
21549 "comma-separated list in using-declaration only available "
21550 "with %<-std=c++17%> or %<-std=gnu++17%>");
21551 goto again;
21554 /* Look for the final `;'. */
21555 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21557 if (access_declaration_p && errorcount == oldcount)
21558 warning_at (diag_token->location, OPT_Wdeprecated,
21559 "access declarations are deprecated "
21560 "in favour of using-declarations; "
21561 "suggestion: add the %<using%> keyword");
21563 return true;
21566 /* C++20 using enum declaration.
21568 using-enum-declaration :
21569 using elaborated-enum-specifier ; */
21571 static void
21572 cp_parser_using_enum (cp_parser *parser)
21574 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21576 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21577 breaks one of the motivating examples in using-enum-5.C.
21578 cp_parser_simple_type_specifier seems to be closer to what we actually
21579 want, though that hasn't been properly specified yet. */
21581 /* Consume 'enum'. */
21582 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
21583 cp_lexer_consume_token (parser->lexer);
21585 cp_token *start = cp_lexer_peek_token (parser->lexer);
21587 tree type = (cp_parser_simple_type_specifier
21588 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
21590 cp_token *end = cp_lexer_previous_token (parser->lexer);
21592 if (type == error_mark_node
21593 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21595 cp_parser_skip_to_end_of_block_or_statement (parser);
21596 return;
21598 if (TREE_CODE (type) == TYPE_DECL)
21599 type = TREE_TYPE (type);
21601 /* The elaborated-enum-specifier shall not name a dependent type and the type
21602 shall have a reachable enum-specifier. */
21603 const char *msg = nullptr;
21604 if (cxx_dialect < cxx20)
21605 msg = _("%<using enum%> "
21606 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21607 else if (dependent_type_p (type))
21608 msg = _("%<using enum%> of dependent type %qT");
21609 else if (TREE_CODE (type) != ENUMERAL_TYPE)
21610 msg = _("%<using enum%> of non-enumeration type %q#T");
21611 else if (!COMPLETE_TYPE_P (type))
21612 msg = _("%<using enum%> of incomplete type %qT");
21613 else if (OPAQUE_ENUM_P (type))
21614 msg = _("%<using enum%> of %qT before its enum-specifier");
21615 if (msg)
21617 location_t loc = make_location (start, start, end);
21618 auto_diagnostic_group g;
21619 error_at (loc, msg, type);
21620 loc = location_of (type);
21621 if (cxx_dialect < cxx20 || loc == input_location)
21623 else if (OPAQUE_ENUM_P (type))
21624 inform (loc, "opaque-enum-declaration here");
21625 else
21626 inform (loc, "declared here");
21629 /* A using-enum-declaration introduces the enumerator names of the named
21630 enumeration as if by a using-declaration for each enumerator. */
21631 if (TREE_CODE (type) == ENUMERAL_TYPE)
21632 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
21633 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
21636 /* Parse an alias-declaration.
21638 alias-declaration:
21639 using identifier attribute-specifier-seq [opt] = type-id */
21641 static tree
21642 cp_parser_alias_declaration (cp_parser* parser)
21644 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
21645 location_t id_location, type_location;
21646 cp_declarator *declarator;
21647 cp_decl_specifier_seq decl_specs;
21648 bool member_p;
21649 const char *saved_message = NULL;
21651 /* Look for the `using' keyword. */
21652 cp_token *using_token
21653 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
21654 if (using_token == NULL)
21655 return error_mark_node;
21657 id_location = cp_lexer_peek_token (parser->lexer)->location;
21658 id = cp_parser_identifier (parser);
21659 if (id == error_mark_node)
21660 return error_mark_node;
21662 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
21663 attributes = cp_parser_attributes_opt (parser);
21664 if (attributes == error_mark_node)
21665 return error_mark_node;
21667 cp_parser_require (parser, CPP_EQ, RT_EQ);
21669 if (cp_parser_error_occurred (parser))
21670 return error_mark_node;
21672 cp_parser_commit_to_tentative_parse (parser);
21674 /* Now we are going to parse the type-id of the declaration. */
21677 [dcl.type]/3 says:
21679 "A type-specifier-seq shall not define a class or enumeration
21680 unless it appears in the type-id of an alias-declaration (7.1.3) that
21681 is not the declaration of a template-declaration."
21683 In other words, if we currently are in an alias template, the
21684 type-id should not define a type.
21686 So let's set parser->type_definition_forbidden_message in that
21687 case; cp_parser_check_type_definition (called by
21688 cp_parser_class_specifier) will then emit an error if a type is
21689 defined in the type-id. */
21690 if (parser->num_template_parameter_lists)
21692 saved_message = parser->type_definition_forbidden_message;
21693 parser->type_definition_forbidden_message =
21694 G_("types may not be defined in alias template declarations");
21697 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21698 &type_location);
21700 /* Restore the error message if need be. */
21701 if (parser->num_template_parameter_lists)
21702 parser->type_definition_forbidden_message = saved_message;
21704 if (type == error_mark_node
21705 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21707 cp_parser_skip_to_end_of_block_or_statement (parser);
21708 return error_mark_node;
21711 /* A typedef-name can also be introduced by an alias-declaration. The
21712 identifier following the using keyword becomes a typedef-name. It has
21713 the same semantics as if it were introduced by the typedef
21714 specifier. In particular, it does not define a new type and it shall
21715 not appear in the type-id. */
21717 clear_decl_specs (&decl_specs);
21718 decl_specs.type = type;
21719 if (attributes != NULL_TREE)
21721 decl_specs.attributes = attributes;
21722 set_and_check_decl_spec_loc (&decl_specs,
21723 ds_attribute,
21724 attrs_token);
21726 set_and_check_decl_spec_loc (&decl_specs,
21727 ds_typedef,
21728 using_token);
21729 set_and_check_decl_spec_loc (&decl_specs,
21730 ds_alias,
21731 using_token);
21732 decl_specs.locations[ds_type_spec] = type_location;
21734 if (parser->num_template_parameter_lists
21735 && !cp_parser_check_template_parameters (parser,
21736 /*num_templates=*/0,
21737 /*template_id*/false,
21738 id_location,
21739 /*declarator=*/NULL))
21740 return error_mark_node;
21742 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
21744 member_p = at_class_scope_p ();
21745 if (member_p)
21746 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
21747 NULL_TREE, attributes);
21748 else
21749 decl = start_decl (declarator, &decl_specs, 0,
21750 attributes, NULL_TREE, &pushed_scope);
21751 if (decl == error_mark_node)
21752 return decl;
21754 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
21756 if (pushed_scope)
21757 pop_scope (pushed_scope);
21759 /* If decl is a template, return its TEMPLATE_DECL so that it gets
21760 added into the symbol table; otherwise, return the TYPE_DECL. */
21761 if (DECL_LANG_SPECIFIC (decl)
21762 && DECL_TEMPLATE_INFO (decl)
21763 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
21765 decl = DECL_TI_TEMPLATE (decl);
21766 if (member_p)
21767 check_member_template (decl);
21770 return decl;
21773 /* Parse a using-directive.
21775 using-directive:
21776 attribute-specifier-seq [opt] using namespace :: [opt]
21777 nested-name-specifier [opt] namespace-name ; */
21779 static void
21780 cp_parser_using_directive (cp_parser* parser)
21782 tree namespace_decl;
21783 tree attribs = cp_parser_std_attribute_spec_seq (parser);
21784 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21786 /* Error during attribute parsing that resulted in skipping
21787 to next semicolon. */
21788 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21789 return;
21792 /* Look for the `using' keyword. */
21793 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21794 /* And the `namespace' keyword. */
21795 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21796 /* Look for the optional `::' operator. */
21797 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21798 /* And the optional nested-name-specifier. */
21799 cp_parser_nested_name_specifier_opt (parser,
21800 /*typename_keyword_p=*/false,
21801 /*check_dependency_p=*/true,
21802 /*type_p=*/false,
21803 /*is_declaration=*/true);
21804 /* Get the namespace being used. */
21805 namespace_decl = cp_parser_namespace_name (parser);
21806 cp_warn_deprecated_use_scopes (namespace_decl);
21807 /* And any specified GNU attributes. */
21808 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21809 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
21811 /* Update the symbol table. */
21812 finish_using_directive (namespace_decl, attribs);
21814 /* Look for the final `;'. */
21815 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21818 /* Parse an asm-definition.
21820 asm-qualifier:
21821 volatile
21822 inline
21823 goto
21825 asm-qualifier-list:
21826 asm-qualifier
21827 asm-qualifier-list asm-qualifier
21829 asm-definition:
21830 asm ( string-literal ) ;
21832 GNU Extension:
21834 asm-definition:
21835 asm asm-qualifier-list [opt] ( string-literal ) ;
21836 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
21837 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21838 : asm-operand-list [opt] ) ;
21839 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21840 : asm-operand-list [opt]
21841 : asm-clobber-list [opt] ) ;
21842 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
21843 : asm-clobber-list [opt]
21844 : asm-goto-list ) ;
21846 The form with asm-goto-list is valid if and only if the asm-qualifier-list
21847 contains goto, and is the only allowed form in that case. No duplicates are
21848 allowed in an asm-qualifier-list. */
21850 static void
21851 cp_parser_asm_definition (cp_parser* parser)
21853 tree string;
21854 tree outputs = NULL_TREE;
21855 tree inputs = NULL_TREE;
21856 tree clobbers = NULL_TREE;
21857 tree labels = NULL_TREE;
21858 tree asm_stmt;
21859 bool extended_p = false;
21860 bool invalid_inputs_p = false;
21861 bool invalid_outputs_p = false;
21862 required_token missing = RT_NONE;
21863 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
21865 /* Look for the `asm' keyword. */
21866 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
21868 /* In C++20, unevaluated inline assembly is permitted in constexpr
21869 functions. */
21870 if (parser->in_function_body
21871 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
21872 && cxx_dialect < cxx20)
21873 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
21874 "function only available with %<-std=c++20%> or "
21875 "%<-std=gnu++20%>");
21877 /* Handle the asm-qualifier-list. */
21878 location_t volatile_loc = UNKNOWN_LOCATION;
21879 location_t inline_loc = UNKNOWN_LOCATION;
21880 location_t goto_loc = UNKNOWN_LOCATION;
21881 location_t first_loc = UNKNOWN_LOCATION;
21883 if (cp_parser_allow_gnu_extensions_p (parser))
21884 for (;;)
21886 cp_token *token = cp_lexer_peek_token (parser->lexer);
21887 location_t loc = token->location;
21888 switch (cp_lexer_peek_token (parser->lexer)->keyword)
21890 case RID_VOLATILE:
21891 if (volatile_loc)
21893 error_at (loc, "duplicate %<asm%> qualifier %qT",
21894 token->u.value);
21895 inform (volatile_loc, "first seen here");
21897 else
21899 if (!parser->in_function_body)
21900 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
21901 "outside of function body", token->u.value);
21902 volatile_loc = loc;
21904 cp_lexer_consume_token (parser->lexer);
21905 continue;
21907 case RID_INLINE:
21908 if (inline_loc)
21910 error_at (loc, "duplicate %<asm%> qualifier %qT",
21911 token->u.value);
21912 inform (inline_loc, "first seen here");
21914 else
21915 inline_loc = loc;
21916 if (!first_loc)
21917 first_loc = loc;
21918 cp_lexer_consume_token (parser->lexer);
21919 continue;
21921 case RID_GOTO:
21922 if (goto_loc)
21924 error_at (loc, "duplicate %<asm%> qualifier %qT",
21925 token->u.value);
21926 inform (goto_loc, "first seen here");
21928 else
21929 goto_loc = loc;
21930 if (!first_loc)
21931 first_loc = loc;
21932 cp_lexer_consume_token (parser->lexer);
21933 continue;
21935 case RID_CONST:
21936 case RID_RESTRICT:
21937 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
21938 cp_lexer_consume_token (parser->lexer);
21939 continue;
21941 default:
21942 break;
21944 break;
21947 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
21948 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
21949 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
21951 if (!parser->in_function_body && (inline_p || goto_p))
21953 error_at (first_loc, "%<asm%> qualifier outside of function body");
21954 inline_p = goto_p = false;
21957 /* Look for the opening `('. */
21958 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21959 return;
21960 /* Look for the string. */
21961 string = cp_parser_string_literal (parser, false, false);
21962 if (string == error_mark_node)
21964 cp_parser_skip_to_closing_parenthesis (parser, true, false,
21965 /*consume_paren=*/true);
21966 return;
21969 /* If we're allowing GNU extensions, check for the extended assembly
21970 syntax. Unfortunately, the `:' tokens need not be separated by
21971 a space in C, and so, for compatibility, we tolerate that here
21972 too. Doing that means that we have to treat the `::' operator as
21973 two `:' tokens. */
21974 if (cp_parser_allow_gnu_extensions_p (parser)
21975 && parser->in_function_body
21976 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
21977 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
21979 bool inputs_p = false;
21980 bool clobbers_p = false;
21981 bool labels_p = false;
21983 /* The extended syntax was used. */
21984 extended_p = true;
21986 /* Look for outputs. */
21987 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21989 /* Consume the `:'. */
21990 cp_lexer_consume_token (parser->lexer);
21991 /* Parse the output-operands. */
21992 if (cp_lexer_next_token_is_not (parser->lexer,
21993 CPP_COLON)
21994 && cp_lexer_next_token_is_not (parser->lexer,
21995 CPP_SCOPE)
21996 && cp_lexer_next_token_is_not (parser->lexer,
21997 CPP_CLOSE_PAREN))
21999 outputs = cp_parser_asm_operand_list (parser);
22000 if (outputs == error_mark_node)
22001 invalid_outputs_p = true;
22004 /* If the next token is `::', there are no outputs, and the
22005 next token is the beginning of the inputs. */
22006 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22007 /* The inputs are coming next. */
22008 inputs_p = true;
22010 /* Look for inputs. */
22011 if (inputs_p
22012 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22014 /* Consume the `:' or `::'. */
22015 cp_lexer_consume_token (parser->lexer);
22016 /* Parse the output-operands. */
22017 if (cp_lexer_next_token_is_not (parser->lexer,
22018 CPP_COLON)
22019 && cp_lexer_next_token_is_not (parser->lexer,
22020 CPP_SCOPE)
22021 && cp_lexer_next_token_is_not (parser->lexer,
22022 CPP_CLOSE_PAREN))
22024 inputs = cp_parser_asm_operand_list (parser);
22025 if (inputs == error_mark_node)
22026 invalid_inputs_p = true;
22029 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22030 /* The clobbers are coming next. */
22031 clobbers_p = true;
22033 /* Look for clobbers. */
22034 if (clobbers_p
22035 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22037 clobbers_p = true;
22038 /* Consume the `:' or `::'. */
22039 cp_lexer_consume_token (parser->lexer);
22040 /* Parse the clobbers. */
22041 if (cp_lexer_next_token_is_not (parser->lexer,
22042 CPP_COLON)
22043 && cp_lexer_next_token_is_not (parser->lexer,
22044 CPP_CLOSE_PAREN))
22045 clobbers = cp_parser_asm_clobber_list (parser);
22047 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22048 /* The labels are coming next. */
22049 labels_p = true;
22051 /* Look for labels. */
22052 if (labels_p
22053 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22055 labels_p = true;
22056 /* Consume the `:' or `::'. */
22057 cp_lexer_consume_token (parser->lexer);
22058 /* Parse the labels. */
22059 labels = cp_parser_asm_label_list (parser);
22062 if (goto_p && !labels_p)
22063 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22065 else if (goto_p)
22066 missing = RT_COLON_SCOPE;
22068 /* Look for the closing `)'. */
22069 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22070 missing ? missing : RT_CLOSE_PAREN))
22071 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22072 /*consume_paren=*/true);
22073 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22075 if (!invalid_inputs_p && !invalid_outputs_p)
22077 /* Create the ASM_EXPR. */
22078 if (parser->in_function_body)
22080 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22081 inputs, clobbers, labels, inline_p);
22082 /* If the extended syntax was not used, mark the ASM_EXPR. */
22083 if (!extended_p)
22085 tree temp = asm_stmt;
22086 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22087 temp = TREE_OPERAND (temp, 0);
22089 ASM_INPUT_P (temp) = 1;
22092 else
22093 symtab->finalize_toplevel_asm (string);
22097 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22098 type that comes from the decl-specifier-seq. */
22100 static tree
22101 strip_declarator_types (tree type, cp_declarator *declarator)
22103 for (cp_declarator *d = declarator; d;)
22104 switch (d->kind)
22106 case cdk_id:
22107 case cdk_decomp:
22108 case cdk_error:
22109 d = NULL;
22110 break;
22112 default:
22113 if (TYPE_PTRMEMFUNC_P (type))
22114 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22115 type = TREE_TYPE (type);
22116 d = d->declarator;
22117 break;
22120 return type;
22123 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22124 a construct looks like a variable definition but is actually a function
22125 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22126 is the declarator for this function declaration. */
22128 static void
22129 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22130 const cp_declarator *declarator)
22132 /* Only warn if we are declaring a function at block scope. */
22133 if (!at_function_scope_p ())
22134 return;
22136 /* And only if there is no storage class specified. */
22137 if (decl_specifiers->storage_class != sc_none
22138 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22139 return;
22141 if (declarator->kind != cdk_function
22142 || !declarator->declarator
22143 || declarator->declarator->kind != cdk_id
22144 || !identifier_p (get_unqualified_id
22145 (const_cast<cp_declarator *>(declarator))))
22146 return;
22148 /* Don't warn when the whole declarator (not just the declarator-id!)
22149 was parenthesized. That is, don't warn for int(n()) but do warn
22150 for int(f)(). */
22151 if (declarator->parenthesized != UNKNOWN_LOCATION)
22152 return;
22154 tree type;
22155 if (decl_specifiers->type)
22157 type = decl_specifiers->type;
22158 if (TREE_CODE (type) == TYPE_DECL)
22159 type = TREE_TYPE (type);
22161 /* If the return type is void there is no ambiguity. */
22162 if (same_type_p (type, void_type_node))
22163 return;
22165 else if (decl_specifiers->any_type_specifiers_p)
22166 /* Code like long f(); will have null ->type. If we have any
22167 type-specifiers, pretend we've seen int. */
22168 type = integer_type_node;
22169 else
22170 return;
22172 auto_diagnostic_group d;
22173 location_t loc = declarator->u.function.parens_loc;
22174 tree params = declarator->u.function.parameters;
22175 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22177 /* The T t() case. */
22178 if (params == void_list_node)
22180 if (warning_at (loc, OPT_Wvexing_parse,
22181 "empty parentheses were disambiguated as a function "
22182 "declaration"))
22184 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22185 means value-initialization or aggregate-initialization, nothing
22186 means default-initialization. We can only suggest removing the
22187 parentheses/adding {} if T has a default constructor. */
22188 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22190 gcc_rich_location iloc (loc);
22191 iloc.add_fixit_remove ();
22192 inform (&iloc, "remove parentheses to default-initialize "
22193 "a variable");
22194 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22196 if (CP_AGGREGATE_TYPE_P (type))
22197 inform (loc, "or replace parentheses with braces to "
22198 "aggregate-initialize a variable");
22199 else
22200 inform (loc, "or replace parentheses with braces to "
22201 "value-initialize a variable");
22205 return;
22208 /* If we had (...) or the parameter-list wasn't parenthesized,
22209 we're done. */
22210 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22211 return;
22213 /* The T t(X()) case. */
22214 if (list_length (params) == 2)
22216 if (warning_at (loc, OPT_Wvexing_parse,
22217 "parentheses were disambiguated as a function "
22218 "declaration"))
22220 gcc_rich_location iloc (loc);
22221 /* {}-initialization means that we can use an initializer-list
22222 constructor if no default constructor is available, so don't
22223 suggest using {} for classes that have an initializer_list
22224 constructor. */
22225 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22227 iloc.add_fixit_replace (get_start (loc), "{");
22228 iloc.add_fixit_replace (get_finish (loc), "}");
22229 inform (&iloc, "replace parentheses with braces to declare a "
22230 "variable");
22232 else
22234 iloc.add_fixit_insert_after (get_start (loc), "(");
22235 iloc.add_fixit_insert_before (get_finish (loc), ")");
22236 inform (&iloc, "add parentheses to declare a variable");
22240 /* The T t(X(), X()) case. */
22241 else if (warning_at (loc, OPT_Wvexing_parse,
22242 "parentheses were disambiguated as a function "
22243 "declaration"))
22245 gcc_rich_location iloc (loc);
22246 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22248 iloc.add_fixit_replace (get_start (loc), "{");
22249 iloc.add_fixit_replace (get_finish (loc), "}");
22250 inform (&iloc, "replace parentheses with braces to declare a "
22251 "variable");
22256 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22257 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22258 has the same meaning as in cp_parser_declarator. */
22260 static void
22261 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22262 cp_decl_specifier_seq *decl_specs,
22263 cp_declarator *declarator,
22264 int *ctor_dtor_or_conv_p)
22266 if (cxx_dialect >= cxx17
22267 && *ctor_dtor_or_conv_p <= 0
22268 && !decl_specs->type
22269 && !decl_specs->any_type_specifiers_p
22270 && function_declarator_p (declarator))
22272 cp_declarator *id = get_id_declarator (declarator);
22273 tree name = id->u.id.unqualified_name;
22274 parser->scope = id->u.id.qualifying_scope;
22275 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22276 if (tmpl
22277 && (DECL_CLASS_TEMPLATE_P (tmpl)
22278 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22280 id->u.id.unqualified_name = dguide_name (tmpl);
22281 id->u.id.sfk = sfk_deduction_guide;
22282 *ctor_dtor_or_conv_p = 1;
22287 /* Declarators [gram.dcl.decl] */
22289 /* Parse an init-declarator.
22291 init-declarator:
22292 declarator initializer [opt]
22294 GNU Extension:
22296 init-declarator:
22297 declarator asm-specification [opt] attributes [opt] initializer [opt]
22299 function-definition:
22300 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22301 function-body
22302 decl-specifier-seq [opt] declarator function-try-block
22304 GNU Extension:
22306 function-definition:
22307 __extension__ function-definition
22309 TM Extension:
22311 function-definition:
22312 decl-specifier-seq [opt] declarator function-transaction-block
22314 The parser flags FLAGS is used to control type-specifier parsing.
22316 The DECL_SPECIFIERS apply to this declarator. Returns a
22317 representation of the entity declared. If MEMBER_P is TRUE, then
22318 this declarator appears in a class scope. The new DECL created by
22319 this declarator is returned.
22321 The CHECKS are access checks that should be performed once we know
22322 what entity is being declared (and, therefore, what classes have
22323 befriended it).
22325 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22326 for a function-definition here as well. If the declarator is a
22327 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22328 be TRUE upon return. By that point, the function-definition will
22329 have been completely parsed.
22331 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22332 is FALSE.
22334 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22335 parsed declaration if it is an uninitialized single declarator not followed
22336 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22337 if present, will not be consumed. If returned, this declarator will be
22338 created with SD_INITIALIZED but will not call cp_finish_decl.
22340 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22341 and there is an initializer, the pointed location_t is set to the
22342 location of the '=' or `(', or '{' in C++11 token introducing the
22343 initializer. */
22345 static tree
22346 cp_parser_init_declarator (cp_parser* parser,
22347 cp_parser_flags flags,
22348 cp_decl_specifier_seq *decl_specifiers,
22349 vec<deferred_access_check, va_gc> *checks,
22350 bool function_definition_allowed_p,
22351 bool member_p,
22352 int declares_class_or_enum,
22353 bool* function_definition_p,
22354 tree* maybe_range_for_decl,
22355 location_t* init_loc,
22356 tree* auto_result)
22358 cp_token *token = NULL, *asm_spec_start_token = NULL,
22359 *attributes_start_token = NULL;
22360 cp_declarator *declarator;
22361 tree prefix_attributes;
22362 tree attributes = NULL;
22363 tree asm_specification;
22364 tree initializer;
22365 tree decl = NULL_TREE;
22366 tree scope;
22367 int is_initialized;
22368 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22369 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22370 "(...)". */
22371 enum cpp_ttype initialization_kind;
22372 bool is_direct_init = false;
22373 bool is_non_constant_init;
22374 int ctor_dtor_or_conv_p;
22375 bool friend_p = cp_parser_friend_p (decl_specifiers);
22376 bool static_p = decl_specifiers->storage_class == sc_static;
22377 tree pushed_scope = NULL_TREE;
22378 bool range_for_decl_p = false;
22379 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22380 location_t tmp_init_loc = UNKNOWN_LOCATION;
22382 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22383 flags |= CP_PARSER_FLAGS_CONSTEVAL;
22385 /* Assume that this is not the declarator for a function
22386 definition. */
22387 if (function_definition_p)
22388 *function_definition_p = false;
22390 /* Default arguments are only permitted for function parameters. */
22391 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22392 parser->default_arg_ok_p = false;
22394 /* Defer access checks while parsing the declarator; we cannot know
22395 what names are accessible until we know what is being
22396 declared. */
22397 resume_deferring_access_checks ();
22399 token = cp_lexer_peek_token (parser->lexer);
22401 /* Parse the declarator. */
22402 declarator
22403 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22404 flags, &ctor_dtor_or_conv_p,
22405 /*parenthesized_p=*/NULL,
22406 member_p, friend_p, static_p);
22407 /* Gather up the deferred checks. */
22408 stop_deferring_access_checks ();
22410 parser->default_arg_ok_p = saved_default_arg_ok_p;
22412 /* If the DECLARATOR was erroneous, there's no need to go
22413 further. */
22414 if (declarator == cp_error_declarator)
22415 return error_mark_node;
22417 /* Check that the number of template-parameter-lists is OK. */
22418 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22419 token->location))
22420 return error_mark_node;
22422 if (declares_class_or_enum & 2)
22423 cp_parser_check_for_definition_in_return_type (declarator,
22424 decl_specifiers->type,
22425 decl_specifiers->locations[ds_type_spec]);
22427 /* Figure out what scope the entity declared by the DECLARATOR is
22428 located in. `grokdeclarator' sometimes changes the scope, so
22429 we compute it now. */
22430 scope = get_scope_of_declarator (declarator);
22432 /* Perform any lookups in the declared type which were thought to be
22433 dependent, but are not in the scope of the declarator. */
22434 decl_specifiers->type
22435 = maybe_update_decl_type (decl_specifiers->type, scope);
22437 /* If we're allowing GNU extensions, look for an
22438 asm-specification. */
22439 if (cp_parser_allow_gnu_extensions_p (parser))
22441 /* Look for an asm-specification. */
22442 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22443 asm_specification = cp_parser_asm_specification_opt (parser);
22445 else
22446 asm_specification = NULL_TREE;
22448 /* Gather the attributes that were provided with the
22449 decl-specifiers. */
22450 prefix_attributes = decl_specifiers->attributes;
22452 /* Look for attributes. */
22453 attributes_start_token = cp_lexer_peek_token (parser->lexer);
22454 attributes = cp_parser_attributes_opt (parser);
22456 /* Peek at the next token. */
22457 token = cp_lexer_peek_token (parser->lexer);
22459 bool bogus_implicit_tmpl = false;
22461 if (function_declarator_p (declarator))
22463 /* Handle C++17 deduction guides. Note that class-scope
22464 non-template deduction guides are instead handled in
22465 cp_parser_member_declaration. */
22466 cp_parser_maybe_adjust_declarator_for_dguide (parser,
22467 decl_specifiers,
22468 declarator,
22469 &ctor_dtor_or_conv_p);
22471 if (!member_p && !cp_parser_error_occurred (parser))
22472 warn_about_ambiguous_parse (decl_specifiers, declarator);
22474 /* Check to see if the token indicates the start of a
22475 function-definition. */
22476 if (cp_parser_token_starts_function_definition_p (token))
22478 if (!function_definition_allowed_p)
22480 /* If a function-definition should not appear here, issue an
22481 error message. */
22482 cp_parser_error (parser,
22483 "a function-definition is not allowed here");
22484 return error_mark_node;
22487 location_t func_brace_location
22488 = cp_lexer_peek_token (parser->lexer)->location;
22490 /* Neither attributes nor an asm-specification are allowed
22491 on a function-definition. */
22492 if (asm_specification)
22493 error_at (asm_spec_start_token->location,
22494 "an %<asm%> specification is not allowed "
22495 "on a function-definition");
22496 if (attributes)
22497 error_at (attributes_start_token->location,
22498 "attributes are not allowed "
22499 "on a function-definition");
22500 /* This is a function-definition. */
22501 *function_definition_p = true;
22503 /* Parse the function definition. */
22504 if (member_p)
22505 decl = cp_parser_save_member_function_body (parser,
22506 decl_specifiers,
22507 declarator,
22508 prefix_attributes);
22509 else
22510 decl =
22511 (cp_parser_function_definition_from_specifiers_and_declarator
22512 (parser, decl_specifiers, prefix_attributes, declarator));
22514 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
22516 /* This is where the prologue starts... */
22517 DECL_STRUCT_FUNCTION (decl)->function_start_locus
22518 = func_brace_location;
22521 return decl;
22524 else if (parser->fully_implicit_function_template_p)
22526 /* A non-template declaration involving a function parameter list
22527 containing an implicit template parameter will be made into a
22528 template. If the resulting declaration is not going to be an
22529 actual function then finish the template scope here to prevent it.
22530 An error message will be issued once we have a decl to talk about.
22532 FIXME probably we should do type deduction rather than create an
22533 implicit template, but the standard currently doesn't allow it. */
22534 bogus_implicit_tmpl = true;
22535 finish_fully_implicit_template (parser, NULL_TREE);
22538 /* [dcl.dcl]
22540 Only in function declarations for constructors, destructors, type
22541 conversions, and deduction guides can the decl-specifier-seq be omitted.
22543 We explicitly postpone this check past the point where we handle
22544 function-definitions because we tolerate function-definitions
22545 that are missing their return types in some modes. */
22546 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
22548 cp_parser_error (parser,
22549 "expected constructor, destructor, or type conversion");
22550 return error_mark_node;
22553 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
22554 if (token->type == CPP_EQ
22555 || token->type == CPP_OPEN_PAREN
22556 || token->type == CPP_OPEN_BRACE)
22558 is_initialized = SD_INITIALIZED;
22559 initialization_kind = token->type;
22560 declarator->init_loc = token->location;
22561 if (maybe_range_for_decl)
22562 *maybe_range_for_decl = error_mark_node;
22563 tmp_init_loc = token->location;
22564 if (init_loc && *init_loc == UNKNOWN_LOCATION)
22565 *init_loc = tmp_init_loc;
22567 if (token->type == CPP_EQ
22568 && function_declarator_p (declarator))
22570 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
22571 if (t2->keyword == RID_DEFAULT)
22572 is_initialized = SD_DEFAULTED;
22573 else if (t2->keyword == RID_DELETE)
22574 is_initialized = SD_DELETED;
22577 else
22579 /* If the init-declarator isn't initialized and isn't followed by a
22580 `,' or `;', it's not a valid init-declarator. */
22581 if (token->type != CPP_COMMA
22582 && token->type != CPP_SEMICOLON)
22584 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
22585 range_for_decl_p = true;
22586 else
22588 if (!maybe_range_for_decl)
22589 cp_parser_error (parser, "expected initializer");
22590 return error_mark_node;
22593 is_initialized = SD_UNINITIALIZED;
22594 initialization_kind = CPP_EOF;
22597 /* Because start_decl has side-effects, we should only call it if we
22598 know we're going ahead. By this point, we know that we cannot
22599 possibly be looking at any other construct. */
22600 cp_parser_commit_to_tentative_parse (parser);
22602 /* Enter the newly declared entry in the symbol table. If we're
22603 processing a declaration in a class-specifier, we wait until
22604 after processing the initializer. */
22605 if (!member_p)
22607 if (parser->in_unbraced_linkage_specification_p)
22608 decl_specifiers->storage_class = sc_extern;
22609 decl = start_decl (declarator, decl_specifiers,
22610 range_for_decl_p? SD_INITIALIZED : is_initialized,
22611 attributes, prefix_attributes, &pushed_scope);
22612 cp_finalize_omp_declare_simd (parser, decl);
22613 cp_finalize_oacc_routine (parser, decl, false);
22614 /* Adjust location of decl if declarator->id_loc is more appropriate:
22615 set, and decl wasn't merged with another decl, in which case its
22616 location would be different from input_location, and more accurate. */
22617 if (DECL_P (decl)
22618 && declarator->id_loc != UNKNOWN_LOCATION
22619 && DECL_SOURCE_LOCATION (decl) == input_location)
22620 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
22622 else if (scope)
22623 /* Enter the SCOPE. That way unqualified names appearing in the
22624 initializer will be looked up in SCOPE. */
22625 pushed_scope = push_scope (scope);
22627 /* Perform deferred access control checks, now that we know in which
22628 SCOPE the declared entity resides. */
22629 if (!member_p && decl)
22631 tree saved_current_function_decl = NULL_TREE;
22633 /* If the entity being declared is a function, pretend that we
22634 are in its scope. If it is a `friend', it may have access to
22635 things that would not otherwise be accessible. */
22636 if (TREE_CODE (decl) == FUNCTION_DECL)
22638 saved_current_function_decl = current_function_decl;
22639 current_function_decl = decl;
22642 /* Perform access checks for template parameters. */
22643 cp_parser_perform_template_parameter_access_checks (checks);
22645 /* Perform the access control checks for the declarator and the
22646 decl-specifiers. */
22647 perform_deferred_access_checks (tf_warning_or_error);
22649 /* Restore the saved value. */
22650 if (TREE_CODE (decl) == FUNCTION_DECL)
22651 current_function_decl = saved_current_function_decl;
22654 /* Parse the initializer. */
22655 initializer = NULL_TREE;
22656 is_direct_init = false;
22657 is_non_constant_init = true;
22658 if (is_initialized)
22660 if (function_declarator_p (declarator))
22662 if (initialization_kind == CPP_EQ)
22663 initializer = cp_parser_pure_specifier (parser);
22664 else
22666 /* If the declaration was erroneous, we don't really
22667 know what the user intended, so just silently
22668 consume the initializer. */
22669 if (decl != error_mark_node)
22670 error_at (tmp_init_loc, "initializer provided for function");
22671 cp_parser_skip_to_closing_parenthesis (parser,
22672 /*recovering=*/true,
22673 /*or_comma=*/false,
22674 /*consume_paren=*/true);
22677 else
22679 /* We want to record the extra mangling scope for in-class
22680 initializers of class members and initializers of static
22681 data member templates and namespace-scope initializers.
22682 The former involves deferring parsing of the initializer
22683 until end of class as with default arguments. So right
22684 here we only handle the latter two. */
22685 bool has_lambda_scope = false;
22687 if (decl != error_mark_node
22688 && !member_p
22689 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
22690 has_lambda_scope = true;
22692 if (has_lambda_scope)
22693 start_lambda_scope (decl);
22694 initializer = cp_parser_initializer (parser,
22695 &is_direct_init,
22696 &is_non_constant_init);
22697 if (has_lambda_scope)
22698 finish_lambda_scope ();
22699 if (initializer == error_mark_node)
22700 cp_parser_skip_to_end_of_statement (parser);
22704 /* The old parser allows attributes to appear after a parenthesized
22705 initializer. Mark Mitchell proposed removing this functionality
22706 on the GCC mailing lists on 2002-08-13. This parser accepts the
22707 attributes -- but ignores them. Made a permerror in GCC 8. */
22708 if (cp_parser_allow_gnu_extensions_p (parser)
22709 && initialization_kind == CPP_OPEN_PAREN
22710 && cp_parser_attributes_opt (parser)
22711 && permerror (input_location,
22712 "attributes after parenthesized initializer ignored"))
22714 static bool hint;
22715 if (flag_permissive && !hint)
22717 hint = true;
22718 inform (input_location,
22719 "this flexibility is deprecated and will be removed");
22723 /* And now complain about a non-function implicit template. */
22724 if (bogus_implicit_tmpl && decl != error_mark_node)
22725 error_at (DECL_SOURCE_LOCATION (decl),
22726 "non-function %qD declared as implicit template", decl);
22728 /* For an in-class declaration, use `grokfield' to create the
22729 declaration. */
22730 if (member_p)
22732 if (pushed_scope)
22734 pop_scope (pushed_scope);
22735 pushed_scope = NULL_TREE;
22737 decl = grokfield (declarator, decl_specifiers,
22738 initializer, !is_non_constant_init,
22739 /*asmspec=*/NULL_TREE,
22740 attr_chainon (attributes, prefix_attributes));
22741 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
22742 cp_parser_save_default_args (parser, decl);
22743 cp_finalize_omp_declare_simd (parser, decl);
22744 cp_finalize_oacc_routine (parser, decl, false);
22747 /* Finish processing the declaration. But, skip member
22748 declarations. */
22749 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
22751 cp_finish_decl (decl,
22752 initializer, !is_non_constant_init,
22753 asm_specification,
22754 /* If the initializer is in parentheses, then this is
22755 a direct-initialization, which means that an
22756 `explicit' constructor is OK. Otherwise, an
22757 `explicit' constructor cannot be used. */
22758 ((is_direct_init || !is_initialized)
22759 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
22761 else if ((cxx_dialect != cxx98) && friend_p
22762 && decl && TREE_CODE (decl) == FUNCTION_DECL)
22763 /* Core issue #226 (C++0x only): A default template-argument
22764 shall not be specified in a friend class template
22765 declaration. */
22766 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
22767 /*is_partial=*/false, /*is_friend_decl=*/1);
22769 if (!friend_p && pushed_scope)
22770 pop_scope (pushed_scope);
22772 if (function_declarator_p (declarator)
22773 && parser->fully_implicit_function_template_p)
22775 if (member_p)
22776 decl = finish_fully_implicit_template (parser, decl);
22777 else
22778 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22781 if (auto_result && is_initialized && decl_specifiers->type
22782 && type_uses_auto (decl_specifiers->type))
22783 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
22785 return decl;
22788 /* Parse a declarator.
22790 declarator:
22791 direct-declarator
22792 ptr-operator declarator
22794 abstract-declarator:
22795 ptr-operator abstract-declarator [opt]
22796 direct-abstract-declarator
22798 GNU Extensions:
22800 declarator:
22801 attributes [opt] direct-declarator
22802 attributes [opt] ptr-operator declarator
22804 abstract-declarator:
22805 attributes [opt] ptr-operator abstract-declarator [opt]
22806 attributes [opt] direct-abstract-declarator
22808 The parser flags FLAGS is used to control type-specifier parsing.
22810 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
22811 detect constructors, destructors, deduction guides, or conversion operators.
22812 It is set to -1 if the declarator is a name, and +1 if it is a
22813 function. Otherwise it is set to zero. Usually you just want to
22814 test for >0, but internally the negative value is used.
22816 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
22817 a decl-specifier-seq unless it declares a constructor, destructor,
22818 or conversion. It might seem that we could check this condition in
22819 semantic analysis, rather than parsing, but that makes it difficult
22820 to handle something like `f()'. We want to notice that there are
22821 no decl-specifiers, and therefore realize that this is an
22822 expression, not a declaration.)
22824 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
22825 the declarator is a direct-declarator of the form "(...)".
22827 MEMBER_P is true iff this declarator is a member-declarator.
22829 FRIEND_P is true iff this declarator is a friend.
22831 STATIC_P is true iff the keyword static was seen. */
22833 static cp_declarator *
22834 cp_parser_declarator (cp_parser* parser,
22835 cp_parser_declarator_kind dcl_kind,
22836 cp_parser_flags flags,
22837 int* ctor_dtor_or_conv_p,
22838 bool* parenthesized_p,
22839 bool member_p, bool friend_p, bool static_p)
22841 cp_declarator *declarator;
22842 enum tree_code code;
22843 cp_cv_quals cv_quals;
22844 tree class_type;
22845 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
22847 /* Assume this is not a constructor, destructor, or type-conversion
22848 operator. */
22849 if (ctor_dtor_or_conv_p)
22850 *ctor_dtor_or_conv_p = 0;
22852 if (cp_parser_allow_gnu_extensions_p (parser))
22853 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
22855 /* Check for the ptr-operator production. */
22856 cp_parser_parse_tentatively (parser);
22857 /* Parse the ptr-operator. */
22858 code = cp_parser_ptr_operator (parser,
22859 &class_type,
22860 &cv_quals,
22861 &std_attributes);
22863 /* If that worked, then we have a ptr-operator. */
22864 if (cp_parser_parse_definitely (parser))
22866 /* If a ptr-operator was found, then this declarator was not
22867 parenthesized. */
22868 if (parenthesized_p)
22869 *parenthesized_p = false;
22870 /* The dependent declarator is optional if we are parsing an
22871 abstract-declarator. */
22872 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
22873 cp_parser_parse_tentatively (parser);
22875 /* Parse the dependent declarator. */
22876 declarator = cp_parser_declarator (parser, dcl_kind, flags,
22877 /*ctor_dtor_or_conv_p=*/NULL,
22878 /*parenthesized_p=*/NULL,
22879 member_p, friend_p, static_p);
22881 /* If we are parsing an abstract-declarator, we must handle the
22882 case where the dependent declarator is absent. */
22883 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
22884 && !cp_parser_parse_definitely (parser))
22885 declarator = NULL;
22887 declarator = cp_parser_make_indirect_declarator
22888 (code, class_type, cv_quals, declarator, std_attributes);
22890 /* Everything else is a direct-declarator. */
22891 else
22893 if (parenthesized_p)
22894 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
22895 CPP_OPEN_PAREN);
22896 declarator = cp_parser_direct_declarator (parser, dcl_kind,
22897 flags, ctor_dtor_or_conv_p,
22898 member_p, friend_p, static_p);
22901 if (gnu_attributes && declarator && declarator != cp_error_declarator)
22902 declarator->attributes = gnu_attributes;
22903 return declarator;
22906 /* Parse a direct-declarator or direct-abstract-declarator.
22908 direct-declarator:
22909 declarator-id
22910 direct-declarator ( parameter-declaration-clause )
22911 cv-qualifier-seq [opt]
22912 ref-qualifier [opt]
22913 exception-specification [opt]
22914 direct-declarator [ constant-expression [opt] ]
22915 ( declarator )
22917 direct-abstract-declarator:
22918 direct-abstract-declarator [opt]
22919 ( parameter-declaration-clause )
22920 cv-qualifier-seq [opt]
22921 ref-qualifier [opt]
22922 exception-specification [opt]
22923 direct-abstract-declarator [opt] [ constant-expression [opt] ]
22924 ( abstract-declarator )
22926 Returns a representation of the declarator. DCL_KIND is
22927 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
22928 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
22929 we are parsing a direct-declarator. It is
22930 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
22931 of ambiguity we prefer an abstract declarator, as per
22932 [dcl.ambig.res].
22933 The parser flags FLAGS is used to control type-specifier parsing.
22934 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
22935 as for cp_parser_declarator. */
22937 static cp_declarator *
22938 cp_parser_direct_declarator (cp_parser* parser,
22939 cp_parser_declarator_kind dcl_kind,
22940 cp_parser_flags flags,
22941 int* ctor_dtor_or_conv_p,
22942 bool member_p, bool friend_p, bool static_p)
22944 cp_token *token;
22945 cp_declarator *declarator = NULL;
22946 tree scope = NULL_TREE;
22947 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22948 bool saved_in_declarator_p = parser->in_declarator_p;
22949 bool first = true;
22950 tree pushed_scope = NULL_TREE;
22951 cp_token *open_paren = NULL, *close_paren = NULL;
22953 while (true)
22955 /* Peek at the next token. */
22956 token = cp_lexer_peek_token (parser->lexer);
22957 if (token->type == CPP_OPEN_PAREN)
22959 /* This is either a parameter-declaration-clause, or a
22960 parenthesized declarator. When we know we are parsing a
22961 named declarator, it must be a parenthesized declarator
22962 if FIRST is true. For instance, `(int)' is a
22963 parameter-declaration-clause, with an omitted
22964 direct-abstract-declarator. But `((*))', is a
22965 parenthesized abstract declarator. Finally, when T is a
22966 template parameter `(T)' is a
22967 parameter-declaration-clause, and not a parenthesized
22968 named declarator.
22970 We first try and parse a parameter-declaration-clause,
22971 and then try a nested declarator (if FIRST is true).
22973 It is not an error for it not to be a
22974 parameter-declaration-clause, even when FIRST is
22975 false. Consider,
22977 int i (int);
22978 int i (3);
22980 The first is the declaration of a function while the
22981 second is the definition of a variable, including its
22982 initializer.
22984 Having seen only the parenthesis, we cannot know which of
22985 these two alternatives should be selected. Even more
22986 complex are examples like:
22988 int i (int (a));
22989 int i (int (3));
22991 The former is a function-declaration; the latter is a
22992 variable initialization.
22994 Thus again, we try a parameter-declaration-clause, and if
22995 that fails, we back out and return. */
22997 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
22999 tree params;
23000 bool is_declarator = false;
23002 open_paren = NULL;
23004 /* In a member-declarator, the only valid interpretation
23005 of a parenthesis is the start of a
23006 parameter-declaration-clause. (It is invalid to
23007 initialize a static data member with a parenthesized
23008 initializer; only the "=" form of initialization is
23009 permitted.) */
23010 if (!member_p)
23011 cp_parser_parse_tentatively (parser);
23013 /* Consume the `('. */
23014 const location_t parens_start = token->location;
23015 matching_parens parens;
23016 parens.consume_open (parser);
23017 if (first)
23019 /* If this is going to be an abstract declarator, we're
23020 in a declarator and we can't have default args. */
23021 parser->default_arg_ok_p = false;
23022 parser->in_declarator_p = true;
23025 begin_scope (sk_function_parms, NULL_TREE);
23027 /* Signal we are in the immediate function context. */
23028 if (flags & CP_PARSER_FLAGS_CONSTEVAL)
23029 current_binding_level->immediate_fn_ctx_p = true;
23031 /* Parse the parameter-declaration-clause. */
23032 params
23033 = cp_parser_parameter_declaration_clause (parser, flags);
23034 const location_t parens_end
23035 = cp_lexer_peek_token (parser->lexer)->location;
23037 /* Consume the `)'. */
23038 parens.require_close (parser);
23040 /* If all went well, parse the cv-qualifier-seq,
23041 ref-qualifier and the exception-specification. */
23042 if (member_p || cp_parser_parse_definitely (parser))
23044 cp_cv_quals cv_quals;
23045 cp_virt_specifiers virt_specifiers;
23046 cp_ref_qualifier ref_qual;
23047 tree exception_specification;
23048 tree late_return;
23049 tree attrs;
23050 bool memfn = (member_p || (pushed_scope
23051 && CLASS_TYPE_P (pushed_scope)));
23052 unsigned char local_variables_forbidden_p
23053 = parser->local_variables_forbidden_p;
23054 /* 'this' is not allowed in static member functions. */
23055 if (static_p || friend_p)
23056 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23058 is_declarator = true;
23060 if (ctor_dtor_or_conv_p)
23061 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23062 first = false;
23064 /* Parse the cv-qualifier-seq. */
23065 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23066 /* Parse the ref-qualifier. */
23067 ref_qual = cp_parser_ref_qualifier_opt (parser);
23068 /* Parse the tx-qualifier. */
23069 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23071 tree save_ccp = current_class_ptr;
23072 tree save_ccr = current_class_ref;
23073 if (memfn && !friend_p && !static_p)
23074 /* DR 1207: 'this' is in scope after the cv-quals. */
23075 inject_this_parameter (current_class_type, cv_quals);
23077 /* If it turned out that this is e.g. a pointer to a
23078 function, we don't want to delay noexcept parsing. */
23079 if (declarator == NULL || declarator->kind != cdk_id)
23080 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23082 /* Parse the exception-specification. */
23083 exception_specification
23084 = cp_parser_exception_specification_opt (parser,
23085 flags);
23087 attrs = cp_parser_std_attribute_spec_seq (parser);
23089 cp_omp_declare_simd_data odsd;
23090 if ((flag_openmp || flag_openmp_simd)
23091 && declarator
23092 && declarator->std_attributes
23093 && declarator->kind == cdk_id)
23095 tree *pa = &declarator->std_attributes;
23096 cp_parser_handle_directive_omp_attributes (parser, pa,
23097 &odsd, false);
23100 /* In here, we handle cases where attribute is used after
23101 the function declaration. For example:
23102 void func (int x) __attribute__((vector(..))); */
23103 tree gnu_attrs = NULL_TREE;
23104 tree requires_clause = NULL_TREE;
23105 late_return
23106 = cp_parser_late_return_type_opt (parser, declarator,
23107 requires_clause);
23109 cp_finalize_omp_declare_simd (parser, &odsd);
23111 /* Parse the virt-specifier-seq. */
23112 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23114 location_t parens_loc = make_location (parens_start,
23115 parens_start,
23116 parens_end);
23117 /* Create the function-declarator. */
23118 declarator = make_call_declarator (declarator,
23119 params,
23120 cv_quals,
23121 virt_specifiers,
23122 ref_qual,
23123 tx_qual,
23124 exception_specification,
23125 late_return,
23126 requires_clause,
23127 parens_loc);
23128 declarator->std_attributes = attrs;
23129 declarator->attributes = gnu_attrs;
23130 /* Any subsequent parameter lists are to do with
23131 return type, so are not those of the declared
23132 function. */
23133 parser->default_arg_ok_p = false;
23135 current_class_ptr = save_ccp;
23136 current_class_ref = save_ccr;
23138 /* Restore the state of local_variables_forbidden_p. */
23139 parser->local_variables_forbidden_p
23140 = local_variables_forbidden_p;
23143 /* Remove the function parms from scope. */
23144 pop_bindings_and_leave_scope ();
23146 if (is_declarator)
23147 /* Repeat the main loop. */
23148 continue;
23151 /* If this is the first, we can try a parenthesized
23152 declarator. */
23153 if (first)
23155 bool saved_in_type_id_in_expr_p;
23157 parser->default_arg_ok_p = saved_default_arg_ok_p;
23158 parser->in_declarator_p = saved_in_declarator_p;
23160 open_paren = token;
23161 /* Consume the `('. */
23162 matching_parens parens;
23163 parens.consume_open (parser);
23164 /* Parse the nested declarator. */
23165 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23166 parser->in_type_id_in_expr_p = true;
23167 declarator
23168 = cp_parser_declarator (parser, dcl_kind, flags,
23169 ctor_dtor_or_conv_p,
23170 /*parenthesized_p=*/NULL,
23171 member_p, friend_p,
23172 /*static_p=*/false);
23173 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23174 first = false;
23175 /* Expect a `)'. */
23176 close_paren = cp_lexer_peek_token (parser->lexer);
23177 if (!parens.require_close (parser))
23178 declarator = cp_error_declarator;
23179 if (declarator == cp_error_declarator)
23180 break;
23182 goto handle_declarator;
23184 /* Otherwise, we must be done. */
23185 else
23186 break;
23188 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23189 && token->type == CPP_OPEN_SQUARE
23190 && !cp_next_tokens_can_be_attribute_p (parser))
23192 /* Parse an array-declarator. */
23193 tree bounds, attrs;
23195 if (ctor_dtor_or_conv_p)
23196 *ctor_dtor_or_conv_p = 0;
23198 open_paren = NULL;
23199 first = false;
23200 parser->default_arg_ok_p = false;
23201 parser->in_declarator_p = true;
23202 /* Consume the `['. */
23203 cp_lexer_consume_token (parser->lexer);
23204 /* Peek at the next token. */
23205 token = cp_lexer_peek_token (parser->lexer);
23206 /* If the next token is `]', then there is no
23207 constant-expression. */
23208 if (token->type != CPP_CLOSE_SQUARE)
23210 bool non_constant_p;
23211 bounds
23212 = cp_parser_constant_expression (parser,
23213 /*allow_non_constant=*/true,
23214 &non_constant_p);
23215 if (!non_constant_p)
23216 /* OK */;
23217 else if (error_operand_p (bounds))
23218 /* Already gave an error. */;
23219 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23220 /* Let compute_array_index_type diagnose this. */;
23221 else if (!parser->in_function_body
23222 || parsing_function_declarator ())
23224 /* Normally, the array bound must be an integral constant
23225 expression. However, as an extension, we allow VLAs
23226 in function scopes as long as they aren't part of a
23227 parameter declaration. */
23228 cp_parser_error (parser,
23229 "array bound is not an integer constant");
23230 bounds = error_mark_node;
23232 else if (processing_template_decl
23233 && !type_dependent_expression_p (bounds))
23235 /* Remember this wasn't a constant-expression. */
23236 bounds = build_nop (TREE_TYPE (bounds), bounds);
23237 TREE_SIDE_EFFECTS (bounds) = 1;
23240 else
23241 bounds = NULL_TREE;
23242 /* Look for the closing `]'. */
23243 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23245 declarator = cp_error_declarator;
23246 break;
23249 attrs = cp_parser_std_attribute_spec_seq (parser);
23250 declarator = make_array_declarator (declarator, bounds);
23251 declarator->std_attributes = attrs;
23253 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23256 tree qualifying_scope;
23257 tree unqualified_name;
23258 tree attrs;
23259 special_function_kind sfk;
23260 bool abstract_ok;
23261 bool pack_expansion_p = false;
23262 cp_token *declarator_id_start_token;
23264 /* Parse a declarator-id */
23265 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23266 if (abstract_ok)
23268 cp_parser_parse_tentatively (parser);
23270 /* If we see an ellipsis, we should be looking at a
23271 parameter pack. */
23272 if (token->type == CPP_ELLIPSIS)
23274 /* Consume the `...' */
23275 cp_lexer_consume_token (parser->lexer);
23277 pack_expansion_p = true;
23281 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23282 unqualified_name
23283 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23284 qualifying_scope = parser->scope;
23285 if (abstract_ok)
23287 bool okay = false;
23289 if (!unqualified_name && pack_expansion_p)
23291 /* Check whether an error occurred. */
23292 okay = !cp_parser_error_occurred (parser);
23294 /* We already consumed the ellipsis to mark a
23295 parameter pack, but we have no way to report it,
23296 so abort the tentative parse. We will be exiting
23297 immediately anyway. */
23298 cp_parser_abort_tentative_parse (parser);
23300 else
23301 okay = cp_parser_parse_definitely (parser);
23303 if (!okay)
23304 unqualified_name = error_mark_node;
23305 else if (unqualified_name
23306 && (qualifying_scope
23307 || (!identifier_p (unqualified_name))))
23309 cp_parser_error (parser, "expected unqualified-id");
23310 unqualified_name = error_mark_node;
23314 if (!unqualified_name)
23315 return NULL;
23316 if (unqualified_name == error_mark_node)
23318 declarator = cp_error_declarator;
23319 pack_expansion_p = false;
23320 declarator->parameter_pack_p = false;
23321 break;
23324 attrs = cp_parser_std_attribute_spec_seq (parser);
23326 if (qualifying_scope && at_namespace_scope_p ()
23327 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23329 /* In the declaration of a member of a template class
23330 outside of the class itself, the SCOPE will sometimes
23331 be a TYPENAME_TYPE. For example, given:
23333 template <typename T>
23334 int S<T>::R::i = 3;
23336 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23337 this context, we must resolve S<T>::R to an ordinary
23338 type, rather than a typename type.
23340 The reason we normally avoid resolving TYPENAME_TYPEs
23341 is that a specialization of `S' might render
23342 `S<T>::R' not a type. However, if `S' is
23343 specialized, then this `i' will not be used, so there
23344 is no harm in resolving the types here. */
23345 tree type;
23347 /* Resolve the TYPENAME_TYPE. */
23348 type = resolve_typename_type (qualifying_scope,
23349 /*only_current_p=*/false);
23350 /* If that failed, the declarator is invalid. */
23351 if (TREE_CODE (type) == TYPENAME_TYPE)
23353 if (typedef_variant_p (type))
23354 error_at (declarator_id_start_token->location,
23355 "cannot define member of dependent typedef "
23356 "%qT", type);
23357 else
23358 error_at (declarator_id_start_token->location,
23359 "%<%T::%E%> is not a type",
23360 TYPE_CONTEXT (qualifying_scope),
23361 TYPE_IDENTIFIER (qualifying_scope));
23363 qualifying_scope = type;
23366 sfk = sfk_none;
23368 if (unqualified_name)
23370 tree class_type;
23372 if (qualifying_scope
23373 && CLASS_TYPE_P (qualifying_scope))
23374 class_type = qualifying_scope;
23375 else
23376 class_type = current_class_type;
23378 if (TREE_CODE (unqualified_name) == TYPE_DECL)
23380 tree name_type = TREE_TYPE (unqualified_name);
23382 if (!class_type || !same_type_p (name_type, class_type))
23384 /* We do not attempt to print the declarator
23385 here because we do not have enough
23386 information about its original syntactic
23387 form. */
23388 cp_parser_error (parser, "invalid declarator");
23389 declarator = cp_error_declarator;
23390 break;
23392 else if (qualifying_scope
23393 && CLASSTYPE_USE_TEMPLATE (name_type))
23395 error_at (declarator_id_start_token->location,
23396 "invalid use of constructor as a template");
23397 inform (declarator_id_start_token->location,
23398 "use %<%T::%D%> instead of %<%T::%D%> to "
23399 "name the constructor in a qualified name",
23400 class_type,
23401 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23402 class_type, name_type);
23403 declarator = cp_error_declarator;
23404 break;
23406 unqualified_name = constructor_name (class_type);
23409 if (class_type)
23411 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23412 sfk = sfk_destructor;
23413 else if (identifier_p (unqualified_name)
23414 && IDENTIFIER_CONV_OP_P (unqualified_name))
23415 sfk = sfk_conversion;
23416 else if (/* There's no way to declare a constructor
23417 for an unnamed type, even if the type
23418 got a name for linkage purposes. */
23419 !TYPE_WAS_UNNAMED (class_type)
23420 /* Handle correctly (c++/19200):
23422 struct S {
23423 struct T{};
23424 friend void S(T);
23427 and also:
23429 namespace N {
23430 void S();
23433 struct S {
23434 friend void N::S();
23435 }; */
23436 && (!friend_p || class_type == qualifying_scope)
23437 && constructor_name_p (unqualified_name,
23438 class_type))
23439 sfk = sfk_constructor;
23440 else if (is_overloaded_fn (unqualified_name)
23441 && DECL_CONSTRUCTOR_P (get_first_fn
23442 (unqualified_name)))
23443 sfk = sfk_constructor;
23445 if (ctor_dtor_or_conv_p && sfk != sfk_none)
23446 *ctor_dtor_or_conv_p = -1;
23449 declarator = make_id_declarator (qualifying_scope,
23450 unqualified_name,
23451 sfk, token->location);
23452 declarator->std_attributes = attrs;
23453 declarator->parameter_pack_p = pack_expansion_p;
23455 if (pack_expansion_p)
23456 maybe_warn_variadic_templates ();
23458 /* We're looking for this case in [temp.res]:
23459 A qualified-id is assumed to name a type if [...]
23460 - it is a decl-specifier of the decl-specifier-seq of a
23461 parameter-declaration in a declarator of a function or
23462 function template declaration, ... */
23463 if (cxx_dialect >= cxx20
23464 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
23465 && declarator->kind == cdk_id
23466 && !at_class_scope_p ()
23467 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23469 /* ...whose declarator-id is qualified. If it isn't, never
23470 assume the parameters to refer to types. */
23471 if (qualifying_scope == NULL_TREE)
23472 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23473 else
23475 /* Now we have something like
23476 template <typename T> int C::x(S::p);
23477 which can be a function template declaration or a
23478 variable template definition. If name lookup for
23479 the declarator-id C::x finds one or more function
23480 templates, assume S::p to name a type. Otherwise,
23481 don't. */
23482 tree decl
23483 = cp_parser_lookup_name (parser, unqualified_name,
23484 none_type,
23485 /*is_template=*/false,
23486 /*is_namespace=*/false,
23487 /*check_dependency=*/false,
23488 /*ambiguous_decls=*/NULL,
23489 token->location);
23491 if (!is_overloaded_fn (decl)
23492 /* Allow
23493 template<typename T>
23494 A<T>::A(T::type) { } */
23495 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
23496 && constructor_name_p (unqualified_name,
23497 qualifying_scope)))
23498 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23503 handle_declarator:;
23504 scope = get_scope_of_declarator (declarator);
23505 if (scope)
23507 /* Any names that appear after the declarator-id for a
23508 member are looked up in the containing scope. */
23509 if (at_function_scope_p ())
23511 /* But declarations with qualified-ids can't appear in a
23512 function. */
23513 cp_parser_error (parser, "qualified-id in declaration");
23514 declarator = cp_error_declarator;
23515 break;
23517 pushed_scope = push_scope (scope);
23519 parser->in_declarator_p = true;
23520 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
23521 || (declarator && declarator->kind == cdk_id))
23522 /* Default args are only allowed on function
23523 declarations. */
23524 parser->default_arg_ok_p = saved_default_arg_ok_p;
23525 else
23526 parser->default_arg_ok_p = false;
23528 first = false;
23530 /* We're done. */
23531 else
23532 break;
23535 /* For an abstract declarator, we might wind up with nothing at this
23536 point. That's an error; the declarator is not optional. */
23537 if (!declarator)
23538 cp_parser_error (parser, "expected declarator");
23539 else if (open_paren)
23541 /* Record overly parenthesized declarator so we can give a
23542 diagnostic about confusing decl/expr disambiguation. */
23543 if (declarator->kind == cdk_array)
23545 /* If the open and close parens are on different lines, this
23546 is probably a formatting thing, so ignore. */
23547 expanded_location open = expand_location (open_paren->location);
23548 expanded_location close = expand_location (close_paren->location);
23549 if (open.line != close.line || open.file != close.file)
23550 open_paren = NULL;
23552 if (open_paren)
23553 declarator->parenthesized = make_location (open_paren->location,
23554 open_paren->location,
23555 close_paren->location);
23558 /* If we entered a scope, we must exit it now. */
23559 if (pushed_scope)
23560 pop_scope (pushed_scope);
23562 parser->default_arg_ok_p = saved_default_arg_ok_p;
23563 parser->in_declarator_p = saved_in_declarator_p;
23565 return declarator;
23568 /* Parse a ptr-operator.
23570 ptr-operator:
23571 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23572 * cv-qualifier-seq [opt]
23574 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23575 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23577 GNU Extension:
23579 ptr-operator:
23580 & cv-qualifier-seq [opt]
23582 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23583 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23584 an rvalue reference. In the case of a pointer-to-member, *TYPE is
23585 filled in with the TYPE containing the member. *CV_QUALS is
23586 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23587 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
23588 Note that the tree codes returned by this function have nothing
23589 to do with the types of trees that will be eventually be created
23590 to represent the pointer or reference type being parsed. They are
23591 just constants with suggestive names. */
23592 static enum tree_code
23593 cp_parser_ptr_operator (cp_parser* parser,
23594 tree* type,
23595 cp_cv_quals *cv_quals,
23596 tree *attributes)
23598 enum tree_code code = ERROR_MARK;
23599 cp_token *token;
23600 tree attrs = NULL_TREE;
23602 /* Assume that it's not a pointer-to-member. */
23603 *type = NULL_TREE;
23604 /* And that there are no cv-qualifiers. */
23605 *cv_quals = TYPE_UNQUALIFIED;
23607 /* Peek at the next token. */
23608 token = cp_lexer_peek_token (parser->lexer);
23610 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
23611 if (token->type == CPP_MULT)
23612 code = INDIRECT_REF;
23613 else if (token->type == CPP_AND)
23614 code = ADDR_EXPR;
23615 else if ((cxx_dialect != cxx98) &&
23616 token->type == CPP_AND_AND) /* C++0x only */
23617 code = NON_LVALUE_EXPR;
23619 if (code != ERROR_MARK)
23621 /* Consume the `*', `&' or `&&'. */
23622 cp_lexer_consume_token (parser->lexer);
23624 /* A `*' can be followed by a cv-qualifier-seq, and so can a
23625 `&', if we are allowing GNU extensions. (The only qualifier
23626 that can legally appear after `&' is `restrict', but that is
23627 enforced during semantic analysis. */
23628 if (code == INDIRECT_REF
23629 || cp_parser_allow_gnu_extensions_p (parser))
23630 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23632 attrs = cp_parser_std_attribute_spec_seq (parser);
23633 if (attributes != NULL)
23634 *attributes = attrs;
23636 else
23638 /* Try the pointer-to-member case. */
23639 cp_parser_parse_tentatively (parser);
23640 /* Look for the optional `::' operator. */
23641 cp_parser_global_scope_opt (parser,
23642 /*current_scope_valid_p=*/false);
23643 /* Look for the nested-name specifier. */
23644 token = cp_lexer_peek_token (parser->lexer);
23645 cp_parser_nested_name_specifier (parser,
23646 /*typename_keyword_p=*/false,
23647 /*check_dependency_p=*/true,
23648 /*type_p=*/false,
23649 /*is_declaration=*/false);
23650 /* If we found it, and the next token is a `*', then we are
23651 indeed looking at a pointer-to-member operator. */
23652 if (!cp_parser_error_occurred (parser)
23653 && cp_parser_require (parser, CPP_MULT, RT_MULT))
23655 /* Indicate that the `*' operator was used. */
23656 code = INDIRECT_REF;
23658 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
23659 error_at (token->location, "%qD is a namespace", parser->scope);
23660 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
23661 error_at (token->location, "cannot form pointer to member of "
23662 "non-class %q#T", parser->scope);
23663 else
23665 /* The type of which the member is a member is given by the
23666 current SCOPE. */
23667 *type = parser->scope;
23668 /* The next name will not be qualified. */
23669 parser->scope = NULL_TREE;
23670 parser->qualifying_scope = NULL_TREE;
23671 parser->object_scope = NULL_TREE;
23672 /* Look for optional c++11 attributes. */
23673 attrs = cp_parser_std_attribute_spec_seq (parser);
23674 if (attributes != NULL)
23675 *attributes = attrs;
23676 /* Look for the optional cv-qualifier-seq. */
23677 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23680 /* If that didn't work we don't have a ptr-operator. */
23681 if (!cp_parser_parse_definitely (parser))
23682 cp_parser_error (parser, "expected ptr-operator");
23685 return code;
23688 /* Parse an (optional) cv-qualifier-seq.
23690 cv-qualifier-seq:
23691 cv-qualifier cv-qualifier-seq [opt]
23693 cv-qualifier:
23694 const
23695 volatile
23697 GNU Extension:
23699 cv-qualifier:
23700 __restrict__
23702 Returns a bitmask representing the cv-qualifiers. */
23704 static cp_cv_quals
23705 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
23707 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
23709 while (true)
23711 cp_token *token;
23712 cp_cv_quals cv_qualifier;
23714 /* Peek at the next token. */
23715 token = cp_lexer_peek_token (parser->lexer);
23716 /* See if it's a cv-qualifier. */
23717 switch (token->keyword)
23719 case RID_CONST:
23720 cv_qualifier = TYPE_QUAL_CONST;
23721 break;
23723 case RID_VOLATILE:
23724 cv_qualifier = TYPE_QUAL_VOLATILE;
23725 break;
23727 case RID_RESTRICT:
23728 cv_qualifier = TYPE_QUAL_RESTRICT;
23729 break;
23731 default:
23732 cv_qualifier = TYPE_UNQUALIFIED;
23733 break;
23736 if (!cv_qualifier)
23737 break;
23739 if (cv_quals & cv_qualifier)
23741 gcc_rich_location richloc (token->location);
23742 richloc.add_fixit_remove ();
23743 error_at (&richloc, "duplicate cv-qualifier");
23744 cp_lexer_purge_token (parser->lexer);
23746 else
23748 cp_lexer_consume_token (parser->lexer);
23749 cv_quals |= cv_qualifier;
23753 return cv_quals;
23756 /* Parse an (optional) ref-qualifier
23758 ref-qualifier:
23762 Returns cp_ref_qualifier representing ref-qualifier. */
23764 static cp_ref_qualifier
23765 cp_parser_ref_qualifier_opt (cp_parser* parser)
23767 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
23769 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
23770 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
23771 return ref_qual;
23773 while (true)
23775 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
23776 cp_token *token = cp_lexer_peek_token (parser->lexer);
23778 switch (token->type)
23780 case CPP_AND:
23781 curr_ref_qual = REF_QUAL_LVALUE;
23782 break;
23784 case CPP_AND_AND:
23785 curr_ref_qual = REF_QUAL_RVALUE;
23786 break;
23788 default:
23789 curr_ref_qual = REF_QUAL_NONE;
23790 break;
23793 if (!curr_ref_qual)
23794 break;
23795 else if (ref_qual)
23797 error_at (token->location, "multiple ref-qualifiers");
23798 cp_lexer_purge_token (parser->lexer);
23800 else
23802 ref_qual = curr_ref_qual;
23803 cp_lexer_consume_token (parser->lexer);
23807 return ref_qual;
23810 /* Parse an optional tx-qualifier.
23812 tx-qualifier:
23813 transaction_safe
23814 transaction_safe_dynamic */
23816 static tree
23817 cp_parser_tx_qualifier_opt (cp_parser *parser)
23819 cp_token *token = cp_lexer_peek_token (parser->lexer);
23820 if (token->type == CPP_NAME)
23822 tree name = token->u.value;
23823 const char *p = IDENTIFIER_POINTER (name);
23824 const int len = strlen ("transaction_safe");
23825 if (startswith (p, "transaction_safe"))
23827 p += len;
23828 if (*p == '\0'
23829 || !strcmp (p, "_dynamic"))
23831 cp_lexer_consume_token (parser->lexer);
23832 if (!flag_tm)
23834 error ("%qE requires %<-fgnu-tm%>", name);
23835 return NULL_TREE;
23837 else
23838 return name;
23842 return NULL_TREE;
23845 /* Parse an (optional) virt-specifier-seq.
23847 virt-specifier-seq:
23848 virt-specifier virt-specifier-seq [opt]
23850 virt-specifier:
23851 override
23852 final
23854 Returns a bitmask representing the virt-specifiers. */
23856 static cp_virt_specifiers
23857 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
23859 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23861 while (true)
23863 cp_token *token;
23864 cp_virt_specifiers virt_specifier;
23866 /* Peek at the next token. */
23867 token = cp_lexer_peek_token (parser->lexer);
23868 /* See if it's a virt-specifier-qualifier. */
23869 if (token->type != CPP_NAME)
23870 break;
23871 if (id_equal (token->u.value, "override"))
23873 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
23874 virt_specifier = VIRT_SPEC_OVERRIDE;
23876 else if (id_equal (token->u.value, "final"))
23878 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
23879 virt_specifier = VIRT_SPEC_FINAL;
23881 else if (id_equal (token->u.value, "__final"))
23883 virt_specifier = VIRT_SPEC_FINAL;
23885 else
23886 break;
23888 if (virt_specifiers & virt_specifier)
23890 gcc_rich_location richloc (token->location);
23891 richloc.add_fixit_remove ();
23892 error_at (&richloc, "duplicate virt-specifier");
23893 cp_lexer_purge_token (parser->lexer);
23895 else
23897 cp_lexer_consume_token (parser->lexer);
23898 virt_specifiers |= virt_specifier;
23901 return virt_specifiers;
23904 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
23905 is in scope even though it isn't real. */
23907 void
23908 inject_this_parameter (tree ctype, cp_cv_quals quals)
23910 tree this_parm;
23912 if (current_class_ptr)
23914 /* We don't clear this between NSDMIs. Is it already what we want? */
23915 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
23916 if (DECL_P (current_class_ptr)
23917 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
23918 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
23919 && cp_type_quals (type) == quals)
23920 return;
23923 this_parm = build_this_parm (NULL_TREE, ctype, quals);
23924 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
23925 current_class_ptr = NULL_TREE;
23926 current_class_ref
23927 = cp_build_fold_indirect_ref (this_parm);
23928 current_class_ptr = this_parm;
23931 /* Return true iff our current scope is a non-static data member
23932 initializer. */
23934 bool
23935 parsing_nsdmi (void)
23937 /* We recognize NSDMI context by the context-less 'this' pointer set up
23938 by the function above. */
23939 if (current_class_ptr
23940 && TREE_CODE (current_class_ptr) == PARM_DECL
23941 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
23942 return true;
23943 return false;
23946 /* True if we're parsing a function declarator. */
23948 bool
23949 parsing_function_declarator ()
23951 /* this_entity is NULL for a function parameter scope while parsing the
23952 declarator; it is set when parsing the body of the function. */
23953 return (current_binding_level->kind == sk_function_parms
23954 && !current_binding_level->this_entity);
23957 /* Parse a late-specified return type, if any. This is not a separate
23958 non-terminal, but part of a function declarator, which looks like
23960 -> trailing-type-specifier-seq abstract-declarator(opt)
23962 Returns the type indicated by the type-id.
23964 In addition to this, parse any queued up #pragma omp declare simd
23965 clauses, and #pragma acc routine clauses.
23967 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
23968 function. */
23970 static tree
23971 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
23972 tree& requires_clause)
23974 cp_token *token;
23975 tree type = NULL_TREE;
23976 bool declare_simd_p = (parser->omp_declare_simd
23977 && declarator
23978 && declarator->kind == cdk_id);
23980 bool oacc_routine_p = (parser->oacc_routine
23981 && declarator
23982 && declarator->kind == cdk_id);
23984 /* Peek at the next token. */
23985 token = cp_lexer_peek_token (parser->lexer);
23986 /* A late-specified return type is indicated by an initial '->'. */
23987 if (token->type != CPP_DEREF
23988 && token->keyword != RID_REQUIRES
23989 && !(token->type == CPP_NAME
23990 && token->u.value == ridpointers[RID_REQUIRES])
23991 && !(declare_simd_p || oacc_routine_p))
23992 return NULL_TREE;
23994 if (token->type == CPP_DEREF)
23996 /* Consume the ->. */
23997 cp_lexer_consume_token (parser->lexer);
23999 type = cp_parser_trailing_type_id (parser);
24002 /* Function declarations may be followed by a trailing
24003 requires-clause. */
24004 requires_clause = cp_parser_requires_clause_opt (parser, false);
24006 if (declare_simd_p)
24007 declarator->attributes
24008 = cp_parser_late_parsing_omp_declare_simd (parser,
24009 declarator->attributes);
24010 if (oacc_routine_p)
24011 declarator->attributes
24012 = cp_parser_late_parsing_oacc_routine (parser,
24013 declarator->attributes);
24015 return type;
24018 /* Parse a declarator-id.
24020 declarator-id:
24021 id-expression
24022 :: [opt] nested-name-specifier [opt] type-name
24024 In the `id-expression' case, the value returned is as for
24025 cp_parser_id_expression if the id-expression was an unqualified-id.
24026 If the id-expression was a qualified-id, then a SCOPE_REF is
24027 returned. The first operand is the scope (either a NAMESPACE_DECL
24028 or TREE_TYPE), but the second is still just a representation of an
24029 unqualified-id. */
24031 static tree
24032 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24034 tree id;
24035 /* The expression must be an id-expression. Assume that qualified
24036 names are the names of types so that:
24038 template <class T>
24039 int S<T>::R::i = 3;
24041 will work; we must treat `S<T>::R' as the name of a type.
24042 Similarly, assume that qualified names are templates, where
24043 required, so that:
24045 template <class T>
24046 int S<T>::R<T>::i = 3;
24048 will work, too. */
24049 id = cp_parser_id_expression (parser,
24050 /*template_keyword_p=*/false,
24051 /*check_dependency_p=*/false,
24052 /*template_p=*/NULL,
24053 /*declarator_p=*/true,
24054 optional_p);
24055 if (id && BASELINK_P (id))
24056 id = BASELINK_FUNCTIONS (id);
24057 return id;
24060 /* Parse a type-id.
24062 type-id:
24063 type-specifier-seq abstract-declarator [opt]
24065 The parser flags FLAGS is used to control type-specifier parsing.
24067 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24069 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24070 i.e. we've just seen "->".
24072 Returns the TYPE specified. */
24074 static tree
24075 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24076 bool is_template_arg, bool is_trailing_return,
24077 location_t *type_location)
24079 cp_decl_specifier_seq type_specifier_seq;
24080 cp_declarator *abstract_declarator;
24082 /* Parse the type-specifier-seq. */
24083 cp_parser_type_specifier_seq (parser, flags,
24084 /*is_declaration=*/false,
24085 is_trailing_return,
24086 &type_specifier_seq);
24087 if (type_location)
24088 *type_location = type_specifier_seq.locations[ds_type_spec];
24090 if (is_template_arg && type_specifier_seq.type
24091 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24092 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24093 /* A bare template name as a template argument is a template template
24094 argument, not a placeholder, so fail parsing it as a type argument. */
24096 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24097 cp_parser_simulate_error (parser);
24098 return error_mark_node;
24100 if (type_specifier_seq.type == error_mark_node)
24101 return error_mark_node;
24103 /* There might or might not be an abstract declarator. */
24104 cp_parser_parse_tentatively (parser);
24105 /* Look for the declarator. */
24106 abstract_declarator
24107 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24108 CP_PARSER_FLAGS_NONE, NULL,
24109 /*parenthesized_p=*/NULL,
24110 /*member_p=*/false,
24111 /*friend_p=*/false,
24112 /*static_p=*/false);
24113 /* Check to see if there really was a declarator. */
24114 if (!cp_parser_parse_definitely (parser))
24115 abstract_declarator = NULL;
24117 bool auto_typeid_ok = false;
24118 /* The concepts TS allows 'auto' as a type-id. */
24119 if (flag_concepts_ts)
24120 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24121 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24122 outside the template-argument-list context here only for the sake of
24123 diagnostic: grokdeclarator then can emit a better error message for
24124 e.g. using T = auto. */
24125 else if (flag_concepts)
24126 auto_typeid_ok = (!parser->in_type_id_in_expr_p
24127 && !parser->in_template_argument_list_p);
24129 if (type_specifier_seq.type
24130 && !auto_typeid_ok
24131 /* None of the valid uses of 'auto' in C++14 involve the type-id
24132 nonterminal, but it is valid in a trailing-return-type. */
24133 && !(cxx_dialect >= cxx14 && is_trailing_return))
24134 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24136 /* A type-id with type 'auto' is only ok if the abstract declarator
24137 is a function declarator with a late-specified return type.
24139 A type-id with 'auto' is also valid in a trailing-return-type
24140 in a compound-requirement. */
24141 if (abstract_declarator
24142 && abstract_declarator->kind == cdk_function
24143 && abstract_declarator->u.function.late_return_type)
24144 /* OK */;
24145 else if (parser->in_result_type_constraint_p)
24146 /* OK */;
24147 else
24149 location_t loc = type_specifier_seq.locations[ds_type_spec];
24150 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24152 if (!cp_parser_simulate_error (parser))
24154 error_at (loc, "missing template arguments after %qT",
24155 auto_node);
24156 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24157 tmpl);
24160 else if (parser->in_template_argument_list_p)
24161 error_at (loc, "%qT not permitted in template argument",
24162 auto_node);
24163 else
24164 error_at (loc, "invalid use of %qT", auto_node);
24165 return error_mark_node;
24169 return groktypename (&type_specifier_seq, abstract_declarator,
24170 is_template_arg);
24173 /* Wrapper for cp_parser_type_id_1. */
24175 static tree
24176 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24177 location_t *type_location)
24179 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24182 /* Wrapper for cp_parser_type_id_1. */
24184 static tree
24185 cp_parser_template_type_arg (cp_parser *parser)
24187 tree r;
24188 const char *saved_message = parser->type_definition_forbidden_message;
24189 parser->type_definition_forbidden_message
24190 = G_("types may not be defined in template arguments");
24191 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24192 parser->type_definition_forbidden_message = saved_message;
24193 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
24195 error ("invalid use of %<auto%> in template argument");
24196 r = error_mark_node;
24198 return r;
24201 /* Wrapper for cp_parser_type_id_1. */
24203 static tree
24204 cp_parser_trailing_type_id (cp_parser *parser)
24206 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24207 false, true, NULL);
24210 /* Parse a type-specifier-seq.
24212 type-specifier-seq:
24213 type-specifier type-specifier-seq [opt]
24215 GNU extension:
24217 type-specifier-seq:
24218 attributes type-specifier-seq [opt]
24220 The parser flags FLAGS is used to control type-specifier parsing.
24222 If IS_DECLARATION is true, we are at the start of a "condition" or
24223 exception-declaration, so we might be followed by a declarator-id.
24225 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24226 i.e. we've just seen "->".
24228 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24230 static void
24231 cp_parser_type_specifier_seq (cp_parser* parser,
24232 cp_parser_flags flags,
24233 bool is_declaration,
24234 bool is_trailing_return,
24235 cp_decl_specifier_seq *type_specifier_seq)
24237 bool seen_type_specifier = false;
24238 cp_token *start_token = NULL;
24240 /* Clear the TYPE_SPECIFIER_SEQ. */
24241 clear_decl_specs (type_specifier_seq);
24243 flags |= CP_PARSER_FLAGS_OPTIONAL;
24244 /* In the context of a trailing return type, enum E { } is an
24245 elaborated-type-specifier followed by a function-body, not an
24246 enum-specifier. */
24247 if (is_trailing_return)
24248 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24250 /* Parse the type-specifiers and attributes. */
24251 while (true)
24253 tree type_specifier;
24254 bool is_cv_qualifier;
24256 /* Check for attributes first. */
24257 if (cp_next_tokens_can_be_attribute_p (parser))
24259 /* GNU attributes at the end of a declaration apply to the
24260 declaration as a whole, not to the trailing return type. So look
24261 ahead to see if these attributes are at the end. */
24262 if (seen_type_specifier && is_trailing_return
24263 && cp_next_tokens_can_be_gnu_attribute_p (parser))
24265 size_t n = cp_parser_skip_attributes_opt (parser, 1);
24266 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24267 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24268 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24269 break;
24271 type_specifier_seq->attributes
24272 = attr_chainon (type_specifier_seq->attributes,
24273 cp_parser_attributes_opt (parser));
24274 continue;
24277 /* record the token of the beginning of the type specifier seq,
24278 for error reporting purposes*/
24279 if (!start_token)
24280 start_token = cp_lexer_peek_token (parser->lexer);
24282 /* Look for the type-specifier. */
24283 type_specifier = cp_parser_type_specifier (parser,
24284 flags,
24285 type_specifier_seq,
24286 /*is_declaration=*/false,
24287 NULL,
24288 &is_cv_qualifier);
24289 if (!type_specifier)
24291 /* If the first type-specifier could not be found, this is not a
24292 type-specifier-seq at all. */
24293 if (!seen_type_specifier)
24295 /* Set in_declarator_p to avoid skipping to the semicolon. */
24296 int in_decl = parser->in_declarator_p;
24297 parser->in_declarator_p = true;
24299 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24300 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24301 cp_parser_error (parser, "expected type-specifier");
24303 parser->in_declarator_p = in_decl;
24305 type_specifier_seq->type = error_mark_node;
24306 return;
24308 /* If subsequent type-specifiers could not be found, the
24309 type-specifier-seq is complete. */
24310 break;
24313 seen_type_specifier = true;
24314 /* The standard says that a condition can be:
24316 type-specifier-seq declarator = assignment-expression
24318 However, given:
24320 struct S {};
24321 if (int S = ...)
24323 we should treat the "S" as a declarator, not as a
24324 type-specifier. The standard doesn't say that explicitly for
24325 type-specifier-seq, but it does say that for
24326 decl-specifier-seq in an ordinary declaration. Perhaps it
24327 would be clearer just to allow a decl-specifier-seq here, and
24328 then add a semantic restriction that if any decl-specifiers
24329 that are not type-specifiers appear, the program is invalid. */
24330 if (is_declaration && !is_cv_qualifier)
24331 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24335 /* Return whether the function currently being declared has an associated
24336 template parameter list. */
24338 static bool
24339 function_being_declared_is_template_p (cp_parser* parser)
24341 if (!current_template_parms || processing_template_parmlist)
24342 return false;
24344 if (parser->implicit_template_scope)
24345 return true;
24347 if (at_class_scope_p ()
24348 && TYPE_BEING_DEFINED (current_class_type))
24349 return parser->num_template_parameter_lists != 0;
24351 return ((int) parser->num_template_parameter_lists > template_class_depth
24352 (current_class_type));
24355 /* Parse a parameter-declaration-clause.
24357 parameter-declaration-clause:
24358 parameter-declaration-list [opt] ... [opt]
24359 parameter-declaration-list , ...
24361 The parser flags FLAGS is used to control type-specifier parsing.
24363 Returns a representation for the parameter declarations. A return
24364 value of NULL indicates a parameter-declaration-clause consisting
24365 only of an ellipsis. */
24367 static tree
24368 cp_parser_parameter_declaration_clause (cp_parser* parser,
24369 cp_parser_flags flags)
24371 tree parameters;
24372 cp_token *token;
24373 bool ellipsis_p;
24375 auto cleanup = make_temp_override
24376 (parser->auto_is_implicit_function_template_parm_p);
24378 if (!processing_specialization
24379 && !processing_template_parmlist
24380 && !processing_explicit_instantiation
24381 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24382 actual function or a random abstract declarator. */
24383 && parser->default_arg_ok_p)
24384 if (!current_function_decl
24385 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24386 parser->auto_is_implicit_function_template_parm_p = true;
24388 /* Peek at the next token. */
24389 token = cp_lexer_peek_token (parser->lexer);
24390 /* Check for trivial parameter-declaration-clauses. */
24391 if (token->type == CPP_ELLIPSIS)
24393 /* Consume the `...' token. */
24394 cp_lexer_consume_token (parser->lexer);
24395 return NULL_TREE;
24397 else if (token->type == CPP_CLOSE_PAREN)
24398 /* There are no parameters. */
24399 return void_list_node;
24400 /* Check for `(void)', too, which is a special case. */
24401 else if (token->keyword == RID_VOID
24402 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24403 == CPP_CLOSE_PAREN))
24405 /* Consume the `void' token. */
24406 cp_lexer_consume_token (parser->lexer);
24407 /* There are no parameters. */
24408 return explicit_void_list_node;
24411 /* Parse the parameter-declaration-list. */
24412 parameters = cp_parser_parameter_declaration_list (parser, flags);
24413 /* If a parse error occurred while parsing the
24414 parameter-declaration-list, then the entire
24415 parameter-declaration-clause is erroneous. */
24416 if (parameters == error_mark_node)
24417 return NULL_TREE;
24419 /* Peek at the next token. */
24420 token = cp_lexer_peek_token (parser->lexer);
24421 /* If it's a `,', the clause should terminate with an ellipsis. */
24422 if (token->type == CPP_COMMA)
24424 /* Consume the `,'. */
24425 cp_lexer_consume_token (parser->lexer);
24426 /* Expect an ellipsis. */
24427 ellipsis_p
24428 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24430 /* It might also be `...' if the optional trailing `,' was
24431 omitted. */
24432 else if (token->type == CPP_ELLIPSIS)
24434 /* Consume the `...' token. */
24435 cp_lexer_consume_token (parser->lexer);
24436 /* And remember that we saw it. */
24437 ellipsis_p = true;
24439 else
24440 ellipsis_p = false;
24442 /* Finish the parameter list. */
24443 if (!ellipsis_p)
24444 parameters = chainon (parameters, void_list_node);
24446 return parameters;
24449 /* Parse a parameter-declaration-list.
24451 parameter-declaration-list:
24452 parameter-declaration
24453 parameter-declaration-list , parameter-declaration
24455 The parser flags FLAGS is used to control type-specifier parsing.
24457 Returns a representation of the parameter-declaration-list, as for
24458 cp_parser_parameter_declaration_clause. However, the
24459 `void_list_node' is never appended to the list. */
24461 static tree
24462 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
24464 tree parameters = NULL_TREE;
24465 tree *tail = &parameters;
24466 bool saved_in_unbraced_linkage_specification_p;
24467 int index = 0;
24469 /* The special considerations that apply to a function within an
24470 unbraced linkage specifications do not apply to the parameters
24471 to the function. */
24472 saved_in_unbraced_linkage_specification_p
24473 = parser->in_unbraced_linkage_specification_p;
24474 parser->in_unbraced_linkage_specification_p = false;
24476 /* Look for more parameters. */
24477 while (true)
24479 cp_parameter_declarator *parameter;
24480 tree decl = error_mark_node;
24481 bool parenthesized_p = false;
24483 /* Parse the parameter. */
24484 parameter
24485 = cp_parser_parameter_declaration (parser, flags,
24486 /*template_parm_p=*/false,
24487 &parenthesized_p);
24489 /* We don't know yet if the enclosing context is unavailable or deprecated,
24490 so wait and deal with it in grokparms if appropriate. */
24491 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
24493 if (parameter && !cp_parser_error_occurred (parser))
24495 decl = grokdeclarator (parameter->declarator,
24496 &parameter->decl_specifiers,
24497 PARM,
24498 parameter->default_argument != NULL_TREE,
24499 &parameter->decl_specifiers.attributes);
24500 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
24501 DECL_SOURCE_LOCATION (decl) = parameter->loc;
24504 deprecated_state = DEPRECATED_NORMAL;
24506 /* If a parse error occurred parsing the parameter declaration,
24507 then the entire parameter-declaration-list is erroneous. */
24508 if (decl == error_mark_node)
24510 parameters = error_mark_node;
24511 break;
24514 if (parameter->decl_specifiers.attributes)
24515 cplus_decl_attributes (&decl,
24516 parameter->decl_specifiers.attributes,
24518 if (DECL_NAME (decl))
24519 decl = pushdecl (decl);
24521 if (decl != error_mark_node)
24523 retrofit_lang_decl (decl);
24524 DECL_PARM_INDEX (decl) = ++index;
24525 DECL_PARM_LEVEL (decl) = function_parm_depth ();
24528 /* Add the new parameter to the list. */
24529 *tail = build_tree_list (parameter->default_argument, decl);
24530 tail = &TREE_CHAIN (*tail);
24532 /* If the parameters were parenthesized, it's the case of
24533 T foo(X(x)) which looks like a variable definition but
24534 is a function declaration. */
24535 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
24536 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
24538 /* Peek at the next token. */
24539 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
24540 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
24541 /* These are for Objective-C++ */
24542 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24543 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24544 /* The parameter-declaration-list is complete. */
24545 break;
24546 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24548 cp_token *token;
24550 /* Peek at the next token. */
24551 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24552 /* If it's an ellipsis, then the list is complete. */
24553 if (token->type == CPP_ELLIPSIS)
24554 break;
24555 /* Otherwise, there must be more parameters. Consume the
24556 `,'. */
24557 cp_lexer_consume_token (parser->lexer);
24558 /* When parsing something like:
24560 int i(float f, double d)
24562 we can tell after seeing the declaration for "f" that we
24563 are not looking at an initialization of a variable "i",
24564 but rather at the declaration of a function "i".
24566 Due to the fact that the parsing of template arguments
24567 (as specified to a template-id) requires backtracking we
24568 cannot use this technique when inside a template argument
24569 list. */
24570 if (!parser->in_template_argument_list_p
24571 && !parser->in_type_id_in_expr_p
24572 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24573 /* However, a parameter-declaration of the form
24574 "float(f)" (which is a valid declaration of a
24575 parameter "f") can also be interpreted as an
24576 expression (the conversion of "f" to "float"). */
24577 && !parenthesized_p)
24578 cp_parser_commit_to_tentative_parse (parser);
24580 else
24582 cp_parser_error (parser, "expected %<,%> or %<...%>");
24583 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24584 cp_parser_skip_to_closing_parenthesis (parser,
24585 /*recovering=*/true,
24586 /*or_comma=*/false,
24587 /*consume_paren=*/false);
24588 break;
24592 parser->in_unbraced_linkage_specification_p
24593 = saved_in_unbraced_linkage_specification_p;
24595 /* Reset implicit_template_scope if we are about to leave the function
24596 parameter list that introduced it. Note that for out-of-line member
24597 definitions, there will be one or more class scopes before we get to
24598 the template parameter scope. */
24600 if (cp_binding_level *its = parser->implicit_template_scope)
24601 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
24603 while (maybe_its->kind == sk_class)
24604 maybe_its = maybe_its->level_chain;
24605 if (maybe_its == its)
24607 parser->implicit_template_parms = 0;
24608 parser->implicit_template_scope = 0;
24612 return parameters;
24615 /* Parse a parameter declaration.
24617 parameter-declaration:
24618 decl-specifier-seq ... [opt] declarator
24619 decl-specifier-seq declarator = assignment-expression
24620 decl-specifier-seq ... [opt] abstract-declarator [opt]
24621 decl-specifier-seq abstract-declarator [opt] = assignment-expression
24623 The parser flags FLAGS is used to control type-specifier parsing.
24625 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
24626 declares a template parameter. (In that case, a non-nested `>'
24627 token encountered during the parsing of the assignment-expression
24628 is not interpreted as a greater-than operator.)
24630 Returns a representation of the parameter, or NULL if an error
24631 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
24632 true iff the declarator is of the form "(p)". */
24634 static cp_parameter_declarator *
24635 cp_parser_parameter_declaration (cp_parser *parser,
24636 cp_parser_flags flags,
24637 bool template_parm_p,
24638 bool *parenthesized_p)
24640 int declares_class_or_enum;
24641 cp_decl_specifier_seq decl_specifiers;
24642 cp_declarator *declarator;
24643 tree default_argument;
24644 cp_token *token = NULL, *declarator_token_start = NULL;
24645 const char *saved_message;
24646 bool template_parameter_pack_p = false;
24648 /* In a template parameter, `>' is not an operator.
24650 [temp.param]
24652 When parsing a default template-argument for a non-type
24653 template-parameter, the first non-nested `>' is taken as the end
24654 of the template parameter-list rather than a greater-than
24655 operator. */
24657 /* Type definitions may not appear in parameter types. */
24658 saved_message = parser->type_definition_forbidden_message;
24659 parser->type_definition_forbidden_message
24660 = G_("types may not be defined in parameter types");
24662 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
24663 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
24664 (current_template_parms)) : 0);
24666 /* Parse the declaration-specifiers. */
24667 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24668 cp_parser_decl_specifier_seq (parser,
24669 flags,
24670 &decl_specifiers,
24671 &declares_class_or_enum);
24673 /* Complain about missing 'typename' or other invalid type names. */
24674 if (!decl_specifiers.any_type_specifiers_p
24675 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24676 decl_specifiers.type = error_mark_node;
24678 /* If an error occurred, there's no reason to attempt to parse the
24679 rest of the declaration. */
24680 if (cp_parser_error_occurred (parser))
24682 parser->type_definition_forbidden_message = saved_message;
24683 return NULL;
24686 /* Peek at the next token. */
24687 token = cp_lexer_peek_token (parser->lexer);
24689 /* If the next token is a `)', `,', `=', `>', or `...', then there
24690 is no declarator. However, when variadic templates are enabled,
24691 there may be a declarator following `...'. */
24692 if (token->type == CPP_CLOSE_PAREN
24693 || token->type == CPP_COMMA
24694 || token->type == CPP_EQ
24695 || token->type == CPP_GREATER)
24697 declarator = NULL;
24698 if (parenthesized_p)
24699 *parenthesized_p = false;
24701 /* Otherwise, there should be a declarator. */
24702 else
24704 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
24705 parser->default_arg_ok_p = false;
24707 /* After seeing a decl-specifier-seq, if the next token is not a
24708 "(" or "{", there is no possibility that the code is a valid
24709 expression. Therefore, if parsing tentatively, we commit at
24710 this point. */
24711 if (!parser->in_template_argument_list_p
24712 /* In an expression context, having seen:
24714 (int((char ...
24716 we cannot be sure whether we are looking at a
24717 function-type (taking a "char" as a parameter) or a cast
24718 of some object of type "char" to "int". */
24719 && !parser->in_type_id_in_expr_p
24720 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24721 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
24723 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24725 if (decl_specifiers.type
24726 && template_placeholder_p (decl_specifiers.type))
24727 /* This is a CTAD expression, not a parameter declaration. */
24728 cp_parser_simulate_error (parser);
24730 else
24731 cp_parser_commit_to_tentative_parse (parser);
24733 /* Parse the declarator. */
24734 declarator_token_start = token;
24735 declarator = cp_parser_declarator (parser,
24736 CP_PARSER_DECLARATOR_EITHER,
24737 CP_PARSER_FLAGS_NONE,
24738 /*ctor_dtor_or_conv_p=*/NULL,
24739 parenthesized_p,
24740 /*member_p=*/false,
24741 /*friend_p=*/false,
24742 /*static_p=*/false);
24743 parser->default_arg_ok_p = saved_default_arg_ok_p;
24744 /* After the declarator, allow more attributes. */
24745 decl_specifiers.attributes
24746 = attr_chainon (decl_specifiers.attributes,
24747 cp_parser_attributes_opt (parser));
24749 /* If the declarator is a template parameter pack, remember that and
24750 clear the flag in the declarator itself so we don't get errors
24751 from grokdeclarator. */
24752 if (template_parm_p && declarator && declarator->parameter_pack_p)
24754 declarator->parameter_pack_p = false;
24755 template_parameter_pack_p = true;
24759 /* If the next token is an ellipsis, and we have not seen a declarator
24760 name, and if either the type of the declarator contains parameter
24761 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
24762 for, eg, abbreviated integral type names), then we actually have a
24763 parameter pack expansion expression. Otherwise, leave the ellipsis
24764 for a C-style variadic function. */
24765 token = cp_lexer_peek_token (parser->lexer);
24767 /* If a function parameter pack was specified and an implicit template
24768 parameter was introduced during cp_parser_parameter_declaration,
24769 change any implicit parameters introduced into packs. */
24770 if (parser->implicit_template_parms
24771 && ((token->type == CPP_ELLIPSIS
24772 && declarator_can_be_parameter_pack (declarator))
24773 || (declarator && declarator->parameter_pack_p)))
24775 int latest_template_parm_idx = TREE_VEC_LENGTH
24776 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
24778 if (latest_template_parm_idx != template_parm_idx)
24779 decl_specifiers.type = convert_generic_types_to_packs
24780 (decl_specifiers.type,
24781 template_parm_idx, latest_template_parm_idx);
24784 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24786 tree type = decl_specifiers.type;
24788 if (type && DECL_P (type))
24789 type = TREE_TYPE (type);
24791 if (((type
24792 && TREE_CODE (type) != TYPE_PACK_EXPANSION
24793 && (template_parm_p || uses_parameter_packs (type)))
24794 || (!type && template_parm_p))
24795 && declarator_can_be_parameter_pack (declarator))
24797 /* Consume the `...'. */
24798 cp_lexer_consume_token (parser->lexer);
24799 maybe_warn_variadic_templates ();
24801 /* Build a pack expansion type */
24802 if (template_parm_p)
24803 template_parameter_pack_p = true;
24804 else if (declarator)
24805 declarator->parameter_pack_p = true;
24806 else
24807 decl_specifiers.type = make_pack_expansion (type);
24811 /* The restriction on defining new types applies only to the type
24812 of the parameter, not to the default argument. */
24813 parser->type_definition_forbidden_message = saved_message;
24815 /* If the next token is `=', then process a default argument. */
24816 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24818 tree type = decl_specifiers.type;
24819 token = cp_lexer_peek_token (parser->lexer);
24820 if (declarator)
24821 declarator->init_loc = token->location;
24822 /* If we are defining a class, then the tokens that make up the
24823 default argument must be saved and processed later. */
24824 if (!template_parm_p && at_class_scope_p ()
24825 && TYPE_BEING_DEFINED (current_class_type)
24826 && !LAMBDA_TYPE_P (current_class_type))
24827 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
24829 /* A constrained-type-specifier may declare a type
24830 template-parameter. */
24831 else if (declares_constrained_type_template_parameter (type))
24832 default_argument
24833 = cp_parser_default_type_template_argument (parser);
24835 /* A constrained-type-specifier may declare a
24836 template-template-parameter. */
24837 else if (declares_constrained_template_template_parameter (type))
24838 default_argument
24839 = cp_parser_default_template_template_argument (parser);
24841 /* Outside of a class definition, we can just parse the
24842 assignment-expression. */
24843 else
24844 default_argument
24845 = cp_parser_default_argument (parser, template_parm_p);
24847 if (!parser->default_arg_ok_p)
24849 permerror (token->location,
24850 "default arguments are only "
24851 "permitted for function parameters");
24853 else if ((declarator && declarator->parameter_pack_p)
24854 || template_parameter_pack_p
24855 || (decl_specifiers.type
24856 && PACK_EXPANSION_P (decl_specifiers.type)))
24858 /* Find the name of the parameter pack. */
24859 cp_declarator *id_declarator = declarator;
24860 while (id_declarator && id_declarator->kind != cdk_id)
24861 id_declarator = id_declarator->declarator;
24863 if (id_declarator && id_declarator->kind == cdk_id)
24864 error_at (declarator_token_start->location,
24865 template_parm_p
24866 ? G_("template parameter pack %qD "
24867 "cannot have a default argument")
24868 : G_("parameter pack %qD cannot have "
24869 "a default argument"),
24870 id_declarator->u.id.unqualified_name);
24871 else
24872 error_at (declarator_token_start->location,
24873 template_parm_p
24874 ? G_("template parameter pack cannot have "
24875 "a default argument")
24876 : G_("parameter pack cannot have a "
24877 "default argument"));
24879 default_argument = NULL_TREE;
24882 else
24883 default_argument = NULL_TREE;
24885 if (default_argument)
24886 STRIP_ANY_LOCATION_WRAPPER (default_argument);
24888 /* Generate a location for the parameter, ranging from the start of the
24889 initial token to the end of the final token (using input_location for
24890 the latter, set up by cp_lexer_set_source_position_from_token when
24891 consuming tokens).
24893 If we have a identifier, then use it for the caret location, e.g.
24895 extern int callee (int one, int (*two)(int, int), float three);
24896 ~~~~~~^~~~~~~~~~~~~~
24898 otherwise, reuse the start location for the caret location e.g.:
24900 extern int callee (int one, int (*)(int, int), float three);
24901 ^~~~~~~~~~~~~~~~~
24904 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
24905 ? declarator->id_loc
24906 : decl_spec_token_start->location);
24907 location_t param_loc = make_location (caret_loc,
24908 decl_spec_token_start->location,
24909 input_location);
24911 return make_parameter_declarator (&decl_specifiers,
24912 declarator,
24913 default_argument,
24914 param_loc,
24915 template_parameter_pack_p);
24918 /* Parse a default argument and return it.
24920 TEMPLATE_PARM_P is true if this is a default argument for a
24921 non-type template parameter. */
24922 static tree
24923 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
24925 tree default_argument = NULL_TREE;
24926 bool saved_greater_than_is_operator_p;
24927 unsigned char saved_local_variables_forbidden_p;
24928 bool non_constant_p, is_direct_init;
24930 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
24931 set correctly. */
24932 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
24933 parser->greater_than_is_operator_p = !template_parm_p;
24934 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
24935 auto ord = make_temp_override (parser->oacc_routine, NULL);
24936 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
24938 /* Local variable names (and the `this' keyword) may not
24939 appear in a default argument. */
24940 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24941 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
24942 /* Parse the assignment-expression. */
24943 if (template_parm_p)
24944 push_deferring_access_checks (dk_no_deferred);
24945 tree saved_class_ptr = NULL_TREE;
24946 tree saved_class_ref = NULL_TREE;
24947 /* The "this" pointer is not valid in a default argument. */
24948 if (cfun)
24950 saved_class_ptr = current_class_ptr;
24951 cp_function_chain->x_current_class_ptr = NULL_TREE;
24952 saved_class_ref = current_class_ref;
24953 cp_function_chain->x_current_class_ref = NULL_TREE;
24955 default_argument
24956 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
24957 /* Restore the "this" pointer. */
24958 if (cfun)
24960 cp_function_chain->x_current_class_ptr = saved_class_ptr;
24961 cp_function_chain->x_current_class_ref = saved_class_ref;
24963 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
24964 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24965 if (template_parm_p)
24966 pop_deferring_access_checks ();
24967 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
24968 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24970 return default_argument;
24973 /* Parse a function-body.
24975 function-body:
24976 compound_statement */
24978 static void
24979 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
24981 cp_parser_compound_statement (parser, NULL, (in_function_try_block
24982 ? BCS_TRY_BLOCK : BCS_NORMAL),
24983 true);
24986 /* Parse a ctor-initializer-opt followed by a function-body. Return
24987 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
24988 is true we are parsing a function-try-block. */
24990 static void
24991 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
24992 bool in_function_try_block)
24994 tree body, list;
24995 const bool check_body_p
24996 = (DECL_CONSTRUCTOR_P (current_function_decl)
24997 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
24998 tree last = NULL;
25000 if (in_function_try_block
25001 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25002 && cxx_dialect < cxx20)
25004 if (DECL_CONSTRUCTOR_P (current_function_decl))
25005 pedwarn (input_location, OPT_Wc__20_extensions,
25006 "function-try-block body of %<constexpr%> constructor only "
25007 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25008 else
25009 pedwarn (input_location, OPT_Wc__20_extensions,
25010 "function-try-block body of %<constexpr%> function only "
25011 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25014 /* Begin the function body. */
25015 body = begin_function_body ();
25016 /* Parse the optional ctor-initializer. */
25017 cp_parser_ctor_initializer_opt (parser);
25019 /* If we're parsing a constexpr constructor definition, we need
25020 to check that the constructor body is indeed empty. However,
25021 before we get to cp_parser_function_body lot of junk has been
25022 generated, so we can't just check that we have an empty block.
25023 Rather we take a snapshot of the outermost block, and check whether
25024 cp_parser_function_body changed its state. */
25025 if (check_body_p)
25027 list = cur_stmt_list;
25028 if (STATEMENT_LIST_TAIL (list))
25029 last = STATEMENT_LIST_TAIL (list)->stmt;
25031 /* Parse the function-body. */
25032 cp_parser_function_body (parser, in_function_try_block);
25033 if (check_body_p)
25034 check_constexpr_ctor_body (last, list, /*complain=*/true);
25035 /* Finish the function body. */
25036 finish_function_body (body);
25039 /* Parse an initializer.
25041 initializer:
25042 = initializer-clause
25043 ( expression-list )
25045 Returns an expression representing the initializer. If no
25046 initializer is present, NULL_TREE is returned.
25048 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25049 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25050 set to TRUE if there is no initializer present. If there is an
25051 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25052 is set to true; otherwise it is set to false. */
25054 static tree
25055 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
25056 bool* non_constant_p, bool subexpression_p)
25058 cp_token *token;
25059 tree init;
25061 /* Peek at the next token. */
25062 token = cp_lexer_peek_token (parser->lexer);
25064 /* Let our caller know whether or not this initializer was
25065 parenthesized. */
25066 *is_direct_init = (token->type != CPP_EQ);
25067 /* Assume that the initializer is constant. */
25068 *non_constant_p = false;
25070 if (token->type == CPP_EQ)
25072 /* Consume the `='. */
25073 cp_lexer_consume_token (parser->lexer);
25074 /* Parse the initializer-clause. */
25075 init = cp_parser_initializer_clause (parser, non_constant_p);
25077 else if (token->type == CPP_OPEN_PAREN)
25079 vec<tree, va_gc> *vec;
25080 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25081 /*cast_p=*/false,
25082 /*allow_expansion_p=*/true,
25083 non_constant_p);
25084 if (vec == NULL)
25085 return error_mark_node;
25086 init = build_tree_list_vec (vec);
25087 release_tree_vector (vec);
25089 else if (token->type == CPP_OPEN_BRACE)
25091 cp_lexer_set_source_position (parser->lexer);
25092 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25093 init = cp_parser_braced_list (parser, non_constant_p);
25094 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25096 else
25098 /* Anything else is an error. */
25099 cp_parser_error (parser, "expected initializer");
25100 init = error_mark_node;
25103 if (!subexpression_p && check_for_bare_parameter_packs (init))
25104 init = error_mark_node;
25106 return init;
25109 /* Parse an initializer-clause.
25111 initializer-clause:
25112 assignment-expression
25113 braced-init-list
25115 Returns an expression representing the initializer.
25117 If the `assignment-expression' production is used the value
25118 returned is simply a representation for the expression.
25120 Otherwise, calls cp_parser_braced_list. */
25122 static cp_expr
25123 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25125 cp_expr initializer;
25127 /* Assume the expression is constant. */
25128 *non_constant_p = false;
25130 /* If it is not a `{', then we are looking at an
25131 assignment-expression. */
25132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25134 initializer
25135 = cp_parser_constant_expression (parser,
25136 /*allow_non_constant_p=*/2,
25137 non_constant_p);
25139 else
25140 initializer = cp_parser_braced_list (parser, non_constant_p);
25142 return initializer;
25145 /* Parse a brace-enclosed initializer list.
25147 braced-init-list:
25148 { initializer-list , [opt] }
25149 { designated-initializer-list , [opt] }
25152 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25153 the elements of the initializer-list (or NULL, if the last
25154 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25155 NULL_TREE. There is no way to detect whether or not the optional
25156 trailing `,' was provided. NON_CONSTANT_P is as for
25157 cp_parser_initializer. */
25159 static cp_expr
25160 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
25162 tree initializer;
25163 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25165 /* Consume the `{' token. */
25166 matching_braces braces;
25167 braces.require_open (parser);
25168 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25169 initializer = make_node (CONSTRUCTOR);
25170 /* If it's not a `}', then there is a non-trivial initializer. */
25171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25173 bool designated;
25174 /* Parse the initializer list. */
25175 CONSTRUCTOR_ELTS (initializer)
25176 = cp_parser_initializer_list (parser, non_constant_p, &designated);
25177 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25178 /* A trailing `,' token is allowed. */
25179 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25180 cp_lexer_consume_token (parser->lexer);
25182 else
25183 *non_constant_p = false;
25184 /* Now, there should be a trailing `}'. */
25185 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25186 braces.require_close (parser);
25187 TREE_TYPE (initializer) = init_list_type_node;
25189 cp_expr result (initializer);
25190 /* Build a location of the form:
25191 { ... }
25192 ^~~~~~~
25193 with caret==start at the open brace, finish at the close brace. */
25194 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25195 result.set_location (combined_loc);
25196 return result;
25199 /* Consume tokens up to, and including, the next non-nested closing `]'.
25200 Returns true iff we found a closing `]'. */
25202 static bool
25203 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25205 unsigned square_depth = 0;
25207 while (true)
25209 cp_token * token = cp_lexer_peek_token (parser->lexer);
25211 switch (token->type)
25213 case CPP_PRAGMA_EOL:
25214 if (!parser->lexer->in_pragma)
25215 break;
25216 /* FALLTHRU */
25217 case CPP_EOF:
25218 /* If we've run out of tokens, then there is no closing `]'. */
25219 return false;
25221 case CPP_OPEN_SQUARE:
25222 ++square_depth;
25223 break;
25225 case CPP_CLOSE_SQUARE:
25226 if (!square_depth--)
25228 cp_lexer_consume_token (parser->lexer);
25229 return true;
25231 break;
25233 default:
25234 break;
25237 /* Consume the token. */
25238 cp_lexer_consume_token (parser->lexer);
25242 /* Return true if we are looking at an array-designator, false otherwise. */
25244 static bool
25245 cp_parser_array_designator_p (cp_parser *parser)
25247 /* Consume the `['. */
25248 cp_lexer_consume_token (parser->lexer);
25250 cp_lexer_save_tokens (parser->lexer);
25252 /* Skip tokens until the next token is a closing square bracket.
25253 If we find the closing `]', and the next token is a `=', then
25254 we are looking at an array designator. */
25255 bool array_designator_p
25256 = (cp_parser_skip_to_closing_square_bracket (parser)
25257 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25259 /* Roll back the tokens we skipped. */
25260 cp_lexer_rollback_tokens (parser->lexer);
25262 return array_designator_p;
25265 /* Parse an initializer-list.
25267 initializer-list:
25268 initializer-clause ... [opt]
25269 initializer-list , initializer-clause ... [opt]
25271 C++20 Extension:
25273 designated-initializer-list:
25274 designated-initializer-clause
25275 designated-initializer-list , designated-initializer-clause
25277 designated-initializer-clause:
25278 designator brace-or-equal-initializer
25280 designator:
25281 . identifier
25283 GNU Extension:
25285 initializer-list:
25286 designation initializer-clause ...[opt]
25287 initializer-list , designation initializer-clause ...[opt]
25289 designation:
25290 . identifier =
25291 identifier :
25292 [ constant-expression ] =
25294 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25295 for the initializer. If the INDEX of the elt is non-NULL, it is the
25296 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25297 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25298 are any designators. */
25300 static vec<constructor_elt, va_gc> *
25301 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25302 bool *designated)
25304 vec<constructor_elt, va_gc> *v = NULL;
25305 bool first_p = true;
25306 tree first_designator = NULL_TREE;
25308 /* Assume all of the expressions are constant. */
25309 *non_constant_p = false;
25311 unsigned nelts = 0;
25312 int suppress = suppress_location_wrappers;
25314 /* Parse the rest of the list. */
25315 while (true)
25317 cp_token *token;
25318 tree designator;
25319 tree initializer;
25320 bool clause_non_constant_p;
25321 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25323 /* Handle the C++20 syntax, '. id ='. */
25324 if ((cxx_dialect >= cxx20
25325 || cp_parser_allow_gnu_extensions_p (parser))
25326 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25327 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25328 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25329 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25330 == CPP_OPEN_BRACE)))
25332 if (pedantic && cxx_dialect < cxx20)
25333 pedwarn (loc, OPT_Wc__20_extensions,
25334 "C++ designated initializers only available with "
25335 "%<-std=c++20%> or %<-std=gnu++20%>");
25336 /* Consume the `.'. */
25337 cp_lexer_consume_token (parser->lexer);
25338 /* Consume the identifier. */
25339 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25340 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25341 /* Consume the `='. */
25342 cp_lexer_consume_token (parser->lexer);
25344 /* Also, if the next token is an identifier and the following one is a
25345 colon, we are looking at the GNU designated-initializer
25346 syntax. */
25347 else if (cp_parser_allow_gnu_extensions_p (parser)
25348 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25349 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25350 == CPP_COLON))
25352 /* Warn the user that they are using an extension. */
25353 pedwarn (loc, OPT_Wpedantic,
25354 "ISO C++ does not allow GNU designated initializers");
25355 /* Consume the identifier. */
25356 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25357 /* Consume the `:'. */
25358 cp_lexer_consume_token (parser->lexer);
25360 /* Also handle C99 array designators, '[ const ] ='. */
25361 else if (cp_parser_allow_gnu_extensions_p (parser)
25362 && !c_dialect_objc ()
25363 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25365 /* In C++11, [ could start a lambda-introducer. */
25366 bool non_const = false;
25368 cp_parser_parse_tentatively (parser);
25370 if (!cp_parser_array_designator_p (parser))
25372 cp_parser_simulate_error (parser);
25373 designator = NULL_TREE;
25375 else
25377 designator = cp_parser_constant_expression (parser, true,
25378 &non_const);
25379 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25380 cp_parser_require (parser, CPP_EQ, RT_EQ);
25383 if (!cp_parser_parse_definitely (parser))
25384 designator = NULL_TREE;
25385 else if (non_const
25386 && (!require_potential_rvalue_constant_expression
25387 (designator)))
25388 designator = NULL_TREE;
25389 if (designator)
25390 /* Warn the user that they are using an extension. */
25391 pedwarn (loc, OPT_Wpedantic,
25392 "ISO C++ does not allow C99 designated initializers");
25394 else
25395 designator = NULL_TREE;
25397 if (first_p)
25399 first_designator = designator;
25400 first_p = false;
25402 else if (cxx_dialect >= cxx20
25403 && first_designator != error_mark_node
25404 && (!first_designator != !designator))
25406 error_at (loc, "either all initializer clauses should be designated "
25407 "or none of them should be");
25408 first_designator = error_mark_node;
25410 else if (cxx_dialect < cxx20 && !first_designator)
25411 first_designator = designator;
25413 /* Parse the initializer. */
25414 initializer = cp_parser_initializer_clause (parser,
25415 &clause_non_constant_p);
25416 /* If any clause is non-constant, so is the entire initializer. */
25417 if (clause_non_constant_p)
25418 *non_constant_p = true;
25420 /* If we have an ellipsis, this is an initializer pack
25421 expansion. */
25422 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25424 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25426 /* Consume the `...'. */
25427 cp_lexer_consume_token (parser->lexer);
25429 if (designator && cxx_dialect >= cxx20)
25430 error_at (loc,
25431 "%<...%> not allowed in designated initializer list");
25433 /* Turn the initializer into an initializer expansion. */
25434 initializer = make_pack_expansion (initializer);
25437 /* Add it to the vector. */
25438 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
25440 /* If the next token is not a comma, we have reached the end of
25441 the list. */
25442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25443 break;
25445 /* Peek at the next token. */
25446 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25447 /* If the next token is a `}', then we're still done. An
25448 initializer-clause can have a trailing `,' after the
25449 initializer-list and before the closing `}'. */
25450 if (token->type == CPP_CLOSE_BRACE)
25451 break;
25453 /* Suppress location wrappers in a long initializer to save memory
25454 (14179). The cutoff is chosen arbitrarily. */
25455 const unsigned loc_max = 256;
25456 unsigned incr = 1;
25457 if (TREE_CODE (initializer) == CONSTRUCTOR)
25458 /* Look one level down because it's easy. Looking deeper would require
25459 passing down a nelts pointer, and I don't think multi-level massive
25460 initializers are common enough to justify this. */
25461 incr = CONSTRUCTOR_NELTS (initializer);
25462 nelts += incr;
25463 if (nelts >= loc_max && (nelts - incr) < loc_max)
25464 ++suppress_location_wrappers;
25466 /* Consume the `,' token. */
25467 cp_lexer_consume_token (parser->lexer);
25470 /* The same identifier shall not appear in multiple designators
25471 of a designated-initializer-list. */
25472 if (first_designator)
25474 unsigned int i;
25475 tree designator, val;
25476 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25477 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25479 if (IDENTIFIER_MARKED (designator))
25481 error_at (cp_expr_loc_or_input_loc (val),
25482 "%<.%s%> designator used multiple times in "
25483 "the same initializer list",
25484 IDENTIFIER_POINTER (designator));
25485 (*v)[i].index = error_mark_node;
25487 else
25488 IDENTIFIER_MARKED (designator) = 1;
25490 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25491 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25492 IDENTIFIER_MARKED (designator) = 0;
25495 suppress_location_wrappers = suppress;
25497 *designated = first_designator != NULL_TREE;
25498 return v;
25501 /* Classes [gram.class] */
25503 /* Parse a class-name.
25505 class-name:
25506 identifier
25507 template-id
25509 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25510 to indicate that names looked up in dependent types should be
25511 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
25512 keyword has been used to indicate that the name that appears next
25513 is a template. TAG_TYPE indicates the explicit tag given before
25514 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
25515 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
25516 is the class being defined in a class-head. If ENUM_OK is TRUE,
25517 enum-names are also accepted.
25519 Returns the TYPE_DECL representing the class. */
25521 static tree
25522 cp_parser_class_name (cp_parser *parser,
25523 bool typename_keyword_p,
25524 bool template_keyword_p,
25525 enum tag_types tag_type,
25526 bool check_dependency_p,
25527 bool class_head_p,
25528 bool is_declaration,
25529 bool enum_ok)
25531 tree decl;
25532 tree identifier = NULL_TREE;
25534 /* All class-names start with an identifier. */
25535 cp_token *token = cp_lexer_peek_token (parser->lexer);
25536 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
25538 cp_parser_error (parser, "expected class-name");
25539 return error_mark_node;
25542 /* PARSER->SCOPE can be cleared when parsing the template-arguments
25543 to a template-id, so we save it here. Consider object scope too,
25544 so that make_typename_type below can use it (cp_parser_template_name
25545 considers object scope also). This may happen with code like
25547 p->template A<T>::a()
25549 where we first want to look up A<T>::a in the class of the object
25550 expression, as per [basic.lookup.classref]. */
25551 tree scope = parser->scope ? parser->scope : parser->context->object_type;
25552 /* This only checks parser->scope to avoid duplicate errors; if
25553 ->object_type is erroneous, go on to give a parse error. */
25554 if (parser->scope == error_mark_node)
25555 return error_mark_node;
25557 /* Any name names a type if we're following the `typename' keyword
25558 in a qualified name where the enclosing scope is type-dependent. */
25559 const bool typename_p = (typename_keyword_p
25560 && parser->scope
25561 && TYPE_P (parser->scope)
25562 && dependent_scope_p (parser->scope));
25563 /* Handle the common case (an identifier, but not a template-id)
25564 efficiently. */
25565 if (token->type == CPP_NAME
25566 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
25568 cp_token *identifier_token;
25569 bool ambiguous_p;
25571 /* Look for the identifier. */
25572 identifier_token = cp_lexer_peek_token (parser->lexer);
25573 ambiguous_p = identifier_token->error_reported;
25574 identifier = cp_parser_identifier (parser);
25575 /* If the next token isn't an identifier, we are certainly not
25576 looking at a class-name. */
25577 if (identifier == error_mark_node)
25578 decl = error_mark_node;
25579 /* If we know this is a type-name, there's no need to look it
25580 up. */
25581 else if (typename_p)
25582 decl = identifier;
25583 else
25585 tree ambiguous_decls;
25586 /* If we already know that this lookup is ambiguous, then
25587 we've already issued an error message; there's no reason
25588 to check again. */
25589 if (ambiguous_p)
25591 cp_parser_simulate_error (parser);
25592 return error_mark_node;
25594 /* If the next token is a `::', then the name must be a type
25595 name.
25597 [basic.lookup.qual]
25599 During the lookup for a name preceding the :: scope
25600 resolution operator, object, function, and enumerator
25601 names are ignored. */
25602 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25603 tag_type = scope_type;
25604 /* Look up the name. */
25605 decl = cp_parser_lookup_name (parser, identifier,
25606 tag_type,
25607 /*is_template=*/false,
25608 /*is_namespace=*/false,
25609 check_dependency_p,
25610 &ambiguous_decls,
25611 identifier_token->location);
25612 if (ambiguous_decls)
25614 if (cp_parser_parsing_tentatively (parser))
25615 cp_parser_simulate_error (parser);
25616 return error_mark_node;
25620 else
25622 /* Try a template-id. */
25623 decl = cp_parser_template_id (parser, template_keyword_p,
25624 check_dependency_p,
25625 tag_type,
25626 is_declaration);
25627 if (decl == error_mark_node)
25628 return error_mark_node;
25631 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
25633 /* If this is a typename, create a TYPENAME_TYPE. */
25634 if (typename_p && decl != error_mark_node)
25636 decl = make_typename_type (scope, decl, typename_type,
25637 /*complain=*/tf_error);
25638 if (decl != error_mark_node)
25639 decl = TYPE_NAME (decl);
25642 decl = strip_using_decl (decl);
25644 /* Check to see that it is really the name of a class. */
25645 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
25646 && identifier_p (TREE_OPERAND (decl, 0))
25647 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25648 /* Situations like this:
25650 template <typename T> struct A {
25651 typename T::template X<int>::I i;
25654 are problematic. Is `T::template X<int>' a class-name? The
25655 standard does not seem to be definitive, but there is no other
25656 valid interpretation of the following `::'. Therefore, those
25657 names are considered class-names. */
25659 decl = make_typename_type (scope, decl, tag_type, tf_error);
25660 if (decl != error_mark_node)
25661 decl = TYPE_NAME (decl);
25663 else if (TREE_CODE (decl) != TYPE_DECL
25664 || TREE_TYPE (decl) == error_mark_node
25665 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
25666 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
25667 /* In Objective-C 2.0, a classname followed by '.' starts a
25668 dot-syntax expression, and it's not a type-name. */
25669 || (c_dialect_objc ()
25670 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
25671 && objc_is_class_name (decl)))
25672 decl = error_mark_node;
25674 if (decl == error_mark_node)
25675 cp_parser_error (parser, "expected class-name");
25676 else if (identifier && !parser->scope)
25677 maybe_note_name_used_in_class (identifier, decl);
25679 return decl;
25682 /* Make sure that any member-function parameters are in scope.
25683 For instance, a function's noexcept-specifier can use the function's
25684 parameters:
25686 struct S {
25687 void fn (int p) noexcept(noexcept(p));
25690 so we need to make sure name lookup can find them. This is used
25691 when we delay parsing of the noexcept-specifier. */
25693 static void
25694 inject_parm_decls (tree decl)
25696 begin_scope (sk_function_parms, decl);
25697 tree args = DECL_ARGUMENTS (decl);
25699 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
25701 if (args && is_this_parameter (args))
25703 gcc_checking_assert (current_class_ptr == NULL_TREE);
25704 current_class_ref = cp_build_fold_indirect_ref (args);
25705 current_class_ptr = args;
25709 /* Undo the effects of inject_parm_decls. */
25711 static void
25712 pop_injected_parms (void)
25714 pop_bindings_and_leave_scope ();
25715 current_class_ptr = current_class_ref = NULL_TREE;
25718 /* Parse a class-specifier.
25720 class-specifier:
25721 class-head { member-specification [opt] }
25723 Returns the TREE_TYPE representing the class. */
25725 static tree
25726 cp_parser_class_specifier_1 (cp_parser* parser)
25728 tree type;
25729 tree attributes = NULL_TREE;
25730 bool nested_name_specifier_p;
25731 unsigned saved_num_template_parameter_lists;
25732 bool saved_in_function_body;
25733 unsigned char in_statement;
25734 bool in_switch_statement_p;
25735 bool saved_in_unbraced_linkage_specification_p;
25736 tree old_scope = NULL_TREE;
25737 tree scope = NULL_TREE;
25738 cp_token *closing_brace;
25740 push_deferring_access_checks (dk_no_deferred);
25742 /* Parse the class-head. */
25743 type = cp_parser_class_head (parser,
25744 &nested_name_specifier_p);
25745 /* If the class-head was a semantic disaster, skip the entire body
25746 of the class. */
25747 if (!type)
25749 cp_parser_skip_to_end_of_block_or_statement (parser);
25750 pop_deferring_access_checks ();
25751 return error_mark_node;
25754 /* Look for the `{'. */
25755 matching_braces braces;
25756 if (!braces.require_open (parser))
25758 pop_deferring_access_checks ();
25759 return error_mark_node;
25762 cp_ensure_no_omp_declare_simd (parser);
25763 cp_ensure_no_oacc_routine (parser);
25765 /* Issue an error message if type-definitions are forbidden here. */
25766 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
25767 /* Remember that we are defining one more class. */
25768 ++parser->num_classes_being_defined;
25769 /* Inside the class, surrounding template-parameter-lists do not
25770 apply. */
25771 saved_num_template_parameter_lists
25772 = parser->num_template_parameter_lists;
25773 parser->num_template_parameter_lists = 0;
25774 /* We are not in a function body. */
25775 saved_in_function_body = parser->in_function_body;
25776 parser->in_function_body = false;
25777 /* Or in a loop. */
25778 in_statement = parser->in_statement;
25779 parser->in_statement = 0;
25780 /* Or in a switch. */
25781 in_switch_statement_p = parser->in_switch_statement_p;
25782 parser->in_switch_statement_p = false;
25783 /* We are not immediately inside an extern "lang" block. */
25784 saved_in_unbraced_linkage_specification_p
25785 = parser->in_unbraced_linkage_specification_p;
25786 parser->in_unbraced_linkage_specification_p = false;
25788 /* Start the class. */
25789 if (nested_name_specifier_p)
25791 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
25792 /* SCOPE must be a scope nested inside current scope. */
25793 if (is_nested_namespace (current_namespace,
25794 decl_namespace_context (scope)))
25795 old_scope = push_inner_scope (scope);
25796 else
25797 nested_name_specifier_p = false;
25799 type = begin_class_definition (type);
25801 if (type == error_mark_node)
25802 /* If the type is erroneous, skip the entire body of the class. */
25803 cp_parser_skip_to_closing_brace (parser);
25804 else
25805 /* Parse the member-specification. */
25806 cp_parser_member_specification_opt (parser);
25808 /* Look for the trailing `}'. */
25809 closing_brace = braces.require_close (parser);
25810 /* Look for trailing attributes to apply to this class. */
25811 if (cp_parser_allow_gnu_extensions_p (parser))
25812 attributes = cp_parser_gnu_attributes_opt (parser);
25813 if (type != error_mark_node)
25814 type = finish_struct (type, attributes);
25815 if (nested_name_specifier_p)
25816 pop_inner_scope (old_scope, scope);
25818 /* We've finished a type definition. Check for the common syntax
25819 error of forgetting a semicolon after the definition. We need to
25820 be careful, as we can't just check for not-a-semicolon and be done
25821 with it; the user might have typed:
25823 class X { } c = ...;
25824 class X { } *p = ...;
25826 and so forth. Instead, enumerate all the possible tokens that
25827 might follow this production; if we don't see one of them, then
25828 complain and silently insert the semicolon. */
25830 cp_token *token = cp_lexer_peek_token (parser->lexer);
25831 bool want_semicolon = true;
25833 if (cp_next_tokens_can_be_std_attribute_p (parser))
25834 /* Don't try to parse c++11 attributes here. As per the
25835 grammar, that should be a task for
25836 cp_parser_decl_specifier_seq. */
25837 want_semicolon = false;
25839 switch (token->type)
25841 case CPP_NAME:
25842 case CPP_SEMICOLON:
25843 case CPP_MULT:
25844 case CPP_AND:
25845 case CPP_OPEN_PAREN:
25846 case CPP_CLOSE_PAREN:
25847 case CPP_COMMA:
25848 want_semicolon = false;
25849 break;
25851 /* While it's legal for type qualifiers and storage class
25852 specifiers to follow type definitions in the grammar, only
25853 compiler testsuites contain code like that. Assume that if
25854 we see such code, then what we're really seeing is a case
25855 like:
25857 class X { }
25858 const <type> var = ...;
25862 class Y { }
25863 static <type> func (...) ...
25865 i.e. the qualifier or specifier applies to the next
25866 declaration. To do so, however, we need to look ahead one
25867 more token to see if *that* token is a type specifier.
25869 This code could be improved to handle:
25871 class Z { }
25872 static const <type> var = ...; */
25873 case CPP_KEYWORD:
25874 if (keyword_is_decl_specifier (token->keyword))
25876 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
25878 /* Handling user-defined types here would be nice, but very
25879 tricky. */
25880 want_semicolon
25881 = (lookahead->type == CPP_KEYWORD
25882 && keyword_begins_type_specifier (lookahead->keyword));
25884 break;
25885 default:
25886 break;
25889 /* If we don't have a type, then something is very wrong and we
25890 shouldn't try to do anything clever. Likewise for not seeing the
25891 closing brace. */
25892 if (closing_brace && TYPE_P (type) && want_semicolon)
25894 /* Locate the closing brace. */
25895 cp_token_position prev
25896 = cp_lexer_previous_token_position (parser->lexer);
25897 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
25898 location_t loc = prev_token->location;
25900 /* We want to suggest insertion of a ';' immediately *after* the
25901 closing brace, so, if we can, offset the location by 1 column. */
25902 location_t next_loc = loc;
25903 if (!linemap_location_from_macro_expansion_p (line_table, loc))
25904 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
25906 rich_location richloc (line_table, next_loc);
25908 /* If we successfully offset the location, suggest the fix-it. */
25909 if (next_loc != loc)
25910 richloc.add_fixit_insert_before (next_loc, ";");
25912 if (CLASSTYPE_DECLARED_CLASS (type))
25913 error_at (&richloc,
25914 "expected %<;%> after class definition");
25915 else if (TREE_CODE (type) == RECORD_TYPE)
25916 error_at (&richloc,
25917 "expected %<;%> after struct definition");
25918 else if (TREE_CODE (type) == UNION_TYPE)
25919 error_at (&richloc,
25920 "expected %<;%> after union definition");
25921 else
25922 gcc_unreachable ();
25924 /* Unget one token and smash it to look as though we encountered
25925 a semicolon in the input stream. */
25926 cp_lexer_set_token_position (parser->lexer, prev);
25927 token = cp_lexer_peek_token (parser->lexer);
25928 token->type = CPP_SEMICOLON;
25929 token->keyword = RID_MAX;
25933 /* If this class is not itself within the scope of another class,
25934 then we need to parse the bodies of all of the queued function
25935 definitions. Note that the queued functions defined in a class
25936 are not always processed immediately following the
25937 class-specifier for that class. Consider:
25939 struct A {
25940 struct B { void f() { sizeof (A); } };
25943 If `f' were processed before the processing of `A' were
25944 completed, there would be no way to compute the size of `A'.
25945 Note that the nesting we are interested in here is lexical --
25946 not the semantic nesting given by TYPE_CONTEXT. In particular,
25947 for:
25949 struct A { struct B; };
25950 struct A::B { void f() { } };
25952 there is no need to delay the parsing of `A::B::f'. */
25953 if (--parser->num_classes_being_defined == 0)
25955 tree decl;
25956 tree class_type = NULL_TREE;
25957 tree pushed_scope = NULL_TREE;
25958 unsigned ix;
25959 cp_default_arg_entry *e;
25961 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
25963 /* Skip default arguments, NSDMIs, etc, in order to improve
25964 error recovery (c++/71169, c++/71832). */
25965 vec_safe_truncate (unparsed_funs_with_default_args, 0);
25966 vec_safe_truncate (unparsed_nsdmis, 0);
25967 vec_safe_truncate (unparsed_funs_with_definitions, 0);
25970 /* In a first pass, parse default arguments to the functions.
25971 Then, in a second pass, parse the bodies of the functions.
25972 This two-phased approach handles cases like:
25974 struct S {
25975 void f() { g(); }
25976 void g(int i = 3);
25980 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
25982 decl = e->decl;
25983 /* If there are default arguments that have not yet been processed,
25984 take care of them now. */
25985 if (class_type != e->class_type)
25987 if (pushed_scope)
25988 pop_scope (pushed_scope);
25989 class_type = e->class_type;
25990 pushed_scope = push_scope (class_type);
25992 /* Make sure that any template parameters are in scope. */
25993 maybe_begin_member_template_processing (decl);
25994 /* Parse the default argument expressions. */
25995 cp_parser_late_parsing_default_args (parser, decl);
25996 /* Remove any template parameters from the symbol table. */
25997 maybe_end_member_template_processing ();
25999 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26001 /* If there are noexcept-specifiers that have not yet been processed,
26002 take care of them now. Do this before processing NSDMIs as they
26003 may depend on noexcept-specifiers already having been processed. */
26004 tree save_ccp = current_class_ptr;
26005 tree save_ccr = current_class_ref;
26006 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26008 tree ctx = DECL_CONTEXT (decl);
26009 if (class_type != ctx)
26011 if (pushed_scope)
26012 pop_scope (pushed_scope);
26013 class_type = ctx;
26014 pushed_scope = push_scope (class_type);
26017 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26018 def_parse = TREE_PURPOSE (def_parse);
26020 /* Make sure that any template parameters are in scope. */
26021 maybe_begin_member_template_processing (decl);
26023 /* Make sure that any member-function parameters are in scope.
26024 This function doesn't expect ccp to be set. */
26025 current_class_ptr = current_class_ref = NULL_TREE;
26026 inject_parm_decls (decl);
26028 /* 'this' is not allowed in static member functions. */
26029 unsigned char local_variables_forbidden_p
26030 = parser->local_variables_forbidden_p;
26031 if (DECL_THIS_STATIC (decl))
26032 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26034 /* Now we can parse the noexcept-specifier. */
26035 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26037 if (spec == error_mark_node)
26038 spec = NULL_TREE;
26040 /* Update the fn's type directly -- it might have escaped
26041 beyond this decl :( */
26042 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26043 /* Update any instantiations we've already created. We must
26044 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26045 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26046 in the pattern. */
26047 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26048 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26049 = spec ? TREE_PURPOSE (spec) : error_mark_node;
26051 /* Restore the state of local_variables_forbidden_p. */
26052 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26054 /* The finish_struct call above performed various override checking,
26055 but it skipped unparsed noexcept-specifier operands. Now that we
26056 have resolved them, check again. */
26057 noexcept_override_late_checks (type, decl);
26059 /* Remove any member-function parameters from the symbol table. */
26060 pop_injected_parms ();
26062 /* Remove any template parameters from the symbol table. */
26063 maybe_end_member_template_processing ();
26065 vec_safe_truncate (unparsed_noexcepts, 0);
26067 /* Now parse any NSDMIs. */
26068 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26070 if (class_type != DECL_CONTEXT (decl))
26072 if (pushed_scope)
26073 pop_scope (pushed_scope);
26074 class_type = DECL_CONTEXT (decl);
26075 pushed_scope = push_scope (class_type);
26077 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26078 cp_parser_late_parsing_nsdmi (parser, decl);
26080 vec_safe_truncate (unparsed_nsdmis, 0);
26081 current_class_ptr = save_ccp;
26082 current_class_ref = save_ccr;
26083 if (pushed_scope)
26084 pop_scope (pushed_scope);
26086 /* Now parse the body of the functions. */
26087 if (flag_openmp)
26089 /* OpenMP UDRs need to be parsed before all other functions. */
26090 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26091 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26092 cp_parser_late_parsing_for_member (parser, decl);
26093 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26094 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26095 cp_parser_late_parsing_for_member (parser, decl);
26097 else
26098 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26099 cp_parser_late_parsing_for_member (parser, decl);
26100 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26103 /* Put back any saved access checks. */
26104 pop_deferring_access_checks ();
26106 /* Restore saved state. */
26107 parser->in_switch_statement_p = in_switch_statement_p;
26108 parser->in_statement = in_statement;
26109 parser->in_function_body = saved_in_function_body;
26110 parser->num_template_parameter_lists
26111 = saved_num_template_parameter_lists;
26112 parser->in_unbraced_linkage_specification_p
26113 = saved_in_unbraced_linkage_specification_p;
26115 return type;
26118 static tree
26119 cp_parser_class_specifier (cp_parser* parser)
26121 tree ret;
26122 timevar_push (TV_PARSE_STRUCT);
26123 ret = cp_parser_class_specifier_1 (parser);
26124 timevar_pop (TV_PARSE_STRUCT);
26125 return ret;
26128 /* Parse a class-head.
26130 class-head:
26131 class-key identifier [opt] base-clause [opt]
26132 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26133 class-key nested-name-specifier [opt] template-id
26134 base-clause [opt]
26136 class-virt-specifier:
26137 final
26139 GNU Extensions:
26140 class-key attributes identifier [opt] base-clause [opt]
26141 class-key attributes nested-name-specifier identifier base-clause [opt]
26142 class-key attributes nested-name-specifier [opt] template-id
26143 base-clause [opt]
26145 Upon return BASES is initialized to the list of base classes (or
26146 NULL, if there are none) in the same form returned by
26147 cp_parser_base_clause.
26149 Returns the TYPE of the indicated class. Sets
26150 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26151 involving a nested-name-specifier was used, and FALSE otherwise.
26153 Returns error_mark_node if this is not a class-head.
26155 Returns NULL_TREE if the class-head is syntactically valid, but
26156 semantically invalid in a way that means we should skip the entire
26157 body of the class. */
26159 static tree
26160 cp_parser_class_head (cp_parser* parser,
26161 bool* nested_name_specifier_p)
26163 tree nested_name_specifier;
26164 enum tag_types class_key;
26165 tree id = NULL_TREE;
26166 tree type = NULL_TREE;
26167 tree attributes;
26168 tree bases;
26169 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26170 bool template_id_p = false;
26171 bool qualified_p = false;
26172 bool invalid_nested_name_p = false;
26173 bool invalid_explicit_specialization_p = false;
26174 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26175 tree pushed_scope = NULL_TREE;
26176 unsigned num_templates;
26177 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26178 /* Assume no nested-name-specifier will be present. */
26179 *nested_name_specifier_p = false;
26180 /* Assume no template parameter lists will be used in defining the
26181 type. */
26182 num_templates = 0;
26183 parser->colon_corrects_to_scope_p = false;
26185 /* Look for the class-key. */
26186 class_key = cp_parser_class_key (parser);
26187 if (class_key == none_type)
26188 return error_mark_node;
26190 location_t class_head_start_location = input_location;
26192 /* Parse the attributes. */
26193 attributes = cp_parser_attributes_opt (parser);
26195 /* If the next token is `::', that is invalid -- but sometimes
26196 people do try to write:
26198 struct ::S {};
26200 Handle this gracefully by accepting the extra qualifier, and then
26201 issuing an error about it later if this really is a
26202 class-head. If it turns out just to be an elaborated type
26203 specifier, remain silent. */
26204 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26205 qualified_p = true;
26207 push_deferring_access_checks (dk_no_check);
26209 /* Determine the name of the class. Begin by looking for an
26210 optional nested-name-specifier. */
26211 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26212 nested_name_specifier
26213 = cp_parser_nested_name_specifier_opt (parser,
26214 /*typename_keyword_p=*/false,
26215 /*check_dependency_p=*/false,
26216 /*type_p=*/true,
26217 /*is_declaration=*/false);
26218 /* If there was a nested-name-specifier, then there *must* be an
26219 identifier. */
26221 cp_token *bad_template_keyword = NULL;
26223 if (nested_name_specifier)
26225 type_start_token = cp_lexer_peek_token (parser->lexer);
26226 /* Although the grammar says `identifier', it really means
26227 `class-name' or `template-name'. You are only allowed to
26228 define a class that has already been declared with this
26229 syntax.
26231 The proposed resolution for Core Issue 180 says that wherever
26232 you see `class T::X' you should treat `X' as a type-name.
26234 It is OK to define an inaccessible class; for example:
26236 class A { class B; };
26237 class A::B {};
26239 We do not know if we will see a class-name, or a
26240 template-name. We look for a class-name first, in case the
26241 class-name is a template-id; if we looked for the
26242 template-name first we would stop after the template-name. */
26243 cp_parser_parse_tentatively (parser);
26244 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26245 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26246 type = cp_parser_class_name (parser,
26247 /*typename_keyword_p=*/false,
26248 /*template_keyword_p=*/false,
26249 class_type,
26250 /*check_dependency_p=*/false,
26251 /*class_head_p=*/true,
26252 /*is_declaration=*/false);
26253 /* If that didn't work, ignore the nested-name-specifier. */
26254 if (!cp_parser_parse_definitely (parser))
26256 invalid_nested_name_p = true;
26257 type_start_token = cp_lexer_peek_token (parser->lexer);
26258 id = cp_parser_identifier (parser);
26259 if (id == error_mark_node)
26260 id = NULL_TREE;
26262 /* If we could not find a corresponding TYPE, treat this
26263 declaration like an unqualified declaration. */
26264 if (type == error_mark_node)
26265 nested_name_specifier = NULL_TREE;
26266 /* Otherwise, count the number of templates used in TYPE and its
26267 containing scopes. */
26268 else
26269 num_templates = num_template_headers_for_class (TREE_TYPE (type));
26271 /* Otherwise, the identifier is optional. */
26272 else
26274 /* We don't know whether what comes next is a template-id,
26275 an identifier, or nothing at all. */
26276 cp_parser_parse_tentatively (parser);
26277 /* Check for a template-id. */
26278 type_start_token = cp_lexer_peek_token (parser->lexer);
26279 id = cp_parser_template_id (parser,
26280 /*template_keyword_p=*/false,
26281 /*check_dependency_p=*/true,
26282 class_key,
26283 /*is_declaration=*/true);
26284 /* If that didn't work, it could still be an identifier. */
26285 if (!cp_parser_parse_definitely (parser))
26287 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26289 type_start_token = cp_lexer_peek_token (parser->lexer);
26290 id = cp_parser_identifier (parser);
26292 else
26293 id = NULL_TREE;
26295 else
26297 template_id_p = true;
26298 ++num_templates;
26302 pop_deferring_access_checks ();
26304 if (id)
26306 cp_parser_check_for_invalid_template_id (parser, id,
26307 class_key,
26308 type_start_token->location);
26310 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
26312 /* If it's not a `:' or a `{' then we can't really be looking at a
26313 class-head, since a class-head only appears as part of a
26314 class-specifier. We have to detect this situation before calling
26315 xref_tag, since that has irreversible side-effects. */
26316 if (!cp_parser_next_token_starts_class_definition_p (parser))
26318 cp_parser_error (parser, "expected %<{%> or %<:%>");
26319 type = error_mark_node;
26320 goto out;
26323 /* At this point, we're going ahead with the class-specifier, even
26324 if some other problem occurs. */
26325 cp_parser_commit_to_tentative_parse (parser);
26326 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
26328 cp_parser_error (parser,
26329 "cannot specify %<override%> for a class");
26330 type = error_mark_node;
26331 goto out;
26333 /* Issue the error about the overly-qualified name now. */
26334 if (qualified_p)
26336 cp_parser_error (parser,
26337 "global qualification of class name is invalid");
26338 type = error_mark_node;
26339 goto out;
26341 else if (invalid_nested_name_p)
26343 cp_parser_error (parser,
26344 "qualified name does not name a class");
26345 type = error_mark_node;
26346 goto out;
26348 else if (nested_name_specifier)
26350 tree scope;
26352 if (bad_template_keyword)
26353 /* [temp.names]: in a qualified-id formed by a class-head-name, the
26354 keyword template shall not appear at the top level. */
26355 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
26356 "keyword %<template%> not allowed in class-head-name");
26358 /* Reject typedef-names in class heads. */
26359 if (!DECL_IMPLICIT_TYPEDEF_P (type))
26361 error_at (type_start_token->location,
26362 "invalid class name in declaration of %qD",
26363 type);
26364 type = NULL_TREE;
26365 goto done;
26368 /* Figure out in what scope the declaration is being placed. */
26369 scope = current_scope ();
26370 /* If that scope does not contain the scope in which the
26371 class was originally declared, the program is invalid. */
26372 if (scope && !is_ancestor (scope, nested_name_specifier))
26374 if (at_namespace_scope_p ())
26375 error_at (type_start_token->location,
26376 "declaration of %qD in namespace %qD which does not "
26377 "enclose %qD",
26378 type, scope, nested_name_specifier);
26379 else
26380 error_at (type_start_token->location,
26381 "declaration of %qD in %qD which does not enclose %qD",
26382 type, scope, nested_name_specifier);
26383 type = NULL_TREE;
26384 goto done;
26386 /* [dcl.meaning]
26388 A declarator-id shall not be qualified except for the
26389 definition of a ... nested class outside of its class
26390 ... [or] the definition or explicit instantiation of a
26391 class member of a namespace outside of its namespace. */
26392 if (scope == nested_name_specifier)
26393 permerror (nested_name_specifier_token_start->location,
26394 "extra qualification not allowed");
26396 /* An explicit-specialization must be preceded by "template <>". If
26397 it is not, try to recover gracefully. */
26398 if (at_namespace_scope_p ()
26399 && parser->num_template_parameter_lists == 0
26400 && !processing_template_parmlist
26401 && template_id_p)
26403 /* Build a location of this form:
26404 struct typename <ARGS>
26405 ^~~~~~~~~~~~~~~~~~~~~~
26406 with caret==start at the start token, and
26407 finishing at the end of the type. */
26408 location_t reported_loc
26409 = make_location (class_head_start_location,
26410 class_head_start_location,
26411 get_finish (type_start_token->location));
26412 rich_location richloc (line_table, reported_loc);
26413 richloc.add_fixit_insert_before (class_head_start_location,
26414 "template <> ");
26415 error_at (&richloc,
26416 "an explicit specialization must be preceded by"
26417 " %<template <>%>");
26418 invalid_explicit_specialization_p = true;
26419 /* Take the same action that would have been taken by
26420 cp_parser_explicit_specialization. */
26421 ++parser->num_template_parameter_lists;
26422 begin_specialization ();
26424 /* There must be no "return" statements between this point and the
26425 end of this function; set "type "to the correct return value and
26426 use "goto done;" to return. */
26427 /* Make sure that the right number of template parameters were
26428 present. */
26429 if (!cp_parser_check_template_parameters (parser, num_templates,
26430 template_id_p,
26431 type_start_token->location,
26432 /*declarator=*/NULL))
26434 /* If something went wrong, there is no point in even trying to
26435 process the class-definition. */
26436 type = NULL_TREE;
26437 goto done;
26440 /* Look up the type. */
26441 if (template_id_p)
26443 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
26444 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
26445 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
26447 error_at (type_start_token->location,
26448 "function template %qD redeclared as a class template", id);
26449 type = error_mark_node;
26451 else
26453 type = TREE_TYPE (id);
26454 type = maybe_process_partial_specialization (type);
26456 /* Check the scope while we still know whether or not we had a
26457 nested-name-specifier. */
26458 if (type != error_mark_node)
26459 check_unqualified_spec_or_inst (type, type_start_token->location);
26461 if (nested_name_specifier)
26462 pushed_scope = push_scope (nested_name_specifier);
26464 else if (nested_name_specifier)
26466 tree class_type;
26468 /* Given:
26470 template <typename T> struct S { struct T };
26471 template <typename T> struct S<T>::T { };
26473 we will get a TYPENAME_TYPE when processing the definition of
26474 `S::T'. We need to resolve it to the actual type before we
26475 try to define it. */
26476 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
26478 class_type = resolve_typename_type (TREE_TYPE (type),
26479 /*only_current_p=*/false);
26480 if (TREE_CODE (class_type) != TYPENAME_TYPE)
26481 type = TYPE_NAME (class_type);
26482 else
26484 cp_parser_error (parser, "could not resolve typename type");
26485 type = error_mark_node;
26489 if (maybe_process_partial_specialization (TREE_TYPE (type))
26490 == error_mark_node)
26492 type = NULL_TREE;
26493 goto done;
26496 class_type = current_class_type;
26497 /* Enter the scope indicated by the nested-name-specifier. */
26498 pushed_scope = push_scope (nested_name_specifier);
26499 /* Get the canonical version of this type. */
26500 type = TYPE_MAIN_DECL (TREE_TYPE (type));
26501 /* Call push_template_decl if it seems like we should be defining a
26502 template either from the template headers or the type we're
26503 defining, so that we diagnose both extra and missing headers. */
26504 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26505 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
26506 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
26508 type = push_template_decl (type);
26509 if (type == error_mark_node)
26511 type = NULL_TREE;
26512 goto done;
26516 type = TREE_TYPE (type);
26517 *nested_name_specifier_p = true;
26519 else /* The name is not a nested name. */
26521 /* If the class was unnamed, create a dummy name. */
26522 if (!id)
26523 id = make_anon_name ();
26524 TAG_how how = (parser->in_type_id_in_expr_p
26525 ? TAG_how::INNERMOST_NON_CLASS
26526 : TAG_how::CURRENT_ONLY);
26527 type = xref_tag (class_key, id, how,
26528 parser->num_template_parameter_lists);
26531 /* Diagnose class/struct/union mismatches. */
26532 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
26533 true, true);
26535 /* Indicate whether this class was declared as a `class' or as a
26536 `struct'. */
26537 if (TREE_CODE (type) == RECORD_TYPE)
26538 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
26540 /* If this type was already complete, and we see another definition,
26541 that's an error. Likewise if the type is already being defined:
26542 this can happen, eg, when it's defined from within an expression
26543 (c++/84605). */
26544 if (type != error_mark_node
26545 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
26547 error_at (type_start_token->location, "redefinition of %q#T",
26548 type);
26549 inform (location_of (type), "previous definition of %q#T",
26550 type);
26551 type = NULL_TREE;
26552 goto done;
26554 else if (type == error_mark_node)
26555 type = NULL_TREE;
26557 if (type)
26559 /* Apply attributes now, before any use of the class as a template
26560 argument in its base list. */
26561 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
26562 fixup_attribute_variants (type);
26565 /* Associate constraints with the type. */
26566 if (flag_concepts)
26567 type = associate_classtype_constraints (type);
26569 /* We will have entered the scope containing the class; the names of
26570 base classes should be looked up in that context. For example:
26572 struct A { struct B {}; struct C; };
26573 struct A::C : B {};
26575 is valid. */
26577 /* Get the list of base-classes, if there is one. Defer access checking
26578 until the entire list has been seen, as per [class.access.general]. */
26579 push_deferring_access_checks (dk_deferred);
26580 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26582 if (type)
26583 pushclass (type);
26584 bases = cp_parser_base_clause (parser);
26585 if (type)
26586 popclass ();
26588 else
26589 bases = NULL_TREE;
26591 /* If we're really defining a class, process the base classes.
26592 If they're invalid, fail. */
26593 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26594 xref_basetypes (type, bases);
26596 /* Now that all bases have been seen and attached to the class, check
26597 accessibility of the types named in the base-clause. This must be
26598 done relative to the class scope, so that we accept e.g.
26600 struct A { protected: struct B {}; };
26601 struct C : A::B, A {}; // OK: A::B is accessible via base A
26603 as per [class.access.general]. */
26604 if (type)
26605 pushclass (type);
26606 pop_to_parent_deferring_access_checks ();
26607 if (type)
26608 popclass ();
26610 done:
26611 /* Leave the scope given by the nested-name-specifier. We will
26612 enter the class scope itself while processing the members. */
26613 if (pushed_scope)
26614 pop_scope (pushed_scope);
26616 if (invalid_explicit_specialization_p)
26618 end_specialization ();
26619 --parser->num_template_parameter_lists;
26622 if (type)
26623 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
26624 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
26625 CLASSTYPE_FINAL (type) = 1;
26626 out:
26627 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26628 return type;
26631 /* Parse a class-key.
26633 class-key:
26634 class
26635 struct
26636 union
26638 Returns the kind of class-key specified, or none_type to indicate
26639 error. */
26641 static enum tag_types
26642 cp_parser_class_key (cp_parser* parser)
26644 cp_token *token;
26645 enum tag_types tag_type;
26647 /* Look for the class-key. */
26648 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
26649 if (!token)
26650 return none_type;
26652 /* Check to see if the TOKEN is a class-key. */
26653 tag_type = cp_parser_token_is_class_key (token);
26654 if (!tag_type)
26655 cp_parser_error (parser, "expected class-key");
26656 return tag_type;
26659 /* Parse a type-parameter-key.
26661 type-parameter-key:
26662 class
26663 typename
26666 static void
26667 cp_parser_type_parameter_key (cp_parser* parser)
26669 /* Look for the type-parameter-key. */
26670 enum tag_types tag_type = none_type;
26671 cp_token *token = cp_lexer_peek_token (parser->lexer);
26672 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
26674 cp_lexer_consume_token (parser->lexer);
26675 if (pedantic && tag_type == typename_type
26676 && cxx_dialect < cxx17)
26677 /* typename is not allowed in a template template parameter
26678 by the standard until C++17. */
26679 pedwarn (token->location, OPT_Wc__17_extensions,
26680 "ISO C++ forbids typename key in template template parameter;"
26681 " use %<-std=c++17%> or %<-std=gnu++17%>");
26683 else
26684 cp_parser_error (parser, "expected %<class%> or %<typename%>");
26686 return;
26689 /* Parse an (optional) member-specification.
26691 member-specification:
26692 member-declaration member-specification [opt]
26693 access-specifier : member-specification [opt] */
26695 static void
26696 cp_parser_member_specification_opt (cp_parser* parser)
26698 while (true)
26700 cp_token *token;
26701 enum rid keyword;
26703 /* Peek at the next token. */
26704 token = cp_lexer_peek_token (parser->lexer);
26705 /* If it's a `}', or EOF then we've seen all the members. */
26706 if (token->type == CPP_CLOSE_BRACE
26707 || token->type == CPP_EOF
26708 || token->type == CPP_PRAGMA_EOL)
26709 break;
26711 /* See if this token is a keyword. */
26712 keyword = token->keyword;
26713 switch (keyword)
26715 case RID_PUBLIC:
26716 case RID_PROTECTED:
26717 case RID_PRIVATE:
26718 /* Consume the access-specifier. */
26719 cp_lexer_consume_token (parser->lexer);
26720 /* Remember which access-specifier is active. */
26721 current_access_specifier = token->u.value;
26722 /* Look for the `:'. */
26723 cp_parser_require (parser, CPP_COLON, RT_COLON);
26724 break;
26726 default:
26727 /* Accept #pragmas at class scope. */
26728 if (token->type == CPP_PRAGMA)
26730 cp_parser_pragma (parser, pragma_member, NULL);
26731 break;
26734 /* Otherwise, the next construction must be a
26735 member-declaration. */
26736 cp_parser_member_declaration (parser);
26741 /* Parse a member-declaration.
26743 member-declaration:
26744 decl-specifier-seq [opt] member-declarator-list [opt] ;
26745 function-definition ; [opt]
26746 :: [opt] nested-name-specifier template [opt] unqualified-id ;
26747 using-declaration
26748 template-declaration
26749 alias-declaration
26751 member-declarator-list:
26752 member-declarator
26753 member-declarator-list , member-declarator
26755 member-declarator:
26756 declarator pure-specifier [opt]
26757 declarator constant-initializer [opt]
26758 identifier [opt] : constant-expression
26760 GNU Extensions:
26762 member-declaration:
26763 __extension__ member-declaration
26765 member-declarator:
26766 declarator attributes [opt] pure-specifier [opt]
26767 declarator attributes [opt] constant-initializer [opt]
26768 identifier [opt] attributes [opt] : constant-expression
26770 C++0x Extensions:
26772 member-declaration:
26773 static_assert-declaration */
26775 static void
26776 cp_parser_member_declaration (cp_parser* parser)
26778 cp_decl_specifier_seq decl_specifiers;
26779 tree prefix_attributes;
26780 tree decl;
26781 int declares_class_or_enum;
26782 bool friend_p;
26783 cp_token *token = NULL;
26784 cp_token *decl_spec_token_start = NULL;
26785 cp_token *initializer_token_start = NULL;
26786 int saved_pedantic;
26787 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26789 /* Check for the `__extension__' keyword. */
26790 if (cp_parser_extension_opt (parser, &saved_pedantic))
26792 /* Recurse. */
26793 cp_parser_member_declaration (parser);
26794 /* Restore the old value of the PEDANTIC flag. */
26795 pedantic = saved_pedantic;
26797 return;
26800 /* Check for a template-declaration. */
26801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26803 /* An explicit specialization here is an error condition, and we
26804 expect the specialization handler to detect and report this. */
26805 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
26806 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
26807 cp_parser_explicit_specialization (parser);
26808 else
26809 cp_parser_template_declaration (parser, /*member_p=*/true);
26811 return;
26813 /* Check for a template introduction. */
26814 else if (cp_parser_template_declaration_after_export (parser, true))
26815 return;
26817 /* Check for a using-declaration. */
26818 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26820 if (cxx_dialect < cxx11)
26821 /* Parse the using-declaration. */
26822 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
26823 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
26824 cp_parser_using_enum (parser);
26825 else
26827 tree decl;
26828 bool alias_decl_expected;
26829 cp_parser_parse_tentatively (parser);
26830 decl = cp_parser_alias_declaration (parser);
26831 /* Note that if we actually see the '=' token after the
26832 identifier, cp_parser_alias_declaration commits the
26833 tentative parse. In that case, we really expect an
26834 alias-declaration. Otherwise, we expect a using
26835 declaration. */
26836 alias_decl_expected =
26837 !cp_parser_uncommitted_to_tentative_parse_p (parser);
26838 cp_parser_parse_definitely (parser);
26840 if (alias_decl_expected)
26841 finish_member_declaration (decl);
26842 else
26843 cp_parser_using_declaration (parser,
26844 /*access_declaration_p=*/false);
26846 return;
26849 /* Check for @defs. */
26850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
26852 tree ivar, member;
26853 tree ivar_chains = cp_parser_objc_defs_expression (parser);
26854 ivar = ivar_chains;
26855 while (ivar)
26857 member = ivar;
26858 ivar = TREE_CHAIN (member);
26859 TREE_CHAIN (member) = NULL_TREE;
26860 finish_member_declaration (member);
26862 return;
26865 /* If the next token is `static_assert' we have a static assertion. */
26866 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
26868 cp_parser_static_assert (parser, /*member_p=*/true);
26869 return;
26872 parser->colon_corrects_to_scope_p = false;
26874 cp_omp_declare_simd_data odsd;
26875 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
26876 goto out;
26878 /* Parse the decl-specifier-seq. */
26879 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26880 cp_parser_decl_specifier_seq (parser,
26881 (CP_PARSER_FLAGS_OPTIONAL
26882 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
26883 &decl_specifiers,
26884 &declares_class_or_enum);
26886 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
26887 cp_parser_handle_directive_omp_attributes (parser,
26888 &decl_specifiers.attributes,
26889 &odsd, true);
26891 /* Check for an invalid type-name. */
26892 if (!decl_specifiers.any_type_specifiers_p
26893 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26894 goto out;
26895 /* If there is no declarator, then the decl-specifier-seq should
26896 specify a type. */
26897 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26899 /* If there was no decl-specifier-seq, and the next token is a
26900 `;', then we have something like:
26902 struct S { ; };
26904 [class.mem]
26906 Each member-declaration shall declare at least one member
26907 name of the class. */
26908 if (!decl_specifiers.any_specifiers_p)
26910 cp_token *token = cp_lexer_peek_token (parser->lexer);
26911 if (!in_system_header_at (token->location))
26913 gcc_rich_location richloc (token->location);
26914 richloc.add_fixit_remove ();
26915 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
26918 else
26920 /* See if this declaration is a friend. */
26921 friend_p = cp_parser_friend_p (&decl_specifiers);
26922 /* If there were decl-specifiers, check to see if there was
26923 a class-declaration. */
26924 tree type = check_tag_decl (&decl_specifiers,
26925 /*explicit_type_instantiation_p=*/false);
26926 /* Nested classes have already been added to the class, but
26927 a `friend' needs to be explicitly registered. */
26928 if (friend_p)
26930 /* If the `friend' keyword was present, the friend must
26931 be introduced with a class-key. */
26932 if (!declares_class_or_enum && cxx_dialect < cxx11)
26933 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
26934 "in C++03 a class-key must be used "
26935 "when declaring a friend");
26936 /* In this case:
26938 template <typename T> struct A {
26939 friend struct A<T>::B;
26942 A<T>::B will be represented by a TYPENAME_TYPE, and
26943 therefore not recognized by check_tag_decl. */
26944 if (!type)
26946 type = decl_specifiers.type;
26947 if (type && TREE_CODE (type) == TYPE_DECL)
26948 type = TREE_TYPE (type);
26950 /* Warn if an attribute cannot appear here, as per
26951 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
26952 we ignore attributes in elaborated-type-specifiers. */
26953 if (!declares_class_or_enum
26954 && cxx11_attribute_p (decl_specifiers.attributes))
26956 decl_specifiers.attributes = NULL_TREE;
26957 if (warning_at (decl_spec_token_start->location,
26958 OPT_Wattributes, "attribute ignored"))
26959 inform (decl_spec_token_start->location, "an attribute "
26960 "that appertains to a friend declaration that "
26961 "is not a definition is ignored");
26963 if (!type || !TYPE_P (type))
26964 error_at (decl_spec_token_start->location,
26965 "friend declaration does not name a class or "
26966 "function");
26967 else
26968 make_friend_class (current_class_type, type,
26969 /*complain=*/true);
26971 /* If there is no TYPE, an error message will already have
26972 been issued. */
26973 else if (!type || type == error_mark_node)
26975 /* An anonymous aggregate has to be handled specially; such
26976 a declaration really declares a data member (with a
26977 particular type), as opposed to a nested class. */
26978 else if (ANON_AGGR_TYPE_P (type))
26980 /* C++11 9.5/6. */
26981 if (decl_specifiers.storage_class != sc_none)
26982 error_at (decl_spec_token_start->location,
26983 "a storage class on an anonymous aggregate "
26984 "in class scope is not allowed");
26986 /* Remove constructors and such from TYPE, now that we
26987 know it is an anonymous aggregate. */
26988 fixup_anonymous_aggr (type);
26989 /* And make the corresponding data member. */
26990 decl = build_decl (decl_spec_token_start->location,
26991 FIELD_DECL, NULL_TREE, type);
26992 /* Add it to the class. */
26993 finish_member_declaration (decl);
26995 else
26996 cp_parser_check_access_in_redeclaration
26997 (TYPE_NAME (type),
26998 decl_spec_token_start->location);
27001 else
27003 bool assume_semicolon = false;
27005 /* Clear attributes from the decl_specifiers but keep them
27006 around as prefix attributes that apply them to the entity
27007 being declared. */
27008 prefix_attributes = decl_specifiers.attributes;
27009 decl_specifiers.attributes = NULL_TREE;
27010 if (parser->omp_declare_simd
27011 && (parser->omp_declare_simd->attribs[0]
27012 == &decl_specifiers.attributes))
27013 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27015 /* See if these declarations will be friends. */
27016 friend_p = cp_parser_friend_p (&decl_specifiers);
27018 /* Keep going until we hit the `;' at the end of the
27019 declaration. */
27020 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27022 tree attributes = NULL_TREE;
27023 tree first_attribute;
27024 tree initializer;
27025 bool named_bitfld = false;
27027 /* Peek at the next token. */
27028 token = cp_lexer_peek_token (parser->lexer);
27030 /* The following code wants to know early if it is a bit-field
27031 or some other declaration. Attributes can appear before
27032 the `:' token. Skip over them without consuming any tokens
27033 to peek if they are followed by `:'. */
27034 if (cp_next_tokens_can_be_attribute_p (parser)
27035 || (token->type == CPP_NAME
27036 && cp_nth_tokens_can_be_attribute_p (parser, 2)
27037 && (named_bitfld = true)))
27039 size_t n
27040 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27041 token = cp_lexer_peek_nth_token (parser->lexer, n);
27044 /* Check for a bitfield declaration. */
27045 if (token->type == CPP_COLON
27046 || (token->type == CPP_NAME
27047 && token == cp_lexer_peek_token (parser->lexer)
27048 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27049 && (named_bitfld = true)))
27051 tree identifier;
27052 tree width;
27053 tree late_attributes = NULL_TREE;
27054 location_t id_location
27055 = cp_lexer_peek_token (parser->lexer)->location;
27057 if (named_bitfld)
27058 identifier = cp_parser_identifier (parser);
27059 else
27060 identifier = NULL_TREE;
27062 /* Look for attributes that apply to the bitfield. */
27063 attributes = cp_parser_attributes_opt (parser);
27065 /* Consume the `:' token. */
27066 cp_lexer_consume_token (parser->lexer);
27068 /* Get the width of the bitfield. */
27069 width = cp_parser_constant_expression (parser, false, NULL,
27070 cxx_dialect >= cxx11);
27072 /* In C++20 and as extension for C++11 and above we allow
27073 default member initializers for bit-fields. */
27074 initializer = NULL_TREE;
27075 if (cxx_dialect >= cxx11
27076 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27077 || cp_lexer_next_token_is (parser->lexer,
27078 CPP_OPEN_BRACE)))
27080 location_t loc
27081 = cp_lexer_peek_token (parser->lexer)->location;
27082 if (cxx_dialect < cxx20
27083 && identifier != NULL_TREE)
27084 pedwarn (loc, OPT_Wc__20_extensions,
27085 "default member initializers for bit-fields "
27086 "only available with %<-std=c++20%> or "
27087 "%<-std=gnu++20%>");
27089 initializer = cp_parser_save_nsdmi (parser);
27090 if (identifier == NULL_TREE)
27092 error_at (loc, "default member initializer for "
27093 "unnamed bit-field");
27094 initializer = NULL_TREE;
27097 else
27099 /* Look for attributes that apply to the bitfield after
27100 the `:' token and width. This is where GCC used to
27101 parse attributes in the past, pedwarn if there is
27102 a std attribute. */
27103 if (cp_next_tokens_can_be_std_attribute_p (parser))
27104 pedwarn (input_location, OPT_Wpedantic,
27105 "ISO C++ allows bit-field attributes only "
27106 "before the %<:%> token");
27108 late_attributes = cp_parser_attributes_opt (parser);
27111 attributes = attr_chainon (attributes, late_attributes);
27113 /* Remember which attributes are prefix attributes and
27114 which are not. */
27115 first_attribute = attributes;
27116 /* Combine the attributes. */
27117 attributes = attr_chainon (prefix_attributes, attributes);
27119 /* Create the bitfield declaration. */
27120 decl = grokbitfield (identifier
27121 ? make_id_declarator (NULL_TREE,
27122 identifier,
27123 sfk_none,
27124 id_location)
27125 : NULL,
27126 &decl_specifiers,
27127 width, initializer,
27128 attributes);
27130 else
27132 cp_declarator *declarator;
27133 tree asm_specification;
27134 int ctor_dtor_or_conv_p;
27135 bool static_p = (decl_specifiers.storage_class == sc_static);
27136 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27137 /* We can't delay parsing for friends,
27138 alias-declarations, and typedefs, even though the
27139 standard seems to require it. */
27140 if (!friend_p
27141 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27142 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27144 /* Parse the declarator. */
27145 declarator
27146 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27147 flags,
27148 &ctor_dtor_or_conv_p,
27149 /*parenthesized_p=*/NULL,
27150 /*member_p=*/true,
27151 friend_p, static_p);
27153 /* If something went wrong parsing the declarator, make sure
27154 that we at least consume some tokens. */
27155 if (declarator == cp_error_declarator)
27157 /* Skip to the end of the statement. */
27158 cp_parser_skip_to_end_of_statement (parser);
27159 /* If the next token is not a semicolon, that is
27160 probably because we just skipped over the body of
27161 a function. So, we consume a semicolon if
27162 present, but do not issue an error message if it
27163 is not present. */
27164 if (cp_lexer_next_token_is (parser->lexer,
27165 CPP_SEMICOLON))
27166 cp_lexer_consume_token (parser->lexer);
27167 goto out;
27170 /* Handle class-scope non-template C++17 deduction guides. */
27171 cp_parser_maybe_adjust_declarator_for_dguide (parser,
27172 &decl_specifiers,
27173 declarator,
27174 &ctor_dtor_or_conv_p);
27176 if (declares_class_or_enum & 2)
27177 cp_parser_check_for_definition_in_return_type
27178 (declarator, decl_specifiers.type,
27179 decl_specifiers.locations[ds_type_spec]);
27181 /* Look for an asm-specification. */
27182 asm_specification = cp_parser_asm_specification_opt (parser);
27183 /* Look for attributes that apply to the declaration. */
27184 attributes = cp_parser_attributes_opt (parser);
27185 /* Remember which attributes are prefix attributes and
27186 which are not. */
27187 first_attribute = attributes;
27188 /* Combine the attributes. */
27189 attributes = attr_chainon (prefix_attributes, attributes);
27191 /* If it's an `=', then we have a constant-initializer or a
27192 pure-specifier. It is not correct to parse the
27193 initializer before registering the member declaration
27194 since the member declaration should be in scope while
27195 its initializer is processed. However, the rest of the
27196 front end does not yet provide an interface that allows
27197 us to handle this correctly. */
27198 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27200 /* In [class.mem]:
27202 A pure-specifier shall be used only in the declaration of
27203 a virtual function.
27205 A member-declarator can contain a constant-initializer
27206 only if it declares a static member of integral or
27207 enumeration type.
27209 Therefore, if the DECLARATOR is for a function, we look
27210 for a pure-specifier; otherwise, we look for a
27211 constant-initializer. When we call `grokfield', it will
27212 perform more stringent semantics checks. */
27213 initializer_token_start = cp_lexer_peek_token (parser->lexer);
27214 declarator->init_loc = initializer_token_start->location;
27215 if (function_declarator_p (declarator)
27216 || (decl_specifiers.type
27217 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27218 && declarator->kind == cdk_id
27219 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27220 == FUNCTION_TYPE)))
27221 initializer = cp_parser_pure_specifier (parser);
27222 else if (decl_specifiers.storage_class != sc_static)
27223 initializer = cp_parser_save_nsdmi (parser);
27224 else if (cxx_dialect >= cxx11)
27226 bool nonconst;
27227 /* Don't require a constant rvalue in C++11, since we
27228 might want a reference constant. We'll enforce
27229 constancy later. */
27230 cp_lexer_consume_token (parser->lexer);
27231 /* Parse the initializer. */
27232 initializer = cp_parser_initializer_clause (parser,
27233 &nonconst);
27235 else
27236 /* Parse the initializer. */
27237 initializer = cp_parser_constant_initializer (parser);
27239 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27240 && !function_declarator_p (declarator))
27242 bool x;
27243 declarator->init_loc
27244 = cp_lexer_peek_token (parser->lexer)->location;
27245 if (decl_specifiers.storage_class != sc_static)
27246 initializer = cp_parser_save_nsdmi (parser);
27247 else
27248 initializer = cp_parser_initializer (parser, &x, &x);
27250 /* Detect invalid bit-field cases such as
27252 int *p : 4;
27253 int &&r : 3;
27255 and similar. */
27256 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27257 /* If there were no type specifiers, it was a
27258 constructor. */
27259 && decl_specifiers.any_type_specifiers_p)
27261 /* This is called for a decent diagnostic only. */
27262 tree d = grokdeclarator (declarator, &decl_specifiers,
27263 BITFIELD, /*initialized=*/false,
27264 &attributes);
27265 if (!error_operand_p (d))
27266 error_at (DECL_SOURCE_LOCATION (d),
27267 "bit-field %qD has non-integral type %qT",
27268 d, TREE_TYPE (d));
27269 cp_parser_skip_to_end_of_statement (parser);
27270 /* Avoid "extra ;" pedwarns. */
27271 if (cp_lexer_next_token_is (parser->lexer,
27272 CPP_SEMICOLON))
27273 cp_lexer_consume_token (parser->lexer);
27274 goto out;
27276 /* Otherwise, there is no initializer. */
27277 else
27278 initializer = NULL_TREE;
27280 /* See if we are probably looking at a function
27281 definition. We are certainly not looking at a
27282 member-declarator. Calling `grokfield' has
27283 side-effects, so we must not do it unless we are sure
27284 that we are looking at a member-declarator. */
27285 if (cp_parser_token_starts_function_definition_p
27286 (cp_lexer_peek_token (parser->lexer)))
27288 /* The grammar does not allow a pure-specifier to be
27289 used when a member function is defined. (It is
27290 possible that this fact is an oversight in the
27291 standard, since a pure function may be defined
27292 outside of the class-specifier. */
27293 if (initializer && initializer_token_start)
27294 error_at (initializer_token_start->location,
27295 "pure-specifier on function-definition");
27296 decl = cp_parser_save_member_function_body (parser,
27297 &decl_specifiers,
27298 declarator,
27299 attributes);
27300 if (parser->fully_implicit_function_template_p)
27301 decl = finish_fully_implicit_template (parser, decl);
27302 /* If the member was not a friend, declare it here. */
27303 if (!friend_p)
27304 finish_member_declaration (decl);
27305 /* Peek at the next token. */
27306 token = cp_lexer_peek_token (parser->lexer);
27307 /* If the next token is a semicolon, consume it. */
27308 if (token->type == CPP_SEMICOLON)
27310 location_t semicolon_loc
27311 = cp_lexer_consume_token (parser->lexer)->location;
27312 gcc_rich_location richloc (semicolon_loc);
27313 richloc.add_fixit_remove ();
27314 warning_at (&richloc, OPT_Wextra_semi,
27315 "extra %<;%> after in-class "
27316 "function definition");
27318 goto out;
27320 else
27321 if (declarator->kind == cdk_function)
27322 declarator->id_loc = token->location;
27323 /* Create the declaration. */
27324 decl = grokfield (declarator, &decl_specifiers,
27325 initializer, /*init_const_expr_p=*/true,
27326 asm_specification, attributes);
27327 if (parser->fully_implicit_function_template_p)
27329 if (friend_p)
27330 finish_fully_implicit_template (parser, 0);
27331 else
27332 decl = finish_fully_implicit_template (parser, decl);
27336 cp_finalize_omp_declare_simd (parser, decl);
27337 cp_finalize_oacc_routine (parser, decl, false);
27339 /* Reset PREFIX_ATTRIBUTES. */
27340 if (attributes != error_mark_node)
27342 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27343 attributes = TREE_CHAIN (attributes);
27344 if (attributes)
27345 TREE_CHAIN (attributes) = NULL_TREE;
27348 /* If there is any qualification still in effect, clear it
27349 now; we will be starting fresh with the next declarator. */
27350 parser->scope = NULL_TREE;
27351 parser->qualifying_scope = NULL_TREE;
27352 parser->object_scope = NULL_TREE;
27353 /* If it's a `,', then there are more declarators. */
27354 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27356 cp_lexer_consume_token (parser->lexer);
27357 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27359 cp_token *token = cp_lexer_previous_token (parser->lexer);
27360 gcc_rich_location richloc (token->location);
27361 richloc.add_fixit_remove ();
27362 error_at (&richloc, "stray %<,%> at end of "
27363 "member declaration");
27366 /* If the next token isn't a `;', then we have a parse error. */
27367 else if (cp_lexer_next_token_is_not (parser->lexer,
27368 CPP_SEMICOLON))
27370 /* The next token might be a ways away from where the
27371 actual semicolon is missing. Find the previous token
27372 and use that for our error position. */
27373 cp_token *token = cp_lexer_previous_token (parser->lexer);
27374 gcc_rich_location richloc (token->location);
27375 richloc.add_fixit_insert_after (";");
27376 error_at (&richloc, "expected %<;%> at end of "
27377 "member declaration");
27379 /* Assume that the user meant to provide a semicolon. If
27380 we were to cp_parser_skip_to_end_of_statement, we might
27381 skip to a semicolon inside a member function definition
27382 and issue nonsensical error messages. */
27383 assume_semicolon = true;
27386 if (decl)
27388 /* Add DECL to the list of members. */
27389 if (!friend_p
27390 /* Explicitly include, eg, NSDMIs, for better error
27391 recovery (c++/58650). */
27392 || !DECL_DECLARES_FUNCTION_P (decl))
27393 finish_member_declaration (decl);
27395 if (DECL_DECLARES_FUNCTION_P (decl))
27396 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
27397 else if (TREE_CODE (decl) == FIELD_DECL
27398 && DECL_INITIAL (decl))
27399 /* Add DECL to the queue of NSDMI to be parsed later. */
27400 vec_safe_push (unparsed_nsdmis, decl);
27403 if (assume_semicolon)
27404 goto out;
27408 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27409 out:
27410 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27411 cp_finalize_omp_declare_simd (parser, &odsd);
27414 /* Parse a pure-specifier.
27416 pure-specifier:
27419 Returns INTEGER_ZERO_NODE if a pure specifier is found.
27420 Otherwise, ERROR_MARK_NODE is returned. */
27422 static tree
27423 cp_parser_pure_specifier (cp_parser* parser)
27425 cp_token *token;
27427 /* Look for the `=' token. */
27428 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27429 return error_mark_node;
27430 /* Look for the `0' token. */
27431 token = cp_lexer_peek_token (parser->lexer);
27433 if (token->type == CPP_EOF
27434 || token->type == CPP_PRAGMA_EOL)
27435 return error_mark_node;
27437 cp_lexer_consume_token (parser->lexer);
27439 /* Accept = default or = delete in c++0x mode. */
27440 if (token->keyword == RID_DEFAULT
27441 || token->keyword == RID_DELETE)
27443 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
27444 return token->u.value;
27447 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
27448 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
27450 cp_parser_error (parser,
27451 "invalid pure specifier (only %<= 0%> is allowed)");
27452 cp_parser_skip_to_end_of_statement (parser);
27453 return error_mark_node;
27455 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27457 error_at (token->location, "templates may not be %<virtual%>");
27458 return error_mark_node;
27461 return integer_zero_node;
27464 /* Parse a constant-initializer.
27466 constant-initializer:
27467 = constant-expression
27469 Returns a representation of the constant-expression. */
27471 static tree
27472 cp_parser_constant_initializer (cp_parser* parser)
27474 /* Look for the `=' token. */
27475 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27476 return error_mark_node;
27478 /* It is invalid to write:
27480 struct S { static const int i = { 7 }; };
27483 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27485 cp_parser_error (parser,
27486 "a brace-enclosed initializer is not allowed here");
27487 /* Consume the opening brace. */
27488 matching_braces braces;
27489 braces.consume_open (parser);
27490 /* Skip the initializer. */
27491 cp_parser_skip_to_closing_brace (parser);
27492 /* Look for the trailing `}'. */
27493 braces.require_close (parser);
27495 return error_mark_node;
27498 return cp_parser_constant_expression (parser);
27501 /* Derived classes [gram.class.derived] */
27503 /* Parse a base-clause.
27505 base-clause:
27506 : base-specifier-list
27508 base-specifier-list:
27509 base-specifier ... [opt]
27510 base-specifier-list , base-specifier ... [opt]
27512 Returns a TREE_LIST representing the base-classes, in the order in
27513 which they were declared. The representation of each node is as
27514 described by cp_parser_base_specifier.
27516 In the case that no bases are specified, this function will return
27517 NULL_TREE, not ERROR_MARK_NODE. */
27519 static tree
27520 cp_parser_base_clause (cp_parser* parser)
27522 tree bases = NULL_TREE;
27524 /* Look for the `:' that begins the list. */
27525 cp_parser_require (parser, CPP_COLON, RT_COLON);
27527 /* Scan the base-specifier-list. */
27528 while (true)
27530 cp_token *token;
27531 tree base;
27532 bool pack_expansion_p = false;
27534 /* Look for the base-specifier. */
27535 base = cp_parser_base_specifier (parser);
27536 /* Look for the (optional) ellipsis. */
27537 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27539 /* Consume the `...'. */
27540 cp_lexer_consume_token (parser->lexer);
27542 pack_expansion_p = true;
27545 /* Add BASE to the front of the list. */
27546 if (base && base != error_mark_node)
27548 if (pack_expansion_p)
27549 /* Make this a pack expansion type. */
27550 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
27552 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
27554 TREE_CHAIN (base) = bases;
27555 bases = base;
27558 /* Peek at the next token. */
27559 token = cp_lexer_peek_token (parser->lexer);
27560 /* If it's not a comma, then the list is complete. */
27561 if (token->type != CPP_COMMA)
27562 break;
27563 /* Consume the `,'. */
27564 cp_lexer_consume_token (parser->lexer);
27567 /* PARSER->SCOPE may still be non-NULL at this point, if the last
27568 base class had a qualified name. However, the next name that
27569 appears is certainly not qualified. */
27570 parser->scope = NULL_TREE;
27571 parser->qualifying_scope = NULL_TREE;
27572 parser->object_scope = NULL_TREE;
27574 return nreverse (bases);
27577 /* Parse a base-specifier.
27579 base-specifier:
27580 :: [opt] nested-name-specifier [opt] class-name
27581 virtual access-specifier [opt] :: [opt] nested-name-specifier
27582 [opt] class-name
27583 access-specifier virtual [opt] :: [opt] nested-name-specifier
27584 [opt] class-name
27586 Returns a TREE_LIST. The TREE_PURPOSE will be one of
27587 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
27588 indicate the specifiers provided. The TREE_VALUE will be a TYPE
27589 (or the ERROR_MARK_NODE) indicating the type that was specified. */
27591 static tree
27592 cp_parser_base_specifier (cp_parser* parser)
27594 cp_token *token;
27595 bool done = false;
27596 bool virtual_p = false;
27597 bool duplicate_virtual_error_issued_p = false;
27598 bool duplicate_access_error_issued_p = false;
27599 bool class_scope_p, template_p;
27600 tree access = access_default_node;
27601 tree type;
27603 /* Process the optional `virtual' and `access-specifier'. */
27604 while (!done)
27606 /* Peek at the next token. */
27607 token = cp_lexer_peek_token (parser->lexer);
27608 /* Process `virtual'. */
27609 switch (token->keyword)
27611 case RID_VIRTUAL:
27612 /* If `virtual' appears more than once, issue an error. */
27613 if (virtual_p && !duplicate_virtual_error_issued_p)
27615 cp_parser_error (parser,
27616 "%<virtual%> specified more than once in base-specifier");
27617 duplicate_virtual_error_issued_p = true;
27620 virtual_p = true;
27622 /* Consume the `virtual' token. */
27623 cp_lexer_consume_token (parser->lexer);
27625 break;
27627 case RID_PUBLIC:
27628 case RID_PROTECTED:
27629 case RID_PRIVATE:
27630 /* If more than one access specifier appears, issue an
27631 error. */
27632 if (access != access_default_node
27633 && !duplicate_access_error_issued_p)
27635 cp_parser_error (parser,
27636 "more than one access specifier in base-specifier");
27637 duplicate_access_error_issued_p = true;
27640 access = ridpointers[(int) token->keyword];
27642 /* Consume the access-specifier. */
27643 cp_lexer_consume_token (parser->lexer);
27645 break;
27647 default:
27648 done = true;
27649 break;
27652 /* It is not uncommon to see programs mechanically, erroneously, use
27653 the 'typename' keyword to denote (dependent) qualified types
27654 as base classes. */
27655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27657 token = cp_lexer_peek_token (parser->lexer);
27658 if (!processing_template_decl)
27659 error_at (token->location,
27660 "keyword %<typename%> not allowed outside of templates");
27661 else
27662 error_at (token->location,
27663 "keyword %<typename%> not allowed in this context "
27664 "(the base class is implicitly a type)");
27665 cp_lexer_consume_token (parser->lexer);
27668 /* Look for the optional `::' operator. */
27669 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
27670 /* Look for the nested-name-specifier. The simplest way to
27671 implement:
27673 [temp.res]
27675 The keyword `typename' is not permitted in a base-specifier or
27676 mem-initializer; in these contexts a qualified name that
27677 depends on a template-parameter is implicitly assumed to be a
27678 type name.
27680 is to pretend that we have seen the `typename' keyword at this
27681 point. */
27682 cp_parser_nested_name_specifier_opt (parser,
27683 /*typename_keyword_p=*/true,
27684 /*check_dependency_p=*/true,
27685 /*type_p=*/true,
27686 /*is_declaration=*/true);
27687 /* If the base class is given by a qualified name, assume that names
27688 we see are type names or templates, as appropriate. */
27689 class_scope_p = (parser->scope && TYPE_P (parser->scope));
27690 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
27692 if (!parser->scope
27693 && cp_lexer_next_token_is_decltype (parser->lexer))
27694 /* DR 950 allows decltype as a base-specifier. */
27695 type = cp_parser_decltype (parser);
27696 else
27698 /* Otherwise, look for the class-name. */
27699 type = cp_parser_class_name (parser,
27700 class_scope_p,
27701 template_p,
27702 typename_type,
27703 /*check_dependency_p=*/true,
27704 /*class_head_p=*/false,
27705 /*is_declaration=*/true);
27706 type = TREE_TYPE (type);
27709 if (type == error_mark_node)
27710 return error_mark_node;
27712 return finish_base_specifier (type, access, virtual_p);
27715 /* Exception handling [gram.exception] */
27717 /* Save the tokens that make up the noexcept-specifier for a member-function.
27718 Returns a DEFERRED_PARSE. */
27720 static tree
27721 cp_parser_save_noexcept (cp_parser *parser)
27723 cp_token *first = parser->lexer->next_token;
27724 /* We want everything up to, including, the final ')'. */
27725 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
27726 cp_token *last = parser->lexer->next_token;
27728 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
27729 to carry the information we will need. */
27730 tree expr = make_node (DEFERRED_PARSE);
27731 /* Save away the noexcept-specifier; we will process it when the
27732 class is complete. */
27733 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
27734 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
27735 expr = build_tree_list (expr, NULL_TREE);
27736 return expr;
27739 /* Used for late processing of noexcept-specifiers of member-functions.
27740 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
27741 we saved for later; parse it now. DECL is the declaration of the
27742 member function. */
27744 static tree
27745 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
27747 /* Make sure we've gotten something that hasn't been parsed yet. */
27748 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
27750 push_unparsed_function_queues (parser);
27752 /* Push the saved tokens for the noexcept-specifier onto the parser's
27753 lexer stack. */
27754 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
27755 cp_parser_push_lexer_for_tokens (parser, tokens);
27757 /* Parse the cached noexcept-specifier. */
27758 tree parsed_arg
27759 = cp_parser_noexcept_specification_opt (parser,
27760 CP_PARSER_FLAGS_NONE,
27761 /*require_constexpr=*/true,
27762 /*consumed_expr=*/NULL,
27763 /*return_cond=*/false);
27765 /* Revert to the main lexer. */
27766 cp_parser_pop_lexer (parser);
27768 /* Restore the queue. */
27769 pop_unparsed_function_queues (parser);
27771 /* And we're done. */
27772 return parsed_arg;
27775 /* Perform late checking of overriding function with respect to their
27776 noexcept-specifiers. TYPE is the class and FNDECL is the function
27777 that potentially overrides some virtual function with the same
27778 signature. */
27780 static void
27781 noexcept_override_late_checks (tree type, tree fndecl)
27783 tree binfo = TYPE_BINFO (type);
27784 tree base_binfo;
27786 if (DECL_STATIC_FUNCTION_P (fndecl))
27787 return;
27789 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
27791 tree basetype = BINFO_TYPE (base_binfo);
27793 if (!TYPE_POLYMORPHIC_P (basetype))
27794 continue;
27796 tree fn = look_for_overrides_here (basetype, fndecl);
27797 if (fn)
27798 maybe_check_overriding_exception_spec (fndecl, fn);
27802 /* Parse an (optional) noexcept-specification.
27804 noexcept-specification:
27805 noexcept ( constant-expression ) [opt]
27807 If no noexcept-specification is present, returns NULL_TREE.
27808 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
27809 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
27810 there are no parentheses. CONSUMED_EXPR will be set accordingly.
27811 Otherwise, returns a noexcept specification unless RETURN_COND is true,
27812 in which case a boolean condition is returned instead. The parser flags
27813 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
27814 the (member) function is `const'. */
27816 static tree
27817 cp_parser_noexcept_specification_opt (cp_parser* parser,
27818 cp_parser_flags flags,
27819 bool require_constexpr,
27820 bool* consumed_expr,
27821 bool return_cond)
27823 cp_token *token;
27824 const char *saved_message;
27826 /* Peek at the next token. */
27827 token = cp_lexer_peek_token (parser->lexer);
27829 /* Is it a noexcept-specification? */
27830 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
27832 tree expr;
27834 /* [class.mem]/6 says that a noexcept-specifer (within the
27835 member-specification of the class) is a complete-class context of
27836 a class. So, if the noexcept-specifier has the optional expression,
27837 just save the tokens, and reparse this after we're done with the
27838 class. */
27840 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
27841 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
27842 /* No need to delay parsing for a number literal or true/false. */
27843 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
27844 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27845 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
27846 && at_class_scope_p ()
27847 && TYPE_BEING_DEFINED (current_class_type)
27848 && !LAMBDA_TYPE_P (current_class_type))
27849 return cp_parser_save_noexcept (parser);
27851 cp_lexer_consume_token (parser->lexer);
27853 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27855 matching_parens parens;
27856 parens.consume_open (parser);
27858 if (require_constexpr)
27860 /* Types may not be defined in an exception-specification. */
27861 saved_message = parser->type_definition_forbidden_message;
27862 parser->type_definition_forbidden_message
27863 = G_("types may not be defined in an exception-specification");
27865 bool non_constant_p;
27866 expr
27867 = cp_parser_constant_expression (parser,
27868 /*allow_non_constant=*/true,
27869 &non_constant_p);
27870 if (non_constant_p
27871 && !require_potential_rvalue_constant_expression (expr))
27873 expr = NULL_TREE;
27874 return_cond = true;
27877 /* Restore the saved message. */
27878 parser->type_definition_forbidden_message = saved_message;
27880 else
27882 expr = cp_parser_expression (parser);
27883 *consumed_expr = true;
27886 parens.require_close (parser);
27888 else
27890 expr = boolean_true_node;
27891 if (!require_constexpr)
27892 *consumed_expr = false;
27895 /* We cannot build a noexcept-spec right away because this will check
27896 that expr is a constexpr. */
27897 if (!return_cond)
27898 return build_noexcept_spec (expr, tf_warning_or_error);
27899 else
27900 return expr;
27902 else
27903 return NULL_TREE;
27906 /* Parse an (optional) exception-specification.
27908 exception-specification:
27909 throw ( type-id-list [opt] )
27911 Returns a TREE_LIST representing the exception-specification. The
27912 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
27913 control parsing. QUALS are qualifiers indicating whether the (member)
27914 function is `const'. */
27916 static tree
27917 cp_parser_exception_specification_opt (cp_parser* parser,
27918 cp_parser_flags flags)
27920 cp_token *token;
27921 tree type_id_list;
27922 const char *saved_message;
27924 /* Peek at the next token. */
27925 token = cp_lexer_peek_token (parser->lexer);
27927 /* Is it a noexcept-specification? */
27928 type_id_list
27929 = cp_parser_noexcept_specification_opt (parser, flags,
27930 /*require_constexpr=*/true,
27931 /*consumed_expr=*/NULL,
27932 /*return_cond=*/false);
27933 if (type_id_list != NULL_TREE)
27934 return type_id_list;
27936 /* If it's not `throw', then there's no exception-specification. */
27937 if (!cp_parser_is_keyword (token, RID_THROW))
27938 return NULL_TREE;
27940 location_t loc = token->location;
27942 /* Consume the `throw'. */
27943 cp_lexer_consume_token (parser->lexer);
27945 /* Look for the `('. */
27946 matching_parens parens;
27947 parens.require_open (parser);
27949 /* Peek at the next token. */
27950 token = cp_lexer_peek_token (parser->lexer);
27951 /* If it's not a `)', then there is a type-id-list. */
27952 if (token->type != CPP_CLOSE_PAREN)
27954 /* Types may not be defined in an exception-specification. */
27955 saved_message = parser->type_definition_forbidden_message;
27956 parser->type_definition_forbidden_message
27957 = G_("types may not be defined in an exception-specification");
27958 /* Parse the type-id-list. */
27959 type_id_list = cp_parser_type_id_list (parser);
27960 /* Restore the saved message. */
27961 parser->type_definition_forbidden_message = saved_message;
27963 if (cxx_dialect >= cxx17)
27965 error_at (loc, "ISO C++17 does not allow dynamic exception "
27966 "specifications");
27967 type_id_list = NULL_TREE;
27969 else if (cxx_dialect >= cxx11)
27970 warning_at (loc, OPT_Wdeprecated,
27971 "dynamic exception specifications are deprecated in "
27972 "C++11");
27974 /* In C++17, throw() is equivalent to noexcept (true). throw()
27975 is deprecated in C++11 and above as well, but is still widely used,
27976 so don't warn about it yet. */
27977 else if (cxx_dialect >= cxx17)
27978 type_id_list = noexcept_true_spec;
27979 else
27980 type_id_list = empty_except_spec;
27982 /* Look for the `)'. */
27983 parens.require_close (parser);
27985 return type_id_list;
27988 /* Parse an (optional) type-id-list.
27990 type-id-list:
27991 type-id ... [opt]
27992 type-id-list , type-id ... [opt]
27994 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
27995 in the order that the types were presented. */
27997 static tree
27998 cp_parser_type_id_list (cp_parser* parser)
28000 tree types = NULL_TREE;
28002 while (true)
28004 cp_token *token;
28005 tree type;
28007 token = cp_lexer_peek_token (parser->lexer);
28009 /* Get the next type-id. */
28010 type = cp_parser_type_id (parser);
28011 /* Check for invalid 'auto'. */
28012 if (flag_concepts && type_uses_auto (type))
28014 error_at (token->location,
28015 "invalid use of %<auto%> in exception-specification");
28016 type = error_mark_node;
28018 /* Parse the optional ellipsis. */
28019 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28021 /* Consume the `...'. */
28022 cp_lexer_consume_token (parser->lexer);
28024 /* Turn the type into a pack expansion expression. */
28025 type = make_pack_expansion (type);
28027 /* Add it to the list. */
28028 types = add_exception_specifier (types, type, /*complain=*/1);
28029 /* Peek at the next token. */
28030 token = cp_lexer_peek_token (parser->lexer);
28031 /* If it is not a `,', we are done. */
28032 if (token->type != CPP_COMMA)
28033 break;
28034 /* Consume the `,'. */
28035 cp_lexer_consume_token (parser->lexer);
28038 return nreverse (types);
28041 /* Parse a try-block.
28043 try-block:
28044 try compound-statement handler-seq */
28046 static tree
28047 cp_parser_try_block (cp_parser* parser)
28049 tree try_block;
28051 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28052 if (parser->in_function_body
28053 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28054 && cxx_dialect < cxx20)
28055 pedwarn (input_location, OPT_Wc__20_extensions,
28056 "%<try%> in %<constexpr%> function only "
28057 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28059 try_block = begin_try_block ();
28060 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28061 finish_try_block (try_block);
28062 cp_parser_handler_seq (parser);
28063 finish_handler_sequence (try_block);
28065 return try_block;
28068 /* Parse a function-try-block.
28070 function-try-block:
28071 try ctor-initializer [opt] function-body handler-seq */
28073 static void
28074 cp_parser_function_try_block (cp_parser* parser)
28076 tree compound_stmt;
28077 tree try_block;
28079 /* Look for the `try' keyword. */
28080 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28081 return;
28082 /* Let the rest of the front end know where we are. */
28083 try_block = begin_function_try_block (&compound_stmt);
28084 /* Parse the function-body. */
28085 cp_parser_ctor_initializer_opt_and_function_body
28086 (parser, /*in_function_try_block=*/true);
28087 /* We're done with the `try' part. */
28088 finish_function_try_block (try_block);
28089 /* Parse the handlers. */
28090 cp_parser_handler_seq (parser);
28091 /* We're done with the handlers. */
28092 finish_function_handler_sequence (try_block, compound_stmt);
28095 /* Parse a handler-seq.
28097 handler-seq:
28098 handler handler-seq [opt] */
28100 static void
28101 cp_parser_handler_seq (cp_parser* parser)
28103 while (true)
28105 cp_token *token;
28107 /* Parse the handler. */
28108 cp_parser_handler (parser);
28109 /* Peek at the next token. */
28110 token = cp_lexer_peek_token (parser->lexer);
28111 /* If it's not `catch' then there are no more handlers. */
28112 if (!cp_parser_is_keyword (token, RID_CATCH))
28113 break;
28117 /* Parse a handler.
28119 handler:
28120 catch ( exception-declaration ) compound-statement */
28122 static void
28123 cp_parser_handler (cp_parser* parser)
28125 tree handler;
28126 tree declaration;
28128 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28129 handler = begin_handler ();
28130 matching_parens parens;
28131 parens.require_open (parser);
28132 declaration = cp_parser_exception_declaration (parser);
28133 finish_handler_parms (declaration, handler);
28134 parens.require_close (parser);
28135 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28136 finish_handler (handler);
28139 /* Parse an exception-declaration.
28141 exception-declaration:
28142 type-specifier-seq declarator
28143 type-specifier-seq abstract-declarator
28144 type-specifier-seq
28147 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28148 ellipsis variant is used. */
28150 static tree
28151 cp_parser_exception_declaration (cp_parser* parser)
28153 cp_decl_specifier_seq type_specifiers;
28154 cp_declarator *declarator;
28155 const char *saved_message;
28157 /* If it's an ellipsis, it's easy to handle. */
28158 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28160 /* Consume the `...' token. */
28161 cp_lexer_consume_token (parser->lexer);
28162 return NULL_TREE;
28165 /* Types may not be defined in exception-declarations. */
28166 saved_message = parser->type_definition_forbidden_message;
28167 parser->type_definition_forbidden_message
28168 = G_("types may not be defined in exception-declarations");
28170 /* Parse the type-specifier-seq. */
28171 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28172 /*is_declaration=*/true,
28173 /*is_trailing_return=*/false,
28174 &type_specifiers);
28175 /* If it's a `)', then there is no declarator. */
28176 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28177 declarator = NULL;
28178 else
28179 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28180 CP_PARSER_FLAGS_NONE,
28181 /*ctor_dtor_or_conv_p=*/NULL,
28182 /*parenthesized_p=*/NULL,
28183 /*member_p=*/false,
28184 /*friend_p=*/false,
28185 /*static_p=*/false);
28187 /* Restore the saved message. */
28188 parser->type_definition_forbidden_message = saved_message;
28190 if (!type_specifiers.any_specifiers_p)
28191 return error_mark_node;
28193 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28196 /* Parse a throw-expression.
28198 throw-expression:
28199 throw assignment-expression [opt]
28201 Returns a THROW_EXPR representing the throw-expression. */
28203 static tree
28204 cp_parser_throw_expression (cp_parser* parser)
28206 tree expression;
28207 cp_token* token;
28208 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28210 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28211 token = cp_lexer_peek_token (parser->lexer);
28212 /* Figure out whether or not there is an assignment-expression
28213 following the "throw" keyword. */
28214 if (token->type == CPP_COMMA
28215 || token->type == CPP_SEMICOLON
28216 || token->type == CPP_CLOSE_PAREN
28217 || token->type == CPP_CLOSE_SQUARE
28218 || token->type == CPP_CLOSE_BRACE
28219 || token->type == CPP_COLON)
28220 expression = NULL_TREE;
28221 else
28222 expression = cp_parser_assignment_expression (parser);
28224 /* Construct a location e.g.:
28225 throw x
28226 ^~~~~~~
28227 with caret == start at the start of the "throw" token, and
28228 the end at the end of the final token we consumed. */
28229 location_t combined_loc = make_location (start_loc, start_loc,
28230 parser->lexer);
28231 expression = build_throw (combined_loc, expression);
28233 return expression;
28236 /* Parse a yield-expression.
28238 yield-expression:
28239 co_yield assignment-expression
28240 co_yield braced-init-list
28242 Returns a CO_YIELD_EXPR representing the yield-expression. */
28244 static tree
28245 cp_parser_yield_expression (cp_parser* parser)
28247 tree expr;
28249 cp_token *token = cp_lexer_peek_token (parser->lexer);
28250 location_t kw_loc = token->location; /* Save for later. */
28252 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28254 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28256 bool expr_non_constant_p;
28257 cp_lexer_set_source_position (parser->lexer);
28258 /* ??? : probably a moot point? */
28259 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28260 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
28262 else
28263 expr = cp_parser_assignment_expression (parser);
28265 if (expr == error_mark_node)
28266 return expr;
28268 return finish_co_yield_expr (kw_loc, expr);
28271 /* GNU Extensions */
28273 /* Parse an (optional) asm-specification.
28275 asm-specification:
28276 asm ( string-literal )
28278 If the asm-specification is present, returns a STRING_CST
28279 corresponding to the string-literal. Otherwise, returns
28280 NULL_TREE. */
28282 static tree
28283 cp_parser_asm_specification_opt (cp_parser* parser)
28285 cp_token *token;
28286 tree asm_specification;
28288 /* Peek at the next token. */
28289 token = cp_lexer_peek_token (parser->lexer);
28290 /* If the next token isn't the `asm' keyword, then there's no
28291 asm-specification. */
28292 if (!cp_parser_is_keyword (token, RID_ASM))
28293 return NULL_TREE;
28295 /* Consume the `asm' token. */
28296 cp_lexer_consume_token (parser->lexer);
28297 /* Look for the `('. */
28298 matching_parens parens;
28299 parens.require_open (parser);
28301 /* Look for the string-literal. */
28302 asm_specification = cp_parser_string_literal (parser, false, false);
28304 /* Look for the `)'. */
28305 parens.require_close (parser);
28307 return asm_specification;
28310 /* Parse an asm-operand-list.
28312 asm-operand-list:
28313 asm-operand
28314 asm-operand-list , asm-operand
28316 asm-operand:
28317 string-literal ( expression )
28318 [ string-literal ] string-literal ( expression )
28320 Returns a TREE_LIST representing the operands. The TREE_VALUE of
28321 each node is the expression. The TREE_PURPOSE is itself a
28322 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28323 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28324 is a STRING_CST for the string literal before the parenthesis. Returns
28325 ERROR_MARK_NODE if any of the operands are invalid. */
28327 static tree
28328 cp_parser_asm_operand_list (cp_parser* parser)
28330 tree asm_operands = NULL_TREE;
28331 bool invalid_operands = false;
28333 while (true)
28335 tree string_literal;
28336 tree expression;
28337 tree name;
28339 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28341 /* Consume the `[' token. */
28342 cp_lexer_consume_token (parser->lexer);
28343 /* Read the operand name. */
28344 name = cp_parser_identifier (parser);
28345 if (name != error_mark_node)
28346 name = build_string (IDENTIFIER_LENGTH (name),
28347 IDENTIFIER_POINTER (name));
28348 /* Look for the closing `]'. */
28349 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28351 else
28352 name = NULL_TREE;
28353 /* Look for the string-literal. */
28354 string_literal = cp_parser_string_literal (parser, false, false);
28356 /* Look for the `('. */
28357 matching_parens parens;
28358 parens.require_open (parser);
28359 /* Parse the expression. */
28360 expression = cp_parser_expression (parser);
28361 /* Look for the `)'. */
28362 parens.require_close (parser);
28364 if (name == error_mark_node
28365 || string_literal == error_mark_node
28366 || expression == error_mark_node)
28367 invalid_operands = true;
28369 /* Add this operand to the list. */
28370 asm_operands = tree_cons (build_tree_list (name, string_literal),
28371 expression,
28372 asm_operands);
28373 /* If the next token is not a `,', there are no more
28374 operands. */
28375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28376 break;
28377 /* Consume the `,'. */
28378 cp_lexer_consume_token (parser->lexer);
28381 return invalid_operands ? error_mark_node : nreverse (asm_operands);
28384 /* Parse an asm-clobber-list.
28386 asm-clobber-list:
28387 string-literal
28388 asm-clobber-list , string-literal
28390 Returns a TREE_LIST, indicating the clobbers in the order that they
28391 appeared. The TREE_VALUE of each node is a STRING_CST. */
28393 static tree
28394 cp_parser_asm_clobber_list (cp_parser* parser)
28396 tree clobbers = NULL_TREE;
28398 while (true)
28400 tree string_literal;
28402 /* Look for the string literal. */
28403 string_literal = cp_parser_string_literal (parser, false, false);
28404 /* Add it to the list. */
28405 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
28406 /* If the next token is not a `,', then the list is
28407 complete. */
28408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28409 break;
28410 /* Consume the `,' token. */
28411 cp_lexer_consume_token (parser->lexer);
28414 return clobbers;
28417 /* Parse an asm-label-list.
28419 asm-label-list:
28420 identifier
28421 asm-label-list , identifier
28423 Returns a TREE_LIST, indicating the labels in the order that they
28424 appeared. The TREE_VALUE of each node is a label. */
28426 static tree
28427 cp_parser_asm_label_list (cp_parser* parser)
28429 tree labels = NULL_TREE;
28431 while (true)
28433 tree identifier, label, name;
28435 /* Look for the identifier. */
28436 identifier = cp_parser_identifier (parser);
28437 if (!error_operand_p (identifier))
28439 label = lookup_label (identifier);
28440 if (TREE_CODE (label) == LABEL_DECL)
28442 TREE_USED (label) = 1;
28443 check_goto (label);
28444 name = build_string (IDENTIFIER_LENGTH (identifier),
28445 IDENTIFIER_POINTER (identifier));
28446 labels = tree_cons (name, label, labels);
28449 /* If the next token is not a `,', then the list is
28450 complete. */
28451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28452 break;
28453 /* Consume the `,' token. */
28454 cp_lexer_consume_token (parser->lexer);
28457 return nreverse (labels);
28460 /* Return TRUE iff the next tokens in the stream are possibly the
28461 beginning of a GNU extension attribute. */
28463 static bool
28464 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
28466 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
28469 /* Return TRUE iff the next tokens in the stream are possibly the
28470 beginning of a standard C++-11 attribute specifier. */
28472 static bool
28473 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
28475 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
28478 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28479 beginning of a standard C++-11 attribute specifier. */
28481 static bool
28482 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
28484 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28486 return (cxx_dialect >= cxx11
28487 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
28488 || (token->type == CPP_OPEN_SQUARE
28489 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
28490 && token->type == CPP_OPEN_SQUARE)));
28493 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28494 beginning of a GNU extension attribute. */
28496 static bool
28497 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
28499 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28501 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
28504 /* Return true iff the next tokens can be the beginning of either a
28505 GNU attribute list, or a standard C++11 attribute sequence. */
28507 static bool
28508 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
28510 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
28511 || cp_next_tokens_can_be_std_attribute_p (parser));
28514 /* Return true iff the next Nth tokens can be the beginning of either
28515 a GNU attribute list, or a standard C++11 attribute sequence. */
28517 static bool
28518 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
28520 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
28521 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
28524 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
28525 of GNU attributes, or return NULL. */
28527 static tree
28528 cp_parser_attributes_opt (cp_parser *parser)
28530 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
28531 return cp_parser_gnu_attributes_opt (parser);
28532 return cp_parser_std_attribute_spec_seq (parser);
28535 /* Parse an (optional) series of attributes.
28537 attributes:
28538 attributes attribute
28540 attribute:
28541 __attribute__ (( attribute-list [opt] ))
28543 The return value is as for cp_parser_gnu_attribute_list. */
28545 static tree
28546 cp_parser_gnu_attributes_opt (cp_parser* parser)
28548 tree attributes = NULL_TREE;
28550 auto cleanup = make_temp_override
28551 (parser->auto_is_implicit_function_template_parm_p, false);
28553 while (true)
28555 cp_token *token;
28556 tree attribute_list;
28557 bool ok = true;
28559 /* Peek at the next token. */
28560 token = cp_lexer_peek_token (parser->lexer);
28561 /* If it's not `__attribute__', then we're done. */
28562 if (token->keyword != RID_ATTRIBUTE)
28563 break;
28565 /* Consume the `__attribute__' keyword. */
28566 cp_lexer_consume_token (parser->lexer);
28567 /* Look for the two `(' tokens. */
28568 matching_parens outer_parens;
28569 if (!outer_parens.require_open (parser))
28570 ok = false;
28571 matching_parens inner_parens;
28572 if (!inner_parens.require_open (parser))
28573 ok = false;
28575 /* Peek at the next token. */
28576 token = cp_lexer_peek_token (parser->lexer);
28577 if (token->type != CPP_CLOSE_PAREN)
28578 /* Parse the attribute-list. */
28579 attribute_list = cp_parser_gnu_attribute_list (parser);
28580 else
28581 /* If the next token is a `)', then there is no attribute
28582 list. */
28583 attribute_list = NULL;
28585 /* Look for the two `)' tokens. */
28586 if (!inner_parens.require_close (parser))
28587 ok = false;
28588 if (!outer_parens.require_close (parser))
28589 ok = false;
28590 if (!ok)
28591 cp_parser_skip_to_end_of_statement (parser);
28593 /* Add these new attributes to the list. */
28594 attributes = attr_chainon (attributes, attribute_list);
28597 return attributes;
28600 /* Parse a GNU attribute-list.
28602 attribute-list:
28603 attribute
28604 attribute-list , attribute
28606 attribute:
28607 identifier
28608 identifier ( identifier )
28609 identifier ( identifier , expression-list )
28610 identifier ( expression-list )
28612 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
28613 to an attribute. The TREE_PURPOSE of each node is the identifier
28614 indicating which attribute is in use. The TREE_VALUE represents
28615 the arguments, if any. */
28617 static tree
28618 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
28620 tree attribute_list = NULL_TREE;
28621 bool save_translate_strings_p = parser->translate_strings_p;
28623 /* Don't create wrapper nodes within attributes: the
28624 handlers don't know how to handle them. */
28625 auto_suppress_location_wrappers sentinel;
28627 parser->translate_strings_p = false;
28628 while (true)
28630 cp_token *token;
28631 tree identifier;
28632 tree attribute;
28634 /* Look for the identifier. We also allow keywords here; for
28635 example `__attribute__ ((const))' is legal. */
28636 token = cp_lexer_peek_token (parser->lexer);
28637 if (token->type == CPP_NAME
28638 || token->type == CPP_KEYWORD)
28640 tree arguments = NULL_TREE;
28642 /* Consume the token, but save it since we need it for the
28643 SIMD enabled function parsing. */
28644 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
28646 /* Save away the identifier that indicates which attribute
28647 this is. */
28648 identifier = (token->type == CPP_KEYWORD)
28649 /* For keywords, use the canonical spelling, not the
28650 parsed identifier. */
28651 ? ridpointers[(int) token->keyword]
28652 : id_token->u.value;
28654 identifier = canonicalize_attr_name (identifier);
28655 attribute = build_tree_list (identifier, NULL_TREE);
28657 /* Peek at the next token. */
28658 token = cp_lexer_peek_token (parser->lexer);
28659 /* If it's an `(', then parse the attribute arguments. */
28660 if (token->type == CPP_OPEN_PAREN)
28662 vec<tree, va_gc> *vec;
28663 int attr_flag = (attribute_takes_identifier_p (identifier)
28664 ? id_attr : normal_attr);
28665 vec = cp_parser_parenthesized_expression_list
28666 (parser, attr_flag, /*cast_p=*/false,
28667 /*allow_expansion_p=*/false,
28668 /*non_constant_p=*/NULL);
28669 if (vec == NULL)
28670 arguments = error_mark_node;
28671 else
28673 arguments = build_tree_list_vec (vec);
28674 release_tree_vector (vec);
28676 /* Save the arguments away. */
28677 TREE_VALUE (attribute) = arguments;
28680 if (arguments != error_mark_node)
28682 /* Add this attribute to the list. */
28683 TREE_CHAIN (attribute) = attribute_list;
28684 attribute_list = attribute;
28687 token = cp_lexer_peek_token (parser->lexer);
28689 /* Unless EXACTLY_ONE is set look for more attributes.
28690 If the next token isn't a `,', we're done. */
28691 if (exactly_one || token->type != CPP_COMMA)
28692 break;
28694 /* Consume the comma and keep going. */
28695 cp_lexer_consume_token (parser->lexer);
28697 parser->translate_strings_p = save_translate_strings_p;
28699 /* We built up the list in reverse order. */
28700 return nreverse (attribute_list);
28703 /* Parse arguments of omp::directive attribute.
28705 ( directive-name ,[opt] clause-list[opt] )
28707 For directive just remember the first/last tokens for subsequent
28708 parsing. */
28710 static void
28711 cp_parser_omp_directive_args (cp_parser *parser, tree attribute)
28713 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
28714 if (first->type == CPP_CLOSE_PAREN)
28716 cp_lexer_consume_token (parser->lexer);
28717 error_at (first->location, "expected OpenMP directive name");
28718 cp_lexer_consume_token (parser->lexer);
28719 TREE_VALUE (attribute) = NULL_TREE;
28720 return;
28722 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
28723 if (n == 1)
28725 cp_lexer_consume_token (parser->lexer);
28726 error_at (first->location, "expected attribute argument as balanced "
28727 "token sequence");
28728 TREE_VALUE (attribute) = NULL_TREE;
28729 return;
28731 for (n = n - 2; n; --n)
28732 cp_lexer_consume_token (parser->lexer);
28733 cp_token *last = cp_lexer_peek_token (parser->lexer);
28734 cp_lexer_consume_token (parser->lexer);
28735 tree arg = make_node (DEFERRED_PARSE);
28736 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
28737 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
28738 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
28741 /* Parse arguments of omp::sequence attribute.
28743 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
28745 static void
28746 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
28748 matching_parens parens;
28749 parens.consume_open (parser);
28752 cp_token *token = cp_lexer_peek_token (parser->lexer);
28753 if (token->type == CPP_NAME
28754 && token->u.value == omp_identifier
28755 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
28757 cp_lexer_consume_token (parser->lexer);
28758 cp_lexer_consume_token (parser->lexer);
28759 token = cp_lexer_peek_token (parser->lexer);
28761 bool directive = false;
28762 const char *p;
28763 if (token->type != CPP_NAME)
28764 p = "";
28765 else
28766 p = IDENTIFIER_POINTER (token->u.value);
28767 if (strcmp (p, "directive") == 0)
28768 directive = true;
28769 else if (strcmp (p, "sequence") != 0)
28771 error_at (token->location, "expected %<directive%> or %<sequence%>");
28772 cp_parser_skip_to_closing_parenthesis (parser,
28773 /*recovering=*/true,
28774 /*or_comma=*/true,
28775 /*consume_paren=*/false);
28776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28777 break;
28778 cp_lexer_consume_token (parser->lexer);
28780 cp_lexer_consume_token (parser->lexer);
28781 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
28782 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
28783 UNKNOWN_LOCATION);
28784 else if (directive)
28785 cp_parser_omp_directive_args (parser, attribute);
28786 else
28787 cp_parser_omp_sequence_args (parser, attribute);
28788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28789 break;
28790 cp_lexer_consume_token (parser->lexer);
28792 while (1);
28793 if (!parens.require_close (parser))
28794 cp_parser_skip_to_closing_parenthesis (parser, true, false,
28795 /*consume_paren=*/true);
28798 /* Parse a standard C++11 attribute.
28800 The returned representation is a TREE_LIST which TREE_PURPOSE is
28801 the scoped name of the attribute, and the TREE_VALUE is its
28802 arguments list.
28804 Note that the scoped name of the attribute is itself a TREE_LIST
28805 which TREE_PURPOSE is the namespace of the attribute, and
28806 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
28807 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
28808 and which TREE_PURPOSE is directly the attribute name.
28810 Clients of the attribute code should use get_attribute_namespace
28811 and get_attribute_name to get the actual namespace and name of
28812 attributes, regardless of their being GNU or C++11 attributes.
28814 attribute:
28815 attribute-token attribute-argument-clause [opt]
28817 attribute-token:
28818 identifier
28819 attribute-scoped-token
28821 attribute-scoped-token:
28822 attribute-namespace :: identifier
28824 attribute-namespace:
28825 identifier
28827 attribute-argument-clause:
28828 ( balanced-token-seq )
28830 balanced-token-seq:
28831 balanced-token [opt]
28832 balanced-token-seq balanced-token
28834 balanced-token:
28835 ( balanced-token-seq )
28836 [ balanced-token-seq ]
28837 { balanced-token-seq }. */
28839 static tree
28840 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
28842 tree attribute, attr_id = NULL_TREE, arguments;
28843 cp_token *token;
28845 auto cleanup = make_temp_override
28846 (parser->auto_is_implicit_function_template_parm_p, false);
28848 /* First, parse name of the attribute, a.k.a attribute-token. */
28850 token = cp_lexer_peek_token (parser->lexer);
28851 if (token->type == CPP_NAME)
28852 attr_id = token->u.value;
28853 else if (token->type == CPP_KEYWORD)
28854 attr_id = ridpointers[(int) token->keyword];
28855 else if (token->flags & NAMED_OP)
28856 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
28858 if (attr_id == NULL_TREE)
28859 return NULL_TREE;
28861 cp_lexer_consume_token (parser->lexer);
28863 token = cp_lexer_peek_token (parser->lexer);
28864 if (token->type == CPP_SCOPE)
28866 /* We are seeing a scoped attribute token. */
28868 cp_lexer_consume_token (parser->lexer);
28869 if (attr_ns)
28870 error_at (token->location, "attribute using prefix used together "
28871 "with scoped attribute token");
28872 attr_ns = attr_id;
28874 token = cp_lexer_peek_token (parser->lexer);
28875 if (token->type == CPP_NAME)
28876 attr_id = token->u.value;
28877 else if (token->type == CPP_KEYWORD)
28878 attr_id = ridpointers[(int) token->keyword];
28879 else if (token->flags & NAMED_OP)
28880 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
28881 else
28883 error_at (token->location,
28884 "expected an identifier for the attribute name");
28885 return error_mark_node;
28887 cp_lexer_consume_token (parser->lexer);
28889 attr_ns = canonicalize_attr_name (attr_ns);
28890 attr_id = canonicalize_attr_name (attr_id);
28891 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
28892 NULL_TREE);
28893 token = cp_lexer_peek_token (parser->lexer);
28895 else if (attr_ns)
28897 attr_ns = canonicalize_attr_name (attr_ns);
28898 attr_id = canonicalize_attr_name (attr_id);
28899 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
28900 NULL_TREE);
28902 else
28904 attr_id = canonicalize_attr_name (attr_id);
28905 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
28906 NULL_TREE);
28907 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
28908 but no longer: we have to be able to tell [[noreturn]] and
28909 __attribute__((noreturn)) apart. */
28910 /* C++14 deprecated attribute is equivalent to GNU's. */
28911 if (is_attribute_p ("deprecated", attr_id))
28912 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
28913 /* C++17 fallthrough attribute is equivalent to GNU's. */
28914 else if (is_attribute_p ("fallthrough", attr_id))
28915 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
28916 /* Transactional Memory TS optimize_for_synchronized attribute is
28917 equivalent to GNU transaction_callable. */
28918 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
28919 TREE_PURPOSE (attribute)
28920 = get_identifier ("transaction_callable");
28921 /* Transactional Memory attributes are GNU attributes. */
28922 else if (tm_attr_to_mask (attr_id))
28923 TREE_PURPOSE (attribute) = attr_id;
28926 /* Now parse the optional argument clause of the attribute. */
28928 if (token->type != CPP_OPEN_PAREN)
28930 if ((flag_openmp || flag_openmp_simd)
28931 && attr_ns == omp_identifier
28932 && (is_attribute_p ("directive", attr_id)
28933 || is_attribute_p ("sequence", attr_id)))
28935 error_at (token->location, "%<omp::%E%> attribute requires argument",
28936 attr_id);
28937 return NULL_TREE;
28939 return attribute;
28943 vec<tree, va_gc> *vec;
28944 int attr_flag = normal_attr;
28946 /* Maybe we don't expect to see any arguments for this attribute. */
28947 const attribute_spec *as
28948 = lookup_attribute_spec (TREE_PURPOSE (attribute));
28949 if (as && as->max_length == 0)
28951 error_at (token->location, "%qE attribute does not take any arguments",
28952 attr_id);
28953 cp_parser_skip_to_closing_parenthesis (parser,
28954 /*recovering=*/true,
28955 /*or_comma=*/false,
28956 /*consume_paren=*/true);
28957 return error_mark_node;
28960 if (attr_ns == gnu_identifier
28961 && attribute_takes_identifier_p (attr_id))
28962 /* A GNU attribute that takes an identifier in parameter. */
28963 attr_flag = id_attr;
28965 if (as == NULL)
28967 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
28969 if (is_attribute_p ("directive", attr_id))
28971 cp_parser_omp_directive_args (parser, attribute);
28972 return attribute;
28974 else if (is_attribute_p ("sequence", attr_id))
28976 TREE_VALUE (TREE_PURPOSE (attribute))
28977 = get_identifier ("directive");
28978 cp_parser_omp_sequence_args (parser, attribute);
28979 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
28980 return attribute;
28984 /* For unknown attributes, just skip balanced tokens instead of
28985 trying to parse the arguments. */
28986 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
28987 cp_lexer_consume_token (parser->lexer);
28988 return attribute;
28991 vec = cp_parser_parenthesized_expression_list
28992 (parser, attr_flag, /*cast_p=*/false,
28993 /*allow_expansion_p=*/true,
28994 /*non_constant_p=*/NULL);
28995 if (vec == NULL)
28996 arguments = error_mark_node;
28997 else
28999 if (vec->is_empty ())
29000 /* e.g. [[attr()]]. */
29001 error_at (token->location, "parentheses must be omitted if "
29002 "%qE attribute argument list is empty",
29003 attr_id);
29004 arguments = build_tree_list_vec (vec);
29005 release_tree_vector (vec);
29008 if (arguments == error_mark_node)
29009 attribute = error_mark_node;
29010 else
29011 TREE_VALUE (attribute) = arguments;
29014 return attribute;
29017 /* Warn if the attribute ATTRIBUTE appears more than once in the
29018 attribute-list ATTRIBUTES. This used to be enforced for certain
29019 attributes, but the restriction was removed in P2156. Note that
29020 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
29021 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29022 found in ATTRIBUTES. */
29024 static bool
29025 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29027 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29028 "likely", "unlikely", "fallthrough",
29029 "no_unique_address" };
29030 if (attributes)
29031 for (const auto &a : alist)
29032 if (is_attribute_p (a, get_attribute_name (attribute))
29033 && lookup_attribute (a, attributes))
29035 if (!from_macro_expansion_at (loc))
29036 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29037 "multiple times", a);
29038 return false;
29040 return true;
29043 /* Parse a list of standard C++-11 attributes.
29045 attribute-list:
29046 attribute [opt]
29047 attribute-list , attribute[opt]
29048 attribute ...
29049 attribute-list , attribute ...
29052 static tree
29053 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29055 tree attributes = NULL_TREE, attribute = NULL_TREE;
29056 cp_token *token = NULL;
29058 while (true)
29060 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29061 attribute = cp_parser_std_attribute (parser, attr_ns);
29062 if (attribute == error_mark_node)
29063 break;
29064 if (attribute != NULL_TREE)
29066 if (cp_parser_check_std_attribute (loc, attributes, attribute))
29068 TREE_CHAIN (attribute) = attributes;
29069 attributes = attribute;
29072 token = cp_lexer_peek_token (parser->lexer);
29073 if (token->type == CPP_ELLIPSIS)
29075 cp_lexer_consume_token (parser->lexer);
29076 if (attribute == NULL_TREE)
29077 error_at (token->location,
29078 "expected attribute before %<...%>");
29079 else
29081 tree pack = make_pack_expansion (TREE_VALUE (attribute));
29082 if (pack == error_mark_node)
29083 return error_mark_node;
29084 TREE_VALUE (attribute) = pack;
29086 token = cp_lexer_peek_token (parser->lexer);
29088 if (token->type != CPP_COMMA)
29089 break;
29090 cp_lexer_consume_token (parser->lexer);
29092 attributes = nreverse (attributes);
29093 return attributes;
29096 /* Parse a standard C++-11 attribute specifier.
29098 attribute-specifier:
29099 [ [ attribute-using-prefix [opt] attribute-list ] ]
29100 alignment-specifier
29102 attribute-using-prefix:
29103 using attribute-namespace :
29105 alignment-specifier:
29106 alignas ( type-id ... [opt] )
29107 alignas ( alignment-expression ... [opt] ). */
29109 static tree
29110 cp_parser_std_attribute_spec (cp_parser *parser)
29112 tree attributes = NULL_TREE;
29113 cp_token *token = cp_lexer_peek_token (parser->lexer);
29115 if (token->type == CPP_OPEN_SQUARE
29116 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
29118 tree attr_ns = NULL_TREE;
29120 cp_lexer_consume_token (parser->lexer);
29121 cp_lexer_consume_token (parser->lexer);
29123 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29125 token = cp_lexer_peek_nth_token (parser->lexer, 2);
29126 if (token->type == CPP_NAME)
29127 attr_ns = token->u.value;
29128 else if (token->type == CPP_KEYWORD)
29129 attr_ns = ridpointers[(int) token->keyword];
29130 else if (token->flags & NAMED_OP)
29131 attr_ns = get_identifier (cpp_type2name (token->type,
29132 token->flags));
29133 if (attr_ns
29134 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
29136 if (cxx_dialect < cxx17)
29137 pedwarn (input_location, OPT_Wc__17_extensions,
29138 "attribute using prefix only available "
29139 "with %<-std=c++17%> or %<-std=gnu++17%>");
29141 cp_lexer_consume_token (parser->lexer);
29142 cp_lexer_consume_token (parser->lexer);
29143 cp_lexer_consume_token (parser->lexer);
29145 else
29146 attr_ns = NULL_TREE;
29149 attributes = cp_parser_std_attribute_list (parser, attr_ns);
29151 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
29152 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
29153 cp_parser_skip_to_end_of_statement (parser);
29154 else
29155 /* Warn about parsing c++11 attribute in non-c++11 mode, only
29156 when we are sure that we have actually parsed them. */
29157 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29159 else
29161 tree alignas_expr;
29163 /* Look for an alignment-specifier. */
29165 token = cp_lexer_peek_token (parser->lexer);
29167 if (token->type != CPP_KEYWORD
29168 || token->keyword != RID_ALIGNAS)
29169 return NULL_TREE;
29171 cp_lexer_consume_token (parser->lexer);
29172 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29174 matching_parens parens;
29175 if (!parens.require_open (parser))
29176 return error_mark_node;
29178 cp_parser_parse_tentatively (parser);
29179 alignas_expr = cp_parser_type_id (parser);
29181 if (!cp_parser_parse_definitely (parser))
29183 alignas_expr = cp_parser_assignment_expression (parser);
29184 if (alignas_expr == error_mark_node)
29185 cp_parser_skip_to_end_of_statement (parser);
29186 if (alignas_expr == NULL_TREE
29187 || alignas_expr == error_mark_node)
29188 return alignas_expr;
29191 alignas_expr = cxx_alignas_expr (alignas_expr);
29192 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
29194 /* Handle alignas (pack...). */
29195 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29197 cp_lexer_consume_token (parser->lexer);
29198 alignas_expr = make_pack_expansion (alignas_expr);
29201 /* Something went wrong, so don't build the attribute. */
29202 if (alignas_expr == error_mark_node)
29203 return error_mark_node;
29205 /* Missing ')' means the code cannot possibly be valid; go ahead
29206 and commit to make sure we issue a hard error. */
29207 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
29208 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29209 cp_parser_commit_to_tentative_parse (parser);
29211 if (!parens.require_close (parser))
29212 return error_mark_node;
29214 /* Build the C++-11 representation of an 'aligned'
29215 attribute. */
29216 attributes
29217 = build_tree_list (build_tree_list (gnu_identifier,
29218 aligned_identifier), alignas_expr);
29221 return attributes;
29224 /* Parse a standard C++-11 attribute-specifier-seq.
29226 attribute-specifier-seq:
29227 attribute-specifier-seq [opt] attribute-specifier
29230 static tree
29231 cp_parser_std_attribute_spec_seq (cp_parser *parser)
29233 tree attr_specs = NULL_TREE;
29234 tree attr_last = NULL_TREE;
29236 /* Don't create wrapper nodes within attributes: the
29237 handlers don't know how to handle them. */
29238 auto_suppress_location_wrappers sentinel;
29240 while (true)
29242 tree attr_spec = cp_parser_std_attribute_spec (parser);
29243 if (attr_spec == NULL_TREE)
29244 break;
29245 if (attr_spec == error_mark_node)
29246 return error_mark_node;
29248 if (attr_last)
29249 TREE_CHAIN (attr_last) = attr_spec;
29250 else
29251 attr_specs = attr_last = attr_spec;
29252 attr_last = tree_last (attr_last);
29255 return attr_specs;
29258 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
29259 return index of the first token after balanced-token, or N on failure. */
29261 static size_t
29262 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
29264 size_t orig_n = n;
29265 int nparens = 0, nbraces = 0, nsquares = 0;
29267 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
29269 case CPP_PRAGMA_EOL:
29270 if (!parser->lexer->in_pragma)
29271 break;
29272 /* FALLTHRU */
29273 case CPP_EOF:
29274 /* Ran out of tokens. */
29275 return orig_n;
29276 case CPP_OPEN_PAREN:
29277 ++nparens;
29278 break;
29279 case CPP_OPEN_BRACE:
29280 ++nbraces;
29281 break;
29282 case CPP_OPEN_SQUARE:
29283 ++nsquares;
29284 break;
29285 case CPP_CLOSE_PAREN:
29286 --nparens;
29287 break;
29288 case CPP_CLOSE_BRACE:
29289 --nbraces;
29290 break;
29291 case CPP_CLOSE_SQUARE:
29292 --nsquares;
29293 break;
29294 default:
29295 break;
29297 while (nparens || nbraces || nsquares);
29298 return n;
29301 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
29302 return index of the first token after the GNU attribute tokens, or N on
29303 failure. */
29305 static size_t
29306 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
29308 while (true)
29310 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
29311 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
29312 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
29313 break;
29315 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
29316 if (n2 == n + 2)
29317 break;
29318 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
29319 break;
29320 n = n2 + 1;
29322 return n;
29325 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
29326 next token), return index of the first token after the standard C++11
29327 attribute tokens, or N on failure. */
29329 static size_t
29330 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
29332 while (true)
29334 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
29335 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
29337 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29338 if (n2 == n + 1)
29339 break;
29340 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
29341 break;
29342 n = n2 + 1;
29344 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
29345 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
29347 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29348 if (n2 == n + 1)
29349 break;
29350 n = n2;
29352 else
29353 break;
29355 return n;
29358 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
29359 as the next token), return index of the first token after the attribute
29360 tokens, or N on failure. */
29362 static size_t
29363 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
29365 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
29366 return cp_parser_skip_gnu_attributes_opt (parser, n);
29367 return cp_parser_skip_std_attribute_spec_seq (parser, n);
29370 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
29371 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
29372 current value of the PEDANTIC flag, regardless of whether or not
29373 the `__extension__' keyword is present. The caller is responsible
29374 for restoring the value of the PEDANTIC flag. */
29376 static bool
29377 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
29379 /* Save the old value of the PEDANTIC flag. */
29380 *saved_pedantic = pedantic;
29382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
29384 /* Consume the `__extension__' token. */
29385 cp_lexer_consume_token (parser->lexer);
29386 /* We're not being pedantic while the `__extension__' keyword is
29387 in effect. */
29388 pedantic = 0;
29390 return true;
29393 return false;
29396 /* Parse a label declaration.
29398 label-declaration:
29399 __label__ label-declarator-seq ;
29401 label-declarator-seq:
29402 identifier , label-declarator-seq
29403 identifier */
29405 static void
29406 cp_parser_label_declaration (cp_parser* parser)
29408 /* Look for the `__label__' keyword. */
29409 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
29411 while (true)
29413 tree identifier;
29415 /* Look for an identifier. */
29416 identifier = cp_parser_identifier (parser);
29417 /* If we failed, stop. */
29418 if (identifier == error_mark_node)
29419 break;
29420 /* Declare it as a label. */
29421 finish_label_decl (identifier);
29422 /* If the next token is a `;', stop. */
29423 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29424 break;
29425 /* Look for the `,' separating the label declarations. */
29426 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
29429 /* Look for the final `;'. */
29430 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29433 // -------------------------------------------------------------------------- //
29434 // Concept definitions
29436 static tree
29437 cp_parser_concept_definition (cp_parser *parser)
29439 /* A concept definition is an unevaluated context. */
29440 cp_unevaluated u;
29442 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
29443 cp_lexer_consume_token (parser->lexer);
29445 cp_expr id = cp_parser_identifier (parser);
29446 if (id == error_mark_node)
29448 cp_parser_skip_to_end_of_statement (parser);
29449 cp_parser_consume_semicolon_at_end_of_statement (parser);
29450 return NULL_TREE;
29453 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29455 cp_parser_skip_to_end_of_statement (parser);
29456 cp_parser_consume_semicolon_at_end_of_statement (parser);
29457 return error_mark_node;
29460 processing_constraint_expression_sentinel parsing_constraint;
29461 tree init = cp_parser_constraint_expression (parser);
29462 if (init == error_mark_node)
29463 cp_parser_skip_to_end_of_statement (parser);
29465 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
29466 but continue as if it were. */
29467 cp_parser_consume_semicolon_at_end_of_statement (parser);
29469 return finish_concept_definition (id, init);
29472 // -------------------------------------------------------------------------- //
29473 // Requires Clause
29475 /* Diagnose an expression that should appear in ()'s within a requires-clause
29476 and suggest where to place those parentheses. */
29478 static void
29479 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
29481 error_at (loc, "expression must be enclosed in parentheses");
29484 static void
29485 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
29487 gcc_rich_location richloc (loc);
29488 richloc.add_fixit_insert_before ("(");
29489 richloc.add_fixit_insert_after (")");
29490 error_at (&richloc, "expression must be enclosed in parentheses");
29493 /* Characterizes the likely kind of expression intended by a mis-written
29494 primary constraint. */
29495 enum primary_constraint_error
29497 pce_ok,
29498 pce_maybe_operator,
29499 pce_maybe_postfix
29502 /* Returns true if the token(s) following a primary-expression in a
29503 constraint-logical-* expression would require parentheses. */
29505 static primary_constraint_error
29506 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
29508 cp_token *token = cp_lexer_peek_token (parser->lexer);
29509 switch (token->type)
29511 default:
29512 return pce_ok;
29514 case CPP_EQ:
29516 /* An equal sign may be part of the definition of a function,
29517 and not an assignment operator, when parsing the expression
29518 for a trailing requires-clause. For example:
29520 template<typename T>
29521 struct S {
29522 S() requires C<T> = default;
29525 Don't try to reparse this a binary operator. */
29526 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
29527 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
29528 return pce_ok;
29530 gcc_fallthrough ();
29533 /* Arithmetic operators. */
29534 case CPP_PLUS:
29535 case CPP_MINUS:
29536 case CPP_MULT:
29537 case CPP_DIV:
29538 case CPP_MOD:
29539 /* Bitwise operators. */
29540 case CPP_AND:
29541 case CPP_OR:
29542 case CPP_XOR:
29543 case CPP_RSHIFT:
29544 case CPP_LSHIFT:
29545 /* Relational operators. */
29546 case CPP_EQ_EQ:
29547 case CPP_NOT_EQ:
29548 case CPP_LESS:
29549 case CPP_GREATER:
29550 case CPP_LESS_EQ:
29551 case CPP_GREATER_EQ:
29552 case CPP_SPACESHIP:
29553 /* Pointer-to-member. */
29554 case CPP_DOT_STAR:
29555 case CPP_DEREF_STAR:
29556 /* Assignment operators. */
29557 case CPP_PLUS_EQ:
29558 case CPP_MINUS_EQ:
29559 case CPP_MULT_EQ:
29560 case CPP_DIV_EQ:
29561 case CPP_MOD_EQ:
29562 case CPP_AND_EQ:
29563 case CPP_OR_EQ:
29564 case CPP_XOR_EQ:
29565 case CPP_RSHIFT_EQ:
29566 case CPP_LSHIFT_EQ:
29567 /* Conditional operator */
29568 case CPP_QUERY:
29569 /* Unenclosed binary or conditional operator. */
29570 return pce_maybe_operator;
29572 case CPP_OPEN_PAREN:
29574 /* A primary constraint that precedes the parameter-list of a
29575 lambda expression is followed by an open paren.
29577 []<typename T> requires C (T a, T b) { ... }
29579 Don't try to re-parse this as a postfix expression. */
29580 if (lambda_p)
29581 return pce_ok;
29583 gcc_fallthrough ();
29585 case CPP_OPEN_SQUARE:
29587 /* A primary-constraint-expression followed by a '[[' is not a
29588 postfix expression. */
29589 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
29590 return pce_ok;
29592 gcc_fallthrough ();
29594 case CPP_PLUS_PLUS:
29595 case CPP_MINUS_MINUS:
29596 case CPP_DOT:
29597 /* Unenclosed postfix operator. */
29598 return pce_maybe_postfix;
29600 case CPP_DEREF:
29601 /* A primary constraint that precedes the lambda-declarator of a
29602 lambda expression is followed by trailing return type.
29604 []<typename T> requires C -> void {}
29606 Don't try to re-parse this as a postfix expression in
29607 C++23 and later. In C++20 ( needs to come in between but we
29608 allow it to be omitted with pedwarn. */
29609 if (lambda_p)
29610 return pce_ok;
29611 /* Unenclosed postfix operator. */
29612 return pce_maybe_postfix;
29616 /* Returns true if the next token begins a unary expression, preceded by
29617 an operator or keyword. */
29619 static bool
29620 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
29622 cp_token *token = cp_lexer_peek_token (parser->lexer);
29623 switch (token->type)
29625 case CPP_NOT:
29626 case CPP_PLUS:
29627 case CPP_MINUS:
29628 case CPP_MULT:
29629 case CPP_COMPL:
29630 case CPP_PLUS_PLUS:
29631 case CPP_MINUS_MINUS:
29632 return true;
29634 case CPP_KEYWORD:
29636 switch (token->keyword)
29638 case RID_STATCAST:
29639 case RID_DYNCAST:
29640 case RID_REINTCAST:
29641 case RID_CONSTCAST:
29642 case RID_TYPEID:
29643 case RID_SIZEOF:
29644 case RID_ALIGNOF:
29645 case RID_NOEXCEPT:
29646 case RID_NEW:
29647 case RID_DELETE:
29648 case RID_THROW:
29649 return true;
29651 default:
29652 break;
29656 default:
29657 break;
29660 return false;
29663 /* Parse a primary expression within a constraint. */
29665 static cp_expr
29666 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
29668 /* If this looks like a unary expression, parse it as such, but diagnose
29669 it as ill-formed; it requires parens. */
29670 if (cp_parser_unary_constraint_requires_parens (parser))
29672 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
29673 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
29674 return e;
29677 cp_lexer_save_tokens (parser->lexer);
29678 cp_id_kind idk;
29679 location_t loc = input_location;
29680 cp_expr expr = cp_parser_primary_expression (parser,
29681 /*address_p=*/false,
29682 /*cast_p=*/false,
29683 /*template_arg_p=*/false,
29684 &idk);
29685 expr.maybe_add_location_wrapper ();
29687 primary_constraint_error pce = pce_ok;
29688 if (expr != error_mark_node)
29690 /* The primary-expression could be part of an unenclosed non-logical
29691 compound expression. */
29692 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
29694 if (pce == pce_ok)
29696 cp_lexer_commit_tokens (parser->lexer);
29697 return finish_constraint_primary_expr (expr);
29700 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
29701 error, but return the expression as if it were valid. */
29702 cp_lexer_rollback_tokens (parser->lexer);
29703 cp_parser_parse_tentatively (parser);
29704 if (pce == pce_maybe_operator)
29705 expr = cp_parser_assignment_expression (parser, NULL, false, false);
29706 else
29707 expr = cp_parser_simple_cast_expression (parser);
29708 if (cp_parser_parse_definitely (parser))
29710 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
29711 return expr;
29714 /* Otherwise, something has gone very wrong, and we can't generate a more
29715 meaningful diagnostic or recover. */
29716 cp_parser_diagnose_ungrouped_constraint_plain (loc);
29717 return error_mark_node;
29720 /* Parse a constraint-logical-and-expression.
29722 constraint-logical-and-expression:
29723 primary-expression
29724 constraint-logical-and-expression '&&' primary-expression */
29726 static cp_expr
29727 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
29729 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29730 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
29732 cp_token *op = cp_lexer_consume_token (parser->lexer);
29733 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29734 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
29736 return lhs;
29739 /* Parse a constraint-logical-or-expression.
29741 constraint-logical-or-expression:
29742 constraint-logical-and-expression
29743 constraint-logical-or-expression '||' constraint-logical-and-expression */
29745 static cp_expr
29746 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
29748 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29749 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
29751 cp_token *op = cp_lexer_consume_token (parser->lexer);
29752 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29753 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
29755 return lhs;
29758 /* Parse the expression after a requires-clause. This has a different grammar
29759 than that in the concepts TS. */
29761 static tree
29762 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
29764 processing_constraint_expression_sentinel parsing_constraint;
29765 temp_override<int> ovr (processing_template_decl);
29766 if (!processing_template_decl)
29767 /* Adjust processing_template_decl so that we always obtain template
29768 trees here. We don't do the usual ++processing_template_decl
29769 because that would skew the template parameter depth of a lambda
29770 within if we're already inside a template. */
29771 processing_template_decl = 1;
29772 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
29773 if (check_for_bare_parameter_packs (expr))
29774 expr = error_mark_node;
29775 return expr;
29778 /* Parse a expression after a requires clause.
29780 constraint-expression:
29781 logical-or-expression
29783 The required logical-or-expression must be a constant expression. Note
29784 that we don't check that the expression is constepxr here. We defer until
29785 we analyze constraints and then, we only check atomic constraints. */
29787 static tree
29788 cp_parser_constraint_expression (cp_parser *parser)
29790 processing_constraint_expression_sentinel parsing_constraint;
29791 temp_override<int> ovr (processing_template_decl);
29792 if (!processing_template_decl)
29793 /* As in cp_parser_requires_clause_expression. */
29794 processing_template_decl = 1;
29795 cp_expr expr = cp_parser_binary_expression (parser, false, true,
29796 PREC_NOT_OPERATOR, NULL);
29797 if (check_for_bare_parameter_packs (expr))
29798 expr = error_mark_node;
29799 expr.maybe_add_location_wrapper ();
29800 return expr;
29803 /* Optionally parse a requires clause:
29805 requires-clause:
29806 `requires` constraint-logical-or-expression.
29807 [ConceptsTS]
29808 `requires constraint-expression.
29810 LAMBDA_P is true when the requires-clause is parsed before the
29811 parameter-list of a lambda-declarator. */
29813 static tree
29814 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
29816 /* A requires clause is an unevaluated context. */
29817 cp_unevaluated u;
29819 cp_token *tok = cp_lexer_peek_token (parser->lexer);
29820 if (tok->keyword != RID_REQUIRES)
29822 if (!flag_concepts && tok->type == CPP_NAME
29823 && tok->u.value == ridpointers[RID_REQUIRES])
29825 error_at (cp_lexer_peek_token (parser->lexer)->location,
29826 "%<requires%> only available with "
29827 "%<-std=c++20%> or %<-fconcepts%>");
29828 /* Parse and discard the requires-clause. */
29829 cp_lexer_consume_token (parser->lexer);
29830 cp_parser_constraint_expression (parser);
29832 return NULL_TREE;
29835 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
29836 if (tok2->type == CPP_OPEN_BRACE)
29838 /* An opening brace following the start of a requires-clause is
29839 ill-formed; the user likely forgot the second `requires' that
29840 would start a requires-expression. */
29841 gcc_rich_location richloc (tok2->location);
29842 richloc.add_fixit_insert_after (tok->location, " requires");
29843 error_at (&richloc, "missing additional %<requires%> to start "
29844 "a requires-expression");
29845 /* Don't consume the `requires', so that it's reused as the start of a
29846 requires-expression. */
29848 else
29849 cp_lexer_consume_token (parser->lexer);
29851 if (!flag_concepts_ts)
29852 return cp_parser_requires_clause_expression (parser, lambda_p);
29853 else
29854 return cp_parser_constraint_expression (parser);
29857 /*---------------------------------------------------------------------------
29858 Requires expressions
29859 ---------------------------------------------------------------------------*/
29861 /* Parse a requires expression
29863 requirement-expression:
29864 'requires' requirement-parameter-list [opt] requirement-body */
29866 static tree
29867 cp_parser_requires_expression (cp_parser *parser)
29869 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
29870 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
29872 /* Avoid committing to outer tentative parse. */
29873 tentative_firewall firewall (parser);
29875 /* This is definitely a requires-expression. */
29876 cp_parser_commit_to_tentative_parse (parser);
29878 tree parms, reqs;
29880 /* Local parameters are delared as variables within the scope
29881 of the expression. They are not visible past the end of
29882 the expression. Expressions within the requires-expression
29883 are unevaluated. */
29884 struct scope_sentinel
29886 scope_sentinel ()
29888 ++cp_unevaluated_operand;
29889 begin_scope (sk_block, NULL_TREE);
29892 ~scope_sentinel ()
29894 pop_bindings_and_leave_scope ();
29895 --cp_unevaluated_operand;
29897 } s;
29899 /* Parse the optional parameter list. */
29900 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29902 parms = cp_parser_requirement_parameter_list (parser);
29903 if (parms == error_mark_node)
29904 return error_mark_node;
29906 else
29907 parms = NULL_TREE;
29909 /* Parse the requirement body. */
29910 temp_override<int> ovr (processing_template_decl);
29911 if (!processing_template_decl)
29912 /* As in cp_parser_requires_clause_expression. */
29913 processing_template_decl = 1;
29914 reqs = cp_parser_requirement_body (parser);
29915 if (reqs == error_mark_node)
29916 return error_mark_node;
29919 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
29920 the parm chain. */
29921 grokparms (parms, &parms);
29922 loc = make_location (loc, loc, parser->lexer);
29923 tree expr = finish_requires_expr (loc, parms, reqs);
29924 if (!processing_template_decl)
29926 /* Perform semantic processing now to diagnose any invalid types and
29927 expressions. */
29928 int saved_errorcount = errorcount;
29929 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
29930 if (errorcount > saved_errorcount)
29931 return error_mark_node;
29933 return expr;
29936 /* Parse a parameterized requirement.
29938 requirement-parameter-list:
29939 '(' parameter-declaration-clause ')' */
29941 static tree
29942 cp_parser_requirement_parameter_list (cp_parser *parser)
29944 matching_parens parens;
29945 if (!parens.require_open (parser))
29946 return error_mark_node;
29948 tree parms = (cp_parser_parameter_declaration_clause
29949 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
29951 if (!parens.require_close (parser))
29952 return error_mark_node;
29954 /* Modify the declared parameters by removing their context
29955 so they don't refer to the enclosing scope and explicitly
29956 indicating that they are constraint variables. */
29957 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
29959 if (parm == void_list_node || parm == explicit_void_list_node)
29960 break;
29961 tree decl = TREE_VALUE (parm);
29962 if (decl != error_mark_node)
29964 DECL_CONTEXT (decl) = NULL_TREE;
29965 CONSTRAINT_VAR_P (decl) = true;
29969 return parms;
29972 /* Parse the body of a requirement.
29974 requirement-body:
29975 '{' requirement-list '}' */
29976 static tree
29977 cp_parser_requirement_body (cp_parser *parser)
29979 matching_braces braces;
29980 if (!braces.require_open (parser))
29981 return error_mark_node;
29983 tree reqs = cp_parser_requirement_seq (parser);
29985 if (!braces.require_close (parser))
29986 return error_mark_node;
29988 return reqs;
29991 /* Parse a sequence of requirements.
29993 requirement-seq:
29994 requirement
29995 requirement-seq requirement */
29997 static tree
29998 cp_parser_requirement_seq (cp_parser *parser)
30000 tree result = NULL_TREE;
30003 tree req = cp_parser_requirement (parser);
30004 if (req != error_mark_node)
30005 result = tree_cons (NULL_TREE, req, result);
30007 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
30008 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
30010 /* If there are no valid requirements, this is not a valid expression. */
30011 if (!result)
30012 return error_mark_node;
30014 /* Reverse the order of requirements so they are analyzed in order. */
30015 return nreverse (result);
30018 /* Parse a syntactic requirement or type requirement.
30020 requirement:
30021 simple-requirement
30022 compound-requirement
30023 type-requirement
30024 nested-requirement */
30026 static tree
30027 cp_parser_requirement (cp_parser *parser)
30029 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30030 return cp_parser_compound_requirement (parser);
30031 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
30032 return cp_parser_type_requirement (parser);
30033 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
30034 return cp_parser_nested_requirement (parser);
30035 else
30036 return cp_parser_simple_requirement (parser);
30039 /* Parse a simple requirement.
30041 simple-requirement:
30042 expression ';' */
30044 static tree
30045 cp_parser_simple_requirement (cp_parser *parser)
30047 location_t start = cp_lexer_peek_token (parser->lexer)->location;
30048 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
30049 if (expr == error_mark_node)
30050 cp_parser_skip_to_end_of_statement (parser);
30052 cp_parser_consume_semicolon_at_end_of_statement (parser);
30054 if (!expr || expr == error_mark_node)
30055 return error_mark_node;
30057 /* Sometimes we don't get locations, so use the cached token location
30058 as a reasonable approximation. */
30059 if (expr.get_location() == UNKNOWN_LOCATION)
30060 expr.set_location (start);
30062 for (tree t = expr; ; )
30064 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
30065 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
30067 t = TREE_OPERAND (t, 0);
30068 continue;
30070 if (concept_check_p (t))
30072 gcc_rich_location richloc (get_start (start));
30073 richloc.add_fixit_insert_before (start, "requires ");
30074 warning_at (&richloc, OPT_Wmissing_requires, "testing "
30075 "if a concept-id is a valid expression; add "
30076 "%<requires%> to check satisfaction");
30078 break;
30081 return finish_simple_requirement (expr.get_location (), expr);
30084 /* Parse a type requirement
30086 type-requirement
30087 nested-name-specifier [opt] required-type-name ';'
30089 required-type-name:
30090 type-name
30091 'template' [opt] simple-template-id */
30093 static tree
30094 cp_parser_type_requirement (cp_parser *parser)
30096 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
30097 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30099 // Save the scope before parsing name specifiers.
30100 tree saved_scope = parser->scope;
30101 tree saved_object_scope = parser->object_scope;
30102 tree saved_qualifying_scope = parser->qualifying_scope;
30103 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
30104 cp_parser_nested_name_specifier_opt (parser,
30105 /*typename_keyword_p=*/true,
30106 /*check_dependency_p=*/false,
30107 /*type_p=*/true,
30108 /*is_declaration=*/false);
30110 tree type;
30111 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30113 cp_lexer_consume_token (parser->lexer);
30114 type = cp_parser_template_id (parser,
30115 /*template_keyword_p=*/true,
30116 /*check_dependency=*/false,
30117 /*tag_type=*/none_type,
30118 /*is_declaration=*/false);
30119 type = make_typename_type (parser->scope, type, typename_type,
30120 /*complain=*/tf_error);
30122 else
30123 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
30125 if (TREE_CODE (type) == TYPE_DECL)
30126 type = TREE_TYPE (type);
30128 parser->scope = saved_scope;
30129 parser->object_scope = saved_object_scope;
30130 parser->qualifying_scope = saved_qualifying_scope;
30132 if (type == error_mark_node)
30133 cp_parser_skip_to_end_of_statement (parser);
30135 cp_parser_consume_semicolon_at_end_of_statement (parser);
30137 if (type == error_mark_node)
30138 return error_mark_node;
30140 loc = make_location (loc, start_tok->location, parser->lexer);
30141 return finish_type_requirement (loc, type);
30144 /* Parse a compound requirement
30146 compound-requirement:
30147 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
30149 static tree
30150 cp_parser_compound_requirement (cp_parser *parser)
30152 /* Parse an expression enclosed in '{ }'s. */
30153 matching_braces braces;
30154 if (!braces.require_open (parser))
30155 return error_mark_node;
30157 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
30159 tree expr = cp_parser_expression (parser, NULL, false, false);
30160 if (expr == error_mark_node)
30161 cp_parser_skip_to_closing_brace (parser);
30163 if (!braces.require_close (parser))
30165 cp_parser_skip_to_end_of_statement (parser);
30166 cp_parser_consume_semicolon_at_end_of_statement (parser);
30167 return error_mark_node;
30170 /* If the expression was invalid, skip the remainder of the requirement. */
30171 if (!expr || expr == error_mark_node)
30173 cp_parser_skip_to_end_of_statement (parser);
30174 cp_parser_consume_semicolon_at_end_of_statement (parser);
30175 return error_mark_node;
30178 /* Parse the optional noexcept. */
30179 bool noexcept_p = false;
30180 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
30182 cp_lexer_consume_token (parser->lexer);
30183 noexcept_p = true;
30186 /* Parse the optional trailing return type. */
30187 tree type = NULL_TREE;
30188 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
30190 cp_lexer_consume_token (parser->lexer);
30191 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30193 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
30194 parser->in_result_type_constraint_p = true;
30195 /* C++20 allows either a type-id or a type-constraint. Parsing
30196 a type-id will subsume the parsing for a type-constraint but
30197 allow for more syntactic forms (e.g., const C<T>*). */
30198 type = cp_parser_trailing_type_id (parser);
30199 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
30200 if (type == error_mark_node)
30201 return error_mark_node;
30203 location_t type_loc = make_location (tok->location, tok->location,
30204 parser->lexer);
30206 /* Check that we haven't written something like 'const C<T>*'. */
30207 if (type_uses_auto (type))
30209 if (!is_auto (type))
30211 error_at (type_loc,
30212 "result type is not a plain type-constraint");
30213 cp_parser_consume_semicolon_at_end_of_statement (parser);
30214 return error_mark_node;
30217 else if (!flag_concepts_ts)
30218 /* P1452R2 removed the trailing-return-type option. */
30219 error_at (type_loc,
30220 "return-type-requirement is not a type-constraint");
30223 location_t loc = make_location (expr_token->location,
30224 braces.open_location (),
30225 parser->lexer);
30227 cp_parser_consume_semicolon_at_end_of_statement (parser);
30229 if (expr == error_mark_node || type == error_mark_node)
30230 return error_mark_node;
30232 return finish_compound_requirement (loc, expr, type, noexcept_p);
30235 /* Parse a nested requirement. This is the same as a requires clause.
30237 nested-requirement:
30238 requires-clause */
30240 static tree
30241 cp_parser_nested_requirement (cp_parser *parser)
30243 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30244 cp_token *tok = cp_lexer_consume_token (parser->lexer);
30245 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30246 tree req = cp_parser_constraint_expression (parser);
30247 if (req == error_mark_node)
30248 cp_parser_skip_to_end_of_statement (parser);
30249 loc = make_location (loc, tok->location, parser->lexer);
30250 cp_parser_consume_semicolon_at_end_of_statement (parser);
30251 if (req == error_mark_node)
30252 return error_mark_node;
30253 return finish_nested_requirement (loc, req);
30256 /* Support Functions */
30258 /* Return the appropriate prefer_type argument for lookup_name based on
30259 tag_type. */
30261 static inline LOOK_want
30262 prefer_type_arg (tag_types tag_type)
30264 switch (tag_type)
30266 case none_type: return LOOK_want::NORMAL; // No preference.
30267 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
30268 default: return LOOK_want::TYPE; // Type only.
30272 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
30273 NAME should have one of the representations used for an
30274 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
30275 is returned. If PARSER->SCOPE is a dependent type, then a
30276 SCOPE_REF is returned.
30278 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
30279 returned; the name was already resolved when the TEMPLATE_ID_EXPR
30280 was formed. Abstractly, such entities should not be passed to this
30281 function, because they do not need to be looked up, but it is
30282 simpler to check for this special case here, rather than at the
30283 call-sites.
30285 In cases not explicitly covered above, this function returns a
30286 DECL, OVERLOAD, or baselink representing the result of the lookup.
30287 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
30288 is returned.
30290 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
30291 (e.g., "struct") that was used. In that case bindings that do not
30292 refer to types are ignored.
30294 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
30295 ignored.
30297 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
30298 are ignored.
30300 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
30301 types.
30303 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
30304 TREE_LIST of candidates if name-lookup results in an ambiguity, and
30305 NULL_TREE otherwise. */
30307 static cp_expr
30308 cp_parser_lookup_name (cp_parser *parser, tree name,
30309 enum tag_types tag_type,
30310 bool is_template,
30311 bool is_namespace,
30312 bool check_dependency,
30313 tree *ambiguous_decls,
30314 location_t name_location)
30316 tree decl;
30317 tree object_type = parser->context->object_type;
30319 /* Assume that the lookup will be unambiguous. */
30320 if (ambiguous_decls)
30321 *ambiguous_decls = NULL_TREE;
30323 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
30324 no longer valid. Note that if we are parsing tentatively, and
30325 the parse fails, OBJECT_TYPE will be automatically restored. */
30326 parser->context->object_type = NULL_TREE;
30328 if (name == error_mark_node)
30329 return error_mark_node;
30331 /* A template-id has already been resolved; there is no lookup to
30332 do. */
30333 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
30334 return name;
30335 if (BASELINK_P (name))
30337 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
30338 == TEMPLATE_ID_EXPR);
30339 return name;
30342 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
30343 it should already have been checked to make sure that the name
30344 used matches the type being destroyed. */
30345 if (TREE_CODE (name) == BIT_NOT_EXPR)
30347 tree type;
30349 /* Figure out to which type this destructor applies. */
30350 if (parser->scope)
30351 type = parser->scope;
30352 else if (object_type)
30353 type = object_type;
30354 else
30355 type = current_class_type;
30356 /* If that's not a class type, there is no destructor. */
30357 if (!type || !CLASS_TYPE_P (type))
30358 return error_mark_node;
30360 /* In a non-static member function, check implicit this->. */
30361 if (current_class_ref)
30362 return lookup_destructor (current_class_ref, parser->scope, name,
30363 tf_warning_or_error);
30365 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
30366 lazily_declare_fn (sfk_destructor, type);
30368 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
30369 return dtor;
30371 return error_mark_node;
30374 /* By this point, the NAME should be an ordinary identifier. If
30375 the id-expression was a qualified name, the qualifying scope is
30376 stored in PARSER->SCOPE at this point. */
30377 gcc_assert (identifier_p (name));
30379 /* Perform the lookup. */
30380 if (parser->scope)
30382 bool dependent_p;
30384 if (parser->scope == error_mark_node)
30385 return error_mark_node;
30387 /* If the SCOPE is dependent, the lookup must be deferred until
30388 the template is instantiated -- unless we are explicitly
30389 looking up names in uninstantiated templates. Even then, we
30390 cannot look up the name if the scope is not a class type; it
30391 might, for example, be a template type parameter. */
30392 dependent_p = (TYPE_P (parser->scope)
30393 && dependent_scope_p (parser->scope));
30394 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
30395 && dependent_p)
30396 /* Defer lookup. */
30397 decl = error_mark_node;
30398 else
30400 tree pushed_scope = NULL_TREE;
30402 /* If PARSER->SCOPE is a dependent type, then it must be a
30403 class type, and we must not be checking dependencies;
30404 otherwise, we would have processed this lookup above. So
30405 that PARSER->SCOPE is not considered a dependent base by
30406 lookup_member, we must enter the scope here. */
30407 if (dependent_p)
30408 pushed_scope = push_scope (parser->scope);
30410 /* If the PARSER->SCOPE is a template specialization, it
30411 may be instantiated during name lookup. In that case,
30412 errors may be issued. Even if we rollback the current
30413 tentative parse, those errors are valid. */
30414 decl = lookup_qualified_name (parser->scope, name,
30415 prefer_type_arg (tag_type),
30416 /*complain=*/true);
30418 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
30419 lookup result and the nested-name-specifier nominates a class C:
30420 * if the name specified after the nested-name-specifier, when
30421 looked up in C, is the injected-class-name of C (Clause 9), or
30422 * if the name specified after the nested-name-specifier is the
30423 same as the identifier or the simple-template-id's template-
30424 name in the last component of the nested-name-specifier,
30425 the name is instead considered to name the constructor of
30426 class C. [ Note: for example, the constructor is not an
30427 acceptable lookup result in an elaborated-type-specifier so
30428 the constructor would not be used in place of the
30429 injected-class-name. --end note ] Such a constructor name
30430 shall be used only in the declarator-id of a declaration that
30431 names a constructor or in a using-declaration. */
30432 if (tag_type == none_type
30433 && DECL_SELF_REFERENCE_P (decl)
30434 && same_type_p (DECL_CONTEXT (decl), parser->scope))
30435 decl = lookup_qualified_name (parser->scope, ctor_identifier,
30436 prefer_type_arg (tag_type),
30437 /*complain=*/true);
30439 if (pushed_scope)
30440 pop_scope (pushed_scope);
30443 /* If the scope is a dependent type and either we deferred lookup or
30444 we did lookup but didn't find the name, rememeber the name. */
30445 if (decl == error_mark_node && TYPE_P (parser->scope)
30446 && dependent_type_p (parser->scope))
30448 if (tag_type)
30450 tree type;
30452 /* The resolution to Core Issue 180 says that `struct
30453 A::B' should be considered a type-name, even if `A'
30454 is dependent. */
30455 type = make_typename_type (parser->scope, name, tag_type,
30456 /*complain=*/tf_error);
30457 if (type != error_mark_node)
30458 decl = TYPE_NAME (type);
30460 else if (is_template
30461 && (cp_parser_next_token_ends_template_argument_p (parser)
30462 || cp_lexer_next_token_is (parser->lexer,
30463 CPP_CLOSE_PAREN)))
30464 decl = make_unbound_class_template (parser->scope,
30465 name, NULL_TREE,
30466 /*complain=*/tf_error);
30467 else
30468 decl = build_qualified_name (/*type=*/NULL_TREE,
30469 parser->scope, name,
30470 is_template);
30472 parser->qualifying_scope = parser->scope;
30473 parser->object_scope = NULL_TREE;
30475 else if (object_type)
30477 /* Look up the name in the scope of the OBJECT_TYPE, unless the
30478 OBJECT_TYPE is not a class. */
30479 if (CLASS_TYPE_P (object_type))
30480 /* If the OBJECT_TYPE is a template specialization, it may
30481 be instantiated during name lookup. In that case, errors
30482 may be issued. Even if we rollback the current tentative
30483 parse, those errors are valid. */
30484 decl = lookup_member (object_type,
30485 name,
30486 /*protect=*/0,
30487 /*prefer_type=*/tag_type != none_type,
30488 tf_warning_or_error);
30489 else
30490 decl = NULL_TREE;
30492 if (!decl)
30493 /* Look it up in the enclosing context. DR 141: When looking for a
30494 template-name after -> or ., only consider class templates. */
30495 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30496 /* DR 141: When looking in the
30497 current enclosing context for a
30498 template-name after -> or ., only
30499 consider class templates. */
30500 : is_template ? LOOK_want::TYPE
30501 : prefer_type_arg (tag_type));
30503 /* If we know we're looking for a type (e.g. A in p->A::x),
30504 mock up a typename. */
30505 if (!decl && object_type && tag_type != none_type
30506 && dependentish_scope_p (object_type))
30508 tree type = build_typename_type (object_type, name, name,
30509 typename_type);
30510 decl = TYPE_NAME (type);
30513 parser->object_scope = object_type;
30514 parser->qualifying_scope = NULL_TREE;
30516 else
30518 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30519 : prefer_type_arg (tag_type));
30520 parser->qualifying_scope = NULL_TREE;
30521 parser->object_scope = NULL_TREE;
30524 /* If the lookup failed, let our caller know. */
30525 if (!decl || decl == error_mark_node)
30526 return error_mark_node;
30528 /* If we have resolved the name of a member declaration, check to
30529 see if the declaration is accessible. When the name resolves to
30530 set of overloaded functions, accessibility is checked when
30531 overload resolution is done. If we have a TREE_LIST, then the lookup
30532 is either ambiguous or it found multiple injected-class-names, the
30533 accessibility of which is trivially satisfied.
30535 During an explicit instantiation, access is not checked at all,
30536 as per [temp.explicit]. */
30537 if (DECL_P (decl))
30538 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
30539 tf_warning_or_error);
30541 /* Pull out the template from an injected-class-name (or multiple). */
30542 if (is_template)
30543 decl = maybe_get_template_decl_from_type_decl (decl);
30545 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
30546 if (TREE_CODE (decl) == TREE_LIST)
30548 if (ambiguous_decls)
30549 *ambiguous_decls = decl;
30550 /* The error message we have to print is too complicated for
30551 cp_parser_error, so we incorporate its actions directly. */
30552 if (!cp_parser_simulate_error (parser))
30554 error_at (name_location, "reference to %qD is ambiguous",
30555 name);
30556 print_candidates (decl);
30558 return error_mark_node;
30561 gcc_assert (DECL_P (decl)
30562 || TREE_CODE (decl) == OVERLOAD
30563 || TREE_CODE (decl) == SCOPE_REF
30564 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
30565 || BASELINK_P (decl));
30567 maybe_record_typedef_use (decl);
30569 return cp_expr (decl, name_location);
30572 /* Like cp_parser_lookup_name, but for use in the typical case where
30573 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
30574 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
30576 static tree
30577 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
30579 return cp_parser_lookup_name (parser, name,
30580 none_type,
30581 /*is_template=*/false,
30582 /*is_namespace=*/false,
30583 /*check_dependency=*/true,
30584 /*ambiguous_decls=*/NULL,
30585 location);
30588 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
30589 the current context, return the TYPE_DECL. If TAG_NAME_P is
30590 true, the DECL indicates the class being defined in a class-head,
30591 or declared in an elaborated-type-specifier.
30593 Otherwise, return DECL. */
30595 static tree
30596 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
30598 /* If the TEMPLATE_DECL is being declared as part of a class-head,
30599 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
30601 struct A {
30602 template <typename T> struct B;
30605 template <typename T> struct A::B {};
30607 Similarly, in an elaborated-type-specifier:
30609 namespace N { struct X{}; }
30611 struct A {
30612 template <typename T> friend struct N::X;
30615 However, if the DECL refers to a class type, and we are in
30616 the scope of the class, then the name lookup automatically
30617 finds the TYPE_DECL created by build_self_reference rather
30618 than a TEMPLATE_DECL. For example, in:
30620 template <class T> struct S {
30621 S s;
30624 there is no need to handle such case. */
30626 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
30627 return DECL_TEMPLATE_RESULT (decl);
30629 return decl;
30632 /* If too many, or too few, template-parameter lists apply to the
30633 declarator, issue an error message. Returns TRUE if all went well,
30634 and FALSE otherwise. */
30636 static bool
30637 cp_parser_check_declarator_template_parameters (cp_parser* parser,
30638 cp_declarator *declarator,
30639 location_t declarator_location)
30641 switch (declarator->kind)
30643 case cdk_id:
30645 unsigned num_templates = 0;
30646 tree scope = declarator->u.id.qualifying_scope;
30647 bool template_id_p = false;
30649 if (scope)
30650 num_templates = num_template_headers_for_class (scope);
30651 else if (TREE_CODE (declarator->u.id.unqualified_name)
30652 == TEMPLATE_ID_EXPR)
30654 /* If the DECLARATOR has the form `X<y>' then it uses one
30655 additional level of template parameters. */
30656 ++num_templates;
30657 template_id_p = true;
30660 return cp_parser_check_template_parameters
30661 (parser, num_templates, template_id_p, declarator_location,
30662 declarator);
30665 case cdk_function:
30666 case cdk_array:
30667 case cdk_pointer:
30668 case cdk_reference:
30669 case cdk_ptrmem:
30670 return (cp_parser_check_declarator_template_parameters
30671 (parser, declarator->declarator, declarator_location));
30673 case cdk_decomp:
30674 case cdk_error:
30675 return true;
30677 default:
30678 gcc_unreachable ();
30680 return false;
30683 /* NUM_TEMPLATES were used in the current declaration. If that is
30684 invalid, return FALSE and issue an error messages. Otherwise,
30685 return TRUE. If DECLARATOR is non-NULL, then we are checking a
30686 declarator and we can print more accurate diagnostics. */
30688 static bool
30689 cp_parser_check_template_parameters (cp_parser* parser,
30690 unsigned num_templates,
30691 bool template_id_p,
30692 location_t location,
30693 cp_declarator *declarator)
30695 /* If there are the same number of template classes and parameter
30696 lists, that's OK. */
30697 if (parser->num_template_parameter_lists == num_templates)
30698 return true;
30699 /* If there are more, but only one more, and the name ends in an identifier,
30700 then we are declaring a primary template. That's OK too. */
30701 if (!template_id_p
30702 && parser->num_template_parameter_lists == num_templates + 1)
30703 return true;
30705 if (cp_parser_simulate_error (parser))
30706 return false;
30708 /* If there are more template classes than parameter lists, we have
30709 something like:
30711 template <class T> void S<T>::R<T>::f (); */
30712 if (parser->num_template_parameter_lists < num_templates)
30714 if (declarator && !current_function_decl)
30715 error_at (location, "specializing member %<%T::%E%> "
30716 "requires %<template<>%> syntax",
30717 declarator->u.id.qualifying_scope,
30718 declarator->u.id.unqualified_name);
30719 else if (declarator)
30720 error_at (location, "invalid declaration of %<%T::%E%>",
30721 declarator->u.id.qualifying_scope,
30722 declarator->u.id.unqualified_name);
30723 else
30724 error_at (location, "too few template-parameter-lists");
30725 return false;
30727 /* Otherwise, there are too many template parameter lists. We have
30728 something like:
30730 template <class T> template <class U> void S::f(); */
30731 error_at (location, "too many template-parameter-lists");
30732 return false;
30735 /* Parse an optional `::' token indicating that the following name is
30736 from the global namespace. If so, PARSER->SCOPE is set to the
30737 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
30738 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
30739 Returns the new value of PARSER->SCOPE, if the `::' token is
30740 present, and NULL_TREE otherwise. */
30742 static tree
30743 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
30745 cp_token *token;
30747 /* Peek at the next token. */
30748 token = cp_lexer_peek_token (parser->lexer);
30749 /* If we're looking at a `::' token then we're starting from the
30750 global namespace, not our current location. */
30751 if (token->type == CPP_SCOPE)
30753 /* Consume the `::' token. */
30754 cp_lexer_consume_token (parser->lexer);
30755 /* Set the SCOPE so that we know where to start the lookup. */
30756 parser->scope = global_namespace;
30757 parser->qualifying_scope = global_namespace;
30758 parser->object_scope = NULL_TREE;
30760 return parser->scope;
30762 else if (!current_scope_valid_p)
30764 parser->scope = NULL_TREE;
30765 parser->qualifying_scope = NULL_TREE;
30766 parser->object_scope = NULL_TREE;
30769 return NULL_TREE;
30772 /* Returns TRUE if the upcoming token sequence is the start of a
30773 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
30774 declarator is preceded by the `friend' specifier. The parser flags FLAGS
30775 is used to control type-specifier parsing. */
30777 static bool
30778 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
30779 bool friend_p)
30781 bool constructor_p;
30782 bool outside_class_specifier_p;
30783 tree nested_name_specifier;
30784 cp_token *next_token;
30786 /* The common case is that this is not a constructor declarator, so
30787 try to avoid doing lots of work if at all possible. It's not
30788 valid declare a constructor at function scope. */
30789 if (parser->in_function_body)
30790 return false;
30791 /* And only certain tokens can begin a constructor declarator. */
30792 next_token = cp_lexer_peek_token (parser->lexer);
30793 if (next_token->type != CPP_NAME
30794 && next_token->type != CPP_SCOPE
30795 && next_token->type != CPP_NESTED_NAME_SPECIFIER
30796 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
30797 declarator-id of a constructor or destructor. */
30798 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
30799 return false;
30801 /* Parse tentatively; we are going to roll back all of the tokens
30802 consumed here. */
30803 cp_parser_parse_tentatively (parser);
30804 /* Assume that we are looking at a constructor declarator. */
30805 constructor_p = true;
30807 /* Look for the optional `::' operator. */
30808 cp_parser_global_scope_opt (parser,
30809 /*current_scope_valid_p=*/false);
30810 /* Look for the nested-name-specifier. */
30811 nested_name_specifier
30812 = (cp_parser_nested_name_specifier_opt (parser,
30813 /*typename_keyword_p=*/false,
30814 /*check_dependency_p=*/false,
30815 /*type_p=*/false,
30816 /*is_declaration=*/false));
30818 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
30819 if (nested_name_specifier
30820 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
30822 tree s = resolve_typename_type (nested_name_specifier,
30823 /*only_current_p=*/false);
30824 if (TREE_CODE (s) != TYPENAME_TYPE)
30825 nested_name_specifier = s;
30828 outside_class_specifier_p = (!at_class_scope_p ()
30829 || !TYPE_BEING_DEFINED (current_class_type)
30830 || friend_p);
30832 /* Outside of a class-specifier, there must be a
30833 nested-name-specifier. Except in C++17 mode, where we
30834 might be declaring a guiding declaration. */
30835 if (!nested_name_specifier && outside_class_specifier_p
30836 && cxx_dialect < cxx17)
30837 constructor_p = false;
30838 else if (nested_name_specifier == error_mark_node)
30839 constructor_p = false;
30841 /* If we have a class scope, this is easy; DR 147 says that S::S always
30842 names the constructor, and no other qualified name could. */
30843 if (constructor_p && nested_name_specifier
30844 && CLASS_TYPE_P (nested_name_specifier))
30846 tree id = cp_parser_unqualified_id (parser,
30847 /*template_keyword_p=*/false,
30848 /*check_dependency_p=*/false,
30849 /*declarator_p=*/true,
30850 /*optional_p=*/false);
30851 if (is_overloaded_fn (id))
30852 id = DECL_NAME (get_first_fn (id));
30853 if (!constructor_name_p (id, nested_name_specifier))
30854 constructor_p = false;
30856 /* If we still think that this might be a constructor-declarator,
30857 look for a class-name. */
30858 else if (constructor_p)
30860 /* If we have:
30862 template <typename T> struct S {
30863 S();
30866 we must recognize that the nested `S' names a class. */
30867 if (cxx_dialect >= cxx17)
30868 cp_parser_parse_tentatively (parser);
30870 tree type_decl;
30871 type_decl = cp_parser_class_name (parser,
30872 /*typename_keyword_p=*/false,
30873 /*template_keyword_p=*/false,
30874 none_type,
30875 /*check_dependency_p=*/false,
30876 /*class_head_p=*/false,
30877 /*is_declaration=*/false);
30879 if (cxx_dialect >= cxx17
30880 && !cp_parser_parse_definitely (parser))
30882 type_decl = NULL_TREE;
30883 tree tmpl = cp_parser_template_name (parser,
30884 /*template_keyword*/false,
30885 /*check_dependency_p*/false,
30886 /*is_declaration*/false,
30887 none_type,
30888 /*is_identifier*/NULL);
30889 if (DECL_CLASS_TEMPLATE_P (tmpl)
30890 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
30891 /* It's a deduction guide, return true. */;
30892 else
30893 cp_parser_simulate_error (parser);
30896 /* If there was no class-name, then this is not a constructor.
30897 Otherwise, if we are in a class-specifier and we aren't
30898 handling a friend declaration, check that its type matches
30899 current_class_type (c++/38313). Note: error_mark_node
30900 is left alone for error recovery purposes. */
30901 constructor_p = (!cp_parser_error_occurred (parser)
30902 && (outside_class_specifier_p
30903 || type_decl == NULL_TREE
30904 || type_decl == error_mark_node
30905 || same_type_p (current_class_type,
30906 TREE_TYPE (type_decl))));
30908 /* If we're still considering a constructor, we have to see a `(',
30909 to begin the parameter-declaration-clause, followed by either a
30910 `)', an `...', or a decl-specifier. We need to check for a
30911 type-specifier to avoid being fooled into thinking that:
30913 S (f) (int);
30915 is a constructor. (It is actually a function named `f' that
30916 takes one parameter (of type `int') and returns a value of type
30917 `S'. */
30918 if (constructor_p
30919 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30920 constructor_p = false;
30922 if (constructor_p
30923 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
30924 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
30925 /* A parameter declaration begins with a decl-specifier,
30926 which is either the "attribute" keyword, a storage class
30927 specifier, or (usually) a type-specifier. */
30928 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
30929 /* GNU attributes can actually appear both at the start of
30930 a parameter and parenthesized declarator.
30931 S (__attribute__((unused)) int);
30932 is a constructor, but
30933 S (__attribute__((unused)) foo) (int);
30934 is a function declaration. [[attribute]] can appear in the
30935 first form too, but not in the second form. */
30936 && !cp_next_tokens_can_be_std_attribute_p (parser))
30938 tree type;
30939 tree pushed_scope = NULL_TREE;
30940 unsigned saved_num_template_parameter_lists;
30942 if (cp_parser_allow_gnu_extensions_p (parser)
30943 && cp_next_tokens_can_be_gnu_attribute_p (parser))
30945 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
30946 while (--n)
30947 cp_lexer_consume_token (parser->lexer);
30950 /* Names appearing in the type-specifier should be looked up
30951 in the scope of the class. */
30952 if (current_class_type)
30953 type = NULL_TREE;
30954 else if (type_decl)
30956 type = TREE_TYPE (type_decl);
30957 if (TREE_CODE (type) == TYPENAME_TYPE)
30959 type = resolve_typename_type (type,
30960 /*only_current_p=*/false);
30961 if (TREE_CODE (type) == TYPENAME_TYPE)
30963 cp_parser_abort_tentative_parse (parser);
30964 return false;
30967 pushed_scope = push_scope (type);
30970 /* Inside the constructor parameter list, surrounding
30971 template-parameter-lists do not apply. */
30972 saved_num_template_parameter_lists
30973 = parser->num_template_parameter_lists;
30974 parser->num_template_parameter_lists = 0;
30976 /* Look for the type-specifier. It's not optional, but its typename
30977 might be. Unless this is a friend declaration; we don't want to
30978 treat
30980 friend S (T::fn)(int);
30982 as a constructor, but with P0634, we might assume a type when
30983 looking for the type-specifier. It is actually a function named
30984 `T::fn' that takes one parameter (of type `int') and returns a
30985 value of type `S'. Constructors can be friends, but they must
30986 use a qualified name.
30988 Parse with an empty set of declaration specifiers since we're
30989 trying to match a decl-specifier-seq of the first parameter.
30990 This must be non-null so that cp_parser_simple_type_specifier
30991 will recognize a constrained placeholder type such as:
30992 'C<int> auto' where C is a type concept. */
30993 cp_decl_specifier_seq ctor_specs;
30994 clear_decl_specs (&ctor_specs);
30995 cp_parser_type_specifier (parser,
30996 (friend_p ? CP_PARSER_FLAGS_NONE
30997 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
30998 /*decl_specs=*/&ctor_specs,
30999 /*is_declarator=*/true,
31000 /*declares_class_or_enum=*/NULL,
31001 /*is_cv_qualifier=*/NULL);
31003 parser->num_template_parameter_lists
31004 = saved_num_template_parameter_lists;
31006 /* Leave the scope of the class. */
31007 if (pushed_scope)
31008 pop_scope (pushed_scope);
31010 constructor_p = !cp_parser_error_occurred (parser);
31014 /* We did not really want to consume any tokens. */
31015 cp_parser_abort_tentative_parse (parser);
31017 return constructor_p;
31020 /* Parse the definition of the function given by the DECL_SPECIFIERS,
31021 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
31022 they must be performed once we are in the scope of the function.
31024 Returns the function defined. */
31026 static tree
31027 cp_parser_function_definition_from_specifiers_and_declarator
31028 (cp_parser* parser,
31029 cp_decl_specifier_seq *decl_specifiers,
31030 tree attributes,
31031 const cp_declarator *declarator)
31033 tree fn;
31034 bool success_p;
31036 /* Begin the function-definition. */
31037 success_p = start_function (decl_specifiers, declarator, attributes);
31039 /* The things we're about to see are not directly qualified by any
31040 template headers we've seen thus far. */
31041 reset_specialization ();
31043 /* If there were names looked up in the decl-specifier-seq that we
31044 did not check, check them now. We must wait until we are in the
31045 scope of the function to perform the checks, since the function
31046 might be a friend. */
31047 perform_deferred_access_checks (tf_warning_or_error);
31049 if (success_p)
31051 cp_finalize_omp_declare_simd (parser, current_function_decl);
31052 parser->omp_declare_simd = NULL;
31053 cp_finalize_oacc_routine (parser, current_function_decl, true);
31054 parser->oacc_routine = NULL;
31057 if (!success_p)
31059 /* Skip the entire function. */
31060 cp_parser_skip_to_end_of_block_or_statement (parser);
31061 fn = error_mark_node;
31063 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
31065 /* Seen already, skip it. An error message has already been output. */
31066 cp_parser_skip_to_end_of_block_or_statement (parser);
31067 fn = current_function_decl;
31068 current_function_decl = NULL_TREE;
31069 /* If this is a function from a class, pop the nested class. */
31070 if (current_class_name)
31071 pop_nested_class ();
31073 else
31075 timevar_id_t tv;
31076 if (DECL_DECLARED_INLINE_P (current_function_decl))
31077 tv = TV_PARSE_INLINE;
31078 else
31079 tv = TV_PARSE_FUNC;
31080 timevar_push (tv);
31081 fn = cp_parser_function_definition_after_declarator (parser,
31082 /*inline_p=*/false);
31083 timevar_pop (tv);
31086 return fn;
31089 /* Parse the part of a function-definition that follows the
31090 declarator. INLINE_P is TRUE iff this function is an inline
31091 function defined within a class-specifier.
31093 Returns the function defined. */
31095 static tree
31096 cp_parser_function_definition_after_declarator (cp_parser* parser,
31097 bool inline_p)
31099 tree fn;
31100 bool saved_in_unbraced_linkage_specification_p;
31101 bool saved_in_function_body;
31102 unsigned saved_num_template_parameter_lists;
31103 cp_token *token;
31104 bool fully_implicit_function_template_p
31105 = parser->fully_implicit_function_template_p;
31106 parser->fully_implicit_function_template_p = false;
31107 tree implicit_template_parms
31108 = parser->implicit_template_parms;
31109 parser->implicit_template_parms = 0;
31110 cp_binding_level* implicit_template_scope
31111 = parser->implicit_template_scope;
31112 parser->implicit_template_scope = 0;
31114 saved_in_function_body = parser->in_function_body;
31115 parser->in_function_body = true;
31116 /* If the next token is `return', then the code may be trying to
31117 make use of the "named return value" extension that G++ used to
31118 support. */
31119 token = cp_lexer_peek_token (parser->lexer);
31120 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
31122 /* Consume the `return' keyword. */
31123 cp_lexer_consume_token (parser->lexer);
31124 /* Look for the identifier that indicates what value is to be
31125 returned. */
31126 cp_parser_identifier (parser);
31127 /* Issue an error message. */
31128 error_at (token->location,
31129 "named return values are no longer supported");
31130 /* Skip tokens until we reach the start of the function body. */
31131 while (true)
31133 cp_token *token = cp_lexer_peek_token (parser->lexer);
31134 if (token->type == CPP_OPEN_BRACE
31135 || token->type == CPP_EOF
31136 || token->type == CPP_PRAGMA_EOL)
31137 break;
31138 cp_lexer_consume_token (parser->lexer);
31141 /* The `extern' in `extern "C" void f () { ... }' does not apply to
31142 anything declared inside `f'. */
31143 saved_in_unbraced_linkage_specification_p
31144 = parser->in_unbraced_linkage_specification_p;
31145 parser->in_unbraced_linkage_specification_p = false;
31146 /* Inside the function, surrounding template-parameter-lists do not
31147 apply. */
31148 saved_num_template_parameter_lists
31149 = parser->num_template_parameter_lists;
31150 parser->num_template_parameter_lists = 0;
31152 /* If the next token is `try', `__transaction_atomic', or
31153 `__transaction_relaxed`, then we are looking at either function-try-block
31154 or function-transaction-block. Note that all of these include the
31155 function-body. */
31156 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
31157 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
31158 else if (cp_lexer_next_token_is_keyword (parser->lexer,
31159 RID_TRANSACTION_RELAXED))
31160 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
31161 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31162 cp_parser_function_try_block (parser);
31163 else
31164 cp_parser_ctor_initializer_opt_and_function_body
31165 (parser, /*in_function_try_block=*/false);
31167 /* Finish the function. */
31168 fn = finish_function (inline_p);
31170 if (modules_p ()
31171 && !inline_p
31172 && TYPE_P (DECL_CONTEXT (fn))
31173 && (DECL_DECLARED_INLINE_P (fn)
31174 || processing_template_decl))
31175 set_defining_module (fn);
31177 /* Generate code for it, if necessary. */
31178 expand_or_defer_fn (fn);
31179 /* Restore the saved values. */
31180 parser->in_unbraced_linkage_specification_p
31181 = saved_in_unbraced_linkage_specification_p;
31182 parser->num_template_parameter_lists
31183 = saved_num_template_parameter_lists;
31184 parser->in_function_body = saved_in_function_body;
31186 parser->fully_implicit_function_template_p
31187 = fully_implicit_function_template_p;
31188 parser->implicit_template_parms
31189 = implicit_template_parms;
31190 parser->implicit_template_scope
31191 = implicit_template_scope;
31193 if (parser->fully_implicit_function_template_p)
31194 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
31196 return fn;
31199 /* Parse a template-declaration body (following argument list). */
31201 static void
31202 cp_parser_template_declaration_after_parameters (cp_parser* parser,
31203 tree parameter_list,
31204 bool member_p)
31206 tree decl = NULL_TREE;
31207 bool friend_p = false;
31209 /* We just processed one more parameter list. */
31210 ++parser->num_template_parameter_lists;
31212 /* Get the deferred access checks from the parameter list. These
31213 will be checked once we know what is being declared, as for a
31214 member template the checks must be performed in the scope of the
31215 class containing the member. */
31216 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
31218 /* Tentatively parse for a new template parameter list, which can either be
31219 the template keyword or a template introduction. */
31220 if (cp_parser_template_declaration_after_export (parser, member_p))
31221 /* OK */;
31222 else if (cxx_dialect >= cxx11
31223 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
31224 decl = cp_parser_alias_declaration (parser);
31225 else if (cxx_dialect >= cxx20 /* Implies flag_concept. */
31226 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
31227 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
31228 /* Allow 'concept bool' to be handled as per the TS. */
31229 decl = cp_parser_concept_definition (parser);
31230 else
31232 cp_token *token = cp_lexer_peek_token (parser->lexer);
31233 decl = cp_parser_single_declaration (parser,
31234 checks,
31235 member_p,
31236 /*explicit_specialization_p=*/false,
31237 &friend_p);
31239 /* If this is a member template declaration, let the front
31240 end know. */
31241 if (member_p && !friend_p && decl)
31243 if (TREE_CODE (decl) == TYPE_DECL)
31244 cp_parser_check_access_in_redeclaration (decl, token->location);
31246 decl = finish_member_template_decl (decl);
31248 else if (friend_p && decl
31249 && DECL_DECLARES_TYPE_P (decl))
31250 make_friend_class (current_class_type, TREE_TYPE (decl),
31251 /*complain=*/true);
31253 /* We are done with the current parameter list. */
31254 --parser->num_template_parameter_lists;
31256 pop_deferring_access_checks ();
31258 /* Finish up. */
31259 finish_template_decl (parameter_list);
31261 /* Check the template arguments for a literal operator template. */
31262 if (decl
31263 && DECL_DECLARES_FUNCTION_P (decl)
31264 && UDLIT_OPER_P (DECL_NAME (decl)))
31266 bool ok = true;
31267 if (parameter_list == NULL_TREE)
31268 ok = false;
31269 else
31271 int num_parms = TREE_VEC_LENGTH (parameter_list);
31272 if (num_parms == 1)
31274 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
31275 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31276 if (TREE_CODE (parm) != PARM_DECL)
31277 ok = false;
31278 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
31279 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31280 /* OK, C++20 string literal operator template. We don't need
31281 to warn in lower dialects here because we will have already
31282 warned about the template parameter. */;
31283 else if (TREE_TYPE (parm) != char_type_node
31284 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31285 ok = false;
31287 else if (num_parms == 2 && cxx_dialect >= cxx14)
31289 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
31290 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
31291 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
31292 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31293 if (TREE_CODE (parm) != PARM_DECL
31294 || TREE_TYPE (parm) != TREE_TYPE (type)
31295 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31296 ok = false;
31297 else
31298 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
31299 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
31300 "ISO C++ did not adopt string literal operator templa"
31301 "tes taking an argument pack of characters");
31303 else
31304 ok = false;
31306 if (!ok)
31308 if (cxx_dialect > cxx17)
31309 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31310 "template %qD has invalid parameter list; expected "
31311 "non-type template parameter pack %<<char...>%> or "
31312 "single non-type parameter of class type",
31313 decl);
31314 else
31315 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31316 "template %qD has invalid parameter list; expected "
31317 "non-type template parameter pack %<<char...>%>",
31318 decl);
31322 /* Register member declarations. */
31323 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
31324 finish_member_declaration (decl);
31325 /* If DECL is a function template, we must return to parse it later.
31326 (Even though there is no definition, there might be default
31327 arguments that need handling.) */
31328 if (member_p && decl
31329 && DECL_DECLARES_FUNCTION_P (decl))
31330 vec_safe_push (unparsed_funs_with_definitions, decl);
31333 /* Parse a template introduction header for a template-declaration. Returns
31334 false if tentative parse fails. */
31336 static bool
31337 cp_parser_template_introduction (cp_parser* parser, bool member_p)
31339 cp_parser_parse_tentatively (parser);
31341 tree saved_scope = parser->scope;
31342 tree saved_object_scope = parser->object_scope;
31343 tree saved_qualifying_scope = parser->qualifying_scope;
31345 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
31347 /* Look for the optional `::' operator. */
31348 cp_parser_global_scope_opt (parser,
31349 /*current_scope_valid_p=*/false);
31350 /* Look for the nested-name-specifier. */
31351 cp_parser_nested_name_specifier_opt (parser,
31352 /*typename_keyword_p=*/false,
31353 /*check_dependency_p=*/true,
31354 /*type_p=*/false,
31355 /*is_declaration=*/false);
31357 cp_token *token = cp_lexer_peek_token (parser->lexer);
31358 tree concept_name = cp_parser_identifier (parser);
31360 /* Look up the concept for which we will be matching
31361 template parameters. */
31362 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
31363 token->location);
31364 parser->scope = saved_scope;
31365 parser->object_scope = saved_object_scope;
31366 parser->qualifying_scope = saved_qualifying_scope;
31368 if (concept_name == error_mark_node
31369 || (seen_error () && !concept_definition_p (tmpl_decl)))
31370 cp_parser_simulate_error (parser);
31372 /* Look for opening brace for introduction. */
31373 matching_braces braces;
31374 braces.require_open (parser);
31375 location_t open_loc = input_location;
31377 if (!cp_parser_parse_definitely (parser))
31378 return false;
31380 push_deferring_access_checks (dk_deferred);
31382 /* Build vector of placeholder parameters and grab
31383 matching identifiers. */
31384 tree introduction_list = cp_parser_introduction_list (parser);
31386 /* Look for closing brace for introduction. */
31387 if (!braces.require_close (parser))
31388 return true;
31390 /* The introduction-list shall not be empty. */
31391 int nargs = TREE_VEC_LENGTH (introduction_list);
31392 if (nargs == 0)
31394 /* In cp_parser_introduction_list we have already issued an error. */
31395 return true;
31398 if (tmpl_decl == error_mark_node)
31400 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
31401 token->location);
31402 return true;
31405 /* Build and associate the constraint. */
31406 location_t introduction_loc = make_location (open_loc,
31407 start_token->location,
31408 parser->lexer);
31409 tree parms = finish_template_introduction (tmpl_decl,
31410 introduction_list,
31411 introduction_loc);
31412 if (parms && parms != error_mark_node)
31414 if (!flag_concepts_ts)
31415 pedwarn (introduction_loc, 0, "template-introductions"
31416 " are not part of C++20 concepts; use %qs to enable",
31417 "-fconcepts-ts");
31419 cp_parser_template_declaration_after_parameters (parser, parms,
31420 member_p);
31421 return true;
31424 if (parms == NULL_TREE)
31425 error_at (token->location, "no matching concept for template-introduction");
31427 return true;
31430 /* Parse a normal template-declaration following the template keyword. */
31432 static void
31433 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
31435 tree parameter_list;
31436 bool need_lang_pop;
31437 location_t location = input_location;
31439 /* Look for the `<' token. */
31440 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
31441 return;
31442 if (at_class_scope_p () && current_function_decl)
31444 /* 14.5.2.2 [temp.mem]
31446 A local class shall not have member templates. */
31447 error_at (location,
31448 "invalid declaration of member template in local class");
31449 cp_parser_skip_to_end_of_block_or_statement (parser);
31450 return;
31452 /* [temp]
31454 A template ... shall not have C linkage. */
31455 if (current_lang_name == lang_name_c)
31457 error_at (location, "template with C linkage");
31458 maybe_show_extern_c_location ();
31459 /* Give it C++ linkage to avoid confusing other parts of the
31460 front end. */
31461 push_lang_context (lang_name_cplusplus);
31462 need_lang_pop = true;
31464 else
31465 need_lang_pop = false;
31467 /* We cannot perform access checks on the template parameter
31468 declarations until we know what is being declared, just as we
31469 cannot check the decl-specifier list. */
31470 push_deferring_access_checks (dk_deferred);
31472 /* If the next token is `>', then we have an invalid
31473 specialization. Rather than complain about an invalid template
31474 parameter, issue an error message here. */
31475 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
31477 cp_parser_error (parser, "invalid explicit specialization");
31478 begin_specialization ();
31479 parameter_list = NULL_TREE;
31481 else
31483 /* Parse the template parameters. */
31484 parameter_list = cp_parser_template_parameter_list (parser);
31487 /* Look for the `>'. */
31488 cp_parser_skip_to_end_of_template_parameter_list (parser);
31490 /* Manage template requirements */
31491 if (flag_concepts)
31493 tree reqs = get_shorthand_constraints (current_template_parms);
31494 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
31495 reqs = combine_constraint_expressions (reqs, treqs);
31496 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
31499 cp_parser_template_declaration_after_parameters (parser, parameter_list,
31500 member_p);
31502 /* For the erroneous case of a template with C linkage, we pushed an
31503 implicit C++ linkage scope; exit that scope now. */
31504 if (need_lang_pop)
31505 pop_lang_context ();
31508 /* Parse a template-declaration, assuming that the `export' (and
31509 `extern') keywords, if present, has already been scanned. MEMBER_P
31510 is as for cp_parser_template_declaration. */
31512 static bool
31513 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
31515 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31517 cp_lexer_consume_token (parser->lexer);
31518 cp_parser_explicit_template_declaration (parser, member_p);
31519 return true;
31521 else if (flag_concepts)
31522 return cp_parser_template_introduction (parser, member_p);
31524 return false;
31527 /* Perform the deferred access checks from a template-parameter-list.
31528 CHECKS is a TREE_LIST of access checks, as returned by
31529 get_deferred_access_checks. */
31531 static void
31532 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
31534 ++processing_template_parmlist;
31535 perform_access_checks (checks, tf_warning_or_error);
31536 --processing_template_parmlist;
31539 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
31540 `function-definition' sequence that follows a template header.
31541 If MEMBER_P is true, this declaration appears in a class scope.
31543 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
31544 *FRIEND_P is set to TRUE iff the declaration is a friend. */
31546 static tree
31547 cp_parser_single_declaration (cp_parser* parser,
31548 vec<deferred_access_check, va_gc> *checks,
31549 bool member_p,
31550 bool explicit_specialization_p,
31551 bool* friend_p)
31553 int declares_class_or_enum;
31554 tree decl = NULL_TREE;
31555 cp_decl_specifier_seq decl_specifiers;
31556 bool function_definition_p = false;
31557 cp_token *decl_spec_token_start;
31559 /* This function is only used when processing a template
31560 declaration. */
31561 gcc_assert (innermost_scope_kind () == sk_template_parms
31562 || innermost_scope_kind () == sk_template_spec);
31564 /* Defer access checks until we know what is being declared. */
31565 push_deferring_access_checks (dk_deferred);
31567 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
31568 alternative. */
31569 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
31570 cp_parser_decl_specifier_seq (parser,
31571 (CP_PARSER_FLAGS_OPTIONAL
31572 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
31573 &decl_specifiers,
31574 &declares_class_or_enum);
31576 cp_omp_declare_simd_data odsd;
31577 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
31578 cp_parser_handle_directive_omp_attributes (parser,
31579 &decl_specifiers.attributes,
31580 &odsd, true);
31582 if (friend_p)
31583 *friend_p = cp_parser_friend_p (&decl_specifiers);
31585 /* There are no template typedefs. */
31586 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
31588 error_at (decl_spec_token_start->location,
31589 "template declaration of %<typedef%>");
31590 decl = error_mark_node;
31593 /* Gather up the access checks that occurred the
31594 decl-specifier-seq. */
31595 stop_deferring_access_checks ();
31597 /* Check for the declaration of a template class. */
31598 if (declares_class_or_enum)
31600 if (cp_parser_declares_only_class_p (parser)
31601 || (declares_class_or_enum & 2))
31603 /* If this is a declaration, but not a definition, associate
31604 any constraints with the type declaration. Constraints
31605 are associated with definitions in cp_parser_class_specifier. */
31606 if (declares_class_or_enum == 1)
31607 associate_classtype_constraints (decl_specifiers.type);
31609 decl = shadow_tag (&decl_specifiers);
31611 /* In this case:
31613 struct C {
31614 friend template <typename T> struct A<T>::B;
31617 A<T>::B will be represented by a TYPENAME_TYPE, and
31618 therefore not recognized by shadow_tag. */
31619 if (friend_p && *friend_p
31620 && !decl
31621 && decl_specifiers.type
31622 && TYPE_P (decl_specifiers.type))
31623 decl = decl_specifiers.type;
31625 if (decl && decl != error_mark_node)
31626 decl = TYPE_NAME (decl);
31627 else
31628 decl = error_mark_node;
31630 /* Perform access checks for template parameters. */
31631 cp_parser_perform_template_parameter_access_checks (checks);
31633 /* Give a helpful diagnostic for
31634 template <class T> struct A { } a;
31635 if we aren't already recovering from an error. */
31636 if (!cp_parser_declares_only_class_p (parser)
31637 && !seen_error ())
31639 error_at (cp_lexer_peek_token (parser->lexer)->location,
31640 "a class template declaration must not declare "
31641 "anything else");
31642 cp_parser_skip_to_end_of_block_or_statement (parser);
31643 goto out;
31648 /* Complain about missing 'typename' or other invalid type names. */
31649 if (!decl_specifiers.any_type_specifiers_p
31650 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
31652 /* cp_parser_parse_and_diagnose_invalid_type_name calls
31653 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
31654 the rest of this declaration. */
31655 decl = error_mark_node;
31656 goto out;
31659 /* If it's not a template class, try for a template function. If
31660 the next token is a `;', then this declaration does not declare
31661 anything. But, if there were errors in the decl-specifiers, then
31662 the error might well have come from an attempted class-specifier.
31663 In that case, there's no need to warn about a missing declarator. */
31664 if (!decl
31665 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
31666 || decl_specifiers.type != error_mark_node))
31668 decl = cp_parser_init_declarator (parser,
31669 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
31670 &decl_specifiers,
31671 checks,
31672 /*function_definition_allowed_p=*/true,
31673 member_p,
31674 declares_class_or_enum,
31675 &function_definition_p,
31676 NULL, NULL, NULL);
31678 /* 7.1.1-1 [dcl.stc]
31680 A storage-class-specifier shall not be specified in an explicit
31681 specialization... */
31682 if (decl
31683 && explicit_specialization_p
31684 && decl_specifiers.storage_class != sc_none)
31686 error_at (decl_spec_token_start->location,
31687 "explicit template specialization cannot have a storage class");
31688 decl = error_mark_node;
31691 if (decl && VAR_P (decl))
31692 check_template_variable (decl);
31695 /* Look for a trailing `;' after the declaration. */
31696 if (!function_definition_p
31697 && (decl == error_mark_node
31698 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
31699 cp_parser_skip_to_end_of_block_or_statement (parser);
31701 out:
31702 pop_deferring_access_checks ();
31704 /* Clear any current qualification; whatever comes next is the start
31705 of something new. */
31706 parser->scope = NULL_TREE;
31707 parser->qualifying_scope = NULL_TREE;
31708 parser->object_scope = NULL_TREE;
31710 cp_finalize_omp_declare_simd (parser, &odsd);
31712 return decl;
31715 /* Parse a cast-expression that is not the operand of a unary "&". */
31717 static cp_expr
31718 cp_parser_simple_cast_expression (cp_parser *parser)
31720 return cp_parser_cast_expression (parser, /*address_p=*/false,
31721 /*cast_p=*/false, /*decltype*/false, NULL);
31724 /* Parse a functional cast to TYPE. Returns an expression
31725 representing the cast. */
31727 static cp_expr
31728 cp_parser_functional_cast (cp_parser* parser, tree type)
31730 vec<tree, va_gc> *vec;
31731 tree expression_list;
31732 cp_expr cast;
31733 bool nonconst_p;
31735 location_t start_loc = input_location;
31737 if (!type)
31738 type = error_mark_node;
31740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31742 cp_lexer_set_source_position (parser->lexer);
31743 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
31744 expression_list = cp_parser_braced_list (parser, &nonconst_p);
31745 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
31746 if (TREE_CODE (type) == TYPE_DECL)
31747 type = TREE_TYPE (type);
31749 cast = finish_compound_literal (type, expression_list,
31750 tf_warning_or_error, fcl_functional);
31751 /* Create a location of the form:
31752 type_name{i, f}
31753 ^~~~~~~~~~~~~~~
31754 with caret == start at the start of the type name,
31755 finishing at the closing brace. */
31756 location_t combined_loc = make_location (start_loc, start_loc,
31757 parser->lexer);
31758 cast.set_location (combined_loc);
31759 return cast;
31763 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
31764 /*cast_p=*/true,
31765 /*allow_expansion_p=*/true,
31766 /*non_constant_p=*/NULL);
31767 if (vec == NULL)
31768 expression_list = error_mark_node;
31769 else
31771 expression_list = build_tree_list_vec (vec);
31772 release_tree_vector (vec);
31775 /* Create a location of the form:
31776 float(i)
31777 ^~~~~~~~
31778 with caret == start at the start of the type name,
31779 finishing at the closing paren. */
31780 location_t combined_loc = make_location (start_loc, start_loc,
31781 parser->lexer);
31782 cast = build_functional_cast (combined_loc, type, expression_list,
31783 tf_warning_or_error);
31785 /* [expr.const]/1: In an integral constant expression "only type
31786 conversions to integral or enumeration type can be used". */
31787 if (TREE_CODE (type) == TYPE_DECL)
31788 type = TREE_TYPE (type);
31789 if (cast != error_mark_node
31790 && !cast_valid_in_integral_constant_expression_p (type)
31791 && cp_parser_non_integral_constant_expression (parser,
31792 NIC_CONSTRUCTOR))
31793 return error_mark_node;
31795 return cast;
31798 /* Save the tokens that make up the body of a member function defined
31799 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
31800 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
31801 specifiers applied to the declaration. Returns the FUNCTION_DECL
31802 for the member function. */
31804 static tree
31805 cp_parser_save_member_function_body (cp_parser* parser,
31806 cp_decl_specifier_seq *decl_specifiers,
31807 cp_declarator *declarator,
31808 tree attributes)
31810 cp_token *first;
31811 cp_token *last;
31812 tree fn;
31813 bool function_try_block = false;
31815 /* Create the FUNCTION_DECL. */
31816 fn = grokmethod (decl_specifiers, declarator, attributes);
31817 cp_finalize_omp_declare_simd (parser, fn);
31818 cp_finalize_oacc_routine (parser, fn, true);
31819 /* If something went badly wrong, bail out now. */
31820 if (fn == error_mark_node)
31822 /* If there's a function-body, skip it. */
31823 if (cp_parser_token_starts_function_definition_p
31824 (cp_lexer_peek_token (parser->lexer)))
31825 cp_parser_skip_to_end_of_block_or_statement (parser);
31826 return error_mark_node;
31829 /* Remember it, if there are default args to post process. */
31830 cp_parser_save_default_args (parser, fn);
31832 /* Save away the tokens that make up the body of the
31833 function. */
31834 first = parser->lexer->next_token;
31836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
31837 cp_lexer_consume_token (parser->lexer);
31838 else if (cp_lexer_next_token_is_keyword (parser->lexer,
31839 RID_TRANSACTION_ATOMIC))
31841 cp_lexer_consume_token (parser->lexer);
31842 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
31843 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
31844 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
31845 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
31846 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
31847 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
31848 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
31850 cp_lexer_consume_token (parser->lexer);
31851 cp_lexer_consume_token (parser->lexer);
31852 cp_lexer_consume_token (parser->lexer);
31853 cp_lexer_consume_token (parser->lexer);
31854 cp_lexer_consume_token (parser->lexer);
31856 else
31857 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
31858 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31860 cp_lexer_consume_token (parser->lexer);
31861 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
31862 break;
31866 /* Handle function try blocks. */
31867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31869 cp_lexer_consume_token (parser->lexer);
31870 function_try_block = true;
31872 /* We can have braced-init-list mem-initializers before the fn body. */
31873 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31875 cp_lexer_consume_token (parser->lexer);
31876 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
31878 /* cache_group will stop after an un-nested { } pair, too. */
31879 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
31880 break;
31882 /* variadic mem-inits have ... after the ')'. */
31883 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31884 cp_lexer_consume_token (parser->lexer);
31887 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
31888 /* Handle function try blocks. */
31889 if (function_try_block)
31890 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
31891 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
31892 last = parser->lexer->next_token;
31894 /* Save away the inline definition; we will process it when the
31895 class is complete. */
31896 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
31897 DECL_PENDING_INLINE_P (fn) = 1;
31899 /* We need to know that this was defined in the class, so that
31900 friend templates are handled correctly. */
31901 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
31903 /* Add FN to the queue of functions to be parsed later. */
31904 vec_safe_push (unparsed_funs_with_definitions, fn);
31906 return fn;
31909 /* Save the tokens that make up the in-class initializer for a non-static
31910 data member. Returns a DEFERRED_PARSE. */
31912 static tree
31913 cp_parser_save_nsdmi (cp_parser* parser)
31915 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
31918 /* Parse a template-argument-list, as well as the trailing ">" (but
31919 not the opening "<"). See cp_parser_template_argument_list for the
31920 return value. */
31922 static tree
31923 cp_parser_enclosed_template_argument_list (cp_parser* parser)
31925 tree arguments;
31926 tree saved_scope;
31927 tree saved_qualifying_scope;
31928 tree saved_object_scope;
31929 bool saved_greater_than_is_operator_p;
31931 /* [temp.names]
31933 When parsing a template-id, the first non-nested `>' is taken as
31934 the end of the template-argument-list rather than a greater-than
31935 operator. */
31936 saved_greater_than_is_operator_p
31937 = parser->greater_than_is_operator_p;
31938 parser->greater_than_is_operator_p = false;
31939 /* Parsing the argument list may modify SCOPE, so we save it
31940 here. */
31941 saved_scope = parser->scope;
31942 saved_qualifying_scope = parser->qualifying_scope;
31943 saved_object_scope = parser->object_scope;
31944 /* We need to evaluate the template arguments, even though this
31945 template-id may be nested within a "sizeof". */
31946 cp_evaluated ev;
31947 /* Parse the template-argument-list itself. */
31948 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
31949 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
31950 arguments = NULL_TREE;
31951 else
31952 arguments = cp_parser_template_argument_list (parser);
31953 /* Look for the `>' that ends the template-argument-list. If we find
31954 a '>>' instead, it's probably just a typo. */
31955 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
31957 if (cxx_dialect != cxx98)
31959 /* In C++0x, a `>>' in a template argument list or cast
31960 expression is considered to be two separate `>'
31961 tokens. So, change the current token to a `>', but don't
31962 consume it: it will be consumed later when the outer
31963 template argument list (or cast expression) is parsed.
31964 Note that this replacement of `>' for `>>' is necessary
31965 even if we are parsing tentatively: in the tentative
31966 case, after calling
31967 cp_parser_enclosed_template_argument_list we will always
31968 throw away all of the template arguments and the first
31969 closing `>', either because the template argument list
31970 was erroneous or because we are replacing those tokens
31971 with a CPP_TEMPLATE_ID token. The second `>' (which will
31972 not have been thrown away) is needed either to close an
31973 outer template argument list or to complete a new-style
31974 cast. */
31975 cp_token *token = cp_lexer_peek_token (parser->lexer);
31976 token->type = CPP_GREATER;
31978 else if (!saved_greater_than_is_operator_p)
31980 /* If we're in a nested template argument list, the '>>' has
31981 to be a typo for '> >'. We emit the error message, but we
31982 continue parsing and we push a '>' as next token, so that
31983 the argument list will be parsed correctly. Note that the
31984 global source location is still on the token before the
31985 '>>', so we need to say explicitly where we want it. */
31986 cp_token *token = cp_lexer_peek_token (parser->lexer);
31987 gcc_rich_location richloc (token->location);
31988 richloc.add_fixit_replace ("> >");
31989 error_at (&richloc, "%<>>%> should be %<> >%> "
31990 "within a nested template argument list");
31992 token->type = CPP_GREATER;
31994 else
31996 /* If this is not a nested template argument list, the '>>'
31997 is a typo for '>'. Emit an error message and continue.
31998 Same deal about the token location, but here we can get it
31999 right by consuming the '>>' before issuing the diagnostic. */
32000 cp_token *token = cp_lexer_consume_token (parser->lexer);
32001 error_at (token->location,
32002 "spurious %<>>%>, use %<>%> to terminate "
32003 "a template argument list");
32006 else
32007 cp_parser_skip_to_end_of_template_parameter_list (parser);
32008 /* The `>' token might be a greater-than operator again now. */
32009 parser->greater_than_is_operator_p
32010 = saved_greater_than_is_operator_p;
32011 /* Restore the SAVED_SCOPE. */
32012 parser->scope = saved_scope;
32013 parser->qualifying_scope = saved_qualifying_scope;
32014 parser->object_scope = saved_object_scope;
32016 return arguments;
32019 /* MEMBER_FUNCTION is a member function, or a friend. If default
32020 arguments, or the body of the function have not yet been parsed,
32021 parse them now. */
32023 static void
32024 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
32026 timevar_push (TV_PARSE_INMETH);
32027 /* If this member is a template, get the underlying
32028 FUNCTION_DECL. */
32029 if (DECL_FUNCTION_TEMPLATE_P (member_function))
32030 member_function = DECL_TEMPLATE_RESULT (member_function);
32032 /* There should not be any class definitions in progress at this
32033 point; the bodies of members are only parsed outside of all class
32034 definitions. */
32035 gcc_assert (parser->num_classes_being_defined == 0);
32036 /* While we're parsing the member functions we might encounter more
32037 classes. We want to handle them right away, but we don't want
32038 them getting mixed up with functions that are currently in the
32039 queue. */
32040 push_unparsed_function_queues (parser);
32042 /* Make sure that any template parameters are in scope. */
32043 maybe_begin_member_template_processing (member_function);
32045 /* If the body of the function has not yet been parsed, parse it
32046 now. */
32047 if (DECL_PENDING_INLINE_P (member_function))
32049 tree function_scope;
32050 cp_token_cache *tokens;
32052 /* The function is no longer pending; we are processing it. */
32053 tokens = DECL_PENDING_INLINE_INFO (member_function);
32054 DECL_PENDING_INLINE_INFO (member_function) = NULL;
32055 DECL_PENDING_INLINE_P (member_function) = 0;
32057 /* If this is a local class, enter the scope of the containing
32058 function. */
32059 function_scope = current_function_decl;
32060 if (function_scope)
32061 push_function_context ();
32063 /* Push the body of the function onto the lexer stack. */
32064 cp_parser_push_lexer_for_tokens (parser, tokens);
32066 /* Let the front end know that we going to be defining this
32067 function. */
32068 start_preparsed_function (member_function, NULL_TREE,
32069 SF_PRE_PARSED | SF_INCLASS_INLINE);
32071 /* #pragma omp declare reduction needs special parsing. */
32072 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
32074 parser->lexer->in_pragma = true;
32075 cp_parser_omp_declare_reduction_exprs (member_function, parser);
32076 finish_function (/*inline_p=*/true);
32077 cp_check_omp_declare_reduction (member_function);
32079 else
32080 /* Now, parse the body of the function. */
32081 cp_parser_function_definition_after_declarator (parser,
32082 /*inline_p=*/true);
32084 /* Leave the scope of the containing function. */
32085 if (function_scope)
32086 pop_function_context ();
32087 cp_parser_pop_lexer (parser);
32090 /* Remove any template parameters from the symbol table. */
32091 maybe_end_member_template_processing ();
32093 /* Restore the queue. */
32094 pop_unparsed_function_queues (parser);
32095 timevar_pop (TV_PARSE_INMETH);
32098 /* If DECL contains any default args, remember it on the unparsed
32099 functions queue. */
32101 static void
32102 cp_parser_save_default_args (cp_parser* parser, tree decl)
32104 tree probe;
32106 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
32107 probe;
32108 probe = TREE_CHAIN (probe))
32109 if (TREE_PURPOSE (probe))
32111 cp_default_arg_entry entry = {current_class_type, decl};
32112 vec_safe_push (unparsed_funs_with_default_args, entry);
32113 break;
32116 /* Remember if there is a noexcept-specifier to post process. */
32117 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
32118 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
32119 vec_safe_push (unparsed_noexcepts, decl);
32122 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
32123 which is either a FIELD_DECL or PARM_DECL. Parse it and return
32124 the result. For a PARM_DECL, PARMTYPE is the corresponding type
32125 from the parameter-type-list. */
32127 static tree
32128 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
32129 tree default_arg, tree parmtype)
32131 cp_token_cache *tokens;
32132 tree parsed_arg;
32133 bool dummy;
32135 if (default_arg == error_mark_node)
32136 return error_mark_node;
32138 /* Push the saved tokens for the default argument onto the parser's
32139 lexer stack. */
32140 tokens = DEFPARSE_TOKENS (default_arg);
32141 cp_parser_push_lexer_for_tokens (parser, tokens);
32143 start_lambda_scope (decl);
32145 /* Parse the default argument. */
32146 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
32147 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
32148 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32150 finish_lambda_scope ();
32152 if (parsed_arg == error_mark_node)
32153 cp_parser_skip_to_end_of_statement (parser);
32155 if (!processing_template_decl)
32157 /* In a non-template class, check conversions now. In a template,
32158 we'll wait and instantiate these as needed. */
32159 if (TREE_CODE (decl) == PARM_DECL)
32160 parsed_arg = check_default_argument (parmtype, parsed_arg,
32161 tf_warning_or_error);
32162 else if (maybe_reject_flexarray_init (decl, parsed_arg))
32163 parsed_arg = error_mark_node;
32164 else
32165 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
32168 /* If the token stream has not been completely used up, then
32169 there was extra junk after the end of the default
32170 argument. */
32171 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
32173 if (TREE_CODE (decl) == PARM_DECL)
32174 cp_parser_error (parser, "expected %<,%>");
32175 else
32176 cp_parser_error (parser, "expected %<;%>");
32179 /* Revert to the main lexer. */
32180 cp_parser_pop_lexer (parser);
32182 return parsed_arg;
32185 /* FIELD is a non-static data member with an initializer which we saved for
32186 later; parse it now. */
32188 static void
32189 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
32191 tree def;
32193 maybe_begin_member_template_processing (field);
32195 push_unparsed_function_queues (parser);
32196 def = cp_parser_late_parse_one_default_arg (parser, field,
32197 DECL_INITIAL (field),
32198 NULL_TREE);
32199 pop_unparsed_function_queues (parser);
32201 maybe_end_member_template_processing ();
32203 DECL_INITIAL (field) = def;
32206 /* FN is a FUNCTION_DECL which may contains a parameter with an
32207 unparsed DEFERRED_PARSE. Parse the default args now. This function
32208 assumes that the current scope is the scope in which the default
32209 argument should be processed. */
32211 static void
32212 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
32214 unsigned char saved_local_variables_forbidden_p;
32216 /* While we're parsing the default args, we might (due to the
32217 statement expression extension) encounter more classes. We want
32218 to handle them right away, but we don't want them getting mixed
32219 up with default args that are currently in the queue. */
32220 push_unparsed_function_queues (parser);
32222 /* Local variable names (and the `this' keyword) may not appear
32223 in a default argument. */
32224 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
32225 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
32227 push_defarg_context (fn);
32229 begin_scope (sk_function_parms, fn);
32231 /* Gather the PARM_DECLs into a vec so we can keep track of them when
32232 pushdecl clears DECL_CHAIN. */
32233 releasing_vec parms;
32234 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
32235 parmdecl = DECL_CHAIN (parmdecl))
32236 vec_safe_push (parms, parmdecl);
32238 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
32239 for (int i = 0;
32240 parm && parm != void_list_node;
32241 parm = TREE_CHAIN (parm),
32242 ++i)
32244 tree default_arg = TREE_PURPOSE (parm);
32245 tree parsed_arg;
32247 tree parmdecl = parms[i];
32248 pushdecl (parmdecl);
32250 if (!default_arg)
32251 continue;
32253 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
32254 /* This can happen for a friend declaration for a function
32255 already declared with default arguments. */
32256 continue;
32258 parsed_arg
32259 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
32260 default_arg,
32261 TREE_VALUE (parm));
32262 TREE_PURPOSE (parm) = parsed_arg;
32264 /* Update any instantiations we've already created. */
32265 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
32266 TREE_PURPOSE (copy) = parsed_arg;
32269 pop_bindings_and_leave_scope ();
32271 /* Restore DECL_CHAINs after clobbering by pushdecl. */
32272 parm = NULL_TREE;
32273 for (int i = parms->length () - 1; i >= 0; --i)
32275 DECL_CHAIN (parms[i]) = parm;
32276 parm = parms[i];
32279 pop_defarg_context ();
32281 /* Make sure no default arg is missing. */
32282 check_default_args (fn);
32284 /* Restore the state of local_variables_forbidden_p. */
32285 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
32287 /* Restore the queue. */
32288 pop_unparsed_function_queues (parser);
32291 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
32293 sizeof ... ( identifier )
32295 where the 'sizeof' token has already been consumed. */
32297 static tree
32298 cp_parser_sizeof_pack (cp_parser *parser)
32300 /* Consume the `...'. */
32301 cp_lexer_consume_token (parser->lexer);
32302 maybe_warn_variadic_templates ();
32304 matching_parens parens;
32305 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
32306 if (paren)
32307 parens.consume_open (parser);
32308 else
32309 permerror (cp_lexer_peek_token (parser->lexer)->location,
32310 "%<sizeof...%> argument must be surrounded by parentheses");
32312 cp_token *token = cp_lexer_peek_token (parser->lexer);
32313 tree name = cp_parser_identifier (parser);
32314 if (name == error_mark_node)
32315 return error_mark_node;
32316 /* The name is not qualified. */
32317 parser->scope = NULL_TREE;
32318 parser->qualifying_scope = NULL_TREE;
32319 parser->object_scope = NULL_TREE;
32320 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
32321 if (expr == error_mark_node)
32322 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
32323 token->location);
32324 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
32325 expr = TREE_TYPE (expr);
32326 else if (TREE_CODE (expr) == CONST_DECL)
32327 expr = DECL_INITIAL (expr);
32328 expr = make_pack_expansion (expr);
32329 PACK_EXPANSION_SIZEOF_P (expr) = true;
32331 if (paren)
32332 parens.require_close (parser);
32334 return expr;
32337 /* Parse the operand of `sizeof' (or a similar operator). Returns
32338 either a TYPE or an expression, depending on the form of the
32339 input. The KEYWORD indicates which kind of expression we have
32340 encountered. */
32342 static tree
32343 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
32345 tree expr = NULL_TREE;
32346 const char *saved_message;
32347 const char *saved_message_arg;
32348 bool saved_integral_constant_expression_p;
32349 bool saved_non_integral_constant_expression_p;
32351 /* If it's a `...', then we are computing the length of a parameter
32352 pack. */
32353 if (keyword == RID_SIZEOF
32354 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32355 return cp_parser_sizeof_pack (parser);
32357 /* Types cannot be defined in a `sizeof' expression. Save away the
32358 old message. */
32359 saved_message = parser->type_definition_forbidden_message;
32360 saved_message_arg = parser->type_definition_forbidden_message_arg;
32361 parser->type_definition_forbidden_message
32362 = G_("types may not be defined in %qs expressions");
32363 parser->type_definition_forbidden_message_arg
32364 = IDENTIFIER_POINTER (ridpointers[keyword]);
32366 /* The restrictions on constant-expressions do not apply inside
32367 sizeof expressions. */
32368 saved_integral_constant_expression_p
32369 = parser->integral_constant_expression_p;
32370 saved_non_integral_constant_expression_p
32371 = parser->non_integral_constant_expression_p;
32372 parser->integral_constant_expression_p = false;
32374 /* Do not actually evaluate the expression. */
32375 ++cp_unevaluated_operand;
32376 ++c_inhibit_evaluation_warnings;
32377 /* If it's a `(', then we might be looking at the type-id
32378 construction. */
32379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32381 tree type = NULL_TREE;
32383 tentative_firewall firewall (parser);
32385 /* We can't be sure yet whether we're looking at a type-id or an
32386 expression. */
32387 cp_parser_parse_tentatively (parser);
32389 matching_parens parens;
32390 parens.consume_open (parser);
32392 /* Note: as a GNU Extension, compound literals are considered
32393 postfix-expressions as they are in C99, so they are valid
32394 arguments to sizeof. See comment in cp_parser_cast_expression
32395 for details. */
32396 if (cp_parser_compound_literal_p (parser))
32397 cp_parser_simulate_error (parser);
32398 else
32400 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
32401 parser->in_type_id_in_expr_p = true;
32402 /* Look for the type-id. */
32403 type = cp_parser_type_id (parser);
32404 /* Look for the closing `)'. */
32405 parens.require_close (parser);
32406 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
32409 /* If all went well, then we're done. */
32410 if (cp_parser_parse_definitely (parser))
32411 expr = type;
32412 else
32414 /* Commit to the tentative_firewall so we get syntax errors. */
32415 cp_parser_commit_to_tentative_parse (parser);
32417 expr = cp_parser_unary_expression (parser);
32420 else
32421 expr = cp_parser_unary_expression (parser);
32423 /* Go back to evaluating expressions. */
32424 --cp_unevaluated_operand;
32425 --c_inhibit_evaluation_warnings;
32427 /* And restore the old one. */
32428 parser->type_definition_forbidden_message = saved_message;
32429 parser->type_definition_forbidden_message_arg = saved_message_arg;
32430 parser->integral_constant_expression_p
32431 = saved_integral_constant_expression_p;
32432 parser->non_integral_constant_expression_p
32433 = saved_non_integral_constant_expression_p;
32435 return expr;
32438 /* If the current declaration has no declarator, return true. */
32440 static bool
32441 cp_parser_declares_only_class_p (cp_parser *parser)
32443 /* If the next token is a `;' or a `,' then there is no
32444 declarator. */
32445 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32446 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
32449 /* Update the DECL_SPECS to reflect the storage class indicated by
32450 KEYWORD. */
32452 static void
32453 cp_parser_set_storage_class (cp_parser *parser,
32454 cp_decl_specifier_seq *decl_specs,
32455 enum rid keyword,
32456 cp_token *token)
32458 cp_storage_class storage_class;
32460 if (parser->in_unbraced_linkage_specification_p)
32462 error_at (token->location, "invalid use of %qD in linkage specification",
32463 ridpointers[keyword]);
32464 return;
32466 else if (decl_specs->storage_class != sc_none)
32468 decl_specs->conflicting_specifiers_p = true;
32469 return;
32472 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
32473 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
32474 && decl_specs->gnu_thread_keyword_p)
32476 pedwarn (decl_specs->locations[ds_thread], 0,
32477 "%<__thread%> before %qD", ridpointers[keyword]);
32480 switch (keyword)
32482 case RID_AUTO:
32483 storage_class = sc_auto;
32484 break;
32485 case RID_REGISTER:
32486 storage_class = sc_register;
32487 break;
32488 case RID_STATIC:
32489 storage_class = sc_static;
32490 break;
32491 case RID_EXTERN:
32492 storage_class = sc_extern;
32493 break;
32494 case RID_MUTABLE:
32495 storage_class = sc_mutable;
32496 break;
32497 default:
32498 gcc_unreachable ();
32500 decl_specs->storage_class = storage_class;
32501 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
32503 /* A storage class specifier cannot be applied alongside a typedef
32504 specifier. If there is a typedef specifier present then set
32505 conflicting_specifiers_p which will trigger an error later
32506 on in grokdeclarator. */
32507 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
32508 decl_specs->conflicting_specifiers_p = true;
32511 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
32512 is true, the type is a class or enum definition. */
32514 static void
32515 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
32516 tree type_spec,
32517 cp_token *token,
32518 bool type_definition_p)
32520 decl_specs->any_specifiers_p = true;
32522 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
32523 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
32524 this is what happened. In system headers, we ignore these
32525 declarations so that G++ can work with system headers that are not
32526 C++-safe. */
32527 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
32528 && !type_definition_p
32529 && (type_spec == boolean_type_node
32530 || type_spec == char8_type_node
32531 || type_spec == char16_type_node
32532 || type_spec == char32_type_node
32533 || type_spec == wchar_type_node)
32534 && (decl_specs->type
32535 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
32536 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
32537 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
32538 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
32540 decl_specs->redefined_builtin_type = type_spec;
32541 set_and_check_decl_spec_loc (decl_specs,
32542 ds_redefined_builtin_type_spec,
32543 token);
32544 if (!decl_specs->type)
32546 decl_specs->type = type_spec;
32547 decl_specs->type_definition_p = false;
32548 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
32551 else if (decl_specs->type)
32552 decl_specs->multiple_types_p = true;
32553 else
32555 decl_specs->type = type_spec;
32556 decl_specs->type_definition_p = type_definition_p;
32557 decl_specs->redefined_builtin_type = NULL_TREE;
32558 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
32562 /* True iff TOKEN is the GNU keyword __thread. */
32564 static bool
32565 token_is__thread (cp_token *token)
32567 gcc_assert (token->keyword == RID_THREAD);
32568 return id_equal (token->u.value, "__thread");
32571 /* Set the location for a declarator specifier and check if it is
32572 duplicated.
32574 DECL_SPECS is the sequence of declarator specifiers onto which to
32575 set the location.
32577 DS is the single declarator specifier to set which location is to
32578 be set onto the existing sequence of declarators.
32580 LOCATION is the location for the declarator specifier to
32581 consider. */
32583 static void
32584 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
32585 cp_decl_spec ds, cp_token *token)
32587 gcc_assert (ds < ds_last);
32589 if (decl_specs == NULL)
32590 return;
32592 location_t location = token->location;
32594 if (decl_specs->locations[ds] == 0)
32596 decl_specs->locations[ds] = location;
32597 if (ds == ds_thread)
32598 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
32600 else
32602 if (ds == ds_long)
32604 if (decl_specs->locations[ds_long_long] != 0)
32605 error_at (location,
32606 "%<long long long%> is too long for GCC");
32607 else
32609 decl_specs->locations[ds_long_long] = location;
32610 pedwarn_cxx98 (location,
32611 OPT_Wlong_long,
32612 "ISO C++ 1998 does not support %<long long%>");
32615 else if (ds == ds_thread)
32617 bool gnu = token_is__thread (token);
32618 gcc_rich_location richloc (location);
32619 if (gnu != decl_specs->gnu_thread_keyword_p)
32621 richloc.add_range (decl_specs->locations[ds_thread]);
32622 error_at (&richloc,
32623 "both %<__thread%> and %<thread_local%> specified");
32625 else
32627 richloc.add_fixit_remove ();
32628 error_at (&richloc, "duplicate %qD", token->u.value);
32631 else
32633 static const char *const decl_spec_names[] = {
32634 "signed",
32635 "unsigned",
32636 "short",
32637 "long",
32638 "const",
32639 "volatile",
32640 "restrict",
32641 "inline",
32642 "virtual",
32643 "explicit",
32644 "friend",
32645 "typedef",
32646 "using",
32647 "constexpr",
32648 "__complex",
32649 "constinit",
32650 "consteval"
32652 gcc_rich_location richloc (location);
32653 richloc.add_fixit_remove ();
32654 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
32659 /* Return true iff the declarator specifier DS is present in the
32660 sequence of declarator specifiers DECL_SPECS. */
32662 bool
32663 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
32664 cp_decl_spec ds)
32666 gcc_assert (ds < ds_last);
32668 if (decl_specs == NULL)
32669 return false;
32671 return decl_specs->locations[ds] != 0;
32674 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
32675 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
32677 static bool
32678 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
32680 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
32683 /* Issue an error message indicating that TOKEN_DESC was expected.
32684 If KEYWORD is true, it indicated this function is called by
32685 cp_parser_require_keword and the required token can only be
32686 a indicated keyword.
32688 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32689 within any error as the location of an "opening" token matching
32690 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32691 RT_CLOSE_PAREN). */
32693 static void
32694 cp_parser_required_error (cp_parser *parser,
32695 required_token token_desc,
32696 bool keyword,
32697 location_t matching_location)
32699 if (cp_parser_simulate_error (parser))
32700 return;
32702 const char *gmsgid = NULL;
32703 switch (token_desc)
32705 case RT_NEW:
32706 gmsgid = G_("expected %<new%>");
32707 break;
32708 case RT_DELETE:
32709 gmsgid = G_("expected %<delete%>");
32710 break;
32711 case RT_RETURN:
32712 gmsgid = G_("expected %<return%>");
32713 break;
32714 case RT_WHILE:
32715 gmsgid = G_("expected %<while%>");
32716 break;
32717 case RT_EXTERN:
32718 gmsgid = G_("expected %<extern%>");
32719 break;
32720 case RT_STATIC_ASSERT:
32721 gmsgid = G_("expected %<static_assert%>");
32722 break;
32723 case RT_DECLTYPE:
32724 gmsgid = G_("expected %<decltype%>");
32725 break;
32726 case RT_OPERATOR:
32727 gmsgid = G_("expected %<operator%>");
32728 break;
32729 case RT_CLASS:
32730 gmsgid = G_("expected %<class%>");
32731 break;
32732 case RT_TEMPLATE:
32733 gmsgid = G_("expected %<template%>");
32734 break;
32735 case RT_NAMESPACE:
32736 gmsgid = G_("expected %<namespace%>");
32737 break;
32738 case RT_USING:
32739 gmsgid = G_("expected %<using%>");
32740 break;
32741 case RT_ASM:
32742 gmsgid = G_("expected %<asm%>");
32743 break;
32744 case RT_TRY:
32745 gmsgid = G_("expected %<try%>");
32746 break;
32747 case RT_CATCH:
32748 gmsgid = G_("expected %<catch%>");
32749 break;
32750 case RT_THROW:
32751 gmsgid = G_("expected %<throw%>");
32752 break;
32753 case RT_AUTO:
32754 gmsgid = G_("expected %<auto%>");
32755 break;
32756 case RT_LABEL:
32757 gmsgid = G_("expected %<__label__%>");
32758 break;
32759 case RT_AT_TRY:
32760 gmsgid = G_("expected %<@try%>");
32761 break;
32762 case RT_AT_SYNCHRONIZED:
32763 gmsgid = G_("expected %<@synchronized%>");
32764 break;
32765 case RT_AT_THROW:
32766 gmsgid = G_("expected %<@throw%>");
32767 break;
32768 case RT_TRANSACTION_ATOMIC:
32769 gmsgid = G_("expected %<__transaction_atomic%>");
32770 break;
32771 case RT_TRANSACTION_RELAXED:
32772 gmsgid = G_("expected %<__transaction_relaxed%>");
32773 break;
32774 case RT_CO_YIELD:
32775 gmsgid = G_("expected %<co_yield%>");
32776 break;
32777 default:
32778 break;
32781 if (!gmsgid && !keyword)
32783 switch (token_desc)
32785 case RT_SEMICOLON:
32786 gmsgid = G_("expected %<;%>");
32787 break;
32788 case RT_OPEN_PAREN:
32789 gmsgid = G_("expected %<(%>");
32790 break;
32791 case RT_CLOSE_BRACE:
32792 gmsgid = G_("expected %<}%>");
32793 break;
32794 case RT_OPEN_BRACE:
32795 gmsgid = G_("expected %<{%>");
32796 break;
32797 case RT_CLOSE_SQUARE:
32798 gmsgid = G_("expected %<]%>");
32799 break;
32800 case RT_OPEN_SQUARE:
32801 gmsgid = G_("expected %<[%>");
32802 break;
32803 case RT_COMMA:
32804 gmsgid = G_("expected %<,%>");
32805 break;
32806 case RT_SCOPE:
32807 gmsgid = G_("expected %<::%>");
32808 break;
32809 case RT_LESS:
32810 gmsgid = G_("expected %<<%>");
32811 break;
32812 case RT_GREATER:
32813 gmsgid = G_("expected %<>%>");
32814 break;
32815 case RT_EQ:
32816 gmsgid = G_("expected %<=%>");
32817 break;
32818 case RT_ELLIPSIS:
32819 gmsgid = G_("expected %<...%>");
32820 break;
32821 case RT_MULT:
32822 gmsgid = G_("expected %<*%>");
32823 break;
32824 case RT_COMPL:
32825 gmsgid = G_("expected %<~%>");
32826 break;
32827 case RT_COLON:
32828 gmsgid = G_("expected %<:%>");
32829 break;
32830 case RT_COLON_SCOPE:
32831 gmsgid = G_("expected %<:%> or %<::%>");
32832 break;
32833 case RT_CLOSE_PAREN:
32834 gmsgid = G_("expected %<)%>");
32835 break;
32836 case RT_COMMA_CLOSE_PAREN:
32837 gmsgid = G_("expected %<,%> or %<)%>");
32838 break;
32839 case RT_PRAGMA_EOL:
32840 gmsgid = G_("expected end of line");
32841 break;
32842 case RT_NAME:
32843 gmsgid = G_("expected identifier");
32844 break;
32845 case RT_SELECT:
32846 gmsgid = G_("expected selection-statement");
32847 break;
32848 case RT_ITERATION:
32849 gmsgid = G_("expected iteration-statement");
32850 break;
32851 case RT_JUMP:
32852 gmsgid = G_("expected jump-statement");
32853 break;
32854 case RT_CLASS_KEY:
32855 gmsgid = G_("expected class-key");
32856 break;
32857 case RT_CLASS_TYPENAME_TEMPLATE:
32858 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
32859 break;
32860 default:
32861 gcc_unreachable ();
32865 if (gmsgid)
32866 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
32870 /* If the next token is of the indicated TYPE, consume it. Otherwise,
32871 issue an error message indicating that TOKEN_DESC was expected.
32873 Returns the token consumed, if the token had the appropriate type.
32874 Otherwise, returns NULL.
32876 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32877 within any error as the location of an "opening" token matching
32878 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32879 RT_CLOSE_PAREN). */
32881 static cp_token *
32882 cp_parser_require (cp_parser* parser,
32883 enum cpp_ttype type,
32884 required_token token_desc,
32885 location_t matching_location)
32887 if (cp_lexer_next_token_is (parser->lexer, type))
32888 return cp_lexer_consume_token (parser->lexer);
32889 else
32891 /* Output the MESSAGE -- unless we're parsing tentatively. */
32892 if (!cp_parser_simulate_error (parser))
32893 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
32894 matching_location);
32895 return NULL;
32899 /* An error message is produced if the next token is not '>'.
32900 All further tokens are skipped until the desired token is
32901 found or '{', '}', ';' or an unbalanced ')' or ']'. */
32903 static void
32904 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
32906 /* Current level of '< ... >'. */
32907 unsigned level = 0;
32908 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
32909 unsigned nesting_depth = 0;
32911 /* Are we ready, yet? If not, issue error message. */
32912 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
32913 return;
32915 /* Skip tokens until the desired token is found. */
32916 while (true)
32918 /* Peek at the next token. */
32919 switch (cp_lexer_peek_token (parser->lexer)->type)
32921 case CPP_LESS:
32922 if (!nesting_depth)
32923 ++level;
32924 break;
32926 case CPP_RSHIFT:
32927 if (cxx_dialect == cxx98)
32928 /* C++0x views the `>>' operator as two `>' tokens, but
32929 C++98 does not. */
32930 break;
32931 else if (!nesting_depth && level-- == 0)
32933 /* We've hit a `>>' where the first `>' closes the
32934 template argument list, and the second `>' is
32935 spurious. Just consume the `>>' and stop; we've
32936 already produced at least one error. */
32937 cp_lexer_consume_token (parser->lexer);
32938 return;
32940 /* Fall through for C++0x, so we handle the second `>' in
32941 the `>>'. */
32942 gcc_fallthrough ();
32944 case CPP_GREATER:
32945 if (!nesting_depth && level-- == 0)
32947 /* We've reached the token we want, consume it and stop. */
32948 cp_lexer_consume_token (parser->lexer);
32949 return;
32951 break;
32953 case CPP_OPEN_PAREN:
32954 case CPP_OPEN_SQUARE:
32955 ++nesting_depth;
32956 break;
32958 case CPP_CLOSE_PAREN:
32959 case CPP_CLOSE_SQUARE:
32960 if (nesting_depth-- == 0)
32961 return;
32962 break;
32964 case CPP_EOF:
32965 case CPP_PRAGMA_EOL:
32966 case CPP_SEMICOLON:
32967 case CPP_OPEN_BRACE:
32968 case CPP_CLOSE_BRACE:
32969 /* The '>' was probably forgotten, don't look further. */
32970 return;
32972 default:
32973 break;
32976 /* Consume this token. */
32977 cp_lexer_consume_token (parser->lexer);
32981 /* If the next token is the indicated keyword, consume it. Otherwise,
32982 issue an error message indicating that TOKEN_DESC was expected.
32984 Returns the token consumed, if the token had the appropriate type.
32985 Otherwise, returns NULL. */
32987 static cp_token *
32988 cp_parser_require_keyword (cp_parser* parser,
32989 enum rid keyword,
32990 required_token token_desc)
32992 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
32994 if (token && token->keyword != keyword)
32996 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
32997 UNKNOWN_LOCATION);
32998 return NULL;
33001 return token;
33004 /* Returns TRUE iff TOKEN is a token that can begin the body of a
33005 function-definition. */
33007 static bool
33008 cp_parser_token_starts_function_definition_p (cp_token* token)
33010 return (/* An ordinary function-body begins with an `{'. */
33011 token->type == CPP_OPEN_BRACE
33012 /* A ctor-initializer begins with a `:'. */
33013 || token->type == CPP_COLON
33014 /* A function-try-block begins with `try'. */
33015 || token->keyword == RID_TRY
33016 /* A function-transaction-block begins with `__transaction_atomic'
33017 or `__transaction_relaxed'. */
33018 || token->keyword == RID_TRANSACTION_ATOMIC
33019 || token->keyword == RID_TRANSACTION_RELAXED
33020 /* The named return value extension begins with `return'. */
33021 || token->keyword == RID_RETURN);
33024 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
33025 definition. */
33027 static bool
33028 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
33030 cp_token *token;
33032 token = cp_lexer_peek_token (parser->lexer);
33033 return (token->type == CPP_OPEN_BRACE
33034 || (token->type == CPP_COLON
33035 && !parser->colon_doesnt_start_class_def_p));
33038 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
33039 C++0x) ending a template-argument. */
33041 static bool
33042 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
33044 cp_token *token;
33046 token = cp_lexer_peek_token (parser->lexer);
33047 return (token->type == CPP_COMMA
33048 || token->type == CPP_GREATER
33049 || token->type == CPP_ELLIPSIS
33050 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
33053 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
33054 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
33056 static bool
33057 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
33058 size_t n)
33060 cp_token *token;
33062 token = cp_lexer_peek_nth_token (parser->lexer, n);
33063 if (token->type == CPP_LESS)
33064 return true;
33065 /* Check for the sequence `<::' in the original code. It would be lexed as
33066 `[:', where `[' is a digraph, and there is no whitespace before
33067 `:'. */
33068 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
33070 cp_token *token2;
33071 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
33072 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
33073 return true;
33075 return false;
33078 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
33079 or none_type otherwise. */
33081 static enum tag_types
33082 cp_parser_token_is_class_key (cp_token* token)
33084 switch (token->keyword)
33086 case RID_CLASS:
33087 return class_type;
33088 case RID_STRUCT:
33089 return record_type;
33090 case RID_UNION:
33091 return union_type;
33093 default:
33094 return none_type;
33098 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
33099 or none_type otherwise or if the token is null. */
33101 static enum tag_types
33102 cp_parser_token_is_type_parameter_key (cp_token* token)
33104 if (!token)
33105 return none_type;
33107 switch (token->keyword)
33109 case RID_CLASS:
33110 return class_type;
33111 case RID_TYPENAME:
33112 return typename_type;
33114 default:
33115 return none_type;
33119 /* Diagnose redundant enum-keys. */
33121 static void
33122 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
33123 tree type, rid scoped_key)
33125 if (!warn_redundant_tags)
33126 return;
33128 tree type_decl = TYPE_MAIN_DECL (type);
33129 tree name = DECL_NAME (type_decl);
33130 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
33131 push_deferring_access_checks (dk_no_check);
33132 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33133 pop_deferring_access_checks ();
33135 /* The enum-key is redundant for uses of the TYPE that are not
33136 declarations and for which name lookup returns just the type
33137 itself. */
33138 if (decl != type_decl)
33139 return;
33141 if (scoped_key != RID_CLASS
33142 && scoped_key != RID_STRUCT
33143 && current_lang_name != lang_name_cplusplus
33144 && current_namespace == global_namespace)
33146 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
33147 enum tag in shared C/C++ code in files (such as headers) included
33148 in the main source file. */
33149 const line_map_ordinary *map = NULL;
33150 linemap_resolve_location (line_table, key_loc,
33151 LRK_MACRO_DEFINITION_LOCATION,
33152 &map);
33153 if (!MAIN_FILE_P (map))
33154 return;
33157 gcc_rich_location richloc (key_loc);
33158 richloc.add_fixit_remove (key_loc);
33159 warning_at (&richloc, OPT_Wredundant_tags,
33160 "redundant enum-key %<enum%s%> in reference to %q#T",
33161 (scoped_key == RID_CLASS ? " class"
33162 : scoped_key == RID_STRUCT ? " struct" : ""), type);
33165 /* Describes the set of declarations of a struct, class, or class template
33166 or its specializations. Used for -Wmismatched-tags. */
33168 class class_decl_loc_t
33170 public:
33172 class_decl_loc_t ()
33173 : locvec (), idxdef (), def_class_key ()
33175 locvec.create (4);
33178 /* Constructs an object for a single declaration of a class with
33179 CLASS_KEY at the current location in the current function (or
33180 at another scope). KEY_REDUNDANT is true if the class-key may
33181 be omitted in the current context without an ambiguity with
33182 another symbol with the same name.
33183 DEF_P is true for a class declaration that is a definition.
33184 CURLOC is the associated location. */
33185 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
33186 location_t curloc = input_location)
33187 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
33189 locvec.create (4);
33190 class_key_loc_t ckl (current_function_decl, curloc, class_key,
33191 key_redundant);
33192 locvec.quick_push (ckl);
33195 /* Copy, assign, and destroy the object. Necessary because LOCVEC
33196 isn't safely copyable and assignable and doesn't release storage
33197 on its own. */
33198 class_decl_loc_t (const class_decl_loc_t &rhs)
33199 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
33200 def_class_key (rhs.def_class_key)
33203 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
33205 if (this == &rhs)
33206 return *this;
33207 locvec.release ();
33208 locvec = rhs.locvec.copy ();
33209 idxdef = rhs.idxdef;
33210 def_class_key = rhs.def_class_key;
33211 return *this;
33214 ~class_decl_loc_t ()
33216 locvec.release ();
33219 /* Issues -Wmismatched-tags for a single class. */
33220 void diag_mismatched_tags (tree);
33222 /* Issues -Wmismatched-tags for all classes. */
33223 static void diag_mismatched_tags ();
33225 /* Adds TYPE_DECL to the collection of class decls and diagnoses
33226 redundant tags (if -Wredundant-tags is enabled). */
33227 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
33229 /* Either adds this decl to the collection of class decls
33230 or diagnoses it, whichever is appropriate. */
33231 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
33233 private:
33235 tree function (unsigned i) const
33237 return locvec[i].func;
33240 location_t location (unsigned i) const
33242 return locvec[i].loc;
33245 bool key_redundant (unsigned i) const
33247 return locvec[i].key_redundant;
33250 tag_types class_key (unsigned i) const
33252 return locvec[i].class_key;
33255 /* True if a definition for the class has been seen. */
33256 bool def_p () const
33258 return idxdef < locvec.length ();
33261 /* The location of a single mention of a class type with the given
33262 class-key. */
33263 struct class_key_loc_t
33265 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
33266 : func (func), loc (loc), class_key (key), key_redundant (redundant)
33269 /* The function the type is mentioned in. */
33270 tree func;
33271 /* The exact location. */
33272 location_t loc;
33273 /* The class-key used in the mention of the type. */
33274 tag_types class_key;
33275 /* True when the class-key could be omitted at this location
33276 without an ambiguity with another symbol of the same name. */
33277 bool key_redundant;
33279 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
33280 vec <class_key_loc_t> locvec;
33281 /* LOCVEC index of the definition or UINT_MAX if none exists. */
33282 unsigned idxdef;
33283 /* The class-key the class was last declared with or none_type when
33284 it has been declared with a mismatched key. */
33285 tag_types def_class_key;
33287 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
33288 description above. */
33289 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
33290 static class_to_loc_map_t class2loc;
33293 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
33295 /* Issue an error message if the CLASS_KEY does not match the TYPE.
33296 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33297 is set for a declaration of class TYPE and clear for a reference to
33298 it that is not a declaration of it. */
33300 static void
33301 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
33302 tag_types class_key, tree type, bool def_p,
33303 bool decl_p)
33305 if (type == error_mark_node)
33306 return;
33308 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
33309 if (seen_as_union != (class_key == union_type))
33311 if (permerror (input_location, "%qs tag used in naming %q#T",
33312 class_key == union_type ? "union"
33313 : class_key == record_type ? "struct" : "class",
33314 type))
33315 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
33316 "%q#T was previously declared here", type);
33317 return;
33320 if (!warn_mismatched_tags && !warn_redundant_tags)
33321 return;
33323 /* Only consider the true class-keys below and ignore typename_type,
33324 etc. that are not C++ class-keys. */
33325 if (class_key != class_type
33326 && class_key != record_type
33327 && class_key != union_type)
33328 return;
33330 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
33333 /* Returns the template or specialization of one to which the RECORD_TYPE
33334 TYPE corresponds. */
33336 static tree
33337 specialization_of (tree type)
33339 tree ret = type;
33341 /* Determine the template or its partial specialization to which TYPE
33342 corresponds. */
33343 if (tree spec = most_specialized_partial_spec (type, tf_none))
33344 if (spec != error_mark_node)
33345 ret = TREE_TYPE (TREE_VALUE (spec));
33347 if (ret == type)
33348 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
33350 return TYPE_MAIN_DECL (ret);
33354 /* Adds the class TYPE to the collection of class decls and diagnoses
33355 redundant tags (if -Wredundant-tags is enabled).
33356 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33357 is set for a (likely, based on syntactic context) declaration of class
33358 TYPE and clear for a reference to it that is not a declaration of it. */
33360 void
33361 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
33362 tag_types class_key, tree type, bool def_p, bool decl_p)
33364 tree type_decl = TYPE_MAIN_DECL (type);
33365 tree name = DECL_NAME (type_decl);
33366 /* Look up the NAME to see if it unambiguously refers to the TYPE
33367 and set KEY_REDUNDANT if so. */
33368 push_deferring_access_checks (dk_no_check);
33369 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33370 pop_deferring_access_checks ();
33372 /* The class-key is redundant for uses of the CLASS_TYPE that are
33373 neither definitions of it nor declarations, and for which name
33374 lookup returns just the type itself. */
33375 bool key_redundant = (!def_p && !decl_p
33376 && (decl == type_decl
33377 || TREE_CODE (decl) == TEMPLATE_DECL
33378 || TYPE_BEING_DEFINED (type)));
33380 if (key_redundant
33381 && class_key != class_type
33382 && current_lang_name != lang_name_cplusplus
33383 && current_namespace == global_namespace)
33385 /* Avoid issuing the diagnostic for apparently redundant struct
33386 and union class-keys in shared C/C++ code in files (such as
33387 headers) included in the main source file. */
33388 const line_map_ordinary *map = NULL;
33389 linemap_resolve_location (line_table, key_loc,
33390 LRK_MACRO_DEFINITION_LOCATION,
33391 &map);
33392 if (!MAIN_FILE_P (map))
33393 key_redundant = false;
33396 /* Set if a declaration of TYPE has previously been seen or if it must
33397 exist in a precompiled header. */
33398 bool exist;
33399 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
33400 if (!exist)
33402 tree type = TREE_TYPE (type_decl);
33403 if (def_p || !COMPLETE_TYPE_P (type))
33405 /* TYPE_DECL is the first declaration or definition of the type
33406 (outside precompiled headers -- see below). Just create
33407 a new entry for it and return unless it's a declaration
33408 involving a template that may need to be diagnosed by
33409 -Wredundant-tags. */
33410 *rdl = class_decl_loc_t (class_key, false, def_p);
33411 if (TREE_CODE (decl) != TEMPLATE_DECL)
33412 return;
33414 else
33416 /* TYPE was previously defined in some unknown precompiled hdeader.
33417 Simply add a record of its definition at an unknown location and
33418 proceed below to add a reference to it at the current location.
33419 (Declarations in precompiled headers that are not definitions
33420 are ignored.) */
33421 tag_types def_key
33422 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
33423 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
33424 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
33425 exist = true;
33429 /* A prior declaration of TYPE_DECL has been seen. */
33431 if (key_redundant)
33433 gcc_rich_location richloc (key_loc);
33434 richloc.add_fixit_remove (key_loc);
33435 warning_at (&richloc, OPT_Wredundant_tags,
33436 "redundant class-key %qs in reference to %q#T",
33437 class_key == union_type ? "union"
33438 : class_key == record_type ? "struct" : "class",
33439 type);
33442 if (!exist)
33443 /* Do nothing if this is the first declaration of the type. */
33444 return;
33446 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
33447 /* Do nothing if the class-key in this declaration matches
33448 the definition. */
33449 return;
33451 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
33452 def_p);
33455 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
33456 of class decls or diagnoses it, whichever is appropriate. */
33458 void
33459 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
33460 tag_types class_key,
33461 bool redundant,
33462 bool def_p)
33464 /* Reset the CLASS_KEY associated with this type on mismatch.
33465 This is an optimization that lets the diagnostic code skip
33466 over classes that use the same class-key in all declarations. */
33467 if (def_class_key != class_key)
33468 def_class_key = none_type;
33470 /* Set IDXDEF to the index of the vector corresponding to
33471 the definition. */
33472 if (def_p)
33473 idxdef = locvec.length ();
33475 /* Append a record of this declaration to the vector. */
33476 class_key_loc_t ckl (current_function_decl, input_location, class_key,
33477 redundant);
33478 locvec.safe_push (ckl);
33480 if (idxdef == UINT_MAX)
33481 return;
33483 /* As a space optimization diagnose declarations of a class
33484 whose definition has been seen and purge the LOCVEC of
33485 all entries except the definition. */
33486 diag_mismatched_tags (type_decl);
33487 if (idxdef)
33489 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
33490 locvec.release ();
33491 locvec.reserve (2);
33492 locvec.safe_push (ent);
33493 idxdef = 0;
33495 else
33496 /* Pop the entry pushed above for this declaration. */
33497 locvec.pop ();
33500 /* Issues -Wmismatched-tags for a single class. */
33502 void
33503 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
33505 if (!warn_mismatched_tags)
33506 return;
33508 /* Number of uses of the class. */
33509 const unsigned ndecls = locvec.length ();
33511 /* The class (or template) declaration guiding the decisions about
33512 the diagnostic. For ordinary classes it's the same as THIS. For
33513 uses of instantiations of templates other than their declarations
33514 it points to the record for the declaration of the corresponding
33515 primary template or partial specialization. */
33516 class_decl_loc_t *cdlguide = this;
33518 tree type = TREE_TYPE (type_decl);
33519 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type))
33521 /* For implicit instantiations of a primary template look up
33522 the primary or partial specialization and use it as
33523 the expected class-key rather than using the class-key of
33524 the first reference to the instantiation. The primary must
33525 be (and inevitably is) at index zero. */
33526 tree spec = specialization_of (type);
33527 cdlguide = class2loc.get (spec);
33528 gcc_assert (cdlguide != NULL);
33530 else
33532 /* Skip declarations that consistently use the same class-key. */
33533 if (def_class_key != none_type)
33534 return;
33537 /* Set if a definition for the class has been seen. */
33538 const bool def_p = cdlguide->def_p ();
33540 /* The index of the declaration whose class-key this declaration
33541 is expected to match. It's either the class-key of the class
33542 definition if one exists or the first declaration otherwise. */
33543 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
33545 /* The class-key the class is expected to be declared with: it's
33546 either the key used in its definition or the first declaration
33547 if no definition has been provided.
33548 For implicit instantiations of a primary template it's
33549 the class-key used to declare the primary with. The primary
33550 must be at index zero. */
33551 const tag_types xpect_key = cdlguide->class_key (idxguide);
33553 unsigned idx = 0;
33554 /* Advance IDX to the first declaration that either is not
33555 a definition or that doesn't match the first declaration
33556 if no definition is provided. */
33557 while (class_key (idx) == xpect_key)
33558 if (++idx == ndecls)
33559 return;
33561 /* Save the current function before changing it below. */
33562 tree save_func = current_function_decl;
33563 /* Set the function declaration to print in diagnostic context. */
33564 current_function_decl = function (idx);
33566 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
33567 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
33569 location_t loc = location (idx);
33570 bool key_redundant_p = key_redundant (idx);
33571 auto_diagnostic_group d;
33572 /* Issue a warning for the first mismatched declaration.
33573 Avoid using "%#qT" since the class-key for the same type will
33574 be the same regardless of which one was used in the declaraion. */
33575 if (warning_at (loc, OPT_Wmismatched_tags,
33576 "%qT declared with a mismatched class-key %qs",
33577 type_decl, xmatchkstr))
33579 /* Suggest how to avoid the warning for each instance since
33580 the guidance may be different depending on context. */
33581 inform (loc,
33582 (key_redundant_p
33583 ? G_("remove the class-key or replace it with %qs")
33584 : G_("replace the class-key with %qs")),
33585 xpectkstr);
33587 /* Also point to the first declaration or definition that guided
33588 the decision to issue the warning above. */
33589 inform (cdlguide->location (idxguide),
33590 (def_p
33591 ? G_("%qT defined as %qs here")
33592 : G_("%qT first declared as %qs here")),
33593 type_decl, xpectkstr);
33596 /* Issue warnings for the remaining inconsistent declarations. */
33597 for (unsigned i = idx + 1; i != ndecls; ++i)
33599 tag_types clskey = class_key (i);
33600 /* Skip over the declarations that match either the definition
33601 if one was provided or the first declaration. */
33602 if (clskey == xpect_key)
33603 continue;
33605 loc = location (i);
33606 key_redundant_p = key_redundant (i);
33607 /* Set the function declaration to print in diagnostic context. */
33608 current_function_decl = function (i);
33609 if (warning_at (loc, OPT_Wmismatched_tags,
33610 "%qT declared with a mismatched class-key %qs",
33611 type_decl, xmatchkstr))
33612 /* Suggest how to avoid the warning for each instance since
33613 the guidance may be different depending on context. */
33614 inform (loc,
33615 (key_redundant_p
33616 ? G_("remove the class-key or replace it with %qs")
33617 : G_("replace the class-key with %qs")),
33618 xpectkstr);
33621 /* Restore the current function in case it was replaced above. */
33622 current_function_decl = save_func;
33625 /* Issues -Wmismatched-tags for all classes. Called at the end
33626 of processing a translation unit, after declarations of all class
33627 types and their uses have been recorded. */
33629 void
33630 class_decl_loc_t::diag_mismatched_tags ()
33632 /* CLASS2LOC should be empty if both -Wmismatched-tags and
33633 -Wredundant-tags are disabled. */
33634 gcc_assert (warn_mismatched_tags
33635 || warn_redundant_tags
33636 || class2loc.is_empty ());
33638 /* Save the current function before changing on return. It should
33639 be null at this point. */
33640 temp_override<tree> cleanup (current_function_decl);
33642 if (warn_mismatched_tags)
33644 /* Iterate over the collected class/struct/template declarations. */
33645 typedef class_to_loc_map_t::iterator iter_t;
33646 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
33648 tree type_decl = (*it).first;
33649 class_decl_loc_t &recloc = (*it).second;
33650 recloc.diag_mismatched_tags (type_decl);
33654 class2loc.empty ();
33657 /* Issue an error message if DECL is redeclared with different
33658 access than its original declaration [class.access.spec/3].
33659 This applies to nested classes, nested class templates and
33660 enumerations [class.mem/1]. */
33662 static void
33663 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
33665 if (!decl
33666 || (!CLASS_TYPE_P (TREE_TYPE (decl))
33667 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
33668 return;
33670 if ((TREE_PRIVATE (decl)
33671 != (current_access_specifier == access_private_node))
33672 || (TREE_PROTECTED (decl)
33673 != (current_access_specifier == access_protected_node)))
33674 error_at (location, "%qD redeclared with different access", decl);
33677 /* Look for the `template' keyword, as a syntactic disambiguator.
33678 Return TRUE iff it is present, in which case it will be
33679 consumed. */
33681 static bool
33682 cp_parser_optional_template_keyword (cp_parser *parser)
33684 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
33686 /* In C++98 the `template' keyword can only be used within templates;
33687 outside templates the parser can always figure out what is a
33688 template and what is not. In C++11, per the resolution of DR 468,
33689 `template' is allowed in cases where it is not strictly necessary. */
33690 if (!processing_template_decl
33691 && pedantic && cxx_dialect == cxx98)
33693 cp_token *token = cp_lexer_peek_token (parser->lexer);
33694 pedwarn (token->location, OPT_Wpedantic,
33695 "in C++98 %<template%> (as a disambiguator) is only "
33696 "allowed within templates");
33697 /* If this part of the token stream is rescanned, the same
33698 error message would be generated. So, we purge the token
33699 from the stream. */
33700 cp_lexer_purge_token (parser->lexer);
33701 return false;
33703 else
33705 /* Consume the `template' keyword. */
33706 cp_lexer_consume_token (parser->lexer);
33707 return true;
33710 return false;
33713 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
33714 set PARSER->SCOPE, and perform other related actions. */
33716 static void
33717 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
33719 struct tree_check *check_value;
33721 /* Get the stored value. */
33722 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
33723 /* Set the scope from the stored value. */
33724 parser->scope = saved_checks_value (check_value);
33725 parser->qualifying_scope = check_value->qualifying_scope;
33726 parser->object_scope = parser->context->object_type;
33727 parser->context->object_type = NULL_TREE;
33730 /* Consume tokens up through a non-nested END token. Returns TRUE if we
33731 encounter the end of a block before what we were looking for. */
33733 static bool
33734 cp_parser_cache_group (cp_parser *parser,
33735 enum cpp_ttype end,
33736 unsigned depth)
33738 while (true)
33740 cp_token *token = cp_lexer_peek_token (parser->lexer);
33742 /* Abort a parenthesized expression if we encounter a semicolon. */
33743 if ((end == CPP_CLOSE_PAREN || depth == 0)
33744 && token->type == CPP_SEMICOLON)
33745 return true;
33746 /* If we've reached the end of the file, stop. */
33747 if (token->type == CPP_EOF
33748 || (end != CPP_PRAGMA_EOL
33749 && token->type == CPP_PRAGMA_EOL))
33750 return true;
33751 if (token->type == CPP_CLOSE_BRACE && depth == 0)
33752 /* We've hit the end of an enclosing block, so there's been some
33753 kind of syntax error. */
33754 return true;
33756 /* Consume the token. */
33757 cp_lexer_consume_token (parser->lexer);
33758 /* See if it starts a new group. */
33759 if (token->type == CPP_OPEN_BRACE)
33761 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
33762 /* In theory this should probably check end == '}', but
33763 cp_parser_save_member_function_body needs it to exit
33764 after either '}' or ')' when called with ')'. */
33765 if (depth == 0)
33766 return false;
33768 else if (token->type == CPP_OPEN_PAREN)
33770 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
33771 if (depth == 0 && end == CPP_CLOSE_PAREN)
33772 return false;
33774 else if (token->type == CPP_PRAGMA)
33775 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
33776 else if (token->type == end)
33777 return false;
33781 /* Like above, for caching a default argument or NSDMI. Both of these are
33782 terminated by a non-nested comma, but it can be unclear whether or not a
33783 comma is nested in a template argument list unless we do more parsing.
33784 In order to handle this ambiguity, when we encounter a ',' after a '<'
33785 we try to parse what follows as a parameter-declaration-list (in the
33786 case of a default argument) or a member-declarator (in the case of an
33787 NSDMI). If that succeeds, then we stop caching. */
33789 static tree
33790 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
33792 unsigned depth = 0;
33793 int maybe_template_id = 0;
33794 cp_token *first_token;
33795 cp_token *token;
33796 tree default_argument;
33798 /* Add tokens until we have processed the entire default
33799 argument. We add the range [first_token, token). */
33800 first_token = cp_lexer_peek_token (parser->lexer);
33801 if (first_token->type == CPP_OPEN_BRACE)
33803 /* For list-initialization, this is straightforward. */
33804 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33805 token = cp_lexer_peek_token (parser->lexer);
33807 else while (true)
33809 bool done = false;
33811 /* Peek at the next token. */
33812 token = cp_lexer_peek_token (parser->lexer);
33813 /* What we do depends on what token we have. */
33814 switch (token->type)
33816 /* In valid code, a default argument must be
33817 immediately followed by a `,' `)', or `...'. */
33818 case CPP_COMMA:
33819 if (depth == 0 && maybe_template_id)
33821 /* If we've seen a '<', we might be in a
33822 template-argument-list. Until Core issue 325 is
33823 resolved, we don't know how this situation ought
33824 to be handled, so try to DTRT. We check whether
33825 what comes after the comma is a valid parameter
33826 declaration list. If it is, then the comma ends
33827 the default argument; otherwise the default
33828 argument continues. */
33829 bool error = false;
33830 cp_token *peek;
33832 /* Set ITALP so cp_parser_parameter_declaration_list
33833 doesn't decide to commit to this parse. */
33834 bool saved_italp = parser->in_template_argument_list_p;
33835 parser->in_template_argument_list_p = true;
33837 cp_parser_parse_tentatively (parser);
33839 if (nsdmi)
33841 /* Parse declarators until we reach a non-comma or
33842 somthing that cannot be an initializer.
33843 Just checking whether we're looking at a single
33844 declarator is insufficient. Consider:
33845 int var = tuple<T,U>::x;
33846 The template parameter 'U' looks exactly like a
33847 declarator. */
33850 int ctor_dtor_or_conv_p;
33851 cp_lexer_consume_token (parser->lexer);
33852 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33853 CP_PARSER_FLAGS_NONE,
33854 &ctor_dtor_or_conv_p,
33855 /*parenthesized_p=*/NULL,
33856 /*member_p=*/true,
33857 /*friend_p=*/false,
33858 /*static_p=*/false);
33859 peek = cp_lexer_peek_token (parser->lexer);
33860 if (cp_parser_error_occurred (parser))
33861 break;
33863 while (peek->type == CPP_COMMA);
33864 /* If we met an '=' or ';' then the original comma
33865 was the end of the NSDMI. Otherwise assume
33866 we're still in the NSDMI. */
33867 error = (peek->type != CPP_EQ
33868 && peek->type != CPP_SEMICOLON);
33870 else
33872 cp_lexer_consume_token (parser->lexer);
33873 begin_scope (sk_function_parms, NULL_TREE);
33874 tree t = cp_parser_parameter_declaration_list
33875 (parser, CP_PARSER_FLAGS_NONE);
33876 if (t == error_mark_node)
33877 error = true;
33878 pop_bindings_and_leave_scope ();
33880 if (!cp_parser_error_occurred (parser) && !error)
33881 done = true;
33882 cp_parser_abort_tentative_parse (parser);
33884 parser->in_template_argument_list_p = saved_italp;
33885 break;
33887 /* FALLTHRU */
33888 case CPP_CLOSE_PAREN:
33889 case CPP_ELLIPSIS:
33890 /* If we run into a non-nested `;', `}', or `]',
33891 then the code is invalid -- but the default
33892 argument is certainly over. */
33893 case CPP_SEMICOLON:
33894 case CPP_CLOSE_BRACE:
33895 case CPP_CLOSE_SQUARE:
33896 if (depth == 0
33897 /* Handle correctly int n = sizeof ... ( p ); */
33898 && token->type != CPP_ELLIPSIS)
33899 done = true;
33900 /* Update DEPTH, if necessary. */
33901 else if (token->type == CPP_CLOSE_PAREN
33902 || token->type == CPP_CLOSE_BRACE
33903 || token->type == CPP_CLOSE_SQUARE)
33904 --depth;
33905 break;
33907 case CPP_OPEN_PAREN:
33908 case CPP_OPEN_SQUARE:
33909 case CPP_OPEN_BRACE:
33910 ++depth;
33911 break;
33913 case CPP_LESS:
33914 if (depth == 0)
33915 /* This might be the comparison operator, or it might
33916 start a template argument list. */
33917 ++maybe_template_id;
33918 break;
33920 case CPP_RSHIFT:
33921 if (cxx_dialect == cxx98)
33922 break;
33923 /* Fall through for C++0x, which treats the `>>'
33924 operator like two `>' tokens in certain
33925 cases. */
33926 gcc_fallthrough ();
33928 case CPP_GREATER:
33929 if (depth == 0)
33931 /* This might be an operator, or it might close a
33932 template argument list. But if a previous '<'
33933 started a template argument list, this will have
33934 closed it, so we can't be in one anymore. */
33935 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
33936 if (maybe_template_id < 0)
33937 maybe_template_id = 0;
33939 break;
33941 /* If we run out of tokens, issue an error message. */
33942 case CPP_EOF:
33943 case CPP_PRAGMA_EOL:
33944 error_at (token->location, "file ends in default argument");
33945 return error_mark_node;
33947 case CPP_NAME:
33948 case CPP_SCOPE:
33949 /* In these cases, we should look for template-ids.
33950 For example, if the default argument is
33951 `X<int, double>()', we need to do name lookup to
33952 figure out whether or not `X' is a template; if
33953 so, the `,' does not end the default argument.
33955 That is not yet done. */
33956 break;
33958 default:
33959 break;
33962 /* If we've reached the end, stop. */
33963 if (done)
33964 break;
33966 /* Add the token to the token block. */
33967 token = cp_lexer_consume_token (parser->lexer);
33970 /* Create a DEFERRED_PARSE to represent the unparsed default
33971 argument. */
33972 default_argument = make_node (DEFERRED_PARSE);
33973 DEFPARSE_TOKENS (default_argument)
33974 = cp_token_cache_new (first_token, token);
33975 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
33977 return default_argument;
33980 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
33982 location_t
33983 defparse_location (tree default_argument)
33985 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
33986 location_t start = tokens->first->location;
33987 location_t end = tokens->last->location;
33988 return make_location (start, start, end);
33991 /* Begin parsing tentatively. We always save tokens while parsing
33992 tentatively so that if the tentative parsing fails we can restore the
33993 tokens. */
33995 static void
33996 cp_parser_parse_tentatively (cp_parser* parser)
33998 /* Enter a new parsing context. */
33999 parser->context = cp_parser_context_new (parser->context);
34000 /* Begin saving tokens. */
34001 cp_lexer_save_tokens (parser->lexer);
34002 /* In order to avoid repetitive access control error messages,
34003 access checks are queued up until we are no longer parsing
34004 tentatively. */
34005 push_deferring_access_checks (dk_deferred);
34008 /* Commit to the currently active tentative parse. */
34010 static void
34011 cp_parser_commit_to_tentative_parse (cp_parser* parser)
34013 cp_parser_context *context;
34014 cp_lexer *lexer;
34016 /* Mark all of the levels as committed. */
34017 lexer = parser->lexer;
34018 for (context = parser->context; context->next; context = context->next)
34020 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34021 break;
34022 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34023 while (!cp_lexer_saving_tokens (lexer))
34024 lexer = lexer->next;
34025 cp_lexer_commit_tokens (lexer);
34029 /* Commit to the topmost currently active tentative parse.
34031 Note that this function shouldn't be called when there are
34032 irreversible side-effects while in a tentative state. For
34033 example, we shouldn't create a permanent entry in the symbol
34034 table, or issue an error message that might not apply if the
34035 tentative parse is aborted. */
34037 static void
34038 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
34040 cp_parser_context *context = parser->context;
34041 cp_lexer *lexer = parser->lexer;
34043 if (context)
34045 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34046 return;
34047 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34049 while (!cp_lexer_saving_tokens (lexer))
34050 lexer = lexer->next;
34051 cp_lexer_commit_tokens (lexer);
34055 /* Abort the currently active tentative parse. All consumed tokens
34056 will be rolled back, and no diagnostics will be issued. */
34058 static void
34059 cp_parser_abort_tentative_parse (cp_parser* parser)
34061 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
34062 || errorcount > 0);
34063 cp_parser_simulate_error (parser);
34064 /* Now, pretend that we want to see if the construct was
34065 successfully parsed. */
34066 cp_parser_parse_definitely (parser);
34069 /* Stop parsing tentatively. If a parse error has occurred, restore the
34070 token stream. Otherwise, commit to the tokens we have consumed.
34071 Returns true if no error occurred; false otherwise. */
34073 static bool
34074 cp_parser_parse_definitely (cp_parser* parser)
34076 bool error_occurred;
34077 cp_parser_context *context;
34079 /* Remember whether or not an error occurred, since we are about to
34080 destroy that information. */
34081 error_occurred = cp_parser_error_occurred (parser);
34082 /* Remove the topmost context from the stack. */
34083 context = parser->context;
34084 parser->context = context->next;
34085 /* If no parse errors occurred, commit to the tentative parse. */
34086 if (!error_occurred)
34088 /* Commit to the tokens read tentatively, unless that was
34089 already done. */
34090 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
34091 cp_lexer_commit_tokens (parser->lexer);
34093 pop_to_parent_deferring_access_checks ();
34095 /* Otherwise, if errors occurred, roll back our state so that things
34096 are just as they were before we began the tentative parse. */
34097 else
34099 cp_lexer_rollback_tokens (parser->lexer);
34100 pop_deferring_access_checks ();
34102 /* Add the context to the front of the free list. */
34103 context->next = cp_parser_context_free_list;
34104 cp_parser_context_free_list = context;
34106 return !error_occurred;
34109 /* Returns true if we are parsing tentatively and are not committed to
34110 this tentative parse. */
34112 static bool
34113 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
34115 return (cp_parser_parsing_tentatively (parser)
34116 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
34119 /* Returns nonzero iff an error has occurred during the most recent
34120 tentative parse. */
34122 static bool
34123 cp_parser_error_occurred (cp_parser* parser)
34125 return (cp_parser_parsing_tentatively (parser)
34126 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
34129 /* Returns nonzero if GNU extensions are allowed. */
34131 static bool
34132 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
34134 return parser->allow_gnu_extensions_p;
34137 /* Objective-C++ Productions */
34140 /* Parse an Objective-C expression, which feeds into a primary-expression
34141 above.
34143 objc-expression:
34144 objc-message-expression
34145 objc-string-literal
34146 objc-encode-expression
34147 objc-protocol-expression
34148 objc-selector-expression
34150 Returns a tree representation of the expression. */
34152 static cp_expr
34153 cp_parser_objc_expression (cp_parser* parser)
34155 /* Try to figure out what kind of declaration is present. */
34156 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34158 switch (kwd->type)
34160 case CPP_OPEN_SQUARE:
34161 return cp_parser_objc_message_expression (parser);
34163 case CPP_OBJC_STRING:
34164 kwd = cp_lexer_consume_token (parser->lexer);
34165 return objc_build_string_object (kwd->u.value);
34167 case CPP_KEYWORD:
34168 switch (kwd->keyword)
34170 case RID_AT_ENCODE:
34171 return cp_parser_objc_encode_expression (parser);
34173 case RID_AT_PROTOCOL:
34174 return cp_parser_objc_protocol_expression (parser);
34176 case RID_AT_SELECTOR:
34177 return cp_parser_objc_selector_expression (parser);
34179 default:
34180 break;
34182 /* FALLTHRU */
34183 default:
34184 error_at (kwd->location,
34185 "misplaced %<@%D%> Objective-C++ construct",
34186 kwd->u.value);
34187 cp_parser_skip_to_end_of_block_or_statement (parser);
34190 return error_mark_node;
34193 /* Parse an Objective-C message expression.
34195 objc-message-expression:
34196 [ objc-message-receiver objc-message-args ]
34198 Returns a representation of an Objective-C message. */
34200 static tree
34201 cp_parser_objc_message_expression (cp_parser* parser)
34203 tree receiver, messageargs;
34205 parser->objective_c_message_context_p = true;
34206 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34207 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
34208 receiver = cp_parser_objc_message_receiver (parser);
34209 messageargs = cp_parser_objc_message_args (parser);
34210 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
34211 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
34213 tree result = objc_build_message_expr (receiver, messageargs);
34215 /* Construct a location e.g.
34216 [self func1:5]
34217 ^~~~~~~~~~~~~~
34218 ranging from the '[' to the ']', with the caret at the start. */
34219 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
34220 protected_set_expr_location (result, combined_loc);
34222 parser->objective_c_message_context_p = false;
34223 return result;
34226 /* Parse an objc-message-receiver.
34228 objc-message-receiver:
34229 expression
34230 simple-type-specifier
34232 Returns a representation of the type or expression. */
34234 static tree
34235 cp_parser_objc_message_receiver (cp_parser* parser)
34237 tree rcv;
34239 /* An Objective-C message receiver may be either (1) a type
34240 or (2) an expression. */
34241 cp_parser_parse_tentatively (parser);
34242 rcv = cp_parser_expression (parser);
34244 /* If that worked out, fine. */
34245 if (cp_parser_parse_definitely (parser))
34246 return rcv;
34248 cp_parser_parse_tentatively (parser);
34249 rcv = cp_parser_simple_type_specifier (parser,
34250 /*decl_specs=*/NULL,
34251 CP_PARSER_FLAGS_NONE);
34253 if (cp_parser_parse_definitely (parser))
34254 return objc_get_class_reference (rcv);
34256 cp_parser_error (parser, "objective-c++ message receiver expected");
34257 return error_mark_node;
34260 /* Parse the arguments and selectors comprising an Objective-C message.
34262 objc-message-args:
34263 objc-selector
34264 objc-selector-args
34265 objc-selector-args , objc-comma-args
34267 objc-selector-args:
34268 objc-selector [opt] : assignment-expression
34269 objc-selector-args objc-selector [opt] : assignment-expression
34271 objc-comma-args:
34272 assignment-expression
34273 objc-comma-args , assignment-expression
34275 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
34276 selector arguments and TREE_VALUE containing a list of comma
34277 arguments. */
34279 static tree
34280 cp_parser_objc_message_args (cp_parser* parser)
34282 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
34283 bool maybe_unary_selector_p = true;
34284 cp_token *token = cp_lexer_peek_token (parser->lexer);
34286 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
34288 tree selector = NULL_TREE, arg;
34290 if (token->type != CPP_COLON)
34291 selector = cp_parser_objc_selector (parser);
34293 /* Detect if we have a unary selector. */
34294 if (maybe_unary_selector_p
34295 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34296 return build_tree_list (selector, NULL_TREE);
34298 maybe_unary_selector_p = false;
34299 cp_parser_require (parser, CPP_COLON, RT_COLON);
34300 arg = cp_parser_assignment_expression (parser);
34302 sel_args
34303 = chainon (sel_args,
34304 build_tree_list (selector, arg));
34306 token = cp_lexer_peek_token (parser->lexer);
34309 /* Handle non-selector arguments, if any. */
34310 while (token->type == CPP_COMMA)
34312 tree arg;
34314 cp_lexer_consume_token (parser->lexer);
34315 arg = cp_parser_assignment_expression (parser);
34317 addl_args
34318 = chainon (addl_args,
34319 build_tree_list (NULL_TREE, arg));
34321 token = cp_lexer_peek_token (parser->lexer);
34324 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
34326 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
34327 return build_tree_list (error_mark_node, error_mark_node);
34330 return build_tree_list (sel_args, addl_args);
34333 /* Parse an Objective-C encode expression.
34335 objc-encode-expression:
34336 @encode objc-typename
34338 Returns an encoded representation of the type argument. */
34340 static cp_expr
34341 cp_parser_objc_encode_expression (cp_parser* parser)
34343 tree type;
34344 cp_token *token;
34345 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34347 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
34348 matching_parens parens;
34349 parens.require_open (parser);
34350 token = cp_lexer_peek_token (parser->lexer);
34351 type = complete_type (cp_parser_type_id (parser));
34352 parens.require_close (parser);
34354 if (!type)
34356 error_at (token->location,
34357 "%<@encode%> must specify a type as an argument");
34358 return error_mark_node;
34361 /* This happens if we find @encode(T) (where T is a template
34362 typename or something dependent on a template typename) when
34363 parsing a template. In that case, we can't compile it
34364 immediately, but we rather create an AT_ENCODE_EXPR which will
34365 need to be instantiated when the template is used.
34367 if (dependent_type_p (type))
34369 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
34370 TREE_READONLY (value) = 1;
34371 return value;
34375 /* Build a location of the form:
34376 @encode(int)
34377 ^~~~~~~~~~~~
34378 with caret==start at the @ token, finishing at the close paren. */
34379 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34381 return cp_expr (objc_build_encode_expr (type), combined_loc);
34384 /* Parse an Objective-C @defs expression. */
34386 static tree
34387 cp_parser_objc_defs_expression (cp_parser *parser)
34389 tree name;
34391 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
34392 matching_parens parens;
34393 parens.require_open (parser);
34394 name = cp_parser_identifier (parser);
34395 parens.require_close (parser);
34397 return objc_get_class_ivars (name);
34400 /* Parse an Objective-C protocol expression.
34402 objc-protocol-expression:
34403 @protocol ( identifier )
34405 Returns a representation of the protocol expression. */
34407 static tree
34408 cp_parser_objc_protocol_expression (cp_parser* parser)
34410 tree proto;
34411 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34413 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
34414 matching_parens parens;
34415 parens.require_open (parser);
34416 proto = cp_parser_identifier (parser);
34417 parens.require_close (parser);
34419 /* Build a location of the form:
34420 @protocol(prot)
34421 ^~~~~~~~~~~~~~~
34422 with caret==start at the @ token, finishing at the close paren. */
34423 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34424 tree result = objc_build_protocol_expr (proto);
34425 protected_set_expr_location (result, combined_loc);
34426 return result;
34429 /* Parse an Objective-C selector expression.
34431 objc-selector-expression:
34432 @selector ( objc-method-signature )
34434 objc-method-signature:
34435 objc-selector
34436 objc-selector-seq
34438 objc-selector-seq:
34439 objc-selector :
34440 objc-selector-seq objc-selector :
34442 Returns a representation of the method selector. */
34444 static tree
34445 cp_parser_objc_selector_expression (cp_parser* parser)
34447 tree sel_seq = NULL_TREE;
34448 bool maybe_unary_selector_p = true;
34449 cp_token *token;
34450 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34452 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
34453 matching_parens parens;
34454 parens.require_open (parser);
34455 token = cp_lexer_peek_token (parser->lexer);
34457 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
34458 || token->type == CPP_SCOPE)
34460 tree selector = NULL_TREE;
34462 if (token->type != CPP_COLON
34463 || token->type == CPP_SCOPE)
34464 selector = cp_parser_objc_selector (parser);
34466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
34467 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
34469 /* Detect if we have a unary selector. */
34470 if (maybe_unary_selector_p)
34472 sel_seq = selector;
34473 goto finish_selector;
34475 else
34477 cp_parser_error (parser, "expected %<:%>");
34480 maybe_unary_selector_p = false;
34481 token = cp_lexer_consume_token (parser->lexer);
34483 if (token->type == CPP_SCOPE)
34485 sel_seq
34486 = chainon (sel_seq,
34487 build_tree_list (selector, NULL_TREE));
34488 sel_seq
34489 = chainon (sel_seq,
34490 build_tree_list (NULL_TREE, NULL_TREE));
34492 else
34493 sel_seq
34494 = chainon (sel_seq,
34495 build_tree_list (selector, NULL_TREE));
34497 token = cp_lexer_peek_token (parser->lexer);
34500 finish_selector:
34501 parens.require_close (parser);
34504 /* Build a location of the form:
34505 @selector(func)
34506 ^~~~~~~~~~~~~~~
34507 with caret==start at the @ token, finishing at the close paren. */
34508 location_t combined_loc = make_location (loc, loc, parser->lexer);
34509 tree result = objc_build_selector_expr (combined_loc, sel_seq);
34510 /* TODO: objc_build_selector_expr doesn't always honor the location. */
34511 protected_set_expr_location (result, combined_loc);
34512 return result;
34515 /* Parse a list of identifiers.
34517 objc-identifier-list:
34518 identifier
34519 objc-identifier-list , identifier
34521 Returns a TREE_LIST of identifier nodes. */
34523 static tree
34524 cp_parser_objc_identifier_list (cp_parser* parser)
34526 tree identifier;
34527 tree list;
34528 cp_token *sep;
34530 identifier = cp_parser_identifier (parser);
34531 if (identifier == error_mark_node)
34532 return error_mark_node;
34534 list = build_tree_list (NULL_TREE, identifier);
34535 sep = cp_lexer_peek_token (parser->lexer);
34537 while (sep->type == CPP_COMMA)
34539 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34540 identifier = cp_parser_identifier (parser);
34541 if (identifier == error_mark_node)
34542 return list;
34544 list = chainon (list, build_tree_list (NULL_TREE,
34545 identifier));
34546 sep = cp_lexer_peek_token (parser->lexer);
34549 return list;
34552 /* Parse an Objective-C alias declaration.
34554 objc-alias-declaration:
34555 @compatibility_alias identifier identifier ;
34557 This function registers the alias mapping with the Objective-C front end.
34558 It returns nothing. */
34560 static void
34561 cp_parser_objc_alias_declaration (cp_parser* parser)
34563 tree alias, orig;
34565 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
34566 alias = cp_parser_identifier (parser);
34567 orig = cp_parser_identifier (parser);
34568 objc_declare_alias (alias, orig);
34569 cp_parser_consume_semicolon_at_end_of_statement (parser);
34572 /* Parse an Objective-C class forward-declaration.
34574 objc-class-declaration:
34575 @class objc-identifier-list ;
34577 The function registers the forward declarations with the Objective-C
34578 front end. It returns nothing. */
34580 static void
34581 cp_parser_objc_class_declaration (cp_parser* parser)
34583 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
34584 while (true)
34586 tree id;
34588 id = cp_parser_identifier (parser);
34589 if (id == error_mark_node)
34590 break;
34592 objc_declare_class (id);
34594 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34595 cp_lexer_consume_token (parser->lexer);
34596 else
34597 break;
34599 cp_parser_consume_semicolon_at_end_of_statement (parser);
34602 /* Parse a list of Objective-C protocol references.
34604 objc-protocol-refs-opt:
34605 objc-protocol-refs [opt]
34607 objc-protocol-refs:
34608 < objc-identifier-list >
34610 Returns a TREE_LIST of identifiers, if any. */
34612 static tree
34613 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
34615 tree protorefs = NULL_TREE;
34617 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
34619 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
34620 protorefs = cp_parser_objc_identifier_list (parser);
34621 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
34624 return protorefs;
34627 /* Parse a Objective-C visibility specification. */
34629 static void
34630 cp_parser_objc_visibility_spec (cp_parser* parser)
34632 cp_token *vis = cp_lexer_peek_token (parser->lexer);
34634 switch (vis->keyword)
34636 case RID_AT_PRIVATE:
34637 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
34638 break;
34639 case RID_AT_PROTECTED:
34640 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
34641 break;
34642 case RID_AT_PUBLIC:
34643 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
34644 break;
34645 case RID_AT_PACKAGE:
34646 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
34647 break;
34648 default:
34649 return;
34652 /* Eat '@private'/'@protected'/'@public'. */
34653 cp_lexer_consume_token (parser->lexer);
34656 /* Parse an Objective-C method type. Return 'true' if it is a class
34657 (+) method, and 'false' if it is an instance (-) method. */
34659 static inline bool
34660 cp_parser_objc_method_type (cp_parser* parser)
34662 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
34663 return true;
34664 else
34665 return false;
34668 /* Parse an Objective-C protocol qualifier. */
34670 static tree
34671 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
34673 tree quals = NULL_TREE, node;
34674 cp_token *token = cp_lexer_peek_token (parser->lexer);
34676 node = token->u.value;
34678 while (node && identifier_p (node)
34679 && (node == ridpointers [(int) RID_IN]
34680 || node == ridpointers [(int) RID_OUT]
34681 || node == ridpointers [(int) RID_INOUT]
34682 || node == ridpointers [(int) RID_BYCOPY]
34683 || node == ridpointers [(int) RID_BYREF]
34684 || node == ridpointers [(int) RID_ONEWAY]))
34686 quals = tree_cons (NULL_TREE, node, quals);
34687 cp_lexer_consume_token (parser->lexer);
34688 token = cp_lexer_peek_token (parser->lexer);
34689 node = token->u.value;
34692 return quals;
34695 /* Parse an Objective-C typename. */
34697 static tree
34698 cp_parser_objc_typename (cp_parser* parser)
34700 tree type_name = NULL_TREE;
34702 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34704 tree proto_quals, cp_type = NULL_TREE;
34706 matching_parens parens;
34707 parens.consume_open (parser); /* Eat '('. */
34708 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
34710 /* An ObjC type name may consist of just protocol qualifiers, in which
34711 case the type shall default to 'id'. */
34712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34714 cp_type = cp_parser_type_id (parser);
34716 /* If the type could not be parsed, an error has already
34717 been produced. For error recovery, behave as if it had
34718 not been specified, which will use the default type
34719 'id'. */
34720 if (cp_type == error_mark_node)
34722 cp_type = NULL_TREE;
34723 /* We need to skip to the closing parenthesis as
34724 cp_parser_type_id() does not seem to do it for
34725 us. */
34726 cp_parser_skip_to_closing_parenthesis (parser,
34727 /*recovering=*/true,
34728 /*or_comma=*/false,
34729 /*consume_paren=*/false);
34733 parens.require_close (parser);
34734 type_name = build_tree_list (proto_quals, cp_type);
34737 return type_name;
34740 /* Check to see if TYPE refers to an Objective-C selector name. */
34742 static bool
34743 cp_parser_objc_selector_p (enum cpp_ttype type)
34745 return (type == CPP_NAME || type == CPP_KEYWORD
34746 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
34747 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
34748 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
34749 || type == CPP_XOR || type == CPP_XOR_EQ);
34752 /* Parse an Objective-C selector. */
34754 static tree
34755 cp_parser_objc_selector (cp_parser* parser)
34757 cp_token *token = cp_lexer_consume_token (parser->lexer);
34759 if (!cp_parser_objc_selector_p (token->type))
34761 error_at (token->location, "invalid Objective-C++ selector name");
34762 return error_mark_node;
34765 /* C++ operator names are allowed to appear in ObjC selectors. */
34766 switch (token->type)
34768 case CPP_AND_AND: return get_identifier ("and");
34769 case CPP_AND_EQ: return get_identifier ("and_eq");
34770 case CPP_AND: return get_identifier ("bitand");
34771 case CPP_OR: return get_identifier ("bitor");
34772 case CPP_COMPL: return get_identifier ("compl");
34773 case CPP_NOT: return get_identifier ("not");
34774 case CPP_NOT_EQ: return get_identifier ("not_eq");
34775 case CPP_OR_OR: return get_identifier ("or");
34776 case CPP_OR_EQ: return get_identifier ("or_eq");
34777 case CPP_XOR: return get_identifier ("xor");
34778 case CPP_XOR_EQ: return get_identifier ("xor_eq");
34779 default: return token->u.value;
34783 /* Parse an Objective-C params list. */
34785 static tree
34786 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
34788 tree params = NULL_TREE;
34789 bool maybe_unary_selector_p = true;
34790 cp_token *token = cp_lexer_peek_token (parser->lexer);
34792 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
34794 tree selector = NULL_TREE, type_name, identifier;
34795 tree parm_attr = NULL_TREE;
34797 if (token->keyword == RID_ATTRIBUTE)
34798 break;
34800 if (token->type != CPP_COLON)
34801 selector = cp_parser_objc_selector (parser);
34803 /* Detect if we have a unary selector. */
34804 if (maybe_unary_selector_p
34805 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34807 params = selector; /* Might be followed by attributes. */
34808 break;
34811 maybe_unary_selector_p = false;
34812 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34814 /* Something went quite wrong. There should be a colon
34815 here, but there is not. Stop parsing parameters. */
34816 break;
34818 type_name = cp_parser_objc_typename (parser);
34819 /* New ObjC allows attributes on parameters too. */
34820 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
34821 parm_attr = cp_parser_attributes_opt (parser);
34822 identifier = cp_parser_identifier (parser);
34824 params
34825 = chainon (params,
34826 objc_build_keyword_decl (selector,
34827 type_name,
34828 identifier,
34829 parm_attr));
34831 token = cp_lexer_peek_token (parser->lexer);
34834 if (params == NULL_TREE)
34836 cp_parser_error (parser, "objective-c++ method declaration is expected");
34837 return error_mark_node;
34840 /* We allow tail attributes for the method. */
34841 if (token->keyword == RID_ATTRIBUTE)
34843 *attributes = cp_parser_attributes_opt (parser);
34844 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
34845 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34846 return params;
34847 cp_parser_error (parser,
34848 "method attributes must be specified at the end");
34849 return error_mark_node;
34852 if (params == NULL_TREE)
34854 cp_parser_error (parser, "objective-c++ method declaration is expected");
34855 return error_mark_node;
34857 return params;
34860 /* Parse the non-keyword Objective-C params. */
34862 static tree
34863 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
34864 tree* attributes)
34866 tree params = make_node (TREE_LIST);
34867 cp_token *token = cp_lexer_peek_token (parser->lexer);
34868 *ellipsisp = false; /* Initially, assume no ellipsis. */
34870 while (token->type == CPP_COMMA)
34872 cp_parameter_declarator *parmdecl;
34873 tree parm;
34875 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34876 token = cp_lexer_peek_token (parser->lexer);
34878 if (token->type == CPP_ELLIPSIS)
34880 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
34881 *ellipsisp = true;
34882 token = cp_lexer_peek_token (parser->lexer);
34883 break;
34886 /* TODO: parse attributes for tail parameters. */
34887 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
34888 false, NULL);
34889 parm = grokdeclarator (parmdecl->declarator,
34890 &parmdecl->decl_specifiers,
34891 PARM, /*initialized=*/0,
34892 /*attrlist=*/NULL);
34894 chainon (params, build_tree_list (NULL_TREE, parm));
34895 token = cp_lexer_peek_token (parser->lexer);
34898 /* We allow tail attributes for the method. */
34899 if (token->keyword == RID_ATTRIBUTE)
34901 if (*attributes == NULL_TREE)
34903 *attributes = cp_parser_attributes_opt (parser);
34904 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
34905 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34906 return params;
34908 else
34909 /* We have an error, but parse the attributes, so that we can
34910 carry on. */
34911 *attributes = cp_parser_attributes_opt (parser);
34913 cp_parser_error (parser,
34914 "method attributes must be specified at the end");
34915 return error_mark_node;
34918 return params;
34921 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
34923 static void
34924 cp_parser_objc_interstitial_code (cp_parser* parser)
34926 cp_token *token = cp_lexer_peek_token (parser->lexer);
34928 /* If the next token is `extern' and the following token is a string
34929 literal, then we have a linkage specification. */
34930 if (token->keyword == RID_EXTERN
34931 && cp_parser_is_pure_string_literal
34932 (cp_lexer_peek_nth_token (parser->lexer, 2)))
34933 cp_parser_linkage_specification (parser, NULL_TREE);
34934 /* Handle #pragma, if any. */
34935 else if (token->type == CPP_PRAGMA)
34936 cp_parser_pragma (parser, pragma_objc_icode, NULL);
34937 /* Allow stray semicolons. */
34938 else if (token->type == CPP_SEMICOLON)
34939 cp_lexer_consume_token (parser->lexer);
34940 /* Mark methods as optional or required, when building protocols. */
34941 else if (token->keyword == RID_AT_OPTIONAL)
34943 cp_lexer_consume_token (parser->lexer);
34944 objc_set_method_opt (true);
34946 else if (token->keyword == RID_AT_REQUIRED)
34948 cp_lexer_consume_token (parser->lexer);
34949 objc_set_method_opt (false);
34951 else if (token->keyword == RID_NAMESPACE)
34952 cp_parser_namespace_definition (parser);
34953 /* Other stray characters must generate errors. */
34954 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
34956 cp_lexer_consume_token (parser->lexer);
34957 error ("stray %qs between Objective-C++ methods",
34958 token->type == CPP_OPEN_BRACE ? "{" : "}");
34960 /* Finally, try to parse a block-declaration, or a function-definition. */
34961 else
34962 cp_parser_block_declaration (parser, /*statement_p=*/false);
34965 /* Parse a method signature. */
34967 static tree
34968 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
34970 tree rettype, kwdparms, optparms;
34971 bool ellipsis = false;
34972 bool is_class_method;
34974 is_class_method = cp_parser_objc_method_type (parser);
34975 rettype = cp_parser_objc_typename (parser);
34976 *attributes = NULL_TREE;
34977 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
34978 if (kwdparms == error_mark_node)
34979 return error_mark_node;
34980 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
34981 if (optparms == error_mark_node)
34982 return error_mark_node;
34984 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
34987 static bool
34988 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
34990 tree tattr;
34991 cp_lexer_save_tokens (parser->lexer);
34992 tattr = cp_parser_attributes_opt (parser);
34993 gcc_assert (tattr) ;
34995 /* If the attributes are followed by a method introducer, this is not allowed.
34996 Dump the attributes and flag the situation. */
34997 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
34998 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34999 return true;
35001 /* Otherwise, the attributes introduce some interstitial code, possibly so
35002 rewind to allow that check. */
35003 cp_lexer_rollback_tokens (parser->lexer);
35004 return false;
35007 /* Parse an Objective-C method prototype list. */
35009 static void
35010 cp_parser_objc_method_prototype_list (cp_parser* parser)
35012 cp_token *token = cp_lexer_peek_token (parser->lexer);
35014 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
35016 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35018 tree attributes, sig;
35019 bool is_class_method;
35020 if (token->type == CPP_PLUS)
35021 is_class_method = true;
35022 else
35023 is_class_method = false;
35024 sig = cp_parser_objc_method_signature (parser, &attributes);
35025 if (sig == error_mark_node)
35027 cp_parser_skip_to_end_of_block_or_statement (parser);
35028 token = cp_lexer_peek_token (parser->lexer);
35029 continue;
35031 objc_add_method_declaration (is_class_method, sig, attributes);
35032 cp_parser_consume_semicolon_at_end_of_statement (parser);
35034 else if (token->keyword == RID_AT_PROPERTY)
35035 cp_parser_objc_at_property_declaration (parser);
35036 else if (token->keyword == RID_ATTRIBUTE
35037 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35038 warning_at (cp_lexer_peek_token (parser->lexer)->location,
35039 OPT_Wattributes,
35040 "prefix attributes are ignored for methods");
35041 else
35042 /* Allow for interspersed non-ObjC++ code. */
35043 cp_parser_objc_interstitial_code (parser);
35045 token = cp_lexer_peek_token (parser->lexer);
35048 if (token->type != CPP_EOF)
35049 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35050 else
35051 cp_parser_error (parser, "expected %<@end%>");
35053 objc_finish_interface ();
35056 /* Parse an Objective-C method definition list. */
35058 static void
35059 cp_parser_objc_method_definition_list (cp_parser* parser)
35061 for (;;)
35063 cp_token *token = cp_lexer_peek_token (parser->lexer);
35065 if (token->keyword == RID_AT_END)
35067 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35068 break;
35070 else if (token->type == CPP_EOF)
35072 cp_parser_error (parser, "expected %<@end%>");
35073 break;
35075 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35077 bool is_class_method = token->type == CPP_PLUS;
35079 push_deferring_access_checks (dk_deferred);
35080 tree attribute;
35081 tree sig = cp_parser_objc_method_signature (parser, &attribute);
35082 if (sig == error_mark_node)
35083 cp_parser_skip_to_end_of_block_or_statement (parser);
35084 else
35086 objc_start_method_definition (is_class_method, sig,
35087 attribute, NULL_TREE);
35089 /* For historical reasons, we accept an optional semicolon. */
35090 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35091 cp_lexer_consume_token (parser->lexer);
35093 perform_deferred_access_checks (tf_warning_or_error);
35094 stop_deferring_access_checks ();
35095 tree meth
35096 = cp_parser_function_definition_after_declarator (parser, false);
35097 pop_deferring_access_checks ();
35098 objc_finish_method_definition (meth);
35101 /* The following case will be removed once @synthesize is
35102 completely implemented. */
35103 else if (token->keyword == RID_AT_PROPERTY)
35104 cp_parser_objc_at_property_declaration (parser);
35105 else if (token->keyword == RID_AT_SYNTHESIZE)
35106 cp_parser_objc_at_synthesize_declaration (parser);
35107 else if (token->keyword == RID_AT_DYNAMIC)
35108 cp_parser_objc_at_dynamic_declaration (parser);
35109 else if (token->keyword == RID_ATTRIBUTE
35110 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35111 warning_at (token->location, OPT_Wattributes,
35112 "prefix attributes are ignored for methods");
35113 else
35114 /* Allow for interspersed non-ObjC++ code. */
35115 cp_parser_objc_interstitial_code (parser);
35118 objc_finish_implementation ();
35121 /* Parse Objective-C ivars. */
35123 static void
35124 cp_parser_objc_class_ivars (cp_parser* parser)
35126 cp_token *token = cp_lexer_peek_token (parser->lexer);
35128 if (token->type != CPP_OPEN_BRACE)
35129 return; /* No ivars specified. */
35131 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
35132 token = cp_lexer_peek_token (parser->lexer);
35134 while (token->type != CPP_CLOSE_BRACE
35135 && token->keyword != RID_AT_END && token->type != CPP_EOF)
35137 cp_decl_specifier_seq declspecs;
35138 int decl_class_or_enum_p;
35139 tree prefix_attributes;
35141 cp_parser_objc_visibility_spec (parser);
35143 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35144 break;
35146 cp_parser_decl_specifier_seq (parser,
35147 CP_PARSER_FLAGS_OPTIONAL,
35148 &declspecs,
35149 &decl_class_or_enum_p);
35151 /* auto, register, static, extern, mutable. */
35152 if (declspecs.storage_class != sc_none)
35154 cp_parser_error (parser, "invalid type for instance variable");
35155 declspecs.storage_class = sc_none;
35158 /* thread_local. */
35159 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
35161 cp_parser_error (parser, "invalid type for instance variable");
35162 declspecs.locations[ds_thread] = 0;
35165 /* typedef. */
35166 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
35168 cp_parser_error (parser, "invalid type for instance variable");
35169 declspecs.locations[ds_typedef] = 0;
35172 prefix_attributes = declspecs.attributes;
35173 declspecs.attributes = NULL_TREE;
35175 /* Keep going until we hit the `;' at the end of the
35176 declaration. */
35177 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35179 tree width = NULL_TREE, attributes, first_attribute, decl;
35180 cp_declarator *declarator = NULL;
35181 int ctor_dtor_or_conv_p;
35183 /* Check for a (possibly unnamed) bitfield declaration. */
35184 token = cp_lexer_peek_token (parser->lexer);
35185 if (token->type == CPP_COLON)
35186 goto eat_colon;
35188 if (token->type == CPP_NAME
35189 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
35190 == CPP_COLON))
35192 /* Get the name of the bitfield. */
35193 declarator = make_id_declarator (NULL_TREE,
35194 cp_parser_identifier (parser),
35195 sfk_none, token->location);
35197 eat_colon:
35198 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
35199 /* Get the width of the bitfield. */
35200 width
35201 = cp_parser_constant_expression (parser);
35203 else
35205 /* Parse the declarator. */
35206 declarator
35207 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35208 CP_PARSER_FLAGS_NONE,
35209 &ctor_dtor_or_conv_p,
35210 /*parenthesized_p=*/NULL,
35211 /*member_p=*/false,
35212 /*friend_p=*/false,
35213 /*static_p=*/false);
35216 /* Look for attributes that apply to the ivar. */
35217 attributes = cp_parser_attributes_opt (parser);
35218 /* Remember which attributes are prefix attributes and
35219 which are not. */
35220 first_attribute = attributes;
35221 /* Combine the attributes. */
35222 attributes = attr_chainon (prefix_attributes, attributes);
35224 if (width)
35225 /* Create the bitfield declaration. */
35226 decl = grokbitfield (declarator, &declspecs,
35227 width, NULL_TREE, attributes);
35228 else
35229 decl = grokfield (declarator, &declspecs,
35230 NULL_TREE, /*init_const_expr_p=*/false,
35231 NULL_TREE, attributes);
35233 /* Add the instance variable. */
35234 if (decl != error_mark_node && decl != NULL_TREE)
35235 objc_add_instance_variable (decl);
35237 /* Reset PREFIX_ATTRIBUTES. */
35238 if (attributes != error_mark_node)
35240 while (attributes && TREE_CHAIN (attributes) != first_attribute)
35241 attributes = TREE_CHAIN (attributes);
35242 if (attributes)
35243 TREE_CHAIN (attributes) = NULL_TREE;
35246 token = cp_lexer_peek_token (parser->lexer);
35248 if (token->type == CPP_COMMA)
35250 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35251 continue;
35253 break;
35256 cp_parser_consume_semicolon_at_end_of_statement (parser);
35257 token = cp_lexer_peek_token (parser->lexer);
35260 if (token->keyword == RID_AT_END)
35261 cp_parser_error (parser, "expected %<}%>");
35263 /* Do not consume the RID_AT_END, so it will be read again as terminating
35264 the @interface of @implementation. */
35265 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
35266 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
35268 /* For historical reasons, we accept an optional semicolon. */
35269 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35270 cp_lexer_consume_token (parser->lexer);
35273 /* Parse an Objective-C protocol declaration. */
35275 static void
35276 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
35278 tree proto, protorefs;
35279 cp_token *tok;
35281 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
35282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35284 tok = cp_lexer_peek_token (parser->lexer);
35285 error_at (tok->location, "identifier expected after %<@protocol%>");
35286 cp_parser_consume_semicolon_at_end_of_statement (parser);
35287 return;
35290 /* See if we have a forward declaration or a definition. */
35291 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
35293 /* Try a forward declaration first. */
35294 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
35296 while (true)
35298 tree id;
35300 id = cp_parser_identifier (parser);
35301 if (id == error_mark_node)
35302 break;
35304 objc_declare_protocol (id, attributes);
35306 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35307 cp_lexer_consume_token (parser->lexer);
35308 else
35309 break;
35311 cp_parser_consume_semicolon_at_end_of_statement (parser);
35314 /* Ok, we got a full-fledged definition (or at least should). */
35315 else
35317 proto = cp_parser_identifier (parser);
35318 protorefs = cp_parser_objc_protocol_refs_opt (parser);
35319 objc_start_protocol (proto, protorefs, attributes);
35320 cp_parser_objc_method_prototype_list (parser);
35324 /* Parse an Objective-C superclass or category. */
35326 static void
35327 cp_parser_objc_superclass_or_category (cp_parser *parser,
35328 bool iface_p,
35329 tree *super,
35330 tree *categ, bool *is_class_extension)
35332 cp_token *next = cp_lexer_peek_token (parser->lexer);
35334 *super = *categ = NULL_TREE;
35335 *is_class_extension = false;
35336 if (next->type == CPP_COLON)
35338 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
35339 *super = cp_parser_identifier (parser);
35341 else if (next->type == CPP_OPEN_PAREN)
35343 matching_parens parens;
35344 parens.consume_open (parser); /* Eat '('. */
35346 /* If there is no category name, and this is an @interface, we
35347 have a class extension. */
35348 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35350 *categ = NULL_TREE;
35351 *is_class_extension = true;
35353 else
35354 *categ = cp_parser_identifier (parser);
35356 parens.require_close (parser);
35360 /* Parse an Objective-C class interface. */
35362 static void
35363 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
35365 tree name, super, categ, protos;
35366 bool is_class_extension;
35368 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
35369 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
35370 name = cp_parser_identifier (parser);
35371 if (name == error_mark_node)
35373 /* It's hard to recover because even if valid @interface stuff
35374 is to follow, we can't compile it (or validate it) if we
35375 don't even know which class it refers to. Let's assume this
35376 was a stray '@interface' token in the stream and skip it.
35378 return;
35380 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
35381 &is_class_extension);
35382 protos = cp_parser_objc_protocol_refs_opt (parser);
35384 /* We have either a class or a category on our hands. */
35385 if (categ || is_class_extension)
35386 objc_start_category_interface (name, categ, protos, attributes);
35387 else
35389 objc_start_class_interface (name, nam_loc, super, protos, attributes);
35390 /* Handle instance variable declarations, if any. */
35391 cp_parser_objc_class_ivars (parser);
35392 objc_continue_interface ();
35395 cp_parser_objc_method_prototype_list (parser);
35398 /* Parse an Objective-C class implementation. */
35400 static void
35401 cp_parser_objc_class_implementation (cp_parser* parser)
35403 tree name, super, categ;
35404 bool is_class_extension;
35406 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
35407 name = cp_parser_identifier (parser);
35408 if (name == error_mark_node)
35410 /* It's hard to recover because even if valid @implementation
35411 stuff is to follow, we can't compile it (or validate it) if
35412 we don't even know which class it refers to. Let's assume
35413 this was a stray '@implementation' token in the stream and
35414 skip it.
35416 return;
35418 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
35419 &is_class_extension);
35421 /* We have either a class or a category on our hands. */
35422 if (categ)
35423 objc_start_category_implementation (name, categ);
35424 else
35426 objc_start_class_implementation (name, super);
35427 /* Handle instance variable declarations, if any. */
35428 cp_parser_objc_class_ivars (parser);
35429 objc_continue_implementation ();
35432 cp_parser_objc_method_definition_list (parser);
35435 /* Consume the @end token and finish off the implementation. */
35437 static void
35438 cp_parser_objc_end_implementation (cp_parser* parser)
35440 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35441 objc_finish_implementation ();
35444 /* Parse an Objective-C declaration. */
35446 static void
35447 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
35449 /* Try to figure out what kind of declaration is present. */
35450 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35452 if (attributes)
35453 switch (kwd->keyword)
35455 case RID_AT_ALIAS:
35456 case RID_AT_CLASS:
35457 case RID_AT_END:
35458 error_at (kwd->location, "attributes may not be specified before"
35459 " the %<@%D%> Objective-C++ keyword",
35460 kwd->u.value);
35461 attributes = NULL;
35462 break;
35463 case RID_AT_IMPLEMENTATION:
35464 warning_at (kwd->location, OPT_Wattributes,
35465 "prefix attributes are ignored before %<@%D%>",
35466 kwd->u.value);
35467 attributes = NULL;
35468 default:
35469 break;
35472 switch (kwd->keyword)
35474 case RID_AT_ALIAS:
35475 cp_parser_objc_alias_declaration (parser);
35476 break;
35477 case RID_AT_CLASS:
35478 cp_parser_objc_class_declaration (parser);
35479 break;
35480 case RID_AT_PROTOCOL:
35481 cp_parser_objc_protocol_declaration (parser, attributes);
35482 break;
35483 case RID_AT_INTERFACE:
35484 cp_parser_objc_class_interface (parser, attributes);
35485 break;
35486 case RID_AT_IMPLEMENTATION:
35487 cp_parser_objc_class_implementation (parser);
35488 break;
35489 case RID_AT_END:
35490 cp_parser_objc_end_implementation (parser);
35491 break;
35492 default:
35493 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35494 kwd->u.value);
35495 cp_parser_skip_to_end_of_block_or_statement (parser);
35499 /* Parse an Objective-C try-catch-finally statement.
35501 objc-try-catch-finally-stmt:
35502 @try compound-statement objc-catch-clause-seq [opt]
35503 objc-finally-clause [opt]
35505 objc-catch-clause-seq:
35506 objc-catch-clause objc-catch-clause-seq [opt]
35508 objc-catch-clause:
35509 @catch ( objc-exception-declaration ) compound-statement
35511 objc-finally-clause:
35512 @finally compound-statement
35514 objc-exception-declaration:
35515 parameter-declaration
35516 '...'
35518 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
35520 Returns NULL_TREE.
35522 PS: This function is identical to c_parser_objc_try_catch_finally_statement
35523 for C. Keep them in sync. */
35525 static tree
35526 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
35528 location_t location;
35529 tree stmt;
35531 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
35532 location = cp_lexer_peek_token (parser->lexer)->location;
35533 objc_maybe_warn_exceptions (location);
35534 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
35535 node, lest it get absorbed into the surrounding block. */
35536 stmt = push_stmt_list ();
35537 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35538 objc_begin_try_stmt (location, pop_stmt_list (stmt));
35540 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
35542 cp_parameter_declarator *parm;
35543 tree parameter_declaration = error_mark_node;
35544 bool seen_open_paren = false;
35545 matching_parens parens;
35547 cp_lexer_consume_token (parser->lexer);
35548 if (parens.require_open (parser))
35549 seen_open_paren = true;
35550 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
35552 /* We have "@catch (...)" (where the '...' are literally
35553 what is in the code). Skip the '...'.
35554 parameter_declaration is set to NULL_TREE, and
35555 objc_being_catch_clauses() knows that that means
35556 '...'. */
35557 cp_lexer_consume_token (parser->lexer);
35558 parameter_declaration = NULL_TREE;
35560 else
35562 /* We have "@catch (NSException *exception)" or something
35563 like that. Parse the parameter declaration. */
35564 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35565 false, NULL);
35566 if (parm == NULL)
35567 parameter_declaration = error_mark_node;
35568 else
35569 parameter_declaration = grokdeclarator (parm->declarator,
35570 &parm->decl_specifiers,
35571 PARM, /*initialized=*/0,
35572 /*attrlist=*/NULL);
35574 if (seen_open_paren)
35575 parens.require_close (parser);
35576 else
35578 /* If there was no open parenthesis, we are recovering from
35579 an error, and we are trying to figure out what mistake
35580 the user has made. */
35582 /* If there is an immediate closing parenthesis, the user
35583 probably forgot the opening one (ie, they typed "@catch
35584 NSException *e)". Parse the closing parenthesis and keep
35585 going. */
35586 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35587 cp_lexer_consume_token (parser->lexer);
35589 /* If these is no immediate closing parenthesis, the user
35590 probably doesn't know that parenthesis are required at
35591 all (ie, they typed "@catch NSException *e"). So, just
35592 forget about the closing parenthesis and keep going. */
35594 objc_begin_catch_clause (parameter_declaration);
35595 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35596 objc_finish_catch_clause ();
35598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
35600 cp_lexer_consume_token (parser->lexer);
35601 location = cp_lexer_peek_token (parser->lexer)->location;
35602 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
35603 node, lest it get absorbed into the surrounding block. */
35604 stmt = push_stmt_list ();
35605 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35606 objc_build_finally_clause (location, pop_stmt_list (stmt));
35609 return objc_finish_try_stmt ();
35612 /* Parse an Objective-C synchronized statement.
35614 objc-synchronized-stmt:
35615 @synchronized ( expression ) compound-statement
35617 Returns NULL_TREE. */
35619 static tree
35620 cp_parser_objc_synchronized_statement (cp_parser *parser)
35622 location_t location;
35623 tree lock, stmt;
35625 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
35627 location = cp_lexer_peek_token (parser->lexer)->location;
35628 objc_maybe_warn_exceptions (location);
35629 matching_parens parens;
35630 parens.require_open (parser);
35631 lock = cp_parser_expression (parser);
35632 parens.require_close (parser);
35634 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
35635 node, lest it get absorbed into the surrounding block. */
35636 stmt = push_stmt_list ();
35637 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35639 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
35642 /* Parse an Objective-C throw statement.
35644 objc-throw-stmt:
35645 @throw assignment-expression [opt] ;
35647 Returns a constructed '@throw' statement. */
35649 static tree
35650 cp_parser_objc_throw_statement (cp_parser *parser)
35652 tree expr = NULL_TREE;
35653 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35655 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
35657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35658 expr = cp_parser_expression (parser);
35660 cp_parser_consume_semicolon_at_end_of_statement (parser);
35662 return objc_build_throw_stmt (loc, expr);
35665 /* Parse an Objective-C statement. */
35667 static tree
35668 cp_parser_objc_statement (cp_parser * parser)
35670 /* Try to figure out what kind of declaration is present. */
35671 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35673 switch (kwd->keyword)
35675 case RID_AT_TRY:
35676 return cp_parser_objc_try_catch_finally_statement (parser);
35677 case RID_AT_SYNCHRONIZED:
35678 return cp_parser_objc_synchronized_statement (parser);
35679 case RID_AT_THROW:
35680 return cp_parser_objc_throw_statement (parser);
35681 default:
35682 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35683 kwd->u.value);
35684 cp_parser_skip_to_end_of_block_or_statement (parser);
35687 return error_mark_node;
35690 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
35691 look ahead to see if an objc keyword follows the attributes. This
35692 is to detect the use of prefix attributes on ObjC @interface and
35693 @protocol. */
35695 static bool
35696 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
35698 cp_lexer_save_tokens (parser->lexer);
35699 tree addon = cp_parser_attributes_opt (parser);
35700 if (addon
35701 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
35703 cp_lexer_commit_tokens (parser->lexer);
35704 if (*attrib)
35705 TREE_CHAIN (*attrib) = addon;
35706 else
35707 *attrib = addon;
35708 return true;
35710 cp_lexer_rollback_tokens (parser->lexer);
35711 return false;
35714 /* This routine is a minimal replacement for
35715 c_parser_struct_declaration () used when parsing the list of
35716 types/names or ObjC++ properties. For example, when parsing the
35717 code
35719 @property (readonly) int a, b, c;
35721 this function is responsible for parsing "int a, int b, int c" and
35722 returning the declarations as CHAIN of DECLs.
35724 TODO: Share this code with cp_parser_objc_class_ivars. It's very
35725 similar parsing. */
35726 static tree
35727 cp_parser_objc_struct_declaration (cp_parser *parser)
35729 tree decls = NULL_TREE;
35730 cp_decl_specifier_seq declspecs;
35731 int decl_class_or_enum_p;
35732 tree prefix_attributes;
35734 cp_parser_decl_specifier_seq (parser,
35735 CP_PARSER_FLAGS_NONE,
35736 &declspecs,
35737 &decl_class_or_enum_p);
35739 if (declspecs.type == error_mark_node)
35740 return error_mark_node;
35742 /* auto, register, static, extern, mutable. */
35743 if (declspecs.storage_class != sc_none)
35745 cp_parser_error (parser, "invalid type for property");
35746 declspecs.storage_class = sc_none;
35749 /* thread_local. */
35750 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
35752 cp_parser_error (parser, "invalid type for property");
35753 declspecs.locations[ds_thread] = 0;
35756 /* typedef. */
35757 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
35759 cp_parser_error (parser, "invalid type for property");
35760 declspecs.locations[ds_typedef] = 0;
35763 prefix_attributes = declspecs.attributes;
35764 declspecs.attributes = NULL_TREE;
35766 /* Keep going until we hit the `;' at the end of the declaration. */
35767 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35769 tree attributes, first_attribute, decl;
35770 cp_declarator *declarator;
35771 cp_token *token;
35773 /* Parse the declarator. */
35774 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35775 CP_PARSER_FLAGS_NONE,
35776 NULL, NULL, false, false, false);
35778 /* Look for attributes that apply to the ivar. */
35779 attributes = cp_parser_attributes_opt (parser);
35780 /* Remember which attributes are prefix attributes and
35781 which are not. */
35782 first_attribute = attributes;
35783 /* Combine the attributes. */
35784 attributes = attr_chainon (prefix_attributes, attributes);
35786 decl = grokfield (declarator, &declspecs,
35787 NULL_TREE, /*init_const_expr_p=*/false,
35788 NULL_TREE, attributes);
35790 if (decl == error_mark_node || decl == NULL_TREE)
35791 return error_mark_node;
35793 /* Reset PREFIX_ATTRIBUTES. */
35794 if (attributes != error_mark_node)
35796 while (attributes && TREE_CHAIN (attributes) != first_attribute)
35797 attributes = TREE_CHAIN (attributes);
35798 if (attributes)
35799 TREE_CHAIN (attributes) = NULL_TREE;
35802 DECL_CHAIN (decl) = decls;
35803 decls = decl;
35805 token = cp_lexer_peek_token (parser->lexer);
35806 if (token->type == CPP_COMMA)
35808 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35809 continue;
35811 else
35812 break;
35814 return decls;
35817 /* Parse an Objective-C @property declaration. The syntax is:
35819 objc-property-declaration:
35820 '@property' objc-property-attributes[opt] struct-declaration ;
35822 objc-property-attributes:
35823 '(' objc-property-attribute-list ')'
35825 objc-property-attribute-list:
35826 objc-property-attribute
35827 objc-property-attribute-list, objc-property-attribute
35829 objc-property-attribute
35830 'getter' = identifier
35831 'setter' = identifier
35832 'readonly'
35833 'readwrite'
35834 'assign'
35835 'retain'
35836 'copy'
35837 'nonatomic'
35839 For example:
35840 @property NSString *name;
35841 @property (readonly) id object;
35842 @property (retain, nonatomic, getter=getTheName) id name;
35843 @property int a, b, c;
35845 PS: This function is identical to
35846 c_parser_objc_at_property_declaration for C. Keep them in sync. */
35847 static void
35848 cp_parser_objc_at_property_declaration (cp_parser *parser)
35850 /* Parse the optional attribute list.
35852 A list of parsed, but not verified, attributes. */
35853 auto_delete_vec<property_attribute_info> prop_attr_list;
35854 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35856 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
35858 /* Parse the optional attribute list... */
35859 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35861 /* Eat the '('. */
35862 matching_parens parens;
35863 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
35864 parens.consume_open (parser);
35865 bool syntax_error = false;
35867 /* Allow empty @property attribute lists, but with a warning. */
35868 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
35869 location_t attr_comb;
35870 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35872 attr_comb = make_location (attr_end, attr_start, attr_end);
35873 warning_at (attr_comb, OPT_Wattributes,
35874 "empty property attribute list");
35876 else
35877 while (true)
35879 cp_token *token = cp_lexer_peek_token (parser->lexer);
35880 attr_start = token->location;
35881 attr_end = get_finish (token->location);
35882 attr_comb = make_location (attr_start, attr_start, attr_end);
35884 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
35886 warning_at (attr_comb, OPT_Wattributes,
35887 "missing property attribute");
35888 if (token->type == CPP_CLOSE_PAREN)
35889 break;
35890 cp_lexer_consume_token (parser->lexer);
35891 continue;
35894 tree attr_name = NULL_TREE;
35895 if (identifier_p (token->u.value))
35896 attr_name = token->u.value;
35898 enum rid keyword;
35899 if (token->type == CPP_NAME)
35900 keyword = C_RID_CODE (token->u.value);
35901 else if (token->type == CPP_KEYWORD
35902 && token->keyword == RID_CLASS)
35903 /* Account for accepting the 'class' keyword in this context. */
35904 keyword = RID_CLASS;
35905 else
35906 keyword = RID_MAX; /* By definition, an unknown property. */
35907 cp_lexer_consume_token (parser->lexer);
35909 enum objc_property_attribute_kind prop_kind
35910 = objc_prop_attr_kind_for_rid (keyword);
35911 property_attribute_info *prop
35912 = new property_attribute_info (attr_name, attr_comb, prop_kind);
35913 prop_attr_list.safe_push (prop);
35915 tree meth_name;
35916 switch (prop->prop_kind)
35918 default: break;
35919 case OBJC_PROPERTY_ATTR_UNKNOWN:
35920 if (attr_name)
35921 error_at (attr_start, "unknown property attribute %qE",
35922 attr_name);
35923 else
35924 error_at (attr_start, "unknown property attribute");
35925 prop->parse_error = syntax_error = true;
35926 break;
35928 case OBJC_PROPERTY_ATTR_GETTER:
35929 case OBJC_PROPERTY_ATTR_SETTER:
35930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35932 attr_comb = make_location (attr_end, attr_start, attr_end);
35933 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
35934 attr_name);
35935 prop->parse_error = syntax_error = true;
35936 break;
35939 token = cp_lexer_peek_token (parser->lexer);
35940 attr_end = token->location;
35941 cp_lexer_consume_token (parser->lexer); /* eat the = */
35943 if (!cp_parser_objc_selector_p
35944 (cp_lexer_peek_token (parser->lexer)->type))
35946 attr_comb = make_location (attr_end, attr_start, attr_end);
35947 error_at (attr_comb, "expected %qE selector name",
35948 attr_name);
35949 prop->parse_error = syntax_error = true;
35950 break;
35953 /* Get the end of the method name, and consume the name. */
35954 token = cp_lexer_peek_token (parser->lexer);
35955 attr_end = get_finish (token->location);
35956 /* Because method names may contain C++ keywords, we have a
35957 routine to fetch them (this also consumes the token). */
35958 meth_name = cp_parser_objc_selector (parser);
35960 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
35962 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35964 attr_comb = make_location (attr_end, attr_start,
35965 attr_end);
35966 error_at (attr_comb, "setter method names must"
35967 " terminate with %<:%>");
35968 prop->parse_error = syntax_error = true;
35970 else
35972 attr_end = get_finish (cp_lexer_peek_token
35973 (parser->lexer)->location);
35974 cp_lexer_consume_token (parser->lexer);
35976 attr_comb = make_location (attr_start, attr_start,
35977 attr_end);
35979 else
35980 attr_comb = make_location (attr_start, attr_start,
35981 attr_end);
35982 prop->ident = meth_name;
35983 /* Updated location including all that was successfully
35984 parsed. */
35985 prop->prop_loc = attr_comb;
35986 break;
35989 /* If we see a comma here, then keep going - even if we already
35990 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
35991 this makes a more useful output and avoid spurious warnings
35992 about missing attributes that are, in fact, specified after the
35993 one with the syntax error. */
35994 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35995 cp_lexer_consume_token (parser->lexer);
35996 else
35997 break;
36000 if (syntax_error || !parens.require_close (parser))
36001 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36002 /*or_comma=*/false,
36003 /*consume_paren=*/true);
36006 /* 'properties' is the list of properties that we read. Usually a
36007 single one, but maybe more (eg, in "@property int a, b, c;" there
36008 are three).
36009 TODO: Update this parsing so that it accepts (erroneous) bitfields so
36010 that we can issue a meaningful and consistent (between C/C++) error
36011 message from objc_add_property_declaration (). */
36012 tree properties = cp_parser_objc_struct_declaration (parser);
36014 if (properties == error_mark_node)
36015 cp_parser_skip_to_end_of_statement (parser);
36016 else if (properties == NULL_TREE)
36017 cp_parser_error (parser, "expected identifier");
36018 else
36020 /* Comma-separated properties are chained together in reverse order;
36021 add them one by one. */
36022 properties = nreverse (properties);
36023 for (; properties; properties = TREE_CHAIN (properties))
36024 objc_add_property_declaration (loc, copy_node (properties),
36025 prop_attr_list);
36028 cp_parser_consume_semicolon_at_end_of_statement (parser);
36031 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
36033 objc-synthesize-declaration:
36034 @synthesize objc-synthesize-identifier-list ;
36036 objc-synthesize-identifier-list:
36037 objc-synthesize-identifier
36038 objc-synthesize-identifier-list, objc-synthesize-identifier
36040 objc-synthesize-identifier
36041 identifier
36042 identifier = identifier
36044 For example:
36045 @synthesize MyProperty;
36046 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
36048 PS: This function is identical to c_parser_objc_at_synthesize_declaration
36049 for C. Keep them in sync.
36051 static void
36052 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
36054 tree list = NULL_TREE;
36055 location_t loc;
36056 loc = cp_lexer_peek_token (parser->lexer)->location;
36058 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
36059 while (true)
36061 tree property, ivar;
36062 property = cp_parser_identifier (parser);
36063 if (property == error_mark_node)
36065 cp_parser_consume_semicolon_at_end_of_statement (parser);
36066 return;
36068 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
36070 cp_lexer_consume_token (parser->lexer);
36071 ivar = cp_parser_identifier (parser);
36072 if (ivar == error_mark_node)
36074 cp_parser_consume_semicolon_at_end_of_statement (parser);
36075 return;
36078 else
36079 ivar = NULL_TREE;
36080 list = chainon (list, build_tree_list (ivar, property));
36081 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36082 cp_lexer_consume_token (parser->lexer);
36083 else
36084 break;
36086 cp_parser_consume_semicolon_at_end_of_statement (parser);
36087 objc_add_synthesize_declaration (loc, list);
36090 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
36092 objc-dynamic-declaration:
36093 @dynamic identifier-list ;
36095 For example:
36096 @dynamic MyProperty;
36097 @dynamic MyProperty, AnotherProperty;
36099 PS: This function is identical to c_parser_objc_at_dynamic_declaration
36100 for C. Keep them in sync.
36102 static void
36103 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
36105 tree list = NULL_TREE;
36106 location_t loc;
36107 loc = cp_lexer_peek_token (parser->lexer)->location;
36109 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
36110 while (true)
36112 tree property;
36113 property = cp_parser_identifier (parser);
36114 if (property == error_mark_node)
36116 cp_parser_consume_semicolon_at_end_of_statement (parser);
36117 return;
36119 list = chainon (list, build_tree_list (NULL, property));
36120 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36121 cp_lexer_consume_token (parser->lexer);
36122 else
36123 break;
36125 cp_parser_consume_semicolon_at_end_of_statement (parser);
36126 objc_add_dynamic_declaration (loc, list);
36130 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
36132 /* Returns name of the next clause.
36133 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
36134 the token is not consumed. Otherwise appropriate pragma_omp_clause is
36135 returned and the token is consumed. */
36137 static pragma_omp_clause
36138 cp_parser_omp_clause_name (cp_parser *parser)
36140 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
36142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36143 result = PRAGMA_OACC_CLAUSE_AUTO;
36144 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
36145 result = PRAGMA_OMP_CLAUSE_IF;
36146 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
36147 result = PRAGMA_OMP_CLAUSE_DEFAULT;
36148 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
36149 result = PRAGMA_OACC_CLAUSE_DELETE;
36150 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
36151 result = PRAGMA_OMP_CLAUSE_PRIVATE;
36152 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36153 result = PRAGMA_OMP_CLAUSE_FOR;
36154 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36156 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36157 const char *p = IDENTIFIER_POINTER (id);
36159 switch (p[0])
36161 case 'a':
36162 if (!strcmp ("affinity", p))
36163 result = PRAGMA_OMP_CLAUSE_AFFINITY;
36164 else if (!strcmp ("aligned", p))
36165 result = PRAGMA_OMP_CLAUSE_ALIGNED;
36166 else if (!strcmp ("allocate", p))
36167 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
36168 else if (!strcmp ("async", p))
36169 result = PRAGMA_OACC_CLAUSE_ASYNC;
36170 else if (!strcmp ("attach", p))
36171 result = PRAGMA_OACC_CLAUSE_ATTACH;
36172 break;
36173 case 'b':
36174 if (!strcmp ("bind", p))
36175 result = PRAGMA_OMP_CLAUSE_BIND;
36176 break;
36177 case 'c':
36178 if (!strcmp ("collapse", p))
36179 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
36180 else if (!strcmp ("copy", p))
36181 result = PRAGMA_OACC_CLAUSE_COPY;
36182 else if (!strcmp ("copyin", p))
36183 result = PRAGMA_OMP_CLAUSE_COPYIN;
36184 else if (!strcmp ("copyout", p))
36185 result = PRAGMA_OACC_CLAUSE_COPYOUT;
36186 else if (!strcmp ("copyprivate", p))
36187 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
36188 else if (!strcmp ("create", p))
36189 result = PRAGMA_OACC_CLAUSE_CREATE;
36190 break;
36191 case 'd':
36192 if (!strcmp ("defaultmap", p))
36193 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
36194 else if (!strcmp ("depend", p))
36195 result = PRAGMA_OMP_CLAUSE_DEPEND;
36196 else if (!strcmp ("detach", p))
36197 result = PRAGMA_OACC_CLAUSE_DETACH;
36198 else if (!strcmp ("device", p))
36199 result = PRAGMA_OMP_CLAUSE_DEVICE;
36200 else if (!strcmp ("deviceptr", p))
36201 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
36202 else if (!strcmp ("device_resident", p))
36203 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
36204 else if (!strcmp ("device_type", p))
36205 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
36206 else if (!strcmp ("dist_schedule", p))
36207 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
36208 break;
36209 case 'f':
36210 if (!strcmp ("filter", p))
36211 result = PRAGMA_OMP_CLAUSE_FILTER;
36212 else if (!strcmp ("final", p))
36213 result = PRAGMA_OMP_CLAUSE_FINAL;
36214 else if (!strcmp ("finalize", p))
36215 result = PRAGMA_OACC_CLAUSE_FINALIZE;
36216 else if (!strcmp ("firstprivate", p))
36217 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
36218 else if (!strcmp ("from", p))
36219 result = PRAGMA_OMP_CLAUSE_FROM;
36220 break;
36221 case 'g':
36222 if (!strcmp ("gang", p))
36223 result = PRAGMA_OACC_CLAUSE_GANG;
36224 else if (!strcmp ("grainsize", p))
36225 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
36226 break;
36227 case 'h':
36228 if (!strcmp ("hint", p))
36229 result = PRAGMA_OMP_CLAUSE_HINT;
36230 else if (!strcmp ("host", p))
36231 result = PRAGMA_OACC_CLAUSE_HOST;
36232 break;
36233 case 'i':
36234 if (!strcmp ("if_present", p))
36235 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
36236 else if (!strcmp ("in_reduction", p))
36237 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
36238 else if (!strcmp ("inbranch", p))
36239 result = PRAGMA_OMP_CLAUSE_INBRANCH;
36240 else if (!strcmp ("independent", p))
36241 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
36242 else if (!strcmp ("is_device_ptr", p))
36243 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
36244 break;
36245 case 'l':
36246 if (!strcmp ("lastprivate", p))
36247 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
36248 else if (!strcmp ("linear", p))
36249 result = PRAGMA_OMP_CLAUSE_LINEAR;
36250 else if (!strcmp ("link", p))
36251 result = PRAGMA_OMP_CLAUSE_LINK;
36252 break;
36253 case 'm':
36254 if (!strcmp ("map", p))
36255 result = PRAGMA_OMP_CLAUSE_MAP;
36256 else if (!strcmp ("mergeable", p))
36257 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
36258 break;
36259 case 'n':
36260 if (!strcmp ("no_create", p))
36261 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
36262 else if (!strcmp ("nogroup", p))
36263 result = PRAGMA_OMP_CLAUSE_NOGROUP;
36264 else if (!strcmp ("nohost", p))
36265 result = PRAGMA_OACC_CLAUSE_NOHOST;
36266 else if (!strcmp ("nontemporal", p))
36267 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
36268 else if (!strcmp ("notinbranch", p))
36269 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
36270 else if (!strcmp ("nowait", p))
36271 result = PRAGMA_OMP_CLAUSE_NOWAIT;
36272 else if (!strcmp ("num_gangs", p))
36273 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
36274 else if (!strcmp ("num_tasks", p))
36275 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
36276 else if (!strcmp ("num_teams", p))
36277 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
36278 else if (!strcmp ("num_threads", p))
36279 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
36280 else if (!strcmp ("num_workers", p))
36281 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
36282 break;
36283 case 'o':
36284 if (!strcmp ("ordered", p))
36285 result = PRAGMA_OMP_CLAUSE_ORDERED;
36286 else if (!strcmp ("order", p))
36287 result = PRAGMA_OMP_CLAUSE_ORDER;
36288 break;
36289 case 'p':
36290 if (!strcmp ("parallel", p))
36291 result = PRAGMA_OMP_CLAUSE_PARALLEL;
36292 else if (!strcmp ("present", p))
36293 result = PRAGMA_OACC_CLAUSE_PRESENT;
36294 else if (!strcmp ("present_or_copy", p)
36295 || !strcmp ("pcopy", p))
36296 result = PRAGMA_OACC_CLAUSE_COPY;
36297 else if (!strcmp ("present_or_copyin", p)
36298 || !strcmp ("pcopyin", p))
36299 result = PRAGMA_OACC_CLAUSE_COPYIN;
36300 else if (!strcmp ("present_or_copyout", p)
36301 || !strcmp ("pcopyout", p))
36302 result = PRAGMA_OACC_CLAUSE_COPYOUT;
36303 else if (!strcmp ("present_or_create", p)
36304 || !strcmp ("pcreate", p))
36305 result = PRAGMA_OACC_CLAUSE_CREATE;
36306 else if (!strcmp ("priority", p))
36307 result = PRAGMA_OMP_CLAUSE_PRIORITY;
36308 else if (!strcmp ("proc_bind", p))
36309 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
36310 break;
36311 case 'r':
36312 if (!strcmp ("reduction", p))
36313 result = PRAGMA_OMP_CLAUSE_REDUCTION;
36314 break;
36315 case 's':
36316 if (!strcmp ("safelen", p))
36317 result = PRAGMA_OMP_CLAUSE_SAFELEN;
36318 else if (!strcmp ("schedule", p))
36319 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
36320 else if (!strcmp ("sections", p))
36321 result = PRAGMA_OMP_CLAUSE_SECTIONS;
36322 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
36323 result = PRAGMA_OACC_CLAUSE_HOST;
36324 else if (!strcmp ("seq", p))
36325 result = PRAGMA_OACC_CLAUSE_SEQ;
36326 else if (!strcmp ("shared", p))
36327 result = PRAGMA_OMP_CLAUSE_SHARED;
36328 else if (!strcmp ("simd", p))
36329 result = PRAGMA_OMP_CLAUSE_SIMD;
36330 else if (!strcmp ("simdlen", p))
36331 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
36332 break;
36333 case 't':
36334 if (!strcmp ("task_reduction", p))
36335 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
36336 else if (!strcmp ("taskgroup", p))
36337 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
36338 else if (!strcmp ("thread_limit", p))
36339 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
36340 else if (!strcmp ("threads", p))
36341 result = PRAGMA_OMP_CLAUSE_THREADS;
36342 else if (!strcmp ("tile", p))
36343 result = PRAGMA_OACC_CLAUSE_TILE;
36344 else if (!strcmp ("to", p))
36345 result = PRAGMA_OMP_CLAUSE_TO;
36346 break;
36347 case 'u':
36348 if (!strcmp ("uniform", p))
36349 result = PRAGMA_OMP_CLAUSE_UNIFORM;
36350 else if (!strcmp ("untied", p))
36351 result = PRAGMA_OMP_CLAUSE_UNTIED;
36352 else if (!strcmp ("use_device", p))
36353 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
36354 else if (!strcmp ("use_device_addr", p))
36355 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
36356 else if (!strcmp ("use_device_ptr", p))
36357 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
36358 break;
36359 case 'v':
36360 if (!strcmp ("vector", p))
36361 result = PRAGMA_OACC_CLAUSE_VECTOR;
36362 else if (!strcmp ("vector_length", p))
36363 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
36364 break;
36365 case 'w':
36366 if (!strcmp ("wait", p))
36367 result = PRAGMA_OACC_CLAUSE_WAIT;
36368 else if (!strcmp ("worker", p))
36369 result = PRAGMA_OACC_CLAUSE_WORKER;
36370 break;
36374 if (result != PRAGMA_OMP_CLAUSE_NONE)
36375 cp_lexer_consume_token (parser->lexer);
36377 return result;
36380 /* Validate that a clause of the given type does not already exist. */
36382 static void
36383 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
36384 const char *name, location_t location)
36386 if (omp_find_clause (clauses, code))
36387 error_at (location, "too many %qs clauses", name);
36390 /* OpenMP 2.5:
36391 variable-list:
36392 identifier
36393 variable-list , identifier
36395 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
36396 colon). An opening parenthesis will have been consumed by the caller.
36398 If KIND is nonzero, create the appropriate node and install the decl
36399 in OMP_CLAUSE_DECL and add the node to the head of the list.
36401 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
36402 return the list created.
36404 COLON can be NULL if only closing parenthesis should end the list,
36405 or pointer to bool which will receive false if the list is terminated
36406 by closing parenthesis or true if the list is terminated by colon.
36408 The optional ALLOW_DEREF argument is true if list items can use the deref
36409 (->) operator. */
36411 static tree
36412 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
36413 tree list, bool *colon,
36414 bool allow_deref = false)
36416 cp_token *token;
36417 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36418 if (colon)
36420 parser->colon_corrects_to_scope_p = false;
36421 *colon = false;
36423 while (1)
36425 tree name, decl;
36427 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36428 cp_parser_parse_tentatively (parser);
36429 token = cp_lexer_peek_token (parser->lexer);
36430 if (kind != 0
36431 && cp_parser_is_keyword (token, RID_THIS))
36433 decl = finish_this_expr ();
36434 if (TREE_CODE (decl) == NON_LVALUE_EXPR
36435 || CONVERT_EXPR_P (decl))
36436 decl = TREE_OPERAND (decl, 0);
36437 cp_lexer_consume_token (parser->lexer);
36439 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
36440 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
36441 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
36443 cp_id_kind idk;
36444 decl = cp_parser_primary_expression (parser, false, false, false,
36445 &idk);
36447 else
36449 name = cp_parser_id_expression (parser, /*template_p=*/false,
36450 /*check_dependency_p=*/true,
36451 /*template_p=*/NULL,
36452 /*declarator_p=*/false,
36453 /*optional_p=*/false);
36454 if (name == error_mark_node)
36456 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36457 && cp_parser_simulate_error (parser))
36458 goto depend_lvalue;
36459 goto skip_comma;
36462 if (identifier_p (name))
36463 decl = cp_parser_lookup_name_simple (parser, name, token->location);
36464 else
36465 decl = name;
36466 if (decl == error_mark_node)
36468 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36469 && cp_parser_simulate_error (parser))
36470 goto depend_lvalue;
36471 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
36472 token->location);
36475 if (outer_automatic_var_p (decl))
36476 decl = process_outer_var_ref (decl, tf_warning_or_error);
36477 if (decl == error_mark_node)
36479 else if (kind != 0)
36481 switch (kind)
36483 case OMP_CLAUSE__CACHE_:
36484 /* The OpenACC cache directive explicitly only allows "array
36485 elements or subarrays". */
36486 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
36488 error_at (token->location, "expected %<[%>");
36489 decl = error_mark_node;
36490 break;
36492 /* FALLTHROUGH. */
36493 case OMP_CLAUSE_MAP:
36494 case OMP_CLAUSE_FROM:
36495 case OMP_CLAUSE_TO:
36496 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36497 || (allow_deref
36498 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
36500 cpp_ttype ttype
36501 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36502 ? CPP_DOT : CPP_DEREF;
36503 location_t loc
36504 = cp_lexer_peek_token (parser->lexer)->location;
36505 cp_id_kind idk = CP_ID_KIND_NONE;
36506 cp_lexer_consume_token (parser->lexer);
36507 decl = convert_from_reference (decl);
36508 decl
36509 = cp_parser_postfix_dot_deref_expression (parser, ttype,
36510 decl, false,
36511 &idk, loc);
36513 /* FALLTHROUGH. */
36514 case OMP_CLAUSE_AFFINITY:
36515 case OMP_CLAUSE_DEPEND:
36516 case OMP_CLAUSE_REDUCTION:
36517 case OMP_CLAUSE_IN_REDUCTION:
36518 case OMP_CLAUSE_TASK_REDUCTION:
36519 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
36521 tree low_bound = NULL_TREE, length = NULL_TREE;
36523 parser->colon_corrects_to_scope_p = false;
36524 cp_lexer_consume_token (parser->lexer);
36525 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36527 low_bound = cp_parser_expression (parser);
36528 /* Later handling is not prepared to see through these. */
36529 gcc_checking_assert (!location_wrapper_p (low_bound));
36531 if (!colon)
36532 parser->colon_corrects_to_scope_p
36533 = saved_colon_corrects_to_scope_p;
36534 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
36535 length = integer_one_node;
36536 else
36538 /* Look for `:'. */
36539 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36541 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36542 && cp_parser_simulate_error (parser))
36543 goto depend_lvalue;
36544 goto skip_comma;
36546 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36547 cp_parser_commit_to_tentative_parse (parser);
36548 if (!cp_lexer_next_token_is (parser->lexer,
36549 CPP_CLOSE_SQUARE))
36551 length = cp_parser_expression (parser);
36552 /* Later handling is not prepared to see through these. */
36553 gcc_checking_assert (!location_wrapper_p (length));
36556 /* Look for the closing `]'. */
36557 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
36558 RT_CLOSE_SQUARE))
36560 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36561 && cp_parser_simulate_error (parser))
36562 goto depend_lvalue;
36563 goto skip_comma;
36566 decl = tree_cons (low_bound, length, decl);
36568 break;
36569 default:
36570 break;
36573 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
36576 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
36577 && cp_parser_simulate_error (parser))
36579 depend_lvalue:
36580 cp_parser_abort_tentative_parse (parser);
36581 decl = cp_parser_assignment_expression (parser, NULL,
36582 false, false);
36584 else
36585 cp_parser_parse_definitely (parser);
36588 tree u = build_omp_clause (token->location, kind);
36589 OMP_CLAUSE_DECL (u) = decl;
36590 OMP_CLAUSE_CHAIN (u) = list;
36591 list = u;
36593 else
36594 list = tree_cons (decl, NULL_TREE, list);
36596 get_comma:
36597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
36598 break;
36599 cp_lexer_consume_token (parser->lexer);
36602 if (colon)
36603 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36605 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36607 *colon = true;
36608 cp_parser_require (parser, CPP_COLON, RT_COLON);
36609 return list;
36612 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36614 int ending;
36616 /* Try to resync to an unnested comma. Copied from
36617 cp_parser_parenthesized_expression_list. */
36618 skip_comma:
36619 if (colon)
36620 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36621 ending = cp_parser_skip_to_closing_parenthesis (parser,
36622 /*recovering=*/true,
36623 /*or_comma=*/true,
36624 /*consume_paren=*/true);
36625 if (ending < 0)
36626 goto get_comma;
36629 return list;
36632 /* Similarly, but expect leading and trailing parenthesis. This is a very
36633 common case for omp clauses. */
36635 static tree
36636 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
36637 bool allow_deref = false)
36639 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36640 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
36641 allow_deref);
36642 return list;
36645 /* OpenACC 2.0:
36646 copy ( variable-list )
36647 copyin ( variable-list )
36648 copyout ( variable-list )
36649 create ( variable-list )
36650 delete ( variable-list )
36651 present ( variable-list )
36653 OpenACC 2.6:
36654 no_create ( variable-list )
36655 attach ( variable-list )
36656 detach ( variable-list ) */
36658 static tree
36659 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
36660 tree list)
36662 enum gomp_map_kind kind;
36663 switch (c_kind)
36665 case PRAGMA_OACC_CLAUSE_ATTACH:
36666 kind = GOMP_MAP_ATTACH;
36667 break;
36668 case PRAGMA_OACC_CLAUSE_COPY:
36669 kind = GOMP_MAP_TOFROM;
36670 break;
36671 case PRAGMA_OACC_CLAUSE_COPYIN:
36672 kind = GOMP_MAP_TO;
36673 break;
36674 case PRAGMA_OACC_CLAUSE_COPYOUT:
36675 kind = GOMP_MAP_FROM;
36676 break;
36677 case PRAGMA_OACC_CLAUSE_CREATE:
36678 kind = GOMP_MAP_ALLOC;
36679 break;
36680 case PRAGMA_OACC_CLAUSE_DELETE:
36681 kind = GOMP_MAP_RELEASE;
36682 break;
36683 case PRAGMA_OACC_CLAUSE_DETACH:
36684 kind = GOMP_MAP_DETACH;
36685 break;
36686 case PRAGMA_OACC_CLAUSE_DEVICE:
36687 kind = GOMP_MAP_FORCE_TO;
36688 break;
36689 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
36690 kind = GOMP_MAP_DEVICE_RESIDENT;
36691 break;
36692 case PRAGMA_OACC_CLAUSE_HOST:
36693 kind = GOMP_MAP_FORCE_FROM;
36694 break;
36695 case PRAGMA_OACC_CLAUSE_LINK:
36696 kind = GOMP_MAP_LINK;
36697 break;
36698 case PRAGMA_OACC_CLAUSE_NO_CREATE:
36699 kind = GOMP_MAP_IF_PRESENT;
36700 break;
36701 case PRAGMA_OACC_CLAUSE_PRESENT:
36702 kind = GOMP_MAP_FORCE_PRESENT;
36703 break;
36704 default:
36705 gcc_unreachable ();
36707 tree nl, c;
36708 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
36710 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
36711 OMP_CLAUSE_SET_MAP_KIND (c, kind);
36713 return nl;
36716 /* OpenACC 2.0:
36717 deviceptr ( variable-list ) */
36719 static tree
36720 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
36722 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36723 tree vars, t;
36725 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
36726 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
36727 variable-list must only allow for pointer variables. */
36728 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36729 for (t = vars; t; t = TREE_CHAIN (t))
36731 tree v = TREE_PURPOSE (t);
36732 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
36733 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
36734 OMP_CLAUSE_DECL (u) = v;
36735 OMP_CLAUSE_CHAIN (u) = list;
36736 list = u;
36739 return list;
36742 /* OpenACC 2.5:
36743 auto
36744 finalize
36745 independent
36746 nohost
36747 seq */
36749 static tree
36750 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
36751 tree list)
36753 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
36755 tree c = build_omp_clause (loc, code);
36756 OMP_CLAUSE_CHAIN (c) = list;
36758 return c;
36761 /* OpenACC:
36762 num_gangs ( expression )
36763 num_workers ( expression )
36764 vector_length ( expression ) */
36766 static tree
36767 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
36768 const char *str, tree list)
36770 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36772 matching_parens parens;
36773 if (!parens.require_open (parser))
36774 return list;
36776 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
36778 if (t == error_mark_node
36779 || !parens.require_close (parser))
36781 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36782 /*or_comma=*/false,
36783 /*consume_paren=*/true);
36784 return list;
36787 check_no_duplicate_clause (list, code, str, loc);
36789 tree c = build_omp_clause (loc, code);
36790 OMP_CLAUSE_OPERAND (c, 0) = t;
36791 OMP_CLAUSE_CHAIN (c) = list;
36792 return c;
36795 /* OpenACC:
36797 gang [( gang-arg-list )]
36798 worker [( [num:] int-expr )]
36799 vector [( [length:] int-expr )]
36801 where gang-arg is one of:
36803 [num:] int-expr
36804 static: size-expr
36806 and size-expr may be:
36809 int-expr
36812 static tree
36813 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
36814 omp_clause_code kind,
36815 const char *str, tree list)
36817 const char *id = "num";
36818 cp_lexer *lexer = parser->lexer;
36819 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
36821 if (kind == OMP_CLAUSE_VECTOR)
36822 id = "length";
36824 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
36826 matching_parens parens;
36827 parens.consume_open (parser);
36831 cp_token *next = cp_lexer_peek_token (lexer);
36832 int idx = 0;
36834 /* Gang static argument. */
36835 if (kind == OMP_CLAUSE_GANG
36836 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
36838 cp_lexer_consume_token (lexer);
36840 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36841 goto cleanup_error;
36843 idx = 1;
36844 if (ops[idx] != NULL)
36846 cp_parser_error (parser, "too many %<static%> arguments");
36847 goto cleanup_error;
36850 /* Check for the '*' argument. */
36851 if (cp_lexer_next_token_is (lexer, CPP_MULT)
36852 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
36853 || cp_lexer_nth_token_is (parser->lexer, 2,
36854 CPP_CLOSE_PAREN)))
36856 cp_lexer_consume_token (lexer);
36857 ops[idx] = integer_minus_one_node;
36859 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
36861 cp_lexer_consume_token (lexer);
36862 continue;
36864 else break;
36867 /* Worker num: argument and vector length: arguments. */
36868 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
36869 && id_equal (next->u.value, id)
36870 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
36872 cp_lexer_consume_token (lexer); /* id */
36873 cp_lexer_consume_token (lexer); /* ':' */
36876 /* Now collect the actual argument. */
36877 if (ops[idx] != NULL_TREE)
36879 cp_parser_error (parser, "unexpected argument");
36880 goto cleanup_error;
36883 tree expr = cp_parser_assignment_expression (parser, NULL, false,
36884 false);
36885 if (expr == error_mark_node)
36886 goto cleanup_error;
36888 mark_exp_read (expr);
36889 ops[idx] = expr;
36891 if (kind == OMP_CLAUSE_GANG
36892 && cp_lexer_next_token_is (lexer, CPP_COMMA))
36894 cp_lexer_consume_token (lexer);
36895 continue;
36897 break;
36899 while (1);
36901 if (!parens.require_close (parser))
36902 goto cleanup_error;
36905 check_no_duplicate_clause (list, kind, str, loc);
36907 c = build_omp_clause (loc, kind);
36909 if (ops[1])
36910 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
36912 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
36913 OMP_CLAUSE_CHAIN (c) = list;
36915 return c;
36917 cleanup_error:
36918 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
36919 return list;
36922 /* OpenACC 2.0:
36923 tile ( size-expr-list ) */
36925 static tree
36926 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
36928 tree c, expr = error_mark_node;
36929 tree tile = NULL_TREE;
36931 /* Collapse and tile are mutually exclusive. (The spec doesn't say
36932 so, but the spec authors never considered such a case and have
36933 differing opinions on what it might mean, including 'not
36934 allowed'.) */
36935 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
36936 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
36937 clause_loc);
36939 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36940 return list;
36944 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
36945 return list;
36947 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
36948 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
36949 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
36951 cp_lexer_consume_token (parser->lexer);
36952 expr = integer_zero_node;
36954 else
36955 expr = cp_parser_constant_expression (parser);
36957 tile = tree_cons (NULL_TREE, expr, tile);
36959 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
36961 /* Consume the trailing ')'. */
36962 cp_lexer_consume_token (parser->lexer);
36964 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
36965 tile = nreverse (tile);
36966 OMP_CLAUSE_TILE_LIST (c) = tile;
36967 OMP_CLAUSE_CHAIN (c) = list;
36968 return c;
36971 /* OpenACC 2.0
36972 Parse wait clause or directive parameters. */
36974 static tree
36975 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
36977 vec<tree, va_gc> *args;
36978 tree t, args_tree;
36980 args = cp_parser_parenthesized_expression_list (parser, non_attr,
36981 /*cast_p=*/false,
36982 /*allow_expansion_p=*/true,
36983 /*non_constant_p=*/NULL);
36985 if (args == NULL || args->length () == 0)
36987 if (args != NULL)
36989 cp_parser_error (parser, "expected integer expression list");
36990 release_tree_vector (args);
36992 return list;
36995 args_tree = build_tree_list_vec (args);
36997 release_tree_vector (args);
36999 for (t = args_tree; t; t = TREE_CHAIN (t))
37001 tree targ = TREE_VALUE (t);
37003 if (targ != error_mark_node)
37005 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
37006 error ("%<wait%> expression must be integral");
37007 else
37009 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
37011 targ = mark_rvalue_use (targ);
37012 OMP_CLAUSE_DECL (c) = targ;
37013 OMP_CLAUSE_CHAIN (c) = list;
37014 list = c;
37019 return list;
37022 /* OpenACC:
37023 wait [( int-expr-list )] */
37025 static tree
37026 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
37028 location_t location = cp_lexer_peek_token (parser->lexer)->location;
37030 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37031 list = cp_parser_oacc_wait_list (parser, location, list);
37032 else
37034 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
37036 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37037 OMP_CLAUSE_CHAIN (c) = list;
37038 list = c;
37041 return list;
37044 /* OpenMP 3.0:
37045 collapse ( constant-expression ) */
37047 static tree
37048 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
37050 tree c, num;
37051 location_t loc;
37052 HOST_WIDE_INT n;
37054 loc = cp_lexer_peek_token (parser->lexer)->location;
37055 matching_parens parens;
37056 if (!parens.require_open (parser))
37057 return list;
37059 num = cp_parser_constant_expression (parser);
37061 if (!parens.require_close (parser))
37062 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37063 /*or_comma=*/false,
37064 /*consume_paren=*/true);
37066 if (num == error_mark_node)
37067 return list;
37068 num = fold_non_dependent_expr (num);
37069 if (!tree_fits_shwi_p (num)
37070 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
37071 || (n = tree_to_shwi (num)) <= 0
37072 || (int) n != n)
37074 error_at (loc, "collapse argument needs positive constant integer expression");
37075 return list;
37078 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
37079 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
37080 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
37081 OMP_CLAUSE_CHAIN (c) = list;
37082 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
37084 return c;
37087 /* OpenMP 2.5:
37088 default ( none | shared )
37090 OpenMP 5.1:
37091 default ( private | firstprivate )
37093 OpenACC:
37094 default ( none | present ) */
37096 static tree
37097 cp_parser_omp_clause_default (cp_parser *parser, tree list,
37098 location_t location, bool is_oacc)
37100 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
37101 tree c;
37103 matching_parens parens;
37104 if (!parens.require_open (parser))
37105 return list;
37106 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37108 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
37109 cp_lexer_consume_token (parser->lexer);
37111 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37113 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37114 const char *p = IDENTIFIER_POINTER (id);
37116 switch (p[0])
37118 case 'n':
37119 if (strcmp ("none", p) != 0)
37120 goto invalid_kind;
37121 kind = OMP_CLAUSE_DEFAULT_NONE;
37122 break;
37124 case 'p':
37125 if (strcmp ("present", p) != 0 || !is_oacc)
37126 goto invalid_kind;
37127 kind = OMP_CLAUSE_DEFAULT_PRESENT;
37128 break;
37130 case 'f':
37131 if (strcmp ("firstprivate", p) != 0 || is_oacc)
37132 goto invalid_kind;
37133 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
37134 break;
37136 case 's':
37137 if (strcmp ("shared", p) != 0 || is_oacc)
37138 goto invalid_kind;
37139 kind = OMP_CLAUSE_DEFAULT_SHARED;
37140 break;
37142 default:
37143 goto invalid_kind;
37146 cp_lexer_consume_token (parser->lexer);
37148 else
37150 invalid_kind:
37151 if (is_oacc)
37152 cp_parser_error (parser, "expected %<none%> or %<present%>");
37153 else
37154 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
37155 "%<private%> or %<firstprivate%>");
37158 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
37159 || !parens.require_close (parser))
37160 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37161 /*or_comma=*/false,
37162 /*consume_paren=*/true);
37164 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
37165 return list;
37167 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
37168 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
37169 OMP_CLAUSE_CHAIN (c) = list;
37170 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
37172 return c;
37175 /* OpenMP 3.1:
37176 final ( expression ) */
37178 static tree
37179 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
37181 tree t, c;
37183 matching_parens parens;
37184 if (!parens.require_open (parser))
37185 return list;
37187 t = cp_parser_assignment_expression (parser);
37189 if (t == error_mark_node
37190 || !parens.require_close (parser))
37191 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37192 /*or_comma=*/false,
37193 /*consume_paren=*/true);
37195 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
37197 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
37198 OMP_CLAUSE_FINAL_EXPR (c) = t;
37199 OMP_CLAUSE_CHAIN (c) = list;
37201 return c;
37204 /* OpenMP 2.5:
37205 if ( expression )
37207 OpenMP 4.5:
37208 if ( directive-name-modifier : expression )
37210 directive-name-modifier:
37211 parallel | task | taskloop | target data | target | target update
37212 | target enter data | target exit data
37214 OpenMP 5.0:
37215 directive-name-modifier:
37216 ... | simd | cancel */
37218 static tree
37219 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
37220 bool is_omp)
37222 tree t, c;
37223 enum tree_code if_modifier = ERROR_MARK;
37225 matching_parens parens;
37226 if (!parens.require_open (parser))
37227 return list;
37229 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37231 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37232 const char *p = IDENTIFIER_POINTER (id);
37233 int n = 2;
37235 if (strcmp ("cancel", p) == 0)
37236 if_modifier = VOID_CST;
37237 else if (strcmp ("parallel", p) == 0)
37238 if_modifier = OMP_PARALLEL;
37239 else if (strcmp ("simd", p) == 0)
37240 if_modifier = OMP_SIMD;
37241 else if (strcmp ("task", p) == 0)
37242 if_modifier = OMP_TASK;
37243 else if (strcmp ("taskloop", p) == 0)
37244 if_modifier = OMP_TASKLOOP;
37245 else if (strcmp ("target", p) == 0)
37247 if_modifier = OMP_TARGET;
37248 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
37250 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
37251 p = IDENTIFIER_POINTER (id);
37252 if (strcmp ("data", p) == 0)
37253 if_modifier = OMP_TARGET_DATA;
37254 else if (strcmp ("update", p) == 0)
37255 if_modifier = OMP_TARGET_UPDATE;
37256 else if (strcmp ("enter", p) == 0)
37257 if_modifier = OMP_TARGET_ENTER_DATA;
37258 else if (strcmp ("exit", p) == 0)
37259 if_modifier = OMP_TARGET_EXIT_DATA;
37260 if (if_modifier != OMP_TARGET)
37261 n = 3;
37262 else
37264 location_t loc
37265 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
37266 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
37267 "or %<exit%>");
37268 if_modifier = ERROR_MARK;
37270 if (if_modifier == OMP_TARGET_ENTER_DATA
37271 || if_modifier == OMP_TARGET_EXIT_DATA)
37273 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
37275 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
37276 p = IDENTIFIER_POINTER (id);
37277 if (strcmp ("data", p) == 0)
37278 n = 4;
37280 if (n != 4)
37282 location_t loc
37283 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
37284 error_at (loc, "expected %<data%>");
37285 if_modifier = ERROR_MARK;
37290 if (if_modifier != ERROR_MARK)
37292 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
37294 while (n-- > 0)
37295 cp_lexer_consume_token (parser->lexer);
37297 else
37299 if (n > 2)
37301 location_t loc
37302 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
37303 error_at (loc, "expected %<:%>");
37305 if_modifier = ERROR_MARK;
37310 t = cp_parser_assignment_expression (parser);
37312 if (t == error_mark_node
37313 || !parens.require_close (parser))
37314 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37315 /*or_comma=*/false,
37316 /*consume_paren=*/true);
37318 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
37319 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
37321 if (if_modifier != ERROR_MARK
37322 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37324 const char *p = NULL;
37325 switch (if_modifier)
37327 case VOID_CST: p = "cancel"; break;
37328 case OMP_PARALLEL: p = "parallel"; break;
37329 case OMP_SIMD: p = "simd"; break;
37330 case OMP_TASK: p = "task"; break;
37331 case OMP_TASKLOOP: p = "taskloop"; break;
37332 case OMP_TARGET_DATA: p = "target data"; break;
37333 case OMP_TARGET: p = "target"; break;
37334 case OMP_TARGET_UPDATE: p = "target update"; break;
37335 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
37336 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
37337 default: gcc_unreachable ();
37339 error_at (location, "too many %<if%> clauses with %qs modifier",
37341 return list;
37343 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37345 if (!is_omp)
37346 error_at (location, "too many %<if%> clauses");
37347 else
37348 error_at (location, "too many %<if%> clauses without modifier");
37349 return list;
37351 else if (if_modifier == ERROR_MARK
37352 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
37354 error_at (location, "if any %<if%> clause has modifier, then all "
37355 "%<if%> clauses have to use modifier");
37356 return list;
37360 c = build_omp_clause (location, OMP_CLAUSE_IF);
37361 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
37362 OMP_CLAUSE_IF_EXPR (c) = t;
37363 OMP_CLAUSE_CHAIN (c) = list;
37365 return c;
37368 /* OpenMP 3.1:
37369 mergeable */
37371 static tree
37372 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
37373 tree list, location_t location)
37375 tree c;
37377 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
37378 location);
37380 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
37381 OMP_CLAUSE_CHAIN (c) = list;
37382 return c;
37385 /* OpenMP 2.5:
37386 nowait */
37388 static tree
37389 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
37390 tree list, location_t location)
37392 tree c;
37394 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
37396 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
37397 OMP_CLAUSE_CHAIN (c) = list;
37398 return c;
37401 /* OpenMP 2.5:
37402 num_threads ( expression ) */
37404 static tree
37405 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
37406 location_t location)
37408 tree t, c;
37410 matching_parens parens;
37411 if (!parens.require_open (parser))
37412 return list;
37414 t = cp_parser_assignment_expression (parser);
37416 if (t == error_mark_node
37417 || !parens.require_close (parser))
37418 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37419 /*or_comma=*/false,
37420 /*consume_paren=*/true);
37422 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
37423 "num_threads", location);
37425 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
37426 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
37427 OMP_CLAUSE_CHAIN (c) = list;
37429 return c;
37432 /* OpenMP 4.5:
37433 num_tasks ( expression )
37435 OpenMP 5.1:
37436 num_tasks ( strict : expression ) */
37438 static tree
37439 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
37440 location_t location)
37442 tree t, c;
37444 matching_parens parens;
37445 if (!parens.require_open (parser))
37446 return list;
37448 bool strict = false;
37449 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37450 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37452 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37453 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37455 strict = true;
37456 cp_lexer_consume_token (parser->lexer);
37457 cp_lexer_consume_token (parser->lexer);
37461 t = cp_parser_assignment_expression (parser);
37463 if (t == error_mark_node
37464 || !parens.require_close (parser))
37465 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37466 /*or_comma=*/false,
37467 /*consume_paren=*/true);
37469 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
37470 "num_tasks", location);
37472 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
37473 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
37474 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
37475 OMP_CLAUSE_CHAIN (c) = list;
37477 return c;
37480 /* OpenMP 4.5:
37481 grainsize ( expression )
37483 OpenMP 5.1:
37484 grainsize ( strict : expression ) */
37486 static tree
37487 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
37488 location_t location)
37490 tree t, c;
37492 matching_parens parens;
37493 if (!parens.require_open (parser))
37494 return list;
37496 bool strict = false;
37497 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37498 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37500 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37501 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37503 strict = true;
37504 cp_lexer_consume_token (parser->lexer);
37505 cp_lexer_consume_token (parser->lexer);
37509 t = cp_parser_assignment_expression (parser);
37511 if (t == error_mark_node
37512 || !parens.require_close (parser))
37513 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37514 /*or_comma=*/false,
37515 /*consume_paren=*/true);
37517 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
37518 "grainsize", location);
37520 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
37521 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
37522 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
37523 OMP_CLAUSE_CHAIN (c) = list;
37525 return c;
37528 /* OpenMP 4.5:
37529 priority ( expression ) */
37531 static tree
37532 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
37533 location_t location)
37535 tree t, c;
37537 matching_parens parens;
37538 if (!parens.require_open (parser))
37539 return list;
37541 t = cp_parser_assignment_expression (parser);
37543 if (t == error_mark_node
37544 || !parens.require_close (parser))
37545 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37546 /*or_comma=*/false,
37547 /*consume_paren=*/true);
37549 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
37550 "priority", location);
37552 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
37553 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
37554 OMP_CLAUSE_CHAIN (c) = list;
37556 return c;
37559 /* OpenMP 4.5:
37560 hint ( expression ) */
37562 static tree
37563 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
37565 tree t, c;
37567 matching_parens parens;
37568 if (!parens.require_open (parser))
37569 return list;
37571 t = cp_parser_assignment_expression (parser);
37573 if (t != error_mark_node)
37575 t = fold_non_dependent_expr (t);
37576 if (!value_dependent_expression_p (t)
37577 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
37578 || !tree_fits_shwi_p (t)
37579 || tree_int_cst_sgn (t) == -1))
37580 error_at (location, "expected constant integer expression with "
37581 "valid sync-hint value");
37583 if (t == error_mark_node
37584 || !parens.require_close (parser))
37585 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37586 /*or_comma=*/false,
37587 /*consume_paren=*/true);
37588 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
37590 c = build_omp_clause (location, OMP_CLAUSE_HINT);
37591 OMP_CLAUSE_HINT_EXPR (c) = t;
37592 OMP_CLAUSE_CHAIN (c) = list;
37594 return c;
37597 /* OpenMP 5.1:
37598 filter ( integer-expression ) */
37600 static tree
37601 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
37603 tree t, c;
37605 matching_parens parens;
37606 if (!parens.require_open (parser))
37607 return list;
37609 t = cp_parser_assignment_expression (parser);
37611 if (t == error_mark_node
37612 || !parens.require_close (parser))
37613 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37614 /*or_comma=*/false,
37615 /*consume_paren=*/true);
37616 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
37618 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
37619 OMP_CLAUSE_FILTER_EXPR (c) = t;
37620 OMP_CLAUSE_CHAIN (c) = list;
37622 return c;
37625 /* OpenMP 4.5:
37626 defaultmap ( tofrom : scalar )
37628 OpenMP 5.0:
37629 defaultmap ( implicit-behavior [ : variable-category ] ) */
37631 static tree
37632 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
37633 location_t location)
37635 tree c, id;
37636 const char *p;
37637 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
37638 enum omp_clause_defaultmap_kind category
37639 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
37641 matching_parens parens;
37642 if (!parens.require_open (parser))
37643 return list;
37645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37646 p = "default";
37647 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37649 invalid_behavior:
37650 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
37651 "%<tofrom%>, %<firstprivate%>, %<none%> "
37652 "or %<default%>");
37653 goto out_err;
37655 else
37657 id = cp_lexer_peek_token (parser->lexer)->u.value;
37658 p = IDENTIFIER_POINTER (id);
37661 switch (p[0])
37663 case 'a':
37664 if (strcmp ("alloc", p) == 0)
37665 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
37666 else
37667 goto invalid_behavior;
37668 break;
37670 case 'd':
37671 if (strcmp ("default", p) == 0)
37672 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
37673 else
37674 goto invalid_behavior;
37675 break;
37677 case 'f':
37678 if (strcmp ("firstprivate", p) == 0)
37679 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
37680 else if (strcmp ("from", p) == 0)
37681 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
37682 else
37683 goto invalid_behavior;
37684 break;
37686 case 'n':
37687 if (strcmp ("none", p) == 0)
37688 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
37689 else
37690 goto invalid_behavior;
37691 break;
37693 case 't':
37694 if (strcmp ("tofrom", p) == 0)
37695 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
37696 else if (strcmp ("to", p) == 0)
37697 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
37698 else
37699 goto invalid_behavior;
37700 break;
37702 default:
37703 goto invalid_behavior;
37705 cp_lexer_consume_token (parser->lexer);
37707 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37709 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37710 goto out_err;
37712 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37714 invalid_category:
37715 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
37716 "%<pointer%>");
37717 goto out_err;
37719 id = cp_lexer_peek_token (parser->lexer)->u.value;
37720 p = IDENTIFIER_POINTER (id);
37722 switch (p[0])
37724 case 'a':
37725 if (strcmp ("aggregate", p) == 0)
37726 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
37727 else
37728 goto invalid_category;
37729 break;
37731 case 'p':
37732 if (strcmp ("pointer", p) == 0)
37733 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
37734 else
37735 goto invalid_category;
37736 break;
37738 case 's':
37739 if (strcmp ("scalar", p) == 0)
37740 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
37741 else
37742 goto invalid_category;
37743 break;
37745 default:
37746 goto invalid_category;
37749 cp_lexer_consume_token (parser->lexer);
37751 if (!parens.require_close (parser))
37752 goto out_err;
37754 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
37755 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
37756 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
37757 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
37758 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
37759 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
37761 enum omp_clause_defaultmap_kind cat = category;
37762 location_t loc = OMP_CLAUSE_LOCATION (c);
37763 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
37764 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
37765 p = NULL;
37766 switch (cat)
37768 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
37769 p = NULL;
37770 break;
37771 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
37772 p = "aggregate";
37773 break;
37774 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
37775 p = "pointer";
37776 break;
37777 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
37778 p = "scalar";
37779 break;
37780 default:
37781 gcc_unreachable ();
37783 if (p)
37784 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
37786 else
37787 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
37788 "category");
37789 break;
37792 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
37793 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
37794 OMP_CLAUSE_CHAIN (c) = list;
37795 return c;
37797 out_err:
37798 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37799 /*or_comma=*/false,
37800 /*consume_paren=*/true);
37801 return list;
37804 /* OpenMP 5.0:
37805 order ( concurrent )
37807 OpenMP 5.1:
37808 order ( order-modifier : concurrent )
37810 order-modifier:
37811 reproducible
37812 unconstrained */
37814 static tree
37815 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
37817 tree c, id;
37818 const char *p;
37819 bool unconstrained = false;
37820 bool reproducible = false;
37822 matching_parens parens;
37823 if (!parens.require_open (parser))
37824 return list;
37826 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37827 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37829 id = cp_lexer_peek_token (parser->lexer)->u.value;
37830 p = IDENTIFIER_POINTER (id);
37831 if (strcmp (p, "unconstrained") == 0)
37832 unconstrained = true;
37833 else if (strcmp (p, "reproducible") == 0)
37834 reproducible = true;
37835 else
37837 cp_parser_error (parser, "expected %<reproducible%> or "
37838 "%<unconstrained%>");
37839 goto out_err;
37841 cp_lexer_consume_token (parser->lexer);
37842 cp_lexer_consume_token (parser->lexer);
37844 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37846 cp_parser_error (parser, "expected %<concurrent%>");
37847 goto out_err;
37849 else
37851 id = cp_lexer_peek_token (parser->lexer)->u.value;
37852 p = IDENTIFIER_POINTER (id);
37854 if (strcmp (p, "concurrent") != 0)
37856 cp_parser_error (parser, "expected %<concurrent%>");
37857 goto out_err;
37859 cp_lexer_consume_token (parser->lexer);
37860 if (!parens.require_close (parser))
37861 goto out_err;
37863 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
37864 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
37865 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
37866 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
37867 OMP_CLAUSE_CHAIN (c) = list;
37868 return c;
37870 out_err:
37871 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37872 /*or_comma=*/false,
37873 /*consume_paren=*/true);
37874 return list;
37877 /* OpenMP 5.0:
37878 bind ( teams | parallel | thread ) */
37880 static tree
37881 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
37882 location_t location)
37884 tree c;
37885 const char *p;
37886 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
37888 matching_parens parens;
37889 if (!parens.require_open (parser))
37890 return list;
37892 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37894 invalid:
37895 cp_parser_error (parser,
37896 "expected %<teams%>, %<parallel%> or %<thread%>");
37897 goto out_err;
37899 else
37901 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37902 p = IDENTIFIER_POINTER (id);
37904 if (strcmp (p, "teams") == 0)
37905 kind = OMP_CLAUSE_BIND_TEAMS;
37906 else if (strcmp (p, "parallel") == 0)
37907 kind = OMP_CLAUSE_BIND_PARALLEL;
37908 else if (strcmp (p, "thread") != 0)
37909 goto invalid;
37910 cp_lexer_consume_token (parser->lexer);
37911 if (!parens.require_close (parser))
37912 goto out_err;
37914 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
37915 c = build_omp_clause (location, OMP_CLAUSE_BIND);
37916 OMP_CLAUSE_BIND_KIND (c) = kind;
37917 OMP_CLAUSE_CHAIN (c) = list;
37918 return c;
37920 out_err:
37921 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37922 /*or_comma=*/false,
37923 /*consume_paren=*/true);
37924 return list;
37927 /* OpenMP 2.5:
37928 ordered
37930 OpenMP 4.5:
37931 ordered ( constant-expression ) */
37933 static tree
37934 cp_parser_omp_clause_ordered (cp_parser *parser,
37935 tree list, location_t location)
37937 tree c, num = NULL_TREE;
37938 HOST_WIDE_INT n;
37940 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
37941 "ordered", location);
37943 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37945 matching_parens parens;
37946 parens.consume_open (parser);
37948 num = cp_parser_constant_expression (parser);
37950 if (!parens.require_close (parser))
37951 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37952 /*or_comma=*/false,
37953 /*consume_paren=*/true);
37955 if (num == error_mark_node)
37956 return list;
37957 num = fold_non_dependent_expr (num);
37958 if (!tree_fits_shwi_p (num)
37959 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
37960 || (n = tree_to_shwi (num)) <= 0
37961 || (int) n != n)
37963 error_at (location,
37964 "ordered argument needs positive constant integer "
37965 "expression");
37966 return list;
37970 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
37971 OMP_CLAUSE_ORDERED_EXPR (c) = num;
37972 OMP_CLAUSE_CHAIN (c) = list;
37973 return c;
37976 /* OpenMP 2.5:
37977 reduction ( reduction-operator : variable-list )
37979 reduction-operator:
37980 One of: + * - & ^ | && ||
37982 OpenMP 3.1:
37984 reduction-operator:
37985 One of: + * - & ^ | && || min max
37987 OpenMP 4.0:
37989 reduction-operator:
37990 One of: + * - & ^ | && ||
37991 id-expression
37993 OpenMP 5.0:
37994 reduction ( reduction-modifier, reduction-operator : variable-list )
37995 in_reduction ( reduction-operator : variable-list )
37996 task_reduction ( reduction-operator : variable-list ) */
37998 static tree
37999 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
38000 bool is_omp, tree list)
38002 enum tree_code code = ERROR_MARK;
38003 tree nlist, c, id = NULL_TREE;
38004 bool task = false;
38005 bool inscan = false;
38007 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38008 return list;
38010 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
38012 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
38013 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38015 cp_lexer_consume_token (parser->lexer);
38016 cp_lexer_consume_token (parser->lexer);
38018 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38019 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38021 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38022 const char *p = IDENTIFIER_POINTER (id);
38023 if (strcmp (p, "task") == 0)
38024 task = true;
38025 else if (strcmp (p, "inscan") == 0)
38026 inscan = true;
38027 if (task || inscan)
38029 cp_lexer_consume_token (parser->lexer);
38030 cp_lexer_consume_token (parser->lexer);
38035 switch (cp_lexer_peek_token (parser->lexer)->type)
38037 case CPP_PLUS: code = PLUS_EXPR; break;
38038 case CPP_MULT: code = MULT_EXPR; break;
38039 case CPP_MINUS: code = MINUS_EXPR; break;
38040 case CPP_AND: code = BIT_AND_EXPR; break;
38041 case CPP_XOR: code = BIT_XOR_EXPR; break;
38042 case CPP_OR: code = BIT_IOR_EXPR; break;
38043 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
38044 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
38045 default: break;
38048 if (code != ERROR_MARK)
38049 cp_lexer_consume_token (parser->lexer);
38050 else
38052 bool saved_colon_corrects_to_scope_p;
38053 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38054 parser->colon_corrects_to_scope_p = false;
38055 id = cp_parser_id_expression (parser, /*template_p=*/false,
38056 /*check_dependency_p=*/true,
38057 /*template_p=*/NULL,
38058 /*declarator_p=*/false,
38059 /*optional_p=*/false);
38060 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38061 if (identifier_p (id))
38063 const char *p = IDENTIFIER_POINTER (id);
38065 if (strcmp (p, "min") == 0)
38066 code = MIN_EXPR;
38067 else if (strcmp (p, "max") == 0)
38068 code = MAX_EXPR;
38069 else if (id == ovl_op_identifier (false, PLUS_EXPR))
38070 code = PLUS_EXPR;
38071 else if (id == ovl_op_identifier (false, MULT_EXPR))
38072 code = MULT_EXPR;
38073 else if (id == ovl_op_identifier (false, MINUS_EXPR))
38074 code = MINUS_EXPR;
38075 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
38076 code = BIT_AND_EXPR;
38077 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
38078 code = BIT_IOR_EXPR;
38079 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
38080 code = BIT_XOR_EXPR;
38081 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
38082 code = TRUTH_ANDIF_EXPR;
38083 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
38084 code = TRUTH_ORIF_EXPR;
38085 id = omp_reduction_id (code, id, NULL_TREE);
38086 tree scope = parser->scope;
38087 if (scope)
38088 id = build_qualified_name (NULL_TREE, scope, id, false);
38089 parser->scope = NULL_TREE;
38090 parser->qualifying_scope = NULL_TREE;
38091 parser->object_scope = NULL_TREE;
38093 else
38095 error ("invalid reduction-identifier");
38096 resync_fail:
38097 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38098 /*or_comma=*/false,
38099 /*consume_paren=*/true);
38100 return list;
38104 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38105 goto resync_fail;
38107 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
38108 NULL);
38109 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38111 OMP_CLAUSE_REDUCTION_CODE (c) = code;
38112 if (task)
38113 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
38114 else if (inscan)
38115 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
38116 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
38119 return nlist;
38122 /* OpenMP 2.5:
38123 schedule ( schedule-kind )
38124 schedule ( schedule-kind , expression )
38126 schedule-kind:
38127 static | dynamic | guided | runtime | auto
38129 OpenMP 4.5:
38130 schedule ( schedule-modifier : schedule-kind )
38131 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
38133 schedule-modifier:
38134 simd
38135 monotonic
38136 nonmonotonic */
38138 static tree
38139 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
38141 tree c, t;
38142 int modifiers = 0, nmodifiers = 0;
38144 matching_parens parens;
38145 if (!parens.require_open (parser))
38146 return list;
38148 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
38150 location_t comma = UNKNOWN_LOCATION;
38151 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38153 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38154 const char *p = IDENTIFIER_POINTER (id);
38155 if (strcmp ("simd", p) == 0)
38156 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
38157 else if (strcmp ("monotonic", p) == 0)
38158 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
38159 else if (strcmp ("nonmonotonic", p) == 0)
38160 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
38161 else
38162 break;
38163 comma = UNKNOWN_LOCATION;
38164 cp_lexer_consume_token (parser->lexer);
38165 if (nmodifiers++ == 0
38166 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38168 comma = cp_lexer_peek_token (parser->lexer)->location;
38169 cp_lexer_consume_token (parser->lexer);
38171 else
38173 cp_parser_require (parser, CPP_COLON, RT_COLON);
38174 break;
38177 if (comma != UNKNOWN_LOCATION)
38178 error_at (comma, "expected %<:%>");
38180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38182 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38183 const char *p = IDENTIFIER_POINTER (id);
38185 switch (p[0])
38187 case 'd':
38188 if (strcmp ("dynamic", p) != 0)
38189 goto invalid_kind;
38190 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
38191 break;
38193 case 'g':
38194 if (strcmp ("guided", p) != 0)
38195 goto invalid_kind;
38196 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
38197 break;
38199 case 'r':
38200 if (strcmp ("runtime", p) != 0)
38201 goto invalid_kind;
38202 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
38203 break;
38205 default:
38206 goto invalid_kind;
38209 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
38210 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
38211 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
38212 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
38213 else
38214 goto invalid_kind;
38215 cp_lexer_consume_token (parser->lexer);
38217 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
38218 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38219 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
38220 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38222 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
38223 "specified");
38224 modifiers = 0;
38227 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38229 cp_token *token;
38230 cp_lexer_consume_token (parser->lexer);
38232 token = cp_lexer_peek_token (parser->lexer);
38233 t = cp_parser_assignment_expression (parser);
38235 if (t == error_mark_node)
38236 goto resync_fail;
38237 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
38238 error_at (token->location, "schedule %<runtime%> does not take "
38239 "a %<chunk_size%> parameter");
38240 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
38241 error_at (token->location, "schedule %<auto%> does not take "
38242 "a %<chunk_size%> parameter");
38243 else
38244 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
38246 if (!parens.require_close (parser))
38247 goto resync_fail;
38249 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
38250 goto resync_fail;
38252 OMP_CLAUSE_SCHEDULE_KIND (c)
38253 = (enum omp_clause_schedule_kind)
38254 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
38256 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
38257 OMP_CLAUSE_CHAIN (c) = list;
38258 return c;
38260 invalid_kind:
38261 cp_parser_error (parser, "invalid schedule kind");
38262 resync_fail:
38263 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38264 /*or_comma=*/false,
38265 /*consume_paren=*/true);
38266 return list;
38269 /* OpenMP 3.0:
38270 untied */
38272 static tree
38273 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
38274 tree list, location_t location)
38276 tree c;
38278 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
38280 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
38281 OMP_CLAUSE_CHAIN (c) = list;
38282 return c;
38285 /* OpenMP 4.0:
38286 inbranch
38287 notinbranch */
38289 static tree
38290 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
38291 tree list, location_t location)
38293 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38294 tree c = build_omp_clause (location, code);
38295 OMP_CLAUSE_CHAIN (c) = list;
38296 return c;
38299 /* OpenMP 4.0:
38300 parallel
38302 sections
38303 taskgroup */
38305 static tree
38306 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
38307 enum omp_clause_code code,
38308 tree list, location_t location)
38310 tree c = build_omp_clause (location, code);
38311 OMP_CLAUSE_CHAIN (c) = list;
38312 return c;
38315 /* OpenMP 4.5:
38316 nogroup */
38318 static tree
38319 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
38320 tree list, location_t location)
38322 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
38323 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
38324 OMP_CLAUSE_CHAIN (c) = list;
38325 return c;
38328 /* OpenMP 4.5:
38329 simd
38330 threads */
38332 static tree
38333 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
38334 enum omp_clause_code code,
38335 tree list, location_t location)
38337 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38338 tree c = build_omp_clause (location, code);
38339 OMP_CLAUSE_CHAIN (c) = list;
38340 return c;
38343 /* OpenMP 4.0:
38344 num_teams ( expression )
38346 OpenMP 5.1:
38347 num_teams ( expression : expression ) */
38349 static tree
38350 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
38351 location_t location)
38353 tree upper, lower = NULL_TREE, c;
38355 matching_parens parens;
38356 if (!parens.require_open (parser))
38357 return list;
38359 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38360 parser->colon_corrects_to_scope_p = false;
38361 upper = cp_parser_assignment_expression (parser);
38362 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38364 if (upper != error_mark_node
38365 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38367 lower = upper;
38368 cp_lexer_consume_token (parser->lexer);
38369 upper = cp_parser_assignment_expression (parser);
38372 if (upper == error_mark_node
38373 || !parens.require_close (parser))
38374 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38375 /*or_comma=*/false,
38376 /*consume_paren=*/true);
38378 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
38379 "num_teams", location);
38381 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
38382 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
38383 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
38384 OMP_CLAUSE_CHAIN (c) = list;
38386 return c;
38389 /* OpenMP 4.0:
38390 thread_limit ( expression ) */
38392 static tree
38393 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
38394 location_t location)
38396 tree t, c;
38398 matching_parens parens;
38399 if (!parens.require_open (parser))
38400 return list;
38402 t = cp_parser_assignment_expression (parser);
38404 if (t == error_mark_node
38405 || !parens.require_close (parser))
38406 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38407 /*or_comma=*/false,
38408 /*consume_paren=*/true);
38410 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
38411 "thread_limit", location);
38413 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
38414 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
38415 OMP_CLAUSE_CHAIN (c) = list;
38417 return c;
38420 /* OpenMP 4.0:
38421 aligned ( variable-list )
38422 aligned ( variable-list : constant-expression ) */
38424 static tree
38425 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
38427 tree nlist, c, alignment = NULL_TREE;
38428 bool colon;
38430 matching_parens parens;
38431 if (!parens.require_open (parser))
38432 return list;
38434 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
38435 &colon);
38437 if (colon)
38439 alignment = cp_parser_constant_expression (parser);
38441 if (!parens.require_close (parser))
38442 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38443 /*or_comma=*/false,
38444 /*consume_paren=*/true);
38446 if (alignment == error_mark_node)
38447 alignment = NULL_TREE;
38450 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38451 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
38453 return nlist;
38456 /* OpenMP 5.0:
38457 allocate ( variable-list )
38458 allocate ( expression : variable-list )
38460 OpenMP 5.1:
38461 allocate ( allocator-modifier : variable-list )
38462 allocate ( allocator-modifier , allocator-modifier : variable-list )
38464 allocator-modifier:
38465 allocator ( expression )
38466 align ( expression ) */
38468 static tree
38469 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
38471 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
38472 bool colon, has_modifiers = false;
38474 matching_parens parens;
38475 if (!parens.require_open (parser))
38476 return list;
38478 cp_parser_parse_tentatively (parser);
38479 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38480 parser->colon_corrects_to_scope_p = false;
38481 for (int mod = 0; mod < 2; mod++)
38482 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38483 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38485 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38486 const char *p = IDENTIFIER_POINTER (id);
38487 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
38488 break;
38489 cp_lexer_consume_token (parser->lexer);
38490 matching_parens parens2;
38491 if (!parens2.require_open (parser))
38492 break;
38493 if (strcmp (p, "allocator") == 0)
38495 if (allocator != NULL_TREE)
38496 break;
38497 allocator = cp_parser_assignment_expression (parser);
38499 else
38501 if (align != NULL_TREE)
38502 break;
38503 align = cp_parser_assignment_expression (parser);
38505 if (!parens2.require_close (parser))
38506 break;
38507 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38509 has_modifiers = true;
38510 break;
38512 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38513 break;
38514 cp_lexer_consume_token (parser->lexer);
38516 else
38517 break;
38518 if (!has_modifiers)
38520 cp_parser_abort_tentative_parse (parser);
38521 align = NULL_TREE;
38522 allocator = NULL_TREE;
38523 cp_parser_parse_tentatively (parser);
38524 allocator = cp_parser_assignment_expression (parser);
38526 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38527 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38529 cp_parser_parse_definitely (parser);
38530 cp_lexer_consume_token (parser->lexer);
38531 if (allocator == error_mark_node)
38532 allocator = NULL_TREE;
38533 if (align == error_mark_node)
38534 align = NULL_TREE;
38536 else
38538 cp_parser_abort_tentative_parse (parser);
38539 allocator = NULL_TREE;
38540 align = NULL_TREE;
38543 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
38544 &colon);
38546 if (allocator || align)
38547 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38549 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
38550 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
38553 return nlist;
38556 /* OpenMP 2.5:
38557 lastprivate ( variable-list )
38559 OpenMP 5.0:
38560 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
38562 static tree
38563 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
38565 bool conditional = false;
38567 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38568 return list;
38570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38571 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38573 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38574 const char *p = IDENTIFIER_POINTER (id);
38576 if (strcmp ("conditional", p) == 0)
38578 conditional = true;
38579 cp_lexer_consume_token (parser->lexer);
38580 cp_lexer_consume_token (parser->lexer);
38584 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
38585 list, NULL);
38587 if (conditional)
38588 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38589 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
38590 return nlist;
38593 /* OpenMP 4.0:
38594 linear ( variable-list )
38595 linear ( variable-list : expression )
38597 OpenMP 4.5:
38598 linear ( modifier ( variable-list ) )
38599 linear ( modifier ( variable-list ) : expression ) */
38601 static tree
38602 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
38603 bool declare_simd)
38605 tree nlist, c, step = integer_one_node;
38606 bool colon;
38607 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
38609 matching_parens parens;
38610 if (!parens.require_open (parser))
38611 return list;
38613 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38615 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38616 const char *p = IDENTIFIER_POINTER (id);
38618 if (strcmp ("ref", p) == 0)
38619 kind = OMP_CLAUSE_LINEAR_REF;
38620 else if (strcmp ("val", p) == 0)
38621 kind = OMP_CLAUSE_LINEAR_VAL;
38622 else if (strcmp ("uval", p) == 0)
38623 kind = OMP_CLAUSE_LINEAR_UVAL;
38624 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38625 cp_lexer_consume_token (parser->lexer);
38626 else
38627 kind = OMP_CLAUSE_LINEAR_DEFAULT;
38630 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
38631 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
38632 &colon);
38633 else
38635 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
38636 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
38637 if (colon)
38638 cp_parser_require (parser, CPP_COLON, RT_COLON);
38639 else if (!parens.require_close (parser))
38640 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38641 /*or_comma=*/false,
38642 /*consume_paren=*/true);
38645 if (colon)
38647 step = NULL_TREE;
38648 if (declare_simd
38649 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38650 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
38652 cp_token *token = cp_lexer_peek_token (parser->lexer);
38653 cp_parser_parse_tentatively (parser);
38654 step = cp_parser_id_expression (parser, /*template_p=*/false,
38655 /*check_dependency_p=*/true,
38656 /*template_p=*/NULL,
38657 /*declarator_p=*/false,
38658 /*optional_p=*/false);
38659 if (step != error_mark_node)
38660 step = cp_parser_lookup_name_simple (parser, step, token->location);
38661 if (step == error_mark_node)
38663 step = NULL_TREE;
38664 cp_parser_abort_tentative_parse (parser);
38666 else if (!cp_parser_parse_definitely (parser))
38667 step = NULL_TREE;
38669 if (!step)
38670 step = cp_parser_assignment_expression (parser);
38672 if (!parens.require_close (parser))
38673 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38674 /*or_comma=*/false,
38675 /*consume_paren=*/true);
38677 if (step == error_mark_node)
38678 return list;
38681 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38683 OMP_CLAUSE_LINEAR_STEP (c) = step;
38684 OMP_CLAUSE_LINEAR_KIND (c) = kind;
38687 return nlist;
38690 /* OpenMP 4.0:
38691 safelen ( constant-expression ) */
38693 static tree
38694 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
38695 location_t location)
38697 tree t, c;
38699 matching_parens parens;
38700 if (!parens.require_open (parser))
38701 return list;
38703 t = cp_parser_constant_expression (parser);
38705 if (t == error_mark_node
38706 || !parens.require_close (parser))
38707 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38708 /*or_comma=*/false,
38709 /*consume_paren=*/true);
38711 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
38713 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
38714 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
38715 OMP_CLAUSE_CHAIN (c) = list;
38717 return c;
38720 /* OpenMP 4.0:
38721 simdlen ( constant-expression ) */
38723 static tree
38724 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
38725 location_t location)
38727 tree t, c;
38729 matching_parens parens;
38730 if (!parens.require_open (parser))
38731 return list;
38733 t = cp_parser_constant_expression (parser);
38735 if (t == error_mark_node
38736 || !parens.require_close (parser))
38737 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38738 /*or_comma=*/false,
38739 /*consume_paren=*/true);
38741 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
38743 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
38744 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
38745 OMP_CLAUSE_CHAIN (c) = list;
38747 return c;
38750 /* OpenMP 4.5:
38751 vec:
38752 identifier [+/- integer]
38753 vec , identifier [+/- integer]
38756 static tree
38757 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
38758 tree list)
38760 tree vec = NULL;
38762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38764 cp_parser_error (parser, "expected identifier");
38765 return list;
38768 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38770 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
38771 tree t, identifier = cp_parser_identifier (parser);
38772 tree addend = NULL;
38774 if (identifier == error_mark_node)
38775 t = error_mark_node;
38776 else
38778 t = cp_parser_lookup_name_simple
38779 (parser, identifier,
38780 cp_lexer_peek_token (parser->lexer)->location);
38781 if (t == error_mark_node)
38782 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
38783 id_loc);
38786 bool neg = false;
38787 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
38788 neg = true;
38789 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
38791 addend = integer_zero_node;
38792 goto add_to_vector;
38794 cp_lexer_consume_token (parser->lexer);
38796 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
38798 cp_parser_error (parser, "expected integer");
38799 return list;
38802 addend = cp_lexer_peek_token (parser->lexer)->u.value;
38803 if (TREE_CODE (addend) != INTEGER_CST)
38805 cp_parser_error (parser, "expected integer");
38806 return list;
38808 cp_lexer_consume_token (parser->lexer);
38810 add_to_vector:
38811 if (t != error_mark_node)
38813 vec = tree_cons (addend, t, vec);
38814 if (neg)
38815 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
38818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
38819 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38820 break;
38822 cp_lexer_consume_token (parser->lexer);
38825 if (vec)
38827 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
38828 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
38829 OMP_CLAUSE_DECL (u) = nreverse (vec);
38830 OMP_CLAUSE_CHAIN (u) = list;
38831 return u;
38833 return list;
38836 /* OpenMP 5.0:
38837 detach ( event-handle ) */
38839 static tree
38840 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
38842 matching_parens parens;
38844 if (!parens.require_open (parser))
38845 return list;
38847 cp_token *token;
38848 tree name, decl;
38850 token = cp_lexer_peek_token (parser->lexer);
38851 name = cp_parser_id_expression (parser, /*template_p=*/false,
38852 /*check_dependency_p=*/true,
38853 /*template_p=*/NULL,
38854 /*declarator_p=*/false,
38855 /*optional_p=*/false);
38856 if (name == error_mark_node)
38857 decl = error_mark_node;
38858 else
38860 if (identifier_p (name))
38861 decl = cp_parser_lookup_name_simple (parser, name, token->location);
38862 else
38863 decl = name;
38864 if (decl == error_mark_node)
38865 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
38866 token->location);
38869 if (decl == error_mark_node
38870 || !parens.require_close (parser))
38871 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38872 /*or_comma=*/false,
38873 /*consume_paren=*/true);
38875 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
38876 OMP_CLAUSE_DECL (u) = decl;
38877 OMP_CLAUSE_CHAIN (u) = list;
38879 return u;
38882 /* OpenMP 5.0:
38883 iterators ( iterators-definition )
38885 iterators-definition:
38886 iterator-specifier
38887 iterator-specifier , iterators-definition
38889 iterator-specifier:
38890 identifier = range-specification
38891 iterator-type identifier = range-specification
38893 range-specification:
38894 begin : end
38895 begin : end : step */
38897 static tree
38898 cp_parser_omp_iterators (cp_parser *parser)
38900 tree ret = NULL_TREE, *last = &ret;
38901 cp_lexer_consume_token (parser->lexer);
38903 matching_parens parens;
38904 if (!parens.require_open (parser))
38905 return error_mark_node;
38907 bool saved_colon_corrects_to_scope_p
38908 = parser->colon_corrects_to_scope_p;
38909 bool saved_colon_doesnt_start_class_def_p
38910 = parser->colon_doesnt_start_class_def_p;
38914 tree iter_type;
38915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38916 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
38917 iter_type = integer_type_node;
38918 else
38920 const char *saved_message
38921 = parser->type_definition_forbidden_message;
38922 parser->type_definition_forbidden_message
38923 = G_("types may not be defined in iterator type");
38925 iter_type = cp_parser_type_id (parser);
38927 parser->type_definition_forbidden_message = saved_message;
38930 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38933 cp_parser_error (parser, "expected identifier");
38934 break;
38937 tree id = cp_parser_identifier (parser);
38938 if (id == error_mark_node)
38939 break;
38941 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38942 break;
38944 parser->colon_corrects_to_scope_p = false;
38945 parser->colon_doesnt_start_class_def_p = true;
38946 tree begin = cp_parser_assignment_expression (parser);
38948 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38949 break;
38951 tree end = cp_parser_assignment_expression (parser);
38953 tree step = integer_one_node;
38954 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38956 cp_lexer_consume_token (parser->lexer);
38957 step = cp_parser_assignment_expression (parser);
38960 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
38961 DECL_ARTIFICIAL (iter_var) = 1;
38962 DECL_CONTEXT (iter_var) = current_function_decl;
38963 pushdecl (iter_var);
38965 *last = make_tree_vec (6);
38966 TREE_VEC_ELT (*last, 0) = iter_var;
38967 TREE_VEC_ELT (*last, 1) = begin;
38968 TREE_VEC_ELT (*last, 2) = end;
38969 TREE_VEC_ELT (*last, 3) = step;
38970 last = &TREE_CHAIN (*last);
38972 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38974 cp_lexer_consume_token (parser->lexer);
38975 continue;
38977 break;
38979 while (1);
38981 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38982 parser->colon_doesnt_start_class_def_p
38983 = saved_colon_doesnt_start_class_def_p;
38985 if (!parens.require_close (parser))
38986 cp_parser_skip_to_closing_parenthesis (parser,
38987 /*recovering=*/true,
38988 /*or_comma=*/false,
38989 /*consume_paren=*/true);
38991 return ret ? ret : error_mark_node;
38994 /* OpenMP 5.0:
38995 affinity ( [aff-modifier :] variable-list )
38996 aff-modifier:
38997 iterator ( iterators-definition ) */
38999 static tree
39000 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
39002 tree nlist, c, iterators = NULL_TREE;
39004 matching_parens parens;
39005 if (!parens.require_open (parser))
39006 return list;
39008 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39010 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39011 const char *p = IDENTIFIER_POINTER (id);
39012 bool parse_iter = ((strcmp ("iterator", p) == 0)
39013 && (cp_lexer_nth_token_is (parser->lexer, 2,
39014 CPP_OPEN_PAREN)));
39015 if (parse_iter)
39017 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
39018 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
39020 if (parse_iter)
39022 begin_scope (sk_omp, NULL);
39023 iterators = cp_parser_omp_iterators (parser);
39024 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39026 if (iterators)
39027 poplevel (0, 1, 0);
39028 cp_parser_skip_to_closing_parenthesis (parser,
39029 /*recovering=*/true,
39030 /*or_comma=*/false,
39031 /*consume_paren=*/true);
39032 return list;
39036 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
39037 list, NULL);
39038 if (iterators)
39040 tree block = poplevel (1, 1, 0);
39041 if (iterators != error_mark_node)
39043 TREE_VEC_ELT (iterators, 5) = block;
39044 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39045 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
39046 OMP_CLAUSE_DECL (c));
39049 return nlist;
39052 /* OpenMP 4.0:
39053 depend ( depend-kind : variable-list )
39055 depend-kind:
39056 in | out | inout
39058 OpenMP 4.5:
39059 depend ( source )
39061 depend ( sink : vec )
39063 OpenMP 5.0:
39064 depend ( depend-modifier , depend-kind: variable-list )
39066 depend-kind:
39067 in | out | inout | mutexinoutset | depobj
39069 depend-modifier:
39070 iterator ( iterators-definition ) */
39072 static tree
39073 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
39075 tree nlist, c, iterators = NULL_TREE;
39076 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
39078 matching_parens parens;
39079 if (!parens.require_open (parser))
39080 return list;
39084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39085 goto invalid_kind;
39087 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39088 const char *p = IDENTIFIER_POINTER (id);
39090 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
39092 begin_scope (sk_omp, NULL);
39093 iterators = cp_parser_omp_iterators (parser);
39094 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
39095 continue;
39097 if (strcmp ("in", p) == 0)
39098 kind = OMP_CLAUSE_DEPEND_IN;
39099 else if (strcmp ("inout", p) == 0)
39100 kind = OMP_CLAUSE_DEPEND_INOUT;
39101 else if (strcmp ("mutexinoutset", p) == 0)
39102 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
39103 else if (strcmp ("out", p) == 0)
39104 kind = OMP_CLAUSE_DEPEND_OUT;
39105 else if (strcmp ("depobj", p) == 0)
39106 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
39107 else if (strcmp ("sink", p) == 0)
39108 kind = OMP_CLAUSE_DEPEND_SINK;
39109 else if (strcmp ("source", p) == 0)
39110 kind = OMP_CLAUSE_DEPEND_SOURCE;
39111 else
39112 goto invalid_kind;
39113 break;
39115 while (1);
39117 cp_lexer_consume_token (parser->lexer);
39119 if (iterators
39120 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
39122 poplevel (0, 1, 0);
39123 error_at (loc, "%<iterator%> modifier incompatible with %qs",
39124 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
39125 iterators = NULL_TREE;
39128 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
39130 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
39131 OMP_CLAUSE_DEPEND_KIND (c) = kind;
39132 OMP_CLAUSE_DECL (c) = NULL_TREE;
39133 OMP_CLAUSE_CHAIN (c) = list;
39134 if (!parens.require_close (parser))
39135 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39136 /*or_comma=*/false,
39137 /*consume_paren=*/true);
39138 return c;
39141 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39142 goto resync_fail;
39144 if (kind == OMP_CLAUSE_DEPEND_SINK)
39146 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
39147 if (!parens.require_close (parser))
39148 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39149 /*or_comma=*/false,
39150 /*consume_paren=*/true);
39152 else
39154 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
39155 list, NULL);
39157 if (iterators)
39159 tree block = poplevel (1, 1, 0);
39160 if (iterators == error_mark_node)
39161 iterators = NULL_TREE;
39162 else
39163 TREE_VEC_ELT (iterators, 5) = block;
39166 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39168 OMP_CLAUSE_DEPEND_KIND (c) = kind;
39169 if (iterators)
39170 OMP_CLAUSE_DECL (c)
39171 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
39174 return nlist;
39176 invalid_kind:
39177 cp_parser_error (parser, "invalid depend kind");
39178 resync_fail:
39179 if (iterators)
39180 poplevel (0, 1, 0);
39181 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39182 /*or_comma=*/false,
39183 /*consume_paren=*/true);
39184 return list;
39187 /* OpenMP 4.0:
39188 map ( map-kind : variable-list )
39189 map ( variable-list )
39191 map-kind:
39192 alloc | to | from | tofrom
39194 OpenMP 4.5:
39195 map-kind:
39196 alloc | to | from | tofrom | release | delete
39198 map ( always [,] map-kind: variable-list )
39200 OpenMP 5.0:
39201 map ( [map-type-modifier[,] ...] map-kind: variable-list )
39203 map-type-modifier:
39204 always | close */
39206 static tree
39207 cp_parser_omp_clause_map (cp_parser *parser, tree list)
39209 tree nlist, c;
39210 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
39212 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39213 return list;
39215 int pos = 1;
39216 int map_kind_pos = 0;
39217 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
39218 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
39220 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
39222 map_kind_pos = pos;
39223 break;
39226 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
39227 pos++;
39228 pos++;
39231 bool always_modifier = false;
39232 bool close_modifier = false;
39233 for (int pos = 1; pos < map_kind_pos; ++pos)
39235 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39236 if (tok->type == CPP_COMMA)
39238 cp_lexer_consume_token (parser->lexer);
39239 continue;
39242 const char *p = IDENTIFIER_POINTER (tok->u.value);
39243 if (strcmp ("always", p) == 0)
39245 if (always_modifier)
39247 cp_parser_error (parser, "too many %<always%> modifiers");
39248 cp_parser_skip_to_closing_parenthesis (parser,
39249 /*recovering=*/true,
39250 /*or_comma=*/false,
39251 /*consume_paren=*/true);
39252 return list;
39254 always_modifier = true;
39256 else if (strcmp ("close", p) == 0)
39258 if (close_modifier)
39260 cp_parser_error (parser, "too many %<close%> modifiers");
39261 cp_parser_skip_to_closing_parenthesis (parser,
39262 /*recovering=*/true,
39263 /*or_comma=*/false,
39264 /*consume_paren=*/true);
39265 return list;
39267 close_modifier = true;
39269 else
39271 cp_parser_error (parser, "%<#pragma omp target%> with "
39272 "modifier other than %<always%> or %<close%>"
39273 "on %<map%> clause");
39274 cp_parser_skip_to_closing_parenthesis (parser,
39275 /*recovering=*/true,
39276 /*or_comma=*/false,
39277 /*consume_paren=*/true);
39278 return list;
39281 cp_lexer_consume_token (parser->lexer);
39284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39285 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39287 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39288 const char *p = IDENTIFIER_POINTER (id);
39290 if (strcmp ("alloc", p) == 0)
39291 kind = GOMP_MAP_ALLOC;
39292 else if (strcmp ("to", p) == 0)
39293 kind = always_modifier ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
39294 else if (strcmp ("from", p) == 0)
39295 kind = always_modifier ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
39296 else if (strcmp ("tofrom", p) == 0)
39297 kind = always_modifier ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
39298 else if (strcmp ("release", p) == 0)
39299 kind = GOMP_MAP_RELEASE;
39300 else
39302 cp_parser_error (parser, "invalid map kind");
39303 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39304 /*or_comma=*/false,
39305 /*consume_paren=*/true);
39306 return list;
39308 cp_lexer_consume_token (parser->lexer);
39309 cp_lexer_consume_token (parser->lexer);
39311 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
39312 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39314 kind = GOMP_MAP_DELETE;
39315 cp_lexer_consume_token (parser->lexer);
39316 cp_lexer_consume_token (parser->lexer);
39319 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
39320 NULL);
39322 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39323 OMP_CLAUSE_SET_MAP_KIND (c, kind);
39325 return nlist;
39328 /* OpenMP 4.0:
39329 device ( expression )
39331 OpenMP 5.0:
39332 device ( [device-modifier :] integer-expression )
39334 device-modifier:
39335 ancestor | device_num */
39337 static tree
39338 cp_parser_omp_clause_device (cp_parser *parser, tree list,
39339 location_t location)
39341 tree t, c;
39342 bool ancestor = false;
39344 matching_parens parens;
39345 if (!parens.require_open (parser))
39346 return list;
39348 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39349 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39351 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39352 const char *p = IDENTIFIER_POINTER (tok->u.value);
39353 if (strcmp ("ancestor", p) == 0)
39355 ancestor = true;
39357 /* A requires directive with the reverse_offload clause must be
39358 specified. */
39359 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
39361 error_at (tok->location, "%<ancestor%> device modifier not "
39362 "preceded by %<requires%> directive "
39363 "with %<reverse_offload%> clause");
39364 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39365 return list;
39368 else if (strcmp ("device_num", p) == 0)
39370 else
39372 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
39373 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39374 return list;
39376 cp_lexer_consume_token (parser->lexer);
39377 cp_lexer_consume_token (parser->lexer);
39380 t = cp_parser_assignment_expression (parser);
39382 if (t == error_mark_node
39383 || !parens.require_close (parser))
39384 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39385 /*or_comma=*/false,
39386 /*consume_paren=*/true);
39388 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
39389 "device", location);
39391 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
39392 OMP_CLAUSE_DEVICE_ID (c) = t;
39393 OMP_CLAUSE_CHAIN (c) = list;
39394 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
39396 return c;
39399 /* OpenMP 4.0:
39400 dist_schedule ( static )
39401 dist_schedule ( static , expression ) */
39403 static tree
39404 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
39405 location_t location)
39407 tree c, t;
39409 matching_parens parens;
39410 if (!parens.require_open (parser))
39411 return list;
39413 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
39415 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39416 goto invalid_kind;
39417 cp_lexer_consume_token (parser->lexer);
39419 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39421 cp_lexer_consume_token (parser->lexer);
39423 t = cp_parser_assignment_expression (parser);
39425 if (t == error_mark_node)
39426 goto resync_fail;
39427 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
39429 if (!parens.require_close (parser))
39430 goto resync_fail;
39432 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39433 goto resync_fail;
39435 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
39436 "dist_schedule", location); */
39437 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
39438 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
39439 OMP_CLAUSE_CHAIN (c) = list;
39440 return c;
39442 invalid_kind:
39443 cp_parser_error (parser, "invalid dist_schedule kind");
39444 resync_fail:
39445 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39446 /*or_comma=*/false,
39447 /*consume_paren=*/true);
39448 return list;
39451 /* OpenMP 4.0:
39452 proc_bind ( proc-bind-kind )
39454 proc-bind-kind:
39455 primary | master | close | spread
39456 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
39458 static tree
39459 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
39460 location_t location)
39462 tree c;
39463 enum omp_clause_proc_bind_kind kind;
39465 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39466 return list;
39468 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39470 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39471 const char *p = IDENTIFIER_POINTER (id);
39473 if (strcmp ("primary", p) == 0)
39474 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
39475 else if (strcmp ("master", p) == 0)
39476 kind = OMP_CLAUSE_PROC_BIND_MASTER;
39477 else if (strcmp ("close", p) == 0)
39478 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
39479 else if (strcmp ("spread", p) == 0)
39480 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
39481 else
39482 goto invalid_kind;
39484 else
39485 goto invalid_kind;
39487 cp_lexer_consume_token (parser->lexer);
39488 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39489 goto resync_fail;
39491 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
39492 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
39493 location);
39494 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
39495 OMP_CLAUSE_CHAIN (c) = list;
39496 return c;
39498 invalid_kind:
39499 cp_parser_error (parser, "invalid depend kind");
39500 resync_fail:
39501 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39502 /*or_comma=*/false,
39503 /*consume_paren=*/true);
39504 return list;
39507 /* OpenMP 5.0:
39508 device_type ( host | nohost | any ) */
39510 static tree
39511 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
39512 location_t location)
39514 tree c;
39515 enum omp_clause_device_type_kind kind;
39517 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39518 return list;
39520 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39522 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39523 const char *p = IDENTIFIER_POINTER (id);
39525 if (strcmp ("host", p) == 0)
39526 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
39527 else if (strcmp ("nohost", p) == 0)
39528 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
39529 else if (strcmp ("any", p) == 0)
39530 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
39531 else
39532 goto invalid_kind;
39534 else
39535 goto invalid_kind;
39537 cp_lexer_consume_token (parser->lexer);
39538 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39539 goto resync_fail;
39541 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
39542 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
39543 location); */
39544 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
39545 OMP_CLAUSE_CHAIN (c) = list;
39546 return c;
39548 invalid_kind:
39549 cp_parser_error (parser, "invalid depend kind");
39550 resync_fail:
39551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39552 /*or_comma=*/false,
39553 /*consume_paren=*/true);
39554 return list;
39557 /* OpenACC:
39558 async [( int-expr )] */
39560 static tree
39561 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
39563 tree c, t;
39564 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39566 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
39568 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39570 matching_parens parens;
39571 parens.consume_open (parser);
39573 t = cp_parser_assignment_expression (parser);
39574 if (t == error_mark_node
39575 || !parens.require_close (parser))
39576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39577 /*or_comma=*/false,
39578 /*consume_paren=*/true);
39581 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
39583 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
39584 OMP_CLAUSE_ASYNC_EXPR (c) = t;
39585 OMP_CLAUSE_CHAIN (c) = list;
39586 list = c;
39588 return list;
39591 /* Parse all OpenACC clauses. The set clauses allowed by the directive
39592 is a bitmask in MASK. Return the list of clauses found. */
39594 static tree
39595 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
39596 const char *where, cp_token *pragma_tok,
39597 bool finish_p = true)
39599 tree clauses = NULL;
39600 bool first = true;
39602 /* Don't create location wrapper nodes within OpenACC clauses. */
39603 auto_suppress_location_wrappers sentinel;
39605 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39607 location_t here;
39608 pragma_omp_clause c_kind;
39609 omp_clause_code code;
39610 const char *c_name;
39611 tree prev = clauses;
39613 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39614 cp_lexer_consume_token (parser->lexer);
39616 here = cp_lexer_peek_token (parser->lexer)->location;
39617 c_kind = cp_parser_omp_clause_name (parser);
39619 switch (c_kind)
39621 case PRAGMA_OACC_CLAUSE_ASYNC:
39622 clauses = cp_parser_oacc_clause_async (parser, clauses);
39623 c_name = "async";
39624 break;
39625 case PRAGMA_OACC_CLAUSE_AUTO:
39626 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
39627 clauses);
39628 c_name = "auto";
39629 break;
39630 case PRAGMA_OACC_CLAUSE_ATTACH:
39631 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39632 c_name = "attach";
39633 break;
39634 case PRAGMA_OACC_CLAUSE_COLLAPSE:
39635 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
39636 c_name = "collapse";
39637 break;
39638 case PRAGMA_OACC_CLAUSE_COPY:
39639 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39640 c_name = "copy";
39641 break;
39642 case PRAGMA_OACC_CLAUSE_COPYIN:
39643 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39644 c_name = "copyin";
39645 break;
39646 case PRAGMA_OACC_CLAUSE_COPYOUT:
39647 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39648 c_name = "copyout";
39649 break;
39650 case PRAGMA_OACC_CLAUSE_CREATE:
39651 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39652 c_name = "create";
39653 break;
39654 case PRAGMA_OACC_CLAUSE_DELETE:
39655 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39656 c_name = "delete";
39657 break;
39658 case PRAGMA_OMP_CLAUSE_DEFAULT:
39659 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
39660 c_name = "default";
39661 break;
39662 case PRAGMA_OACC_CLAUSE_DETACH:
39663 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39664 c_name = "detach";
39665 break;
39666 case PRAGMA_OACC_CLAUSE_DEVICE:
39667 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39668 c_name = "device";
39669 break;
39670 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
39671 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
39672 c_name = "deviceptr";
39673 break;
39674 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
39675 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39676 c_name = "device_resident";
39677 break;
39678 case PRAGMA_OACC_CLAUSE_FINALIZE:
39679 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
39680 clauses);
39681 c_name = "finalize";
39682 break;
39683 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
39684 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39685 clauses);
39686 c_name = "firstprivate";
39687 break;
39688 case PRAGMA_OACC_CLAUSE_GANG:
39689 c_name = "gang";
39690 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
39691 c_name, clauses);
39692 break;
39693 case PRAGMA_OACC_CLAUSE_HOST:
39694 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39695 c_name = "host";
39696 break;
39697 case PRAGMA_OACC_CLAUSE_IF:
39698 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
39699 c_name = "if";
39700 break;
39701 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
39702 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
39703 clauses);
39704 c_name = "if_present";
39705 break;
39706 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
39707 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
39708 clauses);
39709 c_name = "independent";
39710 break;
39711 case PRAGMA_OACC_CLAUSE_LINK:
39712 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39713 c_name = "link";
39714 break;
39715 case PRAGMA_OACC_CLAUSE_NO_CREATE:
39716 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39717 c_name = "no_create";
39718 break;
39719 case PRAGMA_OACC_CLAUSE_NOHOST:
39720 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
39721 clauses);
39722 c_name = "nohost";
39723 break;
39724 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
39725 code = OMP_CLAUSE_NUM_GANGS;
39726 c_name = "num_gangs";
39727 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39728 clauses);
39729 break;
39730 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
39731 c_name = "num_workers";
39732 code = OMP_CLAUSE_NUM_WORKERS;
39733 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39734 clauses);
39735 break;
39736 case PRAGMA_OACC_CLAUSE_PRESENT:
39737 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39738 c_name = "present";
39739 break;
39740 case PRAGMA_OACC_CLAUSE_PRIVATE:
39741 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
39742 clauses);
39743 c_name = "private";
39744 break;
39745 case PRAGMA_OACC_CLAUSE_REDUCTION:
39746 clauses
39747 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
39748 false, clauses);
39749 c_name = "reduction";
39750 break;
39751 case PRAGMA_OACC_CLAUSE_SEQ:
39752 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
39753 clauses);
39754 c_name = "seq";
39755 break;
39756 case PRAGMA_OACC_CLAUSE_TILE:
39757 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
39758 c_name = "tile";
39759 break;
39760 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
39761 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
39762 clauses);
39763 c_name = "use_device";
39764 break;
39765 case PRAGMA_OACC_CLAUSE_VECTOR:
39766 c_name = "vector";
39767 clauses = cp_parser_oacc_shape_clause (parser, here,
39768 OMP_CLAUSE_VECTOR,
39769 c_name, clauses);
39770 break;
39771 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
39772 c_name = "vector_length";
39773 code = OMP_CLAUSE_VECTOR_LENGTH;
39774 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39775 clauses);
39776 break;
39777 case PRAGMA_OACC_CLAUSE_WAIT:
39778 clauses = cp_parser_oacc_clause_wait (parser, clauses);
39779 c_name = "wait";
39780 break;
39781 case PRAGMA_OACC_CLAUSE_WORKER:
39782 c_name = "worker";
39783 clauses = cp_parser_oacc_shape_clause (parser, here,
39784 OMP_CLAUSE_WORKER,
39785 c_name, clauses);
39786 break;
39787 default:
39788 cp_parser_error (parser, "expected %<#pragma acc%> clause");
39789 goto saw_error;
39792 first = false;
39794 if (((mask >> c_kind) & 1) == 0)
39796 /* Remove the invalid clause(s) from the list to avoid
39797 confusing the rest of the compiler. */
39798 clauses = prev;
39799 error_at (here, "%qs is not valid for %qs", c_name, where);
39803 saw_error:
39804 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39806 if (finish_p)
39807 return finish_omp_clauses (clauses, C_ORT_ACC);
39809 return clauses;
39812 /* Parse all OpenMP clauses. The set clauses allowed by the directive
39813 is a bitmask in MASK. Return the list of clauses found.
39814 FINISH_P set if finish_omp_clauses should be called.
39815 NESTED non-zero if clauses should be terminated by closing paren instead
39816 of end of pragma. If it is 2, additionally commas are required in between
39817 the clauses. */
39819 static tree
39820 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
39821 const char *where, cp_token *pragma_tok,
39822 bool finish_p = true, int nested = 0)
39824 tree clauses = NULL;
39825 bool first = true;
39826 cp_token *token = NULL;
39828 /* Don't create location wrapper nodes within OpenMP clauses. */
39829 auto_suppress_location_wrappers sentinel;
39831 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39833 pragma_omp_clause c_kind;
39834 const char *c_name;
39835 tree prev = clauses;
39837 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39838 break;
39840 if (!first
39841 /* OpenMP 5.1 allows optional comma in between directive-name and
39842 clauses everywhere, but as we aren't done with OpenMP 5.0
39843 implementation yet, let's allow it for now only in C++11
39844 attributes. */
39845 || (parser->lexer->in_omp_attribute_pragma && nested != 2))
39847 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39848 cp_lexer_consume_token (parser->lexer);
39849 else if (nested == 2)
39850 error_at (cp_lexer_peek_token (parser->lexer)->location,
39851 "clauses in %<simd%> trait should be separated "
39852 "by %<,%>");
39855 token = cp_lexer_peek_token (parser->lexer);
39856 c_kind = cp_parser_omp_clause_name (parser);
39858 switch (c_kind)
39860 case PRAGMA_OMP_CLAUSE_BIND:
39861 clauses = cp_parser_omp_clause_bind (parser, clauses,
39862 token->location);
39863 c_name = "bind";
39864 break;
39865 case PRAGMA_OMP_CLAUSE_COLLAPSE:
39866 clauses = cp_parser_omp_clause_collapse (parser, clauses,
39867 token->location);
39868 c_name = "collapse";
39869 break;
39870 case PRAGMA_OMP_CLAUSE_COPYIN:
39871 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
39872 c_name = "copyin";
39873 break;
39874 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
39875 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
39876 clauses);
39877 c_name = "copyprivate";
39878 break;
39879 case PRAGMA_OMP_CLAUSE_DEFAULT:
39880 clauses = cp_parser_omp_clause_default (parser, clauses,
39881 token->location, false);
39882 c_name = "default";
39883 break;
39884 case PRAGMA_OMP_CLAUSE_FILTER:
39885 clauses = cp_parser_omp_clause_filter (parser, clauses,
39886 token->location);
39887 c_name = "filter";
39888 break;
39889 case PRAGMA_OMP_CLAUSE_FINAL:
39890 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
39891 c_name = "final";
39892 break;
39893 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
39894 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39895 clauses);
39896 c_name = "firstprivate";
39897 break;
39898 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
39899 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
39900 token->location);
39901 c_name = "grainsize";
39902 break;
39903 case PRAGMA_OMP_CLAUSE_HINT:
39904 clauses = cp_parser_omp_clause_hint (parser, clauses,
39905 token->location);
39906 c_name = "hint";
39907 break;
39908 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
39909 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
39910 token->location);
39911 c_name = "defaultmap";
39912 break;
39913 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
39914 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
39915 clauses);
39916 c_name = "use_device_ptr";
39917 break;
39918 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
39919 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
39920 clauses);
39921 c_name = "use_device_addr";
39922 break;
39923 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
39924 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
39925 clauses);
39926 c_name = "is_device_ptr";
39927 break;
39928 case PRAGMA_OMP_CLAUSE_IF:
39929 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
39930 true);
39931 c_name = "if";
39932 break;
39933 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
39934 clauses
39935 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
39936 true, clauses);
39937 c_name = "in_reduction";
39938 break;
39939 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
39940 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
39941 c_name = "lastprivate";
39942 break;
39943 case PRAGMA_OMP_CLAUSE_MERGEABLE:
39944 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
39945 token->location);
39946 c_name = "mergeable";
39947 break;
39948 case PRAGMA_OMP_CLAUSE_NOWAIT:
39949 clauses = cp_parser_omp_clause_nowait (parser, clauses,
39950 token->location);
39951 c_name = "nowait";
39952 break;
39953 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
39954 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
39955 token->location);
39956 c_name = "num_tasks";
39957 break;
39958 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
39959 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
39960 token->location);
39961 c_name = "num_threads";
39962 break;
39963 case PRAGMA_OMP_CLAUSE_ORDER:
39964 clauses = cp_parser_omp_clause_order (parser, clauses,
39965 token->location);
39966 c_name = "order";
39967 break;
39968 case PRAGMA_OMP_CLAUSE_ORDERED:
39969 clauses = cp_parser_omp_clause_ordered (parser, clauses,
39970 token->location);
39971 c_name = "ordered";
39972 break;
39973 case PRAGMA_OMP_CLAUSE_PRIORITY:
39974 clauses = cp_parser_omp_clause_priority (parser, clauses,
39975 token->location);
39976 c_name = "priority";
39977 break;
39978 case PRAGMA_OMP_CLAUSE_PRIVATE:
39979 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
39980 clauses);
39981 c_name = "private";
39982 break;
39983 case PRAGMA_OMP_CLAUSE_REDUCTION:
39984 clauses
39985 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
39986 true, clauses);
39987 c_name = "reduction";
39988 break;
39989 case PRAGMA_OMP_CLAUSE_SCHEDULE:
39990 clauses = cp_parser_omp_clause_schedule (parser, clauses,
39991 token->location);
39992 c_name = "schedule";
39993 break;
39994 case PRAGMA_OMP_CLAUSE_SHARED:
39995 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
39996 clauses);
39997 c_name = "shared";
39998 break;
39999 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
40000 clauses
40001 = cp_parser_omp_clause_reduction (parser,
40002 OMP_CLAUSE_TASK_REDUCTION,
40003 true, clauses);
40004 c_name = "task_reduction";
40005 break;
40006 case PRAGMA_OMP_CLAUSE_UNTIED:
40007 clauses = cp_parser_omp_clause_untied (parser, clauses,
40008 token->location);
40009 c_name = "untied";
40010 break;
40011 case PRAGMA_OMP_CLAUSE_INBRANCH:
40012 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
40013 clauses, token->location);
40014 c_name = "inbranch";
40015 break;
40016 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
40017 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
40018 clauses);
40019 c_name = "nontemporal";
40020 break;
40021 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
40022 clauses = cp_parser_omp_clause_branch (parser,
40023 OMP_CLAUSE_NOTINBRANCH,
40024 clauses, token->location);
40025 c_name = "notinbranch";
40026 break;
40027 case PRAGMA_OMP_CLAUSE_PARALLEL:
40028 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
40029 clauses, token->location);
40030 c_name = "parallel";
40031 if (!first)
40033 clause_not_first:
40034 error_at (token->location, "%qs must be the first clause of %qs",
40035 c_name, where);
40036 clauses = prev;
40038 break;
40039 case PRAGMA_OMP_CLAUSE_FOR:
40040 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
40041 clauses, token->location);
40042 c_name = "for";
40043 if (!first)
40044 goto clause_not_first;
40045 break;
40046 case PRAGMA_OMP_CLAUSE_SECTIONS:
40047 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
40048 clauses, token->location);
40049 c_name = "sections";
40050 if (!first)
40051 goto clause_not_first;
40052 break;
40053 case PRAGMA_OMP_CLAUSE_TASKGROUP:
40054 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
40055 clauses, token->location);
40056 c_name = "taskgroup";
40057 if (!first)
40058 goto clause_not_first;
40059 break;
40060 case PRAGMA_OMP_CLAUSE_LINK:
40061 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
40062 c_name = "to";
40063 break;
40064 case PRAGMA_OMP_CLAUSE_TO:
40065 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
40066 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
40067 clauses);
40068 else
40069 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
40070 c_name = "to";
40071 break;
40072 case PRAGMA_OMP_CLAUSE_FROM:
40073 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
40074 c_name = "from";
40075 break;
40076 case PRAGMA_OMP_CLAUSE_UNIFORM:
40077 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
40078 clauses);
40079 c_name = "uniform";
40080 break;
40081 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
40082 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
40083 token->location);
40084 c_name = "num_teams";
40085 break;
40086 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
40087 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
40088 token->location);
40089 c_name = "thread_limit";
40090 break;
40091 case PRAGMA_OMP_CLAUSE_ALIGNED:
40092 clauses = cp_parser_omp_clause_aligned (parser, clauses);
40093 c_name = "aligned";
40094 break;
40095 case PRAGMA_OMP_CLAUSE_ALLOCATE:
40096 clauses = cp_parser_omp_clause_allocate (parser, clauses);
40097 c_name = "allocate";
40098 break;
40099 case PRAGMA_OMP_CLAUSE_LINEAR:
40101 bool declare_simd = false;
40102 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
40103 declare_simd = true;
40104 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
40106 c_name = "linear";
40107 break;
40108 case PRAGMA_OMP_CLAUSE_AFFINITY:
40109 clauses = cp_parser_omp_clause_affinity (parser, clauses);
40110 c_name = "affinity";
40111 break;
40112 case PRAGMA_OMP_CLAUSE_DEPEND:
40113 clauses = cp_parser_omp_clause_depend (parser, clauses,
40114 token->location);
40115 c_name = "depend";
40116 break;
40117 case PRAGMA_OMP_CLAUSE_DETACH:
40118 clauses = cp_parser_omp_clause_detach (parser, clauses);
40119 c_name = "detach";
40120 break;
40121 case PRAGMA_OMP_CLAUSE_MAP:
40122 clauses = cp_parser_omp_clause_map (parser, clauses);
40123 c_name = "map";
40124 break;
40125 case PRAGMA_OMP_CLAUSE_DEVICE:
40126 clauses = cp_parser_omp_clause_device (parser, clauses,
40127 token->location);
40128 c_name = "device";
40129 break;
40130 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
40131 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
40132 token->location);
40133 c_name = "dist_schedule";
40134 break;
40135 case PRAGMA_OMP_CLAUSE_PROC_BIND:
40136 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
40137 token->location);
40138 c_name = "proc_bind";
40139 break;
40140 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
40141 clauses = cp_parser_omp_clause_device_type (parser, clauses,
40142 token->location);
40143 c_name = "device_type";
40144 break;
40145 case PRAGMA_OMP_CLAUSE_SAFELEN:
40146 clauses = cp_parser_omp_clause_safelen (parser, clauses,
40147 token->location);
40148 c_name = "safelen";
40149 break;
40150 case PRAGMA_OMP_CLAUSE_SIMDLEN:
40151 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
40152 token->location);
40153 c_name = "simdlen";
40154 break;
40155 case PRAGMA_OMP_CLAUSE_NOGROUP:
40156 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
40157 token->location);
40158 c_name = "nogroup";
40159 break;
40160 case PRAGMA_OMP_CLAUSE_THREADS:
40161 clauses
40162 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
40163 clauses, token->location);
40164 c_name = "threads";
40165 break;
40166 case PRAGMA_OMP_CLAUSE_SIMD:
40167 clauses
40168 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
40169 clauses, token->location);
40170 c_name = "simd";
40171 break;
40172 default:
40173 cp_parser_error (parser, "expected %<#pragma omp%> clause");
40174 goto saw_error;
40177 first = false;
40179 if (((mask >> c_kind) & 1) == 0)
40181 /* Remove the invalid clause(s) from the list to avoid
40182 confusing the rest of the compiler. */
40183 clauses = prev;
40184 error_at (token->location, "%qs is not valid for %qs", c_name, where);
40187 saw_error:
40188 if (!nested)
40189 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40190 if (finish_p)
40192 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
40193 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
40194 else
40195 return finish_omp_clauses (clauses, C_ORT_OMP);
40197 return clauses;
40200 /* OpenMP 2.5:
40201 structured-block:
40202 statement
40204 In practice, we're also interested in adding the statement to an
40205 outer node. So it is convenient if we work around the fact that
40206 cp_parser_statement calls add_stmt. */
40208 static unsigned
40209 cp_parser_begin_omp_structured_block (cp_parser *parser)
40211 unsigned save = parser->in_statement;
40213 /* Only move the values to IN_OMP_BLOCK if they weren't false.
40214 This preserves the "not within loop or switch" style error messages
40215 for nonsense cases like
40216 void foo() {
40217 #pragma omp single
40218 break;
40221 if (parser->in_statement)
40222 parser->in_statement = IN_OMP_BLOCK;
40224 return save;
40227 static void
40228 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
40230 parser->in_statement = save;
40233 static tree
40234 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
40236 tree stmt = begin_omp_structured_block ();
40237 unsigned int save = cp_parser_begin_omp_structured_block (parser);
40239 parser->omp_attrs_forbidden_p = true;
40240 cp_parser_statement (parser, NULL_TREE, false, if_p);
40242 cp_parser_end_omp_structured_block (parser, save);
40243 return finish_omp_structured_block (stmt);
40246 /* OpenMP 5.0:
40247 # pragma omp allocate (list) [allocator(allocator)] */
40249 static void
40250 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
40252 tree allocator = NULL_TREE;
40253 location_t loc = pragma_tok->location;
40254 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
40256 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
40257 if (parser->lexer->in_omp_attribute_pragma
40258 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40259 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40260 cp_lexer_consume_token (parser->lexer);
40262 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40264 matching_parens parens;
40265 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40266 const char *p = IDENTIFIER_POINTER (id);
40267 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40268 cp_lexer_consume_token (parser->lexer);
40269 if (strcmp (p, "allocator") != 0)
40270 error_at (cloc, "expected %<allocator%>");
40271 else if (parens.require_open (parser))
40273 allocator = cp_parser_assignment_expression (parser);
40274 if (allocator == error_mark_node)
40275 allocator = NULL_TREE;
40276 parens.require_close (parser);
40279 cp_parser_require_pragma_eol (parser, pragma_tok);
40281 if (allocator)
40282 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
40283 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40285 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
40288 /* OpenMP 2.5:
40289 # pragma omp atomic new-line
40290 expression-stmt
40292 expression-stmt:
40293 x binop= expr | x++ | ++x | x-- | --x
40294 binop:
40295 +, *, -, /, &, ^, |, <<, >>
40297 where x is an lvalue expression with scalar type.
40299 OpenMP 3.1:
40300 # pragma omp atomic new-line
40301 update-stmt
40303 # pragma omp atomic read new-line
40304 read-stmt
40306 # pragma omp atomic write new-line
40307 write-stmt
40309 # pragma omp atomic update new-line
40310 update-stmt
40312 # pragma omp atomic capture new-line
40313 capture-stmt
40315 # pragma omp atomic capture new-line
40316 capture-block
40318 read-stmt:
40319 v = x
40320 write-stmt:
40321 x = expr
40322 update-stmt:
40323 expression-stmt | x = x binop expr
40324 capture-stmt:
40325 v = expression-stmt
40326 capture-block:
40327 { v = x; update-stmt; } | { update-stmt; v = x; }
40329 OpenMP 4.0:
40330 update-stmt:
40331 expression-stmt | x = x binop expr | x = expr binop x
40332 capture-stmt:
40333 v = update-stmt
40334 capture-block:
40335 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
40337 OpenMP 5.1:
40338 # pragma omp atomic compare new-line
40339 conditional-update-atomic
40341 # pragma omp atomic compare capture new-line
40342 conditional-update-capture-atomic
40344 conditional-update-atomic:
40345 cond-expr-stmt | cond-update-stmt
40346 cond-expr-stmt:
40347 x = expr ordop x ? expr : x;
40348 x = x ordop expr ? expr : x;
40349 x = x == e ? d : x;
40350 cond-update-stmt:
40351 if (expr ordop x) { x = expr; }
40352 if (x ordop expr) { x = expr; }
40353 if (x == e) { x = d; }
40354 ordop:
40355 <, >
40356 conditional-update-capture-atomic:
40357 v = cond-expr-stmt
40358 { v = x; cond-expr-stmt }
40359 { cond-expr-stmt v = x; }
40360 { v = x; cond-update-stmt }
40361 { cond-update-stmt v = x; }
40362 if (x == e) { x = d; } else { v = x; }
40363 { r = x == e; if (r) { x = d; } }
40364 { r = x == e; if (r) { x = d; } else { v = x; } }
40366 where x, r and v are lvalue expressions with scalar type,
40367 expr, e and d are expressions with scalar type and e might be
40368 the same as v. */
40370 static void
40371 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
40373 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
40374 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
40375 location_t loc = pragma_tok->location;
40376 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
40377 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
40378 bool structured_block = false;
40379 bool first = true;
40380 tree clauses = NULL_TREE;
40381 bool capture = false;
40382 bool compare = false;
40383 bool weak = false;
40384 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40385 bool no_semicolon = false;
40386 bool extra_scope = false;
40388 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40390 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
40391 if ((!first || parser->lexer->in_omp_attribute_pragma)
40392 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40393 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40394 cp_lexer_consume_token (parser->lexer);
40396 first = false;
40398 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40400 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40401 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40402 const char *p = IDENTIFIER_POINTER (id);
40403 enum tree_code new_code = ERROR_MARK;
40404 enum omp_memory_order new_memory_order
40405 = OMP_MEMORY_ORDER_UNSPECIFIED;
40406 bool new_capture = false;
40407 bool new_compare = false;
40408 bool new_weak = false;
40409 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40411 if (!strcmp (p, "read"))
40412 new_code = OMP_ATOMIC_READ;
40413 else if (!strcmp (p, "write"))
40414 new_code = NOP_EXPR;
40415 else if (!strcmp (p, "update"))
40416 new_code = OMP_ATOMIC;
40417 else if (openacc && !strcmp (p, "capture"))
40418 new_code = OMP_ATOMIC_CAPTURE_NEW;
40419 else if (openacc)
40421 p = NULL;
40422 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
40423 "or %<capture%> clause");
40425 else if (!strcmp (p, "capture"))
40426 new_capture = true;
40427 else if (!strcmp (p, "compare"))
40428 new_compare = true;
40429 else if (!strcmp (p, "weak"))
40430 new_weak = true;
40431 else if (!strcmp (p, "fail"))
40433 matching_parens parens;
40435 cp_lexer_consume_token (parser->lexer);
40436 if (!parens.require_open (parser))
40437 continue;
40439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40441 id = cp_lexer_peek_token (parser->lexer)->u.value;
40442 const char *q = IDENTIFIER_POINTER (id);
40444 if (!strcmp (q, "seq_cst"))
40445 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
40446 else if (!strcmp (q, "acquire"))
40447 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
40448 else if (!strcmp (q, "relaxed"))
40449 new_fail = OMP_MEMORY_ORDER_RELAXED;
40452 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40454 cp_lexer_consume_token (parser->lexer);
40455 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40456 error_at (cloc, "too many %qs clauses", "fail");
40457 else
40458 fail = new_fail;
40460 else
40461 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
40462 "or %<relaxed%>");
40463 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
40464 || !parens.require_close (parser))
40465 cp_parser_skip_to_closing_parenthesis (parser,
40466 /*recovering=*/true,
40467 /*or_comma=*/false,
40468 /*consume_paren=*/true);
40469 continue;
40471 else if (!strcmp (p, "seq_cst"))
40472 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40473 else if (!strcmp (p, "acq_rel"))
40474 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
40475 else if (!strcmp (p, "release"))
40476 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
40477 else if (!strcmp (p, "acquire"))
40478 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40479 else if (!strcmp (p, "relaxed"))
40480 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
40481 else if (!strcmp (p, "hint"))
40483 cp_lexer_consume_token (parser->lexer);
40484 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
40485 continue;
40487 else
40489 p = NULL;
40490 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
40491 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
40492 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
40493 "%<relaxed%> or %<hint%> clause");
40495 if (p)
40497 if (new_code != ERROR_MARK)
40499 /* OpenACC permits 'update capture'. */
40500 if (openacc
40501 && code == OMP_ATOMIC
40502 && new_code == OMP_ATOMIC_CAPTURE_NEW)
40503 code = new_code;
40504 else if (code != ERROR_MARK)
40505 error_at (cloc, "too many atomic clauses");
40506 else
40507 code = new_code;
40509 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
40511 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
40512 error_at (cloc, "too many memory order clauses");
40513 else
40514 memory_order = new_memory_order;
40516 else if (new_capture)
40518 if (capture)
40519 error_at (cloc, "too many %qs clauses", "capture");
40520 else
40521 capture = true;
40523 else if (new_compare)
40525 if (compare)
40526 error_at (cloc, "too many %qs clauses", "compare");
40527 else
40528 compare = true;
40530 else if (new_weak)
40532 if (weak)
40533 error_at (cloc, "too many %qs clauses", "weak");
40534 else
40535 weak = true;
40537 cp_lexer_consume_token (parser->lexer);
40538 continue;
40541 break;
40543 cp_parser_require_pragma_eol (parser, pragma_tok);
40545 if (code == ERROR_MARK)
40546 code = OMP_ATOMIC;
40547 if (capture)
40549 if (code != OMP_ATOMIC)
40550 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
40551 "clauses", "capture");
40552 else
40553 code = OMP_ATOMIC_CAPTURE_NEW;
40555 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
40557 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
40558 "clauses", "compare");
40559 compare = false;
40561 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
40563 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
40564 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40566 if (weak && !compare)
40568 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
40569 weak = false;
40571 if (openacc)
40572 memory_order = OMP_MEMORY_ORDER_RELAXED;
40573 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
40575 omp_requires_mask
40576 = (enum omp_requires) (omp_requires_mask
40577 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
40578 switch ((enum omp_memory_order)
40579 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
40581 case OMP_MEMORY_ORDER_UNSPECIFIED:
40582 case OMP_MEMORY_ORDER_RELAXED:
40583 memory_order = OMP_MEMORY_ORDER_RELAXED;
40584 break;
40585 case OMP_MEMORY_ORDER_SEQ_CST:
40586 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40587 break;
40588 case OMP_MEMORY_ORDER_ACQ_REL:
40589 switch (code)
40591 case OMP_ATOMIC_READ:
40592 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40593 break;
40594 case NOP_EXPR: /* atomic write */
40595 memory_order = OMP_MEMORY_ORDER_RELEASE;
40596 break;
40597 default:
40598 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
40599 break;
40601 break;
40602 default:
40603 gcc_unreachable ();
40606 else
40607 switch (code)
40609 case OMP_ATOMIC_READ:
40610 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
40612 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
40613 "%<release%> clause");
40614 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40616 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
40617 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40618 break;
40619 case NOP_EXPR: /* atomic write */
40620 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
40622 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
40623 "%<acquire%> clause");
40624 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40626 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
40627 memory_order = OMP_MEMORY_ORDER_RELEASE;
40628 break;
40629 default:
40630 break;
40632 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40633 memory_order
40634 = (enum omp_memory_order) (memory_order
40635 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
40637 switch (code)
40639 case OMP_ATOMIC_READ:
40640 case NOP_EXPR: /* atomic write */
40641 v = cp_parser_unary_expression (parser);
40642 if (v == error_mark_node)
40643 goto saw_error;
40644 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40645 goto saw_error;
40646 if (code == NOP_EXPR)
40647 lhs = cp_parser_expression (parser);
40648 else
40649 lhs = cp_parser_unary_expression (parser);
40650 if (lhs == error_mark_node)
40651 goto saw_error;
40652 if (code == NOP_EXPR)
40654 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
40655 opcode. */
40656 code = OMP_ATOMIC;
40657 rhs = lhs;
40658 lhs = v;
40659 v = NULL_TREE;
40661 goto done;
40662 case OMP_ATOMIC_CAPTURE_NEW:
40663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
40665 cp_lexer_consume_token (parser->lexer);
40666 structured_block = true;
40668 else if (compare
40669 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40670 break;
40671 else
40673 v = cp_parser_unary_expression (parser);
40674 if (v == error_mark_node)
40675 goto saw_error;
40676 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40677 goto saw_error;
40678 if (compare
40679 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40681 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
40682 error_at (eloc, "expected expression");
40683 goto saw_error;
40686 default:
40687 break;
40690 restart:
40691 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40693 cp_lexer_consume_token (parser->lexer);
40695 matching_parens parens;
40696 if (!parens.require_open (parser))
40697 goto saw_error;
40698 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
40699 tree cmp_expr;
40700 if (r)
40701 cmp_expr = cp_parser_unary_expression (parser);
40702 else
40703 cmp_expr = cp_parser_binary_expression (parser, false, true,
40704 PREC_NOT_OPERATOR, NULL);
40705 if (!parens.require_close (parser))
40706 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40707 if (cmp_expr == error_mark_node)
40708 goto saw_error;
40709 if (r)
40711 if (!cp_tree_equal (cmp_expr, r))
40712 goto bad_if;
40713 cmp_expr = rhs;
40714 rhs = NULL_TREE;
40715 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
40717 if (TREE_CODE (cmp_expr) == EQ_EXPR)
40719 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
40721 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
40722 "expected %<==%> comparison in %<if%> condition");
40723 goto saw_error;
40725 else if (TREE_CODE (cmp_expr) != GT_EXPR
40726 && TREE_CODE (cmp_expr) != LT_EXPR)
40728 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
40729 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
40730 "condition");
40731 goto saw_error;
40733 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
40734 goto saw_error;
40736 extra_scope = true;
40737 eloc = cp_lexer_peek_token (parser->lexer)->location;
40738 lhs = cp_parser_unary_expression (parser);
40739 orig_lhs = lhs;
40740 if (lhs == error_mark_node)
40741 goto saw_error;
40742 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
40744 cp_parser_error (parser, "expected %<=%>");
40745 goto saw_error;
40747 cp_lexer_consume_token (parser->lexer);
40748 eloc = cp_lexer_peek_token (parser->lexer)->location;
40749 if (TREE_CODE (cmp_expr) == EQ_EXPR)
40750 rhs1 = cp_parser_expression (parser);
40751 else
40752 rhs1 = cp_parser_simple_cast_expression (parser);
40754 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
40755 goto saw_error;
40757 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
40758 goto saw_error;
40760 extra_scope = false;
40761 no_semicolon = true;
40763 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
40765 if (TREE_CODE (cmp_expr) == EQ_EXPR)
40767 opcode = COND_EXPR;
40768 rhs = TREE_OPERAND (cmp_expr, 1);
40770 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
40772 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
40773 ? MIN_EXPR : MAX_EXPR);
40774 rhs = rhs1;
40775 rhs1 = TREE_OPERAND (cmp_expr, 0);
40777 else
40778 goto bad_if;
40780 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
40781 goto bad_if;
40782 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
40783 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
40785 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
40786 ? MAX_EXPR : MIN_EXPR);
40787 rhs = rhs1;
40788 rhs1 = TREE_OPERAND (cmp_expr, 1);
40790 else
40792 bad_if:
40793 cp_parser_error (parser,
40794 "invalid form of %<#pragma omp atomic compare%>");
40795 goto saw_error;
40798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
40800 if (code != OMP_ATOMIC_CAPTURE_NEW
40801 || (structured_block && r == NULL_TREE)
40802 || TREE_CODE (cmp_expr) != EQ_EXPR)
40804 eloc = cp_lexer_peek_token (parser->lexer)->location;
40805 error_at (eloc, "unexpected %<else%>");
40806 goto saw_error;
40809 cp_lexer_consume_token (parser->lexer);
40811 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
40812 goto saw_error;
40814 extra_scope = true;
40815 v = cp_parser_unary_expression (parser);
40816 if (v == error_mark_node)
40817 goto saw_error;
40818 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40819 goto saw_error;
40821 tree expr = cp_parser_simple_cast_expression (parser);
40823 if (!cp_tree_equal (expr, lhs))
40824 goto bad_if;
40826 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
40827 goto saw_error;
40829 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
40830 goto saw_error;
40832 extra_scope = false;
40833 code = OMP_ATOMIC_CAPTURE_OLD;
40834 if (r == NULL_TREE)
40835 /* Signal to c_finish_omp_atomic that in
40836 if (x == e) { x = d; } else { v = x; }
40837 case the store to v should be conditional. */
40838 r = void_list_node;
40840 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
40842 cp_parser_error (parser, "expected %<else%>");
40843 goto saw_error;
40845 else if (code == OMP_ATOMIC_CAPTURE_NEW
40846 && r != NULL_TREE
40847 && v == NULL_TREE)
40848 code = OMP_ATOMIC;
40849 goto stmt_done;
40851 lhs = cp_parser_unary_expression (parser);
40852 orig_lhs = lhs;
40853 switch (TREE_CODE (lhs))
40855 case ERROR_MARK:
40856 goto saw_error;
40858 case POSTINCREMENT_EXPR:
40859 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
40860 code = OMP_ATOMIC_CAPTURE_OLD;
40861 /* FALLTHROUGH */
40862 case PREINCREMENT_EXPR:
40863 lhs = TREE_OPERAND (lhs, 0);
40864 opcode = PLUS_EXPR;
40865 rhs = integer_one_node;
40866 if (compare)
40867 goto invalid_compare;
40868 break;
40870 case POSTDECREMENT_EXPR:
40871 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
40872 code = OMP_ATOMIC_CAPTURE_OLD;
40873 /* FALLTHROUGH */
40874 case PREDECREMENT_EXPR:
40875 lhs = TREE_OPERAND (lhs, 0);
40876 opcode = MINUS_EXPR;
40877 rhs = integer_one_node;
40878 if (compare)
40879 goto invalid_compare;
40880 break;
40882 case COMPOUND_EXPR:
40883 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
40884 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
40885 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
40886 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
40887 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
40888 (TREE_OPERAND (lhs, 1), 0), 0)))
40889 == BOOLEAN_TYPE)
40890 /* Undo effects of boolean_increment for post {in,de}crement. */
40891 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
40892 /* FALLTHRU */
40893 case MODIFY_EXPR:
40894 if (TREE_CODE (lhs) == MODIFY_EXPR
40895 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
40897 /* Undo effects of boolean_increment. */
40898 if (integer_onep (TREE_OPERAND (lhs, 1)))
40900 /* This is pre or post increment. */
40901 rhs = TREE_OPERAND (lhs, 1);
40902 lhs = TREE_OPERAND (lhs, 0);
40903 opcode = NOP_EXPR;
40904 if (code == OMP_ATOMIC_CAPTURE_NEW
40905 && !structured_block
40906 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
40907 code = OMP_ATOMIC_CAPTURE_OLD;
40908 if (compare)
40909 goto invalid_compare;
40910 break;
40913 /* FALLTHRU */
40914 default:
40915 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
40917 cp_parser_error (parser, "expected %<=%>");
40918 goto saw_error;
40920 switch (cp_lexer_peek_token (parser->lexer)->type)
40922 case CPP_MULT_EQ:
40923 opcode = MULT_EXPR;
40924 break;
40925 case CPP_DIV_EQ:
40926 opcode = TRUNC_DIV_EXPR;
40927 break;
40928 case CPP_PLUS_EQ:
40929 opcode = PLUS_EXPR;
40930 break;
40931 case CPP_MINUS_EQ:
40932 opcode = MINUS_EXPR;
40933 break;
40934 case CPP_LSHIFT_EQ:
40935 opcode = LSHIFT_EXPR;
40936 break;
40937 case CPP_RSHIFT_EQ:
40938 opcode = RSHIFT_EXPR;
40939 break;
40940 case CPP_AND_EQ:
40941 opcode = BIT_AND_EXPR;
40942 break;
40943 case CPP_OR_EQ:
40944 opcode = BIT_IOR_EXPR;
40945 break;
40946 case CPP_XOR_EQ:
40947 opcode = BIT_XOR_EXPR;
40948 break;
40949 case CPP_EQ:
40950 enum cp_parser_prec oprec;
40951 cp_token *token;
40952 cp_lexer_consume_token (parser->lexer);
40953 cp_parser_parse_tentatively (parser);
40954 rhs1 = cp_parser_simple_cast_expression (parser);
40955 if (rhs1 == error_mark_node)
40957 cp_parser_abort_tentative_parse (parser);
40958 cp_parser_simple_cast_expression (parser);
40959 goto saw_error;
40961 token = cp_lexer_peek_token (parser->lexer);
40962 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
40964 cp_parser_abort_tentative_parse (parser);
40965 cp_parser_parse_tentatively (parser);
40966 rhs = cp_parser_binary_expression (parser, false, true,
40967 PREC_NOT_OPERATOR, NULL);
40968 if (rhs == error_mark_node)
40970 cp_parser_abort_tentative_parse (parser);
40971 cp_parser_binary_expression (parser, false, true,
40972 PREC_NOT_OPERATOR, NULL);
40973 goto saw_error;
40975 switch (TREE_CODE (rhs))
40977 case MULT_EXPR:
40978 case TRUNC_DIV_EXPR:
40979 case RDIV_EXPR:
40980 case PLUS_EXPR:
40981 case MINUS_EXPR:
40982 case LSHIFT_EXPR:
40983 case RSHIFT_EXPR:
40984 case BIT_AND_EXPR:
40985 case BIT_IOR_EXPR:
40986 case BIT_XOR_EXPR:
40987 if (compare)
40988 break;
40989 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
40991 if (cp_parser_parse_definitely (parser))
40993 opcode = TREE_CODE (rhs);
40994 rhs1 = TREE_OPERAND (rhs, 0);
40995 rhs = TREE_OPERAND (rhs, 1);
40996 goto stmt_done;
40998 else
40999 goto saw_error;
41001 break;
41002 case EQ_EXPR:
41003 if (!compare
41004 || code != OMP_ATOMIC_CAPTURE_NEW
41005 || !structured_block
41006 || v
41007 || r)
41008 break;
41009 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
41010 && cp_lexer_nth_token_is_keyword (parser->lexer,
41011 2, RID_IF))
41013 if (cp_parser_parse_definitely (parser))
41015 r = lhs;
41016 lhs = NULL_TREE;
41017 rhs1 = NULL_TREE;
41018 cp_lexer_consume_token (parser->lexer);
41019 goto restart;
41022 break;
41023 case GT_EXPR:
41024 case LT_EXPR:
41025 if (compare
41026 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
41027 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
41028 && cp_parser_parse_definitely (parser))
41030 opcode = TREE_CODE (rhs);
41031 rhs1 = TREE_OPERAND (rhs, 0);
41032 rhs = TREE_OPERAND (rhs, 1);
41033 cond_expr:
41034 cp_lexer_consume_token (parser->lexer);
41035 bool saved_colon_corrects_to_scope_p
41036 = parser->colon_corrects_to_scope_p;
41037 parser->colon_corrects_to_scope_p = false;
41038 tree e1 = cp_parser_expression (parser);
41039 parser->colon_corrects_to_scope_p
41040 = saved_colon_corrects_to_scope_p;
41041 cp_parser_require (parser, CPP_COLON, RT_COLON);
41042 tree e2 = cp_parser_simple_cast_expression (parser);
41043 if (cp_tree_equal (lhs, e2))
41045 if (cp_tree_equal (lhs, rhs1))
41047 if (opcode == EQ_EXPR)
41049 opcode = COND_EXPR;
41050 rhs1 = e1;
41051 goto stmt_done;
41053 if (cp_tree_equal (rhs, e1))
41055 opcode
41056 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
41057 rhs = e1;
41058 goto stmt_done;
41061 else
41063 gcc_assert (opcode != EQ_EXPR);
41064 if (cp_tree_equal (rhs1, e1))
41066 opcode
41067 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
41068 rhs1 = rhs;
41069 rhs = e1;
41070 goto stmt_done;
41074 cp_parser_error (parser,
41075 "invalid form of "
41076 "%<#pragma omp atomic compare%>");
41077 goto saw_error;
41079 break;
41080 default:
41081 break;
41083 cp_parser_abort_tentative_parse (parser);
41084 if (structured_block
41085 && code == OMP_ATOMIC_CAPTURE_OLD
41086 && !compare)
41088 rhs = cp_parser_expression (parser);
41089 if (rhs == error_mark_node)
41090 goto saw_error;
41091 opcode = NOP_EXPR;
41092 rhs1 = NULL_TREE;
41093 goto stmt_done;
41095 cp_parser_error (parser,
41096 "invalid form of %<#pragma omp atomic%>");
41097 goto saw_error;
41099 if (!cp_parser_parse_definitely (parser))
41100 goto saw_error;
41101 switch (token->type)
41103 case CPP_SEMICOLON:
41104 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
41106 code = OMP_ATOMIC_CAPTURE_OLD;
41107 v = lhs;
41108 lhs = NULL_TREE;
41109 lhs1 = rhs1;
41110 rhs1 = NULL_TREE;
41111 cp_lexer_consume_token (parser->lexer);
41112 goto restart;
41114 else if (structured_block && !compare)
41116 opcode = NOP_EXPR;
41117 rhs = rhs1;
41118 rhs1 = NULL_TREE;
41119 goto stmt_done;
41121 cp_parser_error (parser,
41122 "invalid form of %<#pragma omp atomic%>");
41123 goto saw_error;
41124 case CPP_MULT:
41125 opcode = MULT_EXPR;
41126 break;
41127 case CPP_DIV:
41128 opcode = TRUNC_DIV_EXPR;
41129 break;
41130 case CPP_PLUS:
41131 opcode = PLUS_EXPR;
41132 break;
41133 case CPP_MINUS:
41134 opcode = MINUS_EXPR;
41135 break;
41136 case CPP_LSHIFT:
41137 opcode = LSHIFT_EXPR;
41138 break;
41139 case CPP_RSHIFT:
41140 opcode = RSHIFT_EXPR;
41141 break;
41142 case CPP_AND:
41143 opcode = BIT_AND_EXPR;
41144 break;
41145 case CPP_OR:
41146 opcode = BIT_IOR_EXPR;
41147 break;
41148 case CPP_XOR:
41149 opcode = BIT_XOR_EXPR;
41150 break;
41151 case CPP_EQ_EQ:
41152 opcode = EQ_EXPR;
41153 break;
41154 case CPP_GREATER:
41155 opcode = GT_EXPR;
41156 break;
41157 case CPP_LESS:
41158 opcode = LT_EXPR;
41159 break;
41160 default:
41161 cp_parser_error (parser,
41162 "invalid operator for %<#pragma omp atomic%>");
41163 goto saw_error;
41165 if (compare
41166 && TREE_CODE_CLASS (opcode) != tcc_comparison)
41168 cp_parser_error (parser,
41169 "invalid form of "
41170 "%<#pragma omp atomic compare%>");
41171 goto saw_error;
41173 oprec = TOKEN_PRECEDENCE (token);
41174 gcc_assert (oprec != PREC_NOT_OPERATOR);
41175 if (commutative_tree_code (opcode))
41176 oprec = (enum cp_parser_prec) (oprec - 1);
41177 cp_lexer_consume_token (parser->lexer);
41178 rhs = cp_parser_binary_expression (parser, false, false,
41179 oprec, NULL);
41180 if (rhs == error_mark_node)
41181 goto saw_error;
41182 if (compare)
41184 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
41186 cp_parser_error (parser,
41187 "invalid form of "
41188 "%<#pragma omp atomic compare%>");
41189 goto saw_error;
41191 goto cond_expr;
41193 goto stmt_done;
41194 default:
41195 cp_parser_error (parser,
41196 "invalid operator for %<#pragma omp atomic%>");
41197 goto saw_error;
41199 cp_lexer_consume_token (parser->lexer);
41201 rhs = cp_parser_expression (parser);
41202 if (rhs == error_mark_node)
41203 goto saw_error;
41204 break;
41206 stmt_done:
41207 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
41209 if (!no_semicolon
41210 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41211 goto saw_error;
41212 no_semicolon = false;
41213 v = cp_parser_unary_expression (parser);
41214 if (v == error_mark_node)
41215 goto saw_error;
41216 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41217 goto saw_error;
41218 lhs1 = cp_parser_unary_expression (parser);
41219 if (lhs1 == error_mark_node)
41220 goto saw_error;
41222 if (structured_block)
41224 if (!no_semicolon)
41225 cp_parser_consume_semicolon_at_end_of_statement (parser);
41226 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
41228 done:
41229 if (weak && opcode != COND_EXPR)
41231 error_at (loc, "%<weak%> clause requires atomic equality comparison");
41232 weak = false;
41234 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
41235 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
41236 rhs1, r, clauses, memory_order, weak);
41237 if (!structured_block && !no_semicolon)
41238 cp_parser_consume_semicolon_at_end_of_statement (parser);
41239 return;
41241 invalid_compare:
41242 error ("invalid form of %<pragma omp atomic compare%>");
41243 /* FALLTHRU */
41244 saw_error:
41245 cp_parser_skip_to_end_of_block_or_statement (parser);
41246 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41247 cp_lexer_consume_token (parser->lexer);
41248 if (structured_block)
41250 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41251 cp_lexer_consume_token (parser->lexer);
41252 else if (code == OMP_ATOMIC_CAPTURE_NEW)
41254 cp_parser_skip_to_end_of_block_or_statement (parser);
41255 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41256 cp_lexer_consume_token (parser->lexer);
41262 /* OpenMP 2.5:
41263 # pragma omp barrier new-line */
41265 static void
41266 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
41268 cp_parser_require_pragma_eol (parser, pragma_tok);
41269 finish_omp_barrier ();
41272 /* OpenMP 2.5:
41273 # pragma omp critical [(name)] new-line
41274 structured-block
41276 OpenMP 4.5:
41277 # pragma omp critical [(name) [hint(expression)]] new-line
41278 structured-block */
41280 #define OMP_CRITICAL_CLAUSE_MASK \
41281 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
41283 static tree
41284 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41286 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
41288 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41290 matching_parens parens;
41291 parens.consume_open (parser);
41293 name = cp_parser_identifier (parser);
41295 if (name == error_mark_node
41296 || !parens.require_close (parser))
41297 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41298 /*or_comma=*/false,
41299 /*consume_paren=*/true);
41300 if (name == error_mark_node)
41301 name = NULL;
41303 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41304 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41305 cp_lexer_consume_token (parser->lexer);
41308 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
41309 "#pragma omp critical", pragma_tok);
41311 stmt = cp_parser_omp_structured_block (parser, if_p);
41312 return c_finish_omp_critical (input_location, stmt, name, clauses);
41315 /* OpenMP 5.0:
41316 # pragma omp depobj ( depobj ) depobj-clause new-line
41318 depobj-clause:
41319 depend (dependence-type : locator)
41320 destroy
41321 update (dependence-type)
41323 dependence-type:
41326 inout
41327 mutexinout */
41329 static void
41330 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
41332 location_t loc = pragma_tok->location;
41333 matching_parens parens;
41334 if (!parens.require_open (parser))
41336 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41337 return;
41340 tree depobj = cp_parser_assignment_expression (parser);
41342 if (!parens.require_close (parser))
41343 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41344 /*or_comma=*/false,
41345 /*consume_paren=*/true);
41347 tree clause = NULL_TREE;
41348 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
41349 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
41350 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41351 if (parser->lexer->in_omp_attribute_pragma
41352 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41353 cp_lexer_consume_token (parser->lexer);
41354 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41356 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41357 const char *p = IDENTIFIER_POINTER (id);
41359 cp_lexer_consume_token (parser->lexer);
41360 if (!strcmp ("depend", p))
41362 /* Don't create location wrapper nodes within the depend clause. */
41363 auto_suppress_location_wrappers sentinel;
41364 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
41365 if (clause)
41366 clause = finish_omp_clauses (clause, C_ORT_OMP);
41367 if (!clause)
41368 clause = error_mark_node;
41370 else if (!strcmp ("destroy", p))
41371 kind = OMP_CLAUSE_DEPEND_LAST;
41372 else if (!strcmp ("update", p))
41374 matching_parens c_parens;
41375 if (c_parens.require_open (parser))
41377 location_t c2_loc
41378 = cp_lexer_peek_token (parser->lexer)->location;
41379 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41381 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
41382 const char *p2 = IDENTIFIER_POINTER (id2);
41384 cp_lexer_consume_token (parser->lexer);
41385 if (!strcmp ("in", p2))
41386 kind = OMP_CLAUSE_DEPEND_IN;
41387 else if (!strcmp ("out", p2))
41388 kind = OMP_CLAUSE_DEPEND_OUT;
41389 else if (!strcmp ("inout", p2))
41390 kind = OMP_CLAUSE_DEPEND_INOUT;
41391 else if (!strcmp ("mutexinoutset", p2))
41392 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
41394 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
41396 clause = error_mark_node;
41397 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
41398 "%<mutexinoutset%>");
41400 if (!c_parens.require_close (parser))
41401 cp_parser_skip_to_closing_parenthesis (parser,
41402 /*recovering=*/true,
41403 /*or_comma=*/false,
41404 /*consume_paren=*/true);
41406 else
41407 clause = error_mark_node;
41410 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
41412 clause = error_mark_node;
41413 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
41415 cp_parser_require_pragma_eol (parser, pragma_tok);
41417 finish_omp_depobj (loc, depobj, kind, clause);
41421 /* OpenMP 2.5:
41422 # pragma omp flush flush-vars[opt] new-line
41424 flush-vars:
41425 ( variable-list )
41427 OpenMP 5.0:
41428 # pragma omp flush memory-order-clause new-line */
41430 static void
41431 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
41433 enum memmodel mo = MEMMODEL_LAST;
41434 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41435 if (parser->lexer->in_omp_attribute_pragma
41436 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41437 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41438 cp_lexer_consume_token (parser->lexer);
41439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41441 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41442 const char *p = IDENTIFIER_POINTER (id);
41443 if (!strcmp (p, "seq_cst"))
41444 mo = MEMMODEL_SEQ_CST;
41445 else if (!strcmp (p, "acq_rel"))
41446 mo = MEMMODEL_ACQ_REL;
41447 else if (!strcmp (p, "release"))
41448 mo = MEMMODEL_RELEASE;
41449 else if (!strcmp (p, "acquire"))
41450 mo = MEMMODEL_ACQUIRE;
41451 else
41452 error_at (cp_lexer_peek_token (parser->lexer)->location,
41453 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
41454 "%<acquire%>");
41455 cp_lexer_consume_token (parser->lexer);
41457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41459 if (mo != MEMMODEL_LAST)
41460 error_at (cp_lexer_peek_token (parser->lexer)->location,
41461 "%<flush%> list specified together with memory order "
41462 "clause");
41463 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
41465 cp_parser_require_pragma_eol (parser, pragma_tok);
41467 finish_omp_flush (mo);
41470 /* Helper function, to parse omp for increment expression. */
41472 static tree
41473 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
41475 tree cond = cp_parser_binary_expression (parser, false, true,
41476 PREC_NOT_OPERATOR, NULL);
41477 if (cond == error_mark_node
41478 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
41480 cp_parser_skip_to_end_of_statement (parser);
41481 return error_mark_node;
41484 switch (TREE_CODE (cond))
41486 case GT_EXPR:
41487 case GE_EXPR:
41488 case LT_EXPR:
41489 case LE_EXPR:
41490 break;
41491 case NE_EXPR:
41492 if (code != OACC_LOOP)
41493 break;
41494 gcc_fallthrough ();
41495 default:
41496 return error_mark_node;
41499 /* If decl is an iterator, preserve LHS and RHS of the relational
41500 expr until finish_omp_for. */
41501 if (decl
41502 && (type_dependent_expression_p (decl)
41503 || CLASS_TYPE_P (TREE_TYPE (decl))))
41504 return cond;
41506 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
41507 TREE_CODE (cond),
41508 TREE_OPERAND (cond, 0), ERROR_MARK,
41509 TREE_OPERAND (cond, 1), ERROR_MARK,
41510 /*overload=*/NULL, tf_warning_or_error);
41513 /* Helper function, to parse omp for increment expression. */
41515 static tree
41516 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
41518 cp_token *token = cp_lexer_peek_token (parser->lexer);
41519 enum tree_code op;
41520 tree lhs, rhs;
41521 cp_id_kind idk;
41522 bool decl_first;
41524 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
41526 op = (token->type == CPP_PLUS_PLUS
41527 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
41528 cp_lexer_consume_token (parser->lexer);
41529 lhs = cp_parser_simple_cast_expression (parser);
41530 if (lhs != decl
41531 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
41532 return error_mark_node;
41533 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
41536 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
41537 if (lhs != decl
41538 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
41539 return error_mark_node;
41541 token = cp_lexer_peek_token (parser->lexer);
41542 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
41544 op = (token->type == CPP_PLUS_PLUS
41545 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
41546 cp_lexer_consume_token (parser->lexer);
41547 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
41550 op = cp_parser_assignment_operator_opt (parser);
41551 if (op == ERROR_MARK)
41552 return error_mark_node;
41554 if (op != NOP_EXPR)
41556 rhs = cp_parser_assignment_expression (parser);
41557 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
41558 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
41561 lhs = cp_parser_binary_expression (parser, false, false,
41562 PREC_ADDITIVE_EXPRESSION, NULL);
41563 token = cp_lexer_peek_token (parser->lexer);
41564 decl_first = (lhs == decl
41565 || (processing_template_decl && cp_tree_equal (lhs, decl)));
41566 if (decl_first)
41567 lhs = NULL_TREE;
41568 if (token->type != CPP_PLUS
41569 && token->type != CPP_MINUS)
41570 return error_mark_node;
41574 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
41575 cp_lexer_consume_token (parser->lexer);
41576 rhs = cp_parser_binary_expression (parser, false, false,
41577 PREC_ADDITIVE_EXPRESSION, NULL);
41578 token = cp_lexer_peek_token (parser->lexer);
41579 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
41581 if (lhs == NULL_TREE)
41583 if (op == PLUS_EXPR)
41584 lhs = rhs;
41585 else
41586 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
41587 tf_warning_or_error);
41589 else
41590 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
41591 ERROR_MARK, NULL, tf_warning_or_error);
41594 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
41596 if (!decl_first)
41598 if ((rhs != decl
41599 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
41600 || op == MINUS_EXPR)
41601 return error_mark_node;
41602 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
41604 else
41605 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
41607 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
41610 /* Parse the initialization statement of an OpenMP for loop.
41612 Return true if the resulting construct should have an
41613 OMP_CLAUSE_PRIVATE added to it. */
41615 static tree
41616 cp_parser_omp_for_loop_init (cp_parser *parser,
41617 tree &this_pre_body,
41618 releasing_vec &for_block,
41619 tree &init,
41620 tree &orig_init,
41621 tree &decl,
41622 tree &real_decl)
41624 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
41625 return NULL_TREE;
41627 tree add_private_clause = NULL_TREE;
41629 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
41631 init-expr:
41632 var = lb
41633 integer-type var = lb
41634 random-access-iterator-type var = lb
41635 pointer-type var = lb
41637 cp_decl_specifier_seq type_specifiers;
41639 /* First, try to parse as an initialized declaration. See
41640 cp_parser_condition, from whence the bulk of this is copied. */
41642 cp_parser_parse_tentatively (parser);
41643 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
41644 /*is_declaration=*/true,
41645 /*is_trailing_return=*/false,
41646 &type_specifiers);
41647 if (cp_parser_parse_definitely (parser))
41649 /* If parsing a type specifier seq succeeded, then this
41650 MUST be a initialized declaration. */
41651 tree asm_specification, attributes;
41652 cp_declarator *declarator;
41654 declarator = cp_parser_declarator (parser,
41655 CP_PARSER_DECLARATOR_NAMED,
41656 CP_PARSER_FLAGS_NONE,
41657 /*ctor_dtor_or_conv_p=*/NULL,
41658 /*parenthesized_p=*/NULL,
41659 /*member_p=*/false,
41660 /*friend_p=*/false,
41661 /*static_p=*/false);
41662 attributes = cp_parser_attributes_opt (parser);
41663 asm_specification = cp_parser_asm_specification_opt (parser);
41665 if (declarator == cp_error_declarator)
41666 cp_parser_skip_to_end_of_statement (parser);
41668 else
41670 tree pushed_scope, auto_node;
41672 decl = start_decl (declarator, &type_specifiers,
41673 SD_INITIALIZED, attributes,
41674 /*prefix_attributes=*/NULL_TREE,
41675 &pushed_scope);
41677 auto_node = type_uses_auto (TREE_TYPE (decl));
41678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
41680 if (cp_lexer_next_token_is (parser->lexer,
41681 CPP_OPEN_PAREN))
41682 error ("parenthesized initialization is not allowed in "
41683 "OpenMP %<for%> loop");
41684 else
41685 /* Trigger an error. */
41686 cp_parser_require (parser, CPP_EQ, RT_EQ);
41688 init = error_mark_node;
41689 cp_parser_skip_to_end_of_statement (parser);
41691 else if (CLASS_TYPE_P (TREE_TYPE (decl))
41692 || type_dependent_expression_p (decl)
41693 || auto_node)
41695 bool is_direct_init, is_non_constant_init;
41697 init = cp_parser_initializer (parser,
41698 &is_direct_init,
41699 &is_non_constant_init);
41701 if (auto_node)
41703 TREE_TYPE (decl)
41704 = do_auto_deduction (TREE_TYPE (decl), init,
41705 auto_node);
41707 if (!CLASS_TYPE_P (TREE_TYPE (decl))
41708 && !type_dependent_expression_p (decl))
41709 goto non_class;
41712 cp_finish_decl (decl, init, !is_non_constant_init,
41713 asm_specification,
41714 LOOKUP_ONLYCONVERTING);
41715 orig_init = init;
41716 if (CLASS_TYPE_P (TREE_TYPE (decl)))
41718 vec_safe_push (for_block, this_pre_body);
41719 init = NULL_TREE;
41721 else
41723 init = pop_stmt_list (this_pre_body);
41724 if (init && TREE_CODE (init) == STATEMENT_LIST)
41726 tree_stmt_iterator i = tsi_start (init);
41727 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
41728 while (!tsi_end_p (i))
41730 tree t = tsi_stmt (i);
41731 if (TREE_CODE (t) == DECL_EXPR
41732 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
41734 tsi_delink (&i);
41735 vec_safe_push (for_block, t);
41736 continue;
41738 break;
41740 if (tsi_one_before_end_p (i))
41742 tree t = tsi_stmt (i);
41743 tsi_delink (&i);
41744 free_stmt_list (init);
41745 init = t;
41749 this_pre_body = NULL_TREE;
41751 else
41753 /* Consume '='. */
41754 cp_lexer_consume_token (parser->lexer);
41755 init = cp_parser_assignment_expression (parser);
41757 non_class:
41758 if (TYPE_REF_P (TREE_TYPE (decl)))
41759 init = error_mark_node;
41760 else
41761 cp_finish_decl (decl, NULL_TREE,
41762 /*init_const_expr_p=*/false,
41763 asm_specification,
41764 LOOKUP_ONLYCONVERTING);
41767 if (pushed_scope)
41768 pop_scope (pushed_scope);
41771 else
41773 cp_id_kind idk;
41774 /* If parsing a type specifier sequence failed, then
41775 this MUST be a simple expression. */
41776 cp_parser_parse_tentatively (parser);
41777 decl = cp_parser_primary_expression (parser, false, false,
41778 false, &idk);
41779 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
41780 if (!cp_parser_error_occurred (parser)
41781 && decl
41782 && (TREE_CODE (decl) == COMPONENT_REF
41783 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
41785 cp_parser_abort_tentative_parse (parser);
41786 cp_parser_parse_tentatively (parser);
41787 cp_token *token = cp_lexer_peek_token (parser->lexer);
41788 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
41789 /*check_dependency_p=*/true,
41790 /*template_p=*/NULL,
41791 /*declarator_p=*/false,
41792 /*optional_p=*/false);
41793 if (name != error_mark_node
41794 && last_tok == cp_lexer_peek_token (parser->lexer))
41796 decl = cp_parser_lookup_name_simple (parser, name,
41797 token->location);
41798 if (TREE_CODE (decl) == FIELD_DECL)
41799 add_private_clause = omp_privatize_field (decl, false);
41801 cp_parser_abort_tentative_parse (parser);
41802 cp_parser_parse_tentatively (parser);
41803 decl = cp_parser_primary_expression (parser, false, false,
41804 false, &idk);
41806 if (!cp_parser_error_occurred (parser)
41807 && decl
41808 && DECL_P (decl)
41809 && CLASS_TYPE_P (TREE_TYPE (decl)))
41811 tree rhs;
41813 cp_parser_parse_definitely (parser);
41814 cp_parser_require (parser, CPP_EQ, RT_EQ);
41815 rhs = cp_parser_assignment_expression (parser);
41816 orig_init = rhs;
41817 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
41818 decl, NOP_EXPR,
41819 rhs,
41820 tf_warning_or_error));
41821 if (!add_private_clause)
41822 add_private_clause = decl;
41824 else
41826 decl = NULL;
41827 cp_parser_abort_tentative_parse (parser);
41828 init = cp_parser_expression (parser);
41829 if (init)
41831 if (TREE_CODE (init) == MODIFY_EXPR
41832 || TREE_CODE (init) == MODOP_EXPR)
41833 real_decl = TREE_OPERAND (init, 0);
41837 return add_private_clause;
41840 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
41842 void
41843 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
41844 tree &decl, tree &orig_decl, tree &init,
41845 tree &orig_init, tree &cond, tree &incr)
41847 tree begin, end, range_temp_decl = NULL_TREE;
41848 tree iter_type, begin_expr, end_expr;
41850 if (processing_template_decl)
41852 if (check_for_bare_parameter_packs (init))
41853 init = error_mark_node;
41854 if (!type_dependent_expression_p (init)
41855 /* do_auto_deduction doesn't mess with template init-lists. */
41856 && !BRACE_ENCLOSED_INITIALIZER_P (init))
41858 tree d = decl;
41859 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
41861 tree v = DECL_VALUE_EXPR (decl);
41862 if (TREE_CODE (v) == ARRAY_REF
41863 && VAR_P (TREE_OPERAND (v, 0))
41864 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
41865 d = TREE_OPERAND (v, 0);
41867 do_range_for_auto_deduction (d, init);
41869 cond = global_namespace;
41870 incr = NULL_TREE;
41871 orig_init = init;
41872 if (this_pre_body)
41873 this_pre_body = pop_stmt_list (this_pre_body);
41874 return;
41877 init = mark_lvalue_use (init);
41879 if (decl == error_mark_node || init == error_mark_node)
41880 /* If an error happened previously do nothing or else a lot of
41881 unhelpful errors would be issued. */
41882 begin_expr = end_expr = iter_type = error_mark_node;
41883 else
41885 tree range_temp;
41887 if (VAR_P (init)
41888 && array_of_runtime_bound_p (TREE_TYPE (init)))
41889 /* Can't bind a reference to an array of runtime bound. */
41890 range_temp = init;
41891 else
41893 range_temp = build_range_temp (init);
41894 DECL_NAME (range_temp) = NULL_TREE;
41895 pushdecl (range_temp);
41896 cp_finish_decl (range_temp, init,
41897 /*is_constant_init*/false, NULL_TREE,
41898 LOOKUP_ONLYCONVERTING);
41899 range_temp_decl = range_temp;
41900 range_temp = convert_from_reference (range_temp);
41902 iter_type = cp_parser_perform_range_for_lookup (range_temp,
41903 &begin_expr, &end_expr);
41906 tree end_iter_type = iter_type;
41907 if (cxx_dialect >= cxx17)
41908 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
41909 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
41910 TREE_USED (end) = 1;
41911 DECL_ARTIFICIAL (end) = 1;
41912 pushdecl (end);
41913 cp_finish_decl (end, end_expr,
41914 /*is_constant_init*/false, NULL_TREE,
41915 LOOKUP_ONLYCONVERTING);
41917 /* The new for initialization statement. */
41918 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
41919 TREE_USED (begin) = 1;
41920 DECL_ARTIFICIAL (begin) = 1;
41921 pushdecl (begin);
41922 orig_init = init;
41923 if (CLASS_TYPE_P (iter_type))
41924 init = NULL_TREE;
41925 else
41927 init = begin_expr;
41928 begin_expr = NULL_TREE;
41930 cp_finish_decl (begin, begin_expr,
41931 /*is_constant_init*/false, NULL_TREE,
41932 LOOKUP_ONLYCONVERTING);
41934 /* The new for condition. */
41935 if (CLASS_TYPE_P (iter_type))
41936 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
41937 else
41938 cond = build_x_binary_op (input_location, NE_EXPR,
41939 begin, ERROR_MARK,
41940 end, ERROR_MARK,
41941 NULL, tf_warning_or_error);
41943 /* The new increment expression. */
41944 if (CLASS_TYPE_P (iter_type))
41945 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
41946 else
41947 incr = finish_unary_op_expr (input_location,
41948 PREINCREMENT_EXPR, begin,
41949 tf_warning_or_error);
41951 orig_decl = decl;
41952 decl = begin;
41953 if (for_block)
41955 vec_safe_push (for_block, this_pre_body);
41956 this_pre_body = NULL_TREE;
41959 tree decomp_first_name = NULL_TREE;
41960 unsigned decomp_cnt = 0;
41961 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
41963 tree v = DECL_VALUE_EXPR (orig_decl);
41964 if (TREE_CODE (v) == ARRAY_REF
41965 && VAR_P (TREE_OPERAND (v, 0))
41966 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
41968 tree d = orig_decl;
41969 orig_decl = TREE_OPERAND (v, 0);
41970 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
41971 decomp_first_name = d;
41975 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
41976 if (auto_node)
41978 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
41979 tf_none);
41980 if (!error_operand_p (t))
41981 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
41982 t, auto_node);
41985 tree v = make_tree_vec (decomp_cnt + 3);
41986 TREE_VEC_ELT (v, 0) = range_temp_decl;
41987 TREE_VEC_ELT (v, 1) = end;
41988 TREE_VEC_ELT (v, 2) = orig_decl;
41989 for (unsigned i = 0; i < decomp_cnt; i++)
41991 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
41992 decomp_first_name = DECL_CHAIN (decomp_first_name);
41994 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
41997 /* Helper for cp_parser_omp_for_loop, finalize part of range for
41998 inside of the collapsed body. */
42000 void
42001 cp_finish_omp_range_for (tree orig, tree begin)
42003 gcc_assert (TREE_CODE (orig) == TREE_LIST
42004 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
42005 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
42006 tree decomp_first_name = NULL_TREE;
42007 unsigned int decomp_cnt = 0;
42009 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42011 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
42012 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
42013 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
42016 /* The declaration is initialized with *__begin inside the loop body. */
42017 cp_finish_decl (decl,
42018 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42019 tf_warning_or_error),
42020 /*is_constant_init*/false, NULL_TREE,
42021 LOOKUP_ONLYCONVERTING);
42022 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42023 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
42026 /* Return true if next tokens contain a standard attribute that contains
42027 omp::directive (DIRECTIVE). */
42029 static bool
42030 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
42031 bool tentative)
42033 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
42034 if (n < 10)
42035 return false;
42036 for (i = 5; i < n - 4; i++)
42037 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
42038 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
42039 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
42041 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
42042 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
42043 if (strcmp (IDENTIFIER_POINTER (first), "directive"))
42044 continue;
42045 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
42046 break;
42048 if (i == n - 4)
42049 return false;
42050 cp_parser_parse_tentatively (parser);
42051 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
42052 location_t last_loc
42053 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
42054 location_t middle_loc = UNKNOWN_LOCATION;
42055 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
42056 int cnt = 0;
42057 bool seen = false;
42058 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
42059 if (get_attribute_namespace (attr) == omp_identifier
42060 && is_attribute_p ("directive", get_attribute_name (attr)))
42062 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
42064 tree d = TREE_VALUE (a);
42065 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
42066 cp_token *first = DEFPARSE_TOKENS (d)->first;
42067 cnt++;
42068 if (first->type == CPP_NAME
42069 && strcmp (IDENTIFIER_POINTER (first->u.value),
42070 directive) == 0)
42072 seen = true;
42073 if (middle_loc == UNKNOWN_LOCATION)
42074 middle_loc = first->location;
42078 if (!seen || tentative)
42080 cp_parser_abort_tentative_parse (parser);
42081 return seen;
42083 if (cnt != 1 || TREE_CHAIN (std_attrs))
42085 error_at (make_location (first_loc, last_loc, middle_loc),
42086 "%<[[omp::directive(%s)]]%> must be the only specified "
42087 "attribute on a statement", directive);
42088 cp_parser_abort_tentative_parse (parser);
42089 return false;
42091 if (!cp_parser_parse_definitely (parser))
42092 return false;
42093 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
42094 return true;
42097 /* Parse an OpenMP structured block sequence. KIND is the corresponding
42098 separating directive. */
42100 static tree
42101 cp_parser_omp_structured_block_sequence (cp_parser *parser,
42102 enum pragma_kind kind)
42104 tree stmt = begin_omp_structured_block ();
42105 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42107 cp_parser_statement (parser, NULL_TREE, false, NULL);
42108 while (true)
42110 cp_token *token = cp_lexer_peek_token (parser->lexer);
42112 if (token->type == CPP_CLOSE_BRACE
42113 || token->type == CPP_EOF
42114 || token->type == CPP_PRAGMA_EOL
42115 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
42116 || (kind != PRAGMA_NONE
42117 && cp_parser_pragma_kind (token) == kind))
42118 break;
42120 if (kind != PRAGMA_NONE
42121 && cp_parser_omp_section_scan (parser,
42122 kind == PRAGMA_OMP_SCAN
42123 ? "scan" : "section", false))
42124 break;
42126 cp_parser_statement (parser, NULL_TREE, false, NULL);
42129 cp_parser_end_omp_structured_block (parser, save);
42130 return finish_omp_structured_block (stmt);
42134 /* OpenMP 5.0:
42136 scan-loop-body:
42137 { structured-block scan-directive structured-block } */
42139 static void
42140 cp_parser_omp_scan_loop_body (cp_parser *parser)
42142 tree substmt, clauses = NULL_TREE;
42144 matching_braces braces;
42145 if (!braces.require_open (parser))
42146 return;
42148 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
42149 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
42150 add_stmt (substmt);
42152 cp_token *tok = cp_lexer_peek_token (parser->lexer);
42153 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
42155 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
42157 cp_lexer_consume_token (parser->lexer);
42159 if (parser->lexer->in_omp_attribute_pragma
42160 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42161 cp_lexer_consume_token (parser->lexer);
42163 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42165 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42166 const char *p = IDENTIFIER_POINTER (id);
42167 if (strcmp (p, "inclusive") == 0)
42168 clause = OMP_CLAUSE_INCLUSIVE;
42169 else if (strcmp (p, "exclusive") == 0)
42170 clause = OMP_CLAUSE_EXCLUSIVE;
42172 if (clause != OMP_CLAUSE_ERROR)
42174 cp_lexer_consume_token (parser->lexer);
42175 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
42177 else
42178 cp_parser_error (parser, "expected %<inclusive%> or "
42179 "%<exclusive%> clause");
42181 cp_parser_require_pragma_eol (parser, tok);
42183 else
42184 error ("expected %<#pragma omp scan%>");
42186 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42187 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
42188 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
42189 clauses);
42190 add_stmt (substmt);
42192 braces.require_close (parser);
42195 /* Parse the restricted form of the for statement allowed by OpenMP. */
42197 static tree
42198 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
42199 tree *cclauses, bool *if_p)
42201 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
42202 tree orig_decl;
42203 tree real_decl, initv, condv, incrv, declv, orig_declv;
42204 tree this_pre_body, cl, ordered_cl = NULL_TREE;
42205 location_t loc_first;
42206 bool collapse_err = false;
42207 int i, collapse = 1, ordered = 0, count, nbraces = 0;
42208 releasing_vec for_block;
42209 auto_vec<tree, 4> orig_inits;
42210 bool tiling = false;
42211 bool inscan = false;
42213 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
42214 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
42215 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
42216 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
42218 tiling = true;
42219 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
42221 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
42222 && OMP_CLAUSE_ORDERED_EXPR (cl))
42224 ordered_cl = cl;
42225 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
42227 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
42228 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
42229 && (code == OMP_SIMD || code == OMP_FOR))
42230 inscan = true;
42232 if (ordered && ordered < collapse)
42234 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42235 "%<ordered%> clause parameter is less than %<collapse%>");
42236 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
42237 = build_int_cst (NULL_TREE, collapse);
42238 ordered = collapse;
42240 if (ordered)
42242 for (tree *pc = &clauses; *pc; )
42243 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
42245 error_at (OMP_CLAUSE_LOCATION (*pc),
42246 "%<linear%> clause may not be specified together "
42247 "with %<ordered%> clause with a parameter");
42248 *pc = OMP_CLAUSE_CHAIN (*pc);
42250 else
42251 pc = &OMP_CLAUSE_CHAIN (*pc);
42254 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
42255 count = ordered ? ordered : collapse;
42257 declv = make_tree_vec (count);
42258 initv = make_tree_vec (count);
42259 condv = make_tree_vec (count);
42260 incrv = make_tree_vec (count);
42261 orig_declv = NULL_TREE;
42263 loc_first = cp_lexer_peek_token (parser->lexer)->location;
42265 for (i = 0; i < count; i++)
42267 int bracecount = 0;
42268 tree add_private_clause = NULL_TREE;
42269 location_t loc;
42271 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42273 if (!collapse_err)
42274 cp_parser_error (parser, "for statement expected");
42275 return NULL;
42277 loc = cp_lexer_consume_token (parser->lexer)->location;
42279 /* Don't create location wrapper nodes within an OpenMP "for"
42280 statement. */
42281 auto_suppress_location_wrappers sentinel;
42283 matching_parens parens;
42284 if (!parens.require_open (parser))
42285 return NULL;
42287 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
42288 this_pre_body = push_stmt_list ();
42290 if (code != OACC_LOOP && cxx_dialect >= cxx11)
42292 /* Save tokens so that we can put them back. */
42293 cp_lexer_save_tokens (parser->lexer);
42295 /* Look for ':' that is not nested in () or {}. */
42296 bool is_range_for
42297 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
42298 /*recovering=*/false,
42299 CPP_COLON,
42300 /*consume_paren=*/
42301 false) == -1);
42303 /* Roll back the tokens we skipped. */
42304 cp_lexer_rollback_tokens (parser->lexer);
42306 if (is_range_for)
42308 bool saved_colon_corrects_to_scope_p
42309 = parser->colon_corrects_to_scope_p;
42311 /* A colon is used in range-based for. */
42312 parser->colon_corrects_to_scope_p = false;
42314 /* Parse the declaration. */
42315 cp_parser_simple_declaration (parser,
42316 /*function_definition_allowed_p=*/
42317 false, &decl);
42318 parser->colon_corrects_to_scope_p
42319 = saved_colon_corrects_to_scope_p;
42321 cp_parser_require (parser, CPP_COLON, RT_COLON);
42323 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
42324 false, 0, true);
42326 cp_convert_omp_range_for (this_pre_body, for_block, decl,
42327 orig_decl, init, orig_init,
42328 cond, incr);
42329 if (this_pre_body)
42331 if (pre_body)
42333 tree t = pre_body;
42334 pre_body = push_stmt_list ();
42335 add_stmt (t);
42336 add_stmt (this_pre_body);
42337 pre_body = pop_stmt_list (pre_body);
42339 else
42340 pre_body = this_pre_body;
42343 if (ordered_cl)
42344 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42345 "%<ordered%> clause with parameter on "
42346 "range-based %<for%> loop");
42348 goto parse_close_paren;
42352 add_private_clause
42353 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
42354 init, orig_init, decl, real_decl);
42356 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42357 if (this_pre_body)
42359 this_pre_body = pop_stmt_list (this_pre_body);
42360 if (pre_body)
42362 tree t = pre_body;
42363 pre_body = push_stmt_list ();
42364 add_stmt (t);
42365 add_stmt (this_pre_body);
42366 pre_body = pop_stmt_list (pre_body);
42368 else
42369 pre_body = this_pre_body;
42372 if (decl)
42373 real_decl = decl;
42374 if (cclauses != NULL
42375 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
42376 && real_decl != NULL_TREE
42377 && code != OMP_LOOP)
42379 tree *c;
42380 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
42381 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
42382 && OMP_CLAUSE_DECL (*c) == real_decl)
42384 error_at (loc, "iteration variable %qD"
42385 " should not be firstprivate", real_decl);
42386 *c = OMP_CLAUSE_CHAIN (*c);
42388 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
42389 && OMP_CLAUSE_DECL (*c) == real_decl)
42391 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
42392 tree l = *c;
42393 *c = OMP_CLAUSE_CHAIN (*c);
42394 if (code == OMP_SIMD)
42396 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
42397 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
42399 else
42401 OMP_CLAUSE_CHAIN (l) = clauses;
42402 clauses = l;
42404 add_private_clause = NULL_TREE;
42406 else
42408 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
42409 && OMP_CLAUSE_DECL (*c) == real_decl)
42410 add_private_clause = NULL_TREE;
42411 c = &OMP_CLAUSE_CHAIN (*c);
42415 if (add_private_clause)
42417 tree c;
42418 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
42420 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
42421 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
42422 && OMP_CLAUSE_DECL (c) == decl)
42423 break;
42424 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
42425 && OMP_CLAUSE_DECL (c) == decl)
42426 error_at (loc, "iteration variable %qD "
42427 "should not be firstprivate",
42428 decl);
42429 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
42430 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
42431 && OMP_CLAUSE_DECL (c) == decl)
42432 error_at (loc, "iteration variable %qD should not be reduction",
42433 decl);
42435 if (c == NULL)
42437 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
42438 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
42439 else if (code != OMP_SIMD)
42440 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
42441 else
42442 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
42443 OMP_CLAUSE_DECL (c) = add_private_clause;
42444 c = finish_omp_clauses (c, C_ORT_OMP);
42445 if (c)
42447 OMP_CLAUSE_CHAIN (c) = clauses;
42448 clauses = c;
42449 /* For linear, signal that we need to fill up
42450 the so far unknown linear step. */
42451 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
42452 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
42457 cond = NULL;
42458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
42459 cond = cp_parser_omp_for_cond (parser, decl, code);
42460 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42462 incr = NULL;
42463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
42465 /* If decl is an iterator, preserve the operator on decl
42466 until finish_omp_for. */
42467 if (real_decl
42468 && ((processing_template_decl
42469 && (TREE_TYPE (real_decl) == NULL_TREE
42470 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
42471 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
42472 incr = cp_parser_omp_for_incr (parser, real_decl);
42473 else
42474 incr = cp_parser_expression (parser);
42475 protected_set_expr_location_if_unset (incr, input_location);
42478 parse_close_paren:
42479 if (!parens.require_close (parser))
42480 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42481 /*or_comma=*/false,
42482 /*consume_paren=*/true);
42484 TREE_VEC_ELT (declv, i) = decl;
42485 TREE_VEC_ELT (initv, i) = init;
42486 TREE_VEC_ELT (condv, i) = cond;
42487 TREE_VEC_ELT (incrv, i) = incr;
42488 if (orig_init)
42490 orig_inits.safe_grow_cleared (i + 1, true);
42491 orig_inits[i] = orig_init;
42493 if (orig_decl)
42495 if (!orig_declv)
42496 orig_declv = copy_node (declv);
42497 TREE_VEC_ELT (orig_declv, i) = orig_decl;
42499 else if (orig_declv)
42500 TREE_VEC_ELT (orig_declv, i) = decl;
42502 if (i == count - 1)
42503 break;
42505 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
42506 in between the collapsed for loops to be still considered perfectly
42507 nested. Hopefully the final version clarifies this.
42508 For now handle (multiple) {'s and empty statements. */
42509 cp_parser_parse_tentatively (parser);
42510 for (;;)
42512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42513 break;
42514 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42516 cp_lexer_consume_token (parser->lexer);
42517 bracecount++;
42519 else if (bracecount
42520 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42521 cp_lexer_consume_token (parser->lexer);
42522 else
42524 loc = cp_lexer_peek_token (parser->lexer)->location;
42525 error_at (loc, "not enough for loops to collapse");
42526 collapse_err = true;
42527 cp_parser_abort_tentative_parse (parser);
42528 declv = NULL_TREE;
42529 break;
42533 if (declv)
42535 cp_parser_parse_definitely (parser);
42536 nbraces += bracecount;
42540 if (nbraces)
42541 if_p = NULL;
42543 /* Note that we saved the original contents of this flag when we entered
42544 the structured block, and so we don't need to re-save it here. */
42545 parser->in_statement = IN_OMP_FOR;
42547 /* Note that the grammar doesn't call for a structured block here,
42548 though the loop as a whole is a structured block. */
42549 if (orig_declv)
42551 body = begin_omp_structured_block ();
42552 for (i = 0; i < count; i++)
42553 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
42554 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
42555 TREE_VEC_ELT (declv, i));
42557 else
42558 body = push_stmt_list ();
42559 if (inscan)
42560 cp_parser_omp_scan_loop_body (parser);
42561 else
42562 cp_parser_statement (parser, NULL_TREE, false, if_p);
42563 if (orig_declv)
42564 body = finish_omp_structured_block (body);
42565 else
42566 body = pop_stmt_list (body);
42568 if (declv == NULL_TREE)
42569 ret = NULL_TREE;
42570 else
42571 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
42572 incrv, body, pre_body, &orig_inits, clauses);
42574 while (nbraces)
42576 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42578 cp_lexer_consume_token (parser->lexer);
42579 nbraces--;
42581 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42582 cp_lexer_consume_token (parser->lexer);
42583 else
42585 if (!collapse_err)
42587 error_at (cp_lexer_peek_token (parser->lexer)->location,
42588 "collapsed loops not perfectly nested");
42590 collapse_err = true;
42591 cp_parser_statement_seq_opt (parser, NULL);
42592 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
42593 break;
42597 while (!for_block->is_empty ())
42599 tree t = for_block->pop ();
42600 if (TREE_CODE (t) == STATEMENT_LIST)
42601 add_stmt (pop_stmt_list (t));
42602 else
42603 add_stmt (t);
42606 return ret;
42609 /* Helper function for OpenMP parsing, split clauses and call
42610 finish_omp_clauses on each of the set of clauses afterwards. */
42612 static void
42613 cp_omp_split_clauses (location_t loc, enum tree_code code,
42614 omp_clause_mask mask, tree clauses, tree *cclauses)
42616 int i;
42617 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
42618 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
42619 if (cclauses[i])
42620 cclauses[i] = finish_omp_clauses (cclauses[i],
42621 i == C_OMP_CLAUSE_SPLIT_TARGET
42622 ? C_ORT_OMP_TARGET : C_ORT_OMP);
42625 /* OpenMP 5.0:
42626 #pragma omp loop loop-clause[optseq] new-line
42627 for-loop */
42629 #define OMP_LOOP_CLAUSE_MASK \
42630 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
42635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42637 static tree
42638 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
42639 char *p_name, omp_clause_mask mask, tree *cclauses,
42640 bool *if_p)
42642 tree clauses, sb, ret;
42643 unsigned int save;
42644 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42646 strcat (p_name, " loop");
42647 mask |= OMP_LOOP_CLAUSE_MASK;
42649 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42650 cclauses == NULL);
42651 if (cclauses)
42653 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
42654 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
42657 keep_next_level (true);
42658 sb = begin_omp_structured_block ();
42659 save = cp_parser_begin_omp_structured_block (parser);
42661 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
42663 cp_parser_end_omp_structured_block (parser, save);
42664 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42666 return ret;
42669 /* OpenMP 4.0:
42670 #pragma omp simd simd-clause[optseq] new-line
42671 for-loop */
42673 #define OMP_SIMD_CLAUSE_MASK \
42674 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
42675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
42676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
42677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
42678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
42683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
42684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42686 static tree
42687 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
42688 char *p_name, omp_clause_mask mask, tree *cclauses,
42689 bool *if_p)
42691 tree clauses, sb, ret;
42692 unsigned int save;
42693 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42695 strcat (p_name, " simd");
42696 mask |= OMP_SIMD_CLAUSE_MASK;
42698 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42699 cclauses == NULL);
42700 if (cclauses)
42702 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
42703 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
42704 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
42705 OMP_CLAUSE_ORDERED);
42706 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
42708 error_at (OMP_CLAUSE_LOCATION (c),
42709 "%<ordered%> clause with parameter may not be specified "
42710 "on %qs construct", p_name);
42711 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
42715 keep_next_level (true);
42716 sb = begin_omp_structured_block ();
42717 save = cp_parser_begin_omp_structured_block (parser);
42719 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
42721 cp_parser_end_omp_structured_block (parser, save);
42722 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42724 return ret;
42727 /* OpenMP 2.5:
42728 #pragma omp for for-clause[optseq] new-line
42729 for-loop
42731 OpenMP 4.0:
42732 #pragma omp for simd for-simd-clause[optseq] new-line
42733 for-loop */
42735 #define OMP_FOR_CLAUSE_MASK \
42736 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
42738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
42740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
42742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
42743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
42744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
42746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42748 static tree
42749 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
42750 char *p_name, omp_clause_mask mask, tree *cclauses,
42751 bool *if_p)
42753 tree clauses, sb, ret;
42754 unsigned int save;
42755 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42757 strcat (p_name, " for");
42758 mask |= OMP_FOR_CLAUSE_MASK;
42759 /* parallel for{, simd} disallows nowait clause, but for
42760 target {teams distribute ,}parallel for{, simd} it should be accepted. */
42761 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
42762 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
42763 /* Composite distribute parallel for{, simd} disallows ordered clause. */
42764 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
42765 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
42767 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42769 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42770 const char *p = IDENTIFIER_POINTER (id);
42772 if (strcmp (p, "simd") == 0)
42774 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
42775 if (cclauses == NULL)
42776 cclauses = cclauses_buf;
42778 cp_lexer_consume_token (parser->lexer);
42779 if (!flag_openmp) /* flag_openmp_simd */
42780 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
42781 cclauses, if_p);
42782 sb = begin_omp_structured_block ();
42783 save = cp_parser_begin_omp_structured_block (parser);
42784 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
42785 cclauses, if_p);
42786 cp_parser_end_omp_structured_block (parser, save);
42787 tree body = finish_omp_structured_block (sb);
42788 if (ret == NULL)
42789 return ret;
42790 ret = make_node (OMP_FOR);
42791 TREE_TYPE (ret) = void_type_node;
42792 OMP_FOR_BODY (ret) = body;
42793 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
42794 SET_EXPR_LOCATION (ret, loc);
42795 add_stmt (ret);
42796 return ret;
42799 if (!flag_openmp) /* flag_openmp_simd */
42801 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42802 return NULL_TREE;
42805 /* Composite distribute parallel for disallows linear clause. */
42806 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
42807 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
42809 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42810 cclauses == NULL);
42811 if (cclauses)
42813 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
42814 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
42817 keep_next_level (true);
42818 sb = begin_omp_structured_block ();
42819 save = cp_parser_begin_omp_structured_block (parser);
42821 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
42823 cp_parser_end_omp_structured_block (parser, save);
42824 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42826 return ret;
42829 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
42830 omp_clause_mask, tree *, bool *);
42832 /* OpenMP 2.5:
42833 # pragma omp master new-line
42834 structured-block */
42836 static tree
42837 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
42838 char *p_name, omp_clause_mask mask, tree *cclauses,
42839 bool *if_p)
42841 tree clauses, sb, ret;
42842 unsigned int save;
42843 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42845 strcat (p_name, " master");
42847 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42849 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42850 const char *p = IDENTIFIER_POINTER (id);
42852 if (strcmp (p, "taskloop") == 0)
42854 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
42855 if (cclauses == NULL)
42856 cclauses = cclauses_buf;
42858 cp_lexer_consume_token (parser->lexer);
42859 if (!flag_openmp) /* flag_openmp_simd */
42860 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
42861 cclauses, if_p);
42862 sb = begin_omp_structured_block ();
42863 save = cp_parser_begin_omp_structured_block (parser);
42864 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
42865 cclauses, if_p);
42866 cp_parser_end_omp_structured_block (parser, save);
42867 tree body = finish_omp_structured_block (sb);
42868 if (ret == NULL)
42869 return ret;
42870 ret = c_finish_omp_master (loc, body);
42871 OMP_MASTER_COMBINED (ret) = 1;
42872 return ret;
42875 if (!flag_openmp) /* flag_openmp_simd */
42877 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42878 return NULL_TREE;
42881 if (cclauses)
42883 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42884 false);
42885 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
42887 else
42888 cp_parser_require_pragma_eol (parser, pragma_tok);
42890 return c_finish_omp_master (loc,
42891 cp_parser_omp_structured_block (parser, if_p));
42894 /* OpenMP 5.1:
42895 # pragma omp masked masked-clauses new-line
42896 structured-block */
42898 #define OMP_MASKED_CLAUSE_MASK \
42899 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
42901 static tree
42902 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
42903 char *p_name, omp_clause_mask mask, tree *cclauses,
42904 bool *if_p)
42906 tree clauses, sb, ret;
42907 unsigned int save;
42908 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42910 strcat (p_name, " masked");
42911 mask |= OMP_MASKED_CLAUSE_MASK;
42913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42915 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42916 const char *p = IDENTIFIER_POINTER (id);
42918 if (strcmp (p, "taskloop") == 0)
42920 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
42921 if (cclauses == NULL)
42922 cclauses = cclauses_buf;
42924 cp_lexer_consume_token (parser->lexer);
42925 if (!flag_openmp) /* flag_openmp_simd */
42926 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
42927 cclauses, if_p);
42928 sb = begin_omp_structured_block ();
42929 save = cp_parser_begin_omp_structured_block (parser);
42930 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
42931 cclauses, if_p);
42932 cp_parser_end_omp_structured_block (parser, save);
42933 tree body = finish_omp_structured_block (sb);
42934 if (ret == NULL)
42935 return ret;
42936 ret = c_finish_omp_masked (loc, body,
42937 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
42938 OMP_MASKED_COMBINED (ret) = 1;
42939 return ret;
42942 if (!flag_openmp) /* flag_openmp_simd */
42944 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42945 return NULL_TREE;
42948 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42949 cclauses == NULL);
42950 if (cclauses)
42952 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
42953 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
42956 return c_finish_omp_masked (loc,
42957 cp_parser_omp_structured_block (parser, if_p),
42958 clauses);
42961 /* OpenMP 2.5:
42962 # pragma omp ordered new-line
42963 structured-block
42965 OpenMP 4.5:
42966 # pragma omp ordered ordered-clauses new-line
42967 structured-block */
42969 #define OMP_ORDERED_CLAUSE_MASK \
42970 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
42971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
42973 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
42974 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
42976 static bool
42977 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
42978 enum pragma_context context, bool *if_p)
42980 location_t loc = pragma_tok->location;
42981 int n = 1;
42983 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
42984 if (parser->lexer->in_omp_attribute_pragma
42985 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42986 n = 2;
42988 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
42990 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
42991 const char *p = IDENTIFIER_POINTER (id);
42993 if (strcmp (p, "depend") == 0)
42995 if (!flag_openmp) /* flag_openmp_simd */
42997 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42998 return false;
43000 if (context == pragma_stmt)
43002 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
43003 "%<depend%> clause may only be used in compound "
43004 "statements");
43005 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43006 return true;
43008 tree clauses
43009 = cp_parser_omp_all_clauses (parser,
43010 OMP_ORDERED_DEPEND_CLAUSE_MASK,
43011 "#pragma omp ordered", pragma_tok);
43012 c_finish_omp_ordered (loc, clauses, NULL_TREE);
43013 return false;
43017 tree clauses
43018 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
43019 "#pragma omp ordered", pragma_tok);
43021 if (!flag_openmp /* flag_openmp_simd */
43022 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
43023 return false;
43025 c_finish_omp_ordered (loc, clauses,
43026 cp_parser_omp_structured_block (parser, if_p));
43027 return true;
43030 /* OpenMP 2.5:
43032 section-scope:
43033 { section-sequence }
43035 section-sequence:
43036 section-directive[opt] structured-block
43037 section-sequence section-directive structured-block */
43039 static tree
43040 cp_parser_omp_sections_scope (cp_parser *parser)
43042 tree stmt, substmt;
43043 bool error_suppress = false;
43044 cp_token *tok;
43046 matching_braces braces;
43047 if (!braces.require_open (parser))
43048 return NULL_TREE;
43050 stmt = push_stmt_list ();
43052 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
43053 != PRAGMA_OMP_SECTION
43054 && !cp_parser_omp_section_scan (parser, "section", true))
43056 substmt = cp_parser_omp_structured_block_sequence (parser,
43057 PRAGMA_OMP_SECTION);
43058 substmt = build1 (OMP_SECTION, void_type_node, substmt);
43059 add_stmt (substmt);
43062 while (1)
43064 tok = cp_lexer_peek_token (parser->lexer);
43065 if (tok->type == CPP_CLOSE_BRACE)
43066 break;
43067 if (tok->type == CPP_EOF)
43068 break;
43070 if (cp_parser_omp_section_scan (parser, "section", false))
43071 tok = cp_lexer_peek_token (parser->lexer);
43072 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
43074 cp_lexer_consume_token (parser->lexer);
43075 cp_parser_require_pragma_eol (parser, tok);
43076 error_suppress = false;
43078 else if (!error_suppress)
43080 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
43081 error_suppress = true;
43084 substmt = cp_parser_omp_structured_block_sequence (parser,
43085 PRAGMA_OMP_SECTION);
43086 substmt = build1 (OMP_SECTION, void_type_node, substmt);
43087 add_stmt (substmt);
43089 braces.require_close (parser);
43091 substmt = pop_stmt_list (stmt);
43093 stmt = make_node (OMP_SECTIONS);
43094 TREE_TYPE (stmt) = void_type_node;
43095 OMP_SECTIONS_BODY (stmt) = substmt;
43097 add_stmt (stmt);
43098 return stmt;
43101 /* OpenMP 2.5:
43102 # pragma omp sections sections-clause[optseq] newline
43103 sections-scope */
43105 #define OMP_SECTIONS_CLAUSE_MASK \
43106 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43113 static tree
43114 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
43115 char *p_name, omp_clause_mask mask, tree *cclauses)
43117 tree clauses, ret;
43118 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43120 strcat (p_name, " sections");
43121 mask |= OMP_SECTIONS_CLAUSE_MASK;
43122 if (cclauses)
43123 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
43125 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43126 cclauses == NULL);
43127 if (cclauses)
43129 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
43130 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
43133 ret = cp_parser_omp_sections_scope (parser);
43134 if (ret)
43135 OMP_SECTIONS_CLAUSES (ret) = clauses;
43137 return ret;
43140 /* OpenMP 2.5:
43141 # pragma omp parallel parallel-clause[optseq] new-line
43142 structured-block
43143 # pragma omp parallel for parallel-for-clause[optseq] new-line
43144 structured-block
43145 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
43146 structured-block
43148 OpenMP 4.0:
43149 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
43150 structured-block */
43152 #define OMP_PARALLEL_CLAUSE_MASK \
43153 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
43159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
43161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
43164 static tree
43165 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
43166 char *p_name, omp_clause_mask mask, tree *cclauses,
43167 bool *if_p)
43169 tree stmt, clauses, block;
43170 unsigned int save;
43171 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43173 strcat (p_name, " parallel");
43174 mask |= OMP_PARALLEL_CLAUSE_MASK;
43175 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
43176 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
43177 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
43178 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
43180 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43182 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43183 if (cclauses == NULL)
43184 cclauses = cclauses_buf;
43186 cp_lexer_consume_token (parser->lexer);
43187 if (!flag_openmp) /* flag_openmp_simd */
43188 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43189 if_p);
43190 block = begin_omp_parallel ();
43191 save = cp_parser_begin_omp_structured_block (parser);
43192 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43193 if_p);
43194 cp_parser_end_omp_structured_block (parser, save);
43195 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43196 block);
43197 if (ret == NULL_TREE)
43198 return ret;
43199 OMP_PARALLEL_COMBINED (stmt) = 1;
43200 return stmt;
43202 /* When combined with distribute, parallel has to be followed by for.
43203 #pragma omp target parallel is allowed though. */
43204 else if (cclauses
43205 && (mask & (OMP_CLAUSE_MASK_1
43206 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43208 error_at (loc, "expected %<for%> after %qs", p_name);
43209 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43210 return NULL_TREE;
43212 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43214 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43215 const char *p = IDENTIFIER_POINTER (id);
43216 if (cclauses == NULL && strcmp (p, "masked") == 0)
43218 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43219 cclauses = cclauses_buf;
43221 cp_lexer_consume_token (parser->lexer);
43222 if (!flag_openmp) /* flag_openmp_simd */
43223 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43224 cclauses, if_p);
43225 block = begin_omp_parallel ();
43226 save = cp_parser_begin_omp_structured_block (parser);
43227 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43228 cclauses, if_p);
43229 cp_parser_end_omp_structured_block (parser, save);
43230 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43231 block);
43232 if (ret == NULL_TREE)
43233 return ret;
43234 /* masked does have just filter clause, but during gimplification
43235 isn't represented by a gimplification omp context, so for
43236 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
43237 so that
43238 #pragma omp parallel masked
43239 #pragma omp taskloop simd lastprivate (x)
43240 isn't confused with
43241 #pragma omp parallel masked taskloop simd lastprivate (x) */
43242 if (OMP_MASKED_COMBINED (ret))
43243 OMP_PARALLEL_COMBINED (stmt) = 1;
43244 return stmt;
43246 else if (cclauses == NULL && strcmp (p, "master") == 0)
43248 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43249 cclauses = cclauses_buf;
43251 cp_lexer_consume_token (parser->lexer);
43252 if (!flag_openmp) /* flag_openmp_simd */
43253 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43254 cclauses, if_p);
43255 block = begin_omp_parallel ();
43256 save = cp_parser_begin_omp_structured_block (parser);
43257 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43258 cclauses, if_p);
43259 cp_parser_end_omp_structured_block (parser, save);
43260 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43261 block);
43262 if (ret == NULL_TREE)
43263 return ret;
43264 /* master doesn't have any clauses and during gimplification
43265 isn't represented by a gimplification omp context, so for
43266 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
43267 so that
43268 #pragma omp parallel master
43269 #pragma omp taskloop simd lastprivate (x)
43270 isn't confused with
43271 #pragma omp parallel master taskloop simd lastprivate (x) */
43272 if (OMP_MASTER_COMBINED (ret))
43273 OMP_PARALLEL_COMBINED (stmt) = 1;
43274 return stmt;
43276 else if (strcmp (p, "loop") == 0)
43278 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43279 if (cclauses == NULL)
43280 cclauses = cclauses_buf;
43282 cp_lexer_consume_token (parser->lexer);
43283 if (!flag_openmp) /* flag_openmp_simd */
43284 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43285 cclauses, if_p);
43286 block = begin_omp_parallel ();
43287 save = cp_parser_begin_omp_structured_block (parser);
43288 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43289 cclauses, if_p);
43290 cp_parser_end_omp_structured_block (parser, save);
43291 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43292 block);
43293 if (ret == NULL_TREE)
43294 return ret;
43295 OMP_PARALLEL_COMBINED (stmt) = 1;
43296 return stmt;
43298 else if (!flag_openmp) /* flag_openmp_simd */
43300 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43301 return NULL_TREE;
43303 else if (cclauses == NULL && strcmp (p, "sections") == 0)
43305 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43306 cclauses = cclauses_buf;
43308 cp_lexer_consume_token (parser->lexer);
43309 block = begin_omp_parallel ();
43310 save = cp_parser_begin_omp_structured_block (parser);
43311 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
43312 cp_parser_end_omp_structured_block (parser, save);
43313 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43314 block);
43315 OMP_PARALLEL_COMBINED (stmt) = 1;
43316 return stmt;
43319 else if (!flag_openmp) /* flag_openmp_simd */
43321 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43322 return NULL_TREE;
43325 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43326 cclauses == NULL);
43327 if (cclauses)
43329 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
43330 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
43333 block = begin_omp_parallel ();
43334 save = cp_parser_begin_omp_structured_block (parser);
43335 parser->omp_attrs_forbidden_p = true;
43336 cp_parser_statement (parser, NULL_TREE, false, if_p);
43337 cp_parser_end_omp_structured_block (parser, save);
43338 stmt = finish_omp_parallel (clauses, block);
43339 return stmt;
43342 /* OpenMP 2.5:
43343 # pragma omp single single-clause[optseq] new-line
43344 structured-block */
43346 #define OMP_SINGLE_CLAUSE_MASK \
43347 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
43350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43353 static tree
43354 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43356 tree stmt = make_node (OMP_SINGLE);
43357 TREE_TYPE (stmt) = void_type_node;
43358 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43360 OMP_SINGLE_CLAUSES (stmt)
43361 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
43362 "#pragma omp single", pragma_tok);
43363 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43365 return add_stmt (stmt);
43368 /* OpenMP 5.1:
43369 # pragma omp scope scope-clause[optseq] new-line
43370 structured-block */
43372 #define OMP_SCOPE_CLAUSE_MASK \
43373 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43377 static tree
43378 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43380 tree stmt = make_node (OMP_SCOPE);
43381 TREE_TYPE (stmt) = void_type_node;
43382 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43384 OMP_SCOPE_CLAUSES (stmt)
43385 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
43386 "#pragma omp scope", pragma_tok);
43387 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43389 return add_stmt (stmt);
43392 /* OpenMP 3.0:
43393 # pragma omp task task-clause[optseq] new-line
43394 structured-block */
43396 #define OMP_TASK_CLAUSE_MASK \
43397 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
43399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
43404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
43405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
43407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
43409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
43410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
43412 static tree
43413 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43415 tree clauses, block;
43416 unsigned int save;
43418 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
43419 "#pragma omp task", pragma_tok);
43420 block = begin_omp_task ();
43421 save = cp_parser_begin_omp_structured_block (parser);
43422 parser->omp_attrs_forbidden_p = true;
43423 cp_parser_statement (parser, NULL_TREE, false, if_p);
43424 cp_parser_end_omp_structured_block (parser, save);
43425 return finish_omp_task (clauses, block);
43428 /* OpenMP 3.0:
43429 # pragma omp taskwait new-line
43431 OpenMP 5.0:
43432 # pragma omp taskwait taskwait-clause[opt] new-line */
43434 #define OMP_TASKWAIT_CLAUSE_MASK \
43435 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
43437 static void
43438 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
43440 tree clauses
43441 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
43442 "#pragma omp taskwait", pragma_tok);
43444 if (clauses)
43446 tree stmt = make_node (OMP_TASK);
43447 TREE_TYPE (stmt) = void_node;
43448 OMP_TASK_CLAUSES (stmt) = clauses;
43449 OMP_TASK_BODY (stmt) = NULL_TREE;
43450 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43451 add_stmt (stmt);
43453 else
43454 finish_omp_taskwait ();
43457 /* OpenMP 3.1:
43458 # pragma omp taskyield new-line */
43460 static void
43461 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
43463 cp_parser_require_pragma_eol (parser, pragma_tok);
43464 finish_omp_taskyield ();
43467 /* OpenMP 4.0:
43468 # pragma omp taskgroup new-line
43469 structured-block
43471 OpenMP 5.0:
43472 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
43474 #define OMP_TASKGROUP_CLAUSE_MASK \
43475 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
43478 static tree
43479 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43481 tree clauses
43482 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
43483 "#pragma omp taskgroup", pragma_tok);
43484 return c_finish_omp_taskgroup (input_location,
43485 cp_parser_omp_structured_block (parser,
43486 if_p),
43487 clauses);
43491 /* OpenMP 2.5:
43492 # pragma omp threadprivate (variable-list) */
43494 static void
43495 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
43497 tree vars;
43499 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43500 cp_parser_require_pragma_eol (parser, pragma_tok);
43502 finish_omp_threadprivate (vars);
43505 /* OpenMP 4.0:
43506 # pragma omp cancel cancel-clause[optseq] new-line */
43508 #define OMP_CANCEL_CLAUSE_MASK \
43509 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
43510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
43511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
43512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
43513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
43515 static void
43516 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
43518 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
43519 "#pragma omp cancel", pragma_tok);
43520 finish_omp_cancel (clauses);
43523 /* OpenMP 4.0:
43524 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
43526 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
43527 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
43528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
43529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
43530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
43532 static bool
43533 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
43534 enum pragma_context context)
43536 tree clauses;
43537 bool point_seen = false;
43539 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43541 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43542 const char *p = IDENTIFIER_POINTER (id);
43544 if (strcmp (p, "point") == 0)
43546 cp_lexer_consume_token (parser->lexer);
43547 point_seen = true;
43550 if (!point_seen)
43552 cp_parser_error (parser, "expected %<point%>");
43553 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43554 return false;
43557 if (context != pragma_compound)
43559 if (context == pragma_stmt)
43560 error_at (pragma_tok->location,
43561 "%<#pragma %s%> may only be used in compound statements",
43562 "omp cancellation point");
43563 else
43564 cp_parser_error (parser, "expected declaration specifiers");
43565 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43566 return true;
43569 clauses = cp_parser_omp_all_clauses (parser,
43570 OMP_CANCELLATION_POINT_CLAUSE_MASK,
43571 "#pragma omp cancellation point",
43572 pragma_tok);
43573 finish_omp_cancellation_point (clauses);
43574 return true;
43577 /* OpenMP 4.0:
43578 #pragma omp distribute distribute-clause[optseq] new-line
43579 for-loop */
43581 #define OMP_DISTRIBUTE_CLAUSE_MASK \
43582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
43586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43590 static tree
43591 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
43592 char *p_name, omp_clause_mask mask, tree *cclauses,
43593 bool *if_p)
43595 tree clauses, sb, ret;
43596 unsigned int save;
43597 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43599 strcat (p_name, " distribute");
43600 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
43602 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43604 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43605 const char *p = IDENTIFIER_POINTER (id);
43606 bool simd = false;
43607 bool parallel = false;
43609 if (strcmp (p, "simd") == 0)
43610 simd = true;
43611 else
43612 parallel = strcmp (p, "parallel") == 0;
43613 if (parallel || simd)
43615 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43616 if (cclauses == NULL)
43617 cclauses = cclauses_buf;
43618 cp_lexer_consume_token (parser->lexer);
43619 if (!flag_openmp) /* flag_openmp_simd */
43621 if (simd)
43622 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43623 cclauses, if_p);
43624 else
43625 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
43626 cclauses, if_p);
43628 sb = begin_omp_structured_block ();
43629 save = cp_parser_begin_omp_structured_block (parser);
43630 if (simd)
43631 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43632 cclauses, if_p);
43633 else
43634 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
43635 cclauses, if_p);
43636 cp_parser_end_omp_structured_block (parser, save);
43637 tree body = finish_omp_structured_block (sb);
43638 if (ret == NULL)
43639 return ret;
43640 ret = make_node (OMP_DISTRIBUTE);
43641 TREE_TYPE (ret) = void_type_node;
43642 OMP_FOR_BODY (ret) = body;
43643 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
43644 SET_EXPR_LOCATION (ret, loc);
43645 add_stmt (ret);
43646 return ret;
43649 if (!flag_openmp) /* flag_openmp_simd */
43651 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43652 return NULL_TREE;
43655 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43656 cclauses == NULL);
43657 if (cclauses)
43659 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
43660 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
43663 keep_next_level (true);
43664 sb = begin_omp_structured_block ();
43665 save = cp_parser_begin_omp_structured_block (parser);
43667 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
43669 cp_parser_end_omp_structured_block (parser, save);
43670 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43672 return ret;
43675 /* OpenMP 4.0:
43676 # pragma omp teams teams-clause[optseq] new-line
43677 structured-block */
43679 #define OMP_TEAMS_CLAUSE_MASK \
43680 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
43685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
43686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
43689 static tree
43690 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
43691 char *p_name, omp_clause_mask mask, tree *cclauses,
43692 bool *if_p)
43694 tree clauses, sb, ret;
43695 unsigned int save;
43696 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43698 strcat (p_name, " teams");
43699 mask |= OMP_TEAMS_CLAUSE_MASK;
43701 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43703 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43704 const char *p = IDENTIFIER_POINTER (id);
43705 if (strcmp (p, "distribute") == 0)
43707 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43708 if (cclauses == NULL)
43709 cclauses = cclauses_buf;
43711 cp_lexer_consume_token (parser->lexer);
43712 if (!flag_openmp) /* flag_openmp_simd */
43713 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
43714 cclauses, if_p);
43715 keep_next_level (true);
43716 sb = begin_omp_structured_block ();
43717 save = cp_parser_begin_omp_structured_block (parser);
43718 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
43719 cclauses, if_p);
43720 cp_parser_end_omp_structured_block (parser, save);
43721 tree body = finish_omp_structured_block (sb);
43722 if (ret == NULL)
43723 return ret;
43724 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43725 ret = make_node (OMP_TEAMS);
43726 TREE_TYPE (ret) = void_type_node;
43727 OMP_TEAMS_CLAUSES (ret) = clauses;
43728 OMP_TEAMS_BODY (ret) = body;
43729 OMP_TEAMS_COMBINED (ret) = 1;
43730 SET_EXPR_LOCATION (ret, loc);
43731 return add_stmt (ret);
43733 else if (strcmp (p, "loop") == 0)
43735 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43736 if (cclauses == NULL)
43737 cclauses = cclauses_buf;
43739 cp_lexer_consume_token (parser->lexer);
43740 if (!flag_openmp) /* flag_openmp_simd */
43741 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43742 cclauses, if_p);
43743 keep_next_level (true);
43744 sb = begin_omp_structured_block ();
43745 save = cp_parser_begin_omp_structured_block (parser);
43746 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43747 cclauses, if_p);
43748 cp_parser_end_omp_structured_block (parser, save);
43749 tree body = finish_omp_structured_block (sb);
43750 if (ret == NULL)
43751 return ret;
43752 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43753 ret = make_node (OMP_TEAMS);
43754 TREE_TYPE (ret) = void_type_node;
43755 OMP_TEAMS_CLAUSES (ret) = clauses;
43756 OMP_TEAMS_BODY (ret) = body;
43757 OMP_TEAMS_COMBINED (ret) = 1;
43758 SET_EXPR_LOCATION (ret, loc);
43759 return add_stmt (ret);
43762 if (!flag_openmp) /* flag_openmp_simd */
43764 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43765 return NULL_TREE;
43768 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43769 cclauses == NULL);
43770 if (cclauses)
43772 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
43773 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43776 tree stmt = make_node (OMP_TEAMS);
43777 TREE_TYPE (stmt) = void_type_node;
43778 OMP_TEAMS_CLAUSES (stmt) = clauses;
43779 keep_next_level (true);
43780 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43781 SET_EXPR_LOCATION (stmt, loc);
43783 return add_stmt (stmt);
43786 /* OpenMP 4.0:
43787 # pragma omp target data target-data-clause[optseq] new-line
43788 structured-block */
43790 #define OMP_TARGET_DATA_CLAUSE_MASK \
43791 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
43792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
43793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
43795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
43797 static tree
43798 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43800 tree clauses
43801 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
43802 "#pragma omp target data", pragma_tok);
43803 c_omp_adjust_map_clauses (clauses, false);
43804 int map_seen = 0;
43805 for (tree *pc = &clauses; *pc;)
43807 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
43808 switch (OMP_CLAUSE_MAP_KIND (*pc))
43810 case GOMP_MAP_TO:
43811 case GOMP_MAP_ALWAYS_TO:
43812 case GOMP_MAP_FROM:
43813 case GOMP_MAP_ALWAYS_FROM:
43814 case GOMP_MAP_TOFROM:
43815 case GOMP_MAP_ALWAYS_TOFROM:
43816 case GOMP_MAP_ALLOC:
43817 map_seen = 3;
43818 break;
43819 case GOMP_MAP_FIRSTPRIVATE_POINTER:
43820 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
43821 case GOMP_MAP_ALWAYS_POINTER:
43822 case GOMP_MAP_ATTACH_DETACH:
43823 break;
43824 default:
43825 map_seen |= 1;
43826 error_at (OMP_CLAUSE_LOCATION (*pc),
43827 "%<#pragma omp target data%> with map-type other "
43828 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
43829 "on %<map%> clause");
43830 *pc = OMP_CLAUSE_CHAIN (*pc);
43831 continue;
43833 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
43834 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
43835 map_seen = 3;
43836 pc = &OMP_CLAUSE_CHAIN (*pc);
43839 if (map_seen != 3)
43841 if (map_seen == 0)
43842 error_at (pragma_tok->location,
43843 "%<#pragma omp target data%> must contain at least "
43844 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
43845 "clause");
43846 return NULL_TREE;
43849 tree stmt = make_node (OMP_TARGET_DATA);
43850 TREE_TYPE (stmt) = void_type_node;
43851 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
43853 keep_next_level (true);
43854 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43856 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43857 return add_stmt (stmt);
43860 /* OpenMP 4.5:
43861 # pragma omp target enter data target-enter-data-clause[optseq] new-line
43862 structured-block */
43864 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
43865 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
43866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
43867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43871 static bool
43872 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
43873 enum pragma_context context)
43875 bool data_seen = false;
43876 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43878 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43879 const char *p = IDENTIFIER_POINTER (id);
43881 if (strcmp (p, "data") == 0)
43883 cp_lexer_consume_token (parser->lexer);
43884 data_seen = true;
43887 if (!data_seen)
43889 cp_parser_error (parser, "expected %<data%>");
43890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43891 return false;
43894 if (context == pragma_stmt)
43896 error_at (pragma_tok->location,
43897 "%<#pragma %s%> may only be used in compound statements",
43898 "omp target enter data");
43899 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43900 return true;
43903 tree clauses
43904 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
43905 "#pragma omp target enter data", pragma_tok);
43906 c_omp_adjust_map_clauses (clauses, false);
43907 int map_seen = 0;
43908 for (tree *pc = &clauses; *pc;)
43910 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
43911 switch (OMP_CLAUSE_MAP_KIND (*pc))
43913 case GOMP_MAP_TO:
43914 case GOMP_MAP_ALWAYS_TO:
43915 case GOMP_MAP_ALLOC:
43916 map_seen = 3;
43917 break;
43918 case GOMP_MAP_FIRSTPRIVATE_POINTER:
43919 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
43920 case GOMP_MAP_ALWAYS_POINTER:
43921 case GOMP_MAP_ATTACH_DETACH:
43922 break;
43923 default:
43924 map_seen |= 1;
43925 error_at (OMP_CLAUSE_LOCATION (*pc),
43926 "%<#pragma omp target enter data%> with map-type other "
43927 "than %<to%> or %<alloc%> on %<map%> clause");
43928 *pc = OMP_CLAUSE_CHAIN (*pc);
43929 continue;
43931 pc = &OMP_CLAUSE_CHAIN (*pc);
43934 if (map_seen != 3)
43936 if (map_seen == 0)
43937 error_at (pragma_tok->location,
43938 "%<#pragma omp target enter data%> must contain at least "
43939 "one %<map%> clause");
43940 return true;
43943 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
43944 TREE_TYPE (stmt) = void_type_node;
43945 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
43946 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43947 add_stmt (stmt);
43948 return true;
43951 /* OpenMP 4.5:
43952 # pragma omp target exit data target-enter-data-clause[optseq] new-line
43953 structured-block */
43955 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
43956 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
43957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
43958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43962 static bool
43963 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
43964 enum pragma_context context)
43966 bool data_seen = false;
43967 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43969 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43970 const char *p = IDENTIFIER_POINTER (id);
43972 if (strcmp (p, "data") == 0)
43974 cp_lexer_consume_token (parser->lexer);
43975 data_seen = true;
43978 if (!data_seen)
43980 cp_parser_error (parser, "expected %<data%>");
43981 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43982 return false;
43985 if (context == pragma_stmt)
43987 error_at (pragma_tok->location,
43988 "%<#pragma %s%> may only be used in compound statements",
43989 "omp target exit data");
43990 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43991 return true;
43994 tree clauses
43995 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
43996 "#pragma omp target exit data", pragma_tok);
43997 c_omp_adjust_map_clauses (clauses, false);
43998 int map_seen = 0;
43999 for (tree *pc = &clauses; *pc;)
44001 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44002 switch (OMP_CLAUSE_MAP_KIND (*pc))
44004 case GOMP_MAP_FROM:
44005 case GOMP_MAP_ALWAYS_FROM:
44006 case GOMP_MAP_RELEASE:
44007 case GOMP_MAP_DELETE:
44008 map_seen = 3;
44009 break;
44010 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44011 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44012 case GOMP_MAP_ALWAYS_POINTER:
44013 case GOMP_MAP_ATTACH_DETACH:
44014 break;
44015 default:
44016 map_seen |= 1;
44017 error_at (OMP_CLAUSE_LOCATION (*pc),
44018 "%<#pragma omp target exit data%> with map-type other "
44019 "than %<from%>, %<release%> or %<delete%> on %<map%>"
44020 " clause");
44021 *pc = OMP_CLAUSE_CHAIN (*pc);
44022 continue;
44024 pc = &OMP_CLAUSE_CHAIN (*pc);
44027 if (map_seen != 3)
44029 if (map_seen == 0)
44030 error_at (pragma_tok->location,
44031 "%<#pragma omp target exit data%> must contain at least "
44032 "one %<map%> clause");
44033 return true;
44036 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
44037 TREE_TYPE (stmt) = void_type_node;
44038 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
44039 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44040 add_stmt (stmt);
44041 return true;
44044 /* OpenMP 4.0:
44045 # pragma omp target update target-update-clause[optseq] new-line */
44047 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
44048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
44049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
44050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44055 static bool
44056 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
44057 enum pragma_context context)
44059 if (context == pragma_stmt)
44061 error_at (pragma_tok->location,
44062 "%<#pragma %s%> may only be used in compound statements",
44063 "omp target update");
44064 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44065 return true;
44068 tree clauses
44069 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
44070 "#pragma omp target update", pragma_tok);
44071 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
44072 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
44074 error_at (pragma_tok->location,
44075 "%<#pragma omp target update%> must contain at least one "
44076 "%<from%> or %<to%> clauses");
44077 return true;
44080 tree stmt = make_node (OMP_TARGET_UPDATE);
44081 TREE_TYPE (stmt) = void_type_node;
44082 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
44083 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44084 add_stmt (stmt);
44085 return true;
44088 /* OpenMP 4.0:
44089 # pragma omp target target-clause[optseq] new-line
44090 structured-block */
44092 #define OMP_TARGET_CLAUSE_MASK \
44093 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
44098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
44101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
44104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
44106 static bool
44107 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
44108 enum pragma_context context, bool *if_p)
44110 tree *pc = NULL, stmt;
44112 if (flag_openmp)
44113 omp_requires_mask
44114 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44116 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44118 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44119 const char *p = IDENTIFIER_POINTER (id);
44120 enum tree_code ccode = ERROR_MARK;
44122 if (strcmp (p, "teams") == 0)
44123 ccode = OMP_TEAMS;
44124 else if (strcmp (p, "parallel") == 0)
44125 ccode = OMP_PARALLEL;
44126 else if (strcmp (p, "simd") == 0)
44127 ccode = OMP_SIMD;
44128 if (ccode != ERROR_MARK)
44130 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
44131 char p_name[sizeof ("#pragma omp target teams distribute "
44132 "parallel for simd")];
44134 cp_lexer_consume_token (parser->lexer);
44135 strcpy (p_name, "#pragma omp target");
44136 if (!flag_openmp) /* flag_openmp_simd */
44138 tree stmt;
44139 switch (ccode)
44141 case OMP_TEAMS:
44142 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
44143 OMP_TARGET_CLAUSE_MASK,
44144 cclauses, if_p);
44145 break;
44146 case OMP_PARALLEL:
44147 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44148 OMP_TARGET_CLAUSE_MASK,
44149 cclauses, if_p);
44150 break;
44151 case OMP_SIMD:
44152 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
44153 OMP_TARGET_CLAUSE_MASK,
44154 cclauses, if_p);
44155 break;
44156 default:
44157 gcc_unreachable ();
44159 return stmt != NULL_TREE;
44161 keep_next_level (true);
44162 tree sb = begin_omp_structured_block (), ret;
44163 unsigned save = cp_parser_begin_omp_structured_block (parser);
44164 switch (ccode)
44166 case OMP_TEAMS:
44167 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
44168 OMP_TARGET_CLAUSE_MASK, cclauses,
44169 if_p);
44170 break;
44171 case OMP_PARALLEL:
44172 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44173 OMP_TARGET_CLAUSE_MASK, cclauses,
44174 if_p);
44175 break;
44176 case OMP_SIMD:
44177 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
44178 OMP_TARGET_CLAUSE_MASK, cclauses,
44179 if_p);
44180 break;
44181 default:
44182 gcc_unreachable ();
44184 cp_parser_end_omp_structured_block (parser, save);
44185 tree body = finish_omp_structured_block (sb);
44186 if (ret == NULL_TREE)
44187 return false;
44188 if (ccode == OMP_TEAMS && !processing_template_decl)
44189 /* For combined target teams, ensure the num_teams and
44190 thread_limit clause expressions are evaluated on the host,
44191 before entering the target construct. */
44192 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44193 c; c = OMP_CLAUSE_CHAIN (c))
44194 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
44195 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
44196 for (int i = 0;
44197 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
44198 if (OMP_CLAUSE_OPERAND (c, i)
44199 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
44201 tree expr = OMP_CLAUSE_OPERAND (c, i);
44202 expr = force_target_expr (TREE_TYPE (expr), expr,
44203 tf_none);
44204 if (expr == error_mark_node)
44205 continue;
44206 tree tmp = TARGET_EXPR_SLOT (expr);
44207 add_stmt (expr);
44208 OMP_CLAUSE_OPERAND (c, i) = expr;
44209 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
44210 OMP_CLAUSE_FIRSTPRIVATE);
44211 OMP_CLAUSE_DECL (tc) = tmp;
44212 OMP_CLAUSE_CHAIN (tc)
44213 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
44214 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
44216 tree stmt = make_node (OMP_TARGET);
44217 TREE_TYPE (stmt) = void_type_node;
44218 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
44219 c_omp_adjust_map_clauses (OMP_TARGET_CLAUSES (stmt), true);
44220 OMP_TARGET_BODY (stmt) = body;
44221 OMP_TARGET_COMBINED (stmt) = 1;
44222 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44223 add_stmt (stmt);
44224 pc = &OMP_TARGET_CLAUSES (stmt);
44225 goto check_clauses;
44227 else if (!flag_openmp) /* flag_openmp_simd */
44229 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44230 return false;
44232 else if (strcmp (p, "data") == 0)
44234 cp_lexer_consume_token (parser->lexer);
44235 cp_parser_omp_target_data (parser, pragma_tok, if_p);
44236 return true;
44238 else if (strcmp (p, "enter") == 0)
44240 cp_lexer_consume_token (parser->lexer);
44241 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
44243 else if (strcmp (p, "exit") == 0)
44245 cp_lexer_consume_token (parser->lexer);
44246 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
44248 else if (strcmp (p, "update") == 0)
44250 cp_lexer_consume_token (parser->lexer);
44251 return cp_parser_omp_target_update (parser, pragma_tok, context);
44254 if (!flag_openmp) /* flag_openmp_simd */
44256 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44257 return false;
44260 stmt = make_node (OMP_TARGET);
44261 TREE_TYPE (stmt) = void_type_node;
44263 OMP_TARGET_CLAUSES (stmt)
44264 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
44265 "#pragma omp target", pragma_tok, false);
44266 for (tree c = OMP_TARGET_CLAUSES (stmt); c; c = OMP_CLAUSE_CHAIN (c))
44267 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44269 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
44270 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
44271 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
44272 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
44273 OMP_CLAUSE_CHAIN (c) = nc;
44275 OMP_TARGET_CLAUSES (stmt)
44276 = finish_omp_clauses (OMP_TARGET_CLAUSES (stmt), C_ORT_OMP_TARGET);
44277 c_omp_adjust_map_clauses (OMP_TARGET_CLAUSES (stmt), true);
44279 pc = &OMP_TARGET_CLAUSES (stmt);
44280 keep_next_level (true);
44281 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44283 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44284 add_stmt (stmt);
44286 check_clauses:
44287 while (*pc)
44289 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44290 switch (OMP_CLAUSE_MAP_KIND (*pc))
44292 case GOMP_MAP_TO:
44293 case GOMP_MAP_ALWAYS_TO:
44294 case GOMP_MAP_FROM:
44295 case GOMP_MAP_ALWAYS_FROM:
44296 case GOMP_MAP_TOFROM:
44297 case GOMP_MAP_ALWAYS_TOFROM:
44298 case GOMP_MAP_ALLOC:
44299 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44300 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44301 case GOMP_MAP_ALWAYS_POINTER:
44302 case GOMP_MAP_ATTACH_DETACH:
44303 break;
44304 default:
44305 error_at (OMP_CLAUSE_LOCATION (*pc),
44306 "%<#pragma omp target%> with map-type other "
44307 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
44308 "on %<map%> clause");
44309 *pc = OMP_CLAUSE_CHAIN (*pc);
44310 continue;
44312 pc = &OMP_CLAUSE_CHAIN (*pc);
44314 return true;
44317 /* OpenACC 2.0:
44318 # pragma acc cache (variable-list) new-line
44321 static tree
44322 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
44324 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
44325 clauses. */
44326 auto_suppress_location_wrappers sentinel;
44328 tree stmt, clauses;
44330 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
44331 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
44333 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
44335 stmt = make_node (OACC_CACHE);
44336 TREE_TYPE (stmt) = void_type_node;
44337 OACC_CACHE_CLAUSES (stmt) = clauses;
44338 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44339 add_stmt (stmt);
44341 return stmt;
44344 /* OpenACC 2.0:
44345 # pragma acc data oacc-data-clause[optseq] new-line
44346 structured-block */
44348 #define OACC_DATA_CLAUSE_MASK \
44349 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
44355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44360 static tree
44361 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44363 tree stmt, clauses, block;
44364 unsigned int save;
44366 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
44367 "#pragma acc data", pragma_tok);
44369 block = begin_omp_parallel ();
44370 save = cp_parser_begin_omp_structured_block (parser);
44371 cp_parser_statement (parser, NULL_TREE, false, if_p);
44372 cp_parser_end_omp_structured_block (parser, save);
44373 stmt = finish_oacc_data (clauses, block);
44374 return stmt;
44377 /* OpenACC 2.0:
44378 # pragma acc host_data <clauses> new-line
44379 structured-block */
44381 #define OACC_HOST_DATA_CLAUSE_MASK \
44382 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
44383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
44386 static tree
44387 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44389 tree stmt, clauses, block;
44390 unsigned int save;
44392 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
44393 "#pragma acc host_data", pragma_tok);
44395 block = begin_omp_parallel ();
44396 save = cp_parser_begin_omp_structured_block (parser);
44397 cp_parser_statement (parser, NULL_TREE, false, if_p);
44398 cp_parser_end_omp_structured_block (parser, save);
44399 stmt = finish_oacc_host_data (clauses, block);
44400 return stmt;
44403 /* OpenACC 2.0:
44404 # pragma acc declare oacc-data-clause[optseq] new-line
44407 #define OACC_DECLARE_CLAUSE_MASK \
44408 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
44414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
44415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44417 static tree
44418 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
44420 tree clauses, stmt;
44421 bool error = false;
44422 bool found_in_scope = global_bindings_p ();
44424 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
44425 "#pragma acc declare", pragma_tok, true);
44428 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44430 error_at (pragma_tok->location,
44431 "no valid clauses specified in %<#pragma acc declare%>");
44432 return NULL_TREE;
44435 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
44437 location_t loc = OMP_CLAUSE_LOCATION (t);
44438 tree decl = OMP_CLAUSE_DECL (t);
44439 if (!DECL_P (decl))
44441 error_at (loc, "array section in %<#pragma acc declare%>");
44442 error = true;
44443 continue;
44445 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
44446 switch (OMP_CLAUSE_MAP_KIND (t))
44448 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44449 case GOMP_MAP_ALLOC:
44450 case GOMP_MAP_TO:
44451 case GOMP_MAP_FORCE_DEVICEPTR:
44452 case GOMP_MAP_DEVICE_RESIDENT:
44453 break;
44455 case GOMP_MAP_LINK:
44456 if (!global_bindings_p ()
44457 && (TREE_STATIC (decl)
44458 || !DECL_EXTERNAL (decl)))
44460 error_at (loc,
44461 "%qD must be a global variable in "
44462 "%<#pragma acc declare link%>",
44463 decl);
44464 error = true;
44465 continue;
44467 break;
44469 default:
44470 if (global_bindings_p ())
44472 error_at (loc, "invalid OpenACC clause at file scope");
44473 error = true;
44474 continue;
44476 if (DECL_EXTERNAL (decl))
44478 error_at (loc,
44479 "invalid use of %<extern%> variable %qD "
44480 "in %<#pragma acc declare%>", decl);
44481 error = true;
44482 continue;
44484 else if (TREE_PUBLIC (decl))
44486 error_at (loc,
44487 "invalid use of %<global%> variable %qD "
44488 "in %<#pragma acc declare%>", decl);
44489 error = true;
44490 continue;
44492 break;
44495 if (!found_in_scope)
44496 /* This seems to ignore the existence of cleanup scopes?
44497 What is the meaning for local extern decls? The local
44498 extern is in this scope, but it is referring to a decl that
44499 is namespace scope. */
44500 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
44501 if (d == decl)
44503 found_in_scope = true;
44504 break;
44506 if (!found_in_scope)
44508 error_at (loc,
44509 "%qD must be a variable declared in the same scope as "
44510 "%<#pragma acc declare%>", decl);
44511 error = true;
44512 continue;
44515 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
44516 || lookup_attribute ("omp declare target link",
44517 DECL_ATTRIBUTES (decl)))
44519 error_at (loc, "variable %qD used more than once with "
44520 "%<#pragma acc declare%>", decl);
44521 error = true;
44522 continue;
44525 if (!error)
44527 tree id;
44529 if (DECL_LOCAL_DECL_P (decl))
44530 /* We need to mark the aliased decl, as that is the entity
44531 that is being referred to. This won't work for
44532 dependent variables, but it didn't work for them before
44533 DECL_LOCAL_DECL_P was a thing either. But then
44534 dependent local extern variable decls are as rare as
44535 hen's teeth. */
44536 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
44537 if (alias != error_mark_node)
44538 decl = alias;
44540 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
44541 id = get_identifier ("omp declare target link");
44542 else
44543 id = get_identifier ("omp declare target");
44545 DECL_ATTRIBUTES (decl)
44546 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
44547 if (current_binding_level->kind == sk_namespace)
44549 symtab_node *node = symtab_node::get (decl);
44550 if (node != NULL)
44552 node->offloadable = 1;
44553 if (ENABLE_OFFLOADING)
44555 g->have_offload = true;
44556 if (is_a <varpool_node *> (node))
44557 vec_safe_push (offload_vars, decl);
44564 if (error || current_binding_level->kind == sk_namespace)
44565 return NULL_TREE;
44567 stmt = make_node (OACC_DECLARE);
44568 TREE_TYPE (stmt) = void_type_node;
44569 OACC_DECLARE_CLAUSES (stmt) = clauses;
44570 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44572 add_stmt (stmt);
44574 return NULL_TREE;
44577 /* OpenACC 2.0:
44578 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
44582 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
44584 LOC is the location of the #pragma token.
44587 #define OACC_ENTER_DATA_CLAUSE_MASK \
44588 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44595 #define OACC_EXIT_DATA_CLAUSE_MASK \
44596 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
44600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
44601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
44602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44604 static tree
44605 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
44606 bool enter)
44608 location_t loc = pragma_tok->location;
44609 tree stmt, clauses;
44610 const char *p = "";
44612 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44613 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
44615 if (strcmp (p, "data") != 0)
44617 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
44618 enter ? "enter" : "exit");
44619 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44620 return NULL_TREE;
44623 cp_lexer_consume_token (parser->lexer);
44625 if (enter)
44626 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
44627 "#pragma acc enter data", pragma_tok);
44628 else
44629 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
44630 "#pragma acc exit data", pragma_tok);
44632 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44634 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
44635 enter ? "enter" : "exit");
44636 return NULL_TREE;
44639 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
44640 TREE_TYPE (stmt) = void_type_node;
44641 OMP_STANDALONE_CLAUSES (stmt) = clauses;
44642 SET_EXPR_LOCATION (stmt, loc);
44643 add_stmt (stmt);
44644 return stmt;
44647 /* OpenACC 2.0:
44648 # pragma acc loop oacc-loop-clause[optseq] new-line
44649 structured-block */
44651 #define OACC_LOOP_CLAUSE_MASK \
44652 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
44653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
44654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
44655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
44656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
44657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
44658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
44659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
44660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
44661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
44663 static tree
44664 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
44665 omp_clause_mask mask, tree *cclauses, bool *if_p)
44667 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
44669 strcat (p_name, " loop");
44670 mask |= OACC_LOOP_CLAUSE_MASK;
44672 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
44673 cclauses == NULL);
44674 if (cclauses)
44676 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
44677 if (*cclauses)
44678 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
44679 if (clauses)
44680 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
44683 tree block = begin_omp_structured_block ();
44684 int save = cp_parser_begin_omp_structured_block (parser);
44685 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
44686 cp_parser_end_omp_structured_block (parser, save);
44687 add_stmt (finish_omp_structured_block (block));
44689 return stmt;
44692 /* OpenACC 2.0:
44693 # pragma acc kernels oacc-kernels-clause[optseq] new-line
44694 structured-block
44698 # pragma acc parallel oacc-parallel-clause[optseq] new-line
44699 structured-block
44701 OpenACC 2.6:
44703 # pragma acc serial oacc-serial-clause[optseq] new-line
44706 #define OACC_KERNELS_CLAUSE_MASK \
44707 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
44714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
44718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
44719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
44720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
44721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44723 #define OACC_PARALLEL_CLAUSE_MASK \
44724 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
44731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
44733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
44736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
44737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
44738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
44739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
44740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
44741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44743 #define OACC_SERIAL_CLAUSE_MASK \
44744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
44751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
44755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
44756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
44757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
44758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44760 static tree
44761 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
44762 char *p_name, bool *if_p)
44764 omp_clause_mask mask;
44765 enum tree_code code;
44766 switch (cp_parser_pragma_kind (pragma_tok))
44768 case PRAGMA_OACC_KERNELS:
44769 strcat (p_name, " kernels");
44770 mask = OACC_KERNELS_CLAUSE_MASK;
44771 code = OACC_KERNELS;
44772 break;
44773 case PRAGMA_OACC_PARALLEL:
44774 strcat (p_name, " parallel");
44775 mask = OACC_PARALLEL_CLAUSE_MASK;
44776 code = OACC_PARALLEL;
44777 break;
44778 case PRAGMA_OACC_SERIAL:
44779 strcat (p_name, " serial");
44780 mask = OACC_SERIAL_CLAUSE_MASK;
44781 code = OACC_SERIAL;
44782 break;
44783 default:
44784 gcc_unreachable ();
44787 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44789 const char *p
44790 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
44791 if (strcmp (p, "loop") == 0)
44793 cp_lexer_consume_token (parser->lexer);
44794 tree block = begin_omp_parallel ();
44795 tree clauses;
44796 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
44797 &clauses, if_p);
44798 protected_set_expr_location (stmt, pragma_tok->location);
44799 return finish_omp_construct (code, block, clauses);
44803 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
44805 tree block = begin_omp_parallel ();
44806 unsigned int save = cp_parser_begin_omp_structured_block (parser);
44807 cp_parser_statement (parser, NULL_TREE, false, if_p);
44808 cp_parser_end_omp_structured_block (parser, save);
44809 return finish_omp_construct (code, block, clauses);
44812 /* OpenACC 2.0:
44813 # pragma acc update oacc-update-clause[optseq] new-line
44816 #define OACC_UPDATE_CLAUSE_MASK \
44817 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
44819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
44820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
44822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
44824 static tree
44825 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
44827 tree stmt, clauses;
44829 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
44830 "#pragma acc update", pragma_tok);
44832 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44834 error_at (pragma_tok->location,
44835 "%<#pragma acc update%> must contain at least one "
44836 "%<device%> or %<host%> or %<self%> clause");
44837 return NULL_TREE;
44840 stmt = make_node (OACC_UPDATE);
44841 TREE_TYPE (stmt) = void_type_node;
44842 OACC_UPDATE_CLAUSES (stmt) = clauses;
44843 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44844 add_stmt (stmt);
44845 return stmt;
44848 /* OpenACC 2.0:
44849 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
44851 LOC is the location of the #pragma token.
44854 #define OACC_WAIT_CLAUSE_MASK \
44855 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
44857 static tree
44858 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
44860 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
44861 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44863 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
44864 list = cp_parser_oacc_wait_list (parser, loc, list);
44866 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
44867 "#pragma acc wait", pragma_tok);
44869 stmt = c_finish_oacc_wait (loc, list, clauses);
44870 stmt = finish_expr_stmt (stmt);
44872 return stmt;
44875 /* OpenMP 4.0:
44876 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
44878 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
44879 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
44880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
44882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
44883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
44884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
44886 static void
44887 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
44888 enum pragma_context context,
44889 bool variant_p)
44891 bool first_p = parser->omp_declare_simd == NULL;
44892 cp_omp_declare_simd_data data;
44893 if (first_p)
44895 data.error_seen = false;
44896 data.fndecl_seen = false;
44897 data.variant_p = variant_p;
44898 data.tokens = vNULL;
44899 data.attribs[0] = NULL;
44900 data.attribs[1] = NULL;
44901 data.loc = UNKNOWN_LOCATION;
44902 /* It is safe to take the address of a local variable; it will only be
44903 used while this scope is live. */
44904 parser->omp_declare_simd = &data;
44906 else if (parser->omp_declare_simd->variant_p != variant_p)
44908 error_at (pragma_tok->location,
44909 "%<#pragma omp declare %s%> followed by "
44910 "%<#pragma omp declare %s%>",
44911 parser->omp_declare_simd->variant_p ? "variant" : "simd",
44912 parser->omp_declare_simd->variant_p ? "simd" : "variant");
44913 parser->omp_declare_simd->error_seen = true;
44916 /* Store away all pragma tokens. */
44917 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
44918 cp_lexer_consume_token (parser->lexer);
44919 cp_parser_require_pragma_eol (parser, pragma_tok);
44920 struct cp_token_cache *cp
44921 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
44922 parser->omp_declare_simd->tokens.safe_push (cp);
44924 if (first_p)
44926 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
44927 cp_parser_pragma (parser, context, NULL);
44928 switch (context)
44930 case pragma_external:
44931 cp_parser_declaration (parser, NULL_TREE);
44932 break;
44933 case pragma_member:
44934 cp_parser_member_declaration (parser);
44935 break;
44936 case pragma_objc_icode:
44937 cp_parser_block_declaration (parser, /*statement_p=*/false);
44938 break;
44939 default:
44940 cp_parser_declaration_statement (parser);
44941 break;
44943 if (parser->omp_declare_simd
44944 && !parser->omp_declare_simd->error_seen
44945 && !parser->omp_declare_simd->fndecl_seen)
44946 error_at (pragma_tok->location,
44947 "%<#pragma omp declare %s%> not immediately followed by "
44948 "function declaration or definition",
44949 parser->omp_declare_simd->variant_p ? "variant" : "simd");
44950 data.tokens.release ();
44951 parser->omp_declare_simd = NULL;
44955 static const char *const omp_construct_selectors[] = {
44956 "simd", "target", "teams", "parallel", "for", NULL };
44957 static const char *const omp_device_selectors[] = {
44958 "kind", "isa", "arch", NULL };
44959 static const char *const omp_implementation_selectors[] = {
44960 "vendor", "extension", "atomic_default_mem_order", "unified_address",
44961 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
44962 static const char *const omp_user_selectors[] = {
44963 "condition", NULL };
44965 /* OpenMP 5.0:
44967 trait-selector:
44968 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
44970 trait-score:
44971 score(score-expression) */
44973 static tree
44974 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
44976 tree ret = NULL_TREE;
44979 tree selector;
44980 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
44981 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44982 selector = cp_lexer_peek_token (parser->lexer)->u.value;
44983 else
44985 cp_parser_error (parser, "expected trait selector name");
44986 return error_mark_node;
44989 tree properties = NULL_TREE;
44990 const char *const *selectors = NULL;
44991 bool allow_score = true;
44992 bool allow_user = false;
44993 int property_limit = 0;
44994 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
44995 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
44996 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
44997 switch (IDENTIFIER_POINTER (set)[0])
44999 case 'c': /* construct */
45000 selectors = omp_construct_selectors;
45001 allow_score = false;
45002 property_limit = 1;
45003 property_kind = CTX_PROPERTY_SIMD;
45004 break;
45005 case 'd': /* device */
45006 selectors = omp_device_selectors;
45007 allow_score = false;
45008 allow_user = true;
45009 property_limit = 3;
45010 property_kind = CTX_PROPERTY_NAME_LIST;
45011 break;
45012 case 'i': /* implementation */
45013 selectors = omp_implementation_selectors;
45014 allow_user = true;
45015 property_limit = 3;
45016 property_kind = CTX_PROPERTY_NAME_LIST;
45017 break;
45018 case 'u': /* user */
45019 selectors = omp_user_selectors;
45020 property_limit = 1;
45021 property_kind = CTX_PROPERTY_EXPR;
45022 break;
45023 default:
45024 gcc_unreachable ();
45026 for (int i = 0; ; i++)
45028 if (selectors[i] == NULL)
45030 if (allow_user)
45032 property_kind = CTX_PROPERTY_USER;
45033 break;
45035 else
45037 error ("selector %qs not allowed for context selector "
45038 "set %qs", IDENTIFIER_POINTER (selector),
45039 IDENTIFIER_POINTER (set));
45040 cp_lexer_consume_token (parser->lexer);
45041 return error_mark_node;
45044 if (i == property_limit)
45045 property_kind = CTX_PROPERTY_NONE;
45046 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
45047 break;
45049 if (property_kind == CTX_PROPERTY_NAME_LIST
45050 && IDENTIFIER_POINTER (set)[0] == 'i'
45051 && strcmp (IDENTIFIER_POINTER (selector),
45052 "atomic_default_mem_order") == 0)
45053 property_kind = CTX_PROPERTY_ID;
45055 cp_lexer_consume_token (parser->lexer);
45057 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
45059 if (property_kind == CTX_PROPERTY_NONE)
45061 error ("selector %qs does not accept any properties",
45062 IDENTIFIER_POINTER (selector));
45063 return error_mark_node;
45066 matching_parens parens;
45067 parens.consume_open (parser);
45069 cp_token *token = cp_lexer_peek_token (parser->lexer);
45070 if (allow_score
45071 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
45072 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
45073 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
45075 cp_lexer_save_tokens (parser->lexer);
45076 cp_lexer_consume_token (parser->lexer);
45077 cp_lexer_consume_token (parser->lexer);
45078 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
45079 true)
45080 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
45082 cp_lexer_rollback_tokens (parser->lexer);
45083 cp_lexer_consume_token (parser->lexer);
45085 matching_parens parens2;
45086 parens2.require_open (parser);
45087 tree score = cp_parser_constant_expression (parser);
45088 if (!parens2.require_close (parser))
45089 cp_parser_skip_to_closing_parenthesis (parser, true,
45090 false, true);
45091 cp_parser_require (parser, CPP_COLON, RT_COLON);
45092 if (score != error_mark_node)
45094 score = fold_non_dependent_expr (score);
45095 if (value_dependent_expression_p (score))
45096 properties = tree_cons (get_identifier (" score"),
45097 score, properties);
45098 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
45099 || TREE_CODE (score) != INTEGER_CST)
45100 error_at (token->location, "score argument must be "
45101 "constant integer expression");
45102 else if (tree_int_cst_sgn (score) < 0)
45103 error_at (token->location, "score argument must be "
45104 "non-negative");
45105 else
45106 properties = tree_cons (get_identifier (" score"),
45107 score, properties);
45110 else
45111 cp_lexer_rollback_tokens (parser->lexer);
45113 token = cp_lexer_peek_token (parser->lexer);
45116 switch (property_kind)
45118 tree t;
45119 case CTX_PROPERTY_USER:
45122 t = cp_parser_constant_expression (parser);
45123 if (t != error_mark_node)
45125 t = fold_non_dependent_expr (t);
45126 if (TREE_CODE (t) == STRING_CST)
45127 properties = tree_cons (NULL_TREE, t, properties);
45128 else if (!value_dependent_expression_p (t)
45129 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45130 || !tree_fits_shwi_p (t)))
45131 error_at (token->location, "property must be "
45132 "constant integer expression or string "
45133 "literal");
45134 else
45135 properties = tree_cons (NULL_TREE, t, properties);
45137 else
45138 return error_mark_node;
45140 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45141 cp_lexer_consume_token (parser->lexer);
45142 else
45143 break;
45145 while (1);
45146 break;
45147 case CTX_PROPERTY_ID:
45148 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45149 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45151 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
45152 cp_lexer_consume_token (parser->lexer);
45153 properties = tree_cons (prop, NULL_TREE, properties);
45155 else
45157 cp_parser_error (parser, "expected identifier");
45158 return error_mark_node;
45160 break;
45161 case CTX_PROPERTY_NAME_LIST:
45164 tree prop = NULL_TREE, value = NULL_TREE;
45165 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45166 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45168 prop = cp_lexer_peek_token (parser->lexer)->u.value;
45169 cp_lexer_consume_token (parser->lexer);
45171 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
45172 value = cp_parser_string_literal (parser, false, false);
45173 else
45175 cp_parser_error (parser, "expected identifier or "
45176 "string literal");
45177 return error_mark_node;
45180 properties = tree_cons (prop, value, properties);
45182 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45183 cp_lexer_consume_token (parser->lexer);
45184 else
45185 break;
45187 while (1);
45188 break;
45189 case CTX_PROPERTY_EXPR:
45190 t = cp_parser_constant_expression (parser);
45191 if (t != error_mark_node)
45193 t = fold_non_dependent_expr (t);
45194 if (!value_dependent_expression_p (t)
45195 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45196 || !tree_fits_shwi_p (t)))
45197 error_at (token->location, "property must be "
45198 "constant integer expression");
45199 else
45200 properties = tree_cons (NULL_TREE, t, properties);
45202 else
45203 return error_mark_node;
45204 break;
45205 case CTX_PROPERTY_SIMD:
45206 if (!has_parms_p)
45208 error_at (token->location, "properties for %<simd%> "
45209 "selector may not be specified in "
45210 "%<metadirective%>");
45211 return error_mark_node;
45213 properties
45214 = cp_parser_omp_all_clauses (parser,
45215 OMP_DECLARE_SIMD_CLAUSE_MASK,
45216 "simd", NULL, true, 2);
45217 break;
45218 default:
45219 gcc_unreachable ();
45222 if (!parens.require_close (parser))
45223 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
45225 properties = nreverse (properties);
45227 else if (property_kind == CTX_PROPERTY_NAME_LIST
45228 || property_kind == CTX_PROPERTY_ID
45229 || property_kind == CTX_PROPERTY_EXPR)
45231 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
45232 return error_mark_node;
45235 ret = tree_cons (selector, properties, ret);
45237 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45238 cp_lexer_consume_token (parser->lexer);
45239 else
45240 break;
45242 while (1);
45244 return nreverse (ret);
45247 /* OpenMP 5.0:
45249 trait-set-selector[,trait-set-selector[,...]]
45251 trait-set-selector:
45252 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
45254 trait-set-selector-name:
45255 constructor
45256 device
45257 implementation
45258 user */
45260 static tree
45261 cp_parser_omp_context_selector_specification (cp_parser *parser,
45262 bool has_parms_p)
45264 tree ret = NULL_TREE;
45267 const char *setp = "";
45268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45269 setp
45270 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45271 switch (setp[0])
45273 case 'c':
45274 if (strcmp (setp, "construct") == 0)
45275 setp = NULL;
45276 break;
45277 case 'd':
45278 if (strcmp (setp, "device") == 0)
45279 setp = NULL;
45280 break;
45281 case 'i':
45282 if (strcmp (setp, "implementation") == 0)
45283 setp = NULL;
45284 break;
45285 case 'u':
45286 if (strcmp (setp, "user") == 0)
45287 setp = NULL;
45288 break;
45289 default:
45290 break;
45292 if (setp)
45294 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
45295 "%<implementation%> or %<user%>");
45296 return error_mark_node;
45299 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
45300 cp_lexer_consume_token (parser->lexer);
45302 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
45303 return error_mark_node;
45305 matching_braces braces;
45306 if (!braces.require_open (parser))
45307 return error_mark_node;
45309 tree selectors
45310 = cp_parser_omp_context_selector (parser, set, has_parms_p);
45311 if (selectors == error_mark_node)
45313 cp_parser_skip_to_closing_brace (parser);
45314 ret = error_mark_node;
45316 else if (ret != error_mark_node)
45317 ret = tree_cons (set, selectors, ret);
45319 braces.require_close (parser);
45321 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45322 cp_lexer_consume_token (parser->lexer);
45323 else
45324 break;
45326 while (1);
45328 if (ret == error_mark_node)
45329 return ret;
45330 return nreverse (ret);
45333 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
45334 that into "omp declare variant base" attribute. */
45336 static tree
45337 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
45338 tree attrs)
45340 matching_parens parens;
45341 if (!parens.require_open (parser))
45343 fail:
45344 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45345 return attrs;
45348 bool template_p;
45349 cp_id_kind idk = CP_ID_KIND_NONE;
45350 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
45351 cp_expr varid
45352 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
45353 /*check_dependency_p=*/true,
45354 /*template_p=*/&template_p,
45355 /*declarator_p=*/false,
45356 /*optional_p=*/false);
45357 parens.require_close (parser);
45359 tree variant;
45360 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
45361 || TREE_CODE (varid) == TYPE_DECL
45362 || varid == error_mark_node)
45363 variant = varid;
45364 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
45365 variant = NULL_TREE;
45366 else
45368 tree ambiguous_decls;
45369 variant = cp_parser_lookup_name (parser, varid, none_type,
45370 template_p, /*is_namespace=*/false,
45371 /*check_dependency=*/true,
45372 &ambiguous_decls,
45373 varid.get_location ());
45374 if (ambiguous_decls)
45375 variant = NULL_TREE;
45377 if (variant == NULL_TREE)
45378 variant = error_mark_node;
45379 else if (TREE_CODE (variant) != SCOPE_REF)
45381 const char *error_msg;
45382 variant
45383 = finish_id_expression (varid, variant, parser->scope,
45384 &idk, false, true,
45385 &parser->non_integral_constant_expression_p,
45386 template_p, true, false, false, &error_msg,
45387 varid.get_location ());
45388 if (error_msg)
45389 cp_parser_error (parser, error_msg);
45391 location_t caret_loc = get_pure_location (varid.get_location ());
45392 location_t start_loc = get_start (varid_token->location);
45393 location_t finish_loc = get_finish (varid.get_location ());
45394 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
45396 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45397 if (parser->lexer->in_omp_attribute_pragma
45398 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45399 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45400 cp_lexer_consume_token (parser->lexer);
45402 const char *clause = "";
45403 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
45404 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45405 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45406 if (strcmp (clause, "match"))
45408 cp_parser_error (parser, "expected %<match%>");
45409 goto fail;
45412 cp_lexer_consume_token (parser->lexer);
45414 if (!parens.require_open (parser))
45415 goto fail;
45417 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
45418 if (ctx == error_mark_node)
45419 goto fail;
45420 ctx = omp_check_context_selector (match_loc, ctx);
45421 if (ctx != error_mark_node && variant != error_mark_node)
45423 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
45424 match_loc);
45425 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
45426 loc_node = tree_cons (match_loc_node,
45427 build_int_cst (integer_type_node, idk),
45428 build_tree_list (loc_node, integer_zero_node));
45429 attrs = tree_cons (get_identifier ("omp declare variant base"),
45430 tree_cons (variant, ctx, loc_node), attrs);
45431 if (processing_template_decl)
45432 ATTR_IS_DEPENDENT (attrs) = 1;
45435 parens.require_close (parser);
45436 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45437 return attrs;
45441 /* Finalize #pragma omp declare simd clauses after direct declarator has
45442 been parsed, and put that into "omp declare simd" attribute. */
45444 static tree
45445 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
45447 struct cp_token_cache *ce;
45448 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
45449 int i;
45451 if (!data->error_seen && data->fndecl_seen)
45453 error ("%<#pragma omp declare %s%> not immediately followed by "
45454 "a single function declaration or definition",
45455 data->variant_p ? "variant" : "simd");
45456 data->error_seen = true;
45458 if (data->error_seen)
45459 return attrs;
45461 FOR_EACH_VEC_ELT (data->tokens, i, ce)
45463 tree c, cl;
45465 cp_parser_push_lexer_for_tokens (parser, ce);
45466 parser->lexer->in_pragma = true;
45467 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
45468 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
45469 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45470 const char *kind = IDENTIFIER_POINTER (id);
45471 cp_lexer_consume_token (parser->lexer);
45472 if (strcmp (kind, "simd") == 0)
45474 /* For now only in C++ attributes, do it always for OpenMP 5.1.
45475 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45476 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45477 cp_lexer_consume_token (parser->lexer); */
45479 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
45480 "#pragma omp declare simd",
45481 pragma_tok);
45482 if (cl)
45483 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
45484 c = build_tree_list (get_identifier ("omp declare simd"), cl);
45485 TREE_CHAIN (c) = attrs;
45486 if (processing_template_decl)
45487 ATTR_IS_DEPENDENT (c) = 1;
45488 attrs = c;
45490 else
45492 gcc_assert (strcmp (kind, "variant") == 0);
45493 attrs
45494 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
45496 cp_parser_pop_lexer (parser);
45499 cp_lexer *lexer = NULL;
45500 for (int i = 0; i < 2; i++)
45502 if (data->attribs[i] == NULL)
45503 continue;
45504 for (tree *pa = data->attribs[i]; *pa; )
45505 if (get_attribute_namespace (*pa) == omp_identifier
45506 && is_attribute_p ("directive", get_attribute_name (*pa)))
45508 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
45510 tree d = TREE_VALUE (a);
45511 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
45512 cp_token *first = DEFPARSE_TOKENS (d)->first;
45513 cp_token *last = DEFPARSE_TOKENS (d)->last;
45514 const char *directive[3] = {};
45515 for (int j = 0; j < 3; j++)
45517 tree id = NULL_TREE;
45518 if (first + j == last)
45519 break;
45520 if (first[j].type == CPP_NAME)
45521 id = first[j].u.value;
45522 else if (first[j].type == CPP_KEYWORD)
45523 id = ridpointers[(int) first[j].keyword];
45524 else
45525 break;
45526 directive[j] = IDENTIFIER_POINTER (id);
45528 const c_omp_directive *dir = NULL;
45529 if (directive[0])
45530 dir = c_omp_categorize_directive (directive[0], directive[1],
45531 directive[2]);
45532 if (dir == NULL)
45534 error_at (first->location,
45535 "unknown OpenMP directive name in "
45536 "%<omp::directive%> attribute argument");
45537 continue;
45539 if (dir->id != PRAGMA_OMP_DECLARE
45540 || (strcmp (directive[1], "simd") != 0
45541 && strcmp (directive[1], "variant") != 0))
45543 error_at (first->location,
45544 "OpenMP directive other than %<declare simd%> "
45545 "or %<declare variant%> appertains to a "
45546 "declaration");
45547 continue;
45550 if (parser->omp_attrs_forbidden_p)
45552 error_at (first->location,
45553 "mixing OpenMP directives with attribute and "
45554 "pragma syntax on the same statement");
45555 parser->omp_attrs_forbidden_p = false;
45558 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
45559 continue;
45560 if (lexer == NULL)
45562 lexer = cp_lexer_alloc ();
45563 lexer->debugging_p = parser->lexer->debugging_p;
45565 vec_safe_reserve (lexer->buffer, (last - first) + 2);
45566 cp_token tok = {};
45567 tok.type = CPP_PRAGMA;
45568 tok.keyword = RID_MAX;
45569 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
45570 tok.location = first->location;
45571 lexer->buffer->quick_push (tok);
45572 while (++first < last)
45573 lexer->buffer->quick_push (*first);
45574 tok = {};
45575 tok.type = CPP_PRAGMA_EOL;
45576 tok.keyword = RID_MAX;
45577 tok.location = last->location;
45578 lexer->buffer->quick_push (tok);
45579 tok = {};
45580 tok.type = CPP_EOF;
45581 tok.keyword = RID_MAX;
45582 tok.location = last->location;
45583 lexer->buffer->quick_push (tok);
45584 lexer->next = parser->lexer;
45585 lexer->next_token = lexer->buffer->address ();
45586 lexer->last_token = lexer->next_token
45587 + lexer->buffer->length ()
45588 - 1;
45589 lexer->in_omp_attribute_pragma = true;
45590 parser->lexer = lexer;
45591 /* Move the current source position to that of the first token
45592 in the new lexer. */
45593 cp_lexer_set_source_position_from_token (lexer->next_token);
45595 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
45596 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45597 const char *kind = IDENTIFIER_POINTER (id);
45598 cp_lexer_consume_token (parser->lexer);
45600 tree c, cl;
45601 if (strcmp (kind, "simd") == 0)
45603 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45604 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45605 cp_lexer_consume_token (parser->lexer);
45607 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
45608 cl = cp_parser_omp_all_clauses (parser, mask,
45609 "#pragma omp declare simd",
45610 pragma_tok);
45611 if (cl)
45612 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
45613 c = build_tree_list (get_identifier ("omp declare simd"),
45614 cl);
45615 TREE_CHAIN (c) = attrs;
45616 if (processing_template_decl)
45617 ATTR_IS_DEPENDENT (c) = 1;
45618 attrs = c;
45620 else
45622 gcc_assert (strcmp (kind, "variant") == 0);
45623 attrs
45624 = cp_finish_omp_declare_variant (parser, pragma_tok,
45625 attrs);
45627 gcc_assert (parser->lexer != lexer);
45628 vec_safe_truncate (lexer->buffer, 0);
45630 *pa = TREE_CHAIN (*pa);
45632 else
45633 pa = &TREE_CHAIN (*pa);
45635 if (lexer)
45636 cp_lexer_destroy (lexer);
45638 data->fndecl_seen = true;
45639 return attrs;
45642 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
45643 on #pragma omp declare target. Return false if errors were reported. */
45645 static bool
45646 handle_omp_declare_target_clause (tree c, tree t, int device_type)
45648 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
45649 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
45650 tree id;
45651 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
45653 id = get_identifier ("omp declare target link");
45654 std::swap (at1, at2);
45656 else
45657 id = get_identifier ("omp declare target");
45658 if (at2)
45660 error_at (OMP_CLAUSE_LOCATION (c),
45661 "%qD specified both in declare target %<link%> and %<to%>"
45662 " clauses", t);
45663 return false;
45665 if (!at1)
45667 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45668 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
45669 return true;
45671 symtab_node *node = symtab_node::get (t);
45672 if (node != NULL)
45674 node->offloadable = 1;
45675 if (ENABLE_OFFLOADING)
45677 g->have_offload = true;
45678 if (is_a <varpool_node *> (node))
45679 vec_safe_push (offload_vars, t);
45683 if (TREE_CODE (t) != FUNCTION_DECL)
45684 return true;
45685 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
45687 tree at3 = lookup_attribute ("omp declare target host",
45688 DECL_ATTRIBUTES (t));
45689 if (at3 == NULL_TREE)
45691 id = get_identifier ("omp declare target host");
45692 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45695 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
45697 tree at3 = lookup_attribute ("omp declare target nohost",
45698 DECL_ATTRIBUTES (t));
45699 if (at3 == NULL_TREE)
45701 id = get_identifier ("omp declare target nohost");
45702 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45705 return true;
45708 /* OpenMP 4.0:
45709 # pragma omp declare target new-line
45710 declarations and definitions
45711 # pragma omp end declare target new-line
45713 OpenMP 4.5:
45714 # pragma omp declare target ( extended-list ) new-line
45716 # pragma omp declare target declare-target-clauses[seq] new-line */
45718 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
45719 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
45720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
45721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
45723 static void
45724 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
45726 tree clauses = NULL_TREE;
45727 int device_type = 0;
45728 bool only_device_type = true;
45729 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
45730 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45731 || (parser->lexer->in_omp_attribute_pragma
45732 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45733 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
45734 clauses
45735 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
45736 "#pragma omp declare target", pragma_tok);
45737 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
45739 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
45740 clauses);
45741 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
45742 cp_parser_require_pragma_eol (parser, pragma_tok);
45744 else
45746 struct omp_declare_target_attr a
45747 = { parser->lexer->in_omp_attribute_pragma };
45748 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
45749 cp_parser_require_pragma_eol (parser, pragma_tok);
45750 return;
45752 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45753 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
45754 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
45755 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45757 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
45758 continue;
45759 tree t = OMP_CLAUSE_DECL (c);
45760 only_device_type = false;
45761 if (!handle_omp_declare_target_clause (c, t, device_type))
45762 continue;
45763 if (VAR_OR_FUNCTION_DECL_P (t)
45764 && DECL_LOCAL_DECL_P (t)
45765 && DECL_LANG_SPECIFIC (t)
45766 && DECL_LOCAL_DECL_ALIAS (t)
45767 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
45768 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
45769 device_type);
45771 if (device_type && only_device_type)
45772 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
45773 "directive with only %<device_type%> clauses ignored");
45776 static void
45777 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
45779 const char *p = "";
45780 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
45781 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45783 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45784 p = IDENTIFIER_POINTER (id);
45786 if (strcmp (p, "declare") == 0)
45788 cp_lexer_consume_token (parser->lexer);
45789 p = "";
45790 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45792 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45793 p = IDENTIFIER_POINTER (id);
45795 if (strcmp (p, "target") == 0)
45796 cp_lexer_consume_token (parser->lexer);
45797 else
45799 cp_parser_error (parser, "expected %<target%>");
45800 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45801 return;
45804 else
45806 cp_parser_error (parser, "expected %<declare%>");
45807 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45808 return;
45810 cp_parser_require_pragma_eol (parser, pragma_tok);
45811 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
45812 error_at (pragma_tok->location,
45813 "%<#pragma omp end declare target%> without corresponding "
45814 "%<#pragma omp declare target%>");
45815 else
45817 omp_declare_target_attr
45818 a = scope_chain->omp_declare_target_attribute->pop ();
45819 if (a.attr_syntax != in_omp_attribute_pragma)
45821 if (a.attr_syntax)
45822 error_at (pragma_tok->location,
45823 "%<declare target%> in attribute syntax terminated "
45824 "with %<end declare target%> in pragma syntax");
45825 else
45826 error_at (pragma_tok->location,
45827 "%<declare target%> in pragma syntax terminated "
45828 "with %<end declare target%> in attribute syntax");
45833 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
45834 expression and optional initializer clause of
45835 #pragma omp declare reduction. We store the expression(s) as
45836 either 3, 6 or 7 special statements inside of the artificial function's
45837 body. The first two statements are DECL_EXPRs for the artificial
45838 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
45839 expression that uses those variables.
45840 If there was any INITIALIZER clause, this is followed by further statements,
45841 the fourth and fifth statements are DECL_EXPRs for the artificial
45842 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
45843 constructor variant (first token after open paren is not omp_priv),
45844 then the sixth statement is a statement with the function call expression
45845 that uses the OMP_PRIV and optionally OMP_ORIG variable.
45846 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
45847 to initialize the OMP_PRIV artificial variable and there is seventh
45848 statement, a DECL_EXPR of the OMP_PRIV statement again. */
45850 static bool
45851 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
45853 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
45854 gcc_assert (TYPE_REF_P (type));
45855 type = TREE_TYPE (type);
45856 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
45857 DECL_ARTIFICIAL (omp_out) = 1;
45858 pushdecl (omp_out);
45859 add_decl_expr (omp_out);
45860 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
45861 DECL_ARTIFICIAL (omp_in) = 1;
45862 pushdecl (omp_in);
45863 add_decl_expr (omp_in);
45864 tree combiner;
45865 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
45867 keep_next_level (true);
45868 tree block = begin_omp_structured_block ();
45869 combiner = cp_parser_expression (parser);
45870 finish_expr_stmt (combiner);
45871 block = finish_omp_structured_block (block);
45872 if (processing_template_decl)
45873 block = build_stmt (input_location, EXPR_STMT, block);
45874 add_stmt (block);
45876 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
45877 return false;
45879 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45880 if (parser->lexer->in_omp_attribute_pragma
45881 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45882 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45883 cp_lexer_consume_token (parser->lexer);
45885 const char *p = "";
45886 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45888 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45889 p = IDENTIFIER_POINTER (id);
45892 if (strcmp (p, "initializer") == 0)
45894 cp_lexer_consume_token (parser->lexer);
45895 matching_parens parens;
45896 if (!parens.require_open (parser))
45897 return false;
45899 p = "";
45900 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45902 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45903 p = IDENTIFIER_POINTER (id);
45906 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
45907 DECL_ARTIFICIAL (omp_priv) = 1;
45908 pushdecl (omp_priv);
45909 add_decl_expr (omp_priv);
45910 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
45911 DECL_ARTIFICIAL (omp_orig) = 1;
45912 pushdecl (omp_orig);
45913 add_decl_expr (omp_orig);
45915 keep_next_level (true);
45916 block = begin_omp_structured_block ();
45918 bool ctor = false;
45919 if (strcmp (p, "omp_priv") == 0)
45921 bool is_direct_init, is_non_constant_init;
45922 ctor = true;
45923 cp_lexer_consume_token (parser->lexer);
45924 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
45925 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
45926 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
45927 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
45928 == CPP_CLOSE_PAREN
45929 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
45930 == CPP_CLOSE_PAREN))
45932 finish_omp_structured_block (block);
45933 error ("invalid initializer clause");
45934 return false;
45936 initializer = cp_parser_initializer (parser, &is_direct_init,
45937 &is_non_constant_init);
45938 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
45939 NULL_TREE, LOOKUP_ONLYCONVERTING);
45941 else
45943 cp_parser_parse_tentatively (parser);
45944 /* Don't create location wrapper nodes here. */
45945 auto_suppress_location_wrappers sentinel;
45946 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
45947 /*check_dependency_p=*/true,
45948 /*template_p=*/NULL,
45949 /*declarator_p=*/false,
45950 /*optional_p=*/false);
45951 vec<tree, va_gc> *args;
45952 if (fn_name == error_mark_node
45953 || cp_parser_error_occurred (parser)
45954 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
45955 || ((args = cp_parser_parenthesized_expression_list
45956 (parser, non_attr, /*cast_p=*/false,
45957 /*allow_expansion_p=*/true,
45958 /*non_constant_p=*/NULL)),
45959 cp_parser_error_occurred (parser)))
45961 finish_omp_structured_block (block);
45962 cp_parser_abort_tentative_parse (parser);
45963 cp_parser_error (parser, "expected id-expression (arguments)");
45964 return false;
45966 unsigned int i;
45967 tree arg;
45968 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
45969 if (arg == omp_priv
45970 || (TREE_CODE (arg) == ADDR_EXPR
45971 && TREE_OPERAND (arg, 0) == omp_priv))
45972 break;
45973 cp_parser_abort_tentative_parse (parser);
45974 if (arg == NULL_TREE)
45975 error ("one of the initializer call arguments should be %<omp_priv%>"
45976 " or %<&omp_priv%>");
45977 initializer = cp_parser_postfix_expression (parser, false, false, false,
45978 false, NULL);
45979 finish_expr_stmt (initializer);
45982 block = finish_omp_structured_block (block);
45983 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
45984 if (processing_template_decl)
45985 block = build_stmt (input_location, EXPR_STMT, block);
45986 add_stmt (block);
45988 if (ctor)
45989 add_decl_expr (omp_orig);
45991 if (!parens.require_close (parser))
45992 return false;
45995 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
45996 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
45997 UNKNOWN_LOCATION);
45999 return true;
46002 /* OpenMP 4.0
46003 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46004 initializer-clause[opt] new-line
46006 initializer-clause:
46007 initializer (omp_priv initializer)
46008 initializer (function-name (argument-list)) */
46010 static void
46011 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
46012 enum pragma_context)
46014 auto_vec<tree> types;
46015 enum tree_code reduc_code = ERROR_MARK;
46016 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
46017 unsigned int i;
46018 cp_token *first_token;
46019 cp_token_cache *cp;
46020 int errs;
46021 void *p;
46023 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
46024 p = obstack_alloc (&declarator_obstack, 0);
46026 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
46027 goto fail;
46029 switch (cp_lexer_peek_token (parser->lexer)->type)
46031 case CPP_PLUS:
46032 reduc_code = PLUS_EXPR;
46033 break;
46034 case CPP_MULT:
46035 reduc_code = MULT_EXPR;
46036 break;
46037 case CPP_MINUS:
46038 reduc_code = MINUS_EXPR;
46039 break;
46040 case CPP_AND:
46041 reduc_code = BIT_AND_EXPR;
46042 break;
46043 case CPP_XOR:
46044 reduc_code = BIT_XOR_EXPR;
46045 break;
46046 case CPP_OR:
46047 reduc_code = BIT_IOR_EXPR;
46048 break;
46049 case CPP_AND_AND:
46050 reduc_code = TRUTH_ANDIF_EXPR;
46051 break;
46052 case CPP_OR_OR:
46053 reduc_code = TRUTH_ORIF_EXPR;
46054 break;
46055 case CPP_NAME:
46056 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
46057 break;
46058 default:
46059 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
46060 "%<|%>, %<&&%>, %<||%> or identifier");
46061 goto fail;
46064 if (reduc_code != ERROR_MARK)
46065 cp_lexer_consume_token (parser->lexer);
46067 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
46068 if (reduc_id == error_mark_node)
46069 goto fail;
46071 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
46072 goto fail;
46074 /* Types may not be defined in declare reduction type list. */
46075 const char *saved_message;
46076 saved_message = parser->type_definition_forbidden_message;
46077 parser->type_definition_forbidden_message
46078 = G_("types may not be defined in declare reduction type list");
46079 bool saved_colon_corrects_to_scope_p;
46080 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
46081 parser->colon_corrects_to_scope_p = false;
46082 bool saved_colon_doesnt_start_class_def_p;
46083 saved_colon_doesnt_start_class_def_p
46084 = parser->colon_doesnt_start_class_def_p;
46085 parser->colon_doesnt_start_class_def_p = true;
46087 while (true)
46089 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46090 type = cp_parser_type_id (parser);
46091 if (type == error_mark_node)
46093 else if (ARITHMETIC_TYPE_P (type)
46094 && (orig_reduc_id == NULL_TREE
46095 || (TREE_CODE (type) != COMPLEX_TYPE
46096 && (id_equal (orig_reduc_id, "min")
46097 || id_equal (orig_reduc_id, "max")))))
46098 error_at (loc, "predeclared arithmetic type %qT in "
46099 "%<#pragma omp declare reduction%>", type);
46100 else if (FUNC_OR_METHOD_TYPE_P (type)
46101 || TREE_CODE (type) == ARRAY_TYPE)
46102 error_at (loc, "function or array type %qT in "
46103 "%<#pragma omp declare reduction%>", type);
46104 else if (TYPE_REF_P (type))
46105 error_at (loc, "reference type %qT in "
46106 "%<#pragma omp declare reduction%>", type);
46107 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
46108 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
46109 "type %qT in %<#pragma omp declare reduction%>", type);
46110 else
46111 types.safe_push (type);
46113 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46114 cp_lexer_consume_token (parser->lexer);
46115 else
46116 break;
46119 /* Restore the saved message. */
46120 parser->type_definition_forbidden_message = saved_message;
46121 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
46122 parser->colon_doesnt_start_class_def_p
46123 = saved_colon_doesnt_start_class_def_p;
46125 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
46126 || types.is_empty ())
46128 fail:
46129 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46130 goto done;
46133 first_token = cp_lexer_peek_token (parser->lexer);
46134 cp = NULL;
46135 errs = errorcount;
46136 FOR_EACH_VEC_ELT (types, i, type)
46138 tree fntype
46139 = build_function_type_list (void_type_node,
46140 cp_build_reference_type (type, false),
46141 NULL_TREE);
46142 tree this_reduc_id = reduc_id;
46143 if (!dependent_type_p (type))
46144 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
46145 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
46146 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
46147 DECL_ARTIFICIAL (fndecl) = 1;
46148 DECL_EXTERNAL (fndecl) = 1;
46149 DECL_DECLARED_INLINE_P (fndecl) = 1;
46150 DECL_IGNORED_P (fndecl) = 1;
46151 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
46152 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
46153 DECL_ATTRIBUTES (fndecl)
46154 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
46155 DECL_ATTRIBUTES (fndecl));
46156 bool block_scope = false;
46157 if (current_function_decl)
46159 block_scope = true;
46160 DECL_CONTEXT (fndecl) = current_function_decl;
46161 DECL_LOCAL_DECL_P (fndecl) = true;
46164 if (processing_template_decl)
46165 fndecl = push_template_decl (fndecl);
46167 if (block_scope)
46169 if (!processing_template_decl)
46170 pushdecl (fndecl);
46172 else if (current_class_type)
46174 if (cp == NULL)
46176 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46177 cp_lexer_consume_token (parser->lexer);
46178 cp = cp_token_cache_new (first_token,
46179 cp_lexer_peek_nth_token (parser->lexer,
46180 2));
46182 DECL_STATIC_FUNCTION_P (fndecl) = 1;
46183 finish_member_declaration (fndecl);
46184 DECL_PENDING_INLINE_INFO (fndecl) = cp;
46185 DECL_PENDING_INLINE_P (fndecl) = 1;
46186 vec_safe_push (unparsed_funs_with_definitions, fndecl);
46187 continue;
46189 else
46191 DECL_CONTEXT (fndecl) = current_namespace;
46192 tree d = pushdecl (fndecl);
46193 /* We should never meet a matched duplicate decl. */
46194 gcc_checking_assert (d == error_mark_node || d == fndecl);
46197 tree block = NULL_TREE;
46198 if (!block_scope)
46199 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
46200 else
46201 block = begin_omp_structured_block ();
46202 if (cp)
46204 cp_parser_push_lexer_for_tokens (parser, cp);
46205 parser->lexer->in_pragma = true;
46208 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
46210 if (cp)
46211 cp_parser_pop_lexer (parser);
46212 if (!block_scope)
46213 finish_function (/*inline_p=*/false);
46214 else
46216 DECL_CONTEXT (fndecl) = current_function_decl;
46217 if (DECL_TEMPLATE_INFO (fndecl))
46218 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
46220 if (!ok)
46221 goto fail;
46223 if (block_scope)
46225 block = finish_omp_structured_block (block);
46226 if (TREE_CODE (block) == BIND_EXPR)
46227 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
46228 else if (TREE_CODE (block) == STATEMENT_LIST)
46229 DECL_SAVED_TREE (fndecl) = block;
46230 if (processing_template_decl)
46231 add_decl_expr (fndecl);
46234 cp_check_omp_declare_reduction (fndecl);
46235 if (cp == NULL && types.length () > 1)
46236 cp = cp_token_cache_new (first_token,
46237 cp_lexer_peek_nth_token (parser->lexer, 2));
46238 if (errs != errorcount)
46239 break;
46242 cp_parser_require_pragma_eol (parser, pragma_tok);
46244 done:
46245 /* Free any declarators allocated. */
46246 obstack_free (&declarator_obstack, p);
46249 /* OpenMP 4.0
46250 #pragma omp declare simd declare-simd-clauses[optseq] new-line
46251 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46252 initializer-clause[opt] new-line
46253 #pragma omp declare target new-line
46255 OpenMP 5.0
46256 #pragma omp declare variant (identifier) match (context-selector) */
46258 static bool
46259 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
46260 enum pragma_context context)
46262 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46264 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46265 const char *p = IDENTIFIER_POINTER (id);
46267 if (strcmp (p, "simd") == 0)
46269 cp_lexer_consume_token (parser->lexer);
46270 cp_parser_omp_declare_simd (parser, pragma_tok,
46271 context, false);
46272 return true;
46274 if (flag_openmp && strcmp (p, "variant") == 0)
46276 cp_lexer_consume_token (parser->lexer);
46277 cp_parser_omp_declare_simd (parser, pragma_tok,
46278 context, true);
46279 return true;
46281 cp_ensure_no_omp_declare_simd (parser);
46282 if (strcmp (p, "reduction") == 0)
46284 cp_lexer_consume_token (parser->lexer);
46285 cp_parser_omp_declare_reduction (parser, pragma_tok,
46286 context);
46287 return false;
46289 if (!flag_openmp) /* flag_openmp_simd */
46291 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46292 return false;
46294 if (strcmp (p, "target") == 0)
46296 cp_lexer_consume_token (parser->lexer);
46297 cp_parser_omp_declare_target (parser, pragma_tok);
46298 return false;
46301 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
46302 "%<target%> or %<variant%>");
46303 cp_parser_require_pragma_eol (parser, pragma_tok);
46304 return false;
46307 /* OpenMP 5.0
46308 #pragma omp requires clauses[optseq] new-line */
46310 static bool
46311 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
46313 bool first = true;
46314 enum omp_requires new_req = (enum omp_requires) 0;
46316 location_t loc = pragma_tok->location;
46317 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46319 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46320 if ((!first || parser->lexer->in_omp_attribute_pragma)
46321 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46322 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46323 cp_lexer_consume_token (parser->lexer);
46325 first = false;
46327 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46329 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46330 const char *p = IDENTIFIER_POINTER (id);
46331 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46332 enum omp_requires this_req = (enum omp_requires) 0;
46334 if (!strcmp (p, "unified_address"))
46335 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
46336 else if (!strcmp (p, "unified_shared_memory"))
46337 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
46338 else if (!strcmp (p, "dynamic_allocators"))
46339 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
46340 else if (!strcmp (p, "reverse_offload"))
46341 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
46342 else if (!strcmp (p, "atomic_default_mem_order"))
46344 cp_lexer_consume_token (parser->lexer);
46346 matching_parens parens;
46347 if (parens.require_open (parser))
46349 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46351 id = cp_lexer_peek_token (parser->lexer)->u.value;
46352 p = IDENTIFIER_POINTER (id);
46354 if (!strcmp (p, "seq_cst"))
46355 this_req
46356 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
46357 else if (!strcmp (p, "relaxed"))
46358 this_req
46359 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
46360 else if (!strcmp (p, "acq_rel"))
46361 this_req
46362 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
46364 if (this_req == 0)
46366 error_at (cp_lexer_peek_token (parser->lexer)->location,
46367 "expected %<seq_cst%>, %<relaxed%> or "
46368 "%<acq_rel%>");
46369 switch (cp_lexer_peek_token (parser->lexer)->type)
46371 case CPP_EOF:
46372 case CPP_PRAGMA_EOL:
46373 case CPP_CLOSE_PAREN:
46374 break;
46375 default:
46376 if (cp_lexer_nth_token_is (parser->lexer, 2,
46377 CPP_CLOSE_PAREN))
46378 cp_lexer_consume_token (parser->lexer);
46379 break;
46382 else
46383 cp_lexer_consume_token (parser->lexer);
46385 if (!parens.require_close (parser))
46386 cp_parser_skip_to_closing_parenthesis (parser,
46387 /*recovering=*/true,
46388 /*or_comma=*/false,
46389 /*consume_paren=*/
46390 true);
46392 if (this_req == 0)
46394 cp_parser_require_pragma_eol (parser, pragma_tok);
46395 return false;
46398 p = NULL;
46400 else
46402 error_at (cloc, "expected %<unified_address%>, "
46403 "%<unified_shared_memory%>, "
46404 "%<dynamic_allocators%>, "
46405 "%<reverse_offload%> "
46406 "or %<atomic_default_mem_order%> clause");
46407 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46408 return false;
46410 if (p)
46411 sorry_at (cloc, "%qs clause on %<requires%> directive not "
46412 "supported yet", p);
46413 if (p)
46414 cp_lexer_consume_token (parser->lexer);
46415 if (this_req)
46417 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46419 if ((this_req & new_req) != 0)
46420 error_at (cloc, "too many %qs clauses", p);
46421 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
46422 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
46423 error_at (cloc, "%qs clause used lexically after first "
46424 "target construct or offloading API", p);
46426 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46428 error_at (cloc, "too many %qs clauses",
46429 "atomic_default_mem_order");
46430 this_req = (enum omp_requires) 0;
46432 else if ((omp_requires_mask
46433 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46435 error_at (cloc, "more than one %<atomic_default_mem_order%>"
46436 " clause in a single compilation unit");
46437 this_req
46438 = (enum omp_requires)
46439 (omp_requires_mask
46440 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
46442 else if ((omp_requires_mask
46443 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
46444 error_at (cloc, "%<atomic_default_mem_order%> clause used "
46445 "lexically after first %<atomic%> construct "
46446 "without memory order clause");
46447 new_req = (enum omp_requires) (new_req | this_req);
46448 omp_requires_mask
46449 = (enum omp_requires) (omp_requires_mask | this_req);
46450 continue;
46453 break;
46455 cp_parser_require_pragma_eol (parser, pragma_tok);
46457 if (new_req == 0)
46458 error_at (loc, "%<pragma omp requires%> requires at least one clause");
46459 return false;
46463 /* OpenMP 5.1:
46464 #pragma omp nothing new-line */
46466 static void
46467 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
46469 cp_parser_require_pragma_eol (parser, pragma_tok);
46473 /* OpenMP 5.1
46474 #pragma omp error clauses[optseq] new-line */
46476 static bool
46477 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
46478 enum pragma_context context)
46480 int at_compilation = -1;
46481 int severity_fatal = -1;
46482 tree message = NULL_TREE;
46483 bool first = true;
46484 bool bad = false;
46485 location_t loc = pragma_tok->location;
46487 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46489 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46490 if ((!first || parser->lexer->in_omp_attribute_pragma)
46491 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46492 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46493 cp_lexer_consume_token (parser->lexer);
46495 first = false;
46497 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
46498 break;
46500 const char *p
46501 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46502 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46503 static const char *args[] = {
46504 "execution", "compilation", "warning", "fatal"
46506 int *v = NULL;
46507 int idx = 0, n = -1;
46508 tree m = NULL_TREE;
46510 if (!strcmp (p, "at"))
46511 v = &at_compilation;
46512 else if (!strcmp (p, "severity"))
46514 v = &severity_fatal;
46515 idx += 2;
46517 else if (strcmp (p, "message"))
46519 error_at (cloc,
46520 "expected %<at%>, %<severity%> or %<message%> clause");
46521 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46522 return false;
46525 cp_lexer_consume_token (parser->lexer);
46527 matching_parens parens;
46528 if (parens.require_open (parser))
46530 if (v == NULL)
46532 m = cp_parser_assignment_expression (parser);
46533 if (type_dependent_expression_p (m))
46534 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
46535 else
46536 m = perform_implicit_conversion_flags (const_string_type_node, m,
46537 tf_warning_or_error,
46538 LOOKUP_NORMAL);
46540 else
46542 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46544 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
46545 const char *q = IDENTIFIER_POINTER (val);
46547 if (!strcmp (q, args[idx]))
46548 n = 0;
46549 else if (!strcmp (q, args[idx + 1]))
46550 n = 1;
46552 if (n == -1)
46554 error_at (cp_lexer_peek_token (parser->lexer)->location,
46555 "expected %qs or %qs", args[idx], args[idx + 1]);
46556 bad = true;
46557 switch (cp_lexer_peek_token (parser->lexer)->type)
46559 case CPP_EOF:
46560 case CPP_PRAGMA_EOL:
46561 case CPP_CLOSE_PAREN:
46562 break;
46563 default:
46564 if (cp_lexer_nth_token_is (parser->lexer, 2,
46565 CPP_CLOSE_PAREN))
46566 cp_lexer_consume_token (parser->lexer);
46567 break;
46570 else
46571 cp_lexer_consume_token (parser->lexer);
46574 if (!parens.require_close (parser))
46575 cp_parser_skip_to_closing_parenthesis (parser,
46576 /*recovering=*/true,
46577 /*or_comma=*/false,
46578 /*consume_paren=*/
46579 true);
46581 if (v == NULL)
46583 if (message)
46585 error_at (cloc, "too many %qs clauses", p);
46586 bad = true;
46588 else
46589 message = m;
46591 else if (n != -1)
46593 if (*v != -1)
46595 error_at (cloc, "too many %qs clauses", p);
46596 bad = true;
46598 else
46599 *v = n;
46602 else
46603 bad = true;
46605 cp_parser_require_pragma_eol (parser, pragma_tok);
46606 if (bad)
46607 return true;
46609 if (at_compilation == -1)
46610 at_compilation = 1;
46611 if (severity_fatal == -1)
46612 severity_fatal = 1;
46613 if (!at_compilation)
46615 if (context != pragma_compound)
46617 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
46618 "may only be used in compound statements");
46619 return true;
46621 tree fndecl
46622 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
46623 : BUILT_IN_GOMP_WARNING);
46624 if (!message)
46625 message = build_zero_cst (const_string_type_node);
46626 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
46627 build_all_ones_cst (size_type_node));
46628 add_stmt (stmt);
46629 return true;
46632 if (in_discarded_stmt)
46633 return false;
46635 const char *msg = NULL;
46636 if (message)
46638 msg = c_getstr (fold_for_warn (message));
46639 if (msg == NULL)
46640 msg = _("<message unknown at compile time>");
46642 if (msg)
46643 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
46644 "%<pragma omp error%> encountered: %s", msg);
46645 else
46646 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
46647 "%<pragma omp error%> encountered");
46648 return false;
46651 /* OpenMP 4.5:
46652 #pragma omp taskloop taskloop-clause[optseq] new-line
46653 for-loop
46655 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
46656 for-loop */
46658 #define OMP_TASKLOOP_CLAUSE_MASK \
46659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
46664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
46665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
46666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
46667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
46668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
46670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
46671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
46672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
46673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
46677 static tree
46678 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
46679 char *p_name, omp_clause_mask mask, tree *cclauses,
46680 bool *if_p)
46682 tree clauses, sb, ret;
46683 unsigned int save;
46684 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46686 strcat (p_name, " taskloop");
46687 mask |= OMP_TASKLOOP_CLAUSE_MASK;
46688 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
46689 clause. */
46690 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
46691 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
46693 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46695 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46696 const char *p = IDENTIFIER_POINTER (id);
46698 if (strcmp (p, "simd") == 0)
46700 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46701 if (cclauses == NULL)
46702 cclauses = cclauses_buf;
46704 cp_lexer_consume_token (parser->lexer);
46705 if (!flag_openmp) /* flag_openmp_simd */
46706 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46707 cclauses, if_p);
46708 sb = begin_omp_structured_block ();
46709 save = cp_parser_begin_omp_structured_block (parser);
46710 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46711 cclauses, if_p);
46712 cp_parser_end_omp_structured_block (parser, save);
46713 tree body = finish_omp_structured_block (sb);
46714 if (ret == NULL)
46715 return ret;
46716 ret = make_node (OMP_TASKLOOP);
46717 TREE_TYPE (ret) = void_type_node;
46718 OMP_FOR_BODY (ret) = body;
46719 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
46720 SET_EXPR_LOCATION (ret, loc);
46721 add_stmt (ret);
46722 return ret;
46725 if (!flag_openmp) /* flag_openmp_simd */
46727 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46728 return NULL_TREE;
46731 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46732 cclauses == NULL);
46733 if (cclauses)
46735 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
46736 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
46739 keep_next_level (true);
46740 sb = begin_omp_structured_block ();
46741 save = cp_parser_begin_omp_structured_block (parser);
46743 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
46744 if_p);
46746 cp_parser_end_omp_structured_block (parser, save);
46747 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
46749 return ret;
46753 /* OpenACC 2.0:
46754 # pragma acc routine oacc-routine-clause[optseq] new-line
46755 function-definition
46757 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
46760 #define OACC_ROUTINE_CLAUSE_MASK \
46761 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
46762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
46763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
46764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
46765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
46767 /* Parse the OpenACC routine pragma. This has an optional '( name )'
46768 component, which must resolve to a declared namespace-scope
46769 function. The clauses are either processed directly (for a named
46770 function), or defered until the immediatley following declaration
46771 is parsed. */
46773 static void
46774 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
46775 enum pragma_context context)
46777 gcc_checking_assert (context == pragma_external);
46778 /* The checking for "another pragma following this one" in the "no optional
46779 '( name )'" case makes sure that we dont re-enter. */
46780 gcc_checking_assert (parser->oacc_routine == NULL);
46782 cp_oacc_routine_data data;
46783 data.error_seen = false;
46784 data.fndecl_seen = false;
46785 data.tokens = vNULL;
46786 data.clauses = NULL_TREE;
46787 data.loc = pragma_tok->location;
46788 /* It is safe to take the address of a local variable; it will only be
46789 used while this scope is live. */
46790 parser->oacc_routine = &data;
46792 /* Look for optional '( name )'. */
46793 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46795 matching_parens parens;
46796 parens.consume_open (parser); /* '(' */
46798 /* We parse the name as an id-expression. If it resolves to
46799 anything other than a non-overloaded function at namespace
46800 scope, it's an error. */
46801 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
46802 tree name = cp_parser_id_expression (parser,
46803 /*template_keyword_p=*/false,
46804 /*check_dependency_p=*/false,
46805 /*template_p=*/NULL,
46806 /*declarator_p=*/false,
46807 /*optional_p=*/false);
46808 tree decl = (identifier_p (name)
46809 ? cp_parser_lookup_name_simple (parser, name, name_loc)
46810 : name);
46811 if (name != error_mark_node && decl == error_mark_node)
46812 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
46814 if (decl == error_mark_node
46815 || !parens.require_close (parser))
46817 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46818 parser->oacc_routine = NULL;
46819 return;
46822 data.clauses
46823 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
46824 "#pragma acc routine",
46825 cp_lexer_peek_token (parser->lexer));
46826 /* The clauses are in reverse order; fix that to make later diagnostic
46827 emission easier. */
46828 data.clauses = nreverse (data.clauses);
46830 if (decl && is_overloaded_fn (decl)
46831 && (TREE_CODE (decl) != FUNCTION_DECL
46832 || DECL_FUNCTION_TEMPLATE_P (decl)))
46834 error_at (name_loc,
46835 "%<#pragma acc routine%> names a set of overloads");
46836 parser->oacc_routine = NULL;
46837 return;
46840 /* Perhaps we should use the same rule as declarations in different
46841 namespaces? */
46842 if (!DECL_NAMESPACE_SCOPE_P (decl))
46844 error_at (name_loc,
46845 "%qD does not refer to a namespace scope function", decl);
46846 parser->oacc_routine = NULL;
46847 return;
46850 if (TREE_CODE (decl) != FUNCTION_DECL)
46852 error_at (name_loc, "%qD does not refer to a function", decl);
46853 parser->oacc_routine = NULL;
46854 return;
46857 cp_finalize_oacc_routine (parser, decl, false);
46858 parser->oacc_routine = NULL;
46860 else /* No optional '( name )'. */
46862 /* Store away all pragma tokens. */
46863 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46864 cp_lexer_consume_token (parser->lexer);
46865 cp_parser_require_pragma_eol (parser, pragma_tok);
46866 struct cp_token_cache *cp
46867 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
46868 parser->oacc_routine->tokens.safe_push (cp);
46870 /* Emit a helpful diagnostic if there's another pragma following this
46871 one. */
46872 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
46874 cp_ensure_no_oacc_routine (parser);
46875 data.tokens.release ();
46876 /* ..., and then just keep going. */
46877 return;
46880 /* We only have to consider the pragma_external case here. */
46881 cp_parser_declaration (parser, NULL_TREE);
46882 if (parser->oacc_routine
46883 && !parser->oacc_routine->fndecl_seen)
46884 cp_ensure_no_oacc_routine (parser);
46885 else
46886 parser->oacc_routine = NULL;
46887 data.tokens.release ();
46891 /* Finalize #pragma acc routine clauses after direct declarator has
46892 been parsed. */
46894 static tree
46895 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
46897 struct cp_token_cache *ce;
46898 cp_oacc_routine_data *data = parser->oacc_routine;
46900 if (!data->error_seen && data->fndecl_seen)
46902 error_at (data->loc,
46903 "%<#pragma acc routine%> not immediately followed by "
46904 "a single function declaration or definition");
46905 data->error_seen = true;
46907 if (data->error_seen)
46908 return attrs;
46910 gcc_checking_assert (data->tokens.length () == 1);
46911 ce = data->tokens[0];
46913 cp_parser_push_lexer_for_tokens (parser, ce);
46914 parser->lexer->in_pragma = true;
46915 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
46917 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
46918 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
46919 parser->oacc_routine->clauses
46920 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
46921 "#pragma acc routine", pragma_tok);
46922 /* The clauses are in reverse order; fix that to make later diagnostic
46923 emission easier. */
46924 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
46925 cp_parser_pop_lexer (parser);
46926 /* Later, cp_finalize_oacc_routine will process the clauses. */
46927 parser->oacc_routine->fndecl_seen = true;
46929 return attrs;
46932 /* Apply any saved OpenACC routine clauses to a just-parsed
46933 declaration. */
46935 static void
46936 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
46938 if (__builtin_expect (parser->oacc_routine != NULL, 0))
46940 /* Keep going if we're in error reporting mode. */
46941 if (parser->oacc_routine->error_seen
46942 || fndecl == error_mark_node)
46943 return;
46945 if (TREE_CODE (fndecl) != FUNCTION_DECL)
46947 if (parser->oacc_routine->fndecl_seen)
46949 error_at (parser->oacc_routine->loc,
46950 "%<#pragma acc routine%> not immediately followed by"
46951 " a single function declaration or definition");
46952 parser->oacc_routine = NULL;
46953 return;
46956 cp_ensure_no_oacc_routine (parser);
46957 return;
46960 int compatible
46961 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
46962 parser->oacc_routine->loc,
46963 "#pragma acc routine");
46964 if (compatible < 0)
46966 parser->oacc_routine = NULL;
46967 return;
46969 if (compatible > 0)
46972 else
46974 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
46976 error_at (parser->oacc_routine->loc,
46977 TREE_USED (fndecl)
46978 ? G_("%<#pragma acc routine%> must be applied before"
46979 " use")
46980 : G_("%<#pragma acc routine%> must be applied before"
46981 " definition"));
46982 parser->oacc_routine = NULL;
46983 return;
46986 /* Set the routine's level of parallelism. */
46987 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
46988 oacc_replace_fn_attrib (fndecl, dims);
46990 /* Add an "omp declare target" attribute. */
46991 DECL_ATTRIBUTES (fndecl)
46992 = tree_cons (get_identifier ("omp declare target"),
46993 parser->oacc_routine->clauses,
46994 DECL_ATTRIBUTES (fndecl));
46999 /* Main entry point to OpenMP statement pragmas. */
47001 static void
47002 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47004 tree stmt;
47005 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
47006 omp_clause_mask mask (0);
47008 switch (cp_parser_pragma_kind (pragma_tok))
47010 case PRAGMA_OACC_ATOMIC:
47011 cp_parser_omp_atomic (parser, pragma_tok, true);
47012 return;
47013 case PRAGMA_OACC_CACHE:
47014 stmt = cp_parser_oacc_cache (parser, pragma_tok);
47015 break;
47016 case PRAGMA_OACC_DATA:
47017 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
47018 break;
47019 case PRAGMA_OACC_ENTER_DATA:
47020 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
47021 break;
47022 case PRAGMA_OACC_EXIT_DATA:
47023 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
47024 break;
47025 case PRAGMA_OACC_HOST_DATA:
47026 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
47027 break;
47028 case PRAGMA_OACC_KERNELS:
47029 case PRAGMA_OACC_PARALLEL:
47030 case PRAGMA_OACC_SERIAL:
47031 strcpy (p_name, "#pragma acc");
47032 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
47033 break;
47034 case PRAGMA_OACC_LOOP:
47035 strcpy (p_name, "#pragma acc");
47036 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
47037 if_p);
47038 break;
47039 case PRAGMA_OACC_UPDATE:
47040 stmt = cp_parser_oacc_update (parser, pragma_tok);
47041 break;
47042 case PRAGMA_OACC_WAIT:
47043 stmt = cp_parser_oacc_wait (parser, pragma_tok);
47044 break;
47045 case PRAGMA_OMP_ALLOCATE:
47046 cp_parser_omp_allocate (parser, pragma_tok);
47047 return;
47048 case PRAGMA_OMP_ATOMIC:
47049 cp_parser_omp_atomic (parser, pragma_tok, false);
47050 return;
47051 case PRAGMA_OMP_CRITICAL:
47052 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
47053 break;
47054 case PRAGMA_OMP_DISTRIBUTE:
47055 strcpy (p_name, "#pragma omp");
47056 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
47057 if_p);
47058 break;
47059 case PRAGMA_OMP_FOR:
47060 strcpy (p_name, "#pragma omp");
47061 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
47062 if_p);
47063 break;
47064 case PRAGMA_OMP_LOOP:
47065 strcpy (p_name, "#pragma omp");
47066 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
47067 if_p);
47068 break;
47069 case PRAGMA_OMP_MASKED:
47070 strcpy (p_name, "#pragma omp");
47071 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
47072 if_p);
47073 break;
47074 case PRAGMA_OMP_MASTER:
47075 strcpy (p_name, "#pragma omp");
47076 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
47077 if_p);
47078 break;
47079 case PRAGMA_OMP_PARALLEL:
47080 strcpy (p_name, "#pragma omp");
47081 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
47082 if_p);
47083 break;
47084 case PRAGMA_OMP_SCOPE:
47085 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
47086 break;
47087 case PRAGMA_OMP_SECTIONS:
47088 strcpy (p_name, "#pragma omp");
47089 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
47090 break;
47091 case PRAGMA_OMP_SIMD:
47092 strcpy (p_name, "#pragma omp");
47093 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
47094 if_p);
47095 break;
47096 case PRAGMA_OMP_SINGLE:
47097 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
47098 break;
47099 case PRAGMA_OMP_TASK:
47100 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
47101 break;
47102 case PRAGMA_OMP_TASKGROUP:
47103 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
47104 break;
47105 case PRAGMA_OMP_TASKLOOP:
47106 strcpy (p_name, "#pragma omp");
47107 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
47108 if_p);
47109 break;
47110 case PRAGMA_OMP_TEAMS:
47111 strcpy (p_name, "#pragma omp");
47112 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
47113 if_p);
47114 break;
47115 default:
47116 gcc_unreachable ();
47119 protected_set_expr_location (stmt, pragma_tok->location);
47122 /* Transactional Memory parsing routines. */
47124 /* Parse a transaction attribute.
47126 txn-attribute:
47127 attribute
47128 [ [ identifier ] ]
47130 We use this instead of cp_parser_attributes_opt for transactions to avoid
47131 the pedwarn in C++98 mode. */
47133 static tree
47134 cp_parser_txn_attribute_opt (cp_parser *parser)
47136 cp_token *token;
47137 tree attr_name, attr = NULL;
47139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
47140 return cp_parser_attributes_opt (parser);
47142 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
47143 return NULL_TREE;
47144 cp_lexer_consume_token (parser->lexer);
47145 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
47146 goto error1;
47148 token = cp_lexer_peek_token (parser->lexer);
47149 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
47151 token = cp_lexer_consume_token (parser->lexer);
47153 attr_name = (token->type == CPP_KEYWORD
47154 /* For keywords, use the canonical spelling,
47155 not the parsed identifier. */
47156 ? ridpointers[(int) token->keyword]
47157 : token->u.value);
47158 attr = build_tree_list (attr_name, NULL_TREE);
47160 else
47161 cp_parser_error (parser, "expected identifier");
47163 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
47164 error1:
47165 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
47166 return attr;
47169 /* Parse a __transaction_atomic or __transaction_relaxed statement.
47171 transaction-statement:
47172 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
47173 compound-statement
47174 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
47177 static tree
47178 cp_parser_transaction (cp_parser *parser, cp_token *token)
47180 unsigned char old_in = parser->in_transaction;
47181 unsigned char this_in = 1, new_in;
47182 enum rid keyword = token->keyword;
47183 tree stmt, attrs, noex;
47185 cp_lexer_consume_token (parser->lexer);
47187 if (keyword == RID_TRANSACTION_RELAXED
47188 || keyword == RID_SYNCHRONIZED)
47189 this_in |= TM_STMT_ATTR_RELAXED;
47190 else
47192 attrs = cp_parser_txn_attribute_opt (parser);
47193 if (attrs)
47194 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
47197 /* Parse a noexcept specification. */
47198 if (keyword == RID_ATOMIC_NOEXCEPT)
47199 noex = boolean_true_node;
47200 else if (keyword == RID_ATOMIC_CANCEL)
47202 /* cancel-and-throw is unimplemented. */
47203 sorry ("%<atomic_cancel%>");
47204 noex = NULL_TREE;
47206 else
47207 noex = cp_parser_noexcept_specification_opt (parser,
47208 CP_PARSER_FLAGS_NONE,
47209 /*require_constexpr=*/true,
47210 /*consumed_expr=*/NULL,
47211 /*return_cond=*/true);
47213 /* Keep track if we're in the lexical scope of an outer transaction. */
47214 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
47216 stmt = begin_transaction_stmt (token->location, NULL, this_in);
47218 parser->in_transaction = new_in;
47219 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
47220 parser->in_transaction = old_in;
47222 finish_transaction_stmt (stmt, NULL, this_in, noex);
47224 return stmt;
47227 /* Parse a __transaction_atomic or __transaction_relaxed expression.
47229 transaction-expression:
47230 __transaction_atomic txn-noexcept-spec[opt] ( expression )
47231 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
47234 static tree
47235 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
47237 unsigned char old_in = parser->in_transaction;
47238 unsigned char this_in = 1;
47239 cp_token *token;
47240 tree expr, noex;
47241 bool noex_expr;
47242 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47244 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
47245 || keyword == RID_TRANSACTION_RELAXED);
47247 if (!flag_tm)
47248 error_at (loc,
47249 keyword == RID_TRANSACTION_RELAXED
47250 ? G_("%<__transaction_relaxed%> without transactional memory "
47251 "support enabled")
47252 : G_("%<__transaction_atomic%> without transactional memory "
47253 "support enabled"));
47255 token = cp_parser_require_keyword (parser, keyword,
47256 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
47257 : RT_TRANSACTION_RELAXED));
47258 gcc_assert (token != NULL);
47260 if (keyword == RID_TRANSACTION_RELAXED)
47261 this_in |= TM_STMT_ATTR_RELAXED;
47263 /* Set this early. This might mean that we allow transaction_cancel in
47264 an expression that we find out later actually has to be a constexpr.
47265 However, we expect that cxx_constant_value will be able to deal with
47266 this; also, if the noexcept has no constexpr, then what we parse next
47267 really is a transaction's body. */
47268 parser->in_transaction = this_in;
47270 /* Parse a noexcept specification. */
47271 noex = cp_parser_noexcept_specification_opt (parser,
47272 CP_PARSER_FLAGS_NONE,
47273 /*require_constexpr=*/false,
47274 &noex_expr,
47275 /*return_cond=*/true);
47277 if (!noex || !noex_expr
47278 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
47280 matching_parens parens;
47281 parens.require_open (parser);
47283 expr = cp_parser_expression (parser);
47284 expr = finish_parenthesized_expr (expr);
47286 parens.require_close (parser);
47288 else
47290 /* The only expression that is available got parsed for the noexcept
47291 already. noexcept is true then. */
47292 expr = noex;
47293 noex = boolean_true_node;
47296 expr = build_transaction_expr (token->location, expr, this_in, noex);
47297 parser->in_transaction = old_in;
47299 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
47300 return error_mark_node;
47302 return (flag_tm ? expr : error_mark_node);
47305 /* Parse a function-transaction-block.
47307 function-transaction-block:
47308 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
47309 function-body
47310 __transaction_atomic txn-attribute[opt] function-try-block
47311 __transaction_relaxed ctor-initializer[opt] function-body
47312 __transaction_relaxed function-try-block
47315 static void
47316 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
47318 unsigned char old_in = parser->in_transaction;
47319 unsigned char new_in = 1;
47320 tree compound_stmt, stmt, attrs;
47321 cp_token *token;
47323 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
47324 || keyword == RID_TRANSACTION_RELAXED);
47325 token = cp_parser_require_keyword (parser, keyword,
47326 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
47327 : RT_TRANSACTION_RELAXED));
47328 gcc_assert (token != NULL);
47330 if (keyword == RID_TRANSACTION_RELAXED)
47331 new_in |= TM_STMT_ATTR_RELAXED;
47332 else
47334 attrs = cp_parser_txn_attribute_opt (parser);
47335 if (attrs)
47336 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
47339 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
47341 parser->in_transaction = new_in;
47343 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
47344 cp_parser_function_try_block (parser);
47345 else
47346 cp_parser_ctor_initializer_opt_and_function_body
47347 (parser, /*in_function_try_block=*/false);
47349 parser->in_transaction = old_in;
47351 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
47354 /* Parse a __transaction_cancel statement.
47356 cancel-statement:
47357 __transaction_cancel txn-attribute[opt] ;
47358 __transaction_cancel txn-attribute[opt] throw-expression ;
47360 ??? Cancel and throw is not yet implemented. */
47362 static tree
47363 cp_parser_transaction_cancel (cp_parser *parser)
47365 cp_token *token;
47366 bool is_outer = false;
47367 tree stmt, attrs;
47369 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
47370 RT_TRANSACTION_CANCEL);
47371 gcc_assert (token != NULL);
47373 attrs = cp_parser_txn_attribute_opt (parser);
47374 if (attrs)
47375 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
47377 /* ??? Parse cancel-and-throw here. */
47379 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
47381 if (!flag_tm)
47383 error_at (token->location, "%<__transaction_cancel%> without "
47384 "transactional memory support enabled");
47385 return error_mark_node;
47387 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
47389 error_at (token->location, "%<__transaction_cancel%> within a "
47390 "%<__transaction_relaxed%>");
47391 return error_mark_node;
47393 else if (is_outer)
47395 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
47396 && !is_tm_may_cancel_outer (current_function_decl))
47398 error_at (token->location, "outer %<__transaction_cancel%> not "
47399 "within outer %<__transaction_atomic%>");
47400 error_at (token->location,
47401 " or a %<transaction_may_cancel_outer%> function");
47402 return error_mark_node;
47405 else if (parser->in_transaction == 0)
47407 error_at (token->location, "%<__transaction_cancel%> not within "
47408 "%<__transaction_atomic%>");
47409 return error_mark_node;
47412 stmt = build_tm_abort_call (token->location, is_outer);
47413 add_stmt (stmt);
47415 return stmt;
47418 /* The parser. */
47420 static GTY (()) cp_parser *the_parser;
47423 /* Special handling for the first token or line in the file. The first
47424 thing in the file might be #pragma GCC pch_preprocess, which loads a
47425 PCH file, which is a GC collection point. So we need to handle this
47426 first pragma without benefit of an existing lexer structure.
47428 Always returns one token to the caller in *FIRST_TOKEN. This is
47429 either the true first token of the file, or the first token after
47430 the initial pragma. */
47432 static void
47433 cp_parser_initial_pragma (cp_token *first_token)
47435 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
47436 return;
47438 cp_lexer_get_preprocessor_token (0, first_token);
47440 tree name = NULL;
47441 if (first_token->type == CPP_STRING)
47443 name = first_token->u.value;
47445 cp_lexer_get_preprocessor_token (0, first_token);
47448 /* Skip to the end of the pragma. */
47449 if (first_token->type != CPP_PRAGMA_EOL)
47451 error_at (first_token->location,
47452 "malformed %<#pragma GCC pch_preprocess%>");
47454 cp_lexer_get_preprocessor_token (0, first_token);
47455 while (first_token->type != CPP_PRAGMA_EOL);
47458 /* Now actually load the PCH file. */
47459 if (name)
47460 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
47462 /* Read one more token to return to our caller. We have to do this
47463 after reading the PCH file in, since its pointers have to be
47464 live. */
47465 cp_lexer_get_preprocessor_token (0, first_token);
47468 /* Parse a pragma GCC ivdep. */
47470 static bool
47471 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
47473 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47474 return true;
47477 /* Parse a pragma GCC unroll. */
47479 static unsigned short
47480 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
47482 location_t location = cp_lexer_peek_token (parser->lexer)->location;
47483 tree expr = cp_parser_constant_expression (parser);
47484 unsigned short unroll;
47485 expr = maybe_constant_value (expr);
47486 HOST_WIDE_INT lunroll = 0;
47487 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
47488 || TREE_CODE (expr) != INTEGER_CST
47489 || (lunroll = tree_to_shwi (expr)) < 0
47490 || lunroll >= USHRT_MAX)
47492 error_at (location, "%<#pragma GCC unroll%> requires an"
47493 " assignment-expression that evaluates to a non-negative"
47494 " integral constant less than %u", USHRT_MAX);
47495 unroll = 0;
47497 else
47499 unroll = (unsigned short)lunroll;
47500 if (unroll == 0)
47501 unroll = 1;
47503 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47504 return unroll;
47507 /* Normal parsing of a pragma token. Here we can (and must) use the
47508 regular lexer. */
47510 static bool
47511 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
47513 cp_token *pragma_tok;
47514 unsigned int id;
47515 tree stmt;
47516 bool ret = false;
47518 pragma_tok = cp_lexer_consume_token (parser->lexer);
47519 gcc_assert (pragma_tok->type == CPP_PRAGMA);
47520 parser->lexer->in_pragma = true;
47522 id = cp_parser_pragma_kind (pragma_tok);
47523 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
47524 cp_ensure_no_omp_declare_simd (parser);
47525 switch (id)
47527 case PRAGMA_GCC_PCH_PREPROCESS:
47528 error_at (pragma_tok->location,
47529 "%<#pragma GCC pch_preprocess%> must be first");
47530 break;
47532 case PRAGMA_OMP_BARRIER:
47533 switch (context)
47535 case pragma_compound:
47536 cp_parser_omp_barrier (parser, pragma_tok);
47537 return false;
47538 case pragma_stmt:
47539 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47540 "used in compound statements", "omp barrier");
47541 ret = true;
47542 break;
47543 default:
47544 goto bad_stmt;
47546 break;
47548 case PRAGMA_OMP_DEPOBJ:
47549 switch (context)
47551 case pragma_compound:
47552 cp_parser_omp_depobj (parser, pragma_tok);
47553 return false;
47554 case pragma_stmt:
47555 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47556 "used in compound statements", "omp depobj");
47557 ret = true;
47558 break;
47559 default:
47560 goto bad_stmt;
47562 break;
47564 case PRAGMA_OMP_FLUSH:
47565 switch (context)
47567 case pragma_compound:
47568 cp_parser_omp_flush (parser, pragma_tok);
47569 return false;
47570 case pragma_stmt:
47571 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47572 "used in compound statements", "omp flush");
47573 ret = true;
47574 break;
47575 default:
47576 goto bad_stmt;
47578 break;
47580 case PRAGMA_OMP_TASKWAIT:
47581 switch (context)
47583 case pragma_compound:
47584 cp_parser_omp_taskwait (parser, pragma_tok);
47585 return false;
47586 case pragma_stmt:
47587 error_at (pragma_tok->location,
47588 "%<#pragma %s%> may only be used in compound statements",
47589 "omp taskwait");
47590 ret = true;
47591 break;
47592 default:
47593 goto bad_stmt;
47595 break;
47597 case PRAGMA_OMP_TASKYIELD:
47598 switch (context)
47600 case pragma_compound:
47601 cp_parser_omp_taskyield (parser, pragma_tok);
47602 return false;
47603 case pragma_stmt:
47604 error_at (pragma_tok->location,
47605 "%<#pragma %s%> may only be used in compound statements",
47606 "omp taskyield");
47607 ret = true;
47608 break;
47609 default:
47610 goto bad_stmt;
47612 break;
47614 case PRAGMA_OMP_CANCEL:
47615 switch (context)
47617 case pragma_compound:
47618 cp_parser_omp_cancel (parser, pragma_tok);
47619 return false;
47620 case pragma_stmt:
47621 error_at (pragma_tok->location,
47622 "%<#pragma %s%> may only be used in compound statements",
47623 "omp cancel");
47624 ret = true;
47625 break;
47626 default:
47627 goto bad_stmt;
47629 break;
47631 case PRAGMA_OMP_CANCELLATION_POINT:
47632 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
47634 case PRAGMA_OMP_THREADPRIVATE:
47635 cp_parser_omp_threadprivate (parser, pragma_tok);
47636 return false;
47638 case PRAGMA_OMP_DECLARE:
47639 return cp_parser_omp_declare (parser, pragma_tok, context);
47641 case PRAGMA_OACC_DECLARE:
47642 cp_parser_oacc_declare (parser, pragma_tok);
47643 return false;
47645 case PRAGMA_OACC_ENTER_DATA:
47646 if (context == pragma_stmt)
47648 error_at (pragma_tok->location,
47649 "%<#pragma %s%> may only be used in compound statements",
47650 "acc enter data");
47651 ret = true;
47652 break;
47654 else if (context != pragma_compound)
47655 goto bad_stmt;
47656 cp_parser_omp_construct (parser, pragma_tok, if_p);
47657 return true;
47659 case PRAGMA_OACC_EXIT_DATA:
47660 if (context == pragma_stmt)
47662 error_at (pragma_tok->location,
47663 "%<#pragma %s%> may only be used in compound statements",
47664 "acc exit data");
47665 ret = true;
47666 break;
47668 else if (context != pragma_compound)
47669 goto bad_stmt;
47670 cp_parser_omp_construct (parser, pragma_tok, if_p);
47671 return true;
47673 case PRAGMA_OACC_ROUTINE:
47674 if (context != pragma_external)
47676 error_at (pragma_tok->location,
47677 "%<#pragma acc routine%> must be at file scope");
47678 ret = true;
47679 break;
47681 cp_parser_oacc_routine (parser, pragma_tok, context);
47682 return false;
47684 case PRAGMA_OACC_UPDATE:
47685 if (context == pragma_stmt)
47687 error_at (pragma_tok->location,
47688 "%<#pragma %s%> may only be used in compound statements",
47689 "acc update");
47690 ret = true;
47691 break;
47693 else if (context != pragma_compound)
47694 goto bad_stmt;
47695 cp_parser_omp_construct (parser, pragma_tok, if_p);
47696 return true;
47698 case PRAGMA_OACC_WAIT:
47699 if (context == pragma_stmt)
47701 error_at (pragma_tok->location,
47702 "%<#pragma %s%> may only be used in compound statements",
47703 "acc wait");
47704 ret = true;
47705 break;
47707 else if (context != pragma_compound)
47708 goto bad_stmt;
47709 cp_parser_omp_construct (parser, pragma_tok, if_p);
47710 return true;
47711 case PRAGMA_OMP_ALLOCATE:
47712 cp_parser_omp_allocate (parser, pragma_tok);
47713 return false;
47714 case PRAGMA_OACC_ATOMIC:
47715 case PRAGMA_OACC_CACHE:
47716 case PRAGMA_OACC_DATA:
47717 case PRAGMA_OACC_HOST_DATA:
47718 case PRAGMA_OACC_KERNELS:
47719 case PRAGMA_OACC_LOOP:
47720 case PRAGMA_OACC_PARALLEL:
47721 case PRAGMA_OACC_SERIAL:
47722 case PRAGMA_OMP_ATOMIC:
47723 case PRAGMA_OMP_CRITICAL:
47724 case PRAGMA_OMP_DISTRIBUTE:
47725 case PRAGMA_OMP_FOR:
47726 case PRAGMA_OMP_LOOP:
47727 case PRAGMA_OMP_MASKED:
47728 case PRAGMA_OMP_MASTER:
47729 case PRAGMA_OMP_PARALLEL:
47730 case PRAGMA_OMP_SCOPE:
47731 case PRAGMA_OMP_SECTIONS:
47732 case PRAGMA_OMP_SIMD:
47733 case PRAGMA_OMP_SINGLE:
47734 case PRAGMA_OMP_TASK:
47735 case PRAGMA_OMP_TASKGROUP:
47736 case PRAGMA_OMP_TASKLOOP:
47737 case PRAGMA_OMP_TEAMS:
47738 if (context != pragma_stmt && context != pragma_compound)
47739 goto bad_stmt;
47740 stmt = push_omp_privatization_clauses (false);
47741 cp_parser_omp_construct (parser, pragma_tok, if_p);
47742 pop_omp_privatization_clauses (stmt);
47743 return true;
47745 case PRAGMA_OMP_REQUIRES:
47746 if (context != pragma_external)
47748 error_at (pragma_tok->location,
47749 "%<#pragma omp requires%> may only be used at file or "
47750 "namespace scope");
47751 ret = true;
47752 break;
47754 return cp_parser_omp_requires (parser, pragma_tok);
47756 case PRAGMA_OMP_NOTHING:
47757 cp_parser_omp_nothing (parser, pragma_tok);
47758 return false;
47760 case PRAGMA_OMP_ERROR:
47761 return cp_parser_omp_error (parser, pragma_tok, context);
47763 case PRAGMA_OMP_ORDERED:
47764 if (context != pragma_stmt && context != pragma_compound)
47765 goto bad_stmt;
47766 stmt = push_omp_privatization_clauses (false);
47767 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
47768 pop_omp_privatization_clauses (stmt);
47769 return ret;
47771 case PRAGMA_OMP_TARGET:
47772 if (context != pragma_stmt && context != pragma_compound)
47773 goto bad_stmt;
47774 stmt = push_omp_privatization_clauses (false);
47775 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
47776 pop_omp_privatization_clauses (stmt);
47777 return ret;
47779 case PRAGMA_OMP_END_DECLARE_TARGET:
47780 cp_parser_omp_end_declare_target (parser, pragma_tok);
47781 return false;
47783 case PRAGMA_OMP_SCAN:
47784 error_at (pragma_tok->location,
47785 "%<#pragma omp scan%> may only be used in "
47786 "a loop construct with %<inscan%> %<reduction%> clause");
47787 break;
47789 case PRAGMA_OMP_SECTION:
47790 error_at (pragma_tok->location,
47791 "%<#pragma omp section%> may only be used in "
47792 "%<#pragma omp sections%> construct");
47793 break;
47795 case PRAGMA_IVDEP:
47797 if (context == pragma_external)
47799 error_at (pragma_tok->location,
47800 "%<#pragma GCC ivdep%> must be inside a function");
47801 break;
47803 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
47804 unsigned short unroll;
47805 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
47806 if (tok->type == CPP_PRAGMA
47807 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
47809 tok = cp_lexer_consume_token (parser->lexer);
47810 unroll = cp_parser_pragma_unroll (parser, tok);
47811 tok = cp_lexer_peek_token (the_parser->lexer);
47813 else
47814 unroll = 0;
47815 if (tok->type != CPP_KEYWORD
47816 || (tok->keyword != RID_FOR
47817 && tok->keyword != RID_WHILE
47818 && tok->keyword != RID_DO))
47820 cp_parser_error (parser, "for, while or do statement expected");
47821 return false;
47823 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
47824 return true;
47827 case PRAGMA_UNROLL:
47829 if (context == pragma_external)
47831 error_at (pragma_tok->location,
47832 "%<#pragma GCC unroll%> must be inside a function");
47833 break;
47835 const unsigned short unroll
47836 = cp_parser_pragma_unroll (parser, pragma_tok);
47837 bool ivdep;
47838 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
47839 if (tok->type == CPP_PRAGMA
47840 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
47842 tok = cp_lexer_consume_token (parser->lexer);
47843 ivdep = cp_parser_pragma_ivdep (parser, tok);
47844 tok = cp_lexer_peek_token (the_parser->lexer);
47846 else
47847 ivdep = false;
47848 if (tok->type != CPP_KEYWORD
47849 || (tok->keyword != RID_FOR
47850 && tok->keyword != RID_WHILE
47851 && tok->keyword != RID_DO))
47853 cp_parser_error (parser, "for, while or do statement expected");
47854 return false;
47856 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
47857 return true;
47860 default:
47861 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
47862 c_invoke_pragma_handler (id);
47863 break;
47865 bad_stmt:
47866 cp_parser_error (parser, "expected declaration specifiers");
47867 break;
47870 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47871 return ret;
47874 /* The interface the pragma parsers have to the lexer. */
47876 enum cpp_ttype
47877 pragma_lex (tree *value, location_t *loc)
47879 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
47880 enum cpp_ttype ret = tok->type;
47882 *value = tok->u.value;
47883 if (loc)
47884 *loc = tok->location;
47886 if (ret == CPP_PRAGMA_EOL)
47887 ret = CPP_EOF;
47888 else if (ret == CPP_STRING)
47889 *value = cp_parser_string_literal (the_parser, false, false);
47890 else
47892 if (ret == CPP_KEYWORD)
47893 ret = CPP_NAME;
47894 cp_lexer_consume_token (the_parser->lexer);
47897 return ret;
47901 /* External interface. */
47903 /* Parse one entire translation unit. */
47905 void
47906 c_parse_file (void)
47908 static bool already_called = false;
47910 if (already_called)
47911 fatal_error (input_location,
47912 "multi-source compilation not implemented for C++");
47913 already_called = true;
47915 /* cp_lexer_new_main is called before doing any GC allocation
47916 because tokenization might load a PCH file. */
47917 cp_lexer *lexer = cp_lexer_new_main ();
47919 the_parser = cp_parser_new (lexer);
47921 cp_parser_translation_unit (the_parser);
47922 class_decl_loc_t::diag_mismatched_tags ();
47924 the_parser = NULL;
47926 finish_translation_unit ();
47929 /* Create an identifier for a generic parameter type (a synthesized
47930 template parameter implied by `auto' or a concept identifier). */
47932 static GTY(()) int generic_parm_count;
47933 static tree
47934 make_generic_type_name ()
47936 char buf[32];
47937 sprintf (buf, "auto:%d", ++generic_parm_count);
47938 return get_identifier (buf);
47941 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
47942 (creating a new template parameter list if necessary). Returns the newly
47943 created template type parm. */
47945 static tree
47946 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
47948 /* A requires-clause is not a function and cannot have placeholders. */
47949 if (current_binding_level->kind == sk_block)
47951 error ("placeholder type not allowed in this context");
47952 return error_mark_node;
47955 gcc_assert (current_binding_level->kind == sk_function_parms);
47957 /* We are either continuing a function template that already contains implicit
47958 template parameters, creating a new fully-implicit function template, or
47959 extending an existing explicit function template with implicit template
47960 parameters. */
47962 cp_binding_level *const entry_scope = current_binding_level;
47964 bool become_template = false;
47965 cp_binding_level *parent_scope = 0;
47967 if (parser->implicit_template_scope)
47969 gcc_assert (parser->implicit_template_parms);
47971 current_binding_level = parser->implicit_template_scope;
47973 else
47975 /* Roll back to the existing template parameter scope (in the case of
47976 extending an explicit function template) or introduce a new template
47977 parameter scope ahead of the function parameter scope (or class scope
47978 in the case of out-of-line member definitions). The function scope is
47979 added back after template parameter synthesis below. */
47981 cp_binding_level *scope = entry_scope;
47983 while (scope->kind == sk_function_parms)
47985 parent_scope = scope;
47986 scope = scope->level_chain;
47988 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
47990 /* If not defining a class, then any class scope is a scope level in
47991 an out-of-line member definition. In this case simply wind back
47992 beyond the first such scope to inject the template parameter list.
47993 Otherwise wind back to the class being defined. The latter can
47994 occur in class member friend declarations such as:
47996 class A {
47997 void foo (auto);
47999 class B {
48000 friend void A::foo (auto);
48003 The template parameter list synthesized for the friend declaration
48004 must be injected in the scope of 'B'. This can also occur in
48005 erroneous cases such as:
48007 struct A {
48008 struct B {
48009 void foo (auto);
48011 void B::foo (auto) {}
48014 Here the attempted definition of 'B::foo' within 'A' is ill-formed
48015 but, nevertheless, the template parameter list synthesized for the
48016 declarator should be injected into the scope of 'A' as if the
48017 ill-formed template was specified explicitly. */
48019 while (scope->kind == sk_class && !scope->defining_class_p)
48021 parent_scope = scope;
48022 scope = scope->level_chain;
48026 current_binding_level = scope;
48028 if (scope->kind != sk_template_parms
48029 || !function_being_declared_is_template_p (parser))
48031 /* Introduce a new template parameter list for implicit template
48032 parameters. */
48034 become_template = true;
48036 parser->implicit_template_scope
48037 = begin_scope (sk_template_parms, NULL);
48039 ++processing_template_decl;
48041 parser->fully_implicit_function_template_p = true;
48042 ++parser->num_template_parameter_lists;
48044 else
48046 /* Synthesize implicit template parameters at the end of the explicit
48047 template parameter list. */
48049 gcc_assert (current_template_parms);
48051 parser->implicit_template_scope = scope;
48053 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48054 parser->implicit_template_parms
48055 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
48059 /* Synthesize a new template parameter and track the current template
48060 parameter chain with implicit_template_parms. */
48062 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
48063 tree synth_id = make_generic_type_name ();
48064 tree synth_tmpl_parm;
48065 bool non_type = false;
48067 /* Synthesize the type template parameter. */
48068 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
48069 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
48071 /* Attach the constraint to the parm before processing. */
48072 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
48073 TREE_TYPE (node) = constr;
48074 tree new_parm
48075 = process_template_parm (parser->implicit_template_parms,
48076 input_location,
48077 node,
48078 /*non_type=*/non_type,
48079 /*param_pack=*/false);
48081 /* Mark the synthetic declaration "virtual". This is used when
48082 comparing template-heads to determine if whether an abbreviated
48083 function template is equivalent to an explicit template.
48085 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
48086 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
48088 // Chain the new parameter to the list of implicit parameters.
48089 if (parser->implicit_template_parms)
48090 parser->implicit_template_parms
48091 = TREE_CHAIN (parser->implicit_template_parms);
48092 else
48093 parser->implicit_template_parms = new_parm;
48095 tree new_decl = get_local_decls ();
48096 if (non_type)
48097 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
48098 new_decl = DECL_INITIAL (new_decl);
48100 /* If creating a fully implicit function template, start the new implicit
48101 template parameter list with this synthesized type, otherwise grow the
48102 current template parameter list. */
48104 if (become_template)
48106 parent_scope->level_chain = current_binding_level;
48108 tree new_parms = make_tree_vec (1);
48109 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
48110 current_template_parms = tree_cons (size_int (processing_template_decl),
48111 new_parms, current_template_parms);
48113 else
48115 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48116 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
48117 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
48118 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
48121 /* If the new parameter was constrained, we need to add that to the
48122 constraints in the template parameter list. */
48123 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
48125 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
48126 reqs = combine_constraint_expressions (reqs, req);
48127 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
48130 current_binding_level = entry_scope;
48132 return new_decl;
48135 /* Finish the declaration of a fully implicit function template. Such a
48136 template has no explicit template parameter list so has not been through the
48137 normal template head and tail processing. synthesize_implicit_template_parm
48138 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
48139 provided if the declaration is a class member such that its template
48140 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
48141 form is returned. Otherwise NULL_TREE is returned. */
48143 static tree
48144 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
48146 gcc_assert (parser->fully_implicit_function_template_p);
48148 if (member_decl_opt && member_decl_opt != error_mark_node
48149 && DECL_VIRTUAL_P (member_decl_opt))
48151 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
48152 "implicit templates may not be %<virtual%>");
48153 DECL_VIRTUAL_P (member_decl_opt) = false;
48156 if (member_decl_opt)
48157 member_decl_opt = finish_member_template_decl (member_decl_opt);
48158 end_template_decl ();
48160 parser->fully_implicit_function_template_p = false;
48161 parser->implicit_template_parms = 0;
48162 parser->implicit_template_scope = 0;
48163 --parser->num_template_parameter_lists;
48165 return member_decl_opt;
48168 /* Like finish_fully_implicit_template, but to be used in error
48169 recovery, rearranging scopes so that we restore the state we had
48170 before synthesize_implicit_template_parm inserted the implement
48171 template parms scope. */
48173 static void
48174 abort_fully_implicit_template (cp_parser *parser)
48176 cp_binding_level *return_to_scope = current_binding_level;
48178 if (parser->implicit_template_scope
48179 && return_to_scope != parser->implicit_template_scope)
48181 cp_binding_level *child = return_to_scope;
48182 for (cp_binding_level *scope = child->level_chain;
48183 scope != parser->implicit_template_scope;
48184 scope = child->level_chain)
48185 child = scope;
48186 child->level_chain = parser->implicit_template_scope->level_chain;
48187 parser->implicit_template_scope->level_chain = return_to_scope;
48188 current_binding_level = parser->implicit_template_scope;
48190 else
48191 return_to_scope = return_to_scope->level_chain;
48193 finish_fully_implicit_template (parser, NULL);
48195 gcc_assert (current_binding_level == return_to_scope);
48198 /* Helper function for diagnostics that have complained about things
48199 being used with 'extern "C"' linkage.
48201 Attempt to issue a note showing where the 'extern "C"' linkage began. */
48203 void
48204 maybe_show_extern_c_location (void)
48206 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
48207 inform (the_parser->innermost_linkage_specification_location,
48208 "%<extern \"C\"%> linkage started here");
48211 #include "gt-cp-parser.h"