2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
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/>. */
22 #define INCLUDE_MEMORY
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
48 #include "c-family/known-headers.h"
53 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
54 and c-lex.cc) and the C++ parser. */
56 /* The various kinds of non integral constant we encounter. */
57 enum non_integral_constant
{
59 /* floating-point literal */
63 /* %<__FUNCTION__%> */
65 /* %<__PRETTY_FUNCTION__%> */
73 /* %<typeid%> operator */
75 /* non-constant compound literals */
83 /* an array reference */
89 /* the address of a label */
103 /* calls to overloaded operators */
107 /* a comma operator */
109 /* a call to a constructor */
111 /* a transaction expression */
115 /* The various kinds of errors about name-lookup failing. */
116 enum name_lookup_error
{
121 /* is not a class or namespace */
123 /* is not a class, namespace, or enumeration */
127 /* The various kinds of required token */
128 enum required_token
{
130 RT_SEMICOLON
, /* ';' */
131 RT_OPEN_PAREN
, /* '(' */
132 RT_CLOSE_BRACE
, /* '}' */
133 RT_OPEN_BRACE
, /* '{' */
134 RT_CLOSE_SQUARE
, /* ']' */
135 RT_OPEN_SQUARE
, /* '[' */
139 RT_GREATER
, /* '>' */
141 RT_ELLIPSIS
, /* '...' */
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 */
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 */
166 RT_CATCH
, /* catch */
167 RT_THROW
, /* throw */
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
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
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
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
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
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
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
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
251 static void noexcept_override_late_checks
254 static void cp_parser_initial_pragma
257 static bool cp_parser_omp_declare_reduction_exprs
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
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 [[ ]]. */
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
;
290 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
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
, " ... ");
309 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
311 if (token
== start_token
)
318 if (token
== curr_token
)
319 fprintf (file
, "[[");
321 cp_lexer_print_token (file
, token
);
323 if (token
== curr_token
)
324 fprintf (file
, "]]");
330 case CPP_CLOSE_BRACE
:
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. */
353 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
355 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
359 debug (vec
<cp_token
, va_gc
> &ref
)
361 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
365 debug (vec
<cp_token
, va_gc
> *ptr
)
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. */
378 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
382 fprintf (file
, "%s: ", desc
);
383 print_node_brief (file
, "", t
, 0);
388 /* Dump parser context C to FILE. */
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. */
403 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
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. */
420 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
423 fprintf (file
, "%s: true\n", desc
);
427 /* Print an unparsed function entry UF to FILE. */
430 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
433 cp_default_arg_entry
*default_arg_fn
;
436 fprintf (file
, "\tFunctions with default args:\n");
438 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
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);
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);
464 fprintf (file
, "\n");
468 /* Print the stack of unparsed member functions S to FILE. */
471 cp_debug_print_unparsed_queues (FILE *file
,
472 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
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. */
490 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
492 cp_token
*next_token
, *first_token
, *start_token
;
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
502 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
507 /* Dump debugging information for the given PARSER. If FILE is NULL,
508 the output is printed on stderr. */
511 cp_debug_parser (FILE *file
, cp_parser
*parser
)
513 const size_t window_size
= 20;
515 expanded_location eloc
;
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
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
);
599 debug (cp_parser
&ref
)
601 cp_debug_parser (stderr
, &ref
);
605 debug (cp_parser
*ptr
)
610 fprintf (stderr
, "<nil>\n");
613 /* Allocate memory for a new lexer object and return it. */
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
);
632 /* Return TRUE if token is the start of a module declaration that will be
633 terminated by a CPP_PRAGMA_EOL token. */
635 cp_token_is_module_directive (cp_token
*token
)
637 return token
->keyword
== RID__EXPORT
638 || token
->keyword
== RID__MODULE
639 || token
->keyword
== RID__IMPORT
;
642 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
645 static enum pragma_kind
646 cp_parser_pragma_kind (cp_token
*token
)
648 if (token
->type
!= CPP_PRAGMA
)
650 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
651 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
654 /* Handle early pragmas such as #pragma GCC diagnostic, which needs to be done
655 during preprocessing for the case of preprocessing-related diagnostics. This
656 is called immediately after pushing the CPP_PRAGMA_EOL token onto
660 cp_lexer_handle_early_pragma (cp_lexer
*lexer
)
662 const auto first_token
= lexer
->buffer
->address ();
663 const auto last_token
= first_token
+ lexer
->buffer
->length () - 1;
665 /* Back up to the start of the pragma so pragma_lex () can parse it when
666 c-pragma lib asks it to. */
667 auto begin
= last_token
;
668 gcc_assert (begin
->type
== CPP_PRAGMA_EOL
);
669 while (begin
->type
!= CPP_PRAGMA
)
671 if (cp_token_is_module_directive (begin
))
673 gcc_assert (begin
!= first_token
);
676 gcc_assert (!lexer
->next_token
);
677 gcc_assert (!lexer
->last_token
);
678 lexer
->next_token
= begin
;
679 lexer
->last_token
= last_token
;
682 const unsigned int id
683 = cp_parser_pragma_kind (cp_lexer_consume_token (lexer
));
684 if (id
>= PRAGMA_FIRST_EXTERNAL
)
685 c_invoke_early_pragma_handler (id
);
687 /* Reset to normal state. */
688 lexer
->next_token
= lexer
->last_token
= nullptr;
692 static cp_parser
*cp_parser_new (cp_lexer
*);
693 static GTY (()) cp_parser
*the_parser
;
695 /* Create a new main C++ lexer, the lexer that gets tokens from the
696 preprocessor, and also create the main parser. */
699 cp_lexer_new_main (void)
703 /* It's possible that parsing the first pragma will load a PCH file,
704 which is a GC collection point. So we have to do that before
705 allocating any memory. */
706 cp_lexer_get_preprocessor_token (0, &token
);
707 cp_parser_initial_pragma (&token
);
708 c_common_no_more_pch ();
710 cp_lexer
*lexer
= cp_lexer_alloc ();
711 /* Put the first token in the buffer. */
712 cp_token
*tok
= lexer
->buffer
->quick_push (token
);
714 uintptr_t filter
= 0;
716 filter
= module_token_cdtor (parse_in
, filter
);
718 /* Create the parser now, so we can use it to handle early pragmas. */
719 gcc_assert (!the_parser
);
720 the_parser
= cp_parser_new (lexer
);
722 /* Get the remaining tokens from the preprocessor. */
723 while (tok
->type
!= CPP_EOF
)
726 /* Process the previous token. */
727 module_token_lang (tok
->type
, tok
->keyword
, tok
->u
.value
,
728 tok
->location
, filter
);
730 /* Check for early pragmas that need to be handled now. */
731 if (tok
->type
== CPP_PRAGMA_EOL
)
732 cp_lexer_handle_early_pragma (lexer
);
734 tok
= vec_safe_push (lexer
->buffer
, cp_token ());
735 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN
, tok
);
738 lexer
->next_token
= lexer
->buffer
->address ();
739 lexer
->last_token
= lexer
->next_token
740 + lexer
->buffer
->length ()
743 if (lexer
->buffer
->length () != 1)
745 /* Set the EOF token's location to be the just after the previous
746 token's range. That way 'at-eof' diagnostics point at something
748 auto range
= get_range_from_loc (line_table
, tok
[-1].location
);
750 = linemap_position_for_loc_and_offset (line_table
, range
.m_finish
, 1);
754 module_token_cdtor (parse_in
, filter
);
756 /* Subsequent preprocessor diagnostics should use compiler
757 diagnostic functions to get the compiler source location. */
758 override_libcpp_locations
= true;
760 maybe_check_all_macros (parse_in
);
762 gcc_assert (!lexer
->next_token
->purged_p
);
766 /* Create a new lexer whose token stream is primed with the tokens in
767 CACHE. When these tokens are exhausted, no new tokens will be read. */
770 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
772 cp_token
*first
= cache
->first
;
773 cp_token
*last
= cache
->last
;
774 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
776 /* We do not own the buffer. */
777 lexer
->buffer
= NULL
;
779 /* Insert an EOF token. */
780 lexer
->saved_type
= last
->type
;
781 lexer
->saved_keyword
= last
->keyword
;
782 last
->type
= CPP_EOF
;
783 last
->keyword
= RID_MAX
;
785 lexer
->next_token
= first
;
786 lexer
->last_token
= last
;
788 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
790 /* Initially we are not debugging. */
791 lexer
->debugging_p
= false;
793 gcc_assert (!lexer
->next_token
->purged_p
794 && !lexer
->last_token
->purged_p
);
798 /* Frees all resources associated with LEXER. */
801 cp_lexer_destroy (cp_lexer
*lexer
)
804 vec_free (lexer
->buffer
);
807 /* Restore the token we overwrite with EOF. */
808 lexer
->last_token
->type
= lexer
->saved_type
;
809 lexer
->last_token
->keyword
= lexer
->saved_keyword
;
811 lexer
->saved_tokens
.release ();
815 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
816 be used. The point of this flag is to help the compiler to fold away calls
817 to cp_lexer_debugging_p within this source file at compile time, when the
818 lexer is not being debugged. */
820 #define LEXER_DEBUGGING_ENABLED_P false
822 /* Returns nonzero if debugging information should be output. */
825 cp_lexer_debugging_p (cp_lexer
*lexer
)
827 if (!LEXER_DEBUGGING_ENABLED_P
)
830 return lexer
->debugging_p
;
834 static inline cp_token_position
835 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
837 return lexer
->next_token
- previous_p
;
840 static inline cp_token
*
841 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
847 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
849 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
852 static inline cp_token_position
853 cp_lexer_previous_token_position (cp_lexer
*lexer
)
855 return cp_lexer_token_position (lexer
, true);
858 static inline cp_token
*
859 cp_lexer_previous_token (cp_lexer
*lexer
)
861 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
863 /* Skip past purged tokens. */
866 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
870 return cp_lexer_token_at (lexer
, tp
);
873 /* Same as above, but return NULL when the lexer doesn't own the token
874 buffer or if the next_token is at the start of the token
875 vector or if all previous tokens are purged. */
878 cp_lexer_safe_previous_token (cp_lexer
*lexer
)
881 && lexer
->next_token
!= lexer
->buffer
->address ())
883 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
885 /* Skip past purged tokens. */
888 if (tp
== lexer
->buffer
->address ())
892 return cp_lexer_token_at (lexer
, tp
);
898 /* Overload for make_location, taking the lexer to mean the location of the
901 static inline location_t
902 make_location (location_t caret
, location_t start
, cp_lexer
*lexer
)
904 cp_token
*t
= cp_lexer_previous_token (lexer
);
905 return make_location (caret
, start
, t
->location
);
908 /* Overload for make_location taking tokens instead of locations. */
910 static inline location_t
911 make_location (cp_token
*caret
, cp_token
*start
, cp_token
*end
)
913 return make_location (caret
->location
, start
->location
, end
->location
);
916 /* nonzero if we are presently saving tokens. */
919 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
921 return lexer
->saved_tokens
.length () != 0;
924 /* Store the next token from the preprocessor in *TOKEN. Return true
925 if we reach EOF. If LEXER is NULL, assume we are handling an
926 initial #pragma pch_preprocess, and thus want the lexer to return
929 Diagnostics issued from this function must have their controlling option (if
930 any) in c.opt annotated as a libcpp option via the CppReason property. */
933 cp_lexer_get_preprocessor_token (unsigned flags
, cp_token
*token
)
935 static int is_extern_c
= 0;
937 /* Get a new token from the preprocessor. */
939 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
941 token
->keyword
= RID_MAX
;
942 token
->purged_p
= false;
943 token
->error_reported
= false;
944 token
->tree_check_p
= false;
945 /* Usually never see a zero, but just in case ... */
946 token
->main_source_p
= line_table
->depth
<= 1;
948 /* On some systems, some header files are surrounded by an
949 implicit extern "C" block. Set a flag in the token if it
950 comes from such a header. */
951 is_extern_c
+= pending_lang_change
;
952 pending_lang_change
= 0;
953 token
->implicit_extern_c
= is_extern_c
> 0;
955 /* Check to see if this token is a keyword. */
956 if (token
->type
== CPP_NAME
)
958 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
960 /* Mark this token as a keyword. */
961 token
->type
= CPP_KEYWORD
;
962 /* Record which keyword. */
963 token
->keyword
= C_RID_CODE (token
->u
.value
);
967 if (warn_cxx11_compat
968 && ((C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
969 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
970 /* These are outside the CXX11 range. */
971 || C_RID_CODE (token
->u
.value
) == RID_ALIGNOF
972 || C_RID_CODE (token
->u
.value
) == RID_ALIGNAS
973 || C_RID_CODE (token
->u
.value
)== RID_THREAD
))
975 /* Warn about the C++11 keyword (but still treat it as
977 warning_at (token
->location
, OPT_Wc__11_compat
,
978 "identifier %qE is a keyword in C++11",
981 /* Clear out the C_RID_CODE so we don't warn about this
982 particular identifier-turned-keyword again. */
983 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
985 if (warn_cxx20_compat
986 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX20
987 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX20
)
989 /* Warn about the C++20 keyword (but still treat it as
991 warning_at (token
->location
, OPT_Wc__20_compat
,
992 "identifier %qE is a keyword in C++20",
995 /* Clear out the C_RID_CODE so we don't warn about this
996 particular identifier-turned-keyword again. */
997 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
1000 token
->keyword
= RID_MAX
;
1003 else if (token
->type
== CPP_AT_NAME
)
1005 /* This only happens in Objective-C++; it must be a keyword. */
1006 token
->type
= CPP_KEYWORD
;
1007 switch (C_RID_CODE (token
->u
.value
))
1009 /* Replace 'class' with '@class', 'private' with '@private',
1010 etc. This prevents confusion with the C++ keyword
1011 'class', and makes the tokens consistent with other
1012 Objective-C 'AT' keywords. For example '@class' is
1013 reported as RID_AT_CLASS which is consistent with
1014 '@synchronized', which is reported as
1015 RID_AT_SYNCHRONIZED.
1017 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
1018 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
1019 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
1020 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
1021 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
1022 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
1023 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
1024 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
1025 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
1030 /* Update the globals input_location and the input file stack from TOKEN. */
1032 cp_lexer_set_source_position_from_token (cp_token
*token
)
1034 input_location
= token
->location
;
1037 /* Update the globals input_location and the input file stack from LEXER. */
1039 cp_lexer_set_source_position (cp_lexer
*lexer
)
1041 cp_token
*token
= cp_lexer_peek_token (lexer
);
1042 cp_lexer_set_source_position_from_token (token
);
1045 /* Return a pointer to the next token in the token stream, but do not
1048 static inline cp_token
*
1049 cp_lexer_peek_token (cp_lexer
*lexer
)
1051 if (cp_lexer_debugging_p (lexer
))
1053 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
1054 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
1055 putc ('\n', cp_lexer_debug_stream
);
1057 return lexer
->next_token
;
1060 /* Return true if the next token has the indicated TYPE. */
1063 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
1065 return cp_lexer_peek_token (lexer
)->type
== type
;
1068 /* Return true if the next token does not have the indicated TYPE. */
1071 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
1073 return !cp_lexer_next_token_is (lexer
, type
);
1076 /* Return true if the next token is the indicated KEYWORD. */
1079 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
1081 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
1085 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
1087 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
1091 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
1093 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
1096 /* Return true if KEYWORD can start a decl-specifier. */
1099 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
1103 /* auto specifier: storage-class-specifier in C++,
1104 simple-type-specifier in C++0x. */
1106 /* Storage classes. */
1112 /* Elaborated type specifiers. */
1118 /* Simple type specifiers. */
1133 /* CV qualifiers. */
1136 /* Function specifiers. */
1139 /* friend/typdef/inline specifiers. */
1143 /* GNU extensions. */
1145 /* C++11 extensions. */
1147 case RID_UNDERLYING_TYPE
:
1149 /* C++20 extensions. */
1155 if (keyword
>= RID_FIRST_INT_N
1156 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
1157 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
1163 /* Return true if the next token is a keyword for a decl-specifier. */
1166 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
1170 token
= cp_lexer_peek_token (lexer
);
1171 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
1174 /* Returns TRUE iff the token T begins a decltype type. */
1177 token_is_decltype (cp_token
*t
)
1179 return (t
->keyword
== RID_DECLTYPE
1180 || t
->type
== CPP_DECLTYPE
);
1183 /* Returns TRUE iff the next token begins a decltype type. */
1186 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1188 cp_token
*t
= cp_lexer_peek_token (lexer
);
1189 return token_is_decltype (t
);
1192 /* Called when processing a token with tree_check_value; perform or defer the
1193 associated checks and return the value. */
1196 saved_checks_value (struct tree_check
*check_value
)
1198 /* Perform any access checks that were deferred. */
1199 vec
<deferred_access_check
, va_gc
> *checks
;
1200 deferred_access_check
*chk
;
1201 checks
= check_value
->checks
;
1205 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1206 perform_or_defer_access_check (chk
->binfo
,
1208 chk
->diag_decl
, tf_warning_or_error
);
1210 /* Return the stored value. */
1211 return check_value
->value
;
1214 /* Return a pointer to the Nth token in the token stream. If N is 1,
1215 then this is precisely equivalent to cp_lexer_peek_token (except
1216 that it is not inline). One would like to disallow that case, but
1217 there is one case (cp_parser_nth_token_starts_template_id) where
1218 the caller passes a variable for N and it might be 1. */
1221 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1225 /* N is 1-based, not zero-based. */
1228 if (cp_lexer_debugging_p (lexer
))
1229 fprintf (cp_lexer_debug_stream
,
1230 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1233 token
= lexer
->next_token
;
1234 while (n
&& token
->type
!= CPP_EOF
)
1237 if (!token
->purged_p
)
1241 if (cp_lexer_debugging_p (lexer
))
1243 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1244 putc ('\n', cp_lexer_debug_stream
);
1250 /* Return the next token, and advance the lexer's next_token pointer
1251 to point to the next non-purged token. */
1254 cp_lexer_consume_token (cp_lexer
* lexer
)
1256 cp_token
*token
= lexer
->next_token
;
1260 gcc_assert (token
->type
!= CPP_EOF
);
1261 lexer
->next_token
++;
1263 while (lexer
->next_token
->purged_p
);
1265 cp_lexer_set_source_position_from_token (token
);
1267 /* Provide debugging output. */
1268 if (cp_lexer_debugging_p (lexer
))
1270 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1271 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1272 putc ('\n', cp_lexer_debug_stream
);
1278 /* Permanently remove the next token from the token stream, and
1279 advance the next_token pointer to refer to the next non-purged
1283 cp_lexer_purge_token (cp_lexer
*lexer
)
1285 cp_token
*tok
= lexer
->next_token
;
1287 gcc_assert (tok
->type
!= CPP_EOF
);
1288 tok
->purged_p
= true;
1289 tok
->location
= UNKNOWN_LOCATION
;
1290 tok
->u
.value
= NULL_TREE
;
1291 tok
->keyword
= RID_MAX
;
1295 while (tok
->purged_p
);
1296 lexer
->next_token
= tok
;
1299 /* Permanently remove all tokens after TOK, up to, but not
1300 including, the token that will be returned next by
1301 cp_lexer_peek_token. */
1304 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1306 cp_token
*peek
= lexer
->next_token
;
1308 gcc_assert (tok
< peek
);
1310 for (tok
++; tok
!= peek
; tok
++)
1312 tok
->purged_p
= true;
1313 tok
->location
= UNKNOWN_LOCATION
;
1314 tok
->u
.value
= NULL_TREE
;
1315 tok
->keyword
= RID_MAX
;
1319 /* Begin saving tokens. All tokens consumed after this point will be
1323 cp_lexer_save_tokens (cp_lexer
* lexer
)
1325 /* Provide debugging output. */
1326 if (cp_lexer_debugging_p (lexer
))
1327 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1329 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1332 /* Commit to the portion of the token stream most recently saved. */
1335 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1337 /* Provide debugging output. */
1338 if (cp_lexer_debugging_p (lexer
))
1339 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1341 lexer
->saved_tokens
.pop ();
1344 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1345 to the token stream. Stop saving tokens. */
1348 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1350 /* Provide debugging output. */
1351 if (cp_lexer_debugging_p (lexer
))
1352 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1354 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1357 /* Determines what saved_token_sentinel does when going out of scope. */
1359 enum saved_token_sentinel_mode
{
1365 /* RAII wrapper around the above functions, with sanity checking (the token
1366 stream should be the same at the point of instantiation as it is at the
1367 point of destruction).
1369 Creating a variable saves tokens. MODE determines what happens when the
1370 object is destroyed. STS_COMMIT commits tokens (default),
1371 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1372 rollback() will immediately roll-back tokens and set MODE to
1375 struct saved_token_sentinel
1379 saved_token_sentinel_mode mode
;
1380 saved_token_sentinel (cp_lexer
*_lexer
,
1381 saved_token_sentinel_mode _mode
= STS_COMMIT
)
1382 : lexer (_lexer
), mode (_mode
)
1384 len
= lexer
->saved_tokens
.length ();
1385 cp_lexer_save_tokens (lexer
);
1389 cp_lexer_rollback_tokens (lexer
);
1390 cp_lexer_set_source_position_from_token
1391 (cp_lexer_previous_token (lexer
));
1392 mode
= STS_DONOTHING
;
1394 ~saved_token_sentinel ()
1396 if (mode
== STS_COMMIT
)
1397 cp_lexer_commit_tokens (lexer
);
1398 else if (mode
== STS_ROLLBACK
)
1401 gcc_assert (lexer
->saved_tokens
.length () == len
);
1405 /* Print a representation of the TOKEN on the STREAM. */
1408 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1410 /* We don't use cpp_type2name here because the parser defines
1411 a few tokens of its own. */
1412 static const char *const token_names
[] = {
1413 /* cpplib-defined token types */
1414 #define OP(e, s) #e,
1415 #define TK(e, s) #e,
1419 /* C++ parser token types - see "Manifest constants", above. */
1422 "NESTED_NAME_SPECIFIER",
1425 /* For some tokens, print the associated data. */
1426 switch (token
->type
)
1429 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1430 For example, `struct' is mapped to an INTEGER_CST. */
1431 if (!identifier_p (token
->u
.value
))
1435 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1442 case CPP_UTF8STRING
:
1443 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1447 print_generic_expr (stream
, token
->u
.value
);
1451 /* If we have a name for the token, print it out. Otherwise, we
1452 simply give the numeric code. */
1453 if (token
->type
< ARRAY_SIZE(token_names
))
1454 fputs (token_names
[token
->type
], stream
);
1456 fprintf (stream
, "[%d]", token
->type
);
1462 debug (cp_token
&ref
)
1464 cp_lexer_print_token (stderr
, &ref
);
1465 fprintf (stderr
, "\n");
1469 debug (cp_token
*ptr
)
1474 fprintf (stderr
, "<nil>\n");
1478 /* Start emitting debugging information. */
1481 cp_lexer_start_debugging (cp_lexer
* lexer
)
1483 if (!LEXER_DEBUGGING_ENABLED_P
)
1484 fatal_error (input_location
,
1485 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1487 lexer
->debugging_p
= true;
1488 cp_lexer_debug_stream
= stderr
;
1491 /* Stop emitting debugging information. */
1494 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1496 if (!LEXER_DEBUGGING_ENABLED_P
)
1497 fatal_error (input_location
,
1498 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1500 lexer
->debugging_p
= false;
1501 cp_lexer_debug_stream
= NULL
;
1504 /* Create a new cp_token_cache, representing a range of tokens. */
1506 static cp_token_cache
*
1507 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1509 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1510 cache
->first
= first
;
1515 /* Diagnose if #pragma omp declare simd isn't followed immediately
1516 by function declaration or definition. */
1519 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1521 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1523 error ("%<#pragma omp declare %s%> not immediately followed by "
1524 "function declaration or definition",
1525 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
1526 parser
->omp_declare_simd
= NULL
;
1530 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1531 and put that into "omp declare simd" attribute. */
1534 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1536 if (UNLIKELY (parser
->omp_declare_simd
!= NULL
))
1538 if (fndecl
== error_mark_node
)
1540 parser
->omp_declare_simd
= NULL
;
1543 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1545 cp_ensure_no_omp_declare_simd (parser
);
1551 /* Similarly, but for use in declaration parsing functions
1552 which call cp_parser_handle_directive_omp_attributes. */
1555 cp_finalize_omp_declare_simd (cp_parser
*parser
, cp_omp_declare_simd_data
*data
)
1557 if (parser
->omp_declare_simd
!= data
)
1560 if (!parser
->omp_declare_simd
->error_seen
1561 && !parser
->omp_declare_simd
->fndecl_seen
)
1562 error_at (parser
->omp_declare_simd
->loc
,
1563 "%<declare %s%> directive not immediately followed by "
1564 "function declaration or definition",
1565 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
1566 parser
->omp_declare_simd
= NULL
;
1569 /* Diagnose if #pragma acc routine isn't followed immediately by function
1570 declaration or definition. */
1573 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1575 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1577 error_at (parser
->oacc_routine
->loc
,
1578 "%<#pragma acc routine%> not immediately followed by "
1579 "function declaration or definition");
1580 parser
->oacc_routine
= NULL
;
1584 /* Decl-specifiers. */
1586 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1589 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1591 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1596 /* Nothing other than the parser should be creating declarators;
1597 declarators are a semi-syntactic representation of C++ entities.
1598 Other parts of the front end that need to create entities (like
1599 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1601 static cp_declarator
*make_call_declarator
1602 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
,
1603 tree
, tree
, tree
, tree
, location_t
);
1604 static cp_declarator
*make_array_declarator
1605 (cp_declarator
*, tree
);
1606 static cp_declarator
*make_pointer_declarator
1607 (cp_cv_quals
, cp_declarator
*, tree
);
1608 static cp_declarator
*make_reference_declarator
1609 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1610 static cp_declarator
*make_ptrmem_declarator
1611 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1613 /* An erroneous declarator. */
1614 static cp_declarator
*cp_error_declarator
;
1616 /* The obstack on which declarators and related data structures are
1618 static struct obstack declarator_obstack
;
1620 /* Alloc BYTES from the declarator memory pool. */
1622 static inline void *
1623 alloc_declarator (size_t bytes
)
1625 return obstack_alloc (&declarator_obstack
, bytes
);
1628 /* Allocate a declarator of the indicated KIND. Clear fields that are
1629 common to all declarators. */
1631 static cp_declarator
*
1632 make_declarator (cp_declarator_kind kind
)
1634 cp_declarator
*declarator
;
1636 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1637 declarator
->kind
= kind
;
1638 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1639 declarator
->attributes
= NULL_TREE
;
1640 declarator
->std_attributes
= NULL_TREE
;
1641 declarator
->declarator
= NULL
;
1642 declarator
->parameter_pack_p
= false;
1643 declarator
->id_loc
= UNKNOWN_LOCATION
;
1644 declarator
->init_loc
= UNKNOWN_LOCATION
;
1649 /* Make a declarator for a generalized identifier. If
1650 QUALIFYING_SCOPE is non-NULL, the identifier is
1651 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1652 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1655 static cp_declarator
*
1656 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1657 special_function_kind sfk
, location_t id_location
)
1659 cp_declarator
*declarator
;
1661 /* It is valid to write:
1663 class C { void f(); };
1667 The standard is not clear about whether `typedef const C D' is
1668 legal; as of 2002-09-15 the committee is considering that
1669 question. EDG 3.0 allows that syntax. Therefore, we do as
1671 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1672 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1674 gcc_assert (identifier_p (unqualified_name
)
1675 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1676 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1678 declarator
= make_declarator (cdk_id
);
1679 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1680 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1681 declarator
->u
.id
.sfk
= sfk
;
1682 declarator
->id_loc
= id_location
;
1687 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1688 of modifiers such as const or volatile to apply to the pointer
1689 type, represented as identifiers. ATTRIBUTES represent the attributes that
1690 appertain to the pointer or reference. */
1693 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1696 cp_declarator
*declarator
;
1698 declarator
= make_declarator (cdk_pointer
);
1699 declarator
->declarator
= target
;
1700 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1701 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1704 declarator
->id_loc
= target
->id_loc
;
1705 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1706 target
->parameter_pack_p
= false;
1709 declarator
->parameter_pack_p
= false;
1711 declarator
->std_attributes
= attributes
;
1716 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1717 represent the attributes that appertain to the pointer or
1721 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1722 bool rvalue_ref
, tree attributes
)
1724 cp_declarator
*declarator
;
1726 declarator
= make_declarator (cdk_reference
);
1727 declarator
->declarator
= target
;
1728 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1729 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1732 declarator
->id_loc
= target
->id_loc
;
1733 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1734 target
->parameter_pack_p
= false;
1737 declarator
->parameter_pack_p
= false;
1739 declarator
->std_attributes
= attributes
;
1744 /* Like make_pointer_declarator -- but for a pointer to a non-static
1745 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1746 appertain to the pointer or reference. */
1749 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1750 cp_declarator
*pointee
,
1753 cp_declarator
*declarator
;
1755 declarator
= make_declarator (cdk_ptrmem
);
1756 declarator
->declarator
= pointee
;
1757 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1758 declarator
->u
.pointer
.class_type
= class_type
;
1762 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1763 pointee
->parameter_pack_p
= false;
1766 declarator
->parameter_pack_p
= false;
1768 declarator
->std_attributes
= attributes
;
1773 /* Make a declarator for the function given by TARGET, with the
1774 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1775 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1776 indicates what exceptions can be thrown. */
1779 make_call_declarator (cp_declarator
*target
,
1781 cp_cv_quals cv_qualifiers
,
1782 cp_virt_specifiers virt_specifiers
,
1783 cp_ref_qualifier ref_qualifier
,
1785 tree exception_specification
,
1786 tree late_return_type
,
1787 tree requires_clause
,
1788 location_t parens_loc
)
1790 cp_declarator
*declarator
;
1792 declarator
= make_declarator (cdk_function
);
1793 declarator
->declarator
= target
;
1794 declarator
->u
.function
.parameters
= parms
;
1795 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1796 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1797 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1798 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1799 declarator
->u
.function
.exception_specification
= exception_specification
;
1800 declarator
->u
.function
.late_return_type
= late_return_type
;
1801 declarator
->u
.function
.requires_clause
= requires_clause
;
1802 declarator
->u
.function
.parens_loc
= parens_loc
;
1805 declarator
->id_loc
= target
->id_loc
;
1806 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1807 target
->parameter_pack_p
= false;
1810 declarator
->parameter_pack_p
= false;
1815 /* Make a declarator for an array of BOUNDS elements, each of which is
1816 defined by ELEMENT. */
1819 make_array_declarator (cp_declarator
*element
, tree bounds
)
1821 cp_declarator
*declarator
;
1823 declarator
= make_declarator (cdk_array
);
1824 declarator
->declarator
= element
;
1825 declarator
->u
.array
.bounds
= bounds
;
1828 declarator
->id_loc
= element
->id_loc
;
1829 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1830 element
->parameter_pack_p
= false;
1833 declarator
->parameter_pack_p
= false;
1838 /* Determine whether the declarator we've seen so far can be a
1839 parameter pack, when followed by an ellipsis. */
1841 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1843 if (declarator
&& declarator
->parameter_pack_p
)
1844 /* We already saw an ellipsis. */
1847 /* Search for a declarator name, or any other declarator that goes
1848 after the point where the ellipsis could appear in a parameter
1849 pack. If we find any of these, then this declarator cannot be
1850 made into a parameter pack. */
1852 while (declarator
&& !found
)
1854 switch ((int)declarator
->kind
)
1866 declarator
= declarator
->declarator
;
1874 cp_parameter_declarator
*no_parameters
;
1876 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1877 DECLARATOR and DEFAULT_ARGUMENT. */
1879 cp_parameter_declarator
*
1880 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1881 cp_declarator
*declarator
,
1882 tree default_argument
,
1884 bool template_parameter_pack_p
= false)
1886 cp_parameter_declarator
*parameter
;
1888 parameter
= ((cp_parameter_declarator
*)
1889 alloc_declarator (sizeof (cp_parameter_declarator
)));
1890 parameter
->next
= NULL
;
1891 if (decl_specifiers
)
1892 parameter
->decl_specifiers
= *decl_specifiers
;
1894 clear_decl_specs (¶meter
->decl_specifiers
);
1895 parameter
->declarator
= declarator
;
1896 parameter
->default_argument
= default_argument
;
1897 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1898 parameter
->loc
= loc
;
1903 /* Returns true iff DECLARATOR is a declaration for a function. */
1906 function_declarator_p (const cp_declarator
*declarator
)
1910 if (declarator
->kind
== cdk_function
1911 && declarator
->declarator
->kind
== cdk_id
)
1913 if (declarator
->kind
== cdk_id
1914 || declarator
->kind
== cdk_decomp
1915 || declarator
->kind
== cdk_error
)
1917 declarator
= declarator
->declarator
;
1927 A cp_parser parses the token stream as specified by the C++
1928 grammar. Its job is purely parsing, not semantic analysis. For
1929 example, the parser breaks the token stream into declarators,
1930 expressions, statements, and other similar syntactic constructs.
1931 It does not check that the types of the expressions on either side
1932 of an assignment-statement are compatible, or that a function is
1933 not declared with a parameter of type `void'.
1935 The parser invokes routines elsewhere in the compiler to perform
1936 semantic analysis and to build up the abstract syntax tree for the
1939 The parser (and the template instantiation code, which is, in a
1940 way, a close relative of parsing) are the only parts of the
1941 compiler that should be calling push_scope and pop_scope, or
1942 related functions. The parser (and template instantiation code)
1943 keeps track of what scope is presently active; everything else
1944 should simply honor that. (The code that generates static
1945 initializers may also need to set the scope, in order to check
1946 access control correctly when emitting the initializers.)
1951 The parser is of the standard recursive-descent variety. Upcoming
1952 tokens in the token stream are examined in order to determine which
1953 production to use when parsing a non-terminal. Some C++ constructs
1954 require arbitrary look ahead to disambiguate. For example, it is
1955 impossible, in the general case, to tell whether a statement is an
1956 expression or declaration without scanning the entire statement.
1957 Therefore, the parser is capable of "parsing tentatively." When the
1958 parser is not sure what construct comes next, it enters this mode.
1959 Then, while we attempt to parse the construct, the parser queues up
1960 error messages, rather than issuing them immediately, and saves the
1961 tokens it consumes. If the construct is parsed successfully, the
1962 parser "commits", i.e., it issues any queued error messages and
1963 the tokens that were being preserved are permanently discarded.
1964 If, however, the construct is not parsed successfully, the parser
1965 rolls back its state completely so that it can resume parsing using
1966 a different alternative.
1971 The performance of the parser could probably be improved substantially.
1972 We could often eliminate the need to parse tentatively by looking ahead
1973 a little bit. In some places, this approach might not entirely eliminate
1974 the need to parse tentatively, but it might still speed up the average
1977 /* Flags that are passed to some parsing functions. These values can
1978 be bitwise-ored together. */
1983 CP_PARSER_FLAGS_NONE
= 0x0,
1984 /* The construct is optional. If it is not present, then no error
1985 should be issued. */
1986 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1987 /* When parsing a type-specifier, treat user-defined type-names
1988 as non-type identifiers. */
1989 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1990 /* When parsing a type-specifier, do not try to parse a class-specifier
1991 or enum-specifier. */
1992 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1993 /* When parsing a decl-specifier-seq, only allow type-specifier or
1995 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1996 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1997 for C++20 consteval. */
1998 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10,
1999 /* When parsing a decl-specifier-seq, allow missing typename. */
2000 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20,
2001 /* When parsing of the noexcept-specifier should be delayed. */
2002 CP_PARSER_FLAGS_DELAY_NOEXCEPT
= 0x40,
2003 /* When parsing a consteval declarator. */
2004 CP_PARSER_FLAGS_CONSTEVAL
= 0x80
2007 /* This type is used for parameters and variables which hold
2008 combinations of the above flags. */
2009 typedef int cp_parser_flags
;
2011 /* The different kinds of declarators we want to parse. */
2013 enum cp_parser_declarator_kind
2015 /* We want an abstract declarator. */
2016 CP_PARSER_DECLARATOR_ABSTRACT
,
2017 /* We want a named declarator. */
2018 CP_PARSER_DECLARATOR_NAMED
,
2019 /* We don't mind, but the name must be an unqualified-id. */
2020 CP_PARSER_DECLARATOR_EITHER
2023 /* The precedence values used to parse binary expressions. The minimum value
2024 of PREC must be 1, because zero is reserved to quickly discriminate
2025 binary operators from other tokens. */
2030 PREC_LOGICAL_OR_EXPRESSION
,
2031 PREC_LOGICAL_AND_EXPRESSION
,
2032 PREC_INCLUSIVE_OR_EXPRESSION
,
2033 PREC_EXCLUSIVE_OR_EXPRESSION
,
2034 PREC_AND_EXPRESSION
,
2035 PREC_EQUALITY_EXPRESSION
,
2036 PREC_RELATIONAL_EXPRESSION
,
2037 PREC_SPACESHIP_EXPRESSION
,
2038 PREC_SHIFT_EXPRESSION
,
2039 PREC_ADDITIVE_EXPRESSION
,
2040 PREC_MULTIPLICATIVE_EXPRESSION
,
2042 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
2045 /* A mapping from a token type to a corresponding tree node type, with a
2046 precedence value. */
2048 struct cp_parser_binary_operations_map_node
2050 /* The token type. */
2051 enum cpp_ttype token_type
;
2052 /* The corresponding tree code. */
2053 enum tree_code tree_type
;
2054 /* The precedence of this operator. */
2055 enum cp_parser_prec prec
;
2058 struct cp_parser_expression_stack_entry
2060 /* Left hand side of the binary operation we are currently
2063 /* Original tree code for left hand side, if it was a binary
2064 expression itself (used for -Wparentheses). */
2065 enum tree_code lhs_type
;
2066 /* Tree code for the binary operation we are parsing. */
2067 enum tree_code tree_type
;
2068 /* Precedence of the binary operation we are parsing. */
2069 enum cp_parser_prec prec
;
2070 /* Location of the binary operation we are parsing. */
2072 /* Flags from the operator token. */
2073 unsigned char flags
;
2076 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2077 entries because precedence levels on the stack are monotonically
2079 typedef struct cp_parser_expression_stack_entry
2080 cp_parser_expression_stack
[NUM_PREC_VALUES
];
2084 /* Constructors and destructors. */
2086 static cp_parser_context
*cp_parser_context_new
2087 (cp_parser_context
*);
2089 /* Class variables. */
2091 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
2093 /* The operator-precedence table used by cp_parser_binary_expression.
2094 Transformed into an associative array (binops_by_token) by
2097 static const cp_parser_binary_operations_map_node binops
[] = {
2098 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
2099 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
2101 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
2102 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
2103 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
2105 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
2106 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
2108 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
2109 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
2111 { CPP_SPACESHIP
, SPACESHIP_EXPR
, PREC_SPACESHIP_EXPRESSION
},
2113 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
2114 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
2115 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
2116 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
2118 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
2119 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
2121 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
2123 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
2125 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
2127 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
2129 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
2132 /* The same as binops, but initialized by cp_parser_new so that
2133 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2135 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
2137 /* Constructors and destructors. */
2139 /* Construct a new context. The context below this one on the stack
2140 is given by NEXT. */
2142 static cp_parser_context
*
2143 cp_parser_context_new (cp_parser_context
* next
)
2145 cp_parser_context
*context
;
2147 /* Allocate the storage. */
2148 if (cp_parser_context_free_list
!= NULL
)
2150 /* Pull the first entry from the free list. */
2151 context
= cp_parser_context_free_list
;
2152 cp_parser_context_free_list
= context
->next
;
2153 memset (context
, 0, sizeof (*context
));
2156 context
= ggc_cleared_alloc
<cp_parser_context
> ();
2158 /* No errors have occurred yet in this context. */
2159 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
2160 /* If this is not the bottommost context, copy information that we
2161 need from the previous context. */
2164 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2165 expression, then we are parsing one in this context, too. */
2166 context
->object_type
= next
->object_type
;
2167 /* Thread the stack. */
2168 context
->next
= next
;
2174 /* Managing the unparsed function queues. */
2176 #define unparsed_funs_with_default_args \
2177 parser->unparsed_queues->last ().funs_with_default_args
2178 #define unparsed_funs_with_definitions \
2179 parser->unparsed_queues->last ().funs_with_definitions
2180 #define unparsed_nsdmis \
2181 parser->unparsed_queues->last ().nsdmis
2182 #define unparsed_noexcepts \
2183 parser->unparsed_queues->last ().noexcepts
2186 push_unparsed_function_queues (cp_parser
*parser
)
2188 cp_unparsed_functions_entry e
= { NULL
, make_tree_vector (), NULL
, NULL
};
2189 vec_safe_push (parser
->unparsed_queues
, e
);
2193 pop_unparsed_function_queues (cp_parser
*parser
)
2195 release_tree_vector (unparsed_funs_with_definitions
);
2196 parser
->unparsed_queues
->pop ();
2201 /* Routines to parse various constructs.
2203 Those that return `tree' will return the error_mark_node (rather
2204 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2205 Sometimes, they will return an ordinary node if error-recovery was
2206 attempted, even though a parse error occurred. So, to check
2207 whether or not a parse error occurred, you should always use
2208 cp_parser_error_occurred. If the construct is optional (indicated
2209 either by an `_opt' in the name of the function that does the
2210 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2211 the construct is not present. */
2213 /* Lexical conventions [gram.lex] */
2215 static cp_expr cp_parser_identifier
2217 static cp_expr cp_parser_string_literal
2218 (cp_parser
*, bool, bool, bool);
2219 static cp_expr cp_parser_userdef_char_literal
2221 static tree cp_parser_userdef_string_literal
2223 static cp_expr cp_parser_userdef_numeric_literal
2226 /* Basic concepts [gram.basic] */
2228 static void cp_parser_translation_unit (cp_parser
*);
2230 /* Expressions [gram.expr] */
2232 static cp_expr cp_parser_primary_expression
2233 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2234 static cp_expr cp_parser_id_expression
2235 (cp_parser
*, bool, bool, bool *, bool, bool);
2236 static cp_expr cp_parser_unqualified_id
2237 (cp_parser
*, bool, bool, bool, bool);
2238 static tree cp_parser_nested_name_specifier_opt
2239 (cp_parser
*, bool, bool, bool, bool, bool = false);
2240 static tree cp_parser_nested_name_specifier
2241 (cp_parser
*, bool, bool, bool, bool);
2242 static tree cp_parser_qualifying_entity
2243 (cp_parser
*, bool, bool, bool, bool, bool);
2244 static cp_expr cp_parser_postfix_expression
2245 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2246 static tree cp_parser_postfix_open_square_expression
2247 (cp_parser
*, tree
, bool, bool);
2248 static tree cp_parser_postfix_dot_deref_expression
2249 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2250 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2251 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2253 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2254 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2255 static void cp_parser_pseudo_destructor_name
2256 (cp_parser
*, tree
, tree
*, tree
*);
2257 static cp_expr cp_parser_unary_expression
2258 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2259 static enum tree_code cp_parser_unary_operator
2261 static tree cp_parser_has_attribute_expression
2263 static tree cp_parser_new_expression
2265 static vec
<tree
, va_gc
> *cp_parser_new_placement
2267 static tree cp_parser_new_type_id
2268 (cp_parser
*, tree
*);
2269 static cp_declarator
*cp_parser_new_declarator_opt
2271 static cp_declarator
*cp_parser_direct_new_declarator
2273 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2275 static tree cp_parser_delete_expression
2277 static cp_expr cp_parser_cast_expression
2278 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2279 static cp_expr cp_parser_binary_expression
2280 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2281 static tree cp_parser_question_colon_clause
2282 (cp_parser
*, cp_expr
);
2283 static cp_expr cp_parser_assignment_expression
2284 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2285 static enum tree_code cp_parser_assignment_operator_opt
2287 static cp_expr cp_parser_expression
2288 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2289 static cp_expr cp_parser_constant_expression
2290 (cp_parser
*, int = 0, bool * = NULL
, bool = false);
2291 static cp_expr cp_parser_builtin_offsetof
2293 static cp_expr cp_parser_lambda_expression
2295 static void cp_parser_lambda_introducer
2296 (cp_parser
*, tree
);
2297 static bool cp_parser_lambda_declarator_opt
2298 (cp_parser
*, tree
);
2299 static void cp_parser_lambda_body
2300 (cp_parser
*, tree
);
2302 /* Statements [gram.stmt.stmt] */
2304 static void cp_parser_statement
2305 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2306 static void cp_parser_label_for_labeled_statement
2307 (cp_parser
*, tree
);
2308 static tree cp_parser_expression_statement
2309 (cp_parser
*, tree
);
2310 static tree cp_parser_compound_statement
2311 (cp_parser
*, tree
, int, bool);
2312 static void cp_parser_statement_seq_opt
2313 (cp_parser
*, tree
);
2314 static tree cp_parser_selection_statement
2315 (cp_parser
*, bool *, vec
<tree
> *);
2316 static tree cp_parser_condition
2318 static tree cp_parser_iteration_statement
2319 (cp_parser
*, bool *, bool, unsigned short);
2320 static bool cp_parser_init_statement
2321 (cp_parser
*, tree
*decl
);
2322 static tree cp_parser_for
2323 (cp_parser
*, bool, unsigned short);
2324 static tree cp_parser_c_for
2325 (cp_parser
*, tree
, tree
, bool, unsigned short);
2326 static tree cp_parser_range_for
2327 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2328 static void do_range_for_auto_deduction
2330 static tree cp_parser_perform_range_for_lookup
2331 (tree
, tree
*, tree
*);
2332 static tree cp_parser_range_for_member_function
2334 static tree cp_parser_jump_statement
2336 static void cp_parser_declaration_statement
2339 static tree cp_parser_implicitly_scoped_statement
2340 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2341 static void cp_parser_already_scoped_statement
2342 (cp_parser
*, bool *, const token_indent_info
&);
2344 /* State of module-declaration parsing. */
2347 MP_NOT_MODULE
, /* Not a module. */
2351 MP_FIRST
, /* First declaration of TU. */
2352 MP_GLOBAL
, /* Global Module Fragment. */
2354 MP_PURVIEW_IMPORTS
, /* Imports of a module. */
2355 MP_PURVIEW
, /* Purview of a named module. */
2357 MP_PRIVATE_IMPORTS
, /* Imports of a Private Module Fragment. */
2358 MP_PRIVATE
, /* Private Module Fragment. */
2361 static module_parse cp_parser_module_declaration
2362 (cp_parser
*parser
, module_parse
, bool exporting
);
2363 static void cp_parser_import_declaration
2364 (cp_parser
*parser
, module_parse
, bool exporting
);
2366 /* Declarations [gram.dcl.dcl] */
2368 static void cp_parser_declaration_seq_opt
2370 static void cp_parser_declaration
2371 (cp_parser
*, tree
);
2372 static void cp_parser_toplevel_declaration
2374 static void cp_parser_block_declaration
2375 (cp_parser
*, bool);
2376 static void cp_parser_simple_declaration
2377 (cp_parser
*, bool, tree
*);
2378 static void cp_parser_decl_specifier_seq
2379 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2380 static tree cp_parser_storage_class_specifier_opt
2382 static tree cp_parser_function_specifier_opt
2383 (cp_parser
*, cp_decl_specifier_seq
*);
2384 static tree cp_parser_type_specifier
2385 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2387 static tree cp_parser_simple_type_specifier
2388 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2389 static tree cp_parser_placeholder_type_specifier
2390 (cp_parser
*, location_t
, tree
, bool);
2391 static tree cp_parser_type_name
2392 (cp_parser
*, bool);
2393 static tree cp_parser_nonclass_name
2394 (cp_parser
* parser
);
2395 static tree cp_parser_elaborated_type_specifier
2396 (cp_parser
*, bool, bool);
2397 static tree cp_parser_enum_specifier
2399 static void cp_parser_enumerator_list
2400 (cp_parser
*, tree
);
2401 static void cp_parser_enumerator_definition
2402 (cp_parser
*, tree
);
2403 static tree cp_parser_namespace_name
2405 static void cp_parser_namespace_definition
2407 static void cp_parser_namespace_body
2409 static tree cp_parser_qualified_namespace_specifier
2411 static void cp_parser_namespace_alias_definition
2413 static bool cp_parser_using_declaration
2414 (cp_parser
*, bool);
2415 static void cp_parser_using_directive
2417 static void cp_parser_using_enum
2419 static tree cp_parser_alias_declaration
2421 static void cp_parser_asm_definition
2423 static void cp_parser_linkage_specification
2424 (cp_parser
*, tree
);
2425 static void cp_parser_static_assert
2426 (cp_parser
*, bool);
2427 static tree cp_parser_decltype
2429 static tree cp_parser_decomposition_declaration
2430 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2432 /* Declarators [gram.dcl.decl] */
2434 static tree cp_parser_init_declarator
2435 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*,
2436 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2437 location_t
*, tree
*);
2438 static cp_declarator
*cp_parser_declarator
2439 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2441 static cp_declarator
*cp_parser_direct_declarator
2442 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool,
2444 static enum tree_code cp_parser_ptr_operator
2445 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2446 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2448 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2450 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2452 static tree cp_parser_tx_qualifier_opt
2454 static tree cp_parser_late_return_type_opt
2455 (cp_parser
*, cp_declarator
*, tree
&);
2456 static tree cp_parser_declarator_id
2457 (cp_parser
*, bool);
2458 static tree cp_parser_type_id
2459 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2460 static tree cp_parser_template_type_arg
2462 static tree
cp_parser_trailing_type_id (cp_parser
*);
2463 static tree cp_parser_type_id_1
2464 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2465 static void cp_parser_type_specifier_seq
2466 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2467 static tree cp_parser_parameter_declaration_clause
2468 (cp_parser
*, cp_parser_flags
);
2469 static tree cp_parser_parameter_declaration_list
2470 (cp_parser
*, cp_parser_flags
, auto_vec
<tree
> *);
2471 static cp_parameter_declarator
*cp_parser_parameter_declaration
2472 (cp_parser
*, cp_parser_flags
, bool, bool *);
2473 static tree cp_parser_default_argument
2474 (cp_parser
*, bool);
2475 static void cp_parser_function_body
2476 (cp_parser
*, bool);
2477 static tree cp_parser_initializer
2478 (cp_parser
*, bool *, bool *, bool = false);
2479 static cp_expr cp_parser_initializer_clause
2480 (cp_parser
*, bool *);
2481 static cp_expr cp_parser_braced_list
2482 (cp_parser
*, bool*);
2483 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2484 (cp_parser
*, bool *, bool *);
2486 static void cp_parser_ctor_initializer_opt_and_function_body
2487 (cp_parser
*, bool);
2489 static tree cp_parser_late_parsing_omp_declare_simd
2490 (cp_parser
*, tree
);
2492 static tree cp_parser_late_parsing_oacc_routine
2493 (cp_parser
*, tree
);
2495 static tree synthesize_implicit_template_parm
2496 (cp_parser
*, tree
);
2497 static tree finish_fully_implicit_template
2498 (cp_parser
*, tree
);
2499 static void abort_fully_implicit_template
2502 /* Classes [gram.class] */
2504 static tree cp_parser_class_name
2505 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2506 static tree cp_parser_class_specifier
2508 static tree cp_parser_class_head
2509 (cp_parser
*, bool *);
2510 static enum tag_types cp_parser_class_key
2512 static void cp_parser_type_parameter_key
2513 (cp_parser
* parser
);
2514 static void cp_parser_member_specification_opt
2516 static void cp_parser_member_declaration
2518 static tree cp_parser_pure_specifier
2520 static tree cp_parser_constant_initializer
2523 /* Derived classes [gram.class.derived] */
2525 static tree cp_parser_base_clause
2527 static tree cp_parser_base_specifier
2530 /* Special member functions [gram.special] */
2532 static tree cp_parser_conversion_function_id
2534 static tree cp_parser_conversion_type_id
2536 static cp_declarator
*cp_parser_conversion_declarator_opt
2538 static void cp_parser_ctor_initializer_opt
2540 static void cp_parser_mem_initializer_list
2542 static tree cp_parser_mem_initializer
2544 static tree cp_parser_mem_initializer_id
2547 /* Overloading [gram.over] */
2549 static cp_expr cp_parser_operator_function_id
2551 static cp_expr cp_parser_operator
2552 (cp_parser
*, location_t
);
2554 /* Templates [gram.temp] */
2556 static void cp_parser_template_declaration
2557 (cp_parser
*, bool);
2558 static tree cp_parser_template_parameter_list
2560 static tree cp_parser_template_parameter
2561 (cp_parser
*, bool *, bool *);
2562 static tree cp_parser_type_parameter
2563 (cp_parser
*, bool *);
2564 static tree cp_parser_template_id
2565 (cp_parser
*, bool, bool, enum tag_types
, bool);
2566 static tree cp_parser_template_id_expr
2567 (cp_parser
*, bool, bool, bool);
2568 static tree cp_parser_template_name
2569 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2570 static tree cp_parser_template_argument_list
2572 static tree cp_parser_template_argument
2574 static void cp_parser_explicit_instantiation
2576 static void cp_parser_explicit_specialization
2579 /* Exception handling [gram.except] */
2581 static tree cp_parser_try_block
2583 static void cp_parser_function_try_block
2585 static void cp_parser_handler_seq
2587 static void cp_parser_handler
2589 static tree cp_parser_exception_declaration
2591 static tree cp_parser_throw_expression
2593 static tree cp_parser_exception_specification_opt
2594 (cp_parser
*, cp_parser_flags
);
2595 static tree cp_parser_type_id_list
2597 static tree cp_parser_noexcept_specification_opt
2598 (cp_parser
*, cp_parser_flags
, bool, bool *, bool);
2600 /* GNU Extensions */
2602 static tree cp_parser_asm_specification_opt
2604 static tree cp_parser_asm_operand_list
2606 static tree cp_parser_asm_clobber_list
2608 static tree cp_parser_asm_label_list
2610 static bool cp_next_tokens_can_be_attribute_p
2612 static bool cp_next_tokens_can_be_gnu_attribute_p
2614 static bool cp_next_tokens_can_be_std_attribute_p
2616 static bool cp_nth_tokens_can_be_std_attribute_p
2617 (cp_parser
*, size_t);
2618 static bool cp_nth_tokens_can_be_gnu_attribute_p
2619 (cp_parser
*, size_t);
2620 static bool cp_nth_tokens_can_be_attribute_p
2621 (cp_parser
*, size_t);
2622 static tree cp_parser_attributes_opt
2624 static tree cp_parser_gnu_attributes_opt
2626 static tree cp_parser_gnu_attribute_list
2627 (cp_parser
*, bool = false);
2628 static tree cp_parser_std_attribute
2629 (cp_parser
*, tree
);
2630 static tree cp_parser_std_attribute_spec
2632 static tree cp_parser_std_attribute_spec_seq
2634 static size_t cp_parser_skip_std_attribute_spec_seq
2635 (cp_parser
*, size_t);
2636 static size_t cp_parser_skip_attributes_opt
2637 (cp_parser
*, size_t);
2638 static bool cp_parser_extension_opt
2639 (cp_parser
*, int *);
2640 static void cp_parser_label_declaration
2643 /* Concept Extensions */
2645 static tree cp_parser_concept_definition
2647 static tree cp_parser_constraint_expression
2649 static tree cp_parser_requires_clause_opt
2650 (cp_parser
*, bool);
2651 static tree cp_parser_requires_expression
2653 static tree cp_parser_requirement_parameter_list
2655 static tree cp_parser_requirement_body
2657 static tree cp_parser_requirement_seq
2659 static tree cp_parser_requirement
2661 static tree cp_parser_simple_requirement
2663 static tree cp_parser_compound_requirement
2665 static tree cp_parser_type_requirement
2667 static tree cp_parser_nested_requirement
2670 /* Transactional Memory Extensions */
2672 static tree cp_parser_transaction
2673 (cp_parser
*, cp_token
*);
2674 static tree cp_parser_transaction_expression
2675 (cp_parser
*, enum rid
);
2676 static void cp_parser_function_transaction
2677 (cp_parser
*, enum rid
);
2678 static tree cp_parser_transaction_cancel
2681 /* Coroutine extensions. */
2683 static tree cp_parser_yield_expression
2687 enum pragma_context
{
2694 static bool cp_parser_pragma
2695 (cp_parser
*, enum pragma_context
, bool *);
2697 /* Objective-C++ Productions */
2699 static tree cp_parser_objc_message_receiver
2701 static tree cp_parser_objc_message_args
2703 static tree cp_parser_objc_message_expression
2705 static cp_expr cp_parser_objc_encode_expression
2707 static tree cp_parser_objc_defs_expression
2709 static tree cp_parser_objc_protocol_expression
2711 static tree cp_parser_objc_selector_expression
2713 static cp_expr cp_parser_objc_expression
2715 static bool cp_parser_objc_selector_p
2717 static tree cp_parser_objc_selector
2719 static tree cp_parser_objc_protocol_refs_opt
2721 static void cp_parser_objc_declaration
2722 (cp_parser
*, tree
);
2723 static tree cp_parser_objc_statement
2725 static bool cp_parser_objc_valid_prefix_attributes
2726 (cp_parser
*, tree
*);
2727 static void cp_parser_objc_at_property_declaration
2729 static void cp_parser_objc_at_synthesize_declaration
2731 static void cp_parser_objc_at_dynamic_declaration
2733 static tree cp_parser_objc_struct_declaration
2736 /* Utility Routines */
2738 static cp_expr cp_parser_lookup_name
2739 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2740 static tree cp_parser_lookup_name_simple
2741 (cp_parser
*, tree
, location_t
);
2742 static tree cp_parser_maybe_treat_template_as_class
2744 static bool cp_parser_check_declarator_template_parameters
2745 (cp_parser
*, cp_declarator
*, location_t
);
2746 static bool cp_parser_check_template_parameters
2747 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2748 static cp_expr cp_parser_simple_cast_expression
2750 static tree cp_parser_global_scope_opt
2751 (cp_parser
*, bool);
2752 static bool cp_parser_constructor_declarator_p
2753 (cp_parser
*, cp_parser_flags
, bool);
2754 static tree cp_parser_function_definition_from_specifiers_and_declarator
2755 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2756 static tree cp_parser_function_definition_after_declarator
2757 (cp_parser
*, bool);
2758 static bool cp_parser_template_declaration_after_export
2759 (cp_parser
*, bool);
2760 static void cp_parser_perform_template_parameter_access_checks
2761 (vec
<deferred_access_check
, va_gc
> *);
2762 static tree cp_parser_single_declaration
2763 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2764 static cp_expr cp_parser_functional_cast
2765 (cp_parser
*, tree
);
2766 static tree cp_parser_save_member_function_body
2767 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2768 static tree cp_parser_save_nsdmi
2770 static tree cp_parser_enclosed_template_argument_list
2772 static void cp_parser_save_default_args
2773 (cp_parser
*, tree
);
2774 static void cp_parser_late_parsing_for_member
2775 (cp_parser
*, tree
);
2776 static tree cp_parser_late_parse_one_default_arg
2777 (cp_parser
*, tree
, tree
, tree
);
2778 static void cp_parser_late_parsing_nsdmi
2779 (cp_parser
*, tree
);
2780 static void cp_parser_late_parsing_default_args
2781 (cp_parser
*, tree
);
2782 static tree cp_parser_sizeof_operand
2783 (cp_parser
*, enum rid
);
2784 static cp_expr cp_parser_trait_expr
2785 (cp_parser
*, enum rid
);
2786 static bool cp_parser_declares_only_class_p
2788 static void cp_parser_set_storage_class
2789 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2790 static void cp_parser_set_decl_spec_type
2791 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2792 static void set_and_check_decl_spec_loc
2793 (cp_decl_specifier_seq
*decl_specs
,
2794 cp_decl_spec ds
, cp_token
*);
2795 static bool cp_parser_friend_p
2796 (const cp_decl_specifier_seq
*);
2797 static void cp_parser_required_error
2798 (cp_parser
*, required_token
, bool, location_t
);
2799 static cp_token
*cp_parser_require
2800 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2801 static cp_token
*cp_parser_require_keyword
2802 (cp_parser
*, enum rid
, required_token
);
2803 static bool cp_parser_token_starts_function_definition_p
2805 static bool cp_parser_next_token_starts_class_definition_p
2807 static bool cp_parser_next_token_ends_template_argument_p
2809 static bool cp_parser_nth_token_starts_template_argument_list_p
2810 (cp_parser
*, size_t);
2811 static enum tag_types cp_parser_token_is_class_key
2813 static enum tag_types cp_parser_token_is_type_parameter_key
2815 static void cp_parser_maybe_warn_enum_key (cp_parser
*, location_t
, tree
, rid
);
2816 static void cp_parser_check_class_key
2817 (cp_parser
*, location_t
, enum tag_types
, tree type
, bool, bool);
2818 static void cp_parser_check_access_in_redeclaration
2819 (tree type
, location_t location
);
2820 static bool cp_parser_optional_template_keyword
2822 static void cp_parser_pre_parsed_nested_name_specifier
2824 static bool cp_parser_cache_group
2825 (cp_parser
*, enum cpp_ttype
, unsigned);
2826 static tree cp_parser_cache_defarg
2827 (cp_parser
*parser
, bool nsdmi
);
2828 static void cp_parser_parse_tentatively
2830 static void cp_parser_commit_to_tentative_parse
2832 static void cp_parser_commit_to_topmost_tentative_parse
2834 static void cp_parser_abort_tentative_parse
2836 static bool cp_parser_parse_definitely
2838 static inline bool cp_parser_parsing_tentatively
2840 static bool cp_parser_uncommitted_to_tentative_parse_p
2842 static void cp_parser_error
2843 (cp_parser
*, const char *);
2844 static void cp_parser_name_lookup_error
2845 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2846 static bool cp_parser_simulate_error
2848 static bool cp_parser_check_type_definition
2850 static void cp_parser_check_for_definition_in_return_type
2851 (cp_declarator
*, tree
, location_t type_location
);
2852 static void cp_parser_check_for_invalid_template_id
2853 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2854 static bool cp_parser_non_integral_constant_expression
2855 (cp_parser
*, non_integral_constant
);
2856 static void cp_parser_diagnose_invalid_type_name
2857 (cp_parser
*, tree
, location_t
);
2858 static bool cp_parser_parse_and_diagnose_invalid_type_name
2860 static int cp_parser_skip_to_closing_parenthesis
2861 (cp_parser
*, bool, bool, bool);
2862 static void cp_parser_skip_to_end_of_statement
2864 static void cp_parser_consume_semicolon_at_end_of_statement
2866 static void cp_parser_skip_to_end_of_block_or_statement
2868 static bool cp_parser_skip_to_closing_brace
2870 static bool cp_parser_skip_entire_template_parameter_list
2872 static void cp_parser_require_end_of_template_parameter_list
2874 static bool cp_parser_skip_to_end_of_template_parameter_list
2876 static void cp_parser_skip_to_pragma_eol
2877 (cp_parser
*, cp_token
*);
2878 static bool cp_parser_error_occurred
2880 static bool cp_parser_allow_gnu_extensions_p
2882 static bool cp_parser_is_pure_string_literal
2884 static bool cp_parser_is_string_literal
2886 static bool cp_parser_is_keyword
2887 (cp_token
*, enum rid
);
2888 static tree cp_parser_make_typename_type
2889 (cp_parser
*, tree
, location_t location
);
2890 static cp_declarator
* cp_parser_make_indirect_declarator
2891 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2892 static bool cp_parser_compound_literal_p
2894 static bool cp_parser_array_designator_p
2896 static bool cp_parser_init_statement_p
2898 static bool cp_parser_skip_to_closing_square_bracket
2900 static size_t cp_parser_skip_balanced_tokens (cp_parser
*, size_t);
2902 // -------------------------------------------------------------------------- //
2903 // Unevaluated Operand Guard
2905 // Implementation of an RAII helper for unevaluated operand parsing.
2906 cp_unevaluated::cp_unevaluated ()
2908 ++cp_unevaluated_operand
;
2909 ++c_inhibit_evaluation_warnings
;
2912 cp_unevaluated::~cp_unevaluated ()
2914 --c_inhibit_evaluation_warnings
;
2915 --cp_unevaluated_operand
;
2918 // -------------------------------------------------------------------------- //
2919 // Tentative Parsing
2921 /* Returns nonzero if we are parsing tentatively. */
2924 cp_parser_parsing_tentatively (cp_parser
* parser
)
2926 return parser
->context
->next
!= NULL
;
2929 /* Returns nonzero if TOKEN is a string literal. */
2932 cp_parser_is_pure_string_literal (cp_token
* token
)
2934 return (token
->type
== CPP_STRING
||
2935 token
->type
== CPP_STRING16
||
2936 token
->type
== CPP_STRING32
||
2937 token
->type
== CPP_WSTRING
||
2938 token
->type
== CPP_UTF8STRING
);
2941 /* Returns nonzero if TOKEN is a string literal
2942 of a user-defined string literal. */
2945 cp_parser_is_string_literal (cp_token
* token
)
2947 return (cp_parser_is_pure_string_literal (token
) ||
2948 token
->type
== CPP_STRING_USERDEF
||
2949 token
->type
== CPP_STRING16_USERDEF
||
2950 token
->type
== CPP_STRING32_USERDEF
||
2951 token
->type
== CPP_WSTRING_USERDEF
||
2952 token
->type
== CPP_UTF8STRING_USERDEF
);
2955 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2958 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2960 return token
->keyword
== keyword
;
2963 /* Helper function for cp_parser_error.
2964 Having peeked a token of kind TOK1_KIND that might signify
2965 a conflict marker, peek successor tokens to determine
2966 if we actually do have a conflict marker.
2967 Specifically, we consider a run of 7 '<', '=' or '>' characters
2968 at the start of a line as a conflict marker.
2969 These come through the lexer as three pairs and a single,
2970 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2971 If it returns true, *OUT_LOC is written to with the location/range
2975 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2976 location_t
*out_loc
)
2978 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2979 if (token2
->type
!= tok1_kind
)
2981 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2982 if (token3
->type
!= tok1_kind
)
2984 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2985 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2988 /* It must be at the start of the line. */
2989 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2990 if (LOCATION_COLUMN (start_loc
) != 1)
2993 /* We have a conflict marker. Construct a location of the form:
2996 with start == caret, finishing at the end of the marker. */
2997 location_t finish_loc
= get_finish (token4
->location
);
2998 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
3003 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3007 get_matching_symbol (required_token token_desc
)
3014 case RT_CLOSE_BRACE
:
3016 case RT_CLOSE_PAREN
:
3021 /* Attempt to convert TOKEN_DESC from a required_token to an
3022 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3024 static enum cpp_ttype
3025 get_required_cpp_ttype (required_token token_desc
)
3030 return CPP_SEMICOLON
;
3032 return CPP_OPEN_PAREN
;
3033 case RT_CLOSE_BRACE
:
3034 return CPP_CLOSE_BRACE
;
3036 return CPP_OPEN_BRACE
;
3037 case RT_CLOSE_SQUARE
:
3038 return CPP_CLOSE_SQUARE
;
3039 case RT_OPEN_SQUARE
:
3040 return CPP_OPEN_SQUARE
;
3045 case RT_CLOSE_PAREN
:
3046 return CPP_CLOSE_PAREN
;
3049 /* Use CPP_EOF as a "no completions possible" code. */
3055 /* Subroutine of cp_parser_error and cp_parser_required_error.
3057 Issue a diagnostic of the form
3058 FILE:LINE: MESSAGE before TOKEN
3059 where TOKEN is the next token in the input stream. MESSAGE
3060 (specified by the caller) is usually of the form "expected
3063 This bypasses the check for tentative passing, and potentially
3064 adds material needed by cp_parser_required_error.
3066 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3067 suggesting insertion of the missing token.
3069 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3070 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3074 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
3075 required_token missing_token_desc
,
3076 location_t matching_location
)
3078 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3079 /* This diagnostic makes more sense if it is tagged to the line
3080 of the token we just peeked at. */
3081 cp_lexer_set_source_position_from_token (token
);
3083 if (token
->type
== CPP_PRAGMA
)
3085 error_at (token
->location
,
3086 "%<#pragma%> is not allowed here");
3087 cp_parser_skip_to_pragma_eol (parser
, token
);
3091 /* If this is actually a conflict marker, report it as such. */
3092 if (token
->type
== CPP_LSHIFT
3093 || token
->type
== CPP_RSHIFT
3094 || token
->type
== CPP_EQ_EQ
)
3097 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
3099 error_at (loc
, "version control conflict marker in file");
3100 expanded_location token_exploc
= expand_location (token
->location
);
3101 /* Consume tokens until the end of the source line. */
3104 cp_lexer_consume_token (parser
->lexer
);
3105 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
3106 if (next
->type
== CPP_EOF
)
3108 if (next
->location
== UNKNOWN_LOCATION
3109 || loc
== UNKNOWN_LOCATION
)
3112 expanded_location next_exploc
= expand_location (next
->location
);
3113 if (next_exploc
.file
!= token_exploc
.file
)
3115 if (next_exploc
.line
!= token_exploc
.line
)
3122 auto_diagnostic_group d
;
3123 gcc_rich_location
richloc (input_location
);
3125 bool added_matching_location
= false;
3127 if (missing_token_desc
!= RT_NONE
)
3128 if (cp_token
*prev_token
= cp_lexer_safe_previous_token (parser
->lexer
))
3130 /* Potentially supply a fix-it hint, suggesting to add the
3131 missing token immediately after the *previous* token.
3132 This may move the primary location within richloc. */
3133 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
3134 location_t prev_token_loc
= prev_token
->location
;
3135 maybe_suggest_missing_token_insertion (&richloc
, ttype
,
3138 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3139 Attempt to consolidate diagnostics by printing it as a
3140 secondary range within the main diagnostic. */
3141 if (matching_location
!= UNKNOWN_LOCATION
)
3142 added_matching_location
3143 = richloc
.add_location_if_nearby (matching_location
);
3146 /* If we were parsing a string-literal and there is an unknown name
3147 token right after, then check to see if that could also have been
3148 a literal string by checking the name against a list of known
3149 standard string literal constants defined in header files. If
3150 there is one, then add that as an hint to the error message. */
3152 if (token
->type
== CPP_NAME
)
3153 if (cp_token
*prev_token
= cp_lexer_safe_previous_token (parser
->lexer
))
3154 if (cp_parser_is_string_literal (prev_token
))
3156 tree name
= token
->u
.value
;
3157 const char *token_name
= IDENTIFIER_POINTER (name
);
3158 const char *header_hint
3159 = get_cp_stdlib_header_for_string_macro_name (token_name
);
3160 if (header_hint
!= NULL
)
3161 h
= name_hint (NULL
, new suggest_missing_header (token
->location
,
3166 /* Actually emit the error. */
3167 c_parse_error (gmsgid
,
3168 /* Because c_parser_error does not understand
3169 CPP_KEYWORD, keywords are treated like
3171 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
3172 token
->u
.value
, token
->flags
, &richloc
);
3174 if (missing_token_desc
!= RT_NONE
)
3176 /* If we weren't able to consolidate matching_location, then
3177 print it as a secondary diagnostic. */
3178 if (matching_location
!= UNKNOWN_LOCATION
3179 && !added_matching_location
)
3180 inform (matching_location
, "to match this %qs",
3181 get_matching_symbol (missing_token_desc
));
3185 /* If not parsing tentatively, issue a diagnostic of the form
3186 FILE:LINE: MESSAGE before TOKEN
3187 where TOKEN is the next token in the input stream. MESSAGE
3188 (specified by the caller) is usually of the form "expected
3192 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
3194 if (!cp_parser_simulate_error (parser
))
3195 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
3198 /* Issue an error about name-lookup failing. NAME is the
3199 IDENTIFIER_NODE DECL is the result of
3200 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3201 the thing that we hoped to find. */
3204 cp_parser_name_lookup_error (cp_parser
* parser
,
3207 name_lookup_error desired
,
3208 location_t location
)
3210 /* If name lookup completely failed, tell the user that NAME was not
3212 if (decl
== error_mark_node
)
3214 if (parser
->scope
&& parser
->scope
!= global_namespace
)
3215 error_at (location
, "%<%E::%E%> has not been declared",
3216 parser
->scope
, name
);
3217 else if (parser
->scope
== global_namespace
)
3218 error_at (location
, "%<::%E%> has not been declared", name
);
3219 else if (parser
->object_scope
3220 && !CLASS_TYPE_P (parser
->object_scope
))
3221 error_at (location
, "request for member %qE in non-class type %qT",
3222 name
, parser
->object_scope
);
3223 else if (parser
->object_scope
)
3224 error_at (location
, "%<%T::%E%> has not been declared",
3225 parser
->object_scope
, name
);
3227 error_at (location
, "%qE has not been declared", name
);
3229 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
3234 error_at (location
, "%<%E::%E%> is not a type",
3235 parser
->scope
, name
);
3238 error_at (location
, "%<%E::%E%> is not a class or namespace",
3239 parser
->scope
, name
);
3243 "%<%E::%E%> is not a class, namespace, or enumeration",
3244 parser
->scope
, name
);
3251 else if (parser
->scope
== global_namespace
)
3256 error_at (location
, "%<::%E%> is not a type", name
);
3259 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3263 "%<::%E%> is not a class, namespace, or enumeration",
3275 error_at (location
, "%qE is not a type", name
);
3278 error_at (location
, "%qE is not a class or namespace", name
);
3282 "%qE is not a class, namespace, or enumeration", name
);
3290 /* If we are parsing tentatively, remember that an error has occurred
3291 during this tentative parse. Returns true if the error was
3292 simulated; false if a message should be issued by the caller. */
3295 cp_parser_simulate_error (cp_parser
* parser
)
3297 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3299 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3305 /* This function is called when a type is defined. If type
3306 definitions are forbidden at this point, an error message is
3310 cp_parser_check_type_definition (cp_parser
* parser
)
3312 /* If types are forbidden here, issue a message. */
3313 if (parser
->type_definition_forbidden_message
)
3315 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3316 or %qs in the message need to be interpreted. */
3317 error (parser
->type_definition_forbidden_message
,
3318 parser
->type_definition_forbidden_message_arg
);
3324 /* This function is called when the DECLARATOR is processed. The TYPE
3325 was a type defined in the decl-specifiers. If it is invalid to
3326 define a type in the decl-specifiers for DECLARATOR, an error is
3327 issued. TYPE_LOCATION is the location of TYPE and is used
3328 for error reporting. */
3331 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3332 tree type
, location_t type_location
)
3334 /* [dcl.fct] forbids type definitions in return types.
3335 Unfortunately, it's not easy to know whether or not we are
3336 processing a return type until after the fact. */
3338 && (declarator
->kind
== cdk_pointer
3339 || declarator
->kind
== cdk_reference
3340 || declarator
->kind
== cdk_ptrmem
))
3341 declarator
= declarator
->declarator
;
3343 && declarator
->kind
== cdk_function
)
3345 error_at (type_location
,
3346 "new types may not be defined in a return type");
3347 inform (type_location
,
3348 "(perhaps a semicolon is missing after the definition of %qT)",
3353 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3354 "<" in any valid C++ program. If the next token is indeed "<",
3355 issue a message warning the user about what appears to be an
3356 invalid attempt to form a template-id. LOCATION is the location
3357 of the type-specifier (TYPE) */
3360 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3362 enum tag_types tag_type
,
3363 location_t location
)
3365 cp_token_position start
= 0;
3367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3369 if (TREE_CODE (type
) == TYPE_DECL
)
3370 type
= TREE_TYPE (type
);
3371 if (TYPE_P (type
) && !template_placeholder_p (type
))
3372 error_at (location
, "%qT is not a template", type
);
3373 else if (identifier_p (type
))
3375 if (tag_type
!= none_type
)
3376 error_at (location
, "%qE is not a class template", type
);
3378 error_at (location
, "%qE is not a template", type
);
3381 error_at (location
, "invalid template-id");
3382 /* Remember the location of the invalid "<". */
3383 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3384 start
= cp_lexer_token_position (parser
->lexer
, true);
3385 /* Consume the "<". */
3386 cp_lexer_consume_token (parser
->lexer
);
3387 /* Parse the template arguments. */
3388 cp_parser_enclosed_template_argument_list (parser
);
3389 /* Permanently remove the invalid template arguments so that
3390 this error message is not issued again. */
3392 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3396 /* If parsing an integral constant-expression, issue an error message
3397 about the fact that THING appeared and return true. Otherwise,
3398 return false. In either case, set
3399 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3402 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3403 non_integral_constant thing
)
3405 parser
->non_integral_constant_expression_p
= true;
3406 if (parser
->integral_constant_expression_p
)
3408 if (!parser
->allow_non_integral_constant_expression_p
)
3410 const char *msg
= NULL
;
3414 pedwarn (input_location
, OPT_Wpedantic
,
3415 "ISO C++ forbids using a floating-point literal "
3416 "in a constant-expression");
3419 error ("a cast to a type other than an integral or "
3420 "enumeration type cannot appear in a "
3421 "constant-expression");
3424 error ("%<typeid%> operator "
3425 "cannot appear in a constant-expression");
3428 error ("non-constant compound literals "
3429 "cannot appear in a constant-expression");
3432 error ("a function call "
3433 "cannot appear in a constant-expression");
3436 error ("an increment "
3437 "cannot appear in a constant-expression");
3440 error ("an decrement "
3441 "cannot appear in a constant-expression");
3444 error ("an array reference "
3445 "cannot appear in a constant-expression");
3447 case NIC_ADDR_LABEL
:
3448 error ("the address of a label "
3449 "cannot appear in a constant-expression");
3451 case NIC_OVERLOADED
:
3452 error ("calls to overloaded operators "
3453 "cannot appear in a constant-expression");
3455 case NIC_ASSIGNMENT
:
3456 error ("an assignment cannot appear in a constant-expression");
3459 error ("a comma operator "
3460 "cannot appear in a constant-expression");
3462 case NIC_CONSTRUCTOR
:
3463 error ("a call to a constructor "
3464 "cannot appear in a constant-expression");
3466 case NIC_TRANSACTION
:
3467 error ("a transaction expression "
3468 "cannot appear in a constant-expression");
3474 msg
= "__FUNCTION__";
3476 case NIC_PRETTY_FUNC
:
3477 msg
= "__PRETTY_FUNCTION__";
3497 case NIC_PREINCREMENT
:
3500 case NIC_PREDECREMENT
:
3513 error ("%qs cannot appear in a constant-expression", msg
);
3520 /* Emit a diagnostic for an invalid type name. This function commits
3521 to the current active tentative parse, if any. (Otherwise, the
3522 problematic construct might be encountered again later, resulting
3523 in duplicate error messages.) LOCATION is the location of ID. */
3526 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3527 location_t location
)
3529 tree decl
, ambiguous_decls
;
3530 cp_parser_commit_to_tentative_parse (parser
);
3531 /* Try to lookup the identifier. */
3532 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3533 /*is_template=*/false,
3534 /*is_namespace=*/false,
3535 /*check_dependency=*/true,
3536 &ambiguous_decls
, location
);
3537 if (ambiguous_decls
)
3538 /* If the lookup was ambiguous, an error will already have
3541 /* If the lookup found a template-name, it means that the user forgot
3542 to specify an argument list. Emit a useful error message. */
3543 if (DECL_TYPE_TEMPLATE_P (decl
))
3545 auto_diagnostic_group d
;
3547 "invalid use of template-name %qE without an argument list",
3549 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3550 inform (location
, "class template argument deduction is only available "
3551 "with %<-std=c++17%> or %<-std=gnu++17%>");
3552 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3554 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3555 error_at (location
, "invalid use of destructor %qD as a type", id
);
3556 else if (TREE_CODE (decl
) == TYPE_DECL
)
3557 /* Something like 'unsigned A a;' */
3558 error_at (location
, "invalid combination of multiple type-specifiers");
3559 else if (!parser
->scope
)
3561 /* Issue an error message. */
3562 auto_diagnostic_group d
;
3564 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3565 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3566 if (const char *suggestion
= hint
.suggestion ())
3568 gcc_rich_location
richloc (location
);
3569 richloc
.add_fixit_replace (suggestion
);
3571 "%qE does not name a type; did you mean %qs?",
3575 error_at (location
, "%qE does not name a type", id
);
3576 /* If we're in a template class, it's possible that the user was
3577 referring to a type from a base class. For example:
3579 template <typename T> struct A { typedef T X; };
3580 template <typename T> struct B : public A<T> { X x; };
3582 The user should have said "typename A<T>::X". */
3583 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3584 inform (location
, "C++11 %<constexpr%> only available with "
3585 "%<-std=c++11%> or %<-std=gnu++11%>");
3586 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3587 inform (location
, "C++11 %<noexcept%> only available with "
3588 "%<-std=c++11%> or %<-std=gnu++11%>");
3589 else if (TREE_CODE (id
) == IDENTIFIER_NODE
3590 && (id_equal (id
, "module") || id_equal (id
, "import")))
3593 inform (location
, "%qE is not recognized as a module control-line",
3595 else if (cxx_dialect
< cxx20
)
3596 inform (location
, "C++20 %qE only available with %<-fmodules-ts%>",
3599 inform (location
, "C++20 %qE only available with %<-fmodules-ts%>"
3600 ", which is not yet enabled with %<-std=c++20%>", id
);
3602 else if (cxx_dialect
< cxx11
3603 && TREE_CODE (id
) == IDENTIFIER_NODE
3604 && id_equal (id
, "thread_local"))
3605 inform (location
, "C++11 %<thread_local%> only available with "
3606 "%<-std=c++11%> or %<-std=gnu++11%>");
3607 else if (cxx_dialect
< cxx20
&& id
== ridpointers
[(int)RID_CONSTINIT
])
3608 inform (location
, "C++20 %<constinit%> only available with "
3609 "%<-std=c++20%> or %<-std=gnu++20%>");
3610 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3611 inform (location
, "%<concept%> only available with %<-std=c++20%> or "
3613 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_REQUIRES
])
3614 inform (location
, "%<requires%> only available with %<-std=c++20%> or "
3616 else if (processing_template_decl
&& current_class_type
3617 && TYPE_BINFO (current_class_type
))
3619 for (tree b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3620 b
; b
= TREE_CHAIN (b
))
3622 tree base_type
= BINFO_TYPE (b
);
3623 if (CLASS_TYPE_P (base_type
)
3624 && dependent_type_p (base_type
))
3626 /* Go from a particular instantiation of the
3627 template (which will have an empty TYPE_FIELDs),
3628 to the main version. */
3629 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3630 for (tree field
= TYPE_FIELDS (base_type
);
3631 field
; field
= DECL_CHAIN (field
))
3632 if (TREE_CODE (field
) == TYPE_DECL
3633 && DECL_NAME (field
) == id
)
3636 "(perhaps %<typename %T::%E%> was intended)",
3637 BINFO_TYPE (b
), id
);
3645 /* Here we diagnose qualified-ids where the scope is actually correct,
3646 but the identifier does not resolve to a valid type name. */
3647 else if (parser
->scope
!= error_mark_node
)
3649 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3651 auto_diagnostic_group d
;
3653 if (decl
== error_mark_node
)
3654 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3656 const char *suggestion
= hint
.suggestion ();
3657 gcc_rich_location
richloc (location_of (id
));
3659 richloc
.add_fixit_replace (suggestion
);
3660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3664 "%qE in namespace %qE does not name a template"
3665 " type; did you mean %qs?",
3666 id
, parser
->scope
, suggestion
);
3669 "%qE in namespace %qE does not name a template type",
3672 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3676 "%qE in namespace %qE does not name a template"
3677 " type; did you mean %qs?",
3678 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3681 "%qE in namespace %qE does not name a template"
3683 TREE_OPERAND (id
, 0), parser
->scope
);
3689 "%qE in namespace %qE does not name a type"
3690 "; did you mean %qs?",
3691 id
, parser
->scope
, suggestion
);
3694 "%qE in namespace %qE does not name a type",
3698 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3700 else if (CLASS_TYPE_P (parser
->scope
)
3701 && constructor_name_p (id
, parser
->scope
))
3704 auto_diagnostic_group d
;
3705 error_at (location
, "%<%T::%E%> names the constructor, not"
3706 " the type", parser
->scope
, id
);
3707 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3708 error_at (location
, "and %qT has no template constructors",
3711 else if (TYPE_P (parser
->scope
)
3712 && dependent_scope_p (parser
->scope
))
3714 gcc_rich_location
richloc (location
);
3715 richloc
.add_fixit_insert_before ("typename ");
3716 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3718 "need %<typename%> before %<%T::%D::%E%> because "
3719 "%<%T::%D%> is a dependent scope",
3720 TYPE_CONTEXT (parser
->scope
),
3721 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3723 TYPE_CONTEXT (parser
->scope
),
3724 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3726 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3727 "%qT is a dependent scope",
3728 parser
->scope
, id
, parser
->scope
);
3730 else if (TYPE_P (parser
->scope
))
3732 auto_diagnostic_group d
;
3733 if (!COMPLETE_TYPE_P (parser
->scope
))
3734 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3736 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3737 error_at (location_of (id
),
3738 "%qE in %q#T does not name a template type",
3740 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3741 error_at (location_of (id
),
3742 "%qE in %q#T does not name a template type",
3743 TREE_OPERAND (id
, 0), parser
->scope
);
3745 error_at (location_of (id
),
3746 "%qE in %q#T does not name a type",
3749 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3756 /* Check for a common situation where a type-name should be present,
3757 but is not, and issue a sensible error message. Returns true if an
3758 invalid type-name was detected.
3760 The situation handled by this function are variable declarations of the
3761 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3762 Usually, `ID' should name a type, but if we got here it means that it
3763 does not. We try to emit the best possible error message depending on
3764 how exactly the id-expression looks like. */
3767 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3770 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3772 /* Avoid duplicate error about ambiguous lookup. */
3773 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3775 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3776 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3780 cp_parser_parse_tentatively (parser
);
3781 id
= cp_parser_id_expression (parser
,
3782 /*template_keyword_p=*/false,
3783 /*check_dependency_p=*/true,
3784 /*template_p=*/NULL
,
3785 /*declarator_p=*/true,
3786 /*optional_p=*/false);
3787 /* If the next token is a (, this is a function with no explicit return
3788 type, i.e. constructor, destructor or conversion op. */
3789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3790 || TREE_CODE (id
) == TYPE_DECL
)
3792 cp_parser_abort_tentative_parse (parser
);
3795 if (!cp_parser_parse_definitely (parser
))
3798 /* Emit a diagnostic for the invalid type. */
3799 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3801 /* If we aren't in the middle of a declarator (i.e. in a
3802 parameter-declaration-clause), skip to the end of the declaration;
3803 there's no point in trying to process it. */
3804 if (!parser
->in_declarator_p
)
3805 cp_parser_skip_to_end_of_block_or_statement (parser
);
3809 /* Consume tokens up to, and including, the next non-nested closing `)'.
3810 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3811 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3812 found an unnested token of that type. */
3815 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3820 unsigned paren_depth
= 0;
3821 unsigned brace_depth
= 0;
3822 unsigned square_depth
= 0;
3823 unsigned condop_depth
= 0;
3825 if (recovering
&& or_ttype
== CPP_EOF
3826 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3831 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3833 /* Have we found what we're looking for before the closing paren? */
3834 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3835 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3838 switch (token
->type
)
3840 case CPP_PRAGMA_EOL
:
3841 if (!parser
->lexer
->in_pragma
)
3845 /* If we've run out of tokens, then there is no closing `)'. */
3848 /* This is good for lambda expression capture-lists. */
3849 case CPP_OPEN_SQUARE
:
3852 case CPP_CLOSE_SQUARE
:
3853 if (!square_depth
--)
3858 /* This matches the processing in skip_to_end_of_statement. */
3863 case CPP_OPEN_BRACE
:
3866 case CPP_CLOSE_BRACE
:
3871 case CPP_OPEN_PAREN
:
3876 case CPP_CLOSE_PAREN
:
3877 if (!brace_depth
&& !paren_depth
--)
3880 cp_lexer_consume_token (parser
->lexer
);
3886 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3891 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3896 if (!cp_token_is_module_directive (token
))
3901 /* We fell into a pragma. Skip it, and continue. */
3902 cp_parser_skip_to_pragma_eol (parser
, recovering
? token
: nullptr);
3909 /* Consume the token. */
3910 cp_lexer_consume_token (parser
->lexer
);
3914 /* Consume tokens up to, and including, the next non-nested closing `)'.
3915 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3916 are doing error recovery. Returns -1 if OR_COMMA is true and we
3917 found an unnested token of that type. */
3920 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3925 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3926 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3927 ttype
, consume_paren
);
3930 /* Consume tokens until we reach the end of the current statement.
3931 Normally, that will be just before consuming a `;'. However, if a
3932 non-nested `}' comes first, then we stop before consuming that. */
3935 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3937 unsigned nesting_depth
= 0;
3939 /* Unwind generic function template scope if necessary. */
3940 if (parser
->fully_implicit_function_template_p
)
3941 abort_fully_implicit_template (parser
);
3945 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3947 switch (token
->type
)
3949 case CPP_PRAGMA_EOL
:
3950 if (!parser
->lexer
->in_pragma
)
3954 /* If we've run out of tokens, stop. */
3958 /* If the next token is a `;', we have reached the end of the
3964 case CPP_CLOSE_BRACE
:
3965 /* If this is a non-nested '}', stop before consuming it.
3966 That way, when confronted with something like:
3970 we stop before consuming the closing '}', even though we
3971 have not yet reached a `;'. */
3972 if (nesting_depth
== 0)
3975 /* If it is the closing '}' for a block that we have
3976 scanned, stop -- but only after consuming the token.
3982 we will stop after the body of the erroneously declared
3983 function, but before consuming the following `typedef'
3985 if (--nesting_depth
== 0)
3987 cp_lexer_consume_token (parser
->lexer
);
3992 case CPP_OPEN_BRACE
:
3997 if (!cp_token_is_module_directive (token
))
4002 /* We fell into a pragma. Skip it, and continue or return. */
4003 cp_parser_skip_to_pragma_eol (parser
, token
);
4012 /* Consume the token. */
4013 cp_lexer_consume_token (parser
->lexer
);
4017 /* This function is called at the end of a statement or declaration.
4018 If the next token is a semicolon, it is consumed; otherwise, error
4019 recovery is attempted. */
4022 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
4024 /* Look for the trailing `;'. */
4025 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
4027 /* If there is additional (erroneous) input, skip to the end of
4029 cp_parser_skip_to_end_of_statement (parser
);
4030 /* If the next token is now a `;', consume it. */
4031 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4032 cp_lexer_consume_token (parser
->lexer
);
4036 /* Skip tokens until we have consumed an entire block, or until we
4037 have consumed a non-nested `;'. */
4040 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
4042 int nesting_depth
= 0;
4044 /* Unwind generic function template scope if necessary. */
4045 if (parser
->fully_implicit_function_template_p
)
4046 abort_fully_implicit_template (parser
);
4048 while (nesting_depth
>= 0)
4050 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4052 switch (token
->type
)
4054 case CPP_PRAGMA_EOL
:
4055 if (!parser
->lexer
->in_pragma
)
4059 /* If we've run out of tokens, stop. */
4063 /* Stop if this is an unnested ';'. */
4068 case CPP_CLOSE_BRACE
:
4069 /* Stop if this is an unnested '}', or closes the outermost
4072 if (nesting_depth
< 0)
4078 case CPP_OPEN_BRACE
:
4084 if (!cp_token_is_module_directive (token
))
4089 /* Skip it, and continue or return. */
4090 cp_parser_skip_to_pragma_eol (parser
, token
);
4099 /* Consume the token. */
4100 cp_lexer_consume_token (parser
->lexer
);
4104 /* Skip tokens until a non-nested closing curly brace is the next
4105 token, or there are no more tokens. Return true in the first case,
4109 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
4111 unsigned nesting_depth
= 0;
4115 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4117 switch (token
->type
)
4119 case CPP_PRAGMA_EOL
:
4120 if (!parser
->lexer
->in_pragma
)
4124 /* If we've run out of tokens, stop. */
4127 case CPP_CLOSE_BRACE
:
4128 /* If the next token is a non-nested `}', then we have reached
4129 the end of the current block. */
4130 if (nesting_depth
-- == 0)
4134 case CPP_OPEN_BRACE
:
4135 /* If it the next token is a `{', then we are entering a new
4136 block. Consume the entire block. */
4144 /* Consume the token. */
4145 cp_lexer_consume_token (parser
->lexer
);
4149 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4150 parameter is the PRAGMA token, allowing us to purge the entire pragma
4151 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4152 forwards (not error recovery). */
4155 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
4161 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4162 before an EOF token, even when the EOF is on the pragma line.
4163 We should never get here without being inside a deferred
4165 gcc_checking_assert (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
));
4166 token
= cp_lexer_consume_token (parser
->lexer
);
4168 while (token
->type
!= CPP_PRAGMA_EOL
);
4172 parser
->lexer
->in_pragma
= false;
4173 if (parser
->lexer
->in_omp_attribute_pragma
4174 && cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4176 parser
->lexer
= parser
->lexer
->next
;
4177 /* Put the current source position back where it was before this
4178 lexer was pushed. */
4179 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4184 /* Require pragma end of line, resyncing with it as necessary. The
4185 arguments are as for cp_parser_skip_to_pragma_eol. */
4188 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
4190 parser
->lexer
->in_pragma
= false;
4191 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
4192 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
4193 else if (parser
->lexer
->in_omp_attribute_pragma
4194 && cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4196 parser
->lexer
= parser
->lexer
->next
;
4197 /* Put the current source position back where it was before this
4198 lexer was pushed. */
4199 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4203 /* This is a simple wrapper around make_typename_type. When the id is
4204 an unresolved identifier node, we can provide a superior diagnostic
4205 using cp_parser_diagnose_invalid_type_name. */
4208 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
4209 location_t id_location
)
4212 if (identifier_p (id
))
4214 result
= make_typename_type (parser
->scope
, id
, typename_type
,
4215 /*complain=*/tf_none
);
4216 if (result
== error_mark_node
)
4217 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
4220 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
4223 /* This is a wrapper around the
4224 make_{pointer,ptrmem,reference}_declarator functions that decides
4225 which one to call based on the CODE and CLASS_TYPE arguments. The
4226 CODE argument should be one of the values returned by
4227 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4228 appertain to the pointer or reference. */
4230 static cp_declarator
*
4231 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
4232 cp_cv_quals cv_qualifiers
,
4233 cp_declarator
*target
,
4236 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
4237 return cp_error_declarator
;
4239 if (code
== INDIRECT_REF
)
4240 if (class_type
== NULL_TREE
)
4241 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
4243 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
4244 target
, attributes
);
4245 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
4246 return make_reference_declarator (cv_qualifiers
, target
,
4248 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
4249 return make_reference_declarator (cv_qualifiers
, target
,
4254 /* Create a new C++ parser. */
4257 cp_parser_new (cp_lexer
*lexer
)
4259 /* Initialize the binops_by_token so that we can get the tree
4260 directly from the token. */
4261 for (unsigned i
= 0; i
< ARRAY_SIZE (binops
); i
++)
4262 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
4264 cp_parser
*parser
= ggc_cleared_alloc
<cp_parser
> ();
4265 parser
->lexer
= lexer
;
4266 parser
->context
= cp_parser_context_new (NULL
);
4268 /* For now, we always accept GNU extensions. */
4269 parser
->allow_gnu_extensions_p
= 1;
4271 /* The `>' token is a greater-than operator, not the end of a
4273 parser
->greater_than_is_operator_p
= true;
4275 parser
->default_arg_ok_p
= true;
4277 /* We are not parsing a constant-expression. */
4278 parser
->integral_constant_expression_p
= false;
4279 parser
->allow_non_integral_constant_expression_p
= false;
4280 parser
->non_integral_constant_expression_p
= false;
4282 /* Local variable names are not forbidden. */
4283 parser
->local_variables_forbidden_p
= 0;
4285 /* We are not processing an `extern "C"' declaration. */
4286 parser
->in_unbraced_linkage_specification_p
= false;
4288 /* We are not processing a declarator. */
4289 parser
->in_declarator_p
= false;
4291 /* We are not processing a template-argument-list. */
4292 parser
->in_template_argument_list_p
= false;
4294 /* We are not in an iteration statement. */
4295 parser
->in_statement
= 0;
4297 /* We are not in a switch statement. */
4298 parser
->in_switch_statement_p
= false;
4300 /* We are not parsing a type-id inside an expression. */
4301 parser
->in_type_id_in_expr_p
= false;
4303 /* String literals should be translated to the execution character set. */
4304 parser
->translate_strings_p
= true;
4306 /* We are not parsing a function body. */
4307 parser
->in_function_body
= false;
4309 /* We can correct until told otherwise. */
4310 parser
->colon_corrects_to_scope_p
= true;
4312 /* The unparsed function queue is empty. */
4313 push_unparsed_function_queues (parser
);
4315 /* There are no classes being defined. */
4316 parser
->num_classes_being_defined
= 0;
4318 /* No template parameters apply. */
4319 parser
->num_template_parameter_lists
= 0;
4321 /* Special parsing data structures. */
4322 parser
->omp_declare_simd
= NULL
;
4323 parser
->oacc_routine
= NULL
;
4325 /* Not declaring an implicit function template. */
4326 parser
->auto_is_implicit_function_template_parm_p
= false;
4327 parser
->fully_implicit_function_template_p
= false;
4328 parser
->implicit_template_parms
= 0;
4329 parser
->implicit_template_scope
= 0;
4331 /* Allow constrained-type-specifiers. */
4332 parser
->prevent_constrained_type_specifiers
= 0;
4334 /* We haven't yet seen an 'extern "C"'. */
4335 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4340 /* Create a cp_lexer structure which will emit the tokens in CACHE
4341 and push it onto the parser's lexer stack. This is used for delayed
4342 parsing of in-class method bodies and default arguments, and should
4343 not be confused with tentative parsing. */
4345 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4347 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4348 lexer
->next
= parser
->lexer
;
4349 parser
->lexer
= lexer
;
4351 /* Move the current source position to that of the first token in the
4353 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4356 /* Pop the top lexer off the parser stack. This is never used for the
4357 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4359 cp_parser_pop_lexer (cp_parser
*parser
)
4361 cp_lexer
*lexer
= parser
->lexer
;
4362 parser
->lexer
= lexer
->next
;
4363 cp_lexer_destroy (lexer
);
4365 /* Put the current source position back where it was before this
4366 lexer was pushed. */
4367 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4370 /* Lexical conventions [gram.lex] */
4372 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4376 cp_parser_identifier (cp_parser
* parser
)
4380 /* Look for the identifier. */
4381 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4382 /* Return the value. */
4384 return cp_expr (token
->u
.value
, token
->location
);
4386 return error_mark_node
;
4389 /* Parse a sequence of adjacent string constants. Returns a
4390 TREE_STRING representing the combined, nul-terminated string
4391 constant. If TRANSLATE is true, translate the string to the
4392 execution character set. If WIDE_OK is true, a wide string is
4395 C++98 [lex.string] says that if a narrow string literal token is
4396 adjacent to a wide string literal token, the behavior is undefined.
4397 However, C99 6.4.5p4 says that this results in a wide string literal.
4398 We follow C99 here, for consistency with the C front end.
4400 This code is largely lifted from lex_string() in c-lex.cc.
4402 FUTURE: ObjC++ will need to handle @-strings here. */
4404 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4405 bool lookup_udlit
= true)
4409 struct obstack str_ob
;
4410 struct obstack loc_ob
;
4411 cpp_string str
, istr
, *strs
;
4413 enum cpp_ttype type
, curr_type
;
4414 int have_suffix_p
= 0;
4416 tree suffix_id
= NULL_TREE
;
4417 bool curr_tok_is_userdef_p
= false;
4419 tok
= cp_lexer_peek_token (parser
->lexer
);
4420 if (!cp_parser_is_string_literal (tok
))
4422 cp_parser_error (parser
, "expected string-literal");
4423 return error_mark_node
;
4426 location_t loc
= tok
->location
;
4428 if (cpp_userdef_string_p (tok
->type
))
4430 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4431 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4432 curr_tok_is_userdef_p
= true;
4436 string_tree
= tok
->u
.value
;
4437 curr_type
= tok
->type
;
4441 /* Try to avoid the overhead of creating and destroying an obstack
4442 for the common case of just one string. */
4443 if (!cp_parser_is_string_literal
4444 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4446 cp_lexer_consume_token (parser
->lexer
);
4448 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4449 str
.len
= TREE_STRING_LENGTH (string_tree
);
4452 if (curr_tok_is_userdef_p
)
4454 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4456 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4459 curr_type
= tok
->type
;
4465 location_t last_tok_loc
= tok
->location
;
4466 gcc_obstack_init (&str_ob
);
4467 gcc_obstack_init (&loc_ob
);
4472 cp_lexer_consume_token (parser
->lexer
);
4474 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4475 str
.len
= TREE_STRING_LENGTH (string_tree
);
4477 if (curr_tok_is_userdef_p
)
4479 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4480 if (have_suffix_p
== 0)
4482 suffix_id
= curr_suffix_id
;
4485 else if (have_suffix_p
== 1
4486 && curr_suffix_id
!= suffix_id
)
4488 error ("inconsistent user-defined literal suffixes"
4489 " %qD and %qD in string literal",
4490 suffix_id
, curr_suffix_id
);
4493 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4496 curr_type
= tok
->type
;
4498 if (type
!= curr_type
)
4500 if (type
== CPP_STRING
)
4502 else if (curr_type
!= CPP_STRING
)
4504 rich_location
rich_loc (line_table
, tok
->location
);
4505 rich_loc
.add_range (last_tok_loc
);
4506 error_at (&rich_loc
,
4507 "concatenation of string literals with "
4508 "conflicting encoding prefixes");
4512 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4513 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4515 last_tok_loc
= tok
->location
;
4517 tok
= cp_lexer_peek_token (parser
->lexer
);
4518 if (cpp_userdef_string_p (tok
->type
))
4520 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4521 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4522 curr_tok_is_userdef_p
= true;
4526 string_tree
= tok
->u
.value
;
4527 curr_type
= tok
->type
;
4528 curr_tok_is_userdef_p
= false;
4531 while (cp_parser_is_string_literal (tok
));
4533 /* A string literal built by concatenation has its caret=start at
4534 the start of the initial string, and its finish at the finish of
4535 the final string literal. */
4536 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4538 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4541 if (type
!= CPP_STRING
&& !wide_ok
)
4543 cp_parser_error (parser
, "a wide string is invalid in this context");
4547 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4548 (parse_in
, strs
, count
, &istr
, type
))
4550 value
= build_string (istr
.len
, (const char *)istr
.text
);
4551 free (CONST_CAST (unsigned char *, istr
.text
));
4554 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4555 gcc_assert (g_string_concat_db
);
4556 g_string_concat_db
->record_string_concatenation (count
, locs
);
4563 TREE_TYPE (value
) = char_array_type_node
;
4565 case CPP_UTF8STRING
:
4567 TREE_TYPE (value
) = char8_array_type_node
;
4569 TREE_TYPE (value
) = char_array_type_node
;
4572 TREE_TYPE (value
) = char16_array_type_node
;
4575 TREE_TYPE (value
) = char32_array_type_node
;
4578 TREE_TYPE (value
) = wchar_array_type_node
;
4582 value
= fix_string_type (value
);
4586 tree literal
= build_userdef_literal (suffix_id
, value
,
4587 OT_NONE
, NULL_TREE
);
4589 value
= cp_parser_userdef_string_literal (literal
);
4595 /* cpp_interpret_string has issued an error. */
4596 value
= error_mark_node
;
4600 obstack_free (&str_ob
, 0);
4601 obstack_free (&loc_ob
, 0);
4604 return cp_expr (value
, loc
);
4607 /* Look up a literal operator with the name and the exact arguments. */
4610 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4612 tree decl
= lookup_name (name
);
4613 if (!decl
|| !is_overloaded_fn (decl
))
4614 return error_mark_node
;
4616 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4620 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4626 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4627 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4629 tree tparm
= TREE_VALUE (parmtypes
);
4630 tree targ
= TREE_TYPE ((*args
)[ix
]);
4631 bool ptr
= TYPE_PTR_P (tparm
);
4632 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4633 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4635 || !same_type_p (TREE_TYPE (tparm
),
4641 && ix
== vec_safe_length (args
)
4642 /* May be this should be sufficient_parms_p instead,
4643 depending on how exactly should user-defined literals
4644 work in presence of default arguments on the literal
4645 operator parameters. */
4646 && parmtypes
== void_list_node
)
4651 return error_mark_node
;
4654 /* Parse a user-defined char constant. Returns a call to a user-defined
4655 literal operator taking the character as an argument. */
4658 cp_parser_userdef_char_literal (cp_parser
*parser
)
4660 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4661 tree literal
= token
->u
.value
;
4662 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4663 tree value
= USERDEF_LITERAL_VALUE (literal
);
4664 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4667 /* Build up a call to the user-defined operator */
4668 /* Lookup the name we got back from the id-expression. */
4670 vec_safe_push (args
, value
);
4671 decl
= lookup_literal_operator (name
, args
);
4672 if (!decl
|| decl
== error_mark_node
)
4674 error ("unable to find character literal operator %qD with %qT argument",
4675 name
, TREE_TYPE (value
));
4676 return error_mark_node
;
4678 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4682 /* A subroutine of cp_parser_userdef_numeric_literal to
4683 create a char... template parameter pack from a string node. */
4686 make_char_string_pack (tree value
)
4689 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4690 const unsigned char *str
4691 = (const unsigned char *) TREE_STRING_POINTER (value
);
4692 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4693 tree argvec
= make_tree_vec (1);
4695 /* Fill in CHARVEC with all of the parameters. */
4696 charvec
= make_tree_vec (len
);
4697 for (i
= 0; i
< len
; ++i
)
4699 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4700 cpp_string in
= { 3, s
};
4701 cpp_string out
= { 0, 0 };
4702 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4704 gcc_assert (out
.len
== 2);
4705 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4709 /* Build the argument packs. */
4710 ARGUMENT_PACK_ARGS (argpack
) = charvec
;
4712 TREE_VEC_ELT (argvec
, 0) = argpack
;
4717 /* A subroutine of cp_parser_userdef_numeric_literal to
4718 create a char... template parameter pack from a string node. */
4721 make_string_pack (tree value
)
4724 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4725 const unsigned char *str
4726 = (const unsigned char *) TREE_STRING_POINTER (value
);
4727 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4728 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4729 tree argvec
= make_tree_vec (2);
4731 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4732 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4734 /* First template parm is character type. */
4735 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4737 /* Fill in CHARVEC with all of the parameters. */
4738 charvec
= make_tree_vec (len
);
4739 for (int i
= 0; i
< len
; ++i
)
4740 TREE_VEC_ELT (charvec
, i
)
4741 = double_int_to_tree (str_char_type_node
,
4742 double_int::from_buffer (str
+ i
* sz
, sz
));
4744 /* Build the argument packs. */
4745 ARGUMENT_PACK_ARGS (argpack
) = charvec
;
4747 TREE_VEC_ELT (argvec
, 1) = argpack
;
4752 /* Parse a user-defined numeric constant. returns a call to a user-defined
4753 literal operator. */
4756 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4758 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4759 tree literal
= token
->u
.value
;
4760 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4761 tree value
= USERDEF_LITERAL_VALUE (literal
);
4762 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4763 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4764 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4767 /* Look for a literal operator taking the exact type of numeric argument
4768 as the literal value. */
4770 vec_safe_push (args
, value
);
4771 decl
= lookup_literal_operator (name
, args
);
4772 if (decl
&& decl
!= error_mark_node
)
4774 result
= finish_call_expr (decl
, &args
, false, true,
4775 tf_warning_or_error
);
4777 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4779 warning_at (token
->location
, OPT_Woverflow
,
4780 "integer literal exceeds range of %qT type",
4781 long_long_unsigned_type_node
);
4786 warning_at (token
->location
, OPT_Woverflow
,
4787 "floating literal exceeds range of %qT type",
4788 long_double_type_node
);
4789 else if (overflow
< 0)
4790 warning_at (token
->location
, OPT_Woverflow
,
4791 "floating literal truncated to zero");
4797 /* If the numeric argument didn't work, look for a raw literal
4798 operator taking a const char* argument consisting of the number
4799 in string format. */
4801 vec_safe_push (args
, num_string
);
4802 decl
= lookup_literal_operator (name
, args
);
4803 if (decl
&& decl
!= error_mark_node
)
4805 result
= finish_call_expr (decl
, &args
, false, true,
4806 tf_warning_or_error
);
4810 /* If the raw literal didn't work, look for a non-type template
4811 function with parameter pack char.... Call the function with
4812 template parameter characters representing the number. */
4814 decl
= lookup_literal_operator (name
, args
);
4815 if (decl
&& decl
!= error_mark_node
)
4817 tree tmpl_args
= make_char_string_pack (num_string
);
4818 if (tmpl_args
== NULL_TREE
)
4820 error ("failed to translate literal to execution character set %qT",
4822 return error_mark_node
;
4824 decl
= lookup_template_function (decl
, tmpl_args
);
4825 result
= finish_call_expr (decl
, &args
, false, true,
4826 tf_warning_or_error
);
4830 /* In C++14 the standard library defines complex number suffixes that
4831 conflict with GNU extensions. Prefer them if <complex> is #included. */
4832 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4833 bool i14
= (cxx_dialect
> cxx11
4834 && (id_equal (suffix_id
, "i")
4835 || id_equal (suffix_id
, "if")
4836 || id_equal (suffix_id
, "il")));
4837 diagnostic_t kind
= DK_ERROR
;
4842 tree cxlit
= lookup_qualified_name (std_node
, "complex_literals",
4843 LOOK_want::NORMAL
, false);
4844 if (cxlit
== error_mark_node
)
4846 /* No <complex>, so pedwarn and use GNU semantics. */
4848 opt
= OPT_Wpedantic
;
4853 = emit_diagnostic (kind
, input_location
, opt
,
4854 "unable to find numeric literal operator %qD", name
);
4857 /* Don't inform either. */;
4860 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4861 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4864 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4865 "GNU built-in suffix");
4868 inform (token
->location
, "use %<-fext-numeric-literals%> "
4869 "to enable more built-in suffixes");
4871 if (kind
== DK_ERROR
)
4872 value
= error_mark_node
;
4875 /* Use the built-in semantics. */
4877 if (id_equal (suffix_id
, "i"))
4879 if (TREE_CODE (value
) == INTEGER_CST
)
4880 type
= integer_type_node
;
4882 type
= double_type_node
;
4884 else if (id_equal (suffix_id
, "if"))
4885 type
= float_type_node
;
4886 else /* if (id_equal (suffix_id, "il")) */
4887 type
= long_double_type_node
;
4889 value
= fold_build2 (COMPLEX_EXPR
, build_complex_type (type
),
4890 build_zero_cst (type
), fold_convert (type
, value
));
4893 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4894 /* Avoid repeated diagnostics. */
4895 token
->u
.value
= value
;
4899 /* Parse a user-defined string constant. Returns a call to a user-defined
4900 literal operator taking a character pointer and the length of the string
4904 cp_parser_userdef_string_literal (tree literal
)
4906 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4907 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4908 tree value
= USERDEF_LITERAL_VALUE (literal
);
4909 int len
= TREE_STRING_LENGTH (value
)
4910 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4913 /* Build up a call to the user-defined operator. */
4914 /* Lookup the name we got back from the id-expression. */
4916 vec_safe_push (args
, value
);
4917 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4918 decl
= lookup_literal_operator (name
, args
);
4920 if (decl
&& decl
!= error_mark_node
)
4921 return finish_call_expr (decl
, &args
, false, true,
4922 tf_warning_or_error
);
4924 /* Look for a suitable template function, either (C++20) with a single
4925 parameter of class type, or (N3599) with typename parameter CharT and
4926 parameter pack CharT... */
4928 decl
= lookup_literal_operator (name
, args
);
4929 if (decl
&& decl
!= error_mark_node
)
4931 /* Use resolve_nondeduced_context to try to choose one form of template
4933 tree tmpl_args
= make_tree_vec (1);
4934 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4935 decl
= lookup_template_function (decl
, tmpl_args
);
4936 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4941 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4942 res
= resolve_nondeduced_context (decl
, tf_none
);
4946 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4947 TREE_OPERAND (decl
, 1) = tmpl_args
;
4948 return finish_call_expr (decl
, &args
, false, true,
4949 tf_warning_or_error
);
4952 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4953 name
, TREE_TYPE (value
), size_type_node
);
4954 return error_mark_node
;
4958 /* Basic concepts [gram.basic] */
4960 /* Parse a translation-unit.
4963 declaration-seq [opt] */
4966 cp_parser_translation_unit (cp_parser
* parser
)
4968 gcc_checking_assert (!cp_error_declarator
);
4970 /* Create the declarator obstack. */
4971 gcc_obstack_init (&declarator_obstack
);
4972 /* Create the error declarator. */
4973 cp_error_declarator
= make_declarator (cdk_error
);
4974 /* Create the empty parameter list. */
4975 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4977 /* Remember where the base of the declarator obstack lies. */
4978 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4980 push_deferring_access_checks (flag_access_control
4981 ? dk_no_deferred
: dk_no_check
);
4983 module_parse mp_state
= MP_NOT_MODULE
;
4984 if (modules_p () && !header_module_p ())
4985 mp_state
= MP_FIRST
;
4987 bool implicit_extern_c
= false;
4989 /* Parse until EOF. */
4992 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4994 /* If we're entering or exiting a region that's implicitly
4995 extern "C", modify the lang context appropriately. This is
4996 so horrible. Please die. */
4997 if (implicit_extern_c
4998 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
5000 implicit_extern_c
= !implicit_extern_c
;
5001 if (implicit_extern_c
)
5002 push_lang_context (lang_name_c
);
5004 pop_lang_context ();
5007 if (token
->type
== CPP_EOF
)
5012 /* Top-level module declarations are ok, and change the
5013 portion of file we're in. Top-level import declarations
5014 are significant for the import portions. */
5016 cp_token
*next
= token
;
5017 bool exporting
= token
->keyword
== RID__EXPORT
;
5020 cp_lexer_consume_token (parser
->lexer
);
5021 next
= cp_lexer_peek_token (parser
->lexer
);
5023 if (next
->keyword
== RID__MODULE
)
5026 = cp_parser_module_declaration (parser
, mp_state
, exporting
);
5029 else if (next
->keyword
== RID__IMPORT
)
5031 if (mp_state
== MP_FIRST
)
5032 mp_state
= MP_NOT_MODULE
;
5033 cp_parser_import_declaration (parser
, mp_state
, exporting
);
5037 gcc_checking_assert (!exporting
);
5039 if (mp_state
== MP_GLOBAL
&& token
->main_source_p
)
5041 static bool warned
= false;
5045 error_at (token
->location
,
5046 "global module fragment contents must be"
5047 " from preprocessor inclusion");
5052 /* This relies on the ordering of module_parse values. */
5053 if (mp_state
== MP_PURVIEW_IMPORTS
|| mp_state
== MP_PRIVATE_IMPORTS
)
5054 /* We're no longer in the import portion of a named module. */
5055 mp_state
= module_parse (mp_state
+ 1);
5056 else if (mp_state
== MP_FIRST
)
5057 mp_state
= MP_NOT_MODULE
;
5059 if (token
->type
== CPP_CLOSE_BRACE
)
5061 cp_parser_error (parser
, "expected declaration");
5062 cp_lexer_consume_token (parser
->lexer
);
5063 /* If the next token is now a `;', consume it. */
5064 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
5065 cp_lexer_consume_token (parser
->lexer
);
5068 cp_parser_toplevel_declaration (parser
);
5071 /* Get rid of the token array; we don't need it any more. */
5072 cp_lexer_destroy (parser
->lexer
);
5073 parser
->lexer
= NULL
;
5075 /* The EOF should have reset this. */
5076 gcc_checking_assert (!implicit_extern_c
);
5078 /* Make sure the declarator obstack was fully cleaned up. */
5079 gcc_assert (obstack_next_free (&declarator_obstack
)
5080 == declarator_obstack_base
);
5083 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5084 decltype context. */
5086 static inline tsubst_flags_t
5087 complain_flags (bool decltype_p
)
5089 tsubst_flags_t complain
= tf_warning_or_error
;
5091 complain
|= tf_decltype
;
5095 /* We're about to parse a collection of statements. If we're currently
5096 parsing tentatively, set up a firewall so that any nested
5097 cp_parser_commit_to_tentative_parse won't affect the current context. */
5099 static cp_token_position
5100 cp_parser_start_tentative_firewall (cp_parser
*parser
)
5102 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5105 cp_parser_parse_tentatively (parser
);
5106 cp_parser_commit_to_topmost_tentative_parse (parser
);
5107 return cp_lexer_token_position (parser
->lexer
, false);
5110 /* We've finished parsing the collection of statements. Wrap up the
5111 firewall and replace the relevant tokens with the parsed form. */
5114 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
5120 /* Finish the firewall level. */
5121 cp_parser_parse_definitely (parser
);
5122 /* And remember the result of the parse for when we try again. */
5123 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
5124 token
->type
= CPP_PREPARSED_EXPR
;
5125 token
->u
.value
= expr
;
5126 token
->keyword
= RID_MAX
;
5127 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5130 /* Like the above functions, but let the user modify the tokens. Used by
5131 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5132 later parses, so it makes sense to localize the effects of
5133 cp_parser_commit_to_tentative_parse. */
5135 struct tentative_firewall
5140 tentative_firewall (cp_parser
*p
): parser(p
)
5142 /* If we're currently parsing tentatively, start a committed level as a
5143 firewall and then an inner tentative parse. */
5144 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
5146 cp_parser_parse_tentatively (parser
);
5147 cp_parser_commit_to_topmost_tentative_parse (parser
);
5148 cp_parser_parse_tentatively (parser
);
5152 ~tentative_firewall()
5156 /* Finish the inner tentative parse and the firewall, propagating any
5157 uncommitted error state to the outer tentative parse. */
5158 bool err
= cp_parser_error_occurred (parser
);
5159 cp_parser_parse_definitely (parser
);
5160 cp_parser_parse_definitely (parser
);
5162 cp_parser_simulate_error (parser
);
5167 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5168 This class is for tracking such a matching pair of symbols.
5169 In particular, it tracks the location of the first token,
5170 so that if the second token is missing, we can highlight the
5171 location of the first token when notifying the user about the
5174 template <typename traits_t
>
5178 /* token_pair's ctor. */
5179 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
5181 /* If the next token is the opening symbol for this pair, consume it and
5183 Otherwise, issue an error and return false.
5184 In either case, record the location of the opening token. */
5186 bool require_open (cp_parser
*parser
)
5188 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5189 return cp_parser_require (parser
, traits_t::open_token_type
,
5190 traits_t::required_token_open
);
5193 /* Consume the next token from PARSER, recording its location as
5194 that of the opening token within the pair. */
5196 cp_token
* consume_open (cp_parser
*parser
)
5198 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
5199 gcc_assert (tok
->type
== traits_t::open_token_type
);
5200 m_open_loc
= tok
->location
;
5204 /* If the next token is the closing symbol for this pair, consume it
5206 Otherwise, issue an error, highlighting the location of the
5207 corresponding opening token, and return NULL. */
5209 cp_token
*require_close (cp_parser
*parser
) const
5211 return cp_parser_require (parser
, traits_t::close_token_type
,
5212 traits_t::required_token_close
,
5216 location_t
open_location () const { return m_open_loc
; }
5219 location_t m_open_loc
;
5222 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5224 struct matching_paren_traits
5226 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
5227 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
5228 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
5229 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
5232 /* "matching_parens" is a token_pair<T> class for tracking matching
5233 pairs of parentheses. */
5235 typedef token_pair
<matching_paren_traits
> matching_parens
;
5237 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5239 struct matching_brace_traits
5241 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
5242 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
5243 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
5244 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
5247 /* "matching_braces" is a token_pair<T> class for tracking matching
5250 typedef token_pair
<matching_brace_traits
> matching_braces
;
5253 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5254 enclosing parentheses. */
5257 cp_parser_statement_expr (cp_parser
*parser
)
5259 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
5261 /* Consume the '('. */
5262 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5263 matching_parens parens
;
5264 parens
.consume_open (parser
);
5265 /* Start the statement-expression. */
5266 tree expr
= begin_stmt_expr ();
5267 /* Parse the compound-statement. */
5268 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
5270 expr
= finish_stmt_expr (expr
, false);
5271 /* Consume the ')'. */
5272 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5273 if (!parens
.require_close (parser
))
5274 cp_parser_skip_to_end_of_statement (parser
);
5276 cp_parser_end_tentative_firewall (parser
, start
, expr
);
5277 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
5278 return cp_expr (expr
, combined_loc
);
5281 /* Expressions [gram.expr] */
5283 /* Parse a fold-operator.
5286 - * / % ^ & | = < > << >>
5287 = -= *= /= %= ^= &= |= <<= >>=
5288 == != <= >= && || , .* ->*
5290 This returns the tree code corresponding to the matched operator
5291 as an int. When the current token matches a compound assignment
5292 operator, the resulting tree code is the negative value of the
5293 non-assignment operator. */
5296 cp_parser_fold_operator (cp_token
*token
)
5298 switch (token
->type
)
5300 case CPP_PLUS
: return PLUS_EXPR
;
5301 case CPP_MINUS
: return MINUS_EXPR
;
5302 case CPP_MULT
: return MULT_EXPR
;
5303 case CPP_DIV
: return TRUNC_DIV_EXPR
;
5304 case CPP_MOD
: return TRUNC_MOD_EXPR
;
5305 case CPP_XOR
: return BIT_XOR_EXPR
;
5306 case CPP_AND
: return BIT_AND_EXPR
;
5307 case CPP_OR
: return BIT_IOR_EXPR
;
5308 case CPP_LSHIFT
: return LSHIFT_EXPR
;
5309 case CPP_RSHIFT
: return RSHIFT_EXPR
;
5311 case CPP_EQ
: return -NOP_EXPR
;
5312 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
5313 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
5314 case CPP_MULT_EQ
: return -MULT_EXPR
;
5315 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
5316 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
5317 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
5318 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
5319 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
5320 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
5321 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
5323 case CPP_EQ_EQ
: return EQ_EXPR
;
5324 case CPP_NOT_EQ
: return NE_EXPR
;
5325 case CPP_LESS
: return LT_EXPR
;
5326 case CPP_GREATER
: return GT_EXPR
;
5327 case CPP_LESS_EQ
: return LE_EXPR
;
5328 case CPP_GREATER_EQ
: return GE_EXPR
;
5330 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
5331 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
5333 case CPP_COMMA
: return COMPOUND_EXPR
;
5335 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
5336 case CPP_DEREF_STAR
: return MEMBER_REF
;
5338 default: return ERROR_MARK
;
5342 /* Returns true if CODE indicates a binary expression, which is not allowed in
5343 the LHS of a fold-expression. More codes will need to be added to use this
5344 function in other contexts. */
5347 is_binary_op (tree_code code
)
5352 case POINTER_PLUS_EXPR
:
5355 case TRUNC_DIV_EXPR
:
5356 case TRUNC_MOD_EXPR
:
5372 case TRUTH_ANDIF_EXPR
:
5373 case TRUTH_ORIF_EXPR
:
5386 /* If the next token is a suitable fold operator, consume it and return as
5387 the function above. */
5390 cp_parser_fold_operator (cp_parser
*parser
)
5392 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5393 int code
= cp_parser_fold_operator (token
);
5394 if (code
!= ERROR_MARK
)
5395 cp_lexer_consume_token (parser
->lexer
);
5399 /* Parse a fold-expression.
5402 ( ... folding-operator cast-expression)
5403 ( cast-expression folding-operator ... )
5404 ( cast-expression folding operator ... folding-operator cast-expression)
5406 Note that the '(' and ')' are matched in primary expression. */
5409 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5417 return error_mark_node
;
5418 cp_lexer_consume_token (parser
->lexer
);
5419 int op
= cp_parser_fold_operator (parser
);
5420 if (op
== ERROR_MARK
)
5422 cp_parser_error (parser
, "expected binary operator");
5423 return error_mark_node
;
5426 tree expr
= cp_parser_cast_expression (parser
, false, false,
5428 if (expr
== error_mark_node
)
5429 return error_mark_node
;
5430 return finish_left_unary_fold_expr (expr
, op
);
5433 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5434 int op
= cp_parser_fold_operator (parser
);
5435 if (op
== ERROR_MARK
)
5437 cp_parser_error (parser
, "expected binary operator");
5438 return error_mark_node
;
5441 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5443 cp_parser_error (parser
, "expected ...");
5444 return error_mark_node
;
5446 cp_lexer_consume_token (parser
->lexer
);
5448 /* The operands of a fold-expression are cast-expressions, so binary or
5449 conditional expressions are not allowed. We check this here to avoid
5450 tentative parsing. */
5451 if (EXPR_P (expr1
) && warning_suppressed_p (expr1
, OPT_Wparentheses
))
5452 /* OK, the expression was parenthesized. */;
5453 else if (is_binary_op (TREE_CODE (expr1
)))
5454 error_at (location_of (expr1
),
5455 "binary expression in operand of fold-expression");
5456 else if (TREE_CODE (expr1
) == COND_EXPR
5457 || (REFERENCE_REF_P (expr1
)
5458 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5459 error_at (location_of (expr1
),
5460 "conditional expression in operand of fold-expression");
5463 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5464 return finish_right_unary_fold_expr (expr1
, op
);
5466 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5468 cp_parser_error (parser
, "mismatched operator in fold-expression");
5469 return error_mark_node
;
5471 cp_lexer_consume_token (parser
->lexer
);
5473 // Binary left or right fold.
5474 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5475 if (expr2
== error_mark_node
)
5476 return error_mark_node
;
5477 return finish_binary_fold_expr (expr1
, expr2
, op
);
5480 /* Parse a primary-expression.
5487 lambda-expression (C++11)
5492 ( compound-statement )
5493 __builtin_va_arg ( assignment-expression , type-id )
5494 __builtin_offsetof ( type-id , offsetof-expression )
5497 __has_nothrow_assign ( type-id )
5498 __has_nothrow_constructor ( type-id )
5499 __has_nothrow_copy ( type-id )
5500 __has_trivial_assign ( type-id )
5501 __has_trivial_constructor ( type-id )
5502 __has_trivial_copy ( type-id )
5503 __has_trivial_destructor ( type-id )
5504 __has_virtual_destructor ( type-id )
5505 __is_abstract ( type-id )
5506 __is_base_of ( type-id , type-id )
5507 __is_class ( type-id )
5508 __is_empty ( type-id )
5509 __is_enum ( type-id )
5510 __is_final ( type-id )
5511 __is_literal_type ( type-id )
5512 __is_pod ( type-id )
5513 __is_polymorphic ( type-id )
5514 __is_std_layout ( type-id )
5515 __is_trivial ( type-id )
5516 __is_union ( type-id )
5518 Objective-C++ Extension:
5526 ADDRESS_P is true iff this expression was immediately preceded by
5527 "&" and therefore might denote a pointer-to-member. CAST_P is true
5528 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5529 true iff this expression is a template argument.
5531 Returns a representation of the expression. Upon return, *IDK
5532 indicates what kind of id-expression (if any) was present. */
5535 cp_parser_primary_expression (cp_parser
*parser
,
5538 bool template_arg_p
,
5542 cp_token
*token
= NULL
;
5544 /* Assume the primary expression is not an id-expression. */
5545 *idk
= CP_ID_KIND_NONE
;
5547 /* Peek at the next token. */
5548 token
= cp_lexer_peek_token (parser
->lexer
);
5549 switch ((int) token
->type
)
5558 user-defined-literal */
5565 case CPP_PREPARSED_EXPR
:
5566 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5567 return cp_parser_userdef_numeric_literal (parser
);
5568 token
= cp_lexer_consume_token (parser
->lexer
);
5569 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5571 error_at (token
->location
,
5572 "fixed-point types not supported in C++");
5573 return error_mark_node
;
5575 /* Floating-point literals are only allowed in an integral
5576 constant expression if they are cast to an integral or
5577 enumeration type. */
5578 if (TREE_CODE (token
->u
.value
) == REAL_CST
5579 && parser
->integral_constant_expression_p
5582 /* CAST_P will be set even in invalid code like "int(2.7 +
5583 ...)". Therefore, we have to check that the next token
5584 is sure to end the cast. */
5587 cp_token
*next_token
;
5589 next_token
= cp_lexer_peek_token (parser
->lexer
);
5590 if (/* The comma at the end of an
5591 enumerator-definition. */
5592 next_token
->type
!= CPP_COMMA
5593 /* The curly brace at the end of an enum-specifier. */
5594 && next_token
->type
!= CPP_CLOSE_BRACE
5595 /* The end of a statement. */
5596 && next_token
->type
!= CPP_SEMICOLON
5597 /* The end of the cast-expression. */
5598 && next_token
->type
!= CPP_CLOSE_PAREN
5599 /* The end of an array bound. */
5600 && next_token
->type
!= CPP_CLOSE_SQUARE
5601 /* The closing ">" in a template-argument-list. */
5602 && (next_token
->type
!= CPP_GREATER
5603 || parser
->greater_than_is_operator_p
)
5604 /* C++0x only: A ">>" treated like two ">" tokens,
5605 in a template-argument-list. */
5606 && (next_token
->type
!= CPP_RSHIFT
5607 || (cxx_dialect
== cxx98
)
5608 || parser
->greater_than_is_operator_p
))
5612 /* If we are within a cast, then the constraint that the
5613 cast is to an integral or enumeration type will be
5614 checked at that point. If we are not within a cast, then
5615 this code is invalid. */
5617 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5619 return (cp_expr (token
->u
.value
, token
->location
, token
->flags
& DECIMAL_INT
)
5620 .maybe_add_location_wrapper ());
5622 case CPP_CHAR_USERDEF
:
5623 case CPP_CHAR16_USERDEF
:
5624 case CPP_CHAR32_USERDEF
:
5625 case CPP_WCHAR_USERDEF
:
5626 case CPP_UTF8CHAR_USERDEF
:
5627 return cp_parser_userdef_char_literal (parser
);
5633 case CPP_UTF8STRING
:
5634 case CPP_STRING_USERDEF
:
5635 case CPP_STRING16_USERDEF
:
5636 case CPP_STRING32_USERDEF
:
5637 case CPP_WSTRING_USERDEF
:
5638 case CPP_UTF8STRING_USERDEF
:
5639 /* ??? Should wide strings be allowed when parser->translate_strings_p
5640 is false (i.e. in attributes)? If not, we can kill the third
5641 argument to cp_parser_string_literal. */
5642 return (cp_parser_string_literal (parser
,
5643 parser
->translate_strings_p
,
5645 .maybe_add_location_wrapper ());
5647 case CPP_OPEN_PAREN
:
5648 /* If we see `( { ' then we are looking at the beginning of
5649 a GNU statement-expression. */
5650 if (cp_parser_allow_gnu_extensions_p (parser
)
5651 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5653 /* Statement-expressions are not allowed by the standard. */
5654 pedwarn (token
->location
, OPT_Wpedantic
,
5655 "ISO C++ forbids braced-groups within expressions");
5657 /* And they're not allowed outside of a function-body; you
5658 cannot, for example, write:
5660 int i = ({ int j = 3; j + 1; });
5662 at class or namespace scope. */
5663 if (!parser
->in_function_body
5664 || parser
->in_template_argument_list_p
)
5666 error_at (token
->location
,
5667 "statement-expressions are not allowed outside "
5668 "functions nor in template-argument lists");
5669 cp_parser_skip_to_end_of_block_or_statement (parser
);
5670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5671 cp_lexer_consume_token (parser
->lexer
);
5672 return error_mark_node
;
5675 return cp_parser_statement_expr (parser
);
5677 /* Otherwise it's a normal parenthesized expression. */
5680 bool saved_greater_than_is_operator_p
;
5682 location_t open_paren_loc
= token
->location
;
5684 /* Consume the `('. */
5685 matching_parens parens
;
5686 parens
.consume_open (parser
);
5687 /* Within a parenthesized expression, a `>' token is always
5688 the greater-than operator. */
5689 saved_greater_than_is_operator_p
5690 = parser
->greater_than_is_operator_p
;
5691 parser
->greater_than_is_operator_p
= true;
5693 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5694 /* Left fold expression. */
5697 /* Parse the parenthesized expression. */
5698 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5700 token
= cp_lexer_peek_token (parser
->lexer
);
5701 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5703 expr
= cp_parser_fold_expression (parser
, expr
);
5704 if (expr
!= error_mark_node
5705 && cxx_dialect
< cxx17
)
5706 pedwarn (input_location
, OPT_Wc__17_extensions
,
5707 "fold-expressions only available with %<-std=c++17%> "
5708 "or %<-std=gnu++17%>");
5711 /* Let the front end know that this expression was
5712 enclosed in parentheses. This matters in case, for
5713 example, the expression is of the form `A::B', since
5714 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5716 expr
= finish_parenthesized_expr (expr
);
5718 /* DR 705: Wrapping an unqualified name in parentheses
5719 suppresses arg-dependent lookup. We want to pass back
5720 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5721 (c++/37862), but none of the others. */
5722 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5723 *idk
= CP_ID_KIND_NONE
;
5725 /* The `>' token might be the end of a template-id or
5726 template-parameter-list now. */
5727 parser
->greater_than_is_operator_p
5728 = saved_greater_than_is_operator_p
;
5730 /* Consume the `)'. */
5731 token
= cp_lexer_peek_token (parser
->lexer
);
5732 location_t close_paren_loc
= token
->location
;
5733 bool no_wparens
= warning_suppressed_p (expr
, OPT_Wparentheses
);
5734 expr
.set_range (open_paren_loc
, close_paren_loc
);
5736 suppress_warning (expr
, OPT_Wparentheses
);
5737 if (!parens
.require_close (parser
)
5738 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5739 cp_parser_skip_to_end_of_statement (parser
);
5744 case CPP_OPEN_SQUARE
:
5746 if (c_dialect_objc ())
5748 /* We might have an Objective-C++ message. */
5749 cp_parser_parse_tentatively (parser
);
5750 tree msg
= cp_parser_objc_message_expression (parser
);
5751 /* If that works out, we're done ... */
5752 if (cp_parser_parse_definitely (parser
))
5754 /* ... else, fall though to see if it's a lambda. */
5756 cp_expr lam
= cp_parser_lambda_expression (parser
);
5757 /* Don't warn about a failed tentative parse. */
5758 if (cp_parser_error_occurred (parser
))
5759 return error_mark_node
;
5760 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5764 case CPP_OBJC_STRING
:
5765 if (c_dialect_objc ())
5766 /* We have an Objective-C++ string literal. */
5767 return cp_parser_objc_expression (parser
);
5768 cp_parser_error (parser
, "expected primary-expression");
5769 return error_mark_node
;
5772 switch (token
->keyword
)
5774 /* These two are the boolean literals. */
5776 cp_lexer_consume_token (parser
->lexer
);
5777 return cp_expr (boolean_true_node
, token
->location
);
5779 cp_lexer_consume_token (parser
->lexer
);
5780 return cp_expr (boolean_false_node
, token
->location
);
5782 /* The `__null' literal. */
5784 cp_lexer_consume_token (parser
->lexer
);
5785 return cp_expr (null_node
, token
->location
);
5787 /* The `nullptr' literal. */
5789 cp_lexer_consume_token (parser
->lexer
);
5790 return cp_expr (nullptr_node
, token
->location
);
5792 /* Recognize the `this' keyword. */
5794 cp_lexer_consume_token (parser
->lexer
);
5795 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5797 error_at (token
->location
,
5798 "%<this%> may not be used in this context");
5799 return error_mark_node
;
5801 /* Pointers cannot appear in constant-expressions. */
5802 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5803 return error_mark_node
;
5804 return cp_expr (finish_this_expr (), token
->location
);
5806 /* The `operator' keyword can be the beginning of an
5811 case RID_FUNCTION_NAME
:
5812 case RID_PRETTY_FUNCTION_NAME
:
5813 case RID_C99_FUNCTION_NAME
:
5815 non_integral_constant name
;
5817 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5818 __func__ are the names of variables -- but they are
5819 treated specially. Therefore, they are handled here,
5820 rather than relying on the generic id-expression logic
5821 below. Grammatically, these names are id-expressions.
5823 Consume the token. */
5824 token
= cp_lexer_consume_token (parser
->lexer
);
5826 switch (token
->keyword
)
5828 case RID_FUNCTION_NAME
:
5829 name
= NIC_FUNC_NAME
;
5831 case RID_PRETTY_FUNCTION_NAME
:
5832 name
= NIC_PRETTY_FUNC
;
5834 case RID_C99_FUNCTION_NAME
:
5835 name
= NIC_C99_FUNC
;
5841 if (cp_parser_non_integral_constant_expression (parser
, name
))
5842 return error_mark_node
;
5844 /* Look up the name. */
5845 return finish_fname (token
->u
.value
);
5852 location_t type_location
;
5853 location_t start_loc
5854 = cp_lexer_peek_token (parser
->lexer
)->location
;
5855 /* The `__builtin_va_arg' construct is used to handle
5856 `va_arg'. Consume the `__builtin_va_arg' token. */
5857 cp_lexer_consume_token (parser
->lexer
);
5858 /* Look for the opening `('. */
5859 matching_parens parens
;
5860 parens
.require_open (parser
);
5861 /* Now, parse the assignment-expression. */
5862 expression
= cp_parser_assignment_expression (parser
);
5863 /* Look for the `,'. */
5864 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5865 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5866 /* Parse the type-id. */
5868 type_id_in_expr_sentinel
s (parser
);
5869 type
= cp_parser_type_id (parser
);
5871 /* Look for the closing `)'. */
5872 location_t finish_loc
5873 = cp_lexer_peek_token (parser
->lexer
)->location
;
5874 parens
.require_close (parser
);
5875 /* Using `va_arg' in a constant-expression is not
5877 if (cp_parser_non_integral_constant_expression (parser
,
5879 return error_mark_node
;
5880 /* Construct a location of the form:
5881 __builtin_va_arg (v, int)
5882 ~~~~~~~~~~~~~~~~~~~~~^~~~
5883 with the caret at the type, ranging from the start of the
5884 "__builtin_va_arg" token to the close paren. */
5885 location_t combined_loc
5886 = make_location (type_location
, start_loc
, finish_loc
);
5887 return build_x_va_arg (combined_loc
, expression
, type
);
5891 return cp_parser_builtin_offsetof (parser
);
5893 case RID_HAS_NOTHROW_ASSIGN
:
5894 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5895 case RID_HAS_NOTHROW_COPY
:
5896 case RID_HAS_TRIVIAL_ASSIGN
:
5897 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5898 case RID_HAS_TRIVIAL_COPY
:
5899 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5900 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5901 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5902 case RID_IS_ABSTRACT
:
5903 case RID_IS_AGGREGATE
:
5904 case RID_IS_BASE_OF
:
5909 case RID_IS_LAYOUT_COMPATIBLE
:
5910 case RID_IS_LITERAL_TYPE
:
5911 case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF
:
5913 case RID_IS_POLYMORPHIC
:
5914 case RID_IS_SAME_AS
:
5915 case RID_IS_STD_LAYOUT
:
5916 case RID_IS_TRIVIAL
:
5917 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5918 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5919 case RID_IS_TRIVIALLY_COPYABLE
:
5921 case RID_IS_ASSIGNABLE
:
5922 case RID_IS_CONSTRUCTIBLE
:
5923 case RID_IS_NOTHROW_ASSIGNABLE
:
5924 case RID_IS_NOTHROW_CONSTRUCTIBLE
:
5925 case RID_IS_CONVERTIBLE
:
5926 case RID_IS_NOTHROW_CONVERTIBLE
:
5927 case RID_REF_CONSTRUCTS_FROM_TEMPORARY
:
5928 case RID_REF_CONVERTS_FROM_TEMPORARY
:
5929 return cp_parser_trait_expr (parser
, token
->keyword
);
5933 return cp_parser_requires_expression (parser
);
5935 /* Objective-C++ expressions. */
5937 case RID_AT_PROTOCOL
:
5938 case RID_AT_SELECTOR
:
5939 return cp_parser_objc_expression (parser
);
5941 case RID_OMP_ALL_MEMORY
:
5942 gcc_assert (flag_openmp
);
5943 cp_lexer_consume_token (parser
->lexer
);
5944 error_at (token
->location
,
5945 "%<omp_all_memory%> may only be used in OpenMP "
5946 "%<depend%> clause");
5947 return error_mark_node
;
5950 if (parser
->in_function_body
5951 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5954 error_at (token
->location
,
5955 "a template declaration cannot appear at block scope");
5956 cp_parser_skip_to_end_of_block_or_statement (parser
);
5957 return error_mark_node
;
5961 cp_parser_error (parser
, "expected primary-expression");
5962 return error_mark_node
;
5965 /* An id-expression can start with either an identifier, a
5966 `::' as the beginning of a qualified-id, or the "operator"
5970 case CPP_TEMPLATE_ID
:
5971 case CPP_NESTED_NAME_SPECIFIER
:
5974 cp_expr id_expression
;
5976 const char *error_msg
;
5979 cp_token
*id_expr_token
;
5981 /* Parse the id-expression. */
5983 = cp_parser_id_expression (parser
,
5984 /*template_keyword_p=*/false,
5985 /*check_dependency_p=*/true,
5987 /*declarator_p=*/false,
5988 /*optional_p=*/false);
5989 if (id_expression
== error_mark_node
)
5990 return error_mark_node
;
5991 id_expr_token
= token
;
5992 token
= cp_lexer_peek_token (parser
->lexer
);
5993 done
= (token
->type
!= CPP_OPEN_SQUARE
5994 && token
->type
!= CPP_OPEN_PAREN
5995 && token
->type
!= CPP_DOT
5996 && token
->type
!= CPP_DEREF
5997 && token
->type
!= CPP_PLUS_PLUS
5998 && token
->type
!= CPP_MINUS_MINUS
);
5999 /* If we have a template-id, then no further lookup is
6000 required. If the template-id was for a template-class, we
6001 will sometimes have a TYPE_DECL at this point. */
6002 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
6003 || TREE_CODE (id_expression
) == TYPE_DECL
)
6004 decl
= id_expression
;
6005 /* Look up the name. */
6008 tree ambiguous_decls
;
6010 /* If we already know that this lookup is ambiguous, then
6011 we've already issued an error message; there's no reason
6013 if (id_expr_token
->type
== CPP_NAME
6014 && id_expr_token
->error_reported
)
6016 cp_parser_simulate_error (parser
);
6017 return error_mark_node
;
6020 decl
= cp_parser_lookup_name (parser
, id_expression
,
6023 /*is_namespace=*/false,
6024 /*check_dependency=*/true,
6026 id_expression
.get_location ());
6027 /* If the lookup was ambiguous, an error will already have
6029 if (ambiguous_decls
)
6030 return error_mark_node
;
6032 /* In Objective-C++, we may have an Objective-C 2.0
6033 dot-syntax for classes here. */
6034 if (c_dialect_objc ()
6035 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
6036 && TREE_CODE (decl
) == TYPE_DECL
6037 && objc_is_class_name (decl
))
6040 cp_lexer_consume_token (parser
->lexer
);
6041 component
= cp_parser_identifier (parser
);
6042 if (component
== error_mark_node
)
6043 return error_mark_node
;
6045 tree result
= objc_build_class_component_ref (id_expression
,
6047 /* Build a location of the form:
6050 with caret at the start of the component name (at
6051 input_location), ranging from the start of the id_expression
6052 to the end of the component name. */
6053 location_t combined_loc
6054 = make_location (input_location
, id_expression
.get_start (),
6055 get_finish (input_location
));
6056 protected_set_expr_location (result
, combined_loc
);
6060 /* In Objective-C++, an instance variable (ivar) may be preferred
6061 to whatever cp_parser_lookup_name() found.
6062 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6063 rest of c-family, we have to do a little extra work to preserve
6064 any location information in cp_expr "decl". Given that
6065 objc_lookup_ivar is implemented in "c-family" and "objc", we
6066 have a trip through the pure "tree" type, rather than cp_expr.
6067 Naively copying it back to "decl" would implicitly give the
6068 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6069 store an EXPR_LOCATION. Hence we only update "decl" (and
6070 hence its location_t) if we get back a different tree node. */
6071 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
6073 if (decl_tree
!= decl
.get_value ())
6074 decl
= cp_expr (decl_tree
);
6076 /* If name lookup gives us a SCOPE_REF, then the
6077 qualifying scope was dependent. */
6078 if (TREE_CODE (decl
) == SCOPE_REF
)
6080 /* At this point, we do not know if DECL is a valid
6081 integral constant expression. We assume that it is
6082 in fact such an expression, so that code like:
6084 template <int N> struct A {
6088 is accepted. At template-instantiation time, we
6089 will check that B<N>::i is actually a constant. */
6092 /* Check to see if DECL is a local variable in a context
6093 where that is forbidden. */
6094 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
6095 && local_variable_p (decl
)
6096 /* DR 2082 permits local variables in unevaluated contexts
6097 within a default argument. */
6098 && !cp_unevaluated_operand
)
6101 = (TREE_CODE (decl
) == PARM_DECL
6102 ? _("parameter %qD may not appear in this context")
6103 : _("local variable %qD may not appear in this context"));
6104 error_at (id_expression
.get_location (), msg
,
6106 return error_mark_node
;
6110 decl
= (finish_id_expression
6111 (id_expression
, decl
, parser
->scope
,
6113 parser
->integral_constant_expression_p
,
6114 parser
->allow_non_integral_constant_expression_p
,
6115 &parser
->non_integral_constant_expression_p
,
6116 template_p
, done
, address_p
,
6119 id_expression
.get_location ()));
6121 cp_parser_error (parser
, error_msg
);
6122 /* Build a location for an id-expression of the form:
6128 i.e. from the start of the first token to the end of the final
6129 token, with the caret at the start of the unqualified-id. */
6130 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
6131 location_t start_loc
= get_start (id_expr_token
->location
);
6132 location_t finish_loc
= get_finish (id_expression
.get_location ());
6133 location_t combined_loc
6134 = make_location (caret_loc
, start_loc
, finish_loc
);
6136 decl
.set_location (combined_loc
);
6140 /* Anything else is an error. */
6142 cp_parser_error (parser
, "expected primary-expression");
6143 return error_mark_node
;
6147 static inline cp_expr
6148 cp_parser_primary_expression (cp_parser
*parser
,
6151 bool template_arg_p
,
6154 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
6155 /*decltype*/false, idk
);
6158 /* Complain about missing template keyword when naming a dependent
6162 missing_template_diag (location_t loc
, diagnostic_t diag_kind
= DK_WARNING
)
6164 if (warning_suppressed_at (loc
, OPT_Wmissing_template_keyword
))
6167 gcc_rich_location
richloc (loc
);
6168 richloc
.add_fixit_insert_before ("template");
6169 emit_diagnostic (diag_kind
, &richloc
, OPT_Wmissing_template_keyword
,
6170 "expected %qs keyword before dependent "
6171 "template name", "template");
6172 suppress_warning_at (loc
, OPT_Wmissing_template_keyword
);
6175 /* Parse an id-expression.
6182 :: [opt] nested-name-specifier template [opt] unqualified-id
6184 :: operator-function-id
6187 Return a representation of the unqualified portion of the
6188 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6189 a `::' or nested-name-specifier.
6191 Often, if the id-expression was a qualified-id, the caller will
6192 want to make a SCOPE_REF to represent the qualified-id. This
6193 function does not do this in order to avoid wastefully creating
6194 SCOPE_REFs when they are not required.
6196 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6199 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6200 uninstantiated templates.
6202 If *TEMPLATE_P is non-NULL, it is set to true iff the
6203 `template' keyword is used to explicitly indicate that the entity
6204 named is a template.
6206 If DECLARATOR_P is true, the id-expression is appearing as part of
6207 a declarator, rather than as part of an expression. */
6210 cp_parser_id_expression (cp_parser
*parser
,
6211 bool template_keyword_p
,
6212 bool check_dependency_p
,
6217 bool global_scope_p
;
6218 bool nested_name_specifier_p
;
6220 /* Assume the `template' keyword was not used. */
6222 *template_p
= template_keyword_p
;
6224 /* Look for the optional `::' operator. */
6226 = (!template_keyword_p
6227 && (cp_parser_global_scope_opt (parser
,
6228 /*current_scope_valid_p=*/false)
6231 /* Look for the optional nested-name-specifier. */
6232 nested_name_specifier_p
6233 = (cp_parser_nested_name_specifier_opt (parser
,
6234 /*typename_keyword_p=*/false,
6241 cp_expr id
= NULL_TREE
;
6242 tree scope
= parser
->scope
;
6244 /* Peek at the next token. */
6245 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6247 /* If there is a nested-name-specifier, then we are looking at
6248 the first qualified-id production. */
6249 if (nested_name_specifier_p
)
6251 tree saved_object_scope
;
6252 tree saved_qualifying_scope
;
6254 /* See if the next token is the `template' keyword. */
6256 template_p
= &template_keyword_p
;
6257 *template_p
= cp_parser_optional_template_keyword (parser
);
6258 /* Name lookup we do during the processing of the
6259 unqualified-id might obliterate SCOPE. */
6260 saved_object_scope
= parser
->object_scope
;
6261 saved_qualifying_scope
= parser
->qualifying_scope
;
6262 /* Process the final unqualified-id. */
6263 id
= cp_parser_unqualified_id (parser
, *template_p
,
6266 /*optional_p=*/false);
6267 /* Restore the SAVED_SCOPE for our caller. */
6268 parser
->scope
= scope
;
6269 parser
->object_scope
= saved_object_scope
;
6270 parser
->qualifying_scope
= saved_qualifying_scope
;
6272 /* Otherwise, if we are in global scope, then we are looking at one
6273 of the other qualified-id productions. */
6274 else if (global_scope_p
)
6276 /* If it's an identifier, and the next token is not a "<", then
6277 we can avoid the template-id case. This is an optimization
6278 for this common case. */
6279 if (token
->type
== CPP_NAME
6280 && !cp_parser_nth_token_starts_template_argument_list_p
6282 return cp_parser_identifier (parser
);
6284 cp_parser_parse_tentatively (parser
);
6285 /* Try a template-id. */
6286 id
= cp_parser_template_id_expr (parser
,
6287 /*template_keyword_p=*/false,
6288 /*check_dependency_p=*/true,
6290 /* If that worked, we're done. */
6291 if (cp_parser_parse_definitely (parser
))
6294 /* Peek at the next token. (Changes in the token buffer may
6295 have invalidated the pointer obtained above.) */
6296 token
= cp_lexer_peek_token (parser
->lexer
);
6298 switch (token
->type
)
6301 id
= cp_parser_identifier (parser
);
6305 if (token
->keyword
== RID_OPERATOR
)
6307 id
= cp_parser_operator_function_id (parser
);
6313 cp_parser_error (parser
, "expected id-expression");
6314 return error_mark_node
;
6320 scope
= parser
->context
->object_type
;
6321 id
= cp_parser_unqualified_id (parser
, template_keyword_p
,
6322 /*check_dependency_p=*/true,
6327 if (id
&& TREE_CODE (id
) == IDENTIFIER_NODE
6328 && warn_missing_template_keyword
6329 && !template_keyword_p
6330 /* Don't warn if we're looking inside templates. */
6331 && check_dependency_p
6332 /* In a template argument list a > could be closing
6333 the enclosing targs. */
6334 && !parser
->in_template_argument_list_p
6335 && scope
&& dependentish_scope_p (scope
)
6336 /* Don't confuse an ill-formed constructor declarator for a missing
6337 template keyword in a return type. */
6338 && !(declarator_p
&& constructor_name_p (id
, scope
))
6339 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1)
6340 && warning_enabled_at (token
->location
,
6341 OPT_Wmissing_template_keyword
))
6343 saved_token_sentinel
toks (parser
->lexer
, STS_ROLLBACK
);
6344 if (cp_parser_skip_entire_template_parameter_list (parser
)
6345 /* An operator after the > suggests that the > ends a
6346 template-id; a name or literal suggests that the > is an
6348 && (cp_lexer_peek_token (parser
->lexer
)->type
6349 <= CPP_LAST_PUNCTUATOR
))
6350 missing_template_diag (token
->location
);
6356 /* Parse an unqualified-id.
6360 operator-function-id
6361 conversion-function-id
6365 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6366 keyword, in a construct like `A::template ...'.
6368 Returns a representation of unqualified-id. For the `identifier'
6369 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6370 production a BIT_NOT_EXPR is returned; the operand of the
6371 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6372 other productions, see the documentation accompanying the
6373 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6374 names are looked up in uninstantiated templates. If DECLARATOR_P
6375 is true, the unqualified-id is appearing as part of a declarator,
6376 rather than as part of an expression. */
6379 cp_parser_unqualified_id (cp_parser
* parser
,
6380 bool template_keyword_p
,
6381 bool check_dependency_p
,
6387 /* Peek at the next token. */
6388 token
= cp_lexer_peek_token (parser
->lexer
);
6390 switch ((int) token
->type
)
6396 /* We don't know yet whether or not this will be a
6398 cp_parser_parse_tentatively (parser
);
6399 /* Try a template-id. */
6400 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6403 /* If it worked, we're done. */
6404 if (cp_parser_parse_definitely (parser
))
6406 /* Otherwise, it's an ordinary identifier. */
6407 return cp_parser_identifier (parser
);
6410 case CPP_TEMPLATE_ID
:
6411 return cp_parser_template_id_expr (parser
, template_keyword_p
,
6418 tree qualifying_scope
;
6422 location_t tilde_loc
= token
->location
;
6424 /* Consume the `~' token. */
6425 cp_lexer_consume_token (parser
->lexer
);
6426 /* Parse the class-name. The standard, as written, seems to
6429 template <typename T> struct S { ~S (); };
6430 template <typename T> S<T>::~S() {}
6432 is invalid, since `~' must be followed by a class-name, but
6433 `S<T>' is dependent, and so not known to be a class.
6434 That's not right; we need to look in uninstantiated
6435 templates. A further complication arises from:
6437 template <typename T> void f(T t) {
6441 Here, it is not possible to look up `T' in the scope of `T'
6442 itself. We must look in both the current scope, and the
6443 scope of the containing complete expression.
6445 Yet another issue is:
6454 The standard does not seem to say that the `S' in `~S'
6455 should refer to the type `S' and not the data member
6458 /* DR 244 says that we look up the name after the "~" in the
6459 same scope as we looked up the qualifying name. That idea
6460 isn't fully worked out; it's more complicated than that. */
6461 scope
= parser
->scope
;
6462 object_scope
= parser
->object_scope
;
6463 qualifying_scope
= parser
->qualifying_scope
;
6465 /* Check for invalid scopes. */
6466 if (scope
== error_mark_node
)
6468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6469 cp_lexer_consume_token (parser
->lexer
);
6470 return error_mark_node
;
6472 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6474 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6475 error_at (token
->location
,
6476 "scope %qT before %<~%> is not a class-name",
6478 cp_parser_simulate_error (parser
);
6479 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6480 cp_lexer_consume_token (parser
->lexer
);
6481 return error_mark_node
;
6483 if (template_keyword_p
)
6485 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6486 error_at (tilde_loc
, "%<template%> keyword not permitted in "
6488 cp_parser_simulate_error (parser
);
6489 return error_mark_node
;
6492 gcc_assert (!scope
|| TYPE_P (scope
));
6494 token
= cp_lexer_peek_token (parser
->lexer
);
6496 /* Create a location with caret == start at the tilde,
6497 finishing at the end of the peeked token, e.g:
6501 = make_location (tilde_loc
, tilde_loc
, token
->location
);
6503 /* If the name is of the form "X::~X" it's OK even if X is a
6507 && token
->type
== CPP_NAME
6508 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6510 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6511 || (CLASS_TYPE_P (scope
)
6512 && constructor_name_p (token
->u
.value
, scope
))))
6514 cp_lexer_consume_token (parser
->lexer
);
6515 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6518 /* ~auto means the destructor of whatever the object is. */
6519 if (cp_parser_is_keyword (token
, RID_AUTO
))
6521 if (cxx_dialect
< cxx14
)
6522 pedwarn (loc
, OPT_Wc__14_extensions
,
6523 "%<~auto%> only available with "
6524 "%<-std=c++14%> or %<-std=gnu++14%>");
6525 cp_lexer_consume_token (parser
->lexer
);
6526 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, make_auto ());
6529 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6530 declarator-id of a constructor or destructor. */
6531 if (token
->type
== CPP_TEMPLATE_ID
&& declarator_p
6532 && cxx_dialect
>= cxx20
)
6534 if (!cp_parser_simulate_error (parser
))
6535 error_at (tilde_loc
, "template-id not allowed for destructor");
6536 return error_mark_node
;
6539 /* If there was an explicit qualification (S::~T), first look
6540 in the scope given by the qualification (i.e., S).
6542 Note: in the calls to cp_parser_class_name below we pass
6543 typename_type so that lookup finds the injected-class-name
6544 rather than the constructor. */
6546 type_decl
= NULL_TREE
;
6549 cp_parser_parse_tentatively (parser
);
6550 type_decl
= cp_parser_class_name (parser
,
6551 /*typename_keyword_p=*/false,
6552 /*template_keyword_p=*/false,
6554 /*check_dependency=*/false,
6555 /*class_head_p=*/false,
6557 if (cp_parser_parse_definitely (parser
))
6560 /* In "N::S::~S", look in "N" as well. */
6561 if (!done
&& scope
&& qualifying_scope
)
6563 cp_parser_parse_tentatively (parser
);
6564 parser
->scope
= qualifying_scope
;
6565 parser
->object_scope
= NULL_TREE
;
6566 parser
->qualifying_scope
= NULL_TREE
;
6568 = cp_parser_class_name (parser
,
6569 /*typename_keyword_p=*/false,
6570 /*template_keyword_p=*/false,
6572 /*check_dependency=*/false,
6573 /*class_head_p=*/false,
6575 if (cp_parser_parse_definitely (parser
))
6578 /* In "p->S::~T", look in the scope given by "*p" as well. */
6579 else if (!done
&& object_scope
)
6581 cp_parser_parse_tentatively (parser
);
6582 parser
->scope
= object_scope
;
6583 parser
->object_scope
= NULL_TREE
;
6584 parser
->qualifying_scope
= NULL_TREE
;
6586 = cp_parser_class_name (parser
,
6587 /*typename_keyword_p=*/false,
6588 /*template_keyword_p=*/false,
6590 /*check_dependency=*/false,
6591 /*class_head_p=*/false,
6593 if (cp_parser_parse_definitely (parser
))
6596 /* Look in the surrounding context. */
6599 parser
->scope
= NULL_TREE
;
6600 parser
->object_scope
= NULL_TREE
;
6601 parser
->qualifying_scope
= NULL_TREE
;
6602 if (processing_template_decl
)
6603 cp_parser_parse_tentatively (parser
);
6605 = cp_parser_class_name (parser
,
6606 /*typename_keyword_p=*/false,
6607 /*template_keyword_p=*/false,
6609 /*check_dependency=*/false,
6610 /*class_head_p=*/false,
6612 if (processing_template_decl
6613 && ! cp_parser_parse_definitely (parser
))
6615 /* We couldn't find a type with this name. If we're parsing
6616 tentatively, fail and try something else. */
6617 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6619 cp_parser_simulate_error (parser
);
6620 return error_mark_node
;
6622 /* Otherwise, accept it and check for a match at instantiation
6624 type_decl
= cp_parser_identifier (parser
);
6625 if (type_decl
!= error_mark_node
)
6626 type_decl
= build_min_nt_loc (loc
, BIT_NOT_EXPR
, type_decl
);
6630 /* If an error occurred, assume that the name of the
6631 destructor is the same as the name of the qualifying
6632 class. That allows us to keep parsing after running
6633 into ill-formed destructor names. */
6634 if (type_decl
== error_mark_node
&& scope
)
6635 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6636 else if (type_decl
== error_mark_node
)
6637 return error_mark_node
;
6639 /* Check that destructor name and scope match. */
6640 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6642 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6644 "declaration of %<~%T%> as member of %qT",
6646 cp_parser_simulate_error (parser
);
6647 return error_mark_node
;
6652 A typedef-name that names a class shall not be used as the
6653 identifier in the declarator for a destructor declaration. */
6655 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6656 && !DECL_SELF_REFERENCE_P (type_decl
)
6657 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6659 "typedef-name %qD used as destructor declarator",
6662 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6666 if (token
->keyword
== RID_OPERATOR
)
6670 /* This could be a template-id, so we try that first. */
6671 cp_parser_parse_tentatively (parser
);
6672 /* Try a template-id. */
6673 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6674 /*check_dependency_p=*/true,
6676 /* If that worked, we're done. */
6677 if (cp_parser_parse_definitely (parser
))
6679 /* We still don't know whether we're looking at an
6680 operator-function-id or a conversion-function-id. */
6681 cp_parser_parse_tentatively (parser
);
6682 /* Try an operator-function-id. */
6683 id
= cp_parser_operator_function_id (parser
);
6684 /* If that didn't work, try a conversion-function-id. */
6685 if (!cp_parser_parse_definitely (parser
))
6686 id
= cp_parser_conversion_function_id (parser
);
6695 cp_parser_error (parser
, "expected unqualified-id");
6696 return error_mark_node
;
6700 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6701 be a template-id or the name shall refer to a class template or an
6705 check_template_keyword_in_nested_name_spec (tree name
)
6707 if (CLASS_TYPE_P (name
)
6708 && ((CLASSTYPE_USE_TEMPLATE (name
)
6709 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name
)))
6710 || CLASSTYPE_IS_TEMPLATE (name
)))
6713 if (TREE_CODE (name
) == TYPENAME_TYPE
6714 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name
)) == TEMPLATE_ID_EXPR
)
6716 /* Alias templates are also OK. */
6717 else if (alias_template_specialization_p (name
, nt_opaque
))
6720 permerror (input_location
, TYPE_P (name
)
6721 ? G_("%qT is not a template")
6722 : G_("%qD is not a template"),
6726 /* Parse an (optional) nested-name-specifier.
6728 nested-name-specifier: [C++98]
6729 class-or-namespace-name :: nested-name-specifier [opt]
6730 class-or-namespace-name :: template nested-name-specifier [opt]
6732 nested-name-specifier: [C++0x]
6735 nested-name-specifier identifier ::
6736 nested-name-specifier template [opt] simple-template-id ::
6738 PARSER->SCOPE should be set appropriately before this function is
6739 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6740 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6743 Sets PARSER->SCOPE to the class (TYPE) or namespace
6744 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6745 it unchanged if there is no nested-name-specifier. Returns the new
6746 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6748 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6750 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6751 part of a declaration and/or decl-specifier. */
6754 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6755 bool typename_keyword_p
,
6756 bool check_dependency_p
,
6758 bool is_declaration
,
6759 bool template_keyword_p
/* = false */)
6761 bool success
= false;
6762 cp_token_position start
= 0;
6765 /* Remember where the nested-name-specifier starts. */
6766 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
6767 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
6769 start
= cp_lexer_token_position (parser
->lexer
, false);
6770 push_deferring_access_checks (dk_deferred
);
6777 tree saved_qualifying_scope
;
6779 /* Spot cases that cannot be the beginning of a
6780 nested-name-specifier. */
6781 token
= cp_lexer_peek_token (parser
->lexer
);
6783 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6784 the already parsed nested-name-specifier. */
6785 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6787 /* Grab the nested-name-specifier and continue the loop. */
6788 cp_parser_pre_parsed_nested_name_specifier (parser
);
6789 /* If we originally encountered this nested-name-specifier
6790 with CHECK_DEPENDENCY_P set to true, we will not have
6791 resolved TYPENAME_TYPEs, so we must do so here. */
6793 && !check_dependency_p
6794 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6796 new_scope
= resolve_typename_type (parser
->scope
,
6797 /*only_current_p=*/false);
6798 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6799 parser
->scope
= new_scope
;
6805 /* Spot cases that cannot be the beginning of a
6806 nested-name-specifier. On the second and subsequent times
6807 through the loop, we look for the `template' keyword. */
6808 if (success
&& token
->keyword
== RID_TEMPLATE
)
6810 /* A template-id can start a nested-name-specifier. */
6811 else if (token
->type
== CPP_TEMPLATE_ID
)
6813 /* DR 743: decltype can be used in a nested-name-specifier. */
6814 else if (token_is_decltype (token
))
6818 /* If the next token is not an identifier, then it is
6819 definitely not a type-name or namespace-name. */
6820 if (token
->type
!= CPP_NAME
)
6822 /* If the following token is neither a `<' (to begin a
6823 template-id), nor a `::', then we are not looking at a
6824 nested-name-specifier. */
6825 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6827 if (token
->type
== CPP_COLON
6828 && parser
->colon_corrects_to_scope_p
6829 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
6830 /* name:name is a valid sequence in an Objective C message. */
6831 && !parser
->objective_c_message_context_p
)
6833 gcc_rich_location
richloc (token
->location
);
6834 richloc
.add_fixit_replace ("::");
6836 "found %<:%> in nested-name-specifier, "
6838 token
->type
= CPP_SCOPE
;
6841 if (token
->type
!= CPP_SCOPE
6842 && !cp_parser_nth_token_starts_template_argument_list_p
6847 /* The nested-name-specifier is optional, so we parse
6849 cp_parser_parse_tentatively (parser
);
6851 /* Look for the optional `template' keyword, if this isn't the
6852 first time through the loop. */
6855 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6856 /* DR1710: "In a qualified-id used as the name in
6857 a typename-specifier, elaborated-type-specifier, using-declaration,
6858 or class-or-decltype, an optional keyword template appearing at
6859 the top level is ignored." */
6860 if (!template_keyword_p
6861 && typename_keyword_p
6862 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
6863 template_keyword_p
= true;
6866 /* Save the old scope since the name lookup we are about to do
6867 might destroy it. */
6868 old_scope
= parser
->scope
;
6869 saved_qualifying_scope
= parser
->qualifying_scope
;
6870 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6871 look up names in "X<T>::I" in order to determine that "Y" is
6872 a template. So, if we have a typename at this point, we make
6873 an effort to look through it. */
6875 && !check_dependency_p
6876 && !typename_keyword_p
6878 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6879 parser
->scope
= resolve_typename_type (parser
->scope
,
6880 /*only_current_p=*/false);
6881 /* Parse the qualifying entity. */
6883 = cp_parser_qualifying_entity (parser
,
6889 /* Look for the `::' token. */
6890 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6892 /* If we found what we wanted, we keep going; otherwise, we're
6894 if (!cp_parser_parse_definitely (parser
))
6896 bool error_p
= false;
6898 /* Restore the OLD_SCOPE since it was valid before the
6899 failed attempt at finding the last
6900 class-or-namespace-name. */
6901 parser
->scope
= old_scope
;
6902 parser
->qualifying_scope
= saved_qualifying_scope
;
6904 /* If the next token is a decltype, and the one after that is a
6905 `::', then the decltype has failed to resolve to a class or
6906 enumeration type. Give this error even when parsing
6907 tentatively since it can't possibly be valid--and we're going
6908 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6909 won't get another chance.*/
6910 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6911 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6914 token
= cp_lexer_consume_token (parser
->lexer
);
6915 tree dtype
= token
->u
.tree_check_value
->value
;
6916 if (dtype
!= error_mark_node
)
6917 error_at (token
->location
, "%<decltype%> evaluates to %qT, "
6918 "which is not a class or enumeration type",
6920 parser
->scope
= error_mark_node
;
6924 cp_lexer_consume_token (parser
->lexer
);
6927 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6928 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6930 /* If we have a non-type template-id followed by ::, it can't
6931 possibly be valid. */
6932 token
= cp_lexer_peek_token (parser
->lexer
);
6933 tree tid
= token
->u
.tree_check_value
->value
;
6934 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6935 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6937 tree tmpl
= NULL_TREE
;
6938 if (is_overloaded_fn (tid
))
6940 tree fns
= get_fns (tid
);
6941 if (OVL_SINGLE_P (fns
))
6942 tmpl
= OVL_FIRST (fns
);
6943 if (function_concept_p (fns
))
6944 error_at (token
->location
, "concept-id %qD "
6945 "in nested-name-specifier", tid
);
6947 error_at (token
->location
, "function template-id "
6948 "%qD in nested-name-specifier", tid
);
6952 tmpl
= TREE_OPERAND (tid
, 0);
6953 if (variable_concept_p (tmpl
)
6954 || standard_concept_p (tmpl
))
6955 error_at (token
->location
, "concept-id %qD "
6956 "in nested-name-specifier", tid
);
6959 /* Variable template. */
6960 gcc_assert (variable_template_p (tmpl
));
6961 error_at (token
->location
, "variable template-id "
6962 "%qD in nested-name-specifier", tid
);
6966 inform (DECL_SOURCE_LOCATION (tmpl
),
6967 "%qD declared here", tmpl
);
6969 parser
->scope
= error_mark_node
;
6973 cp_lexer_consume_token (parser
->lexer
);
6974 cp_lexer_consume_token (parser
->lexer
);
6978 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6980 /* If the next token is an identifier, and the one after
6981 that is a `::', then any valid interpretation would have
6982 found a class-or-namespace-name. */
6983 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6984 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6986 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6989 token
= cp_lexer_consume_token (parser
->lexer
);
6992 if (!token
->error_reported
)
6995 tree ambiguous_decls
;
6997 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6999 /*is_template=*/false,
7000 /*is_namespace=*/false,
7001 /*check_dependency=*/true,
7004 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7005 error_at (token
->location
,
7006 "%qD used without template arguments",
7008 else if (ambiguous_decls
)
7010 // cp_parser_lookup_name has the same diagnostic,
7011 // thus make sure to emit it at most once.
7012 if (cp_parser_uncommitted_to_tentative_parse_p
7015 error_at (token
->location
,
7016 "reference to %qD is ambiguous",
7018 print_candidates (ambiguous_decls
);
7020 decl
= error_mark_node
;
7024 if (cxx_dialect
!= cxx98
)
7025 cp_parser_name_lookup_error
7026 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
7029 cp_parser_name_lookup_error
7030 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
7034 parser
->scope
= error_mark_node
;
7036 /* Treat this as a successful nested-name-specifier
7041 If the name found is not a class-name (clause
7042 _class_) or namespace-name (_namespace.def_), the
7043 program is ill-formed. */
7046 cp_lexer_consume_token (parser
->lexer
);
7050 /* We've found one valid nested-name-specifier. */
7052 /* Name lookup always gives us a DECL. */
7053 if (TREE_CODE (new_scope
) == TYPE_DECL
)
7054 new_scope
= TREE_TYPE (new_scope
);
7055 /* Uses of "template" must be followed by actual templates. */
7056 if (template_keyword_p
)
7057 check_template_keyword_in_nested_name_spec (new_scope
);
7058 /* If it is a class scope, try to complete it; we are about to
7059 be looking up names inside the class. */
7060 if (TYPE_P (new_scope
)
7061 /* Since checking types for dependency can be expensive,
7062 avoid doing it if the type is already complete. */
7063 && !COMPLETE_TYPE_P (new_scope
)
7064 /* Do not try to complete dependent types. */
7065 && !dependent_type_p (new_scope
))
7067 new_scope
= complete_type (new_scope
);
7068 /* If it is a typedef to current class, use the current
7069 class instead, as the typedef won't have any names inside
7071 if (!COMPLETE_TYPE_P (new_scope
)
7072 && currently_open_class (new_scope
))
7073 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
7075 /* Make sure we look in the right scope the next time through
7077 parser
->scope
= new_scope
;
7080 /* If parsing tentatively, replace the sequence of tokens that makes
7081 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7082 token. That way, should we re-parse the token stream, we will
7083 not have to repeat the effort required to do the parse, nor will
7084 we issue duplicate error messages. */
7085 if (success
&& start
)
7089 token
= cp_lexer_token_at (parser
->lexer
, start
);
7090 /* Reset the contents of the START token. */
7091 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
7092 /* Retrieve any deferred checks. Do not pop this access checks yet
7093 so the memory will not be reclaimed during token replacing below. */
7094 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
7095 token
->tree_check_p
= true;
7096 token
->u
.tree_check_value
->value
= parser
->scope
;
7097 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
7098 token
->u
.tree_check_value
->qualifying_scope
=
7099 parser
->qualifying_scope
;
7100 token
->keyword
= RID_MAX
;
7102 /* Purge all subsequent tokens. */
7103 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
7107 pop_to_parent_deferring_access_checks ();
7109 return success
? parser
->scope
: NULL_TREE
;
7112 /* Parse a nested-name-specifier. See
7113 cp_parser_nested_name_specifier_opt for details. This function
7114 behaves identically, except that it will an issue an error if no
7115 nested-name-specifier is present. */
7118 cp_parser_nested_name_specifier (cp_parser
*parser
,
7119 bool typename_keyword_p
,
7120 bool check_dependency_p
,
7122 bool is_declaration
)
7126 /* Look for the nested-name-specifier. */
7127 scope
= cp_parser_nested_name_specifier_opt (parser
,
7132 /* If it was not present, issue an error message. */
7135 cp_parser_error (parser
, "expected nested-name-specifier");
7136 parser
->scope
= NULL_TREE
;
7142 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7143 this is either a class-name or a namespace-name (which corresponds
7144 to the class-or-namespace-name production in the grammar). For
7145 C++0x, it can also be a type-name that refers to an enumeration
7146 type or a simple-template-id.
7148 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7149 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7150 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7151 TYPE_P is TRUE iff the next name should be taken as a class-name,
7152 even the same name is declared to be another entity in the same
7155 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7156 specified by the class-or-namespace-name. If neither is found the
7157 ERROR_MARK_NODE is returned. */
7160 cp_parser_qualifying_entity (cp_parser
*parser
,
7161 bool typename_keyword_p
,
7162 bool template_keyword_p
,
7163 bool check_dependency_p
,
7165 bool is_declaration
)
7168 tree saved_qualifying_scope
;
7169 tree saved_object_scope
;
7172 bool successful_parse_p
;
7174 /* DR 743: decltype can appear in a nested-name-specifier. */
7175 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
7177 scope
= cp_parser_decltype (parser
);
7178 if (TREE_CODE (scope
) != ENUMERAL_TYPE
7179 && !MAYBE_CLASS_TYPE_P (scope
))
7181 cp_parser_simulate_error (parser
);
7182 return error_mark_node
;
7184 if (TYPE_NAME (scope
))
7185 scope
= TYPE_NAME (scope
);
7189 /* Before we try to parse the class-name, we must save away the
7190 current PARSER->SCOPE since cp_parser_class_name will destroy
7192 saved_scope
= parser
->scope
;
7193 saved_qualifying_scope
= parser
->qualifying_scope
;
7194 saved_object_scope
= parser
->object_scope
;
7195 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7196 there is no need to look for a namespace-name. */
7197 only_class_p
= template_keyword_p
7198 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
7200 cp_parser_parse_tentatively (parser
);
7201 scope
= cp_parser_class_name (parser
,
7204 type_p
? class_type
: none_type
,
7206 /*class_head_p=*/false,
7208 /*enum_ok=*/cxx_dialect
> cxx98
);
7209 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
7210 /* If that didn't work, try for a namespace-name. */
7211 if (!only_class_p
&& !successful_parse_p
)
7213 /* Restore the saved scope. */
7214 parser
->scope
= saved_scope
;
7215 parser
->qualifying_scope
= saved_qualifying_scope
;
7216 parser
->object_scope
= saved_object_scope
;
7217 /* If we are not looking at an identifier followed by the scope
7218 resolution operator, then this is not part of a
7219 nested-name-specifier. (Note that this function is only used
7220 to parse the components of a nested-name-specifier.) */
7221 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
7222 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
7223 return error_mark_node
;
7224 scope
= cp_parser_namespace_name (parser
);
7230 /* Return true if we are looking at a compound-literal, false otherwise. */
7233 cp_parser_compound_literal_p (cp_parser
*parser
)
7235 cp_lexer_save_tokens (parser
->lexer
);
7237 /* Skip tokens until the next token is a closing parenthesis.
7238 If we find the closing `)', and the next token is a `{', then
7239 we are looking at a compound-literal. */
7240 bool compound_literal_p
7241 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7242 /*consume_paren=*/true)
7243 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
7245 /* Roll back the tokens we skipped. */
7246 cp_lexer_rollback_tokens (parser
->lexer
);
7248 return compound_literal_p
;
7251 /* Return true if EXPR is the integer constant zero or a complex constant
7252 of zero, without any folding, but ignoring location wrappers. */
7255 literal_integer_zerop (const_tree expr
)
7257 return (location_wrapper_p (expr
)
7258 && integer_zerop (TREE_OPERAND (expr
, 0)));
7261 /* Parse a postfix-expression.
7265 postfix-expression [ expression ]
7266 postfix-expression ( expression-list [opt] )
7267 simple-type-specifier ( expression-list [opt] )
7268 typename :: [opt] nested-name-specifier identifier
7269 ( expression-list [opt] )
7270 typename :: [opt] nested-name-specifier template [opt] template-id
7271 ( expression-list [opt] )
7272 postfix-expression . template [opt] id-expression
7273 postfix-expression -> template [opt] id-expression
7274 postfix-expression . pseudo-destructor-name
7275 postfix-expression -> pseudo-destructor-name
7276 postfix-expression ++
7277 postfix-expression --
7278 dynamic_cast < type-id > ( expression )
7279 static_cast < type-id > ( expression )
7280 reinterpret_cast < type-id > ( expression )
7281 const_cast < type-id > ( expression )
7282 typeid ( expression )
7288 ( type-id ) { initializer-list , [opt] }
7290 This extension is a GNU version of the C99 compound-literal
7291 construct. (The C99 grammar uses `type-name' instead of `type-id',
7292 but they are essentially the same concept.)
7294 If ADDRESS_P is true, the postfix expression is the operand of the
7295 `&' operator. CAST_P is true if this expression is the target of a
7298 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7299 class member access expressions [expr.ref].
7301 Returns a representation of the expression. */
7304 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7305 bool member_access_only_p
, bool decltype_p
,
7306 cp_id_kind
* pidk_return
)
7311 cp_id_kind idk
= CP_ID_KIND_NONE
;
7312 cp_expr postfix_expression
= NULL_TREE
;
7313 bool is_member_access
= false;
7315 /* Peek at the next token. */
7316 token
= cp_lexer_peek_token (parser
->lexer
);
7317 loc
= token
->location
;
7318 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
7320 /* Some of the productions are determined by keywords. */
7321 keyword
= token
->keyword
;
7331 const char *saved_message
;
7332 bool saved_in_type_id_in_expr_p
;
7334 /* All of these can be handled in the same way from the point
7335 of view of parsing. Begin by consuming the token
7336 identifying the cast. */
7337 cp_lexer_consume_token (parser
->lexer
);
7339 /* New types cannot be defined in the cast. */
7340 saved_message
= parser
->type_definition_forbidden_message
;
7341 parser
->type_definition_forbidden_message
7342 = G_("types may not be defined in casts");
7344 /* Look for the opening `<'. */
7345 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
7346 /* Parse the type to which we are casting. */
7347 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7348 parser
->in_type_id_in_expr_p
= true;
7349 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
7351 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7352 /* Look for the closing `>'. */
7353 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
7354 /* Restore the old message. */
7355 parser
->type_definition_forbidden_message
= saved_message
;
7357 bool saved_greater_than_is_operator_p
7358 = parser
->greater_than_is_operator_p
;
7359 parser
->greater_than_is_operator_p
= true;
7361 /* And the expression which is being cast. */
7362 matching_parens parens
;
7363 parens
.require_open (parser
);
7364 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
7365 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
7367 location_t end_loc
= close_paren
?
7368 close_paren
->location
: UNKNOWN_LOCATION
;
7370 parser
->greater_than_is_operator_p
7371 = saved_greater_than_is_operator_p
;
7373 /* Only type conversions to integral or enumeration types
7374 can be used in constant-expressions. */
7375 if (!cast_valid_in_integral_constant_expression_p (type
)
7376 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
7378 postfix_expression
= error_mark_node
;
7382 /* Construct a location e.g. :
7383 reinterpret_cast <int *> (expr)
7384 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7385 ranging from the start of the "*_cast" token to the final closing
7386 paren, with the caret at the start. */
7387 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
7393 = build_dynamic_cast (cp_cast_loc
, type
, expression
,
7394 tf_warning_or_error
);
7398 = build_static_cast (cp_cast_loc
, type
, expression
,
7399 tf_warning_or_error
);
7403 = build_reinterpret_cast (cp_cast_loc
, type
, expression
,
7404 tf_warning_or_error
);
7408 = build_const_cast (cp_cast_loc
, type
, expression
,
7409 tf_warning_or_error
);
7420 const char *saved_message
;
7421 bool saved_in_type_id_in_expr_p
;
7423 /* Consume the `typeid' token. */
7424 cp_lexer_consume_token (parser
->lexer
);
7425 /* Look for the `(' token. */
7426 matching_parens parens
;
7427 parens
.require_open (parser
);
7428 /* Types cannot be defined in a `typeid' expression. */
7429 saved_message
= parser
->type_definition_forbidden_message
;
7430 parser
->type_definition_forbidden_message
7431 = G_("types may not be defined in a %<typeid%> expression");
7432 /* We can't be sure yet whether we're looking at a type-id or an
7434 cp_parser_parse_tentatively (parser
);
7435 /* Try a type-id first. */
7436 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7437 parser
->in_type_id_in_expr_p
= true;
7438 type
= cp_parser_type_id (parser
);
7439 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7440 /* Look for the `)' token. Otherwise, we can't be sure that
7441 we're not looking at an expression: consider `typeid (int
7442 (3))', for example. */
7443 cp_token
*close_paren
= parens
.require_close (parser
);
7444 /* If all went well, simply lookup the type-id. */
7445 if (cp_parser_parse_definitely (parser
))
7446 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
7447 /* Otherwise, fall back to the expression variant. */
7452 /* Look for an expression. */
7453 expression
= cp_parser_expression (parser
, & idk
);
7454 /* Compute its typeid. */
7455 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
7456 /* Look for the `)' token. */
7457 close_paren
= parens
.require_close (parser
);
7459 /* Restore the saved message. */
7460 parser
->type_definition_forbidden_message
= saved_message
;
7461 /* `typeid' may not appear in an integral constant expression. */
7462 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
7463 postfix_expression
= error_mark_node
;
7465 /* Construct a location e.g. :
7468 ranging from the start of the "typeid" token to the final closing
7469 paren, with the caret at the start. */
7472 location_t typeid_loc
7473 = make_location (start_loc
, start_loc
, close_paren
->location
);
7474 postfix_expression
.set_location (typeid_loc
);
7475 postfix_expression
.maybe_add_location_wrapper ();
7483 /* The syntax permitted here is the same permitted for an
7484 elaborated-type-specifier. */
7485 ++parser
->prevent_constrained_type_specifiers
;
7486 type
= cp_parser_elaborated_type_specifier (parser
,
7487 /*is_friend=*/false,
7488 /*is_declaration=*/false);
7489 --parser
->prevent_constrained_type_specifiers
;
7490 postfix_expression
= cp_parser_functional_cast (parser
, type
);
7495 case RID_BUILTIN_SHUFFLE
:
7496 case RID_BUILTIN_SHUFFLEVECTOR
:
7497 case RID_BUILTIN_LAUNDER
:
7498 case RID_BUILTIN_ASSOC_BARRIER
:
7500 vec
<tree
, va_gc
> *vec
;
7502 cp_lexer_consume_token (parser
->lexer
);
7503 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
7504 /*cast_p=*/false, /*allow_expansion_p=*/true,
7505 /*non_constant_p=*/NULL
);
7508 postfix_expression
= error_mark_node
;
7518 if (vec
->length () == 1)
7520 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
7523 error_at (loc
, "wrong number of arguments to "
7524 "%<__builtin_addressof%>");
7525 postfix_expression
= error_mark_node
;
7529 case RID_BUILTIN_LAUNDER
:
7530 if (vec
->length () == 1)
7531 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7532 tf_warning_or_error
);
7535 error_at (loc
, "wrong number of arguments to "
7536 "%<__builtin_launder%>");
7537 postfix_expression
= error_mark_node
;
7541 case RID_BUILTIN_ASSOC_BARRIER
:
7542 if (vec
->length () == 1)
7543 postfix_expression
= build1_loc (loc
, PAREN_EXPR
,
7544 TREE_TYPE ((*vec
)[0]),
7548 error_at (loc
, "wrong number of arguments to "
7549 "%<__builtin_assoc_barrier%>");
7550 postfix_expression
= error_mark_node
;
7554 case RID_BUILTIN_SHUFFLE
:
7555 if (vec
->length () == 2)
7557 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7558 (*vec
)[1], tf_warning_or_error
);
7559 else if (vec
->length () == 3)
7561 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7562 (*vec
)[2], tf_warning_or_error
);
7565 error_at (loc
, "wrong number of arguments to "
7566 "%<__builtin_shuffle%>");
7567 postfix_expression
= error_mark_node
;
7571 case RID_BUILTIN_SHUFFLEVECTOR
:
7572 if (vec
->length () < 3)
7574 error_at (loc
, "wrong number of arguments to "
7575 "%<__builtin_shufflevector%>");
7576 postfix_expression
= error_mark_node
;
7581 = build_x_shufflevector (loc
, vec
, tf_warning_or_error
);
7591 case RID_BUILTIN_CONVERTVECTOR
:
7595 /* Consume the `__builtin_convertvector' token. */
7596 cp_lexer_consume_token (parser
->lexer
);
7597 /* Look for the opening `('. */
7598 matching_parens parens
;
7599 parens
.require_open (parser
);
7600 /* Now, parse the assignment-expression. */
7601 expression
= cp_parser_assignment_expression (parser
);
7602 /* Look for the `,'. */
7603 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7604 location_t type_location
7605 = cp_lexer_peek_token (parser
->lexer
)->location
;
7606 /* Parse the type-id. */
7608 type_id_in_expr_sentinel
s (parser
);
7609 type
= cp_parser_type_id (parser
);
7611 /* Look for the closing `)'. */
7612 parens
.require_close (parser
);
7614 = cp_build_vec_convert (expression
, type_location
, type
,
7615 tf_warning_or_error
);
7619 case RID_BUILTIN_BIT_CAST
:
7623 /* Consume the `__builtin_bit_cast' token. */
7624 cp_lexer_consume_token (parser
->lexer
);
7625 /* Look for the opening `('. */
7626 matching_parens parens
;
7627 parens
.require_open (parser
);
7628 location_t type_location
7629 = cp_lexer_peek_token (parser
->lexer
)->location
;
7630 /* Parse the type-id. */
7632 type_id_in_expr_sentinel
s (parser
);
7633 type
= cp_parser_type_id (parser
);
7635 /* Look for the `,'. */
7636 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7637 /* Now, parse the assignment-expression. */
7638 expression
= cp_parser_assignment_expression (parser
);
7639 /* Look for the closing `)'. */
7640 parens
.require_close (parser
);
7642 = cp_build_bit_cast (type_location
, type
, expression
,
7643 tf_warning_or_error
);
7651 /* If the next thing is a simple-type-specifier, we may be
7652 looking at a functional cast. We could also be looking at
7653 an id-expression. So, we try the functional cast, and if
7654 that doesn't work we fall back to the primary-expression. */
7655 cp_parser_parse_tentatively (parser
);
7656 /* Look for the simple-type-specifier. */
7657 ++parser
->prevent_constrained_type_specifiers
;
7658 type
= cp_parser_simple_type_specifier (parser
,
7659 /*decl_specs=*/NULL
,
7660 CP_PARSER_FLAGS_NONE
);
7661 --parser
->prevent_constrained_type_specifiers
;
7662 /* Parse the cast itself. */
7663 if (!cp_parser_error_occurred (parser
))
7665 = cp_parser_functional_cast (parser
, type
);
7666 /* If that worked, we're done. */
7667 if (cp_parser_parse_definitely (parser
))
7670 /* If the functional-cast didn't work out, try a
7671 compound-literal. */
7672 if (cp_parser_allow_gnu_extensions_p (parser
)
7673 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7675 cp_expr initializer
= NULL_TREE
;
7677 cp_parser_parse_tentatively (parser
);
7679 matching_parens parens
;
7680 parens
.consume_open (parser
);
7682 /* Avoid calling cp_parser_type_id pointlessly, see comment
7683 in cp_parser_cast_expression about c++/29234. */
7684 if (!cp_parser_compound_literal_p (parser
))
7685 cp_parser_simulate_error (parser
);
7688 /* Parse the type. */
7689 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7690 parser
->in_type_id_in_expr_p
= true;
7691 type
= cp_parser_type_id (parser
);
7692 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7693 parens
.require_close (parser
);
7696 /* If things aren't going well, there's no need to
7698 if (!cp_parser_error_occurred (parser
))
7700 bool non_constant_p
;
7701 /* Parse the brace-enclosed initializer list. */
7702 initializer
= cp_parser_braced_list (parser
,
7705 /* If that worked, we're definitely looking at a
7706 compound-literal expression. */
7707 if (cp_parser_parse_definitely (parser
))
7709 /* Warn the user that a compound literal is not
7710 allowed in standard C++. */
7711 pedwarn (input_location
, OPT_Wpedantic
,
7712 "ISO C++ forbids compound-literals");
7713 /* For simplicity, we disallow compound literals in
7714 constant-expressions. We could
7715 allow compound literals of integer type, whose
7716 initializer was a constant, in constant
7717 expressions. Permitting that usage, as a further
7718 extension, would not change the meaning of any
7719 currently accepted programs. (Of course, as
7720 compound literals are not part of ISO C++, the
7721 standard has nothing to say.) */
7722 if (cp_parser_non_integral_constant_expression (parser
,
7725 postfix_expression
= error_mark_node
;
7728 /* Form the representation of the compound-literal. */
7730 = finish_compound_literal (type
, initializer
,
7731 tf_warning_or_error
, fcl_c99
);
7732 postfix_expression
.set_location (initializer
.get_location ());
7737 /* It must be a primary-expression. */
7739 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7740 /*template_arg_p=*/false,
7747 /* Note that we don't need to worry about calling build_cplus_new on a
7748 class-valued CALL_EXPR in decltype when it isn't the end of the
7749 postfix-expression; unary_complex_lvalue will take care of that for
7752 /* Keep looping until the postfix-expression is complete. */
7755 if (idk
== CP_ID_KIND_UNQUALIFIED
7756 && identifier_p (postfix_expression
)
7757 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7758 /* It is not a Koenig lookup function call. */
7760 = unqualified_name_lookup_error (postfix_expression
);
7762 /* Peek at the next token. */
7763 token
= cp_lexer_peek_token (parser
->lexer
);
7765 switch (token
->type
)
7767 case CPP_OPEN_SQUARE
:
7768 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7770 cp_parser_error (parser
,
7771 "two consecutive %<[%> shall "
7772 "only introduce an attribute");
7773 return error_mark_node
;
7776 = cp_parser_postfix_open_square_expression (parser
,
7780 postfix_expression
.set_range (start_loc
,
7781 postfix_expression
.get_location ());
7783 idk
= CP_ID_KIND_NONE
;
7784 is_member_access
= false;
7787 case CPP_OPEN_PAREN
:
7788 /* postfix-expression ( expression-list [opt] ) */
7791 bool is_builtin_constant_p
;
7792 bool saved_integral_constant_expression_p
= false;
7793 bool saved_non_integral_constant_expression_p
= false;
7794 tsubst_flags_t complain
= complain_flags (decltype_p
);
7795 vec
<tree
, va_gc
> *args
;
7796 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7797 location_t combined_loc
= UNKNOWN_LOCATION
;
7799 is_member_access
= false;
7801 tree stripped_expression
7802 = tree_strip_any_location_wrapper (postfix_expression
);
7803 is_builtin_constant_p
7804 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7805 if (is_builtin_constant_p
)
7807 /* The whole point of __builtin_constant_p is to allow
7808 non-constant expressions to appear as arguments. */
7809 saved_integral_constant_expression_p
7810 = parser
->integral_constant_expression_p
;
7811 saved_non_integral_constant_expression_p
7812 = parser
->non_integral_constant_expression_p
;
7813 parser
->integral_constant_expression_p
= false;
7815 args
= (cp_parser_parenthesized_expression_list
7817 /*cast_p=*/false, /*allow_expansion_p=*/true,
7818 /*non_constant_p=*/NULL
,
7819 /*close_paren_loc=*/&close_paren_loc
,
7820 /*wrap_locations_p=*/true));
7821 if (is_builtin_constant_p
)
7823 parser
->integral_constant_expression_p
7824 = saved_integral_constant_expression_p
;
7825 parser
->non_integral_constant_expression_p
7826 = saved_non_integral_constant_expression_p
;
7831 postfix_expression
= error_mark_node
;
7835 /* Function calls are not permitted in
7836 constant-expressions. */
7837 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7838 && cp_parser_non_integral_constant_expression (parser
,
7841 postfix_expression
= error_mark_node
;
7842 release_tree_vector (args
);
7847 if (idk
== CP_ID_KIND_UNQUALIFIED
7848 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7850 if (identifier_p (postfix_expression
)
7851 /* In C++20, we may need to perform ADL for a template
7853 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7854 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7856 if (!args
->is_empty ())
7859 if (!any_type_dependent_arguments_p (args
))
7861 = perform_koenig_lookup (postfix_expression
, args
,
7866 = unqualified_fn_lookup_error (postfix_expression
);
7868 /* We do not perform argument-dependent lookup if
7869 normal lookup finds a non-function, in accordance
7870 with the expected resolution of DR 218. */
7871 else if (!args
->is_empty ()
7872 && is_overloaded_fn (postfix_expression
))
7874 /* Do not do argument dependent lookup if regular
7875 lookup finds a member function or a block-scope
7876 function declaration. [basic.lookup.argdep]/3 */
7877 bool do_adl_p
= true;
7878 tree fns
= get_fns (postfix_expression
);
7879 for (lkp_iterator
iter (fns
); iter
; ++iter
)
7881 tree fn
= STRIP_TEMPLATE (*iter
);
7882 if ((TREE_CODE (fn
) == USING_DECL
7883 && DECL_DEPENDENT_P (fn
))
7884 || DECL_FUNCTION_MEMBER_P (fn
)
7885 || DECL_LOCAL_DECL_P (fn
))
7895 if (!any_type_dependent_arguments_p (args
))
7897 = perform_koenig_lookup (postfix_expression
, args
,
7903 /* Temporarily set input_location to the combined location
7904 with call expression range, as e.g. build_out_target_exprs
7905 called from convert_default_arg relies on input_location,
7906 so updating it only when the call is fully built results
7907 in inconsistencies between location handling in templates
7908 and outside of templates. */
7909 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7910 combined_loc
= make_location (token
->location
, start_loc
,
7912 iloc_sentinel
ils (combined_loc
);
7914 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7916 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7917 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7919 if (processing_template_decl
7920 && (type_dependent_object_expression_p (instance
)
7921 || (!BASELINK_P (fn
)
7922 && TREE_CODE (fn
) != FIELD_DECL
)
7923 || type_dependent_expression_p (fn
)
7924 || any_type_dependent_arguments_p (args
)))
7926 maybe_generic_this_capture (instance
, fn
);
7928 = build_min_nt_call_vec (postfix_expression
, args
);
7930 else if (BASELINK_P (fn
))
7933 = (build_new_method_call
7934 (instance
, fn
, &args
, NULL_TREE
,
7935 (idk
== CP_ID_KIND_QUALIFIED
7936 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7943 = finish_call_expr (postfix_expression
, &args
,
7944 /*disallow_virtual=*/false,
7948 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7949 || TREE_CODE (postfix_expression
) == MEMBER_REF
7950 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7951 postfix_expression
= (build_offset_ref_call_from_tree
7952 (postfix_expression
, &args
,
7954 else if (idk
== CP_ID_KIND_QUALIFIED
)
7955 /* A call to a static class member, or a namespace-scope
7958 = finish_call_expr (postfix_expression
, &args
,
7959 /*disallow_virtual=*/true,
7963 /* All other function calls. */
7965 = finish_call_expr (postfix_expression
, &args
,
7966 /*disallow_virtual=*/false,
7970 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7971 postfix_expression
.set_location (combined_loc
);
7973 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7974 idk
= CP_ID_KIND_NONE
;
7976 release_tree_vector (args
);
7982 /* postfix-expression . template [opt] id-expression
7983 postfix-expression . pseudo-destructor-name
7984 postfix-expression -> template [opt] id-expression
7985 postfix-expression -> pseudo-destructor-name */
7987 /* Consume the `.' or `->' operator. */
7988 cp_lexer_consume_token (parser
->lexer
);
7991 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7995 is_member_access
= true;
7999 /* postfix-expression ++ */
8000 /* Consume the `++' token. */
8001 cp_lexer_consume_token (parser
->lexer
);
8002 /* Generate a representation for the complete expression. */
8004 = finish_increment_expr (postfix_expression
,
8005 POSTINCREMENT_EXPR
);
8006 /* Increments may not appear in constant-expressions. */
8007 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
8008 postfix_expression
= error_mark_node
;
8009 idk
= CP_ID_KIND_NONE
;
8010 is_member_access
= false;
8013 case CPP_MINUS_MINUS
:
8014 /* postfix-expression -- */
8015 /* Consume the `--' token. */
8016 cp_lexer_consume_token (parser
->lexer
);
8017 /* Generate a representation for the complete expression. */
8019 = finish_increment_expr (postfix_expression
,
8020 POSTDECREMENT_EXPR
);
8021 /* Decrements may not appear in constant-expressions. */
8022 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
8023 postfix_expression
= error_mark_node
;
8024 idk
= CP_ID_KIND_NONE
;
8025 is_member_access
= false;
8029 if (pidk_return
!= NULL
)
8030 * pidk_return
= idk
;
8031 if (member_access_only_p
)
8032 return is_member_access
8033 ? postfix_expression
8034 : cp_expr (error_mark_node
);
8036 return postfix_expression
;
8041 /* Helper function for cp_parser_parenthesized_expression_list and
8042 cp_parser_postfix_open_square_expression. Parse a single element
8043 of parenthesized expression list. */
8046 cp_parser_parenthesized_expression_list_elt (cp_parser
*parser
, bool cast_p
,
8047 bool allow_expansion_p
,
8048 bool *non_constant_p
)
8050 cp_expr
expr (NULL_TREE
);
8051 bool expr_non_constant_p
;
8053 /* Parse the next assignment-expression. */
8054 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8056 /* A braced-init-list. */
8057 cp_lexer_set_source_position (parser
->lexer
);
8058 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8059 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8060 if (non_constant_p
&& expr_non_constant_p
)
8061 *non_constant_p
= true;
8063 else if (non_constant_p
)
8065 expr
= cp_parser_constant_expression (parser
,
8066 /*allow_non_constant_p=*/true,
8067 &expr_non_constant_p
);
8068 if (expr_non_constant_p
)
8069 *non_constant_p
= true;
8072 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
, cast_p
);
8074 /* If we have an ellipsis, then this is an expression expansion. */
8075 if (allow_expansion_p
8076 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8078 /* Consume the `...'. */
8079 cp_lexer_consume_token (parser
->lexer
);
8081 /* Build the argument pack. */
8082 expr
= make_pack_expansion (expr
);
8087 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8088 by cp_parser_builtin_offsetof. We're looking for
8090 postfix-expression [ expression ]
8091 postfix-expression [ braced-init-list ] (C++11)
8092 postfix-expression [ expression-list[opt] ] (C++23)
8094 FOR_OFFSETOF is set if we're being called in that context, which
8095 changes how we deal with integer constant expressions. */
8098 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
8099 tree postfix_expression
,
8103 tree index
= NULL_TREE
;
8104 releasing_vec expression_list
= NULL
;
8105 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8106 bool saved_greater_than_is_operator_p
;
8108 /* Consume the `[' token. */
8109 cp_lexer_consume_token (parser
->lexer
);
8111 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
8112 parser
->greater_than_is_operator_p
= true;
8114 /* Parse the index expression. */
8115 /* ??? For offsetof, there is a question of what to allow here. If
8116 offsetof is not being used in an integral constant expression context,
8117 then we *could* get the right answer by computing the value at runtime.
8118 If we are in an integral constant expression context, then we might
8119 could accept any constant expression; hard to say without analysis.
8120 Rather than open the barn door too wide right away, allow only integer
8121 constant expressions here. */
8123 index
= cp_parser_constant_expression (parser
);
8126 if (cxx_dialect
>= cxx23
8127 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
8128 *&expression_list
= make_tree_vector ();
8129 else if (cxx_dialect
>= cxx23
)
8134 = cp_parser_parenthesized_expression_list_elt (parser
,
8142 if (expr
== error_mark_node
)
8143 index
= error_mark_node
;
8144 else if (expression_list
.get () == NULL
8145 && !PACK_EXPANSION_P (expr
.get_value ()))
8146 index
= expr
.get_value ();
8148 vec_safe_push (expression_list
, expr
.get_value ());
8150 /* If the next token isn't a `,', then we are done. */
8151 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8154 if (expression_list
.get () == NULL
&& index
!= error_mark_node
)
8156 *&expression_list
= make_tree_vector_single (index
);
8160 /* Otherwise, consume the `,' and keep going. */
8161 cp_lexer_consume_token (parser
->lexer
);
8163 if (expression_list
.get () && index
== error_mark_node
)
8164 expression_list
.release ();
8166 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8168 bool expr_nonconst_p
;
8169 cp_lexer_set_source_position (parser
->lexer
);
8170 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8171 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
8174 index
= cp_parser_expression (parser
, NULL
, /*cast_p=*/false,
8175 /*decltype_p=*/false,
8176 /*warn_comma_p=*/warn_comma_subscript
);
8179 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
8181 /* Look for the closing `]'. */
8182 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8184 /* Build the ARRAY_REF. */
8185 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
8186 index
, &expression_list
,
8188 | (decltype_p
? tf_decltype
: 0));
8190 /* When not doing offsetof, array references are not permitted in
8191 constant-expressions. */
8193 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
8194 postfix_expression
= error_mark_node
;
8196 return postfix_expression
;
8199 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8200 dereference of incomplete type, returns true if error_mark_node should
8201 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8202 and *DEPENDENT_P. */
8205 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
8208 /* In a template, be permissive by treating an object expression
8209 of incomplete type as dependent (after a pedwarn). */
8210 diagnostic_t kind
= (processing_template_decl
8211 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
8213 switch (TREE_CODE (*postfix_expression
))
8216 case REINTERPRET_CAST_EXPR
:
8217 case CONST_CAST_EXPR
:
8218 case STATIC_CAST_EXPR
:
8219 case DYNAMIC_CAST_EXPR
:
8220 case IMPLICIT_CONV_EXPR
:
8221 case VIEW_CONVERT_EXPR
:
8222 case NON_LVALUE_EXPR
:
8226 /* Don't emit any diagnostic for OVERLOADs. */
8230 /* Avoid clobbering e.g. DECLs. */
8231 if (!EXPR_P (*postfix_expression
))
8236 if (kind
== DK_IGNORED
)
8239 location_t exploc
= location_of (*postfix_expression
);
8240 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
8241 if (!MAYBE_CLASS_TYPE_P (*scope
))
8243 if (kind
== DK_ERROR
)
8244 *scope
= *postfix_expression
= error_mark_node
;
8245 else if (processing_template_decl
)
8247 *dependent_p
= true;
8248 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
8253 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8254 by cp_parser_builtin_offsetof. We're looking for
8256 postfix-expression . template [opt] id-expression
8257 postfix-expression . pseudo-destructor-name
8258 postfix-expression -> template [opt] id-expression
8259 postfix-expression -> pseudo-destructor-name
8261 FOR_OFFSETOF is set if we're being called in that context. That sorta
8262 limits what of the above we'll actually accept, but nevermind.
8263 TOKEN_TYPE is the "." or "->" token, which will already have been
8264 removed from the stream. */
8267 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
8268 enum cpp_ttype token_type
,
8269 cp_expr postfix_expression
,
8270 bool for_offsetof
, cp_id_kind
*idk
,
8271 location_t location
)
8275 bool pseudo_destructor_p
;
8276 tree scope
= NULL_TREE
;
8277 location_t start_loc
= postfix_expression
.get_start ();
8279 /* If this is a `->' operator, dereference the pointer. */
8280 if (token_type
== CPP_DEREF
)
8281 postfix_expression
= build_x_arrow (location
, postfix_expression
,
8282 tf_warning_or_error
);
8283 /* Check to see whether or not the expression is type-dependent and
8284 not the current instantiation. */
8285 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
8286 /* The identifier following the `->' or `.' is not qualified. */
8287 parser
->scope
= NULL_TREE
;
8288 parser
->qualifying_scope
= NULL_TREE
;
8289 parser
->object_scope
= NULL_TREE
;
8290 *idk
= CP_ID_KIND_NONE
;
8292 /* Enter the scope corresponding to the type of the object
8293 given by the POSTFIX_EXPRESSION. */
8296 scope
= TREE_TYPE (postfix_expression
);
8297 /* According to the standard, no expression should ever have
8298 reference type. Unfortunately, we do not currently match
8299 the standard in this respect in that our internal representation
8300 of an expression may have reference type even when the standard
8301 says it does not. Therefore, we have to manually obtain the
8302 underlying type here. */
8303 scope
= non_reference (scope
);
8304 /* The type of the POSTFIX_EXPRESSION must be complete. */
8305 /* Unlike the object expression in other contexts, *this is not
8306 required to be of complete type for purposes of class member
8307 access (5.2.5) outside the member function body. */
8308 if (postfix_expression
!= current_class_ref
8309 && scope
!= error_mark_node
8310 && !currently_open_class (scope
))
8312 scope
= complete_type (scope
);
8313 if (!COMPLETE_TYPE_P (scope
)
8314 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
8316 return error_mark_node
;
8321 /* Let the name lookup machinery know that we are processing a
8322 class member access expression. */
8323 parser
->context
->object_type
= scope
;
8324 /* If something went wrong, we want to be able to discern that case,
8325 as opposed to the case where there was no SCOPE due to the type
8326 of expression being dependent. */
8328 scope
= error_mark_node
;
8329 /* If the SCOPE was erroneous, make the various semantic analysis
8330 functions exit quickly -- and without issuing additional error
8332 if (scope
== error_mark_node
)
8333 postfix_expression
= error_mark_node
;
8339 tree type
= TREE_TYPE (postfix_expression
);
8340 /* If we don't have a (type-dependent) object of class type, use
8341 typeof to figure out the type of the object. */
8342 if (type
== NULL_TREE
|| is_auto (type
))
8343 type
= finish_typeof (postfix_expression
);
8344 parser
->context
->object_type
= type
;
8347 /* Assume this expression is not a pseudo-destructor access. */
8348 pseudo_destructor_p
= false;
8350 /* If the SCOPE is a scalar type, then, if this is a valid program,
8351 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8352 is type dependent, it can be pseudo-destructor-name or something else.
8353 Try to parse it as pseudo-destructor-name first. */
8354 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
8359 cp_parser_parse_tentatively (parser
);
8360 /* Parse the pseudo-destructor-name. */
8362 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
8365 && (cp_parser_error_occurred (parser
)
8366 || !SCALAR_TYPE_P (type
)))
8367 cp_parser_abort_tentative_parse (parser
);
8368 else if (cp_parser_parse_definitely (parser
))
8370 pseudo_destructor_p
= true;
8372 = finish_pseudo_destructor_expr (postfix_expression
,
8377 if (!pseudo_destructor_p
)
8379 /* If the SCOPE is not a scalar type, we are looking at an
8380 ordinary class member access expression, rather than a
8381 pseudo-destructor-name. */
8383 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8384 /* Parse the id-expression. */
8385 name
= (cp_parser_id_expression
8387 cp_parser_optional_template_keyword (parser
),
8388 /*check_dependency_p=*/true,
8390 /*declarator_p=*/false,
8391 /*optional_p=*/false));
8392 /* In general, build a SCOPE_REF if the member name is qualified.
8393 However, if the name was not dependent and has already been
8394 resolved; there is no need to build the SCOPE_REF. For example;
8396 struct X { void f(); };
8397 template <typename T> void f(T* t) { t->X::f(); }
8399 Even though "t" is dependent, "X::f" is not and has been resolved
8400 to a BASELINK; there is no need to include scope information. */
8402 /* But we do need to remember that there was an explicit scope for
8403 virtual function calls. */
8405 *idk
= CP_ID_KIND_QUALIFIED
;
8407 /* If the name is a template-id that names a type, we will get a
8408 TYPE_DECL here. That is invalid code. */
8409 if (TREE_CODE (name
) == TYPE_DECL
)
8411 error_at (token
->location
, "invalid use of %qD", name
);
8412 postfix_expression
= error_mark_node
;
8416 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
8418 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
8420 error_at (token
->location
, "%<%D::%D%> is not a class member",
8421 parser
->scope
, name
);
8422 postfix_expression
= error_mark_node
;
8425 name
= build_qualified_name (/*type=*/NULL_TREE
,
8429 parser
->scope
= NULL_TREE
;
8430 parser
->qualifying_scope
= NULL_TREE
;
8431 parser
->object_scope
= NULL_TREE
;
8433 if (parser
->scope
&& name
&& BASELINK_P (name
))
8434 adjust_result_of_qualified_name_lookup
8435 (name
, parser
->scope
, scope
);
8437 = finish_class_member_access_expr (postfix_expression
, name
,
8439 tf_warning_or_error
);
8440 /* Build a location e.g.:
8443 where the caret is at the deref token, ranging from
8444 the start of postfix_expression to the end of the access expr. */
8445 location_t combined_loc
8446 = make_location (input_location
, start_loc
, parser
->lexer
);
8447 protected_set_expr_location (postfix_expression
, combined_loc
);
8451 /* We no longer need to look up names in the scope of the object on
8452 the left-hand side of the `.' or `->' operator. */
8453 parser
->context
->object_type
= NULL_TREE
;
8455 /* Outside of offsetof, these operators may not appear in
8456 constant-expressions. */
8458 && (cp_parser_non_integral_constant_expression
8459 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
8460 postfix_expression
= error_mark_node
;
8462 return postfix_expression
;
8465 /* Parse a parenthesized expression-list.
8468 assignment-expression
8469 expression-list, assignment-expression
8474 identifier, expression-list
8476 CAST_P is true if this expression is the target of a cast.
8478 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8481 WRAP_LOCATIONS_P is true if expressions within this list for which
8482 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8483 their source locations.
8485 Returns a vector of trees. Each element is a representation of an
8486 assignment-expression. NULL is returned if the ( and or ) are
8487 missing. An empty, but allocated, vector is returned on no
8488 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8489 if we are parsing an attribute list for an attribute that wants a
8490 plain identifier argument, normal_attr for an attribute that wants
8491 an expression, or non_attr if we aren't parsing an attribute list. If
8492 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8493 not all of the expressions in the list were constant.
8494 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8495 will be written to with the location of the closing parenthesis. If
8496 an error occurs, it may or may not be written to. */
8498 static vec
<tree
, va_gc
> *
8499 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
8500 int is_attribute_list
,
8502 bool allow_expansion_p
,
8503 bool *non_constant_p
,
8504 location_t
*close_paren_loc
,
8505 bool wrap_locations_p
)
8507 vec
<tree
, va_gc
> *expression_list
;
8508 tree identifier
= NULL_TREE
;
8509 bool saved_greater_than_is_operator_p
;
8511 /* Assume all the expressions will be constant. */
8513 *non_constant_p
= false;
8515 matching_parens parens
;
8516 if (!parens
.require_open (parser
))
8519 expression_list
= make_tree_vector ();
8521 /* Within a parenthesized expression, a `>' token is always
8522 the greater-than operator. */
8523 saved_greater_than_is_operator_p
8524 = parser
->greater_than_is_operator_p
;
8525 parser
->greater_than_is_operator_p
= true;
8527 cp_expr
expr (NULL_TREE
);
8529 /* Consume expressions until there are no more. */
8530 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
8533 /* At the beginning of attribute lists, check to see if the
8534 next token is an identifier. */
8535 if (is_attribute_list
== id_attr
8536 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
8540 /* Consume the identifier. */
8541 token
= cp_lexer_consume_token (parser
->lexer
);
8542 /* Save the identifier. */
8543 identifier
= token
->u
.value
;
8548 = cp_parser_parenthesized_expression_list_elt (parser
, cast_p
,
8552 if (wrap_locations_p
)
8553 expr
.maybe_add_location_wrapper ();
8555 /* Add it to the list. We add error_mark_node
8556 expressions to the list, so that we can still tell if
8557 the correct form for a parenthesized expression-list
8558 is found. That gives better errors. */
8559 vec_safe_push (expression_list
, expr
.get_value ());
8561 if (expr
== error_mark_node
)
8565 /* After the first item, attribute lists look the same as
8566 expression lists. */
8567 is_attribute_list
= non_attr
;
8570 /* If the next token isn't a `,', then we are done. */
8571 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8574 /* Otherwise, consume the `,' and keep going. */
8575 cp_lexer_consume_token (parser
->lexer
);
8578 if (close_paren_loc
)
8579 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8581 if (!parens
.require_close (parser
))
8586 /* We try and resync to an unnested comma, as that will give the
8587 user better diagnostics. */
8588 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
8589 /*recovering=*/true,
8591 /*consume_paren=*/true);
8596 parser
->greater_than_is_operator_p
8597 = saved_greater_than_is_operator_p
;
8602 parser
->greater_than_is_operator_p
8603 = saved_greater_than_is_operator_p
;
8606 vec_safe_insert (expression_list
, 0, identifier
);
8608 return expression_list
;
8611 /* Parse a pseudo-destructor-name.
8613 pseudo-destructor-name:
8614 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8615 :: [opt] nested-name-specifier template template-id :: ~ type-name
8616 :: [opt] nested-name-specifier [opt] ~ type-name
8618 If either of the first two productions is used, sets *SCOPE to the
8619 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8620 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8621 or ERROR_MARK_NODE if the parse fails. */
8624 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
8629 bool nested_name_specifier_p
;
8632 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
8633 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
8634 && !type_dependent_expression_p (object
))
8636 if (cxx_dialect
< cxx14
)
8637 pedwarn (input_location
, OPT_Wc__14_extensions
,
8638 "%<~auto%> only available with "
8639 "%<-std=c++14%> or %<-std=gnu++14%>");
8640 cp_lexer_consume_token (parser
->lexer
);
8641 cp_lexer_consume_token (parser
->lexer
);
8643 *type
= TREE_TYPE (object
);
8647 /* Assume that things will not work out. */
8648 *type
= error_mark_node
;
8650 /* Look for the optional `::' operator. */
8651 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8652 /* Look for the optional nested-name-specifier. */
8653 nested_name_specifier_p
8654 = (cp_parser_nested_name_specifier_opt (parser
,
8655 /*typename_keyword_p=*/false,
8656 /*check_dependency_p=*/true,
8658 /*is_declaration=*/false)
8660 /* Now, if we saw a nested-name-specifier, we might be doing the
8661 second production. */
8662 if (nested_name_specifier_p
8663 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8665 /* Consume the `template' keyword. */
8666 cp_lexer_consume_token (parser
->lexer
);
8667 /* Parse the template-id. */
8668 cp_parser_template_id (parser
,
8669 /*template_keyword_p=*/true,
8670 /*check_dependency_p=*/false,
8672 /*is_declaration=*/true);
8673 /* Look for the `::' token. */
8674 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8676 /* If the next token is not a `~', then there might be some
8677 additional qualification. */
8678 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8680 /* At this point, we're looking for "type-name :: ~". The type-name
8681 must not be a class-name, since this is a pseudo-destructor. So,
8682 it must be either an enum-name, or a typedef-name -- both of which
8683 are just identifiers. So, we peek ahead to check that the "::"
8684 and "~" tokens are present; if they are not, then we can avoid
8685 calling type_name. */
8686 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8687 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8688 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8690 cp_parser_error (parser
, "non-scalar type");
8694 /* Look for the type-name. */
8695 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8696 if (*scope
== error_mark_node
)
8699 /* Look for the `::' token. */
8700 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8705 /* Look for the `~'. */
8706 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8708 /* Once we see the ~, this has to be a pseudo-destructor. */
8709 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8710 cp_parser_commit_to_topmost_tentative_parse (parser
);
8712 /* Look for the type-name again. We are not responsible for
8713 checking that it matches the first type-name. */
8714 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8717 /* Parse a unary-expression.
8724 unary-operator cast-expression
8725 sizeof unary-expression
8727 alignof ( type-id ) [C++0x]
8734 __extension__ cast-expression
8735 __alignof__ unary-expression
8736 __alignof__ ( type-id )
8737 alignof unary-expression [C++0x]
8738 __real__ cast-expression
8739 __imag__ cast-expression
8741 sizeof ( type-id ) { initializer-list , [opt] }
8742 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8743 __alignof__ ( type-id ) { initializer-list , [opt] }
8745 ADDRESS_P is true iff the unary-expression is appearing as the
8746 operand of the `&' operator. CAST_P is true if this expression is
8747 the target of a cast.
8749 Returns a representation of the expression. */
8752 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8753 bool address_p
, bool cast_p
, bool decltype_p
)
8756 enum tree_code unary_operator
;
8758 /* Peek at the next token. */
8759 token
= cp_lexer_peek_token (parser
->lexer
);
8760 /* Some keywords give away the kind of expression. */
8761 if (token
->type
== CPP_KEYWORD
)
8763 enum rid keyword
= token
->keyword
;
8772 location_t start_loc
= token
->location
;
8774 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8775 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8777 /* Consume the token. */
8778 cp_lexer_consume_token (parser
->lexer
);
8779 /* Parse the operand. */
8780 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8782 /* Construct a location e.g. :
8785 with start == caret at the start of the "alignof"/"sizeof"
8786 token, with the endpoint at the final closing paren. */
8787 location_t compound_loc
8788 = make_location (start_loc
, start_loc
, parser
->lexer
);
8790 if (TYPE_P (operand
))
8791 ret
= cxx_sizeof_or_alignof_type (compound_loc
, operand
, op
,
8795 /* ISO C++ defines alignof only with types, not with
8796 expressions. So pedwarn if alignof is used with a non-
8797 type expression. However, __alignof__ is ok. */
8799 pedwarn (token
->location
, OPT_Wpedantic
,
8800 "ISO C++ does not allow %<alignof%> "
8803 ret
= cxx_sizeof_or_alignof_expr (compound_loc
, operand
, op
,
8806 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8807 SIZEOF_EXPR with the original operand. */
8808 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8810 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8812 if (!processing_template_decl
&& TYPE_P (operand
))
8814 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8815 build1 (NOP_EXPR
, operand
,
8817 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8820 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8821 TREE_SIDE_EFFECTS (ret
) = 0;
8822 TREE_READONLY (ret
) = 1;
8823 SET_EXPR_LOCATION (ret
, compound_loc
);
8827 cp_expr
ret_expr (ret
, compound_loc
);
8828 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8832 case RID_BUILTIN_HAS_ATTRIBUTE
:
8833 return cp_parser_has_attribute_expression (parser
);
8836 return cp_parser_new_expression (parser
);
8839 return cp_parser_delete_expression (parser
);
8843 /* The saved value of the PEDANTIC flag. */
8847 /* Save away the PEDANTIC flag. */
8848 cp_parser_extension_opt (parser
, &saved_pedantic
);
8849 /* Parse the cast-expression. */
8850 expr
= cp_parser_simple_cast_expression (parser
);
8851 /* Restore the PEDANTIC flag. */
8852 pedantic
= saved_pedantic
;
8862 /* Consume the `__real__' or `__imag__' token. */
8863 cp_lexer_consume_token (parser
->lexer
);
8864 /* Parse the cast-expression. */
8865 expression
= cp_parser_simple_cast_expression (parser
);
8866 /* Create the complete representation. */
8867 return build_x_unary_op (token
->location
,
8868 (keyword
== RID_REALPART
8869 ? REALPART_EXPR
: IMAGPART_EXPR
),
8870 expression
, NULL_TREE
,
8871 tf_warning_or_error
);
8875 case RID_TRANSACTION_ATOMIC
:
8876 case RID_TRANSACTION_RELAXED
:
8877 return cp_parser_transaction_expression (parser
, keyword
);
8882 const char *saved_message
;
8883 bool saved_integral_constant_expression_p
;
8884 bool saved_non_integral_constant_expression_p
;
8885 bool saved_greater_than_is_operator_p
;
8887 location_t start_loc
= token
->location
;
8889 cp_lexer_consume_token (parser
->lexer
);
8890 matching_parens parens
;
8891 parens
.require_open (parser
);
8893 saved_message
= parser
->type_definition_forbidden_message
;
8894 parser
->type_definition_forbidden_message
8895 = G_("types may not be defined in %<noexcept%> expressions");
8897 saved_integral_constant_expression_p
8898 = parser
->integral_constant_expression_p
;
8899 saved_non_integral_constant_expression_p
8900 = parser
->non_integral_constant_expression_p
;
8901 parser
->integral_constant_expression_p
= false;
8903 saved_greater_than_is_operator_p
8904 = parser
->greater_than_is_operator_p
;
8905 parser
->greater_than_is_operator_p
= true;
8907 ++cp_unevaluated_operand
;
8908 ++c_inhibit_evaluation_warnings
;
8909 ++cp_noexcept_operand
;
8910 expr
= cp_parser_expression (parser
);
8911 --cp_noexcept_operand
;
8912 --c_inhibit_evaluation_warnings
;
8913 --cp_unevaluated_operand
;
8915 parser
->greater_than_is_operator_p
8916 = saved_greater_than_is_operator_p
;
8918 parser
->integral_constant_expression_p
8919 = saved_integral_constant_expression_p
;
8920 parser
->non_integral_constant_expression_p
8921 = saved_non_integral_constant_expression_p
;
8923 parser
->type_definition_forbidden_message
= saved_message
;
8925 parens
.require_close (parser
);
8927 /* Construct a location of the form:
8930 with start == caret, finishing at the close-paren. */
8931 location_t noexcept_loc
8932 = make_location (start_loc
, start_loc
, parser
->lexer
);
8934 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8941 location_t kw_loc
= token
->location
;
8943 /* Consume the `co_await' token. */
8944 cp_lexer_consume_token (parser
->lexer
);
8945 /* Parse its cast-expression. */
8946 expr
= cp_parser_simple_cast_expression (parser
);
8947 if (expr
== error_mark_node
)
8948 return error_mark_node
;
8950 /* Handle [expr.await]. */
8951 return cp_expr (finish_co_await_expr (kw_loc
, expr
));
8959 /* Look for the `:: new' and `:: delete', which also signal the
8960 beginning of a new-expression, or delete-expression,
8961 respectively. If the next token is `::', then it might be one of
8963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8967 /* See if the token after the `::' is one of the keywords in
8968 which we're interested. */
8969 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8970 /* If it's `new', we have a new-expression. */
8971 if (keyword
== RID_NEW
)
8972 return cp_parser_new_expression (parser
);
8973 /* Similarly, for `delete'. */
8974 else if (keyword
== RID_DELETE
)
8975 return cp_parser_delete_expression (parser
);
8978 /* Look for a unary operator. */
8979 unary_operator
= cp_parser_unary_operator (token
);
8980 /* The `++' and `--' operators can be handled similarly, even though
8981 they are not technically unary-operators in the grammar. */
8982 if (unary_operator
== ERROR_MARK
)
8984 if (token
->type
== CPP_PLUS_PLUS
)
8985 unary_operator
= PREINCREMENT_EXPR
;
8986 else if (token
->type
== CPP_MINUS_MINUS
)
8987 unary_operator
= PREDECREMENT_EXPR
;
8988 /* Handle the GNU address-of-label extension. */
8989 else if (cp_parser_allow_gnu_extensions_p (parser
)
8990 && token
->type
== CPP_AND_AND
)
8994 location_t start_loc
= token
->location
;
8996 /* Consume the '&&' token. */
8997 cp_lexer_consume_token (parser
->lexer
);
8998 /* Look for the identifier. */
8999 identifier
= cp_parser_identifier (parser
);
9000 /* Construct a location of the form:
9003 with caret==start at the "&&", finish at the end of the label. */
9004 location_t combined_loc
9005 = make_location (start_loc
, start_loc
, parser
->lexer
);
9006 /* Create an expression representing the address. */
9007 expression
= finish_label_address_expr (identifier
, combined_loc
);
9008 if (cp_parser_non_integral_constant_expression (parser
,
9010 expression
= error_mark_node
;
9014 if (unary_operator
!= ERROR_MARK
)
9016 cp_expr cast_expression
;
9017 cp_expr expression
= error_mark_node
;
9018 non_integral_constant non_constant_p
= NIC_NONE
;
9019 location_t loc
= token
->location
;
9020 tsubst_flags_t complain
= complain_flags (decltype_p
);
9022 /* Consume the operator token. */
9023 token
= cp_lexer_consume_token (parser
->lexer
);
9024 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
9026 /* Parse the cast-expression. */
9028 = cp_parser_cast_expression (parser
,
9029 unary_operator
== ADDR_EXPR
,
9035 OP_TOKEN CAST_EXPRESSION
9036 ^~~~~~~~~~~~~~~~~~~~~~~~~
9037 with start==caret at the operator token, and
9038 extending to the end of the cast_expression. */
9039 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
9041 /* Now, build an appropriate representation. */
9042 switch (unary_operator
)
9045 non_constant_p
= NIC_STAR
;
9046 expression
= build_x_indirect_ref (loc
, cast_expression
,
9047 RO_UNARY_STAR
, NULL_TREE
,
9049 /* TODO: build_x_indirect_ref does not always honor the
9050 location, so ensure it is set. */
9051 expression
.set_location (loc
);
9055 non_constant_p
= NIC_ADDR
;
9058 expression
= build_x_unary_op (loc
, unary_operator
,
9060 NULL_TREE
, complain
);
9061 /* TODO: build_x_unary_op does not always honor the location,
9062 so ensure it is set. */
9063 expression
.set_location (loc
);
9066 case PREINCREMENT_EXPR
:
9067 case PREDECREMENT_EXPR
:
9068 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
9069 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
9072 /* Immediately fold negation of a constant, unless the constant is 0
9073 (since -0 == 0) or it would overflow. */
9074 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
9077 = tree_strip_any_location_wrapper (cast_expression
);
9078 if (CONSTANT_CLASS_P (stripped_expr
)
9079 && !integer_zerop (stripped_expr
)
9080 && !TREE_OVERFLOW (stripped_expr
))
9082 tree folded
= fold_build1 (unary_operator
,
9083 TREE_TYPE (stripped_expr
),
9085 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
9087 expression
= maybe_wrap_with_location (folded
, loc
);
9093 case UNARY_PLUS_EXPR
:
9094 case TRUTH_NOT_EXPR
:
9095 expression
= finish_unary_op_expr (loc
, unary_operator
,
9096 cast_expression
, complain
);
9103 if (non_constant_p
!= NIC_NONE
9104 && cp_parser_non_integral_constant_expression (parser
,
9106 expression
= error_mark_node
;
9111 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
9112 /*member_access_only_p=*/false,
9117 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9118 unary-operator, the corresponding tree code is returned. */
9120 static enum tree_code
9121 cp_parser_unary_operator (cp_token
* token
)
9123 switch (token
->type
)
9126 return INDIRECT_REF
;
9132 return UNARY_PLUS_EXPR
;
9138 return TRUTH_NOT_EXPR
;
9141 return BIT_NOT_EXPR
;
9148 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9149 Returns a representation of the expression. */
9152 cp_parser_has_attribute_expression (cp_parser
*parser
)
9154 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9156 /* Consume the __builtin_has_attribute token. */
9157 cp_lexer_consume_token (parser
->lexer
);
9159 matching_parens parens
;
9160 if (!parens
.require_open (parser
))
9161 return error_mark_node
;
9163 /* Types cannot be defined in a `sizeof' expression. Save away the
9165 const char *saved_message
= parser
->type_definition_forbidden_message
;
9166 const char *saved_message_arg
9167 = parser
->type_definition_forbidden_message_arg
;
9168 parser
->type_definition_forbidden_message
9169 = G_("types may not be defined in %qs expressions");
9170 parser
->type_definition_forbidden_message_arg
9171 = IDENTIFIER_POINTER (ridpointers
[RID_BUILTIN_HAS_ATTRIBUTE
]);
9173 /* The restrictions on constant-expressions do not apply inside
9174 sizeof expressions. */
9175 bool saved_integral_constant_expression_p
9176 = parser
->integral_constant_expression_p
;
9177 bool saved_non_integral_constant_expression_p
9178 = parser
->non_integral_constant_expression_p
;
9179 parser
->integral_constant_expression_p
= false;
9181 /* Do not actually evaluate the expression. */
9182 ++cp_unevaluated_operand
;
9183 ++c_inhibit_evaluation_warnings
;
9185 tree oper
= NULL_TREE
;
9187 /* We can't be sure yet whether we're looking at a type-id or an
9189 cp_parser_parse_tentatively (parser
);
9191 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9192 parser
->in_type_id_in_expr_p
= true;
9193 /* Look for the type-id. */
9194 oper
= cp_parser_type_id (parser
);
9195 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9197 cp_parser_parse_definitely (parser
);
9199 /* If the type-id production did not work out, then we must be
9200 looking at an expression. */
9201 if (!oper
|| oper
== error_mark_node
)
9202 oper
= cp_parser_assignment_expression (parser
);
9204 STRIP_ANY_LOCATION_WRAPPER (oper
);
9206 /* Go back to evaluating expressions. */
9207 --cp_unevaluated_operand
;
9208 --c_inhibit_evaluation_warnings
;
9210 /* And restore the old one. */
9211 parser
->type_definition_forbidden_message
= saved_message
;
9212 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
9213 parser
->integral_constant_expression_p
9214 = saved_integral_constant_expression_p
;
9215 parser
->non_integral_constant_expression_p
9216 = saved_non_integral_constant_expression_p
;
9218 /* Consume the comma if it's there. */
9219 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
9221 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9222 /*consume_paren=*/true);
9223 return error_mark_node
;
9226 /* Parse the attribute specification. */
9228 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9229 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
9231 if (oper
== error_mark_node
)
9233 else if (processing_template_decl
&& uses_template_parms (oper
))
9234 sorry_at (atloc
, "%<__builtin_has_attribute%> with dependent argument "
9235 "not supported yet");
9238 /* Fold constant expressions used in attributes first. */
9239 cp_check_const_attributes (attr
);
9241 /* Finally, see if OPER has been declared with ATTR. */
9242 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
9245 parens
.require_close (parser
);
9249 error_at (atloc
, "expected identifier");
9250 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9253 /* Construct a location e.g. :
9254 __builtin_has_attribute (oper, attr)
9255 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9256 with start == caret at the start of the built-in token,
9257 and with the endpoint at the final closing paren. */
9258 location_t compound_loc
9259 = make_location (start_loc
, start_loc
, parser
->lexer
);
9261 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
9262 ret_expr
.set_location (compound_loc
);
9263 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
9267 /* Parse a new-expression.
9270 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9271 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9273 Returns a representation of the expression. */
9276 cp_parser_new_expression (cp_parser
* parser
)
9278 bool global_scope_p
;
9279 vec
<tree
, va_gc
> *placement
;
9281 vec
<tree
, va_gc
> *initializer
;
9282 tree nelts
= NULL_TREE
;
9285 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9287 /* Look for the optional `::' operator. */
9289 = (cp_parser_global_scope_opt (parser
,
9290 /*current_scope_valid_p=*/false)
9292 /* Look for the `new' operator. */
9293 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
9294 /* There's no easy way to tell a new-placement from the
9295 `( type-id )' construct. */
9296 cp_parser_parse_tentatively (parser
);
9297 /* Look for a new-placement. */
9298 placement
= cp_parser_new_placement (parser
);
9299 /* If that didn't work out, there's no new-placement. */
9300 if (!cp_parser_parse_definitely (parser
))
9302 if (placement
!= NULL
)
9303 release_tree_vector (placement
);
9307 /* If the next token is a `(', then we have a parenthesized
9309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9312 const char *saved_message
= parser
->type_definition_forbidden_message
;
9314 /* Consume the `('. */
9315 matching_parens parens
;
9316 parens
.consume_open (parser
);
9318 /* Parse the type-id. */
9319 parser
->type_definition_forbidden_message
9320 = G_("types may not be defined in a new-expression");
9322 type_id_in_expr_sentinel
s (parser
);
9323 type
= cp_parser_type_id (parser
);
9325 parser
->type_definition_forbidden_message
= saved_message
;
9327 /* Look for the closing `)'. */
9328 parens
.require_close (parser
);
9329 token
= cp_lexer_peek_token (parser
->lexer
);
9330 /* There should not be a direct-new-declarator in this production,
9331 but GCC used to allowed this, so we check and emit a sensible error
9332 message for this case. */
9333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9335 error_at (token
->location
,
9336 "array bound forbidden after parenthesized type-id");
9337 inform (token
->location
,
9338 "try removing the parentheses around the type-id");
9339 cp_parser_direct_new_declarator (parser
);
9342 /* Otherwise, there must be a new-type-id. */
9344 type
= cp_parser_new_type_id (parser
, &nelts
);
9346 /* If the next token is a `(' or '{', then we have a new-initializer. */
9347 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9348 if (token
->type
== CPP_OPEN_PAREN
9349 || token
->type
== CPP_OPEN_BRACE
)
9350 initializer
= cp_parser_new_initializer (parser
);
9354 /* A new-expression may not appear in an integral constant
9356 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
9357 ret
= error_mark_node
;
9358 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9359 of a new-type-id or type-id of a new-expression, the new-expression shall
9360 contain a new-initializer of the form ( assignment-expression )".
9361 Additionally, consistently with the spirit of DR 1467, we want to accept
9362 'new auto { 2 }' too. */
9363 else if ((ret
= type_uses_auto (type
))
9364 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
9365 && (vec_safe_length (initializer
) != 1
9366 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
9367 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
9369 error_at (token
->location
,
9370 "initialization of new-expression for type %<auto%> "
9371 "requires exactly one element");
9372 ret
= error_mark_node
;
9376 /* Construct a location e.g.:
9379 with caret == start at the start of the "new" token, and the end
9380 at the end of the final token we consumed. */
9381 location_t combined_loc
= make_location (start_loc
, start_loc
,
9383 /* Create a representation of the new-expression. */
9384 ret
= build_new (combined_loc
, &placement
, type
, nelts
, &initializer
,
9385 global_scope_p
, tf_warning_or_error
);
9388 if (placement
!= NULL
)
9389 release_tree_vector (placement
);
9390 if (initializer
!= NULL
)
9391 release_tree_vector (initializer
);
9396 /* Parse a new-placement.
9401 Returns the same representation as for an expression-list. */
9403 static vec
<tree
, va_gc
> *
9404 cp_parser_new_placement (cp_parser
* parser
)
9406 vec
<tree
, va_gc
> *expression_list
;
9408 /* Parse the expression-list. */
9409 expression_list
= (cp_parser_parenthesized_expression_list
9410 (parser
, non_attr
, /*cast_p=*/false,
9411 /*allow_expansion_p=*/true,
9412 /*non_constant_p=*/NULL
));
9414 if (expression_list
&& expression_list
->is_empty ())
9415 error ("expected expression-list or type-id");
9417 return expression_list
;
9420 /* Parse a new-type-id.
9423 type-specifier-seq new-declarator [opt]
9425 Returns the TYPE allocated. If the new-type-id indicates an array
9426 type, *NELTS is set to the number of elements in the last array
9427 bound; the TYPE will not include the last array bound. */
9430 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
9432 cp_decl_specifier_seq type_specifier_seq
;
9433 cp_declarator
*new_declarator
;
9434 cp_declarator
*declarator
;
9435 cp_declarator
*outer_declarator
;
9436 const char *saved_message
;
9438 /* The type-specifier sequence must not contain type definitions.
9439 (It cannot contain declarations of new types either, but if they
9440 are not definitions we will catch that because they are not
9442 saved_message
= parser
->type_definition_forbidden_message
;
9443 parser
->type_definition_forbidden_message
9444 = G_("types may not be defined in a new-type-id");
9445 /* Parse the type-specifier-seq. */
9446 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
9447 /*is_declaration=*/false,
9448 /*is_trailing_return=*/false,
9449 &type_specifier_seq
);
9450 /* Restore the old message. */
9451 parser
->type_definition_forbidden_message
= saved_message
;
9453 if (type_specifier_seq
.type
== error_mark_node
)
9454 return error_mark_node
;
9456 /* Parse the new-declarator. */
9457 new_declarator
= cp_parser_new_declarator_opt (parser
);
9459 /* Determine the number of elements in the last array dimension, if
9462 /* Skip down to the last array dimension. */
9463 declarator
= new_declarator
;
9464 outer_declarator
= NULL
;
9465 while (declarator
&& (declarator
->kind
== cdk_pointer
9466 || declarator
->kind
== cdk_ptrmem
))
9468 outer_declarator
= declarator
;
9469 declarator
= declarator
->declarator
;
9472 && declarator
->kind
== cdk_array
9473 && declarator
->declarator
9474 && declarator
->declarator
->kind
== cdk_array
)
9476 outer_declarator
= declarator
;
9477 declarator
= declarator
->declarator
;
9480 if (declarator
&& declarator
->kind
== cdk_array
)
9482 *nelts
= declarator
->u
.array
.bounds
;
9483 if (*nelts
== error_mark_node
)
9484 *nelts
= integer_one_node
;
9486 if (*nelts
== NULL_TREE
)
9487 /* Leave [] in the declarator. */;
9488 else if (outer_declarator
)
9489 outer_declarator
->declarator
= declarator
->declarator
;
9491 new_declarator
= NULL
;
9494 return groktypename (&type_specifier_seq
, new_declarator
, false);
9497 /* Parse an (optional) new-declarator.
9500 ptr-operator new-declarator [opt]
9501 direct-new-declarator
9503 Returns the declarator. */
9505 static cp_declarator
*
9506 cp_parser_new_declarator_opt (cp_parser
* parser
)
9508 enum tree_code code
;
9509 tree type
, std_attributes
= NULL_TREE
;
9510 cp_cv_quals cv_quals
;
9512 /* We don't know if there's a ptr-operator next, or not. */
9513 cp_parser_parse_tentatively (parser
);
9514 /* Look for a ptr-operator. */
9515 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
9516 /* If that worked, look for more new-declarators. */
9517 if (cp_parser_parse_definitely (parser
))
9519 cp_declarator
*declarator
;
9521 /* Parse another optional declarator. */
9522 declarator
= cp_parser_new_declarator_opt (parser
);
9524 declarator
= cp_parser_make_indirect_declarator
9525 (code
, type
, cv_quals
, declarator
, std_attributes
);
9530 /* If the next token is a `[', there is a direct-new-declarator. */
9531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9532 return cp_parser_direct_new_declarator (parser
);
9537 /* Parse a direct-new-declarator.
9539 direct-new-declarator:
9541 direct-new-declarator [constant-expression]
9545 static cp_declarator
*
9546 cp_parser_direct_new_declarator (cp_parser
* parser
)
9548 cp_declarator
*declarator
= NULL
;
9549 bool first_p
= true;
9556 /* Look for the opening `['. */
9557 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9559 token
= cp_lexer_peek_token (parser
->lexer
);
9560 if (token
->type
== CPP_CLOSE_SQUARE
&& first_p
)
9561 expression
= NULL_TREE
;
9563 expression
= cp_parser_expression (parser
);
9564 /* The standard requires that the expression have integral
9565 type. DR 74 adds enumeration types. We believe that the
9566 real intent is that these expressions be handled like the
9567 expression in a `switch' condition, which also allows
9568 classes with a single conversion to integral or
9569 enumeration type. */
9570 if (expression
&& !processing_template_decl
)
9573 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
9578 error_at (token
->location
,
9579 "expression in new-declarator must have integral "
9580 "or enumeration type");
9581 expression
= error_mark_node
;
9585 /* Look for the closing `]'. */
9586 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9588 /* Add this bound to the declarator. */
9589 declarator
= make_array_declarator (declarator
, expression
);
9591 /* If the next token is not a `[', then there are no more
9593 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
9601 /* Parse a new-initializer.
9604 ( expression-list [opt] )
9607 Returns a representation of the expression-list. */
9609 static vec
<tree
, va_gc
> *
9610 cp_parser_new_initializer (cp_parser
* parser
)
9612 vec
<tree
, va_gc
> *expression_list
;
9614 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9617 bool expr_non_constant_p
;
9618 cp_lexer_set_source_position (parser
->lexer
);
9619 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9620 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9621 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
9622 expression_list
= make_tree_vector_single (t
);
9625 expression_list
= (cp_parser_parenthesized_expression_list
9626 (parser
, non_attr
, /*cast_p=*/false,
9627 /*allow_expansion_p=*/true,
9628 /*non_constant_p=*/NULL
));
9630 return expression_list
;
9633 /* Parse a delete-expression.
9636 :: [opt] delete cast-expression
9637 :: [opt] delete [ ] cast-expression
9639 Returns a representation of the expression. */
9642 cp_parser_delete_expression (cp_parser
* parser
)
9644 bool global_scope_p
;
9647 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9649 /* Look for the optional `::' operator. */
9651 = (cp_parser_global_scope_opt (parser
,
9652 /*current_scope_valid_p=*/false)
9654 /* Look for the `delete' keyword. */
9655 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
9656 /* See if the array syntax is in use. */
9657 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9659 /* Consume the `[' token. */
9660 cp_lexer_consume_token (parser
->lexer
);
9661 /* Look for the `]' token. */
9662 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9663 /* Remember that this is the `[]' construct. */
9669 /* Parse the cast-expression. */
9670 expression
= cp_parser_simple_cast_expression (parser
);
9672 /* A delete-expression may not appear in an integral constant
9674 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9675 return error_mark_node
;
9677 /* Construct a location e.g.:
9680 with caret == start at the start of the "delete" token, and
9681 the end at the end of the final token we consumed. */
9682 location_t combined_loc
= make_location (start_loc
, start_loc
,
9684 expression
= delete_sanity (combined_loc
, expression
, NULL_TREE
, array_p
,
9685 global_scope_p
, tf_warning_or_error
);
9690 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9691 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9695 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9697 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9698 switch (token
->type
)
9704 case CPP_CLOSE_SQUARE
:
9705 case CPP_CLOSE_PAREN
:
9706 case CPP_CLOSE_BRACE
:
9707 case CPP_OPEN_BRACE
:
9711 case CPP_DEREF_STAR
:
9719 case CPP_GREATER_EQ
:
9740 case CPP_OPEN_PAREN
:
9741 /* In ((type ()) () the last () isn't a valid cast-expression,
9742 so the whole must be parsed as postfix-expression. */
9743 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9746 case CPP_OPEN_SQUARE
:
9747 /* '[' may start a primary-expression in obj-c++ and in C++11,
9748 as a lambda-expression, eg, '(void)[]{}'. */
9749 if (cxx_dialect
>= cxx11
)
9751 return c_dialect_objc ();
9754 case CPP_MINUS_MINUS
:
9755 /* '++' and '--' may or may not start a cast-expression:
9757 struct T { void operator++(int); };
9758 void f() { (T())++; }
9771 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9772 in the order: const_cast, static_cast, reinterpret_cast.
9774 Don't suggest dynamic_cast.
9776 Return the first legal cast kind found, or NULL otherwise. */
9779 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9783 /* Reuse the parser logic by attempting to build the various kinds of
9784 cast, with "complain" disabled.
9785 Identify the first such cast that is valid. */
9787 /* Don't attempt to run such logic within template processing. */
9788 if (processing_template_decl
)
9791 /* First try const_cast. */
9792 trial
= build_const_cast (input_location
, dst_type
, orig_expr
, tf_none
);
9793 if (trial
!= error_mark_node
)
9794 return "const_cast";
9796 /* If that fails, try static_cast. */
9797 trial
= build_static_cast (input_location
, dst_type
, orig_expr
, tf_none
);
9798 if (trial
!= error_mark_node
)
9799 return "static_cast";
9801 /* Finally, try reinterpret_cast. */
9802 trial
= build_reinterpret_cast (input_location
, dst_type
, orig_expr
,
9804 if (trial
!= error_mark_node
)
9805 return "reinterpret_cast";
9807 /* No such cast possible. */
9811 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9812 suggesting how to convert a C-style cast of the form:
9816 to a C++-style cast.
9818 The primary range of RICHLOC is asssumed to be that of the original
9819 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9820 of the parens in the C-style cast. */
9823 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9824 location_t close_paren_loc
, tree orig_expr
,
9827 /* This function is non-trivial, so bail out now if the warning isn't
9828 going to be emitted. */
9829 if (!warn_old_style_cast
)
9832 /* Try to find a legal C++ cast, trying them in order:
9833 const_cast, static_cast, reinterpret_cast. */
9834 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9835 if (!cast_suggestion
)
9838 /* Replace the open paren with "CAST_SUGGESTION<". */
9840 pp_string (&pp
, cast_suggestion
);
9842 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9844 /* Replace the close paren with "> (". */
9845 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9847 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9848 rich_loc
->add_fixit_insert_after (")");
9852 /* Parse a cast-expression.
9856 ( type-id ) cast-expression
9858 ADDRESS_P is true iff the unary-expression is appearing as the
9859 operand of the `&' operator. CAST_P is true if this expression is
9860 the target of a cast.
9862 Returns a representation of the expression. */
9865 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9866 bool decltype_p
, cp_id_kind
* pidk
)
9868 /* If it's a `(', then we might be looking at a cast. */
9869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9871 tree type
= NULL_TREE
;
9872 cp_expr
expr (NULL_TREE
);
9873 int cast_expression
= 0;
9874 const char *saved_message
;
9876 /* There's no way to know yet whether or not this is a cast.
9877 For example, `(int (3))' is a unary-expression, while `(int)
9878 3' is a cast. So, we resort to parsing tentatively. */
9879 cp_parser_parse_tentatively (parser
);
9880 /* Types may not be defined in a cast. */
9881 saved_message
= parser
->type_definition_forbidden_message
;
9882 parser
->type_definition_forbidden_message
9883 = G_("types may not be defined in casts");
9884 /* Consume the `('. */
9885 matching_parens parens
;
9886 cp_token
*open_paren
= parens
.consume_open (parser
);
9887 location_t open_paren_loc
= open_paren
->location
;
9888 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9890 /* A very tricky bit is that `(struct S) { 3 }' is a
9891 compound-literal (which we permit in C++ as an extension).
9892 But, that construct is not a cast-expression -- it is a
9893 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9894 is legal; if the compound-literal were a cast-expression,
9895 you'd need an extra set of parentheses.) But, if we parse
9896 the type-id, and it happens to be a class-specifier, then we
9897 will commit to the parse at that point, because we cannot
9898 undo the action that is done when creating a new class. So,
9899 then we cannot back up and do a postfix-expression.
9901 Another tricky case is the following (c++/29234):
9903 struct S { void operator () (); };
9910 As a type-id we parse the parenthesized S()() as a function
9911 returning a function, groktypename complains and we cannot
9912 back up in this case either.
9914 Therefore, we scan ahead to the closing `)', and check to see
9915 if the tokens after the `)' can start a cast-expression. Otherwise
9916 we are dealing with an unary-expression, a postfix-expression
9919 Yet another tricky case, in C++11, is the following (c++/54891):
9923 The issue is that usually, besides the case of lambda-expressions,
9924 the parenthesized type-id cannot be followed by '[', and, eg, we
9925 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9926 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9927 we don't commit, we try a cast-expression, then an unary-expression.
9929 Save tokens so that we can put them back. */
9930 cp_lexer_save_tokens (parser
->lexer
);
9932 /* We may be looking at a cast-expression. */
9933 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9934 /*consume_paren=*/true))
9936 = cp_parser_tokens_start_cast_expression (parser
);
9938 /* Roll back the tokens we skipped. */
9939 cp_lexer_rollback_tokens (parser
->lexer
);
9940 /* If we aren't looking at a cast-expression, simulate an error so
9941 that the call to cp_parser_error_occurred below returns true. */
9942 if (!cast_expression
)
9943 cp_parser_simulate_error (parser
);
9946 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9947 parser
->in_type_id_in_expr_p
= true;
9948 /* Look for the type-id. */
9949 type
= cp_parser_type_id (parser
);
9950 /* Look for the closing `)'. */
9951 cp_token
*close_paren
= parens
.require_close (parser
);
9953 close_paren_loc
= close_paren
->location
;
9954 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9957 /* Restore the saved message. */
9958 parser
->type_definition_forbidden_message
= saved_message
;
9960 /* At this point this can only be either a cast or a
9961 parenthesized ctor such as `(T ())' that looks like a cast to
9962 function returning T. */
9963 if (!cp_parser_error_occurred (parser
))
9965 /* Only commit if the cast-expression doesn't start with
9966 '++', '--', or '[' in C++11. */
9967 if (cast_expression
> 0)
9968 cp_parser_commit_to_topmost_tentative_parse (parser
);
9970 expr
= cp_parser_cast_expression (parser
,
9971 /*address_p=*/false,
9973 /*decltype_p=*/false,
9976 if (cp_parser_parse_definitely (parser
))
9978 /* Warn about old-style casts, if so requested. */
9979 if (warn_old_style_cast
9980 && !in_system_header_at (input_location
)
9981 && !VOID_TYPE_P (type
)
9982 && current_lang_name
!= lang_name_c
)
9984 gcc_rich_location
rich_loc (input_location
);
9985 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9987 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9988 "use of old-style cast to %q#T", type
);
9991 /* Only type conversions to integral or enumeration types
9992 can be used in constant-expressions. */
9993 if (!cast_valid_in_integral_constant_expression_p (type
)
9994 && cp_parser_non_integral_constant_expression (parser
,
9996 return error_mark_node
;
9998 /* Perform the cast. */
10002 with start==caret at the open paren, extending to the
10004 location_t cast_loc
= make_location (open_paren_loc
,
10006 expr
.get_finish ());
10007 expr
= build_c_cast (cast_loc
, type
, expr
);
10012 cp_parser_abort_tentative_parse (parser
);
10015 /* If we get here, then it's not a cast, so it must be a
10016 unary-expression. */
10017 return cp_parser_unary_expression (parser
, pidk
, address_p
,
10018 cast_p
, decltype_p
);
10021 /* Parse a binary expression of the general form:
10025 pm-expression .* cast-expression
10026 pm-expression ->* cast-expression
10028 multiplicative-expression:
10030 multiplicative-expression * pm-expression
10031 multiplicative-expression / pm-expression
10032 multiplicative-expression % pm-expression
10034 additive-expression:
10035 multiplicative-expression
10036 additive-expression + multiplicative-expression
10037 additive-expression - multiplicative-expression
10040 additive-expression
10041 shift-expression << additive-expression
10042 shift-expression >> additive-expression
10044 relational-expression:
10046 relational-expression < shift-expression
10047 relational-expression > shift-expression
10048 relational-expression <= shift-expression
10049 relational-expression >= shift-expression
10053 relational-expression:
10054 relational-expression <? shift-expression
10055 relational-expression >? shift-expression
10057 equality-expression:
10058 relational-expression
10059 equality-expression == relational-expression
10060 equality-expression != relational-expression
10063 equality-expression
10064 and-expression & equality-expression
10066 exclusive-or-expression:
10068 exclusive-or-expression ^ and-expression
10070 inclusive-or-expression:
10071 exclusive-or-expression
10072 inclusive-or-expression | exclusive-or-expression
10074 logical-and-expression:
10075 inclusive-or-expression
10076 logical-and-expression && inclusive-or-expression
10078 logical-or-expression:
10079 logical-and-expression
10080 logical-or-expression || logical-and-expression
10082 All these are implemented with a single function like:
10085 simple-cast-expression
10086 binary-expression <token> binary-expression
10088 CAST_P is true if this expression is the target of a cast.
10090 The binops_by_token map is used to get the tree codes for each <token> type.
10091 binary-expressions are associated according to a precedence table. */
10093 #define TOKEN_PRECEDENCE(token) \
10094 (((token->type == CPP_GREATER \
10095 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10096 && !parser->greater_than_is_operator_p) \
10097 ? PREC_NOT_OPERATOR \
10098 : binops_by_token[token->type].prec)
10101 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
10102 bool no_toplevel_fold_p
,
10104 enum cp_parser_prec prec
,
10107 cp_parser_expression_stack stack
;
10108 cp_parser_expression_stack_entry
*sp
= &stack
[0];
10109 cp_parser_expression_stack_entry
*disable_warnings_sp
= NULL
;
10110 cp_parser_expression_stack_entry current
;
10113 enum tree_code rhs_type
;
10114 enum cp_parser_prec new_prec
, lookahead_prec
;
10117 /* Parse the first expression. */
10118 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
10119 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
10120 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
10121 cast_p
, decltype_p
, pidk
);
10122 current
.prec
= prec
;
10124 if (cp_parser_error_occurred (parser
))
10125 return error_mark_node
;
10129 /* Get an operator token. */
10130 token
= cp_lexer_peek_token (parser
->lexer
);
10132 if (warn_cxx11_compat
10133 && token
->type
== CPP_RSHIFT
10134 && !parser
->greater_than_is_operator_p
)
10136 if (warning_at (token
->location
, OPT_Wc__11_compat
,
10137 "%<>>%> operator is treated"
10138 " as two right angle brackets in C++11"))
10139 inform (token
->location
,
10140 "suggest parentheses around %<>>%> expression");
10143 new_prec
= TOKEN_PRECEDENCE (token
);
10144 if (new_prec
!= PREC_NOT_OPERATOR
10145 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10146 /* This is a fold-expression; handle it later. */
10147 new_prec
= PREC_NOT_OPERATOR
;
10149 /* Popping an entry off the stack means we completed a subexpression:
10150 - either we found a token which is not an operator (`>' where it is not
10151 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10152 will happen repeatedly;
10153 - or, we found an operator which has lower priority. This is the case
10154 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10155 parsing `3 * 4'. */
10156 if (new_prec
<= current
.prec
)
10165 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
10166 current
.loc
= token
->location
;
10167 current
.flags
= token
->flags
;
10169 /* We used the operator token. */
10170 cp_lexer_consume_token (parser
->lexer
);
10172 /* For "false && x" or "true || x", x will never be executed;
10173 disable warnings while evaluating it. */
10174 if ((current
.tree_type
== TRUTH_ANDIF_EXPR
10175 && cp_fully_fold (current
.lhs
) == truthvalue_false_node
)
10176 || (current
.tree_type
== TRUTH_ORIF_EXPR
10177 && cp_fully_fold (current
.lhs
) == truthvalue_true_node
))
10179 disable_warnings_sp
= sp
;
10180 ++c_inhibit_evaluation_warnings
;
10183 /* Extract another operand. It may be the RHS of this expression
10184 or the LHS of a new, higher priority expression. */
10185 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
10186 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
10187 rhs
= cp_parser_simple_cast_expression (parser
);
10189 /* Get another operator token. Look up its precedence to avoid
10190 building a useless (immediately popped) stack entry for common
10191 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10192 token
= cp_lexer_peek_token (parser
->lexer
);
10193 lookahead_prec
= TOKEN_PRECEDENCE (token
);
10194 if (lookahead_prec
!= PREC_NOT_OPERATOR
10195 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10196 lookahead_prec
= PREC_NOT_OPERATOR
;
10197 if (lookahead_prec
> new_prec
)
10199 /* ... and prepare to parse the RHS of the new, higher priority
10200 expression. Since precedence levels on the stack are
10201 monotonically increasing, we do not have to care about
10202 stack overflows. */
10206 current
.lhs_type
= rhs_type
;
10207 current
.prec
= new_prec
;
10208 new_prec
= lookahead_prec
;
10212 lookahead_prec
= new_prec
;
10213 /* If the stack is not empty, we have parsed into LHS the right side
10214 (`4' in the example above) of an expression we had suspended.
10215 We can use the information on the stack to recover the LHS (`3')
10216 from the stack together with the tree code (`MULT_EXPR'), and
10217 the precedence of the higher level subexpression
10218 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10219 which will be used to actually build the additive expression. */
10221 rhs_type
= current
.lhs_type
;
10226 /* Undo the disabling of warnings done above. */
10227 if (sp
== disable_warnings_sp
)
10229 disable_warnings_sp
= NULL
;
10230 --c_inhibit_evaluation_warnings
;
10233 if (warn_logical_not_paren
10234 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
10235 && current
.lhs_type
== TRUTH_NOT_EXPR
10236 /* Avoid warning for !!x == y. */
10237 && (TREE_CODE (current
.lhs
) != NE_EXPR
10238 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
10239 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
10240 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
10241 /* Avoid warning for !b == y where b is boolean. */
10242 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
10243 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
10244 != BOOLEAN_TYPE
))))
10245 /* Avoid warning for !!b == y where b is boolean. */
10246 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
10247 || TREE_TYPE (current
.lhs
) == NULL_TREE
10248 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
10249 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
10250 current
.lhs
, maybe_constant_value (rhs
));
10252 if (warn_xor_used_as_pow
10253 && current
.tree_type
== BIT_XOR_EXPR
10254 /* Don't warn for named "xor" (as opposed to '^'). */
10255 && !(current
.flags
& NAMED_OP
)
10256 && current
.lhs
.decimal_p ()
10257 && rhs
.decimal_p ())
10258 check_for_xor_used_as_pow
10259 (current
.lhs
.get_location (),
10260 tree_strip_any_location_wrapper (current
.lhs
),
10262 tree_strip_any_location_wrapper (rhs
));
10266 location_t combined_loc
= make_location (current
.loc
,
10267 current
.lhs
.get_start (),
10268 rhs
.get_finish ());
10270 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10271 ERROR_MARK for everything that is not a binary expression.
10272 This makes warn_about_parentheses miss some warnings that
10273 involve unary operators. For unary expressions we should
10274 pass the correct tree_code unless the unary expression was
10275 surrounded by parentheses.
10277 if (no_toplevel_fold_p
10278 && lookahead_prec
<= current
.prec
10281 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
10282 current
.lhs
= error_mark_node
;
10285 current
.lhs
.maybe_add_location_wrapper ();
10286 rhs
.maybe_add_location_wrapper ();
10288 = build_min (current
.tree_type
,
10289 TREE_CODE_CLASS (current
.tree_type
)
10291 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
10292 current
.lhs
.get_value (), rhs
.get_value ());
10293 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
10298 op_location_t
op_loc (current
.loc
, combined_loc
);
10299 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
10300 current
.lhs
, current
.lhs_type
,
10301 rhs
, rhs_type
, NULL_TREE
, &overload
,
10302 complain_flags (decltype_p
));
10303 /* TODO: build_x_binary_op doesn't always honor the location. */
10304 current
.lhs
.set_location (combined_loc
);
10306 current
.lhs_type
= current
.tree_type
;
10308 /* If the binary operator required the use of an overloaded operator,
10309 then this expression cannot be an integral constant-expression.
10310 An overloaded operator can be used even if both operands are
10311 otherwise permissible in an integral constant-expression if at
10312 least one of the operands is of enumeration type. */
10315 && cp_parser_non_integral_constant_expression (parser
,
10317 return error_mark_node
;
10320 return current
.lhs
;
10324 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
10325 bool no_toplevel_fold_p
,
10326 enum cp_parser_prec prec
,
10329 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
10330 /*decltype*/false, prec
, pidk
);
10333 /* Parse the `? expression : assignment-expression' part of a
10334 conditional-expression. The LOGICAL_OR_EXPR is the
10335 logical-or-expression that started the conditional-expression.
10336 Returns a representation of the entire conditional-expression.
10338 This routine is used by cp_parser_assignment_expression.
10340 ? expression : assignment-expression
10344 ? : assignment-expression */
10347 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
10349 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
10350 cp_expr assignment_expr
;
10351 struct cp_token
*token
;
10352 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10354 /* Consume the `?' token. */
10355 cp_lexer_consume_token (parser
->lexer
);
10356 token
= cp_lexer_peek_token (parser
->lexer
);
10357 if (cp_parser_allow_gnu_extensions_p (parser
)
10358 && token
->type
== CPP_COLON
)
10360 pedwarn (token
->location
, OPT_Wpedantic
,
10361 "ISO C++ does not allow %<?:%> with omitted middle operand");
10362 /* Implicit true clause. */
10364 c_inhibit_evaluation_warnings
+=
10365 folded_logical_or_expr
== truthvalue_true_node
;
10366 warn_for_omitted_condop (token
->location
, logical_or_expr
);
10370 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10371 parser
->colon_corrects_to_scope_p
= false;
10372 /* Parse the expression. */
10373 c_inhibit_evaluation_warnings
+=
10374 folded_logical_or_expr
== truthvalue_false_node
;
10375 expr
= cp_parser_expression (parser
);
10376 c_inhibit_evaluation_warnings
+=
10377 ((folded_logical_or_expr
== truthvalue_true_node
)
10378 - (folded_logical_or_expr
== truthvalue_false_node
));
10379 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10382 /* The next token should be a `:'. */
10383 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
10384 /* Parse the assignment-expression. */
10385 assignment_expr
= cp_parser_assignment_expression (parser
);
10386 c_inhibit_evaluation_warnings
-=
10387 folded_logical_or_expr
== truthvalue_true_node
;
10389 /* Make a location:
10390 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10391 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10392 with the caret at the "?", ranging from the start of
10393 the logical_or_expr to the end of the assignment_expr. */
10394 loc
= make_location (loc
,
10395 logical_or_expr
.get_start (),
10396 assignment_expr
.get_finish ());
10398 /* Build the conditional-expression. */
10399 return build_x_conditional_expr (loc
, logical_or_expr
,
10402 tf_warning_or_error
);
10405 /* Parse an assignment-expression.
10407 assignment-expression:
10408 conditional-expression
10409 logical-or-expression assignment-operator assignment_expression
10413 CAST_P is true if this expression is the target of a cast.
10414 DECLTYPE_P is true if this expression is the operand of decltype.
10416 Returns a representation for the expression. */
10419 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
10420 bool cast_p
, bool decltype_p
)
10424 /* If the next token is the `throw' keyword, then we're looking at
10425 a throw-expression. */
10426 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
10427 expr
= cp_parser_throw_expression (parser
);
10428 /* If the next token is the `co_yield' keyword, then we're looking at
10429 a yield-expression. */
10430 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CO_YIELD
))
10431 expr
= cp_parser_yield_expression (parser
);
10432 /* Otherwise, it must be that we are looking at a
10433 logical-or-expression. */
10436 /* Parse the binary expressions (logical-or-expression). */
10437 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
10439 PREC_NOT_OPERATOR
, pidk
);
10440 /* If the next token is a `?' then we're actually looking at a
10441 conditional-expression. */
10442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10443 return cp_parser_question_colon_clause (parser
, expr
);
10446 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10448 /* If it's an assignment-operator, we're using the second
10450 enum tree_code assignment_operator
10451 = cp_parser_assignment_operator_opt (parser
);
10452 if (assignment_operator
!= ERROR_MARK
)
10454 bool non_constant_p
;
10456 /* Parse the right-hand side of the assignment. */
10457 cp_expr rhs
= cp_parser_initializer_clause (parser
,
10460 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
10461 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10463 /* An assignment may not appear in a
10464 constant-expression. */
10465 if (cp_parser_non_integral_constant_expression (parser
,
10467 return error_mark_node
;
10468 /* Build the assignment expression. Its default
10472 is the location of the '=' token as the
10473 caret, ranging from the start of the lhs to the
10475 loc
= make_location (loc
,
10477 rhs
.get_finish ());
10478 expr
= build_x_modify_expr (loc
, expr
,
10479 assignment_operator
,
10481 complain_flags (decltype_p
));
10482 /* TODO: build_x_modify_expr doesn't honor the location,
10483 so we must set it here. */
10484 expr
.set_location (loc
);
10492 /* Parse an (optional) assignment-operator.
10494 assignment-operator: one of
10495 = *= /= %= += -= >>= <<= &= ^= |=
10499 assignment-operator: one of
10502 If the next token is an assignment operator, the corresponding tree
10503 code is returned, and the token is consumed. For example, for
10504 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10505 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10506 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10507 operator, ERROR_MARK is returned. */
10509 static enum tree_code
10510 cp_parser_assignment_operator_opt (cp_parser
* parser
)
10515 /* Peek at the next token. */
10516 token
= cp_lexer_peek_token (parser
->lexer
);
10518 switch (token
->type
)
10529 op
= TRUNC_DIV_EXPR
;
10533 op
= TRUNC_MOD_EXPR
;
10544 case CPP_RSHIFT_EQ
:
10548 case CPP_LSHIFT_EQ
:
10565 /* Nothing else is an assignment operator. */
10569 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10570 if (op
!= ERROR_MARK
10571 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10574 /* If it was an assignment operator, consume it. */
10575 if (op
!= ERROR_MARK
)
10576 cp_lexer_consume_token (parser
->lexer
);
10581 /* Parse an expression.
10584 assignment-expression
10585 expression , assignment-expression
10587 CAST_P is true if this expression is the target of a cast.
10588 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10589 except possibly parenthesized or on the RHS of a comma (N3276).
10590 WARN_COMMA_P is true if a comma should be diagnosed.
10592 Returns a representation of the expression. */
10595 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
10596 bool cast_p
, bool decltype_p
, bool warn_comma_p
)
10598 cp_expr expression
= NULL_TREE
;
10599 location_t loc
= UNKNOWN_LOCATION
;
10603 cp_expr assignment_expression
;
10605 /* Parse the next assignment-expression. */
10606 assignment_expression
10607 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
10609 /* We don't create a temporary for a call that is the immediate operand
10610 of decltype or on the RHS of a comma. But when we see a comma, we
10611 need to create a temporary for a call on the LHS. */
10612 if (decltype_p
&& !processing_template_decl
10613 && TREE_CODE (assignment_expression
) == CALL_EXPR
10614 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
10615 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10616 assignment_expression
10617 = build_cplus_new (TREE_TYPE (assignment_expression
),
10618 assignment_expression
, tf_warning_or_error
);
10620 /* If this is the first assignment-expression, we can just
10623 expression
= assignment_expression
;
10626 /* Create a location with caret at the comma, ranging
10627 from the start of the LHS to the end of the RHS. */
10628 loc
= make_location (loc
,
10629 expression
.get_start (),
10630 assignment_expression
.get_finish ());
10631 expression
= build_x_compound_expr (loc
, expression
,
10632 assignment_expression
, NULL_TREE
,
10633 complain_flags (decltype_p
));
10634 expression
.set_location (loc
);
10636 /* If the next token is not a comma, or we're in a fold-expression, then
10637 we are done with the expression. */
10638 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
10639 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10641 /* Consume the `,'. */
10642 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10645 /* [depr.comma.subscript]: A comma expression appearing as
10646 the expr-or-braced-init-list of a subscripting expression
10647 is deprecated. A parenthesized comma expression is not
10649 warning_at (loc
, OPT_Wcomma_subscript
,
10650 "top-level comma expression in array subscript "
10652 warn_comma_p
= false;
10654 cp_lexer_consume_token (parser
->lexer
);
10655 /* A comma operator cannot appear in a constant-expression. */
10656 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
10657 expression
= error_mark_node
;
10663 /* Parse a constant-expression.
10665 constant-expression:
10666 conditional-expression
10668 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10669 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10670 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10671 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10672 greater than 1, this isn't really a constant-expression, only a
10673 potentially constant-evaluated expression. If STRICT_P is true,
10674 only parse a conditional-expression, otherwise parse an
10675 assignment-expression. See below for rationale. */
10678 cp_parser_constant_expression (cp_parser
* parser
,
10679 int allow_non_constant_p
,
10680 bool *non_constant_p
,
10683 bool saved_integral_constant_expression_p
;
10684 bool saved_allow_non_integral_constant_expression_p
;
10685 bool saved_non_integral_constant_expression_p
;
10686 cp_expr expression
;
10688 /* It might seem that we could simply parse the
10689 conditional-expression, and then check to see if it were
10690 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10691 one that the compiler can figure out is constant, possibly after
10692 doing some simplifications or optimizations. The standard has a
10693 precise definition of constant-expression, and we must honor
10694 that, even though it is somewhat more restrictive.
10700 is not a legal declaration, because `(2, 3)' is not a
10701 constant-expression. The `,' operator is forbidden in a
10702 constant-expression. However, GCC's constant-folding machinery
10703 will fold this operation to an INTEGER_CST for `3'. */
10705 /* Save the old settings. */
10706 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
10707 saved_allow_non_integral_constant_expression_p
10708 = parser
->allow_non_integral_constant_expression_p
;
10709 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
10710 /* We are now parsing a constant-expression. */
10711 parser
->integral_constant_expression_p
= true;
10712 parser
->allow_non_integral_constant_expression_p
10713 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
10714 parser
->non_integral_constant_expression_p
= false;
10716 /* A manifestly constant-evaluated expression is evaluated even in an
10717 unevaluated operand. */
10718 cp_evaluated
ev (/*reset if*/allow_non_constant_p
<= 1);
10720 /* Although the grammar says "conditional-expression", when not STRICT_P,
10721 we parse an "assignment-expression", which also permits
10722 "throw-expression" and the use of assignment operators. In the case
10723 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10724 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10725 actually essential that we look for an assignment-expression.
10726 For example, cp_parser_initializer_clauses uses this function to
10727 determine whether a particular assignment-expression is in fact
10731 /* Parse the binary expressions (logical-or-expression). */
10732 expression
= cp_parser_binary_expression (parser
, false, false, false,
10733 PREC_NOT_OPERATOR
, NULL
);
10734 /* If the next token is a `?' then we're actually looking at
10735 a conditional-expression; otherwise we're done. */
10736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10737 expression
= cp_parser_question_colon_clause (parser
, expression
);
10740 expression
= cp_parser_assignment_expression (parser
);
10741 /* Restore the old settings. */
10742 parser
->integral_constant_expression_p
10743 = saved_integral_constant_expression_p
;
10744 parser
->allow_non_integral_constant_expression_p
10745 = saved_allow_non_integral_constant_expression_p
;
10746 if (cxx_dialect
>= cxx11
)
10748 /* Require an rvalue constant expression here; that's what our
10749 callers expect. Reference constant expressions are handled
10750 separately in e.g. cp_parser_template_argument. */
10751 tree decay
= expression
;
10752 if (TREE_TYPE (expression
)
10753 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10754 decay
= build_address (expression
);
10755 bool is_const
= is_rvalue_constant_expression (decay
);
10756 parser
->non_integral_constant_expression_p
= !is_const
;
10757 if (!is_const
&& !allow_non_constant_p
)
10758 require_rvalue_constant_expression (decay
);
10760 if (allow_non_constant_p
)
10761 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10762 parser
->non_integral_constant_expression_p
10763 = saved_non_integral_constant_expression_p
;
10768 /* Parse __builtin_offsetof.
10770 offsetof-expression:
10771 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10773 offsetof-member-designator:
10775 | offsetof-member-designator "." id-expression
10776 | offsetof-member-designator "[" expression "]"
10777 | offsetof-member-designator "->" id-expression */
10780 cp_parser_builtin_offsetof (cp_parser
*parser
)
10782 int save_ice_p
, save_non_ice_p
;
10787 location_t finish_loc
;
10789 /* We're about to accept non-integral-constant things, but will
10790 definitely yield an integral constant expression. Save and
10791 restore these values around our local parsing. */
10792 save_ice_p
= parser
->integral_constant_expression_p
;
10793 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10795 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10797 /* Consume the "__builtin_offsetof" token. */
10798 cp_lexer_consume_token (parser
->lexer
);
10799 /* Consume the opening `('. */
10800 matching_parens parens
;
10801 parens
.require_open (parser
);
10802 /* Parse the type-id. */
10803 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10805 const char *saved_message
= parser
->type_definition_forbidden_message
;
10806 parser
->type_definition_forbidden_message
10807 = G_("types may not be defined within %<__builtin_offsetof%>");
10808 type
= cp_parser_type_id (parser
);
10809 parser
->type_definition_forbidden_message
= saved_message
;
10811 /* Look for the `,'. */
10812 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10813 token
= cp_lexer_peek_token (parser
->lexer
);
10815 /* Build the (type *)null that begins the traditional offsetof macro. */
10817 = build_static_cast (input_location
, build_pointer_type (type
),
10818 null_pointer_node
, tf_warning_or_error
);
10820 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10821 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10822 true, &dummy
, token
->location
);
10825 token
= cp_lexer_peek_token (parser
->lexer
);
10826 switch (token
->type
)
10828 case CPP_OPEN_SQUARE
:
10829 /* offsetof-member-designator "[" expression "]" */
10830 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10835 /* offsetof-member-designator "->" identifier */
10836 expr
= grok_array_decl (token
->location
, expr
, integer_zero_node
,
10837 NULL
, tf_warning_or_error
);
10841 /* offsetof-member-designator "." identifier */
10842 cp_lexer_consume_token (parser
->lexer
);
10843 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10844 expr
, true, &dummy
,
10848 case CPP_CLOSE_PAREN
:
10849 /* Consume the ")" token. */
10850 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10851 cp_lexer_consume_token (parser
->lexer
);
10855 /* Error. We know the following require will fail, but
10856 that gives the proper error message. */
10857 parens
.require_close (parser
);
10858 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10859 expr
= error_mark_node
;
10865 /* Make a location of the form:
10866 __builtin_offsetof (struct s, f)
10867 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10868 with caret at the type-id, ranging from the start of the
10869 "_builtin_offsetof" token to the close paren. */
10870 loc
= make_location (loc
, start_loc
, finish_loc
);
10871 /* The result will be an INTEGER_CST, so we need to explicitly
10872 preserve the location. */
10873 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10876 parser
->integral_constant_expression_p
= save_ice_p
;
10877 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10879 expr
= expr
.maybe_add_location_wrapper ();
10883 /* Parse a trait expression.
10885 Returns a representation of the expression, the underlying type
10886 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10889 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10891 cp_trait_kind kind
;
10892 tree type1
, type2
= NULL_TREE
;
10893 bool binary
= false;
10894 bool variadic
= false;
10898 case RID_HAS_NOTHROW_ASSIGN
:
10899 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10901 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10902 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10904 case RID_HAS_NOTHROW_COPY
:
10905 kind
= CPTK_HAS_NOTHROW_COPY
;
10907 case RID_HAS_TRIVIAL_ASSIGN
:
10908 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10910 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10911 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10913 case RID_HAS_TRIVIAL_COPY
:
10914 kind
= CPTK_HAS_TRIVIAL_COPY
;
10916 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10917 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10919 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10920 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10922 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10923 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10925 case RID_IS_ABSTRACT
:
10926 kind
= CPTK_IS_ABSTRACT
;
10928 case RID_IS_AGGREGATE
:
10929 kind
= CPTK_IS_AGGREGATE
;
10931 case RID_IS_BASE_OF
:
10932 kind
= CPTK_IS_BASE_OF
;
10936 kind
= CPTK_IS_CLASS
;
10939 kind
= CPTK_IS_EMPTY
;
10942 kind
= CPTK_IS_ENUM
;
10945 kind
= CPTK_IS_FINAL
;
10947 case RID_IS_LAYOUT_COMPATIBLE
:
10948 kind
= CPTK_IS_LAYOUT_COMPATIBLE
;
10951 case RID_IS_LITERAL_TYPE
:
10952 kind
= CPTK_IS_LITERAL_TYPE
;
10954 case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF
:
10955 kind
= CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF
;
10959 kind
= CPTK_IS_POD
;
10961 case RID_IS_POLYMORPHIC
:
10962 kind
= CPTK_IS_POLYMORPHIC
;
10964 case RID_IS_SAME_AS
:
10965 kind
= CPTK_IS_SAME_AS
;
10968 case RID_IS_STD_LAYOUT
:
10969 kind
= CPTK_IS_STD_LAYOUT
;
10971 case RID_IS_TRIVIAL
:
10972 kind
= CPTK_IS_TRIVIAL
;
10974 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10975 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10978 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10979 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10982 case RID_IS_TRIVIALLY_COPYABLE
:
10983 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10986 kind
= CPTK_IS_UNION
;
10988 case RID_UNDERLYING_TYPE
:
10989 kind
= CPTK_UNDERLYING_TYPE
;
10994 case RID_DIRECT_BASES
:
10995 kind
= CPTK_DIRECT_BASES
;
10997 case RID_IS_ASSIGNABLE
:
10998 kind
= CPTK_IS_ASSIGNABLE
;
11001 case RID_IS_CONSTRUCTIBLE
:
11002 kind
= CPTK_IS_CONSTRUCTIBLE
;
11005 case RID_IS_NOTHROW_ASSIGNABLE
:
11006 kind
= CPTK_IS_NOTHROW_ASSIGNABLE
;
11009 case RID_IS_NOTHROW_CONSTRUCTIBLE
:
11010 kind
= CPTK_IS_NOTHROW_CONSTRUCTIBLE
;
11013 case RID_IS_CONVERTIBLE
:
11014 kind
= CPTK_IS_CONVERTIBLE
;
11017 case RID_IS_NOTHROW_CONVERTIBLE
:
11018 kind
= CPTK_IS_NOTHROW_CONVERTIBLE
;
11021 case RID_REF_CONSTRUCTS_FROM_TEMPORARY
:
11022 kind
= CPTK_REF_CONSTRUCTS_FROM_TEMPORARY
;
11025 case RID_REF_CONVERTS_FROM_TEMPORARY
:
11026 kind
= CPTK_REF_CONVERTS_FROM_TEMPORARY
;
11030 gcc_unreachable ();
11033 /* Get location of initial token. */
11034 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11036 /* Consume the token. */
11037 cp_lexer_consume_token (parser
->lexer
);
11039 matching_parens parens
;
11040 parens
.require_open (parser
);
11043 type_id_in_expr_sentinel
s (parser
);
11044 type1
= cp_parser_type_id (parser
);
11047 if (type1
== error_mark_node
)
11048 return error_mark_node
;
11052 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
11055 type_id_in_expr_sentinel
s (parser
);
11056 type2
= cp_parser_type_id (parser
);
11059 if (type2
== error_mark_node
)
11060 return error_mark_node
;
11064 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11066 cp_lexer_consume_token (parser
->lexer
);
11067 tree elt
= cp_parser_type_id (parser
);
11068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11070 cp_lexer_consume_token (parser
->lexer
);
11071 elt
= make_pack_expansion (elt
);
11073 if (elt
== error_mark_node
)
11074 return error_mark_node
;
11075 type2
= tree_cons (NULL_TREE
, elt
, type2
);
11077 type2
= nreverse (type2
);
11080 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11081 parens
.require_close (parser
);
11083 /* Construct a location of the form:
11084 __is_trivially_copyable(_Tp)
11085 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
11086 with start == caret, finishing at the close-paren. */
11087 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
11089 /* Complete the trait expression, which may mean either processing
11090 the trait expr now or saving it for template instantiation. */
11093 case CPTK_UNDERLYING_TYPE
:
11094 return cp_expr (finish_underlying_type (type1
), trait_loc
);
11096 return cp_expr (finish_bases (type1
, false), trait_loc
);
11097 case CPTK_DIRECT_BASES
:
11098 return cp_expr (finish_bases (type1
, true), trait_loc
);
11100 return finish_trait_expr (trait_loc
, kind
, type1
, type2
);
11104 /* Parse a lambda expression.
11107 lambda-introducer lambda-declarator [opt] compound-statement
11108 lambda-introducer < template-parameter-list > requires-clause [opt]
11109 lambda-declarator [opt] compound-statement
11111 Returns a representation of the expression. */
11114 cp_parser_lambda_expression (cp_parser
* parser
)
11116 tree lambda_expr
= build_lambda_expr ();
11119 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11120 cp_token_position start
= 0;
11122 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
11124 if (cxx_dialect
>= cxx20
)
11126 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11127 non-type parameter is nonsensical.
11129 Distinguish a lambda in the parameter type from a lambda in the
11130 default argument by looking at local_variables_forbidden_p, which is
11131 only set in default arguments. */
11132 if (processing_template_parmlist
&& !parser
->local_variables_forbidden_p
)
11134 error_at (token
->location
,
11135 "lambda-expression in template parameter type");
11136 token
->error_reported
= true;
11140 else if (cp_unevaluated_operand
)
11142 if (!token
->error_reported
)
11144 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
11145 "lambda-expression in unevaluated context"
11146 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11147 token
->error_reported
= true;
11151 else if (parser
->in_template_argument_list_p
|| processing_template_parmlist
)
11153 if (!token
->error_reported
)
11155 error_at (token
->location
, "lambda-expression in template-argument"
11156 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11157 token
->error_reported
= true;
11162 /* We may be in the middle of deferred access check. Disable
11164 push_deferring_access_checks (dk_no_deferred
);
11166 cp_parser_lambda_introducer (parser
, lambda_expr
);
11167 if (cp_parser_error_occurred (parser
))
11168 return error_mark_node
;
11170 type
= begin_lambda_type (lambda_expr
);
11171 if (type
== error_mark_node
)
11172 return error_mark_node
;
11174 record_lambda_scope (lambda_expr
);
11176 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11177 determine_visibility (TYPE_NAME (type
));
11179 /* Now that we've started the type, add the capture fields for any
11180 explicit captures. */
11181 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
11184 /* Inside the class, surrounding template-parameter-lists do not apply. */
11185 unsigned int saved_num_template_parameter_lists
11186 = parser
->num_template_parameter_lists
;
11187 unsigned char in_statement
= parser
->in_statement
;
11188 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
11189 bool fully_implicit_function_template_p
11190 = parser
->fully_implicit_function_template_p
;
11191 tree implicit_template_parms
= parser
->implicit_template_parms
;
11192 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
11193 bool auto_is_implicit_function_template_parm_p
11194 = parser
->auto_is_implicit_function_template_parm_p
;
11196 parser
->num_template_parameter_lists
= 0;
11197 parser
->in_statement
= 0;
11198 parser
->in_switch_statement_p
= false;
11199 parser
->fully_implicit_function_template_p
= false;
11200 parser
->implicit_template_parms
= 0;
11201 parser
->implicit_template_scope
= 0;
11202 parser
->auto_is_implicit_function_template_parm_p
= false;
11204 /* The body of a lambda in a discarded statement is not discarded. */
11205 bool discarded
= in_discarded_stmt
;
11206 in_discarded_stmt
= 0;
11208 /* Similarly the body of a lambda in immediate function context is not
11209 in immediate function context. */
11210 bool save_in_consteval_if_p
= in_consteval_if_p
;
11211 in_consteval_if_p
= false;
11213 /* By virtue of defining a local class, a lambda expression has access to
11214 the private variables of enclosing classes. */
11216 if (cp_parser_start_tentative_firewall (parser
))
11219 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
11221 if (ok
&& cp_parser_error_occurred (parser
))
11226 cp_parser_lambda_body (parser
, lambda_expr
);
11228 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
11230 if (cp_parser_skip_to_closing_brace (parser
))
11231 cp_lexer_consume_token (parser
->lexer
);
11234 /* The capture list was built up in reverse order; fix that now. */
11235 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
11236 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
11239 maybe_add_lambda_conv_op (type
);
11241 finish_struct (type
, /*attributes=*/NULL_TREE
);
11243 in_consteval_if_p
= save_in_consteval_if_p
;
11244 in_discarded_stmt
= discarded
;
11246 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
11247 parser
->in_statement
= in_statement
;
11248 parser
->in_switch_statement_p
= in_switch_statement_p
;
11249 parser
->fully_implicit_function_template_p
11250 = fully_implicit_function_template_p
;
11251 parser
->implicit_template_parms
= implicit_template_parms
;
11252 parser
->implicit_template_scope
= implicit_template_scope
;
11253 parser
->auto_is_implicit_function_template_parm_p
11254 = auto_is_implicit_function_template_parm_p
;
11257 /* This field is only used during parsing of the lambda. */
11258 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
11260 /* This lambda shouldn't have any proxies left at this point. */
11261 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
11262 /* And now that we're done, push proxies for an enclosing lambda. */
11263 insert_pending_capture_proxies ();
11265 /* Update the lambda expression to a range. */
11266 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
11271 lambda_expr
= build_lambda_object (lambda_expr
);
11273 lambda_expr
= error_mark_node
;
11275 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
11277 pop_deferring_access_checks ();
11279 return lambda_expr
;
11282 /* Parse the beginning of a lambda expression.
11285 [ lambda-capture [opt] ]
11287 LAMBDA_EXPR is the current representation of the lambda expression. */
11290 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
11292 /* Need commas after the first capture. */
11295 /* Eat the leading `['. */
11296 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
11298 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
11300 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
)
11301 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
)
11302 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
11303 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
11304 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11305 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
11307 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
11309 cp_lexer_consume_token (parser
->lexer
);
11312 if (!(at_function_scope_p () || parsing_nsdmi ()))
11313 error ("non-local lambda expression cannot have a capture-default");
11316 hash_set
<tree
, true> ids
;
11317 tree first_capture_id
= NULL_TREE
;
11318 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
11320 cp_token
* capture_token
;
11322 tree capture_init_expr
;
11323 cp_id_kind idk
= CP_ID_KIND_NONE
;
11324 bool explicit_init_p
= false;
11326 enum capture_kind_type
11331 enum capture_kind_type capture_kind
= BY_COPY
;
11333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
11335 error ("expected end of capture-list");
11342 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
11344 /* Possibly capture `this'. */
11345 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
11347 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11348 if (cxx_dialect
< cxx20
&& pedantic
11349 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
11350 pedwarn (loc
, OPT_Wc__20_extensions
,
11351 "explicit by-copy capture of %<this%> "
11352 "with by-copy capture default only available with "
11353 "%<-std=c++20%> or %<-std=gnu++20%>");
11354 cp_lexer_consume_token (parser
->lexer
);
11355 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
11356 pedwarn (input_location
, 0,
11357 "already captured %qD in lambda expression",
11360 add_capture (lambda_expr
, /*id=*/this_identifier
,
11361 /*initializer=*/finish_this_expr (),
11362 /*by_reference_p=*/true, explicit_init_p
);
11366 /* Possibly capture `*this'. */
11367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
11368 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
11370 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11371 if (cxx_dialect
< cxx17
)
11372 pedwarn (loc
, OPT_Wc__17_extensions
,
11373 "%<*this%> capture only available with "
11374 "%<-std=c++17%> or %<-std=gnu++17%>");
11375 cp_lexer_consume_token (parser
->lexer
);
11376 cp_lexer_consume_token (parser
->lexer
);
11377 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
11378 pedwarn (input_location
, 0,
11379 "already captured %qD in lambda expression",
11382 add_capture (lambda_expr
, /*id=*/this_identifier
,
11383 /*initializer=*/finish_this_expr (),
11384 /*by_reference_p=*/false, explicit_init_p
);
11388 /* But reject `&this'. */
11389 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
11390 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
11392 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
11393 "%<this%> cannot be captured by reference");
11394 cp_lexer_consume_token (parser
->lexer
);
11395 cp_lexer_consume_token (parser
->lexer
);
11399 /* Remember whether we want to capture as a reference or not. */
11400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
11402 capture_kind
= BY_REFERENCE
;
11403 cp_lexer_consume_token (parser
->lexer
);
11406 bool init_pack_expansion
= false;
11407 location_t ellipsis_loc
= UNKNOWN_LOCATION
;
11408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11410 ellipsis_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11411 if (cxx_dialect
< cxx20
)
11412 pedwarn (ellipsis_loc
, OPT_Wc__20_extensions
,
11413 "pack init-capture only available with "
11414 "%<-std=c++20%> or %<-std=gnu++20%>");
11415 cp_lexer_consume_token (parser
->lexer
);
11416 init_pack_expansion
= true;
11419 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11420 if (init_pack_expansion
&& capture_kind
!= BY_REFERENCE
11421 && cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
11423 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
11424 0, "%<&%> should come before %<...%>");
11425 capture_kind
= BY_REFERENCE
;
11426 cp_lexer_consume_token (parser
->lexer
);
11429 /* Get the identifier. */
11430 capture_token
= cp_lexer_peek_token (parser
->lexer
);
11431 capture_id
= cp_parser_identifier (parser
);
11433 if (capture_id
== error_mark_node
)
11434 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11435 delimiters, but I modified this to stop on unnested ']' as well. It
11436 was already changed to stop on unnested '}', so the
11437 "closing_parenthesis" name is no more misleading with my change. */
11439 cp_parser_skip_to_closing_parenthesis (parser
,
11440 /*recovering=*/true,
11442 /*consume_paren=*/true);
11446 /* Find the initializer for this capture. */
11447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
11448 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
11449 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11451 bool direct
, non_constant
;
11452 /* An explicit initializer exists. */
11453 if (cxx_dialect
< cxx14
)
11454 pedwarn (input_location
, OPT_Wc__14_extensions
,
11455 "lambda capture initializers "
11456 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11457 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
11458 &non_constant
, true);
11459 explicit_init_p
= true;
11460 if (capture_init_expr
== NULL_TREE
)
11462 error ("empty initializer for lambda init-capture");
11463 capture_init_expr
= error_mark_node
;
11465 if (init_pack_expansion
)
11466 capture_init_expr
= make_pack_expansion (capture_init_expr
);
11470 const char* error_msg
;
11472 /* Turn the identifier into an id-expression. */
11474 = cp_parser_lookup_name_simple (parser
, capture_id
,
11475 capture_token
->location
);
11477 if (capture_init_expr
== error_mark_node
)
11479 unqualified_name_lookup_error (capture_id
);
11482 else if (!VAR_P (capture_init_expr
)
11483 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
11485 error_at (capture_token
->location
,
11486 "capture of non-variable %qE",
11487 capture_init_expr
);
11488 if (DECL_P (capture_init_expr
))
11489 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
11490 "%q#D declared here", capture_init_expr
);
11493 if (VAR_P (capture_init_expr
)
11494 && decl_storage_duration (capture_init_expr
) != dk_auto
)
11496 if (pedwarn (capture_token
->location
, 0, "capture of variable "
11497 "%qD with non-automatic storage duration",
11498 capture_init_expr
))
11499 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
11500 "%q#D declared here", capture_init_expr
);
11505 = finish_id_expression
11510 /*integral_constant_expression_p=*/false,
11511 /*allow_non_integral_constant_expression_p=*/false,
11512 /*non_integral_constant_expression_p=*/NULL
,
11513 /*template_p=*/false,
11515 /*address_p=*/false,
11516 /*template_arg_p=*/false,
11518 capture_token
->location
);
11520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11522 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11523 cp_lexer_consume_token (parser
->lexer
);
11524 capture_init_expr
= make_pack_expansion (capture_init_expr
);
11525 if (init_pack_expansion
)
11527 /* If what follows is an initializer, the second '...' is
11528 invalid. But for cases like [...xs...], the first one
11530 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
11531 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
11532 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11533 ellipsis_loc
= loc
;
11534 error_at (ellipsis_loc
, "too many %<...%> in lambda capture");
11540 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
11541 && !explicit_init_p
)
11543 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
11544 && capture_kind
== BY_COPY
)
11545 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
11546 "of %qD redundant with by-copy capture default",
11548 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
11549 && capture_kind
== BY_REFERENCE
)
11550 pedwarn (capture_token
->location
, 0, "explicit by-reference "
11551 "capture of %qD redundant with by-reference capture "
11552 "default", capture_id
);
11555 /* Check for duplicates.
11556 Optimize for the zero or one explicit captures cases and only create
11557 the hash_set after adding second capture. */
11558 bool found
= false;
11559 if (!ids
.is_empty ())
11560 found
= ids
.add (capture_id
);
11561 else if (first_capture_id
== NULL_TREE
)
11562 first_capture_id
= capture_id
;
11563 else if (capture_id
== first_capture_id
)
11567 ids
.add (first_capture_id
);
11568 ids
.add (capture_id
);
11571 pedwarn (input_location
, 0,
11572 "already captured %qD in lambda expression", capture_id
);
11574 add_capture (lambda_expr
, capture_id
, capture_init_expr
,
11575 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
11578 /* If there is any qualification still in effect, clear it
11579 now; we will be starting fresh with the next capture. */
11580 parser
->scope
= NULL_TREE
;
11581 parser
->qualifying_scope
= NULL_TREE
;
11582 parser
->object_scope
= NULL_TREE
;
11585 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
11588 /* Parse the (optional) middle of a lambda expression.
11591 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11592 lambda-specifiers (C++23)
11595 decl-specifier-seq [opt] noexcept-specifier [opt]
11596 attribute-specifier-seq [opt] trailing-return-type [opt]
11598 LAMBDA_EXPR is the current representation of the lambda expression. */
11601 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
11603 /* 5.1.1.4 of the standard says:
11604 If a lambda-expression does not include a lambda-declarator, it is as if
11605 the lambda-declarator were ().
11606 This means an empty parameter list, no attributes, and no exception
11608 tree param_list
= void_list_node
;
11609 tree std_attrs
= NULL_TREE
;
11610 tree gnu_attrs
= NULL_TREE
;
11611 tree exception_spec
= NULL_TREE
;
11612 tree template_param_list
= NULL_TREE
;
11613 tree tx_qual
= NULL_TREE
;
11614 tree return_type
= NULL_TREE
;
11615 tree trailing_requires_clause
= NULL_TREE
;
11616 bool has_param_list
= false;
11617 location_t omitted_parms_loc
= UNKNOWN_LOCATION
;
11618 cp_decl_specifier_seq lambda_specs
;
11619 clear_decl_specs (&lambda_specs
);
11620 /* A lambda op() is const unless explicitly 'mutable'. */
11621 cp_cv_quals quals
= TYPE_QUAL_CONST
;
11623 /* The template-parameter-list is optional, but must begin with
11624 an opening angle if present. */
11625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
11627 if (cxx_dialect
< cxx14
)
11628 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wc__14_extensions
,
11629 "lambda templates are only available with "
11630 "%<-std=c++14%> or %<-std=gnu++14%>");
11631 else if (pedantic
&& cxx_dialect
< cxx20
)
11632 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wc__20_extensions
,
11633 "lambda templates are only available with "
11634 "%<-std=c++20%> or %<-std=gnu++20%>");
11636 cp_lexer_consume_token (parser
->lexer
);
11638 template_param_list
= cp_parser_template_parameter_list (parser
);
11639 cp_parser_require_end_of_template_parameter_list (parser
);
11641 /* We may have a constrained generic lambda; parse the requires-clause
11642 immediately after the template-parameter-list and combine with any
11643 shorthand constraints present. */
11644 tree dreqs
= cp_parser_requires_clause_opt (parser
, true);
11647 tree reqs
= get_shorthand_constraints (current_template_parms
);
11649 reqs
= combine_constraint_expressions (reqs
, dreqs
);
11650 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
11653 /* We just processed one more parameter list. */
11654 ++parser
->num_template_parameter_lists
;
11657 /* Committee discussion supports allowing attributes here. */
11658 lambda_specs
.attributes
= cp_parser_attributes_opt (parser
);
11660 /* The parameter-declaration-clause is optional (unless
11661 template-parameter-list was given), but must begin with an
11662 opening parenthesis if present. */
11663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
11665 bool is_consteval
= false;
11666 /* For C++20, before parsing the parameter list check if there is
11667 a consteval specifier in the corresponding decl-specifier-seq. */
11668 if (cxx_dialect
>= cxx20
)
11670 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1);
11671 cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_KEYWORD
); n
++)
11673 if (cp_lexer_peek_nth_token (parser
->lexer
, n
)->keyword
11676 is_consteval
= true;
11682 matching_parens parens
;
11683 parens
.consume_open (parser
);
11685 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
11688 current_binding_level
->immediate_fn_ctx_p
= true;
11690 /* Parse parameters. */
11692 = cp_parser_parameter_declaration_clause
11693 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
11695 /* Default arguments shall not be specified in the
11696 parameter-declaration-clause of a lambda-declarator. */
11697 if (pedantic
&& cxx_dialect
< cxx14
)
11698 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
11699 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
11700 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
11701 OPT_Wc__14_extensions
,
11702 "default argument specified for lambda parameter");
11704 parens
.require_close (parser
);
11705 has_param_list
= true;
11707 else if (cxx_dialect
< cxx23
)
11708 omitted_parms_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11710 /* In the decl-specifier-seq of the lambda-declarator, each
11711 decl-specifier shall either be mutable or constexpr. */
11712 int declares_class_or_enum
;
11713 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
11714 cp_parser_decl_specifier_seq (parser
,
11715 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
11716 &lambda_specs
, &declares_class_or_enum
);
11718 if (omitted_parms_loc
&& lambda_specs
.any_specifiers_p
)
11720 pedwarn (omitted_parms_loc
, OPT_Wc__23_extensions
,
11721 "parameter declaration before lambda declaration "
11722 "specifiers only optional with %<-std=c++2b%> or "
11723 "%<-std=gnu++2b%>");
11724 omitted_parms_loc
= UNKNOWN_LOCATION
;
11727 if (lambda_specs
.storage_class
== sc_mutable
)
11729 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
11730 quals
= TYPE_UNQUALIFIED
;
11731 if (lambda_specs
.conflicting_specifiers_p
)
11732 error_at (lambda_specs
.locations
[ds_storage_class
],
11733 "duplicate %<mutable%>");
11736 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
11737 if (omitted_parms_loc
&& tx_qual
)
11739 pedwarn (omitted_parms_loc
, OPT_Wc__23_extensions
,
11740 "parameter declaration before lambda transaction "
11741 "qualifier only optional with %<-std=c++2b%> or "
11742 "%<-std=gnu++2b%>");
11743 omitted_parms_loc
= UNKNOWN_LOCATION
;
11746 /* Parse optional exception specification. */
11748 = cp_parser_exception_specification_opt (parser
, CP_PARSER_FLAGS_NONE
);
11750 if (omitted_parms_loc
&& exception_spec
)
11752 pedwarn (omitted_parms_loc
, OPT_Wc__23_extensions
,
11753 "parameter declaration before lambda exception "
11754 "specification only optional with %<-std=c++2b%> or "
11755 "%<-std=gnu++2b%>");
11756 omitted_parms_loc
= UNKNOWN_LOCATION
;
11759 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11760 attributes that appertain to the function type. */
11761 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
11762 gnu_attrs
= cp_parser_gnu_attributes_opt (parser
);
11764 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11766 /* Parse optional trailing return type. */
11767 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
11769 if (omitted_parms_loc
)
11770 pedwarn (omitted_parms_loc
, OPT_Wc__23_extensions
,
11771 "parameter declaration before lambda trailing "
11772 "return type only optional with %<-std=c++2b%> or "
11773 "%<-std=gnu++2b%>");
11774 cp_lexer_consume_token (parser
->lexer
);
11775 return_type
= cp_parser_trailing_type_id (parser
);
11778 /* Also allow GNU attributes at the very end of the declaration, the usual
11779 place for GNU attributes. */
11780 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
11781 gnu_attrs
= chainon (gnu_attrs
, cp_parser_gnu_attributes_opt (parser
));
11783 if (has_param_list
)
11785 /* Parse optional trailing requires clause. */
11786 trailing_requires_clause
= cp_parser_requires_clause_opt (parser
, false);
11788 /* The function parameters must be in scope all the way until after the
11789 trailing-return-type in case of decltype. */
11790 pop_bindings_and_leave_scope ();
11793 /* Create the function call operator.
11795 Messing with declarators like this is no uglier than building up the
11796 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11799 cp_decl_specifier_seq return_type_specs
;
11800 cp_declarator
* declarator
;
11804 clear_decl_specs (&return_type_specs
);
11805 return_type_specs
.type
= make_auto ();
11807 if (lambda_specs
.locations
[ds_constexpr
])
11809 if (cxx_dialect
>= cxx17
)
11810 return_type_specs
.locations
[ds_constexpr
]
11811 = lambda_specs
.locations
[ds_constexpr
];
11813 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
11814 "lambda only available with %<-std=c++17%> or "
11815 "%<-std=gnu++17%>");
11817 if (lambda_specs
.locations
[ds_consteval
])
11818 return_type_specs
.locations
[ds_consteval
]
11819 = lambda_specs
.locations
[ds_consteval
];
11821 p
= obstack_alloc (&declarator_obstack
, 0);
11823 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
11824 LAMBDA_EXPR_LOCATION (lambda_expr
));
11826 declarator
= make_call_declarator (declarator
, param_list
, quals
,
11827 VIRT_SPEC_UNSPECIFIED
,
11832 trailing_requires_clause
,
11834 declarator
->std_attributes
= std_attrs
;
11836 fco
= grokmethod (&return_type_specs
,
11838 chainon (gnu_attrs
, lambda_specs
.attributes
));
11839 if (fco
!= error_mark_node
)
11841 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
11842 DECL_ARTIFICIAL (fco
) = 1;
11843 /* Give the object parameter a different name. */
11844 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
11845 DECL_SET_LAMBDA_FUNCTION (fco
, true);
11847 if (template_param_list
)
11849 fco
= finish_member_template_decl (fco
);
11850 finish_template_decl (template_param_list
);
11851 --parser
->num_template_parameter_lists
;
11853 else if (parser
->fully_implicit_function_template_p
)
11854 fco
= finish_fully_implicit_template (parser
, fco
);
11856 finish_member_declaration (fco
);
11858 obstack_free (&declarator_obstack
, p
);
11860 return (fco
!= error_mark_node
);
11864 /* Parse the body of a lambda expression, which is simply
11868 but which requires special handling.
11869 LAMBDA_EXPR is the current representation of the lambda expression. */
11872 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
11874 bool nested
= (current_function_decl
!= NULL_TREE
);
11875 unsigned char local_variables_forbidden_p
11876 = parser
->local_variables_forbidden_p
;
11877 bool in_function_body
= parser
->in_function_body
;
11879 /* The body of a lambda-expression is not a subexpression of the enclosing
11884 push_function_context ();
11886 /* Still increment function_depth so that we don't GC in the
11887 middle of an expression. */
11890 auto odsd
= make_temp_override (parser
->omp_declare_simd
, NULL
);
11891 auto ord
= make_temp_override (parser
->oacc_routine
, NULL
);
11892 auto oafp
= make_temp_override (parser
->omp_attrs_forbidden_p
, false);
11893 vec
<tree
> omp_privatization_save
;
11894 save_omp_privatization_clauses (omp_privatization_save
);
11895 /* Clear this in case we're in the middle of a default argument. */
11896 parser
->local_variables_forbidden_p
= 0;
11897 parser
->in_function_body
= true;
11900 local_specialization_stack
s (lss_copy
);
11901 tree fco
= lambda_function (lambda_expr
);
11902 tree body
= start_lambda_function (fco
, lambda_expr
);
11904 /* Originally C++11 required us to peek for 'return expr'; and
11905 process it specially here to deduce the return type. N3638
11906 removed the need for that. */
11907 cp_parser_function_body (parser
, false);
11909 finish_lambda_function (body
);
11912 restore_omp_privatization_clauses (omp_privatization_save
);
11913 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
11914 parser
->in_function_body
= in_function_body
;
11916 pop_function_context();
11921 /* Statements [gram.stmt.stmt] */
11923 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11926 add_debug_begin_stmt (location_t loc
)
11928 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
11930 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11931 /* A concept is never expanded normally. */
11934 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11935 SET_EXPR_LOCATION (stmt
, loc
);
11939 struct cp_omp_attribute_data
11941 cp_token_cache
*tokens
;
11942 const c_omp_directive
*dir
;
11943 c_omp_directive_kind kind
;
11946 /* Handle omp::directive and omp::sequence attributes in ATTRS
11947 (if any) at the start of a statement or in attribute-declaration. */
11950 cp_parser_handle_statement_omp_attributes (cp_parser
*parser
, tree attrs
)
11952 if (!flag_openmp
&& !flag_openmp_simd
)
11955 auto_vec
<cp_omp_attribute_data
, 16> vec
;
11959 for (tree
*pa
= &attrs
; *pa
; )
11960 if (get_attribute_namespace (*pa
) == omp_identifier
11961 && is_attribute_p ("directive", get_attribute_name (*pa
)))
11964 for (tree a
= TREE_VALUE (*pa
); a
; a
= TREE_CHAIN (a
))
11966 tree d
= TREE_VALUE (a
);
11967 gcc_assert (TREE_CODE (d
) == DEFERRED_PARSE
);
11968 cp_token
*first
= DEFPARSE_TOKENS (d
)->first
;
11969 cp_token
*last
= DEFPARSE_TOKENS (d
)->last
;
11970 if (parser
->omp_attrs_forbidden_p
)
11972 error_at (first
->location
,
11973 "mixing OpenMP directives with attribute and pragma "
11974 "syntax on the same statement");
11975 parser
->omp_attrs_forbidden_p
= false;
11978 const char *directive
[3] = {};
11979 for (int i
= 0; i
< 3; i
++)
11981 tree id
= NULL_TREE
;
11982 if (first
+ i
== last
)
11984 if (first
[i
].type
== CPP_NAME
)
11985 id
= first
[i
].u
.value
;
11986 else if (first
[i
].type
== CPP_KEYWORD
)
11987 id
= ridpointers
[(int) first
[i
].keyword
];
11990 directive
[i
] = IDENTIFIER_POINTER (id
);
11992 const c_omp_directive
*dir
= NULL
;
11994 dir
= c_omp_categorize_directive (directive
[0], directive
[1],
11998 error_at (first
->location
,
11999 "unknown OpenMP directive name in %<omp::directive%>"
12000 " attribute argument");
12003 c_omp_directive_kind kind
= dir
->kind
;
12004 if (dir
->id
== PRAGMA_OMP_ORDERED
)
12006 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
12007 depend/doacross clause. */
12009 && (strcmp (directive
[1], "depend") == 0
12010 || strcmp (directive
[1], "doacross") == 0))
12011 kind
= C_OMP_DIR_STANDALONE
;
12012 else if (first
+ 2 < last
12013 && first
[1].type
== CPP_COMMA
12014 && first
[2].type
== CPP_NAME
12015 && (strcmp (IDENTIFIER_POINTER (first
[2].u
.value
),
12017 || strcmp (IDENTIFIER_POINTER (first
[2].u
.value
),
12019 kind
= C_OMP_DIR_STANDALONE
;
12021 else if (dir
->id
== PRAGMA_OMP_ERROR
)
12023 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
12024 int paren_depth
= 0;
12025 for (int i
= 1; first
+ i
< last
; i
++)
12026 if (first
[i
].type
== CPP_OPEN_PAREN
)
12028 else if (first
[i
].type
== CPP_CLOSE_PAREN
)
12030 else if (paren_depth
== 0
12031 && first
+ i
+ 2 < last
12032 && first
[i
].type
== CPP_NAME
12033 && first
[i
+ 1].type
== CPP_OPEN_PAREN
12034 && first
[i
+ 2].type
== CPP_NAME
12035 && !strcmp (IDENTIFIER_POINTER (first
[i
].u
.value
),
12037 && !strcmp (IDENTIFIER_POINTER (first
[i
12041 kind
= C_OMP_DIR_STANDALONE
;
12045 cp_omp_attribute_data v
= { DEFPARSE_TOKENS (d
), dir
, kind
};
12047 if (flag_openmp
|| dir
->simd
)
12048 tokens
+= (last
- first
) + 1;
12050 cp_omp_attribute_data v
= {};
12052 *pa
= TREE_CHAIN (*pa
);
12055 pa
= &TREE_CHAIN (*pa
);
12061 cp_omp_attribute_data
*v
;
12062 cp_omp_attribute_data
*construct_seen
= nullptr;
12063 cp_omp_attribute_data
*standalone_seen
= nullptr;
12064 cp_omp_attribute_data
*prev_standalone_seen
= nullptr;
12065 FOR_EACH_VEC_ELT (vec
, i
, v
)
12068 if (v
->kind
== C_OMP_DIR_CONSTRUCT
&& !construct_seen
)
12069 construct_seen
= v
;
12070 else if (v
->kind
== C_OMP_DIR_STANDALONE
&& !standalone_seen
)
12071 standalone_seen
= v
;
12075 if (standalone_seen
&& !prev_standalone_seen
)
12077 prev_standalone_seen
= standalone_seen
;
12078 standalone_seen
= nullptr;
12082 if (cnt
> 1 && construct_seen
)
12084 error_at (construct_seen
->tokens
->first
->location
,
12085 "OpenMP construct among %<omp::directive%> attributes"
12086 " requires all %<omp::directive%> attributes on the"
12087 " same statement to be in the same %<omp::sequence%>");
12090 if (cnt
> 1 && standalone_seen
&& prev_standalone_seen
)
12092 error_at (standalone_seen
->tokens
->first
->location
,
12093 "multiple OpenMP standalone directives among"
12094 " %<omp::directive%> attributes must be all within the"
12095 " same %<omp::sequence%>");
12099 if (prev_standalone_seen
)
12100 standalone_seen
= prev_standalone_seen
;
12101 if (standalone_seen
12102 && !cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12104 error_at (standalone_seen
->tokens
->first
->location
,
12105 "standalone OpenMP directives in %<omp::directive%> attribute"
12106 " can only appear on an empty statement");
12109 if (cnt
&& cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
12111 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12112 enum pragma_kind kind
= cp_parser_pragma_kind (token
);
12113 if (kind
>= PRAGMA_OMP__START_
&& kind
<= PRAGMA_OMP__LAST_
)
12115 error_at (token
->location
,
12116 "mixing OpenMP directives with attribute and pragma "
12117 "syntax on the same statement");
12125 cp_lexer
*lexer
= cp_lexer_alloc ();
12126 lexer
->debugging_p
= parser
->lexer
->debugging_p
;
12127 vec_safe_reserve (lexer
->buffer
, tokens
, true);
12128 FOR_EACH_VEC_ELT (vec
, i
, v
)
12132 if (!flag_openmp
&& !v
->dir
->simd
)
12134 cp_token
*first
= v
->tokens
->first
;
12135 cp_token
*last
= v
->tokens
->last
;
12137 tok
.type
= CPP_PRAGMA
;
12138 tok
.keyword
= RID_MAX
;
12139 tok
.u
.value
= build_int_cst (NULL
, v
->dir
->id
);
12140 tok
.location
= first
->location
;
12141 lexer
->buffer
->quick_push (tok
);
12142 while (++first
< last
)
12143 lexer
->buffer
->quick_push (*first
);
12145 tok
.type
= CPP_PRAGMA_EOL
;
12146 tok
.keyword
= RID_MAX
;
12147 tok
.location
= last
->location
;
12148 lexer
->buffer
->quick_push (tok
);
12151 tok
.type
= CPP_EOF
;
12152 tok
.keyword
= RID_MAX
;
12153 tok
.location
= lexer
->buffer
->last ().location
;
12154 lexer
->buffer
->quick_push (tok
);
12155 lexer
->next
= parser
->lexer
;
12156 lexer
->next_token
= lexer
->buffer
->address ();
12157 lexer
->last_token
= lexer
->next_token
12158 + lexer
->buffer
->length ()
12160 lexer
->in_omp_attribute_pragma
= true;
12161 parser
->lexer
= lexer
;
12162 /* Move the current source position to that of the first token in the
12164 cp_lexer_set_source_position_from_token (lexer
->next_token
);
12168 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12169 (if any) at the start or after declaration-id of a declaration. */
12172 cp_parser_handle_directive_omp_attributes (cp_parser
*parser
, tree
*pattrs
,
12173 cp_omp_declare_simd_data
*data
,
12176 if (!flag_openmp
&& !flag_openmp_simd
)
12181 bool variant_p
= false;
12182 location_t loc
= UNKNOWN_LOCATION
;
12183 for (tree pa
= *pattrs
; pa
; pa
= TREE_CHAIN (pa
))
12184 if (get_attribute_namespace (pa
) == omp_identifier
12185 && is_attribute_p ("directive", get_attribute_name (pa
)))
12187 for (tree a
= TREE_VALUE (pa
); a
; a
= TREE_CHAIN (a
))
12189 tree d
= TREE_VALUE (a
);
12190 gcc_assert (TREE_CODE (d
) == DEFERRED_PARSE
);
12191 cp_token
*first
= DEFPARSE_TOKENS (d
)->first
;
12192 cp_token
*last
= DEFPARSE_TOKENS (d
)->last
;
12193 const char *directive
[3] = {};
12194 for (int i
= 0; i
< 3; i
++)
12196 tree id
= NULL_TREE
;
12197 if (first
+ i
== last
)
12199 if (first
[i
].type
== CPP_NAME
)
12200 id
= first
[i
].u
.value
;
12201 else if (first
[i
].type
== CPP_KEYWORD
)
12202 id
= ridpointers
[(int) first
[i
].keyword
];
12205 directive
[i
] = IDENTIFIER_POINTER (id
);
12207 const c_omp_directive
*dir
= NULL
;
12209 dir
= c_omp_categorize_directive (directive
[0], directive
[1],
12213 if (dir
->id
== PRAGMA_OMP_DECLARE
12214 && (strcmp (directive
[1], "simd") == 0
12215 || strcmp (directive
[1], "variant") == 0))
12219 variant_p
= strcmp (directive
[1], "variant") == 0;
12220 loc
= first
->location
;
12222 if (start
&& parser
->omp_declare_simd
&& !bad
)
12224 error_at (first
->location
,
12225 "mixing OpenMP directives with attribute and "
12226 "pragma syntax on the same declaration");
12235 for (tree
*pa
= pattrs
; *pa
; )
12236 if (get_attribute_namespace (*pa
) == omp_identifier
12237 && is_attribute_p ("directive", get_attribute_name (*pa
)))
12238 *pa
= TREE_CHAIN (*pa
);
12240 pa
= &TREE_CHAIN (*pa
);
12246 if (parser
->omp_declare_simd
== NULL
)
12248 data
->error_seen
= false;
12249 data
->fndecl_seen
= false;
12250 data
->variant_p
= variant_p
;
12252 data
->tokens
= vNULL
;
12253 data
->attribs
[0] = NULL
;
12254 data
->attribs
[1] = NULL
;
12255 parser
->omp_declare_simd
= data
;
12257 parser
->omp_declare_simd
->attribs
[!start
] = pattrs
;
12260 /* Parse a statement.
12264 expression-statement
12266 selection-statement
12267 iteration-statement
12269 declaration-statement
12276 attribute-specifier-seq (opt) expression-statement
12277 attribute-specifier-seq (opt) compound-statement
12278 attribute-specifier-seq (opt) selection-statement
12279 attribute-specifier-seq (opt) iteration-statement
12280 attribute-specifier-seq (opt) jump-statement
12281 declaration-statement
12282 attribute-specifier-seq (opt) try-block
12285 expression-statement
12294 IN_COMPOUND is true when the statement is nested inside a
12295 cp_parser_compound_statement.
12297 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12298 is a (possibly labeled) if statement which is not enclosed in braces
12299 and has an else clause. This is used to implement -Wparentheses.
12301 CHAIN is a vector of if-else-if conditions. */
12304 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
12305 const bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
12306 location_t
*loc_after_labels
)
12308 tree statement
, std_attrs
= NULL_TREE
;
12310 location_t statement_location
, attrs_loc
;
12311 bool in_omp_attribute_pragma
= parser
->lexer
->in_omp_attribute_pragma
;
12312 bool has_std_attrs
;
12313 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12314 This matters for certain pragmas. */
12315 bool in_compound_for_pragma
= in_compound
;
12320 /* There is no statement yet. */
12321 statement
= NULL_TREE
;
12323 saved_token_sentinel
saved_tokens (parser
->lexer
);
12324 token
= cp_lexer_peek_token (parser
->lexer
);
12325 attrs_loc
= token
->location
;
12326 if (c_dialect_objc ())
12327 /* In obj-c++, seeing '[[' might be the either the beginning of
12328 c++11 attributes, or a nested objc-message-expression. So
12329 let's parse the c++11 attributes tentatively. */
12330 cp_parser_parse_tentatively (parser
);
12331 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
12333 attrs_loc
= make_location (attrs_loc
, attrs_loc
, parser
->lexer
);
12334 if (c_dialect_objc ())
12336 if (!cp_parser_parse_definitely (parser
))
12337 std_attrs
= NULL_TREE
;
12339 has_std_attrs
= cp_lexer_peek_token (parser
->lexer
) != token
;
12341 /* Peek at the next token. */
12342 token
= cp_lexer_peek_token (parser
->lexer
);
12343 bool omp_attrs_forbidden_p
;
12344 omp_attrs_forbidden_p
= parser
->omp_attrs_forbidden_p
;
12346 if (std_attrs
&& (flag_openmp
|| flag_openmp_simd
))
12348 bool handle_omp_attribs
= false;
12349 if (token
->type
== CPP_KEYWORD
)
12350 switch (token
->keyword
)
12360 case RID_CO_RETURN
:
12364 case RID_AT_FINALLY
:
12365 case RID_AT_SYNCHRONIZED
:
12368 case RID_TRANSACTION_ATOMIC
:
12369 case RID_TRANSACTION_RELAXED
:
12370 case RID_SYNCHRONIZED
:
12371 case RID_ATOMIC_NOEXCEPT
:
12372 case RID_ATOMIC_CANCEL
:
12373 case RID_TRANSACTION_CANCEL
:
12374 handle_omp_attribs
= true;
12379 else if (token
->type
== CPP_SEMICOLON
12380 || token
->type
== CPP_OPEN_BRACE
12381 || token
->type
== CPP_PRAGMA
)
12382 handle_omp_attribs
= true;
12383 if (handle_omp_attribs
)
12385 std_attrs
= cp_parser_handle_statement_omp_attributes (parser
,
12387 token
= cp_lexer_peek_token (parser
->lexer
);
12390 parser
->omp_attrs_forbidden_p
= false;
12392 /* Remember the location of the first token in the statement. */
12393 cp_token
*statement_token
= token
;
12394 statement_location
= token
->location
;
12395 add_debug_begin_stmt (statement_location
);
12396 /* If this is a keyword, then that will often determine what kind of
12397 statement we have. */
12398 if (token
->type
== CPP_KEYWORD
)
12400 enum rid keyword
= token
->keyword
;
12406 /* Looks like a labeled-statement with a case label.
12407 Parse the label, and then use tail recursion to parse
12409 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
12410 in_compound_for_pragma
= false;
12411 in_omp_attribute_pragma
= parser
->lexer
->in_omp_attribute_pragma
;
12416 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12417 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
12423 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12424 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
12430 case RID_CO_RETURN
:
12432 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12433 statement
= cp_parser_jump_statement (parser
);
12436 /* Objective-C++ exception-handling constructs. */
12439 case RID_AT_FINALLY
:
12440 case RID_AT_SYNCHRONIZED
:
12442 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12443 statement
= cp_parser_objc_statement (parser
);
12447 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12448 statement
= cp_parser_try_block (parser
);
12451 case RID_NAMESPACE
:
12452 /* This must be a namespace alias definition. */
12455 /* Attributes should be parsed as part of the
12456 declaration, so let's un-parse them. */
12457 saved_tokens
.rollback();
12458 std_attrs
= NULL_TREE
;
12460 cp_parser_declaration_statement (parser
);
12463 case RID_TRANSACTION_ATOMIC
:
12464 case RID_TRANSACTION_RELAXED
:
12465 case RID_SYNCHRONIZED
:
12466 case RID_ATOMIC_NOEXCEPT
:
12467 case RID_ATOMIC_CANCEL
:
12468 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12469 statement
= cp_parser_transaction (parser
, token
);
12471 case RID_TRANSACTION_CANCEL
:
12472 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12473 statement
= cp_parser_transaction_cancel (parser
);
12477 /* It might be a keyword like `int' that can start a
12478 declaration-statement. */
12482 else if (token
->type
== CPP_NAME
)
12484 /* If the next token is a `:', then we are looking at a
12485 labeled-statement. */
12486 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12487 if (token
->type
== CPP_COLON
)
12489 /* Looks like a labeled-statement with an ordinary label.
12490 Parse the label, and then use tail recursion to parse
12493 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
12495 /* If there's no statement, it's not a labeled-statement, just
12496 a label. That's allowed in C++23, but only if we're at the
12497 end of a compound-statement. */
12499 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
12501 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12502 if (cxx_dialect
< cxx23
)
12503 pedwarn (loc
, OPT_Wc__23_extensions
,
12504 "label at end of compound statement only available "
12505 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12508 in_compound_for_pragma
= false;
12509 in_omp_attribute_pragma
= parser
->lexer
->in_omp_attribute_pragma
;
12513 /* Anything that starts with a `{' must be a compound-statement. */
12514 else if (token
->type
== CPP_OPEN_BRACE
)
12516 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12517 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12519 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12520 a statement all its own. */
12521 else if (token
->type
== CPP_PRAGMA
)
12524 cp_lexer
*lexer
= parser
->lexer
;
12525 bool do_restart
= false;
12526 /* Only certain OpenMP pragmas are attached to statements, and thus
12527 are considered statements themselves. All others are not. In
12528 the context of a compound, accept the pragma as a "statement" and
12529 return so that we can check for a close brace. Otherwise we
12530 require a real statement and must go back and read one. */
12531 if (in_compound_for_pragma
)
12532 cp_parser_pragma (parser
, pragma_compound
, if_p
);
12533 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
12535 if (parser
->lexer
!= lexer
12536 && lexer
->in_omp_attribute_pragma
12537 && (!in_omp_attribute_pragma
|| lexer
->orphan_p
))
12539 if (saved_tokens
.lexer
== lexer
)
12541 if (saved_tokens
.mode
== STS_COMMIT
)
12542 cp_lexer_commit_tokens (lexer
);
12543 gcc_assert (lexer
->saved_tokens
.length () == saved_tokens
.len
);
12544 saved_tokens
.lexer
= parser
->lexer
;
12545 saved_tokens
.mode
= STS_DONOTHING
;
12546 saved_tokens
.len
= parser
->lexer
->saved_tokens
.length ();
12548 cp_lexer_destroy (lexer
);
12549 lexer
= parser
->lexer
;
12553 if (parser
->lexer
== lexer
12554 && lexer
->in_omp_attribute_pragma
12555 && !in_omp_attribute_pragma
)
12556 parser
->lexer
->orphan_p
= true;
12559 else if (token
->type
== CPP_EOF
)
12561 cp_parser_error (parser
, "expected statement");
12565 /* Everything else must be a declaration-statement or an
12566 expression-statement. Try for the declaration-statement
12567 first, unless we are looking at a `;', in which case we know that
12568 we have an expression-statement. */
12571 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12574 /* Attributes should be parsed as part of the declaration,
12575 so let's un-parse them. */
12576 saved_tokens
.rollback();
12578 parser
->omp_attrs_forbidden_p
= omp_attrs_forbidden_p
;
12579 cp_parser_parse_tentatively (parser
);
12580 /* Try to parse the declaration-statement. */
12581 cp_parser_declaration_statement (parser
);
12582 parser
->omp_attrs_forbidden_p
= false;
12583 /* If that worked, we're done. */
12584 if (cp_parser_parse_definitely (parser
))
12586 /* It didn't work, restore the post-attribute position. */
12589 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
12590 if (flag_openmp
|| flag_openmp_simd
)
12593 bool handle_omp_attribs
= true;
12594 while (cp_lexer_peek_nth_token (parser
->lexer
, i
)->keyword
12597 switch (cp_lexer_peek_nth_token (parser
->lexer
, i
)->keyword
)
12600 case RID_NAMESPACE
:
12603 case RID_STATIC_ASSERT
:
12604 /* Don't handle OpenMP attribs on keywords that
12605 always start a declaration statement but don't
12606 accept attribute before it and therefore
12607 the tentative cp_parser_declaration_statement
12608 fails to parse because of that. */
12609 handle_omp_attribs
= false;
12615 if (handle_omp_attribs
)
12617 parser
->omp_attrs_forbidden_p
= omp_attrs_forbidden_p
;
12619 = cp_parser_handle_statement_omp_attributes
12620 (parser
, std_attrs
);
12621 parser
->omp_attrs_forbidden_p
= false;
12622 token
= cp_lexer_peek_token (parser
->lexer
);
12623 if (token
->type
== CPP_PRAGMA
)
12629 /* All preceding labels have been parsed at this point. */
12630 if (loc_after_labels
!= NULL
)
12631 *loc_after_labels
= statement_location
;
12633 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12635 /* Look for an expression-statement instead. */
12636 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
12638 /* Handle [[fallthrough]];. */
12639 if (attribute_fallthrough_p (std_attrs
))
12641 /* The next token after the fallthrough attribute is ';'. */
12642 if (statement
== NULL_TREE
)
12644 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12645 statement
= build_call_expr_internal_loc (statement_location
,
12647 void_type_node
, 0);
12648 finish_expr_stmt (statement
);
12651 warning_at (statement_location
, OPT_Wattributes
,
12652 "%<fallthrough%> attribute not followed by %<;%>");
12653 std_attrs
= NULL_TREE
;
12657 /* Set the line number for the statement. */
12658 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
12659 SET_EXPR_LOCATION (statement
, statement_location
);
12661 /* Allow "[[fallthrough]];", but warn otherwise. */
12662 if (std_attrs
!= NULL_TREE
)
12663 warning_at (attrs_loc
,
12665 "attributes at the beginning of statement are ignored");
12668 /* Append ATTR to attribute list ATTRS. */
12671 attr_chainon (tree attrs
, tree attr
)
12673 if (attrs
== error_mark_node
)
12674 return error_mark_node
;
12675 if (attr
== error_mark_node
)
12676 return error_mark_node
;
12677 return chainon (attrs
, attr
);
12680 /* Parse the label for a labeled-statement, i.e.
12683 attribute-specifier-seq[opt] identifier :
12684 attribute-specifier-seq[opt] case constant-expression :
12685 attribute-specifier-seq[opt] default :
12691 case constant-expression ... constant-expression : statement
12693 When a label is parsed without errors, the label is added to the
12694 parse tree by the finish_* functions, so this function doesn't
12695 have to return the label. */
12698 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
12701 tree label
= NULL_TREE
;
12702 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12704 /* The next token should be an identifier. */
12705 token
= cp_lexer_peek_token (parser
->lexer
);
12706 if (token
->type
!= CPP_NAME
12707 && token
->type
!= CPP_KEYWORD
)
12709 cp_parser_error (parser
, "expected labeled-statement");
12713 /* Remember whether this case or a user-defined label is allowed to fall
12715 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
12717 parser
->colon_corrects_to_scope_p
= false;
12718 switch (token
->keyword
)
12722 tree expr
, expr_hi
;
12723 cp_token
*ellipsis
;
12725 /* Consume the `case' token. */
12726 cp_lexer_consume_token (parser
->lexer
);
12727 /* Parse the constant-expression. */
12728 expr
= cp_parser_constant_expression (parser
);
12729 if (check_for_bare_parameter_packs (expr
))
12730 expr
= error_mark_node
;
12732 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
12733 if (ellipsis
->type
== CPP_ELLIPSIS
)
12735 /* Consume the `...' token. */
12736 cp_lexer_consume_token (parser
->lexer
);
12737 expr_hi
= cp_parser_constant_expression (parser
);
12738 if (check_for_bare_parameter_packs (expr_hi
))
12739 expr_hi
= error_mark_node
;
12741 /* We don't need to emit warnings here, as the common code
12742 will do this for us. */
12745 expr_hi
= NULL_TREE
;
12747 if (parser
->in_switch_statement_p
)
12749 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
12750 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
12752 label
= CASE_LABEL (l
);
12753 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
12757 error_at (token
->location
,
12758 "case label %qE not within a switch statement",
12764 /* Consume the `default' token. */
12765 cp_lexer_consume_token (parser
->lexer
);
12767 if (parser
->in_switch_statement_p
)
12769 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
12770 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
12772 label
= CASE_LABEL (l
);
12773 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
12777 error_at (token
->location
, "case label not within a switch statement");
12781 /* Anything else must be an ordinary label. */
12782 label
= finish_label_stmt (cp_parser_identifier (parser
));
12783 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
12784 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
12788 /* Require the `:' token. */
12789 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
12791 /* An ordinary label may optionally be followed by attributes.
12792 However, this is only permitted if the attributes are then
12793 followed by a semicolon. This is because, for backward
12794 compatibility, when parsing
12795 lab: __attribute__ ((unused)) int i;
12796 we want the attribute to attach to "i", not "lab". */
12797 if (label
!= NULL_TREE
12798 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
12801 cp_parser_parse_tentatively (parser
);
12802 attrs
= cp_parser_gnu_attributes_opt (parser
);
12803 if (attrs
== NULL_TREE
12804 /* And fallthrough always binds to the expression-statement. */
12805 || attribute_fallthrough_p (attrs
)
12806 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12807 cp_parser_abort_tentative_parse (parser
);
12808 else if (!cp_parser_parse_definitely (parser
))
12811 attributes
= attr_chainon (attributes
, attrs
);
12814 if (attributes
!= NULL_TREE
)
12815 cplus_decl_attributes (&label
, attributes
, 0);
12817 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12820 /* Parse an expression-statement.
12822 expression-statement:
12825 Returns the new EXPR_STMT -- or NULL_TREE if the expression
12826 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12827 indicates whether this expression-statement is part of an
12828 expression statement. */
12831 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
12833 tree statement
= NULL_TREE
;
12834 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12835 location_t loc
= token
->location
;
12837 /* There might be attribute fallthrough. */
12838 tree attr
= cp_parser_gnu_attributes_opt (parser
);
12840 /* If the next token is a ';', then there is no expression
12842 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12844 statement
= cp_parser_expression (parser
);
12845 if (statement
== error_mark_node
12846 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
12848 cp_parser_skip_to_end_of_block_or_statement (parser
);
12849 return error_mark_node
;
12853 /* Handle [[fallthrough]];. */
12854 if (attribute_fallthrough_p (attr
))
12856 /* The next token after the fallthrough attribute is ';'. */
12857 if (statement
== NULL_TREE
)
12858 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12859 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
12860 void_type_node
, 0);
12862 warning_at (loc
, OPT_Wattributes
,
12863 "%<fallthrough%> attribute not followed by %<;%>");
12867 /* Allow "[[fallthrough]];", but warn otherwise. */
12868 if (attr
!= NULL_TREE
)
12869 warning_at (loc
, OPT_Wattributes
,
12870 "attributes at the beginning of statement are ignored");
12872 /* Give a helpful message for "A<T>::type t;" and the like. */
12873 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
12874 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
12876 if (TREE_CODE (statement
) == SCOPE_REF
)
12877 error_at (token
->location
, "need %<typename%> before %qE because "
12878 "%qT is a dependent scope",
12879 statement
, TREE_OPERAND (statement
, 0));
12880 else if (is_overloaded_fn (statement
)
12881 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
12884 tree fn
= get_first_fn (statement
);
12885 error_at (token
->location
,
12886 "%<%T::%D%> names the constructor, not the type",
12887 DECL_CONTEXT (fn
), DECL_NAME (fn
));
12891 /* Consume the final `;'. */
12892 cp_parser_consume_semicolon_at_end_of_statement (parser
);
12894 if (in_statement_expr
12895 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
12896 /* This is the final expression statement of a statement
12898 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
12899 else if (statement
)
12900 statement
= finish_expr_stmt (statement
);
12905 /* Parse a compound-statement.
12907 compound-statement:
12908 { statement-seq [opt] label-seq [opt] }
12916 compound-statement:
12917 { label-declaration-seq [opt] statement-seq [opt] }
12919 label-declaration-seq:
12921 label-declaration-seq label-declaration
12923 Returns a tree representing the statement. */
12926 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
12927 int bcs_flags
, bool function_body
)
12929 tree compound_stmt
;
12930 matching_braces braces
;
12932 /* Consume the `{'. */
12933 if (!braces
.require_open (parser
))
12934 return error_mark_node
;
12935 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
12936 && !function_body
&& cxx_dialect
< cxx14
)
12937 pedwarn (input_location
, OPT_Wpedantic
,
12938 "compound-statement in %<constexpr%> function");
12939 /* Begin the compound-statement. */
12940 compound_stmt
= begin_compound_stmt (bcs_flags
);
12941 /* If the next keyword is `__label__' we have a label declaration. */
12942 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12943 cp_parser_label_declaration (parser
);
12944 /* Parse an (optional) statement-seq. */
12945 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
12947 /* Consume the `}'. */
12948 braces
.require_close (parser
);
12950 /* Finish the compound-statement. */
12951 finish_compound_stmt (compound_stmt
);
12953 return compound_stmt
;
12956 /* Parse an (optional) statement-seq.
12960 statement-seq [opt] statement */
12963 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
12965 /* Scan statements until there aren't any more. */
12968 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12970 /* If we are looking at a `}', then we have run out of
12971 statements; the same is true if we have reached the end
12972 of file, or have stumbled upon a stray '@end'. */
12973 if (token
->type
== CPP_CLOSE_BRACE
12974 || token
->type
== CPP_EOF
12975 || token
->type
== CPP_PRAGMA_EOL
12976 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
12979 /* If we are in a compound statement and find 'else' then
12980 something went wrong. */
12981 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
12983 if (parser
->in_statement
& IN_IF_STMT
)
12987 token
= cp_lexer_consume_token (parser
->lexer
);
12988 error_at (token
->location
, "%<else%> without a previous %<if%>");
12992 /* Parse the statement. */
12993 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
12997 /* Return true if this is the C++20 version of range-based-for with
13001 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
13005 /* Save tokens so that we can put them back. */
13006 cp_lexer_save_tokens (parser
->lexer
);
13008 /* There has to be an unnested ; followed by an unnested :. */
13009 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
13010 /*recovering=*/false,
13012 /*consume_paren=*/false) != -1)
13015 /* We found the semicolon, eat it now. */
13016 cp_lexer_consume_token (parser
->lexer
);
13018 /* Now look for ':' that is not nested in () or {}. */
13019 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
13020 /*recovering=*/false,
13022 /*consume_paren=*/false) == -1);
13025 /* Roll back the tokens we skipped. */
13026 cp_lexer_rollback_tokens (parser
->lexer
);
13031 /* Return true if we're looking at (init; cond), false otherwise. */
13034 cp_parser_init_statement_p (cp_parser
*parser
)
13036 /* Save tokens so that we can put them back. */
13037 cp_lexer_save_tokens (parser
->lexer
);
13039 /* Look for ';' that is not nested in () or {}. */
13040 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
13041 /*recovering=*/false,
13043 /*consume_paren=*/false);
13045 /* Roll back the tokens we skipped. */
13046 cp_lexer_rollback_tokens (parser
->lexer
);
13051 /* Parse a selection-statement.
13053 selection-statement:
13054 if ( init-statement [opt] condition ) statement
13055 if ( init-statement [opt] condition ) statement else statement
13056 switch ( init-statement [opt] condition ) statement
13058 Returns the new IF_STMT or SWITCH_STMT.
13060 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13061 is a (possibly labeled) if statement which is not enclosed in
13062 braces and has an else clause. This is used to implement
13065 CHAIN is a vector of if-else-if conditions. This is used to implement
13066 -Wduplicated-cond. */
13069 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
13074 token_indent_info guard_tinfo
;
13079 /* Peek at the next token. */
13080 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
13081 guard_tinfo
= get_token_indent_info (token
);
13083 /* See what kind of keyword it is. */
13084 keyword
= token
->keyword
;
13094 if (keyword
== RID_IF
13095 && cp_lexer_next_token_is_keyword (parser
->lexer
,
13099 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
13100 if (cxx_dialect
< cxx17
)
13101 pedwarn (tok
->location
, OPT_Wc__17_extensions
,
13102 "%<if constexpr%> only available with "
13103 "%<-std=c++17%> or %<-std=gnu++17%>");
13106 if (keyword
== RID_IF
&& !cx
)
13108 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13111 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
13112 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2,
13116 cp_lexer_consume_token (parser
->lexer
);
13121 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
13122 if (cxx_dialect
< cxx23
)
13123 pedwarn (tok
->location
, OPT_Wc__23_extensions
,
13124 "%<if consteval%> only available with "
13125 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13127 bool save_in_consteval_if_p
= in_consteval_if_p
;
13128 statement
= begin_if_stmt ();
13129 IF_STMT_CONSTEVAL_P (statement
) = true;
13130 condition
= finish_if_stmt_cond (boolean_false_node
, statement
);
13132 gcc_rich_location
richloc (tok
->location
);
13133 bool non_compound_stmt_p
= false;
13134 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13136 non_compound_stmt_p
= true;
13137 richloc
.add_fixit_insert_after (tok
->location
, "{");
13140 in_consteval_if_p
|= ce
> 0;
13141 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
13143 if (non_compound_stmt_p
)
13145 location_t before_loc
13146 = cp_lexer_peek_token (parser
->lexer
)->location
;
13147 richloc
.add_fixit_insert_before (before_loc
, "}");
13148 error_at (&richloc
,
13149 "%<if consteval%> requires compound statement");
13150 non_compound_stmt_p
= false;
13153 finish_then_clause (statement
);
13155 /* If the next token is `else', parse the else-clause. */
13156 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13159 cp_token
*else_tok
= cp_lexer_peek_token (parser
->lexer
);
13160 gcc_rich_location
else_richloc (else_tok
->location
);
13161 guard_tinfo
= get_token_indent_info (else_tok
);
13162 /* Consume the `else' keyword. */
13163 cp_lexer_consume_token (parser
->lexer
);
13165 begin_else_clause (statement
);
13167 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13169 non_compound_stmt_p
= true;
13170 else_richloc
.add_fixit_insert_after (else_tok
->location
,
13174 in_consteval_if_p
= save_in_consteval_if_p
| (ce
< 0);
13175 cp_parser_implicitly_scoped_statement (parser
, NULL
,
13178 if (non_compound_stmt_p
)
13180 location_t before_loc
13181 = cp_lexer_peek_token (parser
->lexer
)->location
;
13182 else_richloc
.add_fixit_insert_before (before_loc
, "}");
13183 error_at (&else_richloc
,
13184 "%<if consteval%> requires compound statement");
13187 finish_else_clause (statement
);
13190 in_consteval_if_p
= save_in_consteval_if_p
;
13193 std::swap (THEN_CLAUSE (statement
), ELSE_CLAUSE (statement
));
13194 if (THEN_CLAUSE (statement
) == NULL_TREE
)
13195 THEN_CLAUSE (statement
) = build_empty_stmt (tok
->location
);
13198 finish_if_stmt (statement
);
13202 /* Look for the `('. */
13203 matching_parens parens
;
13204 if (!parens
.require_open (parser
))
13206 cp_parser_skip_to_end_of_statement (parser
);
13207 return error_mark_node
;
13210 /* Begin the selection-statement. */
13211 if (keyword
== RID_IF
)
13213 statement
= begin_if_stmt ();
13214 IF_STMT_CONSTEXPR_P (statement
) = cx
;
13217 statement
= begin_switch_stmt ();
13219 /* Parse the optional init-statement. */
13220 if (cp_parser_init_statement_p (parser
))
13223 if (cxx_dialect
< cxx17
)
13224 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
13225 OPT_Wc__17_extensions
,
13226 "init-statement in selection statements only available "
13227 "with %<-std=c++17%> or %<-std=gnu++17%>");
13228 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13229 /* A non-empty init-statement can have arbitrary side
13232 cp_parser_init_statement (parser
, &decl
);
13235 /* Parse the condition. */
13236 condition
= cp_parser_condition (parser
);
13237 /* Look for the `)'. */
13238 if (!parens
.require_close (parser
))
13239 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13240 /*consume_paren=*/true);
13242 if (keyword
== RID_IF
)
13245 unsigned char in_statement
;
13247 /* Add the condition. */
13248 condition
= finish_if_stmt_cond (condition
, statement
);
13250 if (warn_duplicated_cond
)
13251 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
13254 /* Parse the then-clause. */
13255 in_statement
= parser
->in_statement
;
13256 parser
->in_statement
|= IN_IF_STMT
;
13258 /* Outside a template, the non-selected branch of a constexpr
13259 if is a 'discarded statement', i.e. unevaluated. */
13260 bool was_discarded
= in_discarded_stmt
;
13261 bool discard_then
= (cx
&& !processing_template_decl
13262 && integer_zerop (condition
));
13265 in_discarded_stmt
= true;
13266 ++c_inhibit_evaluation_warnings
;
13269 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
13272 parser
->in_statement
= in_statement
;
13274 finish_then_clause (statement
);
13278 THEN_CLAUSE (statement
) = NULL_TREE
;
13279 in_discarded_stmt
= was_discarded
;
13280 --c_inhibit_evaluation_warnings
;
13283 /* If the next token is `else', parse the else-clause. */
13284 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13287 bool discard_else
= (cx
&& !processing_template_decl
13288 && integer_nonzerop (condition
));
13291 in_discarded_stmt
= true;
13292 ++c_inhibit_evaluation_warnings
;
13296 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13297 /* Consume the `else' keyword. */
13298 cp_lexer_consume_token (parser
->lexer
);
13299 if (warn_duplicated_cond
)
13301 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13305 /* We've got "if (COND) else if (COND2)". Start
13306 the condition chain and add COND as the first
13308 chain
= new vec
<tree
> ();
13309 if (!CONSTANT_CLASS_P (condition
)
13310 && !TREE_SIDE_EFFECTS (condition
))
13312 /* Wrap it in a NOP_EXPR so that we can set the
13313 location of the condition. */
13314 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
13316 SET_EXPR_LOCATION (e
, token
->location
);
13317 chain
->safe_push (e
);
13320 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
13322 /* This is if-else without subsequent if. Zap the
13323 condition chain; we would have already warned at
13327 begin_else_clause (statement
);
13328 /* Parse the else-clause. */
13329 cp_parser_implicitly_scoped_statement (parser
, NULL
,
13330 guard_tinfo
, chain
);
13332 finish_else_clause (statement
);
13334 /* If we are currently parsing a then-clause, then
13335 IF_P will not be NULL. We set it to true to
13336 indicate that this if statement has an else clause.
13337 This may trigger the Wparentheses warning below
13338 when we get back up to the parent if statement. */
13344 ELSE_CLAUSE (statement
) = NULL_TREE
;
13345 in_discarded_stmt
= was_discarded
;
13346 --c_inhibit_evaluation_warnings
;
13351 /* This if statement does not have an else clause. If
13352 NESTED_IF is true, then the then-clause has an if
13353 statement which does have an else clause. We warn
13354 about the potential ambiguity. */
13356 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
13357 "suggest explicit braces to avoid ambiguous"
13359 if (warn_duplicated_cond
)
13360 /* We don't need the condition chain anymore. */
13364 /* Now we're all done with the if-statement. */
13365 finish_if_stmt (statement
);
13369 bool in_switch_statement_p
;
13370 unsigned char in_statement
;
13372 /* Add the condition. */
13373 finish_switch_cond (condition
, statement
);
13375 /* Parse the body of the switch-statement. */
13376 in_switch_statement_p
= parser
->in_switch_statement_p
;
13377 in_statement
= parser
->in_statement
;
13378 parser
->in_switch_statement_p
= true;
13379 parser
->in_statement
|= IN_SWITCH_STMT
;
13380 cp_parser_implicitly_scoped_statement (parser
, if_p
,
13382 parser
->in_switch_statement_p
= in_switch_statement_p
;
13383 parser
->in_statement
= in_statement
;
13385 /* Now we're all done with the switch-statement. */
13386 finish_switch_stmt (statement
);
13394 cp_parser_error (parser
, "expected selection-statement");
13395 return error_mark_node
;
13399 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13400 If we have seen at least one decl-specifier, and the next token is not
13401 a parenthesis (after "int (" we might be looking at a functional cast)
13402 neither we are dealing with a concept-check expression then we must be
13403 looking at a declaration. */
13406 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
13407 cp_decl_specifier_seq
*decl_specs
)
13409 if (decl_specs
->any_specifiers_p
13410 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
13411 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
13412 && !cp_parser_error_occurred (parser
)
13413 && !(decl_specs
->type
13414 && TREE_CODE (decl_specs
->type
) == TYPE_DECL
13415 && is_constrained_auto (TREE_TYPE (decl_specs
->type
))))
13416 cp_parser_commit_to_tentative_parse (parser
);
13419 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13420 The declarator shall not specify a function or an array. Returns
13421 TRUE if the declarator is valid, FALSE otherwise. */
13424 cp_parser_check_condition_declarator (cp_parser
* parser
,
13425 cp_declarator
*declarator
,
13428 if (declarator
== cp_error_declarator
13429 || function_declarator_p (declarator
)
13430 || declarator
->kind
== cdk_array
)
13432 if (declarator
== cp_error_declarator
)
13433 /* Already complained. */;
13434 else if (declarator
->kind
== cdk_array
)
13435 error_at (loc
, "condition declares an array");
13437 error_at (loc
, "condition declares a function");
13438 if (parser
->fully_implicit_function_template_p
)
13439 abort_fully_implicit_template (parser
);
13440 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
13441 /*or_comma=*/false,
13442 /*consume_paren=*/false);
13449 /* Parse a condition.
13453 type-specifier-seq declarator = initializer-clause
13454 type-specifier-seq declarator braced-init-list
13459 type-specifier-seq declarator asm-specification [opt]
13460 attributes [opt] = assignment-expression
13462 Returns the expression that should be tested. */
13465 cp_parser_condition (cp_parser
* parser
)
13467 cp_decl_specifier_seq type_specifiers
;
13468 const char *saved_message
;
13469 int declares_class_or_enum
;
13471 /* Try the declaration first. */
13472 cp_parser_parse_tentatively (parser
);
13473 /* New types are not allowed in the type-specifier-seq for a
13475 saved_message
= parser
->type_definition_forbidden_message
;
13476 parser
->type_definition_forbidden_message
13477 = G_("types may not be defined in conditions");
13478 /* Parse the type-specifier-seq. */
13479 cp_parser_decl_specifier_seq (parser
,
13480 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
13482 &declares_class_or_enum
);
13483 /* Restore the saved message. */
13484 parser
->type_definition_forbidden_message
= saved_message
;
13486 /* Gather the attributes that were provided with the
13487 decl-specifiers. */
13488 tree prefix_attributes
= type_specifiers
.attributes
;
13490 cp_parser_maybe_commit_to_declaration (parser
, &type_specifiers
);
13492 /* If all is well, we might be looking at a declaration. */
13493 if (!cp_parser_error_occurred (parser
))
13496 tree asm_specification
;
13498 cp_declarator
*declarator
;
13499 tree initializer
= NULL_TREE
;
13500 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13502 /* Parse the declarator. */
13503 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13504 CP_PARSER_FLAGS_NONE
,
13505 /*ctor_dtor_or_conv_p=*/NULL
,
13506 /*parenthesized_p=*/NULL
,
13507 /*member_p=*/false,
13508 /*friend_p=*/false,
13509 /*static_p=*/false);
13510 /* Parse the attributes. */
13511 attributes
= cp_parser_attributes_opt (parser
);
13512 /* Parse the asm-specification. */
13513 asm_specification
= cp_parser_asm_specification_opt (parser
);
13514 /* If the next token is not an `=' or '{', then we might still be
13515 looking at an expression. For example:
13519 looks like a decl-specifier-seq and a declarator -- but then
13520 there is no `=', so this is an expression. */
13521 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13522 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13523 cp_parser_simulate_error (parser
);
13525 /* If we did see an `=' or '{', then we are looking at a declaration
13527 if (cp_parser_parse_definitely (parser
))
13530 bool non_constant_p
= false;
13531 int flags
= LOOKUP_ONLYCONVERTING
;
13533 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
13534 return error_mark_node
;
13536 /* Create the declaration. */
13537 decl
= start_decl (declarator
, &type_specifiers
,
13538 /*initialized_p=*/true,
13539 attributes
, prefix_attributes
,
13542 declarator
->init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13543 /* Parse the initializer. */
13544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13546 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
13547 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
13550 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13552 /* Consume the `='. */
13553 cp_lexer_consume_token (parser
->lexer
);
13554 initializer
= cp_parser_initializer_clause (parser
,
13559 cp_parser_error (parser
, "expected initializer");
13560 initializer
= error_mark_node
;
13562 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
13563 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
13565 /* Process the initializer. */
13566 cp_finish_decl (decl
,
13567 initializer
, !non_constant_p
,
13572 pop_scope (pushed_scope
);
13574 return convert_from_reference (decl
);
13577 /* If we didn't even get past the declarator successfully, we are
13578 definitely not looking at a declaration. */
13580 cp_parser_abort_tentative_parse (parser
);
13582 /* Otherwise, we are looking at an expression. */
13583 return cp_parser_expression (parser
);
13586 /* Parses a for-statement or range-for-statement until the closing ')',
13590 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
13592 tree init
, scope
, decl
;
13595 /* Begin the for-statement. */
13596 scope
= begin_for_scope (&init
);
13598 /* Maybe parse the optional init-statement in a range-based for loop. */
13599 if (cp_parser_range_based_for_with_init_p (parser
)
13600 /* Checked for diagnostic purposes only. */
13601 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13604 cp_parser_init_statement (parser
, &dummy
);
13605 if (cxx_dialect
< cxx20
)
13607 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
13608 OPT_Wc__20_extensions
,
13609 "range-based %<for%> loops with initializer only "
13610 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13611 decl
= error_mark_node
;
13615 /* Parse the initialization. */
13616 is_range_for
= cp_parser_init_statement (parser
, &decl
);
13619 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
13622 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
13626 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
13627 unsigned short unroll
)
13629 /* Normal for loop */
13630 tree condition
= NULL_TREE
;
13631 tree expression
= NULL_TREE
;
13634 stmt
= begin_for_stmt (scope
, init
);
13635 /* The init-statement has already been parsed in
13636 cp_parser_init_statement, so no work is needed here. */
13637 finish_init_stmt (stmt
);
13639 /* If there's a condition, process it. */
13640 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13641 condition
= cp_parser_condition (parser
);
13644 cp_parser_error (parser
, "missing loop condition in loop with "
13645 "%<GCC ivdep%> pragma");
13646 condition
= error_mark_node
;
13650 cp_parser_error (parser
, "missing loop condition in loop with "
13651 "%<GCC unroll%> pragma");
13652 condition
= error_mark_node
;
13654 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
13655 /* Look for the `;'. */
13656 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13658 /* If there's an expression, process it. */
13659 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
13660 expression
= cp_parser_expression (parser
);
13661 finish_for_expr (expression
, stmt
);
13666 /* Tries to parse a range-based for-statement:
13669 decl-specifier-seq declarator : expression
13671 The decl-specifier-seq declarator and the `:' are already parsed by
13672 cp_parser_init_statement. If processing_template_decl it returns a
13673 newly created RANGE_FOR_STMT; if not, it is converted to a
13674 regular FOR_STMT. */
13677 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
13678 bool ivdep
, unsigned short unroll
, bool is_omp
)
13680 tree stmt
, range_expr
;
13681 auto_vec
<cxx_binding
*, 16> bindings
;
13682 auto_vec
<tree
, 16> names
;
13683 tree decomp_first_name
= NULL_TREE
;
13684 unsigned int decomp_cnt
= 0;
13686 /* Get the range declaration momentarily out of the way so that
13687 the range expression doesn't clash with it. */
13688 if (range_decl
!= error_mark_node
)
13690 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
13692 tree v
= DECL_VALUE_EXPR (range_decl
);
13693 /* For decomposition declaration get all of the corresponding
13694 declarations out of the way. */
13695 if (TREE_CODE (v
) == ARRAY_REF
13696 && VAR_P (TREE_OPERAND (v
, 0))
13697 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
13699 tree d
= range_decl
;
13700 range_decl
= TREE_OPERAND (v
, 0);
13701 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
13702 decomp_first_name
= d
;
13703 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
13705 tree name
= DECL_NAME (d
);
13706 names
.safe_push (name
);
13707 bindings
.safe_push (IDENTIFIER_BINDING (name
));
13708 IDENTIFIER_BINDING (name
)
13709 = IDENTIFIER_BINDING (name
)->previous
;
13713 if (names
.is_empty ())
13715 tree name
= DECL_NAME (range_decl
);
13716 names
.safe_push (name
);
13717 bindings
.safe_push (IDENTIFIER_BINDING (name
));
13718 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
13722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13724 bool expr_non_constant_p
;
13725 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
13728 range_expr
= cp_parser_expression (parser
);
13730 /* Put the range declaration(s) back into scope. */
13731 for (unsigned int i
= 0; i
< names
.length (); i
++)
13733 cxx_binding
*binding
= bindings
[i
];
13734 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
13735 IDENTIFIER_BINDING (names
[i
]) = binding
;
13738 /* finish_omp_for has its own code for the following, so just
13739 return the range_expr instead. */
13743 /* If in template, STMT is converted to a normal for-statement
13744 at instantiation. If not, it is done just ahead. */
13745 if (processing_template_decl
)
13747 if (check_for_bare_parameter_packs (range_expr
))
13748 range_expr
= error_mark_node
;
13749 stmt
= begin_range_for_stmt (scope
, init
);
13751 RANGE_FOR_IVDEP (stmt
) = 1;
13753 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
13754 finish_range_for_decl (stmt
, range_decl
, range_expr
);
13755 if (!type_dependent_expression_p (range_expr
)
13756 /* do_auto_deduction doesn't mess with template init-lists. */
13757 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
13758 do_range_for_auto_deduction (range_decl
, range_expr
);
13762 stmt
= begin_for_stmt (scope
, init
);
13763 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
13764 decomp_first_name
, decomp_cnt
, ivdep
,
13770 /* Subroutine of cp_convert_range_for: given the initializer expression,
13771 builds up the range temporary. */
13774 build_range_temp (tree range_expr
)
13776 /* Find out the type deduced by the declaration
13777 `auto &&__range = range_expr'. */
13778 tree auto_node
= make_auto ();
13779 tree range_type
= cp_build_reference_type (auto_node
, true);
13780 range_type
= do_auto_deduction (range_type
, range_expr
, auto_node
);
13782 /* Create the __range variable. */
13783 tree range_temp
= build_decl (input_location
, VAR_DECL
,
13784 for_range__identifier
, range_type
);
13785 TREE_USED (range_temp
) = 1;
13786 DECL_ARTIFICIAL (range_temp
) = 1;
13791 /* Used by cp_parser_range_for in template context: we aren't going to
13792 do a full conversion yet, but we still need to resolve auto in the
13793 type of the for-range-declaration if present. This is basically
13794 a shortcut version of cp_convert_range_for. */
13797 do_range_for_auto_deduction (tree decl
, tree range_expr
)
13799 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
13802 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
13803 range_temp
= convert_from_reference (build_range_temp (range_expr
));
13804 iter_type
= (cp_parser_perform_range_for_lookup
13805 (range_temp
, &begin_dummy
, &end_dummy
));
13808 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
13810 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
13811 RO_UNARY_STAR
, NULL_TREE
,
13812 tf_warning_or_error
);
13813 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
13814 iter_decl
, auto_node
,
13815 tf_warning_or_error
,
13816 adc_variable_type
);
13821 /* Warns when the loop variable should be changed to a reference type to
13822 avoid unnecessary copying. I.e., from
13824 for (const auto x : range)
13826 where range returns a reference, to
13828 for (const auto &x : range)
13830 if this version doesn't make a copy.
13832 This function also warns when the loop variable is initialized with
13833 a value of a different type resulting in a copy:
13836 for (const double &x : arr)
13838 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
13839 This function is never called when processing_template_decl is on. */
13842 warn_for_range_copy (tree decl
, tree expr
)
13844 if (!warn_range_loop_construct
13845 || decl
== error_mark_node
)
13848 location_t loc
= DECL_SOURCE_LOCATION (decl
);
13849 tree type
= TREE_TYPE (decl
);
13851 if (from_macro_expansion_at (loc
))
13854 if (TYPE_REF_P (type
))
13856 if (glvalue_p (expr
) && ref_conv_binds_directly (type
, expr
).is_false ())
13858 auto_diagnostic_group d
;
13859 if (warning_at (loc
, OPT_Wrange_loop_construct
,
13860 "loop variable %qD of type %qT binds to a temporary "
13861 "constructed from type %qT", decl
, type
,
13864 tree ref
= cp_build_qualified_type (TREE_TYPE (expr
),
13866 ref
= cp_build_reference_type (ref
, /*rval*/false);
13867 inform (loc
, "use non-reference type %qT to make the copy "
13868 "explicit or %qT to prevent copying",
13869 non_reference (type
), ref
);
13874 else if (!CP_TYPE_CONST_P (type
))
13877 /* Since small trivially copyable types are cheap to copy, we suppress the
13878 warning for them. 64B is a common size of a cache line. */
13879 if (TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
13880 || (tree_to_uhwi (TYPE_SIZE_UNIT (type
)) <= 64
13881 && trivially_copyable_p (type
)))
13884 /* If we can initialize a reference directly, suggest that to avoid the
13886 tree rtype
= cp_build_reference_type (type
, /*rval*/false);
13887 if (ref_conv_binds_directly (rtype
, expr
).is_true ())
13889 auto_diagnostic_group d
;
13890 if (warning_at (loc
, OPT_Wrange_loop_construct
,
13891 "loop variable %qD creates a copy from type %qT",
13894 gcc_rich_location
richloc (loc
);
13895 richloc
.add_fixit_insert_before ("&");
13896 inform (&richloc
, "use reference type to prevent copying");
13901 /* Converts a range-based for-statement into a normal
13902 for-statement, as per the definition.
13904 for (RANGE_DECL : RANGE_EXPR)
13907 should be equivalent to:
13910 auto &&__range = RANGE_EXPR;
13911 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
13915 RANGE_DECL = *__begin;
13920 If RANGE_EXPR is an array:
13921 BEGIN_EXPR = __range
13922 END_EXPR = __range + ARRAY_SIZE(__range)
13923 Else if RANGE_EXPR has a member 'begin' or 'end':
13924 BEGIN_EXPR = __range.begin()
13925 END_EXPR = __range.end()
13927 BEGIN_EXPR = begin(__range)
13928 END_EXPR = end(__range);
13930 If __range has a member 'begin' but not 'end', or vice versa, we must
13931 still use the second alternative (it will surely fail, however).
13932 When calling begin()/end() in the third alternative we must use
13933 argument dependent lookup, but always considering 'std' as an associated
13937 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
13938 tree decomp_first_name
, unsigned int decomp_cnt
,
13939 bool ivdep
, unsigned short unroll
)
13942 tree iter_type
, begin_expr
, end_expr
;
13943 tree condition
, expression
;
13945 range_expr
= mark_lvalue_use (range_expr
);
13947 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
13948 /* If an error happened previously do nothing or else a lot of
13949 unhelpful errors would be issued. */
13950 begin_expr
= end_expr
= iter_type
= error_mark_node
;
13955 if (VAR_P (range_expr
)
13956 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
13957 /* Can't bind a reference to an array of runtime bound. */
13958 range_temp
= range_expr
;
13961 range_temp
= build_range_temp (range_expr
);
13962 pushdecl (range_temp
);
13963 cp_finish_decl (range_temp
, range_expr
,
13964 /*is_constant_init*/false, NULL_TREE
,
13965 LOOKUP_ONLYCONVERTING
);
13966 range_temp
= convert_from_reference (range_temp
);
13968 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
13969 &begin_expr
, &end_expr
);
13972 /* The new for initialization statement. */
13973 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
13975 TREE_USED (begin
) = 1;
13976 DECL_ARTIFICIAL (begin
) = 1;
13978 cp_finish_decl (begin
, begin_expr
,
13979 /*is_constant_init*/false, NULL_TREE
,
13980 LOOKUP_ONLYCONVERTING
);
13982 if (cxx_dialect
>= cxx17
)
13983 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
13984 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
13985 TREE_USED (end
) = 1;
13986 DECL_ARTIFICIAL (end
) = 1;
13988 cp_finish_decl (end
, end_expr
,
13989 /*is_constant_init*/false, NULL_TREE
,
13990 LOOKUP_ONLYCONVERTING
);
13992 finish_init_stmt (statement
);
13994 /* The new for condition. */
13995 condition
= build_x_binary_op (input_location
, NE_EXPR
,
13998 NULL_TREE
, NULL
, tf_warning_or_error
);
13999 finish_for_cond (condition
, statement
, ivdep
, unroll
);
14001 /* The new increment expression. */
14002 expression
= finish_unary_op_expr (input_location
,
14003 PREINCREMENT_EXPR
, begin
,
14004 tf_warning_or_error
);
14005 finish_for_expr (expression
, statement
);
14007 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
14008 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
14010 /* The declaration is initialized with *__begin inside the loop body. */
14011 tree deref_begin
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
14012 NULL_TREE
, tf_warning_or_error
);
14013 cp_finish_decl (range_decl
, deref_begin
,
14014 /*is_constant_init*/false, NULL_TREE
,
14015 LOOKUP_ONLYCONVERTING
);
14016 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
14017 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
14019 warn_for_range_copy (range_decl
, deref_begin
);
14024 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
14025 We need to solve both at the same time because the method used
14026 depends on the existence of members begin or end.
14027 Returns the type deduced for the iterator expression. */
14030 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
14032 if (error_operand_p (range
))
14034 *begin
= *end
= error_mark_node
;
14035 return error_mark_node
;
14038 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
14040 error ("range-based %<for%> expression of type %qT "
14041 "has incomplete type", TREE_TYPE (range
));
14042 *begin
= *end
= error_mark_node
;
14043 return error_mark_node
;
14045 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
14047 /* If RANGE is an array, we will use pointer arithmetic. */
14048 *begin
= decay_conversion (range
, tf_warning_or_error
);
14049 *end
= build_binary_op (input_location
, PLUS_EXPR
,
14051 array_type_nelts_top (TREE_TYPE (range
)),
14053 return TREE_TYPE (*begin
);
14057 /* If it is not an array, we must do a bit of magic. */
14058 tree id_begin
, id_end
;
14059 tree member_begin
, member_end
;
14061 *begin
= *end
= error_mark_node
;
14063 id_begin
= get_identifier ("begin");
14064 id_end
= get_identifier ("end");
14065 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
14066 /*protect=*/2, /*want_type=*/false,
14067 tf_warning_or_error
);
14068 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
14069 /*protect=*/2, /*want_type=*/false,
14070 tf_warning_or_error
);
14072 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
14074 /* Use the member functions. */
14075 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
14076 *end
= cp_parser_range_for_member_function (range
, id_end
);
14080 /* Use global functions with ADL. */
14083 vec_safe_push (vec
, range
);
14085 member_begin
= perform_koenig_lookup (id_begin
, vec
,
14086 tf_warning_or_error
);
14087 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
14088 tf_warning_or_error
);
14089 member_end
= perform_koenig_lookup (id_end
, vec
,
14090 tf_warning_or_error
);
14091 *end
= finish_call_expr (member_end
, &vec
, false, true,
14092 tf_warning_or_error
);
14095 /* Last common checks. */
14096 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
14098 /* If one of the expressions is an error do no more checks. */
14099 *begin
= *end
= error_mark_node
;
14100 return error_mark_node
;
14102 else if (type_dependent_expression_p (*begin
)
14103 || type_dependent_expression_p (*end
))
14104 /* Can happen, when, eg, in a template context, Koenig lookup
14105 can't resolve begin/end (c++/58503). */
14109 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
14110 /* The unqualified type of the __begin and __end temporaries should
14111 be the same, as required by the multiple auto declaration. */
14112 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
14114 if (cxx_dialect
>= cxx17
14115 && (build_x_binary_op (input_location
, NE_EXPR
,
14116 *begin
, ERROR_MARK
,
14118 NULL_TREE
, NULL
, tf_none
)
14119 != error_mark_node
))
14120 /* P0184R0 allows __begin and __end to have different types,
14121 but make sure they are comparable so we can give a better
14124 error ("inconsistent begin/end types in range-based %<for%> "
14125 "statement: %qT and %qT",
14126 TREE_TYPE (*begin
), TREE_TYPE (*end
));
14133 /* Helper function for cp_parser_perform_range_for_lookup.
14134 Builds a tree for RANGE.IDENTIFIER(). */
14137 cp_parser_range_for_member_function (tree range
, tree identifier
)
14141 member
= finish_class_member_access_expr (range
, identifier
,
14142 false, tf_warning_or_error
);
14143 if (member
== error_mark_node
)
14144 return error_mark_node
;
14147 res
= finish_call_expr (member
, &vec
,
14148 /*disallow_virtual=*/false,
14149 /*koenig_p=*/false,
14150 tf_warning_or_error
);
14154 /* Parse an iteration-statement.
14156 iteration-statement:
14157 while ( condition ) statement
14158 do statement while ( expression ) ;
14159 for ( init-statement condition [opt] ; expression [opt] )
14162 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14165 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
14166 unsigned short unroll
)
14171 unsigned char in_statement
;
14172 token_indent_info guard_tinfo
;
14174 /* Peek at the next token. */
14175 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
14177 return error_mark_node
;
14179 guard_tinfo
= get_token_indent_info (token
);
14181 /* Remember whether or not we are already within an iteration
14183 in_statement
= parser
->in_statement
;
14185 /* See what kind of keyword it is. */
14186 keyword
= token
->keyword
;
14193 /* Begin the while-statement. */
14194 statement
= begin_while_stmt ();
14195 /* Look for the `('. */
14196 matching_parens parens
;
14197 parens
.require_open (parser
);
14198 /* Parse the condition. */
14199 condition
= cp_parser_condition (parser
);
14200 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
14201 /* Look for the `)'. */
14202 parens
.require_close (parser
);
14203 /* Parse the dependent statement. */
14204 parser
->in_statement
= IN_ITERATION_STMT
;
14205 bool prev
= note_iteration_stmt_body_start ();
14206 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
14207 note_iteration_stmt_body_end (prev
);
14208 parser
->in_statement
= in_statement
;
14209 /* We're done with the while-statement. */
14210 finish_while_stmt (statement
);
14218 /* Begin the do-statement. */
14219 statement
= begin_do_stmt ();
14220 /* Parse the body of the do-statement. */
14221 parser
->in_statement
= IN_ITERATION_STMT
;
14222 bool prev
= note_iteration_stmt_body_start ();
14223 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
14224 note_iteration_stmt_body_end (prev
);
14225 parser
->in_statement
= in_statement
;
14226 finish_do_body (statement
);
14227 /* Look for the `while' keyword. */
14228 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
14229 /* Look for the `('. */
14230 matching_parens parens
;
14231 parens
.require_open (parser
);
14232 /* Parse the expression. */
14233 expression
= cp_parser_expression (parser
);
14234 /* We're done with the do-statement. */
14235 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
14236 /* Look for the `)'. */
14237 parens
.require_close (parser
);
14238 /* Look for the `;'. */
14239 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14245 /* Look for the `('. */
14246 matching_parens parens
;
14247 parens
.require_open (parser
);
14249 statement
= cp_parser_for (parser
, ivdep
, unroll
);
14251 /* Look for the `)'. */
14252 parens
.require_close (parser
);
14254 /* Parse the body of the for-statement. */
14255 parser
->in_statement
= IN_ITERATION_STMT
;
14256 bool prev
= note_iteration_stmt_body_start ();
14257 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
14258 note_iteration_stmt_body_end (prev
);
14259 parser
->in_statement
= in_statement
;
14261 /* We're done with the for-statement. */
14262 finish_for_stmt (statement
);
14267 cp_parser_error (parser
, "expected iteration-statement");
14268 statement
= error_mark_node
;
14275 /* Parse an init-statement or the declarator of a range-based-for.
14276 Returns true if a range-based-for declaration is seen.
14279 expression-statement
14281 alias-declaration */
14284 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
14286 /* If the next token is a `;', then we have an empty
14287 expression-statement. Grammatically, this is also a
14288 simple-declaration, but an invalid one, because it does not
14289 declare anything. Therefore, if we did not handle this case
14290 specially, we would issue an error message about an invalid
14292 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14294 bool is_range_for
= false;
14295 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
14297 /* A colon is used in range-based for. */
14298 parser
->colon_corrects_to_scope_p
= false;
14300 /* We're going to speculatively look for a declaration, falling back
14301 to an expression, if necessary. */
14302 cp_parser_parse_tentatively (parser
);
14303 bool expect_semicolon_p
= true;
14304 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
14306 cp_parser_alias_declaration (parser
);
14307 expect_semicolon_p
= false;
14308 if (cxx_dialect
< cxx23
14309 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
14310 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
14311 OPT_Wc__23_extensions
,
14312 "alias-declaration in init-statement only "
14313 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14316 /* Parse the declaration. */
14317 cp_parser_simple_declaration (parser
,
14318 /*function_definition_allowed_p=*/false,
14320 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
14321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14323 /* It is a range-for, consume the ':'. */
14324 cp_lexer_consume_token (parser
->lexer
);
14325 is_range_for
= true;
14326 if (cxx_dialect
< cxx11
)
14327 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
14328 OPT_Wc__11_extensions
,
14329 "range-based %<for%> loops only available with "
14330 "%<-std=c++11%> or %<-std=gnu++11%>");
14332 else if (expect_semicolon_p
)
14333 /* The ';' is not consumed yet because we told
14334 cp_parser_simple_declaration not to. */
14335 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14337 if (cp_parser_parse_definitely (parser
))
14338 return is_range_for
;
14339 /* If the tentative parse failed, then we shall need to look for an
14340 expression-statement. */
14342 /* If we are here, it is an expression-statement. */
14343 cp_parser_expression_statement (parser
, NULL_TREE
);
14347 /* Parse a jump-statement.
14352 return expression [opt] ;
14353 return braced-init-list ;
14354 coroutine-return-statement;
14360 goto * expression ;
14362 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14365 cp_parser_jump_statement (cp_parser
* parser
)
14367 tree statement
= error_mark_node
;
14370 unsigned char in_statement
;
14372 /* Peek at the next token. */
14373 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
14375 return error_mark_node
;
14377 /* See what kind of keyword it is. */
14378 keyword
= token
->keyword
;
14382 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
14383 switch (in_statement
)
14386 error_at (token
->location
, "break statement not within loop or switch");
14389 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
14390 || in_statement
== IN_ITERATION_STMT
);
14391 statement
= finish_break_stmt ();
14392 if (in_statement
== IN_ITERATION_STMT
)
14393 break_maybe_infinite_loop ();
14396 error_at (token
->location
, "invalid exit from OpenMP structured block");
14399 error_at (token
->location
, "break statement used with OpenMP for loop");
14402 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14406 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
14409 error_at (token
->location
, "continue statement not within a loop");
14411 /* Fall through. */
14412 case IN_ITERATION_STMT
:
14414 statement
= finish_continue_stmt ();
14417 error_at (token
->location
, "invalid exit from OpenMP structured block");
14420 gcc_unreachable ();
14422 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14425 case RID_CO_RETURN
:
14429 bool expr_non_constant_p
;
14431 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14433 cp_lexer_set_source_position (parser
->lexer
);
14434 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14435 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14437 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14438 expr
= cp_parser_expression (parser
);
14440 /* If the next token is a `;', then there is no
14443 /* Build the return-statement, check co-return first, since type
14444 deduction is not valid there. */
14445 if (keyword
== RID_CO_RETURN
)
14446 statement
= finish_co_return_stmt (token
->location
, expr
);
14447 else if (FNDECL_USED_AUTO (current_function_decl
) && in_discarded_stmt
)
14448 /* Don't deduce from a discarded return statement. */;
14450 statement
= finish_return_stmt (expr
);
14451 /* Look for the final `;'. */
14452 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14457 if (parser
->in_function_body
14458 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
14459 && cxx_dialect
< cxx23
)
14461 error ("%<goto%> in %<constexpr%> function only available with "
14462 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14463 cp_function_chain
->invalid_constexpr
= true;
14466 /* Create the goto-statement. */
14467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
14469 /* Issue a warning about this use of a GNU extension. */
14470 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
14471 /* Consume the '*' token. */
14472 cp_lexer_consume_token (parser
->lexer
);
14473 /* Parse the dependent expression. */
14474 finish_goto_stmt (cp_parser_expression (parser
));
14477 finish_goto_stmt (cp_parser_identifier (parser
));
14478 /* Look for the final `;'. */
14479 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14483 cp_parser_error (parser
, "expected jump-statement");
14490 /* Parse a declaration-statement.
14492 declaration-statement:
14493 block-declaration */
14496 cp_parser_declaration_statement (cp_parser
* parser
)
14500 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14501 p
= obstack_alloc (&declarator_obstack
, 0);
14503 /* Parse the block-declaration. */
14504 cp_parser_block_declaration (parser
, /*statement_p=*/true);
14506 /* Free any declarators allocated. */
14507 obstack_free (&declarator_obstack
, p
);
14510 /* Some dependent statements (like `if (cond) statement'), are
14511 implicitly in their own scope. In other words, if the statement is
14512 a single statement (as opposed to a compound-statement), it is
14513 none-the-less treated as if it were enclosed in braces. Any
14514 declarations appearing in the dependent statement are out of scope
14515 after control passes that point. This function parses a statement,
14516 but ensures that is in its own scope, even if it is not a
14517 compound-statement.
14519 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14520 is a (possibly labeled) if statement which is not enclosed in
14521 braces and has an else clause. This is used to implement
14524 CHAIN is a vector of if-else-if conditions. This is used to implement
14527 Returns the new statement. */
14530 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
14531 const token_indent_info
&guard_tinfo
,
14535 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14536 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
14537 token_indent_info body_tinfo
14538 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
14543 /* Mark if () ; with a special NOP_EXPR. */
14544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14546 cp_lexer_consume_token (parser
->lexer
);
14547 statement
= add_stmt (build_empty_stmt (body_loc
));
14549 if (guard_tinfo
.keyword
== RID_IF
14550 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
14551 warning_at (body_loc
, OPT_Wempty_body
,
14552 "suggest braces around empty body in an %<if%> statement");
14553 else if (guard_tinfo
.keyword
== RID_ELSE
)
14554 warning_at (body_loc
, OPT_Wempty_body
,
14555 "suggest braces around empty body in an %<else%> statement");
14557 /* if a compound is opened, we simply parse the statement directly. */
14558 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14559 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
14560 /* If the token is not a `{', then we must take special action. */
14563 /* Create a compound-statement. */
14564 statement
= begin_compound_stmt (0);
14565 /* Parse the dependent-statement. */
14566 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
14567 &body_loc_after_labels
);
14568 /* Finish the dummy compound-statement. */
14569 finish_compound_stmt (statement
);
14572 token_indent_info next_tinfo
14573 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
14574 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
14576 if (body_loc_after_labels
!= UNKNOWN_LOCATION
14577 && next_tinfo
.type
!= CPP_SEMICOLON
)
14578 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
14579 guard_tinfo
.location
, guard_tinfo
.keyword
);
14581 /* Return the statement. */
14585 /* For some dependent statements (like `while (cond) statement'), we
14586 have already created a scope. Therefore, even if the dependent
14587 statement is a compound-statement, we do not want to create another
14591 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
14592 const token_indent_info
&guard_tinfo
)
14594 /* If the token is a `{', then we must take special action. */
14595 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
14597 token_indent_info body_tinfo
14598 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
14599 location_t loc_after_labels
= UNKNOWN_LOCATION
;
14601 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
14602 &loc_after_labels
);
14603 token_indent_info next_tinfo
14604 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
14605 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
14607 if (loc_after_labels
!= UNKNOWN_LOCATION
14608 && next_tinfo
.type
!= CPP_SEMICOLON
)
14609 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
14610 guard_tinfo
.location
,
14611 guard_tinfo
.keyword
);
14615 /* Avoid calling cp_parser_compound_statement, so that we
14616 don't create a new scope. Do everything else by hand. */
14617 matching_braces braces
;
14618 braces
.require_open (parser
);
14619 /* If the next keyword is `__label__' we have a label declaration. */
14620 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
14621 cp_parser_label_declaration (parser
);
14622 /* Parse an (optional) statement-seq. */
14623 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
14624 braces
.require_close (parser
);
14630 /* Parse a module-name,
14632 module-name . identifier
14635 Returns a pointer to module object, NULL. */
14637 static module_state
*
14638 cp_parser_module_name (cp_parser
*parser
)
14640 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14641 if (token
->type
== CPP_HEADER_NAME
)
14643 cp_lexer_consume_token (parser
->lexer
);
14645 return get_module (token
->u
.value
);
14648 module_state
*parent
= NULL
;
14649 bool partitioned
= false;
14650 if (token
->type
== CPP_COLON
&& named_module_p ())
14652 partitioned
= true;
14653 cp_lexer_consume_token (parser
->lexer
);
14658 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
)
14660 cp_parser_error (parser
, "expected module-name");
14664 tree name
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14665 parent
= get_module (name
, parent
, partitioned
);
14666 token
= cp_lexer_peek_token (parser
->lexer
);
14667 if (!partitioned
&& token
->type
== CPP_COLON
)
14668 partitioned
= true;
14669 else if (token
->type
!= CPP_DOT
)
14672 cp_lexer_consume_token (parser
->lexer
);
14678 /* Named module-declaration
14679 __module ; PRAGMA_EOL
14680 __module private ; PRAGMA_EOL (unimplemented)
14681 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14684 static module_parse
14685 cp_parser_module_declaration (cp_parser
*parser
, module_parse mp_state
,
14688 /* We're a pseudo pragma. */
14689 parser
->lexer
->in_pragma
= true;
14690 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
14692 if (flag_header_unit
)
14694 error_at (token
->location
,
14695 "module-declaration not permitted in header-unit");
14698 else if (mp_state
== MP_FIRST
&& !exporting
14699 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14701 /* Start global module fragment. */
14702 cp_lexer_consume_token (parser
->lexer
);
14703 module_kind
= MK_NAMED
;
14704 mp_state
= MP_GLOBAL
;
14705 cp_parser_require_pragma_eol (parser
, token
);
14707 else if (!exporting
14708 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
14709 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_PRIVATE
)
14710 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_SEMICOLON
))
14712 cp_lexer_consume_token (parser
->lexer
);
14713 cp_lexer_consume_token (parser
->lexer
);
14714 cp_lexer_consume_token (parser
->lexer
);
14715 cp_parser_require_pragma_eol (parser
, token
);
14717 if ((mp_state
== MP_PURVIEW
|| mp_state
== MP_PURVIEW_IMPORTS
)
14718 && module_has_cmi_p ())
14720 mp_state
= MP_PRIVATE_IMPORTS
;
14721 sorry_at (token
->location
, "private module fragment");
14724 error_at (token
->location
,
14725 "private module fragment only permitted in purview"
14726 " of module interface or partition");
14728 else if (!(mp_state
== MP_FIRST
|| mp_state
== MP_GLOBAL
))
14730 /* Neither the first declaration, nor in a GMF. */
14731 error_at (token
->location
, "module-declaration only permitted as first"
14732 " declaration, or ending a global module fragment");
14734 cp_parser_skip_to_pragma_eol (parser
, token
);
14738 module_state
*mod
= cp_parser_module_name (parser
);
14739 tree attrs
= cp_parser_attributes_opt (parser
);
14741 mp_state
= MP_PURVIEW_IMPORTS
;
14742 if (!mod
|| !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
14745 declare_module (mod
, token
->location
, exporting
, attrs
, parse_in
);
14746 cp_parser_require_pragma_eol (parser
, token
);
14752 /* Import-declaration
14753 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14756 cp_parser_import_declaration (cp_parser
*parser
, module_parse mp_state
,
14759 /* We're a pseudo pragma. */
14760 parser
->lexer
->in_pragma
= true;
14761 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
14763 if (mp_state
== MP_PURVIEW
|| mp_state
== MP_PRIVATE
)
14765 error_at (token
->location
, "post-module-declaration"
14766 " imports must be contiguous");
14768 inform (token
->location
, "perhaps insert a line break, or other"
14769 " disambiguation, to prevent this being considered a"
14770 " module control-line");
14772 cp_parser_skip_to_pragma_eol (parser
, token
);
14774 else if (current_scope () != global_namespace
)
14776 error_at (token
->location
, "import-declaration must be at global scope");
14781 module_state
*mod
= cp_parser_module_name (parser
);
14782 tree attrs
= cp_parser_attributes_opt (parser
);
14784 if (!mod
|| !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
14786 cp_parser_require_pragma_eol (parser
, token
);
14788 if (parser
->in_unbraced_linkage_specification_p
)
14789 error_at (token
->location
, "import cannot appear directly in"
14790 " a linkage-specification");
14792 if (mp_state
== MP_PURVIEW_IMPORTS
|| mp_state
== MP_PRIVATE_IMPORTS
)
14794 /* Module-purview imports must not be from source inclusion
14797 && private_lookup_attribute ("__translated",
14798 strlen ("__translated"), attrs
))
14799 error_at (token
->location
, "post-module-declaration imports"
14800 " must not be include-translated");
14801 else if (!token
->main_source_p
)
14802 error_at (token
->location
, "post-module-declaration imports"
14803 " must not be from header inclusion");
14806 import_module (mod
, token
->location
, exporting
, attrs
, parse_in
);
14810 /* export-declaration.
14813 export { declaration-seq-opt } */
14816 cp_parser_module_export (cp_parser
*parser
)
14818 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
));
14819 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
14821 if (!module_interface_p ())
14822 error_at (token
->location
,
14823 "%qE may only occur after a module interface declaration",
14826 bool braced
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
);
14828 unsigned mk
= module_kind
;
14829 if (module_exporting_p ())
14830 error_at (token
->location
,
14831 "%qE may only occur once in an export declaration",
14833 module_kind
|= MK_EXPORTING
;
14837 cp_ensure_no_omp_declare_simd (parser
);
14838 cp_ensure_no_oacc_routine (parser
);
14840 cp_lexer_consume_token (parser
->lexer
);
14841 cp_parser_declaration_seq_opt (parser
);
14842 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
14846 /* Explicitly check if the next tokens might be a
14847 module-directive line, so we can give a clearer error message
14848 about why the directive will be rejected. */
14849 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID__MODULE
)
14850 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID__IMPORT
)
14851 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID__EXPORT
))
14852 error_at (token
->location
, "%<export%> not part of following"
14853 " module-directive");
14854 cp_parser_declaration (parser
, NULL_TREE
);
14860 /* Declarations [gram.dcl.dcl] */
14862 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
14863 is the top-level declaration sequence. That affects whether we
14864 deal with module-preamble.
14868 declaration-seq declaration */
14871 cp_parser_declaration_seq_opt (cp_parser
* parser
)
14875 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14877 if (token
->type
== CPP_CLOSE_BRACE
14878 || token
->type
== CPP_EOF
)
14881 cp_parser_toplevel_declaration (parser
);
14885 /* Parse a declaration.
14889 function-definition
14890 template-declaration
14891 explicit-instantiation
14892 explicit-specialization
14893 linkage-specification
14894 namespace-definition
14900 (all these are only allowed at the outermost level, check
14901 that semantically, for better diagnostics)
14903 module-export-declaration
14904 module-import-declaration
14910 __extension__ declaration */
14913 cp_parser_declaration (cp_parser
* parser
, tree prefix_attrs
)
14915 int saved_pedantic
;
14917 /* Check for the `__extension__' keyword. */
14918 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
14920 /* Parse the qualified declaration. */
14921 cp_parser_declaration (parser
, prefix_attrs
);
14922 /* Restore the PEDANTIC flag. */
14923 pedantic
= saved_pedantic
;
14928 /* Try to figure out what kind of declaration is present. */
14929 cp_token
*token1
= cp_lexer_peek_token (parser
->lexer
);
14930 cp_token
*token2
= (token1
->type
== CPP_EOF
14931 ? token1
: cp_lexer_peek_nth_token (parser
->lexer
, 2));
14933 if (token1
->type
== CPP_SEMICOLON
)
14935 cp_lexer_consume_token (parser
->lexer
);
14936 /* A declaration consisting of a single semicolon is invalid
14937 * before C++11. Allow it unless we're being pedantic. */
14938 if (cxx_dialect
< cxx11
)
14939 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
14942 else if (cp_lexer_nth_token_is (parser
->lexer
,
14943 cp_parser_skip_std_attribute_spec_seq (parser
,
14947 location_t attrs_loc
= token1
->location
;
14948 tree std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
14950 if (std_attrs
&& (flag_openmp
|| flag_openmp_simd
))
14952 gcc_assert (!parser
->lexer
->in_omp_attribute_pragma
);
14953 std_attrs
= cp_parser_handle_statement_omp_attributes (parser
,
14955 if (parser
->lexer
->in_omp_attribute_pragma
)
14957 cp_lexer
*lexer
= parser
->lexer
;
14958 while (parser
->lexer
->in_omp_attribute_pragma
)
14960 gcc_assert (cp_lexer_next_token_is (parser
->lexer
,
14962 cp_parser_pragma (parser
, pragma_external
, NULL
);
14964 cp_lexer_destroy (lexer
);
14968 if (std_attrs
!= NULL_TREE
&& !attribute_ignored_p (std_attrs
))
14969 warning_at (make_location (attrs_loc
, attrs_loc
, parser
->lexer
),
14970 OPT_Wattributes
, "attribute ignored");
14971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14972 cp_lexer_consume_token (parser
->lexer
);
14976 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14977 void *p
= obstack_alloc (&declarator_obstack
, 0);
14979 tree attributes
= NULL_TREE
;
14981 /* Conditionally, allow attributes to precede a linkage specification. */
14982 if (token1
->keyword
== RID_ATTRIBUTE
)
14984 cp_lexer_save_tokens (parser
->lexer
);
14985 attributes
= cp_parser_attributes_opt (parser
);
14986 cp_token
*t1
= cp_lexer_peek_token (parser
->lexer
);
14987 cp_token
*t2
= (t1
->type
== CPP_EOF
14988 ? t1
: cp_lexer_peek_nth_token (parser
->lexer
, 2));
14989 if (t1
->keyword
== RID_EXTERN
14990 && cp_parser_is_pure_string_literal (t2
))
14992 cp_lexer_commit_tokens (parser
->lexer
);
14993 /* We might have already been here. */
14994 if (!c_dialect_objc ())
14996 location_t where
= get_finish (t2
->location
);
14997 warning_at (token1
->location
, OPT_Wattributes
, "attributes are"
14998 " not permitted in this position");
14999 where
= linemap_position_for_loc_and_offset (line_table
,
15001 inform (where
, "attributes may be inserted here");
15002 attributes
= NULL_TREE
;
15009 cp_lexer_rollback_tokens (parser
->lexer
);
15010 attributes
= NULL_TREE
;
15013 /* If we already had some attributes, and we've added more, then prepend.
15014 Otherwise attributes just contains any that we just read. */
15018 TREE_CHAIN (prefix_attrs
) = attributes
;
15019 attributes
= prefix_attrs
;
15022 /* If the next token is `extern' and the following token is a string
15023 literal, then we have a linkage specification. */
15024 if (token1
->keyword
== RID_EXTERN
15025 && cp_parser_is_pure_string_literal (token2
))
15026 cp_parser_linkage_specification (parser
, attributes
);
15027 /* If the next token is `template', then we have either a template
15028 declaration, an explicit instantiation, or an explicit
15030 else if (token1
->keyword
== RID_TEMPLATE
)
15032 /* `template <>' indicates a template specialization. */
15033 if (token2
->type
== CPP_LESS
15034 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
15035 cp_parser_explicit_specialization (parser
);
15036 /* `template <' indicates a template declaration. */
15037 else if (token2
->type
== CPP_LESS
)
15038 cp_parser_template_declaration (parser
, /*member_p=*/false);
15039 /* Anything else must be an explicit instantiation. */
15041 cp_parser_explicit_instantiation (parser
);
15043 /* If the next token is `export', it's new-style modules or
15044 old-style template. */
15045 else if (token1
->keyword
== RID_EXPORT
)
15048 cp_parser_template_declaration (parser
, /*member_p=*/false);
15050 cp_parser_module_export (parser
);
15052 else if (cp_token_is_module_directive (token1
))
15054 bool exporting
= token1
->keyword
== RID__EXPORT
;
15055 cp_token
*next
= exporting
? token2
: token1
;
15057 cp_lexer_consume_token (parser
->lexer
);
15058 // In module purview this will be ill-formed.
15059 auto state
= (!named_module_p () ? MP_NOT_MODULE
15060 : module_purview_p () ? MP_PURVIEW
15062 if (next
->keyword
== RID__MODULE
)
15063 cp_parser_module_declaration (parser
, state
, exporting
);
15065 cp_parser_import_declaration (parser
, state
, exporting
);
15067 /* If the next token is `extern', 'static' or 'inline' and the one
15068 after that is `template', we have a GNU extended explicit
15069 instantiation directive. */
15070 else if (cp_parser_allow_gnu_extensions_p (parser
)
15071 && token2
->keyword
== RID_TEMPLATE
15072 && (token1
->keyword
== RID_EXTERN
15073 || token1
->keyword
== RID_STATIC
15074 || token1
->keyword
== RID_INLINE
))
15075 cp_parser_explicit_instantiation (parser
);
15076 /* If the next token is `namespace', check for a named or unnamed
15077 namespace definition. */
15078 else if (token1
->keyword
== RID_NAMESPACE
15079 && (/* A named namespace definition. */
15080 (token2
->type
== CPP_NAME
15081 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
15083 || (token2
->type
== CPP_OPEN_SQUARE
15084 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
15085 == CPP_OPEN_SQUARE
)
15086 /* An unnamed namespace definition. */
15087 || token2
->type
== CPP_OPEN_BRACE
15088 || token2
->keyword
== RID_ATTRIBUTE
))
15089 cp_parser_namespace_definition (parser
);
15090 /* An inline (associated) namespace definition. */
15091 else if (token2
->keyword
== RID_NAMESPACE
15092 && token1
->keyword
== RID_INLINE
)
15093 cp_parser_namespace_definition (parser
);
15094 /* Objective-C++ declaration/definition. */
15095 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
->keyword
))
15096 cp_parser_objc_declaration (parser
, attributes
);
15097 else if (c_dialect_objc ()
15098 && token1
->keyword
== RID_ATTRIBUTE
15099 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
15100 cp_parser_objc_declaration (parser
, attributes
);
15101 /* At this point we may have a template declared by a concept
15103 else if (flag_concepts
15104 && cp_parser_template_declaration_after_export (parser
,
15105 /*member_p=*/false))
15108 /* Try to parse a block-declaration, or a function-definition. */
15109 cp_parser_block_declaration (parser
, /*statement_p=*/false);
15111 /* Free any declarators allocated. */
15112 obstack_free (&declarator_obstack
, p
);
15115 /* Parse a namespace-scope declaration. */
15118 cp_parser_toplevel_declaration (cp_parser
* parser
)
15120 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15122 if (token
->type
== CPP_PRAGMA
)
15123 /* A top-level declaration can consist solely of a #pragma. A
15124 nested declaration cannot, so this is done here and not in
15125 cp_parser_declaration. (A #pragma at block scope is
15126 handled in cp_parser_statement.) */
15127 cp_parser_pragma (parser
, pragma_external
, NULL
);
15129 /* Parse the declaration itself. */
15130 cp_parser_declaration (parser
, NULL_TREE
);
15133 /* Parse a block-declaration.
15138 namespace-alias-definition
15145 __extension__ block-declaration
15150 static_assert-declaration
15152 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15153 part of a declaration-statement. */
15156 cp_parser_block_declaration (cp_parser
*parser
,
15159 int saved_pedantic
;
15161 /* Check for the `__extension__' keyword. */
15162 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
15164 /* Parse the qualified declaration. */
15165 cp_parser_block_declaration (parser
, statement_p
);
15166 /* Restore the PEDANTIC flag. */
15167 pedantic
= saved_pedantic
;
15172 /* Peek at the next token to figure out which kind of declaration is
15174 cp_token
*token1
= cp_lexer_peek_token (parser
->lexer
);
15177 /* If the next keyword is `asm', we have an asm-definition. */
15178 if (token1
->keyword
== RID_ASM
)
15181 cp_parser_commit_to_tentative_parse (parser
);
15182 cp_parser_asm_definition (parser
);
15184 /* If the next keyword is `namespace', we have a
15185 namespace-alias-definition. */
15186 else if (token1
->keyword
== RID_NAMESPACE
)
15187 cp_parser_namespace_alias_definition (parser
);
15188 /* If the next keyword is `using', we have a
15189 using-declaration, a using-directive, or an alias-declaration. */
15190 else if (token1
->keyword
== RID_USING
)
15195 cp_parser_commit_to_tentative_parse (parser
);
15196 /* If the token after `using' is `namespace', then we have a
15197 using-directive. */
15198 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15199 if (token2
->keyword
== RID_NAMESPACE
)
15200 cp_parser_using_directive (parser
);
15201 else if (token2
->keyword
== RID_ENUM
)
15202 cp_parser_using_enum (parser
);
15203 /* If the second token after 'using' is '=', then we have an
15204 alias-declaration. */
15205 else if (cxx_dialect
>= cxx11
15206 && token2
->type
== CPP_NAME
15207 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
15208 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
15209 cp_parser_alias_declaration (parser
);
15210 /* Otherwise, it's a using-declaration. */
15212 cp_parser_using_declaration (parser
,
15213 /*access_declaration_p=*/false);
15215 /* If the next keyword is `__label__' we have a misplaced label
15217 else if (token1
->keyword
== RID_LABEL
)
15219 cp_lexer_consume_token (parser
->lexer
);
15220 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
15221 cp_parser_skip_to_end_of_statement (parser
);
15222 /* If the next token is now a `;', consume it. */
15223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15224 cp_lexer_consume_token (parser
->lexer
);
15226 /* If the next token is `static_assert' we have a static assertion. */
15227 else if (token1
->keyword
== RID_STATIC_ASSERT
)
15228 cp_parser_static_assert (parser
, /*member_p=*/false);
15229 /* If the next tokens after attributes is `using namespace', then we have
15230 a using-directive. */
15231 else if ((attr_idx
= cp_parser_skip_std_attribute_spec_seq (parser
, 1)) != 1
15232 && cp_lexer_nth_token_is_keyword (parser
->lexer
, attr_idx
,
15234 && cp_lexer_nth_token_is_keyword (parser
->lexer
, attr_idx
+ 1,
15238 cp_parser_commit_to_tentative_parse (parser
);
15239 cp_parser_using_directive (parser
);
15241 /* Anything else must be a simple-declaration. */
15243 cp_parser_simple_declaration (parser
, !statement_p
,
15244 /*maybe_range_for_decl*/NULL
);
15247 /* Parse a simple-declaration.
15249 simple-declaration:
15250 decl-specifier-seq [opt] init-declarator-list [opt] ;
15251 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15252 brace-or-equal-initializer ;
15254 init-declarator-list:
15256 init-declarator-list , init-declarator
15258 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15259 function-definition as a simple-declaration.
15261 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15262 parsed declaration if it is an uninitialized single declarator not followed
15263 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15264 if present, will not be consumed. */
15267 cp_parser_simple_declaration (cp_parser
* parser
,
15268 bool function_definition_allowed_p
,
15269 tree
*maybe_range_for_decl
)
15271 cp_decl_specifier_seq decl_specifiers
;
15272 int declares_class_or_enum
;
15273 bool saw_declarator
;
15274 location_t comma_loc
= UNKNOWN_LOCATION
;
15275 location_t init_loc
= UNKNOWN_LOCATION
;
15277 if (maybe_range_for_decl
)
15278 *maybe_range_for_decl
= NULL_TREE
;
15280 /* Defer access checks until we know what is being declared; the
15281 checks for names appearing in the decl-specifier-seq should be
15282 done as if we were in the scope of the thing being declared. */
15283 push_deferring_access_checks (dk_deferred
);
15285 /* Parse the decl-specifier-seq. We have to keep track of whether
15286 or not the decl-specifier-seq declares a named class or
15287 enumeration type, since that is the only case in which the
15288 init-declarator-list is allowed to be empty.
15292 In a simple-declaration, the optional init-declarator-list can be
15293 omitted only when declaring a class or enumeration, that is when
15294 the decl-specifier-seq contains either a class-specifier, an
15295 elaborated-type-specifier, or an enum-specifier. */
15296 cp_parser_decl_specifier_seq (parser
,
15297 CP_PARSER_FLAGS_OPTIONAL
,
15299 &declares_class_or_enum
);
15300 /* We no longer need to defer access checks. */
15301 stop_deferring_access_checks ();
15303 cp_omp_declare_simd_data odsd
;
15304 if (decl_specifiers
.attributes
&& (flag_openmp
|| flag_openmp_simd
))
15305 cp_parser_handle_directive_omp_attributes (parser
,
15306 &decl_specifiers
.attributes
,
15309 /* In a block scope, a valid declaration must always have a
15310 decl-specifier-seq. By not trying to parse declarators, we can
15311 resolve the declaration/expression ambiguity more quickly. */
15312 if (!function_definition_allowed_p
15313 && !decl_specifiers
.any_specifiers_p
)
15315 cp_parser_error (parser
, "expected declaration");
15319 /* If the next two tokens are both identifiers, the code is
15320 erroneous. The usual cause of this situation is code like:
15324 where "T" should name a type -- but does not. */
15325 if (!decl_specifiers
.any_type_specifiers_p
15326 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
15328 /* If parsing tentatively, we should commit; we really are
15329 looking at a declaration. */
15330 cp_parser_commit_to_tentative_parse (parser
);
15335 cp_parser_maybe_commit_to_declaration (parser
, &decl_specifiers
);
15337 /* Look for C++17 decomposition declaration. */
15338 for (size_t n
= 1; ; n
++)
15339 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
15340 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
15342 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
15343 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
15344 && decl_specifiers
.any_specifiers_p
)
15347 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
15348 maybe_range_for_decl
,
15351 /* The next token should be either a `,' or a `;'. */
15352 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15353 /* If it's a `;', we are done. */
15354 if (token
->type
== CPP_SEMICOLON
)
15356 else if (maybe_range_for_decl
)
15358 if (*maybe_range_for_decl
== NULL_TREE
)
15359 *maybe_range_for_decl
= error_mark_node
;
15362 /* Anything else is an error. */
15365 /* If we have already issued an error message we don't need
15366 to issue another one. */
15367 if ((decl
!= error_mark_node
15368 && DECL_INITIAL (decl
) != error_mark_node
)
15369 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
15370 cp_parser_error (parser
, "expected %<;%>");
15371 /* Skip tokens until we reach the end of the statement. */
15372 cp_parser_skip_to_end_of_statement (parser
);
15373 /* If the next token is now a `;', consume it. */
15374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15375 cp_lexer_consume_token (parser
->lexer
);
15383 bool auto_specifier_p
;
15384 /* NULL_TREE if both variable and function declaration are allowed,
15385 error_mark_node if function declaration are not allowed and
15386 a FUNCTION_DECL that should be diagnosed if it is followed by
15387 variable declarations. */
15388 tree auto_function_declaration
;
15390 last_type
= NULL_TREE
;
15392 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
15393 auto_function_declaration
= NULL_TREE
;
15395 /* Keep going until we hit the `;' at the end of the simple
15397 saw_declarator
= false;
15398 while (cp_lexer_next_token_is_not (parser
->lexer
,
15402 bool function_definition_p
;
15404 tree auto_result
= NULL_TREE
;
15406 if (saw_declarator
)
15408 /* If we are processing next declarator, comma is expected */
15409 token
= cp_lexer_peek_token (parser
->lexer
);
15410 gcc_assert (token
->type
== CPP_COMMA
);
15411 cp_lexer_consume_token (parser
->lexer
);
15412 if (maybe_range_for_decl
)
15414 *maybe_range_for_decl
= error_mark_node
;
15415 if (comma_loc
== UNKNOWN_LOCATION
)
15416 comma_loc
= token
->location
;
15420 saw_declarator
= true;
15422 /* Parse the init-declarator. */
15423 decl
= cp_parser_init_declarator (parser
,
15424 CP_PARSER_FLAGS_NONE
,
15427 function_definition_allowed_p
,
15428 /*member_p=*/false,
15429 declares_class_or_enum
,
15430 &function_definition_p
,
15431 maybe_range_for_decl
,
15434 /* If an error occurred while parsing tentatively, exit quickly.
15435 (That usually happens when in the body of a function; each
15436 statement is treated as a declaration-statement until proven
15438 if (cp_parser_error_occurred (parser
))
15441 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
15443 /* If the init-declarator-list contains more than one
15444 init-declarator, they shall all form declarations of
15446 if (auto_function_declaration
== NULL_TREE
)
15447 auto_function_declaration
15448 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
15449 else if (TREE_CODE (decl
) == FUNCTION_DECL
15450 || auto_function_declaration
!= error_mark_node
)
15452 error_at (decl_specifiers
.locations
[ds_type_spec
],
15453 "non-variable %qD in declaration with more than one "
15454 "declarator with placeholder type",
15455 TREE_CODE (decl
) == FUNCTION_DECL
15456 ? decl
: auto_function_declaration
);
15457 auto_function_declaration
= error_mark_node
;
15462 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
15465 && last_type
!= error_mark_node
15466 && !same_type_p (auto_result
, last_type
))
15468 /* If the list of declarators contains more than one declarator,
15469 the type of each declared variable is determined as described
15470 above. If the type deduced for the template parameter U is not
15471 the same in each deduction, the program is ill-formed. */
15472 error_at (decl_specifiers
.locations
[ds_type_spec
],
15473 "inconsistent deduction for %qT: %qT and then %qT",
15474 decl_specifiers
.type
, last_type
, auto_result
);
15475 last_type
= error_mark_node
;
15478 last_type
= auto_result
;
15481 /* Handle function definitions specially. */
15482 if (function_definition_p
)
15484 /* If the next token is a `,', then we are probably
15485 processing something like:
15489 which is erroneous. */
15490 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15492 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15493 error_at (token
->location
,
15495 " declarations and function-definitions is forbidden");
15497 /* Otherwise, we're done with the list of declarators. */
15500 pop_deferring_access_checks ();
15501 cp_finalize_omp_declare_simd (parser
, &odsd
);
15505 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
15506 *maybe_range_for_decl
= decl
;
15507 /* The next token should be either a `,' or a `;'. */
15508 token
= cp_lexer_peek_token (parser
->lexer
);
15509 /* If it's a `,', there are more declarators to come. */
15510 if (token
->type
== CPP_COMMA
)
15511 /* will be consumed next time around */;
15512 /* If it's a `;', we are done. */
15513 else if (token
->type
== CPP_SEMICOLON
)
15515 else if (maybe_range_for_decl
)
15517 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
15518 permerror (decl_specifiers
.locations
[ds_type_spec
],
15519 "types may not be defined in a for-range-declaration");
15522 /* Anything else is an error. */
15525 /* If we have already issued an error message we don't need
15526 to issue another one. */
15527 if ((decl
!= error_mark_node
15528 && DECL_INITIAL (decl
) != error_mark_node
)
15529 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
15530 cp_parser_error (parser
, "expected %<,%> or %<;%>");
15531 /* Skip tokens until we reach the end of the statement. */
15532 cp_parser_skip_to_end_of_statement (parser
);
15533 /* If the next token is now a `;', consume it. */
15534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15535 cp_lexer_consume_token (parser
->lexer
);
15538 /* After the first time around, a function-definition is not
15539 allowed -- even if it was OK at first. For example:
15544 function_definition_allowed_p
= false;
15547 /* Issue an error message if no declarators are present, and the
15548 decl-specifier-seq does not itself declare a class or
15549 enumeration: [dcl.dcl]/3. */
15550 if (!saw_declarator
)
15552 if (cp_parser_declares_only_class_p (parser
))
15554 if (!declares_class_or_enum
15555 && decl_specifiers
.type
15556 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
15557 /* Ensure an error is issued anyway when finish_decltype_type,
15558 called via cp_parser_decl_specifier_seq, returns a class or
15559 an enumeration (c++/51786). */
15560 decl_specifiers
.type
= NULL_TREE
;
15561 shadow_tag (&decl_specifiers
);
15563 /* Perform any deferred access checks. */
15564 perform_deferred_access_checks (tf_warning_or_error
);
15567 /* Consume the `;'. */
15569 if (!maybe_range_for_decl
)
15570 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15571 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15573 if (init_loc
!= UNKNOWN_LOCATION
)
15574 error_at (init_loc
, "initializer in range-based %<for%> loop");
15575 if (comma_loc
!= UNKNOWN_LOCATION
)
15576 error_at (comma_loc
,
15577 "multiple declarations in range-based %<for%> loop");
15581 pop_deferring_access_checks ();
15582 cp_finalize_omp_declare_simd (parser
, &odsd
);
15585 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15586 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15590 cp_parser_decomposition_declaration (cp_parser
*parser
,
15591 cp_decl_specifier_seq
*decl_specifiers
,
15592 tree
*maybe_range_for_decl
,
15593 location_t
*init_loc
)
15595 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
15596 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15597 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
15599 /* Parse the identifier-list. */
15600 auto_vec
<cp_expr
, 10> v
;
15601 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
15604 cp_expr e
= cp_parser_identifier (parser
);
15605 if (e
.get_value () == error_mark_node
)
15608 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15610 cp_lexer_consume_token (parser
->lexer
);
15613 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15614 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15616 end_loc
= UNKNOWN_LOCATION
;
15617 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
15619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
15620 cp_lexer_consume_token (parser
->lexer
);
15623 cp_parser_skip_to_end_of_statement (parser
);
15624 return error_mark_node
;
15628 if (cxx_dialect
< cxx17
)
15629 pedwarn (loc
, OPT_Wc__17_extensions
,
15630 "structured bindings only available with "
15631 "%<-std=c++17%> or %<-std=gnu++17%>");
15634 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
15635 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
15636 declarator
->id_loc
= loc
;
15637 if (ref_qual
!= REF_QUAL_NONE
)
15638 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
15639 ref_qual
== REF_QUAL_RVALUE
,
15641 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
15642 NULL_TREE
, decl_specifiers
->attributes
,
15644 tree orig_decl
= decl
;
15648 cp_decl_specifier_seq decl_specs
;
15649 clear_decl_specs (&decl_specs
);
15650 decl_specs
.type
= make_auto ();
15652 FOR_EACH_VEC_ELT (v
, i
, e
)
15655 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
15656 sfk_none
, e
.get_location ());
15659 declarator
->u
.id
.unqualified_name
= e
.get_value ();
15660 declarator
->id_loc
= e
.get_location ();
15662 tree elt_pushed_scope
;
15663 tree decl2
= start_decl (declarator
, &decl_specs
, SD_DECOMPOSITION
,
15664 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
15665 if (decl2
== error_mark_node
)
15666 decl
= error_mark_node
;
15667 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
15669 /* Ensure we've diagnosed redeclaration if we aren't creating
15671 gcc_assert (errorcount
);
15672 decl
= error_mark_node
;
15676 if (elt_pushed_scope
)
15677 pop_scope (elt_pushed_scope
);
15682 error_at (loc
, "empty structured binding declaration");
15683 decl
= error_mark_node
;
15686 if (maybe_range_for_decl
== NULL
15687 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
15689 bool non_constant_p
= false, is_direct_init
= false;
15690 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15691 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
15693 if (initializer
== NULL_TREE
15694 || (TREE_CODE (initializer
) == TREE_LIST
15695 && TREE_CHAIN (initializer
))
15697 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
15698 && CONSTRUCTOR_NELTS (initializer
) != 1))
15700 error_at (loc
, "invalid initializer for structured binding "
15702 initializer
= error_mark_node
;
15705 if (decl
!= error_mark_node
)
15707 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
15708 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
15709 (is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
15710 cp_finish_decomp (decl
, prev
, v
.length ());
15713 else if (decl
!= error_mark_node
)
15715 *maybe_range_for_decl
= prev
;
15716 /* Ensure DECL_VALUE_EXPR is created for all the decls but
15717 the underlying DECL. */
15718 cp_finish_decomp (decl
, prev
, v
.length ());
15722 pop_scope (pushed_scope
);
15724 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
15726 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
15727 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
15733 /* Parse a decl-specifier-seq.
15735 decl-specifier-seq:
15736 decl-specifier-seq [opt] decl-specifier
15737 decl-specifier attribute-specifier-seq [opt] (C++11)
15740 storage-class-specifier
15751 Concepts Extension:
15756 Set *DECL_SPECS to a representation of the decl-specifier-seq.
15758 The parser flags FLAGS is used to control type-specifier parsing.
15760 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15763 1: one of the decl-specifiers is an elaborated-type-specifier
15764 (i.e., a type declaration)
15765 2: one of the decl-specifiers is an enum-specifier or a
15766 class-specifier (i.e., a type definition)
15771 cp_parser_decl_specifier_seq (cp_parser
* parser
,
15772 cp_parser_flags flags
,
15773 cp_decl_specifier_seq
*decl_specs
,
15774 int* declares_class_or_enum
)
15776 bool constructor_possible_p
= !parser
->in_declarator_p
;
15777 bool found_decl_spec
= false;
15778 cp_token
*start_token
= NULL
;
15781 /* Clear DECL_SPECS. */
15782 clear_decl_specs (decl_specs
);
15784 /* Assume no class or enumeration type is declared. */
15785 *declares_class_or_enum
= 0;
15787 /* Keep reading specifiers until there are no more to read. */
15790 bool constructor_p
;
15794 /* Peek at the next token. */
15795 token
= cp_lexer_peek_token (parser
->lexer
);
15797 /* Save the first token of the decl spec list for error
15800 start_token
= token
;
15801 /* Handle attributes. */
15802 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) == 0
15803 && cp_next_tokens_can_be_attribute_p (parser
))
15805 /* Parse the attributes. */
15806 tree attrs
= cp_parser_attributes_opt (parser
);
15808 /* In a sequence of declaration specifiers, c++11 attributes
15809 appertain to the type that precede them. In that case
15812 The attribute-specifier-seq affects the type only for
15813 the declaration it appears in, not other declarations
15814 involving the same type.
15816 But for now let's force the user to position the
15817 attribute either at the beginning of the declaration or
15818 after the declarator-id, which would clearly mean that it
15819 applies to the declarator. */
15820 if (cxx11_attribute_p (attrs
))
15822 if (!found_decl_spec
)
15823 /* The c++11 attribute is at the beginning of the
15824 declaration. It appertains to the entity being
15828 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
15830 /* This is an attribute following a
15831 class-specifier. */
15832 if (decl_specs
->type_definition_p
)
15833 warn_misplaced_attr_for_class_type (token
->location
,
15839 decl_specs
->std_attributes
15840 = attr_chainon (decl_specs
->std_attributes
, attrs
);
15841 if (decl_specs
->locations
[ds_std_attribute
] == 0)
15842 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
15848 decl_specs
->attributes
15849 = attr_chainon (decl_specs
->attributes
, attrs
);
15850 if (decl_specs
->locations
[ds_attribute
] == 0)
15851 decl_specs
->locations
[ds_attribute
] = token
->location
;
15854 /* Assume we will find a decl-specifier keyword. */
15855 found_decl_spec
= true;
15856 /* If the next token is an appropriate keyword, we can simply
15857 add it to the list. */
15858 switch (token
->keyword
)
15865 if (!at_class_scope_p ())
15867 gcc_rich_location
richloc (token
->location
);
15868 richloc
.add_fixit_remove ();
15869 error_at (&richloc
, "%<friend%> used outside of class");
15870 cp_lexer_purge_token (parser
->lexer
);
15875 /* Consume the token. */
15876 cp_lexer_consume_token (parser
->lexer
);
15880 case RID_CONSTEXPR
:
15882 cp_lexer_consume_token (parser
->lexer
);
15885 case RID_CONSTINIT
:
15887 cp_lexer_consume_token (parser
->lexer
);
15890 case RID_CONSTEVAL
:
15892 cp_lexer_consume_token (parser
->lexer
);
15897 cp_lexer_consume_token (parser
->lexer
);
15899 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
15902 /* Warn for concept as a decl-specifier. We'll rewrite these as
15903 concept declarations later. */
15904 if (!flag_concepts_ts
)
15906 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
15907 if (next
->keyword
== RID_BOOL
)
15908 pedwarn (next
->location
, 0, "the %<bool%> keyword is not "
15909 "allowed in a C++20 concept definition");
15911 pedwarn (token
->location
, 0, "C++20 concept definition syntax "
15912 "is %<concept <name> = <expr>%>");
15915 /* In C++20 a concept definition is just 'concept name = expr;'
15916 Support that syntax as a TS extension by pretending we've seen
15917 the 'bool' specifier. */
15918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
15919 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
)
15920 && !decl_specs
->any_type_specifiers_p
)
15922 cp_parser_set_decl_spec_type (decl_specs
, boolean_type_node
,
15923 token
, /*type_definition*/false);
15924 decl_specs
->any_type_specifiers_p
= true;
15928 /* function-specifier:
15935 cp_parser_function_specifier_opt (parser
, decl_specs
);
15942 /* Consume the token. */
15943 cp_lexer_consume_token (parser
->lexer
);
15945 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
15948 /* A constructor declarator cannot appear in a typedef. */
15949 constructor_possible_p
= false;
15950 /* The "typedef" keyword can only occur in a declaration; we
15951 may as well commit at this point. */
15952 cp_parser_commit_to_tentative_parse (parser
);
15954 if (decl_specs
->storage_class
!= sc_none
)
15955 decl_specs
->conflicting_specifiers_p
= true;
15958 /* storage-class-specifier:
15968 if (cxx_dialect
== cxx98
)
15970 /* Consume the token. */
15971 cp_lexer_consume_token (parser
->lexer
);
15973 /* Complain about `auto' as a storage specifier, if
15974 we're complaining about C++0x compatibility. */
15975 gcc_rich_location
richloc (token
->location
);
15976 richloc
.add_fixit_remove ();
15977 warning_at (&richloc
, OPT_Wc__11_compat
,
15978 "%<auto%> changes meaning in C++11; "
15979 "please remove it");
15981 /* Set the storage class anyway. */
15982 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
15986 /* C++0x auto type-specifier. */
15987 found_decl_spec
= false;
15994 /* Consume the token. */
15995 cp_lexer_consume_token (parser
->lexer
);
15996 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
16000 /* Consume the token. */
16002 cp_lexer_consume_token (parser
->lexer
);
16006 /* We did not yet find a decl-specifier yet. */
16007 found_decl_spec
= false;
16011 if (found_decl_spec
16012 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
16013 && token
->keyword
!= RID_CONSTEXPR
)
16014 error ("%qD invalid in condition", ridpointers
[token
->keyword
]);
16016 if (found_decl_spec
16017 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
16018 && token
->keyword
!= RID_MUTABLE
16019 && token
->keyword
!= RID_CONSTEXPR
16020 && token
->keyword
!= RID_CONSTEVAL
)
16021 error_at (token
->location
, "%qD invalid in lambda",
16022 ridpointers
[token
->keyword
]);
16025 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16027 /* Constructors are a special case. The `S' in `S()' is not a
16028 decl-specifier; it is the beginning of the declarator. */
16030 = (!found_decl_spec
16031 && constructor_possible_p
16032 && (cp_parser_constructor_declarator_p
16033 (parser
, flags
, decl_spec_seq_has_spec_p (decl_specs
,
16036 /* If we don't have a DECL_SPEC yet, then we must be looking at
16037 a type-specifier. */
16038 if (!found_decl_spec
&& !constructor_p
)
16040 int decl_spec_declares_class_or_enum
;
16041 bool is_cv_qualifier
;
16044 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
16045 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
16048 = cp_parser_type_specifier (parser
, flags
,
16050 /*is_declaration=*/true,
16051 &decl_spec_declares_class_or_enum
,
16053 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
16055 /* If this type-specifier referenced a user-defined type
16056 (a typedef, class-name, etc.), then we can't allow any
16057 more such type-specifiers henceforth.
16061 The longest sequence of decl-specifiers that could
16062 possibly be a type name is taken as the
16063 decl-specifier-seq of a declaration. The sequence shall
16064 be self-consistent as described below.
16068 As a general rule, at most one type-specifier is allowed
16069 in the complete decl-specifier-seq of a declaration. The
16070 only exceptions are the following:
16072 -- const or volatile can be combined with any other
16075 -- signed or unsigned can be combined with char, long,
16083 void g (const int Pc);
16085 Here, Pc is *not* part of the decl-specifier seq; it's
16086 the declarator. Therefore, once we see a type-specifier
16087 (other than a cv-qualifier), we forbid any additional
16088 user-defined types. We *do* still allow things like `int
16089 int' to be considered a decl-specifier-seq, and issue the
16090 error message later. */
16091 if (type_spec
&& !is_cv_qualifier
)
16092 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
16093 /* A constructor declarator cannot follow a type-specifier. */
16096 constructor_possible_p
= false;
16097 found_decl_spec
= true;
16098 if (!is_cv_qualifier
)
16099 decl_specs
->any_type_specifiers_p
= true;
16101 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
16102 error_at (token
->location
, "type-specifier invalid in lambda");
16106 /* If we still do not have a DECL_SPEC, then there are no more
16107 decl-specifiers. */
16108 if (!found_decl_spec
)
16111 if (decl_specs
->std_attributes
)
16113 error_at (decl_specs
->locations
[ds_std_attribute
],
16114 "standard attributes in middle of decl-specifiers");
16115 inform (decl_specs
->locations
[ds_std_attribute
],
16116 "standard attributes must precede the decl-specifiers to "
16117 "apply to the declaration, or follow them to apply to "
16121 decl_specs
->any_specifiers_p
= true;
16122 /* After we see one decl-specifier, further decl-specifiers are
16123 always optional. */
16124 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
16127 /* Don't allow a friend specifier with a class definition. */
16128 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
16129 && (*declares_class_or_enum
& 2))
16130 error_at (decl_specs
->locations
[ds_friend
],
16131 "class definition may not be declared a friend");
16134 /* Parse an (optional) storage-class-specifier.
16136 storage-class-specifier:
16145 storage-class-specifier:
16148 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16151 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
16153 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
16156 if (cxx_dialect
!= cxx98
)
16158 /* Fall through for C++98. */
16159 gcc_fallthrough ();
16166 /* Consume the token. */
16167 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16174 /* Parse an (optional) function-specifier.
16176 function-specifier:
16182 explicit(constant-expression)
16184 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16185 Updates DECL_SPECS, if it is non-NULL. */
16188 cp_parser_function_specifier_opt (cp_parser
* parser
,
16189 cp_decl_specifier_seq
*decl_specs
)
16191 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16192 switch (token
->keyword
)
16195 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
16199 /* 14.5.2.3 [temp.mem]
16201 A member function template shall not be virtual. */
16202 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16203 && current_class_type
)
16204 error_at (token
->location
, "templates may not be %<virtual%>");
16206 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
16211 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16212 /* If we see '(', it's C++20 explicit(bool). */
16214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16216 matching_parens parens
;
16217 parens
.consume_open (parser
);
16219 /* New types are not allowed in an explicit-specifier. */
16220 const char *saved_message
16221 = parser
->type_definition_forbidden_message
;
16222 parser
->type_definition_forbidden_message
16223 = G_("types may not be defined in explicit-specifier");
16225 if (cxx_dialect
< cxx20
)
16226 pedwarn (token
->location
, OPT_Wc__20_extensions
,
16227 "%<explicit(bool)%> only available with %<-std=c++20%> "
16228 "or %<-std=gnu++20%>");
16230 /* Parse the constant-expression. */
16231 expr
= cp_parser_constant_expression (parser
);
16233 /* Restore the saved message. */
16234 parser
->type_definition_forbidden_message
= saved_message
;
16235 parens
.require_close (parser
);
16238 /* The explicit-specifier explicit without a constant-expression is
16239 equivalent to the explicit-specifier explicit(true). */
16240 expr
= boolean_true_node
;
16243 "the constant-expression, if supplied, shall be a contextually
16244 converted constant expression of type bool." */
16245 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
16246 /* We could evaluate it -- mark the decl as appropriate. */
16247 if (expr
== boolean_true_node
)
16248 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
16249 else if (expr
== boolean_false_node
)
16250 /* Don't mark the decl as explicit. */;
16251 else if (decl_specs
)
16252 /* The expression was value-dependent. Remember it so that we can
16253 substitute it later. */
16254 decl_specs
->explicit_specifier
= expr
;
16262 /* Consume the token. */
16263 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16266 /* Parse a linkage-specification.
16268 linkage-specification:
16269 extern string-literal { declaration-seq [opt] }
16270 extern string-literal declaration */
16273 cp_parser_linkage_specification (cp_parser
* parser
, tree prefix_attr
)
16277 /* Look for the `extern' keyword. */
16278 cp_token
*extern_token
16279 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
16281 /* Look for the string-literal. */
16282 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
16283 linkage
= cp_parser_string_literal (parser
, false, false);
16285 /* Transform the literal into an identifier. If the literal is a
16286 wide-character string, or contains embedded NULs, then we can't
16287 handle it as the user wants. */
16288 if (linkage
== error_mark_node
16289 || strlen (TREE_STRING_POINTER (linkage
))
16290 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
16292 cp_parser_error (parser
, "invalid linkage-specification");
16293 /* Assume C++ linkage. */
16294 linkage
= lang_name_cplusplus
;
16297 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
16299 /* We're now using the new linkage. */
16300 unsigned saved_module
= module_kind
;
16301 module_kind
&= ~MK_ATTACH
;
16302 push_lang_context (linkage
);
16304 /* Preserve the location of the innermost linkage specification,
16305 tracking the locations of nested specifications via a local. */
16306 location_t saved_location
16307 = parser
->innermost_linkage_specification_location
;
16308 /* Construct a location ranging from the start of the "extern" to
16309 the end of the string-literal, with the caret at the start, e.g.:
16313 parser
->innermost_linkage_specification_location
16314 = make_location (extern_token
->location
,
16315 extern_token
->location
,
16316 get_finish (string_token
->location
));
16318 /* If the next token is a `{', then we're using the first
16320 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16322 cp_ensure_no_omp_declare_simd (parser
);
16323 cp_ensure_no_oacc_routine (parser
);
16325 /* Consume the `{' token. */
16326 matching_braces braces
;
16327 braces
.consume_open (parser
);
16328 /* Parse the declarations. */
16329 cp_parser_declaration_seq_opt (parser
);
16330 /* Look for the closing `}'. */
16331 braces
.require_close (parser
);
16333 /* Otherwise, there's just one declaration. */
16336 bool saved_in_unbraced_linkage_specification_p
;
16338 saved_in_unbraced_linkage_specification_p
16339 = parser
->in_unbraced_linkage_specification_p
;
16340 parser
->in_unbraced_linkage_specification_p
= true;
16341 cp_parser_declaration (parser
, prefix_attr
);
16342 parser
->in_unbraced_linkage_specification_p
16343 = saved_in_unbraced_linkage_specification_p
;
16346 /* We're done with the linkage-specification. */
16347 pop_lang_context ();
16348 module_kind
= saved_module
;
16350 /* Restore location of parent linkage specification, if any. */
16351 parser
->innermost_linkage_specification_location
= saved_location
;
16354 /* Parse a static_assert-declaration.
16356 static_assert-declaration:
16357 static_assert ( constant-expression , string-literal ) ;
16358 static_assert ( constant-expression ) ; (C++17)
16360 If MEMBER_P, this static_assert is a class member. */
16363 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
16366 location_t token_loc
;
16370 /* Peek at the `static_assert' token so we can keep track of exactly
16371 where the static assertion started. */
16372 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16374 /* Look for the `static_assert' keyword. */
16375 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
16379 /* We know we are in a static assertion; commit to any tentative
16381 if (cp_parser_parsing_tentatively (parser
))
16382 cp_parser_commit_to_tentative_parse (parser
);
16384 /* Parse the `(' starting the static assertion condition. */
16385 matching_parens parens
;
16386 parens
.require_open (parser
);
16388 /* Parse the constant-expression. Allow a non-constant expression
16389 here in order to give better diagnostics in finish_static_assert. */
16391 cp_parser_constant_expression (parser
,
16392 /*allow_non_constant_p=*/true,
16393 /*non_constant_p=*/&dummy
);
16395 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
16397 if (pedantic
&& cxx_dialect
< cxx17
)
16398 pedwarn (input_location
, OPT_Wc__17_extensions
,
16399 "%<static_assert%> without a message "
16400 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16402 cp_lexer_consume_token (parser
->lexer
);
16403 message
= build_string (1, "");
16404 TREE_TYPE (message
) = char_array_type_node
;
16405 fix_string_type (message
);
16409 /* Parse the separating `,'. */
16410 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
16412 /* Parse the string-literal message. */
16413 message
= cp_parser_string_literal (parser
,
16414 /*translate=*/false,
16417 /* A `)' completes the static assertion. */
16418 if (!parens
.require_close (parser
))
16419 cp_parser_skip_to_closing_parenthesis (parser
,
16420 /*recovering=*/true,
16421 /*or_comma=*/false,
16422 /*consume_paren=*/true);
16425 /* A semicolon terminates the declaration. */
16426 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16428 /* Get the location for the static assertion. Use that of the
16429 condition if available, otherwise, use that of the "static_assert"
16431 location_t assert_loc
= condition
.get_location ();
16432 if (assert_loc
== UNKNOWN_LOCATION
)
16433 assert_loc
= token_loc
;
16435 /* Complete the static assertion, which may mean either processing
16436 the static assert now or saving it for template instantiation. */
16437 finish_static_assert (condition
, message
, assert_loc
, member_p
,
16438 /*show_expr_p=*/false);
16441 /* Parse the expression in decltype ( expression ). */
16444 cp_parser_decltype_expr (cp_parser
*parser
,
16445 bool &id_expression_or_member_access_p
)
16447 cp_token
*id_expr_start_token
;
16450 /* First, try parsing an id-expression. */
16451 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
16452 cp_parser_parse_tentatively (parser
);
16453 expr
= cp_parser_id_expression (parser
,
16454 /*template_keyword_p=*/false,
16455 /*check_dependency_p=*/true,
16456 /*template_p=*/NULL
,
16457 /*declarator_p=*/false,
16458 /*optional_p=*/false);
16460 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
16462 bool non_integral_constant_expression_p
= false;
16463 tree id_expression
= expr
;
16465 const char *error_msg
;
16467 if (identifier_p (expr
))
16468 /* Lookup the name we got back from the id-expression. */
16469 expr
= cp_parser_lookup_name_simple (parser
, expr
,
16470 id_expr_start_token
->location
);
16472 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
16473 /* A template without args is not a complete id-expression. */
16474 expr
= error_mark_node
;
16477 && expr
!= error_mark_node
16478 && TREE_CODE (expr
) != TYPE_DECL
16479 && (TREE_CODE (expr
) != BIT_NOT_EXPR
16480 || !TYPE_P (TREE_OPERAND (expr
, 0)))
16481 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
16483 /* Complete lookup of the id-expression. */
16484 expr
= (finish_id_expression
16485 (id_expression
, expr
, parser
->scope
, &idk
,
16486 /*integral_constant_expression_p=*/false,
16487 /*allow_non_integral_constant_expression_p=*/true,
16488 &non_integral_constant_expression_p
,
16489 /*template_p=*/false,
16491 /*address_p=*/false,
16492 /*template_arg_p=*/false,
16494 id_expr_start_token
->location
));
16496 if (expr
== error_mark_node
)
16497 /* We found an id-expression, but it was something that we
16498 should not have found. This is an error, not something
16499 we can recover from, so note that we found an
16500 id-expression and we'll recover as gracefully as
16502 id_expression_or_member_access_p
= true;
16506 && expr
!= error_mark_node
16507 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
16508 /* We have an id-expression. */
16509 id_expression_or_member_access_p
= true;
16512 if (!id_expression_or_member_access_p
)
16514 /* Abort the id-expression parse. */
16515 cp_parser_abort_tentative_parse (parser
);
16517 /* Parsing tentatively, again. */
16518 cp_parser_parse_tentatively (parser
);
16520 /* Parse a class member access. */
16521 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
16522 /*cast_p=*/false, /*decltype*/true,
16523 /*member_access_only_p=*/true, NULL
);
16526 && expr
!= error_mark_node
16527 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
16528 /* We have an id-expression. */
16529 id_expression_or_member_access_p
= true;
16532 if (id_expression_or_member_access_p
)
16533 /* We have parsed the complete id-expression or member access. */
16534 cp_parser_parse_definitely (parser
);
16537 /* Abort our attempt to parse an id-expression or member access
16539 cp_parser_abort_tentative_parse (parser
);
16541 /* Parse a full expression. */
16542 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
16543 /*decltype_p=*/true);
16549 /* Parse a `decltype' type. Returns the type.
16551 decltype-specifier:
16552 decltype ( expression )
16554 decltype ( auto ) */
16557 cp_parser_decltype (cp_parser
*parser
)
16559 bool id_expression_or_member_access_p
= false;
16560 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
16562 if (start_token
->type
== CPP_DECLTYPE
)
16564 /* Already parsed. */
16565 cp_lexer_consume_token (parser
->lexer
);
16566 return saved_checks_value (start_token
->u
.tree_check_value
);
16569 /* Look for the `decltype' token. */
16570 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
16571 return error_mark_node
;
16573 /* Parse the opening `('. */
16574 matching_parens parens
;
16575 if (!parens
.require_open (parser
))
16576 return error_mark_node
;
16578 /* Since we're going to preserve any side-effects from this parse, set up a
16579 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16580 in the expression. */
16581 tentative_firewall
firewall (parser
);
16583 /* If in_declarator_p, a reparse as an expression might succeed (60361).
16584 Otherwise, commit now for better diagnostics. */
16585 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
16586 && !parser
->in_declarator_p
)
16587 cp_parser_commit_to_topmost_tentative_parse (parser
);
16589 push_deferring_access_checks (dk_deferred
);
16591 tree expr
= NULL_TREE
;
16593 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
)
16594 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
16596 /* decltype (auto) */
16597 cp_lexer_consume_token (parser
->lexer
);
16598 if (cxx_dialect
< cxx14
)
16600 error_at (start_token
->location
,
16601 "%<decltype(auto)%> type specifier only available with "
16602 "%<-std=c++14%> or %<-std=gnu++14%>");
16603 expr
= error_mark_node
;
16608 /* decltype (expression) */
16610 /* Types cannot be defined in a `decltype' expression. Save away the
16611 old message and set the new one. */
16612 const char *saved_message
= parser
->type_definition_forbidden_message
;
16613 parser
->type_definition_forbidden_message
16614 = G_("types may not be defined in %<decltype%> expressions");
16616 /* The restrictions on constant-expressions do not apply inside
16617 decltype expressions. */
16618 bool saved_integral_constant_expression_p
16619 = parser
->integral_constant_expression_p
;
16620 bool saved_non_integral_constant_expression_p
16621 = parser
->non_integral_constant_expression_p
;
16622 parser
->integral_constant_expression_p
= false;
16624 /* Within a parenthesized expression, a `>' token is always
16625 the greater-than operator. */
16626 bool saved_greater_than_is_operator_p
16627 = parser
->greater_than_is_operator_p
;
16628 parser
->greater_than_is_operator_p
= true;
16630 /* Don't synthesize an implicit template type parameter here. This
16631 could happen with C++23 code like
16633 void f(decltype(new auto{0}));
16635 where we want to deduce the auto right away so that the parameter
16636 is of type 'int *'. */
16637 auto cleanup
= make_temp_override
16638 (parser
->auto_is_implicit_function_template_parm_p
, false);
16640 /* Do not actually evaluate the expression. */
16641 ++cp_unevaluated_operand
;
16643 /* Do not warn about problems with the expression. */
16644 ++c_inhibit_evaluation_warnings
;
16646 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
16647 STRIP_ANY_LOCATION_WRAPPER (expr
);
16649 /* Go back to evaluating expressions. */
16650 --cp_unevaluated_operand
;
16651 --c_inhibit_evaluation_warnings
;
16653 /* The `>' token might be the end of a template-id or
16654 template-parameter-list now. */
16655 parser
->greater_than_is_operator_p
16656 = saved_greater_than_is_operator_p
;
16658 /* Restore the old message and the integral constant expression
16660 parser
->type_definition_forbidden_message
= saved_message
;
16661 parser
->integral_constant_expression_p
16662 = saved_integral_constant_expression_p
;
16663 parser
->non_integral_constant_expression_p
16664 = saved_non_integral_constant_expression_p
;
16667 /* Parse to the closing `)'. */
16668 if (expr
== error_mark_node
|| !parens
.require_close (parser
))
16670 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16671 /*consume_paren=*/true);
16672 expr
= error_mark_node
;
16675 /* If we got a parse error while tentative, bail out now. */
16676 if (cp_parser_error_occurred (parser
))
16678 pop_deferring_access_checks ();
16679 return error_mark_node
;
16684 expr
= make_decltype_auto ();
16686 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
16687 tf_warning_or_error
);
16689 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16691 start_token
->type
= CPP_DECLTYPE
;
16692 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16693 start_token
->tree_check_p
= true;
16694 start_token
->u
.tree_check_value
->value
= expr
;
16695 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16696 start_token
->keyword
= RID_MAX
;
16698 location_t loc
= start_token
->location
;
16699 loc
= make_location (loc
, loc
, parser
->lexer
);
16700 start_token
->location
= loc
;
16702 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
16704 pop_to_parent_deferring_access_checks ();
16709 /* Special member functions [gram.special] */
16711 /* Parse a conversion-function-id.
16713 conversion-function-id:
16714 operator conversion-type-id
16716 Returns an IDENTIFIER_NODE representing the operator. */
16719 cp_parser_conversion_function_id (cp_parser
* parser
)
16723 tree saved_qualifying_scope
;
16724 tree saved_object_scope
;
16725 tree pushed_scope
= NULL_TREE
;
16727 /* Look for the `operator' token. */
16728 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
16729 return error_mark_node
;
16730 /* When we parse the conversion-type-id, the current scope will be
16731 reset. However, we need that information in able to look up the
16732 conversion function later, so we save it here. */
16733 saved_scope
= parser
->scope
;
16734 saved_qualifying_scope
= parser
->qualifying_scope
;
16735 saved_object_scope
= parser
->object_scope
;
16736 /* We must enter the scope of the class so that the names of
16737 entities declared within the class are available in the
16738 conversion-type-id. For example, consider:
16745 S::operator I() { ... }
16747 In order to see that `I' is a type-name in the definition, we
16748 must be in the scope of `S'. */
16750 pushed_scope
= push_scope (saved_scope
);
16751 /* Parse the conversion-type-id. */
16752 type
= cp_parser_conversion_type_id (parser
);
16753 /* Leave the scope of the class, if any. */
16755 pop_scope (pushed_scope
);
16756 /* Restore the saved scope. */
16757 parser
->scope
= saved_scope
;
16758 parser
->qualifying_scope
= saved_qualifying_scope
;
16759 parser
->object_scope
= saved_object_scope
;
16760 /* If the TYPE is invalid, indicate failure. */
16761 if (type
== error_mark_node
)
16762 return error_mark_node
;
16763 return make_conv_op_name (type
);
16766 /* Parse a conversion-type-id:
16768 conversion-type-id:
16769 type-specifier-seq conversion-declarator [opt]
16771 Returns the TYPE specified. */
16774 cp_parser_conversion_type_id (cp_parser
* parser
)
16777 cp_decl_specifier_seq type_specifiers
;
16778 cp_declarator
*declarator
;
16779 tree type_specified
;
16780 const char *saved_message
;
16782 /* Parse the attributes. */
16783 attributes
= cp_parser_attributes_opt (parser
);
16785 saved_message
= parser
->type_definition_forbidden_message
;
16786 parser
->type_definition_forbidden_message
16787 = G_("types may not be defined in a conversion-type-id");
16789 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
16790 optional in conversion-type-id. */
16791 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16792 /*is_declaration=*/false,
16793 /*is_trailing_return=*/false,
16796 parser
->type_definition_forbidden_message
= saved_message
;
16798 /* If that didn't work, stop. */
16799 if (type_specifiers
.type
== error_mark_node
)
16800 return error_mark_node
;
16801 /* Parse the conversion-declarator. */
16802 declarator
= cp_parser_conversion_declarator_opt (parser
);
16804 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
16805 /*initialized=*/0, &attributes
);
16807 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
16809 /* Don't give this error when parsing tentatively. This happens to
16810 work because we always parse this definitively once. */
16811 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
16812 && type_uses_auto (type_specified
))
16814 if (cxx_dialect
< cxx14
)
16816 error ("invalid use of %<auto%> in conversion operator");
16817 return error_mark_node
;
16819 else if (template_parm_scope_p ())
16820 warning (0, "use of %<auto%> in member template "
16821 "conversion operator can never be deduced");
16824 return type_specified
;
16827 /* Parse an (optional) conversion-declarator.
16829 conversion-declarator:
16830 ptr-operator conversion-declarator [opt]
16834 static cp_declarator
*
16835 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
16837 enum tree_code code
;
16838 tree class_type
, std_attributes
= NULL_TREE
;
16839 cp_cv_quals cv_quals
;
16841 /* We don't know if there's a ptr-operator next, or not. */
16842 cp_parser_parse_tentatively (parser
);
16843 /* Try the ptr-operator. */
16844 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
16846 /* If it worked, look for more conversion-declarators. */
16847 if (cp_parser_parse_definitely (parser
))
16849 cp_declarator
*declarator
;
16851 /* Parse another optional declarator. */
16852 declarator
= cp_parser_conversion_declarator_opt (parser
);
16854 declarator
= cp_parser_make_indirect_declarator
16855 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
16863 /* Parse an (optional) ctor-initializer.
16866 : mem-initializer-list */
16869 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
16871 /* If the next token is not a `:', then there is no
16872 ctor-initializer. */
16873 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
16875 /* Do default initialization of any bases and members. */
16876 if (DECL_CONSTRUCTOR_P (current_function_decl
))
16877 finish_mem_initializers (NULL_TREE
);
16881 /* Consume the `:' token. */
16882 cp_lexer_consume_token (parser
->lexer
);
16883 /* And the mem-initializer-list. */
16884 cp_parser_mem_initializer_list (parser
);
16887 /* Parse a mem-initializer-list.
16889 mem-initializer-list:
16890 mem-initializer ... [opt]
16891 mem-initializer ... [opt] , mem-initializer-list */
16894 cp_parser_mem_initializer_list (cp_parser
* parser
)
16896 tree mem_initializer_list
= NULL_TREE
;
16897 tree target_ctor
= error_mark_node
;
16898 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16900 /* Let the semantic analysis code know that we are starting the
16901 mem-initializer-list. */
16902 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
16903 error_at (token
->location
,
16904 "only constructors take member initializers");
16906 /* Loop through the list. */
16909 tree mem_initializer
;
16911 token
= cp_lexer_peek_token (parser
->lexer
);
16912 /* Parse the mem-initializer. */
16913 mem_initializer
= cp_parser_mem_initializer (parser
);
16914 /* If the next token is a `...', we're expanding member initializers. */
16915 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
16917 || (mem_initializer
!= error_mark_node
16918 && check_for_bare_parameter_packs (TREE_PURPOSE
16919 (mem_initializer
))))
16921 /* Consume the `...'. */
16923 cp_lexer_consume_token (parser
->lexer
);
16925 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
16926 can be expanded but members cannot. */
16927 if (mem_initializer
!= error_mark_node
16928 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
16930 error_at (token
->location
,
16931 "cannot expand initializer for member %qD",
16932 TREE_PURPOSE (mem_initializer
));
16933 mem_initializer
= error_mark_node
;
16936 /* Construct the pack expansion type. */
16937 if (mem_initializer
!= error_mark_node
)
16938 mem_initializer
= make_pack_expansion (mem_initializer
);
16940 if (target_ctor
!= error_mark_node
16941 && mem_initializer
!= error_mark_node
)
16943 error ("mem-initializer for %qD follows constructor delegation",
16944 TREE_PURPOSE (mem_initializer
));
16945 mem_initializer
= error_mark_node
;
16947 /* Look for a target constructor. */
16948 if (mem_initializer
!= error_mark_node
16949 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
16950 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
16952 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
16953 if (mem_initializer_list
)
16955 error ("constructor delegation follows mem-initializer for %qD",
16956 TREE_PURPOSE (mem_initializer_list
));
16957 mem_initializer
= error_mark_node
;
16959 target_ctor
= mem_initializer
;
16961 /* Add it to the list, unless it was erroneous. */
16962 if (mem_initializer
!= error_mark_node
)
16964 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
16965 mem_initializer_list
= mem_initializer
;
16967 /* If the next token is not a `,', we're done. */
16968 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16970 /* Consume the `,' token. */
16971 cp_lexer_consume_token (parser
->lexer
);
16974 /* Perform semantic analysis. */
16975 if (DECL_CONSTRUCTOR_P (current_function_decl
))
16976 finish_mem_initializers (mem_initializer_list
);
16979 /* Parse a mem-initializer.
16982 mem-initializer-id ( expression-list [opt] )
16983 mem-initializer-id braced-init-list
16988 ( expression-list [opt] )
16990 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
16991 class) or FIELD_DECL (for a non-static data member) to initialize;
16992 the TREE_VALUE is the expression-list. An empty initialization
16993 list is represented by void_list_node. */
16996 cp_parser_mem_initializer (cp_parser
* parser
)
16998 tree mem_initializer_id
;
16999 tree expression_list
;
17001 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17003 /* Find out what is being initialized. */
17004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17006 permerror (token
->location
,
17007 "anachronistic old-style base class initializer");
17008 mem_initializer_id
= NULL_TREE
;
17012 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
17013 if (mem_initializer_id
== error_mark_node
)
17014 return mem_initializer_id
;
17016 member
= expand_member_init (mem_initializer_id
);
17017 if (member
&& !DECL_P (member
))
17018 in_base_initializer
= 1;
17020 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17022 bool expr_non_constant_p
;
17023 cp_lexer_set_source_position (parser
->lexer
);
17024 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
17025 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
17026 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
17027 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
17031 vec
<tree
, va_gc
> *vec
;
17032 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
17034 /*allow_expansion_p=*/true,
17035 /*non_constant_p=*/NULL
,
17036 /*close_paren_loc=*/NULL
,
17037 /*wrap_locations_p=*/true);
17039 return error_mark_node
;
17040 expression_list
= build_tree_list_vec (vec
);
17041 release_tree_vector (vec
);
17044 if (expression_list
== error_mark_node
)
17045 return error_mark_node
;
17046 if (!expression_list
)
17047 expression_list
= void_type_node
;
17049 in_base_initializer
= 0;
17052 return error_mark_node
;
17053 tree node
= build_tree_list (member
, expression_list
);
17055 /* We can't attach the source location of this initializer directly to
17056 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
17057 within the TREE_TYPE of the list node. */
17059 = make_location (token
->location
, token
->location
, parser
->lexer
);
17060 tree dummy
= build0 (EMPTY_CLASS_EXPR
, NULL_TREE
);
17061 SET_EXPR_LOCATION (dummy
, loc
);
17062 TREE_TYPE (node
) = dummy
;
17067 /* Parse a mem-initializer-id.
17069 mem-initializer-id:
17070 :: [opt] nested-name-specifier [opt] class-name
17071 decltype-specifier (C++11)
17074 Returns a TYPE indicating the class to be initialized for the first
17075 production (and the second in C++11). Returns an IDENTIFIER_NODE
17076 indicating the data member to be initialized for the last production. */
17079 cp_parser_mem_initializer_id (cp_parser
* parser
)
17081 bool global_scope_p
;
17082 bool nested_name_specifier_p
;
17083 bool template_p
= false;
17086 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17088 /* `typename' is not allowed in this context ([temp.res]). */
17089 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
17091 error_at (token
->location
,
17092 "keyword %<typename%> not allowed in this context (a qualified "
17093 "member initializer is implicitly a type)");
17094 cp_lexer_consume_token (parser
->lexer
);
17096 /* Look for the optional `::' operator. */
17098 = (cp_parser_global_scope_opt (parser
,
17099 /*current_scope_valid_p=*/false)
17101 /* Look for the optional nested-name-specifier. The simplest way to
17106 The keyword `typename' is not permitted in a base-specifier or
17107 mem-initializer; in these contexts a qualified name that
17108 depends on a template-parameter is implicitly assumed to be a
17111 is to assume that we have seen the `typename' keyword at this
17113 nested_name_specifier_p
17114 = (cp_parser_nested_name_specifier_opt (parser
,
17115 /*typename_keyword_p=*/true,
17116 /*check_dependency_p=*/true,
17118 /*is_declaration=*/true)
17120 if (nested_name_specifier_p
)
17121 template_p
= cp_parser_optional_template_keyword (parser
);
17122 /* If there is a `::' operator or a nested-name-specifier, then we
17123 are definitely looking for a class-name. */
17124 if (global_scope_p
|| nested_name_specifier_p
)
17125 return cp_parser_class_name (parser
,
17126 /*typename_keyword_p=*/true,
17127 /*template_keyword_p=*/template_p
,
17129 /*check_dependency_p=*/true,
17130 /*class_head_p=*/false,
17131 /*is_declaration=*/true);
17132 /* Otherwise, we could also be looking for an ordinary identifier. */
17133 cp_parser_parse_tentatively (parser
);
17134 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
17135 /* Try a decltype-specifier. */
17136 id
= cp_parser_decltype (parser
);
17138 /* Otherwise, try a class-name. */
17139 id
= cp_parser_class_name (parser
,
17140 /*typename_keyword_p=*/true,
17141 /*template_keyword_p=*/false,
17143 /*check_dependency_p=*/true,
17144 /*class_head_p=*/false,
17145 /*is_declaration=*/true);
17146 /* If we found one, we're done. */
17147 if (cp_parser_parse_definitely (parser
))
17149 /* Otherwise, look for an ordinary identifier. */
17150 return cp_parser_identifier (parser
);
17153 /* Overloading [gram.over] */
17155 /* Parse an operator-function-id.
17157 operator-function-id:
17160 Returns an IDENTIFIER_NODE for the operator which is a
17161 human-readable spelling of the identifier, e.g., `operator +'. */
17164 cp_parser_operator_function_id (cp_parser
* parser
)
17166 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17167 /* Look for the `operator' keyword. */
17168 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
17169 return error_mark_node
;
17170 /* And then the name of the operator itself. */
17171 return cp_parser_operator (parser
, start_loc
);
17174 /* Return an identifier node for a user-defined literal operator.
17175 The suffix identifier is chained to the operator name identifier. */
17178 cp_literal_operator_id (const char* name
)
17181 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
17182 + strlen (name
) + 10);
17183 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
17184 identifier
= get_identifier (buffer
);
17185 XDELETEVEC (buffer
);
17190 /* Parse an operator.
17193 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17194 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17195 || ++ -- , ->* -> () []
17202 Returns an IDENTIFIER_NODE for the operator which is a
17203 human-readable spelling of the identifier, e.g., `operator +'. */
17206 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
17208 tree id
= NULL_TREE
;
17212 /* Peek at the next token. */
17213 token
= cp_lexer_peek_token (parser
->lexer
);
17215 location_t end_loc
= token
->location
;
17217 /* Figure out which operator we have. */
17218 enum tree_code op
= ERROR_MARK
;
17219 bool assop
= false;
17220 bool consumed
= false;
17221 switch (token
->type
)
17225 /* The keyword should be either `new', `delete' or `co_await'. */
17226 if (token
->keyword
== RID_NEW
)
17228 else if (token
->keyword
== RID_DELETE
)
17230 else if (token
->keyword
== RID_CO_AWAIT
)
17231 op
= CO_AWAIT_EXPR
;
17235 /* Consume the `new', `delete' or co_await token. */
17236 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
17238 /* Peek at the next token. */
17239 token
= cp_lexer_peek_token (parser
->lexer
);
17240 /* If it's a `[' token then this is the array variant of the
17242 if (token
->type
== CPP_OPEN_SQUARE
17243 && op
!= CO_AWAIT_EXPR
)
17245 /* Consume the `[' token. */
17246 cp_lexer_consume_token (parser
->lexer
);
17247 /* Look for the `]' token. */
17248 if (cp_token
*close_token
17249 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17250 end_loc
= close_token
->location
;
17251 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
17270 op
= TRUNC_DIV_EXPR
;
17274 op
= TRUNC_MOD_EXPR
;
17294 op
= TRUTH_NOT_EXPR
;
17327 op
= TRUNC_DIV_EXPR
;
17332 op
= TRUNC_MOD_EXPR
;
17358 case CPP_LSHIFT_EQ
:
17363 case CPP_RSHIFT_EQ
:
17380 case CPP_GREATER_EQ
:
17384 case CPP_SPACESHIP
:
17385 op
= SPACESHIP_EXPR
;
17389 op
= TRUTH_ANDIF_EXPR
;
17393 op
= TRUTH_ORIF_EXPR
;
17396 case CPP_PLUS_PLUS
:
17397 op
= POSTINCREMENT_EXPR
;
17400 case CPP_MINUS_MINUS
:
17401 op
= PREDECREMENT_EXPR
;
17405 op
= COMPOUND_EXPR
;
17408 case CPP_DEREF_STAR
:
17413 op
= COMPONENT_REF
;
17418 /* Consume the `?'. */
17419 cp_lexer_consume_token (parser
->lexer
);
17420 /* Look for the matching `:'. */
17421 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
17425 case CPP_OPEN_PAREN
:
17427 /* Consume the `('. */
17428 matching_parens parens
;
17429 parens
.consume_open (parser
);
17430 /* Look for the matching `)'. */
17431 token
= parens
.require_close (parser
);
17433 end_loc
= token
->location
;
17439 case CPP_OPEN_SQUARE
:
17440 /* Consume the `['. */
17441 cp_lexer_consume_token (parser
->lexer
);
17442 /* Look for the matching `]'. */
17443 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
17445 end_loc
= token
->location
;
17450 case CPP_UTF8STRING
:
17451 case CPP_UTF8STRING_USERDEF
:
17458 case CPP_STRING_USERDEF
:
17459 case CPP_WSTRING_USERDEF
:
17460 case CPP_STRING16_USERDEF
:
17461 case CPP_STRING32_USERDEF
:
17467 if (cxx_dialect
== cxx98
)
17468 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
17470 /* Consume the string. */
17471 str
= cp_parser_string_literal (parser
, /*translate=*/true,
17472 /*wide_ok=*/true, /*lookup_udlit=*/false);
17473 if (str
== error_mark_node
)
17474 return error_mark_node
;
17475 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
17477 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
17478 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
17479 end_loc
= str
.get_location ();
17484 /* Look for the suffix identifier. */
17485 token
= cp_lexer_peek_token (parser
->lexer
);
17486 if (token
->type
== CPP_NAME
)
17488 id
= cp_parser_identifier (parser
);
17489 end_loc
= token
->location
;
17491 else if (token
->type
== CPP_KEYWORD
)
17493 error ("unexpected keyword;"
17494 " remove space between quotes and suffix identifier");
17495 return error_mark_node
;
17499 error ("expected suffix identifier");
17500 return error_mark_node
;
17503 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17504 (TREE_TYPE (TREE_TYPE (string_tree
))));
17505 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
17508 error ("expected empty string after %<operator%> keyword");
17509 return error_mark_node
;
17511 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
17514 error ("invalid encoding prefix in literal operator");
17515 return error_mark_node
;
17517 if (id
!= error_mark_node
)
17519 const char *name
= IDENTIFIER_POINTER (id
);
17520 id
= cp_literal_operator_id (name
);
17522 /* Generate a location of the form:
17523 "" _suffix_identifier
17524 ^~~~~~~~~~~~~~~~~~~~~
17525 with caret == start at the start token, finish at the end of the
17526 suffix identifier. */
17527 location_t combined_loc
17528 = make_location (start_loc
, start_loc
, parser
->lexer
);
17529 return cp_expr (id
, combined_loc
);
17533 /* Anything else is an error. */
17537 /* If we have selected an identifier, we need to consume the
17539 if (op
!= ERROR_MARK
)
17541 id
= ovl_op_identifier (assop
, op
);
17543 cp_lexer_consume_token (parser
->lexer
);
17545 /* Otherwise, no valid operator name was present. */
17548 cp_parser_error (parser
, "expected operator");
17549 id
= error_mark_node
;
17552 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
17553 return cp_expr (id
, start_loc
);
17556 /* Parse a template-declaration.
17558 template-declaration:
17559 export [opt] template < template-parameter-list > declaration
17561 If MEMBER_P is TRUE, this template-declaration occurs within a
17564 The grammar rule given by the standard isn't correct. What
17565 is really meant is:
17567 template-declaration:
17568 export [opt] template-parameter-list-seq
17569 decl-specifier-seq [opt] init-declarator [opt] ;
17570 export [opt] template-parameter-list-seq
17571 function-definition
17573 template-parameter-list-seq:
17574 template-parameter-list-seq [opt]
17575 template < template-parameter-list >
17577 Concept Extensions:
17579 template-parameter-list-seq:
17580 template < template-parameter-list > requires-clause [opt]
17583 requires logical-or-expression */
17586 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
17588 /* Check for `export'. */
17589 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
17591 /* Consume the `export' token. */
17592 cp_lexer_consume_token (parser
->lexer
);
17593 /* Warn that this use of export is deprecated. */
17594 if (cxx_dialect
< cxx11
)
17595 warning (0, "keyword %<export%> not implemented, and will be ignored");
17596 else if (cxx_dialect
< cxx20
)
17597 warning (0, "keyword %<export%> is deprecated, and is ignored");
17599 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17602 cp_parser_template_declaration_after_export (parser
, member_p
);
17605 /* Parse a template-parameter-list.
17607 template-parameter-list:
17609 template-parameter-list , template-parameter
17611 Returns a TREE_LIST. Each node represents a template parameter.
17612 The nodes are connected via their TREE_CHAINs. */
17615 cp_parser_template_parameter_list (cp_parser
* parser
)
17617 tree parameter_list
= NULL_TREE
;
17619 /* Don't create wrapper nodes within a template-parameter-list,
17620 since we don't want to have different types based on the
17621 spelling location of constants and decls within them. */
17622 auto_suppress_location_wrappers sentinel
;
17624 begin_template_parm_list ();
17626 /* The loop below parses the template parms. We first need to know
17627 the total number of template parms to be able to compute proper
17628 canonical types of each dependent type. So after the loop, when
17629 we know the total number of template parms,
17630 end_template_parm_list computes the proper canonical types and
17631 fixes up the dependent types accordingly. */
17636 bool is_parameter_pack
;
17637 location_t parm_loc
;
17639 /* Parse the template-parameter. */
17640 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17641 parameter
= cp_parser_template_parameter (parser
,
17643 &is_parameter_pack
);
17644 /* Add it to the list. */
17645 if (parameter
!= error_mark_node
)
17646 parameter_list
= process_template_parm (parameter_list
,
17650 is_parameter_pack
);
17653 tree err_parm
= build_tree_list (parameter
, parameter
);
17654 parameter_list
= chainon (parameter_list
, err_parm
);
17657 /* If the next token is not a `,', we're done. */
17658 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17660 /* Otherwise, consume the `,' token. */
17661 cp_lexer_consume_token (parser
->lexer
);
17664 return end_template_parm_list (parameter_list
);
17667 /* Parse a introduction-list.
17670 introduced-parameter
17671 introduction-list , introduced-parameter
17673 introduced-parameter:
17674 ...[opt] identifier
17676 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
17677 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
17678 WILDCARD_DECL will also have DECL_NAME set and token location in
17679 DECL_SOURCE_LOCATION. */
17682 cp_parser_introduction_list (cp_parser
*parser
)
17684 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
17688 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
17690 cp_lexer_consume_token (parser
->lexer
);
17692 tree identifier
= cp_parser_identifier (parser
);
17693 if (identifier
== error_mark_node
)
17696 /* Build placeholder. */
17697 tree parm
= build_nt (WILDCARD_DECL
);
17698 DECL_SOURCE_LOCATION (parm
)
17699 = cp_lexer_peek_token (parser
->lexer
)->location
;
17700 DECL_NAME (parm
) = identifier
;
17701 WILDCARD_PACK_P (parm
) = is_pack
;
17702 vec_safe_push (introduction_vec
, parm
);
17704 /* If the next token is not a `,', we're done. */
17705 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17707 /* Otherwise, consume the `,' token. */
17708 cp_lexer_consume_token (parser
->lexer
);
17711 /* Convert the vec into a TREE_VEC. */
17712 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
17715 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
17716 TREE_VEC_ELT (introduction_list
, n
) = parm
;
17718 release_tree_vector (introduction_vec
);
17719 return introduction_list
;
17722 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17723 is an abstract declarator. */
17725 static inline cp_declarator
*
17726 get_id_declarator (cp_declarator
*declarator
)
17728 cp_declarator
*d
= declarator
;
17729 while (d
&& d
->kind
!= cdk_id
)
17734 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17735 is an abstract declarator. */
17738 get_unqualified_id (cp_declarator
*declarator
)
17740 declarator
= get_id_declarator (declarator
);
17742 return declarator
->u
.id
.unqualified_name
;
17747 /* Returns true if TYPE would declare a constrained constrained-parameter. */
17750 is_constrained_parameter (tree type
)
17753 && TREE_CODE (type
) == TYPE_DECL
17754 && CONSTRAINED_PARM_CONCEPT (type
)
17755 && DECL_P (CONSTRAINED_PARM_CONCEPT (type
)));
17758 /* Returns true if PARM declares a constrained-parameter. */
17761 is_constrained_parameter (cp_parameter_declarator
*parm
)
17763 return is_constrained_parameter (parm
->decl_specifiers
.type
);
17766 /* Check that the type parameter is only a declarator-id, and that its
17767 type is not cv-qualified. */
17770 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
17771 cp_parameter_declarator
*parm
)
17773 if (!parm
->declarator
)
17776 if (parm
->declarator
->kind
!= cdk_id
)
17778 cp_parser_error (parser
, "invalid constrained type parameter");
17782 /* Don't allow cv-qualified type parameters. */
17783 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
17784 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
17786 cp_parser_error (parser
, "cv-qualified type parameter");
17793 /* Finish parsing/processing a template type parameter and checking
17794 various restrictions. */
17797 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
17799 cp_parameter_declarator
* parmdecl
)
17801 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
17802 return finish_template_type_parm (class_type_node
, id
);
17804 return error_mark_node
;
17808 finish_constrained_template_template_parm (tree proto
, tree id
)
17810 /* FIXME: This should probably be copied, and we may need to adjust
17811 the template parameter depths. */
17812 tree saved_parms
= current_template_parms
;
17813 begin_template_parm_list ();
17814 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
17815 end_template_parm_list ();
17817 tree parm
= finish_template_template_parm (class_type_node
, id
);
17818 current_template_parms
= saved_parms
;
17823 /* Finish parsing/processing a template template parameter by borrowing
17824 the template parameter list from the prototype parameter. */
17827 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
17830 cp_parameter_declarator
*parmdecl
)
17832 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
17833 return error_mark_node
;
17834 return finish_constrained_template_template_parm (proto
, id
);
17837 /* Create a new non-type template parameter from the given PARM
17841 cp_parser_constrained_non_type_template_parm (bool *is_non_type
,
17842 cp_parameter_declarator
*parm
)
17844 *is_non_type
= true;
17845 cp_declarator
*decl
= parm
->declarator
;
17846 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
17847 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
17848 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
17851 /* Build a constrained template parameter based on the PARMDECL
17852 declarator. The type of PARMDECL is the constrained type, which
17853 refers to the prototype template parameter that ultimately
17854 specifies the type of the declared parameter. */
17857 finish_constrained_parameter (cp_parser
*parser
,
17858 cp_parameter_declarator
*parmdecl
,
17861 tree decl
= parmdecl
->decl_specifiers
.type
;
17862 tree id
= get_unqualified_id (parmdecl
->declarator
);
17863 tree def
= parmdecl
->default_argument
;
17864 tree proto
= DECL_INITIAL (decl
);
17866 /* Build the parameter. Return an error if the declarator was invalid. */
17868 if (TREE_CODE (proto
) == TYPE_DECL
)
17869 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
17870 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
17871 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
17874 parm
= cp_parser_constrained_non_type_template_parm (is_non_type
, parmdecl
);
17875 if (parm
== error_mark_node
)
17876 return error_mark_node
;
17878 /* Finish the parameter decl and create a node attaching the
17879 default argument and constraint. */
17880 parm
= build_tree_list (def
, parm
);
17881 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
17886 /* Returns true if the parsed type actually represents the declaration
17887 of a type template-parameter. */
17890 declares_constrained_type_template_parameter (tree type
)
17892 return (is_constrained_parameter (type
)
17893 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
17896 /* Returns true if the parsed type actually represents the declaration of
17897 a template template-parameter. */
17900 declares_constrained_template_template_parameter (tree type
)
17902 return (is_constrained_parameter (type
)
17903 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
17906 /* Parse a default argument for a type template-parameter.
17907 Note that diagnostics are handled in cp_parser_template_parameter. */
17910 cp_parser_default_type_template_argument (cp_parser
*parser
)
17912 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
17914 /* Consume the `=' token. */
17915 cp_lexer_consume_token (parser
->lexer
);
17917 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17919 /* Tell cp_parser_lambda_expression this is a default argument. */
17920 auto lvf
= make_temp_override (parser
->local_variables_forbidden_p
);
17921 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
17923 /* Parse the default-argument. */
17924 push_deferring_access_checks (dk_no_deferred
);
17925 tree default_argument
= cp_parser_type_id (parser
,
17926 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
17928 pop_deferring_access_checks ();
17930 if (flag_concepts
&& type_uses_auto (default_argument
))
17932 error_at (token
->location
,
17933 "invalid use of %<auto%> in default template argument");
17934 return error_mark_node
;
17937 return default_argument
;
17940 /* Parse a default argument for a template template-parameter. */
17943 cp_parser_default_template_template_argument (cp_parser
*parser
)
17945 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
17949 /* Consume the `='. */
17950 cp_lexer_consume_token (parser
->lexer
);
17951 /* Parse the id-expression. */
17952 push_deferring_access_checks (dk_no_deferred
);
17953 /* save token before parsing the id-expression, for error
17955 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
17956 tree default_argument
17957 = cp_parser_id_expression (parser
,
17958 /*template_keyword_p=*/false,
17959 /*check_dependency_p=*/true,
17960 /*template_p=*/&is_template
,
17961 /*declarator_p=*/false,
17962 /*optional_p=*/false);
17963 if (TREE_CODE (default_argument
) == TYPE_DECL
)
17964 /* If the id-expression was a template-id that refers to
17965 a template-class, we already have the declaration here,
17966 so no further lookup is needed. */
17969 /* Look up the name. */
17971 = cp_parser_lookup_name (parser
, default_argument
,
17973 /*is_template=*/is_template
,
17974 /*is_namespace=*/false,
17975 /*check_dependency=*/true,
17976 /*ambiguous_decls=*/NULL
,
17978 /* See if the default argument is valid. */
17979 default_argument
= check_template_template_default_arg (default_argument
);
17980 pop_deferring_access_checks ();
17981 return default_argument
;
17984 /* Parse a template-parameter.
17986 template-parameter:
17988 parameter-declaration
17990 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
17991 the parameter. The TREE_PURPOSE is the default value, if any.
17992 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
17993 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
17994 set to true iff this parameter is a parameter pack. */
17997 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
17998 bool *is_parameter_pack
)
18001 cp_parameter_declarator
*parameter_declarator
;
18004 /* Assume it is a type parameter or a template parameter. */
18005 *is_non_type
= false;
18006 /* Assume it not a parameter pack. */
18007 *is_parameter_pack
= false;
18008 /* Peek at the next token. */
18009 token
= cp_lexer_peek_token (parser
->lexer
);
18010 /* If it is `template', we have a type-parameter. */
18011 if (token
->keyword
== RID_TEMPLATE
)
18012 return cp_parser_type_parameter (parser
, is_parameter_pack
);
18013 /* If it is `class' or `typename' we do not know yet whether it is a
18014 type parameter or a non-type parameter. Consider:
18016 template <typename T, typename T::X X> ...
18020 template <class C, class D*> ...
18022 Here, the first parameter is a type parameter, and the second is
18023 a non-type parameter. We can tell by looking at the token after
18024 the identifier -- if it is a `,', `=', or `>' then we have a type
18026 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
18028 /* Peek at the token after `class' or `typename'. */
18029 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18030 /* If it's an ellipsis, we have a template type parameter
18032 if (token
->type
== CPP_ELLIPSIS
)
18033 return cp_parser_type_parameter (parser
, is_parameter_pack
);
18034 /* If it's an identifier, skip it. */
18035 if (token
->type
== CPP_NAME
)
18036 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
18037 /* Now, see if the token looks like the end of a template
18039 if (token
->type
== CPP_COMMA
18040 || token
->type
== CPP_EQ
18041 || token
->type
== CPP_GREATER
)
18042 return cp_parser_type_parameter (parser
, is_parameter_pack
);
18045 /* Otherwise, it is a non-type parameter or a constrained parameter.
18049 When parsing a default template-argument for a non-type
18050 template-parameter, the first non-nested `>' is taken as the end
18051 of the template parameter-list rather than a greater-than
18053 parameter_declarator
18054 = cp_parser_parameter_declaration (parser
,
18055 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
18056 /*template_parm_p=*/true,
18057 /*parenthesized_p=*/NULL
);
18059 if (!parameter_declarator
)
18060 return error_mark_node
;
18062 /* If the parameter declaration is marked as a parameter pack, set
18063 *IS_PARAMETER_PACK to notify the caller. */
18064 if (parameter_declarator
->template_parameter_pack_p
)
18065 *is_parameter_pack
= true;
18067 if (parameter_declarator
->default_argument
)
18069 /* Can happen in some cases of erroneous input (c++/34892). */
18070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18071 /* Consume the `...' for better error recovery. */
18072 cp_lexer_consume_token (parser
->lexer
);
18075 /* The parameter may have been constrained type parameter. */
18076 if (is_constrained_parameter (parameter_declarator
))
18077 return finish_constrained_parameter (parser
,
18078 parameter_declarator
,
18081 // Now we're sure that the parameter is a non-type parameter.
18082 *is_non_type
= true;
18084 parm
= grokdeclarator (parameter_declarator
->declarator
,
18085 ¶meter_declarator
->decl_specifiers
,
18086 TPARM
, /*initialized=*/0,
18087 /*attrlist=*/NULL
);
18088 if (parm
== error_mark_node
)
18089 return error_mark_node
;
18091 return build_tree_list (parameter_declarator
->default_argument
, parm
);
18094 /* Parse a type-parameter.
18097 class identifier [opt]
18098 class identifier [opt] = type-id
18099 typename identifier [opt]
18100 typename identifier [opt] = type-id
18101 template < template-parameter-list > class identifier [opt]
18102 template < template-parameter-list > class identifier [opt]
18105 GNU Extension (variadic templates):
18108 class ... identifier [opt]
18109 typename ... identifier [opt]
18111 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18112 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18113 the declaration of the parameter.
18115 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18118 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
18123 /* Look for a keyword to tell us what kind of parameter this is. */
18124 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
18126 return error_mark_node
;
18128 switch (token
->keyword
)
18134 tree default_argument
;
18136 /* If the next token is an ellipsis, we have a template
18138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18140 /* Consume the `...' token. */
18141 cp_lexer_consume_token (parser
->lexer
);
18142 maybe_warn_variadic_templates ();
18144 *is_parameter_pack
= true;
18147 /* If the next token is an identifier, then it names the
18149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18150 identifier
= cp_parser_identifier (parser
);
18152 identifier
= NULL_TREE
;
18154 /* Create the parameter. */
18155 parameter
= finish_template_type_parm (class_type_node
, identifier
);
18157 /* If the next token is an `=', we have a default argument. */
18158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18161 = cp_parser_default_type_template_argument (parser
);
18163 /* Template parameter packs cannot have default
18165 if (*is_parameter_pack
)
18168 error_at (token
->location
,
18169 "template parameter pack %qD cannot have a "
18170 "default argument", identifier
);
18172 error_at (token
->location
,
18173 "template parameter packs cannot have "
18174 "default arguments");
18175 default_argument
= NULL_TREE
;
18177 else if (check_for_bare_parameter_packs (default_argument
))
18178 default_argument
= error_mark_node
;
18181 default_argument
= NULL_TREE
;
18183 /* Create the combined representation of the parameter and the
18184 default argument. */
18185 parameter
= build_tree_list (default_argument
, parameter
);
18192 tree default_argument
;
18194 /* Look for the `<'. */
18195 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
18196 /* Parse the template-parameter-list. */
18197 cp_parser_template_parameter_list (parser
);
18198 /* Look for the `>'. */
18199 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
18201 /* If template requirements are present, parse them. */
18204 tree reqs
= get_shorthand_constraints (current_template_parms
);
18205 if (tree dreqs
= cp_parser_requires_clause_opt (parser
, false))
18206 reqs
= combine_constraint_expressions (reqs
, dreqs
);
18207 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
18210 /* Look for the `class' or 'typename' keywords. */
18211 cp_parser_type_parameter_key (parser
);
18212 /* If the next token is an ellipsis, we have a template
18214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18216 /* Consume the `...' token. */
18217 cp_lexer_consume_token (parser
->lexer
);
18218 maybe_warn_variadic_templates ();
18220 *is_parameter_pack
= true;
18222 /* If the next token is an `=', then there is a
18223 default-argument. If the next token is a `>', we are at
18224 the end of the parameter-list. If the next token is a `,',
18225 then we are at the end of this parameter. */
18226 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
18227 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
18228 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18230 identifier
= cp_parser_identifier (parser
);
18231 /* Treat invalid names as if the parameter were nameless. */
18232 if (identifier
== error_mark_node
)
18233 identifier
= NULL_TREE
;
18236 identifier
= NULL_TREE
;
18238 /* Create the template parameter. */
18239 parameter
= finish_template_template_parm (class_type_node
,
18242 /* If the next token is an `=', then there is a
18243 default-argument. */
18244 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18247 = cp_parser_default_template_template_argument (parser
);
18249 /* Template parameter packs cannot have default
18251 if (*is_parameter_pack
)
18254 error_at (token
->location
,
18255 "template parameter pack %qD cannot "
18256 "have a default argument",
18259 error_at (token
->location
, "template parameter packs cannot "
18260 "have default arguments");
18261 default_argument
= NULL_TREE
;
18265 default_argument
= NULL_TREE
;
18267 /* Create the combined representation of the parameter and the
18268 default argument. */
18269 parameter
= build_tree_list (default_argument
, parameter
);
18274 gcc_unreachable ();
18281 /* Parse a template-id.
18284 template-name < template-argument-list [opt] >
18286 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18287 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18288 returned. Otherwise, if the template-name names a function, or set
18289 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18290 names a class, returns a TYPE_DECL for the specialization.
18292 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18293 uninstantiated templates. */
18296 cp_parser_template_id (cp_parser
*parser
,
18297 bool template_keyword_p
,
18298 bool check_dependency_p
,
18299 enum tag_types tag_type
,
18300 bool is_declaration
)
18305 cp_token_position start_of_id
= 0;
18306 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
18307 bool is_identifier
;
18309 /* If the next token corresponds to a template-id, there is no need
18311 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18313 if (token
->type
== CPP_TEMPLATE_ID
)
18315 cp_lexer_consume_token (parser
->lexer
);
18316 return saved_checks_value (token
->u
.tree_check_value
);
18319 /* Avoid performing name lookup if there is no possibility of
18320 finding a template-id. */
18321 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
18322 || (token
->type
== CPP_NAME
18323 && !cp_parser_nth_token_starts_template_argument_list_p
18326 cp_parser_error (parser
, "expected template-id");
18327 return error_mark_node
;
18330 /* Remember where the template-id starts. */
18331 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
18332 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
18334 push_deferring_access_checks (dk_deferred
);
18336 /* Parse the template-name. */
18337 is_identifier
= false;
18338 templ
= cp_parser_template_name (parser
, template_keyword_p
,
18339 check_dependency_p
,
18344 /* Push any access checks inside the firewall we're about to create. */
18345 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
18346 pop_deferring_access_checks ();
18347 if (templ
== error_mark_node
|| is_identifier
)
18350 /* Since we're going to preserve any side-effects from this parse, set up a
18351 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18352 in the template arguments. */
18353 tentative_firewall
firewall (parser
);
18354 reopen_deferring_access_checks (checks
);
18356 /* If we find the sequence `[:' after a template-name, it's probably
18357 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18358 parse correctly the argument list. */
18359 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
18360 == CPP_OPEN_SQUARE
)
18361 && next_token
->flags
& DIGRAPH
18362 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
18364 && !(next_token_2
->flags
& PREV_WHITE
))
18366 cp_parser_parse_tentatively (parser
);
18367 /* Change `:' into `::'. */
18368 next_token_2
->type
= CPP_SCOPE
;
18369 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18371 cp_lexer_consume_token (parser
->lexer
);
18373 /* Parse the arguments. */
18374 arguments
= cp_parser_enclosed_template_argument_list (parser
);
18375 if (!cp_parser_parse_definitely (parser
))
18377 /* If we couldn't parse an argument list, then we revert our changes
18378 and return simply an error. Maybe this is not a template-id
18380 next_token_2
->type
= CPP_COLON
;
18381 cp_parser_error (parser
, "expected %<<%>");
18382 pop_deferring_access_checks ();
18383 return error_mark_node
;
18385 /* Otherwise, emit an error about the invalid digraph, but continue
18386 parsing because we got our argument list. */
18387 if (permerror (next_token
->location
,
18388 "%<<::%> cannot begin a template-argument list"))
18390 static bool hint
= false;
18391 inform (next_token
->location
,
18392 "%<<:%> is an alternate spelling for %<[%>."
18393 " Insert whitespace between %<<%> and %<::%>");
18394 if (!hint
&& !flag_permissive
)
18396 inform (next_token
->location
, "(if you use %<-fpermissive%> "
18397 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18398 "accept your code)");
18405 /* Look for the `<' that starts the template-argument-list. */
18406 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
18408 pop_deferring_access_checks ();
18409 return error_mark_node
;
18411 /* Parse the arguments. */
18412 arguments
= cp_parser_enclosed_template_argument_list (parser
);
18414 if ((cxx_dialect
> cxx17
)
18415 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
18416 && !template_keyword_p
18417 && (cp_parser_error_occurred (parser
)
18418 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
18420 /* This didn't go well. */
18421 if (TREE_CODE (templ
) == FUNCTION_DECL
)
18423 /* C++20 says that "function-name < a;" is now ill-formed. */
18424 if (cp_parser_error_occurred (parser
))
18426 error_at (token
->location
, "invalid template-argument-list");
18427 inform (token
->location
, "function name as the left hand "
18428 "operand of %<<%> is ill-formed in C++20; wrap the "
18429 "function name in %<()%>");
18432 /* We expect "f<targs>" to be followed by "(args)". */
18433 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
18434 "expected %<(%> after template-argument-list");
18436 /* Purge all subsequent tokens. */
18437 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
18440 cp_parser_simulate_error (parser
);
18441 pop_deferring_access_checks ();
18442 return error_mark_node
;
18446 /* Set the location to be of the form:
18447 template-name < template-argument-list [opt] >
18448 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18449 with caret == start at the start of the template-name,
18450 ranging until the closing '>'. */
18451 location_t combined_loc
18452 = make_location (token
->location
, token
->location
, parser
->lexer
);
18454 /* Check for concepts autos where they don't belong. We could
18455 identify types in some cases of identifier TEMPL, looking ahead
18456 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18457 types. We reject them in functions, but if what we have is an
18458 identifier, even with none_type we can't conclude it's NOT a
18459 type, we have to wait for template substitution. */
18460 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
18461 template_id
= error_mark_node
;
18462 /* Build a representation of the specialization. */
18463 else if (identifier_p (templ
))
18464 template_id
= build_min_nt_loc (combined_loc
,
18467 else if (DECL_TYPE_TEMPLATE_P (templ
)
18468 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
18470 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18471 template (rather than some instantiation thereof) only if
18472 is not nested within some other construct. For example, in
18473 "template <typename T> void f(T) { A<T>::", A<T> is just an
18474 instantiation of A. */
18475 bool entering_scope
18476 = (template_parm_scope_p ()
18477 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
18479 = finish_template_type (templ
, arguments
, entering_scope
);
18481 else if (concept_definition_p (templ
))
18483 /* The caller will decide whether this is a concept check or type
18485 template_id
= build2_loc (combined_loc
, TEMPLATE_ID_EXPR
,
18486 boolean_type_node
, templ
, arguments
);
18488 else if (variable_template_p (templ
))
18490 template_id
= lookup_template_variable (templ
, arguments
);
18491 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
18492 SET_EXPR_LOCATION (template_id
, combined_loc
);
18494 else if (TREE_CODE (templ
) == TYPE_DECL
18495 && TREE_CODE (TREE_TYPE (templ
)) == TYPENAME_TYPE
)
18497 /* Some type template in dependent scope. */
18498 tree
&name
= TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ
));
18499 name
= build_min_nt_loc (combined_loc
,
18502 template_id
= templ
;
18506 /* If it's not a class-template or a template-template, it should be
18507 a function-template. */
18508 gcc_assert (OVL_P (templ
) || BASELINK_P (templ
));
18510 template_id
= lookup_template_function (templ
, arguments
);
18511 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
18512 SET_EXPR_LOCATION (template_id
, combined_loc
);
18515 /* If parsing tentatively, replace the sequence of tokens that makes
18516 up the template-id with a CPP_TEMPLATE_ID token. That way,
18517 should we re-parse the token stream, we will not have to repeat
18518 the effort required to do the parse, nor will we issue duplicate
18519 error messages about problems during instantiation of the
18522 /* Don't do this if we had a parse error in a declarator; re-parsing
18523 might succeed if a name changes meaning (60361). */
18524 && !(cp_parser_error_occurred (parser
)
18525 && cp_parser_parsing_tentatively (parser
)
18526 && parser
->in_declarator_p
))
18528 /* Reset the contents of the START_OF_ID token. */
18529 token
->type
= CPP_TEMPLATE_ID
;
18530 token
->location
= combined_loc
;
18532 /* Retrieve any deferred checks. Do not pop this access checks yet
18533 so the memory will not be reclaimed during token replacing below. */
18534 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
18535 token
->tree_check_p
= true;
18536 token
->u
.tree_check_value
->value
= template_id
;
18537 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
18538 token
->keyword
= RID_MAX
;
18540 /* Purge all subsequent tokens. */
18541 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
18543 /* ??? Can we actually assume that, if template_id ==
18544 error_mark_node, we will have issued a diagnostic to the
18545 user, as opposed to simply marking the tentative parse as
18547 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
18548 error_at (token
->location
, "parse error in template argument list");
18551 pop_to_parent_deferring_access_checks ();
18552 return template_id
;
18555 /* Like cp_parser_template_id, called in non-type context. */
18558 cp_parser_template_id_expr (cp_parser
*parser
,
18559 bool template_keyword_p
,
18560 bool check_dependency_p
,
18561 bool is_declaration
)
18563 tree x
= cp_parser_template_id (parser
, template_keyword_p
, check_dependency_p
,
18564 none_type
, is_declaration
);
18565 if (TREE_CODE (x
) == TEMPLATE_ID_EXPR
18566 && concept_check_p (x
))
18567 /* We didn't check the arguments in cp_parser_template_id; do that now. */
18568 return build_concept_id (x
);
18572 /* Parse a template-name.
18577 The standard should actually say:
18581 operator-function-id
18583 A defect report has been filed about this issue.
18585 A conversion-function-id cannot be a template name because they cannot
18586 be part of a template-id. In fact, looking at this code:
18588 a.operator K<int>()
18590 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18591 It is impossible to call a templated conversion-function-id with an
18592 explicit argument list, since the only allowed template parameter is
18593 the type to which it is converting.
18595 If TEMPLATE_KEYWORD_P is true, then we have just seen the
18596 `template' keyword, in a construction like:
18600 In that case `f' is taken to be a template-name, even though there
18601 is no way of knowing for sure.
18603 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18604 name refers to a set of overloaded functions, at least one of which
18605 is a template, or an IDENTIFIER_NODE with the name of the template,
18606 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
18607 names are looked up inside uninstantiated templates. */
18610 cp_parser_template_name (cp_parser
* parser
,
18611 bool template_keyword_p
,
18612 bool check_dependency_p
,
18613 bool is_declaration
,
18614 enum tag_types tag_type
,
18615 bool *is_identifier
)
18619 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18621 /* If the next token is `operator', then we have either an
18622 operator-function-id or a conversion-function-id. */
18623 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
18625 /* We don't know whether we're looking at an
18626 operator-function-id or a conversion-function-id. */
18627 cp_parser_parse_tentatively (parser
);
18628 /* Try an operator-function-id. */
18629 identifier
= cp_parser_operator_function_id (parser
);
18630 /* If that didn't work, try a conversion-function-id. */
18631 if (!cp_parser_parse_definitely (parser
))
18633 cp_parser_error (parser
, "expected template-name");
18634 return error_mark_node
;
18637 /* Look for the identifier. */
18639 identifier
= cp_parser_identifier (parser
);
18641 /* If we didn't find an identifier, we don't have a template-id. */
18642 if (identifier
== error_mark_node
)
18643 return error_mark_node
;
18645 /* If the name immediately followed the `template' keyword, then it
18646 is a template-name. However, if the next token is not `<', then
18647 we do not treat it as a template-name, since it is not being used
18648 as part of a template-id. This enables us to handle constructs
18651 template <typename T> struct S { S(); };
18652 template <typename T> S<T>::S();
18654 correctly. We would treat `S' as a template -- if it were `S<T>'
18655 -- but we do not if there is no `<'. */
18657 if (processing_template_decl
18658 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
18660 /* In a declaration, in a dependent context, we pretend that the
18661 "template" keyword was present in order to improve error
18662 recovery. For example, given:
18664 template <typename T> void f(T::X<int>);
18666 we want to treat "X<int>" as a template-id. */
18668 && !template_keyword_p
18669 && parser
->scope
&& TYPE_P (parser
->scope
)
18670 && check_dependency_p
18671 && dependent_scope_p (parser
->scope
)
18672 /* Do not do this for dtors (or ctors), since they never
18673 need the template keyword before their name. */
18674 && !constructor_name_p (identifier
, parser
->scope
))
18676 cp_token_position start
= 0;
18678 /* Explain what went wrong. */
18679 error_at (token
->location
, "non-template %qD used as template",
18681 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
18682 parser
->scope
, identifier
);
18683 /* If parsing tentatively, find the location of the "<" token. */
18684 if (cp_parser_simulate_error (parser
))
18685 start
= cp_lexer_token_position (parser
->lexer
, true);
18686 /* Parse the template arguments so that we can issue error
18687 messages about them. */
18688 cp_lexer_consume_token (parser
->lexer
);
18689 cp_parser_enclosed_template_argument_list (parser
);
18690 /* Skip tokens until we find a good place from which to
18691 continue parsing. */
18692 cp_parser_skip_to_closing_parenthesis (parser
,
18693 /*recovering=*/true,
18695 /*consume_paren=*/false);
18696 /* If parsing tentatively, permanently remove the
18697 template argument list. That will prevent duplicate
18698 error messages from being issued about the missing
18699 "template" keyword. */
18701 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
18703 *is_identifier
= true;
18704 parser
->context
->object_type
= NULL_TREE
;
18708 /* If the "template" keyword is present, then there is generally
18709 no point in doing name-lookup, so we just return IDENTIFIER.
18710 But, if the qualifying scope is non-dependent then we can
18711 (and must) do name-lookup normally. */
18712 if (template_keyword_p
)
18714 tree scope
= (parser
->scope
? parser
->scope
18715 : parser
->context
->object_type
);
18716 if (scope
&& TYPE_P (scope
)
18717 && (!CLASS_TYPE_P (scope
)
18718 || (check_dependency_p
&& dependent_scope_p (scope
))))
18720 /* We're optimizing away the call to cp_parser_lookup_name, but
18721 we still need to do this. */
18722 parser
->object_scope
= parser
->context
->object_type
;
18723 parser
->context
->object_type
= NULL_TREE
;
18729 /* cp_parser_lookup_name clears OBJECT_TYPE. */
18730 tree scope
= (parser
->scope
? parser
->scope
18731 : parser
->context
->object_type
);
18733 /* Look up the name. */
18734 decl
= cp_parser_lookup_name (parser
, identifier
,
18736 /*is_template=*/true,
18737 /*is_namespace=*/false,
18738 check_dependency_p
,
18739 /*ambiguous_decls=*/NULL
,
18742 decl
= strip_using_decl (decl
);
18744 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18745 template-argument-list if it follows a name that is not a
18746 conversion-function-id and
18747 - that follows the keyword template or a ~ after a nested-name-specifier or
18748 in a class member access expression, or
18749 - for which name lookup finds the injected-class-name of a class template
18750 or finds any declaration of a template, or
18751 - that is an unqualified name for which name lookup either finds one or
18752 more functions or finds nothing, or
18753 - that is a terminal name in a using-declarator (9.9), in a declarator-id
18754 (9.3.4), or in a type-only context other than a nested-name-specifier
18757 /* Handle injected-class-name. */
18758 decl
= maybe_get_template_decl_from_type_decl (decl
);
18760 /* If DECL is a template, then the name was a template-name. */
18761 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
18763 if ((TREE_DEPRECATED (decl
) || TREE_UNAVAILABLE (decl
))
18764 && deprecated_state
!= UNAVAILABLE_DEPRECATED_SUPPRESS
)
18766 tree d
= DECL_TEMPLATE_RESULT (decl
);
18768 if (TREE_CODE (d
) == TYPE_DECL
)
18769 attr
= TYPE_ATTRIBUTES (TREE_TYPE (d
));
18771 attr
= DECL_ATTRIBUTES (d
);
18772 if (TREE_UNAVAILABLE (decl
))
18774 attr
= lookup_attribute ("unavailable", attr
);
18775 error_unavailable_use (decl
, attr
);
18777 else if (TREE_DEPRECATED (decl
)
18778 && deprecated_state
!= DEPRECATED_SUPPRESS
)
18780 attr
= lookup_attribute ("deprecated", attr
);
18781 warn_deprecated_use (decl
, attr
);
18787 /* Look through an overload set for any templates. */
18788 bool found
= false;
18790 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
18791 !found
&& iter
; ++iter
)
18792 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
18795 /* "an unqualified name for which name lookup either finds one or more
18796 functions or finds nothing". */
18798 && (cxx_dialect
> cxx17
)
18800 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
18801 && tag_type
== none_type
)
18803 /* The "more functions" case. Just use the OVERLOAD as normally.
18804 We don't use is_overloaded_fn here to avoid considering
18806 if (TREE_CODE (decl
) == OVERLOAD
18807 /* Name lookup found one function. */
18808 || TREE_CODE (decl
) == FUNCTION_DECL
18809 /* Name lookup found nothing. */
18810 || decl
== error_mark_node
)
18814 /* "that follows the keyword template"..."in a type-only context" */
18815 if (!found
&& scope
18816 && (template_keyword_p
|| tag_type
!= none_type
)
18817 && dependentish_scope_p (scope
)
18818 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
18823 /* The name does not name a template. */
18824 cp_parser_error (parser
, "expected template-name");
18825 return error_mark_node
;
18827 else if ((!DECL_P (decl
) && !is_overloaded_fn (decl
))
18828 || TREE_CODE (decl
) == USING_DECL
18829 /* cp_parser_template_id can only handle some TYPE_DECLs. */
18830 || (TREE_CODE (decl
) == TYPE_DECL
18831 && TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
))
18832 /* Repeat the lookup at instantiation time. */
18839 /* Parse a template-argument-list.
18841 template-argument-list:
18842 template-argument ... [opt]
18843 template-argument-list , template-argument ... [opt]
18845 Returns a TREE_VEC containing the arguments. */
18848 cp_parser_template_argument_list (cp_parser
* parser
)
18850 bool saved_in_template_argument_list_p
;
18852 bool saved_non_ice_p
;
18854 /* Don't create location wrapper nodes within a template-argument-list. */
18855 auto_suppress_location_wrappers sentinel
;
18857 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
18858 parser
->in_template_argument_list_p
= true;
18859 /* Even if the template-id appears in an integral
18860 constant-expression, the contents of the argument list do
18862 saved_ice_p
= parser
->integral_constant_expression_p
;
18863 parser
->integral_constant_expression_p
= false;
18864 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
18865 parser
->non_integral_constant_expression_p
= false;
18867 /* Parse the arguments. */
18868 auto_vec
<tree
, 10> args
;
18871 if (!args
.is_empty ())
18872 /* Consume the comma. */
18873 cp_lexer_consume_token (parser
->lexer
);
18875 /* Parse the template-argument. */
18876 tree argument
= cp_parser_template_argument (parser
);
18878 /* If the next token is an ellipsis, we're expanding a template
18880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18882 if (argument
== error_mark_node
)
18884 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18885 error_at (token
->location
,
18886 "expected parameter pack before %<...%>");
18888 /* Consume the `...' token. */
18889 cp_lexer_consume_token (parser
->lexer
);
18891 /* Make the argument into a TYPE_PACK_EXPANSION or
18892 EXPR_PACK_EXPANSION. */
18893 argument
= make_pack_expansion (argument
);
18896 args
.safe_push (argument
);
18898 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
18900 int n_args
= args
.length ();
18901 tree vec
= make_tree_vec (n_args
);
18903 for (int i
= 0; i
< n_args
; i
++)
18904 TREE_VEC_ELT (vec
, i
) = args
[i
];
18906 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
18907 parser
->integral_constant_expression_p
= saved_ice_p
;
18908 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
18910 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
18914 /* Parse a template-argument.
18917 assignment-expression
18921 The representation is that of an assignment-expression, type-id, or
18922 id-expression -- except that the qualified id-expression is
18923 evaluated, so that the value returned is either a DECL or an
18926 Although the standard says "assignment-expression", it forbids
18927 throw-expressions or assignments in the template argument.
18928 Therefore, we use "conditional-expression" instead. */
18931 cp_parser_template_argument (cp_parser
* parser
)
18936 bool maybe_type_id
= false;
18937 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
18938 location_t loc
= 0;
18941 /* There's really no way to know what we're looking at, so we just
18942 try each alternative in order.
18946 In a template-argument, an ambiguity between a type-id and an
18947 expression is resolved to a type-id, regardless of the form of
18948 the corresponding template-parameter.
18950 Therefore, we try a type-id first. */
18951 cp_parser_parse_tentatively (parser
);
18952 argument
= cp_parser_template_type_arg (parser
);
18953 /* If there was no error parsing the type-id but the next token is a
18954 '>>', our behavior depends on which dialect of C++ we're
18955 parsing. In C++98, we probably found a typo for '> >'. But there
18956 are type-id which are also valid expressions. For instance:
18958 struct X { int operator >> (int); };
18959 template <int V> struct Foo {};
18962 Here 'X()' is a valid type-id of a function type, but the user just
18963 wanted to write the expression "X() >> 5". Thus, we remember that we
18964 found a valid type-id, but we still try to parse the argument as an
18965 expression to see what happens.
18967 In C++0x, the '>>' will be considered two separate '>'
18969 if (!cp_parser_error_occurred (parser
)
18970 && ((cxx_dialect
== cxx98
18971 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
18972 /* Similarly for >= which
18973 cp_parser_next_token_ends_template_argument_p treats for
18974 diagnostics purposes as mistyped > =, but can be valid
18975 after a type-id. */
18976 || cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER_EQ
)))
18978 maybe_type_id
= true;
18979 cp_parser_abort_tentative_parse (parser
);
18983 /* If the next token isn't a `,' or a `>', then this argument wasn't
18984 really finished. This means that the argument is not a valid
18986 if (!cp_parser_next_token_ends_template_argument_p (parser
))
18987 cp_parser_error (parser
, "expected template-argument");
18988 /* If that worked, we're done. */
18989 if (cp_parser_parse_definitely (parser
))
18992 /* We're still not sure what the argument will be. */
18993 cp_parser_parse_tentatively (parser
);
18994 /* Try a template. */
18995 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
18996 argument
= cp_parser_id_expression (parser
,
18997 /*template_keyword_p=*/false,
18998 /*check_dependency_p=*/true,
19000 /*declarator_p=*/false,
19001 /*optional_p=*/false);
19002 /* If the next token isn't a `,' or a `>', then this argument wasn't
19003 really finished. */
19004 if (!cp_parser_next_token_ends_template_argument_p (parser
))
19005 cp_parser_error (parser
, "expected template-argument");
19006 if (!cp_parser_error_occurred (parser
))
19008 /* Figure out what is being referred to. If the id-expression
19009 was for a class template specialization, then we will have a
19010 TYPE_DECL at this point. There is no need to do name lookup
19011 at this point in that case. */
19012 if (TREE_CODE (argument
) != TYPE_DECL
)
19013 argument
= cp_parser_lookup_name (parser
, argument
,
19015 /*is_template=*/template_p
,
19016 /*is_namespace=*/false,
19017 /*check_dependency=*/true,
19018 /*ambiguous_decls=*/NULL
,
19019 argument_start_token
->location
);
19020 if (TREE_CODE (argument
) != TEMPLATE_DECL
19021 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
19022 cp_parser_error (parser
, "expected template-name");
19024 if (cp_parser_parse_definitely (parser
))
19026 if (TREE_UNAVAILABLE (argument
))
19027 error_unavailable_use (argument
, NULL_TREE
);
19028 else if (TREE_DEPRECATED (argument
))
19029 warn_deprecated_use (argument
, NULL_TREE
);
19032 /* It must be a non-type argument. In C++17 any constant-expression is
19034 if (cxx_dialect
> cxx14
)
19037 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
19039 -- an integral constant-expression of integral or enumeration
19042 -- the name of a non-type template-parameter; or
19044 -- the name of an object or function with external linkage...
19046 -- the address of an object or function with external linkage...
19048 -- a pointer to member... */
19049 /* Look for a non-type template parameter. */
19050 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19052 cp_parser_parse_tentatively (parser
);
19053 argument
= cp_parser_primary_expression (parser
,
19054 /*address_p=*/false,
19056 /*template_arg_p=*/true,
19058 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
19059 || !cp_parser_next_token_ends_template_argument_p (parser
))
19060 cp_parser_simulate_error (parser
);
19061 if (cp_parser_parse_definitely (parser
))
19065 /* If the next token is "&", the argument must be the address of an
19066 object or function with external linkage. */
19067 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
19070 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19071 cp_lexer_consume_token (parser
->lexer
);
19073 /* See if we might have an id-expression. */
19074 token
= cp_lexer_peek_token (parser
->lexer
);
19075 if (token
->type
== CPP_NAME
19076 || token
->keyword
== RID_OPERATOR
19077 || token
->type
== CPP_SCOPE
19078 || token
->type
== CPP_TEMPLATE_ID
19079 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
19081 cp_parser_parse_tentatively (parser
);
19082 argument
= cp_parser_primary_expression (parser
,
19085 /*template_arg_p=*/true,
19087 if (cp_parser_error_occurred (parser
)
19088 || !cp_parser_next_token_ends_template_argument_p (parser
))
19089 cp_parser_abort_tentative_parse (parser
);
19094 if (INDIRECT_REF_P (argument
))
19096 /* Strip the dereference temporarily. */
19097 gcc_assert (REFERENCE_REF_P (argument
));
19098 argument
= TREE_OPERAND (argument
, 0);
19101 /* If we're in a template, we represent a qualified-id referring
19102 to a static data member as a SCOPE_REF even if the scope isn't
19103 dependent so that we can check access control later. */
19105 if (TREE_CODE (probe
) == SCOPE_REF
)
19106 probe
= TREE_OPERAND (probe
, 1);
19109 /* A variable without external linkage might still be a
19110 valid constant-expression, so no error is issued here
19111 if the external-linkage check fails. */
19112 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
19113 cp_parser_simulate_error (parser
);
19115 else if (is_overloaded_fn (argument
))
19116 /* All overloaded functions are allowed; if the external
19117 linkage test does not pass, an error will be issued
19121 && (TREE_CODE (argument
) == OFFSET_REF
19122 || TREE_CODE (argument
) == SCOPE_REF
))
19123 /* A pointer-to-member. */
19125 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
19128 cp_parser_simulate_error (parser
);
19130 if (cp_parser_parse_definitely (parser
))
19133 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
19134 NULL_TREE
, tf_warning_or_error
);
19136 argument
= convert_from_reference (argument
);
19141 /* If the argument started with "&", there are no other valid
19142 alternatives at this point. */
19145 cp_parser_error (parser
, "invalid non-type template argument");
19146 return error_mark_node
;
19150 /* If the argument wasn't successfully parsed as a type-id followed
19151 by '>>', the argument can only be a constant expression now.
19152 Otherwise, we try parsing the constant-expression tentatively,
19153 because the argument could really be a type-id. */
19155 cp_parser_parse_tentatively (parser
);
19157 if (cxx_dialect
<= cxx14
)
19158 argument
= cp_parser_constant_expression (parser
);
19161 /* In C++20, we can encounter a braced-init-list. */
19162 if (cxx_dialect
>= cxx20
19163 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19165 bool expr_non_constant_p
;
19166 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
19169 /* With C++17 generalized non-type template arguments we need to handle
19170 lvalue constant expressions, too. */
19171 argument
= cp_parser_assignment_expression (parser
);
19172 require_potential_constant_expression (argument
);
19175 if (!maybe_type_id
)
19177 if (!cp_parser_next_token_ends_template_argument_p (parser
))
19178 cp_parser_error (parser
, "expected template-argument");
19179 if (cp_parser_parse_definitely (parser
))
19181 /* We did our best to parse the argument as a non type-id, but that
19182 was the only alternative that matched (albeit with a '>' after
19183 it). We can assume it's just a typo from the user, and a
19184 diagnostic will then be issued. */
19185 return cp_parser_template_type_arg (parser
);
19188 /* Parse an explicit-instantiation.
19190 explicit-instantiation:
19191 template declaration
19193 Although the standard says `declaration', what it really means is:
19195 explicit-instantiation:
19196 template decl-specifier-seq [opt] declarator [opt] ;
19198 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19199 supposed to be allowed. A defect report has been filed about this
19204 explicit-instantiation:
19205 storage-class-specifier template
19206 decl-specifier-seq [opt] declarator [opt] ;
19207 function-specifier template
19208 decl-specifier-seq [opt] declarator [opt] ; */
19211 cp_parser_explicit_instantiation (cp_parser
* parser
)
19213 int declares_class_or_enum
;
19214 cp_decl_specifier_seq decl_specifiers
;
19215 tree extension_specifier
= NULL_TREE
;
19217 auto_timevar
tv (TV_TEMPLATE_INST
);
19219 /* Look for an (optional) storage-class-specifier or
19220 function-specifier. */
19221 if (cp_parser_allow_gnu_extensions_p (parser
))
19223 extension_specifier
19224 = cp_parser_storage_class_specifier_opt (parser
);
19225 if (!extension_specifier
)
19226 extension_specifier
19227 = cp_parser_function_specifier_opt (parser
,
19228 /*decl_specs=*/NULL
);
19231 /* Look for the `template' keyword. */
19232 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
19233 /* Let the front end know that we are processing an explicit
19235 begin_explicit_instantiation ();
19236 /* [temp.explicit] says that we are supposed to ignore access
19237 control while processing explicit instantiation directives. */
19238 push_deferring_access_checks (dk_no_check
);
19239 /* Parse a decl-specifier-seq. */
19240 cp_parser_decl_specifier_seq (parser
,
19241 CP_PARSER_FLAGS_OPTIONAL
,
19243 &declares_class_or_enum
);
19245 cp_omp_declare_simd_data odsd
;
19246 if (decl_specifiers
.attributes
&& (flag_openmp
|| flag_openmp_simd
))
19247 cp_parser_handle_directive_omp_attributes (parser
,
19248 &decl_specifiers
.attributes
,
19251 /* If there was exactly one decl-specifier, and it declared a class,
19252 and there's no declarator, then we have an explicit type
19254 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
19256 tree type
= check_tag_decl (&decl_specifiers
,
19257 /*explicit_type_instantiation_p=*/true);
19258 /* Turn access control back on for names used during
19259 template instantiation. */
19260 pop_deferring_access_checks ();
19262 do_type_instantiation (type
, extension_specifier
,
19263 /*complain=*/tf_error
);
19267 cp_declarator
*declarator
;
19270 /* Parse the declarator. */
19272 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19273 CP_PARSER_FLAGS_NONE
,
19274 /*ctor_dtor_or_conv_p=*/NULL
,
19275 /*parenthesized_p=*/NULL
,
19276 /*member_p=*/false,
19277 /*friend_p=*/false,
19278 /*static_p=*/false);
19279 if (declares_class_or_enum
& 2)
19280 cp_parser_check_for_definition_in_return_type (declarator
,
19281 decl_specifiers
.type
,
19282 decl_specifiers
.locations
[ds_type_spec
]);
19283 if (declarator
!= cp_error_declarator
)
19285 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
19286 permerror (decl_specifiers
.locations
[ds_inline
],
19287 "explicit instantiation shall not use"
19288 " %<inline%> specifier");
19289 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
19290 permerror (decl_specifiers
.locations
[ds_constexpr
],
19291 "explicit instantiation shall not use"
19292 " %<constexpr%> specifier");
19293 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_consteval
))
19294 permerror (decl_specifiers
.locations
[ds_consteval
],
19295 "explicit instantiation shall not use"
19296 " %<consteval%> specifier");
19298 decl
= grokdeclarator (declarator
, &decl_specifiers
,
19299 NORMAL
, 0, &decl_specifiers
.attributes
);
19300 /* Turn access control back on for names used during
19301 template instantiation. */
19302 pop_deferring_access_checks ();
19303 /* Do the explicit instantiation. */
19304 do_decl_instantiation (decl
, extension_specifier
);
19308 pop_deferring_access_checks ();
19309 /* Skip the body of the explicit instantiation. */
19310 cp_parser_skip_to_end_of_statement (parser
);
19313 /* We're done with the instantiation. */
19314 end_explicit_instantiation ();
19316 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19318 cp_finalize_omp_declare_simd (parser
, &odsd
);
19321 /* Parse an explicit-specialization.
19323 explicit-specialization:
19324 template < > declaration
19326 Although the standard says `declaration', what it really means is:
19328 explicit-specialization:
19329 template <> decl-specifier [opt] init-declarator [opt] ;
19330 template <> function-definition
19331 template <> explicit-specialization
19332 template <> template-declaration */
19335 cp_parser_explicit_specialization (cp_parser
* parser
)
19337 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19339 /* Look for the `template' keyword. */
19340 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
19341 /* Look for the `<'. */
19342 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
19343 /* Look for the `>'. */
19344 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
19345 /* We have processed another parameter list. */
19346 ++parser
->num_template_parameter_lists
;
19350 A template ... explicit specialization ... shall not have C
19352 bool need_lang_pop
= current_lang_name
== lang_name_c
;
19355 error_at (token
->location
, "template specialization with C linkage");
19356 maybe_show_extern_c_location ();
19358 /* Give it C++ linkage to avoid confusing other parts of the
19360 push_lang_context (lang_name_cplusplus
);
19363 /* Let the front end know that we are beginning a specialization. */
19364 if (begin_specialization ())
19366 /* If the next keyword is `template', we need to figure out
19367 whether or not we're looking a template-declaration. */
19368 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
19370 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
19371 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
19372 cp_parser_template_declaration_after_export (parser
,
19373 /*member_p=*/false);
19375 cp_parser_explicit_specialization (parser
);
19378 /* Parse the dependent declaration. */
19379 cp_parser_single_declaration (parser
,
19381 /*member_p=*/false,
19382 /*explicit_specialization_p=*/true,
19383 /*friend_p=*/NULL
);
19386 /* We're done with the specialization. */
19387 end_specialization ();
19389 /* For the erroneous case of a template with C linkage, we pushed an
19390 implicit C++ linkage scope; exit that scope now. */
19392 pop_lang_context ();
19394 /* We're done with this parameter list. */
19395 --parser
->num_template_parameter_lists
;
19398 /* Preserve the attributes across a garbage collect (by making it a GC
19399 root), which can occur when parsing a member function. */
19401 static GTY(()) vec
<tree
, va_gc
> *cp_parser_decl_specs_attrs
;
19403 /* Parse a type-specifier.
19406 simple-type-specifier
19409 elaborated-type-specifier
19417 Returns a representation of the type-specifier. For a
19418 class-specifier, enum-specifier, or elaborated-type-specifier, a
19419 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19421 The parser flags FLAGS is used to control type-specifier parsing.
19423 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19424 in a decl-specifier-seq.
19426 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19427 class-specifier, enum-specifier, or elaborated-type-specifier, then
19428 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19429 if a type is declared; 2 if it is defined. Otherwise, it is set to
19432 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19433 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19434 is set to FALSE. */
19437 cp_parser_type_specifier (cp_parser
* parser
,
19438 cp_parser_flags flags
,
19439 cp_decl_specifier_seq
*decl_specs
,
19440 bool is_declaration
,
19441 int* declares_class_or_enum
,
19442 bool* is_cv_qualifier
)
19444 tree type_spec
= NULL_TREE
;
19447 cp_decl_spec ds
= ds_last
;
19449 /* Assume this type-specifier does not declare a new type. */
19450 if (declares_class_or_enum
)
19451 *declares_class_or_enum
= 0;
19452 /* And that it does not specify a cv-qualifier. */
19453 if (is_cv_qualifier
)
19454 *is_cv_qualifier
= false;
19455 /* Peek at the next token. */
19456 token
= cp_lexer_peek_token (parser
->lexer
);
19458 /* If we're looking at a keyword, we can use that to guide the
19459 production we choose. */
19460 keyword
= token
->keyword
;
19464 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
19465 goto elaborated_type_specifier
;
19467 /* Look for the enum-specifier. */
19468 type_spec
= cp_parser_enum_specifier (parser
);
19469 /* If that worked, we're done. */
19472 if (declares_class_or_enum
)
19473 *declares_class_or_enum
= 2;
19475 cp_parser_set_decl_spec_type (decl_specs
,
19478 /*type_definition_p=*/true);
19482 goto elaborated_type_specifier
;
19484 /* Any of these indicate either a class-specifier, or an
19485 elaborated-type-specifier. */
19489 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
19490 goto elaborated_type_specifier
;
19492 /* Parse tentatively so that we can back up if we don't find a
19493 class-specifier. */
19494 cp_parser_parse_tentatively (parser
);
19495 if (decl_specs
->attributes
)
19496 vec_safe_push (cp_parser_decl_specs_attrs
, decl_specs
->attributes
);
19497 /* Look for the class-specifier. */
19498 type_spec
= cp_parser_class_specifier (parser
);
19499 if (decl_specs
->attributes
)
19500 cp_parser_decl_specs_attrs
->pop ();
19501 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
19502 /* If that worked, we're done. */
19503 if (cp_parser_parse_definitely (parser
))
19505 if (declares_class_or_enum
)
19506 *declares_class_or_enum
= 2;
19508 cp_parser_set_decl_spec_type (decl_specs
,
19511 /*type_definition_p=*/true);
19515 /* Fall through. */
19516 elaborated_type_specifier
:
19517 /* We're declaring (not defining) a class or enum. */
19518 if (declares_class_or_enum
)
19519 *declares_class_or_enum
= 1;
19521 /* Fall through. */
19523 /* Look for an elaborated-type-specifier. */
19525 = (cp_parser_elaborated_type_specifier
19527 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
19530 cp_parser_set_decl_spec_type (decl_specs
,
19533 /*type_definition_p=*/false);
19538 if (is_cv_qualifier
)
19539 *is_cv_qualifier
= true;
19544 if (is_cv_qualifier
)
19545 *is_cv_qualifier
= true;
19550 if (is_cv_qualifier
)
19551 *is_cv_qualifier
= true;
19555 /* The `__complex__' keyword is a GNU extension. */
19563 /* Handle simple keywords. */
19568 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
19569 decl_specs
->any_specifiers_p
= true;
19571 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19574 /* If we do not already have a type-specifier, assume we are looking
19575 at a simple-type-specifier. */
19576 type_spec
= cp_parser_simple_type_specifier (parser
,
19580 /* If we didn't find a type-specifier, and a type-specifier was not
19581 optional in this context, issue an error message. */
19582 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
19584 cp_parser_error (parser
, "expected type specifier");
19585 return error_mark_node
;
19591 /* Parse a simple-type-specifier.
19593 simple-type-specifier:
19594 :: [opt] nested-name-specifier [opt] type-name
19595 :: [opt] nested-name-specifier template template-id
19610 simple-type-specifier:
19612 decltype ( expression )
19615 __underlying_type ( type-id )
19619 nested-name-specifier(opt) template-name
19623 simple-type-specifier:
19625 __typeof__ unary-expression
19626 __typeof__ ( type-id )
19627 __typeof__ ( type-id ) { initializer-list , [opt] }
19629 Concepts Extension:
19631 simple-type-specifier:
19632 constrained-type-specifier
19634 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
19635 appropriately updated. */
19638 cp_parser_simple_type_specifier (cp_parser
* parser
,
19639 cp_decl_specifier_seq
*decl_specs
,
19640 cp_parser_flags flags
)
19642 tree type
= NULL_TREE
;
19646 /* Peek at the next token. */
19647 token
= cp_lexer_peek_token (parser
->lexer
);
19649 /* If we're looking at a keyword, things are easy. */
19650 switch (token
->keyword
)
19654 decl_specs
->explicit_char_p
= true;
19655 type
= char_type_node
;
19658 type
= char8_type_node
;
19661 type
= char16_type_node
;
19664 type
= char32_type_node
;
19667 type
= wchar_type_node
;
19670 type
= boolean_type_node
;
19673 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
19674 type
= short_integer_type_node
;
19678 decl_specs
->explicit_int_p
= true;
19679 type
= integer_type_node
;
19685 idx
= token
->keyword
- RID_INT_N_0
;
19686 if (! int_n_enabled_p
[idx
])
19690 decl_specs
->explicit_intN_p
= true;
19691 decl_specs
->int_n_idx
= idx
;
19692 /* Check if the alternate "__intN__" form has been used instead of
19694 if (startswith (IDENTIFIER_POINTER (token
->u
.value
)
19695 + (IDENTIFIER_LENGTH (token
->u
.value
) - 2), "__"))
19696 decl_specs
->int_n_alt
= true;
19698 type
= int_n_trees
[idx
].signed_type
;
19702 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
19703 type
= long_integer_type_node
;
19706 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
19707 type
= integer_type_node
;
19710 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
19711 type
= unsigned_type_node
;
19714 type
= float_type_node
;
19717 type
= double_type_node
;
19720 type
= void_type_node
;
19724 maybe_warn_cpp0x (CPP0X_AUTO
);
19725 if (parser
->auto_is_implicit_function_template_parm_p
)
19727 /* The 'auto' might be the placeholder return type for a function decl
19728 with trailing return type. */
19729 bool have_trailing_return_fn_decl
= false;
19731 cp_parser_parse_tentatively (parser
);
19732 cp_lexer_consume_token (parser
->lexer
);
19733 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
19734 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
19735 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
19736 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
19738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
19740 cp_lexer_consume_token (parser
->lexer
);
19741 cp_parser_skip_to_closing_parenthesis (parser
,
19742 /*recovering*/false,
19744 /*consume_paren*/true);
19748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
19750 have_trailing_return_fn_decl
= true;
19754 cp_lexer_consume_token (parser
->lexer
);
19756 cp_parser_abort_tentative_parse (parser
);
19758 if (have_trailing_return_fn_decl
)
19760 type
= make_auto ();
19764 if (cxx_dialect
>= cxx14
)
19766 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
19767 type
= TREE_TYPE (type
);
19770 type
= error_mark_node
;
19772 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
19774 if (cxx_dialect
< cxx14
)
19775 error_at (token
->location
,
19776 "use of %<auto%> in lambda parameter declaration "
19777 "only available with "
19778 "%<-std=c++14%> or %<-std=gnu++14%>");
19780 else if (cxx_dialect
< cxx14
)
19781 error_at (token
->location
,
19782 "use of %<auto%> in parameter declaration "
19783 "only available with "
19784 "%<-std=c++14%> or %<-std=gnu++14%>");
19785 else if (!flag_concepts
)
19786 pedwarn (token
->location
, 0,
19787 "use of %<auto%> in parameter declaration "
19788 "only available with %<-std=c++20%> or %<-fconcepts%>");
19791 type
= make_auto ();
19795 /* Since DR 743, decltype can either be a simple-type-specifier by
19796 itself or begin a nested-name-specifier. Parsing it will replace
19797 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19798 handling below decide what to do. */
19799 cp_parser_decltype (parser
);
19800 cp_lexer_set_token_position (parser
->lexer
, token
);
19804 /* Consume the `typeof' token. */
19805 cp_lexer_consume_token (parser
->lexer
);
19806 /* Parse the operand to `typeof'. */
19807 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
19808 /* If it is not already a TYPE, take its type. */
19809 if (!TYPE_P (type
))
19810 type
= finish_typeof (type
);
19813 cp_parser_set_decl_spec_type (decl_specs
, type
,
19815 /*type_definition_p=*/false);
19819 case RID_UNDERLYING_TYPE
:
19820 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
19822 cp_parser_set_decl_spec_type (decl_specs
, type
,
19824 /*type_definition_p=*/false);
19829 case RID_DIRECT_BASES
:
19830 type
= cp_parser_trait_expr (parser
, token
->keyword
);
19832 cp_parser_set_decl_spec_type (decl_specs
, type
,
19834 /*type_definition_p=*/false);
19840 /* If token is an already-parsed decltype not followed by ::,
19841 it's a simple-type-specifier. */
19842 if (token
->type
== CPP_DECLTYPE
19843 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
19845 type
= saved_checks_value (token
->u
.tree_check_value
);
19848 cp_parser_set_decl_spec_type (decl_specs
, type
,
19850 /*type_definition_p=*/false);
19851 /* Remember that we are handling a decltype in order to
19852 implement the resolution of DR 1510 when the argument
19853 isn't instantiation dependent. */
19854 decl_specs
->decltype_p
= true;
19856 cp_lexer_consume_token (parser
->lexer
);
19860 /* If the type-specifier was for a built-in type, we're done. */
19863 /* Record the type. */
19865 && (token
->keyword
!= RID_SIGNED
19866 && token
->keyword
!= RID_UNSIGNED
19867 && token
->keyword
!= RID_SHORT
19868 && token
->keyword
!= RID_LONG
))
19869 cp_parser_set_decl_spec_type (decl_specs
,
19872 /*type_definition_p=*/false);
19874 decl_specs
->any_specifiers_p
= true;
19876 /* Consume the token. */
19877 cp_lexer_consume_token (parser
->lexer
);
19879 if (type
== error_mark_node
)
19880 return error_mark_node
;
19882 /* There is no valid C++ program where a non-template type is
19883 followed by a "<". That usually indicates that the user thought
19884 that the type was a template. */
19885 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
19888 return TYPE_NAME (type
);
19891 /* The type-specifier must be a user-defined type. */
19892 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
19896 const bool typename_p
= (cxx_dialect
>= cxx20
19897 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
19899 /* Don't gobble tokens or issue error messages if this is an
19900 optional type-specifier. */
19901 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
19902 cp_parser_parse_tentatively (parser
);
19904 /* Remember current tentative parsing state -- if we know we need
19905 a type, we can give better diagnostics here. */
19906 bool tent
= cp_parser_parsing_tentatively (parser
);
19908 token
= cp_lexer_peek_token (parser
->lexer
);
19910 /* Look for the optional `::' operator. */
19912 = (cp_parser_global_scope_opt (parser
,
19913 /*current_scope_valid_p=*/false)
19915 /* Look for the nested-name specifier. */
19917 = (cp_parser_nested_name_specifier_opt (parser
,
19918 /*typename_keyword_p=*/false,
19919 /*check_dependency_p=*/true,
19921 /*is_declaration=*/false)
19923 /* If we have seen a nested-name-specifier, and the next token
19924 is `template', then we are using the template-id production. */
19926 && cp_parser_optional_template_keyword (parser
))
19928 /* Look for the template-id. */
19929 type
= cp_parser_template_id (parser
,
19930 /*template_keyword_p=*/true,
19931 /*check_dependency_p=*/true,
19933 /*is_declaration=*/false);
19934 /* If the template-id did not name a type, we are out of
19936 if (TREE_CODE (type
) != TYPE_DECL
)
19938 /* ...unless we pretend we have seen 'typename'. */
19940 type
= cp_parser_make_typename_type (parser
, type
,
19944 cp_parser_error (parser
, "expected template-id for type");
19945 type
= error_mark_node
;
19949 /* DR 1812: A < following a qualified-id in a typename-specifier
19950 could safely be assumed to begin a template argument list, so
19951 the template keyword should be optional. */
19952 else if (parser
->scope
19955 && cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
))
19957 cp_parser_parse_tentatively (parser
);
19959 type
= cp_parser_template_id (parser
,
19960 /*template_keyword_p=*/true,
19961 /*check_dependency_p=*/true,
19963 /*is_declaration=*/false);
19964 /* This is handled below, so back off. */
19965 if (type
&& concept_check_p (type
))
19966 cp_parser_simulate_error (parser
);
19968 if (!cp_parser_parse_definitely (parser
))
19970 else if (TREE_CODE (type
) == TEMPLATE_ID_EXPR
)
19971 type
= make_typename_type (parser
->scope
, type
, typename_type
,
19972 /*complain=*/tf_error
);
19973 else if (TREE_CODE (type
) != TYPE_DECL
)
19977 /* Otherwise, look for a type-name. */
19980 if (cxx_dialect
>= cxx17
)
19981 cp_parser_parse_tentatively (parser
);
19983 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
19985 if (cxx_dialect
>= cxx17
&& !cp_parser_parse_definitely (parser
))
19989 if (!type
&& flag_concepts
&& decl_specs
)
19991 /* Try for a type-constraint with template arguments. We check
19992 decl_specs here to avoid trying this for a functional cast. */
19994 cp_parser_parse_tentatively (parser
);
19996 type
= cp_parser_template_id (parser
,
19997 /*template_keyword_p=*/false,
19998 /*check_dependency_p=*/true,
20000 /*is_declaration=*/false);
20001 if (type
&& concept_check_p (type
))
20003 location_t loc
= EXPR_LOCATION (type
);
20004 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
20006 if (tent
&& type
== error_mark_node
)
20007 /* Perhaps it's a concept-check expression. */
20008 cp_parser_simulate_error (parser
);
20011 cp_parser_simulate_error (parser
);
20013 if (!cp_parser_parse_definitely (parser
))
20017 if (!type
&& cxx_dialect
>= cxx17
)
20019 /* Try class template argument deduction or type-constraint without
20020 template arguments. */
20021 tree name
= cp_parser_identifier (parser
);
20022 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
20023 && parser
->scope
!= error_mark_node
)
20026 = cp_lexer_previous_token (parser
->lexer
)->location
;
20027 tree tmpl
= cp_parser_lookup_name (parser
, name
,
20029 /*is_template=*/false,
20030 /*is_namespace=*/false,
20031 /*check_dependency=*/true,
20032 /*ambiguous_decls=*/NULL
,
20034 if (tmpl
&& tmpl
!= error_mark_node
20035 && ctad_template_p (tmpl
))
20036 type
= make_template_placeholder (tmpl
);
20037 else if (flag_concepts
&& tmpl
&& concept_definition_p (tmpl
))
20038 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
20042 type
= error_mark_node
;
20043 if (!cp_parser_simulate_error (parser
))
20044 cp_parser_name_lookup_error (parser
, name
, tmpl
,
20045 NLE_TYPE
, token
->location
);
20049 type
= error_mark_node
;
20052 /* If it didn't work out, we don't have a TYPE. */
20053 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
20054 && !cp_parser_parse_definitely (parser
))
20057 /* Keep track of all name-lookups performed in class scopes. */
20061 && TREE_CODE (type
) == TYPE_DECL
20062 && identifier_p (DECL_NAME (type
)))
20063 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
20065 if (type
&& decl_specs
)
20066 cp_parser_set_decl_spec_type (decl_specs
, type
,
20068 /*type_definition_p=*/false);
20071 /* If we didn't get a type-name, issue an error message. */
20072 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
20074 cp_parser_error (parser
, "expected type-name");
20075 return error_mark_node
;
20078 if (type
&& type
!= error_mark_node
)
20080 /* See if TYPE is an Objective-C type, and if so, parse and
20081 accept any protocol references following it. Do this before
20082 the cp_parser_check_for_invalid_template_id() call, because
20083 Objective-C types can be followed by '<...>' which would
20084 enclose protocol names rather than template arguments, and so
20085 everything is fine. */
20086 if (c_dialect_objc () && !parser
->scope
20087 && (objc_is_id (type
) || objc_is_class_name (type
)))
20089 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
20090 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
20092 /* Clobber the "unqualified" type previously entered into
20093 DECL_SPECS with the new, improved protocol-qualified version. */
20095 decl_specs
->type
= qual_type
;
20100 /* There is no valid C++ program where a non-template type is
20101 followed by a "<". That usually indicates that the user
20102 thought that the type was a template. */
20103 cp_parser_check_for_invalid_template_id (parser
, type
,
20111 /* Parse the remainder of a placholder-type-specifier.
20113 placeholder-type-specifier:
20114 type-constraint_opt auto
20115 type-constraint_opt decltype(auto)
20117 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20118 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20119 parses the placeholder type, and performs some contextual syntactic analysis.
20121 LOC provides the location of the template name.
20123 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20124 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20125 might actually be a concept-check,
20127 Note that the Concepts TS allows the auto or decltype(auto) to be
20128 omitted in a constrained-type-specifier. */
20131 cp_parser_placeholder_type_specifier (cp_parser
*parser
, location_t loc
,
20132 tree tmpl
, bool tentative
)
20134 if (tmpl
== error_mark_node
)
20135 return error_mark_node
;
20137 tree orig_tmpl
= tmpl
;
20139 /* Get the arguments as written for subsequent analysis. */
20140 tree args
= NULL_TREE
;
20141 if (TREE_CODE (tmpl
) == TEMPLATE_ID_EXPR
)
20143 args
= TREE_OPERAND (tmpl
, 1);
20144 tmpl
= TREE_OPERAND (tmpl
, 0);
20147 /* A concept-name with no arguments can't be an expression. */
20150 tsubst_flags_t complain
= tentative
? tf_none
: tf_warning_or_error
;
20152 /* Get the concept and prototype parameter for the constraint. */
20153 tree_pair info
= finish_type_constraints (tmpl
, args
, complain
);
20154 tree con
= info
.first
;
20155 tree proto
= info
.second
;
20156 if (con
== error_mark_node
)
20157 return error_mark_node
;
20159 /* As per the standard, require auto or decltype(auto), except in some
20160 cases (template parameter lists, -fconcepts-ts enabled). */
20161 cp_token
*placeholder
= NULL
, *close_paren
= NULL
;
20162 if (cxx_dialect
>= cxx20
)
20164 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
20165 placeholder
= cp_lexer_consume_token (parser
->lexer
);
20166 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DECLTYPE
))
20168 placeholder
= cp_lexer_consume_token (parser
->lexer
);
20169 matching_parens parens
;
20170 parens
.require_open (parser
);
20171 cp_parser_require_keyword (parser
, RID_AUTO
, RT_AUTO
);
20172 close_paren
= parens
.require_close (parser
);
20176 /* A type constraint constrains a contextually determined type or type
20177 parameter pack. However, the Concepts TS does allow concepts
20178 to introduce non-type and template template parameters. */
20179 if (TREE_CODE (proto
) != TYPE_DECL
)
20181 if (!flag_concepts_ts
20182 || !processing_template_parmlist
)
20186 error_at (loc
, "%qE does not constrain a type", DECL_NAME (con
));
20187 inform (DECL_SOURCE_LOCATION (con
), "concept defined here");
20189 return error_mark_node
;
20193 /* In a template parameter list, a type-parameter can be introduced
20194 by type-constraints alone. */
20195 if (processing_template_parmlist
&& !placeholder
)
20197 /* In a default argument we may not be creating new parameters. */
20198 if (parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
20200 /* If this assert turns out to be false, do error() instead. */
20201 gcc_assert (tentative
);
20202 return error_mark_node
;
20204 return build_constrained_parameter (con
, proto
, args
);
20207 /* Diagnose issues placeholder issues. */
20208 if (!flag_concepts_ts
20209 && !parser
->in_result_type_constraint_p
20213 /* Perhaps it's a concept-check expression (c++/91073). */
20214 return error_mark_node
;
20216 tree id
= build_nt (TEMPLATE_ID_EXPR
, tmpl
, args
);
20217 tree expr
= DECL_P (orig_tmpl
) ? DECL_NAME (con
) : id
;
20218 error_at (input_location
,
20219 "expected %<auto%> or %<decltype(auto)%> after %qE", expr
);
20220 /* Fall through. This is an error of omission. */
20222 else if (parser
->in_result_type_constraint_p
&& placeholder
)
20224 /* A trailing return type only allows type-constraints. */
20225 error_at (input_location
,
20226 "unexpected placeholder in constrained result type");
20229 /* In a parameter-declaration-clause, a placeholder-type-specifier
20230 results in an invented template parameter. */
20231 if (parser
->auto_is_implicit_function_template_parm_p
)
20235 location_t loc
= make_location (placeholder
->location
,
20236 placeholder
->location
,
20237 close_paren
->location
);
20238 error_at (loc
, "cannot declare a parameter with %<decltype(auto)%>");
20239 return error_mark_node
;
20241 tree parm
= build_constrained_parameter (con
, proto
, args
);
20242 return synthesize_implicit_template_parm (parser
, parm
);
20245 /* Determine if the type should be deduced using template argument
20246 deduction or decltype deduction. Note that the latter is always
20247 used for type-constraints in trailing return types. */
20248 bool decltype_p
= placeholder
20249 ? placeholder
->keyword
== RID_DECLTYPE
20250 : parser
->in_result_type_constraint_p
;
20252 /* Otherwise, this is the type of a variable or return type. */
20254 return make_constrained_decltype_auto (con
, args
);
20256 return make_constrained_auto (con
, args
);
20259 /* Parse a type-name.
20265 simple-template-id [in c++0x]
20282 Returns a TYPE_DECL for the type. */
20285 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
20289 /* We can't know yet whether it is a class-name or not. */
20290 cp_parser_parse_tentatively (parser
);
20291 /* Try a class-name. */
20292 type_decl
= cp_parser_class_name (parser
,
20293 typename_keyword_p
,
20294 /*template_keyword_p=*/false,
20296 /*check_dependency_p=*/true,
20297 /*class_head_p=*/false,
20298 /*is_declaration=*/false);
20299 /* If it's not a class-name, keep looking. */
20300 if (!cp_parser_parse_definitely (parser
))
20302 if (cxx_dialect
< cxx11
)
20303 /* It must be a typedef-name or an enum-name. */
20304 return cp_parser_nonclass_name (parser
);
20306 cp_parser_parse_tentatively (parser
);
20307 /* It is either a simple-template-id representing an
20308 instantiation of an alias template... */
20309 type_decl
= cp_parser_template_id (parser
,
20310 /*template_keyword_p=*/false,
20311 /*check_dependency_p=*/true,
20313 /*is_declaration=*/false);
20314 /* Note that this must be an instantiation of an alias template
20315 because [temp.names]/6 says:
20317 A template-id that names an alias template specialization
20320 Whereas [temp.names]/7 says:
20322 A simple-template-id that names a class template
20323 specialization is a class-name.
20325 With concepts, this could also be a partial-concept-id that
20326 declares a non-type template parameter. */
20327 if (type_decl
!= NULL_TREE
20328 && TREE_CODE (type_decl
) == TYPE_DECL
20329 && TYPE_DECL_ALIAS_P (type_decl
))
20330 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
20332 cp_parser_simulate_error (parser
);
20334 if (!cp_parser_parse_definitely (parser
))
20335 /* ... Or a typedef-name or an enum-name. */
20336 return cp_parser_nonclass_name (parser
);
20342 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20354 Returns a TYPE_DECL for the type. */
20357 cp_parser_nonclass_name (cp_parser
* parser
)
20362 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20363 identifier
= cp_parser_identifier (parser
);
20364 if (identifier
== error_mark_node
)
20365 return error_mark_node
;
20367 /* Look up the type-name. */
20368 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
20370 type_decl
= strip_using_decl (type_decl
);
20372 if (TREE_CODE (type_decl
) != TYPE_DECL
20373 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
20375 /* See if this is an Objective-C type. */
20376 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
20377 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
20379 type_decl
= TYPE_NAME (type
);
20382 /* Issue an error if we did not find a type-name. */
20383 if (TREE_CODE (type_decl
) != TYPE_DECL
20384 /* In Objective-C, we have the complication that class names are
20385 normally type names and start declarations (eg, the
20386 "NSObject" in "NSObject *object;"), but can be used in an
20387 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20388 is an expression. So, a classname followed by a dot is not a
20389 valid type-name. */
20390 || (objc_is_class_name (TREE_TYPE (type_decl
))
20391 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
20393 if (!cp_parser_simulate_error (parser
))
20394 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
20395 NLE_TYPE
, token
->location
);
20396 return error_mark_node
;
20398 /* Remember that the name was used in the definition of the
20399 current class so that we can check later to see if the
20400 meaning would have been different after the class was
20401 entirely defined. */
20402 else if (type_decl
!= error_mark_node
20404 maybe_note_name_used_in_class (identifier
, type_decl
);
20409 /* Parse an elaborated-type-specifier. Note that the grammar given
20410 here incorporates the resolution to DR68.
20412 elaborated-type-specifier:
20413 class-key :: [opt] nested-name-specifier [opt] identifier
20414 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20415 enum-key :: [opt] nested-name-specifier [opt] identifier
20416 typename :: [opt] nested-name-specifier identifier
20417 typename :: [opt] nested-name-specifier template [opt]
20422 elaborated-type-specifier:
20423 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20424 class-key attributes :: [opt] nested-name-specifier [opt]
20425 template [opt] template-id
20426 enum attributes :: [opt] nested-name-specifier [opt] identifier
20428 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20429 declared `friend'. If IS_DECLARATION is TRUE, then this
20430 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20431 something is being declared.
20433 Returns the TYPE specified. */
20436 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
20438 bool is_declaration
)
20440 enum tag_types tag_type
;
20442 tree type
= NULL_TREE
;
20443 tree attributes
= NULL_TREE
;
20445 cp_token
*token
= NULL
;
20447 /* For class and enum types the location of the class-key or enum-key. */
20448 location_t key_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20449 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20450 rid scoped_key
= RID_MAX
;
20452 /* See if we're looking at the `enum' keyword. */
20453 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
20455 /* Consume the `enum' token. */
20456 cp_lexer_consume_token (parser
->lexer
);
20457 /* Remember that it's an enumeration type. */
20458 tag_type
= enum_type
;
20459 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20460 enums) is used here. */
20461 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20462 if (cp_parser_is_keyword (token
, scoped_key
= RID_CLASS
)
20463 || cp_parser_is_keyword (token
, scoped_key
= RID_STRUCT
))
20465 location_t loc
= token
->location
;
20466 gcc_rich_location
richloc (loc
);
20467 richloc
.add_range (input_location
);
20468 richloc
.add_fixit_remove ();
20469 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
20470 "a scoped enum must not use the %qD keyword",
20472 /* Consume the `struct' or `class' and parse it anyway. */
20473 cp_lexer_consume_token (parser
->lexer
);
20474 /* Create a combined location for the whole scoped-enum-key. */
20475 key_loc
= make_location (key_loc
, key_loc
, loc
);
20478 scoped_key
= RID_MAX
;
20480 /* Parse the attributes. */
20481 attributes
= cp_parser_attributes_opt (parser
);
20483 /* Or, it might be `typename'. */
20484 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
20487 /* Consume the `typename' token. */
20488 cp_lexer_consume_token (parser
->lexer
);
20489 /* Remember that it's a `typename' type. */
20490 tag_type
= typename_type
;
20492 /* Otherwise it must be a class-key. */
20495 key_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20496 tag_type
= cp_parser_class_key (parser
);
20497 if (tag_type
== none_type
)
20498 return error_mark_node
;
20499 /* Parse the attributes. */
20500 attributes
= cp_parser_attributes_opt (parser
);
20503 /* Look for the `::' operator. */
20504 globalscope
= cp_parser_global_scope_opt (parser
,
20505 /*current_scope_valid_p=*/false);
20506 /* Look for the nested-name-specifier. */
20507 tree nested_name_specifier
;
20508 if (tag_type
== typename_type
&& !globalscope
)
20510 nested_name_specifier
20511 = cp_parser_nested_name_specifier (parser
,
20512 /*typename_keyword_p=*/true,
20513 /*check_dependency_p=*/true,
20516 if (!nested_name_specifier
)
20517 return error_mark_node
;
20520 /* Even though `typename' is not present, the proposed resolution
20521 to Core Issue 180 says that in `class A<T>::B', `B' should be
20522 considered a type-name, even if `A<T>' is dependent. */
20523 nested_name_specifier
20524 = cp_parser_nested_name_specifier_opt (parser
,
20525 /*typename_keyword_p=*/true,
20526 /*check_dependency_p=*/true,
20529 /* For everything but enumeration types, consider a template-id.
20530 For an enumeration type, consider only a plain identifier. */
20531 if (tag_type
!= enum_type
)
20533 bool template_p
= false;
20536 /* Allow the `template' keyword. */
20537 template_p
= cp_parser_optional_template_keyword (parser
);
20538 /* If we didn't see `template', we don't know if there's a
20539 template-id or not. */
20541 cp_parser_parse_tentatively (parser
);
20542 /* The `template' keyword must follow a nested-name-specifier. */
20543 else if (!nested_name_specifier
&& !globalscope
)
20545 cp_parser_error (parser
, "%<template%> must follow a nested-"
20547 return error_mark_node
;
20550 /* Parse the template-id. */
20551 token
= cp_lexer_peek_token (parser
->lexer
);
20552 decl
= cp_parser_template_id (parser
, template_p
,
20553 /*check_dependency_p=*/true,
20556 /* If we didn't find a template-id, look for an ordinary
20558 if (!template_p
&& !cp_parser_parse_definitely (parser
))
20560 /* We can get here when cp_parser_template_id, called by
20561 cp_parser_class_name with tag_type == none_type, succeeds
20562 and caches a BASELINK. Then, when called again here,
20563 instead of failing and returning an error_mark_node
20564 returns it (see template/typename17.C in C++11).
20565 ??? Could we diagnose this earlier? */
20566 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
20568 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
20569 type
= error_mark_node
;
20571 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20572 in effect, then we must assume that, upon instantiation, the
20573 template will correspond to a class. */
20574 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
20575 && tag_type
== typename_type
)
20576 type
= make_typename_type (parser
->scope
, decl
,
20578 /*complain=*/tf_error
);
20579 /* If the `typename' keyword is in effect and DECL is not a type
20580 decl, then type is non existent. */
20581 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
20583 else if (TREE_CODE (decl
) == TYPE_DECL
)
20585 type
= check_elaborated_type_specifier (tag_type
, decl
,
20586 /*allow_template_p=*/true);
20588 /* If the next token is a semicolon, this must be a specialization,
20589 instantiation, or friend declaration. Check the scope while we
20590 still know whether or not we had a nested-name-specifier. */
20591 if (type
!= error_mark_node
20592 && !nested_name_specifier
&& !is_friend
20593 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20594 check_unqualified_spec_or_inst (type
, token
->location
);
20596 else if (decl
== error_mark_node
)
20597 type
= error_mark_node
;
20602 token
= cp_lexer_peek_token (parser
->lexer
);
20603 identifier
= cp_parser_identifier (parser
);
20605 if (identifier
== error_mark_node
)
20607 parser
->scope
= NULL_TREE
;
20608 return error_mark_node
;
20611 /* For a `typename', we needn't call xref_tag. */
20612 if (tag_type
== typename_type
20613 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
20614 return cp_parser_make_typename_type (parser
, identifier
,
20617 /* Template parameter lists apply only if we are not within a
20618 function parameter list. */
20619 bool template_parm_lists_apply
20620 = parser
->num_template_parameter_lists
;
20621 if (template_parm_lists_apply
)
20622 for (cp_binding_level
*s
= current_binding_level
;
20623 s
&& s
->kind
!= sk_template_parms
;
20624 s
= s
->level_chain
)
20625 if (s
->kind
== sk_function_parms
)
20626 template_parm_lists_apply
= false;
20628 /* Look up a qualified name in the usual way. */
20632 tree ambiguous_decls
;
20634 decl
= cp_parser_lookup_name (parser
, identifier
,
20636 /*is_template=*/false,
20637 /*is_namespace=*/false,
20638 /*check_dependency=*/true,
20642 /* If the lookup was ambiguous, an error will already have been
20644 if (ambiguous_decls
)
20645 return error_mark_node
;
20647 /* If we are parsing friend declaration, DECL may be a
20648 TEMPLATE_DECL tree node here. However, we need to check
20649 whether this TEMPLATE_DECL results in valid code. Consider
20650 the following example:
20653 template <class T> class C {};
20656 template <class T> friend class N::C; // #1, valid code
20658 template <class T> class Y {
20659 friend class N::C; // #2, invalid code
20662 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20663 name lookup of `N::C'. We see that friend declaration must
20664 be template for the code to be valid. Note that
20665 processing_template_decl does not work here since it is
20666 always 1 for the above two cases. */
20668 decl
= (cp_parser_maybe_treat_template_as_class
20669 (decl
, /*tag_name_p=*/is_friend
20670 && template_parm_lists_apply
));
20672 if (TREE_CODE (decl
) != TYPE_DECL
)
20674 cp_parser_diagnose_invalid_type_name (parser
,
20677 return error_mark_node
;
20680 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
20682 bool allow_template
= (template_parm_lists_apply
20683 || DECL_SELF_REFERENCE_P (decl
));
20684 type
= check_elaborated_type_specifier (tag_type
, decl
,
20687 if (type
== error_mark_node
)
20688 return error_mark_node
;
20691 /* Forward declarations of nested types, such as
20696 are invalid unless all components preceding the final '::'
20697 are complete. If all enclosing types are complete, these
20698 declarations become merely pointless.
20700 Invalid forward declarations of nested types are errors
20701 caught elsewhere in parsing. Those that are pointless arrive
20704 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
20705 && !is_friend
&& is_declaration
20706 && !processing_explicit_instantiation
)
20707 warning (0, "declaration %qD does not declare anything", decl
);
20709 type
= TREE_TYPE (decl
);
20713 /* An elaborated-type-specifier sometimes introduces a new type and
20714 sometimes names an existing type. Normally, the rule is that it
20715 introduces a new type only if there is not an existing type of
20716 the same name already in scope. For example, given:
20719 void f() { struct S s; }
20721 the `struct S' in the body of `f' is the same `struct S' as in
20722 the global scope; the existing definition is used. However, if
20723 there were no global declaration, this would introduce a new
20724 local class named `S'.
20726 An exception to this rule applies to the following code:
20728 namespace N { struct S; }
20730 Here, the elaborated-type-specifier names a new type
20731 unconditionally; even if there is already an `S' in the
20732 containing scope this declaration names a new type.
20733 This exception only applies if the elaborated-type-specifier
20734 forms the complete declaration:
20738 A declaration consisting solely of `class-key identifier ;' is
20739 either a redeclaration of the name in the current scope or a
20740 forward declaration of the identifier as a class name. It
20741 introduces the name into the current scope.
20743 We are in this situation precisely when the next token is a `;'.
20745 An exception to the exception is that a `friend' declaration does
20746 *not* name a new type; i.e., given:
20748 struct S { friend struct T; };
20750 `T' is not a new type in the scope of `S'.
20752 Also, `new struct S' or `sizeof (struct S)' never results in the
20753 definition of a new type; a new type can only be declared in a
20754 declaration context. */
20759 /* Friends have special name lookup rules. */
20760 how
= TAG_how::HIDDEN_FRIEND
;
20761 else if (is_declaration
20762 && cp_lexer_next_token_is (parser
->lexer
,
20764 /* This is a `class-key identifier ;' */
20765 how
= TAG_how::CURRENT_ONLY
;
20767 how
= TAG_how::GLOBAL
;
20770 (template_parm_lists_apply
20771 && (cp_parser_next_token_starts_class_definition_p (parser
)
20772 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
20773 /* An unqualified name was used to reference this type, so
20774 there were no qualifying templates. */
20775 if (template_parm_lists_apply
20776 && !cp_parser_check_template_parameters (parser
,
20777 /*num_templates=*/0,
20778 /*template_id*/false,
20780 /*declarator=*/NULL
))
20781 return error_mark_node
;
20783 type
= xref_tag (tag_type
, identifier
, how
, template_p
);
20787 if (type
== error_mark_node
)
20788 return error_mark_node
;
20790 /* Allow attributes on forward declarations of classes. */
20793 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20794 warning (OPT_Wattributes
,
20795 "attributes ignored on uninstantiated type");
20796 else if (tag_type
!= enum_type
20797 && TREE_CODE (type
) != BOUND_TEMPLATE_TEMPLATE_PARM
20798 && CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
20799 && ! processing_explicit_instantiation
)
20800 warning (OPT_Wattributes
,
20801 "attributes ignored on template instantiation");
20802 else if (is_friend
&& cxx11_attribute_p (attributes
))
20804 if (warning (OPT_Wattributes
, "attribute ignored"))
20805 inform (input_location
, "an attribute that appertains to a friend "
20806 "declaration that is not a definition is ignored");
20808 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
20809 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
20811 warning (OPT_Wattributes
,
20812 "attributes ignored on elaborated-type-specifier that is "
20813 "not a forward declaration");
20816 if (tag_type
== enum_type
)
20817 cp_parser_maybe_warn_enum_key (parser
, key_loc
, type
, scoped_key
);
20820 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
20821 for alias definition. */
20822 bool decl_class
= (is_declaration
20823 && cp_parser_declares_only_class_p (parser
));
20824 cp_parser_check_class_key (parser
, key_loc
, tag_type
, type
, false,
20827 /* Indicate whether this class was declared as a `class' or as a
20829 if (CLASS_TYPE_P (type
) && !currently_open_class (type
))
20830 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
20833 /* A "<" cannot follow an elaborated type specifier. If that
20834 happens, the user was probably trying to form a template-id. */
20835 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
20841 /* Parse an enum-specifier.
20844 enum-head { enumerator-list [opt] }
20845 enum-head { enumerator-list , } [C++0x]
20848 enum-key identifier [opt] enum-base [opt]
20849 enum-key nested-name-specifier identifier enum-base [opt]
20854 enum struct [C++0x]
20857 : type-specifier-seq
20859 opaque-enum-specifier:
20860 enum-key identifier enum-base [opt] ;
20863 enum-key attributes[opt] identifier [opt] enum-base [opt]
20864 { enumerator-list [opt] }attributes[opt]
20865 enum-key attributes[opt] identifier [opt] enum-base [opt]
20866 { enumerator-list, }attributes[opt] [C++0x]
20868 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20869 if the token stream isn't an enum-specifier after all. */
20872 cp_parser_enum_specifier (cp_parser
* parser
)
20875 tree type
= NULL_TREE
;
20877 tree nested_name_specifier
= NULL_TREE
;
20879 bool scoped_enum_p
= false;
20880 bool has_underlying_type
= false;
20881 bool nested_being_defined
= false;
20882 bool new_value_list
= false;
20883 bool is_new_type
= false;
20884 bool is_unnamed
= false;
20885 tree underlying_type
= NULL_TREE
;
20886 cp_token
*type_start_token
= NULL
;
20887 auto cleanup
= make_temp_override (parser
->colon_corrects_to_scope_p
, false);
20889 /* Parse tentatively so that we can back up if we don't find a
20891 cp_parser_parse_tentatively (parser
);
20893 /* Caller guarantees that the current token is 'enum', an identifier
20894 possibly follows, and the token after that is an opening brace.
20895 If we don't have an identifier, fabricate an anonymous name for
20896 the enumeration being defined. */
20897 cp_lexer_consume_token (parser
->lexer
);
20899 /* Parse the "class" or "struct", which indicates a scoped
20900 enumeration type in C++0x. */
20901 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
20902 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
20904 if (cxx_dialect
< cxx11
)
20905 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
20907 /* Consume the `struct' or `class' token. */
20908 cp_lexer_consume_token (parser
->lexer
);
20910 scoped_enum_p
= true;
20913 attributes
= cp_parser_attributes_opt (parser
);
20915 /* Clear the qualification. */
20916 parser
->scope
= NULL_TREE
;
20917 parser
->qualifying_scope
= NULL_TREE
;
20918 parser
->object_scope
= NULL_TREE
;
20920 /* Figure out in what scope the declaration is being placed. */
20921 prev_scope
= current_scope ();
20923 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20925 push_deferring_access_checks (dk_no_check
);
20926 nested_name_specifier
20927 = cp_parser_nested_name_specifier_opt (parser
,
20928 /*typename_keyword_p=*/true,
20929 /*check_dependency_p=*/false,
20931 /*is_declaration=*/false);
20933 if (nested_name_specifier
)
20937 identifier
= cp_parser_identifier (parser
);
20938 name
= cp_parser_lookup_name (parser
, identifier
,
20940 /*is_template=*/false,
20941 /*is_namespace=*/false,
20942 /*check_dependency=*/true,
20943 /*ambiguous_decls=*/NULL
,
20945 if (name
&& name
!= error_mark_node
)
20947 type
= TREE_TYPE (name
);
20948 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20950 /* Are template enums allowed in ISO? */
20951 if (template_parm_scope_p ())
20952 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
20953 "%qD is an enumeration template", name
);
20954 /* ignore a typename reference, for it will be solved by name
20959 else if (nested_name_specifier
== error_mark_node
)
20960 /* We already issued an error. */;
20963 error_at (type_start_token
->location
,
20964 "%qD does not name an enumeration in %qT",
20965 identifier
, nested_name_specifier
);
20966 nested_name_specifier
= error_mark_node
;
20971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20972 identifier
= cp_parser_identifier (parser
);
20975 identifier
= make_anon_name ();
20978 error_at (type_start_token
->location
,
20979 "unnamed scoped enum is not allowed");
20982 pop_deferring_access_checks ();
20984 /* Check for the `:' that denotes a specified underlying type in C++0x.
20985 Note that a ':' could also indicate a bitfield width, however. */
20986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20988 cp_decl_specifier_seq type_specifiers
;
20990 /* Consume the `:'. */
20991 cp_lexer_consume_token (parser
->lexer
);
20994 = make_temp_override (parser
->type_definition_forbidden_message
,
20995 G_("types may not be defined in enum-base"));
20997 /* Parse the type-specifier-seq. */
20998 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
20999 /*is_declaration=*/false,
21000 /*is_trailing_return=*/false,
21003 /* At this point this is surely not elaborated type specifier. */
21004 if (!cp_parser_parse_definitely (parser
))
21007 if (cxx_dialect
< cxx11
)
21008 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
21010 has_underlying_type
= true;
21012 /* If that didn't work, stop. */
21013 if (type_specifiers
.type
!= error_mark_node
)
21015 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
21016 /*initialized=*/0, NULL
);
21017 if (underlying_type
== error_mark_node
21018 || check_for_bare_parameter_packs (underlying_type
))
21019 underlying_type
= NULL_TREE
;
21023 /* Look for the `{' but don't consume it yet. */
21024 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21026 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
21028 if (has_underlying_type
)
21029 cp_parser_commit_to_tentative_parse (parser
);
21030 cp_parser_error (parser
, "expected %<{%>");
21031 if (has_underlying_type
)
21032 return error_mark_node
;
21034 /* An opaque-enum-specifier must have a ';' here. */
21035 if ((scoped_enum_p
|| underlying_type
)
21036 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21038 if (has_underlying_type
)
21039 cp_parser_commit_to_tentative_parse (parser
);
21040 cp_parser_error (parser
, "expected %<;%> or %<{%>");
21041 if (has_underlying_type
)
21042 return error_mark_node
;
21046 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
21049 if (nested_name_specifier
)
21051 if (CLASS_TYPE_P (nested_name_specifier
))
21053 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
21054 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
21055 push_scope (nested_name_specifier
);
21057 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
21058 push_nested_namespace (nested_name_specifier
);
21061 /* Issue an error message if type-definitions are forbidden here. */
21062 if (!cp_parser_check_type_definition (parser
))
21063 type
= error_mark_node
;
21065 /* Create the new type. We do this before consuming the opening
21066 brace so the enum will be recorded as being on the line of its
21067 tag (or the 'enum' keyword, if there is no tag). */
21068 type
= start_enum (identifier
, type
, underlying_type
,
21069 attributes
, scoped_enum_p
, &is_new_type
);
21071 /* If the next token is not '{' it is an opaque-enum-specifier or an
21072 elaborated-type-specifier. */
21073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21075 auto_timevar
tv (TV_PARSE_ENUM
);
21077 if (nested_name_specifier
21078 && nested_name_specifier
!= error_mark_node
)
21080 /* The following catches invalid code such as:
21081 enum class S<int>::E { A, B, C }; */
21082 if (!processing_specialization
21083 && CLASS_TYPE_P (nested_name_specifier
)
21084 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
21085 error_at (type_start_token
->location
, "cannot add an enumerator "
21086 "list to a template instantiation");
21088 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
21090 error_at (type_start_token
->location
,
21091 "%<%T::%E%> has not been declared",
21092 TYPE_CONTEXT (nested_name_specifier
),
21093 nested_name_specifier
);
21094 type
= error_mark_node
;
21096 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
21097 && !CLASS_TYPE_P (nested_name_specifier
))
21099 error_at (type_start_token
->location
, "nested name specifier "
21100 "%qT for enum declaration does not name a class "
21101 "or namespace", nested_name_specifier
);
21102 type
= error_mark_node
;
21104 /* If that scope does not contain the scope in which the
21105 class was originally declared, the program is invalid. */
21106 else if (prev_scope
&& !is_ancestor (prev_scope
,
21107 nested_name_specifier
))
21109 if (at_namespace_scope_p ())
21110 error_at (type_start_token
->location
,
21111 "declaration of %qD in namespace %qD which does not "
21113 type
, prev_scope
, nested_name_specifier
);
21115 error_at (type_start_token
->location
,
21116 "declaration of %qD in %qD which does not "
21118 type
, prev_scope
, nested_name_specifier
);
21119 type
= error_mark_node
;
21121 /* If that scope is the scope where the declaration is being placed
21122 the program is invalid. */
21123 else if (CLASS_TYPE_P (nested_name_specifier
)
21124 && CLASS_TYPE_P (prev_scope
)
21125 && same_type_p (nested_name_specifier
, prev_scope
))
21127 permerror (type_start_token
->location
,
21128 "extra qualification not allowed");
21129 nested_name_specifier
= NULL_TREE
;
21134 begin_scope (sk_scoped_enum
, type
);
21136 /* Consume the opening brace. */
21137 matching_braces braces
;
21138 braces
.consume_open (parser
);
21140 if (type
== error_mark_node
)
21141 ; /* Nothing to add */
21142 else if (OPAQUE_ENUM_P (type
)
21143 || (cxx_dialect
> cxx98
&& processing_specialization
))
21145 new_value_list
= true;
21146 SET_OPAQUE_ENUM_P (type
, false);
21147 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
21151 error_at (type_start_token
->location
,
21152 "multiple definition of %q#T", type
);
21153 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
21154 "previous definition here");
21155 type
= error_mark_node
;
21158 if (type
== error_mark_node
)
21159 cp_parser_skip_to_end_of_block_or_statement (parser
);
21160 /* If the next token is not '}', then there are some enumerators. */
21161 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
21163 if (is_unnamed
&& !scoped_enum_p
21164 /* Don't warn for enum {} a; here. */
21165 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SEMICOLON
))
21166 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
21167 "ISO C++ forbids empty unnamed enum");
21171 /* We've seen a '{' so we know we're in an enum-specifier.
21172 Commit to any tentative parse to get syntax errors. */
21173 cp_parser_commit_to_tentative_parse (parser
);
21174 cp_parser_enumerator_list (parser
, type
);
21177 /* Consume the final '}'. */
21178 braces
.require_close (parser
);
21185 /* If a ';' follows, then it is an opaque-enum-specifier
21186 and additional restrictions apply. */
21187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21190 error_at (type_start_token
->location
,
21191 "opaque-enum-specifier without name");
21192 else if (nested_name_specifier
)
21193 error_at (type_start_token
->location
,
21194 "opaque-enum-specifier must use a simple identifier");
21198 /* Look for trailing attributes to apply to this enumeration, and
21199 apply them if appropriate. */
21200 if (cp_parser_allow_gnu_extensions_p (parser
))
21202 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
21203 cplus_decl_attributes (&type
,
21205 (int) ATTR_FLAG_TYPE_IN_PLACE
);
21208 /* Finish up the enumeration. */
21209 if (type
!= error_mark_node
)
21211 if (new_value_list
)
21212 finish_enum_value_list (type
);
21214 finish_enum (type
);
21217 if (nested_name_specifier
)
21219 if (CLASS_TYPE_P (nested_name_specifier
))
21221 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
21222 pop_scope (nested_name_specifier
);
21224 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
21225 pop_nested_namespace (nested_name_specifier
);
21230 /* Parse an enumerator-list. The enumerators all have the indicated
21234 enumerator-definition
21235 enumerator-list , enumerator-definition */
21238 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
21242 /* Parse an enumerator-definition. */
21243 cp_parser_enumerator_definition (parser
, type
);
21245 /* If the next token is not a ',', we've reached the end of
21247 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21249 /* Otherwise, consume the `,' and keep going. */
21250 cp_lexer_consume_token (parser
->lexer
);
21251 /* If the next token is a `}', there is a trailing comma. */
21252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
21254 if (cxx_dialect
< cxx11
)
21255 pedwarn (input_location
, OPT_Wpedantic
,
21256 "comma at end of enumerator list");
21262 /* Parse an enumerator-definition. The enumerator has the indicated
21265 enumerator-definition:
21267 enumerator = constant-expression
21274 enumerator-definition:
21275 enumerator attributes [opt]
21276 enumerator attributes [opt] = constant-expression */
21279 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
21285 /* Save the input location because we are interested in the location
21286 of the identifier and not the location of the explicit value. */
21287 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21289 /* Look for the identifier. */
21290 identifier
= cp_parser_identifier (parser
);
21291 if (identifier
== error_mark_node
)
21294 /* Parse any specified attributes. */
21295 tree attrs
= cp_parser_attributes_opt (parser
);
21297 /* If the next token is an '=', then there is an explicit value. */
21298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21300 /* Consume the `=' token. */
21301 cp_lexer_consume_token (parser
->lexer
);
21302 /* Parse the value. */
21303 value
= cp_parser_constant_expression (parser
);
21308 /* If we are processing a template, make sure the initializer of the
21309 enumerator doesn't contain any bare template parameter pack. */
21310 if (current_lambda_expr ())
21312 /* In a lambda it should work, but doesn't currently. */
21313 if (uses_parameter_packs (value
))
21315 sorry ("unexpanded parameter pack in enumerator in lambda");
21316 value
= error_mark_node
;
21319 else if (check_for_bare_parameter_packs (value
))
21320 value
= error_mark_node
;
21322 /* Create the enumerator. */
21323 build_enumerator (identifier
, value
, type
, attrs
, loc
);
21326 /* Parse a namespace-name.
21329 original-namespace-name
21332 Returns the NAMESPACE_DECL for the namespace. */
21335 cp_parser_namespace_name (cp_parser
* parser
)
21338 tree namespace_decl
;
21340 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21342 /* Get the name of the namespace. */
21343 identifier
= cp_parser_identifier (parser
);
21344 if (identifier
== error_mark_node
)
21345 return error_mark_node
;
21347 /* Look up the identifier in the currently active scope. Look only
21348 for namespaces, due to:
21350 [basic.lookup.udir]
21352 When looking up a namespace-name in a using-directive or alias
21353 definition, only namespace names are considered.
21357 [basic.lookup.qual]
21359 During the lookup of a name preceding the :: scope resolution
21360 operator, object, function, and enumerator names are ignored.
21362 (Note that cp_parser_qualifying_entity only calls this
21363 function if the token after the name is the scope resolution
21365 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
21367 /*is_template=*/false,
21368 /*is_namespace=*/true,
21369 /*check_dependency=*/true,
21370 /*ambiguous_decls=*/NULL
,
21372 /* If it's not a namespace, issue an error. */
21373 if (namespace_decl
== error_mark_node
21374 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
21376 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21378 auto_diagnostic_group d
;
21380 if (namespace_decl
== error_mark_node
21381 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21382 hint
= suggest_alternative_in_explicit_scope (token
->location
,
21385 if (const char *suggestion
= hint
.suggestion ())
21387 gcc_rich_location
richloc (token
->location
);
21388 richloc
.add_fixit_replace (suggestion
);
21389 error_at (&richloc
,
21390 "%qD is not a namespace-name; did you mean %qs?",
21391 identifier
, suggestion
);
21394 error_at (token
->location
, "%qD is not a namespace-name",
21398 cp_parser_error (parser
, "expected namespace-name");
21399 namespace_decl
= error_mark_node
;
21402 return namespace_decl
;
21405 /* Parse a namespace-definition.
21407 namespace-definition:
21408 named-namespace-definition
21409 unnamed-namespace-definition
21411 named-namespace-definition:
21412 original-namespace-definition
21413 extension-namespace-definition
21415 original-namespace-definition:
21416 namespace identifier { namespace-body }
21418 extension-namespace-definition:
21419 namespace original-namespace-name { namespace-body }
21421 unnamed-namespace-definition:
21422 namespace { namespace-body } */
21425 cp_parser_namespace_definition (cp_parser
* parser
)
21428 int nested_definition_count
= 0;
21430 cp_ensure_no_omp_declare_simd (parser
);
21431 cp_ensure_no_oacc_routine (parser
);
21433 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
21434 const bool topmost_inline_p
= is_inline
;
21438 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
21439 cp_lexer_consume_token (parser
->lexer
);
21442 /* Look for the `namespace' keyword. */
21444 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
21446 /* Parse any specified attributes before the identifier. */
21447 tree attribs
= cp_parser_attributes_opt (parser
);
21451 identifier
= NULL_TREE
;
21453 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
21455 if (nested_inline_p
&& nested_definition_count
!= 0)
21457 if (pedantic
&& cxx_dialect
< cxx20
)
21458 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
21459 OPT_Wc__20_extensions
, "nested inline namespace "
21460 "definitions only available with %<-std=c++20%> or "
21461 "%<-std=gnu++20%>");
21462 cp_lexer_consume_token (parser
->lexer
);
21465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
21467 identifier
= cp_parser_identifier (parser
);
21469 if (cp_next_tokens_can_be_std_attribute_p (parser
))
21470 pedwarn (input_location
, OPT_Wpedantic
,
21471 "standard attributes on namespaces must precede "
21472 "the namespace name");
21474 /* Parse any attributes specified after the identifier. */
21475 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
21478 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
21480 /* Don't forget that the innermost namespace might have been
21481 marked as inline. Use |= because we cannot overwrite
21482 IS_INLINE in case the outermost namespace is inline, but
21483 there are no nested inlines. */
21484 is_inline
|= nested_inline_p
;
21488 if (!nested_definition_count
&& pedantic
&& cxx_dialect
< cxx17
)
21489 pedwarn (input_location
, OPT_Wc__17_extensions
,
21490 "nested namespace definitions only available with "
21491 "%<-std=c++17%> or %<-std=gnu++17%>");
21493 /* Nested namespace names can create new namespaces (unlike
21494 other qualified-ids). */
21495 if (int count
= (identifier
21496 ? push_namespace (identifier
, nested_inline_p
)
21498 nested_definition_count
+= count
;
21500 cp_parser_error (parser
, "nested namespace name required");
21501 cp_lexer_consume_token (parser
->lexer
);
21504 if (nested_definition_count
&& !identifier
)
21505 cp_parser_error (parser
, "namespace name required");
21507 if (nested_definition_count
&& attribs
)
21508 error_at (token
->location
,
21509 "a nested namespace definition cannot have attributes");
21510 if (nested_definition_count
&& topmost_inline_p
)
21511 error_at (token
->location
,
21512 "a nested namespace definition cannot be inline");
21514 /* Start the namespace. */
21515 nested_definition_count
+= push_namespace (identifier
, is_inline
);
21517 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
21519 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
21521 /* Look for the `{' to validate starting the namespace. */
21522 matching_braces braces
;
21523 if (braces
.require_open (parser
))
21525 /* Parse the body of the namespace. */
21526 cp_parser_namespace_body (parser
);
21528 /* Look for the final `}'. */
21529 braces
.require_close (parser
);
21532 if (has_visibility
)
21533 pop_visibility (1);
21535 /* Pop the nested namespace definitions. */
21536 while (nested_definition_count
--)
21540 /* Parse a namespace-body.
21543 declaration-seq [opt] */
21546 cp_parser_namespace_body (cp_parser
* parser
)
21548 cp_parser_declaration_seq_opt (parser
);
21551 /* Parse a namespace-alias-definition.
21553 namespace-alias-definition:
21554 namespace identifier = qualified-namespace-specifier ; */
21557 cp_parser_namespace_alias_definition (cp_parser
* parser
)
21560 tree namespace_specifier
;
21562 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21564 /* Look for the `namespace' keyword. */
21565 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
21566 /* Look for the identifier. */
21567 identifier
= cp_parser_identifier (parser
);
21568 if (identifier
== error_mark_node
)
21570 /* Look for the `=' token. */
21571 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
21572 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21574 error_at (token
->location
, "%<namespace%> definition is not allowed here");
21575 /* Skip the definition. */
21576 cp_lexer_consume_token (parser
->lexer
);
21577 if (cp_parser_skip_to_closing_brace (parser
))
21578 cp_lexer_consume_token (parser
->lexer
);
21581 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
21582 /* Look for the qualified-namespace-specifier. */
21583 namespace_specifier
21584 = cp_parser_qualified_namespace_specifier (parser
);
21585 cp_warn_deprecated_use_scopes (namespace_specifier
);
21586 /* Look for the `;' token. */
21587 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21589 /* Register the alias in the symbol table. */
21590 do_namespace_alias (identifier
, namespace_specifier
);
21593 /* Parse a qualified-namespace-specifier.
21595 qualified-namespace-specifier:
21596 :: [opt] nested-name-specifier [opt] namespace-name
21598 Returns a NAMESPACE_DECL corresponding to the specified
21602 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
21604 /* Look for the optional `::'. */
21605 cp_parser_global_scope_opt (parser
,
21606 /*current_scope_valid_p=*/false);
21608 /* Look for the optional nested-name-specifier. */
21609 cp_parser_nested_name_specifier_opt (parser
,
21610 /*typename_keyword_p=*/false,
21611 /*check_dependency_p=*/true,
21613 /*is_declaration=*/true);
21615 return cp_parser_namespace_name (parser
);
21618 /* Subroutine of cp_parser_using_declaration. */
21621 finish_using_decl (tree qscope
, tree identifier
, bool typename_p
= false)
21623 tree decl
= NULL_TREE
;
21624 if (at_class_scope_p ())
21626 /* Create the USING_DECL. */
21627 decl
= do_class_using_decl (qscope
, identifier
);
21629 if (check_for_bare_parameter_packs (decl
))
21630 return error_mark_node
;
21632 if (decl
&& typename_p
)
21633 USING_DECL_TYPENAME_P (decl
) = 1;
21635 /* Add it to the list of members in this class. */
21636 finish_member_declaration (decl
);
21639 finish_nonmember_using_decl (qscope
, identifier
);
21643 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21644 access declaration.
21647 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21648 using :: unqualified-id ;
21650 access-declaration:
21656 cp_parser_using_declaration (cp_parser
* parser
,
21657 bool access_declaration_p
)
21660 bool typename_p
= false;
21661 bool global_scope_p
;
21664 int oldcount
= errorcount
;
21665 cp_token
*diag_token
= NULL
;
21667 if (access_declaration_p
)
21669 diag_token
= cp_lexer_peek_token (parser
->lexer
);
21670 cp_parser_parse_tentatively (parser
);
21674 /* Look for the `using' keyword. */
21675 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
21678 /* Peek at the next token. */
21679 token
= cp_lexer_peek_token (parser
->lexer
);
21680 /* See if it's `typename'. */
21681 if (token
->keyword
== RID_TYPENAME
)
21683 /* Remember that we've seen it. */
21685 /* Consume the `typename' token. */
21686 cp_lexer_consume_token (parser
->lexer
);
21690 /* Look for the optional global scope qualification. */
21692 = (cp_parser_global_scope_opt (parser
,
21693 /*current_scope_valid_p=*/false)
21696 /* If we saw `typename', or didn't see `::', then there must be a
21697 nested-name-specifier present. */
21698 if (typename_p
|| !global_scope_p
)
21700 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
21701 /*check_dependency_p=*/true,
21703 /*is_declaration=*/true);
21704 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
21706 cp_parser_skip_to_end_of_block_or_statement (parser
);
21710 /* Otherwise, we could be in either of the two productions. In that
21711 case, treat the nested-name-specifier as optional. */
21713 qscope
= cp_parser_nested_name_specifier_opt (parser
,
21714 /*typename_keyword_p=*/false,
21715 /*check_dependency_p=*/true,
21717 /*is_declaration=*/true);
21719 qscope
= global_namespace
;
21721 cp_warn_deprecated_use_scopes (qscope
);
21723 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
21724 /* Something has already gone wrong; there's no need to parse
21725 further. Since an error has occurred, the return value of
21726 cp_parser_parse_definitely will be false, as required. */
21727 return cp_parser_parse_definitely (parser
);
21729 token
= cp_lexer_peek_token (parser
->lexer
);
21730 /* Parse the unqualified-id. */
21731 identifier
= cp_parser_unqualified_id (parser
,
21732 /*template_keyword_p=*/false,
21733 /*check_dependency_p=*/true,
21734 /*declarator_p=*/true,
21735 /*optional_p=*/false);
21737 if (access_declaration_p
)
21739 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21740 cp_parser_simulate_error (parser
);
21741 if (!cp_parser_parse_definitely (parser
))
21744 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21746 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
21747 if (cxx_dialect
< cxx17
)
21748 pedwarn (ell
->location
, OPT_Wc__17_extensions
,
21749 "pack expansion in using-declaration only available "
21750 "with %<-std=c++17%> or %<-std=gnu++17%>");
21751 qscope
= make_pack_expansion (qscope
);
21754 /* The function we call to handle a using-declaration is different
21755 depending on what scope we are in. */
21756 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
21758 else if (!identifier_p (identifier
)
21759 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
21760 /* [namespace.udecl]
21762 A using declaration shall not name a template-id. */
21763 error_at (token
->location
,
21764 "a template-id may not appear in a using-declaration");
21767 tree decl
= finish_using_decl (qscope
, identifier
, typename_p
);
21769 if (decl
== error_mark_node
)
21771 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21776 if (!access_declaration_p
21777 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21779 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
21780 if (cxx_dialect
< cxx17
)
21781 pedwarn (comma
->location
, OPT_Wc__17_extensions
,
21782 "comma-separated list in using-declaration only available "
21783 "with %<-std=c++17%> or %<-std=gnu++17%>");
21787 /* Look for the final `;'. */
21788 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21790 if (access_declaration_p
&& errorcount
== oldcount
)
21791 warning_at (diag_token
->location
, OPT_Wdeprecated
,
21792 "access declarations are deprecated "
21793 "in favour of using-declarations; "
21794 "suggestion: add the %<using%> keyword");
21799 /* C++20 using enum declaration.
21801 using-enum-declaration :
21802 using elaborated-enum-specifier ; */
21805 cp_parser_using_enum (cp_parser
*parser
)
21807 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
21809 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21810 breaks one of the motivating examples in using-enum-5.C.
21811 cp_parser_simple_type_specifier seems to be closer to what we actually
21812 want, though that hasn't been properly specified yet. */
21814 /* Consume 'enum'. */
21815 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
));
21816 cp_lexer_consume_token (parser
->lexer
);
21818 cp_token
*start
= cp_lexer_peek_token (parser
->lexer
);
21820 tree type
= (cp_parser_simple_type_specifier
21821 (parser
, NULL
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
21823 cp_token
*end
= cp_lexer_previous_token (parser
->lexer
);
21825 if (type
== error_mark_node
21826 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
21828 cp_parser_skip_to_end_of_block_or_statement (parser
);
21831 if (TREE_CODE (type
) == TYPE_DECL
)
21832 type
= TREE_TYPE (type
);
21834 /* The elaborated-enum-specifier shall not name a dependent type and the type
21835 shall have a reachable enum-specifier. */
21836 const char *msg
= nullptr;
21837 if (cxx_dialect
< cxx20
)
21838 msg
= _("%<using enum%> "
21839 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21840 else if (dependent_type_p (type
))
21841 msg
= _("%<using enum%> of dependent type %qT");
21842 else if (TREE_CODE (type
) != ENUMERAL_TYPE
)
21843 msg
= _("%<using enum%> of non-enumeration type %q#T");
21844 else if (!COMPLETE_TYPE_P (type
))
21845 msg
= _("%<using enum%> of incomplete type %qT");
21846 else if (OPAQUE_ENUM_P (type
))
21847 msg
= _("%<using enum%> of %qT before its enum-specifier");
21850 location_t loc
= make_location (start
, start
, end
);
21851 auto_diagnostic_group g
;
21852 error_at (loc
, msg
, type
);
21853 loc
= location_of (type
);
21854 if (cxx_dialect
< cxx20
|| loc
== input_location
)
21856 else if (OPAQUE_ENUM_P (type
))
21857 inform (loc
, "opaque-enum-declaration here");
21859 inform (loc
, "declared here");
21862 /* A using-enum-declaration introduces the enumerator names of the named
21863 enumeration as if by a using-declaration for each enumerator. */
21864 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
21865 for (tree v
= TYPE_VALUES (type
); v
; v
= TREE_CHAIN (v
))
21866 finish_using_decl (type
, DECL_NAME (TREE_VALUE (v
)));
21869 /* Parse an alias-declaration.
21872 using identifier attribute-specifier-seq [opt] = type-id */
21875 cp_parser_alias_declaration (cp_parser
* parser
)
21877 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
21878 location_t id_location
, type_location
;
21879 cp_declarator
*declarator
;
21880 cp_decl_specifier_seq decl_specs
;
21882 const char *saved_message
= NULL
;
21884 /* Look for the `using' keyword. */
21885 cp_token
*using_token
21886 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
21887 if (using_token
== NULL
)
21888 return error_mark_node
;
21890 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
21891 id
= cp_parser_identifier (parser
);
21892 if (id
== error_mark_node
)
21893 return error_mark_node
;
21895 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
21896 attributes
= cp_parser_attributes_opt (parser
);
21897 if (attributes
== error_mark_node
)
21898 return error_mark_node
;
21900 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
21902 if (cp_parser_error_occurred (parser
))
21903 return error_mark_node
;
21905 cp_parser_commit_to_tentative_parse (parser
);
21907 /* Now we are going to parse the type-id of the declaration. */
21912 "A type-specifier-seq shall not define a class or enumeration
21913 unless it appears in the type-id of an alias-declaration (7.1.3) that
21914 is not the declaration of a template-declaration."
21916 In other words, if we currently are in an alias template, the
21917 type-id should not define a type.
21919 So let's set parser->type_definition_forbidden_message in that
21920 case; cp_parser_check_type_definition (called by
21921 cp_parser_class_specifier) will then emit an error if a type is
21922 defined in the type-id. */
21923 if (parser
->num_template_parameter_lists
)
21925 saved_message
= parser
->type_definition_forbidden_message
;
21926 parser
->type_definition_forbidden_message
=
21927 G_("types may not be defined in alias template declarations");
21930 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
21933 /* Restore the error message if need be. */
21934 if (parser
->num_template_parameter_lists
)
21935 parser
->type_definition_forbidden_message
= saved_message
;
21937 if (type
== error_mark_node
21938 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
21940 cp_parser_skip_to_end_of_block_or_statement (parser
);
21941 return error_mark_node
;
21944 /* A typedef-name can also be introduced by an alias-declaration. The
21945 identifier following the using keyword becomes a typedef-name. It has
21946 the same semantics as if it were introduced by the typedef
21947 specifier. In particular, it does not define a new type and it shall
21948 not appear in the type-id. */
21950 clear_decl_specs (&decl_specs
);
21951 decl_specs
.type
= type
;
21952 if (attributes
!= NULL_TREE
)
21954 decl_specs
.attributes
= attributes
;
21955 set_and_check_decl_spec_loc (&decl_specs
,
21959 set_and_check_decl_spec_loc (&decl_specs
,
21962 set_and_check_decl_spec_loc (&decl_specs
,
21965 decl_specs
.locations
[ds_type_spec
] = type_location
;
21967 if (parser
->num_template_parameter_lists
21968 && !cp_parser_check_template_parameters (parser
,
21969 /*num_templates=*/0,
21970 /*template_id*/false,
21972 /*declarator=*/NULL
))
21973 return error_mark_node
;
21975 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
21977 member_p
= at_class_scope_p ();
21979 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
21980 NULL_TREE
, attributes
);
21982 decl
= start_decl (declarator
, &decl_specs
, 0,
21983 attributes
, NULL_TREE
, &pushed_scope
);
21984 if (decl
== error_mark_node
)
21987 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
21990 pop_scope (pushed_scope
);
21992 /* If decl is a template, return its TEMPLATE_DECL so that it gets
21993 added into the symbol table; otherwise, return the TYPE_DECL. */
21994 if (DECL_LANG_SPECIFIC (decl
)
21995 && DECL_TEMPLATE_INFO (decl
)
21996 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
21998 decl
= DECL_TI_TEMPLATE (decl
);
22000 check_member_template (decl
);
22006 /* Parse a using-directive.
22009 attribute-specifier-seq [opt] using namespace :: [opt]
22010 nested-name-specifier [opt] namespace-name ; */
22013 cp_parser_using_directive (cp_parser
* parser
)
22015 tree namespace_decl
;
22016 tree attribs
= cp_parser_std_attribute_spec_seq (parser
);
22017 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22019 /* Error during attribute parsing that resulted in skipping
22020 to next semicolon. */
22021 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22025 /* Look for the `using' keyword. */
22026 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
22027 /* And the `namespace' keyword. */
22028 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
22029 /* Look for the optional `::' operator. */
22030 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
22031 /* And the optional nested-name-specifier. */
22032 cp_parser_nested_name_specifier_opt (parser
,
22033 /*typename_keyword_p=*/false,
22034 /*check_dependency_p=*/true,
22036 /*is_declaration=*/true);
22037 /* Get the namespace being used. */
22038 namespace_decl
= cp_parser_namespace_name (parser
);
22039 cp_warn_deprecated_use_scopes (namespace_decl
);
22040 /* And any specified GNU attributes. */
22041 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
22042 attribs
= chainon (attribs
, cp_parser_gnu_attributes_opt (parser
));
22044 /* Update the symbol table. */
22045 finish_using_directive (namespace_decl
, attribs
);
22047 /* Look for the final `;'. */
22048 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22051 /* Parse an asm-definition.
22058 asm-qualifier-list:
22060 asm-qualifier-list asm-qualifier
22063 asm ( string-literal ) ;
22068 asm asm-qualifier-list [opt] ( string-literal ) ;
22069 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
22070 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22071 : asm-operand-list [opt] ) ;
22072 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22073 : asm-operand-list [opt]
22074 : asm-clobber-list [opt] ) ;
22075 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
22076 : asm-clobber-list [opt]
22077 : asm-goto-list ) ;
22079 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22080 contains goto, and is the only allowed form in that case. No duplicates are
22081 allowed in an asm-qualifier-list. */
22084 cp_parser_asm_definition (cp_parser
* parser
)
22087 tree outputs
= NULL_TREE
;
22088 tree inputs
= NULL_TREE
;
22089 tree clobbers
= NULL_TREE
;
22090 tree labels
= NULL_TREE
;
22092 bool extended_p
= false;
22093 bool invalid_inputs_p
= false;
22094 bool invalid_outputs_p
= false;
22095 required_token missing
= RT_NONE
;
22096 location_t asm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22098 /* Look for the `asm' keyword. */
22099 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
22101 /* In C++20, unevaluated inline assembly is permitted in constexpr
22103 if (parser
->in_function_body
22104 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
22105 && cxx_dialect
< cxx20
)
22106 pedwarn (asm_loc
, OPT_Wc__20_extensions
, "%<asm%> in %<constexpr%> "
22107 "function only available with %<-std=c++20%> or "
22108 "%<-std=gnu++20%>");
22110 /* Handle the asm-qualifier-list. */
22111 location_t volatile_loc
= UNKNOWN_LOCATION
;
22112 location_t inline_loc
= UNKNOWN_LOCATION
;
22113 location_t goto_loc
= UNKNOWN_LOCATION
;
22114 location_t first_loc
= UNKNOWN_LOCATION
;
22116 if (cp_parser_allow_gnu_extensions_p (parser
))
22119 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22120 location_t loc
= token
->location
;
22121 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
22126 error_at (loc
, "duplicate %<asm%> qualifier %qT",
22128 inform (volatile_loc
, "first seen here");
22132 if (!parser
->in_function_body
)
22133 warning_at (loc
, 0, "%<asm%> qualifier %qT ignored "
22134 "outside of function body", token
->u
.value
);
22135 volatile_loc
= loc
;
22137 cp_lexer_consume_token (parser
->lexer
);
22143 error_at (loc
, "duplicate %<asm%> qualifier %qT",
22145 inform (inline_loc
, "first seen here");
22151 cp_lexer_consume_token (parser
->lexer
);
22157 error_at (loc
, "duplicate %<asm%> qualifier %qT",
22159 inform (goto_loc
, "first seen here");
22165 cp_lexer_consume_token (parser
->lexer
);
22170 error_at (loc
, "%qT is not an %<asm%> qualifier", token
->u
.value
);
22171 cp_lexer_consume_token (parser
->lexer
);
22180 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
22181 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
22182 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
22184 if (!parser
->in_function_body
&& (inline_p
|| goto_p
))
22186 error_at (first_loc
, "%<asm%> qualifier outside of function body");
22187 inline_p
= goto_p
= false;
22190 /* Look for the opening `('. */
22191 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22193 /* Look for the string. */
22194 string
= cp_parser_string_literal (parser
, false, false);
22195 if (string
== error_mark_node
)
22197 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
22198 /*consume_paren=*/true);
22202 /* If we're allowing GNU extensions, check for the extended assembly
22203 syntax. Unfortunately, the `:' tokens need not be separated by
22204 a space in C, and so, for compatibility, we tolerate that here
22205 too. Doing that means that we have to treat the `::' operator as
22207 if (cp_parser_allow_gnu_extensions_p (parser
)
22208 && parser
->in_function_body
22209 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
22210 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
22212 bool inputs_p
= false;
22213 bool clobbers_p
= false;
22214 bool labels_p
= false;
22216 /* The extended syntax was used. */
22219 /* Look for outputs. */
22220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22222 /* Consume the `:'. */
22223 cp_lexer_consume_token (parser
->lexer
);
22224 /* Parse the output-operands. */
22225 if (cp_lexer_next_token_is_not (parser
->lexer
,
22227 && cp_lexer_next_token_is_not (parser
->lexer
,
22229 && cp_lexer_next_token_is_not (parser
->lexer
,
22232 outputs
= cp_parser_asm_operand_list (parser
);
22233 if (outputs
== error_mark_node
)
22234 invalid_outputs_p
= true;
22237 /* If the next token is `::', there are no outputs, and the
22238 next token is the beginning of the inputs. */
22239 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22240 /* The inputs are coming next. */
22243 /* Look for inputs. */
22245 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22247 /* Consume the `:' or `::'. */
22248 cp_lexer_consume_token (parser
->lexer
);
22249 /* Parse the output-operands. */
22250 if (cp_lexer_next_token_is_not (parser
->lexer
,
22252 && cp_lexer_next_token_is_not (parser
->lexer
,
22254 && cp_lexer_next_token_is_not (parser
->lexer
,
22257 inputs
= cp_parser_asm_operand_list (parser
);
22258 if (inputs
== error_mark_node
)
22259 invalid_inputs_p
= true;
22262 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22263 /* The clobbers are coming next. */
22266 /* Look for clobbers. */
22268 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22271 /* Consume the `:' or `::'. */
22272 cp_lexer_consume_token (parser
->lexer
);
22273 /* Parse the clobbers. */
22274 if (cp_lexer_next_token_is_not (parser
->lexer
,
22276 && cp_lexer_next_token_is_not (parser
->lexer
,
22278 clobbers
= cp_parser_asm_clobber_list (parser
);
22280 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22281 /* The labels are coming next. */
22284 /* Look for labels. */
22286 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
22289 /* Consume the `:' or `::'. */
22290 cp_lexer_consume_token (parser
->lexer
);
22291 /* Parse the labels. */
22292 labels
= cp_parser_asm_label_list (parser
);
22295 if (goto_p
&& !labels_p
)
22296 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
22299 missing
= RT_COLON_SCOPE
;
22301 /* Look for the closing `)'. */
22302 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
22303 missing
? missing
: RT_CLOSE_PAREN
))
22304 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
22305 /*consume_paren=*/true);
22306 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22308 if (!invalid_inputs_p
&& !invalid_outputs_p
)
22310 /* Create the ASM_EXPR. */
22311 if (parser
->in_function_body
)
22313 asm_stmt
= finish_asm_stmt (asm_loc
, volatile_p
, string
, outputs
,
22314 inputs
, clobbers
, labels
, inline_p
);
22315 /* If the extended syntax was not used, mark the ASM_EXPR. */
22318 tree temp
= asm_stmt
;
22319 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
22320 temp
= TREE_OPERAND (temp
, 0);
22322 ASM_INPUT_P (temp
) = 1;
22326 symtab
->finalize_toplevel_asm (string
);
22330 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22331 type that comes from the decl-specifier-seq. */
22334 strip_declarator_types (tree type
, cp_declarator
*declarator
)
22336 for (cp_declarator
*d
= declarator
; d
;)
22346 if (TYPE_PTRMEMFUNC_P (type
))
22347 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
22348 type
= TREE_TYPE (type
);
22356 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22357 a construct looks like a variable definition but is actually a function
22358 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22359 is the declarator for this function declaration. */
22362 warn_about_ambiguous_parse (const cp_decl_specifier_seq
*decl_specifiers
,
22363 const cp_declarator
*declarator
)
22365 /* Only warn if we are declaring a function at block scope. */
22366 if (!at_function_scope_p ())
22369 /* And only if there is no storage class specified. */
22370 if (decl_specifiers
->storage_class
!= sc_none
22371 || decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
22374 if (declarator
->kind
!= cdk_function
22375 || !declarator
->declarator
22376 || declarator
->declarator
->kind
!= cdk_id
22377 || !identifier_p (get_unqualified_id
22378 (const_cast<cp_declarator
*>(declarator
))))
22381 /* Don't warn when the whole declarator (not just the declarator-id!)
22382 was parenthesized. That is, don't warn for int(n()) but do warn
22384 if (declarator
->parenthesized
!= UNKNOWN_LOCATION
)
22388 if (decl_specifiers
->type
)
22390 type
= decl_specifiers
->type
;
22391 if (TREE_CODE (type
) == TYPE_DECL
)
22392 type
= TREE_TYPE (type
);
22394 /* If the return type is void there is no ambiguity. */
22395 if (same_type_p (type
, void_type_node
))
22398 else if (decl_specifiers
->any_type_specifiers_p
)
22399 /* Code like long f(); will have null ->type. If we have any
22400 type-specifiers, pretend we've seen int. */
22401 type
= integer_type_node
;
22405 auto_diagnostic_group d
;
22406 location_t loc
= declarator
->u
.function
.parens_loc
;
22407 tree params
= declarator
->u
.function
.parameters
;
22408 const bool has_list_ctor_p
= CLASS_TYPE_P (type
) && TYPE_HAS_LIST_CTOR (type
);
22410 /* The T t() case. */
22411 if (params
== void_list_node
)
22413 if (warning_at (loc
, OPT_Wvexing_parse
,
22414 "empty parentheses were disambiguated as a function "
22417 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22418 means value-initialization or aggregate-initialization, nothing
22419 means default-initialization. We can only suggest removing the
22420 parentheses/adding {} if T has a default constructor. */
22421 if (!CLASS_TYPE_P (type
) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
22423 gcc_rich_location
iloc (loc
);
22424 iloc
.add_fixit_remove ();
22425 inform (&iloc
, "remove parentheses to default-initialize "
22427 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
22429 if (CP_AGGREGATE_TYPE_P (type
))
22430 inform (loc
, "or replace parentheses with braces to "
22431 "aggregate-initialize a variable");
22433 inform (loc
, "or replace parentheses with braces to "
22434 "value-initialize a variable");
22441 /* If we had (...) or the parameter-list wasn't parenthesized,
22443 if (params
== NULL_TREE
|| !PARENTHESIZED_LIST_P (params
))
22446 /* The T t(X()) case. */
22447 if (list_length (params
) == 2)
22449 if (warning_at (loc
, OPT_Wvexing_parse
,
22450 "parentheses were disambiguated as a function "
22453 gcc_rich_location
iloc (loc
);
22454 /* {}-initialization means that we can use an initializer-list
22455 constructor if no default constructor is available, so don't
22456 suggest using {} for classes that have an initializer_list
22458 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
22460 iloc
.add_fixit_replace (get_start (loc
), "{");
22461 iloc
.add_fixit_replace (get_finish (loc
), "}");
22462 inform (&iloc
, "replace parentheses with braces to declare a "
22467 iloc
.add_fixit_insert_after (get_start (loc
), "(");
22468 iloc
.add_fixit_insert_before (get_finish (loc
), ")");
22469 inform (&iloc
, "add parentheses to declare a variable");
22473 /* The T t(X(), X()) case. */
22474 else if (warning_at (loc
, OPT_Wvexing_parse
,
22475 "parentheses were disambiguated as a function "
22478 gcc_rich_location
iloc (loc
);
22479 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
22481 iloc
.add_fixit_replace (get_start (loc
), "{");
22482 iloc
.add_fixit_replace (get_finish (loc
), "}");
22483 inform (&iloc
, "replace parentheses with braces to declare a "
22489 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22490 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22491 has the same meaning as in cp_parser_declarator. */
22494 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser
*parser
,
22495 cp_decl_specifier_seq
*decl_specs
,
22496 cp_declarator
*declarator
,
22497 int *ctor_dtor_or_conv_p
)
22499 if (cxx_dialect
>= cxx17
22500 && *ctor_dtor_or_conv_p
<= 0
22501 && !decl_specs
->type
22502 && !decl_specs
->any_type_specifiers_p
22503 && function_declarator_p (declarator
))
22505 cp_declarator
*id
= get_id_declarator (declarator
);
22506 tree name
= id
->u
.id
.unqualified_name
;
22507 parser
->scope
= id
->u
.id
.qualifying_scope
;
22508 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
22510 && (DECL_CLASS_TEMPLATE_P (tmpl
)
22511 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
22513 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
22514 id
->u
.id
.sfk
= sfk_deduction_guide
;
22515 *ctor_dtor_or_conv_p
= 1;
22520 /* Declarators [gram.dcl.decl] */
22522 /* Parse an init-declarator.
22525 declarator initializer [opt]
22530 declarator asm-specification [opt] attributes [opt] initializer [opt]
22532 function-definition:
22533 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22535 decl-specifier-seq [opt] declarator function-try-block
22539 function-definition:
22540 __extension__ function-definition
22544 function-definition:
22545 decl-specifier-seq [opt] declarator function-transaction-block
22547 The parser flags FLAGS is used to control type-specifier parsing.
22549 The DECL_SPECIFIERS apply to this declarator. Returns a
22550 representation of the entity declared. If MEMBER_P is TRUE, then
22551 this declarator appears in a class scope. The new DECL created by
22552 this declarator is returned.
22554 The CHECKS are access checks that should be performed once we know
22555 what entity is being declared (and, therefore, what classes have
22558 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22559 for a function-definition here as well. If the declarator is a
22560 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22561 be TRUE upon return. By that point, the function-definition will
22562 have been completely parsed.
22564 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22567 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22568 parsed declaration if it is an uninitialized single declarator not followed
22569 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22570 if present, will not be consumed. If returned, this declarator will be
22571 created with SD_INITIALIZED but will not call cp_finish_decl.
22573 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22574 and there is an initializer, the pointed location_t is set to the
22575 location of the '=' or `(', or '{' in C++11 token introducing the
22579 cp_parser_init_declarator (cp_parser
* parser
,
22580 cp_parser_flags flags
,
22581 cp_decl_specifier_seq
*decl_specifiers
,
22582 vec
<deferred_access_check
, va_gc
> *checks
,
22583 bool function_definition_allowed_p
,
22585 int declares_class_or_enum
,
22586 bool* function_definition_p
,
22587 tree
* maybe_range_for_decl
,
22588 location_t
* init_loc
,
22591 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
22592 *attributes_start_token
= NULL
;
22593 cp_declarator
*declarator
;
22594 tree prefix_attributes
;
22595 tree attributes
= NULL
;
22596 tree asm_specification
;
22598 tree decl
= NULL_TREE
;
22600 int is_initialized
;
22601 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22602 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22604 enum cpp_ttype initialization_kind
;
22605 bool is_direct_init
= false;
22606 bool is_non_constant_init
;
22607 int ctor_dtor_or_conv_p
;
22608 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
22609 bool static_p
= decl_specifiers
->storage_class
== sc_static
;
22610 tree pushed_scope
= NULL_TREE
;
22611 bool range_for_decl_p
= false;
22612 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22613 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
22615 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_consteval
))
22616 flags
|= CP_PARSER_FLAGS_CONSTEVAL
;
22618 /* Assume that this is not the declarator for a function
22620 if (function_definition_p
)
22621 *function_definition_p
= false;
22623 /* Default arguments are only permitted for function parameters. */
22624 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
22625 parser
->default_arg_ok_p
= false;
22627 /* Defer access checks while parsing the declarator; we cannot know
22628 what names are accessible until we know what is being
22630 resume_deferring_access_checks ();
22632 token
= cp_lexer_peek_token (parser
->lexer
);
22634 /* Parse the declarator. */
22636 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
22637 flags
, &ctor_dtor_or_conv_p
,
22638 /*parenthesized_p=*/NULL
,
22639 member_p
, friend_p
, static_p
);
22640 /* Gather up the deferred checks. */
22641 stop_deferring_access_checks ();
22643 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22645 /* If the DECLARATOR was erroneous, there's no need to go
22647 if (declarator
== cp_error_declarator
)
22648 return error_mark_node
;
22650 /* Check that the number of template-parameter-lists is OK. */
22651 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
22653 return error_mark_node
;
22655 if (declares_class_or_enum
& 2)
22656 cp_parser_check_for_definition_in_return_type (declarator
,
22657 decl_specifiers
->type
,
22658 decl_specifiers
->locations
[ds_type_spec
]);
22660 /* Figure out what scope the entity declared by the DECLARATOR is
22661 located in. `grokdeclarator' sometimes changes the scope, so
22662 we compute it now. */
22663 scope
= get_scope_of_declarator (declarator
);
22665 /* Perform any lookups in the declared type which were thought to be
22666 dependent, but are not in the scope of the declarator. */
22667 decl_specifiers
->type
22668 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
22670 /* If we're allowing GNU extensions, look for an
22671 asm-specification. */
22672 if (cp_parser_allow_gnu_extensions_p (parser
))
22674 /* Look for an asm-specification. */
22675 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
22676 asm_specification
= cp_parser_asm_specification_opt (parser
);
22679 asm_specification
= NULL_TREE
;
22681 /* Gather the attributes that were provided with the
22682 decl-specifiers. */
22683 prefix_attributes
= decl_specifiers
->attributes
;
22685 /* Look for attributes. */
22686 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
22687 attributes
= cp_parser_attributes_opt (parser
);
22689 /* Peek at the next token. */
22690 token
= cp_lexer_peek_token (parser
->lexer
);
22692 bool bogus_implicit_tmpl
= false;
22694 if (function_declarator_p (declarator
))
22696 /* Handle C++17 deduction guides. Note that class-scope
22697 non-template deduction guides are instead handled in
22698 cp_parser_member_declaration. */
22699 cp_parser_maybe_adjust_declarator_for_dguide (parser
,
22702 &ctor_dtor_or_conv_p
);
22704 if (!member_p
&& !cp_parser_error_occurred (parser
))
22705 warn_about_ambiguous_parse (decl_specifiers
, declarator
);
22707 /* Check to see if the token indicates the start of a
22708 function-definition. */
22709 if (cp_parser_token_starts_function_definition_p (token
))
22711 if (!function_definition_allowed_p
)
22713 /* If a function-definition should not appear here, issue an
22715 cp_parser_error (parser
,
22716 "a function-definition is not allowed here");
22717 return error_mark_node
;
22720 location_t func_brace_location
22721 = cp_lexer_peek_token (parser
->lexer
)->location
;
22723 /* Neither attributes nor an asm-specification are allowed
22724 on a function-definition. */
22725 if (asm_specification
)
22726 error_at (asm_spec_start_token
->location
,
22727 "an %<asm%> specification is not allowed "
22728 "on a function-definition");
22730 error_at (attributes_start_token
->location
,
22731 "attributes are not allowed "
22732 "on a function-definition");
22733 /* This is a function-definition. */
22734 *function_definition_p
= true;
22736 /* Parse the function definition. */
22738 decl
= cp_parser_save_member_function_body (parser
,
22741 prefix_attributes
);
22744 (cp_parser_function_definition_from_specifiers_and_declarator
22745 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
22747 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
22749 /* This is where the prologue starts... */
22750 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
22751 = func_brace_location
;
22757 else if (parser
->fully_implicit_function_template_p
)
22759 /* A non-template declaration involving a function parameter list
22760 containing an implicit template parameter will be made into a
22761 template. If the resulting declaration is not going to be an
22762 actual function then finish the template scope here to prevent it.
22763 An error message will be issued once we have a decl to talk about.
22765 FIXME probably we should do type deduction rather than create an
22766 implicit template, but the standard currently doesn't allow it. */
22767 bogus_implicit_tmpl
= true;
22768 finish_fully_implicit_template (parser
, NULL_TREE
);
22773 Only in function declarations for constructors, destructors, type
22774 conversions, and deduction guides can the decl-specifier-seq be omitted.
22776 We explicitly postpone this check past the point where we handle
22777 function-definitions because we tolerate function-definitions
22778 that are missing their return types in some modes. */
22779 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
22781 cp_parser_error (parser
,
22782 "expected constructor, destructor, or type conversion");
22783 return error_mark_node
;
22786 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
22787 an initializer as well. */
22788 if (token
->type
== CPP_EQ
22789 || token
->type
== CPP_OPEN_PAREN
22790 || token
->type
== CPP_OPEN_BRACE
)
22792 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
22793 It isn't; it's an expression. (Here '(i)' would have already been
22794 parsed as a declarator.) */
22795 if (token
->type
== CPP_OPEN_PAREN
22796 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
22798 cp_lexer_save_tokens (parser
->lexer
);
22799 cp_lexer_consume_token (parser
->lexer
);
22800 cp_parser_skip_to_closing_parenthesis (parser
,
22801 /*recovering*/false,
22803 /*consume_paren*/true);
22804 /* If this is an initializer, only a ',' or ';' can follow: either
22805 we have another init-declarator, or we're at the end of an
22806 init-declarator-list which can only be followed by a ';'. */
22807 bool ok
= (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22808 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
22809 cp_lexer_rollback_tokens (parser
->lexer
);
22810 if (UNLIKELY (!ok
))
22811 /* Not an init-declarator. */
22812 return error_mark_node
;
22814 is_initialized
= SD_INITIALIZED
;
22815 initialization_kind
= token
->type
;
22816 declarator
->init_loc
= token
->location
;
22817 if (maybe_range_for_decl
)
22818 *maybe_range_for_decl
= error_mark_node
;
22819 tmp_init_loc
= token
->location
;
22820 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
22821 *init_loc
= tmp_init_loc
;
22823 if (token
->type
== CPP_EQ
22824 && function_declarator_p (declarator
))
22826 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22827 if (t2
->keyword
== RID_DEFAULT
)
22828 is_initialized
= SD_DEFAULTED
;
22829 else if (t2
->keyword
== RID_DELETE
)
22830 is_initialized
= SD_DELETED
;
22835 /* If the init-declarator isn't initialized and isn't followed by a
22836 `,' or `;', it's not a valid init-declarator. */
22837 if (token
->type
!= CPP_COMMA
22838 && token
->type
!= CPP_SEMICOLON
)
22840 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
22841 range_for_decl_p
= true;
22844 if (!maybe_range_for_decl
)
22845 cp_parser_error (parser
, "expected initializer");
22846 return error_mark_node
;
22849 is_initialized
= SD_UNINITIALIZED
;
22850 initialization_kind
= CPP_EOF
;
22853 /* Because start_decl has side-effects, we should only call it if we
22854 know we're going ahead. By this point, we know that we cannot
22855 possibly be looking at any other construct. */
22856 cp_parser_commit_to_tentative_parse (parser
);
22858 /* Enter the newly declared entry in the symbol table. If we're
22859 processing a declaration in a class-specifier, we wait until
22860 after processing the initializer. */
22863 if (parser
->in_unbraced_linkage_specification_p
)
22864 decl_specifiers
->storage_class
= sc_extern
;
22865 decl
= start_decl (declarator
, decl_specifiers
,
22866 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
22867 attributes
, prefix_attributes
, &pushed_scope
);
22868 cp_finalize_omp_declare_simd (parser
, decl
);
22869 cp_finalize_oacc_routine (parser
, decl
, false);
22870 /* Adjust location of decl if declarator->id_loc is more appropriate:
22871 set, and decl wasn't merged with another decl, in which case its
22872 location would be different from input_location, and more accurate. */
22874 && declarator
->id_loc
!= UNKNOWN_LOCATION
22875 && DECL_SOURCE_LOCATION (decl
) == input_location
)
22876 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
22879 /* Enter the SCOPE. That way unqualified names appearing in the
22880 initializer will be looked up in SCOPE. */
22881 pushed_scope
= push_scope (scope
);
22883 /* Perform deferred access control checks, now that we know in which
22884 SCOPE the declared entity resides. */
22885 if (!member_p
&& decl
)
22887 tree saved_current_function_decl
= NULL_TREE
;
22889 /* If the entity being declared is a function, pretend that we
22890 are in its scope. If it is a `friend', it may have access to
22891 things that would not otherwise be accessible. */
22892 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22894 saved_current_function_decl
= current_function_decl
;
22895 current_function_decl
= decl
;
22898 /* Perform access checks for template parameters. */
22899 cp_parser_perform_template_parameter_access_checks (checks
);
22901 /* Perform the access control checks for the declarator and the
22902 decl-specifiers. */
22903 perform_deferred_access_checks (tf_warning_or_error
);
22905 /* Restore the saved value. */
22906 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22907 current_function_decl
= saved_current_function_decl
;
22910 /* Parse the initializer. */
22911 initializer
= NULL_TREE
;
22912 is_direct_init
= false;
22913 is_non_constant_init
= true;
22914 if (is_initialized
)
22916 if (function_declarator_p (declarator
))
22918 if (initialization_kind
== CPP_EQ
)
22919 initializer
= cp_parser_pure_specifier (parser
);
22922 /* If the declaration was erroneous, we don't really
22923 know what the user intended, so just silently
22924 consume the initializer. */
22925 if (decl
!= error_mark_node
)
22926 error_at (tmp_init_loc
, "initializer provided for function");
22927 cp_parser_skip_to_closing_parenthesis (parser
,
22928 /*recovering=*/true,
22929 /*or_comma=*/false,
22930 /*consume_paren=*/true);
22935 /* We want to record the extra mangling scope for in-class
22936 initializers of class members and initializers of static
22937 data member templates and namespace-scope initializers.
22938 The former involves deferring parsing of the initializer
22939 until end of class as with default arguments. So right
22940 here we only handle the latter two. */
22941 bool has_lambda_scope
= false;
22943 if (decl
!= error_mark_node
22945 && (processing_template_decl
|| DECL_NAMESPACE_SCOPE_P (decl
)))
22946 has_lambda_scope
= true;
22948 if (has_lambda_scope
)
22949 start_lambda_scope (decl
);
22950 initializer
= cp_parser_initializer (parser
,
22952 &is_non_constant_init
);
22953 if (has_lambda_scope
)
22954 finish_lambda_scope ();
22955 if (initializer
== error_mark_node
)
22956 cp_parser_skip_to_end_of_statement (parser
);
22960 /* The old parser allows attributes to appear after a parenthesized
22961 initializer. Mark Mitchell proposed removing this functionality
22962 on the GCC mailing lists on 2002-08-13. This parser accepts the
22963 attributes -- but ignores them. Made a permerror in GCC 8. */
22964 if (cp_parser_allow_gnu_extensions_p (parser
)
22965 && initialization_kind
== CPP_OPEN_PAREN
22966 && cp_parser_attributes_opt (parser
)
22967 && permerror (input_location
,
22968 "attributes after parenthesized initializer ignored"))
22971 if (flag_permissive
&& !hint
)
22974 inform (input_location
,
22975 "this flexibility is deprecated and will be removed");
22979 /* And now complain about a non-function implicit template. */
22980 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
22981 error_at (DECL_SOURCE_LOCATION (decl
),
22982 "non-function %qD declared as implicit template", decl
);
22984 /* For an in-class declaration, use `grokfield' to create the
22990 pop_scope (pushed_scope
);
22991 pushed_scope
= NULL_TREE
;
22993 decl
= grokfield (declarator
, decl_specifiers
,
22994 initializer
, !is_non_constant_init
,
22995 /*asmspec=*/NULL_TREE
,
22996 attr_chainon (attributes
, prefix_attributes
));
22997 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
22998 cp_parser_save_default_args (parser
, decl
);
22999 cp_finalize_omp_declare_simd (parser
, decl
);
23000 cp_finalize_oacc_routine (parser
, decl
, false);
23003 /* Finish processing the declaration. But, skip member
23005 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
23007 cp_finish_decl (decl
,
23008 initializer
, !is_non_constant_init
,
23010 /* If the initializer is in parentheses, then this is
23011 a direct-initialization, which means that an
23012 `explicit' constructor is OK. Otherwise, an
23013 `explicit' constructor cannot be used. */
23014 ((is_direct_init
|| !is_initialized
)
23015 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
23017 else if ((cxx_dialect
!= cxx98
) && friend_p
23018 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
23019 /* Core issue #226 (C++0x only): A default template-argument
23020 shall not be specified in a friend class template
23022 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
23023 /*is_partial=*/false, /*is_friend_decl=*/1);
23025 if (!friend_p
&& pushed_scope
)
23026 pop_scope (pushed_scope
);
23028 if (function_declarator_p (declarator
)
23029 && parser
->fully_implicit_function_template_p
)
23032 decl
= finish_fully_implicit_template (parser
, decl
);
23034 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
23037 if (auto_result
&& is_initialized
&& decl_specifiers
->type
23038 && type_uses_auto (decl_specifiers
->type
))
23039 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
23044 /* Parse a declarator.
23048 ptr-operator declarator
23050 abstract-declarator:
23051 ptr-operator abstract-declarator [opt]
23052 direct-abstract-declarator
23057 attributes [opt] direct-declarator
23058 attributes [opt] ptr-operator declarator
23060 abstract-declarator:
23061 attributes [opt] ptr-operator abstract-declarator [opt]
23062 attributes [opt] direct-abstract-declarator
23064 The parser flags FLAGS is used to control type-specifier parsing.
23066 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23067 detect constructors, destructors, deduction guides, or conversion operators.
23068 It is set to -1 if the declarator is a name, and +1 if it is a
23069 function. Otherwise it is set to zero. Usually you just want to
23070 test for >0, but internally the negative value is used.
23072 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23073 a decl-specifier-seq unless it declares a constructor, destructor,
23074 or conversion. It might seem that we could check this condition in
23075 semantic analysis, rather than parsing, but that makes it difficult
23076 to handle something like `f()'. We want to notice that there are
23077 no decl-specifiers, and therefore realize that this is an
23078 expression, not a declaration.)
23080 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23081 the declarator is a direct-declarator of the form "(...)".
23083 MEMBER_P is true iff this declarator is a member-declarator.
23085 FRIEND_P is true iff this declarator is a friend.
23087 STATIC_P is true iff the keyword static was seen. */
23089 static cp_declarator
*
23090 cp_parser_declarator (cp_parser
* parser
,
23091 cp_parser_declarator_kind dcl_kind
,
23092 cp_parser_flags flags
,
23093 int* ctor_dtor_or_conv_p
,
23094 bool* parenthesized_p
,
23095 bool member_p
, bool friend_p
, bool static_p
)
23097 cp_declarator
*declarator
;
23098 enum tree_code code
;
23099 cp_cv_quals cv_quals
;
23101 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
23103 /* Assume this is not a constructor, destructor, or type-conversion
23105 if (ctor_dtor_or_conv_p
)
23106 *ctor_dtor_or_conv_p
= 0;
23108 if (cp_parser_allow_gnu_extensions_p (parser
))
23109 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
23111 /* Check for the ptr-operator production. */
23112 cp_parser_parse_tentatively (parser
);
23113 /* Parse the ptr-operator. */
23114 code
= cp_parser_ptr_operator (parser
,
23119 /* If that worked, then we have a ptr-operator. */
23120 if (cp_parser_parse_definitely (parser
))
23122 /* If a ptr-operator was found, then this declarator was not
23124 if (parenthesized_p
)
23125 *parenthesized_p
= false;
23126 /* The dependent declarator is optional if we are parsing an
23127 abstract-declarator. */
23128 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
23129 cp_parser_parse_tentatively (parser
);
23131 /* Parse the dependent declarator. */
23132 declarator
= cp_parser_declarator (parser
, dcl_kind
, flags
,
23133 /*ctor_dtor_or_conv_p=*/NULL
,
23134 /*parenthesized_p=*/NULL
,
23135 member_p
, friend_p
, static_p
);
23137 /* If we are parsing an abstract-declarator, we must handle the
23138 case where the dependent declarator is absent. */
23139 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
23140 && !cp_parser_parse_definitely (parser
))
23143 declarator
= cp_parser_make_indirect_declarator
23144 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
23146 /* Everything else is a direct-declarator. */
23149 if (parenthesized_p
)
23150 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
23152 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
23153 flags
, ctor_dtor_or_conv_p
,
23154 member_p
, friend_p
, static_p
);
23157 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
23158 declarator
->attributes
= gnu_attributes
;
23162 /* Parse a direct-declarator or direct-abstract-declarator.
23166 direct-declarator ( parameter-declaration-clause )
23167 cv-qualifier-seq [opt]
23168 ref-qualifier [opt]
23169 exception-specification [opt]
23170 direct-declarator [ constant-expression [opt] ]
23173 direct-abstract-declarator:
23174 direct-abstract-declarator [opt]
23175 ( parameter-declaration-clause )
23176 cv-qualifier-seq [opt]
23177 ref-qualifier [opt]
23178 exception-specification [opt]
23179 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23180 ( abstract-declarator )
23182 Returns a representation of the declarator. DCL_KIND is
23183 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23184 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23185 we are parsing a direct-declarator. It is
23186 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23187 of ambiguity we prefer an abstract declarator, as per
23189 The parser flags FLAGS is used to control type-specifier parsing.
23190 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23191 as for cp_parser_declarator. */
23193 static cp_declarator
*
23194 cp_parser_direct_declarator (cp_parser
* parser
,
23195 cp_parser_declarator_kind dcl_kind
,
23196 cp_parser_flags flags
,
23197 int* ctor_dtor_or_conv_p
,
23198 bool member_p
, bool friend_p
, bool static_p
)
23201 cp_declarator
*declarator
= NULL
;
23202 tree scope
= NULL_TREE
;
23203 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
23204 bool saved_in_declarator_p
= parser
->in_declarator_p
;
23206 tree pushed_scope
= NULL_TREE
;
23207 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
23211 /* Peek at the next token. */
23212 token
= cp_lexer_peek_token (parser
->lexer
);
23213 if (token
->type
== CPP_OPEN_PAREN
)
23215 /* This is either a parameter-declaration-clause, or a
23216 parenthesized declarator. When we know we are parsing a
23217 named declarator, it must be a parenthesized declarator
23218 if FIRST is true. For instance, `(int)' is a
23219 parameter-declaration-clause, with an omitted
23220 direct-abstract-declarator. But `((*))', is a
23221 parenthesized abstract declarator. Finally, when T is a
23222 template parameter `(T)' is a
23223 parameter-declaration-clause, and not a parenthesized
23226 We first try and parse a parameter-declaration-clause,
23227 and then try a nested declarator (if FIRST is true).
23229 It is not an error for it not to be a
23230 parameter-declaration-clause, even when FIRST is
23236 The first is the declaration of a function while the
23237 second is the definition of a variable, including its
23240 Having seen only the parenthesis, we cannot know which of
23241 these two alternatives should be selected. Even more
23242 complex are examples like:
23247 The former is a function-declaration; the latter is a
23248 variable initialization.
23250 Thus again, we try a parameter-declaration-clause, and if
23251 that fails, we back out and return. */
23253 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
23256 bool is_declarator
= false;
23260 /* In a member-declarator, the only valid interpretation
23261 of a parenthesis is the start of a
23262 parameter-declaration-clause. (It is invalid to
23263 initialize a static data member with a parenthesized
23264 initializer; only the "=" form of initialization is
23267 cp_parser_parse_tentatively (parser
);
23269 /* Consume the `('. */
23270 const location_t parens_start
= token
->location
;
23271 matching_parens parens
;
23272 parens
.consume_open (parser
);
23275 /* If this is going to be an abstract declarator, we're
23276 in a declarator and we can't have default args. */
23277 parser
->default_arg_ok_p
= false;
23278 parser
->in_declarator_p
= true;
23281 begin_scope (sk_function_parms
, NULL_TREE
);
23283 /* Signal we are in the immediate function context. */
23284 if (flags
& CP_PARSER_FLAGS_CONSTEVAL
)
23285 current_binding_level
->immediate_fn_ctx_p
= true;
23287 /* Parse the parameter-declaration-clause. */
23289 = cp_parser_parameter_declaration_clause (parser
, flags
);
23290 const location_t parens_end
23291 = cp_lexer_peek_token (parser
->lexer
)->location
;
23293 /* Consume the `)'. */
23294 parens
.require_close (parser
);
23296 /* If all went well, parse the cv-qualifier-seq,
23297 ref-qualifier and the exception-specification. */
23298 if (member_p
|| cp_parser_parse_definitely (parser
))
23300 cp_cv_quals cv_quals
;
23301 cp_virt_specifiers virt_specifiers
;
23302 cp_ref_qualifier ref_qual
;
23303 tree exception_specification
;
23306 bool memfn
= (member_p
|| (pushed_scope
23307 && CLASS_TYPE_P (pushed_scope
)));
23308 unsigned char local_variables_forbidden_p
23309 = parser
->local_variables_forbidden_p
;
23310 /* 'this' is not allowed in static member functions. */
23311 if (static_p
|| friend_p
)
23312 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
23314 is_declarator
= true;
23316 if (ctor_dtor_or_conv_p
)
23317 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
23320 /* Parse the cv-qualifier-seq. */
23321 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
23322 /* Parse the ref-qualifier. */
23323 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
23324 /* Parse the tx-qualifier. */
23325 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
23327 tree save_ccp
= current_class_ptr
;
23328 tree save_ccr
= current_class_ref
;
23329 if (memfn
&& !friend_p
&& !static_p
)
23330 /* DR 1207: 'this' is in scope after the cv-quals. */
23331 inject_this_parameter (current_class_type
, cv_quals
);
23333 /* If it turned out that this is e.g. a pointer to a
23334 function, we don't want to delay noexcept parsing. */
23335 if (declarator
== NULL
|| declarator
->kind
!= cdk_id
)
23336 flags
&= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
23338 /* Parse the exception-specification. */
23339 exception_specification
23340 = cp_parser_exception_specification_opt (parser
,
23343 attrs
= cp_parser_std_attribute_spec_seq (parser
);
23345 cp_omp_declare_simd_data odsd
;
23346 if ((flag_openmp
|| flag_openmp_simd
)
23348 && declarator
->std_attributes
23349 && declarator
->kind
== cdk_id
)
23351 tree
*pa
= &declarator
->std_attributes
;
23352 cp_parser_handle_directive_omp_attributes (parser
, pa
,
23356 /* In here, we handle cases where attribute is used after
23357 the function declaration. For example:
23358 void func (int x) __attribute__((vector(..))); */
23359 tree gnu_attrs
= NULL_TREE
;
23360 tree requires_clause
= NULL_TREE
;
23362 = cp_parser_late_return_type_opt (parser
, declarator
,
23365 cp_finalize_omp_declare_simd (parser
, &odsd
);
23367 /* Parse the virt-specifier-seq. */
23368 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23370 location_t parens_loc
= make_location (parens_start
,
23373 /* Create the function-declarator. */
23374 declarator
= make_call_declarator (declarator
,
23380 exception_specification
,
23384 declarator
->std_attributes
= attrs
;
23385 declarator
->attributes
= gnu_attrs
;
23386 /* Any subsequent parameter lists are to do with
23387 return type, so are not those of the declared
23389 parser
->default_arg_ok_p
= false;
23391 current_class_ptr
= save_ccp
;
23392 current_class_ref
= save_ccr
;
23394 /* Restore the state of local_variables_forbidden_p. */
23395 parser
->local_variables_forbidden_p
23396 = local_variables_forbidden_p
;
23399 /* Remove the function parms from scope. */
23400 pop_bindings_and_leave_scope ();
23403 /* Repeat the main loop. */
23407 /* If this is the first, we can try a parenthesized
23411 bool saved_in_type_id_in_expr_p
;
23413 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
23414 parser
->in_declarator_p
= saved_in_declarator_p
;
23416 open_paren
= token
;
23417 /* Consume the `('. */
23418 matching_parens parens
;
23419 parens
.consume_open (parser
);
23420 /* Parse the nested declarator. */
23421 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
23422 parser
->in_type_id_in_expr_p
= true;
23424 = cp_parser_declarator (parser
, dcl_kind
, flags
,
23425 ctor_dtor_or_conv_p
,
23426 /*parenthesized_p=*/NULL
,
23427 member_p
, friend_p
,
23428 /*static_p=*/false);
23429 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
23431 /* Expect a `)'. */
23432 close_paren
= cp_lexer_peek_token (parser
->lexer
);
23433 if (!parens
.require_close (parser
))
23434 declarator
= cp_error_declarator
;
23435 if (declarator
== cp_error_declarator
)
23438 goto handle_declarator
;
23440 /* Otherwise, we must be done. */
23444 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
23445 && token
->type
== CPP_OPEN_SQUARE
23446 && !cp_next_tokens_can_be_attribute_p (parser
))
23448 /* Parse an array-declarator. */
23449 tree bounds
, attrs
;
23451 if (ctor_dtor_or_conv_p
)
23452 *ctor_dtor_or_conv_p
= 0;
23456 parser
->default_arg_ok_p
= false;
23457 parser
->in_declarator_p
= true;
23458 /* Consume the `['. */
23459 cp_lexer_consume_token (parser
->lexer
);
23460 /* Peek at the next token. */
23461 token
= cp_lexer_peek_token (parser
->lexer
);
23462 /* If the next token is `]', then there is no
23463 constant-expression. */
23464 if (token
->type
!= CPP_CLOSE_SQUARE
)
23466 bool non_constant_p
;
23468 = cp_parser_constant_expression (parser
,
23469 /*allow_non_constant=*/true,
23471 if (!non_constant_p
)
23473 else if (error_operand_p (bounds
))
23474 /* Already gave an error. */;
23475 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
23476 /* Let compute_array_index_type diagnose this. */;
23477 else if (!parser
->in_function_body
23478 || parsing_function_declarator ())
23480 /* Normally, the array bound must be an integral constant
23481 expression. However, as an extension, we allow VLAs
23482 in function scopes as long as they aren't part of a
23483 parameter declaration. */
23484 cp_parser_error (parser
,
23485 "array bound is not an integer constant");
23486 bounds
= error_mark_node
;
23488 else if (processing_template_decl
23489 && !type_dependent_expression_p (bounds
))
23491 /* Remember this wasn't a constant-expression. */
23492 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
23493 TREE_SIDE_EFFECTS (bounds
) = 1;
23497 bounds
= NULL_TREE
;
23498 /* Look for the closing `]'. */
23499 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
23501 declarator
= cp_error_declarator
;
23505 attrs
= cp_parser_std_attribute_spec_seq (parser
);
23506 declarator
= make_array_declarator (declarator
, bounds
);
23507 declarator
->std_attributes
= attrs
;
23509 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
23512 tree qualifying_scope
;
23513 tree unqualified_name
;
23515 special_function_kind sfk
;
23517 bool pack_expansion_p
= false;
23518 cp_token
*declarator_id_start_token
;
23520 /* Parse a declarator-id */
23521 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
23524 cp_parser_parse_tentatively (parser
);
23526 /* If we see an ellipsis, we should be looking at a
23528 if (token
->type
== CPP_ELLIPSIS
)
23530 /* Consume the `...' */
23531 cp_lexer_consume_token (parser
->lexer
);
23533 pack_expansion_p
= true;
23537 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
23539 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
23540 qualifying_scope
= parser
->scope
;
23545 if (!unqualified_name
&& pack_expansion_p
)
23547 /* Check whether an error occurred. */
23548 okay
= !cp_parser_error_occurred (parser
);
23550 /* We already consumed the ellipsis to mark a
23551 parameter pack, but we have no way to report it,
23552 so abort the tentative parse. We will be exiting
23553 immediately anyway. */
23554 cp_parser_abort_tentative_parse (parser
);
23557 okay
= cp_parser_parse_definitely (parser
);
23560 unqualified_name
= error_mark_node
;
23561 else if (unqualified_name
23562 && (qualifying_scope
23563 || (!identifier_p (unqualified_name
))))
23565 cp_parser_error (parser
, "expected unqualified-id");
23566 unqualified_name
= error_mark_node
;
23570 if (!unqualified_name
)
23572 if (unqualified_name
== error_mark_node
)
23574 declarator
= cp_error_declarator
;
23575 pack_expansion_p
= false;
23576 declarator
->parameter_pack_p
= false;
23580 attrs
= cp_parser_std_attribute_spec_seq (parser
);
23582 if (qualifying_scope
&& at_namespace_scope_p ()
23583 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
23585 /* In the declaration of a member of a template class
23586 outside of the class itself, the SCOPE will sometimes
23587 be a TYPENAME_TYPE. For example, given:
23589 template <typename T>
23590 int S<T>::R::i = 3;
23592 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23593 this context, we must resolve S<T>::R to an ordinary
23594 type, rather than a typename type.
23596 The reason we normally avoid resolving TYPENAME_TYPEs
23597 is that a specialization of `S' might render
23598 `S<T>::R' not a type. However, if `S' is
23599 specialized, then this `i' will not be used, so there
23600 is no harm in resolving the types here. */
23603 /* Resolve the TYPENAME_TYPE. */
23604 type
= resolve_typename_type (qualifying_scope
,
23605 /*only_current_p=*/false);
23606 /* If that failed, the declarator is invalid. */
23607 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23609 if (typedef_variant_p (type
))
23610 error_at (declarator_id_start_token
->location
,
23611 "cannot define member of dependent typedef "
23614 error_at (declarator_id_start_token
->location
,
23615 "%<%T::%E%> is not a type",
23616 TYPE_CONTEXT (qualifying_scope
),
23617 TYPE_IDENTIFIER (qualifying_scope
));
23619 qualifying_scope
= type
;
23624 if (unqualified_name
)
23628 if (qualifying_scope
23629 && CLASS_TYPE_P (qualifying_scope
))
23630 class_type
= qualifying_scope
;
23632 class_type
= current_class_type
;
23634 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
23636 tree name_type
= TREE_TYPE (unqualified_name
);
23638 if (!class_type
|| !same_type_p (name_type
, class_type
))
23640 /* We do not attempt to print the declarator
23641 here because we do not have enough
23642 information about its original syntactic
23644 cp_parser_error (parser
, "invalid declarator");
23645 declarator
= cp_error_declarator
;
23648 else if (qualifying_scope
23649 && CLASSTYPE_USE_TEMPLATE (name_type
))
23651 error_at (declarator_id_start_token
->location
,
23652 "invalid use of constructor as a template");
23653 inform (declarator_id_start_token
->location
,
23654 "use %<%T::%D%> instead of %<%T::%D%> to "
23655 "name the constructor in a qualified name",
23657 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
23658 class_type
, name_type
);
23659 declarator
= cp_error_declarator
;
23662 unqualified_name
= constructor_name (class_type
);
23667 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
23668 sfk
= sfk_destructor
;
23669 else if (identifier_p (unqualified_name
)
23670 && IDENTIFIER_CONV_OP_P (unqualified_name
))
23671 sfk
= sfk_conversion
;
23672 else if (/* There's no way to declare a constructor
23673 for an unnamed type, even if the type
23674 got a name for linkage purposes. */
23675 !TYPE_WAS_UNNAMED (class_type
)
23676 /* Handle correctly (c++/19200):
23690 friend void N::S();
23692 && (!friend_p
|| class_type
== qualifying_scope
)
23693 && constructor_name_p (unqualified_name
,
23695 sfk
= sfk_constructor
;
23696 else if (is_overloaded_fn (unqualified_name
)
23697 && DECL_CONSTRUCTOR_P (get_first_fn
23698 (unqualified_name
)))
23699 sfk
= sfk_constructor
;
23701 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
23702 *ctor_dtor_or_conv_p
= -1;
23705 declarator
= make_id_declarator (qualifying_scope
,
23707 sfk
, token
->location
);
23708 declarator
->std_attributes
= attrs
;
23709 declarator
->parameter_pack_p
= pack_expansion_p
;
23711 if (pack_expansion_p
)
23712 maybe_warn_variadic_templates ();
23714 /* We're looking for this case in [temp.res]:
23715 A qualified-id is assumed to name a type if [...]
23716 - it is a decl-specifier of the decl-specifier-seq of a
23717 parameter-declaration in a declarator of a function or
23718 function template declaration, ... */
23719 if (cxx_dialect
>= cxx20
23720 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
23721 && declarator
->kind
== cdk_id
23722 && !at_class_scope_p ()
23723 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23725 /* ...whose declarator-id is qualified. If it isn't, never
23726 assume the parameters to refer to types. */
23727 if (qualifying_scope
== NULL_TREE
)
23728 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
23731 /* Now we have something like
23732 template <typename T> int C::x(S::p);
23733 which can be a function template declaration or a
23734 variable template definition. If name lookup for
23735 the declarator-id C::x finds one or more function
23736 templates, assume S::p to name a type. Otherwise,
23739 = cp_parser_lookup_name (parser
, unqualified_name
,
23741 /*is_template=*/false,
23742 /*is_namespace=*/false,
23743 /*check_dependency=*/false,
23744 /*ambiguous_decls=*/NULL
,
23747 if (!is_overloaded_fn (decl
)
23749 template<typename T>
23750 A<T>::A(T::type) { } */
23751 && !(MAYBE_CLASS_TYPE_P (qualifying_scope
)
23752 && constructor_name_p (unqualified_name
,
23753 qualifying_scope
)))
23754 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
23759 handle_declarator
:;
23760 scope
= get_scope_of_declarator (declarator
);
23763 /* Any names that appear after the declarator-id for a
23764 member are looked up in the containing scope. */
23765 if (at_function_scope_p ())
23767 /* But declarations with qualified-ids can't appear in a
23769 cp_parser_error (parser
, "qualified-id in declaration");
23770 declarator
= cp_error_declarator
;
23773 pushed_scope
= push_scope (scope
);
23775 parser
->in_declarator_p
= true;
23776 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
23777 || (declarator
&& declarator
->kind
== cdk_id
))
23778 /* Default args are only allowed on function
23780 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
23782 parser
->default_arg_ok_p
= false;
23791 /* For an abstract declarator, we might wind up with nothing at this
23792 point. That's an error; the declarator is not optional. */
23794 cp_parser_error (parser
, "expected declarator");
23795 else if (open_paren
)
23797 /* Record overly parenthesized declarator so we can give a
23798 diagnostic about confusing decl/expr disambiguation. */
23799 if (declarator
->kind
== cdk_array
)
23801 /* If the open and close parens are on different lines, this
23802 is probably a formatting thing, so ignore. */
23803 expanded_location open
= expand_location (open_paren
->location
);
23804 expanded_location close
= expand_location (close_paren
->location
);
23805 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
23809 declarator
->parenthesized
= make_location (open_paren
->location
,
23810 open_paren
->location
,
23811 close_paren
->location
);
23814 /* If we entered a scope, we must exit it now. */
23816 pop_scope (pushed_scope
);
23818 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
23819 parser
->in_declarator_p
= saved_in_declarator_p
;
23824 /* Parse a ptr-operator.
23827 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23828 * cv-qualifier-seq [opt]
23830 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23831 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23836 & cv-qualifier-seq [opt]
23838 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23839 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23840 an rvalue reference. In the case of a pointer-to-member, *TYPE is
23841 filled in with the TYPE containing the member. *CV_QUALS is
23842 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23843 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
23844 Note that the tree codes returned by this function have nothing
23845 to do with the types of trees that will be eventually be created
23846 to represent the pointer or reference type being parsed. They are
23847 just constants with suggestive names. */
23848 static enum tree_code
23849 cp_parser_ptr_operator (cp_parser
* parser
,
23851 cp_cv_quals
*cv_quals
,
23854 enum tree_code code
= ERROR_MARK
;
23856 tree attrs
= NULL_TREE
;
23858 /* Assume that it's not a pointer-to-member. */
23860 /* And that there are no cv-qualifiers. */
23861 *cv_quals
= TYPE_UNQUALIFIED
;
23863 /* Peek at the next token. */
23864 token
= cp_lexer_peek_token (parser
->lexer
);
23866 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
23867 if (token
->type
== CPP_MULT
)
23868 code
= INDIRECT_REF
;
23869 else if (token
->type
== CPP_AND
)
23871 else if ((cxx_dialect
!= cxx98
) &&
23872 token
->type
== CPP_AND_AND
) /* C++0x only */
23873 code
= NON_LVALUE_EXPR
;
23875 if (code
!= ERROR_MARK
)
23877 /* Consume the `*', `&' or `&&'. */
23878 cp_lexer_consume_token (parser
->lexer
);
23880 /* A `*' can be followed by a cv-qualifier-seq, and so can a
23881 `&', if we are allowing GNU extensions. (The only qualifier
23882 that can legally appear after `&' is `restrict', but that is
23883 enforced during semantic analysis. */
23884 if (code
== INDIRECT_REF
23885 || cp_parser_allow_gnu_extensions_p (parser
))
23886 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
23888 attrs
= cp_parser_std_attribute_spec_seq (parser
);
23889 if (attributes
!= NULL
)
23890 *attributes
= attrs
;
23894 /* Try the pointer-to-member case. */
23895 cp_parser_parse_tentatively (parser
);
23896 /* Look for the optional `::' operator. */
23897 cp_parser_global_scope_opt (parser
,
23898 /*current_scope_valid_p=*/false);
23899 /* Look for the nested-name specifier. */
23900 token
= cp_lexer_peek_token (parser
->lexer
);
23901 cp_parser_nested_name_specifier (parser
,
23902 /*typename_keyword_p=*/false,
23903 /*check_dependency_p=*/true,
23905 /*is_declaration=*/false);
23906 /* If we found it, and the next token is a `*', then we are
23907 indeed looking at a pointer-to-member operator. */
23908 if (!cp_parser_error_occurred (parser
)
23909 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
23911 /* Indicate that the `*' operator was used. */
23912 code
= INDIRECT_REF
;
23914 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
23915 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
23916 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
23917 error_at (token
->location
, "cannot form pointer to member of "
23918 "non-class %q#T", parser
->scope
);
23921 /* The type of which the member is a member is given by the
23923 *type
= parser
->scope
;
23924 /* The next name will not be qualified. */
23925 parser
->scope
= NULL_TREE
;
23926 parser
->qualifying_scope
= NULL_TREE
;
23927 parser
->object_scope
= NULL_TREE
;
23928 /* Look for optional c++11 attributes. */
23929 attrs
= cp_parser_std_attribute_spec_seq (parser
);
23930 if (attributes
!= NULL
)
23931 *attributes
= attrs
;
23932 /* Look for the optional cv-qualifier-seq. */
23933 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
23936 /* If that didn't work we don't have a ptr-operator. */
23937 if (!cp_parser_parse_definitely (parser
))
23938 cp_parser_error (parser
, "expected ptr-operator");
23944 /* Parse an (optional) cv-qualifier-seq.
23947 cv-qualifier cv-qualifier-seq [opt]
23958 Returns a bitmask representing the cv-qualifiers. */
23961 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
23963 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
23968 cp_cv_quals cv_qualifier
;
23970 /* Peek at the next token. */
23971 token
= cp_lexer_peek_token (parser
->lexer
);
23972 /* See if it's a cv-qualifier. */
23973 switch (token
->keyword
)
23976 cv_qualifier
= TYPE_QUAL_CONST
;
23980 cv_qualifier
= TYPE_QUAL_VOLATILE
;
23984 cv_qualifier
= TYPE_QUAL_RESTRICT
;
23988 cv_qualifier
= TYPE_UNQUALIFIED
;
23995 if (cv_quals
& cv_qualifier
)
23997 gcc_rich_location
richloc (token
->location
);
23998 richloc
.add_fixit_remove ();
23999 error_at (&richloc
, "duplicate cv-qualifier");
24000 cp_lexer_purge_token (parser
->lexer
);
24004 cp_lexer_consume_token (parser
->lexer
);
24005 cv_quals
|= cv_qualifier
;
24012 /* Parse an (optional) ref-qualifier
24018 Returns cp_ref_qualifier representing ref-qualifier. */
24020 static cp_ref_qualifier
24021 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
24023 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
24025 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
24026 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
24031 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
24032 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24034 switch (token
->type
)
24037 curr_ref_qual
= REF_QUAL_LVALUE
;
24041 curr_ref_qual
= REF_QUAL_RVALUE
;
24045 curr_ref_qual
= REF_QUAL_NONE
;
24049 if (!curr_ref_qual
)
24053 error_at (token
->location
, "multiple ref-qualifiers");
24054 cp_lexer_purge_token (parser
->lexer
);
24058 ref_qual
= curr_ref_qual
;
24059 cp_lexer_consume_token (parser
->lexer
);
24066 /* Parse an optional tx-qualifier.
24070 transaction_safe_dynamic */
24073 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
24075 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24076 if (token
->type
== CPP_NAME
)
24078 tree name
= token
->u
.value
;
24079 const char *p
= IDENTIFIER_POINTER (name
);
24080 const int len
= strlen ("transaction_safe");
24081 if (startswith (p
, "transaction_safe"))
24085 || !strcmp (p
, "_dynamic"))
24087 cp_lexer_consume_token (parser
->lexer
);
24090 error ("%qE requires %<-fgnu-tm%>", name
);
24101 /* Parse an (optional) virt-specifier-seq.
24103 virt-specifier-seq:
24104 virt-specifier virt-specifier-seq [opt]
24110 Returns a bitmask representing the virt-specifiers. */
24112 static cp_virt_specifiers
24113 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
24115 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
24120 cp_virt_specifiers virt_specifier
;
24122 /* Peek at the next token. */
24123 token
= cp_lexer_peek_token (parser
->lexer
);
24124 /* See if it's a virt-specifier-qualifier. */
24125 if (token
->type
!= CPP_NAME
)
24127 if (id_equal (token
->u
.value
, "override"))
24129 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
24130 virt_specifier
= VIRT_SPEC_OVERRIDE
;
24132 else if (id_equal (token
->u
.value
, "final"))
24134 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
24135 virt_specifier
= VIRT_SPEC_FINAL
;
24137 else if (id_equal (token
->u
.value
, "__final"))
24139 virt_specifier
= VIRT_SPEC_FINAL
;
24144 if (virt_specifiers
& virt_specifier
)
24146 gcc_rich_location
richloc (token
->location
);
24147 richloc
.add_fixit_remove ();
24148 error_at (&richloc
, "duplicate virt-specifier");
24149 cp_lexer_purge_token (parser
->lexer
);
24153 cp_lexer_consume_token (parser
->lexer
);
24154 virt_specifiers
|= virt_specifier
;
24157 return virt_specifiers
;
24160 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24161 is in scope even though it isn't real. */
24164 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
24168 if (current_class_ptr
)
24170 /* We don't clear this between NSDMIs. Is it already what we want? */
24171 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
24172 if (DECL_P (current_class_ptr
)
24173 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
24174 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
24175 && cp_type_quals (type
) == quals
)
24179 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
24180 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24181 current_class_ptr
= NULL_TREE
;
24183 = cp_build_fold_indirect_ref (this_parm
);
24184 current_class_ptr
= this_parm
;
24187 /* Return true iff our current scope is a non-static data member
24191 parsing_nsdmi (void)
24193 /* We recognize NSDMI context by the context-less 'this' pointer set up
24194 by the function above. */
24195 if (current_class_ptr
24196 && TREE_CODE (current_class_ptr
) == PARM_DECL
24197 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
24202 /* True if we're parsing a function declarator. */
24205 parsing_function_declarator ()
24207 /* this_entity is NULL for a function parameter scope while parsing the
24208 declarator; it is set when parsing the body of the function. */
24209 return (current_binding_level
->kind
== sk_function_parms
24210 && !current_binding_level
->this_entity
);
24213 /* Parse a late-specified return type, if any. This is not a separate
24214 non-terminal, but part of a function declarator, which looks like
24216 -> trailing-type-specifier-seq abstract-declarator(opt)
24218 Returns the type indicated by the type-id.
24220 In addition to this, parse any queued up #pragma omp declare simd
24221 clauses, and #pragma acc routine clauses.
24223 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24227 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
24228 tree
& requires_clause
)
24231 tree type
= NULL_TREE
;
24232 bool declare_simd_p
= (parser
->omp_declare_simd
24234 && declarator
->kind
== cdk_id
);
24236 bool oacc_routine_p
= (parser
->oacc_routine
24238 && declarator
->kind
== cdk_id
);
24240 /* Peek at the next token. */
24241 token
= cp_lexer_peek_token (parser
->lexer
);
24242 /* A late-specified return type is indicated by an initial '->'. */
24243 if (token
->type
!= CPP_DEREF
24244 && token
->keyword
!= RID_REQUIRES
24245 && !(token
->type
== CPP_NAME
24246 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
24247 && !(declare_simd_p
|| oacc_routine_p
))
24250 if (token
->type
== CPP_DEREF
)
24252 /* Consume the ->. */
24253 cp_lexer_consume_token (parser
->lexer
);
24255 type
= cp_parser_trailing_type_id (parser
);
24258 /* Function declarations may be followed by a trailing
24259 requires-clause. */
24260 requires_clause
= cp_parser_requires_clause_opt (parser
, false);
24262 if (declare_simd_p
)
24263 declarator
->attributes
24264 = cp_parser_late_parsing_omp_declare_simd (parser
,
24265 declarator
->attributes
);
24266 if (oacc_routine_p
)
24267 declarator
->attributes
24268 = cp_parser_late_parsing_oacc_routine (parser
,
24269 declarator
->attributes
);
24274 /* Parse a declarator-id.
24278 :: [opt] nested-name-specifier [opt] type-name
24280 In the `id-expression' case, the value returned is as for
24281 cp_parser_id_expression if the id-expression was an unqualified-id.
24282 If the id-expression was a qualified-id, then a SCOPE_REF is
24283 returned. The first operand is the scope (either a NAMESPACE_DECL
24284 or TREE_TYPE), but the second is still just a representation of an
24288 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
24291 /* The expression must be an id-expression. Assume that qualified
24292 names are the names of types so that:
24295 int S<T>::R::i = 3;
24297 will work; we must treat `S<T>::R' as the name of a type.
24298 Similarly, assume that qualified names are templates, where
24302 int S<T>::R<T>::i = 3;
24305 id
= cp_parser_id_expression (parser
,
24306 /*template_keyword_p=*/false,
24307 /*check_dependency_p=*/false,
24308 /*template_p=*/NULL
,
24309 /*declarator_p=*/true,
24311 if (id
&& BASELINK_P (id
))
24312 id
= BASELINK_FUNCTIONS (id
);
24316 /* Parse a type-id.
24319 type-specifier-seq abstract-declarator [opt]
24321 The parser flags FLAGS is used to control type-specifier parsing.
24323 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24325 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24326 i.e. we've just seen "->".
24328 Returns the TYPE specified. */
24331 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
24332 bool is_template_arg
, bool is_trailing_return
,
24333 location_t
*type_location
)
24335 cp_decl_specifier_seq type_specifier_seq
;
24336 cp_declarator
*abstract_declarator
;
24338 /* Parse the type-specifier-seq. */
24339 cp_parser_type_specifier_seq (parser
, flags
,
24340 /*is_declaration=*/false,
24341 is_trailing_return
,
24342 &type_specifier_seq
);
24344 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
24346 if (is_template_arg
&& type_specifier_seq
.type
24347 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
24348 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
24349 /* A bare template name as a template argument is a template template
24350 argument, not a placeholder, so fail parsing it as a type argument. */
24352 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
24353 cp_parser_simulate_error (parser
);
24354 return error_mark_node
;
24356 if (type_specifier_seq
.type
== error_mark_node
)
24357 return error_mark_node
;
24359 /* There might or might not be an abstract declarator. */
24360 cp_parser_parse_tentatively (parser
);
24361 /* Look for the declarator. */
24362 abstract_declarator
24363 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
24364 CP_PARSER_FLAGS_NONE
, NULL
,
24365 /*parenthesized_p=*/NULL
,
24366 /*member_p=*/false,
24367 /*friend_p=*/false,
24368 /*static_p=*/false);
24369 /* Check to see if there really was a declarator. */
24370 if (!cp_parser_parse_definitely (parser
))
24371 abstract_declarator
= NULL
;
24373 bool auto_typeid_ok
= false;
24374 /* The concepts TS allows 'auto' as a type-id. */
24375 if (flag_concepts_ts
)
24376 auto_typeid_ok
= !parser
->in_type_id_in_expr_p
;
24377 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24378 outside the template-argument-list context here only for the sake of
24379 diagnostic: grokdeclarator then can emit a better error message for
24380 e.g. using T = auto. */
24381 else if (flag_concepts
)
24382 auto_typeid_ok
= (!parser
->in_type_id_in_expr_p
24383 && !parser
->in_template_argument_list_p
);
24385 if (type_specifier_seq
.type
24387 /* None of the valid uses of 'auto' in C++14 involve the type-id
24388 nonterminal, but it is valid in a trailing-return-type. */
24389 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
24390 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
24392 /* A type-id with type 'auto' is only ok if the abstract declarator
24393 is a function declarator with a late-specified return type.
24395 A type-id with 'auto' is also valid in a trailing-return-type
24396 in a compound-requirement. */
24397 if (abstract_declarator
24398 && abstract_declarator
->kind
== cdk_function
24399 && abstract_declarator
->u
.function
.late_return_type
)
24401 else if (parser
->in_result_type_constraint_p
)
24405 if (!cp_parser_simulate_error (parser
))
24407 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
24408 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
24410 auto_diagnostic_group g
;
24411 gcc_rich_location
richloc (loc
);
24412 richloc
.add_fixit_insert_after ("<>");
24413 error_at (&richloc
, "missing template arguments after %qE",
24415 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
24418 else if (parser
->in_template_argument_list_p
)
24419 error_at (loc
, "%qT not permitted in template argument",
24422 error_at (loc
, "invalid use of %qT", auto_node
);
24424 return error_mark_node
;
24428 return groktypename (&type_specifier_seq
, abstract_declarator
,
24432 /* Wrapper for cp_parser_type_id_1. */
24435 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
24436 location_t
*type_location
)
24438 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
24441 /* Wrapper for cp_parser_type_id_1. */
24444 cp_parser_template_type_arg (cp_parser
*parser
)
24447 const char *saved_message
= parser
->type_definition_forbidden_message
;
24448 parser
->type_definition_forbidden_message
24449 = G_("types may not be defined in template arguments");
24450 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
24451 parser
->type_definition_forbidden_message
= saved_message
;
24452 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
24454 error ("invalid use of %<auto%> in template argument");
24455 r
= error_mark_node
;
24460 /* Wrapper for cp_parser_type_id_1. */
24463 cp_parser_trailing_type_id (cp_parser
*parser
)
24465 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
24466 false, true, NULL
);
24469 /* Parse a type-specifier-seq.
24471 type-specifier-seq:
24472 type-specifier type-specifier-seq [opt]
24476 type-specifier-seq:
24477 attributes type-specifier-seq [opt]
24479 The parser flags FLAGS is used to control type-specifier parsing.
24481 If IS_DECLARATION is true, we are at the start of a "condition" or
24482 exception-declaration, so we might be followed by a declarator-id.
24484 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24485 i.e. we've just seen "->".
24487 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24490 cp_parser_type_specifier_seq (cp_parser
* parser
,
24491 cp_parser_flags flags
,
24492 bool is_declaration
,
24493 bool is_trailing_return
,
24494 cp_decl_specifier_seq
*type_specifier_seq
)
24496 bool seen_type_specifier
= false;
24497 cp_token
*start_token
= NULL
;
24499 /* Clear the TYPE_SPECIFIER_SEQ. */
24500 clear_decl_specs (type_specifier_seq
);
24502 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
24503 /* In the context of a trailing return type, enum E { } is an
24504 elaborated-type-specifier followed by a function-body, not an
24506 if (is_trailing_return
)
24507 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
24509 /* Parse the type-specifiers and attributes. */
24512 tree type_specifier
;
24513 bool is_cv_qualifier
;
24515 /* Check for attributes first. */
24516 if (cp_next_tokens_can_be_attribute_p (parser
))
24518 /* GNU attributes at the end of a declaration apply to the
24519 declaration as a whole, not to the trailing return type. So look
24520 ahead to see if these attributes are at the end. */
24521 if (seen_type_specifier
&& is_trailing_return
24522 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
24524 size_t n
= cp_parser_skip_attributes_opt (parser
, 1);
24525 cp_token
*tok
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24526 if (tok
->type
== CPP_SEMICOLON
|| tok
->type
== CPP_COMMA
24527 || tok
->type
== CPP_EQ
|| tok
->type
== CPP_OPEN_BRACE
)
24530 type_specifier_seq
->attributes
24531 = attr_chainon (type_specifier_seq
->attributes
,
24532 cp_parser_attributes_opt (parser
));
24536 /* record the token of the beginning of the type specifier seq,
24537 for error reporting purposes*/
24539 start_token
= cp_lexer_peek_token (parser
->lexer
);
24541 /* Look for the type-specifier. */
24542 type_specifier
= cp_parser_type_specifier (parser
,
24544 type_specifier_seq
,
24545 /*is_declaration=*/false,
24548 if (!type_specifier
)
24550 /* If the first type-specifier could not be found, this is not a
24551 type-specifier-seq at all. */
24552 if (!seen_type_specifier
)
24554 /* Set in_declarator_p to avoid skipping to the semicolon. */
24555 int in_decl
= parser
->in_declarator_p
;
24556 parser
->in_declarator_p
= true;
24558 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
24559 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24560 cp_parser_error (parser
, "expected type-specifier");
24562 parser
->in_declarator_p
= in_decl
;
24564 type_specifier_seq
->type
= error_mark_node
;
24567 /* If subsequent type-specifiers could not be found, the
24568 type-specifier-seq is complete. */
24572 seen_type_specifier
= true;
24573 /* The standard says that a condition can be:
24575 type-specifier-seq declarator = assignment-expression
24582 we should treat the "S" as a declarator, not as a
24583 type-specifier. The standard doesn't say that explicitly for
24584 type-specifier-seq, but it does say that for
24585 decl-specifier-seq in an ordinary declaration. Perhaps it
24586 would be clearer just to allow a decl-specifier-seq here, and
24587 then add a semantic restriction that if any decl-specifiers
24588 that are not type-specifiers appear, the program is invalid. */
24589 if (is_declaration
&& !is_cv_qualifier
)
24590 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
24594 /* Return whether the function currently being declared has an associated
24595 template parameter list. */
24598 function_being_declared_is_template_p (cp_parser
* parser
)
24600 if (!current_template_parms
|| processing_template_parmlist
)
24603 if (parser
->implicit_template_scope
)
24606 if (at_class_scope_p ()
24607 && TYPE_BEING_DEFINED (current_class_type
))
24608 return parser
->num_template_parameter_lists
!= 0;
24610 return ((int) parser
->num_template_parameter_lists
> template_class_depth
24611 (current_class_type
));
24614 /* Parse a parameter-declaration-clause.
24616 parameter-declaration-clause:
24617 parameter-declaration-list [opt] ... [opt]
24618 parameter-declaration-list , ...
24620 The parser flags FLAGS is used to control type-specifier parsing.
24622 Returns a representation for the parameter declarations. A return
24623 value of NULL indicates a parameter-declaration-clause consisting
24624 only of an ellipsis. */
24627 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
24628 cp_parser_flags flags
)
24634 auto cleanup
= make_temp_override
24635 (parser
->auto_is_implicit_function_template_parm_p
);
24637 if (!processing_specialization
24638 && !processing_template_parmlist
24639 && !processing_explicit_instantiation
24640 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24641 actual function or a random abstract declarator. */
24642 && parser
->default_arg_ok_p
)
24643 if (!current_function_decl
24644 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
24645 parser
->auto_is_implicit_function_template_parm_p
= true;
24647 /* Peek at the next token. */
24648 token
= cp_lexer_peek_token (parser
->lexer
);
24649 /* Check for trivial parameter-declaration-clauses. */
24650 if (token
->type
== CPP_ELLIPSIS
)
24652 /* Consume the `...' token. */
24653 cp_lexer_consume_token (parser
->lexer
);
24656 else if (token
->type
== CPP_CLOSE_PAREN
)
24657 /* There are no parameters. */
24658 return void_list_node
;
24659 /* Check for `(void)', too, which is a special case. */
24660 else if (token
->keyword
== RID_VOID
24661 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
24662 == CPP_CLOSE_PAREN
))
24664 /* Consume the `void' token. */
24665 cp_lexer_consume_token (parser
->lexer
);
24666 /* There are no parameters. */
24667 return explicit_void_list_node
;
24670 /* A vector of parameters that haven't been pushed yet. */
24671 auto_vec
<tree
> pending_decls
;
24673 /* Parse the parameter-declaration-list. */
24674 parameters
= cp_parser_parameter_declaration_list (parser
, flags
,
24676 /* If a parse error occurred while parsing the
24677 parameter-declaration-list, then the entire
24678 parameter-declaration-clause is erroneous. */
24679 if (parameters
== error_mark_node
)
24682 /* Peek at the next token. */
24683 token
= cp_lexer_peek_token (parser
->lexer
);
24684 /* If it's a `,', the clause should terminate with an ellipsis. */
24685 if (token
->type
== CPP_COMMA
)
24687 /* Consume the `,'. */
24688 cp_lexer_consume_token (parser
->lexer
);
24689 /* Expect an ellipsis. */
24691 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
24693 /* It might also be `...' if the optional trailing `,' was
24695 else if (token
->type
== CPP_ELLIPSIS
)
24697 /* Consume the `...' token. */
24698 cp_lexer_consume_token (parser
->lexer
);
24699 /* And remember that we saw it. */
24703 ellipsis_p
= false;
24705 /* A valid parameter-declaration-clause can only be followed by a ')'.
24706 So it's time to push all the parameters we have seen now that we
24707 know we have a valid declaration. Note that here we may not have
24708 committed yet, nor should we. Pushing here will detect the error
24709 of redefining a parameter. */
24710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24711 for (tree p
: pending_decls
)
24714 /* Finish the parameter list. */
24716 parameters
= chainon (parameters
, void_list_node
);
24721 /* Parse a parameter-declaration-list.
24723 parameter-declaration-list:
24724 parameter-declaration
24725 parameter-declaration-list , parameter-declaration
24727 The parser flags FLAGS is used to control type-specifier parsing.
24728 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
24730 Returns a representation of the parameter-declaration-list, as for
24731 cp_parser_parameter_declaration_clause. However, the
24732 `void_list_node' is never appended to the list. */
24735 cp_parser_parameter_declaration_list (cp_parser
* parser
,
24736 cp_parser_flags flags
,
24737 auto_vec
<tree
> *pending_decls
)
24739 tree parameters
= NULL_TREE
;
24740 tree
*tail
= ¶meters
;
24741 bool saved_in_unbraced_linkage_specification_p
;
24744 /* The special considerations that apply to a function within an
24745 unbraced linkage specifications do not apply to the parameters
24746 to the function. */
24747 saved_in_unbraced_linkage_specification_p
24748 = parser
->in_unbraced_linkage_specification_p
;
24749 parser
->in_unbraced_linkage_specification_p
= false;
24751 /* Look for more parameters. */
24754 cp_parameter_declarator
*parameter
;
24755 tree decl
= error_mark_node
;
24756 bool parenthesized_p
= false;
24758 /* Parse the parameter. */
24760 = cp_parser_parameter_declaration (parser
, flags
,
24761 /*template_parm_p=*/false,
24764 /* We don't know yet if the enclosing context is unavailable or deprecated,
24765 so wait and deal with it in grokparms if appropriate. */
24766 deprecated_state
= UNAVAILABLE_DEPRECATED_SUPPRESS
;
24768 if (parameter
&& !cp_parser_error_occurred (parser
))
24770 decl
= grokdeclarator (parameter
->declarator
,
24771 ¶meter
->decl_specifiers
,
24773 parameter
->default_argument
!= NULL_TREE
,
24774 ¶meter
->decl_specifiers
.attributes
);
24775 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
24776 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
24779 deprecated_state
= DEPRECATED_NORMAL
;
24781 /* If a parse error occurred parsing the parameter declaration,
24782 then the entire parameter-declaration-list is erroneous. */
24783 if (decl
== error_mark_node
)
24785 parameters
= error_mark_node
;
24789 if (parameter
->decl_specifiers
.attributes
)
24790 cplus_decl_attributes (&decl
,
24791 parameter
->decl_specifiers
.attributes
,
24793 if (DECL_NAME (decl
))
24795 /* We cannot always pushdecl while parsing tentatively because
24796 it may have side effects and we can't be sure yet if we're
24797 parsing a declaration, e.g.:
24799 S foo(int(x), int(x), int{x});
24801 where it's not clear if we're dealing with a constructor call
24802 or a function declaration until we've seen the last argument
24803 which breaks it up.
24804 It's safe to pushdecl so long as it doesn't result in a clash
24805 with an already-pushed parameter. But we don't delay pushing
24806 different parameters to handle
24808 S foo(int(i), decltype(i) j = 42);
24812 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
24813 /* See if PARAMETERS already contains a parameter with the same
24814 DECL_NAME as DECL. */
24815 && [parameters
, decl
] {
24816 for (tree p
= parameters
; p
; p
= TREE_CHAIN (p
))
24817 if (DECL_NAME (decl
) == DECL_NAME (TREE_VALUE (p
)))
24821 pending_decls
->safe_push (decl
);
24823 decl
= pushdecl (decl
);
24826 if (decl
!= error_mark_node
)
24828 retrofit_lang_decl (decl
);
24829 DECL_PARM_INDEX (decl
) = ++index
;
24830 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
24833 /* Add the new parameter to the list. */
24834 *tail
= build_tree_list (parameter
->default_argument
, decl
);
24835 tail
= &TREE_CHAIN (*tail
);
24837 /* If the parameters were parenthesized, it's the case of
24838 T foo(X(x)) which looks like a variable definition but
24839 is a function declaration. */
24840 if (index
== 1 || PARENTHESIZED_LIST_P (parameters
))
24841 PARENTHESIZED_LIST_P (parameters
) = parenthesized_p
;
24843 /* Peek at the next token. */
24844 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
24845 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
24846 /* These are for Objective-C++ */
24847 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24848 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24849 /* The parameter-declaration-list is complete. */
24851 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24855 /* Peek at the next token. */
24856 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24857 /* If it's an ellipsis, then the list is complete. */
24858 if (token
->type
== CPP_ELLIPSIS
)
24860 /* Otherwise, there must be more parameters. Consume the
24862 cp_lexer_consume_token (parser
->lexer
);
24863 /* When parsing something like:
24865 int i(float f, double d)
24867 we can tell after seeing the declaration for "f" that we
24868 are not looking at an initialization of a variable "i",
24869 but rather at the declaration of a function "i".
24871 Due to the fact that the parsing of template arguments
24872 (as specified to a template-id) requires backtracking we
24873 cannot use this technique when inside a template argument
24875 if (!parser
->in_template_argument_list_p
24876 && !parser
->in_type_id_in_expr_p
24877 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
24878 /* However, a parameter-declaration of the form
24879 "float(f)" (which is a valid declaration of a
24880 parameter "f") can also be interpreted as an
24881 expression (the conversion of "f" to "float"). */
24882 && !parenthesized_p
)
24883 cp_parser_commit_to_tentative_parse (parser
);
24887 cp_parser_error (parser
, "expected %<,%> or %<...%>");
24888 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
24889 cp_parser_skip_to_closing_parenthesis (parser
,
24890 /*recovering=*/true,
24891 /*or_comma=*/false,
24892 /*consume_paren=*/false);
24897 parser
->in_unbraced_linkage_specification_p
24898 = saved_in_unbraced_linkage_specification_p
;
24900 /* Reset implicit_template_scope if we are about to leave the function
24901 parameter list that introduced it. Note that for out-of-line member
24902 definitions, there will be one or more class scopes before we get to
24903 the template parameter scope. */
24905 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
24906 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
24908 while (maybe_its
->kind
== sk_class
)
24909 maybe_its
= maybe_its
->level_chain
;
24910 if (maybe_its
== its
)
24912 parser
->implicit_template_parms
= 0;
24913 parser
->implicit_template_scope
= 0;
24920 /* Parse a parameter declaration.
24922 parameter-declaration:
24923 decl-specifier-seq ... [opt] declarator
24924 decl-specifier-seq declarator = assignment-expression
24925 decl-specifier-seq ... [opt] abstract-declarator [opt]
24926 decl-specifier-seq abstract-declarator [opt] = assignment-expression
24928 The parser flags FLAGS is used to control type-specifier parsing.
24930 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
24931 declares a template parameter. (In that case, a non-nested `>'
24932 token encountered during the parsing of the assignment-expression
24933 is not interpreted as a greater-than operator.)
24935 Returns a representation of the parameter, or NULL if an error
24936 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
24937 true iff the declarator is of the form "(p)". */
24939 static cp_parameter_declarator
*
24940 cp_parser_parameter_declaration (cp_parser
*parser
,
24941 cp_parser_flags flags
,
24942 bool template_parm_p
,
24943 bool *parenthesized_p
)
24945 int declares_class_or_enum
;
24946 cp_decl_specifier_seq decl_specifiers
;
24947 cp_declarator
*declarator
;
24948 tree default_argument
;
24949 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
24950 const char *saved_message
;
24951 bool template_parameter_pack_p
= false;
24953 /* In a template parameter, `>' is not an operator.
24957 When parsing a default template-argument for a non-type
24958 template-parameter, the first non-nested `>' is taken as the end
24959 of the template parameter-list rather than a greater-than
24962 /* Type definitions may not appear in parameter types. */
24963 saved_message
= parser
->type_definition_forbidden_message
;
24964 parser
->type_definition_forbidden_message
24965 = G_("types may not be defined in parameter types");
24967 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
24968 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
24969 (current_template_parms
)) : 0);
24971 /* Parse the declaration-specifiers. */
24972 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24973 cp_parser_decl_specifier_seq (parser
,
24976 &declares_class_or_enum
);
24978 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
24979 type-constraint opt auto can be used as a decl-specifier of the
24980 decl-specifier-seq of a parameter-declaration of a function declaration
24981 or lambda-expression..." but we must not synthesize an implicit template
24982 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
24983 we want to synthesize only the first auto. */
24984 auto cleanup
= make_temp_override
24985 (parser
->auto_is_implicit_function_template_parm_p
, false);
24987 /* Complain about missing 'typename' or other invalid type names. */
24988 if (!decl_specifiers
.any_type_specifiers_p
24989 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24990 decl_specifiers
.type
= error_mark_node
;
24992 /* If an error occurred, there's no reason to attempt to parse the
24993 rest of the declaration. */
24994 if (cp_parser_error_occurred (parser
))
24996 parser
->type_definition_forbidden_message
= saved_message
;
25000 /* Peek at the next token. */
25001 token
= cp_lexer_peek_token (parser
->lexer
);
25003 /* If the next token is a `)', `,', `=', `>', or `...', then there
25004 is no declarator. However, when variadic templates are enabled,
25005 there may be a declarator following `...'. */
25006 if (token
->type
== CPP_CLOSE_PAREN
25007 || token
->type
== CPP_COMMA
25008 || token
->type
== CPP_EQ
25009 || token
->type
== CPP_GREATER
)
25012 if (parenthesized_p
)
25013 *parenthesized_p
= false;
25015 /* Otherwise, there should be a declarator. */
25018 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
25019 parser
->default_arg_ok_p
= false;
25021 /* After seeing a decl-specifier-seq, if the next token is not a
25022 "(" or "{", there is no possibility that the code is a valid
25023 expression. Therefore, if parsing tentatively, we commit at
25025 if (!parser
->in_template_argument_list_p
25026 /* In an expression context, having seen:
25030 we cannot be sure whether we are looking at a
25031 function-type (taking a "char" as a parameter) or a cast
25032 of some object of type "char" to "int". */
25033 && !parser
->in_type_id_in_expr_p
25034 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
25035 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
25037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25039 if (decl_specifiers
.type
25040 && template_placeholder_p (decl_specifiers
.type
))
25041 /* This is a CTAD expression, not a parameter declaration. */
25042 cp_parser_simulate_error (parser
);
25045 cp_parser_commit_to_tentative_parse (parser
);
25047 /* Parse the declarator. */
25048 declarator_token_start
= token
;
25049 declarator
= cp_parser_declarator (parser
,
25050 CP_PARSER_DECLARATOR_EITHER
,
25051 CP_PARSER_FLAGS_NONE
,
25052 /*ctor_dtor_or_conv_p=*/NULL
,
25054 /*member_p=*/false,
25055 /*friend_p=*/false,
25056 /*static_p=*/false);
25057 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
25058 /* After the declarator, allow more attributes. */
25059 decl_specifiers
.attributes
25060 = attr_chainon (decl_specifiers
.attributes
,
25061 cp_parser_attributes_opt (parser
));
25063 /* If the declarator is a template parameter pack, remember that and
25064 clear the flag in the declarator itself so we don't get errors
25065 from grokdeclarator. */
25066 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
25068 declarator
->parameter_pack_p
= false;
25069 template_parameter_pack_p
= true;
25073 /* If the next token is an ellipsis, and we have not seen a declarator
25074 name, and if either the type of the declarator contains parameter
25075 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25076 for, eg, abbreviated integral type names), then we actually have a
25077 parameter pack expansion expression. Otherwise, leave the ellipsis
25078 for a C-style variadic function. */
25079 token
= cp_lexer_peek_token (parser
->lexer
);
25081 /* If a function parameter pack was specified and an implicit template
25082 parameter was introduced during cp_parser_parameter_declaration,
25083 change any implicit parameters introduced into packs. */
25084 if (parser
->implicit_template_parms
25085 && ((token
->type
== CPP_ELLIPSIS
25086 && declarator_can_be_parameter_pack (declarator
))
25087 || (declarator
&& declarator
->parameter_pack_p
)))
25089 int latest_template_parm_idx
= TREE_VEC_LENGTH
25090 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
25092 if (latest_template_parm_idx
!= template_parm_idx
)
25093 decl_specifiers
.type
= convert_generic_types_to_packs
25094 (decl_specifiers
.type
,
25095 template_parm_idx
, latest_template_parm_idx
);
25098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25100 tree type
= decl_specifiers
.type
;
25102 if (type
&& DECL_P (type
))
25103 type
= TREE_TYPE (type
);
25106 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
25107 && (template_parm_p
|| uses_parameter_packs (type
)))
25108 || (!type
&& template_parm_p
))
25109 && declarator_can_be_parameter_pack (declarator
))
25111 /* Consume the `...'. */
25112 cp_lexer_consume_token (parser
->lexer
);
25113 maybe_warn_variadic_templates ();
25115 /* Build a pack expansion type */
25116 if (template_parm_p
)
25117 template_parameter_pack_p
= true;
25118 else if (declarator
)
25119 declarator
->parameter_pack_p
= true;
25121 decl_specifiers
.type
= make_pack_expansion (type
);
25125 /* The restriction on defining new types applies only to the type
25126 of the parameter, not to the default argument. */
25127 parser
->type_definition_forbidden_message
= saved_message
;
25129 /* If the next token is `=', then process a default argument. */
25130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
25132 tree type
= decl_specifiers
.type
;
25133 token
= cp_lexer_peek_token (parser
->lexer
);
25135 declarator
->init_loc
= token
->location
;
25136 /* If we are defining a class, then the tokens that make up the
25137 default argument must be saved and processed later. */
25138 if (!template_parm_p
&& at_class_scope_p ()
25139 && TYPE_BEING_DEFINED (current_class_type
)
25140 && !LAMBDA_TYPE_P (current_class_type
))
25141 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
25143 /* A constrained-type-specifier may declare a type
25144 template-parameter. */
25145 else if (declares_constrained_type_template_parameter (type
))
25147 = cp_parser_default_type_template_argument (parser
);
25149 /* A constrained-type-specifier may declare a
25150 template-template-parameter. */
25151 else if (declares_constrained_template_template_parameter (type
))
25153 = cp_parser_default_template_template_argument (parser
);
25155 /* Outside of a class definition, we can just parse the
25156 assignment-expression. */
25159 = cp_parser_default_argument (parser
, template_parm_p
);
25161 if (!parser
->default_arg_ok_p
)
25163 permerror (token
->location
,
25164 "default arguments are only "
25165 "permitted for function parameters");
25167 else if ((declarator
&& declarator
->parameter_pack_p
)
25168 || template_parameter_pack_p
25169 || (decl_specifiers
.type
25170 && PACK_EXPANSION_P (decl_specifiers
.type
)))
25172 /* Find the name of the parameter pack. */
25173 cp_declarator
*id_declarator
= declarator
;
25174 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
25175 id_declarator
= id_declarator
->declarator
;
25177 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
25178 error_at (declarator_token_start
->location
,
25180 ? G_("template parameter pack %qD "
25181 "cannot have a default argument")
25182 : G_("parameter pack %qD cannot have "
25183 "a default argument"),
25184 id_declarator
->u
.id
.unqualified_name
);
25186 error_at (declarator_token_start
->location
,
25188 ? G_("template parameter pack cannot have "
25189 "a default argument")
25190 : G_("parameter pack cannot have a "
25191 "default argument"));
25193 default_argument
= NULL_TREE
;
25197 default_argument
= NULL_TREE
;
25199 if (default_argument
)
25200 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
25202 /* Generate a location for the parameter, ranging from the start of the
25203 initial token to the end of the final token (using input_location for
25204 the latter, set up by cp_lexer_set_source_position_from_token when
25207 If we have a identifier, then use it for the caret location, e.g.
25209 extern int callee (int one, int (*two)(int, int), float three);
25210 ~~~~~~^~~~~~~~~~~~~~
25212 otherwise, reuse the start location for the caret location e.g.:
25214 extern int callee (int one, int (*)(int, int), float three);
25218 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
25219 ? declarator
->id_loc
25220 : decl_spec_token_start
->location
);
25221 location_t param_loc
= make_location (caret_loc
,
25222 decl_spec_token_start
->location
,
25225 return make_parameter_declarator (&decl_specifiers
,
25229 template_parameter_pack_p
);
25232 /* Parse a default argument and return it.
25234 TEMPLATE_PARM_P is true if this is a default argument for a
25235 non-type template parameter. */
25237 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
25239 tree default_argument
= NULL_TREE
;
25240 bool saved_greater_than_is_operator_p
;
25241 unsigned char saved_local_variables_forbidden_p
;
25242 bool non_constant_p
, is_direct_init
;
25244 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25246 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
25247 parser
->greater_than_is_operator_p
= !template_parm_p
;
25248 auto odsd
= make_temp_override (parser
->omp_declare_simd
, NULL
);
25249 auto ord
= make_temp_override (parser
->oacc_routine
, NULL
);
25250 auto oafp
= make_temp_override (parser
->omp_attrs_forbidden_p
, false);
25252 /* Local variable names (and the `this' keyword) may not
25253 appear in a default argument. */
25254 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
25255 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
25256 /* Parse the assignment-expression. */
25257 if (template_parm_p
)
25258 push_deferring_access_checks (dk_no_deferred
);
25259 tree saved_class_ptr
= NULL_TREE
;
25260 tree saved_class_ref
= NULL_TREE
;
25261 /* The "this" pointer is not valid in a default argument. */
25264 saved_class_ptr
= current_class_ptr
;
25265 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
25266 saved_class_ref
= current_class_ref
;
25267 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
25270 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
25271 /* Restore the "this" pointer. */
25274 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
25275 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
25277 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
25278 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
25279 if (template_parm_p
)
25280 pop_deferring_access_checks ();
25281 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
25282 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
25284 return default_argument
;
25287 /* Parse a function-body.
25290 compound_statement */
25293 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
25295 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
25296 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
25300 /* Parse a ctor-initializer-opt followed by a function-body. Return
25301 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25302 is true we are parsing a function-try-block. */
25305 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
25306 bool in_function_try_block
)
25309 const bool check_body_p
25310 = (DECL_CONSTRUCTOR_P (current_function_decl
)
25311 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
));
25314 if (in_function_try_block
25315 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
25316 && cxx_dialect
< cxx20
)
25318 if (DECL_CONSTRUCTOR_P (current_function_decl
))
25319 pedwarn (input_location
, OPT_Wc__20_extensions
,
25320 "function-try-block body of %<constexpr%> constructor only "
25321 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25323 pedwarn (input_location
, OPT_Wc__20_extensions
,
25324 "function-try-block body of %<constexpr%> function only "
25325 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25328 /* Begin the function body. */
25329 body
= begin_function_body ();
25330 /* Parse the optional ctor-initializer. */
25331 cp_parser_ctor_initializer_opt (parser
);
25333 /* If we're parsing a constexpr constructor definition, we need
25334 to check that the constructor body is indeed empty. However,
25335 before we get to cp_parser_function_body lot of junk has been
25336 generated, so we can't just check that we have an empty block.
25337 Rather we take a snapshot of the outermost block, and check whether
25338 cp_parser_function_body changed its state. */
25341 list
= cur_stmt_list
;
25342 if (STATEMENT_LIST_TAIL (list
))
25343 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
25345 /* Parse the function-body. */
25346 cp_parser_function_body (parser
, in_function_try_block
);
25348 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
25349 /* Finish the function body. */
25350 finish_function_body (body
);
25353 /* Parse an initializer.
25356 = initializer-clause
25357 ( expression-list )
25359 Returns an expression representing the initializer. If no
25360 initializer is present, NULL_TREE is returned.
25362 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25363 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25364 set to TRUE if there is no initializer present. If there is an
25365 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25366 is set to true; otherwise it is set to false. */
25369 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
25370 bool* non_constant_p
, bool subexpression_p
)
25375 /* Peek at the next token. */
25376 token
= cp_lexer_peek_token (parser
->lexer
);
25378 /* Let our caller know whether or not this initializer was
25380 *is_direct_init
= (token
->type
!= CPP_EQ
);
25381 /* Assume that the initializer is constant. */
25382 *non_constant_p
= false;
25384 if (token
->type
== CPP_EQ
)
25386 /* Consume the `='. */
25387 cp_lexer_consume_token (parser
->lexer
);
25388 /* Parse the initializer-clause. */
25389 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
25391 else if (token
->type
== CPP_OPEN_PAREN
)
25393 vec
<tree
, va_gc
> *vec
;
25394 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
25396 /*allow_expansion_p=*/true,
25399 return error_mark_node
;
25400 init
= build_tree_list_vec (vec
);
25401 release_tree_vector (vec
);
25403 else if (token
->type
== CPP_OPEN_BRACE
)
25405 cp_lexer_set_source_position (parser
->lexer
);
25406 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
25407 init
= cp_parser_braced_list (parser
, non_constant_p
);
25408 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
25412 /* Anything else is an error. */
25413 cp_parser_error (parser
, "expected initializer");
25414 init
= error_mark_node
;
25417 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
25418 init
= error_mark_node
;
25423 /* Parse an initializer-clause.
25425 initializer-clause:
25426 assignment-expression
25429 Returns an expression representing the initializer.
25431 If the `assignment-expression' production is used the value
25432 returned is simply a representation for the expression.
25434 Otherwise, calls cp_parser_braced_list. */
25437 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
25439 cp_expr initializer
;
25441 /* Assume the expression is constant. */
25442 *non_constant_p
= false;
25444 /* If it is not a `{', then we are looking at an
25445 assignment-expression. */
25446 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
25449 = cp_parser_constant_expression (parser
,
25450 /*allow_non_constant_p=*/2,
25454 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
25456 return initializer
;
25459 /* Parse a brace-enclosed initializer list.
25462 { initializer-list , [opt] }
25463 { designated-initializer-list , [opt] }
25466 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25467 the elements of the initializer-list (or NULL, if the last
25468 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25469 NULL_TREE. There is no way to detect whether or not the optional
25470 trailing `,' was provided. NON_CONSTANT_P is as for
25471 cp_parser_initializer. */
25474 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
25477 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25479 /* Consume the `{' token. */
25480 matching_braces braces
;
25481 braces
.require_open (parser
);
25482 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25483 initializer
= make_node (CONSTRUCTOR
);
25484 /* If it's not a `}', then there is a non-trivial initializer. */
25485 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
25488 /* Parse the initializer list. */
25489 CONSTRUCTOR_ELTS (initializer
)
25490 = cp_parser_initializer_list (parser
, non_constant_p
, &designated
);
25491 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer
) = designated
;
25492 /* A trailing `,' token is allowed. */
25493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25494 cp_lexer_consume_token (parser
->lexer
);
25497 *non_constant_p
= false;
25498 /* Now, there should be a trailing `}'. */
25499 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25500 braces
.require_close (parser
);
25501 TREE_TYPE (initializer
) = init_list_type_node
;
25503 cp_expr
result (initializer
);
25504 /* Build a location of the form:
25507 with caret==start at the open brace, finish at the close brace. */
25508 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
25509 result
.set_location (combined_loc
);
25513 /* Consume tokens up to, and including, the next non-nested closing `]'.
25514 Returns true iff we found a closing `]'. */
25517 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
25519 unsigned square_depth
= 0;
25523 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
25525 switch (token
->type
)
25527 case CPP_PRAGMA_EOL
:
25528 if (!parser
->lexer
->in_pragma
)
25532 /* If we've run out of tokens, then there is no closing `]'. */
25535 case CPP_OPEN_SQUARE
:
25539 case CPP_CLOSE_SQUARE
:
25540 if (!square_depth
--)
25542 cp_lexer_consume_token (parser
->lexer
);
25551 /* Consume the token. */
25552 cp_lexer_consume_token (parser
->lexer
);
25556 /* Return true if we are looking at an array-designator, false otherwise. */
25559 cp_parser_array_designator_p (cp_parser
*parser
)
25561 /* Consume the `['. */
25562 cp_lexer_consume_token (parser
->lexer
);
25564 cp_lexer_save_tokens (parser
->lexer
);
25566 /* Skip tokens until the next token is a closing square bracket.
25567 If we find the closing `]', and the next token is a `=', then
25568 we are looking at an array designator. */
25569 bool array_designator_p
25570 = (cp_parser_skip_to_closing_square_bracket (parser
)
25571 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
25573 /* Roll back the tokens we skipped. */
25574 cp_lexer_rollback_tokens (parser
->lexer
);
25576 return array_designator_p
;
25579 /* Parse an initializer-list.
25582 initializer-clause ... [opt]
25583 initializer-list , initializer-clause ... [opt]
25587 designated-initializer-list:
25588 designated-initializer-clause
25589 designated-initializer-list , designated-initializer-clause
25591 designated-initializer-clause:
25592 designator brace-or-equal-initializer
25600 designation initializer-clause ...[opt]
25601 initializer-list , designation initializer-clause ...[opt]
25606 [ constant-expression ] =
25608 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25609 for the initializer. If the INDEX of the elt is non-NULL, it is the
25610 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25611 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25612 are any designators. */
25614 static vec
<constructor_elt
, va_gc
> *
25615 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
,
25618 vec
<constructor_elt
, va_gc
> *v
= NULL
;
25619 bool first_p
= true;
25620 tree first_designator
= NULL_TREE
;
25622 /* Assume all of the expressions are constant. */
25623 *non_constant_p
= false;
25625 unsigned nelts
= 0;
25626 int suppress
= suppress_location_wrappers
;
25628 /* Parse the rest of the list. */
25634 bool clause_non_constant_p
;
25635 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25637 /* Handle the C++20 syntax, '. id ='. */
25638 if ((cxx_dialect
>= cxx20
25639 || cp_parser_allow_gnu_extensions_p (parser
))
25640 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
25641 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
25642 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
25643 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
25644 == CPP_OPEN_BRACE
)))
25646 if (pedantic
&& cxx_dialect
< cxx20
)
25647 pedwarn (loc
, OPT_Wc__20_extensions
,
25648 "C++ designated initializers only available with "
25649 "%<-std=c++20%> or %<-std=gnu++20%>");
25650 /* Consume the `.'. */
25651 cp_lexer_consume_token (parser
->lexer
);
25652 /* Consume the identifier. */
25653 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
25654 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
25655 /* Consume the `='. */
25656 cp_lexer_consume_token (parser
->lexer
);
25658 /* Also, if the next token is an identifier and the following one is a
25659 colon, we are looking at the GNU designated-initializer
25661 else if (cp_parser_allow_gnu_extensions_p (parser
)
25662 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
25663 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
25666 /* Warn the user that they are using an extension. */
25667 pedwarn (loc
, OPT_Wpedantic
,
25668 "ISO C++ does not allow GNU designated initializers");
25669 /* Consume the identifier. */
25670 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
25671 /* Consume the `:'. */
25672 cp_lexer_consume_token (parser
->lexer
);
25674 /* Also handle C99 array designators, '[ const ] ='. */
25675 else if (cp_parser_allow_gnu_extensions_p (parser
)
25676 && !c_dialect_objc ()
25677 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25679 /* In C++11, [ could start a lambda-introducer. */
25680 bool non_const
= false;
25682 cp_parser_parse_tentatively (parser
);
25684 if (!cp_parser_array_designator_p (parser
))
25686 cp_parser_simulate_error (parser
);
25687 designator
= NULL_TREE
;
25691 designator
= cp_parser_constant_expression (parser
, true,
25693 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25694 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
25697 if (!cp_parser_parse_definitely (parser
))
25698 designator
= NULL_TREE
;
25700 && (!require_potential_rvalue_constant_expression
25702 designator
= NULL_TREE
;
25704 /* Warn the user that they are using an extension. */
25705 pedwarn (loc
, OPT_Wpedantic
,
25706 "ISO C++ does not allow C99 designated initializers");
25709 designator
= NULL_TREE
;
25713 first_designator
= designator
;
25716 else if (cxx_dialect
>= cxx20
25717 && first_designator
!= error_mark_node
25718 && (!first_designator
!= !designator
))
25720 error_at (loc
, "either all initializer clauses should be designated "
25721 "or none of them should be");
25722 first_designator
= error_mark_node
;
25724 else if (cxx_dialect
< cxx20
&& !first_designator
)
25725 first_designator
= designator
;
25727 /* Parse the initializer. */
25728 initializer
= cp_parser_initializer_clause (parser
,
25729 &clause_non_constant_p
);
25730 /* If any clause is non-constant, so is the entire initializer. */
25731 if (clause_non_constant_p
)
25732 *non_constant_p
= true;
25734 /* If we have an ellipsis, this is an initializer pack
25736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25738 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25740 /* Consume the `...'. */
25741 cp_lexer_consume_token (parser
->lexer
);
25743 if (designator
&& cxx_dialect
>= cxx20
)
25745 "%<...%> not allowed in designated initializer list");
25747 /* Turn the initializer into an initializer expansion. */
25748 initializer
= make_pack_expansion (initializer
);
25751 /* Add it to the vector. */
25752 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
25754 /* If the next token is not a comma, we have reached the end of
25756 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25759 /* Peek at the next token. */
25760 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25761 /* If the next token is a `}', then we're still done. An
25762 initializer-clause can have a trailing `,' after the
25763 initializer-list and before the closing `}'. */
25764 if (token
->type
== CPP_CLOSE_BRACE
)
25767 /* Suppress location wrappers in a long initializer to save memory
25768 (14179). The cutoff is chosen arbitrarily. */
25769 const unsigned loc_max
= 256;
25771 if (TREE_CODE (initializer
) == CONSTRUCTOR
)
25772 /* Look one level down because it's easy. Looking deeper would require
25773 passing down a nelts pointer, and I don't think multi-level massive
25774 initializers are common enough to justify this. */
25775 incr
= CONSTRUCTOR_NELTS (initializer
);
25777 if (nelts
>= loc_max
&& (nelts
- incr
) < loc_max
)
25778 ++suppress_location_wrappers
;
25780 /* Consume the `,' token. */
25781 cp_lexer_consume_token (parser
->lexer
);
25784 /* The same identifier shall not appear in multiple designators
25785 of a designated-initializer-list. */
25786 if (first_designator
)
25789 tree designator
, val
;
25790 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
25791 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
25793 if (IDENTIFIER_MARKED (designator
))
25795 error_at (cp_expr_loc_or_input_loc (val
),
25796 "%<.%s%> designator used multiple times in "
25797 "the same initializer list",
25798 IDENTIFIER_POINTER (designator
));
25799 (*v
)[i
].index
= error_mark_node
;
25802 IDENTIFIER_MARKED (designator
) = 1;
25804 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
25805 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
25806 IDENTIFIER_MARKED (designator
) = 0;
25809 suppress_location_wrappers
= suppress
;
25811 *designated
= first_designator
!= NULL_TREE
;
25815 /* Classes [gram.class] */
25817 /* Parse a class-name.
25823 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25824 to indicate that names looked up in dependent types should be
25825 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
25826 keyword has been used to indicate that the name that appears next
25827 is a template. TAG_TYPE indicates the explicit tag given before
25828 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
25829 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
25830 is the class being defined in a class-head. If ENUM_OK is TRUE,
25831 enum-names are also accepted.
25833 Returns the TYPE_DECL representing the class. */
25836 cp_parser_class_name (cp_parser
*parser
,
25837 bool typename_keyword_p
,
25838 bool template_keyword_p
,
25839 enum tag_types tag_type
,
25840 bool check_dependency_p
,
25842 bool is_declaration
,
25846 tree identifier
= NULL_TREE
;
25848 /* All class-names start with an identifier. */
25849 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25850 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
25852 cp_parser_error (parser
, "expected class-name");
25853 return error_mark_node
;
25856 /* PARSER->SCOPE can be cleared when parsing the template-arguments
25857 to a template-id, so we save it here. Consider object scope too,
25858 so that make_typename_type below can use it (cp_parser_template_name
25859 considers object scope also). This may happen with code like
25861 p->template A<T>::a()
25863 where we first want to look up A<T>::a in the class of the object
25864 expression, as per [basic.lookup.classref]. */
25865 tree scope
= parser
->scope
? parser
->scope
: parser
->context
->object_type
;
25866 /* This only checks parser->scope to avoid duplicate errors; if
25867 ->object_type is erroneous, go on to give a parse error. */
25868 if (parser
->scope
== error_mark_node
)
25869 return error_mark_node
;
25871 /* Any name names a type if we're following the `typename' keyword
25872 in a qualified name where the enclosing scope is type-dependent. */
25873 const bool typename_p
= (typename_keyword_p
25875 && TYPE_P (parser
->scope
)
25876 && dependent_scope_p (parser
->scope
));
25877 /* Handle the common case (an identifier, but not a template-id)
25879 if (token
->type
== CPP_NAME
25880 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
25882 cp_token
*identifier_token
;
25885 /* Look for the identifier. */
25886 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
25887 ambiguous_p
= identifier_token
->error_reported
;
25888 identifier
= cp_parser_identifier (parser
);
25889 /* If the next token isn't an identifier, we are certainly not
25890 looking at a class-name. */
25891 if (identifier
== error_mark_node
)
25892 decl
= error_mark_node
;
25893 /* If we know this is a type-name, there's no need to look it
25895 else if (typename_p
)
25899 tree ambiguous_decls
;
25900 /* If we already know that this lookup is ambiguous, then
25901 we've already issued an error message; there's no reason
25905 cp_parser_simulate_error (parser
);
25906 return error_mark_node
;
25908 /* If the next token is a `::', then the name must be a type
25911 [basic.lookup.qual]
25913 During the lookup for a name preceding the :: scope
25914 resolution operator, object, function, and enumerator
25915 names are ignored. */
25916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
25917 tag_type
= scope_type
;
25918 /* Look up the name. */
25919 decl
= cp_parser_lookup_name (parser
, identifier
,
25921 /*is_template=*/false,
25922 /*is_namespace=*/false,
25923 check_dependency_p
,
25925 identifier_token
->location
);
25926 if (ambiguous_decls
)
25928 if (cp_parser_parsing_tentatively (parser
))
25929 cp_parser_simulate_error (parser
);
25930 return error_mark_node
;
25936 /* Try a template-id. */
25937 decl
= cp_parser_template_id (parser
, template_keyword_p
,
25938 check_dependency_p
,
25941 if (decl
== error_mark_node
)
25942 return error_mark_node
;
25945 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
25947 /* If this is a typename, create a TYPENAME_TYPE. */
25948 if (typename_p
&& decl
!= error_mark_node
)
25950 decl
= make_typename_type (scope
, decl
, typename_type
,
25951 /*complain=*/tf_error
);
25952 if (decl
!= error_mark_node
)
25953 decl
= TYPE_NAME (decl
);
25956 decl
= strip_using_decl (decl
);
25958 /* Check to see that it is really the name of a class. */
25959 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
25960 && identifier_p (TREE_OPERAND (decl
, 0))
25961 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
25962 /* Situations like this:
25964 template <typename T> struct A {
25965 typename T::template X<int>::I i;
25968 are problematic. Is `T::template X<int>' a class-name? The
25969 standard does not seem to be definitive, but there is no other
25970 valid interpretation of the following `::'. Therefore, those
25971 names are considered class-names. */
25973 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
25974 if (decl
!= error_mark_node
)
25975 decl
= TYPE_NAME (decl
);
25977 else if (TREE_CODE (decl
) != TYPE_DECL
25978 || TREE_TYPE (decl
) == error_mark_node
25979 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
25980 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
25981 /* In Objective-C 2.0, a classname followed by '.' starts a
25982 dot-syntax expression, and it's not a type-name. */
25983 || (c_dialect_objc ()
25984 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
25985 && objc_is_class_name (decl
)))
25986 decl
= error_mark_node
;
25988 if (decl
== error_mark_node
)
25989 cp_parser_error (parser
, "expected class-name");
25990 else if (identifier
&& !parser
->scope
)
25991 maybe_note_name_used_in_class (identifier
, decl
);
25996 /* Make sure that any member-function parameters are in scope.
25997 For instance, a function's noexcept-specifier can use the function's
26001 void fn (int p) noexcept(noexcept(p));
26004 so we need to make sure name lookup can find them. This is used
26005 when we delay parsing of the noexcept-specifier. */
26008 inject_parm_decls (tree decl
)
26010 begin_scope (sk_function_parms
, decl
);
26011 tree args
= DECL_ARGUMENTS (decl
);
26013 do_push_parm_decls (decl
, args
, /*nonparms=*/NULL
);
26015 if (args
&& is_this_parameter (args
))
26017 gcc_checking_assert (current_class_ptr
== NULL_TREE
);
26018 current_class_ref
= cp_build_fold_indirect_ref (args
);
26019 current_class_ptr
= args
;
26023 /* Undo the effects of inject_parm_decls. */
26026 pop_injected_parms (void)
26028 pop_bindings_and_leave_scope ();
26029 current_class_ptr
= current_class_ref
= NULL_TREE
;
26032 /* Parse a class-specifier.
26035 class-head { member-specification [opt] }
26037 Returns the TREE_TYPE representing the class. */
26040 cp_parser_class_specifier (cp_parser
* parser
)
26042 auto_timevar
tv (TV_PARSE_STRUCT
);
26045 tree attributes
= NULL_TREE
;
26046 bool nested_name_specifier_p
;
26047 unsigned saved_num_template_parameter_lists
;
26048 bool saved_in_function_body
;
26049 unsigned char in_statement
;
26050 bool in_switch_statement_p
;
26051 bool saved_in_unbraced_linkage_specification_p
;
26052 tree old_scope
= NULL_TREE
;
26053 tree scope
= NULL_TREE
;
26054 cp_token
*closing_brace
;
26056 push_deferring_access_checks (dk_no_deferred
);
26058 /* Parse the class-head. */
26059 type
= cp_parser_class_head (parser
,
26060 &nested_name_specifier_p
);
26061 /* If the class-head was a semantic disaster, skip the entire body
26065 cp_parser_skip_to_end_of_block_or_statement (parser
);
26066 pop_deferring_access_checks ();
26067 return error_mark_node
;
26070 /* Look for the `{'. */
26071 matching_braces braces
;
26072 if (!braces
.require_open (parser
))
26074 pop_deferring_access_checks ();
26075 return error_mark_node
;
26078 cp_ensure_no_omp_declare_simd (parser
);
26079 cp_ensure_no_oacc_routine (parser
);
26081 /* Issue an error message if type-definitions are forbidden here. */
26082 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
26083 /* Remember that we are defining one more class. */
26084 ++parser
->num_classes_being_defined
;
26085 /* Inside the class, surrounding template-parameter-lists do not
26087 saved_num_template_parameter_lists
26088 = parser
->num_template_parameter_lists
;
26089 parser
->num_template_parameter_lists
= 0;
26090 /* We are not in a function body. */
26091 saved_in_function_body
= parser
->in_function_body
;
26092 parser
->in_function_body
= false;
26093 /* Or in a loop. */
26094 in_statement
= parser
->in_statement
;
26095 parser
->in_statement
= 0;
26096 /* Or in a switch. */
26097 in_switch_statement_p
= parser
->in_switch_statement_p
;
26098 parser
->in_switch_statement_p
= false;
26099 /* We are not immediately inside an extern "lang" block. */
26100 saved_in_unbraced_linkage_specification_p
26101 = parser
->in_unbraced_linkage_specification_p
;
26102 parser
->in_unbraced_linkage_specification_p
= false;
26104 /* Start the class. */
26105 if (nested_name_specifier_p
)
26107 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
26108 /* SCOPE must be a scope nested inside current scope. */
26109 if (is_nested_namespace (current_namespace
,
26110 decl_namespace_context (scope
)))
26111 old_scope
= push_inner_scope (scope
);
26113 nested_name_specifier_p
= false;
26115 type
= begin_class_definition (type
);
26117 if (type
== error_mark_node
)
26118 /* If the type is erroneous, skip the entire body of the class. */
26119 cp_parser_skip_to_closing_brace (parser
);
26121 /* Parse the member-specification. */
26122 cp_parser_member_specification_opt (parser
);
26124 /* Look for the trailing `}'. */
26125 closing_brace
= braces
.require_close (parser
);
26126 /* Look for trailing attributes to apply to this class. */
26127 if (cp_parser_allow_gnu_extensions_p (parser
))
26128 attributes
= cp_parser_gnu_attributes_opt (parser
);
26129 if (type
!= error_mark_node
)
26130 type
= finish_struct (type
, attributes
);
26131 if (nested_name_specifier_p
)
26132 pop_inner_scope (old_scope
, scope
);
26134 /* We've finished a type definition. Check for the common syntax
26135 error of forgetting a semicolon after the definition. We need to
26136 be careful, as we can't just check for not-a-semicolon and be done
26137 with it; the user might have typed:
26139 class X { } c = ...;
26140 class X { } *p = ...;
26142 and so forth. Instead, enumerate all the possible tokens that
26143 might follow this production; if we don't see one of them, then
26144 complain and silently insert the semicolon. */
26146 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26147 bool want_semicolon
= true;
26149 if (cp_next_tokens_can_be_std_attribute_p (parser
))
26150 /* Don't try to parse c++11 attributes here. As per the
26151 grammar, that should be a task for
26152 cp_parser_decl_specifier_seq. */
26153 want_semicolon
= false;
26155 switch (token
->type
)
26158 case CPP_SEMICOLON
:
26161 case CPP_OPEN_PAREN
:
26162 case CPP_CLOSE_PAREN
:
26165 want_semicolon
= false;
26168 /* While it's legal for type qualifiers and storage class
26169 specifiers to follow type definitions in the grammar, only
26170 compiler testsuites contain code like that. Assume that if
26171 we see such code, then what we're really seeing is a case
26175 const <type> var = ...;
26180 static <type> func (...) ...
26182 i.e. the qualifier or specifier applies to the next
26183 declaration. To do so, however, we need to look ahead one
26184 more token to see if *that* token is a type specifier.
26186 This code could be improved to handle:
26189 static const <type> var = ...; */
26191 if (keyword_is_decl_specifier (token
->keyword
))
26193 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26195 /* Handling user-defined types here would be nice, but very
26198 = (lookahead
->type
== CPP_KEYWORD
26199 && keyword_begins_type_specifier (lookahead
->keyword
));
26206 /* If we don't have a type, then something is very wrong and we
26207 shouldn't try to do anything clever. Likewise for not seeing the
26209 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
26211 /* Locate the closing brace. */
26212 cp_token_position prev
26213 = cp_lexer_previous_token_position (parser
->lexer
);
26214 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
26215 location_t loc
= prev_token
->location
;
26217 /* We want to suggest insertion of a ';' immediately *after* the
26218 closing brace, so, if we can, offset the location by 1 column. */
26219 location_t next_loc
= loc
;
26220 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
26221 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
26223 rich_location
richloc (line_table
, next_loc
);
26225 /* If we successfully offset the location, suggest the fix-it. */
26226 if (next_loc
!= loc
)
26227 richloc
.add_fixit_insert_before (next_loc
, ";");
26229 if (CLASSTYPE_DECLARED_CLASS (type
))
26230 error_at (&richloc
,
26231 "expected %<;%> after class definition");
26232 else if (TREE_CODE (type
) == RECORD_TYPE
)
26233 error_at (&richloc
,
26234 "expected %<;%> after struct definition");
26235 else if (TREE_CODE (type
) == UNION_TYPE
)
26236 error_at (&richloc
,
26237 "expected %<;%> after union definition");
26239 gcc_unreachable ();
26241 /* Unget one token and smash it to look as though we encountered
26242 a semicolon in the input stream. */
26243 cp_lexer_set_token_position (parser
->lexer
, prev
);
26244 token
= cp_lexer_peek_token (parser
->lexer
);
26245 token
->type
= CPP_SEMICOLON
;
26246 token
->keyword
= RID_MAX
;
26250 /* If this class is not itself within the scope of another class,
26251 then we need to parse the bodies of all of the queued function
26252 definitions. Note that the queued functions defined in a class
26253 are not always processed immediately following the
26254 class-specifier for that class. Consider:
26257 struct B { void f() { sizeof (A); } };
26260 If `f' were processed before the processing of `A' were
26261 completed, there would be no way to compute the size of `A'.
26262 Note that the nesting we are interested in here is lexical --
26263 not the semantic nesting given by TYPE_CONTEXT. In particular,
26266 struct A { struct B; };
26267 struct A::B { void f() { } };
26269 there is no need to delay the parsing of `A::B::f'. */
26270 if (--parser
->num_classes_being_defined
== 0)
26273 tree class_type
= NULL_TREE
;
26274 tree pushed_scope
= NULL_TREE
;
26276 cp_default_arg_entry
*e
;
26278 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
26280 /* Skip default arguments, NSDMIs, etc, in order to improve
26281 error recovery (c++/71169, c++/71832). */
26282 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
26283 vec_safe_truncate (unparsed_nsdmis
, 0);
26284 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
26287 /* In a first pass, parse default arguments to the functions.
26288 Then, in a second pass, parse the bodies of the functions.
26289 This two-phased approach handles cases like:
26297 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
26300 /* If there are default arguments that have not yet been processed,
26301 take care of them now. */
26302 if (class_type
!= e
->class_type
)
26305 pop_scope (pushed_scope
);
26306 class_type
= e
->class_type
;
26307 pushed_scope
= push_scope (class_type
);
26309 /* Make sure that any template parameters are in scope. */
26310 maybe_begin_member_template_processing (decl
);
26311 /* Parse the default argument expressions. */
26312 cp_parser_late_parsing_default_args (parser
, decl
);
26313 /* Remove any template parameters from the symbol table. */
26314 maybe_end_member_template_processing ();
26316 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
26318 /* If there are noexcept-specifiers that have not yet been processed,
26319 take care of them now. Do this before processing NSDMIs as they
26320 may depend on noexcept-specifiers already having been processed. */
26321 tree save_ccp
= current_class_ptr
;
26322 tree save_ccr
= current_class_ref
;
26323 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts
, ix
, decl
)
26325 tree ctx
= DECL_CONTEXT (decl
);
26326 if (class_type
!= ctx
)
26329 pop_scope (pushed_scope
);
26331 pushed_scope
= push_scope (class_type
);
26334 tree def_parse
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
26335 def_parse
= TREE_PURPOSE (def_parse
);
26337 /* Make sure that any template parameters are in scope. */
26338 maybe_begin_member_template_processing (decl
);
26340 /* Make sure that any member-function parameters are in scope.
26341 This function doesn't expect ccp to be set. */
26342 current_class_ptr
= current_class_ref
= NULL_TREE
;
26343 inject_parm_decls (decl
);
26345 /* 'this' is not allowed in static member functions. */
26346 unsigned char local_variables_forbidden_p
26347 = parser
->local_variables_forbidden_p
;
26348 if (DECL_THIS_STATIC (decl
))
26349 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
26351 /* Now we can parse the noexcept-specifier. */
26352 tree spec
= cp_parser_late_noexcept_specifier (parser
, def_parse
);
26354 if (spec
== error_mark_node
)
26357 /* Update the fn's type directly -- it might have escaped
26358 beyond this decl :( */
26359 fixup_deferred_exception_variants (TREE_TYPE (decl
), spec
);
26360 /* Update any instantiations we've already created. We must
26361 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26362 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26364 for (tree i
: DEFPARSE_INSTANTIATIONS (def_parse
))
26365 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i
))
26366 = spec
? TREE_PURPOSE (spec
) : error_mark_node
;
26368 /* Restore the state of local_variables_forbidden_p. */
26369 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
26371 /* The finish_struct call above performed various override checking,
26372 but it skipped unparsed noexcept-specifier operands. Now that we
26373 have resolved them, check again. */
26374 noexcept_override_late_checks (type
, decl
);
26376 /* Remove any member-function parameters from the symbol table. */
26377 pop_injected_parms ();
26379 /* Remove any template parameters from the symbol table. */
26380 maybe_end_member_template_processing ();
26382 vec_safe_truncate (unparsed_noexcepts
, 0);
26384 /* Now parse any NSDMIs. */
26385 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
26387 if (class_type
!= DECL_CONTEXT (decl
))
26390 pop_scope (pushed_scope
);
26391 class_type
= DECL_CONTEXT (decl
);
26392 pushed_scope
= push_scope (class_type
);
26394 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
26395 cp_parser_late_parsing_nsdmi (parser
, decl
);
26397 vec_safe_truncate (unparsed_nsdmis
, 0);
26398 current_class_ptr
= save_ccp
;
26399 current_class_ref
= save_ccr
;
26401 pop_scope (pushed_scope
);
26403 /* Now parse the body of the functions. */
26406 /* OpenMP UDRs need to be parsed before all other functions. */
26407 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
26408 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
26409 cp_parser_late_parsing_for_member (parser
, decl
);
26410 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
26411 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
26412 cp_parser_late_parsing_for_member (parser
, decl
);
26415 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
26416 cp_parser_late_parsing_for_member (parser
, decl
);
26417 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
26420 /* Put back any saved access checks. */
26421 pop_deferring_access_checks ();
26423 /* Restore saved state. */
26424 parser
->in_switch_statement_p
= in_switch_statement_p
;
26425 parser
->in_statement
= in_statement
;
26426 parser
->in_function_body
= saved_in_function_body
;
26427 parser
->num_template_parameter_lists
26428 = saved_num_template_parameter_lists
;
26429 parser
->in_unbraced_linkage_specification_p
26430 = saved_in_unbraced_linkage_specification_p
;
26435 /* Parse a class-head.
26438 class-key identifier [opt] base-clause [opt]
26439 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26440 class-key nested-name-specifier [opt] template-id
26443 class-virt-specifier:
26447 class-key attributes identifier [opt] base-clause [opt]
26448 class-key attributes nested-name-specifier identifier base-clause [opt]
26449 class-key attributes nested-name-specifier [opt] template-id
26452 Upon return BASES is initialized to the list of base classes (or
26453 NULL, if there are none) in the same form returned by
26454 cp_parser_base_clause.
26456 Returns the TYPE of the indicated class. Sets
26457 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26458 involving a nested-name-specifier was used, and FALSE otherwise.
26460 Returns error_mark_node if this is not a class-head.
26462 Returns NULL_TREE if the class-head is syntactically valid, but
26463 semantically invalid in a way that means we should skip the entire
26464 body of the class. */
26467 cp_parser_class_head (cp_parser
* parser
,
26468 bool* nested_name_specifier_p
)
26470 tree nested_name_specifier
;
26471 enum tag_types class_key
;
26472 tree id
= NULL_TREE
;
26473 tree type
= NULL_TREE
;
26476 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
26477 bool template_id_p
= false;
26478 bool qualified_p
= false;
26479 bool invalid_nested_name_p
= false;
26480 bool invalid_explicit_specialization_p
= false;
26481 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
26482 tree pushed_scope
= NULL_TREE
;
26483 unsigned num_templates
;
26484 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
26485 /* Assume no nested-name-specifier will be present. */
26486 *nested_name_specifier_p
= false;
26487 /* Assume no template parameter lists will be used in defining the
26490 parser
->colon_corrects_to_scope_p
= false;
26492 /* Look for the class-key. */
26493 class_key
= cp_parser_class_key (parser
);
26494 if (class_key
== none_type
)
26495 return error_mark_node
;
26497 location_t class_head_start_location
= input_location
;
26499 /* Parse the attributes. */
26500 attributes
= cp_parser_attributes_opt (parser
);
26502 /* If the next token is `::', that is invalid -- but sometimes
26503 people do try to write:
26507 Handle this gracefully by accepting the extra qualifier, and then
26508 issuing an error about it later if this really is a
26509 class-head. If it turns out just to be an elaborated type
26510 specifier, remain silent. */
26511 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
26512 qualified_p
= true;
26514 push_deferring_access_checks (dk_no_check
);
26516 /* Determine the name of the class. Begin by looking for an
26517 optional nested-name-specifier. */
26518 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
26519 nested_name_specifier
26520 = cp_parser_nested_name_specifier_opt (parser
,
26521 /*typename_keyword_p=*/false,
26522 /*check_dependency_p=*/false,
26524 /*is_declaration=*/false);
26525 /* If there was a nested-name-specifier, then there *must* be an
26528 cp_token
*bad_template_keyword
= NULL
;
26530 if (nested_name_specifier
)
26532 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
26533 /* Although the grammar says `identifier', it really means
26534 `class-name' or `template-name'. You are only allowed to
26535 define a class that has already been declared with this
26538 The proposed resolution for Core Issue 180 says that wherever
26539 you see `class T::X' you should treat `X' as a type-name.
26541 It is OK to define an inaccessible class; for example:
26543 class A { class B; };
26546 We do not know if we will see a class-name, or a
26547 template-name. We look for a class-name first, in case the
26548 class-name is a template-id; if we looked for the
26549 template-name first we would stop after the template-name. */
26550 cp_parser_parse_tentatively (parser
);
26551 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26552 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
26553 type
= cp_parser_class_name (parser
,
26554 /*typename_keyword_p=*/false,
26555 /*template_keyword_p=*/false,
26557 /*check_dependency_p=*/false,
26558 /*class_head_p=*/true,
26559 /*is_declaration=*/false);
26560 /* If that didn't work, ignore the nested-name-specifier. */
26561 if (!cp_parser_parse_definitely (parser
))
26563 invalid_nested_name_p
= true;
26564 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
26565 id
= cp_parser_identifier (parser
);
26566 if (id
== error_mark_node
)
26569 /* If we could not find a corresponding TYPE, treat this
26570 declaration like an unqualified declaration. */
26571 if (type
== error_mark_node
)
26572 nested_name_specifier
= NULL_TREE
;
26573 /* Otherwise, count the number of templates used in TYPE and its
26574 containing scopes. */
26576 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
26578 /* Otherwise, the identifier is optional. */
26581 /* We don't know whether what comes next is a template-id,
26582 an identifier, or nothing at all. */
26583 cp_parser_parse_tentatively (parser
);
26584 /* Check for a template-id. */
26585 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
26586 id
= cp_parser_template_id (parser
,
26587 /*template_keyword_p=*/false,
26588 /*check_dependency_p=*/true,
26590 /*is_declaration=*/true);
26591 /* If that didn't work, it could still be an identifier. */
26592 if (!cp_parser_parse_definitely (parser
))
26594 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26596 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
26597 id
= cp_parser_identifier (parser
);
26604 template_id_p
= true;
26609 pop_deferring_access_checks ();
26613 cp_parser_check_for_invalid_template_id (parser
, id
,
26615 type_start_token
->location
);
26617 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
26619 /* If it's not a `:' or a `{' then we can't really be looking at a
26620 class-head, since a class-head only appears as part of a
26621 class-specifier. We have to detect this situation before calling
26622 xref_tag, since that has irreversible side-effects. */
26623 if (!cp_parser_next_token_starts_class_definition_p (parser
))
26625 cp_parser_error (parser
, "expected %<{%> or %<:%>");
26626 type
= error_mark_node
;
26630 /* At this point, we're going ahead with the class-specifier, even
26631 if some other problem occurs. */
26632 cp_parser_commit_to_tentative_parse (parser
);
26633 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
26635 cp_parser_error (parser
,
26636 "cannot specify %<override%> for a class");
26637 type
= error_mark_node
;
26640 /* Issue the error about the overly-qualified name now. */
26643 cp_parser_error (parser
,
26644 "global qualification of class name is invalid");
26645 type
= error_mark_node
;
26648 else if (invalid_nested_name_p
)
26650 cp_parser_error (parser
,
26651 "qualified name does not name a class");
26652 type
= error_mark_node
;
26655 else if (nested_name_specifier
)
26659 if (bad_template_keyword
)
26660 /* [temp.names]: in a qualified-id formed by a class-head-name, the
26661 keyword template shall not appear at the top level. */
26662 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
26663 "keyword %<template%> not allowed in class-head-name");
26665 /* Reject typedef-names in class heads. */
26666 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
26668 error_at (type_start_token
->location
,
26669 "invalid class name in declaration of %qD",
26675 /* Figure out in what scope the declaration is being placed. */
26676 scope
= current_scope ();
26677 /* If that scope does not contain the scope in which the
26678 class was originally declared, the program is invalid. */
26679 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
26681 if (at_namespace_scope_p ())
26682 error_at (type_start_token
->location
,
26683 "declaration of %qD in namespace %qD which does not "
26685 type
, scope
, nested_name_specifier
);
26687 error_at (type_start_token
->location
,
26688 "declaration of %qD in %qD which does not enclose %qD",
26689 type
, scope
, nested_name_specifier
);
26695 A declarator-id shall not be qualified except for the
26696 definition of a ... nested class outside of its class
26697 ... [or] the definition or explicit instantiation of a
26698 class member of a namespace outside of its namespace. */
26699 if (scope
== nested_name_specifier
)
26700 permerror (nested_name_specifier_token_start
->location
,
26701 "extra qualification not allowed");
26703 /* An explicit-specialization must be preceded by "template <>". If
26704 it is not, try to recover gracefully. */
26705 if (at_namespace_scope_p ()
26706 && parser
->num_template_parameter_lists
== 0
26707 && !processing_template_parmlist
26710 /* Build a location of this form:
26711 struct typename <ARGS>
26712 ^~~~~~~~~~~~~~~~~~~~~~
26713 with caret==start at the start token, and
26714 finishing at the end of the type. */
26715 location_t reported_loc
26716 = make_location (class_head_start_location
,
26717 class_head_start_location
,
26718 get_finish (type_start_token
->location
));
26719 rich_location
richloc (line_table
, reported_loc
);
26720 richloc
.add_fixit_insert_before (class_head_start_location
,
26722 error_at (&richloc
,
26723 "an explicit specialization must be preceded by"
26724 " %<template <>%>");
26725 invalid_explicit_specialization_p
= true;
26726 /* Take the same action that would have been taken by
26727 cp_parser_explicit_specialization. */
26728 ++parser
->num_template_parameter_lists
;
26729 begin_specialization ();
26731 /* There must be no "return" statements between this point and the
26732 end of this function; set "type "to the correct return value and
26733 use "goto done;" to return. */
26734 /* Make sure that the right number of template parameters were
26736 if (!cp_parser_check_template_parameters (parser
, num_templates
,
26738 type_start_token
->location
,
26739 /*declarator=*/NULL
))
26741 /* If something went wrong, there is no point in even trying to
26742 process the class-definition. */
26747 /* Look up the type. */
26750 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
26751 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
26752 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
26754 error_at (type_start_token
->location
,
26755 "function template %qD redeclared as a class template", id
);
26756 type
= error_mark_node
;
26760 type
= TREE_TYPE (id
);
26761 type
= maybe_process_partial_specialization (type
);
26763 /* Check the scope while we still know whether or not we had a
26764 nested-name-specifier. */
26765 if (type
!= error_mark_node
)
26766 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
26768 if (nested_name_specifier
)
26769 pushed_scope
= push_scope (nested_name_specifier
);
26771 else if (nested_name_specifier
)
26773 type
= TREE_TYPE (type
);
26777 template <typename T> struct S { struct T };
26778 template <typename T> struct S<T>::T { };
26780 we will get a TYPENAME_TYPE when processing the definition of
26781 `S::T'. We need to resolve it to the actual type before we
26782 try to define it. */
26783 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26785 type
= resolve_typename_type (type
, /*only_current_p=*/false);
26786 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26788 cp_parser_error (parser
, "could not resolve typename type");
26789 type
= error_mark_node
;
26793 type
= maybe_process_partial_specialization (type
);
26794 if (type
== error_mark_node
)
26800 /* Enter the scope indicated by the nested-name-specifier. */
26801 pushed_scope
= push_scope (nested_name_specifier
);
26802 /* Get the canonical version of this type. */
26803 type
= TYPE_MAIN_DECL (type
);
26804 /* Call push_template_decl if it seems like we should be defining a
26805 template either from the template headers or the type we're
26806 defining, so that we diagnose both extra and missing headers. */
26807 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26808 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
26809 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
26811 type
= push_template_decl (type
);
26812 if (type
== error_mark_node
)
26819 type
= TREE_TYPE (type
);
26820 *nested_name_specifier_p
= true;
26822 else /* The name is not a nested name. */
26824 /* If the class was unnamed, create a dummy name. */
26826 id
= make_anon_name ();
26827 TAG_how how
= (parser
->in_type_id_in_expr_p
26828 ? TAG_how::INNERMOST_NON_CLASS
26829 : TAG_how::CURRENT_ONLY
);
26830 type
= xref_tag (class_key
, id
, how
,
26831 parser
->num_template_parameter_lists
);
26834 /* Diagnose class/struct/union mismatches. */
26835 cp_parser_check_class_key (parser
, UNKNOWN_LOCATION
, class_key
, type
,
26838 /* Indicate whether this class was declared as a `class' or as a
26840 if (TREE_CODE (type
) == RECORD_TYPE
)
26841 CLASSTYPE_DECLARED_CLASS (type
) = class_key
== class_type
;
26843 /* If this type was already complete, and we see another definition,
26844 that's an error. Likewise if the type is already being defined:
26845 this can happen, eg, when it's defined from within an expression
26847 if (type
!= error_mark_node
26848 && (COMPLETE_TYPE_P (type
) || TYPE_BEING_DEFINED (type
)))
26850 error_at (type_start_token
->location
, "redefinition of %q#T",
26852 inform (location_of (type
), "previous definition of %q#T",
26857 else if (type
== error_mark_node
)
26862 if (current_lambda_expr ()
26863 && uses_parameter_packs (attributes
))
26865 /* In a lambda this should work, but doesn't currently. */
26866 sorry ("unexpanded parameter pack in local class in lambda");
26867 attributes
= NULL_TREE
;
26870 /* Apply attributes now, before any use of the class as a template
26871 argument in its base list. */
26872 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
26873 fixup_attribute_variants (type
);
26876 /* Associate constraints with the type. */
26878 type
= associate_classtype_constraints (type
);
26880 /* We will have entered the scope containing the class; the names of
26881 base classes should be looked up in that context. For example:
26883 struct A { struct B {}; struct C; };
26884 struct A::C : B {};
26888 /* Get the list of base-classes, if there is one. Defer access checking
26889 until the entire list has been seen, as per [class.access.general]. */
26890 push_deferring_access_checks (dk_deferred
);
26891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
26895 bases
= cp_parser_base_clause (parser
);
26902 /* If we're really defining a class, process the base classes.
26903 If they're invalid, fail. */
26904 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26905 xref_basetypes (type
, bases
);
26907 /* Now that all bases have been seen and attached to the class, check
26908 accessibility of the types named in the base-clause. This must be
26909 done relative to the class scope, so that we accept e.g.
26911 struct A { protected: struct B {}; };
26912 struct C : A::B, A {}; // OK: A::B is accessible via base A
26914 as per [class.access.general]. */
26917 pop_to_parent_deferring_access_checks ();
26922 /* Leave the scope given by the nested-name-specifier. We will
26923 enter the class scope itself while processing the members. */
26925 pop_scope (pushed_scope
);
26927 if (invalid_explicit_specialization_p
)
26929 end_specialization ();
26930 --parser
->num_template_parameter_lists
;
26934 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
26935 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
26936 CLASSTYPE_FINAL (type
) = 1;
26938 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
26942 /* Parse a class-key.
26949 Returns the kind of class-key specified, or none_type to indicate
26952 static enum tag_types
26953 cp_parser_class_key (cp_parser
* parser
)
26956 enum tag_types tag_type
;
26958 /* Look for the class-key. */
26959 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
26963 /* Check to see if the TOKEN is a class-key. */
26964 tag_type
= cp_parser_token_is_class_key (token
);
26966 cp_parser_error (parser
, "expected class-key");
26970 /* Parse a type-parameter-key.
26972 type-parameter-key:
26978 cp_parser_type_parameter_key (cp_parser
* parser
)
26980 /* Look for the type-parameter-key. */
26981 enum tag_types tag_type
= none_type
;
26982 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26983 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
26985 cp_lexer_consume_token (parser
->lexer
);
26986 if (pedantic
&& tag_type
== typename_type
26987 && cxx_dialect
< cxx17
)
26988 /* typename is not allowed in a template template parameter
26989 by the standard until C++17. */
26990 pedwarn (token
->location
, OPT_Wc__17_extensions
,
26991 "ISO C++ forbids typename key in template template parameter;"
26992 " use %<-std=c++17%> or %<-std=gnu++17%>");
26995 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
27000 /* Parse an (optional) member-specification.
27002 member-specification:
27003 member-declaration member-specification [opt]
27004 access-specifier : member-specification [opt] */
27007 cp_parser_member_specification_opt (cp_parser
* parser
)
27014 /* Peek at the next token. */
27015 token
= cp_lexer_peek_token (parser
->lexer
);
27016 /* If it's a `}', or EOF then we've seen all the members. */
27017 if (token
->type
== CPP_CLOSE_BRACE
27018 || token
->type
== CPP_EOF
27019 || token
->type
== CPP_PRAGMA_EOL
)
27022 /* See if this token is a keyword. */
27023 keyword
= token
->keyword
;
27027 case RID_PROTECTED
:
27029 /* Consume the access-specifier. */
27030 cp_lexer_consume_token (parser
->lexer
);
27031 /* Remember which access-specifier is active. */
27032 current_access_specifier
= token
->u
.value
;
27033 /* Look for the `:'. */
27034 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27038 /* Accept #pragmas at class scope. */
27039 if (token
->type
== CPP_PRAGMA
)
27041 cp_parser_pragma (parser
, pragma_member
, NULL
);
27045 /* Otherwise, the next construction must be a
27046 member-declaration. */
27047 cp_parser_member_declaration (parser
);
27052 /* Parse a member-declaration.
27054 member-declaration:
27055 decl-specifier-seq [opt] member-declarator-list [opt] ;
27056 function-definition ; [opt]
27057 :: [opt] nested-name-specifier template [opt] unqualified-id ;
27059 template-declaration
27062 member-declarator-list:
27064 member-declarator-list , member-declarator
27067 declarator pure-specifier [opt]
27068 declarator constant-initializer [opt]
27069 identifier [opt] : constant-expression
27073 member-declaration:
27074 __extension__ member-declaration
27077 declarator attributes [opt] pure-specifier [opt]
27078 declarator attributes [opt] constant-initializer [opt]
27079 identifier [opt] attributes [opt] : constant-expression
27083 member-declaration:
27084 static_assert-declaration */
27087 cp_parser_member_declaration (cp_parser
* parser
)
27089 cp_decl_specifier_seq decl_specifiers
;
27090 tree prefix_attributes
;
27092 int declares_class_or_enum
;
27094 cp_token
*token
= NULL
;
27095 cp_token
*decl_spec_token_start
= NULL
;
27096 cp_token
*initializer_token_start
= NULL
;
27097 int saved_pedantic
;
27098 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27100 /* Check for the `__extension__' keyword. */
27101 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
27104 cp_parser_member_declaration (parser
);
27105 /* Restore the old value of the PEDANTIC flag. */
27106 pedantic
= saved_pedantic
;
27111 /* Check for a template-declaration. */
27112 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27114 /* An explicit specialization here is an error condition, and we
27115 expect the specialization handler to detect and report this. */
27116 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
27117 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
27118 cp_parser_explicit_specialization (parser
);
27120 cp_parser_template_declaration (parser
, /*member_p=*/true);
27124 /* Check for a template introduction. */
27125 else if (cp_parser_template_declaration_after_export (parser
, true))
27128 /* Check for a using-declaration. */
27129 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27131 if (cxx_dialect
< cxx11
)
27132 /* Parse the using-declaration. */
27133 cp_parser_using_declaration (parser
, /*access_declaration_p=*/false);
27134 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_ENUM
))
27135 cp_parser_using_enum (parser
);
27139 bool alias_decl_expected
;
27140 cp_parser_parse_tentatively (parser
);
27141 decl
= cp_parser_alias_declaration (parser
);
27142 /* Note that if we actually see the '=' token after the
27143 identifier, cp_parser_alias_declaration commits the
27144 tentative parse. In that case, we really expect an
27145 alias-declaration. Otherwise, we expect a using
27147 alias_decl_expected
=
27148 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
27149 cp_parser_parse_definitely (parser
);
27151 if (alias_decl_expected
)
27152 finish_member_declaration (decl
);
27154 cp_parser_using_declaration (parser
,
27155 /*access_declaration_p=*/false);
27160 /* Check for @defs. */
27161 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
27164 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
27165 ivar
= ivar_chains
;
27169 ivar
= TREE_CHAIN (member
);
27170 TREE_CHAIN (member
) = NULL_TREE
;
27171 finish_member_declaration (member
);
27176 /* If the next token is `static_assert' we have a static assertion. */
27177 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
27179 cp_parser_static_assert (parser
, /*member_p=*/true);
27183 parser
->colon_corrects_to_scope_p
= false;
27185 cp_omp_declare_simd_data odsd
;
27186 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
27189 /* Parse the decl-specifier-seq. */
27190 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27191 cp_parser_decl_specifier_seq (parser
,
27192 (CP_PARSER_FLAGS_OPTIONAL
27193 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
27195 &declares_class_or_enum
);
27197 if (decl_specifiers
.attributes
&& (flag_openmp
|| flag_openmp_simd
))
27198 cp_parser_handle_directive_omp_attributes (parser
,
27199 &decl_specifiers
.attributes
,
27202 /* Check for an invalid type-name. */
27203 if (!decl_specifiers
.any_type_specifiers_p
27204 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27206 /* If there is no declarator, then the decl-specifier-seq should
27208 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27210 /* If there was no decl-specifier-seq, and the next token is a
27211 `;', then we have something like:
27217 Each member-declaration shall declare at least one member
27218 name of the class. */
27219 if (!decl_specifiers
.any_specifiers_p
)
27221 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27222 if (!in_system_header_at (token
->location
))
27224 gcc_rich_location
richloc (token
->location
);
27225 richloc
.add_fixit_remove ();
27226 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
27231 /* See if this declaration is a friend. */
27232 friend_p
= cp_parser_friend_p (&decl_specifiers
);
27233 /* If there were decl-specifiers, check to see if there was
27234 a class-declaration. */
27235 tree type
= check_tag_decl (&decl_specifiers
,
27236 /*explicit_type_instantiation_p=*/false);
27237 /* Nested classes have already been added to the class, but
27238 a `friend' needs to be explicitly registered. */
27241 /* If the `friend' keyword was present, the friend must
27242 be introduced with a class-key. */
27243 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
27244 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
27245 "in C++03 a class-key must be used "
27246 "when declaring a friend");
27249 template <typename T> struct A {
27250 friend struct A<T>::B;
27253 A<T>::B will be represented by a TYPENAME_TYPE, and
27254 therefore not recognized by check_tag_decl. */
27257 type
= decl_specifiers
.type
;
27258 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
27259 type
= TREE_TYPE (type
);
27261 /* Warn if an attribute cannot appear here, as per
27262 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
27263 we ignore attributes in elaborated-type-specifiers. */
27264 if (!declares_class_or_enum
27265 && cxx11_attribute_p (decl_specifiers
.attributes
))
27267 decl_specifiers
.attributes
= NULL_TREE
;
27268 if (warning_at (decl_spec_token_start
->location
,
27269 OPT_Wattributes
, "attribute ignored"))
27270 inform (decl_spec_token_start
->location
, "an attribute "
27271 "that appertains to a friend declaration that "
27272 "is not a definition is ignored");
27274 if (!type
|| !TYPE_P (type
))
27275 error_at (decl_spec_token_start
->location
,
27276 "friend declaration does not name a class or "
27279 make_friend_class (current_class_type
, type
,
27280 /*complain=*/true);
27282 /* If there is no TYPE, an error message will already have
27284 else if (!type
|| type
== error_mark_node
)
27286 /* An anonymous aggregate has to be handled specially; such
27287 a declaration really declares a data member (with a
27288 particular type), as opposed to a nested class. */
27289 else if (ANON_AGGR_TYPE_P (type
))
27292 if (decl_specifiers
.storage_class
!= sc_none
)
27293 error_at (decl_spec_token_start
->location
,
27294 "a storage class on an anonymous aggregate "
27295 "in class scope is not allowed");
27297 /* Remove constructors and such from TYPE, now that we
27298 know it is an anonymous aggregate. */
27299 fixup_anonymous_aggr (type
);
27300 /* And make the corresponding data member. */
27301 decl
= build_decl (decl_spec_token_start
->location
,
27302 FIELD_DECL
, NULL_TREE
, type
);
27303 /* Add it to the class. */
27304 finish_member_declaration (decl
);
27307 cp_parser_check_access_in_redeclaration
27309 decl_spec_token_start
->location
);
27314 bool assume_semicolon
= false;
27316 /* Clear attributes from the decl_specifiers but keep them
27317 around as prefix attributes that apply them to the entity
27319 prefix_attributes
= decl_specifiers
.attributes
;
27320 decl_specifiers
.attributes
= NULL_TREE
;
27321 if (parser
->omp_declare_simd
27322 && (parser
->omp_declare_simd
->attribs
[0]
27323 == &decl_specifiers
.attributes
))
27324 parser
->omp_declare_simd
->attribs
[0] = &prefix_attributes
;
27326 /* See if these declarations will be friends. */
27327 friend_p
= cp_parser_friend_p (&decl_specifiers
);
27329 /* Keep going until we hit the `;' at the end of the
27331 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27333 tree attributes
= NULL_TREE
;
27334 tree first_attribute
;
27336 bool named_bitfld
= false;
27338 /* Peek at the next token. */
27339 token
= cp_lexer_peek_token (parser
->lexer
);
27341 /* The following code wants to know early if it is a bit-field
27342 or some other declaration. Attributes can appear before
27343 the `:' token. Skip over them without consuming any tokens
27344 to peek if they are followed by `:'. */
27345 if (cp_next_tokens_can_be_attribute_p (parser
)
27346 || (token
->type
== CPP_NAME
27347 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
27348 && (named_bitfld
= true)))
27351 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
27352 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
27355 /* Check for a bitfield declaration. */
27356 if (token
->type
== CPP_COLON
27357 || (token
->type
== CPP_NAME
27358 && token
== cp_lexer_peek_token (parser
->lexer
)
27359 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
27360 && (named_bitfld
= true)))
27364 tree late_attributes
= NULL_TREE
;
27365 location_t id_location
27366 = cp_lexer_peek_token (parser
->lexer
)->location
;
27369 identifier
= cp_parser_identifier (parser
);
27371 identifier
= NULL_TREE
;
27373 /* Look for attributes that apply to the bitfield. */
27374 attributes
= cp_parser_attributes_opt (parser
);
27376 /* Consume the `:' token. */
27377 cp_lexer_consume_token (parser
->lexer
);
27379 /* Get the width of the bitfield. */
27380 width
= cp_parser_constant_expression (parser
, false, NULL
,
27381 cxx_dialect
>= cxx11
);
27383 /* In C++20 and as extension for C++11 and above we allow
27384 default member initializers for bit-fields. */
27385 initializer
= NULL_TREE
;
27386 if (cxx_dialect
>= cxx11
27387 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
27388 || cp_lexer_next_token_is (parser
->lexer
,
27392 = cp_lexer_peek_token (parser
->lexer
)->location
;
27393 if (cxx_dialect
< cxx20
27394 && identifier
!= NULL_TREE
)
27395 pedwarn (loc
, OPT_Wc__20_extensions
,
27396 "default member initializers for bit-fields "
27397 "only available with %<-std=c++20%> or "
27398 "%<-std=gnu++20%>");
27400 initializer
= cp_parser_save_nsdmi (parser
);
27401 if (identifier
== NULL_TREE
)
27403 error_at (loc
, "default member initializer for "
27404 "unnamed bit-field");
27405 initializer
= NULL_TREE
;
27410 /* Look for attributes that apply to the bitfield after
27411 the `:' token and width. This is where GCC used to
27412 parse attributes in the past, pedwarn if there is
27413 a std attribute. */
27414 if (cp_next_tokens_can_be_std_attribute_p (parser
))
27415 pedwarn (input_location
, OPT_Wpedantic
,
27416 "ISO C++ allows bit-field attributes only "
27417 "before the %<:%> token");
27419 late_attributes
= cp_parser_attributes_opt (parser
);
27422 attributes
= attr_chainon (attributes
, late_attributes
);
27424 /* Remember which attributes are prefix attributes and
27426 first_attribute
= attributes
;
27427 /* Combine the attributes. */
27428 attributes
= attr_chainon (prefix_attributes
, attributes
);
27430 /* Create the bitfield declaration. */
27431 decl
= grokbitfield (identifier
27432 ? make_id_declarator (NULL_TREE
,
27438 width
, initializer
,
27443 cp_declarator
*declarator
;
27444 tree asm_specification
;
27445 int ctor_dtor_or_conv_p
;
27446 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
27447 cp_parser_flags flags
= CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
27448 /* We can't delay parsing for friends,
27449 alias-declarations, and typedefs, even though the
27450 standard seems to require it. */
27452 && !decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27453 flags
|= CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
27455 /* Parse the declarator. */
27457 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
27459 &ctor_dtor_or_conv_p
,
27460 /*parenthesized_p=*/NULL
,
27462 friend_p
, static_p
);
27464 /* If something went wrong parsing the declarator, make sure
27465 that we at least consume some tokens. */
27466 if (declarator
== cp_error_declarator
)
27468 /* Skip to the end of the statement. */
27469 cp_parser_skip_to_end_of_statement (parser
);
27470 /* If the next token is not a semicolon, that is
27471 probably because we just skipped over the body of
27472 a function. So, we consume a semicolon if
27473 present, but do not issue an error message if it
27475 if (cp_lexer_next_token_is (parser
->lexer
,
27477 cp_lexer_consume_token (parser
->lexer
);
27481 /* Handle class-scope non-template C++17 deduction guides. */
27482 cp_parser_maybe_adjust_declarator_for_dguide (parser
,
27485 &ctor_dtor_or_conv_p
);
27487 if (declares_class_or_enum
& 2)
27488 cp_parser_check_for_definition_in_return_type
27489 (declarator
, decl_specifiers
.type
,
27490 decl_specifiers
.locations
[ds_type_spec
]);
27492 /* Look for an asm-specification. */
27493 asm_specification
= cp_parser_asm_specification_opt (parser
);
27494 /* Look for attributes that apply to the declaration. */
27495 attributes
= cp_parser_attributes_opt (parser
);
27496 /* Remember which attributes are prefix attributes and
27498 first_attribute
= attributes
;
27499 /* Combine the attributes. */
27500 attributes
= attr_chainon (prefix_attributes
, attributes
);
27502 /* If it's an `=', then we have a constant-initializer or a
27503 pure-specifier. It is not correct to parse the
27504 initializer before registering the member declaration
27505 since the member declaration should be in scope while
27506 its initializer is processed. However, the rest of the
27507 front end does not yet provide an interface that allows
27508 us to handle this correctly. */
27509 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
27513 A pure-specifier shall be used only in the declaration of
27514 a virtual function.
27516 A member-declarator can contain a constant-initializer
27517 only if it declares a static member of integral or
27520 Therefore, if the DECLARATOR is for a function, we look
27521 for a pure-specifier; otherwise, we look for a
27522 constant-initializer. When we call `grokfield', it will
27523 perform more stringent semantics checks. */
27524 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
27525 declarator
->init_loc
= initializer_token_start
->location
;
27526 if (function_declarator_p (declarator
)
27527 || (decl_specifiers
.type
27528 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
27529 && declarator
->kind
== cdk_id
27530 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
27531 == FUNCTION_TYPE
)))
27532 initializer
= cp_parser_pure_specifier (parser
);
27533 else if (decl_specifiers
.storage_class
!= sc_static
)
27534 initializer
= cp_parser_save_nsdmi (parser
);
27535 else if (cxx_dialect
>= cxx11
)
27538 /* Don't require a constant rvalue in C++11, since we
27539 might want a reference constant. We'll enforce
27540 constancy later. */
27541 cp_lexer_consume_token (parser
->lexer
);
27542 /* Parse the initializer. */
27543 initializer
= cp_parser_initializer_clause (parser
,
27547 /* Parse the initializer. */
27548 initializer
= cp_parser_constant_initializer (parser
);
27550 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
27551 && !function_declarator_p (declarator
))
27554 declarator
->init_loc
27555 = cp_lexer_peek_token (parser
->lexer
)->location
;
27556 if (decl_specifiers
.storage_class
!= sc_static
)
27557 initializer
= cp_parser_save_nsdmi (parser
);
27559 initializer
= cp_parser_initializer (parser
, &x
, &x
);
27561 /* Detect invalid bit-field cases such as
27567 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
27568 /* If there were no type specifiers, it was a
27570 && decl_specifiers
.any_type_specifiers_p
)
27572 /* This is called for a decent diagnostic only. */
27573 tree d
= grokdeclarator (declarator
, &decl_specifiers
,
27574 BITFIELD
, /*initialized=*/false,
27576 if (!error_operand_p (d
))
27577 error_at (DECL_SOURCE_LOCATION (d
),
27578 "bit-field %qD has non-integral type %qT",
27580 cp_parser_skip_to_end_of_statement (parser
);
27581 /* Avoid "extra ;" pedwarns. */
27582 if (cp_lexer_next_token_is (parser
->lexer
,
27584 cp_lexer_consume_token (parser
->lexer
);
27587 /* Otherwise, there is no initializer. */
27589 initializer
= NULL_TREE
;
27591 /* See if we are probably looking at a function
27592 definition. We are certainly not looking at a
27593 member-declarator. Calling `grokfield' has
27594 side-effects, so we must not do it unless we are sure
27595 that we are looking at a member-declarator. */
27596 if (cp_parser_token_starts_function_definition_p
27597 (cp_lexer_peek_token (parser
->lexer
)))
27599 /* The grammar does not allow a pure-specifier to be
27600 used when a member function is defined. (It is
27601 possible that this fact is an oversight in the
27602 standard, since a pure function may be defined
27603 outside of the class-specifier. */
27604 if (initializer
&& initializer_token_start
)
27605 error_at (initializer_token_start
->location
,
27606 "pure-specifier on function-definition");
27607 decl
= cp_parser_save_member_function_body (parser
,
27611 if (parser
->fully_implicit_function_template_p
)
27612 decl
= finish_fully_implicit_template (parser
, decl
);
27613 /* If the member was not a friend, declare it here. */
27615 finish_member_declaration (decl
);
27616 /* Peek at the next token. */
27617 token
= cp_lexer_peek_token (parser
->lexer
);
27618 /* If the next token is a semicolon, consume it. */
27619 if (token
->type
== CPP_SEMICOLON
)
27621 location_t semicolon_loc
27622 = cp_lexer_consume_token (parser
->lexer
)->location
;
27623 gcc_rich_location
richloc (semicolon_loc
);
27624 richloc
.add_fixit_remove ();
27625 warning_at (&richloc
, OPT_Wextra_semi
,
27626 "extra %<;%> after in-class "
27627 "function definition");
27632 if (declarator
->kind
== cdk_function
)
27633 declarator
->id_loc
= token
->location
;
27634 /* Create the declaration. */
27635 decl
= grokfield (declarator
, &decl_specifiers
,
27636 initializer
, /*init_const_expr_p=*/true,
27637 asm_specification
, attributes
);
27638 if (parser
->fully_implicit_function_template_p
)
27641 finish_fully_implicit_template (parser
, 0);
27643 decl
= finish_fully_implicit_template (parser
, decl
);
27647 cp_finalize_omp_declare_simd (parser
, decl
);
27648 cp_finalize_oacc_routine (parser
, decl
, false);
27650 /* Reset PREFIX_ATTRIBUTES. */
27651 if (attributes
!= error_mark_node
)
27653 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
27654 attributes
= TREE_CHAIN (attributes
);
27656 TREE_CHAIN (attributes
) = NULL_TREE
;
27659 /* If there is any qualification still in effect, clear it
27660 now; we will be starting fresh with the next declarator. */
27661 parser
->scope
= NULL_TREE
;
27662 parser
->qualifying_scope
= NULL_TREE
;
27663 parser
->object_scope
= NULL_TREE
;
27664 /* If it's a `,', then there are more declarators. */
27665 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27667 cp_lexer_consume_token (parser
->lexer
);
27668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27670 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
27671 gcc_rich_location
richloc (token
->location
);
27672 richloc
.add_fixit_remove ();
27673 error_at (&richloc
, "stray %<,%> at end of "
27674 "member declaration");
27677 /* If the next token isn't a `;', then we have a parse error. */
27678 else if (cp_lexer_next_token_is_not (parser
->lexer
,
27681 /* The next token might be a ways away from where the
27682 actual semicolon is missing. Find the previous token
27683 and use that for our error position. */
27684 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
27685 gcc_rich_location
richloc (token
->location
);
27686 richloc
.add_fixit_insert_after (";");
27687 error_at (&richloc
, "expected %<;%> at end of "
27688 "member declaration");
27690 /* Assume that the user meant to provide a semicolon. If
27691 we were to cp_parser_skip_to_end_of_statement, we might
27692 skip to a semicolon inside a member function definition
27693 and issue nonsensical error messages. */
27694 assume_semicolon
= true;
27699 /* Add DECL to the list of members. */
27701 /* Explicitly include, eg, NSDMIs, for better error
27702 recovery (c++/58650). */
27703 || !DECL_DECLARES_FUNCTION_P (decl
))
27704 finish_member_declaration (decl
);
27706 if (DECL_DECLARES_FUNCTION_P (decl
))
27707 cp_parser_save_default_args (parser
, STRIP_TEMPLATE (decl
));
27708 else if (TREE_CODE (decl
) == FIELD_DECL
27709 && DECL_INITIAL (decl
))
27710 /* Add DECL to the queue of NSDMI to be parsed later. */
27711 vec_safe_push (unparsed_nsdmis
, decl
);
27714 if (assume_semicolon
)
27719 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27721 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27722 cp_finalize_omp_declare_simd (parser
, &odsd
);
27725 /* Parse a pure-specifier.
27730 Returns INTEGER_ZERO_NODE if a pure specifier is found.
27731 Otherwise, ERROR_MARK_NODE is returned. */
27734 cp_parser_pure_specifier (cp_parser
* parser
)
27738 /* Look for the `=' token. */
27739 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
27740 return error_mark_node
;
27741 /* Look for the `0' token. */
27742 token
= cp_lexer_peek_token (parser
->lexer
);
27744 if (token
->type
== CPP_EOF
27745 || token
->type
== CPP_PRAGMA_EOL
)
27746 return error_mark_node
;
27748 cp_lexer_consume_token (parser
->lexer
);
27750 /* Accept = default or = delete in c++0x mode. */
27751 if (token
->keyword
== RID_DEFAULT
27752 || token
->keyword
== RID_DELETE
)
27754 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
27755 return token
->u
.value
;
27758 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
27759 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
27761 cp_parser_error (parser
,
27762 "invalid pure specifier (only %<= 0%> is allowed)");
27763 cp_parser_skip_to_end_of_statement (parser
);
27764 return error_mark_node
;
27766 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27768 error_at (token
->location
, "templates may not be %<virtual%>");
27769 return error_mark_node
;
27772 return integer_zero_node
;
27775 /* Parse a constant-initializer.
27777 constant-initializer:
27778 = constant-expression
27780 Returns a representation of the constant-expression. */
27783 cp_parser_constant_initializer (cp_parser
* parser
)
27785 /* Look for the `=' token. */
27786 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
27787 return error_mark_node
;
27789 /* It is invalid to write:
27791 struct S { static const int i = { 7 }; };
27794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27796 cp_parser_error (parser
,
27797 "a brace-enclosed initializer is not allowed here");
27798 /* Consume the opening brace. */
27799 matching_braces braces
;
27800 braces
.consume_open (parser
);
27801 /* Skip the initializer. */
27802 cp_parser_skip_to_closing_brace (parser
);
27803 /* Look for the trailing `}'. */
27804 braces
.require_close (parser
);
27806 return error_mark_node
;
27809 return cp_parser_constant_expression (parser
);
27812 /* Derived classes [gram.class.derived] */
27814 /* Parse a base-clause.
27817 : base-specifier-list
27819 base-specifier-list:
27820 base-specifier ... [opt]
27821 base-specifier-list , base-specifier ... [opt]
27823 Returns a TREE_LIST representing the base-classes, in the order in
27824 which they were declared. The representation of each node is as
27825 described by cp_parser_base_specifier.
27827 In the case that no bases are specified, this function will return
27828 NULL_TREE, not ERROR_MARK_NODE. */
27831 cp_parser_base_clause (cp_parser
* parser
)
27833 tree bases
= NULL_TREE
;
27835 /* Look for the `:' that begins the list. */
27836 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27838 /* Scan the base-specifier-list. */
27843 bool pack_expansion_p
= false;
27845 /* Look for the base-specifier. */
27846 base
= cp_parser_base_specifier (parser
);
27847 /* Look for the (optional) ellipsis. */
27848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27850 /* Consume the `...'. */
27851 cp_lexer_consume_token (parser
->lexer
);
27853 pack_expansion_p
= true;
27856 /* Add BASE to the front of the list. */
27857 if (base
&& base
!= error_mark_node
)
27859 if (pack_expansion_p
)
27860 /* Make this a pack expansion type. */
27861 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
27863 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
27865 TREE_CHAIN (base
) = bases
;
27869 /* Peek at the next token. */
27870 token
= cp_lexer_peek_token (parser
->lexer
);
27871 /* If it's not a comma, then the list is complete. */
27872 if (token
->type
!= CPP_COMMA
)
27874 /* Consume the `,'. */
27875 cp_lexer_consume_token (parser
->lexer
);
27878 /* PARSER->SCOPE may still be non-NULL at this point, if the last
27879 base class had a qualified name. However, the next name that
27880 appears is certainly not qualified. */
27881 parser
->scope
= NULL_TREE
;
27882 parser
->qualifying_scope
= NULL_TREE
;
27883 parser
->object_scope
= NULL_TREE
;
27885 return nreverse (bases
);
27888 /* Parse a base-specifier.
27891 :: [opt] nested-name-specifier [opt] class-name
27892 virtual access-specifier [opt] :: [opt] nested-name-specifier
27894 access-specifier virtual [opt] :: [opt] nested-name-specifier
27897 Returns a TREE_LIST. The TREE_PURPOSE will be one of
27898 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
27899 indicate the specifiers provided. The TREE_VALUE will be a TYPE
27900 (or the ERROR_MARK_NODE) indicating the type that was specified. */
27903 cp_parser_base_specifier (cp_parser
* parser
)
27907 bool virtual_p
= false;
27908 bool duplicate_virtual_error_issued_p
= false;
27909 bool duplicate_access_error_issued_p
= false;
27910 bool class_scope_p
, template_p
;
27911 tree access
= access_default_node
;
27914 /* Process the optional `virtual' and `access-specifier'. */
27917 /* Peek at the next token. */
27918 token
= cp_lexer_peek_token (parser
->lexer
);
27919 /* Process `virtual'. */
27920 switch (token
->keyword
)
27923 /* If `virtual' appears more than once, issue an error. */
27924 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
27926 cp_parser_error (parser
,
27927 "%<virtual%> specified more than once in base-specifier");
27928 duplicate_virtual_error_issued_p
= true;
27933 /* Consume the `virtual' token. */
27934 cp_lexer_consume_token (parser
->lexer
);
27939 case RID_PROTECTED
:
27941 /* If more than one access specifier appears, issue an
27943 if (access
!= access_default_node
27944 && !duplicate_access_error_issued_p
)
27946 cp_parser_error (parser
,
27947 "more than one access specifier in base-specifier");
27948 duplicate_access_error_issued_p
= true;
27951 access
= ridpointers
[(int) token
->keyword
];
27953 /* Consume the access-specifier. */
27954 cp_lexer_consume_token (parser
->lexer
);
27963 /* It is not uncommon to see programs mechanically, erroneously, use
27964 the 'typename' keyword to denote (dependent) qualified types
27965 as base classes. */
27966 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
27968 token
= cp_lexer_peek_token (parser
->lexer
);
27969 if (!processing_template_decl
)
27970 error_at (token
->location
,
27971 "keyword %<typename%> not allowed outside of templates");
27973 error_at (token
->location
,
27974 "keyword %<typename%> not allowed in this context "
27975 "(the base class is implicitly a type)");
27976 cp_lexer_consume_token (parser
->lexer
);
27979 /* Look for the optional `::' operator. */
27980 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
27981 /* Look for the nested-name-specifier. The simplest way to
27986 The keyword `typename' is not permitted in a base-specifier or
27987 mem-initializer; in these contexts a qualified name that
27988 depends on a template-parameter is implicitly assumed to be a
27991 is to pretend that we have seen the `typename' keyword at this
27993 cp_parser_nested_name_specifier_opt (parser
,
27994 /*typename_keyword_p=*/true,
27995 /*check_dependency_p=*/true,
27997 /*is_declaration=*/true);
27998 /* If the base class is given by a qualified name, assume that names
27999 we see are type names or templates, as appropriate. */
28000 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
28001 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
28004 && cp_lexer_next_token_is_decltype (parser
->lexer
))
28005 /* DR 950 allows decltype as a base-specifier. */
28006 type
= cp_parser_decltype (parser
);
28009 /* Otherwise, look for the class-name. */
28010 type
= cp_parser_class_name (parser
,
28014 /*check_dependency_p=*/true,
28015 /*class_head_p=*/false,
28016 /*is_declaration=*/true);
28017 type
= TREE_TYPE (type
);
28020 if (type
== error_mark_node
)
28021 return error_mark_node
;
28023 return finish_base_specifier (type
, access
, virtual_p
);
28026 /* Exception handling [gram.exception] */
28028 /* Save the tokens that make up the noexcept-specifier for a member-function.
28029 Returns a DEFERRED_PARSE. */
28032 cp_parser_save_noexcept (cp_parser
*parser
)
28034 cp_token
*first
= parser
->lexer
->next_token
;
28035 /* We want everything up to, including, the final ')'. */
28036 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0);
28037 cp_token
*last
= parser
->lexer
->next_token
;
28039 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
28040 to carry the information we will need. */
28041 tree expr
= make_node (DEFERRED_PARSE
);
28042 /* Save away the noexcept-specifier; we will process it when the
28043 class is complete. */
28044 DEFPARSE_TOKENS (expr
) = cp_token_cache_new (first
, last
);
28045 DEFPARSE_INSTANTIATIONS (expr
) = nullptr;
28046 expr
= build_tree_list (expr
, NULL_TREE
);
28050 /* Used for late processing of noexcept-specifiers of member-functions.
28051 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
28052 we saved for later; parse it now. DECL is the declaration of the
28053 member function. */
28056 cp_parser_late_noexcept_specifier (cp_parser
*parser
, tree default_arg
)
28058 /* Make sure we've gotten something that hasn't been parsed yet. */
28059 gcc_assert (TREE_CODE (default_arg
) == DEFERRED_PARSE
);
28061 push_unparsed_function_queues (parser
);
28063 /* Push the saved tokens for the noexcept-specifier onto the parser's
28065 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_arg
);
28066 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28068 /* Parse the cached noexcept-specifier. */
28070 = cp_parser_noexcept_specification_opt (parser
,
28071 CP_PARSER_FLAGS_NONE
,
28072 /*require_constexpr=*/true,
28073 /*consumed_expr=*/NULL
,
28074 /*return_cond=*/false);
28076 /* Revert to the main lexer. */
28077 cp_parser_pop_lexer (parser
);
28079 /* Restore the queue. */
28080 pop_unparsed_function_queues (parser
);
28082 /* And we're done. */
28086 /* Perform late checking of overriding function with respect to their
28087 noexcept-specifiers. TYPE is the class and FNDECL is the function
28088 that potentially overrides some virtual function with the same
28092 noexcept_override_late_checks (tree type
, tree fndecl
)
28094 tree binfo
= TYPE_BINFO (type
);
28097 if (DECL_STATIC_FUNCTION_P (fndecl
))
28100 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
28102 tree basetype
= BINFO_TYPE (base_binfo
);
28104 if (!TYPE_POLYMORPHIC_P (basetype
))
28107 tree fn
= look_for_overrides_here (basetype
, fndecl
);
28109 maybe_check_overriding_exception_spec (fndecl
, fn
);
28113 /* Parse an (optional) noexcept-specification.
28115 noexcept-specification:
28116 noexcept ( constant-expression ) [opt]
28118 If no noexcept-specification is present, returns NULL_TREE.
28119 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28120 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28121 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28122 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28123 in which case a boolean condition is returned instead. The parser flags
28124 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28125 the (member) function is `const'. */
28128 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
28129 cp_parser_flags flags
,
28130 bool require_constexpr
,
28131 bool* consumed_expr
,
28135 const char *saved_message
;
28137 /* Peek at the next token. */
28138 token
= cp_lexer_peek_token (parser
->lexer
);
28140 /* Is it a noexcept-specification? */
28141 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
28145 /* [class.mem]/6 says that a noexcept-specifer (within the
28146 member-specification of the class) is a complete-class context of
28147 a class. So, if the noexcept-specifier has the optional expression,
28148 just save the tokens, and reparse this after we're done with the
28151 if ((flags
& CP_PARSER_FLAGS_DELAY_NOEXCEPT
)
28152 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
)
28153 /* No need to delay parsing for a number literal or true/false. */
28154 && !((cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NUMBER
)
28155 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
28156 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_PAREN
))
28157 && at_class_scope_p ()
28158 && TYPE_BEING_DEFINED (current_class_type
)
28159 && !LAMBDA_TYPE_P (current_class_type
))
28160 return cp_parser_save_noexcept (parser
);
28162 cp_lexer_consume_token (parser
->lexer
);
28164 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
28166 matching_parens parens
;
28167 parens
.consume_open (parser
);
28169 if (require_constexpr
)
28171 /* Types may not be defined in an exception-specification. */
28172 saved_message
= parser
->type_definition_forbidden_message
;
28173 parser
->type_definition_forbidden_message
28174 = G_("types may not be defined in an exception-specification");
28176 bool non_constant_p
;
28178 = cp_parser_constant_expression (parser
,
28179 /*allow_non_constant=*/true,
28182 && !require_potential_rvalue_constant_expression (expr
))
28185 return_cond
= true;
28188 /* Restore the saved message. */
28189 parser
->type_definition_forbidden_message
= saved_message
;
28193 expr
= cp_parser_expression (parser
);
28194 *consumed_expr
= true;
28197 parens
.require_close (parser
);
28201 expr
= boolean_true_node
;
28202 if (!require_constexpr
)
28203 *consumed_expr
= false;
28206 /* We cannot build a noexcept-spec right away because this will check
28207 that expr is a constexpr. */
28209 return build_noexcept_spec (expr
, tf_warning_or_error
);
28217 /* Parse an (optional) exception-specification.
28219 exception-specification:
28220 throw ( type-id-list [opt] )
28222 Returns a TREE_LIST representing the exception-specification. The
28223 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
28224 control parsing. QUALS are qualifiers indicating whether the (member)
28225 function is `const'. */
28228 cp_parser_exception_specification_opt (cp_parser
* parser
,
28229 cp_parser_flags flags
)
28233 const char *saved_message
;
28235 /* Peek at the next token. */
28236 token
= cp_lexer_peek_token (parser
->lexer
);
28238 /* Is it a noexcept-specification? */
28240 = cp_parser_noexcept_specification_opt (parser
, flags
,
28241 /*require_constexpr=*/true,
28242 /*consumed_expr=*/NULL
,
28243 /*return_cond=*/false);
28244 if (type_id_list
!= NULL_TREE
)
28245 return type_id_list
;
28247 /* If it's not `throw', then there's no exception-specification. */
28248 if (!cp_parser_is_keyword (token
, RID_THROW
))
28251 location_t loc
= token
->location
;
28253 /* Consume the `throw'. */
28254 cp_lexer_consume_token (parser
->lexer
);
28256 /* Look for the `('. */
28257 matching_parens parens
;
28258 parens
.require_open (parser
);
28260 /* Peek at the next token. */
28261 token
= cp_lexer_peek_token (parser
->lexer
);
28262 /* If it's not a `)', then there is a type-id-list. */
28263 if (token
->type
!= CPP_CLOSE_PAREN
)
28265 /* Types may not be defined in an exception-specification. */
28266 saved_message
= parser
->type_definition_forbidden_message
;
28267 parser
->type_definition_forbidden_message
28268 = G_("types may not be defined in an exception-specification");
28269 /* Parse the type-id-list. */
28270 type_id_list
= cp_parser_type_id_list (parser
);
28271 /* Restore the saved message. */
28272 parser
->type_definition_forbidden_message
= saved_message
;
28274 if (cxx_dialect
>= cxx17
)
28276 error_at (loc
, "ISO C++17 does not allow dynamic exception "
28278 type_id_list
= NULL_TREE
;
28280 else if (cxx_dialect
>= cxx11
)
28281 warning_at (loc
, OPT_Wdeprecated
,
28282 "dynamic exception specifications are deprecated in "
28285 /* In C++17, throw() is equivalent to noexcept (true). throw()
28286 is deprecated in C++11 and above as well, but is still widely used,
28287 so don't warn about it yet. */
28288 else if (cxx_dialect
>= cxx17
)
28289 type_id_list
= noexcept_true_spec
;
28291 type_id_list
= empty_except_spec
;
28293 /* Look for the `)'. */
28294 parens
.require_close (parser
);
28296 return type_id_list
;
28299 /* Parse an (optional) type-id-list.
28303 type-id-list , type-id ... [opt]
28305 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28306 in the order that the types were presented. */
28309 cp_parser_type_id_list (cp_parser
* parser
)
28311 tree types
= NULL_TREE
;
28318 token
= cp_lexer_peek_token (parser
->lexer
);
28320 /* Get the next type-id. */
28321 type
= cp_parser_type_id (parser
);
28322 /* Check for invalid 'auto'. */
28323 if (flag_concepts
&& type_uses_auto (type
))
28325 error_at (token
->location
,
28326 "invalid use of %<auto%> in exception-specification");
28327 type
= error_mark_node
;
28329 /* Parse the optional ellipsis. */
28330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28332 /* Consume the `...'. */
28333 cp_lexer_consume_token (parser
->lexer
);
28335 /* Turn the type into a pack expansion expression. */
28336 type
= make_pack_expansion (type
);
28338 /* Add it to the list. */
28339 types
= add_exception_specifier (types
, type
, /*complain=*/1);
28340 /* Peek at the next token. */
28341 token
= cp_lexer_peek_token (parser
->lexer
);
28342 /* If it is not a `,', we are done. */
28343 if (token
->type
!= CPP_COMMA
)
28345 /* Consume the `,'. */
28346 cp_lexer_consume_token (parser
->lexer
);
28349 return nreverse (types
);
28352 /* Parse a try-block.
28355 try compound-statement handler-seq */
28358 cp_parser_try_block (cp_parser
* parser
)
28362 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
28363 if (parser
->in_function_body
28364 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
28365 && cxx_dialect
< cxx20
)
28366 pedwarn (input_location
, OPT_Wc__20_extensions
,
28367 "%<try%> in %<constexpr%> function only "
28368 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28370 try_block
= begin_try_block ();
28371 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
28372 finish_try_block (try_block
);
28373 cp_parser_handler_seq (parser
);
28374 finish_handler_sequence (try_block
);
28379 /* Parse a function-try-block.
28381 function-try-block:
28382 try ctor-initializer [opt] function-body handler-seq */
28385 cp_parser_function_try_block (cp_parser
* parser
)
28387 tree compound_stmt
;
28390 /* Look for the `try' keyword. */
28391 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
28393 /* Let the rest of the front end know where we are. */
28394 try_block
= begin_function_try_block (&compound_stmt
);
28395 /* Parse the function-body. */
28396 cp_parser_ctor_initializer_opt_and_function_body
28397 (parser
, /*in_function_try_block=*/true);
28398 /* We're done with the `try' part. */
28399 finish_function_try_block (try_block
);
28400 /* Parse the handlers. */
28401 cp_parser_handler_seq (parser
);
28402 /* We're done with the handlers. */
28403 finish_function_handler_sequence (try_block
, compound_stmt
);
28406 /* Parse a handler-seq.
28409 handler handler-seq [opt] */
28412 cp_parser_handler_seq (cp_parser
* parser
)
28418 /* Parse the handler. */
28419 cp_parser_handler (parser
);
28420 /* Peek at the next token. */
28421 token
= cp_lexer_peek_token (parser
->lexer
);
28422 /* If it's not `catch' then there are no more handlers. */
28423 if (!cp_parser_is_keyword (token
, RID_CATCH
))
28428 /* Parse a handler.
28431 catch ( exception-declaration ) compound-statement */
28434 cp_parser_handler (cp_parser
* parser
)
28439 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
28440 handler
= begin_handler ();
28441 matching_parens parens
;
28442 parens
.require_open (parser
);
28443 declaration
= cp_parser_exception_declaration (parser
);
28444 finish_handler_parms (declaration
, handler
);
28445 parens
.require_close (parser
);
28446 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
28447 finish_handler (handler
);
28450 /* Parse an exception-declaration.
28452 exception-declaration:
28453 type-specifier-seq declarator
28454 type-specifier-seq abstract-declarator
28458 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28459 ellipsis variant is used. */
28462 cp_parser_exception_declaration (cp_parser
* parser
)
28464 cp_decl_specifier_seq type_specifiers
;
28465 cp_declarator
*declarator
;
28466 const char *saved_message
;
28468 /* If it's an ellipsis, it's easy to handle. */
28469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28471 /* Consume the `...' token. */
28472 cp_lexer_consume_token (parser
->lexer
);
28476 /* Types may not be defined in exception-declarations. */
28477 saved_message
= parser
->type_definition_forbidden_message
;
28478 parser
->type_definition_forbidden_message
28479 = G_("types may not be defined in exception-declarations");
28481 /* Parse the type-specifier-seq. */
28482 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
28483 /*is_declaration=*/true,
28484 /*is_trailing_return=*/false,
28486 /* If it's a `)', then there is no declarator. */
28487 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
28490 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
28491 CP_PARSER_FLAGS_NONE
,
28492 /*ctor_dtor_or_conv_p=*/NULL
,
28493 /*parenthesized_p=*/NULL
,
28494 /*member_p=*/false,
28495 /*friend_p=*/false,
28496 /*static_p=*/false);
28498 /* Restore the saved message. */
28499 parser
->type_definition_forbidden_message
= saved_message
;
28501 if (!type_specifiers
.any_specifiers_p
)
28502 return error_mark_node
;
28504 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
28507 /* Parse a throw-expression.
28510 throw assignment-expression [opt]
28512 Returns a THROW_EXPR representing the throw-expression. */
28515 cp_parser_throw_expression (cp_parser
* parser
)
28519 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28521 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
28522 token
= cp_lexer_peek_token (parser
->lexer
);
28523 /* Figure out whether or not there is an assignment-expression
28524 following the "throw" keyword. */
28525 if (token
->type
== CPP_COMMA
28526 || token
->type
== CPP_SEMICOLON
28527 || token
->type
== CPP_CLOSE_PAREN
28528 || token
->type
== CPP_CLOSE_SQUARE
28529 || token
->type
== CPP_CLOSE_BRACE
28530 || token
->type
== CPP_COLON
)
28531 expression
= NULL_TREE
;
28533 expression
= cp_parser_assignment_expression (parser
);
28535 /* Construct a location e.g.:
28538 with caret == start at the start of the "throw" token, and
28539 the end at the end of the final token we consumed. */
28540 location_t combined_loc
= make_location (start_loc
, start_loc
,
28542 expression
= build_throw (combined_loc
, expression
);
28547 /* Parse a yield-expression.
28550 co_yield assignment-expression
28551 co_yield braced-init-list
28553 Returns a CO_YIELD_EXPR representing the yield-expression. */
28556 cp_parser_yield_expression (cp_parser
* parser
)
28560 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28561 location_t kw_loc
= token
->location
; /* Save for later. */
28563 cp_parser_require_keyword (parser
, RID_CO_YIELD
, RT_CO_YIELD
);
28565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28567 bool expr_non_constant_p
;
28568 cp_lexer_set_source_position (parser
->lexer
);
28569 /* ??? : probably a moot point? */
28570 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28571 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
28574 expr
= cp_parser_assignment_expression (parser
);
28576 if (expr
== error_mark_node
)
28579 return finish_co_yield_expr (kw_loc
, expr
);
28582 /* GNU Extensions */
28584 /* Parse an (optional) asm-specification.
28587 asm ( string-literal )
28589 If the asm-specification is present, returns a STRING_CST
28590 corresponding to the string-literal. Otherwise, returns
28594 cp_parser_asm_specification_opt (cp_parser
* parser
)
28597 tree asm_specification
;
28599 /* Peek at the next token. */
28600 token
= cp_lexer_peek_token (parser
->lexer
);
28601 /* If the next token isn't the `asm' keyword, then there's no
28602 asm-specification. */
28603 if (!cp_parser_is_keyword (token
, RID_ASM
))
28606 /* Consume the `asm' token. */
28607 cp_lexer_consume_token (parser
->lexer
);
28608 /* Look for the `('. */
28609 matching_parens parens
;
28610 parens
.require_open (parser
);
28612 /* Look for the string-literal. */
28613 asm_specification
= cp_parser_string_literal (parser
, false, false);
28615 /* Look for the `)'. */
28616 parens
.require_close (parser
);
28618 return asm_specification
;
28621 /* Parse an asm-operand-list.
28625 asm-operand-list , asm-operand
28628 string-literal ( expression )
28629 [ string-literal ] string-literal ( expression )
28631 Returns a TREE_LIST representing the operands. The TREE_VALUE of
28632 each node is the expression. The TREE_PURPOSE is itself a
28633 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28634 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28635 is a STRING_CST for the string literal before the parenthesis. Returns
28636 ERROR_MARK_NODE if any of the operands are invalid. */
28639 cp_parser_asm_operand_list (cp_parser
* parser
)
28641 tree asm_operands
= NULL_TREE
;
28642 bool invalid_operands
= false;
28646 tree string_literal
;
28650 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
28652 /* Consume the `[' token. */
28653 cp_lexer_consume_token (parser
->lexer
);
28654 /* Read the operand name. */
28655 name
= cp_parser_identifier (parser
);
28656 if (name
!= error_mark_node
)
28657 name
= build_string (IDENTIFIER_LENGTH (name
),
28658 IDENTIFIER_POINTER (name
));
28659 /* Look for the closing `]'. */
28660 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
28664 /* Look for the string-literal. */
28665 string_literal
= cp_parser_string_literal (parser
, false, false);
28667 /* Look for the `('. */
28668 matching_parens parens
;
28669 parens
.require_open (parser
);
28670 /* Parse the expression. */
28671 expression
= cp_parser_expression (parser
);
28672 /* Look for the `)'. */
28673 parens
.require_close (parser
);
28675 if (name
== error_mark_node
28676 || string_literal
== error_mark_node
28677 || expression
== error_mark_node
)
28678 invalid_operands
= true;
28680 /* Add this operand to the list. */
28681 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
28684 /* If the next token is not a `,', there are no more
28686 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
28688 /* Consume the `,'. */
28689 cp_lexer_consume_token (parser
->lexer
);
28692 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
28695 /* Parse an asm-clobber-list.
28699 asm-clobber-list , string-literal
28701 Returns a TREE_LIST, indicating the clobbers in the order that they
28702 appeared. The TREE_VALUE of each node is a STRING_CST. */
28705 cp_parser_asm_clobber_list (cp_parser
* parser
)
28707 tree clobbers
= NULL_TREE
;
28711 tree string_literal
;
28713 /* Look for the string literal. */
28714 string_literal
= cp_parser_string_literal (parser
, false, false);
28715 /* Add it to the list. */
28716 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
28717 /* If the next token is not a `,', then the list is
28719 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
28721 /* Consume the `,' token. */
28722 cp_lexer_consume_token (parser
->lexer
);
28728 /* Parse an asm-label-list.
28732 asm-label-list , identifier
28734 Returns a TREE_LIST, indicating the labels in the order that they
28735 appeared. The TREE_VALUE of each node is a label. */
28738 cp_parser_asm_label_list (cp_parser
* parser
)
28740 tree labels
= NULL_TREE
;
28744 tree identifier
, label
, name
;
28746 /* Look for the identifier. */
28747 identifier
= cp_parser_identifier (parser
);
28748 if (!error_operand_p (identifier
))
28750 label
= lookup_label (identifier
);
28751 if (TREE_CODE (label
) == LABEL_DECL
)
28753 TREE_USED (label
) = 1;
28754 check_goto (label
);
28755 name
= build_string (IDENTIFIER_LENGTH (identifier
),
28756 IDENTIFIER_POINTER (identifier
));
28757 labels
= tree_cons (name
, label
, labels
);
28760 /* If the next token is not a `,', then the list is
28762 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
28764 /* Consume the `,' token. */
28765 cp_lexer_consume_token (parser
->lexer
);
28768 return nreverse (labels
);
28771 /* Return TRUE iff the next tokens in the stream are possibly the
28772 beginning of a GNU extension attribute. */
28775 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
28777 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
28780 /* Return TRUE iff the next tokens in the stream are possibly the
28781 beginning of a standard C++-11 attribute specifier. */
28784 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
28786 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
28789 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28790 beginning of a standard C++-11 attribute specifier. */
28793 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
28795 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28797 return (cxx_dialect
>= cxx11
28798 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
28799 || (token
->type
== CPP_OPEN_SQUARE
28800 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
28801 && token
->type
== CPP_OPEN_SQUARE
)));
28804 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28805 beginning of a GNU extension attribute. */
28808 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
28810 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28812 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
28815 /* Return true iff the next tokens can be the beginning of either a
28816 GNU attribute list, or a standard C++11 attribute sequence. */
28819 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
28821 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
28822 || cp_next_tokens_can_be_std_attribute_p (parser
));
28825 /* Return true iff the next Nth tokens can be the beginning of either
28826 a GNU attribute list, or a standard C++11 attribute sequence. */
28829 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
28831 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
28832 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
28835 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
28836 of GNU attributes, or return NULL. */
28839 cp_parser_attributes_opt (cp_parser
*parser
)
28841 tree attrs
= NULL_TREE
;
28844 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
28845 attrs
= attr_chainon (attrs
, cp_parser_gnu_attributes_opt (parser
));
28846 else if (cp_next_tokens_can_be_std_attribute_p (parser
))
28847 attrs
= attr_chainon (attrs
, cp_parser_std_attribute_spec_seq (parser
));
28854 /* Parse an (optional) series of attributes.
28857 attributes attribute
28860 __attribute__ (( attribute-list [opt] ))
28862 The return value is as for cp_parser_gnu_attribute_list. */
28865 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
28867 tree attributes
= NULL_TREE
;
28869 auto cleanup
= make_temp_override
28870 (parser
->auto_is_implicit_function_template_parm_p
, false);
28875 tree attribute_list
;
28878 /* Peek at the next token. */
28879 token
= cp_lexer_peek_token (parser
->lexer
);
28880 /* If it's not `__attribute__', then we're done. */
28881 if (token
->keyword
!= RID_ATTRIBUTE
)
28884 /* Consume the `__attribute__' keyword. */
28885 cp_lexer_consume_token (parser
->lexer
);
28886 /* Look for the two `(' tokens. */
28887 matching_parens outer_parens
;
28888 if (!outer_parens
.require_open (parser
))
28890 matching_parens inner_parens
;
28891 if (!inner_parens
.require_open (parser
))
28894 /* Peek at the next token. */
28895 token
= cp_lexer_peek_token (parser
->lexer
);
28896 if (token
->type
!= CPP_CLOSE_PAREN
)
28897 /* Parse the attribute-list. */
28898 attribute_list
= cp_parser_gnu_attribute_list (parser
);
28900 /* If the next token is a `)', then there is no attribute
28902 attribute_list
= NULL
;
28904 /* Look for the two `)' tokens. */
28905 if (!inner_parens
.require_close (parser
))
28907 if (!outer_parens
.require_close (parser
))
28910 cp_parser_skip_to_end_of_statement (parser
);
28912 /* Add these new attributes to the list. */
28913 attributes
= attr_chainon (attributes
, attribute_list
);
28919 /* Parse a GNU attribute-list.
28923 attribute-list , attribute
28927 identifier ( identifier )
28928 identifier ( identifier , expression-list )
28929 identifier ( expression-list )
28931 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
28932 to an attribute. The TREE_PURPOSE of each node is the identifier
28933 indicating which attribute is in use. The TREE_VALUE represents
28934 the arguments, if any. */
28937 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
28939 tree attribute_list
= NULL_TREE
;
28940 bool save_translate_strings_p
= parser
->translate_strings_p
;
28942 /* Don't create wrapper nodes within attributes: the
28943 handlers don't know how to handle them. */
28944 auto_suppress_location_wrappers sentinel
;
28946 parser
->translate_strings_p
= false;
28953 /* Look for the identifier. We also allow keywords here; for
28954 example `__attribute__ ((const))' is legal. */
28955 token
= cp_lexer_peek_token (parser
->lexer
);
28956 if (token
->type
== CPP_NAME
28957 || token
->type
== CPP_KEYWORD
)
28959 tree arguments
= NULL_TREE
;
28961 /* Consume the token, but save it since we need it for the
28962 SIMD enabled function parsing. */
28963 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
28965 /* Save away the identifier that indicates which attribute
28967 identifier
= (token
->type
== CPP_KEYWORD
)
28968 /* For keywords, use the canonical spelling, not the
28969 parsed identifier. */
28970 ? ridpointers
[(int) token
->keyword
]
28971 : id_token
->u
.value
;
28973 identifier
= canonicalize_attr_name (identifier
);
28974 attribute
= build_tree_list (identifier
, NULL_TREE
);
28976 /* Peek at the next token. */
28977 token
= cp_lexer_peek_token (parser
->lexer
);
28978 /* If it's an `(', then parse the attribute arguments. */
28979 if (token
->type
== CPP_OPEN_PAREN
)
28981 vec
<tree
, va_gc
> *vec
;
28982 int attr_flag
= (attribute_takes_identifier_p (identifier
)
28983 ? id_attr
: normal_attr
);
28984 vec
= cp_parser_parenthesized_expression_list
28985 (parser
, attr_flag
, /*cast_p=*/false,
28986 /*allow_expansion_p=*/false,
28987 /*non_constant_p=*/NULL
);
28989 arguments
= error_mark_node
;
28992 arguments
= build_tree_list_vec (vec
);
28993 release_tree_vector (vec
);
28995 /* Save the arguments away. */
28996 TREE_VALUE (attribute
) = arguments
;
28999 if (arguments
!= error_mark_node
)
29001 /* Add this attribute to the list. */
29002 TREE_CHAIN (attribute
) = attribute_list
;
29003 attribute_list
= attribute
;
29006 token
= cp_lexer_peek_token (parser
->lexer
);
29008 /* Unless EXACTLY_ONE is set look for more attributes.
29009 If the next token isn't a `,', we're done. */
29010 if (exactly_one
|| token
->type
!= CPP_COMMA
)
29013 /* Consume the comma and keep going. */
29014 cp_lexer_consume_token (parser
->lexer
);
29016 parser
->translate_strings_p
= save_translate_strings_p
;
29018 /* We built up the list in reverse order. */
29019 return nreverse (attribute_list
);
29022 /* Parse arguments of omp::directive attribute.
29024 ( directive-name ,[opt] clause-list[opt] )
29026 For directive just remember the first/last tokens for subsequent
29030 cp_parser_omp_directive_args (cp_parser
*parser
, tree attribute
)
29032 cp_token
*first
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
29033 if (first
->type
== CPP_CLOSE_PAREN
)
29035 cp_lexer_consume_token (parser
->lexer
);
29036 error_at (first
->location
, "expected OpenMP directive name");
29037 cp_lexer_consume_token (parser
->lexer
);
29038 TREE_VALUE (attribute
) = NULL_TREE
;
29041 size_t n
= cp_parser_skip_balanced_tokens (parser
, 1);
29044 cp_lexer_consume_token (parser
->lexer
);
29045 error_at (first
->location
, "expected attribute argument as balanced "
29047 TREE_VALUE (attribute
) = NULL_TREE
;
29050 for (n
= n
- 2; n
; --n
)
29051 cp_lexer_consume_token (parser
->lexer
);
29052 cp_token
*last
= cp_lexer_peek_token (parser
->lexer
);
29053 cp_lexer_consume_token (parser
->lexer
);
29054 tree arg
= make_node (DEFERRED_PARSE
);
29055 DEFPARSE_TOKENS (arg
) = cp_token_cache_new (first
, last
);
29056 DEFPARSE_INSTANTIATIONS (arg
) = nullptr;
29057 TREE_VALUE (attribute
) = tree_cons (NULL_TREE
, arg
, TREE_VALUE (attribute
));
29060 /* Parse arguments of omp::sequence attribute.
29062 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
29065 cp_parser_omp_sequence_args (cp_parser
*parser
, tree attribute
)
29067 matching_parens parens
;
29068 parens
.consume_open (parser
);
29071 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29072 if (token
->type
== CPP_NAME
29073 && token
->u
.value
== omp_identifier
29074 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
29076 cp_lexer_consume_token (parser
->lexer
);
29077 cp_lexer_consume_token (parser
->lexer
);
29078 token
= cp_lexer_peek_token (parser
->lexer
);
29080 bool directive
= false;
29082 if (token
->type
!= CPP_NAME
)
29085 p
= IDENTIFIER_POINTER (token
->u
.value
);
29086 if (strcmp (p
, "directive") == 0)
29088 else if (strcmp (p
, "sequence") != 0)
29090 error_at (token
->location
, "expected %<directive%> or %<sequence%>");
29091 cp_parser_skip_to_closing_parenthesis (parser
,
29092 /*recovering=*/true,
29094 /*consume_paren=*/false);
29095 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
29097 cp_lexer_consume_token (parser
->lexer
);
29099 cp_lexer_consume_token (parser
->lexer
);
29100 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
29101 cp_parser_required_error (parser
, RT_OPEN_PAREN
, false,
29103 else if (directive
)
29104 cp_parser_omp_directive_args (parser
, attribute
);
29106 cp_parser_omp_sequence_args (parser
, attribute
);
29107 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
29109 cp_lexer_consume_token (parser
->lexer
);
29112 if (!parens
.require_close (parser
))
29113 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
29114 /*consume_paren=*/true);
29117 /* Parse a standard C++11 attribute.
29119 The returned representation is a TREE_LIST which TREE_PURPOSE is
29120 the scoped name of the attribute, and the TREE_VALUE is its
29123 Note that the scoped name of the attribute is itself a TREE_LIST
29124 which TREE_PURPOSE is the namespace of the attribute, and
29125 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29126 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29127 and which TREE_PURPOSE is directly the attribute name.
29129 Clients of the attribute code should use get_attribute_namespace
29130 and get_attribute_name to get the actual namespace and name of
29131 attributes, regardless of their being GNU or C++11 attributes.
29134 attribute-token attribute-argument-clause [opt]
29138 attribute-scoped-token
29140 attribute-scoped-token:
29141 attribute-namespace :: identifier
29143 attribute-namespace:
29146 attribute-argument-clause:
29147 ( balanced-token-seq )
29149 balanced-token-seq:
29150 balanced-token [opt]
29151 balanced-token-seq balanced-token
29154 ( balanced-token-seq )
29155 [ balanced-token-seq ]
29156 { balanced-token-seq }. */
29159 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
29161 tree attribute
, attr_id
= NULL_TREE
, arguments
;
29164 auto cleanup
= make_temp_override
29165 (parser
->auto_is_implicit_function_template_parm_p
, false);
29167 /* First, parse name of the attribute, a.k.a attribute-token. */
29169 token
= cp_lexer_peek_token (parser
->lexer
);
29170 if (token
->type
== CPP_NAME
)
29171 attr_id
= token
->u
.value
;
29172 else if (token
->type
== CPP_KEYWORD
)
29173 attr_id
= ridpointers
[(int) token
->keyword
];
29174 else if (token
->flags
& NAMED_OP
)
29175 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
29177 if (attr_id
== NULL_TREE
)
29180 cp_lexer_consume_token (parser
->lexer
);
29182 token
= cp_lexer_peek_token (parser
->lexer
);
29183 if (token
->type
== CPP_SCOPE
)
29185 /* We are seeing a scoped attribute token. */
29187 cp_lexer_consume_token (parser
->lexer
);
29189 error_at (token
->location
, "attribute using prefix used together "
29190 "with scoped attribute token");
29193 token
= cp_lexer_peek_token (parser
->lexer
);
29194 if (token
->type
== CPP_NAME
)
29195 attr_id
= token
->u
.value
;
29196 else if (token
->type
== CPP_KEYWORD
)
29197 attr_id
= ridpointers
[(int) token
->keyword
];
29198 else if (token
->flags
& NAMED_OP
)
29199 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
29202 error_at (token
->location
,
29203 "expected an identifier for the attribute name");
29204 return error_mark_node
;
29206 cp_lexer_consume_token (parser
->lexer
);
29208 attr_ns
= canonicalize_attr_name (attr_ns
);
29209 attr_id
= canonicalize_attr_name (attr_id
);
29210 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
29212 token
= cp_lexer_peek_token (parser
->lexer
);
29216 attr_ns
= canonicalize_attr_name (attr_ns
);
29217 attr_id
= canonicalize_attr_name (attr_id
);
29218 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
29223 attr_id
= canonicalize_attr_name (attr_id
);
29224 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
29226 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29227 but no longer: we have to be able to tell [[noreturn]] and
29228 __attribute__((noreturn)) apart. */
29229 /* C++14 deprecated attribute is equivalent to GNU's. */
29230 if (is_attribute_p ("deprecated", attr_id
))
29231 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
29232 /* C++17 fallthrough attribute is equivalent to GNU's. */
29233 else if (is_attribute_p ("fallthrough", attr_id
))
29234 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
29235 /* Transactional Memory TS optimize_for_synchronized attribute is
29236 equivalent to GNU transaction_callable. */
29237 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
29238 TREE_PURPOSE (attribute
)
29239 = get_identifier ("transaction_callable");
29240 /* Transactional Memory attributes are GNU attributes. */
29241 else if (tm_attr_to_mask (attr_id
))
29242 TREE_PURPOSE (attribute
) = attr_id
;
29245 /* Now parse the optional argument clause of the attribute. */
29247 if (token
->type
!= CPP_OPEN_PAREN
)
29249 if ((flag_openmp
|| flag_openmp_simd
)
29250 && attr_ns
== omp_identifier
29251 && (is_attribute_p ("directive", attr_id
)
29252 || is_attribute_p ("sequence", attr_id
)))
29254 error_at (token
->location
, "%<omp::%E%> attribute requires argument",
29262 vec
<tree
, va_gc
> *vec
;
29263 int attr_flag
= normal_attr
;
29265 /* Maybe we don't expect to see any arguments for this attribute. */
29266 const attribute_spec
*as
29267 = lookup_attribute_spec (TREE_PURPOSE (attribute
));
29268 if (as
&& as
->max_length
== 0)
29270 error_at (token
->location
, "%qE attribute does not take any arguments",
29272 cp_parser_skip_to_closing_parenthesis (parser
,
29273 /*recovering=*/true,
29274 /*or_comma=*/false,
29275 /*consume_paren=*/true);
29276 return error_mark_node
;
29279 if (attr_ns
== gnu_identifier
29280 && attribute_takes_identifier_p (attr_id
))
29281 /* A GNU attribute that takes an identifier in parameter. */
29282 attr_flag
= id_attr
;
29284 /* If this is a fake attribute created to handle -Wno-attributes,
29285 we must skip parsing the arguments. */
29286 if (as
== NULL
|| attribute_ignored_p (as
))
29288 if ((flag_openmp
|| flag_openmp_simd
) && attr_ns
== omp_identifier
)
29290 if (is_attribute_p ("directive", attr_id
))
29292 cp_parser_omp_directive_args (parser
, attribute
);
29295 else if (is_attribute_p ("sequence", attr_id
))
29297 TREE_VALUE (TREE_PURPOSE (attribute
))
29298 = get_identifier ("directive");
29299 cp_parser_omp_sequence_args (parser
, attribute
);
29300 TREE_VALUE (attribute
) = nreverse (TREE_VALUE (attribute
));
29305 /* For unknown attributes, just skip balanced tokens instead of
29306 trying to parse the arguments. */
29307 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1) - 1; n
; --n
)
29308 cp_lexer_consume_token (parser
->lexer
);
29312 vec
= cp_parser_parenthesized_expression_list
29313 (parser
, attr_flag
, /*cast_p=*/false,
29314 /*allow_expansion_p=*/true,
29315 /*non_constant_p=*/NULL
);
29317 arguments
= error_mark_node
;
29320 if (vec
->is_empty ())
29321 /* e.g. [[attr()]]. */
29322 error_at (token
->location
, "parentheses must be omitted if "
29323 "%qE attribute argument list is empty",
29325 arguments
= build_tree_list_vec (vec
);
29326 release_tree_vector (vec
);
29329 if (arguments
== error_mark_node
)
29330 attribute
= error_mark_node
;
29332 TREE_VALUE (attribute
) = arguments
;
29338 /* Warn if the attribute ATTRIBUTE appears more than once in the
29339 attribute-list ATTRIBUTES. This used to be enforced for certain
29340 attributes, but the restriction was removed in P2156. Note that
29341 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
29342 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29343 found in ATTRIBUTES. */
29346 cp_parser_check_std_attribute (location_t loc
, tree attributes
, tree attribute
)
29348 static auto alist
= { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29349 "likely", "unlikely", "fallthrough",
29350 "no_unique_address" };
29352 for (const auto &a
: alist
)
29353 if (is_attribute_p (a
, get_attribute_name (attribute
))
29354 && lookup_attribute (a
, attributes
))
29356 if (!from_macro_expansion_at (loc
))
29357 warning_at (loc
, OPT_Wattributes
, "attribute %qs specified "
29358 "multiple times", a
);
29364 /* Parse a list of standard C++-11 attributes.
29368 attribute-list , attribute[opt]
29370 attribute-list , attribute ...
29374 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
29376 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
29377 cp_token
*token
= NULL
;
29381 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29382 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
29383 if (attribute
== error_mark_node
)
29385 if (attribute
!= NULL_TREE
)
29387 if (cp_parser_check_std_attribute (loc
, attributes
, attribute
))
29389 TREE_CHAIN (attribute
) = attributes
;
29390 attributes
= attribute
;
29393 token
= cp_lexer_peek_token (parser
->lexer
);
29394 if (token
->type
== CPP_ELLIPSIS
)
29396 cp_lexer_consume_token (parser
->lexer
);
29397 if (attribute
== NULL_TREE
)
29398 error_at (token
->location
,
29399 "expected attribute before %<...%>");
29402 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
29403 if (pack
== error_mark_node
)
29404 return error_mark_node
;
29405 TREE_VALUE (attribute
) = pack
;
29407 token
= cp_lexer_peek_token (parser
->lexer
);
29409 if (token
->type
!= CPP_COMMA
)
29411 cp_lexer_consume_token (parser
->lexer
);
29413 attributes
= nreverse (attributes
);
29417 /* Parse a standard C++-11 attribute specifier.
29419 attribute-specifier:
29420 [ [ attribute-using-prefix [opt] attribute-list ] ]
29421 alignment-specifier
29423 attribute-using-prefix:
29424 using attribute-namespace :
29426 alignment-specifier:
29427 alignas ( type-id ... [opt] )
29428 alignas ( alignment-expression ... [opt] ). */
29431 cp_parser_std_attribute_spec (cp_parser
*parser
)
29433 tree attributes
= NULL_TREE
;
29434 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29436 if (token
->type
== CPP_OPEN_SQUARE
29437 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
29439 tree attr_ns
= NULL_TREE
;
29441 cp_lexer_consume_token (parser
->lexer
);
29442 cp_lexer_consume_token (parser
->lexer
);
29444 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
29446 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
29447 if (token
->type
== CPP_NAME
)
29448 attr_ns
= token
->u
.value
;
29449 else if (token
->type
== CPP_KEYWORD
)
29450 attr_ns
= ridpointers
[(int) token
->keyword
];
29451 else if (token
->flags
& NAMED_OP
)
29452 attr_ns
= get_identifier (cpp_type2name (token
->type
,
29455 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
29457 if (cxx_dialect
< cxx17
)
29458 pedwarn (input_location
, OPT_Wc__17_extensions
,
29459 "attribute using prefix only available "
29460 "with %<-std=c++17%> or %<-std=gnu++17%>");
29462 cp_lexer_consume_token (parser
->lexer
);
29463 cp_lexer_consume_token (parser
->lexer
);
29464 cp_lexer_consume_token (parser
->lexer
);
29467 attr_ns
= NULL_TREE
;
29470 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
29472 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
29473 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
29474 cp_parser_skip_to_end_of_statement (parser
);
29476 /* Warn about parsing c++11 attribute in non-c++11 mode, only
29477 when we are sure that we have actually parsed them. */
29478 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
29484 /* Look for an alignment-specifier. */
29486 token
= cp_lexer_peek_token (parser
->lexer
);
29488 if (token
->type
!= CPP_KEYWORD
29489 || token
->keyword
!= RID_ALIGNAS
)
29492 cp_lexer_consume_token (parser
->lexer
);
29493 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
29495 matching_parens parens
;
29496 if (!parens
.require_open (parser
))
29497 return error_mark_node
;
29499 cp_parser_parse_tentatively (parser
);
29500 alignas_expr
= cp_parser_type_id (parser
);
29502 if (!cp_parser_parse_definitely (parser
))
29504 alignas_expr
= cp_parser_assignment_expression (parser
);
29505 if (alignas_expr
== error_mark_node
)
29506 cp_parser_skip_to_end_of_statement (parser
);
29507 if (alignas_expr
== NULL_TREE
29508 || alignas_expr
== error_mark_node
)
29509 return alignas_expr
;
29512 alignas_expr
= cxx_alignas_expr (alignas_expr
);
29513 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
29515 /* Handle alignas (pack...). */
29516 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
29518 cp_lexer_consume_token (parser
->lexer
);
29519 alignas_expr
= make_pack_expansion (alignas_expr
);
29522 /* Something went wrong, so don't build the attribute. */
29523 if (alignas_expr
== error_mark_node
)
29524 return error_mark_node
;
29526 /* Missing ')' means the code cannot possibly be valid; go ahead
29527 and commit to make sure we issue a hard error. */
29528 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
29529 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29530 cp_parser_commit_to_tentative_parse (parser
);
29532 if (!parens
.require_close (parser
))
29533 return error_mark_node
;
29535 /* Build the C++-11 representation of an 'aligned'
29538 = build_tree_list (build_tree_list (gnu_identifier
,
29539 aligned_identifier
), alignas_expr
);
29545 /* Parse a standard C++-11 attribute-specifier-seq.
29547 attribute-specifier-seq:
29548 attribute-specifier-seq [opt] attribute-specifier
29552 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
29554 tree attr_specs
= NULL_TREE
;
29555 tree attr_last
= NULL_TREE
;
29557 /* Don't create wrapper nodes within attributes: the
29558 handlers don't know how to handle them. */
29559 auto_suppress_location_wrappers sentinel
;
29563 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
29564 if (attr_spec
== NULL_TREE
)
29566 if (attr_spec
== error_mark_node
)
29567 return error_mark_node
;
29570 TREE_CHAIN (attr_last
) = attr_spec
;
29572 attr_specs
= attr_last
= attr_spec
;
29573 attr_last
= tree_last (attr_last
);
29579 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
29580 return index of the first token after balanced-token, or N on failure. */
29583 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
29586 int nparens
= 0, nbraces
= 0, nsquares
= 0;
29588 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
29590 case CPP_PRAGMA_EOL
:
29591 if (!parser
->lexer
->in_pragma
)
29595 /* Ran out of tokens. */
29597 case CPP_OPEN_PAREN
:
29600 case CPP_OPEN_BRACE
:
29603 case CPP_OPEN_SQUARE
:
29606 case CPP_CLOSE_PAREN
:
29609 case CPP_CLOSE_BRACE
:
29612 case CPP_CLOSE_SQUARE
:
29618 while (nparens
|| nbraces
|| nsquares
);
29622 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
29623 return index of the first token after the GNU attribute tokens, or N on
29627 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
29631 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
29632 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
29633 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
29636 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
29639 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
29646 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
29647 next token), return index of the first token after the standard C++11
29648 attribute tokens, or N on failure. */
29651 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
29655 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
29656 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
29658 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
29661 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
29665 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
29666 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
29668 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
29679 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
29680 as the next token), return index of the first token after the attribute
29681 tokens, or N on failure. */
29684 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
29686 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
29687 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
29688 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
29691 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
29692 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
29693 current value of the PEDANTIC flag, regardless of whether or not
29694 the `__extension__' keyword is present. The caller is responsible
29695 for restoring the value of the PEDANTIC flag. */
29698 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
29700 /* Save the old value of the PEDANTIC flag. */
29701 *saved_pedantic
= pedantic
;
29703 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
29705 /* Consume the `__extension__' token. */
29706 cp_lexer_consume_token (parser
->lexer
);
29707 /* We're not being pedantic while the `__extension__' keyword is
29717 /* Parse a label declaration.
29720 __label__ label-declarator-seq ;
29722 label-declarator-seq:
29723 identifier , label-declarator-seq
29727 cp_parser_label_declaration (cp_parser
* parser
)
29729 /* Look for the `__label__' keyword. */
29730 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
29736 /* Look for an identifier. */
29737 identifier
= cp_parser_identifier (parser
);
29738 /* If we failed, stop. */
29739 if (identifier
== error_mark_node
)
29741 /* Declare it as a label. */
29742 finish_label_decl (identifier
);
29743 /* If the next token is a `;', stop. */
29744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29746 /* Look for the `,' separating the label declarations. */
29747 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
29750 /* Look for the final `;'. */
29751 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29754 // -------------------------------------------------------------------------- //
29755 // Concept definitions
29758 cp_parser_concept_definition (cp_parser
*parser
)
29760 /* A concept definition is an unevaluated context. */
29763 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
));
29764 cp_lexer_consume_token (parser
->lexer
);
29766 cp_expr id
= cp_parser_identifier (parser
);
29767 if (id
== error_mark_node
)
29769 cp_parser_skip_to_end_of_statement (parser
);
29770 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29774 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29776 cp_parser_skip_to_end_of_statement (parser
);
29777 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29778 return error_mark_node
;
29781 processing_constraint_expression_sentinel parsing_constraint
;
29782 tree init
= cp_parser_constraint_expression (parser
);
29783 if (init
== error_mark_node
)
29784 cp_parser_skip_to_end_of_statement (parser
);
29786 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
29787 but continue as if it were. */
29788 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29790 return finish_concept_definition (id
, init
);
29793 // -------------------------------------------------------------------------- //
29796 /* Diagnose an expression that should appear in ()'s within a requires-clause
29797 and suggest where to place those parentheses. */
29800 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc
)
29802 error_at (loc
, "expression must be enclosed in parentheses");
29806 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc
)
29808 gcc_rich_location
richloc (loc
);
29809 richloc
.add_fixit_insert_before ("(");
29810 richloc
.add_fixit_insert_after (")");
29811 error_at (&richloc
, "expression must be enclosed in parentheses");
29814 /* Characterizes the likely kind of expression intended by a mis-written
29815 primary constraint. */
29816 enum primary_constraint_error
29819 pce_maybe_operator
,
29823 /* Returns true if the token(s) following a primary-expression in a
29824 constraint-logical-* expression would require parentheses. */
29826 static primary_constraint_error
29827 cp_parser_constraint_requires_parens (cp_parser
*parser
, bool lambda_p
)
29829 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29830 switch (token
->type
)
29837 /* An equal sign may be part of the definition of a function,
29838 and not an assignment operator, when parsing the expression
29839 for a trailing requires-clause. For example:
29841 template<typename T>
29843 S() requires C<T> = default;
29846 Don't try to reparse this a binary operator. */
29847 if (cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DELETE
)
29848 || cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DEFAULT
))
29851 gcc_fallthrough ();
29854 /* Arithmetic operators. */
29860 /* Bitwise operators. */
29866 /* Relational operators. */
29872 case CPP_GREATER_EQ
:
29873 case CPP_SPACESHIP
:
29874 /* Pointer-to-member. */
29876 case CPP_DEREF_STAR
:
29877 /* Assignment operators. */
29886 case CPP_RSHIFT_EQ
:
29887 case CPP_LSHIFT_EQ
:
29888 /* Conditional operator */
29890 /* Unenclosed binary or conditional operator. */
29891 return pce_maybe_operator
;
29893 case CPP_OPEN_PAREN
:
29895 /* A primary constraint that precedes the parameter-list of a
29896 lambda expression is followed by an open paren.
29898 []<typename T> requires C (T a, T b) { ... }
29900 Don't try to re-parse this as a postfix expression. */
29904 gcc_fallthrough ();
29906 case CPP_OPEN_SQUARE
:
29908 /* A primary-constraint-expression followed by a '[[' is not a
29909 postfix expression. */
29910 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
))
29913 gcc_fallthrough ();
29915 case CPP_PLUS_PLUS
:
29916 case CPP_MINUS_MINUS
:
29918 /* Unenclosed postfix operator. */
29919 return pce_maybe_postfix
;
29922 /* A primary constraint that precedes the lambda-declarator of a
29923 lambda expression is followed by trailing return type.
29925 []<typename T> requires C -> void {}
29927 Don't try to re-parse this as a postfix expression in
29928 C++23 and later. In C++20 ( needs to come in between but we
29929 allow it to be omitted with pedwarn. */
29932 /* Unenclosed postfix operator. */
29933 return pce_maybe_postfix
;
29937 /* Returns true if the next token begins a unary expression, preceded by
29938 an operator or keyword. */
29941 cp_parser_unary_constraint_requires_parens (cp_parser
*parser
)
29943 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29944 switch (token
->type
)
29951 case CPP_PLUS_PLUS
:
29952 case CPP_MINUS_MINUS
:
29957 switch (token
->keyword
)
29961 case RID_REINTCAST
:
29962 case RID_CONSTCAST
:
29984 /* Parse a primary expression within a constraint. */
29987 cp_parser_constraint_primary_expression (cp_parser
*parser
, bool lambda_p
)
29989 /* If this looks like a unary expression, parse it as such, but diagnose
29990 it as ill-formed; it requires parens. */
29991 if (cp_parser_unary_constraint_requires_parens (parser
))
29993 cp_expr e
= cp_parser_assignment_expression (parser
, NULL
, false, false);
29994 cp_parser_diagnose_ungrouped_constraint_rich (e
.get_location());
29998 cp_lexer_save_tokens (parser
->lexer
);
30000 location_t loc
= input_location
;
30001 cp_expr expr
= cp_parser_primary_expression (parser
,
30002 /*address_p=*/false,
30004 /*template_arg_p=*/false,
30006 expr
.maybe_add_location_wrapper ();
30008 primary_constraint_error pce
= pce_ok
;
30009 if (expr
!= error_mark_node
)
30011 /* The primary-expression could be part of an unenclosed non-logical
30012 compound expression. */
30013 pce
= cp_parser_constraint_requires_parens (parser
, lambda_p
);
30017 cp_lexer_commit_tokens (parser
->lexer
);
30018 return finish_constraint_primary_expr (expr
);
30021 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
30022 error, but return the expression as if it were valid. */
30023 cp_lexer_rollback_tokens (parser
->lexer
);
30024 cp_parser_parse_tentatively (parser
);
30025 if (pce
== pce_maybe_operator
)
30026 expr
= cp_parser_assignment_expression (parser
, NULL
, false, false);
30028 expr
= cp_parser_simple_cast_expression (parser
);
30029 if (cp_parser_parse_definitely (parser
))
30031 cp_parser_diagnose_ungrouped_constraint_rich (expr
.get_location());
30035 /* Otherwise, something has gone very wrong, and we can't generate a more
30036 meaningful diagnostic or recover. */
30037 cp_parser_diagnose_ungrouped_constraint_plain (loc
);
30038 return error_mark_node
;
30041 /* Parse a constraint-logical-and-expression.
30043 constraint-logical-and-expression:
30045 constraint-logical-and-expression '&&' primary-expression */
30048 cp_parser_constraint_logical_and_expression (cp_parser
*parser
, bool lambda_p
)
30050 cp_expr lhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
30051 while (cp_lexer_next_token_is (parser
->lexer
, CPP_AND_AND
))
30053 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
30054 tree rhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
30055 lhs
= finish_constraint_and_expr (op
->location
, lhs
, rhs
);
30060 /* Parse a constraint-logical-or-expression.
30062 constraint-logical-or-expression:
30063 constraint-logical-and-expression
30064 constraint-logical-or-expression '||' constraint-logical-and-expression */
30067 cp_parser_constraint_logical_or_expression (cp_parser
*parser
, bool lambda_p
)
30069 cp_expr lhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
30070 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OR_OR
))
30072 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
30073 cp_expr rhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
30074 lhs
= finish_constraint_or_expr (op
->location
, lhs
, rhs
);
30079 /* Parse the expression after a requires-clause. This has a different grammar
30080 than that in the concepts TS. */
30083 cp_parser_requires_clause_expression (cp_parser
*parser
, bool lambda_p
)
30085 processing_constraint_expression_sentinel parsing_constraint
;
30086 ++processing_template_decl
;
30087 cp_expr expr
= cp_parser_constraint_logical_or_expression (parser
, lambda_p
);
30088 --processing_template_decl
;
30089 if (check_for_bare_parameter_packs (expr
))
30090 expr
= error_mark_node
;
30094 /* Parse a expression after a requires clause.
30096 constraint-expression:
30097 logical-or-expression
30099 The required logical-or-expression must be a constant expression. Note
30100 that we don't check that the expression is constepxr here. We defer until
30101 we analyze constraints and then, we only check atomic constraints. */
30104 cp_parser_constraint_expression (cp_parser
*parser
)
30106 processing_constraint_expression_sentinel parsing_constraint
;
30107 ++processing_template_decl
;
30108 cp_expr expr
= cp_parser_binary_expression (parser
, false, true,
30109 PREC_NOT_OPERATOR
, NULL
);
30110 --processing_template_decl
;
30111 if (check_for_bare_parameter_packs (expr
))
30112 expr
= error_mark_node
;
30113 expr
.maybe_add_location_wrapper ();
30117 /* Optionally parse a requires clause:
30120 `requires` constraint-logical-or-expression.
30122 `requires constraint-expression.
30124 LAMBDA_P is true when the requires-clause is parsed before the
30125 parameter-list of a lambda-declarator. */
30128 cp_parser_requires_clause_opt (cp_parser
*parser
, bool lambda_p
)
30130 /* A requires clause is an unevaluated context. */
30133 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
30134 if (tok
->keyword
!= RID_REQUIRES
)
30136 if (!flag_concepts
&& tok
->type
== CPP_NAME
30137 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
30139 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30140 "%<requires%> only available with "
30141 "%<-std=c++20%> or %<-fconcepts%>");
30142 /* Parse and discard the requires-clause. */
30143 cp_lexer_consume_token (parser
->lexer
);
30144 cp_parser_constraint_expression (parser
);
30149 cp_token
*tok2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30150 if (tok2
->type
== CPP_OPEN_BRACE
)
30152 /* An opening brace following the start of a requires-clause is
30153 ill-formed; the user likely forgot the second `requires' that
30154 would start a requires-expression. */
30155 gcc_rich_location
richloc (tok2
->location
);
30156 richloc
.add_fixit_insert_after (tok
->location
, " requires");
30157 error_at (&richloc
, "missing additional %<requires%> to start "
30158 "a requires-expression");
30159 /* Don't consume the `requires', so that it's reused as the start of a
30160 requires-expression. */
30163 cp_lexer_consume_token (parser
->lexer
);
30165 if (!flag_concepts_ts
)
30166 return cp_parser_requires_clause_expression (parser
, lambda_p
);
30168 return cp_parser_constraint_expression (parser
);
30171 /*---------------------------------------------------------------------------
30172 Requires expressions
30173 ---------------------------------------------------------------------------*/
30175 /* Parse a requires expression
30177 requirement-expression:
30178 'requires' requirement-parameter-list [opt] requirement-body */
30181 cp_parser_requires_expression (cp_parser
*parser
)
30183 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
30184 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
30186 /* Avoid committing to outer tentative parse. */
30187 tentative_firewall
firewall (parser
);
30189 /* This is definitely a requires-expression. */
30190 cp_parser_commit_to_tentative_parse (parser
);
30194 /* Local parameters are delared as variables within the scope
30195 of the expression. They are not visible past the end of
30196 the expression. Expressions within the requires-expression
30197 are unevaluated. */
30198 struct scope_sentinel
30202 ++cp_unevaluated_operand
;
30203 begin_scope (sk_function_parms
, NULL_TREE
);
30204 current_binding_level
->requires_expression
= true;
30209 pop_bindings_and_leave_scope ();
30210 --cp_unevaluated_operand
;
30214 /* Parse the optional parameter list. */
30215 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30217 parms
= cp_parser_requirement_parameter_list (parser
);
30218 if (parms
== error_mark_node
)
30219 return error_mark_node
;
30224 /* Parse the requirement body. */
30225 ++processing_template_decl
;
30226 reqs
= cp_parser_requirement_body (parser
);
30227 --processing_template_decl
;
30228 if (reqs
== error_mark_node
)
30229 return error_mark_node
;
30232 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
30234 grokparms (parms
, &parms
);
30235 loc
= make_location (loc
, loc
, parser
->lexer
);
30236 tree expr
= finish_requires_expr (loc
, parms
, reqs
);
30237 if (!processing_template_decl
)
30239 /* Perform semantic processing now to diagnose any invalid types and
30241 int saved_errorcount
= errorcount
;
30242 tsubst_requires_expr (expr
, NULL_TREE
, tf_warning_or_error
, NULL_TREE
);
30243 if (errorcount
> saved_errorcount
)
30244 return error_mark_node
;
30249 /* Parse a parameterized requirement.
30251 requirement-parameter-list:
30252 '(' parameter-declaration-clause ')' */
30255 cp_parser_requirement_parameter_list (cp_parser
*parser
)
30257 matching_parens parens
;
30258 if (!parens
.require_open (parser
))
30259 return error_mark_node
;
30261 tree parms
= (cp_parser_parameter_declaration_clause
30262 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
30264 if (!parens
.require_close (parser
))
30265 return error_mark_node
;
30267 /* Modify the declared parameters by removing their context
30268 so they don't refer to the enclosing scope and explicitly
30269 indicating that they are constraint variables. */
30270 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
30272 if (parm
== void_list_node
|| parm
== explicit_void_list_node
)
30274 tree decl
= TREE_VALUE (parm
);
30275 if (decl
!= error_mark_node
)
30277 DECL_CONTEXT (decl
) = NULL_TREE
;
30278 CONSTRAINT_VAR_P (decl
) = true;
30285 /* Parse the body of a requirement.
30288 '{' requirement-list '}' */
30290 cp_parser_requirement_body (cp_parser
*parser
)
30292 matching_braces braces
;
30293 if (!braces
.require_open (parser
))
30294 return error_mark_node
;
30296 tree reqs
= cp_parser_requirement_seq (parser
);
30298 if (!braces
.require_close (parser
))
30299 return error_mark_node
;
30304 /* Parse a sequence of requirements.
30308 requirement-seq requirement */
30311 cp_parser_requirement_seq (cp_parser
*parser
)
30313 tree result
= NULL_TREE
;
30316 tree req
= cp_parser_requirement (parser
);
30317 if (req
!= error_mark_node
)
30318 result
= tree_cons (NULL_TREE
, req
, result
);
30320 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
)
30321 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
));
30323 /* If there are no valid requirements, this is not a valid expression. */
30325 return error_mark_node
;
30327 /* Reverse the order of requirements so they are analyzed in order. */
30328 return nreverse (result
);
30331 /* Parse a syntactic requirement or type requirement.
30335 compound-requirement
30337 nested-requirement */
30340 cp_parser_requirement (cp_parser
*parser
)
30342 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30343 return cp_parser_compound_requirement (parser
);
30344 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
30345 return cp_parser_type_requirement (parser
);
30346 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
30347 return cp_parser_nested_requirement (parser
);
30349 return cp_parser_simple_requirement (parser
);
30352 /* Parse a simple requirement.
30354 simple-requirement:
30358 cp_parser_simple_requirement (cp_parser
*parser
)
30360 location_t start
= cp_lexer_peek_token (parser
->lexer
)->location
;
30361 cp_expr expr
= cp_parser_expression (parser
, NULL
, false, false);
30362 if (expr
== error_mark_node
)
30363 cp_parser_skip_to_end_of_statement (parser
);
30365 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30367 if (!expr
|| expr
== error_mark_node
)
30368 return error_mark_node
;
30370 /* Sometimes we don't get locations, so use the cached token location
30371 as a reasonable approximation. */
30372 if (expr
.get_location() == UNKNOWN_LOCATION
)
30373 expr
.set_location (start
);
30375 for (tree t
= expr
; ; )
30377 if (TREE_CODE (t
) == TRUTH_ANDIF_EXPR
30378 || TREE_CODE (t
) == TRUTH_ORIF_EXPR
)
30380 t
= TREE_OPERAND (t
, 0);
30383 if (concept_check_p (t
))
30385 gcc_rich_location
richloc (get_start (start
));
30386 richloc
.add_fixit_insert_before (start
, "requires ");
30387 warning_at (&richloc
, OPT_Wmissing_requires
, "testing "
30388 "if a concept-id is a valid expression; add "
30389 "%<requires%> to check satisfaction");
30394 return finish_simple_requirement (expr
.get_location (), expr
);
30397 /* Parse a type requirement
30400 nested-name-specifier [opt] required-type-name ';'
30402 required-type-name:
30404 'template' [opt] simple-template-id */
30407 cp_parser_type_requirement (cp_parser
*parser
)
30409 cp_token
*start_tok
= cp_lexer_consume_token (parser
->lexer
);
30410 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30412 // Save the scope before parsing name specifiers.
30413 tree saved_scope
= parser
->scope
;
30414 tree saved_object_scope
= parser
->object_scope
;
30415 tree saved_qualifying_scope
= parser
->qualifying_scope
;
30416 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
30417 cp_parser_nested_name_specifier_opt (parser
,
30418 /*typename_keyword_p=*/true,
30419 /*check_dependency_p=*/false,
30421 /*is_declaration=*/false);
30424 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
30426 cp_lexer_consume_token (parser
->lexer
);
30427 type
= cp_parser_template_id (parser
,
30428 /*template_keyword_p=*/true,
30429 /*check_dependency=*/false,
30430 /*tag_type=*/none_type
,
30431 /*is_declaration=*/false);
30432 type
= make_typename_type (parser
->scope
, type
, typename_type
,
30433 /*complain=*/tf_error
);
30436 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
30438 if (TREE_CODE (type
) == TYPE_DECL
)
30439 type
= TREE_TYPE (type
);
30441 parser
->scope
= saved_scope
;
30442 parser
->object_scope
= saved_object_scope
;
30443 parser
->qualifying_scope
= saved_qualifying_scope
;
30445 if (type
== error_mark_node
)
30446 cp_parser_skip_to_end_of_statement (parser
);
30448 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30450 if (type
== error_mark_node
)
30451 return error_mark_node
;
30453 loc
= make_location (loc
, start_tok
->location
, parser
->lexer
);
30454 return finish_type_requirement (loc
, type
);
30457 /* Parse a compound requirement
30459 compound-requirement:
30460 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
30463 cp_parser_compound_requirement (cp_parser
*parser
)
30465 /* Parse an expression enclosed in '{ }'s. */
30466 matching_braces braces
;
30467 if (!braces
.require_open (parser
))
30468 return error_mark_node
;
30470 cp_token
*expr_token
= cp_lexer_peek_token (parser
->lexer
);
30472 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
30473 if (expr
== error_mark_node
)
30474 cp_parser_skip_to_closing_brace (parser
);
30476 if (!braces
.require_close (parser
))
30478 cp_parser_skip_to_end_of_statement (parser
);
30479 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30480 return error_mark_node
;
30483 /* If the expression was invalid, skip the remainder of the requirement. */
30484 if (!expr
|| expr
== error_mark_node
)
30486 cp_parser_skip_to_end_of_statement (parser
);
30487 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30488 return error_mark_node
;
30491 /* Parse the optional noexcept. */
30492 bool noexcept_p
= false;
30493 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
30495 cp_lexer_consume_token (parser
->lexer
);
30499 /* Parse the optional trailing return type. */
30500 tree type
= NULL_TREE
;
30501 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
30503 cp_lexer_consume_token (parser
->lexer
);
30504 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
30506 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
30507 parser
->in_result_type_constraint_p
= true;
30508 /* C++20 allows either a type-id or a type-constraint. Parsing
30509 a type-id will subsume the parsing for a type-constraint but
30510 allow for more syntactic forms (e.g., const C<T>*). */
30511 type
= cp_parser_trailing_type_id (parser
);
30512 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
30513 if (type
== error_mark_node
)
30514 return error_mark_node
;
30516 location_t type_loc
= make_location (tok
->location
, tok
->location
,
30519 /* Check that we haven't written something like 'const C<T>*'. */
30520 if (type_uses_auto (type
))
30522 if (!is_auto (type
))
30524 error_at (type_loc
,
30525 "result type is not a plain type-constraint");
30526 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30527 return error_mark_node
;
30530 else if (!flag_concepts_ts
)
30531 /* P1452R2 removed the trailing-return-type option. */
30532 error_at (type_loc
,
30533 "return-type-requirement is not a type-constraint");
30536 location_t loc
= make_location (expr_token
->location
,
30537 braces
.open_location (),
30540 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30542 if (expr
== error_mark_node
|| type
== error_mark_node
)
30543 return error_mark_node
;
30545 return finish_compound_requirement (loc
, expr
, type
, noexcept_p
);
30548 /* Parse a nested requirement. This is the same as a requires clause.
30550 nested-requirement:
30554 cp_parser_nested_requirement (cp_parser
*parser
)
30556 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
30557 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
30558 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30559 tree req
= cp_parser_constraint_expression (parser
);
30560 if (req
== error_mark_node
)
30561 cp_parser_skip_to_end_of_statement (parser
);
30562 loc
= make_location (loc
, tok
->location
, parser
->lexer
);
30563 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30564 if (req
== error_mark_node
)
30565 return error_mark_node
;
30566 return finish_nested_requirement (loc
, req
);
30569 /* Support Functions */
30571 /* Return the appropriate prefer_type argument for lookup_name based on
30574 static inline LOOK_want
30575 prefer_type_arg (tag_types tag_type
)
30579 case none_type
: return LOOK_want::NORMAL
; // No preference.
30580 case scope_type
: return LOOK_want::TYPE_NAMESPACE
; // Type or namespace.
30581 default: return LOOK_want::TYPE
; // Type only.
30585 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
30586 NAME should have one of the representations used for an
30587 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
30588 is returned. If PARSER->SCOPE is a dependent type, then a
30589 SCOPE_REF is returned.
30591 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
30592 returned; the name was already resolved when the TEMPLATE_ID_EXPR
30593 was formed. Abstractly, such entities should not be passed to this
30594 function, because they do not need to be looked up, but it is
30595 simpler to check for this special case here, rather than at the
30598 In cases not explicitly covered above, this function returns a
30599 DECL, OVERLOAD, or baselink representing the result of the lookup.
30600 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
30603 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
30604 (e.g., "struct") that was used. In that case bindings that do not
30605 refer to types are ignored.
30607 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
30610 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
30613 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
30616 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
30617 TREE_LIST of candidates if name-lookup results in an ambiguity, and
30618 NULL_TREE otherwise. */
30621 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
30622 enum tag_types tag_type
,
30625 bool check_dependency
,
30626 tree
*ambiguous_decls
,
30627 location_t name_location
)
30630 tree object_type
= parser
->context
->object_type
;
30632 /* Assume that the lookup will be unambiguous. */
30633 if (ambiguous_decls
)
30634 *ambiguous_decls
= NULL_TREE
;
30636 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
30637 no longer valid. Note that if we are parsing tentatively, and
30638 the parse fails, OBJECT_TYPE will be automatically restored. */
30639 parser
->context
->object_type
= NULL_TREE
;
30641 if (name
== error_mark_node
)
30642 return error_mark_node
;
30644 /* A template-id has already been resolved; there is no lookup to
30646 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
30648 if (BASELINK_P (name
))
30650 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
30651 == TEMPLATE_ID_EXPR
);
30655 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
30656 it should already have been checked to make sure that the name
30657 used matches the type being destroyed. */
30658 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
30662 /* Figure out to which type this destructor applies. */
30664 type
= parser
->scope
;
30665 else if (object_type
)
30666 type
= object_type
;
30668 type
= current_class_type
;
30669 /* If that's not a class type, there is no destructor. */
30670 if (!type
|| !CLASS_TYPE_P (type
))
30671 return error_mark_node
;
30673 /* In a non-static member function, check implicit this->. */
30674 if (current_class_ref
)
30675 return lookup_destructor (current_class_ref
, parser
->scope
, name
,
30676 tf_warning_or_error
);
30678 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
30679 lazily_declare_fn (sfk_destructor
, type
);
30681 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
30684 return error_mark_node
;
30687 /* By this point, the NAME should be an ordinary identifier. If
30688 the id-expression was a qualified name, the qualifying scope is
30689 stored in PARSER->SCOPE at this point. */
30690 gcc_assert (identifier_p (name
));
30692 /* Perform the lookup. */
30697 if (parser
->scope
== error_mark_node
)
30698 return error_mark_node
;
30700 /* If the SCOPE is dependent, the lookup must be deferred until
30701 the template is instantiated -- unless we are explicitly
30702 looking up names in uninstantiated templates. Even then, we
30703 cannot look up the name if the scope is not a class type; it
30704 might, for example, be a template type parameter. */
30705 dependent_p
= (TYPE_P (parser
->scope
)
30706 && dependent_scope_p (parser
->scope
));
30707 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
30709 /* Defer lookup. */
30710 decl
= error_mark_node
;
30713 tree pushed_scope
= NULL_TREE
;
30715 /* If PARSER->SCOPE is a dependent type, then it must be a
30716 class type, and we must not be checking dependencies;
30717 otherwise, we would have processed this lookup above. So
30718 that PARSER->SCOPE is not considered a dependent base by
30719 lookup_member, we must enter the scope here. */
30721 pushed_scope
= push_scope (parser
->scope
);
30723 /* If the PARSER->SCOPE is a template specialization, it
30724 may be instantiated during name lookup. In that case,
30725 errors may be issued. Even if we rollback the current
30726 tentative parse, those errors are valid. */
30727 decl
= lookup_qualified_name (parser
->scope
, name
,
30728 prefer_type_arg (tag_type
),
30729 /*complain=*/true);
30731 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
30732 lookup result and the nested-name-specifier nominates a class C:
30733 * if the name specified after the nested-name-specifier, when
30734 looked up in C, is the injected-class-name of C (Clause 9), or
30735 * if the name specified after the nested-name-specifier is the
30736 same as the identifier or the simple-template-id's template-
30737 name in the last component of the nested-name-specifier,
30738 the name is instead considered to name the constructor of
30739 class C. [ Note: for example, the constructor is not an
30740 acceptable lookup result in an elaborated-type-specifier so
30741 the constructor would not be used in place of the
30742 injected-class-name. --end note ] Such a constructor name
30743 shall be used only in the declarator-id of a declaration that
30744 names a constructor or in a using-declaration. */
30745 if (tag_type
== none_type
30746 && DECL_SELF_REFERENCE_P (decl
)
30747 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
30748 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
30749 prefer_type_arg (tag_type
),
30750 /*complain=*/true);
30753 pop_scope (pushed_scope
);
30756 /* If the scope is a dependent type and either we deferred lookup or
30757 we did lookup but didn't find the name, rememeber the name. */
30758 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
30759 && dependent_type_p (parser
->scope
))
30765 /* The resolution to Core Issue 180 says that `struct
30766 A::B' should be considered a type-name, even if `A'
30768 type
= make_typename_type (parser
->scope
, name
, tag_type
,
30769 /*complain=*/tf_error
);
30770 if (type
!= error_mark_node
)
30771 decl
= TYPE_NAME (type
);
30773 else if (is_template
30774 && (cp_parser_next_token_ends_template_argument_p (parser
)
30775 || cp_lexer_next_token_is (parser
->lexer
,
30777 decl
= make_unbound_class_template (parser
->scope
,
30779 /*complain=*/tf_error
);
30781 decl
= build_qualified_name (/*type=*/NULL_TREE
,
30782 parser
->scope
, name
,
30785 parser
->qualifying_scope
= parser
->scope
;
30786 parser
->object_scope
= NULL_TREE
;
30788 else if (object_type
)
30790 bool dep
= dependent_scope_p (object_type
);
30792 /* Look up the name in the scope of the OBJECT_TYPE, unless the
30793 OBJECT_TYPE is not a class. */
30794 if (!dep
&& CLASS_TYPE_P (object_type
))
30795 /* If the OBJECT_TYPE is a template specialization, it may
30796 be instantiated during name lookup. In that case, errors
30797 may be issued. Even if we rollback the current tentative
30798 parse, those errors are valid. */
30799 decl
= lookup_member (object_type
,
30802 /*prefer_type=*/tag_type
!= none_type
,
30803 tf_warning_or_error
);
30808 /* Look it up in the enclosing context. DR 141: When looking for a
30809 template-name after -> or ., only consider class templates. */
30810 decl
= lookup_name (name
, is_namespace
? LOOK_want::NAMESPACE
30811 /* DR 141: When looking in the
30812 current enclosing context for a
30813 template-name after -> or ., only
30814 consider class templates. */
30815 : is_template
? LOOK_want::TYPE
30816 : prefer_type_arg (tag_type
));
30818 /* If we did unqualified lookup of a dependent member-qualified name and
30819 found something, do we want to use it? P1787 clarified that we need
30820 to look in the object scope first even if it's dependent, but for now
30821 let's still use it in some cases.
30822 FIXME remember unqualified lookup result to use if member lookup fails
30823 at instantiation time. */
30824 if (decl
&& dep
&& is_template
)
30826 saved_token_sentinel
toks (parser
->lexer
, STS_ROLLBACK
);
30827 /* Only use the unqualified class template lookup if we're actually
30828 looking at a template arg list. */
30829 if (!cp_parser_skip_entire_template_parameter_list (parser
))
30833 /* If we know we're looking for a type (e.g. A in p->A::x),
30834 mock up a typename. */
30835 if (!decl
&& object_type
&& tag_type
!= none_type
30836 && dependentish_scope_p (object_type
))
30838 tree type
= build_typename_type (object_type
, name
, name
,
30840 decl
= TYPE_NAME (type
);
30843 parser
->object_scope
= object_type
;
30844 parser
->qualifying_scope
= NULL_TREE
;
30848 decl
= lookup_name (name
, is_namespace
? LOOK_want::NAMESPACE
30849 : prefer_type_arg (tag_type
));
30850 parser
->qualifying_scope
= NULL_TREE
;
30851 parser
->object_scope
= NULL_TREE
;
30854 /* If the lookup failed, let our caller know. */
30855 if (!decl
|| decl
== error_mark_node
)
30856 return error_mark_node
;
30858 /* If we have resolved the name of a member declaration, check to
30859 see if the declaration is accessible. When the name resolves to
30860 set of overloaded functions, accessibility is checked when
30861 overload resolution is done. If we have a TREE_LIST, then the lookup
30862 is either ambiguous or it found multiple injected-class-names, the
30863 accessibility of which is trivially satisfied.
30865 During an explicit instantiation, access is not checked at all,
30866 as per [temp.explicit]. */
30868 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
,
30869 tf_warning_or_error
);
30871 /* Pull out the template from an injected-class-name (or multiple). */
30873 decl
= maybe_get_template_decl_from_type_decl (decl
);
30875 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
30876 if (TREE_CODE (decl
) == TREE_LIST
)
30878 if (ambiguous_decls
)
30879 *ambiguous_decls
= decl
;
30880 /* The error message we have to print is too complicated for
30881 cp_parser_error, so we incorporate its actions directly. */
30882 if (!cp_parser_simulate_error (parser
))
30884 error_at (name_location
, "reference to %qD is ambiguous",
30886 print_candidates (decl
);
30888 return error_mark_node
;
30891 gcc_assert (DECL_P (decl
)
30892 || TREE_CODE (decl
) == OVERLOAD
30893 || TREE_CODE (decl
) == SCOPE_REF
30894 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
30895 || BASELINK_P (decl
));
30897 maybe_record_typedef_use (decl
);
30899 return cp_expr (decl
, name_location
);
30902 /* Like cp_parser_lookup_name, but for use in the typical case where
30903 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
30904 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
30907 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
30909 return cp_parser_lookup_name (parser
, name
,
30911 /*is_template=*/false,
30912 /*is_namespace=*/false,
30913 /*check_dependency=*/true,
30914 /*ambiguous_decls=*/NULL
,
30918 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
30919 the current context, return the TYPE_DECL. If TAG_NAME_P is
30920 true, the DECL indicates the class being defined in a class-head,
30921 or declared in an elaborated-type-specifier.
30923 Otherwise, return DECL. */
30926 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
30928 /* If the TEMPLATE_DECL is being declared as part of a class-head,
30929 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
30932 template <typename T> struct B;
30935 template <typename T> struct A::B {};
30937 Similarly, in an elaborated-type-specifier:
30939 namespace N { struct X{}; }
30942 template <typename T> friend struct N::X;
30945 However, if the DECL refers to a class type, and we are in
30946 the scope of the class, then the name lookup automatically
30947 finds the TYPE_DECL created by build_self_reference rather
30948 than a TEMPLATE_DECL. For example, in:
30950 template <class T> struct S {
30954 there is no need to handle such case. */
30956 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
30957 return DECL_TEMPLATE_RESULT (decl
);
30962 /* If too many, or too few, template-parameter lists apply to the
30963 declarator, issue an error message. Returns TRUE if all went well,
30964 and FALSE otherwise. */
30967 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
30968 cp_declarator
*declarator
,
30969 location_t declarator_location
)
30971 switch (declarator
->kind
)
30975 unsigned num_templates
= 0;
30976 tree scope
= declarator
->u
.id
.qualifying_scope
;
30977 bool template_id_p
= false;
30980 num_templates
= num_template_headers_for_class (scope
);
30981 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
30982 == TEMPLATE_ID_EXPR
)
30984 /* If the DECLARATOR has the form `X<y>' then it uses one
30985 additional level of template parameters. */
30987 template_id_p
= true;
30990 return cp_parser_check_template_parameters
30991 (parser
, num_templates
, template_id_p
, declarator_location
,
30998 case cdk_reference
:
31000 return (cp_parser_check_declarator_template_parameters
31001 (parser
, declarator
->declarator
, declarator_location
));
31008 gcc_unreachable ();
31013 /* NUM_TEMPLATES were used in the current declaration. If that is
31014 invalid, return FALSE and issue an error messages. Otherwise,
31015 return TRUE. If DECLARATOR is non-NULL, then we are checking a
31016 declarator and we can print more accurate diagnostics. */
31019 cp_parser_check_template_parameters (cp_parser
* parser
,
31020 unsigned num_templates
,
31021 bool template_id_p
,
31022 location_t location
,
31023 cp_declarator
*declarator
)
31025 /* If there are the same number of template classes and parameter
31026 lists, that's OK. */
31027 if (parser
->num_template_parameter_lists
== num_templates
)
31029 /* If there are more, but only one more, and the name ends in an identifier,
31030 then we are declaring a primary template. That's OK too. */
31032 && parser
->num_template_parameter_lists
== num_templates
+ 1)
31035 if (cp_parser_simulate_error (parser
))
31038 /* If there are more template classes than parameter lists, we have
31041 template <class T> void S<T>::R<T>::f (); */
31042 if (parser
->num_template_parameter_lists
< num_templates
)
31044 if (declarator
&& !current_function_decl
)
31045 error_at (location
, "specializing member %<%T::%E%> "
31046 "requires %<template<>%> syntax",
31047 declarator
->u
.id
.qualifying_scope
,
31048 declarator
->u
.id
.unqualified_name
);
31049 else if (declarator
)
31050 error_at (location
, "invalid declaration of %<%T::%E%>",
31051 declarator
->u
.id
.qualifying_scope
,
31052 declarator
->u
.id
.unqualified_name
);
31054 error_at (location
, "too few template-parameter-lists");
31057 /* Otherwise, there are too many template parameter lists. We have
31060 template <class T> template <class U> void S::f(); */
31061 error_at (location
, "too many template-parameter-lists");
31065 /* Parse an optional `::' token indicating that the following name is
31066 from the global namespace. If so, PARSER->SCOPE is set to the
31067 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
31068 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
31069 Returns the new value of PARSER->SCOPE, if the `::' token is
31070 present, and NULL_TREE otherwise. */
31073 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
31077 /* Peek at the next token. */
31078 token
= cp_lexer_peek_token (parser
->lexer
);
31079 /* If we're looking at a `::' token then we're starting from the
31080 global namespace, not our current location. */
31081 if (token
->type
== CPP_SCOPE
)
31083 /* Consume the `::' token. */
31084 cp_lexer_consume_token (parser
->lexer
);
31085 /* Set the SCOPE so that we know where to start the lookup. */
31086 parser
->scope
= global_namespace
;
31087 parser
->qualifying_scope
= global_namespace
;
31088 parser
->object_scope
= NULL_TREE
;
31090 return parser
->scope
;
31092 else if (!current_scope_valid_p
)
31094 parser
->scope
= NULL_TREE
;
31095 parser
->qualifying_scope
= NULL_TREE
;
31096 parser
->object_scope
= NULL_TREE
;
31102 /* Returns TRUE if the upcoming token sequence is the start of a
31103 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
31104 declarator is preceded by the `friend' specifier. The parser flags FLAGS
31105 is used to control type-specifier parsing. */
31108 cp_parser_constructor_declarator_p (cp_parser
*parser
, cp_parser_flags flags
,
31111 bool constructor_p
;
31112 bool outside_class_specifier_p
;
31113 tree nested_name_specifier
;
31114 cp_token
*next_token
;
31116 /* The common case is that this is not a constructor declarator, so
31117 try to avoid doing lots of work if at all possible. It's not
31118 valid declare a constructor at function scope. */
31119 if (parser
->in_function_body
)
31121 /* And only certain tokens can begin a constructor declarator. */
31122 next_token
= cp_lexer_peek_token (parser
->lexer
);
31123 if (next_token
->type
!= CPP_NAME
31124 && next_token
->type
!= CPP_SCOPE
31125 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
31126 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
31127 declarator-id of a constructor or destructor. */
31128 && (next_token
->type
!= CPP_TEMPLATE_ID
|| cxx_dialect
>= cxx20
))
31131 /* Parse tentatively; we are going to roll back all of the tokens
31133 cp_parser_parse_tentatively (parser
);
31134 /* Assume that we are looking at a constructor declarator. */
31135 constructor_p
= true;
31137 /* Look for the optional `::' operator. */
31138 cp_parser_global_scope_opt (parser
,
31139 /*current_scope_valid_p=*/false);
31140 /* Look for the nested-name-specifier. */
31141 nested_name_specifier
31142 = (cp_parser_nested_name_specifier_opt (parser
,
31143 /*typename_keyword_p=*/false,
31144 /*check_dependency_p=*/false,
31146 /*is_declaration=*/false));
31148 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
31149 if (nested_name_specifier
31150 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
31152 tree s
= resolve_typename_type (nested_name_specifier
,
31153 /*only_current_p=*/false);
31154 if (TREE_CODE (s
) != TYPENAME_TYPE
)
31155 nested_name_specifier
= s
;
31158 outside_class_specifier_p
= (!at_class_scope_p ()
31159 || !TYPE_BEING_DEFINED (current_class_type
)
31162 /* Outside of a class-specifier, there must be a
31163 nested-name-specifier. Except in C++17 mode, where we
31164 might be declaring a guiding declaration. */
31165 if (!nested_name_specifier
&& outside_class_specifier_p
31166 && cxx_dialect
< cxx17
)
31167 constructor_p
= false;
31168 else if (nested_name_specifier
== error_mark_node
)
31169 constructor_p
= false;
31171 /* If we have a class scope, this is easy; DR 147 says that S::S always
31172 names the constructor, and no other qualified name could. */
31173 if (constructor_p
&& nested_name_specifier
31174 && CLASS_TYPE_P (nested_name_specifier
))
31176 tree id
= cp_parser_unqualified_id (parser
,
31177 /*template_keyword_p=*/false,
31178 /*check_dependency_p=*/false,
31179 /*declarator_p=*/true,
31180 /*optional_p=*/false);
31181 if (is_overloaded_fn (id
))
31182 id
= DECL_NAME (get_first_fn (id
));
31183 if (!constructor_name_p (id
, nested_name_specifier
))
31184 constructor_p
= false;
31186 /* If we still think that this might be a constructor-declarator,
31187 look for a class-name. */
31188 else if (constructor_p
)
31192 template <typename T> struct S {
31196 we must recognize that the nested `S' names a class. */
31197 if (cxx_dialect
>= cxx17
)
31198 cp_parser_parse_tentatively (parser
);
31201 type_decl
= cp_parser_class_name (parser
,
31202 /*typename_keyword_p=*/false,
31203 /*template_keyword_p=*/false,
31205 /*check_dependency_p=*/false,
31206 /*class_head_p=*/false,
31207 /*is_declaration=*/false);
31209 if (cxx_dialect
>= cxx17
31210 && !cp_parser_parse_definitely (parser
))
31212 type_decl
= NULL_TREE
;
31213 tree tmpl
= cp_parser_template_name (parser
,
31214 /*template_keyword*/false,
31215 /*check_dependency_p*/false,
31216 /*is_declaration*/false,
31218 /*is_identifier*/NULL
);
31219 if (DECL_CLASS_TEMPLATE_P (tmpl
)
31220 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
31221 /* It's a deduction guide, return true. */;
31223 cp_parser_simulate_error (parser
);
31226 /* If there was no class-name, then this is not a constructor.
31227 Otherwise, if we are in a class-specifier and we aren't
31228 handling a friend declaration, check that its type matches
31229 current_class_type (c++/38313). Note: error_mark_node
31230 is left alone for error recovery purposes. */
31231 constructor_p
= (!cp_parser_error_occurred (parser
)
31232 && (outside_class_specifier_p
31233 || type_decl
== NULL_TREE
31234 || type_decl
== error_mark_node
31235 || same_type_p (current_class_type
,
31236 TREE_TYPE (type_decl
))));
31238 /* If we're still considering a constructor, we have to see a `(',
31239 to begin the parameter-declaration-clause, followed by either a
31240 `)', an `...', or a decl-specifier. We need to check for a
31241 type-specifier to avoid being fooled into thinking that:
31245 is a constructor. (It is actually a function named `f' that
31246 takes one parameter (of type `int') and returns a value of type
31249 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31250 constructor_p
= false;
31253 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
31254 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
31255 /* A parameter declaration begins with a decl-specifier,
31256 which is either the "attribute" keyword, a storage class
31257 specifier, or (usually) a type-specifier. */
31258 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
31259 /* GNU attributes can actually appear both at the start of
31260 a parameter and parenthesized declarator.
31261 S (__attribute__((unused)) int);
31262 is a constructor, but
31263 S (__attribute__((unused)) foo) (int);
31264 is a function declaration. [[attribute]] can appear in the
31265 first form too, but not in the second form. */
31266 && !cp_next_tokens_can_be_std_attribute_p (parser
))
31269 tree pushed_scope
= NULL_TREE
;
31270 unsigned saved_num_template_parameter_lists
;
31272 if (cp_parser_allow_gnu_extensions_p (parser
)
31273 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
31275 unsigned int n
= cp_parser_skip_gnu_attributes_opt (parser
, 1);
31277 cp_lexer_consume_token (parser
->lexer
);
31280 /* Names appearing in the type-specifier should be looked up
31281 in the scope of the class. */
31282 if (current_class_type
)
31284 else if (type_decl
)
31286 type
= TREE_TYPE (type_decl
);
31287 if (TREE_CODE (type
) == TYPENAME_TYPE
)
31289 type
= resolve_typename_type (type
,
31290 /*only_current_p=*/false);
31291 if (TREE_CODE (type
) == TYPENAME_TYPE
)
31293 cp_parser_abort_tentative_parse (parser
);
31297 pushed_scope
= push_scope (type
);
31300 /* Inside the constructor parameter list, surrounding
31301 template-parameter-lists do not apply. */
31302 saved_num_template_parameter_lists
31303 = parser
->num_template_parameter_lists
;
31304 parser
->num_template_parameter_lists
= 0;
31306 /* Look for the type-specifier. It's not optional, but its typename
31307 might be. Unless this is a friend declaration; we don't want to
31310 friend S (T::fn)(int);
31312 as a constructor, but with P0634, we might assume a type when
31313 looking for the type-specifier. It is actually a function named
31314 `T::fn' that takes one parameter (of type `int') and returns a
31315 value of type `S'. Constructors can be friends, but they must
31316 use a qualified name.
31318 Parse with an empty set of declaration specifiers since we're
31319 trying to match a decl-specifier-seq of the first parameter.
31320 This must be non-null so that cp_parser_simple_type_specifier
31321 will recognize a constrained placeholder type such as:
31322 'C<int> auto' where C is a type concept. */
31323 cp_decl_specifier_seq ctor_specs
;
31324 clear_decl_specs (&ctor_specs
);
31325 cp_parser_type_specifier (parser
,
31326 (friend_p
? CP_PARSER_FLAGS_NONE
31327 : (flags
& ~CP_PARSER_FLAGS_OPTIONAL
)),
31328 /*decl_specs=*/&ctor_specs
,
31329 /*is_declarator=*/true,
31330 /*declares_class_or_enum=*/NULL
,
31331 /*is_cv_qualifier=*/NULL
);
31333 parser
->num_template_parameter_lists
31334 = saved_num_template_parameter_lists
;
31336 /* Leave the scope of the class. */
31338 pop_scope (pushed_scope
);
31340 constructor_p
= !cp_parser_error_occurred (parser
);
31344 /* We did not really want to consume any tokens. */
31345 cp_parser_abort_tentative_parse (parser
);
31347 return constructor_p
;
31350 /* Parse the definition of the function given by the DECL_SPECIFIERS,
31351 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
31352 they must be performed once we are in the scope of the function.
31354 Returns the function defined. */
31357 cp_parser_function_definition_from_specifiers_and_declarator
31358 (cp_parser
* parser
,
31359 cp_decl_specifier_seq
*decl_specifiers
,
31361 const cp_declarator
*declarator
)
31366 /* Begin the function-definition. */
31367 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
31369 /* The things we're about to see are not directly qualified by any
31370 template headers we've seen thus far. */
31371 reset_specialization ();
31373 /* If there were names looked up in the decl-specifier-seq that we
31374 did not check, check them now. We must wait until we are in the
31375 scope of the function to perform the checks, since the function
31376 might be a friend. */
31377 perform_deferred_access_checks (tf_warning_or_error
);
31381 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
31382 parser
->omp_declare_simd
= NULL
;
31383 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
31384 parser
->oacc_routine
= NULL
;
31389 /* Skip the entire function. */
31390 cp_parser_skip_to_end_of_block_or_statement (parser
);
31391 fn
= error_mark_node
;
31393 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
31395 /* Seen already, skip it. An error message has already been output. */
31396 cp_parser_skip_to_end_of_block_or_statement (parser
);
31397 fn
= current_function_decl
;
31398 current_function_decl
= NULL_TREE
;
31399 /* If this is a function from a class, pop the nested class. */
31400 if (current_class_name
)
31401 pop_nested_class ();
31405 auto_timevar
tv (DECL_DECLARED_INLINE_P (current_function_decl
)
31406 ? TV_PARSE_INLINE
: TV_PARSE_FUNC
);
31407 fn
= cp_parser_function_definition_after_declarator (parser
,
31408 /*inline_p=*/false);
31414 /* Parse the part of a function-definition that follows the
31415 declarator. INLINE_P is TRUE iff this function is an inline
31416 function defined within a class-specifier.
31418 Returns the function defined. */
31421 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
31425 bool saved_in_unbraced_linkage_specification_p
;
31426 bool saved_in_function_body
;
31427 unsigned saved_num_template_parameter_lists
;
31429 bool fully_implicit_function_template_p
31430 = parser
->fully_implicit_function_template_p
;
31431 parser
->fully_implicit_function_template_p
= false;
31432 tree implicit_template_parms
31433 = parser
->implicit_template_parms
;
31434 parser
->implicit_template_parms
= 0;
31435 cp_binding_level
* implicit_template_scope
31436 = parser
->implicit_template_scope
;
31437 parser
->implicit_template_scope
= 0;
31439 saved_in_function_body
= parser
->in_function_body
;
31440 parser
->in_function_body
= true;
31441 /* If the next token is `return', then the code may be trying to
31442 make use of the "named return value" extension that G++ used to
31444 token
= cp_lexer_peek_token (parser
->lexer
);
31445 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
31447 /* Consume the `return' keyword. */
31448 cp_lexer_consume_token (parser
->lexer
);
31449 /* Look for the identifier that indicates what value is to be
31451 cp_parser_identifier (parser
);
31452 /* Issue an error message. */
31453 error_at (token
->location
,
31454 "named return values are no longer supported");
31455 /* Skip tokens until we reach the start of the function body. */
31458 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31459 if (token
->type
== CPP_OPEN_BRACE
31460 || token
->type
== CPP_EOF
31461 || token
->type
== CPP_PRAGMA_EOL
)
31463 cp_lexer_consume_token (parser
->lexer
);
31466 /* The `extern' in `extern "C" void f () { ... }' does not apply to
31467 anything declared inside `f'. */
31468 saved_in_unbraced_linkage_specification_p
31469 = parser
->in_unbraced_linkage_specification_p
;
31470 parser
->in_unbraced_linkage_specification_p
= false;
31471 /* Inside the function, surrounding template-parameter-lists do not
31473 saved_num_template_parameter_lists
31474 = parser
->num_template_parameter_lists
;
31475 parser
->num_template_parameter_lists
= 0;
31477 /* If the next token is `try', `__transaction_atomic', or
31478 `__transaction_relaxed`, then we are looking at either function-try-block
31479 or function-transaction-block. Note that all of these include the
31481 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
31482 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
31483 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
31484 RID_TRANSACTION_RELAXED
))
31485 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
31486 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
31487 cp_parser_function_try_block (parser
);
31489 cp_parser_ctor_initializer_opt_and_function_body
31490 (parser
, /*in_function_try_block=*/false);
31492 /* Finish the function. */
31493 fn
= finish_function (inline_p
);
31497 && TYPE_P (DECL_CONTEXT (fn
))
31498 && (DECL_DECLARED_INLINE_P (fn
)
31499 || processing_template_decl
))
31500 set_defining_module (fn
);
31502 /* Generate code for it, if necessary. */
31503 expand_or_defer_fn (fn
);
31504 /* Restore the saved values. */
31505 parser
->in_unbraced_linkage_specification_p
31506 = saved_in_unbraced_linkage_specification_p
;
31507 parser
->num_template_parameter_lists
31508 = saved_num_template_parameter_lists
;
31509 parser
->in_function_body
= saved_in_function_body
;
31511 parser
->fully_implicit_function_template_p
31512 = fully_implicit_function_template_p
;
31513 parser
->implicit_template_parms
31514 = implicit_template_parms
;
31515 parser
->implicit_template_scope
31516 = implicit_template_scope
;
31518 if (parser
->fully_implicit_function_template_p
)
31519 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
31524 /* Parse a template-declaration body (following argument list). */
31527 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
31528 tree parameter_list
,
31531 tree decl
= NULL_TREE
;
31532 bool friend_p
= false;
31534 /* We just processed one more parameter list. */
31535 ++parser
->num_template_parameter_lists
;
31537 /* Get the deferred access checks from the parameter list. These
31538 will be checked once we know what is being declared, as for a
31539 member template the checks must be performed in the scope of the
31540 class containing the member. */
31541 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
31543 /* Tentatively parse for a new template parameter list, which can either be
31544 the template keyword or a template introduction. */
31545 if (cp_parser_template_declaration_after_export (parser
, member_p
))
31547 else if (cxx_dialect
>= cxx11
31548 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
31549 decl
= cp_parser_alias_declaration (parser
);
31550 else if (cxx_dialect
>= cxx20
/* Implies flag_concept. */
31551 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
)
31552 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_BOOL
))
31553 /* Allow 'concept bool' to be handled as per the TS. */
31554 decl
= cp_parser_concept_definition (parser
);
31557 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31558 decl
= cp_parser_single_declaration (parser
,
31561 /*explicit_specialization_p=*/false,
31564 /* If this is a member template declaration, let the front
31566 if (member_p
&& !friend_p
&& decl
)
31568 if (TREE_CODE (decl
) == TYPE_DECL
)
31569 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
31571 decl
= finish_member_template_decl (decl
);
31573 else if (friend_p
&& decl
31574 && DECL_DECLARES_TYPE_P (decl
))
31575 make_friend_class (current_class_type
, TREE_TYPE (decl
),
31576 /*complain=*/true);
31578 /* We are done with the current parameter list. */
31579 --parser
->num_template_parameter_lists
;
31581 pop_deferring_access_checks ();
31584 finish_template_decl (parameter_list
);
31586 /* Check the template arguments for a literal operator template. */
31588 && DECL_DECLARES_FUNCTION_P (decl
)
31589 && UDLIT_OPER_P (DECL_NAME (decl
)))
31592 if (parameter_list
== NULL_TREE
)
31596 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
31597 if (num_parms
== 1)
31599 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
31600 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
31601 if (TREE_CODE (parm
) != PARM_DECL
)
31603 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm
))
31604 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
31605 /* OK, C++20 string literal operator template. We don't need
31606 to warn in lower dialects here because we will have already
31607 warned about the template parameter. */;
31608 else if (TREE_TYPE (parm
) != char_type_node
31609 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
31612 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
31614 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
31615 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
31616 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
31617 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
31618 if (TREE_CODE (parm
) != PARM_DECL
31619 || TREE_TYPE (parm
) != TREE_TYPE (type
)
31620 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
31623 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
31624 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
31625 "ISO C++ did not adopt string literal operator templa"
31626 "tes taking an argument pack of characters");
31633 if (cxx_dialect
> cxx17
)
31634 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
31635 "template %qD has invalid parameter list; expected "
31636 "non-type template parameter pack %<<char...>%> or "
31637 "single non-type parameter of class type",
31640 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
31641 "template %qD has invalid parameter list; expected "
31642 "non-type template parameter pack %<<char...>%>",
31647 /* Register member declarations. */
31648 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
31649 finish_member_declaration (decl
);
31650 /* If DECL is a function template, we must return to parse it later.
31651 (Even though there is no definition, there might be default
31652 arguments that need handling.) */
31653 if (member_p
&& decl
31654 && DECL_DECLARES_FUNCTION_P (decl
))
31655 vec_safe_push (unparsed_funs_with_definitions
, decl
);
31658 /* Parse a template introduction header for a template-declaration. Returns
31659 false if tentative parse fails. */
31662 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
31664 cp_parser_parse_tentatively (parser
);
31666 tree saved_scope
= parser
->scope
;
31667 tree saved_object_scope
= parser
->object_scope
;
31668 tree saved_qualifying_scope
= parser
->qualifying_scope
;
31669 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31671 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
31673 /* In classes don't parse valid unnamed bitfields as invalid
31674 template introductions. */
31676 parser
->colon_corrects_to_scope_p
= false;
31678 /* Look for the optional `::' operator. */
31679 cp_parser_global_scope_opt (parser
,
31680 /*current_scope_valid_p=*/false);
31681 /* Look for the nested-name-specifier. */
31682 cp_parser_nested_name_specifier_opt (parser
,
31683 /*typename_keyword_p=*/false,
31684 /*check_dependency_p=*/true,
31686 /*is_declaration=*/false);
31688 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31689 tree concept_name
= cp_parser_identifier (parser
);
31691 /* Look up the concept for which we will be matching
31692 template parameters. */
31693 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
31695 parser
->scope
= saved_scope
;
31696 parser
->object_scope
= saved_object_scope
;
31697 parser
->qualifying_scope
= saved_qualifying_scope
;
31698 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31700 if (concept_name
== error_mark_node
31701 || (seen_error () && !concept_definition_p (tmpl_decl
)))
31702 cp_parser_simulate_error (parser
);
31704 /* Look for opening brace for introduction. */
31705 matching_braces braces
;
31706 braces
.require_open (parser
);
31707 location_t open_loc
= input_location
;
31709 if (!cp_parser_parse_definitely (parser
))
31712 push_deferring_access_checks (dk_deferred
);
31714 /* Build vector of placeholder parameters and grab
31715 matching identifiers. */
31716 tree introduction_list
= cp_parser_introduction_list (parser
);
31718 /* Look for closing brace for introduction. */
31719 if (!braces
.require_close (parser
))
31722 /* The introduction-list shall not be empty. */
31723 int nargs
= TREE_VEC_LENGTH (introduction_list
);
31726 /* In cp_parser_introduction_list we have already issued an error. */
31730 if (tmpl_decl
== error_mark_node
)
31732 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
31737 /* Build and associate the constraint. */
31738 location_t introduction_loc
= make_location (open_loc
,
31739 start_token
->location
,
31741 tree parms
= finish_template_introduction (tmpl_decl
,
31744 if (parms
&& parms
!= error_mark_node
)
31746 if (!flag_concepts_ts
)
31747 pedwarn (introduction_loc
, 0, "template-introductions"
31748 " are not part of C++20 concepts; use %qs to enable",
31751 cp_parser_template_declaration_after_parameters (parser
, parms
,
31756 if (parms
== NULL_TREE
)
31757 error_at (token
->location
, "no matching concept for template-introduction");
31762 /* Parse a normal template-declaration following the template keyword. */
31765 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
31767 tree parameter_list
;
31768 bool need_lang_pop
;
31769 location_t location
= input_location
;
31771 /* Look for the `<' token. */
31772 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
31774 if (at_class_scope_p () && current_function_decl
)
31776 /* 14.5.2.2 [temp.mem]
31778 A local class shall not have member templates. */
31779 error_at (location
,
31780 "invalid declaration of member template in local class");
31781 cp_parser_skip_to_end_of_block_or_statement (parser
);
31786 A template ... shall not have C linkage. */
31787 if (current_lang_name
== lang_name_c
)
31789 error_at (location
, "template with C linkage");
31790 maybe_show_extern_c_location ();
31791 /* Give it C++ linkage to avoid confusing other parts of the
31793 push_lang_context (lang_name_cplusplus
);
31794 need_lang_pop
= true;
31797 need_lang_pop
= false;
31799 /* We cannot perform access checks on the template parameter
31800 declarations until we know what is being declared, just as we
31801 cannot check the decl-specifier list. */
31802 push_deferring_access_checks (dk_deferred
);
31804 /* If the next token is `>', then we have an invalid
31805 specialization. Rather than complain about an invalid template
31806 parameter, issue an error message here. */
31807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
31809 cp_parser_error (parser
, "invalid explicit specialization");
31810 begin_specialization ();
31811 parameter_list
= NULL_TREE
;
31815 /* Parse the template parameters. */
31816 parameter_list
= cp_parser_template_parameter_list (parser
);
31819 /* Look for the `>'. */
31820 cp_parser_require_end_of_template_parameter_list (parser
);
31822 /* Manage template requirements */
31825 tree reqs
= get_shorthand_constraints (current_template_parms
);
31826 if (tree treqs
= cp_parser_requires_clause_opt (parser
, false))
31827 reqs
= combine_constraint_expressions (reqs
, treqs
);
31828 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
31831 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
31834 /* For the erroneous case of a template with C linkage, we pushed an
31835 implicit C++ linkage scope; exit that scope now. */
31837 pop_lang_context ();
31840 /* Parse a template-declaration, assuming that the `export' (and
31841 `extern') keywords, if present, has already been scanned. MEMBER_P
31842 is as for cp_parser_template_declaration. */
31845 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
31847 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
31849 cp_lexer_consume_token (parser
->lexer
);
31850 cp_parser_explicit_template_declaration (parser
, member_p
);
31853 else if (flag_concepts
)
31854 return cp_parser_template_introduction (parser
, member_p
);
31859 /* Perform the deferred access checks from a template-parameter-list.
31860 CHECKS is a TREE_LIST of access checks, as returned by
31861 get_deferred_access_checks. */
31864 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
31866 ++processing_template_parmlist
;
31867 perform_access_checks (checks
, tf_warning_or_error
);
31868 --processing_template_parmlist
;
31871 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
31872 `function-definition' sequence that follows a template header.
31873 If MEMBER_P is true, this declaration appears in a class scope.
31875 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
31876 *FRIEND_P is set to TRUE iff the declaration is a friend. */
31879 cp_parser_single_declaration (cp_parser
* parser
,
31880 vec
<deferred_access_check
, va_gc
> *checks
,
31882 bool explicit_specialization_p
,
31885 int declares_class_or_enum
;
31886 tree decl
= NULL_TREE
;
31887 cp_decl_specifier_seq decl_specifiers
;
31888 bool function_definition_p
= false;
31889 cp_token
*decl_spec_token_start
;
31891 /* This function is only used when processing a template
31893 gcc_assert (innermost_scope_kind () == sk_template_parms
31894 || innermost_scope_kind () == sk_template_spec
);
31896 /* Defer access checks until we know what is being declared. */
31897 push_deferring_access_checks (dk_deferred
);
31899 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
31901 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
31902 cp_parser_decl_specifier_seq (parser
,
31903 (CP_PARSER_FLAGS_OPTIONAL
31904 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
31906 &declares_class_or_enum
);
31908 cp_omp_declare_simd_data odsd
;
31909 if (decl_specifiers
.attributes
&& (flag_openmp
|| flag_openmp_simd
))
31910 cp_parser_handle_directive_omp_attributes (parser
,
31911 &decl_specifiers
.attributes
,
31915 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
31917 /* There are no template typedefs. */
31918 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
31920 error_at (decl_spec_token_start
->location
,
31921 "template declaration of %<typedef%>");
31922 decl
= error_mark_node
;
31925 /* Gather up the access checks that occurred the
31926 decl-specifier-seq. */
31927 stop_deferring_access_checks ();
31929 /* Check for the declaration of a template class. */
31930 if (declares_class_or_enum
)
31932 if (cp_parser_declares_only_class_p (parser
)
31933 || (declares_class_or_enum
& 2))
31935 decl
= shadow_tag (&decl_specifiers
);
31940 friend template <typename T> struct A<T>::B;
31943 A<T>::B will be represented by a TYPENAME_TYPE, and
31944 therefore not recognized by shadow_tag. */
31945 if (friend_p
&& *friend_p
31947 && decl_specifiers
.type
31948 && TYPE_P (decl_specifiers
.type
))
31949 decl
= decl_specifiers
.type
;
31951 if (decl
&& decl
!= error_mark_node
)
31952 decl
= TYPE_NAME (decl
);
31954 decl
= error_mark_node
;
31956 /* If this is a declaration, but not a definition, associate
31957 any constraints with the type declaration. Constraints
31958 are associated with definitions in cp_parser_class_specifier. */
31959 if (declares_class_or_enum
== 1)
31960 associate_classtype_constraints (TREE_TYPE (decl
));
31962 /* Perform access checks for template parameters. */
31963 cp_parser_perform_template_parameter_access_checks (checks
);
31965 /* Give a helpful diagnostic for
31966 template <class T> struct A { } a;
31967 if we aren't already recovering from an error. */
31968 if (!cp_parser_declares_only_class_p (parser
)
31971 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31972 "a class template declaration must not declare "
31974 cp_parser_skip_to_end_of_block_or_statement (parser
);
31980 /* Complain about missing 'typename' or other invalid type names. */
31981 if (!decl_specifiers
.any_type_specifiers_p
31982 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
31984 /* cp_parser_parse_and_diagnose_invalid_type_name calls
31985 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
31986 the rest of this declaration. */
31987 decl
= error_mark_node
;
31991 /* If it's not a template class, try for a template function. If
31992 the next token is a `;', then this declaration does not declare
31993 anything. But, if there were errors in the decl-specifiers, then
31994 the error might well have come from an attempted class-specifier.
31995 In that case, there's no need to warn about a missing declarator. */
31997 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
31998 || decl_specifiers
.type
!= error_mark_node
))
32000 int flags
= CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
32001 /* We don't delay parsing for friends, though CWG 2510 may change
32003 if (member_p
&& !(friend_p
&& *friend_p
))
32004 flags
|= CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
32005 decl
= cp_parser_init_declarator (parser
,
32009 /*function_definition_allowed_p=*/true,
32011 declares_class_or_enum
,
32012 &function_definition_p
,
32015 /* 7.1.1-1 [dcl.stc]
32017 A storage-class-specifier shall not be specified in an explicit
32018 specialization... */
32020 && explicit_specialization_p
32021 && decl_specifiers
.storage_class
!= sc_none
)
32023 error_at (decl_spec_token_start
->location
,
32024 "explicit template specialization cannot have a storage class");
32025 decl
= error_mark_node
;
32028 if (decl
&& VAR_P (decl
))
32029 check_template_variable (decl
);
32032 /* Look for a trailing `;' after the declaration. */
32033 if (!function_definition_p
32034 && (decl
== error_mark_node
32035 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
32036 cp_parser_skip_to_end_of_block_or_statement (parser
);
32039 pop_deferring_access_checks ();
32041 /* Clear any current qualification; whatever comes next is the start
32042 of something new. */
32043 parser
->scope
= NULL_TREE
;
32044 parser
->qualifying_scope
= NULL_TREE
;
32045 parser
->object_scope
= NULL_TREE
;
32047 cp_finalize_omp_declare_simd (parser
, &odsd
);
32052 /* Parse a cast-expression that is not the operand of a unary "&". */
32055 cp_parser_simple_cast_expression (cp_parser
*parser
)
32057 return cp_parser_cast_expression (parser
, /*address_p=*/false,
32058 /*cast_p=*/false, /*decltype*/false, NULL
);
32061 /* Parse a functional cast to TYPE. Returns an expression
32062 representing the cast. */
32065 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
32067 vec
<tree
, va_gc
> *vec
;
32068 tree expression_list
;
32072 location_t start_loc
= input_location
;
32075 type
= error_mark_node
;
32077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
32079 cp_lexer_set_source_position (parser
->lexer
);
32080 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
32081 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
32082 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
32083 if (TREE_CODE (type
) == TYPE_DECL
)
32084 type
= TREE_TYPE (type
);
32086 cast
= finish_compound_literal (type
, expression_list
,
32087 tf_warning_or_error
, fcl_functional
);
32088 /* Create a location of the form:
32091 with caret == start at the start of the type name,
32092 finishing at the closing brace. */
32093 location_t combined_loc
= make_location (start_loc
, start_loc
,
32095 cast
.set_location (combined_loc
);
32100 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32102 /*allow_expansion_p=*/true,
32103 /*non_constant_p=*/NULL
);
32105 expression_list
= error_mark_node
;
32108 expression_list
= build_tree_list_vec (vec
);
32109 release_tree_vector (vec
);
32112 /* Create a location of the form:
32115 with caret == start at the start of the type name,
32116 finishing at the closing paren. */
32117 location_t combined_loc
= make_location (start_loc
, start_loc
,
32119 cast
= build_functional_cast (combined_loc
, type
, expression_list
,
32120 tf_warning_or_error
);
32122 /* [expr.const]/1: In an integral constant expression "only type
32123 conversions to integral or enumeration type can be used". */
32124 if (TREE_CODE (type
) == TYPE_DECL
)
32125 type
= TREE_TYPE (type
);
32126 if (cast
!= error_mark_node
32127 && !cast_valid_in_integral_constant_expression_p (type
)
32128 && cp_parser_non_integral_constant_expression (parser
,
32130 return error_mark_node
;
32135 /* Save the tokens that make up the body of a member function defined
32136 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
32137 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
32138 specifiers applied to the declaration. Returns the FUNCTION_DECL
32139 for the member function. */
32142 cp_parser_save_member_function_body (cp_parser
* parser
,
32143 cp_decl_specifier_seq
*decl_specifiers
,
32144 cp_declarator
*declarator
,
32150 bool function_try_block
= false;
32152 /* Create the FUNCTION_DECL. */
32153 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
32154 cp_finalize_omp_declare_simd (parser
, fn
);
32155 cp_finalize_oacc_routine (parser
, fn
, true);
32156 /* If something went badly wrong, bail out now. */
32157 if (fn
== error_mark_node
)
32159 /* If there's a function-body, skip it. */
32160 if (cp_parser_token_starts_function_definition_p
32161 (cp_lexer_peek_token (parser
->lexer
)))
32162 cp_parser_skip_to_end_of_block_or_statement (parser
);
32163 return error_mark_node
;
32166 /* Remember it, if there are default args to post process. */
32167 cp_parser_save_default_args (parser
, fn
);
32169 /* Save away the tokens that make up the body of the
32171 first
= parser
->lexer
->next_token
;
32173 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
32174 cp_lexer_consume_token (parser
->lexer
);
32175 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
32176 RID_TRANSACTION_ATOMIC
))
32178 cp_lexer_consume_token (parser
->lexer
);
32179 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
32180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
32181 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
32182 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
32183 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
32184 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
32185 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
32187 cp_lexer_consume_token (parser
->lexer
);
32188 cp_lexer_consume_token (parser
->lexer
);
32189 cp_lexer_consume_token (parser
->lexer
);
32190 cp_lexer_consume_token (parser
->lexer
);
32191 cp_lexer_consume_token (parser
->lexer
);
32194 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
32195 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
32197 cp_lexer_consume_token (parser
->lexer
);
32198 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
32203 /* Handle function try blocks. */
32204 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
32206 cp_lexer_consume_token (parser
->lexer
);
32207 function_try_block
= true;
32209 /* We can have braced-init-list mem-initializers before the fn body. */
32210 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32212 cp_lexer_consume_token (parser
->lexer
);
32213 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
32215 /* cache_group will stop after an un-nested { } pair, too. */
32216 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
32219 /* variadic mem-inits have ... after the ')'. */
32220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
32221 cp_lexer_consume_token (parser
->lexer
);
32224 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
32225 /* Handle function try blocks. */
32226 if (function_try_block
)
32227 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
32228 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
32229 last
= parser
->lexer
->next_token
;
32231 /* Save away the inline definition; we will process it when the
32232 class is complete. */
32233 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
32234 DECL_PENDING_INLINE_P (fn
) = 1;
32236 /* We need to know that this was defined in the class, so that
32237 friend templates are handled correctly. */
32238 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
32240 /* Add FN to the queue of functions to be parsed later. */
32241 vec_safe_push (unparsed_funs_with_definitions
, fn
);
32246 /* Save the tokens that make up the in-class initializer for a non-static
32247 data member. Returns a DEFERRED_PARSE. */
32250 cp_parser_save_nsdmi (cp_parser
* parser
)
32252 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
32255 /* Parse a template-argument-list, as well as the trailing ">" (but
32256 not the opening "<"). See cp_parser_template_argument_list for the
32260 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
32264 tree saved_qualifying_scope
;
32265 tree saved_object_scope
;
32266 bool saved_greater_than_is_operator_p
;
32270 When parsing a template-id, the first non-nested `>' is taken as
32271 the end of the template-argument-list rather than a greater-than
32273 saved_greater_than_is_operator_p
32274 = parser
->greater_than_is_operator_p
;
32275 parser
->greater_than_is_operator_p
= false;
32276 /* Parsing the argument list may modify SCOPE, so we save it
32278 saved_scope
= parser
->scope
;
32279 saved_qualifying_scope
= parser
->qualifying_scope
;
32280 saved_object_scope
= parser
->object_scope
;
32281 /* We need to evaluate the template arguments, even though this
32282 template-id may be nested within a "sizeof". */
32284 /* Parse the template-argument-list itself. */
32285 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
32286 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
)
32287 || cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER_EQ
)
32288 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT_EQ
))
32290 arguments
= make_tree_vec (0);
32291 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments
, 0);
32294 arguments
= cp_parser_template_argument_list (parser
);
32295 /* Look for the `>' that ends the template-argument-list. If we find
32296 a '>>' instead, it's probably just a typo. */
32297 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
32299 if (cxx_dialect
!= cxx98
)
32301 /* In C++0x, a `>>' in a template argument list or cast
32302 expression is considered to be two separate `>'
32303 tokens. So, change the current token to a `>', but don't
32304 consume it: it will be consumed later when the outer
32305 template argument list (or cast expression) is parsed.
32306 Note that this replacement of `>' for `>>' is necessary
32307 even if we are parsing tentatively: in the tentative
32308 case, after calling
32309 cp_parser_enclosed_template_argument_list we will always
32310 throw away all of the template arguments and the first
32311 closing `>', either because the template argument list
32312 was erroneous or because we are replacing those tokens
32313 with a CPP_TEMPLATE_ID token. The second `>' (which will
32314 not have been thrown away) is needed either to close an
32315 outer template argument list or to complete a new-style
32317 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32318 token
->type
= CPP_GREATER
;
32320 else if (!saved_greater_than_is_operator_p
)
32322 /* If we're in a nested template argument list, the '>>' has
32323 to be a typo for '> >'. We emit the error message, but we
32324 continue parsing and we push a '>' as next token, so that
32325 the argument list will be parsed correctly. Note that the
32326 global source location is still on the token before the
32327 '>>', so we need to say explicitly where we want it. */
32328 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32329 gcc_rich_location
richloc (token
->location
);
32330 richloc
.add_fixit_replace ("> >");
32331 error_at (&richloc
, "%<>>%> should be %<> >%> "
32332 "within a nested template argument list");
32334 token
->type
= CPP_GREATER
;
32338 /* If this is not a nested template argument list, the '>>'
32339 is a typo for '>'. Emit an error message and continue.
32340 Same deal about the token location, but here we can get it
32341 right by consuming the '>>' before issuing the diagnostic. */
32342 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
32343 error_at (token
->location
,
32344 "spurious %<>>%>, use %<>%> to terminate "
32345 "a template argument list");
32348 /* Similarly for >>= and >=. */
32349 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER_EQ
)
32350 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT_EQ
))
32352 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
32353 gcc_rich_location
richloc (token
->location
);
32354 enum cpp_ttype new_type
;
32355 const char *replacement
;
32356 if (token
->type
== CPP_GREATER_EQ
)
32358 replacement
= "> =";
32361 else if (!saved_greater_than_is_operator_p
)
32363 if (cxx_dialect
!= cxx98
)
32364 replacement
= ">> =";
32366 replacement
= "> > =";
32367 new_type
= CPP_GREATER
;
32371 replacement
= "> >=";
32372 new_type
= CPP_GREATER_EQ
;
32374 richloc
.add_fixit_replace (replacement
);
32375 error_at (&richloc
, "%qs should be %qs to terminate a template "
32377 cpp_type2name (token
->type
, token
->flags
), replacement
);
32378 token
->type
= new_type
;
32381 cp_parser_require_end_of_template_parameter_list (parser
);
32382 /* The `>' token might be a greater-than operator again now. */
32383 parser
->greater_than_is_operator_p
32384 = saved_greater_than_is_operator_p
;
32385 /* Restore the SAVED_SCOPE. */
32386 parser
->scope
= saved_scope
;
32387 parser
->qualifying_scope
= saved_qualifying_scope
;
32388 parser
->object_scope
= saved_object_scope
;
32393 /* MEMBER_FUNCTION is a member function, or a friend. If default
32394 arguments, or the body of the function have not yet been parsed,
32398 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
32400 auto_timevar
tv (TV_PARSE_INMETH
);
32402 /* If this member is a template, get the underlying
32404 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
32405 member_function
= DECL_TEMPLATE_RESULT (member_function
);
32407 /* There should not be any class definitions in progress at this
32408 point; the bodies of members are only parsed outside of all class
32410 gcc_assert (parser
->num_classes_being_defined
== 0);
32411 /* While we're parsing the member functions we might encounter more
32412 classes. We want to handle them right away, but we don't want
32413 them getting mixed up with functions that are currently in the
32415 push_unparsed_function_queues (parser
);
32417 /* Make sure that any template parameters are in scope. */
32418 maybe_begin_member_template_processing (member_function
);
32420 /* If the body of the function has not yet been parsed, parse it
32421 now. Except if the tokens have been purged (PR c++/39751). */
32422 if (DECL_PENDING_INLINE_P (member_function
)
32423 && !DECL_PENDING_INLINE_INFO (member_function
)->first
->purged_p
)
32425 tree function_scope
;
32426 cp_token_cache
*tokens
;
32428 /* The function is no longer pending; we are processing it. */
32429 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
32430 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
32431 DECL_PENDING_INLINE_P (member_function
) = 0;
32433 /* If this is a local class, enter the scope of the containing
32435 function_scope
= current_function_decl
;
32436 if (function_scope
)
32437 push_function_context ();
32439 /* Push the body of the function onto the lexer stack. */
32440 cp_parser_push_lexer_for_tokens (parser
, tokens
);
32442 /* Let the front end know that we going to be defining this
32444 start_preparsed_function (member_function
, NULL_TREE
,
32445 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
32447 /* #pragma omp declare reduction needs special parsing. */
32448 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
32450 parser
->lexer
->in_pragma
= true;
32451 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
32452 finish_function (/*inline_p=*/true);
32453 cp_check_omp_declare_reduction (member_function
);
32456 /* Now, parse the body of the function. */
32457 cp_parser_function_definition_after_declarator (parser
,
32458 /*inline_p=*/true);
32460 /* Leave the scope of the containing function. */
32461 if (function_scope
)
32462 pop_function_context ();
32463 cp_parser_pop_lexer (parser
);
32466 /* Remove any template parameters from the symbol table. */
32467 maybe_end_member_template_processing ();
32469 /* Restore the queue. */
32470 pop_unparsed_function_queues (parser
);
32473 /* If DECL contains any default args, remember it on the unparsed
32474 functions queue. */
32477 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
32481 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
32483 probe
= TREE_CHAIN (probe
))
32484 if (TREE_PURPOSE (probe
))
32486 cp_default_arg_entry entry
= {current_class_type
, decl
};
32487 vec_safe_push (unparsed_funs_with_default_args
, entry
);
32491 /* Remember if there is a noexcept-specifier to post process. */
32492 tree spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
32493 if (UNPARSED_NOEXCEPT_SPEC_P (spec
))
32494 vec_safe_push (unparsed_noexcepts
, decl
);
32497 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
32498 which is either a FIELD_DECL or PARM_DECL. Parse it and return
32499 the result. For a PARM_DECL, PARMTYPE is the corresponding type
32500 from the parameter-type-list. */
32503 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
32504 tree default_arg
, tree parmtype
)
32506 cp_token_cache
*tokens
;
32510 if (default_arg
== error_mark_node
)
32511 return error_mark_node
;
32513 /* Push the saved tokens for the default argument onto the parser's
32515 tokens
= DEFPARSE_TOKENS (default_arg
);
32516 cp_parser_push_lexer_for_tokens (parser
, tokens
);
32518 start_lambda_scope (decl
);
32520 /* Parse the default argument. */
32521 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
32522 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
32523 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
32525 finish_lambda_scope ();
32527 if (parsed_arg
== error_mark_node
)
32528 cp_parser_skip_to_end_of_statement (parser
);
32530 if (!processing_template_decl
)
32532 /* In a non-template class, check conversions now. In a template,
32533 we'll wait and instantiate these as needed. */
32534 if (TREE_CODE (decl
) == PARM_DECL
)
32535 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
32536 tf_warning_or_error
);
32537 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
32538 parsed_arg
= error_mark_node
;
32540 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
32543 /* If the token stream has not been completely used up, then
32544 there was extra junk after the end of the default
32546 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
32548 if (TREE_CODE (decl
) == PARM_DECL
)
32549 cp_parser_error (parser
, "expected %<,%>");
32551 cp_parser_error (parser
, "expected %<;%>");
32554 /* Revert to the main lexer. */
32555 cp_parser_pop_lexer (parser
);
32560 /* FIELD is a non-static data member with an initializer which we saved for
32561 later; parse it now. */
32564 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
32568 maybe_begin_member_template_processing (field
);
32570 push_unparsed_function_queues (parser
);
32571 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
32572 DECL_INITIAL (field
),
32574 pop_unparsed_function_queues (parser
);
32576 maybe_end_member_template_processing ();
32578 DECL_INITIAL (field
) = def
;
32581 /* FN is a FUNCTION_DECL which may contains a parameter with an
32582 unparsed DEFERRED_PARSE. Parse the default args now. This function
32583 assumes that the current scope is the scope in which the default
32584 argument should be processed. */
32587 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
32589 unsigned char saved_local_variables_forbidden_p
;
32591 /* While we're parsing the default args, we might (due to the
32592 statement expression extension) encounter more classes. We want
32593 to handle them right away, but we don't want them getting mixed
32594 up with default args that are currently in the queue. */
32595 push_unparsed_function_queues (parser
);
32597 /* Local variable names (and the `this' keyword) may not appear
32598 in a default argument. */
32599 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
32600 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
32602 push_defarg_context (fn
);
32604 begin_scope (sk_function_parms
, fn
);
32606 /* Gather the PARM_DECLs into a vec so we can keep track of them when
32607 pushdecl clears DECL_CHAIN. */
32608 releasing_vec parms
;
32609 for (tree parmdecl
= DECL_ARGUMENTS (fn
); parmdecl
;
32610 parmdecl
= DECL_CHAIN (parmdecl
))
32611 vec_safe_push (parms
, parmdecl
);
32613 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
32615 parm
&& parm
!= void_list_node
;
32616 parm
= TREE_CHAIN (parm
),
32619 tree default_arg
= TREE_PURPOSE (parm
);
32622 tree parmdecl
= parms
[i
];
32623 pushdecl (parmdecl
);
32628 if (TREE_CODE (default_arg
) != DEFERRED_PARSE
)
32629 /* This can happen for a friend declaration for a function
32630 already declared with default arguments. */
32634 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
32636 TREE_VALUE (parm
));
32637 TREE_PURPOSE (parm
) = parsed_arg
;
32639 /* Update any instantiations we've already created. */
32640 for (tree copy
: DEFPARSE_INSTANTIATIONS (default_arg
))
32641 TREE_PURPOSE (copy
) = parsed_arg
;
32644 pop_bindings_and_leave_scope ();
32646 /* Restore DECL_CHAINs after clobbering by pushdecl. */
32648 for (int i
= parms
->length () - 1; i
>= 0; --i
)
32650 DECL_CHAIN (parms
[i
]) = parm
;
32654 pop_defarg_context ();
32656 /* Make sure no default arg is missing. */
32657 check_default_args (fn
);
32659 /* Restore the state of local_variables_forbidden_p. */
32660 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
32662 /* Restore the queue. */
32663 pop_unparsed_function_queues (parser
);
32666 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
32668 sizeof ... ( identifier )
32670 where the 'sizeof' token has already been consumed. */
32673 cp_parser_sizeof_pack (cp_parser
*parser
)
32675 /* Consume the `...'. */
32676 cp_lexer_consume_token (parser
->lexer
);
32677 maybe_warn_variadic_templates ();
32679 matching_parens parens
;
32680 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
32682 parens
.consume_open (parser
);
32684 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
32685 "%<sizeof...%> argument must be surrounded by parentheses");
32687 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32688 tree name
= cp_parser_identifier (parser
);
32689 if (name
== error_mark_node
)
32690 return error_mark_node
;
32691 /* The name is not qualified. */
32692 parser
->scope
= NULL_TREE
;
32693 parser
->qualifying_scope
= NULL_TREE
;
32694 parser
->object_scope
= NULL_TREE
;
32695 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
32696 if (expr
== error_mark_node
)
32697 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
32699 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
32700 expr
= TREE_TYPE (expr
);
32701 else if (TREE_CODE (expr
) == CONST_DECL
)
32702 expr
= DECL_INITIAL (expr
);
32703 expr
= make_pack_expansion (expr
);
32704 if (expr
!= error_mark_node
)
32705 PACK_EXPANSION_SIZEOF_P (expr
) = true;
32708 parens
.require_close (parser
);
32713 /* Parse the operand of `sizeof' (or a similar operator). Returns
32714 either a TYPE or an expression, depending on the form of the
32715 input. The KEYWORD indicates which kind of expression we have
32719 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
32721 tree expr
= NULL_TREE
;
32722 const char *saved_message
;
32723 const char *saved_message_arg
;
32724 bool saved_integral_constant_expression_p
;
32725 bool saved_non_integral_constant_expression_p
;
32727 /* If it's a `...', then we are computing the length of a parameter
32729 if (keyword
== RID_SIZEOF
32730 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
32731 return cp_parser_sizeof_pack (parser
);
32733 /* Types cannot be defined in a `sizeof' expression. Save away the
32735 saved_message
= parser
->type_definition_forbidden_message
;
32736 saved_message_arg
= parser
->type_definition_forbidden_message_arg
;
32737 parser
->type_definition_forbidden_message
32738 = G_("types may not be defined in %qs expressions");
32739 parser
->type_definition_forbidden_message_arg
32740 = IDENTIFIER_POINTER (ridpointers
[keyword
]);
32742 /* The restrictions on constant-expressions do not apply inside
32743 sizeof expressions. */
32744 saved_integral_constant_expression_p
32745 = parser
->integral_constant_expression_p
;
32746 saved_non_integral_constant_expression_p
32747 = parser
->non_integral_constant_expression_p
;
32748 parser
->integral_constant_expression_p
= false;
32750 auto cleanup
= make_temp_override
32751 (parser
->auto_is_implicit_function_template_parm_p
, false);
32753 /* Do not actually evaluate the expression. */
32754 ++cp_unevaluated_operand
;
32755 ++c_inhibit_evaluation_warnings
;
32756 /* If it's a `(', then we might be looking at the type-id
32758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32760 tree type
= NULL_TREE
;
32762 tentative_firewall
firewall (parser
);
32764 /* We can't be sure yet whether we're looking at a type-id or an
32766 cp_parser_parse_tentatively (parser
);
32768 matching_parens parens
;
32769 parens
.consume_open (parser
);
32771 /* Note: as a GNU Extension, compound literals are considered
32772 postfix-expressions as they are in C99, so they are valid
32773 arguments to sizeof. See comment in cp_parser_cast_expression
32775 if (cp_parser_compound_literal_p (parser
))
32776 cp_parser_simulate_error (parser
);
32779 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
32780 parser
->in_type_id_in_expr_p
= true;
32781 /* Look for the type-id. */
32782 type
= cp_parser_type_id (parser
);
32783 /* Look for the closing `)'. */
32784 parens
.require_close (parser
);
32785 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
32788 /* If all went well, then we're done. */
32789 if (cp_parser_parse_definitely (parser
))
32793 /* Commit to the tentative_firewall so we get syntax errors. */
32794 cp_parser_commit_to_tentative_parse (parser
);
32796 expr
= cp_parser_unary_expression (parser
);
32800 expr
= cp_parser_unary_expression (parser
);
32802 /* Go back to evaluating expressions. */
32803 --cp_unevaluated_operand
;
32804 --c_inhibit_evaluation_warnings
;
32806 /* And restore the old one. */
32807 parser
->type_definition_forbidden_message
= saved_message
;
32808 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
32809 parser
->integral_constant_expression_p
32810 = saved_integral_constant_expression_p
;
32811 parser
->non_integral_constant_expression_p
32812 = saved_non_integral_constant_expression_p
;
32817 /* If the current declaration has no declarator, return true. */
32820 cp_parser_declares_only_class_p (cp_parser
*parser
)
32822 /* If the next token is a `;' or a `,' then there is no
32824 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
32825 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
32828 /* Update the DECL_SPECS to reflect the storage class indicated by
32832 cp_parser_set_storage_class (cp_parser
*parser
,
32833 cp_decl_specifier_seq
*decl_specs
,
32837 cp_storage_class storage_class
;
32839 if (parser
->in_unbraced_linkage_specification_p
)
32841 error_at (token
->location
, "invalid use of %qD in linkage specification",
32842 ridpointers
[keyword
]);
32845 else if (decl_specs
->storage_class
!= sc_none
)
32847 decl_specs
->conflicting_specifiers_p
= true;
32851 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
32852 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
32853 && decl_specs
->gnu_thread_keyword_p
)
32855 pedwarn (decl_specs
->locations
[ds_thread
], 0,
32856 "%<__thread%> before %qD", ridpointers
[keyword
]);
32862 storage_class
= sc_auto
;
32865 storage_class
= sc_register
;
32868 storage_class
= sc_static
;
32871 storage_class
= sc_extern
;
32874 storage_class
= sc_mutable
;
32877 gcc_unreachable ();
32879 decl_specs
->storage_class
= storage_class
;
32880 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
32882 /* A storage class specifier cannot be applied alongside a typedef
32883 specifier. If there is a typedef specifier present then set
32884 conflicting_specifiers_p which will trigger an error later
32885 on in grokdeclarator. */
32886 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
32887 decl_specs
->conflicting_specifiers_p
= true;
32890 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
32891 is true, the type is a class or enum definition. */
32894 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
32897 bool type_definition_p
)
32899 decl_specs
->any_specifiers_p
= true;
32901 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
32902 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
32903 this is what happened. In system headers, we ignore these
32904 declarations so that G++ can work with system headers that are not
32906 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
32907 && !type_definition_p
32908 && (type_spec
== boolean_type_node
32909 || type_spec
== char8_type_node
32910 || type_spec
== char16_type_node
32911 || type_spec
== char32_type_node
32912 || type_spec
== wchar_type_node
)
32913 && (decl_specs
->type
32914 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
32915 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
32916 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
32917 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
32919 decl_specs
->redefined_builtin_type
= type_spec
;
32920 set_and_check_decl_spec_loc (decl_specs
,
32921 ds_redefined_builtin_type_spec
,
32923 if (!decl_specs
->type
)
32925 decl_specs
->type
= type_spec
;
32926 decl_specs
->type_definition_p
= false;
32927 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
32930 else if (decl_specs
->type
)
32931 decl_specs
->multiple_types_p
= true;
32934 decl_specs
->type
= type_spec
;
32935 decl_specs
->type_definition_p
= type_definition_p
;
32936 decl_specs
->redefined_builtin_type
= NULL_TREE
;
32937 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
32941 /* True iff TOKEN is the GNU keyword __thread. */
32944 token_is__thread (cp_token
*token
)
32946 gcc_assert (token
->keyword
== RID_THREAD
);
32947 return id_equal (token
->u
.value
, "__thread");
32950 /* Set the location for a declarator specifier and check if it is
32953 DECL_SPECS is the sequence of declarator specifiers onto which to
32956 DS is the single declarator specifier to set which location is to
32957 be set onto the existing sequence of declarators.
32959 LOCATION is the location for the declarator specifier to
32963 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
32964 cp_decl_spec ds
, cp_token
*token
)
32966 gcc_assert (ds
< ds_last
);
32968 if (decl_specs
== NULL
)
32971 location_t location
= token
->location
;
32973 if (decl_specs
->locations
[ds
] == 0)
32975 decl_specs
->locations
[ds
] = location
;
32976 if (ds
== ds_thread
)
32977 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
32983 if (decl_specs
->locations
[ds_long_long
] != 0)
32984 error_at (location
,
32985 "%<long long long%> is too long for GCC");
32988 decl_specs
->locations
[ds_long_long
] = location
;
32989 pedwarn_cxx98 (location
,
32991 "ISO C++ 1998 does not support %<long long%>");
32994 else if (ds
== ds_thread
)
32996 bool gnu
= token_is__thread (token
);
32997 gcc_rich_location
richloc (location
);
32998 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
33000 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
33001 error_at (&richloc
,
33002 "both %<__thread%> and %<thread_local%> specified");
33006 richloc
.add_fixit_remove ();
33007 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
33012 static const char *const decl_spec_names
[] = {
33031 gcc_rich_location
richloc (location
);
33032 richloc
.add_fixit_remove ();
33033 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
33038 /* Return true iff the declarator specifier DS is present in the
33039 sequence of declarator specifiers DECL_SPECS. */
33042 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
33045 gcc_assert (ds
< ds_last
);
33047 if (decl_specs
== NULL
)
33050 return decl_specs
->locations
[ds
] != 0;
33053 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
33054 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
33057 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
33059 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
33062 /* Issue an error message indicating that TOKEN_DESC was expected.
33063 If KEYWORD is true, it indicated this function is called by
33064 cp_parser_require_keword and the required token can only be
33065 a indicated keyword.
33067 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33068 within any error as the location of an "opening" token matching
33069 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33070 RT_CLOSE_PAREN). */
33073 cp_parser_required_error (cp_parser
*parser
,
33074 required_token token_desc
,
33076 location_t matching_location
)
33078 if (cp_parser_simulate_error (parser
))
33081 const char *gmsgid
= NULL
;
33082 switch (token_desc
)
33085 gmsgid
= G_("expected %<new%>");
33088 gmsgid
= G_("expected %<delete%>");
33091 gmsgid
= G_("expected %<return%>");
33094 gmsgid
= G_("expected %<while%>");
33097 gmsgid
= G_("expected %<extern%>");
33099 case RT_STATIC_ASSERT
:
33100 gmsgid
= G_("expected %<static_assert%>");
33103 gmsgid
= G_("expected %<decltype%>");
33106 gmsgid
= G_("expected %<operator%>");
33109 gmsgid
= G_("expected %<class%>");
33112 gmsgid
= G_("expected %<template%>");
33115 gmsgid
= G_("expected %<namespace%>");
33118 gmsgid
= G_("expected %<using%>");
33121 gmsgid
= G_("expected %<asm%>");
33124 gmsgid
= G_("expected %<try%>");
33127 gmsgid
= G_("expected %<catch%>");
33130 gmsgid
= G_("expected %<throw%>");
33133 gmsgid
= G_("expected %<auto%>");
33136 gmsgid
= G_("expected %<__label__%>");
33139 gmsgid
= G_("expected %<@try%>");
33141 case RT_AT_SYNCHRONIZED
:
33142 gmsgid
= G_("expected %<@synchronized%>");
33145 gmsgid
= G_("expected %<@throw%>");
33147 case RT_TRANSACTION_ATOMIC
:
33148 gmsgid
= G_("expected %<__transaction_atomic%>");
33150 case RT_TRANSACTION_RELAXED
:
33151 gmsgid
= G_("expected %<__transaction_relaxed%>");
33154 gmsgid
= G_("expected %<co_yield%>");
33160 if (!gmsgid
&& !keyword
)
33162 switch (token_desc
)
33165 gmsgid
= G_("expected %<;%>");
33167 case RT_OPEN_PAREN
:
33168 gmsgid
= G_("expected %<(%>");
33170 case RT_CLOSE_BRACE
:
33171 gmsgid
= G_("expected %<}%>");
33173 case RT_OPEN_BRACE
:
33174 gmsgid
= G_("expected %<{%>");
33176 case RT_CLOSE_SQUARE
:
33177 gmsgid
= G_("expected %<]%>");
33179 case RT_OPEN_SQUARE
:
33180 gmsgid
= G_("expected %<[%>");
33183 gmsgid
= G_("expected %<,%>");
33186 gmsgid
= G_("expected %<::%>");
33189 gmsgid
= G_("expected %<<%>");
33192 gmsgid
= G_("expected %<>%>");
33195 gmsgid
= G_("expected %<=%>");
33198 gmsgid
= G_("expected %<...%>");
33201 gmsgid
= G_("expected %<*%>");
33204 gmsgid
= G_("expected %<~%>");
33207 gmsgid
= G_("expected %<:%>");
33209 case RT_COLON_SCOPE
:
33210 gmsgid
= G_("expected %<:%> or %<::%>");
33212 case RT_CLOSE_PAREN
:
33213 gmsgid
= G_("expected %<)%>");
33215 case RT_COMMA_CLOSE_PAREN
:
33216 gmsgid
= G_("expected %<,%> or %<)%>");
33218 case RT_PRAGMA_EOL
:
33219 gmsgid
= G_("expected end of line");
33222 gmsgid
= G_("expected identifier");
33225 gmsgid
= G_("expected selection-statement");
33228 gmsgid
= G_("expected iteration-statement");
33231 gmsgid
= G_("expected jump-statement");
33234 gmsgid
= G_("expected class-key");
33236 case RT_CLASS_TYPENAME_TEMPLATE
:
33237 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
33240 gcc_unreachable ();
33245 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
33249 /* If the next token is of the indicated TYPE, consume it. Otherwise,
33250 issue an error message indicating that TOKEN_DESC was expected.
33252 Returns the token consumed, if the token had the appropriate type.
33253 Otherwise, returns NULL.
33255 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33256 within any error as the location of an "opening" token matching
33257 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33258 RT_CLOSE_PAREN). */
33261 cp_parser_require (cp_parser
* parser
,
33262 enum cpp_ttype type
,
33263 required_token token_desc
,
33264 location_t matching_location
)
33266 if (cp_lexer_next_token_is (parser
->lexer
, type
))
33267 return cp_lexer_consume_token (parser
->lexer
);
33270 /* Output the MESSAGE -- unless we're parsing tentatively. */
33271 if (!cp_parser_simulate_error (parser
))
33272 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
33273 matching_location
);
33278 /* Skip an entire parameter list from start to finish. The next token must
33279 be the initial "<" of the parameter list. Returns true on success and
33280 false otherwise. */
33283 cp_parser_skip_entire_template_parameter_list (cp_parser
* parser
)
33285 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
33287 cp_lexer_consume_token (parser
->lexer
);
33288 return cp_parser_skip_to_end_of_template_parameter_list (parser
);
33291 /* Ensure we are at the end of a template parameter list. If we are, return.
33292 If we are not, something has gone wrong, in which case issue an error and
33293 skip to end of the parameter list. */
33296 cp_parser_require_end_of_template_parameter_list (cp_parser
* parser
)
33298 /* Are we ready, yet? If not, issue error message. */
33299 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
33302 cp_parser_skip_to_end_of_template_parameter_list (parser
);
33305 /* You should only call this function from inside a template parameter list
33306 (i.e. the current token should at least be the initial "<" of the
33307 parameter list). If you are skipping the entire list, it may be better to
33308 use cp_parser_skip_entire_template_parameter_list.
33310 Tokens are skipped until the final ">" is found, or if we see
33311 '{', '}', ';', or if we find an unbalanced ')' or ']'.
33313 Returns true if we successfully reached the end, and false if
33314 something unexpected happened (e.g. end of file). */
33317 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
33319 /* Current level of '< ... >'. */
33320 unsigned level
= 0;
33321 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
33322 unsigned nesting_depth
= 0;
33324 /* Skip tokens until the desired token is found. */
33327 /* Peek at the next token. */
33328 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33331 if (!nesting_depth
)
33336 if (cxx_dialect
== cxx98
)
33337 /* C++0x views the `>>' operator as two `>' tokens, but
33340 else if (!nesting_depth
&& level
-- == 0)
33342 /* We've hit a `>>' where the first `>' closes the
33343 template argument list, and the second `>' is
33344 spurious. Just consume the `>>' and stop; we've
33345 already produced at least one error. */
33346 cp_lexer_consume_token (parser
->lexer
);
33349 /* Fall through for C++0x, so we handle the second `>' in
33351 gcc_fallthrough ();
33354 if (!nesting_depth
&& level
-- == 0)
33356 /* We've reached the token we want, consume it and stop. */
33357 cp_lexer_consume_token (parser
->lexer
);
33362 case CPP_OPEN_PAREN
:
33363 case CPP_OPEN_SQUARE
:
33367 case CPP_CLOSE_PAREN
:
33368 case CPP_CLOSE_SQUARE
:
33369 if (nesting_depth
-- == 0)
33374 case CPP_PRAGMA_EOL
:
33375 case CPP_SEMICOLON
:
33376 case CPP_OPEN_BRACE
:
33377 case CPP_CLOSE_BRACE
:
33378 /* The '>' was probably forgotten, don't look further. */
33385 /* Consume this token. */
33386 cp_lexer_consume_token (parser
->lexer
);
33390 /* If the next token is the indicated keyword, consume it. Otherwise,
33391 issue an error message indicating that TOKEN_DESC was expected.
33393 Returns the token consumed, if the token had the appropriate type.
33394 Otherwise, returns NULL. */
33397 cp_parser_require_keyword (cp_parser
* parser
,
33399 required_token token_desc
)
33401 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
33403 if (token
&& token
->keyword
!= keyword
)
33405 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
33413 /* Returns TRUE iff TOKEN is a token that can begin the body of a
33414 function-definition. */
33417 cp_parser_token_starts_function_definition_p (cp_token
* token
)
33419 return (/* An ordinary function-body begins with an `{'. */
33420 token
->type
== CPP_OPEN_BRACE
33421 /* A ctor-initializer begins with a `:'. */
33422 || token
->type
== CPP_COLON
33423 /* A function-try-block begins with `try'. */
33424 || token
->keyword
== RID_TRY
33425 /* A function-transaction-block begins with `__transaction_atomic'
33426 or `__transaction_relaxed'. */
33427 || token
->keyword
== RID_TRANSACTION_ATOMIC
33428 || token
->keyword
== RID_TRANSACTION_RELAXED
33429 /* The named return value extension begins with `return'. */
33430 || token
->keyword
== RID_RETURN
);
33433 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
33437 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
33441 token
= cp_lexer_peek_token (parser
->lexer
);
33442 return (token
->type
== CPP_OPEN_BRACE
33443 || (token
->type
== CPP_COLON
33444 && !parser
->colon_doesnt_start_class_def_p
));
33447 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
33448 C++0x) ending a template-argument. */
33451 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
33455 token
= cp_lexer_peek_token (parser
->lexer
);
33456 return (token
->type
== CPP_COMMA
33457 || token
->type
== CPP_GREATER
33458 || token
->type
== CPP_ELLIPSIS
33459 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
)
33460 /* For better diagnostics, treat >>= like that too, that
33461 shouldn't appear non-nested in template arguments. */
33462 || token
->type
== CPP_RSHIFT_EQ
);
33465 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
33466 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
33469 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
33474 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
33475 if (token
->type
== CPP_LESS
)
33477 /* Check for the sequence `<::' in the original code. It would be lexed as
33478 `[:', where `[' is a digraph, and there is no whitespace before
33480 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
33483 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
33484 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
33490 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
33491 or none_type otherwise. */
33493 static enum tag_types
33494 cp_parser_token_is_class_key (cp_token
* token
)
33496 switch (token
->keyword
)
33501 return record_type
;
33510 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
33511 or none_type otherwise or if the token is null. */
33513 static enum tag_types
33514 cp_parser_token_is_type_parameter_key (cp_token
* token
)
33519 switch (token
->keyword
)
33524 return typename_type
;
33531 /* Diagnose redundant enum-keys. */
33534 cp_parser_maybe_warn_enum_key (cp_parser
*parser
, location_t key_loc
,
33535 tree type
, rid scoped_key
)
33537 if (!warn_redundant_tags
)
33540 tree type_decl
= TYPE_MAIN_DECL (type
);
33541 tree name
= DECL_NAME (type_decl
);
33542 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
33543 push_deferring_access_checks (dk_no_check
);
33544 tree decl
= cp_parser_lookup_name_simple (parser
, name
, input_location
);
33545 pop_deferring_access_checks ();
33547 /* The enum-key is redundant for uses of the TYPE that are not
33548 declarations and for which name lookup returns just the type
33550 if (decl
!= type_decl
)
33553 if (scoped_key
!= RID_CLASS
33554 && scoped_key
!= RID_STRUCT
33555 && current_lang_name
!= lang_name_cplusplus
33556 && current_namespace
== global_namespace
)
33558 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
33559 enum tag in shared C/C++ code in files (such as headers) included
33560 in the main source file. */
33561 const line_map_ordinary
*map
= NULL
;
33562 linemap_resolve_location (line_table
, key_loc
,
33563 LRK_MACRO_DEFINITION_LOCATION
,
33565 if (!MAIN_FILE_P (map
))
33569 gcc_rich_location
richloc (key_loc
);
33570 richloc
.add_fixit_remove (key_loc
);
33571 warning_at (&richloc
, OPT_Wredundant_tags
,
33572 "redundant enum-key %<enum%s%> in reference to %q#T",
33573 (scoped_key
== RID_CLASS
? " class"
33574 : scoped_key
== RID_STRUCT
? " struct" : ""), type
);
33577 /* Describes the set of declarations of a struct, class, or class template
33578 or its specializations. Used for -Wmismatched-tags. */
33580 class class_decl_loc_t
33584 class_decl_loc_t ()
33585 : locvec (), idxdef (), def_class_key ()
33590 /* Constructs an object for a single declaration of a class with
33591 CLASS_KEY at the current location in the current function (or
33592 at another scope). KEY_REDUNDANT is true if the class-key may
33593 be omitted in the current context without an ambiguity with
33594 another symbol with the same name.
33595 DEF_P is true for a class declaration that is a definition.
33596 CURLOC is the associated location. */
33597 class_decl_loc_t (tag_types class_key
, bool key_redundant
, bool def_p
,
33598 location_t curloc
= input_location
)
33599 : locvec (), idxdef (def_p
? 0 : UINT_MAX
), def_class_key (class_key
)
33602 class_key_loc_t
ckl (current_function_decl
, curloc
, class_key
,
33604 locvec
.quick_push (ckl
);
33607 /* Copy, assign, and destroy the object. Necessary because LOCVEC
33608 isn't safely copyable and assignable and doesn't release storage
33610 class_decl_loc_t (const class_decl_loc_t
&rhs
)
33611 : locvec (rhs
.locvec
.copy ()), idxdef (rhs
.idxdef
),
33612 def_class_key (rhs
.def_class_key
)
33615 class_decl_loc_t
& operator= (const class_decl_loc_t
&rhs
)
33620 locvec
= rhs
.locvec
.copy ();
33621 idxdef
= rhs
.idxdef
;
33622 def_class_key
= rhs
.def_class_key
;
33626 ~class_decl_loc_t ()
33631 /* Issues -Wmismatched-tags for a single class. */
33632 void diag_mismatched_tags (tree
);
33634 /* Issues -Wmismatched-tags for all classes. */
33635 static void diag_mismatched_tags ();
33637 /* Adds TYPE_DECL to the collection of class decls and diagnoses
33638 redundant tags (if -Wredundant-tags is enabled). */
33639 static void add (cp_parser
*, location_t
, tag_types
, tree
, bool, bool);
33641 /* Either adds this decl to the collection of class decls
33642 or diagnoses it, whichever is appropriate. */
33643 void add_or_diag_mismatched_tag (tree
, tag_types
, bool, bool);
33647 tree
function (unsigned i
) const
33649 return locvec
[i
].func
;
33652 location_t
location (unsigned i
) const
33654 return locvec
[i
].loc
;
33657 bool key_redundant (unsigned i
) const
33659 return locvec
[i
].key_redundant
;
33662 tag_types
class_key (unsigned i
) const
33664 return locvec
[i
].class_key
;
33667 /* True if a definition for the class has been seen. */
33668 bool def_p () const
33670 return idxdef
< locvec
.length ();
33673 /* The location of a single mention of a class type with the given
33675 struct class_key_loc_t
33677 class_key_loc_t (tree func
, location_t loc
, tag_types key
, bool redundant
)
33678 : func (func
), loc (loc
), class_key (key
), key_redundant (redundant
)
33681 /* The function the type is mentioned in. */
33683 /* The exact location. */
33685 /* The class-key used in the mention of the type. */
33686 tag_types class_key
;
33687 /* True when the class-key could be omitted at this location
33688 without an ambiguity with another symbol of the same name. */
33689 bool key_redundant
;
33691 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
33692 vec
<class_key_loc_t
> locvec
;
33693 /* LOCVEC index of the definition or UINT_MAX if none exists. */
33695 /* The class-key the class was last declared with or none_type when
33696 it has been declared with a mismatched key. */
33697 tag_types def_class_key
;
33699 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
33700 description above. */
33701 typedef hash_map
<tree_decl_hash
, class_decl_loc_t
> class_to_loc_map_t
;
33702 static class_to_loc_map_t class2loc
;
33705 class_decl_loc_t::class_to_loc_map_t
class_decl_loc_t::class2loc
;
33707 /* Issue an error message if the CLASS_KEY does not match the TYPE.
33708 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33709 is set for a declaration of class TYPE and clear for a reference to
33710 it that is not a declaration of it. */
33713 cp_parser_check_class_key (cp_parser
*parser
, location_t key_loc
,
33714 tag_types class_key
, tree type
, bool def_p
,
33717 if (type
== error_mark_node
)
33720 bool seen_as_union
= TREE_CODE (type
) == UNION_TYPE
;
33721 if (seen_as_union
!= (class_key
== union_type
))
33723 if (permerror (input_location
, "%qs tag used in naming %q#T",
33724 class_key
== union_type
? "union"
33725 : class_key
== record_type
? "struct" : "class",
33727 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
33728 "%q#T was previously declared here", type
);
33732 if (!warn_mismatched_tags
&& !warn_redundant_tags
)
33735 /* Only consider the true class-keys below and ignore typename_type,
33736 etc. that are not C++ class-keys. */
33737 if (class_key
!= class_type
33738 && class_key
!= record_type
33739 && class_key
!= union_type
)
33742 class_decl_loc_t::add (parser
, key_loc
, class_key
, type
, def_p
, decl_p
);
33745 /* Returns the template or specialization of one to which the RECORD_TYPE
33746 TYPE corresponds. */
33749 specialization_of (tree type
)
33753 /* Determine the template or its partial specialization to which TYPE
33755 if (tree spec
= most_specialized_partial_spec (type
, tf_none
))
33756 if (spec
!= error_mark_node
)
33757 ret
= TREE_TYPE (TREE_VALUE (spec
));
33760 ret
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type
);
33762 return TYPE_MAIN_DECL (ret
);
33766 /* Adds the class TYPE to the collection of class decls and diagnoses
33767 redundant tags (if -Wredundant-tags is enabled).
33768 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33769 is set for a (likely, based on syntactic context) declaration of class
33770 TYPE and clear for a reference to it that is not a declaration of it. */
33773 class_decl_loc_t::add (cp_parser
*parser
, location_t key_loc
,
33774 tag_types class_key
, tree type
, bool def_p
, bool decl_p
)
33776 tree type_decl
= TYPE_MAIN_DECL (type
);
33777 tree name
= DECL_NAME (type_decl
);
33778 /* Look up the NAME to see if it unambiguously refers to the TYPE
33779 and set KEY_REDUNDANT if so. */
33780 push_deferring_access_checks (dk_no_check
);
33781 tree decl
= cp_parser_lookup_name_simple (parser
, name
, input_location
);
33782 pop_deferring_access_checks ();
33784 /* The class-key is redundant for uses of the CLASS_TYPE that are
33785 neither definitions of it nor declarations, and for which name
33786 lookup returns just the type itself. */
33787 bool key_redundant
= (!def_p
&& !decl_p
33788 && (decl
== type_decl
33789 || TREE_CODE (decl
) == TEMPLATE_DECL
33790 || (CLASS_TYPE_P (type
)
33791 && TYPE_BEING_DEFINED (type
))));
33794 && class_key
!= class_type
33795 && current_lang_name
!= lang_name_cplusplus
33796 && current_namespace
== global_namespace
)
33798 /* Avoid issuing the diagnostic for apparently redundant struct
33799 and union class-keys in shared C/C++ code in files (such as
33800 headers) included in the main source file. */
33801 const line_map_ordinary
*map
= NULL
;
33802 linemap_resolve_location (line_table
, key_loc
,
33803 LRK_MACRO_DEFINITION_LOCATION
,
33805 if (!MAIN_FILE_P (map
))
33806 key_redundant
= false;
33809 /* Set if a declaration of TYPE has previously been seen or if it must
33810 exist in a precompiled header. */
33812 class_decl_loc_t
*rdl
= &class2loc
.get_or_insert (type_decl
, &exist
);
33815 tree type
= TREE_TYPE (type_decl
);
33816 if (def_p
|| !COMPLETE_TYPE_P (type
))
33818 /* TYPE_DECL is the first declaration or definition of the type
33819 (outside precompiled headers -- see below). Just create
33820 a new entry for it and return unless it's a declaration
33821 involving a template that may need to be diagnosed by
33822 -Wredundant-tags. */
33823 *rdl
= class_decl_loc_t (class_key
, false, def_p
);
33824 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
33829 /* TYPE was previously defined in some unknown precompiled header.
33830 Simply add a record of its definition at an unknown location and
33831 proceed below to add a reference to it at the current location.
33832 (Declarations in precompiled headers that are not definitions
33835 = CLASSTYPE_DECLARED_CLASS (type
) ? class_type
: record_type
;
33836 location_t def_loc
= DECL_SOURCE_LOCATION (type_decl
);
33837 *rdl
= class_decl_loc_t (def_key
, false, true, def_loc
);
33842 /* A prior declaration of TYPE_DECL has been seen. */
33846 gcc_rich_location
richloc (key_loc
);
33847 richloc
.add_fixit_remove (key_loc
);
33848 warning_at (&richloc
, OPT_Wredundant_tags
,
33849 "redundant class-key %qs in reference to %q#T",
33850 class_key
== union_type
? "union"
33851 : class_key
== record_type
? "struct" : "class",
33856 /* Do nothing if this is the first declaration of the type. */
33859 if (rdl
->idxdef
!= UINT_MAX
&& rdl
->def_class_key
== class_key
)
33860 /* Do nothing if the class-key in this declaration matches
33864 rdl
->add_or_diag_mismatched_tag (type_decl
, class_key
, key_redundant
,
33868 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
33869 of class decls or diagnoses it, whichever is appropriate. */
33872 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl
,
33873 tag_types class_key
,
33877 /* Reset the CLASS_KEY associated with this type on mismatch.
33878 This is an optimization that lets the diagnostic code skip
33879 over classes that use the same class-key in all declarations. */
33880 if (def_class_key
!= class_key
)
33881 def_class_key
= none_type
;
33883 /* Set IDXDEF to the index of the vector corresponding to
33886 idxdef
= locvec
.length ();
33888 /* Append a record of this declaration to the vector. */
33889 class_key_loc_t
ckl (current_function_decl
, input_location
, class_key
,
33891 locvec
.safe_push (ckl
);
33893 if (idxdef
== UINT_MAX
)
33896 /* As a space optimization diagnose declarations of a class
33897 whose definition has been seen and purge the LOCVEC of
33898 all entries except the definition. */
33899 diag_mismatched_tags (type_decl
);
33902 class_decl_loc_t::class_key_loc_t ent
= locvec
[idxdef
];
33904 locvec
.reserve (2);
33905 locvec
.safe_push (ent
);
33909 /* Pop the entry pushed above for this declaration. */
33913 /* Issues -Wmismatched-tags for a single class. */
33916 class_decl_loc_t::diag_mismatched_tags (tree type_decl
)
33918 if (!warn_mismatched_tags
)
33921 /* Number of uses of the class. */
33922 const unsigned ndecls
= locvec
.length ();
33924 /* The class (or template) declaration guiding the decisions about
33925 the diagnostic. For ordinary classes it's the same as THIS. For
33926 uses of instantiations of templates other than their declarations
33927 it points to the record for the declaration of the corresponding
33928 primary template or partial specialization. */
33929 class_decl_loc_t
*cdlguide
= this;
33931 tree type
= TREE_TYPE (type_decl
);
33932 if (CLASS_TYPE_P (type
) && CLASSTYPE_IMPLICIT_INSTANTIATION (type
))
33934 /* For implicit instantiations of a primary template look up
33935 the primary or partial specialization and use it as
33936 the expected class-key rather than using the class-key of
33937 the first reference to the instantiation. The primary must
33938 be (and inevitably is) at index zero. */
33939 tree spec
= specialization_of (type
);
33940 cdlguide
= class2loc
.get (spec
);
33941 gcc_assert (cdlguide
!= NULL
);
33945 /* Skip declarations that consistently use the same class-key. */
33946 if (def_class_key
!= none_type
)
33950 /* Set if a definition for the class has been seen. */
33951 const bool def_p
= cdlguide
->def_p ();
33953 /* The index of the declaration whose class-key this declaration
33954 is expected to match. It's either the class-key of the class
33955 definition if one exists or the first declaration otherwise. */
33956 const unsigned idxguide
= def_p
? cdlguide
->idxdef
: 0;
33958 /* The class-key the class is expected to be declared with: it's
33959 either the key used in its definition or the first declaration
33960 if no definition has been provided.
33961 For implicit instantiations of a primary template it's
33962 the class-key used to declare the primary with. The primary
33963 must be at index zero. */
33964 const tag_types xpect_key
= cdlguide
->class_key (idxguide
);
33967 /* Advance IDX to the first declaration that either is not
33968 a definition or that doesn't match the first declaration
33969 if no definition is provided. */
33970 while (class_key (idx
) == xpect_key
)
33971 if (++idx
== ndecls
)
33974 /* Save the current function before changing it below. */
33975 tree save_func
= current_function_decl
;
33976 /* Set the function declaration to print in diagnostic context. */
33977 current_function_decl
= function (idx
);
33979 const char *xmatchkstr
= xpect_key
== record_type
? "class" : "struct";
33980 const char *xpectkstr
= xpect_key
== record_type
? "struct" : "class";
33982 location_t loc
= location (idx
);
33983 bool key_redundant_p
= key_redundant (idx
);
33984 auto_diagnostic_group d
;
33985 /* Issue a warning for the first mismatched declaration.
33986 Avoid using "%#qT" since the class-key for the same type will
33987 be the same regardless of which one was used in the declaraion. */
33988 if (warning_at (loc
, OPT_Wmismatched_tags
,
33989 "%qT declared with a mismatched class-key %qs",
33990 type_decl
, xmatchkstr
))
33992 /* Suggest how to avoid the warning for each instance since
33993 the guidance may be different depending on context. */
33996 ? G_("remove the class-key or replace it with %qs")
33997 : G_("replace the class-key with %qs")),
34000 /* Also point to the first declaration or definition that guided
34001 the decision to issue the warning above. */
34002 inform (cdlguide
->location (idxguide
),
34004 ? G_("%qT defined as %qs here")
34005 : G_("%qT first declared as %qs here")),
34006 type_decl
, xpectkstr
);
34009 /* Issue warnings for the remaining inconsistent declarations. */
34010 for (unsigned i
= idx
+ 1; i
!= ndecls
; ++i
)
34012 tag_types clskey
= class_key (i
);
34013 /* Skip over the declarations that match either the definition
34014 if one was provided or the first declaration. */
34015 if (clskey
== xpect_key
)
34018 loc
= location (i
);
34019 key_redundant_p
= key_redundant (i
);
34020 /* Set the function declaration to print in diagnostic context. */
34021 current_function_decl
= function (i
);
34022 if (warning_at (loc
, OPT_Wmismatched_tags
,
34023 "%qT declared with a mismatched class-key %qs",
34024 type_decl
, xmatchkstr
))
34025 /* Suggest how to avoid the warning for each instance since
34026 the guidance may be different depending on context. */
34029 ? G_("remove the class-key or replace it with %qs")
34030 : G_("replace the class-key with %qs")),
34034 /* Restore the current function in case it was replaced above. */
34035 current_function_decl
= save_func
;
34038 /* Issues -Wmismatched-tags for all classes. Called at the end
34039 of processing a translation unit, after declarations of all class
34040 types and their uses have been recorded. */
34043 class_decl_loc_t::diag_mismatched_tags ()
34045 /* CLASS2LOC should be empty if both -Wmismatched-tags and
34046 -Wredundant-tags are disabled. */
34047 gcc_assert (warn_mismatched_tags
34048 || warn_redundant_tags
34049 || class2loc
.is_empty ());
34051 /* Save the current function before changing on return. It should
34052 be null at this point. */
34053 temp_override
<tree
> cleanup (current_function_decl
);
34055 if (warn_mismatched_tags
)
34057 /* Iterate over the collected class/struct/template declarations. */
34058 typedef class_to_loc_map_t::iterator iter_t
;
34059 for (iter_t it
= class2loc
.begin (); it
!= class2loc
.end (); ++it
)
34061 tree type_decl
= (*it
).first
;
34062 class_decl_loc_t
&recloc
= (*it
).second
;
34063 recloc
.diag_mismatched_tags (type_decl
);
34067 class2loc
.empty ();
34070 /* Issue an error message if DECL is redeclared with different
34071 access than its original declaration [class.access.spec/3].
34072 This applies to nested classes, nested class templates and
34073 enumerations [class.mem/1]. */
34076 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
34079 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
34080 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
34083 if ((TREE_PRIVATE (decl
)
34084 != (current_access_specifier
== access_private_node
))
34085 || (TREE_PROTECTED (decl
)
34086 != (current_access_specifier
== access_protected_node
)))
34087 error_at (location
, "%qD redeclared with different access", decl
);
34090 /* Look for the `template' keyword, as a syntactic disambiguator.
34091 Return TRUE iff it is present, in which case it will be
34095 cp_parser_optional_template_keyword (cp_parser
*parser
)
34097 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
34099 /* In C++98 the `template' keyword can only be used within templates;
34100 outside templates the parser can always figure out what is a
34101 template and what is not. In C++11, per the resolution of DR 468,
34102 `template' is allowed in cases where it is not strictly necessary. */
34103 if (!processing_template_decl
34104 && pedantic
&& cxx_dialect
== cxx98
)
34106 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34107 pedwarn (token
->location
, OPT_Wpedantic
,
34108 "in C++98 %<template%> (as a disambiguator) is only "
34109 "allowed within templates");
34110 /* If this part of the token stream is rescanned, the same
34111 error message would be generated. So, we purge the token
34112 from the stream. */
34113 cp_lexer_purge_token (parser
->lexer
);
34118 /* Consume the `template' keyword. */
34119 cp_lexer_consume_token (parser
->lexer
);
34126 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
34127 set PARSER->SCOPE, and perform other related actions. */
34130 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
34132 struct tree_check
*check_value
;
34134 /* Get the stored value. */
34135 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
34136 /* Set the scope from the stored value. */
34137 parser
->scope
= saved_checks_value (check_value
);
34138 parser
->qualifying_scope
= check_value
->qualifying_scope
;
34139 parser
->object_scope
= parser
->context
->object_type
;
34140 parser
->context
->object_type
= NULL_TREE
;
34143 /* Consume tokens up through a non-nested END token. Returns TRUE if we
34144 encounter the end of a block before what we were looking for. */
34147 cp_parser_cache_group (cp_parser
*parser
,
34148 enum cpp_ttype end
,
34153 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34155 /* Abort a parenthesized expression if we encounter a semicolon. */
34156 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
34157 && token
->type
== CPP_SEMICOLON
)
34159 /* If we've reached the end of the file, stop. */
34160 if (token
->type
== CPP_EOF
34161 || (end
!= CPP_PRAGMA_EOL
34162 && token
->type
== CPP_PRAGMA_EOL
))
34164 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
34165 /* We've hit the end of an enclosing block, so there's been some
34166 kind of syntax error. */
34169 /* Consume the token. */
34170 cp_lexer_consume_token (parser
->lexer
);
34171 /* See if it starts a new group. */
34172 if (token
->type
== CPP_OPEN_BRACE
)
34174 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
34175 /* In theory this should probably check end == '}', but
34176 cp_parser_save_member_function_body needs it to exit
34177 after either '}' or ')' when called with ')'. */
34181 else if (token
->type
== CPP_OPEN_PAREN
)
34183 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
34184 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
34187 else if (token
->type
== CPP_PRAGMA
)
34188 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
34189 else if (token
->type
== end
)
34194 /* Like above, for caching a default argument or NSDMI. Both of these are
34195 terminated by a non-nested comma, but it can be unclear whether or not a
34196 comma is nested in a template argument list unless we do more parsing.
34197 In order to handle this ambiguity, when we encounter a ',' after a '<'
34198 we try to parse what follows as a parameter-declaration-list (in the
34199 case of a default argument) or a member-declarator (in the case of an
34200 NSDMI). If that succeeds, then we stop caching. */
34203 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
34205 unsigned depth
= 0;
34206 int maybe_template_id
= 0;
34207 cp_token
*first_token
;
34209 tree default_argument
;
34211 /* Add tokens until we have processed the entire default
34212 argument. We add the range [first_token, token). */
34213 first_token
= cp_lexer_peek_token (parser
->lexer
);
34214 if (first_token
->type
== CPP_OPEN_BRACE
)
34216 /* For list-initialization, this is straightforward. */
34217 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
34218 token
= cp_lexer_peek_token (parser
->lexer
);
34224 /* Peek at the next token. */
34225 token
= cp_lexer_peek_token (parser
->lexer
);
34226 /* What we do depends on what token we have. */
34227 switch (token
->type
)
34229 /* In valid code, a default argument must be
34230 immediately followed by a `,' `)', or `...'. */
34232 if (depth
== 0 && maybe_template_id
)
34234 /* If we've seen a '<', we might be in a
34235 template-argument-list. Until Core issue 325 is
34236 resolved, we don't know how this situation ought
34237 to be handled, so try to DTRT. We check whether
34238 what comes after the comma is a valid parameter
34239 declaration list. If it is, then the comma ends
34240 the default argument; otherwise the default
34241 argument continues. */
34242 bool error
= false;
34245 /* Set ITALP so cp_parser_parameter_declaration_list
34246 doesn't decide to commit to this parse. */
34247 bool saved_italp
= parser
->in_template_argument_list_p
;
34248 parser
->in_template_argument_list_p
= true;
34250 cp_parser_parse_tentatively (parser
);
34254 /* Parse declarators until we reach a non-comma or
34255 somthing that cannot be an initializer.
34256 Just checking whether we're looking at a single
34257 declarator is insufficient. Consider:
34258 int var = tuple<T,U>::x;
34259 The template parameter 'U' looks exactly like a
34263 int ctor_dtor_or_conv_p
;
34264 cp_lexer_consume_token (parser
->lexer
);
34265 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
34266 CP_PARSER_FLAGS_NONE
,
34267 &ctor_dtor_or_conv_p
,
34268 /*parenthesized_p=*/NULL
,
34270 /*friend_p=*/false,
34271 /*static_p=*/false);
34272 peek
= cp_lexer_peek_token (parser
->lexer
);
34273 if (cp_parser_error_occurred (parser
))
34276 while (peek
->type
== CPP_COMMA
);
34277 /* If we met an '=' or ';' then the original comma
34278 was the end of the NSDMI. Otherwise assume
34279 we're still in the NSDMI. */
34280 error
= (peek
->type
!= CPP_EQ
34281 && peek
->type
!= CPP_SEMICOLON
);
34285 cp_lexer_consume_token (parser
->lexer
);
34286 begin_scope (sk_function_parms
, NULL_TREE
);
34287 tree t
= cp_parser_parameter_declaration_list
34288 (parser
, CP_PARSER_FLAGS_NONE
,
34289 /*pending_decls*/nullptr);
34290 if (t
== error_mark_node
)
34292 pop_bindings_and_leave_scope ();
34294 if (!cp_parser_error_occurred (parser
) && !error
)
34296 cp_parser_abort_tentative_parse (parser
);
34298 parser
->in_template_argument_list_p
= saved_italp
;
34302 case CPP_CLOSE_PAREN
:
34304 /* If we run into a non-nested `;', `}', or `]',
34305 then the code is invalid -- but the default
34306 argument is certainly over. */
34307 case CPP_SEMICOLON
:
34308 case CPP_CLOSE_BRACE
:
34309 case CPP_CLOSE_SQUARE
:
34311 /* Handle correctly int n = sizeof ... ( p ); */
34312 && token
->type
!= CPP_ELLIPSIS
)
34314 /* Update DEPTH, if necessary. */
34315 else if (token
->type
== CPP_CLOSE_PAREN
34316 || token
->type
== CPP_CLOSE_BRACE
34317 || token
->type
== CPP_CLOSE_SQUARE
)
34321 case CPP_OPEN_PAREN
:
34322 case CPP_OPEN_SQUARE
:
34323 case CPP_OPEN_BRACE
:
34329 /* This might be the comparison operator, or it might
34330 start a template argument list. */
34331 ++maybe_template_id
;
34335 if (cxx_dialect
== cxx98
)
34337 /* Fall through for C++0x, which treats the `>>'
34338 operator like two `>' tokens in certain
34340 gcc_fallthrough ();
34345 /* This might be an operator, or it might close a
34346 template argument list. But if a previous '<'
34347 started a template argument list, this will have
34348 closed it, so we can't be in one anymore. */
34349 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
34350 if (maybe_template_id
< 0)
34351 maybe_template_id
= 0;
34355 /* If we run out of tokens, issue an error message. */
34357 case CPP_PRAGMA_EOL
:
34358 error_at (token
->location
, "file ends in default argument");
34359 return error_mark_node
;
34363 /* In these cases, we should look for template-ids.
34364 For example, if the default argument is
34365 `X<int, double>()', we need to do name lookup to
34366 figure out whether or not `X' is a template; if
34367 so, the `,' does not end the default argument.
34369 That is not yet done. */
34376 /* If we've reached the end, stop. */
34380 /* Add the token to the token block. */
34381 token
= cp_lexer_consume_token (parser
->lexer
);
34384 /* Create a DEFERRED_PARSE to represent the unparsed default
34386 default_argument
= make_node (DEFERRED_PARSE
);
34387 DEFPARSE_TOKENS (default_argument
)
34388 = cp_token_cache_new (first_token
, token
);
34389 DEFPARSE_INSTANTIATIONS (default_argument
) = NULL
;
34391 return default_argument
;
34394 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
34397 defparse_location (tree default_argument
)
34399 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_argument
);
34400 location_t start
= tokens
->first
->location
;
34401 location_t end
= tokens
->last
->location
;
34402 return make_location (start
, start
, end
);
34405 /* Begin parsing tentatively. We always save tokens while parsing
34406 tentatively so that if the tentative parsing fails we can restore the
34410 cp_parser_parse_tentatively (cp_parser
* parser
)
34412 /* Enter a new parsing context. */
34413 parser
->context
= cp_parser_context_new (parser
->context
);
34414 /* Begin saving tokens. */
34415 cp_lexer_save_tokens (parser
->lexer
);
34416 /* In order to avoid repetitive access control error messages,
34417 access checks are queued up until we are no longer parsing
34419 push_deferring_access_checks (dk_deferred
);
34422 /* Commit to the currently active tentative parse. */
34425 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
34427 cp_parser_context
*context
;
34430 /* Mark all of the levels as committed. */
34431 lexer
= parser
->lexer
;
34432 for (context
= parser
->context
; context
->next
; context
= context
->next
)
34434 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
34436 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
34437 while (!cp_lexer_saving_tokens (lexer
))
34438 lexer
= lexer
->next
;
34439 cp_lexer_commit_tokens (lexer
);
34443 /* Commit to the topmost currently active tentative parse.
34445 Note that this function shouldn't be called when there are
34446 irreversible side-effects while in a tentative state. For
34447 example, we shouldn't create a permanent entry in the symbol
34448 table, or issue an error message that might not apply if the
34449 tentative parse is aborted. */
34452 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
34454 cp_parser_context
*context
= parser
->context
;
34455 cp_lexer
*lexer
= parser
->lexer
;
34459 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
34461 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
34463 while (!cp_lexer_saving_tokens (lexer
))
34464 lexer
= lexer
->next
;
34465 cp_lexer_commit_tokens (lexer
);
34469 /* Abort the currently active tentative parse. All consumed tokens
34470 will be rolled back, and no diagnostics will be issued. */
34473 cp_parser_abort_tentative_parse (cp_parser
* parser
)
34475 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
34476 || errorcount
> 0);
34477 cp_parser_simulate_error (parser
);
34478 /* Now, pretend that we want to see if the construct was
34479 successfully parsed. */
34480 cp_parser_parse_definitely (parser
);
34483 /* Stop parsing tentatively. If a parse error has occurred, restore the
34484 token stream. Otherwise, commit to the tokens we have consumed.
34485 Returns true if no error occurred; false otherwise. */
34488 cp_parser_parse_definitely (cp_parser
* parser
)
34490 bool error_occurred
;
34491 cp_parser_context
*context
;
34493 /* Remember whether or not an error occurred, since we are about to
34494 destroy that information. */
34495 error_occurred
= cp_parser_error_occurred (parser
);
34496 /* Remove the topmost context from the stack. */
34497 context
= parser
->context
;
34498 parser
->context
= context
->next
;
34499 /* If no parse errors occurred, commit to the tentative parse. */
34500 if (!error_occurred
)
34502 /* Commit to the tokens read tentatively, unless that was
34504 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
34505 cp_lexer_commit_tokens (parser
->lexer
);
34507 pop_to_parent_deferring_access_checks ();
34509 /* Otherwise, if errors occurred, roll back our state so that things
34510 are just as they were before we began the tentative parse. */
34513 cp_lexer_rollback_tokens (parser
->lexer
);
34514 pop_deferring_access_checks ();
34516 /* Add the context to the front of the free list. */
34517 context
->next
= cp_parser_context_free_list
;
34518 cp_parser_context_free_list
= context
;
34520 return !error_occurred
;
34523 /* Returns true if we are parsing tentatively and are not committed to
34524 this tentative parse. */
34527 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
34529 return (cp_parser_parsing_tentatively (parser
)
34530 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
34533 /* Returns nonzero iff an error has occurred during the most recent
34534 tentative parse. */
34537 cp_parser_error_occurred (cp_parser
* parser
)
34539 return (cp_parser_parsing_tentatively (parser
)
34540 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
34543 /* Returns nonzero if GNU extensions are allowed. */
34546 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
34548 return parser
->allow_gnu_extensions_p
;
34551 /* Objective-C++ Productions */
34554 /* Parse an Objective-C expression, which feeds into a primary-expression
34558 objc-message-expression
34559 objc-string-literal
34560 objc-encode-expression
34561 objc-protocol-expression
34562 objc-selector-expression
34564 Returns a tree representation of the expression. */
34567 cp_parser_objc_expression (cp_parser
* parser
)
34569 /* Try to figure out what kind of declaration is present. */
34570 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
34574 case CPP_OPEN_SQUARE
:
34575 return cp_parser_objc_message_expression (parser
);
34577 case CPP_OBJC_STRING
:
34578 kwd
= cp_lexer_consume_token (parser
->lexer
);
34579 return objc_build_string_object (kwd
->u
.value
);
34582 switch (kwd
->keyword
)
34584 case RID_AT_ENCODE
:
34585 return cp_parser_objc_encode_expression (parser
);
34587 case RID_AT_PROTOCOL
:
34588 return cp_parser_objc_protocol_expression (parser
);
34590 case RID_AT_SELECTOR
:
34591 return cp_parser_objc_selector_expression (parser
);
34598 error_at (kwd
->location
,
34599 "misplaced %<@%D%> Objective-C++ construct",
34601 cp_parser_skip_to_end_of_block_or_statement (parser
);
34604 return error_mark_node
;
34607 /* Parse an Objective-C message expression.
34609 objc-message-expression:
34610 [ objc-message-receiver objc-message-args ]
34612 Returns a representation of an Objective-C message. */
34615 cp_parser_objc_message_expression (cp_parser
* parser
)
34617 tree receiver
, messageargs
;
34619 parser
->objective_c_message_context_p
= true;
34620 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34621 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
34622 receiver
= cp_parser_objc_message_receiver (parser
);
34623 messageargs
= cp_parser_objc_message_args (parser
);
34624 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34625 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
34627 tree result
= objc_build_message_expr (receiver
, messageargs
);
34629 /* Construct a location e.g.
34632 ranging from the '[' to the ']', with the caret at the start. */
34633 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
34634 protected_set_expr_location (result
, combined_loc
);
34636 parser
->objective_c_message_context_p
= false;
34640 /* Parse an objc-message-receiver.
34642 objc-message-receiver:
34644 simple-type-specifier
34646 Returns a representation of the type or expression. */
34649 cp_parser_objc_message_receiver (cp_parser
* parser
)
34653 /* An Objective-C message receiver may be either (1) a type
34654 or (2) an expression. */
34655 cp_parser_parse_tentatively (parser
);
34656 rcv
= cp_parser_expression (parser
);
34658 /* If that worked out, fine. */
34659 if (cp_parser_parse_definitely (parser
))
34662 cp_parser_parse_tentatively (parser
);
34663 rcv
= cp_parser_simple_type_specifier (parser
,
34664 /*decl_specs=*/NULL
,
34665 CP_PARSER_FLAGS_NONE
);
34667 if (cp_parser_parse_definitely (parser
))
34668 return objc_get_class_reference (rcv
);
34670 cp_parser_error (parser
, "objective-c++ message receiver expected");
34671 return error_mark_node
;
34674 /* Parse the arguments and selectors comprising an Objective-C message.
34679 objc-selector-args , objc-comma-args
34681 objc-selector-args:
34682 objc-selector [opt] : assignment-expression
34683 objc-selector-args objc-selector [opt] : assignment-expression
34686 assignment-expression
34687 objc-comma-args , assignment-expression
34689 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
34690 selector arguments and TREE_VALUE containing a list of comma
34694 cp_parser_objc_message_args (cp_parser
* parser
)
34696 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
34697 bool maybe_unary_selector_p
= true;
34698 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34700 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
34702 tree selector
= NULL_TREE
, arg
;
34704 if (token
->type
!= CPP_COLON
)
34705 selector
= cp_parser_objc_selector (parser
);
34707 /* Detect if we have a unary selector. */
34708 if (maybe_unary_selector_p
34709 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
34710 return build_tree_list (selector
, NULL_TREE
);
34712 maybe_unary_selector_p
= false;
34713 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34714 arg
= cp_parser_assignment_expression (parser
);
34717 = chainon (sel_args
,
34718 build_tree_list (selector
, arg
));
34720 token
= cp_lexer_peek_token (parser
->lexer
);
34723 /* Handle non-selector arguments, if any. */
34724 while (token
->type
== CPP_COMMA
)
34728 cp_lexer_consume_token (parser
->lexer
);
34729 arg
= cp_parser_assignment_expression (parser
);
34732 = chainon (addl_args
,
34733 build_tree_list (NULL_TREE
, arg
));
34735 token
= cp_lexer_peek_token (parser
->lexer
);
34738 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
34740 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
34741 return build_tree_list (error_mark_node
, error_mark_node
);
34744 return build_tree_list (sel_args
, addl_args
);
34747 /* Parse an Objective-C encode expression.
34749 objc-encode-expression:
34750 @encode objc-typename
34752 Returns an encoded representation of the type argument. */
34755 cp_parser_objc_encode_expression (cp_parser
* parser
)
34759 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34761 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
34762 matching_parens parens
;
34763 parens
.require_open (parser
);
34764 token
= cp_lexer_peek_token (parser
->lexer
);
34765 type
= complete_type (cp_parser_type_id (parser
));
34766 parens
.require_close (parser
);
34770 error_at (token
->location
,
34771 "%<@encode%> must specify a type as an argument");
34772 return error_mark_node
;
34775 /* This happens if we find @encode(T) (where T is a template
34776 typename or something dependent on a template typename) when
34777 parsing a template. In that case, we can't compile it
34778 immediately, but we rather create an AT_ENCODE_EXPR which will
34779 need to be instantiated when the template is used.
34781 if (dependent_type_p (type
))
34783 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
34784 TREE_READONLY (value
) = 1;
34789 /* Build a location of the form:
34792 with caret==start at the @ token, finishing at the close paren. */
34793 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
34795 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
34798 /* Parse an Objective-C @defs expression. */
34801 cp_parser_objc_defs_expression (cp_parser
*parser
)
34805 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
34806 matching_parens parens
;
34807 parens
.require_open (parser
);
34808 name
= cp_parser_identifier (parser
);
34809 parens
.require_close (parser
);
34811 return objc_get_class_ivars (name
);
34814 /* Parse an Objective-C protocol expression.
34816 objc-protocol-expression:
34817 @protocol ( identifier )
34819 Returns a representation of the protocol expression. */
34822 cp_parser_objc_protocol_expression (cp_parser
* parser
)
34825 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34827 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
34828 matching_parens parens
;
34829 parens
.require_open (parser
);
34830 proto
= cp_parser_identifier (parser
);
34831 parens
.require_close (parser
);
34833 /* Build a location of the form:
34836 with caret==start at the @ token, finishing at the close paren. */
34837 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
34838 tree result
= objc_build_protocol_expr (proto
);
34839 protected_set_expr_location (result
, combined_loc
);
34843 /* Parse an Objective-C selector expression.
34845 objc-selector-expression:
34846 @selector ( objc-method-signature )
34848 objc-method-signature:
34854 objc-selector-seq objc-selector :
34856 Returns a representation of the method selector. */
34859 cp_parser_objc_selector_expression (cp_parser
* parser
)
34861 tree sel_seq
= NULL_TREE
;
34862 bool maybe_unary_selector_p
= true;
34864 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34866 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
34867 matching_parens parens
;
34868 parens
.require_open (parser
);
34869 token
= cp_lexer_peek_token (parser
->lexer
);
34871 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
34872 || token
->type
== CPP_SCOPE
)
34874 tree selector
= NULL_TREE
;
34876 if (token
->type
!= CPP_COLON
34877 || token
->type
== CPP_SCOPE
)
34878 selector
= cp_parser_objc_selector (parser
);
34880 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
34881 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
34883 /* Detect if we have a unary selector. */
34884 if (maybe_unary_selector_p
)
34886 sel_seq
= selector
;
34887 goto finish_selector
;
34891 cp_parser_error (parser
, "expected %<:%>");
34894 maybe_unary_selector_p
= false;
34895 token
= cp_lexer_consume_token (parser
->lexer
);
34897 if (token
->type
== CPP_SCOPE
)
34900 = chainon (sel_seq
,
34901 build_tree_list (selector
, NULL_TREE
));
34903 = chainon (sel_seq
,
34904 build_tree_list (NULL_TREE
, NULL_TREE
));
34908 = chainon (sel_seq
,
34909 build_tree_list (selector
, NULL_TREE
));
34911 token
= cp_lexer_peek_token (parser
->lexer
);
34915 parens
.require_close (parser
);
34918 /* Build a location of the form:
34921 with caret==start at the @ token, finishing at the close paren. */
34922 location_t combined_loc
= make_location (loc
, loc
, parser
->lexer
);
34923 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
34924 /* TODO: objc_build_selector_expr doesn't always honor the location. */
34925 protected_set_expr_location (result
, combined_loc
);
34929 /* Parse a list of identifiers.
34931 objc-identifier-list:
34933 objc-identifier-list , identifier
34935 Returns a TREE_LIST of identifier nodes. */
34938 cp_parser_objc_identifier_list (cp_parser
* parser
)
34944 identifier
= cp_parser_identifier (parser
);
34945 if (identifier
== error_mark_node
)
34946 return error_mark_node
;
34948 list
= build_tree_list (NULL_TREE
, identifier
);
34949 sep
= cp_lexer_peek_token (parser
->lexer
);
34951 while (sep
->type
== CPP_COMMA
)
34953 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
34954 identifier
= cp_parser_identifier (parser
);
34955 if (identifier
== error_mark_node
)
34958 list
= chainon (list
, build_tree_list (NULL_TREE
,
34960 sep
= cp_lexer_peek_token (parser
->lexer
);
34966 /* Parse an Objective-C alias declaration.
34968 objc-alias-declaration:
34969 @compatibility_alias identifier identifier ;
34971 This function registers the alias mapping with the Objective-C front end.
34972 It returns nothing. */
34975 cp_parser_objc_alias_declaration (cp_parser
* parser
)
34979 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
34980 alias
= cp_parser_identifier (parser
);
34981 orig
= cp_parser_identifier (parser
);
34982 objc_declare_alias (alias
, orig
);
34983 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34986 /* Parse an Objective-C class forward-declaration.
34988 objc-class-declaration:
34989 @class objc-identifier-list ;
34991 The function registers the forward declarations with the Objective-C
34992 front end. It returns nothing. */
34995 cp_parser_objc_class_declaration (cp_parser
* parser
)
34997 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
35002 id
= cp_parser_identifier (parser
);
35003 if (id
== error_mark_node
)
35006 objc_declare_class (id
);
35008 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35009 cp_lexer_consume_token (parser
->lexer
);
35013 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35016 /* Parse a list of Objective-C protocol references.
35018 objc-protocol-refs-opt:
35019 objc-protocol-refs [opt]
35021 objc-protocol-refs:
35022 < objc-identifier-list >
35024 Returns a TREE_LIST of identifiers, if any. */
35027 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
35029 tree protorefs
= NULL_TREE
;
35031 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
35033 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
35034 protorefs
= cp_parser_objc_identifier_list (parser
);
35035 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
35041 /* Parse a Objective-C visibility specification. */
35044 cp_parser_objc_visibility_spec (cp_parser
* parser
)
35046 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
35048 switch (vis
->keyword
)
35050 case RID_AT_PRIVATE
:
35051 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
35053 case RID_AT_PROTECTED
:
35054 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
35056 case RID_AT_PUBLIC
:
35057 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
35059 case RID_AT_PACKAGE
:
35060 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
35066 /* Eat '@private'/'@protected'/'@public'. */
35067 cp_lexer_consume_token (parser
->lexer
);
35070 /* Parse an Objective-C method type. Return 'true' if it is a class
35071 (+) method, and 'false' if it is an instance (-) method. */
35074 cp_parser_objc_method_type (cp_parser
* parser
)
35076 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
35082 /* Parse an Objective-C protocol qualifier. */
35085 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
35087 tree quals
= NULL_TREE
, node
;
35088 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35090 node
= token
->u
.value
;
35092 while (node
&& identifier_p (node
)
35093 && (node
== ridpointers
[(int) RID_IN
]
35094 || node
== ridpointers
[(int) RID_OUT
]
35095 || node
== ridpointers
[(int) RID_INOUT
]
35096 || node
== ridpointers
[(int) RID_BYCOPY
]
35097 || node
== ridpointers
[(int) RID_BYREF
]
35098 || node
== ridpointers
[(int) RID_ONEWAY
]))
35100 quals
= tree_cons (NULL_TREE
, node
, quals
);
35101 cp_lexer_consume_token (parser
->lexer
);
35102 token
= cp_lexer_peek_token (parser
->lexer
);
35103 node
= token
->u
.value
;
35109 /* Parse an Objective-C typename. */
35112 cp_parser_objc_typename (cp_parser
* parser
)
35114 tree type_name
= NULL_TREE
;
35116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35118 tree proto_quals
, cp_type
= NULL_TREE
;
35120 matching_parens parens
;
35121 parens
.consume_open (parser
); /* Eat '('. */
35122 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
35124 /* An ObjC type name may consist of just protocol qualifiers, in which
35125 case the type shall default to 'id'. */
35126 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35128 cp_type
= cp_parser_type_id (parser
);
35130 /* If the type could not be parsed, an error has already
35131 been produced. For error recovery, behave as if it had
35132 not been specified, which will use the default type
35134 if (cp_type
== error_mark_node
)
35136 cp_type
= NULL_TREE
;
35137 /* We need to skip to the closing parenthesis as
35138 cp_parser_type_id() does not seem to do it for
35140 cp_parser_skip_to_closing_parenthesis (parser
,
35141 /*recovering=*/true,
35142 /*or_comma=*/false,
35143 /*consume_paren=*/false);
35147 parens
.require_close (parser
);
35148 type_name
= build_tree_list (proto_quals
, cp_type
);
35154 /* Check to see if TYPE refers to an Objective-C selector name. */
35157 cp_parser_objc_selector_p (enum cpp_ttype type
)
35159 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
35160 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
35161 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
35162 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
35163 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
35166 /* Parse an Objective-C selector. */
35169 cp_parser_objc_selector (cp_parser
* parser
)
35171 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
35173 if (!cp_parser_objc_selector_p (token
->type
))
35175 error_at (token
->location
, "invalid Objective-C++ selector name");
35176 return error_mark_node
;
35179 /* C++ operator names are allowed to appear in ObjC selectors. */
35180 switch (token
->type
)
35182 case CPP_AND_AND
: return get_identifier ("and");
35183 case CPP_AND_EQ
: return get_identifier ("and_eq");
35184 case CPP_AND
: return get_identifier ("bitand");
35185 case CPP_OR
: return get_identifier ("bitor");
35186 case CPP_COMPL
: return get_identifier ("compl");
35187 case CPP_NOT
: return get_identifier ("not");
35188 case CPP_NOT_EQ
: return get_identifier ("not_eq");
35189 case CPP_OR_OR
: return get_identifier ("or");
35190 case CPP_OR_EQ
: return get_identifier ("or_eq");
35191 case CPP_XOR
: return get_identifier ("xor");
35192 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
35193 default: return token
->u
.value
;
35197 /* Parse an Objective-C params list. */
35200 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
35202 tree params
= NULL_TREE
;
35203 bool maybe_unary_selector_p
= true;
35204 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35206 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
35208 tree selector
= NULL_TREE
, type_name
, identifier
;
35209 tree parm_attr
= NULL_TREE
;
35211 if (token
->keyword
== RID_ATTRIBUTE
)
35214 if (token
->type
!= CPP_COLON
)
35215 selector
= cp_parser_objc_selector (parser
);
35217 /* Detect if we have a unary selector. */
35218 if (maybe_unary_selector_p
35219 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
35221 params
= selector
; /* Might be followed by attributes. */
35225 maybe_unary_selector_p
= false;
35226 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
35228 /* Something went quite wrong. There should be a colon
35229 here, but there is not. Stop parsing parameters. */
35232 type_name
= cp_parser_objc_typename (parser
);
35233 /* New ObjC allows attributes on parameters too. */
35234 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
35235 parm_attr
= cp_parser_attributes_opt (parser
);
35236 identifier
= cp_parser_identifier (parser
);
35240 objc_build_keyword_decl (selector
,
35245 token
= cp_lexer_peek_token (parser
->lexer
);
35248 if (params
== NULL_TREE
)
35250 cp_parser_error (parser
, "objective-c++ method declaration is expected");
35251 return error_mark_node
;
35254 /* We allow tail attributes for the method. */
35255 if (token
->keyword
== RID_ATTRIBUTE
)
35257 *attributes
= cp_parser_attributes_opt (parser
);
35258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
35259 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35261 cp_parser_error (parser
,
35262 "method attributes must be specified at the end");
35263 return error_mark_node
;
35266 if (params
== NULL_TREE
)
35268 cp_parser_error (parser
, "objective-c++ method declaration is expected");
35269 return error_mark_node
;
35274 /* Parse the non-keyword Objective-C params. */
35277 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
35280 tree params
= make_node (TREE_LIST
);
35281 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35282 *ellipsisp
= false; /* Initially, assume no ellipsis. */
35284 while (token
->type
== CPP_COMMA
)
35286 cp_parameter_declarator
*parmdecl
;
35289 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
35290 token
= cp_lexer_peek_token (parser
->lexer
);
35292 if (token
->type
== CPP_ELLIPSIS
)
35294 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
35296 token
= cp_lexer_peek_token (parser
->lexer
);
35300 /* TODO: parse attributes for tail parameters. */
35301 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
35303 parm
= grokdeclarator (parmdecl
->declarator
,
35304 &parmdecl
->decl_specifiers
,
35305 PARM
, /*initialized=*/0,
35306 /*attrlist=*/NULL
);
35308 chainon (params
, build_tree_list (NULL_TREE
, parm
));
35309 token
= cp_lexer_peek_token (parser
->lexer
);
35312 /* We allow tail attributes for the method. */
35313 if (token
->keyword
== RID_ATTRIBUTE
)
35315 if (*attributes
== NULL_TREE
)
35317 *attributes
= cp_parser_attributes_opt (parser
);
35318 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
35319 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35323 /* We have an error, but parse the attributes, so that we can
35325 *attributes
= cp_parser_attributes_opt (parser
);
35327 cp_parser_error (parser
,
35328 "method attributes must be specified at the end");
35329 return error_mark_node
;
35335 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
35338 cp_parser_objc_interstitial_code (cp_parser
* parser
)
35340 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35342 /* If the next token is `extern' and the following token is a string
35343 literal, then we have a linkage specification. */
35344 if (token
->keyword
== RID_EXTERN
35345 && cp_parser_is_pure_string_literal
35346 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
35347 cp_parser_linkage_specification (parser
, NULL_TREE
);
35348 /* Handle #pragma, if any. */
35349 else if (token
->type
== CPP_PRAGMA
)
35350 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
35351 /* Allow stray semicolons. */
35352 else if (token
->type
== CPP_SEMICOLON
)
35353 cp_lexer_consume_token (parser
->lexer
);
35354 /* Mark methods as optional or required, when building protocols. */
35355 else if (token
->keyword
== RID_AT_OPTIONAL
)
35357 cp_lexer_consume_token (parser
->lexer
);
35358 objc_set_method_opt (true);
35360 else if (token
->keyword
== RID_AT_REQUIRED
)
35362 cp_lexer_consume_token (parser
->lexer
);
35363 objc_set_method_opt (false);
35365 else if (token
->keyword
== RID_NAMESPACE
)
35366 cp_parser_namespace_definition (parser
);
35367 /* Other stray characters must generate errors. */
35368 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
35370 cp_lexer_consume_token (parser
->lexer
);
35371 error ("stray %qs between Objective-C++ methods",
35372 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
35374 /* Finally, try to parse a block-declaration, or a function-definition. */
35376 cp_parser_block_declaration (parser
, /*statement_p=*/false);
35379 /* Parse a method signature. */
35382 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
35384 tree rettype
, kwdparms
, optparms
;
35385 bool ellipsis
= false;
35386 bool is_class_method
;
35388 is_class_method
= cp_parser_objc_method_type (parser
);
35389 rettype
= cp_parser_objc_typename (parser
);
35390 *attributes
= NULL_TREE
;
35391 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
35392 if (kwdparms
== error_mark_node
)
35393 return error_mark_node
;
35394 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
35395 if (optparms
== error_mark_node
)
35396 return error_mark_node
;
35398 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
35402 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
35405 cp_lexer_save_tokens (parser
->lexer
);
35406 tattr
= cp_parser_attributes_opt (parser
);
35407 gcc_assert (tattr
) ;
35409 /* If the attributes are followed by a method introducer, this is not allowed.
35410 Dump the attributes and flag the situation. */
35411 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
35412 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
35415 /* Otherwise, the attributes introduce some interstitial code, possibly so
35416 rewind to allow that check. */
35417 cp_lexer_rollback_tokens (parser
->lexer
);
35421 /* Parse an Objective-C method prototype list. */
35424 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
35426 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35428 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
35430 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
35432 tree attributes
, sig
;
35433 bool is_class_method
;
35434 if (token
->type
== CPP_PLUS
)
35435 is_class_method
= true;
35437 is_class_method
= false;
35438 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
35439 if (sig
== error_mark_node
)
35441 cp_parser_skip_to_end_of_block_or_statement (parser
);
35442 token
= cp_lexer_peek_token (parser
->lexer
);
35445 objc_add_method_declaration (is_class_method
, sig
, attributes
);
35446 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35448 else if (token
->keyword
== RID_AT_PROPERTY
)
35449 cp_parser_objc_at_property_declaration (parser
);
35450 else if (token
->keyword
== RID_ATTRIBUTE
35451 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
35452 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35454 "prefix attributes are ignored for methods");
35456 /* Allow for interspersed non-ObjC++ code. */
35457 cp_parser_objc_interstitial_code (parser
);
35459 token
= cp_lexer_peek_token (parser
->lexer
);
35462 if (token
->type
!= CPP_EOF
)
35463 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
35465 cp_parser_error (parser
, "expected %<@end%>");
35467 objc_finish_interface ();
35470 /* Parse an Objective-C method definition list. */
35473 cp_parser_objc_method_definition_list (cp_parser
* parser
)
35477 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35479 if (token
->keyword
== RID_AT_END
)
35481 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
35484 else if (token
->type
== CPP_EOF
)
35486 cp_parser_error (parser
, "expected %<@end%>");
35489 else if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
35491 bool is_class_method
= token
->type
== CPP_PLUS
;
35493 push_deferring_access_checks (dk_deferred
);
35495 tree sig
= cp_parser_objc_method_signature (parser
, &attribute
);
35496 if (sig
== error_mark_node
)
35497 cp_parser_skip_to_end_of_block_or_statement (parser
);
35500 objc_start_method_definition (is_class_method
, sig
,
35501 attribute
, NULL_TREE
);
35503 /* For historical reasons, we accept an optional semicolon. */
35504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35505 cp_lexer_consume_token (parser
->lexer
);
35507 perform_deferred_access_checks (tf_warning_or_error
);
35508 stop_deferring_access_checks ();
35510 = cp_parser_function_definition_after_declarator (parser
, false);
35511 pop_deferring_access_checks ();
35512 objc_finish_method_definition (meth
);
35515 /* The following case will be removed once @synthesize is
35516 completely implemented. */
35517 else if (token
->keyword
== RID_AT_PROPERTY
)
35518 cp_parser_objc_at_property_declaration (parser
);
35519 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
35520 cp_parser_objc_at_synthesize_declaration (parser
);
35521 else if (token
->keyword
== RID_AT_DYNAMIC
)
35522 cp_parser_objc_at_dynamic_declaration (parser
);
35523 else if (token
->keyword
== RID_ATTRIBUTE
35524 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
35525 warning_at (token
->location
, OPT_Wattributes
,
35526 "prefix attributes are ignored for methods");
35528 /* Allow for interspersed non-ObjC++ code. */
35529 cp_parser_objc_interstitial_code (parser
);
35532 objc_finish_implementation ();
35535 /* Parse Objective-C ivars. */
35538 cp_parser_objc_class_ivars (cp_parser
* parser
)
35540 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35542 if (token
->type
!= CPP_OPEN_BRACE
)
35543 return; /* No ivars specified. */
35545 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
35546 token
= cp_lexer_peek_token (parser
->lexer
);
35548 while (token
->type
!= CPP_CLOSE_BRACE
35549 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
35551 cp_decl_specifier_seq declspecs
;
35552 int decl_class_or_enum_p
;
35553 tree prefix_attributes
;
35555 cp_parser_objc_visibility_spec (parser
);
35557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35560 cp_parser_decl_specifier_seq (parser
,
35561 CP_PARSER_FLAGS_OPTIONAL
,
35563 &decl_class_or_enum_p
);
35565 /* auto, register, static, extern, mutable. */
35566 if (declspecs
.storage_class
!= sc_none
)
35568 cp_parser_error (parser
, "invalid type for instance variable");
35569 declspecs
.storage_class
= sc_none
;
35572 /* thread_local. */
35573 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
35575 cp_parser_error (parser
, "invalid type for instance variable");
35576 declspecs
.locations
[ds_thread
] = 0;
35580 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
35582 cp_parser_error (parser
, "invalid type for instance variable");
35583 declspecs
.locations
[ds_typedef
] = 0;
35586 prefix_attributes
= declspecs
.attributes
;
35587 declspecs
.attributes
= NULL_TREE
;
35589 /* Keep going until we hit the `;' at the end of the
35591 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35593 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
35594 cp_declarator
*declarator
= NULL
;
35595 int ctor_dtor_or_conv_p
;
35597 /* Check for a (possibly unnamed) bitfield declaration. */
35598 token
= cp_lexer_peek_token (parser
->lexer
);
35599 if (token
->type
== CPP_COLON
)
35602 if (token
->type
== CPP_NAME
35603 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
35606 /* Get the name of the bitfield. */
35607 declarator
= make_id_declarator (NULL_TREE
,
35608 cp_parser_identifier (parser
),
35609 sfk_none
, token
->location
);
35612 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
35613 /* Get the width of the bitfield. */
35615 = cp_parser_constant_expression (parser
);
35619 /* Parse the declarator. */
35621 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
35622 CP_PARSER_FLAGS_NONE
,
35623 &ctor_dtor_or_conv_p
,
35624 /*parenthesized_p=*/NULL
,
35625 /*member_p=*/false,
35626 /*friend_p=*/false,
35627 /*static_p=*/false);
35630 /* Look for attributes that apply to the ivar. */
35631 attributes
= cp_parser_attributes_opt (parser
);
35632 /* Remember which attributes are prefix attributes and
35634 first_attribute
= attributes
;
35635 /* Combine the attributes. */
35636 attributes
= attr_chainon (prefix_attributes
, attributes
);
35639 /* Create the bitfield declaration. */
35640 decl
= grokbitfield (declarator
, &declspecs
,
35641 width
, NULL_TREE
, attributes
);
35643 decl
= grokfield (declarator
, &declspecs
,
35644 NULL_TREE
, /*init_const_expr_p=*/false,
35645 NULL_TREE
, attributes
);
35647 /* Add the instance variable. */
35648 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
35649 objc_add_instance_variable (decl
);
35651 /* Reset PREFIX_ATTRIBUTES. */
35652 if (attributes
!= error_mark_node
)
35654 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
35655 attributes
= TREE_CHAIN (attributes
);
35657 TREE_CHAIN (attributes
) = NULL_TREE
;
35660 token
= cp_lexer_peek_token (parser
->lexer
);
35662 if (token
->type
== CPP_COMMA
)
35664 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
35670 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35671 token
= cp_lexer_peek_token (parser
->lexer
);
35674 if (token
->keyword
== RID_AT_END
)
35675 cp_parser_error (parser
, "expected %<}%>");
35677 /* Do not consume the RID_AT_END, so it will be read again as terminating
35678 the @interface of @implementation. */
35679 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
35680 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
35682 /* For historical reasons, we accept an optional semicolon. */
35683 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35684 cp_lexer_consume_token (parser
->lexer
);
35687 /* Parse an Objective-C protocol declaration. */
35690 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
35692 tree proto
, protorefs
;
35695 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
35696 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
35698 tok
= cp_lexer_peek_token (parser
->lexer
);
35699 error_at (tok
->location
, "identifier expected after %<@protocol%>");
35700 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35704 /* See if we have a forward declaration or a definition. */
35705 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
35707 /* Try a forward declaration first. */
35708 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
35714 id
= cp_parser_identifier (parser
);
35715 if (id
== error_mark_node
)
35718 objc_declare_protocol (id
, attributes
);
35720 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35721 cp_lexer_consume_token (parser
->lexer
);
35725 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35728 /* Ok, we got a full-fledged definition (or at least should). */
35731 proto
= cp_parser_identifier (parser
);
35732 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
35733 objc_start_protocol (proto
, protorefs
, attributes
);
35734 cp_parser_objc_method_prototype_list (parser
);
35738 /* Parse an Objective-C superclass or category. */
35741 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
35744 tree
*categ
, bool *is_class_extension
)
35746 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
35748 *super
= *categ
= NULL_TREE
;
35749 *is_class_extension
= false;
35750 if (next
->type
== CPP_COLON
)
35752 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
35753 *super
= cp_parser_identifier (parser
);
35755 else if (next
->type
== CPP_OPEN_PAREN
)
35757 matching_parens parens
;
35758 parens
.consume_open (parser
); /* Eat '('. */
35760 /* If there is no category name, and this is an @interface, we
35761 have a class extension. */
35762 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
35764 *categ
= NULL_TREE
;
35765 *is_class_extension
= true;
35768 *categ
= cp_parser_identifier (parser
);
35770 parens
.require_close (parser
);
35774 /* Parse an Objective-C class interface. */
35777 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
35779 tree name
, super
, categ
, protos
;
35780 bool is_class_extension
;
35782 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
35783 location_t nam_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35784 name
= cp_parser_identifier (parser
);
35785 if (name
== error_mark_node
)
35787 /* It's hard to recover because even if valid @interface stuff
35788 is to follow, we can't compile it (or validate it) if we
35789 don't even know which class it refers to. Let's assume this
35790 was a stray '@interface' token in the stream and skip it.
35794 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
35795 &is_class_extension
);
35796 protos
= cp_parser_objc_protocol_refs_opt (parser
);
35798 /* We have either a class or a category on our hands. */
35799 if (categ
|| is_class_extension
)
35800 objc_start_category_interface (name
, categ
, protos
, attributes
);
35803 objc_start_class_interface (name
, nam_loc
, super
, protos
, attributes
);
35804 /* Handle instance variable declarations, if any. */
35805 cp_parser_objc_class_ivars (parser
);
35806 objc_continue_interface ();
35809 cp_parser_objc_method_prototype_list (parser
);
35812 /* Parse an Objective-C class implementation. */
35815 cp_parser_objc_class_implementation (cp_parser
* parser
)
35817 tree name
, super
, categ
;
35818 bool is_class_extension
;
35820 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
35821 name
= cp_parser_identifier (parser
);
35822 if (name
== error_mark_node
)
35824 /* It's hard to recover because even if valid @implementation
35825 stuff is to follow, we can't compile it (or validate it) if
35826 we don't even know which class it refers to. Let's assume
35827 this was a stray '@implementation' token in the stream and
35832 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
35833 &is_class_extension
);
35835 /* We have either a class or a category on our hands. */
35837 objc_start_category_implementation (name
, categ
);
35840 objc_start_class_implementation (name
, super
);
35841 /* Handle instance variable declarations, if any. */
35842 cp_parser_objc_class_ivars (parser
);
35843 objc_continue_implementation ();
35846 cp_parser_objc_method_definition_list (parser
);
35849 /* Consume the @end token and finish off the implementation. */
35852 cp_parser_objc_end_implementation (cp_parser
* parser
)
35854 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
35855 objc_finish_implementation ();
35858 /* Parse an Objective-C declaration. */
35861 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
35863 /* Try to figure out what kind of declaration is present. */
35864 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
35867 switch (kwd
->keyword
)
35872 error_at (kwd
->location
, "attributes may not be specified before"
35873 " the %<@%D%> Objective-C++ keyword",
35877 case RID_AT_IMPLEMENTATION
:
35878 warning_at (kwd
->location
, OPT_Wattributes
,
35879 "prefix attributes are ignored before %<@%D%>",
35886 switch (kwd
->keyword
)
35889 cp_parser_objc_alias_declaration (parser
);
35892 cp_parser_objc_class_declaration (parser
);
35894 case RID_AT_PROTOCOL
:
35895 cp_parser_objc_protocol_declaration (parser
, attributes
);
35897 case RID_AT_INTERFACE
:
35898 cp_parser_objc_class_interface (parser
, attributes
);
35900 case RID_AT_IMPLEMENTATION
:
35901 cp_parser_objc_class_implementation (parser
);
35904 cp_parser_objc_end_implementation (parser
);
35907 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
35909 cp_parser_skip_to_end_of_block_or_statement (parser
);
35913 /* Parse an Objective-C try-catch-finally statement.
35915 objc-try-catch-finally-stmt:
35916 @try compound-statement objc-catch-clause-seq [opt]
35917 objc-finally-clause [opt]
35919 objc-catch-clause-seq:
35920 objc-catch-clause objc-catch-clause-seq [opt]
35923 @catch ( objc-exception-declaration ) compound-statement
35925 objc-finally-clause:
35926 @finally compound-statement
35928 objc-exception-declaration:
35929 parameter-declaration
35932 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
35936 PS: This function is identical to c_parser_objc_try_catch_finally_statement
35937 for C. Keep them in sync. */
35940 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
35942 location_t location
;
35945 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
35946 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
35947 objc_maybe_warn_exceptions (location
);
35948 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
35949 node, lest it get absorbed into the surrounding block. */
35950 stmt
= push_stmt_list ();
35951 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
35952 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
35954 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
35956 cp_parameter_declarator
*parm
;
35957 tree parameter_declaration
= error_mark_node
;
35958 bool seen_open_paren
= false;
35959 matching_parens parens
;
35961 cp_lexer_consume_token (parser
->lexer
);
35962 if (parens
.require_open (parser
))
35963 seen_open_paren
= true;
35964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
35966 /* We have "@catch (...)" (where the '...' are literally
35967 what is in the code). Skip the '...'.
35968 parameter_declaration is set to NULL_TREE, and
35969 objc_being_catch_clauses() knows that that means
35971 cp_lexer_consume_token (parser
->lexer
);
35972 parameter_declaration
= NULL_TREE
;
35976 /* We have "@catch (NSException *exception)" or something
35977 like that. Parse the parameter declaration. */
35978 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
35981 parameter_declaration
= error_mark_node
;
35983 parameter_declaration
= grokdeclarator (parm
->declarator
,
35984 &parm
->decl_specifiers
,
35985 PARM
, /*initialized=*/0,
35986 /*attrlist=*/NULL
);
35988 if (seen_open_paren
)
35989 parens
.require_close (parser
);
35992 /* If there was no open parenthesis, we are recovering from
35993 an error, and we are trying to figure out what mistake
35994 the user has made. */
35996 /* If there is an immediate closing parenthesis, the user
35997 probably forgot the opening one (ie, they typed "@catch
35998 NSException *e)". Parse the closing parenthesis and keep
36000 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
36001 cp_lexer_consume_token (parser
->lexer
);
36003 /* If these is no immediate closing parenthesis, the user
36004 probably doesn't know that parenthesis are required at
36005 all (ie, they typed "@catch NSException *e"). So, just
36006 forget about the closing parenthesis and keep going. */
36008 objc_begin_catch_clause (parameter_declaration
);
36009 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
36010 objc_finish_catch_clause ();
36012 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
36014 cp_lexer_consume_token (parser
->lexer
);
36015 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
36016 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
36017 node, lest it get absorbed into the surrounding block. */
36018 stmt
= push_stmt_list ();
36019 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
36020 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
36023 return objc_finish_try_stmt ();
36026 /* Parse an Objective-C synchronized statement.
36028 objc-synchronized-stmt:
36029 @synchronized ( expression ) compound-statement
36031 Returns NULL_TREE. */
36034 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
36036 location_t location
;
36039 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
36041 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
36042 objc_maybe_warn_exceptions (location
);
36043 matching_parens parens
;
36044 parens
.require_open (parser
);
36045 lock
= cp_parser_expression (parser
);
36046 parens
.require_close (parser
);
36048 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
36049 node, lest it get absorbed into the surrounding block. */
36050 stmt
= push_stmt_list ();
36051 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
36053 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
36056 /* Parse an Objective-C throw statement.
36059 @throw assignment-expression [opt] ;
36061 Returns a constructed '@throw' statement. */
36064 cp_parser_objc_throw_statement (cp_parser
*parser
)
36066 tree expr
= NULL_TREE
;
36067 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36069 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
36071 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36072 expr
= cp_parser_expression (parser
);
36074 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36076 return objc_build_throw_stmt (loc
, expr
);
36079 /* Parse an Objective-C statement. */
36082 cp_parser_objc_statement (cp_parser
* parser
)
36084 /* Try to figure out what kind of declaration is present. */
36085 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
36087 switch (kwd
->keyword
)
36090 return cp_parser_objc_try_catch_finally_statement (parser
);
36091 case RID_AT_SYNCHRONIZED
:
36092 return cp_parser_objc_synchronized_statement (parser
);
36094 return cp_parser_objc_throw_statement (parser
);
36096 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
36098 cp_parser_skip_to_end_of_block_or_statement (parser
);
36101 return error_mark_node
;
36104 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
36105 look ahead to see if an objc keyword follows the attributes. This
36106 is to detect the use of prefix attributes on ObjC @interface and
36110 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
36112 cp_lexer_save_tokens (parser
->lexer
);
36113 tree addon
= cp_parser_attributes_opt (parser
);
36115 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
36117 cp_lexer_commit_tokens (parser
->lexer
);
36119 TREE_CHAIN (*attrib
) = addon
;
36124 cp_lexer_rollback_tokens (parser
->lexer
);
36128 /* This routine is a minimal replacement for
36129 c_parser_struct_declaration () used when parsing the list of
36130 types/names or ObjC++ properties. For example, when parsing the
36133 @property (readonly) int a, b, c;
36135 this function is responsible for parsing "int a, int b, int c" and
36136 returning the declarations as CHAIN of DECLs.
36138 TODO: Share this code with cp_parser_objc_class_ivars. It's very
36139 similar parsing. */
36141 cp_parser_objc_struct_declaration (cp_parser
*parser
)
36143 tree decls
= NULL_TREE
;
36144 cp_decl_specifier_seq declspecs
;
36145 int decl_class_or_enum_p
;
36146 tree prefix_attributes
;
36148 cp_parser_decl_specifier_seq (parser
,
36149 CP_PARSER_FLAGS_NONE
,
36151 &decl_class_or_enum_p
);
36153 if (declspecs
.type
== error_mark_node
)
36154 return error_mark_node
;
36156 /* auto, register, static, extern, mutable. */
36157 if (declspecs
.storage_class
!= sc_none
)
36159 cp_parser_error (parser
, "invalid type for property");
36160 declspecs
.storage_class
= sc_none
;
36163 /* thread_local. */
36164 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
36166 cp_parser_error (parser
, "invalid type for property");
36167 declspecs
.locations
[ds_thread
] = 0;
36171 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
36173 cp_parser_error (parser
, "invalid type for property");
36174 declspecs
.locations
[ds_typedef
] = 0;
36177 prefix_attributes
= declspecs
.attributes
;
36178 declspecs
.attributes
= NULL_TREE
;
36180 /* Keep going until we hit the `;' at the end of the declaration. */
36181 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36183 tree attributes
, first_attribute
, decl
;
36184 cp_declarator
*declarator
;
36187 /* Parse the declarator. */
36188 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
36189 CP_PARSER_FLAGS_NONE
,
36190 NULL
, NULL
, false, false, false);
36192 /* Look for attributes that apply to the ivar. */
36193 attributes
= cp_parser_attributes_opt (parser
);
36194 /* Remember which attributes are prefix attributes and
36196 first_attribute
= attributes
;
36197 /* Combine the attributes. */
36198 attributes
= attr_chainon (prefix_attributes
, attributes
);
36200 decl
= grokfield (declarator
, &declspecs
,
36201 NULL_TREE
, /*init_const_expr_p=*/false,
36202 NULL_TREE
, attributes
);
36204 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
36205 return error_mark_node
;
36207 /* Reset PREFIX_ATTRIBUTES. */
36208 if (attributes
!= error_mark_node
)
36210 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
36211 attributes
= TREE_CHAIN (attributes
);
36213 TREE_CHAIN (attributes
) = NULL_TREE
;
36216 DECL_CHAIN (decl
) = decls
;
36219 token
= cp_lexer_peek_token (parser
->lexer
);
36220 if (token
->type
== CPP_COMMA
)
36222 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
36231 /* Parse an Objective-C @property declaration. The syntax is:
36233 objc-property-declaration:
36234 '@property' objc-property-attributes[opt] struct-declaration ;
36236 objc-property-attributes:
36237 '(' objc-property-attribute-list ')'
36239 objc-property-attribute-list:
36240 objc-property-attribute
36241 objc-property-attribute-list, objc-property-attribute
36243 objc-property-attribute
36244 'getter' = identifier
36245 'setter' = identifier
36254 @property NSString *name;
36255 @property (readonly) id object;
36256 @property (retain, nonatomic, getter=getTheName) id name;
36257 @property int a, b, c;
36259 PS: This function is identical to
36260 c_parser_objc_at_property_declaration for C. Keep them in sync. */
36262 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
36264 /* Parse the optional attribute list.
36266 A list of parsed, but not verified, attributes. */
36267 auto_delete_vec
<property_attribute_info
> prop_attr_list
;
36268 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36270 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
36272 /* Parse the optional attribute list... */
36273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36276 matching_parens parens
;
36277 location_t attr_start
= cp_lexer_peek_token (parser
->lexer
)->location
;
36278 parens
.consume_open (parser
);
36279 bool syntax_error
= false;
36281 /* Allow empty @property attribute lists, but with a warning. */
36282 location_t attr_end
= cp_lexer_peek_token (parser
->lexer
)->location
;
36283 location_t attr_comb
;
36284 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
36286 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
36287 warning_at (attr_comb
, OPT_Wattributes
,
36288 "empty property attribute list");
36293 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36294 attr_start
= token
->location
;
36295 attr_end
= get_finish (token
->location
);
36296 attr_comb
= make_location (attr_start
, attr_start
, attr_end
);
36298 if (token
->type
== CPP_CLOSE_PAREN
|| token
->type
== CPP_COMMA
)
36300 warning_at (attr_comb
, OPT_Wattributes
,
36301 "missing property attribute");
36302 if (token
->type
== CPP_CLOSE_PAREN
)
36304 cp_lexer_consume_token (parser
->lexer
);
36308 tree attr_name
= NULL_TREE
;
36309 if (identifier_p (token
->u
.value
))
36310 attr_name
= token
->u
.value
;
36313 if (token
->type
== CPP_NAME
)
36314 keyword
= C_RID_CODE (token
->u
.value
);
36315 else if (token
->type
== CPP_KEYWORD
36316 && token
->keyword
== RID_CLASS
)
36317 /* Account for accepting the 'class' keyword in this context. */
36318 keyword
= RID_CLASS
;
36320 keyword
= RID_MAX
; /* By definition, an unknown property. */
36321 cp_lexer_consume_token (parser
->lexer
);
36323 enum objc_property_attribute_kind prop_kind
36324 = objc_prop_attr_kind_for_rid (keyword
);
36325 property_attribute_info
*prop
36326 = new property_attribute_info (attr_name
, attr_comb
, prop_kind
);
36327 prop_attr_list
.safe_push (prop
);
36330 switch (prop
->prop_kind
)
36333 case OBJC_PROPERTY_ATTR_UNKNOWN
:
36335 error_at (attr_start
, "unknown property attribute %qE",
36338 error_at (attr_start
, "unknown property attribute");
36339 prop
->parse_error
= syntax_error
= true;
36342 case OBJC_PROPERTY_ATTR_GETTER
:
36343 case OBJC_PROPERTY_ATTR_SETTER
:
36344 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
36346 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
36347 error_at (attr_comb
, "expected %<=%> after Objective-C %qE",
36349 prop
->parse_error
= syntax_error
= true;
36353 token
= cp_lexer_peek_token (parser
->lexer
);
36354 attr_end
= token
->location
;
36355 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
36357 if (!cp_parser_objc_selector_p
36358 (cp_lexer_peek_token (parser
->lexer
)->type
))
36360 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
36361 error_at (attr_comb
, "expected %qE selector name",
36363 prop
->parse_error
= syntax_error
= true;
36367 /* Get the end of the method name, and consume the name. */
36368 token
= cp_lexer_peek_token (parser
->lexer
);
36369 attr_end
= get_finish (token
->location
);
36370 /* Because method names may contain C++ keywords, we have a
36371 routine to fetch them (this also consumes the token). */
36372 meth_name
= cp_parser_objc_selector (parser
);
36374 if (prop
->prop_kind
== OBJC_PROPERTY_ATTR_SETTER
)
36376 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
36378 attr_comb
= make_location (attr_end
, attr_start
,
36380 error_at (attr_comb
, "setter method names must"
36381 " terminate with %<:%>");
36382 prop
->parse_error
= syntax_error
= true;
36386 attr_end
= get_finish (cp_lexer_peek_token
36387 (parser
->lexer
)->location
);
36388 cp_lexer_consume_token (parser
->lexer
);
36390 attr_comb
= make_location (attr_start
, attr_start
,
36394 attr_comb
= make_location (attr_start
, attr_start
,
36396 prop
->ident
= meth_name
;
36397 /* Updated location including all that was successfully
36399 prop
->prop_loc
= attr_comb
;
36403 /* If we see a comma here, then keep going - even if we already
36404 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
36405 this makes a more useful output and avoid spurious warnings
36406 about missing attributes that are, in fact, specified after the
36407 one with the syntax error. */
36408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36409 cp_lexer_consume_token (parser
->lexer
);
36414 if (syntax_error
|| !parens
.require_close (parser
))
36415 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36416 /*or_comma=*/false,
36417 /*consume_paren=*/true);
36420 /* 'properties' is the list of properties that we read. Usually a
36421 single one, but maybe more (eg, in "@property int a, b, c;" there
36423 TODO: Update this parsing so that it accepts (erroneous) bitfields so
36424 that we can issue a meaningful and consistent (between C/C++) error
36425 message from objc_add_property_declaration (). */
36426 tree properties
= cp_parser_objc_struct_declaration (parser
);
36428 if (properties
== error_mark_node
)
36429 cp_parser_skip_to_end_of_statement (parser
);
36430 else if (properties
== NULL_TREE
)
36431 cp_parser_error (parser
, "expected identifier");
36434 /* Comma-separated properties are chained together in reverse order;
36435 add them one by one. */
36436 properties
= nreverse (properties
);
36437 for (; properties
; properties
= TREE_CHAIN (properties
))
36438 objc_add_property_declaration (loc
, copy_node (properties
),
36442 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36445 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
36447 objc-synthesize-declaration:
36448 @synthesize objc-synthesize-identifier-list ;
36450 objc-synthesize-identifier-list:
36451 objc-synthesize-identifier
36452 objc-synthesize-identifier-list, objc-synthesize-identifier
36454 objc-synthesize-identifier
36456 identifier = identifier
36459 @synthesize MyProperty;
36460 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
36462 PS: This function is identical to c_parser_objc_at_synthesize_declaration
36463 for C. Keep them in sync.
36466 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
36468 tree list
= NULL_TREE
;
36470 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36472 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
36475 tree property
, ivar
;
36476 property
= cp_parser_identifier (parser
);
36477 if (property
== error_mark_node
)
36479 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
36484 cp_lexer_consume_token (parser
->lexer
);
36485 ivar
= cp_parser_identifier (parser
);
36486 if (ivar
== error_mark_node
)
36488 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36494 list
= chainon (list
, build_tree_list (ivar
, property
));
36495 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36496 cp_lexer_consume_token (parser
->lexer
);
36500 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36501 objc_add_synthesize_declaration (loc
, list
);
36504 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
36506 objc-dynamic-declaration:
36507 @dynamic identifier-list ;
36510 @dynamic MyProperty;
36511 @dynamic MyProperty, AnotherProperty;
36513 PS: This function is identical to c_parser_objc_at_dynamic_declaration
36514 for C. Keep them in sync.
36517 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
36519 tree list
= NULL_TREE
;
36521 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36523 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
36527 property
= cp_parser_identifier (parser
);
36528 if (property
== error_mark_node
)
36530 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36533 list
= chainon (list
, build_tree_list (NULL
, property
));
36534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36535 cp_lexer_consume_token (parser
->lexer
);
36539 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36540 objc_add_dynamic_declaration (loc
, list
);
36544 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
36546 /* Returns name of the next clause.
36547 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
36548 the token is not consumed. Otherwise appropriate pragma_omp_clause is
36549 returned and the token is consumed. */
36551 static pragma_omp_clause
36552 cp_parser_omp_clause_name (cp_parser
*parser
)
36554 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
36556 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
36557 result
= PRAGMA_OACC_CLAUSE_AUTO
;
36558 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
36559 result
= PRAGMA_OMP_CLAUSE_IF
;
36560 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
36561 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
36562 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
36563 result
= PRAGMA_OACC_CLAUSE_DELETE
;
36564 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
36565 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
36566 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36567 result
= PRAGMA_OMP_CLAUSE_FOR
;
36568 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36570 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36571 const char *p
= IDENTIFIER_POINTER (id
);
36576 if (!strcmp ("affinity", p
))
36577 result
= PRAGMA_OMP_CLAUSE_AFFINITY
;
36578 else if (!strcmp ("aligned", p
))
36579 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
36580 else if (!strcmp ("allocate", p
))
36581 result
= PRAGMA_OMP_CLAUSE_ALLOCATE
;
36582 else if (!strcmp ("async", p
))
36583 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
36584 else if (!strcmp ("attach", p
))
36585 result
= PRAGMA_OACC_CLAUSE_ATTACH
;
36588 if (!strcmp ("bind", p
))
36589 result
= PRAGMA_OMP_CLAUSE_BIND
;
36592 if (!strcmp ("collapse", p
))
36593 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
36594 else if (!strcmp ("copy", p
))
36595 result
= PRAGMA_OACC_CLAUSE_COPY
;
36596 else if (!strcmp ("copyin", p
))
36597 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
36598 else if (!strcmp ("copyout", p
))
36599 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
36600 else if (!strcmp ("copyprivate", p
))
36601 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
36602 else if (!strcmp ("create", p
))
36603 result
= PRAGMA_OACC_CLAUSE_CREATE
;
36606 if (!strcmp ("defaultmap", p
))
36607 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
36608 else if (!strcmp ("depend", p
))
36609 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
36610 else if (!strcmp ("detach", p
))
36611 result
= PRAGMA_OACC_CLAUSE_DETACH
;
36612 else if (!strcmp ("device", p
))
36613 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
36614 else if (!strcmp ("deviceptr", p
))
36615 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
36616 else if (!strcmp ("device_resident", p
))
36617 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
36618 else if (!strcmp ("device_type", p
))
36619 result
= PRAGMA_OMP_CLAUSE_DEVICE_TYPE
;
36620 else if (!strcmp ("dist_schedule", p
))
36621 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
36622 else if (!strcmp ("doacross", p
))
36623 result
= PRAGMA_OMP_CLAUSE_DOACROSS
;
36626 if (!strcmp ("enter", p
))
36627 result
= PRAGMA_OMP_CLAUSE_ENTER
;
36630 if (!strcmp ("filter", p
))
36631 result
= PRAGMA_OMP_CLAUSE_FILTER
;
36632 else if (!strcmp ("final", p
))
36633 result
= PRAGMA_OMP_CLAUSE_FINAL
;
36634 else if (!strcmp ("finalize", p
))
36635 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
36636 else if (!strcmp ("firstprivate", p
))
36637 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
36638 else if (!strcmp ("from", p
))
36639 result
= PRAGMA_OMP_CLAUSE_FROM
;
36642 if (!strcmp ("gang", p
))
36643 result
= PRAGMA_OACC_CLAUSE_GANG
;
36644 else if (!strcmp ("grainsize", p
))
36645 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
36648 if (!strcmp ("has_device_addr", p
))
36649 result
= PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR
;
36650 else if (!strcmp ("hint", p
))
36651 result
= PRAGMA_OMP_CLAUSE_HINT
;
36652 else if (!strcmp ("host", p
))
36653 result
= PRAGMA_OACC_CLAUSE_HOST
;
36656 if (!strcmp ("if_present", p
))
36657 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
36658 else if (!strcmp ("in_reduction", p
))
36659 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
36660 else if (!strcmp ("inbranch", p
))
36661 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
36662 else if (!strcmp ("independent", p
))
36663 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
36664 else if (!strcmp ("is_device_ptr", p
))
36665 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
36668 if (!strcmp ("lastprivate", p
))
36669 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
36670 else if (!strcmp ("linear", p
))
36671 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
36672 else if (!strcmp ("link", p
))
36673 result
= PRAGMA_OMP_CLAUSE_LINK
;
36676 if (!strcmp ("map", p
))
36677 result
= PRAGMA_OMP_CLAUSE_MAP
;
36678 else if (!strcmp ("mergeable", p
))
36679 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
36682 if (!strcmp ("no_create", p
))
36683 result
= PRAGMA_OACC_CLAUSE_NO_CREATE
;
36684 else if (!strcmp ("nogroup", p
))
36685 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
36686 else if (!strcmp ("nohost", p
))
36687 result
= PRAGMA_OACC_CLAUSE_NOHOST
;
36688 else if (!strcmp ("nontemporal", p
))
36689 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
36690 else if (!strcmp ("notinbranch", p
))
36691 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
36692 else if (!strcmp ("nowait", p
))
36693 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
36694 else if (!strcmp ("num_gangs", p
))
36695 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
36696 else if (!strcmp ("num_tasks", p
))
36697 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
36698 else if (!strcmp ("num_teams", p
))
36699 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
36700 else if (!strcmp ("num_threads", p
))
36701 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
36702 else if (!strcmp ("num_workers", p
))
36703 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
36706 if (!strcmp ("ordered", p
))
36707 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
36708 else if (!strcmp ("order", p
))
36709 result
= PRAGMA_OMP_CLAUSE_ORDER
;
36712 if (!strcmp ("parallel", p
))
36713 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
36714 else if (!strcmp ("present", p
))
36715 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
36716 else if (!strcmp ("present_or_copy", p
)
36717 || !strcmp ("pcopy", p
))
36718 result
= PRAGMA_OACC_CLAUSE_COPY
;
36719 else if (!strcmp ("present_or_copyin", p
)
36720 || !strcmp ("pcopyin", p
))
36721 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
36722 else if (!strcmp ("present_or_copyout", p
)
36723 || !strcmp ("pcopyout", p
))
36724 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
36725 else if (!strcmp ("present_or_create", p
)
36726 || !strcmp ("pcreate", p
))
36727 result
= PRAGMA_OACC_CLAUSE_CREATE
;
36728 else if (!strcmp ("priority", p
))
36729 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
36730 else if (!strcmp ("proc_bind", p
))
36731 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
36734 if (!strcmp ("reduction", p
))
36735 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
36738 if (!strcmp ("safelen", p
))
36739 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
36740 else if (!strcmp ("schedule", p
))
36741 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
36742 else if (!strcmp ("sections", p
))
36743 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
36744 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
36745 result
= PRAGMA_OACC_CLAUSE_HOST
;
36746 else if (!strcmp ("seq", p
))
36747 result
= PRAGMA_OACC_CLAUSE_SEQ
;
36748 else if (!strcmp ("shared", p
))
36749 result
= PRAGMA_OMP_CLAUSE_SHARED
;
36750 else if (!strcmp ("simd", p
))
36751 result
= PRAGMA_OMP_CLAUSE_SIMD
;
36752 else if (!strcmp ("simdlen", p
))
36753 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
36756 if (!strcmp ("task_reduction", p
))
36757 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
36758 else if (!strcmp ("taskgroup", p
))
36759 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
36760 else if (!strcmp ("thread_limit", p
))
36761 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
36762 else if (!strcmp ("threads", p
))
36763 result
= PRAGMA_OMP_CLAUSE_THREADS
;
36764 else if (!strcmp ("tile", p
))
36765 result
= PRAGMA_OACC_CLAUSE_TILE
;
36766 else if (!strcmp ("to", p
))
36767 result
= PRAGMA_OMP_CLAUSE_TO
;
36770 if (!strcmp ("uniform", p
))
36771 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
36772 else if (!strcmp ("untied", p
))
36773 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
36774 else if (!strcmp ("use_device", p
))
36775 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
36776 else if (!strcmp ("use_device_addr", p
))
36777 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
;
36778 else if (!strcmp ("use_device_ptr", p
))
36779 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
36782 if (!strcmp ("vector", p
))
36783 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
36784 else if (!strcmp ("vector_length", p
))
36785 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
36788 if (!strcmp ("wait", p
))
36789 result
= PRAGMA_OACC_CLAUSE_WAIT
;
36790 else if (!strcmp ("worker", p
))
36791 result
= PRAGMA_OACC_CLAUSE_WORKER
;
36796 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
36797 cp_lexer_consume_token (parser
->lexer
);
36802 /* Validate that a clause of the given type does not already exist. */
36805 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
36806 const char *name
, location_t location
)
36808 if (omp_find_clause (clauses
, code
))
36809 error_at (location
, "too many %qs clauses", name
);
36815 variable-list , identifier
36817 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
36818 colon). An opening parenthesis will have been consumed by the caller.
36820 If KIND is nonzero, create the appropriate node and install the decl
36821 in OMP_CLAUSE_DECL and add the node to the head of the list.
36823 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
36824 return the list created.
36826 COLON can be NULL if only closing parenthesis should end the list,
36827 or pointer to bool which will receive false if the list is terminated
36828 by closing parenthesis or true if the list is terminated by colon.
36830 The optional ALLOW_DEREF argument is true if list items can use the deref
36835 tree low_bound
, length
;
36838 omp_dim (tree lb
, tree len
, location_t lo
, bool nc
)
36839 : low_bound (lb
), length (len
), loc (lo
), no_colon (nc
) {}
36843 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
36844 tree list
, bool *colon
,
36845 bool allow_deref
= false)
36847 auto_vec
<omp_dim
> dims
;
36848 bool array_section_p
;
36850 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
36853 parser
->colon_corrects_to_scope_p
= false;
36860 if (kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36861 cp_parser_parse_tentatively (parser
);
36862 token
= cp_lexer_peek_token (parser
->lexer
);
36864 && cp_parser_is_keyword (token
, RID_THIS
))
36866 decl
= finish_this_expr ();
36867 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
36868 || CONVERT_EXPR_P (decl
))
36869 decl
= TREE_OPERAND (decl
, 0);
36870 cp_lexer_consume_token (parser
->lexer
);
36872 else if (cp_parser_is_keyword (token
, RID_FUNCTION_NAME
)
36873 || cp_parser_is_keyword (token
, RID_PRETTY_FUNCTION_NAME
)
36874 || cp_parser_is_keyword (token
, RID_C99_FUNCTION_NAME
))
36877 decl
= cp_parser_primary_expression (parser
, false, false, false,
36880 else if (kind
== OMP_CLAUSE_DEPEND
36881 && cp_parser_is_keyword (token
, RID_OMP_ALL_MEMORY
)
36882 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
36883 || cp_lexer_nth_token_is (parser
->lexer
, 2,
36886 decl
= ridpointers
[RID_OMP_ALL_MEMORY
];
36887 cp_lexer_consume_token (parser
->lexer
);
36891 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36892 /*check_dependency_p=*/true,
36893 /*template_p=*/NULL
,
36894 /*declarator_p=*/false,
36895 /*optional_p=*/false);
36896 if (name
== error_mark_node
)
36898 if ((kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36899 && cp_parser_simulate_error (parser
))
36900 goto depend_lvalue
;
36904 if (identifier_p (name
))
36905 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
36908 if (decl
== error_mark_node
)
36910 if ((kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36911 && cp_parser_simulate_error (parser
))
36912 goto depend_lvalue
;
36913 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
36917 if (outer_automatic_var_p (decl
))
36918 decl
= process_outer_var_ref (decl
, tf_warning_or_error
);
36919 if (decl
== error_mark_node
)
36921 else if (kind
!= 0)
36925 case OMP_CLAUSE__CACHE_
:
36926 /* The OpenACC cache directive explicitly only allows "array
36927 elements or subarrays". */
36928 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
36930 error_at (token
->location
, "expected %<[%>");
36931 decl
= error_mark_node
;
36935 case OMP_CLAUSE_MAP
:
36936 case OMP_CLAUSE_FROM
:
36937 case OMP_CLAUSE_TO
:
36938 start_component_ref
:
36939 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
36941 && cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
)))
36944 = cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
36945 ? CPP_DOT
: CPP_DEREF
;
36947 = cp_lexer_peek_token (parser
->lexer
)->location
;
36948 cp_id_kind idk
= CP_ID_KIND_NONE
;
36949 cp_lexer_consume_token (parser
->lexer
);
36950 decl
= convert_from_reference (decl
);
36951 decl
= (cp_parser_postfix_dot_deref_expression
36952 (parser
, ttype
, cp_expr (decl
, token
->location
),
36953 false, &idk
, loc
));
36956 case OMP_CLAUSE_AFFINITY
:
36957 case OMP_CLAUSE_DEPEND
:
36958 case OMP_CLAUSE_REDUCTION
:
36959 case OMP_CLAUSE_IN_REDUCTION
:
36960 case OMP_CLAUSE_TASK_REDUCTION
:
36961 case OMP_CLAUSE_HAS_DEVICE_ADDR
:
36962 array_section_p
= false;
36964 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
36966 location_t loc
= UNKNOWN_LOCATION
;
36967 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
36968 bool no_colon
= false;
36970 parser
->colon_corrects_to_scope_p
= false;
36971 cp_lexer_consume_token (parser
->lexer
);
36972 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
36974 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36975 low_bound
= cp_parser_expression (parser
);
36976 /* Later handling is not prepared to see through these. */
36977 gcc_checking_assert (!location_wrapper_p (low_bound
));
36980 parser
->colon_corrects_to_scope_p
36981 = saved_colon_corrects_to_scope_p
;
36982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
36984 length
= integer_one_node
;
36989 /* Look for `:'. */
36990 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36992 if ((kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36993 && cp_parser_simulate_error (parser
))
36994 goto depend_lvalue
;
36997 if (kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36998 cp_parser_commit_to_tentative_parse (parser
);
37000 array_section_p
= true;
37001 if (!cp_lexer_next_token_is (parser
->lexer
,
37004 length
= cp_parser_expression (parser
);
37005 /* Later handling is not prepared to see through these. */
37006 gcc_checking_assert (!location_wrapper_p (length
));
37009 /* Look for the closing `]'. */
37010 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
37013 if ((kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
37014 && cp_parser_simulate_error (parser
))
37015 goto depend_lvalue
;
37019 dims
.safe_push (omp_dim (low_bound
, length
, loc
, no_colon
));
37022 if ((kind
== OMP_CLAUSE_MAP
37023 || kind
== OMP_CLAUSE_FROM
37024 || kind
== OMP_CLAUSE_TO
)
37025 && !array_section_p
37026 && (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
37028 && cp_lexer_next_token_is (parser
->lexer
,
37031 for (unsigned i
= 0; i
< dims
.length (); i
++)
37033 gcc_assert (dims
[i
].length
== integer_one_node
);
37034 decl
= build_array_ref (dims
[i
].loc
,
37035 decl
, dims
[i
].low_bound
);
37037 goto start_component_ref
;
37040 for (unsigned i
= 0; i
< dims
.length (); i
++)
37041 decl
= tree_cons (dims
[i
].low_bound
, dims
[i
].length
, decl
);
37048 if (kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
37050 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
37051 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
37052 && cp_parser_simulate_error (parser
))
37055 cp_parser_abort_tentative_parse (parser
);
37056 decl
= cp_parser_assignment_expression (parser
, NULL
,
37060 cp_parser_parse_definitely (parser
);
37063 tree u
= build_omp_clause (token
->location
, kind
);
37064 OMP_CLAUSE_DECL (u
) = decl
;
37065 OMP_CLAUSE_CHAIN (u
) = list
;
37069 list
= tree_cons (decl
, NULL_TREE
, list
);
37072 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
37074 cp_lexer_consume_token (parser
->lexer
);
37078 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37080 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
37083 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
37087 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37091 /* Try to resync to an unnested comma. Copied from
37092 cp_parser_parenthesized_expression_list. */
37095 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37096 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
37097 /*recovering=*/true,
37099 /*consume_paren=*/true);
37107 /* Similarly, but expect leading and trailing parenthesis. This is a very
37108 common case for omp clauses. */
37111 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
,
37112 bool allow_deref
= false)
37114 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37115 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
,
37121 copy ( variable-list )
37122 copyin ( variable-list )
37123 copyout ( variable-list )
37124 create ( variable-list )
37125 delete ( variable-list )
37126 present ( variable-list )
37129 no_create ( variable-list )
37130 attach ( variable-list )
37131 detach ( variable-list ) */
37134 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
37137 enum gomp_map_kind kind
;
37140 case PRAGMA_OACC_CLAUSE_ATTACH
:
37141 kind
= GOMP_MAP_ATTACH
;
37143 case PRAGMA_OACC_CLAUSE_COPY
:
37144 kind
= GOMP_MAP_TOFROM
;
37146 case PRAGMA_OACC_CLAUSE_COPYIN
:
37147 kind
= GOMP_MAP_TO
;
37149 case PRAGMA_OACC_CLAUSE_COPYOUT
:
37150 kind
= GOMP_MAP_FROM
;
37152 case PRAGMA_OACC_CLAUSE_CREATE
:
37153 kind
= GOMP_MAP_ALLOC
;
37155 case PRAGMA_OACC_CLAUSE_DELETE
:
37156 kind
= GOMP_MAP_RELEASE
;
37158 case PRAGMA_OACC_CLAUSE_DETACH
:
37159 kind
= GOMP_MAP_DETACH
;
37161 case PRAGMA_OACC_CLAUSE_DEVICE
:
37162 kind
= GOMP_MAP_FORCE_TO
;
37164 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
37165 kind
= GOMP_MAP_DEVICE_RESIDENT
;
37167 case PRAGMA_OACC_CLAUSE_HOST
:
37168 kind
= GOMP_MAP_FORCE_FROM
;
37170 case PRAGMA_OACC_CLAUSE_LINK
:
37171 kind
= GOMP_MAP_LINK
;
37173 case PRAGMA_OACC_CLAUSE_NO_CREATE
:
37174 kind
= GOMP_MAP_IF_PRESENT
;
37176 case PRAGMA_OACC_CLAUSE_PRESENT
:
37177 kind
= GOMP_MAP_FORCE_PRESENT
;
37180 gcc_unreachable ();
37183 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
, true);
37185 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
37186 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
37192 deviceptr ( variable-list ) */
37195 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
37197 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37200 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
37201 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
37202 variable-list must only allow for pointer variables. */
37203 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37204 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
37206 tree v
= TREE_PURPOSE (t
);
37207 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
37208 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
37209 OMP_CLAUSE_DECL (u
) = v
;
37210 OMP_CLAUSE_CHAIN (u
) = list
;
37225 cp_parser_oacc_simple_clause (location_t loc
, enum omp_clause_code code
,
37228 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], loc
);
37230 tree c
= build_omp_clause (loc
, code
);
37231 OMP_CLAUSE_CHAIN (c
) = list
;
37237 num_gangs ( expression )
37238 num_workers ( expression )
37239 vector_length ( expression ) */
37242 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
37243 const char *str
, tree list
)
37245 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37247 matching_parens parens
;
37248 if (!parens
.require_open (parser
))
37251 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
37253 if (t
== error_mark_node
37254 || !parens
.require_close (parser
))
37256 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37257 /*or_comma=*/false,
37258 /*consume_paren=*/true);
37262 check_no_duplicate_clause (list
, code
, str
, loc
);
37264 tree c
= build_omp_clause (loc
, code
);
37265 OMP_CLAUSE_OPERAND (c
, 0) = t
;
37266 OMP_CLAUSE_CHAIN (c
) = list
;
37272 gang [( gang-arg-list )]
37273 worker [( [num:] int-expr )]
37274 vector [( [length:] int-expr )]
37276 where gang-arg is one of:
37281 and size-expr may be:
37288 cp_parser_oacc_shape_clause (cp_parser
*parser
, location_t loc
,
37289 omp_clause_code kind
,
37290 const char *str
, tree list
)
37292 const char *id
= "num";
37293 cp_lexer
*lexer
= parser
->lexer
;
37294 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
37296 if (kind
== OMP_CLAUSE_VECTOR
)
37299 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
37301 matching_parens parens
;
37302 parens
.consume_open (parser
);
37306 cp_token
*next
= cp_lexer_peek_token (lexer
);
37309 /* Gang static argument. */
37310 if (kind
== OMP_CLAUSE_GANG
37311 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
37313 cp_lexer_consume_token (lexer
);
37315 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37316 goto cleanup_error
;
37319 if (ops
[idx
] != NULL
)
37321 cp_parser_error (parser
, "too many %<static%> arguments");
37322 goto cleanup_error
;
37325 /* Check for the '*' argument. */
37326 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
37327 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
37328 || cp_lexer_nth_token_is (parser
->lexer
, 2,
37331 cp_lexer_consume_token (lexer
);
37332 ops
[idx
] = integer_minus_one_node
;
37334 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
37336 cp_lexer_consume_token (lexer
);
37342 /* Worker num: argument and vector length: arguments. */
37343 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
37344 && id_equal (next
->u
.value
, id
)
37345 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
37347 cp_lexer_consume_token (lexer
); /* id */
37348 cp_lexer_consume_token (lexer
); /* ':' */
37351 /* Now collect the actual argument. */
37352 if (ops
[idx
] != NULL_TREE
)
37354 cp_parser_error (parser
, "unexpected argument");
37355 goto cleanup_error
;
37358 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
37360 if (expr
== error_mark_node
)
37361 goto cleanup_error
;
37363 mark_exp_read (expr
);
37366 if (kind
== OMP_CLAUSE_GANG
37367 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
37369 cp_lexer_consume_token (lexer
);
37376 if (!parens
.require_close (parser
))
37377 goto cleanup_error
;
37380 check_no_duplicate_clause (list
, kind
, str
, loc
);
37382 c
= build_omp_clause (loc
, kind
);
37385 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
37387 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
37388 OMP_CLAUSE_CHAIN (c
) = list
;
37393 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
37398 tile ( size-expr-list ) */
37401 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
37403 tree c
, expr
= error_mark_node
;
37404 tree tile
= NULL_TREE
;
37406 /* Collapse and tile are mutually exclusive. (The spec doesn't say
37407 so, but the spec authors never considered such a case and have
37408 differing opinions on what it might mean, including 'not
37410 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
37411 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
37414 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37419 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
37422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
37423 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
37424 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
37426 cp_lexer_consume_token (parser
->lexer
);
37427 expr
= integer_zero_node
;
37430 expr
= cp_parser_constant_expression (parser
);
37432 tile
= tree_cons (NULL_TREE
, expr
, tile
);
37434 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
37436 /* Consume the trailing ')'. */
37437 cp_lexer_consume_token (parser
->lexer
);
37439 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
37440 tile
= nreverse (tile
);
37441 OMP_CLAUSE_TILE_LIST (c
) = tile
;
37442 OMP_CLAUSE_CHAIN (c
) = list
;
37447 Parse wait clause or directive parameters. */
37450 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
37452 vec
<tree
, va_gc
> *args
;
37455 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
37457 /*allow_expansion_p=*/true,
37458 /*non_constant_p=*/NULL
);
37460 if (args
== NULL
|| args
->length () == 0)
37464 cp_parser_error (parser
, "expected integer expression list");
37465 release_tree_vector (args
);
37470 args_tree
= build_tree_list_vec (args
);
37472 release_tree_vector (args
);
37474 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
37476 tree targ
= TREE_VALUE (t
);
37478 if (targ
!= error_mark_node
)
37480 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
37481 error ("%<wait%> expression must be integral");
37484 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
37486 targ
= mark_rvalue_use (targ
);
37487 OMP_CLAUSE_DECL (c
) = targ
;
37488 OMP_CLAUSE_CHAIN (c
) = list
;
37498 wait [( int-expr-list )] */
37501 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
37503 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
37505 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37506 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
37509 tree c
= build_omp_clause (location
, OMP_CLAUSE_WAIT
);
37511 OMP_CLAUSE_DECL (c
) = build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
37512 OMP_CLAUSE_CHAIN (c
) = list
;
37520 collapse ( constant-expression ) */
37523 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
37529 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37530 matching_parens parens
;
37531 if (!parens
.require_open (parser
))
37534 num
= cp_parser_constant_expression (parser
);
37536 if (!parens
.require_close (parser
))
37537 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37538 /*or_comma=*/false,
37539 /*consume_paren=*/true);
37541 if (num
== error_mark_node
)
37543 num
= fold_non_dependent_expr (num
);
37544 if (!tree_fits_shwi_p (num
)
37545 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
37546 || (n
= tree_to_shwi (num
)) <= 0
37549 error_at (loc
, "collapse argument needs positive constant integer expression");
37553 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
37554 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
37555 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
37556 OMP_CLAUSE_CHAIN (c
) = list
;
37557 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
37563 default ( none | shared )
37566 default ( private | firstprivate )
37569 default ( none | present ) */
37572 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
37573 location_t location
, bool is_oacc
)
37575 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
37578 matching_parens parens
;
37579 if (!parens
.require_open (parser
))
37581 if (!is_oacc
&& cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
37583 kind
= OMP_CLAUSE_DEFAULT_PRIVATE
;
37584 cp_lexer_consume_token (parser
->lexer
);
37586 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37588 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37589 const char *p
= IDENTIFIER_POINTER (id
);
37594 if (strcmp ("none", p
) != 0)
37596 kind
= OMP_CLAUSE_DEFAULT_NONE
;
37600 if (strcmp ("present", p
) != 0 || !is_oacc
)
37602 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
37606 if (strcmp ("firstprivate", p
) != 0 || is_oacc
)
37608 kind
= OMP_CLAUSE_DEFAULT_FIRSTPRIVATE
;
37612 if (strcmp ("shared", p
) != 0 || is_oacc
)
37614 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
37621 cp_lexer_consume_token (parser
->lexer
);
37627 cp_parser_error (parser
, "expected %<none%> or %<present%>");
37629 cp_parser_error (parser
, "expected %<none%>, %<shared%>, "
37630 "%<private%> or %<firstprivate%>");
37633 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
37634 || !parens
.require_close (parser
))
37635 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37636 /*or_comma=*/false,
37637 /*consume_paren=*/true);
37639 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
37642 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
37643 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
37644 OMP_CLAUSE_CHAIN (c
) = list
;
37645 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
37651 final ( expression ) */
37654 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
37658 matching_parens parens
;
37659 if (!parens
.require_open (parser
))
37662 t
= cp_parser_assignment_expression (parser
);
37664 if (t
== error_mark_node
37665 || !parens
.require_close (parser
))
37666 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37667 /*or_comma=*/false,
37668 /*consume_paren=*/true);
37670 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
37672 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
37673 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
37674 OMP_CLAUSE_CHAIN (c
) = list
;
37683 if ( directive-name-modifier : expression )
37685 directive-name-modifier:
37686 parallel | task | taskloop | target data | target | target update
37687 | target enter data | target exit data
37690 directive-name-modifier:
37691 ... | simd | cancel */
37694 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
37698 enum tree_code if_modifier
= ERROR_MARK
;
37700 matching_parens parens
;
37701 if (!parens
.require_open (parser
))
37704 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37706 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37707 const char *p
= IDENTIFIER_POINTER (id
);
37710 if (strcmp ("cancel", p
) == 0)
37711 if_modifier
= VOID_CST
;
37712 else if (strcmp ("parallel", p
) == 0)
37713 if_modifier
= OMP_PARALLEL
;
37714 else if (strcmp ("simd", p
) == 0)
37715 if_modifier
= OMP_SIMD
;
37716 else if (strcmp ("task", p
) == 0)
37717 if_modifier
= OMP_TASK
;
37718 else if (strcmp ("taskloop", p
) == 0)
37719 if_modifier
= OMP_TASKLOOP
;
37720 else if (strcmp ("target", p
) == 0)
37722 if_modifier
= OMP_TARGET
;
37723 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
37725 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
37726 p
= IDENTIFIER_POINTER (id
);
37727 if (strcmp ("data", p
) == 0)
37728 if_modifier
= OMP_TARGET_DATA
;
37729 else if (strcmp ("update", p
) == 0)
37730 if_modifier
= OMP_TARGET_UPDATE
;
37731 else if (strcmp ("enter", p
) == 0)
37732 if_modifier
= OMP_TARGET_ENTER_DATA
;
37733 else if (strcmp ("exit", p
) == 0)
37734 if_modifier
= OMP_TARGET_EXIT_DATA
;
37735 if (if_modifier
!= OMP_TARGET
)
37740 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
37741 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
37743 if_modifier
= ERROR_MARK
;
37745 if (if_modifier
== OMP_TARGET_ENTER_DATA
37746 || if_modifier
== OMP_TARGET_EXIT_DATA
)
37748 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
37750 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
37751 p
= IDENTIFIER_POINTER (id
);
37752 if (strcmp ("data", p
) == 0)
37758 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
37759 error_at (loc
, "expected %<data%>");
37760 if_modifier
= ERROR_MARK
;
37765 if (if_modifier
!= ERROR_MARK
)
37767 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
37770 cp_lexer_consume_token (parser
->lexer
);
37777 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
37778 error_at (loc
, "expected %<:%>");
37780 if_modifier
= ERROR_MARK
;
37785 t
= cp_parser_assignment_expression (parser
);
37787 if (t
== error_mark_node
37788 || !parens
.require_close (parser
))
37789 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37790 /*or_comma=*/false,
37791 /*consume_paren=*/true);
37793 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37794 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
37796 if (if_modifier
!= ERROR_MARK
37797 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
37799 const char *p
= NULL
;
37800 switch (if_modifier
)
37802 case VOID_CST
: p
= "cancel"; break;
37803 case OMP_PARALLEL
: p
= "parallel"; break;
37804 case OMP_SIMD
: p
= "simd"; break;
37805 case OMP_TASK
: p
= "task"; break;
37806 case OMP_TASKLOOP
: p
= "taskloop"; break;
37807 case OMP_TARGET_DATA
: p
= "target data"; break;
37808 case OMP_TARGET
: p
= "target"; break;
37809 case OMP_TARGET_UPDATE
: p
= "target update"; break;
37810 case OMP_TARGET_ENTER_DATA
: p
= "target enter data"; break;
37811 case OMP_TARGET_EXIT_DATA
: p
= "target exit data"; break;
37812 default: gcc_unreachable ();
37814 error_at (location
, "too many %<if%> clauses with %qs modifier",
37818 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
37821 error_at (location
, "too many %<if%> clauses");
37823 error_at (location
, "too many %<if%> clauses without modifier");
37826 else if (if_modifier
== ERROR_MARK
37827 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
37829 error_at (location
, "if any %<if%> clause has modifier, then all "
37830 "%<if%> clauses have to use modifier");
37835 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
37836 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
37837 OMP_CLAUSE_IF_EXPR (c
) = t
;
37838 OMP_CLAUSE_CHAIN (c
) = list
;
37847 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
37848 tree list
, location_t location
)
37852 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
37855 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
37856 OMP_CLAUSE_CHAIN (c
) = list
;
37864 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
37865 tree list
, location_t location
)
37869 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
37871 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
37872 OMP_CLAUSE_CHAIN (c
) = list
;
37877 num_threads ( expression ) */
37880 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
37881 location_t location
)
37885 matching_parens parens
;
37886 if (!parens
.require_open (parser
))
37889 t
= cp_parser_assignment_expression (parser
);
37891 if (t
== error_mark_node
37892 || !parens
.require_close (parser
))
37893 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37894 /*or_comma=*/false,
37895 /*consume_paren=*/true);
37897 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
37898 "num_threads", location
);
37900 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
37901 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
37902 OMP_CLAUSE_CHAIN (c
) = list
;
37908 num_tasks ( expression )
37911 num_tasks ( strict : expression ) */
37914 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
37915 location_t location
)
37919 matching_parens parens
;
37920 if (!parens
.require_open (parser
))
37923 bool strict
= false;
37924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
37925 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
37927 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37928 if (!strcmp (IDENTIFIER_POINTER (id
), "strict"))
37931 cp_lexer_consume_token (parser
->lexer
);
37932 cp_lexer_consume_token (parser
->lexer
);
37936 t
= cp_parser_assignment_expression (parser
);
37938 if (t
== error_mark_node
37939 || !parens
.require_close (parser
))
37940 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37941 /*or_comma=*/false,
37942 /*consume_paren=*/true);
37944 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
37945 "num_tasks", location
);
37947 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
37948 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
37949 OMP_CLAUSE_NUM_TASKS_STRICT (c
) = strict
;
37950 OMP_CLAUSE_CHAIN (c
) = list
;
37956 grainsize ( expression )
37959 grainsize ( strict : expression ) */
37962 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
37963 location_t location
)
37967 matching_parens parens
;
37968 if (!parens
.require_open (parser
))
37971 bool strict
= false;
37972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
37973 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
37975 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37976 if (!strcmp (IDENTIFIER_POINTER (id
), "strict"))
37979 cp_lexer_consume_token (parser
->lexer
);
37980 cp_lexer_consume_token (parser
->lexer
);
37984 t
= cp_parser_assignment_expression (parser
);
37986 if (t
== error_mark_node
37987 || !parens
.require_close (parser
))
37988 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37989 /*or_comma=*/false,
37990 /*consume_paren=*/true);
37992 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
37993 "grainsize", location
);
37995 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
37996 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
37997 OMP_CLAUSE_GRAINSIZE_STRICT (c
) = strict
;
37998 OMP_CLAUSE_CHAIN (c
) = list
;
38004 priority ( expression ) */
38007 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
38008 location_t location
)
38012 matching_parens parens
;
38013 if (!parens
.require_open (parser
))
38016 t
= cp_parser_assignment_expression (parser
);
38018 if (t
== error_mark_node
38019 || !parens
.require_close (parser
))
38020 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38021 /*or_comma=*/false,
38022 /*consume_paren=*/true);
38024 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
38025 "priority", location
);
38027 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
38028 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
38029 OMP_CLAUSE_CHAIN (c
) = list
;
38035 hint ( expression ) */
38038 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
38042 matching_parens parens
;
38043 if (!parens
.require_open (parser
))
38046 t
= cp_parser_assignment_expression (parser
);
38048 if (t
!= error_mark_node
)
38050 t
= fold_non_dependent_expr (t
);
38051 if (!value_dependent_expression_p (t
)
38052 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
38053 || !tree_fits_shwi_p (t
)
38054 || tree_int_cst_sgn (t
) == -1))
38055 error_at (location
, "expected constant integer expression with "
38056 "valid sync-hint value");
38058 if (t
== error_mark_node
38059 || !parens
.require_close (parser
))
38060 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38061 /*or_comma=*/false,
38062 /*consume_paren=*/true);
38063 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
38065 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
38066 OMP_CLAUSE_HINT_EXPR (c
) = t
;
38067 OMP_CLAUSE_CHAIN (c
) = list
;
38073 filter ( integer-expression ) */
38076 cp_parser_omp_clause_filter (cp_parser
*parser
, tree list
, location_t location
)
38080 matching_parens parens
;
38081 if (!parens
.require_open (parser
))
38084 t
= cp_parser_assignment_expression (parser
);
38086 if (t
== error_mark_node
38087 || !parens
.require_close (parser
))
38088 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38089 /*or_comma=*/false,
38090 /*consume_paren=*/true);
38091 check_no_duplicate_clause (list
, OMP_CLAUSE_FILTER
, "filter", location
);
38093 c
= build_omp_clause (location
, OMP_CLAUSE_FILTER
);
38094 OMP_CLAUSE_FILTER_EXPR (c
) = t
;
38095 OMP_CLAUSE_CHAIN (c
) = list
;
38101 defaultmap ( tofrom : scalar )
38104 defaultmap ( implicit-behavior [ : variable-category ] ) */
38107 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
38108 location_t location
)
38112 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
38113 enum omp_clause_defaultmap_kind category
38114 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
38116 matching_parens parens
;
38117 if (!parens
.require_open (parser
))
38120 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
38122 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38125 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
38126 "%<tofrom%>, %<firstprivate%>, %<none%> "
38132 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38133 p
= IDENTIFIER_POINTER (id
);
38139 if (strcmp ("alloc", p
) == 0)
38140 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
38142 goto invalid_behavior
;
38146 if (strcmp ("default", p
) == 0)
38147 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
38149 goto invalid_behavior
;
38153 if (strcmp ("firstprivate", p
) == 0)
38154 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
38155 else if (strcmp ("from", p
) == 0)
38156 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
38158 goto invalid_behavior
;
38162 if (strcmp ("none", p
) == 0)
38163 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
38165 goto invalid_behavior
;
38169 if (strcmp ("tofrom", p
) == 0)
38170 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
38171 else if (strcmp ("to", p
) == 0)
38172 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
38174 goto invalid_behavior
;
38178 goto invalid_behavior
;
38180 cp_lexer_consume_token (parser
->lexer
);
38182 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
38184 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
38187 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38190 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
38194 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38195 p
= IDENTIFIER_POINTER (id
);
38200 if (strcmp ("aggregate", p
) == 0)
38201 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
38203 goto invalid_category
;
38207 if (strcmp ("pointer", p
) == 0)
38208 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
38210 goto invalid_category
;
38214 if (strcmp ("scalar", p
) == 0)
38215 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
38217 goto invalid_category
;
38221 goto invalid_category
;
38224 cp_lexer_consume_token (parser
->lexer
);
38226 if (!parens
.require_close (parser
))
38229 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
38230 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
38231 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
38232 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
38233 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
38234 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
38236 enum omp_clause_defaultmap_kind cat
= category
;
38237 location_t loc
= OMP_CLAUSE_LOCATION (c
);
38238 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
38239 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
38243 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
38246 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
38249 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
38252 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
38256 gcc_unreachable ();
38259 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
38262 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
38267 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
38268 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
38269 OMP_CLAUSE_CHAIN (c
) = list
;
38273 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38274 /*or_comma=*/false,
38275 /*consume_paren=*/true);
38280 order ( concurrent )
38283 order ( order-modifier : concurrent )
38290 cp_parser_omp_clause_order (cp_parser
*parser
, tree list
, location_t location
)
38294 bool unconstrained
= false;
38295 bool reproducible
= false;
38297 matching_parens parens
;
38298 if (!parens
.require_open (parser
))
38301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
38302 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
38304 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38305 p
= IDENTIFIER_POINTER (id
);
38306 if (strcmp (p
, "unconstrained") == 0)
38307 unconstrained
= true;
38308 else if (strcmp (p
, "reproducible") == 0)
38309 reproducible
= true;
38312 cp_parser_error (parser
, "expected %<reproducible%> or "
38313 "%<unconstrained%>");
38316 cp_lexer_consume_token (parser
->lexer
);
38317 cp_lexer_consume_token (parser
->lexer
);
38319 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38321 cp_parser_error (parser
, "expected %<concurrent%>");
38326 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38327 p
= IDENTIFIER_POINTER (id
);
38329 if (strcmp (p
, "concurrent") != 0)
38331 cp_parser_error (parser
, "expected %<concurrent%>");
38334 cp_lexer_consume_token (parser
->lexer
);
38335 if (!parens
.require_close (parser
))
38338 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDER
, "order", location
);
38339 c
= build_omp_clause (location
, OMP_CLAUSE_ORDER
);
38340 OMP_CLAUSE_ORDER_UNCONSTRAINED (c
) = unconstrained
;
38341 OMP_CLAUSE_ORDER_REPRODUCIBLE (c
) = reproducible
;
38342 OMP_CLAUSE_CHAIN (c
) = list
;
38346 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38347 /*or_comma=*/false,
38348 /*consume_paren=*/true);
38353 bind ( teams | parallel | thread ) */
38356 cp_parser_omp_clause_bind (cp_parser
*parser
, tree list
,
38357 location_t location
)
38361 enum omp_clause_bind_kind kind
= OMP_CLAUSE_BIND_THREAD
;
38363 matching_parens parens
;
38364 if (!parens
.require_open (parser
))
38367 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38370 cp_parser_error (parser
,
38371 "expected %<teams%>, %<parallel%> or %<thread%>");
38376 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38377 p
= IDENTIFIER_POINTER (id
);
38379 if (strcmp (p
, "teams") == 0)
38380 kind
= OMP_CLAUSE_BIND_TEAMS
;
38381 else if (strcmp (p
, "parallel") == 0)
38382 kind
= OMP_CLAUSE_BIND_PARALLEL
;
38383 else if (strcmp (p
, "thread") != 0)
38385 cp_lexer_consume_token (parser
->lexer
);
38386 if (!parens
.require_close (parser
))
38389 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
38390 c
= build_omp_clause (location
, OMP_CLAUSE_BIND
);
38391 OMP_CLAUSE_BIND_KIND (c
) = kind
;
38392 OMP_CLAUSE_CHAIN (c
) = list
;
38396 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38397 /*or_comma=*/false,
38398 /*consume_paren=*/true);
38406 ordered ( constant-expression ) */
38409 cp_parser_omp_clause_ordered (cp_parser
*parser
,
38410 tree list
, location_t location
)
38412 tree c
, num
= NULL_TREE
;
38415 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
38416 "ordered", location
);
38418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38420 matching_parens parens
;
38421 parens
.consume_open (parser
);
38423 num
= cp_parser_constant_expression (parser
);
38425 if (!parens
.require_close (parser
))
38426 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38427 /*or_comma=*/false,
38428 /*consume_paren=*/true);
38430 if (num
== error_mark_node
)
38432 num
= fold_non_dependent_expr (num
);
38433 if (!tree_fits_shwi_p (num
)
38434 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
38435 || (n
= tree_to_shwi (num
)) <= 0
38438 error_at (location
,
38439 "ordered argument needs positive constant integer "
38445 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
38446 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
38447 OMP_CLAUSE_CHAIN (c
) = list
;
38452 reduction ( reduction-operator : variable-list )
38454 reduction-operator:
38455 One of: + * - & ^ | && ||
38459 reduction-operator:
38460 One of: + * - & ^ | && || min max
38464 reduction-operator:
38465 One of: + * - & ^ | && ||
38469 reduction ( reduction-modifier, reduction-operator : variable-list )
38470 in_reduction ( reduction-operator : variable-list )
38471 task_reduction ( reduction-operator : variable-list ) */
38474 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
38475 bool is_omp
, tree list
)
38477 enum tree_code code
= ERROR_MARK
;
38478 tree nlist
, c
, id
= NULL_TREE
;
38480 bool inscan
= false;
38482 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38485 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
38487 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
38488 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
38490 cp_lexer_consume_token (parser
->lexer
);
38491 cp_lexer_consume_token (parser
->lexer
);
38493 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
38494 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
38496 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38497 const char *p
= IDENTIFIER_POINTER (id
);
38498 if (strcmp (p
, "task") == 0)
38500 else if (strcmp (p
, "inscan") == 0)
38502 if (task
|| inscan
)
38504 cp_lexer_consume_token (parser
->lexer
);
38505 cp_lexer_consume_token (parser
->lexer
);
38510 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
38512 case CPP_PLUS
: code
= PLUS_EXPR
; break;
38513 case CPP_MULT
: code
= MULT_EXPR
; break;
38514 case CPP_MINUS
: code
= MINUS_EXPR
; break;
38515 case CPP_AND
: code
= BIT_AND_EXPR
; break;
38516 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
38517 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
38518 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
38519 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
38523 if (code
!= ERROR_MARK
)
38524 cp_lexer_consume_token (parser
->lexer
);
38527 bool saved_colon_corrects_to_scope_p
;
38528 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38529 parser
->colon_corrects_to_scope_p
= false;
38530 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
38531 /*check_dependency_p=*/true,
38532 /*template_p=*/NULL
,
38533 /*declarator_p=*/false,
38534 /*optional_p=*/false);
38535 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
38536 if (identifier_p (id
))
38538 const char *p
= IDENTIFIER_POINTER (id
);
38540 if (strcmp (p
, "min") == 0)
38542 else if (strcmp (p
, "max") == 0)
38544 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
38546 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
38548 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
38550 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
38551 code
= BIT_AND_EXPR
;
38552 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
38553 code
= BIT_IOR_EXPR
;
38554 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
38555 code
= BIT_XOR_EXPR
;
38556 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
38557 code
= TRUTH_ANDIF_EXPR
;
38558 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
38559 code
= TRUTH_ORIF_EXPR
;
38560 id
= omp_reduction_id (code
, id
, NULL_TREE
);
38561 tree scope
= parser
->scope
;
38563 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
38564 parser
->scope
= NULL_TREE
;
38565 parser
->qualifying_scope
= NULL_TREE
;
38566 parser
->object_scope
= NULL_TREE
;
38570 error ("invalid reduction-identifier");
38572 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38573 /*or_comma=*/false,
38574 /*consume_paren=*/true);
38579 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
38582 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
38584 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
38586 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
38588 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
38590 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
38591 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
38598 schedule ( schedule-kind )
38599 schedule ( schedule-kind , expression )
38602 static | dynamic | guided | runtime | auto
38605 schedule ( schedule-modifier : schedule-kind )
38606 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
38614 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
38617 int modifiers
= 0, nmodifiers
= 0;
38619 matching_parens parens
;
38620 if (!parens
.require_open (parser
))
38623 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
38625 location_t comma
= UNKNOWN_LOCATION
;
38626 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38628 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38629 const char *p
= IDENTIFIER_POINTER (id
);
38630 if (strcmp ("simd", p
) == 0)
38631 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
38632 else if (strcmp ("monotonic", p
) == 0)
38633 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
38634 else if (strcmp ("nonmonotonic", p
) == 0)
38635 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
38638 comma
= UNKNOWN_LOCATION
;
38639 cp_lexer_consume_token (parser
->lexer
);
38640 if (nmodifiers
++ == 0
38641 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38643 comma
= cp_lexer_peek_token (parser
->lexer
)->location
;
38644 cp_lexer_consume_token (parser
->lexer
);
38648 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
38652 if (comma
!= UNKNOWN_LOCATION
)
38653 error_at (comma
, "expected %<:%>");
38655 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38657 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38658 const char *p
= IDENTIFIER_POINTER (id
);
38663 if (strcmp ("dynamic", p
) != 0)
38665 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
38669 if (strcmp ("guided", p
) != 0)
38671 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
38675 if (strcmp ("runtime", p
) != 0)
38677 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
38684 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
38685 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
38686 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
38687 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
38690 cp_lexer_consume_token (parser
->lexer
);
38692 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
38693 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
38694 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
38695 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
38697 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
38702 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38705 cp_lexer_consume_token (parser
->lexer
);
38707 token
= cp_lexer_peek_token (parser
->lexer
);
38708 t
= cp_parser_assignment_expression (parser
);
38710 if (t
== error_mark_node
)
38712 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
38713 error_at (token
->location
, "schedule %<runtime%> does not take "
38714 "a %<chunk_size%> parameter");
38715 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
38716 error_at (token
->location
, "schedule %<auto%> does not take "
38717 "a %<chunk_size%> parameter");
38719 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
38721 if (!parens
.require_close (parser
))
38724 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
38727 OMP_CLAUSE_SCHEDULE_KIND (c
)
38728 = (enum omp_clause_schedule_kind
)
38729 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
38731 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
38732 OMP_CLAUSE_CHAIN (c
) = list
;
38736 cp_parser_error (parser
, "invalid schedule kind");
38738 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38739 /*or_comma=*/false,
38740 /*consume_paren=*/true);
38748 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
38749 tree list
, location_t location
)
38753 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
38755 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
38756 OMP_CLAUSE_CHAIN (c
) = list
;
38765 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
38766 tree list
, location_t location
)
38768 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
38769 tree c
= build_omp_clause (location
, code
);
38770 OMP_CLAUSE_CHAIN (c
) = list
;
38781 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
38782 enum omp_clause_code code
,
38783 tree list
, location_t location
)
38785 tree c
= build_omp_clause (location
, code
);
38786 OMP_CLAUSE_CHAIN (c
) = list
;
38794 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
38795 tree list
, location_t location
)
38797 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
38798 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
38799 OMP_CLAUSE_CHAIN (c
) = list
;
38808 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
38809 enum omp_clause_code code
,
38810 tree list
, location_t location
)
38812 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
38813 tree c
= build_omp_clause (location
, code
);
38814 OMP_CLAUSE_CHAIN (c
) = list
;
38819 num_teams ( expression )
38822 num_teams ( expression : expression ) */
38825 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
38826 location_t location
)
38828 tree upper
, lower
= NULL_TREE
, c
;
38830 matching_parens parens
;
38831 if (!parens
.require_open (parser
))
38834 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38835 parser
->colon_corrects_to_scope_p
= false;
38836 upper
= cp_parser_assignment_expression (parser
);
38837 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
38839 if (upper
!= error_mark_node
38840 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
38843 cp_lexer_consume_token (parser
->lexer
);
38844 upper
= cp_parser_assignment_expression (parser
);
38847 if (upper
== error_mark_node
38848 || !parens
.require_close (parser
))
38849 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38850 /*or_comma=*/false,
38851 /*consume_paren=*/true);
38853 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
38854 "num_teams", location
);
38856 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
38857 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c
) = upper
;
38858 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c
) = lower
;
38859 OMP_CLAUSE_CHAIN (c
) = list
;
38865 thread_limit ( expression ) */
38868 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
38869 location_t location
)
38873 matching_parens parens
;
38874 if (!parens
.require_open (parser
))
38877 t
= cp_parser_assignment_expression (parser
);
38879 if (t
== error_mark_node
38880 || !parens
.require_close (parser
))
38881 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38882 /*or_comma=*/false,
38883 /*consume_paren=*/true);
38885 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
38886 "thread_limit", location
);
38888 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
38889 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
38890 OMP_CLAUSE_CHAIN (c
) = list
;
38896 aligned ( variable-list )
38897 aligned ( variable-list : constant-expression ) */
38900 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
38902 tree nlist
, c
, alignment
= NULL_TREE
;
38905 matching_parens parens
;
38906 if (!parens
.require_open (parser
))
38909 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
38914 alignment
= cp_parser_constant_expression (parser
);
38916 if (!parens
.require_close (parser
))
38917 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38918 /*or_comma=*/false,
38919 /*consume_paren=*/true);
38921 if (alignment
== error_mark_node
)
38922 alignment
= NULL_TREE
;
38925 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
38926 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
38932 allocate ( variable-list )
38933 allocate ( expression : variable-list )
38936 allocate ( allocator-modifier : variable-list )
38937 allocate ( allocator-modifier , allocator-modifier : variable-list )
38939 allocator-modifier:
38940 allocator ( expression )
38941 align ( expression ) */
38944 cp_parser_omp_clause_allocate (cp_parser
*parser
, tree list
)
38946 tree nlist
, c
, allocator
= NULL_TREE
, align
= NULL_TREE
;
38947 bool colon
, has_modifiers
= false;
38949 matching_parens parens
;
38950 if (!parens
.require_open (parser
))
38953 cp_parser_parse_tentatively (parser
);
38954 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38955 parser
->colon_corrects_to_scope_p
= false;
38956 for (int mod
= 0; mod
< 2; mod
++)
38957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
38958 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
38960 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38961 const char *p
= IDENTIFIER_POINTER (id
);
38962 if (strcmp (p
, "allocator") != 0 && strcmp (p
, "align") != 0)
38964 cp_lexer_consume_token (parser
->lexer
);
38965 matching_parens parens2
;
38966 if (!parens2
.require_open (parser
))
38968 if (strcmp (p
, "allocator") == 0)
38970 if (allocator
!= NULL_TREE
)
38972 allocator
= cp_parser_assignment_expression (parser
);
38976 if (align
!= NULL_TREE
)
38978 align
= cp_parser_assignment_expression (parser
);
38980 if (!parens2
.require_close (parser
))
38982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
38984 has_modifiers
= true;
38987 if (mod
!= 0 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
38989 cp_lexer_consume_token (parser
->lexer
);
38993 if (!has_modifiers
)
38995 cp_parser_abort_tentative_parse (parser
);
38997 allocator
= NULL_TREE
;
38998 cp_parser_parse_tentatively (parser
);
38999 allocator
= cp_parser_assignment_expression (parser
);
39001 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
39004 cp_parser_parse_definitely (parser
);
39005 cp_lexer_consume_token (parser
->lexer
);
39006 if (allocator
== error_mark_node
)
39007 allocator
= NULL_TREE
;
39008 if (align
== error_mark_node
)
39013 cp_parser_abort_tentative_parse (parser
);
39014 allocator
= NULL_TREE
;
39018 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALLOCATE
, list
,
39021 if (allocator
|| align
)
39022 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
39024 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c
) = allocator
;
39025 OMP_CLAUSE_ALLOCATE_ALIGN (c
) = align
;
39032 lastprivate ( variable-list )
39035 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
39038 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
39040 bool conditional
= false;
39042 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
39046 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
39048 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39049 const char *p
= IDENTIFIER_POINTER (id
);
39051 if (strcmp ("conditional", p
) == 0)
39053 conditional
= true;
39054 cp_lexer_consume_token (parser
->lexer
);
39055 cp_lexer_consume_token (parser
->lexer
);
39059 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
39063 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
39064 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
39069 linear ( variable-list )
39070 linear ( variable-list : expression )
39073 linear ( modifier ( variable-list ) )
39074 linear ( modifier ( variable-list ) : expression )
39082 linear ( variable-list : modifiers-list )
39088 step ( expression ) */
39091 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
39094 tree nlist
, c
, step
= integer_one_node
;
39096 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
39097 bool old_linear_modifier
= false;
39099 matching_parens parens
;
39100 if (!parens
.require_open (parser
))
39103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39105 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39106 const char *p
= IDENTIFIER_POINTER (id
);
39108 if (strcmp ("ref", p
) == 0)
39109 kind
= OMP_CLAUSE_LINEAR_REF
;
39110 else if (strcmp ("val", p
) == 0)
39111 kind
= OMP_CLAUSE_LINEAR_VAL
;
39112 else if (strcmp ("uval", p
) == 0)
39113 kind
= OMP_CLAUSE_LINEAR_UVAL
;
39114 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
39116 cp_lexer_consume_token (parser
->lexer
);
39117 old_linear_modifier
= true;
39120 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
39123 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
39124 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
39128 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
39129 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
39131 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
39132 else if (!parens
.require_close (parser
))
39133 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39134 /*or_comma=*/false,
39135 /*consume_paren=*/true);
39140 bool has_modifiers
= false;
39141 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
39142 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39144 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39145 const char *p
= IDENTIFIER_POINTER (id
);
39147 if (strcmp ("ref", p
) == 0
39148 || strcmp ("val", p
) == 0
39149 || strcmp ("uval", p
) == 0)
39151 else if (strcmp ("step", p
) == 0
39152 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
39154 pos
= cp_parser_skip_balanced_tokens (parser
, 2);
39159 && (cp_lexer_nth_token_is (parser
->lexer
, pos
, CPP_COMMA
)
39160 || cp_lexer_nth_token_is (parser
->lexer
, pos
,
39162 has_modifiers
= true;
39168 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39170 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39171 const char *p
= IDENTIFIER_POINTER (id
);
39172 enum omp_clause_linear_kind nkind
= OMP_CLAUSE_LINEAR_DEFAULT
;
39173 if (strcmp ("ref", p
) == 0)
39174 nkind
= OMP_CLAUSE_LINEAR_REF
;
39175 else if (strcmp ("val", p
) == 0)
39176 nkind
= OMP_CLAUSE_LINEAR_VAL
;
39177 else if (strcmp ("uval", p
) == 0)
39178 nkind
= OMP_CLAUSE_LINEAR_UVAL
;
39179 if (nkind
!= OMP_CLAUSE_LINEAR_DEFAULT
)
39181 if (kind
!= OMP_CLAUSE_LINEAR_DEFAULT
)
39182 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39183 "multiple linear modifiers");
39185 cp_lexer_consume_token (parser
->lexer
);
39187 else if (strcmp ("step", p
) == 0)
39189 location_t step_loc
39190 = cp_lexer_peek_token (parser
->lexer
)->location
;
39191 cp_lexer_consume_token (parser
->lexer
);
39192 matching_parens parens2
;
39193 if (parens2
.require_open (parser
))
39196 error_at (step_loc
, "multiple %<step%> modifiers");
39198 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
39199 && cp_lexer_nth_token_is (parser
->lexer
, 2,
39203 = cp_lexer_peek_token (parser
->lexer
);
39204 location_t tok_loc
= token
->location
;
39205 cp_parser_parse_tentatively (parser
);
39206 step
= cp_parser_id_expression (parser
, false, true,
39207 NULL
, false, false);
39208 if (step
!= error_mark_node
)
39209 step
= cp_parser_lookup_name_simple (parser
, step
,
39211 if (step
== error_mark_node
)
39214 cp_parser_abort_tentative_parse (parser
);
39216 else if (!cp_parser_parse_definitely (parser
))
39220 step
= cp_parser_assignment_expression (parser
);
39221 if (!parens2
.require_close (parser
))
39222 cp_parser_skip_to_closing_parenthesis (parser
, true,
39230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39232 cp_lexer_consume_token (parser
->lexer
);
39238 step
= integer_one_node
;
39240 else if (declare_simd
39241 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
39242 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
39244 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
39245 cp_parser_parse_tentatively (parser
);
39246 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
39247 /*check_dependency_p=*/true,
39248 /*template_p=*/NULL
,
39249 /*declarator_p=*/false,
39250 /*optional_p=*/false);
39251 if (step
!= error_mark_node
)
39252 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
39253 if (step
== error_mark_node
)
39256 cp_parser_abort_tentative_parse (parser
);
39258 else if (!cp_parser_parse_definitely (parser
))
39262 step
= cp_parser_assignment_expression (parser
);
39264 if (!parens
.require_close (parser
))
39265 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39266 /*or_comma=*/false,
39267 /*consume_paren=*/true);
39269 if (step
== error_mark_node
)
39273 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
39275 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
39276 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
39277 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c
) = old_linear_modifier
;
39284 safelen ( constant-expression ) */
39287 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
39288 location_t location
)
39292 matching_parens parens
;
39293 if (!parens
.require_open (parser
))
39296 t
= cp_parser_constant_expression (parser
);
39298 if (t
== error_mark_node
39299 || !parens
.require_close (parser
))
39300 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39301 /*or_comma=*/false,
39302 /*consume_paren=*/true);
39304 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
39306 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
39307 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
39308 OMP_CLAUSE_CHAIN (c
) = list
;
39314 simdlen ( constant-expression ) */
39317 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
39318 location_t location
)
39322 matching_parens parens
;
39323 if (!parens
.require_open (parser
))
39326 t
= cp_parser_constant_expression (parser
);
39328 if (t
== error_mark_node
39329 || !parens
.require_close (parser
))
39330 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39331 /*or_comma=*/false,
39332 /*consume_paren=*/true);
39334 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
39336 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
39337 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
39338 OMP_CLAUSE_CHAIN (c
) = list
;
39345 identifier [+/- integer]
39346 vec , identifier [+/- integer]
39350 cp_parser_omp_clause_doacross_sink (cp_parser
*parser
, location_t clause_loc
,
39351 tree list
, bool depend_p
)
39355 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
39357 cp_parser_error (parser
, "expected identifier");
39363 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39364 if (strcmp (IDENTIFIER_POINTER (id
), "omp_cur_iteration") == 0
39365 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_MINUS
)
39366 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NUMBER
)
39367 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_PAREN
))
39369 tree val
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
39370 if (integer_onep (val
))
39372 cp_lexer_consume_token (parser
->lexer
);
39373 cp_lexer_consume_token (parser
->lexer
);
39374 cp_lexer_consume_token (parser
->lexer
);
39375 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DOACROSS
);
39376 OMP_CLAUSE_DOACROSS_KIND (u
) = OMP_CLAUSE_DOACROSS_SINK
;
39377 OMP_CLAUSE_CHAIN (u
) = list
;
39383 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39385 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39386 tree t
, identifier
= cp_parser_identifier (parser
);
39387 tree addend
= NULL
;
39389 if (identifier
== error_mark_node
)
39390 t
= error_mark_node
;
39393 t
= cp_parser_lookup_name_simple
39394 (parser
, identifier
,
39395 cp_lexer_peek_token (parser
->lexer
)->location
);
39396 if (t
== error_mark_node
)
39397 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
39402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
39404 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
39406 addend
= integer_zero_node
;
39407 goto add_to_vector
;
39409 cp_lexer_consume_token (parser
->lexer
);
39411 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
39413 cp_parser_error (parser
, "expected integer");
39417 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39418 if (TREE_CODE (addend
) != INTEGER_CST
)
39420 cp_parser_error (parser
, "expected integer");
39423 cp_lexer_consume_token (parser
->lexer
);
39426 if (t
!= error_mark_node
)
39428 vec
= tree_cons (addend
, t
, vec
);
39430 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec
) = 1;
39433 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
39434 || !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
39437 cp_lexer_consume_token (parser
->lexer
);
39442 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DOACROSS
);
39443 OMP_CLAUSE_DOACROSS_KIND (u
) = OMP_CLAUSE_DOACROSS_SINK
;
39444 OMP_CLAUSE_DOACROSS_DEPEND (u
) = depend_p
;
39445 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
39446 OMP_CLAUSE_CHAIN (u
) = list
;
39453 detach ( event-handle ) */
39456 cp_parser_omp_clause_detach (cp_parser
*parser
, tree list
)
39458 matching_parens parens
;
39460 if (!parens
.require_open (parser
))
39466 token
= cp_lexer_peek_token (parser
->lexer
);
39467 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39468 /*check_dependency_p=*/true,
39469 /*template_p=*/NULL
,
39470 /*declarator_p=*/false,
39471 /*optional_p=*/false);
39472 if (name
== error_mark_node
)
39473 decl
= error_mark_node
;
39476 if (identifier_p (name
))
39477 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
39480 if (decl
== error_mark_node
)
39481 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
39485 if (decl
== error_mark_node
39486 || !parens
.require_close (parser
))
39487 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39488 /*or_comma=*/false,
39489 /*consume_paren=*/true);
39491 tree u
= build_omp_clause (token
->location
, OMP_CLAUSE_DETACH
);
39492 OMP_CLAUSE_DECL (u
) = decl
;
39493 OMP_CLAUSE_CHAIN (u
) = list
;
39499 iterators ( iterators-definition )
39501 iterators-definition:
39503 iterator-specifier , iterators-definition
39505 iterator-specifier:
39506 identifier = range-specification
39507 iterator-type identifier = range-specification
39509 range-specification:
39511 begin : end : step */
39514 cp_parser_omp_iterators (cp_parser
*parser
)
39516 tree ret
= NULL_TREE
, *last
= &ret
;
39517 cp_lexer_consume_token (parser
->lexer
);
39519 matching_parens parens
;
39520 if (!parens
.require_open (parser
))
39521 return error_mark_node
;
39523 bool saved_colon_corrects_to_scope_p
39524 = parser
->colon_corrects_to_scope_p
;
39525 bool saved_colon_doesnt_start_class_def_p
39526 = parser
->colon_doesnt_start_class_def_p
;
39531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
39532 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
39533 iter_type
= integer_type_node
;
39536 const char *saved_message
39537 = parser
->type_definition_forbidden_message
;
39538 parser
->type_definition_forbidden_message
39539 = G_("types may not be defined in iterator type");
39541 iter_type
= cp_parser_type_id (parser
);
39543 parser
->type_definition_forbidden_message
= saved_message
;
39546 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39547 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
39549 cp_parser_error (parser
, "expected identifier");
39553 tree id
= cp_parser_identifier (parser
);
39554 if (id
== error_mark_node
)
39557 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
39560 parser
->colon_corrects_to_scope_p
= false;
39561 parser
->colon_doesnt_start_class_def_p
= true;
39562 tree begin
= cp_parser_assignment_expression (parser
);
39564 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39567 tree end
= cp_parser_assignment_expression (parser
);
39569 tree step
= integer_one_node
;
39570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
39572 cp_lexer_consume_token (parser
->lexer
);
39573 step
= cp_parser_assignment_expression (parser
);
39576 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
39577 DECL_ARTIFICIAL (iter_var
) = 1;
39578 DECL_CONTEXT (iter_var
) = current_function_decl
;
39579 pushdecl (iter_var
);
39581 *last
= make_tree_vec (6);
39582 TREE_VEC_ELT (*last
, 0) = iter_var
;
39583 TREE_VEC_ELT (*last
, 1) = begin
;
39584 TREE_VEC_ELT (*last
, 2) = end
;
39585 TREE_VEC_ELT (*last
, 3) = step
;
39586 last
= &TREE_CHAIN (*last
);
39588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39590 cp_lexer_consume_token (parser
->lexer
);
39597 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39598 parser
->colon_doesnt_start_class_def_p
39599 = saved_colon_doesnt_start_class_def_p
;
39601 if (!parens
.require_close (parser
))
39602 cp_parser_skip_to_closing_parenthesis (parser
,
39603 /*recovering=*/true,
39604 /*or_comma=*/false,
39605 /*consume_paren=*/true);
39607 return ret
? ret
: error_mark_node
;
39611 affinity ( [aff-modifier :] variable-list )
39613 iterator ( iterators-definition ) */
39616 cp_parser_omp_clause_affinity (cp_parser
*parser
, tree list
)
39618 tree nlist
, c
, iterators
= NULL_TREE
;
39620 matching_parens parens
;
39621 if (!parens
.require_open (parser
))
39624 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39626 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39627 const char *p
= IDENTIFIER_POINTER (id
);
39628 bool parse_iter
= ((strcmp ("iterator", p
) == 0)
39629 && (cp_lexer_nth_token_is (parser
->lexer
, 2,
39633 size_t n
= cp_parser_skip_balanced_tokens (parser
, 2);
39634 parse_iter
= cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
);
39638 begin_scope (sk_omp
, NULL
);
39639 iterators
= cp_parser_omp_iterators (parser
);
39640 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39643 poplevel (0, 1, 0);
39644 cp_parser_skip_to_closing_parenthesis (parser
,
39645 /*recovering=*/true,
39646 /*or_comma=*/false,
39647 /*consume_paren=*/true);
39652 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_AFFINITY
,
39656 tree block
= poplevel (1, 1, 0);
39657 if (iterators
!= error_mark_node
)
39659 TREE_VEC_ELT (iterators
, 5) = block
;
39660 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
39661 OMP_CLAUSE_DECL (c
) = build_tree_list (iterators
,
39662 OMP_CLAUSE_DECL (c
));
39669 depend ( depend-kind : variable-list )
39677 depend ( sink : vec )
39680 depend ( depend-modifier , depend-kind: variable-list )
39683 in | out | inout | mutexinoutset | depobj
39686 iterator ( iterators-definition ) */
39689 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
39691 tree nlist
, c
, iterators
= NULL_TREE
;
39692 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
39693 enum omp_clause_doacross_kind dkind
= OMP_CLAUSE_DOACROSS_LAST
;
39695 matching_parens parens
;
39696 if (!parens
.require_open (parser
))
39701 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
39704 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39705 const char *p
= IDENTIFIER_POINTER (id
);
39707 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
39709 begin_scope (sk_omp
, NULL
);
39710 iterators
= cp_parser_omp_iterators (parser
);
39711 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
39714 if (strcmp ("in", p
) == 0)
39715 kind
= OMP_CLAUSE_DEPEND_IN
;
39716 else if (strcmp ("inout", p
) == 0)
39717 kind
= OMP_CLAUSE_DEPEND_INOUT
;
39718 else if (strcmp ("inoutset", p
) == 0)
39719 kind
= OMP_CLAUSE_DEPEND_INOUTSET
;
39720 else if (strcmp ("mutexinoutset", p
) == 0)
39721 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
39722 else if (strcmp ("out", p
) == 0)
39723 kind
= OMP_CLAUSE_DEPEND_OUT
;
39724 else if (strcmp ("depobj", p
) == 0)
39725 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
39726 else if (strcmp ("sink", p
) == 0)
39727 dkind
= OMP_CLAUSE_DOACROSS_SINK
;
39728 else if (strcmp ("source", p
) == 0)
39729 dkind
= OMP_CLAUSE_DOACROSS_SOURCE
;
39736 cp_lexer_consume_token (parser
->lexer
);
39739 && (dkind
== OMP_CLAUSE_DOACROSS_SOURCE
39740 || dkind
== OMP_CLAUSE_DOACROSS_SINK
))
39742 poplevel (0, 1, 0);
39743 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
39744 dkind
== OMP_CLAUSE_DOACROSS_SOURCE
? "source" : "sink");
39745 iterators
= NULL_TREE
;
39748 if (dkind
== OMP_CLAUSE_DOACROSS_SOURCE
)
39750 c
= build_omp_clause (loc
, OMP_CLAUSE_DOACROSS
);
39751 OMP_CLAUSE_DOACROSS_KIND (c
) = dkind
;
39752 OMP_CLAUSE_DOACROSS_DEPEND (c
) = 1;
39753 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
39754 OMP_CLAUSE_CHAIN (c
) = list
;
39755 if (!parens
.require_close (parser
))
39756 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39757 /*or_comma=*/false,
39758 /*consume_paren=*/true);
39762 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39765 if (dkind
== OMP_CLAUSE_DOACROSS_SINK
)
39767 nlist
= cp_parser_omp_clause_doacross_sink (parser
, loc
, list
, true);
39768 if (!parens
.require_close (parser
))
39769 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39770 /*or_comma=*/false,
39771 /*consume_paren=*/true);
39775 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
39780 tree block
= poplevel (1, 1, 0);
39781 if (iterators
== error_mark_node
)
39782 iterators
= NULL_TREE
;
39784 TREE_VEC_ELT (iterators
, 5) = block
;
39787 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
39789 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
39791 OMP_CLAUSE_DECL (c
)
39792 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
39798 cp_parser_error (parser
, "invalid depend kind");
39801 poplevel (0, 1, 0);
39802 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39803 /*or_comma=*/false,
39804 /*consume_paren=*/true);
39809 doacross ( source : )
39810 doacross ( source : omp_cur_iteration )
39812 doacross ( sink : vec )
39813 doacross ( sink : omp_cur_iteration - logical_iteration ) */
39816 cp_parser_omp_clause_doacross (cp_parser
*parser
, tree list
, location_t loc
)
39819 enum omp_clause_doacross_kind kind
= OMP_CLAUSE_DOACROSS_LAST
;
39821 matching_parens parens
;
39822 if (!parens
.require_open (parser
))
39825 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
39828 cp_parser_error (parser
, "invalid doacross kind");
39830 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39831 /*or_comma=*/false,
39832 /*consume_paren=*/true);
39836 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39837 const char *p
= IDENTIFIER_POINTER (id
);
39839 if (strcmp ("sink", p
) == 0)
39840 kind
= OMP_CLAUSE_DOACROSS_SINK
;
39841 else if (strcmp ("source", p
) == 0)
39842 kind
= OMP_CLAUSE_DOACROSS_SOURCE
;
39846 cp_lexer_consume_token (parser
->lexer
);
39848 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39851 if (kind
== OMP_CLAUSE_DOACROSS_SOURCE
)
39853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39855 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39856 p
= IDENTIFIER_POINTER (id
);
39857 if (strcmp (p
, "omp_cur_iteration") == 0)
39858 cp_lexer_consume_token (parser
->lexer
);
39860 nlist
= build_omp_clause (loc
, OMP_CLAUSE_DOACROSS
);
39861 OMP_CLAUSE_DOACROSS_KIND (nlist
) = OMP_CLAUSE_DOACROSS_SOURCE
;
39862 OMP_CLAUSE_DECL (nlist
) = NULL_TREE
;
39863 OMP_CLAUSE_CHAIN (nlist
) = list
;
39866 nlist
= cp_parser_omp_clause_doacross_sink (parser
, loc
, list
, false);
39868 if (!parens
.require_close (parser
))
39869 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39870 /*or_comma=*/false,
39871 /*consume_paren=*/true);
39876 map ( map-kind : variable-list )
39877 map ( variable-list )
39880 alloc | to | from | tofrom
39884 alloc | to | from | tofrom | release | delete
39886 map ( always [,] map-kind: variable-list )
39889 map ( [map-type-modifier[,] ...] map-kind: variable-list )
39895 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
39898 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
39900 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39904 int map_kind_pos
= 0;
39905 while (cp_lexer_peek_nth_token (parser
->lexer
, pos
)->type
== CPP_NAME
39906 || cp_lexer_peek_nth_token (parser
->lexer
, pos
)->keyword
== RID_DELETE
)
39908 if (cp_lexer_peek_nth_token (parser
->lexer
, pos
+ 1)->type
== CPP_COLON
)
39910 map_kind_pos
= pos
;
39914 if (cp_lexer_peek_nth_token (parser
->lexer
, pos
+ 1)->type
== CPP_COMMA
)
39919 bool always_modifier
= false;
39920 bool close_modifier
= false;
39921 for (int pos
= 1; pos
< map_kind_pos
; ++pos
)
39923 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
39924 if (tok
->type
== CPP_COMMA
)
39926 cp_lexer_consume_token (parser
->lexer
);
39930 const char *p
= IDENTIFIER_POINTER (tok
->u
.value
);
39931 if (strcmp ("always", p
) == 0)
39933 if (always_modifier
)
39935 cp_parser_error (parser
, "too many %<always%> modifiers");
39936 cp_parser_skip_to_closing_parenthesis (parser
,
39937 /*recovering=*/true,
39938 /*or_comma=*/false,
39939 /*consume_paren=*/true);
39942 always_modifier
= true;
39944 else if (strcmp ("close", p
) == 0)
39946 if (close_modifier
)
39948 cp_parser_error (parser
, "too many %<close%> modifiers");
39949 cp_parser_skip_to_closing_parenthesis (parser
,
39950 /*recovering=*/true,
39951 /*or_comma=*/false,
39952 /*consume_paren=*/true);
39955 close_modifier
= true;
39959 cp_parser_error (parser
, "%<#pragma omp target%> with "
39960 "modifier other than %<always%> or "
39961 "%<close%> on %<map%> clause");
39962 cp_parser_skip_to_closing_parenthesis (parser
,
39963 /*recovering=*/true,
39964 /*or_comma=*/false,
39965 /*consume_paren=*/true);
39969 cp_lexer_consume_token (parser
->lexer
);
39972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
39973 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
39975 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39976 const char *p
= IDENTIFIER_POINTER (id
);
39978 if (strcmp ("alloc", p
) == 0)
39979 kind
= GOMP_MAP_ALLOC
;
39980 else if (strcmp ("to", p
) == 0)
39981 kind
= always_modifier
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
39982 else if (strcmp ("from", p
) == 0)
39983 kind
= always_modifier
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
39984 else if (strcmp ("tofrom", p
) == 0)
39985 kind
= always_modifier
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
39986 else if (strcmp ("release", p
) == 0)
39987 kind
= GOMP_MAP_RELEASE
;
39990 cp_parser_error (parser
, "invalid map kind");
39991 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39992 /*or_comma=*/false,
39993 /*consume_paren=*/true);
39996 cp_lexer_consume_token (parser
->lexer
);
39997 cp_lexer_consume_token (parser
->lexer
);
39999 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
40000 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
40002 kind
= GOMP_MAP_DELETE
;
40003 cp_lexer_consume_token (parser
->lexer
);
40004 cp_lexer_consume_token (parser
->lexer
);
40007 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
40010 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
40011 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
40017 device ( expression )
40020 device ( [device-modifier :] integer-expression )
40023 ancestor | device_num */
40026 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
40027 location_t location
)
40030 bool ancestor
= false;
40032 matching_parens parens
;
40033 if (!parens
.require_open (parser
))
40036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
40037 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
40039 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
40040 const char *p
= IDENTIFIER_POINTER (tok
->u
.value
);
40041 if (strcmp ("ancestor", p
) == 0)
40045 /* A requires directive with the reverse_offload clause must be
40047 if ((omp_requires_mask
& OMP_REQUIRES_REVERSE_OFFLOAD
) == 0)
40049 error_at (tok
->location
, "%<ancestor%> device modifier not "
40050 "preceded by %<requires%> directive "
40051 "with %<reverse_offload%> clause");
40052 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
40056 else if (strcmp ("device_num", p
) == 0)
40060 error_at (tok
->location
, "expected %<ancestor%> or %<device_num%>");
40061 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
40064 cp_lexer_consume_token (parser
->lexer
);
40065 cp_lexer_consume_token (parser
->lexer
);
40068 t
= cp_parser_assignment_expression (parser
);
40070 if (t
== error_mark_node
40071 || !parens
.require_close (parser
))
40072 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
40073 /*or_comma=*/false,
40074 /*consume_paren=*/true);
40076 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
40077 "device", location
);
40079 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
40080 OMP_CLAUSE_DEVICE_ID (c
) = t
;
40081 OMP_CLAUSE_CHAIN (c
) = list
;
40082 OMP_CLAUSE_DEVICE_ANCESTOR (c
) = ancestor
;
40088 dist_schedule ( static )
40089 dist_schedule ( static , expression ) */
40092 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
40093 location_t location
)
40097 matching_parens parens
;
40098 if (!parens
.require_open (parser
))
40101 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
40103 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
40105 cp_lexer_consume_token (parser
->lexer
);
40107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
40109 cp_lexer_consume_token (parser
->lexer
);
40111 t
= cp_parser_assignment_expression (parser
);
40113 if (t
== error_mark_node
)
40115 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
40117 if (!parens
.require_close (parser
))
40120 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
40123 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
40124 "dist_schedule", location); */
40125 if (omp_find_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
))
40126 warning_at (location
, 0, "too many %qs clauses", "dist_schedule");
40127 OMP_CLAUSE_CHAIN (c
) = list
;
40131 cp_parser_error (parser
, "invalid dist_schedule kind");
40133 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
40134 /*or_comma=*/false,
40135 /*consume_paren=*/true);
40140 proc_bind ( proc-bind-kind )
40143 primary | master | close | spread
40144 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
40147 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
40148 location_t location
)
40151 enum omp_clause_proc_bind_kind kind
;
40153 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
40156 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40158 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40159 const char *p
= IDENTIFIER_POINTER (id
);
40161 if (strcmp ("primary", p
) == 0)
40162 kind
= OMP_CLAUSE_PROC_BIND_PRIMARY
;
40163 else if (strcmp ("master", p
) == 0)
40164 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
40165 else if (strcmp ("close", p
) == 0)
40166 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
40167 else if (strcmp ("spread", p
) == 0)
40168 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
40175 cp_lexer_consume_token (parser
->lexer
);
40176 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
40179 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
40180 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
40182 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
40183 OMP_CLAUSE_CHAIN (c
) = list
;
40187 cp_parser_error (parser
, "invalid depend kind");
40189 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
40190 /*or_comma=*/false,
40191 /*consume_paren=*/true);
40196 device_type ( host | nohost | any ) */
40199 cp_parser_omp_clause_device_type (cp_parser
*parser
, tree list
,
40200 location_t location
)
40203 enum omp_clause_device_type_kind kind
;
40205 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
40208 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40210 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40211 const char *p
= IDENTIFIER_POINTER (id
);
40213 if (strcmp ("host", p
) == 0)
40214 kind
= OMP_CLAUSE_DEVICE_TYPE_HOST
;
40215 else if (strcmp ("nohost", p
) == 0)
40216 kind
= OMP_CLAUSE_DEVICE_TYPE_NOHOST
;
40217 else if (strcmp ("any", p
) == 0)
40218 kind
= OMP_CLAUSE_DEVICE_TYPE_ANY
;
40225 cp_lexer_consume_token (parser
->lexer
);
40226 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
40229 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE_TYPE
);
40230 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
40232 OMP_CLAUSE_DEVICE_TYPE_KIND (c
) = kind
;
40233 OMP_CLAUSE_CHAIN (c
) = list
;
40237 cp_parser_error (parser
, "invalid depend kind");
40239 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
40240 /*or_comma=*/false,
40241 /*consume_paren=*/true);
40246 async [( int-expr )] */
40249 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
40252 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40254 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
40256 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40258 matching_parens parens
;
40259 parens
.consume_open (parser
);
40261 t
= cp_parser_assignment_expression (parser
);
40262 if (t
== error_mark_node
40263 || !parens
.require_close (parser
))
40264 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
40265 /*or_comma=*/false,
40266 /*consume_paren=*/true);
40269 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
40271 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
40272 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
40273 OMP_CLAUSE_CHAIN (c
) = list
;
40279 /* Parse all OpenACC clauses. The set clauses allowed by the directive
40280 is a bitmask in MASK. Return the list of clauses found. */
40283 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
40284 const char *where
, cp_token
*pragma_tok
,
40285 bool finish_p
= true)
40287 tree clauses
= NULL
;
40290 /* Don't create location wrapper nodes within OpenACC clauses. */
40291 auto_suppress_location_wrappers sentinel
;
40293 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
40296 pragma_omp_clause c_kind
;
40297 omp_clause_code code
;
40298 const char *c_name
;
40299 tree prev
= clauses
;
40301 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
40302 cp_lexer_consume_token (parser
->lexer
);
40304 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
40305 c_kind
= cp_parser_omp_clause_name (parser
);
40309 case PRAGMA_OACC_CLAUSE_ASYNC
:
40310 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
40313 case PRAGMA_OACC_CLAUSE_AUTO
:
40314 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_AUTO
,
40318 case PRAGMA_OACC_CLAUSE_ATTACH
:
40319 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40322 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
40323 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
40324 c_name
= "collapse";
40326 case PRAGMA_OACC_CLAUSE_COPY
:
40327 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40330 case PRAGMA_OACC_CLAUSE_COPYIN
:
40331 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40334 case PRAGMA_OACC_CLAUSE_COPYOUT
:
40335 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40336 c_name
= "copyout";
40338 case PRAGMA_OACC_CLAUSE_CREATE
:
40339 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40342 case PRAGMA_OACC_CLAUSE_DELETE
:
40343 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40346 case PRAGMA_OMP_CLAUSE_DEFAULT
:
40347 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
40348 c_name
= "default";
40350 case PRAGMA_OACC_CLAUSE_DETACH
:
40351 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40354 case PRAGMA_OACC_CLAUSE_DEVICE
:
40355 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40358 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
40359 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
40360 c_name
= "deviceptr";
40362 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
40363 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40364 c_name
= "device_resident";
40366 case PRAGMA_OACC_CLAUSE_FINALIZE
:
40367 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_FINALIZE
,
40369 c_name
= "finalize";
40371 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
40372 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
40374 c_name
= "firstprivate";
40376 case PRAGMA_OACC_CLAUSE_GANG
:
40378 clauses
= cp_parser_oacc_shape_clause (parser
, here
, OMP_CLAUSE_GANG
,
40381 case PRAGMA_OACC_CLAUSE_HOST
:
40382 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40385 case PRAGMA_OACC_CLAUSE_IF
:
40386 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
40389 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
40390 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_IF_PRESENT
,
40392 c_name
= "if_present";
40394 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
40395 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_INDEPENDENT
,
40397 c_name
= "independent";
40399 case PRAGMA_OACC_CLAUSE_LINK
:
40400 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40403 case PRAGMA_OACC_CLAUSE_NO_CREATE
:
40404 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40405 c_name
= "no_create";
40407 case PRAGMA_OACC_CLAUSE_NOHOST
:
40408 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_NOHOST
,
40412 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
40413 code
= OMP_CLAUSE_NUM_GANGS
;
40414 c_name
= "num_gangs";
40415 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
40418 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
40419 c_name
= "num_workers";
40420 code
= OMP_CLAUSE_NUM_WORKERS
;
40421 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
40424 case PRAGMA_OACC_CLAUSE_PRESENT
:
40425 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
40426 c_name
= "present";
40428 case PRAGMA_OACC_CLAUSE_PRIVATE
:
40429 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
40431 c_name
= "private";
40433 case PRAGMA_OACC_CLAUSE_REDUCTION
:
40435 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
40437 c_name
= "reduction";
40439 case PRAGMA_OACC_CLAUSE_SEQ
:
40440 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_SEQ
,
40444 case PRAGMA_OACC_CLAUSE_TILE
:
40445 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
40448 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
40449 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
40451 c_name
= "use_device";
40453 case PRAGMA_OACC_CLAUSE_VECTOR
:
40455 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
40459 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
40460 c_name
= "vector_length";
40461 code
= OMP_CLAUSE_VECTOR_LENGTH
;
40462 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
40465 case PRAGMA_OACC_CLAUSE_WAIT
:
40466 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
40469 case PRAGMA_OACC_CLAUSE_WORKER
:
40471 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
40476 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
40482 if (((mask
>> c_kind
) & 1) == 0)
40484 /* Remove the invalid clause(s) from the list to avoid
40485 confusing the rest of the compiler. */
40487 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
40492 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40495 return finish_omp_clauses (clauses
, C_ORT_ACC
);
40500 /* Parse all OpenMP clauses. The set clauses allowed by the directive
40501 is a bitmask in MASK. Return the list of clauses found.
40502 FINISH_P set if finish_omp_clauses should be called.
40503 NESTED non-zero if clauses should be terminated by closing paren instead
40504 of end of pragma. If it is 2, additionally commas are required in between
40508 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
40509 const char *where
, cp_token
*pragma_tok
,
40510 bool finish_p
= true, int nested
= 0)
40512 tree clauses
= NULL
;
40514 cp_token
*token
= NULL
;
40516 /* Don't create location wrapper nodes within OpenMP clauses. */
40517 auto_suppress_location_wrappers sentinel
;
40519 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
40521 pragma_omp_clause c_kind
;
40522 const char *c_name
;
40523 tree prev
= clauses
;
40525 if (nested
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
40529 /* OpenMP 5.1 allows optional comma in between directive-name and
40530 clauses everywhere, but as we aren't done with OpenMP 5.0
40531 implementation yet, let's allow it for now only in C++11
40533 || (parser
->lexer
->in_omp_attribute_pragma
&& nested
!= 2))
40535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
40536 cp_lexer_consume_token (parser
->lexer
);
40537 else if (nested
== 2)
40538 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
40539 "clauses in %<simd%> trait should be separated "
40543 token
= cp_lexer_peek_token (parser
->lexer
);
40544 c_kind
= cp_parser_omp_clause_name (parser
);
40548 case PRAGMA_OMP_CLAUSE_BIND
:
40549 clauses
= cp_parser_omp_clause_bind (parser
, clauses
,
40553 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
40554 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
40556 c_name
= "collapse";
40558 case PRAGMA_OMP_CLAUSE_COPYIN
:
40559 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
40562 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
40563 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
40565 c_name
= "copyprivate";
40567 case PRAGMA_OMP_CLAUSE_DEFAULT
:
40568 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
40569 token
->location
, false);
40570 c_name
= "default";
40572 case PRAGMA_OMP_CLAUSE_FILTER
:
40573 clauses
= cp_parser_omp_clause_filter (parser
, clauses
,
40577 case PRAGMA_OMP_CLAUSE_FINAL
:
40578 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
40581 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
40582 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
40584 c_name
= "firstprivate";
40586 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
40587 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
40589 c_name
= "grainsize";
40591 case PRAGMA_OMP_CLAUSE_HINT
:
40592 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
40596 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
40597 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
40599 c_name
= "defaultmap";
40601 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
40602 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
40604 c_name
= "use_device_ptr";
40606 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
:
40607 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_ADDR
,
40609 c_name
= "use_device_addr";
40611 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
40612 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
40614 c_name
= "is_device_ptr";
40616 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR
:
40617 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_HAS_DEVICE_ADDR
,
40619 c_name
= "has_device_addr";
40621 case PRAGMA_OMP_CLAUSE_IF
:
40622 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
40626 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
40628 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
40630 c_name
= "in_reduction";
40632 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
40633 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
40634 c_name
= "lastprivate";
40636 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
40637 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
40639 c_name
= "mergeable";
40641 case PRAGMA_OMP_CLAUSE_NOWAIT
:
40642 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
,
40646 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
40647 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
40649 c_name
= "num_tasks";
40651 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
40652 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
40654 c_name
= "num_threads";
40656 case PRAGMA_OMP_CLAUSE_ORDER
:
40657 clauses
= cp_parser_omp_clause_order (parser
, clauses
,
40661 case PRAGMA_OMP_CLAUSE_ORDERED
:
40662 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
40664 c_name
= "ordered";
40666 case PRAGMA_OMP_CLAUSE_PRIORITY
:
40667 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
40669 c_name
= "priority";
40671 case PRAGMA_OMP_CLAUSE_PRIVATE
:
40672 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
40674 c_name
= "private";
40676 case PRAGMA_OMP_CLAUSE_REDUCTION
:
40678 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
40680 c_name
= "reduction";
40682 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
40683 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
40685 c_name
= "schedule";
40687 case PRAGMA_OMP_CLAUSE_SHARED
:
40688 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
40692 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
40694 = cp_parser_omp_clause_reduction (parser
,
40695 OMP_CLAUSE_TASK_REDUCTION
,
40697 c_name
= "task_reduction";
40699 case PRAGMA_OMP_CLAUSE_UNTIED
:
40700 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
40704 case PRAGMA_OMP_CLAUSE_INBRANCH
:
40705 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
40706 clauses
, token
->location
);
40707 c_name
= "inbranch";
40709 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
40710 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
40712 c_name
= "nontemporal";
40714 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
40715 clauses
= cp_parser_omp_clause_branch (parser
,
40716 OMP_CLAUSE_NOTINBRANCH
,
40717 clauses
, token
->location
);
40718 c_name
= "notinbranch";
40720 case PRAGMA_OMP_CLAUSE_PARALLEL
:
40721 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
40722 clauses
, token
->location
);
40723 c_name
= "parallel";
40727 error_at (token
->location
, "%qs must be the first clause of %qs",
40732 case PRAGMA_OMP_CLAUSE_FOR
:
40733 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
40734 clauses
, token
->location
);
40737 goto clause_not_first
;
40739 case PRAGMA_OMP_CLAUSE_SECTIONS
:
40740 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
40741 clauses
, token
->location
);
40742 c_name
= "sections";
40744 goto clause_not_first
;
40746 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
40747 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
40748 clauses
, token
->location
);
40749 c_name
= "taskgroup";
40751 goto clause_not_first
;
40753 case PRAGMA_OMP_CLAUSE_LINK
:
40754 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
40757 case PRAGMA_OMP_CLAUSE_TO
:
40758 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
40760 tree nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ENTER
,
40762 for (tree c
= nl
; c
!= clauses
; c
= OMP_CLAUSE_CHAIN (c
))
40763 OMP_CLAUSE_ENTER_TO (c
) = 1;
40767 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
,
40771 case PRAGMA_OMP_CLAUSE_FROM
:
40772 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
,
40776 case PRAGMA_OMP_CLAUSE_UNIFORM
:
40777 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
40779 c_name
= "uniform";
40781 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
40782 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
40784 c_name
= "num_teams";
40786 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
40787 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
40789 c_name
= "thread_limit";
40791 case PRAGMA_OMP_CLAUSE_ALIGNED
:
40792 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
40793 c_name
= "aligned";
40795 case PRAGMA_OMP_CLAUSE_ALLOCATE
:
40796 clauses
= cp_parser_omp_clause_allocate (parser
, clauses
);
40797 c_name
= "allocate";
40799 case PRAGMA_OMP_CLAUSE_LINEAR
:
40801 bool declare_simd
= false;
40802 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
40803 declare_simd
= true;
40804 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
40808 case PRAGMA_OMP_CLAUSE_AFFINITY
:
40809 clauses
= cp_parser_omp_clause_affinity (parser
, clauses
);
40810 c_name
= "affinity";
40812 case PRAGMA_OMP_CLAUSE_DEPEND
:
40813 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
40817 case PRAGMA_OMP_CLAUSE_DOACROSS
:
40818 clauses
= cp_parser_omp_clause_doacross (parser
, clauses
,
40820 c_name
= "doacross";
40822 case PRAGMA_OMP_CLAUSE_DETACH
:
40823 clauses
= cp_parser_omp_clause_detach (parser
, clauses
);
40826 case PRAGMA_OMP_CLAUSE_MAP
:
40827 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
40830 case PRAGMA_OMP_CLAUSE_DEVICE
:
40831 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
40835 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
40836 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
40838 c_name
= "dist_schedule";
40840 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
40841 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
40843 c_name
= "proc_bind";
40845 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE
:
40846 clauses
= cp_parser_omp_clause_device_type (parser
, clauses
,
40848 c_name
= "device_type";
40850 case PRAGMA_OMP_CLAUSE_SAFELEN
:
40851 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
40853 c_name
= "safelen";
40855 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
40856 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
40858 c_name
= "simdlen";
40860 case PRAGMA_OMP_CLAUSE_NOGROUP
:
40861 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
40863 c_name
= "nogroup";
40865 case PRAGMA_OMP_CLAUSE_THREADS
:
40867 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
40868 clauses
, token
->location
);
40869 c_name
= "threads";
40871 case PRAGMA_OMP_CLAUSE_SIMD
:
40873 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
40874 clauses
, token
->location
);
40877 case PRAGMA_OMP_CLAUSE_ENTER
:
40878 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ENTER
,
40883 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
40889 if (((mask
>> c_kind
) & 1) == 0)
40891 /* Remove the invalid clause(s) from the list to avoid
40892 confusing the rest of the compiler. */
40894 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
40899 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40902 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
40903 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
40905 return finish_omp_clauses (clauses
, C_ORT_OMP
);
40914 In practice, we're also interested in adding the statement to an
40915 outer node. So it is convenient if we work around the fact that
40916 cp_parser_statement calls add_stmt. */
40919 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
40921 unsigned save
= parser
->in_statement
;
40923 /* Only move the values to IN_OMP_BLOCK if they weren't false.
40924 This preserves the "not within loop or switch" style error messages
40925 for nonsense cases like
40931 if (parser
->in_statement
)
40932 parser
->in_statement
= IN_OMP_BLOCK
;
40938 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
40940 parser
->in_statement
= save
;
40944 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
40946 tree stmt
= begin_omp_structured_block ();
40947 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
40949 parser
->omp_attrs_forbidden_p
= true;
40950 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
40952 cp_parser_end_omp_structured_block (parser
, save
);
40953 return finish_omp_structured_block (stmt
);
40957 # pragma omp allocate (list) [allocator(allocator)] */
40960 cp_parser_omp_allocate (cp_parser
*parser
, cp_token
*pragma_tok
)
40962 tree allocator
= NULL_TREE
;
40963 location_t loc
= pragma_tok
->location
;
40964 tree nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ALLOCATE
, NULL_TREE
);
40966 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
40967 if (parser
->lexer
->in_omp_attribute_pragma
40968 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
40969 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
40970 cp_lexer_consume_token (parser
->lexer
);
40972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40974 matching_parens parens
;
40975 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40976 const char *p
= IDENTIFIER_POINTER (id
);
40977 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40978 cp_lexer_consume_token (parser
->lexer
);
40979 if (strcmp (p
, "allocator") != 0)
40980 error_at (cloc
, "expected %<allocator%>");
40981 else if (parens
.require_open (parser
))
40983 allocator
= cp_parser_assignment_expression (parser
);
40984 if (allocator
== error_mark_node
)
40985 allocator
= NULL_TREE
;
40986 parens
.require_close (parser
);
40989 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40992 for (tree c
= nl
; c
!= NULL_TREE
; c
= OMP_CLAUSE_CHAIN (c
))
40993 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c
) = allocator
;
40995 sorry_at (loc
, "%<#pragma omp allocate%> not yet supported");
40999 # pragma omp atomic new-line
41003 x binop= expr | x++ | ++x | x-- | --x
41005 +, *, -, /, &, ^, |, <<, >>
41007 where x is an lvalue expression with scalar type.
41010 # pragma omp atomic new-line
41013 # pragma omp atomic read new-line
41016 # pragma omp atomic write new-line
41019 # pragma omp atomic update new-line
41022 # pragma omp atomic capture new-line
41025 # pragma omp atomic capture new-line
41033 expression-stmt | x = x binop expr
41035 v = expression-stmt
41037 { v = x; update-stmt; } | { update-stmt; v = x; }
41041 expression-stmt | x = x binop expr | x = expr binop x
41045 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
41048 # pragma omp atomic compare new-line
41049 conditional-update-atomic
41051 # pragma omp atomic compare capture new-line
41052 conditional-update-capture-atomic
41054 conditional-update-atomic:
41055 cond-expr-stmt | cond-update-stmt
41057 x = expr ordop x ? expr : x;
41058 x = x ordop expr ? expr : x;
41059 x = x == e ? d : x;
41061 if (expr ordop x) { x = expr; }
41062 if (x ordop expr) { x = expr; }
41063 if (x == e) { x = d; }
41066 conditional-update-capture-atomic:
41068 { v = x; cond-expr-stmt }
41069 { cond-expr-stmt v = x; }
41070 { v = x; cond-update-stmt }
41071 { cond-update-stmt v = x; }
41072 if (x == e) { x = d; } else { v = x; }
41073 { r = x == e; if (r) { x = d; } }
41074 { r = x == e; if (r) { x = d; } else { v = x; } }
41076 where x, r and v are lvalue expressions with scalar type,
41077 expr, e and d are expressions with scalar type and e might be
41081 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
, bool openacc
)
41083 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
41084 tree rhs1
= NULL_TREE
, orig_lhs
, r
= NULL_TREE
;
41085 location_t loc
= pragma_tok
->location
;
41086 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
41087 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
41088 bool structured_block
= false;
41090 tree clauses
= NULL_TREE
;
41091 bool capture
= false;
41092 bool compare
= false;
41094 enum omp_memory_order fail
= OMP_MEMORY_ORDER_UNSPECIFIED
;
41095 bool no_semicolon
= false;
41096 bool extra_scope
= false;
41098 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
41100 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41101 if ((!first
|| parser
->lexer
->in_omp_attribute_pragma
)
41102 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
41103 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
41104 cp_lexer_consume_token (parser
->lexer
);
41108 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41110 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41111 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41112 const char *p
= IDENTIFIER_POINTER (id
);
41113 enum tree_code new_code
= ERROR_MARK
;
41114 enum omp_memory_order new_memory_order
41115 = OMP_MEMORY_ORDER_UNSPECIFIED
;
41116 bool new_capture
= false;
41117 bool new_compare
= false;
41118 bool new_weak
= false;
41119 enum omp_memory_order new_fail
= OMP_MEMORY_ORDER_UNSPECIFIED
;
41121 if (!strcmp (p
, "read"))
41122 new_code
= OMP_ATOMIC_READ
;
41123 else if (!strcmp (p
, "write"))
41124 new_code
= NOP_EXPR
;
41125 else if (!strcmp (p
, "update"))
41126 new_code
= OMP_ATOMIC
;
41127 else if (openacc
&& !strcmp (p
, "capture"))
41128 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
41132 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
41133 "or %<capture%> clause");
41135 else if (!strcmp (p
, "capture"))
41136 new_capture
= true;
41137 else if (!strcmp (p
, "compare"))
41138 new_compare
= true;
41139 else if (!strcmp (p
, "weak"))
41141 else if (!strcmp (p
, "fail"))
41143 matching_parens parens
;
41145 cp_lexer_consume_token (parser
->lexer
);
41146 if (!parens
.require_open (parser
))
41149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41151 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41152 const char *q
= IDENTIFIER_POINTER (id
);
41154 if (!strcmp (q
, "seq_cst"))
41155 new_fail
= OMP_MEMORY_ORDER_SEQ_CST
;
41156 else if (!strcmp (q
, "acquire"))
41157 new_fail
= OMP_MEMORY_ORDER_ACQUIRE
;
41158 else if (!strcmp (q
, "relaxed"))
41159 new_fail
= OMP_MEMORY_ORDER_RELAXED
;
41162 if (new_fail
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
41164 cp_lexer_consume_token (parser
->lexer
);
41165 if (fail
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
41166 error_at (cloc
, "too many %qs clauses", "fail");
41171 cp_parser_error (parser
, "expected %<seq_cst%>, %<acquire%> "
41173 if (new_fail
== OMP_MEMORY_ORDER_UNSPECIFIED
41174 || !parens
.require_close (parser
))
41175 cp_parser_skip_to_closing_parenthesis (parser
,
41176 /*recovering=*/true,
41177 /*or_comma=*/false,
41178 /*consume_paren=*/true);
41181 else if (!strcmp (p
, "seq_cst"))
41182 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
41183 else if (!strcmp (p
, "acq_rel"))
41184 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
41185 else if (!strcmp (p
, "release"))
41186 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
41187 else if (!strcmp (p
, "acquire"))
41188 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
41189 else if (!strcmp (p
, "relaxed"))
41190 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
41191 else if (!strcmp (p
, "hint"))
41193 cp_lexer_consume_token (parser
->lexer
);
41194 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
41200 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
41201 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
41202 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
41203 "%<relaxed%> or %<hint%> clause");
41207 if (new_code
!= ERROR_MARK
)
41209 /* OpenACC permits 'update capture'. */
41211 && code
== OMP_ATOMIC
41212 && new_code
== OMP_ATOMIC_CAPTURE_NEW
)
41214 else if (code
!= ERROR_MARK
)
41215 error_at (cloc
, "too many atomic clauses");
41219 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
41221 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
41222 error_at (cloc
, "too many memory order clauses");
41224 memory_order
= new_memory_order
;
41226 else if (new_capture
)
41229 error_at (cloc
, "too many %qs clauses", "capture");
41233 else if (new_compare
)
41236 error_at (cloc
, "too many %qs clauses", "compare");
41243 error_at (cloc
, "too many %qs clauses", "weak");
41247 cp_lexer_consume_token (parser
->lexer
);
41253 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41255 if (code
== ERROR_MARK
)
41259 if (code
!= OMP_ATOMIC
)
41260 error_at (loc
, "%qs clause is incompatible with %<read%> or %<write%> "
41261 "clauses", "capture");
41263 code
= OMP_ATOMIC_CAPTURE_NEW
;
41265 if (compare
&& code
!= OMP_ATOMIC
&& code
!= OMP_ATOMIC_CAPTURE_NEW
)
41267 error_at (loc
, "%qs clause is incompatible with %<read%> or %<write%> "
41268 "clauses", "compare");
41271 if (fail
!= OMP_MEMORY_ORDER_UNSPECIFIED
&& !compare
)
41273 error_at (loc
, "%qs clause requires %qs clause", "fail", "compare");
41274 fail
= OMP_MEMORY_ORDER_UNSPECIFIED
;
41276 if (weak
&& !compare
)
41278 error_at (loc
, "%qs clause requires %qs clause", "weak", "compare");
41282 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
41283 else if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
41286 = (enum omp_requires
) (omp_requires_mask
41287 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
41288 switch ((enum omp_memory_order
)
41289 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
41291 case OMP_MEMORY_ORDER_UNSPECIFIED
:
41292 case OMP_MEMORY_ORDER_RELAXED
:
41293 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
41295 case OMP_MEMORY_ORDER_SEQ_CST
:
41296 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
41298 case OMP_MEMORY_ORDER_ACQ_REL
:
41301 case OMP_ATOMIC_READ
:
41302 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
41304 case NOP_EXPR
: /* atomic write */
41305 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
41308 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
41313 gcc_unreachable ();
41319 case OMP_ATOMIC_READ
:
41320 if (memory_order
== OMP_MEMORY_ORDER_RELEASE
)
41322 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
41323 "%<release%> clause");
41324 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
41326 else if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
)
41327 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
41329 case NOP_EXPR
: /* atomic write */
41330 if (memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
41332 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
41333 "%<acquire%> clause");
41334 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
41336 else if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
)
41337 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
41342 if (fail
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
41344 = (enum omp_memory_order
) (memory_order
41345 | (fail
<< OMP_FAIL_MEMORY_ORDER_SHIFT
));
41349 case OMP_ATOMIC_READ
:
41350 case NOP_EXPR
: /* atomic write */
41351 v
= cp_parser_unary_expression (parser
);
41352 if (v
== error_mark_node
)
41354 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
41356 if (code
== NOP_EXPR
)
41357 lhs
= cp_parser_expression (parser
);
41359 lhs
= cp_parser_unary_expression (parser
);
41360 if (lhs
== error_mark_node
)
41362 if (code
== NOP_EXPR
)
41364 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
41372 case OMP_ATOMIC_CAPTURE_NEW
:
41373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
41375 cp_lexer_consume_token (parser
->lexer
);
41376 structured_block
= true;
41379 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
41383 v
= cp_parser_unary_expression (parser
);
41384 if (v
== error_mark_node
)
41386 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
41389 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
41391 location_t eloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41392 error_at (eloc
, "expected expression");
41401 if (compare
&& cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
41403 cp_lexer_consume_token (parser
->lexer
);
41405 matching_parens parens
;
41406 if (!parens
.require_open (parser
))
41408 location_t eloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41411 cmp_expr
= cp_parser_unary_expression (parser
);
41413 cmp_expr
= cp_parser_binary_expression (parser
, false, true,
41414 PREC_NOT_OPERATOR
, NULL
);
41415 if (!parens
.require_close (parser
))
41416 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
41417 if (cmp_expr
== error_mark_node
)
41421 if (!cp_tree_equal (cmp_expr
, r
))
41425 gcc_assert (TREE_CODE (cmp_expr
) == EQ_EXPR
);
41427 if (TREE_CODE (cmp_expr
) == EQ_EXPR
)
41429 else if (!structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
41431 error_at (EXPR_LOC_OR_LOC (cmp_expr
, eloc
),
41432 "expected %<==%> comparison in %<if%> condition");
41435 else if (TREE_CODE (cmp_expr
) != GT_EXPR
41436 && TREE_CODE (cmp_expr
) != LT_EXPR
)
41438 error_at (EXPR_LOC_OR_LOC (cmp_expr
, eloc
),
41439 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
41443 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
41446 extra_scope
= true;
41447 eloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41448 lhs
= cp_parser_unary_expression (parser
);
41450 if (lhs
== error_mark_node
)
41452 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
41454 cp_parser_error (parser
, "expected %<=%>");
41457 cp_lexer_consume_token (parser
->lexer
);
41458 eloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41459 if (TREE_CODE (cmp_expr
) == EQ_EXPR
)
41460 rhs1
= cp_parser_expression (parser
);
41462 rhs1
= cp_parser_simple_cast_expression (parser
);
41464 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
41467 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
41470 extra_scope
= false;
41471 no_semicolon
= true;
41473 if (cp_tree_equal (TREE_OPERAND (cmp_expr
, 0), lhs
))
41475 if (TREE_CODE (cmp_expr
) == EQ_EXPR
)
41477 opcode
= COND_EXPR
;
41478 rhs
= TREE_OPERAND (cmp_expr
, 1);
41480 else if (cp_tree_equal (TREE_OPERAND (cmp_expr
, 1), rhs1
))
41482 opcode
= (TREE_CODE (cmp_expr
) == GT_EXPR
41483 ? MIN_EXPR
: MAX_EXPR
);
41485 rhs1
= TREE_OPERAND (cmp_expr
, 0);
41490 else if (TREE_CODE (cmp_expr
) == EQ_EXPR
)
41492 else if (cp_tree_equal (TREE_OPERAND (cmp_expr
, 1), lhs
)
41493 && cp_tree_equal (TREE_OPERAND (cmp_expr
, 0), rhs1
))
41495 opcode
= (TREE_CODE (cmp_expr
) == GT_EXPR
41496 ? MAX_EXPR
: MIN_EXPR
);
41498 rhs1
= TREE_OPERAND (cmp_expr
, 1);
41503 cp_parser_error (parser
,
41504 "invalid form of %<#pragma omp atomic compare%>");
41508 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
41510 if (code
!= OMP_ATOMIC_CAPTURE_NEW
41511 || (structured_block
&& r
== NULL_TREE
)
41512 || TREE_CODE (cmp_expr
) != EQ_EXPR
)
41514 eloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41515 error_at (eloc
, "unexpected %<else%>");
41519 cp_lexer_consume_token (parser
->lexer
);
41521 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
41524 extra_scope
= true;
41525 v
= cp_parser_unary_expression (parser
);
41526 if (v
== error_mark_node
)
41528 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
41531 tree expr
= cp_parser_simple_cast_expression (parser
);
41533 if (!cp_tree_equal (expr
, lhs
))
41536 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
41539 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
41542 extra_scope
= false;
41543 code
= OMP_ATOMIC_CAPTURE_OLD
;
41544 if (r
== NULL_TREE
)
41545 /* Signal to c_finish_omp_atomic that in
41546 if (x == e) { x = d; } else { v = x; }
41547 case the store to v should be conditional. */
41548 r
= void_list_node
;
41550 else if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
41552 cp_parser_error (parser
, "expected %<else%>");
41555 else if (code
== OMP_ATOMIC_CAPTURE_NEW
41561 lhs
= cp_parser_unary_expression (parser
);
41563 switch (TREE_CODE (lhs
))
41568 case POSTINCREMENT_EXPR
:
41569 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
41570 code
= OMP_ATOMIC_CAPTURE_OLD
;
41572 case PREINCREMENT_EXPR
:
41573 lhs
= TREE_OPERAND (lhs
, 0);
41574 opcode
= PLUS_EXPR
;
41575 rhs
= integer_one_node
;
41577 goto invalid_compare
;
41580 case POSTDECREMENT_EXPR
:
41581 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
41582 code
= OMP_ATOMIC_CAPTURE_OLD
;
41584 case PREDECREMENT_EXPR
:
41585 lhs
= TREE_OPERAND (lhs
, 0);
41586 opcode
= MINUS_EXPR
;
41587 rhs
= integer_one_node
;
41589 goto invalid_compare
;
41592 case COMPOUND_EXPR
:
41593 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
41594 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
41595 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
41596 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
41597 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
41598 (TREE_OPERAND (lhs
, 1), 0), 0)))
41600 /* Undo effects of boolean_increment for post {in,de}crement. */
41601 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
41604 if (TREE_CODE (lhs
) == MODIFY_EXPR
41605 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
41607 /* Undo effects of boolean_increment. */
41608 if (integer_onep (TREE_OPERAND (lhs
, 1)))
41610 /* This is pre or post increment. */
41611 rhs
= TREE_OPERAND (lhs
, 1);
41612 lhs
= TREE_OPERAND (lhs
, 0);
41614 if (code
== OMP_ATOMIC_CAPTURE_NEW
41615 && !structured_block
41616 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
41617 code
= OMP_ATOMIC_CAPTURE_OLD
;
41619 goto invalid_compare
;
41625 if (compare
&& !cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
41627 cp_parser_error (parser
, "expected %<=%>");
41630 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
41633 opcode
= MULT_EXPR
;
41636 opcode
= TRUNC_DIV_EXPR
;
41639 opcode
= PLUS_EXPR
;
41642 opcode
= MINUS_EXPR
;
41644 case CPP_LSHIFT_EQ
:
41645 opcode
= LSHIFT_EXPR
;
41647 case CPP_RSHIFT_EQ
:
41648 opcode
= RSHIFT_EXPR
;
41651 opcode
= BIT_AND_EXPR
;
41654 opcode
= BIT_IOR_EXPR
;
41657 opcode
= BIT_XOR_EXPR
;
41660 enum cp_parser_prec oprec
;
41662 cp_lexer_consume_token (parser
->lexer
);
41663 cp_parser_parse_tentatively (parser
);
41664 rhs1
= cp_parser_simple_cast_expression (parser
);
41665 if (rhs1
== error_mark_node
)
41667 cp_parser_abort_tentative_parse (parser
);
41668 cp_parser_simple_cast_expression (parser
);
41671 token
= cp_lexer_peek_token (parser
->lexer
);
41672 if (token
->type
!= CPP_SEMICOLON
41673 && (!compare
|| token
->type
!= CPP_QUERY
)
41674 && !cp_tree_equal (lhs
, rhs1
))
41676 cp_parser_abort_tentative_parse (parser
);
41677 cp_parser_parse_tentatively (parser
);
41678 rhs
= cp_parser_binary_expression (parser
, false, true,
41679 PREC_NOT_OPERATOR
, NULL
);
41680 if (rhs
== error_mark_node
)
41682 cp_parser_abort_tentative_parse (parser
);
41683 cp_parser_binary_expression (parser
, false, true,
41684 PREC_NOT_OPERATOR
, NULL
);
41687 switch (TREE_CODE (rhs
))
41690 case TRUNC_DIV_EXPR
:
41701 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
41703 if (cp_parser_parse_definitely (parser
))
41705 opcode
= TREE_CODE (rhs
);
41706 rhs1
= TREE_OPERAND (rhs
, 0);
41707 rhs
= TREE_OPERAND (rhs
, 1);
41716 || code
!= OMP_ATOMIC_CAPTURE_NEW
41717 || !structured_block
41721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
41722 && cp_lexer_nth_token_is_keyword (parser
->lexer
,
41725 if (cp_parser_parse_definitely (parser
))
41730 cp_lexer_consume_token (parser
->lexer
);
41738 && cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
)
41739 && cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1))
41740 && cp_parser_parse_definitely (parser
))
41742 opcode
= TREE_CODE (rhs
);
41743 rhs1
= TREE_OPERAND (rhs
, 0);
41744 rhs
= TREE_OPERAND (rhs
, 1);
41746 cp_lexer_consume_token (parser
->lexer
);
41747 bool saved_colon_corrects_to_scope_p
41748 = parser
->colon_corrects_to_scope_p
;
41749 parser
->colon_corrects_to_scope_p
= false;
41750 tree e1
= cp_parser_expression (parser
);
41751 parser
->colon_corrects_to_scope_p
41752 = saved_colon_corrects_to_scope_p
;
41753 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
41754 tree e2
= cp_parser_simple_cast_expression (parser
);
41755 if (cp_tree_equal (lhs
, e2
))
41757 if (cp_tree_equal (lhs
, rhs1
))
41759 if (opcode
== EQ_EXPR
)
41761 opcode
= COND_EXPR
;
41765 if (cp_tree_equal (rhs
, e1
))
41768 = opcode
== GT_EXPR
? MIN_EXPR
: MAX_EXPR
;
41775 gcc_assert (opcode
!= EQ_EXPR
);
41776 if (cp_tree_equal (rhs1
, e1
))
41779 = opcode
== GT_EXPR
? MAX_EXPR
: MIN_EXPR
;
41786 cp_parser_error (parser
,
41788 "%<#pragma omp atomic compare%>");
41795 cp_parser_abort_tentative_parse (parser
);
41796 if (structured_block
41797 && code
== OMP_ATOMIC_CAPTURE_OLD
41800 rhs
= cp_parser_expression (parser
);
41801 if (rhs
== error_mark_node
)
41807 cp_parser_error (parser
,
41808 "invalid form of %<#pragma omp atomic%>");
41811 if (!cp_parser_parse_definitely (parser
))
41813 switch (token
->type
)
41815 case CPP_SEMICOLON
:
41816 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
41818 code
= OMP_ATOMIC_CAPTURE_OLD
;
41823 cp_lexer_consume_token (parser
->lexer
);
41826 else if (structured_block
&& !compare
)
41833 cp_parser_error (parser
,
41834 "invalid form of %<#pragma omp atomic%>");
41837 opcode
= MULT_EXPR
;
41840 opcode
= TRUNC_DIV_EXPR
;
41843 opcode
= PLUS_EXPR
;
41846 opcode
= MINUS_EXPR
;
41849 opcode
= LSHIFT_EXPR
;
41852 opcode
= RSHIFT_EXPR
;
41855 opcode
= BIT_AND_EXPR
;
41858 opcode
= BIT_IOR_EXPR
;
41861 opcode
= BIT_XOR_EXPR
;
41873 cp_parser_error (parser
,
41874 "invalid operator for %<#pragma omp atomic%>");
41878 && TREE_CODE_CLASS (opcode
) != tcc_comparison
)
41880 cp_parser_error (parser
,
41882 "%<#pragma omp atomic compare%>");
41885 oprec
= TOKEN_PRECEDENCE (token
);
41886 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
41887 if (commutative_tree_code (opcode
))
41888 oprec
= (enum cp_parser_prec
) (oprec
- 1);
41889 cp_lexer_consume_token (parser
->lexer
);
41890 rhs
= cp_parser_binary_expression (parser
, false, false,
41892 if (rhs
== error_mark_node
)
41896 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
41898 cp_parser_error (parser
,
41900 "%<#pragma omp atomic compare%>");
41907 cp_parser_error (parser
,
41908 "invalid operator for %<#pragma omp atomic%>");
41911 cp_lexer_consume_token (parser
->lexer
);
41913 rhs
= cp_parser_expression (parser
);
41914 if (rhs
== error_mark_node
)
41919 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
&& r
== NULL_TREE
)
41922 && !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
41924 no_semicolon
= false;
41925 v
= cp_parser_unary_expression (parser
);
41926 if (v
== error_mark_node
)
41928 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
41930 lhs1
= cp_parser_unary_expression (parser
);
41931 if (lhs1
== error_mark_node
)
41934 if (structured_block
)
41937 cp_parser_consume_semicolon_at_end_of_statement (parser
);
41938 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
41941 if (weak
&& opcode
!= COND_EXPR
)
41943 error_at (loc
, "%<weak%> clause requires atomic equality comparison");
41946 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
41947 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
41948 rhs1
, r
, clauses
, memory_order
, weak
);
41949 if (!structured_block
&& !no_semicolon
)
41950 cp_parser_consume_semicolon_at_end_of_statement (parser
);
41954 error ("invalid form of %<pragma omp atomic compare%>");
41957 cp_parser_skip_to_end_of_block_or_statement (parser
);
41958 if (extra_scope
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
41959 cp_lexer_consume_token (parser
->lexer
);
41960 if (structured_block
)
41962 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
41963 cp_lexer_consume_token (parser
->lexer
);
41964 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
41966 cp_parser_skip_to_end_of_block_or_statement (parser
);
41967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
41968 cp_lexer_consume_token (parser
->lexer
);
41975 # pragma omp barrier new-line */
41978 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
41980 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41981 finish_omp_barrier ();
41985 # pragma omp critical [(name)] new-line
41989 # pragma omp critical [(name) [hint(expression)]] new-line
41990 structured-block */
41992 #define OMP_CRITICAL_CLAUSE_MASK \
41993 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
41996 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
41998 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
42000 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
42002 matching_parens parens
;
42003 parens
.consume_open (parser
);
42005 name
= cp_parser_identifier (parser
);
42007 if (name
== error_mark_node
42008 || !parens
.require_close (parser
))
42009 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
42010 /*or_comma=*/false,
42011 /*consume_paren=*/true);
42012 if (name
== error_mark_node
)
42015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
42016 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
42017 cp_lexer_consume_token (parser
->lexer
);
42020 clauses
= cp_parser_omp_all_clauses (parser
, OMP_CRITICAL_CLAUSE_MASK
,
42021 "#pragma omp critical", pragma_tok
);
42023 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
42024 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
42028 # pragma omp depobj ( depobj ) depobj-clause new-line
42031 depend (dependence-type : locator)
42033 update (dependence-type)
42042 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
42044 location_t loc
= pragma_tok
->location
;
42045 matching_parens parens
;
42046 if (!parens
.require_open (parser
))
42048 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42052 tree depobj
= cp_parser_assignment_expression (parser
);
42054 if (!parens
.require_close (parser
))
42055 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
42056 /*or_comma=*/false,
42057 /*consume_paren=*/true);
42059 tree clause
= NULL_TREE
;
42060 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INVALID
;
42061 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42062 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
42063 if (parser
->lexer
->in_omp_attribute_pragma
42064 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42065 cp_lexer_consume_token (parser
->lexer
);
42066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42068 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42069 const char *p
= IDENTIFIER_POINTER (id
);
42071 cp_lexer_consume_token (parser
->lexer
);
42072 if (!strcmp ("depend", p
))
42074 /* Don't create location wrapper nodes within the depend clause. */
42075 auto_suppress_location_wrappers sentinel
;
42076 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
42078 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
42080 clause
= error_mark_node
;
42082 else if (!strcmp ("destroy", p
))
42083 kind
= OMP_CLAUSE_DEPEND_LAST
;
42084 else if (!strcmp ("update", p
))
42086 matching_parens c_parens
;
42087 if (c_parens
.require_open (parser
))
42090 = cp_lexer_peek_token (parser
->lexer
)->location
;
42091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42093 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42094 const char *p2
= IDENTIFIER_POINTER (id2
);
42096 cp_lexer_consume_token (parser
->lexer
);
42097 if (!strcmp ("in", p2
))
42098 kind
= OMP_CLAUSE_DEPEND_IN
;
42099 else if (!strcmp ("out", p2
))
42100 kind
= OMP_CLAUSE_DEPEND_OUT
;
42101 else if (!strcmp ("inout", p2
))
42102 kind
= OMP_CLAUSE_DEPEND_INOUT
;
42103 else if (!strcmp ("mutexinoutset", p2
))
42104 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
42105 else if (!strcmp ("inoutset", p2
))
42106 kind
= OMP_CLAUSE_DEPEND_INOUTSET
;
42108 if (kind
== OMP_CLAUSE_DEPEND_INVALID
)
42110 clause
= error_mark_node
;
42111 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%>, "
42112 "%<mutexinoutset%> or %<inoutset%>");
42114 if (!c_parens
.require_close (parser
))
42115 cp_parser_skip_to_closing_parenthesis (parser
,
42116 /*recovering=*/true,
42117 /*or_comma=*/false,
42118 /*consume_paren=*/true);
42121 clause
= error_mark_node
;
42124 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_INVALID
)
42126 clause
= error_mark_node
;
42127 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
42129 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42131 finish_omp_depobj (loc
, depobj
, kind
, clause
);
42136 # pragma omp flush flush-vars[opt] new-line
42142 # pragma omp flush memory-order-clause new-line */
42145 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
42147 enum memmodel mo
= MEMMODEL_LAST
;
42148 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
42149 if (parser
->lexer
->in_omp_attribute_pragma
42150 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
42151 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
42152 cp_lexer_consume_token (parser
->lexer
);
42153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42155 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42156 const char *p
= IDENTIFIER_POINTER (id
);
42157 if (!strcmp (p
, "seq_cst"))
42158 mo
= MEMMODEL_SEQ_CST
;
42159 else if (!strcmp (p
, "acq_rel"))
42160 mo
= MEMMODEL_ACQ_REL
;
42161 else if (!strcmp (p
, "release"))
42162 mo
= MEMMODEL_RELEASE
;
42163 else if (!strcmp (p
, "acquire"))
42164 mo
= MEMMODEL_ACQUIRE
;
42166 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
42167 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
42169 cp_lexer_consume_token (parser
->lexer
);
42171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
42173 if (mo
!= MEMMODEL_LAST
)
42174 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
42175 "%<flush%> list specified together with memory order "
42177 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
42179 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42181 finish_omp_flush (mo
);
42184 /* Helper function, to parse omp for increment expression. */
42187 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
42189 tree cond
= cp_parser_binary_expression (parser
, false, true,
42190 PREC_NOT_OPERATOR
, NULL
);
42191 if (cond
== error_mark_node
42192 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
42194 cp_parser_skip_to_end_of_statement (parser
);
42195 return error_mark_node
;
42198 switch (TREE_CODE (cond
))
42206 if (code
!= OACC_LOOP
)
42208 gcc_fallthrough ();
42210 return error_mark_node
;
42213 /* If decl is an iterator, preserve LHS and RHS of the relational
42214 expr until finish_omp_for. */
42216 && (type_dependent_expression_p (decl
)
42217 || CLASS_TYPE_P (TREE_TYPE (decl
))))
42220 return build_x_binary_op (cp_expr_loc_or_input_loc (cond
),
42222 TREE_OPERAND (cond
, 0), ERROR_MARK
,
42223 TREE_OPERAND (cond
, 1), ERROR_MARK
,
42224 NULL_TREE
, /*overload=*/NULL
, tf_warning_or_error
);
42227 /* Helper function, to parse omp for increment expression. */
42230 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
42232 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
42238 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
42240 op
= (token
->type
== CPP_PLUS_PLUS
42241 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
42242 cp_lexer_consume_token (parser
->lexer
);
42243 lhs
= cp_parser_simple_cast_expression (parser
);
42245 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
42246 return error_mark_node
;
42247 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
42250 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
42252 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
42253 return error_mark_node
;
42255 token
= cp_lexer_peek_token (parser
->lexer
);
42256 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
42258 op
= (token
->type
== CPP_PLUS_PLUS
42259 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
42260 cp_lexer_consume_token (parser
->lexer
);
42261 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
42264 op
= cp_parser_assignment_operator_opt (parser
);
42265 if (op
== ERROR_MARK
)
42266 return error_mark_node
;
42268 if (op
!= NOP_EXPR
)
42270 rhs
= cp_parser_assignment_expression (parser
);
42271 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
42272 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
42275 lhs
= cp_parser_binary_expression (parser
, false, false,
42276 PREC_ADDITIVE_EXPRESSION
, NULL
);
42277 token
= cp_lexer_peek_token (parser
->lexer
);
42278 decl_first
= (lhs
== decl
42279 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
42282 if (token
->type
!= CPP_PLUS
42283 && token
->type
!= CPP_MINUS
)
42284 return error_mark_node
;
42288 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
42289 cp_lexer_consume_token (parser
->lexer
);
42290 rhs
= cp_parser_binary_expression (parser
, false, false,
42291 PREC_ADDITIVE_EXPRESSION
, NULL
);
42292 token
= cp_lexer_peek_token (parser
->lexer
);
42293 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
42295 if (lhs
== NULL_TREE
)
42297 if (op
== PLUS_EXPR
)
42300 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
42301 NULL_TREE
, tf_warning_or_error
);
42304 lhs
= build_x_binary_op (input_location
, op
,
42307 NULL_TREE
, NULL
, tf_warning_or_error
);
42310 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
42315 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
42316 || op
== MINUS_EXPR
)
42317 return error_mark_node
;
42318 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
42321 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
42323 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
42326 /* Parse the initialization statement of an OpenMP for loop.
42328 Return true if the resulting construct should have an
42329 OMP_CLAUSE_PRIVATE added to it. */
42332 cp_parser_omp_for_loop_init (cp_parser
*parser
,
42333 tree
&this_pre_body
,
42334 releasing_vec
&for_block
,
42340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
42343 tree add_private_clause
= NULL_TREE
;
42345 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
42349 integer-type var = lb
42350 random-access-iterator-type var = lb
42351 pointer-type var = lb
42353 cp_decl_specifier_seq type_specifiers
;
42355 /* First, try to parse as an initialized declaration. See
42356 cp_parser_condition, from whence the bulk of this is copied. */
42358 cp_parser_parse_tentatively (parser
);
42359 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
42360 /*is_declaration=*/true,
42361 /*is_trailing_return=*/false,
42363 if (cp_parser_parse_definitely (parser
))
42365 /* If parsing a type specifier seq succeeded, then this
42366 MUST be a initialized declaration. */
42367 tree asm_specification
, attributes
;
42368 cp_declarator
*declarator
;
42370 declarator
= cp_parser_declarator (parser
,
42371 CP_PARSER_DECLARATOR_NAMED
,
42372 CP_PARSER_FLAGS_NONE
,
42373 /*ctor_dtor_or_conv_p=*/NULL
,
42374 /*parenthesized_p=*/NULL
,
42375 /*member_p=*/false,
42376 /*friend_p=*/false,
42377 /*static_p=*/false);
42378 attributes
= cp_parser_attributes_opt (parser
);
42379 asm_specification
= cp_parser_asm_specification_opt (parser
);
42381 if (declarator
== cp_error_declarator
)
42382 cp_parser_skip_to_end_of_statement (parser
);
42386 tree pushed_scope
, auto_node
;
42388 decl
= start_decl (declarator
, &type_specifiers
,
42389 SD_INITIALIZED
, attributes
,
42390 /*prefix_attributes=*/NULL_TREE
,
42393 auto_node
= type_uses_auto (TREE_TYPE (decl
));
42394 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
42396 if (cp_lexer_next_token_is (parser
->lexer
,
42398 error ("parenthesized initialization is not allowed in "
42399 "OpenMP %<for%> loop");
42401 /* Trigger an error. */
42402 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
42404 init
= error_mark_node
;
42405 cp_parser_skip_to_end_of_statement (parser
);
42407 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
42408 || type_dependent_expression_p (decl
)
42411 bool is_direct_init
, is_non_constant_init
;
42413 init
= cp_parser_initializer (parser
,
42415 &is_non_constant_init
);
42420 = do_auto_deduction (TREE_TYPE (decl
), init
,
42423 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
42424 && !type_dependent_expression_p (decl
))
42428 cp_finish_decl (decl
, init
, !is_non_constant_init
,
42430 LOOKUP_ONLYCONVERTING
);
42432 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
42434 vec_safe_push (for_block
, this_pre_body
);
42439 init
= pop_stmt_list (this_pre_body
);
42440 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
42442 tree_stmt_iterator i
= tsi_start (init
);
42443 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
42444 while (!tsi_end_p (i
))
42446 tree t
= tsi_stmt (i
);
42447 if (TREE_CODE (t
) == DECL_EXPR
42448 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
42451 vec_safe_push (for_block
, t
);
42456 if (tsi_one_before_end_p (i
))
42458 tree t
= tsi_stmt (i
);
42460 free_stmt_list (init
);
42465 this_pre_body
= NULL_TREE
;
42470 cp_lexer_consume_token (parser
->lexer
);
42471 init
= cp_parser_assignment_expression (parser
);
42474 if (TYPE_REF_P (TREE_TYPE (decl
)))
42475 init
= error_mark_node
;
42477 cp_finish_decl (decl
, NULL_TREE
,
42478 /*init_const_expr_p=*/false,
42480 LOOKUP_ONLYCONVERTING
);
42484 pop_scope (pushed_scope
);
42490 /* If parsing a type specifier sequence failed, then
42491 this MUST be a simple expression. */
42492 cp_parser_parse_tentatively (parser
);
42493 decl
= cp_parser_primary_expression (parser
, false, false,
42495 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
42496 if (!cp_parser_error_occurred (parser
)
42498 && (TREE_CODE (decl
) == COMPONENT_REF
42499 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
42501 cp_parser_abort_tentative_parse (parser
);
42502 cp_parser_parse_tentatively (parser
);
42503 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
42504 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
42505 /*check_dependency_p=*/true,
42506 /*template_p=*/NULL
,
42507 /*declarator_p=*/false,
42508 /*optional_p=*/false);
42509 if (name
!= error_mark_node
42510 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
42512 decl
= cp_parser_lookup_name_simple (parser
, name
,
42514 if (TREE_CODE (decl
) == FIELD_DECL
)
42515 add_private_clause
= omp_privatize_field (decl
, false);
42517 cp_parser_abort_tentative_parse (parser
);
42518 cp_parser_parse_tentatively (parser
);
42519 decl
= cp_parser_primary_expression (parser
, false, false,
42522 if (!cp_parser_error_occurred (parser
)
42525 && CLASS_TYPE_P (TREE_TYPE (decl
)))
42529 cp_parser_parse_definitely (parser
);
42530 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
42531 rhs
= cp_parser_assignment_expression (parser
);
42533 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
42536 tf_warning_or_error
));
42537 if (!add_private_clause
)
42538 add_private_clause
= decl
;
42543 cp_parser_abort_tentative_parse (parser
);
42544 init
= cp_parser_expression (parser
);
42547 if (TREE_CODE (init
) == MODIFY_EXPR
42548 || TREE_CODE (init
) == MODOP_EXPR
)
42549 real_decl
= TREE_OPERAND (init
, 0);
42553 return add_private_clause
;
42556 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
42559 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
42560 tree
&decl
, tree
&orig_decl
, tree
&init
,
42561 tree
&orig_init
, tree
&cond
, tree
&incr
)
42563 tree begin
, end
, range_temp_decl
= NULL_TREE
;
42564 tree iter_type
, begin_expr
, end_expr
;
42566 if (processing_template_decl
)
42568 if (check_for_bare_parameter_packs (init
))
42569 init
= error_mark_node
;
42570 if (!type_dependent_expression_p (init
)
42571 /* do_auto_deduction doesn't mess with template init-lists. */
42572 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
42575 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
42577 tree v
= DECL_VALUE_EXPR (decl
);
42578 if (TREE_CODE (v
) == ARRAY_REF
42579 && VAR_P (TREE_OPERAND (v
, 0))
42580 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
42581 d
= TREE_OPERAND (v
, 0);
42583 do_range_for_auto_deduction (d
, init
);
42585 cond
= global_namespace
;
42589 this_pre_body
= pop_stmt_list (this_pre_body
);
42593 init
= mark_lvalue_use (init
);
42595 if (decl
== error_mark_node
|| init
== error_mark_node
)
42596 /* If an error happened previously do nothing or else a lot of
42597 unhelpful errors would be issued. */
42598 begin_expr
= end_expr
= iter_type
= error_mark_node
;
42604 && array_of_runtime_bound_p (TREE_TYPE (init
)))
42605 /* Can't bind a reference to an array of runtime bound. */
42609 range_temp
= build_range_temp (init
);
42610 DECL_NAME (range_temp
) = NULL_TREE
;
42611 pushdecl (range_temp
);
42612 cp_finish_decl (range_temp
, init
,
42613 /*is_constant_init*/false, NULL_TREE
,
42614 LOOKUP_ONLYCONVERTING
);
42615 range_temp_decl
= range_temp
;
42616 range_temp
= convert_from_reference (range_temp
);
42618 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
42619 &begin_expr
, &end_expr
);
42622 tree end_iter_type
= iter_type
;
42623 if (cxx_dialect
>= cxx17
)
42624 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
42625 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
42626 TREE_USED (end
) = 1;
42627 DECL_ARTIFICIAL (end
) = 1;
42629 cp_finish_decl (end
, end_expr
,
42630 /*is_constant_init*/false, NULL_TREE
,
42631 LOOKUP_ONLYCONVERTING
);
42633 /* The new for initialization statement. */
42634 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
42635 TREE_USED (begin
) = 1;
42636 DECL_ARTIFICIAL (begin
) = 1;
42639 if (CLASS_TYPE_P (iter_type
))
42644 begin_expr
= NULL_TREE
;
42646 cp_finish_decl (begin
, begin_expr
,
42647 /*is_constant_init*/false, NULL_TREE
,
42648 LOOKUP_ONLYCONVERTING
);
42650 /* The new for condition. */
42651 if (CLASS_TYPE_P (iter_type
))
42652 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
42654 cond
= build_x_binary_op (input_location
, NE_EXPR
,
42657 NULL_TREE
, NULL
, tf_warning_or_error
);
42659 /* The new increment expression. */
42660 if (CLASS_TYPE_P (iter_type
))
42661 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
42663 incr
= finish_unary_op_expr (input_location
,
42664 PREINCREMENT_EXPR
, begin
,
42665 tf_warning_or_error
);
42671 vec_safe_push (for_block
, this_pre_body
);
42672 this_pre_body
= NULL_TREE
;
42675 tree decomp_first_name
= NULL_TREE
;
42676 unsigned decomp_cnt
= 0;
42677 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
42679 tree v
= DECL_VALUE_EXPR (orig_decl
);
42680 if (TREE_CODE (v
) == ARRAY_REF
42681 && VAR_P (TREE_OPERAND (v
, 0))
42682 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
42684 tree d
= orig_decl
;
42685 orig_decl
= TREE_OPERAND (v
, 0);
42686 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
42687 decomp_first_name
= d
;
42691 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
42694 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
42695 NULL_TREE
, tf_none
);
42696 if (!error_operand_p (t
))
42697 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
42701 tree v
= make_tree_vec (decomp_cnt
+ 3);
42702 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
42703 TREE_VEC_ELT (v
, 1) = end
;
42704 TREE_VEC_ELT (v
, 2) = orig_decl
;
42705 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
42707 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
42708 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
42710 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
42713 /* Helper for cp_parser_omp_for_loop, finalize part of range for
42714 inside of the collapsed body. */
42717 cp_finish_omp_range_for (tree orig
, tree begin
)
42719 gcc_assert (TREE_CODE (orig
) == TREE_LIST
42720 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
42721 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
42722 tree decomp_first_name
= NULL_TREE
;
42723 unsigned int decomp_cnt
= 0;
42725 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
42727 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
42728 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
42729 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
42732 /* The declaration is initialized with *__begin inside the loop body. */
42733 cp_finish_decl (decl
,
42734 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
42735 NULL_TREE
, tf_warning_or_error
),
42736 /*is_constant_init*/false, NULL_TREE
,
42737 LOOKUP_ONLYCONVERTING
);
42738 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
42739 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
42742 /* Return true if next tokens contain a standard attribute that contains
42743 omp::directive (DIRECTIVE). */
42746 cp_parser_omp_section_scan (cp_parser
*parser
, const char *directive
,
42749 size_t n
= cp_parser_skip_attributes_opt (parser
, 1), i
;
42752 for (i
= 5; i
< n
- 4; i
++)
42753 if (cp_lexer_nth_token_is (parser
->lexer
, i
, CPP_NAME
)
42754 && cp_lexer_nth_token_is (parser
->lexer
, i
+ 1, CPP_OPEN_PAREN
)
42755 && cp_lexer_nth_token_is (parser
->lexer
, i
+ 2, CPP_NAME
))
42757 tree first
= cp_lexer_peek_nth_token (parser
->lexer
, i
)->u
.value
;
42758 tree second
= cp_lexer_peek_nth_token (parser
->lexer
, i
+ 2)->u
.value
;
42759 if (strcmp (IDENTIFIER_POINTER (first
), "directive"))
42761 if (strcmp (IDENTIFIER_POINTER (second
), directive
) == 0)
42766 cp_parser_parse_tentatively (parser
);
42767 location_t first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42768 location_t last_loc
42769 = cp_lexer_peek_nth_token (parser
->lexer
, n
- 1)->location
;
42770 location_t middle_loc
= UNKNOWN_LOCATION
;
42771 tree std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
42774 for (tree attr
= std_attrs
; attr
; attr
= TREE_CHAIN (attr
))
42775 if (get_attribute_namespace (attr
) == omp_identifier
42776 && is_attribute_p ("directive", get_attribute_name (attr
)))
42778 for (tree a
= TREE_VALUE (attr
); a
; a
= TREE_CHAIN (a
))
42780 tree d
= TREE_VALUE (a
);
42781 gcc_assert (TREE_CODE (d
) == DEFERRED_PARSE
);
42782 cp_token
*first
= DEFPARSE_TOKENS (d
)->first
;
42784 if (first
->type
== CPP_NAME
42785 && strcmp (IDENTIFIER_POINTER (first
->u
.value
),
42789 if (middle_loc
== UNKNOWN_LOCATION
)
42790 middle_loc
= first
->location
;
42794 if (!seen
|| tentative
)
42796 cp_parser_abort_tentative_parse (parser
);
42799 if (cnt
!= 1 || TREE_CHAIN (std_attrs
))
42801 error_at (make_location (first_loc
, last_loc
, middle_loc
),
42802 "%<[[omp::directive(%s)]]%> must be the only specified "
42803 "attribute on a statement", directive
);
42804 cp_parser_abort_tentative_parse (parser
);
42807 if (!cp_parser_parse_definitely (parser
))
42809 cp_parser_handle_statement_omp_attributes (parser
, std_attrs
);
42813 /* Parse an OpenMP structured block sequence. KIND is the corresponding
42814 separating directive. */
42817 cp_parser_omp_structured_block_sequence (cp_parser
*parser
,
42818 enum pragma_kind kind
)
42820 tree stmt
= begin_omp_structured_block ();
42821 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
42823 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
42826 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
42828 if (token
->type
== CPP_CLOSE_BRACE
42829 || token
->type
== CPP_EOF
42830 || token
->type
== CPP_PRAGMA_EOL
42831 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
)
42832 || (kind
!= PRAGMA_NONE
42833 && cp_parser_pragma_kind (token
) == kind
))
42836 if (kind
!= PRAGMA_NONE
42837 && cp_parser_omp_section_scan (parser
,
42838 kind
== PRAGMA_OMP_SCAN
42839 ? "scan" : "section", false))
42842 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
42845 cp_parser_end_omp_structured_block (parser
, save
);
42846 return finish_omp_structured_block (stmt
);
42853 { structured-block scan-directive structured-block } */
42856 cp_parser_omp_scan_loop_body (cp_parser
*parser
)
42858 tree substmt
, clauses
= NULL_TREE
;
42860 matching_braces braces
;
42861 if (!braces
.require_open (parser
))
42864 substmt
= cp_parser_omp_structured_block_sequence (parser
, PRAGMA_OMP_SCAN
);
42865 substmt
= build2 (OMP_SCAN
, void_type_node
, substmt
, NULL_TREE
);
42866 add_stmt (substmt
);
42868 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
42869 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SCAN
)
42871 enum omp_clause_code clause
= OMP_CLAUSE_ERROR
;
42873 cp_lexer_consume_token (parser
->lexer
);
42875 if (parser
->lexer
->in_omp_attribute_pragma
42876 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42877 cp_lexer_consume_token (parser
->lexer
);
42879 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42881 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42882 const char *p
= IDENTIFIER_POINTER (id
);
42883 if (strcmp (p
, "inclusive") == 0)
42884 clause
= OMP_CLAUSE_INCLUSIVE
;
42885 else if (strcmp (p
, "exclusive") == 0)
42886 clause
= OMP_CLAUSE_EXCLUSIVE
;
42888 if (clause
!= OMP_CLAUSE_ERROR
)
42890 cp_lexer_consume_token (parser
->lexer
);
42891 clauses
= cp_parser_omp_var_list (parser
, clause
, NULL_TREE
);
42894 cp_parser_error (parser
, "expected %<inclusive%> or "
42895 "%<exclusive%> clause");
42897 cp_parser_require_pragma_eol (parser
, tok
);
42900 error ("expected %<#pragma omp scan%>");
42902 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
42903 substmt
= cp_parser_omp_structured_block_sequence (parser
, PRAGMA_NONE
);
42904 substmt
= build2_loc (tok
->location
, OMP_SCAN
, void_type_node
, substmt
,
42906 add_stmt (substmt
);
42908 braces
.require_close (parser
);
42911 /* Parse the restricted form of the for statement allowed by OpenMP. */
42914 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
42915 tree
*cclauses
, bool *if_p
)
42917 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
42919 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
42920 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
42921 location_t loc_first
;
42922 bool collapse_err
= false;
42923 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
42924 releasing_vec for_block
;
42925 auto_vec
<tree
, 4> orig_inits
;
42926 bool tiling
= false;
42927 bool inscan
= false;
42929 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
42930 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
42931 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
42932 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
42935 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
42937 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
42938 && OMP_CLAUSE_ORDERED_EXPR (cl
))
42941 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
42943 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_REDUCTION
42944 && OMP_CLAUSE_REDUCTION_INSCAN (cl
)
42945 && (code
== OMP_SIMD
|| code
== OMP_FOR
))
42948 if (ordered
&& ordered
< collapse
)
42950 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
42951 "%<ordered%> clause parameter is less than %<collapse%>");
42952 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
42953 = build_int_cst (NULL_TREE
, collapse
);
42954 ordered
= collapse
;
42957 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
42958 count
= ordered
? ordered
: collapse
;
42960 declv
= make_tree_vec (count
);
42961 initv
= make_tree_vec (count
);
42962 condv
= make_tree_vec (count
);
42963 incrv
= make_tree_vec (count
);
42964 orig_declv
= NULL_TREE
;
42966 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
42968 for (i
= 0; i
< count
; i
++)
42970 int bracecount
= 0;
42971 tree add_private_clause
= NULL_TREE
;
42974 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
42977 cp_parser_error (parser
, "for statement expected");
42980 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
42982 /* Don't create location wrapper nodes within an OpenMP "for"
42984 auto_suppress_location_wrappers sentinel
;
42986 matching_parens parens
;
42987 if (!parens
.require_open (parser
))
42990 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
42991 this_pre_body
= push_stmt_list ();
42993 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
42995 /* Save tokens so that we can put them back. */
42996 cp_lexer_save_tokens (parser
->lexer
);
42998 /* Look for ':' that is not nested in () or {}. */
43000 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
43001 /*recovering=*/false,
43006 /* Roll back the tokens we skipped. */
43007 cp_lexer_rollback_tokens (parser
->lexer
);
43011 bool saved_colon_corrects_to_scope_p
43012 = parser
->colon_corrects_to_scope_p
;
43014 /* A colon is used in range-based for. */
43015 parser
->colon_corrects_to_scope_p
= false;
43017 /* Parse the declaration. */
43018 cp_parser_simple_declaration (parser
,
43019 /*function_definition_allowed_p=*/
43021 parser
->colon_corrects_to_scope_p
43022 = saved_colon_corrects_to_scope_p
;
43024 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
43026 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
43029 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
43030 orig_decl
, init
, orig_init
,
43037 pre_body
= push_stmt_list ();
43039 add_stmt (this_pre_body
);
43040 pre_body
= pop_stmt_list (pre_body
);
43043 pre_body
= this_pre_body
;
43047 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
43048 "%<ordered%> clause with parameter on "
43049 "range-based %<for%> loop");
43051 goto parse_close_paren
;
43056 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
43057 init
, orig_init
, decl
, real_decl
);
43059 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
43062 this_pre_body
= pop_stmt_list (this_pre_body
);
43066 pre_body
= push_stmt_list ();
43068 add_stmt (this_pre_body
);
43069 pre_body
= pop_stmt_list (pre_body
);
43072 pre_body
= this_pre_body
;
43077 if (cclauses
!= NULL
43078 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
43079 && real_decl
!= NULL_TREE
43080 && code
!= OMP_LOOP
)
43083 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
43084 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
43085 && OMP_CLAUSE_DECL (*c
) == real_decl
)
43087 error_at (loc
, "iteration variable %qD"
43088 " should not be firstprivate", real_decl
);
43089 *c
= OMP_CLAUSE_CHAIN (*c
);
43091 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
43092 && OMP_CLAUSE_DECL (*c
) == real_decl
)
43094 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
43096 *c
= OMP_CLAUSE_CHAIN (*c
);
43097 if (code
== OMP_SIMD
)
43099 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
43100 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
43104 OMP_CLAUSE_CHAIN (l
) = clauses
;
43107 add_private_clause
= NULL_TREE
;
43111 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
43112 && OMP_CLAUSE_DECL (*c
) == real_decl
)
43113 add_private_clause
= NULL_TREE
;
43114 c
= &OMP_CLAUSE_CHAIN (*c
);
43118 if (add_private_clause
)
43121 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
43123 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
43124 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
43125 && OMP_CLAUSE_DECL (c
) == decl
)
43127 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
43128 && OMP_CLAUSE_DECL (c
) == decl
)
43129 error_at (loc
, "iteration variable %qD "
43130 "should not be firstprivate",
43132 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
43133 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
43134 && OMP_CLAUSE_DECL (c
) == decl
)
43135 error_at (loc
, "iteration variable %qD should not be reduction",
43140 if ((code
== OMP_SIMD
&& collapse
!= 1) || code
== OMP_LOOP
)
43141 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
43142 else if (code
!= OMP_SIMD
)
43143 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
43145 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
43146 OMP_CLAUSE_DECL (c
) = add_private_clause
;
43147 c
= finish_omp_clauses (c
, C_ORT_OMP
);
43150 OMP_CLAUSE_CHAIN (c
) = clauses
;
43152 /* For linear, signal that we need to fill up
43153 the so far unknown linear step. */
43154 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
43155 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
43161 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
43162 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
43163 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
43166 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
43168 /* If decl is an iterator, preserve the operator on decl
43169 until finish_omp_for. */
43171 && ((processing_template_decl
43172 && (TREE_TYPE (real_decl
) == NULL_TREE
43173 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
43174 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
43175 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
43177 incr
= cp_parser_expression (parser
);
43178 protected_set_expr_location_if_unset (incr
, input_location
);
43182 if (!parens
.require_close (parser
))
43183 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
43184 /*or_comma=*/false,
43185 /*consume_paren=*/true);
43187 TREE_VEC_ELT (declv
, i
) = decl
;
43188 TREE_VEC_ELT (initv
, i
) = init
;
43189 TREE_VEC_ELT (condv
, i
) = cond
;
43190 TREE_VEC_ELT (incrv
, i
) = incr
;
43193 orig_inits
.safe_grow_cleared (i
+ 1, true);
43194 orig_inits
[i
] = orig_init
;
43199 orig_declv
= copy_node (declv
);
43200 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
43202 else if (orig_declv
)
43203 TREE_VEC_ELT (orig_declv
, i
) = decl
;
43205 if (i
== count
- 1)
43208 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
43209 in between the collapsed for loops to be still considered perfectly
43210 nested. Hopefully the final version clarifies this.
43211 For now handle (multiple) {'s and empty statements. */
43212 cp_parser_parse_tentatively (parser
);
43215 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
43217 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
43219 cp_lexer_consume_token (parser
->lexer
);
43222 else if (bracecount
43223 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
43224 cp_lexer_consume_token (parser
->lexer
);
43227 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43228 error_at (loc
, "not enough for loops to collapse");
43229 collapse_err
= true;
43230 cp_parser_abort_tentative_parse (parser
);
43238 cp_parser_parse_definitely (parser
);
43239 nbraces
+= bracecount
;
43246 /* Note that we saved the original contents of this flag when we entered
43247 the structured block, and so we don't need to re-save it here. */
43248 parser
->in_statement
= IN_OMP_FOR
;
43250 /* Note that the grammar doesn't call for a structured block here,
43251 though the loop as a whole is a structured block. */
43254 body
= begin_omp_structured_block ();
43255 for (i
= 0; i
< count
; i
++)
43256 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
43257 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
43258 TREE_VEC_ELT (declv
, i
));
43261 body
= push_stmt_list ();
43263 cp_parser_omp_scan_loop_body (parser
);
43265 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
43267 body
= finish_omp_structured_block (body
);
43269 body
= pop_stmt_list (body
);
43271 if (declv
== NULL_TREE
)
43274 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
43275 incrv
, body
, pre_body
, &orig_inits
, clauses
);
43279 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
43281 cp_lexer_consume_token (parser
->lexer
);
43284 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
43285 cp_lexer_consume_token (parser
->lexer
);
43290 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
43291 "collapsed loops not perfectly nested");
43293 collapse_err
= true;
43294 cp_parser_statement_seq_opt (parser
, NULL
);
43295 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
43300 while (!for_block
->is_empty ())
43302 tree t
= for_block
->pop ();
43303 if (TREE_CODE (t
) == STATEMENT_LIST
)
43304 add_stmt (pop_stmt_list (t
));
43312 /* Helper function for OpenMP parsing, split clauses and call
43313 finish_omp_clauses on each of the set of clauses afterwards. */
43316 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
43317 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
43320 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
43321 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
43323 cclauses
[i
] = finish_omp_clauses (cclauses
[i
],
43324 i
== C_OMP_CLAUSE_SPLIT_TARGET
43325 ? C_ORT_OMP_TARGET
: C_ORT_OMP
);
43329 #pragma omp loop loop-clause[optseq] new-line
43332 #define OMP_LOOP_CLAUSE_MASK \
43333 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
43338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43341 cp_parser_omp_loop (cp_parser
*parser
, cp_token
*pragma_tok
,
43342 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43345 tree clauses
, sb
, ret
;
43347 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43349 strcat (p_name
, " loop");
43350 mask
|= OMP_LOOP_CLAUSE_MASK
;
43352 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43356 cp_omp_split_clauses (loc
, OMP_LOOP
, mask
, clauses
, cclauses
);
43357 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_LOOP
];
43360 keep_next_level (true);
43361 sb
= begin_omp_structured_block ();
43362 save
= cp_parser_begin_omp_structured_block (parser
);
43364 ret
= cp_parser_omp_for_loop (parser
, OMP_LOOP
, clauses
, cclauses
, if_p
);
43366 cp_parser_end_omp_structured_block (parser
, save
);
43367 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
43373 #pragma omp simd simd-clause[optseq] new-line
43376 #define OMP_SIMD_CLAUSE_MASK \
43377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
43378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
43379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
43380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
43381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
43387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43390 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
43391 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43394 tree clauses
, sb
, ret
;
43396 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43398 strcat (p_name
, " simd");
43399 mask
|= OMP_SIMD_CLAUSE_MASK
;
43401 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43405 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
43406 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
43409 keep_next_level (true);
43410 sb
= begin_omp_structured_block ();
43411 save
= cp_parser_begin_omp_structured_block (parser
);
43413 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
43415 cp_parser_end_omp_structured_block (parser
, save
);
43416 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
43422 #pragma omp for for-clause[optseq] new-line
43426 #pragma omp for simd for-simd-clause[optseq] new-line
43429 #define OMP_FOR_CLAUSE_MASK \
43430 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
43434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
43436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
43437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
43438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43443 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
43444 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43447 tree clauses
, sb
, ret
;
43449 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43451 strcat (p_name
, " for");
43452 mask
|= OMP_FOR_CLAUSE_MASK
;
43453 /* parallel for{, simd} disallows nowait clause, but for
43454 target {teams distribute ,}parallel for{, simd} it should be accepted. */
43455 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
43456 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
43457 /* Composite distribute parallel for{, simd} disallows ordered clause. */
43458 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
43459 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
43461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43463 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43464 const char *p
= IDENTIFIER_POINTER (id
);
43466 if (strcmp (p
, "simd") == 0)
43468 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43469 if (cclauses
== NULL
)
43470 cclauses
= cclauses_buf
;
43472 cp_lexer_consume_token (parser
->lexer
);
43473 if (!flag_openmp
) /* flag_openmp_simd */
43474 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
43476 sb
= begin_omp_structured_block ();
43477 save
= cp_parser_begin_omp_structured_block (parser
);
43478 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
43480 cp_parser_end_omp_structured_block (parser
, save
);
43481 tree body
= finish_omp_structured_block (sb
);
43484 ret
= make_node (OMP_FOR
);
43485 TREE_TYPE (ret
) = void_type_node
;
43486 OMP_FOR_BODY (ret
) = body
;
43487 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
43488 SET_EXPR_LOCATION (ret
, loc
);
43493 if (!flag_openmp
) /* flag_openmp_simd */
43495 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43499 /* Composite distribute parallel for disallows linear clause. */
43500 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
43501 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
43503 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43507 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
43508 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
43511 keep_next_level (true);
43512 sb
= begin_omp_structured_block ();
43513 save
= cp_parser_begin_omp_structured_block (parser
);
43515 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
43517 cp_parser_end_omp_structured_block (parser
, save
);
43518 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
43523 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
43524 omp_clause_mask
, tree
*, bool *);
43527 # pragma omp master new-line
43528 structured-block */
43531 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
43532 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43535 tree clauses
, sb
, ret
;
43537 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43539 strcat (p_name
, " master");
43541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43543 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43544 const char *p
= IDENTIFIER_POINTER (id
);
43546 if (strcmp (p
, "taskloop") == 0)
43548 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43549 if (cclauses
== NULL
)
43550 cclauses
= cclauses_buf
;
43552 cp_lexer_consume_token (parser
->lexer
);
43553 if (!flag_openmp
) /* flag_openmp_simd */
43554 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
43556 sb
= begin_omp_structured_block ();
43557 save
= cp_parser_begin_omp_structured_block (parser
);
43558 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
43560 cp_parser_end_omp_structured_block (parser
, save
);
43561 tree body
= finish_omp_structured_block (sb
);
43564 ret
= c_finish_omp_master (loc
, body
);
43565 OMP_MASTER_COMBINED (ret
) = 1;
43569 if (!flag_openmp
) /* flag_openmp_simd */
43571 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43577 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43579 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
43582 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43584 return c_finish_omp_master (loc
,
43585 cp_parser_omp_structured_block (parser
, if_p
));
43589 # pragma omp masked masked-clauses new-line
43590 structured-block */
43592 #define OMP_MASKED_CLAUSE_MASK \
43593 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
43596 cp_parser_omp_masked (cp_parser
*parser
, cp_token
*pragma_tok
,
43597 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43600 tree clauses
, sb
, ret
;
43602 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43604 strcat (p_name
, " masked");
43605 mask
|= OMP_MASKED_CLAUSE_MASK
;
43607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43609 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43610 const char *p
= IDENTIFIER_POINTER (id
);
43612 if (strcmp (p
, "taskloop") == 0)
43614 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43615 if (cclauses
== NULL
)
43616 cclauses
= cclauses_buf
;
43618 cp_lexer_consume_token (parser
->lexer
);
43619 if (!flag_openmp
) /* flag_openmp_simd */
43620 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
43622 sb
= begin_omp_structured_block ();
43623 save
= cp_parser_begin_omp_structured_block (parser
);
43624 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
43626 cp_parser_end_omp_structured_block (parser
, save
);
43627 tree body
= finish_omp_structured_block (sb
);
43630 ret
= c_finish_omp_masked (loc
, body
,
43631 cclauses
[C_OMP_CLAUSE_SPLIT_MASKED
]);
43632 OMP_MASKED_COMBINED (ret
) = 1;
43636 if (!flag_openmp
) /* flag_openmp_simd */
43638 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43642 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43646 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
43647 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_MASKED
];
43650 return c_finish_omp_masked (loc
,
43651 cp_parser_omp_structured_block (parser
, if_p
),
43656 # pragma omp ordered new-line
43660 # pragma omp ordered ordered-clauses new-line
43661 structured-block */
43663 #define OMP_ORDERED_CLAUSE_MASK \
43664 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
43665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
43667 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
43668 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
43672 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
43673 enum pragma_context context
, bool *if_p
)
43675 location_t loc
= pragma_tok
->location
;
43678 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
43679 if (parser
->lexer
->in_omp_attribute_pragma
43680 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
43683 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_NAME
))
43685 tree id
= cp_lexer_peek_nth_token (parser
->lexer
, n
)->u
.value
;
43686 const char *p
= IDENTIFIER_POINTER (id
);
43688 if (strcmp (p
, "depend") == 0 || strcmp (p
, "doacross") == 0)
43690 if (!flag_openmp
) /* flag_openmp_simd */
43692 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43695 if (context
== pragma_stmt
)
43697 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
43698 "%qs clause may only be used in compound "
43700 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43704 = cp_parser_omp_all_clauses (parser
,
43705 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
43706 "#pragma omp ordered", pragma_tok
);
43707 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
43713 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
43714 "#pragma omp ordered", pragma_tok
);
43716 if (!flag_openmp
/* flag_openmp_simd */
43717 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
43720 c_finish_omp_ordered (loc
, clauses
,
43721 cp_parser_omp_structured_block (parser
, if_p
));
43728 { section-sequence }
43731 section-directive[opt] structured-block
43732 section-sequence section-directive structured-block */
43735 cp_parser_omp_sections_scope (cp_parser
*parser
)
43737 tree stmt
, substmt
;
43738 bool error_suppress
= false;
43741 matching_braces braces
;
43742 if (!braces
.require_open (parser
))
43745 stmt
= push_stmt_list ();
43747 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
43748 != PRAGMA_OMP_SECTION
43749 && !cp_parser_omp_section_scan (parser
, "section", true))
43751 substmt
= cp_parser_omp_structured_block_sequence (parser
,
43752 PRAGMA_OMP_SECTION
);
43753 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
43754 add_stmt (substmt
);
43759 tok
= cp_lexer_peek_token (parser
->lexer
);
43760 if (tok
->type
== CPP_CLOSE_BRACE
)
43762 if (tok
->type
== CPP_EOF
)
43765 if (cp_parser_omp_section_scan (parser
, "section", false))
43766 tok
= cp_lexer_peek_token (parser
->lexer
);
43767 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
43769 cp_lexer_consume_token (parser
->lexer
);
43770 cp_parser_require_pragma_eol (parser
, tok
);
43771 error_suppress
= false;
43773 else if (!error_suppress
)
43775 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
43776 error_suppress
= true;
43779 substmt
= cp_parser_omp_structured_block_sequence (parser
,
43780 PRAGMA_OMP_SECTION
);
43781 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
43782 add_stmt (substmt
);
43784 braces
.require_close (parser
);
43786 substmt
= pop_stmt_list (stmt
);
43788 stmt
= make_node (OMP_SECTIONS
);
43789 TREE_TYPE (stmt
) = void_type_node
;
43790 OMP_SECTIONS_BODY (stmt
) = substmt
;
43797 # pragma omp sections sections-clause[optseq] newline
43800 #define OMP_SECTIONS_CLAUSE_MASK \
43801 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43809 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
43810 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
43813 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43815 strcat (p_name
, " sections");
43816 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
43818 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
43820 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43824 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
43825 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
43828 ret
= cp_parser_omp_sections_scope (parser
);
43830 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
43836 # pragma omp parallel parallel-clause[optseq] new-line
43838 # pragma omp parallel for parallel-for-clause[optseq] new-line
43840 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
43844 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
43845 structured-block */
43847 #define OMP_PARALLEL_CLAUSE_MASK \
43848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
43854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
43856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
43860 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
43861 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43864 tree stmt
, clauses
, block
;
43866 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43868 strcat (p_name
, " parallel");
43869 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
43870 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
43871 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
43872 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
43873 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
43875 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
43877 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43878 if (cclauses
== NULL
)
43879 cclauses
= cclauses_buf
;
43881 cp_lexer_consume_token (parser
->lexer
);
43882 if (!flag_openmp
) /* flag_openmp_simd */
43883 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
43885 block
= begin_omp_parallel ();
43886 save
= cp_parser_begin_omp_structured_block (parser
);
43887 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
43889 cp_parser_end_omp_structured_block (parser
, save
);
43890 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
43892 if (ret
== NULL_TREE
)
43894 OMP_PARALLEL_COMBINED (stmt
) = 1;
43897 /* When combined with distribute, parallel has to be followed by for.
43898 #pragma omp target parallel is allowed though. */
43900 && (mask
& (OMP_CLAUSE_MASK_1
43901 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
43903 error_at (loc
, "expected %<for%> after %qs", p_name
);
43904 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43907 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43909 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43910 const char *p
= IDENTIFIER_POINTER (id
);
43911 if (cclauses
== NULL
&& strcmp (p
, "masked") == 0)
43913 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43914 cclauses
= cclauses_buf
;
43916 cp_lexer_consume_token (parser
->lexer
);
43917 if (!flag_openmp
) /* flag_openmp_simd */
43918 return cp_parser_omp_masked (parser
, pragma_tok
, p_name
, mask
,
43920 block
= begin_omp_parallel ();
43921 save
= cp_parser_begin_omp_structured_block (parser
);
43922 tree ret
= cp_parser_omp_masked (parser
, pragma_tok
, p_name
, mask
,
43924 cp_parser_end_omp_structured_block (parser
, save
);
43925 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
43927 if (ret
== NULL_TREE
)
43929 /* masked does have just filter clause, but during gimplification
43930 isn't represented by a gimplification omp context, so for
43931 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
43933 #pragma omp parallel masked
43934 #pragma omp taskloop simd lastprivate (x)
43935 isn't confused with
43936 #pragma omp parallel masked taskloop simd lastprivate (x) */
43937 if (OMP_MASKED_COMBINED (ret
))
43938 OMP_PARALLEL_COMBINED (stmt
) = 1;
43941 else if (cclauses
== NULL
&& strcmp (p
, "master") == 0)
43943 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43944 cclauses
= cclauses_buf
;
43946 cp_lexer_consume_token (parser
->lexer
);
43947 if (!flag_openmp
) /* flag_openmp_simd */
43948 return cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
43950 block
= begin_omp_parallel ();
43951 save
= cp_parser_begin_omp_structured_block (parser
);
43952 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
43954 cp_parser_end_omp_structured_block (parser
, save
);
43955 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
43957 if (ret
== NULL_TREE
)
43959 /* master doesn't have any clauses and during gimplification
43960 isn't represented by a gimplification omp context, so for
43961 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
43963 #pragma omp parallel master
43964 #pragma omp taskloop simd lastprivate (x)
43965 isn't confused with
43966 #pragma omp parallel master taskloop simd lastprivate (x) */
43967 if (OMP_MASTER_COMBINED (ret
))
43968 OMP_PARALLEL_COMBINED (stmt
) = 1;
43971 else if (strcmp (p
, "loop") == 0)
43973 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43974 if (cclauses
== NULL
)
43975 cclauses
= cclauses_buf
;
43977 cp_lexer_consume_token (parser
->lexer
);
43978 if (!flag_openmp
) /* flag_openmp_simd */
43979 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
43981 block
= begin_omp_parallel ();
43982 save
= cp_parser_begin_omp_structured_block (parser
);
43983 tree ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
43985 cp_parser_end_omp_structured_block (parser
, save
);
43986 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
43988 if (ret
== NULL_TREE
)
43990 OMP_PARALLEL_COMBINED (stmt
) = 1;
43993 else if (!flag_openmp
) /* flag_openmp_simd */
43995 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43998 else if (cclauses
== NULL
&& strcmp (p
, "sections") == 0)
44000 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
44001 cclauses
= cclauses_buf
;
44003 cp_lexer_consume_token (parser
->lexer
);
44004 block
= begin_omp_parallel ();
44005 save
= cp_parser_begin_omp_structured_block (parser
);
44006 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
44007 cp_parser_end_omp_structured_block (parser
, save
);
44008 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
44010 OMP_PARALLEL_COMBINED (stmt
) = 1;
44014 else if (!flag_openmp
) /* flag_openmp_simd */
44016 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44020 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
44024 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
44025 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
44028 block
= begin_omp_parallel ();
44029 save
= cp_parser_begin_omp_structured_block (parser
);
44030 parser
->omp_attrs_forbidden_p
= true;
44031 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
44032 cp_parser_end_omp_structured_block (parser
, save
);
44033 stmt
= finish_omp_parallel (clauses
, block
);
44038 # pragma omp single single-clause[optseq] new-line
44039 structured-block */
44041 #define OMP_SINGLE_CLAUSE_MASK \
44042 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
44045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44049 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
44051 tree stmt
= make_node (OMP_SINGLE
);
44052 TREE_TYPE (stmt
) = void_type_node
;
44053 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44055 OMP_SINGLE_CLAUSES (stmt
)
44056 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
44057 "#pragma omp single", pragma_tok
);
44058 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
44060 return add_stmt (stmt
);
44064 # pragma omp scope scope-clause[optseq] new-line
44065 structured-block */
44067 #define OMP_SCOPE_CLAUSE_MASK \
44068 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44075 cp_parser_omp_scope (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
44077 tree stmt
= make_node (OMP_SCOPE
);
44078 TREE_TYPE (stmt
) = void_type_node
;
44079 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44081 OMP_SCOPE_CLAUSES (stmt
)
44082 = cp_parser_omp_all_clauses (parser
, OMP_SCOPE_CLAUSE_MASK
,
44083 "#pragma omp scope", pragma_tok
);
44084 OMP_SCOPE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
44086 return add_stmt (stmt
);
44090 # pragma omp task task-clause[optseq] new-line
44091 structured-block */
44093 #define OMP_TASK_CLAUSE_MASK \
44094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
44096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
44097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
44101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
44102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
44104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
44107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
44110 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
44112 tree clauses
, block
;
44115 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
44116 "#pragma omp task", pragma_tok
);
44117 block
= begin_omp_task ();
44118 save
= cp_parser_begin_omp_structured_block (parser
);
44119 parser
->omp_attrs_forbidden_p
= true;
44120 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
44121 cp_parser_end_omp_structured_block (parser
, save
);
44122 return finish_omp_task (clauses
, block
);
44126 # pragma omp taskwait new-line
44129 # pragma omp taskwait taskwait-clause[opt] new-line */
44131 #define OMP_TASKWAIT_CLAUSE_MASK \
44132 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44136 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
44139 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
44140 "#pragma omp taskwait", pragma_tok
);
44144 tree stmt
= make_node (OMP_TASK
);
44145 TREE_TYPE (stmt
) = void_node
;
44146 OMP_TASK_CLAUSES (stmt
) = clauses
;
44147 OMP_TASK_BODY (stmt
) = NULL_TREE
;
44148 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44152 finish_omp_taskwait ();
44156 # pragma omp taskyield new-line */
44159 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
44161 cp_parser_require_pragma_eol (parser
, pragma_tok
);
44162 finish_omp_taskyield ();
44166 # pragma omp taskgroup new-line
44170 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
44172 #define OMP_TASKGROUP_CLAUSE_MASK \
44173 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
44177 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
44180 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
44181 "#pragma omp taskgroup", pragma_tok
);
44182 return c_finish_omp_taskgroup (input_location
,
44183 cp_parser_omp_structured_block (parser
,
44190 # pragma omp threadprivate (variable-list) */
44193 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
44197 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
44198 cp_parser_require_pragma_eol (parser
, pragma_tok
);
44200 finish_omp_threadprivate (vars
);
44204 # pragma omp cancel cancel-clause[optseq] new-line */
44206 #define OMP_CANCEL_CLAUSE_MASK \
44207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
44208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
44209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
44210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
44211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
44214 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
44216 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
44217 "#pragma omp cancel", pragma_tok
);
44218 finish_omp_cancel (clauses
);
44222 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
44224 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
44225 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
44226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
44227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
44228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
44231 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
44232 enum pragma_context context
)
44235 bool point_seen
= false;
44237 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
44239 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
44240 const char *p
= IDENTIFIER_POINTER (id
);
44242 if (strcmp (p
, "point") == 0)
44244 cp_lexer_consume_token (parser
->lexer
);
44250 cp_parser_error (parser
, "expected %<point%>");
44251 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44255 if (context
!= pragma_compound
)
44257 if (context
== pragma_stmt
)
44258 error_at (pragma_tok
->location
,
44259 "%<#pragma %s%> may only be used in compound statements",
44260 "omp cancellation point");
44262 cp_parser_error (parser
, "expected declaration specifiers");
44263 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44267 clauses
= cp_parser_omp_all_clauses (parser
,
44268 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
44269 "#pragma omp cancellation point",
44271 finish_omp_cancellation_point (clauses
);
44276 #pragma omp distribute distribute-clause[optseq] new-line
44279 #define OMP_DISTRIBUTE_CLAUSE_MASK \
44280 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
44284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44289 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
44290 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
44293 tree clauses
, sb
, ret
;
44295 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
44297 strcat (p_name
, " distribute");
44298 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
44300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
44302 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
44303 const char *p
= IDENTIFIER_POINTER (id
);
44305 bool parallel
= false;
44307 if (strcmp (p
, "simd") == 0)
44310 parallel
= strcmp (p
, "parallel") == 0;
44311 if (parallel
|| simd
)
44313 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
44314 if (cclauses
== NULL
)
44315 cclauses
= cclauses_buf
;
44316 cp_lexer_consume_token (parser
->lexer
);
44317 if (!flag_openmp
) /* flag_openmp_simd */
44320 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
44323 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
44326 sb
= begin_omp_structured_block ();
44327 save
= cp_parser_begin_omp_structured_block (parser
);
44329 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
44332 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
44334 cp_parser_end_omp_structured_block (parser
, save
);
44335 tree body
= finish_omp_structured_block (sb
);
44338 ret
= make_node (OMP_DISTRIBUTE
);
44339 TREE_TYPE (ret
) = void_type_node
;
44340 OMP_FOR_BODY (ret
) = body
;
44341 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
44342 SET_EXPR_LOCATION (ret
, loc
);
44347 if (!flag_openmp
) /* flag_openmp_simd */
44349 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44353 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
44357 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
44358 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
44361 keep_next_level (true);
44362 sb
= begin_omp_structured_block ();
44363 save
= cp_parser_begin_omp_structured_block (parser
);
44365 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
44367 cp_parser_end_omp_structured_block (parser
, save
);
44368 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
44374 # pragma omp teams teams-clause[optseq] new-line
44375 structured-block */
44377 #define OMP_TEAMS_CLAUSE_MASK \
44378 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
44383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
44384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
44388 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
44389 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
44392 tree clauses
, sb
, ret
;
44394 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
44396 strcat (p_name
, " teams");
44397 mask
|= OMP_TEAMS_CLAUSE_MASK
;
44399 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
44401 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
44402 const char *p
= IDENTIFIER_POINTER (id
);
44403 if (strcmp (p
, "distribute") == 0)
44405 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
44406 if (cclauses
== NULL
)
44407 cclauses
= cclauses_buf
;
44409 cp_lexer_consume_token (parser
->lexer
);
44410 if (!flag_openmp
) /* flag_openmp_simd */
44411 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
44413 keep_next_level (true);
44414 sb
= begin_omp_structured_block ();
44415 save
= cp_parser_begin_omp_structured_block (parser
);
44416 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
44418 cp_parser_end_omp_structured_block (parser
, save
);
44419 tree body
= finish_omp_structured_block (sb
);
44422 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
44423 ret
= make_node (OMP_TEAMS
);
44424 TREE_TYPE (ret
) = void_type_node
;
44425 OMP_TEAMS_CLAUSES (ret
) = clauses
;
44426 OMP_TEAMS_BODY (ret
) = body
;
44427 OMP_TEAMS_COMBINED (ret
) = 1;
44428 SET_EXPR_LOCATION (ret
, loc
);
44429 return add_stmt (ret
);
44431 else if (strcmp (p
, "loop") == 0)
44433 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
44434 if (cclauses
== NULL
)
44435 cclauses
= cclauses_buf
;
44437 cp_lexer_consume_token (parser
->lexer
);
44438 if (!flag_openmp
) /* flag_openmp_simd */
44439 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
44441 keep_next_level (true);
44442 sb
= begin_omp_structured_block ();
44443 save
= cp_parser_begin_omp_structured_block (parser
);
44444 ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
44446 cp_parser_end_omp_structured_block (parser
, save
);
44447 tree body
= finish_omp_structured_block (sb
);
44450 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
44451 ret
= make_node (OMP_TEAMS
);
44452 TREE_TYPE (ret
) = void_type_node
;
44453 OMP_TEAMS_CLAUSES (ret
) = clauses
;
44454 OMP_TEAMS_BODY (ret
) = body
;
44455 OMP_TEAMS_COMBINED (ret
) = 1;
44456 SET_EXPR_LOCATION (ret
, loc
);
44457 return add_stmt (ret
);
44460 if (!flag_openmp
) /* flag_openmp_simd */
44462 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44466 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
44470 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
44471 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
44474 tree stmt
= make_node (OMP_TEAMS
);
44475 TREE_TYPE (stmt
) = void_type_node
;
44476 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
44477 keep_next_level (true);
44478 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
44479 SET_EXPR_LOCATION (stmt
, loc
);
44481 return add_stmt (stmt
);
44485 # pragma omp target data target-data-clause[optseq] new-line
44486 structured-block */
44488 #define OMP_TARGET_DATA_CLAUSE_MASK \
44489 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
44493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
44496 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
44500 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
44503 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
44504 "#pragma omp target data", pragma_tok
);
44505 c_omp_adjust_map_clauses (clauses
, false);
44507 for (tree
*pc
= &clauses
; *pc
;)
44509 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
44510 switch (OMP_CLAUSE_MAP_KIND (*pc
))
44513 case GOMP_MAP_ALWAYS_TO
:
44514 case GOMP_MAP_FROM
:
44515 case GOMP_MAP_ALWAYS_FROM
:
44516 case GOMP_MAP_TOFROM
:
44517 case GOMP_MAP_ALWAYS_TOFROM
:
44518 case GOMP_MAP_ALLOC
:
44521 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
44522 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
44523 case GOMP_MAP_ALWAYS_POINTER
:
44524 case GOMP_MAP_ATTACH_DETACH
:
44528 error_at (OMP_CLAUSE_LOCATION (*pc
),
44529 "%<#pragma omp target data%> with map-type other "
44530 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
44531 "on %<map%> clause");
44532 *pc
= OMP_CLAUSE_CHAIN (*pc
);
44535 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
44536 || OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_ADDR
)
44538 pc
= &OMP_CLAUSE_CHAIN (*pc
);
44544 error_at (pragma_tok
->location
,
44545 "%<#pragma omp target data%> must contain at least "
44546 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
44551 tree stmt
= make_node (OMP_TARGET_DATA
);
44552 TREE_TYPE (stmt
) = void_type_node
;
44553 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
44555 keep_next_level (true);
44556 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
44558 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44559 return add_stmt (stmt
);
44563 # pragma omp target enter data target-enter-data-clause[optseq] new-line
44564 structured-block */
44566 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
44567 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44574 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
44575 enum pragma_context context
)
44577 bool data_seen
= false;
44578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
44580 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
44581 const char *p
= IDENTIFIER_POINTER (id
);
44583 if (strcmp (p
, "data") == 0)
44585 cp_lexer_consume_token (parser
->lexer
);
44591 cp_parser_error (parser
, "expected %<data%>");
44592 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44596 if (context
== pragma_stmt
)
44598 error_at (pragma_tok
->location
,
44599 "%<#pragma %s%> may only be used in compound statements",
44600 "omp target enter data");
44601 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44607 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
44610 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
44611 "#pragma omp target enter data", pragma_tok
);
44612 c_omp_adjust_map_clauses (clauses
, false);
44614 for (tree
*pc
= &clauses
; *pc
;)
44616 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
44617 switch (OMP_CLAUSE_MAP_KIND (*pc
))
44620 case GOMP_MAP_ALWAYS_TO
:
44621 case GOMP_MAP_ALLOC
:
44624 case GOMP_MAP_TOFROM
:
44625 OMP_CLAUSE_SET_MAP_KIND (*pc
, GOMP_MAP_TO
);
44628 case GOMP_MAP_ALWAYS_TOFROM
:
44629 OMP_CLAUSE_SET_MAP_KIND (*pc
, GOMP_MAP_ALWAYS_TO
);
44632 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
44633 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
44634 case GOMP_MAP_ALWAYS_POINTER
:
44635 case GOMP_MAP_ATTACH_DETACH
:
44639 error_at (OMP_CLAUSE_LOCATION (*pc
),
44640 "%<#pragma omp target enter data%> with map-type other "
44641 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
44642 *pc
= OMP_CLAUSE_CHAIN (*pc
);
44645 pc
= &OMP_CLAUSE_CHAIN (*pc
);
44651 error_at (pragma_tok
->location
,
44652 "%<#pragma omp target enter data%> must contain at least "
44653 "one %<map%> clause");
44657 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
44658 TREE_TYPE (stmt
) = void_type_node
;
44659 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
44660 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44666 # pragma omp target exit data target-enter-data-clause[optseq] new-line
44667 structured-block */
44669 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
44670 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44677 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
44678 enum pragma_context context
)
44680 bool data_seen
= false;
44681 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
44683 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
44684 const char *p
= IDENTIFIER_POINTER (id
);
44686 if (strcmp (p
, "data") == 0)
44688 cp_lexer_consume_token (parser
->lexer
);
44694 cp_parser_error (parser
, "expected %<data%>");
44695 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44699 if (context
== pragma_stmt
)
44701 error_at (pragma_tok
->location
,
44702 "%<#pragma %s%> may only be used in compound statements",
44703 "omp target exit data");
44704 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44710 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
44713 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
44714 "#pragma omp target exit data", pragma_tok
);
44715 c_omp_adjust_map_clauses (clauses
, false);
44717 for (tree
*pc
= &clauses
; *pc
;)
44719 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
44720 switch (OMP_CLAUSE_MAP_KIND (*pc
))
44722 case GOMP_MAP_FROM
:
44723 case GOMP_MAP_ALWAYS_FROM
:
44724 case GOMP_MAP_RELEASE
:
44725 case GOMP_MAP_DELETE
:
44728 case GOMP_MAP_TOFROM
:
44729 OMP_CLAUSE_SET_MAP_KIND (*pc
, GOMP_MAP_FROM
);
44732 case GOMP_MAP_ALWAYS_TOFROM
:
44733 OMP_CLAUSE_SET_MAP_KIND (*pc
, GOMP_MAP_ALWAYS_FROM
);
44736 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
44737 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
44738 case GOMP_MAP_ALWAYS_POINTER
:
44739 case GOMP_MAP_ATTACH_DETACH
:
44743 error_at (OMP_CLAUSE_LOCATION (*pc
),
44744 "%<#pragma omp target exit data%> with map-type other "
44745 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
44746 "on %<map%> clause");
44747 *pc
= OMP_CLAUSE_CHAIN (*pc
);
44750 pc
= &OMP_CLAUSE_CHAIN (*pc
);
44756 error_at (pragma_tok
->location
,
44757 "%<#pragma omp target exit data%> must contain at least "
44758 "one %<map%> clause");
44762 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
44763 TREE_TYPE (stmt
) = void_type_node
;
44764 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
44765 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44771 # pragma omp target update target-update-clause[optseq] new-line */
44773 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
44774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
44775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
44776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44782 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
44783 enum pragma_context context
)
44785 if (context
== pragma_stmt
)
44787 error_at (pragma_tok
->location
,
44788 "%<#pragma %s%> may only be used in compound statements",
44789 "omp target update");
44790 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44795 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
44796 "#pragma omp target update", pragma_tok
);
44797 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
44798 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
44800 error_at (pragma_tok
->location
,
44801 "%<#pragma omp target update%> must contain at least one "
44802 "%<from%> or %<to%> clauses");
44808 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
44810 tree stmt
= make_node (OMP_TARGET_UPDATE
);
44811 TREE_TYPE (stmt
) = void_type_node
;
44812 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
44813 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44819 # pragma omp target target-clause[optseq] new-line
44820 structured-block */
44822 #define OMP_TARGET_CLAUSE_MASK \
44823 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
44828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
44831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
44834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
44835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
44838 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
44839 enum pragma_context context
, bool *if_p
)
44843 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
44845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
44847 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
44848 const char *p
= IDENTIFIER_POINTER (id
);
44849 enum tree_code ccode
= ERROR_MARK
;
44851 if (strcmp (p
, "teams") == 0)
44853 else if (strcmp (p
, "parallel") == 0)
44854 ccode
= OMP_PARALLEL
;
44855 else if (strcmp (p
, "simd") == 0)
44857 if (ccode
!= ERROR_MARK
)
44859 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
44860 char p_name
[sizeof ("#pragma omp target teams distribute "
44861 "parallel for simd")];
44863 cp_lexer_consume_token (parser
->lexer
);
44864 strcpy (p_name
, "#pragma omp target");
44865 if (!flag_openmp
) /* flag_openmp_simd */
44871 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
44872 OMP_TARGET_CLAUSE_MASK
,
44876 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
44877 OMP_TARGET_CLAUSE_MASK
,
44881 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
44882 OMP_TARGET_CLAUSE_MASK
,
44886 gcc_unreachable ();
44888 return stmt
!= NULL_TREE
;
44890 keep_next_level (true);
44891 tree sb
= begin_omp_structured_block (), ret
;
44892 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
44896 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
44897 OMP_TARGET_CLAUSE_MASK
, cclauses
,
44901 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
44902 OMP_TARGET_CLAUSE_MASK
, cclauses
,
44906 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
44907 OMP_TARGET_CLAUSE_MASK
, cclauses
,
44911 gcc_unreachable ();
44913 cp_parser_end_omp_structured_block (parser
, save
);
44914 tree body
= finish_omp_structured_block (sb
);
44915 if (ret
== NULL_TREE
)
44917 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
44918 /* For combined target teams, ensure the num_teams and
44919 thread_limit clause expressions are evaluated on the host,
44920 before entering the target construct. */
44921 for (tree c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
44922 c
; c
= OMP_CLAUSE_CHAIN (c
))
44923 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
44924 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
44926 i
<= (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
); ++i
)
44927 if (OMP_CLAUSE_OPERAND (c
, i
)
44928 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, i
)) != INTEGER_CST
)
44930 tree expr
= OMP_CLAUSE_OPERAND (c
, i
);
44931 expr
= force_target_expr (TREE_TYPE (expr
), expr
,
44933 if (expr
== error_mark_node
)
44935 tree tmp
= TARGET_EXPR_SLOT (expr
);
44937 OMP_CLAUSE_OPERAND (c
, i
) = expr
;
44938 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
44939 OMP_CLAUSE_FIRSTPRIVATE
);
44940 OMP_CLAUSE_DECL (tc
) = tmp
;
44941 OMP_CLAUSE_CHAIN (tc
)
44942 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
44943 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
44945 c_omp_adjust_map_clauses (cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
], true);
44946 finish_omp_target (pragma_tok
->location
,
44947 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
], body
, true);
44950 else if (!flag_openmp
) /* flag_openmp_simd */
44952 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44955 else if (strcmp (p
, "data") == 0)
44957 cp_lexer_consume_token (parser
->lexer
);
44958 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
44961 else if (strcmp (p
, "enter") == 0)
44963 cp_lexer_consume_token (parser
->lexer
);
44964 return cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
44966 else if (strcmp (p
, "exit") == 0)
44968 cp_lexer_consume_token (parser
->lexer
);
44969 return cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
44971 else if (strcmp (p
, "update") == 0)
44973 cp_lexer_consume_token (parser
->lexer
);
44974 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
44977 if (!flag_openmp
) /* flag_openmp_simd */
44979 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44983 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
44984 "#pragma omp target", pragma_tok
,
44986 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
44987 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
44989 tree nc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
44990 OMP_CLAUSE_DECL (nc
) = OMP_CLAUSE_DECL (c
);
44991 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_ALWAYS_TOFROM
);
44992 OMP_CLAUSE_CHAIN (nc
) = OMP_CLAUSE_CHAIN (c
);
44993 OMP_CLAUSE_CHAIN (c
) = nc
;
44995 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_TARGET
);
44997 c_omp_adjust_map_clauses (clauses
, true);
44998 keep_next_level (true);
44999 tree body
= cp_parser_omp_structured_block (parser
, if_p
);
45001 finish_omp_target (pragma_tok
->location
, clauses
, body
, false);
45006 # pragma acc cache (variable-list) new-line
45010 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
45012 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
45014 auto_suppress_location_wrappers sentinel
;
45016 tree stmt
, clauses
;
45018 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
45019 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
45021 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
45023 stmt
= make_node (OACC_CACHE
);
45024 TREE_TYPE (stmt
) = void_type_node
;
45025 OACC_CACHE_CLAUSES (stmt
) = clauses
;
45026 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
45033 # pragma acc data oacc-data-clause[optseq] new-line
45034 structured-block */
45036 #define OACC_DATA_CLAUSE_MASK \
45037 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
45043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
45049 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
45051 tree stmt
, clauses
, block
;
45054 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
45055 "#pragma acc data", pragma_tok
);
45057 block
= begin_omp_parallel ();
45058 save
= cp_parser_begin_omp_structured_block (parser
);
45059 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
45060 cp_parser_end_omp_structured_block (parser
, save
);
45061 stmt
= finish_oacc_data (clauses
, block
);
45066 # pragma acc host_data <clauses> new-line
45067 structured-block */
45069 #define OACC_HOST_DATA_CLAUSE_MASK \
45070 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
45071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
45075 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
45077 tree stmt
, clauses
, block
;
45080 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
45081 "#pragma acc host_data", pragma_tok
);
45083 block
= begin_omp_parallel ();
45084 save
= cp_parser_begin_omp_structured_block (parser
);
45085 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
45086 cp_parser_end_omp_structured_block (parser
, save
);
45087 stmt
= finish_oacc_host_data (clauses
, block
);
45092 # pragma acc declare oacc-data-clause[optseq] new-line
45095 #define OACC_DECLARE_CLAUSE_MASK \
45096 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
45102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
45103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
45106 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
45108 tree clauses
, stmt
;
45109 bool error
= false;
45110 bool found_in_scope
= global_bindings_p ();
45112 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
45113 "#pragma acc declare", pragma_tok
, true);
45116 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
45118 error_at (pragma_tok
->location
,
45119 "no valid clauses specified in %<#pragma acc declare%>");
45123 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
45125 location_t loc
= OMP_CLAUSE_LOCATION (t
);
45126 tree decl
= OMP_CLAUSE_DECL (t
);
45127 if (!DECL_P (decl
))
45129 error_at (loc
, "array section in %<#pragma acc declare%>");
45133 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
45134 switch (OMP_CLAUSE_MAP_KIND (t
))
45136 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
45137 case GOMP_MAP_ALLOC
:
45139 case GOMP_MAP_FORCE_DEVICEPTR
:
45140 case GOMP_MAP_DEVICE_RESIDENT
:
45143 case GOMP_MAP_LINK
:
45144 if (!global_bindings_p ()
45145 && (TREE_STATIC (decl
)
45146 || !DECL_EXTERNAL (decl
)))
45149 "%qD must be a global variable in "
45150 "%<#pragma acc declare link%>",
45158 if (global_bindings_p ())
45160 error_at (loc
, "invalid OpenACC clause at file scope");
45164 if (DECL_EXTERNAL (decl
))
45167 "invalid use of %<extern%> variable %qD "
45168 "in %<#pragma acc declare%>", decl
);
45172 else if (TREE_PUBLIC (decl
))
45175 "invalid use of %<global%> variable %qD "
45176 "in %<#pragma acc declare%>", decl
);
45183 if (!found_in_scope
)
45184 /* This seems to ignore the existence of cleanup scopes?
45185 What is the meaning for local extern decls? The local
45186 extern is in this scope, but it is referring to a decl that
45187 is namespace scope. */
45188 for (tree d
= current_binding_level
->names
; d
; d
= TREE_CHAIN (d
))
45191 found_in_scope
= true;
45194 if (!found_in_scope
)
45197 "%qD must be a variable declared in the same scope as "
45198 "%<#pragma acc declare%>", decl
);
45203 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
45204 || lookup_attribute ("omp declare target link",
45205 DECL_ATTRIBUTES (decl
)))
45207 error_at (loc
, "variable %qD used more than once with "
45208 "%<#pragma acc declare%>", decl
);
45217 if (DECL_LOCAL_DECL_P (decl
))
45218 /* We need to mark the aliased decl, as that is the entity
45219 that is being referred to. This won't work for
45220 dependent variables, but it didn't work for them before
45221 DECL_LOCAL_DECL_P was a thing either. But then
45222 dependent local extern variable decls are as rare as
45224 if (auto alias
= DECL_LOCAL_DECL_ALIAS (decl
))
45225 if (alias
!= error_mark_node
)
45228 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
45229 id
= get_identifier ("omp declare target link");
45231 id
= get_identifier ("omp declare target");
45233 DECL_ATTRIBUTES (decl
)
45234 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
45235 if (current_binding_level
->kind
== sk_namespace
)
45237 symtab_node
*node
= symtab_node::get (decl
);
45240 node
->offloadable
= 1;
45241 if (ENABLE_OFFLOADING
)
45243 g
->have_offload
= true;
45244 if (is_a
<varpool_node
*> (node
))
45245 vec_safe_push (offload_vars
, decl
);
45252 if (error
|| current_binding_level
->kind
== sk_namespace
)
45255 stmt
= make_node (OACC_DECLARE
);
45256 TREE_TYPE (stmt
) = void_type_node
;
45257 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
45258 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
45266 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
45270 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
45272 LOC is the location of the #pragma token.
45275 #define OACC_ENTER_DATA_CLAUSE_MASK \
45276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45283 #define OACC_EXIT_DATA_CLAUSE_MASK \
45284 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
45288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
45289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
45290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45293 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
45296 location_t loc
= pragma_tok
->location
;
45297 tree stmt
, clauses
;
45298 const char *p
= "";
45300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45301 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
45303 if (strcmp (p
, "data") != 0)
45305 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
45306 enter
? "enter" : "exit");
45307 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
45311 cp_lexer_consume_token (parser
->lexer
);
45314 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
45315 "#pragma acc enter data", pragma_tok
);
45317 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
45318 "#pragma acc exit data", pragma_tok
);
45320 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
45322 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
45323 enter
? "enter" : "exit");
45327 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
45328 TREE_TYPE (stmt
) = void_type_node
;
45329 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
45330 SET_EXPR_LOCATION (stmt
, loc
);
45336 # pragma acc loop oacc-loop-clause[optseq] new-line
45337 structured-block */
45339 #define OACC_LOOP_CLAUSE_MASK \
45340 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
45341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
45344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
45345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
45346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
45347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
45348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
45349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
45352 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
45353 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
45355 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
45357 strcat (p_name
, " loop");
45358 mask
|= OACC_LOOP_CLAUSE_MASK
;
45360 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
45364 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
45366 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
45368 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
45371 tree block
= begin_omp_structured_block ();
45372 int save
= cp_parser_begin_omp_structured_block (parser
);
45373 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
45374 cp_parser_end_omp_structured_block (parser
, save
);
45375 add_stmt (finish_omp_structured_block (block
));
45381 # pragma acc kernels oacc-kernels-clause[optseq] new-line
45386 # pragma acc parallel oacc-parallel-clause[optseq] new-line
45391 # pragma acc serial oacc-serial-clause[optseq] new-line
45394 #define OACC_KERNELS_CLAUSE_MASK \
45395 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
45406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
45407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
45409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45411 #define OACC_PARALLEL_CLAUSE_MASK \
45412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
45421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
45424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
45425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
45429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45431 #define OACC_SERIAL_CLAUSE_MASK \
45432 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
45444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45449 cp_parser_oacc_compute (cp_parser
*parser
, cp_token
*pragma_tok
,
45450 char *p_name
, bool *if_p
)
45452 omp_clause_mask mask
;
45453 enum tree_code code
;
45454 switch (cp_parser_pragma_kind (pragma_tok
))
45456 case PRAGMA_OACC_KERNELS
:
45457 strcat (p_name
, " kernels");
45458 mask
= OACC_KERNELS_CLAUSE_MASK
;
45459 code
= OACC_KERNELS
;
45461 case PRAGMA_OACC_PARALLEL
:
45462 strcat (p_name
, " parallel");
45463 mask
= OACC_PARALLEL_CLAUSE_MASK
;
45464 code
= OACC_PARALLEL
;
45466 case PRAGMA_OACC_SERIAL
:
45467 strcat (p_name
, " serial");
45468 mask
= OACC_SERIAL_CLAUSE_MASK
;
45469 code
= OACC_SERIAL
;
45472 gcc_unreachable ();
45475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45478 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
45479 if (strcmp (p
, "loop") == 0)
45481 cp_lexer_consume_token (parser
->lexer
);
45482 tree block
= begin_omp_parallel ();
45484 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
45486 protected_set_expr_location (stmt
, pragma_tok
->location
);
45487 return finish_omp_construct (code
, block
, clauses
);
45491 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
45493 tree block
= begin_omp_parallel ();
45494 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
45495 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
45496 cp_parser_end_omp_structured_block (parser
, save
);
45497 return finish_omp_construct (code
, block
, clauses
);
45501 # pragma acc update oacc-update-clause[optseq] new-line
45504 #define OACC_UPDATE_CLAUSE_MASK \
45505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
45507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
45508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
45510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
45513 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
45515 tree stmt
, clauses
;
45517 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
45518 "#pragma acc update", pragma_tok
);
45520 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
45522 error_at (pragma_tok
->location
,
45523 "%<#pragma acc update%> must contain at least one "
45524 "%<device%> or %<host%> or %<self%> clause");
45528 stmt
= make_node (OACC_UPDATE
);
45529 TREE_TYPE (stmt
) = void_type_node
;
45530 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
45531 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
45537 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
45539 LOC is the location of the #pragma token.
45542 #define OACC_WAIT_CLAUSE_MASK \
45543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
45546 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
45548 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
45549 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
45551 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
45552 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
45554 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
45555 "#pragma acc wait", pragma_tok
);
45557 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
45558 stmt
= finish_expr_stmt (stmt
);
45564 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
45566 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
45567 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
45568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
45570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
45571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
45572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
45575 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
45576 enum pragma_context context
,
45579 bool first_p
= parser
->omp_declare_simd
== NULL
;
45580 cp_omp_declare_simd_data data
;
45583 data
.error_seen
= false;
45584 data
.fndecl_seen
= false;
45585 data
.variant_p
= variant_p
;
45586 data
.tokens
= vNULL
;
45587 data
.attribs
[0] = NULL
;
45588 data
.attribs
[1] = NULL
;
45589 data
.loc
= UNKNOWN_LOCATION
;
45590 /* It is safe to take the address of a local variable; it will only be
45591 used while this scope is live. */
45592 parser
->omp_declare_simd
= &data
;
45594 else if (parser
->omp_declare_simd
->variant_p
!= variant_p
)
45596 error_at (pragma_tok
->location
,
45597 "%<#pragma omp declare %s%> followed by "
45598 "%<#pragma omp declare %s%>",
45599 parser
->omp_declare_simd
->variant_p
? "variant" : "simd",
45600 parser
->omp_declare_simd
->variant_p
? "simd" : "variant");
45601 parser
->omp_declare_simd
->error_seen
= true;
45604 /* Store away all pragma tokens. */
45605 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
45606 cp_lexer_consume_token (parser
->lexer
);
45607 cp_parser_require_pragma_eol (parser
, pragma_tok
);
45608 struct cp_token_cache
*cp
45609 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
45610 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
45614 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
45615 cp_parser_pragma (parser
, context
, NULL
);
45618 case pragma_external
:
45619 cp_parser_declaration (parser
, NULL_TREE
);
45621 case pragma_member
:
45622 cp_parser_member_declaration (parser
);
45624 case pragma_objc_icode
:
45625 cp_parser_block_declaration (parser
, /*statement_p=*/false);
45628 cp_parser_declaration_statement (parser
);
45631 if (parser
->omp_declare_simd
45632 && !parser
->omp_declare_simd
->error_seen
45633 && !parser
->omp_declare_simd
->fndecl_seen
)
45634 error_at (pragma_tok
->location
,
45635 "%<#pragma omp declare %s%> not immediately followed by "
45636 "function declaration or definition",
45637 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
45638 data
.tokens
.release ();
45639 parser
->omp_declare_simd
= NULL
;
45643 static const char *const omp_construct_selectors
[] = {
45644 "simd", "target", "teams", "parallel", "for", NULL
};
45645 static const char *const omp_device_selectors
[] = {
45646 "kind", "isa", "arch", NULL
};
45647 static const char *const omp_implementation_selectors
[] = {
45648 "vendor", "extension", "atomic_default_mem_order", "unified_address",
45649 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL
};
45650 static const char *const omp_user_selectors
[] = {
45651 "condition", NULL
};
45656 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
45659 score(score-expression) */
45662 cp_parser_omp_context_selector (cp_parser
*parser
, tree set
, bool has_parms_p
)
45664 tree ret
= NULL_TREE
;
45668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
45669 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45670 selector
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45673 cp_parser_error (parser
, "expected trait selector name");
45674 return error_mark_node
;
45677 tree properties
= NULL_TREE
;
45678 const char *const *selectors
= NULL
;
45679 bool allow_score
= true;
45680 bool allow_user
= false;
45681 int property_limit
= 0;
45682 enum { CTX_PROPERTY_NONE
, CTX_PROPERTY_USER
, CTX_PROPERTY_NAME_LIST
,
45683 CTX_PROPERTY_ID
, CTX_PROPERTY_EXPR
,
45684 CTX_PROPERTY_SIMD
} property_kind
= CTX_PROPERTY_NONE
;
45685 switch (IDENTIFIER_POINTER (set
)[0])
45687 case 'c': /* construct */
45688 selectors
= omp_construct_selectors
;
45689 allow_score
= false;
45690 property_limit
= 1;
45691 property_kind
= CTX_PROPERTY_SIMD
;
45693 case 'd': /* device */
45694 selectors
= omp_device_selectors
;
45695 allow_score
= false;
45697 property_limit
= 3;
45698 property_kind
= CTX_PROPERTY_NAME_LIST
;
45700 case 'i': /* implementation */
45701 selectors
= omp_implementation_selectors
;
45703 property_limit
= 3;
45704 property_kind
= CTX_PROPERTY_NAME_LIST
;
45706 case 'u': /* user */
45707 selectors
= omp_user_selectors
;
45708 property_limit
= 1;
45709 property_kind
= CTX_PROPERTY_EXPR
;
45712 gcc_unreachable ();
45714 for (int i
= 0; ; i
++)
45716 if (selectors
[i
] == NULL
)
45720 property_kind
= CTX_PROPERTY_USER
;
45725 error ("selector %qs not allowed for context selector "
45726 "set %qs", IDENTIFIER_POINTER (selector
),
45727 IDENTIFIER_POINTER (set
));
45728 cp_lexer_consume_token (parser
->lexer
);
45729 return error_mark_node
;
45732 if (i
== property_limit
)
45733 property_kind
= CTX_PROPERTY_NONE
;
45734 if (strcmp (selectors
[i
], IDENTIFIER_POINTER (selector
)) == 0)
45737 if (property_kind
== CTX_PROPERTY_NAME_LIST
45738 && IDENTIFIER_POINTER (set
)[0] == 'i'
45739 && strcmp (IDENTIFIER_POINTER (selector
),
45740 "atomic_default_mem_order") == 0)
45741 property_kind
= CTX_PROPERTY_ID
;
45743 cp_lexer_consume_token (parser
->lexer
);
45745 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
45747 if (property_kind
== CTX_PROPERTY_NONE
)
45749 error ("selector %qs does not accept any properties",
45750 IDENTIFIER_POINTER (selector
));
45751 return error_mark_node
;
45754 matching_parens parens
;
45755 parens
.consume_open (parser
);
45757 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
45759 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
45760 && strcmp (IDENTIFIER_POINTER (token
->u
.value
), "score") == 0
45761 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
45763 cp_lexer_save_tokens (parser
->lexer
);
45764 cp_lexer_consume_token (parser
->lexer
);
45765 cp_lexer_consume_token (parser
->lexer
);
45766 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
45768 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
45770 cp_lexer_rollback_tokens (parser
->lexer
);
45771 cp_lexer_consume_token (parser
->lexer
);
45773 matching_parens parens2
;
45774 parens2
.require_open (parser
);
45775 tree score
= cp_parser_constant_expression (parser
);
45776 if (!parens2
.require_close (parser
))
45777 cp_parser_skip_to_closing_parenthesis (parser
, true,
45779 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
45780 if (score
!= error_mark_node
)
45782 score
= fold_non_dependent_expr (score
);
45783 if (value_dependent_expression_p (score
))
45784 properties
= tree_cons (get_identifier (" score"),
45785 score
, properties
);
45786 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score
))
45787 || TREE_CODE (score
) != INTEGER_CST
)
45788 error_at (token
->location
, "score argument must be "
45789 "constant integer expression");
45790 else if (tree_int_cst_sgn (score
) < 0)
45791 error_at (token
->location
, "score argument must be "
45794 properties
= tree_cons (get_identifier (" score"),
45795 score
, properties
);
45799 cp_lexer_rollback_tokens (parser
->lexer
);
45801 token
= cp_lexer_peek_token (parser
->lexer
);
45804 switch (property_kind
)
45807 case CTX_PROPERTY_USER
:
45810 t
= cp_parser_constant_expression (parser
);
45811 if (t
!= error_mark_node
)
45813 t
= fold_non_dependent_expr (t
);
45814 if (TREE_CODE (t
) == STRING_CST
)
45815 properties
= tree_cons (NULL_TREE
, t
, properties
);
45816 else if (!value_dependent_expression_p (t
)
45817 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
45818 || !tree_fits_shwi_p (t
)))
45819 error_at (token
->location
, "property must be "
45820 "constant integer expression or string "
45823 properties
= tree_cons (NULL_TREE
, t
, properties
);
45826 return error_mark_node
;
45828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
45829 cp_lexer_consume_token (parser
->lexer
);
45835 case CTX_PROPERTY_ID
:
45836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
45837 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45839 tree prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45840 cp_lexer_consume_token (parser
->lexer
);
45841 properties
= tree_cons (prop
, NULL_TREE
, properties
);
45845 cp_parser_error (parser
, "expected identifier");
45846 return error_mark_node
;
45849 case CTX_PROPERTY_NAME_LIST
:
45852 tree prop
= NULL_TREE
, value
= NULL_TREE
;
45853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
45854 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45856 prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45857 cp_lexer_consume_token (parser
->lexer
);
45859 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_STRING
))
45860 value
= cp_parser_string_literal (parser
, false, false);
45863 cp_parser_error (parser
, "expected identifier or "
45865 return error_mark_node
;
45868 properties
= tree_cons (prop
, value
, properties
);
45870 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
45871 cp_lexer_consume_token (parser
->lexer
);
45877 case CTX_PROPERTY_EXPR
:
45878 t
= cp_parser_constant_expression (parser
);
45879 if (t
!= error_mark_node
)
45881 t
= fold_non_dependent_expr (t
);
45882 if (!value_dependent_expression_p (t
)
45883 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
45884 || !tree_fits_shwi_p (t
)))
45885 error_at (token
->location
, "property must be "
45886 "constant integer expression");
45888 properties
= tree_cons (NULL_TREE
, t
, properties
);
45891 return error_mark_node
;
45893 case CTX_PROPERTY_SIMD
:
45896 error_at (token
->location
, "properties for %<simd%> "
45897 "selector may not be specified in "
45898 "%<metadirective%>");
45899 return error_mark_node
;
45902 = cp_parser_omp_all_clauses (parser
,
45903 OMP_DECLARE_SIMD_CLAUSE_MASK
,
45904 "simd", NULL
, true, 2);
45907 gcc_unreachable ();
45910 if (!parens
.require_close (parser
))
45911 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
45913 properties
= nreverse (properties
);
45915 else if (property_kind
== CTX_PROPERTY_NAME_LIST
45916 || property_kind
== CTX_PROPERTY_ID
45917 || property_kind
== CTX_PROPERTY_EXPR
)
45919 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
45920 return error_mark_node
;
45923 ret
= tree_cons (selector
, properties
, ret
);
45925 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
45926 cp_lexer_consume_token (parser
->lexer
);
45932 return nreverse (ret
);
45937 trait-set-selector[,trait-set-selector[,...]]
45939 trait-set-selector:
45940 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
45942 trait-set-selector-name:
45949 cp_parser_omp_context_selector_specification (cp_parser
*parser
,
45952 tree ret
= NULL_TREE
;
45955 const char *setp
= "";
45956 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45958 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
45962 if (strcmp (setp
, "construct") == 0)
45966 if (strcmp (setp
, "device") == 0)
45970 if (strcmp (setp
, "implementation") == 0)
45974 if (strcmp (setp
, "user") == 0)
45982 cp_parser_error (parser
, "expected %<construct%>, %<device%>, "
45983 "%<implementation%> or %<user%>");
45984 return error_mark_node
;
45987 tree set
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45988 cp_lexer_consume_token (parser
->lexer
);
45990 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
45991 return error_mark_node
;
45993 matching_braces braces
;
45994 if (!braces
.require_open (parser
))
45995 return error_mark_node
;
45998 = cp_parser_omp_context_selector (parser
, set
, has_parms_p
);
45999 if (selectors
== error_mark_node
)
46001 cp_parser_skip_to_closing_brace (parser
);
46002 ret
= error_mark_node
;
46004 else if (ret
!= error_mark_node
)
46005 ret
= tree_cons (set
, selectors
, ret
);
46007 braces
.require_close (parser
);
46009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
46010 cp_lexer_consume_token (parser
->lexer
);
46016 if (ret
== error_mark_node
)
46018 return nreverse (ret
);
46021 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
46022 that into "omp declare variant base" attribute. */
46025 cp_finish_omp_declare_variant (cp_parser
*parser
, cp_token
*pragma_tok
,
46028 matching_parens parens
;
46029 if (!parens
.require_open (parser
))
46032 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46037 cp_id_kind idk
= CP_ID_KIND_NONE
;
46038 cp_token
*varid_token
= cp_lexer_peek_token (parser
->lexer
);
46040 = cp_parser_id_expression (parser
, /*template_keyword_p=*/false,
46041 /*check_dependency_p=*/true,
46042 /*template_p=*/&template_p
,
46043 /*declarator_p=*/false,
46044 /*optional_p=*/false);
46045 parens
.require_close (parser
);
46048 if (TREE_CODE (varid
) == TEMPLATE_ID_EXPR
46049 || TREE_CODE (varid
) == TYPE_DECL
46050 || varid
== error_mark_node
)
46052 else if (varid_token
->type
== CPP_NAME
&& varid_token
->error_reported
)
46053 variant
= NULL_TREE
;
46056 tree ambiguous_decls
;
46057 variant
= cp_parser_lookup_name (parser
, varid
, none_type
,
46058 template_p
, /*is_namespace=*/false,
46059 /*check_dependency=*/true,
46061 varid
.get_location ());
46062 if (ambiguous_decls
)
46063 variant
= NULL_TREE
;
46065 if (variant
== NULL_TREE
)
46066 variant
= error_mark_node
;
46067 else if (TREE_CODE (variant
) != SCOPE_REF
)
46069 const char *error_msg
;
46071 = finish_id_expression (varid
, variant
, parser
->scope
,
46073 &parser
->non_integral_constant_expression_p
,
46074 template_p
, true, false, false, &error_msg
,
46075 varid
.get_location ());
46077 cp_parser_error (parser
, error_msg
);
46079 location_t caret_loc
= get_pure_location (varid
.get_location ());
46080 location_t start_loc
= get_start (varid_token
->location
);
46081 location_t finish_loc
= get_finish (varid
.get_location ());
46082 location_t varid_loc
= make_location (caret_loc
, start_loc
, finish_loc
);
46084 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46085 if (parser
->lexer
->in_omp_attribute_pragma
46086 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
46087 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
46088 cp_lexer_consume_token (parser
->lexer
);
46090 const char *clause
= "";
46091 location_t match_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
46092 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46093 clause
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
46094 if (strcmp (clause
, "match"))
46096 cp_parser_error (parser
, "expected %<match%>");
46100 cp_lexer_consume_token (parser
->lexer
);
46102 if (!parens
.require_open (parser
))
46105 tree ctx
= cp_parser_omp_context_selector_specification (parser
, true);
46106 if (ctx
== error_mark_node
)
46108 ctx
= omp_check_context_selector (match_loc
, ctx
);
46109 if (ctx
!= error_mark_node
&& variant
!= error_mark_node
)
46111 tree match_loc_node
= maybe_wrap_with_location (integer_zero_node
,
46113 tree loc_node
= maybe_wrap_with_location (integer_zero_node
, varid_loc
);
46114 loc_node
= tree_cons (match_loc_node
,
46115 build_int_cst (integer_type_node
, idk
),
46116 build_tree_list (loc_node
, integer_zero_node
));
46117 attrs
= tree_cons (get_identifier ("omp declare variant base"),
46118 tree_cons (variant
, ctx
, loc_node
), attrs
);
46119 if (processing_template_decl
)
46120 ATTR_IS_DEPENDENT (attrs
) = 1;
46123 parens
.require_close (parser
);
46124 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46129 /* Finalize #pragma omp declare simd clauses after direct declarator has
46130 been parsed, and put that into "omp declare simd" attribute. */
46133 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
46135 struct cp_token_cache
*ce
;
46136 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
46139 if (!data
->error_seen
&& data
->fndecl_seen
)
46141 error ("%<#pragma omp declare %s%> not immediately followed by "
46142 "a single function declaration or definition",
46143 data
->variant_p
? "variant" : "simd");
46144 data
->error_seen
= true;
46146 if (data
->error_seen
)
46149 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
46153 cp_parser_push_lexer_for_tokens (parser
, ce
);
46154 parser
->lexer
->in_pragma
= true;
46155 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
46156 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
46157 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46158 const char *kind
= IDENTIFIER_POINTER (id
);
46159 cp_lexer_consume_token (parser
->lexer
);
46160 if (strcmp (kind
, "simd") == 0)
46162 /* For now only in C++ attributes, do it always for OpenMP 5.1.
46163 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46164 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46165 cp_lexer_consume_token (parser->lexer); */
46167 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
46168 "#pragma omp declare simd",
46171 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
46172 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
46173 TREE_CHAIN (c
) = attrs
;
46174 if (processing_template_decl
)
46175 ATTR_IS_DEPENDENT (c
) = 1;
46180 gcc_assert (strcmp (kind
, "variant") == 0);
46182 = cp_finish_omp_declare_variant (parser
, pragma_tok
, attrs
);
46184 cp_parser_pop_lexer (parser
);
46187 cp_lexer
*lexer
= NULL
;
46188 for (int i
= 0; i
< 2; i
++)
46190 if (data
->attribs
[i
] == NULL
)
46192 for (tree
*pa
= data
->attribs
[i
]; *pa
; )
46193 if (get_attribute_namespace (*pa
) == omp_identifier
46194 && is_attribute_p ("directive", get_attribute_name (*pa
)))
46196 for (tree a
= TREE_VALUE (*pa
); a
; a
= TREE_CHAIN (a
))
46198 tree d
= TREE_VALUE (a
);
46199 gcc_assert (TREE_CODE (d
) == DEFERRED_PARSE
);
46200 cp_token
*first
= DEFPARSE_TOKENS (d
)->first
;
46201 cp_token
*last
= DEFPARSE_TOKENS (d
)->last
;
46202 const char *directive
[3] = {};
46203 for (int j
= 0; j
< 3; j
++)
46205 tree id
= NULL_TREE
;
46206 if (first
+ j
== last
)
46208 if (first
[j
].type
== CPP_NAME
)
46209 id
= first
[j
].u
.value
;
46210 else if (first
[j
].type
== CPP_KEYWORD
)
46211 id
= ridpointers
[(int) first
[j
].keyword
];
46214 directive
[j
] = IDENTIFIER_POINTER (id
);
46216 const c_omp_directive
*dir
= NULL
;
46218 dir
= c_omp_categorize_directive (directive
[0], directive
[1],
46222 error_at (first
->location
,
46223 "unknown OpenMP directive name in "
46224 "%<omp::directive%> attribute argument");
46227 if (dir
->id
!= PRAGMA_OMP_DECLARE
46228 || (strcmp (directive
[1], "simd") != 0
46229 && strcmp (directive
[1], "variant") != 0))
46231 error_at (first
->location
,
46232 "OpenMP directive other than %<declare simd%> "
46233 "or %<declare variant%> appertains to a "
46238 if (parser
->omp_attrs_forbidden_p
)
46240 error_at (first
->location
,
46241 "mixing OpenMP directives with attribute and "
46242 "pragma syntax on the same statement");
46243 parser
->omp_attrs_forbidden_p
= false;
46246 if (!flag_openmp
&& strcmp (directive
[1], "simd") != 0)
46250 lexer
= cp_lexer_alloc ();
46251 lexer
->debugging_p
= parser
->lexer
->debugging_p
;
46253 vec_safe_reserve (lexer
->buffer
, (last
- first
) + 2);
46255 tok
.type
= CPP_PRAGMA
;
46256 tok
.keyword
= RID_MAX
;
46257 tok
.u
.value
= build_int_cst (NULL
, PRAGMA_OMP_DECLARE
);
46258 tok
.location
= first
->location
;
46259 lexer
->buffer
->quick_push (tok
);
46260 while (++first
< last
)
46261 lexer
->buffer
->quick_push (*first
);
46263 tok
.type
= CPP_PRAGMA_EOL
;
46264 tok
.keyword
= RID_MAX
;
46265 tok
.location
= last
->location
;
46266 lexer
->buffer
->quick_push (tok
);
46268 tok
.type
= CPP_EOF
;
46269 tok
.keyword
= RID_MAX
;
46270 tok
.location
= last
->location
;
46271 lexer
->buffer
->quick_push (tok
);
46272 lexer
->next
= parser
->lexer
;
46273 lexer
->next_token
= lexer
->buffer
->address ();
46274 lexer
->last_token
= lexer
->next_token
46275 + lexer
->buffer
->length ()
46277 lexer
->in_omp_attribute_pragma
= true;
46278 parser
->lexer
= lexer
;
46279 /* Move the current source position to that of the first token
46280 in the new lexer. */
46281 cp_lexer_set_source_position_from_token (lexer
->next_token
);
46283 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
46284 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46285 const char *kind
= IDENTIFIER_POINTER (id
);
46286 cp_lexer_consume_token (parser
->lexer
);
46289 if (strcmp (kind
, "simd") == 0)
46291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
46292 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
46293 cp_lexer_consume_token (parser
->lexer
);
46295 omp_clause_mask mask
= OMP_DECLARE_SIMD_CLAUSE_MASK
;
46296 cl
= cp_parser_omp_all_clauses (parser
, mask
,
46297 "#pragma omp declare simd",
46300 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
46301 c
= build_tree_list (get_identifier ("omp declare simd"),
46303 TREE_CHAIN (c
) = attrs
;
46304 if (processing_template_decl
)
46305 ATTR_IS_DEPENDENT (c
) = 1;
46310 gcc_assert (strcmp (kind
, "variant") == 0);
46312 = cp_finish_omp_declare_variant (parser
, pragma_tok
,
46315 gcc_assert (parser
->lexer
!= lexer
);
46316 vec_safe_truncate (lexer
->buffer
, 0);
46318 *pa
= TREE_CHAIN (*pa
);
46321 pa
= &TREE_CHAIN (*pa
);
46324 cp_lexer_destroy (lexer
);
46326 data
->fndecl_seen
= true;
46330 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
46331 on #pragma omp declare target. Return false if errors were reported. */
46334 handle_omp_declare_target_clause (tree c
, tree t
, int device_type
)
46336 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
46337 tree at2
= lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t
));
46339 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
46341 id
= get_identifier ("omp declare target link");
46342 std::swap (at1
, at2
);
46345 id
= get_identifier ("omp declare target");
46348 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ENTER
)
46349 error_at (OMP_CLAUSE_LOCATION (c
),
46350 "%qD specified both in declare target %<link%> and %qs"
46351 " clauses", t
, OMP_CLAUSE_ENTER_TO (c
) ? "to" : "enter");
46353 error_at (OMP_CLAUSE_LOCATION (c
),
46354 "%qD specified both in declare target %<link%> and "
46355 "%<to%> or %<enter%> clauses", t
);
46360 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
46361 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
46364 symtab_node
*node
= symtab_node::get (t
);
46367 node
->offloadable
= 1;
46368 if (ENABLE_OFFLOADING
)
46370 g
->have_offload
= true;
46371 if (is_a
<varpool_node
*> (node
))
46372 vec_safe_push (offload_vars
, t
);
46376 if (TREE_CODE (t
) != FUNCTION_DECL
)
46378 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_HOST
) != 0)
46380 tree at3
= lookup_attribute ("omp declare target host",
46381 DECL_ATTRIBUTES (t
));
46382 if (at3
== NULL_TREE
)
46384 id
= get_identifier ("omp declare target host");
46385 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
46388 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_NOHOST
) != 0)
46390 tree at3
= lookup_attribute ("omp declare target nohost",
46391 DECL_ATTRIBUTES (t
));
46392 if (at3
== NULL_TREE
)
46394 id
= get_identifier ("omp declare target nohost");
46395 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
46402 # pragma omp declare target new-line
46403 declarations and definitions
46404 # pragma omp end declare target new-line
46407 # pragma omp declare target ( extended-list ) new-line
46409 # pragma omp declare target declare-target-clauses[seq] new-line */
46411 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
46412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
46413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
46414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
46415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
46418 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
46420 tree clauses
= NULL_TREE
;
46421 int device_type
= 0;
46422 bool only_device_type
= true;
46423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
46424 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46425 || (parser
->lexer
->in_omp_attribute_pragma
46426 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
46427 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
)))
46429 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
46430 "#pragma omp declare target", pragma_tok
);
46431 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
46433 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ENTER
,
46435 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
46436 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46440 struct omp_declare_target_attr a
46441 = { parser
->lexer
->in_omp_attribute_pragma
};
46442 vec_safe_push (scope_chain
->omp_declare_target_attribute
, a
);
46443 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46446 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
46447 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
46448 device_type
|= OMP_CLAUSE_DEVICE_TYPE_KIND (c
);
46449 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
46451 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
46453 tree t
= OMP_CLAUSE_DECL (c
);
46454 only_device_type
= false;
46455 if (!handle_omp_declare_target_clause (c
, t
, device_type
))
46457 if (VAR_OR_FUNCTION_DECL_P (t
)
46458 && DECL_LOCAL_DECL_P (t
)
46459 && DECL_LANG_SPECIFIC (t
)
46460 && DECL_LOCAL_DECL_ALIAS (t
)
46461 && DECL_LOCAL_DECL_ALIAS (t
) != error_mark_node
)
46462 handle_omp_declare_target_clause (c
, DECL_LOCAL_DECL_ALIAS (t
),
46465 if (device_type
&& only_device_type
)
46466 warning_at (OMP_CLAUSE_LOCATION (clauses
), 0,
46467 "directive with only %<device_type%> clauses ignored");
46471 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
46473 const char *p
= "";
46474 bool in_omp_attribute_pragma
= parser
->lexer
->in_omp_attribute_pragma
;
46475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46477 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46478 p
= IDENTIFIER_POINTER (id
);
46480 if (strcmp (p
, "declare") == 0)
46482 cp_lexer_consume_token (parser
->lexer
);
46484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46486 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46487 p
= IDENTIFIER_POINTER (id
);
46489 if (strcmp (p
, "target") == 0)
46490 cp_lexer_consume_token (parser
->lexer
);
46493 cp_parser_error (parser
, "expected %<target%>");
46494 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46500 cp_parser_error (parser
, "expected %<declare%>");
46501 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46504 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46505 if (!vec_safe_length (scope_chain
->omp_declare_target_attribute
))
46506 error_at (pragma_tok
->location
,
46507 "%<#pragma omp end declare target%> without corresponding "
46508 "%<#pragma omp declare target%>");
46511 omp_declare_target_attr
46512 a
= scope_chain
->omp_declare_target_attribute
->pop ();
46513 if (a
.attr_syntax
!= in_omp_attribute_pragma
)
46516 error_at (pragma_tok
->location
,
46517 "%<declare target%> in attribute syntax terminated "
46518 "with %<end declare target%> in pragma syntax");
46520 error_at (pragma_tok
->location
,
46521 "%<declare target%> in pragma syntax terminated "
46522 "with %<end declare target%> in attribute syntax");
46527 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
46528 expression and optional initializer clause of
46529 #pragma omp declare reduction. We store the expression(s) as
46530 either 3, 6 or 7 special statements inside of the artificial function's
46531 body. The first two statements are DECL_EXPRs for the artificial
46532 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
46533 expression that uses those variables.
46534 If there was any INITIALIZER clause, this is followed by further statements,
46535 the fourth and fifth statements are DECL_EXPRs for the artificial
46536 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
46537 constructor variant (first token after open paren is not omp_priv),
46538 then the sixth statement is a statement with the function call expression
46539 that uses the OMP_PRIV and optionally OMP_ORIG variable.
46540 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
46541 to initialize the OMP_PRIV artificial variable and there is seventh
46542 statement, a DECL_EXPR of the OMP_PRIV statement again. */
46545 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
46547 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
46548 gcc_assert (TYPE_REF_P (type
));
46549 type
= TREE_TYPE (type
);
46550 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
46551 DECL_ARTIFICIAL (omp_out
) = 1;
46552 pushdecl (omp_out
);
46553 add_decl_expr (omp_out
);
46554 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
46555 DECL_ARTIFICIAL (omp_in
) = 1;
46557 add_decl_expr (omp_in
);
46559 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
46561 keep_next_level (true);
46562 tree block
= begin_omp_structured_block ();
46563 combiner
= cp_parser_expression (parser
);
46564 finish_expr_stmt (combiner
);
46565 block
= finish_omp_structured_block (block
);
46566 if (processing_template_decl
)
46567 block
= build_stmt (input_location
, EXPR_STMT
, block
);
46570 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
46573 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46574 if (parser
->lexer
->in_omp_attribute_pragma
46575 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
46576 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
46577 cp_lexer_consume_token (parser
->lexer
);
46579 const char *p
= "";
46580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46582 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46583 p
= IDENTIFIER_POINTER (id
);
46586 if (strcmp (p
, "initializer") == 0)
46588 cp_lexer_consume_token (parser
->lexer
);
46589 matching_parens parens
;
46590 if (!parens
.require_open (parser
))
46594 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46596 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46597 p
= IDENTIFIER_POINTER (id
);
46600 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
46601 DECL_ARTIFICIAL (omp_priv
) = 1;
46602 pushdecl (omp_priv
);
46603 add_decl_expr (omp_priv
);
46604 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
46605 DECL_ARTIFICIAL (omp_orig
) = 1;
46606 pushdecl (omp_orig
);
46607 add_decl_expr (omp_orig
);
46609 keep_next_level (true);
46610 block
= begin_omp_structured_block ();
46613 if (strcmp (p
, "omp_priv") == 0)
46615 bool is_direct_init
, is_non_constant_init
;
46617 cp_lexer_consume_token (parser
->lexer
);
46618 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
46619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
46620 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
46621 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
46623 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
46624 == CPP_CLOSE_PAREN
))
46626 finish_omp_structured_block (block
);
46627 error ("invalid initializer clause");
46630 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
46631 &is_non_constant_init
);
46632 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
46633 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
46637 cp_parser_parse_tentatively (parser
);
46638 /* Don't create location wrapper nodes here. */
46639 auto_suppress_location_wrappers sentinel
;
46640 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
46641 /*check_dependency_p=*/true,
46642 /*template_p=*/NULL
,
46643 /*declarator_p=*/false,
46644 /*optional_p=*/false);
46645 vec
<tree
, va_gc
> *args
;
46646 if (fn_name
== error_mark_node
46647 || cp_parser_error_occurred (parser
)
46648 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
46649 || ((args
= cp_parser_parenthesized_expression_list
46650 (parser
, non_attr
, /*cast_p=*/false,
46651 /*allow_expansion_p=*/true,
46652 /*non_constant_p=*/NULL
)),
46653 cp_parser_error_occurred (parser
)))
46655 finish_omp_structured_block (block
);
46656 cp_parser_abort_tentative_parse (parser
);
46657 cp_parser_error (parser
, "expected id-expression (arguments)");
46662 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
46663 if (arg
== omp_priv
46664 || (TREE_CODE (arg
) == ADDR_EXPR
46665 && TREE_OPERAND (arg
, 0) == omp_priv
))
46667 cp_parser_abort_tentative_parse (parser
);
46668 if (arg
== NULL_TREE
)
46669 error ("one of the initializer call arguments should be %<omp_priv%>"
46670 " or %<&omp_priv%>");
46671 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
46673 finish_expr_stmt (initializer
);
46676 block
= finish_omp_structured_block (block
);
46677 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
46678 if (processing_template_decl
)
46679 block
= build_stmt (input_location
, EXPR_STMT
, block
);
46683 add_decl_expr (omp_orig
);
46685 if (!parens
.require_close (parser
))
46689 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
46690 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
46697 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46698 initializer-clause[opt] new-line
46700 initializer-clause:
46701 initializer (omp_priv initializer)
46702 initializer (function-name (argument-list)) */
46705 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
46706 enum pragma_context
)
46708 auto_vec
<tree
> types
;
46709 enum tree_code reduc_code
= ERROR_MARK
;
46710 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
46712 cp_token
*first_token
;
46713 cp_token_cache
*cp
;
46717 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
46718 p
= obstack_alloc (&declarator_obstack
, 0);
46720 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
46723 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
46726 reduc_code
= PLUS_EXPR
;
46729 reduc_code
= MULT_EXPR
;
46732 reduc_code
= MINUS_EXPR
;
46735 reduc_code
= BIT_AND_EXPR
;
46738 reduc_code
= BIT_XOR_EXPR
;
46741 reduc_code
= BIT_IOR_EXPR
;
46744 reduc_code
= TRUTH_ANDIF_EXPR
;
46747 reduc_code
= TRUTH_ORIF_EXPR
;
46750 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
46753 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
46754 "%<|%>, %<&&%>, %<||%> or identifier");
46758 if (reduc_code
!= ERROR_MARK
)
46759 cp_lexer_consume_token (parser
->lexer
);
46761 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
46762 if (reduc_id
== error_mark_node
)
46765 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
46768 /* Types may not be defined in declare reduction type list. */
46769 const char *saved_message
;
46770 saved_message
= parser
->type_definition_forbidden_message
;
46771 parser
->type_definition_forbidden_message
46772 = G_("types may not be defined in declare reduction type list");
46773 bool saved_colon_corrects_to_scope_p
;
46774 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
46775 parser
->colon_corrects_to_scope_p
= false;
46776 bool saved_colon_doesnt_start_class_def_p
;
46777 saved_colon_doesnt_start_class_def_p
46778 = parser
->colon_doesnt_start_class_def_p
;
46779 parser
->colon_doesnt_start_class_def_p
= true;
46783 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
46784 type
= cp_parser_type_id (parser
);
46785 if (type
== error_mark_node
)
46787 else if (ARITHMETIC_TYPE_P (type
)
46788 && (orig_reduc_id
== NULL_TREE
46789 || (TREE_CODE (type
) != COMPLEX_TYPE
46790 && (id_equal (orig_reduc_id
, "min")
46791 || id_equal (orig_reduc_id
, "max")))))
46792 error_at (loc
, "predeclared arithmetic type %qT in "
46793 "%<#pragma omp declare reduction%>", type
);
46794 else if (FUNC_OR_METHOD_TYPE_P (type
)
46795 || TREE_CODE (type
) == ARRAY_TYPE
)
46796 error_at (loc
, "function or array type %qT in "
46797 "%<#pragma omp declare reduction%>", type
);
46798 else if (TYPE_REF_P (type
))
46799 error_at (loc
, "reference type %qT in "
46800 "%<#pragma omp declare reduction%>", type
);
46801 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
46802 error_at (loc
, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
46803 "type %qT in %<#pragma omp declare reduction%>", type
);
46805 types
.safe_push (type
);
46807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
46808 cp_lexer_consume_token (parser
->lexer
);
46813 /* Restore the saved message. */
46814 parser
->type_definition_forbidden_message
= saved_message
;
46815 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
46816 parser
->colon_doesnt_start_class_def_p
46817 = saved_colon_doesnt_start_class_def_p
;
46819 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
46820 || types
.is_empty ())
46823 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46827 first_token
= cp_lexer_peek_token (parser
->lexer
);
46830 FOR_EACH_VEC_ELT (types
, i
, type
)
46833 = build_function_type_list (void_type_node
,
46834 cp_build_reference_type (type
, false),
46836 tree this_reduc_id
= reduc_id
;
46837 if (!dependent_type_p (type
))
46838 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
46839 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
46840 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
46841 DECL_ARTIFICIAL (fndecl
) = 1;
46842 DECL_EXTERNAL (fndecl
) = 1;
46843 DECL_DECLARED_INLINE_P (fndecl
) = 1;
46844 DECL_IGNORED_P (fndecl
) = 1;
46845 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
46846 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
46847 DECL_ATTRIBUTES (fndecl
)
46848 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
46849 DECL_ATTRIBUTES (fndecl
));
46850 bool block_scope
= false;
46851 if (current_function_decl
)
46853 block_scope
= true;
46854 DECL_CONTEXT (fndecl
) = current_function_decl
;
46855 DECL_LOCAL_DECL_P (fndecl
) = true;
46858 if (processing_template_decl
)
46859 fndecl
= push_template_decl (fndecl
);
46863 if (!processing_template_decl
)
46866 else if (current_class_type
)
46870 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
46871 cp_lexer_consume_token (parser
->lexer
);
46872 cp
= cp_token_cache_new (first_token
,
46873 cp_lexer_peek_nth_token (parser
->lexer
,
46876 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
46877 finish_member_declaration (fndecl
);
46878 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
46879 DECL_PENDING_INLINE_P (fndecl
) = 1;
46880 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
46885 DECL_CONTEXT (fndecl
) = current_namespace
;
46886 tree d
= pushdecl (fndecl
);
46887 /* We should never meet a matched duplicate decl. */
46888 gcc_checking_assert (d
== error_mark_node
|| d
== fndecl
);
46891 tree block
= NULL_TREE
;
46893 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
46895 block
= begin_omp_structured_block ();
46898 cp_parser_push_lexer_for_tokens (parser
, cp
);
46899 parser
->lexer
->in_pragma
= true;
46902 bool ok
= cp_parser_omp_declare_reduction_exprs (fndecl
, parser
);
46905 cp_parser_pop_lexer (parser
);
46907 finish_function (/*inline_p=*/false);
46910 DECL_CONTEXT (fndecl
) = current_function_decl
;
46911 if (DECL_TEMPLATE_INFO (fndecl
))
46912 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl
)) = current_function_decl
;
46919 block
= finish_omp_structured_block (block
);
46920 if (TREE_CODE (block
) == BIND_EXPR
)
46921 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
46922 else if (TREE_CODE (block
) == STATEMENT_LIST
)
46923 DECL_SAVED_TREE (fndecl
) = block
;
46924 if (processing_template_decl
)
46925 add_decl_expr (fndecl
);
46928 cp_check_omp_declare_reduction (fndecl
);
46929 if (cp
== NULL
&& types
.length () > 1)
46930 cp
= cp_token_cache_new (first_token
,
46931 cp_lexer_peek_nth_token (parser
->lexer
, 2));
46932 if (errs
!= errorcount
)
46936 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46939 /* Free any declarators allocated. */
46940 obstack_free (&declarator_obstack
, p
);
46944 #pragma omp declare simd declare-simd-clauses[optseq] new-line
46945 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46946 initializer-clause[opt] new-line
46947 #pragma omp declare target new-line
46950 #pragma omp declare variant (identifier) match (context-selector) */
46953 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
46954 enum pragma_context context
)
46956 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46958 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46959 const char *p
= IDENTIFIER_POINTER (id
);
46961 if (strcmp (p
, "simd") == 0)
46963 cp_lexer_consume_token (parser
->lexer
);
46964 cp_parser_omp_declare_simd (parser
, pragma_tok
,
46968 if (flag_openmp
&& strcmp (p
, "variant") == 0)
46970 cp_lexer_consume_token (parser
->lexer
);
46971 cp_parser_omp_declare_simd (parser
, pragma_tok
,
46975 cp_ensure_no_omp_declare_simd (parser
);
46976 if (strcmp (p
, "reduction") == 0)
46978 cp_lexer_consume_token (parser
->lexer
);
46979 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
46983 if (!flag_openmp
) /* flag_openmp_simd */
46985 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46988 if (strcmp (p
, "target") == 0)
46990 cp_lexer_consume_token (parser
->lexer
);
46991 cp_parser_omp_declare_target (parser
, pragma_tok
);
46995 cp_parser_error (parser
, "expected %<simd%>, %<reduction%>, "
46996 "%<target%> or %<variant%>");
46997 cp_parser_require_pragma_eol (parser
, pragma_tok
);
47002 #pragma omp requires clauses[optseq] new-line */
47005 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
47008 enum omp_requires new_req
= (enum omp_requires
) 0;
47010 location_t loc
= pragma_tok
->location
;
47011 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
47013 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
47014 if ((!first
|| parser
->lexer
->in_omp_attribute_pragma
)
47015 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
47016 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
47017 cp_lexer_consume_token (parser
->lexer
);
47021 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
47023 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
47024 const char *p
= IDENTIFIER_POINTER (id
);
47025 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
47026 enum omp_requires this_req
= (enum omp_requires
) 0;
47028 if (!strcmp (p
, "unified_address"))
47029 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
47030 else if (!strcmp (p
, "unified_shared_memory"))
47031 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
47032 else if (!strcmp (p
, "dynamic_allocators"))
47033 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
47034 else if (!strcmp (p
, "reverse_offload"))
47035 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
47036 else if (!strcmp (p
, "atomic_default_mem_order"))
47038 cp_lexer_consume_token (parser
->lexer
);
47040 matching_parens parens
;
47041 if (parens
.require_open (parser
))
47043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
47045 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
47046 p
= IDENTIFIER_POINTER (id
);
47048 if (!strcmp (p
, "seq_cst"))
47050 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
47051 else if (!strcmp (p
, "relaxed"))
47053 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
47054 else if (!strcmp (p
, "acq_rel"))
47056 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
47060 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
47061 "expected %<seq_cst%>, %<relaxed%> or "
47063 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
47066 case CPP_PRAGMA_EOL
:
47067 case CPP_CLOSE_PAREN
:
47070 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
47072 cp_lexer_consume_token (parser
->lexer
);
47077 cp_lexer_consume_token (parser
->lexer
);
47079 if (!parens
.require_close (parser
))
47080 cp_parser_skip_to_closing_parenthesis (parser
,
47081 /*recovering=*/true,
47082 /*or_comma=*/false,
47088 cp_parser_require_pragma_eol (parser
, pragma_tok
);
47096 error_at (cloc
, "expected %<unified_address%>, "
47097 "%<unified_shared_memory%>, "
47098 "%<dynamic_allocators%>, "
47099 "%<reverse_offload%> "
47100 "or %<atomic_default_mem_order%> clause");
47101 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
47105 cp_lexer_consume_token (parser
->lexer
);
47108 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
47110 if ((this_req
& new_req
) != 0)
47111 error_at (cloc
, "too many %qs clauses", p
);
47112 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
47113 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
47114 error_at (cloc
, "%qs clause used lexically after first "
47115 "target construct or offloading API", p
);
47117 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
47119 error_at (cloc
, "too many %qs clauses",
47120 "atomic_default_mem_order");
47121 this_req
= (enum omp_requires
) 0;
47123 else if ((omp_requires_mask
47124 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
47126 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
47127 " clause in a single compilation unit");
47129 = (enum omp_requires
)
47131 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
47133 else if ((omp_requires_mask
47134 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
47135 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
47136 "lexically after first %<atomic%> construct "
47137 "without memory order clause");
47138 new_req
= (enum omp_requires
) (new_req
| this_req
);
47140 = (enum omp_requires
) (omp_requires_mask
| this_req
);
47146 cp_parser_require_pragma_eol (parser
, pragma_tok
);
47149 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
47155 #pragma omp nothing new-line */
47158 cp_parser_omp_nothing (cp_parser
*parser
, cp_token
*pragma_tok
)
47160 cp_parser_require_pragma_eol (parser
, pragma_tok
);
47165 #pragma omp error clauses[optseq] new-line */
47168 cp_parser_omp_error (cp_parser
*parser
, cp_token
*pragma_tok
,
47169 enum pragma_context context
)
47171 int at_compilation
= -1;
47172 int severity_fatal
= -1;
47173 tree message
= NULL_TREE
;
47176 location_t loc
= pragma_tok
->location
;
47178 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
47180 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
47181 if ((!first
|| parser
->lexer
->in_omp_attribute_pragma
)
47182 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
47183 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
47184 cp_lexer_consume_token (parser
->lexer
);
47188 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
47192 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
47193 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
47194 static const char *args
[] = {
47195 "execution", "compilation", "warning", "fatal"
47198 int idx
= 0, n
= -1;
47199 tree m
= NULL_TREE
;
47201 if (!strcmp (p
, "at"))
47202 v
= &at_compilation
;
47203 else if (!strcmp (p
, "severity"))
47205 v
= &severity_fatal
;
47208 else if (strcmp (p
, "message"))
47211 "expected %<at%>, %<severity%> or %<message%> clause");
47212 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
47216 cp_lexer_consume_token (parser
->lexer
);
47218 matching_parens parens
;
47219 if (parens
.require_open (parser
))
47223 m
= cp_parser_assignment_expression (parser
);
47224 if (type_dependent_expression_p (m
))
47225 m
= build1 (IMPLICIT_CONV_EXPR
, const_string_type_node
, m
);
47227 m
= perform_implicit_conversion_flags (const_string_type_node
, m
,
47228 tf_warning_or_error
,
47233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
47235 tree val
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
47236 const char *q
= IDENTIFIER_POINTER (val
);
47238 if (!strcmp (q
, args
[idx
]))
47240 else if (!strcmp (q
, args
[idx
+ 1]))
47245 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
47246 "expected %qs or %qs", args
[idx
], args
[idx
+ 1]);
47248 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
47251 case CPP_PRAGMA_EOL
:
47252 case CPP_CLOSE_PAREN
:
47255 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
47257 cp_lexer_consume_token (parser
->lexer
);
47262 cp_lexer_consume_token (parser
->lexer
);
47265 if (!parens
.require_close (parser
))
47266 cp_parser_skip_to_closing_parenthesis (parser
,
47267 /*recovering=*/true,
47268 /*or_comma=*/false,
47276 error_at (cloc
, "too many %qs clauses", p
);
47286 error_at (cloc
, "too many %qs clauses", p
);
47296 cp_parser_require_pragma_eol (parser
, pragma_tok
);
47300 if (at_compilation
== -1)
47301 at_compilation
= 1;
47302 if (severity_fatal
== -1)
47303 severity_fatal
= 1;
47304 if (!at_compilation
)
47306 if (context
!= pragma_compound
)
47308 error_at (loc
, "%<#pragma omp error%> with %<at(execution)%> clause "
47309 "may only be used in compound statements");
47313 = builtin_decl_explicit (severity_fatal
? BUILT_IN_GOMP_ERROR
47314 : BUILT_IN_GOMP_WARNING
);
47316 message
= build_zero_cst (const_string_type_node
);
47317 tree stmt
= build_call_expr_loc (loc
, fndecl
, 2, message
,
47318 build_all_ones_cst (size_type_node
));
47323 if (in_discarded_stmt
)
47326 const char *msg
= NULL
;
47329 msg
= c_getstr (fold_for_warn (message
));
47331 msg
= _("<message unknown at compile time>");
47334 emit_diagnostic (severity_fatal
? DK_ERROR
: DK_WARNING
, loc
, 0,
47335 "%<pragma omp error%> encountered: %s", msg
);
47337 emit_diagnostic (severity_fatal
? DK_ERROR
: DK_WARNING
, loc
, 0,
47338 "%<pragma omp error%> encountered");
47343 #pragma omp taskloop taskloop-clause[optseq] new-line
47346 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
47349 #define OMP_TASKLOOP_CLAUSE_MASK \
47350 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
47351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
47352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
47353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
47354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
47355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
47356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
47357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
47358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
47359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
47360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
47361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
47362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
47363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
47364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
47365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
47366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
47369 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
47370 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
47373 tree clauses
, sb
, ret
;
47375 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
47377 strcat (p_name
, " taskloop");
47378 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
47379 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
47381 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
47382 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
47384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
47386 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
47387 const char *p
= IDENTIFIER_POINTER (id
);
47389 if (strcmp (p
, "simd") == 0)
47391 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
47392 if (cclauses
== NULL
)
47393 cclauses
= cclauses_buf
;
47395 cp_lexer_consume_token (parser
->lexer
);
47396 if (!flag_openmp
) /* flag_openmp_simd */
47397 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
47399 sb
= begin_omp_structured_block ();
47400 save
= cp_parser_begin_omp_structured_block (parser
);
47401 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
47403 cp_parser_end_omp_structured_block (parser
, save
);
47404 tree body
= finish_omp_structured_block (sb
);
47407 ret
= make_node (OMP_TASKLOOP
);
47408 TREE_TYPE (ret
) = void_type_node
;
47409 OMP_FOR_BODY (ret
) = body
;
47410 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
47411 SET_EXPR_LOCATION (ret
, loc
);
47416 if (!flag_openmp
) /* flag_openmp_simd */
47418 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
47422 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
47426 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
47427 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
47430 keep_next_level (true);
47431 sb
= begin_omp_structured_block ();
47432 save
= cp_parser_begin_omp_structured_block (parser
);
47434 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
47437 cp_parser_end_omp_structured_block (parser
, save
);
47438 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
47445 # pragma acc routine oacc-routine-clause[optseq] new-line
47446 function-definition
47448 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
47451 #define OACC_ROUTINE_CLAUSE_MASK \
47452 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
47453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
47454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
47455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
47456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
47458 /* Parse the OpenACC routine pragma. This has an optional '( name )'
47459 component, which must resolve to a declared namespace-scope
47460 function. The clauses are either processed directly (for a named
47461 function), or defered until the immediatley following declaration
47465 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
47466 enum pragma_context context
)
47468 gcc_checking_assert (context
== pragma_external
);
47469 /* The checking for "another pragma following this one" in the "no optional
47470 '( name )'" case makes sure that we dont re-enter. */
47471 gcc_checking_assert (parser
->oacc_routine
== NULL
);
47473 cp_oacc_routine_data data
;
47474 data
.error_seen
= false;
47475 data
.fndecl_seen
= false;
47476 data
.tokens
= vNULL
;
47477 data
.clauses
= NULL_TREE
;
47478 data
.loc
= pragma_tok
->location
;
47479 /* It is safe to take the address of a local variable; it will only be
47480 used while this scope is live. */
47481 parser
->oacc_routine
= &data
;
47483 /* Look for optional '( name )'. */
47484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
47486 matching_parens parens
;
47487 parens
.consume_open (parser
); /* '(' */
47489 /* We parse the name as an id-expression. If it resolves to
47490 anything other than a non-overloaded function at namespace
47491 scope, it's an error. */
47492 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
47493 tree name
= cp_parser_id_expression (parser
,
47494 /*template_keyword_p=*/false,
47495 /*check_dependency_p=*/false,
47496 /*template_p=*/NULL
,
47497 /*declarator_p=*/false,
47498 /*optional_p=*/false);
47499 tree decl
= (identifier_p (name
)
47500 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
47502 if (name
!= error_mark_node
&& decl
== error_mark_node
)
47503 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
47505 if (decl
== error_mark_node
47506 || !parens
.require_close (parser
))
47508 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
47509 parser
->oacc_routine
= NULL
;
47514 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
47515 "#pragma acc routine",
47516 cp_lexer_peek_token (parser
->lexer
));
47517 /* The clauses are in reverse order; fix that to make later diagnostic
47518 emission easier. */
47519 data
.clauses
= nreverse (data
.clauses
);
47521 if (decl
&& is_overloaded_fn (decl
)
47522 && (TREE_CODE (decl
) != FUNCTION_DECL
47523 || DECL_FUNCTION_TEMPLATE_P (decl
)))
47525 error_at (name_loc
,
47526 "%<#pragma acc routine%> names a set of overloads");
47527 parser
->oacc_routine
= NULL
;
47531 /* Perhaps we should use the same rule as declarations in different
47533 if (!DECL_NAMESPACE_SCOPE_P (decl
))
47535 error_at (name_loc
,
47536 "%qD does not refer to a namespace scope function", decl
);
47537 parser
->oacc_routine
= NULL
;
47541 if (TREE_CODE (decl
) != FUNCTION_DECL
)
47543 error_at (name_loc
, "%qD does not refer to a function", decl
);
47544 parser
->oacc_routine
= NULL
;
47548 cp_finalize_oacc_routine (parser
, decl
, false);
47549 parser
->oacc_routine
= NULL
;
47551 else /* No optional '( name )'. */
47553 /* Store away all pragma tokens. */
47554 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
47555 cp_lexer_consume_token (parser
->lexer
);
47556 cp_parser_require_pragma_eol (parser
, pragma_tok
);
47557 struct cp_token_cache
*cp
47558 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
47559 parser
->oacc_routine
->tokens
.safe_push (cp
);
47561 /* Emit a helpful diagnostic if there's another pragma following this
47563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
47565 cp_ensure_no_oacc_routine (parser
);
47566 data
.tokens
.release ();
47567 /* ..., and then just keep going. */
47571 /* We only have to consider the pragma_external case here. */
47572 cp_parser_declaration (parser
, NULL_TREE
);
47573 if (parser
->oacc_routine
47574 && !parser
->oacc_routine
->fndecl_seen
)
47575 cp_ensure_no_oacc_routine (parser
);
47577 parser
->oacc_routine
= NULL
;
47578 data
.tokens
.release ();
47582 /* Finalize #pragma acc routine clauses after direct declarator has
47586 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
47588 struct cp_token_cache
*ce
;
47589 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
47591 if (!data
->error_seen
&& data
->fndecl_seen
)
47593 error_at (data
->loc
,
47594 "%<#pragma acc routine%> not immediately followed by "
47595 "a single function declaration or definition");
47596 data
->error_seen
= true;
47598 if (data
->error_seen
)
47601 gcc_checking_assert (data
->tokens
.length () == 1);
47602 ce
= data
->tokens
[0];
47604 cp_parser_push_lexer_for_tokens (parser
, ce
);
47605 parser
->lexer
->in_pragma
= true;
47606 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
47608 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
47609 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
47610 parser
->oacc_routine
->clauses
47611 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
47612 "#pragma acc routine", pragma_tok
);
47613 /* The clauses are in reverse order; fix that to make later diagnostic
47614 emission easier. */
47615 parser
->oacc_routine
->clauses
= nreverse (parser
->oacc_routine
->clauses
);
47616 cp_parser_pop_lexer (parser
);
47617 /* Later, cp_finalize_oacc_routine will process the clauses. */
47618 parser
->oacc_routine
->fndecl_seen
= true;
47623 /* Apply any saved OpenACC routine clauses to a just-parsed
47627 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
47629 if (UNLIKELY (parser
->oacc_routine
!= NULL
))
47631 /* Keep going if we're in error reporting mode. */
47632 if (parser
->oacc_routine
->error_seen
47633 || fndecl
== error_mark_node
)
47636 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
47638 if (parser
->oacc_routine
->fndecl_seen
)
47640 error_at (parser
->oacc_routine
->loc
,
47641 "%<#pragma acc routine%> not immediately followed by"
47642 " a single function declaration or definition");
47643 parser
->oacc_routine
= NULL
;
47647 cp_ensure_no_oacc_routine (parser
);
47652 = oacc_verify_routine_clauses (fndecl
, &parser
->oacc_routine
->clauses
,
47653 parser
->oacc_routine
->loc
,
47654 "#pragma acc routine");
47655 if (compatible
< 0)
47657 parser
->oacc_routine
= NULL
;
47660 if (compatible
> 0)
47665 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
47667 error_at (parser
->oacc_routine
->loc
,
47669 ? G_("%<#pragma acc routine%> must be applied before"
47671 : G_("%<#pragma acc routine%> must be applied before"
47673 parser
->oacc_routine
= NULL
;
47677 /* Set the routine's level of parallelism. */
47678 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
47679 oacc_replace_fn_attrib (fndecl
, dims
);
47681 /* Add an "omp declare target" attribute. */
47682 DECL_ATTRIBUTES (fndecl
)
47683 = tree_cons (get_identifier ("omp declare target"),
47684 parser
->oacc_routine
->clauses
,
47685 DECL_ATTRIBUTES (fndecl
));
47690 /* Main entry point to OpenMP statement pragmas. */
47693 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
47696 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
47697 omp_clause_mask
mask (0);
47699 switch (cp_parser_pragma_kind (pragma_tok
))
47701 case PRAGMA_OACC_ATOMIC
:
47702 cp_parser_omp_atomic (parser
, pragma_tok
, true);
47704 case PRAGMA_OACC_CACHE
:
47705 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
47707 case PRAGMA_OACC_DATA
:
47708 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
47710 case PRAGMA_OACC_ENTER_DATA
:
47711 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
47713 case PRAGMA_OACC_EXIT_DATA
:
47714 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
47716 case PRAGMA_OACC_HOST_DATA
:
47717 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
47719 case PRAGMA_OACC_KERNELS
:
47720 case PRAGMA_OACC_PARALLEL
:
47721 case PRAGMA_OACC_SERIAL
:
47722 strcpy (p_name
, "#pragma acc");
47723 stmt
= cp_parser_oacc_compute (parser
, pragma_tok
, p_name
, if_p
);
47725 case PRAGMA_OACC_LOOP
:
47726 strcpy (p_name
, "#pragma acc");
47727 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
47730 case PRAGMA_OACC_UPDATE
:
47731 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
47733 case PRAGMA_OACC_WAIT
:
47734 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
47736 case PRAGMA_OMP_ALLOCATE
:
47737 cp_parser_omp_allocate (parser
, pragma_tok
);
47739 case PRAGMA_OMP_ATOMIC
:
47740 cp_parser_omp_atomic (parser
, pragma_tok
, false);
47742 case PRAGMA_OMP_CRITICAL
:
47743 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
47745 case PRAGMA_OMP_DISTRIBUTE
:
47746 strcpy (p_name
, "#pragma omp");
47747 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
47750 case PRAGMA_OMP_FOR
:
47751 strcpy (p_name
, "#pragma omp");
47752 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
47755 case PRAGMA_OMP_LOOP
:
47756 strcpy (p_name
, "#pragma omp");
47757 stmt
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
47760 case PRAGMA_OMP_MASKED
:
47761 strcpy (p_name
, "#pragma omp");
47762 stmt
= cp_parser_omp_masked (parser
, pragma_tok
, p_name
, mask
, NULL
,
47765 case PRAGMA_OMP_MASTER
:
47766 strcpy (p_name
, "#pragma omp");
47767 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
47770 case PRAGMA_OMP_PARALLEL
:
47771 strcpy (p_name
, "#pragma omp");
47772 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
47775 case PRAGMA_OMP_SCOPE
:
47776 stmt
= cp_parser_omp_scope (parser
, pragma_tok
, if_p
);
47778 case PRAGMA_OMP_SECTIONS
:
47779 strcpy (p_name
, "#pragma omp");
47780 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
47782 case PRAGMA_OMP_SIMD
:
47783 strcpy (p_name
, "#pragma omp");
47784 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
47787 case PRAGMA_OMP_SINGLE
:
47788 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
47790 case PRAGMA_OMP_TASK
:
47791 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
47793 case PRAGMA_OMP_TASKGROUP
:
47794 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
47796 case PRAGMA_OMP_TASKLOOP
:
47797 strcpy (p_name
, "#pragma omp");
47798 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
47801 case PRAGMA_OMP_TEAMS
:
47802 strcpy (p_name
, "#pragma omp");
47803 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
47807 gcc_unreachable ();
47810 protected_set_expr_location (stmt
, pragma_tok
->location
);
47813 /* Transactional Memory parsing routines. */
47815 /* Parse a transaction attribute.
47821 We use this instead of cp_parser_attributes_opt for transactions to avoid
47822 the pedwarn in C++98 mode. */
47825 cp_parser_txn_attribute_opt (cp_parser
*parser
)
47828 tree attr_name
, attr
= NULL
;
47830 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
47831 return cp_parser_attributes_opt (parser
);
47833 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
47835 cp_lexer_consume_token (parser
->lexer
);
47836 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
47839 token
= cp_lexer_peek_token (parser
->lexer
);
47840 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
47842 token
= cp_lexer_consume_token (parser
->lexer
);
47844 attr_name
= (token
->type
== CPP_KEYWORD
47845 /* For keywords, use the canonical spelling,
47846 not the parsed identifier. */
47847 ? ridpointers
[(int) token
->keyword
]
47849 attr
= build_tree_list (attr_name
, NULL_TREE
);
47852 cp_parser_error (parser
, "expected identifier");
47854 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
47856 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
47860 /* Parse a __transaction_atomic or __transaction_relaxed statement.
47862 transaction-statement:
47863 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
47865 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
47869 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
47871 unsigned char old_in
= parser
->in_transaction
;
47872 unsigned char this_in
= 1, new_in
;
47873 enum rid keyword
= token
->keyword
;
47874 tree stmt
, attrs
, noex
;
47876 cp_lexer_consume_token (parser
->lexer
);
47878 if (keyword
== RID_TRANSACTION_RELAXED
47879 || keyword
== RID_SYNCHRONIZED
)
47880 this_in
|= TM_STMT_ATTR_RELAXED
;
47883 attrs
= cp_parser_txn_attribute_opt (parser
);
47885 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
47888 /* Parse a noexcept specification. */
47889 if (keyword
== RID_ATOMIC_NOEXCEPT
)
47890 noex
= boolean_true_node
;
47891 else if (keyword
== RID_ATOMIC_CANCEL
)
47893 /* cancel-and-throw is unimplemented. */
47894 sorry ("%<atomic_cancel%>");
47898 noex
= cp_parser_noexcept_specification_opt (parser
,
47899 CP_PARSER_FLAGS_NONE
,
47900 /*require_constexpr=*/true,
47901 /*consumed_expr=*/NULL
,
47902 /*return_cond=*/true);
47904 /* Keep track if we're in the lexical scope of an outer transaction. */
47905 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
47907 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
47909 parser
->in_transaction
= new_in
;
47910 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
47911 parser
->in_transaction
= old_in
;
47913 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
47918 /* Parse a __transaction_atomic or __transaction_relaxed expression.
47920 transaction-expression:
47921 __transaction_atomic txn-noexcept-spec[opt] ( expression )
47922 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
47926 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
47928 unsigned char old_in
= parser
->in_transaction
;
47929 unsigned char this_in
= 1;
47933 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
47935 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
47936 || keyword
== RID_TRANSACTION_RELAXED
);
47940 keyword
== RID_TRANSACTION_RELAXED
47941 ? G_("%<__transaction_relaxed%> without transactional memory "
47943 : G_("%<__transaction_atomic%> without transactional memory "
47944 "support enabled"));
47946 token
= cp_parser_require_keyword (parser
, keyword
,
47947 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
47948 : RT_TRANSACTION_RELAXED
));
47949 gcc_assert (token
!= NULL
);
47951 if (keyword
== RID_TRANSACTION_RELAXED
)
47952 this_in
|= TM_STMT_ATTR_RELAXED
;
47954 /* Set this early. This might mean that we allow transaction_cancel in
47955 an expression that we find out later actually has to be a constexpr.
47956 However, we expect that cxx_constant_value will be able to deal with
47957 this; also, if the noexcept has no constexpr, then what we parse next
47958 really is a transaction's body. */
47959 parser
->in_transaction
= this_in
;
47961 /* Parse a noexcept specification. */
47962 noex
= cp_parser_noexcept_specification_opt (parser
,
47963 CP_PARSER_FLAGS_NONE
,
47964 /*require_constexpr=*/false,
47966 /*return_cond=*/true);
47968 if (!noex
|| !noex_expr
47969 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
47971 matching_parens parens
;
47972 parens
.require_open (parser
);
47974 expr
= cp_parser_expression (parser
);
47975 expr
= finish_parenthesized_expr (expr
);
47977 parens
.require_close (parser
);
47981 /* The only expression that is available got parsed for the noexcept
47982 already. noexcept is true then. */
47984 noex
= boolean_true_node
;
47987 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
47988 parser
->in_transaction
= old_in
;
47990 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
47991 return error_mark_node
;
47993 return (flag_tm
? expr
: error_mark_node
);
47996 /* Parse a function-transaction-block.
47998 function-transaction-block:
47999 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
48001 __transaction_atomic txn-attribute[opt] function-try-block
48002 __transaction_relaxed ctor-initializer[opt] function-body
48003 __transaction_relaxed function-try-block
48007 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
48009 unsigned char old_in
= parser
->in_transaction
;
48010 unsigned char new_in
= 1;
48011 tree compound_stmt
, stmt
, attrs
;
48014 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
48015 || keyword
== RID_TRANSACTION_RELAXED
);
48016 token
= cp_parser_require_keyword (parser
, keyword
,
48017 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
48018 : RT_TRANSACTION_RELAXED
));
48019 gcc_assert (token
!= NULL
);
48021 if (keyword
== RID_TRANSACTION_RELAXED
)
48022 new_in
|= TM_STMT_ATTR_RELAXED
;
48025 attrs
= cp_parser_txn_attribute_opt (parser
);
48027 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
48030 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
48032 parser
->in_transaction
= new_in
;
48034 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
48035 cp_parser_function_try_block (parser
);
48037 cp_parser_ctor_initializer_opt_and_function_body
48038 (parser
, /*in_function_try_block=*/false);
48040 parser
->in_transaction
= old_in
;
48042 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
48045 /* Parse a __transaction_cancel statement.
48048 __transaction_cancel txn-attribute[opt] ;
48049 __transaction_cancel txn-attribute[opt] throw-expression ;
48051 ??? Cancel and throw is not yet implemented. */
48054 cp_parser_transaction_cancel (cp_parser
*parser
)
48057 bool is_outer
= false;
48060 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
48061 RT_TRANSACTION_CANCEL
);
48062 gcc_assert (token
!= NULL
);
48064 attrs
= cp_parser_txn_attribute_opt (parser
);
48066 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
48068 /* ??? Parse cancel-and-throw here. */
48070 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
48074 error_at (token
->location
, "%<__transaction_cancel%> without "
48075 "transactional memory support enabled");
48076 return error_mark_node
;
48078 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
48080 error_at (token
->location
, "%<__transaction_cancel%> within a "
48081 "%<__transaction_relaxed%>");
48082 return error_mark_node
;
48086 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
48087 && !is_tm_may_cancel_outer (current_function_decl
))
48089 error_at (token
->location
, "outer %<__transaction_cancel%> not "
48090 "within outer %<__transaction_atomic%>");
48091 error_at (token
->location
,
48092 " or a %<transaction_may_cancel_outer%> function");
48093 return error_mark_node
;
48096 else if (parser
->in_transaction
== 0)
48098 error_at (token
->location
, "%<__transaction_cancel%> not within "
48099 "%<__transaction_atomic%>");
48100 return error_mark_node
;
48103 stmt
= build_tm_abort_call (token
->location
, is_outer
);
48110 /* Special handling for the first token or line in the file. The first
48111 thing in the file might be #pragma GCC pch_preprocess, which loads a
48112 PCH file, which is a GC collection point. So we need to handle this
48113 first pragma without benefit of an existing lexer structure.
48115 Always returns one token to the caller in *FIRST_TOKEN. This is
48116 either the true first token of the file, or the first token after
48117 the initial pragma. */
48120 cp_parser_initial_pragma (cp_token
*first_token
)
48122 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
48125 cp_lexer_get_preprocessor_token (0, first_token
);
48128 if (first_token
->type
== CPP_STRING
)
48130 name
= first_token
->u
.value
;
48132 cp_lexer_get_preprocessor_token (0, first_token
);
48135 /* Skip to the end of the pragma. */
48136 if (first_token
->type
!= CPP_PRAGMA_EOL
)
48138 error_at (first_token
->location
,
48139 "malformed %<#pragma GCC pch_preprocess%>");
48141 cp_lexer_get_preprocessor_token (0, first_token
);
48142 while (first_token
->type
!= CPP_PRAGMA_EOL
);
48145 /* Now actually load the PCH file. */
48147 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
48149 /* Read one more token to return to our caller. We have to do this
48150 after reading the PCH file in, since its pointers have to be
48152 cp_lexer_get_preprocessor_token (0, first_token
);
48155 /* Parse a pragma GCC ivdep. */
48158 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
48160 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
48164 /* Parse a pragma GCC unroll. */
48166 static unsigned short
48167 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
48169 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
48170 tree expr
= cp_parser_constant_expression (parser
);
48171 unsigned short unroll
;
48172 expr
= maybe_constant_value (expr
);
48173 HOST_WIDE_INT lunroll
= 0;
48174 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
48175 || TREE_CODE (expr
) != INTEGER_CST
48176 || (lunroll
= tree_to_shwi (expr
)) < 0
48177 || lunroll
>= USHRT_MAX
)
48179 error_at (location
, "%<#pragma GCC unroll%> requires an"
48180 " assignment-expression that evaluates to a non-negative"
48181 " integral constant less than %u", USHRT_MAX
);
48186 unroll
= (unsigned short)lunroll
;
48190 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
48194 /* Normal parsing of a pragma token. Here we can (and must) use the
48198 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
48200 cp_token
*pragma_tok
;
48205 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
48206 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
48207 parser
->lexer
->in_pragma
= true;
48209 id
= cp_parser_pragma_kind (pragma_tok
);
48210 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
48211 cp_ensure_no_omp_declare_simd (parser
);
48214 case PRAGMA_GCC_PCH_PREPROCESS
:
48215 error_at (pragma_tok
->location
,
48216 "%<#pragma GCC pch_preprocess%> must be first");
48219 case PRAGMA_OMP_BARRIER
:
48222 case pragma_compound
:
48223 cp_parser_omp_barrier (parser
, pragma_tok
);
48226 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
48227 "used in compound statements", "omp barrier");
48235 case PRAGMA_OMP_DEPOBJ
:
48238 case pragma_compound
:
48239 cp_parser_omp_depobj (parser
, pragma_tok
);
48242 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
48243 "used in compound statements", "omp depobj");
48251 case PRAGMA_OMP_FLUSH
:
48254 case pragma_compound
:
48255 cp_parser_omp_flush (parser
, pragma_tok
);
48258 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
48259 "used in compound statements", "omp flush");
48267 case PRAGMA_OMP_TASKWAIT
:
48270 case pragma_compound
:
48271 cp_parser_omp_taskwait (parser
, pragma_tok
);
48274 error_at (pragma_tok
->location
,
48275 "%<#pragma %s%> may only be used in compound statements",
48284 case PRAGMA_OMP_TASKYIELD
:
48287 case pragma_compound
:
48288 cp_parser_omp_taskyield (parser
, pragma_tok
);
48291 error_at (pragma_tok
->location
,
48292 "%<#pragma %s%> may only be used in compound statements",
48301 case PRAGMA_OMP_CANCEL
:
48304 case pragma_compound
:
48305 cp_parser_omp_cancel (parser
, pragma_tok
);
48308 error_at (pragma_tok
->location
,
48309 "%<#pragma %s%> may only be used in compound statements",
48318 case PRAGMA_OMP_CANCELLATION_POINT
:
48319 return cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
48321 case PRAGMA_OMP_THREADPRIVATE
:
48322 cp_parser_omp_threadprivate (parser
, pragma_tok
);
48325 case PRAGMA_OMP_DECLARE
:
48326 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
48328 case PRAGMA_OACC_DECLARE
:
48329 cp_parser_oacc_declare (parser
, pragma_tok
);
48332 case PRAGMA_OACC_ENTER_DATA
:
48333 if (context
== pragma_stmt
)
48335 error_at (pragma_tok
->location
,
48336 "%<#pragma %s%> may only be used in compound statements",
48341 else if (context
!= pragma_compound
)
48343 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
48346 case PRAGMA_OACC_EXIT_DATA
:
48347 if (context
== pragma_stmt
)
48349 error_at (pragma_tok
->location
,
48350 "%<#pragma %s%> may only be used in compound statements",
48355 else if (context
!= pragma_compound
)
48357 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
48360 case PRAGMA_OACC_ROUTINE
:
48361 if (context
!= pragma_external
)
48363 error_at (pragma_tok
->location
,
48364 "%<#pragma acc routine%> must be at file scope");
48368 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
48371 case PRAGMA_OACC_UPDATE
:
48372 if (context
== pragma_stmt
)
48374 error_at (pragma_tok
->location
,
48375 "%<#pragma %s%> may only be used in compound statements",
48380 else if (context
!= pragma_compound
)
48382 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
48385 case PRAGMA_OACC_WAIT
:
48386 if (context
== pragma_stmt
)
48388 error_at (pragma_tok
->location
,
48389 "%<#pragma %s%> may only be used in compound statements",
48394 else if (context
!= pragma_compound
)
48396 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
48398 case PRAGMA_OMP_ALLOCATE
:
48399 cp_parser_omp_allocate (parser
, pragma_tok
);
48401 case PRAGMA_OACC_ATOMIC
:
48402 case PRAGMA_OACC_CACHE
:
48403 case PRAGMA_OACC_DATA
:
48404 case PRAGMA_OACC_HOST_DATA
:
48405 case PRAGMA_OACC_KERNELS
:
48406 case PRAGMA_OACC_LOOP
:
48407 case PRAGMA_OACC_PARALLEL
:
48408 case PRAGMA_OACC_SERIAL
:
48409 case PRAGMA_OMP_ATOMIC
:
48410 case PRAGMA_OMP_CRITICAL
:
48411 case PRAGMA_OMP_DISTRIBUTE
:
48412 case PRAGMA_OMP_FOR
:
48413 case PRAGMA_OMP_LOOP
:
48414 case PRAGMA_OMP_MASKED
:
48415 case PRAGMA_OMP_MASTER
:
48416 case PRAGMA_OMP_PARALLEL
:
48417 case PRAGMA_OMP_SCOPE
:
48418 case PRAGMA_OMP_SECTIONS
:
48419 case PRAGMA_OMP_SIMD
:
48420 case PRAGMA_OMP_SINGLE
:
48421 case PRAGMA_OMP_TASK
:
48422 case PRAGMA_OMP_TASKGROUP
:
48423 case PRAGMA_OMP_TASKLOOP
:
48424 case PRAGMA_OMP_TEAMS
:
48425 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
48427 stmt
= push_omp_privatization_clauses (false);
48428 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
48429 pop_omp_privatization_clauses (stmt
);
48432 case PRAGMA_OMP_REQUIRES
:
48433 if (context
!= pragma_external
)
48435 error_at (pragma_tok
->location
,
48436 "%<#pragma omp requires%> may only be used at file or "
48437 "namespace scope");
48441 return cp_parser_omp_requires (parser
, pragma_tok
);
48443 case PRAGMA_OMP_NOTHING
:
48444 cp_parser_omp_nothing (parser
, pragma_tok
);
48447 case PRAGMA_OMP_ERROR
:
48448 return cp_parser_omp_error (parser
, pragma_tok
, context
);
48450 case PRAGMA_OMP_ORDERED
:
48451 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
48453 stmt
= push_omp_privatization_clauses (false);
48454 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
48455 pop_omp_privatization_clauses (stmt
);
48458 case PRAGMA_OMP_TARGET
:
48459 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
48461 stmt
= push_omp_privatization_clauses (false);
48462 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
48463 pop_omp_privatization_clauses (stmt
);
48466 case PRAGMA_OMP_END_DECLARE_TARGET
:
48467 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
48470 case PRAGMA_OMP_SCAN
:
48471 error_at (pragma_tok
->location
,
48472 "%<#pragma omp scan%> may only be used in "
48473 "a loop construct with %<inscan%> %<reduction%> clause");
48476 case PRAGMA_OMP_SECTION
:
48477 error_at (pragma_tok
->location
,
48478 "%<#pragma omp section%> may only be used in "
48479 "%<#pragma omp sections%> construct");
48484 if (context
== pragma_external
)
48486 error_at (pragma_tok
->location
,
48487 "%<#pragma GCC ivdep%> must be inside a function");
48490 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
48491 unsigned short unroll
;
48492 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
48493 if (tok
->type
== CPP_PRAGMA
48494 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
48496 tok
= cp_lexer_consume_token (parser
->lexer
);
48497 unroll
= cp_parser_pragma_unroll (parser
, tok
);
48498 tok
= cp_lexer_peek_token (the_parser
->lexer
);
48502 if (tok
->type
!= CPP_KEYWORD
48503 || (tok
->keyword
!= RID_FOR
48504 && tok
->keyword
!= RID_WHILE
48505 && tok
->keyword
!= RID_DO
))
48507 cp_parser_error (parser
, "for, while or do statement expected");
48510 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
48514 case PRAGMA_UNROLL
:
48516 if (context
== pragma_external
)
48518 error_at (pragma_tok
->location
,
48519 "%<#pragma GCC unroll%> must be inside a function");
48522 const unsigned short unroll
48523 = cp_parser_pragma_unroll (parser
, pragma_tok
);
48525 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
48526 if (tok
->type
== CPP_PRAGMA
48527 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
48529 tok
= cp_lexer_consume_token (parser
->lexer
);
48530 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
48531 tok
= cp_lexer_peek_token (the_parser
->lexer
);
48535 if (tok
->type
!= CPP_KEYWORD
48536 || (tok
->keyword
!= RID_FOR
48537 && tok
->keyword
!= RID_WHILE
48538 && tok
->keyword
!= RID_DO
))
48540 cp_parser_error (parser
, "for, while or do statement expected");
48543 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
48548 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
48549 c_invoke_pragma_handler (id
);
48553 cp_parser_error (parser
, "expected declaration specifiers");
48557 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
48561 /* The interface the pragma parsers have to the lexer. */
48564 pragma_lex (tree
*value
, location_t
*loc
)
48566 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
48567 enum cpp_ttype ret
= tok
->type
;
48569 *value
= tok
->u
.value
;
48571 *loc
= tok
->location
;
48573 if (ret
== CPP_PRAGMA_EOL
)
48575 else if (ret
== CPP_STRING
)
48576 *value
= cp_parser_string_literal (the_parser
, false, false);
48579 if (ret
== CPP_KEYWORD
)
48581 cp_lexer_consume_token (the_parser
->lexer
);
48588 /* External interface. */
48590 /* Parse one entire translation unit. */
48593 c_parse_file (void)
48595 static bool already_called
= false;
48597 if (already_called
)
48598 fatal_error (input_location
,
48599 "multi-source compilation not implemented for C++");
48600 already_called
= true;
48602 /* cp_lexer_new_main is called before doing any GC allocation
48603 because tokenization might load a PCH file. */
48604 cp_lexer_new_main ();
48606 cp_parser_translation_unit (the_parser
);
48607 class_decl_loc_t::diag_mismatched_tags ();
48611 finish_translation_unit ();
48614 /* Create an identifier for a generic parameter type (a synthesized
48615 template parameter implied by `auto' or a concept identifier). */
48617 static GTY(()) int generic_parm_count
;
48619 make_generic_type_name ()
48622 sprintf (buf
, "auto:%d", ++generic_parm_count
);
48623 return get_identifier (buf
);
48626 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
48627 (creating a new template parameter list if necessary). Returns the newly
48628 created template type parm. */
48631 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
48633 /* A requires-clause is not a function and cannot have placeholders. */
48634 if (current_binding_level
->requires_expression
)
48636 error ("placeholder type not allowed in this context");
48637 return error_mark_node
;
48640 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
48642 /* We are either continuing a function template that already contains implicit
48643 template parameters, creating a new fully-implicit function template, or
48644 extending an existing explicit function template with implicit template
48647 cp_binding_level
*const entry_scope
= current_binding_level
;
48649 bool become_template
= false;
48650 cp_binding_level
*parent_scope
= 0;
48652 if (parser
->implicit_template_scope
)
48654 gcc_assert (parser
->implicit_template_parms
);
48656 current_binding_level
= parser
->implicit_template_scope
;
48660 /* Roll back to the existing template parameter scope (in the case of
48661 extending an explicit function template) or introduce a new template
48662 parameter scope ahead of the function parameter scope (or class scope
48663 in the case of out-of-line member definitions). The function scope is
48664 added back after template parameter synthesis below. */
48666 cp_binding_level
*scope
= entry_scope
;
48668 while (scope
->kind
== sk_function_parms
)
48670 parent_scope
= scope
;
48671 scope
= scope
->level_chain
;
48673 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
48675 /* If not defining a class, then any class scope is a scope level in
48676 an out-of-line member definition. In this case simply wind back
48677 beyond the first such scope to inject the template parameter list.
48678 Otherwise wind back to the class being defined. The latter can
48679 occur in class member friend declarations such as:
48685 friend void A::foo (auto);
48688 The template parameter list synthesized for the friend declaration
48689 must be injected in the scope of 'B'. This can also occur in
48690 erroneous cases such as:
48696 void B::foo (auto) {}
48699 Here the attempted definition of 'B::foo' within 'A' is ill-formed
48700 but, nevertheless, the template parameter list synthesized for the
48701 declarator should be injected into the scope of 'A' as if the
48702 ill-formed template was specified explicitly. */
48704 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
48706 parent_scope
= scope
;
48707 scope
= scope
->level_chain
;
48711 current_binding_level
= scope
;
48713 if (scope
->kind
!= sk_template_parms
48714 || !function_being_declared_is_template_p (parser
))
48716 /* Introduce a new template parameter list for implicit template
48719 become_template
= true;
48721 parser
->implicit_template_scope
48722 = begin_scope (sk_template_parms
, NULL
);
48724 ++processing_template_decl
;
48726 parser
->fully_implicit_function_template_p
= true;
48727 ++parser
->num_template_parameter_lists
;
48731 /* Synthesize implicit template parameters at the end of the explicit
48732 template parameter list. */
48734 gcc_assert (current_template_parms
);
48736 parser
->implicit_template_scope
= scope
;
48738 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
48739 parser
->implicit_template_parms
48740 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
48744 /* Synthesize a new template parameter and track the current template
48745 parameter chain with implicit_template_parms. */
48747 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
48748 tree synth_id
= make_generic_type_name ();
48749 tree synth_tmpl_parm
;
48750 bool non_type
= false;
48752 /* Synthesize the type template parameter. */
48753 gcc_assert(!proto
|| TREE_CODE (proto
) == TYPE_DECL
);
48754 synth_tmpl_parm
= finish_template_type_parm (class_type_node
, synth_id
);
48756 if (become_template
)
48757 current_template_parms
= tree_cons (size_int (current_template_depth
+ 1),
48758 NULL_TREE
, current_template_parms
);
48760 /* Attach the constraint to the parm before processing. */
48761 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
48762 TREE_TYPE (node
) = constr
;
48764 = process_template_parm (parser
->implicit_template_parms
,
48767 /*non_type=*/non_type
,
48768 /*param_pack=*/false);
48770 /* Mark the synthetic declaration "virtual". This is used when
48771 comparing template-heads to determine if whether an abbreviated
48772 function template is equivalent to an explicit template.
48774 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
48775 if (TREE_VALUE (new_parm
) != error_mark_node
)
48776 DECL_VIRTUAL_P (TREE_VALUE (new_parm
)) = true;
48778 // Chain the new parameter to the list of implicit parameters.
48779 if (parser
->implicit_template_parms
)
48780 parser
->implicit_template_parms
48781 = TREE_CHAIN (parser
->implicit_template_parms
);
48783 parser
->implicit_template_parms
= new_parm
;
48785 tree new_decl
= get_local_decls ();
48787 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
48788 new_decl
= DECL_INITIAL (new_decl
);
48790 /* If creating a fully implicit function template, start the new implicit
48791 template parameter list with this synthesized type, otherwise grow the
48792 current template parameter list. */
48794 if (become_template
)
48796 parent_scope
->level_chain
= current_binding_level
;
48798 tree new_parms
= make_tree_vec (1);
48799 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
48800 TREE_VALUE (current_template_parms
) = new_parms
;
48804 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
48805 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
48806 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
48807 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
48810 /* If the new parameter was constrained, we need to add that to the
48811 constraints in the template parameter list. */
48812 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
48814 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
48815 reqs
= combine_constraint_expressions (reqs
, req
);
48816 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
48819 current_binding_level
= entry_scope
;
48824 /* Finish the declaration of a fully implicit function template. Such a
48825 template has no explicit template parameter list so has not been through the
48826 normal template head and tail processing. synthesize_implicit_template_parm
48827 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
48828 provided if the declaration is a class member such that its template
48829 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
48830 form is returned. Otherwise NULL_TREE is returned. */
48833 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
48835 gcc_assert (parser
->fully_implicit_function_template_p
);
48837 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
48838 && DECL_VIRTUAL_P (member_decl_opt
))
48840 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
48841 "implicit templates may not be %<virtual%>");
48842 DECL_VIRTUAL_P (member_decl_opt
) = false;
48845 if (member_decl_opt
)
48846 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
48847 end_template_decl ();
48849 parser
->fully_implicit_function_template_p
= false;
48850 parser
->implicit_template_parms
= 0;
48851 parser
->implicit_template_scope
= 0;
48852 --parser
->num_template_parameter_lists
;
48854 return member_decl_opt
;
48857 /* Like finish_fully_implicit_template, but to be used in error
48858 recovery, rearranging scopes so that we restore the state we had
48859 before synthesize_implicit_template_parm inserted the implement
48860 template parms scope. */
48863 abort_fully_implicit_template (cp_parser
*parser
)
48865 cp_binding_level
*return_to_scope
= current_binding_level
;
48867 if (parser
->implicit_template_scope
48868 && return_to_scope
!= parser
->implicit_template_scope
)
48870 cp_binding_level
*child
= return_to_scope
;
48871 for (cp_binding_level
*scope
= child
->level_chain
;
48872 scope
!= parser
->implicit_template_scope
;
48873 scope
= child
->level_chain
)
48875 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
48876 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
48877 current_binding_level
= parser
->implicit_template_scope
;
48880 return_to_scope
= return_to_scope
->level_chain
;
48882 finish_fully_implicit_template (parser
, NULL
);
48884 gcc_assert (current_binding_level
== return_to_scope
);
48887 /* Helper function for diagnostics that have complained about things
48888 being used with 'extern "C"' linkage.
48890 Attempt to issue a note showing where the 'extern "C"' linkage began. */
48893 maybe_show_extern_c_location (void)
48895 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
48896 inform (the_parser
->innermost_linkage_specification_location
,
48897 "%<extern \"C\"%> linkage started here");
48900 #include "gt-cp-parser.h"