2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
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.c) 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 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 cp_lexer_new_main (void)
640 /* It's possible that parsing the first pragma will load a PCH file,
641 which is a GC collection point. So we have to do that before
642 allocating any memory. */
643 cp_lexer_get_preprocessor_token (0, &token
);
644 cp_parser_initial_pragma (&token
);
645 c_common_no_more_pch ();
647 cp_lexer
*lexer
= cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 cp_token
*tok
= lexer
->buffer
->quick_push (token
);
651 uintptr_t filter
= 0;
653 filter
= module_token_cdtor (parse_in
, filter
);
655 /* Get the remaining tokens from the preprocessor. */
656 while (tok
->type
!= CPP_EOF
)
659 /* Process the previous token. */
660 module_token_lang (tok
->type
, tok
->keyword
, tok
->u
.value
,
661 tok
->location
, filter
);
662 tok
= vec_safe_push (lexer
->buffer
, cp_token ());
663 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN
, tok
);
666 lexer
->next_token
= lexer
->buffer
->address ();
667 lexer
->last_token
= lexer
->next_token
668 + lexer
->buffer
->length ()
671 if (lexer
->buffer
->length () != 1)
673 /* Set the EOF token's location to be the just after the previous
674 token's range. That way 'at-eof' diagnostics point at something
676 auto range
= get_range_from_loc (line_table
, tok
[-1].location
);
678 = linemap_position_for_loc_and_offset (line_table
, range
.m_finish
, 1);
682 module_token_cdtor (parse_in
, filter
);
684 /* Subsequent preprocessor diagnostics should use compiler
685 diagnostic functions to get the compiler source location. */
688 maybe_check_all_macros (parse_in
);
690 gcc_assert (!lexer
->next_token
->purged_p
);
694 /* Create a new lexer whose token stream is primed with the tokens in
695 CACHE. When these tokens are exhausted, no new tokens will be read. */
698 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
700 cp_token
*first
= cache
->first
;
701 cp_token
*last
= cache
->last
;
702 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
704 /* We do not own the buffer. */
705 lexer
->buffer
= NULL
;
707 /* Insert an EOF token. */
708 lexer
->saved_type
= last
->type
;
709 lexer
->saved_keyword
= last
->keyword
;
710 last
->type
= CPP_EOF
;
711 last
->keyword
= RID_MAX
;
713 lexer
->next_token
= first
;
714 lexer
->last_token
= last
;
716 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
718 /* Initially we are not debugging. */
719 lexer
->debugging_p
= false;
721 gcc_assert (!lexer
->next_token
->purged_p
722 && !lexer
->last_token
->purged_p
);
726 /* Frees all resources associated with LEXER. */
729 cp_lexer_destroy (cp_lexer
*lexer
)
732 vec_free (lexer
->buffer
);
735 /* Restore the token we overwrite with EOF. */
736 lexer
->last_token
->type
= lexer
->saved_type
;
737 lexer
->last_token
->keyword
= lexer
->saved_keyword
;
739 lexer
->saved_tokens
.release ();
743 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
744 be used. The point of this flag is to help the compiler to fold away calls
745 to cp_lexer_debugging_p within this source file at compile time, when the
746 lexer is not being debugged. */
748 #define LEXER_DEBUGGING_ENABLED_P false
750 /* Returns nonzero if debugging information should be output. */
753 cp_lexer_debugging_p (cp_lexer
*lexer
)
755 if (!LEXER_DEBUGGING_ENABLED_P
)
758 return lexer
->debugging_p
;
762 static inline cp_token_position
763 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
765 return lexer
->next_token
- previous_p
;
768 static inline cp_token
*
769 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
775 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
777 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
780 static inline cp_token_position
781 cp_lexer_previous_token_position (cp_lexer
*lexer
)
783 return cp_lexer_token_position (lexer
, true);
786 static inline cp_token
*
787 cp_lexer_previous_token (cp_lexer
*lexer
)
789 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
791 /* Skip past purged tokens. */
794 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
798 return cp_lexer_token_at (lexer
, tp
);
801 /* Same as above, but return NULL when the lexer doesn't own the token
802 buffer or if the next_token is at the start of the token
803 vector or if all previous tokens are purged. */
806 cp_lexer_safe_previous_token (cp_lexer
*lexer
)
809 && lexer
->next_token
!= lexer
->buffer
->address ())
811 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
813 /* Skip past purged tokens. */
816 if (tp
== lexer
->buffer
->address ())
820 return cp_lexer_token_at (lexer
, tp
);
826 /* Overload for make_location, taking the lexer to mean the location of the
829 static inline location_t
830 make_location (location_t caret
, location_t start
, cp_lexer
*lexer
)
832 cp_token
*t
= cp_lexer_previous_token (lexer
);
833 return make_location (caret
, start
, t
->location
);
836 /* Overload for make_location taking tokens instead of locations. */
838 static inline location_t
839 make_location (cp_token
*caret
, cp_token
*start
, cp_token
*end
)
841 return make_location (caret
->location
, start
->location
, end
->location
);
844 /* nonzero if we are presently saving tokens. */
847 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
849 return lexer
->saved_tokens
.length () != 0;
852 /* Store the next token from the preprocessor in *TOKEN. Return true
853 if we reach EOF. If LEXER is NULL, assume we are handling an
854 initial #pragma pch_preprocess, and thus want the lexer to return
855 processed strings. */
858 cp_lexer_get_preprocessor_token (unsigned flags
, cp_token
*token
)
860 static int is_extern_c
= 0;
862 /* Get a new token from the preprocessor. */
864 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
866 token
->keyword
= RID_MAX
;
867 token
->purged_p
= false;
868 token
->error_reported
= false;
869 token
->tree_check_p
= false;
870 /* Usually never see a zero, but just in case ... */
871 token
->main_source_p
= line_table
->depth
<= 1;
873 /* On some systems, some header files are surrounded by an
874 implicit extern "C" block. Set a flag in the token if it
875 comes from such a header. */
876 is_extern_c
+= pending_lang_change
;
877 pending_lang_change
= 0;
878 token
->implicit_extern_c
= is_extern_c
> 0;
880 /* Check to see if this token is a keyword. */
881 if (token
->type
== CPP_NAME
)
883 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
885 /* Mark this token as a keyword. */
886 token
->type
= CPP_KEYWORD
;
887 /* Record which keyword. */
888 token
->keyword
= C_RID_CODE (token
->u
.value
);
892 if (warn_cxx11_compat
893 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
894 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
896 /* Warn about the C++0x keyword (but still treat it as
898 warning_at (token
->location
, OPT_Wc__11_compat
,
899 "identifier %qE is a keyword in C++11",
902 /* Clear out the C_RID_CODE so we don't warn about this
903 particular identifier-turned-keyword again. */
904 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
906 if (warn_cxx20_compat
907 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX20
908 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX20
)
910 /* Warn about the C++20 keyword (but still treat it as
912 warning_at (token
->location
, OPT_Wc__20_compat
,
913 "identifier %qE is a keyword in C++20",
916 /* Clear out the C_RID_CODE so we don't warn about this
917 particular identifier-turned-keyword again. */
918 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
921 token
->keyword
= RID_MAX
;
924 else if (token
->type
== CPP_AT_NAME
)
926 /* This only happens in Objective-C++; it must be a keyword. */
927 token
->type
= CPP_KEYWORD
;
928 switch (C_RID_CODE (token
->u
.value
))
930 /* Replace 'class' with '@class', 'private' with '@private',
931 etc. This prevents confusion with the C++ keyword
932 'class', and makes the tokens consistent with other
933 Objective-C 'AT' keywords. For example '@class' is
934 reported as RID_AT_CLASS which is consistent with
935 '@synchronized', which is reported as
938 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
939 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
940 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
941 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
942 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
943 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
944 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
945 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
946 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
951 /* Update the globals input_location and the input file stack from TOKEN. */
953 cp_lexer_set_source_position_from_token (cp_token
*token
)
955 input_location
= token
->location
;
958 /* Update the globals input_location and the input file stack from LEXER. */
960 cp_lexer_set_source_position (cp_lexer
*lexer
)
962 cp_token
*token
= cp_lexer_peek_token (lexer
);
963 cp_lexer_set_source_position_from_token (token
);
966 /* Return a pointer to the next token in the token stream, but do not
969 static inline cp_token
*
970 cp_lexer_peek_token (cp_lexer
*lexer
)
972 if (cp_lexer_debugging_p (lexer
))
974 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
975 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
976 putc ('\n', cp_lexer_debug_stream
);
978 return lexer
->next_token
;
981 /* Return true if the next token has the indicated TYPE. */
984 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
986 return cp_lexer_peek_token (lexer
)->type
== type
;
989 /* Return true if the next token does not have the indicated TYPE. */
992 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
994 return !cp_lexer_next_token_is (lexer
, type
);
997 /* Return true if the next token is the indicated KEYWORD. */
1000 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
1002 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
1006 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
1008 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
1012 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
1014 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
1017 /* Return true if KEYWORD can start a decl-specifier. */
1020 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
1024 /* auto specifier: storage-class-specifier in C++,
1025 simple-type-specifier in C++0x. */
1027 /* Storage classes. */
1033 /* Elaborated type specifiers. */
1039 /* Simple type specifiers. */
1054 /* CV qualifiers. */
1057 /* Function specifiers. */
1060 /* friend/typdef/inline specifiers. */
1064 /* GNU extensions. */
1066 /* C++11 extensions. */
1068 case RID_UNDERLYING_TYPE
:
1070 /* C++20 extensions. */
1076 if (keyword
>= RID_FIRST_INT_N
1077 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
1078 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
1084 /* Return true if the next token is a keyword for a decl-specifier. */
1087 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
1091 token
= cp_lexer_peek_token (lexer
);
1092 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
1095 /* Returns TRUE iff the token T begins a decltype type. */
1098 token_is_decltype (cp_token
*t
)
1100 return (t
->keyword
== RID_DECLTYPE
1101 || t
->type
== CPP_DECLTYPE
);
1104 /* Returns TRUE iff the next token begins a decltype type. */
1107 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1109 cp_token
*t
= cp_lexer_peek_token (lexer
);
1110 return token_is_decltype (t
);
1113 /* Called when processing a token with tree_check_value; perform or defer the
1114 associated checks and return the value. */
1117 saved_checks_value (struct tree_check
*check_value
)
1119 /* Perform any access checks that were deferred. */
1120 vec
<deferred_access_check
, va_gc
> *checks
;
1121 deferred_access_check
*chk
;
1122 checks
= check_value
->checks
;
1126 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1127 perform_or_defer_access_check (chk
->binfo
,
1129 chk
->diag_decl
, tf_warning_or_error
);
1131 /* Return the stored value. */
1132 return check_value
->value
;
1135 /* Return a pointer to the Nth token in the token stream. If N is 1,
1136 then this is precisely equivalent to cp_lexer_peek_token (except
1137 that it is not inline). One would like to disallow that case, but
1138 there is one case (cp_parser_nth_token_starts_template_id) where
1139 the caller passes a variable for N and it might be 1. */
1142 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1146 /* N is 1-based, not zero-based. */
1149 if (cp_lexer_debugging_p (lexer
))
1150 fprintf (cp_lexer_debug_stream
,
1151 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1154 token
= lexer
->next_token
;
1155 while (n
&& token
->type
!= CPP_EOF
)
1158 if (!token
->purged_p
)
1162 if (cp_lexer_debugging_p (lexer
))
1164 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1165 putc ('\n', cp_lexer_debug_stream
);
1171 /* Return the next token, and advance the lexer's next_token pointer
1172 to point to the next non-purged token. */
1175 cp_lexer_consume_token (cp_lexer
* lexer
)
1177 cp_token
*token
= lexer
->next_token
;
1181 gcc_assert (token
->type
!= CPP_EOF
);
1182 lexer
->next_token
++;
1184 while (lexer
->next_token
->purged_p
);
1186 cp_lexer_set_source_position_from_token (token
);
1188 /* Provide debugging output. */
1189 if (cp_lexer_debugging_p (lexer
))
1191 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1192 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1193 putc ('\n', cp_lexer_debug_stream
);
1199 /* Permanently remove the next token from the token stream, and
1200 advance the next_token pointer to refer to the next non-purged
1204 cp_lexer_purge_token (cp_lexer
*lexer
)
1206 cp_token
*tok
= lexer
->next_token
;
1208 gcc_assert (tok
->type
!= CPP_EOF
);
1209 tok
->purged_p
= true;
1210 tok
->location
= UNKNOWN_LOCATION
;
1211 tok
->u
.value
= NULL_TREE
;
1212 tok
->keyword
= RID_MAX
;
1216 while (tok
->purged_p
);
1217 lexer
->next_token
= tok
;
1220 /* Permanently remove all tokens after TOK, up to, but not
1221 including, the token that will be returned next by
1222 cp_lexer_peek_token. */
1225 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1227 cp_token
*peek
= lexer
->next_token
;
1229 gcc_assert (tok
< peek
);
1231 for (tok
++; tok
!= peek
; tok
++)
1233 tok
->purged_p
= true;
1234 tok
->location
= UNKNOWN_LOCATION
;
1235 tok
->u
.value
= NULL_TREE
;
1236 tok
->keyword
= RID_MAX
;
1240 /* Begin saving tokens. All tokens consumed after this point will be
1244 cp_lexer_save_tokens (cp_lexer
* lexer
)
1246 /* Provide debugging output. */
1247 if (cp_lexer_debugging_p (lexer
))
1248 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1250 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1253 /* Commit to the portion of the token stream most recently saved. */
1256 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1258 /* Provide debugging output. */
1259 if (cp_lexer_debugging_p (lexer
))
1260 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1262 lexer
->saved_tokens
.pop ();
1265 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1266 to the token stream. Stop saving tokens. */
1269 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1271 /* Provide debugging output. */
1272 if (cp_lexer_debugging_p (lexer
))
1273 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1275 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1278 /* RAII wrapper around the above functions, with sanity checking. Creating
1279 a variable saves tokens, which are committed when the variable is
1280 destroyed unless they are explicitly rolled back by calling the rollback
1283 struct saved_token_sentinel
1288 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1290 len
= lexer
->saved_tokens
.length ();
1291 cp_lexer_save_tokens (lexer
);
1295 cp_lexer_rollback_tokens (lexer
);
1298 ~saved_token_sentinel()
1301 cp_lexer_commit_tokens (lexer
);
1302 gcc_assert (lexer
->saved_tokens
.length () == len
);
1306 /* Print a representation of the TOKEN on the STREAM. */
1309 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1311 /* We don't use cpp_type2name here because the parser defines
1312 a few tokens of its own. */
1313 static const char *const token_names
[] = {
1314 /* cpplib-defined token types */
1315 #define OP(e, s) #e,
1316 #define TK(e, s) #e,
1320 /* C++ parser token types - see "Manifest constants", above. */
1323 "NESTED_NAME_SPECIFIER",
1326 /* For some tokens, print the associated data. */
1327 switch (token
->type
)
1330 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1331 For example, `struct' is mapped to an INTEGER_CST. */
1332 if (!identifier_p (token
->u
.value
))
1336 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1343 case CPP_UTF8STRING
:
1344 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1348 print_generic_expr (stream
, token
->u
.value
);
1352 /* If we have a name for the token, print it out. Otherwise, we
1353 simply give the numeric code. */
1354 if (token
->type
< ARRAY_SIZE(token_names
))
1355 fputs (token_names
[token
->type
], stream
);
1357 fprintf (stream
, "[%d]", token
->type
);
1363 debug (cp_token
&ref
)
1365 cp_lexer_print_token (stderr
, &ref
);
1366 fprintf (stderr
, "\n");
1370 debug (cp_token
*ptr
)
1375 fprintf (stderr
, "<nil>\n");
1379 /* Start emitting debugging information. */
1382 cp_lexer_start_debugging (cp_lexer
* lexer
)
1384 if (!LEXER_DEBUGGING_ENABLED_P
)
1385 fatal_error (input_location
,
1386 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1388 lexer
->debugging_p
= true;
1389 cp_lexer_debug_stream
= stderr
;
1392 /* Stop emitting debugging information. */
1395 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1397 if (!LEXER_DEBUGGING_ENABLED_P
)
1398 fatal_error (input_location
,
1399 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1401 lexer
->debugging_p
= false;
1402 cp_lexer_debug_stream
= NULL
;
1405 /* Create a new cp_token_cache, representing a range of tokens. */
1407 static cp_token_cache
*
1408 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1410 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1411 cache
->first
= first
;
1416 /* Diagnose if #pragma omp declare simd isn't followed immediately
1417 by function declaration or definition. */
1420 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1422 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1424 error ("%<#pragma omp declare %s%> not immediately followed by "
1425 "function declaration or definition",
1426 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
1427 parser
->omp_declare_simd
= NULL
;
1431 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1432 and put that into "omp declare simd" attribute. */
1435 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1437 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1439 if (fndecl
== error_mark_node
)
1441 parser
->omp_declare_simd
= NULL
;
1444 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1446 cp_ensure_no_omp_declare_simd (parser
);
1452 /* Similarly, but for use in declaration parsing functions
1453 which call cp_parser_handle_directive_omp_attributes. */
1456 cp_finalize_omp_declare_simd (cp_parser
*parser
, cp_omp_declare_simd_data
*data
)
1458 if (parser
->omp_declare_simd
!= data
)
1461 if (!parser
->omp_declare_simd
->error_seen
1462 && !parser
->omp_declare_simd
->fndecl_seen
)
1463 error_at (parser
->omp_declare_simd
->loc
,
1464 "%<declare %s%> directive not immediately followed by "
1465 "function declaration or definition",
1466 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
1467 parser
->omp_declare_simd
= NULL
;
1470 /* Diagnose if #pragma acc routine isn't followed immediately by function
1471 declaration or definition. */
1474 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1476 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1478 error_at (parser
->oacc_routine
->loc
,
1479 "%<#pragma acc routine%> not immediately followed by "
1480 "function declaration or definition");
1481 parser
->oacc_routine
= NULL
;
1485 /* Decl-specifiers. */
1487 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1490 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1492 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1497 /* Nothing other than the parser should be creating declarators;
1498 declarators are a semi-syntactic representation of C++ entities.
1499 Other parts of the front end that need to create entities (like
1500 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1502 static cp_declarator
*make_call_declarator
1503 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
,
1504 tree
, tree
, tree
, tree
, location_t
);
1505 static cp_declarator
*make_array_declarator
1506 (cp_declarator
*, tree
);
1507 static cp_declarator
*make_pointer_declarator
1508 (cp_cv_quals
, cp_declarator
*, tree
);
1509 static cp_declarator
*make_reference_declarator
1510 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1511 static cp_declarator
*make_ptrmem_declarator
1512 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1514 /* An erroneous declarator. */
1515 static cp_declarator
*cp_error_declarator
;
1517 /* The obstack on which declarators and related data structures are
1519 static struct obstack declarator_obstack
;
1521 /* Alloc BYTES from the declarator memory pool. */
1523 static inline void *
1524 alloc_declarator (size_t bytes
)
1526 return obstack_alloc (&declarator_obstack
, bytes
);
1529 /* Allocate a declarator of the indicated KIND. Clear fields that are
1530 common to all declarators. */
1532 static cp_declarator
*
1533 make_declarator (cp_declarator_kind kind
)
1535 cp_declarator
*declarator
;
1537 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1538 declarator
->kind
= kind
;
1539 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1540 declarator
->attributes
= NULL_TREE
;
1541 declarator
->std_attributes
= NULL_TREE
;
1542 declarator
->declarator
= NULL
;
1543 declarator
->parameter_pack_p
= false;
1544 declarator
->id_loc
= UNKNOWN_LOCATION
;
1545 declarator
->init_loc
= UNKNOWN_LOCATION
;
1550 /* Make a declarator for a generalized identifier. If
1551 QUALIFYING_SCOPE is non-NULL, the identifier is
1552 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1553 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1556 static cp_declarator
*
1557 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1558 special_function_kind sfk
, location_t id_location
)
1560 cp_declarator
*declarator
;
1562 /* It is valid to write:
1564 class C { void f(); };
1568 The standard is not clear about whether `typedef const C D' is
1569 legal; as of 2002-09-15 the committee is considering that
1570 question. EDG 3.0 allows that syntax. Therefore, we do as
1572 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1573 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1575 gcc_assert (identifier_p (unqualified_name
)
1576 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1577 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1579 declarator
= make_declarator (cdk_id
);
1580 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1581 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1582 declarator
->u
.id
.sfk
= sfk
;
1583 declarator
->id_loc
= id_location
;
1588 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1589 of modifiers such as const or volatile to apply to the pointer
1590 type, represented as identifiers. ATTRIBUTES represent the attributes that
1591 appertain to the pointer or reference. */
1594 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1597 cp_declarator
*declarator
;
1599 declarator
= make_declarator (cdk_pointer
);
1600 declarator
->declarator
= target
;
1601 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1602 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1605 declarator
->id_loc
= target
->id_loc
;
1606 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1607 target
->parameter_pack_p
= false;
1610 declarator
->parameter_pack_p
= false;
1612 declarator
->std_attributes
= attributes
;
1617 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1618 represent the attributes that appertain to the pointer or
1622 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1623 bool rvalue_ref
, tree attributes
)
1625 cp_declarator
*declarator
;
1627 declarator
= make_declarator (cdk_reference
);
1628 declarator
->declarator
= target
;
1629 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1630 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1633 declarator
->id_loc
= target
->id_loc
;
1634 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1635 target
->parameter_pack_p
= false;
1638 declarator
->parameter_pack_p
= false;
1640 declarator
->std_attributes
= attributes
;
1645 /* Like make_pointer_declarator -- but for a pointer to a non-static
1646 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1647 appertain to the pointer or reference. */
1650 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1651 cp_declarator
*pointee
,
1654 cp_declarator
*declarator
;
1656 declarator
= make_declarator (cdk_ptrmem
);
1657 declarator
->declarator
= pointee
;
1658 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1659 declarator
->u
.pointer
.class_type
= class_type
;
1663 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1664 pointee
->parameter_pack_p
= false;
1667 declarator
->parameter_pack_p
= false;
1669 declarator
->std_attributes
= attributes
;
1674 /* Make a declarator for the function given by TARGET, with the
1675 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1676 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1677 indicates what exceptions can be thrown. */
1680 make_call_declarator (cp_declarator
*target
,
1682 cp_cv_quals cv_qualifiers
,
1683 cp_virt_specifiers virt_specifiers
,
1684 cp_ref_qualifier ref_qualifier
,
1686 tree exception_specification
,
1687 tree late_return_type
,
1688 tree requires_clause
,
1689 location_t parens_loc
)
1691 cp_declarator
*declarator
;
1693 declarator
= make_declarator (cdk_function
);
1694 declarator
->declarator
= target
;
1695 declarator
->u
.function
.parameters
= parms
;
1696 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1697 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1698 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1699 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1700 declarator
->u
.function
.exception_specification
= exception_specification
;
1701 declarator
->u
.function
.late_return_type
= late_return_type
;
1702 declarator
->u
.function
.requires_clause
= requires_clause
;
1703 declarator
->u
.function
.parens_loc
= parens_loc
;
1706 declarator
->id_loc
= target
->id_loc
;
1707 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1708 target
->parameter_pack_p
= false;
1711 declarator
->parameter_pack_p
= false;
1716 /* Make a declarator for an array of BOUNDS elements, each of which is
1717 defined by ELEMENT. */
1720 make_array_declarator (cp_declarator
*element
, tree bounds
)
1722 cp_declarator
*declarator
;
1724 declarator
= make_declarator (cdk_array
);
1725 declarator
->declarator
= element
;
1726 declarator
->u
.array
.bounds
= bounds
;
1729 declarator
->id_loc
= element
->id_loc
;
1730 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1731 element
->parameter_pack_p
= false;
1734 declarator
->parameter_pack_p
= false;
1739 /* Determine whether the declarator we've seen so far can be a
1740 parameter pack, when followed by an ellipsis. */
1742 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1744 if (declarator
&& declarator
->parameter_pack_p
)
1745 /* We already saw an ellipsis. */
1748 /* Search for a declarator name, or any other declarator that goes
1749 after the point where the ellipsis could appear in a parameter
1750 pack. If we find any of these, then this declarator cannot be
1751 made into a parameter pack. */
1753 while (declarator
&& !found
)
1755 switch ((int)declarator
->kind
)
1767 declarator
= declarator
->declarator
;
1775 cp_parameter_declarator
*no_parameters
;
1777 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1778 DECLARATOR and DEFAULT_ARGUMENT. */
1780 cp_parameter_declarator
*
1781 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1782 cp_declarator
*declarator
,
1783 tree default_argument
,
1785 bool template_parameter_pack_p
= false)
1787 cp_parameter_declarator
*parameter
;
1789 parameter
= ((cp_parameter_declarator
*)
1790 alloc_declarator (sizeof (cp_parameter_declarator
)));
1791 parameter
->next
= NULL
;
1792 if (decl_specifiers
)
1793 parameter
->decl_specifiers
= *decl_specifiers
;
1795 clear_decl_specs (¶meter
->decl_specifiers
);
1796 parameter
->declarator
= declarator
;
1797 parameter
->default_argument
= default_argument
;
1798 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1799 parameter
->loc
= loc
;
1804 /* Returns true iff DECLARATOR is a declaration for a function. */
1807 function_declarator_p (const cp_declarator
*declarator
)
1811 if (declarator
->kind
== cdk_function
1812 && declarator
->declarator
->kind
== cdk_id
)
1814 if (declarator
->kind
== cdk_id
1815 || declarator
->kind
== cdk_decomp
1816 || declarator
->kind
== cdk_error
)
1818 declarator
= declarator
->declarator
;
1828 A cp_parser parses the token stream as specified by the C++
1829 grammar. Its job is purely parsing, not semantic analysis. For
1830 example, the parser breaks the token stream into declarators,
1831 expressions, statements, and other similar syntactic constructs.
1832 It does not check that the types of the expressions on either side
1833 of an assignment-statement are compatible, or that a function is
1834 not declared with a parameter of type `void'.
1836 The parser invokes routines elsewhere in the compiler to perform
1837 semantic analysis and to build up the abstract syntax tree for the
1840 The parser (and the template instantiation code, which is, in a
1841 way, a close relative of parsing) are the only parts of the
1842 compiler that should be calling push_scope and pop_scope, or
1843 related functions. The parser (and template instantiation code)
1844 keeps track of what scope is presently active; everything else
1845 should simply honor that. (The code that generates static
1846 initializers may also need to set the scope, in order to check
1847 access control correctly when emitting the initializers.)
1852 The parser is of the standard recursive-descent variety. Upcoming
1853 tokens in the token stream are examined in order to determine which
1854 production to use when parsing a non-terminal. Some C++ constructs
1855 require arbitrary look ahead to disambiguate. For example, it is
1856 impossible, in the general case, to tell whether a statement is an
1857 expression or declaration without scanning the entire statement.
1858 Therefore, the parser is capable of "parsing tentatively." When the
1859 parser is not sure what construct comes next, it enters this mode.
1860 Then, while we attempt to parse the construct, the parser queues up
1861 error messages, rather than issuing them immediately, and saves the
1862 tokens it consumes. If the construct is parsed successfully, the
1863 parser "commits", i.e., it issues any queued error messages and
1864 the tokens that were being preserved are permanently discarded.
1865 If, however, the construct is not parsed successfully, the parser
1866 rolls back its state completely so that it can resume parsing using
1867 a different alternative.
1872 The performance of the parser could probably be improved substantially.
1873 We could often eliminate the need to parse tentatively by looking ahead
1874 a little bit. In some places, this approach might not entirely eliminate
1875 the need to parse tentatively, but it might still speed up the average
1878 /* Flags that are passed to some parsing functions. These values can
1879 be bitwise-ored together. */
1884 CP_PARSER_FLAGS_NONE
= 0x0,
1885 /* The construct is optional. If it is not present, then no error
1886 should be issued. */
1887 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1888 /* When parsing a type-specifier, treat user-defined type-names
1889 as non-type identifiers. */
1890 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1891 /* When parsing a type-specifier, do not try to parse a class-specifier
1892 or enum-specifier. */
1893 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1894 /* When parsing a decl-specifier-seq, only allow type-specifier or
1896 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1897 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1898 for C++20 consteval. */
1899 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10,
1900 /* When parsing a decl-specifier-seq, allow missing typename. */
1901 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20,
1902 /* When parsing of the noexcept-specifier should be delayed. */
1903 CP_PARSER_FLAGS_DELAY_NOEXCEPT
= 0x40,
1904 /* When parsing a consteval declarator. */
1905 CP_PARSER_FLAGS_CONSTEVAL
= 0x80
1908 /* This type is used for parameters and variables which hold
1909 combinations of the above flags. */
1910 typedef int cp_parser_flags
;
1912 /* The different kinds of declarators we want to parse. */
1914 enum cp_parser_declarator_kind
1916 /* We want an abstract declarator. */
1917 CP_PARSER_DECLARATOR_ABSTRACT
,
1918 /* We want a named declarator. */
1919 CP_PARSER_DECLARATOR_NAMED
,
1920 /* We don't mind, but the name must be an unqualified-id. */
1921 CP_PARSER_DECLARATOR_EITHER
1924 /* The precedence values used to parse binary expressions. The minimum value
1925 of PREC must be 1, because zero is reserved to quickly discriminate
1926 binary operators from other tokens. */
1931 PREC_LOGICAL_OR_EXPRESSION
,
1932 PREC_LOGICAL_AND_EXPRESSION
,
1933 PREC_INCLUSIVE_OR_EXPRESSION
,
1934 PREC_EXCLUSIVE_OR_EXPRESSION
,
1935 PREC_AND_EXPRESSION
,
1936 PREC_EQUALITY_EXPRESSION
,
1937 PREC_RELATIONAL_EXPRESSION
,
1938 PREC_SPACESHIP_EXPRESSION
,
1939 PREC_SHIFT_EXPRESSION
,
1940 PREC_ADDITIVE_EXPRESSION
,
1941 PREC_MULTIPLICATIVE_EXPRESSION
,
1943 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1946 /* A mapping from a token type to a corresponding tree node type, with a
1947 precedence value. */
1949 struct cp_parser_binary_operations_map_node
1951 /* The token type. */
1952 enum cpp_ttype token_type
;
1953 /* The corresponding tree code. */
1954 enum tree_code tree_type
;
1955 /* The precedence of this operator. */
1956 enum cp_parser_prec prec
;
1959 struct cp_parser_expression_stack_entry
1961 /* Left hand side of the binary operation we are currently
1964 /* Original tree code for left hand side, if it was a binary
1965 expression itself (used for -Wparentheses). */
1966 enum tree_code lhs_type
;
1967 /* Tree code for the binary operation we are parsing. */
1968 enum tree_code tree_type
;
1969 /* Precedence of the binary operation we are parsing. */
1970 enum cp_parser_prec prec
;
1971 /* Location of the binary operation we are parsing. */
1975 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1976 entries because precedence levels on the stack are monotonically
1978 typedef struct cp_parser_expression_stack_entry
1979 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1983 /* Constructors and destructors. */
1985 static cp_parser_context
*cp_parser_context_new
1986 (cp_parser_context
*);
1988 /* Class variables. */
1990 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1992 /* The operator-precedence table used by cp_parser_binary_expression.
1993 Transformed into an associative array (binops_by_token) by
1996 static const cp_parser_binary_operations_map_node binops
[] = {
1997 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1998 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
2000 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
2001 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
2002 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
2004 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
2005 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
2007 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
2008 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
2010 { CPP_SPACESHIP
, SPACESHIP_EXPR
, PREC_SPACESHIP_EXPRESSION
},
2012 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
2013 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
2014 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
2015 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
2017 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
2018 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
2020 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
2022 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
2024 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
2026 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
2028 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
2031 /* The same as binops, but initialized by cp_parser_new so that
2032 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2034 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
2036 /* Constructors and destructors. */
2038 /* Construct a new context. The context below this one on the stack
2039 is given by NEXT. */
2041 static cp_parser_context
*
2042 cp_parser_context_new (cp_parser_context
* next
)
2044 cp_parser_context
*context
;
2046 /* Allocate the storage. */
2047 if (cp_parser_context_free_list
!= NULL
)
2049 /* Pull the first entry from the free list. */
2050 context
= cp_parser_context_free_list
;
2051 cp_parser_context_free_list
= context
->next
;
2052 memset (context
, 0, sizeof (*context
));
2055 context
= ggc_cleared_alloc
<cp_parser_context
> ();
2057 /* No errors have occurred yet in this context. */
2058 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
2059 /* If this is not the bottommost context, copy information that we
2060 need from the previous context. */
2063 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2064 expression, then we are parsing one in this context, too. */
2065 context
->object_type
= next
->object_type
;
2066 /* Thread the stack. */
2067 context
->next
= next
;
2073 /* Managing the unparsed function queues. */
2075 #define unparsed_funs_with_default_args \
2076 parser->unparsed_queues->last ().funs_with_default_args
2077 #define unparsed_funs_with_definitions \
2078 parser->unparsed_queues->last ().funs_with_definitions
2079 #define unparsed_nsdmis \
2080 parser->unparsed_queues->last ().nsdmis
2081 #define unparsed_noexcepts \
2082 parser->unparsed_queues->last ().noexcepts
2085 push_unparsed_function_queues (cp_parser
*parser
)
2087 cp_unparsed_functions_entry e
= { NULL
, make_tree_vector (), NULL
, NULL
};
2088 vec_safe_push (parser
->unparsed_queues
, e
);
2092 pop_unparsed_function_queues (cp_parser
*parser
)
2094 release_tree_vector (unparsed_funs_with_definitions
);
2095 parser
->unparsed_queues
->pop ();
2100 /* Constructors and destructors. */
2102 static cp_parser
*cp_parser_new
2105 /* Routines to parse various constructs.
2107 Those that return `tree' will return the error_mark_node (rather
2108 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2109 Sometimes, they will return an ordinary node if error-recovery was
2110 attempted, even though a parse error occurred. So, to check
2111 whether or not a parse error occurred, you should always use
2112 cp_parser_error_occurred. If the construct is optional (indicated
2113 either by an `_opt' in the name of the function that does the
2114 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2115 the construct is not present. */
2117 /* Lexical conventions [gram.lex] */
2119 static cp_expr cp_parser_identifier
2121 static cp_expr cp_parser_string_literal
2122 (cp_parser
*, bool, bool, bool);
2123 static cp_expr cp_parser_userdef_char_literal
2125 static tree cp_parser_userdef_string_literal
2127 static cp_expr cp_parser_userdef_numeric_literal
2130 /* Basic concepts [gram.basic] */
2132 static void cp_parser_translation_unit (cp_parser
*);
2134 /* Expressions [gram.expr] */
2136 static cp_expr cp_parser_primary_expression
2137 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2138 static cp_expr cp_parser_id_expression
2139 (cp_parser
*, bool, bool, bool *, bool, bool);
2140 static cp_expr cp_parser_unqualified_id
2141 (cp_parser
*, bool, bool, bool, bool);
2142 static tree cp_parser_nested_name_specifier_opt
2143 (cp_parser
*, bool, bool, bool, bool, bool = false);
2144 static tree cp_parser_nested_name_specifier
2145 (cp_parser
*, bool, bool, bool, bool);
2146 static tree cp_parser_qualifying_entity
2147 (cp_parser
*, bool, bool, bool, bool, bool);
2148 static cp_expr cp_parser_postfix_expression
2149 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2150 static tree cp_parser_postfix_open_square_expression
2151 (cp_parser
*, tree
, bool, bool);
2152 static tree cp_parser_postfix_dot_deref_expression
2153 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2154 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2155 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2157 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2158 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2159 static void cp_parser_pseudo_destructor_name
2160 (cp_parser
*, tree
, tree
*, tree
*);
2161 static cp_expr cp_parser_unary_expression
2162 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2163 static enum tree_code cp_parser_unary_operator
2165 static tree cp_parser_has_attribute_expression
2167 static tree cp_parser_new_expression
2169 static vec
<tree
, va_gc
> *cp_parser_new_placement
2171 static tree cp_parser_new_type_id
2172 (cp_parser
*, tree
*);
2173 static cp_declarator
*cp_parser_new_declarator_opt
2175 static cp_declarator
*cp_parser_direct_new_declarator
2177 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2179 static tree cp_parser_delete_expression
2181 static cp_expr cp_parser_cast_expression
2182 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2183 static cp_expr cp_parser_binary_expression
2184 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2185 static tree cp_parser_question_colon_clause
2186 (cp_parser
*, cp_expr
);
2187 static cp_expr cp_parser_assignment_expression
2188 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2189 static enum tree_code cp_parser_assignment_operator_opt
2191 static cp_expr cp_parser_expression
2192 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2193 static cp_expr cp_parser_constant_expression
2194 (cp_parser
*, int = 0, bool * = NULL
, bool = false);
2195 static cp_expr cp_parser_builtin_offsetof
2197 static cp_expr cp_parser_lambda_expression
2199 static void cp_parser_lambda_introducer
2200 (cp_parser
*, tree
);
2201 static bool cp_parser_lambda_declarator_opt
2202 (cp_parser
*, tree
);
2203 static void cp_parser_lambda_body
2204 (cp_parser
*, tree
);
2206 /* Statements [gram.stmt.stmt] */
2208 static void cp_parser_statement
2209 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2210 static void cp_parser_label_for_labeled_statement
2211 (cp_parser
*, tree
);
2212 static tree cp_parser_expression_statement
2213 (cp_parser
*, tree
);
2214 static tree cp_parser_compound_statement
2215 (cp_parser
*, tree
, int, bool);
2216 static void cp_parser_statement_seq_opt
2217 (cp_parser
*, tree
);
2218 static tree cp_parser_selection_statement
2219 (cp_parser
*, bool *, vec
<tree
> *);
2220 static tree cp_parser_condition
2222 static tree cp_parser_iteration_statement
2223 (cp_parser
*, bool *, bool, unsigned short);
2224 static bool cp_parser_init_statement
2225 (cp_parser
*, tree
*decl
);
2226 static tree cp_parser_for
2227 (cp_parser
*, bool, unsigned short);
2228 static tree cp_parser_c_for
2229 (cp_parser
*, tree
, tree
, bool, unsigned short);
2230 static tree cp_parser_range_for
2231 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2232 static void do_range_for_auto_deduction
2234 static tree cp_parser_perform_range_for_lookup
2235 (tree
, tree
*, tree
*);
2236 static tree cp_parser_range_for_member_function
2238 static tree cp_parser_jump_statement
2240 static void cp_parser_declaration_statement
2243 static tree cp_parser_implicitly_scoped_statement
2244 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2245 static void cp_parser_already_scoped_statement
2246 (cp_parser
*, bool *, const token_indent_info
&);
2248 /* State of module-declaration parsing. */
2251 MP_NOT_MODULE
, /* Not a module. */
2255 MP_FIRST
, /* First declaration of TU. */
2256 MP_GLOBAL
, /* Global Module Fragment. */
2258 MP_PURVIEW_IMPORTS
, /* Imports of a module. */
2259 MP_PURVIEW
, /* Purview of a named module. */
2261 MP_PRIVATE_IMPORTS
, /* Imports of a Private Module Fragment. */
2262 MP_PRIVATE
, /* Private Module Fragment. */
2265 static module_parse cp_parser_module_declaration
2266 (cp_parser
*parser
, module_parse
, bool exporting
);
2267 static void cp_parser_import_declaration
2268 (cp_parser
*parser
, module_parse
, bool exporting
);
2270 /* Declarations [gram.dcl.dcl] */
2272 static void cp_parser_declaration_seq_opt
2274 static void cp_parser_declaration
2275 (cp_parser
*, tree
);
2276 static void cp_parser_toplevel_declaration
2278 static void cp_parser_block_declaration
2279 (cp_parser
*, bool);
2280 static void cp_parser_simple_declaration
2281 (cp_parser
*, bool, tree
*);
2282 static void cp_parser_decl_specifier_seq
2283 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2284 static tree cp_parser_storage_class_specifier_opt
2286 static tree cp_parser_function_specifier_opt
2287 (cp_parser
*, cp_decl_specifier_seq
*);
2288 static tree cp_parser_type_specifier
2289 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2291 static tree cp_parser_simple_type_specifier
2292 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2293 static tree cp_parser_placeholder_type_specifier
2294 (cp_parser
*, location_t
, tree
, bool);
2295 static tree cp_parser_type_name
2296 (cp_parser
*, bool);
2297 static tree cp_parser_nonclass_name
2298 (cp_parser
* parser
);
2299 static tree cp_parser_elaborated_type_specifier
2300 (cp_parser
*, bool, bool);
2301 static tree cp_parser_enum_specifier
2303 static void cp_parser_enumerator_list
2304 (cp_parser
*, tree
);
2305 static void cp_parser_enumerator_definition
2306 (cp_parser
*, tree
);
2307 static tree cp_parser_namespace_name
2309 static void cp_parser_namespace_definition
2311 static void cp_parser_namespace_body
2313 static tree cp_parser_qualified_namespace_specifier
2315 static void cp_parser_namespace_alias_definition
2317 static bool cp_parser_using_declaration
2318 (cp_parser
*, bool);
2319 static void cp_parser_using_directive
2321 static void cp_parser_using_enum
2323 static tree cp_parser_alias_declaration
2325 static void cp_parser_asm_definition
2327 static void cp_parser_linkage_specification
2328 (cp_parser
*, tree
);
2329 static void cp_parser_static_assert
2330 (cp_parser
*, bool);
2331 static tree cp_parser_decltype
2333 static tree cp_parser_decomposition_declaration
2334 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2336 /* Declarators [gram.dcl.decl] */
2338 static tree cp_parser_init_declarator
2339 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*,
2340 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2341 location_t
*, tree
*);
2342 static cp_declarator
*cp_parser_declarator
2343 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2345 static cp_declarator
*cp_parser_direct_declarator
2346 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool,
2348 static enum tree_code cp_parser_ptr_operator
2349 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2350 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2352 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2354 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2356 static tree cp_parser_tx_qualifier_opt
2358 static tree cp_parser_late_return_type_opt
2359 (cp_parser
*, cp_declarator
*, tree
&);
2360 static tree cp_parser_declarator_id
2361 (cp_parser
*, bool);
2362 static tree cp_parser_type_id
2363 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2364 static tree cp_parser_template_type_arg
2366 static tree
cp_parser_trailing_type_id (cp_parser
*);
2367 static tree cp_parser_type_id_1
2368 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2369 static void cp_parser_type_specifier_seq
2370 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2371 static tree cp_parser_parameter_declaration_clause
2372 (cp_parser
*, cp_parser_flags
);
2373 static tree cp_parser_parameter_declaration_list
2374 (cp_parser
*, cp_parser_flags
);
2375 static cp_parameter_declarator
*cp_parser_parameter_declaration
2376 (cp_parser
*, cp_parser_flags
, bool, bool *);
2377 static tree cp_parser_default_argument
2378 (cp_parser
*, bool);
2379 static void cp_parser_function_body
2380 (cp_parser
*, bool);
2381 static tree cp_parser_initializer
2382 (cp_parser
*, bool *, bool *, bool = false);
2383 static cp_expr cp_parser_initializer_clause
2384 (cp_parser
*, bool *);
2385 static cp_expr cp_parser_braced_list
2386 (cp_parser
*, bool*);
2387 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2388 (cp_parser
*, bool *, bool *);
2390 static void cp_parser_ctor_initializer_opt_and_function_body
2391 (cp_parser
*, bool);
2393 static tree cp_parser_late_parsing_omp_declare_simd
2394 (cp_parser
*, tree
);
2396 static tree cp_parser_late_parsing_oacc_routine
2397 (cp_parser
*, tree
);
2399 static tree synthesize_implicit_template_parm
2400 (cp_parser
*, tree
);
2401 static tree finish_fully_implicit_template
2402 (cp_parser
*, tree
);
2403 static void abort_fully_implicit_template
2406 /* Classes [gram.class] */
2408 static tree cp_parser_class_name
2409 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2410 static tree cp_parser_class_specifier
2412 static tree cp_parser_class_head
2413 (cp_parser
*, bool *);
2414 static enum tag_types cp_parser_class_key
2416 static void cp_parser_type_parameter_key
2417 (cp_parser
* parser
);
2418 static void cp_parser_member_specification_opt
2420 static void cp_parser_member_declaration
2422 static tree cp_parser_pure_specifier
2424 static tree cp_parser_constant_initializer
2427 /* Derived classes [gram.class.derived] */
2429 static tree cp_parser_base_clause
2431 static tree cp_parser_base_specifier
2434 /* Special member functions [gram.special] */
2436 static tree cp_parser_conversion_function_id
2438 static tree cp_parser_conversion_type_id
2440 static cp_declarator
*cp_parser_conversion_declarator_opt
2442 static void cp_parser_ctor_initializer_opt
2444 static void cp_parser_mem_initializer_list
2446 static tree cp_parser_mem_initializer
2448 static tree cp_parser_mem_initializer_id
2451 /* Overloading [gram.over] */
2453 static cp_expr cp_parser_operator_function_id
2455 static cp_expr cp_parser_operator
2456 (cp_parser
*, location_t
);
2458 /* Templates [gram.temp] */
2460 static void cp_parser_template_declaration
2461 (cp_parser
*, bool);
2462 static tree cp_parser_template_parameter_list
2464 static tree cp_parser_template_parameter
2465 (cp_parser
*, bool *, bool *);
2466 static tree cp_parser_type_parameter
2467 (cp_parser
*, bool *);
2468 static tree cp_parser_template_id
2469 (cp_parser
*, bool, bool, enum tag_types
, bool);
2470 static tree cp_parser_template_id_expr
2471 (cp_parser
*, bool, bool, bool);
2472 static tree cp_parser_template_name
2473 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2474 static tree cp_parser_template_argument_list
2476 static tree cp_parser_template_argument
2478 static void cp_parser_explicit_instantiation
2480 static void cp_parser_explicit_specialization
2483 /* Exception handling [gram.except] */
2485 static tree cp_parser_try_block
2487 static void cp_parser_function_try_block
2489 static void cp_parser_handler_seq
2491 static void cp_parser_handler
2493 static tree cp_parser_exception_declaration
2495 static tree cp_parser_throw_expression
2497 static tree cp_parser_exception_specification_opt
2498 (cp_parser
*, cp_parser_flags
);
2499 static tree cp_parser_type_id_list
2501 static tree cp_parser_noexcept_specification_opt
2502 (cp_parser
*, cp_parser_flags
, bool, bool *, bool);
2504 /* GNU Extensions */
2506 static tree cp_parser_asm_specification_opt
2508 static tree cp_parser_asm_operand_list
2510 static tree cp_parser_asm_clobber_list
2512 static tree cp_parser_asm_label_list
2514 static bool cp_next_tokens_can_be_attribute_p
2516 static bool cp_next_tokens_can_be_gnu_attribute_p
2518 static bool cp_next_tokens_can_be_std_attribute_p
2520 static bool cp_nth_tokens_can_be_std_attribute_p
2521 (cp_parser
*, size_t);
2522 static bool cp_nth_tokens_can_be_gnu_attribute_p
2523 (cp_parser
*, size_t);
2524 static bool cp_nth_tokens_can_be_attribute_p
2525 (cp_parser
*, size_t);
2526 static tree cp_parser_attributes_opt
2528 static tree cp_parser_gnu_attributes_opt
2530 static tree cp_parser_gnu_attribute_list
2531 (cp_parser
*, bool = false);
2532 static tree cp_parser_std_attribute
2533 (cp_parser
*, tree
);
2534 static tree cp_parser_std_attribute_spec
2536 static tree cp_parser_std_attribute_spec_seq
2538 static size_t cp_parser_skip_std_attribute_spec_seq
2539 (cp_parser
*, size_t);
2540 static size_t cp_parser_skip_attributes_opt
2541 (cp_parser
*, size_t);
2542 static bool cp_parser_extension_opt
2543 (cp_parser
*, int *);
2544 static void cp_parser_label_declaration
2547 /* Concept Extensions */
2549 static tree cp_parser_concept_definition
2551 static tree cp_parser_constraint_expression
2553 static tree cp_parser_requires_clause_opt
2554 (cp_parser
*, bool);
2555 static tree cp_parser_requires_expression
2557 static tree cp_parser_requirement_parameter_list
2559 static tree cp_parser_requirement_body
2561 static tree cp_parser_requirement_seq
2563 static tree cp_parser_requirement
2565 static tree cp_parser_simple_requirement
2567 static tree cp_parser_compound_requirement
2569 static tree cp_parser_type_requirement
2571 static tree cp_parser_nested_requirement
2574 /* Transactional Memory Extensions */
2576 static tree cp_parser_transaction
2577 (cp_parser
*, cp_token
*);
2578 static tree cp_parser_transaction_expression
2579 (cp_parser
*, enum rid
);
2580 static void cp_parser_function_transaction
2581 (cp_parser
*, enum rid
);
2582 static tree cp_parser_transaction_cancel
2585 /* Coroutine extensions. */
2587 static tree cp_parser_yield_expression
2591 enum pragma_context
{
2598 static bool cp_parser_pragma
2599 (cp_parser
*, enum pragma_context
, bool *);
2601 /* Objective-C++ Productions */
2603 static tree cp_parser_objc_message_receiver
2605 static tree cp_parser_objc_message_args
2607 static tree cp_parser_objc_message_expression
2609 static cp_expr cp_parser_objc_encode_expression
2611 static tree cp_parser_objc_defs_expression
2613 static tree cp_parser_objc_protocol_expression
2615 static tree cp_parser_objc_selector_expression
2617 static cp_expr cp_parser_objc_expression
2619 static bool cp_parser_objc_selector_p
2621 static tree cp_parser_objc_selector
2623 static tree cp_parser_objc_protocol_refs_opt
2625 static void cp_parser_objc_declaration
2626 (cp_parser
*, tree
);
2627 static tree cp_parser_objc_statement
2629 static bool cp_parser_objc_valid_prefix_attributes
2630 (cp_parser
*, tree
*);
2631 static void cp_parser_objc_at_property_declaration
2633 static void cp_parser_objc_at_synthesize_declaration
2635 static void cp_parser_objc_at_dynamic_declaration
2637 static tree cp_parser_objc_struct_declaration
2640 /* Utility Routines */
2642 static cp_expr cp_parser_lookup_name
2643 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2644 static tree cp_parser_lookup_name_simple
2645 (cp_parser
*, tree
, location_t
);
2646 static tree cp_parser_maybe_treat_template_as_class
2648 static bool cp_parser_check_declarator_template_parameters
2649 (cp_parser
*, cp_declarator
*, location_t
);
2650 static bool cp_parser_check_template_parameters
2651 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2652 static cp_expr cp_parser_simple_cast_expression
2654 static tree cp_parser_global_scope_opt
2655 (cp_parser
*, bool);
2656 static bool cp_parser_constructor_declarator_p
2657 (cp_parser
*, cp_parser_flags
, bool);
2658 static tree cp_parser_function_definition_from_specifiers_and_declarator
2659 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2660 static tree cp_parser_function_definition_after_declarator
2661 (cp_parser
*, bool);
2662 static bool cp_parser_template_declaration_after_export
2663 (cp_parser
*, bool);
2664 static void cp_parser_perform_template_parameter_access_checks
2665 (vec
<deferred_access_check
, va_gc
> *);
2666 static tree cp_parser_single_declaration
2667 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2668 static cp_expr cp_parser_functional_cast
2669 (cp_parser
*, tree
);
2670 static tree cp_parser_save_member_function_body
2671 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2672 static tree cp_parser_save_nsdmi
2674 static tree cp_parser_enclosed_template_argument_list
2676 static void cp_parser_save_default_args
2677 (cp_parser
*, tree
);
2678 static void cp_parser_late_parsing_for_member
2679 (cp_parser
*, tree
);
2680 static tree cp_parser_late_parse_one_default_arg
2681 (cp_parser
*, tree
, tree
, tree
);
2682 static void cp_parser_late_parsing_nsdmi
2683 (cp_parser
*, tree
);
2684 static void cp_parser_late_parsing_default_args
2685 (cp_parser
*, tree
);
2686 static tree cp_parser_sizeof_operand
2687 (cp_parser
*, enum rid
);
2688 static cp_expr cp_parser_trait_expr
2689 (cp_parser
*, enum rid
);
2690 static bool cp_parser_declares_only_class_p
2692 static void cp_parser_set_storage_class
2693 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2694 static void cp_parser_set_decl_spec_type
2695 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2696 static void set_and_check_decl_spec_loc
2697 (cp_decl_specifier_seq
*decl_specs
,
2698 cp_decl_spec ds
, cp_token
*);
2699 static bool cp_parser_friend_p
2700 (const cp_decl_specifier_seq
*);
2701 static void cp_parser_required_error
2702 (cp_parser
*, required_token
, bool, location_t
);
2703 static cp_token
*cp_parser_require
2704 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2705 static cp_token
*cp_parser_require_keyword
2706 (cp_parser
*, enum rid
, required_token
);
2707 static bool cp_parser_token_starts_function_definition_p
2709 static bool cp_parser_next_token_starts_class_definition_p
2711 static bool cp_parser_next_token_ends_template_argument_p
2713 static bool cp_parser_nth_token_starts_template_argument_list_p
2714 (cp_parser
*, size_t);
2715 static enum tag_types cp_parser_token_is_class_key
2717 static enum tag_types cp_parser_token_is_type_parameter_key
2719 static void cp_parser_maybe_warn_enum_key (cp_parser
*, location_t
, tree
, rid
);
2720 static void cp_parser_check_class_key
2721 (cp_parser
*, location_t
, enum tag_types
, tree type
, bool, bool);
2722 static void cp_parser_check_access_in_redeclaration
2723 (tree type
, location_t location
);
2724 static bool cp_parser_optional_template_keyword
2726 static void cp_parser_pre_parsed_nested_name_specifier
2728 static bool cp_parser_cache_group
2729 (cp_parser
*, enum cpp_ttype
, unsigned);
2730 static tree cp_parser_cache_defarg
2731 (cp_parser
*parser
, bool nsdmi
);
2732 static void cp_parser_parse_tentatively
2734 static void cp_parser_commit_to_tentative_parse
2736 static void cp_parser_commit_to_topmost_tentative_parse
2738 static void cp_parser_abort_tentative_parse
2740 static bool cp_parser_parse_definitely
2742 static inline bool cp_parser_parsing_tentatively
2744 static bool cp_parser_uncommitted_to_tentative_parse_p
2746 static void cp_parser_error
2747 (cp_parser
*, const char *);
2748 static void cp_parser_name_lookup_error
2749 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2750 static bool cp_parser_simulate_error
2752 static bool cp_parser_check_type_definition
2754 static void cp_parser_check_for_definition_in_return_type
2755 (cp_declarator
*, tree
, location_t type_location
);
2756 static void cp_parser_check_for_invalid_template_id
2757 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2758 static bool cp_parser_non_integral_constant_expression
2759 (cp_parser
*, non_integral_constant
);
2760 static void cp_parser_diagnose_invalid_type_name
2761 (cp_parser
*, tree
, location_t
);
2762 static bool cp_parser_parse_and_diagnose_invalid_type_name
2764 static int cp_parser_skip_to_closing_parenthesis
2765 (cp_parser
*, bool, bool, bool);
2766 static void cp_parser_skip_to_end_of_statement
2768 static void cp_parser_consume_semicolon_at_end_of_statement
2770 static void cp_parser_skip_to_end_of_block_or_statement
2772 static bool cp_parser_skip_to_closing_brace
2774 static void cp_parser_skip_to_end_of_template_parameter_list
2776 static void cp_parser_skip_to_pragma_eol
2777 (cp_parser
*, cp_token
*);
2778 static bool cp_parser_error_occurred
2780 static bool cp_parser_allow_gnu_extensions_p
2782 static bool cp_parser_is_pure_string_literal
2784 static bool cp_parser_is_string_literal
2786 static bool cp_parser_is_keyword
2787 (cp_token
*, enum rid
);
2788 static tree cp_parser_make_typename_type
2789 (cp_parser
*, tree
, location_t location
);
2790 static cp_declarator
* cp_parser_make_indirect_declarator
2791 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2792 static bool cp_parser_compound_literal_p
2794 static bool cp_parser_array_designator_p
2796 static bool cp_parser_init_statement_p
2798 static bool cp_parser_skip_to_closing_square_bracket
2800 static size_t cp_parser_skip_balanced_tokens (cp_parser
*, size_t);
2802 // -------------------------------------------------------------------------- //
2803 // Unevaluated Operand Guard
2805 // Implementation of an RAII helper for unevaluated operand parsing.
2806 cp_unevaluated::cp_unevaluated ()
2808 ++cp_unevaluated_operand
;
2809 ++c_inhibit_evaluation_warnings
;
2812 cp_unevaluated::~cp_unevaluated ()
2814 --c_inhibit_evaluation_warnings
;
2815 --cp_unevaluated_operand
;
2818 // -------------------------------------------------------------------------- //
2819 // Tentative Parsing
2821 /* Returns nonzero if we are parsing tentatively. */
2824 cp_parser_parsing_tentatively (cp_parser
* parser
)
2826 return parser
->context
->next
!= NULL
;
2829 /* Returns nonzero if TOKEN is a string literal. */
2832 cp_parser_is_pure_string_literal (cp_token
* token
)
2834 return (token
->type
== CPP_STRING
||
2835 token
->type
== CPP_STRING16
||
2836 token
->type
== CPP_STRING32
||
2837 token
->type
== CPP_WSTRING
||
2838 token
->type
== CPP_UTF8STRING
);
2841 /* Returns nonzero if TOKEN is a string literal
2842 of a user-defined string literal. */
2845 cp_parser_is_string_literal (cp_token
* token
)
2847 return (cp_parser_is_pure_string_literal (token
) ||
2848 token
->type
== CPP_STRING_USERDEF
||
2849 token
->type
== CPP_STRING16_USERDEF
||
2850 token
->type
== CPP_STRING32_USERDEF
||
2851 token
->type
== CPP_WSTRING_USERDEF
||
2852 token
->type
== CPP_UTF8STRING_USERDEF
);
2855 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2858 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2860 return token
->keyword
== keyword
;
2863 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2866 static enum pragma_kind
2867 cp_parser_pragma_kind (cp_token
*token
)
2869 if (token
->type
!= CPP_PRAGMA
)
2871 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2872 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2875 /* Helper function for cp_parser_error.
2876 Having peeked a token of kind TOK1_KIND that might signify
2877 a conflict marker, peek successor tokens to determine
2878 if we actually do have a conflict marker.
2879 Specifically, we consider a run of 7 '<', '=' or '>' characters
2880 at the start of a line as a conflict marker.
2881 These come through the lexer as three pairs and a single,
2882 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2883 If it returns true, *OUT_LOC is written to with the location/range
2887 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2888 location_t
*out_loc
)
2890 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2891 if (token2
->type
!= tok1_kind
)
2893 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2894 if (token3
->type
!= tok1_kind
)
2896 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2897 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2900 /* It must be at the start of the line. */
2901 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2902 if (LOCATION_COLUMN (start_loc
) != 1)
2905 /* We have a conflict marker. Construct a location of the form:
2908 with start == caret, finishing at the end of the marker. */
2909 location_t finish_loc
= get_finish (token4
->location
);
2910 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2915 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2919 get_matching_symbol (required_token token_desc
)
2926 case RT_CLOSE_BRACE
:
2928 case RT_CLOSE_PAREN
:
2933 /* Attempt to convert TOKEN_DESC from a required_token to an
2934 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2936 static enum cpp_ttype
2937 get_required_cpp_ttype (required_token token_desc
)
2942 return CPP_SEMICOLON
;
2944 return CPP_OPEN_PAREN
;
2945 case RT_CLOSE_BRACE
:
2946 return CPP_CLOSE_BRACE
;
2948 return CPP_OPEN_BRACE
;
2949 case RT_CLOSE_SQUARE
:
2950 return CPP_CLOSE_SQUARE
;
2951 case RT_OPEN_SQUARE
:
2952 return CPP_OPEN_SQUARE
;
2957 case RT_CLOSE_PAREN
:
2958 return CPP_CLOSE_PAREN
;
2961 /* Use CPP_EOF as a "no completions possible" code. */
2967 /* Subroutine of cp_parser_error and cp_parser_required_error.
2969 Issue a diagnostic of the form
2970 FILE:LINE: MESSAGE before TOKEN
2971 where TOKEN is the next token in the input stream. MESSAGE
2972 (specified by the caller) is usually of the form "expected
2975 This bypasses the check for tentative passing, and potentially
2976 adds material needed by cp_parser_required_error.
2978 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2979 suggesting insertion of the missing token.
2981 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2982 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2986 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2987 required_token missing_token_desc
,
2988 location_t matching_location
)
2990 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2991 /* This diagnostic makes more sense if it is tagged to the line
2992 of the token we just peeked at. */
2993 cp_lexer_set_source_position_from_token (token
);
2995 if (token
->type
== CPP_PRAGMA
)
2997 error_at (token
->location
,
2998 "%<#pragma%> is not allowed here");
2999 cp_parser_skip_to_pragma_eol (parser
, token
);
3003 /* If this is actually a conflict marker, report it as such. */
3004 if (token
->type
== CPP_LSHIFT
3005 || token
->type
== CPP_RSHIFT
3006 || token
->type
== CPP_EQ_EQ
)
3009 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
3011 error_at (loc
, "version control conflict marker in file");
3012 expanded_location token_exploc
= expand_location (token
->location
);
3013 /* Consume tokens until the end of the source line. */
3016 cp_lexer_consume_token (parser
->lexer
);
3017 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
3018 if (next
->type
== CPP_EOF
)
3020 if (next
->location
== UNKNOWN_LOCATION
3021 || loc
== UNKNOWN_LOCATION
)
3024 expanded_location next_exploc
= expand_location (next
->location
);
3025 if (next_exploc
.file
!= token_exploc
.file
)
3027 if (next_exploc
.line
!= token_exploc
.line
)
3034 auto_diagnostic_group d
;
3035 gcc_rich_location
richloc (input_location
);
3037 bool added_matching_location
= false;
3039 if (missing_token_desc
!= RT_NONE
)
3040 if (cp_token
*prev_token
= cp_lexer_safe_previous_token (parser
->lexer
))
3042 /* Potentially supply a fix-it hint, suggesting to add the
3043 missing token immediately after the *previous* token.
3044 This may move the primary location within richloc. */
3045 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
3046 location_t prev_token_loc
= prev_token
->location
;
3047 maybe_suggest_missing_token_insertion (&richloc
, ttype
,
3050 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3051 Attempt to consolidate diagnostics by printing it as a
3052 secondary range within the main diagnostic. */
3053 if (matching_location
!= UNKNOWN_LOCATION
)
3054 added_matching_location
3055 = richloc
.add_location_if_nearby (matching_location
);
3058 /* If we were parsing a string-literal and there is an unknown name
3059 token right after, then check to see if that could also have been
3060 a literal string by checking the name against a list of known
3061 standard string literal constants defined in header files. If
3062 there is one, then add that as an hint to the error message. */
3064 if (token
->type
== CPP_NAME
)
3065 if (cp_token
*prev_token
= cp_lexer_safe_previous_token (parser
->lexer
))
3066 if (cp_parser_is_string_literal (prev_token
))
3068 tree name
= token
->u
.value
;
3069 const char *token_name
= IDENTIFIER_POINTER (name
);
3070 const char *header_hint
3071 = get_cp_stdlib_header_for_string_macro_name (token_name
);
3072 if (header_hint
!= NULL
)
3073 h
= name_hint (NULL
, new suggest_missing_header (token
->location
,
3078 /* Actually emit the error. */
3079 c_parse_error (gmsgid
,
3080 /* Because c_parser_error does not understand
3081 CPP_KEYWORD, keywords are treated like
3083 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
3084 token
->u
.value
, token
->flags
, &richloc
);
3086 if (missing_token_desc
!= RT_NONE
)
3088 /* If we weren't able to consolidate matching_location, then
3089 print it as a secondary diagnostic. */
3090 if (matching_location
!= UNKNOWN_LOCATION
3091 && !added_matching_location
)
3092 inform (matching_location
, "to match this %qs",
3093 get_matching_symbol (missing_token_desc
));
3097 /* If not parsing tentatively, issue a diagnostic of the form
3098 FILE:LINE: MESSAGE before TOKEN
3099 where TOKEN is the next token in the input stream. MESSAGE
3100 (specified by the caller) is usually of the form "expected
3104 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
3106 if (!cp_parser_simulate_error (parser
))
3107 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
3110 /* Issue an error about name-lookup failing. NAME is the
3111 IDENTIFIER_NODE DECL is the result of
3112 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3113 the thing that we hoped to find. */
3116 cp_parser_name_lookup_error (cp_parser
* parser
,
3119 name_lookup_error desired
,
3120 location_t location
)
3122 /* If name lookup completely failed, tell the user that NAME was not
3124 if (decl
== error_mark_node
)
3126 if (parser
->scope
&& parser
->scope
!= global_namespace
)
3127 error_at (location
, "%<%E::%E%> has not been declared",
3128 parser
->scope
, name
);
3129 else if (parser
->scope
== global_namespace
)
3130 error_at (location
, "%<::%E%> has not been declared", name
);
3131 else if (parser
->object_scope
3132 && !CLASS_TYPE_P (parser
->object_scope
))
3133 error_at (location
, "request for member %qE in non-class type %qT",
3134 name
, parser
->object_scope
);
3135 else if (parser
->object_scope
)
3136 error_at (location
, "%<%T::%E%> has not been declared",
3137 parser
->object_scope
, name
);
3139 error_at (location
, "%qE has not been declared", name
);
3141 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
3146 error_at (location
, "%<%E::%E%> is not a type",
3147 parser
->scope
, name
);
3150 error_at (location
, "%<%E::%E%> is not a class or namespace",
3151 parser
->scope
, name
);
3155 "%<%E::%E%> is not a class, namespace, or enumeration",
3156 parser
->scope
, name
);
3163 else if (parser
->scope
== global_namespace
)
3168 error_at (location
, "%<::%E%> is not a type", name
);
3171 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3175 "%<::%E%> is not a class, namespace, or enumeration",
3187 error_at (location
, "%qE is not a type", name
);
3190 error_at (location
, "%qE is not a class or namespace", name
);
3194 "%qE is not a class, namespace, or enumeration", name
);
3202 /* If we are parsing tentatively, remember that an error has occurred
3203 during this tentative parse. Returns true if the error was
3204 simulated; false if a message should be issued by the caller. */
3207 cp_parser_simulate_error (cp_parser
* parser
)
3209 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3211 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3217 /* This function is called when a type is defined. If type
3218 definitions are forbidden at this point, an error message is
3222 cp_parser_check_type_definition (cp_parser
* parser
)
3224 /* If types are forbidden here, issue a message. */
3225 if (parser
->type_definition_forbidden_message
)
3227 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3228 or %qs in the message need to be interpreted. */
3229 error (parser
->type_definition_forbidden_message
,
3230 parser
->type_definition_forbidden_message_arg
);
3236 /* This function is called when the DECLARATOR is processed. The TYPE
3237 was a type defined in the decl-specifiers. If it is invalid to
3238 define a type in the decl-specifiers for DECLARATOR, an error is
3239 issued. TYPE_LOCATION is the location of TYPE and is used
3240 for error reporting. */
3243 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3244 tree type
, location_t type_location
)
3246 /* [dcl.fct] forbids type definitions in return types.
3247 Unfortunately, it's not easy to know whether or not we are
3248 processing a return type until after the fact. */
3250 && (declarator
->kind
== cdk_pointer
3251 || declarator
->kind
== cdk_reference
3252 || declarator
->kind
== cdk_ptrmem
))
3253 declarator
= declarator
->declarator
;
3255 && declarator
->kind
== cdk_function
)
3257 error_at (type_location
,
3258 "new types may not be defined in a return type");
3259 inform (type_location
,
3260 "(perhaps a semicolon is missing after the definition of %qT)",
3265 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3266 "<" in any valid C++ program. If the next token is indeed "<",
3267 issue a message warning the user about what appears to be an
3268 invalid attempt to form a template-id. LOCATION is the location
3269 of the type-specifier (TYPE) */
3272 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3274 enum tag_types tag_type
,
3275 location_t location
)
3277 cp_token_position start
= 0;
3279 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3281 if (TREE_CODE (type
) == TYPE_DECL
)
3282 type
= TREE_TYPE (type
);
3283 if (TYPE_P (type
) && !template_placeholder_p (type
))
3284 error_at (location
, "%qT is not a template", type
);
3285 else if (identifier_p (type
))
3287 if (tag_type
!= none_type
)
3288 error_at (location
, "%qE is not a class template", type
);
3290 error_at (location
, "%qE is not a template", type
);
3293 error_at (location
, "invalid template-id");
3294 /* Remember the location of the invalid "<". */
3295 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3296 start
= cp_lexer_token_position (parser
->lexer
, true);
3297 /* Consume the "<". */
3298 cp_lexer_consume_token (parser
->lexer
);
3299 /* Parse the template arguments. */
3300 cp_parser_enclosed_template_argument_list (parser
);
3301 /* Permanently remove the invalid template arguments so that
3302 this error message is not issued again. */
3304 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3308 /* If parsing an integral constant-expression, issue an error message
3309 about the fact that THING appeared and return true. Otherwise,
3310 return false. In either case, set
3311 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3314 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3315 non_integral_constant thing
)
3317 parser
->non_integral_constant_expression_p
= true;
3318 if (parser
->integral_constant_expression_p
)
3320 if (!parser
->allow_non_integral_constant_expression_p
)
3322 const char *msg
= NULL
;
3326 pedwarn (input_location
, OPT_Wpedantic
,
3327 "ISO C++ forbids using a floating-point literal "
3328 "in a constant-expression");
3331 error ("a cast to a type other than an integral or "
3332 "enumeration type cannot appear in a "
3333 "constant-expression");
3336 error ("%<typeid%> operator "
3337 "cannot appear in a constant-expression");
3340 error ("non-constant compound literals "
3341 "cannot appear in a constant-expression");
3344 error ("a function call "
3345 "cannot appear in a constant-expression");
3348 error ("an increment "
3349 "cannot appear in a constant-expression");
3352 error ("an decrement "
3353 "cannot appear in a constant-expression");
3356 error ("an array reference "
3357 "cannot appear in a constant-expression");
3359 case NIC_ADDR_LABEL
:
3360 error ("the address of a label "
3361 "cannot appear in a constant-expression");
3363 case NIC_OVERLOADED
:
3364 error ("calls to overloaded operators "
3365 "cannot appear in a constant-expression");
3367 case NIC_ASSIGNMENT
:
3368 error ("an assignment cannot appear in a constant-expression");
3371 error ("a comma operator "
3372 "cannot appear in a constant-expression");
3374 case NIC_CONSTRUCTOR
:
3375 error ("a call to a constructor "
3376 "cannot appear in a constant-expression");
3378 case NIC_TRANSACTION
:
3379 error ("a transaction expression "
3380 "cannot appear in a constant-expression");
3386 msg
= "__FUNCTION__";
3388 case NIC_PRETTY_FUNC
:
3389 msg
= "__PRETTY_FUNCTION__";
3409 case NIC_PREINCREMENT
:
3412 case NIC_PREDECREMENT
:
3425 error ("%qs cannot appear in a constant-expression", msg
);
3432 /* Emit a diagnostic for an invalid type name. This function commits
3433 to the current active tentative parse, if any. (Otherwise, the
3434 problematic construct might be encountered again later, resulting
3435 in duplicate error messages.) LOCATION is the location of ID. */
3438 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3439 location_t location
)
3441 tree decl
, ambiguous_decls
;
3442 cp_parser_commit_to_tentative_parse (parser
);
3443 /* Try to lookup the identifier. */
3444 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3445 /*is_template=*/false,
3446 /*is_namespace=*/false,
3447 /*check_dependency=*/true,
3448 &ambiguous_decls
, location
);
3449 if (ambiguous_decls
)
3450 /* If the lookup was ambiguous, an error will already have
3453 /* If the lookup found a template-name, it means that the user forgot
3454 to specify an argument list. Emit a useful error message. */
3455 if (DECL_TYPE_TEMPLATE_P (decl
))
3457 auto_diagnostic_group d
;
3459 "invalid use of template-name %qE without an argument list",
3461 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3462 inform (location
, "class template argument deduction is only available "
3463 "with %<-std=c++17%> or %<-std=gnu++17%>");
3464 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3466 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3467 error_at (location
, "invalid use of destructor %qD as a type", id
);
3468 else if (TREE_CODE (decl
) == TYPE_DECL
)
3469 /* Something like 'unsigned A a;' */
3470 error_at (location
, "invalid combination of multiple type-specifiers");
3471 else if (!parser
->scope
)
3473 /* Issue an error message. */
3474 auto_diagnostic_group d
;
3476 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3477 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3478 if (const char *suggestion
= hint
.suggestion ())
3480 gcc_rich_location
richloc (location
);
3481 richloc
.add_fixit_replace (suggestion
);
3483 "%qE does not name a type; did you mean %qs?",
3487 error_at (location
, "%qE does not name a type", id
);
3488 /* If we're in a template class, it's possible that the user was
3489 referring to a type from a base class. For example:
3491 template <typename T> struct A { typedef T X; };
3492 template <typename T> struct B : public A<T> { X x; };
3494 The user should have said "typename A<T>::X". */
3495 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3496 inform (location
, "C++11 %<constexpr%> only available with "
3497 "%<-std=c++11%> or %<-std=gnu++11%>");
3498 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3499 inform (location
, "C++11 %<noexcept%> only available with "
3500 "%<-std=c++11%> or %<-std=gnu++11%>");
3501 else if (TREE_CODE (id
) == IDENTIFIER_NODE
3502 && (id_equal (id
, "module") || id_equal (id
, "import")))
3505 inform (location
, "%qE is not recognized as a module control-line",
3507 else if (cxx_dialect
< cxx20
)
3508 inform (location
, "C++20 %qE only available with %<-fmodules-ts%>",
3511 inform (location
, "C++20 %qE only available with %<-fmodules-ts%>"
3512 ", which is not yet enabled with %<-std=c++20%>", id
);
3514 else if (cxx_dialect
< cxx11
3515 && TREE_CODE (id
) == IDENTIFIER_NODE
3516 && id_equal (id
, "thread_local"))
3517 inform (location
, "C++11 %<thread_local%> only available with "
3518 "%<-std=c++11%> or %<-std=gnu++11%>");
3519 else if (cxx_dialect
< cxx20
&& id
== ridpointers
[(int)RID_CONSTINIT
])
3520 inform (location
, "C++20 %<constinit%> only available with "
3521 "%<-std=c++20%> or %<-std=gnu++20%>");
3522 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3523 inform (location
, "%<concept%> only available with %<-std=c++20%> or "
3525 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_REQUIRES
])
3526 inform (location
, "%<requires%> only available with %<-std=c++20%> or "
3528 else if (processing_template_decl
&& current_class_type
3529 && TYPE_BINFO (current_class_type
))
3531 for (tree b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3532 b
; b
= TREE_CHAIN (b
))
3534 tree base_type
= BINFO_TYPE (b
);
3535 if (CLASS_TYPE_P (base_type
)
3536 && dependent_type_p (base_type
))
3538 /* Go from a particular instantiation of the
3539 template (which will have an empty TYPE_FIELDs),
3540 to the main version. */
3541 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3542 for (tree field
= TYPE_FIELDS (base_type
);
3543 field
; field
= DECL_CHAIN (field
))
3544 if (TREE_CODE (field
) == TYPE_DECL
3545 && DECL_NAME (field
) == id
)
3548 "(perhaps %<typename %T::%E%> was intended)",
3549 BINFO_TYPE (b
), id
);
3557 /* Here we diagnose qualified-ids where the scope is actually correct,
3558 but the identifier does not resolve to a valid type name. */
3559 else if (parser
->scope
!= error_mark_node
)
3561 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3563 auto_diagnostic_group d
;
3565 if (decl
== error_mark_node
)
3566 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3568 const char *suggestion
= hint
.suggestion ();
3569 gcc_rich_location
richloc (location_of (id
));
3571 richloc
.add_fixit_replace (suggestion
);
3572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3576 "%qE in namespace %qE does not name a template"
3577 " type; did you mean %qs?",
3578 id
, parser
->scope
, suggestion
);
3581 "%qE in namespace %qE does not name a template type",
3584 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3588 "%qE in namespace %qE does not name a template"
3589 " type; did you mean %qs?",
3590 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3593 "%qE in namespace %qE does not name a template"
3595 TREE_OPERAND (id
, 0), parser
->scope
);
3601 "%qE in namespace %qE does not name a type"
3602 "; did you mean %qs?",
3603 id
, parser
->scope
, suggestion
);
3606 "%qE in namespace %qE does not name a type",
3610 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3612 else if (CLASS_TYPE_P (parser
->scope
)
3613 && constructor_name_p (id
, parser
->scope
))
3616 auto_diagnostic_group d
;
3617 error_at (location
, "%<%T::%E%> names the constructor, not"
3618 " the type", parser
->scope
, id
);
3619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3620 error_at (location
, "and %qT has no template constructors",
3623 else if (TYPE_P (parser
->scope
)
3624 && dependent_scope_p (parser
->scope
))
3626 gcc_rich_location
richloc (location
);
3627 richloc
.add_fixit_insert_before ("typename ");
3628 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3630 "need %<typename%> before %<%T::%D::%E%> because "
3631 "%<%T::%D%> is a dependent scope",
3632 TYPE_CONTEXT (parser
->scope
),
3633 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3635 TYPE_CONTEXT (parser
->scope
),
3636 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3638 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3639 "%qT is a dependent scope",
3640 parser
->scope
, id
, parser
->scope
);
3642 else if (TYPE_P (parser
->scope
))
3644 auto_diagnostic_group d
;
3645 if (!COMPLETE_TYPE_P (parser
->scope
))
3646 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3648 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3649 error_at (location_of (id
),
3650 "%qE in %q#T does not name a template type",
3652 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3653 error_at (location_of (id
),
3654 "%qE in %q#T does not name a template type",
3655 TREE_OPERAND (id
, 0), parser
->scope
);
3657 error_at (location_of (id
),
3658 "%qE in %q#T does not name a type",
3661 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3668 /* Check for a common situation where a type-name should be present,
3669 but is not, and issue a sensible error message. Returns true if an
3670 invalid type-name was detected.
3672 The situation handled by this function are variable declarations of the
3673 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3674 Usually, `ID' should name a type, but if we got here it means that it
3675 does not. We try to emit the best possible error message depending on
3676 how exactly the id-expression looks like. */
3679 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3682 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3684 /* Avoid duplicate error about ambiguous lookup. */
3685 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3687 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3688 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3692 cp_parser_parse_tentatively (parser
);
3693 id
= cp_parser_id_expression (parser
,
3694 /*template_keyword_p=*/false,
3695 /*check_dependency_p=*/true,
3696 /*template_p=*/NULL
,
3697 /*declarator_p=*/true,
3698 /*optional_p=*/false);
3699 /* If the next token is a (, this is a function with no explicit return
3700 type, i.e. constructor, destructor or conversion op. */
3701 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3702 || TREE_CODE (id
) == TYPE_DECL
)
3704 cp_parser_abort_tentative_parse (parser
);
3707 if (!cp_parser_parse_definitely (parser
))
3710 /* Emit a diagnostic for the invalid type. */
3711 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3713 /* If we aren't in the middle of a declarator (i.e. in a
3714 parameter-declaration-clause), skip to the end of the declaration;
3715 there's no point in trying to process it. */
3716 if (!parser
->in_declarator_p
)
3717 cp_parser_skip_to_end_of_block_or_statement (parser
);
3721 /* Consume tokens up to, and including, the next non-nested closing `)'.
3722 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3723 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3724 found an unnested token of that type. */
3727 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3732 unsigned paren_depth
= 0;
3733 unsigned brace_depth
= 0;
3734 unsigned square_depth
= 0;
3735 unsigned condop_depth
= 0;
3737 if (recovering
&& or_ttype
== CPP_EOF
3738 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3743 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3745 /* Have we found what we're looking for before the closing paren? */
3746 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3747 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3750 switch (token
->type
)
3752 case CPP_PRAGMA_EOL
:
3753 if (!parser
->lexer
->in_pragma
)
3757 /* If we've run out of tokens, then there is no closing `)'. */
3760 /* This is good for lambda expression capture-lists. */
3761 case CPP_OPEN_SQUARE
:
3764 case CPP_CLOSE_SQUARE
:
3765 if (!square_depth
--)
3770 /* This matches the processing in skip_to_end_of_statement. */
3775 case CPP_OPEN_BRACE
:
3778 case CPP_CLOSE_BRACE
:
3783 case CPP_OPEN_PAREN
:
3788 case CPP_CLOSE_PAREN
:
3789 if (!brace_depth
&& !paren_depth
--)
3792 cp_lexer_consume_token (parser
->lexer
);
3798 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3803 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3808 if (token
->keyword
!= RID__EXPORT
3809 && token
->keyword
!= RID__MODULE
3810 && token
->keyword
!= RID__IMPORT
)
3815 /* We fell into a pragma. Skip it, and continue. */
3816 cp_parser_skip_to_pragma_eol (parser
, recovering
? token
: nullptr);
3823 /* Consume the token. */
3824 cp_lexer_consume_token (parser
->lexer
);
3828 /* Consume tokens up to, and including, the next non-nested closing `)'.
3829 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3830 are doing error recovery. Returns -1 if OR_COMMA is true and we
3831 found an unnested token of that type. */
3834 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3839 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3840 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3841 ttype
, consume_paren
);
3844 /* Consume tokens until we reach the end of the current statement.
3845 Normally, that will be just before consuming a `;'. However, if a
3846 non-nested `}' comes first, then we stop before consuming that. */
3849 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3851 unsigned nesting_depth
= 0;
3853 /* Unwind generic function template scope if necessary. */
3854 if (parser
->fully_implicit_function_template_p
)
3855 abort_fully_implicit_template (parser
);
3859 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3861 switch (token
->type
)
3863 case CPP_PRAGMA_EOL
:
3864 if (!parser
->lexer
->in_pragma
)
3868 /* If we've run out of tokens, stop. */
3872 /* If the next token is a `;', we have reached the end of the
3878 case CPP_CLOSE_BRACE
:
3879 /* If this is a non-nested '}', stop before consuming it.
3880 That way, when confronted with something like:
3884 we stop before consuming the closing '}', even though we
3885 have not yet reached a `;'. */
3886 if (nesting_depth
== 0)
3889 /* If it is the closing '}' for a block that we have
3890 scanned, stop -- but only after consuming the token.
3896 we will stop after the body of the erroneously declared
3897 function, but before consuming the following `typedef'
3899 if (--nesting_depth
== 0)
3901 cp_lexer_consume_token (parser
->lexer
);
3906 case CPP_OPEN_BRACE
:
3911 if (token
->keyword
!= RID__EXPORT
3912 && token
->keyword
!= RID__MODULE
3913 && token
->keyword
!= RID__IMPORT
)
3918 /* We fell into a pragma. Skip it, and continue or return. */
3919 cp_parser_skip_to_pragma_eol (parser
, token
);
3928 /* Consume the token. */
3929 cp_lexer_consume_token (parser
->lexer
);
3933 /* This function is called at the end of a statement or declaration.
3934 If the next token is a semicolon, it is consumed; otherwise, error
3935 recovery is attempted. */
3938 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3940 /* Look for the trailing `;'. */
3941 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3943 /* If there is additional (erroneous) input, skip to the end of
3945 cp_parser_skip_to_end_of_statement (parser
);
3946 /* If the next token is now a `;', consume it. */
3947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3948 cp_lexer_consume_token (parser
->lexer
);
3952 /* Skip tokens until we have consumed an entire block, or until we
3953 have consumed a non-nested `;'. */
3956 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3958 int nesting_depth
= 0;
3960 /* Unwind generic function template scope if necessary. */
3961 if (parser
->fully_implicit_function_template_p
)
3962 abort_fully_implicit_template (parser
);
3964 while (nesting_depth
>= 0)
3966 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3968 switch (token
->type
)
3970 case CPP_PRAGMA_EOL
:
3971 if (!parser
->lexer
->in_pragma
)
3975 /* If we've run out of tokens, stop. */
3979 /* Stop if this is an unnested ';'. */
3984 case CPP_CLOSE_BRACE
:
3985 /* Stop if this is an unnested '}', or closes the outermost
3988 if (nesting_depth
< 0)
3994 case CPP_OPEN_BRACE
:
4000 if (token
->keyword
!= RID__EXPORT
4001 && token
->keyword
!= RID__MODULE
4002 && token
->keyword
!= RID__IMPORT
)
4007 /* Skip it, and continue or return. */
4008 cp_parser_skip_to_pragma_eol (parser
, token
);
4017 /* Consume the token. */
4018 cp_lexer_consume_token (parser
->lexer
);
4022 /* Skip tokens until a non-nested closing curly brace is the next
4023 token, or there are no more tokens. Return true in the first case,
4027 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
4029 unsigned nesting_depth
= 0;
4033 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4035 switch (token
->type
)
4037 case CPP_PRAGMA_EOL
:
4038 if (!parser
->lexer
->in_pragma
)
4042 /* If we've run out of tokens, stop. */
4045 case CPP_CLOSE_BRACE
:
4046 /* If the next token is a non-nested `}', then we have reached
4047 the end of the current block. */
4048 if (nesting_depth
-- == 0)
4052 case CPP_OPEN_BRACE
:
4053 /* If it the next token is a `{', then we are entering a new
4054 block. Consume the entire block. */
4062 /* Consume the token. */
4063 cp_lexer_consume_token (parser
->lexer
);
4067 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4068 parameter is the PRAGMA token, allowing us to purge the entire pragma
4069 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4070 forwards (not error recovery). */
4073 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
4079 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4080 before an EOF token, even when the EOF is on the pragma line.
4081 We should never get here without being inside a deferred
4083 gcc_checking_assert (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
));
4084 token
= cp_lexer_consume_token (parser
->lexer
);
4086 while (token
->type
!= CPP_PRAGMA_EOL
);
4090 /* Ensure that the pragma is not parsed again. */
4091 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
4092 parser
->lexer
->in_pragma
= false;
4093 if (parser
->lexer
->in_omp_attribute_pragma
4094 && cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4096 parser
->lexer
= parser
->lexer
->next
;
4097 /* Put the current source position back where it was before this
4098 lexer was pushed. */
4099 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4104 /* Require pragma end of line, resyncing with it as necessary. The
4105 arguments are as for cp_parser_skip_to_pragma_eol. */
4108 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
4110 parser
->lexer
->in_pragma
= false;
4111 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
4112 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
4113 else if (parser
->lexer
->in_omp_attribute_pragma
4114 && cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4116 parser
->lexer
= parser
->lexer
->next
;
4117 /* Put the current source position back where it was before this
4118 lexer was pushed. */
4119 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4123 /* This is a simple wrapper around make_typename_type. When the id is
4124 an unresolved identifier node, we can provide a superior diagnostic
4125 using cp_parser_diagnose_invalid_type_name. */
4128 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
4129 location_t id_location
)
4132 if (identifier_p (id
))
4134 result
= make_typename_type (parser
->scope
, id
, typename_type
,
4135 /*complain=*/tf_none
);
4136 if (result
== error_mark_node
)
4137 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
4140 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
4143 /* This is a wrapper around the
4144 make_{pointer,ptrmem,reference}_declarator functions that decides
4145 which one to call based on the CODE and CLASS_TYPE arguments. The
4146 CODE argument should be one of the values returned by
4147 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4148 appertain to the pointer or reference. */
4150 static cp_declarator
*
4151 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
4152 cp_cv_quals cv_qualifiers
,
4153 cp_declarator
*target
,
4156 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
4157 return cp_error_declarator
;
4159 if (code
== INDIRECT_REF
)
4160 if (class_type
== NULL_TREE
)
4161 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
4163 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
4164 target
, attributes
);
4165 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
4166 return make_reference_declarator (cv_qualifiers
, target
,
4168 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
4169 return make_reference_declarator (cv_qualifiers
, target
,
4174 /* Create a new C++ parser. */
4177 cp_parser_new (cp_lexer
*lexer
)
4179 /* Initialize the binops_by_token so that we can get the tree
4180 directly from the token. */
4181 for (unsigned i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
4182 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
4184 cp_parser
*parser
= ggc_cleared_alloc
<cp_parser
> ();
4185 parser
->lexer
= lexer
;
4186 parser
->context
= cp_parser_context_new (NULL
);
4188 /* For now, we always accept GNU extensions. */
4189 parser
->allow_gnu_extensions_p
= 1;
4191 /* The `>' token is a greater-than operator, not the end of a
4193 parser
->greater_than_is_operator_p
= true;
4195 parser
->default_arg_ok_p
= true;
4197 /* We are not parsing a constant-expression. */
4198 parser
->integral_constant_expression_p
= false;
4199 parser
->allow_non_integral_constant_expression_p
= false;
4200 parser
->non_integral_constant_expression_p
= false;
4202 /* Local variable names are not forbidden. */
4203 parser
->local_variables_forbidden_p
= 0;
4205 /* We are not processing an `extern "C"' declaration. */
4206 parser
->in_unbraced_linkage_specification_p
= false;
4208 /* We are not processing a declarator. */
4209 parser
->in_declarator_p
= false;
4211 /* We are not processing a template-argument-list. */
4212 parser
->in_template_argument_list_p
= false;
4214 /* We are not in an iteration statement. */
4215 parser
->in_statement
= 0;
4217 /* We are not in a switch statement. */
4218 parser
->in_switch_statement_p
= false;
4220 /* We are not parsing a type-id inside an expression. */
4221 parser
->in_type_id_in_expr_p
= false;
4223 /* String literals should be translated to the execution character set. */
4224 parser
->translate_strings_p
= true;
4226 /* We are not parsing a function body. */
4227 parser
->in_function_body
= false;
4229 /* We can correct until told otherwise. */
4230 parser
->colon_corrects_to_scope_p
= true;
4232 /* The unparsed function queue is empty. */
4233 push_unparsed_function_queues (parser
);
4235 /* There are no classes being defined. */
4236 parser
->num_classes_being_defined
= 0;
4238 /* No template parameters apply. */
4239 parser
->num_template_parameter_lists
= 0;
4241 /* Special parsing data structures. */
4242 parser
->omp_declare_simd
= NULL
;
4243 parser
->oacc_routine
= NULL
;
4245 /* Not declaring an implicit function template. */
4246 parser
->auto_is_implicit_function_template_parm_p
= false;
4247 parser
->fully_implicit_function_template_p
= false;
4248 parser
->implicit_template_parms
= 0;
4249 parser
->implicit_template_scope
= 0;
4251 /* Allow constrained-type-specifiers. */
4252 parser
->prevent_constrained_type_specifiers
= 0;
4254 /* We haven't yet seen an 'extern "C"'. */
4255 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4260 /* Create a cp_lexer structure which will emit the tokens in CACHE
4261 and push it onto the parser's lexer stack. This is used for delayed
4262 parsing of in-class method bodies and default arguments, and should
4263 not be confused with tentative parsing. */
4265 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4267 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4268 lexer
->next
= parser
->lexer
;
4269 parser
->lexer
= lexer
;
4271 /* Move the current source position to that of the first token in the
4273 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4276 /* Pop the top lexer off the parser stack. This is never used for the
4277 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4279 cp_parser_pop_lexer (cp_parser
*parser
)
4281 cp_lexer
*lexer
= parser
->lexer
;
4282 parser
->lexer
= lexer
->next
;
4283 cp_lexer_destroy (lexer
);
4285 /* Put the current source position back where it was before this
4286 lexer was pushed. */
4287 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4290 /* Lexical conventions [gram.lex] */
4292 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4296 cp_parser_identifier (cp_parser
* parser
)
4300 /* Look for the identifier. */
4301 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4302 /* Return the value. */
4304 return cp_expr (token
->u
.value
, token
->location
);
4306 return error_mark_node
;
4309 /* Parse a sequence of adjacent string constants. Returns a
4310 TREE_STRING representing the combined, nul-terminated string
4311 constant. If TRANSLATE is true, translate the string to the
4312 execution character set. If WIDE_OK is true, a wide string is
4315 C++98 [lex.string] says that if a narrow string literal token is
4316 adjacent to a wide string literal token, the behavior is undefined.
4317 However, C99 6.4.5p4 says that this results in a wide string literal.
4318 We follow C99 here, for consistency with the C front end.
4320 This code is largely lifted from lex_string() in c-lex.c.
4322 FUTURE: ObjC++ will need to handle @-strings here. */
4324 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4325 bool lookup_udlit
= true)
4329 struct obstack str_ob
;
4330 struct obstack loc_ob
;
4331 cpp_string str
, istr
, *strs
;
4333 enum cpp_ttype type
, curr_type
;
4334 int have_suffix_p
= 0;
4336 tree suffix_id
= NULL_TREE
;
4337 bool curr_tok_is_userdef_p
= false;
4339 tok
= cp_lexer_peek_token (parser
->lexer
);
4340 if (!cp_parser_is_string_literal (tok
))
4342 cp_parser_error (parser
, "expected string-literal");
4343 return error_mark_node
;
4346 location_t loc
= tok
->location
;
4348 if (cpp_userdef_string_p (tok
->type
))
4350 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4351 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4352 curr_tok_is_userdef_p
= true;
4356 string_tree
= tok
->u
.value
;
4357 curr_type
= tok
->type
;
4361 /* Try to avoid the overhead of creating and destroying an obstack
4362 for the common case of just one string. */
4363 if (!cp_parser_is_string_literal
4364 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4366 cp_lexer_consume_token (parser
->lexer
);
4368 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4369 str
.len
= TREE_STRING_LENGTH (string_tree
);
4372 if (curr_tok_is_userdef_p
)
4374 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4376 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4379 curr_type
= tok
->type
;
4385 location_t last_tok_loc
= tok
->location
;
4386 gcc_obstack_init (&str_ob
);
4387 gcc_obstack_init (&loc_ob
);
4392 cp_lexer_consume_token (parser
->lexer
);
4394 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4395 str
.len
= TREE_STRING_LENGTH (string_tree
);
4397 if (curr_tok_is_userdef_p
)
4399 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4400 if (have_suffix_p
== 0)
4402 suffix_id
= curr_suffix_id
;
4405 else if (have_suffix_p
== 1
4406 && curr_suffix_id
!= suffix_id
)
4408 error ("inconsistent user-defined literal suffixes"
4409 " %qD and %qD in string literal",
4410 suffix_id
, curr_suffix_id
);
4413 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4416 curr_type
= tok
->type
;
4418 if (type
!= curr_type
)
4420 if (type
== CPP_STRING
)
4422 else if (curr_type
!= CPP_STRING
)
4424 rich_location
rich_loc (line_table
, tok
->location
);
4425 rich_loc
.add_range (last_tok_loc
);
4426 error_at (&rich_loc
,
4427 "concatenation of string literals with "
4428 "conflicting encoding prefixes");
4432 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4433 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4435 last_tok_loc
= tok
->location
;
4437 tok
= cp_lexer_peek_token (parser
->lexer
);
4438 if (cpp_userdef_string_p (tok
->type
))
4440 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4441 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4442 curr_tok_is_userdef_p
= true;
4446 string_tree
= tok
->u
.value
;
4447 curr_type
= tok
->type
;
4448 curr_tok_is_userdef_p
= false;
4451 while (cp_parser_is_string_literal (tok
));
4453 /* A string literal built by concatenation has its caret=start at
4454 the start of the initial string, and its finish at the finish of
4455 the final string literal. */
4456 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4458 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4461 if (type
!= CPP_STRING
&& !wide_ok
)
4463 cp_parser_error (parser
, "a wide string is invalid in this context");
4467 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4468 (parse_in
, strs
, count
, &istr
, type
))
4470 value
= build_string (istr
.len
, (const char *)istr
.text
);
4471 free (CONST_CAST (unsigned char *, istr
.text
));
4474 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4475 gcc_assert (g_string_concat_db
);
4476 g_string_concat_db
->record_string_concatenation (count
, locs
);
4483 TREE_TYPE (value
) = char_array_type_node
;
4485 case CPP_UTF8STRING
:
4487 TREE_TYPE (value
) = char8_array_type_node
;
4489 TREE_TYPE (value
) = char_array_type_node
;
4492 TREE_TYPE (value
) = char16_array_type_node
;
4495 TREE_TYPE (value
) = char32_array_type_node
;
4498 TREE_TYPE (value
) = wchar_array_type_node
;
4502 value
= fix_string_type (value
);
4506 tree literal
= build_userdef_literal (suffix_id
, value
,
4507 OT_NONE
, NULL_TREE
);
4509 value
= cp_parser_userdef_string_literal (literal
);
4515 /* cpp_interpret_string has issued an error. */
4516 value
= error_mark_node
;
4520 obstack_free (&str_ob
, 0);
4521 obstack_free (&loc_ob
, 0);
4524 return cp_expr (value
, loc
);
4527 /* Look up a literal operator with the name and the exact arguments. */
4530 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4532 tree decl
= lookup_name (name
);
4533 if (!decl
|| !is_overloaded_fn (decl
))
4534 return error_mark_node
;
4536 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4540 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4546 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4547 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4549 tree tparm
= TREE_VALUE (parmtypes
);
4550 tree targ
= TREE_TYPE ((*args
)[ix
]);
4551 bool ptr
= TYPE_PTR_P (tparm
);
4552 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4553 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4555 || !same_type_p (TREE_TYPE (tparm
),
4561 && ix
== vec_safe_length (args
)
4562 /* May be this should be sufficient_parms_p instead,
4563 depending on how exactly should user-defined literals
4564 work in presence of default arguments on the literal
4565 operator parameters. */
4566 && parmtypes
== void_list_node
)
4571 return error_mark_node
;
4574 /* Parse a user-defined char constant. Returns a call to a user-defined
4575 literal operator taking the character as an argument. */
4578 cp_parser_userdef_char_literal (cp_parser
*parser
)
4580 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4581 tree literal
= token
->u
.value
;
4582 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4583 tree value
= USERDEF_LITERAL_VALUE (literal
);
4584 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4587 /* Build up a call to the user-defined operator */
4588 /* Lookup the name we got back from the id-expression. */
4590 vec_safe_push (args
, value
);
4591 decl
= lookup_literal_operator (name
, args
);
4592 if (!decl
|| decl
== error_mark_node
)
4594 error ("unable to find character literal operator %qD with %qT argument",
4595 name
, TREE_TYPE (value
));
4596 return error_mark_node
;
4598 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4602 /* A subroutine of cp_parser_userdef_numeric_literal to
4603 create a char... template parameter pack from a string node. */
4606 make_char_string_pack (tree value
)
4609 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4610 const char *str
= TREE_STRING_POINTER (value
);
4611 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4612 tree argvec
= make_tree_vec (1);
4614 /* Fill in CHARVEC with all of the parameters. */
4615 charvec
= make_tree_vec (len
);
4616 for (i
= 0; i
< len
; ++i
)
4618 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4619 cpp_string in
= { 3, s
};
4620 cpp_string out
= { 0, 0 };
4621 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4623 gcc_assert (out
.len
== 2);
4624 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4628 /* Build the argument packs. */
4629 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4631 TREE_VEC_ELT (argvec
, 0) = argpack
;
4636 /* A subroutine of cp_parser_userdef_numeric_literal to
4637 create a char... template parameter pack from a string node. */
4640 make_string_pack (tree value
)
4643 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4644 const unsigned char *str
4645 = (const unsigned char *) TREE_STRING_POINTER (value
);
4646 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4647 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4648 tree argvec
= make_tree_vec (2);
4650 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4651 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4653 /* First template parm is character type. */
4654 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4656 /* Fill in CHARVEC with all of the parameters. */
4657 charvec
= make_tree_vec (len
);
4658 for (int i
= 0; i
< len
; ++i
)
4659 TREE_VEC_ELT (charvec
, i
)
4660 = double_int_to_tree (str_char_type_node
,
4661 double_int::from_buffer (str
+ i
* sz
, sz
));
4663 /* Build the argument packs. */
4664 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4666 TREE_VEC_ELT (argvec
, 1) = argpack
;
4671 /* Parse a user-defined numeric constant. returns a call to a user-defined
4672 literal operator. */
4675 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4677 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4678 tree literal
= token
->u
.value
;
4679 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4680 tree value
= USERDEF_LITERAL_VALUE (literal
);
4681 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4682 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4683 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4686 /* Look for a literal operator taking the exact type of numeric argument
4687 as the literal value. */
4689 vec_safe_push (args
, value
);
4690 decl
= lookup_literal_operator (name
, args
);
4691 if (decl
&& decl
!= error_mark_node
)
4693 result
= finish_call_expr (decl
, &args
, false, true,
4694 tf_warning_or_error
);
4696 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4698 warning_at (token
->location
, OPT_Woverflow
,
4699 "integer literal exceeds range of %qT type",
4700 long_long_unsigned_type_node
);
4705 warning_at (token
->location
, OPT_Woverflow
,
4706 "floating literal exceeds range of %qT type",
4707 long_double_type_node
);
4708 else if (overflow
< 0)
4709 warning_at (token
->location
, OPT_Woverflow
,
4710 "floating literal truncated to zero");
4716 /* If the numeric argument didn't work, look for a raw literal
4717 operator taking a const char* argument consisting of the number
4718 in string format. */
4720 vec_safe_push (args
, num_string
);
4721 decl
= lookup_literal_operator (name
, args
);
4722 if (decl
&& decl
!= error_mark_node
)
4724 result
= finish_call_expr (decl
, &args
, false, true,
4725 tf_warning_or_error
);
4729 /* If the raw literal didn't work, look for a non-type template
4730 function with parameter pack char.... Call the function with
4731 template parameter characters representing the number. */
4733 decl
= lookup_literal_operator (name
, args
);
4734 if (decl
&& decl
!= error_mark_node
)
4736 tree tmpl_args
= make_char_string_pack (num_string
);
4737 if (tmpl_args
== NULL_TREE
)
4739 error ("failed to translate literal to execution character set %qT",
4741 return error_mark_node
;
4743 decl
= lookup_template_function (decl
, tmpl_args
);
4744 result
= finish_call_expr (decl
, &args
, false, true,
4745 tf_warning_or_error
);
4749 /* In C++14 the standard library defines complex number suffixes that
4750 conflict with GNU extensions. Prefer them if <complex> is #included. */
4751 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4752 bool i14
= (cxx_dialect
> cxx11
4753 && (id_equal (suffix_id
, "i")
4754 || id_equal (suffix_id
, "if")
4755 || id_equal (suffix_id
, "il")));
4756 diagnostic_t kind
= DK_ERROR
;
4761 tree cxlit
= lookup_qualified_name (std_node
, "complex_literals",
4762 LOOK_want::NORMAL
, false);
4763 if (cxlit
== error_mark_node
)
4765 /* No <complex>, so pedwarn and use GNU semantics. */
4767 opt
= OPT_Wpedantic
;
4772 = emit_diagnostic (kind
, input_location
, opt
,
4773 "unable to find numeric literal operator %qD", name
);
4776 /* Don't inform either. */;
4779 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4780 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4783 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4784 "GNU built-in suffix");
4787 inform (token
->location
, "use %<-fext-numeric-literals%> "
4788 "to enable more built-in suffixes");
4790 if (kind
== DK_ERROR
)
4791 value
= error_mark_node
;
4794 /* Use the built-in semantics. */
4796 if (id_equal (suffix_id
, "i"))
4798 if (TREE_CODE (value
) == INTEGER_CST
)
4799 type
= integer_type_node
;
4801 type
= double_type_node
;
4803 else if (id_equal (suffix_id
, "if"))
4804 type
= float_type_node
;
4805 else /* if (id_equal (suffix_id, "il")) */
4806 type
= long_double_type_node
;
4808 value
= fold_build2 (COMPLEX_EXPR
, build_complex_type (type
),
4809 build_zero_cst (type
), fold_convert (type
, value
));
4812 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4813 /* Avoid repeated diagnostics. */
4814 token
->u
.value
= value
;
4818 /* Parse a user-defined string constant. Returns a call to a user-defined
4819 literal operator taking a character pointer and the length of the string
4823 cp_parser_userdef_string_literal (tree literal
)
4825 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4826 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4827 tree value
= USERDEF_LITERAL_VALUE (literal
);
4828 int len
= TREE_STRING_LENGTH (value
)
4829 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4832 /* Build up a call to the user-defined operator. */
4833 /* Lookup the name we got back from the id-expression. */
4835 vec_safe_push (args
, value
);
4836 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4837 decl
= lookup_literal_operator (name
, args
);
4839 if (decl
&& decl
!= error_mark_node
)
4840 return finish_call_expr (decl
, &args
, false, true,
4841 tf_warning_or_error
);
4843 /* Look for a suitable template function, either (C++20) with a single
4844 parameter of class type, or (N3599) with typename parameter CharT and
4845 parameter pack CharT... */
4847 decl
= lookup_literal_operator (name
, args
);
4848 if (decl
&& decl
!= error_mark_node
)
4850 /* Use resolve_nondeduced_context to try to choose one form of template
4852 tree tmpl_args
= make_tree_vec (1);
4853 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4854 decl
= lookup_template_function (decl
, tmpl_args
);
4855 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4860 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4861 res
= resolve_nondeduced_context (decl
, tf_none
);
4865 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4866 TREE_OPERAND (decl
, 1) = tmpl_args
;
4867 return finish_call_expr (decl
, &args
, false, true,
4868 tf_warning_or_error
);
4871 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4872 name
, TREE_TYPE (value
), size_type_node
);
4873 return error_mark_node
;
4877 /* Basic concepts [gram.basic] */
4879 /* Parse a translation-unit.
4882 declaration-seq [opt] */
4885 cp_parser_translation_unit (cp_parser
* parser
)
4887 gcc_checking_assert (!cp_error_declarator
);
4889 /* Create the declarator obstack. */
4890 gcc_obstack_init (&declarator_obstack
);
4891 /* Create the error declarator. */
4892 cp_error_declarator
= make_declarator (cdk_error
);
4893 /* Create the empty parameter list. */
4894 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4896 /* Remember where the base of the declarator obstack lies. */
4897 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4899 push_deferring_access_checks (flag_access_control
4900 ? dk_no_deferred
: dk_no_check
);
4902 module_parse mp_state
= MP_NOT_MODULE
;
4903 if (modules_p () && !header_module_p ())
4904 mp_state
= MP_FIRST
;
4906 bool implicit_extern_c
= false;
4908 /* Parse until EOF. */
4911 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4913 /* If we're entering or exiting a region that's implicitly
4914 extern "C", modify the lang context appropriately. This is
4915 so horrible. Please die. */
4916 if (implicit_extern_c
4917 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4919 implicit_extern_c
= !implicit_extern_c
;
4920 if (implicit_extern_c
)
4921 push_lang_context (lang_name_c
);
4923 pop_lang_context ();
4926 if (token
->type
== CPP_EOF
)
4931 /* Top-level module declarations are ok, and change the
4932 portion of file we're in. Top-level import declarations
4933 are significant for the import portions. */
4935 cp_token
*next
= token
;
4936 bool exporting
= token
->keyword
== RID__EXPORT
;
4939 cp_lexer_consume_token (parser
->lexer
);
4940 next
= cp_lexer_peek_token (parser
->lexer
);
4942 if (next
->keyword
== RID__MODULE
)
4945 = cp_parser_module_declaration (parser
, mp_state
, exporting
);
4948 else if (next
->keyword
== RID__IMPORT
)
4950 if (mp_state
== MP_FIRST
)
4951 mp_state
= MP_NOT_MODULE
;
4952 cp_parser_import_declaration (parser
, mp_state
, exporting
);
4956 gcc_checking_assert (!exporting
);
4958 if (mp_state
== MP_GLOBAL
&& token
->main_source_p
)
4960 static bool warned
= false;
4964 error_at (token
->location
,
4965 "global module fragment contents must be"
4966 " from preprocessor inclusion");
4971 /* This relies on the ordering of module_parse values. */
4972 if (mp_state
== MP_PURVIEW_IMPORTS
|| mp_state
== MP_PRIVATE_IMPORTS
)
4973 /* We're no longer in the import portion of a named module. */
4974 mp_state
= module_parse (mp_state
+ 1);
4975 else if (mp_state
== MP_FIRST
)
4976 mp_state
= MP_NOT_MODULE
;
4978 if (token
->type
== CPP_CLOSE_BRACE
)
4980 cp_parser_error (parser
, "expected declaration");
4981 cp_lexer_consume_token (parser
->lexer
);
4982 /* If the next token is now a `;', consume it. */
4983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4984 cp_lexer_consume_token (parser
->lexer
);
4987 cp_parser_toplevel_declaration (parser
);
4990 /* Get rid of the token array; we don't need it any more. */
4991 cp_lexer_destroy (parser
->lexer
);
4992 parser
->lexer
= NULL
;
4994 /* The EOF should have reset this. */
4995 gcc_checking_assert (!implicit_extern_c
);
4997 /* Make sure the declarator obstack was fully cleaned up. */
4998 gcc_assert (obstack_next_free (&declarator_obstack
)
4999 == declarator_obstack_base
);
5002 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5003 decltype context. */
5005 static inline tsubst_flags_t
5006 complain_flags (bool decltype_p
)
5008 tsubst_flags_t complain
= tf_warning_or_error
;
5010 complain
|= tf_decltype
;
5014 /* We're about to parse a collection of statements. If we're currently
5015 parsing tentatively, set up a firewall so that any nested
5016 cp_parser_commit_to_tentative_parse won't affect the current context. */
5018 static cp_token_position
5019 cp_parser_start_tentative_firewall (cp_parser
*parser
)
5021 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5024 cp_parser_parse_tentatively (parser
);
5025 cp_parser_commit_to_topmost_tentative_parse (parser
);
5026 return cp_lexer_token_position (parser
->lexer
, false);
5029 /* We've finished parsing the collection of statements. Wrap up the
5030 firewall and replace the relevant tokens with the parsed form. */
5033 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
5039 /* Finish the firewall level. */
5040 cp_parser_parse_definitely (parser
);
5041 /* And remember the result of the parse for when we try again. */
5042 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
5043 token
->type
= CPP_PREPARSED_EXPR
;
5044 token
->u
.value
= expr
;
5045 token
->keyword
= RID_MAX
;
5046 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5049 /* Like the above functions, but let the user modify the tokens. Used by
5050 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5051 later parses, so it makes sense to localize the effects of
5052 cp_parser_commit_to_tentative_parse. */
5054 struct tentative_firewall
5059 tentative_firewall (cp_parser
*p
): parser(p
)
5061 /* If we're currently parsing tentatively, start a committed level as a
5062 firewall and then an inner tentative parse. */
5063 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
5065 cp_parser_parse_tentatively (parser
);
5066 cp_parser_commit_to_topmost_tentative_parse (parser
);
5067 cp_parser_parse_tentatively (parser
);
5071 ~tentative_firewall()
5075 /* Finish the inner tentative parse and the firewall, propagating any
5076 uncommitted error state to the outer tentative parse. */
5077 bool err
= cp_parser_error_occurred (parser
);
5078 cp_parser_parse_definitely (parser
);
5079 cp_parser_parse_definitely (parser
);
5081 cp_parser_simulate_error (parser
);
5086 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5087 This class is for tracking such a matching pair of symbols.
5088 In particular, it tracks the location of the first token,
5089 so that if the second token is missing, we can highlight the
5090 location of the first token when notifying the user about the
5093 template <typename traits_t
>
5097 /* token_pair's ctor. */
5098 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
5100 /* If the next token is the opening symbol for this pair, consume it and
5102 Otherwise, issue an error and return false.
5103 In either case, record the location of the opening token. */
5105 bool require_open (cp_parser
*parser
)
5107 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5108 return cp_parser_require (parser
, traits_t::open_token_type
,
5109 traits_t::required_token_open
);
5112 /* Consume the next token from PARSER, recording its location as
5113 that of the opening token within the pair. */
5115 cp_token
* consume_open (cp_parser
*parser
)
5117 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
5118 gcc_assert (tok
->type
== traits_t::open_token_type
);
5119 m_open_loc
= tok
->location
;
5123 /* If the next token is the closing symbol for this pair, consume it
5125 Otherwise, issue an error, highlighting the location of the
5126 corresponding opening token, and return NULL. */
5128 cp_token
*require_close (cp_parser
*parser
) const
5130 return cp_parser_require (parser
, traits_t::close_token_type
,
5131 traits_t::required_token_close
,
5135 location_t
open_location () const { return m_open_loc
; }
5138 location_t m_open_loc
;
5141 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5143 struct matching_paren_traits
5145 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
5146 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
5147 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
5148 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
5151 /* "matching_parens" is a token_pair<T> class for tracking matching
5152 pairs of parentheses. */
5154 typedef token_pair
<matching_paren_traits
> matching_parens
;
5156 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5158 struct matching_brace_traits
5160 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
5161 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
5162 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
5163 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
5166 /* "matching_braces" is a token_pair<T> class for tracking matching
5169 typedef token_pair
<matching_brace_traits
> matching_braces
;
5172 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5173 enclosing parentheses. */
5176 cp_parser_statement_expr (cp_parser
*parser
)
5178 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
5180 /* Consume the '('. */
5181 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5182 matching_parens parens
;
5183 parens
.consume_open (parser
);
5184 /* Start the statement-expression. */
5185 tree expr
= begin_stmt_expr ();
5186 /* Parse the compound-statement. */
5187 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
5189 expr
= finish_stmt_expr (expr
, false);
5190 /* Consume the ')'. */
5191 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5192 if (!parens
.require_close (parser
))
5193 cp_parser_skip_to_end_of_statement (parser
);
5195 cp_parser_end_tentative_firewall (parser
, start
, expr
);
5196 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
5197 return cp_expr (expr
, combined_loc
);
5200 /* Expressions [gram.expr] */
5202 /* Parse a fold-operator.
5205 - * / % ^ & | = < > << >>
5206 = -= *= /= %= ^= &= |= <<= >>=
5207 == != <= >= && || , .* ->*
5209 This returns the tree code corresponding to the matched operator
5210 as an int. When the current token matches a compound assignment
5211 operator, the resulting tree code is the negative value of the
5212 non-assignment operator. */
5215 cp_parser_fold_operator (cp_token
*token
)
5217 switch (token
->type
)
5219 case CPP_PLUS
: return PLUS_EXPR
;
5220 case CPP_MINUS
: return MINUS_EXPR
;
5221 case CPP_MULT
: return MULT_EXPR
;
5222 case CPP_DIV
: return TRUNC_DIV_EXPR
;
5223 case CPP_MOD
: return TRUNC_MOD_EXPR
;
5224 case CPP_XOR
: return BIT_XOR_EXPR
;
5225 case CPP_AND
: return BIT_AND_EXPR
;
5226 case CPP_OR
: return BIT_IOR_EXPR
;
5227 case CPP_LSHIFT
: return LSHIFT_EXPR
;
5228 case CPP_RSHIFT
: return RSHIFT_EXPR
;
5230 case CPP_EQ
: return -NOP_EXPR
;
5231 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
5232 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
5233 case CPP_MULT_EQ
: return -MULT_EXPR
;
5234 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
5235 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
5236 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
5237 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
5238 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
5239 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
5240 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
5242 case CPP_EQ_EQ
: return EQ_EXPR
;
5243 case CPP_NOT_EQ
: return NE_EXPR
;
5244 case CPP_LESS
: return LT_EXPR
;
5245 case CPP_GREATER
: return GT_EXPR
;
5246 case CPP_LESS_EQ
: return LE_EXPR
;
5247 case CPP_GREATER_EQ
: return GE_EXPR
;
5249 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
5250 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
5252 case CPP_COMMA
: return COMPOUND_EXPR
;
5254 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
5255 case CPP_DEREF_STAR
: return MEMBER_REF
;
5257 default: return ERROR_MARK
;
5261 /* Returns true if CODE indicates a binary expression, which is not allowed in
5262 the LHS of a fold-expression. More codes will need to be added to use this
5263 function in other contexts. */
5266 is_binary_op (tree_code code
)
5271 case POINTER_PLUS_EXPR
:
5274 case TRUNC_DIV_EXPR
:
5275 case TRUNC_MOD_EXPR
:
5291 case TRUTH_ANDIF_EXPR
:
5292 case TRUTH_ORIF_EXPR
:
5305 /* If the next token is a suitable fold operator, consume it and return as
5306 the function above. */
5309 cp_parser_fold_operator (cp_parser
*parser
)
5311 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5312 int code
= cp_parser_fold_operator (token
);
5313 if (code
!= ERROR_MARK
)
5314 cp_lexer_consume_token (parser
->lexer
);
5318 /* Parse a fold-expression.
5321 ( ... folding-operator cast-expression)
5322 ( cast-expression folding-operator ... )
5323 ( cast-expression folding operator ... folding-operator cast-expression)
5325 Note that the '(' and ')' are matched in primary expression. */
5328 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5336 return error_mark_node
;
5337 cp_lexer_consume_token (parser
->lexer
);
5338 int op
= cp_parser_fold_operator (parser
);
5339 if (op
== ERROR_MARK
)
5341 cp_parser_error (parser
, "expected binary operator");
5342 return error_mark_node
;
5345 tree expr
= cp_parser_cast_expression (parser
, false, false,
5347 if (expr
== error_mark_node
)
5348 return error_mark_node
;
5349 return finish_left_unary_fold_expr (expr
, op
);
5352 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5353 int op
= cp_parser_fold_operator (parser
);
5354 if (op
== ERROR_MARK
)
5356 cp_parser_error (parser
, "expected binary operator");
5357 return error_mark_node
;
5360 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5362 cp_parser_error (parser
, "expected ...");
5363 return error_mark_node
;
5365 cp_lexer_consume_token (parser
->lexer
);
5367 /* The operands of a fold-expression are cast-expressions, so binary or
5368 conditional expressions are not allowed. We check this here to avoid
5369 tentative parsing. */
5370 if (EXPR_P (expr1
) && warning_suppressed_p (expr1
, OPT_Wparentheses
))
5371 /* OK, the expression was parenthesized. */;
5372 else if (is_binary_op (TREE_CODE (expr1
)))
5373 error_at (location_of (expr1
),
5374 "binary expression in operand of fold-expression");
5375 else if (TREE_CODE (expr1
) == COND_EXPR
5376 || (REFERENCE_REF_P (expr1
)
5377 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5378 error_at (location_of (expr1
),
5379 "conditional expression in operand of fold-expression");
5382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5383 return finish_right_unary_fold_expr (expr1
, op
);
5385 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5387 cp_parser_error (parser
, "mismatched operator in fold-expression");
5388 return error_mark_node
;
5390 cp_lexer_consume_token (parser
->lexer
);
5392 // Binary left or right fold.
5393 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5394 if (expr2
== error_mark_node
)
5395 return error_mark_node
;
5396 return finish_binary_fold_expr (expr1
, expr2
, op
);
5399 /* Parse a primary-expression.
5406 lambda-expression (C++11)
5411 ( compound-statement )
5412 __builtin_va_arg ( assignment-expression , type-id )
5413 __builtin_offsetof ( type-id , offsetof-expression )
5416 __has_nothrow_assign ( type-id )
5417 __has_nothrow_constructor ( type-id )
5418 __has_nothrow_copy ( type-id )
5419 __has_trivial_assign ( type-id )
5420 __has_trivial_constructor ( type-id )
5421 __has_trivial_copy ( type-id )
5422 __has_trivial_destructor ( type-id )
5423 __has_virtual_destructor ( type-id )
5424 __is_abstract ( type-id )
5425 __is_base_of ( type-id , type-id )
5426 __is_class ( type-id )
5427 __is_empty ( type-id )
5428 __is_enum ( type-id )
5429 __is_final ( type-id )
5430 __is_literal_type ( type-id )
5431 __is_pod ( type-id )
5432 __is_polymorphic ( type-id )
5433 __is_std_layout ( type-id )
5434 __is_trivial ( type-id )
5435 __is_union ( type-id )
5437 Objective-C++ Extension:
5445 ADDRESS_P is true iff this expression was immediately preceded by
5446 "&" and therefore might denote a pointer-to-member. CAST_P is true
5447 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5448 true iff this expression is a template argument.
5450 Returns a representation of the expression. Upon return, *IDK
5451 indicates what kind of id-expression (if any) was present. */
5454 cp_parser_primary_expression (cp_parser
*parser
,
5457 bool template_arg_p
,
5461 cp_token
*token
= NULL
;
5463 /* Assume the primary expression is not an id-expression. */
5464 *idk
= CP_ID_KIND_NONE
;
5466 /* Peek at the next token. */
5467 token
= cp_lexer_peek_token (parser
->lexer
);
5468 switch ((int) token
->type
)
5477 user-defined-literal */
5484 case CPP_PREPARSED_EXPR
:
5485 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5486 return cp_parser_userdef_numeric_literal (parser
);
5487 token
= cp_lexer_consume_token (parser
->lexer
);
5488 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5490 error_at (token
->location
,
5491 "fixed-point types not supported in C++");
5492 return error_mark_node
;
5494 /* Floating-point literals are only allowed in an integral
5495 constant expression if they are cast to an integral or
5496 enumeration type. */
5497 if (TREE_CODE (token
->u
.value
) == REAL_CST
5498 && parser
->integral_constant_expression_p
5501 /* CAST_P will be set even in invalid code like "int(2.7 +
5502 ...)". Therefore, we have to check that the next token
5503 is sure to end the cast. */
5506 cp_token
*next_token
;
5508 next_token
= cp_lexer_peek_token (parser
->lexer
);
5509 if (/* The comma at the end of an
5510 enumerator-definition. */
5511 next_token
->type
!= CPP_COMMA
5512 /* The curly brace at the end of an enum-specifier. */
5513 && next_token
->type
!= CPP_CLOSE_BRACE
5514 /* The end of a statement. */
5515 && next_token
->type
!= CPP_SEMICOLON
5516 /* The end of the cast-expression. */
5517 && next_token
->type
!= CPP_CLOSE_PAREN
5518 /* The end of an array bound. */
5519 && next_token
->type
!= CPP_CLOSE_SQUARE
5520 /* The closing ">" in a template-argument-list. */
5521 && (next_token
->type
!= CPP_GREATER
5522 || parser
->greater_than_is_operator_p
)
5523 /* C++0x only: A ">>" treated like two ">" tokens,
5524 in a template-argument-list. */
5525 && (next_token
->type
!= CPP_RSHIFT
5526 || (cxx_dialect
== cxx98
)
5527 || parser
->greater_than_is_operator_p
))
5531 /* If we are within a cast, then the constraint that the
5532 cast is to an integral or enumeration type will be
5533 checked at that point. If we are not within a cast, then
5534 this code is invalid. */
5536 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5538 return (cp_expr (token
->u
.value
, token
->location
)
5539 .maybe_add_location_wrapper ());
5541 case CPP_CHAR_USERDEF
:
5542 case CPP_CHAR16_USERDEF
:
5543 case CPP_CHAR32_USERDEF
:
5544 case CPP_WCHAR_USERDEF
:
5545 case CPP_UTF8CHAR_USERDEF
:
5546 return cp_parser_userdef_char_literal (parser
);
5552 case CPP_UTF8STRING
:
5553 case CPP_STRING_USERDEF
:
5554 case CPP_STRING16_USERDEF
:
5555 case CPP_STRING32_USERDEF
:
5556 case CPP_WSTRING_USERDEF
:
5557 case CPP_UTF8STRING_USERDEF
:
5558 /* ??? Should wide strings be allowed when parser->translate_strings_p
5559 is false (i.e. in attributes)? If not, we can kill the third
5560 argument to cp_parser_string_literal. */
5561 return (cp_parser_string_literal (parser
,
5562 parser
->translate_strings_p
,
5564 .maybe_add_location_wrapper ());
5566 case CPP_OPEN_PAREN
:
5567 /* If we see `( { ' then we are looking at the beginning of
5568 a GNU statement-expression. */
5569 if (cp_parser_allow_gnu_extensions_p (parser
)
5570 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5572 /* Statement-expressions are not allowed by the standard. */
5573 pedwarn (token
->location
, OPT_Wpedantic
,
5574 "ISO C++ forbids braced-groups within expressions");
5576 /* And they're not allowed outside of a function-body; you
5577 cannot, for example, write:
5579 int i = ({ int j = 3; j + 1; });
5581 at class or namespace scope. */
5582 if (!parser
->in_function_body
5583 || parser
->in_template_argument_list_p
)
5585 error_at (token
->location
,
5586 "statement-expressions are not allowed outside "
5587 "functions nor in template-argument lists");
5588 cp_parser_skip_to_end_of_block_or_statement (parser
);
5589 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5590 cp_lexer_consume_token (parser
->lexer
);
5591 return error_mark_node
;
5594 return cp_parser_statement_expr (parser
);
5596 /* Otherwise it's a normal parenthesized expression. */
5599 bool saved_greater_than_is_operator_p
;
5601 location_t open_paren_loc
= token
->location
;
5603 /* Consume the `('. */
5604 matching_parens parens
;
5605 parens
.consume_open (parser
);
5606 /* Within a parenthesized expression, a `>' token is always
5607 the greater-than operator. */
5608 saved_greater_than_is_operator_p
5609 = parser
->greater_than_is_operator_p
;
5610 parser
->greater_than_is_operator_p
= true;
5612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5613 /* Left fold expression. */
5616 /* Parse the parenthesized expression. */
5617 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5619 token
= cp_lexer_peek_token (parser
->lexer
);
5620 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5622 expr
= cp_parser_fold_expression (parser
, expr
);
5623 if (expr
!= error_mark_node
5624 && cxx_dialect
< cxx17
)
5625 pedwarn (input_location
, OPT_Wc__17_extensions
,
5626 "fold-expressions only available with %<-std=c++17%> "
5627 "or %<-std=gnu++17%>");
5630 /* Let the front end know that this expression was
5631 enclosed in parentheses. This matters in case, for
5632 example, the expression is of the form `A::B', since
5633 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5635 expr
= finish_parenthesized_expr (expr
);
5637 /* DR 705: Wrapping an unqualified name in parentheses
5638 suppresses arg-dependent lookup. We want to pass back
5639 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5640 (c++/37862), but none of the others. */
5641 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5642 *idk
= CP_ID_KIND_NONE
;
5644 /* The `>' token might be the end of a template-id or
5645 template-parameter-list now. */
5646 parser
->greater_than_is_operator_p
5647 = saved_greater_than_is_operator_p
;
5649 /* Consume the `)'. */
5650 token
= cp_lexer_peek_token (parser
->lexer
);
5651 location_t close_paren_loc
= token
->location
;
5652 bool no_wparens
= warning_suppressed_p (expr
, OPT_Wparentheses
);
5653 expr
.set_range (open_paren_loc
, close_paren_loc
);
5655 suppress_warning (expr
, OPT_Wparentheses
);
5656 if (!parens
.require_close (parser
)
5657 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5658 cp_parser_skip_to_end_of_statement (parser
);
5663 case CPP_OPEN_SQUARE
:
5665 if (c_dialect_objc ())
5667 /* We might have an Objective-C++ message. */
5668 cp_parser_parse_tentatively (parser
);
5669 tree msg
= cp_parser_objc_message_expression (parser
);
5670 /* If that works out, we're done ... */
5671 if (cp_parser_parse_definitely (parser
))
5673 /* ... else, fall though to see if it's a lambda. */
5675 cp_expr lam
= cp_parser_lambda_expression (parser
);
5676 /* Don't warn about a failed tentative parse. */
5677 if (cp_parser_error_occurred (parser
))
5678 return error_mark_node
;
5679 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5683 case CPP_OBJC_STRING
:
5684 if (c_dialect_objc ())
5685 /* We have an Objective-C++ string literal. */
5686 return cp_parser_objc_expression (parser
);
5687 cp_parser_error (parser
, "expected primary-expression");
5688 return error_mark_node
;
5691 switch (token
->keyword
)
5693 /* These two are the boolean literals. */
5695 cp_lexer_consume_token (parser
->lexer
);
5696 return cp_expr (boolean_true_node
, token
->location
);
5698 cp_lexer_consume_token (parser
->lexer
);
5699 return cp_expr (boolean_false_node
, token
->location
);
5701 /* The `__null' literal. */
5703 cp_lexer_consume_token (parser
->lexer
);
5704 return cp_expr (null_node
, token
->location
);
5706 /* The `nullptr' literal. */
5708 cp_lexer_consume_token (parser
->lexer
);
5709 return cp_expr (nullptr_node
, token
->location
);
5711 /* Recognize the `this' keyword. */
5713 cp_lexer_consume_token (parser
->lexer
);
5714 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5716 error_at (token
->location
,
5717 "%<this%> may not be used in this context");
5718 return error_mark_node
;
5720 /* Pointers cannot appear in constant-expressions. */
5721 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5722 return error_mark_node
;
5723 return cp_expr (finish_this_expr (), token
->location
);
5725 /* The `operator' keyword can be the beginning of an
5730 case RID_FUNCTION_NAME
:
5731 case RID_PRETTY_FUNCTION_NAME
:
5732 case RID_C99_FUNCTION_NAME
:
5734 non_integral_constant name
;
5736 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5737 __func__ are the names of variables -- but they are
5738 treated specially. Therefore, they are handled here,
5739 rather than relying on the generic id-expression logic
5740 below. Grammatically, these names are id-expressions.
5742 Consume the token. */
5743 token
= cp_lexer_consume_token (parser
->lexer
);
5745 switch (token
->keyword
)
5747 case RID_FUNCTION_NAME
:
5748 name
= NIC_FUNC_NAME
;
5750 case RID_PRETTY_FUNCTION_NAME
:
5751 name
= NIC_PRETTY_FUNC
;
5753 case RID_C99_FUNCTION_NAME
:
5754 name
= NIC_C99_FUNC
;
5760 if (cp_parser_non_integral_constant_expression (parser
, name
))
5761 return error_mark_node
;
5763 /* Look up the name. */
5764 return finish_fname (token
->u
.value
);
5771 location_t type_location
;
5772 location_t start_loc
5773 = cp_lexer_peek_token (parser
->lexer
)->location
;
5774 /* The `__builtin_va_arg' construct is used to handle
5775 `va_arg'. Consume the `__builtin_va_arg' token. */
5776 cp_lexer_consume_token (parser
->lexer
);
5777 /* Look for the opening `('. */
5778 matching_parens parens
;
5779 parens
.require_open (parser
);
5780 /* Now, parse the assignment-expression. */
5781 expression
= cp_parser_assignment_expression (parser
);
5782 /* Look for the `,'. */
5783 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5784 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5785 /* Parse the type-id. */
5787 type_id_in_expr_sentinel
s (parser
);
5788 type
= cp_parser_type_id (parser
);
5790 /* Look for the closing `)'. */
5791 location_t finish_loc
5792 = cp_lexer_peek_token (parser
->lexer
)->location
;
5793 parens
.require_close (parser
);
5794 /* Using `va_arg' in a constant-expression is not
5796 if (cp_parser_non_integral_constant_expression (parser
,
5798 return error_mark_node
;
5799 /* Construct a location of the form:
5800 __builtin_va_arg (v, int)
5801 ~~~~~~~~~~~~~~~~~~~~~^~~~
5802 with the caret at the type, ranging from the start of the
5803 "__builtin_va_arg" token to the close paren. */
5804 location_t combined_loc
5805 = make_location (type_location
, start_loc
, finish_loc
);
5806 return build_x_va_arg (combined_loc
, expression
, type
);
5810 return cp_parser_builtin_offsetof (parser
);
5812 case RID_HAS_NOTHROW_ASSIGN
:
5813 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5814 case RID_HAS_NOTHROW_COPY
:
5815 case RID_HAS_TRIVIAL_ASSIGN
:
5816 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5817 case RID_HAS_TRIVIAL_COPY
:
5818 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5819 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5820 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5821 case RID_IS_ABSTRACT
:
5822 case RID_IS_AGGREGATE
:
5823 case RID_IS_BASE_OF
:
5828 case RID_IS_LAYOUT_COMPATIBLE
:
5829 case RID_IS_LITERAL_TYPE
:
5830 case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF
:
5832 case RID_IS_POLYMORPHIC
:
5833 case RID_IS_SAME_AS
:
5834 case RID_IS_STD_LAYOUT
:
5835 case RID_IS_TRIVIAL
:
5836 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5837 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5838 case RID_IS_TRIVIALLY_COPYABLE
:
5840 case RID_IS_ASSIGNABLE
:
5841 case RID_IS_CONSTRUCTIBLE
:
5842 case RID_IS_NOTHROW_ASSIGNABLE
:
5843 case RID_IS_NOTHROW_CONSTRUCTIBLE
:
5844 return cp_parser_trait_expr (parser
, token
->keyword
);
5848 return cp_parser_requires_expression (parser
);
5850 /* Objective-C++ expressions. */
5852 case RID_AT_PROTOCOL
:
5853 case RID_AT_SELECTOR
:
5854 return cp_parser_objc_expression (parser
);
5857 if (parser
->in_function_body
5858 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5861 error_at (token
->location
,
5862 "a template declaration cannot appear at block scope");
5863 cp_parser_skip_to_end_of_block_or_statement (parser
);
5864 return error_mark_node
;
5868 cp_parser_error (parser
, "expected primary-expression");
5869 return error_mark_node
;
5872 /* An id-expression can start with either an identifier, a
5873 `::' as the beginning of a qualified-id, or the "operator"
5877 case CPP_TEMPLATE_ID
:
5878 case CPP_NESTED_NAME_SPECIFIER
:
5881 cp_expr id_expression
;
5883 const char *error_msg
;
5886 cp_token
*id_expr_token
;
5888 /* Parse the id-expression. */
5890 = cp_parser_id_expression (parser
,
5891 /*template_keyword_p=*/false,
5892 /*check_dependency_p=*/true,
5894 /*declarator_p=*/false,
5895 /*optional_p=*/false);
5896 if (id_expression
== error_mark_node
)
5897 return error_mark_node
;
5898 id_expr_token
= token
;
5899 token
= cp_lexer_peek_token (parser
->lexer
);
5900 done
= (token
->type
!= CPP_OPEN_SQUARE
5901 && token
->type
!= CPP_OPEN_PAREN
5902 && token
->type
!= CPP_DOT
5903 && token
->type
!= CPP_DEREF
5904 && token
->type
!= CPP_PLUS_PLUS
5905 && token
->type
!= CPP_MINUS_MINUS
);
5906 /* If we have a template-id, then no further lookup is
5907 required. If the template-id was for a template-class, we
5908 will sometimes have a TYPE_DECL at this point. */
5909 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5910 || TREE_CODE (id_expression
) == TYPE_DECL
)
5911 decl
= id_expression
;
5912 /* Look up the name. */
5915 tree ambiguous_decls
;
5917 /* If we already know that this lookup is ambiguous, then
5918 we've already issued an error message; there's no reason
5920 if (id_expr_token
->type
== CPP_NAME
5921 && id_expr_token
->error_reported
)
5923 cp_parser_simulate_error (parser
);
5924 return error_mark_node
;
5927 decl
= cp_parser_lookup_name (parser
, id_expression
,
5930 /*is_namespace=*/false,
5931 /*check_dependency=*/true,
5933 id_expression
.get_location ());
5934 /* If the lookup was ambiguous, an error will already have
5936 if (ambiguous_decls
)
5937 return error_mark_node
;
5939 /* In Objective-C++, we may have an Objective-C 2.0
5940 dot-syntax for classes here. */
5941 if (c_dialect_objc ()
5942 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5943 && TREE_CODE (decl
) == TYPE_DECL
5944 && objc_is_class_name (decl
))
5947 cp_lexer_consume_token (parser
->lexer
);
5948 component
= cp_parser_identifier (parser
);
5949 if (component
== error_mark_node
)
5950 return error_mark_node
;
5952 tree result
= objc_build_class_component_ref (id_expression
,
5954 /* Build a location of the form:
5957 with caret at the start of the component name (at
5958 input_location), ranging from the start of the id_expression
5959 to the end of the component name. */
5960 location_t combined_loc
5961 = make_location (input_location
, id_expression
.get_start (),
5962 get_finish (input_location
));
5963 protected_set_expr_location (result
, combined_loc
);
5967 /* In Objective-C++, an instance variable (ivar) may be preferred
5968 to whatever cp_parser_lookup_name() found.
5969 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5970 rest of c-family, we have to do a little extra work to preserve
5971 any location information in cp_expr "decl". Given that
5972 objc_lookup_ivar is implemented in "c-family" and "objc", we
5973 have a trip through the pure "tree" type, rather than cp_expr.
5974 Naively copying it back to "decl" would implicitly give the
5975 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5976 store an EXPR_LOCATION. Hence we only update "decl" (and
5977 hence its location_t) if we get back a different tree node. */
5978 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5980 if (decl_tree
!= decl
.get_value ())
5981 decl
= cp_expr (decl_tree
);
5983 /* If name lookup gives us a SCOPE_REF, then the
5984 qualifying scope was dependent. */
5985 if (TREE_CODE (decl
) == SCOPE_REF
)
5987 /* At this point, we do not know if DECL is a valid
5988 integral constant expression. We assume that it is
5989 in fact such an expression, so that code like:
5991 template <int N> struct A {
5995 is accepted. At template-instantiation time, we
5996 will check that B<N>::i is actually a constant. */
5999 /* Check to see if DECL is a local variable in a context
6000 where that is forbidden. */
6001 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
6002 && local_variable_p (decl
)
6003 /* DR 2082 permits local variables in unevaluated contexts
6004 within a default argument. */
6005 && !cp_unevaluated_operand
)
6008 = (TREE_CODE (decl
) == PARM_DECL
6009 ? _("parameter %qD may not appear in this context")
6010 : _("local variable %qD may not appear in this context"));
6011 error_at (id_expression
.get_location (), msg
,
6013 return error_mark_node
;
6017 decl
= (finish_id_expression
6018 (id_expression
, decl
, parser
->scope
,
6020 parser
->integral_constant_expression_p
,
6021 parser
->allow_non_integral_constant_expression_p
,
6022 &parser
->non_integral_constant_expression_p
,
6023 template_p
, done
, address_p
,
6026 id_expression
.get_location ()));
6028 cp_parser_error (parser
, error_msg
);
6029 /* Build a location for an id-expression of the form:
6035 i.e. from the start of the first token to the end of the final
6036 token, with the caret at the start of the unqualified-id. */
6037 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
6038 location_t start_loc
= get_start (id_expr_token
->location
);
6039 location_t finish_loc
= get_finish (id_expression
.get_location ());
6040 location_t combined_loc
6041 = make_location (caret_loc
, start_loc
, finish_loc
);
6043 decl
.set_location (combined_loc
);
6047 /* Anything else is an error. */
6049 cp_parser_error (parser
, "expected primary-expression");
6050 return error_mark_node
;
6054 static inline cp_expr
6055 cp_parser_primary_expression (cp_parser
*parser
,
6058 bool template_arg_p
,
6061 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
6062 /*decltype*/false, idk
);
6065 /* Parse an id-expression.
6072 :: [opt] nested-name-specifier template [opt] unqualified-id
6074 :: operator-function-id
6077 Return a representation of the unqualified portion of the
6078 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6079 a `::' or nested-name-specifier.
6081 Often, if the id-expression was a qualified-id, the caller will
6082 want to make a SCOPE_REF to represent the qualified-id. This
6083 function does not do this in order to avoid wastefully creating
6084 SCOPE_REFs when they are not required.
6086 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6089 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6090 uninstantiated templates.
6092 If *TEMPLATE_P is non-NULL, it is set to true iff the
6093 `template' keyword is used to explicitly indicate that the entity
6094 named is a template.
6096 If DECLARATOR_P is true, the id-expression is appearing as part of
6097 a declarator, rather than as part of an expression. */
6100 cp_parser_id_expression (cp_parser
*parser
,
6101 bool template_keyword_p
,
6102 bool check_dependency_p
,
6107 bool global_scope_p
;
6108 bool nested_name_specifier_p
;
6110 /* Assume the `template' keyword was not used. */
6112 *template_p
= template_keyword_p
;
6114 /* Look for the optional `::' operator. */
6116 = (!template_keyword_p
6117 && (cp_parser_global_scope_opt (parser
,
6118 /*current_scope_valid_p=*/false)
6121 /* Look for the optional nested-name-specifier. */
6122 nested_name_specifier_p
6123 = (cp_parser_nested_name_specifier_opt (parser
,
6124 /*typename_keyword_p=*/false,
6131 /* If there is a nested-name-specifier, then we are looking at
6132 the first qualified-id production. */
6133 if (nested_name_specifier_p
)
6136 tree saved_object_scope
;
6137 tree saved_qualifying_scope
;
6138 cp_expr unqualified_id
;
6141 /* See if the next token is the `template' keyword. */
6143 template_p
= &is_template
;
6144 *template_p
= cp_parser_optional_template_keyword (parser
);
6145 /* Name lookup we do during the processing of the
6146 unqualified-id might obliterate SCOPE. */
6147 saved_scope
= parser
->scope
;
6148 saved_object_scope
= parser
->object_scope
;
6149 saved_qualifying_scope
= parser
->qualifying_scope
;
6150 /* Process the final unqualified-id. */
6151 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
6154 /*optional_p=*/false);
6155 /* Restore the SAVED_SCOPE for our caller. */
6156 parser
->scope
= saved_scope
;
6157 parser
->object_scope
= saved_object_scope
;
6158 parser
->qualifying_scope
= saved_qualifying_scope
;
6160 return unqualified_id
;
6162 /* Otherwise, if we are in global scope, then we are looking at one
6163 of the other qualified-id productions. */
6164 else if (global_scope_p
)
6169 /* Peek at the next token. */
6170 token
= cp_lexer_peek_token (parser
->lexer
);
6172 /* If it's an identifier, and the next token is not a "<", then
6173 we can avoid the template-id case. This is an optimization
6174 for this common case. */
6175 if (token
->type
== CPP_NAME
6176 && !cp_parser_nth_token_starts_template_argument_list_p
6178 return cp_parser_identifier (parser
);
6180 cp_parser_parse_tentatively (parser
);
6181 /* Try a template-id. */
6182 id
= cp_parser_template_id_expr (parser
,
6183 /*template_keyword_p=*/false,
6184 /*check_dependency_p=*/true,
6186 /* If that worked, we're done. */
6187 if (cp_parser_parse_definitely (parser
))
6190 /* Peek at the next token. (Changes in the token buffer may
6191 have invalidated the pointer obtained above.) */
6192 token
= cp_lexer_peek_token (parser
->lexer
);
6194 switch (token
->type
)
6197 return cp_parser_identifier (parser
);
6200 if (token
->keyword
== RID_OPERATOR
)
6201 return cp_parser_operator_function_id (parser
);
6205 cp_parser_error (parser
, "expected id-expression");
6206 return error_mark_node
;
6210 return cp_parser_unqualified_id (parser
, template_keyword_p
,
6211 /*check_dependency_p=*/true,
6216 /* Parse an unqualified-id.
6220 operator-function-id
6221 conversion-function-id
6225 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6226 keyword, in a construct like `A::template ...'.
6228 Returns a representation of unqualified-id. For the `identifier'
6229 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6230 production a BIT_NOT_EXPR is returned; the operand of the
6231 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6232 other productions, see the documentation accompanying the
6233 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6234 names are looked up in uninstantiated templates. If DECLARATOR_P
6235 is true, the unqualified-id is appearing as part of a declarator,
6236 rather than as part of an expression. */
6239 cp_parser_unqualified_id (cp_parser
* parser
,
6240 bool template_keyword_p
,
6241 bool check_dependency_p
,
6247 /* Peek at the next token. */
6248 token
= cp_lexer_peek_token (parser
->lexer
);
6250 switch ((int) token
->type
)
6256 /* We don't know yet whether or not this will be a
6258 cp_parser_parse_tentatively (parser
);
6259 /* Try a template-id. */
6260 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6263 /* If it worked, we're done. */
6264 if (cp_parser_parse_definitely (parser
))
6266 /* Otherwise, it's an ordinary identifier. */
6267 return cp_parser_identifier (parser
);
6270 case CPP_TEMPLATE_ID
:
6271 return cp_parser_template_id_expr (parser
, template_keyword_p
,
6278 tree qualifying_scope
;
6282 location_t tilde_loc
= token
->location
;
6284 /* Consume the `~' token. */
6285 cp_lexer_consume_token (parser
->lexer
);
6286 /* Parse the class-name. The standard, as written, seems to
6289 template <typename T> struct S { ~S (); };
6290 template <typename T> S<T>::~S() {}
6292 is invalid, since `~' must be followed by a class-name, but
6293 `S<T>' is dependent, and so not known to be a class.
6294 That's not right; we need to look in uninstantiated
6295 templates. A further complication arises from:
6297 template <typename T> void f(T t) {
6301 Here, it is not possible to look up `T' in the scope of `T'
6302 itself. We must look in both the current scope, and the
6303 scope of the containing complete expression.
6305 Yet another issue is:
6314 The standard does not seem to say that the `S' in `~S'
6315 should refer to the type `S' and not the data member
6318 /* DR 244 says that we look up the name after the "~" in the
6319 same scope as we looked up the qualifying name. That idea
6320 isn't fully worked out; it's more complicated than that. */
6321 scope
= parser
->scope
;
6322 object_scope
= parser
->object_scope
;
6323 qualifying_scope
= parser
->qualifying_scope
;
6325 /* Check for invalid scopes. */
6326 if (scope
== error_mark_node
)
6328 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6329 cp_lexer_consume_token (parser
->lexer
);
6330 return error_mark_node
;
6332 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6334 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6335 error_at (token
->location
,
6336 "scope %qT before %<~%> is not a class-name",
6338 cp_parser_simulate_error (parser
);
6339 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6340 cp_lexer_consume_token (parser
->lexer
);
6341 return error_mark_node
;
6343 if (template_keyword_p
)
6345 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6346 error_at (tilde_loc
, "%<template%> keyword not permitted in "
6348 cp_parser_simulate_error (parser
);
6349 return error_mark_node
;
6352 gcc_assert (!scope
|| TYPE_P (scope
));
6354 token
= cp_lexer_peek_token (parser
->lexer
);
6356 /* Create a location with caret == start at the tilde,
6357 finishing at the end of the peeked token, e.g:
6361 = make_location (tilde_loc
, tilde_loc
, token
->location
);
6363 /* If the name is of the form "X::~X" it's OK even if X is a
6367 && token
->type
== CPP_NAME
6368 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6370 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6371 || (CLASS_TYPE_P (scope
)
6372 && constructor_name_p (token
->u
.value
, scope
))))
6374 cp_lexer_consume_token (parser
->lexer
);
6375 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6378 /* ~auto means the destructor of whatever the object is. */
6379 if (cp_parser_is_keyword (token
, RID_AUTO
))
6381 if (cxx_dialect
< cxx14
)
6382 pedwarn (loc
, OPT_Wc__14_extensions
,
6383 "%<~auto%> only available with "
6384 "%<-std=c++14%> or %<-std=gnu++14%>");
6385 cp_lexer_consume_token (parser
->lexer
);
6386 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, make_auto ());
6389 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6390 declarator-id of a constructor or destructor. */
6391 if (token
->type
== CPP_TEMPLATE_ID
&& declarator_p
6392 && cxx_dialect
>= cxx20
)
6394 if (!cp_parser_simulate_error (parser
))
6395 error_at (tilde_loc
, "template-id not allowed for destructor");
6396 return error_mark_node
;
6399 /* If there was an explicit qualification (S::~T), first look
6400 in the scope given by the qualification (i.e., S).
6402 Note: in the calls to cp_parser_class_name below we pass
6403 typename_type so that lookup finds the injected-class-name
6404 rather than the constructor. */
6406 type_decl
= NULL_TREE
;
6409 cp_parser_parse_tentatively (parser
);
6410 type_decl
= cp_parser_class_name (parser
,
6411 /*typename_keyword_p=*/false,
6412 /*template_keyword_p=*/false,
6414 /*check_dependency=*/false,
6415 /*class_head_p=*/false,
6417 if (cp_parser_parse_definitely (parser
))
6420 /* In "N::S::~S", look in "N" as well. */
6421 if (!done
&& scope
&& qualifying_scope
)
6423 cp_parser_parse_tentatively (parser
);
6424 parser
->scope
= qualifying_scope
;
6425 parser
->object_scope
= NULL_TREE
;
6426 parser
->qualifying_scope
= NULL_TREE
;
6428 = cp_parser_class_name (parser
,
6429 /*typename_keyword_p=*/false,
6430 /*template_keyword_p=*/false,
6432 /*check_dependency=*/false,
6433 /*class_head_p=*/false,
6435 if (cp_parser_parse_definitely (parser
))
6438 /* In "p->S::~T", look in the scope given by "*p" as well. */
6439 else if (!done
&& object_scope
)
6441 cp_parser_parse_tentatively (parser
);
6442 parser
->scope
= object_scope
;
6443 parser
->object_scope
= NULL_TREE
;
6444 parser
->qualifying_scope
= NULL_TREE
;
6446 = cp_parser_class_name (parser
,
6447 /*typename_keyword_p=*/false,
6448 /*template_keyword_p=*/false,
6450 /*check_dependency=*/false,
6451 /*class_head_p=*/false,
6453 if (cp_parser_parse_definitely (parser
))
6456 /* Look in the surrounding context. */
6459 parser
->scope
= NULL_TREE
;
6460 parser
->object_scope
= NULL_TREE
;
6461 parser
->qualifying_scope
= NULL_TREE
;
6462 if (processing_template_decl
)
6463 cp_parser_parse_tentatively (parser
);
6465 = cp_parser_class_name (parser
,
6466 /*typename_keyword_p=*/false,
6467 /*template_keyword_p=*/false,
6469 /*check_dependency=*/false,
6470 /*class_head_p=*/false,
6472 if (processing_template_decl
6473 && ! cp_parser_parse_definitely (parser
))
6475 /* We couldn't find a type with this name. If we're parsing
6476 tentatively, fail and try something else. */
6477 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6479 cp_parser_simulate_error (parser
);
6480 return error_mark_node
;
6482 /* Otherwise, accept it and check for a match at instantiation
6484 type_decl
= cp_parser_identifier (parser
);
6485 if (type_decl
!= error_mark_node
)
6486 type_decl
= build_min_nt_loc (loc
, BIT_NOT_EXPR
, type_decl
);
6490 /* If an error occurred, assume that the name of the
6491 destructor is the same as the name of the qualifying
6492 class. That allows us to keep parsing after running
6493 into ill-formed destructor names. */
6494 if (type_decl
== error_mark_node
&& scope
)
6495 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6496 else if (type_decl
== error_mark_node
)
6497 return error_mark_node
;
6499 /* Check that destructor name and scope match. */
6500 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6502 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6504 "declaration of %<~%T%> as member of %qT",
6506 cp_parser_simulate_error (parser
);
6507 return error_mark_node
;
6512 A typedef-name that names a class shall not be used as the
6513 identifier in the declarator for a destructor declaration. */
6515 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6516 && !DECL_SELF_REFERENCE_P (type_decl
)
6517 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6519 "typedef-name %qD used as destructor declarator",
6522 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6526 if (token
->keyword
== RID_OPERATOR
)
6530 /* This could be a template-id, so we try that first. */
6531 cp_parser_parse_tentatively (parser
);
6532 /* Try a template-id. */
6533 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6534 /*check_dependency_p=*/true,
6536 /* If that worked, we're done. */
6537 if (cp_parser_parse_definitely (parser
))
6539 /* We still don't know whether we're looking at an
6540 operator-function-id or a conversion-function-id. */
6541 cp_parser_parse_tentatively (parser
);
6542 /* Try an operator-function-id. */
6543 id
= cp_parser_operator_function_id (parser
);
6544 /* If that didn't work, try a conversion-function-id. */
6545 if (!cp_parser_parse_definitely (parser
))
6546 id
= cp_parser_conversion_function_id (parser
);
6555 cp_parser_error (parser
, "expected unqualified-id");
6556 return error_mark_node
;
6560 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6561 be a template-id or the name shall refer to a class template or an
6565 check_template_keyword_in_nested_name_spec (tree name
)
6567 if (CLASS_TYPE_P (name
)
6568 && ((CLASSTYPE_USE_TEMPLATE (name
)
6569 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name
)))
6570 || CLASSTYPE_IS_TEMPLATE (name
)))
6573 if (TREE_CODE (name
) == TYPENAME_TYPE
6574 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name
)) == TEMPLATE_ID_EXPR
)
6576 /* Alias templates are also OK. */
6577 else if (alias_template_specialization_p (name
, nt_opaque
))
6580 permerror (input_location
, TYPE_P (name
)
6581 ? G_("%qT is not a template")
6582 : G_("%qD is not a template"),
6586 /* Parse an (optional) nested-name-specifier.
6588 nested-name-specifier: [C++98]
6589 class-or-namespace-name :: nested-name-specifier [opt]
6590 class-or-namespace-name :: template nested-name-specifier [opt]
6592 nested-name-specifier: [C++0x]
6595 nested-name-specifier identifier ::
6596 nested-name-specifier template [opt] simple-template-id ::
6598 PARSER->SCOPE should be set appropriately before this function is
6599 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6600 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6603 Sets PARSER->SCOPE to the class (TYPE) or namespace
6604 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6605 it unchanged if there is no nested-name-specifier. Returns the new
6606 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6608 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6610 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6611 part of a declaration and/or decl-specifier. */
6614 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6615 bool typename_keyword_p
,
6616 bool check_dependency_p
,
6618 bool is_declaration
,
6619 bool template_keyword_p
/* = false */)
6621 bool success
= false;
6622 cp_token_position start
= 0;
6625 /* Remember where the nested-name-specifier starts. */
6626 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
6627 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
6629 start
= cp_lexer_token_position (parser
->lexer
, false);
6630 push_deferring_access_checks (dk_deferred
);
6637 tree saved_qualifying_scope
;
6639 /* Spot cases that cannot be the beginning of a
6640 nested-name-specifier. */
6641 token
= cp_lexer_peek_token (parser
->lexer
);
6643 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6644 the already parsed nested-name-specifier. */
6645 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6647 /* Grab the nested-name-specifier and continue the loop. */
6648 cp_parser_pre_parsed_nested_name_specifier (parser
);
6649 /* If we originally encountered this nested-name-specifier
6650 with CHECK_DEPENDENCY_P set to true, we will not have
6651 resolved TYPENAME_TYPEs, so we must do so here. */
6653 && !check_dependency_p
6654 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6656 new_scope
= resolve_typename_type (parser
->scope
,
6657 /*only_current_p=*/false);
6658 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6659 parser
->scope
= new_scope
;
6665 /* Spot cases that cannot be the beginning of a
6666 nested-name-specifier. On the second and subsequent times
6667 through the loop, we look for the `template' keyword. */
6668 if (success
&& token
->keyword
== RID_TEMPLATE
)
6670 /* A template-id can start a nested-name-specifier. */
6671 else if (token
->type
== CPP_TEMPLATE_ID
)
6673 /* DR 743: decltype can be used in a nested-name-specifier. */
6674 else if (token_is_decltype (token
))
6678 /* If the next token is not an identifier, then it is
6679 definitely not a type-name or namespace-name. */
6680 if (token
->type
!= CPP_NAME
)
6682 /* If the following token is neither a `<' (to begin a
6683 template-id), nor a `::', then we are not looking at a
6684 nested-name-specifier. */
6685 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6687 if (token
->type
== CPP_COLON
6688 && parser
->colon_corrects_to_scope_p
6689 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
6690 /* name:name is a valid sequence in an Objective C message. */
6691 && !parser
->objective_c_message_context_p
)
6693 gcc_rich_location
richloc (token
->location
);
6694 richloc
.add_fixit_replace ("::");
6696 "found %<:%> in nested-name-specifier, "
6698 token
->type
= CPP_SCOPE
;
6701 if (token
->type
!= CPP_SCOPE
6702 && !cp_parser_nth_token_starts_template_argument_list_p
6707 /* The nested-name-specifier is optional, so we parse
6709 cp_parser_parse_tentatively (parser
);
6711 /* Look for the optional `template' keyword, if this isn't the
6712 first time through the loop. */
6715 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6716 /* DR1710: "In a qualified-id used as the name in
6717 a typename-specifier, elaborated-type-specifier, using-declaration,
6718 or class-or-decltype, an optional keyword template appearing at
6719 the top level is ignored." */
6720 if (!template_keyword_p
6721 && typename_keyword_p
6722 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
6723 template_keyword_p
= true;
6726 /* Save the old scope since the name lookup we are about to do
6727 might destroy it. */
6728 old_scope
= parser
->scope
;
6729 saved_qualifying_scope
= parser
->qualifying_scope
;
6730 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6731 look up names in "X<T>::I" in order to determine that "Y" is
6732 a template. So, if we have a typename at this point, we make
6733 an effort to look through it. */
6735 && !check_dependency_p
6736 && !typename_keyword_p
6738 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6739 parser
->scope
= resolve_typename_type (parser
->scope
,
6740 /*only_current_p=*/false);
6741 /* Parse the qualifying entity. */
6743 = cp_parser_qualifying_entity (parser
,
6749 /* Look for the `::' token. */
6750 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6752 /* If we found what we wanted, we keep going; otherwise, we're
6754 if (!cp_parser_parse_definitely (parser
))
6756 bool error_p
= false;
6758 /* Restore the OLD_SCOPE since it was valid before the
6759 failed attempt at finding the last
6760 class-or-namespace-name. */
6761 parser
->scope
= old_scope
;
6762 parser
->qualifying_scope
= saved_qualifying_scope
;
6764 /* If the next token is a decltype, and the one after that is a
6765 `::', then the decltype has failed to resolve to a class or
6766 enumeration type. Give this error even when parsing
6767 tentatively since it can't possibly be valid--and we're going
6768 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6769 won't get another chance.*/
6770 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6771 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6774 token
= cp_lexer_consume_token (parser
->lexer
);
6775 tree dtype
= token
->u
.tree_check_value
->value
;
6776 if (dtype
!= error_mark_node
)
6777 error_at (token
->location
, "%<decltype%> evaluates to %qT, "
6778 "which is not a class or enumeration type",
6780 parser
->scope
= error_mark_node
;
6784 cp_lexer_consume_token (parser
->lexer
);
6787 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6788 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6790 /* If we have a non-type template-id followed by ::, it can't
6791 possibly be valid. */
6792 token
= cp_lexer_peek_token (parser
->lexer
);
6793 tree tid
= token
->u
.tree_check_value
->value
;
6794 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6795 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6797 tree tmpl
= NULL_TREE
;
6798 if (is_overloaded_fn (tid
))
6800 tree fns
= get_fns (tid
);
6801 if (OVL_SINGLE_P (fns
))
6802 tmpl
= OVL_FIRST (fns
);
6803 if (function_concept_p (fns
))
6804 error_at (token
->location
, "concept-id %qD "
6805 "in nested-name-specifier", tid
);
6807 error_at (token
->location
, "function template-id "
6808 "%qD in nested-name-specifier", tid
);
6812 tmpl
= TREE_OPERAND (tid
, 0);
6813 if (variable_concept_p (tmpl
)
6814 || standard_concept_p (tmpl
))
6815 error_at (token
->location
, "concept-id %qD "
6816 "in nested-name-specifier", tid
);
6819 /* Variable template. */
6820 gcc_assert (variable_template_p (tmpl
));
6821 error_at (token
->location
, "variable template-id "
6822 "%qD in nested-name-specifier", tid
);
6826 inform (DECL_SOURCE_LOCATION (tmpl
),
6827 "%qD declared here", tmpl
);
6829 parser
->scope
= error_mark_node
;
6833 cp_lexer_consume_token (parser
->lexer
);
6834 cp_lexer_consume_token (parser
->lexer
);
6838 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6840 /* If the next token is an identifier, and the one after
6841 that is a `::', then any valid interpretation would have
6842 found a class-or-namespace-name. */
6843 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6844 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6846 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6849 token
= cp_lexer_consume_token (parser
->lexer
);
6852 if (!token
->error_reported
)
6855 tree ambiguous_decls
;
6857 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6859 /*is_template=*/false,
6860 /*is_namespace=*/false,
6861 /*check_dependency=*/true,
6864 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6865 error_at (token
->location
,
6866 "%qD used without template arguments",
6868 else if (ambiguous_decls
)
6870 // cp_parser_lookup_name has the same diagnostic,
6871 // thus make sure to emit it at most once.
6872 if (cp_parser_uncommitted_to_tentative_parse_p
6875 error_at (token
->location
,
6876 "reference to %qD is ambiguous",
6878 print_candidates (ambiguous_decls
);
6880 decl
= error_mark_node
;
6884 if (cxx_dialect
!= cxx98
)
6885 cp_parser_name_lookup_error
6886 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6889 cp_parser_name_lookup_error
6890 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6894 parser
->scope
= error_mark_node
;
6896 /* Treat this as a successful nested-name-specifier
6901 If the name found is not a class-name (clause
6902 _class_) or namespace-name (_namespace.def_), the
6903 program is ill-formed. */
6906 cp_lexer_consume_token (parser
->lexer
);
6910 /* We've found one valid nested-name-specifier. */
6912 /* Name lookup always gives us a DECL. */
6913 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6914 new_scope
= TREE_TYPE (new_scope
);
6915 /* Uses of "template" must be followed by actual templates. */
6916 if (template_keyword_p
)
6917 check_template_keyword_in_nested_name_spec (new_scope
);
6918 /* If it is a class scope, try to complete it; we are about to
6919 be looking up names inside the class. */
6920 if (TYPE_P (new_scope
)
6921 /* Since checking types for dependency can be expensive,
6922 avoid doing it if the type is already complete. */
6923 && !COMPLETE_TYPE_P (new_scope
)
6924 /* Do not try to complete dependent types. */
6925 && !dependent_type_p (new_scope
))
6927 new_scope
= complete_type (new_scope
);
6928 /* If it is a typedef to current class, use the current
6929 class instead, as the typedef won't have any names inside
6931 if (!COMPLETE_TYPE_P (new_scope
)
6932 && currently_open_class (new_scope
))
6933 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6935 /* Make sure we look in the right scope the next time through
6937 parser
->scope
= new_scope
;
6940 /* If parsing tentatively, replace the sequence of tokens that makes
6941 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6942 token. That way, should we re-parse the token stream, we will
6943 not have to repeat the effort required to do the parse, nor will
6944 we issue duplicate error messages. */
6945 if (success
&& start
)
6949 token
= cp_lexer_token_at (parser
->lexer
, start
);
6950 /* Reset the contents of the START token. */
6951 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6952 /* Retrieve any deferred checks. Do not pop this access checks yet
6953 so the memory will not be reclaimed during token replacing below. */
6954 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6955 token
->tree_check_p
= true;
6956 token
->u
.tree_check_value
->value
= parser
->scope
;
6957 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6958 token
->u
.tree_check_value
->qualifying_scope
=
6959 parser
->qualifying_scope
;
6960 token
->keyword
= RID_MAX
;
6962 /* Purge all subsequent tokens. */
6963 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6967 pop_to_parent_deferring_access_checks ();
6969 return success
? parser
->scope
: NULL_TREE
;
6972 /* Parse a nested-name-specifier. See
6973 cp_parser_nested_name_specifier_opt for details. This function
6974 behaves identically, except that it will an issue an error if no
6975 nested-name-specifier is present. */
6978 cp_parser_nested_name_specifier (cp_parser
*parser
,
6979 bool typename_keyword_p
,
6980 bool check_dependency_p
,
6982 bool is_declaration
)
6986 /* Look for the nested-name-specifier. */
6987 scope
= cp_parser_nested_name_specifier_opt (parser
,
6992 /* If it was not present, issue an error message. */
6995 cp_parser_error (parser
, "expected nested-name-specifier");
6996 parser
->scope
= NULL_TREE
;
7002 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7003 this is either a class-name or a namespace-name (which corresponds
7004 to the class-or-namespace-name production in the grammar). For
7005 C++0x, it can also be a type-name that refers to an enumeration
7006 type or a simple-template-id.
7008 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7009 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7010 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7011 TYPE_P is TRUE iff the next name should be taken as a class-name,
7012 even the same name is declared to be another entity in the same
7015 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7016 specified by the class-or-namespace-name. If neither is found the
7017 ERROR_MARK_NODE is returned. */
7020 cp_parser_qualifying_entity (cp_parser
*parser
,
7021 bool typename_keyword_p
,
7022 bool template_keyword_p
,
7023 bool check_dependency_p
,
7025 bool is_declaration
)
7028 tree saved_qualifying_scope
;
7029 tree saved_object_scope
;
7032 bool successful_parse_p
;
7034 /* DR 743: decltype can appear in a nested-name-specifier. */
7035 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
7037 scope
= cp_parser_decltype (parser
);
7038 if (TREE_CODE (scope
) != ENUMERAL_TYPE
7039 && !MAYBE_CLASS_TYPE_P (scope
))
7041 cp_parser_simulate_error (parser
);
7042 return error_mark_node
;
7044 if (TYPE_NAME (scope
))
7045 scope
= TYPE_NAME (scope
);
7049 /* Before we try to parse the class-name, we must save away the
7050 current PARSER->SCOPE since cp_parser_class_name will destroy
7052 saved_scope
= parser
->scope
;
7053 saved_qualifying_scope
= parser
->qualifying_scope
;
7054 saved_object_scope
= parser
->object_scope
;
7055 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7056 there is no need to look for a namespace-name. */
7057 only_class_p
= template_keyword_p
7058 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
7060 cp_parser_parse_tentatively (parser
);
7061 scope
= cp_parser_class_name (parser
,
7064 type_p
? class_type
: none_type
,
7066 /*class_head_p=*/false,
7068 /*enum_ok=*/cxx_dialect
> cxx98
);
7069 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
7070 /* If that didn't work, try for a namespace-name. */
7071 if (!only_class_p
&& !successful_parse_p
)
7073 /* Restore the saved scope. */
7074 parser
->scope
= saved_scope
;
7075 parser
->qualifying_scope
= saved_qualifying_scope
;
7076 parser
->object_scope
= saved_object_scope
;
7077 /* If we are not looking at an identifier followed by the scope
7078 resolution operator, then this is not part of a
7079 nested-name-specifier. (Note that this function is only used
7080 to parse the components of a nested-name-specifier.) */
7081 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
7082 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
7083 return error_mark_node
;
7084 scope
= cp_parser_namespace_name (parser
);
7090 /* Return true if we are looking at a compound-literal, false otherwise. */
7093 cp_parser_compound_literal_p (cp_parser
*parser
)
7095 cp_lexer_save_tokens (parser
->lexer
);
7097 /* Skip tokens until the next token is a closing parenthesis.
7098 If we find the closing `)', and the next token is a `{', then
7099 we are looking at a compound-literal. */
7100 bool compound_literal_p
7101 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7102 /*consume_paren=*/true)
7103 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
7105 /* Roll back the tokens we skipped. */
7106 cp_lexer_rollback_tokens (parser
->lexer
);
7108 return compound_literal_p
;
7111 /* Return true if EXPR is the integer constant zero or a complex constant
7112 of zero, without any folding, but ignoring location wrappers. */
7115 literal_integer_zerop (const_tree expr
)
7117 return (location_wrapper_p (expr
)
7118 && integer_zerop (TREE_OPERAND (expr
, 0)));
7121 /* Parse a postfix-expression.
7125 postfix-expression [ expression ]
7126 postfix-expression ( expression-list [opt] )
7127 simple-type-specifier ( expression-list [opt] )
7128 typename :: [opt] nested-name-specifier identifier
7129 ( expression-list [opt] )
7130 typename :: [opt] nested-name-specifier template [opt] template-id
7131 ( expression-list [opt] )
7132 postfix-expression . template [opt] id-expression
7133 postfix-expression -> template [opt] id-expression
7134 postfix-expression . pseudo-destructor-name
7135 postfix-expression -> pseudo-destructor-name
7136 postfix-expression ++
7137 postfix-expression --
7138 dynamic_cast < type-id > ( expression )
7139 static_cast < type-id > ( expression )
7140 reinterpret_cast < type-id > ( expression )
7141 const_cast < type-id > ( expression )
7142 typeid ( expression )
7148 ( type-id ) { initializer-list , [opt] }
7150 This extension is a GNU version of the C99 compound-literal
7151 construct. (The C99 grammar uses `type-name' instead of `type-id',
7152 but they are essentially the same concept.)
7154 If ADDRESS_P is true, the postfix expression is the operand of the
7155 `&' operator. CAST_P is true if this expression is the target of a
7158 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7159 class member access expressions [expr.ref].
7161 Returns a representation of the expression. */
7164 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7165 bool member_access_only_p
, bool decltype_p
,
7166 cp_id_kind
* pidk_return
)
7171 cp_id_kind idk
= CP_ID_KIND_NONE
;
7172 cp_expr postfix_expression
= NULL_TREE
;
7173 bool is_member_access
= false;
7175 /* Peek at the next token. */
7176 token
= cp_lexer_peek_token (parser
->lexer
);
7177 loc
= token
->location
;
7178 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
7180 /* Some of the productions are determined by keywords. */
7181 keyword
= token
->keyword
;
7191 const char *saved_message
;
7192 bool saved_in_type_id_in_expr_p
;
7194 /* All of these can be handled in the same way from the point
7195 of view of parsing. Begin by consuming the token
7196 identifying the cast. */
7197 cp_lexer_consume_token (parser
->lexer
);
7199 /* New types cannot be defined in the cast. */
7200 saved_message
= parser
->type_definition_forbidden_message
;
7201 parser
->type_definition_forbidden_message
7202 = G_("types may not be defined in casts");
7204 /* Look for the opening `<'. */
7205 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
7206 /* Parse the type to which we are casting. */
7207 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7208 parser
->in_type_id_in_expr_p
= true;
7209 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
7211 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7212 /* Look for the closing `>'. */
7213 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
7214 /* Restore the old message. */
7215 parser
->type_definition_forbidden_message
= saved_message
;
7217 bool saved_greater_than_is_operator_p
7218 = parser
->greater_than_is_operator_p
;
7219 parser
->greater_than_is_operator_p
= true;
7221 /* And the expression which is being cast. */
7222 matching_parens parens
;
7223 parens
.require_open (parser
);
7224 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
7225 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
7227 location_t end_loc
= close_paren
?
7228 close_paren
->location
: UNKNOWN_LOCATION
;
7230 parser
->greater_than_is_operator_p
7231 = saved_greater_than_is_operator_p
;
7233 /* Only type conversions to integral or enumeration types
7234 can be used in constant-expressions. */
7235 if (!cast_valid_in_integral_constant_expression_p (type
)
7236 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
7238 postfix_expression
= error_mark_node
;
7242 /* Construct a location e.g. :
7243 reinterpret_cast <int *> (expr)
7244 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7245 ranging from the start of the "*_cast" token to the final closing
7246 paren, with the caret at the start. */
7247 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
7253 = build_dynamic_cast (cp_cast_loc
, type
, expression
,
7254 tf_warning_or_error
);
7258 = build_static_cast (cp_cast_loc
, type
, expression
,
7259 tf_warning_or_error
);
7263 = build_reinterpret_cast (cp_cast_loc
, type
, expression
,
7264 tf_warning_or_error
);
7268 = build_const_cast (cp_cast_loc
, type
, expression
,
7269 tf_warning_or_error
);
7280 const char *saved_message
;
7281 bool saved_in_type_id_in_expr_p
;
7283 /* Consume the `typeid' token. */
7284 cp_lexer_consume_token (parser
->lexer
);
7285 /* Look for the `(' token. */
7286 matching_parens parens
;
7287 parens
.require_open (parser
);
7288 /* Types cannot be defined in a `typeid' expression. */
7289 saved_message
= parser
->type_definition_forbidden_message
;
7290 parser
->type_definition_forbidden_message
7291 = G_("types may not be defined in a %<typeid%> expression");
7292 /* We can't be sure yet whether we're looking at a type-id or an
7294 cp_parser_parse_tentatively (parser
);
7295 /* Try a type-id first. */
7296 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7297 parser
->in_type_id_in_expr_p
= true;
7298 type
= cp_parser_type_id (parser
);
7299 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7300 /* Look for the `)' token. Otherwise, we can't be sure that
7301 we're not looking at an expression: consider `typeid (int
7302 (3))', for example. */
7303 cp_token
*close_paren
= parens
.require_close (parser
);
7304 /* If all went well, simply lookup the type-id. */
7305 if (cp_parser_parse_definitely (parser
))
7306 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
7307 /* Otherwise, fall back to the expression variant. */
7312 /* Look for an expression. */
7313 expression
= cp_parser_expression (parser
, & idk
);
7314 /* Compute its typeid. */
7315 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
7316 /* Look for the `)' token. */
7317 close_paren
= parens
.require_close (parser
);
7319 /* Restore the saved message. */
7320 parser
->type_definition_forbidden_message
= saved_message
;
7321 /* `typeid' may not appear in an integral constant expression. */
7322 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
7323 postfix_expression
= error_mark_node
;
7325 /* Construct a location e.g. :
7328 ranging from the start of the "typeid" token to the final closing
7329 paren, with the caret at the start. */
7332 location_t typeid_loc
7333 = make_location (start_loc
, start_loc
, close_paren
->location
);
7334 postfix_expression
.set_location (typeid_loc
);
7335 postfix_expression
.maybe_add_location_wrapper ();
7343 /* The syntax permitted here is the same permitted for an
7344 elaborated-type-specifier. */
7345 ++parser
->prevent_constrained_type_specifiers
;
7346 type
= cp_parser_elaborated_type_specifier (parser
,
7347 /*is_friend=*/false,
7348 /*is_declaration=*/false);
7349 --parser
->prevent_constrained_type_specifiers
;
7350 postfix_expression
= cp_parser_functional_cast (parser
, type
);
7355 case RID_BUILTIN_SHUFFLE
:
7356 case RID_BUILTIN_SHUFFLEVECTOR
:
7357 case RID_BUILTIN_LAUNDER
:
7358 case RID_BUILTIN_ASSOC_BARRIER
:
7360 vec
<tree
, va_gc
> *vec
;
7362 cp_lexer_consume_token (parser
->lexer
);
7363 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
7364 /*cast_p=*/false, /*allow_expansion_p=*/true,
7365 /*non_constant_p=*/NULL
);
7368 postfix_expression
= error_mark_node
;
7378 if (vec
->length () == 1)
7380 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
7383 error_at (loc
, "wrong number of arguments to "
7384 "%<__builtin_addressof%>");
7385 postfix_expression
= error_mark_node
;
7389 case RID_BUILTIN_LAUNDER
:
7390 if (vec
->length () == 1)
7391 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7392 tf_warning_or_error
);
7395 error_at (loc
, "wrong number of arguments to "
7396 "%<__builtin_launder%>");
7397 postfix_expression
= error_mark_node
;
7401 case RID_BUILTIN_ASSOC_BARRIER
:
7402 if (vec
->length () == 1)
7403 postfix_expression
= build1_loc (loc
, PAREN_EXPR
,
7404 TREE_TYPE ((*vec
)[0]),
7408 error_at (loc
, "wrong number of arguments to "
7409 "%<__builtin_assoc_barrier%>");
7410 postfix_expression
= error_mark_node
;
7414 case RID_BUILTIN_SHUFFLE
:
7415 if (vec
->length () == 2)
7417 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7418 (*vec
)[1], tf_warning_or_error
);
7419 else if (vec
->length () == 3)
7421 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7422 (*vec
)[2], tf_warning_or_error
);
7425 error_at (loc
, "wrong number of arguments to "
7426 "%<__builtin_shuffle%>");
7427 postfix_expression
= error_mark_node
;
7431 case RID_BUILTIN_SHUFFLEVECTOR
:
7432 if (vec
->length () < 3)
7434 error_at (loc
, "wrong number of arguments to "
7435 "%<__builtin_shufflevector%>");
7436 postfix_expression
= error_mark_node
;
7441 = build_x_shufflevector (loc
, vec
, tf_warning_or_error
);
7451 case RID_BUILTIN_CONVERTVECTOR
:
7455 /* Consume the `__builtin_convertvector' token. */
7456 cp_lexer_consume_token (parser
->lexer
);
7457 /* Look for the opening `('. */
7458 matching_parens parens
;
7459 parens
.require_open (parser
);
7460 /* Now, parse the assignment-expression. */
7461 expression
= cp_parser_assignment_expression (parser
);
7462 /* Look for the `,'. */
7463 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7464 location_t type_location
7465 = cp_lexer_peek_token (parser
->lexer
)->location
;
7466 /* Parse the type-id. */
7468 type_id_in_expr_sentinel
s (parser
);
7469 type
= cp_parser_type_id (parser
);
7471 /* Look for the closing `)'. */
7472 parens
.require_close (parser
);
7473 return cp_build_vec_convert (expression
, type_location
, type
,
7474 tf_warning_or_error
);
7477 case RID_BUILTIN_BIT_CAST
:
7481 /* Consume the `__builtin_bit_cast' token. */
7482 cp_lexer_consume_token (parser
->lexer
);
7483 /* Look for the opening `('. */
7484 matching_parens parens
;
7485 parens
.require_open (parser
);
7486 location_t type_location
7487 = cp_lexer_peek_token (parser
->lexer
)->location
;
7488 /* Parse the type-id. */
7490 type_id_in_expr_sentinel
s (parser
);
7491 type
= cp_parser_type_id (parser
);
7493 /* Look for the `,'. */
7494 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7495 /* Now, parse the assignment-expression. */
7496 expression
= cp_parser_assignment_expression (parser
);
7497 /* Look for the closing `)'. */
7498 parens
.require_close (parser
);
7499 return cp_build_bit_cast (type_location
, type
, expression
,
7500 tf_warning_or_error
);
7507 /* If the next thing is a simple-type-specifier, we may be
7508 looking at a functional cast. We could also be looking at
7509 an id-expression. So, we try the functional cast, and if
7510 that doesn't work we fall back to the primary-expression. */
7511 cp_parser_parse_tentatively (parser
);
7512 /* Look for the simple-type-specifier. */
7513 ++parser
->prevent_constrained_type_specifiers
;
7514 type
= cp_parser_simple_type_specifier (parser
,
7515 /*decl_specs=*/NULL
,
7516 CP_PARSER_FLAGS_NONE
);
7517 --parser
->prevent_constrained_type_specifiers
;
7518 /* Parse the cast itself. */
7519 if (!cp_parser_error_occurred (parser
))
7521 = cp_parser_functional_cast (parser
, type
);
7522 /* If that worked, we're done. */
7523 if (cp_parser_parse_definitely (parser
))
7526 /* If the functional-cast didn't work out, try a
7527 compound-literal. */
7528 if (cp_parser_allow_gnu_extensions_p (parser
)
7529 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7531 cp_expr initializer
= NULL_TREE
;
7533 cp_parser_parse_tentatively (parser
);
7535 matching_parens parens
;
7536 parens
.consume_open (parser
);
7538 /* Avoid calling cp_parser_type_id pointlessly, see comment
7539 in cp_parser_cast_expression about c++/29234. */
7540 if (!cp_parser_compound_literal_p (parser
))
7541 cp_parser_simulate_error (parser
);
7544 /* Parse the type. */
7545 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7546 parser
->in_type_id_in_expr_p
= true;
7547 type
= cp_parser_type_id (parser
);
7548 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7549 parens
.require_close (parser
);
7552 /* If things aren't going well, there's no need to
7554 if (!cp_parser_error_occurred (parser
))
7556 bool non_constant_p
;
7557 /* Parse the brace-enclosed initializer list. */
7558 initializer
= cp_parser_braced_list (parser
,
7561 /* If that worked, we're definitely looking at a
7562 compound-literal expression. */
7563 if (cp_parser_parse_definitely (parser
))
7565 /* Warn the user that a compound literal is not
7566 allowed in standard C++. */
7567 pedwarn (input_location
, OPT_Wpedantic
,
7568 "ISO C++ forbids compound-literals");
7569 /* For simplicity, we disallow compound literals in
7570 constant-expressions. We could
7571 allow compound literals of integer type, whose
7572 initializer was a constant, in constant
7573 expressions. Permitting that usage, as a further
7574 extension, would not change the meaning of any
7575 currently accepted programs. (Of course, as
7576 compound literals are not part of ISO C++, the
7577 standard has nothing to say.) */
7578 if (cp_parser_non_integral_constant_expression (parser
,
7581 postfix_expression
= error_mark_node
;
7584 /* Form the representation of the compound-literal. */
7586 = finish_compound_literal (type
, initializer
,
7587 tf_warning_or_error
, fcl_c99
);
7588 postfix_expression
.set_location (initializer
.get_location ());
7593 /* It must be a primary-expression. */
7595 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7596 /*template_arg_p=*/false,
7603 /* Note that we don't need to worry about calling build_cplus_new on a
7604 class-valued CALL_EXPR in decltype when it isn't the end of the
7605 postfix-expression; unary_complex_lvalue will take care of that for
7608 /* Keep looping until the postfix-expression is complete. */
7611 if (idk
== CP_ID_KIND_UNQUALIFIED
7612 && identifier_p (postfix_expression
)
7613 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7614 /* It is not a Koenig lookup function call. */
7616 = unqualified_name_lookup_error (postfix_expression
);
7618 /* Peek at the next token. */
7619 token
= cp_lexer_peek_token (parser
->lexer
);
7621 switch (token
->type
)
7623 case CPP_OPEN_SQUARE
:
7624 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7626 cp_parser_error (parser
,
7627 "two consecutive %<[%> shall "
7628 "only introduce an attribute");
7629 return error_mark_node
;
7632 = cp_parser_postfix_open_square_expression (parser
,
7636 postfix_expression
.set_range (start_loc
,
7637 postfix_expression
.get_location ());
7639 idk
= CP_ID_KIND_NONE
;
7640 is_member_access
= false;
7643 case CPP_OPEN_PAREN
:
7644 /* postfix-expression ( expression-list [opt] ) */
7647 bool is_builtin_constant_p
;
7648 bool saved_integral_constant_expression_p
= false;
7649 bool saved_non_integral_constant_expression_p
= false;
7650 tsubst_flags_t complain
= complain_flags (decltype_p
);
7651 vec
<tree
, va_gc
> *args
;
7652 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7653 location_t combined_loc
= UNKNOWN_LOCATION
;
7655 is_member_access
= false;
7657 tree stripped_expression
7658 = tree_strip_any_location_wrapper (postfix_expression
);
7659 is_builtin_constant_p
7660 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7661 if (is_builtin_constant_p
)
7663 /* The whole point of __builtin_constant_p is to allow
7664 non-constant expressions to appear as arguments. */
7665 saved_integral_constant_expression_p
7666 = parser
->integral_constant_expression_p
;
7667 saved_non_integral_constant_expression_p
7668 = parser
->non_integral_constant_expression_p
;
7669 parser
->integral_constant_expression_p
= false;
7671 args
= (cp_parser_parenthesized_expression_list
7673 /*cast_p=*/false, /*allow_expansion_p=*/true,
7674 /*non_constant_p=*/NULL
,
7675 /*close_paren_loc=*/&close_paren_loc
,
7676 /*wrap_locations_p=*/true));
7677 if (is_builtin_constant_p
)
7679 parser
->integral_constant_expression_p
7680 = saved_integral_constant_expression_p
;
7681 parser
->non_integral_constant_expression_p
7682 = saved_non_integral_constant_expression_p
;
7687 postfix_expression
= error_mark_node
;
7691 /* Function calls are not permitted in
7692 constant-expressions. */
7693 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7694 && cp_parser_non_integral_constant_expression (parser
,
7697 postfix_expression
= error_mark_node
;
7698 release_tree_vector (args
);
7703 if (idk
== CP_ID_KIND_UNQUALIFIED
7704 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7706 if (identifier_p (postfix_expression
)
7707 /* In C++20, we may need to perform ADL for a template
7709 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7710 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7712 if (!args
->is_empty ())
7715 if (!any_type_dependent_arguments_p (args
))
7717 = perform_koenig_lookup (postfix_expression
, args
,
7722 = unqualified_fn_lookup_error (postfix_expression
);
7724 /* We do not perform argument-dependent lookup if
7725 normal lookup finds a non-function, in accordance
7726 with the expected resolution of DR 218. */
7727 else if (!args
->is_empty ()
7728 && is_overloaded_fn (postfix_expression
))
7730 /* Do not do argument dependent lookup if regular
7731 lookup finds a member function or a block-scope
7732 function declaration. [basic.lookup.argdep]/3 */
7733 bool do_adl_p
= true;
7734 tree fns
= get_fns (postfix_expression
);
7735 for (lkp_iterator
iter (fns
); iter
; ++iter
)
7737 tree fn
= STRIP_TEMPLATE (*iter
);
7738 if ((TREE_CODE (fn
) == USING_DECL
7739 && DECL_DEPENDENT_P (fn
))
7740 || DECL_FUNCTION_MEMBER_P (fn
)
7741 || DECL_LOCAL_DECL_P (fn
))
7751 if (!any_type_dependent_arguments_p (args
))
7753 = perform_koenig_lookup (postfix_expression
, args
,
7759 /* Temporarily set input_location to the combined location
7760 with call expression range, as e.g. build_out_target_exprs
7761 called from convert_default_arg relies on input_location,
7762 so updating it only when the call is fully built results
7763 in inconsistencies between location handling in templates
7764 and outside of templates. */
7765 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7766 combined_loc
= make_location (token
->location
, start_loc
,
7768 iloc_sentinel
ils (combined_loc
);
7770 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7772 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7773 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7775 if (processing_template_decl
7776 && (type_dependent_object_expression_p (instance
)
7777 || (!BASELINK_P (fn
)
7778 && TREE_CODE (fn
) != FIELD_DECL
)
7779 || type_dependent_expression_p (fn
)
7780 || any_type_dependent_arguments_p (args
)))
7782 maybe_generic_this_capture (instance
, fn
);
7784 = build_min_nt_call_vec (postfix_expression
, args
);
7786 else if (BASELINK_P (fn
))
7789 = (build_new_method_call
7790 (instance
, fn
, &args
, NULL_TREE
,
7791 (idk
== CP_ID_KIND_QUALIFIED
7792 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7799 = finish_call_expr (postfix_expression
, &args
,
7800 /*disallow_virtual=*/false,
7804 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7805 || TREE_CODE (postfix_expression
) == MEMBER_REF
7806 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7807 postfix_expression
= (build_offset_ref_call_from_tree
7808 (postfix_expression
, &args
,
7810 else if (idk
== CP_ID_KIND_QUALIFIED
)
7811 /* A call to a static class member, or a namespace-scope
7814 = finish_call_expr (postfix_expression
, &args
,
7815 /*disallow_virtual=*/true,
7819 /* All other function calls. */
7821 = finish_call_expr (postfix_expression
, &args
,
7822 /*disallow_virtual=*/false,
7826 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7827 postfix_expression
.set_location (combined_loc
);
7829 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7830 idk
= CP_ID_KIND_NONE
;
7832 release_tree_vector (args
);
7838 /* postfix-expression . template [opt] id-expression
7839 postfix-expression . pseudo-destructor-name
7840 postfix-expression -> template [opt] id-expression
7841 postfix-expression -> pseudo-destructor-name */
7843 /* Consume the `.' or `->' operator. */
7844 cp_lexer_consume_token (parser
->lexer
);
7847 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7851 is_member_access
= true;
7855 /* postfix-expression ++ */
7856 /* Consume the `++' token. */
7857 cp_lexer_consume_token (parser
->lexer
);
7858 /* Generate a representation for the complete expression. */
7860 = finish_increment_expr (postfix_expression
,
7861 POSTINCREMENT_EXPR
);
7862 /* Increments may not appear in constant-expressions. */
7863 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7864 postfix_expression
= error_mark_node
;
7865 idk
= CP_ID_KIND_NONE
;
7866 is_member_access
= false;
7869 case CPP_MINUS_MINUS
:
7870 /* postfix-expression -- */
7871 /* Consume the `--' token. */
7872 cp_lexer_consume_token (parser
->lexer
);
7873 /* Generate a representation for the complete expression. */
7875 = finish_increment_expr (postfix_expression
,
7876 POSTDECREMENT_EXPR
);
7877 /* Decrements may not appear in constant-expressions. */
7878 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7879 postfix_expression
= error_mark_node
;
7880 idk
= CP_ID_KIND_NONE
;
7881 is_member_access
= false;
7885 if (pidk_return
!= NULL
)
7886 * pidk_return
= idk
;
7887 if (member_access_only_p
)
7888 return is_member_access
7889 ? postfix_expression
7890 : cp_expr (error_mark_node
);
7892 return postfix_expression
;
7897 /* Helper function for cp_parser_parenthesized_expression_list and
7898 cp_parser_postfix_open_square_expression. Parse a single element
7899 of parenthesized expression list. */
7902 cp_parser_parenthesized_expression_list_elt (cp_parser
*parser
, bool cast_p
,
7903 bool allow_expansion_p
,
7905 bool *non_constant_p
)
7907 cp_expr
expr (NULL_TREE
);
7908 bool expr_non_constant_p
;
7910 /* Parse the next assignment-expression. */
7911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7913 /* A braced-init-list. */
7914 cp_lexer_set_source_position (parser
->lexer
);
7915 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7916 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7917 if (non_constant_p
&& expr_non_constant_p
)
7918 *non_constant_p
= true;
7920 else if (non_constant_p
)
7922 expr
= cp_parser_constant_expression (parser
,
7923 /*allow_non_constant_p=*/true,
7924 &expr_non_constant_p
);
7925 if (expr_non_constant_p
)
7926 *non_constant_p
= true;
7929 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
, cast_p
);
7932 expr
= instantiate_non_dependent_expr (expr
);
7934 /* If we have an ellipsis, then this is an expression expansion. */
7935 if (allow_expansion_p
7936 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7938 /* Consume the `...'. */
7939 cp_lexer_consume_token (parser
->lexer
);
7941 /* Build the argument pack. */
7942 expr
= make_pack_expansion (expr
);
7947 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7948 by cp_parser_builtin_offsetof. We're looking for
7950 postfix-expression [ expression ]
7951 postfix-expression [ braced-init-list ] (C++11)
7952 postfix-expression [ expression-list[opt] ] (C++23)
7954 FOR_OFFSETOF is set if we're being called in that context, which
7955 changes how we deal with integer constant expressions. */
7958 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7959 tree postfix_expression
,
7963 tree index
= NULL_TREE
;
7964 releasing_vec expression_list
= NULL
;
7965 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7966 bool saved_greater_than_is_operator_p
;
7968 /* Consume the `[' token. */
7969 cp_lexer_consume_token (parser
->lexer
);
7971 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7972 parser
->greater_than_is_operator_p
= true;
7974 /* Parse the index expression. */
7975 /* ??? For offsetof, there is a question of what to allow here. If
7976 offsetof is not being used in an integral constant expression context,
7977 then we *could* get the right answer by computing the value at runtime.
7978 If we are in an integral constant expression context, then we might
7979 could accept any constant expression; hard to say without analysis.
7980 Rather than open the barn door too wide right away, allow only integer
7981 constant expressions here. */
7983 index
= cp_parser_constant_expression (parser
);
7986 if (cxx_dialect
>= cxx23
7987 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
7988 *&expression_list
= make_tree_vector ();
7989 else if (cxx_dialect
>= cxx23
)
7994 = cp_parser_parenthesized_expression_list_elt (parser
,
8004 if (expr
== error_mark_node
)
8005 index
= error_mark_node
;
8006 else if (expression_list
.get () == NULL
8007 && !PACK_EXPANSION_P (expr
.get_value ()))
8008 index
= expr
.get_value ();
8010 vec_safe_push (expression_list
, expr
.get_value ());
8012 /* If the next token isn't a `,', then we are done. */
8013 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8016 if (expression_list
.get () == NULL
&& index
!= error_mark_node
)
8018 *&expression_list
= make_tree_vector_single (index
);
8022 /* Otherwise, consume the `,' and keep going. */
8023 cp_lexer_consume_token (parser
->lexer
);
8025 if (expression_list
.get () && index
== error_mark_node
)
8026 expression_list
.release ();
8028 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8030 bool expr_nonconst_p
;
8031 cp_lexer_set_source_position (parser
->lexer
);
8032 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8033 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
8036 index
= cp_parser_expression (parser
, NULL
, /*cast_p=*/false,
8037 /*decltype_p=*/false,
8038 /*warn_comma_p=*/warn_comma_subscript
);
8041 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
8043 /* Look for the closing `]'. */
8044 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8046 /* Build the ARRAY_REF. */
8047 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
8048 index
, &expression_list
,
8050 | (decltype_p
? tf_decltype
: 0));
8052 /* When not doing offsetof, array references are not permitted in
8053 constant-expressions. */
8055 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
8056 postfix_expression
= error_mark_node
;
8058 return postfix_expression
;
8061 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8062 dereference of incomplete type, returns true if error_mark_node should
8063 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8064 and *DEPENDENT_P. */
8067 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
8070 /* In a template, be permissive by treating an object expression
8071 of incomplete type as dependent (after a pedwarn). */
8072 diagnostic_t kind
= (processing_template_decl
8073 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
8075 switch (TREE_CODE (*postfix_expression
))
8078 case REINTERPRET_CAST_EXPR
:
8079 case CONST_CAST_EXPR
:
8080 case STATIC_CAST_EXPR
:
8081 case DYNAMIC_CAST_EXPR
:
8082 case IMPLICIT_CONV_EXPR
:
8083 case VIEW_CONVERT_EXPR
:
8084 case NON_LVALUE_EXPR
:
8088 /* Don't emit any diagnostic for OVERLOADs. */
8092 /* Avoid clobbering e.g. DECLs. */
8093 if (!EXPR_P (*postfix_expression
))
8098 if (kind
== DK_IGNORED
)
8101 location_t exploc
= location_of (*postfix_expression
);
8102 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
8103 if (!MAYBE_CLASS_TYPE_P (*scope
))
8105 if (kind
== DK_ERROR
)
8106 *scope
= *postfix_expression
= error_mark_node
;
8107 else if (processing_template_decl
)
8109 *dependent_p
= true;
8110 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
8115 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8116 by cp_parser_builtin_offsetof. We're looking for
8118 postfix-expression . template [opt] id-expression
8119 postfix-expression . pseudo-destructor-name
8120 postfix-expression -> template [opt] id-expression
8121 postfix-expression -> pseudo-destructor-name
8123 FOR_OFFSETOF is set if we're being called in that context. That sorta
8124 limits what of the above we'll actually accept, but nevermind.
8125 TOKEN_TYPE is the "." or "->" token, which will already have been
8126 removed from the stream. */
8129 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
8130 enum cpp_ttype token_type
,
8131 cp_expr postfix_expression
,
8132 bool for_offsetof
, cp_id_kind
*idk
,
8133 location_t location
)
8137 bool pseudo_destructor_p
;
8138 tree scope
= NULL_TREE
;
8139 location_t start_loc
= postfix_expression
.get_start ();
8141 /* If this is a `->' operator, dereference the pointer. */
8142 if (token_type
== CPP_DEREF
)
8143 postfix_expression
= build_x_arrow (location
, postfix_expression
,
8144 tf_warning_or_error
);
8145 /* Check to see whether or not the expression is type-dependent and
8146 not the current instantiation. */
8147 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
8148 /* The identifier following the `->' or `.' is not qualified. */
8149 parser
->scope
= NULL_TREE
;
8150 parser
->qualifying_scope
= NULL_TREE
;
8151 parser
->object_scope
= NULL_TREE
;
8152 *idk
= CP_ID_KIND_NONE
;
8154 /* Enter the scope corresponding to the type of the object
8155 given by the POSTFIX_EXPRESSION. */
8158 scope
= TREE_TYPE (postfix_expression
);
8159 /* According to the standard, no expression should ever have
8160 reference type. Unfortunately, we do not currently match
8161 the standard in this respect in that our internal representation
8162 of an expression may have reference type even when the standard
8163 says it does not. Therefore, we have to manually obtain the
8164 underlying type here. */
8165 scope
= non_reference (scope
);
8166 /* The type of the POSTFIX_EXPRESSION must be complete. */
8167 /* Unlike the object expression in other contexts, *this is not
8168 required to be of complete type for purposes of class member
8169 access (5.2.5) outside the member function body. */
8170 if (postfix_expression
!= current_class_ref
8171 && scope
!= error_mark_node
8172 && !currently_open_class (scope
))
8174 scope
= complete_type (scope
);
8175 if (!COMPLETE_TYPE_P (scope
)
8176 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
8178 return error_mark_node
;
8183 /* Let the name lookup machinery know that we are processing a
8184 class member access expression. */
8185 parser
->context
->object_type
= scope
;
8186 /* If something went wrong, we want to be able to discern that case,
8187 as opposed to the case where there was no SCOPE due to the type
8188 of expression being dependent. */
8190 scope
= error_mark_node
;
8191 /* If the SCOPE was erroneous, make the various semantic analysis
8192 functions exit quickly -- and without issuing additional error
8194 if (scope
== error_mark_node
)
8195 postfix_expression
= error_mark_node
;
8201 tree type
= TREE_TYPE (postfix_expression
);
8202 /* If we don't have a (type-dependent) object of class type, use
8203 typeof to figure out the type of the object. */
8204 if (type
== NULL_TREE
)
8205 type
= finish_typeof (postfix_expression
);
8206 parser
->context
->object_type
= type
;
8209 /* Assume this expression is not a pseudo-destructor access. */
8210 pseudo_destructor_p
= false;
8212 /* If the SCOPE is a scalar type, then, if this is a valid program,
8213 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8214 is type dependent, it can be pseudo-destructor-name or something else.
8215 Try to parse it as pseudo-destructor-name first. */
8216 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
8221 cp_parser_parse_tentatively (parser
);
8222 /* Parse the pseudo-destructor-name. */
8224 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
8227 && (cp_parser_error_occurred (parser
)
8228 || !SCALAR_TYPE_P (type
)))
8229 cp_parser_abort_tentative_parse (parser
);
8230 else if (cp_parser_parse_definitely (parser
))
8232 pseudo_destructor_p
= true;
8234 = finish_pseudo_destructor_expr (postfix_expression
,
8239 if (!pseudo_destructor_p
)
8241 /* If the SCOPE is not a scalar type, we are looking at an
8242 ordinary class member access expression, rather than a
8243 pseudo-destructor-name. */
8245 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8246 /* Parse the id-expression. */
8247 name
= (cp_parser_id_expression
8249 cp_parser_optional_template_keyword (parser
),
8250 /*check_dependency_p=*/true,
8252 /*declarator_p=*/false,
8253 /*optional_p=*/false));
8254 /* In general, build a SCOPE_REF if the member name is qualified.
8255 However, if the name was not dependent and has already been
8256 resolved; there is no need to build the SCOPE_REF. For example;
8258 struct X { void f(); };
8259 template <typename T> void f(T* t) { t->X::f(); }
8261 Even though "t" is dependent, "X::f" is not and has been resolved
8262 to a BASELINK; there is no need to include scope information. */
8264 /* But we do need to remember that there was an explicit scope for
8265 virtual function calls. */
8267 *idk
= CP_ID_KIND_QUALIFIED
;
8269 /* If the name is a template-id that names a type, we will get a
8270 TYPE_DECL here. That is invalid code. */
8271 if (TREE_CODE (name
) == TYPE_DECL
)
8273 error_at (token
->location
, "invalid use of %qD", name
);
8274 postfix_expression
= error_mark_node
;
8278 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
8280 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
8282 error_at (token
->location
, "%<%D::%D%> is not a class member",
8283 parser
->scope
, name
);
8284 postfix_expression
= error_mark_node
;
8287 name
= build_qualified_name (/*type=*/NULL_TREE
,
8291 parser
->scope
= NULL_TREE
;
8292 parser
->qualifying_scope
= NULL_TREE
;
8293 parser
->object_scope
= NULL_TREE
;
8295 if (parser
->scope
&& name
&& BASELINK_P (name
))
8296 adjust_result_of_qualified_name_lookup
8297 (name
, parser
->scope
, scope
);
8299 = finish_class_member_access_expr (postfix_expression
, name
,
8301 tf_warning_or_error
);
8302 /* Build a location e.g.:
8305 where the caret is at the deref token, ranging from
8306 the start of postfix_expression to the end of the access expr. */
8307 location_t combined_loc
8308 = make_location (input_location
, start_loc
, parser
->lexer
);
8309 protected_set_expr_location (postfix_expression
, combined_loc
);
8313 /* We no longer need to look up names in the scope of the object on
8314 the left-hand side of the `.' or `->' operator. */
8315 parser
->context
->object_type
= NULL_TREE
;
8317 /* Outside of offsetof, these operators may not appear in
8318 constant-expressions. */
8320 && (cp_parser_non_integral_constant_expression
8321 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
8322 postfix_expression
= error_mark_node
;
8324 return postfix_expression
;
8327 /* Parse a parenthesized expression-list.
8330 assignment-expression
8331 expression-list, assignment-expression
8336 identifier, expression-list
8338 CAST_P is true if this expression is the target of a cast.
8340 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8343 WRAP_LOCATIONS_P is true if expressions within this list for which
8344 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8345 their source locations.
8347 Returns a vector of trees. Each element is a representation of an
8348 assignment-expression. NULL is returned if the ( and or ) are
8349 missing. An empty, but allocated, vector is returned on no
8350 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8351 if we are parsing an attribute list for an attribute that wants a
8352 plain identifier argument, normal_attr for an attribute that wants
8353 an expression, or non_attr if we aren't parsing an attribute list. If
8354 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8355 not all of the expressions in the list were constant.
8356 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8357 will be written to with the location of the closing parenthesis. If
8358 an error occurs, it may or may not be written to. */
8360 static vec
<tree
, va_gc
> *
8361 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
8362 int is_attribute_list
,
8364 bool allow_expansion_p
,
8365 bool *non_constant_p
,
8366 location_t
*close_paren_loc
,
8367 bool wrap_locations_p
)
8369 vec
<tree
, va_gc
> *expression_list
;
8370 bool fold_expr_p
= is_attribute_list
!= non_attr
;
8371 tree identifier
= NULL_TREE
;
8372 bool saved_greater_than_is_operator_p
;
8374 /* Assume all the expressions will be constant. */
8376 *non_constant_p
= false;
8378 matching_parens parens
;
8379 if (!parens
.require_open (parser
))
8382 expression_list
= make_tree_vector ();
8384 /* Within a parenthesized expression, a `>' token is always
8385 the greater-than operator. */
8386 saved_greater_than_is_operator_p
8387 = parser
->greater_than_is_operator_p
;
8388 parser
->greater_than_is_operator_p
= true;
8390 cp_expr
expr (NULL_TREE
);
8392 /* Consume expressions until there are no more. */
8393 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
8396 /* At the beginning of attribute lists, check to see if the
8397 next token is an identifier. */
8398 if (is_attribute_list
== id_attr
8399 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
8403 /* Consume the identifier. */
8404 token
= cp_lexer_consume_token (parser
->lexer
);
8405 /* Save the identifier. */
8406 identifier
= token
->u
.value
;
8411 = cp_parser_parenthesized_expression_list_elt (parser
, cast_p
,
8416 if (wrap_locations_p
)
8417 expr
.maybe_add_location_wrapper ();
8419 /* Add it to the list. We add error_mark_node
8420 expressions to the list, so that we can still tell if
8421 the correct form for a parenthesized expression-list
8422 is found. That gives better errors. */
8423 vec_safe_push (expression_list
, expr
.get_value ());
8425 if (expr
== error_mark_node
)
8429 /* After the first item, attribute lists look the same as
8430 expression lists. */
8431 is_attribute_list
= non_attr
;
8434 /* If the next token isn't a `,', then we are done. */
8435 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8438 /* Otherwise, consume the `,' and keep going. */
8439 cp_lexer_consume_token (parser
->lexer
);
8442 if (close_paren_loc
)
8443 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8445 if (!parens
.require_close (parser
))
8450 /* We try and resync to an unnested comma, as that will give the
8451 user better diagnostics. */
8452 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
8453 /*recovering=*/true,
8455 /*consume_paren=*/true);
8460 parser
->greater_than_is_operator_p
8461 = saved_greater_than_is_operator_p
;
8466 parser
->greater_than_is_operator_p
8467 = saved_greater_than_is_operator_p
;
8470 vec_safe_insert (expression_list
, 0, identifier
);
8472 return expression_list
;
8475 /* Parse a pseudo-destructor-name.
8477 pseudo-destructor-name:
8478 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8479 :: [opt] nested-name-specifier template template-id :: ~ type-name
8480 :: [opt] nested-name-specifier [opt] ~ type-name
8482 If either of the first two productions is used, sets *SCOPE to the
8483 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8484 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8485 or ERROR_MARK_NODE if the parse fails. */
8488 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
8493 bool nested_name_specifier_p
;
8496 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
8497 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
8498 && !type_dependent_expression_p (object
))
8500 if (cxx_dialect
< cxx14
)
8501 pedwarn (input_location
, OPT_Wc__14_extensions
,
8502 "%<~auto%> only available with "
8503 "%<-std=c++14%> or %<-std=gnu++14%>");
8504 cp_lexer_consume_token (parser
->lexer
);
8505 cp_lexer_consume_token (parser
->lexer
);
8507 *type
= TREE_TYPE (object
);
8511 /* Assume that things will not work out. */
8512 *type
= error_mark_node
;
8514 /* Look for the optional `::' operator. */
8515 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8516 /* Look for the optional nested-name-specifier. */
8517 nested_name_specifier_p
8518 = (cp_parser_nested_name_specifier_opt (parser
,
8519 /*typename_keyword_p=*/false,
8520 /*check_dependency_p=*/true,
8522 /*is_declaration=*/false)
8524 /* Now, if we saw a nested-name-specifier, we might be doing the
8525 second production. */
8526 if (nested_name_specifier_p
8527 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8529 /* Consume the `template' keyword. */
8530 cp_lexer_consume_token (parser
->lexer
);
8531 /* Parse the template-id. */
8532 cp_parser_template_id (parser
,
8533 /*template_keyword_p=*/true,
8534 /*check_dependency_p=*/false,
8536 /*is_declaration=*/true);
8537 /* Look for the `::' token. */
8538 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8540 /* If the next token is not a `~', then there might be some
8541 additional qualification. */
8542 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8544 /* At this point, we're looking for "type-name :: ~". The type-name
8545 must not be a class-name, since this is a pseudo-destructor. So,
8546 it must be either an enum-name, or a typedef-name -- both of which
8547 are just identifiers. So, we peek ahead to check that the "::"
8548 and "~" tokens are present; if they are not, then we can avoid
8549 calling type_name. */
8550 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8551 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8552 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8554 cp_parser_error (parser
, "non-scalar type");
8558 /* Look for the type-name. */
8559 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8560 if (*scope
== error_mark_node
)
8563 /* Look for the `::' token. */
8564 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8569 /* Look for the `~'. */
8570 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8572 /* Once we see the ~, this has to be a pseudo-destructor. */
8573 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8574 cp_parser_commit_to_topmost_tentative_parse (parser
);
8576 /* Look for the type-name again. We are not responsible for
8577 checking that it matches the first type-name. */
8578 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8581 /* Parse a unary-expression.
8588 unary-operator cast-expression
8589 sizeof unary-expression
8591 alignof ( type-id ) [C++0x]
8598 __extension__ cast-expression
8599 __alignof__ unary-expression
8600 __alignof__ ( type-id )
8601 alignof unary-expression [C++0x]
8602 __real__ cast-expression
8603 __imag__ cast-expression
8605 sizeof ( type-id ) { initializer-list , [opt] }
8606 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8607 __alignof__ ( type-id ) { initializer-list , [opt] }
8609 ADDRESS_P is true iff the unary-expression is appearing as the
8610 operand of the `&' operator. CAST_P is true if this expression is
8611 the target of a cast.
8613 Returns a representation of the expression. */
8616 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8617 bool address_p
, bool cast_p
, bool decltype_p
)
8620 enum tree_code unary_operator
;
8622 /* Peek at the next token. */
8623 token
= cp_lexer_peek_token (parser
->lexer
);
8624 /* Some keywords give away the kind of expression. */
8625 if (token
->type
== CPP_KEYWORD
)
8627 enum rid keyword
= token
->keyword
;
8636 location_t start_loc
= token
->location
;
8638 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8639 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8641 /* Consume the token. */
8642 cp_lexer_consume_token (parser
->lexer
);
8643 /* Parse the operand. */
8644 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8646 /* Construct a location e.g. :
8649 with start == caret at the start of the "alignof"/"sizeof"
8650 token, with the endpoint at the final closing paren. */
8651 location_t compound_loc
8652 = make_location (start_loc
, start_loc
, parser
->lexer
);
8654 if (TYPE_P (operand
))
8655 ret
= cxx_sizeof_or_alignof_type (compound_loc
, operand
, op
,
8659 /* ISO C++ defines alignof only with types, not with
8660 expressions. So pedwarn if alignof is used with a non-
8661 type expression. However, __alignof__ is ok. */
8663 pedwarn (token
->location
, OPT_Wpedantic
,
8664 "ISO C++ does not allow %<alignof%> "
8667 ret
= cxx_sizeof_or_alignof_expr (compound_loc
, operand
, op
,
8670 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8671 SIZEOF_EXPR with the original operand. */
8672 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8674 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8676 if (!processing_template_decl
&& TYPE_P (operand
))
8678 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8679 build1 (NOP_EXPR
, operand
,
8681 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8684 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8685 TREE_SIDE_EFFECTS (ret
) = 0;
8686 TREE_READONLY (ret
) = 1;
8687 SET_EXPR_LOCATION (ret
, compound_loc
);
8691 cp_expr
ret_expr (ret
, compound_loc
);
8692 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8696 case RID_BUILTIN_HAS_ATTRIBUTE
:
8697 return cp_parser_has_attribute_expression (parser
);
8700 return cp_parser_new_expression (parser
);
8703 return cp_parser_delete_expression (parser
);
8707 /* The saved value of the PEDANTIC flag. */
8711 /* Save away the PEDANTIC flag. */
8712 cp_parser_extension_opt (parser
, &saved_pedantic
);
8713 /* Parse the cast-expression. */
8714 expr
= cp_parser_simple_cast_expression (parser
);
8715 /* Restore the PEDANTIC flag. */
8716 pedantic
= saved_pedantic
;
8726 /* Consume the `__real__' or `__imag__' token. */
8727 cp_lexer_consume_token (parser
->lexer
);
8728 /* Parse the cast-expression. */
8729 expression
= cp_parser_simple_cast_expression (parser
);
8730 /* Create the complete representation. */
8731 return build_x_unary_op (token
->location
,
8732 (keyword
== RID_REALPART
8733 ? REALPART_EXPR
: IMAGPART_EXPR
),
8734 expression
, NULL_TREE
,
8735 tf_warning_or_error
);
8739 case RID_TRANSACTION_ATOMIC
:
8740 case RID_TRANSACTION_RELAXED
:
8741 return cp_parser_transaction_expression (parser
, keyword
);
8746 const char *saved_message
;
8747 bool saved_integral_constant_expression_p
;
8748 bool saved_non_integral_constant_expression_p
;
8749 bool saved_greater_than_is_operator_p
;
8751 location_t start_loc
= token
->location
;
8753 cp_lexer_consume_token (parser
->lexer
);
8754 matching_parens parens
;
8755 parens
.require_open (parser
);
8757 saved_message
= parser
->type_definition_forbidden_message
;
8758 parser
->type_definition_forbidden_message
8759 = G_("types may not be defined in %<noexcept%> expressions");
8761 saved_integral_constant_expression_p
8762 = parser
->integral_constant_expression_p
;
8763 saved_non_integral_constant_expression_p
8764 = parser
->non_integral_constant_expression_p
;
8765 parser
->integral_constant_expression_p
= false;
8767 saved_greater_than_is_operator_p
8768 = parser
->greater_than_is_operator_p
;
8769 parser
->greater_than_is_operator_p
= true;
8771 ++cp_unevaluated_operand
;
8772 ++c_inhibit_evaluation_warnings
;
8773 ++cp_noexcept_operand
;
8774 expr
= cp_parser_expression (parser
);
8775 --cp_noexcept_operand
;
8776 --c_inhibit_evaluation_warnings
;
8777 --cp_unevaluated_operand
;
8779 parser
->greater_than_is_operator_p
8780 = saved_greater_than_is_operator_p
;
8782 parser
->integral_constant_expression_p
8783 = saved_integral_constant_expression_p
;
8784 parser
->non_integral_constant_expression_p
8785 = saved_non_integral_constant_expression_p
;
8787 parser
->type_definition_forbidden_message
= saved_message
;
8789 parens
.require_close (parser
);
8791 /* Construct a location of the form:
8794 with start == caret, finishing at the close-paren. */
8795 location_t noexcept_loc
8796 = make_location (start_loc
, start_loc
, parser
->lexer
);
8798 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8805 location_t kw_loc
= token
->location
;
8807 /* Consume the `co_await' token. */
8808 cp_lexer_consume_token (parser
->lexer
);
8809 /* Parse its cast-expression. */
8810 expr
= cp_parser_simple_cast_expression (parser
);
8811 if (expr
== error_mark_node
)
8812 return error_mark_node
;
8814 /* Handle [expr.await]. */
8815 return cp_expr (finish_co_await_expr (kw_loc
, expr
));
8823 /* Look for the `:: new' and `:: delete', which also signal the
8824 beginning of a new-expression, or delete-expression,
8825 respectively. If the next token is `::', then it might be one of
8827 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8831 /* See if the token after the `::' is one of the keywords in
8832 which we're interested. */
8833 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8834 /* If it's `new', we have a new-expression. */
8835 if (keyword
== RID_NEW
)
8836 return cp_parser_new_expression (parser
);
8837 /* Similarly, for `delete'. */
8838 else if (keyword
== RID_DELETE
)
8839 return cp_parser_delete_expression (parser
);
8842 /* Look for a unary operator. */
8843 unary_operator
= cp_parser_unary_operator (token
);
8844 /* The `++' and `--' operators can be handled similarly, even though
8845 they are not technically unary-operators in the grammar. */
8846 if (unary_operator
== ERROR_MARK
)
8848 if (token
->type
== CPP_PLUS_PLUS
)
8849 unary_operator
= PREINCREMENT_EXPR
;
8850 else if (token
->type
== CPP_MINUS_MINUS
)
8851 unary_operator
= PREDECREMENT_EXPR
;
8852 /* Handle the GNU address-of-label extension. */
8853 else if (cp_parser_allow_gnu_extensions_p (parser
)
8854 && token
->type
== CPP_AND_AND
)
8858 location_t start_loc
= token
->location
;
8860 /* Consume the '&&' token. */
8861 cp_lexer_consume_token (parser
->lexer
);
8862 /* Look for the identifier. */
8863 identifier
= cp_parser_identifier (parser
);
8864 /* Construct a location of the form:
8867 with caret==start at the "&&", finish at the end of the label. */
8868 location_t combined_loc
8869 = make_location (start_loc
, start_loc
, parser
->lexer
);
8870 /* Create an expression representing the address. */
8871 expression
= finish_label_address_expr (identifier
, combined_loc
);
8872 if (cp_parser_non_integral_constant_expression (parser
,
8874 expression
= error_mark_node
;
8878 if (unary_operator
!= ERROR_MARK
)
8880 cp_expr cast_expression
;
8881 cp_expr expression
= error_mark_node
;
8882 non_integral_constant non_constant_p
= NIC_NONE
;
8883 location_t loc
= token
->location
;
8884 tsubst_flags_t complain
= complain_flags (decltype_p
);
8886 /* Consume the operator token. */
8887 token
= cp_lexer_consume_token (parser
->lexer
);
8888 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8890 /* Parse the cast-expression. */
8892 = cp_parser_cast_expression (parser
,
8893 unary_operator
== ADDR_EXPR
,
8899 OP_TOKEN CAST_EXPRESSION
8900 ^~~~~~~~~~~~~~~~~~~~~~~~~
8901 with start==caret at the operator token, and
8902 extending to the end of the cast_expression. */
8903 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8905 /* Now, build an appropriate representation. */
8906 switch (unary_operator
)
8909 non_constant_p
= NIC_STAR
;
8910 expression
= build_x_indirect_ref (loc
, cast_expression
,
8911 RO_UNARY_STAR
, NULL_TREE
,
8913 /* TODO: build_x_indirect_ref does not always honor the
8914 location, so ensure it is set. */
8915 expression
.set_location (loc
);
8919 non_constant_p
= NIC_ADDR
;
8922 expression
= build_x_unary_op (loc
, unary_operator
,
8924 NULL_TREE
, complain
);
8925 /* TODO: build_x_unary_op does not always honor the location,
8926 so ensure it is set. */
8927 expression
.set_location (loc
);
8930 case PREINCREMENT_EXPR
:
8931 case PREDECREMENT_EXPR
:
8932 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8933 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8936 /* Immediately fold negation of a constant, unless the constant is 0
8937 (since -0 == 0) or it would overflow. */
8938 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8941 = tree_strip_any_location_wrapper (cast_expression
);
8942 if (CONSTANT_CLASS_P (stripped_expr
)
8943 && !integer_zerop (stripped_expr
)
8944 && !TREE_OVERFLOW (stripped_expr
))
8946 tree folded
= fold_build1 (unary_operator
,
8947 TREE_TYPE (stripped_expr
),
8949 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8951 expression
= maybe_wrap_with_location (folded
, loc
);
8957 case UNARY_PLUS_EXPR
:
8958 case TRUTH_NOT_EXPR
:
8959 expression
= finish_unary_op_expr (loc
, unary_operator
,
8960 cast_expression
, complain
);
8967 if (non_constant_p
!= NIC_NONE
8968 && cp_parser_non_integral_constant_expression (parser
,
8970 expression
= error_mark_node
;
8975 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8976 /*member_access_only_p=*/false,
8981 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8982 unary-operator, the corresponding tree code is returned. */
8984 static enum tree_code
8985 cp_parser_unary_operator (cp_token
* token
)
8987 switch (token
->type
)
8990 return INDIRECT_REF
;
8996 return UNARY_PLUS_EXPR
;
9002 return TRUTH_NOT_EXPR
;
9005 return BIT_NOT_EXPR
;
9012 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9013 Returns a representation of the expression. */
9016 cp_parser_has_attribute_expression (cp_parser
*parser
)
9018 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9020 /* Consume the __builtin_has_attribute token. */
9021 cp_lexer_consume_token (parser
->lexer
);
9023 matching_parens parens
;
9024 if (!parens
.require_open (parser
))
9025 return error_mark_node
;
9027 /* Types cannot be defined in a `sizeof' expression. Save away the
9029 const char *saved_message
= parser
->type_definition_forbidden_message
;
9030 const char *saved_message_arg
9031 = parser
->type_definition_forbidden_message_arg
;
9032 parser
->type_definition_forbidden_message
9033 = G_("types may not be defined in %qs expressions");
9034 parser
->type_definition_forbidden_message_arg
9035 = IDENTIFIER_POINTER (ridpointers
[RID_BUILTIN_HAS_ATTRIBUTE
]);
9037 /* The restrictions on constant-expressions do not apply inside
9038 sizeof expressions. */
9039 bool saved_integral_constant_expression_p
9040 = parser
->integral_constant_expression_p
;
9041 bool saved_non_integral_constant_expression_p
9042 = parser
->non_integral_constant_expression_p
;
9043 parser
->integral_constant_expression_p
= false;
9045 /* Do not actually evaluate the expression. */
9046 ++cp_unevaluated_operand
;
9047 ++c_inhibit_evaluation_warnings
;
9049 tree oper
= NULL_TREE
;
9051 /* We can't be sure yet whether we're looking at a type-id or an
9053 cp_parser_parse_tentatively (parser
);
9055 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9056 parser
->in_type_id_in_expr_p
= true;
9057 /* Look for the type-id. */
9058 oper
= cp_parser_type_id (parser
);
9059 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9061 cp_parser_parse_definitely (parser
);
9063 /* If the type-id production did not work out, then we must be
9064 looking at an expression. */
9065 if (!oper
|| oper
== error_mark_node
)
9066 oper
= cp_parser_assignment_expression (parser
);
9068 STRIP_ANY_LOCATION_WRAPPER (oper
);
9070 /* Go back to evaluating expressions. */
9071 --cp_unevaluated_operand
;
9072 --c_inhibit_evaluation_warnings
;
9074 /* And restore the old one. */
9075 parser
->type_definition_forbidden_message
= saved_message
;
9076 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
9077 parser
->integral_constant_expression_p
9078 = saved_integral_constant_expression_p
;
9079 parser
->non_integral_constant_expression_p
9080 = saved_non_integral_constant_expression_p
;
9082 /* Consume the comma if it's there. */
9083 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
9085 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9086 /*consume_paren=*/true);
9087 return error_mark_node
;
9090 /* Parse the attribute specification. */
9092 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9093 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
9095 if (oper
== error_mark_node
)
9097 else if (processing_template_decl
&& uses_template_parms (oper
))
9098 sorry_at (atloc
, "%<__builtin_has_attribute%> with dependent argument "
9099 "not supported yet");
9102 /* Fold constant expressions used in attributes first. */
9103 cp_check_const_attributes (attr
);
9105 /* Finally, see if OPER has been declared with ATTR. */
9106 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
9109 parens
.require_close (parser
);
9113 error_at (atloc
, "expected identifier");
9114 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9117 /* Construct a location e.g. :
9118 __builtin_has_attribute (oper, attr)
9119 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9120 with start == caret at the start of the built-in token,
9121 and with the endpoint at the final closing paren. */
9122 location_t compound_loc
9123 = make_location (start_loc
, start_loc
, parser
->lexer
);
9125 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
9126 ret_expr
.set_location (compound_loc
);
9127 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
9131 /* Parse a new-expression.
9134 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9135 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9137 Returns a representation of the expression. */
9140 cp_parser_new_expression (cp_parser
* parser
)
9142 bool global_scope_p
;
9143 vec
<tree
, va_gc
> *placement
;
9145 vec
<tree
, va_gc
> *initializer
;
9146 tree nelts
= NULL_TREE
;
9149 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9151 /* Look for the optional `::' operator. */
9153 = (cp_parser_global_scope_opt (parser
,
9154 /*current_scope_valid_p=*/false)
9156 /* Look for the `new' operator. */
9157 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
9158 /* There's no easy way to tell a new-placement from the
9159 `( type-id )' construct. */
9160 cp_parser_parse_tentatively (parser
);
9161 /* Look for a new-placement. */
9162 placement
= cp_parser_new_placement (parser
);
9163 /* If that didn't work out, there's no new-placement. */
9164 if (!cp_parser_parse_definitely (parser
))
9166 if (placement
!= NULL
)
9167 release_tree_vector (placement
);
9171 /* If the next token is a `(', then we have a parenthesized
9173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9176 const char *saved_message
= parser
->type_definition_forbidden_message
;
9178 /* Consume the `('. */
9179 matching_parens parens
;
9180 parens
.consume_open (parser
);
9182 /* Parse the type-id. */
9183 parser
->type_definition_forbidden_message
9184 = G_("types may not be defined in a new-expression");
9186 type_id_in_expr_sentinel
s (parser
);
9187 type
= cp_parser_type_id (parser
);
9189 parser
->type_definition_forbidden_message
= saved_message
;
9191 /* Look for the closing `)'. */
9192 parens
.require_close (parser
);
9193 token
= cp_lexer_peek_token (parser
->lexer
);
9194 /* There should not be a direct-new-declarator in this production,
9195 but GCC used to allowed this, so we check and emit a sensible error
9196 message for this case. */
9197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9199 error_at (token
->location
,
9200 "array bound forbidden after parenthesized type-id");
9201 inform (token
->location
,
9202 "try removing the parentheses around the type-id");
9203 cp_parser_direct_new_declarator (parser
);
9206 /* Otherwise, there must be a new-type-id. */
9208 type
= cp_parser_new_type_id (parser
, &nelts
);
9210 /* If the next token is a `(' or '{', then we have a new-initializer. */
9211 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9212 if (token
->type
== CPP_OPEN_PAREN
9213 || token
->type
== CPP_OPEN_BRACE
)
9214 initializer
= cp_parser_new_initializer (parser
);
9218 /* A new-expression may not appear in an integral constant
9220 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
9221 ret
= error_mark_node
;
9222 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9223 of a new-type-id or type-id of a new-expression, the new-expression shall
9224 contain a new-initializer of the form ( assignment-expression )".
9225 Additionally, consistently with the spirit of DR 1467, we want to accept
9226 'new auto { 2 }' too. */
9227 else if ((ret
= type_uses_auto (type
))
9228 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
9229 && (vec_safe_length (initializer
) != 1
9230 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
9231 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
9233 error_at (token
->location
,
9234 "initialization of new-expression for type %<auto%> "
9235 "requires exactly one element");
9236 ret
= error_mark_node
;
9240 /* Construct a location e.g.:
9243 with caret == start at the start of the "new" token, and the end
9244 at the end of the final token we consumed. */
9245 location_t combined_loc
= make_location (start_loc
, start_loc
,
9247 /* Create a representation of the new-expression. */
9248 ret
= build_new (combined_loc
, &placement
, type
, nelts
, &initializer
,
9249 global_scope_p
, tf_warning_or_error
);
9252 if (placement
!= NULL
)
9253 release_tree_vector (placement
);
9254 if (initializer
!= NULL
)
9255 release_tree_vector (initializer
);
9260 /* Parse a new-placement.
9265 Returns the same representation as for an expression-list. */
9267 static vec
<tree
, va_gc
> *
9268 cp_parser_new_placement (cp_parser
* parser
)
9270 vec
<tree
, va_gc
> *expression_list
;
9272 /* Parse the expression-list. */
9273 expression_list
= (cp_parser_parenthesized_expression_list
9274 (parser
, non_attr
, /*cast_p=*/false,
9275 /*allow_expansion_p=*/true,
9276 /*non_constant_p=*/NULL
));
9278 if (expression_list
&& expression_list
->is_empty ())
9279 error ("expected expression-list or type-id");
9281 return expression_list
;
9284 /* Parse a new-type-id.
9287 type-specifier-seq new-declarator [opt]
9289 Returns the TYPE allocated. If the new-type-id indicates an array
9290 type, *NELTS is set to the number of elements in the last array
9291 bound; the TYPE will not include the last array bound. */
9294 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
9296 cp_decl_specifier_seq type_specifier_seq
;
9297 cp_declarator
*new_declarator
;
9298 cp_declarator
*declarator
;
9299 cp_declarator
*outer_declarator
;
9300 const char *saved_message
;
9302 /* The type-specifier sequence must not contain type definitions.
9303 (It cannot contain declarations of new types either, but if they
9304 are not definitions we will catch that because they are not
9306 saved_message
= parser
->type_definition_forbidden_message
;
9307 parser
->type_definition_forbidden_message
9308 = G_("types may not be defined in a new-type-id");
9309 /* Parse the type-specifier-seq. */
9310 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
9311 /*is_declaration=*/false,
9312 /*is_trailing_return=*/false,
9313 &type_specifier_seq
);
9314 /* Restore the old message. */
9315 parser
->type_definition_forbidden_message
= saved_message
;
9317 if (type_specifier_seq
.type
== error_mark_node
)
9318 return error_mark_node
;
9320 /* Parse the new-declarator. */
9321 new_declarator
= cp_parser_new_declarator_opt (parser
);
9323 /* Determine the number of elements in the last array dimension, if
9326 /* Skip down to the last array dimension. */
9327 declarator
= new_declarator
;
9328 outer_declarator
= NULL
;
9329 while (declarator
&& (declarator
->kind
== cdk_pointer
9330 || declarator
->kind
== cdk_ptrmem
))
9332 outer_declarator
= declarator
;
9333 declarator
= declarator
->declarator
;
9336 && declarator
->kind
== cdk_array
9337 && declarator
->declarator
9338 && declarator
->declarator
->kind
== cdk_array
)
9340 outer_declarator
= declarator
;
9341 declarator
= declarator
->declarator
;
9344 if (declarator
&& declarator
->kind
== cdk_array
)
9346 *nelts
= declarator
->u
.array
.bounds
;
9347 if (*nelts
== error_mark_node
)
9348 *nelts
= integer_one_node
;
9350 if (*nelts
== NULL_TREE
)
9351 /* Leave [] in the declarator. */;
9352 else if (outer_declarator
)
9353 outer_declarator
->declarator
= declarator
->declarator
;
9355 new_declarator
= NULL
;
9358 return groktypename (&type_specifier_seq
, new_declarator
, false);
9361 /* Parse an (optional) new-declarator.
9364 ptr-operator new-declarator [opt]
9365 direct-new-declarator
9367 Returns the declarator. */
9369 static cp_declarator
*
9370 cp_parser_new_declarator_opt (cp_parser
* parser
)
9372 enum tree_code code
;
9373 tree type
, std_attributes
= NULL_TREE
;
9374 cp_cv_quals cv_quals
;
9376 /* We don't know if there's a ptr-operator next, or not. */
9377 cp_parser_parse_tentatively (parser
);
9378 /* Look for a ptr-operator. */
9379 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
9380 /* If that worked, look for more new-declarators. */
9381 if (cp_parser_parse_definitely (parser
))
9383 cp_declarator
*declarator
;
9385 /* Parse another optional declarator. */
9386 declarator
= cp_parser_new_declarator_opt (parser
);
9388 declarator
= cp_parser_make_indirect_declarator
9389 (code
, type
, cv_quals
, declarator
, std_attributes
);
9394 /* If the next token is a `[', there is a direct-new-declarator. */
9395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9396 return cp_parser_direct_new_declarator (parser
);
9401 /* Parse a direct-new-declarator.
9403 direct-new-declarator:
9405 direct-new-declarator [constant-expression]
9409 static cp_declarator
*
9410 cp_parser_direct_new_declarator (cp_parser
* parser
)
9412 cp_declarator
*declarator
= NULL
;
9413 bool first_p
= true;
9420 /* Look for the opening `['. */
9421 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9423 token
= cp_lexer_peek_token (parser
->lexer
);
9424 if (token
->type
== CPP_CLOSE_SQUARE
&& first_p
)
9425 expression
= NULL_TREE
;
9427 expression
= cp_parser_expression (parser
);
9428 /* The standard requires that the expression have integral
9429 type. DR 74 adds enumeration types. We believe that the
9430 real intent is that these expressions be handled like the
9431 expression in a `switch' condition, which also allows
9432 classes with a single conversion to integral or
9433 enumeration type. */
9434 if (expression
&& !processing_template_decl
)
9437 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
9442 error_at (token
->location
,
9443 "expression in new-declarator must have integral "
9444 "or enumeration type");
9445 expression
= error_mark_node
;
9449 /* Look for the closing `]'. */
9450 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9452 /* Add this bound to the declarator. */
9453 declarator
= make_array_declarator (declarator
, expression
);
9455 /* If the next token is not a `[', then there are no more
9457 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
9465 /* Parse a new-initializer.
9468 ( expression-list [opt] )
9471 Returns a representation of the expression-list. */
9473 static vec
<tree
, va_gc
> *
9474 cp_parser_new_initializer (cp_parser
* parser
)
9476 vec
<tree
, va_gc
> *expression_list
;
9478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9481 bool expr_non_constant_p
;
9482 cp_lexer_set_source_position (parser
->lexer
);
9483 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9484 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9485 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
9486 expression_list
= make_tree_vector_single (t
);
9489 expression_list
= (cp_parser_parenthesized_expression_list
9490 (parser
, non_attr
, /*cast_p=*/false,
9491 /*allow_expansion_p=*/true,
9492 /*non_constant_p=*/NULL
));
9494 return expression_list
;
9497 /* Parse a delete-expression.
9500 :: [opt] delete cast-expression
9501 :: [opt] delete [ ] cast-expression
9503 Returns a representation of the expression. */
9506 cp_parser_delete_expression (cp_parser
* parser
)
9508 bool global_scope_p
;
9511 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9513 /* Look for the optional `::' operator. */
9515 = (cp_parser_global_scope_opt (parser
,
9516 /*current_scope_valid_p=*/false)
9518 /* Look for the `delete' keyword. */
9519 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
9520 /* See if the array syntax is in use. */
9521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9523 /* Consume the `[' token. */
9524 cp_lexer_consume_token (parser
->lexer
);
9525 /* Look for the `]' token. */
9526 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9527 /* Remember that this is the `[]' construct. */
9533 /* Parse the cast-expression. */
9534 expression
= cp_parser_simple_cast_expression (parser
);
9536 /* A delete-expression may not appear in an integral constant
9538 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9539 return error_mark_node
;
9541 /* Construct a location e.g.:
9544 with caret == start at the start of the "delete" token, and
9545 the end at the end of the final token we consumed. */
9546 location_t combined_loc
= make_location (start_loc
, start_loc
,
9548 expression
= delete_sanity (combined_loc
, expression
, NULL_TREE
, array_p
,
9549 global_scope_p
, tf_warning_or_error
);
9554 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9555 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9559 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9561 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9562 switch (token
->type
)
9568 case CPP_CLOSE_SQUARE
:
9569 case CPP_CLOSE_PAREN
:
9570 case CPP_CLOSE_BRACE
:
9571 case CPP_OPEN_BRACE
:
9575 case CPP_DEREF_STAR
:
9583 case CPP_GREATER_EQ
:
9604 case CPP_OPEN_PAREN
:
9605 /* In ((type ()) () the last () isn't a valid cast-expression,
9606 so the whole must be parsed as postfix-expression. */
9607 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9610 case CPP_OPEN_SQUARE
:
9611 /* '[' may start a primary-expression in obj-c++ and in C++11,
9612 as a lambda-expression, eg, '(void)[]{}'. */
9613 if (cxx_dialect
>= cxx11
)
9615 return c_dialect_objc ();
9618 case CPP_MINUS_MINUS
:
9619 /* '++' and '--' may or may not start a cast-expression:
9621 struct T { void operator++(int); };
9622 void f() { (T())++; }
9635 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9636 in the order: const_cast, static_cast, reinterpret_cast.
9638 Don't suggest dynamic_cast.
9640 Return the first legal cast kind found, or NULL otherwise. */
9643 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9647 /* Reuse the parser logic by attempting to build the various kinds of
9648 cast, with "complain" disabled.
9649 Identify the first such cast that is valid. */
9651 /* Don't attempt to run such logic within template processing. */
9652 if (processing_template_decl
)
9655 /* First try const_cast. */
9656 trial
= build_const_cast (input_location
, dst_type
, orig_expr
, tf_none
);
9657 if (trial
!= error_mark_node
)
9658 return "const_cast";
9660 /* If that fails, try static_cast. */
9661 trial
= build_static_cast (input_location
, dst_type
, orig_expr
, tf_none
);
9662 if (trial
!= error_mark_node
)
9663 return "static_cast";
9665 /* Finally, try reinterpret_cast. */
9666 trial
= build_reinterpret_cast (input_location
, dst_type
, orig_expr
,
9668 if (trial
!= error_mark_node
)
9669 return "reinterpret_cast";
9671 /* No such cast possible. */
9675 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9676 suggesting how to convert a C-style cast of the form:
9680 to a C++-style cast.
9682 The primary range of RICHLOC is asssumed to be that of the original
9683 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9684 of the parens in the C-style cast. */
9687 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9688 location_t close_paren_loc
, tree orig_expr
,
9691 /* This function is non-trivial, so bail out now if the warning isn't
9692 going to be emitted. */
9693 if (!warn_old_style_cast
)
9696 /* Try to find a legal C++ cast, trying them in order:
9697 const_cast, static_cast, reinterpret_cast. */
9698 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9699 if (!cast_suggestion
)
9702 /* Replace the open paren with "CAST_SUGGESTION<". */
9704 pp_string (&pp
, cast_suggestion
);
9706 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9708 /* Replace the close paren with "> (". */
9709 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9711 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9712 rich_loc
->add_fixit_insert_after (")");
9716 /* Parse a cast-expression.
9720 ( type-id ) cast-expression
9722 ADDRESS_P is true iff the unary-expression is appearing as the
9723 operand of the `&' operator. CAST_P is true if this expression is
9724 the target of a cast.
9726 Returns a representation of the expression. */
9729 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9730 bool decltype_p
, cp_id_kind
* pidk
)
9732 /* If it's a `(', then we might be looking at a cast. */
9733 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9735 tree type
= NULL_TREE
;
9736 cp_expr
expr (NULL_TREE
);
9737 int cast_expression
= 0;
9738 const char *saved_message
;
9740 /* There's no way to know yet whether or not this is a cast.
9741 For example, `(int (3))' is a unary-expression, while `(int)
9742 3' is a cast. So, we resort to parsing tentatively. */
9743 cp_parser_parse_tentatively (parser
);
9744 /* Types may not be defined in a cast. */
9745 saved_message
= parser
->type_definition_forbidden_message
;
9746 parser
->type_definition_forbidden_message
9747 = G_("types may not be defined in casts");
9748 /* Consume the `('. */
9749 matching_parens parens
;
9750 cp_token
*open_paren
= parens
.consume_open (parser
);
9751 location_t open_paren_loc
= open_paren
->location
;
9752 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9754 /* A very tricky bit is that `(struct S) { 3 }' is a
9755 compound-literal (which we permit in C++ as an extension).
9756 But, that construct is not a cast-expression -- it is a
9757 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9758 is legal; if the compound-literal were a cast-expression,
9759 you'd need an extra set of parentheses.) But, if we parse
9760 the type-id, and it happens to be a class-specifier, then we
9761 will commit to the parse at that point, because we cannot
9762 undo the action that is done when creating a new class. So,
9763 then we cannot back up and do a postfix-expression.
9765 Another tricky case is the following (c++/29234):
9767 struct S { void operator () (); };
9774 As a type-id we parse the parenthesized S()() as a function
9775 returning a function, groktypename complains and we cannot
9776 back up in this case either.
9778 Therefore, we scan ahead to the closing `)', and check to see
9779 if the tokens after the `)' can start a cast-expression. Otherwise
9780 we are dealing with an unary-expression, a postfix-expression
9783 Yet another tricky case, in C++11, is the following (c++/54891):
9787 The issue is that usually, besides the case of lambda-expressions,
9788 the parenthesized type-id cannot be followed by '[', and, eg, we
9789 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9790 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9791 we don't commit, we try a cast-expression, then an unary-expression.
9793 Save tokens so that we can put them back. */
9794 cp_lexer_save_tokens (parser
->lexer
);
9796 /* We may be looking at a cast-expression. */
9797 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9798 /*consume_paren=*/true))
9800 = cp_parser_tokens_start_cast_expression (parser
);
9802 /* Roll back the tokens we skipped. */
9803 cp_lexer_rollback_tokens (parser
->lexer
);
9804 /* If we aren't looking at a cast-expression, simulate an error so
9805 that the call to cp_parser_error_occurred below returns true. */
9806 if (!cast_expression
)
9807 cp_parser_simulate_error (parser
);
9810 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9811 parser
->in_type_id_in_expr_p
= true;
9812 /* Look for the type-id. */
9813 type
= cp_parser_type_id (parser
);
9814 /* Look for the closing `)'. */
9815 cp_token
*close_paren
= parens
.require_close (parser
);
9817 close_paren_loc
= close_paren
->location
;
9818 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9821 /* Restore the saved message. */
9822 parser
->type_definition_forbidden_message
= saved_message
;
9824 /* At this point this can only be either a cast or a
9825 parenthesized ctor such as `(T ())' that looks like a cast to
9826 function returning T. */
9827 if (!cp_parser_error_occurred (parser
))
9829 /* Only commit if the cast-expression doesn't start with
9830 '++', '--', or '[' in C++11. */
9831 if (cast_expression
> 0)
9832 cp_parser_commit_to_topmost_tentative_parse (parser
);
9834 expr
= cp_parser_cast_expression (parser
,
9835 /*address_p=*/false,
9837 /*decltype_p=*/false,
9840 if (cp_parser_parse_definitely (parser
))
9842 /* Warn about old-style casts, if so requested. */
9843 if (warn_old_style_cast
9844 && !in_system_header_at (input_location
)
9845 && !VOID_TYPE_P (type
)
9846 && current_lang_name
!= lang_name_c
)
9848 gcc_rich_location
rich_loc (input_location
);
9849 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9851 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9852 "use of old-style cast to %q#T", type
);
9855 /* Only type conversions to integral or enumeration types
9856 can be used in constant-expressions. */
9857 if (!cast_valid_in_integral_constant_expression_p (type
)
9858 && cp_parser_non_integral_constant_expression (parser
,
9860 return error_mark_node
;
9862 /* Perform the cast. */
9866 with start==caret at the open paren, extending to the
9868 location_t cast_loc
= make_location (open_paren_loc
,
9870 expr
.get_finish ());
9871 expr
= build_c_cast (cast_loc
, type
, expr
);
9876 cp_parser_abort_tentative_parse (parser
);
9879 /* If we get here, then it's not a cast, so it must be a
9880 unary-expression. */
9881 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9882 cast_p
, decltype_p
);
9885 /* Parse a binary expression of the general form:
9889 pm-expression .* cast-expression
9890 pm-expression ->* cast-expression
9892 multiplicative-expression:
9894 multiplicative-expression * pm-expression
9895 multiplicative-expression / pm-expression
9896 multiplicative-expression % pm-expression
9898 additive-expression:
9899 multiplicative-expression
9900 additive-expression + multiplicative-expression
9901 additive-expression - multiplicative-expression
9905 shift-expression << additive-expression
9906 shift-expression >> additive-expression
9908 relational-expression:
9910 relational-expression < shift-expression
9911 relational-expression > shift-expression
9912 relational-expression <= shift-expression
9913 relational-expression >= shift-expression
9917 relational-expression:
9918 relational-expression <? shift-expression
9919 relational-expression >? shift-expression
9921 equality-expression:
9922 relational-expression
9923 equality-expression == relational-expression
9924 equality-expression != relational-expression
9928 and-expression & equality-expression
9930 exclusive-or-expression:
9932 exclusive-or-expression ^ and-expression
9934 inclusive-or-expression:
9935 exclusive-or-expression
9936 inclusive-or-expression | exclusive-or-expression
9938 logical-and-expression:
9939 inclusive-or-expression
9940 logical-and-expression && inclusive-or-expression
9942 logical-or-expression:
9943 logical-and-expression
9944 logical-or-expression || logical-and-expression
9946 All these are implemented with a single function like:
9949 simple-cast-expression
9950 binary-expression <token> binary-expression
9952 CAST_P is true if this expression is the target of a cast.
9954 The binops_by_token map is used to get the tree codes for each <token> type.
9955 binary-expressions are associated according to a precedence table. */
9957 #define TOKEN_PRECEDENCE(token) \
9958 (((token->type == CPP_GREATER \
9959 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9960 && !parser->greater_than_is_operator_p) \
9961 ? PREC_NOT_OPERATOR \
9962 : binops_by_token[token->type].prec)
9965 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9966 bool no_toplevel_fold_p
,
9968 enum cp_parser_prec prec
,
9971 cp_parser_expression_stack stack
;
9972 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9973 cp_parser_expression_stack_entry
*disable_warnings_sp
= NULL
;
9974 cp_parser_expression_stack_entry current
;
9977 enum tree_code rhs_type
;
9978 enum cp_parser_prec new_prec
, lookahead_prec
;
9981 /* Parse the first expression. */
9982 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9983 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9984 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9985 cast_p
, decltype_p
, pidk
);
9986 current
.prec
= prec
;
9988 if (cp_parser_error_occurred (parser
))
9989 return error_mark_node
;
9993 /* Get an operator token. */
9994 token
= cp_lexer_peek_token (parser
->lexer
);
9996 if (warn_cxx11_compat
9997 && token
->type
== CPP_RSHIFT
9998 && !parser
->greater_than_is_operator_p
)
10000 if (warning_at (token
->location
, OPT_Wc__11_compat
,
10001 "%<>>%> operator is treated"
10002 " as two right angle brackets in C++11"))
10003 inform (token
->location
,
10004 "suggest parentheses around %<>>%> expression");
10007 new_prec
= TOKEN_PRECEDENCE (token
);
10008 if (new_prec
!= PREC_NOT_OPERATOR
10009 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10010 /* This is a fold-expression; handle it later. */
10011 new_prec
= PREC_NOT_OPERATOR
;
10013 /* Popping an entry off the stack means we completed a subexpression:
10014 - either we found a token which is not an operator (`>' where it is not
10015 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10016 will happen repeatedly;
10017 - or, we found an operator which has lower priority. This is the case
10018 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10019 parsing `3 * 4'. */
10020 if (new_prec
<= current
.prec
)
10029 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
10030 current
.loc
= token
->location
;
10032 /* We used the operator token. */
10033 cp_lexer_consume_token (parser
->lexer
);
10035 /* For "false && x" or "true || x", x will never be executed;
10036 disable warnings while evaluating it. */
10037 if ((current
.tree_type
== TRUTH_ANDIF_EXPR
10038 && cp_fully_fold (current
.lhs
) == truthvalue_false_node
)
10039 || (current
.tree_type
== TRUTH_ORIF_EXPR
10040 && cp_fully_fold (current
.lhs
) == truthvalue_true_node
))
10042 disable_warnings_sp
= sp
;
10043 ++c_inhibit_evaluation_warnings
;
10046 /* Extract another operand. It may be the RHS of this expression
10047 or the LHS of a new, higher priority expression. */
10048 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
10049 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
10050 rhs
= cp_parser_simple_cast_expression (parser
);
10052 /* Get another operator token. Look up its precedence to avoid
10053 building a useless (immediately popped) stack entry for common
10054 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10055 token
= cp_lexer_peek_token (parser
->lexer
);
10056 lookahead_prec
= TOKEN_PRECEDENCE (token
);
10057 if (lookahead_prec
!= PREC_NOT_OPERATOR
10058 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10059 lookahead_prec
= PREC_NOT_OPERATOR
;
10060 if (lookahead_prec
> new_prec
)
10062 /* ... and prepare to parse the RHS of the new, higher priority
10063 expression. Since precedence levels on the stack are
10064 monotonically increasing, we do not have to care about
10065 stack overflows. */
10069 current
.lhs_type
= rhs_type
;
10070 current
.prec
= new_prec
;
10071 new_prec
= lookahead_prec
;
10075 lookahead_prec
= new_prec
;
10076 /* If the stack is not empty, we have parsed into LHS the right side
10077 (`4' in the example above) of an expression we had suspended.
10078 We can use the information on the stack to recover the LHS (`3')
10079 from the stack together with the tree code (`MULT_EXPR'), and
10080 the precedence of the higher level subexpression
10081 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10082 which will be used to actually build the additive expression. */
10084 rhs_type
= current
.lhs_type
;
10089 /* Undo the disabling of warnings done above. */
10090 if (sp
== disable_warnings_sp
)
10092 disable_warnings_sp
= NULL
;
10093 --c_inhibit_evaluation_warnings
;
10096 if (warn_logical_not_paren
10097 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
10098 && current
.lhs_type
== TRUTH_NOT_EXPR
10099 /* Avoid warning for !!x == y. */
10100 && (TREE_CODE (current
.lhs
) != NE_EXPR
10101 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
10102 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
10103 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
10104 /* Avoid warning for !b == y where b is boolean. */
10105 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
10106 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
10107 != BOOLEAN_TYPE
))))
10108 /* Avoid warning for !!b == y where b is boolean. */
10109 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
10110 || TREE_TYPE (current
.lhs
) == NULL_TREE
10111 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
10112 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
10113 current
.lhs
, maybe_constant_value (rhs
));
10117 location_t combined_loc
= make_location (current
.loc
,
10118 current
.lhs
.get_start (),
10119 rhs
.get_finish ());
10121 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10122 ERROR_MARK for everything that is not a binary expression.
10123 This makes warn_about_parentheses miss some warnings that
10124 involve unary operators. For unary expressions we should
10125 pass the correct tree_code unless the unary expression was
10126 surrounded by parentheses.
10128 if (no_toplevel_fold_p
10129 && lookahead_prec
<= current
.prec
10132 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
10133 current
.lhs
= error_mark_node
;
10136 current
.lhs
.maybe_add_location_wrapper ();
10137 rhs
.maybe_add_location_wrapper ();
10139 = build_min (current
.tree_type
,
10140 TREE_CODE_CLASS (current
.tree_type
)
10142 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
10143 current
.lhs
.get_value (), rhs
.get_value ());
10144 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
10149 op_location_t
op_loc (current
.loc
, combined_loc
);
10150 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
10151 current
.lhs
, current
.lhs_type
,
10152 rhs
, rhs_type
, NULL_TREE
, &overload
,
10153 complain_flags (decltype_p
));
10154 /* TODO: build_x_binary_op doesn't always honor the location. */
10155 current
.lhs
.set_location (combined_loc
);
10157 current
.lhs_type
= current
.tree_type
;
10159 /* If the binary operator required the use of an overloaded operator,
10160 then this expression cannot be an integral constant-expression.
10161 An overloaded operator can be used even if both operands are
10162 otherwise permissible in an integral constant-expression if at
10163 least one of the operands is of enumeration type. */
10166 && cp_parser_non_integral_constant_expression (parser
,
10168 return error_mark_node
;
10171 return current
.lhs
;
10175 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
10176 bool no_toplevel_fold_p
,
10177 enum cp_parser_prec prec
,
10180 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
10181 /*decltype*/false, prec
, pidk
);
10184 /* Parse the `? expression : assignment-expression' part of a
10185 conditional-expression. The LOGICAL_OR_EXPR is the
10186 logical-or-expression that started the conditional-expression.
10187 Returns a representation of the entire conditional-expression.
10189 This routine is used by cp_parser_assignment_expression.
10191 ? expression : assignment-expression
10195 ? : assignment-expression */
10198 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
10200 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
10201 cp_expr assignment_expr
;
10202 struct cp_token
*token
;
10203 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10205 /* Consume the `?' token. */
10206 cp_lexer_consume_token (parser
->lexer
);
10207 token
= cp_lexer_peek_token (parser
->lexer
);
10208 if (cp_parser_allow_gnu_extensions_p (parser
)
10209 && token
->type
== CPP_COLON
)
10211 pedwarn (token
->location
, OPT_Wpedantic
,
10212 "ISO C++ does not allow %<?:%> with omitted middle operand");
10213 /* Implicit true clause. */
10215 c_inhibit_evaluation_warnings
+=
10216 folded_logical_or_expr
== truthvalue_true_node
;
10217 warn_for_omitted_condop (token
->location
, logical_or_expr
);
10221 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10222 parser
->colon_corrects_to_scope_p
= false;
10223 /* Parse the expression. */
10224 c_inhibit_evaluation_warnings
+=
10225 folded_logical_or_expr
== truthvalue_false_node
;
10226 expr
= cp_parser_expression (parser
);
10227 c_inhibit_evaluation_warnings
+=
10228 ((folded_logical_or_expr
== truthvalue_true_node
)
10229 - (folded_logical_or_expr
== truthvalue_false_node
));
10230 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10233 /* The next token should be a `:'. */
10234 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
10235 /* Parse the assignment-expression. */
10236 assignment_expr
= cp_parser_assignment_expression (parser
);
10237 c_inhibit_evaluation_warnings
-=
10238 folded_logical_or_expr
== truthvalue_true_node
;
10240 /* Make a location:
10241 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10242 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10243 with the caret at the "?", ranging from the start of
10244 the logical_or_expr to the end of the assignment_expr. */
10245 loc
= make_location (loc
,
10246 logical_or_expr
.get_start (),
10247 assignment_expr
.get_finish ());
10249 /* Build the conditional-expression. */
10250 return build_x_conditional_expr (loc
, logical_or_expr
,
10253 tf_warning_or_error
);
10256 /* Parse an assignment-expression.
10258 assignment-expression:
10259 conditional-expression
10260 logical-or-expression assignment-operator assignment_expression
10264 CAST_P is true if this expression is the target of a cast.
10265 DECLTYPE_P is true if this expression is the operand of decltype.
10267 Returns a representation for the expression. */
10270 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
10271 bool cast_p
, bool decltype_p
)
10275 /* If the next token is the `throw' keyword, then we're looking at
10276 a throw-expression. */
10277 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
10278 expr
= cp_parser_throw_expression (parser
);
10279 /* If the next token is the `co_yield' keyword, then we're looking at
10280 a yield-expression. */
10281 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CO_YIELD
))
10282 expr
= cp_parser_yield_expression (parser
);
10283 /* Otherwise, it must be that we are looking at a
10284 logical-or-expression. */
10287 /* Parse the binary expressions (logical-or-expression). */
10288 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
10290 PREC_NOT_OPERATOR
, pidk
);
10291 /* If the next token is a `?' then we're actually looking at a
10292 conditional-expression. */
10293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10294 return cp_parser_question_colon_clause (parser
, expr
);
10297 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10299 /* If it's an assignment-operator, we're using the second
10301 enum tree_code assignment_operator
10302 = cp_parser_assignment_operator_opt (parser
);
10303 if (assignment_operator
!= ERROR_MARK
)
10305 bool non_constant_p
;
10307 /* Parse the right-hand side of the assignment. */
10308 cp_expr rhs
= cp_parser_initializer_clause (parser
,
10311 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
10312 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10314 /* An assignment may not appear in a
10315 constant-expression. */
10316 if (cp_parser_non_integral_constant_expression (parser
,
10318 return error_mark_node
;
10319 /* Build the assignment expression. Its default
10323 is the location of the '=' token as the
10324 caret, ranging from the start of the lhs to the
10326 loc
= make_location (loc
,
10328 rhs
.get_finish ());
10329 expr
= build_x_modify_expr (loc
, expr
,
10330 assignment_operator
,
10332 complain_flags (decltype_p
));
10333 /* TODO: build_x_modify_expr doesn't honor the location,
10334 so we must set it here. */
10335 expr
.set_location (loc
);
10343 /* Parse an (optional) assignment-operator.
10345 assignment-operator: one of
10346 = *= /= %= += -= >>= <<= &= ^= |=
10350 assignment-operator: one of
10353 If the next token is an assignment operator, the corresponding tree
10354 code is returned, and the token is consumed. For example, for
10355 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10356 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10357 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10358 operator, ERROR_MARK is returned. */
10360 static enum tree_code
10361 cp_parser_assignment_operator_opt (cp_parser
* parser
)
10366 /* Peek at the next token. */
10367 token
= cp_lexer_peek_token (parser
->lexer
);
10369 switch (token
->type
)
10380 op
= TRUNC_DIV_EXPR
;
10384 op
= TRUNC_MOD_EXPR
;
10395 case CPP_RSHIFT_EQ
:
10399 case CPP_LSHIFT_EQ
:
10416 /* Nothing else is an assignment operator. */
10420 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10421 if (op
!= ERROR_MARK
10422 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10425 /* If it was an assignment operator, consume it. */
10426 if (op
!= ERROR_MARK
)
10427 cp_lexer_consume_token (parser
->lexer
);
10432 /* Parse an expression.
10435 assignment-expression
10436 expression , assignment-expression
10438 CAST_P is true if this expression is the target of a cast.
10439 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10440 except possibly parenthesized or on the RHS of a comma (N3276).
10441 WARN_COMMA_P is true if a comma should be diagnosed.
10443 Returns a representation of the expression. */
10446 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
10447 bool cast_p
, bool decltype_p
, bool warn_comma_p
)
10449 cp_expr expression
= NULL_TREE
;
10450 location_t loc
= UNKNOWN_LOCATION
;
10454 cp_expr assignment_expression
;
10456 /* Parse the next assignment-expression. */
10457 assignment_expression
10458 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
10460 /* We don't create a temporary for a call that is the immediate operand
10461 of decltype or on the RHS of a comma. But when we see a comma, we
10462 need to create a temporary for a call on the LHS. */
10463 if (decltype_p
&& !processing_template_decl
10464 && TREE_CODE (assignment_expression
) == CALL_EXPR
10465 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
10466 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10467 assignment_expression
10468 = build_cplus_new (TREE_TYPE (assignment_expression
),
10469 assignment_expression
, tf_warning_or_error
);
10471 /* If this is the first assignment-expression, we can just
10474 expression
= assignment_expression
;
10477 /* Create a location with caret at the comma, ranging
10478 from the start of the LHS to the end of the RHS. */
10479 loc
= make_location (loc
,
10480 expression
.get_start (),
10481 assignment_expression
.get_finish ());
10482 expression
= build_x_compound_expr (loc
, expression
,
10483 assignment_expression
, NULL_TREE
,
10484 complain_flags (decltype_p
));
10485 expression
.set_location (loc
);
10487 /* If the next token is not a comma, or we're in a fold-expression, then
10488 we are done with the expression. */
10489 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
10490 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10492 /* Consume the `,'. */
10493 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10496 /* [depr.comma.subscript]: A comma expression appearing as
10497 the expr-or-braced-init-list of a subscripting expression
10498 is deprecated. A parenthesized comma expression is not
10500 warning_at (loc
, OPT_Wcomma_subscript
,
10501 "top-level comma expression in array subscript "
10503 warn_comma_p
= false;
10505 cp_lexer_consume_token (parser
->lexer
);
10506 /* A comma operator cannot appear in a constant-expression. */
10507 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
10508 expression
= error_mark_node
;
10514 /* Parse a constant-expression.
10516 constant-expression:
10517 conditional-expression
10519 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10520 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10521 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10522 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10523 greater than 1, this isn't really a constant-expression, only a
10524 potentially constant-evaluated expression. If STRICT_P is true,
10525 only parse a conditional-expression, otherwise parse an
10526 assignment-expression. See below for rationale. */
10529 cp_parser_constant_expression (cp_parser
* parser
,
10530 int allow_non_constant_p
,
10531 bool *non_constant_p
,
10534 bool saved_integral_constant_expression_p
;
10535 bool saved_allow_non_integral_constant_expression_p
;
10536 bool saved_non_integral_constant_expression_p
;
10537 cp_expr expression
;
10539 /* It might seem that we could simply parse the
10540 conditional-expression, and then check to see if it were
10541 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10542 one that the compiler can figure out is constant, possibly after
10543 doing some simplifications or optimizations. The standard has a
10544 precise definition of constant-expression, and we must honor
10545 that, even though it is somewhat more restrictive.
10551 is not a legal declaration, because `(2, 3)' is not a
10552 constant-expression. The `,' operator is forbidden in a
10553 constant-expression. However, GCC's constant-folding machinery
10554 will fold this operation to an INTEGER_CST for `3'. */
10556 /* Save the old settings. */
10557 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
10558 saved_allow_non_integral_constant_expression_p
10559 = parser
->allow_non_integral_constant_expression_p
;
10560 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
10561 /* We are now parsing a constant-expression. */
10562 parser
->integral_constant_expression_p
= true;
10563 parser
->allow_non_integral_constant_expression_p
10564 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
10565 parser
->non_integral_constant_expression_p
= false;
10567 /* A manifestly constant-evaluated expression is evaluated even in an
10568 unevaluated operand. */
10569 cp_evaluated
ev (/*reset if*/allow_non_constant_p
<= 1);
10571 /* Although the grammar says "conditional-expression", when not STRICT_P,
10572 we parse an "assignment-expression", which also permits
10573 "throw-expression" and the use of assignment operators. In the case
10574 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10575 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10576 actually essential that we look for an assignment-expression.
10577 For example, cp_parser_initializer_clauses uses this function to
10578 determine whether a particular assignment-expression is in fact
10582 /* Parse the binary expressions (logical-or-expression). */
10583 expression
= cp_parser_binary_expression (parser
, false, false, false,
10584 PREC_NOT_OPERATOR
, NULL
);
10585 /* If the next token is a `?' then we're actually looking at
10586 a conditional-expression; otherwise we're done. */
10587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10588 expression
= cp_parser_question_colon_clause (parser
, expression
);
10591 expression
= cp_parser_assignment_expression (parser
);
10592 /* Restore the old settings. */
10593 parser
->integral_constant_expression_p
10594 = saved_integral_constant_expression_p
;
10595 parser
->allow_non_integral_constant_expression_p
10596 = saved_allow_non_integral_constant_expression_p
;
10597 if (cxx_dialect
>= cxx11
)
10599 /* Require an rvalue constant expression here; that's what our
10600 callers expect. Reference constant expressions are handled
10601 separately in e.g. cp_parser_template_argument. */
10602 tree decay
= expression
;
10603 if (TREE_TYPE (expression
)
10604 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10605 decay
= build_address (expression
);
10606 bool is_const
= is_rvalue_constant_expression (decay
);
10607 parser
->non_integral_constant_expression_p
= !is_const
;
10608 if (!is_const
&& !allow_non_constant_p
)
10609 require_rvalue_constant_expression (decay
);
10611 if (allow_non_constant_p
)
10612 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10613 parser
->non_integral_constant_expression_p
10614 = saved_non_integral_constant_expression_p
;
10619 /* Parse __builtin_offsetof.
10621 offsetof-expression:
10622 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10624 offsetof-member-designator:
10626 | offsetof-member-designator "." id-expression
10627 | offsetof-member-designator "[" expression "]"
10628 | offsetof-member-designator "->" id-expression */
10631 cp_parser_builtin_offsetof (cp_parser
*parser
)
10633 int save_ice_p
, save_non_ice_p
;
10638 location_t finish_loc
;
10640 /* We're about to accept non-integral-constant things, but will
10641 definitely yield an integral constant expression. Save and
10642 restore these values around our local parsing. */
10643 save_ice_p
= parser
->integral_constant_expression_p
;
10644 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10646 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10648 /* Consume the "__builtin_offsetof" token. */
10649 cp_lexer_consume_token (parser
->lexer
);
10650 /* Consume the opening `('. */
10651 matching_parens parens
;
10652 parens
.require_open (parser
);
10653 /* Parse the type-id. */
10654 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10656 const char *saved_message
= parser
->type_definition_forbidden_message
;
10657 parser
->type_definition_forbidden_message
10658 = G_("types may not be defined within %<__builtin_offsetof%>");
10659 type
= cp_parser_type_id (parser
);
10660 parser
->type_definition_forbidden_message
= saved_message
;
10662 /* Look for the `,'. */
10663 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10664 token
= cp_lexer_peek_token (parser
->lexer
);
10666 /* Build the (type *)null that begins the traditional offsetof macro. */
10668 = build_static_cast (input_location
, build_pointer_type (type
),
10669 null_pointer_node
, tf_warning_or_error
);
10671 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10672 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10673 true, &dummy
, token
->location
);
10676 token
= cp_lexer_peek_token (parser
->lexer
);
10677 switch (token
->type
)
10679 case CPP_OPEN_SQUARE
:
10680 /* offsetof-member-designator "[" expression "]" */
10681 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10686 /* offsetof-member-designator "->" identifier */
10687 expr
= grok_array_decl (token
->location
, expr
, integer_zero_node
,
10688 NULL
, tf_warning_or_error
);
10692 /* offsetof-member-designator "." identifier */
10693 cp_lexer_consume_token (parser
->lexer
);
10694 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10695 expr
, true, &dummy
,
10699 case CPP_CLOSE_PAREN
:
10700 /* Consume the ")" token. */
10701 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10702 cp_lexer_consume_token (parser
->lexer
);
10706 /* Error. We know the following require will fail, but
10707 that gives the proper error message. */
10708 parens
.require_close (parser
);
10709 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10710 expr
= error_mark_node
;
10716 /* Make a location of the form:
10717 __builtin_offsetof (struct s, f)
10718 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10719 with caret at the type-id, ranging from the start of the
10720 "_builtin_offsetof" token to the close paren. */
10721 loc
= make_location (loc
, start_loc
, finish_loc
);
10722 /* The result will be an INTEGER_CST, so we need to explicitly
10723 preserve the location. */
10724 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10727 parser
->integral_constant_expression_p
= save_ice_p
;
10728 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10730 expr
= expr
.maybe_add_location_wrapper ();
10734 /* Parse a trait expression.
10736 Returns a representation of the expression, the underlying type
10737 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10740 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10742 cp_trait_kind kind
;
10743 tree type1
, type2
= NULL_TREE
;
10744 bool binary
= false;
10745 bool variadic
= false;
10749 case RID_HAS_NOTHROW_ASSIGN
:
10750 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10752 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10753 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10755 case RID_HAS_NOTHROW_COPY
:
10756 kind
= CPTK_HAS_NOTHROW_COPY
;
10758 case RID_HAS_TRIVIAL_ASSIGN
:
10759 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10761 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10762 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10764 case RID_HAS_TRIVIAL_COPY
:
10765 kind
= CPTK_HAS_TRIVIAL_COPY
;
10767 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10768 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10770 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10771 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10773 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10774 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10776 case RID_IS_ABSTRACT
:
10777 kind
= CPTK_IS_ABSTRACT
;
10779 case RID_IS_AGGREGATE
:
10780 kind
= CPTK_IS_AGGREGATE
;
10782 case RID_IS_BASE_OF
:
10783 kind
= CPTK_IS_BASE_OF
;
10787 kind
= CPTK_IS_CLASS
;
10790 kind
= CPTK_IS_EMPTY
;
10793 kind
= CPTK_IS_ENUM
;
10796 kind
= CPTK_IS_FINAL
;
10798 case RID_IS_LAYOUT_COMPATIBLE
:
10799 kind
= CPTK_IS_LAYOUT_COMPATIBLE
;
10802 case RID_IS_LITERAL_TYPE
:
10803 kind
= CPTK_IS_LITERAL_TYPE
;
10805 case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF
:
10806 kind
= CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF
;
10810 kind
= CPTK_IS_POD
;
10812 case RID_IS_POLYMORPHIC
:
10813 kind
= CPTK_IS_POLYMORPHIC
;
10815 case RID_IS_SAME_AS
:
10816 kind
= CPTK_IS_SAME_AS
;
10819 case RID_IS_STD_LAYOUT
:
10820 kind
= CPTK_IS_STD_LAYOUT
;
10822 case RID_IS_TRIVIAL
:
10823 kind
= CPTK_IS_TRIVIAL
;
10825 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10826 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10829 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10830 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10833 case RID_IS_TRIVIALLY_COPYABLE
:
10834 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10837 kind
= CPTK_IS_UNION
;
10839 case RID_UNDERLYING_TYPE
:
10840 kind
= CPTK_UNDERLYING_TYPE
;
10845 case RID_DIRECT_BASES
:
10846 kind
= CPTK_DIRECT_BASES
;
10848 case RID_IS_ASSIGNABLE
:
10849 kind
= CPTK_IS_ASSIGNABLE
;
10852 case RID_IS_CONSTRUCTIBLE
:
10853 kind
= CPTK_IS_CONSTRUCTIBLE
;
10856 case RID_IS_NOTHROW_ASSIGNABLE
:
10857 kind
= CPTK_IS_NOTHROW_ASSIGNABLE
;
10860 case RID_IS_NOTHROW_CONSTRUCTIBLE
:
10861 kind
= CPTK_IS_NOTHROW_CONSTRUCTIBLE
;
10865 gcc_unreachable ();
10868 /* Get location of initial token. */
10869 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10871 /* Consume the token. */
10872 cp_lexer_consume_token (parser
->lexer
);
10874 matching_parens parens
;
10875 parens
.require_open (parser
);
10878 type_id_in_expr_sentinel
s (parser
);
10879 type1
= cp_parser_type_id (parser
);
10882 if (type1
== error_mark_node
)
10883 return error_mark_node
;
10887 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10890 type_id_in_expr_sentinel
s (parser
);
10891 type2
= cp_parser_type_id (parser
);
10894 if (type2
== error_mark_node
)
10895 return error_mark_node
;
10899 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10901 cp_lexer_consume_token (parser
->lexer
);
10902 tree elt
= cp_parser_type_id (parser
);
10903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10905 cp_lexer_consume_token (parser
->lexer
);
10906 elt
= make_pack_expansion (elt
);
10908 if (elt
== error_mark_node
)
10909 return error_mark_node
;
10910 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10912 type2
= nreverse (type2
);
10915 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10916 parens
.require_close (parser
);
10918 /* Construct a location of the form:
10919 __is_trivially_copyable(_Tp)
10920 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10921 with start == caret, finishing at the close-paren. */
10922 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10924 /* Complete the trait expression, which may mean either processing
10925 the trait expr now or saving it for template instantiation. */
10928 case CPTK_UNDERLYING_TYPE
:
10929 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10931 return cp_expr (finish_bases (type1
, false), trait_loc
);
10932 case CPTK_DIRECT_BASES
:
10933 return cp_expr (finish_bases (type1
, true), trait_loc
);
10935 return finish_trait_expr (trait_loc
, kind
, type1
, type2
);
10939 /* Parse a lambda expression.
10942 lambda-introducer lambda-declarator [opt] compound-statement
10943 lambda-introducer < template-parameter-list > requires-clause [opt]
10944 lambda-declarator [opt] compound-statement
10946 Returns a representation of the expression. */
10949 cp_parser_lambda_expression (cp_parser
* parser
)
10951 tree lambda_expr
= build_lambda_expr ();
10954 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10955 cp_token_position start
= 0;
10957 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10959 if (cxx_dialect
>= cxx20
)
10961 /* C++20 allows lambdas in unevaluated context, but one in the type of a
10962 non-type parameter is nonsensical.
10964 Distinguish a lambda in the parameter type from a lambda in the
10965 default argument by looking at local_variables_forbidden_p, which is
10966 only set in default arguments. */
10967 if (processing_template_parmlist
&& !parser
->local_variables_forbidden_p
)
10969 error_at (token
->location
,
10970 "lambda-expression in template parameter type");
10971 token
->error_reported
= true;
10975 else if (cp_unevaluated_operand
)
10977 if (!token
->error_reported
)
10979 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10980 "lambda-expression in unevaluated context"
10981 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10982 token
->error_reported
= true;
10986 else if (parser
->in_template_argument_list_p
|| processing_template_parmlist
)
10988 if (!token
->error_reported
)
10990 error_at (token
->location
, "lambda-expression in template-argument"
10991 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10992 token
->error_reported
= true;
10997 /* We may be in the middle of deferred access check. Disable
10999 push_deferring_access_checks (dk_no_deferred
);
11001 cp_parser_lambda_introducer (parser
, lambda_expr
);
11002 if (cp_parser_error_occurred (parser
))
11003 return error_mark_node
;
11005 type
= begin_lambda_type (lambda_expr
);
11006 if (type
== error_mark_node
)
11007 return error_mark_node
;
11009 record_lambda_scope (lambda_expr
);
11011 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11012 determine_visibility (TYPE_NAME (type
));
11014 /* Now that we've started the type, add the capture fields for any
11015 explicit captures. */
11016 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
11019 /* Inside the class, surrounding template-parameter-lists do not apply. */
11020 unsigned int saved_num_template_parameter_lists
11021 = parser
->num_template_parameter_lists
;
11022 unsigned char in_statement
= parser
->in_statement
;
11023 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
11024 bool fully_implicit_function_template_p
11025 = parser
->fully_implicit_function_template_p
;
11026 tree implicit_template_parms
= parser
->implicit_template_parms
;
11027 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
11028 bool auto_is_implicit_function_template_parm_p
11029 = parser
->auto_is_implicit_function_template_parm_p
;
11031 parser
->num_template_parameter_lists
= 0;
11032 parser
->in_statement
= 0;
11033 parser
->in_switch_statement_p
= false;
11034 parser
->fully_implicit_function_template_p
= false;
11035 parser
->implicit_template_parms
= 0;
11036 parser
->implicit_template_scope
= 0;
11037 parser
->auto_is_implicit_function_template_parm_p
= false;
11039 /* The body of a lambda in a discarded statement is not discarded. */
11040 bool discarded
= in_discarded_stmt
;
11041 in_discarded_stmt
= 0;
11043 /* Similarly the body of a lambda in immediate function context is not
11044 in immediate function context. */
11045 bool save_in_consteval_if_p
= in_consteval_if_p
;
11046 in_consteval_if_p
= false;
11048 /* By virtue of defining a local class, a lambda expression has access to
11049 the private variables of enclosing classes. */
11051 if (cp_parser_start_tentative_firewall (parser
))
11054 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
11056 if (ok
&& cp_parser_error_occurred (parser
))
11061 cp_parser_lambda_body (parser
, lambda_expr
);
11063 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
11065 if (cp_parser_skip_to_closing_brace (parser
))
11066 cp_lexer_consume_token (parser
->lexer
);
11069 /* The capture list was built up in reverse order; fix that now. */
11070 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
11071 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
11074 maybe_add_lambda_conv_op (type
);
11076 finish_struct (type
, /*attributes=*/NULL_TREE
);
11078 in_consteval_if_p
= save_in_consteval_if_p
;
11079 in_discarded_stmt
= discarded
;
11081 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
11082 parser
->in_statement
= in_statement
;
11083 parser
->in_switch_statement_p
= in_switch_statement_p
;
11084 parser
->fully_implicit_function_template_p
11085 = fully_implicit_function_template_p
;
11086 parser
->implicit_template_parms
= implicit_template_parms
;
11087 parser
->implicit_template_scope
= implicit_template_scope
;
11088 parser
->auto_is_implicit_function_template_parm_p
11089 = auto_is_implicit_function_template_parm_p
;
11092 /* This field is only used during parsing of the lambda. */
11093 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
11095 /* This lambda shouldn't have any proxies left at this point. */
11096 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
11097 /* And now that we're done, push proxies for an enclosing lambda. */
11098 insert_pending_capture_proxies ();
11100 /* Update the lambda expression to a range. */
11101 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
11106 lambda_expr
= build_lambda_object (lambda_expr
);
11108 lambda_expr
= error_mark_node
;
11110 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
11112 pop_deferring_access_checks ();
11114 return lambda_expr
;
11117 /* Parse the beginning of a lambda expression.
11120 [ lambda-capture [opt] ]
11122 LAMBDA_EXPR is the current representation of the lambda expression. */
11125 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
11127 /* Need commas after the first capture. */
11130 /* Eat the leading `['. */
11131 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
11133 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
11135 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
)
11136 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
)
11137 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
11138 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
11139 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11140 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
11142 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
11144 cp_lexer_consume_token (parser
->lexer
);
11147 if (!(at_function_scope_p () || parsing_nsdmi ()))
11148 error ("non-local lambda expression cannot have a capture-default");
11151 hash_set
<tree
, true> ids
;
11152 tree first_capture_id
= NULL_TREE
;
11153 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
11155 cp_token
* capture_token
;
11157 tree capture_init_expr
;
11158 cp_id_kind idk
= CP_ID_KIND_NONE
;
11159 bool explicit_init_p
= false;
11161 enum capture_kind_type
11166 enum capture_kind_type capture_kind
= BY_COPY
;
11168 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
11170 error ("expected end of capture-list");
11177 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
11179 /* Possibly capture `this'. */
11180 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
11182 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11183 if (cxx_dialect
< cxx20
&& pedantic
11184 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
11185 pedwarn (loc
, OPT_Wc__20_extensions
,
11186 "explicit by-copy capture of %<this%> "
11187 "with by-copy capture default only available with "
11188 "%<-std=c++20%> or %<-std=gnu++20%>");
11189 cp_lexer_consume_token (parser
->lexer
);
11190 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
11191 pedwarn (input_location
, 0,
11192 "already captured %qD in lambda expression",
11195 add_capture (lambda_expr
, /*id=*/this_identifier
,
11196 /*initializer=*/finish_this_expr (),
11197 /*by_reference_p=*/true, explicit_init_p
);
11201 /* Possibly capture `*this'. */
11202 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
11203 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
11205 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11206 if (cxx_dialect
< cxx17
)
11207 pedwarn (loc
, OPT_Wc__17_extensions
,
11208 "%<*this%> capture only available with "
11209 "%<-std=c++17%> or %<-std=gnu++17%>");
11210 cp_lexer_consume_token (parser
->lexer
);
11211 cp_lexer_consume_token (parser
->lexer
);
11212 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
11213 pedwarn (input_location
, 0,
11214 "already captured %qD in lambda expression",
11217 add_capture (lambda_expr
, /*id=*/this_identifier
,
11218 /*initializer=*/finish_this_expr (),
11219 /*by_reference_p=*/false, explicit_init_p
);
11223 /* But reject `&this'. */
11224 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
11225 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
11227 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
11228 "%<this%> cannot be captured by reference");
11229 cp_lexer_consume_token (parser
->lexer
);
11230 cp_lexer_consume_token (parser
->lexer
);
11234 /* Remember whether we want to capture as a reference or not. */
11235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
11237 capture_kind
= BY_REFERENCE
;
11238 cp_lexer_consume_token (parser
->lexer
);
11241 bool init_pack_expansion
= false;
11242 location_t ellipsis_loc
= UNKNOWN_LOCATION
;
11243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11245 ellipsis_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11246 if (cxx_dialect
< cxx20
)
11247 pedwarn (ellipsis_loc
, OPT_Wc__20_extensions
,
11248 "pack init-capture only available with "
11249 "%<-std=c++20%> or %<-std=gnu++20%>");
11250 cp_lexer_consume_token (parser
->lexer
);
11251 init_pack_expansion
= true;
11254 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11255 if (init_pack_expansion
&& capture_kind
!= BY_REFERENCE
11256 && cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
11258 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
11259 0, "%<&%> should come before %<...%>");
11260 capture_kind
= BY_REFERENCE
;
11261 cp_lexer_consume_token (parser
->lexer
);
11264 /* Get the identifier. */
11265 capture_token
= cp_lexer_peek_token (parser
->lexer
);
11266 capture_id
= cp_parser_identifier (parser
);
11268 if (capture_id
== error_mark_node
)
11269 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11270 delimiters, but I modified this to stop on unnested ']' as well. It
11271 was already changed to stop on unnested '}', so the
11272 "closing_parenthesis" name is no more misleading with my change. */
11274 cp_parser_skip_to_closing_parenthesis (parser
,
11275 /*recovering=*/true,
11277 /*consume_paren=*/true);
11281 /* Find the initializer for this capture. */
11282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
11283 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
11284 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11286 bool direct
, non_constant
;
11287 /* An explicit initializer exists. */
11288 if (cxx_dialect
< cxx14
)
11289 pedwarn (input_location
, OPT_Wc__14_extensions
,
11290 "lambda capture initializers "
11291 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11292 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
11293 &non_constant
, true);
11294 explicit_init_p
= true;
11295 if (capture_init_expr
== NULL_TREE
)
11297 error ("empty initializer for lambda init-capture");
11298 capture_init_expr
= error_mark_node
;
11300 if (init_pack_expansion
)
11301 capture_init_expr
= make_pack_expansion (capture_init_expr
);
11305 const char* error_msg
;
11307 /* Turn the identifier into an id-expression. */
11309 = cp_parser_lookup_name_simple (parser
, capture_id
,
11310 capture_token
->location
);
11312 if (capture_init_expr
== error_mark_node
)
11314 unqualified_name_lookup_error (capture_id
);
11317 else if (!VAR_P (capture_init_expr
)
11318 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
11320 error_at (capture_token
->location
,
11321 "capture of non-variable %qE",
11322 capture_init_expr
);
11323 if (DECL_P (capture_init_expr
))
11324 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
11325 "%q#D declared here", capture_init_expr
);
11328 if (VAR_P (capture_init_expr
)
11329 && decl_storage_duration (capture_init_expr
) != dk_auto
)
11331 if (pedwarn (capture_token
->location
, 0, "capture of variable "
11332 "%qD with non-automatic storage duration",
11333 capture_init_expr
))
11334 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
11335 "%q#D declared here", capture_init_expr
);
11340 = finish_id_expression
11345 /*integral_constant_expression_p=*/false,
11346 /*allow_non_integral_constant_expression_p=*/false,
11347 /*non_integral_constant_expression_p=*/NULL
,
11348 /*template_p=*/false,
11350 /*address_p=*/false,
11351 /*template_arg_p=*/false,
11353 capture_token
->location
);
11355 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11357 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11358 cp_lexer_consume_token (parser
->lexer
);
11359 capture_init_expr
= make_pack_expansion (capture_init_expr
);
11360 if (init_pack_expansion
)
11362 /* If what follows is an initializer, the second '...' is
11363 invalid. But for cases like [...xs...], the first one
11365 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
11366 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
11367 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11368 ellipsis_loc
= loc
;
11369 error_at (ellipsis_loc
, "too many %<...%> in lambda capture");
11375 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
11376 && !explicit_init_p
)
11378 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
11379 && capture_kind
== BY_COPY
)
11380 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
11381 "of %qD redundant with by-copy capture default",
11383 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
11384 && capture_kind
== BY_REFERENCE
)
11385 pedwarn (capture_token
->location
, 0, "explicit by-reference "
11386 "capture of %qD redundant with by-reference capture "
11387 "default", capture_id
);
11390 /* Check for duplicates.
11391 Optimize for the zero or one explicit captures cases and only create
11392 the hash_set after adding second capture. */
11393 bool found
= false;
11394 if (!ids
.is_empty ())
11395 found
= ids
.add (capture_id
);
11396 else if (first_capture_id
== NULL_TREE
)
11397 first_capture_id
= capture_id
;
11398 else if (capture_id
== first_capture_id
)
11402 ids
.add (first_capture_id
);
11403 ids
.add (capture_id
);
11406 pedwarn (input_location
, 0,
11407 "already captured %qD in lambda expression", capture_id
);
11409 add_capture (lambda_expr
, capture_id
, capture_init_expr
,
11410 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
11413 /* If there is any qualification still in effect, clear it
11414 now; we will be starting fresh with the next capture. */
11415 parser
->scope
= NULL_TREE
;
11416 parser
->qualifying_scope
= NULL_TREE
;
11417 parser
->object_scope
= NULL_TREE
;
11420 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
11423 /* Parse the (optional) middle of a lambda expression.
11426 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11427 lambda-specifiers (C++23)
11430 decl-specifier-seq [opt] noexcept-specifier [opt]
11431 attribute-specifier-seq [opt] trailing-return-type [opt]
11433 LAMBDA_EXPR is the current representation of the lambda expression. */
11436 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
11438 /* 5.1.1.4 of the standard says:
11439 If a lambda-expression does not include a lambda-declarator, it is as if
11440 the lambda-declarator were ().
11441 This means an empty parameter list, no attributes, and no exception
11443 tree param_list
= void_list_node
;
11444 tree std_attrs
= NULL_TREE
;
11445 tree gnu_attrs
= NULL_TREE
;
11446 tree exception_spec
= NULL_TREE
;
11447 tree template_param_list
= NULL_TREE
;
11448 tree tx_qual
= NULL_TREE
;
11449 tree return_type
= NULL_TREE
;
11450 tree trailing_requires_clause
= NULL_TREE
;
11451 bool has_param_list
= false;
11452 location_t omitted_parms_loc
= UNKNOWN_LOCATION
;
11453 cp_decl_specifier_seq lambda_specs
;
11454 clear_decl_specs (&lambda_specs
);
11455 /* A lambda op() is const unless explicitly 'mutable'. */
11456 cp_cv_quals quals
= TYPE_QUAL_CONST
;
11458 /* The template-parameter-list is optional, but must begin with
11459 an opening angle if present. */
11460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
11462 if (cxx_dialect
< cxx14
)
11463 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wc__14_extensions
,
11464 "lambda templates are only available with "
11465 "%<-std=c++14%> or %<-std=gnu++14%>");
11466 else if (pedantic
&& cxx_dialect
< cxx20
)
11467 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wc__20_extensions
,
11468 "lambda templates are only available with "
11469 "%<-std=c++20%> or %<-std=gnu++20%>");
11471 cp_lexer_consume_token (parser
->lexer
);
11473 template_param_list
= cp_parser_template_parameter_list (parser
);
11474 cp_parser_skip_to_end_of_template_parameter_list (parser
);
11476 /* We may have a constrained generic lambda; parse the requires-clause
11477 immediately after the template-parameter-list and combine with any
11478 shorthand constraints present. */
11479 tree dreqs
= cp_parser_requires_clause_opt (parser
, true);
11482 tree reqs
= get_shorthand_constraints (current_template_parms
);
11484 reqs
= combine_constraint_expressions (reqs
, dreqs
);
11485 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
11488 /* We just processed one more parameter list. */
11489 ++parser
->num_template_parameter_lists
;
11492 /* Committee discussion supports allowing attributes here. */
11493 lambda_specs
.attributes
= cp_parser_attributes_opt (parser
);
11495 /* The parameter-declaration-clause is optional (unless
11496 template-parameter-list was given), but must begin with an
11497 opening parenthesis if present. */
11498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
11500 bool is_consteval
= false;
11501 /* For C++20, before parsing the parameter list check if there is
11502 a consteval specifier in the corresponding decl-specifier-seq. */
11503 if (cxx_dialect
>= cxx20
)
11505 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1);
11506 cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_KEYWORD
); n
++)
11508 if (cp_lexer_peek_nth_token (parser
->lexer
, n
)->keyword
11511 is_consteval
= true;
11517 matching_parens parens
;
11518 parens
.consume_open (parser
);
11520 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
11523 current_binding_level
->immediate_fn_ctx_p
= true;
11525 /* Parse parameters. */
11527 = cp_parser_parameter_declaration_clause
11528 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
11530 /* Default arguments shall not be specified in the
11531 parameter-declaration-clause of a lambda-declarator. */
11532 if (pedantic
&& cxx_dialect
< cxx14
)
11533 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
11534 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
11535 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
11536 OPT_Wc__14_extensions
,
11537 "default argument specified for lambda parameter");
11539 parens
.require_close (parser
);
11540 has_param_list
= true;
11542 else if (cxx_dialect
< cxx23
)
11543 omitted_parms_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11545 /* In the decl-specifier-seq of the lambda-declarator, each
11546 decl-specifier shall either be mutable or constexpr. */
11547 int declares_class_or_enum
;
11548 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
11549 cp_parser_decl_specifier_seq (parser
,
11550 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
11551 &lambda_specs
, &declares_class_or_enum
);
11553 if (omitted_parms_loc
&& lambda_specs
.any_specifiers_p
)
11555 pedwarn (omitted_parms_loc
, OPT_Wc__23_extensions
,
11556 "parameter declaration before lambda declaration "
11557 "specifiers only optional with %<-std=c++2b%> or "
11558 "%<-std=gnu++2b%>");
11559 omitted_parms_loc
= UNKNOWN_LOCATION
;
11562 if (lambda_specs
.storage_class
== sc_mutable
)
11564 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
11565 quals
= TYPE_UNQUALIFIED
;
11566 if (lambda_specs
.conflicting_specifiers_p
)
11567 error_at (lambda_specs
.locations
[ds_storage_class
],
11568 "duplicate %<mutable%>");
11571 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
11572 if (omitted_parms_loc
&& tx_qual
)
11574 pedwarn (omitted_parms_loc
, OPT_Wc__23_extensions
,
11575 "parameter declaration before lambda transaction "
11576 "qualifier only optional with %<-std=c++2b%> or "
11577 "%<-std=gnu++2b%>");
11578 omitted_parms_loc
= UNKNOWN_LOCATION
;
11581 /* Parse optional exception specification. */
11583 = cp_parser_exception_specification_opt (parser
, CP_PARSER_FLAGS_NONE
);
11585 if (omitted_parms_loc
&& exception_spec
)
11587 pedwarn (omitted_parms_loc
, OPT_Wc__23_extensions
,
11588 "parameter declaration before lambda exception "
11589 "specification only optional with %<-std=c++2b%> or "
11590 "%<-std=gnu++2b%>");
11591 omitted_parms_loc
= UNKNOWN_LOCATION
;
11594 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11595 attributes that appertain to the function type. */
11596 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
11597 gnu_attrs
= cp_parser_gnu_attributes_opt (parser
);
11599 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11601 /* Parse optional trailing return type. */
11602 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
11604 if (omitted_parms_loc
)
11605 pedwarn (omitted_parms_loc
, OPT_Wc__23_extensions
,
11606 "parameter declaration before lambda trailing "
11607 "return type only optional with %<-std=c++2b%> or "
11608 "%<-std=gnu++2b%>");
11609 cp_lexer_consume_token (parser
->lexer
);
11610 return_type
= cp_parser_trailing_type_id (parser
);
11613 /* Also allow GNU attributes at the very end of the declaration, the usual
11614 place for GNU attributes. */
11615 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
11616 gnu_attrs
= chainon (gnu_attrs
, cp_parser_gnu_attributes_opt (parser
));
11618 if (has_param_list
)
11620 /* Parse optional trailing requires clause. */
11621 trailing_requires_clause
= cp_parser_requires_clause_opt (parser
, false);
11623 /* The function parameters must be in scope all the way until after the
11624 trailing-return-type in case of decltype. */
11625 pop_bindings_and_leave_scope ();
11628 /* Create the function call operator.
11630 Messing with declarators like this is no uglier than building up the
11631 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11634 cp_decl_specifier_seq return_type_specs
;
11635 cp_declarator
* declarator
;
11639 clear_decl_specs (&return_type_specs
);
11640 return_type_specs
.type
= make_auto ();
11642 if (lambda_specs
.locations
[ds_constexpr
])
11644 if (cxx_dialect
>= cxx17
)
11645 return_type_specs
.locations
[ds_constexpr
]
11646 = lambda_specs
.locations
[ds_constexpr
];
11648 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
11649 "lambda only available with %<-std=c++17%> or "
11650 "%<-std=gnu++17%>");
11652 if (lambda_specs
.locations
[ds_consteval
])
11653 return_type_specs
.locations
[ds_consteval
]
11654 = lambda_specs
.locations
[ds_consteval
];
11656 p
= obstack_alloc (&declarator_obstack
, 0);
11658 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
11659 LAMBDA_EXPR_LOCATION (lambda_expr
));
11661 declarator
= make_call_declarator (declarator
, param_list
, quals
,
11662 VIRT_SPEC_UNSPECIFIED
,
11667 trailing_requires_clause
,
11669 declarator
->std_attributes
= std_attrs
;
11671 fco
= grokmethod (&return_type_specs
,
11673 chainon (gnu_attrs
, lambda_specs
.attributes
));
11674 if (fco
!= error_mark_node
)
11676 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
11677 DECL_ARTIFICIAL (fco
) = 1;
11678 /* Give the object parameter a different name. */
11679 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
11680 DECL_SET_LAMBDA_FUNCTION (fco
, true);
11682 if (template_param_list
)
11684 fco
= finish_member_template_decl (fco
);
11685 finish_template_decl (template_param_list
);
11686 --parser
->num_template_parameter_lists
;
11688 else if (parser
->fully_implicit_function_template_p
)
11689 fco
= finish_fully_implicit_template (parser
, fco
);
11691 finish_member_declaration (fco
);
11693 obstack_free (&declarator_obstack
, p
);
11695 return (fco
!= error_mark_node
);
11699 /* Parse the body of a lambda expression, which is simply
11703 but which requires special handling.
11704 LAMBDA_EXPR is the current representation of the lambda expression. */
11707 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
11709 bool nested
= (current_function_decl
!= NULL_TREE
);
11710 unsigned char local_variables_forbidden_p
11711 = parser
->local_variables_forbidden_p
;
11712 bool in_function_body
= parser
->in_function_body
;
11714 /* The body of a lambda-expression is not a subexpression of the enclosing
11719 push_function_context ();
11721 /* Still increment function_depth so that we don't GC in the
11722 middle of an expression. */
11725 auto odsd
= make_temp_override (parser
->omp_declare_simd
, NULL
);
11726 auto ord
= make_temp_override (parser
->oacc_routine
, NULL
);
11727 auto oafp
= make_temp_override (parser
->omp_attrs_forbidden_p
, false);
11728 vec
<tree
> omp_privatization_save
;
11729 save_omp_privatization_clauses (omp_privatization_save
);
11730 /* Clear this in case we're in the middle of a default argument. */
11731 parser
->local_variables_forbidden_p
= 0;
11732 parser
->in_function_body
= true;
11735 local_specialization_stack
s (lss_copy
);
11736 tree fco
= lambda_function (lambda_expr
);
11737 tree body
= start_lambda_function (fco
, lambda_expr
);
11739 /* Originally C++11 required us to peek for 'return expr'; and
11740 process it specially here to deduce the return type. N3638
11741 removed the need for that. */
11742 cp_parser_function_body (parser
, false);
11744 finish_lambda_function (body
);
11747 restore_omp_privatization_clauses (omp_privatization_save
);
11748 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
11749 parser
->in_function_body
= in_function_body
;
11751 pop_function_context();
11756 /* Statements [gram.stmt.stmt] */
11758 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11761 add_debug_begin_stmt (location_t loc
)
11763 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
11765 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11766 /* A concept is never expanded normally. */
11769 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11770 SET_EXPR_LOCATION (stmt
, loc
);
11774 struct cp_omp_attribute_data
11776 cp_token_cache
*tokens
;
11777 const c_omp_directive
*dir
;
11778 c_omp_directive_kind kind
;
11781 /* Handle omp::directive and omp::sequence attributes in ATTRS
11782 (if any) at the start of a statement or in attribute-declaration. */
11785 cp_parser_handle_statement_omp_attributes (cp_parser
*parser
, tree attrs
)
11787 if (!flag_openmp
&& !flag_openmp_simd
)
11790 auto_vec
<cp_omp_attribute_data
, 16> vec
;
11794 for (tree
*pa
= &attrs
; *pa
; )
11795 if (get_attribute_namespace (*pa
) == omp_identifier
11796 && is_attribute_p ("directive", get_attribute_name (*pa
)))
11799 for (tree a
= TREE_VALUE (*pa
); a
; a
= TREE_CHAIN (a
))
11801 tree d
= TREE_VALUE (a
);
11802 gcc_assert (TREE_CODE (d
) == DEFERRED_PARSE
);
11803 cp_token
*first
= DEFPARSE_TOKENS (d
)->first
;
11804 cp_token
*last
= DEFPARSE_TOKENS (d
)->last
;
11805 if (parser
->omp_attrs_forbidden_p
)
11807 error_at (first
->location
,
11808 "mixing OpenMP directives with attribute and pragma "
11809 "syntax on the same statement");
11810 parser
->omp_attrs_forbidden_p
= false;
11813 const char *directive
[3] = {};
11814 for (int i
= 0; i
< 3; i
++)
11816 tree id
= NULL_TREE
;
11817 if (first
+ i
== last
)
11819 if (first
[i
].type
== CPP_NAME
)
11820 id
= first
[i
].u
.value
;
11821 else if (first
[i
].type
== CPP_KEYWORD
)
11822 id
= ridpointers
[(int) first
[i
].keyword
];
11825 directive
[i
] = IDENTIFIER_POINTER (id
);
11827 const c_omp_directive
*dir
= NULL
;
11829 dir
= c_omp_categorize_directive (directive
[0], directive
[1],
11833 error_at (first
->location
,
11834 "unknown OpenMP directive name in %<omp::directive%>"
11835 " attribute argument");
11838 c_omp_directive_kind kind
= dir
->kind
;
11839 if (dir
->id
== PRAGMA_OMP_ORDERED
)
11841 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
11843 if (directive
[1] && strcmp (directive
[1], "depend") == 0)
11844 kind
= C_OMP_DIR_STANDALONE
;
11845 else if (first
+ 2 < last
11846 && first
[1].type
== CPP_COMMA
11847 && first
[2].type
== CPP_NAME
11848 && strcmp (IDENTIFIER_POINTER (first
[2].u
.value
),
11850 kind
= C_OMP_DIR_STANDALONE
;
11852 else if (dir
->id
== PRAGMA_OMP_ERROR
)
11854 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
11855 int paren_depth
= 0;
11856 for (int i
= 1; first
+ i
< last
; i
++)
11857 if (first
[i
].type
== CPP_OPEN_PAREN
)
11859 else if (first
[i
].type
== CPP_CLOSE_PAREN
)
11861 else if (paren_depth
== 0
11862 && first
+ i
+ 2 < last
11863 && first
[i
].type
== CPP_NAME
11864 && first
[i
+ 1].type
== CPP_OPEN_PAREN
11865 && first
[i
+ 2].type
== CPP_NAME
11866 && !strcmp (IDENTIFIER_POINTER (first
[i
].u
.value
),
11868 && !strcmp (IDENTIFIER_POINTER (first
[i
11872 kind
= C_OMP_DIR_STANDALONE
;
11876 cp_omp_attribute_data v
= { DEFPARSE_TOKENS (d
), dir
, kind
};
11878 if (flag_openmp
|| dir
->simd
)
11879 tokens
+= (last
- first
) + 1;
11881 cp_omp_attribute_data v
= {};
11883 *pa
= TREE_CHAIN (*pa
);
11886 pa
= &TREE_CHAIN (*pa
);
11892 cp_omp_attribute_data
*v
;
11893 cp_omp_attribute_data
*construct_seen
= nullptr;
11894 cp_omp_attribute_data
*standalone_seen
= nullptr;
11895 cp_omp_attribute_data
*prev_standalone_seen
= nullptr;
11896 FOR_EACH_VEC_ELT (vec
, i
, v
)
11899 if (v
->kind
== C_OMP_DIR_CONSTRUCT
&& !construct_seen
)
11900 construct_seen
= v
;
11901 else if (v
->kind
== C_OMP_DIR_STANDALONE
&& !standalone_seen
)
11902 standalone_seen
= v
;
11906 if (standalone_seen
&& !prev_standalone_seen
)
11908 prev_standalone_seen
= standalone_seen
;
11909 standalone_seen
= nullptr;
11913 if (cnt
> 1 && construct_seen
)
11915 error_at (construct_seen
->tokens
->first
->location
,
11916 "OpenMP construct among %<omp::directive%> attributes"
11917 " requires all %<omp::directive%> attributes on the"
11918 " same statement to be in the same %<omp::sequence%>");
11921 if (cnt
> 1 && standalone_seen
&& prev_standalone_seen
)
11923 error_at (standalone_seen
->tokens
->first
->location
,
11924 "multiple OpenMP standalone directives among"
11925 " %<omp::directive%> attributes must be all within the"
11926 " same %<omp::sequence%>");
11930 if (prev_standalone_seen
)
11931 standalone_seen
= prev_standalone_seen
;
11932 if (standalone_seen
11933 && !cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11935 error_at (standalone_seen
->tokens
->first
->location
,
11936 "standalone OpenMP directives in %<omp::directive%> attribute"
11937 " can only appear on an empty statement");
11940 if (cnt
&& cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
11942 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11943 enum pragma_kind kind
= cp_parser_pragma_kind (token
);
11944 if (kind
>= PRAGMA_OMP__START_
&& kind
<= PRAGMA_OMP__LAST_
)
11946 error_at (token
->location
,
11947 "mixing OpenMP directives with attribute and pragma "
11948 "syntax on the same statement");
11956 cp_lexer
*lexer
= cp_lexer_alloc ();
11957 lexer
->debugging_p
= parser
->lexer
->debugging_p
;
11958 vec_safe_reserve (lexer
->buffer
, tokens
, true);
11959 FOR_EACH_VEC_ELT (vec
, i
, v
)
11963 if (!flag_openmp
&& !v
->dir
->simd
)
11965 cp_token
*first
= v
->tokens
->first
;
11966 cp_token
*last
= v
->tokens
->last
;
11968 tok
.type
= CPP_PRAGMA
;
11969 tok
.keyword
= RID_MAX
;
11970 tok
.u
.value
= build_int_cst (NULL
, v
->dir
->id
);
11971 tok
.location
= first
->location
;
11972 lexer
->buffer
->quick_push (tok
);
11973 while (++first
< last
)
11974 lexer
->buffer
->quick_push (*first
);
11976 tok
.type
= CPP_PRAGMA_EOL
;
11977 tok
.keyword
= RID_MAX
;
11978 tok
.location
= last
->location
;
11979 lexer
->buffer
->quick_push (tok
);
11982 tok
.type
= CPP_EOF
;
11983 tok
.keyword
= RID_MAX
;
11984 tok
.location
= lexer
->buffer
->last ().location
;
11985 lexer
->buffer
->quick_push (tok
);
11986 lexer
->next
= parser
->lexer
;
11987 lexer
->next_token
= lexer
->buffer
->address ();
11988 lexer
->last_token
= lexer
->next_token
11989 + lexer
->buffer
->length ()
11991 lexer
->in_omp_attribute_pragma
= true;
11992 parser
->lexer
= lexer
;
11993 /* Move the current source position to that of the first token in the
11995 cp_lexer_set_source_position_from_token (lexer
->next_token
);
11999 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12000 (if any) at the start or after declaration-id of a declaration. */
12003 cp_parser_handle_directive_omp_attributes (cp_parser
*parser
, tree
*pattrs
,
12004 cp_omp_declare_simd_data
*data
,
12007 if (!flag_openmp
&& !flag_openmp_simd
)
12012 bool variant_p
= false;
12013 location_t loc
= UNKNOWN_LOCATION
;
12014 for (tree pa
= *pattrs
; pa
; pa
= TREE_CHAIN (pa
))
12015 if (get_attribute_namespace (pa
) == omp_identifier
12016 && is_attribute_p ("directive", get_attribute_name (pa
)))
12018 for (tree a
= TREE_VALUE (pa
); a
; a
= TREE_CHAIN (a
))
12020 tree d
= TREE_VALUE (a
);
12021 gcc_assert (TREE_CODE (d
) == DEFERRED_PARSE
);
12022 cp_token
*first
= DEFPARSE_TOKENS (d
)->first
;
12023 cp_token
*last
= DEFPARSE_TOKENS (d
)->last
;
12024 const char *directive
[3] = {};
12025 for (int i
= 0; i
< 3; i
++)
12027 tree id
= NULL_TREE
;
12028 if (first
+ i
== last
)
12030 if (first
[i
].type
== CPP_NAME
)
12031 id
= first
[i
].u
.value
;
12032 else if (first
[i
].type
== CPP_KEYWORD
)
12033 id
= ridpointers
[(int) first
[i
].keyword
];
12036 directive
[i
] = IDENTIFIER_POINTER (id
);
12038 const c_omp_directive
*dir
= NULL
;
12040 dir
= c_omp_categorize_directive (directive
[0], directive
[1],
12044 if (dir
->id
== PRAGMA_OMP_DECLARE
12045 && (strcmp (directive
[1], "simd") == 0
12046 || strcmp (directive
[1], "variant") == 0))
12050 variant_p
= strcmp (directive
[1], "variant") == 0;
12051 loc
= first
->location
;
12053 if (start
&& parser
->omp_declare_simd
&& !bad
)
12055 error_at (first
->location
,
12056 "mixing OpenMP directives with attribute and "
12057 "pragma syntax on the same declaration");
12066 for (tree
*pa
= pattrs
; *pa
; )
12067 if (get_attribute_namespace (*pa
) == omp_identifier
12068 && is_attribute_p ("directive", get_attribute_name (*pa
)))
12069 *pa
= TREE_CHAIN (*pa
);
12071 pa
= &TREE_CHAIN (*pa
);
12077 if (parser
->omp_declare_simd
== NULL
)
12079 data
->error_seen
= false;
12080 data
->fndecl_seen
= false;
12081 data
->variant_p
= variant_p
;
12083 data
->tokens
= vNULL
;
12084 data
->attribs
[0] = NULL
;
12085 data
->attribs
[1] = NULL
;
12086 parser
->omp_declare_simd
= data
;
12088 parser
->omp_declare_simd
->attribs
[!start
] = pattrs
;
12091 /* Parse a statement.
12095 expression-statement
12097 selection-statement
12098 iteration-statement
12100 declaration-statement
12107 attribute-specifier-seq (opt) expression-statement
12108 attribute-specifier-seq (opt) compound-statement
12109 attribute-specifier-seq (opt) selection-statement
12110 attribute-specifier-seq (opt) iteration-statement
12111 attribute-specifier-seq (opt) jump-statement
12112 declaration-statement
12113 attribute-specifier-seq (opt) try-block
12116 expression-statement
12125 IN_COMPOUND is true when the statement is nested inside a
12126 cp_parser_compound_statement; this matters for certain pragmas.
12128 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12129 is a (possibly labeled) if statement which is not enclosed in braces
12130 and has an else clause. This is used to implement -Wparentheses.
12132 CHAIN is a vector of if-else-if conditions. */
12135 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
12136 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
12137 location_t
*loc_after_labels
)
12139 tree statement
, std_attrs
= NULL_TREE
;
12141 location_t statement_location
, attrs_loc
;
12142 bool in_omp_attribute_pragma
= parser
->lexer
->in_omp_attribute_pragma
;
12143 bool has_std_attrs
;
12148 /* There is no statement yet. */
12149 statement
= NULL_TREE
;
12151 saved_token_sentinel
saved_tokens (parser
->lexer
);
12152 token
= cp_lexer_peek_token (parser
->lexer
);
12153 attrs_loc
= token
->location
;
12154 if (c_dialect_objc ())
12155 /* In obj-c++, seeing '[[' might be the either the beginning of
12156 c++11 attributes, or a nested objc-message-expression. So
12157 let's parse the c++11 attributes tentatively. */
12158 cp_parser_parse_tentatively (parser
);
12159 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
12161 attrs_loc
= make_location (attrs_loc
, attrs_loc
, parser
->lexer
);
12162 if (c_dialect_objc ())
12164 if (!cp_parser_parse_definitely (parser
))
12165 std_attrs
= NULL_TREE
;
12167 has_std_attrs
= cp_lexer_peek_token (parser
->lexer
) != token
;
12169 /* Peek at the next token. */
12170 token
= cp_lexer_peek_token (parser
->lexer
);
12171 bool omp_attrs_forbidden_p
;
12172 omp_attrs_forbidden_p
= parser
->omp_attrs_forbidden_p
;
12174 if (std_attrs
&& (flag_openmp
|| flag_openmp_simd
))
12176 bool handle_omp_attribs
= false;
12177 if (token
->type
== CPP_KEYWORD
)
12178 switch (token
->keyword
)
12188 case RID_CO_RETURN
:
12192 case RID_AT_FINALLY
:
12193 case RID_AT_SYNCHRONIZED
:
12196 case RID_TRANSACTION_ATOMIC
:
12197 case RID_TRANSACTION_RELAXED
:
12198 case RID_SYNCHRONIZED
:
12199 case RID_ATOMIC_NOEXCEPT
:
12200 case RID_ATOMIC_CANCEL
:
12201 case RID_TRANSACTION_CANCEL
:
12202 handle_omp_attribs
= true;
12207 else if (token
->type
== CPP_SEMICOLON
12208 || token
->type
== CPP_OPEN_BRACE
12209 || token
->type
== CPP_PRAGMA
)
12210 handle_omp_attribs
= true;
12211 if (handle_omp_attribs
)
12213 std_attrs
= cp_parser_handle_statement_omp_attributes (parser
,
12215 token
= cp_lexer_peek_token (parser
->lexer
);
12218 parser
->omp_attrs_forbidden_p
= false;
12220 /* Remember the location of the first token in the statement. */
12221 cp_token
*statement_token
= token
;
12222 statement_location
= token
->location
;
12223 add_debug_begin_stmt (statement_location
);
12224 /* If this is a keyword, then that will often determine what kind of
12225 statement we have. */
12226 if (token
->type
== CPP_KEYWORD
)
12228 enum rid keyword
= token
->keyword
;
12234 /* Looks like a labeled-statement with a case label.
12235 Parse the label, and then use tail recursion to parse
12237 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
12238 in_compound
= false;
12239 in_omp_attribute_pragma
= parser
->lexer
->in_omp_attribute_pragma
;
12244 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12245 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
12251 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12252 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
12258 case RID_CO_RETURN
:
12260 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12261 statement
= cp_parser_jump_statement (parser
);
12264 /* Objective-C++ exception-handling constructs. */
12267 case RID_AT_FINALLY
:
12268 case RID_AT_SYNCHRONIZED
:
12270 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12271 statement
= cp_parser_objc_statement (parser
);
12275 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12276 statement
= cp_parser_try_block (parser
);
12279 case RID_NAMESPACE
:
12280 /* This must be a namespace alias definition. */
12283 /* Attributes should be parsed as part of the
12284 declaration, so let's un-parse them. */
12285 saved_tokens
.rollback();
12286 std_attrs
= NULL_TREE
;
12288 cp_parser_declaration_statement (parser
);
12291 case RID_TRANSACTION_ATOMIC
:
12292 case RID_TRANSACTION_RELAXED
:
12293 case RID_SYNCHRONIZED
:
12294 case RID_ATOMIC_NOEXCEPT
:
12295 case RID_ATOMIC_CANCEL
:
12296 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12297 statement
= cp_parser_transaction (parser
, token
);
12299 case RID_TRANSACTION_CANCEL
:
12300 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12301 statement
= cp_parser_transaction_cancel (parser
);
12305 /* It might be a keyword like `int' that can start a
12306 declaration-statement. */
12310 else if (token
->type
== CPP_NAME
)
12312 /* If the next token is a `:', then we are looking at a
12313 labeled-statement. */
12314 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12315 if (token
->type
== CPP_COLON
)
12317 /* Looks like a labeled-statement with an ordinary label.
12318 Parse the label, and then use tail recursion to parse
12321 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
12322 in_compound
= false;
12323 in_omp_attribute_pragma
= parser
->lexer
->in_omp_attribute_pragma
;
12327 /* Anything that starts with a `{' must be a compound-statement. */
12328 else if (token
->type
== CPP_OPEN_BRACE
)
12330 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12331 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12333 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12334 a statement all its own. */
12335 else if (token
->type
== CPP_PRAGMA
)
12338 cp_lexer
*lexer
= parser
->lexer
;
12339 bool do_restart
= false;
12340 /* Only certain OpenMP pragmas are attached to statements, and thus
12341 are considered statements themselves. All others are not. In
12342 the context of a compound, accept the pragma as a "statement" and
12343 return so that we can check for a close brace. Otherwise we
12344 require a real statement and must go back and read one. */
12346 cp_parser_pragma (parser
, pragma_compound
, if_p
);
12347 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
12349 if (parser
->lexer
!= lexer
12350 && lexer
->in_omp_attribute_pragma
12351 && (!in_omp_attribute_pragma
|| lexer
->orphan_p
))
12353 if (saved_tokens
.lexer
== lexer
)
12355 if (saved_tokens
.commit
)
12356 cp_lexer_commit_tokens (lexer
);
12357 gcc_assert (lexer
->saved_tokens
.length () == saved_tokens
.len
);
12358 saved_tokens
.lexer
= parser
->lexer
;
12359 saved_tokens
.commit
= false;
12360 saved_tokens
.len
= parser
->lexer
->saved_tokens
.length ();
12362 cp_lexer_destroy (lexer
);
12363 lexer
= parser
->lexer
;
12367 if (parser
->lexer
== lexer
12368 && lexer
->in_omp_attribute_pragma
12369 && !in_omp_attribute_pragma
)
12370 parser
->lexer
->orphan_p
= true;
12373 else if (token
->type
== CPP_EOF
)
12375 cp_parser_error (parser
, "expected statement");
12379 /* Everything else must be a declaration-statement or an
12380 expression-statement. Try for the declaration-statement
12381 first, unless we are looking at a `;', in which case we know that
12382 we have an expression-statement. */
12385 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12388 /* Attributes should be parsed as part of the declaration,
12389 so let's un-parse them. */
12390 saved_tokens
.rollback();
12392 parser
->omp_attrs_forbidden_p
= omp_attrs_forbidden_p
;
12393 cp_parser_parse_tentatively (parser
);
12394 /* Try to parse the declaration-statement. */
12395 cp_parser_declaration_statement (parser
);
12396 parser
->omp_attrs_forbidden_p
= false;
12397 /* If that worked, we're done. */
12398 if (cp_parser_parse_definitely (parser
))
12400 /* It didn't work, restore the post-attribute position. */
12403 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
12404 if (flag_openmp
|| flag_openmp_simd
)
12407 bool handle_omp_attribs
= true;
12408 while (cp_lexer_peek_nth_token (parser
->lexer
, i
)->keyword
12411 switch (cp_lexer_peek_nth_token (parser
->lexer
, i
)->keyword
)
12414 case RID_NAMESPACE
:
12417 case RID_STATIC_ASSERT
:
12418 /* Don't handle OpenMP attribs on keywords that
12419 always start a declaration statement but don't
12420 accept attribute before it and therefore
12421 the tentative cp_parser_declaration_statement
12422 fails to parse because of that. */
12423 handle_omp_attribs
= false;
12429 if (handle_omp_attribs
)
12431 parser
->omp_attrs_forbidden_p
= omp_attrs_forbidden_p
;
12433 = cp_parser_handle_statement_omp_attributes
12434 (parser
, std_attrs
);
12435 parser
->omp_attrs_forbidden_p
= false;
12436 token
= cp_lexer_peek_token (parser
->lexer
);
12437 if (token
->type
== CPP_PRAGMA
)
12443 /* All preceding labels have been parsed at this point. */
12444 if (loc_after_labels
!= NULL
)
12445 *loc_after_labels
= statement_location
;
12447 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
12449 /* Look for an expression-statement instead. */
12450 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
12452 /* Handle [[fallthrough]];. */
12453 if (attribute_fallthrough_p (std_attrs
))
12455 /* The next token after the fallthrough attribute is ';'. */
12456 if (statement
== NULL_TREE
)
12458 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12459 statement
= build_call_expr_internal_loc (statement_location
,
12461 void_type_node
, 0);
12462 finish_expr_stmt (statement
);
12465 warning_at (statement_location
, OPT_Wattributes
,
12466 "%<fallthrough%> attribute not followed by %<;%>");
12467 std_attrs
= NULL_TREE
;
12471 /* Set the line number for the statement. */
12472 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
12473 SET_EXPR_LOCATION (statement
, statement_location
);
12475 /* Allow "[[fallthrough]];", but warn otherwise. */
12476 if (std_attrs
!= NULL_TREE
)
12477 warning_at (attrs_loc
,
12479 "attributes at the beginning of statement are ignored");
12482 /* Append ATTR to attribute list ATTRS. */
12485 attr_chainon (tree attrs
, tree attr
)
12487 if (attrs
== error_mark_node
)
12488 return error_mark_node
;
12489 if (attr
== error_mark_node
)
12490 return error_mark_node
;
12491 return chainon (attrs
, attr
);
12494 /* Parse the label for a labeled-statement, i.e.
12497 case constant-expression :
12501 case constant-expression ... constant-expression : statement
12503 When a label is parsed without errors, the label is added to the
12504 parse tree by the finish_* functions, so this function doesn't
12505 have to return the label. */
12508 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
12511 tree label
= NULL_TREE
;
12512 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12514 /* The next token should be an identifier. */
12515 token
= cp_lexer_peek_token (parser
->lexer
);
12516 if (token
->type
!= CPP_NAME
12517 && token
->type
!= CPP_KEYWORD
)
12519 cp_parser_error (parser
, "expected labeled-statement");
12523 /* Remember whether this case or a user-defined label is allowed to fall
12525 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
12527 parser
->colon_corrects_to_scope_p
= false;
12528 switch (token
->keyword
)
12532 tree expr
, expr_hi
;
12533 cp_token
*ellipsis
;
12535 /* Consume the `case' token. */
12536 cp_lexer_consume_token (parser
->lexer
);
12537 /* Parse the constant-expression. */
12538 expr
= cp_parser_constant_expression (parser
);
12539 if (check_for_bare_parameter_packs (expr
))
12540 expr
= error_mark_node
;
12542 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
12543 if (ellipsis
->type
== CPP_ELLIPSIS
)
12545 /* Consume the `...' token. */
12546 cp_lexer_consume_token (parser
->lexer
);
12547 expr_hi
= cp_parser_constant_expression (parser
);
12548 if (check_for_bare_parameter_packs (expr_hi
))
12549 expr_hi
= error_mark_node
;
12551 /* We don't need to emit warnings here, as the common code
12552 will do this for us. */
12555 expr_hi
= NULL_TREE
;
12557 if (parser
->in_switch_statement_p
)
12559 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
12560 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
12562 label
= CASE_LABEL (l
);
12563 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
12567 error_at (token
->location
,
12568 "case label %qE not within a switch statement",
12574 /* Consume the `default' token. */
12575 cp_lexer_consume_token (parser
->lexer
);
12577 if (parser
->in_switch_statement_p
)
12579 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
12580 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
12582 label
= CASE_LABEL (l
);
12583 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
12587 error_at (token
->location
, "case label not within a switch statement");
12591 /* Anything else must be an ordinary label. */
12592 label
= finish_label_stmt (cp_parser_identifier (parser
));
12593 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
12594 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
12598 /* Require the `:' token. */
12599 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
12601 /* An ordinary label may optionally be followed by attributes.
12602 However, this is only permitted if the attributes are then
12603 followed by a semicolon. This is because, for backward
12604 compatibility, when parsing
12605 lab: __attribute__ ((unused)) int i;
12606 we want the attribute to attach to "i", not "lab". */
12607 if (label
!= NULL_TREE
12608 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
12611 cp_parser_parse_tentatively (parser
);
12612 attrs
= cp_parser_gnu_attributes_opt (parser
);
12613 if (attrs
== NULL_TREE
12614 /* And fallthrough always binds to the expression-statement. */
12615 || attribute_fallthrough_p (attrs
)
12616 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12617 cp_parser_abort_tentative_parse (parser
);
12618 else if (!cp_parser_parse_definitely (parser
))
12621 attributes
= attr_chainon (attributes
, attrs
);
12624 if (attributes
!= NULL_TREE
)
12625 cplus_decl_attributes (&label
, attributes
, 0);
12627 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12630 /* Parse an expression-statement.
12632 expression-statement:
12635 Returns the new EXPR_STMT -- or NULL_TREE if the expression
12636 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12637 indicates whether this expression-statement is part of an
12638 expression statement. */
12641 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
12643 tree statement
= NULL_TREE
;
12644 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12645 location_t loc
= token
->location
;
12647 /* There might be attribute fallthrough. */
12648 tree attr
= cp_parser_gnu_attributes_opt (parser
);
12650 /* If the next token is a ';', then there is no expression
12652 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12654 statement
= cp_parser_expression (parser
);
12655 if (statement
== error_mark_node
12656 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
12658 cp_parser_skip_to_end_of_block_or_statement (parser
);
12659 return error_mark_node
;
12663 /* Handle [[fallthrough]];. */
12664 if (attribute_fallthrough_p (attr
))
12666 /* The next token after the fallthrough attribute is ';'. */
12667 if (statement
== NULL_TREE
)
12668 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12669 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
12670 void_type_node
, 0);
12672 warning_at (loc
, OPT_Wattributes
,
12673 "%<fallthrough%> attribute not followed by %<;%>");
12677 /* Allow "[[fallthrough]];", but warn otherwise. */
12678 if (attr
!= NULL_TREE
)
12679 warning_at (loc
, OPT_Wattributes
,
12680 "attributes at the beginning of statement are ignored");
12682 /* Give a helpful message for "A<T>::type t;" and the like. */
12683 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
12684 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
12686 if (TREE_CODE (statement
) == SCOPE_REF
)
12687 error_at (token
->location
, "need %<typename%> before %qE because "
12688 "%qT is a dependent scope",
12689 statement
, TREE_OPERAND (statement
, 0));
12690 else if (is_overloaded_fn (statement
)
12691 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
12694 tree fn
= get_first_fn (statement
);
12695 error_at (token
->location
,
12696 "%<%T::%D%> names the constructor, not the type",
12697 DECL_CONTEXT (fn
), DECL_NAME (fn
));
12701 /* Consume the final `;'. */
12702 cp_parser_consume_semicolon_at_end_of_statement (parser
);
12704 if (in_statement_expr
12705 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
12706 /* This is the final expression statement of a statement
12708 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
12709 else if (statement
)
12710 statement
= finish_expr_stmt (statement
);
12715 /* Parse a compound-statement.
12717 compound-statement:
12718 { statement-seq [opt] }
12722 compound-statement:
12723 { label-declaration-seq [opt] statement-seq [opt] }
12725 label-declaration-seq:
12727 label-declaration-seq label-declaration
12729 Returns a tree representing the statement. */
12732 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
12733 int bcs_flags
, bool function_body
)
12735 tree compound_stmt
;
12736 matching_braces braces
;
12738 /* Consume the `{'. */
12739 if (!braces
.require_open (parser
))
12740 return error_mark_node
;
12741 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
12742 && !function_body
&& cxx_dialect
< cxx14
)
12743 pedwarn (input_location
, OPT_Wpedantic
,
12744 "compound-statement in %<constexpr%> function");
12745 /* Begin the compound-statement. */
12746 compound_stmt
= begin_compound_stmt (bcs_flags
);
12747 /* If the next keyword is `__label__' we have a label declaration. */
12748 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12749 cp_parser_label_declaration (parser
);
12750 /* Parse an (optional) statement-seq. */
12751 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
12754 maybe_splice_retval_cleanup (compound_stmt
);
12756 /* Consume the `}'. */
12757 braces
.require_close (parser
);
12759 /* Finish the compound-statement. */
12760 finish_compound_stmt (compound_stmt
);
12762 return compound_stmt
;
12765 /* Parse an (optional) statement-seq.
12769 statement-seq [opt] statement */
12772 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
12774 /* Scan statements until there aren't any more. */
12777 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12779 /* If we are looking at a `}', then we have run out of
12780 statements; the same is true if we have reached the end
12781 of file, or have stumbled upon a stray '@end'. */
12782 if (token
->type
== CPP_CLOSE_BRACE
12783 || token
->type
== CPP_EOF
12784 || token
->type
== CPP_PRAGMA_EOL
12785 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
12788 /* If we are in a compound statement and find 'else' then
12789 something went wrong. */
12790 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
12792 if (parser
->in_statement
& IN_IF_STMT
)
12796 token
= cp_lexer_consume_token (parser
->lexer
);
12797 error_at (token
->location
, "%<else%> without a previous %<if%>");
12801 /* Parse the statement. */
12802 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
12806 /* Return true if this is the C++20 version of range-based-for with
12810 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
12814 /* Save tokens so that we can put them back. */
12815 cp_lexer_save_tokens (parser
->lexer
);
12817 /* There has to be an unnested ; followed by an unnested :. */
12818 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
12819 /*recovering=*/false,
12821 /*consume_paren=*/false) != -1)
12824 /* We found the semicolon, eat it now. */
12825 cp_lexer_consume_token (parser
->lexer
);
12827 /* Now look for ':' that is not nested in () or {}. */
12828 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
12829 /*recovering=*/false,
12831 /*consume_paren=*/false) == -1);
12834 /* Roll back the tokens we skipped. */
12835 cp_lexer_rollback_tokens (parser
->lexer
);
12840 /* Return true if we're looking at (init; cond), false otherwise. */
12843 cp_parser_init_statement_p (cp_parser
*parser
)
12845 /* Save tokens so that we can put them back. */
12846 cp_lexer_save_tokens (parser
->lexer
);
12848 /* Look for ';' that is not nested in () or {}. */
12849 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
12850 /*recovering=*/false,
12852 /*consume_paren=*/false);
12854 /* Roll back the tokens we skipped. */
12855 cp_lexer_rollback_tokens (parser
->lexer
);
12860 /* Parse a selection-statement.
12862 selection-statement:
12863 if ( init-statement [opt] condition ) statement
12864 if ( init-statement [opt] condition ) statement else statement
12865 switch ( init-statement [opt] condition ) statement
12867 Returns the new IF_STMT or SWITCH_STMT.
12869 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12870 is a (possibly labeled) if statement which is not enclosed in
12871 braces and has an else clause. This is used to implement
12874 CHAIN is a vector of if-else-if conditions. This is used to implement
12875 -Wduplicated-cond. */
12878 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
12883 token_indent_info guard_tinfo
;
12888 /* Peek at the next token. */
12889 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
12890 guard_tinfo
= get_token_indent_info (token
);
12892 /* See what kind of keyword it is. */
12893 keyword
= token
->keyword
;
12903 if (keyword
== RID_IF
12904 && cp_lexer_next_token_is_keyword (parser
->lexer
,
12908 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
12909 if (cxx_dialect
< cxx17
)
12910 pedwarn (tok
->location
, OPT_Wc__17_extensions
,
12911 "%<if constexpr%> only available with "
12912 "%<-std=c++17%> or %<-std=gnu++17%>");
12915 if (keyword
== RID_IF
&& !cx
)
12917 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12920 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
12921 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2,
12925 cp_lexer_consume_token (parser
->lexer
);
12930 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
12931 if (cxx_dialect
< cxx23
)
12932 pedwarn (tok
->location
, OPT_Wc__23_extensions
,
12933 "%<if consteval%> only available with "
12934 "%<-std=c++2b%> or %<-std=gnu++2b%>");
12936 bool save_in_consteval_if_p
= in_consteval_if_p
;
12937 statement
= begin_if_stmt ();
12938 IF_STMT_CONSTEVAL_P (statement
) = true;
12939 condition
= finish_if_stmt_cond (boolean_false_node
, statement
);
12941 gcc_rich_location
richloc (tok
->location
);
12942 bool non_compound_stmt_p
= false;
12943 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12945 non_compound_stmt_p
= true;
12946 richloc
.add_fixit_insert_after (tok
->location
, "{");
12949 in_consteval_if_p
|= ce
> 0;
12950 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12952 if (non_compound_stmt_p
)
12954 location_t before_loc
12955 = cp_lexer_peek_token (parser
->lexer
)->location
;
12956 richloc
.add_fixit_insert_before (before_loc
, "}");
12957 error_at (&richloc
,
12958 "%<if consteval%> requires compound statement");
12959 non_compound_stmt_p
= false;
12962 finish_then_clause (statement
);
12964 /* If the next token is `else', parse the else-clause. */
12965 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12968 cp_token
*else_tok
= cp_lexer_peek_token (parser
->lexer
);
12969 gcc_rich_location
else_richloc (else_tok
->location
);
12970 guard_tinfo
= get_token_indent_info (else_tok
);
12971 /* Consume the `else' keyword. */
12972 cp_lexer_consume_token (parser
->lexer
);
12974 begin_else_clause (statement
);
12976 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12978 non_compound_stmt_p
= true;
12979 else_richloc
.add_fixit_insert_after (else_tok
->location
,
12983 in_consteval_if_p
= save_in_consteval_if_p
| (ce
< 0);
12984 cp_parser_implicitly_scoped_statement (parser
, NULL
,
12987 if (non_compound_stmt_p
)
12989 location_t before_loc
12990 = cp_lexer_peek_token (parser
->lexer
)->location
;
12991 else_richloc
.add_fixit_insert_before (before_loc
, "}");
12992 error_at (&else_richloc
,
12993 "%<if consteval%> requires compound statement");
12996 finish_else_clause (statement
);
12999 in_consteval_if_p
= save_in_consteval_if_p
;
13002 std::swap (THEN_CLAUSE (statement
), ELSE_CLAUSE (statement
));
13003 if (THEN_CLAUSE (statement
) == NULL_TREE
)
13004 THEN_CLAUSE (statement
) = build_empty_stmt (tok
->location
);
13007 finish_if_stmt (statement
);
13011 /* Look for the `('. */
13012 matching_parens parens
;
13013 if (!parens
.require_open (parser
))
13015 cp_parser_skip_to_end_of_statement (parser
);
13016 return error_mark_node
;
13019 /* Begin the selection-statement. */
13020 if (keyword
== RID_IF
)
13022 statement
= begin_if_stmt ();
13023 IF_STMT_CONSTEXPR_P (statement
) = cx
;
13026 statement
= begin_switch_stmt ();
13028 /* Parse the optional init-statement. */
13029 if (cp_parser_init_statement_p (parser
))
13032 if (cxx_dialect
< cxx17
)
13033 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
13034 OPT_Wc__17_extensions
,
13035 "init-statement in selection statements only available "
13036 "with %<-std=c++17%> or %<-std=gnu++17%>");
13037 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13038 /* A non-empty init-statement can have arbitrary side
13041 cp_parser_init_statement (parser
, &decl
);
13044 /* Parse the condition. */
13045 condition
= cp_parser_condition (parser
);
13046 /* Look for the `)'. */
13047 if (!parens
.require_close (parser
))
13048 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13049 /*consume_paren=*/true);
13051 if (keyword
== RID_IF
)
13054 unsigned char in_statement
;
13056 /* Add the condition. */
13057 condition
= finish_if_stmt_cond (condition
, statement
);
13059 if (warn_duplicated_cond
)
13060 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
13063 /* Parse the then-clause. */
13064 in_statement
= parser
->in_statement
;
13065 parser
->in_statement
|= IN_IF_STMT
;
13067 /* Outside a template, the non-selected branch of a constexpr
13068 if is a 'discarded statement', i.e. unevaluated. */
13069 bool was_discarded
= in_discarded_stmt
;
13070 bool discard_then
= (cx
&& !processing_template_decl
13071 && integer_zerop (condition
));
13074 in_discarded_stmt
= true;
13075 ++c_inhibit_evaluation_warnings
;
13078 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
13081 parser
->in_statement
= in_statement
;
13083 finish_then_clause (statement
);
13087 THEN_CLAUSE (statement
) = NULL_TREE
;
13088 in_discarded_stmt
= was_discarded
;
13089 --c_inhibit_evaluation_warnings
;
13092 /* If the next token is `else', parse the else-clause. */
13093 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13096 bool discard_else
= (cx
&& !processing_template_decl
13097 && integer_nonzerop (condition
));
13100 in_discarded_stmt
= true;
13101 ++c_inhibit_evaluation_warnings
;
13105 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13106 /* Consume the `else' keyword. */
13107 cp_lexer_consume_token (parser
->lexer
);
13108 if (warn_duplicated_cond
)
13110 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13114 /* We've got "if (COND) else if (COND2)". Start
13115 the condition chain and add COND as the first
13117 chain
= new vec
<tree
> ();
13118 if (!CONSTANT_CLASS_P (condition
)
13119 && !TREE_SIDE_EFFECTS (condition
))
13121 /* Wrap it in a NOP_EXPR so that we can set the
13122 location of the condition. */
13123 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
13125 SET_EXPR_LOCATION (e
, token
->location
);
13126 chain
->safe_push (e
);
13129 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
13131 /* This is if-else without subsequent if. Zap the
13132 condition chain; we would have already warned at
13136 begin_else_clause (statement
);
13137 /* Parse the else-clause. */
13138 cp_parser_implicitly_scoped_statement (parser
, NULL
,
13139 guard_tinfo
, chain
);
13141 finish_else_clause (statement
);
13143 /* If we are currently parsing a then-clause, then
13144 IF_P will not be NULL. We set it to true to
13145 indicate that this if statement has an else clause.
13146 This may trigger the Wparentheses warning below
13147 when we get back up to the parent if statement. */
13153 ELSE_CLAUSE (statement
) = NULL_TREE
;
13154 in_discarded_stmt
= was_discarded
;
13155 --c_inhibit_evaluation_warnings
;
13160 /* This if statement does not have an else clause. If
13161 NESTED_IF is true, then the then-clause has an if
13162 statement which does have an else clause. We warn
13163 about the potential ambiguity. */
13165 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
13166 "suggest explicit braces to avoid ambiguous"
13168 if (warn_duplicated_cond
)
13169 /* We don't need the condition chain anymore. */
13173 /* Now we're all done with the if-statement. */
13174 finish_if_stmt (statement
);
13178 bool in_switch_statement_p
;
13179 unsigned char in_statement
;
13181 /* Add the condition. */
13182 finish_switch_cond (condition
, statement
);
13184 /* Parse the body of the switch-statement. */
13185 in_switch_statement_p
= parser
->in_switch_statement_p
;
13186 in_statement
= parser
->in_statement
;
13187 parser
->in_switch_statement_p
= true;
13188 parser
->in_statement
|= IN_SWITCH_STMT
;
13189 cp_parser_implicitly_scoped_statement (parser
, if_p
,
13191 parser
->in_switch_statement_p
= in_switch_statement_p
;
13192 parser
->in_statement
= in_statement
;
13194 /* Now we're all done with the switch-statement. */
13195 finish_switch_stmt (statement
);
13203 cp_parser_error (parser
, "expected selection-statement");
13204 return error_mark_node
;
13208 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13209 If we have seen at least one decl-specifier, and the next token is not
13210 a parenthesis (after "int (" we might be looking at a functional cast)
13211 neither we are dealing with a concept-check expression then we must be
13212 looking at a declaration. */
13215 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
13216 cp_decl_specifier_seq
*decl_specs
)
13218 if (decl_specs
->any_specifiers_p
13219 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
13220 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
13221 && !cp_parser_error_occurred (parser
)
13222 && !(decl_specs
->type
13223 && TREE_CODE (decl_specs
->type
) == TYPE_DECL
13224 && is_constrained_auto (TREE_TYPE (decl_specs
->type
))))
13225 cp_parser_commit_to_tentative_parse (parser
);
13228 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13229 The declarator shall not specify a function or an array. Returns
13230 TRUE if the declarator is valid, FALSE otherwise. */
13233 cp_parser_check_condition_declarator (cp_parser
* parser
,
13234 cp_declarator
*declarator
,
13237 if (declarator
== cp_error_declarator
13238 || function_declarator_p (declarator
)
13239 || declarator
->kind
== cdk_array
)
13241 if (declarator
== cp_error_declarator
)
13242 /* Already complained. */;
13243 else if (declarator
->kind
== cdk_array
)
13244 error_at (loc
, "condition declares an array");
13246 error_at (loc
, "condition declares a function");
13247 if (parser
->fully_implicit_function_template_p
)
13248 abort_fully_implicit_template (parser
);
13249 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
13250 /*or_comma=*/false,
13251 /*consume_paren=*/false);
13258 /* Parse a condition.
13262 type-specifier-seq declarator = initializer-clause
13263 type-specifier-seq declarator braced-init-list
13268 type-specifier-seq declarator asm-specification [opt]
13269 attributes [opt] = assignment-expression
13271 Returns the expression that should be tested. */
13274 cp_parser_condition (cp_parser
* parser
)
13276 cp_decl_specifier_seq type_specifiers
;
13277 const char *saved_message
;
13278 int declares_class_or_enum
;
13280 /* Try the declaration first. */
13281 cp_parser_parse_tentatively (parser
);
13282 /* New types are not allowed in the type-specifier-seq for a
13284 saved_message
= parser
->type_definition_forbidden_message
;
13285 parser
->type_definition_forbidden_message
13286 = G_("types may not be defined in conditions");
13287 /* Parse the type-specifier-seq. */
13288 cp_parser_decl_specifier_seq (parser
,
13289 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
13291 &declares_class_or_enum
);
13292 /* Restore the saved message. */
13293 parser
->type_definition_forbidden_message
= saved_message
;
13295 /* Gather the attributes that were provided with the
13296 decl-specifiers. */
13297 tree prefix_attributes
= type_specifiers
.attributes
;
13299 cp_parser_maybe_commit_to_declaration (parser
, &type_specifiers
);
13301 /* If all is well, we might be looking at a declaration. */
13302 if (!cp_parser_error_occurred (parser
))
13305 tree asm_specification
;
13307 cp_declarator
*declarator
;
13308 tree initializer
= NULL_TREE
;
13309 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13311 /* Parse the declarator. */
13312 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13313 CP_PARSER_FLAGS_NONE
,
13314 /*ctor_dtor_or_conv_p=*/NULL
,
13315 /*parenthesized_p=*/NULL
,
13316 /*member_p=*/false,
13317 /*friend_p=*/false,
13318 /*static_p=*/false);
13319 /* Parse the attributes. */
13320 attributes
= cp_parser_attributes_opt (parser
);
13321 /* Parse the asm-specification. */
13322 asm_specification
= cp_parser_asm_specification_opt (parser
);
13323 /* If the next token is not an `=' or '{', then we might still be
13324 looking at an expression. For example:
13328 looks like a decl-specifier-seq and a declarator -- but then
13329 there is no `=', so this is an expression. */
13330 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13331 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13332 cp_parser_simulate_error (parser
);
13334 /* If we did see an `=' or '{', then we are looking at a declaration
13336 if (cp_parser_parse_definitely (parser
))
13339 bool non_constant_p
= false;
13340 int flags
= LOOKUP_ONLYCONVERTING
;
13342 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
13343 return error_mark_node
;
13345 /* Create the declaration. */
13346 decl
= start_decl (declarator
, &type_specifiers
,
13347 /*initialized_p=*/true,
13348 attributes
, prefix_attributes
,
13351 declarator
->init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13352 /* Parse the initializer. */
13353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13355 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
13356 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
13359 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13361 /* Consume the `='. */
13362 cp_lexer_consume_token (parser
->lexer
);
13363 initializer
= cp_parser_initializer_clause (parser
,
13368 cp_parser_error (parser
, "expected initializer");
13369 initializer
= error_mark_node
;
13371 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
13372 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
13374 /* Process the initializer. */
13375 cp_finish_decl (decl
,
13376 initializer
, !non_constant_p
,
13381 pop_scope (pushed_scope
);
13383 return convert_from_reference (decl
);
13386 /* If we didn't even get past the declarator successfully, we are
13387 definitely not looking at a declaration. */
13389 cp_parser_abort_tentative_parse (parser
);
13391 /* Otherwise, we are looking at an expression. */
13392 return cp_parser_expression (parser
);
13395 /* Parses a for-statement or range-for-statement until the closing ')',
13399 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
13401 tree init
, scope
, decl
;
13404 /* Begin the for-statement. */
13405 scope
= begin_for_scope (&init
);
13407 /* Maybe parse the optional init-statement in a range-based for loop. */
13408 if (cp_parser_range_based_for_with_init_p (parser
)
13409 /* Checked for diagnostic purposes only. */
13410 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13413 cp_parser_init_statement (parser
, &dummy
);
13414 if (cxx_dialect
< cxx20
)
13416 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
13417 OPT_Wc__20_extensions
,
13418 "range-based %<for%> loops with initializer only "
13419 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13420 decl
= error_mark_node
;
13424 /* Parse the initialization. */
13425 is_range_for
= cp_parser_init_statement (parser
, &decl
);
13428 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
13431 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
13435 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
13436 unsigned short unroll
)
13438 /* Normal for loop */
13439 tree condition
= NULL_TREE
;
13440 tree expression
= NULL_TREE
;
13443 stmt
= begin_for_stmt (scope
, init
);
13444 /* The init-statement has already been parsed in
13445 cp_parser_init_statement, so no work is needed here. */
13446 finish_init_stmt (stmt
);
13448 /* If there's a condition, process it. */
13449 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13450 condition
= cp_parser_condition (parser
);
13453 cp_parser_error (parser
, "missing loop condition in loop with "
13454 "%<GCC ivdep%> pragma");
13455 condition
= error_mark_node
;
13459 cp_parser_error (parser
, "missing loop condition in loop with "
13460 "%<GCC unroll%> pragma");
13461 condition
= error_mark_node
;
13463 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
13464 /* Look for the `;'. */
13465 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13467 /* If there's an expression, process it. */
13468 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
13469 expression
= cp_parser_expression (parser
);
13470 finish_for_expr (expression
, stmt
);
13475 /* Tries to parse a range-based for-statement:
13478 decl-specifier-seq declarator : expression
13480 The decl-specifier-seq declarator and the `:' are already parsed by
13481 cp_parser_init_statement. If processing_template_decl it returns a
13482 newly created RANGE_FOR_STMT; if not, it is converted to a
13483 regular FOR_STMT. */
13486 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
13487 bool ivdep
, unsigned short unroll
, bool is_omp
)
13489 tree stmt
, range_expr
;
13490 auto_vec
<cxx_binding
*, 16> bindings
;
13491 auto_vec
<tree
, 16> names
;
13492 tree decomp_first_name
= NULL_TREE
;
13493 unsigned int decomp_cnt
= 0;
13495 /* Get the range declaration momentarily out of the way so that
13496 the range expression doesn't clash with it. */
13497 if (range_decl
!= error_mark_node
)
13499 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
13501 tree v
= DECL_VALUE_EXPR (range_decl
);
13502 /* For decomposition declaration get all of the corresponding
13503 declarations out of the way. */
13504 if (TREE_CODE (v
) == ARRAY_REF
13505 && VAR_P (TREE_OPERAND (v
, 0))
13506 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
13508 tree d
= range_decl
;
13509 range_decl
= TREE_OPERAND (v
, 0);
13510 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
13511 decomp_first_name
= d
;
13512 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
13514 tree name
= DECL_NAME (d
);
13515 names
.safe_push (name
);
13516 bindings
.safe_push (IDENTIFIER_BINDING (name
));
13517 IDENTIFIER_BINDING (name
)
13518 = IDENTIFIER_BINDING (name
)->previous
;
13522 if (names
.is_empty ())
13524 tree name
= DECL_NAME (range_decl
);
13525 names
.safe_push (name
);
13526 bindings
.safe_push (IDENTIFIER_BINDING (name
));
13527 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
13531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13533 bool expr_non_constant_p
;
13534 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
13537 range_expr
= cp_parser_expression (parser
);
13539 /* Put the range declaration(s) back into scope. */
13540 for (unsigned int i
= 0; i
< names
.length (); i
++)
13542 cxx_binding
*binding
= bindings
[i
];
13543 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
13544 IDENTIFIER_BINDING (names
[i
]) = binding
;
13547 /* finish_omp_for has its own code for the following, so just
13548 return the range_expr instead. */
13552 /* If in template, STMT is converted to a normal for-statement
13553 at instantiation. If not, it is done just ahead. */
13554 if (processing_template_decl
)
13556 if (check_for_bare_parameter_packs (range_expr
))
13557 range_expr
= error_mark_node
;
13558 stmt
= begin_range_for_stmt (scope
, init
);
13560 RANGE_FOR_IVDEP (stmt
) = 1;
13562 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
13563 finish_range_for_decl (stmt
, range_decl
, range_expr
);
13564 if (!type_dependent_expression_p (range_expr
)
13565 /* do_auto_deduction doesn't mess with template init-lists. */
13566 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
13567 do_range_for_auto_deduction (range_decl
, range_expr
);
13571 stmt
= begin_for_stmt (scope
, init
);
13572 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
13573 decomp_first_name
, decomp_cnt
, ivdep
,
13579 /* Subroutine of cp_convert_range_for: given the initializer expression,
13580 builds up the range temporary. */
13583 build_range_temp (tree range_expr
)
13585 /* Find out the type deduced by the declaration
13586 `auto &&__range = range_expr'. */
13587 tree auto_node
= make_auto ();
13588 tree range_type
= cp_build_reference_type (auto_node
, true);
13589 range_type
= do_auto_deduction (range_type
, range_expr
, auto_node
);
13591 /* Create the __range variable. */
13592 tree range_temp
= build_decl (input_location
, VAR_DECL
,
13593 for_range__identifier
, range_type
);
13594 TREE_USED (range_temp
) = 1;
13595 DECL_ARTIFICIAL (range_temp
) = 1;
13600 /* Used by cp_parser_range_for in template context: we aren't going to
13601 do a full conversion yet, but we still need to resolve auto in the
13602 type of the for-range-declaration if present. This is basically
13603 a shortcut version of cp_convert_range_for. */
13606 do_range_for_auto_deduction (tree decl
, tree range_expr
)
13608 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
13611 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
13612 range_temp
= convert_from_reference (build_range_temp (range_expr
));
13613 iter_type
= (cp_parser_perform_range_for_lookup
13614 (range_temp
, &begin_dummy
, &end_dummy
));
13617 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
13619 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
13620 RO_UNARY_STAR
, NULL_TREE
,
13621 tf_warning_or_error
);
13622 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
13623 iter_decl
, auto_node
,
13624 tf_warning_or_error
,
13625 adc_variable_type
);
13630 /* Warns when the loop variable should be changed to a reference type to
13631 avoid unnecessary copying. I.e., from
13633 for (const auto x : range)
13635 where range returns a reference, to
13637 for (const auto &x : range)
13639 if this version doesn't make a copy.
13641 This function also warns when the loop variable is initialized with
13642 a value of a different type resulting in a copy:
13645 for (const double &x : arr)
13647 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
13648 This function is never called when processing_template_decl is on. */
13651 warn_for_range_copy (tree decl
, tree expr
)
13653 if (!warn_range_loop_construct
13654 || decl
== error_mark_node
)
13657 location_t loc
= DECL_SOURCE_LOCATION (decl
);
13658 tree type
= TREE_TYPE (decl
);
13660 if (from_macro_expansion_at (loc
))
13663 if (TYPE_REF_P (type
))
13665 if (glvalue_p (expr
) && !ref_conv_binds_directly_p (type
, expr
))
13667 auto_diagnostic_group d
;
13668 if (warning_at (loc
, OPT_Wrange_loop_construct
,
13669 "loop variable %qD of type %qT binds to a temporary "
13670 "constructed from type %qT", decl
, type
,
13673 tree ref
= cp_build_qualified_type (TREE_TYPE (expr
),
13675 ref
= cp_build_reference_type (ref
, /*rval*/false);
13676 inform (loc
, "use non-reference type %qT to make the copy "
13677 "explicit or %qT to prevent copying",
13678 non_reference (type
), ref
);
13683 else if (!CP_TYPE_CONST_P (type
))
13686 /* Since small trivially copyable types are cheap to copy, we suppress the
13687 warning for them. 64B is a common size of a cache line. */
13688 if (TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
13689 || (tree_to_uhwi (TYPE_SIZE_UNIT (type
)) <= 64
13690 && trivially_copyable_p (type
)))
13693 tree rtype
= cp_build_reference_type (type
, /*rval*/false);
13694 /* If we could initialize the reference directly, it wouldn't involve any
13696 if (!ref_conv_binds_directly_p (rtype
, expr
))
13699 auto_diagnostic_group d
;
13700 if (warning_at (loc
, OPT_Wrange_loop_construct
,
13701 "loop variable %qD creates a copy from type %qT",
13704 gcc_rich_location
richloc (loc
);
13705 richloc
.add_fixit_insert_before ("&");
13706 inform (&richloc
, "use reference type to prevent copying");
13710 /* Converts a range-based for-statement into a normal
13711 for-statement, as per the definition.
13713 for (RANGE_DECL : RANGE_EXPR)
13716 should be equivalent to:
13719 auto &&__range = RANGE_EXPR;
13720 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
13724 RANGE_DECL = *__begin;
13729 If RANGE_EXPR is an array:
13730 BEGIN_EXPR = __range
13731 END_EXPR = __range + ARRAY_SIZE(__range)
13732 Else if RANGE_EXPR has a member 'begin' or 'end':
13733 BEGIN_EXPR = __range.begin()
13734 END_EXPR = __range.end()
13736 BEGIN_EXPR = begin(__range)
13737 END_EXPR = end(__range);
13739 If __range has a member 'begin' but not 'end', or vice versa, we must
13740 still use the second alternative (it will surely fail, however).
13741 When calling begin()/end() in the third alternative we must use
13742 argument dependent lookup, but always considering 'std' as an associated
13746 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
13747 tree decomp_first_name
, unsigned int decomp_cnt
,
13748 bool ivdep
, unsigned short unroll
)
13751 tree iter_type
, begin_expr
, end_expr
;
13752 tree condition
, expression
;
13754 range_expr
= mark_lvalue_use (range_expr
);
13756 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
13757 /* If an error happened previously do nothing or else a lot of
13758 unhelpful errors would be issued. */
13759 begin_expr
= end_expr
= iter_type
= error_mark_node
;
13764 if (VAR_P (range_expr
)
13765 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
13766 /* Can't bind a reference to an array of runtime bound. */
13767 range_temp
= range_expr
;
13770 range_temp
= build_range_temp (range_expr
);
13771 pushdecl (range_temp
);
13772 cp_finish_decl (range_temp
, range_expr
,
13773 /*is_constant_init*/false, NULL_TREE
,
13774 LOOKUP_ONLYCONVERTING
);
13775 range_temp
= convert_from_reference (range_temp
);
13777 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
13778 &begin_expr
, &end_expr
);
13781 /* The new for initialization statement. */
13782 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
13784 TREE_USED (begin
) = 1;
13785 DECL_ARTIFICIAL (begin
) = 1;
13787 cp_finish_decl (begin
, begin_expr
,
13788 /*is_constant_init*/false, NULL_TREE
,
13789 LOOKUP_ONLYCONVERTING
);
13791 if (cxx_dialect
>= cxx17
)
13792 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
13793 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
13794 TREE_USED (end
) = 1;
13795 DECL_ARTIFICIAL (end
) = 1;
13797 cp_finish_decl (end
, end_expr
,
13798 /*is_constant_init*/false, NULL_TREE
,
13799 LOOKUP_ONLYCONVERTING
);
13801 finish_init_stmt (statement
);
13803 /* The new for condition. */
13804 condition
= build_x_binary_op (input_location
, NE_EXPR
,
13807 NULL_TREE
, NULL
, tf_warning_or_error
);
13808 finish_for_cond (condition
, statement
, ivdep
, unroll
);
13810 /* The new increment expression. */
13811 expression
= finish_unary_op_expr (input_location
,
13812 PREINCREMENT_EXPR
, begin
,
13813 tf_warning_or_error
);
13814 finish_for_expr (expression
, statement
);
13816 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
13817 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
13819 /* The declaration is initialized with *__begin inside the loop body. */
13820 tree deref_begin
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
13821 NULL_TREE
, tf_warning_or_error
);
13822 cp_finish_decl (range_decl
, deref_begin
,
13823 /*is_constant_init*/false, NULL_TREE
,
13824 LOOKUP_ONLYCONVERTING
);
13825 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
13826 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
13828 warn_for_range_copy (range_decl
, deref_begin
);
13833 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
13834 We need to solve both at the same time because the method used
13835 depends on the existence of members begin or end.
13836 Returns the type deduced for the iterator expression. */
13839 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
13841 if (error_operand_p (range
))
13843 *begin
= *end
= error_mark_node
;
13844 return error_mark_node
;
13847 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
13849 error ("range-based %<for%> expression of type %qT "
13850 "has incomplete type", TREE_TYPE (range
));
13851 *begin
= *end
= error_mark_node
;
13852 return error_mark_node
;
13854 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
13856 /* If RANGE is an array, we will use pointer arithmetic. */
13857 *begin
= decay_conversion (range
, tf_warning_or_error
);
13858 *end
= build_binary_op (input_location
, PLUS_EXPR
,
13860 array_type_nelts_top (TREE_TYPE (range
)),
13862 return TREE_TYPE (*begin
);
13866 /* If it is not an array, we must do a bit of magic. */
13867 tree id_begin
, id_end
;
13868 tree member_begin
, member_end
;
13870 *begin
= *end
= error_mark_node
;
13872 id_begin
= get_identifier ("begin");
13873 id_end
= get_identifier ("end");
13874 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
13875 /*protect=*/2, /*want_type=*/false,
13876 tf_warning_or_error
);
13877 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
13878 /*protect=*/2, /*want_type=*/false,
13879 tf_warning_or_error
);
13881 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
13883 /* Use the member functions. */
13884 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
13885 *end
= cp_parser_range_for_member_function (range
, id_end
);
13889 /* Use global functions with ADL. */
13892 vec_safe_push (vec
, range
);
13894 member_begin
= perform_koenig_lookup (id_begin
, vec
,
13895 tf_warning_or_error
);
13896 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
13897 tf_warning_or_error
);
13898 member_end
= perform_koenig_lookup (id_end
, vec
,
13899 tf_warning_or_error
);
13900 *end
= finish_call_expr (member_end
, &vec
, false, true,
13901 tf_warning_or_error
);
13904 /* Last common checks. */
13905 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
13907 /* If one of the expressions is an error do no more checks. */
13908 *begin
= *end
= error_mark_node
;
13909 return error_mark_node
;
13911 else if (type_dependent_expression_p (*begin
)
13912 || type_dependent_expression_p (*end
))
13913 /* Can happen, when, eg, in a template context, Koenig lookup
13914 can't resolve begin/end (c++/58503). */
13918 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
13919 /* The unqualified type of the __begin and __end temporaries should
13920 be the same, as required by the multiple auto declaration. */
13921 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
13923 if (cxx_dialect
>= cxx17
13924 && (build_x_binary_op (input_location
, NE_EXPR
,
13925 *begin
, ERROR_MARK
,
13927 NULL_TREE
, NULL
, tf_none
)
13928 != error_mark_node
))
13929 /* P0184R0 allows __begin and __end to have different types,
13930 but make sure they are comparable so we can give a better
13933 error ("inconsistent begin/end types in range-based %<for%> "
13934 "statement: %qT and %qT",
13935 TREE_TYPE (*begin
), TREE_TYPE (*end
));
13942 /* Helper function for cp_parser_perform_range_for_lookup.
13943 Builds a tree for RANGE.IDENTIFIER(). */
13946 cp_parser_range_for_member_function (tree range
, tree identifier
)
13950 member
= finish_class_member_access_expr (range
, identifier
,
13951 false, tf_warning_or_error
);
13952 if (member
== error_mark_node
)
13953 return error_mark_node
;
13956 res
= finish_call_expr (member
, &vec
,
13957 /*disallow_virtual=*/false,
13958 /*koenig_p=*/false,
13959 tf_warning_or_error
);
13963 /* Parse an iteration-statement.
13965 iteration-statement:
13966 while ( condition ) statement
13967 do statement while ( expression ) ;
13968 for ( init-statement condition [opt] ; expression [opt] )
13971 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
13974 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
13975 unsigned short unroll
)
13980 unsigned char in_statement
;
13981 token_indent_info guard_tinfo
;
13983 /* Peek at the next token. */
13984 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
13986 return error_mark_node
;
13988 guard_tinfo
= get_token_indent_info (token
);
13990 /* Remember whether or not we are already within an iteration
13992 in_statement
= parser
->in_statement
;
13994 /* See what kind of keyword it is. */
13995 keyword
= token
->keyword
;
14002 /* Begin the while-statement. */
14003 statement
= begin_while_stmt ();
14004 /* Look for the `('. */
14005 matching_parens parens
;
14006 parens
.require_open (parser
);
14007 /* Parse the condition. */
14008 condition
= cp_parser_condition (parser
);
14009 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
14010 /* Look for the `)'. */
14011 parens
.require_close (parser
);
14012 /* Parse the dependent statement. */
14013 parser
->in_statement
= IN_ITERATION_STMT
;
14014 bool prev
= note_iteration_stmt_body_start ();
14015 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
14016 note_iteration_stmt_body_end (prev
);
14017 parser
->in_statement
= in_statement
;
14018 /* We're done with the while-statement. */
14019 finish_while_stmt (statement
);
14027 /* Begin the do-statement. */
14028 statement
= begin_do_stmt ();
14029 /* Parse the body of the do-statement. */
14030 parser
->in_statement
= IN_ITERATION_STMT
;
14031 bool prev
= note_iteration_stmt_body_start ();
14032 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
14033 note_iteration_stmt_body_end (prev
);
14034 parser
->in_statement
= in_statement
;
14035 finish_do_body (statement
);
14036 /* Look for the `while' keyword. */
14037 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
14038 /* Look for the `('. */
14039 matching_parens parens
;
14040 parens
.require_open (parser
);
14041 /* Parse the expression. */
14042 expression
= cp_parser_expression (parser
);
14043 /* We're done with the do-statement. */
14044 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
14045 /* Look for the `)'. */
14046 parens
.require_close (parser
);
14047 /* Look for the `;'. */
14048 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14054 /* Look for the `('. */
14055 matching_parens parens
;
14056 parens
.require_open (parser
);
14058 statement
= cp_parser_for (parser
, ivdep
, unroll
);
14060 /* Look for the `)'. */
14061 parens
.require_close (parser
);
14063 /* Parse the body of the for-statement. */
14064 parser
->in_statement
= IN_ITERATION_STMT
;
14065 bool prev
= note_iteration_stmt_body_start ();
14066 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
14067 note_iteration_stmt_body_end (prev
);
14068 parser
->in_statement
= in_statement
;
14070 /* We're done with the for-statement. */
14071 finish_for_stmt (statement
);
14076 cp_parser_error (parser
, "expected iteration-statement");
14077 statement
= error_mark_node
;
14084 /* Parse an init-statement or the declarator of a range-based-for.
14085 Returns true if a range-based-for declaration is seen.
14088 expression-statement
14090 alias-declaration */
14093 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
14095 /* If the next token is a `;', then we have an empty
14096 expression-statement. Grammatically, this is also a
14097 simple-declaration, but an invalid one, because it does not
14098 declare anything. Therefore, if we did not handle this case
14099 specially, we would issue an error message about an invalid
14101 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14103 bool is_range_for
= false;
14104 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
14106 /* A colon is used in range-based for. */
14107 parser
->colon_corrects_to_scope_p
= false;
14109 /* We're going to speculatively look for a declaration, falling back
14110 to an expression, if necessary. */
14111 cp_parser_parse_tentatively (parser
);
14112 bool expect_semicolon_p
= true;
14113 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
14115 cp_parser_alias_declaration (parser
);
14116 expect_semicolon_p
= false;
14117 if (cxx_dialect
< cxx23
14118 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
14119 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
14120 OPT_Wc__23_extensions
,
14121 "alias-declaration in init-statement only "
14122 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14125 /* Parse the declaration. */
14126 cp_parser_simple_declaration (parser
,
14127 /*function_definition_allowed_p=*/false,
14129 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
14130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14132 /* It is a range-for, consume the ':'. */
14133 cp_lexer_consume_token (parser
->lexer
);
14134 is_range_for
= true;
14135 if (cxx_dialect
< cxx11
)
14136 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
14137 OPT_Wc__11_extensions
,
14138 "range-based %<for%> loops only available with "
14139 "%<-std=c++11%> or %<-std=gnu++11%>");
14141 else if (expect_semicolon_p
)
14142 /* The ';' is not consumed yet because we told
14143 cp_parser_simple_declaration not to. */
14144 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14146 if (cp_parser_parse_definitely (parser
))
14147 return is_range_for
;
14148 /* If the tentative parse failed, then we shall need to look for an
14149 expression-statement. */
14151 /* If we are here, it is an expression-statement. */
14152 cp_parser_expression_statement (parser
, NULL_TREE
);
14156 /* Parse a jump-statement.
14161 return expression [opt] ;
14162 return braced-init-list ;
14163 coroutine-return-statement;
14169 goto * expression ;
14171 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14174 cp_parser_jump_statement (cp_parser
* parser
)
14176 tree statement
= error_mark_node
;
14179 unsigned char in_statement
;
14181 /* Peek at the next token. */
14182 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
14184 return error_mark_node
;
14186 /* See what kind of keyword it is. */
14187 keyword
= token
->keyword
;
14191 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
14192 switch (in_statement
)
14195 error_at (token
->location
, "break statement not within loop or switch");
14198 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
14199 || in_statement
== IN_ITERATION_STMT
);
14200 statement
= finish_break_stmt ();
14201 if (in_statement
== IN_ITERATION_STMT
)
14202 break_maybe_infinite_loop ();
14205 error_at (token
->location
, "invalid exit from OpenMP structured block");
14208 error_at (token
->location
, "break statement used with OpenMP for loop");
14211 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14215 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
14218 error_at (token
->location
, "continue statement not within a loop");
14220 /* Fall through. */
14221 case IN_ITERATION_STMT
:
14223 statement
= finish_continue_stmt ();
14226 error_at (token
->location
, "invalid exit from OpenMP structured block");
14229 gcc_unreachable ();
14231 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14234 case RID_CO_RETURN
:
14238 bool expr_non_constant_p
;
14240 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14242 cp_lexer_set_source_position (parser
->lexer
);
14243 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14244 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14246 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14247 expr
= cp_parser_expression (parser
);
14249 /* If the next token is a `;', then there is no
14252 /* Build the return-statement, check co-return first, since type
14253 deduction is not valid there. */
14254 if (keyword
== RID_CO_RETURN
)
14255 statement
= finish_co_return_stmt (token
->location
, expr
);
14256 else if (FNDECL_USED_AUTO (current_function_decl
) && in_discarded_stmt
)
14257 /* Don't deduce from a discarded return statement. */;
14259 statement
= finish_return_stmt (expr
);
14260 /* Look for the final `;'. */
14261 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14266 if (parser
->in_function_body
14267 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
14268 && cxx_dialect
< cxx23
)
14270 error ("%<goto%> in %<constexpr%> function only available with "
14271 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14272 cp_function_chain
->invalid_constexpr
= true;
14275 /* Create the goto-statement. */
14276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
14278 /* Issue a warning about this use of a GNU extension. */
14279 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
14280 /* Consume the '*' token. */
14281 cp_lexer_consume_token (parser
->lexer
);
14282 /* Parse the dependent expression. */
14283 finish_goto_stmt (cp_parser_expression (parser
));
14286 finish_goto_stmt (cp_parser_identifier (parser
));
14287 /* Look for the final `;'. */
14288 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14292 cp_parser_error (parser
, "expected jump-statement");
14299 /* Parse a declaration-statement.
14301 declaration-statement:
14302 block-declaration */
14305 cp_parser_declaration_statement (cp_parser
* parser
)
14309 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14310 p
= obstack_alloc (&declarator_obstack
, 0);
14312 /* Parse the block-declaration. */
14313 cp_parser_block_declaration (parser
, /*statement_p=*/true);
14315 /* Free any declarators allocated. */
14316 obstack_free (&declarator_obstack
, p
);
14319 /* Some dependent statements (like `if (cond) statement'), are
14320 implicitly in their own scope. In other words, if the statement is
14321 a single statement (as opposed to a compound-statement), it is
14322 none-the-less treated as if it were enclosed in braces. Any
14323 declarations appearing in the dependent statement are out of scope
14324 after control passes that point. This function parses a statement,
14325 but ensures that is in its own scope, even if it is not a
14326 compound-statement.
14328 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14329 is a (possibly labeled) if statement which is not enclosed in
14330 braces and has an else clause. This is used to implement
14333 CHAIN is a vector of if-else-if conditions. This is used to implement
14336 Returns the new statement. */
14339 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
14340 const token_indent_info
&guard_tinfo
,
14344 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14345 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
14346 token_indent_info body_tinfo
14347 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
14352 /* Mark if () ; with a special NOP_EXPR. */
14353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14355 cp_lexer_consume_token (parser
->lexer
);
14356 statement
= add_stmt (build_empty_stmt (body_loc
));
14358 if (guard_tinfo
.keyword
== RID_IF
14359 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
14360 warning_at (body_loc
, OPT_Wempty_body
,
14361 "suggest braces around empty body in an %<if%> statement");
14362 else if (guard_tinfo
.keyword
== RID_ELSE
)
14363 warning_at (body_loc
, OPT_Wempty_body
,
14364 "suggest braces around empty body in an %<else%> statement");
14366 /* if a compound is opened, we simply parse the statement directly. */
14367 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14368 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
14369 /* If the token is not a `{', then we must take special action. */
14372 /* Create a compound-statement. */
14373 statement
= begin_compound_stmt (0);
14374 /* Parse the dependent-statement. */
14375 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
14376 &body_loc_after_labels
);
14377 /* Finish the dummy compound-statement. */
14378 finish_compound_stmt (statement
);
14381 token_indent_info next_tinfo
14382 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
14383 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
14385 if (body_loc_after_labels
!= UNKNOWN_LOCATION
14386 && next_tinfo
.type
!= CPP_SEMICOLON
)
14387 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
14388 guard_tinfo
.location
, guard_tinfo
.keyword
);
14390 /* Return the statement. */
14394 /* For some dependent statements (like `while (cond) statement'), we
14395 have already created a scope. Therefore, even if the dependent
14396 statement is a compound-statement, we do not want to create another
14400 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
14401 const token_indent_info
&guard_tinfo
)
14403 /* If the token is a `{', then we must take special action. */
14404 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
14406 token_indent_info body_tinfo
14407 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
14408 location_t loc_after_labels
= UNKNOWN_LOCATION
;
14410 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
14411 &loc_after_labels
);
14412 token_indent_info next_tinfo
14413 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
14414 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
14416 if (loc_after_labels
!= UNKNOWN_LOCATION
14417 && next_tinfo
.type
!= CPP_SEMICOLON
)
14418 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
14419 guard_tinfo
.location
,
14420 guard_tinfo
.keyword
);
14424 /* Avoid calling cp_parser_compound_statement, so that we
14425 don't create a new scope. Do everything else by hand. */
14426 matching_braces braces
;
14427 braces
.require_open (parser
);
14428 /* If the next keyword is `__label__' we have a label declaration. */
14429 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
14430 cp_parser_label_declaration (parser
);
14431 /* Parse an (optional) statement-seq. */
14432 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
14433 braces
.require_close (parser
);
14439 /* Parse a module-name,
14441 module-name . identifier
14444 Returns a pointer to module object, NULL. */
14446 static module_state
*
14447 cp_parser_module_name (cp_parser
*parser
)
14449 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14450 if (token
->type
== CPP_HEADER_NAME
)
14452 cp_lexer_consume_token (parser
->lexer
);
14454 return get_module (token
->u
.value
);
14457 module_state
*parent
= NULL
;
14458 bool partitioned
= false;
14459 if (token
->type
== CPP_COLON
&& named_module_p ())
14461 partitioned
= true;
14462 cp_lexer_consume_token (parser
->lexer
);
14467 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
)
14469 cp_parser_error (parser
, "expected module-name");
14473 tree name
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14474 parent
= get_module (name
, parent
, partitioned
);
14475 token
= cp_lexer_peek_token (parser
->lexer
);
14476 if (!partitioned
&& token
->type
== CPP_COLON
)
14477 partitioned
= true;
14478 else if (token
->type
!= CPP_DOT
)
14481 cp_lexer_consume_token (parser
->lexer
);
14487 /* Named module-declaration
14488 __module ; PRAGMA_EOL
14489 __module private ; PRAGMA_EOL (unimplemented)
14490 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14493 static module_parse
14494 cp_parser_module_declaration (cp_parser
*parser
, module_parse mp_state
,
14497 /* We're a pseudo pragma. */
14498 parser
->lexer
->in_pragma
= true;
14499 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
14501 if (flag_header_unit
)
14503 error_at (token
->location
,
14504 "module-declaration not permitted in header-unit");
14507 else if (mp_state
== MP_FIRST
&& !exporting
14508 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14510 /* Start global module fragment. */
14511 cp_lexer_consume_token (parser
->lexer
);
14512 module_kind
|= MK_GLOBAL
;
14513 mp_state
= MP_GLOBAL
;
14514 cp_parser_require_pragma_eol (parser
, token
);
14516 else if (!exporting
14517 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
14518 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_PRIVATE
)
14519 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_SEMICOLON
))
14521 cp_lexer_consume_token (parser
->lexer
);
14522 cp_lexer_consume_token (parser
->lexer
);
14523 cp_lexer_consume_token (parser
->lexer
);
14524 cp_parser_require_pragma_eol (parser
, token
);
14526 if (!(mp_state
== MP_PURVIEW
|| mp_state
== MP_PURVIEW_IMPORTS
)
14527 || !module_interface_p () || module_partition_p ())
14528 error_at (token
->location
,
14529 "private module fragment only permitted in purview"
14530 " of module interface or partition");
14533 mp_state
= MP_PRIVATE_IMPORTS
;
14534 sorry_at (token
->location
, "private module fragment");
14537 else if (!(mp_state
== MP_FIRST
|| mp_state
== MP_GLOBAL
))
14539 /* Neither the first declaration, nor in a GMF. */
14540 error_at (token
->location
, "module-declaration only permitted as first"
14541 " declaration, or ending a global module fragment");
14543 cp_parser_skip_to_pragma_eol (parser
, token
);
14547 module_state
*mod
= cp_parser_module_name (parser
);
14548 tree attrs
= cp_parser_attributes_opt (parser
);
14550 mp_state
= MP_PURVIEW_IMPORTS
;
14551 if (!mod
|| !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
14554 declare_module (mod
, token
->location
, exporting
, attrs
, parse_in
);
14555 cp_parser_require_pragma_eol (parser
, token
);
14561 /* Import-declaration
14562 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14565 cp_parser_import_declaration (cp_parser
*parser
, module_parse mp_state
,
14568 /* We're a pseudo pragma. */
14569 parser
->lexer
->in_pragma
= true;
14570 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
14572 if (mp_state
!= MP_PURVIEW_IMPORTS
14573 && mp_state
!= MP_PRIVATE_IMPORTS
14574 && module_purview_p ()
14575 && !global_purview_p ())
14577 error_at (token
->location
, "post-module-declaration"
14578 " imports must be contiguous");
14580 inform (token
->location
, "perhaps insert a line break, or other"
14581 " disambiguation, to prevent this being considered a"
14582 " module control-line");
14584 cp_parser_skip_to_pragma_eol (parser
, token
);
14586 else if (current_scope () != global_namespace
)
14588 error_at (token
->location
, "import-declaration must be at global scope");
14593 module_state
*mod
= cp_parser_module_name (parser
);
14594 tree attrs
= cp_parser_attributes_opt (parser
);
14596 if (!mod
|| !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
14598 cp_parser_require_pragma_eol (parser
, token
);
14600 if (parser
->in_unbraced_linkage_specification_p
)
14601 error_at (token
->location
, "import cannot appear directly in"
14602 " a linkage-specification");
14604 /* Module-purview imports must not be from source inclusion
14606 if (attrs
&& module_purview_p () && !global_purview_p ()
14607 && private_lookup_attribute ("__translated",
14608 strlen ("__translated"), attrs
))
14609 error_at (token
->location
, "post-module-declaration imports"
14610 " must not be include-translated");
14611 else if ((mp_state
== MP_PURVIEW_IMPORTS
14612 || mp_state
== MP_PRIVATE_IMPORTS
)
14613 && !token
->main_source_p
)
14614 error_at (token
->location
, "post-module-declaration imports"
14615 " must not be from header inclusion");
14617 import_module (mod
, token
->location
, exporting
, attrs
, parse_in
);
14621 /* export-declaration.
14624 export { declaration-seq-opt } */
14627 cp_parser_module_export (cp_parser
*parser
)
14629 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
));
14630 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
14632 if (!module_interface_p ())
14633 error_at (token
->location
,
14634 "%qE may only occur after a module interface declaration",
14637 bool braced
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
);
14639 unsigned mk
= module_kind
;
14640 if (module_exporting_p ())
14641 error_at (token
->location
,
14642 "%qE may only occur once in an export declaration",
14644 module_kind
|= MK_EXPORTING
;
14648 cp_ensure_no_omp_declare_simd (parser
);
14649 cp_ensure_no_oacc_routine (parser
);
14651 cp_lexer_consume_token (parser
->lexer
);
14652 cp_parser_declaration_seq_opt (parser
);
14653 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
14657 /* Explicitly check if the next tokens might be a
14658 module-directive line, so we can give a clearer error message
14659 about why the directive will be rejected. */
14660 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID__MODULE
)
14661 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID__IMPORT
)
14662 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID__EXPORT
))
14663 error_at (token
->location
, "%<export%> not part of following"
14664 " module-directive");
14665 cp_parser_declaration (parser
, NULL_TREE
);
14671 /* Declarations [gram.dcl.dcl] */
14673 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
14674 is the top-level declaration sequence. That affects whether we
14675 deal with module-preamble.
14679 declaration-seq declaration */
14682 cp_parser_declaration_seq_opt (cp_parser
* parser
)
14686 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14688 if (token
->type
== CPP_CLOSE_BRACE
14689 || token
->type
== CPP_EOF
)
14692 cp_parser_toplevel_declaration (parser
);
14696 /* Parse a declaration.
14700 function-definition
14701 template-declaration
14702 explicit-instantiation
14703 explicit-specialization
14704 linkage-specification
14705 namespace-definition
14711 (all these are only allowed at the outermost level, check
14712 that semantically, for better diagnostics)
14714 module-export-declaration
14715 module-import-declaration
14721 __extension__ declaration */
14724 cp_parser_declaration (cp_parser
* parser
, tree prefix_attrs
)
14726 int saved_pedantic
;
14728 /* Check for the `__extension__' keyword. */
14729 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
14731 /* Parse the qualified declaration. */
14732 cp_parser_declaration (parser
, prefix_attrs
);
14733 /* Restore the PEDANTIC flag. */
14734 pedantic
= saved_pedantic
;
14739 /* Try to figure out what kind of declaration is present. */
14740 cp_token
*token1
= cp_lexer_peek_token (parser
->lexer
);
14741 cp_token
*token2
= (token1
->type
== CPP_EOF
14742 ? token1
: cp_lexer_peek_nth_token (parser
->lexer
, 2));
14744 if (token1
->type
== CPP_SEMICOLON
)
14746 cp_lexer_consume_token (parser
->lexer
);
14747 /* A declaration consisting of a single semicolon is invalid
14748 * before C++11. Allow it unless we're being pedantic. */
14749 if (cxx_dialect
< cxx11
)
14750 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
14753 else if (cp_lexer_nth_token_is (parser
->lexer
,
14754 cp_parser_skip_std_attribute_spec_seq (parser
,
14758 location_t attrs_loc
= token1
->location
;
14759 tree std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
14761 if (std_attrs
&& (flag_openmp
|| flag_openmp_simd
))
14763 gcc_assert (!parser
->lexer
->in_omp_attribute_pragma
);
14764 std_attrs
= cp_parser_handle_statement_omp_attributes (parser
,
14766 if (parser
->lexer
->in_omp_attribute_pragma
)
14768 cp_lexer
*lexer
= parser
->lexer
;
14769 while (parser
->lexer
->in_omp_attribute_pragma
)
14771 gcc_assert (cp_lexer_next_token_is (parser
->lexer
,
14773 cp_parser_pragma (parser
, pragma_external
, NULL
);
14775 cp_lexer_destroy (lexer
);
14779 if (std_attrs
!= NULL_TREE
&& !attribute_ignored_p (std_attrs
))
14780 warning_at (make_location (attrs_loc
, attrs_loc
, parser
->lexer
),
14781 OPT_Wattributes
, "attribute ignored");
14782 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14783 cp_lexer_consume_token (parser
->lexer
);
14787 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14788 void *p
= obstack_alloc (&declarator_obstack
, 0);
14790 tree attributes
= NULL_TREE
;
14792 /* Conditionally, allow attributes to precede a linkage specification. */
14793 if (token1
->keyword
== RID_ATTRIBUTE
)
14795 cp_lexer_save_tokens (parser
->lexer
);
14796 attributes
= cp_parser_attributes_opt (parser
);
14797 cp_token
*t1
= cp_lexer_peek_token (parser
->lexer
);
14798 cp_token
*t2
= (t1
->type
== CPP_EOF
14799 ? t1
: cp_lexer_peek_nth_token (parser
->lexer
, 2));
14800 if (t1
->keyword
== RID_EXTERN
14801 && cp_parser_is_pure_string_literal (t2
))
14803 cp_lexer_commit_tokens (parser
->lexer
);
14804 /* We might have already been here. */
14805 if (!c_dialect_objc ())
14807 location_t where
= get_finish (t2
->location
);
14808 warning_at (token1
->location
, OPT_Wattributes
, "attributes are"
14809 " not permitted in this position");
14810 where
= linemap_position_for_loc_and_offset (line_table
,
14812 inform (where
, "attributes may be inserted here");
14813 attributes
= NULL_TREE
;
14820 cp_lexer_rollback_tokens (parser
->lexer
);
14821 attributes
= NULL_TREE
;
14824 /* If we already had some attributes, and we've added more, then prepend.
14825 Otherwise attributes just contains any that we just read. */
14829 TREE_CHAIN (prefix_attrs
) = attributes
;
14830 attributes
= prefix_attrs
;
14833 /* If the next token is `extern' and the following token is a string
14834 literal, then we have a linkage specification. */
14835 if (token1
->keyword
== RID_EXTERN
14836 && cp_parser_is_pure_string_literal (token2
))
14837 cp_parser_linkage_specification (parser
, attributes
);
14838 /* If the next token is `template', then we have either a template
14839 declaration, an explicit instantiation, or an explicit
14841 else if (token1
->keyword
== RID_TEMPLATE
)
14843 /* `template <>' indicates a template specialization. */
14844 if (token2
->type
== CPP_LESS
14845 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
14846 cp_parser_explicit_specialization (parser
);
14847 /* `template <' indicates a template declaration. */
14848 else if (token2
->type
== CPP_LESS
)
14849 cp_parser_template_declaration (parser
, /*member_p=*/false);
14850 /* Anything else must be an explicit instantiation. */
14852 cp_parser_explicit_instantiation (parser
);
14854 /* If the next token is `export', it's new-style modules or
14855 old-style template. */
14856 else if (token1
->keyword
== RID_EXPORT
)
14859 cp_parser_template_declaration (parser
, /*member_p=*/false);
14861 cp_parser_module_export (parser
);
14863 else if (token1
->keyword
== RID__EXPORT
14864 || token1
->keyword
== RID__IMPORT
14865 || token1
->keyword
== RID__MODULE
)
14867 bool exporting
= token1
->keyword
== RID__EXPORT
;
14868 cp_token
*next
= exporting
? token2
: token1
;
14870 cp_lexer_consume_token (parser
->lexer
);
14871 if (next
->keyword
== RID__MODULE
)
14872 cp_parser_module_declaration (parser
, MP_NOT_MODULE
, exporting
);
14874 cp_parser_import_declaration (parser
, MP_NOT_MODULE
, exporting
);
14876 /* If the next token is `extern', 'static' or 'inline' and the one
14877 after that is `template', we have a GNU extended explicit
14878 instantiation directive. */
14879 else if (cp_parser_allow_gnu_extensions_p (parser
)
14880 && token2
->keyword
== RID_TEMPLATE
14881 && (token1
->keyword
== RID_EXTERN
14882 || token1
->keyword
== RID_STATIC
14883 || token1
->keyword
== RID_INLINE
))
14884 cp_parser_explicit_instantiation (parser
);
14885 /* If the next token is `namespace', check for a named or unnamed
14886 namespace definition. */
14887 else if (token1
->keyword
== RID_NAMESPACE
14888 && (/* A named namespace definition. */
14889 (token2
->type
== CPP_NAME
14890 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
14892 || (token2
->type
== CPP_OPEN_SQUARE
14893 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
14894 == CPP_OPEN_SQUARE
)
14895 /* An unnamed namespace definition. */
14896 || token2
->type
== CPP_OPEN_BRACE
14897 || token2
->keyword
== RID_ATTRIBUTE
))
14898 cp_parser_namespace_definition (parser
);
14899 /* An inline (associated) namespace definition. */
14900 else if (token2
->keyword
== RID_NAMESPACE
14901 && token1
->keyword
== RID_INLINE
)
14902 cp_parser_namespace_definition (parser
);
14903 /* Objective-C++ declaration/definition. */
14904 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
->keyword
))
14905 cp_parser_objc_declaration (parser
, attributes
);
14906 else if (c_dialect_objc ()
14907 && token1
->keyword
== RID_ATTRIBUTE
14908 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
14909 cp_parser_objc_declaration (parser
, attributes
);
14910 /* At this point we may have a template declared by a concept
14912 else if (flag_concepts
14913 && cp_parser_template_declaration_after_export (parser
,
14914 /*member_p=*/false))
14917 /* Try to parse a block-declaration, or a function-definition. */
14918 cp_parser_block_declaration (parser
, /*statement_p=*/false);
14920 /* Free any declarators allocated. */
14921 obstack_free (&declarator_obstack
, p
);
14924 /* Parse a namespace-scope declaration. */
14927 cp_parser_toplevel_declaration (cp_parser
* parser
)
14929 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14931 if (token
->type
== CPP_PRAGMA
)
14932 /* A top-level declaration can consist solely of a #pragma. A
14933 nested declaration cannot, so this is done here and not in
14934 cp_parser_declaration. (A #pragma at block scope is
14935 handled in cp_parser_statement.) */
14936 cp_parser_pragma (parser
, pragma_external
, NULL
);
14938 /* Parse the declaration itself. */
14939 cp_parser_declaration (parser
, NULL_TREE
);
14942 /* Parse a block-declaration.
14947 namespace-alias-definition
14954 __extension__ block-declaration
14959 static_assert-declaration
14961 If STATEMENT_P is TRUE, then this block-declaration is occurring as
14962 part of a declaration-statement. */
14965 cp_parser_block_declaration (cp_parser
*parser
,
14968 int saved_pedantic
;
14970 /* Check for the `__extension__' keyword. */
14971 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
14973 /* Parse the qualified declaration. */
14974 cp_parser_block_declaration (parser
, statement_p
);
14975 /* Restore the PEDANTIC flag. */
14976 pedantic
= saved_pedantic
;
14981 /* Peek at the next token to figure out which kind of declaration is
14983 cp_token
*token1
= cp_lexer_peek_token (parser
->lexer
);
14986 /* If the next keyword is `asm', we have an asm-definition. */
14987 if (token1
->keyword
== RID_ASM
)
14990 cp_parser_commit_to_tentative_parse (parser
);
14991 cp_parser_asm_definition (parser
);
14993 /* If the next keyword is `namespace', we have a
14994 namespace-alias-definition. */
14995 else if (token1
->keyword
== RID_NAMESPACE
)
14996 cp_parser_namespace_alias_definition (parser
);
14997 /* If the next keyword is `using', we have a
14998 using-declaration, a using-directive, or an alias-declaration. */
14999 else if (token1
->keyword
== RID_USING
)
15004 cp_parser_commit_to_tentative_parse (parser
);
15005 /* If the token after `using' is `namespace', then we have a
15006 using-directive. */
15007 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15008 if (token2
->keyword
== RID_NAMESPACE
)
15009 cp_parser_using_directive (parser
);
15010 else if (token2
->keyword
== RID_ENUM
)
15011 cp_parser_using_enum (parser
);
15012 /* If the second token after 'using' is '=', then we have an
15013 alias-declaration. */
15014 else if (cxx_dialect
>= cxx11
15015 && token2
->type
== CPP_NAME
15016 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
15017 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
15018 cp_parser_alias_declaration (parser
);
15019 /* Otherwise, it's a using-declaration. */
15021 cp_parser_using_declaration (parser
,
15022 /*access_declaration_p=*/false);
15024 /* If the next keyword is `__label__' we have a misplaced label
15026 else if (token1
->keyword
== RID_LABEL
)
15028 cp_lexer_consume_token (parser
->lexer
);
15029 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
15030 cp_parser_skip_to_end_of_statement (parser
);
15031 /* If the next token is now a `;', consume it. */
15032 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15033 cp_lexer_consume_token (parser
->lexer
);
15035 /* If the next token is `static_assert' we have a static assertion. */
15036 else if (token1
->keyword
== RID_STATIC_ASSERT
)
15037 cp_parser_static_assert (parser
, /*member_p=*/false);
15038 /* If the next tokens after attributes is `using namespace', then we have
15039 a using-directive. */
15040 else if ((attr_idx
= cp_parser_skip_std_attribute_spec_seq (parser
, 1)) != 1
15041 && cp_lexer_nth_token_is_keyword (parser
->lexer
, attr_idx
,
15043 && cp_lexer_nth_token_is_keyword (parser
->lexer
, attr_idx
+ 1,
15047 cp_parser_commit_to_tentative_parse (parser
);
15048 cp_parser_using_directive (parser
);
15050 /* Anything else must be a simple-declaration. */
15052 cp_parser_simple_declaration (parser
, !statement_p
,
15053 /*maybe_range_for_decl*/NULL
);
15056 /* Parse a simple-declaration.
15058 simple-declaration:
15059 decl-specifier-seq [opt] init-declarator-list [opt] ;
15060 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15061 brace-or-equal-initializer ;
15063 init-declarator-list:
15065 init-declarator-list , init-declarator
15067 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15068 function-definition as a simple-declaration.
15070 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15071 parsed declaration if it is an uninitialized single declarator not followed
15072 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15073 if present, will not be consumed. */
15076 cp_parser_simple_declaration (cp_parser
* parser
,
15077 bool function_definition_allowed_p
,
15078 tree
*maybe_range_for_decl
)
15080 cp_decl_specifier_seq decl_specifiers
;
15081 int declares_class_or_enum
;
15082 bool saw_declarator
;
15083 location_t comma_loc
= UNKNOWN_LOCATION
;
15084 location_t init_loc
= UNKNOWN_LOCATION
;
15086 if (maybe_range_for_decl
)
15087 *maybe_range_for_decl
= NULL_TREE
;
15089 /* Defer access checks until we know what is being declared; the
15090 checks for names appearing in the decl-specifier-seq should be
15091 done as if we were in the scope of the thing being declared. */
15092 push_deferring_access_checks (dk_deferred
);
15094 /* Parse the decl-specifier-seq. We have to keep track of whether
15095 or not the decl-specifier-seq declares a named class or
15096 enumeration type, since that is the only case in which the
15097 init-declarator-list is allowed to be empty.
15101 In a simple-declaration, the optional init-declarator-list can be
15102 omitted only when declaring a class or enumeration, that is when
15103 the decl-specifier-seq contains either a class-specifier, an
15104 elaborated-type-specifier, or an enum-specifier. */
15105 cp_parser_decl_specifier_seq (parser
,
15106 CP_PARSER_FLAGS_OPTIONAL
,
15108 &declares_class_or_enum
);
15109 /* We no longer need to defer access checks. */
15110 stop_deferring_access_checks ();
15112 cp_omp_declare_simd_data odsd
;
15113 if (decl_specifiers
.attributes
&& (flag_openmp
|| flag_openmp_simd
))
15114 cp_parser_handle_directive_omp_attributes (parser
,
15115 &decl_specifiers
.attributes
,
15118 /* In a block scope, a valid declaration must always have a
15119 decl-specifier-seq. By not trying to parse declarators, we can
15120 resolve the declaration/expression ambiguity more quickly. */
15121 if (!function_definition_allowed_p
15122 && !decl_specifiers
.any_specifiers_p
)
15124 cp_parser_error (parser
, "expected declaration");
15128 /* If the next two tokens are both identifiers, the code is
15129 erroneous. The usual cause of this situation is code like:
15133 where "T" should name a type -- but does not. */
15134 if (!decl_specifiers
.any_type_specifiers_p
15135 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
15137 /* If parsing tentatively, we should commit; we really are
15138 looking at a declaration. */
15139 cp_parser_commit_to_tentative_parse (parser
);
15144 cp_parser_maybe_commit_to_declaration (parser
, &decl_specifiers
);
15146 /* Look for C++17 decomposition declaration. */
15147 for (size_t n
= 1; ; n
++)
15148 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
15149 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
15151 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
15152 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
15153 && decl_specifiers
.any_specifiers_p
)
15156 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
15157 maybe_range_for_decl
,
15160 /* The next token should be either a `,' or a `;'. */
15161 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15162 /* If it's a `;', we are done. */
15163 if (token
->type
== CPP_SEMICOLON
)
15165 else if (maybe_range_for_decl
)
15167 if (*maybe_range_for_decl
== NULL_TREE
)
15168 *maybe_range_for_decl
= error_mark_node
;
15171 /* Anything else is an error. */
15174 /* If we have already issued an error message we don't need
15175 to issue another one. */
15176 if ((decl
!= error_mark_node
15177 && DECL_INITIAL (decl
) != error_mark_node
)
15178 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
15179 cp_parser_error (parser
, "expected %<;%>");
15180 /* Skip tokens until we reach the end of the statement. */
15181 cp_parser_skip_to_end_of_statement (parser
);
15182 /* If the next token is now a `;', consume it. */
15183 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15184 cp_lexer_consume_token (parser
->lexer
);
15192 bool auto_specifier_p
;
15193 /* NULL_TREE if both variable and function declaration are allowed,
15194 error_mark_node if function declaration are not allowed and
15195 a FUNCTION_DECL that should be diagnosed if it is followed by
15196 variable declarations. */
15197 tree auto_function_declaration
;
15199 last_type
= NULL_TREE
;
15201 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
15202 auto_function_declaration
= NULL_TREE
;
15204 /* Keep going until we hit the `;' at the end of the simple
15206 saw_declarator
= false;
15207 while (cp_lexer_next_token_is_not (parser
->lexer
,
15211 bool function_definition_p
;
15213 tree auto_result
= NULL_TREE
;
15215 if (saw_declarator
)
15217 /* If we are processing next declarator, comma is expected */
15218 token
= cp_lexer_peek_token (parser
->lexer
);
15219 gcc_assert (token
->type
== CPP_COMMA
);
15220 cp_lexer_consume_token (parser
->lexer
);
15221 if (maybe_range_for_decl
)
15223 *maybe_range_for_decl
= error_mark_node
;
15224 if (comma_loc
== UNKNOWN_LOCATION
)
15225 comma_loc
= token
->location
;
15229 saw_declarator
= true;
15231 /* Parse the init-declarator. */
15232 decl
= cp_parser_init_declarator (parser
,
15233 CP_PARSER_FLAGS_NONE
,
15236 function_definition_allowed_p
,
15237 /*member_p=*/false,
15238 declares_class_or_enum
,
15239 &function_definition_p
,
15240 maybe_range_for_decl
,
15243 /* If an error occurred while parsing tentatively, exit quickly.
15244 (That usually happens when in the body of a function; each
15245 statement is treated as a declaration-statement until proven
15247 if (cp_parser_error_occurred (parser
))
15250 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
15252 /* If the init-declarator-list contains more than one
15253 init-declarator, they shall all form declarations of
15255 if (auto_function_declaration
== NULL_TREE
)
15256 auto_function_declaration
15257 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
15258 else if (TREE_CODE (decl
) == FUNCTION_DECL
15259 || auto_function_declaration
!= error_mark_node
)
15261 error_at (decl_specifiers
.locations
[ds_type_spec
],
15262 "non-variable %qD in declaration with more than one "
15263 "declarator with placeholder type",
15264 TREE_CODE (decl
) == FUNCTION_DECL
15265 ? decl
: auto_function_declaration
);
15266 auto_function_declaration
= error_mark_node
;
15271 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
15274 && last_type
!= error_mark_node
15275 && !same_type_p (auto_result
, last_type
))
15277 /* If the list of declarators contains more than one declarator,
15278 the type of each declared variable is determined as described
15279 above. If the type deduced for the template parameter U is not
15280 the same in each deduction, the program is ill-formed. */
15281 error_at (decl_specifiers
.locations
[ds_type_spec
],
15282 "inconsistent deduction for %qT: %qT and then %qT",
15283 decl_specifiers
.type
, last_type
, auto_result
);
15284 last_type
= error_mark_node
;
15287 last_type
= auto_result
;
15290 /* Handle function definitions specially. */
15291 if (function_definition_p
)
15293 /* If the next token is a `,', then we are probably
15294 processing something like:
15298 which is erroneous. */
15299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15301 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15302 error_at (token
->location
,
15304 " declarations and function-definitions is forbidden");
15306 /* Otherwise, we're done with the list of declarators. */
15309 pop_deferring_access_checks ();
15310 cp_finalize_omp_declare_simd (parser
, &odsd
);
15314 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
15315 *maybe_range_for_decl
= decl
;
15316 /* The next token should be either a `,' or a `;'. */
15317 token
= cp_lexer_peek_token (parser
->lexer
);
15318 /* If it's a `,', there are more declarators to come. */
15319 if (token
->type
== CPP_COMMA
)
15320 /* will be consumed next time around */;
15321 /* If it's a `;', we are done. */
15322 else if (token
->type
== CPP_SEMICOLON
)
15324 else if (maybe_range_for_decl
)
15326 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
15327 permerror (decl_specifiers
.locations
[ds_type_spec
],
15328 "types may not be defined in a for-range-declaration");
15331 /* Anything else is an error. */
15334 /* If we have already issued an error message we don't need
15335 to issue another one. */
15336 if ((decl
!= error_mark_node
15337 && DECL_INITIAL (decl
) != error_mark_node
)
15338 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
15339 cp_parser_error (parser
, "expected %<,%> or %<;%>");
15340 /* Skip tokens until we reach the end of the statement. */
15341 cp_parser_skip_to_end_of_statement (parser
);
15342 /* If the next token is now a `;', consume it. */
15343 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15344 cp_lexer_consume_token (parser
->lexer
);
15347 /* After the first time around, a function-definition is not
15348 allowed -- even if it was OK at first. For example:
15353 function_definition_allowed_p
= false;
15356 /* Issue an error message if no declarators are present, and the
15357 decl-specifier-seq does not itself declare a class or
15358 enumeration: [dcl.dcl]/3. */
15359 if (!saw_declarator
)
15361 if (cp_parser_declares_only_class_p (parser
))
15363 if (!declares_class_or_enum
15364 && decl_specifiers
.type
15365 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
15366 /* Ensure an error is issued anyway when finish_decltype_type,
15367 called via cp_parser_decl_specifier_seq, returns a class or
15368 an enumeration (c++/51786). */
15369 decl_specifiers
.type
= NULL_TREE
;
15370 shadow_tag (&decl_specifiers
);
15372 /* Perform any deferred access checks. */
15373 perform_deferred_access_checks (tf_warning_or_error
);
15376 /* Consume the `;'. */
15378 if (!maybe_range_for_decl
)
15379 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15380 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15382 if (init_loc
!= UNKNOWN_LOCATION
)
15383 error_at (init_loc
, "initializer in range-based %<for%> loop");
15384 if (comma_loc
!= UNKNOWN_LOCATION
)
15385 error_at (comma_loc
,
15386 "multiple declarations in range-based %<for%> loop");
15390 pop_deferring_access_checks ();
15391 cp_finalize_omp_declare_simd (parser
, &odsd
);
15394 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15395 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15399 cp_parser_decomposition_declaration (cp_parser
*parser
,
15400 cp_decl_specifier_seq
*decl_specifiers
,
15401 tree
*maybe_range_for_decl
,
15402 location_t
*init_loc
)
15404 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
15405 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15406 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
15408 /* Parse the identifier-list. */
15409 auto_vec
<cp_expr
, 10> v
;
15410 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
15413 cp_expr e
= cp_parser_identifier (parser
);
15414 if (e
.get_value () == error_mark_node
)
15417 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15419 cp_lexer_consume_token (parser
->lexer
);
15422 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15423 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15425 end_loc
= UNKNOWN_LOCATION
;
15426 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
15428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
15429 cp_lexer_consume_token (parser
->lexer
);
15432 cp_parser_skip_to_end_of_statement (parser
);
15433 return error_mark_node
;
15437 if (cxx_dialect
< cxx17
)
15438 pedwarn (loc
, OPT_Wc__17_extensions
,
15439 "structured bindings only available with "
15440 "%<-std=c++17%> or %<-std=gnu++17%>");
15443 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
15444 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
15445 declarator
->id_loc
= loc
;
15446 if (ref_qual
!= REF_QUAL_NONE
)
15447 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
15448 ref_qual
== REF_QUAL_RVALUE
,
15450 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
15451 NULL_TREE
, decl_specifiers
->attributes
,
15453 tree orig_decl
= decl
;
15457 cp_decl_specifier_seq decl_specs
;
15458 clear_decl_specs (&decl_specs
);
15459 decl_specs
.type
= make_auto ();
15461 FOR_EACH_VEC_ELT (v
, i
, e
)
15464 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
15465 sfk_none
, e
.get_location ());
15468 declarator
->u
.id
.unqualified_name
= e
.get_value ();
15469 declarator
->id_loc
= e
.get_location ();
15471 tree elt_pushed_scope
;
15472 tree decl2
= start_decl (declarator
, &decl_specs
, SD_DECOMPOSITION
,
15473 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
15474 if (decl2
== error_mark_node
)
15475 decl
= error_mark_node
;
15476 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
15478 /* Ensure we've diagnosed redeclaration if we aren't creating
15480 gcc_assert (errorcount
);
15481 decl
= error_mark_node
;
15485 if (elt_pushed_scope
)
15486 pop_scope (elt_pushed_scope
);
15491 error_at (loc
, "empty structured binding declaration");
15492 decl
= error_mark_node
;
15495 if (maybe_range_for_decl
== NULL
15496 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
15498 bool non_constant_p
= false, is_direct_init
= false;
15499 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15500 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
15502 if (initializer
== NULL_TREE
15503 || (TREE_CODE (initializer
) == TREE_LIST
15504 && TREE_CHAIN (initializer
))
15506 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
15507 && CONSTRUCTOR_NELTS (initializer
) != 1))
15509 error_at (loc
, "invalid initializer for structured binding "
15511 initializer
= error_mark_node
;
15514 if (decl
!= error_mark_node
)
15516 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
15517 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
15518 (is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
15519 cp_finish_decomp (decl
, prev
, v
.length ());
15522 else if (decl
!= error_mark_node
)
15524 *maybe_range_for_decl
= prev
;
15525 /* Ensure DECL_VALUE_EXPR is created for all the decls but
15526 the underlying DECL. */
15527 cp_finish_decomp (decl
, prev
, v
.length ());
15531 pop_scope (pushed_scope
);
15533 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
15535 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
15536 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
15542 /* Parse a decl-specifier-seq.
15544 decl-specifier-seq:
15545 decl-specifier-seq [opt] decl-specifier
15546 decl-specifier attribute-specifier-seq [opt] (C++11)
15549 storage-class-specifier
15560 Concepts Extension:
15565 Set *DECL_SPECS to a representation of the decl-specifier-seq.
15567 The parser flags FLAGS is used to control type-specifier parsing.
15569 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15572 1: one of the decl-specifiers is an elaborated-type-specifier
15573 (i.e., a type declaration)
15574 2: one of the decl-specifiers is an enum-specifier or a
15575 class-specifier (i.e., a type definition)
15580 cp_parser_decl_specifier_seq (cp_parser
* parser
,
15581 cp_parser_flags flags
,
15582 cp_decl_specifier_seq
*decl_specs
,
15583 int* declares_class_or_enum
)
15585 bool constructor_possible_p
= !parser
->in_declarator_p
;
15586 bool found_decl_spec
= false;
15587 cp_token
*start_token
= NULL
;
15590 /* Clear DECL_SPECS. */
15591 clear_decl_specs (decl_specs
);
15593 /* Assume no class or enumeration type is declared. */
15594 *declares_class_or_enum
= 0;
15596 /* Keep reading specifiers until there are no more to read. */
15599 bool constructor_p
;
15603 /* Peek at the next token. */
15604 token
= cp_lexer_peek_token (parser
->lexer
);
15606 /* Save the first token of the decl spec list for error
15609 start_token
= token
;
15610 /* Handle attributes. */
15611 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) == 0
15612 && cp_next_tokens_can_be_attribute_p (parser
))
15614 /* Parse the attributes. */
15615 tree attrs
= cp_parser_attributes_opt (parser
);
15617 /* In a sequence of declaration specifiers, c++11 attributes
15618 appertain to the type that precede them. In that case
15621 The attribute-specifier-seq affects the type only for
15622 the declaration it appears in, not other declarations
15623 involving the same type.
15625 But for now let's force the user to position the
15626 attribute either at the beginning of the declaration or
15627 after the declarator-id, which would clearly mean that it
15628 applies to the declarator. */
15629 if (cxx11_attribute_p (attrs
))
15631 if (!found_decl_spec
)
15632 /* The c++11 attribute is at the beginning of the
15633 declaration. It appertains to the entity being
15637 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
15639 /* This is an attribute following a
15640 class-specifier. */
15641 if (decl_specs
->type_definition_p
)
15642 warn_misplaced_attr_for_class_type (token
->location
,
15648 decl_specs
->std_attributes
15649 = attr_chainon (decl_specs
->std_attributes
, attrs
);
15650 if (decl_specs
->locations
[ds_std_attribute
] == 0)
15651 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
15657 decl_specs
->attributes
15658 = attr_chainon (decl_specs
->attributes
, attrs
);
15659 if (decl_specs
->locations
[ds_attribute
] == 0)
15660 decl_specs
->locations
[ds_attribute
] = token
->location
;
15663 /* Assume we will find a decl-specifier keyword. */
15664 found_decl_spec
= true;
15665 /* If the next token is an appropriate keyword, we can simply
15666 add it to the list. */
15667 switch (token
->keyword
)
15674 if (!at_class_scope_p ())
15676 gcc_rich_location
richloc (token
->location
);
15677 richloc
.add_fixit_remove ();
15678 error_at (&richloc
, "%<friend%> used outside of class");
15679 cp_lexer_purge_token (parser
->lexer
);
15684 /* Consume the token. */
15685 cp_lexer_consume_token (parser
->lexer
);
15689 case RID_CONSTEXPR
:
15691 cp_lexer_consume_token (parser
->lexer
);
15694 case RID_CONSTINIT
:
15696 cp_lexer_consume_token (parser
->lexer
);
15699 case RID_CONSTEVAL
:
15701 cp_lexer_consume_token (parser
->lexer
);
15706 cp_lexer_consume_token (parser
->lexer
);
15708 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
15711 /* Warn for concept as a decl-specifier. We'll rewrite these as
15712 concept declarations later. */
15713 if (!flag_concepts_ts
)
15715 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
15716 if (next
->keyword
== RID_BOOL
)
15717 pedwarn (next
->location
, 0, "the %<bool%> keyword is not "
15718 "allowed in a C++20 concept definition");
15720 pedwarn (token
->location
, 0, "C++20 concept definition syntax "
15721 "is %<concept <name> = <expr>%>");
15724 /* In C++20 a concept definition is just 'concept name = expr;'
15725 Support that syntax as a TS extension by pretending we've seen
15726 the 'bool' specifier. */
15727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
15728 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
)
15729 && !decl_specs
->any_type_specifiers_p
)
15731 cp_parser_set_decl_spec_type (decl_specs
, boolean_type_node
,
15732 token
, /*type_definition*/false);
15733 decl_specs
->any_type_specifiers_p
= true;
15737 /* function-specifier:
15744 cp_parser_function_specifier_opt (parser
, decl_specs
);
15751 /* Consume the token. */
15752 cp_lexer_consume_token (parser
->lexer
);
15754 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
15757 /* A constructor declarator cannot appear in a typedef. */
15758 constructor_possible_p
= false;
15759 /* The "typedef" keyword can only occur in a declaration; we
15760 may as well commit at this point. */
15761 cp_parser_commit_to_tentative_parse (parser
);
15763 if (decl_specs
->storage_class
!= sc_none
)
15764 decl_specs
->conflicting_specifiers_p
= true;
15767 /* storage-class-specifier:
15777 if (cxx_dialect
== cxx98
)
15779 /* Consume the token. */
15780 cp_lexer_consume_token (parser
->lexer
);
15782 /* Complain about `auto' as a storage specifier, if
15783 we're complaining about C++0x compatibility. */
15784 gcc_rich_location
richloc (token
->location
);
15785 richloc
.add_fixit_remove ();
15786 warning_at (&richloc
, OPT_Wc__11_compat
,
15787 "%<auto%> changes meaning in C++11; "
15788 "please remove it");
15790 /* Set the storage class anyway. */
15791 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
15795 /* C++0x auto type-specifier. */
15796 found_decl_spec
= false;
15803 /* Consume the token. */
15804 cp_lexer_consume_token (parser
->lexer
);
15805 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
15809 /* Consume the token. */
15811 cp_lexer_consume_token (parser
->lexer
);
15815 /* We did not yet find a decl-specifier yet. */
15816 found_decl_spec
= false;
15820 if (found_decl_spec
15821 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
15822 && token
->keyword
!= RID_CONSTEXPR
)
15823 error ("%<decl-specifier%> invalid in condition");
15825 if (found_decl_spec
15826 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
15827 && token
->keyword
!= RID_MUTABLE
15828 && token
->keyword
!= RID_CONSTEXPR
15829 && token
->keyword
!= RID_CONSTEVAL
)
15830 error_at (token
->location
, "%qD invalid in lambda",
15831 ridpointers
[token
->keyword
]);
15834 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
15836 /* Constructors are a special case. The `S' in `S()' is not a
15837 decl-specifier; it is the beginning of the declarator. */
15839 = (!found_decl_spec
15840 && constructor_possible_p
15841 && (cp_parser_constructor_declarator_p
15842 (parser
, flags
, decl_spec_seq_has_spec_p (decl_specs
,
15845 /* If we don't have a DECL_SPEC yet, then we must be looking at
15846 a type-specifier. */
15847 if (!found_decl_spec
&& !constructor_p
)
15849 int decl_spec_declares_class_or_enum
;
15850 bool is_cv_qualifier
;
15853 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
15854 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
15857 = cp_parser_type_specifier (parser
, flags
,
15859 /*is_declaration=*/true,
15860 &decl_spec_declares_class_or_enum
,
15862 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
15864 /* If this type-specifier referenced a user-defined type
15865 (a typedef, class-name, etc.), then we can't allow any
15866 more such type-specifiers henceforth.
15870 The longest sequence of decl-specifiers that could
15871 possibly be a type name is taken as the
15872 decl-specifier-seq of a declaration. The sequence shall
15873 be self-consistent as described below.
15877 As a general rule, at most one type-specifier is allowed
15878 in the complete decl-specifier-seq of a declaration. The
15879 only exceptions are the following:
15881 -- const or volatile can be combined with any other
15884 -- signed or unsigned can be combined with char, long,
15892 void g (const int Pc);
15894 Here, Pc is *not* part of the decl-specifier seq; it's
15895 the declarator. Therefore, once we see a type-specifier
15896 (other than a cv-qualifier), we forbid any additional
15897 user-defined types. We *do* still allow things like `int
15898 int' to be considered a decl-specifier-seq, and issue the
15899 error message later. */
15900 if (type_spec
&& !is_cv_qualifier
)
15901 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
15902 /* A constructor declarator cannot follow a type-specifier. */
15905 constructor_possible_p
= false;
15906 found_decl_spec
= true;
15907 if (!is_cv_qualifier
)
15908 decl_specs
->any_type_specifiers_p
= true;
15910 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
15911 error_at (token
->location
, "type-specifier invalid in lambda");
15915 /* If we still do not have a DECL_SPEC, then there are no more
15916 decl-specifiers. */
15917 if (!found_decl_spec
)
15920 if (decl_specs
->std_attributes
)
15922 error_at (decl_specs
->locations
[ds_std_attribute
],
15923 "standard attributes in middle of decl-specifiers");
15924 inform (decl_specs
->locations
[ds_std_attribute
],
15925 "standard attributes must precede the decl-specifiers to "
15926 "apply to the declaration, or follow them to apply to "
15930 decl_specs
->any_specifiers_p
= true;
15931 /* After we see one decl-specifier, further decl-specifiers are
15932 always optional. */
15933 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
15936 /* Don't allow a friend specifier with a class definition. */
15937 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
15938 && (*declares_class_or_enum
& 2))
15939 error_at (decl_specs
->locations
[ds_friend
],
15940 "class definition may not be declared a friend");
15943 /* Parse an (optional) storage-class-specifier.
15945 storage-class-specifier:
15954 storage-class-specifier:
15957 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
15960 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
15962 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
15965 if (cxx_dialect
!= cxx98
)
15967 /* Fall through for C++98. */
15968 gcc_fallthrough ();
15975 /* Consume the token. */
15976 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
15983 /* Parse an (optional) function-specifier.
15985 function-specifier:
15991 explicit(constant-expression)
15993 Returns an IDENTIFIER_NODE corresponding to the keyword used.
15994 Updates DECL_SPECS, if it is non-NULL. */
15997 cp_parser_function_specifier_opt (cp_parser
* parser
,
15998 cp_decl_specifier_seq
*decl_specs
)
16000 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16001 switch (token
->keyword
)
16004 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
16008 /* 14.5.2.3 [temp.mem]
16010 A member function template shall not be virtual. */
16011 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16012 && current_class_type
)
16013 error_at (token
->location
, "templates may not be %<virtual%>");
16015 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
16020 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16021 /* If we see '(', it's C++20 explicit(bool). */
16023 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16025 matching_parens parens
;
16026 parens
.consume_open (parser
);
16028 /* New types are not allowed in an explicit-specifier. */
16029 const char *saved_message
16030 = parser
->type_definition_forbidden_message
;
16031 parser
->type_definition_forbidden_message
16032 = G_("types may not be defined in explicit-specifier");
16034 if (cxx_dialect
< cxx20
)
16035 pedwarn (token
->location
, OPT_Wc__20_extensions
,
16036 "%<explicit(bool)%> only available with %<-std=c++20%> "
16037 "or %<-std=gnu++20%>");
16039 /* Parse the constant-expression. */
16040 expr
= cp_parser_constant_expression (parser
);
16042 /* Restore the saved message. */
16043 parser
->type_definition_forbidden_message
= saved_message
;
16044 parens
.require_close (parser
);
16047 /* The explicit-specifier explicit without a constant-expression is
16048 equivalent to the explicit-specifier explicit(true). */
16049 expr
= boolean_true_node
;
16052 "the constant-expression, if supplied, shall be a contextually
16053 converted constant expression of type bool." */
16054 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
16055 /* We could evaluate it -- mark the decl as appropriate. */
16056 if (expr
== boolean_true_node
)
16057 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
16058 else if (expr
== boolean_false_node
)
16059 /* Don't mark the decl as explicit. */;
16060 else if (decl_specs
)
16061 /* The expression was value-dependent. Remember it so that we can
16062 substitute it later. */
16063 decl_specs
->explicit_specifier
= expr
;
16071 /* Consume the token. */
16072 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16075 /* Parse a linkage-specification.
16077 linkage-specification:
16078 extern string-literal { declaration-seq [opt] }
16079 extern string-literal declaration */
16082 cp_parser_linkage_specification (cp_parser
* parser
, tree prefix_attr
)
16086 /* Look for the `extern' keyword. */
16087 cp_token
*extern_token
16088 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
16090 /* Look for the string-literal. */
16091 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
16092 linkage
= cp_parser_string_literal (parser
, false, false);
16094 /* Transform the literal into an identifier. If the literal is a
16095 wide-character string, or contains embedded NULs, then we can't
16096 handle it as the user wants. */
16097 if (strlen (TREE_STRING_POINTER (linkage
))
16098 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
16100 cp_parser_error (parser
, "invalid linkage-specification");
16101 /* Assume C++ linkage. */
16102 linkage
= lang_name_cplusplus
;
16105 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
16107 /* We're now using the new linkage. */
16108 push_lang_context (linkage
);
16110 /* Preserve the location of the innermost linkage specification,
16111 tracking the locations of nested specifications via a local. */
16112 location_t saved_location
16113 = parser
->innermost_linkage_specification_location
;
16114 /* Construct a location ranging from the start of the "extern" to
16115 the end of the string-literal, with the caret at the start, e.g.:
16119 parser
->innermost_linkage_specification_location
16120 = make_location (extern_token
->location
,
16121 extern_token
->location
,
16122 get_finish (string_token
->location
));
16124 /* If the next token is a `{', then we're using the first
16126 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16128 cp_ensure_no_omp_declare_simd (parser
);
16129 cp_ensure_no_oacc_routine (parser
);
16131 /* Consume the `{' token. */
16132 matching_braces braces
;
16133 braces
.consume_open (parser
);
16134 /* Parse the declarations. */
16135 cp_parser_declaration_seq_opt (parser
);
16136 /* Look for the closing `}'. */
16137 braces
.require_close (parser
);
16139 /* Otherwise, there's just one declaration. */
16142 bool saved_in_unbraced_linkage_specification_p
;
16144 saved_in_unbraced_linkage_specification_p
16145 = parser
->in_unbraced_linkage_specification_p
;
16146 parser
->in_unbraced_linkage_specification_p
= true;
16147 cp_parser_declaration (parser
, prefix_attr
);
16148 parser
->in_unbraced_linkage_specification_p
16149 = saved_in_unbraced_linkage_specification_p
;
16152 /* We're done with the linkage-specification. */
16153 pop_lang_context ();
16155 /* Restore location of parent linkage specification, if any. */
16156 parser
->innermost_linkage_specification_location
= saved_location
;
16159 /* Parse a static_assert-declaration.
16161 static_assert-declaration:
16162 static_assert ( constant-expression , string-literal ) ;
16163 static_assert ( constant-expression ) ; (C++17)
16165 If MEMBER_P, this static_assert is a class member. */
16168 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
16171 location_t token_loc
;
16175 /* Peek at the `static_assert' token so we can keep track of exactly
16176 where the static assertion started. */
16177 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16179 /* Look for the `static_assert' keyword. */
16180 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
16184 /* We know we are in a static assertion; commit to any tentative
16186 if (cp_parser_parsing_tentatively (parser
))
16187 cp_parser_commit_to_tentative_parse (parser
);
16189 /* Parse the `(' starting the static assertion condition. */
16190 matching_parens parens
;
16191 parens
.require_open (parser
);
16193 /* Parse the constant-expression. Allow a non-constant expression
16194 here in order to give better diagnostics in finish_static_assert. */
16196 cp_parser_constant_expression (parser
,
16197 /*allow_non_constant_p=*/true,
16198 /*non_constant_p=*/&dummy
);
16200 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
16202 if (pedantic
&& cxx_dialect
< cxx17
)
16203 pedwarn (input_location
, OPT_Wc__17_extensions
,
16204 "%<static_assert%> without a message "
16205 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16207 cp_lexer_consume_token (parser
->lexer
);
16208 message
= build_string (1, "");
16209 TREE_TYPE (message
) = char_array_type_node
;
16210 fix_string_type (message
);
16214 /* Parse the separating `,'. */
16215 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
16217 /* Parse the string-literal message. */
16218 message
= cp_parser_string_literal (parser
,
16219 /*translate=*/false,
16222 /* A `)' completes the static assertion. */
16223 if (!parens
.require_close (parser
))
16224 cp_parser_skip_to_closing_parenthesis (parser
,
16225 /*recovering=*/true,
16226 /*or_comma=*/false,
16227 /*consume_paren=*/true);
16230 /* A semicolon terminates the declaration. */
16231 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16233 /* Get the location for the static assertion. Use that of the
16234 condition if available, otherwise, use that of the "static_assert"
16236 location_t assert_loc
= condition
.get_location ();
16237 if (assert_loc
== UNKNOWN_LOCATION
)
16238 assert_loc
= token_loc
;
16240 /* Complete the static assertion, which may mean either processing
16241 the static assert now or saving it for template instantiation. */
16242 finish_static_assert (condition
, message
, assert_loc
, member_p
,
16243 /*show_expr_p=*/false);
16246 /* Parse the expression in decltype ( expression ). */
16249 cp_parser_decltype_expr (cp_parser
*parser
,
16250 bool &id_expression_or_member_access_p
)
16252 cp_token
*id_expr_start_token
;
16255 /* First, try parsing an id-expression. */
16256 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
16257 cp_parser_parse_tentatively (parser
);
16258 expr
= cp_parser_id_expression (parser
,
16259 /*template_keyword_p=*/false,
16260 /*check_dependency_p=*/true,
16261 /*template_p=*/NULL
,
16262 /*declarator_p=*/false,
16263 /*optional_p=*/false);
16265 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
16267 bool non_integral_constant_expression_p
= false;
16268 tree id_expression
= expr
;
16270 const char *error_msg
;
16272 if (identifier_p (expr
))
16273 /* Lookup the name we got back from the id-expression. */
16274 expr
= cp_parser_lookup_name_simple (parser
, expr
,
16275 id_expr_start_token
->location
);
16277 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
16278 /* A template without args is not a complete id-expression. */
16279 expr
= error_mark_node
;
16282 && expr
!= error_mark_node
16283 && TREE_CODE (expr
) != TYPE_DECL
16284 && (TREE_CODE (expr
) != BIT_NOT_EXPR
16285 || !TYPE_P (TREE_OPERAND (expr
, 0)))
16286 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
16288 /* Complete lookup of the id-expression. */
16289 expr
= (finish_id_expression
16290 (id_expression
, expr
, parser
->scope
, &idk
,
16291 /*integral_constant_expression_p=*/false,
16292 /*allow_non_integral_constant_expression_p=*/true,
16293 &non_integral_constant_expression_p
,
16294 /*template_p=*/false,
16296 /*address_p=*/false,
16297 /*template_arg_p=*/false,
16299 id_expr_start_token
->location
));
16301 if (expr
== error_mark_node
)
16302 /* We found an id-expression, but it was something that we
16303 should not have found. This is an error, not something
16304 we can recover from, so note that we found an
16305 id-expression and we'll recover as gracefully as
16307 id_expression_or_member_access_p
= true;
16311 && expr
!= error_mark_node
16312 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
16313 /* We have an id-expression. */
16314 id_expression_or_member_access_p
= true;
16317 if (!id_expression_or_member_access_p
)
16319 /* Abort the id-expression parse. */
16320 cp_parser_abort_tentative_parse (parser
);
16322 /* Parsing tentatively, again. */
16323 cp_parser_parse_tentatively (parser
);
16325 /* Parse a class member access. */
16326 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
16327 /*cast_p=*/false, /*decltype*/true,
16328 /*member_access_only_p=*/true, NULL
);
16331 && expr
!= error_mark_node
16332 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
16333 /* We have an id-expression. */
16334 id_expression_or_member_access_p
= true;
16337 if (id_expression_or_member_access_p
)
16338 /* We have parsed the complete id-expression or member access. */
16339 cp_parser_parse_definitely (parser
);
16342 /* Abort our attempt to parse an id-expression or member access
16344 cp_parser_abort_tentative_parse (parser
);
16346 /* Parse a full expression. */
16347 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
16348 /*decltype_p=*/true);
16354 /* Parse a `decltype' type. Returns the type.
16356 decltype-specifier:
16357 decltype ( expression )
16359 decltype ( auto ) */
16362 cp_parser_decltype (cp_parser
*parser
)
16364 bool id_expression_or_member_access_p
= false;
16365 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
16367 if (start_token
->type
== CPP_DECLTYPE
)
16369 /* Already parsed. */
16370 cp_lexer_consume_token (parser
->lexer
);
16371 return saved_checks_value (start_token
->u
.tree_check_value
);
16374 /* Look for the `decltype' token. */
16375 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
16376 return error_mark_node
;
16378 /* Parse the opening `('. */
16379 matching_parens parens
;
16380 if (!parens
.require_open (parser
))
16381 return error_mark_node
;
16383 /* Since we're going to preserve any side-effects from this parse, set up a
16384 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16385 in the expression. */
16386 tentative_firewall
firewall (parser
);
16388 /* If in_declarator_p, a reparse as an expression might succeed (60361).
16389 Otherwise, commit now for better diagnostics. */
16390 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
16391 && !parser
->in_declarator_p
)
16392 cp_parser_commit_to_topmost_tentative_parse (parser
);
16394 push_deferring_access_checks (dk_deferred
);
16396 tree expr
= NULL_TREE
;
16398 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
)
16399 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
16401 /* decltype (auto) */
16402 cp_lexer_consume_token (parser
->lexer
);
16403 if (cxx_dialect
< cxx14
)
16405 error_at (start_token
->location
,
16406 "%<decltype(auto)%> type specifier only available with "
16407 "%<-std=c++14%> or %<-std=gnu++14%>");
16408 expr
= error_mark_node
;
16413 /* decltype (expression) */
16415 /* Types cannot be defined in a `decltype' expression. Save away the
16416 old message and set the new one. */
16417 const char *saved_message
= parser
->type_definition_forbidden_message
;
16418 parser
->type_definition_forbidden_message
16419 = G_("types may not be defined in %<decltype%> expressions");
16421 /* The restrictions on constant-expressions do not apply inside
16422 decltype expressions. */
16423 bool saved_integral_constant_expression_p
16424 = parser
->integral_constant_expression_p
;
16425 bool saved_non_integral_constant_expression_p
16426 = parser
->non_integral_constant_expression_p
;
16427 parser
->integral_constant_expression_p
= false;
16429 /* Within a parenthesized expression, a `>' token is always
16430 the greater-than operator. */
16431 bool saved_greater_than_is_operator_p
16432 = parser
->greater_than_is_operator_p
;
16433 parser
->greater_than_is_operator_p
= true;
16435 /* Don't synthesize an implicit template type parameter here. This
16436 could happen with C++23 code like
16438 void f(decltype(new auto{0}));
16440 where we want to deduce the auto right away so that the parameter
16441 is of type 'int *'. */
16442 auto cleanup
= make_temp_override
16443 (parser
->auto_is_implicit_function_template_parm_p
, false);
16445 /* Do not actually evaluate the expression. */
16446 ++cp_unevaluated_operand
;
16448 /* Do not warn about problems with the expression. */
16449 ++c_inhibit_evaluation_warnings
;
16451 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
16452 STRIP_ANY_LOCATION_WRAPPER (expr
);
16454 /* Go back to evaluating expressions. */
16455 --cp_unevaluated_operand
;
16456 --c_inhibit_evaluation_warnings
;
16458 /* The `>' token might be the end of a template-id or
16459 template-parameter-list now. */
16460 parser
->greater_than_is_operator_p
16461 = saved_greater_than_is_operator_p
;
16463 /* Restore the old message and the integral constant expression
16465 parser
->type_definition_forbidden_message
= saved_message
;
16466 parser
->integral_constant_expression_p
16467 = saved_integral_constant_expression_p
;
16468 parser
->non_integral_constant_expression_p
16469 = saved_non_integral_constant_expression_p
;
16472 /* Parse to the closing `)'. */
16473 if (expr
== error_mark_node
|| !parens
.require_close (parser
))
16475 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16476 /*consume_paren=*/true);
16477 expr
= error_mark_node
;
16480 /* If we got a parse error while tentative, bail out now. */
16481 if (cp_parser_error_occurred (parser
))
16483 pop_deferring_access_checks ();
16484 return error_mark_node
;
16489 expr
= make_decltype_auto ();
16491 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
16492 tf_warning_or_error
);
16494 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16496 start_token
->type
= CPP_DECLTYPE
;
16497 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16498 start_token
->tree_check_p
= true;
16499 start_token
->u
.tree_check_value
->value
= expr
;
16500 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16501 start_token
->keyword
= RID_MAX
;
16503 location_t loc
= start_token
->location
;
16504 loc
= make_location (loc
, loc
, parser
->lexer
);
16505 start_token
->location
= loc
;
16507 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
16509 pop_to_parent_deferring_access_checks ();
16514 /* Special member functions [gram.special] */
16516 /* Parse a conversion-function-id.
16518 conversion-function-id:
16519 operator conversion-type-id
16521 Returns an IDENTIFIER_NODE representing the operator. */
16524 cp_parser_conversion_function_id (cp_parser
* parser
)
16528 tree saved_qualifying_scope
;
16529 tree saved_object_scope
;
16530 tree pushed_scope
= NULL_TREE
;
16532 /* Look for the `operator' token. */
16533 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
16534 return error_mark_node
;
16535 /* When we parse the conversion-type-id, the current scope will be
16536 reset. However, we need that information in able to look up the
16537 conversion function later, so we save it here. */
16538 saved_scope
= parser
->scope
;
16539 saved_qualifying_scope
= parser
->qualifying_scope
;
16540 saved_object_scope
= parser
->object_scope
;
16541 /* We must enter the scope of the class so that the names of
16542 entities declared within the class are available in the
16543 conversion-type-id. For example, consider:
16550 S::operator I() { ... }
16552 In order to see that `I' is a type-name in the definition, we
16553 must be in the scope of `S'. */
16555 pushed_scope
= push_scope (saved_scope
);
16556 /* Parse the conversion-type-id. */
16557 type
= cp_parser_conversion_type_id (parser
);
16558 /* Leave the scope of the class, if any. */
16560 pop_scope (pushed_scope
);
16561 /* Restore the saved scope. */
16562 parser
->scope
= saved_scope
;
16563 parser
->qualifying_scope
= saved_qualifying_scope
;
16564 parser
->object_scope
= saved_object_scope
;
16565 /* If the TYPE is invalid, indicate failure. */
16566 if (type
== error_mark_node
)
16567 return error_mark_node
;
16568 return make_conv_op_name (type
);
16571 /* Parse a conversion-type-id:
16573 conversion-type-id:
16574 type-specifier-seq conversion-declarator [opt]
16576 Returns the TYPE specified. */
16579 cp_parser_conversion_type_id (cp_parser
* parser
)
16582 cp_decl_specifier_seq type_specifiers
;
16583 cp_declarator
*declarator
;
16584 tree type_specified
;
16585 const char *saved_message
;
16587 /* Parse the attributes. */
16588 attributes
= cp_parser_attributes_opt (parser
);
16590 saved_message
= parser
->type_definition_forbidden_message
;
16591 parser
->type_definition_forbidden_message
16592 = G_("types may not be defined in a conversion-type-id");
16594 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
16595 optional in conversion-type-id. */
16596 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16597 /*is_declaration=*/false,
16598 /*is_trailing_return=*/false,
16601 parser
->type_definition_forbidden_message
= saved_message
;
16603 /* If that didn't work, stop. */
16604 if (type_specifiers
.type
== error_mark_node
)
16605 return error_mark_node
;
16606 /* Parse the conversion-declarator. */
16607 declarator
= cp_parser_conversion_declarator_opt (parser
);
16609 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
16610 /*initialized=*/0, &attributes
);
16612 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
16614 /* Don't give this error when parsing tentatively. This happens to
16615 work because we always parse this definitively once. */
16616 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
16617 && type_uses_auto (type_specified
))
16619 if (cxx_dialect
< cxx14
)
16621 error ("invalid use of %<auto%> in conversion operator");
16622 return error_mark_node
;
16624 else if (template_parm_scope_p ())
16625 warning (0, "use of %<auto%> in member template "
16626 "conversion operator can never be deduced");
16629 return type_specified
;
16632 /* Parse an (optional) conversion-declarator.
16634 conversion-declarator:
16635 ptr-operator conversion-declarator [opt]
16639 static cp_declarator
*
16640 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
16642 enum tree_code code
;
16643 tree class_type
, std_attributes
= NULL_TREE
;
16644 cp_cv_quals cv_quals
;
16646 /* We don't know if there's a ptr-operator next, or not. */
16647 cp_parser_parse_tentatively (parser
);
16648 /* Try the ptr-operator. */
16649 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
16651 /* If it worked, look for more conversion-declarators. */
16652 if (cp_parser_parse_definitely (parser
))
16654 cp_declarator
*declarator
;
16656 /* Parse another optional declarator. */
16657 declarator
= cp_parser_conversion_declarator_opt (parser
);
16659 declarator
= cp_parser_make_indirect_declarator
16660 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
16668 /* Parse an (optional) ctor-initializer.
16671 : mem-initializer-list */
16674 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
16676 /* If the next token is not a `:', then there is no
16677 ctor-initializer. */
16678 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
16680 /* Do default initialization of any bases and members. */
16681 if (DECL_CONSTRUCTOR_P (current_function_decl
))
16682 finish_mem_initializers (NULL_TREE
);
16686 /* Consume the `:' token. */
16687 cp_lexer_consume_token (parser
->lexer
);
16688 /* And the mem-initializer-list. */
16689 cp_parser_mem_initializer_list (parser
);
16692 /* Parse a mem-initializer-list.
16694 mem-initializer-list:
16695 mem-initializer ... [opt]
16696 mem-initializer ... [opt] , mem-initializer-list */
16699 cp_parser_mem_initializer_list (cp_parser
* parser
)
16701 tree mem_initializer_list
= NULL_TREE
;
16702 tree target_ctor
= error_mark_node
;
16703 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16705 /* Let the semantic analysis code know that we are starting the
16706 mem-initializer-list. */
16707 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
16708 error_at (token
->location
,
16709 "only constructors take member initializers");
16711 /* Loop through the list. */
16714 tree mem_initializer
;
16716 token
= cp_lexer_peek_token (parser
->lexer
);
16717 /* Parse the mem-initializer. */
16718 mem_initializer
= cp_parser_mem_initializer (parser
);
16719 /* If the next token is a `...', we're expanding member initializers. */
16720 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
16722 || (mem_initializer
!= error_mark_node
16723 && check_for_bare_parameter_packs (TREE_PURPOSE
16724 (mem_initializer
))))
16726 /* Consume the `...'. */
16728 cp_lexer_consume_token (parser
->lexer
);
16730 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
16731 can be expanded but members cannot. */
16732 if (mem_initializer
!= error_mark_node
16733 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
16735 error_at (token
->location
,
16736 "cannot expand initializer for member %qD",
16737 TREE_PURPOSE (mem_initializer
));
16738 mem_initializer
= error_mark_node
;
16741 /* Construct the pack expansion type. */
16742 if (mem_initializer
!= error_mark_node
)
16743 mem_initializer
= make_pack_expansion (mem_initializer
);
16745 if (target_ctor
!= error_mark_node
16746 && mem_initializer
!= error_mark_node
)
16748 error ("mem-initializer for %qD follows constructor delegation",
16749 TREE_PURPOSE (mem_initializer
));
16750 mem_initializer
= error_mark_node
;
16752 /* Look for a target constructor. */
16753 if (mem_initializer
!= error_mark_node
16754 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
16755 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
16757 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
16758 if (mem_initializer_list
)
16760 error ("constructor delegation follows mem-initializer for %qD",
16761 TREE_PURPOSE (mem_initializer_list
));
16762 mem_initializer
= error_mark_node
;
16764 target_ctor
= mem_initializer
;
16766 /* Add it to the list, unless it was erroneous. */
16767 if (mem_initializer
!= error_mark_node
)
16769 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
16770 mem_initializer_list
= mem_initializer
;
16772 /* If the next token is not a `,', we're done. */
16773 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16775 /* Consume the `,' token. */
16776 cp_lexer_consume_token (parser
->lexer
);
16779 /* Perform semantic analysis. */
16780 if (DECL_CONSTRUCTOR_P (current_function_decl
))
16781 finish_mem_initializers (mem_initializer_list
);
16784 /* Parse a mem-initializer.
16787 mem-initializer-id ( expression-list [opt] )
16788 mem-initializer-id braced-init-list
16793 ( expression-list [opt] )
16795 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
16796 class) or FIELD_DECL (for a non-static data member) to initialize;
16797 the TREE_VALUE is the expression-list. An empty initialization
16798 list is represented by void_list_node. */
16801 cp_parser_mem_initializer (cp_parser
* parser
)
16803 tree mem_initializer_id
;
16804 tree expression_list
;
16806 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16808 /* Find out what is being initialized. */
16809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16811 permerror (token
->location
,
16812 "anachronistic old-style base class initializer");
16813 mem_initializer_id
= NULL_TREE
;
16817 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
16818 if (mem_initializer_id
== error_mark_node
)
16819 return mem_initializer_id
;
16821 member
= expand_member_init (mem_initializer_id
);
16822 if (member
&& !DECL_P (member
))
16823 in_base_initializer
= 1;
16825 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16827 bool expr_non_constant_p
;
16828 cp_lexer_set_source_position (parser
->lexer
);
16829 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
16830 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
16831 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
16832 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
16836 vec
<tree
, va_gc
> *vec
;
16837 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
16839 /*allow_expansion_p=*/true,
16840 /*non_constant_p=*/NULL
,
16841 /*close_paren_loc=*/NULL
,
16842 /*wrap_locations_p=*/true);
16844 return error_mark_node
;
16845 expression_list
= build_tree_list_vec (vec
);
16846 release_tree_vector (vec
);
16849 if (expression_list
== error_mark_node
)
16850 return error_mark_node
;
16851 if (!expression_list
)
16852 expression_list
= void_type_node
;
16854 in_base_initializer
= 0;
16857 return error_mark_node
;
16858 tree node
= build_tree_list (member
, expression_list
);
16860 /* We can't attach the source location of this initializer directly to
16861 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
16862 within the TREE_TYPE of the list node. */
16864 = make_location (token
->location
, token
->location
, parser
->lexer
);
16865 tree dummy
= build0 (EMPTY_CLASS_EXPR
, NULL_TREE
);
16866 SET_EXPR_LOCATION (dummy
, loc
);
16867 TREE_TYPE (node
) = dummy
;
16872 /* Parse a mem-initializer-id.
16874 mem-initializer-id:
16875 :: [opt] nested-name-specifier [opt] class-name
16876 decltype-specifier (C++11)
16879 Returns a TYPE indicating the class to be initialized for the first
16880 production (and the second in C++11). Returns an IDENTIFIER_NODE
16881 indicating the data member to be initialized for the last production. */
16884 cp_parser_mem_initializer_id (cp_parser
* parser
)
16886 bool global_scope_p
;
16887 bool nested_name_specifier_p
;
16888 bool template_p
= false;
16891 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16893 /* `typename' is not allowed in this context ([temp.res]). */
16894 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
16896 error_at (token
->location
,
16897 "keyword %<typename%> not allowed in this context (a qualified "
16898 "member initializer is implicitly a type)");
16899 cp_lexer_consume_token (parser
->lexer
);
16901 /* Look for the optional `::' operator. */
16903 = (cp_parser_global_scope_opt (parser
,
16904 /*current_scope_valid_p=*/false)
16906 /* Look for the optional nested-name-specifier. The simplest way to
16911 The keyword `typename' is not permitted in a base-specifier or
16912 mem-initializer; in these contexts a qualified name that
16913 depends on a template-parameter is implicitly assumed to be a
16916 is to assume that we have seen the `typename' keyword at this
16918 nested_name_specifier_p
16919 = (cp_parser_nested_name_specifier_opt (parser
,
16920 /*typename_keyword_p=*/true,
16921 /*check_dependency_p=*/true,
16923 /*is_declaration=*/true)
16925 if (nested_name_specifier_p
)
16926 template_p
= cp_parser_optional_template_keyword (parser
);
16927 /* If there is a `::' operator or a nested-name-specifier, then we
16928 are definitely looking for a class-name. */
16929 if (global_scope_p
|| nested_name_specifier_p
)
16930 return cp_parser_class_name (parser
,
16931 /*typename_keyword_p=*/true,
16932 /*template_keyword_p=*/template_p
,
16934 /*check_dependency_p=*/true,
16935 /*class_head_p=*/false,
16936 /*is_declaration=*/true);
16937 /* Otherwise, we could also be looking for an ordinary identifier. */
16938 cp_parser_parse_tentatively (parser
);
16939 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
16940 /* Try a decltype-specifier. */
16941 id
= cp_parser_decltype (parser
);
16943 /* Otherwise, try a class-name. */
16944 id
= cp_parser_class_name (parser
,
16945 /*typename_keyword_p=*/true,
16946 /*template_keyword_p=*/false,
16948 /*check_dependency_p=*/true,
16949 /*class_head_p=*/false,
16950 /*is_declaration=*/true);
16951 /* If we found one, we're done. */
16952 if (cp_parser_parse_definitely (parser
))
16954 /* Otherwise, look for an ordinary identifier. */
16955 return cp_parser_identifier (parser
);
16958 /* Overloading [gram.over] */
16960 /* Parse an operator-function-id.
16962 operator-function-id:
16965 Returns an IDENTIFIER_NODE for the operator which is a
16966 human-readable spelling of the identifier, e.g., `operator +'. */
16969 cp_parser_operator_function_id (cp_parser
* parser
)
16971 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16972 /* Look for the `operator' keyword. */
16973 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
16974 return error_mark_node
;
16975 /* And then the name of the operator itself. */
16976 return cp_parser_operator (parser
, start_loc
);
16979 /* Return an identifier node for a user-defined literal operator.
16980 The suffix identifier is chained to the operator name identifier. */
16983 cp_literal_operator_id (const char* name
)
16986 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
16987 + strlen (name
) + 10);
16988 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
16989 identifier
= get_identifier (buffer
);
16990 XDELETEVEC (buffer
);
16995 /* Parse an operator.
16998 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
16999 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17000 || ++ -- , ->* -> () []
17007 Returns an IDENTIFIER_NODE for the operator which is a
17008 human-readable spelling of the identifier, e.g., `operator +'. */
17011 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
17013 tree id
= NULL_TREE
;
17017 /* Peek at the next token. */
17018 token
= cp_lexer_peek_token (parser
->lexer
);
17020 location_t end_loc
= token
->location
;
17022 /* Figure out which operator we have. */
17023 enum tree_code op
= ERROR_MARK
;
17024 bool assop
= false;
17025 bool consumed
= false;
17026 switch (token
->type
)
17030 /* The keyword should be either `new', `delete' or `co_await'. */
17031 if (token
->keyword
== RID_NEW
)
17033 else if (token
->keyword
== RID_DELETE
)
17035 else if (token
->keyword
== RID_CO_AWAIT
)
17036 op
= CO_AWAIT_EXPR
;
17040 /* Consume the `new', `delete' or co_await token. */
17041 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
17043 /* Peek at the next token. */
17044 token
= cp_lexer_peek_token (parser
->lexer
);
17045 /* If it's a `[' token then this is the array variant of the
17047 if (token
->type
== CPP_OPEN_SQUARE
17048 && op
!= CO_AWAIT_EXPR
)
17050 /* Consume the `[' token. */
17051 cp_lexer_consume_token (parser
->lexer
);
17052 /* Look for the `]' token. */
17053 if (cp_token
*close_token
17054 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17055 end_loc
= close_token
->location
;
17056 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
17075 op
= TRUNC_DIV_EXPR
;
17079 op
= TRUNC_MOD_EXPR
;
17099 op
= TRUTH_NOT_EXPR
;
17132 op
= TRUNC_DIV_EXPR
;
17137 op
= TRUNC_MOD_EXPR
;
17163 case CPP_LSHIFT_EQ
:
17168 case CPP_RSHIFT_EQ
:
17185 case CPP_GREATER_EQ
:
17189 case CPP_SPACESHIP
:
17190 op
= SPACESHIP_EXPR
;
17194 op
= TRUTH_ANDIF_EXPR
;
17198 op
= TRUTH_ORIF_EXPR
;
17201 case CPP_PLUS_PLUS
:
17202 op
= POSTINCREMENT_EXPR
;
17205 case CPP_MINUS_MINUS
:
17206 op
= PREDECREMENT_EXPR
;
17210 op
= COMPOUND_EXPR
;
17213 case CPP_DEREF_STAR
:
17218 op
= COMPONENT_REF
;
17223 /* Consume the `?'. */
17224 cp_lexer_consume_token (parser
->lexer
);
17225 /* Look for the matching `:'. */
17226 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
17230 case CPP_OPEN_PAREN
:
17232 /* Consume the `('. */
17233 matching_parens parens
;
17234 parens
.consume_open (parser
);
17235 /* Look for the matching `)'. */
17236 token
= parens
.require_close (parser
);
17238 end_loc
= token
->location
;
17244 case CPP_OPEN_SQUARE
:
17245 /* Consume the `['. */
17246 cp_lexer_consume_token (parser
->lexer
);
17247 /* Look for the matching `]'. */
17248 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
17250 end_loc
= token
->location
;
17255 case CPP_UTF8STRING
:
17256 case CPP_UTF8STRING_USERDEF
:
17263 case CPP_STRING_USERDEF
:
17264 case CPP_WSTRING_USERDEF
:
17265 case CPP_STRING16_USERDEF
:
17266 case CPP_STRING32_USERDEF
:
17272 if (cxx_dialect
== cxx98
)
17273 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
17275 /* Consume the string. */
17276 str
= cp_parser_string_literal (parser
, /*translate=*/true,
17277 /*wide_ok=*/true, /*lookup_udlit=*/false);
17278 if (str
== error_mark_node
)
17279 return error_mark_node
;
17280 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
17282 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
17283 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
17284 end_loc
= str
.get_location ();
17289 /* Look for the suffix identifier. */
17290 token
= cp_lexer_peek_token (parser
->lexer
);
17291 if (token
->type
== CPP_NAME
)
17293 id
= cp_parser_identifier (parser
);
17294 end_loc
= token
->location
;
17296 else if (token
->type
== CPP_KEYWORD
)
17298 error ("unexpected keyword;"
17299 " remove space between quotes and suffix identifier");
17300 return error_mark_node
;
17304 error ("expected suffix identifier");
17305 return error_mark_node
;
17308 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17309 (TREE_TYPE (TREE_TYPE (string_tree
))));
17310 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
17313 error ("expected empty string after %<operator%> keyword");
17314 return error_mark_node
;
17316 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
17319 error ("invalid encoding prefix in literal operator");
17320 return error_mark_node
;
17322 if (id
!= error_mark_node
)
17324 const char *name
= IDENTIFIER_POINTER (id
);
17325 id
= cp_literal_operator_id (name
);
17327 /* Generate a location of the form:
17328 "" _suffix_identifier
17329 ^~~~~~~~~~~~~~~~~~~~~
17330 with caret == start at the start token, finish at the end of the
17331 suffix identifier. */
17332 location_t combined_loc
17333 = make_location (start_loc
, start_loc
, parser
->lexer
);
17334 return cp_expr (id
, combined_loc
);
17338 /* Anything else is an error. */
17342 /* If we have selected an identifier, we need to consume the
17344 if (op
!= ERROR_MARK
)
17346 id
= ovl_op_identifier (assop
, op
);
17348 cp_lexer_consume_token (parser
->lexer
);
17350 /* Otherwise, no valid operator name was present. */
17353 cp_parser_error (parser
, "expected operator");
17354 id
= error_mark_node
;
17357 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
17358 return cp_expr (id
, start_loc
);
17361 /* Parse a template-declaration.
17363 template-declaration:
17364 export [opt] template < template-parameter-list > declaration
17366 If MEMBER_P is TRUE, this template-declaration occurs within a
17369 The grammar rule given by the standard isn't correct. What
17370 is really meant is:
17372 template-declaration:
17373 export [opt] template-parameter-list-seq
17374 decl-specifier-seq [opt] init-declarator [opt] ;
17375 export [opt] template-parameter-list-seq
17376 function-definition
17378 template-parameter-list-seq:
17379 template-parameter-list-seq [opt]
17380 template < template-parameter-list >
17382 Concept Extensions:
17384 template-parameter-list-seq:
17385 template < template-parameter-list > requires-clause [opt]
17388 requires logical-or-expression */
17391 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
17393 /* Check for `export'. */
17394 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
17396 /* Consume the `export' token. */
17397 cp_lexer_consume_token (parser
->lexer
);
17398 /* Warn that this use of export is deprecated. */
17399 if (cxx_dialect
< cxx11
)
17400 warning (0, "keyword %<export%> not implemented, and will be ignored");
17401 else if (cxx_dialect
< cxx20
)
17402 warning (0, "keyword %<export%> is deprecated, and is ignored");
17404 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17407 cp_parser_template_declaration_after_export (parser
, member_p
);
17410 /* Parse a template-parameter-list.
17412 template-parameter-list:
17414 template-parameter-list , template-parameter
17416 Returns a TREE_LIST. Each node represents a template parameter.
17417 The nodes are connected via their TREE_CHAINs. */
17420 cp_parser_template_parameter_list (cp_parser
* parser
)
17422 tree parameter_list
= NULL_TREE
;
17424 /* Don't create wrapper nodes within a template-parameter-list,
17425 since we don't want to have different types based on the
17426 spelling location of constants and decls within them. */
17427 auto_suppress_location_wrappers sentinel
;
17429 begin_template_parm_list ();
17431 /* The loop below parses the template parms. We first need to know
17432 the total number of template parms to be able to compute proper
17433 canonical types of each dependent type. So after the loop, when
17434 we know the total number of template parms,
17435 end_template_parm_list computes the proper canonical types and
17436 fixes up the dependent types accordingly. */
17441 bool is_parameter_pack
;
17442 location_t parm_loc
;
17444 /* Parse the template-parameter. */
17445 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17446 parameter
= cp_parser_template_parameter (parser
,
17448 &is_parameter_pack
);
17449 /* Add it to the list. */
17450 if (parameter
!= error_mark_node
)
17451 parameter_list
= process_template_parm (parameter_list
,
17455 is_parameter_pack
);
17458 tree err_parm
= build_tree_list (parameter
, parameter
);
17459 parameter_list
= chainon (parameter_list
, err_parm
);
17462 /* If the next token is not a `,', we're done. */
17463 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17465 /* Otherwise, consume the `,' token. */
17466 cp_lexer_consume_token (parser
->lexer
);
17469 return end_template_parm_list (parameter_list
);
17472 /* Parse a introduction-list.
17475 introduced-parameter
17476 introduction-list , introduced-parameter
17478 introduced-parameter:
17479 ...[opt] identifier
17481 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
17482 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
17483 WILDCARD_DECL will also have DECL_NAME set and token location in
17484 DECL_SOURCE_LOCATION. */
17487 cp_parser_introduction_list (cp_parser
*parser
)
17489 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
17493 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
17495 cp_lexer_consume_token (parser
->lexer
);
17497 tree identifier
= cp_parser_identifier (parser
);
17498 if (identifier
== error_mark_node
)
17501 /* Build placeholder. */
17502 tree parm
= build_nt (WILDCARD_DECL
);
17503 DECL_SOURCE_LOCATION (parm
)
17504 = cp_lexer_peek_token (parser
->lexer
)->location
;
17505 DECL_NAME (parm
) = identifier
;
17506 WILDCARD_PACK_P (parm
) = is_pack
;
17507 vec_safe_push (introduction_vec
, parm
);
17509 /* If the next token is not a `,', we're done. */
17510 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17512 /* Otherwise, consume the `,' token. */
17513 cp_lexer_consume_token (parser
->lexer
);
17516 /* Convert the vec into a TREE_VEC. */
17517 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
17520 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
17521 TREE_VEC_ELT (introduction_list
, n
) = parm
;
17523 release_tree_vector (introduction_vec
);
17524 return introduction_list
;
17527 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17528 is an abstract declarator. */
17530 static inline cp_declarator
*
17531 get_id_declarator (cp_declarator
*declarator
)
17533 cp_declarator
*d
= declarator
;
17534 while (d
&& d
->kind
!= cdk_id
)
17539 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17540 is an abstract declarator. */
17543 get_unqualified_id (cp_declarator
*declarator
)
17545 declarator
= get_id_declarator (declarator
);
17547 return declarator
->u
.id
.unqualified_name
;
17552 /* Returns true if TYPE would declare a constrained constrained-parameter. */
17555 is_constrained_parameter (tree type
)
17558 && TREE_CODE (type
) == TYPE_DECL
17559 && CONSTRAINED_PARM_CONCEPT (type
)
17560 && DECL_P (CONSTRAINED_PARM_CONCEPT (type
)));
17563 /* Returns true if PARM declares a constrained-parameter. */
17566 is_constrained_parameter (cp_parameter_declarator
*parm
)
17568 return is_constrained_parameter (parm
->decl_specifiers
.type
);
17571 /* Check that the type parameter is only a declarator-id, and that its
17572 type is not cv-qualified. */
17575 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
17576 cp_parameter_declarator
*parm
)
17578 if (!parm
->declarator
)
17581 if (parm
->declarator
->kind
!= cdk_id
)
17583 cp_parser_error (parser
, "invalid constrained type parameter");
17587 /* Don't allow cv-qualified type parameters. */
17588 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
17589 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
17591 cp_parser_error (parser
, "cv-qualified type parameter");
17598 /* Finish parsing/processing a template type parameter and checking
17599 various restrictions. */
17602 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
17604 cp_parameter_declarator
* parmdecl
)
17606 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
17607 return finish_template_type_parm (class_type_node
, id
);
17609 return error_mark_node
;
17613 finish_constrained_template_template_parm (tree proto
, tree id
)
17615 /* FIXME: This should probably be copied, and we may need to adjust
17616 the template parameter depths. */
17617 tree saved_parms
= current_template_parms
;
17618 begin_template_parm_list ();
17619 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
17620 end_template_parm_list ();
17622 tree parm
= finish_template_template_parm (class_type_node
, id
);
17623 current_template_parms
= saved_parms
;
17628 /* Finish parsing/processing a template template parameter by borrowing
17629 the template parameter list from the prototype parameter. */
17632 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
17635 cp_parameter_declarator
*parmdecl
)
17637 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
17638 return error_mark_node
;
17639 return finish_constrained_template_template_parm (proto
, id
);
17642 /* Create a new non-type template parameter from the given PARM
17646 cp_parser_constrained_non_type_template_parm (bool *is_non_type
,
17647 cp_parameter_declarator
*parm
)
17649 *is_non_type
= true;
17650 cp_declarator
*decl
= parm
->declarator
;
17651 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
17652 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
17653 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
17656 /* Build a constrained template parameter based on the PARMDECL
17657 declarator. The type of PARMDECL is the constrained type, which
17658 refers to the prototype template parameter that ultimately
17659 specifies the type of the declared parameter. */
17662 finish_constrained_parameter (cp_parser
*parser
,
17663 cp_parameter_declarator
*parmdecl
,
17666 tree decl
= parmdecl
->decl_specifiers
.type
;
17667 tree id
= get_unqualified_id (parmdecl
->declarator
);
17668 tree def
= parmdecl
->default_argument
;
17669 tree proto
= DECL_INITIAL (decl
);
17671 /* Build the parameter. Return an error if the declarator was invalid. */
17673 if (TREE_CODE (proto
) == TYPE_DECL
)
17674 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
17675 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
17676 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
17679 parm
= cp_parser_constrained_non_type_template_parm (is_non_type
, parmdecl
);
17680 if (parm
== error_mark_node
)
17681 return error_mark_node
;
17683 /* Finish the parameter decl and create a node attaching the
17684 default argument and constraint. */
17685 parm
= build_tree_list (def
, parm
);
17686 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
17691 /* Returns true if the parsed type actually represents the declaration
17692 of a type template-parameter. */
17695 declares_constrained_type_template_parameter (tree type
)
17697 return (is_constrained_parameter (type
)
17698 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
17701 /* Returns true if the parsed type actually represents the declaration of
17702 a template template-parameter. */
17705 declares_constrained_template_template_parameter (tree type
)
17707 return (is_constrained_parameter (type
)
17708 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
17711 /* Parse a default argument for a type template-parameter.
17712 Note that diagnostics are handled in cp_parser_template_parameter. */
17715 cp_parser_default_type_template_argument (cp_parser
*parser
)
17717 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
17719 /* Consume the `=' token. */
17720 cp_lexer_consume_token (parser
->lexer
);
17722 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17724 /* Tell cp_parser_lambda_expression this is a default argument. */
17725 auto lvf
= make_temp_override (parser
->local_variables_forbidden_p
);
17726 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
17728 /* Parse the default-argument. */
17729 push_deferring_access_checks (dk_no_deferred
);
17730 tree default_argument
= cp_parser_type_id (parser
,
17731 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
17733 pop_deferring_access_checks ();
17735 if (flag_concepts
&& type_uses_auto (default_argument
))
17737 error_at (token
->location
,
17738 "invalid use of %<auto%> in default template argument");
17739 return error_mark_node
;
17742 return default_argument
;
17745 /* Parse a default argument for a template template-parameter. */
17748 cp_parser_default_template_template_argument (cp_parser
*parser
)
17750 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
17754 /* Consume the `='. */
17755 cp_lexer_consume_token (parser
->lexer
);
17756 /* Parse the id-expression. */
17757 push_deferring_access_checks (dk_no_deferred
);
17758 /* save token before parsing the id-expression, for error
17760 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
17761 tree default_argument
17762 = cp_parser_id_expression (parser
,
17763 /*template_keyword_p=*/false,
17764 /*check_dependency_p=*/true,
17765 /*template_p=*/&is_template
,
17766 /*declarator_p=*/false,
17767 /*optional_p=*/false);
17768 if (TREE_CODE (default_argument
) == TYPE_DECL
)
17769 /* If the id-expression was a template-id that refers to
17770 a template-class, we already have the declaration here,
17771 so no further lookup is needed. */
17774 /* Look up the name. */
17776 = cp_parser_lookup_name (parser
, default_argument
,
17778 /*is_template=*/is_template
,
17779 /*is_namespace=*/false,
17780 /*check_dependency=*/true,
17781 /*ambiguous_decls=*/NULL
,
17783 /* See if the default argument is valid. */
17784 default_argument
= check_template_template_default_arg (default_argument
);
17785 pop_deferring_access_checks ();
17786 return default_argument
;
17789 /* Parse a template-parameter.
17791 template-parameter:
17793 parameter-declaration
17795 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
17796 the parameter. The TREE_PURPOSE is the default value, if any.
17797 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
17798 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
17799 set to true iff this parameter is a parameter pack. */
17802 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
17803 bool *is_parameter_pack
)
17806 cp_parameter_declarator
*parameter_declarator
;
17809 /* Assume it is a type parameter or a template parameter. */
17810 *is_non_type
= false;
17811 /* Assume it not a parameter pack. */
17812 *is_parameter_pack
= false;
17813 /* Peek at the next token. */
17814 token
= cp_lexer_peek_token (parser
->lexer
);
17815 /* If it is `template', we have a type-parameter. */
17816 if (token
->keyword
== RID_TEMPLATE
)
17817 return cp_parser_type_parameter (parser
, is_parameter_pack
);
17818 /* If it is `class' or `typename' we do not know yet whether it is a
17819 type parameter or a non-type parameter. Consider:
17821 template <typename T, typename T::X X> ...
17825 template <class C, class D*> ...
17827 Here, the first parameter is a type parameter, and the second is
17828 a non-type parameter. We can tell by looking at the token after
17829 the identifier -- if it is a `,', `=', or `>' then we have a type
17831 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
17833 /* Peek at the token after `class' or `typename'. */
17834 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17835 /* If it's an ellipsis, we have a template type parameter
17837 if (token
->type
== CPP_ELLIPSIS
)
17838 return cp_parser_type_parameter (parser
, is_parameter_pack
);
17839 /* If it's an identifier, skip it. */
17840 if (token
->type
== CPP_NAME
)
17841 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
17842 /* Now, see if the token looks like the end of a template
17844 if (token
->type
== CPP_COMMA
17845 || token
->type
== CPP_EQ
17846 || token
->type
== CPP_GREATER
)
17847 return cp_parser_type_parameter (parser
, is_parameter_pack
);
17850 /* Otherwise, it is a non-type parameter or a constrained parameter.
17854 When parsing a default template-argument for a non-type
17855 template-parameter, the first non-nested `>' is taken as the end
17856 of the template parameter-list rather than a greater-than
17858 parameter_declarator
17859 = cp_parser_parameter_declaration (parser
,
17860 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
17861 /*template_parm_p=*/true,
17862 /*parenthesized_p=*/NULL
);
17864 if (!parameter_declarator
)
17865 return error_mark_node
;
17867 /* If the parameter declaration is marked as a parameter pack, set
17868 *IS_PARAMETER_PACK to notify the caller. */
17869 if (parameter_declarator
->template_parameter_pack_p
)
17870 *is_parameter_pack
= true;
17872 if (parameter_declarator
->default_argument
)
17874 /* Can happen in some cases of erroneous input (c++/34892). */
17875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17876 /* Consume the `...' for better error recovery. */
17877 cp_lexer_consume_token (parser
->lexer
);
17880 /* The parameter may have been constrained type parameter. */
17881 if (is_constrained_parameter (parameter_declarator
))
17882 return finish_constrained_parameter (parser
,
17883 parameter_declarator
,
17886 // Now we're sure that the parameter is a non-type parameter.
17887 *is_non_type
= true;
17889 parm
= grokdeclarator (parameter_declarator
->declarator
,
17890 ¶meter_declarator
->decl_specifiers
,
17891 TPARM
, /*initialized=*/0,
17892 /*attrlist=*/NULL
);
17893 if (parm
== error_mark_node
)
17894 return error_mark_node
;
17896 return build_tree_list (parameter_declarator
->default_argument
, parm
);
17899 /* Parse a type-parameter.
17902 class identifier [opt]
17903 class identifier [opt] = type-id
17904 typename identifier [opt]
17905 typename identifier [opt] = type-id
17906 template < template-parameter-list > class identifier [opt]
17907 template < template-parameter-list > class identifier [opt]
17910 GNU Extension (variadic templates):
17913 class ... identifier [opt]
17914 typename ... identifier [opt]
17916 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
17917 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
17918 the declaration of the parameter.
17920 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
17923 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
17928 /* Look for a keyword to tell us what kind of parameter this is. */
17929 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
17931 return error_mark_node
;
17933 switch (token
->keyword
)
17939 tree default_argument
;
17941 /* If the next token is an ellipsis, we have a template
17943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17945 /* Consume the `...' token. */
17946 cp_lexer_consume_token (parser
->lexer
);
17947 maybe_warn_variadic_templates ();
17949 *is_parameter_pack
= true;
17952 /* If the next token is an identifier, then it names the
17954 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17955 identifier
= cp_parser_identifier (parser
);
17957 identifier
= NULL_TREE
;
17959 /* Create the parameter. */
17960 parameter
= finish_template_type_parm (class_type_node
, identifier
);
17962 /* If the next token is an `=', we have a default argument. */
17963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17966 = cp_parser_default_type_template_argument (parser
);
17968 /* Template parameter packs cannot have default
17970 if (*is_parameter_pack
)
17973 error_at (token
->location
,
17974 "template parameter pack %qD cannot have a "
17975 "default argument", identifier
);
17977 error_at (token
->location
,
17978 "template parameter packs cannot have "
17979 "default arguments");
17980 default_argument
= NULL_TREE
;
17982 else if (check_for_bare_parameter_packs (default_argument
))
17983 default_argument
= error_mark_node
;
17986 default_argument
= NULL_TREE
;
17988 /* Create the combined representation of the parameter and the
17989 default argument. */
17990 parameter
= build_tree_list (default_argument
, parameter
);
17997 tree default_argument
;
17999 /* Look for the `<'. */
18000 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
18001 /* Parse the template-parameter-list. */
18002 cp_parser_template_parameter_list (parser
);
18003 /* Look for the `>'. */
18004 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
18006 /* If template requirements are present, parse them. */
18009 tree reqs
= get_shorthand_constraints (current_template_parms
);
18010 if (tree dreqs
= cp_parser_requires_clause_opt (parser
, false))
18011 reqs
= combine_constraint_expressions (reqs
, dreqs
);
18012 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
18015 /* Look for the `class' or 'typename' keywords. */
18016 cp_parser_type_parameter_key (parser
);
18017 /* If the next token is an ellipsis, we have a template
18019 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18021 /* Consume the `...' token. */
18022 cp_lexer_consume_token (parser
->lexer
);
18023 maybe_warn_variadic_templates ();
18025 *is_parameter_pack
= true;
18027 /* If the next token is an `=', then there is a
18028 default-argument. If the next token is a `>', we are at
18029 the end of the parameter-list. If the next token is a `,',
18030 then we are at the end of this parameter. */
18031 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
18032 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
18033 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18035 identifier
= cp_parser_identifier (parser
);
18036 /* Treat invalid names as if the parameter were nameless. */
18037 if (identifier
== error_mark_node
)
18038 identifier
= NULL_TREE
;
18041 identifier
= NULL_TREE
;
18043 /* Create the template parameter. */
18044 parameter
= finish_template_template_parm (class_type_node
,
18047 /* If the next token is an `=', then there is a
18048 default-argument. */
18049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18052 = cp_parser_default_template_template_argument (parser
);
18054 /* Template parameter packs cannot have default
18056 if (*is_parameter_pack
)
18059 error_at (token
->location
,
18060 "template parameter pack %qD cannot "
18061 "have a default argument",
18064 error_at (token
->location
, "template parameter packs cannot "
18065 "have default arguments");
18066 default_argument
= NULL_TREE
;
18070 default_argument
= NULL_TREE
;
18072 /* Create the combined representation of the parameter and the
18073 default argument. */
18074 parameter
= build_tree_list (default_argument
, parameter
);
18079 gcc_unreachable ();
18086 /* Parse a template-id.
18089 template-name < template-argument-list [opt] >
18091 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18092 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18093 returned. Otherwise, if the template-name names a function, or set
18094 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18095 names a class, returns a TYPE_DECL for the specialization.
18097 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18098 uninstantiated templates. */
18101 cp_parser_template_id (cp_parser
*parser
,
18102 bool template_keyword_p
,
18103 bool check_dependency_p
,
18104 enum tag_types tag_type
,
18105 bool is_declaration
)
18110 cp_token_position start_of_id
= 0;
18111 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
18112 bool is_identifier
;
18114 /* If the next token corresponds to a template-id, there is no need
18116 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18118 if (token
->type
== CPP_TEMPLATE_ID
)
18120 cp_lexer_consume_token (parser
->lexer
);
18121 return saved_checks_value (token
->u
.tree_check_value
);
18124 /* Avoid performing name lookup if there is no possibility of
18125 finding a template-id. */
18126 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
18127 || (token
->type
== CPP_NAME
18128 && !cp_parser_nth_token_starts_template_argument_list_p
18131 cp_parser_error (parser
, "expected template-id");
18132 return error_mark_node
;
18135 /* Remember where the template-id starts. */
18136 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
18137 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
18139 push_deferring_access_checks (dk_deferred
);
18141 /* Parse the template-name. */
18142 is_identifier
= false;
18143 templ
= cp_parser_template_name (parser
, template_keyword_p
,
18144 check_dependency_p
,
18149 /* Push any access checks inside the firewall we're about to create. */
18150 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
18151 pop_deferring_access_checks ();
18152 if (templ
== error_mark_node
|| is_identifier
)
18155 /* Since we're going to preserve any side-effects from this parse, set up a
18156 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18157 in the template arguments. */
18158 tentative_firewall
firewall (parser
);
18159 reopen_deferring_access_checks (checks
);
18161 /* If we find the sequence `[:' after a template-name, it's probably
18162 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18163 parse correctly the argument list. */
18164 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
18165 == CPP_OPEN_SQUARE
)
18166 && next_token
->flags
& DIGRAPH
18167 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
18169 && !(next_token_2
->flags
& PREV_WHITE
))
18171 cp_parser_parse_tentatively (parser
);
18172 /* Change `:' into `::'. */
18173 next_token_2
->type
= CPP_SCOPE
;
18174 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18176 cp_lexer_consume_token (parser
->lexer
);
18178 /* Parse the arguments. */
18179 arguments
= cp_parser_enclosed_template_argument_list (parser
);
18180 if (!cp_parser_parse_definitely (parser
))
18182 /* If we couldn't parse an argument list, then we revert our changes
18183 and return simply an error. Maybe this is not a template-id
18185 next_token_2
->type
= CPP_COLON
;
18186 cp_parser_error (parser
, "expected %<<%>");
18187 pop_deferring_access_checks ();
18188 return error_mark_node
;
18190 /* Otherwise, emit an error about the invalid digraph, but continue
18191 parsing because we got our argument list. */
18192 if (permerror (next_token
->location
,
18193 "%<<::%> cannot begin a template-argument list"))
18195 static bool hint
= false;
18196 inform (next_token
->location
,
18197 "%<<:%> is an alternate spelling for %<[%>."
18198 " Insert whitespace between %<<%> and %<::%>");
18199 if (!hint
&& !flag_permissive
)
18201 inform (next_token
->location
, "(if you use %<-fpermissive%> "
18202 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18203 "accept your code)");
18210 /* Look for the `<' that starts the template-argument-list. */
18211 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
18213 pop_deferring_access_checks ();
18214 return error_mark_node
;
18216 /* Parse the arguments. */
18217 arguments
= cp_parser_enclosed_template_argument_list (parser
);
18219 if ((cxx_dialect
> cxx17
)
18220 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
18221 && !template_keyword_p
18222 && (cp_parser_error_occurred (parser
)
18223 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
18225 /* This didn't go well. */
18226 if (TREE_CODE (templ
) == FUNCTION_DECL
)
18228 /* C++20 says that "function-name < a;" is now ill-formed. */
18229 if (cp_parser_error_occurred (parser
))
18231 error_at (token
->location
, "invalid template-argument-list");
18232 inform (token
->location
, "function name as the left hand "
18233 "operand of %<<%> is ill-formed in C++20; wrap the "
18234 "function name in %<()%>");
18237 /* We expect "f<targs>" to be followed by "(args)". */
18238 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
18239 "expected %<(%> after template-argument-list");
18241 /* Purge all subsequent tokens. */
18242 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
18245 cp_parser_simulate_error (parser
);
18246 pop_deferring_access_checks ();
18247 return error_mark_node
;
18251 /* Set the location to be of the form:
18252 template-name < template-argument-list [opt] >
18253 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18254 with caret == start at the start of the template-name,
18255 ranging until the closing '>'. */
18256 location_t combined_loc
18257 = make_location (token
->location
, token
->location
, parser
->lexer
);
18259 /* Check for concepts autos where they don't belong. We could
18260 identify types in some cases of identifier TEMPL, looking ahead
18261 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18262 types. We reject them in functions, but if what we have is an
18263 identifier, even with none_type we can't conclude it's NOT a
18264 type, we have to wait for template substitution. */
18265 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
18266 template_id
= error_mark_node
;
18267 /* Build a representation of the specialization. */
18268 else if (identifier_p (templ
))
18269 template_id
= build_min_nt_loc (combined_loc
,
18272 else if (DECL_TYPE_TEMPLATE_P (templ
)
18273 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
18275 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18276 template (rather than some instantiation thereof) only if
18277 is not nested within some other construct. For example, in
18278 "template <typename T> void f(T) { A<T>::", A<T> is just an
18279 instantiation of A. */
18280 bool entering_scope
18281 = (template_parm_scope_p ()
18282 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
18284 = finish_template_type (templ
, arguments
, entering_scope
);
18286 else if (concept_definition_p (templ
))
18288 /* The caller will decide whether this is a concept check or type
18290 template_id
= build2_loc (combined_loc
, TEMPLATE_ID_EXPR
,
18291 boolean_type_node
, templ
, arguments
);
18293 else if (variable_template_p (templ
))
18295 template_id
= lookup_template_variable (templ
, arguments
);
18296 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
18297 SET_EXPR_LOCATION (template_id
, combined_loc
);
18299 else if (TREE_CODE (templ
) == TYPE_DECL
18300 && TREE_CODE (TREE_TYPE (templ
)) == TYPENAME_TYPE
)
18302 /* Some type template in dependent scope. */
18303 tree
&name
= TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ
));
18304 name
= build_min_nt_loc (combined_loc
,
18307 template_id
= templ
;
18311 /* If it's not a class-template or a template-template, it should be
18312 a function-template. */
18313 gcc_assert (OVL_P (templ
) || BASELINK_P (templ
));
18315 template_id
= lookup_template_function (templ
, arguments
);
18316 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
18317 SET_EXPR_LOCATION (template_id
, combined_loc
);
18320 /* If parsing tentatively, replace the sequence of tokens that makes
18321 up the template-id with a CPP_TEMPLATE_ID token. That way,
18322 should we re-parse the token stream, we will not have to repeat
18323 the effort required to do the parse, nor will we issue duplicate
18324 error messages about problems during instantiation of the
18327 /* Don't do this if we had a parse error in a declarator; re-parsing
18328 might succeed if a name changes meaning (60361). */
18329 && !(cp_parser_error_occurred (parser
)
18330 && cp_parser_parsing_tentatively (parser
)
18331 && parser
->in_declarator_p
))
18333 /* Reset the contents of the START_OF_ID token. */
18334 token
->type
= CPP_TEMPLATE_ID
;
18335 token
->location
= combined_loc
;
18337 /* Retrieve any deferred checks. Do not pop this access checks yet
18338 so the memory will not be reclaimed during token replacing below. */
18339 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
18340 token
->tree_check_p
= true;
18341 token
->u
.tree_check_value
->value
= template_id
;
18342 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
18343 token
->keyword
= RID_MAX
;
18345 /* Purge all subsequent tokens. */
18346 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
18348 /* ??? Can we actually assume that, if template_id ==
18349 error_mark_node, we will have issued a diagnostic to the
18350 user, as opposed to simply marking the tentative parse as
18352 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
18353 error_at (token
->location
, "parse error in template argument list");
18356 pop_to_parent_deferring_access_checks ();
18357 return template_id
;
18360 /* Like cp_parser_template_id, called in non-type context. */
18363 cp_parser_template_id_expr (cp_parser
*parser
,
18364 bool template_keyword_p
,
18365 bool check_dependency_p
,
18366 bool is_declaration
)
18368 tree x
= cp_parser_template_id (parser
, template_keyword_p
, check_dependency_p
,
18369 none_type
, is_declaration
);
18370 if (TREE_CODE (x
) == TEMPLATE_ID_EXPR
18371 && concept_check_p (x
))
18372 /* We didn't check the arguments in cp_parser_template_id; do that now. */
18373 return build_concept_id (x
);
18377 /* Parse a template-name.
18382 The standard should actually say:
18386 operator-function-id
18388 A defect report has been filed about this issue.
18390 A conversion-function-id cannot be a template name because they cannot
18391 be part of a template-id. In fact, looking at this code:
18393 a.operator K<int>()
18395 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18396 It is impossible to call a templated conversion-function-id with an
18397 explicit argument list, since the only allowed template parameter is
18398 the type to which it is converting.
18400 If TEMPLATE_KEYWORD_P is true, then we have just seen the
18401 `template' keyword, in a construction like:
18405 In that case `f' is taken to be a template-name, even though there
18406 is no way of knowing for sure.
18408 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18409 name refers to a set of overloaded functions, at least one of which
18410 is a template, or an IDENTIFIER_NODE with the name of the template,
18411 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
18412 names are looked up inside uninstantiated templates. */
18415 cp_parser_template_name (cp_parser
* parser
,
18416 bool template_keyword_p
,
18417 bool check_dependency_p
,
18418 bool is_declaration
,
18419 enum tag_types tag_type
,
18420 bool *is_identifier
)
18424 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18426 /* If the next token is `operator', then we have either an
18427 operator-function-id or a conversion-function-id. */
18428 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
18430 /* We don't know whether we're looking at an
18431 operator-function-id or a conversion-function-id. */
18432 cp_parser_parse_tentatively (parser
);
18433 /* Try an operator-function-id. */
18434 identifier
= cp_parser_operator_function_id (parser
);
18435 /* If that didn't work, try a conversion-function-id. */
18436 if (!cp_parser_parse_definitely (parser
))
18438 cp_parser_error (parser
, "expected template-name");
18439 return error_mark_node
;
18442 /* Look for the identifier. */
18444 identifier
= cp_parser_identifier (parser
);
18446 /* If we didn't find an identifier, we don't have a template-id. */
18447 if (identifier
== error_mark_node
)
18448 return error_mark_node
;
18450 /* If the name immediately followed the `template' keyword, then it
18451 is a template-name. However, if the next token is not `<', then
18452 we do not treat it as a template-name, since it is not being used
18453 as part of a template-id. This enables us to handle constructs
18456 template <typename T> struct S { S(); };
18457 template <typename T> S<T>::S();
18459 correctly. We would treat `S' as a template -- if it were `S<T>'
18460 -- but we do not if there is no `<'. */
18462 if (processing_template_decl
18463 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
18465 /* In a declaration, in a dependent context, we pretend that the
18466 "template" keyword was present in order to improve error
18467 recovery. For example, given:
18469 template <typename T> void f(T::X<int>);
18471 we want to treat "X<int>" as a template-id. */
18473 && !template_keyword_p
18474 && parser
->scope
&& TYPE_P (parser
->scope
)
18475 && check_dependency_p
18476 && dependent_scope_p (parser
->scope
)
18477 /* Do not do this for dtors (or ctors), since they never
18478 need the template keyword before their name. */
18479 && !constructor_name_p (identifier
, parser
->scope
))
18481 cp_token_position start
= 0;
18483 /* Explain what went wrong. */
18484 error_at (token
->location
, "non-template %qD used as template",
18486 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
18487 parser
->scope
, identifier
);
18488 /* If parsing tentatively, find the location of the "<" token. */
18489 if (cp_parser_simulate_error (parser
))
18490 start
= cp_lexer_token_position (parser
->lexer
, true);
18491 /* Parse the template arguments so that we can issue error
18492 messages about them. */
18493 cp_lexer_consume_token (parser
->lexer
);
18494 cp_parser_enclosed_template_argument_list (parser
);
18495 /* Skip tokens until we find a good place from which to
18496 continue parsing. */
18497 cp_parser_skip_to_closing_parenthesis (parser
,
18498 /*recovering=*/true,
18500 /*consume_paren=*/false);
18501 /* If parsing tentatively, permanently remove the
18502 template argument list. That will prevent duplicate
18503 error messages from being issued about the missing
18504 "template" keyword. */
18506 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
18508 *is_identifier
= true;
18509 parser
->context
->object_type
= NULL_TREE
;
18513 /* If the "template" keyword is present, then there is generally
18514 no point in doing name-lookup, so we just return IDENTIFIER.
18515 But, if the qualifying scope is non-dependent then we can
18516 (and must) do name-lookup normally. */
18517 if (template_keyword_p
)
18519 tree scope
= (parser
->scope
? parser
->scope
18520 : parser
->context
->object_type
);
18521 if (scope
&& TYPE_P (scope
)
18522 && (!CLASS_TYPE_P (scope
)
18523 || (check_dependency_p
&& dependent_type_p (scope
))))
18525 /* We're optimizing away the call to cp_parser_lookup_name, but
18526 we still need to do this. */
18527 parser
->object_scope
= parser
->context
->object_type
;
18528 parser
->context
->object_type
= NULL_TREE
;
18534 /* cp_parser_lookup_name clears OBJECT_TYPE. */
18535 tree scope
= (parser
->scope
? parser
->scope
18536 : parser
->context
->object_type
);
18538 /* Look up the name. */
18539 decl
= cp_parser_lookup_name (parser
, identifier
,
18541 /*is_template=*/true,
18542 /*is_namespace=*/false,
18543 check_dependency_p
,
18544 /*ambiguous_decls=*/NULL
,
18547 decl
= strip_using_decl (decl
);
18549 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18550 template-argument-list if it follows a name that is not a
18551 conversion-function-id and
18552 - that follows the keyword template or a ~ after a nested-name-specifier or
18553 in a class member access expression, or
18554 - for which name lookup finds the injected-class-name of a class template
18555 or finds any declaration of a template, or
18556 - that is an unqualified name for which name lookup either finds one or
18557 more functions or finds nothing, or
18558 - that is a terminal name in a using-declarator (9.9), in a declarator-id
18559 (9.3.4), or in a type-only context other than a nested-name-specifier
18562 /* If DECL is a template, then the name was a template-name. */
18563 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
18565 if ((TREE_DEPRECATED (decl
) || TREE_UNAVAILABLE (decl
))
18566 && deprecated_state
!= UNAVAILABLE_DEPRECATED_SUPPRESS
)
18568 tree d
= DECL_TEMPLATE_RESULT (decl
);
18570 if (TREE_CODE (d
) == TYPE_DECL
)
18571 attr
= TYPE_ATTRIBUTES (TREE_TYPE (d
));
18573 attr
= DECL_ATTRIBUTES (d
);
18574 if (TREE_UNAVAILABLE (decl
))
18576 attr
= lookup_attribute ("unavailable", attr
);
18577 error_unavailable_use (decl
, attr
);
18579 else if (TREE_DEPRECATED (decl
)
18580 && deprecated_state
!= DEPRECATED_SUPPRESS
)
18582 attr
= lookup_attribute ("deprecated", attr
);
18583 warn_deprecated_use (decl
, attr
);
18589 /* Look through an overload set for any templates. */
18590 bool found
= false;
18592 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
18593 !found
&& iter
; ++iter
)
18594 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
18597 /* "an unqualified name for which name lookup either finds one or more
18598 functions or finds nothing". */
18600 && (cxx_dialect
> cxx17
)
18602 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
18603 && tag_type
== none_type
)
18605 /* The "more functions" case. Just use the OVERLOAD as normally.
18606 We don't use is_overloaded_fn here to avoid considering
18608 if (TREE_CODE (decl
) == OVERLOAD
18609 /* Name lookup found one function. */
18610 || TREE_CODE (decl
) == FUNCTION_DECL
18611 /* Name lookup found nothing. */
18612 || decl
== error_mark_node
)
18616 /* "in a type-only context" */
18617 if (!found
&& scope
18618 && tag_type
!= none_type
18619 && dependentish_scope_p (scope
)
18620 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
18625 /* The name does not name a template. */
18626 cp_parser_error (parser
, "expected template-name");
18627 return error_mark_node
;
18629 else if (decl
== error_mark_node
)
18630 /* Repeat the lookup at instantiation time. */
18637 /* Parse a template-argument-list.
18639 template-argument-list:
18640 template-argument ... [opt]
18641 template-argument-list , template-argument ... [opt]
18643 Returns a TREE_VEC containing the arguments. */
18646 cp_parser_template_argument_list (cp_parser
* parser
)
18648 bool saved_in_template_argument_list_p
;
18650 bool saved_non_ice_p
;
18652 /* Don't create location wrapper nodes within a template-argument-list. */
18653 auto_suppress_location_wrappers sentinel
;
18655 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
18656 parser
->in_template_argument_list_p
= true;
18657 /* Even if the template-id appears in an integral
18658 constant-expression, the contents of the argument list do
18660 saved_ice_p
= parser
->integral_constant_expression_p
;
18661 parser
->integral_constant_expression_p
= false;
18662 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
18663 parser
->non_integral_constant_expression_p
= false;
18665 /* Parse the arguments. */
18666 auto_vec
<tree
, 10> args
;
18669 if (!args
.is_empty ())
18670 /* Consume the comma. */
18671 cp_lexer_consume_token (parser
->lexer
);
18673 /* Parse the template-argument. */
18674 tree argument
= cp_parser_template_argument (parser
);
18676 /* If the next token is an ellipsis, we're expanding a template
18678 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18680 if (argument
== error_mark_node
)
18682 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18683 error_at (token
->location
,
18684 "expected parameter pack before %<...%>");
18686 /* Consume the `...' token. */
18687 cp_lexer_consume_token (parser
->lexer
);
18689 /* Make the argument into a TYPE_PACK_EXPANSION or
18690 EXPR_PACK_EXPANSION. */
18691 argument
= make_pack_expansion (argument
);
18694 args
.safe_push (argument
);
18696 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
18698 int n_args
= args
.length ();
18699 tree vec
= make_tree_vec (n_args
);
18701 for (int i
= 0; i
< n_args
; i
++)
18702 TREE_VEC_ELT (vec
, i
) = args
[i
];
18704 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
18705 parser
->integral_constant_expression_p
= saved_ice_p
;
18706 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
18708 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
18712 /* Parse a template-argument.
18715 assignment-expression
18719 The representation is that of an assignment-expression, type-id, or
18720 id-expression -- except that the qualified id-expression is
18721 evaluated, so that the value returned is either a DECL or an
18724 Although the standard says "assignment-expression", it forbids
18725 throw-expressions or assignments in the template argument.
18726 Therefore, we use "conditional-expression" instead. */
18729 cp_parser_template_argument (cp_parser
* parser
)
18734 bool maybe_type_id
= false;
18735 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
18736 location_t loc
= 0;
18739 /* There's really no way to know what we're looking at, so we just
18740 try each alternative in order.
18744 In a template-argument, an ambiguity between a type-id and an
18745 expression is resolved to a type-id, regardless of the form of
18746 the corresponding template-parameter.
18748 Therefore, we try a type-id first. */
18749 cp_parser_parse_tentatively (parser
);
18750 argument
= cp_parser_template_type_arg (parser
);
18751 /* If there was no error parsing the type-id but the next token is a
18752 '>>', our behavior depends on which dialect of C++ we're
18753 parsing. In C++98, we probably found a typo for '> >'. But there
18754 are type-id which are also valid expressions. For instance:
18756 struct X { int operator >> (int); };
18757 template <int V> struct Foo {};
18760 Here 'X()' is a valid type-id of a function type, but the user just
18761 wanted to write the expression "X() >> 5". Thus, we remember that we
18762 found a valid type-id, but we still try to parse the argument as an
18763 expression to see what happens.
18765 In C++0x, the '>>' will be considered two separate '>'
18767 if (!cp_parser_error_occurred (parser
)
18768 && cxx_dialect
== cxx98
18769 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
18771 maybe_type_id
= true;
18772 cp_parser_abort_tentative_parse (parser
);
18776 /* If the next token isn't a `,' or a `>', then this argument wasn't
18777 really finished. This means that the argument is not a valid
18779 if (!cp_parser_next_token_ends_template_argument_p (parser
))
18780 cp_parser_error (parser
, "expected template-argument");
18781 /* If that worked, we're done. */
18782 if (cp_parser_parse_definitely (parser
))
18785 /* We're still not sure what the argument will be. */
18786 cp_parser_parse_tentatively (parser
);
18787 /* Try a template. */
18788 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
18789 argument
= cp_parser_id_expression (parser
,
18790 /*template_keyword_p=*/false,
18791 /*check_dependency_p=*/true,
18793 /*declarator_p=*/false,
18794 /*optional_p=*/false);
18795 /* If the next token isn't a `,' or a `>', then this argument wasn't
18796 really finished. */
18797 if (!cp_parser_next_token_ends_template_argument_p (parser
))
18798 cp_parser_error (parser
, "expected template-argument");
18799 if (!cp_parser_error_occurred (parser
))
18801 /* Figure out what is being referred to. If the id-expression
18802 was for a class template specialization, then we will have a
18803 TYPE_DECL at this point. There is no need to do name lookup
18804 at this point in that case. */
18805 if (TREE_CODE (argument
) != TYPE_DECL
)
18806 argument
= cp_parser_lookup_name (parser
, argument
,
18808 /*is_template=*/template_p
,
18809 /*is_namespace=*/false,
18810 /*check_dependency=*/true,
18811 /*ambiguous_decls=*/NULL
,
18812 argument_start_token
->location
);
18813 if (TREE_CODE (argument
) != TEMPLATE_DECL
18814 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
18815 cp_parser_error (parser
, "expected template-name");
18817 if (cp_parser_parse_definitely (parser
))
18819 if (TREE_UNAVAILABLE (argument
))
18820 error_unavailable_use (argument
, NULL_TREE
);
18821 else if (TREE_DEPRECATED (argument
))
18822 warn_deprecated_use (argument
, NULL_TREE
);
18825 /* It must be a non-type argument. In C++17 any constant-expression is
18827 if (cxx_dialect
> cxx14
)
18830 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
18832 -- an integral constant-expression of integral or enumeration
18835 -- the name of a non-type template-parameter; or
18837 -- the name of an object or function with external linkage...
18839 -- the address of an object or function with external linkage...
18841 -- a pointer to member... */
18842 /* Look for a non-type template parameter. */
18843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18845 cp_parser_parse_tentatively (parser
);
18846 argument
= cp_parser_primary_expression (parser
,
18847 /*address_p=*/false,
18849 /*template_arg_p=*/true,
18851 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
18852 || !cp_parser_next_token_ends_template_argument_p (parser
))
18853 cp_parser_simulate_error (parser
);
18854 if (cp_parser_parse_definitely (parser
))
18858 /* If the next token is "&", the argument must be the address of an
18859 object or function with external linkage. */
18860 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
18863 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18864 cp_lexer_consume_token (parser
->lexer
);
18866 /* See if we might have an id-expression. */
18867 token
= cp_lexer_peek_token (parser
->lexer
);
18868 if (token
->type
== CPP_NAME
18869 || token
->keyword
== RID_OPERATOR
18870 || token
->type
== CPP_SCOPE
18871 || token
->type
== CPP_TEMPLATE_ID
18872 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
18874 cp_parser_parse_tentatively (parser
);
18875 argument
= cp_parser_primary_expression (parser
,
18878 /*template_arg_p=*/true,
18880 if (cp_parser_error_occurred (parser
)
18881 || !cp_parser_next_token_ends_template_argument_p (parser
))
18882 cp_parser_abort_tentative_parse (parser
);
18887 if (INDIRECT_REF_P (argument
))
18889 /* Strip the dereference temporarily. */
18890 gcc_assert (REFERENCE_REF_P (argument
));
18891 argument
= TREE_OPERAND (argument
, 0);
18894 /* If we're in a template, we represent a qualified-id referring
18895 to a static data member as a SCOPE_REF even if the scope isn't
18896 dependent so that we can check access control later. */
18898 if (TREE_CODE (probe
) == SCOPE_REF
)
18899 probe
= TREE_OPERAND (probe
, 1);
18902 /* A variable without external linkage might still be a
18903 valid constant-expression, so no error is issued here
18904 if the external-linkage check fails. */
18905 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
18906 cp_parser_simulate_error (parser
);
18908 else if (is_overloaded_fn (argument
))
18909 /* All overloaded functions are allowed; if the external
18910 linkage test does not pass, an error will be issued
18914 && (TREE_CODE (argument
) == OFFSET_REF
18915 || TREE_CODE (argument
) == SCOPE_REF
))
18916 /* A pointer-to-member. */
18918 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
18921 cp_parser_simulate_error (parser
);
18923 if (cp_parser_parse_definitely (parser
))
18926 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
18927 NULL_TREE
, tf_warning_or_error
);
18929 argument
= convert_from_reference (argument
);
18934 /* If the argument started with "&", there are no other valid
18935 alternatives at this point. */
18938 cp_parser_error (parser
, "invalid non-type template argument");
18939 return error_mark_node
;
18943 /* If the argument wasn't successfully parsed as a type-id followed
18944 by '>>', the argument can only be a constant expression now.
18945 Otherwise, we try parsing the constant-expression tentatively,
18946 because the argument could really be a type-id. */
18948 cp_parser_parse_tentatively (parser
);
18950 if (cxx_dialect
<= cxx14
)
18951 argument
= cp_parser_constant_expression (parser
);
18954 /* In C++20, we can encounter a braced-init-list. */
18955 if (cxx_dialect
>= cxx20
18956 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18958 bool expr_non_constant_p
;
18959 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
18962 /* With C++17 generalized non-type template arguments we need to handle
18963 lvalue constant expressions, too. */
18964 argument
= cp_parser_assignment_expression (parser
);
18965 require_potential_constant_expression (argument
);
18968 if (!maybe_type_id
)
18970 if (!cp_parser_next_token_ends_template_argument_p (parser
))
18971 cp_parser_error (parser
, "expected template-argument");
18972 if (cp_parser_parse_definitely (parser
))
18974 /* We did our best to parse the argument as a non type-id, but that
18975 was the only alternative that matched (albeit with a '>' after
18976 it). We can assume it's just a typo from the user, and a
18977 diagnostic will then be issued. */
18978 return cp_parser_template_type_arg (parser
);
18981 /* Parse an explicit-instantiation.
18983 explicit-instantiation:
18984 template declaration
18986 Although the standard says `declaration', what it really means is:
18988 explicit-instantiation:
18989 template decl-specifier-seq [opt] declarator [opt] ;
18991 Things like `template int S<int>::i = 5, int S<double>::j;' are not
18992 supposed to be allowed. A defect report has been filed about this
18997 explicit-instantiation:
18998 storage-class-specifier template
18999 decl-specifier-seq [opt] declarator [opt] ;
19000 function-specifier template
19001 decl-specifier-seq [opt] declarator [opt] ; */
19004 cp_parser_explicit_instantiation (cp_parser
* parser
)
19006 int declares_class_or_enum
;
19007 cp_decl_specifier_seq decl_specifiers
;
19008 tree extension_specifier
= NULL_TREE
;
19010 timevar_push (TV_TEMPLATE_INST
);
19012 /* Look for an (optional) storage-class-specifier or
19013 function-specifier. */
19014 if (cp_parser_allow_gnu_extensions_p (parser
))
19016 extension_specifier
19017 = cp_parser_storage_class_specifier_opt (parser
);
19018 if (!extension_specifier
)
19019 extension_specifier
19020 = cp_parser_function_specifier_opt (parser
,
19021 /*decl_specs=*/NULL
);
19024 /* Look for the `template' keyword. */
19025 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
19026 /* Let the front end know that we are processing an explicit
19028 begin_explicit_instantiation ();
19029 /* [temp.explicit] says that we are supposed to ignore access
19030 control while processing explicit instantiation directives. */
19031 push_deferring_access_checks (dk_no_check
);
19032 /* Parse a decl-specifier-seq. */
19033 cp_parser_decl_specifier_seq (parser
,
19034 CP_PARSER_FLAGS_OPTIONAL
,
19036 &declares_class_or_enum
);
19038 cp_omp_declare_simd_data odsd
;
19039 if (decl_specifiers
.attributes
&& (flag_openmp
|| flag_openmp_simd
))
19040 cp_parser_handle_directive_omp_attributes (parser
,
19041 &decl_specifiers
.attributes
,
19044 /* If there was exactly one decl-specifier, and it declared a class,
19045 and there's no declarator, then we have an explicit type
19047 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
19049 tree type
= check_tag_decl (&decl_specifiers
,
19050 /*explicit_type_instantiation_p=*/true);
19051 /* Turn access control back on for names used during
19052 template instantiation. */
19053 pop_deferring_access_checks ();
19055 do_type_instantiation (type
, extension_specifier
,
19056 /*complain=*/tf_error
);
19060 cp_declarator
*declarator
;
19063 /* Parse the declarator. */
19065 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19066 CP_PARSER_FLAGS_NONE
,
19067 /*ctor_dtor_or_conv_p=*/NULL
,
19068 /*parenthesized_p=*/NULL
,
19069 /*member_p=*/false,
19070 /*friend_p=*/false,
19071 /*static_p=*/false);
19072 if (declares_class_or_enum
& 2)
19073 cp_parser_check_for_definition_in_return_type (declarator
,
19074 decl_specifiers
.type
,
19075 decl_specifiers
.locations
[ds_type_spec
]);
19076 if (declarator
!= cp_error_declarator
)
19078 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
19079 permerror (decl_specifiers
.locations
[ds_inline
],
19080 "explicit instantiation shall not use"
19081 " %<inline%> specifier");
19082 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
19083 permerror (decl_specifiers
.locations
[ds_constexpr
],
19084 "explicit instantiation shall not use"
19085 " %<constexpr%> specifier");
19086 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_consteval
))
19087 permerror (decl_specifiers
.locations
[ds_consteval
],
19088 "explicit instantiation shall not use"
19089 " %<consteval%> specifier");
19091 decl
= grokdeclarator (declarator
, &decl_specifiers
,
19092 NORMAL
, 0, &decl_specifiers
.attributes
);
19093 /* Turn access control back on for names used during
19094 template instantiation. */
19095 pop_deferring_access_checks ();
19096 /* Do the explicit instantiation. */
19097 do_decl_instantiation (decl
, extension_specifier
);
19101 pop_deferring_access_checks ();
19102 /* Skip the body of the explicit instantiation. */
19103 cp_parser_skip_to_end_of_statement (parser
);
19106 /* We're done with the instantiation. */
19107 end_explicit_instantiation ();
19109 cp_parser_consume_semicolon_at_end_of_statement (parser
);
19111 timevar_pop (TV_TEMPLATE_INST
);
19113 cp_finalize_omp_declare_simd (parser
, &odsd
);
19116 /* Parse an explicit-specialization.
19118 explicit-specialization:
19119 template < > declaration
19121 Although the standard says `declaration', what it really means is:
19123 explicit-specialization:
19124 template <> decl-specifier [opt] init-declarator [opt] ;
19125 template <> function-definition
19126 template <> explicit-specialization
19127 template <> template-declaration */
19130 cp_parser_explicit_specialization (cp_parser
* parser
)
19132 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19134 /* Look for the `template' keyword. */
19135 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
19136 /* Look for the `<'. */
19137 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
19138 /* Look for the `>'. */
19139 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
19140 /* We have processed another parameter list. */
19141 ++parser
->num_template_parameter_lists
;
19145 A template ... explicit specialization ... shall not have C
19147 bool need_lang_pop
= current_lang_name
== lang_name_c
;
19150 error_at (token
->location
, "template specialization with C linkage");
19151 maybe_show_extern_c_location ();
19153 /* Give it C++ linkage to avoid confusing other parts of the
19155 push_lang_context (lang_name_cplusplus
);
19158 /* Let the front end know that we are beginning a specialization. */
19159 if (begin_specialization ())
19161 /* If the next keyword is `template', we need to figure out
19162 whether or not we're looking a template-declaration. */
19163 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
19165 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
19166 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
19167 cp_parser_template_declaration_after_export (parser
,
19168 /*member_p=*/false);
19170 cp_parser_explicit_specialization (parser
);
19173 /* Parse the dependent declaration. */
19174 cp_parser_single_declaration (parser
,
19176 /*member_p=*/false,
19177 /*explicit_specialization_p=*/true,
19178 /*friend_p=*/NULL
);
19181 /* We're done with the specialization. */
19182 end_specialization ();
19184 /* For the erroneous case of a template with C linkage, we pushed an
19185 implicit C++ linkage scope; exit that scope now. */
19187 pop_lang_context ();
19189 /* We're done with this parameter list. */
19190 --parser
->num_template_parameter_lists
;
19193 /* Preserve the attributes across a garbage collect (by making it a GC
19194 root), which can occur when parsing a member function. */
19196 static GTY(()) vec
<tree
, va_gc
> *cp_parser_decl_specs_attrs
;
19198 /* Parse a type-specifier.
19201 simple-type-specifier
19204 elaborated-type-specifier
19212 Returns a representation of the type-specifier. For a
19213 class-specifier, enum-specifier, or elaborated-type-specifier, a
19214 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19216 The parser flags FLAGS is used to control type-specifier parsing.
19218 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19219 in a decl-specifier-seq.
19221 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19222 class-specifier, enum-specifier, or elaborated-type-specifier, then
19223 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19224 if a type is declared; 2 if it is defined. Otherwise, it is set to
19227 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19228 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19229 is set to FALSE. */
19232 cp_parser_type_specifier (cp_parser
* parser
,
19233 cp_parser_flags flags
,
19234 cp_decl_specifier_seq
*decl_specs
,
19235 bool is_declaration
,
19236 int* declares_class_or_enum
,
19237 bool* is_cv_qualifier
)
19239 tree type_spec
= NULL_TREE
;
19242 cp_decl_spec ds
= ds_last
;
19244 /* Assume this type-specifier does not declare a new type. */
19245 if (declares_class_or_enum
)
19246 *declares_class_or_enum
= 0;
19247 /* And that it does not specify a cv-qualifier. */
19248 if (is_cv_qualifier
)
19249 *is_cv_qualifier
= false;
19250 /* Peek at the next token. */
19251 token
= cp_lexer_peek_token (parser
->lexer
);
19253 /* If we're looking at a keyword, we can use that to guide the
19254 production we choose. */
19255 keyword
= token
->keyword
;
19259 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
19260 goto elaborated_type_specifier
;
19262 /* Look for the enum-specifier. */
19263 type_spec
= cp_parser_enum_specifier (parser
);
19264 /* If that worked, we're done. */
19267 if (declares_class_or_enum
)
19268 *declares_class_or_enum
= 2;
19270 cp_parser_set_decl_spec_type (decl_specs
,
19273 /*type_definition_p=*/true);
19277 goto elaborated_type_specifier
;
19279 /* Any of these indicate either a class-specifier, or an
19280 elaborated-type-specifier. */
19284 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
19285 goto elaborated_type_specifier
;
19287 /* Parse tentatively so that we can back up if we don't find a
19288 class-specifier. */
19289 cp_parser_parse_tentatively (parser
);
19290 if (decl_specs
->attributes
)
19291 vec_safe_push (cp_parser_decl_specs_attrs
, decl_specs
->attributes
);
19292 /* Look for the class-specifier. */
19293 type_spec
= cp_parser_class_specifier (parser
);
19294 if (decl_specs
->attributes
)
19295 cp_parser_decl_specs_attrs
->pop ();
19296 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
19297 /* If that worked, we're done. */
19298 if (cp_parser_parse_definitely (parser
))
19300 if (declares_class_or_enum
)
19301 *declares_class_or_enum
= 2;
19303 cp_parser_set_decl_spec_type (decl_specs
,
19306 /*type_definition_p=*/true);
19310 /* Fall through. */
19311 elaborated_type_specifier
:
19312 /* We're declaring (not defining) a class or enum. */
19313 if (declares_class_or_enum
)
19314 *declares_class_or_enum
= 1;
19316 /* Fall through. */
19318 /* Look for an elaborated-type-specifier. */
19320 = (cp_parser_elaborated_type_specifier
19322 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
19325 cp_parser_set_decl_spec_type (decl_specs
,
19328 /*type_definition_p=*/false);
19333 if (is_cv_qualifier
)
19334 *is_cv_qualifier
= true;
19339 if (is_cv_qualifier
)
19340 *is_cv_qualifier
= true;
19345 if (is_cv_qualifier
)
19346 *is_cv_qualifier
= true;
19350 /* The `__complex__' keyword is a GNU extension. */
19358 /* Handle simple keywords. */
19363 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
19364 decl_specs
->any_specifiers_p
= true;
19366 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19369 /* If we do not already have a type-specifier, assume we are looking
19370 at a simple-type-specifier. */
19371 type_spec
= cp_parser_simple_type_specifier (parser
,
19375 /* If we didn't find a type-specifier, and a type-specifier was not
19376 optional in this context, issue an error message. */
19377 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
19379 cp_parser_error (parser
, "expected type specifier");
19380 return error_mark_node
;
19386 /* Parse a simple-type-specifier.
19388 simple-type-specifier:
19389 :: [opt] nested-name-specifier [opt] type-name
19390 :: [opt] nested-name-specifier template template-id
19405 simple-type-specifier:
19407 decltype ( expression )
19410 __underlying_type ( type-id )
19414 nested-name-specifier(opt) template-name
19418 simple-type-specifier:
19420 __typeof__ unary-expression
19421 __typeof__ ( type-id )
19422 __typeof__ ( type-id ) { initializer-list , [opt] }
19424 Concepts Extension:
19426 simple-type-specifier:
19427 constrained-type-specifier
19429 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
19430 appropriately updated. */
19433 cp_parser_simple_type_specifier (cp_parser
* parser
,
19434 cp_decl_specifier_seq
*decl_specs
,
19435 cp_parser_flags flags
)
19437 tree type
= NULL_TREE
;
19441 /* Peek at the next token. */
19442 token
= cp_lexer_peek_token (parser
->lexer
);
19444 /* If we're looking at a keyword, things are easy. */
19445 switch (token
->keyword
)
19449 decl_specs
->explicit_char_p
= true;
19450 type
= char_type_node
;
19453 type
= char8_type_node
;
19456 type
= char16_type_node
;
19459 type
= char32_type_node
;
19462 type
= wchar_type_node
;
19465 type
= boolean_type_node
;
19468 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
19469 type
= short_integer_type_node
;
19473 decl_specs
->explicit_int_p
= true;
19474 type
= integer_type_node
;
19480 idx
= token
->keyword
- RID_INT_N_0
;
19481 if (! int_n_enabled_p
[idx
])
19485 decl_specs
->explicit_intN_p
= true;
19486 decl_specs
->int_n_idx
= idx
;
19487 /* Check if the alternate "__intN__" form has been used instead of
19489 if (startswith (IDENTIFIER_POINTER (token
->u
.value
)
19490 + (IDENTIFIER_LENGTH (token
->u
.value
) - 2), "__"))
19491 decl_specs
->int_n_alt
= true;
19493 type
= int_n_trees
[idx
].signed_type
;
19497 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
19498 type
= long_integer_type_node
;
19501 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
19502 type
= integer_type_node
;
19505 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
19506 type
= unsigned_type_node
;
19509 type
= float_type_node
;
19512 type
= double_type_node
;
19515 type
= void_type_node
;
19519 maybe_warn_cpp0x (CPP0X_AUTO
);
19520 if (parser
->auto_is_implicit_function_template_parm_p
)
19522 /* The 'auto' might be the placeholder return type for a function decl
19523 with trailing return type. */
19524 bool have_trailing_return_fn_decl
= false;
19526 cp_parser_parse_tentatively (parser
);
19527 cp_lexer_consume_token (parser
->lexer
);
19528 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
19529 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
19530 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
19531 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
19533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
19535 cp_lexer_consume_token (parser
->lexer
);
19536 cp_parser_skip_to_closing_parenthesis (parser
,
19537 /*recovering*/false,
19539 /*consume_paren*/true);
19543 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
19545 have_trailing_return_fn_decl
= true;
19549 cp_lexer_consume_token (parser
->lexer
);
19551 cp_parser_abort_tentative_parse (parser
);
19553 if (have_trailing_return_fn_decl
)
19555 type
= make_auto ();
19559 if (cxx_dialect
>= cxx14
)
19561 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
19562 type
= TREE_TYPE (type
);
19565 type
= error_mark_node
;
19567 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
19569 if (cxx_dialect
< cxx14
)
19570 error_at (token
->location
,
19571 "use of %<auto%> in lambda parameter declaration "
19572 "only available with "
19573 "%<-std=c++14%> or %<-std=gnu++14%>");
19575 else if (cxx_dialect
< cxx14
)
19576 error_at (token
->location
,
19577 "use of %<auto%> in parameter declaration "
19578 "only available with "
19579 "%<-std=c++14%> or %<-std=gnu++14%>");
19580 else if (!flag_concepts
)
19581 pedwarn (token
->location
, 0,
19582 "use of %<auto%> in parameter declaration "
19583 "only available with %<-std=c++20%> or %<-fconcepts%>");
19586 type
= make_auto ();
19590 /* Since DR 743, decltype can either be a simple-type-specifier by
19591 itself or begin a nested-name-specifier. Parsing it will replace
19592 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19593 handling below decide what to do. */
19594 cp_parser_decltype (parser
);
19595 cp_lexer_set_token_position (parser
->lexer
, token
);
19599 /* Consume the `typeof' token. */
19600 cp_lexer_consume_token (parser
->lexer
);
19601 /* Parse the operand to `typeof'. */
19602 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
19603 /* If it is not already a TYPE, take its type. */
19604 if (!TYPE_P (type
))
19605 type
= finish_typeof (type
);
19608 cp_parser_set_decl_spec_type (decl_specs
, type
,
19610 /*type_definition_p=*/false);
19614 case RID_UNDERLYING_TYPE
:
19615 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
19617 cp_parser_set_decl_spec_type (decl_specs
, type
,
19619 /*type_definition_p=*/false);
19624 case RID_DIRECT_BASES
:
19625 type
= cp_parser_trait_expr (parser
, token
->keyword
);
19627 cp_parser_set_decl_spec_type (decl_specs
, type
,
19629 /*type_definition_p=*/false);
19635 /* If token is an already-parsed decltype not followed by ::,
19636 it's a simple-type-specifier. */
19637 if (token
->type
== CPP_DECLTYPE
19638 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
19640 type
= saved_checks_value (token
->u
.tree_check_value
);
19643 cp_parser_set_decl_spec_type (decl_specs
, type
,
19645 /*type_definition_p=*/false);
19646 /* Remember that we are handling a decltype in order to
19647 implement the resolution of DR 1510 when the argument
19648 isn't instantiation dependent. */
19649 decl_specs
->decltype_p
= true;
19651 cp_lexer_consume_token (parser
->lexer
);
19655 /* If the type-specifier was for a built-in type, we're done. */
19658 /* Record the type. */
19660 && (token
->keyword
!= RID_SIGNED
19661 && token
->keyword
!= RID_UNSIGNED
19662 && token
->keyword
!= RID_SHORT
19663 && token
->keyword
!= RID_LONG
))
19664 cp_parser_set_decl_spec_type (decl_specs
,
19667 /*type_definition_p=*/false);
19669 decl_specs
->any_specifiers_p
= true;
19671 /* Consume the token. */
19672 cp_lexer_consume_token (parser
->lexer
);
19674 if (type
== error_mark_node
)
19675 return error_mark_node
;
19677 /* There is no valid C++ program where a non-template type is
19678 followed by a "<". That usually indicates that the user thought
19679 that the type was a template. */
19680 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
19683 return TYPE_NAME (type
);
19686 /* The type-specifier must be a user-defined type. */
19687 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
19691 const bool typename_p
= (cxx_dialect
>= cxx20
19692 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
19694 /* Don't gobble tokens or issue error messages if this is an
19695 optional type-specifier. */
19696 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
19697 cp_parser_parse_tentatively (parser
);
19699 /* Remember current tentative parsing state -- if we know we need
19700 a type, we can give better diagnostics here. */
19701 bool tent
= cp_parser_parsing_tentatively (parser
);
19703 token
= cp_lexer_peek_token (parser
->lexer
);
19705 /* Look for the optional `::' operator. */
19707 = (cp_parser_global_scope_opt (parser
,
19708 /*current_scope_valid_p=*/false)
19710 /* Look for the nested-name specifier. */
19712 = (cp_parser_nested_name_specifier_opt (parser
,
19713 /*typename_keyword_p=*/false,
19714 /*check_dependency_p=*/true,
19716 /*is_declaration=*/false)
19718 /* If we have seen a nested-name-specifier, and the next token
19719 is `template', then we are using the template-id production. */
19721 && cp_parser_optional_template_keyword (parser
))
19723 /* Look for the template-id. */
19724 type
= cp_parser_template_id (parser
,
19725 /*template_keyword_p=*/true,
19726 /*check_dependency_p=*/true,
19728 /*is_declaration=*/false);
19729 /* If the template-id did not name a type, we are out of
19731 if (TREE_CODE (type
) != TYPE_DECL
)
19733 /* ...unless we pretend we have seen 'typename'. */
19735 type
= cp_parser_make_typename_type (parser
, type
,
19739 cp_parser_error (parser
, "expected template-id for type");
19740 type
= error_mark_node
;
19744 /* DR 1812: A < following a qualified-id in a typename-specifier
19745 could safely be assumed to begin a template argument list, so
19746 the template keyword should be optional. */
19747 else if (parser
->scope
19750 && cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
))
19752 cp_parser_parse_tentatively (parser
);
19754 type
= cp_parser_template_id (parser
,
19755 /*template_keyword_p=*/true,
19756 /*check_dependency_p=*/true,
19758 /*is_declaration=*/false);
19759 /* This is handled below, so back off. */
19760 if (type
&& concept_check_p (type
))
19761 cp_parser_simulate_error (parser
);
19763 if (!cp_parser_parse_definitely (parser
))
19765 else if (TREE_CODE (type
) == TEMPLATE_ID_EXPR
)
19766 type
= make_typename_type (parser
->scope
, type
, typename_type
,
19767 /*complain=*/tf_error
);
19768 else if (TREE_CODE (type
) != TYPE_DECL
)
19772 /* Otherwise, look for a type-name. */
19775 if (cxx_dialect
>= cxx17
)
19776 cp_parser_parse_tentatively (parser
);
19778 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
19780 if (cxx_dialect
>= cxx17
&& !cp_parser_parse_definitely (parser
))
19784 if (!type
&& flag_concepts
&& decl_specs
)
19786 /* Try for a type-constraint with template arguments. We check
19787 decl_specs here to avoid trying this for a functional cast. */
19789 cp_parser_parse_tentatively (parser
);
19791 type
= cp_parser_template_id (parser
,
19792 /*template_keyword_p=*/false,
19793 /*check_dependency_p=*/true,
19795 /*is_declaration=*/false);
19796 if (type
&& concept_check_p (type
))
19798 location_t loc
= EXPR_LOCATION (type
);
19799 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
19801 if (tent
&& type
== error_mark_node
)
19802 /* Perhaps it's a concept-check expression. */
19803 cp_parser_simulate_error (parser
);
19806 cp_parser_simulate_error (parser
);
19808 if (!cp_parser_parse_definitely (parser
))
19812 if (!type
&& cxx_dialect
>= cxx17
)
19814 /* Try class template argument deduction or type-constraint without
19815 template arguments. */
19816 tree name
= cp_parser_identifier (parser
);
19817 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
19818 && parser
->scope
!= error_mark_node
)
19821 = cp_lexer_previous_token (parser
->lexer
)->location
;
19822 tree tmpl
= cp_parser_lookup_name (parser
, name
,
19824 /*is_template=*/false,
19825 /*is_namespace=*/false,
19826 /*check_dependency=*/true,
19827 /*ambiguous_decls=*/NULL
,
19829 if (tmpl
&& tmpl
!= error_mark_node
19830 && ctad_template_p (tmpl
))
19831 type
= make_template_placeholder (tmpl
);
19832 else if (flag_concepts
&& tmpl
&& concept_definition_p (tmpl
))
19833 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
19837 type
= error_mark_node
;
19838 if (!cp_parser_simulate_error (parser
))
19839 cp_parser_name_lookup_error (parser
, name
, tmpl
,
19840 NLE_TYPE
, token
->location
);
19844 type
= error_mark_node
;
19847 /* If it didn't work out, we don't have a TYPE. */
19848 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
19849 && !cp_parser_parse_definitely (parser
))
19852 /* Keep track of all name-lookups performed in class scopes. */
19856 && TREE_CODE (type
) == TYPE_DECL
19857 && identifier_p (DECL_NAME (type
)))
19858 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
19860 if (type
&& decl_specs
)
19861 cp_parser_set_decl_spec_type (decl_specs
, type
,
19863 /*type_definition_p=*/false);
19866 /* If we didn't get a type-name, issue an error message. */
19867 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
19869 cp_parser_error (parser
, "expected type-name");
19870 return error_mark_node
;
19873 if (type
&& type
!= error_mark_node
)
19875 /* See if TYPE is an Objective-C type, and if so, parse and
19876 accept any protocol references following it. Do this before
19877 the cp_parser_check_for_invalid_template_id() call, because
19878 Objective-C types can be followed by '<...>' which would
19879 enclose protocol names rather than template arguments, and so
19880 everything is fine. */
19881 if (c_dialect_objc () && !parser
->scope
19882 && (objc_is_id (type
) || objc_is_class_name (type
)))
19884 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
19885 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
19887 /* Clobber the "unqualified" type previously entered into
19888 DECL_SPECS with the new, improved protocol-qualified version. */
19890 decl_specs
->type
= qual_type
;
19895 /* There is no valid C++ program where a non-template type is
19896 followed by a "<". That usually indicates that the user
19897 thought that the type was a template. */
19898 cp_parser_check_for_invalid_template_id (parser
, type
,
19906 /* Parse the remainder of a placholder-type-specifier.
19908 placeholder-type-specifier:
19909 type-constraint_opt auto
19910 type-constraint_opt decltype(auto)
19912 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
19913 and passed as TMPL. This function converts TMPL to an actual type-constraint,
19914 parses the placeholder type, and performs some contextual syntactic analysis.
19916 LOC provides the location of the template name.
19918 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
19919 don't give an error if TMPL isn't a valid type-constraint, as the template-id
19920 might actually be a concept-check,
19922 Note that the Concepts TS allows the auto or decltype(auto) to be
19923 omitted in a constrained-type-specifier. */
19926 cp_parser_placeholder_type_specifier (cp_parser
*parser
, location_t loc
,
19927 tree tmpl
, bool tentative
)
19929 if (tmpl
== error_mark_node
)
19930 return error_mark_node
;
19932 tree orig_tmpl
= tmpl
;
19934 /* Get the arguments as written for subsequent analysis. */
19935 tree args
= NULL_TREE
;
19936 if (TREE_CODE (tmpl
) == TEMPLATE_ID_EXPR
)
19938 args
= TREE_OPERAND (tmpl
, 1);
19939 tmpl
= TREE_OPERAND (tmpl
, 0);
19942 /* A concept-name with no arguments can't be an expression. */
19945 tsubst_flags_t complain
= tentative
? tf_none
: tf_warning_or_error
;
19947 /* Get the concept and prototype parameter for the constraint. */
19948 tree_pair info
= finish_type_constraints (tmpl
, args
, complain
);
19949 tree con
= info
.first
;
19950 tree proto
= info
.second
;
19951 if (con
== error_mark_node
)
19952 return error_mark_node
;
19954 /* As per the standard, require auto or decltype(auto), except in some
19955 cases (template parameter lists, -fconcepts-ts enabled). */
19956 cp_token
*placeholder
= NULL
, *close_paren
= NULL
;
19957 if (cxx_dialect
>= cxx20
)
19959 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
19960 placeholder
= cp_lexer_consume_token (parser
->lexer
);
19961 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DECLTYPE
))
19963 placeholder
= cp_lexer_consume_token (parser
->lexer
);
19964 matching_parens parens
;
19965 parens
.require_open (parser
);
19966 cp_parser_require_keyword (parser
, RID_AUTO
, RT_AUTO
);
19967 close_paren
= parens
.require_close (parser
);
19971 /* A type constraint constrains a contextually determined type or type
19972 parameter pack. However, the Concepts TS does allow concepts
19973 to introduce non-type and template template parameters. */
19974 if (TREE_CODE (proto
) != TYPE_DECL
)
19976 if (!flag_concepts_ts
19977 || !processing_template_parmlist
)
19981 error_at (loc
, "%qE does not constrain a type", DECL_NAME (con
));
19982 inform (DECL_SOURCE_LOCATION (con
), "concept defined here");
19984 return error_mark_node
;
19988 /* In a template parameter list, a type-parameter can be introduced
19989 by type-constraints alone. */
19990 if (processing_template_parmlist
&& !placeholder
)
19991 return build_constrained_parameter (con
, proto
, args
);
19993 /* Diagnose issues placeholder issues. */
19994 if (!flag_concepts_ts
19995 && !parser
->in_result_type_constraint_p
19999 /* Perhaps it's a concept-check expression (c++/91073). */
20000 return error_mark_node
;
20002 tree id
= build_nt (TEMPLATE_ID_EXPR
, tmpl
, args
);
20003 tree expr
= DECL_P (orig_tmpl
) ? DECL_NAME (con
) : id
;
20004 error_at (input_location
,
20005 "expected %<auto%> or %<decltype(auto)%> after %qE", expr
);
20006 /* Fall through. This is an error of omission. */
20008 else if (parser
->in_result_type_constraint_p
&& placeholder
)
20010 /* A trailing return type only allows type-constraints. */
20011 error_at (input_location
,
20012 "unexpected placeholder in constrained result type");
20015 /* In a parameter-declaration-clause, a placeholder-type-specifier
20016 results in an invented template parameter. */
20017 if (parser
->auto_is_implicit_function_template_parm_p
)
20021 location_t loc
= make_location (placeholder
->location
,
20022 placeholder
->location
,
20023 close_paren
->location
);
20024 error_at (loc
, "cannot declare a parameter with %<decltype(auto)%>");
20025 return error_mark_node
;
20027 tree parm
= build_constrained_parameter (con
, proto
, args
);
20028 return synthesize_implicit_template_parm (parser
, parm
);
20031 /* Determine if the type should be deduced using template argument
20032 deduction or decltype deduction. Note that the latter is always
20033 used for type-constraints in trailing return types. */
20034 bool decltype_p
= placeholder
20035 ? placeholder
->keyword
== RID_DECLTYPE
20036 : parser
->in_result_type_constraint_p
;
20038 /* Otherwise, this is the type of a variable or return type. */
20040 return make_constrained_decltype_auto (con
, args
);
20042 return make_constrained_auto (con
, args
);
20045 /* Parse a type-name.
20051 simple-template-id [in c++0x]
20068 Returns a TYPE_DECL for the type. */
20071 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
20075 /* We can't know yet whether it is a class-name or not. */
20076 cp_parser_parse_tentatively (parser
);
20077 /* Try a class-name. */
20078 type_decl
= cp_parser_class_name (parser
,
20079 typename_keyword_p
,
20080 /*template_keyword_p=*/false,
20082 /*check_dependency_p=*/true,
20083 /*class_head_p=*/false,
20084 /*is_declaration=*/false);
20085 /* If it's not a class-name, keep looking. */
20086 if (!cp_parser_parse_definitely (parser
))
20088 if (cxx_dialect
< cxx11
)
20089 /* It must be a typedef-name or an enum-name. */
20090 return cp_parser_nonclass_name (parser
);
20092 cp_parser_parse_tentatively (parser
);
20093 /* It is either a simple-template-id representing an
20094 instantiation of an alias template... */
20095 type_decl
= cp_parser_template_id (parser
,
20096 /*template_keyword_p=*/false,
20097 /*check_dependency_p=*/true,
20099 /*is_declaration=*/false);
20100 /* Note that this must be an instantiation of an alias template
20101 because [temp.names]/6 says:
20103 A template-id that names an alias template specialization
20106 Whereas [temp.names]/7 says:
20108 A simple-template-id that names a class template
20109 specialization is a class-name.
20111 With concepts, this could also be a partial-concept-id that
20112 declares a non-type template parameter. */
20113 if (type_decl
!= NULL_TREE
20114 && TREE_CODE (type_decl
) == TYPE_DECL
20115 && TYPE_DECL_ALIAS_P (type_decl
))
20116 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
20118 cp_parser_simulate_error (parser
);
20120 if (!cp_parser_parse_definitely (parser
))
20121 /* ... Or a typedef-name or an enum-name. */
20122 return cp_parser_nonclass_name (parser
);
20128 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20140 Returns a TYPE_DECL for the type. */
20143 cp_parser_nonclass_name (cp_parser
* parser
)
20148 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20149 identifier
= cp_parser_identifier (parser
);
20150 if (identifier
== error_mark_node
)
20151 return error_mark_node
;
20153 /* Look up the type-name. */
20154 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
20156 type_decl
= strip_using_decl (type_decl
);
20158 if (TREE_CODE (type_decl
) != TYPE_DECL
20159 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
20161 /* See if this is an Objective-C type. */
20162 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
20163 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
20165 type_decl
= TYPE_NAME (type
);
20168 /* Issue an error if we did not find a type-name. */
20169 if (TREE_CODE (type_decl
) != TYPE_DECL
20170 /* In Objective-C, we have the complication that class names are
20171 normally type names and start declarations (eg, the
20172 "NSObject" in "NSObject *object;"), but can be used in an
20173 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20174 is an expression. So, a classname followed by a dot is not a
20175 valid type-name. */
20176 || (objc_is_class_name (TREE_TYPE (type_decl
))
20177 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
20179 if (!cp_parser_simulate_error (parser
))
20180 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
20181 NLE_TYPE
, token
->location
);
20182 return error_mark_node
;
20184 /* Remember that the name was used in the definition of the
20185 current class so that we can check later to see if the
20186 meaning would have been different after the class was
20187 entirely defined. */
20188 else if (type_decl
!= error_mark_node
20190 maybe_note_name_used_in_class (identifier
, type_decl
);
20195 /* Parse an elaborated-type-specifier. Note that the grammar given
20196 here incorporates the resolution to DR68.
20198 elaborated-type-specifier:
20199 class-key :: [opt] nested-name-specifier [opt] identifier
20200 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20201 enum-key :: [opt] nested-name-specifier [opt] identifier
20202 typename :: [opt] nested-name-specifier identifier
20203 typename :: [opt] nested-name-specifier template [opt]
20208 elaborated-type-specifier:
20209 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20210 class-key attributes :: [opt] nested-name-specifier [opt]
20211 template [opt] template-id
20212 enum attributes :: [opt] nested-name-specifier [opt] identifier
20214 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20215 declared `friend'. If IS_DECLARATION is TRUE, then this
20216 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20217 something is being declared.
20219 Returns the TYPE specified. */
20222 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
20224 bool is_declaration
)
20226 enum tag_types tag_type
;
20228 tree type
= NULL_TREE
;
20229 tree attributes
= NULL_TREE
;
20231 cp_token
*token
= NULL
;
20233 /* For class and enum types the location of the class-key or enum-key. */
20234 location_t key_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20235 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20236 rid scoped_key
= RID_MAX
;
20238 /* See if we're looking at the `enum' keyword. */
20239 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
20241 /* Consume the `enum' token. */
20242 cp_lexer_consume_token (parser
->lexer
);
20243 /* Remember that it's an enumeration type. */
20244 tag_type
= enum_type
;
20245 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20246 enums) is used here. */
20247 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20248 if (cp_parser_is_keyword (token
, scoped_key
= RID_CLASS
)
20249 || cp_parser_is_keyword (token
, scoped_key
= RID_STRUCT
))
20251 location_t loc
= token
->location
;
20252 gcc_rich_location
richloc (loc
);
20253 richloc
.add_range (input_location
);
20254 richloc
.add_fixit_remove ();
20255 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
20256 "a scoped enum must not use the %qD keyword",
20258 /* Consume the `struct' or `class' and parse it anyway. */
20259 cp_lexer_consume_token (parser
->lexer
);
20260 /* Create a combined location for the whole scoped-enum-key. */
20261 key_loc
= make_location (key_loc
, key_loc
, loc
);
20264 scoped_key
= RID_MAX
;
20266 /* Parse the attributes. */
20267 attributes
= cp_parser_attributes_opt (parser
);
20269 /* Or, it might be `typename'. */
20270 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
20273 /* Consume the `typename' token. */
20274 cp_lexer_consume_token (parser
->lexer
);
20275 /* Remember that it's a `typename' type. */
20276 tag_type
= typename_type
;
20278 /* Otherwise it must be a class-key. */
20281 key_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20282 tag_type
= cp_parser_class_key (parser
);
20283 if (tag_type
== none_type
)
20284 return error_mark_node
;
20285 /* Parse the attributes. */
20286 attributes
= cp_parser_attributes_opt (parser
);
20289 /* Look for the `::' operator. */
20290 globalscope
= cp_parser_global_scope_opt (parser
,
20291 /*current_scope_valid_p=*/false);
20292 /* Look for the nested-name-specifier. */
20293 tree nested_name_specifier
;
20294 if (tag_type
== typename_type
&& !globalscope
)
20296 nested_name_specifier
20297 = cp_parser_nested_name_specifier (parser
,
20298 /*typename_keyword_p=*/true,
20299 /*check_dependency_p=*/true,
20302 if (!nested_name_specifier
)
20303 return error_mark_node
;
20306 /* Even though `typename' is not present, the proposed resolution
20307 to Core Issue 180 says that in `class A<T>::B', `B' should be
20308 considered a type-name, even if `A<T>' is dependent. */
20309 nested_name_specifier
20310 = cp_parser_nested_name_specifier_opt (parser
,
20311 /*typename_keyword_p=*/true,
20312 /*check_dependency_p=*/true,
20315 /* For everything but enumeration types, consider a template-id.
20316 For an enumeration type, consider only a plain identifier. */
20317 if (tag_type
!= enum_type
)
20319 bool template_p
= false;
20322 /* Allow the `template' keyword. */
20323 template_p
= cp_parser_optional_template_keyword (parser
);
20324 /* If we didn't see `template', we don't know if there's a
20325 template-id or not. */
20327 cp_parser_parse_tentatively (parser
);
20328 /* The `template' keyword must follow a nested-name-specifier. */
20329 else if (!nested_name_specifier
&& !globalscope
)
20331 cp_parser_error (parser
, "%<template%> must follow a nested-"
20333 return error_mark_node
;
20336 /* Parse the template-id. */
20337 token
= cp_lexer_peek_token (parser
->lexer
);
20338 decl
= cp_parser_template_id (parser
, template_p
,
20339 /*check_dependency_p=*/true,
20342 /* If we didn't find a template-id, look for an ordinary
20344 if (!template_p
&& !cp_parser_parse_definitely (parser
))
20346 /* We can get here when cp_parser_template_id, called by
20347 cp_parser_class_name with tag_type == none_type, succeeds
20348 and caches a BASELINK. Then, when called again here,
20349 instead of failing and returning an error_mark_node
20350 returns it (see template/typename17.C in C++11).
20351 ??? Could we diagnose this earlier? */
20352 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
20354 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
20355 type
= error_mark_node
;
20357 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20358 in effect, then we must assume that, upon instantiation, the
20359 template will correspond to a class. */
20360 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
20361 && tag_type
== typename_type
)
20362 type
= make_typename_type (parser
->scope
, decl
,
20364 /*complain=*/tf_error
);
20365 /* If the `typename' keyword is in effect and DECL is not a type
20366 decl, then type is non existent. */
20367 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
20369 else if (TREE_CODE (decl
) == TYPE_DECL
)
20371 type
= check_elaborated_type_specifier (tag_type
, decl
,
20372 /*allow_template_p=*/true);
20374 /* If the next token is a semicolon, this must be a specialization,
20375 instantiation, or friend declaration. Check the scope while we
20376 still know whether or not we had a nested-name-specifier. */
20377 if (type
!= error_mark_node
20378 && !nested_name_specifier
&& !is_friend
20379 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20380 check_unqualified_spec_or_inst (type
, token
->location
);
20382 else if (decl
== error_mark_node
)
20383 type
= error_mark_node
;
20388 token
= cp_lexer_peek_token (parser
->lexer
);
20389 identifier
= cp_parser_identifier (parser
);
20391 if (identifier
== error_mark_node
)
20393 parser
->scope
= NULL_TREE
;
20394 return error_mark_node
;
20397 /* For a `typename', we needn't call xref_tag. */
20398 if (tag_type
== typename_type
20399 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
20400 return cp_parser_make_typename_type (parser
, identifier
,
20403 /* Template parameter lists apply only if we are not within a
20404 function parameter list. */
20405 bool template_parm_lists_apply
20406 = parser
->num_template_parameter_lists
;
20407 if (template_parm_lists_apply
)
20408 for (cp_binding_level
*s
= current_binding_level
;
20409 s
&& s
->kind
!= sk_template_parms
;
20410 s
= s
->level_chain
)
20411 if (s
->kind
== sk_function_parms
)
20412 template_parm_lists_apply
= false;
20414 /* Look up a qualified name in the usual way. */
20418 tree ambiguous_decls
;
20420 decl
= cp_parser_lookup_name (parser
, identifier
,
20422 /*is_template=*/false,
20423 /*is_namespace=*/false,
20424 /*check_dependency=*/true,
20428 /* If the lookup was ambiguous, an error will already have been
20430 if (ambiguous_decls
)
20431 return error_mark_node
;
20433 /* If we are parsing friend declaration, DECL may be a
20434 TEMPLATE_DECL tree node here. However, we need to check
20435 whether this TEMPLATE_DECL results in valid code. Consider
20436 the following example:
20439 template <class T> class C {};
20442 template <class T> friend class N::C; // #1, valid code
20444 template <class T> class Y {
20445 friend class N::C; // #2, invalid code
20448 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20449 name lookup of `N::C'. We see that friend declaration must
20450 be template for the code to be valid. Note that
20451 processing_template_decl does not work here since it is
20452 always 1 for the above two cases. */
20454 decl
= (cp_parser_maybe_treat_template_as_class
20455 (decl
, /*tag_name_p=*/is_friend
20456 && template_parm_lists_apply
));
20458 if (TREE_CODE (decl
) != TYPE_DECL
)
20460 cp_parser_diagnose_invalid_type_name (parser
,
20463 return error_mark_node
;
20466 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
20468 bool allow_template
= (template_parm_lists_apply
20469 || DECL_SELF_REFERENCE_P (decl
));
20470 type
= check_elaborated_type_specifier (tag_type
, decl
,
20473 if (type
== error_mark_node
)
20474 return error_mark_node
;
20477 /* Forward declarations of nested types, such as
20482 are invalid unless all components preceding the final '::'
20483 are complete. If all enclosing types are complete, these
20484 declarations become merely pointless.
20486 Invalid forward declarations of nested types are errors
20487 caught elsewhere in parsing. Those that are pointless arrive
20490 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
20491 && !is_friend
&& is_declaration
20492 && !processing_explicit_instantiation
)
20493 warning (0, "declaration %qD does not declare anything", decl
);
20495 type
= TREE_TYPE (decl
);
20499 /* An elaborated-type-specifier sometimes introduces a new type and
20500 sometimes names an existing type. Normally, the rule is that it
20501 introduces a new type only if there is not an existing type of
20502 the same name already in scope. For example, given:
20505 void f() { struct S s; }
20507 the `struct S' in the body of `f' is the same `struct S' as in
20508 the global scope; the existing definition is used. However, if
20509 there were no global declaration, this would introduce a new
20510 local class named `S'.
20512 An exception to this rule applies to the following code:
20514 namespace N { struct S; }
20516 Here, the elaborated-type-specifier names a new type
20517 unconditionally; even if there is already an `S' in the
20518 containing scope this declaration names a new type.
20519 This exception only applies if the elaborated-type-specifier
20520 forms the complete declaration:
20524 A declaration consisting solely of `class-key identifier ;' is
20525 either a redeclaration of the name in the current scope or a
20526 forward declaration of the identifier as a class name. It
20527 introduces the name into the current scope.
20529 We are in this situation precisely when the next token is a `;'.
20531 An exception to the exception is that a `friend' declaration does
20532 *not* name a new type; i.e., given:
20534 struct S { friend struct T; };
20536 `T' is not a new type in the scope of `S'.
20538 Also, `new struct S' or `sizeof (struct S)' never results in the
20539 definition of a new type; a new type can only be declared in a
20540 declaration context. */
20545 /* Friends have special name lookup rules. */
20546 how
= TAG_how::HIDDEN_FRIEND
;
20547 else if (is_declaration
20548 && cp_lexer_next_token_is (parser
->lexer
,
20550 /* This is a `class-key identifier ;' */
20551 how
= TAG_how::CURRENT_ONLY
;
20553 how
= TAG_how::GLOBAL
;
20556 (template_parm_lists_apply
20557 && (cp_parser_next_token_starts_class_definition_p (parser
)
20558 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
20559 /* An unqualified name was used to reference this type, so
20560 there were no qualifying templates. */
20561 if (template_parm_lists_apply
20562 && !cp_parser_check_template_parameters (parser
,
20563 /*num_templates=*/0,
20564 /*template_id*/false,
20566 /*declarator=*/NULL
))
20567 return error_mark_node
;
20569 type
= xref_tag (tag_type
, identifier
, how
, template_p
);
20573 if (type
== error_mark_node
)
20574 return error_mark_node
;
20576 /* Allow attributes on forward declarations of classes. */
20579 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20580 warning (OPT_Wattributes
,
20581 "attributes ignored on uninstantiated type");
20582 else if (tag_type
!= enum_type
20583 && TREE_CODE (type
) != BOUND_TEMPLATE_TEMPLATE_PARM
20584 && CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
20585 && ! processing_explicit_instantiation
)
20586 warning (OPT_Wattributes
,
20587 "attributes ignored on template instantiation");
20588 else if (is_friend
&& cxx11_attribute_p (attributes
))
20590 if (warning (OPT_Wattributes
, "attribute ignored"))
20591 inform (input_location
, "an attribute that appertains to a friend "
20592 "declaration that is not a definition is ignored");
20594 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
20595 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
20597 warning (OPT_Wattributes
,
20598 "attributes ignored on elaborated-type-specifier that is "
20599 "not a forward declaration");
20602 if (tag_type
== enum_type
)
20603 cp_parser_maybe_warn_enum_key (parser
, key_loc
, type
, scoped_key
);
20606 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
20607 for alias definition. */
20608 bool decl_class
= (is_declaration
20609 && cp_parser_declares_only_class_p (parser
));
20610 cp_parser_check_class_key (parser
, key_loc
, tag_type
, type
, false,
20613 /* Indicate whether this class was declared as a `class' or as a
20615 if (CLASS_TYPE_P (type
) && !currently_open_class (type
))
20616 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
20619 /* A "<" cannot follow an elaborated type specifier. If that
20620 happens, the user was probably trying to form a template-id. */
20621 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
20627 /* Parse an enum-specifier.
20630 enum-head { enumerator-list [opt] }
20631 enum-head { enumerator-list , } [C++0x]
20634 enum-key identifier [opt] enum-base [opt]
20635 enum-key nested-name-specifier identifier enum-base [opt]
20640 enum struct [C++0x]
20643 : type-specifier-seq
20645 opaque-enum-specifier:
20646 enum-key identifier enum-base [opt] ;
20649 enum-key attributes[opt] identifier [opt] enum-base [opt]
20650 { enumerator-list [opt] }attributes[opt]
20651 enum-key attributes[opt] identifier [opt] enum-base [opt]
20652 { enumerator-list, }attributes[opt] [C++0x]
20654 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20655 if the token stream isn't an enum-specifier after all. */
20658 cp_parser_enum_specifier (cp_parser
* parser
)
20661 tree type
= NULL_TREE
;
20663 tree nested_name_specifier
= NULL_TREE
;
20665 bool scoped_enum_p
= false;
20666 bool has_underlying_type
= false;
20667 bool nested_being_defined
= false;
20668 bool new_value_list
= false;
20669 bool is_new_type
= false;
20670 bool is_unnamed
= false;
20671 tree underlying_type
= NULL_TREE
;
20672 cp_token
*type_start_token
= NULL
;
20673 auto cleanup
= make_temp_override (parser
->colon_corrects_to_scope_p
, false);
20675 /* Parse tentatively so that we can back up if we don't find a
20677 cp_parser_parse_tentatively (parser
);
20679 /* Caller guarantees that the current token is 'enum', an identifier
20680 possibly follows, and the token after that is an opening brace.
20681 If we don't have an identifier, fabricate an anonymous name for
20682 the enumeration being defined. */
20683 cp_lexer_consume_token (parser
->lexer
);
20685 /* Parse the "class" or "struct", which indicates a scoped
20686 enumeration type in C++0x. */
20687 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
20688 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
20690 if (cxx_dialect
< cxx11
)
20691 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
20693 /* Consume the `struct' or `class' token. */
20694 cp_lexer_consume_token (parser
->lexer
);
20696 scoped_enum_p
= true;
20699 attributes
= cp_parser_attributes_opt (parser
);
20701 /* Clear the qualification. */
20702 parser
->scope
= NULL_TREE
;
20703 parser
->qualifying_scope
= NULL_TREE
;
20704 parser
->object_scope
= NULL_TREE
;
20706 /* Figure out in what scope the declaration is being placed. */
20707 prev_scope
= current_scope ();
20709 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20711 push_deferring_access_checks (dk_no_check
);
20712 nested_name_specifier
20713 = cp_parser_nested_name_specifier_opt (parser
,
20714 /*typename_keyword_p=*/true,
20715 /*check_dependency_p=*/false,
20717 /*is_declaration=*/false);
20719 if (nested_name_specifier
)
20723 identifier
= cp_parser_identifier (parser
);
20724 name
= cp_parser_lookup_name (parser
, identifier
,
20726 /*is_template=*/false,
20727 /*is_namespace=*/false,
20728 /*check_dependency=*/true,
20729 /*ambiguous_decls=*/NULL
,
20731 if (name
&& name
!= error_mark_node
)
20733 type
= TREE_TYPE (name
);
20734 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20736 /* Are template enums allowed in ISO? */
20737 if (template_parm_scope_p ())
20738 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
20739 "%qD is an enumeration template", name
);
20740 /* ignore a typename reference, for it will be solved by name
20745 else if (nested_name_specifier
== error_mark_node
)
20746 /* We already issued an error. */;
20749 error_at (type_start_token
->location
,
20750 "%qD does not name an enumeration in %qT",
20751 identifier
, nested_name_specifier
);
20752 nested_name_specifier
= error_mark_node
;
20757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20758 identifier
= cp_parser_identifier (parser
);
20761 identifier
= make_anon_name ();
20764 error_at (type_start_token
->location
,
20765 "unnamed scoped enum is not allowed");
20768 pop_deferring_access_checks ();
20770 /* Check for the `:' that denotes a specified underlying type in C++0x.
20771 Note that a ':' could also indicate a bitfield width, however. */
20772 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20774 cp_decl_specifier_seq type_specifiers
;
20776 /* Consume the `:'. */
20777 cp_lexer_consume_token (parser
->lexer
);
20780 = make_temp_override (parser
->type_definition_forbidden_message
,
20781 G_("types may not be defined in enum-base"));
20783 /* Parse the type-specifier-seq. */
20784 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
20785 /*is_declaration=*/false,
20786 /*is_trailing_return=*/false,
20789 /* At this point this is surely not elaborated type specifier. */
20790 if (!cp_parser_parse_definitely (parser
))
20793 if (cxx_dialect
< cxx11
)
20794 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
20796 has_underlying_type
= true;
20798 /* If that didn't work, stop. */
20799 if (type_specifiers
.type
!= error_mark_node
)
20801 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
20802 /*initialized=*/0, NULL
);
20803 if (underlying_type
== error_mark_node
20804 || check_for_bare_parameter_packs (underlying_type
))
20805 underlying_type
= NULL_TREE
;
20809 /* Look for the `{' but don't consume it yet. */
20810 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
20812 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
20814 if (has_underlying_type
)
20815 cp_parser_commit_to_tentative_parse (parser
);
20816 cp_parser_error (parser
, "expected %<{%>");
20817 if (has_underlying_type
)
20818 return error_mark_node
;
20820 /* An opaque-enum-specifier must have a ';' here. */
20821 if ((scoped_enum_p
|| underlying_type
)
20822 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20824 if (has_underlying_type
)
20825 cp_parser_commit_to_tentative_parse (parser
);
20826 cp_parser_error (parser
, "expected %<;%> or %<{%>");
20827 if (has_underlying_type
)
20828 return error_mark_node
;
20832 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
20835 if (nested_name_specifier
)
20837 if (CLASS_TYPE_P (nested_name_specifier
))
20839 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
20840 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
20841 push_scope (nested_name_specifier
);
20843 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
20844 push_nested_namespace (nested_name_specifier
);
20847 /* Issue an error message if type-definitions are forbidden here. */
20848 if (!cp_parser_check_type_definition (parser
))
20849 type
= error_mark_node
;
20851 /* Create the new type. We do this before consuming the opening
20852 brace so the enum will be recorded as being on the line of its
20853 tag (or the 'enum' keyword, if there is no tag). */
20854 type
= start_enum (identifier
, type
, underlying_type
,
20855 attributes
, scoped_enum_p
, &is_new_type
);
20857 /* If the next token is not '{' it is an opaque-enum-specifier or an
20858 elaborated-type-specifier. */
20859 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
20861 timevar_push (TV_PARSE_ENUM
);
20862 if (nested_name_specifier
20863 && nested_name_specifier
!= error_mark_node
)
20865 /* The following catches invalid code such as:
20866 enum class S<int>::E { A, B, C }; */
20867 if (!processing_specialization
20868 && CLASS_TYPE_P (nested_name_specifier
)
20869 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
20870 error_at (type_start_token
->location
, "cannot add an enumerator "
20871 "list to a template instantiation");
20873 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
20875 error_at (type_start_token
->location
,
20876 "%<%T::%E%> has not been declared",
20877 TYPE_CONTEXT (nested_name_specifier
),
20878 nested_name_specifier
);
20879 type
= error_mark_node
;
20881 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
20882 && !CLASS_TYPE_P (nested_name_specifier
))
20884 error_at (type_start_token
->location
, "nested name specifier "
20885 "%qT for enum declaration does not name a class "
20886 "or namespace", nested_name_specifier
);
20887 type
= error_mark_node
;
20889 /* If that scope does not contain the scope in which the
20890 class was originally declared, the program is invalid. */
20891 else if (prev_scope
&& !is_ancestor (prev_scope
,
20892 nested_name_specifier
))
20894 if (at_namespace_scope_p ())
20895 error_at (type_start_token
->location
,
20896 "declaration of %qD in namespace %qD which does not "
20898 type
, prev_scope
, nested_name_specifier
);
20900 error_at (type_start_token
->location
,
20901 "declaration of %qD in %qD which does not "
20903 type
, prev_scope
, nested_name_specifier
);
20904 type
= error_mark_node
;
20906 /* If that scope is the scope where the declaration is being placed
20907 the program is invalid. */
20908 else if (CLASS_TYPE_P (nested_name_specifier
)
20909 && CLASS_TYPE_P (prev_scope
)
20910 && same_type_p (nested_name_specifier
, prev_scope
))
20912 permerror (type_start_token
->location
,
20913 "extra qualification not allowed");
20914 nested_name_specifier
= NULL_TREE
;
20919 begin_scope (sk_scoped_enum
, type
);
20921 /* Consume the opening brace. */
20922 matching_braces braces
;
20923 braces
.consume_open (parser
);
20925 if (type
== error_mark_node
)
20926 ; /* Nothing to add */
20927 else if (OPAQUE_ENUM_P (type
)
20928 || (cxx_dialect
> cxx98
&& processing_specialization
))
20930 new_value_list
= true;
20931 SET_OPAQUE_ENUM_P (type
, false);
20932 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
20936 error_at (type_start_token
->location
,
20937 "multiple definition of %q#T", type
);
20938 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
20939 "previous definition here");
20940 type
= error_mark_node
;
20943 if (type
== error_mark_node
)
20944 cp_parser_skip_to_end_of_block_or_statement (parser
);
20945 /* If the next token is not '}', then there are some enumerators. */
20946 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
20948 if (is_unnamed
&& !scoped_enum_p
)
20949 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
20950 "ISO C++ forbids empty unnamed enum");
20954 /* We've seen a '{' so we know we're in an enum-specifier.
20955 Commit to any tentative parse to get syntax errors. */
20956 cp_parser_commit_to_tentative_parse (parser
);
20957 cp_parser_enumerator_list (parser
, type
);
20960 /* Consume the final '}'. */
20961 braces
.require_close (parser
);
20965 timevar_pop (TV_PARSE_ENUM
);
20969 /* If a ';' follows, then it is an opaque-enum-specifier
20970 and additional restrictions apply. */
20971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20974 error_at (type_start_token
->location
,
20975 "opaque-enum-specifier without name");
20976 else if (nested_name_specifier
)
20977 error_at (type_start_token
->location
,
20978 "opaque-enum-specifier must use a simple identifier");
20982 /* Look for trailing attributes to apply to this enumeration, and
20983 apply them if appropriate. */
20984 if (cp_parser_allow_gnu_extensions_p (parser
))
20986 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
20987 cplus_decl_attributes (&type
,
20989 (int) ATTR_FLAG_TYPE_IN_PLACE
);
20992 /* Finish up the enumeration. */
20993 if (type
!= error_mark_node
)
20995 if (new_value_list
)
20996 finish_enum_value_list (type
);
20998 finish_enum (type
);
21001 if (nested_name_specifier
)
21003 if (CLASS_TYPE_P (nested_name_specifier
))
21005 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
21006 pop_scope (nested_name_specifier
);
21008 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
21009 pop_nested_namespace (nested_name_specifier
);
21014 /* Parse an enumerator-list. The enumerators all have the indicated
21018 enumerator-definition
21019 enumerator-list , enumerator-definition */
21022 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
21026 /* Parse an enumerator-definition. */
21027 cp_parser_enumerator_definition (parser
, type
);
21029 /* If the next token is not a ',', we've reached the end of
21031 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21033 /* Otherwise, consume the `,' and keep going. */
21034 cp_lexer_consume_token (parser
->lexer
);
21035 /* If the next token is a `}', there is a trailing comma. */
21036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
21038 if (cxx_dialect
< cxx11
)
21039 pedwarn (input_location
, OPT_Wpedantic
,
21040 "comma at end of enumerator list");
21046 /* Parse an enumerator-definition. The enumerator has the indicated
21049 enumerator-definition:
21051 enumerator = constant-expression
21058 enumerator-definition:
21059 enumerator attributes [opt]
21060 enumerator attributes [opt] = constant-expression */
21063 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
21069 /* Save the input location because we are interested in the location
21070 of the identifier and not the location of the explicit value. */
21071 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21073 /* Look for the identifier. */
21074 identifier
= cp_parser_identifier (parser
);
21075 if (identifier
== error_mark_node
)
21078 /* Parse any specified attributes. */
21079 tree attrs
= cp_parser_attributes_opt (parser
);
21081 /* If the next token is an '=', then there is an explicit value. */
21082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21084 /* Consume the `=' token. */
21085 cp_lexer_consume_token (parser
->lexer
);
21086 /* Parse the value. */
21087 value
= cp_parser_constant_expression (parser
);
21092 /* If we are processing a template, make sure the initializer of the
21093 enumerator doesn't contain any bare template parameter pack. */
21094 if (check_for_bare_parameter_packs (value
))
21095 value
= error_mark_node
;
21097 /* Create the enumerator. */
21098 build_enumerator (identifier
, value
, type
, attrs
, loc
);
21101 /* Parse a namespace-name.
21104 original-namespace-name
21107 Returns the NAMESPACE_DECL for the namespace. */
21110 cp_parser_namespace_name (cp_parser
* parser
)
21113 tree namespace_decl
;
21115 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21117 /* Get the name of the namespace. */
21118 identifier
= cp_parser_identifier (parser
);
21119 if (identifier
== error_mark_node
)
21120 return error_mark_node
;
21122 /* Look up the identifier in the currently active scope. Look only
21123 for namespaces, due to:
21125 [basic.lookup.udir]
21127 When looking up a namespace-name in a using-directive or alias
21128 definition, only namespace names are considered.
21132 [basic.lookup.qual]
21134 During the lookup of a name preceding the :: scope resolution
21135 operator, object, function, and enumerator names are ignored.
21137 (Note that cp_parser_qualifying_entity only calls this
21138 function if the token after the name is the scope resolution
21140 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
21142 /*is_template=*/false,
21143 /*is_namespace=*/true,
21144 /*check_dependency=*/true,
21145 /*ambiguous_decls=*/NULL
,
21147 /* If it's not a namespace, issue an error. */
21148 if (namespace_decl
== error_mark_node
21149 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
21151 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21153 auto_diagnostic_group d
;
21155 if (namespace_decl
== error_mark_node
21156 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21157 hint
= suggest_alternative_in_explicit_scope (token
->location
,
21160 if (const char *suggestion
= hint
.suggestion ())
21162 gcc_rich_location
richloc (token
->location
);
21163 richloc
.add_fixit_replace (suggestion
);
21164 error_at (&richloc
,
21165 "%qD is not a namespace-name; did you mean %qs?",
21166 identifier
, suggestion
);
21169 error_at (token
->location
, "%qD is not a namespace-name",
21173 cp_parser_error (parser
, "expected namespace-name");
21174 namespace_decl
= error_mark_node
;
21177 return namespace_decl
;
21180 /* Parse a namespace-definition.
21182 namespace-definition:
21183 named-namespace-definition
21184 unnamed-namespace-definition
21186 named-namespace-definition:
21187 original-namespace-definition
21188 extension-namespace-definition
21190 original-namespace-definition:
21191 namespace identifier { namespace-body }
21193 extension-namespace-definition:
21194 namespace original-namespace-name { namespace-body }
21196 unnamed-namespace-definition:
21197 namespace { namespace-body } */
21200 cp_parser_namespace_definition (cp_parser
* parser
)
21203 int nested_definition_count
= 0;
21205 cp_ensure_no_omp_declare_simd (parser
);
21206 cp_ensure_no_oacc_routine (parser
);
21208 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
21209 const bool topmost_inline_p
= is_inline
;
21213 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
21214 cp_lexer_consume_token (parser
->lexer
);
21217 /* Look for the `namespace' keyword. */
21219 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
21221 /* Parse any specified attributes before the identifier. */
21222 tree attribs
= cp_parser_attributes_opt (parser
);
21226 identifier
= NULL_TREE
;
21228 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
21230 if (nested_inline_p
&& nested_definition_count
!= 0)
21232 if (pedantic
&& cxx_dialect
< cxx20
)
21233 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
21234 OPT_Wc__20_extensions
, "nested inline namespace "
21235 "definitions only available with %<-std=c++20%> or "
21236 "%<-std=gnu++20%>");
21237 cp_lexer_consume_token (parser
->lexer
);
21240 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
21242 identifier
= cp_parser_identifier (parser
);
21244 if (cp_next_tokens_can_be_std_attribute_p (parser
))
21245 pedwarn (input_location
, OPT_Wpedantic
,
21246 "standard attributes on namespaces must precede "
21247 "the namespace name");
21249 /* Parse any attributes specified after the identifier. */
21250 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
21253 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
21255 /* Don't forget that the innermost namespace might have been
21256 marked as inline. Use |= because we cannot overwrite
21257 IS_INLINE in case the outermost namespace is inline, but
21258 there are no nested inlines. */
21259 is_inline
|= nested_inline_p
;
21263 if (!nested_definition_count
&& pedantic
&& cxx_dialect
< cxx17
)
21264 pedwarn (input_location
, OPT_Wc__17_extensions
,
21265 "nested namespace definitions only available with "
21266 "%<-std=c++17%> or %<-std=gnu++17%>");
21268 /* Nested namespace names can create new namespaces (unlike
21269 other qualified-ids). */
21270 if (int count
= (identifier
21271 ? push_namespace (identifier
, nested_inline_p
)
21273 nested_definition_count
+= count
;
21275 cp_parser_error (parser
, "nested namespace name required");
21276 cp_lexer_consume_token (parser
->lexer
);
21279 if (nested_definition_count
&& !identifier
)
21280 cp_parser_error (parser
, "namespace name required");
21282 if (nested_definition_count
&& attribs
)
21283 error_at (token
->location
,
21284 "a nested namespace definition cannot have attributes");
21285 if (nested_definition_count
&& topmost_inline_p
)
21286 error_at (token
->location
,
21287 "a nested namespace definition cannot be inline");
21289 /* Start the namespace. */
21290 nested_definition_count
+= push_namespace (identifier
, is_inline
);
21292 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
21294 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
21296 /* Look for the `{' to validate starting the namespace. */
21297 matching_braces braces
;
21298 if (braces
.require_open (parser
))
21300 /* Parse the body of the namespace. */
21301 cp_parser_namespace_body (parser
);
21303 /* Look for the final `}'. */
21304 braces
.require_close (parser
);
21307 if (has_visibility
)
21308 pop_visibility (1);
21310 /* Pop the nested namespace definitions. */
21311 while (nested_definition_count
--)
21315 /* Parse a namespace-body.
21318 declaration-seq [opt] */
21321 cp_parser_namespace_body (cp_parser
* parser
)
21323 cp_parser_declaration_seq_opt (parser
);
21326 /* Parse a namespace-alias-definition.
21328 namespace-alias-definition:
21329 namespace identifier = qualified-namespace-specifier ; */
21332 cp_parser_namespace_alias_definition (cp_parser
* parser
)
21335 tree namespace_specifier
;
21337 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21339 /* Look for the `namespace' keyword. */
21340 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
21341 /* Look for the identifier. */
21342 identifier
= cp_parser_identifier (parser
);
21343 if (identifier
== error_mark_node
)
21345 /* Look for the `=' token. */
21346 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
21347 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21349 error_at (token
->location
, "%<namespace%> definition is not allowed here");
21350 /* Skip the definition. */
21351 cp_lexer_consume_token (parser
->lexer
);
21352 if (cp_parser_skip_to_closing_brace (parser
))
21353 cp_lexer_consume_token (parser
->lexer
);
21356 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
21357 /* Look for the qualified-namespace-specifier. */
21358 namespace_specifier
21359 = cp_parser_qualified_namespace_specifier (parser
);
21360 cp_warn_deprecated_use_scopes (namespace_specifier
);
21361 /* Look for the `;' token. */
21362 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21364 /* Register the alias in the symbol table. */
21365 do_namespace_alias (identifier
, namespace_specifier
);
21368 /* Parse a qualified-namespace-specifier.
21370 qualified-namespace-specifier:
21371 :: [opt] nested-name-specifier [opt] namespace-name
21373 Returns a NAMESPACE_DECL corresponding to the specified
21377 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
21379 /* Look for the optional `::'. */
21380 cp_parser_global_scope_opt (parser
,
21381 /*current_scope_valid_p=*/false);
21383 /* Look for the optional nested-name-specifier. */
21384 cp_parser_nested_name_specifier_opt (parser
,
21385 /*typename_keyword_p=*/false,
21386 /*check_dependency_p=*/true,
21388 /*is_declaration=*/true);
21390 return cp_parser_namespace_name (parser
);
21393 /* Subroutine of cp_parser_using_declaration. */
21396 finish_using_decl (tree qscope
, tree identifier
, bool typename_p
= false)
21398 tree decl
= NULL_TREE
;
21399 if (at_class_scope_p ())
21401 /* Create the USING_DECL. */
21402 decl
= do_class_using_decl (qscope
, identifier
);
21404 if (check_for_bare_parameter_packs (decl
))
21405 return error_mark_node
;
21407 if (decl
&& typename_p
)
21408 USING_DECL_TYPENAME_P (decl
) = 1;
21410 /* Add it to the list of members in this class. */
21411 finish_member_declaration (decl
);
21414 finish_nonmember_using_decl (qscope
, identifier
);
21418 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21419 access declaration.
21422 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21423 using :: unqualified-id ;
21425 access-declaration:
21431 cp_parser_using_declaration (cp_parser
* parser
,
21432 bool access_declaration_p
)
21435 bool typename_p
= false;
21436 bool global_scope_p
;
21439 int oldcount
= errorcount
;
21440 cp_token
*diag_token
= NULL
;
21442 if (access_declaration_p
)
21444 diag_token
= cp_lexer_peek_token (parser
->lexer
);
21445 cp_parser_parse_tentatively (parser
);
21449 /* Look for the `using' keyword. */
21450 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
21453 /* Peek at the next token. */
21454 token
= cp_lexer_peek_token (parser
->lexer
);
21455 /* See if it's `typename'. */
21456 if (token
->keyword
== RID_TYPENAME
)
21458 /* Remember that we've seen it. */
21460 /* Consume the `typename' token. */
21461 cp_lexer_consume_token (parser
->lexer
);
21465 /* Look for the optional global scope qualification. */
21467 = (cp_parser_global_scope_opt (parser
,
21468 /*current_scope_valid_p=*/false)
21471 /* If we saw `typename', or didn't see `::', then there must be a
21472 nested-name-specifier present. */
21473 if (typename_p
|| !global_scope_p
)
21475 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
21476 /*check_dependency_p=*/true,
21478 /*is_declaration=*/true);
21479 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
21481 cp_parser_skip_to_end_of_block_or_statement (parser
);
21485 /* Otherwise, we could be in either of the two productions. In that
21486 case, treat the nested-name-specifier as optional. */
21488 qscope
= cp_parser_nested_name_specifier_opt (parser
,
21489 /*typename_keyword_p=*/false,
21490 /*check_dependency_p=*/true,
21492 /*is_declaration=*/true);
21494 qscope
= global_namespace
;
21496 cp_warn_deprecated_use_scopes (qscope
);
21498 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
21499 /* Something has already gone wrong; there's no need to parse
21500 further. Since an error has occurred, the return value of
21501 cp_parser_parse_definitely will be false, as required. */
21502 return cp_parser_parse_definitely (parser
);
21504 token
= cp_lexer_peek_token (parser
->lexer
);
21505 /* Parse the unqualified-id. */
21506 identifier
= cp_parser_unqualified_id (parser
,
21507 /*template_keyword_p=*/false,
21508 /*check_dependency_p=*/true,
21509 /*declarator_p=*/true,
21510 /*optional_p=*/false);
21512 if (access_declaration_p
)
21514 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21515 cp_parser_simulate_error (parser
);
21516 if (!cp_parser_parse_definitely (parser
))
21519 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21521 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
21522 if (cxx_dialect
< cxx17
)
21523 pedwarn (ell
->location
, OPT_Wc__17_extensions
,
21524 "pack expansion in using-declaration only available "
21525 "with %<-std=c++17%> or %<-std=gnu++17%>");
21526 qscope
= make_pack_expansion (qscope
);
21529 /* The function we call to handle a using-declaration is different
21530 depending on what scope we are in. */
21531 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
21533 else if (!identifier_p (identifier
)
21534 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
21535 /* [namespace.udecl]
21537 A using declaration shall not name a template-id. */
21538 error_at (token
->location
,
21539 "a template-id may not appear in a using-declaration");
21542 tree decl
= finish_using_decl (qscope
, identifier
, typename_p
);
21544 if (decl
== error_mark_node
)
21546 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21551 if (!access_declaration_p
21552 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21554 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
21555 if (cxx_dialect
< cxx17
)
21556 pedwarn (comma
->location
, OPT_Wc__17_extensions
,
21557 "comma-separated list in using-declaration only available "
21558 "with %<-std=c++17%> or %<-std=gnu++17%>");
21562 /* Look for the final `;'. */
21563 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21565 if (access_declaration_p
&& errorcount
== oldcount
)
21566 warning_at (diag_token
->location
, OPT_Wdeprecated
,
21567 "access declarations are deprecated "
21568 "in favour of using-declarations; "
21569 "suggestion: add the %<using%> keyword");
21574 /* C++20 using enum declaration.
21576 using-enum-declaration :
21577 using elaborated-enum-specifier ; */
21580 cp_parser_using_enum (cp_parser
*parser
)
21582 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
21584 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21585 breaks one of the motivating examples in using-enum-5.C.
21586 cp_parser_simple_type_specifier seems to be closer to what we actually
21587 want, though that hasn't been properly specified yet. */
21589 /* Consume 'enum'. */
21590 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
));
21591 cp_lexer_consume_token (parser
->lexer
);
21593 cp_token
*start
= cp_lexer_peek_token (parser
->lexer
);
21595 tree type
= (cp_parser_simple_type_specifier
21596 (parser
, NULL
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
21598 cp_token
*end
= cp_lexer_previous_token (parser
->lexer
);
21600 if (type
== error_mark_node
21601 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
21603 cp_parser_skip_to_end_of_block_or_statement (parser
);
21606 if (TREE_CODE (type
) == TYPE_DECL
)
21607 type
= TREE_TYPE (type
);
21609 /* The elaborated-enum-specifier shall not name a dependent type and the type
21610 shall have a reachable enum-specifier. */
21611 const char *msg
= nullptr;
21612 if (cxx_dialect
< cxx20
)
21613 msg
= _("%<using enum%> "
21614 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21615 else if (dependent_type_p (type
))
21616 msg
= _("%<using enum%> of dependent type %qT");
21617 else if (TREE_CODE (type
) != ENUMERAL_TYPE
)
21618 msg
= _("%<using enum%> of non-enumeration type %q#T");
21619 else if (!COMPLETE_TYPE_P (type
))
21620 msg
= _("%<using enum%> of incomplete type %qT");
21621 else if (OPAQUE_ENUM_P (type
))
21622 msg
= _("%<using enum%> of %qT before its enum-specifier");
21625 location_t loc
= make_location (start
, start
, end
);
21626 auto_diagnostic_group g
;
21627 error_at (loc
, msg
, type
);
21628 loc
= location_of (type
);
21629 if (cxx_dialect
< cxx20
|| loc
== input_location
)
21631 else if (OPAQUE_ENUM_P (type
))
21632 inform (loc
, "opaque-enum-declaration here");
21634 inform (loc
, "declared here");
21637 /* A using-enum-declaration introduces the enumerator names of the named
21638 enumeration as if by a using-declaration for each enumerator. */
21639 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
21640 for (tree v
= TYPE_VALUES (type
); v
; v
= TREE_CHAIN (v
))
21641 finish_using_decl (type
, DECL_NAME (TREE_VALUE (v
)));
21644 /* Parse an alias-declaration.
21647 using identifier attribute-specifier-seq [opt] = type-id */
21650 cp_parser_alias_declaration (cp_parser
* parser
)
21652 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
21653 location_t id_location
, type_location
;
21654 cp_declarator
*declarator
;
21655 cp_decl_specifier_seq decl_specs
;
21657 const char *saved_message
= NULL
;
21659 /* Look for the `using' keyword. */
21660 cp_token
*using_token
21661 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
21662 if (using_token
== NULL
)
21663 return error_mark_node
;
21665 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
21666 id
= cp_parser_identifier (parser
);
21667 if (id
== error_mark_node
)
21668 return error_mark_node
;
21670 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
21671 attributes
= cp_parser_attributes_opt (parser
);
21672 if (attributes
== error_mark_node
)
21673 return error_mark_node
;
21675 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
21677 if (cp_parser_error_occurred (parser
))
21678 return error_mark_node
;
21680 cp_parser_commit_to_tentative_parse (parser
);
21682 /* Now we are going to parse the type-id of the declaration. */
21687 "A type-specifier-seq shall not define a class or enumeration
21688 unless it appears in the type-id of an alias-declaration (7.1.3) that
21689 is not the declaration of a template-declaration."
21691 In other words, if we currently are in an alias template, the
21692 type-id should not define a type.
21694 So let's set parser->type_definition_forbidden_message in that
21695 case; cp_parser_check_type_definition (called by
21696 cp_parser_class_specifier) will then emit an error if a type is
21697 defined in the type-id. */
21698 if (parser
->num_template_parameter_lists
)
21700 saved_message
= parser
->type_definition_forbidden_message
;
21701 parser
->type_definition_forbidden_message
=
21702 G_("types may not be defined in alias template declarations");
21705 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
21708 /* Restore the error message if need be. */
21709 if (parser
->num_template_parameter_lists
)
21710 parser
->type_definition_forbidden_message
= saved_message
;
21712 if (type
== error_mark_node
21713 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
21715 cp_parser_skip_to_end_of_block_or_statement (parser
);
21716 return error_mark_node
;
21719 /* A typedef-name can also be introduced by an alias-declaration. The
21720 identifier following the using keyword becomes a typedef-name. It has
21721 the same semantics as if it were introduced by the typedef
21722 specifier. In particular, it does not define a new type and it shall
21723 not appear in the type-id. */
21725 clear_decl_specs (&decl_specs
);
21726 decl_specs
.type
= type
;
21727 if (attributes
!= NULL_TREE
)
21729 decl_specs
.attributes
= attributes
;
21730 set_and_check_decl_spec_loc (&decl_specs
,
21734 set_and_check_decl_spec_loc (&decl_specs
,
21737 set_and_check_decl_spec_loc (&decl_specs
,
21740 decl_specs
.locations
[ds_type_spec
] = type_location
;
21742 if (parser
->num_template_parameter_lists
21743 && !cp_parser_check_template_parameters (parser
,
21744 /*num_templates=*/0,
21745 /*template_id*/false,
21747 /*declarator=*/NULL
))
21748 return error_mark_node
;
21750 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
21752 member_p
= at_class_scope_p ();
21754 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
21755 NULL_TREE
, attributes
);
21757 decl
= start_decl (declarator
, &decl_specs
, 0,
21758 attributes
, NULL_TREE
, &pushed_scope
);
21759 if (decl
== error_mark_node
)
21762 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
21765 pop_scope (pushed_scope
);
21767 /* If decl is a template, return its TEMPLATE_DECL so that it gets
21768 added into the symbol table; otherwise, return the TYPE_DECL. */
21769 if (DECL_LANG_SPECIFIC (decl
)
21770 && DECL_TEMPLATE_INFO (decl
)
21771 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
21773 decl
= DECL_TI_TEMPLATE (decl
);
21775 check_member_template (decl
);
21781 /* Parse a using-directive.
21784 attribute-specifier-seq [opt] using namespace :: [opt]
21785 nested-name-specifier [opt] namespace-name ; */
21788 cp_parser_using_directive (cp_parser
* parser
)
21790 tree namespace_decl
;
21791 tree attribs
= cp_parser_std_attribute_spec_seq (parser
);
21792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21794 /* Error during attribute parsing that resulted in skipping
21795 to next semicolon. */
21796 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21800 /* Look for the `using' keyword. */
21801 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
21802 /* And the `namespace' keyword. */
21803 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
21804 /* Look for the optional `::' operator. */
21805 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
21806 /* And the optional nested-name-specifier. */
21807 cp_parser_nested_name_specifier_opt (parser
,
21808 /*typename_keyword_p=*/false,
21809 /*check_dependency_p=*/true,
21811 /*is_declaration=*/true);
21812 /* Get the namespace being used. */
21813 namespace_decl
= cp_parser_namespace_name (parser
);
21814 cp_warn_deprecated_use_scopes (namespace_decl
);
21815 /* And any specified GNU attributes. */
21816 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
21817 attribs
= chainon (attribs
, cp_parser_gnu_attributes_opt (parser
));
21819 /* Update the symbol table. */
21820 finish_using_directive (namespace_decl
, attribs
);
21822 /* Look for the final `;'. */
21823 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21826 /* Parse an asm-definition.
21833 asm-qualifier-list:
21835 asm-qualifier-list asm-qualifier
21838 asm ( string-literal ) ;
21843 asm asm-qualifier-list [opt] ( string-literal ) ;
21844 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
21845 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21846 : asm-operand-list [opt] ) ;
21847 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21848 : asm-operand-list [opt]
21849 : asm-clobber-list [opt] ) ;
21850 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
21851 : asm-clobber-list [opt]
21852 : asm-goto-list ) ;
21854 The form with asm-goto-list is valid if and only if the asm-qualifier-list
21855 contains goto, and is the only allowed form in that case. No duplicates are
21856 allowed in an asm-qualifier-list. */
21859 cp_parser_asm_definition (cp_parser
* parser
)
21862 tree outputs
= NULL_TREE
;
21863 tree inputs
= NULL_TREE
;
21864 tree clobbers
= NULL_TREE
;
21865 tree labels
= NULL_TREE
;
21867 bool extended_p
= false;
21868 bool invalid_inputs_p
= false;
21869 bool invalid_outputs_p
= false;
21870 required_token missing
= RT_NONE
;
21871 location_t asm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21873 /* Look for the `asm' keyword. */
21874 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
21876 /* In C++20, unevaluated inline assembly is permitted in constexpr
21878 if (parser
->in_function_body
21879 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
21880 && cxx_dialect
< cxx20
)
21881 pedwarn (asm_loc
, OPT_Wc__20_extensions
, "%<asm%> in %<constexpr%> "
21882 "function only available with %<-std=c++20%> or "
21883 "%<-std=gnu++20%>");
21885 /* Handle the asm-qualifier-list. */
21886 location_t volatile_loc
= UNKNOWN_LOCATION
;
21887 location_t inline_loc
= UNKNOWN_LOCATION
;
21888 location_t goto_loc
= UNKNOWN_LOCATION
;
21889 location_t first_loc
= UNKNOWN_LOCATION
;
21891 if (cp_parser_allow_gnu_extensions_p (parser
))
21894 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21895 location_t loc
= token
->location
;
21896 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
21901 error_at (loc
, "duplicate %<asm%> qualifier %qT",
21903 inform (volatile_loc
, "first seen here");
21907 if (!parser
->in_function_body
)
21908 warning_at (loc
, 0, "%<asm%> qualifier %qT ignored "
21909 "outside of function body", token
->u
.value
);
21910 volatile_loc
= loc
;
21912 cp_lexer_consume_token (parser
->lexer
);
21918 error_at (loc
, "duplicate %<asm%> qualifier %qT",
21920 inform (inline_loc
, "first seen here");
21926 cp_lexer_consume_token (parser
->lexer
);
21932 error_at (loc
, "duplicate %<asm%> qualifier %qT",
21934 inform (goto_loc
, "first seen here");
21940 cp_lexer_consume_token (parser
->lexer
);
21945 error_at (loc
, "%qT is not an %<asm%> qualifier", token
->u
.value
);
21946 cp_lexer_consume_token (parser
->lexer
);
21955 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
21956 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
21957 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
21959 if (!parser
->in_function_body
&& (inline_p
|| goto_p
))
21961 error_at (first_loc
, "%<asm%> qualifier outside of function body");
21962 inline_p
= goto_p
= false;
21965 /* Look for the opening `('. */
21966 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
21968 /* Look for the string. */
21969 string
= cp_parser_string_literal (parser
, false, false);
21970 if (string
== error_mark_node
)
21972 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
21973 /*consume_paren=*/true);
21977 /* If we're allowing GNU extensions, check for the extended assembly
21978 syntax. Unfortunately, the `:' tokens need not be separated by
21979 a space in C, and so, for compatibility, we tolerate that here
21980 too. Doing that means that we have to treat the `::' operator as
21982 if (cp_parser_allow_gnu_extensions_p (parser
)
21983 && parser
->in_function_body
21984 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
21985 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
21987 bool inputs_p
= false;
21988 bool clobbers_p
= false;
21989 bool labels_p
= false;
21991 /* The extended syntax was used. */
21994 /* Look for outputs. */
21995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
21997 /* Consume the `:'. */
21998 cp_lexer_consume_token (parser
->lexer
);
21999 /* Parse the output-operands. */
22000 if (cp_lexer_next_token_is_not (parser
->lexer
,
22002 && cp_lexer_next_token_is_not (parser
->lexer
,
22004 && cp_lexer_next_token_is_not (parser
->lexer
,
22007 outputs
= cp_parser_asm_operand_list (parser
);
22008 if (outputs
== error_mark_node
)
22009 invalid_outputs_p
= true;
22012 /* If the next token is `::', there are no outputs, and the
22013 next token is the beginning of the inputs. */
22014 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22015 /* The inputs are coming next. */
22018 /* Look for inputs. */
22020 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22022 /* Consume the `:' or `::'. */
22023 cp_lexer_consume_token (parser
->lexer
);
22024 /* Parse the output-operands. */
22025 if (cp_lexer_next_token_is_not (parser
->lexer
,
22027 && cp_lexer_next_token_is_not (parser
->lexer
,
22029 && cp_lexer_next_token_is_not (parser
->lexer
,
22032 inputs
= cp_parser_asm_operand_list (parser
);
22033 if (inputs
== error_mark_node
)
22034 invalid_inputs_p
= true;
22037 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22038 /* The clobbers are coming next. */
22041 /* Look for clobbers. */
22043 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22046 /* Consume the `:' or `::'. */
22047 cp_lexer_consume_token (parser
->lexer
);
22048 /* Parse the clobbers. */
22049 if (cp_lexer_next_token_is_not (parser
->lexer
,
22051 && cp_lexer_next_token_is_not (parser
->lexer
,
22053 clobbers
= cp_parser_asm_clobber_list (parser
);
22055 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22056 /* The labels are coming next. */
22059 /* Look for labels. */
22061 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
22064 /* Consume the `:' or `::'. */
22065 cp_lexer_consume_token (parser
->lexer
);
22066 /* Parse the labels. */
22067 labels
= cp_parser_asm_label_list (parser
);
22070 if (goto_p
&& !labels_p
)
22071 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
22074 missing
= RT_COLON_SCOPE
;
22076 /* Look for the closing `)'. */
22077 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
22078 missing
? missing
: RT_CLOSE_PAREN
))
22079 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
22080 /*consume_paren=*/true);
22081 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22083 if (!invalid_inputs_p
&& !invalid_outputs_p
)
22085 /* Create the ASM_EXPR. */
22086 if (parser
->in_function_body
)
22088 asm_stmt
= finish_asm_stmt (asm_loc
, volatile_p
, string
, outputs
,
22089 inputs
, clobbers
, labels
, inline_p
);
22090 /* If the extended syntax was not used, mark the ASM_EXPR. */
22093 tree temp
= asm_stmt
;
22094 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
22095 temp
= TREE_OPERAND (temp
, 0);
22097 ASM_INPUT_P (temp
) = 1;
22101 symtab
->finalize_toplevel_asm (string
);
22105 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22106 type that comes from the decl-specifier-seq. */
22109 strip_declarator_types (tree type
, cp_declarator
*declarator
)
22111 for (cp_declarator
*d
= declarator
; d
;)
22121 if (TYPE_PTRMEMFUNC_P (type
))
22122 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
22123 type
= TREE_TYPE (type
);
22131 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22132 a construct looks like a variable definition but is actually a function
22133 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22134 is the declarator for this function declaration. */
22137 warn_about_ambiguous_parse (const cp_decl_specifier_seq
*decl_specifiers
,
22138 const cp_declarator
*declarator
)
22140 /* Only warn if we are declaring a function at block scope. */
22141 if (!at_function_scope_p ())
22144 /* And only if there is no storage class specified. */
22145 if (decl_specifiers
->storage_class
!= sc_none
22146 || decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
22149 if (declarator
->kind
!= cdk_function
22150 || !declarator
->declarator
22151 || declarator
->declarator
->kind
!= cdk_id
22152 || !identifier_p (get_unqualified_id
22153 (const_cast<cp_declarator
*>(declarator
))))
22156 /* Don't warn when the whole declarator (not just the declarator-id!)
22157 was parenthesized. That is, don't warn for int(n()) but do warn
22159 if (declarator
->parenthesized
!= UNKNOWN_LOCATION
)
22163 if (decl_specifiers
->type
)
22165 type
= decl_specifiers
->type
;
22166 if (TREE_CODE (type
) == TYPE_DECL
)
22167 type
= TREE_TYPE (type
);
22169 /* If the return type is void there is no ambiguity. */
22170 if (same_type_p (type
, void_type_node
))
22173 else if (decl_specifiers
->any_type_specifiers_p
)
22174 /* Code like long f(); will have null ->type. If we have any
22175 type-specifiers, pretend we've seen int. */
22176 type
= integer_type_node
;
22180 auto_diagnostic_group d
;
22181 location_t loc
= declarator
->u
.function
.parens_loc
;
22182 tree params
= declarator
->u
.function
.parameters
;
22183 const bool has_list_ctor_p
= CLASS_TYPE_P (type
) && TYPE_HAS_LIST_CTOR (type
);
22185 /* The T t() case. */
22186 if (params
== void_list_node
)
22188 if (warning_at (loc
, OPT_Wvexing_parse
,
22189 "empty parentheses were disambiguated as a function "
22192 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22193 means value-initialization or aggregate-initialization, nothing
22194 means default-initialization. We can only suggest removing the
22195 parentheses/adding {} if T has a default constructor. */
22196 if (!CLASS_TYPE_P (type
) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
22198 gcc_rich_location
iloc (loc
);
22199 iloc
.add_fixit_remove ();
22200 inform (&iloc
, "remove parentheses to default-initialize "
22202 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
22204 if (CP_AGGREGATE_TYPE_P (type
))
22205 inform (loc
, "or replace parentheses with braces to "
22206 "aggregate-initialize a variable");
22208 inform (loc
, "or replace parentheses with braces to "
22209 "value-initialize a variable");
22216 /* If we had (...) or the parameter-list wasn't parenthesized,
22218 if (params
== NULL_TREE
|| !PARENTHESIZED_LIST_P (params
))
22221 /* The T t(X()) case. */
22222 if (list_length (params
) == 2)
22224 if (warning_at (loc
, OPT_Wvexing_parse
,
22225 "parentheses were disambiguated as a function "
22228 gcc_rich_location
iloc (loc
);
22229 /* {}-initialization means that we can use an initializer-list
22230 constructor if no default constructor is available, so don't
22231 suggest using {} for classes that have an initializer_list
22233 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
22235 iloc
.add_fixit_replace (get_start (loc
), "{");
22236 iloc
.add_fixit_replace (get_finish (loc
), "}");
22237 inform (&iloc
, "replace parentheses with braces to declare a "
22242 iloc
.add_fixit_insert_after (get_start (loc
), "(");
22243 iloc
.add_fixit_insert_before (get_finish (loc
), ")");
22244 inform (&iloc
, "add parentheses to declare a variable");
22248 /* The T t(X(), X()) case. */
22249 else if (warning_at (loc
, OPT_Wvexing_parse
,
22250 "parentheses were disambiguated as a function "
22253 gcc_rich_location
iloc (loc
);
22254 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
22256 iloc
.add_fixit_replace (get_start (loc
), "{");
22257 iloc
.add_fixit_replace (get_finish (loc
), "}");
22258 inform (&iloc
, "replace parentheses with braces to declare a "
22264 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22265 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22266 has the same meaning as in cp_parser_declarator. */
22269 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser
*parser
,
22270 cp_decl_specifier_seq
*decl_specs
,
22271 cp_declarator
*declarator
,
22272 int *ctor_dtor_or_conv_p
)
22274 if (cxx_dialect
>= cxx17
22275 && *ctor_dtor_or_conv_p
<= 0
22276 && !decl_specs
->type
22277 && !decl_specs
->any_type_specifiers_p
22278 && function_declarator_p (declarator
))
22280 cp_declarator
*id
= get_id_declarator (declarator
);
22281 tree name
= id
->u
.id
.unqualified_name
;
22282 parser
->scope
= id
->u
.id
.qualifying_scope
;
22283 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
22285 && (DECL_CLASS_TEMPLATE_P (tmpl
)
22286 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
22288 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
22289 id
->u
.id
.sfk
= sfk_deduction_guide
;
22290 *ctor_dtor_or_conv_p
= 1;
22295 /* Declarators [gram.dcl.decl] */
22297 /* Parse an init-declarator.
22300 declarator initializer [opt]
22305 declarator asm-specification [opt] attributes [opt] initializer [opt]
22307 function-definition:
22308 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22310 decl-specifier-seq [opt] declarator function-try-block
22314 function-definition:
22315 __extension__ function-definition
22319 function-definition:
22320 decl-specifier-seq [opt] declarator function-transaction-block
22322 The parser flags FLAGS is used to control type-specifier parsing.
22324 The DECL_SPECIFIERS apply to this declarator. Returns a
22325 representation of the entity declared. If MEMBER_P is TRUE, then
22326 this declarator appears in a class scope. The new DECL created by
22327 this declarator is returned.
22329 The CHECKS are access checks that should be performed once we know
22330 what entity is being declared (and, therefore, what classes have
22333 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22334 for a function-definition here as well. If the declarator is a
22335 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22336 be TRUE upon return. By that point, the function-definition will
22337 have been completely parsed.
22339 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22342 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22343 parsed declaration if it is an uninitialized single declarator not followed
22344 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22345 if present, will not be consumed. If returned, this declarator will be
22346 created with SD_INITIALIZED but will not call cp_finish_decl.
22348 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22349 and there is an initializer, the pointed location_t is set to the
22350 location of the '=' or `(', or '{' in C++11 token introducing the
22354 cp_parser_init_declarator (cp_parser
* parser
,
22355 cp_parser_flags flags
,
22356 cp_decl_specifier_seq
*decl_specifiers
,
22357 vec
<deferred_access_check
, va_gc
> *checks
,
22358 bool function_definition_allowed_p
,
22360 int declares_class_or_enum
,
22361 bool* function_definition_p
,
22362 tree
* maybe_range_for_decl
,
22363 location_t
* init_loc
,
22366 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
22367 *attributes_start_token
= NULL
;
22368 cp_declarator
*declarator
;
22369 tree prefix_attributes
;
22370 tree attributes
= NULL
;
22371 tree asm_specification
;
22373 tree decl
= NULL_TREE
;
22375 int is_initialized
;
22376 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22377 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22379 enum cpp_ttype initialization_kind
;
22380 bool is_direct_init
= false;
22381 bool is_non_constant_init
;
22382 int ctor_dtor_or_conv_p
;
22383 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
22384 bool static_p
= decl_specifiers
->storage_class
== sc_static
;
22385 tree pushed_scope
= NULL_TREE
;
22386 bool range_for_decl_p
= false;
22387 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22388 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
22390 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_consteval
))
22391 flags
|= CP_PARSER_FLAGS_CONSTEVAL
;
22393 /* Assume that this is not the declarator for a function
22395 if (function_definition_p
)
22396 *function_definition_p
= false;
22398 /* Default arguments are only permitted for function parameters. */
22399 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
22400 parser
->default_arg_ok_p
= false;
22402 /* Defer access checks while parsing the declarator; we cannot know
22403 what names are accessible until we know what is being
22405 resume_deferring_access_checks ();
22407 token
= cp_lexer_peek_token (parser
->lexer
);
22409 /* Parse the declarator. */
22411 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
22412 flags
, &ctor_dtor_or_conv_p
,
22413 /*parenthesized_p=*/NULL
,
22414 member_p
, friend_p
, static_p
);
22415 /* Gather up the deferred checks. */
22416 stop_deferring_access_checks ();
22418 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22420 /* If the DECLARATOR was erroneous, there's no need to go
22422 if (declarator
== cp_error_declarator
)
22423 return error_mark_node
;
22425 /* Check that the number of template-parameter-lists is OK. */
22426 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
22428 return error_mark_node
;
22430 if (declares_class_or_enum
& 2)
22431 cp_parser_check_for_definition_in_return_type (declarator
,
22432 decl_specifiers
->type
,
22433 decl_specifiers
->locations
[ds_type_spec
]);
22435 /* Figure out what scope the entity declared by the DECLARATOR is
22436 located in. `grokdeclarator' sometimes changes the scope, so
22437 we compute it now. */
22438 scope
= get_scope_of_declarator (declarator
);
22440 /* Perform any lookups in the declared type which were thought to be
22441 dependent, but are not in the scope of the declarator. */
22442 decl_specifiers
->type
22443 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
22445 /* If we're allowing GNU extensions, look for an
22446 asm-specification. */
22447 if (cp_parser_allow_gnu_extensions_p (parser
))
22449 /* Look for an asm-specification. */
22450 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
22451 asm_specification
= cp_parser_asm_specification_opt (parser
);
22454 asm_specification
= NULL_TREE
;
22456 /* Gather the attributes that were provided with the
22457 decl-specifiers. */
22458 prefix_attributes
= decl_specifiers
->attributes
;
22460 /* Look for attributes. */
22461 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
22462 attributes
= cp_parser_attributes_opt (parser
);
22464 /* Peek at the next token. */
22465 token
= cp_lexer_peek_token (parser
->lexer
);
22467 bool bogus_implicit_tmpl
= false;
22469 if (function_declarator_p (declarator
))
22471 /* Handle C++17 deduction guides. Note that class-scope
22472 non-template deduction guides are instead handled in
22473 cp_parser_member_declaration. */
22474 cp_parser_maybe_adjust_declarator_for_dguide (parser
,
22477 &ctor_dtor_or_conv_p
);
22479 if (!member_p
&& !cp_parser_error_occurred (parser
))
22480 warn_about_ambiguous_parse (decl_specifiers
, declarator
);
22482 /* Check to see if the token indicates the start of a
22483 function-definition. */
22484 if (cp_parser_token_starts_function_definition_p (token
))
22486 if (!function_definition_allowed_p
)
22488 /* If a function-definition should not appear here, issue an
22490 cp_parser_error (parser
,
22491 "a function-definition is not allowed here");
22492 return error_mark_node
;
22495 location_t func_brace_location
22496 = cp_lexer_peek_token (parser
->lexer
)->location
;
22498 /* Neither attributes nor an asm-specification are allowed
22499 on a function-definition. */
22500 if (asm_specification
)
22501 error_at (asm_spec_start_token
->location
,
22502 "an %<asm%> specification is not allowed "
22503 "on a function-definition");
22505 error_at (attributes_start_token
->location
,
22506 "attributes are not allowed "
22507 "on a function-definition");
22508 /* This is a function-definition. */
22509 *function_definition_p
= true;
22511 /* Parse the function definition. */
22513 decl
= cp_parser_save_member_function_body (parser
,
22516 prefix_attributes
);
22519 (cp_parser_function_definition_from_specifiers_and_declarator
22520 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
22522 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
22524 /* This is where the prologue starts... */
22525 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
22526 = func_brace_location
;
22532 else if (parser
->fully_implicit_function_template_p
)
22534 /* A non-template declaration involving a function parameter list
22535 containing an implicit template parameter will be made into a
22536 template. If the resulting declaration is not going to be an
22537 actual function then finish the template scope here to prevent it.
22538 An error message will be issued once we have a decl to talk about.
22540 FIXME probably we should do type deduction rather than create an
22541 implicit template, but the standard currently doesn't allow it. */
22542 bogus_implicit_tmpl
= true;
22543 finish_fully_implicit_template (parser
, NULL_TREE
);
22548 Only in function declarations for constructors, destructors, type
22549 conversions, and deduction guides can the decl-specifier-seq be omitted.
22551 We explicitly postpone this check past the point where we handle
22552 function-definitions because we tolerate function-definitions
22553 that are missing their return types in some modes. */
22554 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
22556 cp_parser_error (parser
,
22557 "expected constructor, destructor, or type conversion");
22558 return error_mark_node
;
22561 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
22562 if (token
->type
== CPP_EQ
22563 || token
->type
== CPP_OPEN_PAREN
22564 || token
->type
== CPP_OPEN_BRACE
)
22566 is_initialized
= SD_INITIALIZED
;
22567 initialization_kind
= token
->type
;
22568 declarator
->init_loc
= token
->location
;
22569 if (maybe_range_for_decl
)
22570 *maybe_range_for_decl
= error_mark_node
;
22571 tmp_init_loc
= token
->location
;
22572 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
22573 *init_loc
= tmp_init_loc
;
22575 if (token
->type
== CPP_EQ
22576 && function_declarator_p (declarator
))
22578 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22579 if (t2
->keyword
== RID_DEFAULT
)
22580 is_initialized
= SD_DEFAULTED
;
22581 else if (t2
->keyword
== RID_DELETE
)
22582 is_initialized
= SD_DELETED
;
22587 /* If the init-declarator isn't initialized and isn't followed by a
22588 `,' or `;', it's not a valid init-declarator. */
22589 if (token
->type
!= CPP_COMMA
22590 && token
->type
!= CPP_SEMICOLON
)
22592 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
22593 range_for_decl_p
= true;
22596 if (!maybe_range_for_decl
)
22597 cp_parser_error (parser
, "expected initializer");
22598 return error_mark_node
;
22601 is_initialized
= SD_UNINITIALIZED
;
22602 initialization_kind
= CPP_EOF
;
22605 /* Because start_decl has side-effects, we should only call it if we
22606 know we're going ahead. By this point, we know that we cannot
22607 possibly be looking at any other construct. */
22608 cp_parser_commit_to_tentative_parse (parser
);
22610 /* Enter the newly declared entry in the symbol table. If we're
22611 processing a declaration in a class-specifier, we wait until
22612 after processing the initializer. */
22615 if (parser
->in_unbraced_linkage_specification_p
)
22616 decl_specifiers
->storage_class
= sc_extern
;
22617 decl
= start_decl (declarator
, decl_specifiers
,
22618 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
22619 attributes
, prefix_attributes
, &pushed_scope
);
22620 cp_finalize_omp_declare_simd (parser
, decl
);
22621 cp_finalize_oacc_routine (parser
, decl
, false);
22622 /* Adjust location of decl if declarator->id_loc is more appropriate:
22623 set, and decl wasn't merged with another decl, in which case its
22624 location would be different from input_location, and more accurate. */
22626 && declarator
->id_loc
!= UNKNOWN_LOCATION
22627 && DECL_SOURCE_LOCATION (decl
) == input_location
)
22628 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
22631 /* Enter the SCOPE. That way unqualified names appearing in the
22632 initializer will be looked up in SCOPE. */
22633 pushed_scope
= push_scope (scope
);
22635 /* Perform deferred access control checks, now that we know in which
22636 SCOPE the declared entity resides. */
22637 if (!member_p
&& decl
)
22639 tree saved_current_function_decl
= NULL_TREE
;
22641 /* If the entity being declared is a function, pretend that we
22642 are in its scope. If it is a `friend', it may have access to
22643 things that would not otherwise be accessible. */
22644 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22646 saved_current_function_decl
= current_function_decl
;
22647 current_function_decl
= decl
;
22650 /* Perform access checks for template parameters. */
22651 cp_parser_perform_template_parameter_access_checks (checks
);
22653 /* Perform the access control checks for the declarator and the
22654 decl-specifiers. */
22655 perform_deferred_access_checks (tf_warning_or_error
);
22657 /* Restore the saved value. */
22658 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22659 current_function_decl
= saved_current_function_decl
;
22662 /* Parse the initializer. */
22663 initializer
= NULL_TREE
;
22664 is_direct_init
= false;
22665 is_non_constant_init
= true;
22666 if (is_initialized
)
22668 if (function_declarator_p (declarator
))
22670 if (initialization_kind
== CPP_EQ
)
22671 initializer
= cp_parser_pure_specifier (parser
);
22674 /* If the declaration was erroneous, we don't really
22675 know what the user intended, so just silently
22676 consume the initializer. */
22677 if (decl
!= error_mark_node
)
22678 error_at (tmp_init_loc
, "initializer provided for function");
22679 cp_parser_skip_to_closing_parenthesis (parser
,
22680 /*recovering=*/true,
22681 /*or_comma=*/false,
22682 /*consume_paren=*/true);
22687 /* We want to record the extra mangling scope for in-class
22688 initializers of class members and initializers of static
22689 data member templates and namespace-scope initializers.
22690 The former involves deferring parsing of the initializer
22691 until end of class as with default arguments. So right
22692 here we only handle the latter two. */
22693 bool has_lambda_scope
= false;
22695 if (decl
!= error_mark_node
22697 && (processing_template_decl
|| DECL_NAMESPACE_SCOPE_P (decl
)))
22698 has_lambda_scope
= true;
22700 if (has_lambda_scope
)
22701 start_lambda_scope (decl
);
22702 initializer
= cp_parser_initializer (parser
,
22704 &is_non_constant_init
);
22705 if (has_lambda_scope
)
22706 finish_lambda_scope ();
22707 if (initializer
== error_mark_node
)
22708 cp_parser_skip_to_end_of_statement (parser
);
22712 /* The old parser allows attributes to appear after a parenthesized
22713 initializer. Mark Mitchell proposed removing this functionality
22714 on the GCC mailing lists on 2002-08-13. This parser accepts the
22715 attributes -- but ignores them. Made a permerror in GCC 8. */
22716 if (cp_parser_allow_gnu_extensions_p (parser
)
22717 && initialization_kind
== CPP_OPEN_PAREN
22718 && cp_parser_attributes_opt (parser
)
22719 && permerror (input_location
,
22720 "attributes after parenthesized initializer ignored"))
22723 if (flag_permissive
&& !hint
)
22726 inform (input_location
,
22727 "this flexibility is deprecated and will be removed");
22731 /* And now complain about a non-function implicit template. */
22732 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
22733 error_at (DECL_SOURCE_LOCATION (decl
),
22734 "non-function %qD declared as implicit template", decl
);
22736 /* For an in-class declaration, use `grokfield' to create the
22742 pop_scope (pushed_scope
);
22743 pushed_scope
= NULL_TREE
;
22745 decl
= grokfield (declarator
, decl_specifiers
,
22746 initializer
, !is_non_constant_init
,
22747 /*asmspec=*/NULL_TREE
,
22748 attr_chainon (attributes
, prefix_attributes
));
22749 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
22750 cp_parser_save_default_args (parser
, decl
);
22751 cp_finalize_omp_declare_simd (parser
, decl
);
22752 cp_finalize_oacc_routine (parser
, decl
, false);
22755 /* Finish processing the declaration. But, skip member
22757 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
22759 cp_finish_decl (decl
,
22760 initializer
, !is_non_constant_init
,
22762 /* If the initializer is in parentheses, then this is
22763 a direct-initialization, which means that an
22764 `explicit' constructor is OK. Otherwise, an
22765 `explicit' constructor cannot be used. */
22766 ((is_direct_init
|| !is_initialized
)
22767 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
22769 else if ((cxx_dialect
!= cxx98
) && friend_p
22770 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
22771 /* Core issue #226 (C++0x only): A default template-argument
22772 shall not be specified in a friend class template
22774 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
22775 /*is_partial=*/false, /*is_friend_decl=*/1);
22777 if (!friend_p
&& pushed_scope
)
22778 pop_scope (pushed_scope
);
22780 if (function_declarator_p (declarator
)
22781 && parser
->fully_implicit_function_template_p
)
22784 decl
= finish_fully_implicit_template (parser
, decl
);
22786 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
22789 if (auto_result
&& is_initialized
&& decl_specifiers
->type
22790 && type_uses_auto (decl_specifiers
->type
))
22791 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
22796 /* Parse a declarator.
22800 ptr-operator declarator
22802 abstract-declarator:
22803 ptr-operator abstract-declarator [opt]
22804 direct-abstract-declarator
22809 attributes [opt] direct-declarator
22810 attributes [opt] ptr-operator declarator
22812 abstract-declarator:
22813 attributes [opt] ptr-operator abstract-declarator [opt]
22814 attributes [opt] direct-abstract-declarator
22816 The parser flags FLAGS is used to control type-specifier parsing.
22818 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
22819 detect constructors, destructors, deduction guides, or conversion operators.
22820 It is set to -1 if the declarator is a name, and +1 if it is a
22821 function. Otherwise it is set to zero. Usually you just want to
22822 test for >0, but internally the negative value is used.
22824 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
22825 a decl-specifier-seq unless it declares a constructor, destructor,
22826 or conversion. It might seem that we could check this condition in
22827 semantic analysis, rather than parsing, but that makes it difficult
22828 to handle something like `f()'. We want to notice that there are
22829 no decl-specifiers, and therefore realize that this is an
22830 expression, not a declaration.)
22832 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
22833 the declarator is a direct-declarator of the form "(...)".
22835 MEMBER_P is true iff this declarator is a member-declarator.
22837 FRIEND_P is true iff this declarator is a friend.
22839 STATIC_P is true iff the keyword static was seen. */
22841 static cp_declarator
*
22842 cp_parser_declarator (cp_parser
* parser
,
22843 cp_parser_declarator_kind dcl_kind
,
22844 cp_parser_flags flags
,
22845 int* ctor_dtor_or_conv_p
,
22846 bool* parenthesized_p
,
22847 bool member_p
, bool friend_p
, bool static_p
)
22849 cp_declarator
*declarator
;
22850 enum tree_code code
;
22851 cp_cv_quals cv_quals
;
22853 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
22855 /* Assume this is not a constructor, destructor, or type-conversion
22857 if (ctor_dtor_or_conv_p
)
22858 *ctor_dtor_or_conv_p
= 0;
22860 if (cp_parser_allow_gnu_extensions_p (parser
))
22861 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
22863 /* Check for the ptr-operator production. */
22864 cp_parser_parse_tentatively (parser
);
22865 /* Parse the ptr-operator. */
22866 code
= cp_parser_ptr_operator (parser
,
22871 /* If that worked, then we have a ptr-operator. */
22872 if (cp_parser_parse_definitely (parser
))
22874 /* If a ptr-operator was found, then this declarator was not
22876 if (parenthesized_p
)
22877 *parenthesized_p
= false;
22878 /* The dependent declarator is optional if we are parsing an
22879 abstract-declarator. */
22880 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
22881 cp_parser_parse_tentatively (parser
);
22883 /* Parse the dependent declarator. */
22884 declarator
= cp_parser_declarator (parser
, dcl_kind
, flags
,
22885 /*ctor_dtor_or_conv_p=*/NULL
,
22886 /*parenthesized_p=*/NULL
,
22887 member_p
, friend_p
, static_p
);
22889 /* If we are parsing an abstract-declarator, we must handle the
22890 case where the dependent declarator is absent. */
22891 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
22892 && !cp_parser_parse_definitely (parser
))
22895 declarator
= cp_parser_make_indirect_declarator
22896 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
22898 /* Everything else is a direct-declarator. */
22901 if (parenthesized_p
)
22902 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
22904 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
22905 flags
, ctor_dtor_or_conv_p
,
22906 member_p
, friend_p
, static_p
);
22909 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
22910 declarator
->attributes
= gnu_attributes
;
22914 /* Parse a direct-declarator or direct-abstract-declarator.
22918 direct-declarator ( parameter-declaration-clause )
22919 cv-qualifier-seq [opt]
22920 ref-qualifier [opt]
22921 exception-specification [opt]
22922 direct-declarator [ constant-expression [opt] ]
22925 direct-abstract-declarator:
22926 direct-abstract-declarator [opt]
22927 ( parameter-declaration-clause )
22928 cv-qualifier-seq [opt]
22929 ref-qualifier [opt]
22930 exception-specification [opt]
22931 direct-abstract-declarator [opt] [ constant-expression [opt] ]
22932 ( abstract-declarator )
22934 Returns a representation of the declarator. DCL_KIND is
22935 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
22936 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
22937 we are parsing a direct-declarator. It is
22938 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
22939 of ambiguity we prefer an abstract declarator, as per
22941 The parser flags FLAGS is used to control type-specifier parsing.
22942 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
22943 as for cp_parser_declarator. */
22945 static cp_declarator
*
22946 cp_parser_direct_declarator (cp_parser
* parser
,
22947 cp_parser_declarator_kind dcl_kind
,
22948 cp_parser_flags flags
,
22949 int* ctor_dtor_or_conv_p
,
22950 bool member_p
, bool friend_p
, bool static_p
)
22953 cp_declarator
*declarator
= NULL
;
22954 tree scope
= NULL_TREE
;
22955 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22956 bool saved_in_declarator_p
= parser
->in_declarator_p
;
22958 tree pushed_scope
= NULL_TREE
;
22959 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
22963 /* Peek at the next token. */
22964 token
= cp_lexer_peek_token (parser
->lexer
);
22965 if (token
->type
== CPP_OPEN_PAREN
)
22967 /* This is either a parameter-declaration-clause, or a
22968 parenthesized declarator. When we know we are parsing a
22969 named declarator, it must be a parenthesized declarator
22970 if FIRST is true. For instance, `(int)' is a
22971 parameter-declaration-clause, with an omitted
22972 direct-abstract-declarator. But `((*))', is a
22973 parenthesized abstract declarator. Finally, when T is a
22974 template parameter `(T)' is a
22975 parameter-declaration-clause, and not a parenthesized
22978 We first try and parse a parameter-declaration-clause,
22979 and then try a nested declarator (if FIRST is true).
22981 It is not an error for it not to be a
22982 parameter-declaration-clause, even when FIRST is
22988 The first is the declaration of a function while the
22989 second is the definition of a variable, including its
22992 Having seen only the parenthesis, we cannot know which of
22993 these two alternatives should be selected. Even more
22994 complex are examples like:
22999 The former is a function-declaration; the latter is a
23000 variable initialization.
23002 Thus again, we try a parameter-declaration-clause, and if
23003 that fails, we back out and return. */
23005 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
23008 bool is_declarator
= false;
23012 /* In a member-declarator, the only valid interpretation
23013 of a parenthesis is the start of a
23014 parameter-declaration-clause. (It is invalid to
23015 initialize a static data member with a parenthesized
23016 initializer; only the "=" form of initialization is
23019 cp_parser_parse_tentatively (parser
);
23021 /* Consume the `('. */
23022 const location_t parens_start
= token
->location
;
23023 matching_parens parens
;
23024 parens
.consume_open (parser
);
23027 /* If this is going to be an abstract declarator, we're
23028 in a declarator and we can't have default args. */
23029 parser
->default_arg_ok_p
= false;
23030 parser
->in_declarator_p
= true;
23033 begin_scope (sk_function_parms
, NULL_TREE
);
23035 /* Signal we are in the immediate function context. */
23036 if (flags
& CP_PARSER_FLAGS_CONSTEVAL
)
23037 current_binding_level
->immediate_fn_ctx_p
= true;
23039 /* Parse the parameter-declaration-clause. */
23041 = cp_parser_parameter_declaration_clause (parser
, flags
);
23042 const location_t parens_end
23043 = cp_lexer_peek_token (parser
->lexer
)->location
;
23045 /* Consume the `)'. */
23046 parens
.require_close (parser
);
23048 /* If all went well, parse the cv-qualifier-seq,
23049 ref-qualifier and the exception-specification. */
23050 if (member_p
|| cp_parser_parse_definitely (parser
))
23052 cp_cv_quals cv_quals
;
23053 cp_virt_specifiers virt_specifiers
;
23054 cp_ref_qualifier ref_qual
;
23055 tree exception_specification
;
23058 bool memfn
= (member_p
|| (pushed_scope
23059 && CLASS_TYPE_P (pushed_scope
)));
23060 unsigned char local_variables_forbidden_p
23061 = parser
->local_variables_forbidden_p
;
23062 /* 'this' is not allowed in static member functions. */
23063 if (static_p
|| friend_p
)
23064 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
23066 is_declarator
= true;
23068 if (ctor_dtor_or_conv_p
)
23069 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
23072 /* Parse the cv-qualifier-seq. */
23073 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
23074 /* Parse the ref-qualifier. */
23075 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
23076 /* Parse the tx-qualifier. */
23077 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
23079 tree save_ccp
= current_class_ptr
;
23080 tree save_ccr
= current_class_ref
;
23081 if (memfn
&& !friend_p
&& !static_p
)
23082 /* DR 1207: 'this' is in scope after the cv-quals. */
23083 inject_this_parameter (current_class_type
, cv_quals
);
23085 /* If it turned out that this is e.g. a pointer to a
23086 function, we don't want to delay noexcept parsing. */
23087 if (declarator
== NULL
|| declarator
->kind
!= cdk_id
)
23088 flags
&= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
23090 /* Parse the exception-specification. */
23091 exception_specification
23092 = cp_parser_exception_specification_opt (parser
,
23095 attrs
= cp_parser_std_attribute_spec_seq (parser
);
23097 cp_omp_declare_simd_data odsd
;
23098 if ((flag_openmp
|| flag_openmp_simd
)
23100 && declarator
->std_attributes
23101 && declarator
->kind
== cdk_id
)
23103 tree
*pa
= &declarator
->std_attributes
;
23104 cp_parser_handle_directive_omp_attributes (parser
, pa
,
23108 /* In here, we handle cases where attribute is used after
23109 the function declaration. For example:
23110 void func (int x) __attribute__((vector(..))); */
23111 tree gnu_attrs
= NULL_TREE
;
23112 tree requires_clause
= NULL_TREE
;
23114 = cp_parser_late_return_type_opt (parser
, declarator
,
23117 cp_finalize_omp_declare_simd (parser
, &odsd
);
23119 /* Parse the virt-specifier-seq. */
23120 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23122 location_t parens_loc
= make_location (parens_start
,
23125 /* Create the function-declarator. */
23126 declarator
= make_call_declarator (declarator
,
23132 exception_specification
,
23136 declarator
->std_attributes
= attrs
;
23137 declarator
->attributes
= gnu_attrs
;
23138 /* Any subsequent parameter lists are to do with
23139 return type, so are not those of the declared
23141 parser
->default_arg_ok_p
= false;
23143 current_class_ptr
= save_ccp
;
23144 current_class_ref
= save_ccr
;
23146 /* Restore the state of local_variables_forbidden_p. */
23147 parser
->local_variables_forbidden_p
23148 = local_variables_forbidden_p
;
23151 /* Remove the function parms from scope. */
23152 pop_bindings_and_leave_scope ();
23155 /* Repeat the main loop. */
23159 /* If this is the first, we can try a parenthesized
23163 bool saved_in_type_id_in_expr_p
;
23165 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
23166 parser
->in_declarator_p
= saved_in_declarator_p
;
23168 open_paren
= token
;
23169 /* Consume the `('. */
23170 matching_parens parens
;
23171 parens
.consume_open (parser
);
23172 /* Parse the nested declarator. */
23173 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
23174 parser
->in_type_id_in_expr_p
= true;
23176 = cp_parser_declarator (parser
, dcl_kind
, flags
,
23177 ctor_dtor_or_conv_p
,
23178 /*parenthesized_p=*/NULL
,
23179 member_p
, friend_p
,
23180 /*static_p=*/false);
23181 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
23183 /* Expect a `)'. */
23184 close_paren
= cp_lexer_peek_token (parser
->lexer
);
23185 if (!parens
.require_close (parser
))
23186 declarator
= cp_error_declarator
;
23187 if (declarator
== cp_error_declarator
)
23190 goto handle_declarator
;
23192 /* Otherwise, we must be done. */
23196 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
23197 && token
->type
== CPP_OPEN_SQUARE
23198 && !cp_next_tokens_can_be_attribute_p (parser
))
23200 /* Parse an array-declarator. */
23201 tree bounds
, attrs
;
23203 if (ctor_dtor_or_conv_p
)
23204 *ctor_dtor_or_conv_p
= 0;
23208 parser
->default_arg_ok_p
= false;
23209 parser
->in_declarator_p
= true;
23210 /* Consume the `['. */
23211 cp_lexer_consume_token (parser
->lexer
);
23212 /* Peek at the next token. */
23213 token
= cp_lexer_peek_token (parser
->lexer
);
23214 /* If the next token is `]', then there is no
23215 constant-expression. */
23216 if (token
->type
!= CPP_CLOSE_SQUARE
)
23218 bool non_constant_p
;
23220 = cp_parser_constant_expression (parser
,
23221 /*allow_non_constant=*/true,
23223 if (!non_constant_p
)
23225 else if (error_operand_p (bounds
))
23226 /* Already gave an error. */;
23227 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
23228 /* Let compute_array_index_type diagnose this. */;
23229 else if (!parser
->in_function_body
23230 || parsing_function_declarator ())
23232 /* Normally, the array bound must be an integral constant
23233 expression. However, as an extension, we allow VLAs
23234 in function scopes as long as they aren't part of a
23235 parameter declaration. */
23236 cp_parser_error (parser
,
23237 "array bound is not an integer constant");
23238 bounds
= error_mark_node
;
23240 else if (processing_template_decl
23241 && !type_dependent_expression_p (bounds
))
23243 /* Remember this wasn't a constant-expression. */
23244 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
23245 TREE_SIDE_EFFECTS (bounds
) = 1;
23249 bounds
= NULL_TREE
;
23250 /* Look for the closing `]'. */
23251 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
23253 declarator
= cp_error_declarator
;
23257 attrs
= cp_parser_std_attribute_spec_seq (parser
);
23258 declarator
= make_array_declarator (declarator
, bounds
);
23259 declarator
->std_attributes
= attrs
;
23261 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
23264 tree qualifying_scope
;
23265 tree unqualified_name
;
23267 special_function_kind sfk
;
23269 bool pack_expansion_p
= false;
23270 cp_token
*declarator_id_start_token
;
23272 /* Parse a declarator-id */
23273 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
23276 cp_parser_parse_tentatively (parser
);
23278 /* If we see an ellipsis, we should be looking at a
23280 if (token
->type
== CPP_ELLIPSIS
)
23282 /* Consume the `...' */
23283 cp_lexer_consume_token (parser
->lexer
);
23285 pack_expansion_p
= true;
23289 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
23291 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
23292 qualifying_scope
= parser
->scope
;
23297 if (!unqualified_name
&& pack_expansion_p
)
23299 /* Check whether an error occurred. */
23300 okay
= !cp_parser_error_occurred (parser
);
23302 /* We already consumed the ellipsis to mark a
23303 parameter pack, but we have no way to report it,
23304 so abort the tentative parse. We will be exiting
23305 immediately anyway. */
23306 cp_parser_abort_tentative_parse (parser
);
23309 okay
= cp_parser_parse_definitely (parser
);
23312 unqualified_name
= error_mark_node
;
23313 else if (unqualified_name
23314 && (qualifying_scope
23315 || (!identifier_p (unqualified_name
))))
23317 cp_parser_error (parser
, "expected unqualified-id");
23318 unqualified_name
= error_mark_node
;
23322 if (!unqualified_name
)
23324 if (unqualified_name
== error_mark_node
)
23326 declarator
= cp_error_declarator
;
23327 pack_expansion_p
= false;
23328 declarator
->parameter_pack_p
= false;
23332 attrs
= cp_parser_std_attribute_spec_seq (parser
);
23334 if (qualifying_scope
&& at_namespace_scope_p ()
23335 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
23337 /* In the declaration of a member of a template class
23338 outside of the class itself, the SCOPE will sometimes
23339 be a TYPENAME_TYPE. For example, given:
23341 template <typename T>
23342 int S<T>::R::i = 3;
23344 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23345 this context, we must resolve S<T>::R to an ordinary
23346 type, rather than a typename type.
23348 The reason we normally avoid resolving TYPENAME_TYPEs
23349 is that a specialization of `S' might render
23350 `S<T>::R' not a type. However, if `S' is
23351 specialized, then this `i' will not be used, so there
23352 is no harm in resolving the types here. */
23355 /* Resolve the TYPENAME_TYPE. */
23356 type
= resolve_typename_type (qualifying_scope
,
23357 /*only_current_p=*/false);
23358 /* If that failed, the declarator is invalid. */
23359 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23361 if (typedef_variant_p (type
))
23362 error_at (declarator_id_start_token
->location
,
23363 "cannot define member of dependent typedef "
23366 error_at (declarator_id_start_token
->location
,
23367 "%<%T::%E%> is not a type",
23368 TYPE_CONTEXT (qualifying_scope
),
23369 TYPE_IDENTIFIER (qualifying_scope
));
23371 qualifying_scope
= type
;
23376 if (unqualified_name
)
23380 if (qualifying_scope
23381 && CLASS_TYPE_P (qualifying_scope
))
23382 class_type
= qualifying_scope
;
23384 class_type
= current_class_type
;
23386 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
23388 tree name_type
= TREE_TYPE (unqualified_name
);
23390 if (!class_type
|| !same_type_p (name_type
, class_type
))
23392 /* We do not attempt to print the declarator
23393 here because we do not have enough
23394 information about its original syntactic
23396 cp_parser_error (parser
, "invalid declarator");
23397 declarator
= cp_error_declarator
;
23400 else if (qualifying_scope
23401 && CLASSTYPE_USE_TEMPLATE (name_type
))
23403 error_at (declarator_id_start_token
->location
,
23404 "invalid use of constructor as a template");
23405 inform (declarator_id_start_token
->location
,
23406 "use %<%T::%D%> instead of %<%T::%D%> to "
23407 "name the constructor in a qualified name",
23409 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
23410 class_type
, name_type
);
23411 declarator
= cp_error_declarator
;
23414 unqualified_name
= constructor_name (class_type
);
23419 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
23420 sfk
= sfk_destructor
;
23421 else if (identifier_p (unqualified_name
)
23422 && IDENTIFIER_CONV_OP_P (unqualified_name
))
23423 sfk
= sfk_conversion
;
23424 else if (/* There's no way to declare a constructor
23425 for an unnamed type, even if the type
23426 got a name for linkage purposes. */
23427 !TYPE_WAS_UNNAMED (class_type
)
23428 /* Handle correctly (c++/19200):
23442 friend void N::S();
23444 && (!friend_p
|| class_type
== qualifying_scope
)
23445 && constructor_name_p (unqualified_name
,
23447 sfk
= sfk_constructor
;
23448 else if (is_overloaded_fn (unqualified_name
)
23449 && DECL_CONSTRUCTOR_P (get_first_fn
23450 (unqualified_name
)))
23451 sfk
= sfk_constructor
;
23453 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
23454 *ctor_dtor_or_conv_p
= -1;
23457 declarator
= make_id_declarator (qualifying_scope
,
23459 sfk
, token
->location
);
23460 declarator
->std_attributes
= attrs
;
23461 declarator
->parameter_pack_p
= pack_expansion_p
;
23463 if (pack_expansion_p
)
23464 maybe_warn_variadic_templates ();
23466 /* We're looking for this case in [temp.res]:
23467 A qualified-id is assumed to name a type if [...]
23468 - it is a decl-specifier of the decl-specifier-seq of a
23469 parameter-declaration in a declarator of a function or
23470 function template declaration, ... */
23471 if (cxx_dialect
>= cxx20
23472 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
23473 && declarator
->kind
== cdk_id
23474 && !at_class_scope_p ()
23475 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23477 /* ...whose declarator-id is qualified. If it isn't, never
23478 assume the parameters to refer to types. */
23479 if (qualifying_scope
== NULL_TREE
)
23480 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
23483 /* Now we have something like
23484 template <typename T> int C::x(S::p);
23485 which can be a function template declaration or a
23486 variable template definition. If name lookup for
23487 the declarator-id C::x finds one or more function
23488 templates, assume S::p to name a type. Otherwise,
23491 = cp_parser_lookup_name (parser
, unqualified_name
,
23493 /*is_template=*/false,
23494 /*is_namespace=*/false,
23495 /*check_dependency=*/false,
23496 /*ambiguous_decls=*/NULL
,
23499 if (!is_overloaded_fn (decl
)
23501 template<typename T>
23502 A<T>::A(T::type) { } */
23503 && !(MAYBE_CLASS_TYPE_P (qualifying_scope
)
23504 && constructor_name_p (unqualified_name
,
23505 qualifying_scope
)))
23506 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
23511 handle_declarator
:;
23512 scope
= get_scope_of_declarator (declarator
);
23515 /* Any names that appear after the declarator-id for a
23516 member are looked up in the containing scope. */
23517 if (at_function_scope_p ())
23519 /* But declarations with qualified-ids can't appear in a
23521 cp_parser_error (parser
, "qualified-id in declaration");
23522 declarator
= cp_error_declarator
;
23525 pushed_scope
= push_scope (scope
);
23527 parser
->in_declarator_p
= true;
23528 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
23529 || (declarator
&& declarator
->kind
== cdk_id
))
23530 /* Default args are only allowed on function
23532 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
23534 parser
->default_arg_ok_p
= false;
23543 /* For an abstract declarator, we might wind up with nothing at this
23544 point. That's an error; the declarator is not optional. */
23546 cp_parser_error (parser
, "expected declarator");
23547 else if (open_paren
)
23549 /* Record overly parenthesized declarator so we can give a
23550 diagnostic about confusing decl/expr disambiguation. */
23551 if (declarator
->kind
== cdk_array
)
23553 /* If the open and close parens are on different lines, this
23554 is probably a formatting thing, so ignore. */
23555 expanded_location open
= expand_location (open_paren
->location
);
23556 expanded_location close
= expand_location (close_paren
->location
);
23557 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
23561 declarator
->parenthesized
= make_location (open_paren
->location
,
23562 open_paren
->location
,
23563 close_paren
->location
);
23566 /* If we entered a scope, we must exit it now. */
23568 pop_scope (pushed_scope
);
23570 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
23571 parser
->in_declarator_p
= saved_in_declarator_p
;
23576 /* Parse a ptr-operator.
23579 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23580 * cv-qualifier-seq [opt]
23582 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23583 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23588 & cv-qualifier-seq [opt]
23590 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23591 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23592 an rvalue reference. In the case of a pointer-to-member, *TYPE is
23593 filled in with the TYPE containing the member. *CV_QUALS is
23594 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23595 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
23596 Note that the tree codes returned by this function have nothing
23597 to do with the types of trees that will be eventually be created
23598 to represent the pointer or reference type being parsed. They are
23599 just constants with suggestive names. */
23600 static enum tree_code
23601 cp_parser_ptr_operator (cp_parser
* parser
,
23603 cp_cv_quals
*cv_quals
,
23606 enum tree_code code
= ERROR_MARK
;
23608 tree attrs
= NULL_TREE
;
23610 /* Assume that it's not a pointer-to-member. */
23612 /* And that there are no cv-qualifiers. */
23613 *cv_quals
= TYPE_UNQUALIFIED
;
23615 /* Peek at the next token. */
23616 token
= cp_lexer_peek_token (parser
->lexer
);
23618 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
23619 if (token
->type
== CPP_MULT
)
23620 code
= INDIRECT_REF
;
23621 else if (token
->type
== CPP_AND
)
23623 else if ((cxx_dialect
!= cxx98
) &&
23624 token
->type
== CPP_AND_AND
) /* C++0x only */
23625 code
= NON_LVALUE_EXPR
;
23627 if (code
!= ERROR_MARK
)
23629 /* Consume the `*', `&' or `&&'. */
23630 cp_lexer_consume_token (parser
->lexer
);
23632 /* A `*' can be followed by a cv-qualifier-seq, and so can a
23633 `&', if we are allowing GNU extensions. (The only qualifier
23634 that can legally appear after `&' is `restrict', but that is
23635 enforced during semantic analysis. */
23636 if (code
== INDIRECT_REF
23637 || cp_parser_allow_gnu_extensions_p (parser
))
23638 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
23640 attrs
= cp_parser_std_attribute_spec_seq (parser
);
23641 if (attributes
!= NULL
)
23642 *attributes
= attrs
;
23646 /* Try the pointer-to-member case. */
23647 cp_parser_parse_tentatively (parser
);
23648 /* Look for the optional `::' operator. */
23649 cp_parser_global_scope_opt (parser
,
23650 /*current_scope_valid_p=*/false);
23651 /* Look for the nested-name specifier. */
23652 token
= cp_lexer_peek_token (parser
->lexer
);
23653 cp_parser_nested_name_specifier (parser
,
23654 /*typename_keyword_p=*/false,
23655 /*check_dependency_p=*/true,
23657 /*is_declaration=*/false);
23658 /* If we found it, and the next token is a `*', then we are
23659 indeed looking at a pointer-to-member operator. */
23660 if (!cp_parser_error_occurred (parser
)
23661 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
23663 /* Indicate that the `*' operator was used. */
23664 code
= INDIRECT_REF
;
23666 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
23667 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
23668 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
23669 error_at (token
->location
, "cannot form pointer to member of "
23670 "non-class %q#T", parser
->scope
);
23673 /* The type of which the member is a member is given by the
23675 *type
= parser
->scope
;
23676 /* The next name will not be qualified. */
23677 parser
->scope
= NULL_TREE
;
23678 parser
->qualifying_scope
= NULL_TREE
;
23679 parser
->object_scope
= NULL_TREE
;
23680 /* Look for optional c++11 attributes. */
23681 attrs
= cp_parser_std_attribute_spec_seq (parser
);
23682 if (attributes
!= NULL
)
23683 *attributes
= attrs
;
23684 /* Look for the optional cv-qualifier-seq. */
23685 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
23688 /* If that didn't work we don't have a ptr-operator. */
23689 if (!cp_parser_parse_definitely (parser
))
23690 cp_parser_error (parser
, "expected ptr-operator");
23696 /* Parse an (optional) cv-qualifier-seq.
23699 cv-qualifier cv-qualifier-seq [opt]
23710 Returns a bitmask representing the cv-qualifiers. */
23713 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
23715 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
23720 cp_cv_quals cv_qualifier
;
23722 /* Peek at the next token. */
23723 token
= cp_lexer_peek_token (parser
->lexer
);
23724 /* See if it's a cv-qualifier. */
23725 switch (token
->keyword
)
23728 cv_qualifier
= TYPE_QUAL_CONST
;
23732 cv_qualifier
= TYPE_QUAL_VOLATILE
;
23736 cv_qualifier
= TYPE_QUAL_RESTRICT
;
23740 cv_qualifier
= TYPE_UNQUALIFIED
;
23747 if (cv_quals
& cv_qualifier
)
23749 gcc_rich_location
richloc (token
->location
);
23750 richloc
.add_fixit_remove ();
23751 error_at (&richloc
, "duplicate cv-qualifier");
23752 cp_lexer_purge_token (parser
->lexer
);
23756 cp_lexer_consume_token (parser
->lexer
);
23757 cv_quals
|= cv_qualifier
;
23764 /* Parse an (optional) ref-qualifier
23770 Returns cp_ref_qualifier representing ref-qualifier. */
23772 static cp_ref_qualifier
23773 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
23775 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
23777 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
23778 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
23783 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
23784 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23786 switch (token
->type
)
23789 curr_ref_qual
= REF_QUAL_LVALUE
;
23793 curr_ref_qual
= REF_QUAL_RVALUE
;
23797 curr_ref_qual
= REF_QUAL_NONE
;
23801 if (!curr_ref_qual
)
23805 error_at (token
->location
, "multiple ref-qualifiers");
23806 cp_lexer_purge_token (parser
->lexer
);
23810 ref_qual
= curr_ref_qual
;
23811 cp_lexer_consume_token (parser
->lexer
);
23818 /* Parse an optional tx-qualifier.
23822 transaction_safe_dynamic */
23825 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
23827 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23828 if (token
->type
== CPP_NAME
)
23830 tree name
= token
->u
.value
;
23831 const char *p
= IDENTIFIER_POINTER (name
);
23832 const int len
= strlen ("transaction_safe");
23833 if (startswith (p
, "transaction_safe"))
23837 || !strcmp (p
, "_dynamic"))
23839 cp_lexer_consume_token (parser
->lexer
);
23842 error ("%qE requires %<-fgnu-tm%>", name
);
23853 /* Parse an (optional) virt-specifier-seq.
23855 virt-specifier-seq:
23856 virt-specifier virt-specifier-seq [opt]
23862 Returns a bitmask representing the virt-specifiers. */
23864 static cp_virt_specifiers
23865 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
23867 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23872 cp_virt_specifiers virt_specifier
;
23874 /* Peek at the next token. */
23875 token
= cp_lexer_peek_token (parser
->lexer
);
23876 /* See if it's a virt-specifier-qualifier. */
23877 if (token
->type
!= CPP_NAME
)
23879 if (id_equal (token
->u
.value
, "override"))
23881 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
23882 virt_specifier
= VIRT_SPEC_OVERRIDE
;
23884 else if (id_equal (token
->u
.value
, "final"))
23886 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
23887 virt_specifier
= VIRT_SPEC_FINAL
;
23889 else if (id_equal (token
->u
.value
, "__final"))
23891 virt_specifier
= VIRT_SPEC_FINAL
;
23896 if (virt_specifiers
& virt_specifier
)
23898 gcc_rich_location
richloc (token
->location
);
23899 richloc
.add_fixit_remove ();
23900 error_at (&richloc
, "duplicate virt-specifier");
23901 cp_lexer_purge_token (parser
->lexer
);
23905 cp_lexer_consume_token (parser
->lexer
);
23906 virt_specifiers
|= virt_specifier
;
23909 return virt_specifiers
;
23912 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
23913 is in scope even though it isn't real. */
23916 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
23920 if (current_class_ptr
)
23922 /* We don't clear this between NSDMIs. Is it already what we want? */
23923 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
23924 if (DECL_P (current_class_ptr
)
23925 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
23926 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
23927 && cp_type_quals (type
) == quals
)
23931 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
23932 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
23933 current_class_ptr
= NULL_TREE
;
23935 = cp_build_fold_indirect_ref (this_parm
);
23936 current_class_ptr
= this_parm
;
23939 /* Return true iff our current scope is a non-static data member
23943 parsing_nsdmi (void)
23945 /* We recognize NSDMI context by the context-less 'this' pointer set up
23946 by the function above. */
23947 if (current_class_ptr
23948 && TREE_CODE (current_class_ptr
) == PARM_DECL
23949 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
23954 /* True if we're parsing a function declarator. */
23957 parsing_function_declarator ()
23959 /* this_entity is NULL for a function parameter scope while parsing the
23960 declarator; it is set when parsing the body of the function. */
23961 return (current_binding_level
->kind
== sk_function_parms
23962 && !current_binding_level
->this_entity
);
23965 /* Parse a late-specified return type, if any. This is not a separate
23966 non-terminal, but part of a function declarator, which looks like
23968 -> trailing-type-specifier-seq abstract-declarator(opt)
23970 Returns the type indicated by the type-id.
23972 In addition to this, parse any queued up #pragma omp declare simd
23973 clauses, and #pragma acc routine clauses.
23975 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
23979 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
23980 tree
& requires_clause
)
23983 tree type
= NULL_TREE
;
23984 bool declare_simd_p
= (parser
->omp_declare_simd
23986 && declarator
->kind
== cdk_id
);
23988 bool oacc_routine_p
= (parser
->oacc_routine
23990 && declarator
->kind
== cdk_id
);
23992 /* Peek at the next token. */
23993 token
= cp_lexer_peek_token (parser
->lexer
);
23994 /* A late-specified return type is indicated by an initial '->'. */
23995 if (token
->type
!= CPP_DEREF
23996 && token
->keyword
!= RID_REQUIRES
23997 && !(token
->type
== CPP_NAME
23998 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
23999 && !(declare_simd_p
|| oacc_routine_p
))
24002 if (token
->type
== CPP_DEREF
)
24004 /* Consume the ->. */
24005 cp_lexer_consume_token (parser
->lexer
);
24007 type
= cp_parser_trailing_type_id (parser
);
24010 /* Function declarations may be followed by a trailing
24011 requires-clause. */
24012 requires_clause
= cp_parser_requires_clause_opt (parser
, false);
24014 if (declare_simd_p
)
24015 declarator
->attributes
24016 = cp_parser_late_parsing_omp_declare_simd (parser
,
24017 declarator
->attributes
);
24018 if (oacc_routine_p
)
24019 declarator
->attributes
24020 = cp_parser_late_parsing_oacc_routine (parser
,
24021 declarator
->attributes
);
24026 /* Parse a declarator-id.
24030 :: [opt] nested-name-specifier [opt] type-name
24032 In the `id-expression' case, the value returned is as for
24033 cp_parser_id_expression if the id-expression was an unqualified-id.
24034 If the id-expression was a qualified-id, then a SCOPE_REF is
24035 returned. The first operand is the scope (either a NAMESPACE_DECL
24036 or TREE_TYPE), but the second is still just a representation of an
24040 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
24043 /* The expression must be an id-expression. Assume that qualified
24044 names are the names of types so that:
24047 int S<T>::R::i = 3;
24049 will work; we must treat `S<T>::R' as the name of a type.
24050 Similarly, assume that qualified names are templates, where
24054 int S<T>::R<T>::i = 3;
24057 id
= cp_parser_id_expression (parser
,
24058 /*template_keyword_p=*/false,
24059 /*check_dependency_p=*/false,
24060 /*template_p=*/NULL
,
24061 /*declarator_p=*/true,
24063 if (id
&& BASELINK_P (id
))
24064 id
= BASELINK_FUNCTIONS (id
);
24068 /* Parse a type-id.
24071 type-specifier-seq abstract-declarator [opt]
24073 The parser flags FLAGS is used to control type-specifier parsing.
24075 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24077 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24078 i.e. we've just seen "->".
24080 Returns the TYPE specified. */
24083 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
24084 bool is_template_arg
, bool is_trailing_return
,
24085 location_t
*type_location
)
24087 cp_decl_specifier_seq type_specifier_seq
;
24088 cp_declarator
*abstract_declarator
;
24090 /* Parse the type-specifier-seq. */
24091 cp_parser_type_specifier_seq (parser
, flags
,
24092 /*is_declaration=*/false,
24093 is_trailing_return
,
24094 &type_specifier_seq
);
24096 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
24098 if (is_template_arg
&& type_specifier_seq
.type
24099 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
24100 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
24101 /* A bare template name as a template argument is a template template
24102 argument, not a placeholder, so fail parsing it as a type argument. */
24104 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
24105 cp_parser_simulate_error (parser
);
24106 return error_mark_node
;
24108 if (type_specifier_seq
.type
== error_mark_node
)
24109 return error_mark_node
;
24111 /* There might or might not be an abstract declarator. */
24112 cp_parser_parse_tentatively (parser
);
24113 /* Look for the declarator. */
24114 abstract_declarator
24115 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
24116 CP_PARSER_FLAGS_NONE
, NULL
,
24117 /*parenthesized_p=*/NULL
,
24118 /*member_p=*/false,
24119 /*friend_p=*/false,
24120 /*static_p=*/false);
24121 /* Check to see if there really was a declarator. */
24122 if (!cp_parser_parse_definitely (parser
))
24123 abstract_declarator
= NULL
;
24125 bool auto_typeid_ok
= false;
24126 /* The concepts TS allows 'auto' as a type-id. */
24127 if (flag_concepts_ts
)
24128 auto_typeid_ok
= !parser
->in_type_id_in_expr_p
;
24129 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24130 outside the template-argument-list context here only for the sake of
24131 diagnostic: grokdeclarator then can emit a better error message for
24132 e.g. using T = auto. */
24133 else if (flag_concepts
)
24134 auto_typeid_ok
= (!parser
->in_type_id_in_expr_p
24135 && !parser
->in_template_argument_list_p
);
24137 if (type_specifier_seq
.type
24139 /* None of the valid uses of 'auto' in C++14 involve the type-id
24140 nonterminal, but it is valid in a trailing-return-type. */
24141 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
24142 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
24144 /* A type-id with type 'auto' is only ok if the abstract declarator
24145 is a function declarator with a late-specified return type.
24147 A type-id with 'auto' is also valid in a trailing-return-type
24148 in a compound-requirement. */
24149 if (abstract_declarator
24150 && abstract_declarator
->kind
== cdk_function
24151 && abstract_declarator
->u
.function
.late_return_type
)
24153 else if (parser
->in_result_type_constraint_p
)
24157 if (!cp_parser_simulate_error (parser
))
24159 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
24160 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
24162 error_at (loc
, "missing template arguments after %qT",
24164 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
24167 else if (parser
->in_template_argument_list_p
)
24168 error_at (loc
, "%qT not permitted in template argument",
24171 error_at (loc
, "invalid use of %qT", auto_node
);
24173 return error_mark_node
;
24177 return groktypename (&type_specifier_seq
, abstract_declarator
,
24181 /* Wrapper for cp_parser_type_id_1. */
24184 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
24185 location_t
*type_location
)
24187 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
24190 /* Wrapper for cp_parser_type_id_1. */
24193 cp_parser_template_type_arg (cp_parser
*parser
)
24196 const char *saved_message
= parser
->type_definition_forbidden_message
;
24197 parser
->type_definition_forbidden_message
24198 = G_("types may not be defined in template arguments");
24199 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
24200 parser
->type_definition_forbidden_message
= saved_message
;
24201 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
24203 error ("invalid use of %<auto%> in template argument");
24204 r
= error_mark_node
;
24209 /* Wrapper for cp_parser_type_id_1. */
24212 cp_parser_trailing_type_id (cp_parser
*parser
)
24214 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
24215 false, true, NULL
);
24218 /* Parse a type-specifier-seq.
24220 type-specifier-seq:
24221 type-specifier type-specifier-seq [opt]
24225 type-specifier-seq:
24226 attributes type-specifier-seq [opt]
24228 The parser flags FLAGS is used to control type-specifier parsing.
24230 If IS_DECLARATION is true, we are at the start of a "condition" or
24231 exception-declaration, so we might be followed by a declarator-id.
24233 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24234 i.e. we've just seen "->".
24236 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24239 cp_parser_type_specifier_seq (cp_parser
* parser
,
24240 cp_parser_flags flags
,
24241 bool is_declaration
,
24242 bool is_trailing_return
,
24243 cp_decl_specifier_seq
*type_specifier_seq
)
24245 bool seen_type_specifier
= false;
24246 cp_token
*start_token
= NULL
;
24248 /* Clear the TYPE_SPECIFIER_SEQ. */
24249 clear_decl_specs (type_specifier_seq
);
24251 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
24252 /* In the context of a trailing return type, enum E { } is an
24253 elaborated-type-specifier followed by a function-body, not an
24255 if (is_trailing_return
)
24256 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
24258 /* Parse the type-specifiers and attributes. */
24261 tree type_specifier
;
24262 bool is_cv_qualifier
;
24264 /* Check for attributes first. */
24265 if (cp_next_tokens_can_be_attribute_p (parser
))
24267 /* GNU attributes at the end of a declaration apply to the
24268 declaration as a whole, not to the trailing return type. So look
24269 ahead to see if these attributes are at the end. */
24270 if (seen_type_specifier
&& is_trailing_return
24271 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
24273 size_t n
= cp_parser_skip_attributes_opt (parser
, 1);
24274 cp_token
*tok
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24275 if (tok
->type
== CPP_SEMICOLON
|| tok
->type
== CPP_COMMA
24276 || tok
->type
== CPP_EQ
|| tok
->type
== CPP_OPEN_BRACE
)
24279 type_specifier_seq
->attributes
24280 = attr_chainon (type_specifier_seq
->attributes
,
24281 cp_parser_attributes_opt (parser
));
24285 /* record the token of the beginning of the type specifier seq,
24286 for error reporting purposes*/
24288 start_token
= cp_lexer_peek_token (parser
->lexer
);
24290 /* Look for the type-specifier. */
24291 type_specifier
= cp_parser_type_specifier (parser
,
24293 type_specifier_seq
,
24294 /*is_declaration=*/false,
24297 if (!type_specifier
)
24299 /* If the first type-specifier could not be found, this is not a
24300 type-specifier-seq at all. */
24301 if (!seen_type_specifier
)
24303 /* Set in_declarator_p to avoid skipping to the semicolon. */
24304 int in_decl
= parser
->in_declarator_p
;
24305 parser
->in_declarator_p
= true;
24307 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
24308 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24309 cp_parser_error (parser
, "expected type-specifier");
24311 parser
->in_declarator_p
= in_decl
;
24313 type_specifier_seq
->type
= error_mark_node
;
24316 /* If subsequent type-specifiers could not be found, the
24317 type-specifier-seq is complete. */
24321 seen_type_specifier
= true;
24322 /* The standard says that a condition can be:
24324 type-specifier-seq declarator = assignment-expression
24331 we should treat the "S" as a declarator, not as a
24332 type-specifier. The standard doesn't say that explicitly for
24333 type-specifier-seq, but it does say that for
24334 decl-specifier-seq in an ordinary declaration. Perhaps it
24335 would be clearer just to allow a decl-specifier-seq here, and
24336 then add a semantic restriction that if any decl-specifiers
24337 that are not type-specifiers appear, the program is invalid. */
24338 if (is_declaration
&& !is_cv_qualifier
)
24339 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
24343 /* Return whether the function currently being declared has an associated
24344 template parameter list. */
24347 function_being_declared_is_template_p (cp_parser
* parser
)
24349 if (!current_template_parms
|| processing_template_parmlist
)
24352 if (parser
->implicit_template_scope
)
24355 if (at_class_scope_p ()
24356 && TYPE_BEING_DEFINED (current_class_type
))
24357 return parser
->num_template_parameter_lists
!= 0;
24359 return ((int) parser
->num_template_parameter_lists
> template_class_depth
24360 (current_class_type
));
24363 /* Parse a parameter-declaration-clause.
24365 parameter-declaration-clause:
24366 parameter-declaration-list [opt] ... [opt]
24367 parameter-declaration-list , ...
24369 The parser flags FLAGS is used to control type-specifier parsing.
24371 Returns a representation for the parameter declarations. A return
24372 value of NULL indicates a parameter-declaration-clause consisting
24373 only of an ellipsis. */
24376 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
24377 cp_parser_flags flags
)
24383 auto cleanup
= make_temp_override
24384 (parser
->auto_is_implicit_function_template_parm_p
);
24386 if (!processing_specialization
24387 && !processing_template_parmlist
24388 && !processing_explicit_instantiation
24389 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24390 actual function or a random abstract declarator. */
24391 && parser
->default_arg_ok_p
)
24392 if (!current_function_decl
24393 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
24394 parser
->auto_is_implicit_function_template_parm_p
= true;
24396 /* Peek at the next token. */
24397 token
= cp_lexer_peek_token (parser
->lexer
);
24398 /* Check for trivial parameter-declaration-clauses. */
24399 if (token
->type
== CPP_ELLIPSIS
)
24401 /* Consume the `...' token. */
24402 cp_lexer_consume_token (parser
->lexer
);
24405 else if (token
->type
== CPP_CLOSE_PAREN
)
24406 /* There are no parameters. */
24407 return void_list_node
;
24408 /* Check for `(void)', too, which is a special case. */
24409 else if (token
->keyword
== RID_VOID
24410 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
24411 == CPP_CLOSE_PAREN
))
24413 /* Consume the `void' token. */
24414 cp_lexer_consume_token (parser
->lexer
);
24415 /* There are no parameters. */
24416 return explicit_void_list_node
;
24419 /* Parse the parameter-declaration-list. */
24420 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
24421 /* If a parse error occurred while parsing the
24422 parameter-declaration-list, then the entire
24423 parameter-declaration-clause is erroneous. */
24424 if (parameters
== error_mark_node
)
24427 /* Peek at the next token. */
24428 token
= cp_lexer_peek_token (parser
->lexer
);
24429 /* If it's a `,', the clause should terminate with an ellipsis. */
24430 if (token
->type
== CPP_COMMA
)
24432 /* Consume the `,'. */
24433 cp_lexer_consume_token (parser
->lexer
);
24434 /* Expect an ellipsis. */
24436 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
24438 /* It might also be `...' if the optional trailing `,' was
24440 else if (token
->type
== CPP_ELLIPSIS
)
24442 /* Consume the `...' token. */
24443 cp_lexer_consume_token (parser
->lexer
);
24444 /* And remember that we saw it. */
24448 ellipsis_p
= false;
24450 /* Finish the parameter list. */
24452 parameters
= chainon (parameters
, void_list_node
);
24457 /* Parse a parameter-declaration-list.
24459 parameter-declaration-list:
24460 parameter-declaration
24461 parameter-declaration-list , parameter-declaration
24463 The parser flags FLAGS is used to control type-specifier parsing.
24465 Returns a representation of the parameter-declaration-list, as for
24466 cp_parser_parameter_declaration_clause. However, the
24467 `void_list_node' is never appended to the list. */
24470 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
24472 tree parameters
= NULL_TREE
;
24473 tree
*tail
= ¶meters
;
24474 bool saved_in_unbraced_linkage_specification_p
;
24477 /* The special considerations that apply to a function within an
24478 unbraced linkage specifications do not apply to the parameters
24479 to the function. */
24480 saved_in_unbraced_linkage_specification_p
24481 = parser
->in_unbraced_linkage_specification_p
;
24482 parser
->in_unbraced_linkage_specification_p
= false;
24484 /* Look for more parameters. */
24487 cp_parameter_declarator
*parameter
;
24488 tree decl
= error_mark_node
;
24489 bool parenthesized_p
= false;
24491 /* Parse the parameter. */
24493 = cp_parser_parameter_declaration (parser
, flags
,
24494 /*template_parm_p=*/false,
24497 /* We don't know yet if the enclosing context is unavailable or deprecated,
24498 so wait and deal with it in grokparms if appropriate. */
24499 deprecated_state
= UNAVAILABLE_DEPRECATED_SUPPRESS
;
24501 if (parameter
&& !cp_parser_error_occurred (parser
))
24503 decl
= grokdeclarator (parameter
->declarator
,
24504 ¶meter
->decl_specifiers
,
24506 parameter
->default_argument
!= NULL_TREE
,
24507 ¶meter
->decl_specifiers
.attributes
);
24508 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
24509 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
24512 deprecated_state
= DEPRECATED_NORMAL
;
24514 /* If a parse error occurred parsing the parameter declaration,
24515 then the entire parameter-declaration-list is erroneous. */
24516 if (decl
== error_mark_node
)
24518 parameters
= error_mark_node
;
24522 if (parameter
->decl_specifiers
.attributes
)
24523 cplus_decl_attributes (&decl
,
24524 parameter
->decl_specifiers
.attributes
,
24526 if (DECL_NAME (decl
))
24527 decl
= pushdecl (decl
);
24529 if (decl
!= error_mark_node
)
24531 retrofit_lang_decl (decl
);
24532 DECL_PARM_INDEX (decl
) = ++index
;
24533 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
24536 /* Add the new parameter to the list. */
24537 *tail
= build_tree_list (parameter
->default_argument
, decl
);
24538 tail
= &TREE_CHAIN (*tail
);
24540 /* If the parameters were parenthesized, it's the case of
24541 T foo(X(x)) which looks like a variable definition but
24542 is a function declaration. */
24543 if (index
== 1 || PARENTHESIZED_LIST_P (parameters
))
24544 PARENTHESIZED_LIST_P (parameters
) = parenthesized_p
;
24546 /* Peek at the next token. */
24547 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
24548 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
24549 /* These are for Objective-C++ */
24550 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24551 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24552 /* The parameter-declaration-list is complete. */
24554 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24558 /* Peek at the next token. */
24559 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24560 /* If it's an ellipsis, then the list is complete. */
24561 if (token
->type
== CPP_ELLIPSIS
)
24563 /* Otherwise, there must be more parameters. Consume the
24565 cp_lexer_consume_token (parser
->lexer
);
24566 /* When parsing something like:
24568 int i(float f, double d)
24570 we can tell after seeing the declaration for "f" that we
24571 are not looking at an initialization of a variable "i",
24572 but rather at the declaration of a function "i".
24574 Due to the fact that the parsing of template arguments
24575 (as specified to a template-id) requires backtracking we
24576 cannot use this technique when inside a template argument
24578 if (!parser
->in_template_argument_list_p
24579 && !parser
->in_type_id_in_expr_p
24580 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
24581 /* However, a parameter-declaration of the form
24582 "float(f)" (which is a valid declaration of a
24583 parameter "f") can also be interpreted as an
24584 expression (the conversion of "f" to "float"). */
24585 && !parenthesized_p
)
24586 cp_parser_commit_to_tentative_parse (parser
);
24590 cp_parser_error (parser
, "expected %<,%> or %<...%>");
24591 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
24592 cp_parser_skip_to_closing_parenthesis (parser
,
24593 /*recovering=*/true,
24594 /*or_comma=*/false,
24595 /*consume_paren=*/false);
24600 parser
->in_unbraced_linkage_specification_p
24601 = saved_in_unbraced_linkage_specification_p
;
24603 /* Reset implicit_template_scope if we are about to leave the function
24604 parameter list that introduced it. Note that for out-of-line member
24605 definitions, there will be one or more class scopes before we get to
24606 the template parameter scope. */
24608 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
24609 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
24611 while (maybe_its
->kind
== sk_class
)
24612 maybe_its
= maybe_its
->level_chain
;
24613 if (maybe_its
== its
)
24615 parser
->implicit_template_parms
= 0;
24616 parser
->implicit_template_scope
= 0;
24623 /* Parse a parameter declaration.
24625 parameter-declaration:
24626 decl-specifier-seq ... [opt] declarator
24627 decl-specifier-seq declarator = assignment-expression
24628 decl-specifier-seq ... [opt] abstract-declarator [opt]
24629 decl-specifier-seq abstract-declarator [opt] = assignment-expression
24631 The parser flags FLAGS is used to control type-specifier parsing.
24633 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
24634 declares a template parameter. (In that case, a non-nested `>'
24635 token encountered during the parsing of the assignment-expression
24636 is not interpreted as a greater-than operator.)
24638 Returns a representation of the parameter, or NULL if an error
24639 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
24640 true iff the declarator is of the form "(p)". */
24642 static cp_parameter_declarator
*
24643 cp_parser_parameter_declaration (cp_parser
*parser
,
24644 cp_parser_flags flags
,
24645 bool template_parm_p
,
24646 bool *parenthesized_p
)
24648 int declares_class_or_enum
;
24649 cp_decl_specifier_seq decl_specifiers
;
24650 cp_declarator
*declarator
;
24651 tree default_argument
;
24652 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
24653 const char *saved_message
;
24654 bool template_parameter_pack_p
= false;
24656 /* In a template parameter, `>' is not an operator.
24660 When parsing a default template-argument for a non-type
24661 template-parameter, the first non-nested `>' is taken as the end
24662 of the template parameter-list rather than a greater-than
24665 /* Type definitions may not appear in parameter types. */
24666 saved_message
= parser
->type_definition_forbidden_message
;
24667 parser
->type_definition_forbidden_message
24668 = G_("types may not be defined in parameter types");
24670 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
24671 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
24672 (current_template_parms
)) : 0);
24674 /* Parse the declaration-specifiers. */
24675 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24676 cp_parser_decl_specifier_seq (parser
,
24679 &declares_class_or_enum
);
24681 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
24682 type-constraint opt auto can be used as a decl-specifier of the
24683 decl-specifier-seq of a parameter-declaration of a function declaration
24684 or lambda-expression..." but we must not synthesize an implicit template
24685 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
24686 we want to synthesize only the first auto. */
24687 auto cleanup
= make_temp_override
24688 (parser
->auto_is_implicit_function_template_parm_p
, false);
24690 /* Complain about missing 'typename' or other invalid type names. */
24691 if (!decl_specifiers
.any_type_specifiers_p
24692 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24693 decl_specifiers
.type
= error_mark_node
;
24695 /* If an error occurred, there's no reason to attempt to parse the
24696 rest of the declaration. */
24697 if (cp_parser_error_occurred (parser
))
24699 parser
->type_definition_forbidden_message
= saved_message
;
24703 /* Peek at the next token. */
24704 token
= cp_lexer_peek_token (parser
->lexer
);
24706 /* If the next token is a `)', `,', `=', `>', or `...', then there
24707 is no declarator. However, when variadic templates are enabled,
24708 there may be a declarator following `...'. */
24709 if (token
->type
== CPP_CLOSE_PAREN
24710 || token
->type
== CPP_COMMA
24711 || token
->type
== CPP_EQ
24712 || token
->type
== CPP_GREATER
)
24715 if (parenthesized_p
)
24716 *parenthesized_p
= false;
24718 /* Otherwise, there should be a declarator. */
24721 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
24722 parser
->default_arg_ok_p
= false;
24724 /* After seeing a decl-specifier-seq, if the next token is not a
24725 "(" or "{", there is no possibility that the code is a valid
24726 expression. Therefore, if parsing tentatively, we commit at
24728 if (!parser
->in_template_argument_list_p
24729 /* In an expression context, having seen:
24733 we cannot be sure whether we are looking at a
24734 function-type (taking a "char" as a parameter) or a cast
24735 of some object of type "char" to "int". */
24736 && !parser
->in_type_id_in_expr_p
24737 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
24738 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
24740 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24742 if (decl_specifiers
.type
24743 && template_placeholder_p (decl_specifiers
.type
))
24744 /* This is a CTAD expression, not a parameter declaration. */
24745 cp_parser_simulate_error (parser
);
24748 cp_parser_commit_to_tentative_parse (parser
);
24750 /* Parse the declarator. */
24751 declarator_token_start
= token
;
24752 declarator
= cp_parser_declarator (parser
,
24753 CP_PARSER_DECLARATOR_EITHER
,
24754 CP_PARSER_FLAGS_NONE
,
24755 /*ctor_dtor_or_conv_p=*/NULL
,
24757 /*member_p=*/false,
24758 /*friend_p=*/false,
24759 /*static_p=*/false);
24760 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
24761 /* After the declarator, allow more attributes. */
24762 decl_specifiers
.attributes
24763 = attr_chainon (decl_specifiers
.attributes
,
24764 cp_parser_attributes_opt (parser
));
24766 /* If the declarator is a template parameter pack, remember that and
24767 clear the flag in the declarator itself so we don't get errors
24768 from grokdeclarator. */
24769 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
24771 declarator
->parameter_pack_p
= false;
24772 template_parameter_pack_p
= true;
24776 /* If the next token is an ellipsis, and we have not seen a declarator
24777 name, and if either the type of the declarator contains parameter
24778 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
24779 for, eg, abbreviated integral type names), then we actually have a
24780 parameter pack expansion expression. Otherwise, leave the ellipsis
24781 for a C-style variadic function. */
24782 token
= cp_lexer_peek_token (parser
->lexer
);
24784 /* If a function parameter pack was specified and an implicit template
24785 parameter was introduced during cp_parser_parameter_declaration,
24786 change any implicit parameters introduced into packs. */
24787 if (parser
->implicit_template_parms
24788 && ((token
->type
== CPP_ELLIPSIS
24789 && declarator_can_be_parameter_pack (declarator
))
24790 || (declarator
&& declarator
->parameter_pack_p
)))
24792 int latest_template_parm_idx
= TREE_VEC_LENGTH
24793 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
24795 if (latest_template_parm_idx
!= template_parm_idx
)
24796 decl_specifiers
.type
= convert_generic_types_to_packs
24797 (decl_specifiers
.type
,
24798 template_parm_idx
, latest_template_parm_idx
);
24801 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24803 tree type
= decl_specifiers
.type
;
24805 if (type
&& DECL_P (type
))
24806 type
= TREE_TYPE (type
);
24809 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
24810 && (template_parm_p
|| uses_parameter_packs (type
)))
24811 || (!type
&& template_parm_p
))
24812 && declarator_can_be_parameter_pack (declarator
))
24814 /* Consume the `...'. */
24815 cp_lexer_consume_token (parser
->lexer
);
24816 maybe_warn_variadic_templates ();
24818 /* Build a pack expansion type */
24819 if (template_parm_p
)
24820 template_parameter_pack_p
= true;
24821 else if (declarator
)
24822 declarator
->parameter_pack_p
= true;
24824 decl_specifiers
.type
= make_pack_expansion (type
);
24828 /* The restriction on defining new types applies only to the type
24829 of the parameter, not to the default argument. */
24830 parser
->type_definition_forbidden_message
= saved_message
;
24832 /* If the next token is `=', then process a default argument. */
24833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24835 tree type
= decl_specifiers
.type
;
24836 token
= cp_lexer_peek_token (parser
->lexer
);
24838 declarator
->init_loc
= token
->location
;
24839 /* If we are defining a class, then the tokens that make up the
24840 default argument must be saved and processed later. */
24841 if (!template_parm_p
&& at_class_scope_p ()
24842 && TYPE_BEING_DEFINED (current_class_type
)
24843 && !LAMBDA_TYPE_P (current_class_type
))
24844 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
24846 /* A constrained-type-specifier may declare a type
24847 template-parameter. */
24848 else if (declares_constrained_type_template_parameter (type
))
24850 = cp_parser_default_type_template_argument (parser
);
24852 /* A constrained-type-specifier may declare a
24853 template-template-parameter. */
24854 else if (declares_constrained_template_template_parameter (type
))
24856 = cp_parser_default_template_template_argument (parser
);
24858 /* Outside of a class definition, we can just parse the
24859 assignment-expression. */
24862 = cp_parser_default_argument (parser
, template_parm_p
);
24864 if (!parser
->default_arg_ok_p
)
24866 permerror (token
->location
,
24867 "default arguments are only "
24868 "permitted for function parameters");
24870 else if ((declarator
&& declarator
->parameter_pack_p
)
24871 || template_parameter_pack_p
24872 || (decl_specifiers
.type
24873 && PACK_EXPANSION_P (decl_specifiers
.type
)))
24875 /* Find the name of the parameter pack. */
24876 cp_declarator
*id_declarator
= declarator
;
24877 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
24878 id_declarator
= id_declarator
->declarator
;
24880 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
24881 error_at (declarator_token_start
->location
,
24883 ? G_("template parameter pack %qD "
24884 "cannot have a default argument")
24885 : G_("parameter pack %qD cannot have "
24886 "a default argument"),
24887 id_declarator
->u
.id
.unqualified_name
);
24889 error_at (declarator_token_start
->location
,
24891 ? G_("template parameter pack cannot have "
24892 "a default argument")
24893 : G_("parameter pack cannot have a "
24894 "default argument"));
24896 default_argument
= NULL_TREE
;
24900 default_argument
= NULL_TREE
;
24902 if (default_argument
)
24903 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
24905 /* Generate a location for the parameter, ranging from the start of the
24906 initial token to the end of the final token (using input_location for
24907 the latter, set up by cp_lexer_set_source_position_from_token when
24910 If we have a identifier, then use it for the caret location, e.g.
24912 extern int callee (int one, int (*two)(int, int), float three);
24913 ~~~~~~^~~~~~~~~~~~~~
24915 otherwise, reuse the start location for the caret location e.g.:
24917 extern int callee (int one, int (*)(int, int), float three);
24921 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
24922 ? declarator
->id_loc
24923 : decl_spec_token_start
->location
);
24924 location_t param_loc
= make_location (caret_loc
,
24925 decl_spec_token_start
->location
,
24928 return make_parameter_declarator (&decl_specifiers
,
24932 template_parameter_pack_p
);
24935 /* Parse a default argument and return it.
24937 TEMPLATE_PARM_P is true if this is a default argument for a
24938 non-type template parameter. */
24940 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
24942 tree default_argument
= NULL_TREE
;
24943 bool saved_greater_than_is_operator_p
;
24944 unsigned char saved_local_variables_forbidden_p
;
24945 bool non_constant_p
, is_direct_init
;
24947 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
24949 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
24950 parser
->greater_than_is_operator_p
= !template_parm_p
;
24951 auto odsd
= make_temp_override (parser
->omp_declare_simd
, NULL
);
24952 auto ord
= make_temp_override (parser
->oacc_routine
, NULL
);
24953 auto oafp
= make_temp_override (parser
->omp_attrs_forbidden_p
, false);
24955 /* Local variable names (and the `this' keyword) may not
24956 appear in a default argument. */
24957 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
24958 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
24959 /* Parse the assignment-expression. */
24960 if (template_parm_p
)
24961 push_deferring_access_checks (dk_no_deferred
);
24962 tree saved_class_ptr
= NULL_TREE
;
24963 tree saved_class_ref
= NULL_TREE
;
24964 /* The "this" pointer is not valid in a default argument. */
24967 saved_class_ptr
= current_class_ptr
;
24968 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
24969 saved_class_ref
= current_class_ref
;
24970 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
24973 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
24974 /* Restore the "this" pointer. */
24977 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
24978 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
24980 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
24981 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
24982 if (template_parm_p
)
24983 pop_deferring_access_checks ();
24984 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
24985 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
24987 return default_argument
;
24990 /* Parse a function-body.
24993 compound_statement */
24996 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
24998 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
24999 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
25003 /* Parse a ctor-initializer-opt followed by a function-body. Return
25004 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25005 is true we are parsing a function-try-block. */
25008 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
25009 bool in_function_try_block
)
25012 const bool check_body_p
25013 = (DECL_CONSTRUCTOR_P (current_function_decl
)
25014 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
));
25017 if (in_function_try_block
25018 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
25019 && cxx_dialect
< cxx20
)
25021 if (DECL_CONSTRUCTOR_P (current_function_decl
))
25022 pedwarn (input_location
, OPT_Wc__20_extensions
,
25023 "function-try-block body of %<constexpr%> constructor only "
25024 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25026 pedwarn (input_location
, OPT_Wc__20_extensions
,
25027 "function-try-block body of %<constexpr%> function only "
25028 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25031 /* Begin the function body. */
25032 body
= begin_function_body ();
25033 /* Parse the optional ctor-initializer. */
25034 cp_parser_ctor_initializer_opt (parser
);
25036 /* If we're parsing a constexpr constructor definition, we need
25037 to check that the constructor body is indeed empty. However,
25038 before we get to cp_parser_function_body lot of junk has been
25039 generated, so we can't just check that we have an empty block.
25040 Rather we take a snapshot of the outermost block, and check whether
25041 cp_parser_function_body changed its state. */
25044 list
= cur_stmt_list
;
25045 if (STATEMENT_LIST_TAIL (list
))
25046 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
25048 /* Parse the function-body. */
25049 cp_parser_function_body (parser
, in_function_try_block
);
25051 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
25052 /* Finish the function body. */
25053 finish_function_body (body
);
25056 /* Parse an initializer.
25059 = initializer-clause
25060 ( expression-list )
25062 Returns an expression representing the initializer. If no
25063 initializer is present, NULL_TREE is returned.
25065 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25066 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25067 set to TRUE if there is no initializer present. If there is an
25068 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25069 is set to true; otherwise it is set to false. */
25072 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
25073 bool* non_constant_p
, bool subexpression_p
)
25078 /* Peek at the next token. */
25079 token
= cp_lexer_peek_token (parser
->lexer
);
25081 /* Let our caller know whether or not this initializer was
25083 *is_direct_init
= (token
->type
!= CPP_EQ
);
25084 /* Assume that the initializer is constant. */
25085 *non_constant_p
= false;
25087 if (token
->type
== CPP_EQ
)
25089 /* Consume the `='. */
25090 cp_lexer_consume_token (parser
->lexer
);
25091 /* Parse the initializer-clause. */
25092 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
25094 else if (token
->type
== CPP_OPEN_PAREN
)
25096 vec
<tree
, va_gc
> *vec
;
25097 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
25099 /*allow_expansion_p=*/true,
25102 return error_mark_node
;
25103 init
= build_tree_list_vec (vec
);
25104 release_tree_vector (vec
);
25106 else if (token
->type
== CPP_OPEN_BRACE
)
25108 cp_lexer_set_source_position (parser
->lexer
);
25109 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
25110 init
= cp_parser_braced_list (parser
, non_constant_p
);
25111 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
25115 /* Anything else is an error. */
25116 cp_parser_error (parser
, "expected initializer");
25117 init
= error_mark_node
;
25120 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
25121 init
= error_mark_node
;
25126 /* Parse an initializer-clause.
25128 initializer-clause:
25129 assignment-expression
25132 Returns an expression representing the initializer.
25134 If the `assignment-expression' production is used the value
25135 returned is simply a representation for the expression.
25137 Otherwise, calls cp_parser_braced_list. */
25140 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
25142 cp_expr initializer
;
25144 /* Assume the expression is constant. */
25145 *non_constant_p
= false;
25147 /* If it is not a `{', then we are looking at an
25148 assignment-expression. */
25149 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
25152 = cp_parser_constant_expression (parser
,
25153 /*allow_non_constant_p=*/2,
25157 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
25159 return initializer
;
25162 /* Parse a brace-enclosed initializer list.
25165 { initializer-list , [opt] }
25166 { designated-initializer-list , [opt] }
25169 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25170 the elements of the initializer-list (or NULL, if the last
25171 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25172 NULL_TREE. There is no way to detect whether or not the optional
25173 trailing `,' was provided. NON_CONSTANT_P is as for
25174 cp_parser_initializer. */
25177 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
25180 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25182 /* Consume the `{' token. */
25183 matching_braces braces
;
25184 braces
.require_open (parser
);
25185 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25186 initializer
= make_node (CONSTRUCTOR
);
25187 /* If it's not a `}', then there is a non-trivial initializer. */
25188 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
25191 /* Parse the initializer list. */
25192 CONSTRUCTOR_ELTS (initializer
)
25193 = cp_parser_initializer_list (parser
, non_constant_p
, &designated
);
25194 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer
) = designated
;
25195 /* A trailing `,' token is allowed. */
25196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25197 cp_lexer_consume_token (parser
->lexer
);
25200 *non_constant_p
= false;
25201 /* Now, there should be a trailing `}'. */
25202 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25203 braces
.require_close (parser
);
25204 TREE_TYPE (initializer
) = init_list_type_node
;
25206 cp_expr
result (initializer
);
25207 /* Build a location of the form:
25210 with caret==start at the open brace, finish at the close brace. */
25211 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
25212 result
.set_location (combined_loc
);
25216 /* Consume tokens up to, and including, the next non-nested closing `]'.
25217 Returns true iff we found a closing `]'. */
25220 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
25222 unsigned square_depth
= 0;
25226 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
25228 switch (token
->type
)
25230 case CPP_PRAGMA_EOL
:
25231 if (!parser
->lexer
->in_pragma
)
25235 /* If we've run out of tokens, then there is no closing `]'. */
25238 case CPP_OPEN_SQUARE
:
25242 case CPP_CLOSE_SQUARE
:
25243 if (!square_depth
--)
25245 cp_lexer_consume_token (parser
->lexer
);
25254 /* Consume the token. */
25255 cp_lexer_consume_token (parser
->lexer
);
25259 /* Return true if we are looking at an array-designator, false otherwise. */
25262 cp_parser_array_designator_p (cp_parser
*parser
)
25264 /* Consume the `['. */
25265 cp_lexer_consume_token (parser
->lexer
);
25267 cp_lexer_save_tokens (parser
->lexer
);
25269 /* Skip tokens until the next token is a closing square bracket.
25270 If we find the closing `]', and the next token is a `=', then
25271 we are looking at an array designator. */
25272 bool array_designator_p
25273 = (cp_parser_skip_to_closing_square_bracket (parser
)
25274 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
25276 /* Roll back the tokens we skipped. */
25277 cp_lexer_rollback_tokens (parser
->lexer
);
25279 return array_designator_p
;
25282 /* Parse an initializer-list.
25285 initializer-clause ... [opt]
25286 initializer-list , initializer-clause ... [opt]
25290 designated-initializer-list:
25291 designated-initializer-clause
25292 designated-initializer-list , designated-initializer-clause
25294 designated-initializer-clause:
25295 designator brace-or-equal-initializer
25303 designation initializer-clause ...[opt]
25304 initializer-list , designation initializer-clause ...[opt]
25309 [ constant-expression ] =
25311 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25312 for the initializer. If the INDEX of the elt is non-NULL, it is the
25313 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25314 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25315 are any designators. */
25317 static vec
<constructor_elt
, va_gc
> *
25318 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
,
25321 vec
<constructor_elt
, va_gc
> *v
= NULL
;
25322 bool first_p
= true;
25323 tree first_designator
= NULL_TREE
;
25325 /* Assume all of the expressions are constant. */
25326 *non_constant_p
= false;
25328 unsigned nelts
= 0;
25329 int suppress
= suppress_location_wrappers
;
25331 /* Parse the rest of the list. */
25337 bool clause_non_constant_p
;
25338 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25340 /* Handle the C++20 syntax, '. id ='. */
25341 if ((cxx_dialect
>= cxx20
25342 || cp_parser_allow_gnu_extensions_p (parser
))
25343 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
25344 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
25345 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
25346 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
25347 == CPP_OPEN_BRACE
)))
25349 if (pedantic
&& cxx_dialect
< cxx20
)
25350 pedwarn (loc
, OPT_Wc__20_extensions
,
25351 "C++ designated initializers only available with "
25352 "%<-std=c++20%> or %<-std=gnu++20%>");
25353 /* Consume the `.'. */
25354 cp_lexer_consume_token (parser
->lexer
);
25355 /* Consume the identifier. */
25356 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
25357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
25358 /* Consume the `='. */
25359 cp_lexer_consume_token (parser
->lexer
);
25361 /* Also, if the next token is an identifier and the following one is a
25362 colon, we are looking at the GNU designated-initializer
25364 else if (cp_parser_allow_gnu_extensions_p (parser
)
25365 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
25366 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
25369 /* Warn the user that they are using an extension. */
25370 pedwarn (loc
, OPT_Wpedantic
,
25371 "ISO C++ does not allow GNU designated initializers");
25372 /* Consume the identifier. */
25373 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
25374 /* Consume the `:'. */
25375 cp_lexer_consume_token (parser
->lexer
);
25377 /* Also handle C99 array designators, '[ const ] ='. */
25378 else if (cp_parser_allow_gnu_extensions_p (parser
)
25379 && !c_dialect_objc ()
25380 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25382 /* In C++11, [ could start a lambda-introducer. */
25383 bool non_const
= false;
25385 cp_parser_parse_tentatively (parser
);
25387 if (!cp_parser_array_designator_p (parser
))
25389 cp_parser_simulate_error (parser
);
25390 designator
= NULL_TREE
;
25394 designator
= cp_parser_constant_expression (parser
, true,
25396 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25397 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
25400 if (!cp_parser_parse_definitely (parser
))
25401 designator
= NULL_TREE
;
25403 && (!require_potential_rvalue_constant_expression
25405 designator
= NULL_TREE
;
25407 /* Warn the user that they are using an extension. */
25408 pedwarn (loc
, OPT_Wpedantic
,
25409 "ISO C++ does not allow C99 designated initializers");
25412 designator
= NULL_TREE
;
25416 first_designator
= designator
;
25419 else if (cxx_dialect
>= cxx20
25420 && first_designator
!= error_mark_node
25421 && (!first_designator
!= !designator
))
25423 error_at (loc
, "either all initializer clauses should be designated "
25424 "or none of them should be");
25425 first_designator
= error_mark_node
;
25427 else if (cxx_dialect
< cxx20
&& !first_designator
)
25428 first_designator
= designator
;
25430 /* Parse the initializer. */
25431 initializer
= cp_parser_initializer_clause (parser
,
25432 &clause_non_constant_p
);
25433 /* If any clause is non-constant, so is the entire initializer. */
25434 if (clause_non_constant_p
)
25435 *non_constant_p
= true;
25437 /* If we have an ellipsis, this is an initializer pack
25439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25441 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25443 /* Consume the `...'. */
25444 cp_lexer_consume_token (parser
->lexer
);
25446 if (designator
&& cxx_dialect
>= cxx20
)
25448 "%<...%> not allowed in designated initializer list");
25450 /* Turn the initializer into an initializer expansion. */
25451 initializer
= make_pack_expansion (initializer
);
25454 /* Add it to the vector. */
25455 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
25457 /* If the next token is not a comma, we have reached the end of
25459 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25462 /* Peek at the next token. */
25463 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25464 /* If the next token is a `}', then we're still done. An
25465 initializer-clause can have a trailing `,' after the
25466 initializer-list and before the closing `}'. */
25467 if (token
->type
== CPP_CLOSE_BRACE
)
25470 /* Suppress location wrappers in a long initializer to save memory
25471 (14179). The cutoff is chosen arbitrarily. */
25472 const unsigned loc_max
= 256;
25474 if (TREE_CODE (initializer
) == CONSTRUCTOR
)
25475 /* Look one level down because it's easy. Looking deeper would require
25476 passing down a nelts pointer, and I don't think multi-level massive
25477 initializers are common enough to justify this. */
25478 incr
= CONSTRUCTOR_NELTS (initializer
);
25480 if (nelts
>= loc_max
&& (nelts
- incr
) < loc_max
)
25481 ++suppress_location_wrappers
;
25483 /* Consume the `,' token. */
25484 cp_lexer_consume_token (parser
->lexer
);
25487 /* The same identifier shall not appear in multiple designators
25488 of a designated-initializer-list. */
25489 if (first_designator
)
25492 tree designator
, val
;
25493 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
25494 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
25496 if (IDENTIFIER_MARKED (designator
))
25498 error_at (cp_expr_loc_or_input_loc (val
),
25499 "%<.%s%> designator used multiple times in "
25500 "the same initializer list",
25501 IDENTIFIER_POINTER (designator
));
25502 (*v
)[i
].index
= error_mark_node
;
25505 IDENTIFIER_MARKED (designator
) = 1;
25507 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
25508 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
25509 IDENTIFIER_MARKED (designator
) = 0;
25512 suppress_location_wrappers
= suppress
;
25514 *designated
= first_designator
!= NULL_TREE
;
25518 /* Classes [gram.class] */
25520 /* Parse a class-name.
25526 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25527 to indicate that names looked up in dependent types should be
25528 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
25529 keyword has been used to indicate that the name that appears next
25530 is a template. TAG_TYPE indicates the explicit tag given before
25531 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
25532 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
25533 is the class being defined in a class-head. If ENUM_OK is TRUE,
25534 enum-names are also accepted.
25536 Returns the TYPE_DECL representing the class. */
25539 cp_parser_class_name (cp_parser
*parser
,
25540 bool typename_keyword_p
,
25541 bool template_keyword_p
,
25542 enum tag_types tag_type
,
25543 bool check_dependency_p
,
25545 bool is_declaration
,
25549 tree identifier
= NULL_TREE
;
25551 /* All class-names start with an identifier. */
25552 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25553 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
25555 cp_parser_error (parser
, "expected class-name");
25556 return error_mark_node
;
25559 /* PARSER->SCOPE can be cleared when parsing the template-arguments
25560 to a template-id, so we save it here. Consider object scope too,
25561 so that make_typename_type below can use it (cp_parser_template_name
25562 considers object scope also). This may happen with code like
25564 p->template A<T>::a()
25566 where we first want to look up A<T>::a in the class of the object
25567 expression, as per [basic.lookup.classref]. */
25568 tree scope
= parser
->scope
? parser
->scope
: parser
->context
->object_type
;
25569 /* This only checks parser->scope to avoid duplicate errors; if
25570 ->object_type is erroneous, go on to give a parse error. */
25571 if (parser
->scope
== error_mark_node
)
25572 return error_mark_node
;
25574 /* Any name names a type if we're following the `typename' keyword
25575 in a qualified name where the enclosing scope is type-dependent. */
25576 const bool typename_p
= (typename_keyword_p
25578 && TYPE_P (parser
->scope
)
25579 && dependent_scope_p (parser
->scope
));
25580 /* Handle the common case (an identifier, but not a template-id)
25582 if (token
->type
== CPP_NAME
25583 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
25585 cp_token
*identifier_token
;
25588 /* Look for the identifier. */
25589 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
25590 ambiguous_p
= identifier_token
->error_reported
;
25591 identifier
= cp_parser_identifier (parser
);
25592 /* If the next token isn't an identifier, we are certainly not
25593 looking at a class-name. */
25594 if (identifier
== error_mark_node
)
25595 decl
= error_mark_node
;
25596 /* If we know this is a type-name, there's no need to look it
25598 else if (typename_p
)
25602 tree ambiguous_decls
;
25603 /* If we already know that this lookup is ambiguous, then
25604 we've already issued an error message; there's no reason
25608 cp_parser_simulate_error (parser
);
25609 return error_mark_node
;
25611 /* If the next token is a `::', then the name must be a type
25614 [basic.lookup.qual]
25616 During the lookup for a name preceding the :: scope
25617 resolution operator, object, function, and enumerator
25618 names are ignored. */
25619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
25620 tag_type
= scope_type
;
25621 /* Look up the name. */
25622 decl
= cp_parser_lookup_name (parser
, identifier
,
25624 /*is_template=*/false,
25625 /*is_namespace=*/false,
25626 check_dependency_p
,
25628 identifier_token
->location
);
25629 if (ambiguous_decls
)
25631 if (cp_parser_parsing_tentatively (parser
))
25632 cp_parser_simulate_error (parser
);
25633 return error_mark_node
;
25639 /* Try a template-id. */
25640 decl
= cp_parser_template_id (parser
, template_keyword_p
,
25641 check_dependency_p
,
25644 if (decl
== error_mark_node
)
25645 return error_mark_node
;
25648 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
25650 /* If this is a typename, create a TYPENAME_TYPE. */
25651 if (typename_p
&& decl
!= error_mark_node
)
25653 decl
= make_typename_type (scope
, decl
, typename_type
,
25654 /*complain=*/tf_error
);
25655 if (decl
!= error_mark_node
)
25656 decl
= TYPE_NAME (decl
);
25659 decl
= strip_using_decl (decl
);
25661 /* Check to see that it is really the name of a class. */
25662 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
25663 && identifier_p (TREE_OPERAND (decl
, 0))
25664 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
25665 /* Situations like this:
25667 template <typename T> struct A {
25668 typename T::template X<int>::I i;
25671 are problematic. Is `T::template X<int>' a class-name? The
25672 standard does not seem to be definitive, but there is no other
25673 valid interpretation of the following `::'. Therefore, those
25674 names are considered class-names. */
25676 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
25677 if (decl
!= error_mark_node
)
25678 decl
= TYPE_NAME (decl
);
25680 else if (TREE_CODE (decl
) != TYPE_DECL
25681 || TREE_TYPE (decl
) == error_mark_node
25682 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
25683 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
25684 /* In Objective-C 2.0, a classname followed by '.' starts a
25685 dot-syntax expression, and it's not a type-name. */
25686 || (c_dialect_objc ()
25687 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
25688 && objc_is_class_name (decl
)))
25689 decl
= error_mark_node
;
25691 if (decl
== error_mark_node
)
25692 cp_parser_error (parser
, "expected class-name");
25693 else if (identifier
&& !parser
->scope
)
25694 maybe_note_name_used_in_class (identifier
, decl
);
25699 /* Make sure that any member-function parameters are in scope.
25700 For instance, a function's noexcept-specifier can use the function's
25704 void fn (int p) noexcept(noexcept(p));
25707 so we need to make sure name lookup can find them. This is used
25708 when we delay parsing of the noexcept-specifier. */
25711 inject_parm_decls (tree decl
)
25713 begin_scope (sk_function_parms
, decl
);
25714 tree args
= DECL_ARGUMENTS (decl
);
25716 do_push_parm_decls (decl
, args
, /*nonparms=*/NULL
);
25718 if (args
&& is_this_parameter (args
))
25720 gcc_checking_assert (current_class_ptr
== NULL_TREE
);
25721 current_class_ref
= cp_build_fold_indirect_ref (args
);
25722 current_class_ptr
= args
;
25726 /* Undo the effects of inject_parm_decls. */
25729 pop_injected_parms (void)
25731 pop_bindings_and_leave_scope ();
25732 current_class_ptr
= current_class_ref
= NULL_TREE
;
25735 /* Parse a class-specifier.
25738 class-head { member-specification [opt] }
25740 Returns the TREE_TYPE representing the class. */
25743 cp_parser_class_specifier_1 (cp_parser
* parser
)
25746 tree attributes
= NULL_TREE
;
25747 bool nested_name_specifier_p
;
25748 unsigned saved_num_template_parameter_lists
;
25749 bool saved_in_function_body
;
25750 unsigned char in_statement
;
25751 bool in_switch_statement_p
;
25752 bool saved_in_unbraced_linkage_specification_p
;
25753 tree old_scope
= NULL_TREE
;
25754 tree scope
= NULL_TREE
;
25755 cp_token
*closing_brace
;
25757 push_deferring_access_checks (dk_no_deferred
);
25759 /* Parse the class-head. */
25760 type
= cp_parser_class_head (parser
,
25761 &nested_name_specifier_p
);
25762 /* If the class-head was a semantic disaster, skip the entire body
25766 cp_parser_skip_to_end_of_block_or_statement (parser
);
25767 pop_deferring_access_checks ();
25768 return error_mark_node
;
25771 /* Look for the `{'. */
25772 matching_braces braces
;
25773 if (!braces
.require_open (parser
))
25775 pop_deferring_access_checks ();
25776 return error_mark_node
;
25779 cp_ensure_no_omp_declare_simd (parser
);
25780 cp_ensure_no_oacc_routine (parser
);
25782 /* Issue an error message if type-definitions are forbidden here. */
25783 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
25784 /* Remember that we are defining one more class. */
25785 ++parser
->num_classes_being_defined
;
25786 /* Inside the class, surrounding template-parameter-lists do not
25788 saved_num_template_parameter_lists
25789 = parser
->num_template_parameter_lists
;
25790 parser
->num_template_parameter_lists
= 0;
25791 /* We are not in a function body. */
25792 saved_in_function_body
= parser
->in_function_body
;
25793 parser
->in_function_body
= false;
25794 /* Or in a loop. */
25795 in_statement
= parser
->in_statement
;
25796 parser
->in_statement
= 0;
25797 /* Or in a switch. */
25798 in_switch_statement_p
= parser
->in_switch_statement_p
;
25799 parser
->in_switch_statement_p
= false;
25800 /* We are not immediately inside an extern "lang" block. */
25801 saved_in_unbraced_linkage_specification_p
25802 = parser
->in_unbraced_linkage_specification_p
;
25803 parser
->in_unbraced_linkage_specification_p
= false;
25805 /* Start the class. */
25806 if (nested_name_specifier_p
)
25808 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
25809 /* SCOPE must be a scope nested inside current scope. */
25810 if (is_nested_namespace (current_namespace
,
25811 decl_namespace_context (scope
)))
25812 old_scope
= push_inner_scope (scope
);
25814 nested_name_specifier_p
= false;
25816 type
= begin_class_definition (type
);
25818 if (type
== error_mark_node
)
25819 /* If the type is erroneous, skip the entire body of the class. */
25820 cp_parser_skip_to_closing_brace (parser
);
25822 /* Parse the member-specification. */
25823 cp_parser_member_specification_opt (parser
);
25825 /* Look for the trailing `}'. */
25826 closing_brace
= braces
.require_close (parser
);
25827 /* Look for trailing attributes to apply to this class. */
25828 if (cp_parser_allow_gnu_extensions_p (parser
))
25829 attributes
= cp_parser_gnu_attributes_opt (parser
);
25830 if (type
!= error_mark_node
)
25831 type
= finish_struct (type
, attributes
);
25832 if (nested_name_specifier_p
)
25833 pop_inner_scope (old_scope
, scope
);
25835 /* We've finished a type definition. Check for the common syntax
25836 error of forgetting a semicolon after the definition. We need to
25837 be careful, as we can't just check for not-a-semicolon and be done
25838 with it; the user might have typed:
25840 class X { } c = ...;
25841 class X { } *p = ...;
25843 and so forth. Instead, enumerate all the possible tokens that
25844 might follow this production; if we don't see one of them, then
25845 complain and silently insert the semicolon. */
25847 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25848 bool want_semicolon
= true;
25850 if (cp_next_tokens_can_be_std_attribute_p (parser
))
25851 /* Don't try to parse c++11 attributes here. As per the
25852 grammar, that should be a task for
25853 cp_parser_decl_specifier_seq. */
25854 want_semicolon
= false;
25856 switch (token
->type
)
25859 case CPP_SEMICOLON
:
25862 case CPP_OPEN_PAREN
:
25863 case CPP_CLOSE_PAREN
:
25865 want_semicolon
= false;
25868 /* While it's legal for type qualifiers and storage class
25869 specifiers to follow type definitions in the grammar, only
25870 compiler testsuites contain code like that. Assume that if
25871 we see such code, then what we're really seeing is a case
25875 const <type> var = ...;
25880 static <type> func (...) ...
25882 i.e. the qualifier or specifier applies to the next
25883 declaration. To do so, however, we need to look ahead one
25884 more token to see if *that* token is a type specifier.
25886 This code could be improved to handle:
25889 static const <type> var = ...; */
25891 if (keyword_is_decl_specifier (token
->keyword
))
25893 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25895 /* Handling user-defined types here would be nice, but very
25898 = (lookahead
->type
== CPP_KEYWORD
25899 && keyword_begins_type_specifier (lookahead
->keyword
));
25906 /* If we don't have a type, then something is very wrong and we
25907 shouldn't try to do anything clever. Likewise for not seeing the
25909 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
25911 /* Locate the closing brace. */
25912 cp_token_position prev
25913 = cp_lexer_previous_token_position (parser
->lexer
);
25914 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
25915 location_t loc
= prev_token
->location
;
25917 /* We want to suggest insertion of a ';' immediately *after* the
25918 closing brace, so, if we can, offset the location by 1 column. */
25919 location_t next_loc
= loc
;
25920 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
25921 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
25923 rich_location
richloc (line_table
, next_loc
);
25925 /* If we successfully offset the location, suggest the fix-it. */
25926 if (next_loc
!= loc
)
25927 richloc
.add_fixit_insert_before (next_loc
, ";");
25929 if (CLASSTYPE_DECLARED_CLASS (type
))
25930 error_at (&richloc
,
25931 "expected %<;%> after class definition");
25932 else if (TREE_CODE (type
) == RECORD_TYPE
)
25933 error_at (&richloc
,
25934 "expected %<;%> after struct definition");
25935 else if (TREE_CODE (type
) == UNION_TYPE
)
25936 error_at (&richloc
,
25937 "expected %<;%> after union definition");
25939 gcc_unreachable ();
25941 /* Unget one token and smash it to look as though we encountered
25942 a semicolon in the input stream. */
25943 cp_lexer_set_token_position (parser
->lexer
, prev
);
25944 token
= cp_lexer_peek_token (parser
->lexer
);
25945 token
->type
= CPP_SEMICOLON
;
25946 token
->keyword
= RID_MAX
;
25950 /* If this class is not itself within the scope of another class,
25951 then we need to parse the bodies of all of the queued function
25952 definitions. Note that the queued functions defined in a class
25953 are not always processed immediately following the
25954 class-specifier for that class. Consider:
25957 struct B { void f() { sizeof (A); } };
25960 If `f' were processed before the processing of `A' were
25961 completed, there would be no way to compute the size of `A'.
25962 Note that the nesting we are interested in here is lexical --
25963 not the semantic nesting given by TYPE_CONTEXT. In particular,
25966 struct A { struct B; };
25967 struct A::B { void f() { } };
25969 there is no need to delay the parsing of `A::B::f'. */
25970 if (--parser
->num_classes_being_defined
== 0)
25973 tree class_type
= NULL_TREE
;
25974 tree pushed_scope
= NULL_TREE
;
25976 cp_default_arg_entry
*e
;
25978 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
25980 /* Skip default arguments, NSDMIs, etc, in order to improve
25981 error recovery (c++/71169, c++/71832). */
25982 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
25983 vec_safe_truncate (unparsed_nsdmis
, 0);
25984 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
25987 /* In a first pass, parse default arguments to the functions.
25988 Then, in a second pass, parse the bodies of the functions.
25989 This two-phased approach handles cases like:
25997 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
26000 /* If there are default arguments that have not yet been processed,
26001 take care of them now. */
26002 if (class_type
!= e
->class_type
)
26005 pop_scope (pushed_scope
);
26006 class_type
= e
->class_type
;
26007 pushed_scope
= push_scope (class_type
);
26009 /* Make sure that any template parameters are in scope. */
26010 maybe_begin_member_template_processing (decl
);
26011 /* Parse the default argument expressions. */
26012 cp_parser_late_parsing_default_args (parser
, decl
);
26013 /* Remove any template parameters from the symbol table. */
26014 maybe_end_member_template_processing ();
26016 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
26018 /* If there are noexcept-specifiers that have not yet been processed,
26019 take care of them now. Do this before processing NSDMIs as they
26020 may depend on noexcept-specifiers already having been processed. */
26021 tree save_ccp
= current_class_ptr
;
26022 tree save_ccr
= current_class_ref
;
26023 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts
, ix
, decl
)
26025 tree ctx
= DECL_CONTEXT (decl
);
26026 if (class_type
!= ctx
)
26029 pop_scope (pushed_scope
);
26031 pushed_scope
= push_scope (class_type
);
26034 tree def_parse
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
26035 def_parse
= TREE_PURPOSE (def_parse
);
26037 /* Make sure that any template parameters are in scope. */
26038 maybe_begin_member_template_processing (decl
);
26040 /* Make sure that any member-function parameters are in scope.
26041 This function doesn't expect ccp to be set. */
26042 current_class_ptr
= current_class_ref
= NULL_TREE
;
26043 inject_parm_decls (decl
);
26045 /* 'this' is not allowed in static member functions. */
26046 unsigned char local_variables_forbidden_p
26047 = parser
->local_variables_forbidden_p
;
26048 if (DECL_THIS_STATIC (decl
))
26049 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
26051 /* Now we can parse the noexcept-specifier. */
26052 tree spec
= cp_parser_late_noexcept_specifier (parser
, def_parse
);
26054 if (spec
== error_mark_node
)
26057 /* Update the fn's type directly -- it might have escaped
26058 beyond this decl :( */
26059 fixup_deferred_exception_variants (TREE_TYPE (decl
), spec
);
26060 /* Update any instantiations we've already created. We must
26061 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26062 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26064 for (tree i
: DEFPARSE_INSTANTIATIONS (def_parse
))
26065 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i
))
26066 = spec
? TREE_PURPOSE (spec
) : error_mark_node
;
26068 /* Restore the state of local_variables_forbidden_p. */
26069 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
26071 /* The finish_struct call above performed various override checking,
26072 but it skipped unparsed noexcept-specifier operands. Now that we
26073 have resolved them, check again. */
26074 noexcept_override_late_checks (type
, decl
);
26076 /* Remove any member-function parameters from the symbol table. */
26077 pop_injected_parms ();
26079 /* Remove any template parameters from the symbol table. */
26080 maybe_end_member_template_processing ();
26082 vec_safe_truncate (unparsed_noexcepts
, 0);
26084 /* Now parse any NSDMIs. */
26085 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
26087 if (class_type
!= DECL_CONTEXT (decl
))
26090 pop_scope (pushed_scope
);
26091 class_type
= DECL_CONTEXT (decl
);
26092 pushed_scope
= push_scope (class_type
);
26094 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
26095 cp_parser_late_parsing_nsdmi (parser
, decl
);
26097 vec_safe_truncate (unparsed_nsdmis
, 0);
26098 current_class_ptr
= save_ccp
;
26099 current_class_ref
= save_ccr
;
26101 pop_scope (pushed_scope
);
26103 /* Now parse the body of the functions. */
26106 /* OpenMP UDRs need to be parsed before all other functions. */
26107 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
26108 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
26109 cp_parser_late_parsing_for_member (parser
, decl
);
26110 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
26111 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
26112 cp_parser_late_parsing_for_member (parser
, decl
);
26115 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
26116 cp_parser_late_parsing_for_member (parser
, decl
);
26117 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
26120 /* Put back any saved access checks. */
26121 pop_deferring_access_checks ();
26123 /* Restore saved state. */
26124 parser
->in_switch_statement_p
= in_switch_statement_p
;
26125 parser
->in_statement
= in_statement
;
26126 parser
->in_function_body
= saved_in_function_body
;
26127 parser
->num_template_parameter_lists
26128 = saved_num_template_parameter_lists
;
26129 parser
->in_unbraced_linkage_specification_p
26130 = saved_in_unbraced_linkage_specification_p
;
26136 cp_parser_class_specifier (cp_parser
* parser
)
26139 timevar_push (TV_PARSE_STRUCT
);
26140 ret
= cp_parser_class_specifier_1 (parser
);
26141 timevar_pop (TV_PARSE_STRUCT
);
26145 /* Parse a class-head.
26148 class-key identifier [opt] base-clause [opt]
26149 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26150 class-key nested-name-specifier [opt] template-id
26153 class-virt-specifier:
26157 class-key attributes identifier [opt] base-clause [opt]
26158 class-key attributes nested-name-specifier identifier base-clause [opt]
26159 class-key attributes nested-name-specifier [opt] template-id
26162 Upon return BASES is initialized to the list of base classes (or
26163 NULL, if there are none) in the same form returned by
26164 cp_parser_base_clause.
26166 Returns the TYPE of the indicated class. Sets
26167 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26168 involving a nested-name-specifier was used, and FALSE otherwise.
26170 Returns error_mark_node if this is not a class-head.
26172 Returns NULL_TREE if the class-head is syntactically valid, but
26173 semantically invalid in a way that means we should skip the entire
26174 body of the class. */
26177 cp_parser_class_head (cp_parser
* parser
,
26178 bool* nested_name_specifier_p
)
26180 tree nested_name_specifier
;
26181 enum tag_types class_key
;
26182 tree id
= NULL_TREE
;
26183 tree type
= NULL_TREE
;
26186 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
26187 bool template_id_p
= false;
26188 bool qualified_p
= false;
26189 bool invalid_nested_name_p
= false;
26190 bool invalid_explicit_specialization_p
= false;
26191 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
26192 tree pushed_scope
= NULL_TREE
;
26193 unsigned num_templates
;
26194 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
26195 /* Assume no nested-name-specifier will be present. */
26196 *nested_name_specifier_p
= false;
26197 /* Assume no template parameter lists will be used in defining the
26200 parser
->colon_corrects_to_scope_p
= false;
26202 /* Look for the class-key. */
26203 class_key
= cp_parser_class_key (parser
);
26204 if (class_key
== none_type
)
26205 return error_mark_node
;
26207 location_t class_head_start_location
= input_location
;
26209 /* Parse the attributes. */
26210 attributes
= cp_parser_attributes_opt (parser
);
26212 /* If the next token is `::', that is invalid -- but sometimes
26213 people do try to write:
26217 Handle this gracefully by accepting the extra qualifier, and then
26218 issuing an error about it later if this really is a
26219 class-head. If it turns out just to be an elaborated type
26220 specifier, remain silent. */
26221 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
26222 qualified_p
= true;
26224 push_deferring_access_checks (dk_no_check
);
26226 /* Determine the name of the class. Begin by looking for an
26227 optional nested-name-specifier. */
26228 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
26229 nested_name_specifier
26230 = cp_parser_nested_name_specifier_opt (parser
,
26231 /*typename_keyword_p=*/false,
26232 /*check_dependency_p=*/false,
26234 /*is_declaration=*/false);
26235 /* If there was a nested-name-specifier, then there *must* be an
26238 cp_token
*bad_template_keyword
= NULL
;
26240 if (nested_name_specifier
)
26242 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
26243 /* Although the grammar says `identifier', it really means
26244 `class-name' or `template-name'. You are only allowed to
26245 define a class that has already been declared with this
26248 The proposed resolution for Core Issue 180 says that wherever
26249 you see `class T::X' you should treat `X' as a type-name.
26251 It is OK to define an inaccessible class; for example:
26253 class A { class B; };
26256 We do not know if we will see a class-name, or a
26257 template-name. We look for a class-name first, in case the
26258 class-name is a template-id; if we looked for the
26259 template-name first we would stop after the template-name. */
26260 cp_parser_parse_tentatively (parser
);
26261 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26262 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
26263 type
= cp_parser_class_name (parser
,
26264 /*typename_keyword_p=*/false,
26265 /*template_keyword_p=*/false,
26267 /*check_dependency_p=*/false,
26268 /*class_head_p=*/true,
26269 /*is_declaration=*/false);
26270 /* If that didn't work, ignore the nested-name-specifier. */
26271 if (!cp_parser_parse_definitely (parser
))
26273 invalid_nested_name_p
= true;
26274 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
26275 id
= cp_parser_identifier (parser
);
26276 if (id
== error_mark_node
)
26279 /* If we could not find a corresponding TYPE, treat this
26280 declaration like an unqualified declaration. */
26281 if (type
== error_mark_node
)
26282 nested_name_specifier
= NULL_TREE
;
26283 /* Otherwise, count the number of templates used in TYPE and its
26284 containing scopes. */
26286 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
26288 /* Otherwise, the identifier is optional. */
26291 /* We don't know whether what comes next is a template-id,
26292 an identifier, or nothing at all. */
26293 cp_parser_parse_tentatively (parser
);
26294 /* Check for a template-id. */
26295 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
26296 id
= cp_parser_template_id (parser
,
26297 /*template_keyword_p=*/false,
26298 /*check_dependency_p=*/true,
26300 /*is_declaration=*/true);
26301 /* If that didn't work, it could still be an identifier. */
26302 if (!cp_parser_parse_definitely (parser
))
26304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26306 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
26307 id
= cp_parser_identifier (parser
);
26314 template_id_p
= true;
26319 pop_deferring_access_checks ();
26323 cp_parser_check_for_invalid_template_id (parser
, id
,
26325 type_start_token
->location
);
26327 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
26329 /* If it's not a `:' or a `{' then we can't really be looking at a
26330 class-head, since a class-head only appears as part of a
26331 class-specifier. We have to detect this situation before calling
26332 xref_tag, since that has irreversible side-effects. */
26333 if (!cp_parser_next_token_starts_class_definition_p (parser
))
26335 cp_parser_error (parser
, "expected %<{%> or %<:%>");
26336 type
= error_mark_node
;
26340 /* At this point, we're going ahead with the class-specifier, even
26341 if some other problem occurs. */
26342 cp_parser_commit_to_tentative_parse (parser
);
26343 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
26345 cp_parser_error (parser
,
26346 "cannot specify %<override%> for a class");
26347 type
= error_mark_node
;
26350 /* Issue the error about the overly-qualified name now. */
26353 cp_parser_error (parser
,
26354 "global qualification of class name is invalid");
26355 type
= error_mark_node
;
26358 else if (invalid_nested_name_p
)
26360 cp_parser_error (parser
,
26361 "qualified name does not name a class");
26362 type
= error_mark_node
;
26365 else if (nested_name_specifier
)
26369 if (bad_template_keyword
)
26370 /* [temp.names]: in a qualified-id formed by a class-head-name, the
26371 keyword template shall not appear at the top level. */
26372 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
26373 "keyword %<template%> not allowed in class-head-name");
26375 /* Reject typedef-names in class heads. */
26376 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
26378 error_at (type_start_token
->location
,
26379 "invalid class name in declaration of %qD",
26385 /* Figure out in what scope the declaration is being placed. */
26386 scope
= current_scope ();
26387 /* If that scope does not contain the scope in which the
26388 class was originally declared, the program is invalid. */
26389 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
26391 if (at_namespace_scope_p ())
26392 error_at (type_start_token
->location
,
26393 "declaration of %qD in namespace %qD which does not "
26395 type
, scope
, nested_name_specifier
);
26397 error_at (type_start_token
->location
,
26398 "declaration of %qD in %qD which does not enclose %qD",
26399 type
, scope
, nested_name_specifier
);
26405 A declarator-id shall not be qualified except for the
26406 definition of a ... nested class outside of its class
26407 ... [or] the definition or explicit instantiation of a
26408 class member of a namespace outside of its namespace. */
26409 if (scope
== nested_name_specifier
)
26410 permerror (nested_name_specifier_token_start
->location
,
26411 "extra qualification not allowed");
26413 /* An explicit-specialization must be preceded by "template <>". If
26414 it is not, try to recover gracefully. */
26415 if (at_namespace_scope_p ()
26416 && parser
->num_template_parameter_lists
== 0
26417 && !processing_template_parmlist
26420 /* Build a location of this form:
26421 struct typename <ARGS>
26422 ^~~~~~~~~~~~~~~~~~~~~~
26423 with caret==start at the start token, and
26424 finishing at the end of the type. */
26425 location_t reported_loc
26426 = make_location (class_head_start_location
,
26427 class_head_start_location
,
26428 get_finish (type_start_token
->location
));
26429 rich_location
richloc (line_table
, reported_loc
);
26430 richloc
.add_fixit_insert_before (class_head_start_location
,
26432 error_at (&richloc
,
26433 "an explicit specialization must be preceded by"
26434 " %<template <>%>");
26435 invalid_explicit_specialization_p
= true;
26436 /* Take the same action that would have been taken by
26437 cp_parser_explicit_specialization. */
26438 ++parser
->num_template_parameter_lists
;
26439 begin_specialization ();
26441 /* There must be no "return" statements between this point and the
26442 end of this function; set "type "to the correct return value and
26443 use "goto done;" to return. */
26444 /* Make sure that the right number of template parameters were
26446 if (!cp_parser_check_template_parameters (parser
, num_templates
,
26448 type_start_token
->location
,
26449 /*declarator=*/NULL
))
26451 /* If something went wrong, there is no point in even trying to
26452 process the class-definition. */
26457 /* Look up the type. */
26460 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
26461 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
26462 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
26464 error_at (type_start_token
->location
,
26465 "function template %qD redeclared as a class template", id
);
26466 type
= error_mark_node
;
26470 type
= TREE_TYPE (id
);
26471 type
= maybe_process_partial_specialization (type
);
26473 /* Check the scope while we still know whether or not we had a
26474 nested-name-specifier. */
26475 if (type
!= error_mark_node
)
26476 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
26478 if (nested_name_specifier
)
26479 pushed_scope
= push_scope (nested_name_specifier
);
26481 else if (nested_name_specifier
)
26487 template <typename T> struct S { struct T };
26488 template <typename T> struct S<T>::T { };
26490 we will get a TYPENAME_TYPE when processing the definition of
26491 `S::T'. We need to resolve it to the actual type before we
26492 try to define it. */
26493 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
26495 class_type
= resolve_typename_type (TREE_TYPE (type
),
26496 /*only_current_p=*/false);
26497 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
26498 type
= TYPE_NAME (class_type
);
26501 cp_parser_error (parser
, "could not resolve typename type");
26502 type
= error_mark_node
;
26506 if (maybe_process_partial_specialization (TREE_TYPE (type
))
26507 == error_mark_node
)
26513 class_type
= current_class_type
;
26514 /* Enter the scope indicated by the nested-name-specifier. */
26515 pushed_scope
= push_scope (nested_name_specifier
);
26516 /* Get the canonical version of this type. */
26517 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
26518 /* Call push_template_decl if it seems like we should be defining a
26519 template either from the template headers or the type we're
26520 defining, so that we diagnose both extra and missing headers. */
26521 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26522 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
26523 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
26525 type
= push_template_decl (type
);
26526 if (type
== error_mark_node
)
26533 type
= TREE_TYPE (type
);
26534 *nested_name_specifier_p
= true;
26536 else /* The name is not a nested name. */
26538 /* If the class was unnamed, create a dummy name. */
26540 id
= make_anon_name ();
26541 TAG_how how
= (parser
->in_type_id_in_expr_p
26542 ? TAG_how::INNERMOST_NON_CLASS
26543 : TAG_how::CURRENT_ONLY
);
26544 type
= xref_tag (class_key
, id
, how
,
26545 parser
->num_template_parameter_lists
);
26548 /* Diagnose class/struct/union mismatches. */
26549 cp_parser_check_class_key (parser
, UNKNOWN_LOCATION
, class_key
, type
,
26552 /* Indicate whether this class was declared as a `class' or as a
26554 if (TREE_CODE (type
) == RECORD_TYPE
)
26555 CLASSTYPE_DECLARED_CLASS (type
) = class_key
== class_type
;
26557 /* If this type was already complete, and we see another definition,
26558 that's an error. Likewise if the type is already being defined:
26559 this can happen, eg, when it's defined from within an expression
26561 if (type
!= error_mark_node
26562 && (COMPLETE_TYPE_P (type
) || TYPE_BEING_DEFINED (type
)))
26564 error_at (type_start_token
->location
, "redefinition of %q#T",
26566 inform (location_of (type
), "previous definition of %q#T",
26571 else if (type
== error_mark_node
)
26576 /* Apply attributes now, before any use of the class as a template
26577 argument in its base list. */
26578 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
26579 fixup_attribute_variants (type
);
26582 /* Associate constraints with the type. */
26584 type
= associate_classtype_constraints (type
);
26586 /* We will have entered the scope containing the class; the names of
26587 base classes should be looked up in that context. For example:
26589 struct A { struct B {}; struct C; };
26590 struct A::C : B {};
26594 /* Get the list of base-classes, if there is one. Defer access checking
26595 until the entire list has been seen, as per [class.access.general]. */
26596 push_deferring_access_checks (dk_deferred
);
26597 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
26601 bases
= cp_parser_base_clause (parser
);
26608 /* If we're really defining a class, process the base classes.
26609 If they're invalid, fail. */
26610 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26611 xref_basetypes (type
, bases
);
26613 /* Now that all bases have been seen and attached to the class, check
26614 accessibility of the types named in the base-clause. This must be
26615 done relative to the class scope, so that we accept e.g.
26617 struct A { protected: struct B {}; };
26618 struct C : A::B, A {}; // OK: A::B is accessible via base A
26620 as per [class.access.general]. */
26623 pop_to_parent_deferring_access_checks ();
26628 /* Leave the scope given by the nested-name-specifier. We will
26629 enter the class scope itself while processing the members. */
26631 pop_scope (pushed_scope
);
26633 if (invalid_explicit_specialization_p
)
26635 end_specialization ();
26636 --parser
->num_template_parameter_lists
;
26640 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
26641 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
26642 CLASSTYPE_FINAL (type
) = 1;
26644 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
26648 /* Parse a class-key.
26655 Returns the kind of class-key specified, or none_type to indicate
26658 static enum tag_types
26659 cp_parser_class_key (cp_parser
* parser
)
26662 enum tag_types tag_type
;
26664 /* Look for the class-key. */
26665 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
26669 /* Check to see if the TOKEN is a class-key. */
26670 tag_type
= cp_parser_token_is_class_key (token
);
26672 cp_parser_error (parser
, "expected class-key");
26676 /* Parse a type-parameter-key.
26678 type-parameter-key:
26684 cp_parser_type_parameter_key (cp_parser
* parser
)
26686 /* Look for the type-parameter-key. */
26687 enum tag_types tag_type
= none_type
;
26688 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26689 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
26691 cp_lexer_consume_token (parser
->lexer
);
26692 if (pedantic
&& tag_type
== typename_type
26693 && cxx_dialect
< cxx17
)
26694 /* typename is not allowed in a template template parameter
26695 by the standard until C++17. */
26696 pedwarn (token
->location
, OPT_Wc__17_extensions
,
26697 "ISO C++ forbids typename key in template template parameter;"
26698 " use %<-std=c++17%> or %<-std=gnu++17%>");
26701 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
26706 /* Parse an (optional) member-specification.
26708 member-specification:
26709 member-declaration member-specification [opt]
26710 access-specifier : member-specification [opt] */
26713 cp_parser_member_specification_opt (cp_parser
* parser
)
26720 /* Peek at the next token. */
26721 token
= cp_lexer_peek_token (parser
->lexer
);
26722 /* If it's a `}', or EOF then we've seen all the members. */
26723 if (token
->type
== CPP_CLOSE_BRACE
26724 || token
->type
== CPP_EOF
26725 || token
->type
== CPP_PRAGMA_EOL
)
26728 /* See if this token is a keyword. */
26729 keyword
= token
->keyword
;
26733 case RID_PROTECTED
:
26735 /* Consume the access-specifier. */
26736 cp_lexer_consume_token (parser
->lexer
);
26737 /* Remember which access-specifier is active. */
26738 current_access_specifier
= token
->u
.value
;
26739 /* Look for the `:'. */
26740 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
26744 /* Accept #pragmas at class scope. */
26745 if (token
->type
== CPP_PRAGMA
)
26747 cp_parser_pragma (parser
, pragma_member
, NULL
);
26751 /* Otherwise, the next construction must be a
26752 member-declaration. */
26753 cp_parser_member_declaration (parser
);
26758 /* Parse a member-declaration.
26760 member-declaration:
26761 decl-specifier-seq [opt] member-declarator-list [opt] ;
26762 function-definition ; [opt]
26763 :: [opt] nested-name-specifier template [opt] unqualified-id ;
26765 template-declaration
26768 member-declarator-list:
26770 member-declarator-list , member-declarator
26773 declarator pure-specifier [opt]
26774 declarator constant-initializer [opt]
26775 identifier [opt] : constant-expression
26779 member-declaration:
26780 __extension__ member-declaration
26783 declarator attributes [opt] pure-specifier [opt]
26784 declarator attributes [opt] constant-initializer [opt]
26785 identifier [opt] attributes [opt] : constant-expression
26789 member-declaration:
26790 static_assert-declaration */
26793 cp_parser_member_declaration (cp_parser
* parser
)
26795 cp_decl_specifier_seq decl_specifiers
;
26796 tree prefix_attributes
;
26798 int declares_class_or_enum
;
26800 cp_token
*token
= NULL
;
26801 cp_token
*decl_spec_token_start
= NULL
;
26802 cp_token
*initializer_token_start
= NULL
;
26803 int saved_pedantic
;
26804 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
26806 /* Check for the `__extension__' keyword. */
26807 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
26810 cp_parser_member_declaration (parser
);
26811 /* Restore the old value of the PEDANTIC flag. */
26812 pedantic
= saved_pedantic
;
26817 /* Check for a template-declaration. */
26818 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26820 /* An explicit specialization here is an error condition, and we
26821 expect the specialization handler to detect and report this. */
26822 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
26823 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
26824 cp_parser_explicit_specialization (parser
);
26826 cp_parser_template_declaration (parser
, /*member_p=*/true);
26830 /* Check for a template introduction. */
26831 else if (cp_parser_template_declaration_after_export (parser
, true))
26834 /* Check for a using-declaration. */
26835 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26837 if (cxx_dialect
< cxx11
)
26838 /* Parse the using-declaration. */
26839 cp_parser_using_declaration (parser
, /*access_declaration_p=*/false);
26840 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_ENUM
))
26841 cp_parser_using_enum (parser
);
26845 bool alias_decl_expected
;
26846 cp_parser_parse_tentatively (parser
);
26847 decl
= cp_parser_alias_declaration (parser
);
26848 /* Note that if we actually see the '=' token after the
26849 identifier, cp_parser_alias_declaration commits the
26850 tentative parse. In that case, we really expect an
26851 alias-declaration. Otherwise, we expect a using
26853 alias_decl_expected
=
26854 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
26855 cp_parser_parse_definitely (parser
);
26857 if (alias_decl_expected
)
26858 finish_member_declaration (decl
);
26860 cp_parser_using_declaration (parser
,
26861 /*access_declaration_p=*/false);
26866 /* Check for @defs. */
26867 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
26870 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
26871 ivar
= ivar_chains
;
26875 ivar
= TREE_CHAIN (member
);
26876 TREE_CHAIN (member
) = NULL_TREE
;
26877 finish_member_declaration (member
);
26882 /* If the next token is `static_assert' we have a static assertion. */
26883 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
26885 cp_parser_static_assert (parser
, /*member_p=*/true);
26889 parser
->colon_corrects_to_scope_p
= false;
26891 cp_omp_declare_simd_data odsd
;
26892 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
26895 /* Parse the decl-specifier-seq. */
26896 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
26897 cp_parser_decl_specifier_seq (parser
,
26898 (CP_PARSER_FLAGS_OPTIONAL
26899 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
26901 &declares_class_or_enum
);
26903 if (decl_specifiers
.attributes
&& (flag_openmp
|| flag_openmp_simd
))
26904 cp_parser_handle_directive_omp_attributes (parser
,
26905 &decl_specifiers
.attributes
,
26908 /* Check for an invalid type-name. */
26909 if (!decl_specifiers
.any_type_specifiers_p
26910 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
26912 /* If there is no declarator, then the decl-specifier-seq should
26914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26916 /* If there was no decl-specifier-seq, and the next token is a
26917 `;', then we have something like:
26923 Each member-declaration shall declare at least one member
26924 name of the class. */
26925 if (!decl_specifiers
.any_specifiers_p
)
26927 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26928 if (!in_system_header_at (token
->location
))
26930 gcc_rich_location
richloc (token
->location
);
26931 richloc
.add_fixit_remove ();
26932 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
26937 /* See if this declaration is a friend. */
26938 friend_p
= cp_parser_friend_p (&decl_specifiers
);
26939 /* If there were decl-specifiers, check to see if there was
26940 a class-declaration. */
26941 tree type
= check_tag_decl (&decl_specifiers
,
26942 /*explicit_type_instantiation_p=*/false);
26943 /* Nested classes have already been added to the class, but
26944 a `friend' needs to be explicitly registered. */
26947 /* If the `friend' keyword was present, the friend must
26948 be introduced with a class-key. */
26949 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
26950 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
26951 "in C++03 a class-key must be used "
26952 "when declaring a friend");
26955 template <typename T> struct A {
26956 friend struct A<T>::B;
26959 A<T>::B will be represented by a TYPENAME_TYPE, and
26960 therefore not recognized by check_tag_decl. */
26963 type
= decl_specifiers
.type
;
26964 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
26965 type
= TREE_TYPE (type
);
26967 /* Warn if an attribute cannot appear here, as per
26968 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
26969 we ignore attributes in elaborated-type-specifiers. */
26970 if (!declares_class_or_enum
26971 && cxx11_attribute_p (decl_specifiers
.attributes
))
26973 decl_specifiers
.attributes
= NULL_TREE
;
26974 if (warning_at (decl_spec_token_start
->location
,
26975 OPT_Wattributes
, "attribute ignored"))
26976 inform (decl_spec_token_start
->location
, "an attribute "
26977 "that appertains to a friend declaration that "
26978 "is not a definition is ignored");
26980 if (!type
|| !TYPE_P (type
))
26981 error_at (decl_spec_token_start
->location
,
26982 "friend declaration does not name a class or "
26985 make_friend_class (current_class_type
, type
,
26986 /*complain=*/true);
26988 /* If there is no TYPE, an error message will already have
26990 else if (!type
|| type
== error_mark_node
)
26992 /* An anonymous aggregate has to be handled specially; such
26993 a declaration really declares a data member (with a
26994 particular type), as opposed to a nested class. */
26995 else if (ANON_AGGR_TYPE_P (type
))
26998 if (decl_specifiers
.storage_class
!= sc_none
)
26999 error_at (decl_spec_token_start
->location
,
27000 "a storage class on an anonymous aggregate "
27001 "in class scope is not allowed");
27003 /* Remove constructors and such from TYPE, now that we
27004 know it is an anonymous aggregate. */
27005 fixup_anonymous_aggr (type
);
27006 /* And make the corresponding data member. */
27007 decl
= build_decl (decl_spec_token_start
->location
,
27008 FIELD_DECL
, NULL_TREE
, type
);
27009 /* Add it to the class. */
27010 finish_member_declaration (decl
);
27013 cp_parser_check_access_in_redeclaration
27015 decl_spec_token_start
->location
);
27020 bool assume_semicolon
= false;
27022 /* Clear attributes from the decl_specifiers but keep them
27023 around as prefix attributes that apply them to the entity
27025 prefix_attributes
= decl_specifiers
.attributes
;
27026 decl_specifiers
.attributes
= NULL_TREE
;
27027 if (parser
->omp_declare_simd
27028 && (parser
->omp_declare_simd
->attribs
[0]
27029 == &decl_specifiers
.attributes
))
27030 parser
->omp_declare_simd
->attribs
[0] = &prefix_attributes
;
27032 /* See if these declarations will be friends. */
27033 friend_p
= cp_parser_friend_p (&decl_specifiers
);
27035 /* Keep going until we hit the `;' at the end of the
27037 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27039 tree attributes
= NULL_TREE
;
27040 tree first_attribute
;
27042 bool named_bitfld
= false;
27044 /* Peek at the next token. */
27045 token
= cp_lexer_peek_token (parser
->lexer
);
27047 /* The following code wants to know early if it is a bit-field
27048 or some other declaration. Attributes can appear before
27049 the `:' token. Skip over them without consuming any tokens
27050 to peek if they are followed by `:'. */
27051 if (cp_next_tokens_can_be_attribute_p (parser
)
27052 || (token
->type
== CPP_NAME
27053 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
27054 && (named_bitfld
= true)))
27057 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
27058 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
27061 /* Check for a bitfield declaration. */
27062 if (token
->type
== CPP_COLON
27063 || (token
->type
== CPP_NAME
27064 && token
== cp_lexer_peek_token (parser
->lexer
)
27065 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
27066 && (named_bitfld
= true)))
27070 tree late_attributes
= NULL_TREE
;
27071 location_t id_location
27072 = cp_lexer_peek_token (parser
->lexer
)->location
;
27075 identifier
= cp_parser_identifier (parser
);
27077 identifier
= NULL_TREE
;
27079 /* Look for attributes that apply to the bitfield. */
27080 attributes
= cp_parser_attributes_opt (parser
);
27082 /* Consume the `:' token. */
27083 cp_lexer_consume_token (parser
->lexer
);
27085 /* Get the width of the bitfield. */
27086 width
= cp_parser_constant_expression (parser
, false, NULL
,
27087 cxx_dialect
>= cxx11
);
27089 /* In C++20 and as extension for C++11 and above we allow
27090 default member initializers for bit-fields. */
27091 initializer
= NULL_TREE
;
27092 if (cxx_dialect
>= cxx11
27093 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
27094 || cp_lexer_next_token_is (parser
->lexer
,
27098 = cp_lexer_peek_token (parser
->lexer
)->location
;
27099 if (cxx_dialect
< cxx20
27100 && identifier
!= NULL_TREE
)
27101 pedwarn (loc
, OPT_Wc__20_extensions
,
27102 "default member initializers for bit-fields "
27103 "only available with %<-std=c++20%> or "
27104 "%<-std=gnu++20%>");
27106 initializer
= cp_parser_save_nsdmi (parser
);
27107 if (identifier
== NULL_TREE
)
27109 error_at (loc
, "default member initializer for "
27110 "unnamed bit-field");
27111 initializer
= NULL_TREE
;
27116 /* Look for attributes that apply to the bitfield after
27117 the `:' token and width. This is where GCC used to
27118 parse attributes in the past, pedwarn if there is
27119 a std attribute. */
27120 if (cp_next_tokens_can_be_std_attribute_p (parser
))
27121 pedwarn (input_location
, OPT_Wpedantic
,
27122 "ISO C++ allows bit-field attributes only "
27123 "before the %<:%> token");
27125 late_attributes
= cp_parser_attributes_opt (parser
);
27128 attributes
= attr_chainon (attributes
, late_attributes
);
27130 /* Remember which attributes are prefix attributes and
27132 first_attribute
= attributes
;
27133 /* Combine the attributes. */
27134 attributes
= attr_chainon (prefix_attributes
, attributes
);
27136 /* Create the bitfield declaration. */
27137 decl
= grokbitfield (identifier
27138 ? make_id_declarator (NULL_TREE
,
27144 width
, initializer
,
27149 cp_declarator
*declarator
;
27150 tree asm_specification
;
27151 int ctor_dtor_or_conv_p
;
27152 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
27153 cp_parser_flags flags
= CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
27154 /* We can't delay parsing for friends,
27155 alias-declarations, and typedefs, even though the
27156 standard seems to require it. */
27158 && !decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27159 flags
|= CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
27161 /* Parse the declarator. */
27163 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
27165 &ctor_dtor_or_conv_p
,
27166 /*parenthesized_p=*/NULL
,
27168 friend_p
, static_p
);
27170 /* If something went wrong parsing the declarator, make sure
27171 that we at least consume some tokens. */
27172 if (declarator
== cp_error_declarator
)
27174 /* Skip to the end of the statement. */
27175 cp_parser_skip_to_end_of_statement (parser
);
27176 /* If the next token is not a semicolon, that is
27177 probably because we just skipped over the body of
27178 a function. So, we consume a semicolon if
27179 present, but do not issue an error message if it
27181 if (cp_lexer_next_token_is (parser
->lexer
,
27183 cp_lexer_consume_token (parser
->lexer
);
27187 /* Handle class-scope non-template C++17 deduction guides. */
27188 cp_parser_maybe_adjust_declarator_for_dguide (parser
,
27191 &ctor_dtor_or_conv_p
);
27193 if (declares_class_or_enum
& 2)
27194 cp_parser_check_for_definition_in_return_type
27195 (declarator
, decl_specifiers
.type
,
27196 decl_specifiers
.locations
[ds_type_spec
]);
27198 /* Look for an asm-specification. */
27199 asm_specification
= cp_parser_asm_specification_opt (parser
);
27200 /* Look for attributes that apply to the declaration. */
27201 attributes
= cp_parser_attributes_opt (parser
);
27202 /* Remember which attributes are prefix attributes and
27204 first_attribute
= attributes
;
27205 /* Combine the attributes. */
27206 attributes
= attr_chainon (prefix_attributes
, attributes
);
27208 /* If it's an `=', then we have a constant-initializer or a
27209 pure-specifier. It is not correct to parse the
27210 initializer before registering the member declaration
27211 since the member declaration should be in scope while
27212 its initializer is processed. However, the rest of the
27213 front end does not yet provide an interface that allows
27214 us to handle this correctly. */
27215 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
27219 A pure-specifier shall be used only in the declaration of
27220 a virtual function.
27222 A member-declarator can contain a constant-initializer
27223 only if it declares a static member of integral or
27226 Therefore, if the DECLARATOR is for a function, we look
27227 for a pure-specifier; otherwise, we look for a
27228 constant-initializer. When we call `grokfield', it will
27229 perform more stringent semantics checks. */
27230 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
27231 declarator
->init_loc
= initializer_token_start
->location
;
27232 if (function_declarator_p (declarator
)
27233 || (decl_specifiers
.type
27234 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
27235 && declarator
->kind
== cdk_id
27236 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
27237 == FUNCTION_TYPE
)))
27238 initializer
= cp_parser_pure_specifier (parser
);
27239 else if (decl_specifiers
.storage_class
!= sc_static
)
27240 initializer
= cp_parser_save_nsdmi (parser
);
27241 else if (cxx_dialect
>= cxx11
)
27244 /* Don't require a constant rvalue in C++11, since we
27245 might want a reference constant. We'll enforce
27246 constancy later. */
27247 cp_lexer_consume_token (parser
->lexer
);
27248 /* Parse the initializer. */
27249 initializer
= cp_parser_initializer_clause (parser
,
27253 /* Parse the initializer. */
27254 initializer
= cp_parser_constant_initializer (parser
);
27256 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
27257 && !function_declarator_p (declarator
))
27260 declarator
->init_loc
27261 = cp_lexer_peek_token (parser
->lexer
)->location
;
27262 if (decl_specifiers
.storage_class
!= sc_static
)
27263 initializer
= cp_parser_save_nsdmi (parser
);
27265 initializer
= cp_parser_initializer (parser
, &x
, &x
);
27267 /* Detect invalid bit-field cases such as
27273 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
27274 /* If there were no type specifiers, it was a
27276 && decl_specifiers
.any_type_specifiers_p
)
27278 /* This is called for a decent diagnostic only. */
27279 tree d
= grokdeclarator (declarator
, &decl_specifiers
,
27280 BITFIELD
, /*initialized=*/false,
27282 if (!error_operand_p (d
))
27283 error_at (DECL_SOURCE_LOCATION (d
),
27284 "bit-field %qD has non-integral type %qT",
27286 cp_parser_skip_to_end_of_statement (parser
);
27287 /* Avoid "extra ;" pedwarns. */
27288 if (cp_lexer_next_token_is (parser
->lexer
,
27290 cp_lexer_consume_token (parser
->lexer
);
27293 /* Otherwise, there is no initializer. */
27295 initializer
= NULL_TREE
;
27297 /* See if we are probably looking at a function
27298 definition. We are certainly not looking at a
27299 member-declarator. Calling `grokfield' has
27300 side-effects, so we must not do it unless we are sure
27301 that we are looking at a member-declarator. */
27302 if (cp_parser_token_starts_function_definition_p
27303 (cp_lexer_peek_token (parser
->lexer
)))
27305 /* The grammar does not allow a pure-specifier to be
27306 used when a member function is defined. (It is
27307 possible that this fact is an oversight in the
27308 standard, since a pure function may be defined
27309 outside of the class-specifier. */
27310 if (initializer
&& initializer_token_start
)
27311 error_at (initializer_token_start
->location
,
27312 "pure-specifier on function-definition");
27313 decl
= cp_parser_save_member_function_body (parser
,
27317 if (parser
->fully_implicit_function_template_p
)
27318 decl
= finish_fully_implicit_template (parser
, decl
);
27319 /* If the member was not a friend, declare it here. */
27321 finish_member_declaration (decl
);
27322 /* Peek at the next token. */
27323 token
= cp_lexer_peek_token (parser
->lexer
);
27324 /* If the next token is a semicolon, consume it. */
27325 if (token
->type
== CPP_SEMICOLON
)
27327 location_t semicolon_loc
27328 = cp_lexer_consume_token (parser
->lexer
)->location
;
27329 gcc_rich_location
richloc (semicolon_loc
);
27330 richloc
.add_fixit_remove ();
27331 warning_at (&richloc
, OPT_Wextra_semi
,
27332 "extra %<;%> after in-class "
27333 "function definition");
27338 if (declarator
->kind
== cdk_function
)
27339 declarator
->id_loc
= token
->location
;
27340 /* Create the declaration. */
27341 decl
= grokfield (declarator
, &decl_specifiers
,
27342 initializer
, /*init_const_expr_p=*/true,
27343 asm_specification
, attributes
);
27344 if (parser
->fully_implicit_function_template_p
)
27347 finish_fully_implicit_template (parser
, 0);
27349 decl
= finish_fully_implicit_template (parser
, decl
);
27353 cp_finalize_omp_declare_simd (parser
, decl
);
27354 cp_finalize_oacc_routine (parser
, decl
, false);
27356 /* Reset PREFIX_ATTRIBUTES. */
27357 if (attributes
!= error_mark_node
)
27359 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
27360 attributes
= TREE_CHAIN (attributes
);
27362 TREE_CHAIN (attributes
) = NULL_TREE
;
27365 /* If there is any qualification still in effect, clear it
27366 now; we will be starting fresh with the next declarator. */
27367 parser
->scope
= NULL_TREE
;
27368 parser
->qualifying_scope
= NULL_TREE
;
27369 parser
->object_scope
= NULL_TREE
;
27370 /* If it's a `,', then there are more declarators. */
27371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27373 cp_lexer_consume_token (parser
->lexer
);
27374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27376 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
27377 gcc_rich_location
richloc (token
->location
);
27378 richloc
.add_fixit_remove ();
27379 error_at (&richloc
, "stray %<,%> at end of "
27380 "member declaration");
27383 /* If the next token isn't a `;', then we have a parse error. */
27384 else if (cp_lexer_next_token_is_not (parser
->lexer
,
27387 /* The next token might be a ways away from where the
27388 actual semicolon is missing. Find the previous token
27389 and use that for our error position. */
27390 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
27391 gcc_rich_location
richloc (token
->location
);
27392 richloc
.add_fixit_insert_after (";");
27393 error_at (&richloc
, "expected %<;%> at end of "
27394 "member declaration");
27396 /* Assume that the user meant to provide a semicolon. If
27397 we were to cp_parser_skip_to_end_of_statement, we might
27398 skip to a semicolon inside a member function definition
27399 and issue nonsensical error messages. */
27400 assume_semicolon
= true;
27405 /* Add DECL to the list of members. */
27407 /* Explicitly include, eg, NSDMIs, for better error
27408 recovery (c++/58650). */
27409 || !DECL_DECLARES_FUNCTION_P (decl
))
27410 finish_member_declaration (decl
);
27412 if (DECL_DECLARES_FUNCTION_P (decl
))
27413 cp_parser_save_default_args (parser
, STRIP_TEMPLATE (decl
));
27414 else if (TREE_CODE (decl
) == FIELD_DECL
27415 && DECL_INITIAL (decl
))
27416 /* Add DECL to the queue of NSDMI to be parsed later. */
27417 vec_safe_push (unparsed_nsdmis
, decl
);
27420 if (assume_semicolon
)
27425 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27427 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27428 cp_finalize_omp_declare_simd (parser
, &odsd
);
27431 /* Parse a pure-specifier.
27436 Returns INTEGER_ZERO_NODE if a pure specifier is found.
27437 Otherwise, ERROR_MARK_NODE is returned. */
27440 cp_parser_pure_specifier (cp_parser
* parser
)
27444 /* Look for the `=' token. */
27445 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
27446 return error_mark_node
;
27447 /* Look for the `0' token. */
27448 token
= cp_lexer_peek_token (parser
->lexer
);
27450 if (token
->type
== CPP_EOF
27451 || token
->type
== CPP_PRAGMA_EOL
)
27452 return error_mark_node
;
27454 cp_lexer_consume_token (parser
->lexer
);
27456 /* Accept = default or = delete in c++0x mode. */
27457 if (token
->keyword
== RID_DEFAULT
27458 || token
->keyword
== RID_DELETE
)
27460 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
27461 return token
->u
.value
;
27464 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
27465 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
27467 cp_parser_error (parser
,
27468 "invalid pure specifier (only %<= 0%> is allowed)");
27469 cp_parser_skip_to_end_of_statement (parser
);
27470 return error_mark_node
;
27472 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27474 error_at (token
->location
, "templates may not be %<virtual%>");
27475 return error_mark_node
;
27478 return integer_zero_node
;
27481 /* Parse a constant-initializer.
27483 constant-initializer:
27484 = constant-expression
27486 Returns a representation of the constant-expression. */
27489 cp_parser_constant_initializer (cp_parser
* parser
)
27491 /* Look for the `=' token. */
27492 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
27493 return error_mark_node
;
27495 /* It is invalid to write:
27497 struct S { static const int i = { 7 }; };
27500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27502 cp_parser_error (parser
,
27503 "a brace-enclosed initializer is not allowed here");
27504 /* Consume the opening brace. */
27505 matching_braces braces
;
27506 braces
.consume_open (parser
);
27507 /* Skip the initializer. */
27508 cp_parser_skip_to_closing_brace (parser
);
27509 /* Look for the trailing `}'. */
27510 braces
.require_close (parser
);
27512 return error_mark_node
;
27515 return cp_parser_constant_expression (parser
);
27518 /* Derived classes [gram.class.derived] */
27520 /* Parse a base-clause.
27523 : base-specifier-list
27525 base-specifier-list:
27526 base-specifier ... [opt]
27527 base-specifier-list , base-specifier ... [opt]
27529 Returns a TREE_LIST representing the base-classes, in the order in
27530 which they were declared. The representation of each node is as
27531 described by cp_parser_base_specifier.
27533 In the case that no bases are specified, this function will return
27534 NULL_TREE, not ERROR_MARK_NODE. */
27537 cp_parser_base_clause (cp_parser
* parser
)
27539 tree bases
= NULL_TREE
;
27541 /* Look for the `:' that begins the list. */
27542 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27544 /* Scan the base-specifier-list. */
27549 bool pack_expansion_p
= false;
27551 /* Look for the base-specifier. */
27552 base
= cp_parser_base_specifier (parser
);
27553 /* Look for the (optional) ellipsis. */
27554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27556 /* Consume the `...'. */
27557 cp_lexer_consume_token (parser
->lexer
);
27559 pack_expansion_p
= true;
27562 /* Add BASE to the front of the list. */
27563 if (base
&& base
!= error_mark_node
)
27565 if (pack_expansion_p
)
27566 /* Make this a pack expansion type. */
27567 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
27569 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
27571 TREE_CHAIN (base
) = bases
;
27575 /* Peek at the next token. */
27576 token
= cp_lexer_peek_token (parser
->lexer
);
27577 /* If it's not a comma, then the list is complete. */
27578 if (token
->type
!= CPP_COMMA
)
27580 /* Consume the `,'. */
27581 cp_lexer_consume_token (parser
->lexer
);
27584 /* PARSER->SCOPE may still be non-NULL at this point, if the last
27585 base class had a qualified name. However, the next name that
27586 appears is certainly not qualified. */
27587 parser
->scope
= NULL_TREE
;
27588 parser
->qualifying_scope
= NULL_TREE
;
27589 parser
->object_scope
= NULL_TREE
;
27591 return nreverse (bases
);
27594 /* Parse a base-specifier.
27597 :: [opt] nested-name-specifier [opt] class-name
27598 virtual access-specifier [opt] :: [opt] nested-name-specifier
27600 access-specifier virtual [opt] :: [opt] nested-name-specifier
27603 Returns a TREE_LIST. The TREE_PURPOSE will be one of
27604 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
27605 indicate the specifiers provided. The TREE_VALUE will be a TYPE
27606 (or the ERROR_MARK_NODE) indicating the type that was specified. */
27609 cp_parser_base_specifier (cp_parser
* parser
)
27613 bool virtual_p
= false;
27614 bool duplicate_virtual_error_issued_p
= false;
27615 bool duplicate_access_error_issued_p
= false;
27616 bool class_scope_p
, template_p
;
27617 tree access
= access_default_node
;
27620 /* Process the optional `virtual' and `access-specifier'. */
27623 /* Peek at the next token. */
27624 token
= cp_lexer_peek_token (parser
->lexer
);
27625 /* Process `virtual'. */
27626 switch (token
->keyword
)
27629 /* If `virtual' appears more than once, issue an error. */
27630 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
27632 cp_parser_error (parser
,
27633 "%<virtual%> specified more than once in base-specifier");
27634 duplicate_virtual_error_issued_p
= true;
27639 /* Consume the `virtual' token. */
27640 cp_lexer_consume_token (parser
->lexer
);
27645 case RID_PROTECTED
:
27647 /* If more than one access specifier appears, issue an
27649 if (access
!= access_default_node
27650 && !duplicate_access_error_issued_p
)
27652 cp_parser_error (parser
,
27653 "more than one access specifier in base-specifier");
27654 duplicate_access_error_issued_p
= true;
27657 access
= ridpointers
[(int) token
->keyword
];
27659 /* Consume the access-specifier. */
27660 cp_lexer_consume_token (parser
->lexer
);
27669 /* It is not uncommon to see programs mechanically, erroneously, use
27670 the 'typename' keyword to denote (dependent) qualified types
27671 as base classes. */
27672 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
27674 token
= cp_lexer_peek_token (parser
->lexer
);
27675 if (!processing_template_decl
)
27676 error_at (token
->location
,
27677 "keyword %<typename%> not allowed outside of templates");
27679 error_at (token
->location
,
27680 "keyword %<typename%> not allowed in this context "
27681 "(the base class is implicitly a type)");
27682 cp_lexer_consume_token (parser
->lexer
);
27685 /* Look for the optional `::' operator. */
27686 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
27687 /* Look for the nested-name-specifier. The simplest way to
27692 The keyword `typename' is not permitted in a base-specifier or
27693 mem-initializer; in these contexts a qualified name that
27694 depends on a template-parameter is implicitly assumed to be a
27697 is to pretend that we have seen the `typename' keyword at this
27699 cp_parser_nested_name_specifier_opt (parser
,
27700 /*typename_keyword_p=*/true,
27701 /*check_dependency_p=*/true,
27703 /*is_declaration=*/true);
27704 /* If the base class is given by a qualified name, assume that names
27705 we see are type names or templates, as appropriate. */
27706 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
27707 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
27710 && cp_lexer_next_token_is_decltype (parser
->lexer
))
27711 /* DR 950 allows decltype as a base-specifier. */
27712 type
= cp_parser_decltype (parser
);
27715 /* Otherwise, look for the class-name. */
27716 type
= cp_parser_class_name (parser
,
27720 /*check_dependency_p=*/true,
27721 /*class_head_p=*/false,
27722 /*is_declaration=*/true);
27723 type
= TREE_TYPE (type
);
27726 if (type
== error_mark_node
)
27727 return error_mark_node
;
27729 return finish_base_specifier (type
, access
, virtual_p
);
27732 /* Exception handling [gram.exception] */
27734 /* Save the tokens that make up the noexcept-specifier for a member-function.
27735 Returns a DEFERRED_PARSE. */
27738 cp_parser_save_noexcept (cp_parser
*parser
)
27740 cp_token
*first
= parser
->lexer
->next_token
;
27741 /* We want everything up to, including, the final ')'. */
27742 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0);
27743 cp_token
*last
= parser
->lexer
->next_token
;
27745 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
27746 to carry the information we will need. */
27747 tree expr
= make_node (DEFERRED_PARSE
);
27748 /* Save away the noexcept-specifier; we will process it when the
27749 class is complete. */
27750 DEFPARSE_TOKENS (expr
) = cp_token_cache_new (first
, last
);
27751 DEFPARSE_INSTANTIATIONS (expr
) = nullptr;
27752 expr
= build_tree_list (expr
, NULL_TREE
);
27756 /* Used for late processing of noexcept-specifiers of member-functions.
27757 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
27758 we saved for later; parse it now. DECL is the declaration of the
27759 member function. */
27762 cp_parser_late_noexcept_specifier (cp_parser
*parser
, tree default_arg
)
27764 /* Make sure we've gotten something that hasn't been parsed yet. */
27765 gcc_assert (TREE_CODE (default_arg
) == DEFERRED_PARSE
);
27767 push_unparsed_function_queues (parser
);
27769 /* Push the saved tokens for the noexcept-specifier onto the parser's
27771 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_arg
);
27772 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27774 /* Parse the cached noexcept-specifier. */
27776 = cp_parser_noexcept_specification_opt (parser
,
27777 CP_PARSER_FLAGS_NONE
,
27778 /*require_constexpr=*/true,
27779 /*consumed_expr=*/NULL
,
27780 /*return_cond=*/false);
27782 /* Revert to the main lexer. */
27783 cp_parser_pop_lexer (parser
);
27785 /* Restore the queue. */
27786 pop_unparsed_function_queues (parser
);
27788 /* And we're done. */
27792 /* Perform late checking of overriding function with respect to their
27793 noexcept-specifiers. TYPE is the class and FNDECL is the function
27794 that potentially overrides some virtual function with the same
27798 noexcept_override_late_checks (tree type
, tree fndecl
)
27800 tree binfo
= TYPE_BINFO (type
);
27803 if (DECL_STATIC_FUNCTION_P (fndecl
))
27806 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
27808 tree basetype
= BINFO_TYPE (base_binfo
);
27810 if (!TYPE_POLYMORPHIC_P (basetype
))
27813 tree fn
= look_for_overrides_here (basetype
, fndecl
);
27815 maybe_check_overriding_exception_spec (fndecl
, fn
);
27819 /* Parse an (optional) noexcept-specification.
27821 noexcept-specification:
27822 noexcept ( constant-expression ) [opt]
27824 If no noexcept-specification is present, returns NULL_TREE.
27825 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
27826 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
27827 there are no parentheses. CONSUMED_EXPR will be set accordingly.
27828 Otherwise, returns a noexcept specification unless RETURN_COND is true,
27829 in which case a boolean condition is returned instead. The parser flags
27830 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
27831 the (member) function is `const'. */
27834 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
27835 cp_parser_flags flags
,
27836 bool require_constexpr
,
27837 bool* consumed_expr
,
27841 const char *saved_message
;
27843 /* Peek at the next token. */
27844 token
= cp_lexer_peek_token (parser
->lexer
);
27846 /* Is it a noexcept-specification? */
27847 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
27851 /* [class.mem]/6 says that a noexcept-specifer (within the
27852 member-specification of the class) is a complete-class context of
27853 a class. So, if the noexcept-specifier has the optional expression,
27854 just save the tokens, and reparse this after we're done with the
27857 if ((flags
& CP_PARSER_FLAGS_DELAY_NOEXCEPT
)
27858 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
)
27859 /* No need to delay parsing for a number literal or true/false. */
27860 && !((cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NUMBER
)
27861 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27862 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_PAREN
))
27863 && at_class_scope_p ()
27864 && TYPE_BEING_DEFINED (current_class_type
)
27865 && !LAMBDA_TYPE_P (current_class_type
))
27866 return cp_parser_save_noexcept (parser
);
27868 cp_lexer_consume_token (parser
->lexer
);
27870 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
27872 matching_parens parens
;
27873 parens
.consume_open (parser
);
27875 if (require_constexpr
)
27877 /* Types may not be defined in an exception-specification. */
27878 saved_message
= parser
->type_definition_forbidden_message
;
27879 parser
->type_definition_forbidden_message
27880 = G_("types may not be defined in an exception-specification");
27882 bool non_constant_p
;
27884 = cp_parser_constant_expression (parser
,
27885 /*allow_non_constant=*/true,
27888 && !require_potential_rvalue_constant_expression (expr
))
27891 return_cond
= true;
27894 /* Restore the saved message. */
27895 parser
->type_definition_forbidden_message
= saved_message
;
27899 expr
= cp_parser_expression (parser
);
27900 *consumed_expr
= true;
27903 parens
.require_close (parser
);
27907 expr
= boolean_true_node
;
27908 if (!require_constexpr
)
27909 *consumed_expr
= false;
27912 /* We cannot build a noexcept-spec right away because this will check
27913 that expr is a constexpr. */
27915 return build_noexcept_spec (expr
, tf_warning_or_error
);
27923 /* Parse an (optional) exception-specification.
27925 exception-specification:
27926 throw ( type-id-list [opt] )
27928 Returns a TREE_LIST representing the exception-specification. The
27929 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
27930 control parsing. QUALS are qualifiers indicating whether the (member)
27931 function is `const'. */
27934 cp_parser_exception_specification_opt (cp_parser
* parser
,
27935 cp_parser_flags flags
)
27939 const char *saved_message
;
27941 /* Peek at the next token. */
27942 token
= cp_lexer_peek_token (parser
->lexer
);
27944 /* Is it a noexcept-specification? */
27946 = cp_parser_noexcept_specification_opt (parser
, flags
,
27947 /*require_constexpr=*/true,
27948 /*consumed_expr=*/NULL
,
27949 /*return_cond=*/false);
27950 if (type_id_list
!= NULL_TREE
)
27951 return type_id_list
;
27953 /* If it's not `throw', then there's no exception-specification. */
27954 if (!cp_parser_is_keyword (token
, RID_THROW
))
27957 location_t loc
= token
->location
;
27959 /* Consume the `throw'. */
27960 cp_lexer_consume_token (parser
->lexer
);
27962 /* Look for the `('. */
27963 matching_parens parens
;
27964 parens
.require_open (parser
);
27966 /* Peek at the next token. */
27967 token
= cp_lexer_peek_token (parser
->lexer
);
27968 /* If it's not a `)', then there is a type-id-list. */
27969 if (token
->type
!= CPP_CLOSE_PAREN
)
27971 /* Types may not be defined in an exception-specification. */
27972 saved_message
= parser
->type_definition_forbidden_message
;
27973 parser
->type_definition_forbidden_message
27974 = G_("types may not be defined in an exception-specification");
27975 /* Parse the type-id-list. */
27976 type_id_list
= cp_parser_type_id_list (parser
);
27977 /* Restore the saved message. */
27978 parser
->type_definition_forbidden_message
= saved_message
;
27980 if (cxx_dialect
>= cxx17
)
27982 error_at (loc
, "ISO C++17 does not allow dynamic exception "
27984 type_id_list
= NULL_TREE
;
27986 else if (cxx_dialect
>= cxx11
)
27987 warning_at (loc
, OPT_Wdeprecated
,
27988 "dynamic exception specifications are deprecated in "
27991 /* In C++17, throw() is equivalent to noexcept (true). throw()
27992 is deprecated in C++11 and above as well, but is still widely used,
27993 so don't warn about it yet. */
27994 else if (cxx_dialect
>= cxx17
)
27995 type_id_list
= noexcept_true_spec
;
27997 type_id_list
= empty_except_spec
;
27999 /* Look for the `)'. */
28000 parens
.require_close (parser
);
28002 return type_id_list
;
28005 /* Parse an (optional) type-id-list.
28009 type-id-list , type-id ... [opt]
28011 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28012 in the order that the types were presented. */
28015 cp_parser_type_id_list (cp_parser
* parser
)
28017 tree types
= NULL_TREE
;
28024 token
= cp_lexer_peek_token (parser
->lexer
);
28026 /* Get the next type-id. */
28027 type
= cp_parser_type_id (parser
);
28028 /* Check for invalid 'auto'. */
28029 if (flag_concepts
&& type_uses_auto (type
))
28031 error_at (token
->location
,
28032 "invalid use of %<auto%> in exception-specification");
28033 type
= error_mark_node
;
28035 /* Parse the optional ellipsis. */
28036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28038 /* Consume the `...'. */
28039 cp_lexer_consume_token (parser
->lexer
);
28041 /* Turn the type into a pack expansion expression. */
28042 type
= make_pack_expansion (type
);
28044 /* Add it to the list. */
28045 types
= add_exception_specifier (types
, type
, /*complain=*/1);
28046 /* Peek at the next token. */
28047 token
= cp_lexer_peek_token (parser
->lexer
);
28048 /* If it is not a `,', we are done. */
28049 if (token
->type
!= CPP_COMMA
)
28051 /* Consume the `,'. */
28052 cp_lexer_consume_token (parser
->lexer
);
28055 return nreverse (types
);
28058 /* Parse a try-block.
28061 try compound-statement handler-seq */
28064 cp_parser_try_block (cp_parser
* parser
)
28068 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
28069 if (parser
->in_function_body
28070 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
28071 && cxx_dialect
< cxx20
)
28072 pedwarn (input_location
, OPT_Wc__20_extensions
,
28073 "%<try%> in %<constexpr%> function only "
28074 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28076 try_block
= begin_try_block ();
28077 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
28078 finish_try_block (try_block
);
28079 cp_parser_handler_seq (parser
);
28080 finish_handler_sequence (try_block
);
28085 /* Parse a function-try-block.
28087 function-try-block:
28088 try ctor-initializer [opt] function-body handler-seq */
28091 cp_parser_function_try_block (cp_parser
* parser
)
28093 tree compound_stmt
;
28096 /* Look for the `try' keyword. */
28097 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
28099 /* Let the rest of the front end know where we are. */
28100 try_block
= begin_function_try_block (&compound_stmt
);
28101 /* Parse the function-body. */
28102 cp_parser_ctor_initializer_opt_and_function_body
28103 (parser
, /*in_function_try_block=*/true);
28104 /* We're done with the `try' part. */
28105 finish_function_try_block (try_block
);
28106 /* Parse the handlers. */
28107 cp_parser_handler_seq (parser
);
28108 /* We're done with the handlers. */
28109 finish_function_handler_sequence (try_block
, compound_stmt
);
28112 /* Parse a handler-seq.
28115 handler handler-seq [opt] */
28118 cp_parser_handler_seq (cp_parser
* parser
)
28124 /* Parse the handler. */
28125 cp_parser_handler (parser
);
28126 /* Peek at the next token. */
28127 token
= cp_lexer_peek_token (parser
->lexer
);
28128 /* If it's not `catch' then there are no more handlers. */
28129 if (!cp_parser_is_keyword (token
, RID_CATCH
))
28134 /* Parse a handler.
28137 catch ( exception-declaration ) compound-statement */
28140 cp_parser_handler (cp_parser
* parser
)
28145 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
28146 handler
= begin_handler ();
28147 matching_parens parens
;
28148 parens
.require_open (parser
);
28149 declaration
= cp_parser_exception_declaration (parser
);
28150 finish_handler_parms (declaration
, handler
);
28151 parens
.require_close (parser
);
28152 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
28153 finish_handler (handler
);
28156 /* Parse an exception-declaration.
28158 exception-declaration:
28159 type-specifier-seq declarator
28160 type-specifier-seq abstract-declarator
28164 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28165 ellipsis variant is used. */
28168 cp_parser_exception_declaration (cp_parser
* parser
)
28170 cp_decl_specifier_seq type_specifiers
;
28171 cp_declarator
*declarator
;
28172 const char *saved_message
;
28174 /* If it's an ellipsis, it's easy to handle. */
28175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28177 /* Consume the `...' token. */
28178 cp_lexer_consume_token (parser
->lexer
);
28182 /* Types may not be defined in exception-declarations. */
28183 saved_message
= parser
->type_definition_forbidden_message
;
28184 parser
->type_definition_forbidden_message
28185 = G_("types may not be defined in exception-declarations");
28187 /* Parse the type-specifier-seq. */
28188 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
28189 /*is_declaration=*/true,
28190 /*is_trailing_return=*/false,
28192 /* If it's a `)', then there is no declarator. */
28193 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
28196 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
28197 CP_PARSER_FLAGS_NONE
,
28198 /*ctor_dtor_or_conv_p=*/NULL
,
28199 /*parenthesized_p=*/NULL
,
28200 /*member_p=*/false,
28201 /*friend_p=*/false,
28202 /*static_p=*/false);
28204 /* Restore the saved message. */
28205 parser
->type_definition_forbidden_message
= saved_message
;
28207 if (!type_specifiers
.any_specifiers_p
)
28208 return error_mark_node
;
28210 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
28213 /* Parse a throw-expression.
28216 throw assignment-expression [opt]
28218 Returns a THROW_EXPR representing the throw-expression. */
28221 cp_parser_throw_expression (cp_parser
* parser
)
28225 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28227 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
28228 token
= cp_lexer_peek_token (parser
->lexer
);
28229 /* Figure out whether or not there is an assignment-expression
28230 following the "throw" keyword. */
28231 if (token
->type
== CPP_COMMA
28232 || token
->type
== CPP_SEMICOLON
28233 || token
->type
== CPP_CLOSE_PAREN
28234 || token
->type
== CPP_CLOSE_SQUARE
28235 || token
->type
== CPP_CLOSE_BRACE
28236 || token
->type
== CPP_COLON
)
28237 expression
= NULL_TREE
;
28239 expression
= cp_parser_assignment_expression (parser
);
28241 /* Construct a location e.g.:
28244 with caret == start at the start of the "throw" token, and
28245 the end at the end of the final token we consumed. */
28246 location_t combined_loc
= make_location (start_loc
, start_loc
,
28248 expression
= build_throw (combined_loc
, expression
);
28253 /* Parse a yield-expression.
28256 co_yield assignment-expression
28257 co_yield braced-init-list
28259 Returns a CO_YIELD_EXPR representing the yield-expression. */
28262 cp_parser_yield_expression (cp_parser
* parser
)
28266 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28267 location_t kw_loc
= token
->location
; /* Save for later. */
28269 cp_parser_require_keyword (parser
, RID_CO_YIELD
, RT_CO_YIELD
);
28271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28273 bool expr_non_constant_p
;
28274 cp_lexer_set_source_position (parser
->lexer
);
28275 /* ??? : probably a moot point? */
28276 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28277 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
28280 expr
= cp_parser_assignment_expression (parser
);
28282 if (expr
== error_mark_node
)
28285 return finish_co_yield_expr (kw_loc
, expr
);
28288 /* GNU Extensions */
28290 /* Parse an (optional) asm-specification.
28293 asm ( string-literal )
28295 If the asm-specification is present, returns a STRING_CST
28296 corresponding to the string-literal. Otherwise, returns
28300 cp_parser_asm_specification_opt (cp_parser
* parser
)
28303 tree asm_specification
;
28305 /* Peek at the next token. */
28306 token
= cp_lexer_peek_token (parser
->lexer
);
28307 /* If the next token isn't the `asm' keyword, then there's no
28308 asm-specification. */
28309 if (!cp_parser_is_keyword (token
, RID_ASM
))
28312 /* Consume the `asm' token. */
28313 cp_lexer_consume_token (parser
->lexer
);
28314 /* Look for the `('. */
28315 matching_parens parens
;
28316 parens
.require_open (parser
);
28318 /* Look for the string-literal. */
28319 asm_specification
= cp_parser_string_literal (parser
, false, false);
28321 /* Look for the `)'. */
28322 parens
.require_close (parser
);
28324 return asm_specification
;
28327 /* Parse an asm-operand-list.
28331 asm-operand-list , asm-operand
28334 string-literal ( expression )
28335 [ string-literal ] string-literal ( expression )
28337 Returns a TREE_LIST representing the operands. The TREE_VALUE of
28338 each node is the expression. The TREE_PURPOSE is itself a
28339 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28340 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28341 is a STRING_CST for the string literal before the parenthesis. Returns
28342 ERROR_MARK_NODE if any of the operands are invalid. */
28345 cp_parser_asm_operand_list (cp_parser
* parser
)
28347 tree asm_operands
= NULL_TREE
;
28348 bool invalid_operands
= false;
28352 tree string_literal
;
28356 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
28358 /* Consume the `[' token. */
28359 cp_lexer_consume_token (parser
->lexer
);
28360 /* Read the operand name. */
28361 name
= cp_parser_identifier (parser
);
28362 if (name
!= error_mark_node
)
28363 name
= build_string (IDENTIFIER_LENGTH (name
),
28364 IDENTIFIER_POINTER (name
));
28365 /* Look for the closing `]'. */
28366 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
28370 /* Look for the string-literal. */
28371 string_literal
= cp_parser_string_literal (parser
, false, false);
28373 /* Look for the `('. */
28374 matching_parens parens
;
28375 parens
.require_open (parser
);
28376 /* Parse the expression. */
28377 expression
= cp_parser_expression (parser
);
28378 /* Look for the `)'. */
28379 parens
.require_close (parser
);
28381 if (name
== error_mark_node
28382 || string_literal
== error_mark_node
28383 || expression
== error_mark_node
)
28384 invalid_operands
= true;
28386 /* Add this operand to the list. */
28387 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
28390 /* If the next token is not a `,', there are no more
28392 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
28394 /* Consume the `,'. */
28395 cp_lexer_consume_token (parser
->lexer
);
28398 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
28401 /* Parse an asm-clobber-list.
28405 asm-clobber-list , string-literal
28407 Returns a TREE_LIST, indicating the clobbers in the order that they
28408 appeared. The TREE_VALUE of each node is a STRING_CST. */
28411 cp_parser_asm_clobber_list (cp_parser
* parser
)
28413 tree clobbers
= NULL_TREE
;
28417 tree string_literal
;
28419 /* Look for the string literal. */
28420 string_literal
= cp_parser_string_literal (parser
, false, false);
28421 /* Add it to the list. */
28422 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
28423 /* If the next token is not a `,', then the list is
28425 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
28427 /* Consume the `,' token. */
28428 cp_lexer_consume_token (parser
->lexer
);
28434 /* Parse an asm-label-list.
28438 asm-label-list , identifier
28440 Returns a TREE_LIST, indicating the labels in the order that they
28441 appeared. The TREE_VALUE of each node is a label. */
28444 cp_parser_asm_label_list (cp_parser
* parser
)
28446 tree labels
= NULL_TREE
;
28450 tree identifier
, label
, name
;
28452 /* Look for the identifier. */
28453 identifier
= cp_parser_identifier (parser
);
28454 if (!error_operand_p (identifier
))
28456 label
= lookup_label (identifier
);
28457 if (TREE_CODE (label
) == LABEL_DECL
)
28459 TREE_USED (label
) = 1;
28460 check_goto (label
);
28461 name
= build_string (IDENTIFIER_LENGTH (identifier
),
28462 IDENTIFIER_POINTER (identifier
));
28463 labels
= tree_cons (name
, label
, labels
);
28466 /* If the next token is not a `,', then the list is
28468 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
28470 /* Consume the `,' token. */
28471 cp_lexer_consume_token (parser
->lexer
);
28474 return nreverse (labels
);
28477 /* Return TRUE iff the next tokens in the stream are possibly the
28478 beginning of a GNU extension attribute. */
28481 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
28483 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
28486 /* Return TRUE iff the next tokens in the stream are possibly the
28487 beginning of a standard C++-11 attribute specifier. */
28490 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
28492 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
28495 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28496 beginning of a standard C++-11 attribute specifier. */
28499 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
28501 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28503 return (cxx_dialect
>= cxx11
28504 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
28505 || (token
->type
== CPP_OPEN_SQUARE
28506 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
28507 && token
->type
== CPP_OPEN_SQUARE
)));
28510 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28511 beginning of a GNU extension attribute. */
28514 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
28516 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28518 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
28521 /* Return true iff the next tokens can be the beginning of either a
28522 GNU attribute list, or a standard C++11 attribute sequence. */
28525 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
28527 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
28528 || cp_next_tokens_can_be_std_attribute_p (parser
));
28531 /* Return true iff the next Nth tokens can be the beginning of either
28532 a GNU attribute list, or a standard C++11 attribute sequence. */
28535 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
28537 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
28538 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
28541 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
28542 of GNU attributes, or return NULL. */
28545 cp_parser_attributes_opt (cp_parser
*parser
)
28547 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
28548 return cp_parser_gnu_attributes_opt (parser
);
28549 return cp_parser_std_attribute_spec_seq (parser
);
28552 /* Parse an (optional) series of attributes.
28555 attributes attribute
28558 __attribute__ (( attribute-list [opt] ))
28560 The return value is as for cp_parser_gnu_attribute_list. */
28563 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
28565 tree attributes
= NULL_TREE
;
28567 auto cleanup
= make_temp_override
28568 (parser
->auto_is_implicit_function_template_parm_p
, false);
28573 tree attribute_list
;
28576 /* Peek at the next token. */
28577 token
= cp_lexer_peek_token (parser
->lexer
);
28578 /* If it's not `__attribute__', then we're done. */
28579 if (token
->keyword
!= RID_ATTRIBUTE
)
28582 /* Consume the `__attribute__' keyword. */
28583 cp_lexer_consume_token (parser
->lexer
);
28584 /* Look for the two `(' tokens. */
28585 matching_parens outer_parens
;
28586 if (!outer_parens
.require_open (parser
))
28588 matching_parens inner_parens
;
28589 if (!inner_parens
.require_open (parser
))
28592 /* Peek at the next token. */
28593 token
= cp_lexer_peek_token (parser
->lexer
);
28594 if (token
->type
!= CPP_CLOSE_PAREN
)
28595 /* Parse the attribute-list. */
28596 attribute_list
= cp_parser_gnu_attribute_list (parser
);
28598 /* If the next token is a `)', then there is no attribute
28600 attribute_list
= NULL
;
28602 /* Look for the two `)' tokens. */
28603 if (!inner_parens
.require_close (parser
))
28605 if (!outer_parens
.require_close (parser
))
28608 cp_parser_skip_to_end_of_statement (parser
);
28610 /* Add these new attributes to the list. */
28611 attributes
= attr_chainon (attributes
, attribute_list
);
28617 /* Parse a GNU attribute-list.
28621 attribute-list , attribute
28625 identifier ( identifier )
28626 identifier ( identifier , expression-list )
28627 identifier ( expression-list )
28629 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
28630 to an attribute. The TREE_PURPOSE of each node is the identifier
28631 indicating which attribute is in use. The TREE_VALUE represents
28632 the arguments, if any. */
28635 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
28637 tree attribute_list
= NULL_TREE
;
28638 bool save_translate_strings_p
= parser
->translate_strings_p
;
28640 /* Don't create wrapper nodes within attributes: the
28641 handlers don't know how to handle them. */
28642 auto_suppress_location_wrappers sentinel
;
28644 parser
->translate_strings_p
= false;
28651 /* Look for the identifier. We also allow keywords here; for
28652 example `__attribute__ ((const))' is legal. */
28653 token
= cp_lexer_peek_token (parser
->lexer
);
28654 if (token
->type
== CPP_NAME
28655 || token
->type
== CPP_KEYWORD
)
28657 tree arguments
= NULL_TREE
;
28659 /* Consume the token, but save it since we need it for the
28660 SIMD enabled function parsing. */
28661 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
28663 /* Save away the identifier that indicates which attribute
28665 identifier
= (token
->type
== CPP_KEYWORD
)
28666 /* For keywords, use the canonical spelling, not the
28667 parsed identifier. */
28668 ? ridpointers
[(int) token
->keyword
]
28669 : id_token
->u
.value
;
28671 identifier
= canonicalize_attr_name (identifier
);
28672 attribute
= build_tree_list (identifier
, NULL_TREE
);
28674 /* Peek at the next token. */
28675 token
= cp_lexer_peek_token (parser
->lexer
);
28676 /* If it's an `(', then parse the attribute arguments. */
28677 if (token
->type
== CPP_OPEN_PAREN
)
28679 vec
<tree
, va_gc
> *vec
;
28680 int attr_flag
= (attribute_takes_identifier_p (identifier
)
28681 ? id_attr
: normal_attr
);
28682 vec
= cp_parser_parenthesized_expression_list
28683 (parser
, attr_flag
, /*cast_p=*/false,
28684 /*allow_expansion_p=*/false,
28685 /*non_constant_p=*/NULL
);
28687 arguments
= error_mark_node
;
28690 arguments
= build_tree_list_vec (vec
);
28691 release_tree_vector (vec
);
28693 /* Save the arguments away. */
28694 TREE_VALUE (attribute
) = arguments
;
28697 if (arguments
!= error_mark_node
)
28699 /* Add this attribute to the list. */
28700 TREE_CHAIN (attribute
) = attribute_list
;
28701 attribute_list
= attribute
;
28704 token
= cp_lexer_peek_token (parser
->lexer
);
28706 /* Unless EXACTLY_ONE is set look for more attributes.
28707 If the next token isn't a `,', we're done. */
28708 if (exactly_one
|| token
->type
!= CPP_COMMA
)
28711 /* Consume the comma and keep going. */
28712 cp_lexer_consume_token (parser
->lexer
);
28714 parser
->translate_strings_p
= save_translate_strings_p
;
28716 /* We built up the list in reverse order. */
28717 return nreverse (attribute_list
);
28720 /* Parse arguments of omp::directive attribute.
28722 ( directive-name ,[opt] clause-list[opt] )
28724 For directive just remember the first/last tokens for subsequent
28728 cp_parser_omp_directive_args (cp_parser
*parser
, tree attribute
)
28730 cp_token
*first
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
28731 if (first
->type
== CPP_CLOSE_PAREN
)
28733 cp_lexer_consume_token (parser
->lexer
);
28734 error_at (first
->location
, "expected OpenMP directive name");
28735 cp_lexer_consume_token (parser
->lexer
);
28736 TREE_VALUE (attribute
) = NULL_TREE
;
28739 size_t n
= cp_parser_skip_balanced_tokens (parser
, 1);
28742 cp_lexer_consume_token (parser
->lexer
);
28743 error_at (first
->location
, "expected attribute argument as balanced "
28745 TREE_VALUE (attribute
) = NULL_TREE
;
28748 for (n
= n
- 2; n
; --n
)
28749 cp_lexer_consume_token (parser
->lexer
);
28750 cp_token
*last
= cp_lexer_peek_token (parser
->lexer
);
28751 cp_lexer_consume_token (parser
->lexer
);
28752 tree arg
= make_node (DEFERRED_PARSE
);
28753 DEFPARSE_TOKENS (arg
) = cp_token_cache_new (first
, last
);
28754 DEFPARSE_INSTANTIATIONS (arg
) = nullptr;
28755 TREE_VALUE (attribute
) = tree_cons (NULL_TREE
, arg
, TREE_VALUE (attribute
));
28758 /* Parse arguments of omp::sequence attribute.
28760 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
28763 cp_parser_omp_sequence_args (cp_parser
*parser
, tree attribute
)
28765 matching_parens parens
;
28766 parens
.consume_open (parser
);
28769 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28770 if (token
->type
== CPP_NAME
28771 && token
->u
.value
== omp_identifier
28772 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
28774 cp_lexer_consume_token (parser
->lexer
);
28775 cp_lexer_consume_token (parser
->lexer
);
28776 token
= cp_lexer_peek_token (parser
->lexer
);
28778 bool directive
= false;
28780 if (token
->type
!= CPP_NAME
)
28783 p
= IDENTIFIER_POINTER (token
->u
.value
);
28784 if (strcmp (p
, "directive") == 0)
28786 else if (strcmp (p
, "sequence") != 0)
28788 error_at (token
->location
, "expected %<directive%> or %<sequence%>");
28789 cp_parser_skip_to_closing_parenthesis (parser
,
28790 /*recovering=*/true,
28792 /*consume_paren=*/false);
28793 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
28795 cp_lexer_consume_token (parser
->lexer
);
28797 cp_lexer_consume_token (parser
->lexer
);
28798 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
28799 cp_parser_required_error (parser
, RT_OPEN_PAREN
, false,
28801 else if (directive
)
28802 cp_parser_omp_directive_args (parser
, attribute
);
28804 cp_parser_omp_sequence_args (parser
, attribute
);
28805 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
28807 cp_lexer_consume_token (parser
->lexer
);
28810 if (!parens
.require_close (parser
))
28811 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
28812 /*consume_paren=*/true);
28815 /* Parse a standard C++11 attribute.
28817 The returned representation is a TREE_LIST which TREE_PURPOSE is
28818 the scoped name of the attribute, and the TREE_VALUE is its
28821 Note that the scoped name of the attribute is itself a TREE_LIST
28822 which TREE_PURPOSE is the namespace of the attribute, and
28823 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
28824 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
28825 and which TREE_PURPOSE is directly the attribute name.
28827 Clients of the attribute code should use get_attribute_namespace
28828 and get_attribute_name to get the actual namespace and name of
28829 attributes, regardless of their being GNU or C++11 attributes.
28832 attribute-token attribute-argument-clause [opt]
28836 attribute-scoped-token
28838 attribute-scoped-token:
28839 attribute-namespace :: identifier
28841 attribute-namespace:
28844 attribute-argument-clause:
28845 ( balanced-token-seq )
28847 balanced-token-seq:
28848 balanced-token [opt]
28849 balanced-token-seq balanced-token
28852 ( balanced-token-seq )
28853 [ balanced-token-seq ]
28854 { balanced-token-seq }. */
28857 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
28859 tree attribute
, attr_id
= NULL_TREE
, arguments
;
28862 auto cleanup
= make_temp_override
28863 (parser
->auto_is_implicit_function_template_parm_p
, false);
28865 /* First, parse name of the attribute, a.k.a attribute-token. */
28867 token
= cp_lexer_peek_token (parser
->lexer
);
28868 if (token
->type
== CPP_NAME
)
28869 attr_id
= token
->u
.value
;
28870 else if (token
->type
== CPP_KEYWORD
)
28871 attr_id
= ridpointers
[(int) token
->keyword
];
28872 else if (token
->flags
& NAMED_OP
)
28873 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
28875 if (attr_id
== NULL_TREE
)
28878 cp_lexer_consume_token (parser
->lexer
);
28880 token
= cp_lexer_peek_token (parser
->lexer
);
28881 if (token
->type
== CPP_SCOPE
)
28883 /* We are seeing a scoped attribute token. */
28885 cp_lexer_consume_token (parser
->lexer
);
28887 error_at (token
->location
, "attribute using prefix used together "
28888 "with scoped attribute token");
28891 token
= cp_lexer_peek_token (parser
->lexer
);
28892 if (token
->type
== CPP_NAME
)
28893 attr_id
= token
->u
.value
;
28894 else if (token
->type
== CPP_KEYWORD
)
28895 attr_id
= ridpointers
[(int) token
->keyword
];
28896 else if (token
->flags
& NAMED_OP
)
28897 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
28900 error_at (token
->location
,
28901 "expected an identifier for the attribute name");
28902 return error_mark_node
;
28904 cp_lexer_consume_token (parser
->lexer
);
28906 attr_ns
= canonicalize_attr_name (attr_ns
);
28907 attr_id
= canonicalize_attr_name (attr_id
);
28908 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
28910 token
= cp_lexer_peek_token (parser
->lexer
);
28914 attr_ns
= canonicalize_attr_name (attr_ns
);
28915 attr_id
= canonicalize_attr_name (attr_id
);
28916 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
28921 attr_id
= canonicalize_attr_name (attr_id
);
28922 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
28924 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
28925 but no longer: we have to be able to tell [[noreturn]] and
28926 __attribute__((noreturn)) apart. */
28927 /* C++14 deprecated attribute is equivalent to GNU's. */
28928 if (is_attribute_p ("deprecated", attr_id
))
28929 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
28930 /* C++17 fallthrough attribute is equivalent to GNU's. */
28931 else if (is_attribute_p ("fallthrough", attr_id
))
28932 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
28933 /* Transactional Memory TS optimize_for_synchronized attribute is
28934 equivalent to GNU transaction_callable. */
28935 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
28936 TREE_PURPOSE (attribute
)
28937 = get_identifier ("transaction_callable");
28938 /* Transactional Memory attributes are GNU attributes. */
28939 else if (tm_attr_to_mask (attr_id
))
28940 TREE_PURPOSE (attribute
) = attr_id
;
28943 /* Now parse the optional argument clause of the attribute. */
28945 if (token
->type
!= CPP_OPEN_PAREN
)
28947 if ((flag_openmp
|| flag_openmp_simd
)
28948 && attr_ns
== omp_identifier
28949 && (is_attribute_p ("directive", attr_id
)
28950 || is_attribute_p ("sequence", attr_id
)))
28952 error_at (token
->location
, "%<omp::%E%> attribute requires argument",
28960 vec
<tree
, va_gc
> *vec
;
28961 int attr_flag
= normal_attr
;
28963 /* Maybe we don't expect to see any arguments for this attribute. */
28964 const attribute_spec
*as
28965 = lookup_attribute_spec (TREE_PURPOSE (attribute
));
28966 if (as
&& as
->max_length
== 0)
28968 error_at (token
->location
, "%qE attribute does not take any arguments",
28970 cp_parser_skip_to_closing_parenthesis (parser
,
28971 /*recovering=*/true,
28972 /*or_comma=*/false,
28973 /*consume_paren=*/true);
28974 return error_mark_node
;
28977 if (attr_ns
== gnu_identifier
28978 && attribute_takes_identifier_p (attr_id
))
28979 /* A GNU attribute that takes an identifier in parameter. */
28980 attr_flag
= id_attr
;
28982 /* If this is a fake attribute created to handle -Wno-attributes,
28983 we must skip parsing the arguments. */
28984 if (as
== NULL
|| attribute_ignored_p (as
))
28986 if ((flag_openmp
|| flag_openmp_simd
) && attr_ns
== omp_identifier
)
28988 if (is_attribute_p ("directive", attr_id
))
28990 cp_parser_omp_directive_args (parser
, attribute
);
28993 else if (is_attribute_p ("sequence", attr_id
))
28995 TREE_VALUE (TREE_PURPOSE (attribute
))
28996 = get_identifier ("directive");
28997 cp_parser_omp_sequence_args (parser
, attribute
);
28998 TREE_VALUE (attribute
) = nreverse (TREE_VALUE (attribute
));
29003 /* For unknown attributes, just skip balanced tokens instead of
29004 trying to parse the arguments. */
29005 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1) - 1; n
; --n
)
29006 cp_lexer_consume_token (parser
->lexer
);
29010 vec
= cp_parser_parenthesized_expression_list
29011 (parser
, attr_flag
, /*cast_p=*/false,
29012 /*allow_expansion_p=*/true,
29013 /*non_constant_p=*/NULL
);
29015 arguments
= error_mark_node
;
29018 if (vec
->is_empty ())
29019 /* e.g. [[attr()]]. */
29020 error_at (token
->location
, "parentheses must be omitted if "
29021 "%qE attribute argument list is empty",
29023 arguments
= build_tree_list_vec (vec
);
29024 release_tree_vector (vec
);
29027 if (arguments
== error_mark_node
)
29028 attribute
= error_mark_node
;
29030 TREE_VALUE (attribute
) = arguments
;
29036 /* Warn if the attribute ATTRIBUTE appears more than once in the
29037 attribute-list ATTRIBUTES. This used to be enforced for certain
29038 attributes, but the restriction was removed in P2156. Note that
29039 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
29040 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29041 found in ATTRIBUTES. */
29044 cp_parser_check_std_attribute (location_t loc
, tree attributes
, tree attribute
)
29046 static auto alist
= { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29047 "likely", "unlikely", "fallthrough",
29048 "no_unique_address" };
29050 for (const auto &a
: alist
)
29051 if (is_attribute_p (a
, get_attribute_name (attribute
))
29052 && lookup_attribute (a
, attributes
))
29054 if (!from_macro_expansion_at (loc
))
29055 warning_at (loc
, OPT_Wattributes
, "attribute %qs specified "
29056 "multiple times", a
);
29062 /* Parse a list of standard C++-11 attributes.
29066 attribute-list , attribute[opt]
29068 attribute-list , attribute ...
29072 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
29074 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
29075 cp_token
*token
= NULL
;
29079 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29080 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
29081 if (attribute
== error_mark_node
)
29083 if (attribute
!= NULL_TREE
)
29085 if (cp_parser_check_std_attribute (loc
, attributes
, attribute
))
29087 TREE_CHAIN (attribute
) = attributes
;
29088 attributes
= attribute
;
29091 token
= cp_lexer_peek_token (parser
->lexer
);
29092 if (token
->type
== CPP_ELLIPSIS
)
29094 cp_lexer_consume_token (parser
->lexer
);
29095 if (attribute
== NULL_TREE
)
29096 error_at (token
->location
,
29097 "expected attribute before %<...%>");
29100 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
29101 if (pack
== error_mark_node
)
29102 return error_mark_node
;
29103 TREE_VALUE (attribute
) = pack
;
29105 token
= cp_lexer_peek_token (parser
->lexer
);
29107 if (token
->type
!= CPP_COMMA
)
29109 cp_lexer_consume_token (parser
->lexer
);
29111 attributes
= nreverse (attributes
);
29115 /* Parse a standard C++-11 attribute specifier.
29117 attribute-specifier:
29118 [ [ attribute-using-prefix [opt] attribute-list ] ]
29119 alignment-specifier
29121 attribute-using-prefix:
29122 using attribute-namespace :
29124 alignment-specifier:
29125 alignas ( type-id ... [opt] )
29126 alignas ( alignment-expression ... [opt] ). */
29129 cp_parser_std_attribute_spec (cp_parser
*parser
)
29131 tree attributes
= NULL_TREE
;
29132 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29134 if (token
->type
== CPP_OPEN_SQUARE
29135 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
29137 tree attr_ns
= NULL_TREE
;
29139 cp_lexer_consume_token (parser
->lexer
);
29140 cp_lexer_consume_token (parser
->lexer
);
29142 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
29144 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
29145 if (token
->type
== CPP_NAME
)
29146 attr_ns
= token
->u
.value
;
29147 else if (token
->type
== CPP_KEYWORD
)
29148 attr_ns
= ridpointers
[(int) token
->keyword
];
29149 else if (token
->flags
& NAMED_OP
)
29150 attr_ns
= get_identifier (cpp_type2name (token
->type
,
29153 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
29155 if (cxx_dialect
< cxx17
)
29156 pedwarn (input_location
, OPT_Wc__17_extensions
,
29157 "attribute using prefix only available "
29158 "with %<-std=c++17%> or %<-std=gnu++17%>");
29160 cp_lexer_consume_token (parser
->lexer
);
29161 cp_lexer_consume_token (parser
->lexer
);
29162 cp_lexer_consume_token (parser
->lexer
);
29165 attr_ns
= NULL_TREE
;
29168 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
29170 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
29171 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
29172 cp_parser_skip_to_end_of_statement (parser
);
29174 /* Warn about parsing c++11 attribute in non-c++11 mode, only
29175 when we are sure that we have actually parsed them. */
29176 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
29182 /* Look for an alignment-specifier. */
29184 token
= cp_lexer_peek_token (parser
->lexer
);
29186 if (token
->type
!= CPP_KEYWORD
29187 || token
->keyword
!= RID_ALIGNAS
)
29190 cp_lexer_consume_token (parser
->lexer
);
29191 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
29193 matching_parens parens
;
29194 if (!parens
.require_open (parser
))
29195 return error_mark_node
;
29197 cp_parser_parse_tentatively (parser
);
29198 alignas_expr
= cp_parser_type_id (parser
);
29200 if (!cp_parser_parse_definitely (parser
))
29202 alignas_expr
= cp_parser_assignment_expression (parser
);
29203 if (alignas_expr
== error_mark_node
)
29204 cp_parser_skip_to_end_of_statement (parser
);
29205 if (alignas_expr
== NULL_TREE
29206 || alignas_expr
== error_mark_node
)
29207 return alignas_expr
;
29210 alignas_expr
= cxx_alignas_expr (alignas_expr
);
29211 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
29213 /* Handle alignas (pack...). */
29214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
29216 cp_lexer_consume_token (parser
->lexer
);
29217 alignas_expr
= make_pack_expansion (alignas_expr
);
29220 /* Something went wrong, so don't build the attribute. */
29221 if (alignas_expr
== error_mark_node
)
29222 return error_mark_node
;
29224 /* Missing ')' means the code cannot possibly be valid; go ahead
29225 and commit to make sure we issue a hard error. */
29226 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
29227 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29228 cp_parser_commit_to_tentative_parse (parser
);
29230 if (!parens
.require_close (parser
))
29231 return error_mark_node
;
29233 /* Build the C++-11 representation of an 'aligned'
29236 = build_tree_list (build_tree_list (gnu_identifier
,
29237 aligned_identifier
), alignas_expr
);
29243 /* Parse a standard C++-11 attribute-specifier-seq.
29245 attribute-specifier-seq:
29246 attribute-specifier-seq [opt] attribute-specifier
29250 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
29252 tree attr_specs
= NULL_TREE
;
29253 tree attr_last
= NULL_TREE
;
29255 /* Don't create wrapper nodes within attributes: the
29256 handlers don't know how to handle them. */
29257 auto_suppress_location_wrappers sentinel
;
29261 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
29262 if (attr_spec
== NULL_TREE
)
29264 if (attr_spec
== error_mark_node
)
29265 return error_mark_node
;
29268 TREE_CHAIN (attr_last
) = attr_spec
;
29270 attr_specs
= attr_last
= attr_spec
;
29271 attr_last
= tree_last (attr_last
);
29277 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
29278 return index of the first token after balanced-token, or N on failure. */
29281 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
29284 int nparens
= 0, nbraces
= 0, nsquares
= 0;
29286 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
29288 case CPP_PRAGMA_EOL
:
29289 if (!parser
->lexer
->in_pragma
)
29293 /* Ran out of tokens. */
29295 case CPP_OPEN_PAREN
:
29298 case CPP_OPEN_BRACE
:
29301 case CPP_OPEN_SQUARE
:
29304 case CPP_CLOSE_PAREN
:
29307 case CPP_CLOSE_BRACE
:
29310 case CPP_CLOSE_SQUARE
:
29316 while (nparens
|| nbraces
|| nsquares
);
29320 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
29321 return index of the first token after the GNU attribute tokens, or N on
29325 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
29329 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
29330 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
29331 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
29334 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
29337 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
29344 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
29345 next token), return index of the first token after the standard C++11
29346 attribute tokens, or N on failure. */
29349 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
29353 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
29354 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
29356 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
29359 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
29363 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
29364 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
29366 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
29377 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
29378 as the next token), return index of the first token after the attribute
29379 tokens, or N on failure. */
29382 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
29384 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
29385 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
29386 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
29389 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
29390 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
29391 current value of the PEDANTIC flag, regardless of whether or not
29392 the `__extension__' keyword is present. The caller is responsible
29393 for restoring the value of the PEDANTIC flag. */
29396 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
29398 /* Save the old value of the PEDANTIC flag. */
29399 *saved_pedantic
= pedantic
;
29401 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
29403 /* Consume the `__extension__' token. */
29404 cp_lexer_consume_token (parser
->lexer
);
29405 /* We're not being pedantic while the `__extension__' keyword is
29415 /* Parse a label declaration.
29418 __label__ label-declarator-seq ;
29420 label-declarator-seq:
29421 identifier , label-declarator-seq
29425 cp_parser_label_declaration (cp_parser
* parser
)
29427 /* Look for the `__label__' keyword. */
29428 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
29434 /* Look for an identifier. */
29435 identifier
= cp_parser_identifier (parser
);
29436 /* If we failed, stop. */
29437 if (identifier
== error_mark_node
)
29439 /* Declare it as a label. */
29440 finish_label_decl (identifier
);
29441 /* If the next token is a `;', stop. */
29442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29444 /* Look for the `,' separating the label declarations. */
29445 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
29448 /* Look for the final `;'. */
29449 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29452 // -------------------------------------------------------------------------- //
29453 // Concept definitions
29456 cp_parser_concept_definition (cp_parser
*parser
)
29458 /* A concept definition is an unevaluated context. */
29461 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
));
29462 cp_lexer_consume_token (parser
->lexer
);
29464 cp_expr id
= cp_parser_identifier (parser
);
29465 if (id
== error_mark_node
)
29467 cp_parser_skip_to_end_of_statement (parser
);
29468 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29472 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29474 cp_parser_skip_to_end_of_statement (parser
);
29475 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29476 return error_mark_node
;
29479 processing_constraint_expression_sentinel parsing_constraint
;
29480 tree init
= cp_parser_constraint_expression (parser
);
29481 if (init
== error_mark_node
)
29482 cp_parser_skip_to_end_of_statement (parser
);
29484 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
29485 but continue as if it were. */
29486 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29488 return finish_concept_definition (id
, init
);
29491 // -------------------------------------------------------------------------- //
29494 /* Diagnose an expression that should appear in ()'s within a requires-clause
29495 and suggest where to place those parentheses. */
29498 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc
)
29500 error_at (loc
, "expression must be enclosed in parentheses");
29504 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc
)
29506 gcc_rich_location
richloc (loc
);
29507 richloc
.add_fixit_insert_before ("(");
29508 richloc
.add_fixit_insert_after (")");
29509 error_at (&richloc
, "expression must be enclosed in parentheses");
29512 /* Characterizes the likely kind of expression intended by a mis-written
29513 primary constraint. */
29514 enum primary_constraint_error
29517 pce_maybe_operator
,
29521 /* Returns true if the token(s) following a primary-expression in a
29522 constraint-logical-* expression would require parentheses. */
29524 static primary_constraint_error
29525 cp_parser_constraint_requires_parens (cp_parser
*parser
, bool lambda_p
)
29527 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29528 switch (token
->type
)
29535 /* An equal sign may be part of the definition of a function,
29536 and not an assignment operator, when parsing the expression
29537 for a trailing requires-clause. For example:
29539 template<typename T>
29541 S() requires C<T> = default;
29544 Don't try to reparse this a binary operator. */
29545 if (cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DELETE
)
29546 || cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DEFAULT
))
29549 gcc_fallthrough ();
29552 /* Arithmetic operators. */
29558 /* Bitwise operators. */
29564 /* Relational operators. */
29570 case CPP_GREATER_EQ
:
29571 case CPP_SPACESHIP
:
29572 /* Pointer-to-member. */
29574 case CPP_DEREF_STAR
:
29575 /* Assignment operators. */
29584 case CPP_RSHIFT_EQ
:
29585 case CPP_LSHIFT_EQ
:
29586 /* Conditional operator */
29588 /* Unenclosed binary or conditional operator. */
29589 return pce_maybe_operator
;
29591 case CPP_OPEN_PAREN
:
29593 /* A primary constraint that precedes the parameter-list of a
29594 lambda expression is followed by an open paren.
29596 []<typename T> requires C (T a, T b) { ... }
29598 Don't try to re-parse this as a postfix expression. */
29602 gcc_fallthrough ();
29604 case CPP_OPEN_SQUARE
:
29606 /* A primary-constraint-expression followed by a '[[' is not a
29607 postfix expression. */
29608 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
))
29611 gcc_fallthrough ();
29613 case CPP_PLUS_PLUS
:
29614 case CPP_MINUS_MINUS
:
29616 /* Unenclosed postfix operator. */
29617 return pce_maybe_postfix
;
29620 /* A primary constraint that precedes the lambda-declarator of a
29621 lambda expression is followed by trailing return type.
29623 []<typename T> requires C -> void {}
29625 Don't try to re-parse this as a postfix expression in
29626 C++23 and later. In C++20 ( needs to come in between but we
29627 allow it to be omitted with pedwarn. */
29630 /* Unenclosed postfix operator. */
29631 return pce_maybe_postfix
;
29635 /* Returns true if the next token begins a unary expression, preceded by
29636 an operator or keyword. */
29639 cp_parser_unary_constraint_requires_parens (cp_parser
*parser
)
29641 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29642 switch (token
->type
)
29649 case CPP_PLUS_PLUS
:
29650 case CPP_MINUS_MINUS
:
29655 switch (token
->keyword
)
29659 case RID_REINTCAST
:
29660 case RID_CONSTCAST
:
29682 /* Parse a primary expression within a constraint. */
29685 cp_parser_constraint_primary_expression (cp_parser
*parser
, bool lambda_p
)
29687 /* If this looks like a unary expression, parse it as such, but diagnose
29688 it as ill-formed; it requires parens. */
29689 if (cp_parser_unary_constraint_requires_parens (parser
))
29691 cp_expr e
= cp_parser_assignment_expression (parser
, NULL
, false, false);
29692 cp_parser_diagnose_ungrouped_constraint_rich (e
.get_location());
29696 cp_lexer_save_tokens (parser
->lexer
);
29698 location_t loc
= input_location
;
29699 cp_expr expr
= cp_parser_primary_expression (parser
,
29700 /*address_p=*/false,
29702 /*template_arg_p=*/false,
29704 expr
.maybe_add_location_wrapper ();
29706 primary_constraint_error pce
= pce_ok
;
29707 if (expr
!= error_mark_node
)
29709 /* The primary-expression could be part of an unenclosed non-logical
29710 compound expression. */
29711 pce
= cp_parser_constraint_requires_parens (parser
, lambda_p
);
29715 cp_lexer_commit_tokens (parser
->lexer
);
29716 return finish_constraint_primary_expr (expr
);
29719 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
29720 error, but return the expression as if it were valid. */
29721 cp_lexer_rollback_tokens (parser
->lexer
);
29722 cp_parser_parse_tentatively (parser
);
29723 if (pce
== pce_maybe_operator
)
29724 expr
= cp_parser_assignment_expression (parser
, NULL
, false, false);
29726 expr
= cp_parser_simple_cast_expression (parser
);
29727 if (cp_parser_parse_definitely (parser
))
29729 cp_parser_diagnose_ungrouped_constraint_rich (expr
.get_location());
29733 /* Otherwise, something has gone very wrong, and we can't generate a more
29734 meaningful diagnostic or recover. */
29735 cp_parser_diagnose_ungrouped_constraint_plain (loc
);
29736 return error_mark_node
;
29739 /* Parse a constraint-logical-and-expression.
29741 constraint-logical-and-expression:
29743 constraint-logical-and-expression '&&' primary-expression */
29746 cp_parser_constraint_logical_and_expression (cp_parser
*parser
, bool lambda_p
)
29748 cp_expr lhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
29749 while (cp_lexer_next_token_is (parser
->lexer
, CPP_AND_AND
))
29751 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
29752 tree rhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
29753 lhs
= finish_constraint_and_expr (op
->location
, lhs
, rhs
);
29758 /* Parse a constraint-logical-or-expression.
29760 constraint-logical-or-expression:
29761 constraint-logical-and-expression
29762 constraint-logical-or-expression '||' constraint-logical-and-expression */
29765 cp_parser_constraint_logical_or_expression (cp_parser
*parser
, bool lambda_p
)
29767 cp_expr lhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
29768 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OR_OR
))
29770 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
29771 cp_expr rhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
29772 lhs
= finish_constraint_or_expr (op
->location
, lhs
, rhs
);
29777 /* Parse the expression after a requires-clause. This has a different grammar
29778 than that in the concepts TS. */
29781 cp_parser_requires_clause_expression (cp_parser
*parser
, bool lambda_p
)
29783 processing_constraint_expression_sentinel parsing_constraint
;
29784 ++processing_template_decl
;
29785 cp_expr expr
= cp_parser_constraint_logical_or_expression (parser
, lambda_p
);
29786 --processing_template_decl
;
29787 if (check_for_bare_parameter_packs (expr
))
29788 expr
= error_mark_node
;
29792 /* Parse a expression after a requires clause.
29794 constraint-expression:
29795 logical-or-expression
29797 The required logical-or-expression must be a constant expression. Note
29798 that we don't check that the expression is constepxr here. We defer until
29799 we analyze constraints and then, we only check atomic constraints. */
29802 cp_parser_constraint_expression (cp_parser
*parser
)
29804 processing_constraint_expression_sentinel parsing_constraint
;
29805 ++processing_template_decl
;
29806 cp_expr expr
= cp_parser_binary_expression (parser
, false, true,
29807 PREC_NOT_OPERATOR
, NULL
);
29808 --processing_template_decl
;
29809 if (check_for_bare_parameter_packs (expr
))
29810 expr
= error_mark_node
;
29811 expr
.maybe_add_location_wrapper ();
29815 /* Optionally parse a requires clause:
29818 `requires` constraint-logical-or-expression.
29820 `requires constraint-expression.
29822 LAMBDA_P is true when the requires-clause is parsed before the
29823 parameter-list of a lambda-declarator. */
29826 cp_parser_requires_clause_opt (cp_parser
*parser
, bool lambda_p
)
29828 /* A requires clause is an unevaluated context. */
29831 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
29832 if (tok
->keyword
!= RID_REQUIRES
)
29834 if (!flag_concepts
&& tok
->type
== CPP_NAME
29835 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
29837 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29838 "%<requires%> only available with "
29839 "%<-std=c++20%> or %<-fconcepts%>");
29840 /* Parse and discard the requires-clause. */
29841 cp_lexer_consume_token (parser
->lexer
);
29842 cp_parser_constraint_expression (parser
);
29847 cp_token
*tok2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
29848 if (tok2
->type
== CPP_OPEN_BRACE
)
29850 /* An opening brace following the start of a requires-clause is
29851 ill-formed; the user likely forgot the second `requires' that
29852 would start a requires-expression. */
29853 gcc_rich_location
richloc (tok2
->location
);
29854 richloc
.add_fixit_insert_after (tok
->location
, " requires");
29855 error_at (&richloc
, "missing additional %<requires%> to start "
29856 "a requires-expression");
29857 /* Don't consume the `requires', so that it's reused as the start of a
29858 requires-expression. */
29861 cp_lexer_consume_token (parser
->lexer
);
29863 if (!flag_concepts_ts
)
29864 return cp_parser_requires_clause_expression (parser
, lambda_p
);
29866 return cp_parser_constraint_expression (parser
);
29869 /*---------------------------------------------------------------------------
29870 Requires expressions
29871 ---------------------------------------------------------------------------*/
29873 /* Parse a requires expression
29875 requirement-expression:
29876 'requires' requirement-parameter-list [opt] requirement-body */
29879 cp_parser_requires_expression (cp_parser
*parser
)
29881 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
29882 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
29884 /* Avoid committing to outer tentative parse. */
29885 tentative_firewall
firewall (parser
);
29887 /* This is definitely a requires-expression. */
29888 cp_parser_commit_to_tentative_parse (parser
);
29892 /* Local parameters are delared as variables within the scope
29893 of the expression. They are not visible past the end of
29894 the expression. Expressions within the requires-expression
29895 are unevaluated. */
29896 struct scope_sentinel
29900 ++cp_unevaluated_operand
;
29901 begin_scope (sk_block
, NULL_TREE
);
29906 pop_bindings_and_leave_scope ();
29907 --cp_unevaluated_operand
;
29911 /* Parse the optional parameter list. */
29912 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29914 parms
= cp_parser_requirement_parameter_list (parser
);
29915 if (parms
== error_mark_node
)
29916 return error_mark_node
;
29921 /* Parse the requirement body. */
29922 ++processing_template_decl
;
29923 reqs
= cp_parser_requirement_body (parser
);
29924 --processing_template_decl
;
29925 if (reqs
== error_mark_node
)
29926 return error_mark_node
;
29929 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
29931 grokparms (parms
, &parms
);
29932 loc
= make_location (loc
, loc
, parser
->lexer
);
29933 tree expr
= finish_requires_expr (loc
, parms
, reqs
);
29934 if (!processing_template_decl
)
29936 /* Perform semantic processing now to diagnose any invalid types and
29938 int saved_errorcount
= errorcount
;
29939 tsubst_requires_expr (expr
, NULL_TREE
, tf_warning_or_error
, NULL_TREE
);
29940 if (errorcount
> saved_errorcount
)
29941 return error_mark_node
;
29946 /* Parse a parameterized requirement.
29948 requirement-parameter-list:
29949 '(' parameter-declaration-clause ')' */
29952 cp_parser_requirement_parameter_list (cp_parser
*parser
)
29954 matching_parens parens
;
29955 if (!parens
.require_open (parser
))
29956 return error_mark_node
;
29958 tree parms
= (cp_parser_parameter_declaration_clause
29959 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
29961 if (!parens
.require_close (parser
))
29962 return error_mark_node
;
29964 /* Modify the declared parameters by removing their context
29965 so they don't refer to the enclosing scope and explicitly
29966 indicating that they are constraint variables. */
29967 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
29969 if (parm
== void_list_node
|| parm
== explicit_void_list_node
)
29971 tree decl
= TREE_VALUE (parm
);
29972 if (decl
!= error_mark_node
)
29974 DECL_CONTEXT (decl
) = NULL_TREE
;
29975 CONSTRAINT_VAR_P (decl
) = true;
29982 /* Parse the body of a requirement.
29985 '{' requirement-list '}' */
29987 cp_parser_requirement_body (cp_parser
*parser
)
29989 matching_braces braces
;
29990 if (!braces
.require_open (parser
))
29991 return error_mark_node
;
29993 tree reqs
= cp_parser_requirement_seq (parser
);
29995 if (!braces
.require_close (parser
))
29996 return error_mark_node
;
30001 /* Parse a sequence of requirements.
30005 requirement-seq requirement */
30008 cp_parser_requirement_seq (cp_parser
*parser
)
30010 tree result
= NULL_TREE
;
30013 tree req
= cp_parser_requirement (parser
);
30014 if (req
!= error_mark_node
)
30015 result
= tree_cons (NULL_TREE
, req
, result
);
30017 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
)
30018 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
));
30020 /* If there are no valid requirements, this is not a valid expression. */
30022 return error_mark_node
;
30024 /* Reverse the order of requirements so they are analyzed in order. */
30025 return nreverse (result
);
30028 /* Parse a syntactic requirement or type requirement.
30032 compound-requirement
30034 nested-requirement */
30037 cp_parser_requirement (cp_parser
*parser
)
30039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30040 return cp_parser_compound_requirement (parser
);
30041 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
30042 return cp_parser_type_requirement (parser
);
30043 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
30044 return cp_parser_nested_requirement (parser
);
30046 return cp_parser_simple_requirement (parser
);
30049 /* Parse a simple requirement.
30051 simple-requirement:
30055 cp_parser_simple_requirement (cp_parser
*parser
)
30057 location_t start
= cp_lexer_peek_token (parser
->lexer
)->location
;
30058 cp_expr expr
= cp_parser_expression (parser
, NULL
, false, false);
30059 if (expr
== error_mark_node
)
30060 cp_parser_skip_to_end_of_statement (parser
);
30062 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30064 if (!expr
|| expr
== error_mark_node
)
30065 return error_mark_node
;
30067 /* Sometimes we don't get locations, so use the cached token location
30068 as a reasonable approximation. */
30069 if (expr
.get_location() == UNKNOWN_LOCATION
)
30070 expr
.set_location (start
);
30072 for (tree t
= expr
; ; )
30074 if (TREE_CODE (t
) == TRUTH_ANDIF_EXPR
30075 || TREE_CODE (t
) == TRUTH_ORIF_EXPR
)
30077 t
= TREE_OPERAND (t
, 0);
30080 if (concept_check_p (t
))
30082 gcc_rich_location
richloc (get_start (start
));
30083 richloc
.add_fixit_insert_before (start
, "requires ");
30084 warning_at (&richloc
, OPT_Wmissing_requires
, "testing "
30085 "if a concept-id is a valid expression; add "
30086 "%<requires%> to check satisfaction");
30091 return finish_simple_requirement (expr
.get_location (), expr
);
30094 /* Parse a type requirement
30097 nested-name-specifier [opt] required-type-name ';'
30099 required-type-name:
30101 'template' [opt] simple-template-id */
30104 cp_parser_type_requirement (cp_parser
*parser
)
30106 cp_token
*start_tok
= cp_lexer_consume_token (parser
->lexer
);
30107 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30109 // Save the scope before parsing name specifiers.
30110 tree saved_scope
= parser
->scope
;
30111 tree saved_object_scope
= parser
->object_scope
;
30112 tree saved_qualifying_scope
= parser
->qualifying_scope
;
30113 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
30114 cp_parser_nested_name_specifier_opt (parser
,
30115 /*typename_keyword_p=*/true,
30116 /*check_dependency_p=*/false,
30118 /*is_declaration=*/false);
30121 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
30123 cp_lexer_consume_token (parser
->lexer
);
30124 type
= cp_parser_template_id (parser
,
30125 /*template_keyword_p=*/true,
30126 /*check_dependency=*/false,
30127 /*tag_type=*/none_type
,
30128 /*is_declaration=*/false);
30129 type
= make_typename_type (parser
->scope
, type
, typename_type
,
30130 /*complain=*/tf_error
);
30133 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
30135 if (TREE_CODE (type
) == TYPE_DECL
)
30136 type
= TREE_TYPE (type
);
30138 parser
->scope
= saved_scope
;
30139 parser
->object_scope
= saved_object_scope
;
30140 parser
->qualifying_scope
= saved_qualifying_scope
;
30142 if (type
== error_mark_node
)
30143 cp_parser_skip_to_end_of_statement (parser
);
30145 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30147 if (type
== error_mark_node
)
30148 return error_mark_node
;
30150 loc
= make_location (loc
, start_tok
->location
, parser
->lexer
);
30151 return finish_type_requirement (loc
, type
);
30154 /* Parse a compound requirement
30156 compound-requirement:
30157 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
30160 cp_parser_compound_requirement (cp_parser
*parser
)
30162 /* Parse an expression enclosed in '{ }'s. */
30163 matching_braces braces
;
30164 if (!braces
.require_open (parser
))
30165 return error_mark_node
;
30167 cp_token
*expr_token
= cp_lexer_peek_token (parser
->lexer
);
30169 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
30170 if (expr
== error_mark_node
)
30171 cp_parser_skip_to_closing_brace (parser
);
30173 if (!braces
.require_close (parser
))
30175 cp_parser_skip_to_end_of_statement (parser
);
30176 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30177 return error_mark_node
;
30180 /* If the expression was invalid, skip the remainder of the requirement. */
30181 if (!expr
|| expr
== error_mark_node
)
30183 cp_parser_skip_to_end_of_statement (parser
);
30184 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30185 return error_mark_node
;
30188 /* Parse the optional noexcept. */
30189 bool noexcept_p
= false;
30190 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
30192 cp_lexer_consume_token (parser
->lexer
);
30196 /* Parse the optional trailing return type. */
30197 tree type
= NULL_TREE
;
30198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
30200 cp_lexer_consume_token (parser
->lexer
);
30201 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
30203 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
30204 parser
->in_result_type_constraint_p
= true;
30205 /* C++20 allows either a type-id or a type-constraint. Parsing
30206 a type-id will subsume the parsing for a type-constraint but
30207 allow for more syntactic forms (e.g., const C<T>*). */
30208 type
= cp_parser_trailing_type_id (parser
);
30209 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
30210 if (type
== error_mark_node
)
30211 return error_mark_node
;
30213 location_t type_loc
= make_location (tok
->location
, tok
->location
,
30216 /* Check that we haven't written something like 'const C<T>*'. */
30217 if (type_uses_auto (type
))
30219 if (!is_auto (type
))
30221 error_at (type_loc
,
30222 "result type is not a plain type-constraint");
30223 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30224 return error_mark_node
;
30227 else if (!flag_concepts_ts
)
30228 /* P1452R2 removed the trailing-return-type option. */
30229 error_at (type_loc
,
30230 "return-type-requirement is not a type-constraint");
30233 location_t loc
= make_location (expr_token
->location
,
30234 braces
.open_location (),
30237 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30239 if (expr
== error_mark_node
|| type
== error_mark_node
)
30240 return error_mark_node
;
30242 return finish_compound_requirement (loc
, expr
, type
, noexcept_p
);
30245 /* Parse a nested requirement. This is the same as a requires clause.
30247 nested-requirement:
30251 cp_parser_nested_requirement (cp_parser
*parser
)
30253 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
30254 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
30255 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30256 tree req
= cp_parser_constraint_expression (parser
);
30257 if (req
== error_mark_node
)
30258 cp_parser_skip_to_end_of_statement (parser
);
30259 loc
= make_location (loc
, tok
->location
, parser
->lexer
);
30260 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30261 if (req
== error_mark_node
)
30262 return error_mark_node
;
30263 return finish_nested_requirement (loc
, req
);
30266 /* Support Functions */
30268 /* Return the appropriate prefer_type argument for lookup_name based on
30271 static inline LOOK_want
30272 prefer_type_arg (tag_types tag_type
)
30276 case none_type
: return LOOK_want::NORMAL
; // No preference.
30277 case scope_type
: return LOOK_want::TYPE_NAMESPACE
; // Type or namespace.
30278 default: return LOOK_want::TYPE
; // Type only.
30282 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
30283 NAME should have one of the representations used for an
30284 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
30285 is returned. If PARSER->SCOPE is a dependent type, then a
30286 SCOPE_REF is returned.
30288 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
30289 returned; the name was already resolved when the TEMPLATE_ID_EXPR
30290 was formed. Abstractly, such entities should not be passed to this
30291 function, because they do not need to be looked up, but it is
30292 simpler to check for this special case here, rather than at the
30295 In cases not explicitly covered above, this function returns a
30296 DECL, OVERLOAD, or baselink representing the result of the lookup.
30297 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
30300 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
30301 (e.g., "struct") that was used. In that case bindings that do not
30302 refer to types are ignored.
30304 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
30307 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
30310 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
30313 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
30314 TREE_LIST of candidates if name-lookup results in an ambiguity, and
30315 NULL_TREE otherwise. */
30318 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
30319 enum tag_types tag_type
,
30322 bool check_dependency
,
30323 tree
*ambiguous_decls
,
30324 location_t name_location
)
30327 tree object_type
= parser
->context
->object_type
;
30329 /* Assume that the lookup will be unambiguous. */
30330 if (ambiguous_decls
)
30331 *ambiguous_decls
= NULL_TREE
;
30333 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
30334 no longer valid. Note that if we are parsing tentatively, and
30335 the parse fails, OBJECT_TYPE will be automatically restored. */
30336 parser
->context
->object_type
= NULL_TREE
;
30338 if (name
== error_mark_node
)
30339 return error_mark_node
;
30341 /* A template-id has already been resolved; there is no lookup to
30343 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
30345 if (BASELINK_P (name
))
30347 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
30348 == TEMPLATE_ID_EXPR
);
30352 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
30353 it should already have been checked to make sure that the name
30354 used matches the type being destroyed. */
30355 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
30359 /* Figure out to which type this destructor applies. */
30361 type
= parser
->scope
;
30362 else if (object_type
)
30363 type
= object_type
;
30365 type
= current_class_type
;
30366 /* If that's not a class type, there is no destructor. */
30367 if (!type
|| !CLASS_TYPE_P (type
))
30368 return error_mark_node
;
30370 /* In a non-static member function, check implicit this->. */
30371 if (current_class_ref
)
30372 return lookup_destructor (current_class_ref
, parser
->scope
, name
,
30373 tf_warning_or_error
);
30375 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
30376 lazily_declare_fn (sfk_destructor
, type
);
30378 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
30381 return error_mark_node
;
30384 /* By this point, the NAME should be an ordinary identifier. If
30385 the id-expression was a qualified name, the qualifying scope is
30386 stored in PARSER->SCOPE at this point. */
30387 gcc_assert (identifier_p (name
));
30389 /* Perform the lookup. */
30394 if (parser
->scope
== error_mark_node
)
30395 return error_mark_node
;
30397 /* If the SCOPE is dependent, the lookup must be deferred until
30398 the template is instantiated -- unless we are explicitly
30399 looking up names in uninstantiated templates. Even then, we
30400 cannot look up the name if the scope is not a class type; it
30401 might, for example, be a template type parameter. */
30402 dependent_p
= (TYPE_P (parser
->scope
)
30403 && dependent_scope_p (parser
->scope
));
30404 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
30406 /* Defer lookup. */
30407 decl
= error_mark_node
;
30410 tree pushed_scope
= NULL_TREE
;
30412 /* If PARSER->SCOPE is a dependent type, then it must be a
30413 class type, and we must not be checking dependencies;
30414 otherwise, we would have processed this lookup above. So
30415 that PARSER->SCOPE is not considered a dependent base by
30416 lookup_member, we must enter the scope here. */
30418 pushed_scope
= push_scope (parser
->scope
);
30420 /* If the PARSER->SCOPE is a template specialization, it
30421 may be instantiated during name lookup. In that case,
30422 errors may be issued. Even if we rollback the current
30423 tentative parse, those errors are valid. */
30424 decl
= lookup_qualified_name (parser
->scope
, name
,
30425 prefer_type_arg (tag_type
),
30426 /*complain=*/true);
30428 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
30429 lookup result and the nested-name-specifier nominates a class C:
30430 * if the name specified after the nested-name-specifier, when
30431 looked up in C, is the injected-class-name of C (Clause 9), or
30432 * if the name specified after the nested-name-specifier is the
30433 same as the identifier or the simple-template-id's template-
30434 name in the last component of the nested-name-specifier,
30435 the name is instead considered to name the constructor of
30436 class C. [ Note: for example, the constructor is not an
30437 acceptable lookup result in an elaborated-type-specifier so
30438 the constructor would not be used in place of the
30439 injected-class-name. --end note ] Such a constructor name
30440 shall be used only in the declarator-id of a declaration that
30441 names a constructor or in a using-declaration. */
30442 if (tag_type
== none_type
30443 && DECL_SELF_REFERENCE_P (decl
)
30444 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
30445 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
30446 prefer_type_arg (tag_type
),
30447 /*complain=*/true);
30450 pop_scope (pushed_scope
);
30453 /* If the scope is a dependent type and either we deferred lookup or
30454 we did lookup but didn't find the name, rememeber the name. */
30455 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
30456 && dependent_type_p (parser
->scope
))
30462 /* The resolution to Core Issue 180 says that `struct
30463 A::B' should be considered a type-name, even if `A'
30465 type
= make_typename_type (parser
->scope
, name
, tag_type
,
30466 /*complain=*/tf_error
);
30467 if (type
!= error_mark_node
)
30468 decl
= TYPE_NAME (type
);
30470 else if (is_template
30471 && (cp_parser_next_token_ends_template_argument_p (parser
)
30472 || cp_lexer_next_token_is (parser
->lexer
,
30474 decl
= make_unbound_class_template (parser
->scope
,
30476 /*complain=*/tf_error
);
30478 decl
= build_qualified_name (/*type=*/NULL_TREE
,
30479 parser
->scope
, name
,
30482 parser
->qualifying_scope
= parser
->scope
;
30483 parser
->object_scope
= NULL_TREE
;
30485 else if (object_type
)
30487 /* Look up the name in the scope of the OBJECT_TYPE, unless the
30488 OBJECT_TYPE is not a class. */
30489 if (CLASS_TYPE_P (object_type
))
30490 /* If the OBJECT_TYPE is a template specialization, it may
30491 be instantiated during name lookup. In that case, errors
30492 may be issued. Even if we rollback the current tentative
30493 parse, those errors are valid. */
30494 decl
= lookup_member (object_type
,
30497 /*prefer_type=*/tag_type
!= none_type
,
30498 tf_warning_or_error
);
30503 /* Look it up in the enclosing context. DR 141: When looking for a
30504 template-name after -> or ., only consider class templates. */
30505 decl
= lookup_name (name
, is_namespace
? LOOK_want::NAMESPACE
30506 /* DR 141: When looking in the
30507 current enclosing context for a
30508 template-name after -> or ., only
30509 consider class templates. */
30510 : is_template
? LOOK_want::TYPE
30511 : prefer_type_arg (tag_type
));
30513 /* If we know we're looking for a type (e.g. A in p->A::x),
30514 mock up a typename. */
30515 if (!decl
&& object_type
&& tag_type
!= none_type
30516 && dependentish_scope_p (object_type
))
30518 tree type
= build_typename_type (object_type
, name
, name
,
30520 decl
= TYPE_NAME (type
);
30523 parser
->object_scope
= object_type
;
30524 parser
->qualifying_scope
= NULL_TREE
;
30528 decl
= lookup_name (name
, is_namespace
? LOOK_want::NAMESPACE
30529 : prefer_type_arg (tag_type
));
30530 parser
->qualifying_scope
= NULL_TREE
;
30531 parser
->object_scope
= NULL_TREE
;
30534 /* If the lookup failed, let our caller know. */
30535 if (!decl
|| decl
== error_mark_node
)
30536 return error_mark_node
;
30538 /* If we have resolved the name of a member declaration, check to
30539 see if the declaration is accessible. When the name resolves to
30540 set of overloaded functions, accessibility is checked when
30541 overload resolution is done. If we have a TREE_LIST, then the lookup
30542 is either ambiguous or it found multiple injected-class-names, the
30543 accessibility of which is trivially satisfied.
30545 During an explicit instantiation, access is not checked at all,
30546 as per [temp.explicit]. */
30548 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
,
30549 tf_warning_or_error
);
30551 /* Pull out the template from an injected-class-name (or multiple). */
30553 decl
= maybe_get_template_decl_from_type_decl (decl
);
30555 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
30556 if (TREE_CODE (decl
) == TREE_LIST
)
30558 if (ambiguous_decls
)
30559 *ambiguous_decls
= decl
;
30560 /* The error message we have to print is too complicated for
30561 cp_parser_error, so we incorporate its actions directly. */
30562 if (!cp_parser_simulate_error (parser
))
30564 error_at (name_location
, "reference to %qD is ambiguous",
30566 print_candidates (decl
);
30568 return error_mark_node
;
30571 gcc_assert (DECL_P (decl
)
30572 || TREE_CODE (decl
) == OVERLOAD
30573 || TREE_CODE (decl
) == SCOPE_REF
30574 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
30575 || BASELINK_P (decl
));
30577 maybe_record_typedef_use (decl
);
30579 return cp_expr (decl
, name_location
);
30582 /* Like cp_parser_lookup_name, but for use in the typical case where
30583 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
30584 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
30587 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
30589 return cp_parser_lookup_name (parser
, name
,
30591 /*is_template=*/false,
30592 /*is_namespace=*/false,
30593 /*check_dependency=*/true,
30594 /*ambiguous_decls=*/NULL
,
30598 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
30599 the current context, return the TYPE_DECL. If TAG_NAME_P is
30600 true, the DECL indicates the class being defined in a class-head,
30601 or declared in an elaborated-type-specifier.
30603 Otherwise, return DECL. */
30606 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
30608 /* If the TEMPLATE_DECL is being declared as part of a class-head,
30609 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
30612 template <typename T> struct B;
30615 template <typename T> struct A::B {};
30617 Similarly, in an elaborated-type-specifier:
30619 namespace N { struct X{}; }
30622 template <typename T> friend struct N::X;
30625 However, if the DECL refers to a class type, and we are in
30626 the scope of the class, then the name lookup automatically
30627 finds the TYPE_DECL created by build_self_reference rather
30628 than a TEMPLATE_DECL. For example, in:
30630 template <class T> struct S {
30634 there is no need to handle such case. */
30636 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
30637 return DECL_TEMPLATE_RESULT (decl
);
30642 /* If too many, or too few, template-parameter lists apply to the
30643 declarator, issue an error message. Returns TRUE if all went well,
30644 and FALSE otherwise. */
30647 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
30648 cp_declarator
*declarator
,
30649 location_t declarator_location
)
30651 switch (declarator
->kind
)
30655 unsigned num_templates
= 0;
30656 tree scope
= declarator
->u
.id
.qualifying_scope
;
30657 bool template_id_p
= false;
30660 num_templates
= num_template_headers_for_class (scope
);
30661 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
30662 == TEMPLATE_ID_EXPR
)
30664 /* If the DECLARATOR has the form `X<y>' then it uses one
30665 additional level of template parameters. */
30667 template_id_p
= true;
30670 return cp_parser_check_template_parameters
30671 (parser
, num_templates
, template_id_p
, declarator_location
,
30678 case cdk_reference
:
30680 return (cp_parser_check_declarator_template_parameters
30681 (parser
, declarator
->declarator
, declarator_location
));
30688 gcc_unreachable ();
30693 /* NUM_TEMPLATES were used in the current declaration. If that is
30694 invalid, return FALSE and issue an error messages. Otherwise,
30695 return TRUE. If DECLARATOR is non-NULL, then we are checking a
30696 declarator and we can print more accurate diagnostics. */
30699 cp_parser_check_template_parameters (cp_parser
* parser
,
30700 unsigned num_templates
,
30701 bool template_id_p
,
30702 location_t location
,
30703 cp_declarator
*declarator
)
30705 /* If there are the same number of template classes and parameter
30706 lists, that's OK. */
30707 if (parser
->num_template_parameter_lists
== num_templates
)
30709 /* If there are more, but only one more, and the name ends in an identifier,
30710 then we are declaring a primary template. That's OK too. */
30712 && parser
->num_template_parameter_lists
== num_templates
+ 1)
30715 if (cp_parser_simulate_error (parser
))
30718 /* If there are more template classes than parameter lists, we have
30721 template <class T> void S<T>::R<T>::f (); */
30722 if (parser
->num_template_parameter_lists
< num_templates
)
30724 if (declarator
&& !current_function_decl
)
30725 error_at (location
, "specializing member %<%T::%E%> "
30726 "requires %<template<>%> syntax",
30727 declarator
->u
.id
.qualifying_scope
,
30728 declarator
->u
.id
.unqualified_name
);
30729 else if (declarator
)
30730 error_at (location
, "invalid declaration of %<%T::%E%>",
30731 declarator
->u
.id
.qualifying_scope
,
30732 declarator
->u
.id
.unqualified_name
);
30734 error_at (location
, "too few template-parameter-lists");
30737 /* Otherwise, there are too many template parameter lists. We have
30740 template <class T> template <class U> void S::f(); */
30741 error_at (location
, "too many template-parameter-lists");
30745 /* Parse an optional `::' token indicating that the following name is
30746 from the global namespace. If so, PARSER->SCOPE is set to the
30747 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
30748 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
30749 Returns the new value of PARSER->SCOPE, if the `::' token is
30750 present, and NULL_TREE otherwise. */
30753 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
30757 /* Peek at the next token. */
30758 token
= cp_lexer_peek_token (parser
->lexer
);
30759 /* If we're looking at a `::' token then we're starting from the
30760 global namespace, not our current location. */
30761 if (token
->type
== CPP_SCOPE
)
30763 /* Consume the `::' token. */
30764 cp_lexer_consume_token (parser
->lexer
);
30765 /* Set the SCOPE so that we know where to start the lookup. */
30766 parser
->scope
= global_namespace
;
30767 parser
->qualifying_scope
= global_namespace
;
30768 parser
->object_scope
= NULL_TREE
;
30770 return parser
->scope
;
30772 else if (!current_scope_valid_p
)
30774 parser
->scope
= NULL_TREE
;
30775 parser
->qualifying_scope
= NULL_TREE
;
30776 parser
->object_scope
= NULL_TREE
;
30782 /* Returns TRUE if the upcoming token sequence is the start of a
30783 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
30784 declarator is preceded by the `friend' specifier. The parser flags FLAGS
30785 is used to control type-specifier parsing. */
30788 cp_parser_constructor_declarator_p (cp_parser
*parser
, cp_parser_flags flags
,
30791 bool constructor_p
;
30792 bool outside_class_specifier_p
;
30793 tree nested_name_specifier
;
30794 cp_token
*next_token
;
30796 /* The common case is that this is not a constructor declarator, so
30797 try to avoid doing lots of work if at all possible. It's not
30798 valid declare a constructor at function scope. */
30799 if (parser
->in_function_body
)
30801 /* And only certain tokens can begin a constructor declarator. */
30802 next_token
= cp_lexer_peek_token (parser
->lexer
);
30803 if (next_token
->type
!= CPP_NAME
30804 && next_token
->type
!= CPP_SCOPE
30805 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
30806 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
30807 declarator-id of a constructor or destructor. */
30808 && (next_token
->type
!= CPP_TEMPLATE_ID
|| cxx_dialect
>= cxx20
))
30811 /* Parse tentatively; we are going to roll back all of the tokens
30813 cp_parser_parse_tentatively (parser
);
30814 /* Assume that we are looking at a constructor declarator. */
30815 constructor_p
= true;
30817 /* Look for the optional `::' operator. */
30818 cp_parser_global_scope_opt (parser
,
30819 /*current_scope_valid_p=*/false);
30820 /* Look for the nested-name-specifier. */
30821 nested_name_specifier
30822 = (cp_parser_nested_name_specifier_opt (parser
,
30823 /*typename_keyword_p=*/false,
30824 /*check_dependency_p=*/false,
30826 /*is_declaration=*/false));
30828 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
30829 if (nested_name_specifier
30830 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
30832 tree s
= resolve_typename_type (nested_name_specifier
,
30833 /*only_current_p=*/false);
30834 if (TREE_CODE (s
) != TYPENAME_TYPE
)
30835 nested_name_specifier
= s
;
30838 outside_class_specifier_p
= (!at_class_scope_p ()
30839 || !TYPE_BEING_DEFINED (current_class_type
)
30842 /* Outside of a class-specifier, there must be a
30843 nested-name-specifier. Except in C++17 mode, where we
30844 might be declaring a guiding declaration. */
30845 if (!nested_name_specifier
&& outside_class_specifier_p
30846 && cxx_dialect
< cxx17
)
30847 constructor_p
= false;
30848 else if (nested_name_specifier
== error_mark_node
)
30849 constructor_p
= false;
30851 /* If we have a class scope, this is easy; DR 147 says that S::S always
30852 names the constructor, and no other qualified name could. */
30853 if (constructor_p
&& nested_name_specifier
30854 && CLASS_TYPE_P (nested_name_specifier
))
30856 tree id
= cp_parser_unqualified_id (parser
,
30857 /*template_keyword_p=*/false,
30858 /*check_dependency_p=*/false,
30859 /*declarator_p=*/true,
30860 /*optional_p=*/false);
30861 if (is_overloaded_fn (id
))
30862 id
= DECL_NAME (get_first_fn (id
));
30863 if (!constructor_name_p (id
, nested_name_specifier
))
30864 constructor_p
= false;
30866 /* If we still think that this might be a constructor-declarator,
30867 look for a class-name. */
30868 else if (constructor_p
)
30872 template <typename T> struct S {
30876 we must recognize that the nested `S' names a class. */
30877 if (cxx_dialect
>= cxx17
)
30878 cp_parser_parse_tentatively (parser
);
30881 type_decl
= cp_parser_class_name (parser
,
30882 /*typename_keyword_p=*/false,
30883 /*template_keyword_p=*/false,
30885 /*check_dependency_p=*/false,
30886 /*class_head_p=*/false,
30887 /*is_declaration=*/false);
30889 if (cxx_dialect
>= cxx17
30890 && !cp_parser_parse_definitely (parser
))
30892 type_decl
= NULL_TREE
;
30893 tree tmpl
= cp_parser_template_name (parser
,
30894 /*template_keyword*/false,
30895 /*check_dependency_p*/false,
30896 /*is_declaration*/false,
30898 /*is_identifier*/NULL
);
30899 if (DECL_CLASS_TEMPLATE_P (tmpl
)
30900 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
30901 /* It's a deduction guide, return true. */;
30903 cp_parser_simulate_error (parser
);
30906 /* If there was no class-name, then this is not a constructor.
30907 Otherwise, if we are in a class-specifier and we aren't
30908 handling a friend declaration, check that its type matches
30909 current_class_type (c++/38313). Note: error_mark_node
30910 is left alone for error recovery purposes. */
30911 constructor_p
= (!cp_parser_error_occurred (parser
)
30912 && (outside_class_specifier_p
30913 || type_decl
== NULL_TREE
30914 || type_decl
== error_mark_node
30915 || same_type_p (current_class_type
,
30916 TREE_TYPE (type_decl
))));
30918 /* If we're still considering a constructor, we have to see a `(',
30919 to begin the parameter-declaration-clause, followed by either a
30920 `)', an `...', or a decl-specifier. We need to check for a
30921 type-specifier to avoid being fooled into thinking that:
30925 is a constructor. (It is actually a function named `f' that
30926 takes one parameter (of type `int') and returns a value of type
30929 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30930 constructor_p
= false;
30933 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
30934 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
30935 /* A parameter declaration begins with a decl-specifier,
30936 which is either the "attribute" keyword, a storage class
30937 specifier, or (usually) a type-specifier. */
30938 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
30939 /* GNU attributes can actually appear both at the start of
30940 a parameter and parenthesized declarator.
30941 S (__attribute__((unused)) int);
30942 is a constructor, but
30943 S (__attribute__((unused)) foo) (int);
30944 is a function declaration. [[attribute]] can appear in the
30945 first form too, but not in the second form. */
30946 && !cp_next_tokens_can_be_std_attribute_p (parser
))
30949 tree pushed_scope
= NULL_TREE
;
30950 unsigned saved_num_template_parameter_lists
;
30952 if (cp_parser_allow_gnu_extensions_p (parser
)
30953 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
30955 unsigned int n
= cp_parser_skip_gnu_attributes_opt (parser
, 1);
30957 cp_lexer_consume_token (parser
->lexer
);
30960 /* Names appearing in the type-specifier should be looked up
30961 in the scope of the class. */
30962 if (current_class_type
)
30964 else if (type_decl
)
30966 type
= TREE_TYPE (type_decl
);
30967 if (TREE_CODE (type
) == TYPENAME_TYPE
)
30969 type
= resolve_typename_type (type
,
30970 /*only_current_p=*/false);
30971 if (TREE_CODE (type
) == TYPENAME_TYPE
)
30973 cp_parser_abort_tentative_parse (parser
);
30977 pushed_scope
= push_scope (type
);
30980 /* Inside the constructor parameter list, surrounding
30981 template-parameter-lists do not apply. */
30982 saved_num_template_parameter_lists
30983 = parser
->num_template_parameter_lists
;
30984 parser
->num_template_parameter_lists
= 0;
30986 /* Look for the type-specifier. It's not optional, but its typename
30987 might be. Unless this is a friend declaration; we don't want to
30990 friend S (T::fn)(int);
30992 as a constructor, but with P0634, we might assume a type when
30993 looking for the type-specifier. It is actually a function named
30994 `T::fn' that takes one parameter (of type `int') and returns a
30995 value of type `S'. Constructors can be friends, but they must
30996 use a qualified name.
30998 Parse with an empty set of declaration specifiers since we're
30999 trying to match a decl-specifier-seq of the first parameter.
31000 This must be non-null so that cp_parser_simple_type_specifier
31001 will recognize a constrained placeholder type such as:
31002 'C<int> auto' where C is a type concept. */
31003 cp_decl_specifier_seq ctor_specs
;
31004 clear_decl_specs (&ctor_specs
);
31005 cp_parser_type_specifier (parser
,
31006 (friend_p
? CP_PARSER_FLAGS_NONE
31007 : (flags
& ~CP_PARSER_FLAGS_OPTIONAL
)),
31008 /*decl_specs=*/&ctor_specs
,
31009 /*is_declarator=*/true,
31010 /*declares_class_or_enum=*/NULL
,
31011 /*is_cv_qualifier=*/NULL
);
31013 parser
->num_template_parameter_lists
31014 = saved_num_template_parameter_lists
;
31016 /* Leave the scope of the class. */
31018 pop_scope (pushed_scope
);
31020 constructor_p
= !cp_parser_error_occurred (parser
);
31024 /* We did not really want to consume any tokens. */
31025 cp_parser_abort_tentative_parse (parser
);
31027 return constructor_p
;
31030 /* Parse the definition of the function given by the DECL_SPECIFIERS,
31031 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
31032 they must be performed once we are in the scope of the function.
31034 Returns the function defined. */
31037 cp_parser_function_definition_from_specifiers_and_declarator
31038 (cp_parser
* parser
,
31039 cp_decl_specifier_seq
*decl_specifiers
,
31041 const cp_declarator
*declarator
)
31046 /* Begin the function-definition. */
31047 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
31049 /* The things we're about to see are not directly qualified by any
31050 template headers we've seen thus far. */
31051 reset_specialization ();
31053 /* If there were names looked up in the decl-specifier-seq that we
31054 did not check, check them now. We must wait until we are in the
31055 scope of the function to perform the checks, since the function
31056 might be a friend. */
31057 perform_deferred_access_checks (tf_warning_or_error
);
31061 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
31062 parser
->omp_declare_simd
= NULL
;
31063 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
31064 parser
->oacc_routine
= NULL
;
31069 /* Skip the entire function. */
31070 cp_parser_skip_to_end_of_block_or_statement (parser
);
31071 fn
= error_mark_node
;
31073 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
31075 /* Seen already, skip it. An error message has already been output. */
31076 cp_parser_skip_to_end_of_block_or_statement (parser
);
31077 fn
= current_function_decl
;
31078 current_function_decl
= NULL_TREE
;
31079 /* If this is a function from a class, pop the nested class. */
31080 if (current_class_name
)
31081 pop_nested_class ();
31086 if (DECL_DECLARED_INLINE_P (current_function_decl
))
31087 tv
= TV_PARSE_INLINE
;
31089 tv
= TV_PARSE_FUNC
;
31091 fn
= cp_parser_function_definition_after_declarator (parser
,
31092 /*inline_p=*/false);
31099 /* Parse the part of a function-definition that follows the
31100 declarator. INLINE_P is TRUE iff this function is an inline
31101 function defined within a class-specifier.
31103 Returns the function defined. */
31106 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
31110 bool saved_in_unbraced_linkage_specification_p
;
31111 bool saved_in_function_body
;
31112 unsigned saved_num_template_parameter_lists
;
31114 bool fully_implicit_function_template_p
31115 = parser
->fully_implicit_function_template_p
;
31116 parser
->fully_implicit_function_template_p
= false;
31117 tree implicit_template_parms
31118 = parser
->implicit_template_parms
;
31119 parser
->implicit_template_parms
= 0;
31120 cp_binding_level
* implicit_template_scope
31121 = parser
->implicit_template_scope
;
31122 parser
->implicit_template_scope
= 0;
31124 saved_in_function_body
= parser
->in_function_body
;
31125 parser
->in_function_body
= true;
31126 /* If the next token is `return', then the code may be trying to
31127 make use of the "named return value" extension that G++ used to
31129 token
= cp_lexer_peek_token (parser
->lexer
);
31130 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
31132 /* Consume the `return' keyword. */
31133 cp_lexer_consume_token (parser
->lexer
);
31134 /* Look for the identifier that indicates what value is to be
31136 cp_parser_identifier (parser
);
31137 /* Issue an error message. */
31138 error_at (token
->location
,
31139 "named return values are no longer supported");
31140 /* Skip tokens until we reach the start of the function body. */
31143 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31144 if (token
->type
== CPP_OPEN_BRACE
31145 || token
->type
== CPP_EOF
31146 || token
->type
== CPP_PRAGMA_EOL
)
31148 cp_lexer_consume_token (parser
->lexer
);
31151 /* The `extern' in `extern "C" void f () { ... }' does not apply to
31152 anything declared inside `f'. */
31153 saved_in_unbraced_linkage_specification_p
31154 = parser
->in_unbraced_linkage_specification_p
;
31155 parser
->in_unbraced_linkage_specification_p
= false;
31156 /* Inside the function, surrounding template-parameter-lists do not
31158 saved_num_template_parameter_lists
31159 = parser
->num_template_parameter_lists
;
31160 parser
->num_template_parameter_lists
= 0;
31162 /* If the next token is `try', `__transaction_atomic', or
31163 `__transaction_relaxed`, then we are looking at either function-try-block
31164 or function-transaction-block. Note that all of these include the
31166 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
31167 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
31168 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
31169 RID_TRANSACTION_RELAXED
))
31170 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
31171 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
31172 cp_parser_function_try_block (parser
);
31174 cp_parser_ctor_initializer_opt_and_function_body
31175 (parser
, /*in_function_try_block=*/false);
31177 /* Finish the function. */
31178 fn
= finish_function (inline_p
);
31182 && TYPE_P (DECL_CONTEXT (fn
))
31183 && (DECL_DECLARED_INLINE_P (fn
)
31184 || processing_template_decl
))
31185 set_defining_module (fn
);
31187 /* Generate code for it, if necessary. */
31188 expand_or_defer_fn (fn
);
31189 /* Restore the saved values. */
31190 parser
->in_unbraced_linkage_specification_p
31191 = saved_in_unbraced_linkage_specification_p
;
31192 parser
->num_template_parameter_lists
31193 = saved_num_template_parameter_lists
;
31194 parser
->in_function_body
= saved_in_function_body
;
31196 parser
->fully_implicit_function_template_p
31197 = fully_implicit_function_template_p
;
31198 parser
->implicit_template_parms
31199 = implicit_template_parms
;
31200 parser
->implicit_template_scope
31201 = implicit_template_scope
;
31203 if (parser
->fully_implicit_function_template_p
)
31204 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
31209 /* Parse a template-declaration body (following argument list). */
31212 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
31213 tree parameter_list
,
31216 tree decl
= NULL_TREE
;
31217 bool friend_p
= false;
31219 /* We just processed one more parameter list. */
31220 ++parser
->num_template_parameter_lists
;
31222 /* Get the deferred access checks from the parameter list. These
31223 will be checked once we know what is being declared, as for a
31224 member template the checks must be performed in the scope of the
31225 class containing the member. */
31226 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
31228 /* Tentatively parse for a new template parameter list, which can either be
31229 the template keyword or a template introduction. */
31230 if (cp_parser_template_declaration_after_export (parser
, member_p
))
31232 else if (cxx_dialect
>= cxx11
31233 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
31234 decl
= cp_parser_alias_declaration (parser
);
31235 else if (cxx_dialect
>= cxx20
/* Implies flag_concept. */
31236 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
)
31237 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_BOOL
))
31238 /* Allow 'concept bool' to be handled as per the TS. */
31239 decl
= cp_parser_concept_definition (parser
);
31242 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31243 decl
= cp_parser_single_declaration (parser
,
31246 /*explicit_specialization_p=*/false,
31249 /* If this is a member template declaration, let the front
31251 if (member_p
&& !friend_p
&& decl
)
31253 if (TREE_CODE (decl
) == TYPE_DECL
)
31254 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
31256 decl
= finish_member_template_decl (decl
);
31258 else if (friend_p
&& decl
31259 && DECL_DECLARES_TYPE_P (decl
))
31260 make_friend_class (current_class_type
, TREE_TYPE (decl
),
31261 /*complain=*/true);
31263 /* We are done with the current parameter list. */
31264 --parser
->num_template_parameter_lists
;
31266 pop_deferring_access_checks ();
31269 finish_template_decl (parameter_list
);
31271 /* Check the template arguments for a literal operator template. */
31273 && DECL_DECLARES_FUNCTION_P (decl
)
31274 && UDLIT_OPER_P (DECL_NAME (decl
)))
31277 if (parameter_list
== NULL_TREE
)
31281 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
31282 if (num_parms
== 1)
31284 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
31285 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
31286 if (TREE_CODE (parm
) != PARM_DECL
)
31288 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm
))
31289 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
31290 /* OK, C++20 string literal operator template. We don't need
31291 to warn in lower dialects here because we will have already
31292 warned about the template parameter. */;
31293 else if (TREE_TYPE (parm
) != char_type_node
31294 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
31297 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
31299 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
31300 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
31301 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
31302 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
31303 if (TREE_CODE (parm
) != PARM_DECL
31304 || TREE_TYPE (parm
) != TREE_TYPE (type
)
31305 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
31308 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
31309 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
31310 "ISO C++ did not adopt string literal operator templa"
31311 "tes taking an argument pack of characters");
31318 if (cxx_dialect
> cxx17
)
31319 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
31320 "template %qD has invalid parameter list; expected "
31321 "non-type template parameter pack %<<char...>%> or "
31322 "single non-type parameter of class type",
31325 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
31326 "template %qD has invalid parameter list; expected "
31327 "non-type template parameter pack %<<char...>%>",
31332 /* Register member declarations. */
31333 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
31334 finish_member_declaration (decl
);
31335 /* If DECL is a function template, we must return to parse it later.
31336 (Even though there is no definition, there might be default
31337 arguments that need handling.) */
31338 if (member_p
&& decl
31339 && DECL_DECLARES_FUNCTION_P (decl
))
31340 vec_safe_push (unparsed_funs_with_definitions
, decl
);
31343 /* Parse a template introduction header for a template-declaration. Returns
31344 false if tentative parse fails. */
31347 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
31349 cp_parser_parse_tentatively (parser
);
31351 tree saved_scope
= parser
->scope
;
31352 tree saved_object_scope
= parser
->object_scope
;
31353 tree saved_qualifying_scope
= parser
->qualifying_scope
;
31355 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
31357 /* Look for the optional `::' operator. */
31358 cp_parser_global_scope_opt (parser
,
31359 /*current_scope_valid_p=*/false);
31360 /* Look for the nested-name-specifier. */
31361 cp_parser_nested_name_specifier_opt (parser
,
31362 /*typename_keyword_p=*/false,
31363 /*check_dependency_p=*/true,
31365 /*is_declaration=*/false);
31367 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31368 tree concept_name
= cp_parser_identifier (parser
);
31370 /* Look up the concept for which we will be matching
31371 template parameters. */
31372 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
31374 parser
->scope
= saved_scope
;
31375 parser
->object_scope
= saved_object_scope
;
31376 parser
->qualifying_scope
= saved_qualifying_scope
;
31378 if (concept_name
== error_mark_node
31379 || (seen_error () && !concept_definition_p (tmpl_decl
)))
31380 cp_parser_simulate_error (parser
);
31382 /* Look for opening brace for introduction. */
31383 matching_braces braces
;
31384 braces
.require_open (parser
);
31385 location_t open_loc
= input_location
;
31387 if (!cp_parser_parse_definitely (parser
))
31390 push_deferring_access_checks (dk_deferred
);
31392 /* Build vector of placeholder parameters and grab
31393 matching identifiers. */
31394 tree introduction_list
= cp_parser_introduction_list (parser
);
31396 /* Look for closing brace for introduction. */
31397 if (!braces
.require_close (parser
))
31400 /* The introduction-list shall not be empty. */
31401 int nargs
= TREE_VEC_LENGTH (introduction_list
);
31404 /* In cp_parser_introduction_list we have already issued an error. */
31408 if (tmpl_decl
== error_mark_node
)
31410 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
31415 /* Build and associate the constraint. */
31416 location_t introduction_loc
= make_location (open_loc
,
31417 start_token
->location
,
31419 tree parms
= finish_template_introduction (tmpl_decl
,
31422 if (parms
&& parms
!= error_mark_node
)
31424 if (!flag_concepts_ts
)
31425 pedwarn (introduction_loc
, 0, "template-introductions"
31426 " are not part of C++20 concepts; use %qs to enable",
31429 cp_parser_template_declaration_after_parameters (parser
, parms
,
31434 if (parms
== NULL_TREE
)
31435 error_at (token
->location
, "no matching concept for template-introduction");
31440 /* Parse a normal template-declaration following the template keyword. */
31443 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
31445 tree parameter_list
;
31446 bool need_lang_pop
;
31447 location_t location
= input_location
;
31449 /* Look for the `<' token. */
31450 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
31452 if (at_class_scope_p () && current_function_decl
)
31454 /* 14.5.2.2 [temp.mem]
31456 A local class shall not have member templates. */
31457 error_at (location
,
31458 "invalid declaration of member template in local class");
31459 cp_parser_skip_to_end_of_block_or_statement (parser
);
31464 A template ... shall not have C linkage. */
31465 if (current_lang_name
== lang_name_c
)
31467 error_at (location
, "template with C linkage");
31468 maybe_show_extern_c_location ();
31469 /* Give it C++ linkage to avoid confusing other parts of the
31471 push_lang_context (lang_name_cplusplus
);
31472 need_lang_pop
= true;
31475 need_lang_pop
= false;
31477 /* We cannot perform access checks on the template parameter
31478 declarations until we know what is being declared, just as we
31479 cannot check the decl-specifier list. */
31480 push_deferring_access_checks (dk_deferred
);
31482 /* If the next token is `>', then we have an invalid
31483 specialization. Rather than complain about an invalid template
31484 parameter, issue an error message here. */
31485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
31487 cp_parser_error (parser
, "invalid explicit specialization");
31488 begin_specialization ();
31489 parameter_list
= NULL_TREE
;
31493 /* Parse the template parameters. */
31494 parameter_list
= cp_parser_template_parameter_list (parser
);
31497 /* Look for the `>'. */
31498 cp_parser_skip_to_end_of_template_parameter_list (parser
);
31500 /* Manage template requirements */
31503 tree reqs
= get_shorthand_constraints (current_template_parms
);
31504 if (tree treqs
= cp_parser_requires_clause_opt (parser
, false))
31505 reqs
= combine_constraint_expressions (reqs
, treqs
);
31506 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
31509 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
31512 /* For the erroneous case of a template with C linkage, we pushed an
31513 implicit C++ linkage scope; exit that scope now. */
31515 pop_lang_context ();
31518 /* Parse a template-declaration, assuming that the `export' (and
31519 `extern') keywords, if present, has already been scanned. MEMBER_P
31520 is as for cp_parser_template_declaration. */
31523 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
31525 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
31527 cp_lexer_consume_token (parser
->lexer
);
31528 cp_parser_explicit_template_declaration (parser
, member_p
);
31531 else if (flag_concepts
)
31532 return cp_parser_template_introduction (parser
, member_p
);
31537 /* Perform the deferred access checks from a template-parameter-list.
31538 CHECKS is a TREE_LIST of access checks, as returned by
31539 get_deferred_access_checks. */
31542 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
31544 ++processing_template_parmlist
;
31545 perform_access_checks (checks
, tf_warning_or_error
);
31546 --processing_template_parmlist
;
31549 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
31550 `function-definition' sequence that follows a template header.
31551 If MEMBER_P is true, this declaration appears in a class scope.
31553 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
31554 *FRIEND_P is set to TRUE iff the declaration is a friend. */
31557 cp_parser_single_declaration (cp_parser
* parser
,
31558 vec
<deferred_access_check
, va_gc
> *checks
,
31560 bool explicit_specialization_p
,
31563 int declares_class_or_enum
;
31564 tree decl
= NULL_TREE
;
31565 cp_decl_specifier_seq decl_specifiers
;
31566 bool function_definition_p
= false;
31567 cp_token
*decl_spec_token_start
;
31569 /* This function is only used when processing a template
31571 gcc_assert (innermost_scope_kind () == sk_template_parms
31572 || innermost_scope_kind () == sk_template_spec
);
31574 /* Defer access checks until we know what is being declared. */
31575 push_deferring_access_checks (dk_deferred
);
31577 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
31579 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
31580 cp_parser_decl_specifier_seq (parser
,
31581 (CP_PARSER_FLAGS_OPTIONAL
31582 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
31584 &declares_class_or_enum
);
31586 cp_omp_declare_simd_data odsd
;
31587 if (decl_specifiers
.attributes
&& (flag_openmp
|| flag_openmp_simd
))
31588 cp_parser_handle_directive_omp_attributes (parser
,
31589 &decl_specifiers
.attributes
,
31593 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
31595 /* There are no template typedefs. */
31596 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
31598 error_at (decl_spec_token_start
->location
,
31599 "template declaration of %<typedef%>");
31600 decl
= error_mark_node
;
31603 /* Gather up the access checks that occurred the
31604 decl-specifier-seq. */
31605 stop_deferring_access_checks ();
31607 /* Check for the declaration of a template class. */
31608 if (declares_class_or_enum
)
31610 if (cp_parser_declares_only_class_p (parser
)
31611 || (declares_class_or_enum
& 2))
31613 /* If this is a declaration, but not a definition, associate
31614 any constraints with the type declaration. Constraints
31615 are associated with definitions in cp_parser_class_specifier. */
31616 if (declares_class_or_enum
== 1)
31617 associate_classtype_constraints (decl_specifiers
.type
);
31619 decl
= shadow_tag (&decl_specifiers
);
31624 friend template <typename T> struct A<T>::B;
31627 A<T>::B will be represented by a TYPENAME_TYPE, and
31628 therefore not recognized by shadow_tag. */
31629 if (friend_p
&& *friend_p
31631 && decl_specifiers
.type
31632 && TYPE_P (decl_specifiers
.type
))
31633 decl
= decl_specifiers
.type
;
31635 if (decl
&& decl
!= error_mark_node
)
31636 decl
= TYPE_NAME (decl
);
31638 decl
= error_mark_node
;
31640 /* Perform access checks for template parameters. */
31641 cp_parser_perform_template_parameter_access_checks (checks
);
31643 /* Give a helpful diagnostic for
31644 template <class T> struct A { } a;
31645 if we aren't already recovering from an error. */
31646 if (!cp_parser_declares_only_class_p (parser
)
31649 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31650 "a class template declaration must not declare "
31652 cp_parser_skip_to_end_of_block_or_statement (parser
);
31658 /* Complain about missing 'typename' or other invalid type names. */
31659 if (!decl_specifiers
.any_type_specifiers_p
31660 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
31662 /* cp_parser_parse_and_diagnose_invalid_type_name calls
31663 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
31664 the rest of this declaration. */
31665 decl
= error_mark_node
;
31669 /* If it's not a template class, try for a template function. If
31670 the next token is a `;', then this declaration does not declare
31671 anything. But, if there were errors in the decl-specifiers, then
31672 the error might well have come from an attempted class-specifier.
31673 In that case, there's no need to warn about a missing declarator. */
31675 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
31676 || decl_specifiers
.type
!= error_mark_node
))
31678 int flags
= CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
31679 /* We don't delay parsing for friends, though CWG 2510 may change
31681 if (member_p
&& !(friend_p
&& *friend_p
))
31682 flags
|= CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
31683 decl
= cp_parser_init_declarator (parser
,
31687 /*function_definition_allowed_p=*/true,
31689 declares_class_or_enum
,
31690 &function_definition_p
,
31693 /* 7.1.1-1 [dcl.stc]
31695 A storage-class-specifier shall not be specified in an explicit
31696 specialization... */
31698 && explicit_specialization_p
31699 && decl_specifiers
.storage_class
!= sc_none
)
31701 error_at (decl_spec_token_start
->location
,
31702 "explicit template specialization cannot have a storage class");
31703 decl
= error_mark_node
;
31706 if (decl
&& VAR_P (decl
))
31707 check_template_variable (decl
);
31710 /* Look for a trailing `;' after the declaration. */
31711 if (!function_definition_p
31712 && (decl
== error_mark_node
31713 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
31714 cp_parser_skip_to_end_of_block_or_statement (parser
);
31717 pop_deferring_access_checks ();
31719 /* Clear any current qualification; whatever comes next is the start
31720 of something new. */
31721 parser
->scope
= NULL_TREE
;
31722 parser
->qualifying_scope
= NULL_TREE
;
31723 parser
->object_scope
= NULL_TREE
;
31725 cp_finalize_omp_declare_simd (parser
, &odsd
);
31730 /* Parse a cast-expression that is not the operand of a unary "&". */
31733 cp_parser_simple_cast_expression (cp_parser
*parser
)
31735 return cp_parser_cast_expression (parser
, /*address_p=*/false,
31736 /*cast_p=*/false, /*decltype*/false, NULL
);
31739 /* Parse a functional cast to TYPE. Returns an expression
31740 representing the cast. */
31743 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
31745 vec
<tree
, va_gc
> *vec
;
31746 tree expression_list
;
31750 location_t start_loc
= input_location
;
31753 type
= error_mark_node
;
31755 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
31757 cp_lexer_set_source_position (parser
->lexer
);
31758 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
31759 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
31760 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
31761 if (TREE_CODE (type
) == TYPE_DECL
)
31762 type
= TREE_TYPE (type
);
31764 cast
= finish_compound_literal (type
, expression_list
,
31765 tf_warning_or_error
, fcl_functional
);
31766 /* Create a location of the form:
31769 with caret == start at the start of the type name,
31770 finishing at the closing brace. */
31771 location_t combined_loc
= make_location (start_loc
, start_loc
,
31773 cast
.set_location (combined_loc
);
31778 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31780 /*allow_expansion_p=*/true,
31781 /*non_constant_p=*/NULL
);
31783 expression_list
= error_mark_node
;
31786 expression_list
= build_tree_list_vec (vec
);
31787 release_tree_vector (vec
);
31790 /* Create a location of the form:
31793 with caret == start at the start of the type name,
31794 finishing at the closing paren. */
31795 location_t combined_loc
= make_location (start_loc
, start_loc
,
31797 cast
= build_functional_cast (combined_loc
, type
, expression_list
,
31798 tf_warning_or_error
);
31800 /* [expr.const]/1: In an integral constant expression "only type
31801 conversions to integral or enumeration type can be used". */
31802 if (TREE_CODE (type
) == TYPE_DECL
)
31803 type
= TREE_TYPE (type
);
31804 if (cast
!= error_mark_node
31805 && !cast_valid_in_integral_constant_expression_p (type
)
31806 && cp_parser_non_integral_constant_expression (parser
,
31808 return error_mark_node
;
31813 /* Save the tokens that make up the body of a member function defined
31814 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
31815 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
31816 specifiers applied to the declaration. Returns the FUNCTION_DECL
31817 for the member function. */
31820 cp_parser_save_member_function_body (cp_parser
* parser
,
31821 cp_decl_specifier_seq
*decl_specifiers
,
31822 cp_declarator
*declarator
,
31828 bool function_try_block
= false;
31830 /* Create the FUNCTION_DECL. */
31831 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
31832 cp_finalize_omp_declare_simd (parser
, fn
);
31833 cp_finalize_oacc_routine (parser
, fn
, true);
31834 /* If something went badly wrong, bail out now. */
31835 if (fn
== error_mark_node
)
31837 /* If there's a function-body, skip it. */
31838 if (cp_parser_token_starts_function_definition_p
31839 (cp_lexer_peek_token (parser
->lexer
)))
31840 cp_parser_skip_to_end_of_block_or_statement (parser
);
31841 return error_mark_node
;
31844 /* Remember it, if there are default args to post process. */
31845 cp_parser_save_default_args (parser
, fn
);
31847 /* Save away the tokens that make up the body of the
31849 first
= parser
->lexer
->next_token
;
31851 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
31852 cp_lexer_consume_token (parser
->lexer
);
31853 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
31854 RID_TRANSACTION_ATOMIC
))
31856 cp_lexer_consume_token (parser
->lexer
);
31857 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
31858 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
31859 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
31860 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
31861 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
31862 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
31863 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
31865 cp_lexer_consume_token (parser
->lexer
);
31866 cp_lexer_consume_token (parser
->lexer
);
31867 cp_lexer_consume_token (parser
->lexer
);
31868 cp_lexer_consume_token (parser
->lexer
);
31869 cp_lexer_consume_token (parser
->lexer
);
31872 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
31873 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
31875 cp_lexer_consume_token (parser
->lexer
);
31876 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
31881 /* Handle function try blocks. */
31882 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
31884 cp_lexer_consume_token (parser
->lexer
);
31885 function_try_block
= true;
31887 /* We can have braced-init-list mem-initializers before the fn body. */
31888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31890 cp_lexer_consume_token (parser
->lexer
);
31891 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
31893 /* cache_group will stop after an un-nested { } pair, too. */
31894 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
31897 /* variadic mem-inits have ... after the ')'. */
31898 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31899 cp_lexer_consume_token (parser
->lexer
);
31902 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
31903 /* Handle function try blocks. */
31904 if (function_try_block
)
31905 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
31906 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
31907 last
= parser
->lexer
->next_token
;
31909 /* Save away the inline definition; we will process it when the
31910 class is complete. */
31911 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
31912 DECL_PENDING_INLINE_P (fn
) = 1;
31914 /* We need to know that this was defined in the class, so that
31915 friend templates are handled correctly. */
31916 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
31918 /* Add FN to the queue of functions to be parsed later. */
31919 vec_safe_push (unparsed_funs_with_definitions
, fn
);
31924 /* Save the tokens that make up the in-class initializer for a non-static
31925 data member. Returns a DEFERRED_PARSE. */
31928 cp_parser_save_nsdmi (cp_parser
* parser
)
31930 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
31933 /* Parse a template-argument-list, as well as the trailing ">" (but
31934 not the opening "<"). See cp_parser_template_argument_list for the
31938 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
31942 tree saved_qualifying_scope
;
31943 tree saved_object_scope
;
31944 bool saved_greater_than_is_operator_p
;
31948 When parsing a template-id, the first non-nested `>' is taken as
31949 the end of the template-argument-list rather than a greater-than
31951 saved_greater_than_is_operator_p
31952 = parser
->greater_than_is_operator_p
;
31953 parser
->greater_than_is_operator_p
= false;
31954 /* Parsing the argument list may modify SCOPE, so we save it
31956 saved_scope
= parser
->scope
;
31957 saved_qualifying_scope
= parser
->qualifying_scope
;
31958 saved_object_scope
= parser
->object_scope
;
31959 /* We need to evaluate the template arguments, even though this
31960 template-id may be nested within a "sizeof". */
31962 /* Parse the template-argument-list itself. */
31963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
31964 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
31965 arguments
= NULL_TREE
;
31967 arguments
= cp_parser_template_argument_list (parser
);
31968 /* Look for the `>' that ends the template-argument-list. If we find
31969 a '>>' instead, it's probably just a typo. */
31970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
31972 if (cxx_dialect
!= cxx98
)
31974 /* In C++0x, a `>>' in a template argument list or cast
31975 expression is considered to be two separate `>'
31976 tokens. So, change the current token to a `>', but don't
31977 consume it: it will be consumed later when the outer
31978 template argument list (or cast expression) is parsed.
31979 Note that this replacement of `>' for `>>' is necessary
31980 even if we are parsing tentatively: in the tentative
31981 case, after calling
31982 cp_parser_enclosed_template_argument_list we will always
31983 throw away all of the template arguments and the first
31984 closing `>', either because the template argument list
31985 was erroneous or because we are replacing those tokens
31986 with a CPP_TEMPLATE_ID token. The second `>' (which will
31987 not have been thrown away) is needed either to close an
31988 outer template argument list or to complete a new-style
31990 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31991 token
->type
= CPP_GREATER
;
31993 else if (!saved_greater_than_is_operator_p
)
31995 /* If we're in a nested template argument list, the '>>' has
31996 to be a typo for '> >'. We emit the error message, but we
31997 continue parsing and we push a '>' as next token, so that
31998 the argument list will be parsed correctly. Note that the
31999 global source location is still on the token before the
32000 '>>', so we need to say explicitly where we want it. */
32001 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32002 gcc_rich_location
richloc (token
->location
);
32003 richloc
.add_fixit_replace ("> >");
32004 error_at (&richloc
, "%<>>%> should be %<> >%> "
32005 "within a nested template argument list");
32007 token
->type
= CPP_GREATER
;
32011 /* If this is not a nested template argument list, the '>>'
32012 is a typo for '>'. Emit an error message and continue.
32013 Same deal about the token location, but here we can get it
32014 right by consuming the '>>' before issuing the diagnostic. */
32015 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
32016 error_at (token
->location
,
32017 "spurious %<>>%>, use %<>%> to terminate "
32018 "a template argument list");
32022 cp_parser_skip_to_end_of_template_parameter_list (parser
);
32023 /* The `>' token might be a greater-than operator again now. */
32024 parser
->greater_than_is_operator_p
32025 = saved_greater_than_is_operator_p
;
32026 /* Restore the SAVED_SCOPE. */
32027 parser
->scope
= saved_scope
;
32028 parser
->qualifying_scope
= saved_qualifying_scope
;
32029 parser
->object_scope
= saved_object_scope
;
32034 /* MEMBER_FUNCTION is a member function, or a friend. If default
32035 arguments, or the body of the function have not yet been parsed,
32039 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
32041 timevar_push (TV_PARSE_INMETH
);
32042 /* If this member is a template, get the underlying
32044 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
32045 member_function
= DECL_TEMPLATE_RESULT (member_function
);
32047 /* There should not be any class definitions in progress at this
32048 point; the bodies of members are only parsed outside of all class
32050 gcc_assert (parser
->num_classes_being_defined
== 0);
32051 /* While we're parsing the member functions we might encounter more
32052 classes. We want to handle them right away, but we don't want
32053 them getting mixed up with functions that are currently in the
32055 push_unparsed_function_queues (parser
);
32057 /* Make sure that any template parameters are in scope. */
32058 maybe_begin_member_template_processing (member_function
);
32060 /* If the body of the function has not yet been parsed, parse it
32062 if (DECL_PENDING_INLINE_P (member_function
))
32064 tree function_scope
;
32065 cp_token_cache
*tokens
;
32067 /* The function is no longer pending; we are processing it. */
32068 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
32069 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
32070 DECL_PENDING_INLINE_P (member_function
) = 0;
32072 /* If this is a local class, enter the scope of the containing
32074 function_scope
= current_function_decl
;
32075 if (function_scope
)
32076 push_function_context ();
32078 /* Push the body of the function onto the lexer stack. */
32079 cp_parser_push_lexer_for_tokens (parser
, tokens
);
32081 /* Let the front end know that we going to be defining this
32083 start_preparsed_function (member_function
, NULL_TREE
,
32084 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
32086 /* #pragma omp declare reduction needs special parsing. */
32087 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
32089 parser
->lexer
->in_pragma
= true;
32090 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
32091 finish_function (/*inline_p=*/true);
32092 cp_check_omp_declare_reduction (member_function
);
32095 /* Now, parse the body of the function. */
32096 cp_parser_function_definition_after_declarator (parser
,
32097 /*inline_p=*/true);
32099 /* Leave the scope of the containing function. */
32100 if (function_scope
)
32101 pop_function_context ();
32102 cp_parser_pop_lexer (parser
);
32105 /* Remove any template parameters from the symbol table. */
32106 maybe_end_member_template_processing ();
32108 /* Restore the queue. */
32109 pop_unparsed_function_queues (parser
);
32110 timevar_pop (TV_PARSE_INMETH
);
32113 /* If DECL contains any default args, remember it on the unparsed
32114 functions queue. */
32117 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
32121 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
32123 probe
= TREE_CHAIN (probe
))
32124 if (TREE_PURPOSE (probe
))
32126 cp_default_arg_entry entry
= {current_class_type
, decl
};
32127 vec_safe_push (unparsed_funs_with_default_args
, entry
);
32131 /* Remember if there is a noexcept-specifier to post process. */
32132 tree spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
32133 if (UNPARSED_NOEXCEPT_SPEC_P (spec
))
32134 vec_safe_push (unparsed_noexcepts
, decl
);
32137 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
32138 which is either a FIELD_DECL or PARM_DECL. Parse it and return
32139 the result. For a PARM_DECL, PARMTYPE is the corresponding type
32140 from the parameter-type-list. */
32143 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
32144 tree default_arg
, tree parmtype
)
32146 cp_token_cache
*tokens
;
32150 if (default_arg
== error_mark_node
)
32151 return error_mark_node
;
32153 /* Push the saved tokens for the default argument onto the parser's
32155 tokens
= DEFPARSE_TOKENS (default_arg
);
32156 cp_parser_push_lexer_for_tokens (parser
, tokens
);
32158 start_lambda_scope (decl
);
32160 /* Parse the default argument. */
32161 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
32162 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
32163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
32165 finish_lambda_scope ();
32167 if (parsed_arg
== error_mark_node
)
32168 cp_parser_skip_to_end_of_statement (parser
);
32170 if (!processing_template_decl
)
32172 /* In a non-template class, check conversions now. In a template,
32173 we'll wait and instantiate these as needed. */
32174 if (TREE_CODE (decl
) == PARM_DECL
)
32175 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
32176 tf_warning_or_error
);
32177 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
32178 parsed_arg
= error_mark_node
;
32180 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
32183 /* If the token stream has not been completely used up, then
32184 there was extra junk after the end of the default
32186 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
32188 if (TREE_CODE (decl
) == PARM_DECL
)
32189 cp_parser_error (parser
, "expected %<,%>");
32191 cp_parser_error (parser
, "expected %<;%>");
32194 /* Revert to the main lexer. */
32195 cp_parser_pop_lexer (parser
);
32200 /* FIELD is a non-static data member with an initializer which we saved for
32201 later; parse it now. */
32204 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
32208 maybe_begin_member_template_processing (field
);
32210 push_unparsed_function_queues (parser
);
32211 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
32212 DECL_INITIAL (field
),
32214 pop_unparsed_function_queues (parser
);
32216 maybe_end_member_template_processing ();
32218 DECL_INITIAL (field
) = def
;
32221 /* FN is a FUNCTION_DECL which may contains a parameter with an
32222 unparsed DEFERRED_PARSE. Parse the default args now. This function
32223 assumes that the current scope is the scope in which the default
32224 argument should be processed. */
32227 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
32229 unsigned char saved_local_variables_forbidden_p
;
32231 /* While we're parsing the default args, we might (due to the
32232 statement expression extension) encounter more classes. We want
32233 to handle them right away, but we don't want them getting mixed
32234 up with default args that are currently in the queue. */
32235 push_unparsed_function_queues (parser
);
32237 /* Local variable names (and the `this' keyword) may not appear
32238 in a default argument. */
32239 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
32240 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
32242 push_defarg_context (fn
);
32244 begin_scope (sk_function_parms
, fn
);
32246 /* Gather the PARM_DECLs into a vec so we can keep track of them when
32247 pushdecl clears DECL_CHAIN. */
32248 releasing_vec parms
;
32249 for (tree parmdecl
= DECL_ARGUMENTS (fn
); parmdecl
;
32250 parmdecl
= DECL_CHAIN (parmdecl
))
32251 vec_safe_push (parms
, parmdecl
);
32253 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
32255 parm
&& parm
!= void_list_node
;
32256 parm
= TREE_CHAIN (parm
),
32259 tree default_arg
= TREE_PURPOSE (parm
);
32262 tree parmdecl
= parms
[i
];
32263 pushdecl (parmdecl
);
32268 if (TREE_CODE (default_arg
) != DEFERRED_PARSE
)
32269 /* This can happen for a friend declaration for a function
32270 already declared with default arguments. */
32274 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
32276 TREE_VALUE (parm
));
32277 TREE_PURPOSE (parm
) = parsed_arg
;
32279 /* Update any instantiations we've already created. */
32280 for (tree copy
: DEFPARSE_INSTANTIATIONS (default_arg
))
32281 TREE_PURPOSE (copy
) = parsed_arg
;
32284 pop_bindings_and_leave_scope ();
32286 /* Restore DECL_CHAINs after clobbering by pushdecl. */
32288 for (int i
= parms
->length () - 1; i
>= 0; --i
)
32290 DECL_CHAIN (parms
[i
]) = parm
;
32294 pop_defarg_context ();
32296 /* Make sure no default arg is missing. */
32297 check_default_args (fn
);
32299 /* Restore the state of local_variables_forbidden_p. */
32300 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
32302 /* Restore the queue. */
32303 pop_unparsed_function_queues (parser
);
32306 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
32308 sizeof ... ( identifier )
32310 where the 'sizeof' token has already been consumed. */
32313 cp_parser_sizeof_pack (cp_parser
*parser
)
32315 /* Consume the `...'. */
32316 cp_lexer_consume_token (parser
->lexer
);
32317 maybe_warn_variadic_templates ();
32319 matching_parens parens
;
32320 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
32322 parens
.consume_open (parser
);
32324 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
32325 "%<sizeof...%> argument must be surrounded by parentheses");
32327 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32328 tree name
= cp_parser_identifier (parser
);
32329 if (name
== error_mark_node
)
32330 return error_mark_node
;
32331 /* The name is not qualified. */
32332 parser
->scope
= NULL_TREE
;
32333 parser
->qualifying_scope
= NULL_TREE
;
32334 parser
->object_scope
= NULL_TREE
;
32335 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
32336 if (expr
== error_mark_node
)
32337 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
32339 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
32340 expr
= TREE_TYPE (expr
);
32341 else if (TREE_CODE (expr
) == CONST_DECL
)
32342 expr
= DECL_INITIAL (expr
);
32343 expr
= make_pack_expansion (expr
);
32344 PACK_EXPANSION_SIZEOF_P (expr
) = true;
32347 parens
.require_close (parser
);
32352 /* Parse the operand of `sizeof' (or a similar operator). Returns
32353 either a TYPE or an expression, depending on the form of the
32354 input. The KEYWORD indicates which kind of expression we have
32358 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
32360 tree expr
= NULL_TREE
;
32361 const char *saved_message
;
32362 const char *saved_message_arg
;
32363 bool saved_integral_constant_expression_p
;
32364 bool saved_non_integral_constant_expression_p
;
32366 /* If it's a `...', then we are computing the length of a parameter
32368 if (keyword
== RID_SIZEOF
32369 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
32370 return cp_parser_sizeof_pack (parser
);
32372 /* Types cannot be defined in a `sizeof' expression. Save away the
32374 saved_message
= parser
->type_definition_forbidden_message
;
32375 saved_message_arg
= parser
->type_definition_forbidden_message_arg
;
32376 parser
->type_definition_forbidden_message
32377 = G_("types may not be defined in %qs expressions");
32378 parser
->type_definition_forbidden_message_arg
32379 = IDENTIFIER_POINTER (ridpointers
[keyword
]);
32381 /* The restrictions on constant-expressions do not apply inside
32382 sizeof expressions. */
32383 saved_integral_constant_expression_p
32384 = parser
->integral_constant_expression_p
;
32385 saved_non_integral_constant_expression_p
32386 = parser
->non_integral_constant_expression_p
;
32387 parser
->integral_constant_expression_p
= false;
32389 auto cleanup
= make_temp_override
32390 (parser
->auto_is_implicit_function_template_parm_p
, false);
32392 /* Do not actually evaluate the expression. */
32393 ++cp_unevaluated_operand
;
32394 ++c_inhibit_evaluation_warnings
;
32395 /* If it's a `(', then we might be looking at the type-id
32397 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32399 tree type
= NULL_TREE
;
32401 tentative_firewall
firewall (parser
);
32403 /* We can't be sure yet whether we're looking at a type-id or an
32405 cp_parser_parse_tentatively (parser
);
32407 matching_parens parens
;
32408 parens
.consume_open (parser
);
32410 /* Note: as a GNU Extension, compound literals are considered
32411 postfix-expressions as they are in C99, so they are valid
32412 arguments to sizeof. See comment in cp_parser_cast_expression
32414 if (cp_parser_compound_literal_p (parser
))
32415 cp_parser_simulate_error (parser
);
32418 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
32419 parser
->in_type_id_in_expr_p
= true;
32420 /* Look for the type-id. */
32421 type
= cp_parser_type_id (parser
);
32422 /* Look for the closing `)'. */
32423 parens
.require_close (parser
);
32424 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
32427 /* If all went well, then we're done. */
32428 if (cp_parser_parse_definitely (parser
))
32432 /* Commit to the tentative_firewall so we get syntax errors. */
32433 cp_parser_commit_to_tentative_parse (parser
);
32435 expr
= cp_parser_unary_expression (parser
);
32439 expr
= cp_parser_unary_expression (parser
);
32441 /* Go back to evaluating expressions. */
32442 --cp_unevaluated_operand
;
32443 --c_inhibit_evaluation_warnings
;
32445 /* And restore the old one. */
32446 parser
->type_definition_forbidden_message
= saved_message
;
32447 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
32448 parser
->integral_constant_expression_p
32449 = saved_integral_constant_expression_p
;
32450 parser
->non_integral_constant_expression_p
32451 = saved_non_integral_constant_expression_p
;
32456 /* If the current declaration has no declarator, return true. */
32459 cp_parser_declares_only_class_p (cp_parser
*parser
)
32461 /* If the next token is a `;' or a `,' then there is no
32463 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
32464 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
32467 /* Update the DECL_SPECS to reflect the storage class indicated by
32471 cp_parser_set_storage_class (cp_parser
*parser
,
32472 cp_decl_specifier_seq
*decl_specs
,
32476 cp_storage_class storage_class
;
32478 if (parser
->in_unbraced_linkage_specification_p
)
32480 error_at (token
->location
, "invalid use of %qD in linkage specification",
32481 ridpointers
[keyword
]);
32484 else if (decl_specs
->storage_class
!= sc_none
)
32486 decl_specs
->conflicting_specifiers_p
= true;
32490 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
32491 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
32492 && decl_specs
->gnu_thread_keyword_p
)
32494 pedwarn (decl_specs
->locations
[ds_thread
], 0,
32495 "%<__thread%> before %qD", ridpointers
[keyword
]);
32501 storage_class
= sc_auto
;
32504 storage_class
= sc_register
;
32507 storage_class
= sc_static
;
32510 storage_class
= sc_extern
;
32513 storage_class
= sc_mutable
;
32516 gcc_unreachable ();
32518 decl_specs
->storage_class
= storage_class
;
32519 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
32521 /* A storage class specifier cannot be applied alongside a typedef
32522 specifier. If there is a typedef specifier present then set
32523 conflicting_specifiers_p which will trigger an error later
32524 on in grokdeclarator. */
32525 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
32526 decl_specs
->conflicting_specifiers_p
= true;
32529 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
32530 is true, the type is a class or enum definition. */
32533 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
32536 bool type_definition_p
)
32538 decl_specs
->any_specifiers_p
= true;
32540 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
32541 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
32542 this is what happened. In system headers, we ignore these
32543 declarations so that G++ can work with system headers that are not
32545 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
32546 && !type_definition_p
32547 && (type_spec
== boolean_type_node
32548 || type_spec
== char8_type_node
32549 || type_spec
== char16_type_node
32550 || type_spec
== char32_type_node
32551 || type_spec
== wchar_type_node
)
32552 && (decl_specs
->type
32553 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
32554 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
32555 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
32556 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
32558 decl_specs
->redefined_builtin_type
= type_spec
;
32559 set_and_check_decl_spec_loc (decl_specs
,
32560 ds_redefined_builtin_type_spec
,
32562 if (!decl_specs
->type
)
32564 decl_specs
->type
= type_spec
;
32565 decl_specs
->type_definition_p
= false;
32566 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
32569 else if (decl_specs
->type
)
32570 decl_specs
->multiple_types_p
= true;
32573 decl_specs
->type
= type_spec
;
32574 decl_specs
->type_definition_p
= type_definition_p
;
32575 decl_specs
->redefined_builtin_type
= NULL_TREE
;
32576 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
32580 /* True iff TOKEN is the GNU keyword __thread. */
32583 token_is__thread (cp_token
*token
)
32585 gcc_assert (token
->keyword
== RID_THREAD
);
32586 return id_equal (token
->u
.value
, "__thread");
32589 /* Set the location for a declarator specifier and check if it is
32592 DECL_SPECS is the sequence of declarator specifiers onto which to
32595 DS is the single declarator specifier to set which location is to
32596 be set onto the existing sequence of declarators.
32598 LOCATION is the location for the declarator specifier to
32602 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
32603 cp_decl_spec ds
, cp_token
*token
)
32605 gcc_assert (ds
< ds_last
);
32607 if (decl_specs
== NULL
)
32610 location_t location
= token
->location
;
32612 if (decl_specs
->locations
[ds
] == 0)
32614 decl_specs
->locations
[ds
] = location
;
32615 if (ds
== ds_thread
)
32616 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
32622 if (decl_specs
->locations
[ds_long_long
] != 0)
32623 error_at (location
,
32624 "%<long long long%> is too long for GCC");
32627 decl_specs
->locations
[ds_long_long
] = location
;
32628 pedwarn_cxx98 (location
,
32630 "ISO C++ 1998 does not support %<long long%>");
32633 else if (ds
== ds_thread
)
32635 bool gnu
= token_is__thread (token
);
32636 gcc_rich_location
richloc (location
);
32637 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
32639 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
32640 error_at (&richloc
,
32641 "both %<__thread%> and %<thread_local%> specified");
32645 richloc
.add_fixit_remove ();
32646 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
32651 static const char *const decl_spec_names
[] = {
32670 gcc_rich_location
richloc (location
);
32671 richloc
.add_fixit_remove ();
32672 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
32677 /* Return true iff the declarator specifier DS is present in the
32678 sequence of declarator specifiers DECL_SPECS. */
32681 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
32684 gcc_assert (ds
< ds_last
);
32686 if (decl_specs
== NULL
)
32689 return decl_specs
->locations
[ds
] != 0;
32692 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
32693 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
32696 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
32698 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
32701 /* Issue an error message indicating that TOKEN_DESC was expected.
32702 If KEYWORD is true, it indicated this function is called by
32703 cp_parser_require_keword and the required token can only be
32704 a indicated keyword.
32706 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32707 within any error as the location of an "opening" token matching
32708 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32709 RT_CLOSE_PAREN). */
32712 cp_parser_required_error (cp_parser
*parser
,
32713 required_token token_desc
,
32715 location_t matching_location
)
32717 if (cp_parser_simulate_error (parser
))
32720 const char *gmsgid
= NULL
;
32721 switch (token_desc
)
32724 gmsgid
= G_("expected %<new%>");
32727 gmsgid
= G_("expected %<delete%>");
32730 gmsgid
= G_("expected %<return%>");
32733 gmsgid
= G_("expected %<while%>");
32736 gmsgid
= G_("expected %<extern%>");
32738 case RT_STATIC_ASSERT
:
32739 gmsgid
= G_("expected %<static_assert%>");
32742 gmsgid
= G_("expected %<decltype%>");
32745 gmsgid
= G_("expected %<operator%>");
32748 gmsgid
= G_("expected %<class%>");
32751 gmsgid
= G_("expected %<template%>");
32754 gmsgid
= G_("expected %<namespace%>");
32757 gmsgid
= G_("expected %<using%>");
32760 gmsgid
= G_("expected %<asm%>");
32763 gmsgid
= G_("expected %<try%>");
32766 gmsgid
= G_("expected %<catch%>");
32769 gmsgid
= G_("expected %<throw%>");
32772 gmsgid
= G_("expected %<auto%>");
32775 gmsgid
= G_("expected %<__label__%>");
32778 gmsgid
= G_("expected %<@try%>");
32780 case RT_AT_SYNCHRONIZED
:
32781 gmsgid
= G_("expected %<@synchronized%>");
32784 gmsgid
= G_("expected %<@throw%>");
32786 case RT_TRANSACTION_ATOMIC
:
32787 gmsgid
= G_("expected %<__transaction_atomic%>");
32789 case RT_TRANSACTION_RELAXED
:
32790 gmsgid
= G_("expected %<__transaction_relaxed%>");
32793 gmsgid
= G_("expected %<co_yield%>");
32799 if (!gmsgid
&& !keyword
)
32801 switch (token_desc
)
32804 gmsgid
= G_("expected %<;%>");
32806 case RT_OPEN_PAREN
:
32807 gmsgid
= G_("expected %<(%>");
32809 case RT_CLOSE_BRACE
:
32810 gmsgid
= G_("expected %<}%>");
32812 case RT_OPEN_BRACE
:
32813 gmsgid
= G_("expected %<{%>");
32815 case RT_CLOSE_SQUARE
:
32816 gmsgid
= G_("expected %<]%>");
32818 case RT_OPEN_SQUARE
:
32819 gmsgid
= G_("expected %<[%>");
32822 gmsgid
= G_("expected %<,%>");
32825 gmsgid
= G_("expected %<::%>");
32828 gmsgid
= G_("expected %<<%>");
32831 gmsgid
= G_("expected %<>%>");
32834 gmsgid
= G_("expected %<=%>");
32837 gmsgid
= G_("expected %<...%>");
32840 gmsgid
= G_("expected %<*%>");
32843 gmsgid
= G_("expected %<~%>");
32846 gmsgid
= G_("expected %<:%>");
32848 case RT_COLON_SCOPE
:
32849 gmsgid
= G_("expected %<:%> or %<::%>");
32851 case RT_CLOSE_PAREN
:
32852 gmsgid
= G_("expected %<)%>");
32854 case RT_COMMA_CLOSE_PAREN
:
32855 gmsgid
= G_("expected %<,%> or %<)%>");
32857 case RT_PRAGMA_EOL
:
32858 gmsgid
= G_("expected end of line");
32861 gmsgid
= G_("expected identifier");
32864 gmsgid
= G_("expected selection-statement");
32867 gmsgid
= G_("expected iteration-statement");
32870 gmsgid
= G_("expected jump-statement");
32873 gmsgid
= G_("expected class-key");
32875 case RT_CLASS_TYPENAME_TEMPLATE
:
32876 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
32879 gcc_unreachable ();
32884 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
32888 /* If the next token is of the indicated TYPE, consume it. Otherwise,
32889 issue an error message indicating that TOKEN_DESC was expected.
32891 Returns the token consumed, if the token had the appropriate type.
32892 Otherwise, returns NULL.
32894 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32895 within any error as the location of an "opening" token matching
32896 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32897 RT_CLOSE_PAREN). */
32900 cp_parser_require (cp_parser
* parser
,
32901 enum cpp_ttype type
,
32902 required_token token_desc
,
32903 location_t matching_location
)
32905 if (cp_lexer_next_token_is (parser
->lexer
, type
))
32906 return cp_lexer_consume_token (parser
->lexer
);
32909 /* Output the MESSAGE -- unless we're parsing tentatively. */
32910 if (!cp_parser_simulate_error (parser
))
32911 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
32912 matching_location
);
32917 /* An error message is produced if the next token is not '>'.
32918 All further tokens are skipped until the desired token is
32919 found or '{', '}', ';' or an unbalanced ')' or ']'. */
32922 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
32924 /* Current level of '< ... >'. */
32925 unsigned level
= 0;
32926 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
32927 unsigned nesting_depth
= 0;
32929 /* Are we ready, yet? If not, issue error message. */
32930 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
32933 /* Skip tokens until the desired token is found. */
32936 /* Peek at the next token. */
32937 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32940 if (!nesting_depth
)
32945 if (cxx_dialect
== cxx98
)
32946 /* C++0x views the `>>' operator as two `>' tokens, but
32949 else if (!nesting_depth
&& level
-- == 0)
32951 /* We've hit a `>>' where the first `>' closes the
32952 template argument list, and the second `>' is
32953 spurious. Just consume the `>>' and stop; we've
32954 already produced at least one error. */
32955 cp_lexer_consume_token (parser
->lexer
);
32958 /* Fall through for C++0x, so we handle the second `>' in
32960 gcc_fallthrough ();
32963 if (!nesting_depth
&& level
-- == 0)
32965 /* We've reached the token we want, consume it and stop. */
32966 cp_lexer_consume_token (parser
->lexer
);
32971 case CPP_OPEN_PAREN
:
32972 case CPP_OPEN_SQUARE
:
32976 case CPP_CLOSE_PAREN
:
32977 case CPP_CLOSE_SQUARE
:
32978 if (nesting_depth
-- == 0)
32983 case CPP_PRAGMA_EOL
:
32984 case CPP_SEMICOLON
:
32985 case CPP_OPEN_BRACE
:
32986 case CPP_CLOSE_BRACE
:
32987 /* The '>' was probably forgotten, don't look further. */
32994 /* Consume this token. */
32995 cp_lexer_consume_token (parser
->lexer
);
32999 /* If the next token is the indicated keyword, consume it. Otherwise,
33000 issue an error message indicating that TOKEN_DESC was expected.
33002 Returns the token consumed, if the token had the appropriate type.
33003 Otherwise, returns NULL. */
33006 cp_parser_require_keyword (cp_parser
* parser
,
33008 required_token token_desc
)
33010 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
33012 if (token
&& token
->keyword
!= keyword
)
33014 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
33022 /* Returns TRUE iff TOKEN is a token that can begin the body of a
33023 function-definition. */
33026 cp_parser_token_starts_function_definition_p (cp_token
* token
)
33028 return (/* An ordinary function-body begins with an `{'. */
33029 token
->type
== CPP_OPEN_BRACE
33030 /* A ctor-initializer begins with a `:'. */
33031 || token
->type
== CPP_COLON
33032 /* A function-try-block begins with `try'. */
33033 || token
->keyword
== RID_TRY
33034 /* A function-transaction-block begins with `__transaction_atomic'
33035 or `__transaction_relaxed'. */
33036 || token
->keyword
== RID_TRANSACTION_ATOMIC
33037 || token
->keyword
== RID_TRANSACTION_RELAXED
33038 /* The named return value extension begins with `return'. */
33039 || token
->keyword
== RID_RETURN
);
33042 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
33046 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
33050 token
= cp_lexer_peek_token (parser
->lexer
);
33051 return (token
->type
== CPP_OPEN_BRACE
33052 || (token
->type
== CPP_COLON
33053 && !parser
->colon_doesnt_start_class_def_p
));
33056 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
33057 C++0x) ending a template-argument. */
33060 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
33064 token
= cp_lexer_peek_token (parser
->lexer
);
33065 return (token
->type
== CPP_COMMA
33066 || token
->type
== CPP_GREATER
33067 || token
->type
== CPP_ELLIPSIS
33068 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
33071 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
33072 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
33075 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
33080 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
33081 if (token
->type
== CPP_LESS
)
33083 /* Check for the sequence `<::' in the original code. It would be lexed as
33084 `[:', where `[' is a digraph, and there is no whitespace before
33086 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
33089 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
33090 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
33096 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
33097 or none_type otherwise. */
33099 static enum tag_types
33100 cp_parser_token_is_class_key (cp_token
* token
)
33102 switch (token
->keyword
)
33107 return record_type
;
33116 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
33117 or none_type otherwise or if the token is null. */
33119 static enum tag_types
33120 cp_parser_token_is_type_parameter_key (cp_token
* token
)
33125 switch (token
->keyword
)
33130 return typename_type
;
33137 /* Diagnose redundant enum-keys. */
33140 cp_parser_maybe_warn_enum_key (cp_parser
*parser
, location_t key_loc
,
33141 tree type
, rid scoped_key
)
33143 if (!warn_redundant_tags
)
33146 tree type_decl
= TYPE_MAIN_DECL (type
);
33147 tree name
= DECL_NAME (type_decl
);
33148 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
33149 push_deferring_access_checks (dk_no_check
);
33150 tree decl
= cp_parser_lookup_name_simple (parser
, name
, input_location
);
33151 pop_deferring_access_checks ();
33153 /* The enum-key is redundant for uses of the TYPE that are not
33154 declarations and for which name lookup returns just the type
33156 if (decl
!= type_decl
)
33159 if (scoped_key
!= RID_CLASS
33160 && scoped_key
!= RID_STRUCT
33161 && current_lang_name
!= lang_name_cplusplus
33162 && current_namespace
== global_namespace
)
33164 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
33165 enum tag in shared C/C++ code in files (such as headers) included
33166 in the main source file. */
33167 const line_map_ordinary
*map
= NULL
;
33168 linemap_resolve_location (line_table
, key_loc
,
33169 LRK_MACRO_DEFINITION_LOCATION
,
33171 if (!MAIN_FILE_P (map
))
33175 gcc_rich_location
richloc (key_loc
);
33176 richloc
.add_fixit_remove (key_loc
);
33177 warning_at (&richloc
, OPT_Wredundant_tags
,
33178 "redundant enum-key %<enum%s%> in reference to %q#T",
33179 (scoped_key
== RID_CLASS
? " class"
33180 : scoped_key
== RID_STRUCT
? " struct" : ""), type
);
33183 /* Describes the set of declarations of a struct, class, or class template
33184 or its specializations. Used for -Wmismatched-tags. */
33186 class class_decl_loc_t
33190 class_decl_loc_t ()
33191 : locvec (), idxdef (), def_class_key ()
33196 /* Constructs an object for a single declaration of a class with
33197 CLASS_KEY at the current location in the current function (or
33198 at another scope). KEY_REDUNDANT is true if the class-key may
33199 be omitted in the current context without an ambiguity with
33200 another symbol with the same name.
33201 DEF_P is true for a class declaration that is a definition.
33202 CURLOC is the associated location. */
33203 class_decl_loc_t (tag_types class_key
, bool key_redundant
, bool def_p
,
33204 location_t curloc
= input_location
)
33205 : locvec (), idxdef (def_p
? 0 : UINT_MAX
), def_class_key (class_key
)
33208 class_key_loc_t
ckl (current_function_decl
, curloc
, class_key
,
33210 locvec
.quick_push (ckl
);
33213 /* Copy, assign, and destroy the object. Necessary because LOCVEC
33214 isn't safely copyable and assignable and doesn't release storage
33216 class_decl_loc_t (const class_decl_loc_t
&rhs
)
33217 : locvec (rhs
.locvec
.copy ()), idxdef (rhs
.idxdef
),
33218 def_class_key (rhs
.def_class_key
)
33221 class_decl_loc_t
& operator= (const class_decl_loc_t
&rhs
)
33226 locvec
= rhs
.locvec
.copy ();
33227 idxdef
= rhs
.idxdef
;
33228 def_class_key
= rhs
.def_class_key
;
33232 ~class_decl_loc_t ()
33237 /* Issues -Wmismatched-tags for a single class. */
33238 void diag_mismatched_tags (tree
);
33240 /* Issues -Wmismatched-tags for all classes. */
33241 static void diag_mismatched_tags ();
33243 /* Adds TYPE_DECL to the collection of class decls and diagnoses
33244 redundant tags (if -Wredundant-tags is enabled). */
33245 static void add (cp_parser
*, location_t
, tag_types
, tree
, bool, bool);
33247 /* Either adds this decl to the collection of class decls
33248 or diagnoses it, whichever is appropriate. */
33249 void add_or_diag_mismatched_tag (tree
, tag_types
, bool, bool);
33253 tree
function (unsigned i
) const
33255 return locvec
[i
].func
;
33258 location_t
location (unsigned i
) const
33260 return locvec
[i
].loc
;
33263 bool key_redundant (unsigned i
) const
33265 return locvec
[i
].key_redundant
;
33268 tag_types
class_key (unsigned i
) const
33270 return locvec
[i
].class_key
;
33273 /* True if a definition for the class has been seen. */
33274 bool def_p () const
33276 return idxdef
< locvec
.length ();
33279 /* The location of a single mention of a class type with the given
33281 struct class_key_loc_t
33283 class_key_loc_t (tree func
, location_t loc
, tag_types key
, bool redundant
)
33284 : func (func
), loc (loc
), class_key (key
), key_redundant (redundant
)
33287 /* The function the type is mentioned in. */
33289 /* The exact location. */
33291 /* The class-key used in the mention of the type. */
33292 tag_types class_key
;
33293 /* True when the class-key could be omitted at this location
33294 without an ambiguity with another symbol of the same name. */
33295 bool key_redundant
;
33297 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
33298 vec
<class_key_loc_t
> locvec
;
33299 /* LOCVEC index of the definition or UINT_MAX if none exists. */
33301 /* The class-key the class was last declared with or none_type when
33302 it has been declared with a mismatched key. */
33303 tag_types def_class_key
;
33305 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
33306 description above. */
33307 typedef hash_map
<tree_decl_hash
, class_decl_loc_t
> class_to_loc_map_t
;
33308 static class_to_loc_map_t class2loc
;
33311 class_decl_loc_t::class_to_loc_map_t
class_decl_loc_t::class2loc
;
33313 /* Issue an error message if the CLASS_KEY does not match the TYPE.
33314 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33315 is set for a declaration of class TYPE and clear for a reference to
33316 it that is not a declaration of it. */
33319 cp_parser_check_class_key (cp_parser
*parser
, location_t key_loc
,
33320 tag_types class_key
, tree type
, bool def_p
,
33323 if (type
== error_mark_node
)
33326 bool seen_as_union
= TREE_CODE (type
) == UNION_TYPE
;
33327 if (seen_as_union
!= (class_key
== union_type
))
33329 if (permerror (input_location
, "%qs tag used in naming %q#T",
33330 class_key
== union_type
? "union"
33331 : class_key
== record_type
? "struct" : "class",
33333 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
33334 "%q#T was previously declared here", type
);
33338 if (!warn_mismatched_tags
&& !warn_redundant_tags
)
33341 /* Only consider the true class-keys below and ignore typename_type,
33342 etc. that are not C++ class-keys. */
33343 if (class_key
!= class_type
33344 && class_key
!= record_type
33345 && class_key
!= union_type
)
33348 class_decl_loc_t::add (parser
, key_loc
, class_key
, type
, def_p
, decl_p
);
33351 /* Returns the template or specialization of one to which the RECORD_TYPE
33352 TYPE corresponds. */
33355 specialization_of (tree type
)
33359 /* Determine the template or its partial specialization to which TYPE
33361 if (tree spec
= most_specialized_partial_spec (type
, tf_none
))
33362 if (spec
!= error_mark_node
)
33363 ret
= TREE_TYPE (TREE_VALUE (spec
));
33366 ret
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type
);
33368 return TYPE_MAIN_DECL (ret
);
33372 /* Adds the class TYPE to the collection of class decls and diagnoses
33373 redundant tags (if -Wredundant-tags is enabled).
33374 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33375 is set for a (likely, based on syntactic context) declaration of class
33376 TYPE and clear for a reference to it that is not a declaration of it. */
33379 class_decl_loc_t::add (cp_parser
*parser
, location_t key_loc
,
33380 tag_types class_key
, tree type
, bool def_p
, bool decl_p
)
33382 tree type_decl
= TYPE_MAIN_DECL (type
);
33383 tree name
= DECL_NAME (type_decl
);
33384 /* Look up the NAME to see if it unambiguously refers to the TYPE
33385 and set KEY_REDUNDANT if so. */
33386 push_deferring_access_checks (dk_no_check
);
33387 tree decl
= cp_parser_lookup_name_simple (parser
, name
, input_location
);
33388 pop_deferring_access_checks ();
33390 /* The class-key is redundant for uses of the CLASS_TYPE that are
33391 neither definitions of it nor declarations, and for which name
33392 lookup returns just the type itself. */
33393 bool key_redundant
= (!def_p
&& !decl_p
33394 && (decl
== type_decl
33395 || TREE_CODE (decl
) == TEMPLATE_DECL
33396 || TYPE_BEING_DEFINED (type
)));
33399 && class_key
!= class_type
33400 && current_lang_name
!= lang_name_cplusplus
33401 && current_namespace
== global_namespace
)
33403 /* Avoid issuing the diagnostic for apparently redundant struct
33404 and union class-keys in shared C/C++ code in files (such as
33405 headers) included in the main source file. */
33406 const line_map_ordinary
*map
= NULL
;
33407 linemap_resolve_location (line_table
, key_loc
,
33408 LRK_MACRO_DEFINITION_LOCATION
,
33410 if (!MAIN_FILE_P (map
))
33411 key_redundant
= false;
33414 /* Set if a declaration of TYPE has previously been seen or if it must
33415 exist in a precompiled header. */
33417 class_decl_loc_t
*rdl
= &class2loc
.get_or_insert (type_decl
, &exist
);
33420 tree type
= TREE_TYPE (type_decl
);
33421 if (def_p
|| !COMPLETE_TYPE_P (type
))
33423 /* TYPE_DECL is the first declaration or definition of the type
33424 (outside precompiled headers -- see below). Just create
33425 a new entry for it and return unless it's a declaration
33426 involving a template that may need to be diagnosed by
33427 -Wredundant-tags. */
33428 *rdl
= class_decl_loc_t (class_key
, false, def_p
);
33429 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
33434 /* TYPE was previously defined in some unknown precompiled hdeader.
33435 Simply add a record of its definition at an unknown location and
33436 proceed below to add a reference to it at the current location.
33437 (Declarations in precompiled headers that are not definitions
33440 = CLASSTYPE_DECLARED_CLASS (type
) ? class_type
: record_type
;
33441 location_t def_loc
= DECL_SOURCE_LOCATION (type_decl
);
33442 *rdl
= class_decl_loc_t (def_key
, false, true, def_loc
);
33447 /* A prior declaration of TYPE_DECL has been seen. */
33451 gcc_rich_location
richloc (key_loc
);
33452 richloc
.add_fixit_remove (key_loc
);
33453 warning_at (&richloc
, OPT_Wredundant_tags
,
33454 "redundant class-key %qs in reference to %q#T",
33455 class_key
== union_type
? "union"
33456 : class_key
== record_type
? "struct" : "class",
33461 /* Do nothing if this is the first declaration of the type. */
33464 if (rdl
->idxdef
!= UINT_MAX
&& rdl
->def_class_key
== class_key
)
33465 /* Do nothing if the class-key in this declaration matches
33469 rdl
->add_or_diag_mismatched_tag (type_decl
, class_key
, key_redundant
,
33473 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
33474 of class decls or diagnoses it, whichever is appropriate. */
33477 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl
,
33478 tag_types class_key
,
33482 /* Reset the CLASS_KEY associated with this type on mismatch.
33483 This is an optimization that lets the diagnostic code skip
33484 over classes that use the same class-key in all declarations. */
33485 if (def_class_key
!= class_key
)
33486 def_class_key
= none_type
;
33488 /* Set IDXDEF to the index of the vector corresponding to
33491 idxdef
= locvec
.length ();
33493 /* Append a record of this declaration to the vector. */
33494 class_key_loc_t
ckl (current_function_decl
, input_location
, class_key
,
33496 locvec
.safe_push (ckl
);
33498 if (idxdef
== UINT_MAX
)
33501 /* As a space optimization diagnose declarations of a class
33502 whose definition has been seen and purge the LOCVEC of
33503 all entries except the definition. */
33504 diag_mismatched_tags (type_decl
);
33507 class_decl_loc_t::class_key_loc_t ent
= locvec
[idxdef
];
33509 locvec
.reserve (2);
33510 locvec
.safe_push (ent
);
33514 /* Pop the entry pushed above for this declaration. */
33518 /* Issues -Wmismatched-tags for a single class. */
33521 class_decl_loc_t::diag_mismatched_tags (tree type_decl
)
33523 if (!warn_mismatched_tags
)
33526 /* Number of uses of the class. */
33527 const unsigned ndecls
= locvec
.length ();
33529 /* The class (or template) declaration guiding the decisions about
33530 the diagnostic. For ordinary classes it's the same as THIS. For
33531 uses of instantiations of templates other than their declarations
33532 it points to the record for the declaration of the corresponding
33533 primary template or partial specialization. */
33534 class_decl_loc_t
*cdlguide
= this;
33536 tree type
= TREE_TYPE (type_decl
);
33537 if (CLASS_TYPE_P (type
) && CLASSTYPE_IMPLICIT_INSTANTIATION (type
))
33539 /* For implicit instantiations of a primary template look up
33540 the primary or partial specialization and use it as
33541 the expected class-key rather than using the class-key of
33542 the first reference to the instantiation. The primary must
33543 be (and inevitably is) at index zero. */
33544 tree spec
= specialization_of (type
);
33545 cdlguide
= class2loc
.get (spec
);
33546 gcc_assert (cdlguide
!= NULL
);
33550 /* Skip declarations that consistently use the same class-key. */
33551 if (def_class_key
!= none_type
)
33555 /* Set if a definition for the class has been seen. */
33556 const bool def_p
= cdlguide
->def_p ();
33558 /* The index of the declaration whose class-key this declaration
33559 is expected to match. It's either the class-key of the class
33560 definition if one exists or the first declaration otherwise. */
33561 const unsigned idxguide
= def_p
? cdlguide
->idxdef
: 0;
33563 /* The class-key the class is expected to be declared with: it's
33564 either the key used in its definition or the first declaration
33565 if no definition has been provided.
33566 For implicit instantiations of a primary template it's
33567 the class-key used to declare the primary with. The primary
33568 must be at index zero. */
33569 const tag_types xpect_key
= cdlguide
->class_key (idxguide
);
33572 /* Advance IDX to the first declaration that either is not
33573 a definition or that doesn't match the first declaration
33574 if no definition is provided. */
33575 while (class_key (idx
) == xpect_key
)
33576 if (++idx
== ndecls
)
33579 /* Save the current function before changing it below. */
33580 tree save_func
= current_function_decl
;
33581 /* Set the function declaration to print in diagnostic context. */
33582 current_function_decl
= function (idx
);
33584 const char *xmatchkstr
= xpect_key
== record_type
? "class" : "struct";
33585 const char *xpectkstr
= xpect_key
== record_type
? "struct" : "class";
33587 location_t loc
= location (idx
);
33588 bool key_redundant_p
= key_redundant (idx
);
33589 auto_diagnostic_group d
;
33590 /* Issue a warning for the first mismatched declaration.
33591 Avoid using "%#qT" since the class-key for the same type will
33592 be the same regardless of which one was used in the declaraion. */
33593 if (warning_at (loc
, OPT_Wmismatched_tags
,
33594 "%qT declared with a mismatched class-key %qs",
33595 type_decl
, xmatchkstr
))
33597 /* Suggest how to avoid the warning for each instance since
33598 the guidance may be different depending on context. */
33601 ? G_("remove the class-key or replace it with %qs")
33602 : G_("replace the class-key with %qs")),
33605 /* Also point to the first declaration or definition that guided
33606 the decision to issue the warning above. */
33607 inform (cdlguide
->location (idxguide
),
33609 ? G_("%qT defined as %qs here")
33610 : G_("%qT first declared as %qs here")),
33611 type_decl
, xpectkstr
);
33614 /* Issue warnings for the remaining inconsistent declarations. */
33615 for (unsigned i
= idx
+ 1; i
!= ndecls
; ++i
)
33617 tag_types clskey
= class_key (i
);
33618 /* Skip over the declarations that match either the definition
33619 if one was provided or the first declaration. */
33620 if (clskey
== xpect_key
)
33623 loc
= location (i
);
33624 key_redundant_p
= key_redundant (i
);
33625 /* Set the function declaration to print in diagnostic context. */
33626 current_function_decl
= function (i
);
33627 if (warning_at (loc
, OPT_Wmismatched_tags
,
33628 "%qT declared with a mismatched class-key %qs",
33629 type_decl
, xmatchkstr
))
33630 /* Suggest how to avoid the warning for each instance since
33631 the guidance may be different depending on context. */
33634 ? G_("remove the class-key or replace it with %qs")
33635 : G_("replace the class-key with %qs")),
33639 /* Restore the current function in case it was replaced above. */
33640 current_function_decl
= save_func
;
33643 /* Issues -Wmismatched-tags for all classes. Called at the end
33644 of processing a translation unit, after declarations of all class
33645 types and their uses have been recorded. */
33648 class_decl_loc_t::diag_mismatched_tags ()
33650 /* CLASS2LOC should be empty if both -Wmismatched-tags and
33651 -Wredundant-tags are disabled. */
33652 gcc_assert (warn_mismatched_tags
33653 || warn_redundant_tags
33654 || class2loc
.is_empty ());
33656 /* Save the current function before changing on return. It should
33657 be null at this point. */
33658 temp_override
<tree
> cleanup (current_function_decl
);
33660 if (warn_mismatched_tags
)
33662 /* Iterate over the collected class/struct/template declarations. */
33663 typedef class_to_loc_map_t::iterator iter_t
;
33664 for (iter_t it
= class2loc
.begin (); it
!= class2loc
.end (); ++it
)
33666 tree type_decl
= (*it
).first
;
33667 class_decl_loc_t
&recloc
= (*it
).second
;
33668 recloc
.diag_mismatched_tags (type_decl
);
33672 class2loc
.empty ();
33675 /* Issue an error message if DECL is redeclared with different
33676 access than its original declaration [class.access.spec/3].
33677 This applies to nested classes, nested class templates and
33678 enumerations [class.mem/1]. */
33681 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
33684 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
33685 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
33688 if ((TREE_PRIVATE (decl
)
33689 != (current_access_specifier
== access_private_node
))
33690 || (TREE_PROTECTED (decl
)
33691 != (current_access_specifier
== access_protected_node
)))
33692 error_at (location
, "%qD redeclared with different access", decl
);
33695 /* Look for the `template' keyword, as a syntactic disambiguator.
33696 Return TRUE iff it is present, in which case it will be
33700 cp_parser_optional_template_keyword (cp_parser
*parser
)
33702 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
33704 /* In C++98 the `template' keyword can only be used within templates;
33705 outside templates the parser can always figure out what is a
33706 template and what is not. In C++11, per the resolution of DR 468,
33707 `template' is allowed in cases where it is not strictly necessary. */
33708 if (!processing_template_decl
33709 && pedantic
&& cxx_dialect
== cxx98
)
33711 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33712 pedwarn (token
->location
, OPT_Wpedantic
,
33713 "in C++98 %<template%> (as a disambiguator) is only "
33714 "allowed within templates");
33715 /* If this part of the token stream is rescanned, the same
33716 error message would be generated. So, we purge the token
33717 from the stream. */
33718 cp_lexer_purge_token (parser
->lexer
);
33723 /* Consume the `template' keyword. */
33724 cp_lexer_consume_token (parser
->lexer
);
33731 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
33732 set PARSER->SCOPE, and perform other related actions. */
33735 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
33737 struct tree_check
*check_value
;
33739 /* Get the stored value. */
33740 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
33741 /* Set the scope from the stored value. */
33742 parser
->scope
= saved_checks_value (check_value
);
33743 parser
->qualifying_scope
= check_value
->qualifying_scope
;
33744 parser
->object_scope
= parser
->context
->object_type
;
33745 parser
->context
->object_type
= NULL_TREE
;
33748 /* Consume tokens up through a non-nested END token. Returns TRUE if we
33749 encounter the end of a block before what we were looking for. */
33752 cp_parser_cache_group (cp_parser
*parser
,
33753 enum cpp_ttype end
,
33758 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33760 /* Abort a parenthesized expression if we encounter a semicolon. */
33761 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
33762 && token
->type
== CPP_SEMICOLON
)
33764 /* If we've reached the end of the file, stop. */
33765 if (token
->type
== CPP_EOF
33766 || (end
!= CPP_PRAGMA_EOL
33767 && token
->type
== CPP_PRAGMA_EOL
))
33769 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
33770 /* We've hit the end of an enclosing block, so there's been some
33771 kind of syntax error. */
33774 /* Consume the token. */
33775 cp_lexer_consume_token (parser
->lexer
);
33776 /* See if it starts a new group. */
33777 if (token
->type
== CPP_OPEN_BRACE
)
33779 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
33780 /* In theory this should probably check end == '}', but
33781 cp_parser_save_member_function_body needs it to exit
33782 after either '}' or ')' when called with ')'. */
33786 else if (token
->type
== CPP_OPEN_PAREN
)
33788 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
33789 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
33792 else if (token
->type
== CPP_PRAGMA
)
33793 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
33794 else if (token
->type
== end
)
33799 /* Like above, for caching a default argument or NSDMI. Both of these are
33800 terminated by a non-nested comma, but it can be unclear whether or not a
33801 comma is nested in a template argument list unless we do more parsing.
33802 In order to handle this ambiguity, when we encounter a ',' after a '<'
33803 we try to parse what follows as a parameter-declaration-list (in the
33804 case of a default argument) or a member-declarator (in the case of an
33805 NSDMI). If that succeeds, then we stop caching. */
33808 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
33810 unsigned depth
= 0;
33811 int maybe_template_id
= 0;
33812 cp_token
*first_token
;
33814 tree default_argument
;
33816 /* Add tokens until we have processed the entire default
33817 argument. We add the range [first_token, token). */
33818 first_token
= cp_lexer_peek_token (parser
->lexer
);
33819 if (first_token
->type
== CPP_OPEN_BRACE
)
33821 /* For list-initialization, this is straightforward. */
33822 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
33823 token
= cp_lexer_peek_token (parser
->lexer
);
33829 /* Peek at the next token. */
33830 token
= cp_lexer_peek_token (parser
->lexer
);
33831 /* What we do depends on what token we have. */
33832 switch (token
->type
)
33834 /* In valid code, a default argument must be
33835 immediately followed by a `,' `)', or `...'. */
33837 if (depth
== 0 && maybe_template_id
)
33839 /* If we've seen a '<', we might be in a
33840 template-argument-list. Until Core issue 325 is
33841 resolved, we don't know how this situation ought
33842 to be handled, so try to DTRT. We check whether
33843 what comes after the comma is a valid parameter
33844 declaration list. If it is, then the comma ends
33845 the default argument; otherwise the default
33846 argument continues. */
33847 bool error
= false;
33850 /* Set ITALP so cp_parser_parameter_declaration_list
33851 doesn't decide to commit to this parse. */
33852 bool saved_italp
= parser
->in_template_argument_list_p
;
33853 parser
->in_template_argument_list_p
= true;
33855 cp_parser_parse_tentatively (parser
);
33859 /* Parse declarators until we reach a non-comma or
33860 somthing that cannot be an initializer.
33861 Just checking whether we're looking at a single
33862 declarator is insufficient. Consider:
33863 int var = tuple<T,U>::x;
33864 The template parameter 'U' looks exactly like a
33868 int ctor_dtor_or_conv_p
;
33869 cp_lexer_consume_token (parser
->lexer
);
33870 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
33871 CP_PARSER_FLAGS_NONE
,
33872 &ctor_dtor_or_conv_p
,
33873 /*parenthesized_p=*/NULL
,
33875 /*friend_p=*/false,
33876 /*static_p=*/false);
33877 peek
= cp_lexer_peek_token (parser
->lexer
);
33878 if (cp_parser_error_occurred (parser
))
33881 while (peek
->type
== CPP_COMMA
);
33882 /* If we met an '=' or ';' then the original comma
33883 was the end of the NSDMI. Otherwise assume
33884 we're still in the NSDMI. */
33885 error
= (peek
->type
!= CPP_EQ
33886 && peek
->type
!= CPP_SEMICOLON
);
33890 cp_lexer_consume_token (parser
->lexer
);
33891 begin_scope (sk_function_parms
, NULL_TREE
);
33892 tree t
= cp_parser_parameter_declaration_list
33893 (parser
, CP_PARSER_FLAGS_NONE
);
33894 if (t
== error_mark_node
)
33896 pop_bindings_and_leave_scope ();
33898 if (!cp_parser_error_occurred (parser
) && !error
)
33900 cp_parser_abort_tentative_parse (parser
);
33902 parser
->in_template_argument_list_p
= saved_italp
;
33906 case CPP_CLOSE_PAREN
:
33908 /* If we run into a non-nested `;', `}', or `]',
33909 then the code is invalid -- but the default
33910 argument is certainly over. */
33911 case CPP_SEMICOLON
:
33912 case CPP_CLOSE_BRACE
:
33913 case CPP_CLOSE_SQUARE
:
33915 /* Handle correctly int n = sizeof ... ( p ); */
33916 && token
->type
!= CPP_ELLIPSIS
)
33918 /* Update DEPTH, if necessary. */
33919 else if (token
->type
== CPP_CLOSE_PAREN
33920 || token
->type
== CPP_CLOSE_BRACE
33921 || token
->type
== CPP_CLOSE_SQUARE
)
33925 case CPP_OPEN_PAREN
:
33926 case CPP_OPEN_SQUARE
:
33927 case CPP_OPEN_BRACE
:
33933 /* This might be the comparison operator, or it might
33934 start a template argument list. */
33935 ++maybe_template_id
;
33939 if (cxx_dialect
== cxx98
)
33941 /* Fall through for C++0x, which treats the `>>'
33942 operator like two `>' tokens in certain
33944 gcc_fallthrough ();
33949 /* This might be an operator, or it might close a
33950 template argument list. But if a previous '<'
33951 started a template argument list, this will have
33952 closed it, so we can't be in one anymore. */
33953 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
33954 if (maybe_template_id
< 0)
33955 maybe_template_id
= 0;
33959 /* If we run out of tokens, issue an error message. */
33961 case CPP_PRAGMA_EOL
:
33962 error_at (token
->location
, "file ends in default argument");
33963 return error_mark_node
;
33967 /* In these cases, we should look for template-ids.
33968 For example, if the default argument is
33969 `X<int, double>()', we need to do name lookup to
33970 figure out whether or not `X' is a template; if
33971 so, the `,' does not end the default argument.
33973 That is not yet done. */
33980 /* If we've reached the end, stop. */
33984 /* Add the token to the token block. */
33985 token
= cp_lexer_consume_token (parser
->lexer
);
33988 /* Create a DEFERRED_PARSE to represent the unparsed default
33990 default_argument
= make_node (DEFERRED_PARSE
);
33991 DEFPARSE_TOKENS (default_argument
)
33992 = cp_token_cache_new (first_token
, token
);
33993 DEFPARSE_INSTANTIATIONS (default_argument
) = NULL
;
33995 return default_argument
;
33998 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
34001 defparse_location (tree default_argument
)
34003 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_argument
);
34004 location_t start
= tokens
->first
->location
;
34005 location_t end
= tokens
->last
->location
;
34006 return make_location (start
, start
, end
);
34009 /* Begin parsing tentatively. We always save tokens while parsing
34010 tentatively so that if the tentative parsing fails we can restore the
34014 cp_parser_parse_tentatively (cp_parser
* parser
)
34016 /* Enter a new parsing context. */
34017 parser
->context
= cp_parser_context_new (parser
->context
);
34018 /* Begin saving tokens. */
34019 cp_lexer_save_tokens (parser
->lexer
);
34020 /* In order to avoid repetitive access control error messages,
34021 access checks are queued up until we are no longer parsing
34023 push_deferring_access_checks (dk_deferred
);
34026 /* Commit to the currently active tentative parse. */
34029 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
34031 cp_parser_context
*context
;
34034 /* Mark all of the levels as committed. */
34035 lexer
= parser
->lexer
;
34036 for (context
= parser
->context
; context
->next
; context
= context
->next
)
34038 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
34040 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
34041 while (!cp_lexer_saving_tokens (lexer
))
34042 lexer
= lexer
->next
;
34043 cp_lexer_commit_tokens (lexer
);
34047 /* Commit to the topmost currently active tentative parse.
34049 Note that this function shouldn't be called when there are
34050 irreversible side-effects while in a tentative state. For
34051 example, we shouldn't create a permanent entry in the symbol
34052 table, or issue an error message that might not apply if the
34053 tentative parse is aborted. */
34056 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
34058 cp_parser_context
*context
= parser
->context
;
34059 cp_lexer
*lexer
= parser
->lexer
;
34063 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
34065 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
34067 while (!cp_lexer_saving_tokens (lexer
))
34068 lexer
= lexer
->next
;
34069 cp_lexer_commit_tokens (lexer
);
34073 /* Abort the currently active tentative parse. All consumed tokens
34074 will be rolled back, and no diagnostics will be issued. */
34077 cp_parser_abort_tentative_parse (cp_parser
* parser
)
34079 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
34080 || errorcount
> 0);
34081 cp_parser_simulate_error (parser
);
34082 /* Now, pretend that we want to see if the construct was
34083 successfully parsed. */
34084 cp_parser_parse_definitely (parser
);
34087 /* Stop parsing tentatively. If a parse error has occurred, restore the
34088 token stream. Otherwise, commit to the tokens we have consumed.
34089 Returns true if no error occurred; false otherwise. */
34092 cp_parser_parse_definitely (cp_parser
* parser
)
34094 bool error_occurred
;
34095 cp_parser_context
*context
;
34097 /* Remember whether or not an error occurred, since we are about to
34098 destroy that information. */
34099 error_occurred
= cp_parser_error_occurred (parser
);
34100 /* Remove the topmost context from the stack. */
34101 context
= parser
->context
;
34102 parser
->context
= context
->next
;
34103 /* If no parse errors occurred, commit to the tentative parse. */
34104 if (!error_occurred
)
34106 /* Commit to the tokens read tentatively, unless that was
34108 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
34109 cp_lexer_commit_tokens (parser
->lexer
);
34111 pop_to_parent_deferring_access_checks ();
34113 /* Otherwise, if errors occurred, roll back our state so that things
34114 are just as they were before we began the tentative parse. */
34117 cp_lexer_rollback_tokens (parser
->lexer
);
34118 pop_deferring_access_checks ();
34120 /* Add the context to the front of the free list. */
34121 context
->next
= cp_parser_context_free_list
;
34122 cp_parser_context_free_list
= context
;
34124 return !error_occurred
;
34127 /* Returns true if we are parsing tentatively and are not committed to
34128 this tentative parse. */
34131 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
34133 return (cp_parser_parsing_tentatively (parser
)
34134 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
34137 /* Returns nonzero iff an error has occurred during the most recent
34138 tentative parse. */
34141 cp_parser_error_occurred (cp_parser
* parser
)
34143 return (cp_parser_parsing_tentatively (parser
)
34144 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
34147 /* Returns nonzero if GNU extensions are allowed. */
34150 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
34152 return parser
->allow_gnu_extensions_p
;
34155 /* Objective-C++ Productions */
34158 /* Parse an Objective-C expression, which feeds into a primary-expression
34162 objc-message-expression
34163 objc-string-literal
34164 objc-encode-expression
34165 objc-protocol-expression
34166 objc-selector-expression
34168 Returns a tree representation of the expression. */
34171 cp_parser_objc_expression (cp_parser
* parser
)
34173 /* Try to figure out what kind of declaration is present. */
34174 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
34178 case CPP_OPEN_SQUARE
:
34179 return cp_parser_objc_message_expression (parser
);
34181 case CPP_OBJC_STRING
:
34182 kwd
= cp_lexer_consume_token (parser
->lexer
);
34183 return objc_build_string_object (kwd
->u
.value
);
34186 switch (kwd
->keyword
)
34188 case RID_AT_ENCODE
:
34189 return cp_parser_objc_encode_expression (parser
);
34191 case RID_AT_PROTOCOL
:
34192 return cp_parser_objc_protocol_expression (parser
);
34194 case RID_AT_SELECTOR
:
34195 return cp_parser_objc_selector_expression (parser
);
34202 error_at (kwd
->location
,
34203 "misplaced %<@%D%> Objective-C++ construct",
34205 cp_parser_skip_to_end_of_block_or_statement (parser
);
34208 return error_mark_node
;
34211 /* Parse an Objective-C message expression.
34213 objc-message-expression:
34214 [ objc-message-receiver objc-message-args ]
34216 Returns a representation of an Objective-C message. */
34219 cp_parser_objc_message_expression (cp_parser
* parser
)
34221 tree receiver
, messageargs
;
34223 parser
->objective_c_message_context_p
= true;
34224 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34225 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
34226 receiver
= cp_parser_objc_message_receiver (parser
);
34227 messageargs
= cp_parser_objc_message_args (parser
);
34228 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34229 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
34231 tree result
= objc_build_message_expr (receiver
, messageargs
);
34233 /* Construct a location e.g.
34236 ranging from the '[' to the ']', with the caret at the start. */
34237 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
34238 protected_set_expr_location (result
, combined_loc
);
34240 parser
->objective_c_message_context_p
= false;
34244 /* Parse an objc-message-receiver.
34246 objc-message-receiver:
34248 simple-type-specifier
34250 Returns a representation of the type or expression. */
34253 cp_parser_objc_message_receiver (cp_parser
* parser
)
34257 /* An Objective-C message receiver may be either (1) a type
34258 or (2) an expression. */
34259 cp_parser_parse_tentatively (parser
);
34260 rcv
= cp_parser_expression (parser
);
34262 /* If that worked out, fine. */
34263 if (cp_parser_parse_definitely (parser
))
34266 cp_parser_parse_tentatively (parser
);
34267 rcv
= cp_parser_simple_type_specifier (parser
,
34268 /*decl_specs=*/NULL
,
34269 CP_PARSER_FLAGS_NONE
);
34271 if (cp_parser_parse_definitely (parser
))
34272 return objc_get_class_reference (rcv
);
34274 cp_parser_error (parser
, "objective-c++ message receiver expected");
34275 return error_mark_node
;
34278 /* Parse the arguments and selectors comprising an Objective-C message.
34283 objc-selector-args , objc-comma-args
34285 objc-selector-args:
34286 objc-selector [opt] : assignment-expression
34287 objc-selector-args objc-selector [opt] : assignment-expression
34290 assignment-expression
34291 objc-comma-args , assignment-expression
34293 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
34294 selector arguments and TREE_VALUE containing a list of comma
34298 cp_parser_objc_message_args (cp_parser
* parser
)
34300 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
34301 bool maybe_unary_selector_p
= true;
34302 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34304 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
34306 tree selector
= NULL_TREE
, arg
;
34308 if (token
->type
!= CPP_COLON
)
34309 selector
= cp_parser_objc_selector (parser
);
34311 /* Detect if we have a unary selector. */
34312 if (maybe_unary_selector_p
34313 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
34314 return build_tree_list (selector
, NULL_TREE
);
34316 maybe_unary_selector_p
= false;
34317 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34318 arg
= cp_parser_assignment_expression (parser
);
34321 = chainon (sel_args
,
34322 build_tree_list (selector
, arg
));
34324 token
= cp_lexer_peek_token (parser
->lexer
);
34327 /* Handle non-selector arguments, if any. */
34328 while (token
->type
== CPP_COMMA
)
34332 cp_lexer_consume_token (parser
->lexer
);
34333 arg
= cp_parser_assignment_expression (parser
);
34336 = chainon (addl_args
,
34337 build_tree_list (NULL_TREE
, arg
));
34339 token
= cp_lexer_peek_token (parser
->lexer
);
34342 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
34344 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
34345 return build_tree_list (error_mark_node
, error_mark_node
);
34348 return build_tree_list (sel_args
, addl_args
);
34351 /* Parse an Objective-C encode expression.
34353 objc-encode-expression:
34354 @encode objc-typename
34356 Returns an encoded representation of the type argument. */
34359 cp_parser_objc_encode_expression (cp_parser
* parser
)
34363 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34365 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
34366 matching_parens parens
;
34367 parens
.require_open (parser
);
34368 token
= cp_lexer_peek_token (parser
->lexer
);
34369 type
= complete_type (cp_parser_type_id (parser
));
34370 parens
.require_close (parser
);
34374 error_at (token
->location
,
34375 "%<@encode%> must specify a type as an argument");
34376 return error_mark_node
;
34379 /* This happens if we find @encode(T) (where T is a template
34380 typename or something dependent on a template typename) when
34381 parsing a template. In that case, we can't compile it
34382 immediately, but we rather create an AT_ENCODE_EXPR which will
34383 need to be instantiated when the template is used.
34385 if (dependent_type_p (type
))
34387 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
34388 TREE_READONLY (value
) = 1;
34393 /* Build a location of the form:
34396 with caret==start at the @ token, finishing at the close paren. */
34397 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
34399 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
34402 /* Parse an Objective-C @defs expression. */
34405 cp_parser_objc_defs_expression (cp_parser
*parser
)
34409 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
34410 matching_parens parens
;
34411 parens
.require_open (parser
);
34412 name
= cp_parser_identifier (parser
);
34413 parens
.require_close (parser
);
34415 return objc_get_class_ivars (name
);
34418 /* Parse an Objective-C protocol expression.
34420 objc-protocol-expression:
34421 @protocol ( identifier )
34423 Returns a representation of the protocol expression. */
34426 cp_parser_objc_protocol_expression (cp_parser
* parser
)
34429 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34431 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
34432 matching_parens parens
;
34433 parens
.require_open (parser
);
34434 proto
= cp_parser_identifier (parser
);
34435 parens
.require_close (parser
);
34437 /* Build a location of the form:
34440 with caret==start at the @ token, finishing at the close paren. */
34441 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
34442 tree result
= objc_build_protocol_expr (proto
);
34443 protected_set_expr_location (result
, combined_loc
);
34447 /* Parse an Objective-C selector expression.
34449 objc-selector-expression:
34450 @selector ( objc-method-signature )
34452 objc-method-signature:
34458 objc-selector-seq objc-selector :
34460 Returns a representation of the method selector. */
34463 cp_parser_objc_selector_expression (cp_parser
* parser
)
34465 tree sel_seq
= NULL_TREE
;
34466 bool maybe_unary_selector_p
= true;
34468 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34470 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
34471 matching_parens parens
;
34472 parens
.require_open (parser
);
34473 token
= cp_lexer_peek_token (parser
->lexer
);
34475 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
34476 || token
->type
== CPP_SCOPE
)
34478 tree selector
= NULL_TREE
;
34480 if (token
->type
!= CPP_COLON
34481 || token
->type
== CPP_SCOPE
)
34482 selector
= cp_parser_objc_selector (parser
);
34484 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
34485 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
34487 /* Detect if we have a unary selector. */
34488 if (maybe_unary_selector_p
)
34490 sel_seq
= selector
;
34491 goto finish_selector
;
34495 cp_parser_error (parser
, "expected %<:%>");
34498 maybe_unary_selector_p
= false;
34499 token
= cp_lexer_consume_token (parser
->lexer
);
34501 if (token
->type
== CPP_SCOPE
)
34504 = chainon (sel_seq
,
34505 build_tree_list (selector
, NULL_TREE
));
34507 = chainon (sel_seq
,
34508 build_tree_list (NULL_TREE
, NULL_TREE
));
34512 = chainon (sel_seq
,
34513 build_tree_list (selector
, NULL_TREE
));
34515 token
= cp_lexer_peek_token (parser
->lexer
);
34519 parens
.require_close (parser
);
34522 /* Build a location of the form:
34525 with caret==start at the @ token, finishing at the close paren. */
34526 location_t combined_loc
= make_location (loc
, loc
, parser
->lexer
);
34527 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
34528 /* TODO: objc_build_selector_expr doesn't always honor the location. */
34529 protected_set_expr_location (result
, combined_loc
);
34533 /* Parse a list of identifiers.
34535 objc-identifier-list:
34537 objc-identifier-list , identifier
34539 Returns a TREE_LIST of identifier nodes. */
34542 cp_parser_objc_identifier_list (cp_parser
* parser
)
34548 identifier
= cp_parser_identifier (parser
);
34549 if (identifier
== error_mark_node
)
34550 return error_mark_node
;
34552 list
= build_tree_list (NULL_TREE
, identifier
);
34553 sep
= cp_lexer_peek_token (parser
->lexer
);
34555 while (sep
->type
== CPP_COMMA
)
34557 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
34558 identifier
= cp_parser_identifier (parser
);
34559 if (identifier
== error_mark_node
)
34562 list
= chainon (list
, build_tree_list (NULL_TREE
,
34564 sep
= cp_lexer_peek_token (parser
->lexer
);
34570 /* Parse an Objective-C alias declaration.
34572 objc-alias-declaration:
34573 @compatibility_alias identifier identifier ;
34575 This function registers the alias mapping with the Objective-C front end.
34576 It returns nothing. */
34579 cp_parser_objc_alias_declaration (cp_parser
* parser
)
34583 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
34584 alias
= cp_parser_identifier (parser
);
34585 orig
= cp_parser_identifier (parser
);
34586 objc_declare_alias (alias
, orig
);
34587 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34590 /* Parse an Objective-C class forward-declaration.
34592 objc-class-declaration:
34593 @class objc-identifier-list ;
34595 The function registers the forward declarations with the Objective-C
34596 front end. It returns nothing. */
34599 cp_parser_objc_class_declaration (cp_parser
* parser
)
34601 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
34606 id
= cp_parser_identifier (parser
);
34607 if (id
== error_mark_node
)
34610 objc_declare_class (id
);
34612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34613 cp_lexer_consume_token (parser
->lexer
);
34617 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34620 /* Parse a list of Objective-C protocol references.
34622 objc-protocol-refs-opt:
34623 objc-protocol-refs [opt]
34625 objc-protocol-refs:
34626 < objc-identifier-list >
34628 Returns a TREE_LIST of identifiers, if any. */
34631 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
34633 tree protorefs
= NULL_TREE
;
34635 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
34637 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
34638 protorefs
= cp_parser_objc_identifier_list (parser
);
34639 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
34645 /* Parse a Objective-C visibility specification. */
34648 cp_parser_objc_visibility_spec (cp_parser
* parser
)
34650 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
34652 switch (vis
->keyword
)
34654 case RID_AT_PRIVATE
:
34655 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
34657 case RID_AT_PROTECTED
:
34658 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
34660 case RID_AT_PUBLIC
:
34661 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
34663 case RID_AT_PACKAGE
:
34664 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
34670 /* Eat '@private'/'@protected'/'@public'. */
34671 cp_lexer_consume_token (parser
->lexer
);
34674 /* Parse an Objective-C method type. Return 'true' if it is a class
34675 (+) method, and 'false' if it is an instance (-) method. */
34678 cp_parser_objc_method_type (cp_parser
* parser
)
34680 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
34686 /* Parse an Objective-C protocol qualifier. */
34689 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
34691 tree quals
= NULL_TREE
, node
;
34692 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34694 node
= token
->u
.value
;
34696 while (node
&& identifier_p (node
)
34697 && (node
== ridpointers
[(int) RID_IN
]
34698 || node
== ridpointers
[(int) RID_OUT
]
34699 || node
== ridpointers
[(int) RID_INOUT
]
34700 || node
== ridpointers
[(int) RID_BYCOPY
]
34701 || node
== ridpointers
[(int) RID_BYREF
]
34702 || node
== ridpointers
[(int) RID_ONEWAY
]))
34704 quals
= tree_cons (NULL_TREE
, node
, quals
);
34705 cp_lexer_consume_token (parser
->lexer
);
34706 token
= cp_lexer_peek_token (parser
->lexer
);
34707 node
= token
->u
.value
;
34713 /* Parse an Objective-C typename. */
34716 cp_parser_objc_typename (cp_parser
* parser
)
34718 tree type_name
= NULL_TREE
;
34720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34722 tree proto_quals
, cp_type
= NULL_TREE
;
34724 matching_parens parens
;
34725 parens
.consume_open (parser
); /* Eat '('. */
34726 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
34728 /* An ObjC type name may consist of just protocol qualifiers, in which
34729 case the type shall default to 'id'. */
34730 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
34732 cp_type
= cp_parser_type_id (parser
);
34734 /* If the type could not be parsed, an error has already
34735 been produced. For error recovery, behave as if it had
34736 not been specified, which will use the default type
34738 if (cp_type
== error_mark_node
)
34740 cp_type
= NULL_TREE
;
34741 /* We need to skip to the closing parenthesis as
34742 cp_parser_type_id() does not seem to do it for
34744 cp_parser_skip_to_closing_parenthesis (parser
,
34745 /*recovering=*/true,
34746 /*or_comma=*/false,
34747 /*consume_paren=*/false);
34751 parens
.require_close (parser
);
34752 type_name
= build_tree_list (proto_quals
, cp_type
);
34758 /* Check to see if TYPE refers to an Objective-C selector name. */
34761 cp_parser_objc_selector_p (enum cpp_ttype type
)
34763 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
34764 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
34765 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
34766 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
34767 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
34770 /* Parse an Objective-C selector. */
34773 cp_parser_objc_selector (cp_parser
* parser
)
34775 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
34777 if (!cp_parser_objc_selector_p (token
->type
))
34779 error_at (token
->location
, "invalid Objective-C++ selector name");
34780 return error_mark_node
;
34783 /* C++ operator names are allowed to appear in ObjC selectors. */
34784 switch (token
->type
)
34786 case CPP_AND_AND
: return get_identifier ("and");
34787 case CPP_AND_EQ
: return get_identifier ("and_eq");
34788 case CPP_AND
: return get_identifier ("bitand");
34789 case CPP_OR
: return get_identifier ("bitor");
34790 case CPP_COMPL
: return get_identifier ("compl");
34791 case CPP_NOT
: return get_identifier ("not");
34792 case CPP_NOT_EQ
: return get_identifier ("not_eq");
34793 case CPP_OR_OR
: return get_identifier ("or");
34794 case CPP_OR_EQ
: return get_identifier ("or_eq");
34795 case CPP_XOR
: return get_identifier ("xor");
34796 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
34797 default: return token
->u
.value
;
34801 /* Parse an Objective-C params list. */
34804 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
34806 tree params
= NULL_TREE
;
34807 bool maybe_unary_selector_p
= true;
34808 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34810 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
34812 tree selector
= NULL_TREE
, type_name
, identifier
;
34813 tree parm_attr
= NULL_TREE
;
34815 if (token
->keyword
== RID_ATTRIBUTE
)
34818 if (token
->type
!= CPP_COLON
)
34819 selector
= cp_parser_objc_selector (parser
);
34821 /* Detect if we have a unary selector. */
34822 if (maybe_unary_selector_p
34823 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
34825 params
= selector
; /* Might be followed by attributes. */
34829 maybe_unary_selector_p
= false;
34830 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34832 /* Something went quite wrong. There should be a colon
34833 here, but there is not. Stop parsing parameters. */
34836 type_name
= cp_parser_objc_typename (parser
);
34837 /* New ObjC allows attributes on parameters too. */
34838 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
34839 parm_attr
= cp_parser_attributes_opt (parser
);
34840 identifier
= cp_parser_identifier (parser
);
34844 objc_build_keyword_decl (selector
,
34849 token
= cp_lexer_peek_token (parser
->lexer
);
34852 if (params
== NULL_TREE
)
34854 cp_parser_error (parser
, "objective-c++ method declaration is expected");
34855 return error_mark_node
;
34858 /* We allow tail attributes for the method. */
34859 if (token
->keyword
== RID_ATTRIBUTE
)
34861 *attributes
= cp_parser_attributes_opt (parser
);
34862 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
34863 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34865 cp_parser_error (parser
,
34866 "method attributes must be specified at the end");
34867 return error_mark_node
;
34870 if (params
== NULL_TREE
)
34872 cp_parser_error (parser
, "objective-c++ method declaration is expected");
34873 return error_mark_node
;
34878 /* Parse the non-keyword Objective-C params. */
34881 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
34884 tree params
= make_node (TREE_LIST
);
34885 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34886 *ellipsisp
= false; /* Initially, assume no ellipsis. */
34888 while (token
->type
== CPP_COMMA
)
34890 cp_parameter_declarator
*parmdecl
;
34893 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
34894 token
= cp_lexer_peek_token (parser
->lexer
);
34896 if (token
->type
== CPP_ELLIPSIS
)
34898 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
34900 token
= cp_lexer_peek_token (parser
->lexer
);
34904 /* TODO: parse attributes for tail parameters. */
34905 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
34907 parm
= grokdeclarator (parmdecl
->declarator
,
34908 &parmdecl
->decl_specifiers
,
34909 PARM
, /*initialized=*/0,
34910 /*attrlist=*/NULL
);
34912 chainon (params
, build_tree_list (NULL_TREE
, parm
));
34913 token
= cp_lexer_peek_token (parser
->lexer
);
34916 /* We allow tail attributes for the method. */
34917 if (token
->keyword
== RID_ATTRIBUTE
)
34919 if (*attributes
== NULL_TREE
)
34921 *attributes
= cp_parser_attributes_opt (parser
);
34922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
34923 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34927 /* We have an error, but parse the attributes, so that we can
34929 *attributes
= cp_parser_attributes_opt (parser
);
34931 cp_parser_error (parser
,
34932 "method attributes must be specified at the end");
34933 return error_mark_node
;
34939 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
34942 cp_parser_objc_interstitial_code (cp_parser
* parser
)
34944 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34946 /* If the next token is `extern' and the following token is a string
34947 literal, then we have a linkage specification. */
34948 if (token
->keyword
== RID_EXTERN
34949 && cp_parser_is_pure_string_literal
34950 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
34951 cp_parser_linkage_specification (parser
, NULL_TREE
);
34952 /* Handle #pragma, if any. */
34953 else if (token
->type
== CPP_PRAGMA
)
34954 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
34955 /* Allow stray semicolons. */
34956 else if (token
->type
== CPP_SEMICOLON
)
34957 cp_lexer_consume_token (parser
->lexer
);
34958 /* Mark methods as optional or required, when building protocols. */
34959 else if (token
->keyword
== RID_AT_OPTIONAL
)
34961 cp_lexer_consume_token (parser
->lexer
);
34962 objc_set_method_opt (true);
34964 else if (token
->keyword
== RID_AT_REQUIRED
)
34966 cp_lexer_consume_token (parser
->lexer
);
34967 objc_set_method_opt (false);
34969 else if (token
->keyword
== RID_NAMESPACE
)
34970 cp_parser_namespace_definition (parser
);
34971 /* Other stray characters must generate errors. */
34972 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
34974 cp_lexer_consume_token (parser
->lexer
);
34975 error ("stray %qs between Objective-C++ methods",
34976 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
34978 /* Finally, try to parse a block-declaration, or a function-definition. */
34980 cp_parser_block_declaration (parser
, /*statement_p=*/false);
34983 /* Parse a method signature. */
34986 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
34988 tree rettype
, kwdparms
, optparms
;
34989 bool ellipsis
= false;
34990 bool is_class_method
;
34992 is_class_method
= cp_parser_objc_method_type (parser
);
34993 rettype
= cp_parser_objc_typename (parser
);
34994 *attributes
= NULL_TREE
;
34995 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
34996 if (kwdparms
== error_mark_node
)
34997 return error_mark_node
;
34998 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
34999 if (optparms
== error_mark_node
)
35000 return error_mark_node
;
35002 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
35006 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
35009 cp_lexer_save_tokens (parser
->lexer
);
35010 tattr
= cp_parser_attributes_opt (parser
);
35011 gcc_assert (tattr
) ;
35013 /* If the attributes are followed by a method introducer, this is not allowed.
35014 Dump the attributes and flag the situation. */
35015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
35016 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
35019 /* Otherwise, the attributes introduce some interstitial code, possibly so
35020 rewind to allow that check. */
35021 cp_lexer_rollback_tokens (parser
->lexer
);
35025 /* Parse an Objective-C method prototype list. */
35028 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
35030 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35032 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
35034 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
35036 tree attributes
, sig
;
35037 bool is_class_method
;
35038 if (token
->type
== CPP_PLUS
)
35039 is_class_method
= true;
35041 is_class_method
= false;
35042 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
35043 if (sig
== error_mark_node
)
35045 cp_parser_skip_to_end_of_block_or_statement (parser
);
35046 token
= cp_lexer_peek_token (parser
->lexer
);
35049 objc_add_method_declaration (is_class_method
, sig
, attributes
);
35050 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35052 else if (token
->keyword
== RID_AT_PROPERTY
)
35053 cp_parser_objc_at_property_declaration (parser
);
35054 else if (token
->keyword
== RID_ATTRIBUTE
35055 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
35056 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35058 "prefix attributes are ignored for methods");
35060 /* Allow for interspersed non-ObjC++ code. */
35061 cp_parser_objc_interstitial_code (parser
);
35063 token
= cp_lexer_peek_token (parser
->lexer
);
35066 if (token
->type
!= CPP_EOF
)
35067 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
35069 cp_parser_error (parser
, "expected %<@end%>");
35071 objc_finish_interface ();
35074 /* Parse an Objective-C method definition list. */
35077 cp_parser_objc_method_definition_list (cp_parser
* parser
)
35081 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35083 if (token
->keyword
== RID_AT_END
)
35085 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
35088 else if (token
->type
== CPP_EOF
)
35090 cp_parser_error (parser
, "expected %<@end%>");
35093 else if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
35095 bool is_class_method
= token
->type
== CPP_PLUS
;
35097 push_deferring_access_checks (dk_deferred
);
35099 tree sig
= cp_parser_objc_method_signature (parser
, &attribute
);
35100 if (sig
== error_mark_node
)
35101 cp_parser_skip_to_end_of_block_or_statement (parser
);
35104 objc_start_method_definition (is_class_method
, sig
,
35105 attribute
, NULL_TREE
);
35107 /* For historical reasons, we accept an optional semicolon. */
35108 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35109 cp_lexer_consume_token (parser
->lexer
);
35111 perform_deferred_access_checks (tf_warning_or_error
);
35112 stop_deferring_access_checks ();
35114 = cp_parser_function_definition_after_declarator (parser
, false);
35115 pop_deferring_access_checks ();
35116 objc_finish_method_definition (meth
);
35119 /* The following case will be removed once @synthesize is
35120 completely implemented. */
35121 else if (token
->keyword
== RID_AT_PROPERTY
)
35122 cp_parser_objc_at_property_declaration (parser
);
35123 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
35124 cp_parser_objc_at_synthesize_declaration (parser
);
35125 else if (token
->keyword
== RID_AT_DYNAMIC
)
35126 cp_parser_objc_at_dynamic_declaration (parser
);
35127 else if (token
->keyword
== RID_ATTRIBUTE
35128 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
35129 warning_at (token
->location
, OPT_Wattributes
,
35130 "prefix attributes are ignored for methods");
35132 /* Allow for interspersed non-ObjC++ code. */
35133 cp_parser_objc_interstitial_code (parser
);
35136 objc_finish_implementation ();
35139 /* Parse Objective-C ivars. */
35142 cp_parser_objc_class_ivars (cp_parser
* parser
)
35144 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35146 if (token
->type
!= CPP_OPEN_BRACE
)
35147 return; /* No ivars specified. */
35149 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
35150 token
= cp_lexer_peek_token (parser
->lexer
);
35152 while (token
->type
!= CPP_CLOSE_BRACE
35153 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
35155 cp_decl_specifier_seq declspecs
;
35156 int decl_class_or_enum_p
;
35157 tree prefix_attributes
;
35159 cp_parser_objc_visibility_spec (parser
);
35161 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35164 cp_parser_decl_specifier_seq (parser
,
35165 CP_PARSER_FLAGS_OPTIONAL
,
35167 &decl_class_or_enum_p
);
35169 /* auto, register, static, extern, mutable. */
35170 if (declspecs
.storage_class
!= sc_none
)
35172 cp_parser_error (parser
, "invalid type for instance variable");
35173 declspecs
.storage_class
= sc_none
;
35176 /* thread_local. */
35177 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
35179 cp_parser_error (parser
, "invalid type for instance variable");
35180 declspecs
.locations
[ds_thread
] = 0;
35184 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
35186 cp_parser_error (parser
, "invalid type for instance variable");
35187 declspecs
.locations
[ds_typedef
] = 0;
35190 prefix_attributes
= declspecs
.attributes
;
35191 declspecs
.attributes
= NULL_TREE
;
35193 /* Keep going until we hit the `;' at the end of the
35195 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35197 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
35198 cp_declarator
*declarator
= NULL
;
35199 int ctor_dtor_or_conv_p
;
35201 /* Check for a (possibly unnamed) bitfield declaration. */
35202 token
= cp_lexer_peek_token (parser
->lexer
);
35203 if (token
->type
== CPP_COLON
)
35206 if (token
->type
== CPP_NAME
35207 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
35210 /* Get the name of the bitfield. */
35211 declarator
= make_id_declarator (NULL_TREE
,
35212 cp_parser_identifier (parser
),
35213 sfk_none
, token
->location
);
35216 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
35217 /* Get the width of the bitfield. */
35219 = cp_parser_constant_expression (parser
);
35223 /* Parse the declarator. */
35225 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
35226 CP_PARSER_FLAGS_NONE
,
35227 &ctor_dtor_or_conv_p
,
35228 /*parenthesized_p=*/NULL
,
35229 /*member_p=*/false,
35230 /*friend_p=*/false,
35231 /*static_p=*/false);
35234 /* Look for attributes that apply to the ivar. */
35235 attributes
= cp_parser_attributes_opt (parser
);
35236 /* Remember which attributes are prefix attributes and
35238 first_attribute
= attributes
;
35239 /* Combine the attributes. */
35240 attributes
= attr_chainon (prefix_attributes
, attributes
);
35243 /* Create the bitfield declaration. */
35244 decl
= grokbitfield (declarator
, &declspecs
,
35245 width
, NULL_TREE
, attributes
);
35247 decl
= grokfield (declarator
, &declspecs
,
35248 NULL_TREE
, /*init_const_expr_p=*/false,
35249 NULL_TREE
, attributes
);
35251 /* Add the instance variable. */
35252 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
35253 objc_add_instance_variable (decl
);
35255 /* Reset PREFIX_ATTRIBUTES. */
35256 if (attributes
!= error_mark_node
)
35258 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
35259 attributes
= TREE_CHAIN (attributes
);
35261 TREE_CHAIN (attributes
) = NULL_TREE
;
35264 token
= cp_lexer_peek_token (parser
->lexer
);
35266 if (token
->type
== CPP_COMMA
)
35268 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
35274 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35275 token
= cp_lexer_peek_token (parser
->lexer
);
35278 if (token
->keyword
== RID_AT_END
)
35279 cp_parser_error (parser
, "expected %<}%>");
35281 /* Do not consume the RID_AT_END, so it will be read again as terminating
35282 the @interface of @implementation. */
35283 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
35284 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
35286 /* For historical reasons, we accept an optional semicolon. */
35287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35288 cp_lexer_consume_token (parser
->lexer
);
35291 /* Parse an Objective-C protocol declaration. */
35294 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
35296 tree proto
, protorefs
;
35299 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
35300 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
35302 tok
= cp_lexer_peek_token (parser
->lexer
);
35303 error_at (tok
->location
, "identifier expected after %<@protocol%>");
35304 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35308 /* See if we have a forward declaration or a definition. */
35309 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
35311 /* Try a forward declaration first. */
35312 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
35318 id
= cp_parser_identifier (parser
);
35319 if (id
== error_mark_node
)
35322 objc_declare_protocol (id
, attributes
);
35324 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35325 cp_lexer_consume_token (parser
->lexer
);
35329 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35332 /* Ok, we got a full-fledged definition (or at least should). */
35335 proto
= cp_parser_identifier (parser
);
35336 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
35337 objc_start_protocol (proto
, protorefs
, attributes
);
35338 cp_parser_objc_method_prototype_list (parser
);
35342 /* Parse an Objective-C superclass or category. */
35345 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
35348 tree
*categ
, bool *is_class_extension
)
35350 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
35352 *super
= *categ
= NULL_TREE
;
35353 *is_class_extension
= false;
35354 if (next
->type
== CPP_COLON
)
35356 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
35357 *super
= cp_parser_identifier (parser
);
35359 else if (next
->type
== CPP_OPEN_PAREN
)
35361 matching_parens parens
;
35362 parens
.consume_open (parser
); /* Eat '('. */
35364 /* If there is no category name, and this is an @interface, we
35365 have a class extension. */
35366 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
35368 *categ
= NULL_TREE
;
35369 *is_class_extension
= true;
35372 *categ
= cp_parser_identifier (parser
);
35374 parens
.require_close (parser
);
35378 /* Parse an Objective-C class interface. */
35381 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
35383 tree name
, super
, categ
, protos
;
35384 bool is_class_extension
;
35386 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
35387 location_t nam_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35388 name
= cp_parser_identifier (parser
);
35389 if (name
== error_mark_node
)
35391 /* It's hard to recover because even if valid @interface stuff
35392 is to follow, we can't compile it (or validate it) if we
35393 don't even know which class it refers to. Let's assume this
35394 was a stray '@interface' token in the stream and skip it.
35398 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
35399 &is_class_extension
);
35400 protos
= cp_parser_objc_protocol_refs_opt (parser
);
35402 /* We have either a class or a category on our hands. */
35403 if (categ
|| is_class_extension
)
35404 objc_start_category_interface (name
, categ
, protos
, attributes
);
35407 objc_start_class_interface (name
, nam_loc
, super
, protos
, attributes
);
35408 /* Handle instance variable declarations, if any. */
35409 cp_parser_objc_class_ivars (parser
);
35410 objc_continue_interface ();
35413 cp_parser_objc_method_prototype_list (parser
);
35416 /* Parse an Objective-C class implementation. */
35419 cp_parser_objc_class_implementation (cp_parser
* parser
)
35421 tree name
, super
, categ
;
35422 bool is_class_extension
;
35424 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
35425 name
= cp_parser_identifier (parser
);
35426 if (name
== error_mark_node
)
35428 /* It's hard to recover because even if valid @implementation
35429 stuff is to follow, we can't compile it (or validate it) if
35430 we don't even know which class it refers to. Let's assume
35431 this was a stray '@implementation' token in the stream and
35436 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
35437 &is_class_extension
);
35439 /* We have either a class or a category on our hands. */
35441 objc_start_category_implementation (name
, categ
);
35444 objc_start_class_implementation (name
, super
);
35445 /* Handle instance variable declarations, if any. */
35446 cp_parser_objc_class_ivars (parser
);
35447 objc_continue_implementation ();
35450 cp_parser_objc_method_definition_list (parser
);
35453 /* Consume the @end token and finish off the implementation. */
35456 cp_parser_objc_end_implementation (cp_parser
* parser
)
35458 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
35459 objc_finish_implementation ();
35462 /* Parse an Objective-C declaration. */
35465 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
35467 /* Try to figure out what kind of declaration is present. */
35468 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
35471 switch (kwd
->keyword
)
35476 error_at (kwd
->location
, "attributes may not be specified before"
35477 " the %<@%D%> Objective-C++ keyword",
35481 case RID_AT_IMPLEMENTATION
:
35482 warning_at (kwd
->location
, OPT_Wattributes
,
35483 "prefix attributes are ignored before %<@%D%>",
35490 switch (kwd
->keyword
)
35493 cp_parser_objc_alias_declaration (parser
);
35496 cp_parser_objc_class_declaration (parser
);
35498 case RID_AT_PROTOCOL
:
35499 cp_parser_objc_protocol_declaration (parser
, attributes
);
35501 case RID_AT_INTERFACE
:
35502 cp_parser_objc_class_interface (parser
, attributes
);
35504 case RID_AT_IMPLEMENTATION
:
35505 cp_parser_objc_class_implementation (parser
);
35508 cp_parser_objc_end_implementation (parser
);
35511 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
35513 cp_parser_skip_to_end_of_block_or_statement (parser
);
35517 /* Parse an Objective-C try-catch-finally statement.
35519 objc-try-catch-finally-stmt:
35520 @try compound-statement objc-catch-clause-seq [opt]
35521 objc-finally-clause [opt]
35523 objc-catch-clause-seq:
35524 objc-catch-clause objc-catch-clause-seq [opt]
35527 @catch ( objc-exception-declaration ) compound-statement
35529 objc-finally-clause:
35530 @finally compound-statement
35532 objc-exception-declaration:
35533 parameter-declaration
35536 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
35540 PS: This function is identical to c_parser_objc_try_catch_finally_statement
35541 for C. Keep them in sync. */
35544 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
35546 location_t location
;
35549 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
35550 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
35551 objc_maybe_warn_exceptions (location
);
35552 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
35553 node, lest it get absorbed into the surrounding block. */
35554 stmt
= push_stmt_list ();
35555 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
35556 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
35558 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
35560 cp_parameter_declarator
*parm
;
35561 tree parameter_declaration
= error_mark_node
;
35562 bool seen_open_paren
= false;
35563 matching_parens parens
;
35565 cp_lexer_consume_token (parser
->lexer
);
35566 if (parens
.require_open (parser
))
35567 seen_open_paren
= true;
35568 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
35570 /* We have "@catch (...)" (where the '...' are literally
35571 what is in the code). Skip the '...'.
35572 parameter_declaration is set to NULL_TREE, and
35573 objc_being_catch_clauses() knows that that means
35575 cp_lexer_consume_token (parser
->lexer
);
35576 parameter_declaration
= NULL_TREE
;
35580 /* We have "@catch (NSException *exception)" or something
35581 like that. Parse the parameter declaration. */
35582 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
35585 parameter_declaration
= error_mark_node
;
35587 parameter_declaration
= grokdeclarator (parm
->declarator
,
35588 &parm
->decl_specifiers
,
35589 PARM
, /*initialized=*/0,
35590 /*attrlist=*/NULL
);
35592 if (seen_open_paren
)
35593 parens
.require_close (parser
);
35596 /* If there was no open parenthesis, we are recovering from
35597 an error, and we are trying to figure out what mistake
35598 the user has made. */
35600 /* If there is an immediate closing parenthesis, the user
35601 probably forgot the opening one (ie, they typed "@catch
35602 NSException *e)". Parse the closing parenthesis and keep
35604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
35605 cp_lexer_consume_token (parser
->lexer
);
35607 /* If these is no immediate closing parenthesis, the user
35608 probably doesn't know that parenthesis are required at
35609 all (ie, they typed "@catch NSException *e"). So, just
35610 forget about the closing parenthesis and keep going. */
35612 objc_begin_catch_clause (parameter_declaration
);
35613 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
35614 objc_finish_catch_clause ();
35616 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
35618 cp_lexer_consume_token (parser
->lexer
);
35619 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
35620 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
35621 node, lest it get absorbed into the surrounding block. */
35622 stmt
= push_stmt_list ();
35623 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
35624 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
35627 return objc_finish_try_stmt ();
35630 /* Parse an Objective-C synchronized statement.
35632 objc-synchronized-stmt:
35633 @synchronized ( expression ) compound-statement
35635 Returns NULL_TREE. */
35638 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
35640 location_t location
;
35643 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
35645 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
35646 objc_maybe_warn_exceptions (location
);
35647 matching_parens parens
;
35648 parens
.require_open (parser
);
35649 lock
= cp_parser_expression (parser
);
35650 parens
.require_close (parser
);
35652 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
35653 node, lest it get absorbed into the surrounding block. */
35654 stmt
= push_stmt_list ();
35655 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
35657 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
35660 /* Parse an Objective-C throw statement.
35663 @throw assignment-expression [opt] ;
35665 Returns a constructed '@throw' statement. */
35668 cp_parser_objc_throw_statement (cp_parser
*parser
)
35670 tree expr
= NULL_TREE
;
35671 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35673 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
35675 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35676 expr
= cp_parser_expression (parser
);
35678 cp_parser_consume_semicolon_at_end_of_statement (parser
);
35680 return objc_build_throw_stmt (loc
, expr
);
35683 /* Parse an Objective-C statement. */
35686 cp_parser_objc_statement (cp_parser
* parser
)
35688 /* Try to figure out what kind of declaration is present. */
35689 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
35691 switch (kwd
->keyword
)
35694 return cp_parser_objc_try_catch_finally_statement (parser
);
35695 case RID_AT_SYNCHRONIZED
:
35696 return cp_parser_objc_synchronized_statement (parser
);
35698 return cp_parser_objc_throw_statement (parser
);
35700 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
35702 cp_parser_skip_to_end_of_block_or_statement (parser
);
35705 return error_mark_node
;
35708 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
35709 look ahead to see if an objc keyword follows the attributes. This
35710 is to detect the use of prefix attributes on ObjC @interface and
35714 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
35716 cp_lexer_save_tokens (parser
->lexer
);
35717 tree addon
= cp_parser_attributes_opt (parser
);
35719 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
35721 cp_lexer_commit_tokens (parser
->lexer
);
35723 TREE_CHAIN (*attrib
) = addon
;
35728 cp_lexer_rollback_tokens (parser
->lexer
);
35732 /* This routine is a minimal replacement for
35733 c_parser_struct_declaration () used when parsing the list of
35734 types/names or ObjC++ properties. For example, when parsing the
35737 @property (readonly) int a, b, c;
35739 this function is responsible for parsing "int a, int b, int c" and
35740 returning the declarations as CHAIN of DECLs.
35742 TODO: Share this code with cp_parser_objc_class_ivars. It's very
35743 similar parsing. */
35745 cp_parser_objc_struct_declaration (cp_parser
*parser
)
35747 tree decls
= NULL_TREE
;
35748 cp_decl_specifier_seq declspecs
;
35749 int decl_class_or_enum_p
;
35750 tree prefix_attributes
;
35752 cp_parser_decl_specifier_seq (parser
,
35753 CP_PARSER_FLAGS_NONE
,
35755 &decl_class_or_enum_p
);
35757 if (declspecs
.type
== error_mark_node
)
35758 return error_mark_node
;
35760 /* auto, register, static, extern, mutable. */
35761 if (declspecs
.storage_class
!= sc_none
)
35763 cp_parser_error (parser
, "invalid type for property");
35764 declspecs
.storage_class
= sc_none
;
35767 /* thread_local. */
35768 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
35770 cp_parser_error (parser
, "invalid type for property");
35771 declspecs
.locations
[ds_thread
] = 0;
35775 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
35777 cp_parser_error (parser
, "invalid type for property");
35778 declspecs
.locations
[ds_typedef
] = 0;
35781 prefix_attributes
= declspecs
.attributes
;
35782 declspecs
.attributes
= NULL_TREE
;
35784 /* Keep going until we hit the `;' at the end of the declaration. */
35785 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35787 tree attributes
, first_attribute
, decl
;
35788 cp_declarator
*declarator
;
35791 /* Parse the declarator. */
35792 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
35793 CP_PARSER_FLAGS_NONE
,
35794 NULL
, NULL
, false, false, false);
35796 /* Look for attributes that apply to the ivar. */
35797 attributes
= cp_parser_attributes_opt (parser
);
35798 /* Remember which attributes are prefix attributes and
35800 first_attribute
= attributes
;
35801 /* Combine the attributes. */
35802 attributes
= attr_chainon (prefix_attributes
, attributes
);
35804 decl
= grokfield (declarator
, &declspecs
,
35805 NULL_TREE
, /*init_const_expr_p=*/false,
35806 NULL_TREE
, attributes
);
35808 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
35809 return error_mark_node
;
35811 /* Reset PREFIX_ATTRIBUTES. */
35812 if (attributes
!= error_mark_node
)
35814 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
35815 attributes
= TREE_CHAIN (attributes
);
35817 TREE_CHAIN (attributes
) = NULL_TREE
;
35820 DECL_CHAIN (decl
) = decls
;
35823 token
= cp_lexer_peek_token (parser
->lexer
);
35824 if (token
->type
== CPP_COMMA
)
35826 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
35835 /* Parse an Objective-C @property declaration. The syntax is:
35837 objc-property-declaration:
35838 '@property' objc-property-attributes[opt] struct-declaration ;
35840 objc-property-attributes:
35841 '(' objc-property-attribute-list ')'
35843 objc-property-attribute-list:
35844 objc-property-attribute
35845 objc-property-attribute-list, objc-property-attribute
35847 objc-property-attribute
35848 'getter' = identifier
35849 'setter' = identifier
35858 @property NSString *name;
35859 @property (readonly) id object;
35860 @property (retain, nonatomic, getter=getTheName) id name;
35861 @property int a, b, c;
35863 PS: This function is identical to
35864 c_parser_objc_at_property_declaration for C. Keep them in sync. */
35866 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
35868 /* Parse the optional attribute list.
35870 A list of parsed, but not verified, attributes. */
35871 auto_delete_vec
<property_attribute_info
> prop_attr_list
;
35872 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35874 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
35876 /* Parse the optional attribute list... */
35877 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35880 matching_parens parens
;
35881 location_t attr_start
= cp_lexer_peek_token (parser
->lexer
)->location
;
35882 parens
.consume_open (parser
);
35883 bool syntax_error
= false;
35885 /* Allow empty @property attribute lists, but with a warning. */
35886 location_t attr_end
= cp_lexer_peek_token (parser
->lexer
)->location
;
35887 location_t attr_comb
;
35888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
35890 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
35891 warning_at (attr_comb
, OPT_Wattributes
,
35892 "empty property attribute list");
35897 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35898 attr_start
= token
->location
;
35899 attr_end
= get_finish (token
->location
);
35900 attr_comb
= make_location (attr_start
, attr_start
, attr_end
);
35902 if (token
->type
== CPP_CLOSE_PAREN
|| token
->type
== CPP_COMMA
)
35904 warning_at (attr_comb
, OPT_Wattributes
,
35905 "missing property attribute");
35906 if (token
->type
== CPP_CLOSE_PAREN
)
35908 cp_lexer_consume_token (parser
->lexer
);
35912 tree attr_name
= NULL_TREE
;
35913 if (identifier_p (token
->u
.value
))
35914 attr_name
= token
->u
.value
;
35917 if (token
->type
== CPP_NAME
)
35918 keyword
= C_RID_CODE (token
->u
.value
);
35919 else if (token
->type
== CPP_KEYWORD
35920 && token
->keyword
== RID_CLASS
)
35921 /* Account for accepting the 'class' keyword in this context. */
35922 keyword
= RID_CLASS
;
35924 keyword
= RID_MAX
; /* By definition, an unknown property. */
35925 cp_lexer_consume_token (parser
->lexer
);
35927 enum objc_property_attribute_kind prop_kind
35928 = objc_prop_attr_kind_for_rid (keyword
);
35929 property_attribute_info
*prop
35930 = new property_attribute_info (attr_name
, attr_comb
, prop_kind
);
35931 prop_attr_list
.safe_push (prop
);
35934 switch (prop
->prop_kind
)
35937 case OBJC_PROPERTY_ATTR_UNKNOWN
:
35939 error_at (attr_start
, "unknown property attribute %qE",
35942 error_at (attr_start
, "unknown property attribute");
35943 prop
->parse_error
= syntax_error
= true;
35946 case OBJC_PROPERTY_ATTR_GETTER
:
35947 case OBJC_PROPERTY_ATTR_SETTER
:
35948 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
35950 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
35951 error_at (attr_comb
, "expected %<=%> after Objective-C %qE",
35953 prop
->parse_error
= syntax_error
= true;
35957 token
= cp_lexer_peek_token (parser
->lexer
);
35958 attr_end
= token
->location
;
35959 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
35961 if (!cp_parser_objc_selector_p
35962 (cp_lexer_peek_token (parser
->lexer
)->type
))
35964 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
35965 error_at (attr_comb
, "expected %qE selector name",
35967 prop
->parse_error
= syntax_error
= true;
35971 /* Get the end of the method name, and consume the name. */
35972 token
= cp_lexer_peek_token (parser
->lexer
);
35973 attr_end
= get_finish (token
->location
);
35974 /* Because method names may contain C++ keywords, we have a
35975 routine to fetch them (this also consumes the token). */
35976 meth_name
= cp_parser_objc_selector (parser
);
35978 if (prop
->prop_kind
== OBJC_PROPERTY_ATTR_SETTER
)
35980 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
35982 attr_comb
= make_location (attr_end
, attr_start
,
35984 error_at (attr_comb
, "setter method names must"
35985 " terminate with %<:%>");
35986 prop
->parse_error
= syntax_error
= true;
35990 attr_end
= get_finish (cp_lexer_peek_token
35991 (parser
->lexer
)->location
);
35992 cp_lexer_consume_token (parser
->lexer
);
35994 attr_comb
= make_location (attr_start
, attr_start
,
35998 attr_comb
= make_location (attr_start
, attr_start
,
36000 prop
->ident
= meth_name
;
36001 /* Updated location including all that was successfully
36003 prop
->prop_loc
= attr_comb
;
36007 /* If we see a comma here, then keep going - even if we already
36008 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
36009 this makes a more useful output and avoid spurious warnings
36010 about missing attributes that are, in fact, specified after the
36011 one with the syntax error. */
36012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36013 cp_lexer_consume_token (parser
->lexer
);
36018 if (syntax_error
|| !parens
.require_close (parser
))
36019 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36020 /*or_comma=*/false,
36021 /*consume_paren=*/true);
36024 /* 'properties' is the list of properties that we read. Usually a
36025 single one, but maybe more (eg, in "@property int a, b, c;" there
36027 TODO: Update this parsing so that it accepts (erroneous) bitfields so
36028 that we can issue a meaningful and consistent (between C/C++) error
36029 message from objc_add_property_declaration (). */
36030 tree properties
= cp_parser_objc_struct_declaration (parser
);
36032 if (properties
== error_mark_node
)
36033 cp_parser_skip_to_end_of_statement (parser
);
36034 else if (properties
== NULL_TREE
)
36035 cp_parser_error (parser
, "expected identifier");
36038 /* Comma-separated properties are chained together in reverse order;
36039 add them one by one. */
36040 properties
= nreverse (properties
);
36041 for (; properties
; properties
= TREE_CHAIN (properties
))
36042 objc_add_property_declaration (loc
, copy_node (properties
),
36046 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36049 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
36051 objc-synthesize-declaration:
36052 @synthesize objc-synthesize-identifier-list ;
36054 objc-synthesize-identifier-list:
36055 objc-synthesize-identifier
36056 objc-synthesize-identifier-list, objc-synthesize-identifier
36058 objc-synthesize-identifier
36060 identifier = identifier
36063 @synthesize MyProperty;
36064 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
36066 PS: This function is identical to c_parser_objc_at_synthesize_declaration
36067 for C. Keep them in sync.
36070 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
36072 tree list
= NULL_TREE
;
36074 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36076 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
36079 tree property
, ivar
;
36080 property
= cp_parser_identifier (parser
);
36081 if (property
== error_mark_node
)
36083 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
36088 cp_lexer_consume_token (parser
->lexer
);
36089 ivar
= cp_parser_identifier (parser
);
36090 if (ivar
== error_mark_node
)
36092 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36098 list
= chainon (list
, build_tree_list (ivar
, property
));
36099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36100 cp_lexer_consume_token (parser
->lexer
);
36104 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36105 objc_add_synthesize_declaration (loc
, list
);
36108 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
36110 objc-dynamic-declaration:
36111 @dynamic identifier-list ;
36114 @dynamic MyProperty;
36115 @dynamic MyProperty, AnotherProperty;
36117 PS: This function is identical to c_parser_objc_at_dynamic_declaration
36118 for C. Keep them in sync.
36121 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
36123 tree list
= NULL_TREE
;
36125 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36127 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
36131 property
= cp_parser_identifier (parser
);
36132 if (property
== error_mark_node
)
36134 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36137 list
= chainon (list
, build_tree_list (NULL
, property
));
36138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36139 cp_lexer_consume_token (parser
->lexer
);
36143 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36144 objc_add_dynamic_declaration (loc
, list
);
36148 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
36150 /* Returns name of the next clause.
36151 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
36152 the token is not consumed. Otherwise appropriate pragma_omp_clause is
36153 returned and the token is consumed. */
36155 static pragma_omp_clause
36156 cp_parser_omp_clause_name (cp_parser
*parser
)
36158 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
36160 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
36161 result
= PRAGMA_OACC_CLAUSE_AUTO
;
36162 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
36163 result
= PRAGMA_OMP_CLAUSE_IF
;
36164 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
36165 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
36166 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
36167 result
= PRAGMA_OACC_CLAUSE_DELETE
;
36168 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
36169 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
36170 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36171 result
= PRAGMA_OMP_CLAUSE_FOR
;
36172 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36174 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36175 const char *p
= IDENTIFIER_POINTER (id
);
36180 if (!strcmp ("affinity", p
))
36181 result
= PRAGMA_OMP_CLAUSE_AFFINITY
;
36182 else if (!strcmp ("aligned", p
))
36183 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
36184 else if (!strcmp ("allocate", p
))
36185 result
= PRAGMA_OMP_CLAUSE_ALLOCATE
;
36186 else if (!strcmp ("async", p
))
36187 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
36188 else if (!strcmp ("attach", p
))
36189 result
= PRAGMA_OACC_CLAUSE_ATTACH
;
36192 if (!strcmp ("bind", p
))
36193 result
= PRAGMA_OMP_CLAUSE_BIND
;
36196 if (!strcmp ("collapse", p
))
36197 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
36198 else if (!strcmp ("copy", p
))
36199 result
= PRAGMA_OACC_CLAUSE_COPY
;
36200 else if (!strcmp ("copyin", p
))
36201 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
36202 else if (!strcmp ("copyout", p
))
36203 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
36204 else if (!strcmp ("copyprivate", p
))
36205 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
36206 else if (!strcmp ("create", p
))
36207 result
= PRAGMA_OACC_CLAUSE_CREATE
;
36210 if (!strcmp ("defaultmap", p
))
36211 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
36212 else if (!strcmp ("depend", p
))
36213 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
36214 else if (!strcmp ("detach", p
))
36215 result
= PRAGMA_OACC_CLAUSE_DETACH
;
36216 else if (!strcmp ("device", p
))
36217 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
36218 else if (!strcmp ("deviceptr", p
))
36219 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
36220 else if (!strcmp ("device_resident", p
))
36221 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
36222 else if (!strcmp ("device_type", p
))
36223 result
= PRAGMA_OMP_CLAUSE_DEVICE_TYPE
;
36224 else if (!strcmp ("dist_schedule", p
))
36225 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
36228 if (!strcmp ("filter", p
))
36229 result
= PRAGMA_OMP_CLAUSE_FILTER
;
36230 else if (!strcmp ("final", p
))
36231 result
= PRAGMA_OMP_CLAUSE_FINAL
;
36232 else if (!strcmp ("finalize", p
))
36233 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
36234 else if (!strcmp ("firstprivate", p
))
36235 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
36236 else if (!strcmp ("from", p
))
36237 result
= PRAGMA_OMP_CLAUSE_FROM
;
36240 if (!strcmp ("gang", p
))
36241 result
= PRAGMA_OACC_CLAUSE_GANG
;
36242 else if (!strcmp ("grainsize", p
))
36243 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
36246 if (!strcmp ("hint", p
))
36247 result
= PRAGMA_OMP_CLAUSE_HINT
;
36248 else if (!strcmp ("host", p
))
36249 result
= PRAGMA_OACC_CLAUSE_HOST
;
36252 if (!strcmp ("if_present", p
))
36253 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
36254 else if (!strcmp ("in_reduction", p
))
36255 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
36256 else if (!strcmp ("inbranch", p
))
36257 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
36258 else if (!strcmp ("independent", p
))
36259 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
36260 else if (!strcmp ("is_device_ptr", p
))
36261 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
36264 if (!strcmp ("lastprivate", p
))
36265 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
36266 else if (!strcmp ("linear", p
))
36267 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
36268 else if (!strcmp ("link", p
))
36269 result
= PRAGMA_OMP_CLAUSE_LINK
;
36272 if (!strcmp ("map", p
))
36273 result
= PRAGMA_OMP_CLAUSE_MAP
;
36274 else if (!strcmp ("mergeable", p
))
36275 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
36278 if (!strcmp ("no_create", p
))
36279 result
= PRAGMA_OACC_CLAUSE_NO_CREATE
;
36280 else if (!strcmp ("nogroup", p
))
36281 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
36282 else if (!strcmp ("nohost", p
))
36283 result
= PRAGMA_OACC_CLAUSE_NOHOST
;
36284 else if (!strcmp ("nontemporal", p
))
36285 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
36286 else if (!strcmp ("notinbranch", p
))
36287 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
36288 else if (!strcmp ("nowait", p
))
36289 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
36290 else if (!strcmp ("num_gangs", p
))
36291 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
36292 else if (!strcmp ("num_tasks", p
))
36293 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
36294 else if (!strcmp ("num_teams", p
))
36295 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
36296 else if (!strcmp ("num_threads", p
))
36297 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
36298 else if (!strcmp ("num_workers", p
))
36299 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
36302 if (!strcmp ("ordered", p
))
36303 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
36304 else if (!strcmp ("order", p
))
36305 result
= PRAGMA_OMP_CLAUSE_ORDER
;
36308 if (!strcmp ("parallel", p
))
36309 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
36310 else if (!strcmp ("present", p
))
36311 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
36312 else if (!strcmp ("present_or_copy", p
)
36313 || !strcmp ("pcopy", p
))
36314 result
= PRAGMA_OACC_CLAUSE_COPY
;
36315 else if (!strcmp ("present_or_copyin", p
)
36316 || !strcmp ("pcopyin", p
))
36317 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
36318 else if (!strcmp ("present_or_copyout", p
)
36319 || !strcmp ("pcopyout", p
))
36320 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
36321 else if (!strcmp ("present_or_create", p
)
36322 || !strcmp ("pcreate", p
))
36323 result
= PRAGMA_OACC_CLAUSE_CREATE
;
36324 else if (!strcmp ("priority", p
))
36325 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
36326 else if (!strcmp ("proc_bind", p
))
36327 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
36330 if (!strcmp ("reduction", p
))
36331 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
36334 if (!strcmp ("safelen", p
))
36335 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
36336 else if (!strcmp ("schedule", p
))
36337 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
36338 else if (!strcmp ("sections", p
))
36339 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
36340 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
36341 result
= PRAGMA_OACC_CLAUSE_HOST
;
36342 else if (!strcmp ("seq", p
))
36343 result
= PRAGMA_OACC_CLAUSE_SEQ
;
36344 else if (!strcmp ("shared", p
))
36345 result
= PRAGMA_OMP_CLAUSE_SHARED
;
36346 else if (!strcmp ("simd", p
))
36347 result
= PRAGMA_OMP_CLAUSE_SIMD
;
36348 else if (!strcmp ("simdlen", p
))
36349 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
36352 if (!strcmp ("task_reduction", p
))
36353 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
36354 else if (!strcmp ("taskgroup", p
))
36355 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
36356 else if (!strcmp ("thread_limit", p
))
36357 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
36358 else if (!strcmp ("threads", p
))
36359 result
= PRAGMA_OMP_CLAUSE_THREADS
;
36360 else if (!strcmp ("tile", p
))
36361 result
= PRAGMA_OACC_CLAUSE_TILE
;
36362 else if (!strcmp ("to", p
))
36363 result
= PRAGMA_OMP_CLAUSE_TO
;
36366 if (!strcmp ("uniform", p
))
36367 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
36368 else if (!strcmp ("untied", p
))
36369 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
36370 else if (!strcmp ("use_device", p
))
36371 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
36372 else if (!strcmp ("use_device_addr", p
))
36373 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
;
36374 else if (!strcmp ("use_device_ptr", p
))
36375 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
36378 if (!strcmp ("vector", p
))
36379 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
36380 else if (!strcmp ("vector_length", p
))
36381 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
36384 if (!strcmp ("wait", p
))
36385 result
= PRAGMA_OACC_CLAUSE_WAIT
;
36386 else if (!strcmp ("worker", p
))
36387 result
= PRAGMA_OACC_CLAUSE_WORKER
;
36392 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
36393 cp_lexer_consume_token (parser
->lexer
);
36398 /* Validate that a clause of the given type does not already exist. */
36401 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
36402 const char *name
, location_t location
)
36404 if (omp_find_clause (clauses
, code
))
36405 error_at (location
, "too many %qs clauses", name
);
36411 variable-list , identifier
36413 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
36414 colon). An opening parenthesis will have been consumed by the caller.
36416 If KIND is nonzero, create the appropriate node and install the decl
36417 in OMP_CLAUSE_DECL and add the node to the head of the list.
36419 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
36420 return the list created.
36422 COLON can be NULL if only closing parenthesis should end the list,
36423 or pointer to bool which will receive false if the list is terminated
36424 by closing parenthesis or true if the list is terminated by colon.
36426 The optional ALLOW_DEREF argument is true if list items can use the deref
36431 tree low_bound
, length
;
36434 omp_dim (tree lb
, tree len
, location_t lo
, bool nc
)
36435 : low_bound (lb
), length (len
), loc (lo
), no_colon (nc
) {}
36439 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
36440 tree list
, bool *colon
,
36441 bool allow_deref
= false)
36443 auto_vec
<omp_dim
> dims
;
36444 bool array_section_p
;
36446 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
36449 parser
->colon_corrects_to_scope_p
= false;
36456 if (kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36457 cp_parser_parse_tentatively (parser
);
36458 token
= cp_lexer_peek_token (parser
->lexer
);
36460 && cp_parser_is_keyword (token
, RID_THIS
))
36462 decl
= finish_this_expr ();
36463 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
36464 || CONVERT_EXPR_P (decl
))
36465 decl
= TREE_OPERAND (decl
, 0);
36466 cp_lexer_consume_token (parser
->lexer
);
36468 else if (cp_parser_is_keyword (token
, RID_FUNCTION_NAME
)
36469 || cp_parser_is_keyword (token
, RID_PRETTY_FUNCTION_NAME
)
36470 || cp_parser_is_keyword (token
, RID_C99_FUNCTION_NAME
))
36473 decl
= cp_parser_primary_expression (parser
, false, false, false,
36478 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36479 /*check_dependency_p=*/true,
36480 /*template_p=*/NULL
,
36481 /*declarator_p=*/false,
36482 /*optional_p=*/false);
36483 if (name
== error_mark_node
)
36485 if ((kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36486 && cp_parser_simulate_error (parser
))
36487 goto depend_lvalue
;
36491 if (identifier_p (name
))
36492 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
36495 if (decl
== error_mark_node
)
36497 if ((kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36498 && cp_parser_simulate_error (parser
))
36499 goto depend_lvalue
;
36500 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
36504 if (outer_automatic_var_p (decl
))
36505 decl
= process_outer_var_ref (decl
, tf_warning_or_error
);
36506 if (decl
== error_mark_node
)
36508 else if (kind
!= 0)
36512 case OMP_CLAUSE__CACHE_
:
36513 /* The OpenACC cache directive explicitly only allows "array
36514 elements or subarrays". */
36515 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
36517 error_at (token
->location
, "expected %<[%>");
36518 decl
= error_mark_node
;
36522 case OMP_CLAUSE_MAP
:
36523 case OMP_CLAUSE_FROM
:
36524 case OMP_CLAUSE_TO
:
36525 start_component_ref
:
36526 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
36528 && cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
)))
36531 = cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
36532 ? CPP_DOT
: CPP_DEREF
;
36534 = cp_lexer_peek_token (parser
->lexer
)->location
;
36535 cp_id_kind idk
= CP_ID_KIND_NONE
;
36536 cp_lexer_consume_token (parser
->lexer
);
36537 decl
= convert_from_reference (decl
);
36539 = cp_parser_postfix_dot_deref_expression (parser
, ttype
,
36544 case OMP_CLAUSE_AFFINITY
:
36545 case OMP_CLAUSE_DEPEND
:
36546 case OMP_CLAUSE_REDUCTION
:
36547 case OMP_CLAUSE_IN_REDUCTION
:
36548 case OMP_CLAUSE_TASK_REDUCTION
:
36549 array_section_p
= false;
36551 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
36553 location_t loc
= UNKNOWN_LOCATION
;
36554 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
36555 bool no_colon
= false;
36557 parser
->colon_corrects_to_scope_p
= false;
36558 cp_lexer_consume_token (parser
->lexer
);
36559 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
36561 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36562 low_bound
= cp_parser_expression (parser
);
36563 /* Later handling is not prepared to see through these. */
36564 gcc_checking_assert (!location_wrapper_p (low_bound
));
36567 parser
->colon_corrects_to_scope_p
36568 = saved_colon_corrects_to_scope_p
;
36569 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
36571 length
= integer_one_node
;
36576 /* Look for `:'. */
36577 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36579 if ((kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36580 && cp_parser_simulate_error (parser
))
36581 goto depend_lvalue
;
36584 if (kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36585 cp_parser_commit_to_tentative_parse (parser
);
36587 array_section_p
= true;
36588 if (!cp_lexer_next_token_is (parser
->lexer
,
36591 length
= cp_parser_expression (parser
);
36592 /* Later handling is not prepared to see through these. */
36593 gcc_checking_assert (!location_wrapper_p (length
));
36596 /* Look for the closing `]'. */
36597 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
36600 if ((kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36601 && cp_parser_simulate_error (parser
))
36602 goto depend_lvalue
;
36606 dims
.safe_push (omp_dim (low_bound
, length
, loc
, no_colon
));
36609 if ((kind
== OMP_CLAUSE_MAP
36610 || kind
== OMP_CLAUSE_FROM
36611 || kind
== OMP_CLAUSE_TO
)
36612 && !array_section_p
36613 && (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
36615 && cp_lexer_next_token_is (parser
->lexer
,
36618 for (unsigned i
= 0; i
< dims
.length (); i
++)
36620 gcc_assert (dims
[i
].length
== integer_one_node
);
36621 decl
= build_array_ref (dims
[i
].loc
,
36622 decl
, dims
[i
].low_bound
);
36624 goto start_component_ref
;
36627 for (unsigned i
= 0; i
< dims
.length (); i
++)
36628 decl
= tree_cons (dims
[i
].low_bound
, dims
[i
].length
, decl
);
36635 if (kind
== OMP_CLAUSE_DEPEND
|| kind
== OMP_CLAUSE_AFFINITY
)
36637 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
36638 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
36639 && cp_parser_simulate_error (parser
))
36642 cp_parser_abort_tentative_parse (parser
);
36643 decl
= cp_parser_assignment_expression (parser
, NULL
,
36647 cp_parser_parse_definitely (parser
);
36650 tree u
= build_omp_clause (token
->location
, kind
);
36651 OMP_CLAUSE_DECL (u
) = decl
;
36652 OMP_CLAUSE_CHAIN (u
) = list
;
36656 list
= tree_cons (decl
, NULL_TREE
, list
);
36659 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
36661 cp_lexer_consume_token (parser
->lexer
);
36665 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
36667 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
36670 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36674 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36678 /* Try to resync to an unnested comma. Copied from
36679 cp_parser_parenthesized_expression_list. */
36682 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
36683 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
36684 /*recovering=*/true,
36686 /*consume_paren=*/true);
36694 /* Similarly, but expect leading and trailing parenthesis. This is a very
36695 common case for omp clauses. */
36698 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
,
36699 bool allow_deref
= false)
36701 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36702 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
,
36708 copy ( variable-list )
36709 copyin ( variable-list )
36710 copyout ( variable-list )
36711 create ( variable-list )
36712 delete ( variable-list )
36713 present ( variable-list )
36716 no_create ( variable-list )
36717 attach ( variable-list )
36718 detach ( variable-list ) */
36721 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
36724 enum gomp_map_kind kind
;
36727 case PRAGMA_OACC_CLAUSE_ATTACH
:
36728 kind
= GOMP_MAP_ATTACH
;
36730 case PRAGMA_OACC_CLAUSE_COPY
:
36731 kind
= GOMP_MAP_TOFROM
;
36733 case PRAGMA_OACC_CLAUSE_COPYIN
:
36734 kind
= GOMP_MAP_TO
;
36736 case PRAGMA_OACC_CLAUSE_COPYOUT
:
36737 kind
= GOMP_MAP_FROM
;
36739 case PRAGMA_OACC_CLAUSE_CREATE
:
36740 kind
= GOMP_MAP_ALLOC
;
36742 case PRAGMA_OACC_CLAUSE_DELETE
:
36743 kind
= GOMP_MAP_RELEASE
;
36745 case PRAGMA_OACC_CLAUSE_DETACH
:
36746 kind
= GOMP_MAP_DETACH
;
36748 case PRAGMA_OACC_CLAUSE_DEVICE
:
36749 kind
= GOMP_MAP_FORCE_TO
;
36751 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
36752 kind
= GOMP_MAP_DEVICE_RESIDENT
;
36754 case PRAGMA_OACC_CLAUSE_HOST
:
36755 kind
= GOMP_MAP_FORCE_FROM
;
36757 case PRAGMA_OACC_CLAUSE_LINK
:
36758 kind
= GOMP_MAP_LINK
;
36760 case PRAGMA_OACC_CLAUSE_NO_CREATE
:
36761 kind
= GOMP_MAP_IF_PRESENT
;
36763 case PRAGMA_OACC_CLAUSE_PRESENT
:
36764 kind
= GOMP_MAP_FORCE_PRESENT
;
36767 gcc_unreachable ();
36770 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
, true);
36772 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36773 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
36779 deviceptr ( variable-list ) */
36782 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
36784 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36787 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
36788 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
36789 variable-list must only allow for pointer variables. */
36790 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36791 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
36793 tree v
= TREE_PURPOSE (t
);
36794 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
36795 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
36796 OMP_CLAUSE_DECL (u
) = v
;
36797 OMP_CLAUSE_CHAIN (u
) = list
;
36812 cp_parser_oacc_simple_clause (location_t loc
, enum omp_clause_code code
,
36815 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], loc
);
36817 tree c
= build_omp_clause (loc
, code
);
36818 OMP_CLAUSE_CHAIN (c
) = list
;
36824 num_gangs ( expression )
36825 num_workers ( expression )
36826 vector_length ( expression ) */
36829 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
36830 const char *str
, tree list
)
36832 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36834 matching_parens parens
;
36835 if (!parens
.require_open (parser
))
36838 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
36840 if (t
== error_mark_node
36841 || !parens
.require_close (parser
))
36843 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36844 /*or_comma=*/false,
36845 /*consume_paren=*/true);
36849 check_no_duplicate_clause (list
, code
, str
, loc
);
36851 tree c
= build_omp_clause (loc
, code
);
36852 OMP_CLAUSE_OPERAND (c
, 0) = t
;
36853 OMP_CLAUSE_CHAIN (c
) = list
;
36859 gang [( gang-arg-list )]
36860 worker [( [num:] int-expr )]
36861 vector [( [length:] int-expr )]
36863 where gang-arg is one of:
36868 and size-expr may be:
36875 cp_parser_oacc_shape_clause (cp_parser
*parser
, location_t loc
,
36876 omp_clause_code kind
,
36877 const char *str
, tree list
)
36879 const char *id
= "num";
36880 cp_lexer
*lexer
= parser
->lexer
;
36881 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
36883 if (kind
== OMP_CLAUSE_VECTOR
)
36886 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
36888 matching_parens parens
;
36889 parens
.consume_open (parser
);
36893 cp_token
*next
= cp_lexer_peek_token (lexer
);
36896 /* Gang static argument. */
36897 if (kind
== OMP_CLAUSE_GANG
36898 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
36900 cp_lexer_consume_token (lexer
);
36902 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36903 goto cleanup_error
;
36906 if (ops
[idx
] != NULL
)
36908 cp_parser_error (parser
, "too many %<static%> arguments");
36909 goto cleanup_error
;
36912 /* Check for the '*' argument. */
36913 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
36914 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
36915 || cp_lexer_nth_token_is (parser
->lexer
, 2,
36918 cp_lexer_consume_token (lexer
);
36919 ops
[idx
] = integer_minus_one_node
;
36921 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
36923 cp_lexer_consume_token (lexer
);
36929 /* Worker num: argument and vector length: arguments. */
36930 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
36931 && id_equal (next
->u
.value
, id
)
36932 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
36934 cp_lexer_consume_token (lexer
); /* id */
36935 cp_lexer_consume_token (lexer
); /* ':' */
36938 /* Now collect the actual argument. */
36939 if (ops
[idx
] != NULL_TREE
)
36941 cp_parser_error (parser
, "unexpected argument");
36942 goto cleanup_error
;
36945 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
36947 if (expr
== error_mark_node
)
36948 goto cleanup_error
;
36950 mark_exp_read (expr
);
36953 if (kind
== OMP_CLAUSE_GANG
36954 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
36956 cp_lexer_consume_token (lexer
);
36963 if (!parens
.require_close (parser
))
36964 goto cleanup_error
;
36967 check_no_duplicate_clause (list
, kind
, str
, loc
);
36969 c
= build_omp_clause (loc
, kind
);
36972 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
36974 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
36975 OMP_CLAUSE_CHAIN (c
) = list
;
36980 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
36985 tile ( size-expr-list ) */
36988 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
36990 tree c
, expr
= error_mark_node
;
36991 tree tile
= NULL_TREE
;
36993 /* Collapse and tile are mutually exclusive. (The spec doesn't say
36994 so, but the spec authors never considered such a case and have
36995 differing opinions on what it might mean, including 'not
36997 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
36998 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
37001 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37006 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
37009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
37010 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
37011 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
37013 cp_lexer_consume_token (parser
->lexer
);
37014 expr
= integer_zero_node
;
37017 expr
= cp_parser_constant_expression (parser
);
37019 tile
= tree_cons (NULL_TREE
, expr
, tile
);
37021 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
37023 /* Consume the trailing ')'. */
37024 cp_lexer_consume_token (parser
->lexer
);
37026 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
37027 tile
= nreverse (tile
);
37028 OMP_CLAUSE_TILE_LIST (c
) = tile
;
37029 OMP_CLAUSE_CHAIN (c
) = list
;
37034 Parse wait clause or directive parameters. */
37037 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
37039 vec
<tree
, va_gc
> *args
;
37042 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
37044 /*allow_expansion_p=*/true,
37045 /*non_constant_p=*/NULL
);
37047 if (args
== NULL
|| args
->length () == 0)
37051 cp_parser_error (parser
, "expected integer expression list");
37052 release_tree_vector (args
);
37057 args_tree
= build_tree_list_vec (args
);
37059 release_tree_vector (args
);
37061 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
37063 tree targ
= TREE_VALUE (t
);
37065 if (targ
!= error_mark_node
)
37067 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
37068 error ("%<wait%> expression must be integral");
37071 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
37073 targ
= mark_rvalue_use (targ
);
37074 OMP_CLAUSE_DECL (c
) = targ
;
37075 OMP_CLAUSE_CHAIN (c
) = list
;
37085 wait [( int-expr-list )] */
37088 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
37090 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
37092 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37093 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
37096 tree c
= build_omp_clause (location
, OMP_CLAUSE_WAIT
);
37098 OMP_CLAUSE_DECL (c
) = build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
37099 OMP_CLAUSE_CHAIN (c
) = list
;
37107 collapse ( constant-expression ) */
37110 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
37116 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37117 matching_parens parens
;
37118 if (!parens
.require_open (parser
))
37121 num
= cp_parser_constant_expression (parser
);
37123 if (!parens
.require_close (parser
))
37124 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37125 /*or_comma=*/false,
37126 /*consume_paren=*/true);
37128 if (num
== error_mark_node
)
37130 num
= fold_non_dependent_expr (num
);
37131 if (!tree_fits_shwi_p (num
)
37132 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
37133 || (n
= tree_to_shwi (num
)) <= 0
37136 error_at (loc
, "collapse argument needs positive constant integer expression");
37140 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
37141 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
37142 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
37143 OMP_CLAUSE_CHAIN (c
) = list
;
37144 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
37150 default ( none | shared )
37153 default ( private | firstprivate )
37156 default ( none | present ) */
37159 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
37160 location_t location
, bool is_oacc
)
37162 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
37165 matching_parens parens
;
37166 if (!parens
.require_open (parser
))
37168 if (!is_oacc
&& cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
37170 kind
= OMP_CLAUSE_DEFAULT_PRIVATE
;
37171 cp_lexer_consume_token (parser
->lexer
);
37173 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37175 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37176 const char *p
= IDENTIFIER_POINTER (id
);
37181 if (strcmp ("none", p
) != 0)
37183 kind
= OMP_CLAUSE_DEFAULT_NONE
;
37187 if (strcmp ("present", p
) != 0 || !is_oacc
)
37189 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
37193 if (strcmp ("firstprivate", p
) != 0 || is_oacc
)
37195 kind
= OMP_CLAUSE_DEFAULT_FIRSTPRIVATE
;
37199 if (strcmp ("shared", p
) != 0 || is_oacc
)
37201 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
37208 cp_lexer_consume_token (parser
->lexer
);
37214 cp_parser_error (parser
, "expected %<none%> or %<present%>");
37216 cp_parser_error (parser
, "expected %<none%>, %<shared%>, "
37217 "%<private%> or %<firstprivate%>");
37220 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
37221 || !parens
.require_close (parser
))
37222 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37223 /*or_comma=*/false,
37224 /*consume_paren=*/true);
37226 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
37229 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
37230 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
37231 OMP_CLAUSE_CHAIN (c
) = list
;
37232 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
37238 final ( expression ) */
37241 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
37245 matching_parens parens
;
37246 if (!parens
.require_open (parser
))
37249 t
= cp_parser_assignment_expression (parser
);
37251 if (t
== error_mark_node
37252 || !parens
.require_close (parser
))
37253 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37254 /*or_comma=*/false,
37255 /*consume_paren=*/true);
37257 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
37259 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
37260 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
37261 OMP_CLAUSE_CHAIN (c
) = list
;
37270 if ( directive-name-modifier : expression )
37272 directive-name-modifier:
37273 parallel | task | taskloop | target data | target | target update
37274 | target enter data | target exit data
37277 directive-name-modifier:
37278 ... | simd | cancel */
37281 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
37285 enum tree_code if_modifier
= ERROR_MARK
;
37287 matching_parens parens
;
37288 if (!parens
.require_open (parser
))
37291 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37293 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37294 const char *p
= IDENTIFIER_POINTER (id
);
37297 if (strcmp ("cancel", p
) == 0)
37298 if_modifier
= VOID_CST
;
37299 else if (strcmp ("parallel", p
) == 0)
37300 if_modifier
= OMP_PARALLEL
;
37301 else if (strcmp ("simd", p
) == 0)
37302 if_modifier
= OMP_SIMD
;
37303 else if (strcmp ("task", p
) == 0)
37304 if_modifier
= OMP_TASK
;
37305 else if (strcmp ("taskloop", p
) == 0)
37306 if_modifier
= OMP_TASKLOOP
;
37307 else if (strcmp ("target", p
) == 0)
37309 if_modifier
= OMP_TARGET
;
37310 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
37312 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
37313 p
= IDENTIFIER_POINTER (id
);
37314 if (strcmp ("data", p
) == 0)
37315 if_modifier
= OMP_TARGET_DATA
;
37316 else if (strcmp ("update", p
) == 0)
37317 if_modifier
= OMP_TARGET_UPDATE
;
37318 else if (strcmp ("enter", p
) == 0)
37319 if_modifier
= OMP_TARGET_ENTER_DATA
;
37320 else if (strcmp ("exit", p
) == 0)
37321 if_modifier
= OMP_TARGET_EXIT_DATA
;
37322 if (if_modifier
!= OMP_TARGET
)
37327 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
37328 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
37330 if_modifier
= ERROR_MARK
;
37332 if (if_modifier
== OMP_TARGET_ENTER_DATA
37333 || if_modifier
== OMP_TARGET_EXIT_DATA
)
37335 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
37337 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
37338 p
= IDENTIFIER_POINTER (id
);
37339 if (strcmp ("data", p
) == 0)
37345 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
37346 error_at (loc
, "expected %<data%>");
37347 if_modifier
= ERROR_MARK
;
37352 if (if_modifier
!= ERROR_MARK
)
37354 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
37357 cp_lexer_consume_token (parser
->lexer
);
37364 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
37365 error_at (loc
, "expected %<:%>");
37367 if_modifier
= ERROR_MARK
;
37372 t
= cp_parser_assignment_expression (parser
);
37374 if (t
== error_mark_node
37375 || !parens
.require_close (parser
))
37376 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37377 /*or_comma=*/false,
37378 /*consume_paren=*/true);
37380 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37381 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
37383 if (if_modifier
!= ERROR_MARK
37384 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
37386 const char *p
= NULL
;
37387 switch (if_modifier
)
37389 case VOID_CST
: p
= "cancel"; break;
37390 case OMP_PARALLEL
: p
= "parallel"; break;
37391 case OMP_SIMD
: p
= "simd"; break;
37392 case OMP_TASK
: p
= "task"; break;
37393 case OMP_TASKLOOP
: p
= "taskloop"; break;
37394 case OMP_TARGET_DATA
: p
= "target data"; break;
37395 case OMP_TARGET
: p
= "target"; break;
37396 case OMP_TARGET_UPDATE
: p
= "target update"; break;
37397 case OMP_TARGET_ENTER_DATA
: p
= "target enter data"; break;
37398 case OMP_TARGET_EXIT_DATA
: p
= "target exit data"; break;
37399 default: gcc_unreachable ();
37401 error_at (location
, "too many %<if%> clauses with %qs modifier",
37405 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
37408 error_at (location
, "too many %<if%> clauses");
37410 error_at (location
, "too many %<if%> clauses without modifier");
37413 else if (if_modifier
== ERROR_MARK
37414 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
37416 error_at (location
, "if any %<if%> clause has modifier, then all "
37417 "%<if%> clauses have to use modifier");
37422 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
37423 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
37424 OMP_CLAUSE_IF_EXPR (c
) = t
;
37425 OMP_CLAUSE_CHAIN (c
) = list
;
37434 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
37435 tree list
, location_t location
)
37439 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
37442 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
37443 OMP_CLAUSE_CHAIN (c
) = list
;
37451 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
37452 tree list
, location_t location
)
37456 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
37458 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
37459 OMP_CLAUSE_CHAIN (c
) = list
;
37464 num_threads ( expression ) */
37467 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
37468 location_t location
)
37472 matching_parens parens
;
37473 if (!parens
.require_open (parser
))
37476 t
= cp_parser_assignment_expression (parser
);
37478 if (t
== error_mark_node
37479 || !parens
.require_close (parser
))
37480 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37481 /*or_comma=*/false,
37482 /*consume_paren=*/true);
37484 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
37485 "num_threads", location
);
37487 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
37488 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
37489 OMP_CLAUSE_CHAIN (c
) = list
;
37495 num_tasks ( expression )
37498 num_tasks ( strict : expression ) */
37501 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
37502 location_t location
)
37506 matching_parens parens
;
37507 if (!parens
.require_open (parser
))
37510 bool strict
= false;
37511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
37512 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
37514 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37515 if (!strcmp (IDENTIFIER_POINTER (id
), "strict"))
37518 cp_lexer_consume_token (parser
->lexer
);
37519 cp_lexer_consume_token (parser
->lexer
);
37523 t
= cp_parser_assignment_expression (parser
);
37525 if (t
== error_mark_node
37526 || !parens
.require_close (parser
))
37527 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37528 /*or_comma=*/false,
37529 /*consume_paren=*/true);
37531 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
37532 "num_tasks", location
);
37534 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
37535 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
37536 OMP_CLAUSE_NUM_TASKS_STRICT (c
) = strict
;
37537 OMP_CLAUSE_CHAIN (c
) = list
;
37543 grainsize ( expression )
37546 grainsize ( strict : expression ) */
37549 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
37550 location_t location
)
37554 matching_parens parens
;
37555 if (!parens
.require_open (parser
))
37558 bool strict
= false;
37559 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
37560 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
37562 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37563 if (!strcmp (IDENTIFIER_POINTER (id
), "strict"))
37566 cp_lexer_consume_token (parser
->lexer
);
37567 cp_lexer_consume_token (parser
->lexer
);
37571 t
= cp_parser_assignment_expression (parser
);
37573 if (t
== error_mark_node
37574 || !parens
.require_close (parser
))
37575 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37576 /*or_comma=*/false,
37577 /*consume_paren=*/true);
37579 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
37580 "grainsize", location
);
37582 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
37583 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
37584 OMP_CLAUSE_GRAINSIZE_STRICT (c
) = strict
;
37585 OMP_CLAUSE_CHAIN (c
) = list
;
37591 priority ( expression ) */
37594 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
37595 location_t location
)
37599 matching_parens parens
;
37600 if (!parens
.require_open (parser
))
37603 t
= cp_parser_assignment_expression (parser
);
37605 if (t
== error_mark_node
37606 || !parens
.require_close (parser
))
37607 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37608 /*or_comma=*/false,
37609 /*consume_paren=*/true);
37611 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
37612 "priority", location
);
37614 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
37615 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
37616 OMP_CLAUSE_CHAIN (c
) = list
;
37622 hint ( expression ) */
37625 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
37629 matching_parens parens
;
37630 if (!parens
.require_open (parser
))
37633 t
= cp_parser_assignment_expression (parser
);
37635 if (t
!= error_mark_node
)
37637 t
= fold_non_dependent_expr (t
);
37638 if (!value_dependent_expression_p (t
)
37639 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
37640 || !tree_fits_shwi_p (t
)
37641 || tree_int_cst_sgn (t
) == -1))
37642 error_at (location
, "expected constant integer expression with "
37643 "valid sync-hint value");
37645 if (t
== error_mark_node
37646 || !parens
.require_close (parser
))
37647 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37648 /*or_comma=*/false,
37649 /*consume_paren=*/true);
37650 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
37652 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
37653 OMP_CLAUSE_HINT_EXPR (c
) = t
;
37654 OMP_CLAUSE_CHAIN (c
) = list
;
37660 filter ( integer-expression ) */
37663 cp_parser_omp_clause_filter (cp_parser
*parser
, tree list
, location_t location
)
37667 matching_parens parens
;
37668 if (!parens
.require_open (parser
))
37671 t
= cp_parser_assignment_expression (parser
);
37673 if (t
== error_mark_node
37674 || !parens
.require_close (parser
))
37675 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37676 /*or_comma=*/false,
37677 /*consume_paren=*/true);
37678 check_no_duplicate_clause (list
, OMP_CLAUSE_FILTER
, "filter", location
);
37680 c
= build_omp_clause (location
, OMP_CLAUSE_FILTER
);
37681 OMP_CLAUSE_FILTER_EXPR (c
) = t
;
37682 OMP_CLAUSE_CHAIN (c
) = list
;
37688 defaultmap ( tofrom : scalar )
37691 defaultmap ( implicit-behavior [ : variable-category ] ) */
37694 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
37695 location_t location
)
37699 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
37700 enum omp_clause_defaultmap_kind category
37701 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
37703 matching_parens parens
;
37704 if (!parens
.require_open (parser
))
37707 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
37709 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37712 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
37713 "%<tofrom%>, %<firstprivate%>, %<none%> "
37719 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37720 p
= IDENTIFIER_POINTER (id
);
37726 if (strcmp ("alloc", p
) == 0)
37727 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
37729 goto invalid_behavior
;
37733 if (strcmp ("default", p
) == 0)
37734 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
37736 goto invalid_behavior
;
37740 if (strcmp ("firstprivate", p
) == 0)
37741 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
37742 else if (strcmp ("from", p
) == 0)
37743 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
37745 goto invalid_behavior
;
37749 if (strcmp ("none", p
) == 0)
37750 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
37752 goto invalid_behavior
;
37756 if (strcmp ("tofrom", p
) == 0)
37757 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
37758 else if (strcmp ("to", p
) == 0)
37759 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
37761 goto invalid_behavior
;
37765 goto invalid_behavior
;
37767 cp_lexer_consume_token (parser
->lexer
);
37769 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
37771 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37774 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37777 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
37781 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37782 p
= IDENTIFIER_POINTER (id
);
37787 if (strcmp ("aggregate", p
) == 0)
37788 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
37790 goto invalid_category
;
37794 if (strcmp ("pointer", p
) == 0)
37795 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
37797 goto invalid_category
;
37801 if (strcmp ("scalar", p
) == 0)
37802 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
37804 goto invalid_category
;
37808 goto invalid_category
;
37811 cp_lexer_consume_token (parser
->lexer
);
37813 if (!parens
.require_close (parser
))
37816 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37817 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
37818 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
37819 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
37820 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
37821 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
37823 enum omp_clause_defaultmap_kind cat
= category
;
37824 location_t loc
= OMP_CLAUSE_LOCATION (c
);
37825 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
37826 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
37830 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
37833 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
37836 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
37839 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
37843 gcc_unreachable ();
37846 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
37849 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
37854 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
37855 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
37856 OMP_CLAUSE_CHAIN (c
) = list
;
37860 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37861 /*or_comma=*/false,
37862 /*consume_paren=*/true);
37867 order ( concurrent )
37870 order ( order-modifier : concurrent )
37877 cp_parser_omp_clause_order (cp_parser
*parser
, tree list
, location_t location
)
37881 bool unconstrained
= false;
37882 bool reproducible
= false;
37884 matching_parens parens
;
37885 if (!parens
.require_open (parser
))
37888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
37889 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
37891 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37892 p
= IDENTIFIER_POINTER (id
);
37893 if (strcmp (p
, "unconstrained") == 0)
37894 unconstrained
= true;
37895 else if (strcmp (p
, "reproducible") == 0)
37896 reproducible
= true;
37899 cp_parser_error (parser
, "expected %<reproducible%> or "
37900 "%<unconstrained%>");
37903 cp_lexer_consume_token (parser
->lexer
);
37904 cp_lexer_consume_token (parser
->lexer
);
37906 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37908 cp_parser_error (parser
, "expected %<concurrent%>");
37913 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37914 p
= IDENTIFIER_POINTER (id
);
37916 if (strcmp (p
, "concurrent") != 0)
37918 cp_parser_error (parser
, "expected %<concurrent%>");
37921 cp_lexer_consume_token (parser
->lexer
);
37922 if (!parens
.require_close (parser
))
37925 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDER
, "order", location
);
37926 c
= build_omp_clause (location
, OMP_CLAUSE_ORDER
);
37927 OMP_CLAUSE_ORDER_UNCONSTRAINED (c
) = unconstrained
;
37928 OMP_CLAUSE_ORDER_REPRODUCIBLE (c
) = reproducible
;
37929 OMP_CLAUSE_CHAIN (c
) = list
;
37933 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37934 /*or_comma=*/false,
37935 /*consume_paren=*/true);
37940 bind ( teams | parallel | thread ) */
37943 cp_parser_omp_clause_bind (cp_parser
*parser
, tree list
,
37944 location_t location
)
37948 enum omp_clause_bind_kind kind
= OMP_CLAUSE_BIND_THREAD
;
37950 matching_parens parens
;
37951 if (!parens
.require_open (parser
))
37954 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37957 cp_parser_error (parser
,
37958 "expected %<teams%>, %<parallel%> or %<thread%>");
37963 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37964 p
= IDENTIFIER_POINTER (id
);
37966 if (strcmp (p
, "teams") == 0)
37967 kind
= OMP_CLAUSE_BIND_TEAMS
;
37968 else if (strcmp (p
, "parallel") == 0)
37969 kind
= OMP_CLAUSE_BIND_PARALLEL
;
37970 else if (strcmp (p
, "thread") != 0)
37972 cp_lexer_consume_token (parser
->lexer
);
37973 if (!parens
.require_close (parser
))
37976 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
37977 c
= build_omp_clause (location
, OMP_CLAUSE_BIND
);
37978 OMP_CLAUSE_BIND_KIND (c
) = kind
;
37979 OMP_CLAUSE_CHAIN (c
) = list
;
37983 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37984 /*or_comma=*/false,
37985 /*consume_paren=*/true);
37993 ordered ( constant-expression ) */
37996 cp_parser_omp_clause_ordered (cp_parser
*parser
,
37997 tree list
, location_t location
)
37999 tree c
, num
= NULL_TREE
;
38002 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
38003 "ordered", location
);
38005 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38007 matching_parens parens
;
38008 parens
.consume_open (parser
);
38010 num
= cp_parser_constant_expression (parser
);
38012 if (!parens
.require_close (parser
))
38013 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38014 /*or_comma=*/false,
38015 /*consume_paren=*/true);
38017 if (num
== error_mark_node
)
38019 num
= fold_non_dependent_expr (num
);
38020 if (!tree_fits_shwi_p (num
)
38021 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
38022 || (n
= tree_to_shwi (num
)) <= 0
38025 error_at (location
,
38026 "ordered argument needs positive constant integer "
38032 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
38033 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
38034 OMP_CLAUSE_CHAIN (c
) = list
;
38039 reduction ( reduction-operator : variable-list )
38041 reduction-operator:
38042 One of: + * - & ^ | && ||
38046 reduction-operator:
38047 One of: + * - & ^ | && || min max
38051 reduction-operator:
38052 One of: + * - & ^ | && ||
38056 reduction ( reduction-modifier, reduction-operator : variable-list )
38057 in_reduction ( reduction-operator : variable-list )
38058 task_reduction ( reduction-operator : variable-list ) */
38061 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
38062 bool is_omp
, tree list
)
38064 enum tree_code code
= ERROR_MARK
;
38065 tree nlist
, c
, id
= NULL_TREE
;
38067 bool inscan
= false;
38069 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38072 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
38074 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
38075 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
38077 cp_lexer_consume_token (parser
->lexer
);
38078 cp_lexer_consume_token (parser
->lexer
);
38080 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
38081 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
38083 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38084 const char *p
= IDENTIFIER_POINTER (id
);
38085 if (strcmp (p
, "task") == 0)
38087 else if (strcmp (p
, "inscan") == 0)
38089 if (task
|| inscan
)
38091 cp_lexer_consume_token (parser
->lexer
);
38092 cp_lexer_consume_token (parser
->lexer
);
38097 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
38099 case CPP_PLUS
: code
= PLUS_EXPR
; break;
38100 case CPP_MULT
: code
= MULT_EXPR
; break;
38101 case CPP_MINUS
: code
= MINUS_EXPR
; break;
38102 case CPP_AND
: code
= BIT_AND_EXPR
; break;
38103 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
38104 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
38105 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
38106 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
38110 if (code
!= ERROR_MARK
)
38111 cp_lexer_consume_token (parser
->lexer
);
38114 bool saved_colon_corrects_to_scope_p
;
38115 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38116 parser
->colon_corrects_to_scope_p
= false;
38117 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
38118 /*check_dependency_p=*/true,
38119 /*template_p=*/NULL
,
38120 /*declarator_p=*/false,
38121 /*optional_p=*/false);
38122 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
38123 if (identifier_p (id
))
38125 const char *p
= IDENTIFIER_POINTER (id
);
38127 if (strcmp (p
, "min") == 0)
38129 else if (strcmp (p
, "max") == 0)
38131 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
38133 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
38135 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
38137 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
38138 code
= BIT_AND_EXPR
;
38139 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
38140 code
= BIT_IOR_EXPR
;
38141 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
38142 code
= BIT_XOR_EXPR
;
38143 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
38144 code
= TRUTH_ANDIF_EXPR
;
38145 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
38146 code
= TRUTH_ORIF_EXPR
;
38147 id
= omp_reduction_id (code
, id
, NULL_TREE
);
38148 tree scope
= parser
->scope
;
38150 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
38151 parser
->scope
= NULL_TREE
;
38152 parser
->qualifying_scope
= NULL_TREE
;
38153 parser
->object_scope
= NULL_TREE
;
38157 error ("invalid reduction-identifier");
38159 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38160 /*or_comma=*/false,
38161 /*consume_paren=*/true);
38166 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
38169 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
38171 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
38173 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
38175 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
38177 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
38178 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
38185 schedule ( schedule-kind )
38186 schedule ( schedule-kind , expression )
38189 static | dynamic | guided | runtime | auto
38192 schedule ( schedule-modifier : schedule-kind )
38193 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
38201 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
38204 int modifiers
= 0, nmodifiers
= 0;
38206 matching_parens parens
;
38207 if (!parens
.require_open (parser
))
38210 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
38212 location_t comma
= UNKNOWN_LOCATION
;
38213 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38215 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38216 const char *p
= IDENTIFIER_POINTER (id
);
38217 if (strcmp ("simd", p
) == 0)
38218 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
38219 else if (strcmp ("monotonic", p
) == 0)
38220 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
38221 else if (strcmp ("nonmonotonic", p
) == 0)
38222 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
38225 comma
= UNKNOWN_LOCATION
;
38226 cp_lexer_consume_token (parser
->lexer
);
38227 if (nmodifiers
++ == 0
38228 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38230 comma
= cp_lexer_peek_token (parser
->lexer
)->location
;
38231 cp_lexer_consume_token (parser
->lexer
);
38235 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
38239 if (comma
!= UNKNOWN_LOCATION
)
38240 error_at (comma
, "expected %<:%>");
38242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38244 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38245 const char *p
= IDENTIFIER_POINTER (id
);
38250 if (strcmp ("dynamic", p
) != 0)
38252 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
38256 if (strcmp ("guided", p
) != 0)
38258 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
38262 if (strcmp ("runtime", p
) != 0)
38264 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
38271 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
38272 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
38273 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
38274 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
38277 cp_lexer_consume_token (parser
->lexer
);
38279 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
38280 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
38281 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
38282 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
38284 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
38289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38292 cp_lexer_consume_token (parser
->lexer
);
38294 token
= cp_lexer_peek_token (parser
->lexer
);
38295 t
= cp_parser_assignment_expression (parser
);
38297 if (t
== error_mark_node
)
38299 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
38300 error_at (token
->location
, "schedule %<runtime%> does not take "
38301 "a %<chunk_size%> parameter");
38302 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
38303 error_at (token
->location
, "schedule %<auto%> does not take "
38304 "a %<chunk_size%> parameter");
38306 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
38308 if (!parens
.require_close (parser
))
38311 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
38314 OMP_CLAUSE_SCHEDULE_KIND (c
)
38315 = (enum omp_clause_schedule_kind
)
38316 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
38318 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
38319 OMP_CLAUSE_CHAIN (c
) = list
;
38323 cp_parser_error (parser
, "invalid schedule kind");
38325 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38326 /*or_comma=*/false,
38327 /*consume_paren=*/true);
38335 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
38336 tree list
, location_t location
)
38340 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
38342 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
38343 OMP_CLAUSE_CHAIN (c
) = list
;
38352 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
38353 tree list
, location_t location
)
38355 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
38356 tree c
= build_omp_clause (location
, code
);
38357 OMP_CLAUSE_CHAIN (c
) = list
;
38368 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
38369 enum omp_clause_code code
,
38370 tree list
, location_t location
)
38372 tree c
= build_omp_clause (location
, code
);
38373 OMP_CLAUSE_CHAIN (c
) = list
;
38381 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
38382 tree list
, location_t location
)
38384 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
38385 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
38386 OMP_CLAUSE_CHAIN (c
) = list
;
38395 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
38396 enum omp_clause_code code
,
38397 tree list
, location_t location
)
38399 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
38400 tree c
= build_omp_clause (location
, code
);
38401 OMP_CLAUSE_CHAIN (c
) = list
;
38406 num_teams ( expression )
38409 num_teams ( expression : expression ) */
38412 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
38413 location_t location
)
38415 tree upper
, lower
= NULL_TREE
, c
;
38417 matching_parens parens
;
38418 if (!parens
.require_open (parser
))
38421 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38422 parser
->colon_corrects_to_scope_p
= false;
38423 upper
= cp_parser_assignment_expression (parser
);
38424 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
38426 if (upper
!= error_mark_node
38427 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
38430 cp_lexer_consume_token (parser
->lexer
);
38431 upper
= cp_parser_assignment_expression (parser
);
38434 if (upper
== error_mark_node
38435 || !parens
.require_close (parser
))
38436 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38437 /*or_comma=*/false,
38438 /*consume_paren=*/true);
38440 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
38441 "num_teams", location
);
38443 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
38444 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c
) = upper
;
38445 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c
) = lower
;
38446 OMP_CLAUSE_CHAIN (c
) = list
;
38452 thread_limit ( expression ) */
38455 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
38456 location_t location
)
38460 matching_parens parens
;
38461 if (!parens
.require_open (parser
))
38464 t
= cp_parser_assignment_expression (parser
);
38466 if (t
== error_mark_node
38467 || !parens
.require_close (parser
))
38468 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38469 /*or_comma=*/false,
38470 /*consume_paren=*/true);
38472 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
38473 "thread_limit", location
);
38475 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
38476 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
38477 OMP_CLAUSE_CHAIN (c
) = list
;
38483 aligned ( variable-list )
38484 aligned ( variable-list : constant-expression ) */
38487 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
38489 tree nlist
, c
, alignment
= NULL_TREE
;
38492 matching_parens parens
;
38493 if (!parens
.require_open (parser
))
38496 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
38501 alignment
= cp_parser_constant_expression (parser
);
38503 if (!parens
.require_close (parser
))
38504 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38505 /*or_comma=*/false,
38506 /*consume_paren=*/true);
38508 if (alignment
== error_mark_node
)
38509 alignment
= NULL_TREE
;
38512 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
38513 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
38519 allocate ( variable-list )
38520 allocate ( expression : variable-list )
38523 allocate ( allocator-modifier : variable-list )
38524 allocate ( allocator-modifier , allocator-modifier : variable-list )
38526 allocator-modifier:
38527 allocator ( expression )
38528 align ( expression ) */
38531 cp_parser_omp_clause_allocate (cp_parser
*parser
, tree list
)
38533 tree nlist
, c
, allocator
= NULL_TREE
, align
= NULL_TREE
;
38534 bool colon
, has_modifiers
= false;
38536 matching_parens parens
;
38537 if (!parens
.require_open (parser
))
38540 cp_parser_parse_tentatively (parser
);
38541 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38542 parser
->colon_corrects_to_scope_p
= false;
38543 for (int mod
= 0; mod
< 2; mod
++)
38544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
38545 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
38547 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38548 const char *p
= IDENTIFIER_POINTER (id
);
38549 if (strcmp (p
, "allocator") != 0 && strcmp (p
, "align") != 0)
38551 cp_lexer_consume_token (parser
->lexer
);
38552 matching_parens parens2
;
38553 if (!parens2
.require_open (parser
))
38555 if (strcmp (p
, "allocator") == 0)
38557 if (allocator
!= NULL_TREE
)
38559 allocator
= cp_parser_assignment_expression (parser
);
38563 if (align
!= NULL_TREE
)
38565 align
= cp_parser_assignment_expression (parser
);
38567 if (!parens2
.require_close (parser
))
38569 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
38571 has_modifiers
= true;
38574 if (mod
!= 0 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
38576 cp_lexer_consume_token (parser
->lexer
);
38580 if (!has_modifiers
)
38582 cp_parser_abort_tentative_parse (parser
);
38584 allocator
= NULL_TREE
;
38585 cp_parser_parse_tentatively (parser
);
38586 allocator
= cp_parser_assignment_expression (parser
);
38588 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
38589 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
38591 cp_parser_parse_definitely (parser
);
38592 cp_lexer_consume_token (parser
->lexer
);
38593 if (allocator
== error_mark_node
)
38594 allocator
= NULL_TREE
;
38595 if (align
== error_mark_node
)
38600 cp_parser_abort_tentative_parse (parser
);
38601 allocator
= NULL_TREE
;
38605 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALLOCATE
, list
,
38608 if (allocator
|| align
)
38609 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
38611 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c
) = allocator
;
38612 OMP_CLAUSE_ALLOCATE_ALIGN (c
) = align
;
38619 lastprivate ( variable-list )
38622 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
38625 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
38627 bool conditional
= false;
38629 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38632 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
38633 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
38635 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38636 const char *p
= IDENTIFIER_POINTER (id
);
38638 if (strcmp ("conditional", p
) == 0)
38640 conditional
= true;
38641 cp_lexer_consume_token (parser
->lexer
);
38642 cp_lexer_consume_token (parser
->lexer
);
38646 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
38650 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
38651 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
38656 linear ( variable-list )
38657 linear ( variable-list : expression )
38660 linear ( modifier ( variable-list ) )
38661 linear ( modifier ( variable-list ) : expression ) */
38664 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
38667 tree nlist
, c
, step
= integer_one_node
;
38669 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
38671 matching_parens parens
;
38672 if (!parens
.require_open (parser
))
38675 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38677 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38678 const char *p
= IDENTIFIER_POINTER (id
);
38680 if (strcmp ("ref", p
) == 0)
38681 kind
= OMP_CLAUSE_LINEAR_REF
;
38682 else if (strcmp ("val", p
) == 0)
38683 kind
= OMP_CLAUSE_LINEAR_VAL
;
38684 else if (strcmp ("uval", p
) == 0)
38685 kind
= OMP_CLAUSE_LINEAR_UVAL
;
38686 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
38687 cp_lexer_consume_token (parser
->lexer
);
38689 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
38692 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
38693 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
38697 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
38698 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
38700 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
38701 else if (!parens
.require_close (parser
))
38702 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38703 /*or_comma=*/false,
38704 /*consume_paren=*/true);
38711 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
38712 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
38714 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38715 cp_parser_parse_tentatively (parser
);
38716 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
38717 /*check_dependency_p=*/true,
38718 /*template_p=*/NULL
,
38719 /*declarator_p=*/false,
38720 /*optional_p=*/false);
38721 if (step
!= error_mark_node
)
38722 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
38723 if (step
== error_mark_node
)
38726 cp_parser_abort_tentative_parse (parser
);
38728 else if (!cp_parser_parse_definitely (parser
))
38732 step
= cp_parser_assignment_expression (parser
);
38734 if (!parens
.require_close (parser
))
38735 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38736 /*or_comma=*/false,
38737 /*consume_paren=*/true);
38739 if (step
== error_mark_node
)
38743 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
38745 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
38746 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
38753 safelen ( constant-expression ) */
38756 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
38757 location_t location
)
38761 matching_parens parens
;
38762 if (!parens
.require_open (parser
))
38765 t
= cp_parser_constant_expression (parser
);
38767 if (t
== error_mark_node
38768 || !parens
.require_close (parser
))
38769 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38770 /*or_comma=*/false,
38771 /*consume_paren=*/true);
38773 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
38775 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
38776 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
38777 OMP_CLAUSE_CHAIN (c
) = list
;
38783 simdlen ( constant-expression ) */
38786 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
38787 location_t location
)
38791 matching_parens parens
;
38792 if (!parens
.require_open (parser
))
38795 t
= cp_parser_constant_expression (parser
);
38797 if (t
== error_mark_node
38798 || !parens
.require_close (parser
))
38799 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38800 /*or_comma=*/false,
38801 /*consume_paren=*/true);
38803 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
38805 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
38806 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
38807 OMP_CLAUSE_CHAIN (c
) = list
;
38814 identifier [+/- integer]
38815 vec , identifier [+/- integer]
38819 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
38824 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
38826 cp_parser_error (parser
, "expected identifier");
38830 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38832 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38833 tree t
, identifier
= cp_parser_identifier (parser
);
38834 tree addend
= NULL
;
38836 if (identifier
== error_mark_node
)
38837 t
= error_mark_node
;
38840 t
= cp_parser_lookup_name_simple
38841 (parser
, identifier
,
38842 cp_lexer_peek_token (parser
->lexer
)->location
);
38843 if (t
== error_mark_node
)
38844 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
38849 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
38851 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
38853 addend
= integer_zero_node
;
38854 goto add_to_vector
;
38856 cp_lexer_consume_token (parser
->lexer
);
38858 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
38860 cp_parser_error (parser
, "expected integer");
38864 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38865 if (TREE_CODE (addend
) != INTEGER_CST
)
38867 cp_parser_error (parser
, "expected integer");
38870 cp_lexer_consume_token (parser
->lexer
);
38873 if (t
!= error_mark_node
)
38875 vec
= tree_cons (addend
, t
, vec
);
38877 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
38880 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
38881 || !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
38884 cp_lexer_consume_token (parser
->lexer
);
38889 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
38890 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
38891 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
38892 OMP_CLAUSE_CHAIN (u
) = list
;
38899 detach ( event-handle ) */
38902 cp_parser_omp_clause_detach (cp_parser
*parser
, tree list
)
38904 matching_parens parens
;
38906 if (!parens
.require_open (parser
))
38912 token
= cp_lexer_peek_token (parser
->lexer
);
38913 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
38914 /*check_dependency_p=*/true,
38915 /*template_p=*/NULL
,
38916 /*declarator_p=*/false,
38917 /*optional_p=*/false);
38918 if (name
== error_mark_node
)
38919 decl
= error_mark_node
;
38922 if (identifier_p (name
))
38923 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
38926 if (decl
== error_mark_node
)
38927 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
38931 if (decl
== error_mark_node
38932 || !parens
.require_close (parser
))
38933 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38934 /*or_comma=*/false,
38935 /*consume_paren=*/true);
38937 tree u
= build_omp_clause (token
->location
, OMP_CLAUSE_DETACH
);
38938 OMP_CLAUSE_DECL (u
) = decl
;
38939 OMP_CLAUSE_CHAIN (u
) = list
;
38945 iterators ( iterators-definition )
38947 iterators-definition:
38949 iterator-specifier , iterators-definition
38951 iterator-specifier:
38952 identifier = range-specification
38953 iterator-type identifier = range-specification
38955 range-specification:
38957 begin : end : step */
38960 cp_parser_omp_iterators (cp_parser
*parser
)
38962 tree ret
= NULL_TREE
, *last
= &ret
;
38963 cp_lexer_consume_token (parser
->lexer
);
38965 matching_parens parens
;
38966 if (!parens
.require_open (parser
))
38967 return error_mark_node
;
38969 bool saved_colon_corrects_to_scope_p
38970 = parser
->colon_corrects_to_scope_p
;
38971 bool saved_colon_doesnt_start_class_def_p
38972 = parser
->colon_doesnt_start_class_def_p
;
38977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
38978 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
38979 iter_type
= integer_type_node
;
38982 const char *saved_message
38983 = parser
->type_definition_forbidden_message
;
38984 parser
->type_definition_forbidden_message
38985 = G_("types may not be defined in iterator type");
38987 iter_type
= cp_parser_type_id (parser
);
38989 parser
->type_definition_forbidden_message
= saved_message
;
38992 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38993 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
38995 cp_parser_error (parser
, "expected identifier");
38999 tree id
= cp_parser_identifier (parser
);
39000 if (id
== error_mark_node
)
39003 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
39006 parser
->colon_corrects_to_scope_p
= false;
39007 parser
->colon_doesnt_start_class_def_p
= true;
39008 tree begin
= cp_parser_assignment_expression (parser
);
39010 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39013 tree end
= cp_parser_assignment_expression (parser
);
39015 tree step
= integer_one_node
;
39016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
39018 cp_lexer_consume_token (parser
->lexer
);
39019 step
= cp_parser_assignment_expression (parser
);
39022 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
39023 DECL_ARTIFICIAL (iter_var
) = 1;
39024 DECL_CONTEXT (iter_var
) = current_function_decl
;
39025 pushdecl (iter_var
);
39027 *last
= make_tree_vec (6);
39028 TREE_VEC_ELT (*last
, 0) = iter_var
;
39029 TREE_VEC_ELT (*last
, 1) = begin
;
39030 TREE_VEC_ELT (*last
, 2) = end
;
39031 TREE_VEC_ELT (*last
, 3) = step
;
39032 last
= &TREE_CHAIN (*last
);
39034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39036 cp_lexer_consume_token (parser
->lexer
);
39043 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39044 parser
->colon_doesnt_start_class_def_p
39045 = saved_colon_doesnt_start_class_def_p
;
39047 if (!parens
.require_close (parser
))
39048 cp_parser_skip_to_closing_parenthesis (parser
,
39049 /*recovering=*/true,
39050 /*or_comma=*/false,
39051 /*consume_paren=*/true);
39053 return ret
? ret
: error_mark_node
;
39057 affinity ( [aff-modifier :] variable-list )
39059 iterator ( iterators-definition ) */
39062 cp_parser_omp_clause_affinity (cp_parser
*parser
, tree list
)
39064 tree nlist
, c
, iterators
= NULL_TREE
;
39066 matching_parens parens
;
39067 if (!parens
.require_open (parser
))
39070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39072 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39073 const char *p
= IDENTIFIER_POINTER (id
);
39074 bool parse_iter
= ((strcmp ("iterator", p
) == 0)
39075 && (cp_lexer_nth_token_is (parser
->lexer
, 2,
39079 size_t n
= cp_parser_skip_balanced_tokens (parser
, 2);
39080 parse_iter
= cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
);
39084 begin_scope (sk_omp
, NULL
);
39085 iterators
= cp_parser_omp_iterators (parser
);
39086 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39089 poplevel (0, 1, 0);
39090 cp_parser_skip_to_closing_parenthesis (parser
,
39091 /*recovering=*/true,
39092 /*or_comma=*/false,
39093 /*consume_paren=*/true);
39098 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_AFFINITY
,
39102 tree block
= poplevel (1, 1, 0);
39103 if (iterators
!= error_mark_node
)
39105 TREE_VEC_ELT (iterators
, 5) = block
;
39106 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
39107 OMP_CLAUSE_DECL (c
) = build_tree_list (iterators
,
39108 OMP_CLAUSE_DECL (c
));
39115 depend ( depend-kind : variable-list )
39123 depend ( sink : vec )
39126 depend ( depend-modifier , depend-kind: variable-list )
39129 in | out | inout | mutexinoutset | depobj
39132 iterator ( iterators-definition ) */
39135 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
39137 tree nlist
, c
, iterators
= NULL_TREE
;
39138 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
39140 matching_parens parens
;
39141 if (!parens
.require_open (parser
))
39146 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
39149 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39150 const char *p
= IDENTIFIER_POINTER (id
);
39152 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
39154 begin_scope (sk_omp
, NULL
);
39155 iterators
= cp_parser_omp_iterators (parser
);
39156 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
39159 if (strcmp ("in", p
) == 0)
39160 kind
= OMP_CLAUSE_DEPEND_IN
;
39161 else if (strcmp ("inout", p
) == 0)
39162 kind
= OMP_CLAUSE_DEPEND_INOUT
;
39163 else if (strcmp ("mutexinoutset", p
) == 0)
39164 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
39165 else if (strcmp ("out", p
) == 0)
39166 kind
= OMP_CLAUSE_DEPEND_OUT
;
39167 else if (strcmp ("depobj", p
) == 0)
39168 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
39169 else if (strcmp ("sink", p
) == 0)
39170 kind
= OMP_CLAUSE_DEPEND_SINK
;
39171 else if (strcmp ("source", p
) == 0)
39172 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
39179 cp_lexer_consume_token (parser
->lexer
);
39182 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
39184 poplevel (0, 1, 0);
39185 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
39186 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
39187 iterators
= NULL_TREE
;
39190 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
39192 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
39193 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
39194 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
39195 OMP_CLAUSE_CHAIN (c
) = list
;
39196 if (!parens
.require_close (parser
))
39197 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39198 /*or_comma=*/false,
39199 /*consume_paren=*/true);
39203 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39206 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
39208 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
39209 if (!parens
.require_close (parser
))
39210 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39211 /*or_comma=*/false,
39212 /*consume_paren=*/true);
39216 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
39221 tree block
= poplevel (1, 1, 0);
39222 if (iterators
== error_mark_node
)
39223 iterators
= NULL_TREE
;
39225 TREE_VEC_ELT (iterators
, 5) = block
;
39228 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
39230 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
39232 OMP_CLAUSE_DECL (c
)
39233 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
39239 cp_parser_error (parser
, "invalid depend kind");
39242 poplevel (0, 1, 0);
39243 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39244 /*or_comma=*/false,
39245 /*consume_paren=*/true);
39250 map ( map-kind : variable-list )
39251 map ( variable-list )
39254 alloc | to | from | tofrom
39258 alloc | to | from | tofrom | release | delete
39260 map ( always [,] map-kind: variable-list )
39263 map ( [map-type-modifier[,] ...] map-kind: variable-list )
39269 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
39272 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
39274 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39278 int map_kind_pos
= 0;
39279 while (cp_lexer_peek_nth_token (parser
->lexer
, pos
)->type
== CPP_NAME
39280 || cp_lexer_peek_nth_token (parser
->lexer
, pos
)->keyword
== RID_DELETE
)
39282 if (cp_lexer_peek_nth_token (parser
->lexer
, pos
+ 1)->type
== CPP_COLON
)
39284 map_kind_pos
= pos
;
39288 if (cp_lexer_peek_nth_token (parser
->lexer
, pos
+ 1)->type
== CPP_COMMA
)
39293 bool always_modifier
= false;
39294 bool close_modifier
= false;
39295 for (int pos
= 1; pos
< map_kind_pos
; ++pos
)
39297 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
39298 if (tok
->type
== CPP_COMMA
)
39300 cp_lexer_consume_token (parser
->lexer
);
39304 const char *p
= IDENTIFIER_POINTER (tok
->u
.value
);
39305 if (strcmp ("always", p
) == 0)
39307 if (always_modifier
)
39309 cp_parser_error (parser
, "too many %<always%> modifiers");
39310 cp_parser_skip_to_closing_parenthesis (parser
,
39311 /*recovering=*/true,
39312 /*or_comma=*/false,
39313 /*consume_paren=*/true);
39316 always_modifier
= true;
39318 else if (strcmp ("close", p
) == 0)
39320 if (close_modifier
)
39322 cp_parser_error (parser
, "too many %<close%> modifiers");
39323 cp_parser_skip_to_closing_parenthesis (parser
,
39324 /*recovering=*/true,
39325 /*or_comma=*/false,
39326 /*consume_paren=*/true);
39329 close_modifier
= true;
39333 cp_parser_error (parser
, "%<#pragma omp target%> with "
39334 "modifier other than %<always%> or %<close%>"
39335 "on %<map%> clause");
39336 cp_parser_skip_to_closing_parenthesis (parser
,
39337 /*recovering=*/true,
39338 /*or_comma=*/false,
39339 /*consume_paren=*/true);
39343 cp_lexer_consume_token (parser
->lexer
);
39346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
39347 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
39349 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39350 const char *p
= IDENTIFIER_POINTER (id
);
39352 if (strcmp ("alloc", p
) == 0)
39353 kind
= GOMP_MAP_ALLOC
;
39354 else if (strcmp ("to", p
) == 0)
39355 kind
= always_modifier
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
39356 else if (strcmp ("from", p
) == 0)
39357 kind
= always_modifier
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
39358 else if (strcmp ("tofrom", p
) == 0)
39359 kind
= always_modifier
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
39360 else if (strcmp ("release", p
) == 0)
39361 kind
= GOMP_MAP_RELEASE
;
39364 cp_parser_error (parser
, "invalid map kind");
39365 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39366 /*or_comma=*/false,
39367 /*consume_paren=*/true);
39370 cp_lexer_consume_token (parser
->lexer
);
39371 cp_lexer_consume_token (parser
->lexer
);
39373 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
39374 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
39376 kind
= GOMP_MAP_DELETE
;
39377 cp_lexer_consume_token (parser
->lexer
);
39378 cp_lexer_consume_token (parser
->lexer
);
39381 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
39384 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
39385 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
39391 device ( expression )
39394 device ( [device-modifier :] integer-expression )
39397 ancestor | device_num */
39400 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
39401 location_t location
)
39404 bool ancestor
= false;
39406 matching_parens parens
;
39407 if (!parens
.require_open (parser
))
39410 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
39411 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
39413 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
39414 const char *p
= IDENTIFIER_POINTER (tok
->u
.value
);
39415 if (strcmp ("ancestor", p
) == 0)
39419 /* A requires directive with the reverse_offload clause must be
39421 if ((omp_requires_mask
& OMP_REQUIRES_REVERSE_OFFLOAD
) == 0)
39423 error_at (tok
->location
, "%<ancestor%> device modifier not "
39424 "preceded by %<requires%> directive "
39425 "with %<reverse_offload%> clause");
39426 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
39430 else if (strcmp ("device_num", p
) == 0)
39434 error_at (tok
->location
, "expected %<ancestor%> or %<device_num%>");
39435 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
39438 cp_lexer_consume_token (parser
->lexer
);
39439 cp_lexer_consume_token (parser
->lexer
);
39442 t
= cp_parser_assignment_expression (parser
);
39444 if (t
== error_mark_node
39445 || !parens
.require_close (parser
))
39446 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39447 /*or_comma=*/false,
39448 /*consume_paren=*/true);
39450 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
39451 "device", location
);
39453 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
39454 OMP_CLAUSE_DEVICE_ID (c
) = t
;
39455 OMP_CLAUSE_CHAIN (c
) = list
;
39456 OMP_CLAUSE_DEVICE_ANCESTOR (c
) = ancestor
;
39462 dist_schedule ( static )
39463 dist_schedule ( static , expression ) */
39466 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
39467 location_t location
)
39471 matching_parens parens
;
39472 if (!parens
.require_open (parser
))
39475 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
39477 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
39479 cp_lexer_consume_token (parser
->lexer
);
39481 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39483 cp_lexer_consume_token (parser
->lexer
);
39485 t
= cp_parser_assignment_expression (parser
);
39487 if (t
== error_mark_node
)
39489 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
39491 if (!parens
.require_close (parser
))
39494 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
39497 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
39498 "dist_schedule", location); */
39499 if (omp_find_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
))
39500 warning_at (location
, 0, "too many %qs clauses", "dist_schedule");
39501 OMP_CLAUSE_CHAIN (c
) = list
;
39505 cp_parser_error (parser
, "invalid dist_schedule kind");
39507 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39508 /*or_comma=*/false,
39509 /*consume_paren=*/true);
39514 proc_bind ( proc-bind-kind )
39517 primary | master | close | spread
39518 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
39521 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
39522 location_t location
)
39525 enum omp_clause_proc_bind_kind kind
;
39527 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39530 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39532 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39533 const char *p
= IDENTIFIER_POINTER (id
);
39535 if (strcmp ("primary", p
) == 0)
39536 kind
= OMP_CLAUSE_PROC_BIND_PRIMARY
;
39537 else if (strcmp ("master", p
) == 0)
39538 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
39539 else if (strcmp ("close", p
) == 0)
39540 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
39541 else if (strcmp ("spread", p
) == 0)
39542 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
39549 cp_lexer_consume_token (parser
->lexer
);
39550 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
39553 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
39554 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
39556 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
39557 OMP_CLAUSE_CHAIN (c
) = list
;
39561 cp_parser_error (parser
, "invalid depend kind");
39563 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39564 /*or_comma=*/false,
39565 /*consume_paren=*/true);
39570 device_type ( host | nohost | any ) */
39573 cp_parser_omp_clause_device_type (cp_parser
*parser
, tree list
,
39574 location_t location
)
39577 enum omp_clause_device_type_kind kind
;
39579 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39584 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39585 const char *p
= IDENTIFIER_POINTER (id
);
39587 if (strcmp ("host", p
) == 0)
39588 kind
= OMP_CLAUSE_DEVICE_TYPE_HOST
;
39589 else if (strcmp ("nohost", p
) == 0)
39590 kind
= OMP_CLAUSE_DEVICE_TYPE_NOHOST
;
39591 else if (strcmp ("any", p
) == 0)
39592 kind
= OMP_CLAUSE_DEVICE_TYPE_ANY
;
39599 cp_lexer_consume_token (parser
->lexer
);
39600 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
39603 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE_TYPE
);
39604 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
39606 OMP_CLAUSE_DEVICE_TYPE_KIND (c
) = kind
;
39607 OMP_CLAUSE_CHAIN (c
) = list
;
39611 cp_parser_error (parser
, "invalid depend kind");
39613 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39614 /*or_comma=*/false,
39615 /*consume_paren=*/true);
39620 async [( int-expr )] */
39623 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
39626 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39628 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
39630 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
39632 matching_parens parens
;
39633 parens
.consume_open (parser
);
39635 t
= cp_parser_assignment_expression (parser
);
39636 if (t
== error_mark_node
39637 || !parens
.require_close (parser
))
39638 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39639 /*or_comma=*/false,
39640 /*consume_paren=*/true);
39643 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
39645 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
39646 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
39647 OMP_CLAUSE_CHAIN (c
) = list
;
39653 /* Parse all OpenACC clauses. The set clauses allowed by the directive
39654 is a bitmask in MASK. Return the list of clauses found. */
39657 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
39658 const char *where
, cp_token
*pragma_tok
,
39659 bool finish_p
= true)
39661 tree clauses
= NULL
;
39664 /* Don't create location wrapper nodes within OpenACC clauses. */
39665 auto_suppress_location_wrappers sentinel
;
39667 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39670 pragma_omp_clause c_kind
;
39671 omp_clause_code code
;
39672 const char *c_name
;
39673 tree prev
= clauses
;
39675 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39676 cp_lexer_consume_token (parser
->lexer
);
39678 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
39679 c_kind
= cp_parser_omp_clause_name (parser
);
39683 case PRAGMA_OACC_CLAUSE_ASYNC
:
39684 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
39687 case PRAGMA_OACC_CLAUSE_AUTO
:
39688 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_AUTO
,
39692 case PRAGMA_OACC_CLAUSE_ATTACH
:
39693 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39696 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
39697 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
39698 c_name
= "collapse";
39700 case PRAGMA_OACC_CLAUSE_COPY
:
39701 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39704 case PRAGMA_OACC_CLAUSE_COPYIN
:
39705 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39708 case PRAGMA_OACC_CLAUSE_COPYOUT
:
39709 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39710 c_name
= "copyout";
39712 case PRAGMA_OACC_CLAUSE_CREATE
:
39713 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39716 case PRAGMA_OACC_CLAUSE_DELETE
:
39717 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39720 case PRAGMA_OMP_CLAUSE_DEFAULT
:
39721 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
39722 c_name
= "default";
39724 case PRAGMA_OACC_CLAUSE_DETACH
:
39725 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39728 case PRAGMA_OACC_CLAUSE_DEVICE
:
39729 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39732 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
39733 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
39734 c_name
= "deviceptr";
39736 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
39737 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39738 c_name
= "device_resident";
39740 case PRAGMA_OACC_CLAUSE_FINALIZE
:
39741 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_FINALIZE
,
39743 c_name
= "finalize";
39745 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
39746 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
39748 c_name
= "firstprivate";
39750 case PRAGMA_OACC_CLAUSE_GANG
:
39752 clauses
= cp_parser_oacc_shape_clause (parser
, here
, OMP_CLAUSE_GANG
,
39755 case PRAGMA_OACC_CLAUSE_HOST
:
39756 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39759 case PRAGMA_OACC_CLAUSE_IF
:
39760 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
39763 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
39764 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_IF_PRESENT
,
39766 c_name
= "if_present";
39768 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
39769 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_INDEPENDENT
,
39771 c_name
= "independent";
39773 case PRAGMA_OACC_CLAUSE_LINK
:
39774 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39777 case PRAGMA_OACC_CLAUSE_NO_CREATE
:
39778 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39779 c_name
= "no_create";
39781 case PRAGMA_OACC_CLAUSE_NOHOST
:
39782 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_NOHOST
,
39786 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
39787 code
= OMP_CLAUSE_NUM_GANGS
;
39788 c_name
= "num_gangs";
39789 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
39792 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
39793 c_name
= "num_workers";
39794 code
= OMP_CLAUSE_NUM_WORKERS
;
39795 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
39798 case PRAGMA_OACC_CLAUSE_PRESENT
:
39799 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
39800 c_name
= "present";
39802 case PRAGMA_OACC_CLAUSE_PRIVATE
:
39803 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
39805 c_name
= "private";
39807 case PRAGMA_OACC_CLAUSE_REDUCTION
:
39809 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
39811 c_name
= "reduction";
39813 case PRAGMA_OACC_CLAUSE_SEQ
:
39814 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_SEQ
,
39818 case PRAGMA_OACC_CLAUSE_TILE
:
39819 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
39822 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
39823 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
39825 c_name
= "use_device";
39827 case PRAGMA_OACC_CLAUSE_VECTOR
:
39829 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
39833 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
39834 c_name
= "vector_length";
39835 code
= OMP_CLAUSE_VECTOR_LENGTH
;
39836 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
39839 case PRAGMA_OACC_CLAUSE_WAIT
:
39840 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
39843 case PRAGMA_OACC_CLAUSE_WORKER
:
39845 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
39850 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
39856 if (((mask
>> c_kind
) & 1) == 0)
39858 /* Remove the invalid clause(s) from the list to avoid
39859 confusing the rest of the compiler. */
39861 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
39866 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39869 return finish_omp_clauses (clauses
, C_ORT_ACC
);
39874 /* Parse all OpenMP clauses. The set clauses allowed by the directive
39875 is a bitmask in MASK. Return the list of clauses found.
39876 FINISH_P set if finish_omp_clauses should be called.
39877 NESTED non-zero if clauses should be terminated by closing paren instead
39878 of end of pragma. If it is 2, additionally commas are required in between
39882 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
39883 const char *where
, cp_token
*pragma_tok
,
39884 bool finish_p
= true, int nested
= 0)
39886 tree clauses
= NULL
;
39888 cp_token
*token
= NULL
;
39890 /* Don't create location wrapper nodes within OpenMP clauses. */
39891 auto_suppress_location_wrappers sentinel
;
39893 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39895 pragma_omp_clause c_kind
;
39896 const char *c_name
;
39897 tree prev
= clauses
;
39899 if (nested
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
39903 /* OpenMP 5.1 allows optional comma in between directive-name and
39904 clauses everywhere, but as we aren't done with OpenMP 5.0
39905 implementation yet, let's allow it for now only in C++11
39907 || (parser
->lexer
->in_omp_attribute_pragma
&& nested
!= 2))
39909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39910 cp_lexer_consume_token (parser
->lexer
);
39911 else if (nested
== 2)
39912 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39913 "clauses in %<simd%> trait should be separated "
39917 token
= cp_lexer_peek_token (parser
->lexer
);
39918 c_kind
= cp_parser_omp_clause_name (parser
);
39922 case PRAGMA_OMP_CLAUSE_BIND
:
39923 clauses
= cp_parser_omp_clause_bind (parser
, clauses
,
39927 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
39928 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
39930 c_name
= "collapse";
39932 case PRAGMA_OMP_CLAUSE_COPYIN
:
39933 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
39936 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
39937 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
39939 c_name
= "copyprivate";
39941 case PRAGMA_OMP_CLAUSE_DEFAULT
:
39942 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
39943 token
->location
, false);
39944 c_name
= "default";
39946 case PRAGMA_OMP_CLAUSE_FILTER
:
39947 clauses
= cp_parser_omp_clause_filter (parser
, clauses
,
39951 case PRAGMA_OMP_CLAUSE_FINAL
:
39952 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
39955 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
39956 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
39958 c_name
= "firstprivate";
39960 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
39961 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
39963 c_name
= "grainsize";
39965 case PRAGMA_OMP_CLAUSE_HINT
:
39966 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
39970 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
39971 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
39973 c_name
= "defaultmap";
39975 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
39976 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
39978 c_name
= "use_device_ptr";
39980 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
:
39981 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_ADDR
,
39983 c_name
= "use_device_addr";
39985 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
39986 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
39988 c_name
= "is_device_ptr";
39990 case PRAGMA_OMP_CLAUSE_IF
:
39991 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
39995 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
39997 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
39999 c_name
= "in_reduction";
40001 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
40002 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
40003 c_name
= "lastprivate";
40005 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
40006 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
40008 c_name
= "mergeable";
40010 case PRAGMA_OMP_CLAUSE_NOWAIT
:
40011 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
,
40015 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
40016 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
40018 c_name
= "num_tasks";
40020 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
40021 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
40023 c_name
= "num_threads";
40025 case PRAGMA_OMP_CLAUSE_ORDER
:
40026 clauses
= cp_parser_omp_clause_order (parser
, clauses
,
40030 case PRAGMA_OMP_CLAUSE_ORDERED
:
40031 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
40033 c_name
= "ordered";
40035 case PRAGMA_OMP_CLAUSE_PRIORITY
:
40036 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
40038 c_name
= "priority";
40040 case PRAGMA_OMP_CLAUSE_PRIVATE
:
40041 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
40043 c_name
= "private";
40045 case PRAGMA_OMP_CLAUSE_REDUCTION
:
40047 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
40049 c_name
= "reduction";
40051 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
40052 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
40054 c_name
= "schedule";
40056 case PRAGMA_OMP_CLAUSE_SHARED
:
40057 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
40061 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
40063 = cp_parser_omp_clause_reduction (parser
,
40064 OMP_CLAUSE_TASK_REDUCTION
,
40066 c_name
= "task_reduction";
40068 case PRAGMA_OMP_CLAUSE_UNTIED
:
40069 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
40073 case PRAGMA_OMP_CLAUSE_INBRANCH
:
40074 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
40075 clauses
, token
->location
);
40076 c_name
= "inbranch";
40078 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
40079 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
40081 c_name
= "nontemporal";
40083 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
40084 clauses
= cp_parser_omp_clause_branch (parser
,
40085 OMP_CLAUSE_NOTINBRANCH
,
40086 clauses
, token
->location
);
40087 c_name
= "notinbranch";
40089 case PRAGMA_OMP_CLAUSE_PARALLEL
:
40090 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
40091 clauses
, token
->location
);
40092 c_name
= "parallel";
40096 error_at (token
->location
, "%qs must be the first clause of %qs",
40101 case PRAGMA_OMP_CLAUSE_FOR
:
40102 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
40103 clauses
, token
->location
);
40106 goto clause_not_first
;
40108 case PRAGMA_OMP_CLAUSE_SECTIONS
:
40109 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
40110 clauses
, token
->location
);
40111 c_name
= "sections";
40113 goto clause_not_first
;
40115 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
40116 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
40117 clauses
, token
->location
);
40118 c_name
= "taskgroup";
40120 goto clause_not_first
;
40122 case PRAGMA_OMP_CLAUSE_LINK
:
40123 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
40126 case PRAGMA_OMP_CLAUSE_TO
:
40127 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
40128 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
40131 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
,
40135 case PRAGMA_OMP_CLAUSE_FROM
:
40136 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
,
40140 case PRAGMA_OMP_CLAUSE_UNIFORM
:
40141 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
40143 c_name
= "uniform";
40145 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
40146 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
40148 c_name
= "num_teams";
40150 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
40151 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
40153 c_name
= "thread_limit";
40155 case PRAGMA_OMP_CLAUSE_ALIGNED
:
40156 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
40157 c_name
= "aligned";
40159 case PRAGMA_OMP_CLAUSE_ALLOCATE
:
40160 clauses
= cp_parser_omp_clause_allocate (parser
, clauses
);
40161 c_name
= "allocate";
40163 case PRAGMA_OMP_CLAUSE_LINEAR
:
40165 bool declare_simd
= false;
40166 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
40167 declare_simd
= true;
40168 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
40172 case PRAGMA_OMP_CLAUSE_AFFINITY
:
40173 clauses
= cp_parser_omp_clause_affinity (parser
, clauses
);
40174 c_name
= "affinity";
40176 case PRAGMA_OMP_CLAUSE_DEPEND
:
40177 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
40181 case PRAGMA_OMP_CLAUSE_DETACH
:
40182 clauses
= cp_parser_omp_clause_detach (parser
, clauses
);
40185 case PRAGMA_OMP_CLAUSE_MAP
:
40186 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
40189 case PRAGMA_OMP_CLAUSE_DEVICE
:
40190 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
40194 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
40195 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
40197 c_name
= "dist_schedule";
40199 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
40200 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
40202 c_name
= "proc_bind";
40204 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE
:
40205 clauses
= cp_parser_omp_clause_device_type (parser
, clauses
,
40207 c_name
= "device_type";
40209 case PRAGMA_OMP_CLAUSE_SAFELEN
:
40210 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
40212 c_name
= "safelen";
40214 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
40215 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
40217 c_name
= "simdlen";
40219 case PRAGMA_OMP_CLAUSE_NOGROUP
:
40220 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
40222 c_name
= "nogroup";
40224 case PRAGMA_OMP_CLAUSE_THREADS
:
40226 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
40227 clauses
, token
->location
);
40228 c_name
= "threads";
40230 case PRAGMA_OMP_CLAUSE_SIMD
:
40232 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
40233 clauses
, token
->location
);
40237 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
40243 if (((mask
>> c_kind
) & 1) == 0)
40245 /* Remove the invalid clause(s) from the list to avoid
40246 confusing the rest of the compiler. */
40248 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
40253 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40256 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
40257 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
40259 return finish_omp_clauses (clauses
, C_ORT_OMP
);
40268 In practice, we're also interested in adding the statement to an
40269 outer node. So it is convenient if we work around the fact that
40270 cp_parser_statement calls add_stmt. */
40273 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
40275 unsigned save
= parser
->in_statement
;
40277 /* Only move the values to IN_OMP_BLOCK if they weren't false.
40278 This preserves the "not within loop or switch" style error messages
40279 for nonsense cases like
40285 if (parser
->in_statement
)
40286 parser
->in_statement
= IN_OMP_BLOCK
;
40292 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
40294 parser
->in_statement
= save
;
40298 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
40300 tree stmt
= begin_omp_structured_block ();
40301 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
40303 parser
->omp_attrs_forbidden_p
= true;
40304 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
40306 cp_parser_end_omp_structured_block (parser
, save
);
40307 return finish_omp_structured_block (stmt
);
40311 # pragma omp allocate (list) [allocator(allocator)] */
40314 cp_parser_omp_allocate (cp_parser
*parser
, cp_token
*pragma_tok
)
40316 tree allocator
= NULL_TREE
;
40317 location_t loc
= pragma_tok
->location
;
40318 tree nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ALLOCATE
, NULL_TREE
);
40320 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
40321 if (parser
->lexer
->in_omp_attribute_pragma
40322 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
40323 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
40324 cp_lexer_consume_token (parser
->lexer
);
40326 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40328 matching_parens parens
;
40329 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40330 const char *p
= IDENTIFIER_POINTER (id
);
40331 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40332 cp_lexer_consume_token (parser
->lexer
);
40333 if (strcmp (p
, "allocator") != 0)
40334 error_at (cloc
, "expected %<allocator%>");
40335 else if (parens
.require_open (parser
))
40337 allocator
= cp_parser_assignment_expression (parser
);
40338 if (allocator
== error_mark_node
)
40339 allocator
= NULL_TREE
;
40340 parens
.require_close (parser
);
40343 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40346 for (tree c
= nl
; c
!= NULL_TREE
; c
= OMP_CLAUSE_CHAIN (c
))
40347 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c
) = allocator
;
40349 sorry_at (loc
, "%<#pragma omp allocate%> not yet supported");
40353 # pragma omp atomic new-line
40357 x binop= expr | x++ | ++x | x-- | --x
40359 +, *, -, /, &, ^, |, <<, >>
40361 where x is an lvalue expression with scalar type.
40364 # pragma omp atomic new-line
40367 # pragma omp atomic read new-line
40370 # pragma omp atomic write new-line
40373 # pragma omp atomic update new-line
40376 # pragma omp atomic capture new-line
40379 # pragma omp atomic capture new-line
40387 expression-stmt | x = x binop expr
40389 v = expression-stmt
40391 { v = x; update-stmt; } | { update-stmt; v = x; }
40395 expression-stmt | x = x binop expr | x = expr binop x
40399 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
40402 # pragma omp atomic compare new-line
40403 conditional-update-atomic
40405 # pragma omp atomic compare capture new-line
40406 conditional-update-capture-atomic
40408 conditional-update-atomic:
40409 cond-expr-stmt | cond-update-stmt
40411 x = expr ordop x ? expr : x;
40412 x = x ordop expr ? expr : x;
40413 x = x == e ? d : x;
40415 if (expr ordop x) { x = expr; }
40416 if (x ordop expr) { x = expr; }
40417 if (x == e) { x = d; }
40420 conditional-update-capture-atomic:
40422 { v = x; cond-expr-stmt }
40423 { cond-expr-stmt v = x; }
40424 { v = x; cond-update-stmt }
40425 { cond-update-stmt v = x; }
40426 if (x == e) { x = d; } else { v = x; }
40427 { r = x == e; if (r) { x = d; } }
40428 { r = x == e; if (r) { x = d; } else { v = x; } }
40430 where x, r and v are lvalue expressions with scalar type,
40431 expr, e and d are expressions with scalar type and e might be
40435 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
, bool openacc
)
40437 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
40438 tree rhs1
= NULL_TREE
, orig_lhs
, r
= NULL_TREE
;
40439 location_t loc
= pragma_tok
->location
;
40440 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
40441 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
40442 bool structured_block
= false;
40444 tree clauses
= NULL_TREE
;
40445 bool capture
= false;
40446 bool compare
= false;
40448 enum omp_memory_order fail
= OMP_MEMORY_ORDER_UNSPECIFIED
;
40449 bool no_semicolon
= false;
40450 bool extra_scope
= false;
40452 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
40454 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
40455 if ((!first
|| parser
->lexer
->in_omp_attribute_pragma
)
40456 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
40457 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
40458 cp_lexer_consume_token (parser
->lexer
);
40462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40464 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40465 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40466 const char *p
= IDENTIFIER_POINTER (id
);
40467 enum tree_code new_code
= ERROR_MARK
;
40468 enum omp_memory_order new_memory_order
40469 = OMP_MEMORY_ORDER_UNSPECIFIED
;
40470 bool new_capture
= false;
40471 bool new_compare
= false;
40472 bool new_weak
= false;
40473 enum omp_memory_order new_fail
= OMP_MEMORY_ORDER_UNSPECIFIED
;
40475 if (!strcmp (p
, "read"))
40476 new_code
= OMP_ATOMIC_READ
;
40477 else if (!strcmp (p
, "write"))
40478 new_code
= NOP_EXPR
;
40479 else if (!strcmp (p
, "update"))
40480 new_code
= OMP_ATOMIC
;
40481 else if (openacc
&& !strcmp (p
, "capture"))
40482 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
40486 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
40487 "or %<capture%> clause");
40489 else if (!strcmp (p
, "capture"))
40490 new_capture
= true;
40491 else if (!strcmp (p
, "compare"))
40492 new_compare
= true;
40493 else if (!strcmp (p
, "weak"))
40495 else if (!strcmp (p
, "fail"))
40497 matching_parens parens
;
40499 cp_lexer_consume_token (parser
->lexer
);
40500 if (!parens
.require_open (parser
))
40503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40505 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40506 const char *q
= IDENTIFIER_POINTER (id
);
40508 if (!strcmp (q
, "seq_cst"))
40509 new_fail
= OMP_MEMORY_ORDER_SEQ_CST
;
40510 else if (!strcmp (q
, "acquire"))
40511 new_fail
= OMP_MEMORY_ORDER_ACQUIRE
;
40512 else if (!strcmp (q
, "relaxed"))
40513 new_fail
= OMP_MEMORY_ORDER_RELAXED
;
40516 if (new_fail
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
40518 cp_lexer_consume_token (parser
->lexer
);
40519 if (fail
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
40520 error_at (cloc
, "too many %qs clauses", "fail");
40525 cp_parser_error (parser
, "expected %<seq_cst%>, %<acquire%> "
40527 if (new_fail
== OMP_MEMORY_ORDER_UNSPECIFIED
40528 || !parens
.require_close (parser
))
40529 cp_parser_skip_to_closing_parenthesis (parser
,
40530 /*recovering=*/true,
40531 /*or_comma=*/false,
40532 /*consume_paren=*/true);
40535 else if (!strcmp (p
, "seq_cst"))
40536 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
40537 else if (!strcmp (p
, "acq_rel"))
40538 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
40539 else if (!strcmp (p
, "release"))
40540 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
40541 else if (!strcmp (p
, "acquire"))
40542 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
40543 else if (!strcmp (p
, "relaxed"))
40544 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
40545 else if (!strcmp (p
, "hint"))
40547 cp_lexer_consume_token (parser
->lexer
);
40548 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
40554 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
40555 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
40556 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
40557 "%<relaxed%> or %<hint%> clause");
40561 if (new_code
!= ERROR_MARK
)
40563 /* OpenACC permits 'update capture'. */
40565 && code
== OMP_ATOMIC
40566 && new_code
== OMP_ATOMIC_CAPTURE_NEW
)
40568 else if (code
!= ERROR_MARK
)
40569 error_at (cloc
, "too many atomic clauses");
40573 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
40575 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
40576 error_at (cloc
, "too many memory order clauses");
40578 memory_order
= new_memory_order
;
40580 else if (new_capture
)
40583 error_at (cloc
, "too many %qs clauses", "capture");
40587 else if (new_compare
)
40590 error_at (cloc
, "too many %qs clauses", "compare");
40597 error_at (cloc
, "too many %qs clauses", "weak");
40601 cp_lexer_consume_token (parser
->lexer
);
40607 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40609 if (code
== ERROR_MARK
)
40613 if (code
!= OMP_ATOMIC
)
40614 error_at (loc
, "%qs clause is incompatible with %<read%> or %<write%> "
40615 "clauses", "capture");
40617 code
= OMP_ATOMIC_CAPTURE_NEW
;
40619 if (compare
&& code
!= OMP_ATOMIC
&& code
!= OMP_ATOMIC_CAPTURE_NEW
)
40621 error_at (loc
, "%qs clause is incompatible with %<read%> or %<write%> "
40622 "clauses", "compare");
40625 if (fail
!= OMP_MEMORY_ORDER_UNSPECIFIED
&& !compare
)
40627 error_at (loc
, "%qs clause requires %qs clause", "fail", "compare");
40628 fail
= OMP_MEMORY_ORDER_UNSPECIFIED
;
40630 if (weak
&& !compare
)
40632 error_at (loc
, "%qs clause requires %qs clause", "weak", "compare");
40636 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
40637 else if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
40640 = (enum omp_requires
) (omp_requires_mask
40641 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
40642 switch ((enum omp_memory_order
)
40643 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
40645 case OMP_MEMORY_ORDER_UNSPECIFIED
:
40646 case OMP_MEMORY_ORDER_RELAXED
:
40647 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
40649 case OMP_MEMORY_ORDER_SEQ_CST
:
40650 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
40652 case OMP_MEMORY_ORDER_ACQ_REL
:
40655 case OMP_ATOMIC_READ
:
40656 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
40658 case NOP_EXPR
: /* atomic write */
40659 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
40662 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
40667 gcc_unreachable ();
40673 case OMP_ATOMIC_READ
:
40674 if (memory_order
== OMP_MEMORY_ORDER_RELEASE
)
40676 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
40677 "%<release%> clause");
40678 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
40680 else if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
)
40681 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
40683 case NOP_EXPR
: /* atomic write */
40684 if (memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
40686 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
40687 "%<acquire%> clause");
40688 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
40690 else if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
)
40691 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
40696 if (fail
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
40698 = (enum omp_memory_order
) (memory_order
40699 | (fail
<< OMP_FAIL_MEMORY_ORDER_SHIFT
));
40703 case OMP_ATOMIC_READ
:
40704 case NOP_EXPR
: /* atomic write */
40705 v
= cp_parser_unary_expression (parser
);
40706 if (v
== error_mark_node
)
40708 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
40710 if (code
== NOP_EXPR
)
40711 lhs
= cp_parser_expression (parser
);
40713 lhs
= cp_parser_unary_expression (parser
);
40714 if (lhs
== error_mark_node
)
40716 if (code
== NOP_EXPR
)
40718 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
40726 case OMP_ATOMIC_CAPTURE_NEW
:
40727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
40729 cp_lexer_consume_token (parser
->lexer
);
40730 structured_block
= true;
40733 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
40737 v
= cp_parser_unary_expression (parser
);
40738 if (v
== error_mark_node
)
40740 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
40743 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
40745 location_t eloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40746 error_at (eloc
, "expected expression");
40755 if (compare
&& cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
40757 cp_lexer_consume_token (parser
->lexer
);
40759 matching_parens parens
;
40760 if (!parens
.require_open (parser
))
40762 location_t eloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40765 cmp_expr
= cp_parser_unary_expression (parser
);
40767 cmp_expr
= cp_parser_binary_expression (parser
, false, true,
40768 PREC_NOT_OPERATOR
, NULL
);
40769 if (!parens
.require_close (parser
))
40770 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
40771 if (cmp_expr
== error_mark_node
)
40775 if (!cp_tree_equal (cmp_expr
, r
))
40779 gcc_assert (TREE_CODE (cmp_expr
) == EQ_EXPR
);
40781 if (TREE_CODE (cmp_expr
) == EQ_EXPR
)
40783 else if (!structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
40785 error_at (EXPR_LOC_OR_LOC (cmp_expr
, eloc
),
40786 "expected %<==%> comparison in %<if%> condition");
40789 else if (TREE_CODE (cmp_expr
) != GT_EXPR
40790 && TREE_CODE (cmp_expr
) != LT_EXPR
)
40792 error_at (EXPR_LOC_OR_LOC (cmp_expr
, eloc
),
40793 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
40797 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
40800 extra_scope
= true;
40801 eloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40802 lhs
= cp_parser_unary_expression (parser
);
40804 if (lhs
== error_mark_node
)
40806 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
40808 cp_parser_error (parser
, "expected %<=%>");
40811 cp_lexer_consume_token (parser
->lexer
);
40812 eloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40813 if (TREE_CODE (cmp_expr
) == EQ_EXPR
)
40814 rhs1
= cp_parser_expression (parser
);
40816 rhs1
= cp_parser_simple_cast_expression (parser
);
40818 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
40821 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
40824 extra_scope
= false;
40825 no_semicolon
= true;
40827 if (cp_tree_equal (TREE_OPERAND (cmp_expr
, 0), lhs
))
40829 if (TREE_CODE (cmp_expr
) == EQ_EXPR
)
40831 opcode
= COND_EXPR
;
40832 rhs
= TREE_OPERAND (cmp_expr
, 1);
40834 else if (cp_tree_equal (TREE_OPERAND (cmp_expr
, 1), rhs1
))
40836 opcode
= (TREE_CODE (cmp_expr
) == GT_EXPR
40837 ? MIN_EXPR
: MAX_EXPR
);
40839 rhs1
= TREE_OPERAND (cmp_expr
, 0);
40844 else if (TREE_CODE (cmp_expr
) == EQ_EXPR
)
40846 else if (cp_tree_equal (TREE_OPERAND (cmp_expr
, 1), lhs
)
40847 && cp_tree_equal (TREE_OPERAND (cmp_expr
, 0), rhs1
))
40849 opcode
= (TREE_CODE (cmp_expr
) == GT_EXPR
40850 ? MAX_EXPR
: MIN_EXPR
);
40852 rhs1
= TREE_OPERAND (cmp_expr
, 1);
40857 cp_parser_error (parser
,
40858 "invalid form of %<#pragma omp atomic compare%>");
40862 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
40864 if (code
!= OMP_ATOMIC_CAPTURE_NEW
40865 || (structured_block
&& r
== NULL_TREE
)
40866 || TREE_CODE (cmp_expr
) != EQ_EXPR
)
40868 eloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40869 error_at (eloc
, "unexpected %<else%>");
40873 cp_lexer_consume_token (parser
->lexer
);
40875 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
40878 extra_scope
= true;
40879 v
= cp_parser_unary_expression (parser
);
40880 if (v
== error_mark_node
)
40882 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
40885 tree expr
= cp_parser_simple_cast_expression (parser
);
40887 if (!cp_tree_equal (expr
, lhs
))
40890 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
40893 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
40896 extra_scope
= false;
40897 code
= OMP_ATOMIC_CAPTURE_OLD
;
40898 if (r
== NULL_TREE
)
40899 /* Signal to c_finish_omp_atomic that in
40900 if (x == e) { x = d; } else { v = x; }
40901 case the store to v should be conditional. */
40902 r
= void_list_node
;
40904 else if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
40906 cp_parser_error (parser
, "expected %<else%>");
40909 else if (code
== OMP_ATOMIC_CAPTURE_NEW
40915 lhs
= cp_parser_unary_expression (parser
);
40917 switch (TREE_CODE (lhs
))
40922 case POSTINCREMENT_EXPR
:
40923 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
40924 code
= OMP_ATOMIC_CAPTURE_OLD
;
40926 case PREINCREMENT_EXPR
:
40927 lhs
= TREE_OPERAND (lhs
, 0);
40928 opcode
= PLUS_EXPR
;
40929 rhs
= integer_one_node
;
40931 goto invalid_compare
;
40934 case POSTDECREMENT_EXPR
:
40935 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
40936 code
= OMP_ATOMIC_CAPTURE_OLD
;
40938 case PREDECREMENT_EXPR
:
40939 lhs
= TREE_OPERAND (lhs
, 0);
40940 opcode
= MINUS_EXPR
;
40941 rhs
= integer_one_node
;
40943 goto invalid_compare
;
40946 case COMPOUND_EXPR
:
40947 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
40948 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
40949 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
40950 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
40951 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
40952 (TREE_OPERAND (lhs
, 1), 0), 0)))
40954 /* Undo effects of boolean_increment for post {in,de}crement. */
40955 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
40958 if (TREE_CODE (lhs
) == MODIFY_EXPR
40959 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
40961 /* Undo effects of boolean_increment. */
40962 if (integer_onep (TREE_OPERAND (lhs
, 1)))
40964 /* This is pre or post increment. */
40965 rhs
= TREE_OPERAND (lhs
, 1);
40966 lhs
= TREE_OPERAND (lhs
, 0);
40968 if (code
== OMP_ATOMIC_CAPTURE_NEW
40969 && !structured_block
40970 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
40971 code
= OMP_ATOMIC_CAPTURE_OLD
;
40973 goto invalid_compare
;
40979 if (compare
&& !cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
40981 cp_parser_error (parser
, "expected %<=%>");
40984 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
40987 opcode
= MULT_EXPR
;
40990 opcode
= TRUNC_DIV_EXPR
;
40993 opcode
= PLUS_EXPR
;
40996 opcode
= MINUS_EXPR
;
40998 case CPP_LSHIFT_EQ
:
40999 opcode
= LSHIFT_EXPR
;
41001 case CPP_RSHIFT_EQ
:
41002 opcode
= RSHIFT_EXPR
;
41005 opcode
= BIT_AND_EXPR
;
41008 opcode
= BIT_IOR_EXPR
;
41011 opcode
= BIT_XOR_EXPR
;
41014 enum cp_parser_prec oprec
;
41016 cp_lexer_consume_token (parser
->lexer
);
41017 cp_parser_parse_tentatively (parser
);
41018 rhs1
= cp_parser_simple_cast_expression (parser
);
41019 if (rhs1
== error_mark_node
)
41021 cp_parser_abort_tentative_parse (parser
);
41022 cp_parser_simple_cast_expression (parser
);
41025 token
= cp_lexer_peek_token (parser
->lexer
);
41026 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
41028 cp_parser_abort_tentative_parse (parser
);
41029 cp_parser_parse_tentatively (parser
);
41030 rhs
= cp_parser_binary_expression (parser
, false, true,
41031 PREC_NOT_OPERATOR
, NULL
);
41032 if (rhs
== error_mark_node
)
41034 cp_parser_abort_tentative_parse (parser
);
41035 cp_parser_binary_expression (parser
, false, true,
41036 PREC_NOT_OPERATOR
, NULL
);
41039 switch (TREE_CODE (rhs
))
41042 case TRUNC_DIV_EXPR
:
41053 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
41055 if (cp_parser_parse_definitely (parser
))
41057 opcode
= TREE_CODE (rhs
);
41058 rhs1
= TREE_OPERAND (rhs
, 0);
41059 rhs
= TREE_OPERAND (rhs
, 1);
41068 || code
!= OMP_ATOMIC_CAPTURE_NEW
41069 || !structured_block
41073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
41074 && cp_lexer_nth_token_is_keyword (parser
->lexer
,
41077 if (cp_parser_parse_definitely (parser
))
41082 cp_lexer_consume_token (parser
->lexer
);
41090 && cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
)
41091 && cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1))
41092 && cp_parser_parse_definitely (parser
))
41094 opcode
= TREE_CODE (rhs
);
41095 rhs1
= TREE_OPERAND (rhs
, 0);
41096 rhs
= TREE_OPERAND (rhs
, 1);
41098 cp_lexer_consume_token (parser
->lexer
);
41099 bool saved_colon_corrects_to_scope_p
41100 = parser
->colon_corrects_to_scope_p
;
41101 parser
->colon_corrects_to_scope_p
= false;
41102 tree e1
= cp_parser_expression (parser
);
41103 parser
->colon_corrects_to_scope_p
41104 = saved_colon_corrects_to_scope_p
;
41105 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
41106 tree e2
= cp_parser_simple_cast_expression (parser
);
41107 if (cp_tree_equal (lhs
, e2
))
41109 if (cp_tree_equal (lhs
, rhs1
))
41111 if (opcode
== EQ_EXPR
)
41113 opcode
= COND_EXPR
;
41117 if (cp_tree_equal (rhs
, e1
))
41120 = opcode
== GT_EXPR
? MIN_EXPR
: MAX_EXPR
;
41127 gcc_assert (opcode
!= EQ_EXPR
);
41128 if (cp_tree_equal (rhs1
, e1
))
41131 = opcode
== GT_EXPR
? MAX_EXPR
: MIN_EXPR
;
41138 cp_parser_error (parser
,
41140 "%<#pragma omp atomic compare%>");
41147 cp_parser_abort_tentative_parse (parser
);
41148 if (structured_block
41149 && code
== OMP_ATOMIC_CAPTURE_OLD
41152 rhs
= cp_parser_expression (parser
);
41153 if (rhs
== error_mark_node
)
41159 cp_parser_error (parser
,
41160 "invalid form of %<#pragma omp atomic%>");
41163 if (!cp_parser_parse_definitely (parser
))
41165 switch (token
->type
)
41167 case CPP_SEMICOLON
:
41168 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
41170 code
= OMP_ATOMIC_CAPTURE_OLD
;
41175 cp_lexer_consume_token (parser
->lexer
);
41178 else if (structured_block
&& !compare
)
41185 cp_parser_error (parser
,
41186 "invalid form of %<#pragma omp atomic%>");
41189 opcode
= MULT_EXPR
;
41192 opcode
= TRUNC_DIV_EXPR
;
41195 opcode
= PLUS_EXPR
;
41198 opcode
= MINUS_EXPR
;
41201 opcode
= LSHIFT_EXPR
;
41204 opcode
= RSHIFT_EXPR
;
41207 opcode
= BIT_AND_EXPR
;
41210 opcode
= BIT_IOR_EXPR
;
41213 opcode
= BIT_XOR_EXPR
;
41225 cp_parser_error (parser
,
41226 "invalid operator for %<#pragma omp atomic%>");
41230 && TREE_CODE_CLASS (opcode
) != tcc_comparison
)
41232 cp_parser_error (parser
,
41234 "%<#pragma omp atomic compare%>");
41237 oprec
= TOKEN_PRECEDENCE (token
);
41238 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
41239 if (commutative_tree_code (opcode
))
41240 oprec
= (enum cp_parser_prec
) (oprec
- 1);
41241 cp_lexer_consume_token (parser
->lexer
);
41242 rhs
= cp_parser_binary_expression (parser
, false, false,
41244 if (rhs
== error_mark_node
)
41248 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
41250 cp_parser_error (parser
,
41252 "%<#pragma omp atomic compare%>");
41259 cp_parser_error (parser
,
41260 "invalid operator for %<#pragma omp atomic%>");
41263 cp_lexer_consume_token (parser
->lexer
);
41265 rhs
= cp_parser_expression (parser
);
41266 if (rhs
== error_mark_node
)
41271 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
&& r
== NULL_TREE
)
41274 && !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
41276 no_semicolon
= false;
41277 v
= cp_parser_unary_expression (parser
);
41278 if (v
== error_mark_node
)
41280 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
41282 lhs1
= cp_parser_unary_expression (parser
);
41283 if (lhs1
== error_mark_node
)
41286 if (structured_block
)
41289 cp_parser_consume_semicolon_at_end_of_statement (parser
);
41290 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
41293 if (weak
&& opcode
!= COND_EXPR
)
41295 error_at (loc
, "%<weak%> clause requires atomic equality comparison");
41298 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
41299 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
41300 rhs1
, r
, clauses
, memory_order
, weak
);
41301 if (!structured_block
&& !no_semicolon
)
41302 cp_parser_consume_semicolon_at_end_of_statement (parser
);
41306 error ("invalid form of %<pragma omp atomic compare%>");
41309 cp_parser_skip_to_end_of_block_or_statement (parser
);
41310 if (extra_scope
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
41311 cp_lexer_consume_token (parser
->lexer
);
41312 if (structured_block
)
41314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
41315 cp_lexer_consume_token (parser
->lexer
);
41316 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
41318 cp_parser_skip_to_end_of_block_or_statement (parser
);
41319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
41320 cp_lexer_consume_token (parser
->lexer
);
41327 # pragma omp barrier new-line */
41330 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
41332 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41333 finish_omp_barrier ();
41337 # pragma omp critical [(name)] new-line
41341 # pragma omp critical [(name) [hint(expression)]] new-line
41342 structured-block */
41344 #define OMP_CRITICAL_CLAUSE_MASK \
41345 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
41348 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
41350 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
41352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
41354 matching_parens parens
;
41355 parens
.consume_open (parser
);
41357 name
= cp_parser_identifier (parser
);
41359 if (name
== error_mark_node
41360 || !parens
.require_close (parser
))
41361 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
41362 /*or_comma=*/false,
41363 /*consume_paren=*/true);
41364 if (name
== error_mark_node
)
41367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
41368 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
41369 cp_lexer_consume_token (parser
->lexer
);
41372 clauses
= cp_parser_omp_all_clauses (parser
, OMP_CRITICAL_CLAUSE_MASK
,
41373 "#pragma omp critical", pragma_tok
);
41375 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
41376 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
41380 # pragma omp depobj ( depobj ) depobj-clause new-line
41383 depend (dependence-type : locator)
41385 update (dependence-type)
41394 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
41396 location_t loc
= pragma_tok
->location
;
41397 matching_parens parens
;
41398 if (!parens
.require_open (parser
))
41400 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41404 tree depobj
= cp_parser_assignment_expression (parser
);
41406 if (!parens
.require_close (parser
))
41407 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
41408 /*or_comma=*/false,
41409 /*consume_paren=*/true);
41411 tree clause
= NULL_TREE
;
41412 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
41413 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41414 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41415 if (parser
->lexer
->in_omp_attribute_pragma
41416 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
41417 cp_lexer_consume_token (parser
->lexer
);
41418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41420 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41421 const char *p
= IDENTIFIER_POINTER (id
);
41423 cp_lexer_consume_token (parser
->lexer
);
41424 if (!strcmp ("depend", p
))
41426 /* Don't create location wrapper nodes within the depend clause. */
41427 auto_suppress_location_wrappers sentinel
;
41428 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
41430 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
41432 clause
= error_mark_node
;
41434 else if (!strcmp ("destroy", p
))
41435 kind
= OMP_CLAUSE_DEPEND_LAST
;
41436 else if (!strcmp ("update", p
))
41438 matching_parens c_parens
;
41439 if (c_parens
.require_open (parser
))
41442 = cp_lexer_peek_token (parser
->lexer
)->location
;
41443 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41445 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41446 const char *p2
= IDENTIFIER_POINTER (id2
);
41448 cp_lexer_consume_token (parser
->lexer
);
41449 if (!strcmp ("in", p2
))
41450 kind
= OMP_CLAUSE_DEPEND_IN
;
41451 else if (!strcmp ("out", p2
))
41452 kind
= OMP_CLAUSE_DEPEND_OUT
;
41453 else if (!strcmp ("inout", p2
))
41454 kind
= OMP_CLAUSE_DEPEND_INOUT
;
41455 else if (!strcmp ("mutexinoutset", p2
))
41456 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
41458 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
41460 clause
= error_mark_node
;
41461 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
41462 "%<mutexinoutset%>");
41464 if (!c_parens
.require_close (parser
))
41465 cp_parser_skip_to_closing_parenthesis (parser
,
41466 /*recovering=*/true,
41467 /*or_comma=*/false,
41468 /*consume_paren=*/true);
41471 clause
= error_mark_node
;
41474 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
41476 clause
= error_mark_node
;
41477 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
41479 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41481 finish_omp_depobj (loc
, depobj
, kind
, clause
);
41486 # pragma omp flush flush-vars[opt] new-line
41492 # pragma omp flush memory-order-clause new-line */
41495 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
41497 enum memmodel mo
= MEMMODEL_LAST
;
41498 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41499 if (parser
->lexer
->in_omp_attribute_pragma
41500 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
41501 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
41502 cp_lexer_consume_token (parser
->lexer
);
41503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41505 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41506 const char *p
= IDENTIFIER_POINTER (id
);
41507 if (!strcmp (p
, "seq_cst"))
41508 mo
= MEMMODEL_SEQ_CST
;
41509 else if (!strcmp (p
, "acq_rel"))
41510 mo
= MEMMODEL_ACQ_REL
;
41511 else if (!strcmp (p
, "release"))
41512 mo
= MEMMODEL_RELEASE
;
41513 else if (!strcmp (p
, "acquire"))
41514 mo
= MEMMODEL_ACQUIRE
;
41516 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
41517 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
41519 cp_lexer_consume_token (parser
->lexer
);
41521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
41523 if (mo
!= MEMMODEL_LAST
)
41524 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
41525 "%<flush%> list specified together with memory order "
41527 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
41529 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41531 finish_omp_flush (mo
);
41534 /* Helper function, to parse omp for increment expression. */
41537 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
41539 tree cond
= cp_parser_binary_expression (parser
, false, true,
41540 PREC_NOT_OPERATOR
, NULL
);
41541 if (cond
== error_mark_node
41542 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
41544 cp_parser_skip_to_end_of_statement (parser
);
41545 return error_mark_node
;
41548 switch (TREE_CODE (cond
))
41556 if (code
!= OACC_LOOP
)
41558 gcc_fallthrough ();
41560 return error_mark_node
;
41563 /* If decl is an iterator, preserve LHS and RHS of the relational
41564 expr until finish_omp_for. */
41566 && (type_dependent_expression_p (decl
)
41567 || CLASS_TYPE_P (TREE_TYPE (decl
))))
41570 return build_x_binary_op (cp_expr_loc_or_input_loc (cond
),
41572 TREE_OPERAND (cond
, 0), ERROR_MARK
,
41573 TREE_OPERAND (cond
, 1), ERROR_MARK
,
41574 NULL_TREE
, /*overload=*/NULL
, tf_warning_or_error
);
41577 /* Helper function, to parse omp for increment expression. */
41580 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
41582 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
41588 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
41590 op
= (token
->type
== CPP_PLUS_PLUS
41591 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
41592 cp_lexer_consume_token (parser
->lexer
);
41593 lhs
= cp_parser_simple_cast_expression (parser
);
41595 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
41596 return error_mark_node
;
41597 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
41600 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
41602 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
41603 return error_mark_node
;
41605 token
= cp_lexer_peek_token (parser
->lexer
);
41606 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
41608 op
= (token
->type
== CPP_PLUS_PLUS
41609 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
41610 cp_lexer_consume_token (parser
->lexer
);
41611 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
41614 op
= cp_parser_assignment_operator_opt (parser
);
41615 if (op
== ERROR_MARK
)
41616 return error_mark_node
;
41618 if (op
!= NOP_EXPR
)
41620 rhs
= cp_parser_assignment_expression (parser
);
41621 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
41622 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
41625 lhs
= cp_parser_binary_expression (parser
, false, false,
41626 PREC_ADDITIVE_EXPRESSION
, NULL
);
41627 token
= cp_lexer_peek_token (parser
->lexer
);
41628 decl_first
= (lhs
== decl
41629 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
41632 if (token
->type
!= CPP_PLUS
41633 && token
->type
!= CPP_MINUS
)
41634 return error_mark_node
;
41638 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
41639 cp_lexer_consume_token (parser
->lexer
);
41640 rhs
= cp_parser_binary_expression (parser
, false, false,
41641 PREC_ADDITIVE_EXPRESSION
, NULL
);
41642 token
= cp_lexer_peek_token (parser
->lexer
);
41643 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
41645 if (lhs
== NULL_TREE
)
41647 if (op
== PLUS_EXPR
)
41650 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
41651 NULL_TREE
, tf_warning_or_error
);
41654 lhs
= build_x_binary_op (input_location
, op
,
41657 NULL_TREE
, NULL
, tf_warning_or_error
);
41660 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
41665 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
41666 || op
== MINUS_EXPR
)
41667 return error_mark_node
;
41668 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
41671 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
41673 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
41676 /* Parse the initialization statement of an OpenMP for loop.
41678 Return true if the resulting construct should have an
41679 OMP_CLAUSE_PRIVATE added to it. */
41682 cp_parser_omp_for_loop_init (cp_parser
*parser
,
41683 tree
&this_pre_body
,
41684 releasing_vec
&for_block
,
41690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
41693 tree add_private_clause
= NULL_TREE
;
41695 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
41699 integer-type var = lb
41700 random-access-iterator-type var = lb
41701 pointer-type var = lb
41703 cp_decl_specifier_seq type_specifiers
;
41705 /* First, try to parse as an initialized declaration. See
41706 cp_parser_condition, from whence the bulk of this is copied. */
41708 cp_parser_parse_tentatively (parser
);
41709 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
41710 /*is_declaration=*/true,
41711 /*is_trailing_return=*/false,
41713 if (cp_parser_parse_definitely (parser
))
41715 /* If parsing a type specifier seq succeeded, then this
41716 MUST be a initialized declaration. */
41717 tree asm_specification
, attributes
;
41718 cp_declarator
*declarator
;
41720 declarator
= cp_parser_declarator (parser
,
41721 CP_PARSER_DECLARATOR_NAMED
,
41722 CP_PARSER_FLAGS_NONE
,
41723 /*ctor_dtor_or_conv_p=*/NULL
,
41724 /*parenthesized_p=*/NULL
,
41725 /*member_p=*/false,
41726 /*friend_p=*/false,
41727 /*static_p=*/false);
41728 attributes
= cp_parser_attributes_opt (parser
);
41729 asm_specification
= cp_parser_asm_specification_opt (parser
);
41731 if (declarator
== cp_error_declarator
)
41732 cp_parser_skip_to_end_of_statement (parser
);
41736 tree pushed_scope
, auto_node
;
41738 decl
= start_decl (declarator
, &type_specifiers
,
41739 SD_INITIALIZED
, attributes
,
41740 /*prefix_attributes=*/NULL_TREE
,
41743 auto_node
= type_uses_auto (TREE_TYPE (decl
));
41744 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
41746 if (cp_lexer_next_token_is (parser
->lexer
,
41748 error ("parenthesized initialization is not allowed in "
41749 "OpenMP %<for%> loop");
41751 /* Trigger an error. */
41752 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
41754 init
= error_mark_node
;
41755 cp_parser_skip_to_end_of_statement (parser
);
41757 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
41758 || type_dependent_expression_p (decl
)
41761 bool is_direct_init
, is_non_constant_init
;
41763 init
= cp_parser_initializer (parser
,
41765 &is_non_constant_init
);
41770 = do_auto_deduction (TREE_TYPE (decl
), init
,
41773 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
41774 && !type_dependent_expression_p (decl
))
41778 cp_finish_decl (decl
, init
, !is_non_constant_init
,
41780 LOOKUP_ONLYCONVERTING
);
41782 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
41784 vec_safe_push (for_block
, this_pre_body
);
41789 init
= pop_stmt_list (this_pre_body
);
41790 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
41792 tree_stmt_iterator i
= tsi_start (init
);
41793 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
41794 while (!tsi_end_p (i
))
41796 tree t
= tsi_stmt (i
);
41797 if (TREE_CODE (t
) == DECL_EXPR
41798 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
41801 vec_safe_push (for_block
, t
);
41806 if (tsi_one_before_end_p (i
))
41808 tree t
= tsi_stmt (i
);
41810 free_stmt_list (init
);
41815 this_pre_body
= NULL_TREE
;
41820 cp_lexer_consume_token (parser
->lexer
);
41821 init
= cp_parser_assignment_expression (parser
);
41824 if (TYPE_REF_P (TREE_TYPE (decl
)))
41825 init
= error_mark_node
;
41827 cp_finish_decl (decl
, NULL_TREE
,
41828 /*init_const_expr_p=*/false,
41830 LOOKUP_ONLYCONVERTING
);
41834 pop_scope (pushed_scope
);
41840 /* If parsing a type specifier sequence failed, then
41841 this MUST be a simple expression. */
41842 cp_parser_parse_tentatively (parser
);
41843 decl
= cp_parser_primary_expression (parser
, false, false,
41845 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
41846 if (!cp_parser_error_occurred (parser
)
41848 && (TREE_CODE (decl
) == COMPONENT_REF
41849 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
41851 cp_parser_abort_tentative_parse (parser
);
41852 cp_parser_parse_tentatively (parser
);
41853 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
41854 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
41855 /*check_dependency_p=*/true,
41856 /*template_p=*/NULL
,
41857 /*declarator_p=*/false,
41858 /*optional_p=*/false);
41859 if (name
!= error_mark_node
41860 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
41862 decl
= cp_parser_lookup_name_simple (parser
, name
,
41864 if (TREE_CODE (decl
) == FIELD_DECL
)
41865 add_private_clause
= omp_privatize_field (decl
, false);
41867 cp_parser_abort_tentative_parse (parser
);
41868 cp_parser_parse_tentatively (parser
);
41869 decl
= cp_parser_primary_expression (parser
, false, false,
41872 if (!cp_parser_error_occurred (parser
)
41875 && CLASS_TYPE_P (TREE_TYPE (decl
)))
41879 cp_parser_parse_definitely (parser
);
41880 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
41881 rhs
= cp_parser_assignment_expression (parser
);
41883 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
41886 tf_warning_or_error
));
41887 if (!add_private_clause
)
41888 add_private_clause
= decl
;
41893 cp_parser_abort_tentative_parse (parser
);
41894 init
= cp_parser_expression (parser
);
41897 if (TREE_CODE (init
) == MODIFY_EXPR
41898 || TREE_CODE (init
) == MODOP_EXPR
)
41899 real_decl
= TREE_OPERAND (init
, 0);
41903 return add_private_clause
;
41906 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
41909 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
41910 tree
&decl
, tree
&orig_decl
, tree
&init
,
41911 tree
&orig_init
, tree
&cond
, tree
&incr
)
41913 tree begin
, end
, range_temp_decl
= NULL_TREE
;
41914 tree iter_type
, begin_expr
, end_expr
;
41916 if (processing_template_decl
)
41918 if (check_for_bare_parameter_packs (init
))
41919 init
= error_mark_node
;
41920 if (!type_dependent_expression_p (init
)
41921 /* do_auto_deduction doesn't mess with template init-lists. */
41922 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
41925 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
41927 tree v
= DECL_VALUE_EXPR (decl
);
41928 if (TREE_CODE (v
) == ARRAY_REF
41929 && VAR_P (TREE_OPERAND (v
, 0))
41930 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
41931 d
= TREE_OPERAND (v
, 0);
41933 do_range_for_auto_deduction (d
, init
);
41935 cond
= global_namespace
;
41939 this_pre_body
= pop_stmt_list (this_pre_body
);
41943 init
= mark_lvalue_use (init
);
41945 if (decl
== error_mark_node
|| init
== error_mark_node
)
41946 /* If an error happened previously do nothing or else a lot of
41947 unhelpful errors would be issued. */
41948 begin_expr
= end_expr
= iter_type
= error_mark_node
;
41954 && array_of_runtime_bound_p (TREE_TYPE (init
)))
41955 /* Can't bind a reference to an array of runtime bound. */
41959 range_temp
= build_range_temp (init
);
41960 DECL_NAME (range_temp
) = NULL_TREE
;
41961 pushdecl (range_temp
);
41962 cp_finish_decl (range_temp
, init
,
41963 /*is_constant_init*/false, NULL_TREE
,
41964 LOOKUP_ONLYCONVERTING
);
41965 range_temp_decl
= range_temp
;
41966 range_temp
= convert_from_reference (range_temp
);
41968 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
41969 &begin_expr
, &end_expr
);
41972 tree end_iter_type
= iter_type
;
41973 if (cxx_dialect
>= cxx17
)
41974 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
41975 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
41976 TREE_USED (end
) = 1;
41977 DECL_ARTIFICIAL (end
) = 1;
41979 cp_finish_decl (end
, end_expr
,
41980 /*is_constant_init*/false, NULL_TREE
,
41981 LOOKUP_ONLYCONVERTING
);
41983 /* The new for initialization statement. */
41984 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
41985 TREE_USED (begin
) = 1;
41986 DECL_ARTIFICIAL (begin
) = 1;
41989 if (CLASS_TYPE_P (iter_type
))
41994 begin_expr
= NULL_TREE
;
41996 cp_finish_decl (begin
, begin_expr
,
41997 /*is_constant_init*/false, NULL_TREE
,
41998 LOOKUP_ONLYCONVERTING
);
42000 /* The new for condition. */
42001 if (CLASS_TYPE_P (iter_type
))
42002 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
42004 cond
= build_x_binary_op (input_location
, NE_EXPR
,
42007 NULL_TREE
, NULL
, tf_warning_or_error
);
42009 /* The new increment expression. */
42010 if (CLASS_TYPE_P (iter_type
))
42011 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
42013 incr
= finish_unary_op_expr (input_location
,
42014 PREINCREMENT_EXPR
, begin
,
42015 tf_warning_or_error
);
42021 vec_safe_push (for_block
, this_pre_body
);
42022 this_pre_body
= NULL_TREE
;
42025 tree decomp_first_name
= NULL_TREE
;
42026 unsigned decomp_cnt
= 0;
42027 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
42029 tree v
= DECL_VALUE_EXPR (orig_decl
);
42030 if (TREE_CODE (v
) == ARRAY_REF
42031 && VAR_P (TREE_OPERAND (v
, 0))
42032 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
42034 tree d
= orig_decl
;
42035 orig_decl
= TREE_OPERAND (v
, 0);
42036 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
42037 decomp_first_name
= d
;
42041 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
42044 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
42045 NULL_TREE
, tf_none
);
42046 if (!error_operand_p (t
))
42047 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
42051 tree v
= make_tree_vec (decomp_cnt
+ 3);
42052 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
42053 TREE_VEC_ELT (v
, 1) = end
;
42054 TREE_VEC_ELT (v
, 2) = orig_decl
;
42055 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
42057 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
42058 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
42060 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
42063 /* Helper for cp_parser_omp_for_loop, finalize part of range for
42064 inside of the collapsed body. */
42067 cp_finish_omp_range_for (tree orig
, tree begin
)
42069 gcc_assert (TREE_CODE (orig
) == TREE_LIST
42070 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
42071 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
42072 tree decomp_first_name
= NULL_TREE
;
42073 unsigned int decomp_cnt
= 0;
42075 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
42077 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
42078 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
42079 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
42082 /* The declaration is initialized with *__begin inside the loop body. */
42083 cp_finish_decl (decl
,
42084 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
42085 NULL_TREE
, tf_warning_or_error
),
42086 /*is_constant_init*/false, NULL_TREE
,
42087 LOOKUP_ONLYCONVERTING
);
42088 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
42089 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
42092 /* Return true if next tokens contain a standard attribute that contains
42093 omp::directive (DIRECTIVE). */
42096 cp_parser_omp_section_scan (cp_parser
*parser
, const char *directive
,
42099 size_t n
= cp_parser_skip_attributes_opt (parser
, 1), i
;
42102 for (i
= 5; i
< n
- 4; i
++)
42103 if (cp_lexer_nth_token_is (parser
->lexer
, i
, CPP_NAME
)
42104 && cp_lexer_nth_token_is (parser
->lexer
, i
+ 1, CPP_OPEN_PAREN
)
42105 && cp_lexer_nth_token_is (parser
->lexer
, i
+ 2, CPP_NAME
))
42107 tree first
= cp_lexer_peek_nth_token (parser
->lexer
, i
)->u
.value
;
42108 tree second
= cp_lexer_peek_nth_token (parser
->lexer
, i
+ 2)->u
.value
;
42109 if (strcmp (IDENTIFIER_POINTER (first
), "directive"))
42111 if (strcmp (IDENTIFIER_POINTER (second
), directive
) == 0)
42116 cp_parser_parse_tentatively (parser
);
42117 location_t first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42118 location_t last_loc
42119 = cp_lexer_peek_nth_token (parser
->lexer
, n
- 1)->location
;
42120 location_t middle_loc
= UNKNOWN_LOCATION
;
42121 tree std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
42124 for (tree attr
= std_attrs
; attr
; attr
= TREE_CHAIN (attr
))
42125 if (get_attribute_namespace (attr
) == omp_identifier
42126 && is_attribute_p ("directive", get_attribute_name (attr
)))
42128 for (tree a
= TREE_VALUE (attr
); a
; a
= TREE_CHAIN (a
))
42130 tree d
= TREE_VALUE (a
);
42131 gcc_assert (TREE_CODE (d
) == DEFERRED_PARSE
);
42132 cp_token
*first
= DEFPARSE_TOKENS (d
)->first
;
42134 if (first
->type
== CPP_NAME
42135 && strcmp (IDENTIFIER_POINTER (first
->u
.value
),
42139 if (middle_loc
== UNKNOWN_LOCATION
)
42140 middle_loc
= first
->location
;
42144 if (!seen
|| tentative
)
42146 cp_parser_abort_tentative_parse (parser
);
42149 if (cnt
!= 1 || TREE_CHAIN (std_attrs
))
42151 error_at (make_location (first_loc
, last_loc
, middle_loc
),
42152 "%<[[omp::directive(%s)]]%> must be the only specified "
42153 "attribute on a statement", directive
);
42154 cp_parser_abort_tentative_parse (parser
);
42157 if (!cp_parser_parse_definitely (parser
))
42159 cp_parser_handle_statement_omp_attributes (parser
, std_attrs
);
42163 /* Parse an OpenMP structured block sequence. KIND is the corresponding
42164 separating directive. */
42167 cp_parser_omp_structured_block_sequence (cp_parser
*parser
,
42168 enum pragma_kind kind
)
42170 tree stmt
= begin_omp_structured_block ();
42171 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
42173 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
42176 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
42178 if (token
->type
== CPP_CLOSE_BRACE
42179 || token
->type
== CPP_EOF
42180 || token
->type
== CPP_PRAGMA_EOL
42181 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
)
42182 || (kind
!= PRAGMA_NONE
42183 && cp_parser_pragma_kind (token
) == kind
))
42186 if (kind
!= PRAGMA_NONE
42187 && cp_parser_omp_section_scan (parser
,
42188 kind
== PRAGMA_OMP_SCAN
42189 ? "scan" : "section", false))
42192 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
42195 cp_parser_end_omp_structured_block (parser
, save
);
42196 return finish_omp_structured_block (stmt
);
42203 { structured-block scan-directive structured-block } */
42206 cp_parser_omp_scan_loop_body (cp_parser
*parser
)
42208 tree substmt
, clauses
= NULL_TREE
;
42210 matching_braces braces
;
42211 if (!braces
.require_open (parser
))
42214 substmt
= cp_parser_omp_structured_block_sequence (parser
, PRAGMA_OMP_SCAN
);
42215 substmt
= build2 (OMP_SCAN
, void_type_node
, substmt
, NULL_TREE
);
42216 add_stmt (substmt
);
42218 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
42219 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SCAN
)
42221 enum omp_clause_code clause
= OMP_CLAUSE_ERROR
;
42223 cp_lexer_consume_token (parser
->lexer
);
42225 if (parser
->lexer
->in_omp_attribute_pragma
42226 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42227 cp_lexer_consume_token (parser
->lexer
);
42229 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42231 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42232 const char *p
= IDENTIFIER_POINTER (id
);
42233 if (strcmp (p
, "inclusive") == 0)
42234 clause
= OMP_CLAUSE_INCLUSIVE
;
42235 else if (strcmp (p
, "exclusive") == 0)
42236 clause
= OMP_CLAUSE_EXCLUSIVE
;
42238 if (clause
!= OMP_CLAUSE_ERROR
)
42240 cp_lexer_consume_token (parser
->lexer
);
42241 clauses
= cp_parser_omp_var_list (parser
, clause
, NULL_TREE
);
42244 cp_parser_error (parser
, "expected %<inclusive%> or "
42245 "%<exclusive%> clause");
42247 cp_parser_require_pragma_eol (parser
, tok
);
42250 error ("expected %<#pragma omp scan%>");
42252 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
42253 substmt
= cp_parser_omp_structured_block_sequence (parser
, PRAGMA_NONE
);
42254 substmt
= build2_loc (tok
->location
, OMP_SCAN
, void_type_node
, substmt
,
42256 add_stmt (substmt
);
42258 braces
.require_close (parser
);
42261 /* Parse the restricted form of the for statement allowed by OpenMP. */
42264 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
42265 tree
*cclauses
, bool *if_p
)
42267 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
42269 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
42270 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
42271 location_t loc_first
;
42272 bool collapse_err
= false;
42273 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
42274 releasing_vec for_block
;
42275 auto_vec
<tree
, 4> orig_inits
;
42276 bool tiling
= false;
42277 bool inscan
= false;
42279 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
42280 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
42281 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
42282 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
42285 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
42287 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
42288 && OMP_CLAUSE_ORDERED_EXPR (cl
))
42291 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
42293 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_REDUCTION
42294 && OMP_CLAUSE_REDUCTION_INSCAN (cl
)
42295 && (code
== OMP_SIMD
|| code
== OMP_FOR
))
42298 if (ordered
&& ordered
< collapse
)
42300 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
42301 "%<ordered%> clause parameter is less than %<collapse%>");
42302 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
42303 = build_int_cst (NULL_TREE
, collapse
);
42304 ordered
= collapse
;
42308 for (tree
*pc
= &clauses
; *pc
; )
42309 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
42311 error_at (OMP_CLAUSE_LOCATION (*pc
),
42312 "%<linear%> clause may not be specified together "
42313 "with %<ordered%> clause with a parameter");
42314 *pc
= OMP_CLAUSE_CHAIN (*pc
);
42317 pc
= &OMP_CLAUSE_CHAIN (*pc
);
42320 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
42321 count
= ordered
? ordered
: collapse
;
42323 declv
= make_tree_vec (count
);
42324 initv
= make_tree_vec (count
);
42325 condv
= make_tree_vec (count
);
42326 incrv
= make_tree_vec (count
);
42327 orig_declv
= NULL_TREE
;
42329 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
42331 for (i
= 0; i
< count
; i
++)
42333 int bracecount
= 0;
42334 tree add_private_clause
= NULL_TREE
;
42337 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
42340 cp_parser_error (parser
, "for statement expected");
42343 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
42345 /* Don't create location wrapper nodes within an OpenMP "for"
42347 auto_suppress_location_wrappers sentinel
;
42349 matching_parens parens
;
42350 if (!parens
.require_open (parser
))
42353 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
42354 this_pre_body
= push_stmt_list ();
42356 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
42358 /* Save tokens so that we can put them back. */
42359 cp_lexer_save_tokens (parser
->lexer
);
42361 /* Look for ':' that is not nested in () or {}. */
42363 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
42364 /*recovering=*/false,
42369 /* Roll back the tokens we skipped. */
42370 cp_lexer_rollback_tokens (parser
->lexer
);
42374 bool saved_colon_corrects_to_scope_p
42375 = parser
->colon_corrects_to_scope_p
;
42377 /* A colon is used in range-based for. */
42378 parser
->colon_corrects_to_scope_p
= false;
42380 /* Parse the declaration. */
42381 cp_parser_simple_declaration (parser
,
42382 /*function_definition_allowed_p=*/
42384 parser
->colon_corrects_to_scope_p
42385 = saved_colon_corrects_to_scope_p
;
42387 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
42389 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
42392 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
42393 orig_decl
, init
, orig_init
,
42400 pre_body
= push_stmt_list ();
42402 add_stmt (this_pre_body
);
42403 pre_body
= pop_stmt_list (pre_body
);
42406 pre_body
= this_pre_body
;
42410 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
42411 "%<ordered%> clause with parameter on "
42412 "range-based %<for%> loop");
42414 goto parse_close_paren
;
42419 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
42420 init
, orig_init
, decl
, real_decl
);
42422 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
42425 this_pre_body
= pop_stmt_list (this_pre_body
);
42429 pre_body
= push_stmt_list ();
42431 add_stmt (this_pre_body
);
42432 pre_body
= pop_stmt_list (pre_body
);
42435 pre_body
= this_pre_body
;
42440 if (cclauses
!= NULL
42441 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
42442 && real_decl
!= NULL_TREE
42443 && code
!= OMP_LOOP
)
42446 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
42447 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
42448 && OMP_CLAUSE_DECL (*c
) == real_decl
)
42450 error_at (loc
, "iteration variable %qD"
42451 " should not be firstprivate", real_decl
);
42452 *c
= OMP_CLAUSE_CHAIN (*c
);
42454 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
42455 && OMP_CLAUSE_DECL (*c
) == real_decl
)
42457 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
42459 *c
= OMP_CLAUSE_CHAIN (*c
);
42460 if (code
== OMP_SIMD
)
42462 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
42463 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
42467 OMP_CLAUSE_CHAIN (l
) = clauses
;
42470 add_private_clause
= NULL_TREE
;
42474 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
42475 && OMP_CLAUSE_DECL (*c
) == real_decl
)
42476 add_private_clause
= NULL_TREE
;
42477 c
= &OMP_CLAUSE_CHAIN (*c
);
42481 if (add_private_clause
)
42484 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
42486 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
42487 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
42488 && OMP_CLAUSE_DECL (c
) == decl
)
42490 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
42491 && OMP_CLAUSE_DECL (c
) == decl
)
42492 error_at (loc
, "iteration variable %qD "
42493 "should not be firstprivate",
42495 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
42496 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
42497 && OMP_CLAUSE_DECL (c
) == decl
)
42498 error_at (loc
, "iteration variable %qD should not be reduction",
42503 if ((code
== OMP_SIMD
&& collapse
!= 1) || code
== OMP_LOOP
)
42504 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
42505 else if (code
!= OMP_SIMD
)
42506 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
42508 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
42509 OMP_CLAUSE_DECL (c
) = add_private_clause
;
42510 c
= finish_omp_clauses (c
, C_ORT_OMP
);
42513 OMP_CLAUSE_CHAIN (c
) = clauses
;
42515 /* For linear, signal that we need to fill up
42516 the so far unknown linear step. */
42517 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
42518 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
42524 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
42525 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
42526 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
42529 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
42531 /* If decl is an iterator, preserve the operator on decl
42532 until finish_omp_for. */
42534 && ((processing_template_decl
42535 && (TREE_TYPE (real_decl
) == NULL_TREE
42536 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
42537 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
42538 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
42540 incr
= cp_parser_expression (parser
);
42541 protected_set_expr_location_if_unset (incr
, input_location
);
42545 if (!parens
.require_close (parser
))
42546 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
42547 /*or_comma=*/false,
42548 /*consume_paren=*/true);
42550 TREE_VEC_ELT (declv
, i
) = decl
;
42551 TREE_VEC_ELT (initv
, i
) = init
;
42552 TREE_VEC_ELT (condv
, i
) = cond
;
42553 TREE_VEC_ELT (incrv
, i
) = incr
;
42556 orig_inits
.safe_grow_cleared (i
+ 1, true);
42557 orig_inits
[i
] = orig_init
;
42562 orig_declv
= copy_node (declv
);
42563 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
42565 else if (orig_declv
)
42566 TREE_VEC_ELT (orig_declv
, i
) = decl
;
42568 if (i
== count
- 1)
42571 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
42572 in between the collapsed for loops to be still considered perfectly
42573 nested. Hopefully the final version clarifies this.
42574 For now handle (multiple) {'s and empty statements. */
42575 cp_parser_parse_tentatively (parser
);
42578 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
42580 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
42582 cp_lexer_consume_token (parser
->lexer
);
42585 else if (bracecount
42586 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
42587 cp_lexer_consume_token (parser
->lexer
);
42590 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42591 error_at (loc
, "not enough for loops to collapse");
42592 collapse_err
= true;
42593 cp_parser_abort_tentative_parse (parser
);
42601 cp_parser_parse_definitely (parser
);
42602 nbraces
+= bracecount
;
42609 /* Note that we saved the original contents of this flag when we entered
42610 the structured block, and so we don't need to re-save it here. */
42611 parser
->in_statement
= IN_OMP_FOR
;
42613 /* Note that the grammar doesn't call for a structured block here,
42614 though the loop as a whole is a structured block. */
42617 body
= begin_omp_structured_block ();
42618 for (i
= 0; i
< count
; i
++)
42619 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
42620 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
42621 TREE_VEC_ELT (declv
, i
));
42624 body
= push_stmt_list ();
42626 cp_parser_omp_scan_loop_body (parser
);
42628 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
42630 body
= finish_omp_structured_block (body
);
42632 body
= pop_stmt_list (body
);
42634 if (declv
== NULL_TREE
)
42637 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
42638 incrv
, body
, pre_body
, &orig_inits
, clauses
);
42642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
42644 cp_lexer_consume_token (parser
->lexer
);
42647 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
42648 cp_lexer_consume_token (parser
->lexer
);
42653 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
42654 "collapsed loops not perfectly nested");
42656 collapse_err
= true;
42657 cp_parser_statement_seq_opt (parser
, NULL
);
42658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
42663 while (!for_block
->is_empty ())
42665 tree t
= for_block
->pop ();
42666 if (TREE_CODE (t
) == STATEMENT_LIST
)
42667 add_stmt (pop_stmt_list (t
));
42675 /* Helper function for OpenMP parsing, split clauses and call
42676 finish_omp_clauses on each of the set of clauses afterwards. */
42679 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
42680 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
42683 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
42684 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
42686 cclauses
[i
] = finish_omp_clauses (cclauses
[i
],
42687 i
== C_OMP_CLAUSE_SPLIT_TARGET
42688 ? C_ORT_OMP_TARGET
: C_ORT_OMP
);
42692 #pragma omp loop loop-clause[optseq] new-line
42695 #define OMP_LOOP_CLAUSE_MASK \
42696 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
42701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42704 cp_parser_omp_loop (cp_parser
*parser
, cp_token
*pragma_tok
,
42705 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
42708 tree clauses
, sb
, ret
;
42710 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42712 strcat (p_name
, " loop");
42713 mask
|= OMP_LOOP_CLAUSE_MASK
;
42715 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
42719 cp_omp_split_clauses (loc
, OMP_LOOP
, mask
, clauses
, cclauses
);
42720 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_LOOP
];
42723 keep_next_level (true);
42724 sb
= begin_omp_structured_block ();
42725 save
= cp_parser_begin_omp_structured_block (parser
);
42727 ret
= cp_parser_omp_for_loop (parser
, OMP_LOOP
, clauses
, cclauses
, if_p
);
42729 cp_parser_end_omp_structured_block (parser
, save
);
42730 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
42736 #pragma omp simd simd-clause[optseq] new-line
42739 #define OMP_SIMD_CLAUSE_MASK \
42740 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
42741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
42742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
42743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
42744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
42749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
42750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42753 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
42754 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
42757 tree clauses
, sb
, ret
;
42759 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42761 strcat (p_name
, " simd");
42762 mask
|= OMP_SIMD_CLAUSE_MASK
;
42764 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
42768 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
42769 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
42770 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
42771 OMP_CLAUSE_ORDERED
);
42772 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
42774 error_at (OMP_CLAUSE_LOCATION (c
),
42775 "%<ordered%> clause with parameter may not be specified "
42776 "on %qs construct", p_name
);
42777 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
42781 keep_next_level (true);
42782 sb
= begin_omp_structured_block ();
42783 save
= cp_parser_begin_omp_structured_block (parser
);
42785 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
42787 cp_parser_end_omp_structured_block (parser
, save
);
42788 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
42794 #pragma omp for for-clause[optseq] new-line
42798 #pragma omp for simd for-simd-clause[optseq] new-line
42801 #define OMP_FOR_CLAUSE_MASK \
42802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
42804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
42806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
42808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
42809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
42810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
42812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42815 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
42816 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
42819 tree clauses
, sb
, ret
;
42821 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42823 strcat (p_name
, " for");
42824 mask
|= OMP_FOR_CLAUSE_MASK
;
42825 /* parallel for{, simd} disallows nowait clause, but for
42826 target {teams distribute ,}parallel for{, simd} it should be accepted. */
42827 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
42828 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
42829 /* Composite distribute parallel for{, simd} disallows ordered clause. */
42830 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
42831 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
42833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42835 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42836 const char *p
= IDENTIFIER_POINTER (id
);
42838 if (strcmp (p
, "simd") == 0)
42840 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
42841 if (cclauses
== NULL
)
42842 cclauses
= cclauses_buf
;
42844 cp_lexer_consume_token (parser
->lexer
);
42845 if (!flag_openmp
) /* flag_openmp_simd */
42846 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
42848 sb
= begin_omp_structured_block ();
42849 save
= cp_parser_begin_omp_structured_block (parser
);
42850 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
42852 cp_parser_end_omp_structured_block (parser
, save
);
42853 tree body
= finish_omp_structured_block (sb
);
42856 ret
= make_node (OMP_FOR
);
42857 TREE_TYPE (ret
) = void_type_node
;
42858 OMP_FOR_BODY (ret
) = body
;
42859 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
42860 SET_EXPR_LOCATION (ret
, loc
);
42865 if (!flag_openmp
) /* flag_openmp_simd */
42867 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42871 /* Composite distribute parallel for disallows linear clause. */
42872 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
42873 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
42875 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
42879 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
42880 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
42883 keep_next_level (true);
42884 sb
= begin_omp_structured_block ();
42885 save
= cp_parser_begin_omp_structured_block (parser
);
42887 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
42889 cp_parser_end_omp_structured_block (parser
, save
);
42890 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
42895 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
42896 omp_clause_mask
, tree
*, bool *);
42899 # pragma omp master new-line
42900 structured-block */
42903 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
42904 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
42907 tree clauses
, sb
, ret
;
42909 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42911 strcat (p_name
, " master");
42913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42915 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42916 const char *p
= IDENTIFIER_POINTER (id
);
42918 if (strcmp (p
, "taskloop") == 0)
42920 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
42921 if (cclauses
== NULL
)
42922 cclauses
= cclauses_buf
;
42924 cp_lexer_consume_token (parser
->lexer
);
42925 if (!flag_openmp
) /* flag_openmp_simd */
42926 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
42928 sb
= begin_omp_structured_block ();
42929 save
= cp_parser_begin_omp_structured_block (parser
);
42930 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
42932 cp_parser_end_omp_structured_block (parser
, save
);
42933 tree body
= finish_omp_structured_block (sb
);
42936 ret
= c_finish_omp_master (loc
, body
);
42937 OMP_MASTER_COMBINED (ret
) = 1;
42941 if (!flag_openmp
) /* flag_openmp_simd */
42943 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42949 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
42951 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
42954 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42956 return c_finish_omp_master (loc
,
42957 cp_parser_omp_structured_block (parser
, if_p
));
42961 # pragma omp masked masked-clauses new-line
42962 structured-block */
42964 #define OMP_MASKED_CLAUSE_MASK \
42965 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
42968 cp_parser_omp_masked (cp_parser
*parser
, cp_token
*pragma_tok
,
42969 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
42972 tree clauses
, sb
, ret
;
42974 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42976 strcat (p_name
, " masked");
42977 mask
|= OMP_MASKED_CLAUSE_MASK
;
42979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42981 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42982 const char *p
= IDENTIFIER_POINTER (id
);
42984 if (strcmp (p
, "taskloop") == 0)
42986 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
42987 if (cclauses
== NULL
)
42988 cclauses
= cclauses_buf
;
42990 cp_lexer_consume_token (parser
->lexer
);
42991 if (!flag_openmp
) /* flag_openmp_simd */
42992 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
42994 sb
= begin_omp_structured_block ();
42995 save
= cp_parser_begin_omp_structured_block (parser
);
42996 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
42998 cp_parser_end_omp_structured_block (parser
, save
);
42999 tree body
= finish_omp_structured_block (sb
);
43002 ret
= c_finish_omp_masked (loc
, body
,
43003 cclauses
[C_OMP_CLAUSE_SPLIT_MASKED
]);
43004 OMP_MASKED_COMBINED (ret
) = 1;
43008 if (!flag_openmp
) /* flag_openmp_simd */
43010 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43014 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43018 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
43019 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_MASKED
];
43022 return c_finish_omp_masked (loc
,
43023 cp_parser_omp_structured_block (parser
, if_p
),
43028 # pragma omp ordered new-line
43032 # pragma omp ordered ordered-clauses new-line
43033 structured-block */
43035 #define OMP_ORDERED_CLAUSE_MASK \
43036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
43037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
43039 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
43040 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
43043 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
43044 enum pragma_context context
, bool *if_p
)
43046 location_t loc
= pragma_tok
->location
;
43049 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
43050 if (parser
->lexer
->in_omp_attribute_pragma
43051 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
43054 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_NAME
))
43056 tree id
= cp_lexer_peek_nth_token (parser
->lexer
, n
)->u
.value
;
43057 const char *p
= IDENTIFIER_POINTER (id
);
43059 if (strcmp (p
, "depend") == 0)
43061 if (!flag_openmp
) /* flag_openmp_simd */
43063 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43066 if (context
== pragma_stmt
)
43068 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
43069 "%<depend%> clause may only be used in compound "
43071 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43075 = cp_parser_omp_all_clauses (parser
,
43076 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
43077 "#pragma omp ordered", pragma_tok
);
43078 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
43084 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
43085 "#pragma omp ordered", pragma_tok
);
43087 if (!flag_openmp
/* flag_openmp_simd */
43088 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
43091 c_finish_omp_ordered (loc
, clauses
,
43092 cp_parser_omp_structured_block (parser
, if_p
));
43099 { section-sequence }
43102 section-directive[opt] structured-block
43103 section-sequence section-directive structured-block */
43106 cp_parser_omp_sections_scope (cp_parser
*parser
)
43108 tree stmt
, substmt
;
43109 bool error_suppress
= false;
43112 matching_braces braces
;
43113 if (!braces
.require_open (parser
))
43116 stmt
= push_stmt_list ();
43118 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
43119 != PRAGMA_OMP_SECTION
43120 && !cp_parser_omp_section_scan (parser
, "section", true))
43122 substmt
= cp_parser_omp_structured_block_sequence (parser
,
43123 PRAGMA_OMP_SECTION
);
43124 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
43125 add_stmt (substmt
);
43130 tok
= cp_lexer_peek_token (parser
->lexer
);
43131 if (tok
->type
== CPP_CLOSE_BRACE
)
43133 if (tok
->type
== CPP_EOF
)
43136 if (cp_parser_omp_section_scan (parser
, "section", false))
43137 tok
= cp_lexer_peek_token (parser
->lexer
);
43138 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
43140 cp_lexer_consume_token (parser
->lexer
);
43141 cp_parser_require_pragma_eol (parser
, tok
);
43142 error_suppress
= false;
43144 else if (!error_suppress
)
43146 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
43147 error_suppress
= true;
43150 substmt
= cp_parser_omp_structured_block_sequence (parser
,
43151 PRAGMA_OMP_SECTION
);
43152 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
43153 add_stmt (substmt
);
43155 braces
.require_close (parser
);
43157 substmt
= pop_stmt_list (stmt
);
43159 stmt
= make_node (OMP_SECTIONS
);
43160 TREE_TYPE (stmt
) = void_type_node
;
43161 OMP_SECTIONS_BODY (stmt
) = substmt
;
43168 # pragma omp sections sections-clause[optseq] newline
43171 #define OMP_SECTIONS_CLAUSE_MASK \
43172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43180 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
43181 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
43184 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43186 strcat (p_name
, " sections");
43187 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
43189 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
43191 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43195 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
43196 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
43199 ret
= cp_parser_omp_sections_scope (parser
);
43201 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
43207 # pragma omp parallel parallel-clause[optseq] new-line
43209 # pragma omp parallel for parallel-for-clause[optseq] new-line
43211 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
43215 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
43216 structured-block */
43218 #define OMP_PARALLEL_CLAUSE_MASK \
43219 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
43225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
43227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
43231 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
43232 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43235 tree stmt
, clauses
, block
;
43237 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43239 strcat (p_name
, " parallel");
43240 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
43241 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
43242 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
43243 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
43244 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
43246 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
43248 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43249 if (cclauses
== NULL
)
43250 cclauses
= cclauses_buf
;
43252 cp_lexer_consume_token (parser
->lexer
);
43253 if (!flag_openmp
) /* flag_openmp_simd */
43254 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
43256 block
= begin_omp_parallel ();
43257 save
= cp_parser_begin_omp_structured_block (parser
);
43258 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
43260 cp_parser_end_omp_structured_block (parser
, save
);
43261 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
43263 if (ret
== NULL_TREE
)
43265 OMP_PARALLEL_COMBINED (stmt
) = 1;
43268 /* When combined with distribute, parallel has to be followed by for.
43269 #pragma omp target parallel is allowed though. */
43271 && (mask
& (OMP_CLAUSE_MASK_1
43272 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
43274 error_at (loc
, "expected %<for%> after %qs", p_name
);
43275 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43278 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43280 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43281 const char *p
= IDENTIFIER_POINTER (id
);
43282 if (cclauses
== NULL
&& strcmp (p
, "masked") == 0)
43284 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43285 cclauses
= cclauses_buf
;
43287 cp_lexer_consume_token (parser
->lexer
);
43288 if (!flag_openmp
) /* flag_openmp_simd */
43289 return cp_parser_omp_masked (parser
, pragma_tok
, p_name
, mask
,
43291 block
= begin_omp_parallel ();
43292 save
= cp_parser_begin_omp_structured_block (parser
);
43293 tree ret
= cp_parser_omp_masked (parser
, pragma_tok
, p_name
, mask
,
43295 cp_parser_end_omp_structured_block (parser
, save
);
43296 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
43298 if (ret
== NULL_TREE
)
43300 /* masked does have just filter clause, but during gimplification
43301 isn't represented by a gimplification omp context, so for
43302 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
43304 #pragma omp parallel masked
43305 #pragma omp taskloop simd lastprivate (x)
43306 isn't confused with
43307 #pragma omp parallel masked taskloop simd lastprivate (x) */
43308 if (OMP_MASKED_COMBINED (ret
))
43309 OMP_PARALLEL_COMBINED (stmt
) = 1;
43312 else if (cclauses
== NULL
&& strcmp (p
, "master") == 0)
43314 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43315 cclauses
= cclauses_buf
;
43317 cp_lexer_consume_token (parser
->lexer
);
43318 if (!flag_openmp
) /* flag_openmp_simd */
43319 return cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
43321 block
= begin_omp_parallel ();
43322 save
= cp_parser_begin_omp_structured_block (parser
);
43323 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
43325 cp_parser_end_omp_structured_block (parser
, save
);
43326 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
43328 if (ret
== NULL_TREE
)
43330 /* master doesn't have any clauses and during gimplification
43331 isn't represented by a gimplification omp context, so for
43332 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
43334 #pragma omp parallel master
43335 #pragma omp taskloop simd lastprivate (x)
43336 isn't confused with
43337 #pragma omp parallel master taskloop simd lastprivate (x) */
43338 if (OMP_MASTER_COMBINED (ret
))
43339 OMP_PARALLEL_COMBINED (stmt
) = 1;
43342 else if (strcmp (p
, "loop") == 0)
43344 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43345 if (cclauses
== NULL
)
43346 cclauses
= cclauses_buf
;
43348 cp_lexer_consume_token (parser
->lexer
);
43349 if (!flag_openmp
) /* flag_openmp_simd */
43350 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
43352 block
= begin_omp_parallel ();
43353 save
= cp_parser_begin_omp_structured_block (parser
);
43354 tree ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
43356 cp_parser_end_omp_structured_block (parser
, save
);
43357 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
43359 if (ret
== NULL_TREE
)
43361 OMP_PARALLEL_COMBINED (stmt
) = 1;
43364 else if (!flag_openmp
) /* flag_openmp_simd */
43366 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43369 else if (cclauses
== NULL
&& strcmp (p
, "sections") == 0)
43371 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43372 cclauses
= cclauses_buf
;
43374 cp_lexer_consume_token (parser
->lexer
);
43375 block
= begin_omp_parallel ();
43376 save
= cp_parser_begin_omp_structured_block (parser
);
43377 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
43378 cp_parser_end_omp_structured_block (parser
, save
);
43379 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
43381 OMP_PARALLEL_COMBINED (stmt
) = 1;
43385 else if (!flag_openmp
) /* flag_openmp_simd */
43387 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43391 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43395 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
43396 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
43399 block
= begin_omp_parallel ();
43400 save
= cp_parser_begin_omp_structured_block (parser
);
43401 parser
->omp_attrs_forbidden_p
= true;
43402 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
43403 cp_parser_end_omp_structured_block (parser
, save
);
43404 stmt
= finish_omp_parallel (clauses
, block
);
43409 # pragma omp single single-clause[optseq] new-line
43410 structured-block */
43412 #define OMP_SINGLE_CLAUSE_MASK \
43413 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
43416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43420 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
43422 tree stmt
= make_node (OMP_SINGLE
);
43423 TREE_TYPE (stmt
) = void_type_node
;
43424 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
43426 OMP_SINGLE_CLAUSES (stmt
)
43427 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
43428 "#pragma omp single", pragma_tok
);
43429 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
43431 return add_stmt (stmt
);
43435 # pragma omp scope scope-clause[optseq] new-line
43436 structured-block */
43438 #define OMP_SCOPE_CLAUSE_MASK \
43439 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43444 cp_parser_omp_scope (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
43446 tree stmt
= make_node (OMP_SCOPE
);
43447 TREE_TYPE (stmt
) = void_type_node
;
43448 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
43450 OMP_SCOPE_CLAUSES (stmt
)
43451 = cp_parser_omp_all_clauses (parser
, OMP_SCOPE_CLAUSE_MASK
,
43452 "#pragma omp scope", pragma_tok
);
43453 OMP_SCOPE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
43455 return add_stmt (stmt
);
43459 # pragma omp task task-clause[optseq] new-line
43460 structured-block */
43462 #define OMP_TASK_CLAUSE_MASK \
43463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
43465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
43470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
43471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
43473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
43475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
43476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
43479 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
43481 tree clauses
, block
;
43484 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
43485 "#pragma omp task", pragma_tok
);
43486 block
= begin_omp_task ();
43487 save
= cp_parser_begin_omp_structured_block (parser
);
43488 parser
->omp_attrs_forbidden_p
= true;
43489 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
43490 cp_parser_end_omp_structured_block (parser
, save
);
43491 return finish_omp_task (clauses
, block
);
43495 # pragma omp taskwait new-line
43498 # pragma omp taskwait taskwait-clause[opt] new-line */
43500 #define OMP_TASKWAIT_CLAUSE_MASK \
43501 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
43504 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
43507 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
43508 "#pragma omp taskwait", pragma_tok
);
43512 tree stmt
= make_node (OMP_TASK
);
43513 TREE_TYPE (stmt
) = void_node
;
43514 OMP_TASK_CLAUSES (stmt
) = clauses
;
43515 OMP_TASK_BODY (stmt
) = NULL_TREE
;
43516 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
43520 finish_omp_taskwait ();
43524 # pragma omp taskyield new-line */
43527 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
43529 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43530 finish_omp_taskyield ();
43534 # pragma omp taskgroup new-line
43538 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
43540 #define OMP_TASKGROUP_CLAUSE_MASK \
43541 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
43545 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
43548 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
43549 "#pragma omp taskgroup", pragma_tok
);
43550 return c_finish_omp_taskgroup (input_location
,
43551 cp_parser_omp_structured_block (parser
,
43558 # pragma omp threadprivate (variable-list) */
43561 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
43565 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
43566 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43568 finish_omp_threadprivate (vars
);
43572 # pragma omp cancel cancel-clause[optseq] new-line */
43574 #define OMP_CANCEL_CLAUSE_MASK \
43575 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
43576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
43577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
43578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
43579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
43582 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
43584 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
43585 "#pragma omp cancel", pragma_tok
);
43586 finish_omp_cancel (clauses
);
43590 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
43592 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
43593 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
43594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
43595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
43596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
43599 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
43600 enum pragma_context context
)
43603 bool point_seen
= false;
43605 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43607 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43608 const char *p
= IDENTIFIER_POINTER (id
);
43610 if (strcmp (p
, "point") == 0)
43612 cp_lexer_consume_token (parser
->lexer
);
43618 cp_parser_error (parser
, "expected %<point%>");
43619 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43623 if (context
!= pragma_compound
)
43625 if (context
== pragma_stmt
)
43626 error_at (pragma_tok
->location
,
43627 "%<#pragma %s%> may only be used in compound statements",
43628 "omp cancellation point");
43630 cp_parser_error (parser
, "expected declaration specifiers");
43631 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43635 clauses
= cp_parser_omp_all_clauses (parser
,
43636 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
43637 "#pragma omp cancellation point",
43639 finish_omp_cancellation_point (clauses
);
43644 #pragma omp distribute distribute-clause[optseq] new-line
43647 #define OMP_DISTRIBUTE_CLAUSE_MASK \
43648 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
43652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43657 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
43658 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43661 tree clauses
, sb
, ret
;
43663 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43665 strcat (p_name
, " distribute");
43666 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
43668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43670 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43671 const char *p
= IDENTIFIER_POINTER (id
);
43673 bool parallel
= false;
43675 if (strcmp (p
, "simd") == 0)
43678 parallel
= strcmp (p
, "parallel") == 0;
43679 if (parallel
|| simd
)
43681 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43682 if (cclauses
== NULL
)
43683 cclauses
= cclauses_buf
;
43684 cp_lexer_consume_token (parser
->lexer
);
43685 if (!flag_openmp
) /* flag_openmp_simd */
43688 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
43691 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
43694 sb
= begin_omp_structured_block ();
43695 save
= cp_parser_begin_omp_structured_block (parser
);
43697 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
43700 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
43702 cp_parser_end_omp_structured_block (parser
, save
);
43703 tree body
= finish_omp_structured_block (sb
);
43706 ret
= make_node (OMP_DISTRIBUTE
);
43707 TREE_TYPE (ret
) = void_type_node
;
43708 OMP_FOR_BODY (ret
) = body
;
43709 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
43710 SET_EXPR_LOCATION (ret
, loc
);
43715 if (!flag_openmp
) /* flag_openmp_simd */
43717 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43721 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43725 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
43726 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
43729 keep_next_level (true);
43730 sb
= begin_omp_structured_block ();
43731 save
= cp_parser_begin_omp_structured_block (parser
);
43733 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
43735 cp_parser_end_omp_structured_block (parser
, save
);
43736 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
43742 # pragma omp teams teams-clause[optseq] new-line
43743 structured-block */
43745 #define OMP_TEAMS_CLAUSE_MASK \
43746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
43751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
43752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
43756 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
43757 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43760 tree clauses
, sb
, ret
;
43762 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43764 strcat (p_name
, " teams");
43765 mask
|= OMP_TEAMS_CLAUSE_MASK
;
43767 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43769 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43770 const char *p
= IDENTIFIER_POINTER (id
);
43771 if (strcmp (p
, "distribute") == 0)
43773 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43774 if (cclauses
== NULL
)
43775 cclauses
= cclauses_buf
;
43777 cp_lexer_consume_token (parser
->lexer
);
43778 if (!flag_openmp
) /* flag_openmp_simd */
43779 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
43781 keep_next_level (true);
43782 sb
= begin_omp_structured_block ();
43783 save
= cp_parser_begin_omp_structured_block (parser
);
43784 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
43786 cp_parser_end_omp_structured_block (parser
, save
);
43787 tree body
= finish_omp_structured_block (sb
);
43790 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
43791 ret
= make_node (OMP_TEAMS
);
43792 TREE_TYPE (ret
) = void_type_node
;
43793 OMP_TEAMS_CLAUSES (ret
) = clauses
;
43794 OMP_TEAMS_BODY (ret
) = body
;
43795 OMP_TEAMS_COMBINED (ret
) = 1;
43796 SET_EXPR_LOCATION (ret
, loc
);
43797 return add_stmt (ret
);
43799 else if (strcmp (p
, "loop") == 0)
43801 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43802 if (cclauses
== NULL
)
43803 cclauses
= cclauses_buf
;
43805 cp_lexer_consume_token (parser
->lexer
);
43806 if (!flag_openmp
) /* flag_openmp_simd */
43807 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
43809 keep_next_level (true);
43810 sb
= begin_omp_structured_block ();
43811 save
= cp_parser_begin_omp_structured_block (parser
);
43812 ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
43814 cp_parser_end_omp_structured_block (parser
, save
);
43815 tree body
= finish_omp_structured_block (sb
);
43818 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
43819 ret
= make_node (OMP_TEAMS
);
43820 TREE_TYPE (ret
) = void_type_node
;
43821 OMP_TEAMS_CLAUSES (ret
) = clauses
;
43822 OMP_TEAMS_BODY (ret
) = body
;
43823 OMP_TEAMS_COMBINED (ret
) = 1;
43824 SET_EXPR_LOCATION (ret
, loc
);
43825 return add_stmt (ret
);
43828 if (!flag_openmp
) /* flag_openmp_simd */
43830 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43834 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43838 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
43839 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
43842 tree stmt
= make_node (OMP_TEAMS
);
43843 TREE_TYPE (stmt
) = void_type_node
;
43844 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
43845 keep_next_level (true);
43846 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
43847 SET_EXPR_LOCATION (stmt
, loc
);
43849 return add_stmt (stmt
);
43853 # pragma omp target data target-data-clause[optseq] new-line
43854 structured-block */
43856 #define OMP_TARGET_DATA_CLAUSE_MASK \
43857 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
43858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
43859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
43861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
43864 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
43867 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
43868 "#pragma omp target data", pragma_tok
);
43869 c_omp_adjust_map_clauses (clauses
, false);
43871 for (tree
*pc
= &clauses
; *pc
;)
43873 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
43874 switch (OMP_CLAUSE_MAP_KIND (*pc
))
43877 case GOMP_MAP_ALWAYS_TO
:
43878 case GOMP_MAP_FROM
:
43879 case GOMP_MAP_ALWAYS_FROM
:
43880 case GOMP_MAP_TOFROM
:
43881 case GOMP_MAP_ALWAYS_TOFROM
:
43882 case GOMP_MAP_ALLOC
:
43885 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
43886 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
43887 case GOMP_MAP_ALWAYS_POINTER
:
43888 case GOMP_MAP_ATTACH_DETACH
:
43892 error_at (OMP_CLAUSE_LOCATION (*pc
),
43893 "%<#pragma omp target data%> with map-type other "
43894 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
43895 "on %<map%> clause");
43896 *pc
= OMP_CLAUSE_CHAIN (*pc
);
43899 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
43900 || OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_ADDR
)
43902 pc
= &OMP_CLAUSE_CHAIN (*pc
);
43908 error_at (pragma_tok
->location
,
43909 "%<#pragma omp target data%> must contain at least "
43910 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
43915 tree stmt
= make_node (OMP_TARGET_DATA
);
43916 TREE_TYPE (stmt
) = void_type_node
;
43917 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
43919 keep_next_level (true);
43920 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
43922 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
43923 return add_stmt (stmt
);
43927 # pragma omp target enter data target-enter-data-clause[optseq] new-line
43928 structured-block */
43930 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
43931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
43932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
43933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43938 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
43939 enum pragma_context context
)
43941 bool data_seen
= false;
43942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43944 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43945 const char *p
= IDENTIFIER_POINTER (id
);
43947 if (strcmp (p
, "data") == 0)
43949 cp_lexer_consume_token (parser
->lexer
);
43955 cp_parser_error (parser
, "expected %<data%>");
43956 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43960 if (context
== pragma_stmt
)
43962 error_at (pragma_tok
->location
,
43963 "%<#pragma %s%> may only be used in compound statements",
43964 "omp target enter data");
43965 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43970 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
43971 "#pragma omp target enter data", pragma_tok
);
43972 c_omp_adjust_map_clauses (clauses
, false);
43974 for (tree
*pc
= &clauses
; *pc
;)
43976 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
43977 switch (OMP_CLAUSE_MAP_KIND (*pc
))
43980 case GOMP_MAP_ALWAYS_TO
:
43981 case GOMP_MAP_ALLOC
:
43984 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
43985 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
43986 case GOMP_MAP_ALWAYS_POINTER
:
43987 case GOMP_MAP_ATTACH_DETACH
:
43991 error_at (OMP_CLAUSE_LOCATION (*pc
),
43992 "%<#pragma omp target enter data%> with map-type other "
43993 "than %<to%> or %<alloc%> on %<map%> clause");
43994 *pc
= OMP_CLAUSE_CHAIN (*pc
);
43997 pc
= &OMP_CLAUSE_CHAIN (*pc
);
44003 error_at (pragma_tok
->location
,
44004 "%<#pragma omp target enter data%> must contain at least "
44005 "one %<map%> clause");
44009 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
44010 TREE_TYPE (stmt
) = void_type_node
;
44011 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
44012 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44018 # pragma omp target exit data target-enter-data-clause[optseq] new-line
44019 structured-block */
44021 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
44022 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44029 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
44030 enum pragma_context context
)
44032 bool data_seen
= false;
44033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
44035 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
44036 const char *p
= IDENTIFIER_POINTER (id
);
44038 if (strcmp (p
, "data") == 0)
44040 cp_lexer_consume_token (parser
->lexer
);
44046 cp_parser_error (parser
, "expected %<data%>");
44047 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44051 if (context
== pragma_stmt
)
44053 error_at (pragma_tok
->location
,
44054 "%<#pragma %s%> may only be used in compound statements",
44055 "omp target exit data");
44056 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44061 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
44062 "#pragma omp target exit data", pragma_tok
);
44063 c_omp_adjust_map_clauses (clauses
, false);
44065 for (tree
*pc
= &clauses
; *pc
;)
44067 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
44068 switch (OMP_CLAUSE_MAP_KIND (*pc
))
44070 case GOMP_MAP_FROM
:
44071 case GOMP_MAP_ALWAYS_FROM
:
44072 case GOMP_MAP_RELEASE
:
44073 case GOMP_MAP_DELETE
:
44076 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
44077 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
44078 case GOMP_MAP_ALWAYS_POINTER
:
44079 case GOMP_MAP_ATTACH_DETACH
:
44083 error_at (OMP_CLAUSE_LOCATION (*pc
),
44084 "%<#pragma omp target exit data%> with map-type other "
44085 "than %<from%>, %<release%> or %<delete%> on %<map%>"
44087 *pc
= OMP_CLAUSE_CHAIN (*pc
);
44090 pc
= &OMP_CLAUSE_CHAIN (*pc
);
44096 error_at (pragma_tok
->location
,
44097 "%<#pragma omp target exit data%> must contain at least "
44098 "one %<map%> clause");
44102 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
44103 TREE_TYPE (stmt
) = void_type_node
;
44104 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
44105 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44111 # pragma omp target update target-update-clause[optseq] new-line */
44113 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
44114 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
44115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
44116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44122 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
44123 enum pragma_context context
)
44125 if (context
== pragma_stmt
)
44127 error_at (pragma_tok
->location
,
44128 "%<#pragma %s%> may only be used in compound statements",
44129 "omp target update");
44130 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44135 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
44136 "#pragma omp target update", pragma_tok
);
44137 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
44138 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
44140 error_at (pragma_tok
->location
,
44141 "%<#pragma omp target update%> must contain at least one "
44142 "%<from%> or %<to%> clauses");
44146 tree stmt
= make_node (OMP_TARGET_UPDATE
);
44147 TREE_TYPE (stmt
) = void_type_node
;
44148 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
44149 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44155 # pragma omp target target-clause[optseq] new-line
44156 structured-block */
44158 #define OMP_TARGET_CLAUSE_MASK \
44159 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
44164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
44167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
44170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
44173 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
44174 enum pragma_context context
, bool *if_p
)
44178 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
44180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
44182 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
44183 const char *p
= IDENTIFIER_POINTER (id
);
44184 enum tree_code ccode
= ERROR_MARK
;
44186 if (strcmp (p
, "teams") == 0)
44188 else if (strcmp (p
, "parallel") == 0)
44189 ccode
= OMP_PARALLEL
;
44190 else if (strcmp (p
, "simd") == 0)
44192 if (ccode
!= ERROR_MARK
)
44194 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
44195 char p_name
[sizeof ("#pragma omp target teams distribute "
44196 "parallel for simd")];
44198 cp_lexer_consume_token (parser
->lexer
);
44199 strcpy (p_name
, "#pragma omp target");
44200 if (!flag_openmp
) /* flag_openmp_simd */
44206 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
44207 OMP_TARGET_CLAUSE_MASK
,
44211 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
44212 OMP_TARGET_CLAUSE_MASK
,
44216 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
44217 OMP_TARGET_CLAUSE_MASK
,
44221 gcc_unreachable ();
44223 return stmt
!= NULL_TREE
;
44225 keep_next_level (true);
44226 tree sb
= begin_omp_structured_block (), ret
;
44227 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
44231 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
44232 OMP_TARGET_CLAUSE_MASK
, cclauses
,
44236 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
44237 OMP_TARGET_CLAUSE_MASK
, cclauses
,
44241 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
44242 OMP_TARGET_CLAUSE_MASK
, cclauses
,
44246 gcc_unreachable ();
44248 cp_parser_end_omp_structured_block (parser
, save
);
44249 tree body
= finish_omp_structured_block (sb
);
44250 if (ret
== NULL_TREE
)
44252 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
44253 /* For combined target teams, ensure the num_teams and
44254 thread_limit clause expressions are evaluated on the host,
44255 before entering the target construct. */
44256 for (tree c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
44257 c
; c
= OMP_CLAUSE_CHAIN (c
))
44258 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
44259 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
44261 i
<= (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
); ++i
)
44262 if (OMP_CLAUSE_OPERAND (c
, i
)
44263 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, i
)) != INTEGER_CST
)
44265 tree expr
= OMP_CLAUSE_OPERAND (c
, i
);
44266 expr
= force_target_expr (TREE_TYPE (expr
), expr
,
44268 if (expr
== error_mark_node
)
44270 tree tmp
= TARGET_EXPR_SLOT (expr
);
44272 OMP_CLAUSE_OPERAND (c
, i
) = expr
;
44273 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
44274 OMP_CLAUSE_FIRSTPRIVATE
);
44275 OMP_CLAUSE_DECL (tc
) = tmp
;
44276 OMP_CLAUSE_CHAIN (tc
)
44277 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
44278 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
44280 c_omp_adjust_map_clauses (cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
], true);
44281 finish_omp_target (pragma_tok
->location
,
44282 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
], body
, true);
44285 else if (!flag_openmp
) /* flag_openmp_simd */
44287 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44290 else if (strcmp (p
, "data") == 0)
44292 cp_lexer_consume_token (parser
->lexer
);
44293 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
44296 else if (strcmp (p
, "enter") == 0)
44298 cp_lexer_consume_token (parser
->lexer
);
44299 return cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
44301 else if (strcmp (p
, "exit") == 0)
44303 cp_lexer_consume_token (parser
->lexer
);
44304 return cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
44306 else if (strcmp (p
, "update") == 0)
44308 cp_lexer_consume_token (parser
->lexer
);
44309 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
44312 if (!flag_openmp
) /* flag_openmp_simd */
44314 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44318 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
44319 "#pragma omp target", pragma_tok
,
44321 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
44322 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
44324 tree nc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
44325 OMP_CLAUSE_DECL (nc
) = OMP_CLAUSE_DECL (c
);
44326 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_ALWAYS_TOFROM
);
44327 OMP_CLAUSE_CHAIN (nc
) = OMP_CLAUSE_CHAIN (c
);
44328 OMP_CLAUSE_CHAIN (c
) = nc
;
44330 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_TARGET
);
44332 c_omp_adjust_map_clauses (clauses
, true);
44333 keep_next_level (true);
44334 tree body
= cp_parser_omp_structured_block (parser
, if_p
);
44336 finish_omp_target (pragma_tok
->location
, clauses
, body
, false);
44341 # pragma acc cache (variable-list) new-line
44345 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
44347 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
44349 auto_suppress_location_wrappers sentinel
;
44351 tree stmt
, clauses
;
44353 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
44354 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
44356 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
44358 stmt
= make_node (OACC_CACHE
);
44359 TREE_TYPE (stmt
) = void_type_node
;
44360 OACC_CACHE_CLAUSES (stmt
) = clauses
;
44361 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44368 # pragma acc data oacc-data-clause[optseq] new-line
44369 structured-block */
44371 #define OACC_DATA_CLAUSE_MASK \
44372 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
44378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44384 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
44386 tree stmt
, clauses
, block
;
44389 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
44390 "#pragma acc data", pragma_tok
);
44392 block
= begin_omp_parallel ();
44393 save
= cp_parser_begin_omp_structured_block (parser
);
44394 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
44395 cp_parser_end_omp_structured_block (parser
, save
);
44396 stmt
= finish_oacc_data (clauses
, block
);
44401 # pragma acc host_data <clauses> new-line
44402 structured-block */
44404 #define OACC_HOST_DATA_CLAUSE_MASK \
44405 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
44406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
44410 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
44412 tree stmt
, clauses
, block
;
44415 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
44416 "#pragma acc host_data", pragma_tok
);
44418 block
= begin_omp_parallel ();
44419 save
= cp_parser_begin_omp_structured_block (parser
);
44420 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
44421 cp_parser_end_omp_structured_block (parser
, save
);
44422 stmt
= finish_oacc_host_data (clauses
, block
);
44427 # pragma acc declare oacc-data-clause[optseq] new-line
44430 #define OACC_DECLARE_CLAUSE_MASK \
44431 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
44437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
44438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44441 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
44443 tree clauses
, stmt
;
44444 bool error
= false;
44445 bool found_in_scope
= global_bindings_p ();
44447 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
44448 "#pragma acc declare", pragma_tok
, true);
44451 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
44453 error_at (pragma_tok
->location
,
44454 "no valid clauses specified in %<#pragma acc declare%>");
44458 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
44460 location_t loc
= OMP_CLAUSE_LOCATION (t
);
44461 tree decl
= OMP_CLAUSE_DECL (t
);
44462 if (!DECL_P (decl
))
44464 error_at (loc
, "array section in %<#pragma acc declare%>");
44468 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
44469 switch (OMP_CLAUSE_MAP_KIND (t
))
44471 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
44472 case GOMP_MAP_ALLOC
:
44474 case GOMP_MAP_FORCE_DEVICEPTR
:
44475 case GOMP_MAP_DEVICE_RESIDENT
:
44478 case GOMP_MAP_LINK
:
44479 if (!global_bindings_p ()
44480 && (TREE_STATIC (decl
)
44481 || !DECL_EXTERNAL (decl
)))
44484 "%qD must be a global variable in "
44485 "%<#pragma acc declare link%>",
44493 if (global_bindings_p ())
44495 error_at (loc
, "invalid OpenACC clause at file scope");
44499 if (DECL_EXTERNAL (decl
))
44502 "invalid use of %<extern%> variable %qD "
44503 "in %<#pragma acc declare%>", decl
);
44507 else if (TREE_PUBLIC (decl
))
44510 "invalid use of %<global%> variable %qD "
44511 "in %<#pragma acc declare%>", decl
);
44518 if (!found_in_scope
)
44519 /* This seems to ignore the existence of cleanup scopes?
44520 What is the meaning for local extern decls? The local
44521 extern is in this scope, but it is referring to a decl that
44522 is namespace scope. */
44523 for (tree d
= current_binding_level
->names
; d
; d
= TREE_CHAIN (d
))
44526 found_in_scope
= true;
44529 if (!found_in_scope
)
44532 "%qD must be a variable declared in the same scope as "
44533 "%<#pragma acc declare%>", decl
);
44538 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
44539 || lookup_attribute ("omp declare target link",
44540 DECL_ATTRIBUTES (decl
)))
44542 error_at (loc
, "variable %qD used more than once with "
44543 "%<#pragma acc declare%>", decl
);
44552 if (DECL_LOCAL_DECL_P (decl
))
44553 /* We need to mark the aliased decl, as that is the entity
44554 that is being referred to. This won't work for
44555 dependent variables, but it didn't work for them before
44556 DECL_LOCAL_DECL_P was a thing either. But then
44557 dependent local extern variable decls are as rare as
44559 if (auto alias
= DECL_LOCAL_DECL_ALIAS (decl
))
44560 if (alias
!= error_mark_node
)
44563 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
44564 id
= get_identifier ("omp declare target link");
44566 id
= get_identifier ("omp declare target");
44568 DECL_ATTRIBUTES (decl
)
44569 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
44570 if (current_binding_level
->kind
== sk_namespace
)
44572 symtab_node
*node
= symtab_node::get (decl
);
44575 node
->offloadable
= 1;
44576 if (ENABLE_OFFLOADING
)
44578 g
->have_offload
= true;
44579 if (is_a
<varpool_node
*> (node
))
44580 vec_safe_push (offload_vars
, decl
);
44587 if (error
|| current_binding_level
->kind
== sk_namespace
)
44590 stmt
= make_node (OACC_DECLARE
);
44591 TREE_TYPE (stmt
) = void_type_node
;
44592 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
44593 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44601 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
44605 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
44607 LOC is the location of the #pragma token.
44610 #define OACC_ENTER_DATA_CLAUSE_MASK \
44611 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44618 #define OACC_EXIT_DATA_CLAUSE_MASK \
44619 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
44623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
44624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
44625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44628 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
44631 location_t loc
= pragma_tok
->location
;
44632 tree stmt
, clauses
;
44633 const char *p
= "";
44635 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
44636 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
44638 if (strcmp (p
, "data") != 0)
44640 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
44641 enter
? "enter" : "exit");
44642 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44646 cp_lexer_consume_token (parser
->lexer
);
44649 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
44650 "#pragma acc enter data", pragma_tok
);
44652 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
44653 "#pragma acc exit data", pragma_tok
);
44655 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
44657 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
44658 enter
? "enter" : "exit");
44662 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
44663 TREE_TYPE (stmt
) = void_type_node
;
44664 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
44665 SET_EXPR_LOCATION (stmt
, loc
);
44671 # pragma acc loop oacc-loop-clause[optseq] new-line
44672 structured-block */
44674 #define OACC_LOOP_CLAUSE_MASK \
44675 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
44676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
44677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
44678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
44679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
44680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
44681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
44682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
44683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
44684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
44687 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
44688 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
44690 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
44692 strcat (p_name
, " loop");
44693 mask
|= OACC_LOOP_CLAUSE_MASK
;
44695 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
44699 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
44701 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
44703 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
44706 tree block
= begin_omp_structured_block ();
44707 int save
= cp_parser_begin_omp_structured_block (parser
);
44708 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
44709 cp_parser_end_omp_structured_block (parser
, save
);
44710 add_stmt (finish_omp_structured_block (block
));
44716 # pragma acc kernels oacc-kernels-clause[optseq] new-line
44721 # pragma acc parallel oacc-parallel-clause[optseq] new-line
44726 # pragma acc serial oacc-serial-clause[optseq] new-line
44729 #define OACC_KERNELS_CLAUSE_MASK \
44730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
44737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
44741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
44742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
44743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
44744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44746 #define OACC_PARALLEL_CLAUSE_MASK \
44747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
44754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
44756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
44759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
44760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
44761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
44762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
44763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
44764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44766 #define OACC_SERIAL_CLAUSE_MASK \
44767 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
44774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
44778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
44779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
44780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
44781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44784 cp_parser_oacc_compute (cp_parser
*parser
, cp_token
*pragma_tok
,
44785 char *p_name
, bool *if_p
)
44787 omp_clause_mask mask
;
44788 enum tree_code code
;
44789 switch (cp_parser_pragma_kind (pragma_tok
))
44791 case PRAGMA_OACC_KERNELS
:
44792 strcat (p_name
, " kernels");
44793 mask
= OACC_KERNELS_CLAUSE_MASK
;
44794 code
= OACC_KERNELS
;
44796 case PRAGMA_OACC_PARALLEL
:
44797 strcat (p_name
, " parallel");
44798 mask
= OACC_PARALLEL_CLAUSE_MASK
;
44799 code
= OACC_PARALLEL
;
44801 case PRAGMA_OACC_SERIAL
:
44802 strcat (p_name
, " serial");
44803 mask
= OACC_SERIAL_CLAUSE_MASK
;
44804 code
= OACC_SERIAL
;
44807 gcc_unreachable ();
44810 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
44813 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
44814 if (strcmp (p
, "loop") == 0)
44816 cp_lexer_consume_token (parser
->lexer
);
44817 tree block
= begin_omp_parallel ();
44819 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
44821 protected_set_expr_location (stmt
, pragma_tok
->location
);
44822 return finish_omp_construct (code
, block
, clauses
);
44826 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
44828 tree block
= begin_omp_parallel ();
44829 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
44830 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
44831 cp_parser_end_omp_structured_block (parser
, save
);
44832 return finish_omp_construct (code
, block
, clauses
);
44836 # pragma acc update oacc-update-clause[optseq] new-line
44839 #define OACC_UPDATE_CLAUSE_MASK \
44840 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
44841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
44842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
44843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
44845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
44848 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
44850 tree stmt
, clauses
;
44852 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
44853 "#pragma acc update", pragma_tok
);
44855 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
44857 error_at (pragma_tok
->location
,
44858 "%<#pragma acc update%> must contain at least one "
44859 "%<device%> or %<host%> or %<self%> clause");
44863 stmt
= make_node (OACC_UPDATE
);
44864 TREE_TYPE (stmt
) = void_type_node
;
44865 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
44866 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
44872 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
44874 LOC is the location of the #pragma token.
44877 #define OACC_WAIT_CLAUSE_MASK \
44878 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
44881 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
44883 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
44884 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
44886 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
44887 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
44889 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
44890 "#pragma acc wait", pragma_tok
);
44892 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
44893 stmt
= finish_expr_stmt (stmt
);
44899 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
44901 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
44902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
44903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
44905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
44906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
44907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
44910 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
44911 enum pragma_context context
,
44914 bool first_p
= parser
->omp_declare_simd
== NULL
;
44915 cp_omp_declare_simd_data data
;
44918 data
.error_seen
= false;
44919 data
.fndecl_seen
= false;
44920 data
.variant_p
= variant_p
;
44921 data
.tokens
= vNULL
;
44922 data
.attribs
[0] = NULL
;
44923 data
.attribs
[1] = NULL
;
44924 data
.loc
= UNKNOWN_LOCATION
;
44925 /* It is safe to take the address of a local variable; it will only be
44926 used while this scope is live. */
44927 parser
->omp_declare_simd
= &data
;
44929 else if (parser
->omp_declare_simd
->variant_p
!= variant_p
)
44931 error_at (pragma_tok
->location
,
44932 "%<#pragma omp declare %s%> followed by "
44933 "%<#pragma omp declare %s%>",
44934 parser
->omp_declare_simd
->variant_p
? "variant" : "simd",
44935 parser
->omp_declare_simd
->variant_p
? "simd" : "variant");
44936 parser
->omp_declare_simd
->error_seen
= true;
44939 /* Store away all pragma tokens. */
44940 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
44941 cp_lexer_consume_token (parser
->lexer
);
44942 cp_parser_require_pragma_eol (parser
, pragma_tok
);
44943 struct cp_token_cache
*cp
44944 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
44945 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
44949 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
44950 cp_parser_pragma (parser
, context
, NULL
);
44953 case pragma_external
:
44954 cp_parser_declaration (parser
, NULL_TREE
);
44956 case pragma_member
:
44957 cp_parser_member_declaration (parser
);
44959 case pragma_objc_icode
:
44960 cp_parser_block_declaration (parser
, /*statement_p=*/false);
44963 cp_parser_declaration_statement (parser
);
44966 if (parser
->omp_declare_simd
44967 && !parser
->omp_declare_simd
->error_seen
44968 && !parser
->omp_declare_simd
->fndecl_seen
)
44969 error_at (pragma_tok
->location
,
44970 "%<#pragma omp declare %s%> not immediately followed by "
44971 "function declaration or definition",
44972 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
44973 data
.tokens
.release ();
44974 parser
->omp_declare_simd
= NULL
;
44978 static const char *const omp_construct_selectors
[] = {
44979 "simd", "target", "teams", "parallel", "for", NULL
};
44980 static const char *const omp_device_selectors
[] = {
44981 "kind", "isa", "arch", NULL
};
44982 static const char *const omp_implementation_selectors
[] = {
44983 "vendor", "extension", "atomic_default_mem_order", "unified_address",
44984 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL
};
44985 static const char *const omp_user_selectors
[] = {
44986 "condition", NULL
};
44991 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
44994 score(score-expression) */
44997 cp_parser_omp_context_selector (cp_parser
*parser
, tree set
, bool has_parms_p
)
44999 tree ret
= NULL_TREE
;
45003 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
45004 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45005 selector
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45008 cp_parser_error (parser
, "expected trait selector name");
45009 return error_mark_node
;
45012 tree properties
= NULL_TREE
;
45013 const char *const *selectors
= NULL
;
45014 bool allow_score
= true;
45015 bool allow_user
= false;
45016 int property_limit
= 0;
45017 enum { CTX_PROPERTY_NONE
, CTX_PROPERTY_USER
, CTX_PROPERTY_NAME_LIST
,
45018 CTX_PROPERTY_ID
, CTX_PROPERTY_EXPR
,
45019 CTX_PROPERTY_SIMD
} property_kind
= CTX_PROPERTY_NONE
;
45020 switch (IDENTIFIER_POINTER (set
)[0])
45022 case 'c': /* construct */
45023 selectors
= omp_construct_selectors
;
45024 allow_score
= false;
45025 property_limit
= 1;
45026 property_kind
= CTX_PROPERTY_SIMD
;
45028 case 'd': /* device */
45029 selectors
= omp_device_selectors
;
45030 allow_score
= false;
45032 property_limit
= 3;
45033 property_kind
= CTX_PROPERTY_NAME_LIST
;
45035 case 'i': /* implementation */
45036 selectors
= omp_implementation_selectors
;
45038 property_limit
= 3;
45039 property_kind
= CTX_PROPERTY_NAME_LIST
;
45041 case 'u': /* user */
45042 selectors
= omp_user_selectors
;
45043 property_limit
= 1;
45044 property_kind
= CTX_PROPERTY_EXPR
;
45047 gcc_unreachable ();
45049 for (int i
= 0; ; i
++)
45051 if (selectors
[i
] == NULL
)
45055 property_kind
= CTX_PROPERTY_USER
;
45060 error ("selector %qs not allowed for context selector "
45061 "set %qs", IDENTIFIER_POINTER (selector
),
45062 IDENTIFIER_POINTER (set
));
45063 cp_lexer_consume_token (parser
->lexer
);
45064 return error_mark_node
;
45067 if (i
== property_limit
)
45068 property_kind
= CTX_PROPERTY_NONE
;
45069 if (strcmp (selectors
[i
], IDENTIFIER_POINTER (selector
)) == 0)
45072 if (property_kind
== CTX_PROPERTY_NAME_LIST
45073 && IDENTIFIER_POINTER (set
)[0] == 'i'
45074 && strcmp (IDENTIFIER_POINTER (selector
),
45075 "atomic_default_mem_order") == 0)
45076 property_kind
= CTX_PROPERTY_ID
;
45078 cp_lexer_consume_token (parser
->lexer
);
45080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
45082 if (property_kind
== CTX_PROPERTY_NONE
)
45084 error ("selector %qs does not accept any properties",
45085 IDENTIFIER_POINTER (selector
));
45086 return error_mark_node
;
45089 matching_parens parens
;
45090 parens
.consume_open (parser
);
45092 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
45094 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
45095 && strcmp (IDENTIFIER_POINTER (token
->u
.value
), "score") == 0
45096 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
45098 cp_lexer_save_tokens (parser
->lexer
);
45099 cp_lexer_consume_token (parser
->lexer
);
45100 cp_lexer_consume_token (parser
->lexer
);
45101 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
45103 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
45105 cp_lexer_rollback_tokens (parser
->lexer
);
45106 cp_lexer_consume_token (parser
->lexer
);
45108 matching_parens parens2
;
45109 parens2
.require_open (parser
);
45110 tree score
= cp_parser_constant_expression (parser
);
45111 if (!parens2
.require_close (parser
))
45112 cp_parser_skip_to_closing_parenthesis (parser
, true,
45114 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
45115 if (score
!= error_mark_node
)
45117 score
= fold_non_dependent_expr (score
);
45118 if (value_dependent_expression_p (score
))
45119 properties
= tree_cons (get_identifier (" score"),
45120 score
, properties
);
45121 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score
))
45122 || TREE_CODE (score
) != INTEGER_CST
)
45123 error_at (token
->location
, "score argument must be "
45124 "constant integer expression");
45125 else if (tree_int_cst_sgn (score
) < 0)
45126 error_at (token
->location
, "score argument must be "
45129 properties
= tree_cons (get_identifier (" score"),
45130 score
, properties
);
45134 cp_lexer_rollback_tokens (parser
->lexer
);
45136 token
= cp_lexer_peek_token (parser
->lexer
);
45139 switch (property_kind
)
45142 case CTX_PROPERTY_USER
:
45145 t
= cp_parser_constant_expression (parser
);
45146 if (t
!= error_mark_node
)
45148 t
= fold_non_dependent_expr (t
);
45149 if (TREE_CODE (t
) == STRING_CST
)
45150 properties
= tree_cons (NULL_TREE
, t
, properties
);
45151 else if (!value_dependent_expression_p (t
)
45152 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
45153 || !tree_fits_shwi_p (t
)))
45154 error_at (token
->location
, "property must be "
45155 "constant integer expression or string "
45158 properties
= tree_cons (NULL_TREE
, t
, properties
);
45161 return error_mark_node
;
45163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
45164 cp_lexer_consume_token (parser
->lexer
);
45170 case CTX_PROPERTY_ID
:
45171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
45172 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45174 tree prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45175 cp_lexer_consume_token (parser
->lexer
);
45176 properties
= tree_cons (prop
, NULL_TREE
, properties
);
45180 cp_parser_error (parser
, "expected identifier");
45181 return error_mark_node
;
45184 case CTX_PROPERTY_NAME_LIST
:
45187 tree prop
= NULL_TREE
, value
= NULL_TREE
;
45188 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
45189 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45191 prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45192 cp_lexer_consume_token (parser
->lexer
);
45194 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_STRING
))
45195 value
= cp_parser_string_literal (parser
, false, false);
45198 cp_parser_error (parser
, "expected identifier or "
45200 return error_mark_node
;
45203 properties
= tree_cons (prop
, value
, properties
);
45205 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
45206 cp_lexer_consume_token (parser
->lexer
);
45212 case CTX_PROPERTY_EXPR
:
45213 t
= cp_parser_constant_expression (parser
);
45214 if (t
!= error_mark_node
)
45216 t
= fold_non_dependent_expr (t
);
45217 if (!value_dependent_expression_p (t
)
45218 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
45219 || !tree_fits_shwi_p (t
)))
45220 error_at (token
->location
, "property must be "
45221 "constant integer expression");
45223 properties
= tree_cons (NULL_TREE
, t
, properties
);
45226 return error_mark_node
;
45228 case CTX_PROPERTY_SIMD
:
45231 error_at (token
->location
, "properties for %<simd%> "
45232 "selector may not be specified in "
45233 "%<metadirective%>");
45234 return error_mark_node
;
45237 = cp_parser_omp_all_clauses (parser
,
45238 OMP_DECLARE_SIMD_CLAUSE_MASK
,
45239 "simd", NULL
, true, 2);
45242 gcc_unreachable ();
45245 if (!parens
.require_close (parser
))
45246 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
45248 properties
= nreverse (properties
);
45250 else if (property_kind
== CTX_PROPERTY_NAME_LIST
45251 || property_kind
== CTX_PROPERTY_ID
45252 || property_kind
== CTX_PROPERTY_EXPR
)
45254 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
45255 return error_mark_node
;
45258 ret
= tree_cons (selector
, properties
, ret
);
45260 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
45261 cp_lexer_consume_token (parser
->lexer
);
45267 return nreverse (ret
);
45272 trait-set-selector[,trait-set-selector[,...]]
45274 trait-set-selector:
45275 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
45277 trait-set-selector-name:
45284 cp_parser_omp_context_selector_specification (cp_parser
*parser
,
45287 tree ret
= NULL_TREE
;
45290 const char *setp
= "";
45291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45293 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
45297 if (strcmp (setp
, "construct") == 0)
45301 if (strcmp (setp
, "device") == 0)
45305 if (strcmp (setp
, "implementation") == 0)
45309 if (strcmp (setp
, "user") == 0)
45317 cp_parser_error (parser
, "expected %<construct%>, %<device%>, "
45318 "%<implementation%> or %<user%>");
45319 return error_mark_node
;
45322 tree set
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45323 cp_lexer_consume_token (parser
->lexer
);
45325 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
45326 return error_mark_node
;
45328 matching_braces braces
;
45329 if (!braces
.require_open (parser
))
45330 return error_mark_node
;
45333 = cp_parser_omp_context_selector (parser
, set
, has_parms_p
);
45334 if (selectors
== error_mark_node
)
45336 cp_parser_skip_to_closing_brace (parser
);
45337 ret
= error_mark_node
;
45339 else if (ret
!= error_mark_node
)
45340 ret
= tree_cons (set
, selectors
, ret
);
45342 braces
.require_close (parser
);
45344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
45345 cp_lexer_consume_token (parser
->lexer
);
45351 if (ret
== error_mark_node
)
45353 return nreverse (ret
);
45356 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
45357 that into "omp declare variant base" attribute. */
45360 cp_finish_omp_declare_variant (cp_parser
*parser
, cp_token
*pragma_tok
,
45363 matching_parens parens
;
45364 if (!parens
.require_open (parser
))
45367 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
45372 cp_id_kind idk
= CP_ID_KIND_NONE
;
45373 cp_token
*varid_token
= cp_lexer_peek_token (parser
->lexer
);
45375 = cp_parser_id_expression (parser
, /*template_keyword_p=*/false,
45376 /*check_dependency_p=*/true,
45377 /*template_p=*/&template_p
,
45378 /*declarator_p=*/false,
45379 /*optional_p=*/false);
45380 parens
.require_close (parser
);
45383 if (TREE_CODE (varid
) == TEMPLATE_ID_EXPR
45384 || TREE_CODE (varid
) == TYPE_DECL
45385 || varid
== error_mark_node
)
45387 else if (varid_token
->type
== CPP_NAME
&& varid_token
->error_reported
)
45388 variant
= NULL_TREE
;
45391 tree ambiguous_decls
;
45392 variant
= cp_parser_lookup_name (parser
, varid
, none_type
,
45393 template_p
, /*is_namespace=*/false,
45394 /*check_dependency=*/true,
45396 varid
.get_location ());
45397 if (ambiguous_decls
)
45398 variant
= NULL_TREE
;
45400 if (variant
== NULL_TREE
)
45401 variant
= error_mark_node
;
45402 else if (TREE_CODE (variant
) != SCOPE_REF
)
45404 const char *error_msg
;
45406 = finish_id_expression (varid
, variant
, parser
->scope
,
45408 &parser
->non_integral_constant_expression_p
,
45409 template_p
, true, false, false, &error_msg
,
45410 varid
.get_location ());
45412 cp_parser_error (parser
, error_msg
);
45414 location_t caret_loc
= get_pure_location (varid
.get_location ());
45415 location_t start_loc
= get_start (varid_token
->location
);
45416 location_t finish_loc
= get_finish (varid
.get_location ());
45417 location_t varid_loc
= make_location (caret_loc
, start_loc
, finish_loc
);
45419 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45420 if (parser
->lexer
->in_omp_attribute_pragma
45421 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
45422 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
45423 cp_lexer_consume_token (parser
->lexer
);
45425 const char *clause
= "";
45426 location_t match_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
45427 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45428 clause
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
45429 if (strcmp (clause
, "match"))
45431 cp_parser_error (parser
, "expected %<match%>");
45435 cp_lexer_consume_token (parser
->lexer
);
45437 if (!parens
.require_open (parser
))
45440 tree ctx
= cp_parser_omp_context_selector_specification (parser
, true);
45441 if (ctx
== error_mark_node
)
45443 ctx
= omp_check_context_selector (match_loc
, ctx
);
45444 if (ctx
!= error_mark_node
&& variant
!= error_mark_node
)
45446 tree match_loc_node
= maybe_wrap_with_location (integer_zero_node
,
45448 tree loc_node
= maybe_wrap_with_location (integer_zero_node
, varid_loc
);
45449 loc_node
= tree_cons (match_loc_node
,
45450 build_int_cst (integer_type_node
, idk
),
45451 build_tree_list (loc_node
, integer_zero_node
));
45452 attrs
= tree_cons (get_identifier ("omp declare variant base"),
45453 tree_cons (variant
, ctx
, loc_node
), attrs
);
45454 if (processing_template_decl
)
45455 ATTR_IS_DEPENDENT (attrs
) = 1;
45458 parens
.require_close (parser
);
45459 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
45464 /* Finalize #pragma omp declare simd clauses after direct declarator has
45465 been parsed, and put that into "omp declare simd" attribute. */
45468 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
45470 struct cp_token_cache
*ce
;
45471 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
45474 if (!data
->error_seen
&& data
->fndecl_seen
)
45476 error ("%<#pragma omp declare %s%> not immediately followed by "
45477 "a single function declaration or definition",
45478 data
->variant_p
? "variant" : "simd");
45479 data
->error_seen
= true;
45481 if (data
->error_seen
)
45484 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
45488 cp_parser_push_lexer_for_tokens (parser
, ce
);
45489 parser
->lexer
->in_pragma
= true;
45490 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
45491 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
45492 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45493 const char *kind
= IDENTIFIER_POINTER (id
);
45494 cp_lexer_consume_token (parser
->lexer
);
45495 if (strcmp (kind
, "simd") == 0)
45497 /* For now only in C++ attributes, do it always for OpenMP 5.1.
45498 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45499 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45500 cp_lexer_consume_token (parser->lexer); */
45502 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
45503 "#pragma omp declare simd",
45506 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
45507 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
45508 TREE_CHAIN (c
) = attrs
;
45509 if (processing_template_decl
)
45510 ATTR_IS_DEPENDENT (c
) = 1;
45515 gcc_assert (strcmp (kind
, "variant") == 0);
45517 = cp_finish_omp_declare_variant (parser
, pragma_tok
, attrs
);
45519 cp_parser_pop_lexer (parser
);
45522 cp_lexer
*lexer
= NULL
;
45523 for (int i
= 0; i
< 2; i
++)
45525 if (data
->attribs
[i
] == NULL
)
45527 for (tree
*pa
= data
->attribs
[i
]; *pa
; )
45528 if (get_attribute_namespace (*pa
) == omp_identifier
45529 && is_attribute_p ("directive", get_attribute_name (*pa
)))
45531 for (tree a
= TREE_VALUE (*pa
); a
; a
= TREE_CHAIN (a
))
45533 tree d
= TREE_VALUE (a
);
45534 gcc_assert (TREE_CODE (d
) == DEFERRED_PARSE
);
45535 cp_token
*first
= DEFPARSE_TOKENS (d
)->first
;
45536 cp_token
*last
= DEFPARSE_TOKENS (d
)->last
;
45537 const char *directive
[3] = {};
45538 for (int j
= 0; j
< 3; j
++)
45540 tree id
= NULL_TREE
;
45541 if (first
+ j
== last
)
45543 if (first
[j
].type
== CPP_NAME
)
45544 id
= first
[j
].u
.value
;
45545 else if (first
[j
].type
== CPP_KEYWORD
)
45546 id
= ridpointers
[(int) first
[j
].keyword
];
45549 directive
[j
] = IDENTIFIER_POINTER (id
);
45551 const c_omp_directive
*dir
= NULL
;
45553 dir
= c_omp_categorize_directive (directive
[0], directive
[1],
45557 error_at (first
->location
,
45558 "unknown OpenMP directive name in "
45559 "%<omp::directive%> attribute argument");
45562 if (dir
->id
!= PRAGMA_OMP_DECLARE
45563 || (strcmp (directive
[1], "simd") != 0
45564 && strcmp (directive
[1], "variant") != 0))
45566 error_at (first
->location
,
45567 "OpenMP directive other than %<declare simd%> "
45568 "or %<declare variant%> appertains to a "
45573 if (parser
->omp_attrs_forbidden_p
)
45575 error_at (first
->location
,
45576 "mixing OpenMP directives with attribute and "
45577 "pragma syntax on the same statement");
45578 parser
->omp_attrs_forbidden_p
= false;
45581 if (!flag_openmp
&& strcmp (directive
[1], "simd") != 0)
45585 lexer
= cp_lexer_alloc ();
45586 lexer
->debugging_p
= parser
->lexer
->debugging_p
;
45588 vec_safe_reserve (lexer
->buffer
, (last
- first
) + 2);
45590 tok
.type
= CPP_PRAGMA
;
45591 tok
.keyword
= RID_MAX
;
45592 tok
.u
.value
= build_int_cst (NULL
, PRAGMA_OMP_DECLARE
);
45593 tok
.location
= first
->location
;
45594 lexer
->buffer
->quick_push (tok
);
45595 while (++first
< last
)
45596 lexer
->buffer
->quick_push (*first
);
45598 tok
.type
= CPP_PRAGMA_EOL
;
45599 tok
.keyword
= RID_MAX
;
45600 tok
.location
= last
->location
;
45601 lexer
->buffer
->quick_push (tok
);
45603 tok
.type
= CPP_EOF
;
45604 tok
.keyword
= RID_MAX
;
45605 tok
.location
= last
->location
;
45606 lexer
->buffer
->quick_push (tok
);
45607 lexer
->next
= parser
->lexer
;
45608 lexer
->next_token
= lexer
->buffer
->address ();
45609 lexer
->last_token
= lexer
->next_token
45610 + lexer
->buffer
->length ()
45612 lexer
->in_omp_attribute_pragma
= true;
45613 parser
->lexer
= lexer
;
45614 /* Move the current source position to that of the first token
45615 in the new lexer. */
45616 cp_lexer_set_source_position_from_token (lexer
->next_token
);
45618 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
45619 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45620 const char *kind
= IDENTIFIER_POINTER (id
);
45621 cp_lexer_consume_token (parser
->lexer
);
45624 if (strcmp (kind
, "simd") == 0)
45626 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
45627 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
45628 cp_lexer_consume_token (parser
->lexer
);
45630 omp_clause_mask mask
= OMP_DECLARE_SIMD_CLAUSE_MASK
;
45631 cl
= cp_parser_omp_all_clauses (parser
, mask
,
45632 "#pragma omp declare simd",
45635 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
45636 c
= build_tree_list (get_identifier ("omp declare simd"),
45638 TREE_CHAIN (c
) = attrs
;
45639 if (processing_template_decl
)
45640 ATTR_IS_DEPENDENT (c
) = 1;
45645 gcc_assert (strcmp (kind
, "variant") == 0);
45647 = cp_finish_omp_declare_variant (parser
, pragma_tok
,
45650 gcc_assert (parser
->lexer
!= lexer
);
45651 vec_safe_truncate (lexer
->buffer
, 0);
45653 *pa
= TREE_CHAIN (*pa
);
45656 pa
= &TREE_CHAIN (*pa
);
45659 cp_lexer_destroy (lexer
);
45661 data
->fndecl_seen
= true;
45665 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
45666 on #pragma omp declare target. Return false if errors were reported. */
45669 handle_omp_declare_target_clause (tree c
, tree t
, int device_type
)
45671 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
45672 tree at2
= lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t
));
45674 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
45676 id
= get_identifier ("omp declare target link");
45677 std::swap (at1
, at2
);
45680 id
= get_identifier ("omp declare target");
45683 error_at (OMP_CLAUSE_LOCATION (c
),
45684 "%qD specified both in declare target %<link%> and %<to%>"
45690 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
45691 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
45694 symtab_node
*node
= symtab_node::get (t
);
45697 node
->offloadable
= 1;
45698 if (ENABLE_OFFLOADING
)
45700 g
->have_offload
= true;
45701 if (is_a
<varpool_node
*> (node
))
45702 vec_safe_push (offload_vars
, t
);
45706 if (TREE_CODE (t
) != FUNCTION_DECL
)
45708 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_HOST
) != 0)
45710 tree at3
= lookup_attribute ("omp declare target host",
45711 DECL_ATTRIBUTES (t
));
45712 if (at3
== NULL_TREE
)
45714 id
= get_identifier ("omp declare target host");
45715 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
45718 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_NOHOST
) != 0)
45720 tree at3
= lookup_attribute ("omp declare target nohost",
45721 DECL_ATTRIBUTES (t
));
45722 if (at3
== NULL_TREE
)
45724 id
= get_identifier ("omp declare target nohost");
45725 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
45732 # pragma omp declare target new-line
45733 declarations and definitions
45734 # pragma omp end declare target new-line
45737 # pragma omp declare target ( extended-list ) new-line
45739 # pragma omp declare target declare-target-clauses[seq] new-line */
45741 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
45742 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
45743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
45744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
45747 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
45749 tree clauses
= NULL_TREE
;
45750 int device_type
= 0;
45751 bool only_device_type
= true;
45752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
45753 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45754 || (parser
->lexer
->in_omp_attribute_pragma
45755 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
45756 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
)))
45758 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
45759 "#pragma omp declare target", pragma_tok
);
45760 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
45762 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
45764 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
45765 cp_parser_require_pragma_eol (parser
, pragma_tok
);
45769 struct omp_declare_target_attr a
45770 = { parser
->lexer
->in_omp_attribute_pragma
};
45771 vec_safe_push (scope_chain
->omp_declare_target_attribute
, a
);
45772 cp_parser_require_pragma_eol (parser
, pragma_tok
);
45775 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
45776 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
45777 device_type
|= OMP_CLAUSE_DEVICE_TYPE_KIND (c
);
45778 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
45780 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
45782 tree t
= OMP_CLAUSE_DECL (c
);
45783 only_device_type
= false;
45784 if (!handle_omp_declare_target_clause (c
, t
, device_type
))
45786 if (VAR_OR_FUNCTION_DECL_P (t
)
45787 && DECL_LOCAL_DECL_P (t
)
45788 && DECL_LANG_SPECIFIC (t
)
45789 && DECL_LOCAL_DECL_ALIAS (t
)
45790 && DECL_LOCAL_DECL_ALIAS (t
) != error_mark_node
)
45791 handle_omp_declare_target_clause (c
, DECL_LOCAL_DECL_ALIAS (t
),
45794 if (device_type
&& only_device_type
)
45795 warning_at (OMP_CLAUSE_LOCATION (clauses
), 0,
45796 "directive with only %<device_type%> clauses ignored");
45800 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
45802 const char *p
= "";
45803 bool in_omp_attribute_pragma
= parser
->lexer
->in_omp_attribute_pragma
;
45804 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45806 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45807 p
= IDENTIFIER_POINTER (id
);
45809 if (strcmp (p
, "declare") == 0)
45811 cp_lexer_consume_token (parser
->lexer
);
45813 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45815 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45816 p
= IDENTIFIER_POINTER (id
);
45818 if (strcmp (p
, "target") == 0)
45819 cp_lexer_consume_token (parser
->lexer
);
45822 cp_parser_error (parser
, "expected %<target%>");
45823 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
45829 cp_parser_error (parser
, "expected %<declare%>");
45830 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
45833 cp_parser_require_pragma_eol (parser
, pragma_tok
);
45834 if (!vec_safe_length (scope_chain
->omp_declare_target_attribute
))
45835 error_at (pragma_tok
->location
,
45836 "%<#pragma omp end declare target%> without corresponding "
45837 "%<#pragma omp declare target%>");
45840 omp_declare_target_attr
45841 a
= scope_chain
->omp_declare_target_attribute
->pop ();
45842 if (a
.attr_syntax
!= in_omp_attribute_pragma
)
45845 error_at (pragma_tok
->location
,
45846 "%<declare target%> in attribute syntax terminated "
45847 "with %<end declare target%> in pragma syntax");
45849 error_at (pragma_tok
->location
,
45850 "%<declare target%> in pragma syntax terminated "
45851 "with %<end declare target%> in attribute syntax");
45856 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
45857 expression and optional initializer clause of
45858 #pragma omp declare reduction. We store the expression(s) as
45859 either 3, 6 or 7 special statements inside of the artificial function's
45860 body. The first two statements are DECL_EXPRs for the artificial
45861 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
45862 expression that uses those variables.
45863 If there was any INITIALIZER clause, this is followed by further statements,
45864 the fourth and fifth statements are DECL_EXPRs for the artificial
45865 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
45866 constructor variant (first token after open paren is not omp_priv),
45867 then the sixth statement is a statement with the function call expression
45868 that uses the OMP_PRIV and optionally OMP_ORIG variable.
45869 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
45870 to initialize the OMP_PRIV artificial variable and there is seventh
45871 statement, a DECL_EXPR of the OMP_PRIV statement again. */
45874 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
45876 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
45877 gcc_assert (TYPE_REF_P (type
));
45878 type
= TREE_TYPE (type
);
45879 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
45880 DECL_ARTIFICIAL (omp_out
) = 1;
45881 pushdecl (omp_out
);
45882 add_decl_expr (omp_out
);
45883 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
45884 DECL_ARTIFICIAL (omp_in
) = 1;
45886 add_decl_expr (omp_in
);
45888 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
45890 keep_next_level (true);
45891 tree block
= begin_omp_structured_block ();
45892 combiner
= cp_parser_expression (parser
);
45893 finish_expr_stmt (combiner
);
45894 block
= finish_omp_structured_block (block
);
45895 if (processing_template_decl
)
45896 block
= build_stmt (input_location
, EXPR_STMT
, block
);
45899 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
45902 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45903 if (parser
->lexer
->in_omp_attribute_pragma
45904 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
45905 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
45906 cp_lexer_consume_token (parser
->lexer
);
45908 const char *p
= "";
45909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45911 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45912 p
= IDENTIFIER_POINTER (id
);
45915 if (strcmp (p
, "initializer") == 0)
45917 cp_lexer_consume_token (parser
->lexer
);
45918 matching_parens parens
;
45919 if (!parens
.require_open (parser
))
45923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
45925 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
45926 p
= IDENTIFIER_POINTER (id
);
45929 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
45930 DECL_ARTIFICIAL (omp_priv
) = 1;
45931 pushdecl (omp_priv
);
45932 add_decl_expr (omp_priv
);
45933 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
45934 DECL_ARTIFICIAL (omp_orig
) = 1;
45935 pushdecl (omp_orig
);
45936 add_decl_expr (omp_orig
);
45938 keep_next_level (true);
45939 block
= begin_omp_structured_block ();
45942 if (strcmp (p
, "omp_priv") == 0)
45944 bool is_direct_init
, is_non_constant_init
;
45946 cp_lexer_consume_token (parser
->lexer
);
45947 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
45948 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
45949 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
45950 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
45952 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
45953 == CPP_CLOSE_PAREN
))
45955 finish_omp_structured_block (block
);
45956 error ("invalid initializer clause");
45959 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
45960 &is_non_constant_init
);
45961 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
45962 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
45966 cp_parser_parse_tentatively (parser
);
45967 /* Don't create location wrapper nodes here. */
45968 auto_suppress_location_wrappers sentinel
;
45969 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
45970 /*check_dependency_p=*/true,
45971 /*template_p=*/NULL
,
45972 /*declarator_p=*/false,
45973 /*optional_p=*/false);
45974 vec
<tree
, va_gc
> *args
;
45975 if (fn_name
== error_mark_node
45976 || cp_parser_error_occurred (parser
)
45977 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
45978 || ((args
= cp_parser_parenthesized_expression_list
45979 (parser
, non_attr
, /*cast_p=*/false,
45980 /*allow_expansion_p=*/true,
45981 /*non_constant_p=*/NULL
)),
45982 cp_parser_error_occurred (parser
)))
45984 finish_omp_structured_block (block
);
45985 cp_parser_abort_tentative_parse (parser
);
45986 cp_parser_error (parser
, "expected id-expression (arguments)");
45991 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
45992 if (arg
== omp_priv
45993 || (TREE_CODE (arg
) == ADDR_EXPR
45994 && TREE_OPERAND (arg
, 0) == omp_priv
))
45996 cp_parser_abort_tentative_parse (parser
);
45997 if (arg
== NULL_TREE
)
45998 error ("one of the initializer call arguments should be %<omp_priv%>"
45999 " or %<&omp_priv%>");
46000 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
46002 finish_expr_stmt (initializer
);
46005 block
= finish_omp_structured_block (block
);
46006 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
46007 if (processing_template_decl
)
46008 block
= build_stmt (input_location
, EXPR_STMT
, block
);
46012 add_decl_expr (omp_orig
);
46014 if (!parens
.require_close (parser
))
46018 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
46019 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
46026 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46027 initializer-clause[opt] new-line
46029 initializer-clause:
46030 initializer (omp_priv initializer)
46031 initializer (function-name (argument-list)) */
46034 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
46035 enum pragma_context
)
46037 auto_vec
<tree
> types
;
46038 enum tree_code reduc_code
= ERROR_MARK
;
46039 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
46041 cp_token
*first_token
;
46042 cp_token_cache
*cp
;
46046 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
46047 p
= obstack_alloc (&declarator_obstack
, 0);
46049 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
46052 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
46055 reduc_code
= PLUS_EXPR
;
46058 reduc_code
= MULT_EXPR
;
46061 reduc_code
= MINUS_EXPR
;
46064 reduc_code
= BIT_AND_EXPR
;
46067 reduc_code
= BIT_XOR_EXPR
;
46070 reduc_code
= BIT_IOR_EXPR
;
46073 reduc_code
= TRUTH_ANDIF_EXPR
;
46076 reduc_code
= TRUTH_ORIF_EXPR
;
46079 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
46082 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
46083 "%<|%>, %<&&%>, %<||%> or identifier");
46087 if (reduc_code
!= ERROR_MARK
)
46088 cp_lexer_consume_token (parser
->lexer
);
46090 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
46091 if (reduc_id
== error_mark_node
)
46094 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
46097 /* Types may not be defined in declare reduction type list. */
46098 const char *saved_message
;
46099 saved_message
= parser
->type_definition_forbidden_message
;
46100 parser
->type_definition_forbidden_message
46101 = G_("types may not be defined in declare reduction type list");
46102 bool saved_colon_corrects_to_scope_p
;
46103 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
46104 parser
->colon_corrects_to_scope_p
= false;
46105 bool saved_colon_doesnt_start_class_def_p
;
46106 saved_colon_doesnt_start_class_def_p
46107 = parser
->colon_doesnt_start_class_def_p
;
46108 parser
->colon_doesnt_start_class_def_p
= true;
46112 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
46113 type
= cp_parser_type_id (parser
);
46114 if (type
== error_mark_node
)
46116 else if (ARITHMETIC_TYPE_P (type
)
46117 && (orig_reduc_id
== NULL_TREE
46118 || (TREE_CODE (type
) != COMPLEX_TYPE
46119 && (id_equal (orig_reduc_id
, "min")
46120 || id_equal (orig_reduc_id
, "max")))))
46121 error_at (loc
, "predeclared arithmetic type %qT in "
46122 "%<#pragma omp declare reduction%>", type
);
46123 else if (FUNC_OR_METHOD_TYPE_P (type
)
46124 || TREE_CODE (type
) == ARRAY_TYPE
)
46125 error_at (loc
, "function or array type %qT in "
46126 "%<#pragma omp declare reduction%>", type
);
46127 else if (TYPE_REF_P (type
))
46128 error_at (loc
, "reference type %qT in "
46129 "%<#pragma omp declare reduction%>", type
);
46130 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
46131 error_at (loc
, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
46132 "type %qT in %<#pragma omp declare reduction%>", type
);
46134 types
.safe_push (type
);
46136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
46137 cp_lexer_consume_token (parser
->lexer
);
46142 /* Restore the saved message. */
46143 parser
->type_definition_forbidden_message
= saved_message
;
46144 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
46145 parser
->colon_doesnt_start_class_def_p
46146 = saved_colon_doesnt_start_class_def_p
;
46148 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
46149 || types
.is_empty ())
46152 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46156 first_token
= cp_lexer_peek_token (parser
->lexer
);
46159 FOR_EACH_VEC_ELT (types
, i
, type
)
46162 = build_function_type_list (void_type_node
,
46163 cp_build_reference_type (type
, false),
46165 tree this_reduc_id
= reduc_id
;
46166 if (!dependent_type_p (type
))
46167 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
46168 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
46169 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
46170 DECL_ARTIFICIAL (fndecl
) = 1;
46171 DECL_EXTERNAL (fndecl
) = 1;
46172 DECL_DECLARED_INLINE_P (fndecl
) = 1;
46173 DECL_IGNORED_P (fndecl
) = 1;
46174 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
46175 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
46176 DECL_ATTRIBUTES (fndecl
)
46177 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
46178 DECL_ATTRIBUTES (fndecl
));
46179 bool block_scope
= false;
46180 if (current_function_decl
)
46182 block_scope
= true;
46183 DECL_CONTEXT (fndecl
) = current_function_decl
;
46184 DECL_LOCAL_DECL_P (fndecl
) = true;
46187 if (processing_template_decl
)
46188 fndecl
= push_template_decl (fndecl
);
46192 if (!processing_template_decl
)
46195 else if (current_class_type
)
46199 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
46200 cp_lexer_consume_token (parser
->lexer
);
46201 cp
= cp_token_cache_new (first_token
,
46202 cp_lexer_peek_nth_token (parser
->lexer
,
46205 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
46206 finish_member_declaration (fndecl
);
46207 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
46208 DECL_PENDING_INLINE_P (fndecl
) = 1;
46209 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
46214 DECL_CONTEXT (fndecl
) = current_namespace
;
46215 tree d
= pushdecl (fndecl
);
46216 /* We should never meet a matched duplicate decl. */
46217 gcc_checking_assert (d
== error_mark_node
|| d
== fndecl
);
46220 tree block
= NULL_TREE
;
46222 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
46224 block
= begin_omp_structured_block ();
46227 cp_parser_push_lexer_for_tokens (parser
, cp
);
46228 parser
->lexer
->in_pragma
= true;
46231 bool ok
= cp_parser_omp_declare_reduction_exprs (fndecl
, parser
);
46234 cp_parser_pop_lexer (parser
);
46236 finish_function (/*inline_p=*/false);
46239 DECL_CONTEXT (fndecl
) = current_function_decl
;
46240 if (DECL_TEMPLATE_INFO (fndecl
))
46241 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl
)) = current_function_decl
;
46248 block
= finish_omp_structured_block (block
);
46249 if (TREE_CODE (block
) == BIND_EXPR
)
46250 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
46251 else if (TREE_CODE (block
) == STATEMENT_LIST
)
46252 DECL_SAVED_TREE (fndecl
) = block
;
46253 if (processing_template_decl
)
46254 add_decl_expr (fndecl
);
46257 cp_check_omp_declare_reduction (fndecl
);
46258 if (cp
== NULL
&& types
.length () > 1)
46259 cp
= cp_token_cache_new (first_token
,
46260 cp_lexer_peek_nth_token (parser
->lexer
, 2));
46261 if (errs
!= errorcount
)
46265 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46268 /* Free any declarators allocated. */
46269 obstack_free (&declarator_obstack
, p
);
46273 #pragma omp declare simd declare-simd-clauses[optseq] new-line
46274 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46275 initializer-clause[opt] new-line
46276 #pragma omp declare target new-line
46279 #pragma omp declare variant (identifier) match (context-selector) */
46282 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
46283 enum pragma_context context
)
46285 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46287 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46288 const char *p
= IDENTIFIER_POINTER (id
);
46290 if (strcmp (p
, "simd") == 0)
46292 cp_lexer_consume_token (parser
->lexer
);
46293 cp_parser_omp_declare_simd (parser
, pragma_tok
,
46297 if (flag_openmp
&& strcmp (p
, "variant") == 0)
46299 cp_lexer_consume_token (parser
->lexer
);
46300 cp_parser_omp_declare_simd (parser
, pragma_tok
,
46304 cp_ensure_no_omp_declare_simd (parser
);
46305 if (strcmp (p
, "reduction") == 0)
46307 cp_lexer_consume_token (parser
->lexer
);
46308 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
46312 if (!flag_openmp
) /* flag_openmp_simd */
46314 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46317 if (strcmp (p
, "target") == 0)
46319 cp_lexer_consume_token (parser
->lexer
);
46320 cp_parser_omp_declare_target (parser
, pragma_tok
);
46324 cp_parser_error (parser
, "expected %<simd%>, %<reduction%>, "
46325 "%<target%> or %<variant%>");
46326 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46331 #pragma omp requires clauses[optseq] new-line */
46334 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
46337 enum omp_requires new_req
= (enum omp_requires
) 0;
46339 location_t loc
= pragma_tok
->location
;
46340 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
46342 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46343 if ((!first
|| parser
->lexer
->in_omp_attribute_pragma
)
46344 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
46345 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
46346 cp_lexer_consume_token (parser
->lexer
);
46350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46352 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46353 const char *p
= IDENTIFIER_POINTER (id
);
46354 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
46355 enum omp_requires this_req
= (enum omp_requires
) 0;
46357 if (!strcmp (p
, "unified_address"))
46358 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
46359 else if (!strcmp (p
, "unified_shared_memory"))
46360 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
46361 else if (!strcmp (p
, "dynamic_allocators"))
46362 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
46363 else if (!strcmp (p
, "reverse_offload"))
46364 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
46365 else if (!strcmp (p
, "atomic_default_mem_order"))
46367 cp_lexer_consume_token (parser
->lexer
);
46369 matching_parens parens
;
46370 if (parens
.require_open (parser
))
46372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46374 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46375 p
= IDENTIFIER_POINTER (id
);
46377 if (!strcmp (p
, "seq_cst"))
46379 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
46380 else if (!strcmp (p
, "relaxed"))
46382 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
46383 else if (!strcmp (p
, "acq_rel"))
46385 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
46389 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
46390 "expected %<seq_cst%>, %<relaxed%> or "
46392 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
46395 case CPP_PRAGMA_EOL
:
46396 case CPP_CLOSE_PAREN
:
46399 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
46401 cp_lexer_consume_token (parser
->lexer
);
46406 cp_lexer_consume_token (parser
->lexer
);
46408 if (!parens
.require_close (parser
))
46409 cp_parser_skip_to_closing_parenthesis (parser
,
46410 /*recovering=*/true,
46411 /*or_comma=*/false,
46417 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46425 error_at (cloc
, "expected %<unified_address%>, "
46426 "%<unified_shared_memory%>, "
46427 "%<dynamic_allocators%>, "
46428 "%<reverse_offload%> "
46429 "or %<atomic_default_mem_order%> clause");
46430 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46434 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
46435 "supported yet", p
);
46437 cp_lexer_consume_token (parser
->lexer
);
46440 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
46442 if ((this_req
& new_req
) != 0)
46443 error_at (cloc
, "too many %qs clauses", p
);
46444 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
46445 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
46446 error_at (cloc
, "%qs clause used lexically after first "
46447 "target construct or offloading API", p
);
46449 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
46451 error_at (cloc
, "too many %qs clauses",
46452 "atomic_default_mem_order");
46453 this_req
= (enum omp_requires
) 0;
46455 else if ((omp_requires_mask
46456 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
46458 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
46459 " clause in a single compilation unit");
46461 = (enum omp_requires
)
46463 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
46465 else if ((omp_requires_mask
46466 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
46467 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
46468 "lexically after first %<atomic%> construct "
46469 "without memory order clause");
46470 new_req
= (enum omp_requires
) (new_req
| this_req
);
46472 = (enum omp_requires
) (omp_requires_mask
| this_req
);
46478 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46481 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
46487 #pragma omp nothing new-line */
46490 cp_parser_omp_nothing (cp_parser
*parser
, cp_token
*pragma_tok
)
46492 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46497 #pragma omp error clauses[optseq] new-line */
46500 cp_parser_omp_error (cp_parser
*parser
, cp_token
*pragma_tok
,
46501 enum pragma_context context
)
46503 int at_compilation
= -1;
46504 int severity_fatal
= -1;
46505 tree message
= NULL_TREE
;
46508 location_t loc
= pragma_tok
->location
;
46510 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
46512 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46513 if ((!first
|| parser
->lexer
->in_omp_attribute_pragma
)
46514 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
46515 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
46516 cp_lexer_consume_token (parser
->lexer
);
46520 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
46524 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
46525 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
46526 static const char *args
[] = {
46527 "execution", "compilation", "warning", "fatal"
46530 int idx
= 0, n
= -1;
46531 tree m
= NULL_TREE
;
46533 if (!strcmp (p
, "at"))
46534 v
= &at_compilation
;
46535 else if (!strcmp (p
, "severity"))
46537 v
= &severity_fatal
;
46540 else if (strcmp (p
, "message"))
46543 "expected %<at%>, %<severity%> or %<message%> clause");
46544 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46548 cp_lexer_consume_token (parser
->lexer
);
46550 matching_parens parens
;
46551 if (parens
.require_open (parser
))
46555 m
= cp_parser_assignment_expression (parser
);
46556 if (type_dependent_expression_p (m
))
46557 m
= build1 (IMPLICIT_CONV_EXPR
, const_string_type_node
, m
);
46559 m
= perform_implicit_conversion_flags (const_string_type_node
, m
,
46560 tf_warning_or_error
,
46565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46567 tree val
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46568 const char *q
= IDENTIFIER_POINTER (val
);
46570 if (!strcmp (q
, args
[idx
]))
46572 else if (!strcmp (q
, args
[idx
+ 1]))
46577 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
46578 "expected %qs or %qs", args
[idx
], args
[idx
+ 1]);
46580 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
46583 case CPP_PRAGMA_EOL
:
46584 case CPP_CLOSE_PAREN
:
46587 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
46589 cp_lexer_consume_token (parser
->lexer
);
46594 cp_lexer_consume_token (parser
->lexer
);
46597 if (!parens
.require_close (parser
))
46598 cp_parser_skip_to_closing_parenthesis (parser
,
46599 /*recovering=*/true,
46600 /*or_comma=*/false,
46608 error_at (cloc
, "too many %qs clauses", p
);
46618 error_at (cloc
, "too many %qs clauses", p
);
46628 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46632 if (at_compilation
== -1)
46633 at_compilation
= 1;
46634 if (severity_fatal
== -1)
46635 severity_fatal
= 1;
46636 if (!at_compilation
)
46638 if (context
!= pragma_compound
)
46640 error_at (loc
, "%<#pragma omp error%> with %<at(execution)%> clause "
46641 "may only be used in compound statements");
46645 = builtin_decl_explicit (severity_fatal
? BUILT_IN_GOMP_ERROR
46646 : BUILT_IN_GOMP_WARNING
);
46648 message
= build_zero_cst (const_string_type_node
);
46649 tree stmt
= build_call_expr_loc (loc
, fndecl
, 2, message
,
46650 build_all_ones_cst (size_type_node
));
46655 if (in_discarded_stmt
)
46658 const char *msg
= NULL
;
46661 msg
= c_getstr (fold_for_warn (message
));
46663 msg
= _("<message unknown at compile time>");
46666 emit_diagnostic (severity_fatal
? DK_ERROR
: DK_WARNING
, loc
, 0,
46667 "%<pragma omp error%> encountered: %s", msg
);
46669 emit_diagnostic (severity_fatal
? DK_ERROR
: DK_WARNING
, loc
, 0,
46670 "%<pragma omp error%> encountered");
46675 #pragma omp taskloop taskloop-clause[optseq] new-line
46678 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
46681 #define OMP_TASKLOOP_CLAUSE_MASK \
46682 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
46687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
46688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
46689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
46690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
46691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
46693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
46694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
46695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
46696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
46701 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
46702 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
46705 tree clauses
, sb
, ret
;
46707 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
46709 strcat (p_name
, " taskloop");
46710 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
46711 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
46713 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
46714 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
46716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
46718 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
46719 const char *p
= IDENTIFIER_POINTER (id
);
46721 if (strcmp (p
, "simd") == 0)
46723 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
46724 if (cclauses
== NULL
)
46725 cclauses
= cclauses_buf
;
46727 cp_lexer_consume_token (parser
->lexer
);
46728 if (!flag_openmp
) /* flag_openmp_simd */
46729 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
46731 sb
= begin_omp_structured_block ();
46732 save
= cp_parser_begin_omp_structured_block (parser
);
46733 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
46735 cp_parser_end_omp_structured_block (parser
, save
);
46736 tree body
= finish_omp_structured_block (sb
);
46739 ret
= make_node (OMP_TASKLOOP
);
46740 TREE_TYPE (ret
) = void_type_node
;
46741 OMP_FOR_BODY (ret
) = body
;
46742 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
46743 SET_EXPR_LOCATION (ret
, loc
);
46748 if (!flag_openmp
) /* flag_openmp_simd */
46750 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46754 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
46758 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
46759 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
46762 keep_next_level (true);
46763 sb
= begin_omp_structured_block ();
46764 save
= cp_parser_begin_omp_structured_block (parser
);
46766 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
46769 cp_parser_end_omp_structured_block (parser
, save
);
46770 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
46777 # pragma acc routine oacc-routine-clause[optseq] new-line
46778 function-definition
46780 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
46783 #define OACC_ROUTINE_CLAUSE_MASK \
46784 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
46785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
46786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
46787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
46788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
46790 /* Parse the OpenACC routine pragma. This has an optional '( name )'
46791 component, which must resolve to a declared namespace-scope
46792 function. The clauses are either processed directly (for a named
46793 function), or defered until the immediatley following declaration
46797 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
46798 enum pragma_context context
)
46800 gcc_checking_assert (context
== pragma_external
);
46801 /* The checking for "another pragma following this one" in the "no optional
46802 '( name )'" case makes sure that we dont re-enter. */
46803 gcc_checking_assert (parser
->oacc_routine
== NULL
);
46805 cp_oacc_routine_data data
;
46806 data
.error_seen
= false;
46807 data
.fndecl_seen
= false;
46808 data
.tokens
= vNULL
;
46809 data
.clauses
= NULL_TREE
;
46810 data
.loc
= pragma_tok
->location
;
46811 /* It is safe to take the address of a local variable; it will only be
46812 used while this scope is live. */
46813 parser
->oacc_routine
= &data
;
46815 /* Look for optional '( name )'. */
46816 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
46818 matching_parens parens
;
46819 parens
.consume_open (parser
); /* '(' */
46821 /* We parse the name as an id-expression. If it resolves to
46822 anything other than a non-overloaded function at namespace
46823 scope, it's an error. */
46824 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
46825 tree name
= cp_parser_id_expression (parser
,
46826 /*template_keyword_p=*/false,
46827 /*check_dependency_p=*/false,
46828 /*template_p=*/NULL
,
46829 /*declarator_p=*/false,
46830 /*optional_p=*/false);
46831 tree decl
= (identifier_p (name
)
46832 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
46834 if (name
!= error_mark_node
&& decl
== error_mark_node
)
46835 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
46837 if (decl
== error_mark_node
46838 || !parens
.require_close (parser
))
46840 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
46841 parser
->oacc_routine
= NULL
;
46846 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
46847 "#pragma acc routine",
46848 cp_lexer_peek_token (parser
->lexer
));
46849 /* The clauses are in reverse order; fix that to make later diagnostic
46850 emission easier. */
46851 data
.clauses
= nreverse (data
.clauses
);
46853 if (decl
&& is_overloaded_fn (decl
)
46854 && (TREE_CODE (decl
) != FUNCTION_DECL
46855 || DECL_FUNCTION_TEMPLATE_P (decl
)))
46857 error_at (name_loc
,
46858 "%<#pragma acc routine%> names a set of overloads");
46859 parser
->oacc_routine
= NULL
;
46863 /* Perhaps we should use the same rule as declarations in different
46865 if (!DECL_NAMESPACE_SCOPE_P (decl
))
46867 error_at (name_loc
,
46868 "%qD does not refer to a namespace scope function", decl
);
46869 parser
->oacc_routine
= NULL
;
46873 if (TREE_CODE (decl
) != FUNCTION_DECL
)
46875 error_at (name_loc
, "%qD does not refer to a function", decl
);
46876 parser
->oacc_routine
= NULL
;
46880 cp_finalize_oacc_routine (parser
, decl
, false);
46881 parser
->oacc_routine
= NULL
;
46883 else /* No optional '( name )'. */
46885 /* Store away all pragma tokens. */
46886 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
46887 cp_lexer_consume_token (parser
->lexer
);
46888 cp_parser_require_pragma_eol (parser
, pragma_tok
);
46889 struct cp_token_cache
*cp
46890 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
46891 parser
->oacc_routine
->tokens
.safe_push (cp
);
46893 /* Emit a helpful diagnostic if there's another pragma following this
46895 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
46897 cp_ensure_no_oacc_routine (parser
);
46898 data
.tokens
.release ();
46899 /* ..., and then just keep going. */
46903 /* We only have to consider the pragma_external case here. */
46904 cp_parser_declaration (parser
, NULL_TREE
);
46905 if (parser
->oacc_routine
46906 && !parser
->oacc_routine
->fndecl_seen
)
46907 cp_ensure_no_oacc_routine (parser
);
46909 parser
->oacc_routine
= NULL
;
46910 data
.tokens
.release ();
46914 /* Finalize #pragma acc routine clauses after direct declarator has
46918 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
46920 struct cp_token_cache
*ce
;
46921 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
46923 if (!data
->error_seen
&& data
->fndecl_seen
)
46925 error_at (data
->loc
,
46926 "%<#pragma acc routine%> not immediately followed by "
46927 "a single function declaration or definition");
46928 data
->error_seen
= true;
46930 if (data
->error_seen
)
46933 gcc_checking_assert (data
->tokens
.length () == 1);
46934 ce
= data
->tokens
[0];
46936 cp_parser_push_lexer_for_tokens (parser
, ce
);
46937 parser
->lexer
->in_pragma
= true;
46938 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
46940 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
46941 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
46942 parser
->oacc_routine
->clauses
46943 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
46944 "#pragma acc routine", pragma_tok
);
46945 /* The clauses are in reverse order; fix that to make later diagnostic
46946 emission easier. */
46947 parser
->oacc_routine
->clauses
= nreverse (parser
->oacc_routine
->clauses
);
46948 cp_parser_pop_lexer (parser
);
46949 /* Later, cp_finalize_oacc_routine will process the clauses. */
46950 parser
->oacc_routine
->fndecl_seen
= true;
46955 /* Apply any saved OpenACC routine clauses to a just-parsed
46959 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
46961 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
46963 /* Keep going if we're in error reporting mode. */
46964 if (parser
->oacc_routine
->error_seen
46965 || fndecl
== error_mark_node
)
46968 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
46970 if (parser
->oacc_routine
->fndecl_seen
)
46972 error_at (parser
->oacc_routine
->loc
,
46973 "%<#pragma acc routine%> not immediately followed by"
46974 " a single function declaration or definition");
46975 parser
->oacc_routine
= NULL
;
46979 cp_ensure_no_oacc_routine (parser
);
46984 = oacc_verify_routine_clauses (fndecl
, &parser
->oacc_routine
->clauses
,
46985 parser
->oacc_routine
->loc
,
46986 "#pragma acc routine");
46987 if (compatible
< 0)
46989 parser
->oacc_routine
= NULL
;
46992 if (compatible
> 0)
46997 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
46999 error_at (parser
->oacc_routine
->loc
,
47001 ? G_("%<#pragma acc routine%> must be applied before"
47003 : G_("%<#pragma acc routine%> must be applied before"
47005 parser
->oacc_routine
= NULL
;
47009 /* Set the routine's level of parallelism. */
47010 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
47011 oacc_replace_fn_attrib (fndecl
, dims
);
47013 /* Add an "omp declare target" attribute. */
47014 DECL_ATTRIBUTES (fndecl
)
47015 = tree_cons (get_identifier ("omp declare target"),
47016 parser
->oacc_routine
->clauses
,
47017 DECL_ATTRIBUTES (fndecl
));
47022 /* Main entry point to OpenMP statement pragmas. */
47025 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
47028 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
47029 omp_clause_mask
mask (0);
47031 switch (cp_parser_pragma_kind (pragma_tok
))
47033 case PRAGMA_OACC_ATOMIC
:
47034 cp_parser_omp_atomic (parser
, pragma_tok
, true);
47036 case PRAGMA_OACC_CACHE
:
47037 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
47039 case PRAGMA_OACC_DATA
:
47040 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
47042 case PRAGMA_OACC_ENTER_DATA
:
47043 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
47045 case PRAGMA_OACC_EXIT_DATA
:
47046 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
47048 case PRAGMA_OACC_HOST_DATA
:
47049 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
47051 case PRAGMA_OACC_KERNELS
:
47052 case PRAGMA_OACC_PARALLEL
:
47053 case PRAGMA_OACC_SERIAL
:
47054 strcpy (p_name
, "#pragma acc");
47055 stmt
= cp_parser_oacc_compute (parser
, pragma_tok
, p_name
, if_p
);
47057 case PRAGMA_OACC_LOOP
:
47058 strcpy (p_name
, "#pragma acc");
47059 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
47062 case PRAGMA_OACC_UPDATE
:
47063 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
47065 case PRAGMA_OACC_WAIT
:
47066 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
47068 case PRAGMA_OMP_ALLOCATE
:
47069 cp_parser_omp_allocate (parser
, pragma_tok
);
47071 case PRAGMA_OMP_ATOMIC
:
47072 cp_parser_omp_atomic (parser
, pragma_tok
, false);
47074 case PRAGMA_OMP_CRITICAL
:
47075 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
47077 case PRAGMA_OMP_DISTRIBUTE
:
47078 strcpy (p_name
, "#pragma omp");
47079 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
47082 case PRAGMA_OMP_FOR
:
47083 strcpy (p_name
, "#pragma omp");
47084 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
47087 case PRAGMA_OMP_LOOP
:
47088 strcpy (p_name
, "#pragma omp");
47089 stmt
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
47092 case PRAGMA_OMP_MASKED
:
47093 strcpy (p_name
, "#pragma omp");
47094 stmt
= cp_parser_omp_masked (parser
, pragma_tok
, p_name
, mask
, NULL
,
47097 case PRAGMA_OMP_MASTER
:
47098 strcpy (p_name
, "#pragma omp");
47099 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
47102 case PRAGMA_OMP_PARALLEL
:
47103 strcpy (p_name
, "#pragma omp");
47104 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
47107 case PRAGMA_OMP_SCOPE
:
47108 stmt
= cp_parser_omp_scope (parser
, pragma_tok
, if_p
);
47110 case PRAGMA_OMP_SECTIONS
:
47111 strcpy (p_name
, "#pragma omp");
47112 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
47114 case PRAGMA_OMP_SIMD
:
47115 strcpy (p_name
, "#pragma omp");
47116 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
47119 case PRAGMA_OMP_SINGLE
:
47120 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
47122 case PRAGMA_OMP_TASK
:
47123 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
47125 case PRAGMA_OMP_TASKGROUP
:
47126 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
47128 case PRAGMA_OMP_TASKLOOP
:
47129 strcpy (p_name
, "#pragma omp");
47130 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
47133 case PRAGMA_OMP_TEAMS
:
47134 strcpy (p_name
, "#pragma omp");
47135 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
47139 gcc_unreachable ();
47142 protected_set_expr_location (stmt
, pragma_tok
->location
);
47145 /* Transactional Memory parsing routines. */
47147 /* Parse a transaction attribute.
47153 We use this instead of cp_parser_attributes_opt for transactions to avoid
47154 the pedwarn in C++98 mode. */
47157 cp_parser_txn_attribute_opt (cp_parser
*parser
)
47160 tree attr_name
, attr
= NULL
;
47162 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
47163 return cp_parser_attributes_opt (parser
);
47165 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
47167 cp_lexer_consume_token (parser
->lexer
);
47168 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
47171 token
= cp_lexer_peek_token (parser
->lexer
);
47172 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
47174 token
= cp_lexer_consume_token (parser
->lexer
);
47176 attr_name
= (token
->type
== CPP_KEYWORD
47177 /* For keywords, use the canonical spelling,
47178 not the parsed identifier. */
47179 ? ridpointers
[(int) token
->keyword
]
47181 attr
= build_tree_list (attr_name
, NULL_TREE
);
47184 cp_parser_error (parser
, "expected identifier");
47186 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
47188 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
47192 /* Parse a __transaction_atomic or __transaction_relaxed statement.
47194 transaction-statement:
47195 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
47197 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
47201 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
47203 unsigned char old_in
= parser
->in_transaction
;
47204 unsigned char this_in
= 1, new_in
;
47205 enum rid keyword
= token
->keyword
;
47206 tree stmt
, attrs
, noex
;
47208 cp_lexer_consume_token (parser
->lexer
);
47210 if (keyword
== RID_TRANSACTION_RELAXED
47211 || keyword
== RID_SYNCHRONIZED
)
47212 this_in
|= TM_STMT_ATTR_RELAXED
;
47215 attrs
= cp_parser_txn_attribute_opt (parser
);
47217 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
47220 /* Parse a noexcept specification. */
47221 if (keyword
== RID_ATOMIC_NOEXCEPT
)
47222 noex
= boolean_true_node
;
47223 else if (keyword
== RID_ATOMIC_CANCEL
)
47225 /* cancel-and-throw is unimplemented. */
47226 sorry ("%<atomic_cancel%>");
47230 noex
= cp_parser_noexcept_specification_opt (parser
,
47231 CP_PARSER_FLAGS_NONE
,
47232 /*require_constexpr=*/true,
47233 /*consumed_expr=*/NULL
,
47234 /*return_cond=*/true);
47236 /* Keep track if we're in the lexical scope of an outer transaction. */
47237 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
47239 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
47241 parser
->in_transaction
= new_in
;
47242 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
47243 parser
->in_transaction
= old_in
;
47245 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
47250 /* Parse a __transaction_atomic or __transaction_relaxed expression.
47252 transaction-expression:
47253 __transaction_atomic txn-noexcept-spec[opt] ( expression )
47254 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
47258 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
47260 unsigned char old_in
= parser
->in_transaction
;
47261 unsigned char this_in
= 1;
47265 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
47267 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
47268 || keyword
== RID_TRANSACTION_RELAXED
);
47272 keyword
== RID_TRANSACTION_RELAXED
47273 ? G_("%<__transaction_relaxed%> without transactional memory "
47275 : G_("%<__transaction_atomic%> without transactional memory "
47276 "support enabled"));
47278 token
= cp_parser_require_keyword (parser
, keyword
,
47279 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
47280 : RT_TRANSACTION_RELAXED
));
47281 gcc_assert (token
!= NULL
);
47283 if (keyword
== RID_TRANSACTION_RELAXED
)
47284 this_in
|= TM_STMT_ATTR_RELAXED
;
47286 /* Set this early. This might mean that we allow transaction_cancel in
47287 an expression that we find out later actually has to be a constexpr.
47288 However, we expect that cxx_constant_value will be able to deal with
47289 this; also, if the noexcept has no constexpr, then what we parse next
47290 really is a transaction's body. */
47291 parser
->in_transaction
= this_in
;
47293 /* Parse a noexcept specification. */
47294 noex
= cp_parser_noexcept_specification_opt (parser
,
47295 CP_PARSER_FLAGS_NONE
,
47296 /*require_constexpr=*/false,
47298 /*return_cond=*/true);
47300 if (!noex
|| !noex_expr
47301 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
47303 matching_parens parens
;
47304 parens
.require_open (parser
);
47306 expr
= cp_parser_expression (parser
);
47307 expr
= finish_parenthesized_expr (expr
);
47309 parens
.require_close (parser
);
47313 /* The only expression that is available got parsed for the noexcept
47314 already. noexcept is true then. */
47316 noex
= boolean_true_node
;
47319 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
47320 parser
->in_transaction
= old_in
;
47322 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
47323 return error_mark_node
;
47325 return (flag_tm
? expr
: error_mark_node
);
47328 /* Parse a function-transaction-block.
47330 function-transaction-block:
47331 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
47333 __transaction_atomic txn-attribute[opt] function-try-block
47334 __transaction_relaxed ctor-initializer[opt] function-body
47335 __transaction_relaxed function-try-block
47339 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
47341 unsigned char old_in
= parser
->in_transaction
;
47342 unsigned char new_in
= 1;
47343 tree compound_stmt
, stmt
, attrs
;
47346 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
47347 || keyword
== RID_TRANSACTION_RELAXED
);
47348 token
= cp_parser_require_keyword (parser
, keyword
,
47349 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
47350 : RT_TRANSACTION_RELAXED
));
47351 gcc_assert (token
!= NULL
);
47353 if (keyword
== RID_TRANSACTION_RELAXED
)
47354 new_in
|= TM_STMT_ATTR_RELAXED
;
47357 attrs
= cp_parser_txn_attribute_opt (parser
);
47359 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
47362 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
47364 parser
->in_transaction
= new_in
;
47366 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
47367 cp_parser_function_try_block (parser
);
47369 cp_parser_ctor_initializer_opt_and_function_body
47370 (parser
, /*in_function_try_block=*/false);
47372 parser
->in_transaction
= old_in
;
47374 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
47377 /* Parse a __transaction_cancel statement.
47380 __transaction_cancel txn-attribute[opt] ;
47381 __transaction_cancel txn-attribute[opt] throw-expression ;
47383 ??? Cancel and throw is not yet implemented. */
47386 cp_parser_transaction_cancel (cp_parser
*parser
)
47389 bool is_outer
= false;
47392 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
47393 RT_TRANSACTION_CANCEL
);
47394 gcc_assert (token
!= NULL
);
47396 attrs
= cp_parser_txn_attribute_opt (parser
);
47398 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
47400 /* ??? Parse cancel-and-throw here. */
47402 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
47406 error_at (token
->location
, "%<__transaction_cancel%> without "
47407 "transactional memory support enabled");
47408 return error_mark_node
;
47410 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
47412 error_at (token
->location
, "%<__transaction_cancel%> within a "
47413 "%<__transaction_relaxed%>");
47414 return error_mark_node
;
47418 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
47419 && !is_tm_may_cancel_outer (current_function_decl
))
47421 error_at (token
->location
, "outer %<__transaction_cancel%> not "
47422 "within outer %<__transaction_atomic%>");
47423 error_at (token
->location
,
47424 " or a %<transaction_may_cancel_outer%> function");
47425 return error_mark_node
;
47428 else if (parser
->in_transaction
== 0)
47430 error_at (token
->location
, "%<__transaction_cancel%> not within "
47431 "%<__transaction_atomic%>");
47432 return error_mark_node
;
47435 stmt
= build_tm_abort_call (token
->location
, is_outer
);
47443 static GTY (()) cp_parser
*the_parser
;
47446 /* Special handling for the first token or line in the file. The first
47447 thing in the file might be #pragma GCC pch_preprocess, which loads a
47448 PCH file, which is a GC collection point. So we need to handle this
47449 first pragma without benefit of an existing lexer structure.
47451 Always returns one token to the caller in *FIRST_TOKEN. This is
47452 either the true first token of the file, or the first token after
47453 the initial pragma. */
47456 cp_parser_initial_pragma (cp_token
*first_token
)
47458 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
47461 cp_lexer_get_preprocessor_token (0, first_token
);
47464 if (first_token
->type
== CPP_STRING
)
47466 name
= first_token
->u
.value
;
47468 cp_lexer_get_preprocessor_token (0, first_token
);
47471 /* Skip to the end of the pragma. */
47472 if (first_token
->type
!= CPP_PRAGMA_EOL
)
47474 error_at (first_token
->location
,
47475 "malformed %<#pragma GCC pch_preprocess%>");
47477 cp_lexer_get_preprocessor_token (0, first_token
);
47478 while (first_token
->type
!= CPP_PRAGMA_EOL
);
47481 /* Now actually load the PCH file. */
47483 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
47485 /* Read one more token to return to our caller. We have to do this
47486 after reading the PCH file in, since its pointers have to be
47488 cp_lexer_get_preprocessor_token (0, first_token
);
47491 /* Parse a pragma GCC ivdep. */
47494 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
47496 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
47500 /* Parse a pragma GCC unroll. */
47502 static unsigned short
47503 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
47505 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
47506 tree expr
= cp_parser_constant_expression (parser
);
47507 unsigned short unroll
;
47508 expr
= maybe_constant_value (expr
);
47509 HOST_WIDE_INT lunroll
= 0;
47510 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
47511 || TREE_CODE (expr
) != INTEGER_CST
47512 || (lunroll
= tree_to_shwi (expr
)) < 0
47513 || lunroll
>= USHRT_MAX
)
47515 error_at (location
, "%<#pragma GCC unroll%> requires an"
47516 " assignment-expression that evaluates to a non-negative"
47517 " integral constant less than %u", USHRT_MAX
);
47522 unroll
= (unsigned short)lunroll
;
47526 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
47530 /* Normal parsing of a pragma token. Here we can (and must) use the
47534 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
47536 cp_token
*pragma_tok
;
47541 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
47542 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
47543 parser
->lexer
->in_pragma
= true;
47545 id
= cp_parser_pragma_kind (pragma_tok
);
47546 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
47547 cp_ensure_no_omp_declare_simd (parser
);
47550 case PRAGMA_GCC_PCH_PREPROCESS
:
47551 error_at (pragma_tok
->location
,
47552 "%<#pragma GCC pch_preprocess%> must be first");
47555 case PRAGMA_OMP_BARRIER
:
47558 case pragma_compound
:
47559 cp_parser_omp_barrier (parser
, pragma_tok
);
47562 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
47563 "used in compound statements", "omp barrier");
47571 case PRAGMA_OMP_DEPOBJ
:
47574 case pragma_compound
:
47575 cp_parser_omp_depobj (parser
, pragma_tok
);
47578 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
47579 "used in compound statements", "omp depobj");
47587 case PRAGMA_OMP_FLUSH
:
47590 case pragma_compound
:
47591 cp_parser_omp_flush (parser
, pragma_tok
);
47594 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
47595 "used in compound statements", "omp flush");
47603 case PRAGMA_OMP_TASKWAIT
:
47606 case pragma_compound
:
47607 cp_parser_omp_taskwait (parser
, pragma_tok
);
47610 error_at (pragma_tok
->location
,
47611 "%<#pragma %s%> may only be used in compound statements",
47620 case PRAGMA_OMP_TASKYIELD
:
47623 case pragma_compound
:
47624 cp_parser_omp_taskyield (parser
, pragma_tok
);
47627 error_at (pragma_tok
->location
,
47628 "%<#pragma %s%> may only be used in compound statements",
47637 case PRAGMA_OMP_CANCEL
:
47640 case pragma_compound
:
47641 cp_parser_omp_cancel (parser
, pragma_tok
);
47644 error_at (pragma_tok
->location
,
47645 "%<#pragma %s%> may only be used in compound statements",
47654 case PRAGMA_OMP_CANCELLATION_POINT
:
47655 return cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
47657 case PRAGMA_OMP_THREADPRIVATE
:
47658 cp_parser_omp_threadprivate (parser
, pragma_tok
);
47661 case PRAGMA_OMP_DECLARE
:
47662 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
47664 case PRAGMA_OACC_DECLARE
:
47665 cp_parser_oacc_declare (parser
, pragma_tok
);
47668 case PRAGMA_OACC_ENTER_DATA
:
47669 if (context
== pragma_stmt
)
47671 error_at (pragma_tok
->location
,
47672 "%<#pragma %s%> may only be used in compound statements",
47677 else if (context
!= pragma_compound
)
47679 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
47682 case PRAGMA_OACC_EXIT_DATA
:
47683 if (context
== pragma_stmt
)
47685 error_at (pragma_tok
->location
,
47686 "%<#pragma %s%> may only be used in compound statements",
47691 else if (context
!= pragma_compound
)
47693 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
47696 case PRAGMA_OACC_ROUTINE
:
47697 if (context
!= pragma_external
)
47699 error_at (pragma_tok
->location
,
47700 "%<#pragma acc routine%> must be at file scope");
47704 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
47707 case PRAGMA_OACC_UPDATE
:
47708 if (context
== pragma_stmt
)
47710 error_at (pragma_tok
->location
,
47711 "%<#pragma %s%> may only be used in compound statements",
47716 else if (context
!= pragma_compound
)
47718 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
47721 case PRAGMA_OACC_WAIT
:
47722 if (context
== pragma_stmt
)
47724 error_at (pragma_tok
->location
,
47725 "%<#pragma %s%> may only be used in compound statements",
47730 else if (context
!= pragma_compound
)
47732 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
47734 case PRAGMA_OMP_ALLOCATE
:
47735 cp_parser_omp_allocate (parser
, pragma_tok
);
47737 case PRAGMA_OACC_ATOMIC
:
47738 case PRAGMA_OACC_CACHE
:
47739 case PRAGMA_OACC_DATA
:
47740 case PRAGMA_OACC_HOST_DATA
:
47741 case PRAGMA_OACC_KERNELS
:
47742 case PRAGMA_OACC_LOOP
:
47743 case PRAGMA_OACC_PARALLEL
:
47744 case PRAGMA_OACC_SERIAL
:
47745 case PRAGMA_OMP_ATOMIC
:
47746 case PRAGMA_OMP_CRITICAL
:
47747 case PRAGMA_OMP_DISTRIBUTE
:
47748 case PRAGMA_OMP_FOR
:
47749 case PRAGMA_OMP_LOOP
:
47750 case PRAGMA_OMP_MASKED
:
47751 case PRAGMA_OMP_MASTER
:
47752 case PRAGMA_OMP_PARALLEL
:
47753 case PRAGMA_OMP_SCOPE
:
47754 case PRAGMA_OMP_SECTIONS
:
47755 case PRAGMA_OMP_SIMD
:
47756 case PRAGMA_OMP_SINGLE
:
47757 case PRAGMA_OMP_TASK
:
47758 case PRAGMA_OMP_TASKGROUP
:
47759 case PRAGMA_OMP_TASKLOOP
:
47760 case PRAGMA_OMP_TEAMS
:
47761 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
47763 stmt
= push_omp_privatization_clauses (false);
47764 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
47765 pop_omp_privatization_clauses (stmt
);
47768 case PRAGMA_OMP_REQUIRES
:
47769 if (context
!= pragma_external
)
47771 error_at (pragma_tok
->location
,
47772 "%<#pragma omp requires%> may only be used at file or "
47773 "namespace scope");
47777 return cp_parser_omp_requires (parser
, pragma_tok
);
47779 case PRAGMA_OMP_NOTHING
:
47780 cp_parser_omp_nothing (parser
, pragma_tok
);
47783 case PRAGMA_OMP_ERROR
:
47784 return cp_parser_omp_error (parser
, pragma_tok
, context
);
47786 case PRAGMA_OMP_ORDERED
:
47787 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
47789 stmt
= push_omp_privatization_clauses (false);
47790 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
47791 pop_omp_privatization_clauses (stmt
);
47794 case PRAGMA_OMP_TARGET
:
47795 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
47797 stmt
= push_omp_privatization_clauses (false);
47798 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
47799 pop_omp_privatization_clauses (stmt
);
47802 case PRAGMA_OMP_END_DECLARE_TARGET
:
47803 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
47806 case PRAGMA_OMP_SCAN
:
47807 error_at (pragma_tok
->location
,
47808 "%<#pragma omp scan%> may only be used in "
47809 "a loop construct with %<inscan%> %<reduction%> clause");
47812 case PRAGMA_OMP_SECTION
:
47813 error_at (pragma_tok
->location
,
47814 "%<#pragma omp section%> may only be used in "
47815 "%<#pragma omp sections%> construct");
47820 if (context
== pragma_external
)
47822 error_at (pragma_tok
->location
,
47823 "%<#pragma GCC ivdep%> must be inside a function");
47826 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
47827 unsigned short unroll
;
47828 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
47829 if (tok
->type
== CPP_PRAGMA
47830 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
47832 tok
= cp_lexer_consume_token (parser
->lexer
);
47833 unroll
= cp_parser_pragma_unroll (parser
, tok
);
47834 tok
= cp_lexer_peek_token (the_parser
->lexer
);
47838 if (tok
->type
!= CPP_KEYWORD
47839 || (tok
->keyword
!= RID_FOR
47840 && tok
->keyword
!= RID_WHILE
47841 && tok
->keyword
!= RID_DO
))
47843 cp_parser_error (parser
, "for, while or do statement expected");
47846 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
47850 case PRAGMA_UNROLL
:
47852 if (context
== pragma_external
)
47854 error_at (pragma_tok
->location
,
47855 "%<#pragma GCC unroll%> must be inside a function");
47858 const unsigned short unroll
47859 = cp_parser_pragma_unroll (parser
, pragma_tok
);
47861 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
47862 if (tok
->type
== CPP_PRAGMA
47863 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
47865 tok
= cp_lexer_consume_token (parser
->lexer
);
47866 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
47867 tok
= cp_lexer_peek_token (the_parser
->lexer
);
47871 if (tok
->type
!= CPP_KEYWORD
47872 || (tok
->keyword
!= RID_FOR
47873 && tok
->keyword
!= RID_WHILE
47874 && tok
->keyword
!= RID_DO
))
47876 cp_parser_error (parser
, "for, while or do statement expected");
47879 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
47884 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
47885 c_invoke_pragma_handler (id
);
47889 cp_parser_error (parser
, "expected declaration specifiers");
47893 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
47897 /* The interface the pragma parsers have to the lexer. */
47900 pragma_lex (tree
*value
, location_t
*loc
)
47902 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
47903 enum cpp_ttype ret
= tok
->type
;
47905 *value
= tok
->u
.value
;
47907 *loc
= tok
->location
;
47909 if (ret
== CPP_PRAGMA_EOL
)
47911 else if (ret
== CPP_STRING
)
47912 *value
= cp_parser_string_literal (the_parser
, false, false);
47915 if (ret
== CPP_KEYWORD
)
47917 cp_lexer_consume_token (the_parser
->lexer
);
47924 /* External interface. */
47926 /* Parse one entire translation unit. */
47929 c_parse_file (void)
47931 static bool already_called
= false;
47933 if (already_called
)
47934 fatal_error (input_location
,
47935 "multi-source compilation not implemented for C++");
47936 already_called
= true;
47938 /* cp_lexer_new_main is called before doing any GC allocation
47939 because tokenization might load a PCH file. */
47940 cp_lexer
*lexer
= cp_lexer_new_main ();
47942 the_parser
= cp_parser_new (lexer
);
47944 cp_parser_translation_unit (the_parser
);
47945 class_decl_loc_t::diag_mismatched_tags ();
47949 finish_translation_unit ();
47952 /* Create an identifier for a generic parameter type (a synthesized
47953 template parameter implied by `auto' or a concept identifier). */
47955 static GTY(()) int generic_parm_count
;
47957 make_generic_type_name ()
47960 sprintf (buf
, "auto:%d", ++generic_parm_count
);
47961 return get_identifier (buf
);
47964 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
47965 (creating a new template parameter list if necessary). Returns the newly
47966 created template type parm. */
47969 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
47971 /* A requires-clause is not a function and cannot have placeholders. */
47972 if (current_binding_level
->kind
== sk_block
)
47974 error ("placeholder type not allowed in this context");
47975 return error_mark_node
;
47978 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
47980 /* We are either continuing a function template that already contains implicit
47981 template parameters, creating a new fully-implicit function template, or
47982 extending an existing explicit function template with implicit template
47985 cp_binding_level
*const entry_scope
= current_binding_level
;
47987 bool become_template
= false;
47988 cp_binding_level
*parent_scope
= 0;
47990 if (parser
->implicit_template_scope
)
47992 gcc_assert (parser
->implicit_template_parms
);
47994 current_binding_level
= parser
->implicit_template_scope
;
47998 /* Roll back to the existing template parameter scope (in the case of
47999 extending an explicit function template) or introduce a new template
48000 parameter scope ahead of the function parameter scope (or class scope
48001 in the case of out-of-line member definitions). The function scope is
48002 added back after template parameter synthesis below. */
48004 cp_binding_level
*scope
= entry_scope
;
48006 while (scope
->kind
== sk_function_parms
)
48008 parent_scope
= scope
;
48009 scope
= scope
->level_chain
;
48011 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
48013 /* If not defining a class, then any class scope is a scope level in
48014 an out-of-line member definition. In this case simply wind back
48015 beyond the first such scope to inject the template parameter list.
48016 Otherwise wind back to the class being defined. The latter can
48017 occur in class member friend declarations such as:
48023 friend void A::foo (auto);
48026 The template parameter list synthesized for the friend declaration
48027 must be injected in the scope of 'B'. This can also occur in
48028 erroneous cases such as:
48034 void B::foo (auto) {}
48037 Here the attempted definition of 'B::foo' within 'A' is ill-formed
48038 but, nevertheless, the template parameter list synthesized for the
48039 declarator should be injected into the scope of 'A' as if the
48040 ill-formed template was specified explicitly. */
48042 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
48044 parent_scope
= scope
;
48045 scope
= scope
->level_chain
;
48049 current_binding_level
= scope
;
48051 if (scope
->kind
!= sk_template_parms
48052 || !function_being_declared_is_template_p (parser
))
48054 /* Introduce a new template parameter list for implicit template
48057 become_template
= true;
48059 parser
->implicit_template_scope
48060 = begin_scope (sk_template_parms
, NULL
);
48062 ++processing_template_decl
;
48064 parser
->fully_implicit_function_template_p
= true;
48065 ++parser
->num_template_parameter_lists
;
48069 /* Synthesize implicit template parameters at the end of the explicit
48070 template parameter list. */
48072 gcc_assert (current_template_parms
);
48074 parser
->implicit_template_scope
= scope
;
48076 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
48077 parser
->implicit_template_parms
48078 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
48082 /* Synthesize a new template parameter and track the current template
48083 parameter chain with implicit_template_parms. */
48085 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
48086 tree synth_id
= make_generic_type_name ();
48087 tree synth_tmpl_parm
;
48088 bool non_type
= false;
48090 /* Synthesize the type template parameter. */
48091 gcc_assert(!proto
|| TREE_CODE (proto
) == TYPE_DECL
);
48092 synth_tmpl_parm
= finish_template_type_parm (class_type_node
, synth_id
);
48094 if (become_template
)
48095 current_template_parms
= tree_cons (size_int (current_template_depth
+ 1),
48096 NULL_TREE
, current_template_parms
);
48098 /* Attach the constraint to the parm before processing. */
48099 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
48100 TREE_TYPE (node
) = constr
;
48102 = process_template_parm (parser
->implicit_template_parms
,
48105 /*non_type=*/non_type
,
48106 /*param_pack=*/false);
48108 /* Mark the synthetic declaration "virtual". This is used when
48109 comparing template-heads to determine if whether an abbreviated
48110 function template is equivalent to an explicit template.
48112 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
48113 DECL_VIRTUAL_P (TREE_VALUE (new_parm
)) = true;
48115 // Chain the new parameter to the list of implicit parameters.
48116 if (parser
->implicit_template_parms
)
48117 parser
->implicit_template_parms
48118 = TREE_CHAIN (parser
->implicit_template_parms
);
48120 parser
->implicit_template_parms
= new_parm
;
48122 tree new_decl
= get_local_decls ();
48124 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
48125 new_decl
= DECL_INITIAL (new_decl
);
48127 /* If creating a fully implicit function template, start the new implicit
48128 template parameter list with this synthesized type, otherwise grow the
48129 current template parameter list. */
48131 if (become_template
)
48133 parent_scope
->level_chain
= current_binding_level
;
48135 tree new_parms
= make_tree_vec (1);
48136 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
48137 TREE_VALUE (current_template_parms
) = new_parms
;
48141 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
48142 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
48143 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
48144 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
48147 /* If the new parameter was constrained, we need to add that to the
48148 constraints in the template parameter list. */
48149 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
48151 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
48152 reqs
= combine_constraint_expressions (reqs
, req
);
48153 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
48156 current_binding_level
= entry_scope
;
48161 /* Finish the declaration of a fully implicit function template. Such a
48162 template has no explicit template parameter list so has not been through the
48163 normal template head and tail processing. synthesize_implicit_template_parm
48164 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
48165 provided if the declaration is a class member such that its template
48166 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
48167 form is returned. Otherwise NULL_TREE is returned. */
48170 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
48172 gcc_assert (parser
->fully_implicit_function_template_p
);
48174 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
48175 && DECL_VIRTUAL_P (member_decl_opt
))
48177 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
48178 "implicit templates may not be %<virtual%>");
48179 DECL_VIRTUAL_P (member_decl_opt
) = false;
48182 if (member_decl_opt
)
48183 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
48184 end_template_decl ();
48186 parser
->fully_implicit_function_template_p
= false;
48187 parser
->implicit_template_parms
= 0;
48188 parser
->implicit_template_scope
= 0;
48189 --parser
->num_template_parameter_lists
;
48191 return member_decl_opt
;
48194 /* Like finish_fully_implicit_template, but to be used in error
48195 recovery, rearranging scopes so that we restore the state we had
48196 before synthesize_implicit_template_parm inserted the implement
48197 template parms scope. */
48200 abort_fully_implicit_template (cp_parser
*parser
)
48202 cp_binding_level
*return_to_scope
= current_binding_level
;
48204 if (parser
->implicit_template_scope
48205 && return_to_scope
!= parser
->implicit_template_scope
)
48207 cp_binding_level
*child
= return_to_scope
;
48208 for (cp_binding_level
*scope
= child
->level_chain
;
48209 scope
!= parser
->implicit_template_scope
;
48210 scope
= child
->level_chain
)
48212 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
48213 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
48214 current_binding_level
= parser
->implicit_template_scope
;
48217 return_to_scope
= return_to_scope
->level_chain
;
48219 finish_fully_implicit_template (parser
, NULL
);
48221 gcc_assert (current_binding_level
== return_to_scope
);
48224 /* Helper function for diagnostics that have complained about things
48225 being used with 'extern "C"' linkage.
48227 Attempt to issue a note showing where the 'extern "C"' linkage began. */
48230 maybe_show_extern_c_location (void)
48232 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
48233 inform (the_parser
->innermost_linkage_specification_location
,
48234 "%<extern \"C\"%> linkage started here");
48237 #include "gt-cp-parser.h"